| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1 | //===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===// |
| 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| Alexander Kornienko | 90ff607 | 2012-12-20 02:09:13 +0000 | [diff] [blame] | 9 | // This file implements the Decl::print method, which pretty prints the |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 10 | // AST back out to C/Objective-C/C++/Objective-C++ code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #include "clang/AST/ASTContext.h" |
| Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 14 | #include "clang/AST/Attr.h" |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 15 | #include "clang/AST/Decl.h" |
| 16 | #include "clang/AST/DeclCXX.h" |
| 17 | #include "clang/AST/DeclObjC.h" |
| Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
| Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclVisitor.h" |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 22 | #include "clang/AST/PrettyPrinter.h" |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 23 | #include "clang/Basic/Module.h" |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | using namespace clang; |
| 26 | |
| 27 | namespace { |
| Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 28 | class DeclPrinter : public DeclVisitor<DeclPrinter> { |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 29 | raw_ostream &Out; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 30 | PrintingPolicy Policy; |
| Alex Lorenz | 36070ed | 2017-08-17 13:41:55 +0000 | [diff] [blame] | 31 | const ASTContext &Context; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 32 | unsigned Indentation; |
| Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 33 | bool PrintInstantiation; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 34 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 35 | raw_ostream& Indent() { return Indent(Indentation); } |
| 36 | raw_ostream& Indent(unsigned Indentation); |
| 37 | void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 38 | |
| Anders Carlsson | 601d6e4 | 2009-08-28 22:39:52 +0000 | [diff] [blame] | 39 | void Print(AccessSpecifier AS); |
| Alex Lorenz | dc616fa | 2017-11-16 01:31:27 +0000 | [diff] [blame] | 40 | void PrintConstructorInitializers(CXXConstructorDecl *CDecl, |
| 41 | std::string &Proto); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | |
| Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 43 | /// Print an Objective-C method type in parentheses. |
| 44 | /// |
| 45 | /// \param Quals The Objective-C declaration qualifiers. |
| 46 | /// \param T The type to print. |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 47 | void PrintObjCMethodType(ASTContext &Ctx, Decl::ObjCDeclQualifier Quals, |
| Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 48 | QualType T); |
| 49 | |
| Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 50 | void PrintObjCTypeParams(ObjCTypeParamList *Params); |
| 51 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 52 | public: |
| Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 53 | DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy, |
| Alex Lorenz | 36070ed | 2017-08-17 13:41:55 +0000 | [diff] [blame] | 54 | const ASTContext &Context, unsigned Indentation = 0, |
| 55 | bool PrintInstantiation = false) |
| 56 | : Out(Out), Policy(Policy), Context(Context), Indentation(Indentation), |
| 57 | PrintInstantiation(PrintInstantiation) {} |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 58 | |
| 59 | void VisitDeclContext(DeclContext *DC, bool Indent = true); |
| 60 | |
| 61 | void VisitTranslationUnitDecl(TranslationUnitDecl *D); |
| 62 | void VisitTypedefDecl(TypedefDecl *D); |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 63 | void VisitTypeAliasDecl(TypeAliasDecl *D); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 64 | void VisitEnumDecl(EnumDecl *D); |
| 65 | void VisitRecordDecl(RecordDecl *D); |
| 66 | void VisitEnumConstantDecl(EnumConstantDecl *D); |
| Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 67 | void VisitEmptyDecl(EmptyDecl *D); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 68 | void VisitFunctionDecl(FunctionDecl *D); |
| Fariborz Jahanian | bfc3ef5 | 2012-12-05 00:38:44 +0000 | [diff] [blame] | 69 | void VisitFriendDecl(FriendDecl *D); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 70 | void VisitFieldDecl(FieldDecl *D); |
| 71 | void VisitVarDecl(VarDecl *D); |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 72 | void VisitLabelDecl(LabelDecl *D); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 73 | void VisitParmVarDecl(ParmVarDecl *D); |
| 74 | void VisitFileScopeAsmDecl(FileScopeAsmDecl *D); |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 75 | void VisitImportDecl(ImportDecl *D); |
| Peter Collingbourne | 7d8a0b5 | 2011-03-16 18:37:27 +0000 | [diff] [blame] | 76 | void VisitStaticAssertDecl(StaticAssertDecl *D); |
| Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 77 | void VisitNamespaceDecl(NamespaceDecl *D); |
| Douglas Gregor | 3bc6e4c | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 78 | void VisitUsingDirectiveDecl(UsingDirectiveDecl *D); |
| Douglas Gregor | 1823193 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 79 | void VisitNamespaceAliasDecl(NamespaceAliasDecl *D); |
| Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 80 | void VisitCXXRecordDecl(CXXRecordDecl *D); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 81 | void VisitLinkageSpecDecl(LinkageSpecDecl *D); |
| Richard Smith | 030f499 | 2011-04-15 13:38:57 +0000 | [diff] [blame] | 82 | void VisitTemplateDecl(const TemplateDecl *D); |
| Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 83 | void VisitFunctionTemplateDecl(FunctionTemplateDecl *D); |
| 84 | void VisitClassTemplateDecl(ClassTemplateDecl *D); |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 85 | void VisitClassTemplateSpecializationDecl( |
| 86 | ClassTemplateSpecializationDecl *D); |
| 87 | void VisitClassTemplatePartialSpecializationDecl( |
| 88 | ClassTemplatePartialSpecializationDecl *D); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 89 | void VisitObjCMethodDecl(ObjCMethodDecl *D); |
| 90 | void VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
| 91 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 92 | void VisitObjCProtocolDecl(ObjCProtocolDecl *D); |
| 93 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
| 94 | void VisitObjCCategoryDecl(ObjCCategoryDecl *D); |
| 95 | void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D); |
| 96 | void VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
| 97 | void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); |
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 98 | void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); |
| 99 | void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); |
| Anders Carlsson | df5a1c8 | 2009-08-28 19:16:39 +0000 | [diff] [blame] | 100 | void VisitUsingDecl(UsingDecl *D); |
| John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 101 | void VisitUsingShadowDecl(UsingShadowDecl *D); |
| Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 102 | void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D); |
| Alexey Bataev | 25ed0c0 | 2019-03-07 17:54:44 +0000 | [diff] [blame] | 103 | void VisitOMPAllocateDecl(OMPAllocateDecl *D); |
| Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 104 | void VisitOMPRequiresDecl(OMPRequiresDecl *D); |
| Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 105 | void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D); |
| Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 106 | void VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D); |
| Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 107 | void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D); |
| Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 108 | |
| Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 109 | void printTemplateParameters(const TemplateParameterList *Params, |
| 110 | bool OmitTemplateKW = false); |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 111 | void printTemplateArguments(const TemplateArgumentList &Args, |
| 112 | const TemplateParameterList *Params = nullptr); |
| Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 113 | void prettyPrintAttributes(Decl *D); |
| Alexey Bataev | 6d45532 | 2015-10-12 06:59:48 +0000 | [diff] [blame] | 114 | void prettyPrintPragmas(Decl *D); |
| Richard Smith | a4bb292 | 2014-07-23 03:17:06 +0000 | [diff] [blame] | 115 | void printDeclType(QualType T, StringRef DeclName, bool Pack = false); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 116 | }; |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 117 | } |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 118 | |
| Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 119 | void Decl::print(raw_ostream &Out, unsigned Indentation, |
| 120 | bool PrintInstantiation) const { |
| Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 121 | print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 124 | void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy, |
| Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 125 | unsigned Indentation, bool PrintInstantiation) const { |
| Alex Lorenz | 36070ed | 2017-08-17 13:41:55 +0000 | [diff] [blame] | 126 | DeclPrinter Printer(Out, Policy, getASTContext(), Indentation, |
| 127 | PrintInstantiation); |
| Anders Carlsson | 46f87dc | 2009-09-26 21:58:53 +0000 | [diff] [blame] | 128 | Printer.Visit(const_cast<Decl*>(this)); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 131 | void TemplateParameterList::print(raw_ostream &Out, const ASTContext &Context, |
| 132 | bool OmitTemplateKW) const { |
| 133 | print(Out, Context, Context.getPrintingPolicy(), OmitTemplateKW); |
| 134 | } |
| 135 | |
| 136 | void TemplateParameterList::print(raw_ostream &Out, const ASTContext &Context, |
| 137 | const PrintingPolicy &Policy, |
| 138 | bool OmitTemplateKW) const { |
| 139 | DeclPrinter Printer(Out, Policy, Context); |
| 140 | Printer.printTemplateParameters(this, OmitTemplateKW); |
| 141 | } |
| 142 | |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 143 | static QualType GetBaseType(QualType T) { |
| 144 | // FIXME: This should be on the Type class! |
| 145 | QualType BaseType = T; |
| 146 | while (!BaseType->isSpecifierType()) { |
| Artem Belevich | 224879e | 2018-01-17 19:29:39 +0000 | [diff] [blame] | 147 | if (const PointerType *PTy = BaseType->getAs<PointerType>()) |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 148 | BaseType = PTy->getPointeeType(); |
| Ted Kremenek | fa0a0b6 | 2012-10-11 20:58:14 +0000 | [diff] [blame] | 149 | else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>()) |
| 150 | BaseType = BPy->getPointeeType(); |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 151 | else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType)) |
| 152 | BaseType = ATy->getElementType(); |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 153 | else if (const FunctionType* FTy = BaseType->getAs<FunctionType>()) |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 154 | BaseType = FTy->getReturnType(); |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 155 | else if (const VectorType *VTy = BaseType->getAs<VectorType>()) |
| Douglas Gregor | 1554825 | 2009-07-01 23:58:14 +0000 | [diff] [blame] | 156 | BaseType = VTy->getElementType(); |
| Eli Friedman | 70bc6e6 | 2012-08-08 03:47:15 +0000 | [diff] [blame] | 157 | else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>()) |
| 158 | BaseType = RTy->getPointeeType(); |
| Vassil Vassilev | cdaa31f | 2016-07-08 16:04:22 +0000 | [diff] [blame] | 159 | else if (const AutoType *ATy = BaseType->getAs<AutoType>()) |
| 160 | BaseType = ATy->getDeducedType(); |
| Artem Belevich | 224879e | 2018-01-17 19:29:39 +0000 | [diff] [blame] | 161 | else if (const ParenType *PTy = BaseType->getAs<ParenType>()) |
| 162 | BaseType = PTy->desugar(); |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 163 | else |
| Artem Belevich | 224879e | 2018-01-17 19:29:39 +0000 | [diff] [blame] | 164 | // This must be a syntax error. |
| 165 | break; |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 166 | } |
| 167 | return BaseType; |
| 168 | } |
| 169 | |
| 170 | static QualType getDeclType(Decl* D) { |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 171 | if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D)) |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 172 | return TDD->getUnderlyingType(); |
| 173 | if (ValueDecl* VD = dyn_cast<ValueDecl>(D)) |
| 174 | return VD->getType(); |
| 175 | return QualType(); |
| 176 | } |
| 177 | |
| 178 | void Decl::printGroup(Decl** Begin, unsigned NumDecls, |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 179 | raw_ostream &Out, const PrintingPolicy &Policy, |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 180 | unsigned Indentation) { |
| 181 | if (NumDecls == 1) { |
| Argyrios Kyrtzidis | 8a803cc | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 182 | (*Begin)->print(Out, Policy, Indentation); |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 183 | return; |
| 184 | } |
| 185 | |
| 186 | Decl** End = Begin + NumDecls; |
| 187 | TagDecl* TD = dyn_cast<TagDecl>(*Begin); |
| 188 | if (TD) |
| 189 | ++Begin; |
| 190 | |
| 191 | PrintingPolicy SubPolicy(Policy); |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 192 | |
| 193 | bool isFirst = true; |
| 194 | for ( ; Begin != End; ++Begin) { |
| 195 | if (isFirst) { |
| Steven Watanabe | 9359b8f | 2016-03-18 21:35:59 +0000 | [diff] [blame] | 196 | if(TD) |
| 197 | SubPolicy.IncludeTagDefinition = true; |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 198 | SubPolicy.SuppressSpecifiers = false; |
| 199 | isFirst = false; |
| 200 | } else { |
| 201 | if (!isFirst) Out << ", "; |
| Steven Watanabe | 9359b8f | 2016-03-18 21:35:59 +0000 | [diff] [blame] | 202 | SubPolicy.IncludeTagDefinition = false; |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 203 | SubPolicy.SuppressSpecifiers = true; |
| 204 | } |
| 205 | |
| Argyrios Kyrtzidis | 8a803cc | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 206 | (*Begin)->print(Out, SubPolicy, Indentation); |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
| Alp Toker | ef6b007 | 2014-01-04 13:47:14 +0000 | [diff] [blame] | 210 | LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const { |
| Anders Carlsson | 538af09 | 2009-12-09 17:27:46 +0000 | [diff] [blame] | 211 | // Get the translation unit |
| 212 | const DeclContext *DC = this; |
| 213 | while (!DC->isTranslationUnit()) |
| 214 | DC = DC->getParent(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 215 | |
| Anders Carlsson | 538af09 | 2009-12-09 17:27:46 +0000 | [diff] [blame] | 216 | ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext(); |
| Alex Lorenz | 36070ed | 2017-08-17 13:41:55 +0000 | [diff] [blame] | 217 | DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), Ctx, 0); |
| Anders Carlsson | 538af09 | 2009-12-09 17:27:46 +0000 | [diff] [blame] | 218 | Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false); |
| 219 | } |
| 220 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 221 | raw_ostream& DeclPrinter::Indent(unsigned Indentation) { |
| Daniel Dunbar | 671f45e | 2009-11-21 09:12:06 +0000 | [diff] [blame] | 222 | for (unsigned i = 0; i != Indentation; ++i) |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 223 | Out << " "; |
| 224 | return Out; |
| 225 | } |
| 226 | |
| Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 227 | void DeclPrinter::prettyPrintAttributes(Decl *D) { |
| Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 228 | if (Policy.PolishForDeclaration) |
| Fariborz Jahanian | a7d76d2 | 2012-10-17 21:58:03 +0000 | [diff] [blame] | 229 | return; |
| Alexey Bataev | 6d45532 | 2015-10-12 06:59:48 +0000 | [diff] [blame] | 230 | |
| Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 231 | if (D->hasAttrs()) { |
| 232 | AttrVec &Attrs = D->getAttrs(); |
| Alexey Bataev | 6d45532 | 2015-10-12 06:59:48 +0000 | [diff] [blame] | 233 | for (auto *A : Attrs) { |
| Joel E. Denny | 9454864 | 2018-05-15 22:16:47 +0000 | [diff] [blame] | 234 | if (A->isInherited() || A->isImplicit()) |
| Joel E. Denny | 7509a2f | 2018-05-14 19:36:45 +0000 | [diff] [blame] | 235 | continue; |
| Alexey Bataev | 6d45532 | 2015-10-12 06:59:48 +0000 | [diff] [blame] | 236 | switch (A->getKind()) { |
| 237 | #define ATTR(X) |
| 238 | #define PRAGMA_SPELLING_ATTR(X) case attr::X: |
| 239 | #include "clang/Basic/AttrList.inc" |
| 240 | break; |
| 241 | default: |
| 242 | A->printPretty(Out, Policy); |
| 243 | break; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | void DeclPrinter::prettyPrintPragmas(Decl *D) { |
| 250 | if (Policy.PolishForDeclaration) |
| 251 | return; |
| 252 | |
| 253 | if (D->hasAttrs()) { |
| 254 | AttrVec &Attrs = D->getAttrs(); |
| 255 | for (auto *A : Attrs) { |
| 256 | switch (A->getKind()) { |
| 257 | #define ATTR(X) |
| 258 | #define PRAGMA_SPELLING_ATTR(X) case attr::X: |
| 259 | #include "clang/Basic/AttrList.inc" |
| 260 | A->printPretty(Out, Policy); |
| 261 | Indent(); |
| 262 | break; |
| 263 | default: |
| 264 | break; |
| 265 | } |
| Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| Richard Smith | a4bb292 | 2014-07-23 03:17:06 +0000 | [diff] [blame] | 270 | void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) { |
| 271 | // Normally, a PackExpansionType is written as T[3]... (for instance, as a |
| 272 | // template argument), but if it is the type of a declaration, the ellipsis |
| 273 | // is placed before the name being declared. |
| 274 | if (auto *PET = T->getAs<PackExpansionType>()) { |
| 275 | Pack = true; |
| 276 | T = PET->getPattern(); |
| 277 | } |
| Steven Watanabe | 9359b8f | 2016-03-18 21:35:59 +0000 | [diff] [blame] | 278 | T.print(Out, Policy, (Pack ? "..." : "") + DeclName, Indentation); |
| Richard Smith | a4bb292 | 2014-07-23 03:17:06 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 281 | void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) { |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 282 | this->Indent(); |
| Argyrios Kyrtzidis | 8a803cc | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 283 | Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation); |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 284 | Out << ";\n"; |
| 285 | Decls.clear(); |
| 286 | |
| 287 | } |
| 288 | |
| Anders Carlsson | 601d6e4 | 2009-08-28 22:39:52 +0000 | [diff] [blame] | 289 | void DeclPrinter::Print(AccessSpecifier AS) { |
| 290 | switch(AS) { |
| David Blaikie | aa347f9 | 2011-09-23 20:26:49 +0000 | [diff] [blame] | 291 | case AS_none: llvm_unreachable("No access specifier!"); |
| Anders Carlsson | 601d6e4 | 2009-08-28 22:39:52 +0000 | [diff] [blame] | 292 | case AS_public: Out << "public"; break; |
| 293 | case AS_protected: Out << "protected"; break; |
| Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 294 | case AS_private: Out << "private"; break; |
| Anders Carlsson | 601d6e4 | 2009-08-28 22:39:52 +0000 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
| Alex Lorenz | dc616fa | 2017-11-16 01:31:27 +0000 | [diff] [blame] | 298 | void DeclPrinter::PrintConstructorInitializers(CXXConstructorDecl *CDecl, |
| 299 | std::string &Proto) { |
| 300 | bool HasInitializerList = false; |
| 301 | for (const auto *BMInitializer : CDecl->inits()) { |
| 302 | if (BMInitializer->isInClassMemberInitializer()) |
| 303 | continue; |
| 304 | |
| 305 | if (!HasInitializerList) { |
| 306 | Proto += " : "; |
| 307 | Out << Proto; |
| 308 | Proto.clear(); |
| 309 | HasInitializerList = true; |
| 310 | } else |
| 311 | Out << ", "; |
| 312 | |
| 313 | if (BMInitializer->isAnyMemberInitializer()) { |
| 314 | FieldDecl *FD = BMInitializer->getAnyMember(); |
| 315 | Out << *FD; |
| 316 | } else { |
| 317 | Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy); |
| 318 | } |
| 319 | |
| 320 | Out << "("; |
| 321 | if (!BMInitializer->getInit()) { |
| 322 | // Nothing to print |
| 323 | } else { |
| 324 | Expr *Init = BMInitializer->getInit(); |
| 325 | if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init)) |
| 326 | Init = Tmp->getSubExpr(); |
| 327 | |
| 328 | Init = Init->IgnoreParens(); |
| 329 | |
| 330 | Expr *SimpleInit = nullptr; |
| 331 | Expr **Args = nullptr; |
| 332 | unsigned NumArgs = 0; |
| 333 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
| 334 | Args = ParenList->getExprs(); |
| 335 | NumArgs = ParenList->getNumExprs(); |
| 336 | } else if (CXXConstructExpr *Construct = |
| 337 | dyn_cast<CXXConstructExpr>(Init)) { |
| 338 | Args = Construct->getArgs(); |
| 339 | NumArgs = Construct->getNumArgs(); |
| 340 | } else |
| 341 | SimpleInit = Init; |
| 342 | |
| 343 | if (SimpleInit) |
| 344 | SimpleInit->printPretty(Out, nullptr, Policy, Indentation); |
| 345 | else { |
| 346 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 347 | assert(Args[I] != nullptr && "Expected non-null Expr"); |
| 348 | if (isa<CXXDefaultArgExpr>(Args[I])) |
| 349 | break; |
| 350 | |
| 351 | if (I) |
| 352 | Out << ", "; |
| 353 | Args[I]->printPretty(Out, nullptr, Policy, Indentation); |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | Out << ")"; |
| 358 | if (BMInitializer->isPackExpansion()) |
| 359 | Out << "..."; |
| 360 | } |
| 361 | } |
| 362 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 363 | //---------------------------------------------------------------------------- |
| 364 | // Common C declarations |
| 365 | //---------------------------------------------------------------------------- |
| 366 | |
| 367 | void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) { |
| Dmitri Gribenko | a93a7e8 | 2012-08-21 17:36:32 +0000 | [diff] [blame] | 368 | if (Policy.TerseOutput) |
| Dmitri Gribenko | 309856a | 2012-08-20 23:39:06 +0000 | [diff] [blame] | 369 | return; |
| 370 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 371 | if (Indent) |
| 372 | Indentation += Policy.Indentation; |
| 373 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 374 | SmallVector<Decl*, 2> Decls; |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 375 | for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end(); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 376 | D != DEnd; ++D) { |
| Ted Kremenek | 28e1c91 | 2010-07-30 00:47:46 +0000 | [diff] [blame] | 377 | |
| 378 | // Don't print ObjCIvarDecls, as they are printed when visiting the |
| 379 | // containing ObjCInterfaceDecl. |
| 380 | if (isa<ObjCIvarDecl>(*D)) |
| 381 | continue; |
| 382 | |
| Alexander Kornienko | 90ff607 | 2012-12-20 02:09:13 +0000 | [diff] [blame] | 383 | // Skip over implicit declarations in pretty-printing mode. |
| 384 | if (D->isImplicit()) |
| 385 | continue; |
| 386 | |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 387 | // Don't print implicit specializations, as they are printed when visiting |
| 388 | // corresponding templates. |
| 389 | if (auto FD = dyn_cast<FunctionDecl>(*D)) |
| 390 | if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation && |
| 391 | !isa<ClassTemplateSpecializationDecl>(DC)) |
| 392 | continue; |
| 393 | |
| Joel E. Denny | ae7c944 | 2018-05-15 00:44:14 +0000 | [diff] [blame] | 394 | // The next bits of code handle stuff like "struct {int x;} a,b"; we're |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 395 | // forced to merge the declarations because there's no other way to |
| Joel E. Denny | ae7c944 | 2018-05-15 00:44:14 +0000 | [diff] [blame] | 396 | // refer to the struct in question. When that struct is named instead, we |
| 397 | // also need to merge to avoid splitting off a stand-alone struct |
| 398 | // declaration that produces the warning ext_no_declarators in some |
| 399 | // contexts. |
| 400 | // |
| 401 | // This limited merging is safe without a bunch of other checks because it |
| 402 | // only merges declarations directly referring to the tag, not typedefs. |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 403 | // |
| 404 | // Check whether the current declaration should be grouped with a previous |
| Joel E. Denny | ae7c944 | 2018-05-15 00:44:14 +0000 | [diff] [blame] | 405 | // non-free-standing tag declaration. |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 406 | QualType CurDeclType = getDeclType(*D); |
| 407 | if (!Decls.empty() && !CurDeclType.isNull()) { |
| 408 | QualType BaseType = GetBaseType(CurDeclType); |
| Joel E. Denny | ae7c944 | 2018-05-15 00:44:14 +0000 | [diff] [blame] | 409 | if (!BaseType.isNull() && isa<ElaboratedType>(BaseType) && |
| 410 | cast<ElaboratedType>(BaseType)->getOwnedTagDecl() == Decls[0]) { |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 411 | Decls.push_back(*D); |
| 412 | continue; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | // If we have a merged group waiting to be handled, handle it now. |
| 417 | if (!Decls.empty()) |
| 418 | ProcessDeclGroup(Decls); |
| 419 | |
| Joel E. Denny | ae7c944 | 2018-05-15 00:44:14 +0000 | [diff] [blame] | 420 | // If the current declaration is not a free standing declaration, save it |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 421 | // so we can merge it with the subsequent declaration(s) using it. |
| Joel E. Denny | ae7c944 | 2018-05-15 00:44:14 +0000 | [diff] [blame] | 422 | if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->isFreeStanding()) { |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 423 | Decls.push_back(*D); |
| 424 | continue; |
| 425 | } |
| Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 426 | |
| 427 | if (isa<AccessSpecDecl>(*D)) { |
| 428 | Indentation -= Policy.Indentation; |
| 429 | this->Indent(); |
| 430 | Print(D->getAccess()); |
| 431 | Out << ":\n"; |
| 432 | Indentation += Policy.Indentation; |
| 433 | continue; |
| 434 | } |
| 435 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 436 | this->Indent(); |
| 437 | Visit(*D); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 438 | |
| 439 | // FIXME: Need to be able to tell the DeclPrinter when |
| Yaron Keren | 51db877 | 2014-05-07 09:53:02 +0000 | [diff] [blame] | 440 | const char *Terminator = nullptr; |
| Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 441 | if (isa<OMPThreadPrivateDecl>(*D) || isa<OMPDeclareReductionDecl>(*D) || |
| Alexey Bataev | 25ed0c0 | 2019-03-07 17:54:44 +0000 | [diff] [blame] | 442 | isa<OMPDeclareMapperDecl>(*D) || isa<OMPRequiresDecl>(*D) || |
| 443 | isa<OMPAllocateDecl>(*D)) |
| Yaron Keren | 51db877 | 2014-05-07 09:53:02 +0000 | [diff] [blame] | 444 | Terminator = nullptr; |
| Serge Pavlov | dc586c4 | 2016-10-31 05:11:12 +0000 | [diff] [blame] | 445 | else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->hasBody()) |
| Yaron Keren | 51db877 | 2014-05-07 09:53:02 +0000 | [diff] [blame] | 446 | Terminator = nullptr; |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 447 | else if (auto FD = dyn_cast<FunctionDecl>(*D)) { |
| 448 | if (FD->isThisDeclarationADefinition()) |
| 449 | Terminator = nullptr; |
| 450 | else |
| 451 | Terminator = ";"; |
| 452 | } else if (auto TD = dyn_cast<FunctionTemplateDecl>(*D)) { |
| 453 | if (TD->getTemplatedDecl()->isThisDeclarationADefinition()) |
| 454 | Terminator = nullptr; |
| 455 | else |
| 456 | Terminator = ";"; |
| 457 | } else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) || |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 458 | isa<ObjCImplementationDecl>(*D) || |
| Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 459 | isa<ObjCInterfaceDecl>(*D) || |
| 460 | isa<ObjCProtocolDecl>(*D) || |
| 461 | isa<ObjCCategoryImplDecl>(*D) || |
| 462 | isa<ObjCCategoryDecl>(*D)) |
| Yaron Keren | 51db877 | 2014-05-07 09:53:02 +0000 | [diff] [blame] | 463 | Terminator = nullptr; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 464 | else if (isa<EnumConstantDecl>(*D)) { |
| 465 | DeclContext::decl_iterator Next = D; |
| 466 | ++Next; |
| 467 | if (Next != DEnd) |
| 468 | Terminator = ","; |
| 469 | } else |
| 470 | Terminator = ";"; |
| 471 | |
| 472 | if (Terminator) |
| 473 | Out << Terminator; |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 474 | if (!Policy.TerseOutput && |
| 475 | ((isa<FunctionDecl>(*D) && |
| 476 | cast<FunctionDecl>(*D)->doesThisDeclarationHaveABody()) || |
| 477 | (isa<FunctionTemplateDecl>(*D) && |
| 478 | cast<FunctionTemplateDecl>(*D)->getTemplatedDecl()->doesThisDeclarationHaveABody()))) |
| 479 | ; // StmtPrinter already added '\n' after CompoundStmt. |
| 480 | else |
| 481 | Out << "\n"; |
| Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 482 | |
| 483 | // Declare target attribute is special one, natural spelling for the pragma |
| 484 | // assumes "ending" construct so print it here. |
| 485 | if (D->hasAttr<OMPDeclareTargetDeclAttr>()) |
| 486 | Out << "#pragma omp end declare target\n"; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 489 | if (!Decls.empty()) |
| 490 | ProcessDeclGroup(Decls); |
| 491 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 492 | if (Indent) |
| 493 | Indentation -= Policy.Indentation; |
| 494 | } |
| 495 | |
| 496 | void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 497 | VisitDeclContext(D, false); |
| 498 | } |
| 499 | |
| 500 | void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) { |
| Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 501 | if (!Policy.SuppressSpecifiers) { |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 502 | Out << "typedef "; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 503 | |
| Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 504 | if (D->isModulePrivate()) |
| 505 | Out << "__module_private__ "; |
| 506 | } |
| Steven Watanabe | 9359b8f | 2016-03-18 21:35:59 +0000 | [diff] [blame] | 507 | QualType Ty = D->getTypeSourceInfo()->getType(); |
| 508 | Ty.print(Out, Policy, D->getName(), Indentation); |
| Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 509 | prettyPrintAttributes(D); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 512 | void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 513 | Out << "using " << *D; |
| 514 | prettyPrintAttributes(D); |
| 515 | Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy); |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 518 | void DeclPrinter::VisitEnumDecl(EnumDecl *D) { |
| Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 519 | if (!Policy.SuppressSpecifiers && D->isModulePrivate()) |
| 520 | Out << "__module_private__ "; |
| Joel E. Denny | c2575a3 | 2018-04-24 14:50:23 +0000 | [diff] [blame] | 521 | Out << "enum"; |
| Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 522 | if (D->isScoped()) { |
| 523 | if (D->isScopedUsingClassTag()) |
| Joel E. Denny | c2575a3 | 2018-04-24 14:50:23 +0000 | [diff] [blame] | 524 | Out << " class"; |
| Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 525 | else |
| Joel E. Denny | c2575a3 | 2018-04-24 14:50:23 +0000 | [diff] [blame] | 526 | Out << " struct"; |
| Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 527 | } |
| Joel E. Denny | c2575a3 | 2018-04-24 14:50:23 +0000 | [diff] [blame] | 528 | |
| 529 | prettyPrintAttributes(D); |
| 530 | |
| 531 | Out << ' ' << *D; |
| Douglas Gregor | ec0e366 | 2010-12-01 16:01:08 +0000 | [diff] [blame] | 532 | |
| Serge Pavlov | 08c8de2 | 2016-11-04 06:03:34 +0000 | [diff] [blame] | 533 | if (D->isFixed() && D->getASTContext().getLangOpts().CPlusPlus11) |
| Argyrios Kyrtzidis | a18347e | 2012-05-05 04:20:37 +0000 | [diff] [blame] | 534 | Out << " : " << D->getIntegerType().stream(Policy); |
| Douglas Gregor | ec0e366 | 2010-12-01 16:01:08 +0000 | [diff] [blame] | 535 | |
| John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 536 | if (D->isCompleteDefinition()) { |
| Douglas Gregor | ec0e366 | 2010-12-01 16:01:08 +0000 | [diff] [blame] | 537 | Out << " {\n"; |
| 538 | VisitDeclContext(D); |
| 539 | Indent() << "}"; |
| 540 | } |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | void DeclPrinter::VisitRecordDecl(RecordDecl *D) { |
| Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 544 | if (!Policy.SuppressSpecifiers && D->isModulePrivate()) |
| 545 | Out << "__module_private__ "; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 546 | Out << D->getKindName(); |
| Aaron Ballman | 5388538 | 2014-09-15 16:45:30 +0000 | [diff] [blame] | 547 | |
| 548 | prettyPrintAttributes(D); |
| 549 | |
| Benjamin Kramer | b11416d | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 550 | if (D->getIdentifier()) |
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 551 | Out << ' ' << *D; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 552 | |
| John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 553 | if (D->isCompleteDefinition()) { |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 554 | Out << " {\n"; |
| 555 | VisitDeclContext(D); |
| 556 | Indent() << "}"; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 561 | Out << *D; |
| Jordan Rose | ccca669 | 2017-01-20 03:33:42 +0000 | [diff] [blame] | 562 | prettyPrintAttributes(D); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 563 | if (Expr *Init = D->getInitExpr()) { |
| 564 | Out << " = "; |
| George Karpenkov | 64885ae | 2018-09-15 02:02:31 +0000 | [diff] [blame] | 565 | Init->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
| Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 569 | static void printExplicitSpecifier(ExplicitSpecifier ES, llvm::raw_ostream &Out, |
| 570 | PrintingPolicy &Policy, |
| 571 | unsigned Indentation) { |
| 572 | std::string Proto = "explicit"; |
| 573 | llvm::raw_string_ostream EOut(Proto); |
| 574 | if (ES.getExpr()) { |
| 575 | EOut << "("; |
| 576 | ES.getExpr()->printPretty(EOut, nullptr, Policy, Indentation); |
| 577 | EOut << ")"; |
| 578 | } |
| 579 | EOut << " "; |
| 580 | EOut.flush(); |
| 581 | Out << EOut.str(); |
| 582 | } |
| 583 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 584 | void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { |
| Alexey Bataev | 6d45532 | 2015-10-12 06:59:48 +0000 | [diff] [blame] | 585 | if (!D->getDescribedFunctionTemplate() && |
| 586 | !D->isFunctionTemplateSpecialization()) |
| 587 | prettyPrintPragmas(D); |
| 588 | |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 589 | if (D->isFunctionTemplateSpecialization()) |
| 590 | Out << "template<> "; |
| Alex Lorenz | 47fd10c | 2017-04-18 15:12:34 +0000 | [diff] [blame] | 591 | else if (!D->getDescribedFunctionTemplate()) { |
| 592 | for (unsigned I = 0, NumTemplateParams = D->getNumTemplateParameterLists(); |
| 593 | I < NumTemplateParams; ++I) |
| 594 | printTemplateParameters(D->getTemplateParameterList(I)); |
| 595 | } |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 596 | |
| Fariborz Jahanian | 69c403c | 2012-12-05 22:19:06 +0000 | [diff] [blame] | 597 | CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D); |
| Benjamin Kramer | 00e8a19 | 2014-02-25 18:03:55 +0000 | [diff] [blame] | 598 | CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D); |
| Richard Smith | 057ec50 | 2017-02-18 01:01:48 +0000 | [diff] [blame] | 599 | CXXDeductionGuideDecl *GuideDecl = dyn_cast<CXXDeductionGuideDecl>(D); |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 600 | if (!Policy.SuppressSpecifiers) { |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 601 | switch (D->getStorageClass()) { |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 602 | case SC_None: break; |
| 603 | case SC_Extern: Out << "extern "; break; |
| 604 | case SC_Static: Out << "static "; break; |
| 605 | case SC_PrivateExtern: Out << "__private_extern__ "; break; |
| Anastasia Stulova | bcea696 | 2015-09-30 14:08:20 +0000 | [diff] [blame] | 606 | case SC_Auto: case SC_Register: |
| Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 607 | llvm_unreachable("invalid for functions"); |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 608 | } |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 609 | |
| Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 610 | if (D->isInlineSpecified()) Out << "inline "; |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 611 | if (D->isVirtualAsWritten()) Out << "virtual "; |
| Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 612 | if (D->isModulePrivate()) Out << "__module_private__ "; |
| Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 613 | if (D->isConstexprSpecified() && !D->isExplicitlyDefaulted()) |
| 614 | Out << "constexpr "; |
| 615 | if (D->isConsteval()) Out << "consteval "; |
| Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 616 | ExplicitSpecifier ExplicitSpec = ExplicitSpecifier::getFromDecl(D); |
| 617 | if (ExplicitSpec.isSpecified()) |
| 618 | printExplicitSpecifier(ExplicitSpec, Out, Policy, Indentation); |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 619 | } |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 620 | |
| Douglas Gregor | 2d042f1 | 2009-05-30 05:39:39 +0000 | [diff] [blame] | 621 | PrintingPolicy SubPolicy(Policy); |
| 622 | SubPolicy.SuppressSpecifiers = false; |
| Alex Lorenz | a981c7d | 2017-04-11 16:46:03 +0000 | [diff] [blame] | 623 | std::string Proto; |
| Serge Pavlov | 842022a | 2017-11-23 05:38:20 +0000 | [diff] [blame] | 624 | |
| 625 | if (Policy.FullyQualifiedName) { |
| 626 | Proto += D->getQualifiedNameAsString(); |
| 627 | } else { |
| 628 | if (!Policy.SuppressScope) { |
| 629 | if (const NestedNameSpecifier *NS = D->getQualifier()) { |
| 630 | llvm::raw_string_ostream OS(Proto); |
| 631 | NS->print(OS, Policy); |
| 632 | } |
| Alex Lorenz | a981c7d | 2017-04-11 16:46:03 +0000 | [diff] [blame] | 633 | } |
| Serge Pavlov | 842022a | 2017-11-23 05:38:20 +0000 | [diff] [blame] | 634 | Proto += D->getNameInfo().getAsString(); |
| Alex Lorenz | a981c7d | 2017-04-11 16:46:03 +0000 | [diff] [blame] | 635 | } |
| Serge Pavlov | 842022a | 2017-11-23 05:38:20 +0000 | [diff] [blame] | 636 | |
| Richard Smith | 057ec50 | 2017-02-18 01:01:48 +0000 | [diff] [blame] | 637 | if (GuideDecl) |
| 638 | Proto = GuideDecl->getDeducedTemplate()->getDeclName().getAsString(); |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 639 | if (const TemplateArgumentList *TArgs = D->getTemplateSpecializationArgs()) { |
| 640 | llvm::raw_string_ostream POut(Proto); |
| Alex Lorenz | 36070ed | 2017-08-17 13:41:55 +0000 | [diff] [blame] | 641 | DeclPrinter TArgPrinter(POut, SubPolicy, Context, Indentation); |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 642 | TArgPrinter.printTemplateArguments(*TArgs); |
| 643 | } |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 644 | |
| Abramo Bagnara | 6d81063 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 645 | QualType Ty = D->getType(); |
| John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 646 | while (const ParenType *PT = dyn_cast<ParenType>(Ty)) { |
| Abramo Bagnara | 6d81063 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 647 | Proto = '(' + Proto + ')'; |
| 648 | Ty = PT->getInnerType(); |
| 649 | } |
| 650 | |
| Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 651 | if (const FunctionType *AFT = Ty->getAs<FunctionType>()) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 652 | const FunctionProtoType *FT = nullptr; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 653 | if (D->hasWrittenPrototype()) |
| 654 | FT = dyn_cast<FunctionProtoType>(AFT); |
| 655 | |
| 656 | Proto += "("; |
| 657 | if (FT) { |
| 658 | llvm::raw_string_ostream POut(Proto); |
| Alex Lorenz | 36070ed | 2017-08-17 13:41:55 +0000 | [diff] [blame] | 659 | DeclPrinter ParamPrinter(POut, SubPolicy, Context, Indentation); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 660 | for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { |
| 661 | if (i) POut << ", "; |
| 662 | ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); |
| 663 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 664 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 665 | if (FT->isVariadic()) { |
| 666 | if (D->getNumParams()) POut << ", "; |
| 667 | POut << "..."; |
| 668 | } |
| Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 669 | } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) { |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 670 | for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { |
| 671 | if (i) |
| 672 | Proto += ", "; |
| 673 | Proto += D->getParamDecl(i)->getNameAsString(); |
| 674 | } |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | Proto += ")"; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 678 | |
| David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 679 | if (FT) { |
| 680 | if (FT->isConst()) |
| Douglas Gregor | 8fc96fc | 2010-11-19 18:44:34 +0000 | [diff] [blame] | 681 | Proto += " const"; |
| David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 682 | if (FT->isVolatile()) |
| Douglas Gregor | 8fc96fc | 2010-11-19 18:44:34 +0000 | [diff] [blame] | 683 | Proto += " volatile"; |
| David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 684 | if (FT->isRestrict()) |
| Douglas Gregor | 8fc96fc | 2010-11-19 18:44:34 +0000 | [diff] [blame] | 685 | Proto += " restrict"; |
| Benjamin Kramer | 2907b08 | 2014-02-25 18:49:49 +0000 | [diff] [blame] | 686 | |
| 687 | switch (FT->getRefQualifier()) { |
| 688 | case RQ_None: |
| 689 | break; |
| 690 | case RQ_LValue: |
| 691 | Proto += " &"; |
| 692 | break; |
| 693 | case RQ_RValue: |
| 694 | Proto += " &&"; |
| 695 | break; |
| 696 | } |
| Douglas Gregor | 8fc96fc | 2010-11-19 18:44:34 +0000 | [diff] [blame] | 697 | } |
| Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 698 | |
| 699 | if (FT && FT->hasDynamicExceptionSpec()) { |
| Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 700 | Proto += " throw("; |
| Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 701 | if (FT->getExceptionSpecType() == EST_MSAny) |
| Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 702 | Proto += "..."; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 703 | else |
| Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 704 | for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) { |
| 705 | if (I) |
| 706 | Proto += ", "; |
| Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 707 | |
| Dmitri Gribenko | 76bb5cabfa | 2012-09-10 21:20:09 +0000 | [diff] [blame] | 708 | Proto += FT->getExceptionType(I).getAsString(SubPolicy); |
| Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 709 | } |
| 710 | Proto += ")"; |
| Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 711 | } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) { |
| 712 | Proto += " noexcept"; |
| Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 713 | if (isComputedNoexcept(FT->getExceptionSpecType())) { |
| Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 714 | Proto += "("; |
| 715 | llvm::raw_string_ostream EOut(Proto); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 716 | FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy, |
| Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 717 | Indentation); |
| 718 | EOut.flush(); |
| 719 | Proto += EOut.str(); |
| 720 | Proto += ")"; |
| 721 | } |
| Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| Fariborz Jahanian | 69c403c | 2012-12-05 22:19:06 +0000 | [diff] [blame] | 724 | if (CDecl) { |
| Alex Lorenz | dc616fa | 2017-11-16 01:31:27 +0000 | [diff] [blame] | 725 | if (!Policy.TerseOutput) |
| 726 | PrintConstructorInitializers(CDecl, Proto); |
| Benjamin Kramer | 2907b08 | 2014-02-25 18:49:49 +0000 | [diff] [blame] | 727 | } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) { |
| Richard Smith | e656076 | 2013-02-22 05:54:51 +0000 | [diff] [blame] | 728 | if (FT && FT->hasTrailingReturn()) { |
| Richard Smith | 057ec50 | 2017-02-18 01:01:48 +0000 | [diff] [blame] | 729 | if (!GuideDecl) |
| 730 | Out << "auto "; |
| 731 | Out << Proto << " -> "; |
| Richard Smith | e656076 | 2013-02-22 05:54:51 +0000 | [diff] [blame] | 732 | Proto.clear(); |
| 733 | } |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 734 | AFT->getReturnType().print(Out, Policy, Proto); |
| Benjamin Kramer | 00e8a19 | 2014-02-25 18:03:55 +0000 | [diff] [blame] | 735 | Proto.clear(); |
| Richard Smith | e656076 | 2013-02-22 05:54:51 +0000 | [diff] [blame] | 736 | } |
| Benjamin Kramer | 00e8a19 | 2014-02-25 18:03:55 +0000 | [diff] [blame] | 737 | Out << Proto; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 738 | } else { |
| Argyrios Kyrtzidis | a18347e | 2012-05-05 04:20:37 +0000 | [diff] [blame] | 739 | Ty.print(Out, Policy, Proto); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 742 | prettyPrintAttributes(D); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 743 | |
| 744 | if (D->isPure()) |
| 745 | Out << " = 0"; |
| Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 746 | else if (D->isDeletedAsWritten()) |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 747 | Out << " = delete"; |
| Fariborz Jahanian | de872af | 2012-12-05 22:53:06 +0000 | [diff] [blame] | 748 | else if (D->isExplicitlyDefaulted()) |
| 749 | Out << " = default"; |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 750 | else if (D->doesThisDeclarationHaveABody()) { |
| 751 | if (!Policy.TerseOutput) { |
| 752 | if (!D->hasPrototype() && D->getNumParams()) { |
| 753 | // This is a K&R function definition, so we need to print the |
| 754 | // parameters. |
| 755 | Out << '\n'; |
| Alex Lorenz | 36070ed | 2017-08-17 13:41:55 +0000 | [diff] [blame] | 756 | DeclPrinter ParamPrinter(Out, SubPolicy, Context, Indentation); |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 757 | Indentation += Policy.Indentation; |
| 758 | for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { |
| 759 | Indent(); |
| 760 | ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); |
| 761 | Out << ";\n"; |
| 762 | } |
| 763 | Indentation -= Policy.Indentation; |
| 764 | } else |
| 765 | Out << ' '; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 766 | |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 767 | if (D->getBody()) |
| 768 | D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation); |
| 769 | } else { |
| Alex Lorenz | 35019db | 2017-11-16 01:28:25 +0000 | [diff] [blame] | 770 | if (!Policy.TerseOutput && isa<CXXConstructorDecl>(*D)) |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 771 | Out << " {}"; |
| 772 | } |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | |
| Fariborz Jahanian | bfc3ef5 | 2012-12-05 00:38:44 +0000 | [diff] [blame] | 776 | void DeclPrinter::VisitFriendDecl(FriendDecl *D) { |
| 777 | if (TypeSourceInfo *TSI = D->getFriendType()) { |
| Enea Zaffanella | eb22c87 | 2013-01-31 09:54:08 +0000 | [diff] [blame] | 778 | unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists(); |
| 779 | for (unsigned i = 0; i < NumTPLists; ++i) |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 780 | printTemplateParameters(D->getFriendTypeTemplateParameterList(i)); |
| Fariborz Jahanian | 7a16a02 | 2012-12-21 21:43:05 +0000 | [diff] [blame] | 781 | Out << "friend "; |
| 782 | Out << " " << TSI->getType().getAsString(Policy); |
| Fariborz Jahanian | bfc3ef5 | 2012-12-05 00:38:44 +0000 | [diff] [blame] | 783 | } |
| 784 | else if (FunctionDecl *FD = |
| 785 | dyn_cast<FunctionDecl>(D->getFriendDecl())) { |
| 786 | Out << "friend "; |
| 787 | VisitFunctionDecl(FD); |
| 788 | } |
| 789 | else if (FunctionTemplateDecl *FTD = |
| 790 | dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) { |
| 791 | Out << "friend "; |
| 792 | VisitFunctionTemplateDecl(FTD); |
| 793 | } |
| 794 | else if (ClassTemplateDecl *CTD = |
| 795 | dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) { |
| 796 | Out << "friend "; |
| Fariborz Jahanian | 7a16a02 | 2012-12-21 21:43:05 +0000 | [diff] [blame] | 797 | VisitRedeclarableTemplateDecl(CTD); |
| Fariborz Jahanian | bfc3ef5 | 2012-12-05 00:38:44 +0000 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 801 | void DeclPrinter::VisitFieldDecl(FieldDecl *D) { |
| Alexey Bataev | 6d45532 | 2015-10-12 06:59:48 +0000 | [diff] [blame] | 802 | // FIXME: add printing of pragma attributes if required. |
| Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 803 | if (!Policy.SuppressSpecifiers && D->isMutable()) |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 804 | Out << "mutable "; |
| Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 805 | if (!Policy.SuppressSpecifiers && D->isModulePrivate()) |
| 806 | Out << "__module_private__ "; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 807 | |
| Fariborz Jahanian | b5f3468 | 2013-05-01 20:53:21 +0000 | [diff] [blame] | 808 | Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()). |
| Steven Watanabe | 9359b8f | 2016-03-18 21:35:59 +0000 | [diff] [blame] | 809 | stream(Policy, D->getName(), Indentation); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 810 | |
| 811 | if (D->isBitField()) { |
| 812 | Out << " : "; |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 813 | D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 814 | } |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 815 | |
| 816 | Expr *Init = D->getInClassInitializer(); |
| 817 | if (!Policy.SuppressInitializers && Init) { |
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 818 | if (D->getInClassInitStyle() == ICIS_ListInit) |
| 819 | Out << " "; |
| 820 | else |
| 821 | Out << " = "; |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 822 | Init->printPretty(Out, nullptr, Policy, Indentation); |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 823 | } |
| Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 824 | prettyPrintAttributes(D); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 827 | void DeclPrinter::VisitLabelDecl(LabelDecl *D) { |
| Benjamin Kramer | db0fc51 | 2012-02-07 11:57:57 +0000 | [diff] [blame] | 828 | Out << *D << ":"; |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 829 | } |
| 830 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 831 | void DeclPrinter::VisitVarDecl(VarDecl *D) { |
| Alexey Bataev | 6d45532 | 2015-10-12 06:59:48 +0000 | [diff] [blame] | 832 | prettyPrintPragmas(D); |
| Vassil Vassilev | 1002373 | 2016-07-08 21:09:08 +0000 | [diff] [blame] | 833 | |
| 834 | QualType T = D->getTypeSourceInfo() |
| 835 | ? D->getTypeSourceInfo()->getType() |
| 836 | : D->getASTContext().getUnqualifiedObjCPointerType(D->getType()); |
| 837 | |
| Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 838 | if (!Policy.SuppressSpecifiers) { |
| 839 | StorageClass SC = D->getStorageClass(); |
| 840 | if (SC != SC_None) |
| 841 | Out << VarDecl::getStorageClassSpecifierString(SC) << " "; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 842 | |
| Enea Zaffanella | acb8ecd | 2013-05-04 08:27:07 +0000 | [diff] [blame] | 843 | switch (D->getTSCSpec()) { |
| 844 | case TSCS_unspecified: |
| Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 845 | break; |
| Enea Zaffanella | acb8ecd | 2013-05-04 08:27:07 +0000 | [diff] [blame] | 846 | case TSCS___thread: |
| 847 | Out << "__thread "; |
| 848 | break; |
| 849 | case TSCS__Thread_local: |
| Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 850 | Out << "_Thread_local "; |
| 851 | break; |
| Enea Zaffanella | acb8ecd | 2013-05-04 08:27:07 +0000 | [diff] [blame] | 852 | case TSCS_thread_local: |
| Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 853 | Out << "thread_local "; |
| 854 | break; |
| 855 | } |
| 856 | |
| 857 | if (D->isModulePrivate()) |
| 858 | Out << "__module_private__ "; |
| Vassil Vassilev | 1002373 | 2016-07-08 21:09:08 +0000 | [diff] [blame] | 859 | |
| 860 | if (D->isConstexpr()) { |
| 861 | Out << "constexpr "; |
| 862 | T.removeLocalConst(); |
| 863 | } |
| Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 864 | } |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 865 | |
| Richard Smith | a4bb292 | 2014-07-23 03:17:06 +0000 | [diff] [blame] | 866 | printDeclType(T, D->getName()); |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 867 | Expr *Init = D->getInit(); |
| 868 | if (!Policy.SuppressInitializers && Init) { |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 869 | bool ImplicitInit = false; |
| Dmitri Gribenko | b614fab | 2013-02-03 23:02:47 +0000 | [diff] [blame] | 870 | if (CXXConstructExpr *Construct = |
| 871 | dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) { |
| Dmitri Gribenko | 6835e37 | 2013-01-27 21:28:24 +0000 | [diff] [blame] | 872 | if (D->getInitStyle() == VarDecl::CallInit && |
| 873 | !Construct->isListInitialization()) { |
| 874 | ImplicitInit = Construct->getNumArgs() == 0 || |
| 875 | Construct->getArg(0)->isDefaultArgument(); |
| 876 | } |
| 877 | } |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 878 | if (!ImplicitInit) { |
| Eli Friedman | 92125c4 | 2012-10-19 20:36:44 +0000 | [diff] [blame] | 879 | if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init)) |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 880 | Out << "("; |
| 881 | else if (D->getInitStyle() == VarDecl::CInit) { |
| 882 | Out << " = "; |
| 883 | } |
| Benjamin Kramer | 54f81ed | 2016-01-25 10:34:06 +0000 | [diff] [blame] | 884 | PrintingPolicy SubPolicy(Policy); |
| 885 | SubPolicy.SuppressSpecifiers = false; |
| Steven Watanabe | 9359b8f | 2016-03-18 21:35:59 +0000 | [diff] [blame] | 886 | SubPolicy.IncludeTagDefinition = false; |
| Benjamin Kramer | 54f81ed | 2016-01-25 10:34:06 +0000 | [diff] [blame] | 887 | Init->printPretty(Out, nullptr, SubPolicy, Indentation); |
| Eli Friedman | 92125c4 | 2012-10-19 20:36:44 +0000 | [diff] [blame] | 888 | if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init)) |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 889 | Out << ")"; |
| Ted Kremenek | 3586938 | 2010-09-17 23:04:38 +0000 | [diff] [blame] | 890 | } |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 891 | } |
| Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 892 | prettyPrintAttributes(D); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) { |
| 896 | VisitVarDecl(D); |
| 897 | } |
| 898 | |
| 899 | void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { |
| 900 | Out << "__asm ("; |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 901 | D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 902 | Out << ")"; |
| 903 | } |
| 904 | |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 905 | void DeclPrinter::VisitImportDecl(ImportDecl *D) { |
| Douglas Gregor | c50d492 | 2012-12-11 22:11:52 +0000 | [diff] [blame] | 906 | Out << "@import " << D->getImportedModule()->getFullModuleName() |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 907 | << ";\n"; |
| 908 | } |
| 909 | |
| Peter Collingbourne | 7d8a0b5 | 2011-03-16 18:37:27 +0000 | [diff] [blame] | 910 | void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 911 | Out << "static_assert("; |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 912 | D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation); |
| David Majnemer | cdffc36 | 2015-06-05 18:03:58 +0000 | [diff] [blame] | 913 | if (StringLiteral *SL = D->getMessage()) { |
| 914 | Out << ", "; |
| 915 | SL->printPretty(Out, nullptr, Policy, Indentation); |
| 916 | } |
| Peter Collingbourne | 7d8a0b5 | 2011-03-16 18:37:27 +0000 | [diff] [blame] | 917 | Out << ")"; |
| 918 | } |
| 919 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 920 | //---------------------------------------------------------------------------- |
| 921 | // C++ declarations |
| 922 | //---------------------------------------------------------------------------- |
| Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 923 | void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) { |
| Douglas Gregor | b526370 | 2012-05-01 01:43:38 +0000 | [diff] [blame] | 924 | if (D->isInline()) |
| 925 | Out << "inline "; |
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 926 | Out << "namespace " << *D << " {\n"; |
| Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 927 | VisitDeclContext(D); |
| 928 | Indent() << "}"; |
| 929 | } |
| 930 | |
| Douglas Gregor | 3bc6e4c | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 931 | void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
| 932 | Out << "using namespace "; |
| 933 | if (D->getQualifier()) |
| 934 | D->getQualifier()->print(Out, Policy); |
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 935 | Out << *D->getNominatedNamespaceAsWritten(); |
| Douglas Gregor | 3bc6e4c | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| Douglas Gregor | 1823193 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 938 | void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 939 | Out << "namespace " << *D << " = "; |
| Douglas Gregor | 1823193 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 940 | if (D->getQualifier()) |
| 941 | D->getQualifier()->print(Out, Policy); |
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 942 | Out << *D->getAliasedNamespace(); |
| Douglas Gregor | 1823193 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 943 | } |
| 944 | |
| Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 945 | void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) { |
| 946 | prettyPrintAttributes(D); |
| Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 947 | } |
| 948 | |
| Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 949 | void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) { |
| Alexey Bataev | 6d45532 | 2015-10-12 06:59:48 +0000 | [diff] [blame] | 950 | // FIXME: add printing of pragma attributes if required. |
| Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 951 | if (!Policy.SuppressSpecifiers && D->isModulePrivate()) |
| 952 | Out << "__module_private__ "; |
| Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 953 | Out << D->getKindName(); |
| Aaron Ballman | 5388538 | 2014-09-15 16:45:30 +0000 | [diff] [blame] | 954 | |
| 955 | prettyPrintAttributes(D); |
| 956 | |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 957 | if (D->getIdentifier()) { |
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 958 | Out << ' ' << *D; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 959 | |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 960 | if (auto S = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 961 | printTemplateArguments(S->getTemplateArgs(), S->getTemplateParameters()); |
| 962 | else if (auto S = dyn_cast<ClassTemplateSpecializationDecl>(D)) |
| 963 | printTemplateArguments(S->getTemplateArgs()); |
| 964 | } |
| 965 | |
| John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 966 | if (D->isCompleteDefinition()) { |
| Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 967 | // Print the base classes |
| 968 | if (D->getNumBases()) { |
| 969 | Out << " : "; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 970 | for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(), |
| 971 | BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) { |
| Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 972 | if (Base != D->bases_begin()) |
| 973 | Out << ", "; |
| 974 | |
| 975 | if (Base->isVirtual()) |
| 976 | Out << "virtual "; |
| 977 | |
| Anders Carlsson | 6df9e07 | 2009-08-29 20:36:12 +0000 | [diff] [blame] | 978 | AccessSpecifier AS = Base->getAccessSpecifierAsWritten(); |
| Richard Smith | a593419 | 2014-07-23 03:22:10 +0000 | [diff] [blame] | 979 | if (AS != AS_none) { |
| Anders Carlsson | 6df9e07 | 2009-08-29 20:36:12 +0000 | [diff] [blame] | 980 | Print(AS); |
| Richard Smith | a593419 | 2014-07-23 03:22:10 +0000 | [diff] [blame] | 981 | Out << " "; |
| 982 | } |
| 983 | Out << Base->getType().getAsString(Policy); |
| Douglas Gregor | 5fc8c9e | 2011-04-27 17:07:55 +0000 | [diff] [blame] | 984 | |
| 985 | if (Base->isPackExpansion()) |
| 986 | Out << "..."; |
| Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | |
| 990 | // Print the class definition |
| Douglas Gregor | 7a1a7cb | 2009-05-31 07:13:39 +0000 | [diff] [blame] | 991 | // FIXME: Doesn't print access specifiers, e.g., "public:" |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 992 | if (Policy.TerseOutput) { |
| 993 | Out << " {}"; |
| 994 | } else { |
| 995 | Out << " {\n"; |
| 996 | VisitDeclContext(D); |
| 997 | Indent() << "}"; |
| 998 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 999 | } |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 1003 | const char *l; |
| Jonas Devlieghere | 5c49c26 | 2019-09-23 23:49:36 +0000 | [diff] [blame] | 1004 | if (D->getLanguage() == LinkageSpecDecl::lang_c) |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1005 | l = "C"; |
| Jonas Devlieghere | 5c49c26 | 2019-09-23 23:49:36 +0000 | [diff] [blame] | 1006 | else { |
| 1007 | assert(D->getLanguage() == LinkageSpecDecl::lang_cxx && |
| 1008 | "unknown language in linkage specification"); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1009 | l = "C++"; |
| 1010 | } |
| 1011 | |
| 1012 | Out << "extern \"" << l << "\" "; |
| 1013 | if (D->hasBraces()) { |
| 1014 | Out << "{\n"; |
| 1015 | VisitDeclContext(D); |
| 1016 | Indent() << "}"; |
| 1017 | } else |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1018 | Visit(*D->decls_begin()); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1021 | void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params, |
| 1022 | bool OmitTemplateKW) { |
| Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 1023 | assert(Params); |
| Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 1024 | |
| Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1025 | if (!OmitTemplateKW) |
| 1026 | Out << "template "; |
| 1027 | Out << '<'; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1028 | |
| Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1029 | bool NeedComma = false; |
| 1030 | for (const Decl *Param : *Params) { |
| 1031 | if (Param->isImplicit()) |
| 1032 | continue; |
| 1033 | |
| 1034 | if (NeedComma) |
| Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 1035 | Out << ", "; |
| Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1036 | else |
| 1037 | NeedComma = true; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1038 | |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1039 | if (auto TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1040 | |
| Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 1041 | if (TTP->wasDeclaredWithTypename()) |
| Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1042 | Out << "typename"; |
| Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 1043 | else |
| Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1044 | Out << "class"; |
| Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 1045 | |
| Anders Carlsson | fb1d776 | 2009-06-12 22:23:22 +0000 | [diff] [blame] | 1046 | if (TTP->isParameterPack()) |
| Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1047 | Out << " ..."; |
| 1048 | else if (!TTP->getName().empty()) |
| 1049 | Out << ' '; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1050 | |
| Benjamin Kramer | db0fc51 | 2012-02-07 11:57:57 +0000 | [diff] [blame] | 1051 | Out << *TTP; |
| Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 1052 | |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1053 | if (TTP->hasDefaultArgument()) { |
| Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 1054 | Out << " = "; |
| 1055 | Out << TTP->getDefaultArgument().getAsString(Policy); |
| 1056 | }; |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1057 | } else if (auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| Richard Smith | a4bb292 | 2014-07-23 03:17:06 +0000 | [diff] [blame] | 1058 | StringRef Name; |
| 1059 | if (IdentifierInfo *II = NTTP->getIdentifier()) |
| 1060 | Name = II->getName(); |
| 1061 | printDeclType(NTTP->getType(), Name, NTTP->isParameterPack()); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1062 | |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1063 | if (NTTP->hasDefaultArgument()) { |
| Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 1064 | Out << " = "; |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1065 | NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy, |
| 1066 | Indentation); |
| Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 1067 | } |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1068 | } else if (auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| Richard Smith | 030f499 | 2011-04-15 13:38:57 +0000 | [diff] [blame] | 1069 | VisitTemplateDecl(TTPD); |
| 1070 | // FIXME: print the default argument, if present. |
| Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 1071 | } |
| 1072 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1073 | |
| Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1074 | Out << '>'; |
| 1075 | if (!OmitTemplateKW) |
| 1076 | Out << ' '; |
| Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1079 | void DeclPrinter::printTemplateArguments(const TemplateArgumentList &Args, |
| 1080 | const TemplateParameterList *Params) { |
| 1081 | Out << "<"; |
| 1082 | for (size_t I = 0, E = Args.size(); I < E; ++I) { |
| 1083 | const TemplateArgument &A = Args[I]; |
| 1084 | if (I) |
| 1085 | Out << ", "; |
| 1086 | if (Params) { |
| 1087 | if (A.getKind() == TemplateArgument::Type) |
| 1088 | if (auto T = A.getAsType()->getAs<TemplateTypeParmType>()) { |
| 1089 | auto P = cast<TemplateTypeParmDecl>(Params->getParam(T->getIndex())); |
| 1090 | Out << *P; |
| 1091 | continue; |
| 1092 | } |
| 1093 | if (A.getKind() == TemplateArgument::Template) { |
| 1094 | if (auto T = A.getAsTemplate().getAsTemplateDecl()) |
| 1095 | if (auto TD = dyn_cast<TemplateTemplateParmDecl>(T)) { |
| 1096 | auto P = cast<TemplateTemplateParmDecl>( |
| 1097 | Params->getParam(TD->getIndex())); |
| 1098 | Out << *P; |
| 1099 | continue; |
| 1100 | } |
| 1101 | } |
| 1102 | if (A.getKind() == TemplateArgument::Expression) { |
| 1103 | if (auto E = dyn_cast<DeclRefExpr>(A.getAsExpr())) |
| 1104 | if (auto N = dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { |
| 1105 | auto P = cast<NonTypeTemplateParmDecl>( |
| 1106 | Params->getParam(N->getIndex())); |
| 1107 | Out << *P; |
| 1108 | continue; |
| 1109 | } |
| 1110 | } |
| 1111 | } |
| 1112 | A.print(Policy, Out); |
| 1113 | } |
| 1114 | Out << ">"; |
| 1115 | } |
| 1116 | |
| Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 1117 | void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) { |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1118 | printTemplateParameters(D->getTemplateParameters()); |
| Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 1119 | |
| Richard Smith | 030f499 | 2011-04-15 13:38:57 +0000 | [diff] [blame] | 1120 | if (const TemplateTemplateParmDecl *TTP = |
| 1121 | dyn_cast<TemplateTemplateParmDecl>(D)) { |
| Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1122 | Out << "class "; |
| 1123 | if (TTP->isParameterPack()) |
| 1124 | Out << "..."; |
| 1125 | Out << D->getName(); |
| Saar Raz | d7aae33 | 2019-07-10 21:25:49 +0000 | [diff] [blame] | 1126 | } else if (auto *TD = D->getTemplatedDecl()) |
| 1127 | Visit(TD); |
| 1128 | else if (const auto *Concept = dyn_cast<ConceptDecl>(D)) { |
| 1129 | Out << "concept " << Concept->getName() << " = " ; |
| 1130 | Concept->getConstraintExpr()->printPretty(Out, nullptr, Policy, |
| 1131 | Indentation); |
| 1132 | Out << ";"; |
| Craig Silverstein | 4a5d086 | 2010-07-09 20:25:10 +0000 | [diff] [blame] | 1133 | } |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
| Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 1136 | void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| Alexey Bataev | 6d45532 | 2015-10-12 06:59:48 +0000 | [diff] [blame] | 1137 | prettyPrintPragmas(D->getTemplatedDecl()); |
| Alex Lorenz | 47fd10c | 2017-04-18 15:12:34 +0000 | [diff] [blame] | 1138 | // Print any leading template parameter lists. |
| 1139 | if (const FunctionDecl *FD = D->getTemplatedDecl()) { |
| 1140 | for (unsigned I = 0, NumTemplateParams = FD->getNumTemplateParameterLists(); |
| 1141 | I < NumTemplateParams; ++I) |
| 1142 | printTemplateParameters(FD->getTemplateParameterList(I)); |
| 1143 | } |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1144 | VisitRedeclarableTemplateDecl(D); |
| Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 1145 | // Declare target attribute is special one, natural spelling for the pragma |
| 1146 | // assumes "ending" construct so print it here. |
| 1147 | if (D->getTemplatedDecl()->hasAttr<OMPDeclareTargetDeclAttr>()) |
| 1148 | Out << "#pragma omp end declare target\n"; |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1149 | |
| Richard Smith | 057ec50 | 2017-02-18 01:01:48 +0000 | [diff] [blame] | 1150 | // Never print "instantiations" for deduction guides (they don't really |
| 1151 | // have them). |
| 1152 | if (PrintInstantiation && |
| 1153 | !isa<CXXDeductionGuideDecl>(D->getTemplatedDecl())) { |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1154 | FunctionDecl *PrevDecl = D->getTemplatedDecl(); |
| 1155 | const FunctionDecl *Def; |
| 1156 | if (PrevDecl->isDefined(Def) && Def != PrevDecl) |
| 1157 | return; |
| 1158 | for (auto *I : D->specializations()) |
| 1159 | if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) { |
| 1160 | if (!PrevDecl->isThisDeclarationADefinition()) |
| 1161 | Out << ";\n"; |
| 1162 | Indent(); |
| 1163 | prettyPrintPragmas(I); |
| 1164 | Visit(I); |
| 1165 | } |
| 1166 | } |
| Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1170 | VisitRedeclarableTemplateDecl(D); |
| Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 1171 | |
| Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1172 | if (PrintInstantiation) { |
| 1173 | for (auto *I : D->specializations()) |
| 1174 | if (I->getSpecializationKind() == TSK_ImplicitInstantiation) { |
| 1175 | if (D->isThisDeclarationADefinition()) |
| 1176 | Out << ";"; |
| 1177 | Out << "\n"; |
| 1178 | Visit(I); |
| 1179 | } |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | void DeclPrinter::VisitClassTemplateSpecializationDecl( |
| 1184 | ClassTemplateSpecializationDecl *D) { |
| 1185 | Out << "template<> "; |
| 1186 | VisitCXXRecordDecl(D); |
| 1187 | } |
| 1188 | |
| 1189 | void DeclPrinter::VisitClassTemplatePartialSpecializationDecl( |
| 1190 | ClassTemplatePartialSpecializationDecl *D) { |
| 1191 | printTemplateParameters(D->getTemplateParameters()); |
| 1192 | VisitCXXRecordDecl(D); |
| Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1195 | //---------------------------------------------------------------------------- |
| 1196 | // Objective-C declarations |
| 1197 | //---------------------------------------------------------------------------- |
| 1198 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1199 | void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx, |
| 1200 | Decl::ObjCDeclQualifier Quals, |
| Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1201 | QualType T) { |
| 1202 | Out << '('; |
| 1203 | if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In) |
| 1204 | Out << "in "; |
| 1205 | if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout) |
| 1206 | Out << "inout "; |
| 1207 | if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out) |
| 1208 | Out << "out "; |
| 1209 | if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy) |
| 1210 | Out << "bycopy "; |
| 1211 | if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref) |
| 1212 | Out << "byref "; |
| 1213 | if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway) |
| 1214 | Out << "oneway "; |
| 1215 | if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) { |
| Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 1216 | if (auto nullability = AttributedType::stripOuterNullability(T)) |
| 1217 | Out << getNullabilitySpelling(*nullability, true) << ' '; |
| Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1218 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1219 | |
| Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1220 | Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy); |
| 1221 | Out << ')'; |
| 1222 | } |
| 1223 | |
| Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1224 | void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) { |
| 1225 | Out << "<"; |
| 1226 | unsigned First = true; |
| 1227 | for (auto *Param : *Params) { |
| 1228 | if (First) { |
| 1229 | First = false; |
| 1230 | } else { |
| 1231 | Out << ", "; |
| 1232 | } |
| 1233 | |
| Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 1234 | switch (Param->getVariance()) { |
| 1235 | case ObjCTypeParamVariance::Invariant: |
| 1236 | break; |
| 1237 | |
| 1238 | case ObjCTypeParamVariance::Covariant: |
| 1239 | Out << "__covariant "; |
| 1240 | break; |
| 1241 | |
| 1242 | case ObjCTypeParamVariance::Contravariant: |
| 1243 | Out << "__contravariant "; |
| 1244 | break; |
| 1245 | } |
| 1246 | |
| Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1247 | Out << Param->getDeclName().getAsString(); |
| 1248 | |
| 1249 | if (Param->hasExplicitBound()) { |
| 1250 | Out << " : " << Param->getUnderlyingType().getAsString(Policy); |
| 1251 | } |
| 1252 | } |
| 1253 | Out << ">"; |
| 1254 | } |
| 1255 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1256 | void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) { |
| 1257 | if (OMD->isInstanceMethod()) |
| Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1258 | Out << "- "; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1259 | else |
| Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1260 | Out << "+ "; |
| Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1261 | if (!OMD->getReturnType().isNull()) { |
| 1262 | PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(), |
| 1263 | OMD->getReturnType()); |
| 1264 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1266 | std::string name = OMD->getSelector().getAsString(); |
| 1267 | std::string::size_type pos, lastPos = 0; |
| David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 1268 | for (const auto *PI : OMD->parameters()) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1269 | // FIXME: selector is missing here! |
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1270 | pos = name.find_first_of(':', lastPos); |
| Alex Lorenz | bbf4f70 | 2017-06-02 15:02:59 +0000 | [diff] [blame] | 1271 | if (lastPos != 0) |
| 1272 | Out << " "; |
| 1273 | Out << name.substr(lastPos, pos - lastPos) << ':'; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1274 | PrintObjCMethodType(OMD->getASTContext(), |
| Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1275 | PI->getObjCDeclQualifier(), |
| 1276 | PI->getType()); |
| 1277 | Out << *PI; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1278 | lastPos = pos + 1; |
| 1279 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1280 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1281 | if (OMD->param_begin() == OMD->param_end()) |
| Alex Lorenz | bbf4f70 | 2017-06-02 15:02:59 +0000 | [diff] [blame] | 1282 | Out << name; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1283 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1284 | if (OMD->isVariadic()) |
| 1285 | Out << ", ..."; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1286 | |
| Fariborz Jahanian | aae7fef | 2014-10-03 20:05:33 +0000 | [diff] [blame] | 1287 | prettyPrintAttributes(OMD); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1288 | |
| Fariborz Jahanian | a7d76d2 | 2012-10-17 21:58:03 +0000 | [diff] [blame] | 1289 | if (OMD->getBody() && !Policy.TerseOutput) { |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1290 | Out << ' '; |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1291 | OMD->getBody()->printPretty(Out, nullptr, Policy); |
| Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1292 | } |
| Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 1293 | else if (Policy.PolishForDeclaration) |
| Fariborz Jahanian | 9b7ab87 | 2012-12-18 23:02:59 +0000 | [diff] [blame] | 1294 | Out << ';'; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) { |
| 1298 | std::string I = OID->getNameAsString(); |
| 1299 | ObjCInterfaceDecl *SID = OID->getSuperClass(); |
| 1300 | |
| Fariborz Jahanian | aae7fef | 2014-10-03 20:05:33 +0000 | [diff] [blame] | 1301 | bool eolnOut = false; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1302 | if (SID) |
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1303 | Out << "@implementation " << I << " : " << *SID; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1304 | else |
| 1305 | Out << "@implementation " << I; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1306 | |
| Fariborz Jahanian | 95759ff | 2012-12-04 00:47:33 +0000 | [diff] [blame] | 1307 | if (OID->ivar_size() > 0) { |
| 1308 | Out << "{\n"; |
| Fariborz Jahanian | aae7fef | 2014-10-03 20:05:33 +0000 | [diff] [blame] | 1309 | eolnOut = true; |
| Fariborz Jahanian | 95759ff | 2012-12-04 00:47:33 +0000 | [diff] [blame] | 1310 | Indentation += Policy.Indentation; |
| Aaron Ballman | d6d25de | 2014-03-14 15:16:45 +0000 | [diff] [blame] | 1311 | for (const auto *I : OID->ivars()) { |
| Fariborz Jahanian | b5f3468 | 2013-05-01 20:53:21 +0000 | [diff] [blame] | 1312 | Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()). |
| Aaron Ballman | d6d25de | 2014-03-14 15:16:45 +0000 | [diff] [blame] | 1313 | getAsString(Policy) << ' ' << *I << ";\n"; |
| Fariborz Jahanian | 95759ff | 2012-12-04 00:47:33 +0000 | [diff] [blame] | 1314 | } |
| 1315 | Indentation -= Policy.Indentation; |
| 1316 | Out << "}\n"; |
| 1317 | } |
| Fariborz Jahanian | aae7fef | 2014-10-03 20:05:33 +0000 | [diff] [blame] | 1318 | else if (SID || (OID->decls_begin() != OID->decls_end())) { |
| 1319 | Out << "\n"; |
| 1320 | eolnOut = true; |
| 1321 | } |
| Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1322 | VisitDeclContext(OID, false); |
| Fariborz Jahanian | aae7fef | 2014-10-03 20:05:33 +0000 | [diff] [blame] | 1323 | if (!eolnOut) |
| 1324 | Out << "\n"; |
| Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1325 | Out << "@end"; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) { |
| 1329 | std::string I = OID->getNameAsString(); |
| 1330 | ObjCInterfaceDecl *SID = OID->getSuperClass(); |
| 1331 | |
| Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1332 | if (!OID->isThisDeclarationADefinition()) { |
| Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1333 | Out << "@class " << I; |
| 1334 | |
| 1335 | if (auto TypeParams = OID->getTypeParamListAsWritten()) { |
| 1336 | PrintObjCTypeParams(TypeParams); |
| 1337 | } |
| 1338 | |
| 1339 | Out << ";"; |
| Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1340 | return; |
| 1341 | } |
| Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 1342 | bool eolnOut = false; |
| Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1343 | Out << "@interface " << I; |
| 1344 | |
| 1345 | if (auto TypeParams = OID->getTypeParamListAsWritten()) { |
| 1346 | PrintObjCTypeParams(TypeParams); |
| 1347 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1348 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1349 | if (SID) |
| Bob Wilson | 1f24ea15 | 2015-10-01 00:53:13 +0000 | [diff] [blame] | 1350 | Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1351 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1352 | // Protocols? |
| 1353 | const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols(); |
| 1354 | if (!Protocols.empty()) { |
| 1355 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 1356 | E = Protocols.end(); I != E; ++I) |
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1357 | Out << (I == Protocols.begin() ? '<' : ',') << **I; |
| Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1358 | Out << "> "; |
| Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 1359 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1360 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1361 | if (OID->ivar_size() > 0) { |
| Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1362 | Out << "{\n"; |
| Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 1363 | eolnOut = true; |
| Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1364 | Indentation += Policy.Indentation; |
| Aaron Ballman | 59abbd4 | 2014-03-13 21:09:43 +0000 | [diff] [blame] | 1365 | for (const auto *I : OID->ivars()) { |
| 1366 | Indent() << I->getASTContext() |
| 1367 | .getUnqualifiedObjCPointerType(I->getType()) |
| 1368 | .getAsString(Policy) << ' ' << *I << ";\n"; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1369 | } |
| Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1370 | Indentation -= Policy.Indentation; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1371 | Out << "}\n"; |
| 1372 | } |
| Fariborz Jahanian | aae7fef | 2014-10-03 20:05:33 +0000 | [diff] [blame] | 1373 | else if (SID || (OID->decls_begin() != OID->decls_end())) { |
| Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 1374 | Out << "\n"; |
| 1375 | eolnOut = true; |
| 1376 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1378 | VisitDeclContext(OID, false); |
| Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 1379 | if (!eolnOut) |
| Fariborz Jahanian | aae7fef | 2014-10-03 20:05:33 +0000 | [diff] [blame] | 1380 | Out << "\n"; |
| Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1381 | Out << "@end"; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1382 | // FIXME: implement the rest... |
| 1383 | } |
| 1384 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1385 | void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
| Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1386 | if (!PID->isThisDeclarationADefinition()) { |
| Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 1387 | Out << "@protocol " << *PID << ";\n"; |
| Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1388 | return; |
| 1389 | } |
| Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 1390 | // Protocols? |
| 1391 | const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols(); |
| 1392 | if (!Protocols.empty()) { |
| 1393 | Out << "@protocol " << *PID; |
| 1394 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 1395 | E = Protocols.end(); I != E; ++I) |
| 1396 | Out << (I == Protocols.begin() ? '<' : ',') << **I; |
| 1397 | Out << ">\n"; |
| 1398 | } else |
| 1399 | Out << "@protocol " << *PID << '\n'; |
| Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1400 | VisitDeclContext(PID, false); |
| 1401 | Out << "@end"; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { |
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1405 | Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n"; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1406 | |
| 1407 | VisitDeclContext(PID, false); |
| Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1408 | Out << "@end"; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1409 | // FIXME: implement the rest... |
| 1410 | } |
| 1411 | |
| 1412 | void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) { |
| Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1413 | Out << "@interface " << *PID->getClassInterface(); |
| 1414 | if (auto TypeParams = PID->getTypeParamList()) { |
| 1415 | PrintObjCTypeParams(TypeParams); |
| 1416 | } |
| 1417 | Out << "(" << *PID << ")\n"; |
| Fariborz Jahanian | 4cf177e | 2012-12-04 17:20:57 +0000 | [diff] [blame] | 1418 | if (PID->ivar_size() > 0) { |
| 1419 | Out << "{\n"; |
| 1420 | Indentation += Policy.Indentation; |
| Aaron Ballman | 865fbcd | 2014-03-14 13:13:27 +0000 | [diff] [blame] | 1421 | for (const auto *I : PID->ivars()) |
| Fariborz Jahanian | b5f3468 | 2013-05-01 20:53:21 +0000 | [diff] [blame] | 1422 | Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()). |
| Aaron Ballman | 865fbcd | 2014-03-14 13:13:27 +0000 | [diff] [blame] | 1423 | getAsString(Policy) << ' ' << *I << ";\n"; |
| Fariborz Jahanian | 4cf177e | 2012-12-04 17:20:57 +0000 | [diff] [blame] | 1424 | Indentation -= Policy.Indentation; |
| 1425 | Out << "}\n"; |
| 1426 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1427 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1428 | VisitDeclContext(PID, false); |
| Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1429 | Out << "@end"; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1430 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1431 | // FIXME: implement the rest... |
| 1432 | } |
| 1433 | |
| 1434 | void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { |
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1435 | Out << "@compatibility_alias " << *AID |
| 1436 | << ' ' << *AID->getClassInterface() << ";\n"; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | /// PrintObjCPropertyDecl - print a property declaration. |
| 1440 | /// |
| David Goldman | fa8185c | 2019-04-08 19:52:45 +0000 | [diff] [blame] | 1441 | /// Print attributes in the following order: |
| 1442 | /// - class |
| 1443 | /// - nonatomic | atomic |
| 1444 | /// - assign | retain | strong | copy | weak | unsafe_unretained |
| 1445 | /// - readwrite | readonly |
| 1446 | /// - getter & setter |
| 1447 | /// - nullability |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1448 | void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) { |
| 1449 | if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required) |
| 1450 | Out << "@required\n"; |
| 1451 | else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) |
| 1452 | Out << "@optional\n"; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1453 | |
| Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1454 | QualType T = PDecl->getType(); |
| 1455 | |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1456 | Out << "@property"; |
| 1457 | if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) { |
| 1458 | bool first = true; |
| David Goldman | fa8185c | 2019-04-08 19:52:45 +0000 | [diff] [blame] | 1459 | Out << "("; |
| 1460 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) { |
| 1461 | Out << (first ? "" : ", ") << "class"; |
| Ted Kremenek | 897af91 | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 1462 | first = false; |
| 1463 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1464 | |
| Ted Kremenek | 897af91 | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 1465 | if (PDecl->getPropertyAttributes() & |
| 1466 | ObjCPropertyDecl::OBJC_PR_nonatomic) { |
| David Goldman | fa8185c | 2019-04-08 19:52:45 +0000 | [diff] [blame] | 1467 | Out << (first ? "" : ", ") << "nonatomic"; |
| Ted Kremenek | 897af91 | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 1468 | first = false; |
| 1469 | } |
| 1470 | if (PDecl->getPropertyAttributes() & |
| 1471 | ObjCPropertyDecl::OBJC_PR_atomic) { |
| David Goldman | fa8185c | 2019-04-08 19:52:45 +0000 | [diff] [blame] | 1472 | Out << (first ? "" : ", ") << "atomic"; |
| 1473 | first = false; |
| 1474 | } |
| 1475 | |
| 1476 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) { |
| 1477 | Out << (first ? "" : ", ") << "assign"; |
| 1478 | first = false; |
| 1479 | } |
| 1480 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) { |
| 1481 | Out << (first ? "" : ", ") << "retain"; |
| 1482 | first = false; |
| 1483 | } |
| 1484 | |
| 1485 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) { |
| 1486 | Out << (first ? "" : ", ") << "strong"; |
| 1487 | first = false; |
| 1488 | } |
| 1489 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) { |
| 1490 | Out << (first ? "" : ", ") << "copy"; |
| 1491 | first = false; |
| 1492 | } |
| 1493 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak) { |
| 1494 | Out << (first ? "" : ", ") << "weak"; |
| 1495 | first = false; |
| 1496 | } |
| 1497 | if (PDecl->getPropertyAttributes() |
| 1498 | & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) { |
| 1499 | Out << (first ? "" : ", ") << "unsafe_unretained"; |
| 1500 | first = false; |
| 1501 | } |
| 1502 | |
| 1503 | if (PDecl->getPropertyAttributes() & |
| 1504 | ObjCPropertyDecl::OBJC_PR_readwrite) { |
| 1505 | Out << (first ? "" : ", ") << "readwrite"; |
| 1506 | first = false; |
| 1507 | } |
| 1508 | if (PDecl->getPropertyAttributes() & |
| 1509 | ObjCPropertyDecl::OBJC_PR_readonly) { |
| 1510 | Out << (first ? "" : ", ") << "readonly"; |
| 1511 | first = false; |
| 1512 | } |
| 1513 | |
| 1514 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 1515 | Out << (first ? "" : ", ") << "getter = "; |
| 1516 | PDecl->getGetterName().print(Out); |
| 1517 | first = false; |
| 1518 | } |
| 1519 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 1520 | Out << (first ? "" : ", ") << "setter = "; |
| 1521 | PDecl->getSetterName().print(Out); |
| Ted Kremenek | 897af91 | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 1522 | first = false; |
| 1523 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1524 | |
| Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1525 | if (PDecl->getPropertyAttributes() & |
| 1526 | ObjCPropertyDecl::OBJC_PR_nullability) { |
| Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 1527 | if (auto nullability = AttributedType::stripOuterNullability(T)) { |
| Douglas Gregor | 849ebc2 | 2015-06-19 18:14:46 +0000 | [diff] [blame] | 1528 | if (*nullability == NullabilityKind::Unspecified && |
| 1529 | (PDecl->getPropertyAttributes() & |
| 1530 | ObjCPropertyDecl::OBJC_PR_null_resettable)) { |
| David Goldman | fa8185c | 2019-04-08 19:52:45 +0000 | [diff] [blame] | 1531 | Out << (first ? "" : ", ") << "null_resettable"; |
| Douglas Gregor | 849ebc2 | 2015-06-19 18:14:46 +0000 | [diff] [blame] | 1532 | } else { |
| David Goldman | fa8185c | 2019-04-08 19:52:45 +0000 | [diff] [blame] | 1533 | Out << (first ? "" : ", ") |
| Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 1534 | << getNullabilitySpelling(*nullability, true); |
| Douglas Gregor | 849ebc2 | 2015-06-19 18:14:46 +0000 | [diff] [blame] | 1535 | } |
| Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1536 | first = false; |
| 1537 | } |
| 1538 | } |
| 1539 | |
| Ted Kremenek | 897af91 | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 1540 | (void) first; // Silence dead store warning due to idiomatic code. |
| David Goldman | fa8185c | 2019-04-08 19:52:45 +0000 | [diff] [blame] | 1541 | Out << ")"; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1542 | } |
| David Goldman | fa8185c | 2019-04-08 19:52:45 +0000 | [diff] [blame] | 1543 | std::string TypeStr = PDecl->getASTContext().getUnqualifiedObjCPointerType(T). |
| 1544 | getAsString(Policy); |
| 1545 | Out << ' ' << TypeStr; |
| 1546 | if (!StringRef(TypeStr).endswith("*")) |
| 1547 | Out << ' '; |
| 1548 | Out << *PDecl; |
| Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 1549 | if (Policy.PolishForDeclaration) |
| 1550 | Out << ';'; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { |
| 1554 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) |
| Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1555 | Out << "@synthesize "; |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1556 | else |
| Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1557 | Out << "@dynamic "; |
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1558 | Out << *PID->getPropertyDecl(); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1559 | if (PID->getPropertyIvarDecl()) |
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1560 | Out << '=' << *PID->getPropertyIvarDecl(); |
| Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1561 | } |
| Anders Carlsson | df5a1c8 | 2009-08-28 19:16:39 +0000 | [diff] [blame] | 1562 | |
| 1563 | void DeclPrinter::VisitUsingDecl(UsingDecl *D) { |
| Enea Zaffanella | c70b251 | 2013-07-17 17:28:56 +0000 | [diff] [blame] | 1564 | if (!D->isAccessDeclaration()) |
| 1565 | Out << "using "; |
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 1566 | if (D->hasTypename()) |
| Enea Zaffanella | c70b251 | 2013-07-17 17:28:56 +0000 | [diff] [blame] | 1567 | Out << "typename "; |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1568 | D->getQualifier()->print(Out, Policy); |
| Alex Lorenz | ea9b59a | 2016-10-03 12:22:17 +0000 | [diff] [blame] | 1569 | |
| 1570 | // Use the correct record name when the using declaration is used for |
| 1571 | // inheriting constructors. |
| 1572 | for (const auto *Shadow : D->shadows()) { |
| 1573 | if (const auto *ConstructorShadow = |
| 1574 | dyn_cast<ConstructorUsingShadowDecl>(Shadow)) { |
| 1575 | assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext()); |
| 1576 | Out << *ConstructorShadow->getNominatedBaseClass(); |
| 1577 | return; |
| 1578 | } |
| 1579 | } |
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1580 | Out << *D; |
| Anders Carlsson | df5a1c8 | 2009-08-28 19:16:39 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1583 | void |
| 1584 | DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) { |
| 1585 | Out << "using typename "; |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1586 | D->getQualifier()->print(Out, Policy); |
| Benjamin Kramer | b11416d | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 1587 | Out << D->getDeclName(); |
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
| Enea Zaffanella | c70b251 | 2013-07-17 17:28:56 +0000 | [diff] [blame] | 1591 | if (!D->isAccessDeclaration()) |
| 1592 | Out << "using "; |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1593 | D->getQualifier()->print(Out, Policy); |
| Benjamin Kramer | 36d514e | 2015-09-23 13:43:16 +0000 | [diff] [blame] | 1594 | Out << D->getDeclName(); |
| Anders Carlsson | df5a1c8 | 2009-08-28 19:16:39 +0000 | [diff] [blame] | 1595 | } |
| John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 1596 | |
| 1597 | void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) { |
| 1598 | // ignore |
| 1599 | } |
| Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1600 | |
| 1601 | void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) { |
| 1602 | Out << "#pragma omp threadprivate"; |
| 1603 | if (!D->varlist_empty()) { |
| 1604 | for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(), |
| 1605 | E = D->varlist_end(); |
| Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1606 | I != E; ++I) { |
| Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 1607 | Out << (I == D->varlist_begin() ? '(' : ','); |
| George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 1608 | NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl(); |
| Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 1609 | ND->printQualifiedName(Out); |
| Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1610 | } |
| 1611 | Out << ")"; |
| 1612 | } |
| 1613 | } |
| 1614 | |
| Alexey Bataev | 25ed0c0 | 2019-03-07 17:54:44 +0000 | [diff] [blame] | 1615 | void DeclPrinter::VisitOMPAllocateDecl(OMPAllocateDecl *D) { |
| 1616 | Out << "#pragma omp allocate"; |
| 1617 | if (!D->varlist_empty()) { |
| 1618 | for (OMPAllocateDecl::varlist_iterator I = D->varlist_begin(), |
| 1619 | E = D->varlist_end(); |
| 1620 | I != E; ++I) { |
| 1621 | Out << (I == D->varlist_begin() ? '(' : ','); |
| 1622 | NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl(); |
| 1623 | ND->printQualifiedName(Out); |
| 1624 | } |
| 1625 | Out << ")"; |
| 1626 | } |
| Alexey Bataev | 9cc10fc | 2019-03-12 18:52:33 +0000 | [diff] [blame] | 1627 | if (!D->clauselist_empty()) { |
| 1628 | Out << " "; |
| 1629 | OMPClausePrinter Printer(Out, Policy); |
| 1630 | for (OMPClause *C : D->clauselists()) |
| 1631 | Printer.Visit(C); |
| 1632 | } |
| Alexey Bataev | 25ed0c0 | 2019-03-07 17:54:44 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
| Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 1635 | void DeclPrinter::VisitOMPRequiresDecl(OMPRequiresDecl *D) { |
| 1636 | Out << "#pragma omp requires "; |
| 1637 | if (!D->clauselist_empty()) { |
| Patrick Lyster | 7a2a27c | 2018-11-02 12:18:11 +0000 | [diff] [blame] | 1638 | OMPClausePrinter Printer(Out, Policy); |
| 1639 | for (auto I = D->clauselist_begin(), E = D->clauselist_end(); I != E; ++I) |
| 1640 | Printer.Visit(*I); |
| Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 1641 | } |
| 1642 | } |
| 1643 | |
| Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 1644 | void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) { |
| 1645 | if (!D->isInvalidDecl()) { |
| 1646 | Out << "#pragma omp declare reduction ("; |
| 1647 | if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) { |
| Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 1648 | const char *OpName = |
| Richard Smith | 7fa2b74 | 2019-06-14 20:01:51 +0000 | [diff] [blame] | 1649 | getOperatorSpelling(D->getDeclName().getCXXOverloadedOperator()); |
| Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 1650 | assert(OpName && "not an overloaded operator"); |
| 1651 | Out << OpName; |
| 1652 | } else { |
| 1653 | assert(D->getDeclName().isIdentifier()); |
| 1654 | D->printName(Out); |
| 1655 | } |
| 1656 | Out << " : "; |
| 1657 | D->getType().print(Out, Policy); |
| 1658 | Out << " : "; |
| 1659 | D->getCombiner()->printPretty(Out, nullptr, Policy, 0); |
| 1660 | Out << ")"; |
| 1661 | if (auto *Init = D->getInitializer()) { |
| 1662 | Out << " initializer("; |
| Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 1663 | switch (D->getInitializerKind()) { |
| 1664 | case OMPDeclareReductionDecl::DirectInit: |
| 1665 | Out << "omp_priv("; |
| 1666 | break; |
| 1667 | case OMPDeclareReductionDecl::CopyInit: |
| 1668 | Out << "omp_priv = "; |
| 1669 | break; |
| 1670 | case OMPDeclareReductionDecl::CallInit: |
| 1671 | break; |
| 1672 | } |
| Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 1673 | Init->printPretty(Out, nullptr, Policy, 0); |
| Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 1674 | if (D->getInitializerKind() == OMPDeclareReductionDecl::DirectInit) |
| 1675 | Out << ")"; |
| Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 1676 | Out << ")"; |
| 1677 | } |
| 1678 | } |
| 1679 | } |
| 1680 | |
| Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 1681 | void DeclPrinter::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) { |
| 1682 | if (!D->isInvalidDecl()) { |
| 1683 | Out << "#pragma omp declare mapper ("; |
| 1684 | D->printName(Out); |
| 1685 | Out << " : "; |
| 1686 | D->getType().print(Out, Policy); |
| 1687 | Out << " "; |
| 1688 | Out << D->getVarName(); |
| 1689 | Out << ")"; |
| 1690 | if (!D->clauselist_empty()) { |
| 1691 | OMPClausePrinter Printer(Out, Policy); |
| 1692 | for (auto *C : D->clauselists()) { |
| 1693 | Out << " "; |
| 1694 | Printer.Visit(C); |
| 1695 | } |
| 1696 | } |
| 1697 | } |
| 1698 | } |
| 1699 | |
| Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 1700 | void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) { |
| Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 1701 | D->getInit()->printPretty(Out, nullptr, Policy, Indentation); |
| 1702 | } |
| 1703 | |