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