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