Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 1 | //===--- DeclBase.cpp - Declaration 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 | // This file implements the Decl and DeclContext classes. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/DeclBase.h" |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 15 | #include "clang/AST/Decl.h" |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclContextInternals.h" |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
John McCall | 92b7f70 | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclFriend.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
| 20 | #include "clang/AST/DeclTemplate.h" |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 21 | #include "clang/AST/DependentDiagnostic.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExternalASTSource.h" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 23 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 24 | #include "clang/AST/Type.h" |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 25 | #include "clang/AST/Stmt.h" |
| 26 | #include "clang/AST/StmtCXX.h" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 29 | #include <algorithm> |
Chris Lattner | 3daed52 | 2009-03-02 22:20:04 +0000 | [diff] [blame] | 30 | #include <cstdio> |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 31 | #include <vector> |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 32 | using namespace clang; |
| 33 | |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | // Statistics |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 38 | #define DECL(Derived, Base) static int n##Derived##s = 0; |
| 39 | #include "clang/AST/DeclNodes.def" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 40 | |
| 41 | static bool StatSwitch = false; |
| 42 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 43 | const char *Decl::getDeclKindName() const { |
| 44 | switch (DeclKind) { |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 45 | default: assert(0 && "Declaration not in DeclNodes.def!"); |
| 46 | #define DECL(Derived, Base) case Derived: return #Derived; |
| 47 | #include "clang/AST/DeclNodes.def" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
Douglas Gregor | 4273857 | 2010-03-05 00:26:45 +0000 | [diff] [blame] | 51 | void Decl::setInvalidDecl(bool Invalid) { |
| 52 | InvalidDecl = Invalid; |
| 53 | if (Invalid) { |
| 54 | // Defensive maneuver for ill-formed code: we're likely not to make it to |
| 55 | // a point where we set the access specifier, so default it to "public" |
| 56 | // to avoid triggering asserts elsewhere in the front end. |
| 57 | setAccess(AS_public); |
| 58 | } |
| 59 | } |
| 60 | |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 61 | const char *DeclContext::getDeclKindName() const { |
| 62 | switch (DeclKind) { |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 63 | default: assert(0 && "Declaration context not in DeclNodes.def!"); |
Argyrios Kyrtzidis | 1ad4dd7 | 2009-02-16 14:28:33 +0000 | [diff] [blame] | 64 | #define DECL(Derived, Base) case Decl::Derived: return #Derived; |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 65 | #include "clang/AST/DeclNodes.def" |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 69 | bool Decl::CollectingStats(bool Enable) { |
Kovarththanan Rajaratnam | 2024f4d | 2009-11-29 14:54:35 +0000 | [diff] [blame] | 70 | if (Enable) StatSwitch = true; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 71 | return StatSwitch; |
| 72 | } |
| 73 | |
| 74 | void Decl::PrintStats() { |
| 75 | fprintf(stderr, "*** Decl Stats:\n"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 76 | |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 77 | int totalDecls = 0; |
| 78 | #define DECL(Derived, Base) totalDecls += n##Derived##s; |
| 79 | #include "clang/AST/DeclNodes.def" |
| 80 | fprintf(stderr, " %d decls total.\n", totalDecls); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 82 | int totalBytes = 0; |
| 83 | #define DECL(Derived, Base) \ |
| 84 | if (n##Derived##s > 0) { \ |
| 85 | totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl)); \ |
| 86 | fprintf(stderr, " %d " #Derived " decls, %d each (%d bytes)\n", \ |
| 87 | n##Derived##s, (int)sizeof(Derived##Decl), \ |
| 88 | (int)(n##Derived##s * sizeof(Derived##Decl))); \ |
| 89 | } |
| 90 | #include "clang/AST/DeclNodes.def" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 92 | fprintf(stderr, "Total bytes = %d\n", totalBytes); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void Decl::addDeclKind(Kind k) { |
| 96 | switch (k) { |
Douglas Gregor | 64650af | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 97 | default: assert(0 && "Declaration not in DeclNodes.def!"); |
| 98 | #define DECL(Derived, Base) case Derived: ++n##Derived##s; break; |
| 99 | #include "clang/AST/DeclNodes.def" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Anders Carlsson | 67e3320 | 2009-06-13 00:08:58 +0000 | [diff] [blame] | 103 | bool Decl::isTemplateParameterPack() const { |
| 104 | if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this)) |
| 105 | return TTP->isParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Anders Carlsson | 67e3320 | 2009-06-13 00:08:58 +0000 | [diff] [blame] | 107 | return false; |
| 108 | } |
| 109 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 110 | bool Decl::isFunctionOrFunctionTemplate() const { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 111 | if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this)) |
Anders Carlsson | 58badb7 | 2009-06-26 05:26:50 +0000 | [diff] [blame] | 112 | return UD->getTargetDecl()->isFunctionOrFunctionTemplate(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 114 | return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this); |
| 115 | } |
| 116 | |
Douglas Gregor | 79c2278 | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 117 | bool Decl::isDefinedOutsideFunctionOrMethod() const { |
| 118 | for (const DeclContext *DC = getDeclContext(); |
| 119 | DC && !DC->isTranslationUnit(); |
| 120 | DC = DC->getParent()) |
| 121 | if (DC->isFunctionOrMethod()) |
| 122 | return false; |
| 123 | |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 128 | //===----------------------------------------------------------------------===// |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 129 | // PrettyStackTraceDecl Implementation |
| 130 | //===----------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 132 | void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const { |
| 133 | SourceLocation TheLoc = Loc; |
| 134 | if (TheLoc.isInvalid() && TheDecl) |
| 135 | TheLoc = TheDecl->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 137 | if (TheLoc.isValid()) { |
| 138 | TheLoc.print(OS, SM); |
| 139 | OS << ": "; |
| 140 | } |
| 141 | |
| 142 | OS << Message; |
| 143 | |
Daniel Dunbar | c523656 | 2009-11-21 09:05:59 +0000 | [diff] [blame] | 144 | if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 145 | OS << " '" << DN->getQualifiedNameAsString() << '\''; |
| 146 | OS << '\n'; |
| 147 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 148 | |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 149 | //===----------------------------------------------------------------------===// |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 150 | // Decl Implementation |
| 151 | //===----------------------------------------------------------------------===// |
| 152 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 153 | // Out-of-line virtual method providing a home for Decl. |
| 154 | Decl::~Decl() { |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 155 | assert(!HasAttrs && "attributes should have been freed by Destroy"); |
| 156 | } |
| 157 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 158 | void Decl::setDeclContext(DeclContext *DC) { |
| 159 | if (isOutOfSemaDC()) |
| 160 | delete getMultipleDC(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 161 | |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 162 | DeclCtx = DC; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void Decl::setLexicalDeclContext(DeclContext *DC) { |
| 166 | if (DC == getLexicalDeclContext()) |
| 167 | return; |
| 168 | |
| 169 | if (isInSemaDC()) { |
Ted Kremenek | 94a3900 | 2009-12-01 00:07:10 +0000 | [diff] [blame] | 170 | MultipleDC *MDC = new (getASTContext()) MultipleDC(); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 171 | MDC->SemanticDC = getDeclContext(); |
| 172 | MDC->LexicalDC = DC; |
Chris Lattner | ee219fd | 2009-03-29 06:06:59 +0000 | [diff] [blame] | 173 | DeclCtx = MDC; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 174 | } else { |
| 175 | getMultipleDC()->LexicalDC = DC; |
| 176 | } |
| 177 | } |
| 178 | |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 179 | bool Decl::isInAnonymousNamespace() const { |
| 180 | const DeclContext *DC = getDeclContext(); |
| 181 | do { |
| 182 | if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC)) |
| 183 | if (ND->isAnonymousNamespace()) |
| 184 | return true; |
| 185 | } while ((DC = DC->getParent())); |
| 186 | |
| 187 | return false; |
| 188 | } |
| 189 | |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 190 | TranslationUnitDecl *Decl::getTranslationUnitDecl() { |
Argyrios Kyrtzidis | 9b34669 | 2009-06-30 02:34:53 +0000 | [diff] [blame] | 191 | if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this)) |
| 192 | return TUD; |
| 193 | |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 194 | DeclContext *DC = getDeclContext(); |
| 195 | assert(DC && "This decl is not contained in a translation unit!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 196 | |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 197 | while (!DC->isTranslationUnit()) { |
| 198 | DC = DC->getParent(); |
| 199 | assert(DC && "This decl is not contained in a translation unit!"); |
| 200 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 202 | return cast<TranslationUnitDecl>(DC); |
| 203 | } |
| 204 | |
| 205 | ASTContext &Decl::getASTContext() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 206 | return getTranslationUnitDecl()->getASTContext(); |
Argyrios Kyrtzidis | 3708b3d | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Tanya Lattner | 12ead49 | 2010-02-17 02:17:21 +0000 | [diff] [blame] | 209 | bool Decl::isUsed() const { |
| 210 | if (Used) |
| 211 | return true; |
| 212 | |
| 213 | // Check for used attribute. |
| 214 | if (hasAttr<UsedAttr>()) |
| 215 | return true; |
| 216 | |
| 217 | // Check redeclarations for used attribute. |
| 218 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 219 | if (I->hasAttr<UsedAttr>() || I->Used) |
| 220 | return true; |
| 221 | } |
| 222 | |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 227 | unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) { |
| 228 | switch (DeclKind) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 229 | case Function: |
| 230 | case CXXMethod: |
| 231 | case CXXConstructor: |
| 232 | case CXXDestructor: |
| 233 | case CXXConversion: |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 234 | case Typedef: |
| 235 | case EnumConstant: |
| 236 | case Var: |
| 237 | case ImplicitParam: |
| 238 | case ParmVar: |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 239 | case NonTypeTemplateParm: |
| 240 | case ObjCMethod: |
| 241 | case ObjCContainer: |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 242 | case ObjCInterface: |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 243 | case ObjCProperty: |
| 244 | case ObjCCompatibleAlias: |
| 245 | return IDNS_Ordinary; |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 246 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 247 | case UsingShadow: |
| 248 | return 0; // we'll actually overwrite this later |
| 249 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 250 | case UnresolvedUsingValue: |
| 251 | case UnresolvedUsingTypename: |
| 252 | return IDNS_Ordinary | IDNS_Using; |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 253 | |
| 254 | case Using: |
| 255 | return IDNS_Using; |
| 256 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 257 | case ObjCProtocol: |
Douglas Gregor | 8fc463a | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 258 | return IDNS_ObjCProtocol; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 260 | case Field: |
| 261 | case ObjCAtDefsField: |
| 262 | case ObjCIvar: |
| 263 | return IDNS_Member; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 265 | case Record: |
| 266 | case CXXRecord: |
| 267 | case Enum: |
| 268 | case TemplateTypeParm: |
| 269 | return IDNS_Tag; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 270 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 271 | case Namespace: |
| 272 | case Template: |
| 273 | case FunctionTemplate: |
| 274 | case ClassTemplate: |
| 275 | case TemplateTemplateParm: |
Anders Carlsson | faf0e87 | 2009-03-28 23:02:53 +0000 | [diff] [blame] | 276 | case NamespaceAlias: |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 277 | return IDNS_Tag | IDNS_Ordinary; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 279 | // Never have names. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 280 | case Friend: |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 281 | case FriendTemplate: |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 282 | case LinkageSpec: |
| 283 | case FileScopeAsm: |
| 284 | case StaticAssert: |
| 285 | case ObjCClass: |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 286 | case ObjCPropertyImpl: |
| 287 | case ObjCForwardProtocol: |
| 288 | case Block: |
| 289 | case TranslationUnit: |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 290 | |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 291 | case UsingDirective: |
| 292 | case ClassTemplateSpecialization: |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 293 | case ClassTemplatePartialSpecialization: |
Douglas Gregor | bd4187b | 2010-04-22 23:19:50 +0000 | [diff] [blame^] | 294 | case ObjCImplementation: |
| 295 | case ObjCCategory: |
| 296 | case ObjCCategoryImpl: |
| 297 | // Never looked up by name. |
Chris Lattner | 769dbdf | 2009-03-27 20:18:19 +0000 | [diff] [blame] | 298 | return 0; |
| 299 | } |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 300 | |
| 301 | return 0; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 304 | void Decl::addAttr(Attr *NewAttr) { |
| 305 | Attr *&ExistingAttr = getASTContext().getDeclAttrs(this); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 306 | |
| 307 | NewAttr->setNext(ExistingAttr); |
| 308 | ExistingAttr = NewAttr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 309 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 310 | HasAttrs = true; |
| 311 | } |
| 312 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 313 | void Decl::invalidateAttrs() { |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 314 | if (!HasAttrs) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 316 | HasAttrs = false; |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 317 | getASTContext().eraseDeclAttrs(this); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 320 | const Attr *Decl::getAttrsImpl() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | assert(HasAttrs && "getAttrs() should verify this!"); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 322 | return getASTContext().getDeclAttrs(this); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 325 | void Decl::swapAttrs(Decl *RHS) { |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 326 | bool HasLHSAttr = this->HasAttrs; |
| 327 | bool HasRHSAttr = RHS->HasAttrs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 329 | // Usually, neither decl has attrs, nothing to do. |
| 330 | if (!HasLHSAttr && !HasRHSAttr) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 331 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 332 | // If 'this' has no attrs, swap the other way. |
| 333 | if (!HasLHSAttr) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 334 | return RHS->swapAttrs(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 335 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 336 | ASTContext &Context = getASTContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 338 | // Handle the case when both decls have attrs. |
| 339 | if (HasRHSAttr) { |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 340 | std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS)); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 341 | return; |
| 342 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 344 | // Otherwise, LHS has an attr and RHS doesn't. |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 345 | Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this); |
| 346 | Context.eraseDeclAttrs(this); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 347 | this->HasAttrs = false; |
| 348 | RHS->HasAttrs = true; |
| 349 | } |
| 350 | |
| 351 | |
Chris Lattner | cc58147 | 2009-03-04 06:05:19 +0000 | [diff] [blame] | 352 | void Decl::Destroy(ASTContext &C) { |
| 353 | // Free attributes for this decl. |
| 354 | if (HasAttrs) { |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 355 | C.getDeclAttrs(this)->Destroy(C); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 356 | invalidateAttrs(); |
Chris Lattner | cc58147 | 2009-03-04 06:05:19 +0000 | [diff] [blame] | 357 | HasAttrs = false; |
| 358 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
Douglas Gregor | a0fc55f | 2009-01-13 19:47:12 +0000 | [diff] [blame] | 360 | #if 0 |
Douglas Gregor | 00ad0ef | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 361 | // FIXME: Once ownership is fully understood, we can enable this code |
| 362 | if (DeclContext *DC = dyn_cast<DeclContext>(this)) |
| 363 | DC->decls_begin()->Destroy(C); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 364 | |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 365 | // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0 |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 366 | // within the loop, only the Destroy method for the first Decl |
| 367 | // will deallocate all of the Decls in a chain. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 368 | |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 369 | Decl* N = getNextDeclInContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 371 | while (N) { |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 372 | Decl* Tmp = N->getNextDeclInContext(); |
| 373 | N->NextDeclInContext = 0; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 374 | N->Destroy(C); |
| 375 | N = Tmp; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | } |
Douglas Gregor | a0fc55f | 2009-01-13 19:47:12 +0000 | [diff] [blame] | 377 | |
Ted Kremenek | 94a3900 | 2009-12-01 00:07:10 +0000 | [diff] [blame] | 378 | if (isOutOfSemaDC()) |
| 379 | delete (C) getMultipleDC(); |
| 380 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 381 | this->~Decl(); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 382 | C.Deallocate((void *)this); |
Ted Kremenek | 94a3900 | 2009-12-01 00:07:10 +0000 | [diff] [blame] | 383 | #endif |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 386 | Decl *Decl::castFromDeclContext (const DeclContext *D) { |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 387 | Decl::Kind DK = D->getDeclKind(); |
| 388 | switch(DK) { |
| 389 | #define DECL_CONTEXT(Name) \ |
| 390 | case Decl::Name: \ |
| 391 | return static_cast<Name##Decl*>(const_cast<DeclContext*>(D)); |
| 392 | #define DECL_CONTEXT_BASE(Name) |
| 393 | #include "clang/AST/DeclNodes.def" |
| 394 | default: |
| 395 | #define DECL_CONTEXT_BASE(Name) \ |
| 396 | if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \ |
| 397 | return static_cast<Name##Decl*>(const_cast<DeclContext*>(D)); |
| 398 | #include "clang/AST/DeclNodes.def" |
| 399 | assert(false && "a decl that inherits DeclContext isn't handled"); |
| 400 | return 0; |
| 401 | } |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | DeclContext *Decl::castToDeclContext(const Decl *D) { |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 405 | Decl::Kind DK = D->getKind(); |
| 406 | switch(DK) { |
| 407 | #define DECL_CONTEXT(Name) \ |
| 408 | case Decl::Name: \ |
| 409 | return static_cast<Name##Decl*>(const_cast<Decl*>(D)); |
| 410 | #define DECL_CONTEXT_BASE(Name) |
| 411 | #include "clang/AST/DeclNodes.def" |
| 412 | default: |
| 413 | #define DECL_CONTEXT_BASE(Name) \ |
| 414 | if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \ |
| 415 | return static_cast<Name##Decl*>(const_cast<Decl*>(D)); |
| 416 | #include "clang/AST/DeclNodes.def" |
| 417 | assert(false && "a decl that inherits DeclContext isn't handled"); |
| 418 | return 0; |
| 419 | } |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 422 | CompoundStmt* Decl::getCompoundBody() const { |
| 423 | return dyn_cast_or_null<CompoundStmt>(getBody()); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 426 | SourceLocation Decl::getBodyRBrace() const { |
| 427 | Stmt *Body = getBody(); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 428 | if (!Body) |
| 429 | return SourceLocation(); |
| 430 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body)) |
| 431 | return CS->getRBracLoc(); |
| 432 | assert(isa<CXXTryStmt>(Body) && |
| 433 | "Body can only be CompoundStmt or CXXTryStmt"); |
| 434 | return cast<CXXTryStmt>(Body)->getSourceRange().getEnd(); |
| 435 | } |
| 436 | |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 437 | #ifndef NDEBUG |
| 438 | void Decl::CheckAccessDeclContext() const { |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 439 | // Suppress this check if any of the following hold: |
| 440 | // 1. this is the translation unit (and thus has no parent) |
| 441 | // 2. this is a template parameter (and thus doesn't belong to its context) |
| 442 | // 3. this is a ParmVarDecl (which can be in a record context during |
| 443 | // the brief period between its creation and the creation of the |
| 444 | // FunctionDecl) |
| 445 | // 4. the context is not a record |
Anders Carlsson | 35eda44 | 2009-08-29 20:47:47 +0000 | [diff] [blame] | 446 | if (isa<TranslationUnitDecl>(this) || |
Douglas Gregor | fdd8ab1 | 2010-02-22 17:53:38 +0000 | [diff] [blame] | 447 | !isa<CXXRecordDecl>(getDeclContext()) || |
| 448 | isInvalidDecl()) |
Anders Carlsson | 35eda44 | 2009-08-29 20:47:47 +0000 | [diff] [blame] | 449 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | |
| 451 | assert(Access != AS_none && |
Anders Carlsson | 1329c27 | 2009-03-25 23:38:06 +0000 | [diff] [blame] | 452 | "Access specifier is AS_none inside a record decl"); |
| 453 | } |
| 454 | |
| 455 | #endif |
| 456 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 457 | //===----------------------------------------------------------------------===// |
| 458 | // DeclContext Implementation |
| 459 | //===----------------------------------------------------------------------===// |
| 460 | |
Argyrios Kyrtzidis | 3d7641e | 2009-02-16 14:29:28 +0000 | [diff] [blame] | 461 | bool DeclContext::classof(const Decl *D) { |
| 462 | switch (D->getKind()) { |
| 463 | #define DECL_CONTEXT(Name) case Decl::Name: |
| 464 | #define DECL_CONTEXT_BASE(Name) |
| 465 | #include "clang/AST/DeclNodes.def" |
| 466 | return true; |
| 467 | default: |
| 468 | #define DECL_CONTEXT_BASE(Name) \ |
| 469 | if (D->getKind() >= Decl::Name##First && \ |
| 470 | D->getKind() <= Decl::Name##Last) \ |
| 471 | return true; |
| 472 | #include "clang/AST/DeclNodes.def" |
| 473 | return false; |
| 474 | } |
| 475 | } |
| 476 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 477 | DeclContext::~DeclContext() { |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 478 | // FIXME: Currently ~ASTContext will delete the StoredDeclsMaps because |
| 479 | // ~DeclContext() is not guaranteed to be called when ASTContext uses |
| 480 | // a BumpPtrAllocator. |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 481 | // delete LookupPtr; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | void DeclContext::DestroyDecls(ASTContext &C) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 485 | for (decl_iterator D = decls_begin(); D != decls_end(); ) |
Douglas Gregor | 00ad0ef | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 486 | (*D++)->Destroy(C); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Douglas Gregor | e942bbe | 2009-09-10 16:57:35 +0000 | [diff] [blame] | 489 | /// \brief Find the parent context of this context that will be |
| 490 | /// used for unqualified name lookup. |
| 491 | /// |
| 492 | /// Generally, the parent lookup context is the semantic context. However, for |
| 493 | /// a friend function the parent lookup context is the lexical context, which |
| 494 | /// is the class in which the friend is declared. |
| 495 | DeclContext *DeclContext::getLookupParent() { |
| 496 | // FIXME: Find a better way to identify friends |
| 497 | if (isa<FunctionDecl>(this)) |
| 498 | if (getParent()->getLookupContext()->isFileContext() && |
| 499 | getLexicalParent()->getLookupContext()->isRecord()) |
| 500 | return getLexicalParent(); |
| 501 | |
| 502 | return getParent(); |
| 503 | } |
| 504 | |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 505 | bool DeclContext::isDependentContext() const { |
| 506 | if (isFileContext()) |
| 507 | return false; |
| 508 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 509 | if (isa<ClassTemplatePartialSpecializationDecl>(this)) |
| 510 | return true; |
| 511 | |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 512 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) |
| 513 | if (Record->getDescribedClassTemplate()) |
| 514 | return true; |
| 515 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 516 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) { |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 517 | if (Function->getDescribedFunctionTemplate()) |
| 518 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 519 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 520 | // Friend function declarations are dependent if their *lexical* |
| 521 | // context is dependent. |
| 522 | if (cast<Decl>(this)->getFriendObjectKind()) |
| 523 | return getLexicalParent()->isDependentContext(); |
| 524 | } |
| 525 | |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 526 | return getParent() && getParent()->isDependentContext(); |
| 527 | } |
| 528 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 529 | bool DeclContext::isTransparentContext() const { |
| 530 | if (DeclKind == Decl::Enum) |
| 531 | return true; // FIXME: Check for C++0x scoped enums |
| 532 | else if (DeclKind == Decl::LinkageSpec) |
| 533 | return true; |
Douglas Gregor | 6510079 | 2009-02-26 00:02:51 +0000 | [diff] [blame] | 534 | else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast) |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 535 | return cast<RecordDecl>(this)->isAnonymousStructOrUnion(); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 536 | else if (DeclKind == Decl::Namespace) |
| 537 | return false; // FIXME: Check for C++0x inline namespaces |
| 538 | |
| 539 | return false; |
| 540 | } |
| 541 | |
Douglas Gregor | 6dd38da | 2009-08-27 06:03:53 +0000 | [diff] [blame] | 542 | bool DeclContext::Encloses(DeclContext *DC) { |
| 543 | if (getPrimaryContext() != this) |
| 544 | return getPrimaryContext()->Encloses(DC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | |
Douglas Gregor | 6dd38da | 2009-08-27 06:03:53 +0000 | [diff] [blame] | 546 | for (; DC; DC = DC->getParent()) |
| 547 | if (DC->getPrimaryContext() == this) |
| 548 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 549 | return false; |
Douglas Gregor | 6dd38da | 2009-08-27 06:03:53 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 552 | DeclContext *DeclContext::getPrimaryContext() { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 553 | switch (DeclKind) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 554 | case Decl::TranslationUnit: |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 555 | case Decl::LinkageSpec: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 556 | case Decl::Block: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 557 | // There is only one DeclContext for these entities. |
| 558 | return this; |
| 559 | |
| 560 | case Decl::Namespace: |
| 561 | // The original namespace is our primary context. |
| 562 | return static_cast<NamespaceDecl*>(this)->getOriginalNamespace(); |
| 563 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 564 | case Decl::ObjCMethod: |
| 565 | return this; |
| 566 | |
| 567 | case Decl::ObjCInterface: |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 568 | case Decl::ObjCProtocol: |
| 569 | case Decl::ObjCCategory: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 570 | // FIXME: Can Objective-C interfaces be forward-declared? |
| 571 | return this; |
| 572 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 573 | case Decl::ObjCImplementation: |
| 574 | case Decl::ObjCCategoryImpl: |
| 575 | return this; |
| 576 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 577 | default: |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 578 | if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) { |
| 579 | // If this is a tag type that has a definition or is currently |
| 580 | // being defined, that definition is our primary context. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 581 | TagDecl *Tag = cast<TagDecl>(this); |
| 582 | assert(isa<TagType>(Tag->TypeForDecl) || |
| 583 | isa<InjectedClassNameType>(Tag->TypeForDecl)); |
| 584 | |
| 585 | if (TagDecl *Def = Tag->getDefinition()) |
| 586 | return Def; |
| 587 | |
| 588 | if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) { |
| 589 | const TagType *TagTy = cast<TagType>(Tag->TypeForDecl); |
| 590 | if (TagTy->isBeingDefined()) |
| 591 | // FIXME: is it necessarily being defined in the decl |
| 592 | // that owns the type? |
| 593 | return TagTy->getDecl(); |
| 594 | } |
| 595 | |
| 596 | return Tag; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 599 | assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast && |
| 600 | "Unknown DeclContext kind"); |
| 601 | return this; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | DeclContext *DeclContext::getNextContext() { |
| 606 | switch (DeclKind) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 607 | case Decl::Namespace: |
| 608 | // Return the next namespace |
| 609 | return static_cast<NamespaceDecl*>(this)->getNextNamespace(); |
| 610 | |
| 611 | default: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 612 | return 0; |
| 613 | } |
| 614 | } |
| 615 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 616 | /// \brief Load the declarations within this lexical storage from an |
| 617 | /// external source. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 618 | void |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 619 | DeclContext::LoadLexicalDeclsFromExternalStorage() const { |
| 620 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 621 | assert(hasExternalLexicalStorage() && Source && "No external storage?"); |
| 622 | |
Eli Friedman | b0156ea | 2009-04-27 23:43:36 +0000 | [diff] [blame] | 623 | llvm::SmallVector<uint32_t, 64> Decls; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 624 | if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this), |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 625 | Decls)) |
| 626 | return; |
| 627 | |
| 628 | // There is no longer any lexical storage in this context |
| 629 | ExternalLexicalStorage = false; |
| 630 | |
| 631 | if (Decls.empty()) |
| 632 | return; |
| 633 | |
| 634 | // Resolve all of the declaration IDs into declarations, building up |
| 635 | // a chain of declarations via the Decl::NextDeclInContext field. |
| 636 | Decl *FirstNewDecl = 0; |
| 637 | Decl *PrevDecl = 0; |
| 638 | for (unsigned I = 0, N = Decls.size(); I != N; ++I) { |
| 639 | Decl *D = Source->GetDecl(Decls[I]); |
| 640 | if (PrevDecl) |
| 641 | PrevDecl->NextDeclInContext = D; |
| 642 | else |
| 643 | FirstNewDecl = D; |
| 644 | |
| 645 | PrevDecl = D; |
| 646 | } |
| 647 | |
| 648 | // Splice the newly-read declarations into the beginning of the list |
| 649 | // of declarations. |
| 650 | PrevDecl->NextDeclInContext = FirstDecl; |
| 651 | FirstDecl = FirstNewDecl; |
| 652 | if (!LastDecl) |
| 653 | LastDecl = PrevDecl; |
| 654 | } |
| 655 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 656 | void |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 657 | DeclContext::LoadVisibleDeclsFromExternalStorage() const { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 658 | DeclContext *This = const_cast<DeclContext *>(this); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 659 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 660 | assert(hasExternalVisibleStorage() && Source && "No external storage?"); |
| 661 | |
| 662 | llvm::SmallVector<VisibleDeclaration, 64> Decls; |
| 663 | if (Source->ReadDeclsVisibleInContext(This, Decls)) |
| 664 | return; |
| 665 | |
| 666 | // There is no longer any visible storage in this context |
| 667 | ExternalVisibleStorage = false; |
| 668 | |
| 669 | // Load the declaration IDs for all of the names visible in this |
| 670 | // context. |
| 671 | assert(!LookupPtr && "Have a lookup map before de-serialization?"); |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 672 | StoredDeclsMap *Map = CreateStoredDeclsMap(getParentASTContext()); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 673 | for (unsigned I = 0, N = Decls.size(); I != N; ++I) { |
| 674 | (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations); |
| 675 | } |
| 676 | } |
| 677 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 678 | DeclContext::decl_iterator DeclContext::decls_begin() const { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 679 | if (hasExternalLexicalStorage()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 680 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 681 | |
| 682 | // FIXME: Check whether we need to load some declarations from |
| 683 | // external storage. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 684 | return decl_iterator(FirstDecl); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 687 | DeclContext::decl_iterator DeclContext::decls_end() const { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 688 | if (hasExternalLexicalStorage()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 689 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 690 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 691 | return decl_iterator(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 694 | bool DeclContext::decls_empty() const { |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 695 | if (hasExternalLexicalStorage()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 696 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 697 | |
| 698 | return !FirstDecl; |
| 699 | } |
| 700 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 701 | void DeclContext::removeDecl(Decl *D) { |
| 702 | assert(D->getLexicalDeclContext() == this && |
| 703 | "decl being removed from non-lexical context"); |
| 704 | assert((D->NextDeclInContext || D == LastDecl) && |
| 705 | "decl is not in decls list"); |
| 706 | |
| 707 | // Remove D from the decl chain. This is O(n) but hopefully rare. |
| 708 | if (D == FirstDecl) { |
| 709 | if (D == LastDecl) |
| 710 | FirstDecl = LastDecl = 0; |
| 711 | else |
| 712 | FirstDecl = D->NextDeclInContext; |
| 713 | } else { |
| 714 | for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) { |
| 715 | assert(I && "decl not found in linked list"); |
| 716 | if (I->NextDeclInContext == D) { |
| 717 | I->NextDeclInContext = D->NextDeclInContext; |
| 718 | if (D == LastDecl) LastDecl = I; |
| 719 | break; |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | // Mark that D is no longer in the decl chain. |
| 725 | D->NextDeclInContext = 0; |
| 726 | |
| 727 | // Remove D from the lookup table if necessary. |
| 728 | if (isa<NamedDecl>(D)) { |
| 729 | NamedDecl *ND = cast<NamedDecl>(D); |
| 730 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 731 | StoredDeclsMap *Map = getPrimaryContext()->LookupPtr; |
| 732 | if (!Map) return; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 733 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 734 | StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName()); |
| 735 | assert(Pos != Map->end() && "no lookup entry for decl"); |
| 736 | Pos->second.remove(ND); |
| 737 | } |
| 738 | } |
| 739 | |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 740 | void DeclContext::addHiddenDecl(Decl *D) { |
Chris Lattner | 7f0be13 | 2009-02-20 00:56:18 +0000 | [diff] [blame] | 741 | assert(D->getLexicalDeclContext() == this && |
| 742 | "Decl inserted into wrong lexical context"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 743 | assert(!D->getNextDeclInContext() && D != LastDecl && |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 744 | "Decl already inserted into a DeclContext"); |
| 745 | |
| 746 | if (FirstDecl) { |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 747 | LastDecl->NextDeclInContext = D; |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 748 | LastDecl = D; |
| 749 | } else { |
| 750 | FirstDecl = LastDecl = D; |
| 751 | } |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | void DeclContext::addDecl(Decl *D) { |
| 755 | addHiddenDecl(D); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 756 | |
| 757 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 758 | ND->getDeclContext()->makeDeclVisibleInContext(ND); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 761 | /// buildLookup - Build the lookup data structure with all of the |
| 762 | /// declarations in DCtx (and any other contexts linked to it or |
| 763 | /// transparent contexts nested within it). |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 764 | void DeclContext::buildLookup(DeclContext *DCtx) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 765 | for (; DCtx; DCtx = DCtx->getNextContext()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | for (decl_iterator D = DCtx->decls_begin(), |
| 767 | DEnd = DCtx->decls_end(); |
Douglas Gregor | 4f3b8f8 | 2009-01-06 07:17:58 +0000 | [diff] [blame] | 768 | D != DEnd; ++D) { |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 769 | // Insert this declaration into the lookup structure, but only |
| 770 | // if it's semantically in its decl context. During non-lazy |
| 771 | // lookup building, this is implicitly enforced by addDecl. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 772 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 773 | if (D->getDeclContext() == DCtx) |
| 774 | makeDeclVisibleInContextImpl(ND); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 775 | |
Ted Kremenek | c32b1d8 | 2009-11-17 22:58:30 +0000 | [diff] [blame] | 776 | // Insert any forward-declared Objective-C interfaces into the lookup |
| 777 | // data structure. |
| 778 | if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D)) |
| 779 | for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end(); |
| 780 | I != IEnd; ++I) |
Ted Kremenek | 321c22f | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 781 | makeDeclVisibleInContextImpl(I->getInterface()); |
Ted Kremenek | c32b1d8 | 2009-11-17 22:58:30 +0000 | [diff] [blame] | 782 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 783 | // If this declaration is itself a transparent declaration context, |
| 784 | // add its members (recursively). |
| 785 | if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D)) |
| 786 | if (InnerCtx->isTransparentContext()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 787 | buildLookup(InnerCtx->getPrimaryContext()); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | } |
| 791 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 792 | DeclContext::lookup_result |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 793 | DeclContext::lookup(DeclarationName Name) { |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 794 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 795 | if (PrimaryContext != this) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 796 | return PrimaryContext->lookup(Name); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 797 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 798 | if (hasExternalVisibleStorage()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 799 | LoadVisibleDeclsFromExternalStorage(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 800 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 801 | /// If there is no lookup data structure, build one now by walking |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 802 | /// all of the linked DeclContexts (in declaration order!) and |
| 803 | /// inserting their values. |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 804 | if (!LookupPtr) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 805 | buildLookup(this); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 806 | |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 807 | if (!LookupPtr) |
Chris Lattner | 9194250 | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 808 | return lookup_result(0, 0); |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 809 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 810 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 811 | StoredDeclsMap::iterator Pos = LookupPtr->find(Name); |
| 812 | if (Pos == LookupPtr->end()) |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 813 | return lookup_result(0, 0); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 814 | return Pos->second.getLookupResult(getParentASTContext()); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 817 | DeclContext::lookup_const_result |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 818 | DeclContext::lookup(DeclarationName Name) const { |
| 819 | return const_cast<DeclContext*>(this)->lookup(Name); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 822 | DeclContext *DeclContext::getLookupContext() { |
| 823 | DeclContext *Ctx = this; |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 824 | // Skip through transparent contexts. |
Douglas Gregor | ce35607 | 2009-01-06 23:51:29 +0000 | [diff] [blame] | 825 | while (Ctx->isTransparentContext()) |
| 826 | Ctx = Ctx->getParent(); |
| 827 | return Ctx; |
| 828 | } |
| 829 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 830 | DeclContext *DeclContext::getEnclosingNamespaceContext() { |
| 831 | DeclContext *Ctx = this; |
| 832 | // Skip through non-namespace, non-translation-unit contexts. |
| 833 | while (!Ctx->isFileContext() || Ctx->isTransparentContext()) |
| 834 | Ctx = Ctx->getParent(); |
| 835 | return Ctx->getPrimaryContext(); |
| 836 | } |
| 837 | |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 838 | void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 839 | // FIXME: This feels like a hack. Should DeclarationName support |
| 840 | // template-ids, or is there a better way to keep specializations |
| 841 | // from being visible? |
| 842 | if (isa<ClassTemplateSpecializationDecl>(D)) |
| 843 | return; |
Eli Friedman | 6bc2013 | 2009-12-08 05:40:03 +0000 | [diff] [blame] | 844 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 845 | if (FD->isFunctionTemplateSpecialization()) |
| 846 | return; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 847 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 848 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 849 | if (PrimaryContext != this) { |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 850 | PrimaryContext->makeDeclVisibleInContext(D, Recoverable); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 851 | return; |
| 852 | } |
| 853 | |
| 854 | // If we already have a lookup data structure, perform the insertion |
| 855 | // into it. Otherwise, be lazy and don't build that structure until |
| 856 | // someone asks for it. |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 857 | if (LookupPtr || !Recoverable) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 858 | makeDeclVisibleInContextImpl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 859 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 860 | // If we are a transparent context, insert into our parent context, |
| 861 | // too. This operation is recursive. |
| 862 | if (isTransparentContext()) |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 863 | getParent()->makeDeclVisibleInContext(D, Recoverable); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 866 | void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 867 | // Skip unnamed declarations. |
| 868 | if (!D->getDeclName()) |
| 869 | return; |
| 870 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 871 | // FIXME: This feels like a hack. Should DeclarationName support |
| 872 | // template-ids, or is there a better way to keep specializations |
| 873 | // from being visible? |
| 874 | if (isa<ClassTemplateSpecializationDecl>(D)) |
| 875 | return; |
| 876 | |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 877 | ASTContext *C = 0; |
| 878 | if (!LookupPtr) { |
| 879 | C = &getParentASTContext(); |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 880 | CreateStoredDeclsMap(*C); |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 881 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 882 | |
| 883 | // Insert this declaration into the map. |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 884 | StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()]; |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 885 | if (DeclNameEntries.isNull()) { |
| 886 | DeclNameEntries.setOnlyValue(D); |
Chris Lattner | bd6c800 | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 887 | return; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 888 | } |
Chris Lattner | 9194250 | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 889 | |
Chris Lattner | bdc3d00 | 2009-02-20 01:10:07 +0000 | [diff] [blame] | 890 | // If it is possible that this is a redeclaration, check to see if there is |
| 891 | // already a decl for which declarationReplaces returns true. If there is |
| 892 | // one, just replace it and return. |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 893 | if (!C) |
| 894 | C = &getParentASTContext(); |
| 895 | |
| 896 | if (DeclNameEntries.HandleRedeclaration(*C, D)) |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 897 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 898 | |
Chris Lattner | bd6c800 | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 899 | // Put this declaration into the appropriate slot. |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 900 | DeclNameEntries.AddSubsequentDecl(D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 901 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 902 | |
| 903 | /// Returns iterator range [First, Last) of UsingDirectiveDecls stored within |
| 904 | /// this context. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 905 | DeclContext::udir_iterator_range |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 906 | DeclContext::getUsingDirectives() const { |
| 907 | lookup_const_result Result = lookup(UsingDirectiveDecl::getName()); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 908 | return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first), |
| 909 | reinterpret_cast<udir_iterator>(Result.second)); |
| 910 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 911 | |
| 912 | void StoredDeclsList::materializeDecls(ASTContext &Context) { |
| 913 | if (isNull()) |
| 914 | return; |
| 915 | |
| 916 | switch ((DataKind)(Data & 0x03)) { |
| 917 | case DK_Decl: |
| 918 | case DK_Decl_Vector: |
| 919 | break; |
| 920 | |
| 921 | case DK_DeclID: { |
| 922 | // Resolve this declaration ID to an actual declaration by |
| 923 | // querying the external AST source. |
| 924 | unsigned DeclID = Data >> 2; |
| 925 | |
| 926 | ExternalASTSource *Source = Context.getExternalSource(); |
| 927 | assert(Source && "No external AST source available!"); |
| 928 | |
| 929 | Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID)); |
| 930 | break; |
| 931 | } |
| 932 | |
| 933 | case DK_ID_Vector: { |
| 934 | // We have a vector of declaration IDs. Resolve all of them to |
| 935 | // actual declarations. |
| 936 | VectorTy &Vector = *getAsVector(); |
| 937 | ExternalASTSource *Source = Context.getExternalSource(); |
| 938 | assert(Source && "No external AST source available!"); |
| 939 | |
| 940 | for (unsigned I = 0, N = Vector.size(); I != N; ++I) |
| 941 | Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I])); |
| 942 | |
| 943 | Data = (Data & ~0x03) | DK_Decl_Vector; |
| 944 | break; |
| 945 | } |
| 946 | } |
| 947 | } |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 948 | |
| 949 | //===----------------------------------------------------------------------===// |
| 950 | // Creation and Destruction of StoredDeclsMaps. // |
| 951 | //===----------------------------------------------------------------------===// |
| 952 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 953 | StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const { |
| 954 | assert(!LookupPtr && "context already has a decls map"); |
| 955 | assert(getPrimaryContext() == this && |
| 956 | "creating decls map on non-primary context"); |
| 957 | |
| 958 | StoredDeclsMap *M; |
| 959 | bool Dependent = isDependentContext(); |
| 960 | if (Dependent) |
| 961 | M = new DependentStoredDeclsMap(); |
| 962 | else |
| 963 | M = new StoredDeclsMap(); |
| 964 | M->Previous = C.LastSDM; |
| 965 | C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent); |
| 966 | LookupPtr = M; |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 967 | return M; |
| 968 | } |
| 969 | |
| 970 | void ASTContext::ReleaseDeclContextMaps() { |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 971 | // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap |
| 972 | // pointer because the subclass doesn't add anything that needs to |
| 973 | // be deleted. |
| 974 | |
| 975 | StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt()); |
| 976 | } |
| 977 | |
| 978 | void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) { |
| 979 | while (Map) { |
| 980 | // Advance the iteration before we invalidate memory. |
| 981 | llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous; |
| 982 | |
| 983 | if (Dependent) |
| 984 | delete static_cast<DependentStoredDeclsMap*>(Map); |
| 985 | else |
| 986 | delete Map; |
| 987 | |
| 988 | Map = Next.getPointer(); |
| 989 | Dependent = Next.getInt(); |
| 990 | } |
| 991 | } |
| 992 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 993 | DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C, |
| 994 | DeclContext *Parent, |
| 995 | const PartialDiagnostic &PDiag) { |
| 996 | assert(Parent->isDependentContext() |
| 997 | && "cannot iterate dependent diagnostics of non-dependent context"); |
| 998 | Parent = Parent->getPrimaryContext(); |
| 999 | if (!Parent->LookupPtr) |
| 1000 | Parent->CreateStoredDeclsMap(C); |
| 1001 | |
| 1002 | DependentStoredDeclsMap *Map |
| 1003 | = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr); |
| 1004 | |
Douglas Gregor | b836518 | 2010-03-29 23:56:53 +0000 | [diff] [blame] | 1005 | // Allocate the copy of the PartialDiagnostic via the ASTContext's |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 1006 | // BumpPtrAllocator, rather than the ASTContext itself. |
Douglas Gregor | b836518 | 2010-03-29 23:56:53 +0000 | [diff] [blame] | 1007 | PartialDiagnostic::Storage *DiagStorage = 0; |
| 1008 | if (PDiag.hasStorage()) |
| 1009 | DiagStorage = new (C) PartialDiagnostic::Storage; |
| 1010 | |
| 1011 | DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage); |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1012 | |
| 1013 | // TODO: Maybe we shouldn't reverse the order during insertion. |
| 1014 | DD->NextDiagnostic = Map->FirstDiagnostic; |
| 1015 | Map->FirstDiagnostic = DD; |
| 1016 | |
| 1017 | return DD; |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 1018 | } |