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 | } |
Argyrios Kyrtzidis | a18347e | 2012-05-05 04:20:37 +0000 | [diff] [blame] | 340 | D->getUnderlyingType().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) { |
Benjamin Kramer | db0fc51 | 2012-02-07 11:57:57 +0000 | [diff] [blame] | 345 | Out << "using " << *D << " = " << D->getUnderlyingType().getAsString(Policy); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 348 | void DeclPrinter::VisitEnumDecl(EnumDecl *D) { |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 349 | if (!Policy.SuppressSpecifiers && D->isModulePrivate()) |
| 350 | Out << "__module_private__ "; |
Douglas Gregor | ec0e366 | 2010-12-01 16:01:08 +0000 | [diff] [blame] | 351 | Out << "enum "; |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 352 | if (D->isScoped()) { |
| 353 | if (D->isScopedUsingClassTag()) |
| 354 | Out << "class "; |
| 355 | else |
| 356 | Out << "struct "; |
| 357 | } |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 358 | Out << *D; |
Douglas Gregor | ec0e366 | 2010-12-01 16:01:08 +0000 | [diff] [blame] | 359 | |
Argyrios Kyrtzidis | a18347e | 2012-05-05 04:20:37 +0000 | [diff] [blame] | 360 | if (D->isFixed()) |
| 361 | Out << " : " << D->getIntegerType().stream(Policy); |
Douglas Gregor | ec0e366 | 2010-12-01 16:01:08 +0000 | [diff] [blame] | 362 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 363 | if (D->isCompleteDefinition()) { |
Douglas Gregor | ec0e366 | 2010-12-01 16:01:08 +0000 | [diff] [blame] | 364 | Out << " {\n"; |
| 365 | VisitDeclContext(D); |
| 366 | Indent() << "}"; |
| 367 | } |
Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 368 | prettyPrintAttributes(D); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | void DeclPrinter::VisitRecordDecl(RecordDecl *D) { |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 372 | if (!Policy.SuppressSpecifiers && D->isModulePrivate()) |
| 373 | Out << "__module_private__ "; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 374 | Out << D->getKindName(); |
Benjamin Kramer | b11416d | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 375 | if (D->getIdentifier()) |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 376 | Out << ' ' << *D; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 377 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 378 | if (D->isCompleteDefinition()) { |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 379 | Out << " {\n"; |
| 380 | VisitDeclContext(D); |
| 381 | Indent() << "}"; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) { |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 386 | Out << *D; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 387 | if (Expr *Init = D->getInitExpr()) { |
| 388 | Out << " = "; |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 389 | Init->printPretty(Out, 0, Policy, Indentation); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { |
Fariborz Jahanian | 69c403c | 2012-12-05 22:19:06 +0000 | [diff] [blame] | 394 | CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D); |
Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 395 | if (!Policy.SuppressSpecifiers) { |
Peter Collingbourne | 32fab90 | 2011-11-08 02:52:52 +0000 | [diff] [blame] | 396 | switch (D->getStorageClassAsWritten()) { |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 397 | case SC_None: break; |
| 398 | case SC_Extern: Out << "extern "; break; |
| 399 | case SC_Static: Out << "static "; break; |
| 400 | case SC_PrivateExtern: Out << "__private_extern__ "; break; |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 401 | case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal: |
| 402 | llvm_unreachable("invalid for functions"); |
Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 403 | } |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 404 | |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 405 | if (D->isInlineSpecified()) Out << "inline "; |
Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 406 | if (D->isVirtualAsWritten()) Out << "virtual "; |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 407 | if (D->isModulePrivate()) Out << "__module_private__ "; |
Fariborz Jahanian | 69c403c | 2012-12-05 22:19:06 +0000 | [diff] [blame] | 408 | if (CDecl && CDecl->isExplicitSpecified()) |
| 409 | Out << "explicit "; |
Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 410 | } |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 411 | |
Douglas Gregor | 2d042f1 | 2009-05-30 05:39:39 +0000 | [diff] [blame] | 412 | PrintingPolicy SubPolicy(Policy); |
| 413 | SubPolicy.SuppressSpecifiers = false; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 414 | std::string Proto = D->getNameInfo().getAsString(); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 415 | |
Abramo Bagnara | 6d81063 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 416 | QualType Ty = D->getType(); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 417 | while (const ParenType *PT = dyn_cast<ParenType>(Ty)) { |
Abramo Bagnara | 6d81063 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 418 | Proto = '(' + Proto + ')'; |
| 419 | Ty = PT->getInnerType(); |
| 420 | } |
| 421 | |
| 422 | if (isa<FunctionType>(Ty)) { |
| 423 | const FunctionType *AFT = Ty->getAs<FunctionType>(); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 424 | const FunctionProtoType *FT = 0; |
| 425 | if (D->hasWrittenPrototype()) |
| 426 | FT = dyn_cast<FunctionProtoType>(AFT); |
| 427 | |
| 428 | Proto += "("; |
| 429 | if (FT) { |
| 430 | llvm::raw_string_ostream POut(Proto); |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 431 | DeclPrinter ParamPrinter(POut, SubPolicy, Indentation); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 432 | for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { |
| 433 | if (i) POut << ", "; |
| 434 | ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); |
| 435 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 437 | if (FT->isVariadic()) { |
| 438 | if (D->getNumParams()) POut << ", "; |
| 439 | POut << "..."; |
| 440 | } |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 441 | } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) { |
Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 442 | for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { |
| 443 | if (i) |
| 444 | Proto += ", "; |
| 445 | Proto += D->getParamDecl(i)->getNameAsString(); |
| 446 | } |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | Proto += ")"; |
Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 450 | |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 451 | if (FT) { |
| 452 | if (FT->isConst()) |
Douglas Gregor | 8fc96fc | 2010-11-19 18:44:34 +0000 | [diff] [blame] | 453 | Proto += " const"; |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 454 | if (FT->isVolatile()) |
Douglas Gregor | 8fc96fc | 2010-11-19 18:44:34 +0000 | [diff] [blame] | 455 | Proto += " volatile"; |
David Blaikie | f5697e5 | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 456 | if (FT->isRestrict()) |
Douglas Gregor | 8fc96fc | 2010-11-19 18:44:34 +0000 | [diff] [blame] | 457 | Proto += " restrict"; |
| 458 | } |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 459 | |
| 460 | if (FT && FT->hasDynamicExceptionSpec()) { |
Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 461 | Proto += " throw("; |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 462 | if (FT->getExceptionSpecType() == EST_MSAny) |
Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 463 | Proto += "..."; |
| 464 | else |
| 465 | for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) { |
| 466 | if (I) |
| 467 | Proto += ", "; |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 468 | |
Dmitri Gribenko | 76bb5cabfa | 2012-09-10 21:20:09 +0000 | [diff] [blame] | 469 | Proto += FT->getExceptionType(I).getAsString(SubPolicy); |
Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 470 | } |
| 471 | Proto += ")"; |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 472 | } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) { |
| 473 | Proto += " noexcept"; |
| 474 | if (FT->getExceptionSpecType() == EST_ComputedNoexcept) { |
| 475 | Proto += "("; |
| 476 | llvm::raw_string_ostream EOut(Proto); |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 477 | FT->getNoexceptExpr()->printPretty(EOut, 0, SubPolicy, |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 478 | Indentation); |
| 479 | EOut.flush(); |
| 480 | Proto += EOut.str(); |
| 481 | Proto += ")"; |
| 482 | } |
Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Fariborz Jahanian | 69c403c | 2012-12-05 22:19:06 +0000 | [diff] [blame] | 485 | if (CDecl) { |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 486 | bool HasInitializerList = false; |
| 487 | for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(), |
| 488 | E = CDecl->init_end(); |
| 489 | B != E; ++B) { |
| 490 | CXXCtorInitializer * BMInitializer = (*B); |
| 491 | if (BMInitializer->isInClassMemberInitializer()) |
| 492 | continue; |
| 493 | |
| 494 | if (!HasInitializerList) { |
| 495 | Proto += " : "; |
| 496 | Out << Proto; |
| 497 | Proto.clear(); |
| 498 | HasInitializerList = true; |
| 499 | } else |
| 500 | Out << ", "; |
| 501 | |
| 502 | if (BMInitializer->isAnyMemberInitializer()) { |
| 503 | FieldDecl *FD = BMInitializer->getAnyMember(); |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 504 | Out << *FD; |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 505 | } else { |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 506 | Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | Out << "("; |
| 510 | if (!BMInitializer->getInit()) { |
| 511 | // Nothing to print |
| 512 | } else { |
| 513 | Expr *Init = BMInitializer->getInit(); |
| 514 | if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init)) |
| 515 | Init = Tmp->getSubExpr(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 516 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 517 | Init = Init->IgnoreParens(); |
| 518 | |
| 519 | Expr *SimpleInit = 0; |
| 520 | Expr **Args = 0; |
| 521 | unsigned NumArgs = 0; |
| 522 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
| 523 | Args = ParenList->getExprs(); |
| 524 | NumArgs = ParenList->getNumExprs(); |
| 525 | } else if (CXXConstructExpr *Construct |
| 526 | = dyn_cast<CXXConstructExpr>(Init)) { |
| 527 | Args = Construct->getArgs(); |
| 528 | NumArgs = Construct->getNumArgs(); |
| 529 | } else |
| 530 | SimpleInit = Init; |
| 531 | |
| 532 | if (SimpleInit) |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 533 | SimpleInit->printPretty(Out, 0, Policy, Indentation); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 534 | else { |
| 535 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 536 | if (isa<CXXDefaultArgExpr>(Args[I])) |
| 537 | break; |
| 538 | |
| 539 | if (I) |
| 540 | Out << ", "; |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 541 | Args[I]->printPretty(Out, 0, Policy, Indentation); |
Fariborz Jahanian | 494720b | 2009-07-13 20:18:13 +0000 | [diff] [blame] | 542 | } |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 543 | } |
Fariborz Jahanian | 494720b | 2009-07-13 20:18:13 +0000 | [diff] [blame] | 544 | } |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 545 | Out << ")"; |
Fariborz Jahanian | 494720b | 2009-07-13 20:18:13 +0000 | [diff] [blame] | 546 | } |
Fariborz Jahanian | 14ef479 | 2012-12-05 19:54:11 +0000 | [diff] [blame] | 547 | if (!Proto.empty()) |
| 548 | Out << Proto; |
Richard Smith | e656076 | 2013-02-22 05:54:51 +0000 | [diff] [blame] | 549 | } else { |
| 550 | if (FT && FT->hasTrailingReturn()) { |
| 551 | Out << "auto " << Proto << " -> "; |
| 552 | Proto.clear(); |
| 553 | } |
Argyrios Kyrtzidis | a18347e | 2012-05-05 04:20:37 +0000 | [diff] [blame] | 554 | AFT->getResultType().print(Out, Policy, Proto); |
Richard Smith | e656076 | 2013-02-22 05:54:51 +0000 | [diff] [blame] | 555 | } |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 556 | } else { |
Argyrios Kyrtzidis | a18347e | 2012-05-05 04:20:37 +0000 | [diff] [blame] | 557 | Ty.print(Out, Policy, Proto); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 560 | prettyPrintAttributes(D); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 561 | |
| 562 | if (D->isPure()) |
| 563 | Out << " = 0"; |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 564 | else if (D->isDeletedAsWritten()) |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 565 | Out << " = delete"; |
Fariborz Jahanian | de872af | 2012-12-05 22:53:06 +0000 | [diff] [blame] | 566 | else if (D->isExplicitlyDefaulted()) |
| 567 | Out << " = default"; |
Dmitri Gribenko | a9a2af7 | 2012-08-21 17:47:24 +0000 | [diff] [blame] | 568 | else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) { |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 569 | if (!D->hasPrototype() && D->getNumParams()) { |
| 570 | // This is a K&R function definition, so we need to print the |
| 571 | // parameters. |
| 572 | Out << '\n'; |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 573 | DeclPrinter ParamPrinter(Out, SubPolicy, Indentation); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 574 | Indentation += Policy.Indentation; |
| 575 | for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { |
| 576 | Indent(); |
Douglas Gregor | 2d042f1 | 2009-05-30 05:39:39 +0000 | [diff] [blame] | 577 | ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 578 | Out << ";\n"; |
| 579 | } |
| 580 | Indentation -= Policy.Indentation; |
| 581 | } else |
| 582 | Out << ' '; |
| 583 | |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 584 | D->getBody()->printPretty(Out, 0, SubPolicy, Indentation); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 585 | Out << '\n'; |
| 586 | } |
| 587 | } |
| 588 | |
Fariborz Jahanian | bfc3ef5 | 2012-12-05 00:38:44 +0000 | [diff] [blame] | 589 | void DeclPrinter::VisitFriendDecl(FriendDecl *D) { |
| 590 | if (TypeSourceInfo *TSI = D->getFriendType()) { |
Enea Zaffanella | eb22c87 | 2013-01-31 09:54:08 +0000 | [diff] [blame] | 591 | unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists(); |
| 592 | for (unsigned i = 0; i < NumTPLists; ++i) |
| 593 | PrintTemplateParameters(D->getFriendTypeTemplateParameterList(i)); |
Fariborz Jahanian | 7a16a02 | 2012-12-21 21:43:05 +0000 | [diff] [blame] | 594 | Out << "friend "; |
| 595 | Out << " " << TSI->getType().getAsString(Policy); |
Fariborz Jahanian | bfc3ef5 | 2012-12-05 00:38:44 +0000 | [diff] [blame] | 596 | } |
| 597 | else if (FunctionDecl *FD = |
| 598 | dyn_cast<FunctionDecl>(D->getFriendDecl())) { |
| 599 | Out << "friend "; |
| 600 | VisitFunctionDecl(FD); |
| 601 | } |
| 602 | else if (FunctionTemplateDecl *FTD = |
| 603 | dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) { |
| 604 | Out << "friend "; |
| 605 | VisitFunctionTemplateDecl(FTD); |
| 606 | } |
| 607 | else if (ClassTemplateDecl *CTD = |
| 608 | dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) { |
| 609 | Out << "friend "; |
Fariborz Jahanian | 7a16a02 | 2012-12-21 21:43:05 +0000 | [diff] [blame] | 610 | VisitRedeclarableTemplateDecl(CTD); |
Fariborz Jahanian | bfc3ef5 | 2012-12-05 00:38:44 +0000 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 614 | void DeclPrinter::VisitFieldDecl(FieldDecl *D) { |
Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 615 | if (!Policy.SuppressSpecifiers && D->isMutable()) |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 616 | Out << "mutable "; |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 617 | if (!Policy.SuppressSpecifiers && D->isModulePrivate()) |
| 618 | Out << "__module_private__ "; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 619 | |
Argyrios Kyrtzidis | a18347e | 2012-05-05 04:20:37 +0000 | [diff] [blame] | 620 | Out << D->getType().stream(Policy, D->getName()); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 621 | |
| 622 | if (D->isBitField()) { |
| 623 | Out << " : "; |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 624 | D->getBitWidth()->printPretty(Out, 0, Policy, Indentation); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 625 | } |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 626 | |
| 627 | Expr *Init = D->getInClassInitializer(); |
| 628 | if (!Policy.SuppressInitializers && Init) { |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 629 | if (D->getInClassInitStyle() == ICIS_ListInit) |
| 630 | Out << " "; |
| 631 | else |
| 632 | Out << " = "; |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 633 | Init->printPretty(Out, 0, Policy, Indentation); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 634 | } |
Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 635 | prettyPrintAttributes(D); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 638 | void DeclPrinter::VisitLabelDecl(LabelDecl *D) { |
Benjamin Kramer | db0fc51 | 2012-02-07 11:57:57 +0000 | [diff] [blame] | 639 | Out << *D << ":"; |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 643 | void DeclPrinter::VisitVarDecl(VarDecl *D) { |
Peter Collingbourne | 32fab90 | 2011-11-08 02:52:52 +0000 | [diff] [blame] | 644 | StorageClass SCAsWritten = D->getStorageClassAsWritten(); |
| 645 | if (!Policy.SuppressSpecifiers && SCAsWritten != SC_None) |
| 646 | Out << VarDecl::getStorageClassSpecifierString(SCAsWritten) << " "; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 647 | |
Eli Friedman | 7963584 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 648 | if (!Policy.SuppressSpecifiers && D->isThreadSpecified()) |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 649 | Out << "__thread "; |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 650 | if (!Policy.SuppressSpecifiers && D->isModulePrivate()) |
| 651 | Out << "__module_private__ "; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 652 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 653 | QualType T = D->getType(); |
John McCall | 856bbea | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 654 | if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 655 | T = Parm->getOriginalType(); |
Argyrios Kyrtzidis | a18347e | 2012-05-05 04:20:37 +0000 | [diff] [blame] | 656 | T.print(Out, Policy, D->getName()); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 657 | Expr *Init = D->getInit(); |
| 658 | if (!Policy.SuppressInitializers && Init) { |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 659 | bool ImplicitInit = false; |
Dmitri Gribenko | b614fab | 2013-02-03 23:02:47 +0000 | [diff] [blame] | 660 | if (CXXConstructExpr *Construct = |
| 661 | dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) { |
Dmitri Gribenko | 6835e37 | 2013-01-27 21:28:24 +0000 | [diff] [blame] | 662 | if (D->getInitStyle() == VarDecl::CallInit && |
| 663 | !Construct->isListInitialization()) { |
| 664 | ImplicitInit = Construct->getNumArgs() == 0 || |
| 665 | Construct->getArg(0)->isDefaultArgument(); |
| 666 | } |
| 667 | } |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 668 | if (!ImplicitInit) { |
Eli Friedman | 92125c4 | 2012-10-19 20:36:44 +0000 | [diff] [blame] | 669 | if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init)) |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 670 | Out << "("; |
| 671 | else if (D->getInitStyle() == VarDecl::CInit) { |
| 672 | Out << " = "; |
| 673 | } |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 674 | Init->printPretty(Out, 0, Policy, Indentation); |
Eli Friedman | 92125c4 | 2012-10-19 20:36:44 +0000 | [diff] [blame] | 675 | if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init)) |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 676 | Out << ")"; |
Ted Kremenek | 3586938 | 2010-09-17 23:04:38 +0000 | [diff] [blame] | 677 | } |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 678 | } |
Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 679 | prettyPrintAttributes(D); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) { |
| 683 | VisitVarDecl(D); |
| 684 | } |
| 685 | |
| 686 | void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { |
| 687 | Out << "__asm ("; |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 688 | D->getAsmString()->printPretty(Out, 0, Policy, Indentation); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 689 | Out << ")"; |
| 690 | } |
| 691 | |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 692 | void DeclPrinter::VisitImportDecl(ImportDecl *D) { |
Douglas Gregor | c50d492 | 2012-12-11 22:11:52 +0000 | [diff] [blame] | 693 | Out << "@import " << D->getImportedModule()->getFullModuleName() |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 694 | << ";\n"; |
| 695 | } |
| 696 | |
Peter Collingbourne | 7d8a0b5 | 2011-03-16 18:37:27 +0000 | [diff] [blame] | 697 | void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 698 | Out << "static_assert("; |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 699 | D->getAssertExpr()->printPretty(Out, 0, Policy, Indentation); |
Peter Collingbourne | 7d8a0b5 | 2011-03-16 18:37:27 +0000 | [diff] [blame] | 700 | Out << ", "; |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 701 | D->getMessage()->printPretty(Out, 0, Policy, Indentation); |
Peter Collingbourne | 7d8a0b5 | 2011-03-16 18:37:27 +0000 | [diff] [blame] | 702 | Out << ")"; |
| 703 | } |
| 704 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 705 | //---------------------------------------------------------------------------- |
| 706 | // C++ declarations |
| 707 | //---------------------------------------------------------------------------- |
Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 708 | void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) { |
Douglas Gregor | b526370 | 2012-05-01 01:43:38 +0000 | [diff] [blame] | 709 | if (D->isInline()) |
| 710 | Out << "inline "; |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 711 | Out << "namespace " << *D << " {\n"; |
Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 712 | VisitDeclContext(D); |
| 713 | Indent() << "}"; |
| 714 | } |
| 715 | |
Douglas Gregor | 3bc6e4c | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 716 | void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
| 717 | Out << "using namespace "; |
| 718 | if (D->getQualifier()) |
| 719 | D->getQualifier()->print(Out, Policy); |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 720 | Out << *D->getNominatedNamespaceAsWritten(); |
Douglas Gregor | 3bc6e4c | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Douglas Gregor | 1823193 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 723 | void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 724 | Out << "namespace " << *D << " = "; |
Douglas Gregor | 1823193 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 725 | if (D->getQualifier()) |
| 726 | D->getQualifier()->print(Out, Policy); |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 727 | Out << *D->getAliasedNamespace(); |
Douglas Gregor | 1823193 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 730 | void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) { |
| 731 | prettyPrintAttributes(D); |
Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 734 | void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) { |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 735 | if (!Policy.SuppressSpecifiers && D->isModulePrivate()) |
| 736 | Out << "__module_private__ "; |
Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 737 | Out << D->getKindName(); |
Benjamin Kramer | b11416d | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 738 | if (D->getIdentifier()) |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 739 | Out << ' ' << *D; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 740 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 741 | if (D->isCompleteDefinition()) { |
Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 742 | // Print the base classes |
| 743 | if (D->getNumBases()) { |
| 744 | Out << " : "; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(), |
| 746 | BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) { |
Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 747 | if (Base != D->bases_begin()) |
| 748 | Out << ", "; |
| 749 | |
| 750 | if (Base->isVirtual()) |
| 751 | Out << "virtual "; |
| 752 | |
Anders Carlsson | 6df9e07 | 2009-08-29 20:36:12 +0000 | [diff] [blame] | 753 | AccessSpecifier AS = Base->getAccessSpecifierAsWritten(); |
| 754 | if (AS != AS_none) |
| 755 | Print(AS); |
Anders Carlsson | 601d6e4 | 2009-08-28 22:39:52 +0000 | [diff] [blame] | 756 | Out << " " << Base->getType().getAsString(Policy); |
Douglas Gregor | 5fc8c9e | 2011-04-27 17:07:55 +0000 | [diff] [blame] | 757 | |
| 758 | if (Base->isPackExpansion()) |
| 759 | Out << "..."; |
Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
| 763 | // Print the class definition |
Douglas Gregor | 7a1a7cb | 2009-05-31 07:13:39 +0000 | [diff] [blame] | 764 | // FIXME: Doesn't print access specifiers, e.g., "public:" |
Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 765 | Out << " {\n"; |
| 766 | VisitDeclContext(D); |
| 767 | Indent() << "}"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 768 | } |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 772 | const char *l; |
| 773 | if (D->getLanguage() == LinkageSpecDecl::lang_c) |
| 774 | l = "C"; |
| 775 | else { |
| 776 | assert(D->getLanguage() == LinkageSpecDecl::lang_cxx && |
| 777 | "unknown language in linkage specification"); |
| 778 | l = "C++"; |
| 779 | } |
| 780 | |
| 781 | Out << "extern \"" << l << "\" "; |
| 782 | if (D->hasBraces()) { |
| 783 | Out << "{\n"; |
| 784 | VisitDeclContext(D); |
| 785 | Indent() << "}"; |
| 786 | } else |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 787 | Visit(*D->decls_begin()); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Enea Zaffanella | eb22c87 | 2013-01-31 09:54:08 +0000 | [diff] [blame] | 790 | void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params, |
| 791 | const TemplateArgumentList *Args) { |
Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 792 | assert(Params); |
| 793 | assert(!Args || Params->size() == Args->size()); |
| 794 | |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 795 | Out << "template <"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 796 | |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 797 | for (unsigned i = 0, e = Params->size(); i != e; ++i) { |
| 798 | if (i != 0) |
| 799 | Out << ", "; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 800 | |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 801 | const Decl *Param = Params->getParam(i); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 802 | if (const TemplateTypeParmDecl *TTP = |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 803 | dyn_cast<TemplateTypeParmDecl>(Param)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 804 | |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 805 | if (TTP->wasDeclaredWithTypename()) |
| 806 | Out << "typename "; |
| 807 | else |
| 808 | Out << "class "; |
| 809 | |
Anders Carlsson | fb1d776 | 2009-06-12 22:23:22 +0000 | [diff] [blame] | 810 | if (TTP->isParameterPack()) |
| 811 | Out << "... "; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 812 | |
Benjamin Kramer | db0fc51 | 2012-02-07 11:57:57 +0000 | [diff] [blame] | 813 | Out << *TTP; |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 814 | |
Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 815 | if (Args) { |
| 816 | Out << " = "; |
| 817 | Args->get(i).print(Policy, Out); |
| 818 | } else if (TTP->hasDefaultArgument()) { |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 819 | Out << " = "; |
| 820 | Out << TTP->getDefaultArgument().getAsString(Policy); |
| 821 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 822 | } else if (const NonTypeTemplateParmDecl *NTTP = |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 823 | dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 824 | Out << NTTP->getType().getAsString(Policy); |
| 825 | |
Douglas Gregor | eb5a39d | 2010-12-24 00:15:10 +0000 | [diff] [blame] | 826 | if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType())) |
| 827 | Out << "..."; |
| 828 | |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 829 | if (IdentifierInfo *Name = NTTP->getIdentifier()) { |
| 830 | Out << ' '; |
| 831 | Out << Name->getName(); |
| 832 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 833 | |
Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 834 | if (Args) { |
| 835 | Out << " = "; |
| 836 | Args->get(i).print(Policy, Out); |
| 837 | } else if (NTTP->hasDefaultArgument()) { |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 838 | Out << " = "; |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 839 | NTTP->getDefaultArgument()->printPretty(Out, 0, Policy, Indentation); |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 840 | } |
Richard Smith | 030f499 | 2011-04-15 13:38:57 +0000 | [diff] [blame] | 841 | } else if (const TemplateTemplateParmDecl *TTPD = |
| 842 | dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 843 | VisitTemplateDecl(TTPD); |
| 844 | // FIXME: print the default argument, if present. |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 845 | } |
| 846 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 847 | |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 848 | Out << "> "; |
Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) { |
| 852 | PrintTemplateParameters(D->getTemplateParameters()); |
Anders Carlsson | 40f8f8d | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 853 | |
Richard Smith | 030f499 | 2011-04-15 13:38:57 +0000 | [diff] [blame] | 854 | if (const TemplateTemplateParmDecl *TTP = |
| 855 | dyn_cast<TemplateTemplateParmDecl>(D)) { |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 856 | Out << "class "; |
| 857 | if (TTP->isParameterPack()) |
| 858 | Out << "..."; |
| 859 | Out << D->getName(); |
Craig Silverstein | 4a5d086 | 2010-07-09 20:25:10 +0000 | [diff] [blame] | 860 | } else { |
| 861 | Visit(D->getTemplatedDecl()); |
| 862 | } |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Richard Trieu | 82398f9 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 865 | void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 866 | if (PrintInstantiation) { |
| 867 | TemplateParameterList *Params = D->getTemplateParameters(); |
| 868 | for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end(); |
| 869 | I != E; ++I) { |
| 870 | PrintTemplateParameters(Params, (*I)->getTemplateSpecializationArgs()); |
| 871 | Visit(*I); |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | return VisitRedeclarableTemplateDecl(D); |
| 876 | } |
| 877 | |
| 878 | void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 879 | if (PrintInstantiation) { |
| 880 | TemplateParameterList *Params = D->getTemplateParameters(); |
| 881 | for (ClassTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end(); |
| 882 | I != E; ++I) { |
| 883 | PrintTemplateParameters(Params, &(*I)->getTemplateArgs()); |
| 884 | Visit(*I); |
| 885 | Out << '\n'; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | return VisitRedeclarableTemplateDecl(D); |
| 890 | } |
| 891 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 892 | //---------------------------------------------------------------------------- |
| 893 | // Objective-C declarations |
| 894 | //---------------------------------------------------------------------------- |
| 895 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 896 | void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) { |
| 897 | if (OMD->isInstanceMethod()) |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 898 | Out << "- "; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 899 | else |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 900 | Out << "+ "; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 901 | if (!OMD->getResultType().isNull()) |
Douglas Gregor | 5f478b7 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 902 | Out << '(' << OMD->getResultType().getAsString(Policy) << ")"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 903 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 904 | std::string name = OMD->getSelector().getAsString(); |
| 905 | std::string::size_type pos, lastPos = 0; |
| 906 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 907 | E = OMD->param_end(); PI != E; ++PI) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 908 | // FIXME: selector is missing here! |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 909 | pos = name.find_first_of(':', lastPos); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 910 | Out << " " << name.substr(lastPos, pos - lastPos); |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 911 | Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << **PI; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 912 | lastPos = pos + 1; |
| 913 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 914 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 915 | if (OMD->param_begin() == OMD->param_end()) |
| 916 | Out << " " << name; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 917 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 918 | if (OMD->isVariadic()) |
| 919 | Out << ", ..."; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 920 | |
Fariborz Jahanian | a7d76d2 | 2012-10-17 21:58:03 +0000 | [diff] [blame] | 921 | if (OMD->getBody() && !Policy.TerseOutput) { |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 922 | Out << ' '; |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 923 | OMD->getBody()->printPretty(Out, 0, Policy); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 924 | Out << '\n'; |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 925 | } |
Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 926 | else if (Policy.PolishForDeclaration) |
Fariborz Jahanian | 9b7ab87 | 2012-12-18 23:02:59 +0000 | [diff] [blame] | 927 | Out << ';'; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) { |
| 931 | std::string I = OID->getNameAsString(); |
| 932 | ObjCInterfaceDecl *SID = OID->getSuperClass(); |
| 933 | |
| 934 | if (SID) |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 935 | Out << "@implementation " << I << " : " << *SID; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 936 | else |
| 937 | Out << "@implementation " << I; |
Fariborz Jahanian | 95759ff | 2012-12-04 00:47:33 +0000 | [diff] [blame] | 938 | |
| 939 | if (OID->ivar_size() > 0) { |
| 940 | Out << "{\n"; |
| 941 | Indentation += Policy.Indentation; |
| 942 | for (ObjCImplementationDecl::ivar_iterator I = OID->ivar_begin(), |
| 943 | E = OID->ivar_end(); I != E; ++I) { |
| 944 | Indent() << I->getType().getAsString(Policy) << ' ' << **I << ";\n"; |
| 945 | } |
| 946 | Indentation -= Policy.Indentation; |
| 947 | Out << "}\n"; |
| 948 | } |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 949 | VisitDeclContext(OID, false); |
| 950 | Out << "@end"; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) { |
| 954 | std::string I = OID->getNameAsString(); |
| 955 | ObjCInterfaceDecl *SID = OID->getSuperClass(); |
| 956 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 957 | if (!OID->isThisDeclarationADefinition()) { |
| 958 | Out << "@class " << I << ";"; |
| 959 | return; |
| 960 | } |
Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 961 | bool eolnOut = false; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 962 | if (SID) |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 963 | Out << "@interface " << I << " : " << *SID; |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 964 | else |
| 965 | Out << "@interface " << I; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 966 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 967 | // Protocols? |
| 968 | const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols(); |
| 969 | if (!Protocols.empty()) { |
| 970 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 971 | E = Protocols.end(); I != E; ++I) |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 972 | Out << (I == Protocols.begin() ? '<' : ',') << **I; |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 973 | Out << "> "; |
Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 974 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 975 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 976 | if (OID->ivar_size() > 0) { |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 977 | Out << "{\n"; |
Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 978 | eolnOut = true; |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 979 | Indentation += Policy.Indentation; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 980 | for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
| 981 | E = OID->ivar_end(); I != E; ++I) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 982 | Indent() << I->getType().getAsString(Policy) << ' ' << **I << ";\n"; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 983 | } |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 984 | Indentation -= Policy.Indentation; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 985 | Out << "}\n"; |
| 986 | } |
Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 987 | else if (SID) { |
| 988 | Out << "\n"; |
| 989 | eolnOut = true; |
| 990 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 991 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 992 | VisitDeclContext(OID, false); |
Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 993 | if (!eolnOut) |
| 994 | Out << ' '; |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 995 | Out << "@end"; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 996 | // FIXME: implement the rest... |
| 997 | } |
| 998 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 999 | void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1000 | if (!PID->isThisDeclarationADefinition()) { |
Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 1001 | Out << "@protocol " << *PID << ";\n"; |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1002 | return; |
| 1003 | } |
Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 1004 | // Protocols? |
| 1005 | const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols(); |
| 1006 | if (!Protocols.empty()) { |
| 1007 | Out << "@protocol " << *PID; |
| 1008 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 1009 | E = Protocols.end(); I != E; ++I) |
| 1010 | Out << (I == Protocols.begin() ? '<' : ',') << **I; |
| 1011 | Out << ">\n"; |
| 1012 | } else |
| 1013 | Out << "@protocol " << *PID << '\n'; |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1014 | VisitDeclContext(PID, false); |
| 1015 | Out << "@end"; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1019 | Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n"; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1020 | |
| 1021 | VisitDeclContext(PID, false); |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1022 | Out << "@end"; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1023 | // FIXME: implement the rest... |
| 1024 | } |
| 1025 | |
| 1026 | void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) { |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1027 | Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n"; |
Fariborz Jahanian | 4cf177e | 2012-12-04 17:20:57 +0000 | [diff] [blame] | 1028 | if (PID->ivar_size() > 0) { |
| 1029 | Out << "{\n"; |
| 1030 | Indentation += Policy.Indentation; |
| 1031 | for (ObjCCategoryDecl::ivar_iterator I = PID->ivar_begin(), |
| 1032 | E = PID->ivar_end(); I != E; ++I) { |
| 1033 | Indent() << I->getType().getAsString(Policy) << ' ' << **I << ";\n"; |
| 1034 | } |
| 1035 | Indentation -= Policy.Indentation; |
| 1036 | Out << "}\n"; |
| 1037 | } |
| 1038 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1039 | VisitDeclContext(PID, false); |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1040 | Out << "@end"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1041 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1042 | // FIXME: implement the rest... |
| 1043 | } |
| 1044 | |
| 1045 | void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1046 | Out << "@compatibility_alias " << *AID |
| 1047 | << ' ' << *AID->getClassInterface() << ";\n"; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | /// PrintObjCPropertyDecl - print a property declaration. |
| 1051 | /// |
| 1052 | void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) { |
| 1053 | if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required) |
| 1054 | Out << "@required\n"; |
| 1055 | else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) |
| 1056 | Out << "@optional\n"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1057 | |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1058 | Out << "@property"; |
| 1059 | if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) { |
| 1060 | bool first = true; |
| 1061 | Out << " ("; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1062 | if (PDecl->getPropertyAttributes() & |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1063 | ObjCPropertyDecl::OBJC_PR_readonly) { |
| 1064 | Out << (first ? ' ' : ',') << "readonly"; |
| 1065 | first = false; |
Ted Kremenek | 897af91 | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 1066 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1067 | |
Ted Kremenek | 897af91 | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 1068 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 1069 | Out << (first ? ' ' : ',') << "getter = " |
| 1070 | << PDecl->getGetterName().getAsString(); |
| 1071 | first = false; |
| 1072 | } |
| 1073 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 1074 | Out << (first ? ' ' : ',') << "setter = " |
| 1075 | << PDecl->getSetterName().getAsString(); |
| 1076 | first = false; |
| 1077 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1078 | |
Ted Kremenek | 897af91 | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 1079 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) { |
| 1080 | Out << (first ? ' ' : ',') << "assign"; |
| 1081 | first = false; |
| 1082 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1083 | |
Ted Kremenek | 897af91 | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 1084 | if (PDecl->getPropertyAttributes() & |
| 1085 | ObjCPropertyDecl::OBJC_PR_readwrite) { |
| 1086 | Out << (first ? ' ' : ',') << "readwrite"; |
| 1087 | first = false; |
| 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_retain) { |
| 1091 | Out << (first ? ' ' : ',') << "retain"; |
| 1092 | first = false; |
| 1093 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1094 | |
Ted Kremenek | 897af91 | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 1095 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) { |
| 1096 | Out << (first ? ' ' : ',') << "strong"; |
| 1097 | first = false; |
| 1098 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1099 | |
Ted Kremenek | 897af91 | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 1100 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) { |
| 1101 | Out << (first ? ' ' : ',') << "copy"; |
| 1102 | first = false; |
| 1103 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1104 | |
Ted Kremenek | 897af91 | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 1105 | if (PDecl->getPropertyAttributes() & |
| 1106 | ObjCPropertyDecl::OBJC_PR_nonatomic) { |
| 1107 | Out << (first ? ' ' : ',') << "nonatomic"; |
| 1108 | first = false; |
| 1109 | } |
| 1110 | if (PDecl->getPropertyAttributes() & |
| 1111 | ObjCPropertyDecl::OBJC_PR_atomic) { |
| 1112 | Out << (first ? ' ' : ',') << "atomic"; |
| 1113 | first = false; |
| 1114 | } |
| 1115 | |
| 1116 | (void) first; // Silence dead store warning due to idiomatic code. |
| 1117 | Out << " )"; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1118 | } |
Fariborz Jahanian | 0389e52 | 2012-12-19 23:36:00 +0000 | [diff] [blame] | 1119 | Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << *PDecl; |
| 1120 | if (Policy.PolishForDeclaration) |
| 1121 | Out << ';'; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { |
| 1125 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1126 | Out << "@synthesize "; |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1127 | else |
Douglas Gregor | 36098ff | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1128 | Out << "@dynamic "; |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1129 | Out << *PID->getPropertyDecl(); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1130 | if (PID->getPropertyIvarDecl()) |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1131 | Out << '=' << *PID->getPropertyIvarDecl(); |
Douglas Gregor | 278f52e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1132 | } |
Anders Carlsson | df5a1c8 | 2009-08-28 19:16:39 +0000 | [diff] [blame] | 1133 | |
| 1134 | void DeclPrinter::VisitUsingDecl(UsingDecl *D) { |
| 1135 | Out << "using "; |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1136 | D->getQualifier()->print(Out, Policy); |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1137 | Out << *D; |
Anders Carlsson | df5a1c8 | 2009-08-28 19:16:39 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1140 | void |
| 1141 | DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) { |
| 1142 | Out << "using typename "; |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1143 | D->getQualifier()->print(Out, Policy); |
Benjamin Kramer | b11416d | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 1144 | Out << D->getDeclName(); |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
Anders Carlsson | df5a1c8 | 2009-08-28 19:16:39 +0000 | [diff] [blame] | 1148 | Out << "using "; |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1149 | D->getQualifier()->print(Out, Policy); |
Fariborz Jahanian | 3ec3921 | 2012-12-06 00:09:40 +0000 | [diff] [blame] | 1150 | Out << D->getName(); |
Anders Carlsson | df5a1c8 | 2009-08-28 19:16:39 +0000 | [diff] [blame] | 1151 | } |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 1152 | |
| 1153 | void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) { |
| 1154 | // ignore |
| 1155 | } |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame^] | 1156 | |
| 1157 | void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) { |
| 1158 | Out << "#pragma omp threadprivate"; |
| 1159 | if (!D->varlist_empty()) { |
| 1160 | for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(), |
| 1161 | E = D->varlist_end(); |
| 1162 | I != E; ++I) { |
| 1163 | Out << (I == D->varlist_begin() ? '(' : ',') |
| 1164 | << *cast<NamedDecl>((*I)->getDecl()); |
| 1165 | } |
| 1166 | Out << ")"; |
| 1167 | } |
| 1168 | } |
| 1169 | |