ASTDumper.cpp 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495
  1. //===--- ASTDumper.cpp - Dumping implementation for ASTs ------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the AST dump methods, which dump out the
  11. // AST in a form that exposes type details and other fields.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/Attr.h"
  16. #include "clang/AST/CommentVisitor.h"
  17. #include "clang/AST/DeclCXX.h"
  18. #include "clang/AST/DeclLookups.h"
  19. #include "clang/AST/DeclObjC.h"
  20. #include "clang/AST/DeclVisitor.h"
  21. #include "clang/AST/StmtVisitor.h"
  22. #include "clang/AST/TypeVisitor.h"
  23. #include "clang/Basic/Module.h"
  24. #include "clang/Basic/SourceManager.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. using namespace clang;
  27. using namespace clang::comments;
  28. //===----------------------------------------------------------------------===//
  29. // ASTDumper Visitor
  30. //===----------------------------------------------------------------------===//
  31. namespace {
  32. // Colors used for various parts of the AST dump
  33. // Do not use bold yellow for any text. It is hard to read on white screens.
  34. struct TerminalColor {
  35. raw_ostream::Colors Color;
  36. bool Bold;
  37. };
  38. // Red - CastColor
  39. // Green - TypeColor
  40. // Bold Green - DeclKindNameColor, UndeserializedColor
  41. // Yellow - AddressColor, LocationColor
  42. // Blue - CommentColor, NullColor, IndentColor
  43. // Bold Blue - AttrColor
  44. // Bold Magenta - StmtColor
  45. // Cyan - ValueKindColor, ObjectKindColor
  46. // Bold Cyan - ValueColor, DeclNameColor
  47. // Decl kind names (VarDecl, FunctionDecl, etc)
  48. static const TerminalColor DeclKindNameColor = { raw_ostream::GREEN, true };
  49. // Attr names (CleanupAttr, GuardedByAttr, etc)
  50. static const TerminalColor AttrColor = { raw_ostream::BLUE, true };
  51. // Statement names (DeclStmt, ImplicitCastExpr, etc)
  52. static const TerminalColor StmtColor = { raw_ostream::MAGENTA, true };
  53. // Comment names (FullComment, ParagraphComment, TextComment, etc)
  54. static const TerminalColor CommentColor = { raw_ostream::BLUE, false };
  55. // Type names (int, float, etc, plus user defined types)
  56. static const TerminalColor TypeColor = { raw_ostream::GREEN, false };
  57. // Pointer address
  58. static const TerminalColor AddressColor = { raw_ostream::YELLOW, false };
  59. // Source locations
  60. static const TerminalColor LocationColor = { raw_ostream::YELLOW, false };
  61. // lvalue/xvalue
  62. static const TerminalColor ValueKindColor = { raw_ostream::CYAN, false };
  63. // bitfield/objcproperty/objcsubscript/vectorcomponent
  64. static const TerminalColor ObjectKindColor = { raw_ostream::CYAN, false };
  65. // Null statements
  66. static const TerminalColor NullColor = { raw_ostream::BLUE, false };
  67. // Undeserialized entities
  68. static const TerminalColor UndeserializedColor = { raw_ostream::GREEN, true };
  69. // CastKind from CastExpr's
  70. static const TerminalColor CastColor = { raw_ostream::RED, false };
  71. // Value of the statement
  72. static const TerminalColor ValueColor = { raw_ostream::CYAN, true };
  73. // Decl names
  74. static const TerminalColor DeclNameColor = { raw_ostream::CYAN, true };
  75. // Indents ( `, -. | )
  76. static const TerminalColor IndentColor = { raw_ostream::BLUE, false };
  77. class ASTDumper
  78. : public ConstDeclVisitor<ASTDumper>, public ConstStmtVisitor<ASTDumper>,
  79. public ConstCommentVisitor<ASTDumper>, public TypeVisitor<ASTDumper> {
  80. raw_ostream &OS;
  81. const CommandTraits *Traits;
  82. const SourceManager *SM;
  83. /// Pending[i] is an action to dump an entity at level i.
  84. llvm::SmallVector<std::function<void(bool isLastChild)>, 32> Pending;
  85. /// Indicates whether we're at the top level.
  86. bool TopLevel;
  87. /// Indicates if we're handling the first child after entering a new depth.
  88. bool FirstChild;
  89. /// Prefix for currently-being-dumped entity.
  90. std::string Prefix;
  91. /// Keep track of the last location we print out so that we can
  92. /// print out deltas from then on out.
  93. const char *LastLocFilename;
  94. unsigned LastLocLine;
  95. /// The \c FullComment parent of the comment being dumped.
  96. const FullComment *FC;
  97. bool ShowColors;
  98. /// Dump a child of the current node.
  99. template<typename Fn> void dumpChild(Fn doDumpChild) {
  100. // If we're at the top level, there's nothing interesting to do; just
  101. // run the dumper.
  102. if (TopLevel) {
  103. TopLevel = false;
  104. doDumpChild();
  105. while (!Pending.empty()) {
  106. Pending.back()(true);
  107. Pending.pop_back();
  108. }
  109. Prefix.clear();
  110. OS << "\n";
  111. TopLevel = true;
  112. return;
  113. }
  114. const FullComment *OrigFC = FC;
  115. auto dumpWithIndent = [this, doDumpChild, OrigFC](bool isLastChild) {
  116. // Print out the appropriate tree structure and work out the prefix for
  117. // children of this node. For instance:
  118. //
  119. // A Prefix = ""
  120. // |-B Prefix = "| "
  121. // | `-C Prefix = "| "
  122. // `-D Prefix = " "
  123. // |-E Prefix = " | "
  124. // `-F Prefix = " "
  125. // G Prefix = ""
  126. //
  127. // Note that the first level gets no prefix.
  128. {
  129. OS << '\n';
  130. ColorScope Color(*this, IndentColor);
  131. OS << Prefix << (isLastChild ? '`' : '|') << '-';
  132. this->Prefix.push_back(isLastChild ? ' ' : '|');
  133. this->Prefix.push_back(' ');
  134. }
  135. FirstChild = true;
  136. unsigned Depth = Pending.size();
  137. FC = OrigFC;
  138. doDumpChild();
  139. // If any children are left, they're the last at their nesting level.
  140. // Dump those ones out now.
  141. while (Depth < Pending.size()) {
  142. Pending.back()(true);
  143. this->Pending.pop_back();
  144. }
  145. // Restore the old prefix.
  146. this->Prefix.resize(Prefix.size() - 2);
  147. };
  148. if (FirstChild) {
  149. Pending.push_back(std::move(dumpWithIndent));
  150. } else {
  151. Pending.back()(false);
  152. Pending.back() = std::move(dumpWithIndent);
  153. }
  154. FirstChild = false;
  155. }
  156. class ColorScope {
  157. ASTDumper &Dumper;
  158. public:
  159. ColorScope(ASTDumper &Dumper, TerminalColor Color)
  160. : Dumper(Dumper) {
  161. if (Dumper.ShowColors)
  162. Dumper.OS.changeColor(Color.Color, Color.Bold);
  163. }
  164. ~ColorScope() {
  165. if (Dumper.ShowColors)
  166. Dumper.OS.resetColor();
  167. }
  168. };
  169. public:
  170. ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
  171. const SourceManager *SM)
  172. : OS(OS), Traits(Traits), SM(SM), TopLevel(true), FirstChild(true),
  173. LastLocFilename(""), LastLocLine(~0U), FC(nullptr),
  174. ShowColors(SM && SM->getDiagnostics().getShowColors()) { }
  175. ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
  176. const SourceManager *SM, bool ShowColors)
  177. : OS(OS), Traits(Traits), SM(SM), TopLevel(true), FirstChild(true),
  178. LastLocFilename(""), LastLocLine(~0U),
  179. ShowColors(ShowColors) { }
  180. void dumpDecl(const Decl *D);
  181. void dumpStmt(const Stmt *S);
  182. void dumpFullComment(const FullComment *C);
  183. // Utilities
  184. void dumpPointer(const void *Ptr);
  185. void dumpSourceRange(SourceRange R);
  186. void dumpLocation(SourceLocation Loc);
  187. void dumpBareType(QualType T, bool Desugar = true);
  188. void dumpType(QualType T);
  189. void dumpTypeAsChild(QualType T);
  190. void dumpTypeAsChild(const Type *T);
  191. void dumpBareDeclRef(const Decl *Node);
  192. void dumpDeclRef(const Decl *Node, const char *Label = nullptr);
  193. void dumpName(const NamedDecl *D);
  194. bool hasNodes(const DeclContext *DC);
  195. void dumpDeclContext(const DeclContext *DC);
  196. void dumpLookups(const DeclContext *DC, bool DumpDecls);
  197. void dumpAttr(const Attr *A);
  198. void dumpHLSLUnusualAnnotations(const llvm::ArrayRef<hlsl::UnusualAnnotation*> UA);
  199. // C++ Utilities
  200. void dumpAccessSpecifier(AccessSpecifier AS);
  201. void dumpCXXCtorInitializer(const CXXCtorInitializer *Init);
  202. void dumpTemplateParameters(const TemplateParameterList *TPL);
  203. void dumpTemplateArgumentListInfo(const TemplateArgumentListInfo &TALI);
  204. void dumpTemplateArgumentLoc(const TemplateArgumentLoc &A);
  205. void dumpTemplateArgumentList(const TemplateArgumentList &TAL);
  206. void dumpTemplateArgument(const TemplateArgument &A,
  207. SourceRange R = SourceRange());
  208. // Objective-C utilities.
  209. void dumpObjCTypeParamList(const ObjCTypeParamList *typeParams);
  210. // Types
  211. void VisitComplexType(const ComplexType *T) {
  212. dumpTypeAsChild(T->getElementType());
  213. }
  214. void VisitPointerType(const PointerType *T) {
  215. dumpTypeAsChild(T->getPointeeType());
  216. }
  217. void VisitBlockPointerType(const BlockPointerType *T) {
  218. dumpTypeAsChild(T->getPointeeType());
  219. }
  220. void VisitReferenceType(const ReferenceType *T) {
  221. dumpTypeAsChild(T->getPointeeType());
  222. }
  223. void VisitRValueReferenceType(const ReferenceType *T) {
  224. if (T->isSpelledAsLValue())
  225. OS << " written as lvalue reference";
  226. VisitReferenceType(T);
  227. }
  228. void VisitMemberPointerType(const MemberPointerType *T) {
  229. dumpTypeAsChild(T->getClass());
  230. dumpTypeAsChild(T->getPointeeType());
  231. }
  232. void VisitArrayType(const ArrayType *T) {
  233. switch (T->getSizeModifier()) {
  234. case ArrayType::Normal: break;
  235. case ArrayType::Static: OS << " static"; break;
  236. case ArrayType::Star: OS << " *"; break;
  237. }
  238. OS << " " << T->getIndexTypeQualifiers().getAsString();
  239. dumpTypeAsChild(T->getElementType());
  240. }
  241. void VisitConstantArrayType(const ConstantArrayType *T) {
  242. OS << " " << T->getSize();
  243. VisitArrayType(T);
  244. }
  245. void VisitVariableArrayType(const VariableArrayType *T) {
  246. OS << " ";
  247. dumpSourceRange(T->getBracketsRange());
  248. VisitArrayType(T);
  249. dumpStmt(T->getSizeExpr());
  250. }
  251. void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
  252. VisitArrayType(T);
  253. OS << " ";
  254. dumpSourceRange(T->getBracketsRange());
  255. dumpStmt(T->getSizeExpr());
  256. }
  257. void VisitDependentSizedExtVectorType(
  258. const DependentSizedExtVectorType *T) {
  259. OS << " ";
  260. dumpLocation(T->getAttributeLoc());
  261. dumpTypeAsChild(T->getElementType());
  262. dumpStmt(T->getSizeExpr());
  263. }
  264. void VisitVectorType(const VectorType *T) {
  265. switch (T->getVectorKind()) {
  266. case VectorType::GenericVector: break;
  267. case VectorType::AltiVecVector: OS << " altivec"; break;
  268. case VectorType::AltiVecPixel: OS << " altivec pixel"; break;
  269. case VectorType::AltiVecBool: OS << " altivec bool"; break;
  270. case VectorType::NeonVector: OS << " neon"; break;
  271. case VectorType::NeonPolyVector: OS << " neon poly"; break;
  272. }
  273. OS << " " << T->getNumElements();
  274. dumpTypeAsChild(T->getElementType());
  275. }
  276. void VisitFunctionType(const FunctionType *T) {
  277. auto EI = T->getExtInfo();
  278. if (EI.getNoReturn()) OS << " noreturn";
  279. if (EI.getProducesResult()) OS << " produces_result";
  280. if (EI.getHasRegParm()) OS << " regparm " << EI.getRegParm();
  281. OS << " " << FunctionType::getNameForCallConv(EI.getCC());
  282. dumpTypeAsChild(T->getReturnType());
  283. }
  284. void VisitFunctionProtoType(const FunctionProtoType *T) {
  285. auto EPI = T->getExtProtoInfo();
  286. if (EPI.HasTrailingReturn) OS << " trailing_return";
  287. if (T->isConst()) OS << " const";
  288. if (T->isVolatile()) OS << " volatile";
  289. if (T->isRestrict()) OS << " restrict";
  290. switch (EPI.RefQualifier) {
  291. case RQ_None: break;
  292. case RQ_LValue: OS << " &"; break;
  293. case RQ_RValue: OS << " &&"; break;
  294. }
  295. // FIXME: Exception specification.
  296. // FIXME: Consumed parameters.
  297. VisitFunctionType(T);
  298. for (QualType PT : T->getParamTypes())
  299. dumpTypeAsChild(PT);
  300. if (EPI.Variadic)
  301. dumpChild([=] { OS << "..."; });
  302. }
  303. void VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
  304. dumpDeclRef(T->getDecl());
  305. }
  306. void VisitTypedefType(const TypedefType *T) {
  307. dumpDeclRef(T->getDecl());
  308. }
  309. void VisitTypeOfExprType(const TypeOfExprType *T) {
  310. dumpStmt(T->getUnderlyingExpr());
  311. }
  312. void VisitDecltypeType(const DecltypeType *T) {
  313. dumpStmt(T->getUnderlyingExpr());
  314. }
  315. void VisitUnaryTransformType(const UnaryTransformType *T) {
  316. switch (T->getUTTKind()) {
  317. case UnaryTransformType::EnumUnderlyingType:
  318. OS << " underlying_type";
  319. break;
  320. }
  321. dumpTypeAsChild(T->getBaseType());
  322. }
  323. void VisitTagType(const TagType *T) {
  324. dumpDeclRef(T->getDecl());
  325. }
  326. void VisitAttributedType(const AttributedType *T) {
  327. // FIXME: AttrKind
  328. dumpTypeAsChild(T->getModifiedType());
  329. }
  330. void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
  331. OS << " depth " << T->getDepth() << " index " << T->getIndex();
  332. if (T->isParameterPack()) OS << " pack";
  333. dumpDeclRef(T->getDecl());
  334. }
  335. void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) {
  336. dumpTypeAsChild(T->getReplacedParameter());
  337. }
  338. void VisitSubstTemplateTypeParmPackType(
  339. const SubstTemplateTypeParmPackType *T) {
  340. dumpTypeAsChild(T->getReplacedParameter());
  341. dumpTemplateArgument(T->getArgumentPack());
  342. }
  343. void VisitAutoType(const AutoType *T) {
  344. if (T->isDecltypeAuto()) OS << " decltype(auto)";
  345. if (!T->isDeduced())
  346. OS << " undeduced";
  347. }
  348. void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
  349. if (T->isTypeAlias()) OS << " alias";
  350. OS << " "; T->getTemplateName().dump(OS);
  351. for (auto &Arg : *T)
  352. dumpTemplateArgument(Arg);
  353. if (T->isTypeAlias())
  354. dumpTypeAsChild(T->getAliasedType());
  355. }
  356. void VisitInjectedClassNameType(const InjectedClassNameType *T) {
  357. dumpDeclRef(T->getDecl());
  358. }
  359. void VisitObjCInterfaceType(const ObjCInterfaceType *T) {
  360. dumpDeclRef(T->getDecl());
  361. }
  362. void VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
  363. dumpTypeAsChild(T->getPointeeType());
  364. }
  365. void VisitAtomicType(const AtomicType *T) {
  366. dumpTypeAsChild(T->getValueType());
  367. }
  368. void VisitAdjustedType(const AdjustedType *T) {
  369. dumpTypeAsChild(T->getOriginalType());
  370. }
  371. void VisitPackExpansionType(const PackExpansionType *T) {
  372. if (auto N = T->getNumExpansions()) OS << " expansions " << *N;
  373. if (!T->isSugared())
  374. dumpTypeAsChild(T->getPattern());
  375. }
  376. // FIXME: ElaboratedType, DependentNameType,
  377. // DependentTemplateSpecializationType, ObjCObjectType
  378. // Decls
  379. void VisitLabelDecl(const LabelDecl *D);
  380. void VisitTypedefDecl(const TypedefDecl *D);
  381. void VisitEnumDecl(const EnumDecl *D);
  382. void VisitRecordDecl(const RecordDecl *D);
  383. void VisitEnumConstantDecl(const EnumConstantDecl *D);
  384. void VisitIndirectFieldDecl(const IndirectFieldDecl *D);
  385. void VisitFunctionDecl(const FunctionDecl *D);
  386. void VisitFieldDecl(const FieldDecl *D);
  387. void VisitVarDecl(const VarDecl *D);
  388. void VisitFileScopeAsmDecl(const FileScopeAsmDecl *D);
  389. void VisitImportDecl(const ImportDecl *D);
  390. void VisitHLSLBufferDecl(const HLSLBufferDecl* D); // HLSL Change
  391. // C++ Decls
  392. void VisitNamespaceDecl(const NamespaceDecl *D);
  393. void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D);
  394. void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
  395. void VisitTypeAliasDecl(const TypeAliasDecl *D);
  396. void VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D);
  397. void VisitCXXRecordDecl(const CXXRecordDecl *D);
  398. void VisitStaticAssertDecl(const StaticAssertDecl *D);
  399. template<typename SpecializationDecl>
  400. void VisitTemplateDeclSpecialization(const SpecializationDecl *D,
  401. bool DumpExplicitInst,
  402. bool DumpRefOnly);
  403. template<typename TemplateDecl>
  404. void VisitTemplateDecl(const TemplateDecl *D, bool DumpExplicitInst);
  405. void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
  406. void VisitClassTemplateDecl(const ClassTemplateDecl *D);
  407. void VisitClassTemplateSpecializationDecl(
  408. const ClassTemplateSpecializationDecl *D);
  409. void VisitClassTemplatePartialSpecializationDecl(
  410. const ClassTemplatePartialSpecializationDecl *D);
  411. void VisitClassScopeFunctionSpecializationDecl(
  412. const ClassScopeFunctionSpecializationDecl *D);
  413. void VisitVarTemplateDecl(const VarTemplateDecl *D);
  414. void VisitVarTemplateSpecializationDecl(
  415. const VarTemplateSpecializationDecl *D);
  416. void VisitVarTemplatePartialSpecializationDecl(
  417. const VarTemplatePartialSpecializationDecl *D);
  418. void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
  419. void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
  420. void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
  421. void VisitUsingDecl(const UsingDecl *D);
  422. void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D);
  423. void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D);
  424. void VisitUsingShadowDecl(const UsingShadowDecl *D);
  425. void VisitLinkageSpecDecl(const LinkageSpecDecl *D);
  426. void VisitAccessSpecDecl(const AccessSpecDecl *D);
  427. void VisitFriendDecl(const FriendDecl *D);
  428. // ObjC Decls
  429. void VisitObjCIvarDecl(const ObjCIvarDecl *D);
  430. void VisitObjCMethodDecl(const ObjCMethodDecl *D);
  431. void VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D);
  432. void VisitObjCCategoryDecl(const ObjCCategoryDecl *D);
  433. void VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D);
  434. void VisitObjCProtocolDecl(const ObjCProtocolDecl *D);
  435. void VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D);
  436. void VisitObjCImplementationDecl(const ObjCImplementationDecl *D);
  437. void VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D);
  438. void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
  439. void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
  440. void VisitBlockDecl(const BlockDecl *D);
  441. // Stmts.
  442. void VisitStmt(const Stmt *Node);
  443. void VisitDeclStmt(const DeclStmt *Node);
  444. void VisitAttributedStmt(const AttributedStmt *Node);
  445. void VisitLabelStmt(const LabelStmt *Node);
  446. void VisitGotoStmt(const GotoStmt *Node);
  447. void VisitCXXCatchStmt(const CXXCatchStmt *Node);
  448. // Exprs
  449. void VisitExpr(const Expr *Node);
  450. void VisitCastExpr(const CastExpr *Node);
  451. void VisitDeclRefExpr(const DeclRefExpr *Node);
  452. void VisitPredefinedExpr(const PredefinedExpr *Node);
  453. void VisitCharacterLiteral(const CharacterLiteral *Node);
  454. void VisitIntegerLiteral(const IntegerLiteral *Node);
  455. void VisitFloatingLiteral(const FloatingLiteral *Node);
  456. void VisitStringLiteral(const StringLiteral *Str);
  457. void VisitInitListExpr(const InitListExpr *ILE);
  458. void VisitUnaryOperator(const UnaryOperator *Node);
  459. void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Node);
  460. void VisitMemberExpr(const MemberExpr *Node);
  461. void VisitExtVectorElementExpr(const ExtVectorElementExpr *Node);
  462. void VisitExtMatrixElementExpr(const ExtMatrixElementExpr *Node); // HLSL Change
  463. void VisitHLSLVectorElementExpr(const HLSLVectorElementExpr *Node); // HLSL Change
  464. void VisitBinaryOperator(const BinaryOperator *Node);
  465. void VisitCompoundAssignOperator(const CompoundAssignOperator *Node);
  466. void VisitAddrLabelExpr(const AddrLabelExpr *Node);
  467. void VisitBlockExpr(const BlockExpr *Node);
  468. void VisitOpaqueValueExpr(const OpaqueValueExpr *Node);
  469. // C++
  470. void VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node);
  471. void VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node);
  472. void VisitCXXThisExpr(const CXXThisExpr *Node);
  473. void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node);
  474. void VisitCXXConstructExpr(const CXXConstructExpr *Node);
  475. void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node);
  476. void VisitCXXNewExpr(const CXXNewExpr *Node);
  477. void VisitCXXDeleteExpr(const CXXDeleteExpr *Node);
  478. void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node);
  479. void VisitExprWithCleanups(const ExprWithCleanups *Node);
  480. void VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node);
  481. void dumpCXXTemporary(const CXXTemporary *Temporary);
  482. void VisitLambdaExpr(const LambdaExpr *Node) {
  483. VisitExpr(Node);
  484. dumpDecl(Node->getLambdaClass());
  485. }
  486. void VisitSizeOfPackExpr(const SizeOfPackExpr *Node);
  487. // ObjC
  488. void VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node);
  489. void VisitObjCEncodeExpr(const ObjCEncodeExpr *Node);
  490. void VisitObjCMessageExpr(const ObjCMessageExpr *Node);
  491. void VisitObjCBoxedExpr(const ObjCBoxedExpr *Node);
  492. void VisitObjCSelectorExpr(const ObjCSelectorExpr *Node);
  493. void VisitObjCProtocolExpr(const ObjCProtocolExpr *Node);
  494. void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node);
  495. void VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node);
  496. void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node);
  497. void VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node);
  498. // Comments.
  499. const char *getCommandName(unsigned CommandID);
  500. void dumpComment(const Comment *C);
  501. // Inline comments.
  502. void visitTextComment(const TextComment *C);
  503. void visitInlineCommandComment(const InlineCommandComment *C);
  504. void visitHTMLStartTagComment(const HTMLStartTagComment *C);
  505. void visitHTMLEndTagComment(const HTMLEndTagComment *C);
  506. // Block comments.
  507. void visitBlockCommandComment(const BlockCommandComment *C);
  508. void visitParamCommandComment(const ParamCommandComment *C);
  509. void visitTParamCommandComment(const TParamCommandComment *C);
  510. void visitVerbatimBlockComment(const VerbatimBlockComment *C);
  511. void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
  512. void visitVerbatimLineComment(const VerbatimLineComment *C);
  513. };
  514. }
  515. //===----------------------------------------------------------------------===//
  516. // Utilities
  517. //===----------------------------------------------------------------------===//
  518. void ASTDumper::dumpPointer(const void *Ptr) {
  519. ColorScope Color(*this, AddressColor);
  520. OS << ' ' << Ptr;
  521. }
  522. void ASTDumper::dumpLocation(SourceLocation Loc) {
  523. if (!SM)
  524. return;
  525. ColorScope Color(*this, LocationColor);
  526. SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
  527. // The general format we print out is filename:line:col, but we drop pieces
  528. // that haven't changed since the last loc printed.
  529. PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
  530. if (PLoc.isInvalid()) {
  531. OS << "<invalid sloc>";
  532. return;
  533. }
  534. if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
  535. OS << PLoc.getFilename() << ':' << PLoc.getLine()
  536. << ':' << PLoc.getColumn();
  537. LastLocFilename = PLoc.getFilename();
  538. LastLocLine = PLoc.getLine();
  539. } else if (PLoc.getLine() != LastLocLine) {
  540. OS << "line" << ':' << PLoc.getLine()
  541. << ':' << PLoc.getColumn();
  542. LastLocLine = PLoc.getLine();
  543. } else {
  544. OS << "col" << ':' << PLoc.getColumn();
  545. }
  546. }
  547. void ASTDumper::dumpSourceRange(SourceRange R) {
  548. // Can't translate locations if a SourceManager isn't available.
  549. if (!SM)
  550. return;
  551. OS << " <";
  552. dumpLocation(R.getBegin());
  553. if (R.getBegin() != R.getEnd()) {
  554. OS << ", ";
  555. dumpLocation(R.getEnd());
  556. }
  557. OS << ">";
  558. // <t2.c:123:421[blah], t2.c:412:321>
  559. }
  560. void ASTDumper::dumpBareType(QualType T, bool Desugar) {
  561. ColorScope Color(*this, TypeColor);
  562. SplitQualType T_split = T.split();
  563. OS << "'" << QualType::getAsString(T_split) << "'";
  564. if (Desugar && !T.isNull()) {
  565. // If the type is sugared, also dump a (shallow) desugared type.
  566. SplitQualType D_split = T.getSplitDesugaredType();
  567. if (T_split != D_split)
  568. OS << ":'" << QualType::getAsString(D_split) << "'";
  569. }
  570. }
  571. void ASTDumper::dumpType(QualType T) {
  572. OS << ' ';
  573. dumpBareType(T);
  574. }
  575. void ASTDumper::dumpTypeAsChild(QualType T) {
  576. SplitQualType SQT = T.split();
  577. if (!SQT.Quals.hasQualifiers())
  578. return dumpTypeAsChild(SQT.Ty);
  579. dumpChild([=] {
  580. OS << "QualType";
  581. dumpPointer(T.getAsOpaquePtr());
  582. OS << " ";
  583. dumpBareType(T, false);
  584. OS << " " << T.split().Quals.getAsString();
  585. dumpTypeAsChild(T.split().Ty);
  586. });
  587. }
  588. void ASTDumper::dumpTypeAsChild(const Type *T) {
  589. dumpChild([=] {
  590. if (!T) {
  591. ColorScope Color(*this, NullColor);
  592. OS << "<<<NULL>>>";
  593. return;
  594. }
  595. {
  596. ColorScope Color(*this, TypeColor);
  597. OS << T->getTypeClassName() << "Type";
  598. }
  599. dumpPointer(T);
  600. OS << " ";
  601. dumpBareType(QualType(T, 0), false);
  602. QualType SingleStepDesugar =
  603. T->getLocallyUnqualifiedSingleStepDesugaredType();
  604. if (SingleStepDesugar != QualType(T, 0))
  605. OS << " sugar";
  606. if (T->isDependentType())
  607. OS << " dependent";
  608. else if (T->isInstantiationDependentType())
  609. OS << " instantiation_dependent";
  610. if (T->isVariablyModifiedType())
  611. OS << " variably_modified";
  612. if (T->containsUnexpandedParameterPack())
  613. OS << " contains_unexpanded_pack";
  614. if (T->isFromAST())
  615. OS << " imported";
  616. TypeVisitor<ASTDumper>::Visit(T);
  617. if (SingleStepDesugar != QualType(T, 0))
  618. dumpTypeAsChild(SingleStepDesugar);
  619. });
  620. }
  621. void ASTDumper::dumpBareDeclRef(const Decl *D) {
  622. {
  623. ColorScope Color(*this, DeclKindNameColor);
  624. OS << D->getDeclKindName();
  625. }
  626. dumpPointer(D);
  627. if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
  628. ColorScope Color(*this, DeclNameColor);
  629. OS << " '" << ND->getDeclName() << '\'';
  630. }
  631. if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
  632. dumpType(VD->getType());
  633. }
  634. void ASTDumper::dumpDeclRef(const Decl *D, const char *Label) {
  635. if (!D)
  636. return;
  637. dumpChild([=]{
  638. if (Label)
  639. OS << Label << ' ';
  640. dumpBareDeclRef(D);
  641. });
  642. }
  643. void ASTDumper::dumpName(const NamedDecl *ND) {
  644. if (ND->getDeclName()) {
  645. ColorScope Color(*this, DeclNameColor);
  646. OS << ' ' << ND->getNameAsString();
  647. }
  648. }
  649. bool ASTDumper::hasNodes(const DeclContext *DC) {
  650. if (!DC)
  651. return false;
  652. return DC->hasExternalLexicalStorage() ||
  653. DC->noload_decls_begin() != DC->noload_decls_end();
  654. }
  655. void ASTDumper::dumpDeclContext(const DeclContext *DC) {
  656. if (!DC)
  657. return;
  658. for (auto *D : DC->noload_decls())
  659. dumpDecl(D);
  660. if (DC->hasExternalLexicalStorage()) {
  661. dumpChild([=]{
  662. ColorScope Color(*this, UndeserializedColor);
  663. OS << "<undeserialized declarations>";
  664. });
  665. }
  666. }
  667. void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
  668. dumpChild([=] {
  669. OS << "StoredDeclsMap ";
  670. dumpBareDeclRef(cast<Decl>(DC));
  671. const DeclContext *Primary = DC->getPrimaryContext();
  672. if (Primary != DC) {
  673. OS << " primary";
  674. dumpPointer(cast<Decl>(Primary));
  675. }
  676. bool HasUndeserializedLookups = Primary->hasExternalVisibleStorage();
  677. DeclContext::all_lookups_iterator I = Primary->noload_lookups_begin(),
  678. E = Primary->noload_lookups_end();
  679. while (I != E) {
  680. DeclarationName Name = I.getLookupName();
  681. DeclContextLookupResult R = *I++;
  682. dumpChild([=] {
  683. OS << "DeclarationName ";
  684. {
  685. ColorScope Color(*this, DeclNameColor);
  686. OS << '\'' << Name << '\'';
  687. }
  688. for (DeclContextLookupResult::iterator RI = R.begin(), RE = R.end();
  689. RI != RE; ++RI) {
  690. dumpChild([=] {
  691. dumpBareDeclRef(*RI);
  692. if ((*RI)->isHidden())
  693. OS << " hidden";
  694. // If requested, dump the redecl chain for this lookup.
  695. if (DumpDecls) {
  696. // Dump earliest decl first.
  697. std::function<void(Decl *)> DumpWithPrev = [&](Decl *D) {
  698. if (Decl *Prev = D->getPreviousDecl())
  699. DumpWithPrev(Prev);
  700. dumpDecl(D);
  701. };
  702. DumpWithPrev(*RI);
  703. }
  704. });
  705. }
  706. });
  707. }
  708. if (HasUndeserializedLookups) {
  709. dumpChild([=] {
  710. ColorScope Color(*this, UndeserializedColor);
  711. OS << "<undeserialized lookups>";
  712. });
  713. }
  714. });
  715. }
  716. void ASTDumper::dumpAttr(const Attr *A) {
  717. dumpChild([=] {
  718. {
  719. ColorScope Color(*this, AttrColor);
  720. switch (A->getKind()) {
  721. #define ATTR(X) case attr::X: OS << #X; break;
  722. #include "clang/Basic/AttrList.inc"
  723. default:
  724. llvm_unreachable("unexpected attribute kind");
  725. }
  726. OS << "Attr";
  727. }
  728. dumpPointer(A);
  729. dumpSourceRange(A->getRange());
  730. if (A->isInherited())
  731. OS << " Inherited";
  732. if (A->isImplicit())
  733. OS << " Implicit";
  734. #include "clang/AST/AttrDump.inc"
  735. });
  736. }
  737. static void dumpPreviousDeclImpl(raw_ostream &OS, ...) {}
  738. template<typename T>
  739. static void dumpPreviousDeclImpl(raw_ostream &OS, const Mergeable<T> *D) {
  740. const T *First = D->getFirstDecl();
  741. if (First != D)
  742. OS << " first " << First;
  743. }
  744. template<typename T>
  745. static void dumpPreviousDeclImpl(raw_ostream &OS, const Redeclarable<T> *D) {
  746. const T *Prev = D->getPreviousDecl();
  747. if (Prev)
  748. OS << " prev " << Prev;
  749. }
  750. /// Dump the previous declaration in the redeclaration chain for a declaration,
  751. /// if any.
  752. static void dumpPreviousDecl(raw_ostream &OS, const Decl *D) {
  753. switch (D->getKind()) {
  754. #define DECL(DERIVED, BASE) \
  755. case Decl::DERIVED: \
  756. return dumpPreviousDeclImpl(OS, cast<DERIVED##Decl>(D));
  757. #define ABSTRACT_DECL(DECL)
  758. #include "clang/AST/DeclNodes.inc"
  759. }
  760. llvm_unreachable("Decl that isn't part of DeclNodes.inc!");
  761. }
  762. //===----------------------------------------------------------------------===//
  763. // C++ Utilities
  764. //===----------------------------------------------------------------------===//
  765. void ASTDumper::dumpAccessSpecifier(AccessSpecifier AS) {
  766. switch (AS) {
  767. case AS_none:
  768. break;
  769. case AS_public:
  770. OS << "public";
  771. break;
  772. case AS_protected:
  773. OS << "protected";
  774. break;
  775. case AS_private:
  776. OS << "private";
  777. break;
  778. }
  779. }
  780. void ASTDumper::dumpCXXCtorInitializer(const CXXCtorInitializer *Init) {
  781. dumpChild([=] {
  782. OS << "CXXCtorInitializer";
  783. if (Init->isAnyMemberInitializer()) {
  784. OS << ' ';
  785. dumpBareDeclRef(Init->getAnyMember());
  786. } else if (Init->isBaseInitializer()) {
  787. dumpType(QualType(Init->getBaseClass(), 0));
  788. } else if (Init->isDelegatingInitializer()) {
  789. dumpType(Init->getTypeSourceInfo()->getType());
  790. } else {
  791. llvm_unreachable("Unknown initializer type");
  792. }
  793. dumpStmt(Init->getInit());
  794. });
  795. }
  796. void ASTDumper::dumpTemplateParameters(const TemplateParameterList *TPL) {
  797. if (!TPL)
  798. return;
  799. for (TemplateParameterList::const_iterator I = TPL->begin(), E = TPL->end();
  800. I != E; ++I)
  801. dumpDecl(*I);
  802. }
  803. void ASTDumper::dumpTemplateArgumentListInfo(
  804. const TemplateArgumentListInfo &TALI) {
  805. for (unsigned i = 0, e = TALI.size(); i < e; ++i)
  806. dumpTemplateArgumentLoc(TALI[i]);
  807. }
  808. void ASTDumper::dumpTemplateArgumentLoc(const TemplateArgumentLoc &A) {
  809. dumpTemplateArgument(A.getArgument(), A.getSourceRange());
  810. }
  811. void ASTDumper::dumpTemplateArgumentList(const TemplateArgumentList &TAL) {
  812. for (unsigned i = 0, e = TAL.size(); i < e; ++i)
  813. dumpTemplateArgument(TAL[i]);
  814. }
  815. void ASTDumper::dumpTemplateArgument(const TemplateArgument &A, SourceRange R) {
  816. dumpChild([=] {
  817. OS << "TemplateArgument";
  818. if (R.isValid())
  819. dumpSourceRange(R);
  820. switch (A.getKind()) {
  821. case TemplateArgument::Null:
  822. OS << " null";
  823. break;
  824. case TemplateArgument::Type:
  825. OS << " type";
  826. dumpType(A.getAsType());
  827. break;
  828. case TemplateArgument::Declaration:
  829. OS << " decl";
  830. dumpDeclRef(A.getAsDecl());
  831. break;
  832. case TemplateArgument::NullPtr:
  833. OS << " nullptr";
  834. break;
  835. case TemplateArgument::Integral:
  836. OS << " integral " << A.getAsIntegral();
  837. break;
  838. case TemplateArgument::Template:
  839. OS << " template ";
  840. A.getAsTemplate().dump(OS);
  841. break;
  842. case TemplateArgument::TemplateExpansion:
  843. OS << " template expansion";
  844. A.getAsTemplateOrTemplatePattern().dump(OS);
  845. break;
  846. case TemplateArgument::Expression:
  847. OS << " expr";
  848. dumpStmt(A.getAsExpr());
  849. break;
  850. case TemplateArgument::Pack:
  851. OS << " pack";
  852. for (TemplateArgument::pack_iterator I = A.pack_begin(), E = A.pack_end();
  853. I != E; ++I)
  854. dumpTemplateArgument(*I);
  855. break;
  856. }
  857. });
  858. }
  859. //===----------------------------------------------------------------------===//
  860. // Objective-C Utilities
  861. //===----------------------------------------------------------------------===//
  862. void ASTDumper::dumpObjCTypeParamList(const ObjCTypeParamList *typeParams) {
  863. if (!typeParams)
  864. return;
  865. for (auto typeParam : *typeParams) {
  866. dumpDecl(typeParam);
  867. }
  868. }
  869. //===----------------------------------------------------------------------===//
  870. // Decl dumping methods.
  871. //===----------------------------------------------------------------------===//
  872. // HLSL Change Begins
  873. void ASTDumper::dumpHLSLUnusualAnnotations(const ArrayRef<hlsl::UnusualAnnotation*> UA)
  874. {
  875. for (auto It = UA.begin(), E = UA.end(); It != E; ++It)
  876. {
  877. dumpChild([=] {
  878. {
  879. ColorScope Color(*this, AttrColor);
  880. switch ((*It)->getKind())
  881. {
  882. case hlsl::UnusualAnnotation::UA_ConstantPacking:
  883. OS << "ConstantPacking"; break;
  884. case hlsl::UnusualAnnotation::UA_RegisterAssignment:
  885. OS << "RegisterAssignment"; break;
  886. case hlsl::UnusualAnnotation::UA_SemanticDecl:
  887. OS << "SemanticDecl"; break;
  888. }
  889. }
  890. dumpPointer(It);
  891. dumpSourceRange(SourceRange((*It)->Loc));
  892. switch ((*It)->getKind())
  893. {
  894. case hlsl::UnusualAnnotation::UA_ConstantPacking: {
  895. const hlsl::ConstantPacking* constantPacking = cast<hlsl::ConstantPacking>(*It);
  896. OS << " packoffset(c";
  897. OS << constantPacking->Subcomponent;
  898. OS << ".";
  899. const char *xyzw[4] = { "x", "y", "z", "w" };
  900. if(constantPacking->ComponentOffset < 4)
  901. OS << xyzw[constantPacking->ComponentOffset];
  902. else
  903. OS << "<invalid>";
  904. OS << ")";
  905. if (!constantPacking->IsValid)
  906. OS << " invalid";
  907. break;
  908. }
  909. case hlsl::UnusualAnnotation::UA_RegisterAssignment: {
  910. const hlsl::RegisterAssignment* registerAssignment = cast<hlsl::RegisterAssignment>(*It);
  911. OS << " register(";
  912. if (!registerAssignment->ShaderProfile.empty())
  913. OS << registerAssignment->ShaderProfile << ", ";
  914. if (!registerAssignment->RegisterType)
  915. OS << "invalid";
  916. else
  917. OS << std::string(&(registerAssignment->RegisterType), 1);
  918. OS << registerAssignment->RegisterNumber + registerAssignment->RegisterOffset;
  919. if (registerAssignment->RegisterSpace)
  920. OS << ", space" << registerAssignment->RegisterSpace;
  921. OS << ")";
  922. if (!registerAssignment->IsValid)
  923. OS << " invalid";
  924. break;
  925. }
  926. case hlsl::UnusualAnnotation::UA_SemanticDecl: {
  927. const hlsl::SemanticDecl* semanticDecl = cast<hlsl::SemanticDecl>(*It);
  928. OS << " \"" << semanticDecl->SemanticName << "\"";
  929. break;
  930. }
  931. }
  932. });
  933. }
  934. }
  935. // HLSL Change Ends
  936. void ASTDumper::dumpDecl(const Decl *D) {
  937. // HLSL Change Starts: Don't display decls with invalid SourceLocations.
  938. if (D && D->getDeclContext() &&
  939. D->getDeclContext()->getDeclKind() == Decl::Kind::TranslationUnit &&
  940. D->getSourceRange().isInvalid())
  941. {
  942. return;
  943. }
  944. // HLSL Change Ends
  945. dumpChild([=] {
  946. if (!D) {
  947. ColorScope Color(*this, NullColor);
  948. OS << "<<<NULL>>>";
  949. return;
  950. }
  951. {
  952. ColorScope Color(*this, DeclKindNameColor);
  953. OS << D->getDeclKindName() << "Decl";
  954. }
  955. dumpPointer(D);
  956. if (D->getLexicalDeclContext() != D->getDeclContext())
  957. OS << " parent " << cast<Decl>(D->getDeclContext());
  958. // HLSL Change Begin - dump HLSLBufferDecl
  959. if (const HLSLBufferDecl *bufDecl =
  960. dyn_cast_or_null<HLSLBufferDecl>(D->getDeclContext())) {
  961. OS << " parent " << bufDecl->getDeclKindName();
  962. dumpPointer(bufDecl);
  963. }
  964. // HLSL Change End
  965. dumpPreviousDecl(OS, D);
  966. dumpSourceRange(D->getSourceRange());
  967. OS << ' ';
  968. dumpLocation(D->getLocation());
  969. if (Module *M = D->getImportedOwningModule())
  970. OS << " in " << M->getFullModuleName();
  971. else if (Module *M = D->getLocalOwningModule())
  972. OS << " in (local) " << M->getFullModuleName();
  973. if (auto *ND = dyn_cast<NamedDecl>(D))
  974. for (Module *M : D->getASTContext().getModulesWithMergedDefinition(
  975. const_cast<NamedDecl *>(ND)))
  976. dumpChild([=] { OS << "also in " << M->getFullModuleName(); });
  977. if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
  978. if (ND->isHidden())
  979. OS << " hidden";
  980. if (D->isImplicit())
  981. OS << " implicit";
  982. if (D->isUsed())
  983. OS << " used";
  984. else if (D->isThisDeclarationReferenced())
  985. OS << " referenced";
  986. if (D->isInvalidDecl())
  987. OS << " invalid";
  988. if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
  989. if (FD->isConstexpr())
  990. OS << " constexpr";
  991. ConstDeclVisitor<ASTDumper>::Visit(D);
  992. for (Decl::attr_iterator I = D->attr_begin(), E = D->attr_end(); I != E;
  993. ++I)
  994. dumpAttr(*I);
  995. // HLSL Change Begins
  996. if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
  997. dumpHLSLUnusualAnnotations(const_cast<NamedDecl*>(ND)->getUnusualAnnotations());
  998. // HLSL Change Ends
  999. if (const FullComment *Comment =
  1000. D->getASTContext().getLocalCommentForDeclUncached(D))
  1001. dumpFullComment(Comment);
  1002. // Decls within functions are visited by the body.
  1003. if (!isa<FunctionDecl>(*D) && !isa<ObjCMethodDecl>(*D) &&
  1004. hasNodes(dyn_cast<DeclContext>(D)))
  1005. dumpDeclContext(cast<DeclContext>(D));
  1006. });
  1007. }
  1008. void ASTDumper::VisitLabelDecl(const LabelDecl *D) {
  1009. dumpName(D);
  1010. }
  1011. void ASTDumper::VisitTypedefDecl(const TypedefDecl *D) {
  1012. dumpName(D);
  1013. dumpType(D->getUnderlyingType());
  1014. if (D->isModulePrivate())
  1015. OS << " __module_private__";
  1016. }
  1017. // HLSL Change Starts
  1018. void ASTDumper::VisitHLSLBufferDecl(const HLSLBufferDecl* D) {
  1019. if (D->isCBuffer())
  1020. OS << " cbuffer";
  1021. else
  1022. OS << " tbuffer";
  1023. dumpName(D);
  1024. }
  1025. // HLSL Change Ends
  1026. void ASTDumper::VisitEnumDecl(const EnumDecl *D) {
  1027. if (D->isScoped()) {
  1028. if (D->isScopedUsingClassTag())
  1029. OS << " class";
  1030. else
  1031. OS << " struct";
  1032. }
  1033. dumpName(D);
  1034. if (D->isModulePrivate())
  1035. OS << " __module_private__";
  1036. if (D->isFixed())
  1037. dumpType(D->getIntegerType());
  1038. }
  1039. void ASTDumper::VisitRecordDecl(const RecordDecl *D) {
  1040. OS << ' ' << D->getKindName();
  1041. dumpName(D);
  1042. if (D->isModulePrivate())
  1043. OS << " __module_private__";
  1044. if (D->isCompleteDefinition())
  1045. OS << " definition";
  1046. }
  1047. void ASTDumper::VisitEnumConstantDecl(const EnumConstantDecl *D) {
  1048. dumpName(D);
  1049. dumpType(D->getType());
  1050. if (const Expr *Init = D->getInitExpr())
  1051. dumpStmt(Init);
  1052. }
  1053. void ASTDumper::VisitIndirectFieldDecl(const IndirectFieldDecl *D) {
  1054. dumpName(D);
  1055. dumpType(D->getType());
  1056. for (auto *Child : D->chain())
  1057. dumpDeclRef(Child);
  1058. }
  1059. void ASTDumper::VisitFunctionDecl(const FunctionDecl *D) {
  1060. dumpName(D);
  1061. dumpType(D->getType());
  1062. StorageClass SC = D->getStorageClass();
  1063. if (SC != SC_None)
  1064. OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
  1065. if (D->isInlineSpecified())
  1066. OS << " inline";
  1067. if (D->isVirtualAsWritten())
  1068. OS << " virtual";
  1069. if (D->isModulePrivate())
  1070. OS << " __module_private__";
  1071. if (D->isPure())
  1072. OS << " pure";
  1073. else if (D->isDeletedAsWritten())
  1074. OS << " delete";
  1075. if (const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>()) {
  1076. FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
  1077. switch (EPI.ExceptionSpec.Type) {
  1078. default: break;
  1079. case EST_Unevaluated:
  1080. OS << " noexcept-unevaluated " << EPI.ExceptionSpec.SourceDecl;
  1081. break;
  1082. case EST_Uninstantiated:
  1083. OS << " noexcept-uninstantiated " << EPI.ExceptionSpec.SourceTemplate;
  1084. break;
  1085. }
  1086. }
  1087. if (const FunctionTemplateSpecializationInfo *FTSI =
  1088. D->getTemplateSpecializationInfo())
  1089. dumpTemplateArgumentList(*FTSI->TemplateArguments);
  1090. for (ArrayRef<NamedDecl *>::iterator
  1091. I = D->getDeclsInPrototypeScope().begin(),
  1092. E = D->getDeclsInPrototypeScope().end(); I != E; ++I)
  1093. dumpDecl(*I);
  1094. if (!D->param_begin() && D->getNumParams())
  1095. dumpChild([=] { OS << "<<NULL params x " << D->getNumParams() << ">>"; });
  1096. else
  1097. for (FunctionDecl::param_const_iterator I = D->param_begin(),
  1098. E = D->param_end();
  1099. I != E; ++I)
  1100. dumpDecl(*I);
  1101. if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(D))
  1102. for (CXXConstructorDecl::init_const_iterator I = C->init_begin(),
  1103. E = C->init_end();
  1104. I != E; ++I)
  1105. dumpCXXCtorInitializer(*I);
  1106. if (D->doesThisDeclarationHaveABody())
  1107. dumpStmt(D->getBody());
  1108. }
  1109. void ASTDumper::VisitFieldDecl(const FieldDecl *D) {
  1110. dumpName(D);
  1111. dumpType(D->getType());
  1112. if (D->isMutable())
  1113. OS << " mutable";
  1114. if (D->isModulePrivate())
  1115. OS << " __module_private__";
  1116. if (D->isBitField())
  1117. dumpStmt(D->getBitWidth());
  1118. if (Expr *Init = D->getInClassInitializer())
  1119. dumpStmt(Init);
  1120. }
  1121. void ASTDumper::VisitVarDecl(const VarDecl *D) {
  1122. dumpName(D);
  1123. dumpType(D->getType());
  1124. StorageClass SC = D->getStorageClass();
  1125. if (SC != SC_None)
  1126. OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
  1127. switch (D->getTLSKind()) {
  1128. case VarDecl::TLS_None: break;
  1129. case VarDecl::TLS_Static: OS << " tls"; break;
  1130. case VarDecl::TLS_Dynamic: OS << " tls_dynamic"; break;
  1131. }
  1132. if (D->isModulePrivate())
  1133. OS << " __module_private__";
  1134. if (D->isNRVOVariable())
  1135. OS << " nrvo";
  1136. if (D->hasInit()) {
  1137. switch (D->getInitStyle()) {
  1138. case VarDecl::CInit: OS << " cinit"; break;
  1139. case VarDecl::CallInit: OS << " callinit"; break;
  1140. case VarDecl::ListInit: OS << " listinit"; break;
  1141. }
  1142. dumpStmt(D->getInit());
  1143. }
  1144. }
  1145. void ASTDumper::VisitFileScopeAsmDecl(const FileScopeAsmDecl *D) {
  1146. dumpStmt(D->getAsmString());
  1147. }
  1148. void ASTDumper::VisitImportDecl(const ImportDecl *D) {
  1149. OS << ' ' << D->getImportedModule()->getFullModuleName();
  1150. }
  1151. //===----------------------------------------------------------------------===//
  1152. // C++ Declarations
  1153. //===----------------------------------------------------------------------===//
  1154. void ASTDumper::VisitNamespaceDecl(const NamespaceDecl *D) {
  1155. dumpName(D);
  1156. if (D->isInline())
  1157. OS << " inline";
  1158. if (!D->isOriginalNamespace())
  1159. dumpDeclRef(D->getOriginalNamespace(), "original");
  1160. }
  1161. void ASTDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
  1162. OS << ' ';
  1163. dumpBareDeclRef(D->getNominatedNamespace());
  1164. }
  1165. void ASTDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
  1166. dumpName(D);
  1167. dumpDeclRef(D->getAliasedNamespace());
  1168. }
  1169. void ASTDumper::VisitTypeAliasDecl(const TypeAliasDecl *D) {
  1170. dumpName(D);
  1171. dumpType(D->getUnderlyingType());
  1172. }
  1173. void ASTDumper::VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D) {
  1174. dumpName(D);
  1175. dumpTemplateParameters(D->getTemplateParameters());
  1176. dumpDecl(D->getTemplatedDecl());
  1177. }
  1178. void ASTDumper::VisitCXXRecordDecl(const CXXRecordDecl *D) {
  1179. VisitRecordDecl(D);
  1180. if (!D->isCompleteDefinition())
  1181. return;
  1182. for (const auto &I : D->bases()) {
  1183. dumpChild([=] {
  1184. if (I.isVirtual())
  1185. OS << "virtual ";
  1186. dumpAccessSpecifier(I.getAccessSpecifier());
  1187. dumpType(I.getType());
  1188. if (I.isPackExpansion())
  1189. OS << "...";
  1190. });
  1191. }
  1192. }
  1193. void ASTDumper::VisitStaticAssertDecl(const StaticAssertDecl *D) {
  1194. dumpStmt(D->getAssertExpr());
  1195. dumpStmt(D->getMessage());
  1196. }
  1197. template<typename SpecializationDecl>
  1198. void ASTDumper::VisitTemplateDeclSpecialization(const SpecializationDecl *D,
  1199. bool DumpExplicitInst,
  1200. bool DumpRefOnly) {
  1201. bool DumpedAny = false;
  1202. for (auto *RedeclWithBadType : D->redecls()) {
  1203. // FIXME: The redecls() range sometimes has elements of a less-specific
  1204. // type. (In particular, ClassTemplateSpecializationDecl::redecls() gives
  1205. // us TagDecls, and should give CXXRecordDecls).
  1206. auto *Redecl = dyn_cast<SpecializationDecl>(RedeclWithBadType);
  1207. if (!Redecl) {
  1208. // Found the injected-class-name for a class template. This will be dumped
  1209. // as part of its surrounding class so we don't need to dump it here.
  1210. assert(isa<CXXRecordDecl>(RedeclWithBadType) &&
  1211. "expected an injected-class-name");
  1212. continue;
  1213. }
  1214. switch (Redecl->getTemplateSpecializationKind()) {
  1215. case TSK_ExplicitInstantiationDeclaration:
  1216. case TSK_ExplicitInstantiationDefinition:
  1217. if (!DumpExplicitInst)
  1218. break;
  1219. // Fall through.
  1220. case TSK_Undeclared:
  1221. case TSK_ImplicitInstantiation:
  1222. if (DumpRefOnly)
  1223. dumpDeclRef(Redecl);
  1224. else
  1225. dumpDecl(Redecl);
  1226. DumpedAny = true;
  1227. break;
  1228. case TSK_ExplicitSpecialization:
  1229. break;
  1230. }
  1231. }
  1232. // Ensure we dump at least one decl for each specialization.
  1233. if (!DumpedAny)
  1234. dumpDeclRef(D);
  1235. }
  1236. template<typename TemplateDecl>
  1237. void ASTDumper::VisitTemplateDecl(const TemplateDecl *D,
  1238. bool DumpExplicitInst) {
  1239. dumpName(D);
  1240. dumpTemplateParameters(D->getTemplateParameters());
  1241. dumpDecl(D->getTemplatedDecl());
  1242. for (auto *Child : D->specializations())
  1243. VisitTemplateDeclSpecialization(Child, DumpExplicitInst,
  1244. !D->isCanonicalDecl());
  1245. }
  1246. void ASTDumper::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
  1247. // FIXME: We don't add a declaration of a function template specialization
  1248. // to its context when it's explicitly instantiated, so dump explicit
  1249. // instantiations when we dump the template itself.
  1250. VisitTemplateDecl(D, true);
  1251. }
  1252. void ASTDumper::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
  1253. VisitTemplateDecl(D, false);
  1254. }
  1255. void ASTDumper::VisitClassTemplateSpecializationDecl(
  1256. const ClassTemplateSpecializationDecl *D) {
  1257. VisitCXXRecordDecl(D);
  1258. dumpTemplateArgumentList(D->getTemplateArgs());
  1259. }
  1260. void ASTDumper::VisitClassTemplatePartialSpecializationDecl(
  1261. const ClassTemplatePartialSpecializationDecl *D) {
  1262. VisitClassTemplateSpecializationDecl(D);
  1263. dumpTemplateParameters(D->getTemplateParameters());
  1264. }
  1265. void ASTDumper::VisitClassScopeFunctionSpecializationDecl(
  1266. const ClassScopeFunctionSpecializationDecl *D) {
  1267. dumpDeclRef(D->getSpecialization());
  1268. if (D->hasExplicitTemplateArgs())
  1269. dumpTemplateArgumentListInfo(D->templateArgs());
  1270. }
  1271. void ASTDumper::VisitVarTemplateDecl(const VarTemplateDecl *D) {
  1272. VisitTemplateDecl(D, false);
  1273. }
  1274. void ASTDumper::VisitVarTemplateSpecializationDecl(
  1275. const VarTemplateSpecializationDecl *D) {
  1276. dumpTemplateArgumentList(D->getTemplateArgs());
  1277. VisitVarDecl(D);
  1278. }
  1279. void ASTDumper::VisitVarTemplatePartialSpecializationDecl(
  1280. const VarTemplatePartialSpecializationDecl *D) {
  1281. dumpTemplateParameters(D->getTemplateParameters());
  1282. VisitVarTemplateSpecializationDecl(D);
  1283. }
  1284. void ASTDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
  1285. if (D->wasDeclaredWithTypename())
  1286. OS << " typename";
  1287. else
  1288. OS << " class";
  1289. if (D->isParameterPack())
  1290. OS << " ...";
  1291. dumpName(D);
  1292. if (D->hasDefaultArgument())
  1293. dumpTemplateArgument(D->getDefaultArgument());
  1294. }
  1295. void ASTDumper::VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
  1296. dumpType(D->getType());
  1297. if (D->isParameterPack())
  1298. OS << " ...";
  1299. dumpName(D);
  1300. if (D->hasDefaultArgument())
  1301. dumpTemplateArgument(D->getDefaultArgument());
  1302. }
  1303. void ASTDumper::VisitTemplateTemplateParmDecl(
  1304. const TemplateTemplateParmDecl *D) {
  1305. if (D->isParameterPack())
  1306. OS << " ...";
  1307. dumpName(D);
  1308. dumpTemplateParameters(D->getTemplateParameters());
  1309. if (D->hasDefaultArgument())
  1310. dumpTemplateArgumentLoc(D->getDefaultArgument());
  1311. }
  1312. void ASTDumper::VisitUsingDecl(const UsingDecl *D) {
  1313. OS << ' ';
  1314. D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
  1315. OS << D->getNameAsString();
  1316. }
  1317. void ASTDumper::VisitUnresolvedUsingTypenameDecl(
  1318. const UnresolvedUsingTypenameDecl *D) {
  1319. OS << ' ';
  1320. D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
  1321. OS << D->getNameAsString();
  1322. }
  1323. void ASTDumper::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
  1324. OS << ' ';
  1325. D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
  1326. OS << D->getNameAsString();
  1327. dumpType(D->getType());
  1328. }
  1329. void ASTDumper::VisitUsingShadowDecl(const UsingShadowDecl *D) {
  1330. OS << ' ';
  1331. dumpBareDeclRef(D->getTargetDecl());
  1332. }
  1333. void ASTDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
  1334. switch (D->getLanguage()) {
  1335. case LinkageSpecDecl::lang_c: OS << " C"; break;
  1336. case LinkageSpecDecl::lang_cxx: OS << " C++"; break;
  1337. }
  1338. }
  1339. void ASTDumper::VisitAccessSpecDecl(const AccessSpecDecl *D) {
  1340. OS << ' ';
  1341. dumpAccessSpecifier(D->getAccess());
  1342. }
  1343. void ASTDumper::VisitFriendDecl(const FriendDecl *D) {
  1344. if (TypeSourceInfo *T = D->getFriendType())
  1345. dumpType(T->getType());
  1346. else
  1347. dumpDecl(D->getFriendDecl());
  1348. }
  1349. //===----------------------------------------------------------------------===//
  1350. // Obj-C Declarations
  1351. //===----------------------------------------------------------------------===//
  1352. void ASTDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) {
  1353. dumpName(D);
  1354. dumpType(D->getType());
  1355. if (D->getSynthesize())
  1356. OS << " synthesize";
  1357. switch (D->getAccessControl()) {
  1358. case ObjCIvarDecl::None:
  1359. OS << " none";
  1360. break;
  1361. case ObjCIvarDecl::Private:
  1362. OS << " private";
  1363. break;
  1364. case ObjCIvarDecl::Protected:
  1365. OS << " protected";
  1366. break;
  1367. case ObjCIvarDecl::Public:
  1368. OS << " public";
  1369. break;
  1370. case ObjCIvarDecl::Package:
  1371. OS << " package";
  1372. break;
  1373. }
  1374. }
  1375. void ASTDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
  1376. if (D->isInstanceMethod())
  1377. OS << " -";
  1378. else
  1379. OS << " +";
  1380. dumpName(D);
  1381. dumpType(D->getReturnType());
  1382. if (D->isThisDeclarationADefinition()) {
  1383. dumpDeclContext(D);
  1384. } else {
  1385. for (ObjCMethodDecl::param_const_iterator I = D->param_begin(),
  1386. E = D->param_end();
  1387. I != E; ++I)
  1388. dumpDecl(*I);
  1389. }
  1390. if (D->isVariadic())
  1391. dumpChild([=] { OS << "..."; });
  1392. if (D->hasBody())
  1393. dumpStmt(D->getBody());
  1394. }
  1395. void ASTDumper::VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D) {
  1396. dumpName(D);
  1397. switch (D->getVariance()) {
  1398. case ObjCTypeParamVariance::Invariant:
  1399. break;
  1400. case ObjCTypeParamVariance::Covariant:
  1401. OS << " covariant";
  1402. break;
  1403. case ObjCTypeParamVariance::Contravariant:
  1404. OS << " contravariant";
  1405. break;
  1406. }
  1407. if (D->hasExplicitBound())
  1408. OS << " bounded";
  1409. dumpType(D->getUnderlyingType());
  1410. }
  1411. void ASTDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
  1412. dumpName(D);
  1413. dumpDeclRef(D->getClassInterface());
  1414. dumpObjCTypeParamList(D->getTypeParamList());
  1415. dumpDeclRef(D->getImplementation());
  1416. for (ObjCCategoryDecl::protocol_iterator I = D->protocol_begin(),
  1417. E = D->protocol_end();
  1418. I != E; ++I)
  1419. dumpDeclRef(*I);
  1420. }
  1421. void ASTDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
  1422. dumpName(D);
  1423. dumpDeclRef(D->getClassInterface());
  1424. dumpDeclRef(D->getCategoryDecl());
  1425. }
  1426. void ASTDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
  1427. dumpName(D);
  1428. for (auto *Child : D->protocols())
  1429. dumpDeclRef(Child);
  1430. }
  1431. void ASTDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
  1432. dumpName(D);
  1433. dumpObjCTypeParamList(D->getTypeParamListAsWritten());
  1434. dumpDeclRef(D->getSuperClass(), "super");
  1435. dumpDeclRef(D->getImplementation());
  1436. for (auto *Child : D->protocols())
  1437. dumpDeclRef(Child);
  1438. }
  1439. void ASTDumper::VisitObjCImplementationDecl(const ObjCImplementationDecl *D) {
  1440. dumpName(D);
  1441. dumpDeclRef(D->getSuperClass(), "super");
  1442. dumpDeclRef(D->getClassInterface());
  1443. for (ObjCImplementationDecl::init_const_iterator I = D->init_begin(),
  1444. E = D->init_end();
  1445. I != E; ++I)
  1446. dumpCXXCtorInitializer(*I);
  1447. }
  1448. void ASTDumper::VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D) {
  1449. dumpName(D);
  1450. dumpDeclRef(D->getClassInterface());
  1451. }
  1452. void ASTDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
  1453. dumpName(D);
  1454. dumpType(D->getType());
  1455. if (D->getPropertyImplementation() == ObjCPropertyDecl::Required)
  1456. OS << " required";
  1457. else if (D->getPropertyImplementation() == ObjCPropertyDecl::Optional)
  1458. OS << " optional";
  1459. ObjCPropertyDecl::PropertyAttributeKind Attrs = D->getPropertyAttributes();
  1460. if (Attrs != ObjCPropertyDecl::OBJC_PR_noattr) {
  1461. if (Attrs & ObjCPropertyDecl::OBJC_PR_readonly)
  1462. OS << " readonly";
  1463. if (Attrs & ObjCPropertyDecl::OBJC_PR_assign)
  1464. OS << " assign";
  1465. if (Attrs & ObjCPropertyDecl::OBJC_PR_readwrite)
  1466. OS << " readwrite";
  1467. if (Attrs & ObjCPropertyDecl::OBJC_PR_retain)
  1468. OS << " retain";
  1469. if (Attrs & ObjCPropertyDecl::OBJC_PR_copy)
  1470. OS << " copy";
  1471. if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic)
  1472. OS << " nonatomic";
  1473. if (Attrs & ObjCPropertyDecl::OBJC_PR_atomic)
  1474. OS << " atomic";
  1475. if (Attrs & ObjCPropertyDecl::OBJC_PR_weak)
  1476. OS << " weak";
  1477. if (Attrs & ObjCPropertyDecl::OBJC_PR_strong)
  1478. OS << " strong";
  1479. if (Attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained)
  1480. OS << " unsafe_unretained";
  1481. if (Attrs & ObjCPropertyDecl::OBJC_PR_getter)
  1482. dumpDeclRef(D->getGetterMethodDecl(), "getter");
  1483. if (Attrs & ObjCPropertyDecl::OBJC_PR_setter)
  1484. dumpDeclRef(D->getSetterMethodDecl(), "setter");
  1485. }
  1486. }
  1487. void ASTDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
  1488. dumpName(D->getPropertyDecl());
  1489. if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
  1490. OS << " synthesize";
  1491. else
  1492. OS << " dynamic";
  1493. dumpDeclRef(D->getPropertyDecl());
  1494. dumpDeclRef(D->getPropertyIvarDecl());
  1495. }
  1496. void ASTDumper::VisitBlockDecl(const BlockDecl *D) {
  1497. for (auto I : D->params())
  1498. dumpDecl(I);
  1499. if (D->isVariadic())
  1500. dumpChild([=]{ OS << "..."; });
  1501. if (D->capturesCXXThis())
  1502. dumpChild([=]{ OS << "capture this"; });
  1503. for (const auto &I : D->captures()) {
  1504. dumpChild([=] {
  1505. OS << "capture";
  1506. if (I.isByRef())
  1507. OS << " byref";
  1508. if (I.isNested())
  1509. OS << " nested";
  1510. if (I.getVariable()) {
  1511. OS << ' ';
  1512. dumpBareDeclRef(I.getVariable());
  1513. }
  1514. if (I.hasCopyExpr())
  1515. dumpStmt(I.getCopyExpr());
  1516. });
  1517. }
  1518. dumpStmt(D->getBody());
  1519. }
  1520. //===----------------------------------------------------------------------===//
  1521. // Stmt dumping methods.
  1522. //===----------------------------------------------------------------------===//
  1523. void ASTDumper::dumpStmt(const Stmt *S) {
  1524. dumpChild([=] {
  1525. if (!S) {
  1526. ColorScope Color(*this, NullColor);
  1527. OS << "<<<NULL>>>";
  1528. return;
  1529. }
  1530. if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
  1531. VisitDeclStmt(DS);
  1532. return;
  1533. }
  1534. ConstStmtVisitor<ASTDumper>::Visit(S);
  1535. for (const Stmt *SubStmt : S->children())
  1536. dumpStmt(SubStmt);
  1537. });
  1538. }
  1539. void ASTDumper::VisitStmt(const Stmt *Node) {
  1540. {
  1541. ColorScope Color(*this, StmtColor);
  1542. OS << Node->getStmtClassName();
  1543. }
  1544. dumpPointer(Node);
  1545. dumpSourceRange(Node->getSourceRange());
  1546. }
  1547. void ASTDumper::VisitDeclStmt(const DeclStmt *Node) {
  1548. VisitStmt(Node);
  1549. for (DeclStmt::const_decl_iterator I = Node->decl_begin(),
  1550. E = Node->decl_end();
  1551. I != E; ++I)
  1552. dumpDecl(*I);
  1553. }
  1554. void ASTDumper::VisitAttributedStmt(const AttributedStmt *Node) {
  1555. VisitStmt(Node);
  1556. for (ArrayRef<const Attr *>::iterator I = Node->getAttrs().begin(),
  1557. E = Node->getAttrs().end();
  1558. I != E; ++I)
  1559. dumpAttr(*I);
  1560. }
  1561. void ASTDumper::VisitLabelStmt(const LabelStmt *Node) {
  1562. VisitStmt(Node);
  1563. OS << " '" << Node->getName() << "'";
  1564. }
  1565. void ASTDumper::VisitGotoStmt(const GotoStmt *Node) {
  1566. VisitStmt(Node);
  1567. OS << " '" << Node->getLabel()->getName() << "'";
  1568. dumpPointer(Node->getLabel());
  1569. }
  1570. void ASTDumper::VisitCXXCatchStmt(const CXXCatchStmt *Node) {
  1571. VisitStmt(Node);
  1572. dumpDecl(Node->getExceptionDecl());
  1573. }
  1574. //===----------------------------------------------------------------------===//
  1575. // Expr dumping methods.
  1576. //===----------------------------------------------------------------------===//
  1577. void ASTDumper::VisitExpr(const Expr *Node) {
  1578. VisitStmt(Node);
  1579. dumpType(Node->getType());
  1580. {
  1581. ColorScope Color(*this, ValueKindColor);
  1582. switch (Node->getValueKind()) {
  1583. case VK_RValue:
  1584. break;
  1585. case VK_LValue:
  1586. OS << " lvalue";
  1587. break;
  1588. case VK_XValue:
  1589. OS << " xvalue";
  1590. break;
  1591. }
  1592. }
  1593. {
  1594. ColorScope Color(*this, ObjectKindColor);
  1595. switch (Node->getObjectKind()) {
  1596. case OK_Ordinary:
  1597. break;
  1598. case OK_BitField:
  1599. OS << " bitfield";
  1600. break;
  1601. case OK_ObjCProperty:
  1602. OS << " objcproperty";
  1603. break;
  1604. case OK_ObjCSubscript:
  1605. OS << " objcsubscript";
  1606. break;
  1607. case OK_VectorComponent:
  1608. OS << " vectorcomponent";
  1609. break;
  1610. }
  1611. }
  1612. }
  1613. static void dumpBasePath(raw_ostream &OS, const CastExpr *Node) {
  1614. if (Node->path_empty())
  1615. return;
  1616. OS << " (";
  1617. bool First = true;
  1618. for (CastExpr::path_const_iterator I = Node->path_begin(),
  1619. E = Node->path_end();
  1620. I != E; ++I) {
  1621. const CXXBaseSpecifier *Base = *I;
  1622. if (!First)
  1623. OS << " -> ";
  1624. const CXXRecordDecl *RD =
  1625. cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
  1626. if (Base->isVirtual())
  1627. OS << "virtual ";
  1628. OS << RD->getName();
  1629. First = false;
  1630. }
  1631. OS << ')';
  1632. }
  1633. void ASTDumper::VisitCastExpr(const CastExpr *Node) {
  1634. VisitExpr(Node);
  1635. OS << " <";
  1636. {
  1637. ColorScope Color(*this, CastColor);
  1638. OS << Node->getCastKindName();
  1639. }
  1640. dumpBasePath(OS, Node);
  1641. OS << ">";
  1642. }
  1643. void ASTDumper::VisitDeclRefExpr(const DeclRefExpr *Node) {
  1644. VisitExpr(Node);
  1645. OS << " ";
  1646. dumpBareDeclRef(Node->getDecl());
  1647. if (Node->getDecl() != Node->getFoundDecl()) {
  1648. OS << " (";
  1649. dumpBareDeclRef(Node->getFoundDecl());
  1650. OS << ")";
  1651. }
  1652. }
  1653. void ASTDumper::VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node) {
  1654. VisitExpr(Node);
  1655. OS << " (";
  1656. if (!Node->requiresADL())
  1657. OS << "no ";
  1658. OS << "ADL) = '" << Node->getName() << '\'';
  1659. UnresolvedLookupExpr::decls_iterator
  1660. I = Node->decls_begin(), E = Node->decls_end();
  1661. if (I == E)
  1662. OS << " empty";
  1663. for (; I != E; ++I)
  1664. dumpPointer(*I);
  1665. }
  1666. void ASTDumper::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node) {
  1667. VisitExpr(Node);
  1668. {
  1669. ColorScope Color(*this, DeclKindNameColor);
  1670. OS << " " << Node->getDecl()->getDeclKindName() << "Decl";
  1671. }
  1672. OS << "='" << *Node->getDecl() << "'";
  1673. dumpPointer(Node->getDecl());
  1674. if (Node->isFreeIvar())
  1675. OS << " isFreeIvar";
  1676. }
  1677. void ASTDumper::VisitPredefinedExpr(const PredefinedExpr *Node) {
  1678. VisitExpr(Node);
  1679. OS << " " << PredefinedExpr::getIdentTypeName(Node->getIdentType());
  1680. }
  1681. void ASTDumper::VisitCharacterLiteral(const CharacterLiteral *Node) {
  1682. VisitExpr(Node);
  1683. ColorScope Color(*this, ValueColor);
  1684. OS << " " << Node->getValue();
  1685. }
  1686. void ASTDumper::VisitIntegerLiteral(const IntegerLiteral *Node) {
  1687. VisitExpr(Node);
  1688. bool isSigned = Node->getType()->isSignedIntegerType();
  1689. ColorScope Color(*this, ValueColor);
  1690. OS << " " << Node->getValue().toString(10, isSigned);
  1691. }
  1692. void ASTDumper::VisitFloatingLiteral(const FloatingLiteral *Node) {
  1693. VisitExpr(Node);
  1694. ColorScope Color(*this, ValueColor);
  1695. OS << " " << Node->getValueAsApproximateDouble();
  1696. }
  1697. void ASTDumper::VisitStringLiteral(const StringLiteral *Str) {
  1698. VisitExpr(Str);
  1699. ColorScope Color(*this, ValueColor);
  1700. OS << " ";
  1701. Str->outputString(OS);
  1702. }
  1703. void ASTDumper::VisitInitListExpr(const InitListExpr *ILE) {
  1704. VisitExpr(ILE);
  1705. if (auto *Filler = ILE->getArrayFiller()) {
  1706. dumpChild([=] {
  1707. OS << "array filler";
  1708. dumpStmt(Filler);
  1709. });
  1710. }
  1711. if (auto *Field = ILE->getInitializedFieldInUnion()) {
  1712. OS << " field ";
  1713. dumpBareDeclRef(Field);
  1714. }
  1715. }
  1716. void ASTDumper::VisitUnaryOperator(const UnaryOperator *Node) {
  1717. VisitExpr(Node);
  1718. OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
  1719. << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
  1720. }
  1721. void ASTDumper::VisitUnaryExprOrTypeTraitExpr(
  1722. const UnaryExprOrTypeTraitExpr *Node) {
  1723. VisitExpr(Node);
  1724. switch(Node->getKind()) {
  1725. case UETT_SizeOf:
  1726. OS << " sizeof";
  1727. break;
  1728. case UETT_AlignOf:
  1729. OS << " alignof";
  1730. break;
  1731. case UETT_VecStep:
  1732. OS << " vec_step";
  1733. break;
  1734. case UETT_OpenMPRequiredSimdAlign:
  1735. OS << " __builtin_omp_required_simd_align";
  1736. break;
  1737. }
  1738. if (Node->isArgumentType())
  1739. dumpType(Node->getArgumentType());
  1740. }
  1741. void ASTDumper::VisitMemberExpr(const MemberExpr *Node) {
  1742. VisitExpr(Node);
  1743. OS << " " << (Node->isArrow() ? "->" : ".") << *Node->getMemberDecl();
  1744. dumpPointer(Node->getMemberDecl());
  1745. }
  1746. void ASTDumper::VisitExtVectorElementExpr(const ExtVectorElementExpr *Node) {
  1747. VisitExpr(Node);
  1748. OS << " " << Node->getAccessor().getNameStart();
  1749. }
  1750. // HLSL Change Starts
  1751. void ASTDumper::VisitExtMatrixElementExpr(const ExtMatrixElementExpr *Node) {
  1752. VisitExpr(Node);
  1753. OS << " " << Node->getAccessor().getNameStart();
  1754. }
  1755. void ASTDumper::VisitHLSLVectorElementExpr(const HLSLVectorElementExpr *Node) {
  1756. VisitExpr(Node);
  1757. OS << " " << Node->getAccessor().getNameStart();
  1758. }
  1759. // HLSL Change Ends
  1760. void ASTDumper::VisitBinaryOperator(const BinaryOperator *Node) {
  1761. VisitExpr(Node);
  1762. OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
  1763. }
  1764. void ASTDumper::VisitCompoundAssignOperator(
  1765. const CompoundAssignOperator *Node) {
  1766. VisitExpr(Node);
  1767. OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
  1768. << "' ComputeLHSTy=";
  1769. dumpBareType(Node->getComputationLHSType());
  1770. OS << " ComputeResultTy=";
  1771. dumpBareType(Node->getComputationResultType());
  1772. }
  1773. void ASTDumper::VisitBlockExpr(const BlockExpr *Node) {
  1774. VisitExpr(Node);
  1775. dumpDecl(Node->getBlockDecl());
  1776. }
  1777. void ASTDumper::VisitOpaqueValueExpr(const OpaqueValueExpr *Node) {
  1778. VisitExpr(Node);
  1779. if (Expr *Source = Node->getSourceExpr())
  1780. dumpStmt(Source);
  1781. }
  1782. // GNU extensions.
  1783. void ASTDumper::VisitAddrLabelExpr(const AddrLabelExpr *Node) {
  1784. VisitExpr(Node);
  1785. OS << " " << Node->getLabel()->getName();
  1786. dumpPointer(Node->getLabel());
  1787. }
  1788. //===----------------------------------------------------------------------===//
  1789. // C++ Expressions
  1790. //===----------------------------------------------------------------------===//
  1791. void ASTDumper::VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node) {
  1792. VisitExpr(Node);
  1793. OS << " " << Node->getCastName()
  1794. << "<" << Node->getTypeAsWritten().getAsString() << ">"
  1795. << " <" << Node->getCastKindName();
  1796. dumpBasePath(OS, Node);
  1797. OS << ">";
  1798. }
  1799. void ASTDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node) {
  1800. VisitExpr(Node);
  1801. OS << " " << (Node->getValue() ? "true" : "false");
  1802. }
  1803. void ASTDumper::VisitCXXThisExpr(const CXXThisExpr *Node) {
  1804. VisitExpr(Node);
  1805. OS << " this";
  1806. }
  1807. void ASTDumper::VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node) {
  1808. VisitExpr(Node);
  1809. OS << " functional cast to " << Node->getTypeAsWritten().getAsString()
  1810. << " <" << Node->getCastKindName() << ">";
  1811. }
  1812. void ASTDumper::VisitCXXConstructExpr(const CXXConstructExpr *Node) {
  1813. VisitExpr(Node);
  1814. CXXConstructorDecl *Ctor = Node->getConstructor();
  1815. dumpType(Ctor->getType());
  1816. if (Node->isElidable())
  1817. OS << " elidable";
  1818. if (Node->requiresZeroInitialization())
  1819. OS << " zeroing";
  1820. }
  1821. void ASTDumper::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node) {
  1822. VisitExpr(Node);
  1823. OS << " ";
  1824. dumpCXXTemporary(Node->getTemporary());
  1825. }
  1826. void ASTDumper::VisitCXXNewExpr(const CXXNewExpr *Node) {
  1827. VisitExpr(Node);
  1828. if (Node->isGlobalNew())
  1829. OS << " global";
  1830. if (Node->isArray())
  1831. OS << " array";
  1832. if (Node->getOperatorNew()) {
  1833. OS << ' ';
  1834. dumpBareDeclRef(Node->getOperatorNew());
  1835. }
  1836. // We could dump the deallocation function used in case of error, but it's
  1837. // usually not that interesting.
  1838. }
  1839. void ASTDumper::VisitCXXDeleteExpr(const CXXDeleteExpr *Node) {
  1840. VisitExpr(Node);
  1841. if (Node->isGlobalDelete())
  1842. OS << " global";
  1843. if (Node->isArrayForm())
  1844. OS << " array";
  1845. if (Node->getOperatorDelete()) {
  1846. OS << ' ';
  1847. dumpBareDeclRef(Node->getOperatorDelete());
  1848. }
  1849. }
  1850. void
  1851. ASTDumper::VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node) {
  1852. VisitExpr(Node);
  1853. if (const ValueDecl *VD = Node->getExtendingDecl()) {
  1854. OS << " extended by ";
  1855. dumpBareDeclRef(VD);
  1856. }
  1857. }
  1858. void ASTDumper::VisitExprWithCleanups(const ExprWithCleanups *Node) {
  1859. VisitExpr(Node);
  1860. for (unsigned i = 0, e = Node->getNumObjects(); i != e; ++i)
  1861. dumpDeclRef(Node->getObject(i), "cleanup");
  1862. }
  1863. void ASTDumper::dumpCXXTemporary(const CXXTemporary *Temporary) {
  1864. OS << "(CXXTemporary";
  1865. dumpPointer(Temporary);
  1866. OS << ")";
  1867. }
  1868. void ASTDumper::VisitSizeOfPackExpr(const SizeOfPackExpr *Node) {
  1869. VisitExpr(Node);
  1870. dumpPointer(Node->getPack());
  1871. dumpName(Node->getPack());
  1872. }
  1873. //===----------------------------------------------------------------------===//
  1874. // Obj-C Expressions
  1875. //===----------------------------------------------------------------------===//
  1876. void ASTDumper::VisitObjCMessageExpr(const ObjCMessageExpr *Node) {
  1877. VisitExpr(Node);
  1878. OS << " selector=";
  1879. Node->getSelector().print(OS);
  1880. switch (Node->getReceiverKind()) {
  1881. case ObjCMessageExpr::Instance:
  1882. break;
  1883. case ObjCMessageExpr::Class:
  1884. OS << " class=";
  1885. dumpBareType(Node->getClassReceiver());
  1886. break;
  1887. case ObjCMessageExpr::SuperInstance:
  1888. OS << " super (instance)";
  1889. break;
  1890. case ObjCMessageExpr::SuperClass:
  1891. OS << " super (class)";
  1892. break;
  1893. }
  1894. }
  1895. void ASTDumper::VisitObjCBoxedExpr(const ObjCBoxedExpr *Node) {
  1896. VisitExpr(Node);
  1897. OS << " selector=";
  1898. Node->getBoxingMethod()->getSelector().print(OS);
  1899. }
  1900. void ASTDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node) {
  1901. VisitStmt(Node);
  1902. if (const VarDecl *CatchParam = Node->getCatchParamDecl())
  1903. dumpDecl(CatchParam);
  1904. else
  1905. OS << " catch all";
  1906. }
  1907. void ASTDumper::VisitObjCEncodeExpr(const ObjCEncodeExpr *Node) {
  1908. VisitExpr(Node);
  1909. dumpType(Node->getEncodedType());
  1910. }
  1911. void ASTDumper::VisitObjCSelectorExpr(const ObjCSelectorExpr *Node) {
  1912. VisitExpr(Node);
  1913. OS << " ";
  1914. Node->getSelector().print(OS);
  1915. }
  1916. void ASTDumper::VisitObjCProtocolExpr(const ObjCProtocolExpr *Node) {
  1917. VisitExpr(Node);
  1918. OS << ' ' << *Node->getProtocol();
  1919. }
  1920. void ASTDumper::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node) {
  1921. VisitExpr(Node);
  1922. if (Node->isImplicitProperty()) {
  1923. OS << " Kind=MethodRef Getter=\"";
  1924. if (Node->getImplicitPropertyGetter())
  1925. Node->getImplicitPropertyGetter()->getSelector().print(OS);
  1926. else
  1927. OS << "(null)";
  1928. OS << "\" Setter=\"";
  1929. if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter())
  1930. Setter->getSelector().print(OS);
  1931. else
  1932. OS << "(null)";
  1933. OS << "\"";
  1934. } else {
  1935. OS << " Kind=PropertyRef Property=\"" << *Node->getExplicitProperty() <<'"';
  1936. }
  1937. if (Node->isSuperReceiver())
  1938. OS << " super";
  1939. OS << " Messaging=";
  1940. if (Node->isMessagingGetter() && Node->isMessagingSetter())
  1941. OS << "Getter&Setter";
  1942. else if (Node->isMessagingGetter())
  1943. OS << "Getter";
  1944. else if (Node->isMessagingSetter())
  1945. OS << "Setter";
  1946. }
  1947. void ASTDumper::VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node) {
  1948. VisitExpr(Node);
  1949. if (Node->isArraySubscriptRefExpr())
  1950. OS << " Kind=ArraySubscript GetterForArray=\"";
  1951. else
  1952. OS << " Kind=DictionarySubscript GetterForDictionary=\"";
  1953. if (Node->getAtIndexMethodDecl())
  1954. Node->getAtIndexMethodDecl()->getSelector().print(OS);
  1955. else
  1956. OS << "(null)";
  1957. if (Node->isArraySubscriptRefExpr())
  1958. OS << "\" SetterForArray=\"";
  1959. else
  1960. OS << "\" SetterForDictionary=\"";
  1961. if (Node->setAtIndexMethodDecl())
  1962. Node->setAtIndexMethodDecl()->getSelector().print(OS);
  1963. else
  1964. OS << "(null)";
  1965. }
  1966. void ASTDumper::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node) {
  1967. VisitExpr(Node);
  1968. OS << " " << (Node->getValue() ? "__objc_yes" : "__objc_no");
  1969. }
  1970. //===----------------------------------------------------------------------===//
  1971. // Comments
  1972. //===----------------------------------------------------------------------===//
  1973. const char *ASTDumper::getCommandName(unsigned CommandID) {
  1974. if (Traits)
  1975. return Traits->getCommandInfo(CommandID)->Name;
  1976. const CommandInfo *Info = CommandTraits::getBuiltinCommandInfo(CommandID);
  1977. if (Info)
  1978. return Info->Name;
  1979. return "<not a builtin command>";
  1980. }
  1981. void ASTDumper::dumpFullComment(const FullComment *C) {
  1982. if (!C)
  1983. return;
  1984. FC = C;
  1985. dumpComment(C);
  1986. FC = nullptr;
  1987. }
  1988. void ASTDumper::dumpComment(const Comment *C) {
  1989. dumpChild([=] {
  1990. if (!C) {
  1991. ColorScope Color(*this, NullColor);
  1992. OS << "<<<NULL>>>";
  1993. return;
  1994. }
  1995. {
  1996. ColorScope Color(*this, CommentColor);
  1997. OS << C->getCommentKindName();
  1998. }
  1999. dumpPointer(C);
  2000. dumpSourceRange(C->getSourceRange());
  2001. ConstCommentVisitor<ASTDumper>::visit(C);
  2002. for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
  2003. I != E; ++I)
  2004. dumpComment(*I);
  2005. });
  2006. }
  2007. void ASTDumper::visitTextComment(const TextComment *C) {
  2008. OS << " Text=\"" << C->getText() << "\"";
  2009. }
  2010. void ASTDumper::visitInlineCommandComment(const InlineCommandComment *C) {
  2011. OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
  2012. switch (C->getRenderKind()) {
  2013. case InlineCommandComment::RenderNormal:
  2014. OS << " RenderNormal";
  2015. break;
  2016. case InlineCommandComment::RenderBold:
  2017. OS << " RenderBold";
  2018. break;
  2019. case InlineCommandComment::RenderMonospaced:
  2020. OS << " RenderMonospaced";
  2021. break;
  2022. case InlineCommandComment::RenderEmphasized:
  2023. OS << " RenderEmphasized";
  2024. break;
  2025. }
  2026. for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
  2027. OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
  2028. }
  2029. void ASTDumper::visitHTMLStartTagComment(const HTMLStartTagComment *C) {
  2030. OS << " Name=\"" << C->getTagName() << "\"";
  2031. if (C->getNumAttrs() != 0) {
  2032. OS << " Attrs: ";
  2033. for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
  2034. const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
  2035. OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
  2036. }
  2037. }
  2038. if (C->isSelfClosing())
  2039. OS << " SelfClosing";
  2040. }
  2041. void ASTDumper::visitHTMLEndTagComment(const HTMLEndTagComment *C) {
  2042. OS << " Name=\"" << C->getTagName() << "\"";
  2043. }
  2044. void ASTDumper::visitBlockCommandComment(const BlockCommandComment *C) {
  2045. OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
  2046. for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
  2047. OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
  2048. }
  2049. void ASTDumper::visitParamCommandComment(const ParamCommandComment *C) {
  2050. OS << " " << ParamCommandComment::getDirectionAsString(C->getDirection());
  2051. if (C->isDirectionExplicit())
  2052. OS << " explicitly";
  2053. else
  2054. OS << " implicitly";
  2055. if (C->hasParamName()) {
  2056. if (C->isParamIndexValid())
  2057. OS << " Param=\"" << C->getParamName(FC) << "\"";
  2058. else
  2059. OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
  2060. }
  2061. if (C->isParamIndexValid() && !C->isVarArgParam())
  2062. OS << " ParamIndex=" << C->getParamIndex();
  2063. }
  2064. void ASTDumper::visitTParamCommandComment(const TParamCommandComment *C) {
  2065. if (C->hasParamName()) {
  2066. if (C->isPositionValid())
  2067. OS << " Param=\"" << C->getParamName(FC) << "\"";
  2068. else
  2069. OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
  2070. }
  2071. if (C->isPositionValid()) {
  2072. OS << " Position=<";
  2073. for (unsigned i = 0, e = C->getDepth(); i != e; ++i) {
  2074. OS << C->getIndex(i);
  2075. if (i != e - 1)
  2076. OS << ", ";
  2077. }
  2078. OS << ">";
  2079. }
  2080. }
  2081. void ASTDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C) {
  2082. OS << " Name=\"" << getCommandName(C->getCommandID()) << "\""
  2083. " CloseName=\"" << C->getCloseName() << "\"";
  2084. }
  2085. void ASTDumper::visitVerbatimBlockLineComment(
  2086. const VerbatimBlockLineComment *C) {
  2087. OS << " Text=\"" << C->getText() << "\"";
  2088. }
  2089. void ASTDumper::visitVerbatimLineComment(const VerbatimLineComment *C) {
  2090. OS << " Text=\"" << C->getText() << "\"";
  2091. }
  2092. //===----------------------------------------------------------------------===//
  2093. // Type method implementations
  2094. //===----------------------------------------------------------------------===//
  2095. void QualType::dump(const char *msg) const {
  2096. if (msg)
  2097. llvm::errs() << msg << ": ";
  2098. dump();
  2099. }
  2100. LLVM_DUMP_METHOD void QualType::dump() const {
  2101. ASTDumper Dumper(llvm::errs(), nullptr, nullptr);
  2102. Dumper.dumpTypeAsChild(*this);
  2103. }
  2104. LLVM_DUMP_METHOD void Type::dump() const { QualType(this, 0).dump(); }
  2105. //===----------------------------------------------------------------------===//
  2106. // Decl method implementations
  2107. //===----------------------------------------------------------------------===//
  2108. LLVM_DUMP_METHOD void Decl::dump() const { dump(llvm::errs()); }
  2109. LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS) const {
  2110. ASTDumper P(OS, &getASTContext().getCommentCommandTraits(),
  2111. &getASTContext().getSourceManager());
  2112. P.dumpDecl(this);
  2113. }
  2114. LLVM_DUMP_METHOD void Decl::dumpColor() const {
  2115. ASTDumper P(llvm::errs(), &getASTContext().getCommentCommandTraits(),
  2116. &getASTContext().getSourceManager(), /*ShowColors*/true);
  2117. P.dumpDecl(this);
  2118. }
  2119. LLVM_DUMP_METHOD void DeclContext::dumpLookups() const {
  2120. dumpLookups(llvm::errs());
  2121. }
  2122. LLVM_DUMP_METHOD void DeclContext::dumpLookups(raw_ostream &OS,
  2123. bool DumpDecls) const {
  2124. const DeclContext *DC = this;
  2125. while (!DC->isTranslationUnit())
  2126. DC = DC->getParent();
  2127. ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
  2128. ASTDumper P(OS, &Ctx.getCommentCommandTraits(), &Ctx.getSourceManager());
  2129. P.dumpLookups(this, DumpDecls);
  2130. }
  2131. //===----------------------------------------------------------------------===//
  2132. // Stmt method implementations
  2133. //===----------------------------------------------------------------------===//
  2134. LLVM_DUMP_METHOD void Stmt::dump(SourceManager &SM) const {
  2135. dump(llvm::errs(), SM);
  2136. }
  2137. LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS, SourceManager &SM) const {
  2138. ASTDumper P(OS, nullptr, &SM);
  2139. P.dumpStmt(this);
  2140. }
  2141. LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS) const {
  2142. ASTDumper P(OS, nullptr, nullptr);
  2143. P.dumpStmt(this);
  2144. }
  2145. LLVM_DUMP_METHOD void Stmt::dump() const {
  2146. ASTDumper P(llvm::errs(), nullptr, nullptr);
  2147. P.dumpStmt(this);
  2148. }
  2149. LLVM_DUMP_METHOD void Stmt::dumpColor() const {
  2150. ASTDumper P(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
  2151. P.dumpStmt(this);
  2152. }
  2153. //===----------------------------------------------------------------------===//
  2154. // Comment method implementations
  2155. //===----------------------------------------------------------------------===//
  2156. LLVM_DUMP_METHOD void Comment::dump() const {
  2157. dump(llvm::errs(), nullptr, nullptr);
  2158. }
  2159. LLVM_DUMP_METHOD void Comment::dump(const ASTContext &Context) const {
  2160. dump(llvm::errs(), &Context.getCommentCommandTraits(),
  2161. &Context.getSourceManager());
  2162. }
  2163. void Comment::dump(raw_ostream &OS, const CommandTraits *Traits,
  2164. const SourceManager *SM) const {
  2165. const FullComment *FC = dyn_cast<FullComment>(this);
  2166. ASTDumper D(OS, Traits, SM);
  2167. D.dumpFullComment(FC);
  2168. }
  2169. LLVM_DUMP_METHOD void Comment::dumpColor() const {
  2170. const FullComment *FC = dyn_cast<FullComment>(this);
  2171. ASTDumper D(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
  2172. D.dumpFullComment(FC);
  2173. }