DeclPrinter.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  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. && !Policy.LangOpts.HLSL // HLSL Change - no access specifier for hlsl.
  296. ) {
  297. Indentation -= Policy.Indentation;
  298. this->Indent();
  299. Print(D->getAccess());
  300. Out << ":\n";
  301. Indentation += Policy.Indentation;
  302. continue;
  303. }
  304. this->Indent();
  305. Visit(*D);
  306. // FIXME: Need to be able to tell the DeclPrinter when
  307. const char *Terminator = nullptr;
  308. if (isa<OMPThreadPrivateDecl>(*D))
  309. Terminator = nullptr;
  310. else if (isa<FunctionDecl>(*D) &&
  311. cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
  312. Terminator = nullptr;
  313. else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
  314. Terminator = nullptr;
  315. else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
  316. isa<ObjCImplementationDecl>(*D) ||
  317. isa<ObjCInterfaceDecl>(*D) ||
  318. isa<ObjCProtocolDecl>(*D) ||
  319. isa<ObjCCategoryImplDecl>(*D) ||
  320. isa<ObjCCategoryDecl>(*D))
  321. Terminator = nullptr;
  322. else if (isa<HLSLBufferDecl>(*D)) // HLSL Change
  323. Terminator = nullptr;
  324. else if (isa<EnumConstantDecl>(*D)) {
  325. DeclContext::decl_iterator Next = D;
  326. ++Next;
  327. if (Next != DEnd)
  328. Terminator = ",";
  329. } else
  330. Terminator = ";";
  331. if (Terminator)
  332. Out << Terminator;
  333. Out << "\n";
  334. }
  335. if (!Decls.empty())
  336. ProcessDeclGroup(Decls);
  337. if (Indent)
  338. Indentation -= Policy.Indentation;
  339. }
  340. void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
  341. VisitDeclContext(D, false);
  342. }
  343. void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
  344. if (!Policy.SuppressSpecifiers) {
  345. Out << "typedef ";
  346. if (D->isModulePrivate())
  347. Out << "__module_private__ ";
  348. }
  349. D->getTypeSourceInfo()->getType().print(Out, Policy, D->getName());
  350. PrintUnusualAnnotations(D); // HLSL Change
  351. prettyPrintAttributes(D);
  352. }
  353. void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
  354. Out << "using " << *D;
  355. PrintUnusualAnnotations(D); // HLSL Change
  356. prettyPrintAttributes(D);
  357. Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
  358. }
  359. void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
  360. if (!Policy.SuppressSpecifiers && D->isModulePrivate())
  361. Out << "__module_private__ ";
  362. Out << "enum ";
  363. if (D->isScoped()) {
  364. if (D->isScopedUsingClassTag())
  365. Out << "class ";
  366. else
  367. Out << "struct ";
  368. }
  369. Out << *D;
  370. if (D->isFixed())
  371. Out << " : " << D->getIntegerType().stream(Policy);
  372. if (D->isCompleteDefinition()) {
  373. Out << " {\n";
  374. VisitDeclContext(D);
  375. Indent() << "}";
  376. }
  377. PrintUnusualAnnotations(D); // HLSL Change
  378. prettyPrintAttributes(D);
  379. }
  380. void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
  381. if (!Policy.SuppressSpecifiers && D->isModulePrivate())
  382. Out << "__module_private__ ";
  383. Out << D->getKindName();
  384. prettyPrintAttributes(D);
  385. if (D->getIdentifier())
  386. Out << ' ' << *D;
  387. if (D->isCompleteDefinition()) {
  388. Out << " {\n";
  389. VisitDeclContext(D);
  390. Indent() << "}";
  391. }
  392. }
  393. void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
  394. Out << *D;
  395. if (Expr *Init = D->getInitExpr()) {
  396. Out << " = ";
  397. Init->printPretty(Out, nullptr, Policy, Indentation);
  398. }
  399. }
  400. void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
  401. CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
  402. CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
  403. if (!Policy.SuppressSpecifiers) {
  404. switch (D->getStorageClass()) {
  405. case SC_None: break;
  406. case SC_Extern: Out << "extern "; break;
  407. case SC_Static: Out << "static "; break;
  408. case SC_PrivateExtern: Out << "__private_extern__ "; break;
  409. case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal:
  410. llvm_unreachable("invalid for functions");
  411. }
  412. if (D->isInlineSpecified()) Out << "inline ";
  413. if (D->isVirtualAsWritten()) Out << "virtual ";
  414. if (D->isModulePrivate()) Out << "__module_private__ ";
  415. if (D->isConstexpr() && !D->isExplicitlyDefaulted()) Out << "constexpr ";
  416. if ((CDecl && CDecl->isExplicitSpecified()) ||
  417. (ConversionDecl && ConversionDecl->isExplicit()))
  418. Out << "explicit ";
  419. }
  420. // HLSL Change Begin
  421. if (D->hasAttrs() && Policy.LangOpts.HLSL)
  422. PrintHLSLPreAttr(D);
  423. // HLSL Change End
  424. PrintingPolicy SubPolicy(Policy);
  425. SubPolicy.SuppressSpecifiers = false;
  426. std::string Proto = D->getNameInfo().getAsString();
  427. // HLSL Change Begin
  428. DeclContext *Namespace = D->getEnclosingNamespaceContext();
  429. DeclContext *Enclosing = D->getLexicalParent();
  430. if (!Enclosing->isNamespace() && Namespace->isNamespace()) {
  431. NamespaceDecl* ns = (NamespaceDecl*)Namespace;
  432. Proto = ns->getName().str() + "::" + Proto;
  433. }
  434. // HLSL Change End
  435. QualType Ty = D->getType();
  436. while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
  437. Proto = '(' + Proto + ')';
  438. Ty = PT->getInnerType();
  439. }
  440. if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
  441. const FunctionProtoType *FT = nullptr;
  442. if (D->hasWrittenPrototype())
  443. FT = dyn_cast<FunctionProtoType>(AFT);
  444. Proto += "(";
  445. if (FT) {
  446. llvm::raw_string_ostream POut(Proto);
  447. DeclPrinter ParamPrinter(POut, SubPolicy, Indentation);
  448. for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
  449. if (i) POut << ", ";
  450. ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
  451. }
  452. if (FT->isVariadic()) {
  453. if (D->getNumParams()) POut << ", ";
  454. POut << "...";
  455. }
  456. } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
  457. for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
  458. if (i)
  459. Proto += ", ";
  460. Proto += D->getParamDecl(i)->getNameAsString();
  461. }
  462. }
  463. Proto += ")";
  464. if (FT) {
  465. if (FT->isConst())
  466. Proto += " const";
  467. if (FT->isVolatile())
  468. Proto += " volatile";
  469. if (FT->isRestrict())
  470. Proto += " restrict";
  471. switch (FT->getRefQualifier()) {
  472. case RQ_None:
  473. break;
  474. case RQ_LValue:
  475. Proto += " &";
  476. break;
  477. case RQ_RValue:
  478. Proto += " &&";
  479. break;
  480. }
  481. }
  482. if (FT && FT->hasDynamicExceptionSpec()) {
  483. Proto += " throw(";
  484. if (FT->getExceptionSpecType() == EST_MSAny)
  485. Proto += "...";
  486. else
  487. for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
  488. if (I)
  489. Proto += ", ";
  490. Proto += FT->getExceptionType(I).getAsString(SubPolicy);
  491. }
  492. Proto += ")";
  493. } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
  494. Proto += " noexcept";
  495. if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
  496. Proto += "(";
  497. llvm::raw_string_ostream EOut(Proto);
  498. FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
  499. Indentation);
  500. EOut.flush();
  501. Proto += EOut.str();
  502. Proto += ")";
  503. }
  504. }
  505. if (CDecl) {
  506. bool HasInitializerList = false;
  507. for (const auto *BMInitializer : CDecl->inits()) {
  508. if (BMInitializer->isInClassMemberInitializer())
  509. continue;
  510. if (!HasInitializerList) {
  511. Proto += " : ";
  512. Out << Proto;
  513. Proto.clear();
  514. HasInitializerList = true;
  515. } else
  516. Out << ", ";
  517. if (BMInitializer->isAnyMemberInitializer()) {
  518. FieldDecl *FD = BMInitializer->getAnyMember();
  519. Out << *FD;
  520. } else {
  521. Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
  522. }
  523. Out << "(";
  524. if (!BMInitializer->getInit()) {
  525. // Nothing to print
  526. } else {
  527. Expr *Init = BMInitializer->getInit();
  528. if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
  529. Init = Tmp->getSubExpr();
  530. Init = Init->IgnoreParens();
  531. Expr *SimpleInit = nullptr;
  532. Expr **Args = nullptr;
  533. unsigned NumArgs = 0;
  534. if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
  535. Args = ParenList->getExprs();
  536. NumArgs = ParenList->getNumExprs();
  537. } else if (CXXConstructExpr *Construct
  538. = dyn_cast<CXXConstructExpr>(Init)) {
  539. Args = Construct->getArgs();
  540. NumArgs = Construct->getNumArgs();
  541. } else
  542. SimpleInit = Init;
  543. if (SimpleInit)
  544. SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
  545. else {
  546. for (unsigned I = 0; I != NumArgs; ++I) {
  547. assert(Args[I] != nullptr && "Expected non-null Expr");
  548. if (isa<CXXDefaultArgExpr>(Args[I]))
  549. break;
  550. if (I)
  551. Out << ", ";
  552. Args[I]->printPretty(Out, nullptr, Policy, Indentation);
  553. }
  554. }
  555. }
  556. Out << ")";
  557. if (BMInitializer->isPackExpansion())
  558. Out << "...";
  559. }
  560. } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
  561. if (FT && FT->hasTrailingReturn()) {
  562. Out << "auto " << Proto << " -> ";
  563. Proto.clear();
  564. }
  565. AFT->getReturnType().print(Out, Policy, Proto);
  566. Proto.clear();
  567. }
  568. Out << Proto;
  569. } else {
  570. Ty.print(Out, Policy, Proto);
  571. }
  572. PrintUnusualAnnotations(D); // HLSL Change
  573. prettyPrintAttributes(D);
  574. if (D->isPure())
  575. Out << " = 0";
  576. else if (D->isDeletedAsWritten())
  577. Out << " = delete";
  578. else if (D->isExplicitlyDefaulted())
  579. Out << " = default";
  580. else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) {
  581. if (!D->hasPrototype() && D->getNumParams()) {
  582. // This is a K&R function definition, so we need to print the
  583. // parameters.
  584. Out << '\n';
  585. DeclPrinter ParamPrinter(Out, SubPolicy, Indentation);
  586. Indentation += Policy.Indentation;
  587. for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
  588. Indent();
  589. ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
  590. Out << ";\n";
  591. }
  592. Indentation -= Policy.Indentation;
  593. } else
  594. Out << ' ';
  595. if (D->getBody())
  596. D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
  597. Out << '\n';
  598. }
  599. }
  600. void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
  601. if (TypeSourceInfo *TSI = D->getFriendType()) {
  602. unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
  603. for (unsigned i = 0; i < NumTPLists; ++i)
  604. PrintTemplateParameters(D->getFriendTypeTemplateParameterList(i));
  605. Out << "friend ";
  606. Out << " " << TSI->getType().getAsString(Policy);
  607. }
  608. else if (FunctionDecl *FD =
  609. dyn_cast<FunctionDecl>(D->getFriendDecl())) {
  610. Out << "friend ";
  611. VisitFunctionDecl(FD);
  612. }
  613. else if (FunctionTemplateDecl *FTD =
  614. dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
  615. Out << "friend ";
  616. VisitFunctionTemplateDecl(FTD);
  617. }
  618. else if (ClassTemplateDecl *CTD =
  619. dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
  620. Out << "friend ";
  621. VisitRedeclarableTemplateDecl(CTD);
  622. }
  623. }
  624. void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
  625. if (!Policy.SuppressSpecifiers && D->isMutable())
  626. Out << "mutable ";
  627. if (!Policy.SuppressSpecifiers && D->isModulePrivate())
  628. Out << "__module_private__ ";
  629. // HLSL Change Begin
  630. if (D->hasAttrs())
  631. PrintHLSLPreAttr(D);
  632. // HLSL Change End
  633. Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
  634. stream(Policy, D->getName());
  635. if (D->isBitField()) {
  636. Out << " : ";
  637. D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
  638. }
  639. Expr *Init = D->getInClassInitializer();
  640. if (!Policy.SuppressInitializers && Init) {
  641. if (D->getInClassInitStyle() == ICIS_ListInit)
  642. Out << " ";
  643. else
  644. Out << " = ";
  645. Init->printPretty(Out, nullptr, Policy, Indentation);
  646. }
  647. PrintUnusualAnnotations(D); // HLSL Change
  648. prettyPrintAttributes(D);
  649. }
  650. void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
  651. Out << *D << ":";
  652. }
  653. void DeclPrinter::VisitVarDecl(VarDecl *D) {
  654. if (!Policy.SuppressSpecifiers) {
  655. StorageClass SC = D->getStorageClass();
  656. if (SC != SC_None)
  657. Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
  658. switch (D->getTSCSpec()) {
  659. case TSCS_unspecified:
  660. break;
  661. case TSCS___thread:
  662. Out << "__thread ";
  663. break;
  664. case TSCS__Thread_local:
  665. Out << "_Thread_local ";
  666. break;
  667. case TSCS_thread_local:
  668. Out << "thread_local ";
  669. break;
  670. }
  671. if (D->isModulePrivate())
  672. Out << "__module_private__ ";
  673. }
  674. // HLSL Change Begin
  675. if (D->hasAttrs() && Policy.LangOpts.HLSL)
  676. PrintHLSLPreAttr(D);
  677. // HLSL Change End
  678. QualType T = D->getTypeSourceInfo()
  679. ? D->getTypeSourceInfo()->getType()
  680. : D->getASTContext().getUnqualifiedObjCPointerType(D->getType());
  681. // HLSL Change Begin
  682. if (D->hasAttrs() && Policy.LangOpts.HLSL) {
  683. printDeclType(T.getNonReferenceType(), D->getName());
  684. }
  685. else {
  686. printDeclType(T, D->getName());
  687. }
  688. // HLSL Change end
  689. Expr *Init = D->getInit();
  690. if (!Policy.SuppressInitializers && Init) {
  691. bool ImplicitInit = false;
  692. if (CXXConstructExpr *Construct =
  693. dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
  694. if (D->getInitStyle() == VarDecl::CallInit &&
  695. !Construct->isListInitialization()) {
  696. ImplicitInit = Construct->getNumArgs() == 0 ||
  697. Construct->getArg(0)->isDefaultArgument();
  698. }
  699. }
  700. if (!ImplicitInit) {
  701. if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
  702. Out << "(";
  703. else if (D->getInitStyle() == VarDecl::CInit) {
  704. Out << " = ";
  705. }
  706. Init->printPretty(Out, nullptr, Policy, Indentation);
  707. if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
  708. Out << ")";
  709. }
  710. }
  711. PrintUnusualAnnotations(D); // HLSL Change
  712. prettyPrintAttributes(D);
  713. }
  714. void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
  715. VisitVarDecl(D);
  716. }
  717. void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
  718. Out << "__asm (";
  719. D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
  720. Out << ")";
  721. }
  722. void DeclPrinter::VisitImportDecl(ImportDecl *D) {
  723. Out << "@import " << D->getImportedModule()->getFullModuleName()
  724. << ";\n";
  725. }
  726. void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
  727. Out << "static_assert(";
  728. D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
  729. if (StringLiteral *SL = D->getMessage()) {
  730. Out << ", ";
  731. SL->printPretty(Out, nullptr, Policy, Indentation);
  732. }
  733. Out << ")";
  734. }
  735. //----------------------------------------------------------------------------
  736. // C++ declarations
  737. //----------------------------------------------------------------------------
  738. void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
  739. if (D->isInline())
  740. Out << "inline ";
  741. Out << "namespace " << *D << " {\n";
  742. VisitDeclContext(D);
  743. Indent() << "}";
  744. }
  745. void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
  746. Out << "using namespace ";
  747. if (D->getQualifier())
  748. D->getQualifier()->print(Out, Policy);
  749. Out << *D->getNominatedNamespaceAsWritten();
  750. }
  751. void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
  752. Out << "namespace " << *D << " = ";
  753. if (D->getQualifier())
  754. D->getQualifier()->print(Out, Policy);
  755. Out << *D->getAliasedNamespace();
  756. }
  757. void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
  758. prettyPrintAttributes(D);
  759. }
  760. void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
  761. if (!Policy.SuppressSpecifiers && D->isModulePrivate())
  762. Out << "__module_private__ ";
  763. // HLSL Change Begin
  764. if (!Policy.LangOpts.HLSL || !D->isInterface()) {
  765. Out << D->getKindName();
  766. }
  767. else {
  768. Out << "interface";
  769. }
  770. // HLSL Change End
  771. PrintUnusualAnnotations(D); // HLSL Change
  772. prettyPrintAttributes(D);
  773. if (D->getIdentifier())
  774. Out << ' ' << *D;
  775. if (D->isCompleteDefinition()) {
  776. // Print the base classes
  777. if (D->getNumBases()) {
  778. Out << " : ";
  779. for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
  780. BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
  781. if (Base != D->bases_begin())
  782. Out << ", ";
  783. if (Base->isVirtual())
  784. Out << "virtual ";
  785. AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
  786. if (AS != AS_none
  787. && !Policy.LangOpts.HLSL // HLSL Change - no access specifier for hlsl.
  788. ) {
  789. Print(AS);
  790. Out << " ";
  791. }
  792. Out << Base->getType().getAsString(Policy);
  793. if (Base->isPackExpansion())
  794. Out << "...";
  795. }
  796. }
  797. // Print the class definition
  798. // FIXME: Doesn't print access specifiers, e.g., "public:"
  799. Out << " {\n";
  800. VisitDeclContext(D);
  801. Indent() << "}";
  802. }
  803. }
  804. void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
  805. const char *l;
  806. if (D->getLanguage() == LinkageSpecDecl::lang_c)
  807. l = "C";
  808. else {
  809. assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
  810. "unknown language in linkage specification");
  811. l = "C++";
  812. }
  813. Out << "extern \"" << l << "\" ";
  814. if (D->hasBraces()) {
  815. Out << "{\n";
  816. VisitDeclContext(D);
  817. Indent() << "}";
  818. } else
  819. Visit(*D->decls_begin());
  820. }
  821. void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params,
  822. const TemplateArgumentList *Args) {
  823. assert(Params);
  824. assert(!Args || Params->size() == Args->size());
  825. Out << "template <";
  826. for (unsigned i = 0, e = Params->size(); i != e; ++i) {
  827. if (i != 0)
  828. Out << ", ";
  829. const Decl *Param = Params->getParam(i);
  830. if (const TemplateTypeParmDecl *TTP =
  831. dyn_cast<TemplateTypeParmDecl>(Param)) {
  832. if (TTP->wasDeclaredWithTypename())
  833. Out << "typename ";
  834. else
  835. Out << "class ";
  836. if (TTP->isParameterPack())
  837. Out << "...";
  838. Out << *TTP;
  839. if (Args) {
  840. Out << " = ";
  841. Args->get(i).print(Policy, Out);
  842. } else if (TTP->hasDefaultArgument()) {
  843. Out << " = ";
  844. Out << TTP->getDefaultArgument().getAsString(Policy);
  845. };
  846. } else if (const NonTypeTemplateParmDecl *NTTP =
  847. dyn_cast<NonTypeTemplateParmDecl>(Param)) {
  848. StringRef Name;
  849. if (IdentifierInfo *II = NTTP->getIdentifier())
  850. Name = II->getName();
  851. printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
  852. if (Args) {
  853. Out << " = ";
  854. Args->get(i).print(Policy, Out);
  855. } else if (NTTP->hasDefaultArgument()) {
  856. Out << " = ";
  857. NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
  858. Indentation);
  859. }
  860. } else if (const TemplateTemplateParmDecl *TTPD =
  861. dyn_cast<TemplateTemplateParmDecl>(Param)) {
  862. VisitTemplateDecl(TTPD);
  863. // FIXME: print the default argument, if present.
  864. }
  865. }
  866. Out << "> ";
  867. }
  868. void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
  869. PrintTemplateParameters(D->getTemplateParameters());
  870. if (const TemplateTemplateParmDecl *TTP =
  871. dyn_cast<TemplateTemplateParmDecl>(D)) {
  872. Out << "class ";
  873. if (TTP->isParameterPack())
  874. Out << "...";
  875. Out << D->getName();
  876. } else {
  877. Visit(D->getTemplatedDecl());
  878. }
  879. }
  880. void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
  881. if (PrintInstantiation) {
  882. TemplateParameterList *Params = D->getTemplateParameters();
  883. for (auto *I : D->specializations()) {
  884. PrintTemplateParameters(Params, I->getTemplateSpecializationArgs());
  885. Visit(I);
  886. }
  887. }
  888. return VisitRedeclarableTemplateDecl(D);
  889. }
  890. void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
  891. if (PrintInstantiation) {
  892. TemplateParameterList *Params = D->getTemplateParameters();
  893. for (auto *I : D->specializations()) {
  894. PrintTemplateParameters(Params, &I->getTemplateArgs());
  895. Visit(I);
  896. Out << '\n';
  897. }
  898. }
  899. return VisitRedeclarableTemplateDecl(D);
  900. }
  901. //----------------------------------------------------------------------------
  902. // Objective-C declarations
  903. //----------------------------------------------------------------------------
  904. void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
  905. Decl::ObjCDeclQualifier Quals,
  906. QualType T) {
  907. Out << '(';
  908. if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
  909. Out << "in ";
  910. if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
  911. Out << "inout ";
  912. if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
  913. Out << "out ";
  914. if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
  915. Out << "bycopy ";
  916. if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
  917. Out << "byref ";
  918. if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
  919. Out << "oneway ";
  920. if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
  921. if (auto nullability = AttributedType::stripOuterNullability(T))
  922. Out << getNullabilitySpelling(*nullability, true) << ' ';
  923. }
  924. Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
  925. Out << ')';
  926. }
  927. void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
  928. Out << "<";
  929. unsigned First = true;
  930. for (auto *Param : *Params) {
  931. if (First) {
  932. First = false;
  933. } else {
  934. Out << ", ";
  935. }
  936. switch (Param->getVariance()) {
  937. case ObjCTypeParamVariance::Invariant:
  938. break;
  939. case ObjCTypeParamVariance::Covariant:
  940. Out << "__covariant ";
  941. break;
  942. case ObjCTypeParamVariance::Contravariant:
  943. Out << "__contravariant ";
  944. break;
  945. }
  946. Out << Param->getDeclName().getAsString();
  947. if (Param->hasExplicitBound()) {
  948. Out << " : " << Param->getUnderlyingType().getAsString(Policy);
  949. }
  950. }
  951. Out << ">";
  952. }
  953. void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
  954. if (OMD->isInstanceMethod())
  955. Out << "- ";
  956. else
  957. Out << "+ ";
  958. if (!OMD->getReturnType().isNull()) {
  959. PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
  960. OMD->getReturnType());
  961. }
  962. std::string name = OMD->getSelector().getAsString();
  963. std::string::size_type pos, lastPos = 0;
  964. for (const auto *PI : OMD->params()) {
  965. // FIXME: selector is missing here!
  966. pos = name.find_first_of(':', lastPos);
  967. Out << " " << name.substr(lastPos, pos - lastPos) << ':';
  968. PrintObjCMethodType(OMD->getASTContext(),
  969. PI->getObjCDeclQualifier(),
  970. PI->getType());
  971. Out << *PI;
  972. lastPos = pos + 1;
  973. }
  974. if (OMD->param_begin() == OMD->param_end())
  975. Out << " " << name;
  976. if (OMD->isVariadic())
  977. Out << ", ...";
  978. prettyPrintAttributes(OMD);
  979. if (OMD->getBody() && !Policy.TerseOutput) {
  980. Out << ' ';
  981. OMD->getBody()->printPretty(Out, nullptr, Policy);
  982. }
  983. else if (Policy.PolishForDeclaration)
  984. Out << ';';
  985. }
  986. void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
  987. std::string I = OID->getNameAsString();
  988. ObjCInterfaceDecl *SID = OID->getSuperClass();
  989. bool eolnOut = false;
  990. if (SID)
  991. Out << "@implementation " << I << " : " << *SID;
  992. else
  993. Out << "@implementation " << I;
  994. if (OID->ivar_size() > 0) {
  995. Out << "{\n";
  996. eolnOut = true;
  997. Indentation += Policy.Indentation;
  998. for (const auto *I : OID->ivars()) {
  999. Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
  1000. getAsString(Policy) << ' ' << *I << ";\n";
  1001. }
  1002. Indentation -= Policy.Indentation;
  1003. Out << "}\n";
  1004. }
  1005. else if (SID || (OID->decls_begin() != OID->decls_end())) {
  1006. Out << "\n";
  1007. eolnOut = true;
  1008. }
  1009. VisitDeclContext(OID, false);
  1010. if (!eolnOut)
  1011. Out << "\n";
  1012. Out << "@end";
  1013. }
  1014. void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
  1015. std::string I = OID->getNameAsString();
  1016. ObjCInterfaceDecl *SID = OID->getSuperClass();
  1017. if (!OID->isThisDeclarationADefinition()) {
  1018. Out << "@class " << I;
  1019. if (auto TypeParams = OID->getTypeParamListAsWritten()) {
  1020. PrintObjCTypeParams(TypeParams);
  1021. }
  1022. Out << ";";
  1023. return;
  1024. }
  1025. bool eolnOut = false;
  1026. Out << "@interface " << I;
  1027. if (auto TypeParams = OID->getTypeParamListAsWritten()) {
  1028. PrintObjCTypeParams(TypeParams);
  1029. }
  1030. if (SID)
  1031. Out << " : " << OID->getSuperClass()->getName();
  1032. // Protocols?
  1033. const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
  1034. if (!Protocols.empty()) {
  1035. for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
  1036. E = Protocols.end(); I != E; ++I)
  1037. Out << (I == Protocols.begin() ? '<' : ',') << **I;
  1038. Out << "> ";
  1039. }
  1040. if (OID->ivar_size() > 0) {
  1041. Out << "{\n";
  1042. eolnOut = true;
  1043. Indentation += Policy.Indentation;
  1044. for (const auto *I : OID->ivars()) {
  1045. Indent() << I->getASTContext()
  1046. .getUnqualifiedObjCPointerType(I->getType())
  1047. .getAsString(Policy) << ' ' << *I << ";\n";
  1048. }
  1049. Indentation -= Policy.Indentation;
  1050. Out << "}\n";
  1051. }
  1052. else if (SID || (OID->decls_begin() != OID->decls_end())) {
  1053. Out << "\n";
  1054. eolnOut = true;
  1055. }
  1056. VisitDeclContext(OID, false);
  1057. if (!eolnOut)
  1058. Out << "\n";
  1059. Out << "@end";
  1060. // FIXME: implement the rest...
  1061. }
  1062. void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
  1063. if (!PID->isThisDeclarationADefinition()) {
  1064. Out << "@protocol " << *PID << ";\n";
  1065. return;
  1066. }
  1067. // Protocols?
  1068. const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
  1069. if (!Protocols.empty()) {
  1070. Out << "@protocol " << *PID;
  1071. for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
  1072. E = Protocols.end(); I != E; ++I)
  1073. Out << (I == Protocols.begin() ? '<' : ',') << **I;
  1074. Out << ">\n";
  1075. } else
  1076. Out << "@protocol " << *PID << '\n';
  1077. VisitDeclContext(PID, false);
  1078. Out << "@end";
  1079. }
  1080. void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
  1081. Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
  1082. VisitDeclContext(PID, false);
  1083. Out << "@end";
  1084. // FIXME: implement the rest...
  1085. }
  1086. void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
  1087. Out << "@interface " << *PID->getClassInterface();
  1088. if (auto TypeParams = PID->getTypeParamList()) {
  1089. PrintObjCTypeParams(TypeParams);
  1090. }
  1091. Out << "(" << *PID << ")\n";
  1092. if (PID->ivar_size() > 0) {
  1093. Out << "{\n";
  1094. Indentation += Policy.Indentation;
  1095. for (const auto *I : PID->ivars())
  1096. Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
  1097. getAsString(Policy) << ' ' << *I << ";\n";
  1098. Indentation -= Policy.Indentation;
  1099. Out << "}\n";
  1100. }
  1101. VisitDeclContext(PID, false);
  1102. Out << "@end";
  1103. // FIXME: implement the rest...
  1104. }
  1105. void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
  1106. Out << "@compatibility_alias " << *AID
  1107. << ' ' << *AID->getClassInterface() << ";\n";
  1108. }
  1109. /// PrintObjCPropertyDecl - print a property declaration.
  1110. ///
  1111. void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
  1112. if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
  1113. Out << "@required\n";
  1114. else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
  1115. Out << "@optional\n";
  1116. QualType T = PDecl->getType();
  1117. Out << "@property";
  1118. if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
  1119. bool first = true;
  1120. Out << " (";
  1121. if (PDecl->getPropertyAttributes() &
  1122. ObjCPropertyDecl::OBJC_PR_readonly) {
  1123. Out << (first ? ' ' : ',') << "readonly";
  1124. first = false;
  1125. }
  1126. if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
  1127. Out << (first ? ' ' : ',') << "getter = ";
  1128. PDecl->getGetterName().print(Out);
  1129. first = false;
  1130. }
  1131. if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
  1132. Out << (first ? ' ' : ',') << "setter = ";
  1133. PDecl->getSetterName().print(Out);
  1134. first = false;
  1135. }
  1136. if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
  1137. Out << (first ? ' ' : ',') << "assign";
  1138. first = false;
  1139. }
  1140. if (PDecl->getPropertyAttributes() &
  1141. ObjCPropertyDecl::OBJC_PR_readwrite) {
  1142. Out << (first ? ' ' : ',') << "readwrite";
  1143. first = false;
  1144. }
  1145. if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
  1146. Out << (first ? ' ' : ',') << "retain";
  1147. first = false;
  1148. }
  1149. if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
  1150. Out << (first ? ' ' : ',') << "strong";
  1151. first = false;
  1152. }
  1153. if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
  1154. Out << (first ? ' ' : ',') << "copy";
  1155. first = false;
  1156. }
  1157. if (PDecl->getPropertyAttributes() &
  1158. ObjCPropertyDecl::OBJC_PR_nonatomic) {
  1159. Out << (first ? ' ' : ',') << "nonatomic";
  1160. first = false;
  1161. }
  1162. if (PDecl->getPropertyAttributes() &
  1163. ObjCPropertyDecl::OBJC_PR_atomic) {
  1164. Out << (first ? ' ' : ',') << "atomic";
  1165. first = false;
  1166. }
  1167. if (PDecl->getPropertyAttributes() &
  1168. ObjCPropertyDecl::OBJC_PR_nullability) {
  1169. if (auto nullability = AttributedType::stripOuterNullability(T)) {
  1170. if (*nullability == NullabilityKind::Unspecified &&
  1171. (PDecl->getPropertyAttributes() &
  1172. ObjCPropertyDecl::OBJC_PR_null_resettable)) {
  1173. Out << (first ? ' ' : ',') << "null_resettable";
  1174. } else {
  1175. Out << (first ? ' ' : ',')
  1176. << getNullabilitySpelling(*nullability, true);
  1177. }
  1178. first = false;
  1179. }
  1180. }
  1181. (void) first; // Silence dead store warning due to idiomatic code.
  1182. Out << " )";
  1183. }
  1184. Out << ' ' << PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
  1185. getAsString(Policy) << ' ' << *PDecl;
  1186. if (Policy.PolishForDeclaration)
  1187. Out << ';';
  1188. }
  1189. void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
  1190. if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
  1191. Out << "@synthesize ";
  1192. else
  1193. Out << "@dynamic ";
  1194. Out << *PID->getPropertyDecl();
  1195. if (PID->getPropertyIvarDecl())
  1196. Out << '=' << *PID->getPropertyIvarDecl();
  1197. }
  1198. void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
  1199. if (!D->isAccessDeclaration())
  1200. Out << "using ";
  1201. if (D->hasTypename())
  1202. Out << "typename ";
  1203. D->getQualifier()->print(Out, Policy);
  1204. Out << *D;
  1205. }
  1206. void
  1207. DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
  1208. Out << "using typename ";
  1209. D->getQualifier()->print(Out, Policy);
  1210. Out << D->getDeclName();
  1211. }
  1212. void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
  1213. if (!D->isAccessDeclaration())
  1214. Out << "using ";
  1215. D->getQualifier()->print(Out, Policy);
  1216. Out << D->getName();
  1217. }
  1218. void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
  1219. // ignore
  1220. }
  1221. void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
  1222. Out << "#pragma omp threadprivate";
  1223. if (!D->varlist_empty()) {
  1224. for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
  1225. E = D->varlist_end();
  1226. I != E; ++I) {
  1227. Out << (I == D->varlist_begin() ? '(' : ',');
  1228. NamedDecl *ND = cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
  1229. ND->printQualifiedName(Out);
  1230. }
  1231. Out << ")";
  1232. }
  1233. }
  1234. // HLSL Change Begin
  1235. void DeclPrinter::VisitHLSLBufferDecl(HLSLBufferDecl *D) {
  1236. if (D->isCBuffer()) {
  1237. Out << "cbuffer ";
  1238. }
  1239. else {
  1240. Out << "tbuffer ";
  1241. }
  1242. Out << *D;
  1243. PrintUnusualAnnotations(D);
  1244. prettyPrintAttributes(D);
  1245. Out << " {\n";
  1246. VisitDeclContext(D);
  1247. Indent() << "}";
  1248. }
  1249. void DeclPrinter::PrintUnusualAnnotations(NamedDecl* D) {
  1250. if (D->isInvalidDecl())
  1251. return;
  1252. ArrayRef<hlsl::UnusualAnnotation *> Annotations = D->getUnusualAnnotations();
  1253. if (!Annotations.empty()) {
  1254. for (auto i = Annotations.begin(), e = Annotations.end(); i != e; ++i) {
  1255. VisitHLSLUnusualAnnotation(*i);
  1256. }
  1257. }
  1258. }
  1259. void DeclPrinter::VisitHLSLUnusualAnnotation(const hlsl::UnusualAnnotation *UA) {
  1260. switch (UA->getKind()) {
  1261. case hlsl::UnusualAnnotation::UA_SemanticDecl: {
  1262. const hlsl::SemanticDecl * semdecl = dyn_cast<hlsl::SemanticDecl>(UA);
  1263. Out << " : " << semdecl->SemanticName.str();
  1264. break;
  1265. }
  1266. case hlsl::UnusualAnnotation::UA_RegisterAssignment: {
  1267. const hlsl::RegisterAssignment * ra = dyn_cast<hlsl::RegisterAssignment>(UA);
  1268. if (ra->RegisterType) {
  1269. Out << " : register(";
  1270. if (!ra->ShaderProfile.empty()) {
  1271. Out << ra->ShaderProfile.str() << ", ";
  1272. }
  1273. Out << ra->RegisterType << ra->RegisterNumber;
  1274. if (ra->RegisterOffset) {
  1275. Out << "[" << ra->RegisterOffset << "]";
  1276. }
  1277. Out << ")";
  1278. }
  1279. break;
  1280. }
  1281. case hlsl::UnusualAnnotation::UA_ConstantPacking: {
  1282. const hlsl::ConstantPacking * cp = dyn_cast<hlsl::ConstantPacking>(UA);
  1283. Out << " : packoffset(c" << cp->Subcomponent; //packing applies to constant registers (c) only
  1284. if (cp->ComponentOffset) {
  1285. switch (cp->ComponentOffset) {
  1286. case 1:
  1287. Out << ".y";
  1288. break;
  1289. case 2:
  1290. Out << ".z";
  1291. break;
  1292. case 3:
  1293. Out << ".w";
  1294. break;
  1295. }
  1296. }
  1297. Out << ")";
  1298. break;
  1299. }
  1300. }
  1301. }
  1302. void DeclPrinter::PrintHLSLPreAttr(NamedDecl* D) {
  1303. AttrVec &Attrs = D->getAttrs();
  1304. std::vector<Attr*> tempVec;
  1305. for (AttrVec::const_reverse_iterator i = Attrs.rbegin(), e = Attrs.rend(); i != e; ++i) {
  1306. Attr *A = *i;
  1307. hlsl::CustomPrintHLSLAttr(A, Out, Policy, Indentation);
  1308. }
  1309. }
  1310. // HLSL Change End