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 = NULL;
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 = ArrayRef<const ParmVarDecl *>(FD->param_begin(),
162 ResultType = FD->getResultType();
163 unsigned NumLists = FD->getNumTemplateParameterLists();
165 TemplateKind = TemplateSpecialization;
167 FD->getTemplateParameterList(NumLists - 1);
170 if (K == Decl::CXXMethod || K == Decl::CXXConstructor ||
171 K == Decl::CXXDestructor || K == Decl::CXXConversion) {
172 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CommentDecl);
173 IsInstanceMethod = MD->isInstance();
174 IsClassMethod = !IsInstanceMethod;
178 case Decl::ObjCMethod: {
179 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(CommentDecl);
181 ParamVars = ArrayRef<const ParmVarDecl *>(MD->param_begin(),
183 ResultType = MD->getResultType();
185 IsInstanceMethod = MD->isInstanceMethod();
186 IsClassMethod = !IsInstanceMethod;
189 case Decl::FunctionTemplate: {
190 const FunctionTemplateDecl *FTD = cast<FunctionTemplateDecl>(CommentDecl);
192 TemplateKind = Template;
193 const FunctionDecl *FD = FTD->getTemplatedDecl();
194 ParamVars = ArrayRef<const ParmVarDecl *>(FD->param_begin(),
196 ResultType = FD->getResultType();
197 TemplateParameters = FTD->getTemplateParameters();
200 case Decl::ClassTemplate: {
201 const ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(CommentDecl);
203 TemplateKind = Template;
204 TemplateParameters = CTD->getTemplateParameters();
207 case Decl::ClassTemplatePartialSpecialization: {
208 const ClassTemplatePartialSpecializationDecl *CTPSD =
209 cast<ClassTemplatePartialSpecializationDecl>(CommentDecl);
211 TemplateKind = TemplatePartialSpecialization;
212 TemplateParameters = CTPSD->getTemplateParameters();
215 case Decl::ClassTemplateSpecialization:
217 TemplateKind = TemplateSpecialization;
220 case Decl::CXXRecord:
225 case Decl::EnumConstant:
227 case Decl::ObjCAtDefsField:
230 case Decl::Namespace:
231 Kind = NamespaceKind;
233 case Decl::Typedef: {
235 // If this is a typedef to something we consider a function, extract
236 // arguments and return type.
237 const TypedefDecl *TD = cast<TypedefDecl>(CommentDecl);
238 const TypeSourceInfo *TSI = TD->getTypeSourceInfo();
241 TypeLoc TL = TSI->getTypeLoc().getUnqualifiedLoc();
243 TL = TL.IgnoreParens();
244 // Look through qualified types.
245 if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>()) {
246 TL = QualifiedTL.getUnqualifiedLoc();
249 // Look through pointer types.
250 if (PointerTypeLoc PointerTL = TL.getAs<PointerTypeLoc>()) {
251 TL = PointerTL.getPointeeLoc().getUnqualifiedLoc();
254 if (BlockPointerTypeLoc BlockPointerTL =
255 TL.getAs<BlockPointerTypeLoc>()) {
256 TL = BlockPointerTL.getPointeeLoc().getUnqualifiedLoc();
259 if (MemberPointerTypeLoc MemberPointerTL =
260 TL.getAs<MemberPointerTypeLoc>()) {
261 TL = MemberPointerTL.getPointeeLoc().getUnqualifiedLoc();
264 // Is this a typedef for a function type?
265 if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) {
267 ArrayRef<ParmVarDecl *> Params = FTL.getParams();
268 ParamVars = ArrayRef<const ParmVarDecl *>(Params.data(),
270 ResultType = FTL.getResultLoc().getType();
277 case Decl::TypeAlias:
280 case Decl::TypeAliasTemplate: {
281 const TypeAliasTemplateDecl *TAT = cast<TypeAliasTemplateDecl>(CommentDecl);
283 TemplateKind = Template;
284 TemplateParameters = TAT->getTemplateParameters();
295 StringRef ParamCommandComment::getParamName(const FullComment *FC) const {
296 assert(isParamIndexValid());
299 return FC->getDeclInfo()->ParamVars[getParamIndex()]->getName();
302 StringRef TParamCommandComment::getParamName(const FullComment *FC) const {
303 assert(isPositionValid());
304 const TemplateParameterList *TPL = FC->getDeclInfo()->TemplateParameters;
305 for (unsigned i = 0, e = getDepth(); i != e; ++i) {
307 return TPL->getParam(getIndex(i))->getName();
308 const NamedDecl *Param = TPL->getParam(getIndex(i));
309 if (const TemplateTemplateParmDecl *TTP =
310 dyn_cast<TemplateTemplateParmDecl>(Param))
311 TPL = TTP->getTemplateParameters();
316 } // end namespace comments
317 } // end namespace clang