ASTDumper.cpp 75 KB

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