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