Deleted Added
full compact
StmtPrinter.cpp (263508) StmtPrinter.cpp (266715)
1//===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which
11// pretty print the AST back out to C code.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/Attr.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclObjC.h"
19#include "clang/AST/DeclTemplate.h"
20#include "clang/AST/Expr.h"
21#include "clang/AST/ExprCXX.h"
22#include "clang/AST/PrettyPrinter.h"
23#include "clang/AST/StmtVisitor.h"
24#include "clang/Basic/CharInfo.h"
25#include "llvm/ADT/SmallString.h"
26#include "llvm/Support/Format.h"
27using namespace clang;
28
29//===----------------------------------------------------------------------===//
30// StmtPrinter Visitor
31//===----------------------------------------------------------------------===//
32
33namespace {
34 class StmtPrinter : public StmtVisitor<StmtPrinter> {
35 raw_ostream &OS;
36 unsigned IndentLevel;
37 clang::PrinterHelper* Helper;
38 PrintingPolicy Policy;
39
40 public:
41 StmtPrinter(raw_ostream &os, PrinterHelper* helper,
42 const PrintingPolicy &Policy,
43 unsigned Indentation = 0)
44 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy) {}
45
46 void PrintStmt(Stmt *S) {
47 PrintStmt(S, Policy.Indentation);
48 }
49
50 void PrintStmt(Stmt *S, int SubIndent) {
51 IndentLevel += SubIndent;
52 if (S && isa<Expr>(S)) {
53 // If this is an expr used in a stmt context, indent and newline it.
54 Indent();
55 Visit(S);
56 OS << ";\n";
57 } else if (S) {
58 Visit(S);
59 } else {
60 Indent() << "<<<NULL STATEMENT>>>\n";
61 }
62 IndentLevel -= SubIndent;
63 }
64
65 void PrintRawCompoundStmt(CompoundStmt *S);
66 void PrintRawDecl(Decl *D);
67 void PrintRawDeclStmt(const DeclStmt *S);
68 void PrintRawIfStmt(IfStmt *If);
69 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
70 void PrintCallArgs(CallExpr *E);
71 void PrintRawSEHExceptHandler(SEHExceptStmt *S);
72 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
73
74 void PrintExpr(Expr *E) {
75 if (E)
76 Visit(E);
77 else
78 OS << "<null expr>";
79 }
80
81 raw_ostream &Indent(int Delta = 0) {
82 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
83 OS << " ";
84 return OS;
85 }
86
87 void Visit(Stmt* S) {
88 if (Helper && Helper->handledStmt(S,OS))
89 return;
90 else StmtVisitor<StmtPrinter>::Visit(S);
91 }
92
93 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
94 Indent() << "<<unknown stmt type>>\n";
95 }
96 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
97 OS << "<<unknown expr type>>";
98 }
99 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
100
101#define ABSTRACT_STMT(CLASS)
102#define STMT(CLASS, PARENT) \
103 void Visit##CLASS(CLASS *Node);
104#include "clang/AST/StmtNodes.inc"
105 };
106}
107
108//===----------------------------------------------------------------------===//
109// Stmt printing methods.
110//===----------------------------------------------------------------------===//
111
112/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
113/// with no newline after the }.
114void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
115 OS << "{\n";
116 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
117 I != E; ++I)
118 PrintStmt(*I);
119
120 Indent() << "}";
121}
122
123void StmtPrinter::PrintRawDecl(Decl *D) {
124 D->print(OS, Policy, IndentLevel);
125}
126
127void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
128 DeclStmt::const_decl_iterator Begin = S->decl_begin(), End = S->decl_end();
129 SmallVector<Decl*, 2> Decls;
130 for ( ; Begin != End; ++Begin)
131 Decls.push_back(*Begin);
132
133 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
134}
135
136void StmtPrinter::VisitNullStmt(NullStmt *Node) {
137 Indent() << ";\n";
138}
139
140void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
141 Indent();
142 PrintRawDeclStmt(Node);
143 OS << ";\n";
144}
145
146void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
147 Indent();
148 PrintRawCompoundStmt(Node);
149 OS << "\n";
150}
151
152void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
153 Indent(-1) << "case ";
154 PrintExpr(Node->getLHS());
155 if (Node->getRHS()) {
156 OS << " ... ";
157 PrintExpr(Node->getRHS());
158 }
159 OS << ":\n";
160
161 PrintStmt(Node->getSubStmt(), 0);
162}
163
164void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
165 Indent(-1) << "default:\n";
166 PrintStmt(Node->getSubStmt(), 0);
167}
168
169void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
170 Indent(-1) << Node->getName() << ":\n";
171 PrintStmt(Node->getSubStmt(), 0);
172}
173
174void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
175 OS << "[[";
176 bool first = true;
177 for (ArrayRef<const Attr*>::iterator it = Node->getAttrs().begin(),
178 end = Node->getAttrs().end();
179 it != end; ++it) {
180 if (!first) {
181 OS << ", ";
182 first = false;
183 }
184 // TODO: check this
185 (*it)->printPretty(OS, Policy);
186 }
187 OS << "]] ";
188 PrintStmt(Node->getSubStmt(), 0);
189}
190
191void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
192 OS << "if (";
193 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
194 PrintRawDeclStmt(DS);
195 else
196 PrintExpr(If->getCond());
197 OS << ')';
198
199 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
200 OS << ' ';
201 PrintRawCompoundStmt(CS);
202 OS << (If->getElse() ? ' ' : '\n');
203 } else {
204 OS << '\n';
205 PrintStmt(If->getThen());
206 if (If->getElse()) Indent();
207 }
208
209 if (Stmt *Else = If->getElse()) {
210 OS << "else";
211
212 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
213 OS << ' ';
214 PrintRawCompoundStmt(CS);
215 OS << '\n';
216 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
217 OS << ' ';
218 PrintRawIfStmt(ElseIf);
219 } else {
220 OS << '\n';
221 PrintStmt(If->getElse());
222 }
223 }
224}
225
226void StmtPrinter::VisitIfStmt(IfStmt *If) {
227 Indent();
228 PrintRawIfStmt(If);
229}
230
231void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
232 Indent() << "switch (";
233 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
234 PrintRawDeclStmt(DS);
235 else
236 PrintExpr(Node->getCond());
237 OS << ")";
238
239 // Pretty print compoundstmt bodies (very common).
240 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
241 OS << " ";
242 PrintRawCompoundStmt(CS);
243 OS << "\n";
244 } else {
245 OS << "\n";
246 PrintStmt(Node->getBody());
247 }
248}
249
250void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
251 Indent() << "while (";
252 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
253 PrintRawDeclStmt(DS);
254 else
255 PrintExpr(Node->getCond());
256 OS << ")\n";
257 PrintStmt(Node->getBody());
258}
259
260void StmtPrinter::VisitDoStmt(DoStmt *Node) {
261 Indent() << "do ";
262 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
263 PrintRawCompoundStmt(CS);
264 OS << " ";
265 } else {
266 OS << "\n";
267 PrintStmt(Node->getBody());
268 Indent();
269 }
270
271 OS << "while (";
272 PrintExpr(Node->getCond());
273 OS << ");\n";
274}
275
276void StmtPrinter::VisitForStmt(ForStmt *Node) {
277 Indent() << "for (";
278 if (Node->getInit()) {
279 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
280 PrintRawDeclStmt(DS);
281 else
282 PrintExpr(cast<Expr>(Node->getInit()));
283 }
284 OS << ";";
285 if (Node->getCond()) {
286 OS << " ";
287 PrintExpr(Node->getCond());
288 }
289 OS << ";";
290 if (Node->getInc()) {
291 OS << " ";
292 PrintExpr(Node->getInc());
293 }
294 OS << ") ";
295
296 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
297 PrintRawCompoundStmt(CS);
298 OS << "\n";
299 } else {
300 OS << "\n";
301 PrintStmt(Node->getBody());
302 }
303}
304
305void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
306 Indent() << "for (";
307 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
308 PrintRawDeclStmt(DS);
309 else
310 PrintExpr(cast<Expr>(Node->getElement()));
311 OS << " in ";
312 PrintExpr(Node->getCollection());
313 OS << ") ";
314
315 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
316 PrintRawCompoundStmt(CS);
317 OS << "\n";
318 } else {
319 OS << "\n";
320 PrintStmt(Node->getBody());
321 }
322}
323
324void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
325 Indent() << "for (";
326 PrintingPolicy SubPolicy(Policy);
327 SubPolicy.SuppressInitializers = true;
328 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
329 OS << " : ";
330 PrintExpr(Node->getRangeInit());
331 OS << ") {\n";
332 PrintStmt(Node->getBody());
333 Indent() << "}\n";
334}
335
336void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
337 Indent();
338 if (Node->isIfExists())
339 OS << "__if_exists (";
340 else
341 OS << "__if_not_exists (";
342
343 if (NestedNameSpecifier *Qualifier
344 = Node->getQualifierLoc().getNestedNameSpecifier())
345 Qualifier->print(OS, Policy);
346
347 OS << Node->getNameInfo() << ") ";
348
349 PrintRawCompoundStmt(Node->getSubStmt());
350}
351
352void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
353 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
354}
355
356void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
357 Indent() << "goto *";
358 PrintExpr(Node->getTarget());
359 OS << ";\n";
360}
361
362void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
363 Indent() << "continue;\n";
364}
365
366void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
367 Indent() << "break;\n";
368}
369
370
371void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
372 Indent() << "return";
373 if (Node->getRetValue()) {
374 OS << " ";
375 PrintExpr(Node->getRetValue());
376 }
377 OS << ";\n";
378}
379
380
381void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
382 Indent() << "asm ";
383
384 if (Node->isVolatile())
385 OS << "volatile ";
386
387 OS << "(";
388 VisitStringLiteral(Node->getAsmString());
389
390 // Outputs
391 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
392 Node->getNumClobbers() != 0)
393 OS << " : ";
394
395 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
396 if (i != 0)
397 OS << ", ";
398
399 if (!Node->getOutputName(i).empty()) {
400 OS << '[';
401 OS << Node->getOutputName(i);
402 OS << "] ";
403 }
404
405 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
406 OS << " ";
407 Visit(Node->getOutputExpr(i));
408 }
409
410 // Inputs
411 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
412 OS << " : ";
413
414 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
415 if (i != 0)
416 OS << ", ";
417
418 if (!Node->getInputName(i).empty()) {
419 OS << '[';
420 OS << Node->getInputName(i);
421 OS << "] ";
422 }
423
424 VisitStringLiteral(Node->getInputConstraintLiteral(i));
425 OS << " ";
426 Visit(Node->getInputExpr(i));
427 }
428
429 // Clobbers
430 if (Node->getNumClobbers() != 0)
431 OS << " : ";
432
433 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
434 if (i != 0)
435 OS << ", ";
436
437 VisitStringLiteral(Node->getClobberStringLiteral(i));
438 }
439
440 OS << ");\n";
441}
442
443void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
444 // FIXME: Implement MS style inline asm statement printer.
445 Indent() << "__asm ";
446 if (Node->hasBraces())
447 OS << "{\n";
448 OS << Node->getAsmString() << "\n";
449 if (Node->hasBraces())
450 Indent() << "}\n";
451}
452
453void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
454 PrintStmt(Node->getCapturedDecl()->getBody());
455}
456
457void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
458 Indent() << "@try";
459 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
460 PrintRawCompoundStmt(TS);
461 OS << "\n";
462 }
463
464 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
465 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
466 Indent() << "@catch(";
467 if (catchStmt->getCatchParamDecl()) {
468 if (Decl *DS = catchStmt->getCatchParamDecl())
469 PrintRawDecl(DS);
470 }
471 OS << ")";
472 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
473 PrintRawCompoundStmt(CS);
474 OS << "\n";
475 }
476 }
477
478 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
479 Node->getFinallyStmt())) {
480 Indent() << "@finally";
481 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
482 OS << "\n";
483 }
484}
485
486void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
487}
488
489void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
490 Indent() << "@catch (...) { /* todo */ } \n";
491}
492
493void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
494 Indent() << "@throw";
495 if (Node->getThrowExpr()) {
496 OS << " ";
497 PrintExpr(Node->getThrowExpr());
498 }
499 OS << ";\n";
500}
501
502void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
503 Indent() << "@synchronized (";
504 PrintExpr(Node->getSynchExpr());
505 OS << ")";
506 PrintRawCompoundStmt(Node->getSynchBody());
507 OS << "\n";
508}
509
510void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
511 Indent() << "@autoreleasepool";
512 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
513 OS << "\n";
514}
515
516void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
517 OS << "catch (";
518 if (Decl *ExDecl = Node->getExceptionDecl())
519 PrintRawDecl(ExDecl);
520 else
521 OS << "...";
522 OS << ") ";
523 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
524}
525
526void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
527 Indent();
528 PrintRawCXXCatchStmt(Node);
529 OS << "\n";
530}
531
532void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
533 Indent() << "try ";
534 PrintRawCompoundStmt(Node->getTryBlock());
535 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
536 OS << " ";
537 PrintRawCXXCatchStmt(Node->getHandler(i));
538 }
539 OS << "\n";
540}
541
542void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
543 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
544 PrintRawCompoundStmt(Node->getTryBlock());
545 SEHExceptStmt *E = Node->getExceptHandler();
546 SEHFinallyStmt *F = Node->getFinallyHandler();
547 if(E)
548 PrintRawSEHExceptHandler(E);
549 else {
550 assert(F && "Must have a finally block...");
551 PrintRawSEHFinallyStmt(F);
552 }
553 OS << "\n";
554}
555
556void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
557 OS << "__finally ";
558 PrintRawCompoundStmt(Node->getBlock());
559 OS << "\n";
560}
561
562void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
563 OS << "__except (";
564 VisitExpr(Node->getFilterExpr());
565 OS << ")\n";
566 PrintRawCompoundStmt(Node->getBlock());
567 OS << "\n";
568}
569
570void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
571 Indent();
572 PrintRawSEHExceptHandler(Node);
573 OS << "\n";
574}
575
576void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
577 Indent();
578 PrintRawSEHFinallyStmt(Node);
579 OS << "\n";
580}
581
582//===----------------------------------------------------------------------===//
583// OpenMP clauses printing methods
584//===----------------------------------------------------------------------===//
585
586namespace {
587class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> {
588 raw_ostream &OS;
589 /// \brief Process clauses with list of variables.
590 template <typename T>
591 void VisitOMPClauseList(T *Node, char StartSym);
592public:
593 OMPClausePrinter(raw_ostream &OS) : OS(OS) { }
594#define OPENMP_CLAUSE(Name, Class) \
595 void Visit##Class(Class *S);
596#include "clang/Basic/OpenMPKinds.def"
597};
598
599void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
600 OS << "default("
601 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
602 << ")";
603}
604
605template<typename T>
606void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
607 for (typename T::varlist_iterator I = Node->varlist_begin(),
608 E = Node->varlist_end();
609 I != E; ++I)
610 OS << (I == Node->varlist_begin() ? StartSym : ',')
611 << *cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
612}
613
614void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
615 if (!Node->varlist_empty()) {
616 OS << "private";
617 VisitOMPClauseList(Node, '(');
618 OS << ")";
619 }
620}
621
622void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
623 if (!Node->varlist_empty()) {
624 OS << "firstprivate";
625 VisitOMPClauseList(Node, '(');
626 OS << ")";
627 }
628}
629
630void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
631 if (!Node->varlist_empty()) {
632 OS << "shared";
633 VisitOMPClauseList(Node, '(');
634 OS << ")";
635 }
636}
637
638}
639
640//===----------------------------------------------------------------------===//
641// OpenMP directives printing methods
642//===----------------------------------------------------------------------===//
643
644void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
645 Indent() << "#pragma omp parallel ";
646
647 OMPClausePrinter Printer(OS);
648 ArrayRef<OMPClause *> Clauses = Node->clauses();
649 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
650 I != E; ++I)
651 if (*I && !(*I)->isImplicit()) {
652 Printer.Visit(*I);
653 OS << ' ';
654 }
655 OS << "\n";
656 if (Node->getAssociatedStmt()) {
657 assert(isa<CapturedStmt>(Node->getAssociatedStmt()) &&
658 "Expected captured statement!");
659 Stmt *CS = cast<CapturedStmt>(Node->getAssociatedStmt())->getCapturedStmt();
660 PrintStmt(CS);
661 }
662}
663//===----------------------------------------------------------------------===//
664// Expr printing methods.
665//===----------------------------------------------------------------------===//
666
667void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
668 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
669 Qualifier->print(OS, Policy);
670 if (Node->hasTemplateKeyword())
671 OS << "template ";
672 OS << Node->getNameInfo();
673 if (Node->hasExplicitTemplateArgs())
674 TemplateSpecializationType::PrintTemplateArgumentList(
675 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
676}
677
678void StmtPrinter::VisitDependentScopeDeclRefExpr(
679 DependentScopeDeclRefExpr *Node) {
680 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
681 Qualifier->print(OS, Policy);
682 if (Node->hasTemplateKeyword())
683 OS << "template ";
684 OS << Node->getNameInfo();
685 if (Node->hasExplicitTemplateArgs())
686 TemplateSpecializationType::PrintTemplateArgumentList(
687 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
688}
689
690void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
691 if (Node->getQualifier())
692 Node->getQualifier()->print(OS, Policy);
693 if (Node->hasTemplateKeyword())
694 OS << "template ";
695 OS << Node->getNameInfo();
696 if (Node->hasExplicitTemplateArgs())
697 TemplateSpecializationType::PrintTemplateArgumentList(
698 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
699}
700
701void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
702 if (Node->getBase()) {
703 PrintExpr(Node->getBase());
704 OS << (Node->isArrow() ? "->" : ".");
705 }
706 OS << *Node->getDecl();
707}
708
709void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
710 if (Node->isSuperReceiver())
711 OS << "super.";
1//===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which
11// pretty print the AST back out to C code.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/Attr.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclObjC.h"
19#include "clang/AST/DeclTemplate.h"
20#include "clang/AST/Expr.h"
21#include "clang/AST/ExprCXX.h"
22#include "clang/AST/PrettyPrinter.h"
23#include "clang/AST/StmtVisitor.h"
24#include "clang/Basic/CharInfo.h"
25#include "llvm/ADT/SmallString.h"
26#include "llvm/Support/Format.h"
27using namespace clang;
28
29//===----------------------------------------------------------------------===//
30// StmtPrinter Visitor
31//===----------------------------------------------------------------------===//
32
33namespace {
34 class StmtPrinter : public StmtVisitor<StmtPrinter> {
35 raw_ostream &OS;
36 unsigned IndentLevel;
37 clang::PrinterHelper* Helper;
38 PrintingPolicy Policy;
39
40 public:
41 StmtPrinter(raw_ostream &os, PrinterHelper* helper,
42 const PrintingPolicy &Policy,
43 unsigned Indentation = 0)
44 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy) {}
45
46 void PrintStmt(Stmt *S) {
47 PrintStmt(S, Policy.Indentation);
48 }
49
50 void PrintStmt(Stmt *S, int SubIndent) {
51 IndentLevel += SubIndent;
52 if (S && isa<Expr>(S)) {
53 // If this is an expr used in a stmt context, indent and newline it.
54 Indent();
55 Visit(S);
56 OS << ";\n";
57 } else if (S) {
58 Visit(S);
59 } else {
60 Indent() << "<<<NULL STATEMENT>>>\n";
61 }
62 IndentLevel -= SubIndent;
63 }
64
65 void PrintRawCompoundStmt(CompoundStmt *S);
66 void PrintRawDecl(Decl *D);
67 void PrintRawDeclStmt(const DeclStmt *S);
68 void PrintRawIfStmt(IfStmt *If);
69 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
70 void PrintCallArgs(CallExpr *E);
71 void PrintRawSEHExceptHandler(SEHExceptStmt *S);
72 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
73
74 void PrintExpr(Expr *E) {
75 if (E)
76 Visit(E);
77 else
78 OS << "<null expr>";
79 }
80
81 raw_ostream &Indent(int Delta = 0) {
82 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
83 OS << " ";
84 return OS;
85 }
86
87 void Visit(Stmt* S) {
88 if (Helper && Helper->handledStmt(S,OS))
89 return;
90 else StmtVisitor<StmtPrinter>::Visit(S);
91 }
92
93 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
94 Indent() << "<<unknown stmt type>>\n";
95 }
96 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
97 OS << "<<unknown expr type>>";
98 }
99 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
100
101#define ABSTRACT_STMT(CLASS)
102#define STMT(CLASS, PARENT) \
103 void Visit##CLASS(CLASS *Node);
104#include "clang/AST/StmtNodes.inc"
105 };
106}
107
108//===----------------------------------------------------------------------===//
109// Stmt printing methods.
110//===----------------------------------------------------------------------===//
111
112/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
113/// with no newline after the }.
114void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
115 OS << "{\n";
116 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
117 I != E; ++I)
118 PrintStmt(*I);
119
120 Indent() << "}";
121}
122
123void StmtPrinter::PrintRawDecl(Decl *D) {
124 D->print(OS, Policy, IndentLevel);
125}
126
127void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
128 DeclStmt::const_decl_iterator Begin = S->decl_begin(), End = S->decl_end();
129 SmallVector<Decl*, 2> Decls;
130 for ( ; Begin != End; ++Begin)
131 Decls.push_back(*Begin);
132
133 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
134}
135
136void StmtPrinter::VisitNullStmt(NullStmt *Node) {
137 Indent() << ";\n";
138}
139
140void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
141 Indent();
142 PrintRawDeclStmt(Node);
143 OS << ";\n";
144}
145
146void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
147 Indent();
148 PrintRawCompoundStmt(Node);
149 OS << "\n";
150}
151
152void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
153 Indent(-1) << "case ";
154 PrintExpr(Node->getLHS());
155 if (Node->getRHS()) {
156 OS << " ... ";
157 PrintExpr(Node->getRHS());
158 }
159 OS << ":\n";
160
161 PrintStmt(Node->getSubStmt(), 0);
162}
163
164void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
165 Indent(-1) << "default:\n";
166 PrintStmt(Node->getSubStmt(), 0);
167}
168
169void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
170 Indent(-1) << Node->getName() << ":\n";
171 PrintStmt(Node->getSubStmt(), 0);
172}
173
174void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
175 OS << "[[";
176 bool first = true;
177 for (ArrayRef<const Attr*>::iterator it = Node->getAttrs().begin(),
178 end = Node->getAttrs().end();
179 it != end; ++it) {
180 if (!first) {
181 OS << ", ";
182 first = false;
183 }
184 // TODO: check this
185 (*it)->printPretty(OS, Policy);
186 }
187 OS << "]] ";
188 PrintStmt(Node->getSubStmt(), 0);
189}
190
191void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
192 OS << "if (";
193 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
194 PrintRawDeclStmt(DS);
195 else
196 PrintExpr(If->getCond());
197 OS << ')';
198
199 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
200 OS << ' ';
201 PrintRawCompoundStmt(CS);
202 OS << (If->getElse() ? ' ' : '\n');
203 } else {
204 OS << '\n';
205 PrintStmt(If->getThen());
206 if (If->getElse()) Indent();
207 }
208
209 if (Stmt *Else = If->getElse()) {
210 OS << "else";
211
212 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
213 OS << ' ';
214 PrintRawCompoundStmt(CS);
215 OS << '\n';
216 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
217 OS << ' ';
218 PrintRawIfStmt(ElseIf);
219 } else {
220 OS << '\n';
221 PrintStmt(If->getElse());
222 }
223 }
224}
225
226void StmtPrinter::VisitIfStmt(IfStmt *If) {
227 Indent();
228 PrintRawIfStmt(If);
229}
230
231void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
232 Indent() << "switch (";
233 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
234 PrintRawDeclStmt(DS);
235 else
236 PrintExpr(Node->getCond());
237 OS << ")";
238
239 // Pretty print compoundstmt bodies (very common).
240 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
241 OS << " ";
242 PrintRawCompoundStmt(CS);
243 OS << "\n";
244 } else {
245 OS << "\n";
246 PrintStmt(Node->getBody());
247 }
248}
249
250void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
251 Indent() << "while (";
252 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
253 PrintRawDeclStmt(DS);
254 else
255 PrintExpr(Node->getCond());
256 OS << ")\n";
257 PrintStmt(Node->getBody());
258}
259
260void StmtPrinter::VisitDoStmt(DoStmt *Node) {
261 Indent() << "do ";
262 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
263 PrintRawCompoundStmt(CS);
264 OS << " ";
265 } else {
266 OS << "\n";
267 PrintStmt(Node->getBody());
268 Indent();
269 }
270
271 OS << "while (";
272 PrintExpr(Node->getCond());
273 OS << ");\n";
274}
275
276void StmtPrinter::VisitForStmt(ForStmt *Node) {
277 Indent() << "for (";
278 if (Node->getInit()) {
279 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
280 PrintRawDeclStmt(DS);
281 else
282 PrintExpr(cast<Expr>(Node->getInit()));
283 }
284 OS << ";";
285 if (Node->getCond()) {
286 OS << " ";
287 PrintExpr(Node->getCond());
288 }
289 OS << ";";
290 if (Node->getInc()) {
291 OS << " ";
292 PrintExpr(Node->getInc());
293 }
294 OS << ") ";
295
296 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
297 PrintRawCompoundStmt(CS);
298 OS << "\n";
299 } else {
300 OS << "\n";
301 PrintStmt(Node->getBody());
302 }
303}
304
305void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
306 Indent() << "for (";
307 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
308 PrintRawDeclStmt(DS);
309 else
310 PrintExpr(cast<Expr>(Node->getElement()));
311 OS << " in ";
312 PrintExpr(Node->getCollection());
313 OS << ") ";
314
315 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
316 PrintRawCompoundStmt(CS);
317 OS << "\n";
318 } else {
319 OS << "\n";
320 PrintStmt(Node->getBody());
321 }
322}
323
324void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
325 Indent() << "for (";
326 PrintingPolicy SubPolicy(Policy);
327 SubPolicy.SuppressInitializers = true;
328 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
329 OS << " : ";
330 PrintExpr(Node->getRangeInit());
331 OS << ") {\n";
332 PrintStmt(Node->getBody());
333 Indent() << "}\n";
334}
335
336void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
337 Indent();
338 if (Node->isIfExists())
339 OS << "__if_exists (";
340 else
341 OS << "__if_not_exists (";
342
343 if (NestedNameSpecifier *Qualifier
344 = Node->getQualifierLoc().getNestedNameSpecifier())
345 Qualifier->print(OS, Policy);
346
347 OS << Node->getNameInfo() << ") ";
348
349 PrintRawCompoundStmt(Node->getSubStmt());
350}
351
352void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
353 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
354}
355
356void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
357 Indent() << "goto *";
358 PrintExpr(Node->getTarget());
359 OS << ";\n";
360}
361
362void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
363 Indent() << "continue;\n";
364}
365
366void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
367 Indent() << "break;\n";
368}
369
370
371void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
372 Indent() << "return";
373 if (Node->getRetValue()) {
374 OS << " ";
375 PrintExpr(Node->getRetValue());
376 }
377 OS << ";\n";
378}
379
380
381void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
382 Indent() << "asm ";
383
384 if (Node->isVolatile())
385 OS << "volatile ";
386
387 OS << "(";
388 VisitStringLiteral(Node->getAsmString());
389
390 // Outputs
391 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
392 Node->getNumClobbers() != 0)
393 OS << " : ";
394
395 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
396 if (i != 0)
397 OS << ", ";
398
399 if (!Node->getOutputName(i).empty()) {
400 OS << '[';
401 OS << Node->getOutputName(i);
402 OS << "] ";
403 }
404
405 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
406 OS << " ";
407 Visit(Node->getOutputExpr(i));
408 }
409
410 // Inputs
411 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
412 OS << " : ";
413
414 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
415 if (i != 0)
416 OS << ", ";
417
418 if (!Node->getInputName(i).empty()) {
419 OS << '[';
420 OS << Node->getInputName(i);
421 OS << "] ";
422 }
423
424 VisitStringLiteral(Node->getInputConstraintLiteral(i));
425 OS << " ";
426 Visit(Node->getInputExpr(i));
427 }
428
429 // Clobbers
430 if (Node->getNumClobbers() != 0)
431 OS << " : ";
432
433 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
434 if (i != 0)
435 OS << ", ";
436
437 VisitStringLiteral(Node->getClobberStringLiteral(i));
438 }
439
440 OS << ");\n";
441}
442
443void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
444 // FIXME: Implement MS style inline asm statement printer.
445 Indent() << "__asm ";
446 if (Node->hasBraces())
447 OS << "{\n";
448 OS << Node->getAsmString() << "\n";
449 if (Node->hasBraces())
450 Indent() << "}\n";
451}
452
453void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
454 PrintStmt(Node->getCapturedDecl()->getBody());
455}
456
457void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
458 Indent() << "@try";
459 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
460 PrintRawCompoundStmt(TS);
461 OS << "\n";
462 }
463
464 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
465 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
466 Indent() << "@catch(";
467 if (catchStmt->getCatchParamDecl()) {
468 if (Decl *DS = catchStmt->getCatchParamDecl())
469 PrintRawDecl(DS);
470 }
471 OS << ")";
472 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
473 PrintRawCompoundStmt(CS);
474 OS << "\n";
475 }
476 }
477
478 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
479 Node->getFinallyStmt())) {
480 Indent() << "@finally";
481 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
482 OS << "\n";
483 }
484}
485
486void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
487}
488
489void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
490 Indent() << "@catch (...) { /* todo */ } \n";
491}
492
493void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
494 Indent() << "@throw";
495 if (Node->getThrowExpr()) {
496 OS << " ";
497 PrintExpr(Node->getThrowExpr());
498 }
499 OS << ";\n";
500}
501
502void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
503 Indent() << "@synchronized (";
504 PrintExpr(Node->getSynchExpr());
505 OS << ")";
506 PrintRawCompoundStmt(Node->getSynchBody());
507 OS << "\n";
508}
509
510void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
511 Indent() << "@autoreleasepool";
512 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
513 OS << "\n";
514}
515
516void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
517 OS << "catch (";
518 if (Decl *ExDecl = Node->getExceptionDecl())
519 PrintRawDecl(ExDecl);
520 else
521 OS << "...";
522 OS << ") ";
523 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
524}
525
526void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
527 Indent();
528 PrintRawCXXCatchStmt(Node);
529 OS << "\n";
530}
531
532void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
533 Indent() << "try ";
534 PrintRawCompoundStmt(Node->getTryBlock());
535 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
536 OS << " ";
537 PrintRawCXXCatchStmt(Node->getHandler(i));
538 }
539 OS << "\n";
540}
541
542void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
543 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
544 PrintRawCompoundStmt(Node->getTryBlock());
545 SEHExceptStmt *E = Node->getExceptHandler();
546 SEHFinallyStmt *F = Node->getFinallyHandler();
547 if(E)
548 PrintRawSEHExceptHandler(E);
549 else {
550 assert(F && "Must have a finally block...");
551 PrintRawSEHFinallyStmt(F);
552 }
553 OS << "\n";
554}
555
556void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
557 OS << "__finally ";
558 PrintRawCompoundStmt(Node->getBlock());
559 OS << "\n";
560}
561
562void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
563 OS << "__except (";
564 VisitExpr(Node->getFilterExpr());
565 OS << ")\n";
566 PrintRawCompoundStmt(Node->getBlock());
567 OS << "\n";
568}
569
570void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
571 Indent();
572 PrintRawSEHExceptHandler(Node);
573 OS << "\n";
574}
575
576void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
577 Indent();
578 PrintRawSEHFinallyStmt(Node);
579 OS << "\n";
580}
581
582//===----------------------------------------------------------------------===//
583// OpenMP clauses printing methods
584//===----------------------------------------------------------------------===//
585
586namespace {
587class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> {
588 raw_ostream &OS;
589 /// \brief Process clauses with list of variables.
590 template <typename T>
591 void VisitOMPClauseList(T *Node, char StartSym);
592public:
593 OMPClausePrinter(raw_ostream &OS) : OS(OS) { }
594#define OPENMP_CLAUSE(Name, Class) \
595 void Visit##Class(Class *S);
596#include "clang/Basic/OpenMPKinds.def"
597};
598
599void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
600 OS << "default("
601 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
602 << ")";
603}
604
605template<typename T>
606void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
607 for (typename T::varlist_iterator I = Node->varlist_begin(),
608 E = Node->varlist_end();
609 I != E; ++I)
610 OS << (I == Node->varlist_begin() ? StartSym : ',')
611 << *cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
612}
613
614void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
615 if (!Node->varlist_empty()) {
616 OS << "private";
617 VisitOMPClauseList(Node, '(');
618 OS << ")";
619 }
620}
621
622void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
623 if (!Node->varlist_empty()) {
624 OS << "firstprivate";
625 VisitOMPClauseList(Node, '(');
626 OS << ")";
627 }
628}
629
630void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
631 if (!Node->varlist_empty()) {
632 OS << "shared";
633 VisitOMPClauseList(Node, '(');
634 OS << ")";
635 }
636}
637
638}
639
640//===----------------------------------------------------------------------===//
641// OpenMP directives printing methods
642//===----------------------------------------------------------------------===//
643
644void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
645 Indent() << "#pragma omp parallel ";
646
647 OMPClausePrinter Printer(OS);
648 ArrayRef<OMPClause *> Clauses = Node->clauses();
649 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
650 I != E; ++I)
651 if (*I && !(*I)->isImplicit()) {
652 Printer.Visit(*I);
653 OS << ' ';
654 }
655 OS << "\n";
656 if (Node->getAssociatedStmt()) {
657 assert(isa<CapturedStmt>(Node->getAssociatedStmt()) &&
658 "Expected captured statement!");
659 Stmt *CS = cast<CapturedStmt>(Node->getAssociatedStmt())->getCapturedStmt();
660 PrintStmt(CS);
661 }
662}
663//===----------------------------------------------------------------------===//
664// Expr printing methods.
665//===----------------------------------------------------------------------===//
666
667void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
668 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
669 Qualifier->print(OS, Policy);
670 if (Node->hasTemplateKeyword())
671 OS << "template ";
672 OS << Node->getNameInfo();
673 if (Node->hasExplicitTemplateArgs())
674 TemplateSpecializationType::PrintTemplateArgumentList(
675 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
676}
677
678void StmtPrinter::VisitDependentScopeDeclRefExpr(
679 DependentScopeDeclRefExpr *Node) {
680 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
681 Qualifier->print(OS, Policy);
682 if (Node->hasTemplateKeyword())
683 OS << "template ";
684 OS << Node->getNameInfo();
685 if (Node->hasExplicitTemplateArgs())
686 TemplateSpecializationType::PrintTemplateArgumentList(
687 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
688}
689
690void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
691 if (Node->getQualifier())
692 Node->getQualifier()->print(OS, Policy);
693 if (Node->hasTemplateKeyword())
694 OS << "template ";
695 OS << Node->getNameInfo();
696 if (Node->hasExplicitTemplateArgs())
697 TemplateSpecializationType::PrintTemplateArgumentList(
698 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
699}
700
701void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
702 if (Node->getBase()) {
703 PrintExpr(Node->getBase());
704 OS << (Node->isArrow() ? "->" : ".");
705 }
706 OS << *Node->getDecl();
707}
708
709void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
710 if (Node->isSuperReceiver())
711 OS << "super.";
712 else if (Node->getBase()) {
712 else if (Node->isObjectReceiver() && Node->getBase()) {
713 PrintExpr(Node->getBase());
714 OS << ".";
713 PrintExpr(Node->getBase());
714 OS << ".";
715 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
716 OS << Node->getClassReceiver()->getName() << ".";
715 }
716
717 if (Node->isImplicitProperty())
718 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
719 else
720 OS << Node->getExplicitProperty()->getName();
721}
722
723void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
724
725 PrintExpr(Node->getBaseExpr());
726 OS << "[";
727 PrintExpr(Node->getKeyExpr());
728 OS << "]";
729}
730
731void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
732 switch (Node->getIdentType()) {
733 default:
734 llvm_unreachable("unknown case");
735 case PredefinedExpr::Func:
736 OS << "__func__";
737 break;
738 case PredefinedExpr::Function:
739 OS << "__FUNCTION__";
740 break;
741 case PredefinedExpr::FuncDName:
742 OS << "__FUNCDNAME__";
743 break;
744 case PredefinedExpr::LFunction:
745 OS << "L__FUNCTION__";
746 break;
747 case PredefinedExpr::PrettyFunction:
748 OS << "__PRETTY_FUNCTION__";
749 break;
750 }
751}
752
753void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
754 unsigned value = Node->getValue();
755
756 switch (Node->getKind()) {
757 case CharacterLiteral::Ascii: break; // no prefix.
758 case CharacterLiteral::Wide: OS << 'L'; break;
759 case CharacterLiteral::UTF16: OS << 'u'; break;
760 case CharacterLiteral::UTF32: OS << 'U'; break;
761 }
762
763 switch (value) {
764 case '\\':
765 OS << "'\\\\'";
766 break;
767 case '\'':
768 OS << "'\\''";
769 break;
770 case '\a':
771 // TODO: K&R: the meaning of '\\a' is different in traditional C
772 OS << "'\\a'";
773 break;
774 case '\b':
775 OS << "'\\b'";
776 break;
777 // Nonstandard escape sequence.
778 /*case '\e':
779 OS << "'\\e'";
780 break;*/
781 case '\f':
782 OS << "'\\f'";
783 break;
784 case '\n':
785 OS << "'\\n'";
786 break;
787 case '\r':
788 OS << "'\\r'";
789 break;
790 case '\t':
791 OS << "'\\t'";
792 break;
793 case '\v':
794 OS << "'\\v'";
795 break;
796 default:
797 if (value < 256 && isPrintable((unsigned char)value))
798 OS << "'" << (char)value << "'";
799 else if (value < 256)
800 OS << "'\\x" << llvm::format("%02x", value) << "'";
801 else if (value <= 0xFFFF)
802 OS << "'\\u" << llvm::format("%04x", value) << "'";
803 else
804 OS << "'\\U" << llvm::format("%08x", value) << "'";
805 }
806}
807
808void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
809 bool isSigned = Node->getType()->isSignedIntegerType();
810 OS << Node->getValue().toString(10, isSigned);
811
812 // Emit suffixes. Integer literals are always a builtin integer type.
813 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
814 default: llvm_unreachable("Unexpected type for integer literal!");
815 // FIXME: The Short and UShort cases are to handle cases where a short
816 // integeral literal is formed during template instantiation. They should
817 // be removed when template instantiation no longer needs integer literals.
818 case BuiltinType::Short:
819 case BuiltinType::UShort:
820 case BuiltinType::Int: break; // no suffix.
821 case BuiltinType::UInt: OS << 'U'; break;
822 case BuiltinType::Long: OS << 'L'; break;
823 case BuiltinType::ULong: OS << "UL"; break;
824 case BuiltinType::LongLong: OS << "LL"; break;
825 case BuiltinType::ULongLong: OS << "ULL"; break;
826 case BuiltinType::Int128: OS << "i128"; break;
827 case BuiltinType::UInt128: OS << "Ui128"; break;
828 }
829}
830
831static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
832 bool PrintSuffix) {
833 SmallString<16> Str;
834 Node->getValue().toString(Str);
835 OS << Str;
836 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
837 OS << '.'; // Trailing dot in order to separate from ints.
838
839 if (!PrintSuffix)
840 return;
841
842 // Emit suffixes. Float literals are always a builtin float type.
843 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
844 default: llvm_unreachable("Unexpected type for float literal!");
845 case BuiltinType::Half: break; // FIXME: suffix?
846 case BuiltinType::Double: break; // no suffix.
847 case BuiltinType::Float: OS << 'F'; break;
848 case BuiltinType::LongDouble: OS << 'L'; break;
849 }
850}
851
852void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
853 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
854}
855
856void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
857 PrintExpr(Node->getSubExpr());
858 OS << "i";
859}
860
861void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
862 Str->outputString(OS);
863}
864void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
865 OS << "(";
866 PrintExpr(Node->getSubExpr());
867 OS << ")";
868}
869void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
870 if (!Node->isPostfix()) {
871 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
872
873 // Print a space if this is an "identifier operator" like __real, or if
874 // it might be concatenated incorrectly like '+'.
875 switch (Node->getOpcode()) {
876 default: break;
877 case UO_Real:
878 case UO_Imag:
879 case UO_Extension:
880 OS << ' ';
881 break;
882 case UO_Plus:
883 case UO_Minus:
884 if (isa<UnaryOperator>(Node->getSubExpr()))
885 OS << ' ';
886 break;
887 }
888 }
889 PrintExpr(Node->getSubExpr());
890
891 if (Node->isPostfix())
892 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
893}
894
895void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
896 OS << "__builtin_offsetof(";
897 Node->getTypeSourceInfo()->getType().print(OS, Policy);
898 OS << ", ";
899 bool PrintedSomething = false;
900 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
901 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
902 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
903 // Array node
904 OS << "[";
905 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
906 OS << "]";
907 PrintedSomething = true;
908 continue;
909 }
910
911 // Skip implicit base indirections.
912 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
913 continue;
914
915 // Field or identifier node.
916 IdentifierInfo *Id = ON.getFieldName();
917 if (!Id)
918 continue;
919
920 if (PrintedSomething)
921 OS << ".";
922 else
923 PrintedSomething = true;
924 OS << Id->getName();
925 }
926 OS << ")";
927}
928
929void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
930 switch(Node->getKind()) {
931 case UETT_SizeOf:
932 OS << "sizeof";
933 break;
934 case UETT_AlignOf:
935 if (Policy.LangOpts.CPlusPlus)
936 OS << "alignof";
937 else if (Policy.LangOpts.C11)
938 OS << "_Alignof";
939 else
940 OS << "__alignof";
941 break;
942 case UETT_VecStep:
943 OS << "vec_step";
944 break;
945 }
946 if (Node->isArgumentType()) {
947 OS << '(';
948 Node->getArgumentType().print(OS, Policy);
949 OS << ')';
950 } else {
951 OS << " ";
952 PrintExpr(Node->getArgumentExpr());
953 }
954}
955
956void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
957 OS << "_Generic(";
958 PrintExpr(Node->getControllingExpr());
959 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
960 OS << ", ";
961 QualType T = Node->getAssocType(i);
962 if (T.isNull())
963 OS << "default";
964 else
965 T.print(OS, Policy);
966 OS << ": ";
967 PrintExpr(Node->getAssocExpr(i));
968 }
969 OS << ")";
970}
971
972void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
973 PrintExpr(Node->getLHS());
974 OS << "[";
975 PrintExpr(Node->getRHS());
976 OS << "]";
977}
978
979void StmtPrinter::PrintCallArgs(CallExpr *Call) {
980 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
981 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
982 // Don't print any defaulted arguments
983 break;
984 }
985
986 if (i) OS << ", ";
987 PrintExpr(Call->getArg(i));
988 }
989}
990
991void StmtPrinter::VisitCallExpr(CallExpr *Call) {
992 PrintExpr(Call->getCallee());
993 OS << "(";
994 PrintCallArgs(Call);
995 OS << ")";
996}
997void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
998 // FIXME: Suppress printing implicit bases (like "this")
999 PrintExpr(Node->getBase());
1000
1001 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1002 FieldDecl *ParentDecl = ParentMember
1003 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : NULL;
1004
1005 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1006 OS << (Node->isArrow() ? "->" : ".");
1007
1008 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1009 if (FD->isAnonymousStructOrUnion())
1010 return;
1011
1012 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1013 Qualifier->print(OS, Policy);
1014 if (Node->hasTemplateKeyword())
1015 OS << "template ";
1016 OS << Node->getMemberNameInfo();
1017 if (Node->hasExplicitTemplateArgs())
1018 TemplateSpecializationType::PrintTemplateArgumentList(
1019 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1020}
1021void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1022 PrintExpr(Node->getBase());
1023 OS << (Node->isArrow() ? "->isa" : ".isa");
1024}
1025
1026void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1027 PrintExpr(Node->getBase());
1028 OS << ".";
1029 OS << Node->getAccessor().getName();
1030}
1031void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1032 OS << '(';
1033 Node->getTypeAsWritten().print(OS, Policy);
1034 OS << ')';
1035 PrintExpr(Node->getSubExpr());
1036}
1037void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1038 OS << '(';
1039 Node->getType().print(OS, Policy);
1040 OS << ')';
1041 PrintExpr(Node->getInitializer());
1042}
1043void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1044 // No need to print anything, simply forward to the sub expression.
1045 PrintExpr(Node->getSubExpr());
1046}
1047void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1048 PrintExpr(Node->getLHS());
1049 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1050 PrintExpr(Node->getRHS());
1051}
1052void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1053 PrintExpr(Node->getLHS());
1054 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1055 PrintExpr(Node->getRHS());
1056}
1057void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1058 PrintExpr(Node->getCond());
1059 OS << " ? ";
1060 PrintExpr(Node->getLHS());
1061 OS << " : ";
1062 PrintExpr(Node->getRHS());
1063}
1064
1065// GNU extensions.
1066
1067void
1068StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1069 PrintExpr(Node->getCommon());
1070 OS << " ?: ";
1071 PrintExpr(Node->getFalseExpr());
1072}
1073void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1074 OS << "&&" << Node->getLabel()->getName();
1075}
1076
1077void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1078 OS << "(";
1079 PrintRawCompoundStmt(E->getSubStmt());
1080 OS << ")";
1081}
1082
1083void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1084 OS << "__builtin_choose_expr(";
1085 PrintExpr(Node->getCond());
1086 OS << ", ";
1087 PrintExpr(Node->getLHS());
1088 OS << ", ";
1089 PrintExpr(Node->getRHS());
1090 OS << ")";
1091}
1092
1093void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1094 OS << "__null";
1095}
1096
1097void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1098 OS << "__builtin_shufflevector(";
1099 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1100 if (i) OS << ", ";
1101 PrintExpr(Node->getExpr(i));
1102 }
1103 OS << ")";
1104}
1105
1106void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1107 OS << "__builtin_convertvector(";
1108 PrintExpr(Node->getSrcExpr());
1109 OS << ", ";
1110 Node->getType().print(OS, Policy);
1111 OS << ")";
1112}
1113
1114void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1115 if (Node->getSyntacticForm()) {
1116 Visit(Node->getSyntacticForm());
1117 return;
1118 }
1119
1120 OS << "{ ";
1121 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1122 if (i) OS << ", ";
1123 if (Node->getInit(i))
1124 PrintExpr(Node->getInit(i));
1125 else
1126 OS << "0";
1127 }
1128 OS << " }";
1129}
1130
1131void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1132 OS << "( ";
1133 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1134 if (i) OS << ", ";
1135 PrintExpr(Node->getExpr(i));
1136 }
1137 OS << " )";
1138}
1139
1140void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1141 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1142 DEnd = Node->designators_end();
1143 D != DEnd; ++D) {
1144 if (D->isFieldDesignator()) {
1145 if (D->getDotLoc().isInvalid())
1146 OS << D->getFieldName()->getName() << ":";
1147 else
1148 OS << "." << D->getFieldName()->getName();
1149 } else {
1150 OS << "[";
1151 if (D->isArrayDesignator()) {
1152 PrintExpr(Node->getArrayIndex(*D));
1153 } else {
1154 PrintExpr(Node->getArrayRangeStart(*D));
1155 OS << " ... ";
1156 PrintExpr(Node->getArrayRangeEnd(*D));
1157 }
1158 OS << "]";
1159 }
1160 }
1161
1162 OS << " = ";
1163 PrintExpr(Node->getInit());
1164}
1165
1166void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1167 if (Policy.LangOpts.CPlusPlus) {
1168 OS << "/*implicit*/";
1169 Node->getType().print(OS, Policy);
1170 OS << "()";
1171 } else {
1172 OS << "/*implicit*/(";
1173 Node->getType().print(OS, Policy);
1174 OS << ')';
1175 if (Node->getType()->isRecordType())
1176 OS << "{}";
1177 else
1178 OS << 0;
1179 }
1180}
1181
1182void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1183 OS << "__builtin_va_arg(";
1184 PrintExpr(Node->getSubExpr());
1185 OS << ", ";
1186 Node->getType().print(OS, Policy);
1187 OS << ")";
1188}
1189
1190void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1191 PrintExpr(Node->getSyntacticForm());
1192}
1193
1194void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1195 const char *Name = 0;
1196 switch (Node->getOp()) {
1197#define BUILTIN(ID, TYPE, ATTRS)
1198#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1199 case AtomicExpr::AO ## ID: \
1200 Name = #ID "("; \
1201 break;
1202#include "clang/Basic/Builtins.def"
1203 }
1204 OS << Name;
1205
1206 // AtomicExpr stores its subexpressions in a permuted order.
1207 PrintExpr(Node->getPtr());
1208 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1209 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
1210 OS << ", ";
1211 PrintExpr(Node->getVal1());
1212 }
1213 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1214 Node->isCmpXChg()) {
1215 OS << ", ";
1216 PrintExpr(Node->getVal2());
1217 }
1218 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1219 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1220 OS << ", ";
1221 PrintExpr(Node->getWeak());
1222 }
1223 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1224 OS << ", ";
1225 PrintExpr(Node->getOrder());
1226 }
1227 if (Node->isCmpXChg()) {
1228 OS << ", ";
1229 PrintExpr(Node->getOrderFail());
1230 }
1231 OS << ")";
1232}
1233
1234// C++
1235void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1236 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1237 "",
1238#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1239 Spelling,
1240#include "clang/Basic/OperatorKinds.def"
1241 };
1242
1243 OverloadedOperatorKind Kind = Node->getOperator();
1244 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1245 if (Node->getNumArgs() == 1) {
1246 OS << OpStrings[Kind] << ' ';
1247 PrintExpr(Node->getArg(0));
1248 } else {
1249 PrintExpr(Node->getArg(0));
1250 OS << ' ' << OpStrings[Kind];
1251 }
1252 } else if (Kind == OO_Arrow) {
1253 PrintExpr(Node->getArg(0));
1254 } else if (Kind == OO_Call) {
1255 PrintExpr(Node->getArg(0));
1256 OS << '(';
1257 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1258 if (ArgIdx > 1)
1259 OS << ", ";
1260 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1261 PrintExpr(Node->getArg(ArgIdx));
1262 }
1263 OS << ')';
1264 } else if (Kind == OO_Subscript) {
1265 PrintExpr(Node->getArg(0));
1266 OS << '[';
1267 PrintExpr(Node->getArg(1));
1268 OS << ']';
1269 } else if (Node->getNumArgs() == 1) {
1270 OS << OpStrings[Kind] << ' ';
1271 PrintExpr(Node->getArg(0));
1272 } else if (Node->getNumArgs() == 2) {
1273 PrintExpr(Node->getArg(0));
1274 OS << ' ' << OpStrings[Kind] << ' ';
1275 PrintExpr(Node->getArg(1));
1276 } else {
1277 llvm_unreachable("unknown overloaded operator");
1278 }
1279}
1280
1281void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1282 VisitCallExpr(cast<CallExpr>(Node));
1283}
1284
1285void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1286 PrintExpr(Node->getCallee());
1287 OS << "<<<";
1288 PrintCallArgs(Node->getConfig());
1289 OS << ">>>(";
1290 PrintCallArgs(Node);
1291 OS << ")";
1292}
1293
1294void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1295 OS << Node->getCastName() << '<';
1296 Node->getTypeAsWritten().print(OS, Policy);
1297 OS << ">(";
1298 PrintExpr(Node->getSubExpr());
1299 OS << ")";
1300}
1301
1302void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1303 VisitCXXNamedCastExpr(Node);
1304}
1305
1306void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1307 VisitCXXNamedCastExpr(Node);
1308}
1309
1310void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1311 VisitCXXNamedCastExpr(Node);
1312}
1313
1314void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1315 VisitCXXNamedCastExpr(Node);
1316}
1317
1318void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1319 OS << "typeid(";
1320 if (Node->isTypeOperand()) {
1321 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1322 } else {
1323 PrintExpr(Node->getExprOperand());
1324 }
1325 OS << ")";
1326}
1327
1328void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1329 OS << "__uuidof(";
1330 if (Node->isTypeOperand()) {
1331 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1332 } else {
1333 PrintExpr(Node->getExprOperand());
1334 }
1335 OS << ")";
1336}
1337
1338void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1339 PrintExpr(Node->getBaseExpr());
1340 if (Node->isArrow())
1341 OS << "->";
1342 else
1343 OS << ".";
1344 if (NestedNameSpecifier *Qualifier =
1345 Node->getQualifierLoc().getNestedNameSpecifier())
1346 Qualifier->print(OS, Policy);
1347 OS << Node->getPropertyDecl()->getDeclName();
1348}
1349
1350void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1351 switch (Node->getLiteralOperatorKind()) {
1352 case UserDefinedLiteral::LOK_Raw:
1353 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
1354 break;
1355 case UserDefinedLiteral::LOK_Template: {
1356 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1357 const TemplateArgumentList *Args =
1358 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1359 assert(Args);
1360 const TemplateArgument &Pack = Args->get(0);
1361 for (TemplateArgument::pack_iterator I = Pack.pack_begin(),
1362 E = Pack.pack_end(); I != E; ++I) {
1363 char C = (char)I->getAsIntegral().getZExtValue();
1364 OS << C;
1365 }
1366 break;
1367 }
1368 case UserDefinedLiteral::LOK_Integer: {
1369 // Print integer literal without suffix.
1370 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1371 OS << Int->getValue().toString(10, /*isSigned*/false);
1372 break;
1373 }
1374 case UserDefinedLiteral::LOK_Floating: {
1375 // Print floating literal without suffix.
1376 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1377 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1378 break;
1379 }
1380 case UserDefinedLiteral::LOK_String:
1381 case UserDefinedLiteral::LOK_Character:
1382 PrintExpr(Node->getCookedLiteral());
1383 break;
1384 }
1385 OS << Node->getUDSuffix()->getName();
1386}
1387
1388void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1389 OS << (Node->getValue() ? "true" : "false");
1390}
1391
1392void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1393 OS << "nullptr";
1394}
1395
1396void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1397 OS << "this";
1398}
1399
1400void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1401 if (Node->getSubExpr() == 0)
1402 OS << "throw";
1403 else {
1404 OS << "throw ";
1405 PrintExpr(Node->getSubExpr());
1406 }
1407}
1408
1409void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1410 // Nothing to print: we picked up the default argument.
1411}
1412
1413void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1414 // Nothing to print: we picked up the default initializer.
1415}
1416
1417void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1418 Node->getType().print(OS, Policy);
1419 OS << "(";
1420 PrintExpr(Node->getSubExpr());
1421 OS << ")";
1422}
1423
1424void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1425 PrintExpr(Node->getSubExpr());
1426}
1427
1428void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1429 Node->getType().print(OS, Policy);
1430 OS << "(";
1431 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1432 ArgEnd = Node->arg_end();
1433 Arg != ArgEnd; ++Arg) {
1434 if (Arg->isDefaultArgument())
1435 break;
1436 if (Arg != Node->arg_begin())
1437 OS << ", ";
1438 PrintExpr(*Arg);
1439 }
1440 OS << ")";
1441}
1442
1443void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1444 OS << '[';
1445 bool NeedComma = false;
1446 switch (Node->getCaptureDefault()) {
1447 case LCD_None:
1448 break;
1449
1450 case LCD_ByCopy:
1451 OS << '=';
1452 NeedComma = true;
1453 break;
1454
1455 case LCD_ByRef:
1456 OS << '&';
1457 NeedComma = true;
1458 break;
1459 }
1460 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1461 CEnd = Node->explicit_capture_end();
1462 C != CEnd;
1463 ++C) {
1464 if (NeedComma)
1465 OS << ", ";
1466 NeedComma = true;
1467
1468 switch (C->getCaptureKind()) {
1469 case LCK_This:
1470 OS << "this";
1471 break;
1472
1473 case LCK_ByRef:
1474 if (Node->getCaptureDefault() != LCD_ByRef || C->isInitCapture())
1475 OS << '&';
1476 OS << C->getCapturedVar()->getName();
1477 break;
1478
1479 case LCK_ByCopy:
1480 OS << C->getCapturedVar()->getName();
1481 break;
1482 }
1483
1484 if (C->isInitCapture())
1485 PrintExpr(C->getCapturedVar()->getInit());
1486 }
1487 OS << ']';
1488
1489 if (Node->hasExplicitParameters()) {
1490 OS << " (";
1491 CXXMethodDecl *Method = Node->getCallOperator();
1492 NeedComma = false;
1493 for (CXXMethodDecl::param_iterator P = Method->param_begin(),
1494 PEnd = Method->param_end();
1495 P != PEnd; ++P) {
1496 if (NeedComma) {
1497 OS << ", ";
1498 } else {
1499 NeedComma = true;
1500 }
1501 std::string ParamStr = (*P)->getNameAsString();
1502 (*P)->getOriginalType().print(OS, Policy, ParamStr);
1503 }
1504 if (Method->isVariadic()) {
1505 if (NeedComma)
1506 OS << ", ";
1507 OS << "...";
1508 }
1509 OS << ')';
1510
1511 if (Node->isMutable())
1512 OS << " mutable";
1513
1514 const FunctionProtoType *Proto
1515 = Method->getType()->getAs<FunctionProtoType>();
1516 Proto->printExceptionSpecification(OS, Policy);
1517
1518 // FIXME: Attributes
1519
1520 // Print the trailing return type if it was specified in the source.
1521 if (Node->hasExplicitResultType()) {
1522 OS << " -> ";
1523 Proto->getResultType().print(OS, Policy);
1524 }
1525 }
1526
1527 // Print the body.
1528 CompoundStmt *Body = Node->getBody();
1529 OS << ' ';
1530 PrintStmt(Body);
1531}
1532
1533void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
1534 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1535 TSInfo->getType().print(OS, Policy);
1536 else
1537 Node->getType().print(OS, Policy);
1538 OS << "()";
1539}
1540
1541void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1542 if (E->isGlobalNew())
1543 OS << "::";
1544 OS << "new ";
1545 unsigned NumPlace = E->getNumPlacementArgs();
1546 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
1547 OS << "(";
1548 PrintExpr(E->getPlacementArg(0));
1549 for (unsigned i = 1; i < NumPlace; ++i) {
1550 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
1551 break;
1552 OS << ", ";
1553 PrintExpr(E->getPlacementArg(i));
1554 }
1555 OS << ") ";
1556 }
1557 if (E->isParenTypeId())
1558 OS << "(";
1559 std::string TypeS;
1560 if (Expr *Size = E->getArraySize()) {
1561 llvm::raw_string_ostream s(TypeS);
1562 s << '[';
1563 Size->printPretty(s, Helper, Policy);
1564 s << ']';
1565 }
1566 E->getAllocatedType().print(OS, Policy, TypeS);
1567 if (E->isParenTypeId())
1568 OS << ")";
1569
1570 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1571 if (InitStyle) {
1572 if (InitStyle == CXXNewExpr::CallInit)
1573 OS << "(";
1574 PrintExpr(E->getInitializer());
1575 if (InitStyle == CXXNewExpr::CallInit)
1576 OS << ")";
1577 }
1578}
1579
1580void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1581 if (E->isGlobalDelete())
1582 OS << "::";
1583 OS << "delete ";
1584 if (E->isArrayForm())
1585 OS << "[] ";
1586 PrintExpr(E->getArgument());
1587}
1588
1589void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1590 PrintExpr(E->getBase());
1591 if (E->isArrow())
1592 OS << "->";
1593 else
1594 OS << '.';
1595 if (E->getQualifier())
1596 E->getQualifier()->print(OS, Policy);
1597 OS << "~";
1598
1599 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1600 OS << II->getName();
1601 else
1602 E->getDestroyedType().print(OS, Policy);
1603}
1604
1605void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
1606 if (E->isListInitialization())
1607 OS << "{ ";
1608
1609 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1610 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1611 // Don't print any defaulted arguments
1612 break;
1613 }
1614
1615 if (i) OS << ", ";
1616 PrintExpr(E->getArg(i));
1617 }
1618
1619 if (E->isListInitialization())
1620 OS << " }";
1621}
1622
1623void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
1624 PrintExpr(E->getSubExpr());
1625}
1626
1627void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
1628 // Just forward to the sub expression.
1629 PrintExpr(E->getSubExpr());
1630}
1631
1632void
1633StmtPrinter::VisitCXXUnresolvedConstructExpr(
1634 CXXUnresolvedConstructExpr *Node) {
1635 Node->getTypeAsWritten().print(OS, Policy);
1636 OS << "(";
1637 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
1638 ArgEnd = Node->arg_end();
1639 Arg != ArgEnd; ++Arg) {
1640 if (Arg != Node->arg_begin())
1641 OS << ", ";
1642 PrintExpr(*Arg);
1643 }
1644 OS << ")";
1645}
1646
1647void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1648 CXXDependentScopeMemberExpr *Node) {
1649 if (!Node->isImplicitAccess()) {
1650 PrintExpr(Node->getBase());
1651 OS << (Node->isArrow() ? "->" : ".");
1652 }
1653 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1654 Qualifier->print(OS, Policy);
1655 if (Node->hasTemplateKeyword())
1656 OS << "template ";
1657 OS << Node->getMemberNameInfo();
1658 if (Node->hasExplicitTemplateArgs())
1659 TemplateSpecializationType::PrintTemplateArgumentList(
1660 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1661}
1662
1663void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
1664 if (!Node->isImplicitAccess()) {
1665 PrintExpr(Node->getBase());
1666 OS << (Node->isArrow() ? "->" : ".");
1667 }
1668 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1669 Qualifier->print(OS, Policy);
1670 if (Node->hasTemplateKeyword())
1671 OS << "template ";
1672 OS << Node->getMemberNameInfo();
1673 if (Node->hasExplicitTemplateArgs())
1674 TemplateSpecializationType::PrintTemplateArgumentList(
1675 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1676}
1677
1678static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1679 switch (UTT) {
1680 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1681 case UTT_HasNothrowMoveAssign: return "__has_nothrow_move_assign";
1682 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1683 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1684 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1685 case UTT_HasTrivialMoveAssign: return "__has_trivial_move_assign";
1686 case UTT_HasTrivialMoveConstructor: return "__has_trivial_move_constructor";
1687 case UTT_HasTrivialDefaultConstructor: return "__has_trivial_constructor";
1688 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1689 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1690 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1691 case UTT_IsAbstract: return "__is_abstract";
1692 case UTT_IsArithmetic: return "__is_arithmetic";
1693 case UTT_IsArray: return "__is_array";
1694 case UTT_IsClass: return "__is_class";
1695 case UTT_IsCompleteType: return "__is_complete_type";
1696 case UTT_IsCompound: return "__is_compound";
1697 case UTT_IsConst: return "__is_const";
1698 case UTT_IsEmpty: return "__is_empty";
1699 case UTT_IsEnum: return "__is_enum";
1700 case UTT_IsFinal: return "__is_final";
1701 case UTT_IsFloatingPoint: return "__is_floating_point";
1702 case UTT_IsFunction: return "__is_function";
1703 case UTT_IsFundamental: return "__is_fundamental";
1704 case UTT_IsIntegral: return "__is_integral";
1705 case UTT_IsInterfaceClass: return "__is_interface_class";
1706 case UTT_IsLiteral: return "__is_literal";
1707 case UTT_IsLvalueReference: return "__is_lvalue_reference";
1708 case UTT_IsMemberFunctionPointer: return "__is_member_function_pointer";
1709 case UTT_IsMemberObjectPointer: return "__is_member_object_pointer";
1710 case UTT_IsMemberPointer: return "__is_member_pointer";
1711 case UTT_IsObject: return "__is_object";
1712 case UTT_IsPOD: return "__is_pod";
1713 case UTT_IsPointer: return "__is_pointer";
1714 case UTT_IsPolymorphic: return "__is_polymorphic";
1715 case UTT_IsReference: return "__is_reference";
1716 case UTT_IsRvalueReference: return "__is_rvalue_reference";
1717 case UTT_IsScalar: return "__is_scalar";
1718 case UTT_IsSealed: return "__is_sealed";
1719 case UTT_IsSigned: return "__is_signed";
1720 case UTT_IsStandardLayout: return "__is_standard_layout";
1721 case UTT_IsTrivial: return "__is_trivial";
1722 case UTT_IsTriviallyCopyable: return "__is_trivially_copyable";
1723 case UTT_IsUnion: return "__is_union";
1724 case UTT_IsUnsigned: return "__is_unsigned";
1725 case UTT_IsVoid: return "__is_void";
1726 case UTT_IsVolatile: return "__is_volatile";
1727 }
1728 llvm_unreachable("Type trait not covered by switch statement");
1729}
1730
1731static const char *getTypeTraitName(BinaryTypeTrait BTT) {
1732 switch (BTT) {
1733 case BTT_IsBaseOf: return "__is_base_of";
1734 case BTT_IsConvertible: return "__is_convertible";
1735 case BTT_IsSame: return "__is_same";
1736 case BTT_TypeCompatible: return "__builtin_types_compatible_p";
1737 case BTT_IsConvertibleTo: return "__is_convertible_to";
1738 case BTT_IsTriviallyAssignable: return "__is_trivially_assignable";
1739 }
1740 llvm_unreachable("Binary type trait not covered by switch");
1741}
1742
1743static const char *getTypeTraitName(TypeTrait TT) {
1744 switch (TT) {
1745 case clang::TT_IsTriviallyConstructible:return "__is_trivially_constructible";
1746 }
1747 llvm_unreachable("Type trait not covered by switch");
1748}
1749
1750static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1751 switch (ATT) {
1752 case ATT_ArrayRank: return "__array_rank";
1753 case ATT_ArrayExtent: return "__array_extent";
1754 }
1755 llvm_unreachable("Array type trait not covered by switch");
1756}
1757
1758static const char *getExpressionTraitName(ExpressionTrait ET) {
1759 switch (ET) {
1760 case ET_IsLValueExpr: return "__is_lvalue_expr";
1761 case ET_IsRValueExpr: return "__is_rvalue_expr";
1762 }
1763 llvm_unreachable("Expression type trait not covered by switch");
1764}
1765
1766void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1767 OS << getTypeTraitName(E->getTrait()) << '(';
1768 E->getQueriedType().print(OS, Policy);
1769 OS << ')';
1770}
1771
1772void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
1773 OS << getTypeTraitName(E->getTrait()) << '(';
1774 E->getLhsType().print(OS, Policy);
1775 OS << ',';
1776 E->getRhsType().print(OS, Policy);
1777 OS << ')';
1778}
1779
1780void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1781 OS << getTypeTraitName(E->getTrait()) << "(";
1782 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
1783 if (I > 0)
1784 OS << ", ";
1785 E->getArg(I)->getType().print(OS, Policy);
1786 }
1787 OS << ")";
1788}
1789
1790void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
1791 OS << getTypeTraitName(E->getTrait()) << '(';
1792 E->getQueriedType().print(OS, Policy);
1793 OS << ')';
1794}
1795
1796void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
1797 OS << getExpressionTraitName(E->getTrait()) << '(';
1798 PrintExpr(E->getQueriedExpression());
1799 OS << ')';
1800}
1801
1802void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1803 OS << "noexcept(";
1804 PrintExpr(E->getOperand());
1805 OS << ")";
1806}
1807
1808void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
1809 PrintExpr(E->getPattern());
1810 OS << "...";
1811}
1812
1813void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
1814 OS << "sizeof...(" << *E->getPack() << ")";
1815}
1816
1817void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1818 SubstNonTypeTemplateParmPackExpr *Node) {
1819 OS << *Node->getParameterPack();
1820}
1821
1822void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
1823 SubstNonTypeTemplateParmExpr *Node) {
1824 Visit(Node->getReplacement());
1825}
1826
1827void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
1828 OS << *E->getParameterPack();
1829}
1830
1831void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
1832 PrintExpr(Node->GetTemporaryExpr());
1833}
1834
1835// Obj-C
1836
1837void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1838 OS << "@";
1839 VisitStringLiteral(Node->getString());
1840}
1841
1842void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
1843 OS << "@";
1844 Visit(E->getSubExpr());
1845}
1846
1847void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
1848 OS << "@[ ";
1849 StmtRange ch = E->children();
1850 if (ch.first != ch.second) {
1851 while (1) {
1852 Visit(*ch.first);
1853 ++ch.first;
1854 if (ch.first == ch.second) break;
1855 OS << ", ";
1856 }
1857 }
1858 OS << " ]";
1859}
1860
1861void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
1862 OS << "@{ ";
1863 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
1864 if (I > 0)
1865 OS << ", ";
1866
1867 ObjCDictionaryElement Element = E->getKeyValueElement(I);
1868 Visit(Element.Key);
1869 OS << " : ";
1870 Visit(Element.Value);
1871 if (Element.isPackExpansion())
1872 OS << "...";
1873 }
1874 OS << " }";
1875}
1876
1877void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
1878 OS << "@encode(";
1879 Node->getEncodedType().print(OS, Policy);
1880 OS << ')';
1881}
1882
1883void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
1884 OS << "@selector(" << Node->getSelector().getAsString() << ')';
1885}
1886
1887void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
1888 OS << "@protocol(" << *Node->getProtocol() << ')';
1889}
1890
1891void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1892 OS << "[";
1893 switch (Mess->getReceiverKind()) {
1894 case ObjCMessageExpr::Instance:
1895 PrintExpr(Mess->getInstanceReceiver());
1896 break;
1897
1898 case ObjCMessageExpr::Class:
1899 Mess->getClassReceiver().print(OS, Policy);
1900 break;
1901
1902 case ObjCMessageExpr::SuperInstance:
1903 case ObjCMessageExpr::SuperClass:
1904 OS << "Super";
1905 break;
1906 }
1907
1908 OS << ' ';
1909 Selector selector = Mess->getSelector();
1910 if (selector.isUnarySelector()) {
1911 OS << selector.getNameForSlot(0);
1912 } else {
1913 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
1914 if (i < selector.getNumArgs()) {
1915 if (i > 0) OS << ' ';
1916 if (selector.getIdentifierInfoForSlot(i))
1917 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
1918 else
1919 OS << ":";
1920 }
1921 else OS << ", "; // Handle variadic methods.
1922
1923 PrintExpr(Mess->getArg(i));
1924 }
1925 }
1926 OS << "]";
1927}
1928
1929void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
1930 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
1931}
1932
1933void
1934StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
1935 PrintExpr(E->getSubExpr());
1936}
1937
1938void
1939StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
1940 OS << '(' << E->getBridgeKindName();
1941 E->getType().print(OS, Policy);
1942 OS << ')';
1943 PrintExpr(E->getSubExpr());
1944}
1945
1946void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
1947 BlockDecl *BD = Node->getBlockDecl();
1948 OS << "^";
1949
1950 const FunctionType *AFT = Node->getFunctionType();
1951
1952 if (isa<FunctionNoProtoType>(AFT)) {
1953 OS << "()";
1954 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
1955 OS << '(';
1956 for (BlockDecl::param_iterator AI = BD->param_begin(),
1957 E = BD->param_end(); AI != E; ++AI) {
1958 if (AI != BD->param_begin()) OS << ", ";
1959 std::string ParamStr = (*AI)->getNameAsString();
1960 (*AI)->getType().print(OS, Policy, ParamStr);
1961 }
1962
1963 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
1964 if (FT->isVariadic()) {
1965 if (!BD->param_empty()) OS << ", ";
1966 OS << "...";
1967 }
1968 OS << ')';
1969 }
1970 OS << "{ }";
1971}
1972
1973void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
1974 PrintExpr(Node->getSourceExpr());
1975}
1976
1977void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
1978 OS << "__builtin_astype(";
1979 PrintExpr(Node->getSrcExpr());
1980 OS << ", ";
1981 Node->getType().print(OS, Policy);
1982 OS << ")";
1983}
1984
1985//===----------------------------------------------------------------------===//
1986// Stmt method implementations
1987//===----------------------------------------------------------------------===//
1988
1989void Stmt::dumpPretty(const ASTContext &Context) const {
1990 printPretty(llvm::errs(), 0, PrintingPolicy(Context.getLangOpts()));
1991}
1992
1993void Stmt::printPretty(raw_ostream &OS,
1994 PrinterHelper *Helper,
1995 const PrintingPolicy &Policy,
1996 unsigned Indentation) const {
1997 if (this == 0) {
1998 OS << "<NULL>";
1999 return;
2000 }
2001
2002 StmtPrinter P(OS, Helper, Policy, Indentation);
2003 P.Visit(const_cast<Stmt*>(this));
2004}
2005
2006//===----------------------------------------------------------------------===//
2007// PrinterHelper
2008//===----------------------------------------------------------------------===//
2009
2010// Implement virtual destructor.
2011PrinterHelper::~PrinterHelper() {}
717 }
718
719 if (Node->isImplicitProperty())
720 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
721 else
722 OS << Node->getExplicitProperty()->getName();
723}
724
725void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
726
727 PrintExpr(Node->getBaseExpr());
728 OS << "[";
729 PrintExpr(Node->getKeyExpr());
730 OS << "]";
731}
732
733void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
734 switch (Node->getIdentType()) {
735 default:
736 llvm_unreachable("unknown case");
737 case PredefinedExpr::Func:
738 OS << "__func__";
739 break;
740 case PredefinedExpr::Function:
741 OS << "__FUNCTION__";
742 break;
743 case PredefinedExpr::FuncDName:
744 OS << "__FUNCDNAME__";
745 break;
746 case PredefinedExpr::LFunction:
747 OS << "L__FUNCTION__";
748 break;
749 case PredefinedExpr::PrettyFunction:
750 OS << "__PRETTY_FUNCTION__";
751 break;
752 }
753}
754
755void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
756 unsigned value = Node->getValue();
757
758 switch (Node->getKind()) {
759 case CharacterLiteral::Ascii: break; // no prefix.
760 case CharacterLiteral::Wide: OS << 'L'; break;
761 case CharacterLiteral::UTF16: OS << 'u'; break;
762 case CharacterLiteral::UTF32: OS << 'U'; break;
763 }
764
765 switch (value) {
766 case '\\':
767 OS << "'\\\\'";
768 break;
769 case '\'':
770 OS << "'\\''";
771 break;
772 case '\a':
773 // TODO: K&R: the meaning of '\\a' is different in traditional C
774 OS << "'\\a'";
775 break;
776 case '\b':
777 OS << "'\\b'";
778 break;
779 // Nonstandard escape sequence.
780 /*case '\e':
781 OS << "'\\e'";
782 break;*/
783 case '\f':
784 OS << "'\\f'";
785 break;
786 case '\n':
787 OS << "'\\n'";
788 break;
789 case '\r':
790 OS << "'\\r'";
791 break;
792 case '\t':
793 OS << "'\\t'";
794 break;
795 case '\v':
796 OS << "'\\v'";
797 break;
798 default:
799 if (value < 256 && isPrintable((unsigned char)value))
800 OS << "'" << (char)value << "'";
801 else if (value < 256)
802 OS << "'\\x" << llvm::format("%02x", value) << "'";
803 else if (value <= 0xFFFF)
804 OS << "'\\u" << llvm::format("%04x", value) << "'";
805 else
806 OS << "'\\U" << llvm::format("%08x", value) << "'";
807 }
808}
809
810void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
811 bool isSigned = Node->getType()->isSignedIntegerType();
812 OS << Node->getValue().toString(10, isSigned);
813
814 // Emit suffixes. Integer literals are always a builtin integer type.
815 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
816 default: llvm_unreachable("Unexpected type for integer literal!");
817 // FIXME: The Short and UShort cases are to handle cases where a short
818 // integeral literal is formed during template instantiation. They should
819 // be removed when template instantiation no longer needs integer literals.
820 case BuiltinType::Short:
821 case BuiltinType::UShort:
822 case BuiltinType::Int: break; // no suffix.
823 case BuiltinType::UInt: OS << 'U'; break;
824 case BuiltinType::Long: OS << 'L'; break;
825 case BuiltinType::ULong: OS << "UL"; break;
826 case BuiltinType::LongLong: OS << "LL"; break;
827 case BuiltinType::ULongLong: OS << "ULL"; break;
828 case BuiltinType::Int128: OS << "i128"; break;
829 case BuiltinType::UInt128: OS << "Ui128"; break;
830 }
831}
832
833static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
834 bool PrintSuffix) {
835 SmallString<16> Str;
836 Node->getValue().toString(Str);
837 OS << Str;
838 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
839 OS << '.'; // Trailing dot in order to separate from ints.
840
841 if (!PrintSuffix)
842 return;
843
844 // Emit suffixes. Float literals are always a builtin float type.
845 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
846 default: llvm_unreachable("Unexpected type for float literal!");
847 case BuiltinType::Half: break; // FIXME: suffix?
848 case BuiltinType::Double: break; // no suffix.
849 case BuiltinType::Float: OS << 'F'; break;
850 case BuiltinType::LongDouble: OS << 'L'; break;
851 }
852}
853
854void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
855 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
856}
857
858void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
859 PrintExpr(Node->getSubExpr());
860 OS << "i";
861}
862
863void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
864 Str->outputString(OS);
865}
866void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
867 OS << "(";
868 PrintExpr(Node->getSubExpr());
869 OS << ")";
870}
871void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
872 if (!Node->isPostfix()) {
873 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
874
875 // Print a space if this is an "identifier operator" like __real, or if
876 // it might be concatenated incorrectly like '+'.
877 switch (Node->getOpcode()) {
878 default: break;
879 case UO_Real:
880 case UO_Imag:
881 case UO_Extension:
882 OS << ' ';
883 break;
884 case UO_Plus:
885 case UO_Minus:
886 if (isa<UnaryOperator>(Node->getSubExpr()))
887 OS << ' ';
888 break;
889 }
890 }
891 PrintExpr(Node->getSubExpr());
892
893 if (Node->isPostfix())
894 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
895}
896
897void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
898 OS << "__builtin_offsetof(";
899 Node->getTypeSourceInfo()->getType().print(OS, Policy);
900 OS << ", ";
901 bool PrintedSomething = false;
902 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
903 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
904 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
905 // Array node
906 OS << "[";
907 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
908 OS << "]";
909 PrintedSomething = true;
910 continue;
911 }
912
913 // Skip implicit base indirections.
914 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
915 continue;
916
917 // Field or identifier node.
918 IdentifierInfo *Id = ON.getFieldName();
919 if (!Id)
920 continue;
921
922 if (PrintedSomething)
923 OS << ".";
924 else
925 PrintedSomething = true;
926 OS << Id->getName();
927 }
928 OS << ")";
929}
930
931void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
932 switch(Node->getKind()) {
933 case UETT_SizeOf:
934 OS << "sizeof";
935 break;
936 case UETT_AlignOf:
937 if (Policy.LangOpts.CPlusPlus)
938 OS << "alignof";
939 else if (Policy.LangOpts.C11)
940 OS << "_Alignof";
941 else
942 OS << "__alignof";
943 break;
944 case UETT_VecStep:
945 OS << "vec_step";
946 break;
947 }
948 if (Node->isArgumentType()) {
949 OS << '(';
950 Node->getArgumentType().print(OS, Policy);
951 OS << ')';
952 } else {
953 OS << " ";
954 PrintExpr(Node->getArgumentExpr());
955 }
956}
957
958void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
959 OS << "_Generic(";
960 PrintExpr(Node->getControllingExpr());
961 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
962 OS << ", ";
963 QualType T = Node->getAssocType(i);
964 if (T.isNull())
965 OS << "default";
966 else
967 T.print(OS, Policy);
968 OS << ": ";
969 PrintExpr(Node->getAssocExpr(i));
970 }
971 OS << ")";
972}
973
974void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
975 PrintExpr(Node->getLHS());
976 OS << "[";
977 PrintExpr(Node->getRHS());
978 OS << "]";
979}
980
981void StmtPrinter::PrintCallArgs(CallExpr *Call) {
982 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
983 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
984 // Don't print any defaulted arguments
985 break;
986 }
987
988 if (i) OS << ", ";
989 PrintExpr(Call->getArg(i));
990 }
991}
992
993void StmtPrinter::VisitCallExpr(CallExpr *Call) {
994 PrintExpr(Call->getCallee());
995 OS << "(";
996 PrintCallArgs(Call);
997 OS << ")";
998}
999void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
1000 // FIXME: Suppress printing implicit bases (like "this")
1001 PrintExpr(Node->getBase());
1002
1003 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1004 FieldDecl *ParentDecl = ParentMember
1005 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : NULL;
1006
1007 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1008 OS << (Node->isArrow() ? "->" : ".");
1009
1010 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1011 if (FD->isAnonymousStructOrUnion())
1012 return;
1013
1014 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1015 Qualifier->print(OS, Policy);
1016 if (Node->hasTemplateKeyword())
1017 OS << "template ";
1018 OS << Node->getMemberNameInfo();
1019 if (Node->hasExplicitTemplateArgs())
1020 TemplateSpecializationType::PrintTemplateArgumentList(
1021 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1022}
1023void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1024 PrintExpr(Node->getBase());
1025 OS << (Node->isArrow() ? "->isa" : ".isa");
1026}
1027
1028void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1029 PrintExpr(Node->getBase());
1030 OS << ".";
1031 OS << Node->getAccessor().getName();
1032}
1033void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1034 OS << '(';
1035 Node->getTypeAsWritten().print(OS, Policy);
1036 OS << ')';
1037 PrintExpr(Node->getSubExpr());
1038}
1039void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1040 OS << '(';
1041 Node->getType().print(OS, Policy);
1042 OS << ')';
1043 PrintExpr(Node->getInitializer());
1044}
1045void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1046 // No need to print anything, simply forward to the sub expression.
1047 PrintExpr(Node->getSubExpr());
1048}
1049void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1050 PrintExpr(Node->getLHS());
1051 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1052 PrintExpr(Node->getRHS());
1053}
1054void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1055 PrintExpr(Node->getLHS());
1056 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1057 PrintExpr(Node->getRHS());
1058}
1059void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1060 PrintExpr(Node->getCond());
1061 OS << " ? ";
1062 PrintExpr(Node->getLHS());
1063 OS << " : ";
1064 PrintExpr(Node->getRHS());
1065}
1066
1067// GNU extensions.
1068
1069void
1070StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1071 PrintExpr(Node->getCommon());
1072 OS << " ?: ";
1073 PrintExpr(Node->getFalseExpr());
1074}
1075void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1076 OS << "&&" << Node->getLabel()->getName();
1077}
1078
1079void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1080 OS << "(";
1081 PrintRawCompoundStmt(E->getSubStmt());
1082 OS << ")";
1083}
1084
1085void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1086 OS << "__builtin_choose_expr(";
1087 PrintExpr(Node->getCond());
1088 OS << ", ";
1089 PrintExpr(Node->getLHS());
1090 OS << ", ";
1091 PrintExpr(Node->getRHS());
1092 OS << ")";
1093}
1094
1095void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1096 OS << "__null";
1097}
1098
1099void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1100 OS << "__builtin_shufflevector(";
1101 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1102 if (i) OS << ", ";
1103 PrintExpr(Node->getExpr(i));
1104 }
1105 OS << ")";
1106}
1107
1108void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1109 OS << "__builtin_convertvector(";
1110 PrintExpr(Node->getSrcExpr());
1111 OS << ", ";
1112 Node->getType().print(OS, Policy);
1113 OS << ")";
1114}
1115
1116void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1117 if (Node->getSyntacticForm()) {
1118 Visit(Node->getSyntacticForm());
1119 return;
1120 }
1121
1122 OS << "{ ";
1123 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1124 if (i) OS << ", ";
1125 if (Node->getInit(i))
1126 PrintExpr(Node->getInit(i));
1127 else
1128 OS << "0";
1129 }
1130 OS << " }";
1131}
1132
1133void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1134 OS << "( ";
1135 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1136 if (i) OS << ", ";
1137 PrintExpr(Node->getExpr(i));
1138 }
1139 OS << " )";
1140}
1141
1142void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1143 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1144 DEnd = Node->designators_end();
1145 D != DEnd; ++D) {
1146 if (D->isFieldDesignator()) {
1147 if (D->getDotLoc().isInvalid())
1148 OS << D->getFieldName()->getName() << ":";
1149 else
1150 OS << "." << D->getFieldName()->getName();
1151 } else {
1152 OS << "[";
1153 if (D->isArrayDesignator()) {
1154 PrintExpr(Node->getArrayIndex(*D));
1155 } else {
1156 PrintExpr(Node->getArrayRangeStart(*D));
1157 OS << " ... ";
1158 PrintExpr(Node->getArrayRangeEnd(*D));
1159 }
1160 OS << "]";
1161 }
1162 }
1163
1164 OS << " = ";
1165 PrintExpr(Node->getInit());
1166}
1167
1168void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1169 if (Policy.LangOpts.CPlusPlus) {
1170 OS << "/*implicit*/";
1171 Node->getType().print(OS, Policy);
1172 OS << "()";
1173 } else {
1174 OS << "/*implicit*/(";
1175 Node->getType().print(OS, Policy);
1176 OS << ')';
1177 if (Node->getType()->isRecordType())
1178 OS << "{}";
1179 else
1180 OS << 0;
1181 }
1182}
1183
1184void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1185 OS << "__builtin_va_arg(";
1186 PrintExpr(Node->getSubExpr());
1187 OS << ", ";
1188 Node->getType().print(OS, Policy);
1189 OS << ")";
1190}
1191
1192void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1193 PrintExpr(Node->getSyntacticForm());
1194}
1195
1196void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1197 const char *Name = 0;
1198 switch (Node->getOp()) {
1199#define BUILTIN(ID, TYPE, ATTRS)
1200#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1201 case AtomicExpr::AO ## ID: \
1202 Name = #ID "("; \
1203 break;
1204#include "clang/Basic/Builtins.def"
1205 }
1206 OS << Name;
1207
1208 // AtomicExpr stores its subexpressions in a permuted order.
1209 PrintExpr(Node->getPtr());
1210 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1211 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
1212 OS << ", ";
1213 PrintExpr(Node->getVal1());
1214 }
1215 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1216 Node->isCmpXChg()) {
1217 OS << ", ";
1218 PrintExpr(Node->getVal2());
1219 }
1220 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1221 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1222 OS << ", ";
1223 PrintExpr(Node->getWeak());
1224 }
1225 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1226 OS << ", ";
1227 PrintExpr(Node->getOrder());
1228 }
1229 if (Node->isCmpXChg()) {
1230 OS << ", ";
1231 PrintExpr(Node->getOrderFail());
1232 }
1233 OS << ")";
1234}
1235
1236// C++
1237void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1238 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1239 "",
1240#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1241 Spelling,
1242#include "clang/Basic/OperatorKinds.def"
1243 };
1244
1245 OverloadedOperatorKind Kind = Node->getOperator();
1246 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1247 if (Node->getNumArgs() == 1) {
1248 OS << OpStrings[Kind] << ' ';
1249 PrintExpr(Node->getArg(0));
1250 } else {
1251 PrintExpr(Node->getArg(0));
1252 OS << ' ' << OpStrings[Kind];
1253 }
1254 } else if (Kind == OO_Arrow) {
1255 PrintExpr(Node->getArg(0));
1256 } else if (Kind == OO_Call) {
1257 PrintExpr(Node->getArg(0));
1258 OS << '(';
1259 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1260 if (ArgIdx > 1)
1261 OS << ", ";
1262 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1263 PrintExpr(Node->getArg(ArgIdx));
1264 }
1265 OS << ')';
1266 } else if (Kind == OO_Subscript) {
1267 PrintExpr(Node->getArg(0));
1268 OS << '[';
1269 PrintExpr(Node->getArg(1));
1270 OS << ']';
1271 } else if (Node->getNumArgs() == 1) {
1272 OS << OpStrings[Kind] << ' ';
1273 PrintExpr(Node->getArg(0));
1274 } else if (Node->getNumArgs() == 2) {
1275 PrintExpr(Node->getArg(0));
1276 OS << ' ' << OpStrings[Kind] << ' ';
1277 PrintExpr(Node->getArg(1));
1278 } else {
1279 llvm_unreachable("unknown overloaded operator");
1280 }
1281}
1282
1283void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1284 VisitCallExpr(cast<CallExpr>(Node));
1285}
1286
1287void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1288 PrintExpr(Node->getCallee());
1289 OS << "<<<";
1290 PrintCallArgs(Node->getConfig());
1291 OS << ">>>(";
1292 PrintCallArgs(Node);
1293 OS << ")";
1294}
1295
1296void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1297 OS << Node->getCastName() << '<';
1298 Node->getTypeAsWritten().print(OS, Policy);
1299 OS << ">(";
1300 PrintExpr(Node->getSubExpr());
1301 OS << ")";
1302}
1303
1304void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1305 VisitCXXNamedCastExpr(Node);
1306}
1307
1308void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1309 VisitCXXNamedCastExpr(Node);
1310}
1311
1312void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1313 VisitCXXNamedCastExpr(Node);
1314}
1315
1316void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1317 VisitCXXNamedCastExpr(Node);
1318}
1319
1320void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1321 OS << "typeid(";
1322 if (Node->isTypeOperand()) {
1323 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1324 } else {
1325 PrintExpr(Node->getExprOperand());
1326 }
1327 OS << ")";
1328}
1329
1330void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1331 OS << "__uuidof(";
1332 if (Node->isTypeOperand()) {
1333 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1334 } else {
1335 PrintExpr(Node->getExprOperand());
1336 }
1337 OS << ")";
1338}
1339
1340void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1341 PrintExpr(Node->getBaseExpr());
1342 if (Node->isArrow())
1343 OS << "->";
1344 else
1345 OS << ".";
1346 if (NestedNameSpecifier *Qualifier =
1347 Node->getQualifierLoc().getNestedNameSpecifier())
1348 Qualifier->print(OS, Policy);
1349 OS << Node->getPropertyDecl()->getDeclName();
1350}
1351
1352void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1353 switch (Node->getLiteralOperatorKind()) {
1354 case UserDefinedLiteral::LOK_Raw:
1355 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
1356 break;
1357 case UserDefinedLiteral::LOK_Template: {
1358 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1359 const TemplateArgumentList *Args =
1360 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1361 assert(Args);
1362 const TemplateArgument &Pack = Args->get(0);
1363 for (TemplateArgument::pack_iterator I = Pack.pack_begin(),
1364 E = Pack.pack_end(); I != E; ++I) {
1365 char C = (char)I->getAsIntegral().getZExtValue();
1366 OS << C;
1367 }
1368 break;
1369 }
1370 case UserDefinedLiteral::LOK_Integer: {
1371 // Print integer literal without suffix.
1372 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1373 OS << Int->getValue().toString(10, /*isSigned*/false);
1374 break;
1375 }
1376 case UserDefinedLiteral::LOK_Floating: {
1377 // Print floating literal without suffix.
1378 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1379 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1380 break;
1381 }
1382 case UserDefinedLiteral::LOK_String:
1383 case UserDefinedLiteral::LOK_Character:
1384 PrintExpr(Node->getCookedLiteral());
1385 break;
1386 }
1387 OS << Node->getUDSuffix()->getName();
1388}
1389
1390void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1391 OS << (Node->getValue() ? "true" : "false");
1392}
1393
1394void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1395 OS << "nullptr";
1396}
1397
1398void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1399 OS << "this";
1400}
1401
1402void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1403 if (Node->getSubExpr() == 0)
1404 OS << "throw";
1405 else {
1406 OS << "throw ";
1407 PrintExpr(Node->getSubExpr());
1408 }
1409}
1410
1411void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1412 // Nothing to print: we picked up the default argument.
1413}
1414
1415void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1416 // Nothing to print: we picked up the default initializer.
1417}
1418
1419void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1420 Node->getType().print(OS, Policy);
1421 OS << "(";
1422 PrintExpr(Node->getSubExpr());
1423 OS << ")";
1424}
1425
1426void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1427 PrintExpr(Node->getSubExpr());
1428}
1429
1430void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1431 Node->getType().print(OS, Policy);
1432 OS << "(";
1433 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1434 ArgEnd = Node->arg_end();
1435 Arg != ArgEnd; ++Arg) {
1436 if (Arg->isDefaultArgument())
1437 break;
1438 if (Arg != Node->arg_begin())
1439 OS << ", ";
1440 PrintExpr(*Arg);
1441 }
1442 OS << ")";
1443}
1444
1445void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1446 OS << '[';
1447 bool NeedComma = false;
1448 switch (Node->getCaptureDefault()) {
1449 case LCD_None:
1450 break;
1451
1452 case LCD_ByCopy:
1453 OS << '=';
1454 NeedComma = true;
1455 break;
1456
1457 case LCD_ByRef:
1458 OS << '&';
1459 NeedComma = true;
1460 break;
1461 }
1462 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1463 CEnd = Node->explicit_capture_end();
1464 C != CEnd;
1465 ++C) {
1466 if (NeedComma)
1467 OS << ", ";
1468 NeedComma = true;
1469
1470 switch (C->getCaptureKind()) {
1471 case LCK_This:
1472 OS << "this";
1473 break;
1474
1475 case LCK_ByRef:
1476 if (Node->getCaptureDefault() != LCD_ByRef || C->isInitCapture())
1477 OS << '&';
1478 OS << C->getCapturedVar()->getName();
1479 break;
1480
1481 case LCK_ByCopy:
1482 OS << C->getCapturedVar()->getName();
1483 break;
1484 }
1485
1486 if (C->isInitCapture())
1487 PrintExpr(C->getCapturedVar()->getInit());
1488 }
1489 OS << ']';
1490
1491 if (Node->hasExplicitParameters()) {
1492 OS << " (";
1493 CXXMethodDecl *Method = Node->getCallOperator();
1494 NeedComma = false;
1495 for (CXXMethodDecl::param_iterator P = Method->param_begin(),
1496 PEnd = Method->param_end();
1497 P != PEnd; ++P) {
1498 if (NeedComma) {
1499 OS << ", ";
1500 } else {
1501 NeedComma = true;
1502 }
1503 std::string ParamStr = (*P)->getNameAsString();
1504 (*P)->getOriginalType().print(OS, Policy, ParamStr);
1505 }
1506 if (Method->isVariadic()) {
1507 if (NeedComma)
1508 OS << ", ";
1509 OS << "...";
1510 }
1511 OS << ')';
1512
1513 if (Node->isMutable())
1514 OS << " mutable";
1515
1516 const FunctionProtoType *Proto
1517 = Method->getType()->getAs<FunctionProtoType>();
1518 Proto->printExceptionSpecification(OS, Policy);
1519
1520 // FIXME: Attributes
1521
1522 // Print the trailing return type if it was specified in the source.
1523 if (Node->hasExplicitResultType()) {
1524 OS << " -> ";
1525 Proto->getResultType().print(OS, Policy);
1526 }
1527 }
1528
1529 // Print the body.
1530 CompoundStmt *Body = Node->getBody();
1531 OS << ' ';
1532 PrintStmt(Body);
1533}
1534
1535void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
1536 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1537 TSInfo->getType().print(OS, Policy);
1538 else
1539 Node->getType().print(OS, Policy);
1540 OS << "()";
1541}
1542
1543void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1544 if (E->isGlobalNew())
1545 OS << "::";
1546 OS << "new ";
1547 unsigned NumPlace = E->getNumPlacementArgs();
1548 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
1549 OS << "(";
1550 PrintExpr(E->getPlacementArg(0));
1551 for (unsigned i = 1; i < NumPlace; ++i) {
1552 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
1553 break;
1554 OS << ", ";
1555 PrintExpr(E->getPlacementArg(i));
1556 }
1557 OS << ") ";
1558 }
1559 if (E->isParenTypeId())
1560 OS << "(";
1561 std::string TypeS;
1562 if (Expr *Size = E->getArraySize()) {
1563 llvm::raw_string_ostream s(TypeS);
1564 s << '[';
1565 Size->printPretty(s, Helper, Policy);
1566 s << ']';
1567 }
1568 E->getAllocatedType().print(OS, Policy, TypeS);
1569 if (E->isParenTypeId())
1570 OS << ")";
1571
1572 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1573 if (InitStyle) {
1574 if (InitStyle == CXXNewExpr::CallInit)
1575 OS << "(";
1576 PrintExpr(E->getInitializer());
1577 if (InitStyle == CXXNewExpr::CallInit)
1578 OS << ")";
1579 }
1580}
1581
1582void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1583 if (E->isGlobalDelete())
1584 OS << "::";
1585 OS << "delete ";
1586 if (E->isArrayForm())
1587 OS << "[] ";
1588 PrintExpr(E->getArgument());
1589}
1590
1591void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1592 PrintExpr(E->getBase());
1593 if (E->isArrow())
1594 OS << "->";
1595 else
1596 OS << '.';
1597 if (E->getQualifier())
1598 E->getQualifier()->print(OS, Policy);
1599 OS << "~";
1600
1601 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1602 OS << II->getName();
1603 else
1604 E->getDestroyedType().print(OS, Policy);
1605}
1606
1607void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
1608 if (E->isListInitialization())
1609 OS << "{ ";
1610
1611 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1612 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1613 // Don't print any defaulted arguments
1614 break;
1615 }
1616
1617 if (i) OS << ", ";
1618 PrintExpr(E->getArg(i));
1619 }
1620
1621 if (E->isListInitialization())
1622 OS << " }";
1623}
1624
1625void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
1626 PrintExpr(E->getSubExpr());
1627}
1628
1629void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
1630 // Just forward to the sub expression.
1631 PrintExpr(E->getSubExpr());
1632}
1633
1634void
1635StmtPrinter::VisitCXXUnresolvedConstructExpr(
1636 CXXUnresolvedConstructExpr *Node) {
1637 Node->getTypeAsWritten().print(OS, Policy);
1638 OS << "(";
1639 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
1640 ArgEnd = Node->arg_end();
1641 Arg != ArgEnd; ++Arg) {
1642 if (Arg != Node->arg_begin())
1643 OS << ", ";
1644 PrintExpr(*Arg);
1645 }
1646 OS << ")";
1647}
1648
1649void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1650 CXXDependentScopeMemberExpr *Node) {
1651 if (!Node->isImplicitAccess()) {
1652 PrintExpr(Node->getBase());
1653 OS << (Node->isArrow() ? "->" : ".");
1654 }
1655 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1656 Qualifier->print(OS, Policy);
1657 if (Node->hasTemplateKeyword())
1658 OS << "template ";
1659 OS << Node->getMemberNameInfo();
1660 if (Node->hasExplicitTemplateArgs())
1661 TemplateSpecializationType::PrintTemplateArgumentList(
1662 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1663}
1664
1665void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
1666 if (!Node->isImplicitAccess()) {
1667 PrintExpr(Node->getBase());
1668 OS << (Node->isArrow() ? "->" : ".");
1669 }
1670 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1671 Qualifier->print(OS, Policy);
1672 if (Node->hasTemplateKeyword())
1673 OS << "template ";
1674 OS << Node->getMemberNameInfo();
1675 if (Node->hasExplicitTemplateArgs())
1676 TemplateSpecializationType::PrintTemplateArgumentList(
1677 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1678}
1679
1680static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1681 switch (UTT) {
1682 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1683 case UTT_HasNothrowMoveAssign: return "__has_nothrow_move_assign";
1684 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1685 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1686 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1687 case UTT_HasTrivialMoveAssign: return "__has_trivial_move_assign";
1688 case UTT_HasTrivialMoveConstructor: return "__has_trivial_move_constructor";
1689 case UTT_HasTrivialDefaultConstructor: return "__has_trivial_constructor";
1690 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1691 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1692 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1693 case UTT_IsAbstract: return "__is_abstract";
1694 case UTT_IsArithmetic: return "__is_arithmetic";
1695 case UTT_IsArray: return "__is_array";
1696 case UTT_IsClass: return "__is_class";
1697 case UTT_IsCompleteType: return "__is_complete_type";
1698 case UTT_IsCompound: return "__is_compound";
1699 case UTT_IsConst: return "__is_const";
1700 case UTT_IsEmpty: return "__is_empty";
1701 case UTT_IsEnum: return "__is_enum";
1702 case UTT_IsFinal: return "__is_final";
1703 case UTT_IsFloatingPoint: return "__is_floating_point";
1704 case UTT_IsFunction: return "__is_function";
1705 case UTT_IsFundamental: return "__is_fundamental";
1706 case UTT_IsIntegral: return "__is_integral";
1707 case UTT_IsInterfaceClass: return "__is_interface_class";
1708 case UTT_IsLiteral: return "__is_literal";
1709 case UTT_IsLvalueReference: return "__is_lvalue_reference";
1710 case UTT_IsMemberFunctionPointer: return "__is_member_function_pointer";
1711 case UTT_IsMemberObjectPointer: return "__is_member_object_pointer";
1712 case UTT_IsMemberPointer: return "__is_member_pointer";
1713 case UTT_IsObject: return "__is_object";
1714 case UTT_IsPOD: return "__is_pod";
1715 case UTT_IsPointer: return "__is_pointer";
1716 case UTT_IsPolymorphic: return "__is_polymorphic";
1717 case UTT_IsReference: return "__is_reference";
1718 case UTT_IsRvalueReference: return "__is_rvalue_reference";
1719 case UTT_IsScalar: return "__is_scalar";
1720 case UTT_IsSealed: return "__is_sealed";
1721 case UTT_IsSigned: return "__is_signed";
1722 case UTT_IsStandardLayout: return "__is_standard_layout";
1723 case UTT_IsTrivial: return "__is_trivial";
1724 case UTT_IsTriviallyCopyable: return "__is_trivially_copyable";
1725 case UTT_IsUnion: return "__is_union";
1726 case UTT_IsUnsigned: return "__is_unsigned";
1727 case UTT_IsVoid: return "__is_void";
1728 case UTT_IsVolatile: return "__is_volatile";
1729 }
1730 llvm_unreachable("Type trait not covered by switch statement");
1731}
1732
1733static const char *getTypeTraitName(BinaryTypeTrait BTT) {
1734 switch (BTT) {
1735 case BTT_IsBaseOf: return "__is_base_of";
1736 case BTT_IsConvertible: return "__is_convertible";
1737 case BTT_IsSame: return "__is_same";
1738 case BTT_TypeCompatible: return "__builtin_types_compatible_p";
1739 case BTT_IsConvertibleTo: return "__is_convertible_to";
1740 case BTT_IsTriviallyAssignable: return "__is_trivially_assignable";
1741 }
1742 llvm_unreachable("Binary type trait not covered by switch");
1743}
1744
1745static const char *getTypeTraitName(TypeTrait TT) {
1746 switch (TT) {
1747 case clang::TT_IsTriviallyConstructible:return "__is_trivially_constructible";
1748 }
1749 llvm_unreachable("Type trait not covered by switch");
1750}
1751
1752static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1753 switch (ATT) {
1754 case ATT_ArrayRank: return "__array_rank";
1755 case ATT_ArrayExtent: return "__array_extent";
1756 }
1757 llvm_unreachable("Array type trait not covered by switch");
1758}
1759
1760static const char *getExpressionTraitName(ExpressionTrait ET) {
1761 switch (ET) {
1762 case ET_IsLValueExpr: return "__is_lvalue_expr";
1763 case ET_IsRValueExpr: return "__is_rvalue_expr";
1764 }
1765 llvm_unreachable("Expression type trait not covered by switch");
1766}
1767
1768void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1769 OS << getTypeTraitName(E->getTrait()) << '(';
1770 E->getQueriedType().print(OS, Policy);
1771 OS << ')';
1772}
1773
1774void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
1775 OS << getTypeTraitName(E->getTrait()) << '(';
1776 E->getLhsType().print(OS, Policy);
1777 OS << ',';
1778 E->getRhsType().print(OS, Policy);
1779 OS << ')';
1780}
1781
1782void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1783 OS << getTypeTraitName(E->getTrait()) << "(";
1784 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
1785 if (I > 0)
1786 OS << ", ";
1787 E->getArg(I)->getType().print(OS, Policy);
1788 }
1789 OS << ")";
1790}
1791
1792void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
1793 OS << getTypeTraitName(E->getTrait()) << '(';
1794 E->getQueriedType().print(OS, Policy);
1795 OS << ')';
1796}
1797
1798void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
1799 OS << getExpressionTraitName(E->getTrait()) << '(';
1800 PrintExpr(E->getQueriedExpression());
1801 OS << ')';
1802}
1803
1804void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1805 OS << "noexcept(";
1806 PrintExpr(E->getOperand());
1807 OS << ")";
1808}
1809
1810void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
1811 PrintExpr(E->getPattern());
1812 OS << "...";
1813}
1814
1815void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
1816 OS << "sizeof...(" << *E->getPack() << ")";
1817}
1818
1819void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1820 SubstNonTypeTemplateParmPackExpr *Node) {
1821 OS << *Node->getParameterPack();
1822}
1823
1824void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
1825 SubstNonTypeTemplateParmExpr *Node) {
1826 Visit(Node->getReplacement());
1827}
1828
1829void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
1830 OS << *E->getParameterPack();
1831}
1832
1833void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
1834 PrintExpr(Node->GetTemporaryExpr());
1835}
1836
1837// Obj-C
1838
1839void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1840 OS << "@";
1841 VisitStringLiteral(Node->getString());
1842}
1843
1844void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
1845 OS << "@";
1846 Visit(E->getSubExpr());
1847}
1848
1849void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
1850 OS << "@[ ";
1851 StmtRange ch = E->children();
1852 if (ch.first != ch.second) {
1853 while (1) {
1854 Visit(*ch.first);
1855 ++ch.first;
1856 if (ch.first == ch.second) break;
1857 OS << ", ";
1858 }
1859 }
1860 OS << " ]";
1861}
1862
1863void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
1864 OS << "@{ ";
1865 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
1866 if (I > 0)
1867 OS << ", ";
1868
1869 ObjCDictionaryElement Element = E->getKeyValueElement(I);
1870 Visit(Element.Key);
1871 OS << " : ";
1872 Visit(Element.Value);
1873 if (Element.isPackExpansion())
1874 OS << "...";
1875 }
1876 OS << " }";
1877}
1878
1879void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
1880 OS << "@encode(";
1881 Node->getEncodedType().print(OS, Policy);
1882 OS << ')';
1883}
1884
1885void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
1886 OS << "@selector(" << Node->getSelector().getAsString() << ')';
1887}
1888
1889void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
1890 OS << "@protocol(" << *Node->getProtocol() << ')';
1891}
1892
1893void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1894 OS << "[";
1895 switch (Mess->getReceiverKind()) {
1896 case ObjCMessageExpr::Instance:
1897 PrintExpr(Mess->getInstanceReceiver());
1898 break;
1899
1900 case ObjCMessageExpr::Class:
1901 Mess->getClassReceiver().print(OS, Policy);
1902 break;
1903
1904 case ObjCMessageExpr::SuperInstance:
1905 case ObjCMessageExpr::SuperClass:
1906 OS << "Super";
1907 break;
1908 }
1909
1910 OS << ' ';
1911 Selector selector = Mess->getSelector();
1912 if (selector.isUnarySelector()) {
1913 OS << selector.getNameForSlot(0);
1914 } else {
1915 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
1916 if (i < selector.getNumArgs()) {
1917 if (i > 0) OS << ' ';
1918 if (selector.getIdentifierInfoForSlot(i))
1919 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
1920 else
1921 OS << ":";
1922 }
1923 else OS << ", "; // Handle variadic methods.
1924
1925 PrintExpr(Mess->getArg(i));
1926 }
1927 }
1928 OS << "]";
1929}
1930
1931void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
1932 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
1933}
1934
1935void
1936StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
1937 PrintExpr(E->getSubExpr());
1938}
1939
1940void
1941StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
1942 OS << '(' << E->getBridgeKindName();
1943 E->getType().print(OS, Policy);
1944 OS << ')';
1945 PrintExpr(E->getSubExpr());
1946}
1947
1948void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
1949 BlockDecl *BD = Node->getBlockDecl();
1950 OS << "^";
1951
1952 const FunctionType *AFT = Node->getFunctionType();
1953
1954 if (isa<FunctionNoProtoType>(AFT)) {
1955 OS << "()";
1956 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
1957 OS << '(';
1958 for (BlockDecl::param_iterator AI = BD->param_begin(),
1959 E = BD->param_end(); AI != E; ++AI) {
1960 if (AI != BD->param_begin()) OS << ", ";
1961 std::string ParamStr = (*AI)->getNameAsString();
1962 (*AI)->getType().print(OS, Policy, ParamStr);
1963 }
1964
1965 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
1966 if (FT->isVariadic()) {
1967 if (!BD->param_empty()) OS << ", ";
1968 OS << "...";
1969 }
1970 OS << ')';
1971 }
1972 OS << "{ }";
1973}
1974
1975void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
1976 PrintExpr(Node->getSourceExpr());
1977}
1978
1979void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
1980 OS << "__builtin_astype(";
1981 PrintExpr(Node->getSrcExpr());
1982 OS << ", ";
1983 Node->getType().print(OS, Policy);
1984 OS << ")";
1985}
1986
1987//===----------------------------------------------------------------------===//
1988// Stmt method implementations
1989//===----------------------------------------------------------------------===//
1990
1991void Stmt::dumpPretty(const ASTContext &Context) const {
1992 printPretty(llvm::errs(), 0, PrintingPolicy(Context.getLangOpts()));
1993}
1994
1995void Stmt::printPretty(raw_ostream &OS,
1996 PrinterHelper *Helper,
1997 const PrintingPolicy &Policy,
1998 unsigned Indentation) const {
1999 if (this == 0) {
2000 OS << "<NULL>";
2001 return;
2002 }
2003
2004 StmtPrinter P(OS, Helper, Policy, Indentation);
2005 P.Visit(const_cast<Stmt*>(this));
2006}
2007
2008//===----------------------------------------------------------------------===//
2009// PrinterHelper
2010//===----------------------------------------------------------------------===//
2011
2012// Implement virtual destructor.
2013PrinterHelper::~PrinterHelper() {}