DeclPrinter.cpp 45 KB

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