1 1.1 joerg //===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===// 2 1.1 joerg // 3 1.1 joerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 1.1 joerg // See https://llvm.org/LICENSE.txt for license information. 5 1.1 joerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 1.1 joerg // 7 1.1 joerg //===----------------------------------------------------------------------===// 8 1.1 joerg // 9 1.1 joerg // This file implements the Decl::print method, which pretty prints the 10 1.1 joerg // AST back out to C/Objective-C/C++/Objective-C++ code. 11 1.1 joerg // 12 1.1 joerg //===----------------------------------------------------------------------===// 13 1.1 joerg #include "clang/AST/ASTContext.h" 14 1.1 joerg #include "clang/AST/Attr.h" 15 1.1 joerg #include "clang/AST/Decl.h" 16 1.1 joerg #include "clang/AST/DeclCXX.h" 17 1.1 joerg #include "clang/AST/DeclObjC.h" 18 1.1 joerg #include "clang/AST/DeclTemplate.h" 19 1.1 joerg #include "clang/AST/DeclVisitor.h" 20 1.1 joerg #include "clang/AST/Expr.h" 21 1.1 joerg #include "clang/AST/ExprCXX.h" 22 1.1 joerg #include "clang/AST/PrettyPrinter.h" 23 1.1 joerg #include "clang/Basic/Module.h" 24 1.1 joerg #include "llvm/Support/raw_ostream.h" 25 1.1 joerg using namespace clang; 26 1.1 joerg 27 1.1 joerg namespace { 28 1.1 joerg class DeclPrinter : public DeclVisitor<DeclPrinter> { 29 1.1 joerg raw_ostream &Out; 30 1.1 joerg PrintingPolicy Policy; 31 1.1 joerg const ASTContext &Context; 32 1.1 joerg unsigned Indentation; 33 1.1 joerg bool PrintInstantiation; 34 1.1 joerg 35 1.1 joerg raw_ostream& Indent() { return Indent(Indentation); } 36 1.1 joerg raw_ostream& Indent(unsigned Indentation); 37 1.1 joerg void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls); 38 1.1 joerg 39 1.1 joerg void Print(AccessSpecifier AS); 40 1.1 joerg void PrintConstructorInitializers(CXXConstructorDecl *CDecl, 41 1.1 joerg std::string &Proto); 42 1.1 joerg 43 1.1 joerg /// Print an Objective-C method type in parentheses. 44 1.1 joerg /// 45 1.1 joerg /// \param Quals The Objective-C declaration qualifiers. 46 1.1 joerg /// \param T The type to print. 47 1.1 joerg void PrintObjCMethodType(ASTContext &Ctx, Decl::ObjCDeclQualifier Quals, 48 1.1 joerg QualType T); 49 1.1 joerg 50 1.1 joerg void PrintObjCTypeParams(ObjCTypeParamList *Params); 51 1.1 joerg 52 1.1 joerg public: 53 1.1 joerg DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy, 54 1.1 joerg const ASTContext &Context, unsigned Indentation = 0, 55 1.1 joerg bool PrintInstantiation = false) 56 1.1 joerg : Out(Out), Policy(Policy), Context(Context), Indentation(Indentation), 57 1.1 joerg PrintInstantiation(PrintInstantiation) {} 58 1.1 joerg 59 1.1 joerg void VisitDeclContext(DeclContext *DC, bool Indent = true); 60 1.1 joerg 61 1.1 joerg void VisitTranslationUnitDecl(TranslationUnitDecl *D); 62 1.1 joerg void VisitTypedefDecl(TypedefDecl *D); 63 1.1 joerg void VisitTypeAliasDecl(TypeAliasDecl *D); 64 1.1 joerg void VisitEnumDecl(EnumDecl *D); 65 1.1 joerg void VisitRecordDecl(RecordDecl *D); 66 1.1 joerg void VisitEnumConstantDecl(EnumConstantDecl *D); 67 1.1 joerg void VisitEmptyDecl(EmptyDecl *D); 68 1.1 joerg void VisitFunctionDecl(FunctionDecl *D); 69 1.1 joerg void VisitFriendDecl(FriendDecl *D); 70 1.1 joerg void VisitFieldDecl(FieldDecl *D); 71 1.1 joerg void VisitVarDecl(VarDecl *D); 72 1.1 joerg void VisitLabelDecl(LabelDecl *D); 73 1.1 joerg void VisitParmVarDecl(ParmVarDecl *D); 74 1.1 joerg void VisitFileScopeAsmDecl(FileScopeAsmDecl *D); 75 1.1 joerg void VisitImportDecl(ImportDecl *D); 76 1.1 joerg void VisitStaticAssertDecl(StaticAssertDecl *D); 77 1.1 joerg void VisitNamespaceDecl(NamespaceDecl *D); 78 1.1 joerg void VisitUsingDirectiveDecl(UsingDirectiveDecl *D); 79 1.1 joerg void VisitNamespaceAliasDecl(NamespaceAliasDecl *D); 80 1.1 joerg void VisitCXXRecordDecl(CXXRecordDecl *D); 81 1.1 joerg void VisitLinkageSpecDecl(LinkageSpecDecl *D); 82 1.1 joerg void VisitTemplateDecl(const TemplateDecl *D); 83 1.1 joerg void VisitFunctionTemplateDecl(FunctionTemplateDecl *D); 84 1.1 joerg void VisitClassTemplateDecl(ClassTemplateDecl *D); 85 1.1 joerg void VisitClassTemplateSpecializationDecl( 86 1.1 joerg ClassTemplateSpecializationDecl *D); 87 1.1 joerg void VisitClassTemplatePartialSpecializationDecl( 88 1.1 joerg ClassTemplatePartialSpecializationDecl *D); 89 1.1 joerg void VisitObjCMethodDecl(ObjCMethodDecl *D); 90 1.1 joerg void VisitObjCImplementationDecl(ObjCImplementationDecl *D); 91 1.1 joerg void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); 92 1.1 joerg void VisitObjCProtocolDecl(ObjCProtocolDecl *D); 93 1.1 joerg void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); 94 1.1 joerg void VisitObjCCategoryDecl(ObjCCategoryDecl *D); 95 1.1 joerg void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D); 96 1.1 joerg void VisitObjCPropertyDecl(ObjCPropertyDecl *D); 97 1.1 joerg void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); 98 1.1 joerg void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); 99 1.1 joerg void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); 100 1.1 joerg void VisitUsingDecl(UsingDecl *D); 101 1.1 joerg void VisitUsingShadowDecl(UsingShadowDecl *D); 102 1.1 joerg void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D); 103 1.1 joerg void VisitOMPAllocateDecl(OMPAllocateDecl *D); 104 1.1 joerg void VisitOMPRequiresDecl(OMPRequiresDecl *D); 105 1.1 joerg void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D); 106 1.1 joerg void VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D); 107 1.1 joerg void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D); 108 1.1.1.2 joerg void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *TTP); 109 1.1.1.2 joerg void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *NTTP); 110 1.1 joerg 111 1.1 joerg void printTemplateParameters(const TemplateParameterList *Params, 112 1.1 joerg bool OmitTemplateKW = false); 113 1.1.1.2 joerg void printTemplateArguments(llvm::ArrayRef<TemplateArgument> Args, 114 1.1.1.2 joerg const TemplateParameterList *Params, 115 1.1.1.2 joerg bool TemplOverloaded); 116 1.1.1.2 joerg void printTemplateArguments(llvm::ArrayRef<TemplateArgumentLoc> Args, 117 1.1.1.2 joerg const TemplateParameterList *Params, 118 1.1.1.2 joerg bool TemplOverloaded); 119 1.1 joerg void prettyPrintAttributes(Decl *D); 120 1.1 joerg void prettyPrintPragmas(Decl *D); 121 1.1 joerg void printDeclType(QualType T, StringRef DeclName, bool Pack = false); 122 1.1 joerg }; 123 1.1 joerg } 124 1.1 joerg 125 1.1 joerg void Decl::print(raw_ostream &Out, unsigned Indentation, 126 1.1 joerg bool PrintInstantiation) const { 127 1.1 joerg print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation); 128 1.1 joerg } 129 1.1 joerg 130 1.1 joerg void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy, 131 1.1 joerg unsigned Indentation, bool PrintInstantiation) const { 132 1.1 joerg DeclPrinter Printer(Out, Policy, getASTContext(), Indentation, 133 1.1 joerg PrintInstantiation); 134 1.1 joerg Printer.Visit(const_cast<Decl*>(this)); 135 1.1 joerg } 136 1.1 joerg 137 1.1 joerg void TemplateParameterList::print(raw_ostream &Out, const ASTContext &Context, 138 1.1 joerg bool OmitTemplateKW) const { 139 1.1 joerg print(Out, Context, Context.getPrintingPolicy(), OmitTemplateKW); 140 1.1 joerg } 141 1.1 joerg 142 1.1 joerg void TemplateParameterList::print(raw_ostream &Out, const ASTContext &Context, 143 1.1 joerg const PrintingPolicy &Policy, 144 1.1 joerg bool OmitTemplateKW) const { 145 1.1 joerg DeclPrinter Printer(Out, Policy, Context); 146 1.1 joerg Printer.printTemplateParameters(this, OmitTemplateKW); 147 1.1 joerg } 148 1.1 joerg 149 1.1 joerg static QualType GetBaseType(QualType T) { 150 1.1 joerg // FIXME: This should be on the Type class! 151 1.1 joerg QualType BaseType = T; 152 1.1 joerg while (!BaseType->isSpecifierType()) { 153 1.1 joerg if (const PointerType *PTy = BaseType->getAs<PointerType>()) 154 1.1 joerg BaseType = PTy->getPointeeType(); 155 1.1 joerg else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>()) 156 1.1 joerg BaseType = BPy->getPointeeType(); 157 1.1 joerg else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType)) 158 1.1 joerg BaseType = ATy->getElementType(); 159 1.1 joerg else if (const FunctionType* FTy = BaseType->getAs<FunctionType>()) 160 1.1 joerg BaseType = FTy->getReturnType(); 161 1.1 joerg else if (const VectorType *VTy = BaseType->getAs<VectorType>()) 162 1.1 joerg BaseType = VTy->getElementType(); 163 1.1 joerg else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>()) 164 1.1 joerg BaseType = RTy->getPointeeType(); 165 1.1 joerg else if (const AutoType *ATy = BaseType->getAs<AutoType>()) 166 1.1 joerg BaseType = ATy->getDeducedType(); 167 1.1 joerg else if (const ParenType *PTy = BaseType->getAs<ParenType>()) 168 1.1 joerg BaseType = PTy->desugar(); 169 1.1 joerg else 170 1.1 joerg // This must be a syntax error. 171 1.1 joerg break; 172 1.1 joerg } 173 1.1 joerg return BaseType; 174 1.1 joerg } 175 1.1 joerg 176 1.1 joerg static QualType getDeclType(Decl* D) { 177 1.1 joerg if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D)) 178 1.1 joerg return TDD->getUnderlyingType(); 179 1.1 joerg if (ValueDecl* VD = dyn_cast<ValueDecl>(D)) 180 1.1 joerg return VD->getType(); 181 1.1 joerg return QualType(); 182 1.1 joerg } 183 1.1 joerg 184 1.1 joerg void Decl::printGroup(Decl** Begin, unsigned NumDecls, 185 1.1 joerg raw_ostream &Out, const PrintingPolicy &Policy, 186 1.1 joerg unsigned Indentation) { 187 1.1 joerg if (NumDecls == 1) { 188 1.1 joerg (*Begin)->print(Out, Policy, Indentation); 189 1.1 joerg return; 190 1.1 joerg } 191 1.1 joerg 192 1.1 joerg Decl** End = Begin + NumDecls; 193 1.1 joerg TagDecl* TD = dyn_cast<TagDecl>(*Begin); 194 1.1 joerg if (TD) 195 1.1 joerg ++Begin; 196 1.1 joerg 197 1.1 joerg PrintingPolicy SubPolicy(Policy); 198 1.1 joerg 199 1.1 joerg bool isFirst = true; 200 1.1 joerg for ( ; Begin != End; ++Begin) { 201 1.1 joerg if (isFirst) { 202 1.1 joerg if(TD) 203 1.1 joerg SubPolicy.IncludeTagDefinition = true; 204 1.1 joerg SubPolicy.SuppressSpecifiers = false; 205 1.1 joerg isFirst = false; 206 1.1 joerg } else { 207 1.1 joerg if (!isFirst) Out << ", "; 208 1.1 joerg SubPolicy.IncludeTagDefinition = false; 209 1.1 joerg SubPolicy.SuppressSpecifiers = true; 210 1.1 joerg } 211 1.1 joerg 212 1.1 joerg (*Begin)->print(Out, SubPolicy, Indentation); 213 1.1 joerg } 214 1.1 joerg } 215 1.1 joerg 216 1.1 joerg LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const { 217 1.1 joerg // Get the translation unit 218 1.1 joerg const DeclContext *DC = this; 219 1.1 joerg while (!DC->isTranslationUnit()) 220 1.1 joerg DC = DC->getParent(); 221 1.1 joerg 222 1.1 joerg ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext(); 223 1.1 joerg DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), Ctx, 0); 224 1.1 joerg Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false); 225 1.1 joerg } 226 1.1 joerg 227 1.1 joerg raw_ostream& DeclPrinter::Indent(unsigned Indentation) { 228 1.1 joerg for (unsigned i = 0; i != Indentation; ++i) 229 1.1 joerg Out << " "; 230 1.1 joerg return Out; 231 1.1 joerg } 232 1.1 joerg 233 1.1 joerg void DeclPrinter::prettyPrintAttributes(Decl *D) { 234 1.1 joerg if (Policy.PolishForDeclaration) 235 1.1 joerg return; 236 1.1 joerg 237 1.1 joerg if (D->hasAttrs()) { 238 1.1 joerg AttrVec &Attrs = D->getAttrs(); 239 1.1 joerg for (auto *A : Attrs) { 240 1.1 joerg if (A->isInherited() || A->isImplicit()) 241 1.1 joerg continue; 242 1.1 joerg switch (A->getKind()) { 243 1.1 joerg #define ATTR(X) 244 1.1 joerg #define PRAGMA_SPELLING_ATTR(X) case attr::X: 245 1.1 joerg #include "clang/Basic/AttrList.inc" 246 1.1 joerg break; 247 1.1 joerg default: 248 1.1 joerg A->printPretty(Out, Policy); 249 1.1 joerg break; 250 1.1 joerg } 251 1.1 joerg } 252 1.1 joerg } 253 1.1 joerg } 254 1.1 joerg 255 1.1 joerg void DeclPrinter::prettyPrintPragmas(Decl *D) { 256 1.1 joerg if (Policy.PolishForDeclaration) 257 1.1 joerg return; 258 1.1 joerg 259 1.1 joerg if (D->hasAttrs()) { 260 1.1 joerg AttrVec &Attrs = D->getAttrs(); 261 1.1 joerg for (auto *A : Attrs) { 262 1.1 joerg switch (A->getKind()) { 263 1.1 joerg #define ATTR(X) 264 1.1 joerg #define PRAGMA_SPELLING_ATTR(X) case attr::X: 265 1.1 joerg #include "clang/Basic/AttrList.inc" 266 1.1 joerg A->printPretty(Out, Policy); 267 1.1 joerg Indent(); 268 1.1 joerg break; 269 1.1 joerg default: 270 1.1 joerg break; 271 1.1 joerg } 272 1.1 joerg } 273 1.1 joerg } 274 1.1 joerg } 275 1.1 joerg 276 1.1 joerg void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) { 277 1.1 joerg // Normally, a PackExpansionType is written as T[3]... (for instance, as a 278 1.1 joerg // template argument), but if it is the type of a declaration, the ellipsis 279 1.1 joerg // is placed before the name being declared. 280 1.1 joerg if (auto *PET = T->getAs<PackExpansionType>()) { 281 1.1 joerg Pack = true; 282 1.1 joerg T = PET->getPattern(); 283 1.1 joerg } 284 1.1 joerg T.print(Out, Policy, (Pack ? "..." : "") + DeclName, Indentation); 285 1.1 joerg } 286 1.1 joerg 287 1.1 joerg void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) { 288 1.1 joerg this->Indent(); 289 1.1 joerg Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation); 290 1.1 joerg Out << ";\n"; 291 1.1 joerg Decls.clear(); 292 1.1 joerg 293 1.1 joerg } 294 1.1 joerg 295 1.1 joerg void DeclPrinter::Print(AccessSpecifier AS) { 296 1.1.1.2 joerg const auto AccessSpelling = getAccessSpelling(AS); 297 1.1.1.2 joerg if (AccessSpelling.empty()) 298 1.1.1.2 joerg llvm_unreachable("No access specifier!"); 299 1.1.1.2 joerg Out << AccessSpelling; 300 1.1 joerg } 301 1.1 joerg 302 1.1 joerg void DeclPrinter::PrintConstructorInitializers(CXXConstructorDecl *CDecl, 303 1.1 joerg std::string &Proto) { 304 1.1 joerg bool HasInitializerList = false; 305 1.1 joerg for (const auto *BMInitializer : CDecl->inits()) { 306 1.1 joerg if (BMInitializer->isInClassMemberInitializer()) 307 1.1 joerg continue; 308 1.1 joerg 309 1.1 joerg if (!HasInitializerList) { 310 1.1 joerg Proto += " : "; 311 1.1 joerg Out << Proto; 312 1.1 joerg Proto.clear(); 313 1.1 joerg HasInitializerList = true; 314 1.1 joerg } else 315 1.1 joerg Out << ", "; 316 1.1 joerg 317 1.1 joerg if (BMInitializer->isAnyMemberInitializer()) { 318 1.1 joerg FieldDecl *FD = BMInitializer->getAnyMember(); 319 1.1 joerg Out << *FD; 320 1.1 joerg } else { 321 1.1 joerg Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy); 322 1.1 joerg } 323 1.1 joerg 324 1.1 joerg Out << "("; 325 1.1 joerg if (!BMInitializer->getInit()) { 326 1.1 joerg // Nothing to print 327 1.1 joerg } else { 328 1.1 joerg Expr *Init = BMInitializer->getInit(); 329 1.1 joerg if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init)) 330 1.1 joerg Init = Tmp->getSubExpr(); 331 1.1 joerg 332 1.1 joerg Init = Init->IgnoreParens(); 333 1.1 joerg 334 1.1 joerg Expr *SimpleInit = nullptr; 335 1.1 joerg Expr **Args = nullptr; 336 1.1 joerg unsigned NumArgs = 0; 337 1.1 joerg if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { 338 1.1 joerg Args = ParenList->getExprs(); 339 1.1 joerg NumArgs = ParenList->getNumExprs(); 340 1.1 joerg } else if (CXXConstructExpr *Construct = 341 1.1 joerg dyn_cast<CXXConstructExpr>(Init)) { 342 1.1 joerg Args = Construct->getArgs(); 343 1.1 joerg NumArgs = Construct->getNumArgs(); 344 1.1 joerg } else 345 1.1 joerg SimpleInit = Init; 346 1.1 joerg 347 1.1 joerg if (SimpleInit) 348 1.1.1.2 joerg SimpleInit->printPretty(Out, nullptr, Policy, Indentation, "\n", 349 1.1.1.2 joerg &Context); 350 1.1 joerg else { 351 1.1 joerg for (unsigned I = 0; I != NumArgs; ++I) { 352 1.1 joerg assert(Args[I] != nullptr && "Expected non-null Expr"); 353 1.1 joerg if (isa<CXXDefaultArgExpr>(Args[I])) 354 1.1 joerg break; 355 1.1 joerg 356 1.1 joerg if (I) 357 1.1 joerg Out << ", "; 358 1.1.1.2 joerg Args[I]->printPretty(Out, nullptr, Policy, Indentation, "\n", 359 1.1.1.2 joerg &Context); 360 1.1 joerg } 361 1.1 joerg } 362 1.1 joerg } 363 1.1 joerg Out << ")"; 364 1.1 joerg if (BMInitializer->isPackExpansion()) 365 1.1 joerg Out << "..."; 366 1.1 joerg } 367 1.1 joerg } 368 1.1 joerg 369 1.1 joerg //---------------------------------------------------------------------------- 370 1.1 joerg // Common C declarations 371 1.1 joerg //---------------------------------------------------------------------------- 372 1.1 joerg 373 1.1 joerg void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) { 374 1.1 joerg if (Policy.TerseOutput) 375 1.1 joerg return; 376 1.1 joerg 377 1.1 joerg if (Indent) 378 1.1 joerg Indentation += Policy.Indentation; 379 1.1 joerg 380 1.1 joerg SmallVector<Decl*, 2> Decls; 381 1.1 joerg for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end(); 382 1.1 joerg D != DEnd; ++D) { 383 1.1 joerg 384 1.1 joerg // Don't print ObjCIvarDecls, as they are printed when visiting the 385 1.1 joerg // containing ObjCInterfaceDecl. 386 1.1 joerg if (isa<ObjCIvarDecl>(*D)) 387 1.1 joerg continue; 388 1.1 joerg 389 1.1 joerg // Skip over implicit declarations in pretty-printing mode. 390 1.1 joerg if (D->isImplicit()) 391 1.1 joerg continue; 392 1.1 joerg 393 1.1 joerg // Don't print implicit specializations, as they are printed when visiting 394 1.1 joerg // corresponding templates. 395 1.1 joerg if (auto FD = dyn_cast<FunctionDecl>(*D)) 396 1.1 joerg if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation && 397 1.1 joerg !isa<ClassTemplateSpecializationDecl>(DC)) 398 1.1 joerg continue; 399 1.1 joerg 400 1.1 joerg // The next bits of code handle stuff like "struct {int x;} a,b"; we're 401 1.1 joerg // forced to merge the declarations because there's no other way to 402 1.1 joerg // refer to the struct in question. When that struct is named instead, we 403 1.1 joerg // also need to merge to avoid splitting off a stand-alone struct 404 1.1 joerg // declaration that produces the warning ext_no_declarators in some 405 1.1 joerg // contexts. 406 1.1 joerg // 407 1.1 joerg // This limited merging is safe without a bunch of other checks because it 408 1.1 joerg // only merges declarations directly referring to the tag, not typedefs. 409 1.1 joerg // 410 1.1 joerg // Check whether the current declaration should be grouped with a previous 411 1.1 joerg // non-free-standing tag declaration. 412 1.1 joerg QualType CurDeclType = getDeclType(*D); 413 1.1 joerg if (!Decls.empty() && !CurDeclType.isNull()) { 414 1.1 joerg QualType BaseType = GetBaseType(CurDeclType); 415 1.1 joerg if (!BaseType.isNull() && isa<ElaboratedType>(BaseType) && 416 1.1 joerg cast<ElaboratedType>(BaseType)->getOwnedTagDecl() == Decls[0]) { 417 1.1 joerg Decls.push_back(*D); 418 1.1 joerg continue; 419 1.1 joerg } 420 1.1 joerg } 421 1.1 joerg 422 1.1 joerg // If we have a merged group waiting to be handled, handle it now. 423 1.1 joerg if (!Decls.empty()) 424 1.1 joerg ProcessDeclGroup(Decls); 425 1.1 joerg 426 1.1 joerg // If the current declaration is not a free standing declaration, save it 427 1.1 joerg // so we can merge it with the subsequent declaration(s) using it. 428 1.1 joerg if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->isFreeStanding()) { 429 1.1 joerg Decls.push_back(*D); 430 1.1 joerg continue; 431 1.1 joerg } 432 1.1 joerg 433 1.1 joerg if (isa<AccessSpecDecl>(*D)) { 434 1.1 joerg Indentation -= Policy.Indentation; 435 1.1 joerg this->Indent(); 436 1.1 joerg Print(D->getAccess()); 437 1.1 joerg Out << ":\n"; 438 1.1 joerg Indentation += Policy.Indentation; 439 1.1 joerg continue; 440 1.1 joerg } 441 1.1 joerg 442 1.1 joerg this->Indent(); 443 1.1 joerg Visit(*D); 444 1.1 joerg 445 1.1 joerg // FIXME: Need to be able to tell the DeclPrinter when 446 1.1 joerg const char *Terminator = nullptr; 447 1.1 joerg if (isa<OMPThreadPrivateDecl>(*D) || isa<OMPDeclareReductionDecl>(*D) || 448 1.1 joerg isa<OMPDeclareMapperDecl>(*D) || isa<OMPRequiresDecl>(*D) || 449 1.1 joerg isa<OMPAllocateDecl>(*D)) 450 1.1 joerg Terminator = nullptr; 451 1.1 joerg else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->hasBody()) 452 1.1 joerg Terminator = nullptr; 453 1.1 joerg else if (auto FD = dyn_cast<FunctionDecl>(*D)) { 454 1.1 joerg if (FD->isThisDeclarationADefinition()) 455 1.1 joerg Terminator = nullptr; 456 1.1 joerg else 457 1.1 joerg Terminator = ";"; 458 1.1 joerg } else if (auto TD = dyn_cast<FunctionTemplateDecl>(*D)) { 459 1.1 joerg if (TD->getTemplatedDecl()->isThisDeclarationADefinition()) 460 1.1 joerg Terminator = nullptr; 461 1.1 joerg else 462 1.1 joerg Terminator = ";"; 463 1.1 joerg } else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) || 464 1.1 joerg isa<ObjCImplementationDecl>(*D) || 465 1.1 joerg isa<ObjCInterfaceDecl>(*D) || 466 1.1 joerg isa<ObjCProtocolDecl>(*D) || 467 1.1 joerg isa<ObjCCategoryImplDecl>(*D) || 468 1.1 joerg isa<ObjCCategoryDecl>(*D)) 469 1.1 joerg Terminator = nullptr; 470 1.1 joerg else if (isa<EnumConstantDecl>(*D)) { 471 1.1 joerg DeclContext::decl_iterator Next = D; 472 1.1 joerg ++Next; 473 1.1 joerg if (Next != DEnd) 474 1.1 joerg Terminator = ","; 475 1.1 joerg } else 476 1.1 joerg Terminator = ";"; 477 1.1 joerg 478 1.1 joerg if (Terminator) 479 1.1 joerg Out << Terminator; 480 1.1 joerg if (!Policy.TerseOutput && 481 1.1 joerg ((isa<FunctionDecl>(*D) && 482 1.1 joerg cast<FunctionDecl>(*D)->doesThisDeclarationHaveABody()) || 483 1.1 joerg (isa<FunctionTemplateDecl>(*D) && 484 1.1 joerg cast<FunctionTemplateDecl>(*D)->getTemplatedDecl()->doesThisDeclarationHaveABody()))) 485 1.1 joerg ; // StmtPrinter already added '\n' after CompoundStmt. 486 1.1 joerg else 487 1.1 joerg Out << "\n"; 488 1.1 joerg 489 1.1 joerg // Declare target attribute is special one, natural spelling for the pragma 490 1.1 joerg // assumes "ending" construct so print it here. 491 1.1 joerg if (D->hasAttr<OMPDeclareTargetDeclAttr>()) 492 1.1 joerg Out << "#pragma omp end declare target\n"; 493 1.1 joerg } 494 1.1 joerg 495 1.1 joerg if (!Decls.empty()) 496 1.1 joerg ProcessDeclGroup(Decls); 497 1.1 joerg 498 1.1 joerg if (Indent) 499 1.1 joerg Indentation -= Policy.Indentation; 500 1.1 joerg } 501 1.1 joerg 502 1.1 joerg void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { 503 1.1 joerg VisitDeclContext(D, false); 504 1.1 joerg } 505 1.1 joerg 506 1.1 joerg void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) { 507 1.1 joerg if (!Policy.SuppressSpecifiers) { 508 1.1 joerg Out << "typedef "; 509 1.1 joerg 510 1.1 joerg if (D->isModulePrivate()) 511 1.1 joerg Out << "__module_private__ "; 512 1.1 joerg } 513 1.1 joerg QualType Ty = D->getTypeSourceInfo()->getType(); 514 1.1 joerg Ty.print(Out, Policy, D->getName(), Indentation); 515 1.1 joerg prettyPrintAttributes(D); 516 1.1 joerg } 517 1.1 joerg 518 1.1 joerg void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) { 519 1.1 joerg Out << "using " << *D; 520 1.1 joerg prettyPrintAttributes(D); 521 1.1 joerg Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy); 522 1.1 joerg } 523 1.1 joerg 524 1.1 joerg void DeclPrinter::VisitEnumDecl(EnumDecl *D) { 525 1.1 joerg if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 526 1.1 joerg Out << "__module_private__ "; 527 1.1 joerg Out << "enum"; 528 1.1 joerg if (D->isScoped()) { 529 1.1 joerg if (D->isScopedUsingClassTag()) 530 1.1 joerg Out << " class"; 531 1.1 joerg else 532 1.1 joerg Out << " struct"; 533 1.1 joerg } 534 1.1 joerg 535 1.1 joerg prettyPrintAttributes(D); 536 1.1 joerg 537 1.1.1.2 joerg if (D->getDeclName()) 538 1.1.1.2 joerg Out << ' ' << D->getDeclName(); 539 1.1 joerg 540 1.1.1.2 joerg if (D->isFixed()) 541 1.1 joerg Out << " : " << D->getIntegerType().stream(Policy); 542 1.1 joerg 543 1.1 joerg if (D->isCompleteDefinition()) { 544 1.1 joerg Out << " {\n"; 545 1.1 joerg VisitDeclContext(D); 546 1.1 joerg Indent() << "}"; 547 1.1 joerg } 548 1.1 joerg } 549 1.1 joerg 550 1.1 joerg void DeclPrinter::VisitRecordDecl(RecordDecl *D) { 551 1.1 joerg if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 552 1.1 joerg Out << "__module_private__ "; 553 1.1 joerg Out << D->getKindName(); 554 1.1 joerg 555 1.1 joerg prettyPrintAttributes(D); 556 1.1 joerg 557 1.1 joerg if (D->getIdentifier()) 558 1.1 joerg Out << ' ' << *D; 559 1.1 joerg 560 1.1 joerg if (D->isCompleteDefinition()) { 561 1.1 joerg Out << " {\n"; 562 1.1 joerg VisitDeclContext(D); 563 1.1 joerg Indent() << "}"; 564 1.1 joerg } 565 1.1 joerg } 566 1.1 joerg 567 1.1 joerg void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) { 568 1.1 joerg Out << *D; 569 1.1 joerg prettyPrintAttributes(D); 570 1.1 joerg if (Expr *Init = D->getInitExpr()) { 571 1.1 joerg Out << " = "; 572 1.1 joerg Init->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context); 573 1.1 joerg } 574 1.1 joerg } 575 1.1 joerg 576 1.1 joerg static void printExplicitSpecifier(ExplicitSpecifier ES, llvm::raw_ostream &Out, 577 1.1.1.2 joerg PrintingPolicy &Policy, unsigned Indentation, 578 1.1.1.2 joerg const ASTContext &Context) { 579 1.1 joerg std::string Proto = "explicit"; 580 1.1 joerg llvm::raw_string_ostream EOut(Proto); 581 1.1 joerg if (ES.getExpr()) { 582 1.1 joerg EOut << "("; 583 1.1.1.2 joerg ES.getExpr()->printPretty(EOut, nullptr, Policy, Indentation, "\n", 584 1.1.1.2 joerg &Context); 585 1.1 joerg EOut << ")"; 586 1.1 joerg } 587 1.1 joerg EOut << " "; 588 1.1 joerg EOut.flush(); 589 1.1 joerg Out << EOut.str(); 590 1.1 joerg } 591 1.1 joerg 592 1.1 joerg void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { 593 1.1 joerg if (!D->getDescribedFunctionTemplate() && 594 1.1 joerg !D->isFunctionTemplateSpecialization()) 595 1.1 joerg prettyPrintPragmas(D); 596 1.1 joerg 597 1.1 joerg if (D->isFunctionTemplateSpecialization()) 598 1.1 joerg Out << "template<> "; 599 1.1 joerg else if (!D->getDescribedFunctionTemplate()) { 600 1.1 joerg for (unsigned I = 0, NumTemplateParams = D->getNumTemplateParameterLists(); 601 1.1 joerg I < NumTemplateParams; ++I) 602 1.1 joerg printTemplateParameters(D->getTemplateParameterList(I)); 603 1.1 joerg } 604 1.1 joerg 605 1.1 joerg CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D); 606 1.1 joerg CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D); 607 1.1 joerg CXXDeductionGuideDecl *GuideDecl = dyn_cast<CXXDeductionGuideDecl>(D); 608 1.1 joerg if (!Policy.SuppressSpecifiers) { 609 1.1 joerg switch (D->getStorageClass()) { 610 1.1 joerg case SC_None: break; 611 1.1 joerg case SC_Extern: Out << "extern "; break; 612 1.1 joerg case SC_Static: Out << "static "; break; 613 1.1 joerg case SC_PrivateExtern: Out << "__private_extern__ "; break; 614 1.1 joerg case SC_Auto: case SC_Register: 615 1.1 joerg llvm_unreachable("invalid for functions"); 616 1.1 joerg } 617 1.1 joerg 618 1.1 joerg if (D->isInlineSpecified()) Out << "inline "; 619 1.1 joerg if (D->isVirtualAsWritten()) Out << "virtual "; 620 1.1 joerg if (D->isModulePrivate()) Out << "__module_private__ "; 621 1.1 joerg if (D->isConstexprSpecified() && !D->isExplicitlyDefaulted()) 622 1.1 joerg Out << "constexpr "; 623 1.1 joerg if (D->isConsteval()) Out << "consteval "; 624 1.1 joerg ExplicitSpecifier ExplicitSpec = ExplicitSpecifier::getFromDecl(D); 625 1.1 joerg if (ExplicitSpec.isSpecified()) 626 1.1.1.2 joerg printExplicitSpecifier(ExplicitSpec, Out, Policy, Indentation, Context); 627 1.1 joerg } 628 1.1 joerg 629 1.1 joerg PrintingPolicy SubPolicy(Policy); 630 1.1 joerg SubPolicy.SuppressSpecifiers = false; 631 1.1 joerg std::string Proto; 632 1.1 joerg 633 1.1 joerg if (Policy.FullyQualifiedName) { 634 1.1 joerg Proto += D->getQualifiedNameAsString(); 635 1.1 joerg } else { 636 1.1.1.2 joerg llvm::raw_string_ostream OS(Proto); 637 1.1 joerg if (!Policy.SuppressScope) { 638 1.1 joerg if (const NestedNameSpecifier *NS = D->getQualifier()) { 639 1.1 joerg NS->print(OS, Policy); 640 1.1 joerg } 641 1.1 joerg } 642 1.1.1.2 joerg D->getNameInfo().printName(OS, Policy); 643 1.1 joerg } 644 1.1 joerg 645 1.1 joerg if (GuideDecl) 646 1.1 joerg Proto = GuideDecl->getDeducedTemplate()->getDeclName().getAsString(); 647 1.1.1.2 joerg if (D->isFunctionTemplateSpecialization()) { 648 1.1 joerg llvm::raw_string_ostream POut(Proto); 649 1.1 joerg DeclPrinter TArgPrinter(POut, SubPolicy, Context, Indentation); 650 1.1.1.2 joerg const auto *TArgAsWritten = D->getTemplateSpecializationArgsAsWritten(); 651 1.1.1.2 joerg const TemplateParameterList *TPL = D->getTemplateSpecializationInfo() 652 1.1.1.2 joerg ->getTemplate() 653 1.1.1.2 joerg ->getTemplateParameters(); 654 1.1.1.2 joerg if (TArgAsWritten && !Policy.PrintCanonicalTypes) 655 1.1.1.2 joerg TArgPrinter.printTemplateArguments(TArgAsWritten->arguments(), TPL, 656 1.1.1.2 joerg /*TemplOverloaded*/ true); 657 1.1.1.2 joerg else if (const TemplateArgumentList *TArgs = 658 1.1.1.2 joerg D->getTemplateSpecializationArgs()) 659 1.1.1.2 joerg TArgPrinter.printTemplateArguments(TArgs->asArray(), TPL, 660 1.1.1.2 joerg /*TemplOverloaded*/ true); 661 1.1 joerg } 662 1.1 joerg 663 1.1 joerg QualType Ty = D->getType(); 664 1.1 joerg while (const ParenType *PT = dyn_cast<ParenType>(Ty)) { 665 1.1 joerg Proto = '(' + Proto + ')'; 666 1.1 joerg Ty = PT->getInnerType(); 667 1.1 joerg } 668 1.1 joerg 669 1.1 joerg if (const FunctionType *AFT = Ty->getAs<FunctionType>()) { 670 1.1 joerg const FunctionProtoType *FT = nullptr; 671 1.1 joerg if (D->hasWrittenPrototype()) 672 1.1 joerg FT = dyn_cast<FunctionProtoType>(AFT); 673 1.1 joerg 674 1.1 joerg Proto += "("; 675 1.1 joerg if (FT) { 676 1.1 joerg llvm::raw_string_ostream POut(Proto); 677 1.1 joerg DeclPrinter ParamPrinter(POut, SubPolicy, Context, Indentation); 678 1.1 joerg for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 679 1.1 joerg if (i) POut << ", "; 680 1.1 joerg ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); 681 1.1 joerg } 682 1.1 joerg 683 1.1 joerg if (FT->isVariadic()) { 684 1.1 joerg if (D->getNumParams()) POut << ", "; 685 1.1 joerg POut << "..."; 686 1.1 joerg } 687 1.1 joerg } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) { 688 1.1 joerg for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 689 1.1 joerg if (i) 690 1.1 joerg Proto += ", "; 691 1.1 joerg Proto += D->getParamDecl(i)->getNameAsString(); 692 1.1 joerg } 693 1.1 joerg } 694 1.1 joerg 695 1.1 joerg Proto += ")"; 696 1.1 joerg 697 1.1 joerg if (FT) { 698 1.1 joerg if (FT->isConst()) 699 1.1 joerg Proto += " const"; 700 1.1 joerg if (FT->isVolatile()) 701 1.1 joerg Proto += " volatile"; 702 1.1 joerg if (FT->isRestrict()) 703 1.1 joerg Proto += " restrict"; 704 1.1 joerg 705 1.1 joerg switch (FT->getRefQualifier()) { 706 1.1 joerg case RQ_None: 707 1.1 joerg break; 708 1.1 joerg case RQ_LValue: 709 1.1 joerg Proto += " &"; 710 1.1 joerg break; 711 1.1 joerg case RQ_RValue: 712 1.1 joerg Proto += " &&"; 713 1.1 joerg break; 714 1.1 joerg } 715 1.1 joerg } 716 1.1 joerg 717 1.1 joerg if (FT && FT->hasDynamicExceptionSpec()) { 718 1.1 joerg Proto += " throw("; 719 1.1 joerg if (FT->getExceptionSpecType() == EST_MSAny) 720 1.1 joerg Proto += "..."; 721 1.1 joerg else 722 1.1 joerg for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) { 723 1.1 joerg if (I) 724 1.1 joerg Proto += ", "; 725 1.1 joerg 726 1.1 joerg Proto += FT->getExceptionType(I).getAsString(SubPolicy); 727 1.1 joerg } 728 1.1 joerg Proto += ")"; 729 1.1 joerg } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) { 730 1.1 joerg Proto += " noexcept"; 731 1.1 joerg if (isComputedNoexcept(FT->getExceptionSpecType())) { 732 1.1 joerg Proto += "("; 733 1.1 joerg llvm::raw_string_ostream EOut(Proto); 734 1.1 joerg FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy, 735 1.1.1.2 joerg Indentation, "\n", &Context); 736 1.1 joerg EOut.flush(); 737 1.1 joerg Proto += EOut.str(); 738 1.1 joerg Proto += ")"; 739 1.1 joerg } 740 1.1 joerg } 741 1.1 joerg 742 1.1 joerg if (CDecl) { 743 1.1 joerg if (!Policy.TerseOutput) 744 1.1 joerg PrintConstructorInitializers(CDecl, Proto); 745 1.1 joerg } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) { 746 1.1 joerg if (FT && FT->hasTrailingReturn()) { 747 1.1 joerg if (!GuideDecl) 748 1.1 joerg Out << "auto "; 749 1.1 joerg Out << Proto << " -> "; 750 1.1 joerg Proto.clear(); 751 1.1 joerg } 752 1.1 joerg AFT->getReturnType().print(Out, Policy, Proto); 753 1.1 joerg Proto.clear(); 754 1.1 joerg } 755 1.1 joerg Out << Proto; 756 1.1.1.2 joerg 757 1.1.1.2 joerg if (Expr *TrailingRequiresClause = D->getTrailingRequiresClause()) { 758 1.1.1.2 joerg Out << " requires "; 759 1.1.1.2 joerg TrailingRequiresClause->printPretty(Out, nullptr, SubPolicy, Indentation, 760 1.1.1.2 joerg "\n", &Context); 761 1.1.1.2 joerg } 762 1.1 joerg } else { 763 1.1 joerg Ty.print(Out, Policy, Proto); 764 1.1 joerg } 765 1.1 joerg 766 1.1 joerg prettyPrintAttributes(D); 767 1.1 joerg 768 1.1 joerg if (D->isPure()) 769 1.1 joerg Out << " = 0"; 770 1.1 joerg else if (D->isDeletedAsWritten()) 771 1.1 joerg Out << " = delete"; 772 1.1 joerg else if (D->isExplicitlyDefaulted()) 773 1.1 joerg Out << " = default"; 774 1.1 joerg else if (D->doesThisDeclarationHaveABody()) { 775 1.1 joerg if (!Policy.TerseOutput) { 776 1.1 joerg if (!D->hasPrototype() && D->getNumParams()) { 777 1.1 joerg // This is a K&R function definition, so we need to print the 778 1.1 joerg // parameters. 779 1.1 joerg Out << '\n'; 780 1.1 joerg DeclPrinter ParamPrinter(Out, SubPolicy, Context, Indentation); 781 1.1 joerg Indentation += Policy.Indentation; 782 1.1 joerg for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 783 1.1 joerg Indent(); 784 1.1 joerg ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); 785 1.1 joerg Out << ";\n"; 786 1.1 joerg } 787 1.1 joerg Indentation -= Policy.Indentation; 788 1.1 joerg } else 789 1.1 joerg Out << ' '; 790 1.1 joerg 791 1.1 joerg if (D->getBody()) 792 1.1.1.2 joerg D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation, "\n", 793 1.1.1.2 joerg &Context); 794 1.1 joerg } else { 795 1.1 joerg if (!Policy.TerseOutput && isa<CXXConstructorDecl>(*D)) 796 1.1 joerg Out << " {}"; 797 1.1 joerg } 798 1.1 joerg } 799 1.1 joerg } 800 1.1 joerg 801 1.1 joerg void DeclPrinter::VisitFriendDecl(FriendDecl *D) { 802 1.1 joerg if (TypeSourceInfo *TSI = D->getFriendType()) { 803 1.1 joerg unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists(); 804 1.1 joerg for (unsigned i = 0; i < NumTPLists; ++i) 805 1.1 joerg printTemplateParameters(D->getFriendTypeTemplateParameterList(i)); 806 1.1 joerg Out << "friend "; 807 1.1 joerg Out << " " << TSI->getType().getAsString(Policy); 808 1.1 joerg } 809 1.1 joerg else if (FunctionDecl *FD = 810 1.1 joerg dyn_cast<FunctionDecl>(D->getFriendDecl())) { 811 1.1 joerg Out << "friend "; 812 1.1 joerg VisitFunctionDecl(FD); 813 1.1 joerg } 814 1.1 joerg else if (FunctionTemplateDecl *FTD = 815 1.1 joerg dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) { 816 1.1 joerg Out << "friend "; 817 1.1 joerg VisitFunctionTemplateDecl(FTD); 818 1.1 joerg } 819 1.1 joerg else if (ClassTemplateDecl *CTD = 820 1.1 joerg dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) { 821 1.1 joerg Out << "friend "; 822 1.1 joerg VisitRedeclarableTemplateDecl(CTD); 823 1.1 joerg } 824 1.1 joerg } 825 1.1 joerg 826 1.1 joerg void DeclPrinter::VisitFieldDecl(FieldDecl *D) { 827 1.1 joerg // FIXME: add printing of pragma attributes if required. 828 1.1 joerg if (!Policy.SuppressSpecifiers && D->isMutable()) 829 1.1 joerg Out << "mutable "; 830 1.1 joerg if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 831 1.1 joerg Out << "__module_private__ "; 832 1.1 joerg 833 1.1 joerg Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()). 834 1.1 joerg stream(Policy, D->getName(), Indentation); 835 1.1 joerg 836 1.1 joerg if (D->isBitField()) { 837 1.1 joerg Out << " : "; 838 1.1.1.2 joerg D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation, "\n", 839 1.1.1.2 joerg &Context); 840 1.1 joerg } 841 1.1 joerg 842 1.1 joerg Expr *Init = D->getInClassInitializer(); 843 1.1 joerg if (!Policy.SuppressInitializers && Init) { 844 1.1 joerg if (D->getInClassInitStyle() == ICIS_ListInit) 845 1.1 joerg Out << " "; 846 1.1 joerg else 847 1.1 joerg Out << " = "; 848 1.1.1.2 joerg Init->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context); 849 1.1 joerg } 850 1.1 joerg prettyPrintAttributes(D); 851 1.1 joerg } 852 1.1 joerg 853 1.1 joerg void DeclPrinter::VisitLabelDecl(LabelDecl *D) { 854 1.1 joerg Out << *D << ":"; 855 1.1 joerg } 856 1.1 joerg 857 1.1 joerg void DeclPrinter::VisitVarDecl(VarDecl *D) { 858 1.1 joerg prettyPrintPragmas(D); 859 1.1 joerg 860 1.1 joerg QualType T = D->getTypeSourceInfo() 861 1.1 joerg ? D->getTypeSourceInfo()->getType() 862 1.1 joerg : D->getASTContext().getUnqualifiedObjCPointerType(D->getType()); 863 1.1 joerg 864 1.1 joerg if (!Policy.SuppressSpecifiers) { 865 1.1 joerg StorageClass SC = D->getStorageClass(); 866 1.1 joerg if (SC != SC_None) 867 1.1 joerg Out << VarDecl::getStorageClassSpecifierString(SC) << " "; 868 1.1 joerg 869 1.1 joerg switch (D->getTSCSpec()) { 870 1.1 joerg case TSCS_unspecified: 871 1.1 joerg break; 872 1.1 joerg case TSCS___thread: 873 1.1 joerg Out << "__thread "; 874 1.1 joerg break; 875 1.1 joerg case TSCS__Thread_local: 876 1.1 joerg Out << "_Thread_local "; 877 1.1 joerg break; 878 1.1 joerg case TSCS_thread_local: 879 1.1 joerg Out << "thread_local "; 880 1.1 joerg break; 881 1.1 joerg } 882 1.1 joerg 883 1.1 joerg if (D->isModulePrivate()) 884 1.1 joerg Out << "__module_private__ "; 885 1.1 joerg 886 1.1 joerg if (D->isConstexpr()) { 887 1.1 joerg Out << "constexpr "; 888 1.1 joerg T.removeLocalConst(); 889 1.1 joerg } 890 1.1 joerg } 891 1.1 joerg 892 1.1 joerg printDeclType(T, D->getName()); 893 1.1 joerg Expr *Init = D->getInit(); 894 1.1 joerg if (!Policy.SuppressInitializers && Init) { 895 1.1 joerg bool ImplicitInit = false; 896 1.1 joerg if (CXXConstructExpr *Construct = 897 1.1 joerg dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) { 898 1.1 joerg if (D->getInitStyle() == VarDecl::CallInit && 899 1.1 joerg !Construct->isListInitialization()) { 900 1.1 joerg ImplicitInit = Construct->getNumArgs() == 0 || 901 1.1 joerg Construct->getArg(0)->isDefaultArgument(); 902 1.1 joerg } 903 1.1 joerg } 904 1.1 joerg if (!ImplicitInit) { 905 1.1 joerg if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init)) 906 1.1 joerg Out << "("; 907 1.1 joerg else if (D->getInitStyle() == VarDecl::CInit) { 908 1.1 joerg Out << " = "; 909 1.1 joerg } 910 1.1 joerg PrintingPolicy SubPolicy(Policy); 911 1.1 joerg SubPolicy.SuppressSpecifiers = false; 912 1.1 joerg SubPolicy.IncludeTagDefinition = false; 913 1.1.1.2 joerg Init->printPretty(Out, nullptr, SubPolicy, Indentation, "\n", &Context); 914 1.1 joerg if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init)) 915 1.1 joerg Out << ")"; 916 1.1 joerg } 917 1.1 joerg } 918 1.1 joerg prettyPrintAttributes(D); 919 1.1 joerg } 920 1.1 joerg 921 1.1 joerg void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) { 922 1.1 joerg VisitVarDecl(D); 923 1.1 joerg } 924 1.1 joerg 925 1.1 joerg void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { 926 1.1 joerg Out << "__asm ("; 927 1.1.1.2 joerg D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation, "\n", 928 1.1.1.2 joerg &Context); 929 1.1 joerg Out << ")"; 930 1.1 joerg } 931 1.1 joerg 932 1.1 joerg void DeclPrinter::VisitImportDecl(ImportDecl *D) { 933 1.1 joerg Out << "@import " << D->getImportedModule()->getFullModuleName() 934 1.1 joerg << ";\n"; 935 1.1 joerg } 936 1.1 joerg 937 1.1 joerg void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) { 938 1.1 joerg Out << "static_assert("; 939 1.1.1.2 joerg D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation, "\n", 940 1.1.1.2 joerg &Context); 941 1.1 joerg if (StringLiteral *SL = D->getMessage()) { 942 1.1 joerg Out << ", "; 943 1.1.1.2 joerg SL->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context); 944 1.1 joerg } 945 1.1 joerg Out << ")"; 946 1.1 joerg } 947 1.1 joerg 948 1.1 joerg //---------------------------------------------------------------------------- 949 1.1 joerg // C++ declarations 950 1.1 joerg //---------------------------------------------------------------------------- 951 1.1 joerg void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) { 952 1.1 joerg if (D->isInline()) 953 1.1 joerg Out << "inline "; 954 1.1.1.2 joerg 955 1.1.1.2 joerg Out << "namespace "; 956 1.1.1.2 joerg if (D->getDeclName()) 957 1.1.1.2 joerg Out << D->getDeclName() << ' '; 958 1.1.1.2 joerg Out << "{\n"; 959 1.1.1.2 joerg 960 1.1 joerg VisitDeclContext(D); 961 1.1 joerg Indent() << "}"; 962 1.1 joerg } 963 1.1 joerg 964 1.1 joerg void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { 965 1.1 joerg Out << "using namespace "; 966 1.1 joerg if (D->getQualifier()) 967 1.1 joerg D->getQualifier()->print(Out, Policy); 968 1.1 joerg Out << *D->getNominatedNamespaceAsWritten(); 969 1.1 joerg } 970 1.1 joerg 971 1.1 joerg void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { 972 1.1 joerg Out << "namespace " << *D << " = "; 973 1.1 joerg if (D->getQualifier()) 974 1.1 joerg D->getQualifier()->print(Out, Policy); 975 1.1 joerg Out << *D->getAliasedNamespace(); 976 1.1 joerg } 977 1.1 joerg 978 1.1 joerg void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) { 979 1.1 joerg prettyPrintAttributes(D); 980 1.1 joerg } 981 1.1 joerg 982 1.1 joerg void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) { 983 1.1 joerg // FIXME: add printing of pragma attributes if required. 984 1.1 joerg if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 985 1.1 joerg Out << "__module_private__ "; 986 1.1 joerg Out << D->getKindName(); 987 1.1 joerg 988 1.1 joerg prettyPrintAttributes(D); 989 1.1 joerg 990 1.1 joerg if (D->getIdentifier()) { 991 1.1 joerg Out << ' ' << *D; 992 1.1 joerg 993 1.1.1.2 joerg if (auto S = dyn_cast<ClassTemplateSpecializationDecl>(D)) { 994 1.1.1.2 joerg ArrayRef<TemplateArgument> Args = S->getTemplateArgs().asArray(); 995 1.1.1.2 joerg if (!Policy.PrintCanonicalTypes) 996 1.1.1.2 joerg if (const auto* TSI = S->getTypeAsWritten()) 997 1.1.1.2 joerg if (const auto *TST = 998 1.1.1.2 joerg dyn_cast<TemplateSpecializationType>(TSI->getType())) 999 1.1.1.2 joerg Args = TST->template_arguments(); 1000 1.1.1.2 joerg printTemplateArguments( 1001 1.1.1.2 joerg Args, S->getSpecializedTemplate()->getTemplateParameters(), 1002 1.1.1.2 joerg /*TemplOverloaded*/ false); 1003 1.1.1.2 joerg } 1004 1.1 joerg } 1005 1.1 joerg 1006 1.1 joerg if (D->isCompleteDefinition()) { 1007 1.1 joerg // Print the base classes 1008 1.1 joerg if (D->getNumBases()) { 1009 1.1 joerg Out << " : "; 1010 1.1 joerg for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(), 1011 1.1 joerg BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) { 1012 1.1 joerg if (Base != D->bases_begin()) 1013 1.1 joerg Out << ", "; 1014 1.1 joerg 1015 1.1 joerg if (Base->isVirtual()) 1016 1.1 joerg Out << "virtual "; 1017 1.1 joerg 1018 1.1 joerg AccessSpecifier AS = Base->getAccessSpecifierAsWritten(); 1019 1.1 joerg if (AS != AS_none) { 1020 1.1 joerg Print(AS); 1021 1.1 joerg Out << " "; 1022 1.1 joerg } 1023 1.1 joerg Out << Base->getType().getAsString(Policy); 1024 1.1 joerg 1025 1.1 joerg if (Base->isPackExpansion()) 1026 1.1 joerg Out << "..."; 1027 1.1 joerg } 1028 1.1 joerg } 1029 1.1 joerg 1030 1.1 joerg // Print the class definition 1031 1.1 joerg // FIXME: Doesn't print access specifiers, e.g., "public:" 1032 1.1 joerg if (Policy.TerseOutput) { 1033 1.1 joerg Out << " {}"; 1034 1.1 joerg } else { 1035 1.1 joerg Out << " {\n"; 1036 1.1 joerg VisitDeclContext(D); 1037 1.1 joerg Indent() << "}"; 1038 1.1 joerg } 1039 1.1 joerg } 1040 1.1 joerg } 1041 1.1 joerg 1042 1.1 joerg void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { 1043 1.1 joerg const char *l; 1044 1.1.1.2 joerg if (D->getLanguage() == LinkageSpecDecl::lang_c) 1045 1.1 joerg l = "C"; 1046 1.1.1.2 joerg else { 1047 1.1.1.2 joerg assert(D->getLanguage() == LinkageSpecDecl::lang_cxx && 1048 1.1.1.2 joerg "unknown language in linkage specification"); 1049 1.1 joerg l = "C++"; 1050 1.1 joerg } 1051 1.1 joerg 1052 1.1 joerg Out << "extern \"" << l << "\" "; 1053 1.1 joerg if (D->hasBraces()) { 1054 1.1 joerg Out << "{\n"; 1055 1.1 joerg VisitDeclContext(D); 1056 1.1 joerg Indent() << "}"; 1057 1.1 joerg } else 1058 1.1 joerg Visit(*D->decls_begin()); 1059 1.1 joerg } 1060 1.1 joerg 1061 1.1 joerg void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params, 1062 1.1 joerg bool OmitTemplateKW) { 1063 1.1 joerg assert(Params); 1064 1.1 joerg 1065 1.1 joerg if (!OmitTemplateKW) 1066 1.1 joerg Out << "template "; 1067 1.1 joerg Out << '<'; 1068 1.1 joerg 1069 1.1 joerg bool NeedComma = false; 1070 1.1 joerg for (const Decl *Param : *Params) { 1071 1.1 joerg if (Param->isImplicit()) 1072 1.1 joerg continue; 1073 1.1 joerg 1074 1.1 joerg if (NeedComma) 1075 1.1 joerg Out << ", "; 1076 1.1 joerg else 1077 1.1 joerg NeedComma = true; 1078 1.1 joerg 1079 1.1.1.2 joerg if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { 1080 1.1.1.2 joerg VisitTemplateTypeParmDecl(TTP); 1081 1.1 joerg } else if (auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { 1082 1.1.1.2 joerg VisitNonTypeTemplateParmDecl(NTTP); 1083 1.1 joerg } else if (auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) { 1084 1.1 joerg VisitTemplateDecl(TTPD); 1085 1.1 joerg // FIXME: print the default argument, if present. 1086 1.1 joerg } 1087 1.1 joerg } 1088 1.1 joerg 1089 1.1 joerg Out << '>'; 1090 1.1 joerg if (!OmitTemplateKW) 1091 1.1 joerg Out << ' '; 1092 1.1 joerg } 1093 1.1 joerg 1094 1.1.1.2 joerg void DeclPrinter::printTemplateArguments(ArrayRef<TemplateArgument> Args, 1095 1.1.1.2 joerg const TemplateParameterList *Params, 1096 1.1.1.2 joerg bool TemplOverloaded) { 1097 1.1 joerg Out << "<"; 1098 1.1 joerg for (size_t I = 0, E = Args.size(); I < E; ++I) { 1099 1.1 joerg if (I) 1100 1.1 joerg Out << ", "; 1101 1.1.1.2 joerg if (TemplOverloaded || !Params) 1102 1.1.1.2 joerg Args[I].print(Policy, Out, /*IncludeType*/ true); 1103 1.1.1.2 joerg else 1104 1.1.1.2 joerg Args[I].print( 1105 1.1.1.2 joerg Policy, Out, 1106 1.1.1.2 joerg TemplateParameterList::shouldIncludeTypeForArgument(Params, I)); 1107 1.1.1.2 joerg } 1108 1.1.1.2 joerg Out << ">"; 1109 1.1.1.2 joerg } 1110 1.1.1.2 joerg 1111 1.1.1.2 joerg void DeclPrinter::printTemplateArguments(ArrayRef<TemplateArgumentLoc> Args, 1112 1.1.1.2 joerg const TemplateParameterList *Params, 1113 1.1.1.2 joerg bool TemplOverloaded) { 1114 1.1.1.2 joerg Out << "<"; 1115 1.1.1.2 joerg for (size_t I = 0, E = Args.size(); I < E; ++I) { 1116 1.1.1.2 joerg if (I) 1117 1.1.1.2 joerg Out << ", "; 1118 1.1.1.2 joerg if (TemplOverloaded) 1119 1.1.1.2 joerg Args[I].getArgument().print(Policy, Out, /*IncludeType*/ true); 1120 1.1.1.2 joerg else 1121 1.1.1.2 joerg Args[I].getArgument().print( 1122 1.1.1.2 joerg Policy, Out, 1123 1.1.1.2 joerg TemplateParameterList::shouldIncludeTypeForArgument(Params, I)); 1124 1.1 joerg } 1125 1.1 joerg Out << ">"; 1126 1.1 joerg } 1127 1.1 joerg 1128 1.1 joerg void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) { 1129 1.1 joerg printTemplateParameters(D->getTemplateParameters()); 1130 1.1 joerg 1131 1.1 joerg if (const TemplateTemplateParmDecl *TTP = 1132 1.1 joerg dyn_cast<TemplateTemplateParmDecl>(D)) { 1133 1.1.1.2 joerg Out << "class"; 1134 1.1.1.2 joerg 1135 1.1 joerg if (TTP->isParameterPack()) 1136 1.1.1.2 joerg Out << " ..."; 1137 1.1.1.2 joerg else if (TTP->getDeclName()) 1138 1.1.1.2 joerg Out << ' '; 1139 1.1.1.2 joerg 1140 1.1.1.2 joerg if (TTP->getDeclName()) 1141 1.1.1.2 joerg Out << TTP->getDeclName(); 1142 1.1 joerg } else if (auto *TD = D->getTemplatedDecl()) 1143 1.1 joerg Visit(TD); 1144 1.1 joerg else if (const auto *Concept = dyn_cast<ConceptDecl>(D)) { 1145 1.1 joerg Out << "concept " << Concept->getName() << " = " ; 1146 1.1.1.2 joerg Concept->getConstraintExpr()->printPretty(Out, nullptr, Policy, Indentation, 1147 1.1.1.2 joerg "\n", &Context); 1148 1.1 joerg Out << ";"; 1149 1.1 joerg } 1150 1.1 joerg } 1151 1.1 joerg 1152 1.1 joerg void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { 1153 1.1 joerg prettyPrintPragmas(D->getTemplatedDecl()); 1154 1.1 joerg // Print any leading template parameter lists. 1155 1.1 joerg if (const FunctionDecl *FD = D->getTemplatedDecl()) { 1156 1.1 joerg for (unsigned I = 0, NumTemplateParams = FD->getNumTemplateParameterLists(); 1157 1.1 joerg I < NumTemplateParams; ++I) 1158 1.1 joerg printTemplateParameters(FD->getTemplateParameterList(I)); 1159 1.1 joerg } 1160 1.1 joerg VisitRedeclarableTemplateDecl(D); 1161 1.1 joerg // Declare target attribute is special one, natural spelling for the pragma 1162 1.1 joerg // assumes "ending" construct so print it here. 1163 1.1 joerg if (D->getTemplatedDecl()->hasAttr<OMPDeclareTargetDeclAttr>()) 1164 1.1 joerg Out << "#pragma omp end declare target\n"; 1165 1.1 joerg 1166 1.1 joerg // Never print "instantiations" for deduction guides (they don't really 1167 1.1 joerg // have them). 1168 1.1 joerg if (PrintInstantiation && 1169 1.1 joerg !isa<CXXDeductionGuideDecl>(D->getTemplatedDecl())) { 1170 1.1 joerg FunctionDecl *PrevDecl = D->getTemplatedDecl(); 1171 1.1 joerg const FunctionDecl *Def; 1172 1.1 joerg if (PrevDecl->isDefined(Def) && Def != PrevDecl) 1173 1.1 joerg return; 1174 1.1 joerg for (auto *I : D->specializations()) 1175 1.1 joerg if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) { 1176 1.1 joerg if (!PrevDecl->isThisDeclarationADefinition()) 1177 1.1 joerg Out << ";\n"; 1178 1.1 joerg Indent(); 1179 1.1 joerg prettyPrintPragmas(I); 1180 1.1 joerg Visit(I); 1181 1.1 joerg } 1182 1.1 joerg } 1183 1.1 joerg } 1184 1.1 joerg 1185 1.1 joerg void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) { 1186 1.1 joerg VisitRedeclarableTemplateDecl(D); 1187 1.1 joerg 1188 1.1 joerg if (PrintInstantiation) { 1189 1.1 joerg for (auto *I : D->specializations()) 1190 1.1 joerg if (I->getSpecializationKind() == TSK_ImplicitInstantiation) { 1191 1.1 joerg if (D->isThisDeclarationADefinition()) 1192 1.1 joerg Out << ";"; 1193 1.1 joerg Out << "\n"; 1194 1.1 joerg Visit(I); 1195 1.1 joerg } 1196 1.1 joerg } 1197 1.1 joerg } 1198 1.1 joerg 1199 1.1 joerg void DeclPrinter::VisitClassTemplateSpecializationDecl( 1200 1.1 joerg ClassTemplateSpecializationDecl *D) { 1201 1.1 joerg Out << "template<> "; 1202 1.1 joerg VisitCXXRecordDecl(D); 1203 1.1 joerg } 1204 1.1 joerg 1205 1.1 joerg void DeclPrinter::VisitClassTemplatePartialSpecializationDecl( 1206 1.1 joerg ClassTemplatePartialSpecializationDecl *D) { 1207 1.1 joerg printTemplateParameters(D->getTemplateParameters()); 1208 1.1 joerg VisitCXXRecordDecl(D); 1209 1.1 joerg } 1210 1.1 joerg 1211 1.1 joerg //---------------------------------------------------------------------------- 1212 1.1 joerg // Objective-C declarations 1213 1.1 joerg //---------------------------------------------------------------------------- 1214 1.1 joerg 1215 1.1 joerg void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx, 1216 1.1 joerg Decl::ObjCDeclQualifier Quals, 1217 1.1 joerg QualType T) { 1218 1.1 joerg Out << '('; 1219 1.1 joerg if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In) 1220 1.1 joerg Out << "in "; 1221 1.1 joerg if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout) 1222 1.1 joerg Out << "inout "; 1223 1.1 joerg if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out) 1224 1.1 joerg Out << "out "; 1225 1.1 joerg if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy) 1226 1.1 joerg Out << "bycopy "; 1227 1.1 joerg if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref) 1228 1.1 joerg Out << "byref "; 1229 1.1 joerg if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway) 1230 1.1 joerg Out << "oneway "; 1231 1.1 joerg if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) { 1232 1.1 joerg if (auto nullability = AttributedType::stripOuterNullability(T)) 1233 1.1 joerg Out << getNullabilitySpelling(*nullability, true) << ' '; 1234 1.1 joerg } 1235 1.1 joerg 1236 1.1 joerg Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy); 1237 1.1 joerg Out << ')'; 1238 1.1 joerg } 1239 1.1 joerg 1240 1.1 joerg void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) { 1241 1.1 joerg Out << "<"; 1242 1.1 joerg unsigned First = true; 1243 1.1 joerg for (auto *Param : *Params) { 1244 1.1 joerg if (First) { 1245 1.1 joerg First = false; 1246 1.1 joerg } else { 1247 1.1 joerg Out << ", "; 1248 1.1 joerg } 1249 1.1 joerg 1250 1.1 joerg switch (Param->getVariance()) { 1251 1.1 joerg case ObjCTypeParamVariance::Invariant: 1252 1.1 joerg break; 1253 1.1 joerg 1254 1.1 joerg case ObjCTypeParamVariance::Covariant: 1255 1.1 joerg Out << "__covariant "; 1256 1.1 joerg break; 1257 1.1 joerg 1258 1.1 joerg case ObjCTypeParamVariance::Contravariant: 1259 1.1 joerg Out << "__contravariant "; 1260 1.1 joerg break; 1261 1.1 joerg } 1262 1.1 joerg 1263 1.1.1.2 joerg Out << Param->getDeclName(); 1264 1.1 joerg 1265 1.1 joerg if (Param->hasExplicitBound()) { 1266 1.1 joerg Out << " : " << Param->getUnderlyingType().getAsString(Policy); 1267 1.1 joerg } 1268 1.1 joerg } 1269 1.1 joerg Out << ">"; 1270 1.1 joerg } 1271 1.1 joerg 1272 1.1 joerg void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) { 1273 1.1 joerg if (OMD->isInstanceMethod()) 1274 1.1 joerg Out << "- "; 1275 1.1 joerg else 1276 1.1 joerg Out << "+ "; 1277 1.1 joerg if (!OMD->getReturnType().isNull()) { 1278 1.1 joerg PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(), 1279 1.1 joerg OMD->getReturnType()); 1280 1.1 joerg } 1281 1.1 joerg 1282 1.1 joerg std::string name = OMD->getSelector().getAsString(); 1283 1.1 joerg std::string::size_type pos, lastPos = 0; 1284 1.1 joerg for (const auto *PI : OMD->parameters()) { 1285 1.1 joerg // FIXME: selector is missing here! 1286 1.1 joerg pos = name.find_first_of(':', lastPos); 1287 1.1 joerg if (lastPos != 0) 1288 1.1 joerg Out << " "; 1289 1.1 joerg Out << name.substr(lastPos, pos - lastPos) << ':'; 1290 1.1 joerg PrintObjCMethodType(OMD->getASTContext(), 1291 1.1 joerg PI->getObjCDeclQualifier(), 1292 1.1 joerg PI->getType()); 1293 1.1 joerg Out << *PI; 1294 1.1 joerg lastPos = pos + 1; 1295 1.1 joerg } 1296 1.1 joerg 1297 1.1 joerg if (OMD->param_begin() == OMD->param_end()) 1298 1.1 joerg Out << name; 1299 1.1 joerg 1300 1.1 joerg if (OMD->isVariadic()) 1301 1.1 joerg Out << ", ..."; 1302 1.1 joerg 1303 1.1 joerg prettyPrintAttributes(OMD); 1304 1.1 joerg 1305 1.1 joerg if (OMD->getBody() && !Policy.TerseOutput) { 1306 1.1 joerg Out << ' '; 1307 1.1.1.2 joerg OMD->getBody()->printPretty(Out, nullptr, Policy, Indentation, "\n", 1308 1.1.1.2 joerg &Context); 1309 1.1 joerg } 1310 1.1 joerg else if (Policy.PolishForDeclaration) 1311 1.1 joerg Out << ';'; 1312 1.1 joerg } 1313 1.1 joerg 1314 1.1 joerg void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) { 1315 1.1 joerg std::string I = OID->getNameAsString(); 1316 1.1 joerg ObjCInterfaceDecl *SID = OID->getSuperClass(); 1317 1.1 joerg 1318 1.1 joerg bool eolnOut = false; 1319 1.1 joerg if (SID) 1320 1.1 joerg Out << "@implementation " << I << " : " << *SID; 1321 1.1 joerg else 1322 1.1 joerg Out << "@implementation " << I; 1323 1.1 joerg 1324 1.1 joerg if (OID->ivar_size() > 0) { 1325 1.1 joerg Out << "{\n"; 1326 1.1 joerg eolnOut = true; 1327 1.1 joerg Indentation += Policy.Indentation; 1328 1.1 joerg for (const auto *I : OID->ivars()) { 1329 1.1 joerg Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()). 1330 1.1 joerg getAsString(Policy) << ' ' << *I << ";\n"; 1331 1.1 joerg } 1332 1.1 joerg Indentation -= Policy.Indentation; 1333 1.1 joerg Out << "}\n"; 1334 1.1 joerg } 1335 1.1 joerg else if (SID || (OID->decls_begin() != OID->decls_end())) { 1336 1.1 joerg Out << "\n"; 1337 1.1 joerg eolnOut = true; 1338 1.1 joerg } 1339 1.1 joerg VisitDeclContext(OID, false); 1340 1.1 joerg if (!eolnOut) 1341 1.1 joerg Out << "\n"; 1342 1.1 joerg Out << "@end"; 1343 1.1 joerg } 1344 1.1 joerg 1345 1.1 joerg void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) { 1346 1.1 joerg std::string I = OID->getNameAsString(); 1347 1.1 joerg ObjCInterfaceDecl *SID = OID->getSuperClass(); 1348 1.1 joerg 1349 1.1 joerg if (!OID->isThisDeclarationADefinition()) { 1350 1.1 joerg Out << "@class " << I; 1351 1.1 joerg 1352 1.1 joerg if (auto TypeParams = OID->getTypeParamListAsWritten()) { 1353 1.1 joerg PrintObjCTypeParams(TypeParams); 1354 1.1 joerg } 1355 1.1 joerg 1356 1.1 joerg Out << ";"; 1357 1.1 joerg return; 1358 1.1 joerg } 1359 1.1 joerg bool eolnOut = false; 1360 1.1 joerg Out << "@interface " << I; 1361 1.1 joerg 1362 1.1 joerg if (auto TypeParams = OID->getTypeParamListAsWritten()) { 1363 1.1 joerg PrintObjCTypeParams(TypeParams); 1364 1.1 joerg } 1365 1.1 joerg 1366 1.1 joerg if (SID) 1367 1.1 joerg Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy); 1368 1.1 joerg 1369 1.1 joerg // Protocols? 1370 1.1 joerg const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols(); 1371 1.1 joerg if (!Protocols.empty()) { 1372 1.1 joerg for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), 1373 1.1 joerg E = Protocols.end(); I != E; ++I) 1374 1.1 joerg Out << (I == Protocols.begin() ? '<' : ',') << **I; 1375 1.1 joerg Out << "> "; 1376 1.1 joerg } 1377 1.1 joerg 1378 1.1 joerg if (OID->ivar_size() > 0) { 1379 1.1 joerg Out << "{\n"; 1380 1.1 joerg eolnOut = true; 1381 1.1 joerg Indentation += Policy.Indentation; 1382 1.1 joerg for (const auto *I : OID->ivars()) { 1383 1.1 joerg Indent() << I->getASTContext() 1384 1.1 joerg .getUnqualifiedObjCPointerType(I->getType()) 1385 1.1 joerg .getAsString(Policy) << ' ' << *I << ";\n"; 1386 1.1 joerg } 1387 1.1 joerg Indentation -= Policy.Indentation; 1388 1.1 joerg Out << "}\n"; 1389 1.1 joerg } 1390 1.1 joerg else if (SID || (OID->decls_begin() != OID->decls_end())) { 1391 1.1 joerg Out << "\n"; 1392 1.1 joerg eolnOut = true; 1393 1.1 joerg } 1394 1.1 joerg 1395 1.1 joerg VisitDeclContext(OID, false); 1396 1.1 joerg if (!eolnOut) 1397 1.1 joerg Out << "\n"; 1398 1.1 joerg Out << "@end"; 1399 1.1 joerg // FIXME: implement the rest... 1400 1.1 joerg } 1401 1.1 joerg 1402 1.1 joerg void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { 1403 1.1 joerg if (!PID->isThisDeclarationADefinition()) { 1404 1.1 joerg Out << "@protocol " << *PID << ";\n"; 1405 1.1 joerg return; 1406 1.1 joerg } 1407 1.1 joerg // Protocols? 1408 1.1 joerg const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols(); 1409 1.1 joerg if (!Protocols.empty()) { 1410 1.1 joerg Out << "@protocol " << *PID; 1411 1.1 joerg for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), 1412 1.1 joerg E = Protocols.end(); I != E; ++I) 1413 1.1 joerg Out << (I == Protocols.begin() ? '<' : ',') << **I; 1414 1.1 joerg Out << ">\n"; 1415 1.1 joerg } else 1416 1.1 joerg Out << "@protocol " << *PID << '\n'; 1417 1.1 joerg VisitDeclContext(PID, false); 1418 1.1 joerg Out << "@end"; 1419 1.1 joerg } 1420 1.1 joerg 1421 1.1 joerg void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { 1422 1.1.1.2 joerg Out << "@implementation "; 1423 1.1.1.2 joerg if (const auto *CID = PID->getClassInterface()) 1424 1.1.1.2 joerg Out << *CID; 1425 1.1.1.2 joerg else 1426 1.1.1.2 joerg Out << "<<error-type>>"; 1427 1.1.1.2 joerg Out << '(' << *PID << ")\n"; 1428 1.1 joerg 1429 1.1 joerg VisitDeclContext(PID, false); 1430 1.1 joerg Out << "@end"; 1431 1.1 joerg // FIXME: implement the rest... 1432 1.1 joerg } 1433 1.1 joerg 1434 1.1 joerg void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) { 1435 1.1.1.2 joerg Out << "@interface "; 1436 1.1.1.2 joerg if (const auto *CID = PID->getClassInterface()) 1437 1.1.1.2 joerg Out << *CID; 1438 1.1.1.2 joerg else 1439 1.1.1.2 joerg Out << "<<error-type>>"; 1440 1.1 joerg if (auto TypeParams = PID->getTypeParamList()) { 1441 1.1 joerg PrintObjCTypeParams(TypeParams); 1442 1.1 joerg } 1443 1.1 joerg Out << "(" << *PID << ")\n"; 1444 1.1 joerg if (PID->ivar_size() > 0) { 1445 1.1 joerg Out << "{\n"; 1446 1.1 joerg Indentation += Policy.Indentation; 1447 1.1 joerg for (const auto *I : PID->ivars()) 1448 1.1 joerg Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()). 1449 1.1 joerg getAsString(Policy) << ' ' << *I << ";\n"; 1450 1.1 joerg Indentation -= Policy.Indentation; 1451 1.1 joerg Out << "}\n"; 1452 1.1 joerg } 1453 1.1 joerg 1454 1.1 joerg VisitDeclContext(PID, false); 1455 1.1 joerg Out << "@end"; 1456 1.1 joerg 1457 1.1 joerg // FIXME: implement the rest... 1458 1.1 joerg } 1459 1.1 joerg 1460 1.1 joerg void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { 1461 1.1 joerg Out << "@compatibility_alias " << *AID 1462 1.1 joerg << ' ' << *AID->getClassInterface() << ";\n"; 1463 1.1 joerg } 1464 1.1 joerg 1465 1.1 joerg /// PrintObjCPropertyDecl - print a property declaration. 1466 1.1 joerg /// 1467 1.1 joerg /// Print attributes in the following order: 1468 1.1 joerg /// - class 1469 1.1 joerg /// - nonatomic | atomic 1470 1.1 joerg /// - assign | retain | strong | copy | weak | unsafe_unretained 1471 1.1 joerg /// - readwrite | readonly 1472 1.1 joerg /// - getter & setter 1473 1.1 joerg /// - nullability 1474 1.1 joerg void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) { 1475 1.1 joerg if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required) 1476 1.1 joerg Out << "@required\n"; 1477 1.1 joerg else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) 1478 1.1 joerg Out << "@optional\n"; 1479 1.1 joerg 1480 1.1 joerg QualType T = PDecl->getType(); 1481 1.1 joerg 1482 1.1 joerg Out << "@property"; 1483 1.1.1.2 joerg if (PDecl->getPropertyAttributes() != ObjCPropertyAttribute::kind_noattr) { 1484 1.1 joerg bool first = true; 1485 1.1 joerg Out << "("; 1486 1.1.1.2 joerg if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_class) { 1487 1.1 joerg Out << (first ? "" : ", ") << "class"; 1488 1.1 joerg first = false; 1489 1.1 joerg } 1490 1.1 joerg 1491 1.1.1.2 joerg if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_direct) { 1492 1.1.1.2 joerg Out << (first ? "" : ", ") << "direct"; 1493 1.1.1.2 joerg first = false; 1494 1.1.1.2 joerg } 1495 1.1.1.2 joerg 1496 1.1 joerg if (PDecl->getPropertyAttributes() & 1497 1.1.1.2 joerg ObjCPropertyAttribute::kind_nonatomic) { 1498 1.1 joerg Out << (first ? "" : ", ") << "nonatomic"; 1499 1.1 joerg first = false; 1500 1.1 joerg } 1501 1.1.1.2 joerg if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_atomic) { 1502 1.1 joerg Out << (first ? "" : ", ") << "atomic"; 1503 1.1 joerg first = false; 1504 1.1 joerg } 1505 1.1 joerg 1506 1.1.1.2 joerg if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_assign) { 1507 1.1 joerg Out << (first ? "" : ", ") << "assign"; 1508 1.1 joerg first = false; 1509 1.1 joerg } 1510 1.1.1.2 joerg if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_retain) { 1511 1.1 joerg Out << (first ? "" : ", ") << "retain"; 1512 1.1 joerg first = false; 1513 1.1 joerg } 1514 1.1 joerg 1515 1.1.1.2 joerg if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_strong) { 1516 1.1 joerg Out << (first ? "" : ", ") << "strong"; 1517 1.1 joerg first = false; 1518 1.1 joerg } 1519 1.1.1.2 joerg if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_copy) { 1520 1.1 joerg Out << (first ? "" : ", ") << "copy"; 1521 1.1 joerg first = false; 1522 1.1 joerg } 1523 1.1.1.2 joerg if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_weak) { 1524 1.1 joerg Out << (first ? "" : ", ") << "weak"; 1525 1.1 joerg first = false; 1526 1.1 joerg } 1527 1.1.1.2 joerg if (PDecl->getPropertyAttributes() & 1528 1.1.1.2 joerg ObjCPropertyAttribute::kind_unsafe_unretained) { 1529 1.1 joerg Out << (first ? "" : ", ") << "unsafe_unretained"; 1530 1.1 joerg first = false; 1531 1.1 joerg } 1532 1.1 joerg 1533 1.1 joerg if (PDecl->getPropertyAttributes() & 1534 1.1.1.2 joerg ObjCPropertyAttribute::kind_readwrite) { 1535 1.1 joerg Out << (first ? "" : ", ") << "readwrite"; 1536 1.1 joerg first = false; 1537 1.1 joerg } 1538 1.1.1.2 joerg if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_readonly) { 1539 1.1 joerg Out << (first ? "" : ", ") << "readonly"; 1540 1.1 joerg first = false; 1541 1.1 joerg } 1542 1.1 joerg 1543 1.1.1.2 joerg if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_getter) { 1544 1.1 joerg Out << (first ? "" : ", ") << "getter = "; 1545 1.1 joerg PDecl->getGetterName().print(Out); 1546 1.1 joerg first = false; 1547 1.1 joerg } 1548 1.1.1.2 joerg if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_setter) { 1549 1.1 joerg Out << (first ? "" : ", ") << "setter = "; 1550 1.1 joerg PDecl->getSetterName().print(Out); 1551 1.1 joerg first = false; 1552 1.1 joerg } 1553 1.1 joerg 1554 1.1 joerg if (PDecl->getPropertyAttributes() & 1555 1.1.1.2 joerg ObjCPropertyAttribute::kind_nullability) { 1556 1.1 joerg if (auto nullability = AttributedType::stripOuterNullability(T)) { 1557 1.1 joerg if (*nullability == NullabilityKind::Unspecified && 1558 1.1 joerg (PDecl->getPropertyAttributes() & 1559 1.1.1.2 joerg ObjCPropertyAttribute::kind_null_resettable)) { 1560 1.1 joerg Out << (first ? "" : ", ") << "null_resettable"; 1561 1.1 joerg } else { 1562 1.1 joerg Out << (first ? "" : ", ") 1563 1.1 joerg << getNullabilitySpelling(*nullability, true); 1564 1.1 joerg } 1565 1.1 joerg first = false; 1566 1.1 joerg } 1567 1.1 joerg } 1568 1.1 joerg 1569 1.1 joerg (void) first; // Silence dead store warning due to idiomatic code. 1570 1.1 joerg Out << ")"; 1571 1.1 joerg } 1572 1.1 joerg std::string TypeStr = PDecl->getASTContext().getUnqualifiedObjCPointerType(T). 1573 1.1 joerg getAsString(Policy); 1574 1.1 joerg Out << ' ' << TypeStr; 1575 1.1 joerg if (!StringRef(TypeStr).endswith("*")) 1576 1.1 joerg Out << ' '; 1577 1.1 joerg Out << *PDecl; 1578 1.1 joerg if (Policy.PolishForDeclaration) 1579 1.1 joerg Out << ';'; 1580 1.1 joerg } 1581 1.1 joerg 1582 1.1 joerg void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { 1583 1.1 joerg if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) 1584 1.1 joerg Out << "@synthesize "; 1585 1.1 joerg else 1586 1.1 joerg Out << "@dynamic "; 1587 1.1 joerg Out << *PID->getPropertyDecl(); 1588 1.1 joerg if (PID->getPropertyIvarDecl()) 1589 1.1 joerg Out << '=' << *PID->getPropertyIvarDecl(); 1590 1.1 joerg } 1591 1.1 joerg 1592 1.1 joerg void DeclPrinter::VisitUsingDecl(UsingDecl *D) { 1593 1.1 joerg if (!D->isAccessDeclaration()) 1594 1.1 joerg Out << "using "; 1595 1.1 joerg if (D->hasTypename()) 1596 1.1 joerg Out << "typename "; 1597 1.1 joerg D->getQualifier()->print(Out, Policy); 1598 1.1 joerg 1599 1.1 joerg // Use the correct record name when the using declaration is used for 1600 1.1 joerg // inheriting constructors. 1601 1.1 joerg for (const auto *Shadow : D->shadows()) { 1602 1.1 joerg if (const auto *ConstructorShadow = 1603 1.1 joerg dyn_cast<ConstructorUsingShadowDecl>(Shadow)) { 1604 1.1 joerg assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext()); 1605 1.1 joerg Out << *ConstructorShadow->getNominatedBaseClass(); 1606 1.1 joerg return; 1607 1.1 joerg } 1608 1.1 joerg } 1609 1.1 joerg Out << *D; 1610 1.1 joerg } 1611 1.1 joerg 1612 1.1 joerg void 1613 1.1 joerg DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) { 1614 1.1 joerg Out << "using typename "; 1615 1.1 joerg D->getQualifier()->print(Out, Policy); 1616 1.1 joerg Out << D->getDeclName(); 1617 1.1 joerg } 1618 1.1 joerg 1619 1.1 joerg void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { 1620 1.1 joerg if (!D->isAccessDeclaration()) 1621 1.1 joerg Out << "using "; 1622 1.1 joerg D->getQualifier()->print(Out, Policy); 1623 1.1 joerg Out << D->getDeclName(); 1624 1.1 joerg } 1625 1.1 joerg 1626 1.1 joerg void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) { 1627 1.1 joerg // ignore 1628 1.1 joerg } 1629 1.1 joerg 1630 1.1 joerg void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) { 1631 1.1 joerg Out << "#pragma omp threadprivate"; 1632 1.1 joerg if (!D->varlist_empty()) { 1633 1.1 joerg for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(), 1634 1.1 joerg E = D->varlist_end(); 1635 1.1 joerg I != E; ++I) { 1636 1.1 joerg Out << (I == D->varlist_begin() ? '(' : ','); 1637 1.1 joerg NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl(); 1638 1.1 joerg ND->printQualifiedName(Out); 1639 1.1 joerg } 1640 1.1 joerg Out << ")"; 1641 1.1 joerg } 1642 1.1 joerg } 1643 1.1 joerg 1644 1.1 joerg void DeclPrinter::VisitOMPAllocateDecl(OMPAllocateDecl *D) { 1645 1.1 joerg Out << "#pragma omp allocate"; 1646 1.1 joerg if (!D->varlist_empty()) { 1647 1.1 joerg for (OMPAllocateDecl::varlist_iterator I = D->varlist_begin(), 1648 1.1 joerg E = D->varlist_end(); 1649 1.1 joerg I != E; ++I) { 1650 1.1 joerg Out << (I == D->varlist_begin() ? '(' : ','); 1651 1.1 joerg NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl(); 1652 1.1 joerg ND->printQualifiedName(Out); 1653 1.1 joerg } 1654 1.1 joerg Out << ")"; 1655 1.1 joerg } 1656 1.1 joerg if (!D->clauselist_empty()) { 1657 1.1 joerg Out << " "; 1658 1.1 joerg OMPClausePrinter Printer(Out, Policy); 1659 1.1 joerg for (OMPClause *C : D->clauselists()) 1660 1.1 joerg Printer.Visit(C); 1661 1.1 joerg } 1662 1.1 joerg } 1663 1.1 joerg 1664 1.1 joerg void DeclPrinter::VisitOMPRequiresDecl(OMPRequiresDecl *D) { 1665 1.1 joerg Out << "#pragma omp requires "; 1666 1.1 joerg if (!D->clauselist_empty()) { 1667 1.1 joerg OMPClausePrinter Printer(Out, Policy); 1668 1.1 joerg for (auto I = D->clauselist_begin(), E = D->clauselist_end(); I != E; ++I) 1669 1.1 joerg Printer.Visit(*I); 1670 1.1 joerg } 1671 1.1 joerg } 1672 1.1 joerg 1673 1.1 joerg void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) { 1674 1.1 joerg if (!D->isInvalidDecl()) { 1675 1.1 joerg Out << "#pragma omp declare reduction ("; 1676 1.1 joerg if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) { 1677 1.1 joerg const char *OpName = 1678 1.1 joerg getOperatorSpelling(D->getDeclName().getCXXOverloadedOperator()); 1679 1.1 joerg assert(OpName && "not an overloaded operator"); 1680 1.1 joerg Out << OpName; 1681 1.1 joerg } else { 1682 1.1 joerg assert(D->getDeclName().isIdentifier()); 1683 1.1 joerg D->printName(Out); 1684 1.1 joerg } 1685 1.1 joerg Out << " : "; 1686 1.1 joerg D->getType().print(Out, Policy); 1687 1.1 joerg Out << " : "; 1688 1.1.1.2 joerg D->getCombiner()->printPretty(Out, nullptr, Policy, 0, "\n", &Context); 1689 1.1 joerg Out << ")"; 1690 1.1 joerg if (auto *Init = D->getInitializer()) { 1691 1.1 joerg Out << " initializer("; 1692 1.1 joerg switch (D->getInitializerKind()) { 1693 1.1 joerg case OMPDeclareReductionDecl::DirectInit: 1694 1.1 joerg Out << "omp_priv("; 1695 1.1 joerg break; 1696 1.1 joerg case OMPDeclareReductionDecl::CopyInit: 1697 1.1 joerg Out << "omp_priv = "; 1698 1.1 joerg break; 1699 1.1 joerg case OMPDeclareReductionDecl::CallInit: 1700 1.1 joerg break; 1701 1.1 joerg } 1702 1.1.1.2 joerg Init->printPretty(Out, nullptr, Policy, 0, "\n", &Context); 1703 1.1 joerg if (D->getInitializerKind() == OMPDeclareReductionDecl::DirectInit) 1704 1.1 joerg Out << ")"; 1705 1.1 joerg Out << ")"; 1706 1.1 joerg } 1707 1.1 joerg } 1708 1.1 joerg } 1709 1.1 joerg 1710 1.1 joerg void DeclPrinter::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) { 1711 1.1 joerg if (!D->isInvalidDecl()) { 1712 1.1 joerg Out << "#pragma omp declare mapper ("; 1713 1.1 joerg D->printName(Out); 1714 1.1 joerg Out << " : "; 1715 1.1 joerg D->getType().print(Out, Policy); 1716 1.1 joerg Out << " "; 1717 1.1 joerg Out << D->getVarName(); 1718 1.1 joerg Out << ")"; 1719 1.1 joerg if (!D->clauselist_empty()) { 1720 1.1 joerg OMPClausePrinter Printer(Out, Policy); 1721 1.1 joerg for (auto *C : D->clauselists()) { 1722 1.1 joerg Out << " "; 1723 1.1 joerg Printer.Visit(C); 1724 1.1 joerg } 1725 1.1 joerg } 1726 1.1 joerg } 1727 1.1 joerg } 1728 1.1 joerg 1729 1.1 joerg void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) { 1730 1.1.1.2 joerg D->getInit()->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context); 1731 1.1 joerg } 1732 1.1 joerg 1733 1.1.1.2 joerg void DeclPrinter::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *TTP) { 1734 1.1.1.2 joerg if (const TypeConstraint *TC = TTP->getTypeConstraint()) 1735 1.1.1.2 joerg TC->print(Out, Policy); 1736 1.1.1.2 joerg else if (TTP->wasDeclaredWithTypename()) 1737 1.1.1.2 joerg Out << "typename"; 1738 1.1.1.2 joerg else 1739 1.1.1.2 joerg Out << "class"; 1740 1.1.1.2 joerg 1741 1.1.1.2 joerg if (TTP->isParameterPack()) 1742 1.1.1.2 joerg Out << " ..."; 1743 1.1.1.2 joerg else if (TTP->getDeclName()) 1744 1.1.1.2 joerg Out << ' '; 1745 1.1.1.2 joerg 1746 1.1.1.2 joerg if (TTP->getDeclName()) 1747 1.1.1.2 joerg Out << TTP->getDeclName(); 1748 1.1.1.2 joerg 1749 1.1.1.2 joerg if (TTP->hasDefaultArgument()) { 1750 1.1.1.2 joerg Out << " = "; 1751 1.1.1.2 joerg Out << TTP->getDefaultArgument().getAsString(Policy); 1752 1.1.1.2 joerg } 1753 1.1.1.2 joerg } 1754 1.1.1.2 joerg 1755 1.1.1.2 joerg void DeclPrinter::VisitNonTypeTemplateParmDecl( 1756 1.1.1.2 joerg const NonTypeTemplateParmDecl *NTTP) { 1757 1.1.1.2 joerg StringRef Name; 1758 1.1.1.2 joerg if (IdentifierInfo *II = NTTP->getIdentifier()) 1759 1.1.1.2 joerg Name = II->getName(); 1760 1.1.1.2 joerg printDeclType(NTTP->getType(), Name, NTTP->isParameterPack()); 1761 1.1.1.2 joerg 1762 1.1.1.2 joerg if (NTTP->hasDefaultArgument()) { 1763 1.1.1.2 joerg Out << " = "; 1764 1.1.1.2 joerg NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy, Indentation, 1765 1.1.1.2 joerg "\n", &Context); 1766 1.1.1.2 joerg } 1767 1.1.1.2 joerg } 1768