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