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