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, |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 651 | DeclarationName Name, |
| 652 | llvm::SmallVectorImpl<NamedDecl*> &Decls) { |
| 653 | ASTContext &Context = DC->getParentASTContext();; |
| 654 | |
| 655 | StoredDeclsMap *Map; |
| 656 | if (!(Map = DC->LookupPtr)) |
| 657 | Map = DC->CreateStoredDeclsMap(Context); |
| 658 | |
| 659 | StoredDeclsList &List = (*Map)[Name]; |
| 660 | for (unsigned I = 0, N = Decls.size(); I != N; ++I) { |
| 661 | if (List.isNull()) |
| 662 | List.setOnlyValue(Decls[I]); |
| 663 | else |
| 664 | List.AddSubsequentDecl(Decls[I]); |
| 665 | } |
| 666 | |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 667 | return List.getLookupResult(); |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Argyrios Kyrtzidis | a60786b | 2010-08-20 23:35:55 +0000 | [diff] [blame] | 670 | void ExternalASTSource::MaterializeVisibleDeclsForName(const DeclContext *DC, |
| 671 | DeclarationName Name, |
| 672 | llvm::SmallVectorImpl<NamedDecl*> &Decls) { |
| 673 | assert(DC->LookupPtr); |
| 674 | StoredDeclsMap &Map = *DC->LookupPtr; |
| 675 | |
| 676 | // If there's an entry in the table the visible decls for this name have |
| 677 | // already been deserialized. |
| 678 | if (Map.find(Name) == Map.end()) { |
| 679 | StoredDeclsList &List = Map[Name]; |
| 680 | for (unsigned I = 0, N = Decls.size(); I != N; ++I) { |
| 681 | if (List.isNull()) |
| 682 | List.setOnlyValue(Decls[I]); |
| 683 | else |
| 684 | List.AddSubsequentDecl(Decls[I]); |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | |
Sebastian Redl | 681d723 | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 689 | DeclContext::decl_iterator DeclContext::noload_decls_begin() const { |
| 690 | return decl_iterator(FirstDecl); |
| 691 | } |
| 692 | |
| 693 | DeclContext::decl_iterator DeclContext::noload_decls_end() const { |
| 694 | return decl_iterator(); |
| 695 | } |
| 696 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 697 | DeclContext::decl_iterator DeclContext::decls_begin() const { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 698 | if (hasExternalLexicalStorage()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 699 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 700 | |
| 701 | // FIXME: Check whether we need to load some declarations from |
| 702 | // external storage. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 703 | return decl_iterator(FirstDecl); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 706 | DeclContext::decl_iterator DeclContext::decls_end() const { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 707 | if (hasExternalLexicalStorage()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 708 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 709 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 710 | return decl_iterator(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 713 | bool DeclContext::decls_empty() const { |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 714 | if (hasExternalLexicalStorage()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 715 | LoadLexicalDeclsFromExternalStorage(); |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 716 | |
| 717 | return !FirstDecl; |
| 718 | } |
| 719 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 720 | void DeclContext::removeDecl(Decl *D) { |
| 721 | assert(D->getLexicalDeclContext() == this && |
| 722 | "decl being removed from non-lexical context"); |
| 723 | assert((D->NextDeclInContext || D == LastDecl) && |
| 724 | "decl is not in decls list"); |
| 725 | |
| 726 | // Remove D from the decl chain. This is O(n) but hopefully rare. |
| 727 | if (D == FirstDecl) { |
| 728 | if (D == LastDecl) |
| 729 | FirstDecl = LastDecl = 0; |
| 730 | else |
| 731 | FirstDecl = D->NextDeclInContext; |
| 732 | } else { |
| 733 | for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) { |
| 734 | assert(I && "decl not found in linked list"); |
| 735 | if (I->NextDeclInContext == D) { |
| 736 | I->NextDeclInContext = D->NextDeclInContext; |
| 737 | if (D == LastDecl) LastDecl = I; |
| 738 | break; |
| 739 | } |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | // Mark that D is no longer in the decl chain. |
| 744 | D->NextDeclInContext = 0; |
| 745 | |
| 746 | // Remove D from the lookup table if necessary. |
| 747 | if (isa<NamedDecl>(D)) { |
| 748 | NamedDecl *ND = cast<NamedDecl>(D); |
| 749 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 750 | StoredDeclsMap *Map = getPrimaryContext()->LookupPtr; |
| 751 | if (!Map) return; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 752 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 753 | StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName()); |
| 754 | assert(Pos != Map->end() && "no lookup entry for decl"); |
| 755 | Pos->second.remove(ND); |
| 756 | } |
| 757 | } |
| 758 | |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 759 | void DeclContext::addHiddenDecl(Decl *D) { |
Chris Lattner | 7f0be13 | 2009-02-20 00:56:18 +0000 | [diff] [blame] | 760 | assert(D->getLexicalDeclContext() == this && |
| 761 | "Decl inserted into wrong lexical context"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 762 | assert(!D->getNextDeclInContext() && D != LastDecl && |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 763 | "Decl already inserted into a DeclContext"); |
| 764 | |
| 765 | if (FirstDecl) { |
Chris Lattner | 244a67d | 2009-03-28 06:04:26 +0000 | [diff] [blame] | 766 | LastDecl->NextDeclInContext = D; |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 767 | LastDecl = D; |
| 768 | } else { |
| 769 | FirstDecl = LastDecl = D; |
| 770 | } |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | void DeclContext::addDecl(Decl *D) { |
| 774 | addHiddenDecl(D); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 775 | |
| 776 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 777 | ND->getDeclContext()->makeDeclVisibleInContext(ND); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 780 | /// buildLookup - Build the lookup data structure with all of the |
| 781 | /// declarations in DCtx (and any other contexts linked to it or |
| 782 | /// transparent contexts nested within it). |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 783 | void DeclContext::buildLookup(DeclContext *DCtx) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 784 | for (; DCtx; DCtx = DCtx->getNextContext()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 785 | for (decl_iterator D = DCtx->decls_begin(), |
| 786 | DEnd = DCtx->decls_end(); |
Douglas Gregor | 4f3b8f8 | 2009-01-06 07:17:58 +0000 | [diff] [blame] | 787 | D != DEnd; ++D) { |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 788 | // Insert this declaration into the lookup structure, but only |
| 789 | // if it's semantically in its decl context. During non-lazy |
| 790 | // lookup building, this is implicitly enforced by addDecl. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 791 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 792 | if (D->getDeclContext() == DCtx) |
| 793 | makeDeclVisibleInContextImpl(ND); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 794 | |
Ted Kremenek | c32b1d8 | 2009-11-17 22:58:30 +0000 | [diff] [blame] | 795 | // Insert any forward-declared Objective-C interfaces into the lookup |
| 796 | // data structure. |
| 797 | if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D)) |
| 798 | for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end(); |
| 799 | I != IEnd; ++I) |
Ted Kremenek | 321c22f | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 800 | makeDeclVisibleInContextImpl(I->getInterface()); |
Ted Kremenek | c32b1d8 | 2009-11-17 22:58:30 +0000 | [diff] [blame] | 801 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 802 | // If this declaration is itself a transparent declaration context, |
| 803 | // add its members (recursively). |
| 804 | if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D)) |
| 805 | if (InnerCtx->isTransparentContext()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 806 | buildLookup(InnerCtx->getPrimaryContext()); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | } |
| 810 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 811 | DeclContext::lookup_result |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 812 | DeclContext::lookup(DeclarationName Name) { |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 813 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 814 | if (PrimaryContext != this) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 815 | return PrimaryContext->lookup(Name); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 816 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 817 | if (hasExternalVisibleStorage()) { |
| 818 | // Check to see if we've already cached the lookup results. |
| 819 | if (LookupPtr) { |
| 820 | StoredDeclsMap::iterator I = LookupPtr->find(Name); |
| 821 | if (I != LookupPtr->end()) |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 822 | return I->second.getLookupResult(); |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
| 826 | return Source->FindExternalVisibleDeclsByName(this, Name); |
| 827 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 828 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 829 | /// If there is no lookup data structure, build one now by walking |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 830 | /// all of the linked DeclContexts (in declaration order!) and |
| 831 | /// inserting their values. |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 832 | if (!LookupPtr) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 833 | buildLookup(this); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 834 | |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 835 | if (!LookupPtr) |
Douglas Gregor | a5fdd9c | 2010-05-11 06:18:17 +0000 | [diff] [blame] | 836 | return lookup_result(lookup_iterator(0), lookup_iterator(0)); |
Douglas Gregor | c36c540 | 2009-04-09 17:29:08 +0000 | [diff] [blame] | 837 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 838 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 839 | StoredDeclsMap::iterator Pos = LookupPtr->find(Name); |
| 840 | if (Pos == LookupPtr->end()) |
Douglas Gregor | a5fdd9c | 2010-05-11 06:18:17 +0000 | [diff] [blame] | 841 | return lookup_result(lookup_iterator(0), lookup_iterator(0)); |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 842 | return Pos->second.getLookupResult(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 845 | DeclContext::lookup_const_result |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 846 | DeclContext::lookup(DeclarationName Name) const { |
| 847 | return const_cast<DeclContext*>(this)->lookup(Name); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Chris Lattner | 0cf2b19 | 2009-03-27 19:19:59 +0000 | [diff] [blame] | 850 | DeclContext *DeclContext::getLookupContext() { |
| 851 | DeclContext *Ctx = this; |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 852 | // Skip through transparent contexts. |
Douglas Gregor | ce35607 | 2009-01-06 23:51:29 +0000 | [diff] [blame] | 853 | while (Ctx->isTransparentContext()) |
| 854 | Ctx = Ctx->getParent(); |
| 855 | return Ctx; |
| 856 | } |
| 857 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 858 | DeclContext *DeclContext::getEnclosingNamespaceContext() { |
| 859 | DeclContext *Ctx = this; |
| 860 | // Skip through non-namespace, non-translation-unit contexts. |
Sebastian Redl | 51a8a37 | 2010-08-31 00:36:23 +0000 | [diff] [blame^] | 861 | while (!Ctx->isFileContext()) |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 862 | Ctx = Ctx->getParent(); |
| 863 | return Ctx->getPrimaryContext(); |
| 864 | } |
| 865 | |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 866 | void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 867 | // FIXME: This feels like a hack. Should DeclarationName support |
| 868 | // template-ids, or is there a better way to keep specializations |
| 869 | // from being visible? |
| 870 | if (isa<ClassTemplateSpecializationDecl>(D)) |
| 871 | return; |
Eli Friedman | 6bc2013 | 2009-12-08 05:40:03 +0000 | [diff] [blame] | 872 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 873 | if (FD->isFunctionTemplateSpecialization()) |
| 874 | return; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 875 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 876 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 877 | if (PrimaryContext != this) { |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 878 | PrimaryContext->makeDeclVisibleInContext(D, Recoverable); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 879 | return; |
| 880 | } |
| 881 | |
| 882 | // If we already have a lookup data structure, perform the insertion |
Argyrios Kyrtzidis | 5586b01 | 2010-07-04 21:44:25 +0000 | [diff] [blame] | 883 | // into it. If we haven't deserialized externally stored decls, deserialize |
| 884 | // them so we can add the decl. Otherwise, be lazy and don't build that |
| 885 | // structure until someone asks for it. |
| 886 | if (LookupPtr || !Recoverable || hasExternalVisibleStorage()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 887 | makeDeclVisibleInContextImpl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 888 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 889 | // If we are a transparent context, insert into our parent context, |
| 890 | // too. This operation is recursive. |
| 891 | if (isTransparentContext()) |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 892 | getParent()->makeDeclVisibleInContext(D, Recoverable); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 895 | void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 896 | // Skip unnamed declarations. |
| 897 | if (!D->getDeclName()) |
| 898 | return; |
| 899 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 900 | // FIXME: This feels like a hack. Should DeclarationName support |
| 901 | // template-ids, or is there a better way to keep specializations |
| 902 | // from being visible? |
| 903 | if (isa<ClassTemplateSpecializationDecl>(D)) |
| 904 | return; |
| 905 | |
Argyrios Kyrtzidis | 5586b01 | 2010-07-04 21:44:25 +0000 | [diff] [blame] | 906 | ASTContext *C = 0; |
| 907 | if (!LookupPtr) { |
| 908 | C = &getParentASTContext(); |
| 909 | CreateStoredDeclsMap(*C); |
| 910 | } |
| 911 | |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 912 | // If there is an external AST source, load any declarations it knows about |
| 913 | // with this declaration's name. |
| 914 | // If the lookup table contains an entry about this name it means that we |
| 915 | // have already checked the external source. |
| 916 | if (ExternalASTSource *Source = getParentASTContext().getExternalSource()) |
| 917 | if (hasExternalVisibleStorage() && |
| 918 | LookupPtr->find(D->getDeclName()) == LookupPtr->end()) |
| 919 | Source->FindExternalVisibleDeclsByName(this, D->getDeclName()); |
| 920 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 921 | // Insert this declaration into the map. |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 922 | StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()]; |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 923 | if (DeclNameEntries.isNull()) { |
| 924 | DeclNameEntries.setOnlyValue(D); |
Chris Lattner | bd6c800 | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 925 | return; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 926 | } |
Chris Lattner | 9194250 | 2009-02-20 00:55:03 +0000 | [diff] [blame] | 927 | |
Chris Lattner | bdc3d00 | 2009-02-20 01:10:07 +0000 | [diff] [blame] | 928 | // If it is possible that this is a redeclaration, check to see if there is |
| 929 | // already a decl for which declarationReplaces returns true. If there is |
| 930 | // one, just replace it and return. |
Argyrios Kyrtzidis | 074dcc8 | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 931 | if (DeclNameEntries.HandleRedeclaration(D)) |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 932 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 933 | |
Chris Lattner | bd6c800 | 2009-02-19 07:00:44 +0000 | [diff] [blame] | 934 | // Put this declaration into the appropriate slot. |
Chris Lattner | 67762a3 | 2009-02-20 01:44:05 +0000 | [diff] [blame] | 935 | DeclNameEntries.AddSubsequentDecl(D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 936 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 937 | |
Argyrios Kyrtzidis | a60786b | 2010-08-20 23:35:55 +0000 | [diff] [blame] | 938 | void DeclContext::MaterializeVisibleDeclsFromExternalStorage() { |
| 939 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
| 940 | assert(hasExternalVisibleStorage() && Source && "No external storage?"); |
| 941 | |
| 942 | if (!LookupPtr) |
| 943 | CreateStoredDeclsMap(getParentASTContext()); |
| 944 | Source->MaterializeVisibleDecls(this); |
| 945 | } |
| 946 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 947 | /// Returns iterator range [First, Last) of UsingDirectiveDecls stored within |
| 948 | /// this context. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 949 | DeclContext::udir_iterator_range |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 950 | DeclContext::getUsingDirectives() const { |
| 951 | lookup_const_result Result = lookup(UsingDirectiveDecl::getName()); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 952 | return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first), |
| 953 | reinterpret_cast<udir_iterator>(Result.second)); |
| 954 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 955 | |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 956 | //===----------------------------------------------------------------------===// |
| 957 | // Creation and Destruction of StoredDeclsMaps. // |
| 958 | //===----------------------------------------------------------------------===// |
| 959 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 960 | StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const { |
| 961 | assert(!LookupPtr && "context already has a decls map"); |
| 962 | assert(getPrimaryContext() == this && |
| 963 | "creating decls map on non-primary context"); |
| 964 | |
| 965 | StoredDeclsMap *M; |
| 966 | bool Dependent = isDependentContext(); |
| 967 | if (Dependent) |
| 968 | M = new DependentStoredDeclsMap(); |
| 969 | else |
| 970 | M = new StoredDeclsMap(); |
| 971 | M->Previous = C.LastSDM; |
| 972 | C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent); |
| 973 | LookupPtr = M; |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 974 | return M; |
| 975 | } |
| 976 | |
| 977 | void ASTContext::ReleaseDeclContextMaps() { |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 978 | // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap |
| 979 | // pointer because the subclass doesn't add anything that needs to |
| 980 | // be deleted. |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 981 | StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt()); |
| 982 | } |
| 983 | |
| 984 | void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) { |
| 985 | while (Map) { |
| 986 | // Advance the iteration before we invalidate memory. |
| 987 | llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous; |
| 988 | |
| 989 | if (Dependent) |
| 990 | delete static_cast<DependentStoredDeclsMap*>(Map); |
| 991 | else |
| 992 | delete Map; |
| 993 | |
| 994 | Map = Next.getPointer(); |
| 995 | Dependent = Next.getInt(); |
| 996 | } |
| 997 | } |
| 998 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 999 | DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C, |
| 1000 | DeclContext *Parent, |
| 1001 | const PartialDiagnostic &PDiag) { |
| 1002 | assert(Parent->isDependentContext() |
| 1003 | && "cannot iterate dependent diagnostics of non-dependent context"); |
| 1004 | Parent = Parent->getPrimaryContext(); |
| 1005 | if (!Parent->LookupPtr) |
| 1006 | Parent->CreateStoredDeclsMap(C); |
| 1007 | |
| 1008 | DependentStoredDeclsMap *Map |
| 1009 | = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr); |
| 1010 | |
Douglas Gregor | b836518 | 2010-03-29 23:56:53 +0000 | [diff] [blame] | 1011 | // Allocate the copy of the PartialDiagnostic via the ASTContext's |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 1012 | // BumpPtrAllocator, rather than the ASTContext itself. |
Douglas Gregor | b836518 | 2010-03-29 23:56:53 +0000 | [diff] [blame] | 1013 | PartialDiagnostic::Storage *DiagStorage = 0; |
| 1014 | if (PDiag.hasStorage()) |
| 1015 | DiagStorage = new (C) PartialDiagnostic::Storage; |
| 1016 | |
| 1017 | DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage); |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 1018 | |
| 1019 | // TODO: Maybe we shouldn't reverse the order during insertion. |
| 1020 | DD->NextDiagnostic = Map->FirstDiagnostic; |
| 1021 | Map->FirstDiagnostic = DD; |
| 1022 | |
| 1023 | return DD; |
Ted Kremenek | 3478eb6 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 1024 | } |