1 //===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which
11 // pretty print the AST back out to C code.
13 //===----------------------------------------------------------------------===//
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;
29 //===----------------------------------------------------------------------===//
30 // StmtPrinter Visitor
31 //===----------------------------------------------------------------------===//
34 class StmtPrinter : public StmtVisitor<StmtPrinter> {
37 clang::PrinterHelper* Helper;
38 PrintingPolicy Policy;
41 StmtPrinter(raw_ostream &os, PrinterHelper* helper,
42 const PrintingPolicy &Policy,
43 unsigned Indentation = 0)
44 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy) {}
46 void PrintStmt(Stmt *S) {
47 PrintStmt(S, Policy.Indentation);
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.
60 Indent() << "<<<NULL STATEMENT>>>\n";
62 IndentLevel -= SubIndent;
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);
74 void PrintExpr(Expr *E) {
81 raw_ostream &Indent(int Delta = 0) {
82 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
88 if (Helper && Helper->handledStmt(S,OS))
90 else StmtVisitor<StmtPrinter>::Visit(S);
93 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
94 Indent() << "<<unknown stmt type>>\n";
96 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
97 OS << "<<unknown expr type>>";
99 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
101 #define ABSTRACT_STMT(CLASS)
102 #define STMT(CLASS, PARENT) \
103 void Visit##CLASS(CLASS *Node);
104 #include "clang/AST/StmtNodes.inc"
108 //===----------------------------------------------------------------------===//
109 // Stmt printing methods.
110 //===----------------------------------------------------------------------===//
112 /// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
113 /// with no newline after the }.
114 void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
116 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
123 void StmtPrinter::PrintRawDecl(Decl *D) {
124 D->print(OS, Policy, IndentLevel);
127 void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
128 DeclStmt::const_decl_iterator Begin = S->decl_begin(), End = S->decl_end();
129 SmallVector<Decl*, 2> Decls;
130 for ( ; Begin != End; ++Begin)
131 Decls.push_back(*Begin);
133 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
136 void StmtPrinter::VisitNullStmt(NullStmt *Node) {
140 void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
142 PrintRawDeclStmt(Node);
146 void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
148 PrintRawCompoundStmt(Node);
152 void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
153 Indent(-1) << "case ";
154 PrintExpr(Node->getLHS());
155 if (Node->getRHS()) {
157 PrintExpr(Node->getRHS());
161 PrintStmt(Node->getSubStmt(), 0);
164 void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
165 Indent(-1) << "default:\n";
166 PrintStmt(Node->getSubStmt(), 0);
169 void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
170 Indent(-1) << Node->getName() << ":\n";
171 PrintStmt(Node->getSubStmt(), 0);
174 void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
177 for (ArrayRef<const Attr*>::iterator it = Node->getAttrs().begin(),
178 end = Node->getAttrs().end();
185 (*it)->printPretty(OS, Policy);
188 PrintStmt(Node->getSubStmt(), 0);
191 void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
193 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
194 PrintRawDeclStmt(DS);
196 PrintExpr(If->getCond());
199 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
201 PrintRawCompoundStmt(CS);
202 OS << (If->getElse() ? ' ' : '\n');
205 PrintStmt(If->getThen());
206 if (If->getElse()) Indent();
209 if (Stmt *Else = If->getElse()) {
212 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
214 PrintRawCompoundStmt(CS);
216 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
218 PrintRawIfStmt(ElseIf);
221 PrintStmt(If->getElse());
226 void StmtPrinter::VisitIfStmt(IfStmt *If) {
231 void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
232 Indent() << "switch (";
233 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
234 PrintRawDeclStmt(DS);
236 PrintExpr(Node->getCond());
239 // Pretty print compoundstmt bodies (very common).
240 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
242 PrintRawCompoundStmt(CS);
246 PrintStmt(Node->getBody());
250 void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
251 Indent() << "while (";
252 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
253 PrintRawDeclStmt(DS);
255 PrintExpr(Node->getCond());
257 PrintStmt(Node->getBody());
260 void StmtPrinter::VisitDoStmt(DoStmt *Node) {
262 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
263 PrintRawCompoundStmt(CS);
267 PrintStmt(Node->getBody());
272 PrintExpr(Node->getCond());
276 void StmtPrinter::VisitForStmt(ForStmt *Node) {
278 if (Node->getInit()) {
279 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
280 PrintRawDeclStmt(DS);
282 PrintExpr(cast<Expr>(Node->getInit()));
285 if (Node->getCond()) {
287 PrintExpr(Node->getCond());
290 if (Node->getInc()) {
292 PrintExpr(Node->getInc());
296 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
297 PrintRawCompoundStmt(CS);
301 PrintStmt(Node->getBody());
305 void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
307 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
308 PrintRawDeclStmt(DS);
310 PrintExpr(cast<Expr>(Node->getElement()));
312 PrintExpr(Node->getCollection());
315 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
316 PrintRawCompoundStmt(CS);
320 PrintStmt(Node->getBody());
324 void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
326 PrintingPolicy SubPolicy(Policy);
327 SubPolicy.SuppressInitializers = true;
328 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
330 PrintExpr(Node->getRangeInit());
332 PrintStmt(Node->getBody());
336 void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
338 if (Node->isIfExists())
339 OS << "__if_exists (";
341 OS << "__if_not_exists (";
343 if (NestedNameSpecifier *Qualifier
344 = Node->getQualifierLoc().getNestedNameSpecifier())
345 Qualifier->print(OS, Policy);
347 OS << Node->getNameInfo() << ") ";
349 PrintRawCompoundStmt(Node->getSubStmt());
352 void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
353 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
356 void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
357 Indent() << "goto *";
358 PrintExpr(Node->getTarget());
362 void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
363 Indent() << "continue;\n";
366 void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
367 Indent() << "break;\n";
371 void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
372 Indent() << "return";
373 if (Node->getRetValue()) {
375 PrintExpr(Node->getRetValue());
381 void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
384 if (Node->isVolatile())
388 VisitStringLiteral(Node->getAsmString());
391 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
392 Node->getNumClobbers() != 0)
395 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
399 if (!Node->getOutputName(i).empty()) {
401 OS << Node->getOutputName(i);
405 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
407 Visit(Node->getOutputExpr(i));
411 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
414 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
418 if (!Node->getInputName(i).empty()) {
420 OS << Node->getInputName(i);
424 VisitStringLiteral(Node->getInputConstraintLiteral(i));
426 Visit(Node->getInputExpr(i));
430 if (Node->getNumClobbers() != 0)
433 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
437 VisitStringLiteral(Node->getClobberStringLiteral(i));
443 void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
444 // FIXME: Implement MS style inline asm statement printer.
445 Indent() << "__asm ";
446 if (Node->hasBraces())
448 OS << Node->getAsmString() << "\n";
449 if (Node->hasBraces())
453 void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
454 PrintStmt(Node->getCapturedDecl()->getBody());
457 void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
459 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
460 PrintRawCompoundStmt(TS);
464 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
465 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
466 Indent() << "@catch(";
467 if (catchStmt->getCatchParamDecl()) {
468 if (Decl *DS = catchStmt->getCatchParamDecl())
472 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
473 PrintRawCompoundStmt(CS);
478 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
479 Node->getFinallyStmt())) {
480 Indent() << "@finally";
481 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
486 void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
489 void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
490 Indent() << "@catch (...) { /* todo */ } \n";
493 void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
494 Indent() << "@throw";
495 if (Node->getThrowExpr()) {
497 PrintExpr(Node->getThrowExpr());
502 void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
503 Indent() << "@synchronized (";
504 PrintExpr(Node->getSynchExpr());
506 PrintRawCompoundStmt(Node->getSynchBody());
510 void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
511 Indent() << "@autoreleasepool";
512 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
516 void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
518 if (Decl *ExDecl = Node->getExceptionDecl())
519 PrintRawDecl(ExDecl);
523 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
526 void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
528 PrintRawCXXCatchStmt(Node);
532 void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
534 PrintRawCompoundStmt(Node->getTryBlock());
535 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
537 PrintRawCXXCatchStmt(Node->getHandler(i));
542 void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
543 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
544 PrintRawCompoundStmt(Node->getTryBlock());
545 SEHExceptStmt *E = Node->getExceptHandler();
546 SEHFinallyStmt *F = Node->getFinallyHandler();
548 PrintRawSEHExceptHandler(E);
550 assert(F && "Must have a finally block...");
551 PrintRawSEHFinallyStmt(F);
556 void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
558 PrintRawCompoundStmt(Node->getBlock());
562 void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
564 VisitExpr(Node->getFilterExpr());
566 PrintRawCompoundStmt(Node->getBlock());
570 void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
572 PrintRawSEHExceptHandler(Node);
576 void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
578 PrintRawSEHFinallyStmt(Node);
582 //===----------------------------------------------------------------------===//
583 // OpenMP clauses printing methods
584 //===----------------------------------------------------------------------===//
587 class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> {
589 /// \brief Process clauses with list of variables.
590 template <typename T>
591 void VisitOMPClauseList(T *Node, char StartSym);
593 OMPClausePrinter(raw_ostream &OS) : OS(OS) { }
594 #define OPENMP_CLAUSE(Name, Class) \
595 void Visit##Class(Class *S);
596 #include "clang/Basic/OpenMPKinds.def"
599 void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
601 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
606 void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
607 for (typename T::varlist_iterator I = Node->varlist_begin(),
608 E = Node->varlist_end();
610 OS << (I == Node->varlist_begin() ? StartSym : ',')
611 << *cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
614 void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
615 if (!Node->varlist_empty()) {
617 VisitOMPClauseList(Node, '(');
622 void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
623 if (!Node->varlist_empty()) {
624 OS << "firstprivate";
625 VisitOMPClauseList(Node, '(');
630 void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
631 if (!Node->varlist_empty()) {
633 VisitOMPClauseList(Node, '(');
640 //===----------------------------------------------------------------------===//
641 // OpenMP directives printing methods
642 //===----------------------------------------------------------------------===//
644 void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
645 Indent() << "#pragma omp parallel ";
647 OMPClausePrinter Printer(OS);
648 ArrayRef<OMPClause *> Clauses = Node->clauses();
649 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
651 if (*I && !(*I)->isImplicit()) {
656 if (Node->getAssociatedStmt()) {
657 assert(isa<CapturedStmt>(Node->getAssociatedStmt()) &&
658 "Expected captured statement!");
659 Stmt *CS = cast<CapturedStmt>(Node->getAssociatedStmt())->getCapturedStmt();
663 //===----------------------------------------------------------------------===//
664 // Expr printing methods.
665 //===----------------------------------------------------------------------===//
667 void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
668 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
669 Qualifier->print(OS, Policy);
670 if (Node->hasTemplateKeyword())
672 OS << Node->getNameInfo();
673 if (Node->hasExplicitTemplateArgs())
674 TemplateSpecializationType::PrintTemplateArgumentList(
675 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
678 void StmtPrinter::VisitDependentScopeDeclRefExpr(
679 DependentScopeDeclRefExpr *Node) {
680 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
681 Qualifier->print(OS, Policy);
682 if (Node->hasTemplateKeyword())
684 OS << Node->getNameInfo();
685 if (Node->hasExplicitTemplateArgs())
686 TemplateSpecializationType::PrintTemplateArgumentList(
687 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
690 void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
691 if (Node->getQualifier())
692 Node->getQualifier()->print(OS, Policy);
693 if (Node->hasTemplateKeyword())
695 OS << Node->getNameInfo();
696 if (Node->hasExplicitTemplateArgs())
697 TemplateSpecializationType::PrintTemplateArgumentList(
698 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
701 void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
702 if (Node->getBase()) {
703 PrintExpr(Node->getBase());
704 OS << (Node->isArrow() ? "->" : ".");
706 OS << *Node->getDecl();
709 void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
710 if (Node->isSuperReceiver())
712 else if (Node->isObjectReceiver() && Node->getBase()) {
713 PrintExpr(Node->getBase());
715 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
716 OS << Node->getClassReceiver()->getName() << ".";
719 if (Node->isImplicitProperty())
720 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
722 OS << Node->getExplicitProperty()->getName();
725 void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
727 PrintExpr(Node->getBaseExpr());
729 PrintExpr(Node->getKeyExpr());
733 void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
734 switch (Node->getIdentType()) {
736 llvm_unreachable("unknown case");
737 case PredefinedExpr::Func:
740 case PredefinedExpr::Function:
741 OS << "__FUNCTION__";
743 case PredefinedExpr::FuncDName:
744 OS << "__FUNCDNAME__";
746 case PredefinedExpr::LFunction:
747 OS << "L__FUNCTION__";
749 case PredefinedExpr::PrettyFunction:
750 OS << "__PRETTY_FUNCTION__";
755 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
756 unsigned value = Node->getValue();
758 switch (Node->getKind()) {
759 case CharacterLiteral::Ascii: break; // no prefix.
760 case CharacterLiteral::Wide: OS << 'L'; break;
761 case CharacterLiteral::UTF16: OS << 'u'; break;
762 case CharacterLiteral::UTF32: OS << 'U'; break;
773 // TODO: K&R: the meaning of '\\a' is different in traditional C
779 // Nonstandard escape sequence.
799 if (value < 256 && isPrintable((unsigned char)value))
800 OS << "'" << (char)value << "'";
801 else if (value < 256)
802 OS << "'\\x" << llvm::format("%02x", value) << "'";
803 else if (value <= 0xFFFF)
804 OS << "'\\u" << llvm::format("%04x", value) << "'";
806 OS << "'\\U" << llvm::format("%08x", value) << "'";
810 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
811 bool isSigned = Node->getType()->isSignedIntegerType();
812 OS << Node->getValue().toString(10, isSigned);
814 // Emit suffixes. Integer literals are always a builtin integer type.
815 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
816 default: llvm_unreachable("Unexpected type for integer literal!");
817 // FIXME: The Short and UShort cases are to handle cases where a short
818 // integeral literal is formed during template instantiation. They should
819 // be removed when template instantiation no longer needs integer literals.
820 case BuiltinType::Short:
821 case BuiltinType::UShort:
822 case BuiltinType::Int: break; // no suffix.
823 case BuiltinType::UInt: OS << 'U'; break;
824 case BuiltinType::Long: OS << 'L'; break;
825 case BuiltinType::ULong: OS << "UL"; break;
826 case BuiltinType::LongLong: OS << "LL"; break;
827 case BuiltinType::ULongLong: OS << "ULL"; break;
828 case BuiltinType::Int128: OS << "i128"; break;
829 case BuiltinType::UInt128: OS << "Ui128"; break;
833 static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
836 Node->getValue().toString(Str);
838 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
839 OS << '.'; // Trailing dot in order to separate from ints.
844 // Emit suffixes. Float literals are always a builtin float type.
845 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
846 default: llvm_unreachable("Unexpected type for float literal!");
847 case BuiltinType::Half: break; // FIXME: suffix?
848 case BuiltinType::Double: break; // no suffix.
849 case BuiltinType::Float: OS << 'F'; break;
850 case BuiltinType::LongDouble: OS << 'L'; break;
854 void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
855 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
858 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
859 PrintExpr(Node->getSubExpr());
863 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
864 Str->outputString(OS);
866 void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
868 PrintExpr(Node->getSubExpr());
871 void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
872 if (!Node->isPostfix()) {
873 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
875 // Print a space if this is an "identifier operator" like __real, or if
876 // it might be concatenated incorrectly like '+'.
877 switch (Node->getOpcode()) {
886 if (isa<UnaryOperator>(Node->getSubExpr()))
891 PrintExpr(Node->getSubExpr());
893 if (Node->isPostfix())
894 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
897 void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
898 OS << "__builtin_offsetof(";
899 Node->getTypeSourceInfo()->getType().print(OS, Policy);
901 bool PrintedSomething = false;
902 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
903 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
904 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
907 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
909 PrintedSomething = true;
913 // Skip implicit base indirections.
914 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
917 // Field or identifier node.
918 IdentifierInfo *Id = ON.getFieldName();
922 if (PrintedSomething)
925 PrintedSomething = true;
931 void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
932 switch(Node->getKind()) {
937 if (Policy.LangOpts.CPlusPlus)
939 else if (Policy.LangOpts.C11)
948 if (Node->isArgumentType()) {
950 Node->getArgumentType().print(OS, Policy);
954 PrintExpr(Node->getArgumentExpr());
958 void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
960 PrintExpr(Node->getControllingExpr());
961 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
963 QualType T = Node->getAssocType(i);
969 PrintExpr(Node->getAssocExpr(i));
974 void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
975 PrintExpr(Node->getLHS());
977 PrintExpr(Node->getRHS());
981 void StmtPrinter::PrintCallArgs(CallExpr *Call) {
982 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
983 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
984 // Don't print any defaulted arguments
989 PrintExpr(Call->getArg(i));
993 void StmtPrinter::VisitCallExpr(CallExpr *Call) {
994 PrintExpr(Call->getCallee());
999 void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
1000 // FIXME: Suppress printing implicit bases (like "this")
1001 PrintExpr(Node->getBase());
1003 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1004 FieldDecl *ParentDecl = ParentMember
1005 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : NULL;
1007 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1008 OS << (Node->isArrow() ? "->" : ".");
1010 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1011 if (FD->isAnonymousStructOrUnion())
1014 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1015 Qualifier->print(OS, Policy);
1016 if (Node->hasTemplateKeyword())
1018 OS << Node->getMemberNameInfo();
1019 if (Node->hasExplicitTemplateArgs())
1020 TemplateSpecializationType::PrintTemplateArgumentList(
1021 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1023 void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1024 PrintExpr(Node->getBase());
1025 OS << (Node->isArrow() ? "->isa" : ".isa");
1028 void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
1029 PrintExpr(Node->getBase());
1031 OS << Node->getAccessor().getName();
1033 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
1035 Node->getTypeAsWritten().print(OS, Policy);
1037 PrintExpr(Node->getSubExpr());
1039 void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
1041 Node->getType().print(OS, Policy);
1043 PrintExpr(Node->getInitializer());
1045 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
1046 // No need to print anything, simply forward to the sub expression.
1047 PrintExpr(Node->getSubExpr());
1049 void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1050 PrintExpr(Node->getLHS());
1051 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1052 PrintExpr(Node->getRHS());
1054 void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1055 PrintExpr(Node->getLHS());
1056 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1057 PrintExpr(Node->getRHS());
1059 void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1060 PrintExpr(Node->getCond());
1062 PrintExpr(Node->getLHS());
1064 PrintExpr(Node->getRHS());
1070 StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1071 PrintExpr(Node->getCommon());
1073 PrintExpr(Node->getFalseExpr());
1075 void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
1076 OS << "&&" << Node->getLabel()->getName();
1079 void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1081 PrintRawCompoundStmt(E->getSubStmt());
1085 void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1086 OS << "__builtin_choose_expr(";
1087 PrintExpr(Node->getCond());
1089 PrintExpr(Node->getLHS());
1091 PrintExpr(Node->getRHS());
1095 void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1099 void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1100 OS << "__builtin_shufflevector(";
1101 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1103 PrintExpr(Node->getExpr(i));
1108 void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1109 OS << "__builtin_convertvector(";
1110 PrintExpr(Node->getSrcExpr());
1112 Node->getType().print(OS, Policy);
1116 void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
1117 if (Node->getSyntacticForm()) {
1118 Visit(Node->getSyntacticForm());
1123 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1125 if (Node->getInit(i))
1126 PrintExpr(Node->getInit(i));
1133 void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1135 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1137 PrintExpr(Node->getExpr(i));
1142 void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
1143 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1144 DEnd = Node->designators_end();
1146 if (D->isFieldDesignator()) {
1147 if (D->getDotLoc().isInvalid())
1148 OS << D->getFieldName()->getName() << ":";
1150 OS << "." << D->getFieldName()->getName();
1153 if (D->isArrayDesignator()) {
1154 PrintExpr(Node->getArrayIndex(*D));
1156 PrintExpr(Node->getArrayRangeStart(*D));
1158 PrintExpr(Node->getArrayRangeEnd(*D));
1165 PrintExpr(Node->getInit());
1168 void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
1169 if (Policy.LangOpts.CPlusPlus) {
1170 OS << "/*implicit*/";
1171 Node->getType().print(OS, Policy);
1174 OS << "/*implicit*/(";
1175 Node->getType().print(OS, Policy);
1177 if (Node->getType()->isRecordType())
1184 void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
1185 OS << "__builtin_va_arg(";
1186 PrintExpr(Node->getSubExpr());
1188 Node->getType().print(OS, Policy);
1192 void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1193 PrintExpr(Node->getSyntacticForm());
1196 void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1197 const char *Name = 0;
1198 switch (Node->getOp()) {
1199 #define BUILTIN(ID, TYPE, ATTRS)
1200 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1201 case AtomicExpr::AO ## ID: \
1204 #include "clang/Basic/Builtins.def"
1208 // AtomicExpr stores its subexpressions in a permuted order.
1209 PrintExpr(Node->getPtr());
1210 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1211 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
1213 PrintExpr(Node->getVal1());
1215 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1216 Node->isCmpXChg()) {
1218 PrintExpr(Node->getVal2());
1220 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1221 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1223 PrintExpr(Node->getWeak());
1225 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1227 PrintExpr(Node->getOrder());
1229 if (Node->isCmpXChg()) {
1231 PrintExpr(Node->getOrderFail());
1237 void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1238 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1240 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1242 #include "clang/Basic/OperatorKinds.def"
1245 OverloadedOperatorKind Kind = Node->getOperator();
1246 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1247 if (Node->getNumArgs() == 1) {
1248 OS << OpStrings[Kind] << ' ';
1249 PrintExpr(Node->getArg(0));
1251 PrintExpr(Node->getArg(0));
1252 OS << ' ' << OpStrings[Kind];
1254 } else if (Kind == OO_Arrow) {
1255 PrintExpr(Node->getArg(0));
1256 } else if (Kind == OO_Call) {
1257 PrintExpr(Node->getArg(0));
1259 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1262 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1263 PrintExpr(Node->getArg(ArgIdx));
1266 } else if (Kind == OO_Subscript) {
1267 PrintExpr(Node->getArg(0));
1269 PrintExpr(Node->getArg(1));
1271 } else if (Node->getNumArgs() == 1) {
1272 OS << OpStrings[Kind] << ' ';
1273 PrintExpr(Node->getArg(0));
1274 } else if (Node->getNumArgs() == 2) {
1275 PrintExpr(Node->getArg(0));
1276 OS << ' ' << OpStrings[Kind] << ' ';
1277 PrintExpr(Node->getArg(1));
1279 llvm_unreachable("unknown overloaded operator");
1283 void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1284 VisitCallExpr(cast<CallExpr>(Node));
1287 void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1288 PrintExpr(Node->getCallee());
1290 PrintCallArgs(Node->getConfig());
1292 PrintCallArgs(Node);
1296 void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1297 OS << Node->getCastName() << '<';
1298 Node->getTypeAsWritten().print(OS, Policy);
1300 PrintExpr(Node->getSubExpr());
1304 void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1305 VisitCXXNamedCastExpr(Node);
1308 void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1309 VisitCXXNamedCastExpr(Node);
1312 void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1313 VisitCXXNamedCastExpr(Node);
1316 void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1317 VisitCXXNamedCastExpr(Node);
1320 void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1322 if (Node->isTypeOperand()) {
1323 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1325 PrintExpr(Node->getExprOperand());
1330 void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1332 if (Node->isTypeOperand()) {
1333 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
1335 PrintExpr(Node->getExprOperand());
1340 void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1341 PrintExpr(Node->getBaseExpr());
1342 if (Node->isArrow())
1346 if (NestedNameSpecifier *Qualifier =
1347 Node->getQualifierLoc().getNestedNameSpecifier())
1348 Qualifier->print(OS, Policy);
1349 OS << Node->getPropertyDecl()->getDeclName();
1352 void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1353 switch (Node->getLiteralOperatorKind()) {
1354 case UserDefinedLiteral::LOK_Raw:
1355 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
1357 case UserDefinedLiteral::LOK_Template: {
1358 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1359 const TemplateArgumentList *Args =
1360 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1362 const TemplateArgument &Pack = Args->get(0);
1363 for (TemplateArgument::pack_iterator I = Pack.pack_begin(),
1364 E = Pack.pack_end(); I != E; ++I) {
1365 char C = (char)I->getAsIntegral().getZExtValue();
1370 case UserDefinedLiteral::LOK_Integer: {
1371 // Print integer literal without suffix.
1372 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1373 OS << Int->getValue().toString(10, /*isSigned*/false);
1376 case UserDefinedLiteral::LOK_Floating: {
1377 // Print floating literal without suffix.
1378 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1379 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1382 case UserDefinedLiteral::LOK_String:
1383 case UserDefinedLiteral::LOK_Character:
1384 PrintExpr(Node->getCookedLiteral());
1387 OS << Node->getUDSuffix()->getName();
1390 void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1391 OS << (Node->getValue() ? "true" : "false");
1394 void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1398 void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1402 void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1403 if (Node->getSubExpr() == 0)
1407 PrintExpr(Node->getSubExpr());
1411 void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1412 // Nothing to print: we picked up the default argument.
1415 void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1416 // Nothing to print: we picked up the default initializer.
1419 void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1420 Node->getType().print(OS, Policy);
1422 PrintExpr(Node->getSubExpr());
1426 void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1427 PrintExpr(Node->getSubExpr());
1430 void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1431 Node->getType().print(OS, Policy);
1433 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1434 ArgEnd = Node->arg_end();
1435 Arg != ArgEnd; ++Arg) {
1436 if (Arg->isDefaultArgument())
1438 if (Arg != Node->arg_begin())
1445 void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1447 bool NeedComma = false;
1448 switch (Node->getCaptureDefault()) {
1462 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1463 CEnd = Node->explicit_capture_end();
1470 switch (C->getCaptureKind()) {
1476 if (Node->getCaptureDefault() != LCD_ByRef || C->isInitCapture())
1478 OS << C->getCapturedVar()->getName();
1482 OS << C->getCapturedVar()->getName();
1486 if (C->isInitCapture())
1487 PrintExpr(C->getCapturedVar()->getInit());
1491 if (Node->hasExplicitParameters()) {
1493 CXXMethodDecl *Method = Node->getCallOperator();
1495 for (CXXMethodDecl::param_iterator P = Method->param_begin(),
1496 PEnd = Method->param_end();
1503 std::string ParamStr = (*P)->getNameAsString();
1504 (*P)->getOriginalType().print(OS, Policy, ParamStr);
1506 if (Method->isVariadic()) {
1513 if (Node->isMutable())
1516 const FunctionProtoType *Proto
1517 = Method->getType()->getAs<FunctionProtoType>();
1518 Proto->printExceptionSpecification(OS, Policy);
1520 // FIXME: Attributes
1522 // Print the trailing return type if it was specified in the source.
1523 if (Node->hasExplicitResultType()) {
1525 Proto->getResultType().print(OS, Policy);
1530 CompoundStmt *Body = Node->getBody();
1535 void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
1536 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1537 TSInfo->getType().print(OS, Policy);
1539 Node->getType().print(OS, Policy);
1543 void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1544 if (E->isGlobalNew())
1547 unsigned NumPlace = E->getNumPlacementArgs();
1548 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
1550 PrintExpr(E->getPlacementArg(0));
1551 for (unsigned i = 1; i < NumPlace; ++i) {
1552 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
1555 PrintExpr(E->getPlacementArg(i));
1559 if (E->isParenTypeId())
1562 if (Expr *Size = E->getArraySize()) {
1563 llvm::raw_string_ostream s(TypeS);
1565 Size->printPretty(s, Helper, Policy);
1568 E->getAllocatedType().print(OS, Policy, TypeS);
1569 if (E->isParenTypeId())
1572 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1574 if (InitStyle == CXXNewExpr::CallInit)
1576 PrintExpr(E->getInitializer());
1577 if (InitStyle == CXXNewExpr::CallInit)
1582 void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1583 if (E->isGlobalDelete())
1586 if (E->isArrayForm())
1588 PrintExpr(E->getArgument());
1591 void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1592 PrintExpr(E->getBase());
1597 if (E->getQualifier())
1598 E->getQualifier()->print(OS, Policy);
1601 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1602 OS << II->getName();
1604 E->getDestroyedType().print(OS, Policy);
1607 void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
1608 if (E->isListInitialization())
1611 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1612 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1613 // Don't print any defaulted arguments
1618 PrintExpr(E->getArg(i));
1621 if (E->isListInitialization())
1625 void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
1626 PrintExpr(E->getSubExpr());
1629 void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
1630 // Just forward to the sub expression.
1631 PrintExpr(E->getSubExpr());
1635 StmtPrinter::VisitCXXUnresolvedConstructExpr(
1636 CXXUnresolvedConstructExpr *Node) {
1637 Node->getTypeAsWritten().print(OS, Policy);
1639 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
1640 ArgEnd = Node->arg_end();
1641 Arg != ArgEnd; ++Arg) {
1642 if (Arg != Node->arg_begin())
1649 void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1650 CXXDependentScopeMemberExpr *Node) {
1651 if (!Node->isImplicitAccess()) {
1652 PrintExpr(Node->getBase());
1653 OS << (Node->isArrow() ? "->" : ".");
1655 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1656 Qualifier->print(OS, Policy);
1657 if (Node->hasTemplateKeyword())
1659 OS << Node->getMemberNameInfo();
1660 if (Node->hasExplicitTemplateArgs())
1661 TemplateSpecializationType::PrintTemplateArgumentList(
1662 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1665 void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
1666 if (!Node->isImplicitAccess()) {
1667 PrintExpr(Node->getBase());
1668 OS << (Node->isArrow() ? "->" : ".");
1670 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1671 Qualifier->print(OS, Policy);
1672 if (Node->hasTemplateKeyword())
1674 OS << Node->getMemberNameInfo();
1675 if (Node->hasExplicitTemplateArgs())
1676 TemplateSpecializationType::PrintTemplateArgumentList(
1677 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
1680 static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1682 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1683 case UTT_HasNothrowMoveAssign: return "__has_nothrow_move_assign";
1684 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1685 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1686 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1687 case UTT_HasTrivialMoveAssign: return "__has_trivial_move_assign";
1688 case UTT_HasTrivialMoveConstructor: return "__has_trivial_move_constructor";
1689 case UTT_HasTrivialDefaultConstructor: return "__has_trivial_constructor";
1690 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1691 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1692 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1693 case UTT_IsAbstract: return "__is_abstract";
1694 case UTT_IsArithmetic: return "__is_arithmetic";
1695 case UTT_IsArray: return "__is_array";
1696 case UTT_IsClass: return "__is_class";
1697 case UTT_IsCompleteType: return "__is_complete_type";
1698 case UTT_IsCompound: return "__is_compound";
1699 case UTT_IsConst: return "__is_const";
1700 case UTT_IsEmpty: return "__is_empty";
1701 case UTT_IsEnum: return "__is_enum";
1702 case UTT_IsFinal: return "__is_final";
1703 case UTT_IsFloatingPoint: return "__is_floating_point";
1704 case UTT_IsFunction: return "__is_function";
1705 case UTT_IsFundamental: return "__is_fundamental";
1706 case UTT_IsIntegral: return "__is_integral";
1707 case UTT_IsInterfaceClass: return "__is_interface_class";
1708 case UTT_IsLiteral: return "__is_literal";
1709 case UTT_IsLvalueReference: return "__is_lvalue_reference";
1710 case UTT_IsMemberFunctionPointer: return "__is_member_function_pointer";
1711 case UTT_IsMemberObjectPointer: return "__is_member_object_pointer";
1712 case UTT_IsMemberPointer: return "__is_member_pointer";
1713 case UTT_IsObject: return "__is_object";
1714 case UTT_IsPOD: return "__is_pod";
1715 case UTT_IsPointer: return "__is_pointer";
1716 case UTT_IsPolymorphic: return "__is_polymorphic";
1717 case UTT_IsReference: return "__is_reference";
1718 case UTT_IsRvalueReference: return "__is_rvalue_reference";
1719 case UTT_IsScalar: return "__is_scalar";
1720 case UTT_IsSealed: return "__is_sealed";
1721 case UTT_IsSigned: return "__is_signed";
1722 case UTT_IsStandardLayout: return "__is_standard_layout";
1723 case UTT_IsTrivial: return "__is_trivial";
1724 case UTT_IsTriviallyCopyable: return "__is_trivially_copyable";
1725 case UTT_IsUnion: return "__is_union";
1726 case UTT_IsUnsigned: return "__is_unsigned";
1727 case UTT_IsVoid: return "__is_void";
1728 case UTT_IsVolatile: return "__is_volatile";
1730 llvm_unreachable("Type trait not covered by switch statement");
1733 static const char *getTypeTraitName(BinaryTypeTrait BTT) {
1735 case BTT_IsBaseOf: return "__is_base_of";
1736 case BTT_IsConvertible: return "__is_convertible";
1737 case BTT_IsSame: return "__is_same";
1738 case BTT_TypeCompatible: return "__builtin_types_compatible_p";
1739 case BTT_IsConvertibleTo: return "__is_convertible_to";
1740 case BTT_IsTriviallyAssignable: return "__is_trivially_assignable";
1742 llvm_unreachable("Binary type trait not covered by switch");
1745 static const char *getTypeTraitName(TypeTrait TT) {
1747 case clang::TT_IsTriviallyConstructible:return "__is_trivially_constructible";
1749 llvm_unreachable("Type trait not covered by switch");
1752 static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1754 case ATT_ArrayRank: return "__array_rank";
1755 case ATT_ArrayExtent: return "__array_extent";
1757 llvm_unreachable("Array type trait not covered by switch");
1760 static const char *getExpressionTraitName(ExpressionTrait ET) {
1762 case ET_IsLValueExpr: return "__is_lvalue_expr";
1763 case ET_IsRValueExpr: return "__is_rvalue_expr";
1765 llvm_unreachable("Expression type trait not covered by switch");
1768 void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1769 OS << getTypeTraitName(E->getTrait()) << '(';
1770 E->getQueriedType().print(OS, Policy);
1774 void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
1775 OS << getTypeTraitName(E->getTrait()) << '(';
1776 E->getLhsType().print(OS, Policy);
1778 E->getRhsType().print(OS, Policy);
1782 void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1783 OS << getTypeTraitName(E->getTrait()) << "(";
1784 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
1787 E->getArg(I)->getType().print(OS, Policy);
1792 void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
1793 OS << getTypeTraitName(E->getTrait()) << '(';
1794 E->getQueriedType().print(OS, Policy);
1798 void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
1799 OS << getExpressionTraitName(E->getTrait()) << '(';
1800 PrintExpr(E->getQueriedExpression());
1804 void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1806 PrintExpr(E->getOperand());
1810 void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
1811 PrintExpr(E->getPattern());
1815 void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
1816 OS << "sizeof...(" << *E->getPack() << ")";
1819 void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1820 SubstNonTypeTemplateParmPackExpr *Node) {
1821 OS << *Node->getParameterPack();
1824 void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
1825 SubstNonTypeTemplateParmExpr *Node) {
1826 Visit(Node->getReplacement());
1829 void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
1830 OS << *E->getParameterPack();
1833 void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
1834 PrintExpr(Node->GetTemporaryExpr());
1839 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1841 VisitStringLiteral(Node->getString());
1844 void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
1846 Visit(E->getSubExpr());
1849 void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
1851 StmtRange ch = E->children();
1852 if (ch.first != ch.second) {
1856 if (ch.first == ch.second) break;
1863 void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
1865 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
1869 ObjCDictionaryElement Element = E->getKeyValueElement(I);
1872 Visit(Element.Value);
1873 if (Element.isPackExpansion())
1879 void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
1881 Node->getEncodedType().print(OS, Policy);
1885 void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
1886 OS << "@selector(" << Node->getSelector().getAsString() << ')';
1889 void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
1890 OS << "@protocol(" << *Node->getProtocol() << ')';
1893 void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1895 switch (Mess->getReceiverKind()) {
1896 case ObjCMessageExpr::Instance:
1897 PrintExpr(Mess->getInstanceReceiver());
1900 case ObjCMessageExpr::Class:
1901 Mess->getClassReceiver().print(OS, Policy);
1904 case ObjCMessageExpr::SuperInstance:
1905 case ObjCMessageExpr::SuperClass:
1911 Selector selector = Mess->getSelector();
1912 if (selector.isUnarySelector()) {
1913 OS << selector.getNameForSlot(0);
1915 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
1916 if (i < selector.getNumArgs()) {
1917 if (i > 0) OS << ' ';
1918 if (selector.getIdentifierInfoForSlot(i))
1919 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
1923 else OS << ", "; // Handle variadic methods.
1925 PrintExpr(Mess->getArg(i));
1931 void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
1932 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
1936 StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
1937 PrintExpr(E->getSubExpr());
1941 StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
1942 OS << '(' << E->getBridgeKindName();
1943 E->getType().print(OS, Policy);
1945 PrintExpr(E->getSubExpr());
1948 void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
1949 BlockDecl *BD = Node->getBlockDecl();
1952 const FunctionType *AFT = Node->getFunctionType();
1954 if (isa<FunctionNoProtoType>(AFT)) {
1956 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
1958 for (BlockDecl::param_iterator AI = BD->param_begin(),
1959 E = BD->param_end(); AI != E; ++AI) {
1960 if (AI != BD->param_begin()) OS << ", ";
1961 std::string ParamStr = (*AI)->getNameAsString();
1962 (*AI)->getType().print(OS, Policy, ParamStr);
1965 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
1966 if (FT->isVariadic()) {
1967 if (!BD->param_empty()) OS << ", ";
1975 void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
1976 PrintExpr(Node->getSourceExpr());
1979 void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
1980 OS << "__builtin_astype(";
1981 PrintExpr(Node->getSrcExpr());
1983 Node->getType().print(OS, Policy);
1987 //===----------------------------------------------------------------------===//
1988 // Stmt method implementations
1989 //===----------------------------------------------------------------------===//
1991 void Stmt::dumpPretty(const ASTContext &Context) const {
1992 printPretty(llvm::errs(), 0, PrintingPolicy(Context.getLangOpts()));
1995 void Stmt::printPretty(raw_ostream &OS,
1996 PrinterHelper *Helper,
1997 const PrintingPolicy &Policy,
1998 unsigned Indentation) const {
2004 StmtPrinter P(OS, Helper, Policy, Indentation);
2005 P.Visit(const_cast<Stmt*>(this));
2008 //===----------------------------------------------------------------------===//
2010 //===----------------------------------------------------------------------===//
2012 // Implement virtual destructor.
2013 PrinterHelper::~PrinterHelper() {}