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