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