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 "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/raw_ostream.h"
21 const char *Comment::getCommentKindName() const {
22 switch (getCommentKind()) {
23 case NoCommentKind: return "NoCommentKind";
24 #define ABSTRACT_COMMENT(COMMENT)
25 #define COMMENT(CLASS, PARENT) \
28 #include "clang/AST/CommentNodes.inc"
30 #undef ABSTRACT_COMMENT
32 llvm_unreachable("Unknown comment kind!");
40 good implements_child_begin_end(Comment::child_iterator (T::*)() const) {
44 static inline bad implements_child_begin_end(
45 Comment::child_iterator (Comment::*)() const) {
49 #define ASSERT_IMPLEMENTS_child_begin(function) \
50 (void) sizeof(good(implements_child_begin_end(function)))
52 static inline void CheckCommentASTNodes() {
53 #define ABSTRACT_COMMENT(COMMENT)
54 #define COMMENT(CLASS, PARENT) \
55 ASSERT_IMPLEMENTS_child_begin(&CLASS::child_begin); \
56 ASSERT_IMPLEMENTS_child_begin(&CLASS::child_end);
57 #include "clang/AST/CommentNodes.inc"
59 #undef ABSTRACT_COMMENT
62 #undef ASSERT_IMPLEMENTS_child_begin
64 } // end unnamed namespace
66 Comment::child_iterator Comment::child_begin() const {
67 switch (getCommentKind()) {
68 case NoCommentKind: llvm_unreachable("comment without a kind");
69 #define ABSTRACT_COMMENT(COMMENT)
70 #define COMMENT(CLASS, PARENT) \
72 return static_cast<const CLASS *>(this)->child_begin();
73 #include "clang/AST/CommentNodes.inc"
75 #undef ABSTRACT_COMMENT
77 llvm_unreachable("Unknown comment kind!");
80 Comment::child_iterator Comment::child_end() const {
81 switch (getCommentKind()) {
82 case NoCommentKind: llvm_unreachable("comment without a kind");
83 #define ABSTRACT_COMMENT(COMMENT)
84 #define COMMENT(CLASS, PARENT) \
86 return static_cast<const CLASS *>(this)->child_end();
87 #include "clang/AST/CommentNodes.inc"
89 #undef ABSTRACT_COMMENT
91 llvm_unreachable("Unknown comment kind!");
94 bool TextComment::isWhitespaceNoCache() const {
95 for (StringRef::const_iterator I = Text.begin(), E = Text.end();
98 if (C != ' ' && C != '\n' && C != '\r' &&
99 C != '\t' && C != '\f' && C != '\v')
105 bool ParagraphComment::isWhitespaceNoCache() const {
106 for (child_iterator I = child_begin(), E = child_end(); I != E; ++I) {
107 if (const TextComment *TC = dyn_cast<TextComment>(*I)) {
108 if (!TC->isWhitespace())
116 const char *ParamCommandComment::getDirectionAsString(PassDirection D) {
118 case ParamCommandComment::In:
120 case ParamCommandComment::Out:
122 case ParamCommandComment::InOut:
125 llvm_unreachable("unknown PassDirection");
128 void DeclInfo::fill() {
133 TemplateKind = NotTemplate;
134 IsObjCMethod = false;
135 IsInstanceMethod = false;
136 IsClassMethod = false;
138 TemplateParameters = NULL;
141 // If there is no declaration, the defaults is our only guess.
145 CurrentDecl = CommentDecl;
147 Decl::Kind K = CommentDecl->getKind();
150 // Defaults are should be good for declarations we don't handle explicitly.
153 case Decl::CXXMethod:
154 case Decl::CXXConstructor:
155 case Decl::CXXDestructor:
156 case Decl::CXXConversion: {
157 const FunctionDecl *FD = cast<FunctionDecl>(CommentDecl);
159 ParamVars = ArrayRef<const ParmVarDecl *>(FD->param_begin(),
161 ResultType = FD->getResultType();
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 = ArrayRef<const ParmVarDecl *>(MD->param_begin(),
182 ResultType = MD->getResultType();
184 IsInstanceMethod = MD->isInstanceMethod();
185 IsClassMethod = !IsInstanceMethod;
188 case Decl::FunctionTemplate: {
189 const FunctionTemplateDecl *FTD = cast<FunctionTemplateDecl>(CommentDecl);
191 TemplateKind = Template;
192 const FunctionDecl *FD = FTD->getTemplatedDecl();
193 ParamVars = ArrayRef<const ParmVarDecl *>(FD->param_begin(),
195 ResultType = FD->getResultType();
196 TemplateParameters = FTD->getTemplateParameters();
199 case Decl::ClassTemplate: {
200 const ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(CommentDecl);
202 TemplateKind = Template;
203 TemplateParameters = CTD->getTemplateParameters();
206 case Decl::ClassTemplatePartialSpecialization: {
207 const ClassTemplatePartialSpecializationDecl *CTPSD =
208 cast<ClassTemplatePartialSpecializationDecl>(CommentDecl);
210 TemplateKind = TemplatePartialSpecialization;
211 TemplateParameters = CTPSD->getTemplateParameters();
214 case Decl::ClassTemplateSpecialization:
216 TemplateKind = TemplateSpecialization;
219 case Decl::CXXRecord:
224 case Decl::EnumConstant:
226 case Decl::ObjCAtDefsField:
229 case Decl::Namespace:
230 Kind = NamespaceKind;
232 case Decl::Typedef: {
234 // If this is a typedef to something we consider a function, extract
235 // arguments and return type.
236 const TypedefDecl *TD = cast<TypedefDecl>(CommentDecl);
237 const TypeSourceInfo *TSI = TD->getTypeSourceInfo();
240 TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
242 TL = TL.IgnoreParens();
243 // Look through qualified types.
244 if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>()) {
245 TL = QualifiedTL.getUnqualifiedLoc();
248 // Look through pointer types.
249 if (PointerTypeLoc PointerTL = TL.getAs<PointerTypeLoc>()) {
250 TL = PointerTL.getPointeeLoc().getUnqualifiedLoc();
253 if (BlockPointerTypeLoc BlockPointerTL =
254 TL.getAs<BlockPointerTypeLoc>()) {
255 TL = BlockPointerTL.getPointeeLoc().getUnqualifiedLoc();
258 if (MemberPointerTypeLoc MemberPointerTL =
259 TL.getAs<MemberPointerTypeLoc>()) {
260 TL = MemberPointerTL.getPointeeLoc().getUnqualifiedLoc();
263 // Is this a typedef for a function type?
264 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
266 ArrayRef<ParmVarDecl *> Params = FTL.getParams();
267 ParamVars = ArrayRef<const ParmVarDecl *>(Params.data(),
269 ResultType = FTL.getResultLoc().getType();
276 case Decl::TypeAlias:
279 case Decl::TypeAliasTemplate: {
280 const TypeAliasTemplateDecl *TAT = cast<TypeAliasTemplateDecl>(CommentDecl);
282 TemplateKind = Template;
283 TemplateParameters = TAT->getTemplateParameters();
294 StringRef ParamCommandComment::getParamName(const FullComment *FC) const {
295 assert(isParamIndexValid());
296 return FC->getThisDeclInfo()->ParamVars[getParamIndex()]->getName();
299 StringRef TParamCommandComment::getParamName(const FullComment *FC) const {
300 assert(isPositionValid());
301 const TemplateParameterList *TPL = FC->getThisDeclInfo()->TemplateParameters;
302 for (unsigned i = 0, e = getDepth(); i != e; ++i) {
304 return TPL->getParam(getIndex(i))->getName();
305 const NamedDecl *Param = TPL->getParam(getIndex(i));
306 if (const TemplateTemplateParmDecl *TTP =
307 dyn_cast<TemplateTemplateParmDecl>(Param))
308 TPL = TTP->getTemplateParameters();
313 } // end namespace comments
314 } // end namespace clang