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