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