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