clang  10.0.0svn
DeclPrinter.cpp
Go to the documentation of this file.
1 //===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the Decl::print method, which pretty prints the
10 // AST back out to C/Objective-C/C++/Objective-C++ code.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "clang/AST/ASTContext.h"
14 #include "clang/AST/Attr.h"
15 #include "clang/AST/Decl.h"
16 #include "clang/AST/DeclCXX.h"
17 #include "clang/AST/DeclObjC.h"
18 #include "clang/AST/DeclTemplate.h"
19 #include "clang/AST/DeclVisitor.h"
20 #include "clang/AST/Expr.h"
21 #include "clang/AST/ExprCXX.h"
23 #include "clang/Basic/Module.h"
24 #include "llvm/Support/raw_ostream.h"
25 using namespace clang;
26 
27 namespace {
28  class DeclPrinter : public DeclVisitor<DeclPrinter> {
29  raw_ostream &Out;
30  PrintingPolicy Policy;
31  const ASTContext &Context;
32  unsigned Indentation;
33  bool PrintInstantiation;
34 
35  raw_ostream& Indent() { return Indent(Indentation); }
36  raw_ostream& Indent(unsigned Indentation);
37  void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls);
38 
39  void Print(AccessSpecifier AS);
40  void PrintConstructorInitializers(CXXConstructorDecl *CDecl,
41  std::string &Proto);
42 
43  /// Print an Objective-C method type in parentheses.
44  ///
45  /// \param Quals The Objective-C declaration qualifiers.
46  /// \param T The type to print.
47  void PrintObjCMethodType(ASTContext &Ctx, Decl::ObjCDeclQualifier Quals,
48  QualType T);
49 
50  void PrintObjCTypeParams(ObjCTypeParamList *Params);
51 
52  public:
53  DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy,
54  const ASTContext &Context, unsigned Indentation = 0,
55  bool PrintInstantiation = false)
56  : Out(Out), Policy(Policy), Context(Context), Indentation(Indentation),
57  PrintInstantiation(PrintInstantiation) {}
58 
59  void VisitDeclContext(DeclContext *DC, bool Indent = true);
60 
61  void VisitTranslationUnitDecl(TranslationUnitDecl *D);
62  void VisitTypedefDecl(TypedefDecl *D);
63  void VisitTypeAliasDecl(TypeAliasDecl *D);
64  void VisitEnumDecl(EnumDecl *D);
65  void VisitRecordDecl(RecordDecl *D);
66  void VisitEnumConstantDecl(EnumConstantDecl *D);
67  void VisitEmptyDecl(EmptyDecl *D);
68  void VisitFunctionDecl(FunctionDecl *D);
69  void VisitFriendDecl(FriendDecl *D);
70  void VisitFieldDecl(FieldDecl *D);
71  void VisitVarDecl(VarDecl *D);
72  void VisitLabelDecl(LabelDecl *D);
73  void VisitParmVarDecl(ParmVarDecl *D);
74  void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
75  void VisitImportDecl(ImportDecl *D);
76  void VisitStaticAssertDecl(StaticAssertDecl *D);
77  void VisitNamespaceDecl(NamespaceDecl *D);
78  void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
79  void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
80  void VisitCXXRecordDecl(CXXRecordDecl *D);
81  void VisitLinkageSpecDecl(LinkageSpecDecl *D);
82  void VisitTemplateDecl(const TemplateDecl *D);
83  void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
84  void VisitClassTemplateDecl(ClassTemplateDecl *D);
85  void VisitClassTemplateSpecializationDecl(
87  void VisitClassTemplatePartialSpecializationDecl(
89  void VisitObjCMethodDecl(ObjCMethodDecl *D);
90  void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
91  void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
92  void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
93  void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
94  void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
95  void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
96  void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
97  void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
98  void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
99  void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
100  void VisitUsingDecl(UsingDecl *D);
101  void VisitUsingShadowDecl(UsingShadowDecl *D);
102  void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
103  void VisitOMPAllocateDecl(OMPAllocateDecl *D);
104  void VisitOMPRequiresDecl(OMPRequiresDecl *D);
105  void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
106  void VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D);
107  void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
108 
109  void printTemplateParameters(const TemplateParameterList *Params,
110  bool OmitTemplateKW = false);
111  void printTemplateArguments(const TemplateArgumentList &Args,
112  const TemplateParameterList *Params = nullptr);
113  void prettyPrintAttributes(Decl *D);
114  void prettyPrintPragmas(Decl *D);
115  void printDeclType(QualType T, StringRef DeclName, bool Pack = false);
116  };
117 }
118 
119 void Decl::print(raw_ostream &Out, unsigned Indentation,
120  bool PrintInstantiation) const {
121  print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
122 }
123 
124 void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
125  unsigned Indentation, bool PrintInstantiation) const {
126  DeclPrinter Printer(Out, Policy, getASTContext(), Indentation,
127  PrintInstantiation);
128  Printer.Visit(const_cast<Decl*>(this));
129 }
130 
131 void TemplateParameterList::print(raw_ostream &Out, const ASTContext &Context,
132  bool OmitTemplateKW) const {
133  print(Out, Context, Context.getPrintingPolicy(), OmitTemplateKW);
134 }
135 
136 void TemplateParameterList::print(raw_ostream &Out, const ASTContext &Context,
137  const PrintingPolicy &Policy,
138  bool OmitTemplateKW) const {
139  DeclPrinter Printer(Out, Policy, Context);
140  Printer.printTemplateParameters(this, OmitTemplateKW);
141 }
142 
144  // FIXME: This should be on the Type class!
145  QualType BaseType = T;
146  while (!BaseType->isSpecifierType()) {
147  if (const PointerType *PTy = BaseType->getAs<PointerType>())
148  BaseType = PTy->getPointeeType();
149  else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
150  BaseType = BPy->getPointeeType();
151  else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
152  BaseType = ATy->getElementType();
153  else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
154  BaseType = FTy->getReturnType();
155  else if (const VectorType *VTy = BaseType->getAs<VectorType>())
156  BaseType = VTy->getElementType();
157  else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
158  BaseType = RTy->getPointeeType();
159  else if (const AutoType *ATy = BaseType->getAs<AutoType>())
160  BaseType = ATy->getDeducedType();
161  else if (const ParenType *PTy = BaseType->getAs<ParenType>())
162  BaseType = PTy->desugar();
163  else
164  // This must be a syntax error.
165  break;
166  }
167  return BaseType;
168 }
169 
171  if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
172  return TDD->getUnderlyingType();
173  if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
174  return VD->getType();
175  return QualType();
176 }
177 
178 void Decl::printGroup(Decl** Begin, unsigned NumDecls,
179  raw_ostream &Out, const PrintingPolicy &Policy,
180  unsigned Indentation) {
181  if (NumDecls == 1) {
182  (*Begin)->print(Out, Policy, Indentation);
183  return;
184  }
185 
186  Decl** End = Begin + NumDecls;
187  TagDecl* TD = dyn_cast<TagDecl>(*Begin);
188  if (TD)
189  ++Begin;
190 
191  PrintingPolicy SubPolicy(Policy);
192 
193  bool isFirst = true;
194  for ( ; Begin != End; ++Begin) {
195  if (isFirst) {
196  if(TD)
197  SubPolicy.IncludeTagDefinition = true;
198  SubPolicy.SuppressSpecifiers = false;
199  isFirst = false;
200  } else {
201  if (!isFirst) Out << ", ";
202  SubPolicy.IncludeTagDefinition = false;
203  SubPolicy.SuppressSpecifiers = true;
204  }
205 
206  (*Begin)->print(Out, SubPolicy, Indentation);
207  }
208 }
209 
210 LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
211  // Get the translation unit
212  const DeclContext *DC = this;
213  while (!DC->isTranslationUnit())
214  DC = DC->getParent();
215 
216  ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
217  DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), Ctx, 0);
218  Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
219 }
220 
221 raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
222  for (unsigned i = 0; i != Indentation; ++i)
223  Out << " ";
224  return Out;
225 }
226 
227 void DeclPrinter::prettyPrintAttributes(Decl *D) {
228  if (Policy.PolishForDeclaration)
229  return;
230 
231  if (D->hasAttrs()) {
232  AttrVec &Attrs = D->getAttrs();
233  for (auto *A : Attrs) {
234  if (A->isInherited() || A->isImplicit())
235  continue;
236  switch (A->getKind()) {
237 #define ATTR(X)
238 #define PRAGMA_SPELLING_ATTR(X) case attr::X:
239 #include "clang/Basic/AttrList.inc"
240  break;
241  default:
242  A->printPretty(Out, Policy);
243  break;
244  }
245  }
246  }
247 }
248 
249 void DeclPrinter::prettyPrintPragmas(Decl *D) {
250  if (Policy.PolishForDeclaration)
251  return;
252 
253  if (D->hasAttrs()) {
254  AttrVec &Attrs = D->getAttrs();
255  for (auto *A : Attrs) {
256  switch (A->getKind()) {
257 #define ATTR(X)
258 #define PRAGMA_SPELLING_ATTR(X) case attr::X:
259 #include "clang/Basic/AttrList.inc"
260  A->printPretty(Out, Policy);
261  Indent();
262  break;
263  default:
264  break;
265  }
266  }
267  }
268 }
269 
270 void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) {
271  // Normally, a PackExpansionType is written as T[3]... (for instance, as a
272  // template argument), but if it is the type of a declaration, the ellipsis
273  // is placed before the name being declared.
274  if (auto *PET = T->getAs<PackExpansionType>()) {
275  Pack = true;
276  T = PET->getPattern();
277  }
278  T.print(Out, Policy, (Pack ? "..." : "") + DeclName, Indentation);
279 }
280 
281 void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
282  this->Indent();
283  Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
284  Out << ";\n";
285  Decls.clear();
286 
287 }
288 
289 void DeclPrinter::Print(AccessSpecifier AS) {
290  switch(AS) {
291  case AS_none: llvm_unreachable("No access specifier!");
292  case AS_public: Out << "public"; break;
293  case AS_protected: Out << "protected"; break;
294  case AS_private: Out << "private"; break;
295  }
296 }
297 
298 void DeclPrinter::PrintConstructorInitializers(CXXConstructorDecl *CDecl,
299  std::string &Proto) {
300  bool HasInitializerList = false;
301  for (const auto *BMInitializer : CDecl->inits()) {
302  if (BMInitializer->isInClassMemberInitializer())
303  continue;
304 
305  if (!HasInitializerList) {
306  Proto += " : ";
307  Out << Proto;
308  Proto.clear();
309  HasInitializerList = true;
310  } else
311  Out << ", ";
312 
313  if (BMInitializer->isAnyMemberInitializer()) {
314  FieldDecl *FD = BMInitializer->getAnyMember();
315  Out << *FD;
316  } else {
317  Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
318  }
319 
320  Out << "(";
321  if (!BMInitializer->getInit()) {
322  // Nothing to print
323  } else {
324  Expr *Init = BMInitializer->getInit();
325  if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
326  Init = Tmp->getSubExpr();
327 
328  Init = Init->IgnoreParens();
329 
330  Expr *SimpleInit = nullptr;
331  Expr **Args = nullptr;
332  unsigned NumArgs = 0;
333  if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
334  Args = ParenList->getExprs();
335  NumArgs = ParenList->getNumExprs();
336  } else if (CXXConstructExpr *Construct =
337  dyn_cast<CXXConstructExpr>(Init)) {
338  Args = Construct->getArgs();
339  NumArgs = Construct->getNumArgs();
340  } else
341  SimpleInit = Init;
342 
343  if (SimpleInit)
344  SimpleInit->printPretty(Out, nullptr, Policy, Indentation);
345  else {
346  for (unsigned I = 0; I != NumArgs; ++I) {
347  assert(Args[I] != nullptr && "Expected non-null Expr");
348  if (isa<CXXDefaultArgExpr>(Args[I]))
349  break;
350 
351  if (I)
352  Out << ", ";
353  Args[I]->printPretty(Out, nullptr, Policy, Indentation);
354  }
355  }
356  }
357  Out << ")";
358  if (BMInitializer->isPackExpansion())
359  Out << "...";
360  }
361 }
362 
363 //----------------------------------------------------------------------------
364 // Common C declarations
365 //----------------------------------------------------------------------------
366 
367 void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
368  if (Policy.TerseOutput)
369  return;
370 
371  if (Indent)
372  Indentation += Policy.Indentation;
373 
374  SmallVector<Decl*, 2> Decls;
375  for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
376  D != DEnd; ++D) {
377 
378  // Don't print ObjCIvarDecls, as they are printed when visiting the
379  // containing ObjCInterfaceDecl.
380  if (isa<ObjCIvarDecl>(*D))
381  continue;
382 
383  // Skip over implicit declarations in pretty-printing mode.
384  if (D->isImplicit())
385  continue;
386 
387  // Don't print implicit specializations, as they are printed when visiting
388  // corresponding templates.
389  if (auto FD = dyn_cast<FunctionDecl>(*D))
390  if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation &&
391  !isa<ClassTemplateSpecializationDecl>(DC))
392  continue;
393 
394  // The next bits of code handle stuff like "struct {int x;} a,b"; we're
395  // forced to merge the declarations because there's no other way to
396  // refer to the struct in question. When that struct is named instead, we
397  // also need to merge to avoid splitting off a stand-alone struct
398  // declaration that produces the warning ext_no_declarators in some
399  // contexts.
400  //
401  // This limited merging is safe without a bunch of other checks because it
402  // only merges declarations directly referring to the tag, not typedefs.
403  //
404  // Check whether the current declaration should be grouped with a previous
405  // non-free-standing tag declaration.
406  QualType CurDeclType = getDeclType(*D);
407  if (!Decls.empty() && !CurDeclType.isNull()) {
408  QualType BaseType = GetBaseType(CurDeclType);
409  if (!BaseType.isNull() && isa<ElaboratedType>(BaseType) &&
410  cast<ElaboratedType>(BaseType)->getOwnedTagDecl() == Decls[0]) {
411  Decls.push_back(*D);
412  continue;
413  }
414  }
415 
416  // If we have a merged group waiting to be handled, handle it now.
417  if (!Decls.empty())
418  ProcessDeclGroup(Decls);
419 
420  // If the current declaration is not a free standing declaration, save it
421  // so we can merge it with the subsequent declaration(s) using it.
422  if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->isFreeStanding()) {
423  Decls.push_back(*D);
424  continue;
425  }
426 
427  if (isa<AccessSpecDecl>(*D)) {
428  Indentation -= Policy.Indentation;
429  this->Indent();
430  Print(D->getAccess());
431  Out << ":\n";
432  Indentation += Policy.Indentation;
433  continue;
434  }
435 
436  this->Indent();
437  Visit(*D);
438 
439  // FIXME: Need to be able to tell the DeclPrinter when
440  const char *Terminator = nullptr;
441  if (isa<OMPThreadPrivateDecl>(*D) || isa<OMPDeclareReductionDecl>(*D) ||
442  isa<OMPDeclareMapperDecl>(*D) || isa<OMPRequiresDecl>(*D) ||
443  isa<OMPAllocateDecl>(*D))
444  Terminator = nullptr;
445  else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->hasBody())
446  Terminator = nullptr;
447  else if (auto FD = dyn_cast<FunctionDecl>(*D)) {
448  if (FD->isThisDeclarationADefinition())
449  Terminator = nullptr;
450  else
451  Terminator = ";";
452  } else if (auto TD = dyn_cast<FunctionTemplateDecl>(*D)) {
453  if (TD->getTemplatedDecl()->isThisDeclarationADefinition())
454  Terminator = nullptr;
455  else
456  Terminator = ";";
457  } else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
458  isa<ObjCImplementationDecl>(*D) ||
459  isa<ObjCInterfaceDecl>(*D) ||
460  isa<ObjCProtocolDecl>(*D) ||
461  isa<ObjCCategoryImplDecl>(*D) ||
462  isa<ObjCCategoryDecl>(*D))
463  Terminator = nullptr;
464  else if (isa<EnumConstantDecl>(*D)) {
466  ++Next;
467  if (Next != DEnd)
468  Terminator = ",";
469  } else
470  Terminator = ";";
471 
472  if (Terminator)
473  Out << Terminator;
474  if (!Policy.TerseOutput &&
475  ((isa<FunctionDecl>(*D) &&
476  cast<FunctionDecl>(*D)->doesThisDeclarationHaveABody()) ||
477  (isa<FunctionTemplateDecl>(*D) &&
478  cast<FunctionTemplateDecl>(*D)->getTemplatedDecl()->doesThisDeclarationHaveABody())))
479  ; // StmtPrinter already added '\n' after CompoundStmt.
480  else
481  Out << "\n";
482 
483  // Declare target attribute is special one, natural spelling for the pragma
484  // assumes "ending" construct so print it here.
485  if (D->hasAttr<OMPDeclareTargetDeclAttr>())
486  Out << "#pragma omp end declare target\n";
487  }
488 
489  if (!Decls.empty())
490  ProcessDeclGroup(Decls);
491 
492  if (Indent)
493  Indentation -= Policy.Indentation;
494 }
495 
496 void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
497  VisitDeclContext(D, false);
498 }
499 
500 void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
501  if (!Policy.SuppressSpecifiers) {
502  Out << "typedef ";
503 
504  if (D->isModulePrivate())
505  Out << "__module_private__ ";
506  }
507  QualType Ty = D->getTypeSourceInfo()->getType();
508  Ty.print(Out, Policy, D->getName(), Indentation);
509  prettyPrintAttributes(D);
510 }
511 
512 void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
513  Out << "using " << *D;
514  prettyPrintAttributes(D);
515  Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
516 }
517 
518 void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
519  if (!Policy.SuppressSpecifiers && D->isModulePrivate())
520  Out << "__module_private__ ";
521  Out << "enum";
522  if (D->isScoped()) {
523  if (D->isScopedUsingClassTag())
524  Out << " class";
525  else
526  Out << " struct";
527  }
528 
529  prettyPrintAttributes(D);
530 
531  Out << ' ' << *D;
532 
533  if (D->isFixed() && D->getASTContext().getLangOpts().CPlusPlus11)
534  Out << " : " << D->getIntegerType().stream(Policy);
535 
536  if (D->isCompleteDefinition()) {
537  Out << " {\n";
538  VisitDeclContext(D);
539  Indent() << "}";
540  }
541 }
542 
543 void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
544  if (!Policy.SuppressSpecifiers && D->isModulePrivate())
545  Out << "__module_private__ ";
546  Out << D->getKindName();
547 
548  prettyPrintAttributes(D);
549 
550  if (D->getIdentifier())
551  Out << ' ' << *D;
552 
553  if (D->isCompleteDefinition()) {
554  Out << " {\n";
555  VisitDeclContext(D);
556  Indent() << "}";
557  }
558 }
559 
560 void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
561  Out << *D;
562  prettyPrintAttributes(D);
563  if (Expr *Init = D->getInitExpr()) {
564  Out << " = ";
565  Init->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context);
566  }
567 }
568 
569 static void printExplicitSpecifier(ExplicitSpecifier ES, llvm::raw_ostream &Out,
570  PrintingPolicy &Policy,
571  unsigned Indentation) {
572  std::string Proto = "explicit";
573  llvm::raw_string_ostream EOut(Proto);
574  if (ES.getExpr()) {
575  EOut << "(";
576  ES.getExpr()->printPretty(EOut, nullptr, Policy, Indentation);
577  EOut << ")";
578  }
579  EOut << " ";
580  EOut.flush();
581  Out << EOut.str();
582 }
583 
584 void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
585  if (!D->getDescribedFunctionTemplate() &&
587  prettyPrintPragmas(D);
588 
590  Out << "template<> ";
591  else if (!D->getDescribedFunctionTemplate()) {
592  for (unsigned I = 0, NumTemplateParams = D->getNumTemplateParameterLists();
593  I < NumTemplateParams; ++I)
594  printTemplateParameters(D->getTemplateParameterList(I));
595  }
596 
597  CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
598  CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
599  CXXDeductionGuideDecl *GuideDecl = dyn_cast<CXXDeductionGuideDecl>(D);
600  if (!Policy.SuppressSpecifiers) {
601  switch (D->getStorageClass()) {
602  case SC_None: break;
603  case SC_Extern: Out << "extern "; break;
604  case SC_Static: Out << "static "; break;
605  case SC_PrivateExtern: Out << "__private_extern__ "; break;
606  case SC_Auto: case SC_Register:
607  llvm_unreachable("invalid for functions");
608  }
609 
610  if (D->isInlineSpecified()) Out << "inline ";
611  if (D->isVirtualAsWritten()) Out << "virtual ";
612  if (D->isModulePrivate()) Out << "__module_private__ ";
614  Out << "constexpr ";
615  if (D->isConsteval()) Out << "consteval ";
617  if (ExplicitSpec.isSpecified())
618  printExplicitSpecifier(ExplicitSpec, Out, Policy, Indentation);
619  }
620 
621  PrintingPolicy SubPolicy(Policy);
622  SubPolicy.SuppressSpecifiers = false;
623  std::string Proto;
624 
625  if (Policy.FullyQualifiedName) {
626  Proto += D->getQualifiedNameAsString();
627  } else {
628  if (!Policy.SuppressScope) {
629  if (const NestedNameSpecifier *NS = D->getQualifier()) {
630  llvm::raw_string_ostream OS(Proto);
631  NS->print(OS, Policy);
632  }
633  }
634  Proto += D->getNameInfo().getAsString();
635  }
636 
637  if (GuideDecl)
638  Proto = GuideDecl->getDeducedTemplate()->getDeclName().getAsString();
639  if (const TemplateArgumentList *TArgs = D->getTemplateSpecializationArgs()) {
640  llvm::raw_string_ostream POut(Proto);
641  DeclPrinter TArgPrinter(POut, SubPolicy, Context, Indentation);
642  TArgPrinter.printTemplateArguments(*TArgs);
643  }
644 
645  QualType Ty = D->getType();
646  while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
647  Proto = '(' + Proto + ')';
648  Ty = PT->getInnerType();
649  }
650 
651  if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
652  const FunctionProtoType *FT = nullptr;
653  if (D->hasWrittenPrototype())
654  FT = dyn_cast<FunctionProtoType>(AFT);
655 
656  Proto += "(";
657  if (FT) {
658  llvm::raw_string_ostream POut(Proto);
659  DeclPrinter ParamPrinter(POut, SubPolicy, Context, Indentation);
660  for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
661  if (i) POut << ", ";
662  ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
663  }
664 
665  if (FT->isVariadic()) {
666  if (D->getNumParams()) POut << ", ";
667  POut << "...";
668  }
669  } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
670  for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
671  if (i)
672  Proto += ", ";
673  Proto += D->getParamDecl(i)->getNameAsString();
674  }
675  }
676 
677  Proto += ")";
678 
679  if (FT) {
680  if (FT->isConst())
681  Proto += " const";
682  if (FT->isVolatile())
683  Proto += " volatile";
684  if (FT->isRestrict())
685  Proto += " restrict";
686 
687  switch (FT->getRefQualifier()) {
688  case RQ_None:
689  break;
690  case RQ_LValue:
691  Proto += " &";
692  break;
693  case RQ_RValue:
694  Proto += " &&";
695  break;
696  }
697  }
698 
699  if (FT && FT->hasDynamicExceptionSpec()) {
700  Proto += " throw(";
701  if (FT->getExceptionSpecType() == EST_MSAny)
702  Proto += "...";
703  else
704  for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
705  if (I)
706  Proto += ", ";
707 
708  Proto += FT->getExceptionType(I).getAsString(SubPolicy);
709  }
710  Proto += ")";
711  } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
712  Proto += " noexcept";
714  Proto += "(";
715  llvm::raw_string_ostream EOut(Proto);
716  FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
717  Indentation);
718  EOut.flush();
719  Proto += EOut.str();
720  Proto += ")";
721  }
722  }
723 
724  if (CDecl) {
725  if (!Policy.TerseOutput)
726  PrintConstructorInitializers(CDecl, Proto);
727  } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) {
728  if (FT && FT->hasTrailingReturn()) {
729  if (!GuideDecl)
730  Out << "auto ";
731  Out << Proto << " -> ";
732  Proto.clear();
733  }
734  AFT->getReturnType().print(Out, Policy, Proto);
735  Proto.clear();
736  }
737  Out << Proto;
738  } else {
739  Ty.print(Out, Policy, Proto);
740  }
741 
742  prettyPrintAttributes(D);
743 
744  if (D->isPure())
745  Out << " = 0";
746  else if (D->isDeletedAsWritten())
747  Out << " = delete";
748  else if (D->isExplicitlyDefaulted())
749  Out << " = default";
750  else if (D->doesThisDeclarationHaveABody()) {
751  if (!Policy.TerseOutput) {
752  if (!D->hasPrototype() && D->getNumParams()) {
753  // This is a K&R function definition, so we need to print the
754  // parameters.
755  Out << '\n';
756  DeclPrinter ParamPrinter(Out, SubPolicy, Context, Indentation);
757  Indentation += Policy.Indentation;
758  for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
759  Indent();
760  ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
761  Out << ";\n";
762  }
763  Indentation -= Policy.Indentation;
764  } else
765  Out << ' ';
766 
767  if (D->getBody())
768  D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation);
769  } else {
770  if (!Policy.TerseOutput && isa<CXXConstructorDecl>(*D))
771  Out << " {}";
772  }
773  }
774 }
775 
776 void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
777  if (TypeSourceInfo *TSI = D->getFriendType()) {
778  unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
779  for (unsigned i = 0; i < NumTPLists; ++i)
780  printTemplateParameters(D->getFriendTypeTemplateParameterList(i));
781  Out << "friend ";
782  Out << " " << TSI->getType().getAsString(Policy);
783  }
784  else if (FunctionDecl *FD =
785  dyn_cast<FunctionDecl>(D->getFriendDecl())) {
786  Out << "friend ";
787  VisitFunctionDecl(FD);
788  }
789  else if (FunctionTemplateDecl *FTD =
790  dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
791  Out << "friend ";
792  VisitFunctionTemplateDecl(FTD);
793  }
794  else if (ClassTemplateDecl *CTD =
795  dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
796  Out << "friend ";
797  VisitRedeclarableTemplateDecl(CTD);
798  }
799 }
800 
801 void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
802  // FIXME: add printing of pragma attributes if required.
803  if (!Policy.SuppressSpecifiers && D->isMutable())
804  Out << "mutable ";
805  if (!Policy.SuppressSpecifiers && D->isModulePrivate())
806  Out << "__module_private__ ";
807 
809  stream(Policy, D->getName(), Indentation);
810 
811  if (D->isBitField()) {
812  Out << " : ";
813  D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation);
814  }
815 
816  Expr *Init = D->getInClassInitializer();
817  if (!Policy.SuppressInitializers && Init) {
819  Out << " ";
820  else
821  Out << " = ";
822  Init->printPretty(Out, nullptr, Policy, Indentation);
823  }
824  prettyPrintAttributes(D);
825 }
826 
827 void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
828  Out << *D << ":";
829 }
830 
831 void DeclPrinter::VisitVarDecl(VarDecl *D) {
832  prettyPrintPragmas(D);
833 
834  QualType T = D->getTypeSourceInfo()
835  ? D->getTypeSourceInfo()->getType()
837 
838  if (!Policy.SuppressSpecifiers) {
839  StorageClass SC = D->getStorageClass();
840  if (SC != SC_None)
841  Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
842 
843  switch (D->getTSCSpec()) {
844  case TSCS_unspecified:
845  break;
846  case TSCS___thread:
847  Out << "__thread ";
848  break;
849  case TSCS__Thread_local:
850  Out << "_Thread_local ";
851  break;
852  case TSCS_thread_local:
853  Out << "thread_local ";
854  break;
855  }
856 
857  if (D->isModulePrivate())
858  Out << "__module_private__ ";
859 
860  if (D->isConstexpr()) {
861  Out << "constexpr ";
862  T.removeLocalConst();
863  }
864  }
865 
866  printDeclType(T, D->getName());
867  Expr *Init = D->getInit();
868  if (!Policy.SuppressInitializers && Init) {
869  bool ImplicitInit = false;
870  if (CXXConstructExpr *Construct =
871  dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
872  if (D->getInitStyle() == VarDecl::CallInit &&
873  !Construct->isListInitialization()) {
874  ImplicitInit = Construct->getNumArgs() == 0 ||
875  Construct->getArg(0)->isDefaultArgument();
876  }
877  }
878  if (!ImplicitInit) {
879  if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
880  Out << "(";
881  else if (D->getInitStyle() == VarDecl::CInit) {
882  Out << " = ";
883  }
884  PrintingPolicy SubPolicy(Policy);
885  SubPolicy.SuppressSpecifiers = false;
886  SubPolicy.IncludeTagDefinition = false;
887  Init->printPretty(Out, nullptr, SubPolicy, Indentation);
888  if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init))
889  Out << ")";
890  }
891  }
892  prettyPrintAttributes(D);
893 }
894 
895 void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
896  VisitVarDecl(D);
897 }
898 
899 void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
900  Out << "__asm (";
901  D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation);
902  Out << ")";
903 }
904 
905 void DeclPrinter::VisitImportDecl(ImportDecl *D) {
906  Out << "@import " << D->getImportedModule()->getFullModuleName()
907  << ";\n";
908 }
909 
910 void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
911  Out << "static_assert(";
912  D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation);
913  if (StringLiteral *SL = D->getMessage()) {
914  Out << ", ";
915  SL->printPretty(Out, nullptr, Policy, Indentation);
916  }
917  Out << ")";
918 }
919 
920 //----------------------------------------------------------------------------
921 // C++ declarations
922 //----------------------------------------------------------------------------
923 void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
924  if (D->isInline())
925  Out << "inline ";
926  Out << "namespace " << *D << " {\n";
927  VisitDeclContext(D);
928  Indent() << "}";
929 }
930 
931 void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
932  Out << "using namespace ";
933  if (D->getQualifier())
934  D->getQualifier()->print(Out, Policy);
935  Out << *D->getNominatedNamespaceAsWritten();
936 }
937 
938 void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
939  Out << "namespace " << *D << " = ";
940  if (D->getQualifier())
941  D->getQualifier()->print(Out, Policy);
942  Out << *D->getAliasedNamespace();
943 }
944 
945 void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
946  prettyPrintAttributes(D);
947 }
948 
949 void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
950  // FIXME: add printing of pragma attributes if required.
951  if (!Policy.SuppressSpecifiers && D->isModulePrivate())
952  Out << "__module_private__ ";
953  Out << D->getKindName();
954 
955  prettyPrintAttributes(D);
956 
957  if (D->getIdentifier()) {
958  Out << ' ' << *D;
959 
960  if (auto S = dyn_cast<ClassTemplatePartialSpecializationDecl>(D))
961  printTemplateArguments(S->getTemplateArgs(), S->getTemplateParameters());
962  else if (auto S = dyn_cast<ClassTemplateSpecializationDecl>(D))
963  printTemplateArguments(S->getTemplateArgs());
964  }
965 
966  if (D->isCompleteDefinition()) {
967  // Print the base classes
968  if (D->getNumBases()) {
969  Out << " : ";
971  BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
972  if (Base != D->bases_begin())
973  Out << ", ";
974 
975  if (Base->isVirtual())
976  Out << "virtual ";
977 
978  AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
979  if (AS != AS_none) {
980  Print(AS);
981  Out << " ";
982  }
983  Out << Base->getType().getAsString(Policy);
984 
985  if (Base->isPackExpansion())
986  Out << "...";
987  }
988  }
989 
990  // Print the class definition
991  // FIXME: Doesn't print access specifiers, e.g., "public:"
992  if (Policy.TerseOutput) {
993  Out << " {}";
994  } else {
995  Out << " {\n";
996  VisitDeclContext(D);
997  Indent() << "}";
998  }
999  }
1000 }
1001 
1002 void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
1003  const char *l;
1005  l = "C";
1006  else {
1007  assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
1008  "unknown language in linkage specification");
1009  l = "C++";
1010  }
1011 
1012  Out << "extern \"" << l << "\" ";
1013  if (D->hasBraces()) {
1014  Out << "{\n";
1015  VisitDeclContext(D);
1016  Indent() << "}";
1017  } else
1018  Visit(*D->decls_begin());
1019 }
1020 
1021 void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params,
1022  bool OmitTemplateKW) {
1023  assert(Params);
1024 
1025  if (!OmitTemplateKW)
1026  Out << "template ";
1027  Out << '<';
1028 
1029  bool NeedComma = false;
1030  for (const Decl *Param : *Params) {
1031  if (Param->isImplicit())
1032  continue;
1033 
1034  if (NeedComma)
1035  Out << ", ";
1036  else
1037  NeedComma = true;
1038 
1039  if (auto TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
1040 
1041  if (TTP->wasDeclaredWithTypename())
1042  Out << "typename";
1043  else
1044  Out << "class";
1045 
1046  if (TTP->isParameterPack())
1047  Out << " ...";
1048  else if (!TTP->getName().empty())
1049  Out << ' ';
1050 
1051  Out << *TTP;
1052 
1053  if (TTP->hasDefaultArgument()) {
1054  Out << " = ";
1055  Out << TTP->getDefaultArgument().getAsString(Policy);
1056  };
1057  } else if (auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1058  StringRef Name;
1059  if (IdentifierInfo *II = NTTP->getIdentifier())
1060  Name = II->getName();
1061  printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
1062 
1063  if (NTTP->hasDefaultArgument()) {
1064  Out << " = ";
1065  NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy,
1066  Indentation);
1067  }
1068  } else if (auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) {
1069  VisitTemplateDecl(TTPD);
1070  // FIXME: print the default argument, if present.
1071  }
1072  }
1073 
1074  Out << '>';
1075  if (!OmitTemplateKW)
1076  Out << ' ';
1077 }
1078 
1079 void DeclPrinter::printTemplateArguments(const TemplateArgumentList &Args,
1080  const TemplateParameterList *Params) {
1081  Out << "<";
1082  for (size_t I = 0, E = Args.size(); I < E; ++I) {
1083  const TemplateArgument &A = Args[I];
1084  if (I)
1085  Out << ", ";
1086  if (Params) {
1087  if (A.getKind() == TemplateArgument::Type)
1088  if (auto T = A.getAsType()->getAs<TemplateTypeParmType>()) {
1089  auto P = cast<TemplateTypeParmDecl>(Params->getParam(T->getIndex()));
1090  Out << *P;
1091  continue;
1092  }
1093  if (A.getKind() == TemplateArgument::Template) {
1094  if (auto T = A.getAsTemplate().getAsTemplateDecl())
1095  if (auto TD = dyn_cast<TemplateTemplateParmDecl>(T)) {
1096  auto P = cast<TemplateTemplateParmDecl>(
1097  Params->getParam(TD->getIndex()));
1098  Out << *P;
1099  continue;
1100  }
1101  }
1102  if (A.getKind() == TemplateArgument::Expression) {
1103  if (auto E = dyn_cast<DeclRefExpr>(A.getAsExpr()))
1104  if (auto N = dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1105  auto P = cast<NonTypeTemplateParmDecl>(
1106  Params->getParam(N->getIndex()));
1107  Out << *P;
1108  continue;
1109  }
1110  }
1111  }
1112  A.print(Policy, Out);
1113  }
1114  Out << ">";
1115 }
1116 
1117 void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
1118  printTemplateParameters(D->getTemplateParameters());
1119 
1120  if (const TemplateTemplateParmDecl *TTP =
1121  dyn_cast<TemplateTemplateParmDecl>(D)) {
1122  Out << "class ";
1123  if (TTP->isParameterPack())
1124  Out << "...";
1125  Out << D->getName();
1126  } else if (auto *TD = D->getTemplatedDecl())
1127  Visit(TD);
1128  else if (const auto *Concept = dyn_cast<ConceptDecl>(D)) {
1129  Out << "concept " << Concept->getName() << " = " ;
1130  Concept->getConstraintExpr()->printPretty(Out, nullptr, Policy,
1131  Indentation);
1132  Out << ";";
1133  }
1134 }
1135 
1136 void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
1137  prettyPrintPragmas(D->getTemplatedDecl());
1138  // Print any leading template parameter lists.
1139  if (const FunctionDecl *FD = D->getTemplatedDecl()) {
1140  for (unsigned I = 0, NumTemplateParams = FD->getNumTemplateParameterLists();
1141  I < NumTemplateParams; ++I)
1142  printTemplateParameters(FD->getTemplateParameterList(I));
1143  }
1144  VisitRedeclarableTemplateDecl(D);
1145  // Declare target attribute is special one, natural spelling for the pragma
1146  // assumes "ending" construct so print it here.
1147  if (D->getTemplatedDecl()->hasAttr<OMPDeclareTargetDeclAttr>())
1148  Out << "#pragma omp end declare target\n";
1149 
1150  // Never print "instantiations" for deduction guides (they don't really
1151  // have them).
1152  if (PrintInstantiation &&
1153  !isa<CXXDeductionGuideDecl>(D->getTemplatedDecl())) {
1154  FunctionDecl *PrevDecl = D->getTemplatedDecl();
1155  const FunctionDecl *Def;
1156  if (PrevDecl->isDefined(Def) && Def != PrevDecl)
1157  return;
1158  for (auto *I : D->specializations())
1159  if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) {
1160  if (!PrevDecl->isThisDeclarationADefinition())
1161  Out << ";\n";
1162  Indent();
1163  prettyPrintPragmas(I);
1164  Visit(I);
1165  }
1166  }
1167 }
1168 
1169 void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
1170  VisitRedeclarableTemplateDecl(D);
1171 
1172  if (PrintInstantiation) {
1173  for (auto *I : D->specializations())
1174  if (I->getSpecializationKind() == TSK_ImplicitInstantiation) {
1176  Out << ";";
1177  Out << "\n";
1178  Visit(I);
1179  }
1180  }
1181 }
1182 
1183 void DeclPrinter::VisitClassTemplateSpecializationDecl(
1185  Out << "template<> ";
1186  VisitCXXRecordDecl(D);
1187 }
1188 
1189 void DeclPrinter::VisitClassTemplatePartialSpecializationDecl(
1191  printTemplateParameters(D->getTemplateParameters());
1192  VisitCXXRecordDecl(D);
1193 }
1194 
1195 //----------------------------------------------------------------------------
1196 // Objective-C declarations
1197 //----------------------------------------------------------------------------
1198 
1199 void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
1201  QualType T) {
1202  Out << '(';
1203  if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
1204  Out << "in ";
1205  if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
1206  Out << "inout ";
1207  if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
1208  Out << "out ";
1209  if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
1210  Out << "bycopy ";
1211  if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
1212  Out << "byref ";
1213  if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1214  Out << "oneway ";
1215  if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
1216  if (auto nullability = AttributedType::stripOuterNullability(T))
1217  Out << getNullabilitySpelling(*nullability, true) << ' ';
1218  }
1219 
1220  Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
1221  Out << ')';
1222 }
1223 
1224 void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1225  Out << "<";
1226  unsigned First = true;
1227  for (auto *Param : *Params) {
1228  if (First) {
1229  First = false;
1230  } else {
1231  Out << ", ";
1232  }
1233 
1234  switch (Param->getVariance()) {
1236  break;
1237 
1239  Out << "__covariant ";
1240  break;
1241 
1243  Out << "__contravariant ";
1244  break;
1245  }
1246 
1247  Out << Param->getDeclName().getAsString();
1248 
1249  if (Param->hasExplicitBound()) {
1250  Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1251  }
1252  }
1253  Out << ">";
1254 }
1255 
1256 void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1257  if (OMD->isInstanceMethod())
1258  Out << "- ";
1259  else
1260  Out << "+ ";
1261  if (!OMD->getReturnType().isNull()) {
1262  PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
1263  OMD->getReturnType());
1264  }
1265 
1266  std::string name = OMD->getSelector().getAsString();
1267  std::string::size_type pos, lastPos = 0;
1268  for (const auto *PI : OMD->parameters()) {
1269  // FIXME: selector is missing here!
1270  pos = name.find_first_of(':', lastPos);
1271  if (lastPos != 0)
1272  Out << " ";
1273  Out << name.substr(lastPos, pos - lastPos) << ':';
1274  PrintObjCMethodType(OMD->getASTContext(),
1275  PI->getObjCDeclQualifier(),
1276  PI->getType());
1277  Out << *PI;
1278  lastPos = pos + 1;
1279  }
1280 
1281  if (OMD->param_begin() == OMD->param_end())
1282  Out << name;
1283 
1284  if (OMD->isVariadic())
1285  Out << ", ...";
1286 
1287  prettyPrintAttributes(OMD);
1288 
1289  if (OMD->getBody() && !Policy.TerseOutput) {
1290  Out << ' ';
1291  OMD->getBody()->printPretty(Out, nullptr, Policy);
1292  }
1293  else if (Policy.PolishForDeclaration)
1294  Out << ';';
1295 }
1296 
1297 void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1298  std::string I = OID->getNameAsString();
1299  ObjCInterfaceDecl *SID = OID->getSuperClass();
1300 
1301  bool eolnOut = false;
1302  if (SID)
1303  Out << "@implementation " << I << " : " << *SID;
1304  else
1305  Out << "@implementation " << I;
1306 
1307  if (OID->ivar_size() > 0) {
1308  Out << "{\n";
1309  eolnOut = true;
1310  Indentation += Policy.Indentation;
1311  for (const auto *I : OID->ivars()) {
1312  Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
1313  getAsString(Policy) << ' ' << *I << ";\n";
1314  }
1315  Indentation -= Policy.Indentation;
1316  Out << "}\n";
1317  }
1318  else if (SID || (OID->decls_begin() != OID->decls_end())) {
1319  Out << "\n";
1320  eolnOut = true;
1321  }
1322  VisitDeclContext(OID, false);
1323  if (!eolnOut)
1324  Out << "\n";
1325  Out << "@end";
1326 }
1327 
1328 void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1329  std::string I = OID->getNameAsString();
1330  ObjCInterfaceDecl *SID = OID->getSuperClass();
1331 
1332  if (!OID->isThisDeclarationADefinition()) {
1333  Out << "@class " << I;
1334 
1335  if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1336  PrintObjCTypeParams(TypeParams);
1337  }
1338 
1339  Out << ";";
1340  return;
1341  }
1342  bool eolnOut = false;
1343  Out << "@interface " << I;
1344 
1345  if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1346  PrintObjCTypeParams(TypeParams);
1347  }
1348 
1349  if (SID)
1350  Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
1351 
1352  // Protocols?
1353  const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1354  if (!Protocols.empty()) {
1355  for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1356  E = Protocols.end(); I != E; ++I)
1357  Out << (I == Protocols.begin() ? '<' : ',') << **I;
1358  Out << "> ";
1359  }
1360 
1361  if (OID->ivar_size() > 0) {
1362  Out << "{\n";
1363  eolnOut = true;
1364  Indentation += Policy.Indentation;
1365  for (const auto *I : OID->ivars()) {
1366  Indent() << I->getASTContext()
1367  .getUnqualifiedObjCPointerType(I->getType())
1368  .getAsString(Policy) << ' ' << *I << ";\n";
1369  }
1370  Indentation -= Policy.Indentation;
1371  Out << "}\n";
1372  }
1373  else if (SID || (OID->decls_begin() != OID->decls_end())) {
1374  Out << "\n";
1375  eolnOut = true;
1376  }
1377 
1378  VisitDeclContext(OID, false);
1379  if (!eolnOut)
1380  Out << "\n";
1381  Out << "@end";
1382  // FIXME: implement the rest...
1383 }
1384 
1385 void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
1386  if (!PID->isThisDeclarationADefinition()) {
1387  Out << "@protocol " << *PID << ";\n";
1388  return;
1389  }
1390  // Protocols?
1391  const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1392  if (!Protocols.empty()) {
1393  Out << "@protocol " << *PID;
1394  for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1395  E = Protocols.end(); I != E; ++I)
1396  Out << (I == Protocols.begin() ? '<' : ',') << **I;
1397  Out << ">\n";
1398  } else
1399  Out << "@protocol " << *PID << '\n';
1400  VisitDeclContext(PID, false);
1401  Out << "@end";
1402 }
1403 
1404 void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
1405  Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n";
1406 
1407  VisitDeclContext(PID, false);
1408  Out << "@end";
1409  // FIXME: implement the rest...
1410 }
1411 
1412 void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
1413  Out << "@interface " << *PID->getClassInterface();
1414  if (auto TypeParams = PID->getTypeParamList()) {
1415  PrintObjCTypeParams(TypeParams);
1416  }
1417  Out << "(" << *PID << ")\n";
1418  if (PID->ivar_size() > 0) {
1419  Out << "{\n";
1420  Indentation += Policy.Indentation;
1421  for (const auto *I : PID->ivars())
1422  Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
1423  getAsString(Policy) << ' ' << *I << ";\n";
1424  Indentation -= Policy.Indentation;
1425  Out << "}\n";
1426  }
1427 
1428  VisitDeclContext(PID, false);
1429  Out << "@end";
1430 
1431  // FIXME: implement the rest...
1432 }
1433 
1434 void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
1435  Out << "@compatibility_alias " << *AID
1436  << ' ' << *AID->getClassInterface() << ";\n";
1437 }
1438 
1439 /// PrintObjCPropertyDecl - print a property declaration.
1440 ///
1441 /// Print attributes in the following order:
1442 /// - class
1443 /// - nonatomic | atomic
1444 /// - assign | retain | strong | copy | weak | unsafe_unretained
1445 /// - readwrite | readonly
1446 /// - getter & setter
1447 /// - nullability
1448 void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1450  Out << "@required\n";
1452  Out << "@optional\n";
1453 
1454  QualType T = PDecl->getType();
1455 
1456  Out << "@property";
1458  bool first = true;
1459  Out << "(";
1461  Out << (first ? "" : ", ") << "class";
1462  first = false;
1463  }
1464 
1465  if (PDecl->getPropertyAttributes() &
1467  Out << (first ? "" : ", ") << "nonatomic";
1468  first = false;
1469  }
1470  if (PDecl->getPropertyAttributes() &
1472  Out << (first ? "" : ", ") << "atomic";
1473  first = false;
1474  }
1475 
1477  Out << (first ? "" : ", ") << "assign";
1478  first = false;
1479  }
1481  Out << (first ? "" : ", ") << "retain";
1482  first = false;
1483  }
1484 
1486  Out << (first ? "" : ", ") << "strong";
1487  first = false;
1488  }
1490  Out << (first ? "" : ", ") << "copy";
1491  first = false;
1492  }
1494  Out << (first ? "" : ", ") << "weak";
1495  first = false;
1496  }
1497  if (PDecl->getPropertyAttributes()
1499  Out << (first ? "" : ", ") << "unsafe_unretained";
1500  first = false;
1501  }
1502 
1503  if (PDecl->getPropertyAttributes() &
1505  Out << (first ? "" : ", ") << "readwrite";
1506  first = false;
1507  }
1508  if (PDecl->getPropertyAttributes() &
1510  Out << (first ? "" : ", ") << "readonly";
1511  first = false;
1512  }
1513 
1515  Out << (first ? "" : ", ") << "getter = ";
1516  PDecl->getGetterName().print(Out);
1517  first = false;
1518  }
1520  Out << (first ? "" : ", ") << "setter = ";
1521  PDecl->getSetterName().print(Out);
1522  first = false;
1523  }
1524 
1525  if (PDecl->getPropertyAttributes() &
1527  if (auto nullability = AttributedType::stripOuterNullability(T)) {
1528  if (*nullability == NullabilityKind::Unspecified &&
1529  (PDecl->getPropertyAttributes() &
1531  Out << (first ? "" : ", ") << "null_resettable";
1532  } else {
1533  Out << (first ? "" : ", ")
1534  << getNullabilitySpelling(*nullability, true);
1535  }
1536  first = false;
1537  }
1538  }
1539 
1540  (void) first; // Silence dead store warning due to idiomatic code.
1541  Out << ")";
1542  }
1543  std::string TypeStr = PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
1544  getAsString(Policy);
1545  Out << ' ' << TypeStr;
1546  if (!StringRef(TypeStr).endswith("*"))
1547  Out << ' ';
1548  Out << *PDecl;
1549  if (Policy.PolishForDeclaration)
1550  Out << ';';
1551 }
1552 
1553 void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1555  Out << "@synthesize ";
1556  else
1557  Out << "@dynamic ";
1558  Out << *PID->getPropertyDecl();
1559  if (PID->getPropertyIvarDecl())
1560  Out << '=' << *PID->getPropertyIvarDecl();
1561 }
1562 
1563 void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1564  if (!D->isAccessDeclaration())
1565  Out << "using ";
1566  if (D->hasTypename())
1567  Out << "typename ";
1568  D->getQualifier()->print(Out, Policy);
1569 
1570  // Use the correct record name when the using declaration is used for
1571  // inheriting constructors.
1572  for (const auto *Shadow : D->shadows()) {
1573  if (const auto *ConstructorShadow =
1574  dyn_cast<ConstructorUsingShadowDecl>(Shadow)) {
1575  assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext());
1576  Out << *ConstructorShadow->getNominatedBaseClass();
1577  return;
1578  }
1579  }
1580  Out << *D;
1581 }
1582 
1583 void
1584 DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1585  Out << "using typename ";
1586  D->getQualifier()->print(Out, Policy);
1587  Out << D->getDeclName();
1588 }
1589 
1590 void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1591  if (!D->isAccessDeclaration())
1592  Out << "using ";
1593  D->getQualifier()->print(Out, Policy);
1594  Out << D->getDeclName();
1595 }
1596 
1597 void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1598  // ignore
1599 }
1600 
1601 void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1602  Out << "#pragma omp threadprivate";
1603  if (!D->varlist_empty()) {
1605  E = D->varlist_end();
1606  I != E; ++I) {
1607  Out << (I == D->varlist_begin() ? '(' : ',');
1608  NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl();
1609  ND->printQualifiedName(Out);
1610  }
1611  Out << ")";
1612  }
1613 }
1614 
1615 void DeclPrinter::VisitOMPAllocateDecl(OMPAllocateDecl *D) {
1616  Out << "#pragma omp allocate";
1617  if (!D->varlist_empty()) {
1619  E = D->varlist_end();
1620  I != E; ++I) {
1621  Out << (I == D->varlist_begin() ? '(' : ',');
1622  NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl();
1623  ND->printQualifiedName(Out);
1624  }
1625  Out << ")";
1626  }
1627  if (!D->clauselist_empty()) {
1628  Out << " ";
1629  OMPClausePrinter Printer(Out, Policy);
1630  for (OMPClause *C : D->clauselists())
1631  Printer.Visit(C);
1632  }
1633 }
1634 
1635 void DeclPrinter::VisitOMPRequiresDecl(OMPRequiresDecl *D) {
1636  Out << "#pragma omp requires ";
1637  if (!D->clauselist_empty()) {
1638  OMPClausePrinter Printer(Out, Policy);
1639  for (auto I = D->clauselist_begin(), E = D->clauselist_end(); I != E; ++I)
1640  Printer.Visit(*I);
1641  }
1642 }
1643 
1644 void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1645  if (!D->isInvalidDecl()) {
1646  Out << "#pragma omp declare reduction (";
1648  const char *OpName =
1650  assert(OpName && "not an overloaded operator");
1651  Out << OpName;
1652  } else {
1653  assert(D->getDeclName().isIdentifier());
1654  D->printName(Out);
1655  }
1656  Out << " : ";
1657  D->getType().print(Out, Policy);
1658  Out << " : ";
1659  D->getCombiner()->printPretty(Out, nullptr, Policy, 0);
1660  Out << ")";
1661  if (auto *Init = D->getInitializer()) {
1662  Out << " initializer(";
1663  switch (D->getInitializerKind()) {
1665  Out << "omp_priv(";
1666  break;
1668  Out << "omp_priv = ";
1669  break;
1671  break;
1672  }
1673  Init->printPretty(Out, nullptr, Policy, 0);
1675  Out << ")";
1676  Out << ")";
1677  }
1678  }
1679 }
1680 
1681 void DeclPrinter::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
1682  if (!D->isInvalidDecl()) {
1683  Out << "#pragma omp declare mapper (";
1684  D->printName(Out);
1685  Out << " : ";
1686  D->getType().print(Out, Policy);
1687  Out << " ";
1688  Out << D->getVarName();
1689  Out << ")";
1690  if (!D->clauselist_empty()) {
1691  OMPClausePrinter Printer(Out, Policy);
1692  for (auto *C : D->clauselists()) {
1693  Out << " ";
1694  Printer.Visit(C);
1695  }
1696  }
1697  }
1698 }
1699 
1700 void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
1701  D->getInit()->printPretty(Out, nullptr, Policy, Indentation);
1702 }
1703 
clauselist_range clauselists()
Definition: DeclOpenMP.h:270
Defines the clang::ASTContext interface.
Represents a function declaration or definition.
Definition: Decl.h:1748
RefQualifierKind getRefQualifier() const
Retrieve the ref-qualifier associated with this function type.
Definition: Type.h:4047
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition: Type.h:2560
A (possibly-)qualified type.
Definition: Type.h:643
bool isThisDeclarationADefinition() const
Returns whether this template declaration defines the primary class pattern.
unsigned getFriendTypeNumTemplateParameterLists() const
Definition: DeclFriend.h:127
varlist_iterator varlist_end()
Definition: DeclOpenMP.h:498
ObjCInterfaceDecl * getClassInterface()
Definition: DeclObjC.h:2324
unsigned getNumBases() const
Retrieves the number of base classes of this class.
Definition: DeclCXX.h:819
unsigned getNumExceptions() const
Return the number of types in the exception specification.
Definition: Type.h:3972
iterator begin() const
Definition: DeclObjC.h:90
bool isThisDeclarationADefinition() const
Determine whether this particular declaration of this class is actually also a definition.
Definition: DeclObjC.h:1529
ObjCIvarDecl * getPropertyIvarDecl() const
Definition: DeclObjC.h:2826
InClassInitStyle getInClassInitStyle() const
Get the kind of (C++11) default member initializer that this field has.
Definition: Decl.h:2740
Expr * getBitWidth() const
Definition: Decl.h:2696
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: Type.h:3378
This represents &#39;#pragma omp allocate ...&#39; directive.
Definition: DeclOpenMP.h:422
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee...
Definition: Type.cpp:505
An instance of this object exists for each enum constant that is defined.
Definition: Decl.h:2819
StorageClass getStorageClass() const
Returns the storage class as written in the source.
Definition: Decl.h:1019
Represents the declaration of a typedef-name via the &#39;typedef&#39; type specifier.
Definition: Decl.h:3051
spec_range specializations() const
Defines the clang::Module class, which describes a module in the source code.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
bool isVariadic() const
Whether this function prototype is variadic.
Definition: Type.h:4026
ThreadStorageClassSpecifier getTSCSpec() const
Definition: Decl.h:1028
Defines the C++ template declaration subclasses.
OverloadedOperatorKind getCXXOverloadedOperator() const
If this name is the name of an overloadable operator in C++ (e.g., operator+), retrieve the kind of o...
QualType getUnqualifiedObjCPointerType(QualType type) const
getUnqualifiedObjCPointerType - Returns version of Objective-C pointer type with lifetime qualifier r...
Definition: ASTContext.h:1964
ivar_range ivars() const
Definition: DeclObjC.h:1457
Represents a C++11 auto or C++14 decltype(auto) type.
Definition: Type.h:4808
bool hasWrittenPrototype() const
Whether this function has a written prototype.
Definition: Decl.h:2086
Represents an empty-declaration.
Definition: Decl.h:4301
The parameter is covariant, e.g., X<T> is a subtype of X<U> when the type parameter is covariant and ...
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:2835
Represent a C++ namespace.
Definition: Decl.h:514
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1331
NestedNameSpecifier * getQualifier() const
Retrieve the nested-name-specifier that qualifies the name of the namespace.
Definition: DeclCXX.h:3101
bool clauselist_empty() const
Definition: DeclOpenMP.h:489
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...
Definition: Specifiers.h:112
A container of type source information.
Definition: Decl.h:86
Store information needed for an explicit specifier.
Definition: DeclCXX.h:2001
static void printGroup(Decl **Begin, unsigned NumDecls, raw_ostream &Out, const PrintingPolicy &Policy, unsigned Indentation=0)
RangeSelector name(std::string ID)
Given a node with a "name", (like NamedDecl, DeclRefExpr or CxxCtorInitializer) selects the name&#39;s to...
bool isDefined(const FunctionDecl *&Definition) const
Returns true if the function has a definition that does not need to be instantiated.
Definition: Decl.cpp:2797
param_const_iterator param_end() const
Definition: DeclObjC.h:351
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2574
bool isVirtualAsWritten() const
Whether this function is marked as virtual explicitly.
Definition: Decl.h:2014
bool isCompleteDefinition() const
Return true if this decl has its body fully specified.
Definition: Decl.h:3202
TemplateParameterList * getTemplateParameters() const
Get the list of template parameters.
FriendDecl - Represents the declaration of a friend entity, which can be a function, a type, or a templated function or type.
Definition: DeclFriend.h:53
Represents a variable declaration or definition.
Definition: Decl.h:812
const T * getAs() const
Member-template getAs<specific type>&#39;.
Definition: Type.h:6824
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:138
bool clauselist_empty() const
Definition: DeclOpenMP.h:268
bool isConst() const
Definition: Type.h:3653
bool isInvalidDecl() const
Definition: DeclBase.h:553
Stores a list of template parameters for a TemplateDecl and its derived classes.
Definition: DeclTemplate.h:67
Describes how types, statements, expressions, and declarations should be printed. ...
Definition: PrettyPrinter.h:37
Represents an expression – generally a full-expression – that introduces cleanups to be run at the ...
Definition: ExprCXX.h:3212
Represents a parameter to a function.
Definition: Decl.h:1564
Defines the clang::Expr interface and subclasses for C++ expressions.
iterator end() const
Definition: DeclObjC.h:91
bool isNoexceptExceptionSpec(ExceptionSpecificationType ESpecType)
const ObjCInterfaceDecl * getSuperClass() const
Definition: DeclObjC.h:2688
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition: Decl.h:269
Represents a struct/union/class.
Definition: Decl.h:3626
LanguageIDs getLanguage() const
Return the language specified by this linkage specification.
Definition: DeclCXX.h:2996
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition: Decl.h:297
C11 _Thread_local.
Definition: Specifiers.h:230
One of these records is kept for each identifier that is lexed.
Represents a class template specialization, which refers to a class template with a given set of temp...
TypeSourceInfo * getFriendType() const
If this friend declaration names an (untemplated but possibly dependent) type, return the type; other...
Definition: DeclFriend.h:123
StringLiteral * getMessage()
Definition: DeclCXX.h:3908
const ObjCProtocolList & getReferencedProtocols() const
Definition: DeclObjC.h:2120
void print(raw_ostream &Out, unsigned Indentation=0, bool PrintInstantiation=false) const
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
bool isInline() const
Returns true if this is an inline namespace declaration.
Definition: Decl.h:575
The parameter is contravariant, e.g., X<T> is a subtype of X<U> when the type parameter is covariant ...
NameKind getNameKind() const
Determine what kind of name this is.
Represents a member of a struct/union/class.
Definition: Decl.h:2607
RetTy Visit(PTR(OMPClause) S)
InitKind getInitializerKind() const
Get initializer kind.
Definition: DeclOpenMP.h:173
std::string getNameAsString() const
Get the name of the class associated with this interface.
Definition: DeclObjC.h:2680
NamedDecl * getFriendDecl() const
If this friend declaration doesn&#39;t name a type, return the inner declaration.
Definition: DeclFriend.h:138
ObjCTypeParamList * getTypeParamListAsWritten() const
Retrieve the type parameters written on this particular declaration of the class. ...
Definition: DeclObjC.h:1310
Represents a C++ using-declaration.
Definition: DeclCXX.h:3488
Expr * getInitializer()
Get initializer expression (if specified) of the declare reduction construct.
Definition: DeclOpenMP.h:170
bool isBitField() const
Determines whether this field is a bitfield.
Definition: Decl.h:2685
An lvalue ref-qualifier was provided (&).
Definition: Type.h:1377
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
Definition: Decl.cpp:2808
static QualType getDeclType(Decl *D)
std::string getFullModuleName(bool AllowStringLiterals=false) const
Retrieve the full name of this module, including the path from its top-level module.
Definition: Module.cpp:213
Microsoft throw(...) extension.
QualType getExceptionType(unsigned i) const
Return the ith exception type, where 0 <= i < getNumExceptions().
Definition: Type.h:3980
void print(raw_ostream &OS, const PrintingPolicy &Policy, const Twine &PlaceHolder=Twine(), unsigned Indentation=0) const
unsigned ivar_size() const
Definition: DeclObjC.h:2712
bool hasPrototype() const
Whether this function has a prototype, either because one was explicitly written or because it was "i...
Definition: Decl.h:2081
FunctionTemplateDecl * getDescribedFunctionTemplate() const
Retrieves the function template that is described by this function declaration.
Definition: Decl.cpp:3443
bool clauselist_empty() const
Definition: DeclOpenMP.h:388
Whether values of this type can be null is (explicitly) unspecified.
PropertyAttributeKind getPropertyAttributes() const
Definition: DeclObjC.h:839
NamedDecl * getNominatedNamespaceAsWritten()
Definition: DeclCXX.h:3105
const clang::PrintingPolicy & getPrintingPolicy() const
Definition: ASTContext.h:657
bool isAccessDeclaration() const
Return true if it is a C++03 access declaration (no &#39;using&#39;).
Definition: DeclCXX.h:3540
bool isConstexpr() const
Whether this variable is (C++11) constexpr.
Definition: Decl.h:1386
bool hasDynamicExceptionSpec() const
Return whether this function has a dynamic (throw) exception spec.
Definition: Type.h:3955
TemplateParameterList * getFriendTypeTemplateParameterList(unsigned N) const
Definition: DeclFriend.h:131
base_class_iterator bases_begin()
Definition: DeclCXX.h:832
Represents an Objective-C protocol declaration.
Definition: DeclObjC.h:2063
PropertyControl getPropertyImplementation() const
Definition: DeclObjC.h:932
Represents an ObjC class declaration.
Definition: DeclObjC.h:1171
Represents a linkage specification.
Definition: DeclCXX.h:2962
QualType getReturnType() const
Definition: DeclObjC.h:322
void print(raw_ostream &OS, const PrintingPolicy &Policy, bool ResolveTemplateArguments=false) const
Print this nested name specifier to the given output stream.
bool isConstexprSpecified() const
Definition: Decl.h:2117
Module * getImportedModule() const
Retrieve the module that was imported by the import declaration.
Definition: Decl.h:4233
unsigned IncludeTagDefinition
When true, include the body of a tag definition.
ObjCPropertyImplDecl - Represents implementation declaration of a property in a class or category imp...
Definition: DeclObjC.h:2758
bool hasBraces() const
Determines whether this linkage specification had braces in its syntactic form.
Definition: DeclCXX.h:3005
bool hasAttr() const
Definition: DeclBase.h:542
NestedNameSpecifier * getQualifier() const
Retrieve the nested-name-specifier that qualifies the name of the namespace.
Definition: DeclCXX.h:3224
Represents the declaration of a typedef-name via a C++11 alias-declaration.
Definition: Decl.h:3071
Represents a prototype with parameter type info, e.g.
Definition: Type.h:3710
const ObjCObjectType * getSuperClassType() const
Retrieve the superclass type.
Definition: DeclObjC.h:1564
DeclarationName getVarName()
Get the name of the variable declared in the mapper.
Definition: DeclOpenMP.h:288
bool empty() const
Definition: DeclObjC.h:71
Expr * getCombiner()
Get combiner expression of the declare reduction construct.
Definition: DeclOpenMP.h:152
MutableArrayRef< Expr * >::iterator varlist_iterator
Definition: DeclOpenMP.h:69
unsigned ivar_size() const
Definition: DeclObjC.h:1475
TypeSourceInfo * getTypeSourceInfo() const
Definition: Decl.h:2999
This represents &#39;#pragma omp requires...&#39; directive.
Definition: DeclOpenMP.h:345
bool isConsteval() const
Definition: Decl.h:2120
varlist_iterator varlist_begin()
Definition: DeclOpenMP.h:497
bool isInlineSpecified() const
Determine whether the "inline" keyword was specified for this function.
Definition: Decl.h:2377
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:636
This represents one expression.
Definition: Expr.h:108
StringRef getKindName() const
Definition: Decl.h:3272
SourceLocation End
Selector getSetterName() const
Definition: DeclObjC.h:913
llvm::StringRef getAsString(SyncScope S)
Definition: SyncScope.h:50
bool isScopedUsingClassTag() const
Returns true if this is a C++11 scoped enumeration.
Definition: Decl.h:3556
bool isThisDeclarationADefinition() const
Returns whether this specific declaration of the function is also a definition that does not contain ...
Definition: Decl.h:1997
bool isThisDeclarationADefinition() const
Determine whether this particular declaration is also the definition.
Definition: DeclObjC.h:2220
bool isImplicit() const
isImplicit - Indicates whether the declaration was implicitly generated by the implementation.
Definition: DeclBase.h:558
const TemplateArgumentList * getTemplateSpecializationArgs() const
Retrieve the template arguments used to produce this function template specialization from the primar...
Definition: Decl.cpp:3551
NestedNameSpecifier * getQualifier() const
Retrieve the nested-name-specifier that qualifies the name of this declaration, if it was present in ...
Definition: Decl.h:744
TemplateParameterList * getTemplateParameterList(unsigned index) const
Definition: Decl.h:763
ObjCInterfaceDecl * getSuperClass() const
Definition: DeclObjC.cpp:337
SourceLocation Begin
StorageClass getStorageClass() const
Returns the storage class as written in the source.
Definition: Decl.h:2366
const Expr * getExpr() const
Definition: DeclCXX.h:2010
void removeLocalConst()
Definition: Type.h:6227
bool isFunctionTemplateSpecialization() const
Determine whether this function is a function template specialization.
Definition: Decl.h:2467
void print(llvm::raw_ostream &OS) const
Prints the full selector name (e.g. "foo:bar:").
static QualType GetBaseType(QualType T)
static Optional< NullabilityKind > stripOuterNullability(QualType &T)
Strip off the top-level nullability annotation on the given type, if it&#39;s there.
Definition: Type.cpp:3943
StorageClass
Storage classes.
Definition: Specifiers.h:234
bool isIdentifier() const
Predicate functions for querying what type of name this is.
bool isSpecifierType() const
Returns true if this type can be represented by some set of type specifiers.
Definition: Type.cpp:2575
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:1772
bool isInstanceMethod() const
Definition: DeclObjC.h:421
clauselist_range clauselists()
Definition: DeclOpenMP.h:502
Represents a GCC generic vector type.
Definition: Type.h:3191
unsigned SuppressSpecifiers
Whether we should suppress printing of the actual specifiers for the given type or declaration...
Definition: PrettyPrinter.h:82
Represents a C++ deduction guide declaration.
Definition: DeclCXX.h:2056
void printQualifiedName(raw_ostream &OS) const
Returns a human-readable qualified name for this declaration, like A::B::i, for i being member of nam...
Definition: Decl.cpp:1555
Selector getSelector() const
Definition: DeclObjC.h:320
Represents a C++ conversion function within a class.
Definition: DeclCXX.h:2899
This template specialization was implicitly instantiated from a template.
Definition: Specifiers.h:180
bool isNull() const
Return true if this QualType doesn&#39;t point to a type yet.
Definition: Type.h:708
InitializationStyle getInitStyle() const
The style of initialization for this declaration.
Definition: Decl.h:1283
QualType getType() const
Definition: DeclObjC.h:828
AttrVec & getAttrs()
Definition: DeclBase.h:490
bool hasAttrs() const
Definition: DeclBase.h:484
NestedNameSpecifier * getQualifier() const
Retrieve the nested-name-specifier that qualifies the name.
Definition: DeclCXX.h:3842
bool hasTrailingReturn() const
Whether this function prototype has a trailing return type.
Definition: Type.h:4037
virtual void printName(raw_ostream &os) const
Definition: Decl.cpp:1544
bool isComputedNoexcept(ExceptionSpecificationType ESpecType)
std::string getAsString() const
Derive the full selector name (e.g.
Sugar for parentheses used when specifying types.
Definition: Type.h:2530
bool isPure() const
Whether this virtual function is pure, i.e.
Definition: Decl.h:2023
This represents &#39;#pragma omp declare reduction ...&#39; directive.
Definition: DeclOpenMP.h:102
decl_iterator decls_begin() const
Definition: DeclBase.cpp:1378
NestedNameSpecifier * getQualifier() const
Retrieve the nested-name-specifier that qualifies the name.
Definition: DeclCXX.h:3752
Pseudo declaration for capturing expressions.
Definition: DeclOpenMP.h:312
ObjCTypeParamList * getTypeParamList() const
Retrieve the type parameter list associated with this category or extension.
Definition: DeclObjC.h:2329
std::string getNameAsString() const
Get a human-readable name for the declaration, even if it is one of the special kinds of names (C++ c...
Definition: Decl.h:291
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3097
ASTContext & getASTContext() const LLVM_READONLY
Definition: DeclBase.cpp:376
Represents the declaration of a label.
Definition: Decl.h:468
Represents a dependent using declaration which was not marked with typename.
Definition: DeclCXX.h:3707
Expr * getInClassInitializer() const
Get the C++11 default member initializer for this member, or null if one has not been set...
Definition: Decl.h:2754
Stmt * getBody() const override
Retrieve the body of this method, if it has one.
Definition: DeclObjC.cpp:851
bool isRestrict() const
Definition: Type.h:3655
varlist_iterator varlist_begin()
Definition: DeclOpenMP.h:83
GNU __thread.
Definition: Specifiers.h:224
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
No ref-qualifier was provided.
Definition: Type.h:1374
C-style initialization with assignment.
Definition: Decl.h:817
const ParmVarDecl * getParamDecl(unsigned i) const
Definition: Decl.h:2312
ObjCCategoryDecl - Represents a category declaration.
Definition: DeclObjC.h:2279
This is a basic class for representing single OpenMP clause.
Definition: OpenMPClause.h:50
Expr * getNoexceptExpr() const
Return the expression inside noexcept(expression), or a null pointer if there is none (because the ex...
Definition: Type.h:3987
bool isScoped() const
Returns true if this is a C++11 scoped enumeration.
Definition: Decl.h:3553
Direct list-initialization.
Definition: Specifiers.h:260
Represents one property declaration in an Objective-C interface.
Definition: DeclObjC.h:728
A simple visitor class that helps create declaration visitors.
Definition: DeclVisitor.h:66
static ExplicitSpecifier getFromDecl(FunctionDecl *Function)
Definition: DeclCXX.cpp:1908
Indicates that the nullability of the type was spelled with a property attribute rather than a type q...
Definition: DeclObjC.h:748
ObjCDeclQualifier getObjCDeclQualifier() const
Definition: DeclObjC.h:246
static void printExplicitSpecifier(ExplicitSpecifier ES, llvm::raw_ostream &Out, PrintingPolicy &Policy, unsigned Indentation)
bool isExplicitlyDefaulted() const
Whether this function is explicitly defaulted per C++0x.
Definition: Decl.h:2053
DeclarationNameInfo getNameInfo() const
Definition: Decl.h:1915
Represents a C++11 static_assert declaration.
Definition: DeclCXX.h:3882
An rvalue ref-qualifier was provided (&&).
Definition: Type.h:1380
clauselist_iterator clauselist_begin()
Definition: DeclOpenMP.h:396
ExceptionSpecificationType getExceptionSpecType() const
Get the kind of exception specification on this function.
Definition: Type.h:3946
Describes a module import declaration, which makes the contents of the named module visible in the cu...
Definition: Decl.h:4189
Represents a pack expansion of types.
Definition: Type.h:5416
C++11 thread_local.
Definition: Specifiers.h:227
bool varlist_empty() const
Definition: DeclOpenMP.h:75
static const char * getStorageClassSpecifierString(StorageClass SC)
Return the string used to specify the storage class SC.
Definition: Decl.cpp:1922
decl_iterator - Iterates through the declarations stored within this context.
Definition: DeclBase.h:1978
Base class for declarations which introduce a typedef-name.
Definition: Decl.h:2949
const ObjCInterfaceDecl * getClassInterface() const
Definition: DeclObjC.h:2439
StreamedQualTypeHelper stream(const PrintingPolicy &Policy, const Twine &PlaceHolder=Twine(), unsigned Indentation=0) const
Definition: Type.h:1035
Dataflow Directional Tag Classes.
std::string getAsString() const
getAsString - Retrieve the human-readable string for this name.
ivar_range ivars() const
Definition: DeclObjC.h:2702
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1271
The base class of all kinds of template declarations (e.g., class, function, etc.).
Definition: DeclTemplate.h:403
Kind getPropertyImplementation() const
Definition: DeclObjC.h:2822
bool isAccessDeclaration() const
Return true if it is a C++03 access declaration (no &#39;using&#39;).
Definition: DeclCXX.h:3745
const ObjCProtocolList & getReferencedProtocols() const
Definition: DeclObjC.h:1340
const Expr * getInit() const
Definition: Decl.h:1219
AccessSpecifier getAccess() const
Definition: DeclBase.h:473
void print(raw_ostream &Out, const ASTContext &Context, bool OmitTemplateKW=false) const
Represents a dependent using declaration which was marked with typename.
Definition: DeclCXX.h:3803
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
Definition: Type.h:979
unsigned getNumTemplateParameterLists() const
Definition: Decl.h:759
Represents an enum.
Definition: Decl.h:3359
const ObjCInterfaceDecl * getClassInterface() const
Definition: DeclObjC.h:2746
MutableArrayRef< Expr * >::iterator varlist_iterator
Definition: DeclOpenMP.h:476
Pointer to a block type.
Definition: Type.h:2662
ObjCImplementationDecl - Represents a class definition - this is where method definitions are specifi...
Definition: DeclObjC.h:2551
const char * getOperatorSpelling(OverloadedOperatorKind Operator)
Retrieve the spelling of the given overloaded operator, without the preceding "operator" keyword...
TypeSourceInfo * getTypeSourceInfo() const
Definition: Decl.h:715
param_const_iterator param_begin() const
Definition: DeclObjC.h:347
Base for LValueReferenceType and RValueReferenceType.
Definition: Type.h:2696
clauselist_iterator clauselist_end()
Definition: DeclOpenMP.h:397
bool isVolatile() const
Definition: Type.h:3654
Represents a base class of a C++ class.
Definition: DeclCXX.h:192
bool hasTypename() const
Return true if the using declaration has &#39;typename&#39;.
Definition: DeclCXX.h:3543
A template argument list.
Definition: DeclTemplate.h:214
shadow_range shadows() const
Definition: DeclCXX.h:3588
varlist_iterator varlist_end()
Definition: DeclOpenMP.h:84
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
Call-style initialization (C++98)
Definition: Decl.h:820
void printPretty(raw_ostream &OS, PrinterHelper *Helper, const PrintingPolicy &Policy, unsigned Indentation=0, StringRef NewlineSymbol="\, const ASTContext *Context=nullptr) const
bool isMutable() const
Determines whether this field is mutable (C++ only).
Definition: Decl.h:2682
Represents a C++ struct/union/class.
Definition: DeclCXX.h:300
ObjCDeclQualifier
ObjCDeclQualifier - &#39;Qualifiers&#39; written next to the return and parameter types in method declaration...
Definition: DeclBase.h:200
base_class_iterator bases_end()
Definition: DeclCXX.h:834
bool varlist_empty() const
Definition: DeclOpenMP.h:487
Declaration of a class template.
Stores a list of Objective-C type parameters for a parameterized class or a category/extension thereo...
Definition: DeclObjC.h:636
bool isVariadic() const
Definition: DeclObjC.h:426
bool isSpecified() const
Return true if the ExplicitSpecifier isn&#39;t defaulted.
Definition: DeclCXX.h:2014
This represents &#39;#pragma omp declare mapper ...&#39; directive.
Definition: DeclOpenMP.h:217
StringLiteral - This represents a string literal expression, e.g.
Definition: Expr.h:1681
QualType getIntegerType() const
Return the integer type this enum decl corresponds to.
Definition: Decl.h:3513
bool doesThisDeclarationHaveABody() const
Returns whether this specific declaration of the function has a body.
Definition: Decl.h:2003
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:275
NestedNameSpecifier * getQualifier() const
Retrieve the nested-name-specifier that qualifies the name.
Definition: DeclCXX.h:3531
std::string getQualifiedNameAsString() const
Definition: Decl.cpp:1548
The top declaration context.
Definition: Decl.h:107
raw_ostream & Indent(raw_ostream &Out, const unsigned int Space, bool IsDot)
Definition: JsonSupport.h:20
ObjCPropertyDecl * getPropertyDecl() const
Definition: DeclObjC.h:2817
bool isDefaultArgument() const
Determine whether this expression is a default function argument.
Definition: Expr.cpp:2993
QualType getType() const
Definition: Decl.h:647
bool isModulePrivate() const
Whether this declaration was marked as being private to the module in which it was defined...
Definition: DeclBase.h:607
This represents a decl that may have a name.
Definition: Decl.h:248
bool isTranslationUnit() const
Definition: DeclBase.h:1847
Represents a C++ namespace alias.
Definition: DeclCXX.h:3156
Selector getGetterName() const
Definition: DeclObjC.h:905
Represents C++ using-directive.
Definition: DeclCXX.h:3052
llvm::StringRef getNullabilitySpelling(NullabilityKind kind, bool isContextSensitive=false)
Retrieve the spelling of the given nullability kind.
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition: Decl.cpp:3138
This represents &#39;#pragma omp threadprivate ...&#39; directive.
Definition: DeclOpenMP.h:39
ObjCCategoryImplDecl - An object of this class encapsulates a category @implementation declaration...
Definition: DeclObjC.h:2498
The parameter is invariant: must match exactly.
void dumpDeclContext() const
Declaration of a template function.
bool isDeletedAsWritten() const
Definition: Decl.h:2167
const StringLiteral * getAsmString() const
Definition: Decl.h:3880
Represents a shadow declaration introduced into a scope by a (resolved) using declaration.
Definition: DeclCXX.h:3275
QualType getType() const
Return the type wrapped by this type source info.
Definition: Decl.h:97
ArrayRef< ParmVarDecl * > parameters() const
Definition: DeclObjC.h:366
ObjCCompatibleAliasDecl - Represents alias of a class.
Definition: DeclObjC.h:2728
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point...
Definition: Expr.cpp:2956
decl_iterator decls_end() const
Definition: DeclBase.h:2023