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