Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 1 | //===--- Comment.cpp - Comment AST node implementation --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "clang/AST/Comment.h" |
Dmitri Gribenko | 1ca7ecc | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 11 | #include "clang/AST/Decl.h" |
| 12 | #include "clang/AST/DeclObjC.h" |
| 13 | #include "clang/AST/DeclTemplate.h" |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 14 | #include "llvm/Support/ErrorHandling.h" |
Dmitri Gribenko | fb3643a | 2012-07-18 16:30:42 +0000 | [diff] [blame] | 15 | #include "llvm/Support/raw_ostream.h" |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 16 | |
| 17 | namespace clang { |
| 18 | namespace comments { |
| 19 | |
| 20 | const char *Comment::getCommentKindName() const { |
| 21 | switch (getCommentKind()) { |
| 22 | case NoCommentKind: return "NoCommentKind"; |
| 23 | #define ABSTRACT_COMMENT(COMMENT) |
| 24 | #define COMMENT(CLASS, PARENT) \ |
| 25 | case CLASS##Kind: \ |
| 26 | return #CLASS; |
| 27 | #include "clang/AST/CommentNodes.inc" |
| 28 | #undef COMMENT |
| 29 | #undef ABSTRACT_COMMENT |
| 30 | } |
| 31 | llvm_unreachable("Unknown comment kind!"); |
| 32 | } |
| 33 | |
Dmitri Gribenko | fb3643a | 2012-07-18 16:30:42 +0000 | [diff] [blame] | 34 | void Comment::dump() const { |
| 35 | // It is important that Comment::dump() is defined in a different TU than |
| 36 | // Comment::dump(raw_ostream, SourceManager). If both functions were defined |
| 37 | // in CommentDumper.cpp, that object file would be removed by linker because |
| 38 | // none of its functions are referenced by other object files, despite the |
| 39 | // LLVM_ATTRIBUTE_USED. |
| 40 | dump(llvm::errs(), NULL); |
| 41 | } |
| 42 | |
| 43 | void Comment::dump(SourceManager &SM) const { |
| 44 | dump(llvm::errs(), &SM); |
| 45 | } |
| 46 | |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 47 | namespace { |
| 48 | struct good {}; |
| 49 | struct bad {}; |
| 50 | |
| 51 | template <typename T> |
| 52 | good implements_child_begin_end(Comment::child_iterator (T::*)() const) { |
| 53 | return good(); |
| 54 | } |
| 55 | |
| 56 | static inline bad implements_child_begin_end( |
| 57 | Comment::child_iterator (Comment::*)() const) { |
| 58 | return bad(); |
| 59 | } |
| 60 | |
| 61 | #define ASSERT_IMPLEMENTS_child_begin(function) \ |
| 62 | (void) sizeof(good(implements_child_begin_end(function))) |
| 63 | |
| 64 | static inline void CheckCommentASTNodes() { |
| 65 | #define ABSTRACT_COMMENT(COMMENT) |
| 66 | #define COMMENT(CLASS, PARENT) \ |
| 67 | ASSERT_IMPLEMENTS_child_begin(&CLASS::child_begin); \ |
| 68 | ASSERT_IMPLEMENTS_child_begin(&CLASS::child_end); |
| 69 | #include "clang/AST/CommentNodes.inc" |
| 70 | #undef COMMENT |
| 71 | #undef ABSTRACT_COMMENT |
| 72 | } |
| 73 | |
| 74 | #undef ASSERT_IMPLEMENTS_child_begin |
| 75 | |
| 76 | } // end unnamed namespace |
| 77 | |
| 78 | Comment::child_iterator Comment::child_begin() const { |
| 79 | switch (getCommentKind()) { |
| 80 | case NoCommentKind: llvm_unreachable("comment without a kind"); |
| 81 | #define ABSTRACT_COMMENT(COMMENT) |
| 82 | #define COMMENT(CLASS, PARENT) \ |
| 83 | case CLASS##Kind: \ |
| 84 | return static_cast<const CLASS *>(this)->child_begin(); |
| 85 | #include "clang/AST/CommentNodes.inc" |
| 86 | #undef COMMENT |
| 87 | #undef ABSTRACT_COMMENT |
| 88 | } |
Matt Beaumont-Gay | 4d48b5c | 2012-07-06 21:13:09 +0000 | [diff] [blame] | 89 | llvm_unreachable("Unknown comment kind!"); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | Comment::child_iterator Comment::child_end() const { |
| 93 | switch (getCommentKind()) { |
| 94 | case NoCommentKind: llvm_unreachable("comment without a kind"); |
| 95 | #define ABSTRACT_COMMENT(COMMENT) |
| 96 | #define COMMENT(CLASS, PARENT) \ |
| 97 | case CLASS##Kind: \ |
| 98 | return static_cast<const CLASS *>(this)->child_end(); |
| 99 | #include "clang/AST/CommentNodes.inc" |
| 100 | #undef COMMENT |
| 101 | #undef ABSTRACT_COMMENT |
| 102 | } |
Matt Beaumont-Gay | 4d48b5c | 2012-07-06 21:13:09 +0000 | [diff] [blame] | 103 | llvm_unreachable("Unknown comment kind!"); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Dmitri Gribenko | 0f7f10b | 2012-07-18 20:54:32 +0000 | [diff] [blame] | 106 | bool TextComment::isWhitespaceNoCache() const { |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 107 | for (StringRef::const_iterator I = Text.begin(), E = Text.end(); |
| 108 | I != E; ++I) { |
| 109 | const char C = *I; |
| 110 | if (C != ' ' && C != '\n' && C != '\r' && |
| 111 | C != '\t' && C != '\f' && C != '\v') |
| 112 | return false; |
| 113 | } |
| 114 | return true; |
| 115 | } |
| 116 | |
Dmitri Gribenko | 0f7f10b | 2012-07-18 20:54:32 +0000 | [diff] [blame] | 117 | bool ParagraphComment::isWhitespaceNoCache() const { |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 118 | for (child_iterator I = child_begin(), E = child_end(); I != E; ++I) { |
| 119 | if (const TextComment *TC = dyn_cast<TextComment>(*I)) { |
| 120 | if (!TC->isWhitespace()) |
| 121 | return false; |
Dmitri Gribenko | 858e69f | 2012-07-19 00:01:56 +0000 | [diff] [blame] | 122 | } else |
| 123 | return false; |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 124 | } |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | const char *ParamCommandComment::getDirectionAsString(PassDirection D) { |
| 129 | switch (D) { |
| 130 | case ParamCommandComment::In: |
| 131 | return "[in]"; |
| 132 | case ParamCommandComment::Out: |
| 133 | return "[out]"; |
| 134 | case ParamCommandComment::InOut: |
| 135 | return "[in,out]"; |
| 136 | } |
| 137 | llvm_unreachable("unknown PassDirection"); |
| 138 | } |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 139 | |
Dmitri Gribenko | 1ca7ecc | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 140 | void DeclInfo::fill() { |
| 141 | assert(!IsFilled); |
| 142 | |
| 143 | // Set defaults. |
Dmitri Gribenko | af19a6a | 2012-08-02 21:45:39 +0000 | [diff] [blame] | 144 | Kind = FunctionKind; |
Dmitri Gribenko | 1ca7ecc | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 145 | IsTemplateDecl = false; |
| 146 | IsTemplateSpecialization = false; |
| 147 | IsTemplatePartialSpecialization = false; |
| 148 | IsInstanceMethod = false; |
| 149 | IsClassMethod = false; |
| 150 | ParamVars = ArrayRef<const ParmVarDecl *>(); |
| 151 | TemplateParameters = NULL; |
| 152 | |
| 153 | if (!ThisDecl) { |
Dmitri Gribenko | 5b32a08 | 2012-08-03 00:01:01 +0000 | [diff] [blame^] | 154 | // If there is no declaration, the defaults is our only guess. |
| 155 | IsFilled = true; |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | Decl::Kind K = ThisDecl->getKind(); |
| 160 | switch (K) { |
| 161 | default: |
| 162 | // Defaults are should be good for declarations we don't handle explicitly. |
| 163 | break; |
| 164 | case Decl::Function: |
| 165 | case Decl::CXXMethod: |
| 166 | case Decl::CXXConstructor: |
| 167 | case Decl::CXXDestructor: |
| 168 | case Decl::CXXConversion: { |
| 169 | const FunctionDecl *FD = cast<FunctionDecl>(ThisDecl); |
Dmitri Gribenko | af19a6a | 2012-08-02 21:45:39 +0000 | [diff] [blame] | 170 | Kind = FunctionKind; |
Dmitri Gribenko | 1ca7ecc | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 171 | ParamVars = ArrayRef<const ParmVarDecl *>(FD->param_begin(), |
| 172 | FD->getNumParams()); |
| 173 | unsigned NumLists = FD->getNumTemplateParameterLists(); |
| 174 | if (NumLists != 0) { |
| 175 | IsTemplateDecl = true; |
| 176 | IsTemplateSpecialization = true; |
| 177 | TemplateParameters = |
| 178 | FD->getTemplateParameterList(NumLists - 1); |
| 179 | } |
| 180 | |
Dmitri Gribenko | 5b32a08 | 2012-08-03 00:01:01 +0000 | [diff] [blame^] | 181 | if (K == Decl::CXXMethod) { |
| 182 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(ThisDecl); |
Dmitri Gribenko | 1ca7ecc | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 183 | IsInstanceMethod = MD->isInstance(); |
| 184 | IsClassMethod = !IsInstanceMethod; |
| 185 | } |
Dmitri Gribenko | 5b32a08 | 2012-08-03 00:01:01 +0000 | [diff] [blame^] | 186 | break; |
| 187 | } |
| 188 | case Decl::ObjCMethod: { |
| 189 | const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(ThisDecl); |
Dmitri Gribenko | af19a6a | 2012-08-02 21:45:39 +0000 | [diff] [blame] | 190 | Kind = FunctionKind; |
Dmitri Gribenko | 1ca7ecc | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 191 | ParamVars = ArrayRef<const ParmVarDecl *>(MD->param_begin(), |
| 192 | MD->param_size()); |
| 193 | IsInstanceMethod = MD->isInstanceMethod(); |
| 194 | IsClassMethod = !IsInstanceMethod; |
Dmitri Gribenko | 5b32a08 | 2012-08-03 00:01:01 +0000 | [diff] [blame^] | 195 | break; |
| 196 | } |
| 197 | case Decl::FunctionTemplate: { |
| 198 | const FunctionTemplateDecl *FTD = cast<FunctionTemplateDecl>(ThisDecl); |
Dmitri Gribenko | af19a6a | 2012-08-02 21:45:39 +0000 | [diff] [blame] | 199 | Kind = FunctionKind; |
Dmitri Gribenko | 1ca7ecc | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 200 | IsTemplateDecl = true; |
| 201 | const FunctionDecl *FD = FTD->getTemplatedDecl(); |
| 202 | ParamVars = ArrayRef<const ParmVarDecl *>(FD->param_begin(), |
| 203 | FD->getNumParams()); |
| 204 | TemplateParameters = FTD->getTemplateParameters(); |
Dmitri Gribenko | 5b32a08 | 2012-08-03 00:01:01 +0000 | [diff] [blame^] | 205 | break; |
| 206 | } |
| 207 | case Decl::ClassTemplate: { |
| 208 | const ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(ThisDecl); |
Dmitri Gribenko | af19a6a | 2012-08-02 21:45:39 +0000 | [diff] [blame] | 209 | Kind = ClassKind; |
Dmitri Gribenko | 1ca7ecc | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 210 | IsTemplateDecl = true; |
| 211 | TemplateParameters = CTD->getTemplateParameters(); |
Dmitri Gribenko | 5b32a08 | 2012-08-03 00:01:01 +0000 | [diff] [blame^] | 212 | break; |
| 213 | } |
| 214 | case Decl::ClassTemplatePartialSpecialization: { |
| 215 | const ClassTemplatePartialSpecializationDecl *CTPSD = |
| 216 | cast<ClassTemplatePartialSpecializationDecl>(ThisDecl); |
Dmitri Gribenko | af19a6a | 2012-08-02 21:45:39 +0000 | [diff] [blame] | 217 | Kind = ClassKind; |
Dmitri Gribenko | 1ca7ecc | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 218 | IsTemplateDecl = true; |
| 219 | IsTemplatePartialSpecialization = true; |
| 220 | TemplateParameters = CTPSD->getTemplateParameters(); |
Dmitri Gribenko | 5b32a08 | 2012-08-03 00:01:01 +0000 | [diff] [blame^] | 221 | break; |
| 222 | } |
| 223 | case Decl::ClassTemplateSpecialization: |
Dmitri Gribenko | af19a6a | 2012-08-02 21:45:39 +0000 | [diff] [blame] | 224 | Kind = ClassKind; |
Dmitri Gribenko | 1ca7ecc | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 225 | IsTemplateDecl = true; |
| 226 | IsTemplateSpecialization = true; |
Dmitri Gribenko | 5b32a08 | 2012-08-03 00:01:01 +0000 | [diff] [blame^] | 227 | break; |
| 228 | case Decl::Record: |
Dmitri Gribenko | af19a6a | 2012-08-02 21:45:39 +0000 | [diff] [blame] | 229 | Kind = ClassKind; |
Dmitri Gribenko | 5b32a08 | 2012-08-03 00:01:01 +0000 | [diff] [blame^] | 230 | break; |
| 231 | case Decl::Var: |
| 232 | case Decl::Field: |
| 233 | case Decl::ObjCIvar: |
| 234 | case Decl::ObjCAtDefsField: |
Dmitri Gribenko | af19a6a | 2012-08-02 21:45:39 +0000 | [diff] [blame] | 235 | Kind = VariableKind; |
Dmitri Gribenko | 5b32a08 | 2012-08-03 00:01:01 +0000 | [diff] [blame^] | 236 | break; |
| 237 | case Decl::Namespace: |
Dmitri Gribenko | af19a6a | 2012-08-02 21:45:39 +0000 | [diff] [blame] | 238 | Kind = NamespaceKind; |
Dmitri Gribenko | 5b32a08 | 2012-08-03 00:01:01 +0000 | [diff] [blame^] | 239 | break; |
| 240 | case Decl::Typedef: |
| 241 | case Decl::TypeAlias: |
Dmitri Gribenko | af19a6a | 2012-08-02 21:45:39 +0000 | [diff] [blame] | 242 | Kind = TypedefKind; |
Dmitri Gribenko | 5b32a08 | 2012-08-03 00:01:01 +0000 | [diff] [blame^] | 243 | break; |
| 244 | case Decl::TypeAliasTemplate: { |
| 245 | const TypeAliasTemplateDecl *TAT = cast<TypeAliasTemplateDecl>(ThisDecl); |
Dmitri Gribenko | af19a6a | 2012-08-02 21:45:39 +0000 | [diff] [blame] | 246 | Kind = TypedefKind; |
Dmitri Gribenko | 967e5d7 | 2012-08-02 21:36:57 +0000 | [diff] [blame] | 247 | IsTemplateDecl = true; |
| 248 | TemplateParameters = TAT->getTemplateParameters(); |
Dmitri Gribenko | 5b32a08 | 2012-08-03 00:01:01 +0000 | [diff] [blame^] | 249 | break; |
Dmitri Gribenko | 1ca7ecc | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 250 | } |
Dmitri Gribenko | 5b32a08 | 2012-08-03 00:01:01 +0000 | [diff] [blame^] | 251 | } |
| 252 | |
Dmitri Gribenko | 1ca7ecc | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 253 | IsFilled = true; |
| 254 | } |
| 255 | |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 256 | } // end namespace comments |
| 257 | } // end namespace clang |
Dmitri Gribenko | af19a6a | 2012-08-02 21:45:39 +0000 | [diff] [blame] | 258 | |