DeclPrinter.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  1. //===--- DeclPrinter.cpp - Printing implementation for Decl 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 Decl::print method, which pretty prints the
  11. // AST back out to C/Objective-C/C++/Objective-C++ code.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/Attr.h"
  16. #include "clang/AST/Decl.h"
  17. #include "clang/AST/DeclCXX.h"
  18. #include "clang/AST/DeclObjC.h"
  19. #include "clang/AST/DeclVisitor.h"
  20. #include "clang/AST/Expr.h"
  21. #include "clang/AST/ExprCXX.h"
  22. #include "clang/AST/PrettyPrinter.h"
  23. #include "clang/Basic/Module.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. #include "clang/Sema/SemaHLSL.h" // HLSL Change
  26. using namespace clang;
  27. // //
  28. ///////////////////////////////////////////////////////////////////////////////
  29. namespace {
  30. class DeclPrinter : public DeclVisitor<DeclPrinter> {
  31. raw_ostream &Out;
  32. PrintingPolicy Policy;
  33. unsigned Indentation;
  34. bool PrintInstantiation;
  35. raw_ostream& Indent() { return Indent(Indentation); }
  36. raw_ostream& Indent(unsigned Indentation);
  37. void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls);
  38. void Print(AccessSpecifier AS);
  39. /// Print an Objective-C method type in parentheses.
  40. ///
  41. /// \param Quals The Objective-C declaration qualifiers.
  42. /// \param T The type to print.
  43. void PrintObjCMethodType(ASTContext &Ctx, Decl::ObjCDeclQualifier Quals,
  44. QualType T);
  45. void PrintObjCTypeParams(ObjCTypeParamList *Params);
  46. public:
  47. DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy,
  48. unsigned Indentation = 0, bool PrintInstantiation = false)
  49. : Out(Out), Policy(Policy), Indentation(Indentation),
  50. PrintInstantiation(PrintInstantiation) { }
  51. void VisitDeclContext(DeclContext *DC, bool Indent = true);
  52. void VisitTranslationUnitDecl(TranslationUnitDecl *D);
  53. void VisitTypedefDecl(TypedefDecl *D);
  54. void VisitTypeAliasDecl(TypeAliasDecl *D);
  55. void VisitEnumDecl(EnumDecl *D);
  56. void VisitRecordDecl(RecordDecl *D);
  57. void VisitEnumConstantDecl(EnumConstantDecl *D);
  58. void VisitEmptyDecl(EmptyDecl *D);
  59. void VisitFunctionDecl(FunctionDecl *D);
  60. void VisitFriendDecl(FriendDecl *D);
  61. void VisitFieldDecl(FieldDecl *D);
  62. void VisitVarDecl(VarDecl *D);
  63. void VisitLabelDecl(LabelDecl *D);
  64. void VisitParmVarDecl(ParmVarDecl *D);
  65. void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
  66. void VisitImportDecl(ImportDecl *D);
  67. void VisitStaticAssertDecl(StaticAssertDecl *D);
  68. void VisitNamespaceDecl(NamespaceDecl *D);
  69. void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
  70. void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
  71. void VisitCXXRecordDecl(CXXRecordDecl *D);
  72. void VisitLinkageSpecDecl(LinkageSpecDecl *D);
  73. void VisitTemplateDecl(const TemplateDecl *D);
  74. void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
  75. void VisitClassTemplateDecl(ClassTemplateDecl *D);
  76. void VisitObjCMethodDecl(ObjCMethodDecl *D);
  77. void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
  78. void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
  79. void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
  80. void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
  81. void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
  82. void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
  83. void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
  84. void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
  85. void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
  86. void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
  87. void VisitUsingDecl(UsingDecl *D);
  88. void VisitUsingShadowDecl(UsingShadowDecl *D);
  89. void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
  90. void PrintTemplateParameters(const TemplateParameterList *Params,
  91. const TemplateArgumentList *Args = nullptr);
  92. void prettyPrintAttributes(Decl *D);
  93. void printDeclType(QualType T, StringRef DeclName, bool Pack = false);
  94. // HLSL Change Begin
  95. void VisitHLSLBufferDecl(HLSLBufferDecl *D);
  96. void PrintUnusualAnnotations(NamedDecl *D);
  97. void VisitHLSLUnusualAnnotation(const hlsl::UnusualAnnotation *UA);
  98. void PrintHLSLPreAttr(NamedDecl *D);
  99. // HLSL Change End
  100. };
  101. }
  102. void Decl::print(raw_ostream &Out, unsigned Indentation,
  103. bool PrintInstantiation) const {
  104. print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
  105. }
  106. void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
  107. unsigned Indentation, bool PrintInstantiation) const {
  108. DeclPrinter Printer(Out, Policy, Indentation, PrintInstantiation);
  109. Printer.Visit(const_cast<Decl*>(this));
  110. }
  111. static QualType GetBaseType(QualType T) {
  112. // FIXME: This should be on the Type class!
  113. QualType BaseType = T;
  114. while (!BaseType->isSpecifierType()) {
  115. if (isa<TypedefType>(BaseType))
  116. break;
  117. else if (const PointerType* PTy = BaseType->getAs<PointerType>())
  118. BaseType = PTy->getPointeeType();
  119. else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
  120. BaseType = BPy->getPointeeType();
  121. else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
  122. BaseType = ATy->getElementType();
  123. else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
  124. BaseType = FTy->getReturnType();
  125. else if (const VectorType *VTy = BaseType->getAs<VectorType>())
  126. BaseType = VTy->getElementType();
  127. else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
  128. BaseType = RTy->getPointeeType();
  129. else
  130. llvm_unreachable("Unknown declarator!");
  131. }
  132. return BaseType;
  133. }
  134. static QualType getDeclType(Decl* D) {
  135. if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
  136. return TDD->getUnderlyingType();
  137. if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
  138. return VD->getType();
  139. return QualType();
  140. }
  141. void Decl::printGroup(Decl** Begin, unsigned NumDecls,
  142. raw_ostream &Out, const PrintingPolicy &Policy,
  143. unsigned Indentation) {
  144. if (NumDecls == 1) {
  145. (*Begin)->print(Out, Policy, Indentation);
  146. return;
  147. }
  148. Decl** End = Begin + NumDecls;
  149. TagDecl* TD = dyn_cast<TagDecl>(*Begin);
  150. if (TD)
  151. ++Begin;
  152. // HLSL Change Begin - anonymous struct need to have static.
  153. //static const struct {
  154. // float a;
  155. // SamplerState s;
  156. //} A = {1.2, ss};
  157. // will be rewrite to
  158. // struct {
  159. // float a;
  160. // SamplerState s;
  161. //} static const A = {1.2, ss};
  162. // without this change.
  163. bool bAnonymous = false;
  164. if (TD && TD->getName().empty()) {
  165. bAnonymous = true;
  166. }
  167. if (bAnonymous && Begin) {
  168. if (VarDecl *VD = dyn_cast<VarDecl>(*Begin)) {
  169. if (!Policy.SuppressSpecifiers) {
  170. StorageClass SC = VD->getStorageClass();
  171. if (SC != SC_None)
  172. Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
  173. if (VD->getType().hasQualifiers())
  174. VD->getType().getQualifiers().print(Out, Policy,
  175. /*appendSpaceIfNonEmpty*/ true);
  176. }
  177. }
  178. }
  179. // HLSL Change End
  180. PrintingPolicy SubPolicy(Policy);
  181. if (TD && TD->isCompleteDefinition()) {
  182. TD->print(Out, Policy, Indentation);
  183. Out << " ";
  184. SubPolicy.SuppressTag = true;
  185. }
  186. bool isFirst = true;
  187. for ( ; Begin != End; ++Begin) {
  188. if (isFirst) {
  189. SubPolicy.SuppressSpecifiers = bAnonymous; // HLSL Change.
  190. isFirst = false;
  191. } else {
  192. if (!isFirst) Out << ", ";
  193. SubPolicy.SuppressSpecifiers = true;
  194. }
  195. (*Begin)->print(Out, SubPolicy, Indentation);
  196. }
  197. }
  198. LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
  199. // Get the translation unit
  200. const DeclContext *DC = this;
  201. while (!DC->isTranslationUnit())
  202. DC = DC->getParent();
  203. ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
  204. DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), 0);
  205. Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
  206. }
  207. raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
  208. for (unsigned i = 0; i != Indentation; ++i)
  209. Out << " ";
  210. return Out;
  211. }
  212. void DeclPrinter::prettyPrintAttributes(Decl *D) {
  213. if (Policy.PolishForDeclaration)
  214. return;
  215. if (D->hasAttrs()) {
  216. AttrVec &Attrs = D->getAttrs();
  217. for (AttrVec::const_iterator i=Attrs.begin(), e=Attrs.end(); i!=e; ++i) {
  218. Attr *A = *i;
  219. if (!hlsl::IsHLSLAttr(A->getKind())) // HLSL Change
  220. A->printPretty(Out, Policy);
  221. }
  222. }
  223. }
  224. void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) {
  225. // Normally, a PackExpansionType is written as T[3]... (for instance, as a
  226. // template argument), but if it is the type of a declaration, the ellipsis
  227. // is placed before the name being declared.
  228. if (auto *PET = T->getAs<PackExpansionType>()) {
  229. Pack = true;
  230. T = PET->getPattern();
  231. }
  232. T.print(Out, Policy, (Pack ? "..." : "") + DeclName);
  233. }
  234. void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
  235. this->Indent();
  236. Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
  237. Out << ";\n";
  238. Decls.clear();
  239. }
  240. void DeclPrinter::Print(AccessSpecifier AS) {
  241. switch(AS) {
  242. case AS_none: llvm_unreachable("No access specifier!");
  243. case AS_public: Out << "public"; break;
  244. case AS_protected: Out << "protected"; break;
  245. case AS_private: Out << "private"; break;
  246. }
  247. }
  248. //----------------------------------------------------------------------------
  249. // Common C declarations
  250. //----------------------------------------------------------------------------
  251. void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
  252. if (Policy.TerseOutput)
  253. return;
  254. if (Indent)
  255. Indentation += Policy.Indentation;
  256. SmallVector<Decl*, 2> Decls;
  257. for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
  258. D != DEnd; ++D) {
  259. // Don't print ObjCIvarDecls, as they are printed when visiting the
  260. // containing ObjCInterfaceDecl.
  261. if (isa<ObjCIvarDecl>(*D))
  262. continue;
  263. // Skip over implicit declarations in pretty-printing mode.
  264. if (D->isImplicit())
  265. continue;
  266. // The next bits of code handles stuff like "struct {int x;} a,b"; we're
  267. // forced to merge the declarations because there's no other way to
  268. // refer to the struct in question. This limited merging is safe without
  269. // a bunch of other checks because it only merges declarations directly
  270. // referring to the tag, not typedefs.
  271. //
  272. // Check whether the current declaration should be grouped with a previous
  273. // unnamed struct.
  274. QualType CurDeclType = getDeclType(*D);
  275. if (!Decls.empty() && !CurDeclType.isNull()) {
  276. QualType BaseType = GetBaseType(CurDeclType);
  277. if (!BaseType.isNull() && isa<ElaboratedType>(BaseType))
  278. BaseType = cast<ElaboratedType>(BaseType)->getNamedType();
  279. if (!BaseType.isNull() && isa<TagType>(BaseType) &&
  280. cast<TagType>(BaseType)->getDecl() == Decls[0]) {
  281. Decls.push_back(*D);
  282. continue;
  283. }
  284. }
  285. // If we have a merged group waiting to be handled, handle it now.
  286. if (!Decls.empty())
  287. ProcessDeclGroup(Decls);
  288. // If the current declaration is an unnamed tag type, save it
  289. // so we can merge it with the subsequent declaration(s) using it.
  290. if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
  291. Decls.push_back(*D);
  292. continue;
  293. }
  294. if (isa<AccessSpecDecl>(*D))
  295. if (!Policy.LangOpts.HLSL) { // HLSL Change - no access specifier for hlsl.
  296. Indentation -= Policy.Indentation;
  297. this->Indent();
  298. Print(D->getAccess());
  299. Out << ":\n";
  300. Indentation += Policy.Indentation;
  301. continue;
  302. }
  303. this->Indent();
  304. Visit(*D);
  305. // FIXME: Need to be able to tell the DeclPrinter when
  306. const char *Terminator = nullptr;
  307. if (isa<OMPThreadPrivateDecl>(*D))
  308. Terminator = nullptr;
  309. else if (isa<FunctionDecl>(*D) &&
  310. cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
  311. Terminator = nullptr;
  312. else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
  313. Terminator = nullptr;
  314. else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
  315. isa<ObjCImplementationDecl>(*D) ||
  316. isa<ObjCInterfaceDecl>(*D) ||
  317. isa<ObjCProtocolDecl>(*D) ||
  318. isa<ObjCCategoryImplDecl>(*D) ||
  319. isa<ObjCCategoryDecl>(*D))
  320. Terminator = nullptr;
  321. else if (isa<HLSLBufferDecl>(*D)) // HLSL Change
  322. Terminator = nullptr;
  323. else if (isa<EnumConstantDecl>(*D)) {
  324. DeclContext::decl_iterator Next = D;
  325. ++Next;
  326. if (Next != DEnd)
  327. Terminator = ",";
  328. } else
  329. Terminator = ";";
  330. if (Terminator)
  331. Out << Terminator;
  332. Out << "\n";
  333. }
  334. if (!Decls.empty())
  335. ProcessDeclGroup(Decls);
  336. if (Indent)
  337. Indentation -= Policy.Indentation;
  338. }
  339. void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
  340. VisitDeclContext(D, false);
  341. }
  342. void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
  343. if (!Policy.SuppressSpecifiers) {
  344. Out << "typedef ";
  345. if (D->isModulePrivate())
  346. Out << "__module_private__ ";
  347. }
  348. D->getTypeSourceInfo()->getType().print(Out, Policy, D->getName());
  349. PrintUnusualAnnotations(D); // HLSL Change
  350. prettyPrintAttributes(D);
  351. }
  352. void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
  353. Out << "using " << *D;
  354. PrintUnusualAnnotations(D); // HLSL Change
  355. prettyPrintAttributes(D);
  356. Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
  357. }
  358. void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
  359. if (!Policy.SuppressSpecifiers && D->isModulePrivate())
  360. Out << "__module_private__ ";
  361. Out << "enum ";
  362. if (D->isScoped()) {
  363. if (D->isScopedUsingClassTag())
  364. Out << "class ";
  365. else
  366. Out << "struct ";
  367. }
  368. Out << *D;
  369. if (D->isFixed())
  370. Out << " : " << D->getIntegerType().stream(Policy);
  371. if (D->isCompleteDefinition()) {
  372. Out << " {\n";
  373. VisitDeclContext(D);
  374. Indent() << "}";
  375. }
  376. PrintUnusualAnnotations(D); // HLSL Change
  377. prettyPrintAttributes(D);
  378. }
  379. void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
  380. if (!Policy.SuppressSpecifiers && D->isModulePrivate())
  381. Out << "__module_private__ ";
  382. Out << D->getKindName();
  383. prettyPrintAttributes(D);
  384. if (D->getIdentifier())
  385. Out << ' ' << *D;
  386. if (D->isCompleteDefinition()) {
  387. Out << " {\n";
  388. VisitDeclContext(D);
  389. Indent() << "}";
  390. }
  391. }
  392. void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
  393. Out << *D;
  394. if (Expr *Init = D->getInitExpr()) {
  395. Out << " = ";
  396. Init->printPretty(Out, nullptr, Policy, Indentation);
  397. }
  398. }
  399. void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
  400. CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
  401. CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
  402. if (!Policy.SuppressSpecifiers) {
  403. switch (D->getStorageClass()) {
  404. case SC_None: break;
  405. case SC_Extern: Out << "extern "; break;
  406. case SC_Static: Out << "static "; break;
  407. case SC_PrivateExtern: Out << "__private_extern__ "; break;
  408. case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal:
  409. llvm_unreachable("invalid for functions");
  410. }
  411. if (D->isInlineSpecified()) Out << "inline ";
  412. if (D->isVirtualAsWritten()) Out << "virtual ";
  413. if (D->isModulePrivate()) Out << "__module_private__ ";
  414. if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
  415. if ((CDecl && CDecl->isExplicitSpecified()) ||
  416. (ConversionDecl && ConversionDecl->isExplicit()))
  417. Out << "explicit ";
  418. }
  419. // HLSL Change Begin
  420. if (D->hasAttrs() && Policy.LangOpts.HLSL)
  421. PrintHLSLPreAttr(D);
  422. // HLSL Change End
  423. PrintingPolicy SubPolicy(Policy);
  424. SubPolicy.SuppressSpecifiers = false;
  425. std::string Proto = D->getNameInfo().getAsString();
  426. // HLSL Change Begin
  427. DeclContext *Namespace = D->getEnclosingNamespaceContext();
  428. DeclContext *Enclosing = D->getLexicalParent();
  429. if (!Enclosing->isNamespace() && Namespace->isNamespace()) {
  430. NamespaceDecl* ns = (NamespaceDecl*)Namespace;
  431. Proto = ns->getName().str() + "::" + Proto;
  432. }
  433. // HLSL Change End
  434. QualType Ty = D->getType();
  435. while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
  436. Proto = '(' + Proto + ')';
  437. Ty = PT->getInnerType();
  438. }
  439. if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
  440. const FunctionProtoType *FT = nullptr;
  441. if (D->hasWrittenPrototype())
  442. FT = dyn_cast<FunctionProtoType>(AFT);
  443. Proto += "(";
  444. if (FT) {
  445. llvm::raw_string_ostream POut(Proto);
  446. DeclPrinter ParamPrinter(POut, SubPolicy, Indentation);
  447. for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
  448. if (i) POut << ", ";
  449. ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
  450. }
  451. if (FT->isVariadic()) {
  452. if (D->getNumParams()) POut << ", ";
  453. POut << "...";
  454. }
  455. } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
  456. for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
  457. if (i)
  458. Proto += ", ";
  459. Proto += D->getParamDecl(i)->getNameAsString();
  460. }
  461. }
  462. Proto += ")";
  463. if (FT) {
  464. if (FT->isConst())
  465. Proto += " const";
  466. if (FT->isVolatile())
  467. Proto += " volatile";
  468. if (FT->isRestrict())
  469. Proto += " restrict";
  470. switch (FT->getRefQualifier()) {
  471. case RQ_None:
  472. break;
  473. case RQ_LValue:
  474. Proto += " &";
  475. break;
  476. case RQ_RValue:
  477. Proto += " &&";
  478. break;
  479. }
  480. }
  481. if (FT && FT->hasDynamicExceptionSpec()) {
  482. Proto += " throw(";
  483. if (FT->getExceptionSpecType() == EST_MSAny)
  484. Proto += "...";
  485. else
  486. for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
  487. if (I)
  488. Proto += ", ";
  489. Proto += FT->getExceptionType(I).getAsString(SubPolicy);
  490. }
  491. Proto += ")";
  492. } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
  493. Proto += " noexcept";
  494. if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
  495. Proto += "(";
  496. llvm::raw_string_ostream EOut(Proto);
  497. FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
  498. Indentation);
  499. EOut.flush();
  500. Proto += EOut.str();
  501. Proto += ")";
  502. }
  503. }
  504. if (CDecl) {
  505. bool HasInitializerList = false;
  506. for (const auto *BMInitializer : CDecl->inits()) {
  507. if (BMInitializer->isInClassMemberInitializer())
  508. continue;
  509. if (!HasInitializerList) {
  510. Proto += " : ";
  511. Out << Proto;
  512. Proto.clear();
  513. HasInitializerList = true;
  514. } else
  515. Out << ", ";
  516. if (BMInitializer->isAnyMemberInitializer()) {
  517. FieldDecl *FD = BMInitializer->getAnyMember();
  518. Out << *FD;
  519. } else {
  520. Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
  521. }
  522. Out << "(";
  523. if (!BMInitializer->getInit()) {
  524. // Nothing to print
  525. } else {
  526. Expr *Init = BMInitializer->getInit();
  527. if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
  528. Init = Tmp->getSubExpr();
  529. Init = Init->IgnoreParens();
  530. Expr *SimpleInit = nullptr;
  531. Expr **Args = nullptr;
  532. unsigned NumArgs = 0;
  533. if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
  534. Args = ParenList->getExprs();
  535. NumArgs = ParenList->getNumExprs();
  536. } else if (CXXConstructExpr *Construct
  537. = dyn_cast<CXXConstructExpr>(Init)) {
  538. Args = Construct->getArgs();
  539. NumArgs = Construct->getNumArgs();
  540. } else
  541. SimpleInit = Init;
  542. if (SimpleInit)
  543. SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
  544. else {
  545. for (unsigned I = 0; I != NumArgs; ++I) {
  546. assert(Args[I] != nullptr && "Expected non-null Expr");
  547. if (isa<CXXDefaultArgExpr>(Args[I]))
  548. break;
  549. if (I)
  550. Out << ", ";
  551. Args[I]->printPretty(Out, nullptr, Policy, Indentation);
  552. }
  553. }
  554. }
  555. Out << ")";
  556. if (BMInitializer->isPackExpansion())
  557. Out << "...";
  558. }
  559. } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
  560. if (FT && FT->hasTrailingReturn()) {
  561. Out << "auto " << Proto << " -> ";
  562. Proto.clear();
  563. }
  564. AFT->getReturnType().print(Out, Policy, Proto);
  565. Proto.clear();
  566. }
  567. Out << Proto;
  568. } else {
  569. Ty.print(Out, Policy, Proto);
  570. }
  571. PrintUnusualAnnotations(D); // HLSL Change
  572. prettyPrintAttributes(D);
  573. if (D->isPure())
  574. Out << " = 0";
  575. else if (D->isDeletedAsWritten())
  576. Out << " = delete";
  577. else if (D->isExplicitlyDefaulted())
  578. Out << " = default";
  579. else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) {
  580. if (!D->hasPrototype() && D->getNumParams()) {
  581. // This is a K&R function definition, so we need to print the
  582. // parameters.
  583. Out << '\n';
  584. DeclPrinter ParamPrinter(Out, SubPolicy, Indentation);
  585. Indentation += Policy.Indentation;
  586. for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
  587. Indent();
  588. ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
  589. Out << ";\n";
  590. }
  591. Indentation -= Policy.Indentation;
  592. } else
  593. Out << ' ';
  594. if (D->getBody())
  595. D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
  596. Out << '\n';
  597. }
  598. }
  599. void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
  600. if (TypeSourceInfo *TSI = D->getFriendType()) {
  601. unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
  602. for (unsigned i = 0; i < NumTPLists; ++i)
  603. PrintTemplateParameters(D->getFriendTypeTemplateParameterList(i));
  604. Out << "friend ";
  605. Out << " " << TSI->getType().getAsString(Policy);
  606. }
  607. else if (FunctionDecl *FD =
  608. dyn_cast<FunctionDecl>(D->getFriendDecl())) {
  609. Out << "friend ";
  610. VisitFunctionDecl(FD);
  611. }
  612. else if (FunctionTemplateDecl *FTD =
  613. dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
  614. Out << "friend ";
  615. VisitFunctionTemplateDecl(FTD);
  616. }
  617. else if (ClassTemplateDecl *CTD =
  618. dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
  619. Out << "friend ";
  620. VisitRedeclarableTemplateDecl(CTD);
  621. }
  622. }
  623. void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
  624. if (!Policy.SuppressSpecifiers && D->isMutable())
  625. Out << "mutable ";
  626. if (!Policy.SuppressSpecifiers && D->isModulePrivate())
  627. Out << "__module_private__ ";
  628. // HLSL Change Begin
  629. if (D->hasAttrs())
  630. PrintHLSLPreAttr(D);
  631. // HLSL Change End
  632. Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
  633. stream(Policy, D->getName());
  634. if (D->isBitField()) {
  635. Out << " : ";
  636. D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
  637. }
  638. Expr *Init = D->getInClassInitializer();
  639. if (!Policy.SuppressInitializers && Init) {
  640. if (D->getInClassInitStyle() == ICIS_ListInit)
  641. Out << " ";
  642. else
  643. Out << " = ";
  644. Init->printPretty(Out, nullptr, Policy, Indentation);
  645. }
  646. PrintUnusualAnnotations(D); // HLSL Change
  647. prettyPrintAttributes(D);
  648. }
  649. void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
  650. Out << *D << ":";
  651. }
  652. void DeclPrinter::VisitVarDecl(VarDecl *D) {
  653. if (!Policy.SuppressSpecifiers) {
  654. StorageClass SC = D->getStorageClass();
  655. if (SC != SC_None)
  656. Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
  657. switch (D->getTSCSpec()) {
  658. case TSCS_unspecified:
  659. break;
  660. case TSCS___thread:
  661. Out << "__thread ";
  662. break;
  663. case TSCS__Thread_local:
  664. Out << "_Thread_local ";
  665. break;
  666. case TSCS_thread_local:
  667. Out << "thread_local ";
  668. break;
  669. }
  670. if (D->isModulePrivate())
  671. Out << "__module_private__ ";
  672. }
  673. // HLSL Change Begin
  674. if (D->hasAttrs() && Policy.LangOpts.HLSL)
  675. PrintHLSLPreAttr(D);
  676. // HLSL Change End
  677. QualType T = D->getTypeSourceInfo()
  678. ? D->getTypeSourceInfo()->getType()
  679. : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
  680. // HLSL Change Begin
  681. if (D->hasAttrs() && Policy.LangOpts.HLSL) {
  682. printDeclType(T.getNonReferenceType(), D->getName());
  683. }
  684. else {
  685. printDeclType(T, D->getName());
  686. }
  687. // HLSL Change end
  688. Expr *Init = D->getInit();
  689. if (!Policy.SuppressInitializers && Init) {
  690. bool ImplicitInit = false;
  691. if (CXXConstructExpr *Construct =
  692. dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
  693. if (D->getInitStyle() == VarDecl::CallInit &&
  694. !Construct->isListInitialization()) {
  695. ImplicitInit = Construct->getNumArgs() == 0 ||
  696. Construct->getArg(0)->isDefaultArgument();
  697. }
  698. }
  699. if (!ImplicitInit) {
  700. if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
  701. Out << "(";
  702. else if (D->getInitStyle() == VarDecl::CInit) {
  703. Out << " = ";
  704. }
  705. Init->printPretty(Out, nullptr, Policy, Indentation);
  706. if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
  707. Out << ")";
  708. }
  709. }
  710. PrintUnusualAnnotations(D); // HLSL Change
  711. prettyPrintAttributes(D);
  712. }
  713. void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
  714. VisitVarDecl(D);
  715. }
  716. void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
  717. Out << "__asm (";
  718. D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
  719. Out << ")";
  720. }
  721. void DeclPrinter::VisitImportDecl(ImportDecl *D) {
  722. Out << "@import " << D->getImportedModule()->getFullModuleName()
  723. << ";\n";
  724. }
  725. void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
  726. Out << "static_assert(";
  727. D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
  728. if (StringLiteral *SL = D->getMessage()) {
  729. Out << ", ";
  730. SL->printPretty(Out, nullptr, Policy, Indentation);
  731. }
  732. Out << ")";
  733. }
  734. //----------------------------------------------------------------------------
  735. // C++ declarations
  736. //----------------------------------------------------------------------------
  737. void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
  738. if (D->isInline())
  739. Out << "inline ";
  740. Out << "namespace " << *D << " {\n";
  741. VisitDeclContext(D);
  742. Indent() << "}";
  743. }
  744. void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
  745. Out << "using namespace ";
  746. if (D->getQualifier())
  747. D->getQualifier()->print(Out, Policy);
  748. Out << *D->getNominatedNamespaceAsWritten();
  749. }
  750. void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
  751. Out << "namespace " << *D << " = ";
  752. if (D->getQualifier())
  753. D->getQualifier()->print(Out, Policy);
  754. Out << *D->getAliasedNamespace();
  755. }
  756. void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
  757. prettyPrintAttributes(D);
  758. }
  759. void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
  760. if (!Policy.SuppressSpecifiers && D->isModulePrivate())
  761. Out << "__module_private__ ";
  762. // HLSL Change Begin
  763. if (!Policy.LangOpts.HLSL || !D->isInterface()) {
  764. Out << D->getKindName();
  765. }
  766. else {
  767. Out << "interface";
  768. }
  769. // HLSL Change End
  770. PrintUnusualAnnotations(D); // HLSL Change
  771. prettyPrintAttributes(D);
  772. if (D->getIdentifier())
  773. Out << ' ' << *D;
  774. if (D->isCompleteDefinition()) {
  775. // Print the base classes
  776. if (D->getNumBases()) {
  777. Out << " : ";
  778. for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
  779. BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
  780. if (Base != D->bases_begin())
  781. Out << ", ";
  782. if (Base->isVirtual())
  783. Out << "virtual ";
  784. AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
  785. if (AS != AS_none
  786. && !Policy.LangOpts.HLSL // HLSL Change - no access specifier for hlsl.
  787. ) {
  788. Print(AS);
  789. Out << " ";
  790. }
  791. Out << Base->getType().getAsString(Policy);
  792. if (Base->isPackExpansion())
  793. Out << "...";
  794. }
  795. }
  796. // Print the class definition
  797. // FIXME: Doesn't print access specifiers, e.g., "public:"
  798. Out << " {\n";
  799. VisitDeclContext(D);
  800. Indent() << "}";
  801. }
  802. }
  803. void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
  804. const char *l;
  805. if (D->getLanguage() == LinkageSpecDecl::lang_c)
  806. l = "C";
  807. else {
  808. assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
  809. "unknown language in linkage specification");
  810. l = "C++";
  811. }
  812. Out << "extern \"" << l << "\" ";
  813. if (D->hasBraces()) {
  814. Out << "{\n";
  815. VisitDeclContext(D);
  816. Indent() << "}";
  817. } else
  818. Visit(*D->decls_begin());
  819. }
  820. void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params,
  821. const TemplateArgumentList *Args) {
  822. assert(Params);
  823. assert(!Args || Params->size() == Args->size());
  824. Out << "template <";
  825. for (unsigned i = 0, e = Params->size(); i != e; ++i) {
  826. if (i != 0)
  827. Out << ", ";
  828. const Decl *Param = Params->getParam(i);
  829. if (const TemplateTypeParmDecl *TTP =
  830. dyn_cast<TemplateTypeParmDecl>(Param)) {
  831. if (TTP->wasDeclaredWithTypename())
  832. Out << "typename ";
  833. else
  834. Out << "class ";
  835. if (TTP->isParameterPack())
  836. Out << "...";
  837. Out << *TTP;
  838. if (Args) {
  839. Out << " = ";
  840. Args->get(i).print(Policy, Out);
  841. } else if (TTP->hasDefaultArgument()) {
  842. Out << " = ";
  843. Out << TTP->getDefaultArgument().getAsString(Policy);
  844. };
  845. } else if (const NonTypeTemplateParmDecl *NTTP =
  846. dyn_cast<NonTypeTemplateParmDecl>(Param)) {
  847. StringRef Name;
  848. if (IdentifierInfo *II = NTTP->getIdentifier())
  849. Name = II->getName();
  850. printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
  851. if (Args) {
  852. Out << " = ";
  853. Args->get(i).print(Policy, Out);
  854. } else if (NTTP->hasDefaultArgument()) {
  855. Out << " = ";
  856. NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
  857. Indentation);
  858. }
  859. } else if (const TemplateTemplateParmDecl *TTPD =
  860. dyn_cast<TemplateTemplateParmDecl>(Param)) {
  861. VisitTemplateDecl(TTPD);
  862. // FIXME: print the default argument, if present.
  863. }
  864. }
  865. Out << "> ";
  866. }
  867. void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
  868. PrintTemplateParameters(D->getTemplateParameters());
  869. if (const TemplateTemplateParmDecl *TTP =
  870. dyn_cast<TemplateTemplateParmDecl>(D)) {
  871. Out << "class ";
  872. if (TTP->isParameterPack())
  873. Out << "...";
  874. Out << D->getName();
  875. } else {
  876. Visit(D->getTemplatedDecl());
  877. }
  878. }
  879. void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
  880. if (PrintInstantiation) {
  881. TemplateParameterList *Params = D->getTemplateParameters();
  882. for (auto *I : D->specializations()) {
  883. PrintTemplateParameters(Params, I->getTemplateSpecializationArgs());
  884. Visit(I);
  885. }
  886. }
  887. return VisitRedeclarableTemplateDecl(D);
  888. }
  889. void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
  890. if (PrintInstantiation) {
  891. TemplateParameterList *Params = D->getTemplateParameters();
  892. for (auto *I : D->specializations()) {
  893. PrintTemplateParameters(Params, &I->getTemplateArgs());
  894. Visit(I);
  895. Out << '\n';
  896. }
  897. }
  898. return VisitRedeclarableTemplateDecl(D);
  899. }
  900. //----------------------------------------------------------------------------
  901. // Objective-C declarations
  902. //----------------------------------------------------------------------------
  903. void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
  904. Decl::ObjCDeclQualifier Quals,
  905. QualType T) {
  906. Out << '(';
  907. if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
  908. Out << "in ";
  909. if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
  910. Out << "inout ";
  911. if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
  912. Out << "out ";
  913. if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
  914. Out << "bycopy ";
  915. if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
  916. Out << "byref ";
  917. if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
  918. Out << "oneway ";
  919. if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
  920. if (auto nullability = AttributedType::stripOuterNullability(T))
  921. Out << getNullabilitySpelling(*nullability, true) << ' ';
  922. }
  923. Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
  924. Out << ')';
  925. }
  926. void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
  927. Out << "<";
  928. unsigned First = true;
  929. for (auto *Param : *Params) {
  930. if (First) {
  931. First = false;
  932. } else {
  933. Out << ", ";
  934. }
  935. switch (Param->getVariance()) {
  936. case ObjCTypeParamVariance::Invariant:
  937. break;
  938. case ObjCTypeParamVariance::Covariant:
  939. Out << "__covariant ";
  940. break;
  941. case ObjCTypeParamVariance::Contravariant:
  942. Out << "__contravariant ";
  943. break;
  944. }
  945. Out << Param->getDeclName().getAsString();
  946. if (Param->hasExplicitBound()) {
  947. Out << " : " << Param->getUnderlyingType().getAsString(Policy);
  948. }
  949. }
  950. Out << ">";
  951. }
  952. void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
  953. if (OMD->isInstanceMethod())
  954. Out << "- ";
  955. else
  956. Out << "+ ";
  957. if (!OMD->getReturnType().isNull()) {
  958. PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
  959. OMD->getReturnType());
  960. }
  961. std::string name = OMD->getSelector().getAsString();
  962. std::string::size_type pos, lastPos = 0;
  963. for (const auto *PI : OMD->params()) {
  964. // FIXME: selector is missing here!
  965. pos = name.find_first_of(':', lastPos);
  966. Out << " " << name.substr(lastPos, pos - lastPos) << ':';
  967. PrintObjCMethodType(OMD->getASTContext(),
  968. PI->getObjCDeclQualifier(),
  969. PI->getType());
  970. Out << *PI;
  971. lastPos = pos + 1;
  972. }
  973. if (OMD->param_begin() == OMD->param_end())
  974. Out << " " << name;
  975. if (OMD->isVariadic())
  976. Out << ", ...";
  977. prettyPrintAttributes(OMD);
  978. if (OMD->getBody() && !Policy.TerseOutput) {
  979. Out << ' ';
  980. OMD->getBody()->printPretty(Out, nullptr, Policy);
  981. }
  982. else if (Policy.PolishForDeclaration)
  983. Out << ';';
  984. }
  985. void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
  986. std::string I = OID->getNameAsString();
  987. ObjCInterfaceDecl *SID = OID->getSuperClass();
  988. bool eolnOut = false;
  989. if (SID)
  990. Out << "@implementation " << I << " : " << *SID;
  991. else
  992. Out << "@implementation " << I;
  993. if (OID->ivar_size() > 0) {
  994. Out << "{\n";
  995. eolnOut = true;
  996. Indentation += Policy.Indentation;
  997. for (const auto *I : OID->ivars()) {
  998. Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
  999. getAsString(Policy) << ' ' << *I << ";\n";
  1000. }
  1001. Indentation -= Policy.Indentation;
  1002. Out << "}\n";
  1003. }
  1004. else if (SID || (OID->decls_begin() != OID->decls_end())) {
  1005. Out << "\n";
  1006. eolnOut = true;
  1007. }
  1008. VisitDeclContext(OID, false);
  1009. if (!eolnOut)
  1010. Out << "\n";
  1011. Out << "@end";
  1012. }
  1013. void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
  1014. std::string I = OID->getNameAsString();
  1015. ObjCInterfaceDecl *SID = OID->getSuperClass();
  1016. if (!OID->isThisDeclarationADefinition()) {
  1017. Out << "@class " << I;
  1018. if (auto TypeParams = OID->getTypeParamListAsWritten()) {
  1019. PrintObjCTypeParams(TypeParams);
  1020. }
  1021. Out << ";";
  1022. return;
  1023. }
  1024. bool eolnOut = false;
  1025. Out << "@interface " << I;
  1026. if (auto TypeParams = OID->getTypeParamListAsWritten()) {
  1027. PrintObjCTypeParams(TypeParams);
  1028. }
  1029. if (SID)
  1030. Out << " : " << OID->getSuperClass()->getName();
  1031. // Protocols?
  1032. const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
  1033. if (!Protocols.empty()) {
  1034. for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
  1035. E = Protocols.end(); I != E; ++I)
  1036. Out << (I == Protocols.begin() ? '<' : ',') << **I;
  1037. Out << "> ";
  1038. }
  1039. if (OID->ivar_size() > 0) {
  1040. Out << "{\n";
  1041. eolnOut = true;
  1042. Indentation += Policy.Indentation;
  1043. for (const auto *I : OID->ivars()) {
  1044. Indent() << I->getASTContext()
  1045. .getUnqualifiedObjCPointerType(I->getType())
  1046. .getAsString(Policy) << ' ' << *I << ";\n";
  1047. }
  1048. Indentation -= Policy.Indentation;
  1049. Out << "}\n";
  1050. }
  1051. else if (SID || (OID->decls_begin() != OID->decls_end())) {
  1052. Out << "\n";
  1053. eolnOut = true;
  1054. }
  1055. VisitDeclContext(OID, false);
  1056. if (!eolnOut)
  1057. Out << "\n";
  1058. Out << "@end";
  1059. // FIXME: implement the rest...
  1060. }
  1061. void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
  1062. if (!PID->isThisDeclarationADefinition()) {
  1063. Out << "@protocol " << *PID << ";\n";
  1064. return;
  1065. }
  1066. // Protocols?
  1067. const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
  1068. if (!Protocols.empty()) {
  1069. Out << "@protocol " << *PID;
  1070. for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
  1071. E = Protocols.end(); I != E; ++I)
  1072. Out << (I == Protocols.begin() ? '<' : ',') << **I;
  1073. Out << ">\n";
  1074. } else
  1075. Out << "@protocol " << *PID << '\n';
  1076. VisitDeclContext(PID, false);
  1077. Out << "@end";
  1078. }
  1079. void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
  1080. Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
  1081. VisitDeclContext(PID, false);
  1082. Out << "@end";
  1083. // FIXME: implement the rest...
  1084. }
  1085. void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
  1086. Out << "@interface " << *PID->getClassInterface();
  1087. if (auto TypeParams = PID->getTypeParamList()) {
  1088. PrintObjCTypeParams(TypeParams);
  1089. }
  1090. Out << "(" << *PID << ")\n";
  1091. if (PID->ivar_size() > 0) {
  1092. Out << "{\n";
  1093. Indentation += Policy.Indentation;
  1094. for (const auto *I : PID->ivars())
  1095. Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
  1096. getAsString(Policy) << ' ' << *I << ";\n";
  1097. Indentation -= Policy.Indentation;
  1098. Out << "}\n";
  1099. }
  1100. VisitDeclContext(PID, false);
  1101. Out << "@end";
  1102. // FIXME: implement the rest...
  1103. }
  1104. void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
  1105. Out << "@compatibility_alias " << *AID
  1106. << ' ' << *AID->getClassInterface() << ";\n";
  1107. }
  1108. /// PrintObjCPropertyDecl - print a property declaration.
  1109. ///
  1110. void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
  1111. if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
  1112. Out << "@required\n";
  1113. else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
  1114. Out << "@optional\n";
  1115. QualType T = PDecl->getType();
  1116. Out << "@property";
  1117. if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
  1118. bool first = true;
  1119. Out << " (";
  1120. if (PDecl->getPropertyAttributes() &
  1121. ObjCPropertyDecl::OBJC_PR_readonly) {
  1122. Out << (first ? ' ' : ',') << "readonly";
  1123. first = false;
  1124. }
  1125. if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
  1126. Out << (first ? ' ' : ',') << "getter = ";
  1127. PDecl->getGetterName().print(Out);
  1128. first = false;
  1129. }
  1130. if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
  1131. Out << (first ? ' ' : ',') << "setter = ";
  1132. PDecl->getSetterName().print(Out);
  1133. first = false;
  1134. }
  1135. if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
  1136. Out << (first ? ' ' : ',') << "assign";
  1137. first = false;
  1138. }
  1139. if (PDecl->getPropertyAttributes() &
  1140. ObjCPropertyDecl::OBJC_PR_readwrite) {
  1141. Out << (first ? ' ' : ',') << "readwrite";
  1142. first = false;
  1143. }
  1144. if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
  1145. Out << (first ? ' ' : ',') << "retain";
  1146. first = false;
  1147. }
  1148. if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
  1149. Out << (first ? ' ' : ',') << "strong";
  1150. first = false;
  1151. }
  1152. if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
  1153. Out << (first ? ' ' : ',') << "copy";
  1154. first = false;
  1155. }
  1156. if (PDecl->getPropertyAttributes() &
  1157. ObjCPropertyDecl::OBJC_PR_nonatomic) {
  1158. Out << (first ? ' ' : ',') << "nonatomic";
  1159. first = false;
  1160. }
  1161. if (PDecl->getPropertyAttributes() &
  1162. ObjCPropertyDecl::OBJC_PR_atomic) {
  1163. Out << (first ? ' ' : ',') << "atomic";
  1164. first = false;
  1165. }
  1166. if (PDecl->getPropertyAttributes() &
  1167. ObjCPropertyDecl::OBJC_PR_nullability) {
  1168. if (auto nullability = AttributedType::stripOuterNullability(T)) {
  1169. if (*nullability == NullabilityKind::Unspecified &&
  1170. (PDecl->getPropertyAttributes() &
  1171. ObjCPropertyDecl::OBJC_PR_null_resettable)) {
  1172. Out << (first ? ' ' : ',') << "null_resettable";
  1173. } else {
  1174. Out << (first ? ' ' : ',')
  1175. << getNullabilitySpelling(*nullability, true);
  1176. }
  1177. first = false;
  1178. }
  1179. }
  1180. (void) first; // Silence dead store warning due to idiomatic code.
  1181. Out << " )";
  1182. }
  1183. Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
  1184. getAsString(Policy) << ' ' << *PDecl;
  1185. if (Policy.PolishForDeclaration)
  1186. Out << ';';
  1187. }
  1188. void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
  1189. if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
  1190. Out << "@synthesize ";
  1191. else
  1192. Out << "@dynamic ";
  1193. Out << *PID->getPropertyDecl();
  1194. if (PID->getPropertyIvarDecl())
  1195. Out << '=' << *PID->getPropertyIvarDecl();
  1196. }
  1197. void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
  1198. if (!D->isAccessDeclaration())
  1199. Out << "using ";
  1200. if (D->hasTypename())
  1201. Out << "typename ";
  1202. D->getQualifier()->print(Out, Policy);
  1203. Out << *D;
  1204. }
  1205. void
  1206. DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
  1207. Out << "using typename ";
  1208. D->getQualifier()->print(Out, Policy);
  1209. Out << D->getDeclName();
  1210. }
  1211. void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
  1212. if (!D->isAccessDeclaration())
  1213. Out << "using ";
  1214. D->getQualifier()->print(Out, Policy);
  1215. Out << D->getName();
  1216. }
  1217. void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
  1218. // ignore
  1219. }
  1220. void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
  1221. Out << "#pragma omp threadprivate";
  1222. if (!D->varlist_empty()) {
  1223. for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
  1224. E = D->varlist_end();
  1225. I != E; ++I) {
  1226. Out << (I == D->varlist_begin() ? '(' : ',');
  1227. NamedDecl *ND = cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
  1228. ND->printQualifiedName(Out);
  1229. }
  1230. Out << ")";
  1231. }
  1232. }
  1233. // HLSL Change Begin
  1234. void DeclPrinter::VisitHLSLBufferDecl(HLSLBufferDecl *D) {
  1235. if (D->isCBuffer()) {
  1236. Out << "cbuffer ";
  1237. }
  1238. else {
  1239. Out << "tbuffer ";
  1240. }
  1241. Out << *D;
  1242. PrintUnusualAnnotations(D);
  1243. prettyPrintAttributes(D);
  1244. Out << " {\n";
  1245. VisitDeclContext(D);
  1246. Indent() << "}";
  1247. }
  1248. void DeclPrinter::PrintUnusualAnnotations(NamedDecl* D) {
  1249. if (D->isInvalidDecl())
  1250. return;
  1251. ArrayRef<hlsl::UnusualAnnotation *> Annotations = D->getUnusualAnnotations();
  1252. if (!Annotations.empty()) {
  1253. for (auto i = Annotations.begin(), e = Annotations.end(); i != e; ++i) {
  1254. VisitHLSLUnusualAnnotation(*i);
  1255. }
  1256. }
  1257. }
  1258. void DeclPrinter::VisitHLSLUnusualAnnotation(const hlsl::UnusualAnnotation *UA) {
  1259. switch (UA->getKind()) {
  1260. case hlsl::UnusualAnnotation::UA_SemanticDecl: {
  1261. const hlsl::SemanticDecl * semdecl = dyn_cast<hlsl::SemanticDecl>(UA);
  1262. Out << " : " << semdecl->SemanticName.str();
  1263. break;
  1264. }
  1265. case hlsl::UnusualAnnotation::UA_RegisterAssignment: {
  1266. const hlsl::RegisterAssignment * ra = dyn_cast<hlsl::RegisterAssignment>(UA);
  1267. if (ra->RegisterType) {
  1268. Out << " : register(";
  1269. if (!ra->ShaderProfile.empty()) {
  1270. Out << ra->ShaderProfile.str() << ", ";
  1271. }
  1272. Out << ra->RegisterType << ra->RegisterNumber;
  1273. if (ra->RegisterOffset) {
  1274. Out << "[" << ra->RegisterOffset << "]";
  1275. }
  1276. Out << ")";
  1277. }
  1278. break;
  1279. }
  1280. case hlsl::UnusualAnnotation::UA_ConstantPacking: {
  1281. const hlsl::ConstantPacking * cp = dyn_cast<hlsl::ConstantPacking>(UA);
  1282. Out << " : packoffset(c" << cp->Subcomponent; //packing applies to constant registers (c) only
  1283. if (cp->ComponentOffset) {
  1284. switch (cp->ComponentOffset) {
  1285. case 1:
  1286. Out << ".y";
  1287. break;
  1288. case 2:
  1289. Out << ".z";
  1290. break;
  1291. case 3:
  1292. Out << ".w";
  1293. break;
  1294. }
  1295. }
  1296. Out << ")";
  1297. break;
  1298. }
  1299. }
  1300. }
  1301. void DeclPrinter::PrintHLSLPreAttr(NamedDecl* D) {
  1302. AttrVec &Attrs = D->getAttrs();
  1303. std::vector<Attr*> tempVec;
  1304. for (AttrVec::const_reverse_iterator i = Attrs.rbegin(), e = Attrs.rend(); i != e; ++i) {
  1305. Attr *A = *i;
  1306. hlsl::CustomPrintHLSLAttr(A, Out, Policy, Indentation);
  1307. }
  1308. }
  1309. // HLSL Change End