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