ASTDumper.cpp 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525
  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. case hlsl::UnusualAnnotation::UA_PayloadAccessQualifier:
  889. OS << "PayloadAccessQualifier"; break;
  890. }
  891. }
  892. dumpPointer(It);
  893. dumpSourceRange(SourceRange((*It)->Loc));
  894. switch ((*It)->getKind())
  895. {
  896. case hlsl::UnusualAnnotation::UA_ConstantPacking: {
  897. const hlsl::ConstantPacking* constantPacking = cast<hlsl::ConstantPacking>(*It);
  898. OS << " packoffset(c";
  899. OS << constantPacking->Subcomponent;
  900. OS << ".";
  901. const char *xyzw[4] = { "x", "y", "z", "w" };
  902. if(constantPacking->ComponentOffset < 4)
  903. OS << xyzw[constantPacking->ComponentOffset];
  904. else
  905. OS << "<invalid>";
  906. OS << ")";
  907. if (!constantPacking->IsValid)
  908. OS << " invalid";
  909. break;
  910. }
  911. case hlsl::UnusualAnnotation::UA_RegisterAssignment: {
  912. const hlsl::RegisterAssignment* registerAssignment = cast<hlsl::RegisterAssignment>(*It);
  913. OS << " register(";
  914. if (!registerAssignment->ShaderProfile.empty())
  915. OS << registerAssignment->ShaderProfile << ", ";
  916. bool needsComma = false;
  917. if (!registerAssignment->isSpaceOnly()) {
  918. if (!registerAssignment->RegisterType)
  919. OS << "invalid";
  920. else
  921. OS << StringRef(&registerAssignment->RegisterType, 1);
  922. OS << registerAssignment->RegisterNumber + registerAssignment->RegisterOffset;
  923. needsComma = true;
  924. }
  925. if (registerAssignment->RegisterSpace.hasValue()) {
  926. if (needsComma) OS << ", ";
  927. OS << "space" << registerAssignment->RegisterSpace.getValue();
  928. }
  929. OS << ")";
  930. if (!registerAssignment->IsValid)
  931. OS << " invalid";
  932. break;
  933. }
  934. case hlsl::UnusualAnnotation::UA_SemanticDecl: {
  935. const hlsl::SemanticDecl* semanticDecl = cast<hlsl::SemanticDecl>(*It);
  936. OS << " \"" << semanticDecl->SemanticName << "\"";
  937. break;
  938. }
  939. case hlsl::UnusualAnnotation::UA_PayloadAccessQualifier: {
  940. const hlsl::PayloadAccessAnnotation *annotation =
  941. cast<hlsl::PayloadAccessAnnotation>(*It);
  942. OS << " "
  943. << (annotation->qualifier == hlsl::DXIL::PayloadAccessQualifier::Read
  944. ? "read"
  945. : "write")
  946. << "(";
  947. StringRef shaderStageNames[] = {"caller", "closesthit", "miss", "anyhit"};
  948. for (unsigned i = 0; i < annotation->ShaderStages.size(); ++i) {
  949. OS << shaderStageNames[static_cast<unsigned>(
  950. annotation->ShaderStages[i])];
  951. if (i < annotation->ShaderStages.size() - 1)
  952. OS << ", ";
  953. }
  954. OS << ")";
  955. break;
  956. }
  957. }
  958. });
  959. }
  960. }
  961. // HLSL Change Ends
  962. void ASTDumper::dumpDecl(const Decl *D) {
  963. // HLSL Change Starts: Don't display decls with invalid SourceLocations.
  964. if (D && D->getDeclContext() &&
  965. D->getDeclContext()->getDeclKind() == Decl::Kind::TranslationUnit &&
  966. D->getSourceRange().isInvalid())
  967. {
  968. return;
  969. }
  970. // HLSL Change Ends
  971. dumpChild([=] {
  972. if (!D) {
  973. ColorScope Color(*this, NullColor);
  974. OS << "<<<NULL>>>";
  975. return;
  976. }
  977. {
  978. ColorScope Color(*this, DeclKindNameColor);
  979. OS << D->getDeclKindName() << "Decl";
  980. }
  981. dumpPointer(D);
  982. if (D->getLexicalDeclContext() != D->getDeclContext())
  983. OS << " parent " << cast<Decl>(D->getDeclContext());
  984. // HLSL Change Begin - dump HLSLBufferDecl
  985. if (const HLSLBufferDecl *bufDecl =
  986. dyn_cast_or_null<HLSLBufferDecl>(D->getDeclContext())) {
  987. OS << " parent " << bufDecl->getDeclKindName();
  988. dumpPointer(bufDecl);
  989. }
  990. // HLSL Change End
  991. dumpPreviousDecl(OS, D);
  992. dumpSourceRange(D->getSourceRange());
  993. OS << ' ';
  994. dumpLocation(D->getLocation());
  995. if (Module *M = D->getImportedOwningModule())
  996. OS << " in " << M->getFullModuleName();
  997. else if (Module *M = D->getLocalOwningModule())
  998. OS << " in (local) " << M->getFullModuleName();
  999. if (auto *ND = dyn_cast<NamedDecl>(D))
  1000. for (Module *M : D->getASTContext().getModulesWithMergedDefinition(
  1001. const_cast<NamedDecl *>(ND)))
  1002. dumpChild([=] { OS << "also in " << M->getFullModuleName(); });
  1003. if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
  1004. if (ND->isHidden())
  1005. OS << " hidden";
  1006. if (D->isImplicit())
  1007. OS << " implicit";
  1008. if (D->isUsed())
  1009. OS << " used";
  1010. else if (D->isThisDeclarationReferenced())
  1011. OS << " referenced";
  1012. if (D->isInvalidDecl())
  1013. OS << " invalid";
  1014. if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
  1015. if (FD->isConstexpr())
  1016. OS << " constexpr";
  1017. ConstDeclVisitor<ASTDumper>::Visit(D);
  1018. for (Decl::attr_iterator I = D->attr_begin(), E = D->attr_end(); I != E;
  1019. ++I)
  1020. dumpAttr(*I);
  1021. // HLSL Change Begins
  1022. if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
  1023. dumpHLSLUnusualAnnotations(const_cast<NamedDecl*>(ND)->getUnusualAnnotations());
  1024. // HLSL Change Ends
  1025. if (const FullComment *Comment =
  1026. D->getASTContext().getLocalCommentForDeclUncached(D))
  1027. dumpFullComment(Comment);
  1028. // Decls within functions are visited by the body.
  1029. if (!isa<FunctionDecl>(*D) && !isa<ObjCMethodDecl>(*D) &&
  1030. hasNodes(dyn_cast<DeclContext>(D)))
  1031. dumpDeclContext(cast<DeclContext>(D));
  1032. });
  1033. }
  1034. void ASTDumper::VisitLabelDecl(const LabelDecl *D) {
  1035. dumpName(D);
  1036. }
  1037. void ASTDumper::VisitTypedefDecl(const TypedefDecl *D) {
  1038. dumpName(D);
  1039. dumpType(D->getUnderlyingType());
  1040. if (D->isModulePrivate())
  1041. OS << " __module_private__";
  1042. }
  1043. // HLSL Change Starts
  1044. void ASTDumper::VisitHLSLBufferDecl(const HLSLBufferDecl* D) {
  1045. if (D->isCBuffer())
  1046. OS << " cbuffer";
  1047. else
  1048. OS << " tbuffer";
  1049. dumpName(D);
  1050. }
  1051. // HLSL Change Ends
  1052. void ASTDumper::VisitEnumDecl(const EnumDecl *D) {
  1053. if (D->isScoped()) {
  1054. if (D->isScopedUsingClassTag())
  1055. OS << " class";
  1056. else
  1057. OS << " struct";
  1058. }
  1059. dumpName(D);
  1060. if (D->isModulePrivate())
  1061. OS << " __module_private__";
  1062. if (D->isFixed())
  1063. dumpType(D->getIntegerType());
  1064. }
  1065. void ASTDumper::VisitRecordDecl(const RecordDecl *D) {
  1066. OS << ' ' << D->getKindName();
  1067. dumpName(D);
  1068. if (D->isModulePrivate())
  1069. OS << " __module_private__";
  1070. if (D->isCompleteDefinition())
  1071. OS << " definition";
  1072. }
  1073. void ASTDumper::VisitEnumConstantDecl(const EnumConstantDecl *D) {
  1074. dumpName(D);
  1075. dumpType(D->getType());
  1076. if (const Expr *Init = D->getInitExpr())
  1077. dumpStmt(Init);
  1078. }
  1079. void ASTDumper::VisitIndirectFieldDecl(const IndirectFieldDecl *D) {
  1080. dumpName(D);
  1081. dumpType(D->getType());
  1082. for (auto *Child : D->chain())
  1083. dumpDeclRef(Child);
  1084. }
  1085. void ASTDumper::VisitFunctionDecl(const FunctionDecl *D) {
  1086. dumpName(D);
  1087. dumpType(D->getType());
  1088. StorageClass SC = D->getStorageClass();
  1089. if (SC != SC_None)
  1090. OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
  1091. if (D->isInlineSpecified())
  1092. OS << " inline";
  1093. if (D->isVirtualAsWritten())
  1094. OS << " virtual";
  1095. if (D->isModulePrivate())
  1096. OS << " __module_private__";
  1097. if (D->isPure())
  1098. OS << " pure";
  1099. else if (D->isDeletedAsWritten())
  1100. OS << " delete";
  1101. if (const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>()) {
  1102. FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
  1103. switch (EPI.ExceptionSpec.Type) {
  1104. default: break;
  1105. case EST_Unevaluated:
  1106. OS << " noexcept-unevaluated " << EPI.ExceptionSpec.SourceDecl;
  1107. break;
  1108. case EST_Uninstantiated:
  1109. OS << " noexcept-uninstantiated " << EPI.ExceptionSpec.SourceTemplate;
  1110. break;
  1111. }
  1112. }
  1113. if (const FunctionTemplateSpecializationInfo *FTSI =
  1114. D->getTemplateSpecializationInfo())
  1115. dumpTemplateArgumentList(*FTSI->TemplateArguments);
  1116. for (ArrayRef<NamedDecl *>::iterator
  1117. I = D->getDeclsInPrototypeScope().begin(),
  1118. E = D->getDeclsInPrototypeScope().end(); I != E; ++I)
  1119. dumpDecl(*I);
  1120. if (!D->param_begin() && D->getNumParams())
  1121. dumpChild([=] { OS << "<<NULL params x " << D->getNumParams() << ">>"; });
  1122. else
  1123. for (FunctionDecl::param_const_iterator I = D->param_begin(),
  1124. E = D->param_end();
  1125. I != E; ++I)
  1126. dumpDecl(*I);
  1127. if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(D))
  1128. for (CXXConstructorDecl::init_const_iterator I = C->init_begin(),
  1129. E = C->init_end();
  1130. I != E; ++I)
  1131. dumpCXXCtorInitializer(*I);
  1132. if (D->doesThisDeclarationHaveABody())
  1133. dumpStmt(D->getBody());
  1134. }
  1135. void ASTDumper::VisitFieldDecl(const FieldDecl *D) {
  1136. dumpName(D);
  1137. dumpType(D->getType());
  1138. if (D->isMutable())
  1139. OS << " mutable";
  1140. if (D->isModulePrivate())
  1141. OS << " __module_private__";
  1142. if (D->isBitField())
  1143. dumpStmt(D->getBitWidth());
  1144. if (Expr *Init = D->getInClassInitializer())
  1145. dumpStmt(Init);
  1146. }
  1147. void ASTDumper::VisitVarDecl(const VarDecl *D) {
  1148. dumpName(D);
  1149. dumpType(D->getType());
  1150. StorageClass SC = D->getStorageClass();
  1151. if (SC != SC_None)
  1152. OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
  1153. switch (D->getTLSKind()) {
  1154. case VarDecl::TLS_None: break;
  1155. case VarDecl::TLS_Static: OS << " tls"; break;
  1156. case VarDecl::TLS_Dynamic: OS << " tls_dynamic"; break;
  1157. }
  1158. if (D->isModulePrivate())
  1159. OS << " __module_private__";
  1160. if (D->isNRVOVariable())
  1161. OS << " nrvo";
  1162. if (D->hasInit()) {
  1163. switch (D->getInitStyle()) {
  1164. case VarDecl::CInit: OS << " cinit"; break;
  1165. case VarDecl::CallInit: OS << " callinit"; break;
  1166. case VarDecl::ListInit: OS << " listinit"; break;
  1167. }
  1168. dumpStmt(D->getInit());
  1169. }
  1170. }
  1171. void ASTDumper::VisitFileScopeAsmDecl(const FileScopeAsmDecl *D) {
  1172. dumpStmt(D->getAsmString());
  1173. }
  1174. void ASTDumper::VisitImportDecl(const ImportDecl *D) {
  1175. OS << ' ' << D->getImportedModule()->getFullModuleName();
  1176. }
  1177. //===----------------------------------------------------------------------===//
  1178. // C++ Declarations
  1179. //===----------------------------------------------------------------------===//
  1180. void ASTDumper::VisitNamespaceDecl(const NamespaceDecl *D) {
  1181. dumpName(D);
  1182. if (D->isInline())
  1183. OS << " inline";
  1184. if (!D->isOriginalNamespace())
  1185. dumpDeclRef(D->getOriginalNamespace(), "original");
  1186. }
  1187. void ASTDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
  1188. OS << ' ';
  1189. dumpBareDeclRef(D->getNominatedNamespace());
  1190. }
  1191. void ASTDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
  1192. dumpName(D);
  1193. dumpDeclRef(D->getAliasedNamespace());
  1194. }
  1195. void ASTDumper::VisitTypeAliasDecl(const TypeAliasDecl *D) {
  1196. dumpName(D);
  1197. dumpType(D->getUnderlyingType());
  1198. }
  1199. void ASTDumper::VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D) {
  1200. dumpName(D);
  1201. dumpTemplateParameters(D->getTemplateParameters());
  1202. dumpDecl(D->getTemplatedDecl());
  1203. }
  1204. void ASTDumper::VisitCXXRecordDecl(const CXXRecordDecl *D) {
  1205. VisitRecordDecl(D);
  1206. if (!D->isCompleteDefinition())
  1207. return;
  1208. for (const auto &I : D->bases()) {
  1209. dumpChild([=] {
  1210. if (I.isVirtual())
  1211. OS << "virtual ";
  1212. dumpAccessSpecifier(I.getAccessSpecifier());
  1213. dumpType(I.getType());
  1214. if (I.isPackExpansion())
  1215. OS << "...";
  1216. });
  1217. }
  1218. }
  1219. void ASTDumper::VisitStaticAssertDecl(const StaticAssertDecl *D) {
  1220. dumpStmt(D->getAssertExpr());
  1221. dumpStmt(D->getMessage());
  1222. }
  1223. template<typename SpecializationDecl>
  1224. void ASTDumper::VisitTemplateDeclSpecialization(const SpecializationDecl *D,
  1225. bool DumpExplicitInst,
  1226. bool DumpRefOnly) {
  1227. bool DumpedAny = false;
  1228. for (auto *RedeclWithBadType : D->redecls()) {
  1229. // FIXME: The redecls() range sometimes has elements of a less-specific
  1230. // type. (In particular, ClassTemplateSpecializationDecl::redecls() gives
  1231. // us TagDecls, and should give CXXRecordDecls).
  1232. auto *Redecl = dyn_cast<SpecializationDecl>(RedeclWithBadType);
  1233. if (!Redecl) {
  1234. // Found the injected-class-name for a class template. This will be dumped
  1235. // as part of its surrounding class so we don't need to dump it here.
  1236. assert(isa<CXXRecordDecl>(RedeclWithBadType) &&
  1237. "expected an injected-class-name");
  1238. continue;
  1239. }
  1240. switch (Redecl->getTemplateSpecializationKind()) {
  1241. case TSK_ExplicitInstantiationDeclaration:
  1242. case TSK_ExplicitInstantiationDefinition:
  1243. if (!DumpExplicitInst)
  1244. break;
  1245. // Fall through.
  1246. case TSK_Undeclared:
  1247. case TSK_ImplicitInstantiation:
  1248. if (DumpRefOnly)
  1249. dumpDeclRef(Redecl);
  1250. else
  1251. dumpDecl(Redecl);
  1252. DumpedAny = true;
  1253. break;
  1254. case TSK_ExplicitSpecialization:
  1255. break;
  1256. }
  1257. }
  1258. // Ensure we dump at least one decl for each specialization.
  1259. if (!DumpedAny)
  1260. dumpDeclRef(D);
  1261. }
  1262. template<typename TemplateDecl>
  1263. void ASTDumper::VisitTemplateDecl(const TemplateDecl *D,
  1264. bool DumpExplicitInst) {
  1265. dumpName(D);
  1266. dumpTemplateParameters(D->getTemplateParameters());
  1267. dumpDecl(D->getTemplatedDecl());
  1268. for (auto *Child : D->specializations())
  1269. VisitTemplateDeclSpecialization(Child, DumpExplicitInst,
  1270. !D->isCanonicalDecl());
  1271. }
  1272. void ASTDumper::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
  1273. // FIXME: We don't add a declaration of a function template specialization
  1274. // to its context when it's explicitly instantiated, so dump explicit
  1275. // instantiations when we dump the template itself.
  1276. VisitTemplateDecl(D, true);
  1277. }
  1278. void ASTDumper::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
  1279. VisitTemplateDecl(D, false);
  1280. }
  1281. void ASTDumper::VisitClassTemplateSpecializationDecl(
  1282. const ClassTemplateSpecializationDecl *D) {
  1283. VisitCXXRecordDecl(D);
  1284. dumpTemplateArgumentList(D->getTemplateArgs());
  1285. }
  1286. void ASTDumper::VisitClassTemplatePartialSpecializationDecl(
  1287. const ClassTemplatePartialSpecializationDecl *D) {
  1288. VisitClassTemplateSpecializationDecl(D);
  1289. dumpTemplateParameters(D->getTemplateParameters());
  1290. }
  1291. void ASTDumper::VisitClassScopeFunctionSpecializationDecl(
  1292. const ClassScopeFunctionSpecializationDecl *D) {
  1293. dumpDeclRef(D->getSpecialization());
  1294. if (D->hasExplicitTemplateArgs())
  1295. dumpTemplateArgumentListInfo(D->templateArgs());
  1296. }
  1297. void ASTDumper::VisitVarTemplateDecl(const VarTemplateDecl *D) {
  1298. VisitTemplateDecl(D, false);
  1299. }
  1300. void ASTDumper::VisitVarTemplateSpecializationDecl(
  1301. const VarTemplateSpecializationDecl *D) {
  1302. dumpTemplateArgumentList(D->getTemplateArgs());
  1303. VisitVarDecl(D);
  1304. }
  1305. void ASTDumper::VisitVarTemplatePartialSpecializationDecl(
  1306. const VarTemplatePartialSpecializationDecl *D) {
  1307. dumpTemplateParameters(D->getTemplateParameters());
  1308. VisitVarTemplateSpecializationDecl(D);
  1309. }
  1310. void ASTDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
  1311. if (D->wasDeclaredWithTypename())
  1312. OS << " typename";
  1313. else
  1314. OS << " class";
  1315. if (D->isParameterPack())
  1316. OS << " ...";
  1317. dumpName(D);
  1318. if (D->hasDefaultArgument())
  1319. dumpTemplateArgument(D->getDefaultArgument());
  1320. }
  1321. void ASTDumper::VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
  1322. dumpType(D->getType());
  1323. if (D->isParameterPack())
  1324. OS << " ...";
  1325. dumpName(D);
  1326. if (D->hasDefaultArgument())
  1327. dumpTemplateArgument(D->getDefaultArgument());
  1328. }
  1329. void ASTDumper::VisitTemplateTemplateParmDecl(
  1330. const TemplateTemplateParmDecl *D) {
  1331. if (D->isParameterPack())
  1332. OS << " ...";
  1333. dumpName(D);
  1334. dumpTemplateParameters(D->getTemplateParameters());
  1335. if (D->hasDefaultArgument())
  1336. dumpTemplateArgumentLoc(D->getDefaultArgument());
  1337. }
  1338. void ASTDumper::VisitUsingDecl(const UsingDecl *D) {
  1339. OS << ' ';
  1340. D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
  1341. OS << D->getNameAsString();
  1342. }
  1343. void ASTDumper::VisitUnresolvedUsingTypenameDecl(
  1344. const UnresolvedUsingTypenameDecl *D) {
  1345. OS << ' ';
  1346. D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
  1347. OS << D->getNameAsString();
  1348. }
  1349. void ASTDumper::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
  1350. OS << ' ';
  1351. D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
  1352. OS << D->getNameAsString();
  1353. dumpType(D->getType());
  1354. }
  1355. void ASTDumper::VisitUsingShadowDecl(const UsingShadowDecl *D) {
  1356. OS << ' ';
  1357. dumpBareDeclRef(D->getTargetDecl());
  1358. }
  1359. void ASTDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
  1360. switch (D->getLanguage()) {
  1361. case LinkageSpecDecl::lang_c: OS << " C"; break;
  1362. case LinkageSpecDecl::lang_cxx: OS << " C++"; break;
  1363. }
  1364. }
  1365. void ASTDumper::VisitAccessSpecDecl(const AccessSpecDecl *D) {
  1366. OS << ' ';
  1367. dumpAccessSpecifier(D->getAccess());
  1368. }
  1369. void ASTDumper::VisitFriendDecl(const FriendDecl *D) {
  1370. if (TypeSourceInfo *T = D->getFriendType())
  1371. dumpType(T->getType());
  1372. else
  1373. dumpDecl(D->getFriendDecl());
  1374. }
  1375. //===----------------------------------------------------------------------===//
  1376. // Obj-C Declarations
  1377. //===----------------------------------------------------------------------===//
  1378. void ASTDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) {
  1379. dumpName(D);
  1380. dumpType(D->getType());
  1381. if (D->getSynthesize())
  1382. OS << " synthesize";
  1383. switch (D->getAccessControl()) {
  1384. case ObjCIvarDecl::None:
  1385. OS << " none";
  1386. break;
  1387. case ObjCIvarDecl::Private:
  1388. OS << " private";
  1389. break;
  1390. case ObjCIvarDecl::Protected:
  1391. OS << " protected";
  1392. break;
  1393. case ObjCIvarDecl::Public:
  1394. OS << " public";
  1395. break;
  1396. case ObjCIvarDecl::Package:
  1397. OS << " package";
  1398. break;
  1399. }
  1400. }
  1401. void ASTDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
  1402. if (D->isInstanceMethod())
  1403. OS << " -";
  1404. else
  1405. OS << " +";
  1406. dumpName(D);
  1407. dumpType(D->getReturnType());
  1408. if (D->isThisDeclarationADefinition()) {
  1409. dumpDeclContext(D);
  1410. } else {
  1411. for (ObjCMethodDecl::param_const_iterator I = D->param_begin(),
  1412. E = D->param_end();
  1413. I != E; ++I)
  1414. dumpDecl(*I);
  1415. }
  1416. if (D->isVariadic())
  1417. dumpChild([=] { OS << "..."; });
  1418. if (D->hasBody())
  1419. dumpStmt(D->getBody());
  1420. }
  1421. void ASTDumper::VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D) {
  1422. dumpName(D);
  1423. switch (D->getVariance()) {
  1424. case ObjCTypeParamVariance::Invariant:
  1425. break;
  1426. case ObjCTypeParamVariance::Covariant:
  1427. OS << " covariant";
  1428. break;
  1429. case ObjCTypeParamVariance::Contravariant:
  1430. OS << " contravariant";
  1431. break;
  1432. }
  1433. if (D->hasExplicitBound())
  1434. OS << " bounded";
  1435. dumpType(D->getUnderlyingType());
  1436. }
  1437. void ASTDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
  1438. dumpName(D);
  1439. dumpDeclRef(D->getClassInterface());
  1440. dumpObjCTypeParamList(D->getTypeParamList());
  1441. dumpDeclRef(D->getImplementation());
  1442. for (ObjCCategoryDecl::protocol_iterator I = D->protocol_begin(),
  1443. E = D->protocol_end();
  1444. I != E; ++I)
  1445. dumpDeclRef(*I);
  1446. }
  1447. void ASTDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
  1448. dumpName(D);
  1449. dumpDeclRef(D->getClassInterface());
  1450. dumpDeclRef(D->getCategoryDecl());
  1451. }
  1452. void ASTDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
  1453. dumpName(D);
  1454. for (auto *Child : D->protocols())
  1455. dumpDeclRef(Child);
  1456. }
  1457. void ASTDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
  1458. dumpName(D);
  1459. dumpObjCTypeParamList(D->getTypeParamListAsWritten());
  1460. dumpDeclRef(D->getSuperClass(), "super");
  1461. dumpDeclRef(D->getImplementation());
  1462. for (auto *Child : D->protocols())
  1463. dumpDeclRef(Child);
  1464. }
  1465. void ASTDumper::VisitObjCImplementationDecl(const ObjCImplementationDecl *D) {
  1466. dumpName(D);
  1467. dumpDeclRef(D->getSuperClass(), "super");
  1468. dumpDeclRef(D->getClassInterface());
  1469. for (ObjCImplementationDecl::init_const_iterator I = D->init_begin(),
  1470. E = D->init_end();
  1471. I != E; ++I)
  1472. dumpCXXCtorInitializer(*I);
  1473. }
  1474. void ASTDumper::VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D) {
  1475. dumpName(D);
  1476. dumpDeclRef(D->getClassInterface());
  1477. }
  1478. void ASTDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
  1479. dumpName(D);
  1480. dumpType(D->getType());
  1481. if (D->getPropertyImplementation() == ObjCPropertyDecl::Required)
  1482. OS << " required";
  1483. else if (D->getPropertyImplementation() == ObjCPropertyDecl::Optional)
  1484. OS << " optional";
  1485. ObjCPropertyDecl::PropertyAttributeKind Attrs = D->getPropertyAttributes();
  1486. if (Attrs != ObjCPropertyDecl::OBJC_PR_noattr) {
  1487. if (Attrs & ObjCPropertyDecl::OBJC_PR_readonly)
  1488. OS << " readonly";
  1489. if (Attrs & ObjCPropertyDecl::OBJC_PR_assign)
  1490. OS << " assign";
  1491. if (Attrs & ObjCPropertyDecl::OBJC_PR_readwrite)
  1492. OS << " readwrite";
  1493. if (Attrs & ObjCPropertyDecl::OBJC_PR_retain)
  1494. OS << " retain";
  1495. if (Attrs & ObjCPropertyDecl::OBJC_PR_copy)
  1496. OS << " copy";
  1497. if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic)
  1498. OS << " nonatomic";
  1499. if (Attrs & ObjCPropertyDecl::OBJC_PR_atomic)
  1500. OS << " atomic";
  1501. if (Attrs & ObjCPropertyDecl::OBJC_PR_weak)
  1502. OS << " weak";
  1503. if (Attrs & ObjCPropertyDecl::OBJC_PR_strong)
  1504. OS << " strong";
  1505. if (Attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained)
  1506. OS << " unsafe_unretained";
  1507. if (Attrs & ObjCPropertyDecl::OBJC_PR_getter)
  1508. dumpDeclRef(D->getGetterMethodDecl(), "getter");
  1509. if (Attrs & ObjCPropertyDecl::OBJC_PR_setter)
  1510. dumpDeclRef(D->getSetterMethodDecl(), "setter");
  1511. }
  1512. }
  1513. void ASTDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
  1514. dumpName(D->getPropertyDecl());
  1515. if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
  1516. OS << " synthesize";
  1517. else
  1518. OS << " dynamic";
  1519. dumpDeclRef(D->getPropertyDecl());
  1520. dumpDeclRef(D->getPropertyIvarDecl());
  1521. }
  1522. void ASTDumper::VisitBlockDecl(const BlockDecl *D) {
  1523. for (auto I : D->params())
  1524. dumpDecl(I);
  1525. if (D->isVariadic())
  1526. dumpChild([=]{ OS << "..."; });
  1527. if (D->capturesCXXThis())
  1528. dumpChild([=]{ OS << "capture this"; });
  1529. for (const auto &I : D->captures()) {
  1530. dumpChild([=] {
  1531. OS << "capture";
  1532. if (I.isByRef())
  1533. OS << " byref";
  1534. if (I.isNested())
  1535. OS << " nested";
  1536. if (I.getVariable()) {
  1537. OS << ' ';
  1538. dumpBareDeclRef(I.getVariable());
  1539. }
  1540. if (I.hasCopyExpr())
  1541. dumpStmt(I.getCopyExpr());
  1542. });
  1543. }
  1544. dumpStmt(D->getBody());
  1545. }
  1546. //===----------------------------------------------------------------------===//
  1547. // Stmt dumping methods.
  1548. //===----------------------------------------------------------------------===//
  1549. void ASTDumper::dumpStmt(const Stmt *S) {
  1550. dumpChild([=] {
  1551. if (!S) {
  1552. ColorScope Color(*this, NullColor);
  1553. OS << "<<<NULL>>>";
  1554. return;
  1555. }
  1556. if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
  1557. VisitDeclStmt(DS);
  1558. return;
  1559. }
  1560. ConstStmtVisitor<ASTDumper>::Visit(S);
  1561. for (const Stmt *SubStmt : S->children())
  1562. dumpStmt(SubStmt);
  1563. });
  1564. }
  1565. void ASTDumper::VisitStmt(const Stmt *Node) {
  1566. {
  1567. ColorScope Color(*this, StmtColor);
  1568. OS << Node->getStmtClassName();
  1569. }
  1570. dumpPointer(Node);
  1571. dumpSourceRange(Node->getSourceRange());
  1572. }
  1573. void ASTDumper::VisitDeclStmt(const DeclStmt *Node) {
  1574. VisitStmt(Node);
  1575. for (DeclStmt::const_decl_iterator I = Node->decl_begin(),
  1576. E = Node->decl_end();
  1577. I != E; ++I)
  1578. dumpDecl(*I);
  1579. }
  1580. void ASTDumper::VisitAttributedStmt(const AttributedStmt *Node) {
  1581. VisitStmt(Node);
  1582. for (ArrayRef<const Attr *>::iterator I = Node->getAttrs().begin(),
  1583. E = Node->getAttrs().end();
  1584. I != E; ++I)
  1585. dumpAttr(*I);
  1586. }
  1587. void ASTDumper::VisitLabelStmt(const LabelStmt *Node) {
  1588. VisitStmt(Node);
  1589. OS << " '" << Node->getName() << "'";
  1590. }
  1591. void ASTDumper::VisitGotoStmt(const GotoStmt *Node) {
  1592. VisitStmt(Node);
  1593. OS << " '" << Node->getLabel()->getName() << "'";
  1594. dumpPointer(Node->getLabel());
  1595. }
  1596. void ASTDumper::VisitCXXCatchStmt(const CXXCatchStmt *Node) {
  1597. VisitStmt(Node);
  1598. dumpDecl(Node->getExceptionDecl());
  1599. }
  1600. //===----------------------------------------------------------------------===//
  1601. // Expr dumping methods.
  1602. //===----------------------------------------------------------------------===//
  1603. void ASTDumper::VisitExpr(const Expr *Node) {
  1604. VisitStmt(Node);
  1605. dumpType(Node->getType());
  1606. {
  1607. ColorScope Color(*this, ValueKindColor);
  1608. switch (Node->getValueKind()) {
  1609. case VK_RValue:
  1610. break;
  1611. case VK_LValue:
  1612. OS << " lvalue";
  1613. break;
  1614. case VK_XValue:
  1615. OS << " xvalue";
  1616. break;
  1617. }
  1618. }
  1619. {
  1620. ColorScope Color(*this, ObjectKindColor);
  1621. switch (Node->getObjectKind()) {
  1622. case OK_Ordinary:
  1623. break;
  1624. case OK_BitField:
  1625. OS << " bitfield";
  1626. break;
  1627. case OK_ObjCProperty:
  1628. OS << " objcproperty";
  1629. break;
  1630. case OK_ObjCSubscript:
  1631. OS << " objcsubscript";
  1632. break;
  1633. case OK_VectorComponent:
  1634. OS << " vectorcomponent";
  1635. break;
  1636. }
  1637. }
  1638. }
  1639. static void dumpBasePath(raw_ostream &OS, const CastExpr *Node) {
  1640. if (Node->path_empty())
  1641. return;
  1642. OS << " (";
  1643. bool First = true;
  1644. for (CastExpr::path_const_iterator I = Node->path_begin(),
  1645. E = Node->path_end();
  1646. I != E; ++I) {
  1647. const CXXBaseSpecifier *Base = *I;
  1648. if (!First)
  1649. OS << " -> ";
  1650. const CXXRecordDecl *RD =
  1651. cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
  1652. if (Base->isVirtual())
  1653. OS << "virtual ";
  1654. OS << RD->getName();
  1655. First = false;
  1656. }
  1657. OS << ')';
  1658. }
  1659. void ASTDumper::VisitCastExpr(const CastExpr *Node) {
  1660. VisitExpr(Node);
  1661. OS << " <";
  1662. {
  1663. ColorScope Color(*this, CastColor);
  1664. OS << Node->getCastKindName();
  1665. }
  1666. dumpBasePath(OS, Node);
  1667. OS << ">";
  1668. }
  1669. void ASTDumper::VisitDeclRefExpr(const DeclRefExpr *Node) {
  1670. VisitExpr(Node);
  1671. OS << " ";
  1672. dumpBareDeclRef(Node->getDecl());
  1673. if (Node->getDecl() != Node->getFoundDecl()) {
  1674. OS << " (";
  1675. dumpBareDeclRef(Node->getFoundDecl());
  1676. OS << ")";
  1677. }
  1678. }
  1679. void ASTDumper::VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node) {
  1680. VisitExpr(Node);
  1681. OS << " (";
  1682. if (!Node->requiresADL())
  1683. OS << "no ";
  1684. OS << "ADL) = '" << Node->getName() << '\'';
  1685. UnresolvedLookupExpr::decls_iterator
  1686. I = Node->decls_begin(), E = Node->decls_end();
  1687. if (I == E)
  1688. OS << " empty";
  1689. for (; I != E; ++I)
  1690. dumpPointer(*I);
  1691. }
  1692. void ASTDumper::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node) {
  1693. VisitExpr(Node);
  1694. {
  1695. ColorScope Color(*this, DeclKindNameColor);
  1696. OS << " " << Node->getDecl()->getDeclKindName() << "Decl";
  1697. }
  1698. OS << "='" << *Node->getDecl() << "'";
  1699. dumpPointer(Node->getDecl());
  1700. if (Node->isFreeIvar())
  1701. OS << " isFreeIvar";
  1702. }
  1703. void ASTDumper::VisitPredefinedExpr(const PredefinedExpr *Node) {
  1704. VisitExpr(Node);
  1705. OS << " " << PredefinedExpr::getIdentTypeName(Node->getIdentType());
  1706. }
  1707. void ASTDumper::VisitCharacterLiteral(const CharacterLiteral *Node) {
  1708. VisitExpr(Node);
  1709. ColorScope Color(*this, ValueColor);
  1710. OS << " " << Node->getValue();
  1711. }
  1712. void ASTDumper::VisitIntegerLiteral(const IntegerLiteral *Node) {
  1713. VisitExpr(Node);
  1714. bool isSigned = Node->getType()->isSignedIntegerType();
  1715. ColorScope Color(*this, ValueColor);
  1716. OS << " " << Node->getValue().toString(10, isSigned);
  1717. }
  1718. void ASTDumper::VisitFloatingLiteral(const FloatingLiteral *Node) {
  1719. VisitExpr(Node);
  1720. ColorScope Color(*this, ValueColor);
  1721. OS << " " << Node->getValueAsApproximateDouble();
  1722. }
  1723. void ASTDumper::VisitStringLiteral(const StringLiteral *Str) {
  1724. VisitExpr(Str);
  1725. ColorScope Color(*this, ValueColor);
  1726. OS << " ";
  1727. Str->outputString(OS);
  1728. }
  1729. void ASTDumper::VisitInitListExpr(const InitListExpr *ILE) {
  1730. VisitExpr(ILE);
  1731. if (auto *Filler = ILE->getArrayFiller()) {
  1732. dumpChild([=] {
  1733. OS << "array filler";
  1734. dumpStmt(Filler);
  1735. });
  1736. }
  1737. if (auto *Field = ILE->getInitializedFieldInUnion()) {
  1738. OS << " field ";
  1739. dumpBareDeclRef(Field);
  1740. }
  1741. }
  1742. void ASTDumper::VisitUnaryOperator(const UnaryOperator *Node) {
  1743. VisitExpr(Node);
  1744. OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
  1745. << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
  1746. }
  1747. void ASTDumper::VisitUnaryExprOrTypeTraitExpr(
  1748. const UnaryExprOrTypeTraitExpr *Node) {
  1749. VisitExpr(Node);
  1750. switch(Node->getKind()) {
  1751. case UETT_SizeOf:
  1752. OS << " sizeof";
  1753. break;
  1754. case UETT_AlignOf:
  1755. OS << " alignof";
  1756. break;
  1757. case UETT_VecStep:
  1758. OS << " vec_step";
  1759. break;
  1760. case UETT_OpenMPRequiredSimdAlign:
  1761. OS << " __builtin_omp_required_simd_align";
  1762. break;
  1763. // HLSL Change Begins
  1764. case UETT_ArrayLength:
  1765. OS << " Length";
  1766. // HLSLC Change Ends
  1767. }
  1768. if (Node->isArgumentType())
  1769. dumpType(Node->getArgumentType());
  1770. }
  1771. void ASTDumper::VisitMemberExpr(const MemberExpr *Node) {
  1772. VisitExpr(Node);
  1773. OS << " " << (Node->isArrow() ? "->" : ".") << *Node->getMemberDecl();
  1774. dumpPointer(Node->getMemberDecl());
  1775. }
  1776. void ASTDumper::VisitExtVectorElementExpr(const ExtVectorElementExpr *Node) {
  1777. VisitExpr(Node);
  1778. OS << " " << Node->getAccessor().getNameStart();
  1779. }
  1780. // HLSL Change Starts
  1781. void ASTDumper::VisitExtMatrixElementExpr(const ExtMatrixElementExpr *Node) {
  1782. VisitExpr(Node);
  1783. OS << " " << Node->getAccessor().getNameStart();
  1784. }
  1785. void ASTDumper::VisitHLSLVectorElementExpr(const HLSLVectorElementExpr *Node) {
  1786. VisitExpr(Node);
  1787. OS << " " << Node->getAccessor().getNameStart();
  1788. }
  1789. // HLSL Change Ends
  1790. void ASTDumper::VisitBinaryOperator(const BinaryOperator *Node) {
  1791. VisitExpr(Node);
  1792. OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
  1793. }
  1794. void ASTDumper::VisitCompoundAssignOperator(
  1795. const CompoundAssignOperator *Node) {
  1796. VisitExpr(Node);
  1797. OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
  1798. << "' ComputeLHSTy=";
  1799. dumpBareType(Node->getComputationLHSType());
  1800. OS << " ComputeResultTy=";
  1801. dumpBareType(Node->getComputationResultType());
  1802. }
  1803. void ASTDumper::VisitBlockExpr(const BlockExpr *Node) {
  1804. VisitExpr(Node);
  1805. dumpDecl(Node->getBlockDecl());
  1806. }
  1807. void ASTDumper::VisitOpaqueValueExpr(const OpaqueValueExpr *Node) {
  1808. VisitExpr(Node);
  1809. if (Expr *Source = Node->getSourceExpr())
  1810. dumpStmt(Source);
  1811. }
  1812. // GNU extensions.
  1813. void ASTDumper::VisitAddrLabelExpr(const AddrLabelExpr *Node) {
  1814. VisitExpr(Node);
  1815. OS << " " << Node->getLabel()->getName();
  1816. dumpPointer(Node->getLabel());
  1817. }
  1818. //===----------------------------------------------------------------------===//
  1819. // C++ Expressions
  1820. //===----------------------------------------------------------------------===//
  1821. void ASTDumper::VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node) {
  1822. VisitExpr(Node);
  1823. OS << " " << Node->getCastName()
  1824. << "<" << Node->getTypeAsWritten().getAsString() << ">"
  1825. << " <" << Node->getCastKindName();
  1826. dumpBasePath(OS, Node);
  1827. OS << ">";
  1828. }
  1829. void ASTDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node) {
  1830. VisitExpr(Node);
  1831. OS << " " << (Node->getValue() ? "true" : "false");
  1832. }
  1833. void ASTDumper::VisitCXXThisExpr(const CXXThisExpr *Node) {
  1834. VisitExpr(Node);
  1835. OS << " this";
  1836. }
  1837. void ASTDumper::VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node) {
  1838. VisitExpr(Node);
  1839. OS << " functional cast to " << Node->getTypeAsWritten().getAsString()
  1840. << " <" << Node->getCastKindName() << ">";
  1841. }
  1842. void ASTDumper::VisitCXXConstructExpr(const CXXConstructExpr *Node) {
  1843. VisitExpr(Node);
  1844. CXXConstructorDecl *Ctor = Node->getConstructor();
  1845. dumpType(Ctor->getType());
  1846. if (Node->isElidable())
  1847. OS << " elidable";
  1848. if (Node->requiresZeroInitialization())
  1849. OS << " zeroing";
  1850. }
  1851. void ASTDumper::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node) {
  1852. VisitExpr(Node);
  1853. OS << " ";
  1854. dumpCXXTemporary(Node->getTemporary());
  1855. }
  1856. void ASTDumper::VisitCXXNewExpr(const CXXNewExpr *Node) {
  1857. VisitExpr(Node);
  1858. if (Node->isGlobalNew())
  1859. OS << " global";
  1860. if (Node->isArray())
  1861. OS << " array";
  1862. if (Node->getOperatorNew()) {
  1863. OS << ' ';
  1864. dumpBareDeclRef(Node->getOperatorNew());
  1865. }
  1866. // We could dump the deallocation function used in case of error, but it's
  1867. // usually not that interesting.
  1868. }
  1869. void ASTDumper::VisitCXXDeleteExpr(const CXXDeleteExpr *Node) {
  1870. VisitExpr(Node);
  1871. if (Node->isGlobalDelete())
  1872. OS << " global";
  1873. if (Node->isArrayForm())
  1874. OS << " array";
  1875. if (Node->getOperatorDelete()) {
  1876. OS << ' ';
  1877. dumpBareDeclRef(Node->getOperatorDelete());
  1878. }
  1879. }
  1880. void
  1881. ASTDumper::VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node) {
  1882. VisitExpr(Node);
  1883. if (const ValueDecl *VD = Node->getExtendingDecl()) {
  1884. OS << " extended by ";
  1885. dumpBareDeclRef(VD);
  1886. }
  1887. }
  1888. void ASTDumper::VisitExprWithCleanups(const ExprWithCleanups *Node) {
  1889. VisitExpr(Node);
  1890. for (unsigned i = 0, e = Node->getNumObjects(); i != e; ++i)
  1891. dumpDeclRef(Node->getObject(i), "cleanup");
  1892. }
  1893. void ASTDumper::dumpCXXTemporary(const CXXTemporary *Temporary) {
  1894. OS << "(CXXTemporary";
  1895. dumpPointer(Temporary);
  1896. OS << ")";
  1897. }
  1898. void ASTDumper::VisitSizeOfPackExpr(const SizeOfPackExpr *Node) {
  1899. VisitExpr(Node);
  1900. dumpPointer(Node->getPack());
  1901. dumpName(Node->getPack());
  1902. }
  1903. //===----------------------------------------------------------------------===//
  1904. // Obj-C Expressions
  1905. //===----------------------------------------------------------------------===//
  1906. void ASTDumper::VisitObjCMessageExpr(const ObjCMessageExpr *Node) {
  1907. VisitExpr(Node);
  1908. OS << " selector=";
  1909. Node->getSelector().print(OS);
  1910. switch (Node->getReceiverKind()) {
  1911. case ObjCMessageExpr::Instance:
  1912. break;
  1913. case ObjCMessageExpr::Class:
  1914. OS << " class=";
  1915. dumpBareType(Node->getClassReceiver());
  1916. break;
  1917. case ObjCMessageExpr::SuperInstance:
  1918. OS << " super (instance)";
  1919. break;
  1920. case ObjCMessageExpr::SuperClass:
  1921. OS << " super (class)";
  1922. break;
  1923. }
  1924. }
  1925. void ASTDumper::VisitObjCBoxedExpr(const ObjCBoxedExpr *Node) {
  1926. VisitExpr(Node);
  1927. OS << " selector=";
  1928. Node->getBoxingMethod()->getSelector().print(OS);
  1929. }
  1930. void ASTDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node) {
  1931. VisitStmt(Node);
  1932. if (const VarDecl *CatchParam = Node->getCatchParamDecl())
  1933. dumpDecl(CatchParam);
  1934. else
  1935. OS << " catch all";
  1936. }
  1937. void ASTDumper::VisitObjCEncodeExpr(const ObjCEncodeExpr *Node) {
  1938. VisitExpr(Node);
  1939. dumpType(Node->getEncodedType());
  1940. }
  1941. void ASTDumper::VisitObjCSelectorExpr(const ObjCSelectorExpr *Node) {
  1942. VisitExpr(Node);
  1943. OS << " ";
  1944. Node->getSelector().print(OS);
  1945. }
  1946. void ASTDumper::VisitObjCProtocolExpr(const ObjCProtocolExpr *Node) {
  1947. VisitExpr(Node);
  1948. OS << ' ' << *Node->getProtocol();
  1949. }
  1950. void ASTDumper::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node) {
  1951. VisitExpr(Node);
  1952. if (Node->isImplicitProperty()) {
  1953. OS << " Kind=MethodRef Getter=\"";
  1954. if (Node->getImplicitPropertyGetter())
  1955. Node->getImplicitPropertyGetter()->getSelector().print(OS);
  1956. else
  1957. OS << "(null)";
  1958. OS << "\" Setter=\"";
  1959. if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter())
  1960. Setter->getSelector().print(OS);
  1961. else
  1962. OS << "(null)";
  1963. OS << "\"";
  1964. } else {
  1965. OS << " Kind=PropertyRef Property=\"" << *Node->getExplicitProperty() <<'"';
  1966. }
  1967. if (Node->isSuperReceiver())
  1968. OS << " super";
  1969. OS << " Messaging=";
  1970. if (Node->isMessagingGetter() && Node->isMessagingSetter())
  1971. OS << "Getter&Setter";
  1972. else if (Node->isMessagingGetter())
  1973. OS << "Getter";
  1974. else if (Node->isMessagingSetter())
  1975. OS << "Setter";
  1976. }
  1977. void ASTDumper::VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node) {
  1978. VisitExpr(Node);
  1979. if (Node->isArraySubscriptRefExpr())
  1980. OS << " Kind=ArraySubscript GetterForArray=\"";
  1981. else
  1982. OS << " Kind=DictionarySubscript GetterForDictionary=\"";
  1983. if (Node->getAtIndexMethodDecl())
  1984. Node->getAtIndexMethodDecl()->getSelector().print(OS);
  1985. else
  1986. OS << "(null)";
  1987. if (Node->isArraySubscriptRefExpr())
  1988. OS << "\" SetterForArray=\"";
  1989. else
  1990. OS << "\" SetterForDictionary=\"";
  1991. if (Node->setAtIndexMethodDecl())
  1992. Node->setAtIndexMethodDecl()->getSelector().print(OS);
  1993. else
  1994. OS << "(null)";
  1995. }
  1996. void ASTDumper::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node) {
  1997. VisitExpr(Node);
  1998. OS << " " << (Node->getValue() ? "__objc_yes" : "__objc_no");
  1999. }
  2000. //===----------------------------------------------------------------------===//
  2001. // Comments
  2002. //===----------------------------------------------------------------------===//
  2003. const char *ASTDumper::getCommandName(unsigned CommandID) {
  2004. if (Traits)
  2005. return Traits->getCommandInfo(CommandID)->Name;
  2006. const CommandInfo *Info = CommandTraits::getBuiltinCommandInfo(CommandID);
  2007. if (Info)
  2008. return Info->Name;
  2009. return "<not a builtin command>";
  2010. }
  2011. void ASTDumper::dumpFullComment(const FullComment *C) {
  2012. if (!C)
  2013. return;
  2014. FC = C;
  2015. dumpComment(C);
  2016. FC = nullptr;
  2017. }
  2018. void ASTDumper::dumpComment(const Comment *C) {
  2019. dumpChild([=] {
  2020. if (!C) {
  2021. ColorScope Color(*this, NullColor);
  2022. OS << "<<<NULL>>>";
  2023. return;
  2024. }
  2025. {
  2026. ColorScope Color(*this, CommentColor);
  2027. OS << C->getCommentKindName();
  2028. }
  2029. dumpPointer(C);
  2030. dumpSourceRange(C->getSourceRange());
  2031. ConstCommentVisitor<ASTDumper>::visit(C);
  2032. for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
  2033. I != E; ++I)
  2034. dumpComment(*I);
  2035. });
  2036. }
  2037. void ASTDumper::visitTextComment(const TextComment *C) {
  2038. OS << " Text=\"" << C->getText() << "\"";
  2039. }
  2040. void ASTDumper::visitInlineCommandComment(const InlineCommandComment *C) {
  2041. OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
  2042. switch (C->getRenderKind()) {
  2043. case InlineCommandComment::RenderNormal:
  2044. OS << " RenderNormal";
  2045. break;
  2046. case InlineCommandComment::RenderBold:
  2047. OS << " RenderBold";
  2048. break;
  2049. case InlineCommandComment::RenderMonospaced:
  2050. OS << " RenderMonospaced";
  2051. break;
  2052. case InlineCommandComment::RenderEmphasized:
  2053. OS << " RenderEmphasized";
  2054. break;
  2055. }
  2056. for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
  2057. OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
  2058. }
  2059. void ASTDumper::visitHTMLStartTagComment(const HTMLStartTagComment *C) {
  2060. OS << " Name=\"" << C->getTagName() << "\"";
  2061. if (C->getNumAttrs() != 0) {
  2062. OS << " Attrs: ";
  2063. for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
  2064. const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
  2065. OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
  2066. }
  2067. }
  2068. if (C->isSelfClosing())
  2069. OS << " SelfClosing";
  2070. }
  2071. void ASTDumper::visitHTMLEndTagComment(const HTMLEndTagComment *C) {
  2072. OS << " Name=\"" << C->getTagName() << "\"";
  2073. }
  2074. void ASTDumper::visitBlockCommandComment(const BlockCommandComment *C) {
  2075. OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
  2076. for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
  2077. OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
  2078. }
  2079. void ASTDumper::visitParamCommandComment(const ParamCommandComment *C) {
  2080. OS << " " << ParamCommandComment::getDirectionAsString(C->getDirection());
  2081. if (C->isDirectionExplicit())
  2082. OS << " explicitly";
  2083. else
  2084. OS << " implicitly";
  2085. if (C->hasParamName()) {
  2086. if (C->isParamIndexValid())
  2087. OS << " Param=\"" << C->getParamName(FC) << "\"";
  2088. else
  2089. OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
  2090. }
  2091. if (C->isParamIndexValid() && !C->isVarArgParam())
  2092. OS << " ParamIndex=" << C->getParamIndex();
  2093. }
  2094. void ASTDumper::visitTParamCommandComment(const TParamCommandComment *C) {
  2095. if (C->hasParamName()) {
  2096. if (C->isPositionValid())
  2097. OS << " Param=\"" << C->getParamName(FC) << "\"";
  2098. else
  2099. OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
  2100. }
  2101. if (C->isPositionValid()) {
  2102. OS << " Position=<";
  2103. for (unsigned i = 0, e = C->getDepth(); i != e; ++i) {
  2104. OS << C->getIndex(i);
  2105. if (i != e - 1)
  2106. OS << ", ";
  2107. }
  2108. OS << ">";
  2109. }
  2110. }
  2111. void ASTDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C) {
  2112. OS << " Name=\"" << getCommandName(C->getCommandID()) << "\""
  2113. " CloseName=\"" << C->getCloseName() << "\"";
  2114. }
  2115. void ASTDumper::visitVerbatimBlockLineComment(
  2116. const VerbatimBlockLineComment *C) {
  2117. OS << " Text=\"" << C->getText() << "\"";
  2118. }
  2119. void ASTDumper::visitVerbatimLineComment(const VerbatimLineComment *C) {
  2120. OS << " Text=\"" << C->getText() << "\"";
  2121. }
  2122. //===----------------------------------------------------------------------===//
  2123. // Type method implementations
  2124. //===----------------------------------------------------------------------===//
  2125. void QualType::dump(const char *msg) const {
  2126. if (msg)
  2127. llvm::errs() << msg << ": ";
  2128. dump();
  2129. }
  2130. LLVM_DUMP_METHOD void QualType::dump() const {
  2131. ASTDumper Dumper(llvm::errs(), nullptr, nullptr);
  2132. Dumper.dumpTypeAsChild(*this);
  2133. }
  2134. LLVM_DUMP_METHOD void Type::dump() const { QualType(this, 0).dump(); }
  2135. //===----------------------------------------------------------------------===//
  2136. // Decl method implementations
  2137. //===----------------------------------------------------------------------===//
  2138. LLVM_DUMP_METHOD void Decl::dump() const { dump(llvm::errs()); }
  2139. LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS) const {
  2140. ASTDumper P(OS, &getASTContext().getCommentCommandTraits(),
  2141. &getASTContext().getSourceManager());
  2142. P.dumpDecl(this);
  2143. }
  2144. LLVM_DUMP_METHOD void Decl::dumpColor() const {
  2145. ASTDumper P(llvm::errs(), &getASTContext().getCommentCommandTraits(),
  2146. &getASTContext().getSourceManager(), /*ShowColors*/true);
  2147. P.dumpDecl(this);
  2148. }
  2149. LLVM_DUMP_METHOD void DeclContext::dumpLookups() const {
  2150. dumpLookups(llvm::errs());
  2151. }
  2152. LLVM_DUMP_METHOD void DeclContext::dumpLookups(raw_ostream &OS,
  2153. bool DumpDecls) const {
  2154. const DeclContext *DC = this;
  2155. while (!DC->isTranslationUnit())
  2156. DC = DC->getParent();
  2157. ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
  2158. ASTDumper P(OS, &Ctx.getCommentCommandTraits(), &Ctx.getSourceManager());
  2159. P.dumpLookups(this, DumpDecls);
  2160. }
  2161. //===----------------------------------------------------------------------===//
  2162. // Stmt method implementations
  2163. //===----------------------------------------------------------------------===//
  2164. LLVM_DUMP_METHOD void Stmt::dump(SourceManager &SM) const {
  2165. dump(llvm::errs(), SM);
  2166. }
  2167. LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS, SourceManager &SM) const {
  2168. ASTDumper P(OS, nullptr, &SM);
  2169. P.dumpStmt(this);
  2170. }
  2171. LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS) const {
  2172. ASTDumper P(OS, nullptr, nullptr);
  2173. P.dumpStmt(this);
  2174. }
  2175. LLVM_DUMP_METHOD void Stmt::dump() const {
  2176. ASTDumper P(llvm::errs(), nullptr, nullptr);
  2177. P.dumpStmt(this);
  2178. }
  2179. LLVM_DUMP_METHOD void Stmt::dumpColor() const {
  2180. ASTDumper P(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
  2181. P.dumpStmt(this);
  2182. }
  2183. //===----------------------------------------------------------------------===//
  2184. // Comment method implementations
  2185. //===----------------------------------------------------------------------===//
  2186. LLVM_DUMP_METHOD void Comment::dump() const {
  2187. dump(llvm::errs(), nullptr, nullptr);
  2188. }
  2189. LLVM_DUMP_METHOD void Comment::dump(const ASTContext &Context) const {
  2190. dump(llvm::errs(), &Context.getCommentCommandTraits(),
  2191. &Context.getSourceManager());
  2192. }
  2193. void Comment::dump(raw_ostream &OS, const CommandTraits *Traits,
  2194. const SourceManager *SM) const {
  2195. const FullComment *FC = dyn_cast<FullComment>(this);
  2196. ASTDumper D(OS, Traits, SM);
  2197. D.dumpFullComment(FC);
  2198. }
  2199. LLVM_DUMP_METHOD void Comment::dumpColor() const {
  2200. const FullComment *FC = dyn_cast<FullComment>(this);
  2201. ASTDumper D(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
  2202. D.dumpFullComment(FC);
  2203. }