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