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