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