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