StmtPrinter.cpp revision 249423
1193326Sed//===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===// 2193326Sed// 3193326Sed// The LLVM Compiler Infrastructure 4193326Sed// 5193326Sed// This file is distributed under the University of Illinois Open Source 6193326Sed// License. See LICENSE.TXT for details. 7193326Sed// 8193326Sed//===----------------------------------------------------------------------===// 9193326Sed// 10193326Sed// This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which 11193326Sed// pretty print the AST back out to C code. 12193326Sed// 13193326Sed//===----------------------------------------------------------------------===// 14193326Sed 15239462Sdim#include "clang/AST/ASTContext.h" 16249423Sdim#include "clang/AST/Attr.h" 17193326Sed#include "clang/AST/DeclCXX.h" 18193326Sed#include "clang/AST/DeclObjC.h" 19218893Sdim#include "clang/AST/DeclTemplate.h" 20207619Srdivacky#include "clang/AST/Expr.h" 21218893Sdim#include "clang/AST/ExprCXX.h" 22249423Sdim#include "clang/AST/PrettyPrinter.h" 23249423Sdim#include "clang/AST/StmtVisitor.h" 24249423Sdim#include "clang/Basic/CharInfo.h" 25234353Sdim#include "llvm/ADT/SmallString.h" 26249423Sdim#include "llvm/Support/Format.h" 27193326Sedusing namespace clang; 28193326Sed 29193326Sed//===----------------------------------------------------------------------===// 30193326Sed// StmtPrinter Visitor 31193326Sed//===----------------------------------------------------------------------===// 32193326Sed 33193326Sednamespace { 34199990Srdivacky class StmtPrinter : public StmtVisitor<StmtPrinter> { 35226633Sdim raw_ostream &OS; 36193326Sed unsigned IndentLevel; 37193326Sed clang::PrinterHelper* Helper; 38193326Sed PrintingPolicy Policy; 39193326Sed 40193326Sed public: 41239462Sdim StmtPrinter(raw_ostream &os, PrinterHelper* helper, 42195341Sed const PrintingPolicy &Policy, 43193326Sed unsigned Indentation = 0) 44239462Sdim : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy) {} 45198092Srdivacky 46193326Sed void PrintStmt(Stmt *S) { 47193326Sed PrintStmt(S, Policy.Indentation); 48193326Sed } 49193326Sed 50193326Sed void PrintStmt(Stmt *S, int SubIndent) { 51193326Sed IndentLevel += SubIndent; 52193326Sed if (S && isa<Expr>(S)) { 53193326Sed // If this is an expr used in a stmt context, indent and newline it. 54193326Sed Indent(); 55193326Sed Visit(S); 56193326Sed OS << ";\n"; 57193326Sed } else if (S) { 58193326Sed Visit(S); 59193326Sed } else { 60193326Sed Indent() << "<<<NULL STATEMENT>>>\n"; 61193326Sed } 62193326Sed IndentLevel -= SubIndent; 63193326Sed } 64193326Sed 65193326Sed void PrintRawCompoundStmt(CompoundStmt *S); 66193326Sed void PrintRawDecl(Decl *D); 67243830Sdim void PrintRawDeclStmt(const DeclStmt *S); 68193326Sed void PrintRawIfStmt(IfStmt *If); 69193326Sed void PrintRawCXXCatchStmt(CXXCatchStmt *Catch); 70218893Sdim void PrintCallArgs(CallExpr *E); 71221345Sdim void PrintRawSEHExceptHandler(SEHExceptStmt *S); 72221345Sdim void PrintRawSEHFinallyStmt(SEHFinallyStmt *S); 73198092Srdivacky 74193326Sed void PrintExpr(Expr *E) { 75193326Sed if (E) 76193326Sed Visit(E); 77193326Sed else 78193326Sed OS << "<null expr>"; 79193326Sed } 80198092Srdivacky 81226633Sdim raw_ostream &Indent(int Delta = 0) { 82193326Sed for (int i = 0, e = IndentLevel+Delta; i < e; ++i) 83193326Sed OS << " "; 84193326Sed return OS; 85193326Sed } 86198092Srdivacky 87198092Srdivacky void Visit(Stmt* S) { 88193326Sed if (Helper && Helper->handledStmt(S,OS)) 89193326Sed return; 90193326Sed else StmtVisitor<StmtPrinter>::Visit(S); 91193326Sed } 92212904Sdim 93218893Sdim void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED { 94212904Sdim Indent() << "<<unknown stmt type>>\n"; 95212904Sdim } 96218893Sdim void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED { 97212904Sdim OS << "<<unknown expr type>>"; 98212904Sdim } 99212904Sdim void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node); 100198092Srdivacky 101212904Sdim#define ABSTRACT_STMT(CLASS) 102193326Sed#define STMT(CLASS, PARENT) \ 103193326Sed void Visit##CLASS(CLASS *Node); 104208600Srdivacky#include "clang/AST/StmtNodes.inc" 105193326Sed }; 106193326Sed} 107193326Sed 108193326Sed//===----------------------------------------------------------------------===// 109193326Sed// Stmt printing methods. 110193326Sed//===----------------------------------------------------------------------===// 111193326Sed 112193326Sed/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and 113193326Sed/// with no newline after the }. 114193326Sedvoid StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) { 115193326Sed OS << "{\n"; 116193326Sed for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end(); 117193326Sed I != E; ++I) 118193326Sed PrintStmt(*I); 119198092Srdivacky 120193326Sed Indent() << "}"; 121193326Sed} 122193326Sed 123193326Sedvoid StmtPrinter::PrintRawDecl(Decl *D) { 124195341Sed D->print(OS, Policy, IndentLevel); 125193326Sed} 126193326Sed 127243830Sdimvoid StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) { 128243830Sdim DeclStmt::const_decl_iterator Begin = S->decl_begin(), End = S->decl_end(); 129226633Sdim SmallVector<Decl*, 2> Decls; 130198092Srdivacky for ( ; Begin != End; ++Begin) 131193326Sed Decls.push_back(*Begin); 132193326Sed 133195341Sed Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel); 134193326Sed} 135193326Sed 136193326Sedvoid StmtPrinter::VisitNullStmt(NullStmt *Node) { 137193326Sed Indent() << ";\n"; 138193326Sed} 139193326Sed 140193326Sedvoid StmtPrinter::VisitDeclStmt(DeclStmt *Node) { 141193326Sed Indent(); 142193326Sed PrintRawDeclStmt(Node); 143193326Sed OS << ";\n"; 144193326Sed} 145193326Sed 146193326Sedvoid StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) { 147193326Sed Indent(); 148193326Sed PrintRawCompoundStmt(Node); 149193326Sed OS << "\n"; 150193326Sed} 151193326Sed 152193326Sedvoid StmtPrinter::VisitCaseStmt(CaseStmt *Node) { 153193326Sed Indent(-1) << "case "; 154193326Sed PrintExpr(Node->getLHS()); 155193326Sed if (Node->getRHS()) { 156193326Sed OS << " ... "; 157193326Sed PrintExpr(Node->getRHS()); 158193326Sed } 159193326Sed OS << ":\n"; 160198092Srdivacky 161193326Sed PrintStmt(Node->getSubStmt(), 0); 162193326Sed} 163193326Sed 164193326Sedvoid StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) { 165193326Sed Indent(-1) << "default:\n"; 166193326Sed PrintStmt(Node->getSubStmt(), 0); 167193326Sed} 168193326Sed 169193326Sedvoid StmtPrinter::VisitLabelStmt(LabelStmt *Node) { 170193326Sed Indent(-1) << Node->getName() << ":\n"; 171193326Sed PrintStmt(Node->getSubStmt(), 0); 172193326Sed} 173193326Sed 174234982Sdimvoid StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) { 175234982Sdim OS << "[["; 176234982Sdim bool first = true; 177239462Sdim for (ArrayRef<const Attr*>::iterator it = Node->getAttrs().begin(), 178239462Sdim end = Node->getAttrs().end(); 179239462Sdim it != end; ++it) { 180234982Sdim if (!first) { 181234982Sdim OS << ", "; 182234982Sdim first = false; 183234982Sdim } 184234982Sdim // TODO: check this 185239462Sdim (*it)->printPretty(OS, Policy); 186234982Sdim } 187234982Sdim OS << "]] "; 188234982Sdim PrintStmt(Node->getSubStmt(), 0); 189234982Sdim} 190234982Sdim 191193326Sedvoid StmtPrinter::PrintRawIfStmt(IfStmt *If) { 192193326Sed OS << "if ("; 193243830Sdim if (const DeclStmt *DS = If->getConditionVariableDeclStmt()) 194243830Sdim PrintRawDeclStmt(DS); 195243830Sdim else 196243830Sdim PrintExpr(If->getCond()); 197193326Sed OS << ')'; 198198092Srdivacky 199193326Sed if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) { 200193326Sed OS << ' '; 201193326Sed PrintRawCompoundStmt(CS); 202193326Sed OS << (If->getElse() ? ' ' : '\n'); 203193326Sed } else { 204193326Sed OS << '\n'; 205193326Sed PrintStmt(If->getThen()); 206193326Sed if (If->getElse()) Indent(); 207193326Sed } 208198092Srdivacky 209193326Sed if (Stmt *Else = If->getElse()) { 210193326Sed OS << "else"; 211198092Srdivacky 212193326Sed if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) { 213193326Sed OS << ' '; 214193326Sed PrintRawCompoundStmt(CS); 215193326Sed OS << '\n'; 216193326Sed } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) { 217193326Sed OS << ' '; 218193326Sed PrintRawIfStmt(ElseIf); 219193326Sed } else { 220193326Sed OS << '\n'; 221193326Sed PrintStmt(If->getElse()); 222193326Sed } 223193326Sed } 224193326Sed} 225193326Sed 226193326Sedvoid StmtPrinter::VisitIfStmt(IfStmt *If) { 227193326Sed Indent(); 228193326Sed PrintRawIfStmt(If); 229193326Sed} 230193326Sed 231193326Sedvoid StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) { 232193326Sed Indent() << "switch ("; 233243830Sdim if (const DeclStmt *DS = Node->getConditionVariableDeclStmt()) 234243830Sdim PrintRawDeclStmt(DS); 235243830Sdim else 236243830Sdim PrintExpr(Node->getCond()); 237193326Sed OS << ")"; 238198092Srdivacky 239193326Sed // Pretty print compoundstmt bodies (very common). 240193326Sed if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 241193326Sed OS << " "; 242193326Sed PrintRawCompoundStmt(CS); 243193326Sed OS << "\n"; 244193326Sed } else { 245193326Sed OS << "\n"; 246193326Sed PrintStmt(Node->getBody()); 247193326Sed } 248193326Sed} 249193326Sed 250193326Sedvoid StmtPrinter::VisitWhileStmt(WhileStmt *Node) { 251193326Sed Indent() << "while ("; 252243830Sdim if (const DeclStmt *DS = Node->getConditionVariableDeclStmt()) 253243830Sdim PrintRawDeclStmt(DS); 254243830Sdim else 255243830Sdim PrintExpr(Node->getCond()); 256193326Sed OS << ")\n"; 257193326Sed PrintStmt(Node->getBody()); 258193326Sed} 259193326Sed 260193326Sedvoid StmtPrinter::VisitDoStmt(DoStmt *Node) { 261193326Sed Indent() << "do "; 262193326Sed if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 263193326Sed PrintRawCompoundStmt(CS); 264193326Sed OS << " "; 265193326Sed } else { 266193326Sed OS << "\n"; 267193326Sed PrintStmt(Node->getBody()); 268193326Sed Indent(); 269193326Sed } 270198092Srdivacky 271193326Sed OS << "while ("; 272193326Sed PrintExpr(Node->getCond()); 273193326Sed OS << ");\n"; 274193326Sed} 275193326Sed 276193326Sedvoid StmtPrinter::VisitForStmt(ForStmt *Node) { 277193326Sed Indent() << "for ("; 278193326Sed if (Node->getInit()) { 279193326Sed if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit())) 280193326Sed PrintRawDeclStmt(DS); 281193326Sed else 282193326Sed PrintExpr(cast<Expr>(Node->getInit())); 283193326Sed } 284193326Sed OS << ";"; 285193326Sed if (Node->getCond()) { 286193326Sed OS << " "; 287193326Sed PrintExpr(Node->getCond()); 288193326Sed } 289193326Sed OS << ";"; 290193326Sed if (Node->getInc()) { 291193326Sed OS << " "; 292193326Sed PrintExpr(Node->getInc()); 293193326Sed } 294193326Sed OS << ") "; 295198092Srdivacky 296193326Sed if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 297193326Sed PrintRawCompoundStmt(CS); 298193326Sed OS << "\n"; 299193326Sed } else { 300193326Sed OS << "\n"; 301193326Sed PrintStmt(Node->getBody()); 302193326Sed } 303193326Sed} 304193326Sed 305193326Sedvoid StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) { 306193326Sed Indent() << "for ("; 307193326Sed if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement())) 308193326Sed PrintRawDeclStmt(DS); 309193326Sed else 310193326Sed PrintExpr(cast<Expr>(Node->getElement())); 311193326Sed OS << " in "; 312193326Sed PrintExpr(Node->getCollection()); 313193326Sed OS << ") "; 314198092Srdivacky 315193326Sed if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { 316193326Sed PrintRawCompoundStmt(CS); 317193326Sed OS << "\n"; 318193326Sed } else { 319193326Sed OS << "\n"; 320193326Sed PrintStmt(Node->getBody()); 321193326Sed } 322193326Sed} 323193326Sed 324221345Sdimvoid StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) { 325221345Sdim Indent() << "for ("; 326221345Sdim PrintingPolicy SubPolicy(Policy); 327221345Sdim SubPolicy.SuppressInitializers = true; 328221345Sdim Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel); 329221345Sdim OS << " : "; 330221345Sdim PrintExpr(Node->getRangeInit()); 331221345Sdim OS << ") {\n"; 332221345Sdim PrintStmt(Node->getBody()); 333221345Sdim Indent() << "}\n"; 334221345Sdim} 335221345Sdim 336234353Sdimvoid StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) { 337234353Sdim Indent(); 338234353Sdim if (Node->isIfExists()) 339234353Sdim OS << "__if_exists ("; 340234353Sdim else 341234353Sdim OS << "__if_not_exists ("; 342234353Sdim 343234353Sdim if (NestedNameSpecifier *Qualifier 344234353Sdim = Node->getQualifierLoc().getNestedNameSpecifier()) 345234353Sdim Qualifier->print(OS, Policy); 346234353Sdim 347234353Sdim OS << Node->getNameInfo() << ") "; 348234353Sdim 349234353Sdim PrintRawCompoundStmt(Node->getSubStmt()); 350234353Sdim} 351234353Sdim 352193326Sedvoid StmtPrinter::VisitGotoStmt(GotoStmt *Node) { 353193326Sed Indent() << "goto " << Node->getLabel()->getName() << ";\n"; 354193326Sed} 355193326Sed 356193326Sedvoid StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) { 357193326Sed Indent() << "goto *"; 358193326Sed PrintExpr(Node->getTarget()); 359193326Sed OS << ";\n"; 360193326Sed} 361193326Sed 362193326Sedvoid StmtPrinter::VisitContinueStmt(ContinueStmt *Node) { 363193326Sed Indent() << "continue;\n"; 364193326Sed} 365193326Sed 366193326Sedvoid StmtPrinter::VisitBreakStmt(BreakStmt *Node) { 367193326Sed Indent() << "break;\n"; 368193326Sed} 369193326Sed 370193326Sed 371193326Sedvoid StmtPrinter::VisitReturnStmt(ReturnStmt *Node) { 372193326Sed Indent() << "return"; 373193326Sed if (Node->getRetValue()) { 374193326Sed OS << " "; 375193326Sed PrintExpr(Node->getRetValue()); 376193326Sed } 377193326Sed OS << ";\n"; 378193326Sed} 379193326Sed 380193326Sed 381243830Sdimvoid StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) { 382193326Sed Indent() << "asm "; 383198092Srdivacky 384193326Sed if (Node->isVolatile()) 385193326Sed OS << "volatile "; 386198092Srdivacky 387193326Sed OS << "("; 388193326Sed VisitStringLiteral(Node->getAsmString()); 389198092Srdivacky 390193326Sed // Outputs 391193326Sed if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 || 392193326Sed Node->getNumClobbers() != 0) 393193326Sed OS << " : "; 394198092Srdivacky 395193326Sed for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) { 396193326Sed if (i != 0) 397193326Sed OS << ", "; 398198092Srdivacky 399193326Sed if (!Node->getOutputName(i).empty()) { 400193326Sed OS << '['; 401193326Sed OS << Node->getOutputName(i); 402193326Sed OS << "] "; 403193326Sed } 404198092Srdivacky 405193326Sed VisitStringLiteral(Node->getOutputConstraintLiteral(i)); 406193326Sed OS << " "; 407193326Sed Visit(Node->getOutputExpr(i)); 408193326Sed } 409198092Srdivacky 410193326Sed // Inputs 411193326Sed if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0) 412193326Sed OS << " : "; 413198092Srdivacky 414193326Sed for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) { 415193326Sed if (i != 0) 416193326Sed OS << ", "; 417198092Srdivacky 418193326Sed if (!Node->getInputName(i).empty()) { 419193326Sed OS << '['; 420193326Sed OS << Node->getInputName(i); 421193326Sed OS << "] "; 422193326Sed } 423198092Srdivacky 424193326Sed VisitStringLiteral(Node->getInputConstraintLiteral(i)); 425193326Sed OS << " "; 426193326Sed Visit(Node->getInputExpr(i)); 427193326Sed } 428198092Srdivacky 429193326Sed // Clobbers 430193326Sed if (Node->getNumClobbers() != 0) 431193326Sed OS << " : "; 432198092Srdivacky 433193326Sed for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) { 434193326Sed if (i != 0) 435193326Sed OS << ", "; 436198092Srdivacky 437243830Sdim VisitStringLiteral(Node->getClobberStringLiteral(i)); 438193326Sed } 439198092Srdivacky 440193326Sed OS << ");\n"; 441193326Sed} 442193326Sed 443239462Sdimvoid StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) { 444239462Sdim // FIXME: Implement MS style inline asm statement printer. 445239462Sdim Indent() << "__asm "; 446239462Sdim if (Node->hasBraces()) 447239462Sdim OS << "{\n"; 448239462Sdim OS << *(Node->getAsmString()) << "\n"; 449239462Sdim if (Node->hasBraces()) 450239462Sdim Indent() << "}\n"; 451239462Sdim} 452239462Sdim 453193326Sedvoid StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) { 454193326Sed Indent() << "@try"; 455193326Sed if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) { 456193326Sed PrintRawCompoundStmt(TS); 457193326Sed OS << "\n"; 458193326Sed } 459198092Srdivacky 460207619Srdivacky for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) { 461207619Srdivacky ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I); 462193326Sed Indent() << "@catch("; 463193326Sed if (catchStmt->getCatchParamDecl()) { 464193326Sed if (Decl *DS = catchStmt->getCatchParamDecl()) 465193326Sed PrintRawDecl(DS); 466193326Sed } 467193326Sed OS << ")"; 468198092Srdivacky if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) { 469198092Srdivacky PrintRawCompoundStmt(CS); 470198092Srdivacky OS << "\n"; 471198092Srdivacky } 472193326Sed } 473198092Srdivacky 474198092Srdivacky if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>( 475198092Srdivacky Node->getFinallyStmt())) { 476193326Sed Indent() << "@finally"; 477193326Sed PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody())); 478193326Sed OS << "\n"; 479198092Srdivacky } 480193326Sed} 481193326Sed 482193326Sedvoid StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) { 483193326Sed} 484193326Sed 485193326Sedvoid StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) { 486193326Sed Indent() << "@catch (...) { /* todo */ } \n"; 487193326Sed} 488193326Sed 489193326Sedvoid StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) { 490193326Sed Indent() << "@throw"; 491193326Sed if (Node->getThrowExpr()) { 492193326Sed OS << " "; 493193326Sed PrintExpr(Node->getThrowExpr()); 494193326Sed } 495193326Sed OS << ";\n"; 496193326Sed} 497193326Sed 498193326Sedvoid StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) { 499193326Sed Indent() << "@synchronized ("; 500193326Sed PrintExpr(Node->getSynchExpr()); 501193326Sed OS << ")"; 502193326Sed PrintRawCompoundStmt(Node->getSynchBody()); 503193326Sed OS << "\n"; 504193326Sed} 505193326Sed 506224145Sdimvoid StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) { 507224145Sdim Indent() << "@autoreleasepool"; 508224145Sdim PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt())); 509224145Sdim OS << "\n"; 510224145Sdim} 511224145Sdim 512193326Sedvoid StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) { 513193326Sed OS << "catch ("; 514193326Sed if (Decl *ExDecl = Node->getExceptionDecl()) 515193326Sed PrintRawDecl(ExDecl); 516193326Sed else 517193326Sed OS << "..."; 518193326Sed OS << ") "; 519193326Sed PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock())); 520193326Sed} 521193326Sed 522193326Sedvoid StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) { 523193326Sed Indent(); 524193326Sed PrintRawCXXCatchStmt(Node); 525193326Sed OS << "\n"; 526193326Sed} 527193326Sed 528193326Sedvoid StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) { 529193326Sed Indent() << "try "; 530193326Sed PrintRawCompoundStmt(Node->getTryBlock()); 531198092Srdivacky for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) { 532193326Sed OS << " "; 533193326Sed PrintRawCXXCatchStmt(Node->getHandler(i)); 534193326Sed } 535193326Sed OS << "\n"; 536193326Sed} 537193326Sed 538221345Sdimvoid StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) { 539221345Sdim Indent() << (Node->getIsCXXTry() ? "try " : "__try "); 540221345Sdim PrintRawCompoundStmt(Node->getTryBlock()); 541221345Sdim SEHExceptStmt *E = Node->getExceptHandler(); 542221345Sdim SEHFinallyStmt *F = Node->getFinallyHandler(); 543221345Sdim if(E) 544221345Sdim PrintRawSEHExceptHandler(E); 545221345Sdim else { 546221345Sdim assert(F && "Must have a finally block..."); 547221345Sdim PrintRawSEHFinallyStmt(F); 548221345Sdim } 549221345Sdim OS << "\n"; 550221345Sdim} 551221345Sdim 552221345Sdimvoid StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) { 553221345Sdim OS << "__finally "; 554221345Sdim PrintRawCompoundStmt(Node->getBlock()); 555221345Sdim OS << "\n"; 556221345Sdim} 557221345Sdim 558221345Sdimvoid StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) { 559221345Sdim OS << "__except ("; 560221345Sdim VisitExpr(Node->getFilterExpr()); 561221345Sdim OS << ")\n"; 562221345Sdim PrintRawCompoundStmt(Node->getBlock()); 563221345Sdim OS << "\n"; 564221345Sdim} 565221345Sdim 566221345Sdimvoid StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) { 567221345Sdim Indent(); 568221345Sdim PrintRawSEHExceptHandler(Node); 569221345Sdim OS << "\n"; 570221345Sdim} 571221345Sdim 572221345Sdimvoid StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) { 573221345Sdim Indent(); 574221345Sdim PrintRawSEHFinallyStmt(Node); 575221345Sdim OS << "\n"; 576221345Sdim} 577221345Sdim 578193326Sed//===----------------------------------------------------------------------===// 579193326Sed// Expr printing methods. 580193326Sed//===----------------------------------------------------------------------===// 581193326Sed 582193326Sedvoid StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) { 583198893Srdivacky if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 584198893Srdivacky Qualifier->print(OS, Policy); 585234353Sdim if (Node->hasTemplateKeyword()) 586234353Sdim OS << "template "; 587212904Sdim OS << Node->getNameInfo(); 588212904Sdim if (Node->hasExplicitTemplateArgs()) 589249423Sdim TemplateSpecializationType::PrintTemplateArgumentList( 590249423Sdim OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 591193326Sed} 592193326Sed 593199990Srdivackyvoid StmtPrinter::VisitDependentScopeDeclRefExpr( 594199990Srdivacky DependentScopeDeclRefExpr *Node) { 595218893Sdim if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 596218893Sdim Qualifier->print(OS, Policy); 597234353Sdim if (Node->hasTemplateKeyword()) 598234353Sdim OS << "template "; 599212904Sdim OS << Node->getNameInfo(); 600199990Srdivacky if (Node->hasExplicitTemplateArgs()) 601249423Sdim TemplateSpecializationType::PrintTemplateArgumentList( 602249423Sdim OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 603193326Sed} 604193326Sed 605199990Srdivackyvoid StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) { 606195341Sed if (Node->getQualifier()) 607195341Sed Node->getQualifier()->print(OS, Policy); 608234353Sdim if (Node->hasTemplateKeyword()) 609234353Sdim OS << "template "; 610212904Sdim OS << Node->getNameInfo(); 611199990Srdivacky if (Node->hasExplicitTemplateArgs()) 612249423Sdim TemplateSpecializationType::PrintTemplateArgumentList( 613249423Sdim OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 614195341Sed} 615195341Sed 616193326Sedvoid StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { 617193326Sed if (Node->getBase()) { 618193326Sed PrintExpr(Node->getBase()); 619193326Sed OS << (Node->isArrow() ? "->" : "."); 620193326Sed } 621226633Sdim OS << *Node->getDecl(); 622193326Sed} 623193326Sed 624193326Sedvoid StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) { 625218893Sdim if (Node->isSuperReceiver()) 626218893Sdim OS << "super."; 627218893Sdim else if (Node->getBase()) { 628193326Sed PrintExpr(Node->getBase()); 629193326Sed OS << "."; 630193326Sed } 631193326Sed 632218893Sdim if (Node->isImplicitProperty()) 633218893Sdim OS << Node->getImplicitPropertyGetter()->getSelector().getAsString(); 634218893Sdim else 635218893Sdim OS << Node->getExplicitProperty()->getName(); 636193326Sed} 637193326Sed 638234353Sdimvoid StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) { 639234353Sdim 640234353Sdim PrintExpr(Node->getBaseExpr()); 641234353Sdim OS << "["; 642234353Sdim PrintExpr(Node->getKeyExpr()); 643234353Sdim OS << "]"; 644234353Sdim} 645234353Sdim 646193326Sedvoid StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) { 647193326Sed switch (Node->getIdentType()) { 648193326Sed default: 649226633Sdim llvm_unreachable("unknown case"); 650193326Sed case PredefinedExpr::Func: 651193326Sed OS << "__func__"; 652193326Sed break; 653193326Sed case PredefinedExpr::Function: 654193326Sed OS << "__FUNCTION__"; 655193326Sed break; 656239462Sdim case PredefinedExpr::LFunction: 657239462Sdim OS << "L__FUNCTION__"; 658239462Sdim break; 659193326Sed case PredefinedExpr::PrettyFunction: 660193326Sed OS << "__PRETTY_FUNCTION__"; 661193326Sed break; 662193326Sed } 663193326Sed} 664193326Sed 665193326Sedvoid StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) { 666193326Sed unsigned value = Node->getValue(); 667226633Sdim 668226633Sdim switch (Node->getKind()) { 669226633Sdim case CharacterLiteral::Ascii: break; // no prefix. 670226633Sdim case CharacterLiteral::Wide: OS << 'L'; break; 671226633Sdim case CharacterLiteral::UTF16: OS << 'u'; break; 672226633Sdim case CharacterLiteral::UTF32: OS << 'U'; break; 673226633Sdim } 674226633Sdim 675193326Sed switch (value) { 676193326Sed case '\\': 677193326Sed OS << "'\\\\'"; 678193326Sed break; 679193326Sed case '\'': 680193326Sed OS << "'\\''"; 681193326Sed break; 682193326Sed case '\a': 683193326Sed // TODO: K&R: the meaning of '\\a' is different in traditional C 684193326Sed OS << "'\\a'"; 685193326Sed break; 686193326Sed case '\b': 687193326Sed OS << "'\\b'"; 688193326Sed break; 689193326Sed // Nonstandard escape sequence. 690193326Sed /*case '\e': 691193326Sed OS << "'\\e'"; 692193326Sed break;*/ 693193326Sed case '\f': 694193326Sed OS << "'\\f'"; 695193326Sed break; 696193326Sed case '\n': 697193326Sed OS << "'\\n'"; 698193326Sed break; 699193326Sed case '\r': 700193326Sed OS << "'\\r'"; 701193326Sed break; 702193326Sed case '\t': 703193326Sed OS << "'\\t'"; 704193326Sed break; 705193326Sed case '\v': 706193326Sed OS << "'\\v'"; 707193326Sed break; 708193326Sed default: 709249423Sdim if (value < 256 && isPrintable((unsigned char)value)) 710193326Sed OS << "'" << (char)value << "'"; 711249423Sdim else if (value < 256) 712249423Sdim OS << "'\\x" << llvm::format("%02x", value) << "'"; 713249423Sdim else if (value <= 0xFFFF) 714249423Sdim OS << "'\\u" << llvm::format("%04x", value) << "'"; 715249423Sdim else 716249423Sdim OS << "'\\U" << llvm::format("%08x", value) << "'"; 717193326Sed } 718193326Sed} 719193326Sed 720193326Sedvoid StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) { 721193326Sed bool isSigned = Node->getType()->isSignedIntegerType(); 722193326Sed OS << Node->getValue().toString(10, isSigned); 723198092Srdivacky 724193326Sed // Emit suffixes. Integer literals are always a builtin integer type. 725198092Srdivacky switch (Node->getType()->getAs<BuiltinType>()->getKind()) { 726226633Sdim default: llvm_unreachable("Unexpected type for integer literal!"); 727234353Sdim // FIXME: The Short and UShort cases are to handle cases where a short 728234353Sdim // integeral literal is formed during template instantiation. They should 729234353Sdim // be removed when template instantiation no longer needs integer literals. 730234353Sdim case BuiltinType::Short: 731234353Sdim case BuiltinType::UShort: 732193326Sed case BuiltinType::Int: break; // no suffix. 733193326Sed case BuiltinType::UInt: OS << 'U'; break; 734193326Sed case BuiltinType::Long: OS << 'L'; break; 735193326Sed case BuiltinType::ULong: OS << "UL"; break; 736193326Sed case BuiltinType::LongLong: OS << "LL"; break; 737193326Sed case BuiltinType::ULongLong: OS << "ULL"; break; 738234353Sdim case BuiltinType::Int128: OS << "i128"; break; 739234353Sdim case BuiltinType::UInt128: OS << "Ui128"; break; 740193326Sed } 741193326Sed} 742243830Sdim 743243830Sdimstatic void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node, 744243830Sdim bool PrintSuffix) { 745234353Sdim SmallString<16> Str; 746226633Sdim Node->getValue().toString(Str); 747226633Sdim OS << Str; 748243830Sdim if (Str.find_first_not_of("-0123456789") == StringRef::npos) 749243830Sdim OS << '.'; // Trailing dot in order to separate from ints. 750243830Sdim 751243830Sdim if (!PrintSuffix) 752243830Sdim return; 753243830Sdim 754243830Sdim // Emit suffixes. Float literals are always a builtin float type. 755243830Sdim switch (Node->getType()->getAs<BuiltinType>()->getKind()) { 756243830Sdim default: llvm_unreachable("Unexpected type for float literal!"); 757243830Sdim case BuiltinType::Half: break; // FIXME: suffix? 758243830Sdim case BuiltinType::Double: break; // no suffix. 759243830Sdim case BuiltinType::Float: OS << 'F'; break; 760243830Sdim case BuiltinType::LongDouble: OS << 'L'; break; 761243830Sdim } 762193326Sed} 763193326Sed 764243830Sdimvoid StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) { 765243830Sdim PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true); 766243830Sdim} 767243830Sdim 768193326Sedvoid StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) { 769193326Sed PrintExpr(Node->getSubExpr()); 770193326Sed OS << "i"; 771193326Sed} 772193326Sed 773193326Sedvoid StmtPrinter::VisitStringLiteral(StringLiteral *Str) { 774239462Sdim Str->outputString(OS); 775193326Sed} 776193326Sedvoid StmtPrinter::VisitParenExpr(ParenExpr *Node) { 777193326Sed OS << "("; 778193326Sed PrintExpr(Node->getSubExpr()); 779193326Sed OS << ")"; 780193326Sed} 781193326Sedvoid StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { 782193326Sed if (!Node->isPostfix()) { 783193326Sed OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 784198092Srdivacky 785194613Sed // Print a space if this is an "identifier operator" like __real, or if 786194613Sed // it might be concatenated incorrectly like '+'. 787193326Sed switch (Node->getOpcode()) { 788193326Sed default: break; 789212904Sdim case UO_Real: 790212904Sdim case UO_Imag: 791212904Sdim case UO_Extension: 792193326Sed OS << ' '; 793193326Sed break; 794212904Sdim case UO_Plus: 795212904Sdim case UO_Minus: 796194613Sed if (isa<UnaryOperator>(Node->getSubExpr())) 797194613Sed OS << ' '; 798194613Sed break; 799193326Sed } 800193326Sed } 801193326Sed PrintExpr(Node->getSubExpr()); 802198092Srdivacky 803193326Sed if (Node->isPostfix()) 804193326Sed OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); 805193326Sed} 806193326Sed 807207619Srdivackyvoid StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) { 808207619Srdivacky OS << "__builtin_offsetof("; 809249423Sdim Node->getTypeSourceInfo()->getType().print(OS, Policy); 810249423Sdim OS << ", "; 811207619Srdivacky bool PrintedSomething = false; 812207619Srdivacky for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) { 813207619Srdivacky OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i); 814207619Srdivacky if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) { 815207619Srdivacky // Array node 816207619Srdivacky OS << "["; 817207619Srdivacky PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex())); 818207619Srdivacky OS << "]"; 819207619Srdivacky PrintedSomething = true; 820207619Srdivacky continue; 821207619Srdivacky } 822207619Srdivacky 823207619Srdivacky // Skip implicit base indirections. 824207619Srdivacky if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base) 825207619Srdivacky continue; 826207619Srdivacky 827207619Srdivacky // Field or identifier node. 828207619Srdivacky IdentifierInfo *Id = ON.getFieldName(); 829207619Srdivacky if (!Id) 830207619Srdivacky continue; 831207619Srdivacky 832207619Srdivacky if (PrintedSomething) 833207619Srdivacky OS << "."; 834207619Srdivacky else 835207619Srdivacky PrintedSomething = true; 836207619Srdivacky OS << Id->getName(); 837207619Srdivacky } 838207619Srdivacky OS << ")"; 839207619Srdivacky} 840207619Srdivacky 841221345Sdimvoid StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){ 842221345Sdim switch(Node->getKind()) { 843221345Sdim case UETT_SizeOf: 844221345Sdim OS << "sizeof"; 845221345Sdim break; 846221345Sdim case UETT_AlignOf: 847239462Sdim if (Policy.LangOpts.CPlusPlus) 848239462Sdim OS << "alignof"; 849239462Sdim else if (Policy.LangOpts.C11) 850239462Sdim OS << "_Alignof"; 851239462Sdim else 852239462Sdim OS << "__alignof"; 853221345Sdim break; 854221345Sdim case UETT_VecStep: 855221345Sdim OS << "vec_step"; 856221345Sdim break; 857221345Sdim } 858249423Sdim if (Node->isArgumentType()) { 859249423Sdim OS << '('; 860249423Sdim Node->getArgumentType().print(OS, Policy); 861249423Sdim OS << ')'; 862249423Sdim } else { 863193326Sed OS << " "; 864193326Sed PrintExpr(Node->getArgumentExpr()); 865193326Sed } 866193326Sed} 867221345Sdim 868221345Sdimvoid StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) { 869221345Sdim OS << "_Generic("; 870221345Sdim PrintExpr(Node->getControllingExpr()); 871221345Sdim for (unsigned i = 0; i != Node->getNumAssocs(); ++i) { 872221345Sdim OS << ", "; 873221345Sdim QualType T = Node->getAssocType(i); 874221345Sdim if (T.isNull()) 875221345Sdim OS << "default"; 876221345Sdim else 877249423Sdim T.print(OS, Policy); 878221345Sdim OS << ": "; 879221345Sdim PrintExpr(Node->getAssocExpr(i)); 880221345Sdim } 881221345Sdim OS << ")"; 882221345Sdim} 883221345Sdim 884193326Sedvoid StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) { 885193326Sed PrintExpr(Node->getLHS()); 886193326Sed OS << "["; 887193326Sed PrintExpr(Node->getRHS()); 888193326Sed OS << "]"; 889193326Sed} 890193326Sed 891218893Sdimvoid StmtPrinter::PrintCallArgs(CallExpr *Call) { 892193326Sed for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) { 893193326Sed if (isa<CXXDefaultArgExpr>(Call->getArg(i))) { 894193326Sed // Don't print any defaulted arguments 895193326Sed break; 896193326Sed } 897193326Sed 898193326Sed if (i) OS << ", "; 899193326Sed PrintExpr(Call->getArg(i)); 900193326Sed } 901218893Sdim} 902218893Sdim 903218893Sdimvoid StmtPrinter::VisitCallExpr(CallExpr *Call) { 904218893Sdim PrintExpr(Call->getCallee()); 905218893Sdim OS << "("; 906218893Sdim PrintCallArgs(Call); 907193326Sed OS << ")"; 908193326Sed} 909193326Sedvoid StmtPrinter::VisitMemberExpr(MemberExpr *Node) { 910193326Sed // FIXME: Suppress printing implicit bases (like "this") 911193326Sed PrintExpr(Node->getBase()); 912249423Sdim 913249423Sdim MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase()); 914249423Sdim FieldDecl *ParentDecl = ParentMember 915249423Sdim ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : NULL; 916249423Sdim 917249423Sdim if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion()) 918249423Sdim OS << (Node->isArrow() ? "->" : "."); 919249423Sdim 920202379Srdivacky if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl())) 921202379Srdivacky if (FD->isAnonymousStructOrUnion()) 922202379Srdivacky return; 923249423Sdim 924198092Srdivacky if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 925198092Srdivacky Qualifier->print(OS, Policy); 926234353Sdim if (Node->hasTemplateKeyword()) 927234353Sdim OS << "template "; 928212904Sdim OS << Node->getMemberNameInfo(); 929212904Sdim if (Node->hasExplicitTemplateArgs()) 930249423Sdim TemplateSpecializationType::PrintTemplateArgumentList( 931249423Sdim OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 932193326Sed} 933198092Srdivackyvoid StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) { 934198092Srdivacky PrintExpr(Node->getBase()); 935198092Srdivacky OS << (Node->isArrow() ? "->isa" : ".isa"); 936198092Srdivacky} 937198092Srdivacky 938193326Sedvoid StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) { 939193326Sed PrintExpr(Node->getBase()); 940193326Sed OS << "."; 941193326Sed OS << Node->getAccessor().getName(); 942193326Sed} 943193326Sedvoid StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) { 944249423Sdim OS << '('; 945249423Sdim Node->getTypeAsWritten().print(OS, Policy); 946249423Sdim OS << ')'; 947193326Sed PrintExpr(Node->getSubExpr()); 948193326Sed} 949193326Sedvoid StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) { 950249423Sdim OS << '('; 951249423Sdim Node->getType().print(OS, Policy); 952249423Sdim OS << ')'; 953193326Sed PrintExpr(Node->getInitializer()); 954193326Sed} 955193326Sedvoid StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) { 956193326Sed // No need to print anything, simply forward to the sub expression. 957193326Sed PrintExpr(Node->getSubExpr()); 958193326Sed} 959193326Sedvoid StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) { 960193326Sed PrintExpr(Node->getLHS()); 961193326Sed OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 962193326Sed PrintExpr(Node->getRHS()); 963193326Sed} 964193326Sedvoid StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) { 965193326Sed PrintExpr(Node->getLHS()); 966193326Sed OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; 967193326Sed PrintExpr(Node->getRHS()); 968193326Sed} 969193326Sedvoid StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { 970193326Sed PrintExpr(Node->getCond()); 971218893Sdim OS << " ? "; 972218893Sdim PrintExpr(Node->getLHS()); 973218893Sdim OS << " : "; 974193326Sed PrintExpr(Node->getRHS()); 975193326Sed} 976193326Sed 977193326Sed// GNU extensions. 978193326Sed 979218893Sdimvoid 980218893SdimStmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) { 981218893Sdim PrintExpr(Node->getCommon()); 982218893Sdim OS << " ?: "; 983218893Sdim PrintExpr(Node->getFalseExpr()); 984218893Sdim} 985193326Sedvoid StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) { 986193326Sed OS << "&&" << Node->getLabel()->getName(); 987193326Sed} 988193326Sed 989193326Sedvoid StmtPrinter::VisitStmtExpr(StmtExpr *E) { 990193326Sed OS << "("; 991193326Sed PrintRawCompoundStmt(E->getSubStmt()); 992193326Sed OS << ")"; 993193326Sed} 994193326Sed 995193326Sedvoid StmtPrinter::VisitChooseExpr(ChooseExpr *Node) { 996193326Sed OS << "__builtin_choose_expr("; 997193326Sed PrintExpr(Node->getCond()); 998193326Sed OS << ", "; 999193326Sed PrintExpr(Node->getLHS()); 1000193326Sed OS << ", "; 1001193326Sed PrintExpr(Node->getRHS()); 1002193326Sed OS << ")"; 1003193326Sed} 1004193326Sed 1005193326Sedvoid StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) { 1006193326Sed OS << "__null"; 1007193326Sed} 1008193326Sed 1009193326Sedvoid StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) { 1010193326Sed OS << "__builtin_shufflevector("; 1011193326Sed for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) { 1012193326Sed if (i) OS << ", "; 1013193326Sed PrintExpr(Node->getExpr(i)); 1014193326Sed } 1015193326Sed OS << ")"; 1016193326Sed} 1017193326Sed 1018193326Sedvoid StmtPrinter::VisitInitListExpr(InitListExpr* Node) { 1019193326Sed if (Node->getSyntacticForm()) { 1020193326Sed Visit(Node->getSyntacticForm()); 1021193326Sed return; 1022193326Sed } 1023193326Sed 1024193326Sed OS << "{ "; 1025193326Sed for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) { 1026193326Sed if (i) OS << ", "; 1027193326Sed if (Node->getInit(i)) 1028193326Sed PrintExpr(Node->getInit(i)); 1029193326Sed else 1030193326Sed OS << "0"; 1031193326Sed } 1032193326Sed OS << " }"; 1033193326Sed} 1034193326Sed 1035198092Srdivackyvoid StmtPrinter::VisitParenListExpr(ParenListExpr* Node) { 1036198092Srdivacky OS << "( "; 1037198092Srdivacky for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) { 1038198092Srdivacky if (i) OS << ", "; 1039198092Srdivacky PrintExpr(Node->getExpr(i)); 1040198092Srdivacky } 1041198092Srdivacky OS << " )"; 1042198092Srdivacky} 1043198092Srdivacky 1044193326Sedvoid StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) { 1045193326Sed for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(), 1046193326Sed DEnd = Node->designators_end(); 1047193326Sed D != DEnd; ++D) { 1048193326Sed if (D->isFieldDesignator()) { 1049193326Sed if (D->getDotLoc().isInvalid()) 1050193326Sed OS << D->getFieldName()->getName() << ":"; 1051193326Sed else 1052193326Sed OS << "." << D->getFieldName()->getName(); 1053193326Sed } else { 1054193326Sed OS << "["; 1055193326Sed if (D->isArrayDesignator()) { 1056193326Sed PrintExpr(Node->getArrayIndex(*D)); 1057193326Sed } else { 1058193326Sed PrintExpr(Node->getArrayRangeStart(*D)); 1059193326Sed OS << " ... "; 1060198092Srdivacky PrintExpr(Node->getArrayRangeEnd(*D)); 1061193326Sed } 1062193326Sed OS << "]"; 1063193326Sed } 1064193326Sed } 1065193326Sed 1066193326Sed OS << " = "; 1067193326Sed PrintExpr(Node->getInit()); 1068193326Sed} 1069193326Sed 1070193326Sedvoid StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) { 1071249423Sdim if (Policy.LangOpts.CPlusPlus) { 1072249423Sdim OS << "/*implicit*/"; 1073249423Sdim Node->getType().print(OS, Policy); 1074249423Sdim OS << "()"; 1075249423Sdim } else { 1076249423Sdim OS << "/*implicit*/("; 1077249423Sdim Node->getType().print(OS, Policy); 1078249423Sdim OS << ')'; 1079193326Sed if (Node->getType()->isRecordType()) 1080193326Sed OS << "{}"; 1081193326Sed else 1082193326Sed OS << 0; 1083193326Sed } 1084193326Sed} 1085193326Sed 1086193326Sedvoid StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) { 1087193326Sed OS << "__builtin_va_arg("; 1088193326Sed PrintExpr(Node->getSubExpr()); 1089193326Sed OS << ", "; 1090249423Sdim Node->getType().print(OS, Policy); 1091193326Sed OS << ")"; 1092193326Sed} 1093193326Sed 1094234353Sdimvoid StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) { 1095234353Sdim PrintExpr(Node->getSyntacticForm()); 1096234353Sdim} 1097234353Sdim 1098226633Sdimvoid StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) { 1099226633Sdim const char *Name = 0; 1100226633Sdim switch (Node->getOp()) { 1101234353Sdim#define BUILTIN(ID, TYPE, ATTRS) 1102234353Sdim#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ 1103234353Sdim case AtomicExpr::AO ## ID: \ 1104234353Sdim Name = #ID "("; \ 1105234353Sdim break; 1106234353Sdim#include "clang/Basic/Builtins.def" 1107226633Sdim } 1108226633Sdim OS << Name; 1109234353Sdim 1110234353Sdim // AtomicExpr stores its subexpressions in a permuted order. 1111226633Sdim PrintExpr(Node->getPtr()); 1112226633Sdim OS << ", "; 1113234353Sdim if (Node->getOp() != AtomicExpr::AO__c11_atomic_load && 1114234353Sdim Node->getOp() != AtomicExpr::AO__atomic_load_n) { 1115226633Sdim PrintExpr(Node->getVal1()); 1116226633Sdim OS << ", "; 1117226633Sdim } 1118234353Sdim if (Node->getOp() == AtomicExpr::AO__atomic_exchange || 1119234353Sdim Node->isCmpXChg()) { 1120226633Sdim PrintExpr(Node->getVal2()); 1121226633Sdim OS << ", "; 1122226633Sdim } 1123234353Sdim if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange || 1124234353Sdim Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) { 1125234353Sdim PrintExpr(Node->getWeak()); 1126234353Sdim OS << ", "; 1127234353Sdim } 1128234353Sdim if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) 1129234353Sdim PrintExpr(Node->getOrder()); 1130226633Sdim if (Node->isCmpXChg()) { 1131226633Sdim OS << ", "; 1132226633Sdim PrintExpr(Node->getOrderFail()); 1133226633Sdim } 1134226633Sdim OS << ")"; 1135226633Sdim} 1136226633Sdim 1137193326Sed// C++ 1138193326Sedvoid StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) { 1139193326Sed const char *OpStrings[NUM_OVERLOADED_OPERATORS] = { 1140193326Sed "", 1141193326Sed#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 1142193326Sed Spelling, 1143193326Sed#include "clang/Basic/OperatorKinds.def" 1144193326Sed }; 1145193326Sed 1146193326Sed OverloadedOperatorKind Kind = Node->getOperator(); 1147193326Sed if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { 1148193326Sed if (Node->getNumArgs() == 1) { 1149193326Sed OS << OpStrings[Kind] << ' '; 1150193326Sed PrintExpr(Node->getArg(0)); 1151193326Sed } else { 1152193326Sed PrintExpr(Node->getArg(0)); 1153193326Sed OS << ' ' << OpStrings[Kind]; 1154193326Sed } 1155243830Sdim } else if (Kind == OO_Arrow) { 1156243830Sdim PrintExpr(Node->getArg(0)); 1157193326Sed } else if (Kind == OO_Call) { 1158193326Sed PrintExpr(Node->getArg(0)); 1159193326Sed OS << '('; 1160193326Sed for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) { 1161193326Sed if (ArgIdx > 1) 1162193326Sed OS << ", "; 1163193326Sed if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx))) 1164193326Sed PrintExpr(Node->getArg(ArgIdx)); 1165193326Sed } 1166193326Sed OS << ')'; 1167193326Sed } else if (Kind == OO_Subscript) { 1168193326Sed PrintExpr(Node->getArg(0)); 1169193326Sed OS << '['; 1170193326Sed PrintExpr(Node->getArg(1)); 1171193326Sed OS << ']'; 1172193326Sed } else if (Node->getNumArgs() == 1) { 1173193326Sed OS << OpStrings[Kind] << ' '; 1174193326Sed PrintExpr(Node->getArg(0)); 1175193326Sed } else if (Node->getNumArgs() == 2) { 1176193326Sed PrintExpr(Node->getArg(0)); 1177193326Sed OS << ' ' << OpStrings[Kind] << ' '; 1178193326Sed PrintExpr(Node->getArg(1)); 1179193326Sed } else { 1180226633Sdim llvm_unreachable("unknown overloaded operator"); 1181193326Sed } 1182193326Sed} 1183193326Sed 1184193326Sedvoid StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) { 1185193326Sed VisitCallExpr(cast<CallExpr>(Node)); 1186193326Sed} 1187193326Sed 1188218893Sdimvoid StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) { 1189218893Sdim PrintExpr(Node->getCallee()); 1190218893Sdim OS << "<<<"; 1191218893Sdim PrintCallArgs(Node->getConfig()); 1192218893Sdim OS << ">>>("; 1193218893Sdim PrintCallArgs(Node); 1194218893Sdim OS << ")"; 1195218893Sdim} 1196218893Sdim 1197193326Sedvoid StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) { 1198193326Sed OS << Node->getCastName() << '<'; 1199249423Sdim Node->getTypeAsWritten().print(OS, Policy); 1200249423Sdim OS << ">("; 1201193326Sed PrintExpr(Node->getSubExpr()); 1202193326Sed OS << ")"; 1203193326Sed} 1204193326Sed 1205193326Sedvoid StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) { 1206193326Sed VisitCXXNamedCastExpr(Node); 1207193326Sed} 1208193326Sed 1209193326Sedvoid StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) { 1210193326Sed VisitCXXNamedCastExpr(Node); 1211193326Sed} 1212193326Sed 1213193326Sedvoid StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) { 1214193326Sed VisitCXXNamedCastExpr(Node); 1215193326Sed} 1216193326Sed 1217193326Sedvoid StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) { 1218193326Sed VisitCXXNamedCastExpr(Node); 1219193326Sed} 1220193326Sed 1221193326Sedvoid StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) { 1222193326Sed OS << "typeid("; 1223193326Sed if (Node->isTypeOperand()) { 1224249423Sdim Node->getTypeOperand().print(OS, Policy); 1225193326Sed } else { 1226193326Sed PrintExpr(Node->getExprOperand()); 1227193326Sed } 1228193326Sed OS << ")"; 1229193326Sed} 1230193326Sed 1231218893Sdimvoid StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) { 1232218893Sdim OS << "__uuidof("; 1233218893Sdim if (Node->isTypeOperand()) { 1234249423Sdim Node->getTypeOperand().print(OS, Policy); 1235218893Sdim } else { 1236218893Sdim PrintExpr(Node->getExprOperand()); 1237218893Sdim } 1238218893Sdim OS << ")"; 1239218893Sdim} 1240218893Sdim 1241234353Sdimvoid StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) { 1242234353Sdim switch (Node->getLiteralOperatorKind()) { 1243234353Sdim case UserDefinedLiteral::LOK_Raw: 1244234353Sdim OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString(); 1245234353Sdim break; 1246234353Sdim case UserDefinedLiteral::LOK_Template: { 1247234353Sdim DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts()); 1248234353Sdim const TemplateArgumentList *Args = 1249234353Sdim cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs(); 1250234353Sdim assert(Args); 1251234353Sdim const TemplateArgument &Pack = Args->get(0); 1252234353Sdim for (TemplateArgument::pack_iterator I = Pack.pack_begin(), 1253234353Sdim E = Pack.pack_end(); I != E; ++I) { 1254239462Sdim char C = (char)I->getAsIntegral().getZExtValue(); 1255234353Sdim OS << C; 1256234353Sdim } 1257234353Sdim break; 1258234353Sdim } 1259234353Sdim case UserDefinedLiteral::LOK_Integer: { 1260234353Sdim // Print integer literal without suffix. 1261234353Sdim IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral()); 1262234353Sdim OS << Int->getValue().toString(10, /*isSigned*/false); 1263234353Sdim break; 1264234353Sdim } 1265243830Sdim case UserDefinedLiteral::LOK_Floating: { 1266243830Sdim // Print floating literal without suffix. 1267243830Sdim FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral()); 1268243830Sdim PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false); 1269243830Sdim break; 1270243830Sdim } 1271234353Sdim case UserDefinedLiteral::LOK_String: 1272234353Sdim case UserDefinedLiteral::LOK_Character: 1273234353Sdim PrintExpr(Node->getCookedLiteral()); 1274234353Sdim break; 1275234353Sdim } 1276234353Sdim OS << Node->getUDSuffix()->getName(); 1277234353Sdim} 1278234353Sdim 1279193326Sedvoid StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { 1280193326Sed OS << (Node->getValue() ? "true" : "false"); 1281193326Sed} 1282193326Sed 1283193326Sedvoid StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) { 1284193326Sed OS << "nullptr"; 1285193326Sed} 1286193326Sed 1287193326Sedvoid StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) { 1288193326Sed OS << "this"; 1289193326Sed} 1290193326Sed 1291193326Sedvoid StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) { 1292193326Sed if (Node->getSubExpr() == 0) 1293193326Sed OS << "throw"; 1294193326Sed else { 1295193326Sed OS << "throw "; 1296193326Sed PrintExpr(Node->getSubExpr()); 1297193326Sed } 1298193326Sed} 1299193326Sed 1300193326Sedvoid StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) { 1301193326Sed // Nothing to print: we picked up the default argument 1302193326Sed} 1303193326Sed 1304193326Sedvoid StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) { 1305249423Sdim Node->getType().print(OS, Policy); 1306193326Sed OS << "("; 1307193326Sed PrintExpr(Node->getSubExpr()); 1308193326Sed OS << ")"; 1309193326Sed} 1310193326Sed 1311193326Sedvoid StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) { 1312193326Sed PrintExpr(Node->getSubExpr()); 1313193326Sed} 1314193326Sed 1315193326Sedvoid StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) { 1316249423Sdim Node->getType().print(OS, Policy); 1317193326Sed OS << "("; 1318193326Sed for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(), 1319198092Srdivacky ArgEnd = Node->arg_end(); 1320193326Sed Arg != ArgEnd; ++Arg) { 1321193326Sed if (Arg != Node->arg_begin()) 1322193326Sed OS << ", "; 1323193326Sed PrintExpr(*Arg); 1324193326Sed } 1325193326Sed OS << ")"; 1326193326Sed} 1327193326Sed 1328234353Sdimvoid StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) { 1329234353Sdim OS << '['; 1330234353Sdim bool NeedComma = false; 1331234353Sdim switch (Node->getCaptureDefault()) { 1332234353Sdim case LCD_None: 1333234353Sdim break; 1334234353Sdim 1335234353Sdim case LCD_ByCopy: 1336234353Sdim OS << '='; 1337234353Sdim NeedComma = true; 1338234353Sdim break; 1339234353Sdim 1340234353Sdim case LCD_ByRef: 1341234353Sdim OS << '&'; 1342234353Sdim NeedComma = true; 1343234353Sdim break; 1344234353Sdim } 1345234353Sdim for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(), 1346234353Sdim CEnd = Node->explicit_capture_end(); 1347234353Sdim C != CEnd; 1348234353Sdim ++C) { 1349234353Sdim if (NeedComma) 1350234353Sdim OS << ", "; 1351234353Sdim NeedComma = true; 1352234353Sdim 1353234353Sdim switch (C->getCaptureKind()) { 1354234353Sdim case LCK_This: 1355234353Sdim OS << "this"; 1356234353Sdim break; 1357234353Sdim 1358234353Sdim case LCK_ByRef: 1359234353Sdim if (Node->getCaptureDefault() != LCD_ByRef) 1360234353Sdim OS << '&'; 1361234353Sdim OS << C->getCapturedVar()->getName(); 1362234353Sdim break; 1363234353Sdim 1364234353Sdim case LCK_ByCopy: 1365234353Sdim if (Node->getCaptureDefault() != LCD_ByCopy) 1366234353Sdim OS << '='; 1367234353Sdim OS << C->getCapturedVar()->getName(); 1368234353Sdim break; 1369234353Sdim } 1370234353Sdim } 1371234353Sdim OS << ']'; 1372234353Sdim 1373234353Sdim if (Node->hasExplicitParameters()) { 1374234353Sdim OS << " ("; 1375234353Sdim CXXMethodDecl *Method = Node->getCallOperator(); 1376234353Sdim NeedComma = false; 1377234353Sdim for (CXXMethodDecl::param_iterator P = Method->param_begin(), 1378234353Sdim PEnd = Method->param_end(); 1379234353Sdim P != PEnd; ++P) { 1380234353Sdim if (NeedComma) { 1381234353Sdim OS << ", "; 1382234353Sdim } else { 1383234353Sdim NeedComma = true; 1384234353Sdim } 1385234353Sdim std::string ParamStr = (*P)->getNameAsString(); 1386249423Sdim (*P)->getOriginalType().print(OS, Policy, ParamStr); 1387234353Sdim } 1388234353Sdim if (Method->isVariadic()) { 1389234353Sdim if (NeedComma) 1390234353Sdim OS << ", "; 1391234353Sdim OS << "..."; 1392234353Sdim } 1393234353Sdim OS << ')'; 1394234353Sdim 1395234353Sdim if (Node->isMutable()) 1396234353Sdim OS << " mutable"; 1397234353Sdim 1398234353Sdim const FunctionProtoType *Proto 1399234353Sdim = Method->getType()->getAs<FunctionProtoType>(); 1400249423Sdim Proto->printExceptionSpecification(OS, Policy); 1401234353Sdim 1402234353Sdim // FIXME: Attributes 1403234353Sdim 1404234353Sdim // Print the trailing return type if it was specified in the source. 1405249423Sdim if (Node->hasExplicitResultType()) { 1406249423Sdim OS << " -> "; 1407249423Sdim Proto->getResultType().print(OS, Policy); 1408249423Sdim } 1409234353Sdim } 1410234353Sdim 1411234353Sdim // Print the body. 1412234353Sdim CompoundStmt *Body = Node->getBody(); 1413234353Sdim OS << ' '; 1414234353Sdim PrintStmt(Body); 1415234353Sdim} 1416234353Sdim 1417210299Sedvoid StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) { 1418218893Sdim if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo()) 1419249423Sdim TSInfo->getType().print(OS, Policy); 1420218893Sdim else 1421249423Sdim Node->getType().print(OS, Policy); 1422249423Sdim OS << "()"; 1423193326Sed} 1424193326Sed 1425193326Sedvoid StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) { 1426193326Sed if (E->isGlobalNew()) 1427193326Sed OS << "::"; 1428193326Sed OS << "new "; 1429193326Sed unsigned NumPlace = E->getNumPlacementArgs(); 1430243830Sdim if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) { 1431193326Sed OS << "("; 1432193326Sed PrintExpr(E->getPlacementArg(0)); 1433193326Sed for (unsigned i = 1; i < NumPlace; ++i) { 1434243830Sdim if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i))) 1435243830Sdim break; 1436193326Sed OS << ", "; 1437193326Sed PrintExpr(E->getPlacementArg(i)); 1438193326Sed } 1439193326Sed OS << ") "; 1440193326Sed } 1441193326Sed if (E->isParenTypeId()) 1442193326Sed OS << "("; 1443193326Sed std::string TypeS; 1444193326Sed if (Expr *Size = E->getArraySize()) { 1445193326Sed llvm::raw_string_ostream s(TypeS); 1446249423Sdim s << '['; 1447239462Sdim Size->printPretty(s, Helper, Policy); 1448249423Sdim s << ']'; 1449193326Sed } 1450249423Sdim E->getAllocatedType().print(OS, Policy, TypeS); 1451193326Sed if (E->isParenTypeId()) 1452193326Sed OS << ")"; 1453193326Sed 1454234353Sdim CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle(); 1455234353Sdim if (InitStyle) { 1456234353Sdim if (InitStyle == CXXNewExpr::CallInit) 1457234353Sdim OS << "("; 1458234353Sdim PrintExpr(E->getInitializer()); 1459234353Sdim if (InitStyle == CXXNewExpr::CallInit) 1460234353Sdim OS << ")"; 1461193326Sed } 1462193326Sed} 1463193326Sed 1464193326Sedvoid StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { 1465193326Sed if (E->isGlobalDelete()) 1466193326Sed OS << "::"; 1467193326Sed OS << "delete "; 1468193326Sed if (E->isArrayForm()) 1469193326Sed OS << "[] "; 1470193326Sed PrintExpr(E->getArgument()); 1471193326Sed} 1472193326Sed 1473198092Srdivackyvoid StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { 1474198092Srdivacky PrintExpr(E->getBase()); 1475198092Srdivacky if (E->isArrow()) 1476198092Srdivacky OS << "->"; 1477198092Srdivacky else 1478198092Srdivacky OS << '.'; 1479198092Srdivacky if (E->getQualifier()) 1480198092Srdivacky E->getQualifier()->print(OS, Policy); 1481243830Sdim OS << "~"; 1482198092Srdivacky 1483204643Srdivacky if (IdentifierInfo *II = E->getDestroyedTypeIdentifier()) 1484204643Srdivacky OS << II->getName(); 1485204643Srdivacky else 1486249423Sdim E->getDestroyedType().print(OS, Policy); 1487198092Srdivacky} 1488198092Srdivacky 1489193326Sedvoid StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) { 1490249423Sdim if (E->isListInitialization()) 1491249423Sdim OS << "{ "; 1492249423Sdim 1493218893Sdim for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { 1494218893Sdim if (isa<CXXDefaultArgExpr>(E->getArg(i))) { 1495218893Sdim // Don't print any defaulted arguments 1496218893Sdim break; 1497218893Sdim } 1498218893Sdim 1499218893Sdim if (i) OS << ", "; 1500218893Sdim PrintExpr(E->getArg(i)); 1501202379Srdivacky } 1502249423Sdim 1503249423Sdim if (E->isListInitialization()) 1504249423Sdim OS << " }"; 1505193326Sed} 1506193326Sed 1507218893Sdimvoid StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) { 1508193326Sed // Just forward to the sub expression. 1509193326Sed PrintExpr(E->getSubExpr()); 1510193326Sed} 1511193326Sed 1512198092Srdivackyvoid 1513193326SedStmtPrinter::VisitCXXUnresolvedConstructExpr( 1514193326Sed CXXUnresolvedConstructExpr *Node) { 1515249423Sdim Node->getTypeAsWritten().print(OS, Policy); 1516193326Sed OS << "("; 1517193326Sed for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(), 1518198092Srdivacky ArgEnd = Node->arg_end(); 1519193326Sed Arg != ArgEnd; ++Arg) { 1520193326Sed if (Arg != Node->arg_begin()) 1521193326Sed OS << ", "; 1522193326Sed PrintExpr(*Arg); 1523193326Sed } 1524193326Sed OS << ")"; 1525193326Sed} 1526193326Sed 1527199990Srdivackyvoid StmtPrinter::VisitCXXDependentScopeMemberExpr( 1528199990Srdivacky CXXDependentScopeMemberExpr *Node) { 1529200583Srdivacky if (!Node->isImplicitAccess()) { 1530200583Srdivacky PrintExpr(Node->getBase()); 1531200583Srdivacky OS << (Node->isArrow() ? "->" : "."); 1532200583Srdivacky } 1533198092Srdivacky if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1534198092Srdivacky Qualifier->print(OS, Policy); 1535234353Sdim if (Node->hasTemplateKeyword()) 1536198092Srdivacky OS << "template "; 1537212904Sdim OS << Node->getMemberNameInfo(); 1538249423Sdim if (Node->hasExplicitTemplateArgs()) 1539249423Sdim TemplateSpecializationType::PrintTemplateArgumentList( 1540249423Sdim OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 1541193326Sed} 1542193326Sed 1543199990Srdivackyvoid StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) { 1544200583Srdivacky if (!Node->isImplicitAccess()) { 1545200583Srdivacky PrintExpr(Node->getBase()); 1546200583Srdivacky OS << (Node->isArrow() ? "->" : "."); 1547200583Srdivacky } 1548199990Srdivacky if (NestedNameSpecifier *Qualifier = Node->getQualifier()) 1549199990Srdivacky Qualifier->print(OS, Policy); 1550234353Sdim if (Node->hasTemplateKeyword()) 1551234353Sdim OS << "template "; 1552212904Sdim OS << Node->getMemberNameInfo(); 1553249423Sdim if (Node->hasExplicitTemplateArgs()) 1554249423Sdim TemplateSpecializationType::PrintTemplateArgumentList( 1555249423Sdim OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy); 1556199990Srdivacky} 1557199990Srdivacky 1558193326Sedstatic const char *getTypeTraitName(UnaryTypeTrait UTT) { 1559193326Sed switch (UTT) { 1560193326Sed case UTT_HasNothrowAssign: return "__has_nothrow_assign"; 1561249423Sdim case UTT_HasNothrowMoveAssign: return "__has_nothrow_move_assign"; 1562193326Sed case UTT_HasNothrowConstructor: return "__has_nothrow_constructor"; 1563221345Sdim case UTT_HasNothrowCopy: return "__has_nothrow_copy"; 1564193326Sed case UTT_HasTrivialAssign: return "__has_trivial_assign"; 1565249423Sdim case UTT_HasTrivialMoveAssign: return "__has_trivial_move_assign"; 1566249423Sdim case UTT_HasTrivialMoveConstructor: return "__has_trivial_move_constructor"; 1567223017Sdim case UTT_HasTrivialDefaultConstructor: return "__has_trivial_constructor"; 1568221345Sdim case UTT_HasTrivialCopy: return "__has_trivial_copy"; 1569193326Sed case UTT_HasTrivialDestructor: return "__has_trivial_destructor"; 1570193326Sed case UTT_HasVirtualDestructor: return "__has_virtual_destructor"; 1571193326Sed case UTT_IsAbstract: return "__is_abstract"; 1572221345Sdim case UTT_IsArithmetic: return "__is_arithmetic"; 1573221345Sdim case UTT_IsArray: return "__is_array"; 1574193326Sed case UTT_IsClass: return "__is_class"; 1575221345Sdim case UTT_IsCompleteType: return "__is_complete_type"; 1576221345Sdim case UTT_IsCompound: return "__is_compound"; 1577221345Sdim case UTT_IsConst: return "__is_const"; 1578193326Sed case UTT_IsEmpty: return "__is_empty"; 1579193326Sed case UTT_IsEnum: return "__is_enum"; 1580234353Sdim case UTT_IsFinal: return "__is_final"; 1581221345Sdim case UTT_IsFloatingPoint: return "__is_floating_point"; 1582221345Sdim case UTT_IsFunction: return "__is_function"; 1583221345Sdim case UTT_IsFundamental: return "__is_fundamental"; 1584221345Sdim case UTT_IsIntegral: return "__is_integral"; 1585243830Sdim case UTT_IsInterfaceClass: return "__is_interface_class"; 1586221345Sdim case UTT_IsLiteral: return "__is_literal"; 1587221345Sdim case UTT_IsLvalueReference: return "__is_lvalue_reference"; 1588221345Sdim case UTT_IsMemberFunctionPointer: return "__is_member_function_pointer"; 1589221345Sdim case UTT_IsMemberObjectPointer: return "__is_member_object_pointer"; 1590221345Sdim case UTT_IsMemberPointer: return "__is_member_pointer"; 1591221345Sdim case UTT_IsObject: return "__is_object"; 1592193326Sed case UTT_IsPOD: return "__is_pod"; 1593221345Sdim case UTT_IsPointer: return "__is_pointer"; 1594193326Sed case UTT_IsPolymorphic: return "__is_polymorphic"; 1595221345Sdim case UTT_IsReference: return "__is_reference"; 1596221345Sdim case UTT_IsRvalueReference: return "__is_rvalue_reference"; 1597221345Sdim case UTT_IsScalar: return "__is_scalar"; 1598221345Sdim case UTT_IsSigned: return "__is_signed"; 1599221345Sdim case UTT_IsStandardLayout: return "__is_standard_layout"; 1600221345Sdim case UTT_IsTrivial: return "__is_trivial"; 1601223017Sdim case UTT_IsTriviallyCopyable: return "__is_trivially_copyable"; 1602193326Sed case UTT_IsUnion: return "__is_union"; 1603221345Sdim case UTT_IsUnsigned: return "__is_unsigned"; 1604221345Sdim case UTT_IsVoid: return "__is_void"; 1605221345Sdim case UTT_IsVolatile: return "__is_volatile"; 1606193326Sed } 1607221345Sdim llvm_unreachable("Type trait not covered by switch statement"); 1608193326Sed} 1609193326Sed 1610218893Sdimstatic const char *getTypeTraitName(BinaryTypeTrait BTT) { 1611218893Sdim switch (BTT) { 1612234353Sdim case BTT_IsBaseOf: return "__is_base_of"; 1613234353Sdim case BTT_IsConvertible: return "__is_convertible"; 1614234353Sdim case BTT_IsSame: return "__is_same"; 1615234353Sdim case BTT_TypeCompatible: return "__builtin_types_compatible_p"; 1616234353Sdim case BTT_IsConvertibleTo: return "__is_convertible_to"; 1617234353Sdim case BTT_IsTriviallyAssignable: return "__is_trivially_assignable"; 1618218893Sdim } 1619221345Sdim llvm_unreachable("Binary type trait not covered by switch"); 1620218893Sdim} 1621218893Sdim 1622234353Sdimstatic const char *getTypeTraitName(TypeTrait TT) { 1623234353Sdim switch (TT) { 1624234353Sdim case clang::TT_IsTriviallyConstructible:return "__is_trivially_constructible"; 1625234353Sdim } 1626234353Sdim llvm_unreachable("Type trait not covered by switch"); 1627234353Sdim} 1628234353Sdim 1629221345Sdimstatic const char *getTypeTraitName(ArrayTypeTrait ATT) { 1630221345Sdim switch (ATT) { 1631221345Sdim case ATT_ArrayRank: return "__array_rank"; 1632221345Sdim case ATT_ArrayExtent: return "__array_extent"; 1633221345Sdim } 1634221345Sdim llvm_unreachable("Array type trait not covered by switch"); 1635221345Sdim} 1636221345Sdim 1637221345Sdimstatic const char *getExpressionTraitName(ExpressionTrait ET) { 1638221345Sdim switch (ET) { 1639221345Sdim case ET_IsLValueExpr: return "__is_lvalue_expr"; 1640221345Sdim case ET_IsRValueExpr: return "__is_rvalue_expr"; 1641221345Sdim } 1642221345Sdim llvm_unreachable("Expression type trait not covered by switch"); 1643221345Sdim} 1644221345Sdim 1645193326Sedvoid StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { 1646249423Sdim OS << getTypeTraitName(E->getTrait()) << '('; 1647249423Sdim E->getQueriedType().print(OS, Policy); 1648249423Sdim OS << ')'; 1649193326Sed} 1650193326Sed 1651218893Sdimvoid StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) { 1652249423Sdim OS << getTypeTraitName(E->getTrait()) << '('; 1653249423Sdim E->getLhsType().print(OS, Policy); 1654249423Sdim OS << ','; 1655249423Sdim E->getRhsType().print(OS, Policy); 1656249423Sdim OS << ')'; 1657218893Sdim} 1658218893Sdim 1659234353Sdimvoid StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) { 1660234353Sdim OS << getTypeTraitName(E->getTrait()) << "("; 1661234353Sdim for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) { 1662234353Sdim if (I > 0) 1663234353Sdim OS << ", "; 1664249423Sdim E->getArg(I)->getType().print(OS, Policy); 1665234353Sdim } 1666234353Sdim OS << ")"; 1667234353Sdim} 1668234353Sdim 1669221345Sdimvoid StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { 1670249423Sdim OS << getTypeTraitName(E->getTrait()) << '('; 1671249423Sdim E->getQueriedType().print(OS, Policy); 1672249423Sdim OS << ')'; 1673221345Sdim} 1674221345Sdim 1675221345Sdimvoid StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { 1676249423Sdim OS << getExpressionTraitName(E->getTrait()) << '('; 1677249423Sdim PrintExpr(E->getQueriedExpression()); 1678249423Sdim OS << ')'; 1679221345Sdim} 1680221345Sdim 1681218893Sdimvoid StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { 1682218893Sdim OS << "noexcept("; 1683218893Sdim PrintExpr(E->getOperand()); 1684218893Sdim OS << ")"; 1685218893Sdim} 1686218893Sdim 1687218893Sdimvoid StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) { 1688218893Sdim PrintExpr(E->getPattern()); 1689218893Sdim OS << "..."; 1690218893Sdim} 1691218893Sdim 1692218893Sdimvoid StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { 1693234353Sdim OS << "sizeof...(" << *E->getPack() << ")"; 1694218893Sdim} 1695218893Sdim 1696218893Sdimvoid StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr( 1697218893Sdim SubstNonTypeTemplateParmPackExpr *Node) { 1698234353Sdim OS << *Node->getParameterPack(); 1699218893Sdim} 1700218893Sdim 1701224145Sdimvoid StmtPrinter::VisitSubstNonTypeTemplateParmExpr( 1702224145Sdim SubstNonTypeTemplateParmExpr *Node) { 1703224145Sdim Visit(Node->getReplacement()); 1704224145Sdim} 1705224145Sdim 1706243830Sdimvoid StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) { 1707243830Sdim OS << *E->getParameterPack(); 1708243830Sdim} 1709243830Sdim 1710224145Sdimvoid StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){ 1711224145Sdim PrintExpr(Node->GetTemporaryExpr()); 1712224145Sdim} 1713224145Sdim 1714198092Srdivacky// Obj-C 1715193326Sed 1716193326Sedvoid StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) { 1717193326Sed OS << "@"; 1718193326Sed VisitStringLiteral(Node->getString()); 1719193326Sed} 1720193326Sed 1721239462Sdimvoid StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) { 1722234353Sdim OS << "@"; 1723239462Sdim Visit(E->getSubExpr()); 1724234353Sdim} 1725234353Sdim 1726234353Sdimvoid StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) { 1727234353Sdim OS << "@[ "; 1728234353Sdim StmtRange ch = E->children(); 1729234353Sdim if (ch.first != ch.second) { 1730234353Sdim while (1) { 1731234353Sdim Visit(*ch.first); 1732234353Sdim ++ch.first; 1733234353Sdim if (ch.first == ch.second) break; 1734234353Sdim OS << ", "; 1735234353Sdim } 1736234353Sdim } 1737234353Sdim OS << " ]"; 1738234353Sdim} 1739234353Sdim 1740234353Sdimvoid StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) { 1741234353Sdim OS << "@{ "; 1742234353Sdim for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) { 1743234353Sdim if (I > 0) 1744234353Sdim OS << ", "; 1745234353Sdim 1746234353Sdim ObjCDictionaryElement Element = E->getKeyValueElement(I); 1747234353Sdim Visit(Element.Key); 1748234353Sdim OS << " : "; 1749234353Sdim Visit(Element.Value); 1750234353Sdim if (Element.isPackExpansion()) 1751234353Sdim OS << "..."; 1752234353Sdim } 1753234353Sdim OS << " }"; 1754234353Sdim} 1755234353Sdim 1756193326Sedvoid StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) { 1757249423Sdim OS << "@encode("; 1758249423Sdim Node->getEncodedType().print(OS, Policy); 1759249423Sdim OS << ')'; 1760193326Sed} 1761193326Sed 1762193326Sedvoid StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) { 1763193326Sed OS << "@selector(" << Node->getSelector().getAsString() << ')'; 1764193326Sed} 1765193326Sed 1766193326Sedvoid StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) { 1767226633Sdim OS << "@protocol(" << *Node->getProtocol() << ')'; 1768193326Sed} 1769193326Sed 1770193326Sedvoid StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) { 1771193326Sed OS << "["; 1772207619Srdivacky switch (Mess->getReceiverKind()) { 1773207619Srdivacky case ObjCMessageExpr::Instance: 1774207619Srdivacky PrintExpr(Mess->getInstanceReceiver()); 1775207619Srdivacky break; 1776207619Srdivacky 1777207619Srdivacky case ObjCMessageExpr::Class: 1778249423Sdim Mess->getClassReceiver().print(OS, Policy); 1779207619Srdivacky break; 1780207619Srdivacky 1781207619Srdivacky case ObjCMessageExpr::SuperInstance: 1782207619Srdivacky case ObjCMessageExpr::SuperClass: 1783207619Srdivacky OS << "Super"; 1784207619Srdivacky break; 1785207619Srdivacky } 1786207619Srdivacky 1787193326Sed OS << ' '; 1788193326Sed Selector selector = Mess->getSelector(); 1789193326Sed if (selector.isUnarySelector()) { 1790218893Sdim OS << selector.getNameForSlot(0); 1791193326Sed } else { 1792193326Sed for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) { 1793193326Sed if (i < selector.getNumArgs()) { 1794193326Sed if (i > 0) OS << ' '; 1795193326Sed if (selector.getIdentifierInfoForSlot(i)) 1796193326Sed OS << selector.getIdentifierInfoForSlot(i)->getName() << ':'; 1797193326Sed else 1798193326Sed OS << ":"; 1799193326Sed } 1800193326Sed else OS << ", "; // Handle variadic methods. 1801198092Srdivacky 1802193326Sed PrintExpr(Mess->getArg(i)); 1803193326Sed } 1804193326Sed } 1805193326Sed OS << "]"; 1806193326Sed} 1807193326Sed 1808234353Sdimvoid StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) { 1809234353Sdim OS << (Node->getValue() ? "__objc_yes" : "__objc_no"); 1810234353Sdim} 1811234353Sdim 1812224145Sdimvoid 1813224145SdimStmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) { 1814224145Sdim PrintExpr(E->getSubExpr()); 1815224145Sdim} 1816193326Sed 1817224145Sdimvoid 1818224145SdimStmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) { 1819249423Sdim OS << '(' << E->getBridgeKindName(); 1820249423Sdim E->getType().print(OS, Policy); 1821249423Sdim OS << ')'; 1822224145Sdim PrintExpr(E->getSubExpr()); 1823224145Sdim} 1824224145Sdim 1825193326Sedvoid StmtPrinter::VisitBlockExpr(BlockExpr *Node) { 1826193326Sed BlockDecl *BD = Node->getBlockDecl(); 1827193326Sed OS << "^"; 1828198092Srdivacky 1829193326Sed const FunctionType *AFT = Node->getFunctionType(); 1830198092Srdivacky 1831193326Sed if (isa<FunctionNoProtoType>(AFT)) { 1832193326Sed OS << "()"; 1833193326Sed } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) { 1834193326Sed OS << '('; 1835193326Sed for (BlockDecl::param_iterator AI = BD->param_begin(), 1836193326Sed E = BD->param_end(); AI != E; ++AI) { 1837193326Sed if (AI != BD->param_begin()) OS << ", "; 1838249423Sdim std::string ParamStr = (*AI)->getNameAsString(); 1839249423Sdim (*AI)->getType().print(OS, Policy, ParamStr); 1840193326Sed } 1841198092Srdivacky 1842193326Sed const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); 1843193326Sed if (FT->isVariadic()) { 1844193326Sed if (!BD->param_empty()) OS << ", "; 1845193326Sed OS << "..."; 1846193326Sed } 1847193326Sed OS << ')'; 1848193326Sed } 1849249423Sdim OS << "{ }"; 1850193326Sed} 1851193326Sed 1852234353Sdimvoid StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) { 1853234353Sdim PrintExpr(Node->getSourceExpr()); 1854193326Sed} 1855218893Sdim 1856223017Sdimvoid StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) { 1857223017Sdim OS << "__builtin_astype("; 1858223017Sdim PrintExpr(Node->getSrcExpr()); 1859249423Sdim OS << ", "; 1860249423Sdim Node->getType().print(OS, Policy); 1861223017Sdim OS << ")"; 1862223017Sdim} 1863223017Sdim 1864193326Sed//===----------------------------------------------------------------------===// 1865193326Sed// Stmt method implementations 1866193326Sed//===----------------------------------------------------------------------===// 1867193326Sed 1868239462Sdimvoid Stmt::dumpPretty(ASTContext &Context) const { 1869239462Sdim printPretty(llvm::errs(), 0, PrintingPolicy(Context.getLangOpts())); 1870193326Sed} 1871193326Sed 1872239462Sdimvoid Stmt::printPretty(raw_ostream &OS, 1873239462Sdim PrinterHelper *Helper, 1874193326Sed const PrintingPolicy &Policy, 1875193326Sed unsigned Indentation) const { 1876193326Sed if (this == 0) { 1877193326Sed OS << "<NULL>"; 1878193326Sed return; 1879193326Sed } 1880193326Sed 1881239462Sdim StmtPrinter P(OS, Helper, Policy, Indentation); 1882193326Sed P.Visit(const_cast<Stmt*>(this)); 1883193326Sed} 1884193326Sed 1885193326Sed//===----------------------------------------------------------------------===// 1886193326Sed// PrinterHelper 1887193326Sed//===----------------------------------------------------------------------===// 1888193326Sed 1889193326Sed// Implement virtual destructor. 1890193326SedPrinterHelper::~PrinterHelper() {} 1891