1ec92531cSDmitri Gribenko //===--- Comment.cpp - Comment AST node implementation --------------------===//
2ec92531cSDmitri Gribenko //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ec92531cSDmitri Gribenko //
7ec92531cSDmitri Gribenko //===----------------------------------------------------------------------===//
8ec92531cSDmitri Gribenko 
9ec92531cSDmitri Gribenko #include "clang/AST/Comment.h"
109670f847SMehdi Amini #include "clang/AST/ASTContext.h"
11527ab211SDmitri Gribenko #include "clang/AST/Decl.h"
12527ab211SDmitri Gribenko #include "clang/AST/DeclObjC.h"
13527ab211SDmitri Gribenko #include "clang/AST/DeclTemplate.h"
14db6adabaSDmitri Gribenko #include "clang/Basic/CharInfo.h"
15ec92531cSDmitri Gribenko #include "llvm/Support/ErrorHandling.h"
162535f043SBruno Ricci #include <type_traits>
17ec92531cSDmitri Gribenko 
18ec92531cSDmitri Gribenko namespace clang {
19ec92531cSDmitri Gribenko namespace comments {
20ec92531cSDmitri Gribenko 
212535f043SBruno Ricci // Check that no comment class has a non-trival destructor. They are allocated
222535f043SBruno Ricci // with a BumpPtrAllocator and therefore their destructor is not executed.
232535f043SBruno Ricci #define ABSTRACT_COMMENT(COMMENT)
242535f043SBruno Ricci #define COMMENT(CLASS, PARENT)                                                 \
252535f043SBruno Ricci   static_assert(std::is_trivially_destructible<CLASS>::value,                  \
262535f043SBruno Ricci                 #CLASS " should be trivially destructible!");
272535f043SBruno Ricci #include "clang/AST/CommentNodes.inc"
282535f043SBruno Ricci #undef COMMENT
292535f043SBruno Ricci #undef ABSTRACT_COMMENT
302535f043SBruno Ricci 
312535f043SBruno Ricci // DeclInfo is also allocated with a BumpPtrAllocator.
322535f043SBruno Ricci static_assert(std::is_trivially_destructible<DeclInfo>::value,
332535f043SBruno Ricci               "DeclInfo should be trivially destructible!");
342535f043SBruno Ricci 
getCommentKindName() const35ec92531cSDmitri Gribenko const char *Comment::getCommentKindName() const {
36ec92531cSDmitri Gribenko   switch (getCommentKind()) {
37ec92531cSDmitri Gribenko   case NoCommentKind: return "NoCommentKind";
38ec92531cSDmitri Gribenko #define ABSTRACT_COMMENT(COMMENT)
39ec92531cSDmitri Gribenko #define COMMENT(CLASS, PARENT) \
40ec92531cSDmitri Gribenko   case CLASS##Kind: \
41ec92531cSDmitri Gribenko     return #CLASS;
42ec92531cSDmitri Gribenko #include "clang/AST/CommentNodes.inc"
43ec92531cSDmitri Gribenko #undef COMMENT
44ec92531cSDmitri Gribenko #undef ABSTRACT_COMMENT
45ec92531cSDmitri Gribenko   }
46ec92531cSDmitri Gribenko   llvm_unreachable("Unknown comment kind!");
47ec92531cSDmitri Gribenko }
48ec92531cSDmitri Gribenko 
49ec92531cSDmitri Gribenko namespace {
50ec92531cSDmitri Gribenko struct good {};
51ec92531cSDmitri Gribenko struct bad {};
52ec92531cSDmitri Gribenko 
53ec92531cSDmitri Gribenko template <typename T>
implements_child_begin_end(Comment::child_iterator (T::*)()const)54ec92531cSDmitri Gribenko good implements_child_begin_end(Comment::child_iterator (T::*)() const) {
55ec92531cSDmitri Gribenko   return good();
56ec92531cSDmitri Gribenko }
57ec92531cSDmitri Gribenko 
58dc41d791SEli Friedman LLVM_ATTRIBUTE_UNUSED
implements_child_begin_end(Comment::child_iterator (Comment::*)()const)59ec92531cSDmitri Gribenko static inline bad implements_child_begin_end(
60ec92531cSDmitri Gribenko                       Comment::child_iterator (Comment::*)() const) {
61ec92531cSDmitri Gribenko   return bad();
62ec92531cSDmitri Gribenko }
63ec92531cSDmitri Gribenko 
64ec92531cSDmitri Gribenko #define ASSERT_IMPLEMENTS_child_begin(function) \
65dc41d791SEli Friedman   (void) good(implements_child_begin_end(function))
66ec92531cSDmitri Gribenko 
67dc41d791SEli Friedman LLVM_ATTRIBUTE_UNUSED
CheckCommentASTNodes()68ec92531cSDmitri Gribenko static inline void CheckCommentASTNodes() {
69ec92531cSDmitri Gribenko #define ABSTRACT_COMMENT(COMMENT)
70ec92531cSDmitri Gribenko #define COMMENT(CLASS, PARENT) \
71ec92531cSDmitri Gribenko   ASSERT_IMPLEMENTS_child_begin(&CLASS::child_begin); \
72ec92531cSDmitri Gribenko   ASSERT_IMPLEMENTS_child_begin(&CLASS::child_end);
73ec92531cSDmitri Gribenko #include "clang/AST/CommentNodes.inc"
74ec92531cSDmitri Gribenko #undef COMMENT
75ec92531cSDmitri Gribenko #undef ABSTRACT_COMMENT
76ec92531cSDmitri Gribenko }
77ec92531cSDmitri Gribenko 
78ec92531cSDmitri Gribenko #undef ASSERT_IMPLEMENTS_child_begin
79ec92531cSDmitri Gribenko 
80ec92531cSDmitri Gribenko } // end unnamed namespace
81ec92531cSDmitri Gribenko 
child_begin() const82ec92531cSDmitri Gribenko Comment::child_iterator Comment::child_begin() const {
83ec92531cSDmitri Gribenko   switch (getCommentKind()) {
84ec92531cSDmitri Gribenko   case NoCommentKind: llvm_unreachable("comment without a kind");
85ec92531cSDmitri Gribenko #define ABSTRACT_COMMENT(COMMENT)
86ec92531cSDmitri Gribenko #define COMMENT(CLASS, PARENT) \
87ec92531cSDmitri Gribenko   case CLASS##Kind: \
88ec92531cSDmitri Gribenko     return static_cast<const CLASS *>(this)->child_begin();
89ec92531cSDmitri Gribenko #include "clang/AST/CommentNodes.inc"
90ec92531cSDmitri Gribenko #undef COMMENT
91ec92531cSDmitri Gribenko #undef ABSTRACT_COMMENT
92ec92531cSDmitri Gribenko   }
934106ea3bSMatt Beaumont-Gay   llvm_unreachable("Unknown comment kind!");
94ec92531cSDmitri Gribenko }
95ec92531cSDmitri Gribenko 
child_end() const96ec92531cSDmitri Gribenko Comment::child_iterator Comment::child_end() const {
97ec92531cSDmitri Gribenko   switch (getCommentKind()) {
98ec92531cSDmitri Gribenko   case NoCommentKind: llvm_unreachable("comment without a kind");
99ec92531cSDmitri Gribenko #define ABSTRACT_COMMENT(COMMENT)
100ec92531cSDmitri Gribenko #define COMMENT(CLASS, PARENT) \
101ec92531cSDmitri Gribenko   case CLASS##Kind: \
102ec92531cSDmitri Gribenko     return static_cast<const CLASS *>(this)->child_end();
103ec92531cSDmitri Gribenko #include "clang/AST/CommentNodes.inc"
104ec92531cSDmitri Gribenko #undef COMMENT
105ec92531cSDmitri Gribenko #undef ABSTRACT_COMMENT
106ec92531cSDmitri Gribenko   }
1074106ea3bSMatt Beaumont-Gay   llvm_unreachable("Unknown comment kind!");
108ec92531cSDmitri Gribenko }
109ec92531cSDmitri Gribenko 
isWhitespaceNoCache() const11005c3c755SDmitri Gribenko bool TextComment::isWhitespaceNoCache() const {
111*c2bb9637SKazu Hirata   return llvm::all_of(Text, clang::isWhitespace);
112f26054f0SDmitri Gribenko }
113f26054f0SDmitri Gribenko 
isWhitespaceNoCache() const11405c3c755SDmitri Gribenko bool ParagraphComment::isWhitespaceNoCache() const {
115f26054f0SDmitri Gribenko   for (child_iterator I = child_begin(), E = child_end(); I != E; ++I) {
116f26054f0SDmitri Gribenko     if (const TextComment *TC = dyn_cast<TextComment>(*I)) {
117f26054f0SDmitri Gribenko       if (!TC->isWhitespace())
118f26054f0SDmitri Gribenko         return false;
1197b2ca3e8SDmitri Gribenko     } else
1207b2ca3e8SDmitri Gribenko       return false;
121f26054f0SDmitri Gribenko   }
122f26054f0SDmitri Gribenko   return true;
123f26054f0SDmitri Gribenko }
124f26054f0SDmitri Gribenko 
lookThroughTypedefOrTypeAliasLocs(TypeLoc & SrcTL)12572ae62c1SBruno Cardoso Lopes static TypeLoc lookThroughTypedefOrTypeAliasLocs(TypeLoc &SrcTL) {
12672ae62c1SBruno Cardoso Lopes   TypeLoc TL = SrcTL.IgnoreParens();
12772ae62c1SBruno Cardoso Lopes 
1286246cc6cSAlex Lorenz   // Look through attribute types.
1296246cc6cSAlex Lorenz   if (AttributedTypeLoc AttributeTL = TL.getAs<AttributedTypeLoc>())
1306246cc6cSAlex Lorenz     return AttributeTL.getModifiedLoc();
13172ae62c1SBruno Cardoso Lopes   // Look through qualified types.
13272ae62c1SBruno Cardoso Lopes   if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>())
13372ae62c1SBruno Cardoso Lopes     return QualifiedTL.getUnqualifiedLoc();
13472ae62c1SBruno Cardoso Lopes   // Look through pointer types.
13572ae62c1SBruno Cardoso Lopes   if (PointerTypeLoc PointerTL = TL.getAs<PointerTypeLoc>())
13672ae62c1SBruno Cardoso Lopes     return PointerTL.getPointeeLoc().getUnqualifiedLoc();
13772ae62c1SBruno Cardoso Lopes   // Look through reference types.
13872ae62c1SBruno Cardoso Lopes   if (ReferenceTypeLoc ReferenceTL = TL.getAs<ReferenceTypeLoc>())
13972ae62c1SBruno Cardoso Lopes     return ReferenceTL.getPointeeLoc().getUnqualifiedLoc();
14072ae62c1SBruno Cardoso Lopes   // Look through adjusted types.
14172ae62c1SBruno Cardoso Lopes   if (AdjustedTypeLoc ATL = TL.getAs<AdjustedTypeLoc>())
14272ae62c1SBruno Cardoso Lopes     return ATL.getOriginalLoc();
14372ae62c1SBruno Cardoso Lopes   if (BlockPointerTypeLoc BlockPointerTL = TL.getAs<BlockPointerTypeLoc>())
14472ae62c1SBruno Cardoso Lopes     return BlockPointerTL.getPointeeLoc().getUnqualifiedLoc();
14572ae62c1SBruno Cardoso Lopes   if (MemberPointerTypeLoc MemberPointerTL = TL.getAs<MemberPointerTypeLoc>())
14672ae62c1SBruno Cardoso Lopes     return MemberPointerTL.getPointeeLoc().getUnqualifiedLoc();
14772ae62c1SBruno Cardoso Lopes   if (ElaboratedTypeLoc ETL = TL.getAs<ElaboratedTypeLoc>())
14872ae62c1SBruno Cardoso Lopes     return ETL.getNamedTypeLoc();
14972ae62c1SBruno Cardoso Lopes 
15072ae62c1SBruno Cardoso Lopes   return TL;
15172ae62c1SBruno Cardoso Lopes }
15272ae62c1SBruno Cardoso Lopes 
getFunctionTypeLoc(TypeLoc TL,FunctionTypeLoc & ResFTL)15372ae62c1SBruno Cardoso Lopes static bool getFunctionTypeLoc(TypeLoc TL, FunctionTypeLoc &ResFTL) {
15472ae62c1SBruno Cardoso Lopes   TypeLoc PrevTL;
15572ae62c1SBruno Cardoso Lopes   while (PrevTL != TL) {
15672ae62c1SBruno Cardoso Lopes     PrevTL = TL;
15772ae62c1SBruno Cardoso Lopes     TL = lookThroughTypedefOrTypeAliasLocs(TL);
15872ae62c1SBruno Cardoso Lopes   }
15972ae62c1SBruno Cardoso Lopes 
16072ae62c1SBruno Cardoso Lopes   if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
16172ae62c1SBruno Cardoso Lopes     ResFTL = FTL;
16272ae62c1SBruno Cardoso Lopes     return true;
16372ae62c1SBruno Cardoso Lopes   }
16472ae62c1SBruno Cardoso Lopes 
16572ae62c1SBruno Cardoso Lopes   if (TemplateSpecializationTypeLoc STL =
16672ae62c1SBruno Cardoso Lopes           TL.getAs<TemplateSpecializationTypeLoc>()) {
16772ae62c1SBruno Cardoso Lopes     // If we have a typedef to a template specialization with exactly one
16872ae62c1SBruno Cardoso Lopes     // template argument of a function type, this looks like std::function,
16972ae62c1SBruno Cardoso Lopes     // boost::function, or other function wrapper.  Treat these typedefs as
17072ae62c1SBruno Cardoso Lopes     // functions.
17172ae62c1SBruno Cardoso Lopes     if (STL.getNumArgs() != 1)
17272ae62c1SBruno Cardoso Lopes       return false;
17372ae62c1SBruno Cardoso Lopes     TemplateArgumentLoc MaybeFunction = STL.getArgLoc(0);
17472ae62c1SBruno Cardoso Lopes     if (MaybeFunction.getArgument().getKind() != TemplateArgument::Type)
17572ae62c1SBruno Cardoso Lopes       return false;
17672ae62c1SBruno Cardoso Lopes     TypeSourceInfo *MaybeFunctionTSI = MaybeFunction.getTypeSourceInfo();
17772ae62c1SBruno Cardoso Lopes     TypeLoc TL = MaybeFunctionTSI->getTypeLoc().getUnqualifiedLoc();
17872ae62c1SBruno Cardoso Lopes     if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
17972ae62c1SBruno Cardoso Lopes       ResFTL = FTL;
18072ae62c1SBruno Cardoso Lopes       return true;
18172ae62c1SBruno Cardoso Lopes     }
18272ae62c1SBruno Cardoso Lopes   }
18372ae62c1SBruno Cardoso Lopes 
18472ae62c1SBruno Cardoso Lopes   return false;
18572ae62c1SBruno Cardoso Lopes }
18672ae62c1SBruno Cardoso Lopes 
getDirectionAsString(PassDirection D)187f26054f0SDmitri Gribenko const char *ParamCommandComment::getDirectionAsString(PassDirection D) {
188f26054f0SDmitri Gribenko   switch (D) {
189f26054f0SDmitri Gribenko   case ParamCommandComment::In:
190f26054f0SDmitri Gribenko     return "[in]";
191f26054f0SDmitri Gribenko   case ParamCommandComment::Out:
192f26054f0SDmitri Gribenko     return "[out]";
193f26054f0SDmitri Gribenko   case ParamCommandComment::InOut:
194f26054f0SDmitri Gribenko     return "[in,out]";
195f26054f0SDmitri Gribenko   }
196f26054f0SDmitri Gribenko   llvm_unreachable("unknown PassDirection");
197f26054f0SDmitri Gribenko }
198ec92531cSDmitri Gribenko 
fill()199527ab211SDmitri Gribenko void DeclInfo::fill() {
200527ab211SDmitri Gribenko   assert(!IsFilled);
201527ab211SDmitri Gribenko 
202527ab211SDmitri Gribenko   // Set defaults.
20364305830SDmitri Gribenko   Kind = OtherKind;
2048e5d5f1fSDmitri Gribenko   TemplateKind = NotTemplate;
205558babc5SDmitri Gribenko   IsObjCMethod = false;
206527ab211SDmitri Gribenko   IsInstanceMethod = false;
207527ab211SDmitri Gribenko   IsClassMethod = false;
2084e7df1efSAaron Puchert   IsVariadic = false;
20944ebbd54SDmitri Gribenko   ParamVars = None;
21036250ad6SCraig Topper   TemplateParameters = nullptr;
211527ab211SDmitri Gribenko 
2121c883b9bSFariborz Jahanian   if (!CommentDecl) {
2130567de88SDmitri Gribenko     // If there is no declaration, the defaults is our only guess.
2140567de88SDmitri Gribenko     IsFilled = true;
2150567de88SDmitri Gribenko     return;
2160567de88SDmitri Gribenko   }
217a7d76d26SFariborz Jahanian   CurrentDecl = CommentDecl;
2180567de88SDmitri Gribenko 
2191c883b9bSFariborz Jahanian   Decl::Kind K = CommentDecl->getKind();
2203506e42aSAaron Puchert   const TypeSourceInfo *TSI = nullptr;
2210567de88SDmitri Gribenko   switch (K) {
2220567de88SDmitri Gribenko   default:
2230567de88SDmitri Gribenko     // Defaults are should be good for declarations we don't handle explicitly.
2240567de88SDmitri Gribenko     break;
2250567de88SDmitri Gribenko   case Decl::Function:
2260567de88SDmitri Gribenko   case Decl::CXXMethod:
2270567de88SDmitri Gribenko   case Decl::CXXConstructor:
2280567de88SDmitri Gribenko   case Decl::CXXDestructor:
2290567de88SDmitri Gribenko   case Decl::CXXConversion: {
2301c883b9bSFariborz Jahanian     const FunctionDecl *FD = cast<FunctionDecl>(CommentDecl);
23137a7fafdSDmitri Gribenko     Kind = FunctionKind;
23259f77921SDavid Majnemer     ParamVars = FD->parameters();
233314cc81bSAlp Toker     ReturnType = FD->getReturnType();
234527ab211SDmitri Gribenko     unsigned NumLists = FD->getNumTemplateParameterLists();
235527ab211SDmitri Gribenko     if (NumLists != 0) {
2368e5d5f1fSDmitri Gribenko       TemplateKind = TemplateSpecialization;
237527ab211SDmitri Gribenko       TemplateParameters =
238527ab211SDmitri Gribenko           FD->getTemplateParameterList(NumLists - 1);
239527ab211SDmitri Gribenko     }
240527ab211SDmitri Gribenko 
24164305830SDmitri Gribenko     if (K == Decl::CXXMethod || K == Decl::CXXConstructor ||
24264305830SDmitri Gribenko         K == Decl::CXXDestructor || K == Decl::CXXConversion) {
2431c883b9bSFariborz Jahanian       const CXXMethodDecl *MD = cast<CXXMethodDecl>(CommentDecl);
244527ab211SDmitri Gribenko       IsInstanceMethod = MD->isInstance();
245527ab211SDmitri Gribenko       IsClassMethod = !IsInstanceMethod;
246527ab211SDmitri Gribenko     }
2474e7df1efSAaron Puchert     IsVariadic = FD->isVariadic();
2483010883fSAaron Puchert     assert(involvesFunctionType());
2490567de88SDmitri Gribenko     break;
2500567de88SDmitri Gribenko   }
2510567de88SDmitri Gribenko   case Decl::ObjCMethod: {
2521c883b9bSFariborz Jahanian     const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(CommentDecl);
25337a7fafdSDmitri Gribenko     Kind = FunctionKind;
25459f77921SDavid Majnemer     ParamVars = MD->parameters();
255314cc81bSAlp Toker     ReturnType = MD->getReturnType();
256558babc5SDmitri Gribenko     IsObjCMethod = true;
257527ab211SDmitri Gribenko     IsInstanceMethod = MD->isInstanceMethod();
258527ab211SDmitri Gribenko     IsClassMethod = !IsInstanceMethod;
2594e7df1efSAaron Puchert     IsVariadic = MD->isVariadic();
2603010883fSAaron Puchert     assert(involvesFunctionType());
2610567de88SDmitri Gribenko     break;
2620567de88SDmitri Gribenko   }
2630567de88SDmitri Gribenko   case Decl::FunctionTemplate: {
2641c883b9bSFariborz Jahanian     const FunctionTemplateDecl *FTD = cast<FunctionTemplateDecl>(CommentDecl);
26537a7fafdSDmitri Gribenko     Kind = FunctionKind;
2668e5d5f1fSDmitri Gribenko     TemplateKind = Template;
267527ab211SDmitri Gribenko     const FunctionDecl *FD = FTD->getTemplatedDecl();
26859f77921SDavid Majnemer     ParamVars = FD->parameters();
269314cc81bSAlp Toker     ReturnType = FD->getReturnType();
270527ab211SDmitri Gribenko     TemplateParameters = FTD->getTemplateParameters();
2714e7df1efSAaron Puchert     IsVariadic = FD->isVariadic();
2723010883fSAaron Puchert     assert(involvesFunctionType());
2730567de88SDmitri Gribenko     break;
2740567de88SDmitri Gribenko   }
2750567de88SDmitri Gribenko   case Decl::ClassTemplate: {
2761c883b9bSFariborz Jahanian     const ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(CommentDecl);
27737a7fafdSDmitri Gribenko     Kind = ClassKind;
2788e5d5f1fSDmitri Gribenko     TemplateKind = Template;
279527ab211SDmitri Gribenko     TemplateParameters = CTD->getTemplateParameters();
2800567de88SDmitri Gribenko     break;
2810567de88SDmitri Gribenko   }
2820567de88SDmitri Gribenko   case Decl::ClassTemplatePartialSpecialization: {
2830567de88SDmitri Gribenko     const ClassTemplatePartialSpecializationDecl *CTPSD =
2841c883b9bSFariborz Jahanian         cast<ClassTemplatePartialSpecializationDecl>(CommentDecl);
28537a7fafdSDmitri Gribenko     Kind = ClassKind;
2868e5d5f1fSDmitri Gribenko     TemplateKind = TemplatePartialSpecialization;
287527ab211SDmitri Gribenko     TemplateParameters = CTPSD->getTemplateParameters();
2880567de88SDmitri Gribenko     break;
2890567de88SDmitri Gribenko   }
2900567de88SDmitri Gribenko   case Decl::ClassTemplateSpecialization:
29137a7fafdSDmitri Gribenko     Kind = ClassKind;
2928e5d5f1fSDmitri Gribenko     TemplateKind = TemplateSpecialization;
2930567de88SDmitri Gribenko     break;
2940567de88SDmitri Gribenko   case Decl::Record:
29564305830SDmitri Gribenko   case Decl::CXXRecord:
29637a7fafdSDmitri Gribenko     Kind = ClassKind;
2970567de88SDmitri Gribenko     break;
2980567de88SDmitri Gribenko   case Decl::Var:
29963ef0e17SAaron Puchert     if (const VarTemplateDecl *VTD =
30063ef0e17SAaron Puchert             cast<VarDecl>(CommentDecl)->getDescribedVarTemplate()) {
30163ef0e17SAaron Puchert       TemplateKind = TemplateSpecialization;
30263ef0e17SAaron Puchert       TemplateParameters = VTD->getTemplateParameters();
30363ef0e17SAaron Puchert     }
30463ef0e17SAaron Puchert     LLVM_FALLTHROUGH;
3050567de88SDmitri Gribenko   case Decl::Field:
30694ef6357SDmitri Gribenko   case Decl::EnumConstant:
3070567de88SDmitri Gribenko   case Decl::ObjCIvar:
3080567de88SDmitri Gribenko   case Decl::ObjCAtDefsField:
3093506e42aSAaron Puchert   case Decl::ObjCProperty:
3106b82a75dSAlex Lorenz     if (const auto *VD = dyn_cast<DeclaratorDecl>(CommentDecl))
3116b82a75dSAlex Lorenz       TSI = VD->getTypeSourceInfo();
3126b82a75dSAlex Lorenz     else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(CommentDecl))
3136b82a75dSAlex Lorenz       TSI = PD->getTypeSourceInfo();
31437a7fafdSDmitri Gribenko     Kind = VariableKind;
3150567de88SDmitri Gribenko     break;
31663ef0e17SAaron Puchert   case Decl::VarTemplate: {
31763ef0e17SAaron Puchert     const VarTemplateDecl *VTD = cast<VarTemplateDecl>(CommentDecl);
31863ef0e17SAaron Puchert     Kind = VariableKind;
31963ef0e17SAaron Puchert     TemplateKind = Template;
32063ef0e17SAaron Puchert     TemplateParameters = VTD->getTemplateParameters();
32163ef0e17SAaron Puchert     if (const VarDecl *VD = VTD->getTemplatedDecl())
32263ef0e17SAaron Puchert       TSI = VD->getTypeSourceInfo();
32363ef0e17SAaron Puchert     break;
32463ef0e17SAaron Puchert   }
3250567de88SDmitri Gribenko   case Decl::Namespace:
32637a7fafdSDmitri Gribenko     Kind = NamespaceKind;
3270567de88SDmitri Gribenko     break;
328b09db225SBruno Cardoso Lopes   case Decl::TypeAlias:
3293506e42aSAaron Puchert   case Decl::Typedef:
330907f6b8cSDmitri Gribenko     Kind = TypedefKind;
3313506e42aSAaron Puchert     TSI = cast<TypedefNameDecl>(CommentDecl)->getTypeSourceInfo();
332907f6b8cSDmitri Gribenko     break;
3330567de88SDmitri Gribenko   case Decl::TypeAliasTemplate: {
3341c883b9bSFariborz Jahanian     const TypeAliasTemplateDecl *TAT = cast<TypeAliasTemplateDecl>(CommentDecl);
33537a7fafdSDmitri Gribenko     Kind = TypedefKind;
3368e5d5f1fSDmitri Gribenko     TemplateKind = Template;
337baeb60e9SDmitri Gribenko     TemplateParameters = TAT->getTemplateParameters();
3383506e42aSAaron Puchert     if (TypeAliasDecl *TAD = TAT->getTemplatedDecl())
3393506e42aSAaron Puchert       TSI = TAD->getTypeSourceInfo();
3400567de88SDmitri Gribenko     break;
341527ab211SDmitri Gribenko   }
342168d2341SDmitri Gribenko   case Decl::Enum:
343168d2341SDmitri Gribenko     Kind = EnumKind;
344168d2341SDmitri Gribenko     break;
3450567de88SDmitri Gribenko   }
3460567de88SDmitri Gribenko 
3473506e42aSAaron Puchert   // If the type is a typedef / using to something we consider a function,
3483506e42aSAaron Puchert   // extract arguments and return type.
3493506e42aSAaron Puchert   if (TSI) {
3503506e42aSAaron Puchert     TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
3513506e42aSAaron Puchert     FunctionTypeLoc FTL;
3523506e42aSAaron Puchert     if (getFunctionTypeLoc(TL, FTL)) {
3533506e42aSAaron Puchert       ParamVars = FTL.getParams();
3543506e42aSAaron Puchert       ReturnType = FTL.getReturnLoc().getType();
3554e7df1efSAaron Puchert       if (const auto *FPT = dyn_cast<FunctionProtoType>(FTL.getTypePtr()))
3564e7df1efSAaron Puchert         IsVariadic = FPT->isVariadic();
3573010883fSAaron Puchert       assert(involvesFunctionType());
3583506e42aSAaron Puchert     }
3593506e42aSAaron Puchert   }
3603506e42aSAaron Puchert 
361527ab211SDmitri Gribenko   IsFilled = true;
362527ab211SDmitri Gribenko }
363527ab211SDmitri Gribenko 
getParamName(const FullComment * FC) const364923f305bSDmitri Gribenko StringRef ParamCommandComment::getParamName(const FullComment *FC) const {
3659d2f1e75SFariborz Jahanian   assert(isParamIndexValid());
36602489eb4SDmitri Gribenko   if (isVarArgParam())
36702489eb4SDmitri Gribenko     return "...";
368e4055692SFariborz Jahanian   return FC->getDeclInfo()->ParamVars[getParamIndex()]->getName();
36942e31323SFariborz Jahanian }
37042e31323SFariborz Jahanian 
getParamName(const FullComment * FC) const371923f305bSDmitri Gribenko StringRef TParamCommandComment::getParamName(const FullComment *FC) const {
3729d2f1e75SFariborz Jahanian   assert(isPositionValid());
373e4055692SFariborz Jahanian   const TemplateParameterList *TPL = FC->getDeclInfo()->TemplateParameters;
37414ec3f3aSFariborz Jahanian   for (unsigned i = 0, e = getDepth(); i != e; ++i) {
3753b417b7cSSimon Pilgrim     assert(TPL && "Unknown TemplateParameterList");
37614ec3f3aSFariborz Jahanian     if (i == e - 1)
37714ec3f3aSFariborz Jahanian       return TPL->getParam(getIndex(i))->getName();
37814ec3f3aSFariborz Jahanian     const NamedDecl *Param = TPL->getParam(getIndex(i));
3793b417b7cSSimon Pilgrim     if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param))
38014ec3f3aSFariborz Jahanian       TPL = TTP->getTemplateParameters();
38114ec3f3aSFariborz Jahanian   }
3829d2f1e75SFariborz Jahanian   return "";
38314ec3f3aSFariborz Jahanian }
38414ec3f3aSFariborz Jahanian 
385ec92531cSDmitri Gribenko } // end namespace comments
386ec92531cSDmitri Gribenko } // end namespace clang
38737a7fafdSDmitri Gribenko 
388