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); |
Sam McCall | 87054ec | 2019-11-14 14:16:14 +0100 | [diff] [blame] | 111 | void printTemplateArguments(llvm::ArrayRef<TemplateArgument> Args); |
| 112 | void printTemplateArguments(llvm::ArrayRef<TemplateArgumentLoc> Args); |
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 { |
Sam McCall | 575e09d | 2019-11-15 19:19:17 +0100 | [diff] [blame] | 628 | llvm::raw_string_ostream OS(Proto); |
Serge Pavlov | 842022a | 2017-11-23 05:38:20 +0000 | [diff] [blame] | 629 | if (!Policy.SuppressScope) { |
| 630 | if (const NestedNameSpecifier *NS = D->getQualifier()) { |
Serge Pavlov | 842022a | 2017-11-23 05:38:20 +0000 | [diff] [blame] | 631 | NS->print(OS, Policy); |
| 632 | } |
Alex Lorenz | a981c7d | 2017-04-11 16:46:03 +0000 | [diff] [blame] | 633 | } |
Sam McCall | 575e09d | 2019-11-15 19:19:17 +0100 | [diff] [blame] | 634 | D->getNameInfo().printName(OS, Policy); |
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(); |
Sam McCall | 87054ec | 2019-11-14 14:16:14 +0100 | [diff] [blame] | 639 | if (D->isFunctionTemplateSpecialization()) { |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 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); |
Sam McCall | 87054ec | 2019-11-14 14:16:14 +0100 | [diff] [blame] | 642 | const auto *TArgAsWritten = D->getTemplateSpecializationArgsAsWritten(); |
| 643 | if (TArgAsWritten && !Policy.PrintCanonicalTypes) |
| 644 | TArgPrinter.printTemplateArguments(TArgAsWritten->arguments()); |
| 645 | else if (const TemplateArgumentList *TArgs = |
| 646 | D->getTemplateSpecializationArgs()) |
| 647 | TArgPrinter.printTemplateArguments(TArgs->asArray()); |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 648 | } |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 649 | |
Abramo Bagnara | 6d81063 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 650 | QualType Ty = D->getType(); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 651 | while (const ParenType *PT = dyn_cast<ParenType>(Ty)) { |
Abramo Bagnara | 6d81063 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 652 | Proto = '(' + Proto + ')'; |
| 653 | Ty = PT->getInnerType(); |
| 654 | } |
| 655 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 656 | if (const FunctionType *AFT = Ty->getAs<FunctionType>()) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 657 | const FunctionProtoType *FT = nullptr; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 658 | if (D->hasWrittenPrototype()) |
| 659 | FT = dyn_cast<FunctionProtoType>(AFT); |
| 660 | |
| 661 | Proto += "("; |
| 662 | if (FT) { |
| 663 | llvm::raw_string_ostream POut(Proto); |
Alex Lorenz | 36070ed | 2017-08-17 13:41:55 +0000 | [diff] [blame] | 664 | DeclPrinter ParamPrinter(POut, SubPolicy, Context, Indentation); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 665 | for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { |
| 666 | if (i) POut << ", "; |
| 667 | ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); |
| 668 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 669 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 670 | if (FT->isVariadic()) { |
| 671 | if (D->getNumParams()) POut << ", "; |
| 672 | POut << "..."; |
| 673 | } |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 674 | } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) { |
Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 675 | for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { |
| 676 | if (i) |
| 677 | Proto += ", "; |
| 678 | Proto += D->getParamDecl(i)->getNameAsString(); |
| 679 | } |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | Proto += ")"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 683 | |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 684 | if (FT) { |
| 685 | if (FT->isConst()) |
Douglas Gregor | 8fc96fc | 2010-11-19 18:44:34 +0000 | [diff] [blame] | 686 | Proto += " const"; |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 687 | if (FT->isVolatile()) |
Douglas Gregor | 8fc96fc | 2010-11-19 18:44:34 +0000 | [diff] [blame] | 688 | Proto += " volatile"; |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 689 | if (FT->isRestrict()) |
Douglas Gregor | 8fc96fc | 2010-11-19 18:44:34 +0000 | [diff] [blame] | 690 | Proto += " restrict"; |
Benjamin Kramer | 2907b08 | 2014-02-25 18:49:49 +0000 | [diff] [blame] | 691 | |
| 692 | switch (FT->getRefQualifier()) { |
| 693 | case RQ_None: |
| 694 | break; |
| 695 | case RQ_LValue: |
| 696 | Proto += " &"; |
| 697 | break; |
| 698 | case RQ_RValue: |
| 699 | Proto += " &&"; |
| 700 | break; |
| 701 | } |
Douglas Gregor | 8fc96fc | 2010-11-19 18:44:34 +0000 | [diff] [blame] | 702 | } |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 703 | |
| 704 | if (FT && FT->hasDynamicExceptionSpec()) { |
Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 705 | Proto += " throw("; |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 706 | if (FT->getExceptionSpecType() == EST_MSAny) |
Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 707 | Proto += "..."; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 708 | else |
Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 709 | for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) { |
| 710 | if (I) |
| 711 | Proto += ", "; |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 712 | |
Dmitri Gribenko | 76bb5cabfa | 2012-09-10 21:20:09 +0000 | [diff] [blame] | 713 | Proto += FT->getExceptionType(I).getAsString(SubPolicy); |
Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 714 | } |
| 715 | Proto += ")"; |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 716 | } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) { |
| 717 | Proto += " noexcept"; |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 718 | if (isComputedNoexcept(FT->getExceptionSpecType())) { |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 719 | Proto += "("; |
| 720 | llvm::raw_string_ostream EOut(Proto); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 721 | FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy, |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 722 | Indentation); |
| 723 | EOut.flush(); |
| 724 | Proto += EOut.str(); |
| 725 | Proto += ")"; |
| 726 | } |
Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Fariborz Jahanian | 69c403c | 2012-12-05 22:19:06 +0000 | [diff] [blame] | 729 | if (CDecl) { |
Alex Lorenz | dc616fa | 2017-11-16 01:31:27 +0000 | [diff] [blame] | 730 | if (!Policy.TerseOutput) |
| 731 | PrintConstructorInitializers(CDecl, Proto); |
Benjamin Kramer | 2907b08 | 2014-02-25 18:49:49 +0000 | [diff] [blame] | 732 | } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) { |
Richard Smith | e656076 | 2013-02-22 05:54:51 +0000 | [diff] [blame] | 733 | if (FT && FT->hasTrailingReturn()) { |
Richard Smith | 057ec50 | 2017-02-18 01:01:48 +0000 | [diff] [blame] | 734 | if (!GuideDecl) |
| 735 | Out << "auto "; |
| 736 | Out << Proto << " -> "; |
Richard Smith | e656076 | 2013-02-22 05:54:51 +0000 | [diff] [blame] | 737 | Proto.clear(); |
| 738 | } |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 739 | AFT->getReturnType().print(Out, Policy, Proto); |
Benjamin Kramer | 00e8a19 | 2014-02-25 18:03:55 +0000 | [diff] [blame] | 740 | Proto.clear(); |
Richard Smith | e656076 | 2013-02-22 05:54:51 +0000 | [diff] [blame] | 741 | } |
Benjamin Kramer | 00e8a19 | 2014-02-25 18:03:55 +0000 | [diff] [blame] | 742 | Out << Proto; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 743 | } else { |
Argyrios Kyrtzidis | a18347e | 2012-05-05 04:20:37 +0000 | [diff] [blame] | 744 | Ty.print(Out, Policy, Proto); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 747 | prettyPrintAttributes(D); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 748 | |
| 749 | if (D->isPure()) |
| 750 | Out << " = 0"; |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 751 | else if (D->isDeletedAsWritten()) |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 752 | Out << " = delete"; |
Fariborz Jahanian | de872af | 2012-12-05 22:53:06 +0000 | [diff] [blame] | 753 | else if (D->isExplicitlyDefaulted()) |
| 754 | Out << " = default"; |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 755 | else if (D->doesThisDeclarationHaveABody()) { |
| 756 | if (!Policy.TerseOutput) { |
| 757 | if (!D->hasPrototype() && D->getNumParams()) { |
| 758 | // This is a K&R function definition, so we need to print the |
| 759 | // parameters. |
| 760 | Out << '\n'; |
Alex Lorenz | 36070ed | 2017-08-17 13:41:55 +0000 | [diff] [blame] | 761 | DeclPrinter ParamPrinter(Out, SubPolicy, Context, Indentation); |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 762 | Indentation += Policy.Indentation; |
| 763 | for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { |
| 764 | Indent(); |
| 765 | ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); |
| 766 | Out << ";\n"; |
| 767 | } |
| 768 | Indentation -= Policy.Indentation; |
| 769 | } else |
| 770 | Out << ' '; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 771 | |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 772 | if (D->getBody()) |
| 773 | D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation); |
| 774 | } else { |
Alex Lorenz | 35019db | 2017-11-16 01:28:25 +0000 | [diff] [blame] | 775 | if (!Policy.TerseOutput && isa<CXXConstructorDecl>(*D)) |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 776 | Out << " {}"; |
| 777 | } |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | |
Fariborz Jahanian | bfc3ef5 | 2012-12-05 00:38:44 +0000 | [diff] [blame] | 781 | void DeclPrinter::VisitFriendDecl(FriendDecl *D) { |
| 782 | if (TypeSourceInfo *TSI = D->getFriendType()) { |
Enea Zaffanella | eb22c87 | 2013-01-31 09:54:08 +0000 | [diff] [blame] | 783 | unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists(); |
| 784 | for (unsigned i = 0; i < NumTPLists; ++i) |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 785 | printTemplateParameters(D->getFriendTypeTemplateParameterList(i)); |
Fariborz Jahanian | 7a16a02 | 2012-12-21 21:43:05 +0000 | [diff] [blame] | 786 | Out << "friend "; |
| 787 | Out << " " << TSI->getType().getAsString(Policy); |
Fariborz Jahanian | bfc3ef5 | 2012-12-05 00:38:44 +0000 | [diff] [blame] | 788 | } |
| 789 | else if (FunctionDecl *FD = |
| 790 | dyn_cast<FunctionDecl>(D->getFriendDecl())) { |
| 791 | Out << "friend "; |
| 792 | VisitFunctionDecl(FD); |
| 793 | } |
| 794 | else if (FunctionTemplateDecl *FTD = |
| 795 | dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) { |
| 796 | Out << "friend "; |
| 797 | VisitFunctionTemplateDecl(FTD); |
| 798 | } |
| 799 | else if (ClassTemplateDecl *CTD = |
| 800 | dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) { |
| 801 | Out << "friend "; |
Fariborz Jahanian | 7a16a02 | 2012-12-21 21:43:05 +0000 | [diff] [blame] | 802 | VisitRedeclarableTemplateDecl(CTD); |
Fariborz Jahanian | bfc3ef5 | 2012-12-05 00:38:44 +0000 | [diff] [blame] | 803 | } |
| 804 | } |
| 805 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 806 | void DeclPrinter::VisitFieldDecl(FieldDecl *D) { |
Alexey Bataev | 6d45532 | 2015-10-12 06:59:48 +0000 | [diff] [blame] | 807 | // FIXME: add printing of pragma attributes if required. |
Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 808 | if (!Policy.SuppressSpecifiers && D->isMutable()) |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 809 | Out << "mutable "; |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 810 | if (!Policy.SuppressSpecifiers && D->isModulePrivate()) |
| 811 | Out << "__module_private__ "; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 812 | |
Fariborz Jahanian | b5f3468 | 2013-05-01 20:53:21 +0000 | [diff] [blame] | 813 | Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()). |
Steven Watanabe | 9359b8f | 2016-03-18 21:35:59 +0000 | [diff] [blame] | 814 | stream(Policy, D->getName(), Indentation); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 815 | |
| 816 | if (D->isBitField()) { |
| 817 | Out << " : "; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 818 | D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 819 | } |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 820 | |
| 821 | Expr *Init = D->getInClassInitializer(); |
| 822 | if (!Policy.SuppressInitializers && Init) { |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 823 | if (D->getInClassInitStyle() == ICIS_ListInit) |
| 824 | Out << " "; |
| 825 | else |
| 826 | Out << " = "; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 827 | Init->printPretty(Out, nullptr, Policy, Indentation); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 828 | } |
Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 829 | prettyPrintAttributes(D); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 832 | void DeclPrinter::VisitLabelDecl(LabelDecl *D) { |
Benjamin Kramer | db0fc51 | 2012-02-07 11:57:57 +0000 | [diff] [blame] | 833 | Out << *D << ":"; |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 836 | void DeclPrinter::VisitVarDecl(VarDecl *D) { |
Alexey Bataev | 6d45532 | 2015-10-12 06:59:48 +0000 | [diff] [blame] | 837 | prettyPrintPragmas(D); |
Vassil Vassilev | 1002373 | 2016-07-08 21:09:08 +0000 | [diff] [blame] | 838 | |
| 839 | QualType T = D->getTypeSourceInfo() |
| 840 | ? D->getTypeSourceInfo()->getType() |
| 841 | : D->getASTContext().getUnqualifiedObjCPointerType(D->getType()); |
| 842 | |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 843 | if (!Policy.SuppressSpecifiers) { |
| 844 | StorageClass SC = D->getStorageClass(); |
| 845 | if (SC != SC_None) |
| 846 | Out << VarDecl::getStorageClassSpecifierString(SC) << " "; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 847 | |
Enea Zaffanella | acb8ecd | 2013-05-04 08:27:07 +0000 | [diff] [blame] | 848 | switch (D->getTSCSpec()) { |
| 849 | case TSCS_unspecified: |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 850 | break; |
Enea Zaffanella | acb8ecd | 2013-05-04 08:27:07 +0000 | [diff] [blame] | 851 | case TSCS___thread: |
| 852 | Out << "__thread "; |
| 853 | break; |
| 854 | case TSCS__Thread_local: |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 855 | Out << "_Thread_local "; |
| 856 | break; |
Enea Zaffanella | acb8ecd | 2013-05-04 08:27:07 +0000 | [diff] [blame] | 857 | case TSCS_thread_local: |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 858 | Out << "thread_local "; |
| 859 | break; |
| 860 | } |
| 861 | |
| 862 | if (D->isModulePrivate()) |
| 863 | Out << "__module_private__ "; |
Vassil Vassilev | 1002373 | 2016-07-08 21:09:08 +0000 | [diff] [blame] | 864 | |
| 865 | if (D->isConstexpr()) { |
| 866 | Out << "constexpr "; |
| 867 | T.removeLocalConst(); |
| 868 | } |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 869 | } |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 870 | |
Richard Smith | a4bb292 | 2014-07-23 03:17:06 +0000 | [diff] [blame] | 871 | printDeclType(T, D->getName()); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 872 | Expr *Init = D->getInit(); |
| 873 | if (!Policy.SuppressInitializers && Init) { |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 874 | bool ImplicitInit = false; |
Dmitri Gribenko | b614fab | 2013-02-03 23:02:47 +0000 | [diff] [blame] | 875 | if (CXXConstructExpr *Construct = |
| 876 | dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) { |
Dmitri Gribenko | 6835e37 | 2013-01-27 21:28:24 +0000 | [diff] [blame] | 877 | if (D->getInitStyle() == VarDecl::CallInit && |
| 878 | !Construct->isListInitialization()) { |
| 879 | ImplicitInit = Construct->getNumArgs() == 0 || |
| 880 | Construct->getArg(0)->isDefaultArgument(); |
| 881 | } |
| 882 | } |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 883 | if (!ImplicitInit) { |
Eli Friedman | 92125c4 | 2012-10-19 20:36:44 +0000 | [diff] [blame] | 884 | if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init)) |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 885 | Out << "("; |
| 886 | else if (D->getInitStyle() == VarDecl::CInit) { |
| 887 | Out << " = "; |
| 888 | } |
Benjamin Kramer | 54f81ed | 2016-01-25 10:34:06 +0000 | [diff] [blame] | 889 | PrintingPolicy SubPolicy(Policy); |
| 890 | SubPolicy.SuppressSpecifiers = false; |
Steven Watanabe | 9359b8f | 2016-03-18 21:35:59 +0000 | [diff] [blame] | 891 | SubPolicy.IncludeTagDefinition = false; |
Benjamin Kramer | 54f81ed | 2016-01-25 10:34:06 +0000 | [diff] [blame] | 892 | Init->printPretty(Out, nullptr, SubPolicy, Indentation); |
Eli Friedman | 92125c4 | 2012-10-19 20:36:44 +0000 | [diff] [blame] | 893 | if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init)) |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 894 | Out << ")"; |
Ted Kremenek | 3586938 | 2010-09-17 23:04:38 +0000 | [diff] [blame] | 895 | } |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 896 | } |
Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 897 | prettyPrintAttributes(D); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) { |
| 901 | VisitVarDecl(D); |
| 902 | } |
| 903 | |
| 904 | void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { |
| 905 | Out << "__asm ("; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 906 | D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 907 | Out << ")"; |
| 908 | } |
| 909 | |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 910 | void DeclPrinter::VisitImportDecl(ImportDecl *D) { |
Douglas Gregor | c50d492 | 2012-12-11 22:11:52 +0000 | [diff] [blame] | 911 | Out << "@import " << D->getImportedModule()->getFullModuleName() |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 912 | << ";\n"; |
| 913 | } |
| 914 | |
Peter Collingbourne | 7d8a0b5 | 2011-03-16 18:37:27 +0000 | [diff] [blame] | 915 | void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 916 | Out << "static_assert("; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 917 | D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation); |
David Majnemer | cdffc36 | 2015-06-05 18:03:58 +0000 | [diff] [blame] | 918 | if (StringLiteral *SL = D->getMessage()) { |
| 919 | Out << ", "; |
| 920 | SL->printPretty(Out, nullptr, Policy, Indentation); |
| 921 | } |
Peter Collingbourne | 7d8a0b5 | 2011-03-16 18:37:27 +0000 | [diff] [blame] | 922 | Out << ")"; |
| 923 | } |
| 924 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 925 | //---------------------------------------------------------------------------- |
| 926 | // C++ declarations |
| 927 | //---------------------------------------------------------------------------- |
Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 928 | void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) { |
Douglas Gregor | b526370 | 2012-05-01 01:43:38 +0000 | [diff] [blame] | 929 | if (D->isInline()) |
| 930 | Out << "inline "; |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 931 | Out << "namespace " << *D << " {\n"; |
Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 932 | VisitDeclContext(D); |
| 933 | Indent() << "}"; |
| 934 | } |
| 935 | |
Douglas Gregor | 3bc6e4c | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 936 | void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
| 937 | Out << "using namespace "; |
| 938 | if (D->getQualifier()) |
| 939 | D->getQualifier()->print(Out, Policy); |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 940 | Out << *D->getNominatedNamespaceAsWritten(); |
Douglas Gregor | 3bc6e4c | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Douglas Gregor | 1823193 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 943 | void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 944 | Out << "namespace " << *D << " = "; |
Douglas Gregor | 1823193 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 945 | if (D->getQualifier()) |
| 946 | D->getQualifier()->print(Out, Policy); |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 947 | Out << *D->getAliasedNamespace(); |
Douglas Gregor | 1823193 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 950 | void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) { |
| 951 | prettyPrintAttributes(D); |
Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 952 | } |
| 953 | |
Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 954 | void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) { |
Alexey Bataev | 6d45532 | 2015-10-12 06:59:48 +0000 | [diff] [blame] | 955 | // FIXME: add printing of pragma attributes if required. |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 956 | if (!Policy.SuppressSpecifiers && D->isModulePrivate()) |
| 957 | Out << "__module_private__ "; |
Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 958 | Out << D->getKindName(); |
Aaron Ballman | 5388538 | 2014-09-15 16:45:30 +0000 | [diff] [blame] | 959 | |
| 960 | prettyPrintAttributes(D); |
| 961 | |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 962 | if (D->getIdentifier()) { |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 963 | Out << ' ' << *D; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 964 | |
Sam McCall | 87054ec | 2019-11-14 14:16:14 +0100 | [diff] [blame] | 965 | if (auto S = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
| 966 | ArrayRef<TemplateArgument> Args = S->getTemplateArgs().asArray(); |
| 967 | if (!Policy.PrintCanonicalTypes) |
| 968 | if (const auto* TSI = S->getTypeAsWritten()) |
| 969 | if (const auto *TST = |
| 970 | dyn_cast<TemplateSpecializationType>(TSI->getType())) |
| 971 | Args = TST->template_arguments(); |
| 972 | printTemplateArguments(Args); |
| 973 | } |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 974 | } |
| 975 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 976 | if (D->isCompleteDefinition()) { |
Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 977 | // Print the base classes |
| 978 | if (D->getNumBases()) { |
| 979 | Out << " : "; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 980 | for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(), |
| 981 | BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) { |
Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 982 | if (Base != D->bases_begin()) |
| 983 | Out << ", "; |
| 984 | |
| 985 | if (Base->isVirtual()) |
| 986 | Out << "virtual "; |
| 987 | |
Anders Carlsson | 6df9e07 | 2009-08-29 20:36:12 +0000 | [diff] [blame] | 988 | AccessSpecifier AS = Base->getAccessSpecifierAsWritten(); |
Richard Smith | a593419 | 2014-07-23 03:22:10 +0000 | [diff] [blame] | 989 | if (AS != AS_none) { |
Anders Carlsson | 6df9e07 | 2009-08-29 20:36:12 +0000 | [diff] [blame] | 990 | Print(AS); |
Richard Smith | a593419 | 2014-07-23 03:22:10 +0000 | [diff] [blame] | 991 | Out << " "; |
| 992 | } |
| 993 | Out << Base->getType().getAsString(Policy); |
Douglas Gregor | 5fc8c9e | 2011-04-27 17:07:55 +0000 | [diff] [blame] | 994 | |
| 995 | if (Base->isPackExpansion()) |
| 996 | Out << "..."; |
Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 997 | } |
| 998 | } |
| 999 | |
| 1000 | // Print the class definition |
Douglas Gregor | 7a1a7cb | 2009-05-31 07:13:39 +0000 | [diff] [blame] | 1001 | // FIXME: Doesn't print access specifiers, e.g., "public:" |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1002 | if (Policy.TerseOutput) { |
| 1003 | Out << " {}"; |
| 1004 | } else { |
| 1005 | Out << " {\n"; |
| 1006 | VisitDeclContext(D); |
| 1007 | Indent() << "}"; |
| 1008 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1009 | } |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 1013 | const char *l; |
Adrian Prantl | 350de4f | 2019-09-24 00:38:49 +0000 | [diff] [blame] | 1014 | switch (D->getLanguage()) { |
| 1015 | case LinkageSpecDecl::lang_c: |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1016 | l = "C"; |
Adrian Prantl | 350de4f | 2019-09-24 00:38:49 +0000 | [diff] [blame] | 1017 | break; |
| 1018 | case LinkageSpecDecl::lang_cxx_14: |
| 1019 | l = "C++14"; |
| 1020 | break; |
| 1021 | case LinkageSpecDecl::lang_cxx_11: |
| 1022 | l = "C++11"; |
| 1023 | break; |
| 1024 | case LinkageSpecDecl::lang_cxx: |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1025 | l = "C++"; |
Adrian Prantl | 350de4f | 2019-09-24 00:38:49 +0000 | [diff] [blame] | 1026 | break; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | Out << "extern \"" << l << "\" "; |
| 1030 | if (D->hasBraces()) { |
| 1031 | Out << "{\n"; |
| 1032 | VisitDeclContext(D); |
| 1033 | Indent() << "}"; |
| 1034 | } else |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1035 | Visit(*D->decls_begin()); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1038 | void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params, |
| 1039 | bool OmitTemplateKW) { |
Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 1040 | assert(Params); |
Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 1041 | |
Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1042 | if (!OmitTemplateKW) |
| 1043 | Out << "template "; |
| 1044 | Out << '<'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1045 | |
Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1046 | bool NeedComma = false; |
| 1047 | for (const Decl *Param : *Params) { |
| 1048 | if (Param->isImplicit()) |
| 1049 | continue; |
| 1050 | |
| 1051 | if (NeedComma) |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 1052 | Out << ", "; |
Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1053 | else |
| 1054 | NeedComma = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1055 | |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1056 | if (auto TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1057 | |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 1058 | if (TTP->wasDeclaredWithTypename()) |
Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1059 | Out << "typename"; |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 1060 | else |
Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1061 | Out << "class"; |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 1062 | |
Anders Carlsson | fb1d776 | 2009-06-12 22:23:22 +0000 | [diff] [blame] | 1063 | if (TTP->isParameterPack()) |
Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1064 | Out << " ..."; |
| 1065 | else if (!TTP->getName().empty()) |
| 1066 | Out << ' '; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1067 | |
Benjamin Kramer | db0fc51 | 2012-02-07 11:57:57 +0000 | [diff] [blame] | 1068 | Out << *TTP; |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 1069 | |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1070 | if (TTP->hasDefaultArgument()) { |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 1071 | Out << " = "; |
| 1072 | Out << TTP->getDefaultArgument().getAsString(Policy); |
| 1073 | }; |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1074 | } else if (auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Richard Smith | a4bb292 | 2014-07-23 03:17:06 +0000 | [diff] [blame] | 1075 | StringRef Name; |
| 1076 | if (IdentifierInfo *II = NTTP->getIdentifier()) |
| 1077 | Name = II->getName(); |
| 1078 | printDeclType(NTTP->getType(), Name, NTTP->isParameterPack()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1079 | |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1080 | if (NTTP->hasDefaultArgument()) { |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 1081 | Out << " = "; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1082 | NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy, |
| 1083 | Indentation); |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 1084 | } |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1085 | } else if (auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
Richard Smith | 030f499 | 2011-04-15 13:38:57 +0000 | [diff] [blame] | 1086 | VisitTemplateDecl(TTPD); |
| 1087 | // FIXME: print the default argument, if present. |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 1088 | } |
| 1089 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1090 | |
Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1091 | Out << '>'; |
| 1092 | if (!OmitTemplateKW) |
| 1093 | Out << ' '; |
Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Sam McCall | 87054ec | 2019-11-14 14:16:14 +0100 | [diff] [blame] | 1096 | void DeclPrinter::printTemplateArguments(ArrayRef<TemplateArgument> Args) { |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1097 | Out << "<"; |
| 1098 | for (size_t I = 0, E = Args.size(); I < E; ++I) { |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1099 | if (I) |
| 1100 | Out << ", "; |
Sam McCall | 87054ec | 2019-11-14 14:16:14 +0100 | [diff] [blame] | 1101 | Args[I].print(Policy, Out); |
| 1102 | } |
| 1103 | Out << ">"; |
| 1104 | } |
| 1105 | |
| 1106 | void DeclPrinter::printTemplateArguments(ArrayRef<TemplateArgumentLoc> Args) { |
| 1107 | Out << "<"; |
| 1108 | for (size_t I = 0, E = Args.size(); I < E; ++I) { |
| 1109 | if (I) |
| 1110 | Out << ", "; |
| 1111 | Args[I].getArgument().print(Policy, Out); |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1112 | } |
| 1113 | Out << ">"; |
| 1114 | } |
| 1115 | |
Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 1116 | void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) { |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1117 | printTemplateParameters(D->getTemplateParameters()); |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 1118 | |
Richard Smith | 030f499 | 2011-04-15 13:38:57 +0000 | [diff] [blame] | 1119 | if (const TemplateTemplateParmDecl *TTP = |
| 1120 | dyn_cast<TemplateTemplateParmDecl>(D)) { |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1121 | Out << "class "; |
| 1122 | if (TTP->isParameterPack()) |
| 1123 | Out << "..."; |
| 1124 | Out << D->getName(); |
Saar Raz | d7aae33 | 2019-07-10 21:25:49 +0000 | [diff] [blame] | 1125 | } else if (auto *TD = D->getTemplatedDecl()) |
| 1126 | Visit(TD); |
| 1127 | else if (const auto *Concept = dyn_cast<ConceptDecl>(D)) { |
| 1128 | Out << "concept " << Concept->getName() << " = " ; |
| 1129 | Concept->getConstraintExpr()->printPretty(Out, nullptr, Policy, |
| 1130 | Indentation); |
| 1131 | Out << ";"; |
Craig Silverstein | 4a5d086 | 2010-07-09 20:25:10 +0000 | [diff] [blame] | 1132 | } |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 1135 | void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
Alexey Bataev | 6d45532 | 2015-10-12 06:59:48 +0000 | [diff] [blame] | 1136 | prettyPrintPragmas(D->getTemplatedDecl()); |
Alex Lorenz | 47fd10c | 2017-04-18 15:12:34 +0000 | [diff] [blame] | 1137 | // Print any leading template parameter lists. |
| 1138 | if (const FunctionDecl *FD = D->getTemplatedDecl()) { |
| 1139 | for (unsigned I = 0, NumTemplateParams = FD->getNumTemplateParameterLists(); |
| 1140 | I < NumTemplateParams; ++I) |
| 1141 | printTemplateParameters(FD->getTemplateParameterList(I)); |
| 1142 | } |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1143 | VisitRedeclarableTemplateDecl(D); |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 1144 | // Declare target attribute is special one, natural spelling for the pragma |
| 1145 | // assumes "ending" construct so print it here. |
| 1146 | if (D->getTemplatedDecl()->hasAttr<OMPDeclareTargetDeclAttr>()) |
| 1147 | Out << "#pragma omp end declare target\n"; |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1148 | |
Richard Smith | 057ec50 | 2017-02-18 01:01:48 +0000 | [diff] [blame] | 1149 | // Never print "instantiations" for deduction guides (they don't really |
| 1150 | // have them). |
| 1151 | if (PrintInstantiation && |
| 1152 | !isa<CXXDeductionGuideDecl>(D->getTemplatedDecl())) { |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1153 | FunctionDecl *PrevDecl = D->getTemplatedDecl(); |
| 1154 | const FunctionDecl *Def; |
| 1155 | if (PrevDecl->isDefined(Def) && Def != PrevDecl) |
| 1156 | return; |
| 1157 | for (auto *I : D->specializations()) |
| 1158 | if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) { |
| 1159 | if (!PrevDecl->isThisDeclarationADefinition()) |
| 1160 | Out << ";\n"; |
| 1161 | Indent(); |
| 1162 | prettyPrintPragmas(I); |
| 1163 | Visit(I); |
| 1164 | } |
| 1165 | } |
Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1169 | VisitRedeclarableTemplateDecl(D); |
Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 1170 | |
Serge Pavlov | a67a4d2 | 2016-11-10 08:49:37 +0000 | [diff] [blame] | 1171 | if (PrintInstantiation) { |
| 1172 | for (auto *I : D->specializations()) |
| 1173 | if (I->getSpecializationKind() == TSK_ImplicitInstantiation) { |
| 1174 | if (D->isThisDeclarationADefinition()) |
| 1175 | Out << ";"; |
| 1176 | Out << "\n"; |
| 1177 | Visit(I); |
| 1178 | } |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | void DeclPrinter::VisitClassTemplateSpecializationDecl( |
| 1183 | ClassTemplateSpecializationDecl *D) { |
| 1184 | Out << "template<> "; |
| 1185 | VisitCXXRecordDecl(D); |
| 1186 | } |
| 1187 | |
| 1188 | void DeclPrinter::VisitClassTemplatePartialSpecializationDecl( |
| 1189 | ClassTemplatePartialSpecializationDecl *D) { |
| 1190 | printTemplateParameters(D->getTemplateParameters()); |
| 1191 | VisitCXXRecordDecl(D); |
Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1194 | //---------------------------------------------------------------------------- |
| 1195 | // Objective-C declarations |
| 1196 | //---------------------------------------------------------------------------- |
| 1197 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1198 | void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx, |
| 1199 | Decl::ObjCDeclQualifier Quals, |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1200 | QualType T) { |
| 1201 | Out << '('; |
| 1202 | if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In) |
| 1203 | Out << "in "; |
| 1204 | if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout) |
| 1205 | Out << "inout "; |
| 1206 | if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out) |
| 1207 | Out << "out "; |
| 1208 | if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy) |
| 1209 | Out << "bycopy "; |
| 1210 | if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref) |
| 1211 | Out << "byref "; |
| 1212 | if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway) |
| 1213 | Out << "oneway "; |
| 1214 | if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) { |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 1215 | if (auto nullability = AttributedType::stripOuterNullability(T)) |
| 1216 | Out << getNullabilitySpelling(*nullability, true) << ' '; |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1217 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1218 | |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1219 | Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy); |
| 1220 | Out << ')'; |
| 1221 | } |
| 1222 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1223 | void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) { |
| 1224 | Out << "<"; |
| 1225 | unsigned First = true; |
| 1226 | for (auto *Param : *Params) { |
| 1227 | if (First) { |
| 1228 | First = false; |
| 1229 | } else { |
| 1230 | Out << ", "; |
| 1231 | } |
| 1232 | |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 1233 | switch (Param->getVariance()) { |
| 1234 | case ObjCTypeParamVariance::Invariant: |
| 1235 | break; |
| 1236 | |
| 1237 | case ObjCTypeParamVariance::Covariant: |
| 1238 | Out << "__covariant "; |
| 1239 | break; |
| 1240 | |
| 1241 | case ObjCTypeParamVariance::Contravariant: |
| 1242 | Out << "__contravariant "; |
| 1243 | break; |
| 1244 | } |
| 1245 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1246 | Out << Param->getDeclName().getAsString(); |
| 1247 | |
| 1248 | if (Param->hasExplicitBound()) { |
| 1249 | Out << " : " << Param->getUnderlyingType().getAsString(Policy); |
| 1250 | } |
| 1251 | } |
| 1252 | Out << ">"; |
| 1253 | } |
| 1254 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1255 | void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) { |
| 1256 | if (OMD->isInstanceMethod()) |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1257 | Out << "- "; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1258 | else |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1259 | Out << "+ "; |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1260 | if (!OMD->getReturnType().isNull()) { |
| 1261 | PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(), |
| 1262 | OMD->getReturnType()); |
| 1263 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1264 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1265 | std::string name = OMD->getSelector().getAsString(); |
| 1266 | std::string::size_type pos, lastPos = 0; |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 1267 | for (const auto *PI : OMD->parameters()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1268 | // FIXME: selector is missing here! |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1269 | pos = name.find_first_of(':', lastPos); |
Alex Lorenz | bbf4f70 | 2017-06-02 15:02:59 +0000 | [diff] [blame] | 1270 | if (lastPos != 0) |
| 1271 | Out << " "; |
| 1272 | Out << name.substr(lastPos, pos - lastPos) << ':'; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1273 | PrintObjCMethodType(OMD->getASTContext(), |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1274 | PI->getObjCDeclQualifier(), |
| 1275 | PI->getType()); |
| 1276 | Out << *PI; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1277 | lastPos = pos + 1; |
| 1278 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1279 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1280 | if (OMD->param_begin() == OMD->param_end()) |
Alex Lorenz | bbf4f70 | 2017-06-02 15:02:59 +0000 | [diff] [blame] | 1281 | Out << name; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1282 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1283 | if (OMD->isVariadic()) |
| 1284 | Out << ", ..."; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1285 | |
Fariborz Jahanian | aae7fef | 2014-10-03 20:05:33 +0000 | [diff] [blame] | 1286 | prettyPrintAttributes(OMD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1287 | |
Fariborz Jahanian | a7d76d2 | 2012-10-17 21:58:03 +0000 | [diff] [blame] | 1288 | if (OMD->getBody() && !Policy.TerseOutput) { |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1289 | Out << ' '; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1290 | OMD->getBody()->printPretty(Out, nullptr, Policy); |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1291 | } |
Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 1292 | else if (Policy.PolishForDeclaration) |
Fariborz Jahanian | 9b7ab87 | 2012-12-18 23:02:59 +0000 | [diff] [blame] | 1293 | Out << ';'; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) { |
| 1297 | std::string I = OID->getNameAsString(); |
| 1298 | ObjCInterfaceDecl *SID = OID->getSuperClass(); |
| 1299 | |
Fariborz Jahanian | aae7fef | 2014-10-03 20:05:33 +0000 | [diff] [blame] | 1300 | bool eolnOut = false; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1301 | if (SID) |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1302 | Out << "@implementation " << I << " : " << *SID; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1303 | else |
| 1304 | Out << "@implementation " << I; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1305 | |
Fariborz Jahanian | 95759ff | 2012-12-04 00:47:33 +0000 | [diff] [blame] | 1306 | if (OID->ivar_size() > 0) { |
| 1307 | Out << "{\n"; |
Fariborz Jahanian | aae7fef | 2014-10-03 20:05:33 +0000 | [diff] [blame] | 1308 | eolnOut = true; |
Fariborz Jahanian | 95759ff | 2012-12-04 00:47:33 +0000 | [diff] [blame] | 1309 | Indentation += Policy.Indentation; |
Aaron Ballman | d6d25de | 2014-03-14 15:16:45 +0000 | [diff] [blame] | 1310 | for (const auto *I : OID->ivars()) { |
Fariborz Jahanian | b5f3468 | 2013-05-01 20:53:21 +0000 | [diff] [blame] | 1311 | Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()). |
Aaron Ballman | d6d25de | 2014-03-14 15:16:45 +0000 | [diff] [blame] | 1312 | getAsString(Policy) << ' ' << *I << ";\n"; |
Fariborz Jahanian | 95759ff | 2012-12-04 00:47:33 +0000 | [diff] [blame] | 1313 | } |
| 1314 | Indentation -= Policy.Indentation; |
| 1315 | Out << "}\n"; |
| 1316 | } |
Fariborz Jahanian | aae7fef | 2014-10-03 20:05:33 +0000 | [diff] [blame] | 1317 | else if (SID || (OID->decls_begin() != OID->decls_end())) { |
| 1318 | Out << "\n"; |
| 1319 | eolnOut = true; |
| 1320 | } |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1321 | VisitDeclContext(OID, false); |
Fariborz Jahanian | aae7fef | 2014-10-03 20:05:33 +0000 | [diff] [blame] | 1322 | if (!eolnOut) |
| 1323 | Out << "\n"; |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1324 | Out << "@end"; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) { |
| 1328 | std::string I = OID->getNameAsString(); |
| 1329 | ObjCInterfaceDecl *SID = OID->getSuperClass(); |
| 1330 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1331 | if (!OID->isThisDeclarationADefinition()) { |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1332 | Out << "@class " << I; |
| 1333 | |
| 1334 | if (auto TypeParams = OID->getTypeParamListAsWritten()) { |
| 1335 | PrintObjCTypeParams(TypeParams); |
| 1336 | } |
| 1337 | |
| 1338 | Out << ";"; |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1339 | return; |
| 1340 | } |
Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 1341 | bool eolnOut = false; |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1342 | Out << "@interface " << I; |
| 1343 | |
| 1344 | if (auto TypeParams = OID->getTypeParamListAsWritten()) { |
| 1345 | PrintObjCTypeParams(TypeParams); |
| 1346 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1347 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1348 | if (SID) |
Bob Wilson | 1f24ea15 | 2015-10-01 00:53:13 +0000 | [diff] [blame] | 1349 | Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1350 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1351 | // Protocols? |
| 1352 | const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols(); |
| 1353 | if (!Protocols.empty()) { |
| 1354 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 1355 | E = Protocols.end(); I != E; ++I) |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1356 | Out << (I == Protocols.begin() ? '<' : ',') << **I; |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1357 | Out << "> "; |
Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 1358 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1359 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1360 | if (OID->ivar_size() > 0) { |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1361 | Out << "{\n"; |
Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 1362 | eolnOut = true; |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1363 | Indentation += Policy.Indentation; |
Aaron Ballman | 59abbd4 | 2014-03-13 21:09:43 +0000 | [diff] [blame] | 1364 | for (const auto *I : OID->ivars()) { |
| 1365 | Indent() << I->getASTContext() |
| 1366 | .getUnqualifiedObjCPointerType(I->getType()) |
| 1367 | .getAsString(Policy) << ' ' << *I << ";\n"; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1368 | } |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1369 | Indentation -= Policy.Indentation; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1370 | Out << "}\n"; |
| 1371 | } |
Fariborz Jahanian | aae7fef | 2014-10-03 20:05:33 +0000 | [diff] [blame] | 1372 | else if (SID || (OID->decls_begin() != OID->decls_end())) { |
Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 1373 | Out << "\n"; |
| 1374 | eolnOut = true; |
| 1375 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1376 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1377 | VisitDeclContext(OID, false); |
Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 1378 | if (!eolnOut) |
Fariborz Jahanian | aae7fef | 2014-10-03 20:05:33 +0000 | [diff] [blame] | 1379 | Out << "\n"; |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1380 | Out << "@end"; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1381 | // FIXME: implement the rest... |
| 1382 | } |
| 1383 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1384 | void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1385 | if (!PID->isThisDeclarationADefinition()) { |
Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 1386 | Out << "@protocol " << *PID << ";\n"; |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1387 | return; |
| 1388 | } |
Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 1389 | // Protocols? |
| 1390 | const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols(); |
| 1391 | if (!Protocols.empty()) { |
| 1392 | Out << "@protocol " << *PID; |
| 1393 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 1394 | E = Protocols.end(); I != E; ++I) |
| 1395 | Out << (I == Protocols.begin() ? '<' : ',') << **I; |
| 1396 | Out << ">\n"; |
| 1397 | } else |
| 1398 | Out << "@protocol " << *PID << '\n'; |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1399 | VisitDeclContext(PID, false); |
| 1400 | Out << "@end"; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1404 | Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n"; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1405 | |
| 1406 | VisitDeclContext(PID, false); |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1407 | Out << "@end"; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1408 | // FIXME: implement the rest... |
| 1409 | } |
| 1410 | |
| 1411 | void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) { |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1412 | Out << "@interface " << *PID->getClassInterface(); |
| 1413 | if (auto TypeParams = PID->getTypeParamList()) { |
| 1414 | PrintObjCTypeParams(TypeParams); |
| 1415 | } |
| 1416 | Out << "(" << *PID << ")\n"; |
Fariborz Jahanian | 4cf177e | 2012-12-04 17:20:57 +0000 | [diff] [blame] | 1417 | if (PID->ivar_size() > 0) { |
| 1418 | Out << "{\n"; |
| 1419 | Indentation += Policy.Indentation; |
Aaron Ballman | 865fbcd | 2014-03-14 13:13:27 +0000 | [diff] [blame] | 1420 | for (const auto *I : PID->ivars()) |
Fariborz Jahanian | b5f3468 | 2013-05-01 20:53:21 +0000 | [diff] [blame] | 1421 | Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()). |
Aaron Ballman | 865fbcd | 2014-03-14 13:13:27 +0000 | [diff] [blame] | 1422 | getAsString(Policy) << ' ' << *I << ";\n"; |
Fariborz Jahanian | 4cf177e | 2012-12-04 17:20:57 +0000 | [diff] [blame] | 1423 | Indentation -= Policy.Indentation; |
| 1424 | Out << "}\n"; |
| 1425 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1426 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1427 | VisitDeclContext(PID, false); |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1428 | Out << "@end"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1430 | // FIXME: implement the rest... |
| 1431 | } |
| 1432 | |
| 1433 | void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1434 | Out << "@compatibility_alias " << *AID |
| 1435 | << ' ' << *AID->getClassInterface() << ";\n"; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | /// PrintObjCPropertyDecl - print a property declaration. |
| 1439 | /// |
David Goldman | fa8185c | 2019-04-08 19:52:45 +0000 | [diff] [blame] | 1440 | /// Print attributes in the following order: |
| 1441 | /// - class |
| 1442 | /// - nonatomic | atomic |
| 1443 | /// - assign | retain | strong | copy | weak | unsafe_unretained |
| 1444 | /// - readwrite | readonly |
| 1445 | /// - getter & setter |
| 1446 | /// - nullability |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1447 | void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) { |
| 1448 | if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required) |
| 1449 | Out << "@required\n"; |
| 1450 | else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) |
| 1451 | Out << "@optional\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1452 | |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1453 | QualType T = PDecl->getType(); |
| 1454 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1455 | Out << "@property"; |
| 1456 | if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) { |
| 1457 | bool first = true; |
David Goldman | fa8185c | 2019-04-08 19:52:45 +0000 | [diff] [blame] | 1458 | Out << "("; |
| 1459 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_class) { |
| 1460 | Out << (first ? "" : ", ") << "class"; |
Ted Kremenek | 897af91 | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 1461 | first = false; |
| 1462 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | |
Pierre Habouzit | d4e1ba3 | 2019-11-07 23:14:58 -0800 | [diff] [blame^] | 1464 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_direct) { |
| 1465 | Out << (first ? "" : ", ") << "direct"; |
| 1466 | first = false; |
| 1467 | } |
| 1468 | |
Ted Kremenek | 897af91 | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 1469 | if (PDecl->getPropertyAttributes() & |
| 1470 | ObjCPropertyDecl::OBJC_PR_nonatomic) { |
David Goldman | fa8185c | 2019-04-08 19:52:45 +0000 | [diff] [blame] | 1471 | Out << (first ? "" : ", ") << "nonatomic"; |
Ted Kremenek | 897af91 | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 1472 | first = false; |
| 1473 | } |
| 1474 | if (PDecl->getPropertyAttributes() & |
| 1475 | ObjCPropertyDecl::OBJC_PR_atomic) { |
David Goldman | fa8185c | 2019-04-08 19:52:45 +0000 | [diff] [blame] | 1476 | Out << (first ? "" : ", ") << "atomic"; |
| 1477 | first = false; |
| 1478 | } |
| 1479 | |
| 1480 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) { |
| 1481 | Out << (first ? "" : ", ") << "assign"; |
| 1482 | first = false; |
| 1483 | } |
| 1484 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) { |
| 1485 | Out << (first ? "" : ", ") << "retain"; |
| 1486 | first = false; |
| 1487 | } |
| 1488 | |
| 1489 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) { |
| 1490 | Out << (first ? "" : ", ") << "strong"; |
| 1491 | first = false; |
| 1492 | } |
| 1493 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) { |
| 1494 | Out << (first ? "" : ", ") << "copy"; |
| 1495 | first = false; |
| 1496 | } |
| 1497 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak) { |
| 1498 | Out << (first ? "" : ", ") << "weak"; |
| 1499 | first = false; |
| 1500 | } |
| 1501 | if (PDecl->getPropertyAttributes() |
| 1502 | & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) { |
| 1503 | Out << (first ? "" : ", ") << "unsafe_unretained"; |
| 1504 | first = false; |
| 1505 | } |
| 1506 | |
| 1507 | if (PDecl->getPropertyAttributes() & |
| 1508 | ObjCPropertyDecl::OBJC_PR_readwrite) { |
| 1509 | Out << (first ? "" : ", ") << "readwrite"; |
| 1510 | first = false; |
| 1511 | } |
| 1512 | if (PDecl->getPropertyAttributes() & |
| 1513 | ObjCPropertyDecl::OBJC_PR_readonly) { |
| 1514 | Out << (first ? "" : ", ") << "readonly"; |
| 1515 | first = false; |
| 1516 | } |
| 1517 | |
| 1518 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 1519 | Out << (first ? "" : ", ") << "getter = "; |
| 1520 | PDecl->getGetterName().print(Out); |
| 1521 | first = false; |
| 1522 | } |
| 1523 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 1524 | Out << (first ? "" : ", ") << "setter = "; |
| 1525 | PDecl->getSetterName().print(Out); |
Ted Kremenek | 897af91 | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 1526 | first = false; |
| 1527 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1528 | |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1529 | if (PDecl->getPropertyAttributes() & |
| 1530 | ObjCPropertyDecl::OBJC_PR_nullability) { |
Douglas Gregor | 86b4268 | 2015-06-19 18:27:52 +0000 | [diff] [blame] | 1531 | if (auto nullability = AttributedType::stripOuterNullability(T)) { |
Douglas Gregor | 849ebc2 | 2015-06-19 18:14:46 +0000 | [diff] [blame] | 1532 | if (*nullability == NullabilityKind::Unspecified && |
| 1533 | (PDecl->getPropertyAttributes() & |
| 1534 | ObjCPropertyDecl::OBJC_PR_null_resettable)) { |
David Goldman | fa8185c | 2019-04-08 19:52:45 +0000 | [diff] [blame] | 1535 | Out << (first ? "" : ", ") << "null_resettable"; |
Douglas Gregor | 849ebc2 | 2015-06-19 18:14:46 +0000 | [diff] [blame] | 1536 | } else { |
David Goldman | fa8185c | 2019-04-08 19:52:45 +0000 | [diff] [blame] | 1537 | Out << (first ? "" : ", ") |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 1538 | << getNullabilitySpelling(*nullability, true); |
Douglas Gregor | 849ebc2 | 2015-06-19 18:14:46 +0000 | [diff] [blame] | 1539 | } |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1540 | first = false; |
| 1541 | } |
| 1542 | } |
| 1543 | |
Ted Kremenek | 897af91 | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 1544 | (void) first; // Silence dead store warning due to idiomatic code. |
David Goldman | fa8185c | 2019-04-08 19:52:45 +0000 | [diff] [blame] | 1545 | Out << ")"; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1546 | } |
David Goldman | fa8185c | 2019-04-08 19:52:45 +0000 | [diff] [blame] | 1547 | std::string TypeStr = PDecl->getASTContext().getUnqualifiedObjCPointerType(T). |
| 1548 | getAsString(Policy); |
| 1549 | Out << ' ' << TypeStr; |
| 1550 | if (!StringRef(TypeStr).endswith("*")) |
| 1551 | Out << ' '; |
| 1552 | Out << *PDecl; |
Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 1553 | if (Policy.PolishForDeclaration) |
| 1554 | Out << ';'; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { |
| 1558 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1559 | Out << "@synthesize "; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1560 | else |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1561 | Out << "@dynamic "; |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1562 | Out << *PID->getPropertyDecl(); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1563 | if (PID->getPropertyIvarDecl()) |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1564 | Out << '=' << *PID->getPropertyIvarDecl(); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1565 | } |
Anders Carlsson | df5a1c8 | 2009-08-28 19:16:39 +0000 | [diff] [blame] | 1566 | |
| 1567 | void DeclPrinter::VisitUsingDecl(UsingDecl *D) { |
Enea Zaffanella | c70b251 | 2013-07-17 17:28:56 +0000 | [diff] [blame] | 1568 | if (!D->isAccessDeclaration()) |
| 1569 | Out << "using "; |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 1570 | if (D->hasTypename()) |
Enea Zaffanella | c70b251 | 2013-07-17 17:28:56 +0000 | [diff] [blame] | 1571 | Out << "typename "; |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1572 | D->getQualifier()->print(Out, Policy); |
Alex Lorenz | ea9b59a | 2016-10-03 12:22:17 +0000 | [diff] [blame] | 1573 | |
| 1574 | // Use the correct record name when the using declaration is used for |
| 1575 | // inheriting constructors. |
| 1576 | for (const auto *Shadow : D->shadows()) { |
| 1577 | if (const auto *ConstructorShadow = |
| 1578 | dyn_cast<ConstructorUsingShadowDecl>(Shadow)) { |
| 1579 | assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext()); |
| 1580 | Out << *ConstructorShadow->getNominatedBaseClass(); |
| 1581 | return; |
| 1582 | } |
| 1583 | } |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1584 | Out << *D; |
Anders Carlsson | df5a1c8 | 2009-08-28 19:16:39 +0000 | [diff] [blame] | 1585 | } |
| 1586 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1587 | void |
| 1588 | DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) { |
| 1589 | Out << "using typename "; |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1590 | D->getQualifier()->print(Out, Policy); |
Benjamin Kramer | b11416d | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 1591 | Out << D->getDeclName(); |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
Enea Zaffanella | c70b251 | 2013-07-17 17:28:56 +0000 | [diff] [blame] | 1595 | if (!D->isAccessDeclaration()) |
| 1596 | Out << "using "; |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1597 | D->getQualifier()->print(Out, Policy); |
Benjamin Kramer | 36d514e | 2015-09-23 13:43:16 +0000 | [diff] [blame] | 1598 | Out << D->getDeclName(); |
Anders Carlsson | df5a1c8 | 2009-08-28 19:16:39 +0000 | [diff] [blame] | 1599 | } |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 1600 | |
| 1601 | void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) { |
| 1602 | // ignore |
| 1603 | } |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1604 | |
| 1605 | void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) { |
| 1606 | Out << "#pragma omp threadprivate"; |
| 1607 | if (!D->varlist_empty()) { |
| 1608 | for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(), |
| 1609 | E = D->varlist_end(); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 1610 | I != E; ++I) { |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 1611 | Out << (I == D->varlist_begin() ? '(' : ','); |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 1612 | NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl(); |
Alexey Bataev | 7d2960b | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 1613 | ND->printQualifiedName(Out); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1614 | } |
| 1615 | Out << ")"; |
| 1616 | } |
| 1617 | } |
| 1618 | |
Alexey Bataev | 25ed0c0 | 2019-03-07 17:54:44 +0000 | [diff] [blame] | 1619 | void DeclPrinter::VisitOMPAllocateDecl(OMPAllocateDecl *D) { |
| 1620 | Out << "#pragma omp allocate"; |
| 1621 | if (!D->varlist_empty()) { |
| 1622 | for (OMPAllocateDecl::varlist_iterator I = D->varlist_begin(), |
| 1623 | E = D->varlist_end(); |
| 1624 | I != E; ++I) { |
| 1625 | Out << (I == D->varlist_begin() ? '(' : ','); |
| 1626 | NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl(); |
| 1627 | ND->printQualifiedName(Out); |
| 1628 | } |
| 1629 | Out << ")"; |
| 1630 | } |
Alexey Bataev | 9cc10fc | 2019-03-12 18:52:33 +0000 | [diff] [blame] | 1631 | if (!D->clauselist_empty()) { |
| 1632 | Out << " "; |
| 1633 | OMPClausePrinter Printer(Out, Policy); |
| 1634 | for (OMPClause *C : D->clauselists()) |
| 1635 | Printer.Visit(C); |
| 1636 | } |
Alexey Bataev | 25ed0c0 | 2019-03-07 17:54:44 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 1639 | void DeclPrinter::VisitOMPRequiresDecl(OMPRequiresDecl *D) { |
| 1640 | Out << "#pragma omp requires "; |
| 1641 | if (!D->clauselist_empty()) { |
Patrick Lyster | 7a2a27c | 2018-11-02 12:18:11 +0000 | [diff] [blame] | 1642 | OMPClausePrinter Printer(Out, Policy); |
| 1643 | for (auto I = D->clauselist_begin(), E = D->clauselist_end(); I != E; ++I) |
| 1644 | Printer.Visit(*I); |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 1645 | } |
| 1646 | } |
| 1647 | |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 1648 | void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) { |
| 1649 | if (!D->isInvalidDecl()) { |
| 1650 | Out << "#pragma omp declare reduction ("; |
| 1651 | if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) { |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 1652 | const char *OpName = |
Richard Smith | 7fa2b74 | 2019-06-14 20:01:51 +0000 | [diff] [blame] | 1653 | getOperatorSpelling(D->getDeclName().getCXXOverloadedOperator()); |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 1654 | assert(OpName && "not an overloaded operator"); |
| 1655 | Out << OpName; |
| 1656 | } else { |
| 1657 | assert(D->getDeclName().isIdentifier()); |
| 1658 | D->printName(Out); |
| 1659 | } |
| 1660 | Out << " : "; |
| 1661 | D->getType().print(Out, Policy); |
| 1662 | Out << " : "; |
| 1663 | D->getCombiner()->printPretty(Out, nullptr, Policy, 0); |
| 1664 | Out << ")"; |
| 1665 | if (auto *Init = D->getInitializer()) { |
| 1666 | Out << " initializer("; |
Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 1667 | switch (D->getInitializerKind()) { |
| 1668 | case OMPDeclareReductionDecl::DirectInit: |
| 1669 | Out << "omp_priv("; |
| 1670 | break; |
| 1671 | case OMPDeclareReductionDecl::CopyInit: |
| 1672 | Out << "omp_priv = "; |
| 1673 | break; |
| 1674 | case OMPDeclareReductionDecl::CallInit: |
| 1675 | break; |
| 1676 | } |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 1677 | Init->printPretty(Out, nullptr, Policy, 0); |
Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 1678 | if (D->getInitializerKind() == OMPDeclareReductionDecl::DirectInit) |
| 1679 | Out << ")"; |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 1680 | Out << ")"; |
| 1681 | } |
| 1682 | } |
| 1683 | } |
| 1684 | |
Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 1685 | void DeclPrinter::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) { |
| 1686 | if (!D->isInvalidDecl()) { |
| 1687 | Out << "#pragma omp declare mapper ("; |
| 1688 | D->printName(Out); |
| 1689 | Out << " : "; |
| 1690 | D->getType().print(Out, Policy); |
| 1691 | Out << " "; |
| 1692 | Out << D->getVarName(); |
| 1693 | Out << ")"; |
| 1694 | if (!D->clauselist_empty()) { |
| 1695 | OMPClausePrinter Printer(Out, Policy); |
| 1696 | for (auto *C : D->clauselists()) { |
| 1697 | Out << " "; |
| 1698 | Printer.Visit(C); |
| 1699 | } |
| 1700 | } |
| 1701 | } |
| 1702 | } |
| 1703 | |
Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 1704 | void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) { |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 1705 | D->getInit()->printPretty(Out, nullptr, Policy, Indentation); |
| 1706 | } |
| 1707 | |