1 //===--- Comment.cpp - Comment AST node implementation --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/AST/ASTContext.h"
11 #include "clang/AST/Comment.h"
12 #include "clang/AST/Decl.h"
13 #include "clang/AST/DeclObjC.h"
14 #include "clang/AST/DeclTemplate.h"
15 #include "clang/Basic/CharInfo.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/raw_ostream.h"
22 const char *Comment::getCommentKindName() const {
23 switch (getCommentKind()) {
24 case NoCommentKind
: return "NoCommentKind";
25 #define ABSTRACT_COMMENT(COMMENT)
26 #define COMMENT(CLASS, PARENT) \
29 #include "clang/AST/CommentNodes.inc"
31 #undef ABSTRACT_COMMENT
33 llvm_unreachable("Unknown comment kind!");
41 good
implements_child_begin_end(Comment::child_iterator (T::*)() const) {
46 static inline bad
implements_child_begin_end(
47 Comment::child_iterator (Comment::*)() const) {
51 #define ASSERT_IMPLEMENTS_child_begin(function) \
52 (void) good(implements_child_begin_end(function))
55 static inline void CheckCommentASTNodes() {
56 #define ABSTRACT_COMMENT(COMMENT)
57 #define COMMENT(CLASS, PARENT) \
58 ASSERT_IMPLEMENTS_child_begin(&CLASS::child_begin); \
59 ASSERT_IMPLEMENTS_child_begin(&CLASS::child_end);
60 #include "clang/AST/CommentNodes.inc"
62 #undef ABSTRACT_COMMENT
65 #undef ASSERT_IMPLEMENTS_child_begin
67 } // end unnamed namespace
69 Comment::child_iterator
Comment::child_begin() const {
70 switch (getCommentKind()) {
71 case NoCommentKind
: llvm_unreachable("comment without a kind");
72 #define ABSTRACT_COMMENT(COMMENT)
73 #define COMMENT(CLASS, PARENT) \
75 return static_cast<const CLASS *>(this)->child_begin();
76 #include "clang/AST/CommentNodes.inc"
78 #undef ABSTRACT_COMMENT
80 llvm_unreachable("Unknown comment kind!");
83 Comment::child_iterator
Comment::child_end() const {
84 switch (getCommentKind()) {
85 case NoCommentKind
: llvm_unreachable("comment without a kind");
86 #define ABSTRACT_COMMENT(COMMENT)
87 #define COMMENT(CLASS, PARENT) \
89 return static_cast<const CLASS *>(this)->child_end();
90 #include "clang/AST/CommentNodes.inc"
92 #undef ABSTRACT_COMMENT
94 llvm_unreachable("Unknown comment kind!");
97 bool TextComment::isWhitespaceNoCache() const {
98 for (StringRef::const_iterator I
= Text
.begin(), E
= Text
.end();
100 if (!clang::isWhitespace(*I
))
106 bool ParagraphComment::isWhitespaceNoCache() const {
107 for (child_iterator I
= child_begin(), E
= child_end(); I
!= E
; ++I
) {
108 if (const TextComment
*TC
= dyn_cast
<TextComment
>(*I
)) {
109 if (!TC
->isWhitespace())
117 const char *ParamCommandComment::getDirectionAsString(PassDirection D
) {
119 case ParamCommandComment::In
:
121 case ParamCommandComment::Out
:
123 case ParamCommandComment::InOut
:
126 llvm_unreachable("unknown PassDirection");
129 void DeclInfo::fill() {
134 TemplateKind
= NotTemplate
;
135 IsObjCMethod
= false;
136 IsInstanceMethod
= false;
137 IsClassMethod
= false;
139 TemplateParameters
= nullptr;
142 // If there is no declaration, the defaults is our only guess.
146 CurrentDecl
= CommentDecl
;
148 Decl::Kind K
= CommentDecl
->getKind();
151 // Defaults are should be good for declarations we don't handle explicitly.
154 case Decl::CXXMethod
:
155 case Decl::CXXConstructor
:
156 case Decl::CXXDestructor
:
157 case Decl::CXXConversion
: {
158 const FunctionDecl
*FD
= cast
<FunctionDecl
>(CommentDecl
);
160 ParamVars
= llvm::makeArrayRef(FD
->param_begin(), FD
->getNumParams());
161 ReturnType
= FD
->getReturnType();
162 unsigned NumLists
= FD
->getNumTemplateParameterLists();
164 TemplateKind
= TemplateSpecialization
;
166 FD
->getTemplateParameterList(NumLists
- 1);
169 if (K
== Decl::CXXMethod
|| K
== Decl::CXXConstructor
||
170 K
== Decl::CXXDestructor
|| K
== Decl::CXXConversion
) {
171 const CXXMethodDecl
*MD
= cast
<CXXMethodDecl
>(CommentDecl
);
172 IsInstanceMethod
= MD
->isInstance();
173 IsClassMethod
= !IsInstanceMethod
;
177 case Decl::ObjCMethod
: {
178 const ObjCMethodDecl
*MD
= cast
<ObjCMethodDecl
>(CommentDecl
);
180 ParamVars
= llvm::makeArrayRef(MD
->param_begin(), MD
->param_size());
181 ReturnType
= MD
->getReturnType();
183 IsInstanceMethod
= MD
->isInstanceMethod();
184 IsClassMethod
= !IsInstanceMethod
;
187 case Decl::FunctionTemplate
: {
188 const FunctionTemplateDecl
*FTD
= cast
<FunctionTemplateDecl
>(CommentDecl
);
190 TemplateKind
= Template
;
191 const FunctionDecl
*FD
= FTD
->getTemplatedDecl();
192 ParamVars
= llvm::makeArrayRef(FD
->param_begin(), FD
->getNumParams());
193 ReturnType
= FD
->getReturnType();
194 TemplateParameters
= FTD
->getTemplateParameters();
197 case Decl::ClassTemplate
: {
198 const ClassTemplateDecl
*CTD
= cast
<ClassTemplateDecl
>(CommentDecl
);
200 TemplateKind
= Template
;
201 TemplateParameters
= CTD
->getTemplateParameters();
204 case Decl::ClassTemplatePartialSpecialization
: {
205 const ClassTemplatePartialSpecializationDecl
*CTPSD
=
206 cast
<ClassTemplatePartialSpecializationDecl
>(CommentDecl
);
208 TemplateKind
= TemplatePartialSpecialization
;
209 TemplateParameters
= CTPSD
->getTemplateParameters();
212 case Decl::ClassTemplateSpecialization
:
214 TemplateKind
= TemplateSpecialization
;
217 case Decl::CXXRecord
:
222 case Decl::EnumConstant
:
224 case Decl::ObjCAtDefsField
:
227 case Decl::Namespace
:
228 Kind
= NamespaceKind
;
230 case Decl::Typedef
: {
232 // If this is a typedef to something we consider a function, extract
233 // arguments and return type.
234 const TypedefDecl
*TD
= cast
<TypedefDecl
>(CommentDecl
);
235 const TypeSourceInfo
*TSI
= TD
->getTypeSourceInfo();
238 TypeLoc TL
= TSI
->getTypeLoc().getUnqualifiedLoc();
240 TL
= TL
.IgnoreParens();
241 // Look through qualified types.
242 if (QualifiedTypeLoc QualifiedTL
= TL
.getAs
<QualifiedTypeLoc
>()) {
243 TL
= QualifiedTL
.getUnqualifiedLoc();
246 // Look through pointer types.
247 if (PointerTypeLoc PointerTL
= TL
.getAs
<PointerTypeLoc
>()) {
248 TL
= PointerTL
.getPointeeLoc().getUnqualifiedLoc();
251 // Look through reference types.
252 if (ReferenceTypeLoc ReferenceTL
= TL
.getAs
<ReferenceTypeLoc
>()) {
253 TL
= ReferenceTL
.getPointeeLoc().getUnqualifiedLoc();
256 // Look through adjusted types.
257 if (AdjustedTypeLoc ATL
= TL
.getAs
<AdjustedTypeLoc
>()) {
258 TL
= ATL
.getOriginalLoc();
261 if (BlockPointerTypeLoc BlockPointerTL
=
262 TL
.getAs
<BlockPointerTypeLoc
>()) {
263 TL
= BlockPointerTL
.getPointeeLoc().getUnqualifiedLoc();
266 if (MemberPointerTypeLoc MemberPointerTL
=
267 TL
.getAs
<MemberPointerTypeLoc
>()) {
268 TL
= MemberPointerTL
.getPointeeLoc().getUnqualifiedLoc();
271 if (ElaboratedTypeLoc ETL
= TL
.getAs
<ElaboratedTypeLoc
>()) {
272 TL
= ETL
.getNamedTypeLoc();
275 // Is this a typedef for a function type?
276 if (FunctionTypeLoc FTL
= TL
.getAs
<FunctionTypeLoc
>()) {
278 ParamVars
= FTL
.getParams();
279 ReturnType
= FTL
.getReturnLoc().getType();
282 if (TemplateSpecializationTypeLoc STL
=
283 TL
.getAs
<TemplateSpecializationTypeLoc
>()) {
284 // If we have a typedef to a template specialization with exactly one
285 // template argument of a function type, this looks like std::function,
286 // boost::function, or other function wrapper. Treat these typedefs as
288 if (STL
.getNumArgs() != 1)
290 TemplateArgumentLoc MaybeFunction
= STL
.getArgLoc(0);
291 if (MaybeFunction
.getArgument().getKind() != TemplateArgument::Type
)
293 TypeSourceInfo
*MaybeFunctionTSI
= MaybeFunction
.getTypeSourceInfo();
294 TypeLoc TL
= MaybeFunctionTSI
->getTypeLoc().getUnqualifiedLoc();
295 if (FunctionTypeLoc FTL
= TL
.getAs
<FunctionTypeLoc
>()) {
297 ParamVars
= FTL
.getParams();
298 ReturnType
= FTL
.getReturnLoc().getType();
306 case Decl::TypeAlias
:
309 case Decl::TypeAliasTemplate
: {
310 const TypeAliasTemplateDecl
*TAT
= cast
<TypeAliasTemplateDecl
>(CommentDecl
);
312 TemplateKind
= Template
;
313 TemplateParameters
= TAT
->getTemplateParameters();
324 StringRef
ParamCommandComment::getParamName(const FullComment
*FC
) const {
325 assert(isParamIndexValid());
328 return FC
->getDeclInfo()->ParamVars
[getParamIndex()]->getName();
331 StringRef
TParamCommandComment::getParamName(const FullComment
*FC
) const {
332 assert(isPositionValid());
333 const TemplateParameterList
*TPL
= FC
->getDeclInfo()->TemplateParameters
;
334 for (unsigned i
= 0, e
= getDepth(); i
!= e
; ++i
) {
336 return TPL
->getParam(getIndex(i
))->getName();
337 const NamedDecl
*Param
= TPL
->getParam(getIndex(i
));
338 if (const TemplateTemplateParmDecl
*TTP
=
339 dyn_cast
<TemplateTemplateParmDecl
>(Param
))
340 TPL
= TTP
->getTemplateParameters();
345 } // end namespace comments
346 } // end namespace clang