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" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclObjC.h" |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 18 | #include "clang/AST/Type.h" |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseMap.h" |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 20 | #include <algorithm> |
| 21 | #include <functional> |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 22 | #include <vector> |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | // Statistics |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | // temporary statistics gathering |
| 30 | static unsigned nFuncs = 0; |
| 31 | static unsigned nVars = 0; |
| 32 | static unsigned nParmVars = 0; |
Fariborz Jahanian | 4f5420d | 2008-12-20 21:06:28 +0000 | [diff] [blame] | 33 | static unsigned nOriginalParmVars = 0; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 34 | static unsigned nSUC = 0; |
Argyrios Kyrtzidis | 55d71f9 | 2008-08-10 01:47:31 +0000 | [diff] [blame] | 35 | static unsigned nCXXSUC = 0; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 36 | static unsigned nEnumConst = 0; |
| 37 | static unsigned nEnumDecls = 0; |
| 38 | static unsigned nNamespaces = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 39 | static unsigned nOverFuncs = 0; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 40 | static unsigned nTypedef = 0; |
| 41 | static unsigned nFieldDecls = 0; |
| 42 | static unsigned nInterfaceDecls = 0; |
| 43 | static unsigned nClassDecls = 0; |
| 44 | static unsigned nMethodDecls = 0; |
| 45 | static unsigned nProtocolDecls = 0; |
| 46 | static unsigned nForwardProtocolDecls = 0; |
| 47 | static unsigned nCategoryDecls = 0; |
| 48 | static unsigned nIvarDecls = 0; |
Ted Kremenek | 01e6779 | 2008-08-20 03:26:33 +0000 | [diff] [blame] | 49 | static unsigned nAtDefsFieldDecls = 0; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 50 | static unsigned nObjCImplementationDecls = 0; |
| 51 | static unsigned nObjCCategoryImpl = 0; |
| 52 | static unsigned nObjCCompatibleAlias = 0; |
| 53 | static unsigned nObjCPropertyDecl = 0; |
| 54 | static unsigned nObjCPropertyImplDecl = 0; |
| 55 | static unsigned nLinkageSpecDecl = 0; |
| 56 | static unsigned nFileScopeAsmDecl = 0; |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 57 | static unsigned nBlockDecls = 0; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 58 | |
| 59 | static bool StatSwitch = false; |
| 60 | |
| 61 | // This keeps track of all decl attributes. Since so few decls have attrs, we |
| 62 | // keep them in a hash map instead of wasting space in the Decl class. |
| 63 | typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy; |
| 64 | |
| 65 | static DeclAttrMapTy *DeclAttrs = 0; |
| 66 | |
| 67 | const char *Decl::getDeclKindName() const { |
| 68 | switch (DeclKind) { |
| 69 | default: assert(0 && "Unknown decl kind!"); |
| 70 | case Namespace: return "Namespace"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 71 | case OverloadedFunction: return "OverloadedFunction"; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 72 | case Typedef: return "Typedef"; |
| 73 | case Function: return "Function"; |
| 74 | case Var: return "Var"; |
| 75 | case ParmVar: return "ParmVar"; |
Fariborz Jahanian | 4f5420d | 2008-12-20 21:06:28 +0000 | [diff] [blame] | 76 | case OriginalParmVar: return "OriginalParmVar"; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 77 | case EnumConstant: return "EnumConstant"; |
| 78 | case ObjCIvar: return "ObjCIvar"; |
| 79 | case ObjCInterface: return "ObjCInterface"; |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 80 | case ObjCImplementation: return "ObjCImplementation"; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 81 | case ObjCClass: return "ObjCClass"; |
| 82 | case ObjCMethod: return "ObjCMethod"; |
| 83 | case ObjCProtocol: return "ObjCProtocol"; |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 84 | case ObjCProperty: return "ObjCProperty"; |
| 85 | case ObjCPropertyImpl: return "ObjCPropertyImpl"; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 86 | case ObjCForwardProtocol: return "ObjCForwardProtocol"; |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 87 | case Record: return "Record"; |
| 88 | case CXXRecord: return "CXXRecord"; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 89 | case Enum: return "Enum"; |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 90 | case Block: return "Block"; |
Steve Naroff | 4c92fea | 2009-01-14 01:27:31 +0000 | [diff] [blame] | 91 | case Field: return "Field"; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame^] | 95 | const char *DeclContext::getDeclKindName() const { |
| 96 | switch (DeclKind) { |
| 97 | default: assert(0 && "Unknown decl kind!"); |
| 98 | case Decl::TranslationUnit: return "TranslationUnit"; |
| 99 | case Decl::Namespace: return "Namespace"; |
| 100 | case Decl::OverloadedFunction: return "OverloadedFunction"; |
| 101 | case Decl::Typedef: return "Typedef"; |
| 102 | case Decl::Function: return "Function"; |
| 103 | case Decl::Var: return "Var"; |
| 104 | case Decl::ParmVar: return "ParmVar"; |
| 105 | case Decl::OriginalParmVar: return "OriginalParmVar"; |
| 106 | case Decl::EnumConstant: return "EnumConstant"; |
| 107 | case Decl::ObjCIvar: return "ObjCIvar"; |
| 108 | case Decl::ObjCInterface: return "ObjCInterface"; |
| 109 | case Decl::ObjCImplementation: return "ObjCImplementation"; |
| 110 | case Decl::ObjCClass: return "ObjCClass"; |
| 111 | case Decl::ObjCMethod: return "ObjCMethod"; |
| 112 | case Decl::ObjCProtocol: return "ObjCProtocol"; |
| 113 | case Decl::ObjCProperty: return "ObjCProperty"; |
| 114 | case Decl::ObjCPropertyImpl: return "ObjCPropertyImpl"; |
| 115 | case Decl::ObjCForwardProtocol: return "ObjCForwardProtocol"; |
| 116 | case Decl::Record: return "Record"; |
| 117 | case Decl::CXXRecord: return "CXXRecord"; |
| 118 | case Decl::Enum: return "Enum"; |
| 119 | case Decl::Block: return "Block"; |
| 120 | case Decl::Field: return "Field"; |
| 121 | } |
| 122 | } |
| 123 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 124 | bool Decl::CollectingStats(bool Enable) { |
| 125 | if (Enable) |
| 126 | StatSwitch = true; |
| 127 | return StatSwitch; |
| 128 | } |
| 129 | |
| 130 | void Decl::PrintStats() { |
| 131 | fprintf(stderr, "*** Decl Stats:\n"); |
| 132 | fprintf(stderr, " %d decls total.\n", |
Fariborz Jahanian | 4f5420d | 2008-12-20 21:06:28 +0000 | [diff] [blame] | 133 | int(nFuncs+nVars+nParmVars+nOriginalParmVars+nFieldDecls+nSUC+nCXXSUC+ |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 134 | nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+ |
| 135 | nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls+ |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 136 | nAtDefsFieldDecls+nNamespaces+nOverFuncs)); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 137 | fprintf(stderr, " %d namespace decls, %d each (%d bytes)\n", |
| 138 | nNamespaces, (int)sizeof(NamespaceDecl), |
| 139 | int(nNamespaces*sizeof(NamespaceDecl))); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 140 | fprintf(stderr, " %d overloaded function decls, %d each (%d bytes)\n", |
| 141 | nOverFuncs, (int)sizeof(OverloadedFunctionDecl), |
| 142 | int(nOverFuncs*sizeof(OverloadedFunctionDecl))); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 143 | fprintf(stderr, " %d function decls, %d each (%d bytes)\n", |
| 144 | nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl))); |
| 145 | fprintf(stderr, " %d variable decls, %d each (%d bytes)\n", |
| 146 | nVars, (int)sizeof(VarDecl), |
| 147 | int(nVars*sizeof(VarDecl))); |
| 148 | fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n", |
| 149 | nParmVars, (int)sizeof(ParmVarDecl), |
| 150 | int(nParmVars*sizeof(ParmVarDecl))); |
Fariborz Jahanian | 4f5420d | 2008-12-20 21:06:28 +0000 | [diff] [blame] | 151 | fprintf(stderr, " %d original parameter variable decls, %d each (%d bytes)\n", |
| 152 | nOriginalParmVars, (int)sizeof(ParmVarWithOriginalTypeDecl), |
| 153 | int(nOriginalParmVars*sizeof(ParmVarWithOriginalTypeDecl))); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 154 | fprintf(stderr, " %d field decls, %d each (%d bytes)\n", |
| 155 | nFieldDecls, (int)sizeof(FieldDecl), |
| 156 | int(nFieldDecls*sizeof(FieldDecl))); |
Ted Kremenek | 01e6779 | 2008-08-20 03:26:33 +0000 | [diff] [blame] | 157 | fprintf(stderr, " %d @defs generated field decls, %d each (%d bytes)\n", |
| 158 | nAtDefsFieldDecls, (int)sizeof(ObjCAtDefsFieldDecl), |
| 159 | int(nAtDefsFieldDecls*sizeof(ObjCAtDefsFieldDecl))); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 160 | fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n", |
| 161 | nSUC, (int)sizeof(RecordDecl), |
| 162 | int(nSUC*sizeof(RecordDecl))); |
Argyrios Kyrtzidis | 55d71f9 | 2008-08-10 01:47:31 +0000 | [diff] [blame] | 163 | fprintf(stderr, " %d C++ struct/union/class decls, %d each (%d bytes)\n", |
| 164 | nCXXSUC, (int)sizeof(CXXRecordDecl), |
| 165 | int(nCXXSUC*sizeof(CXXRecordDecl))); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 166 | fprintf(stderr, " %d enum decls, %d each (%d bytes)\n", |
| 167 | nEnumDecls, (int)sizeof(EnumDecl), |
| 168 | int(nEnumDecls*sizeof(EnumDecl))); |
| 169 | fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n", |
| 170 | nEnumConst, (int)sizeof(EnumConstantDecl), |
| 171 | int(nEnumConst*sizeof(EnumConstantDecl))); |
| 172 | fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n", |
| 173 | nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl))); |
| 174 | // Objective-C decls... |
| 175 | fprintf(stderr, " %d interface decls, %d each (%d bytes)\n", |
| 176 | nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl), |
| 177 | int(nInterfaceDecls*sizeof(ObjCInterfaceDecl))); |
| 178 | fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n", |
| 179 | nIvarDecls, (int)sizeof(ObjCIvarDecl), |
| 180 | int(nIvarDecls*sizeof(ObjCIvarDecl))); |
| 181 | fprintf(stderr, " %d class decls, %d each (%d bytes)\n", |
| 182 | nClassDecls, (int)sizeof(ObjCClassDecl), |
| 183 | int(nClassDecls*sizeof(ObjCClassDecl))); |
| 184 | fprintf(stderr, " %d method decls, %d each (%d bytes)\n", |
| 185 | nMethodDecls, (int)sizeof(ObjCMethodDecl), |
| 186 | int(nMethodDecls*sizeof(ObjCMethodDecl))); |
| 187 | fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n", |
| 188 | nProtocolDecls, (int)sizeof(ObjCProtocolDecl), |
| 189 | int(nProtocolDecls*sizeof(ObjCProtocolDecl))); |
| 190 | fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n", |
| 191 | nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl), |
| 192 | int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl))); |
| 193 | fprintf(stderr, " %d category decls, %d each (%d bytes)\n", |
| 194 | nCategoryDecls, (int)sizeof(ObjCCategoryDecl), |
| 195 | int(nCategoryDecls*sizeof(ObjCCategoryDecl))); |
| 196 | |
| 197 | fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n", |
| 198 | nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl), |
| 199 | int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl))); |
| 200 | |
| 201 | fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n", |
| 202 | nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl), |
| 203 | int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl))); |
| 204 | |
| 205 | fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n", |
| 206 | nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl), |
| 207 | int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl))); |
| 208 | |
| 209 | fprintf(stderr, " %d property decls, %d each (%d bytes)\n", |
| 210 | nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl), |
| 211 | int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl))); |
| 212 | |
| 213 | fprintf(stderr, " %d property implementation decls, %d each (%d bytes)\n", |
| 214 | nObjCPropertyImplDecl, (int)sizeof(ObjCPropertyImplDecl), |
| 215 | int(nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl))); |
| 216 | |
| 217 | fprintf(stderr, "Total bytes = %d\n", |
| 218 | int(nFuncs*sizeof(FunctionDecl)+ |
| 219 | nVars*sizeof(VarDecl)+nParmVars*sizeof(ParmVarDecl)+ |
Fariborz Jahanian | 4f5420d | 2008-12-20 21:06:28 +0000 | [diff] [blame] | 220 | nOriginalParmVars*sizeof(ParmVarWithOriginalTypeDecl)+ |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 221 | nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+ |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 222 | nCXXSUC*sizeof(CXXRecordDecl)+ |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 223 | nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+ |
| 224 | nTypedef*sizeof(TypedefDecl)+ |
| 225 | nInterfaceDecls*sizeof(ObjCInterfaceDecl)+ |
| 226 | nIvarDecls*sizeof(ObjCIvarDecl)+ |
| 227 | nClassDecls*sizeof(ObjCClassDecl)+ |
| 228 | nMethodDecls*sizeof(ObjCMethodDecl)+ |
| 229 | nProtocolDecls*sizeof(ObjCProtocolDecl)+ |
| 230 | nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+ |
| 231 | nCategoryDecls*sizeof(ObjCCategoryDecl)+ |
| 232 | nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+ |
| 233 | nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+ |
| 234 | nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+ |
| 235 | nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+ |
| 236 | nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)+ |
| 237 | nLinkageSpecDecl*sizeof(LinkageSpecDecl)+ |
| 238 | nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)+ |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 239 | nNamespaces*sizeof(NamespaceDecl)+ |
| 240 | nOverFuncs*sizeof(OverloadedFunctionDecl))); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 241 | |
| 242 | } |
| 243 | |
| 244 | void Decl::addDeclKind(Kind k) { |
| 245 | switch (k) { |
| 246 | case Namespace: nNamespaces++; break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 247 | case OverloadedFunction: nOverFuncs++; break; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 248 | case Typedef: nTypedef++; break; |
| 249 | case Function: nFuncs++; break; |
| 250 | case Var: nVars++; break; |
| 251 | case ParmVar: nParmVars++; break; |
Fariborz Jahanian | 4f5420d | 2008-12-20 21:06:28 +0000 | [diff] [blame] | 252 | case OriginalParmVar: nOriginalParmVars++; break; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 253 | case EnumConstant: nEnumConst++; break; |
| 254 | case Field: nFieldDecls++; break; |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 255 | case Record: nSUC++; break; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 256 | case Enum: nEnumDecls++; break; |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 257 | case ObjCContainer: break; // is abstract...no need to account for. |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 258 | case ObjCInterface: nInterfaceDecls++; break; |
| 259 | case ObjCClass: nClassDecls++; break; |
| 260 | case ObjCMethod: nMethodDecls++; break; |
| 261 | case ObjCProtocol: nProtocolDecls++; break; |
| 262 | case ObjCForwardProtocol: nForwardProtocolDecls++; break; |
| 263 | case ObjCCategory: nCategoryDecls++; break; |
| 264 | case ObjCIvar: nIvarDecls++; break; |
Ted Kremenek | 01e6779 | 2008-08-20 03:26:33 +0000 | [diff] [blame] | 265 | case ObjCAtDefsField: nAtDefsFieldDecls++; break; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 266 | case ObjCImplementation: nObjCImplementationDecls++; break; |
| 267 | case ObjCCategoryImpl: nObjCCategoryImpl++; break; |
| 268 | case ObjCCompatibleAlias: nObjCCompatibleAlias++; break; |
| 269 | case ObjCProperty: nObjCPropertyDecl++; break; |
| 270 | case ObjCPropertyImpl: nObjCPropertyImplDecl++; break; |
| 271 | case LinkageSpec: nLinkageSpecDecl++; break; |
| 272 | case FileScopeAsm: nFileScopeAsmDecl++; break; |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 273 | case Block: nBlockDecls++; break; |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 274 | case ImplicitParam: |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 275 | case TranslationUnit: break; |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 276 | |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 277 | case CXXRecord: nCXXSUC++; break; |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 278 | // FIXME: Statistics for C++ decls. |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 279 | case TemplateTypeParm: |
| 280 | case NonTypeTemplateParm: |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 281 | case CXXMethod: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 282 | case CXXConstructor: |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 283 | case CXXDestructor: |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 284 | case CXXConversion: |
Argyrios Kyrtzidis | d3bb44f | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 285 | case CXXClassVar: |
| 286 | break; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| 290 | //===----------------------------------------------------------------------===// |
| 291 | // Decl Implementation |
| 292 | //===----------------------------------------------------------------------===// |
| 293 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 294 | void Decl::setDeclContext(DeclContext *DC) { |
| 295 | if (isOutOfSemaDC()) |
| 296 | delete getMultipleDC(); |
| 297 | |
| 298 | DeclCtx = reinterpret_cast<uintptr_t>(DC); |
| 299 | } |
| 300 | |
| 301 | void Decl::setLexicalDeclContext(DeclContext *DC) { |
| 302 | if (DC == getLexicalDeclContext()) |
| 303 | return; |
| 304 | |
| 305 | if (isInSemaDC()) { |
| 306 | MultipleDC *MDC = new MultipleDC(); |
| 307 | MDC->SemanticDC = getDeclContext(); |
| 308 | MDC->LexicalDC = DC; |
| 309 | DeclCtx = reinterpret_cast<uintptr_t>(MDC) | 0x1; |
| 310 | } else { |
| 311 | getMultipleDC()->LexicalDC = DC; |
| 312 | } |
| 313 | } |
| 314 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 315 | // Out-of-line virtual method providing a home for Decl. |
| 316 | Decl::~Decl() { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 317 | if (isOutOfSemaDC()) |
| 318 | delete getMultipleDC(); |
| 319 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 320 | if (!HasAttrs) |
| 321 | return; |
| 322 | |
| 323 | DeclAttrMapTy::iterator it = DeclAttrs->find(this); |
| 324 | assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!"); |
| 325 | |
| 326 | // release attributes. |
| 327 | delete it->second; |
| 328 | invalidateAttrs(); |
| 329 | } |
| 330 | |
| 331 | void Decl::addAttr(Attr *NewAttr) { |
| 332 | if (!DeclAttrs) |
| 333 | DeclAttrs = new DeclAttrMapTy(); |
| 334 | |
| 335 | Attr *&ExistingAttr = (*DeclAttrs)[this]; |
| 336 | |
| 337 | NewAttr->setNext(ExistingAttr); |
| 338 | ExistingAttr = NewAttr; |
| 339 | |
| 340 | HasAttrs = true; |
| 341 | } |
| 342 | |
| 343 | void Decl::invalidateAttrs() { |
| 344 | if (!HasAttrs) return; |
| 345 | |
| 346 | HasAttrs = false; |
| 347 | (*DeclAttrs)[this] = 0; |
| 348 | DeclAttrs->erase(this); |
| 349 | |
| 350 | if (DeclAttrs->empty()) { |
| 351 | delete DeclAttrs; |
| 352 | DeclAttrs = 0; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | const Attr *Decl::getAttrs() const { |
| 357 | if (!HasAttrs) |
| 358 | return 0; |
| 359 | |
| 360 | return (*DeclAttrs)[this]; |
| 361 | } |
| 362 | |
| 363 | void Decl::swapAttrs(Decl *RHS) { |
| 364 | bool HasLHSAttr = this->HasAttrs; |
| 365 | bool HasRHSAttr = RHS->HasAttrs; |
| 366 | |
| 367 | // Usually, neither decl has attrs, nothing to do. |
| 368 | if (!HasLHSAttr && !HasRHSAttr) return; |
| 369 | |
| 370 | // If 'this' has no attrs, swap the other way. |
| 371 | if (!HasLHSAttr) |
| 372 | return RHS->swapAttrs(this); |
| 373 | |
| 374 | // Handle the case when both decls have attrs. |
| 375 | if (HasRHSAttr) { |
| 376 | std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]); |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | // Otherwise, LHS has an attr and RHS doesn't. |
| 381 | (*DeclAttrs)[RHS] = (*DeclAttrs)[this]; |
| 382 | (*DeclAttrs).erase(this); |
| 383 | this->HasAttrs = false; |
| 384 | RHS->HasAttrs = true; |
| 385 | } |
| 386 | |
| 387 | |
| 388 | void Decl::Destroy(ASTContext& C) { |
Douglas Gregor | a0fc55f | 2009-01-13 19:47:12 +0000 | [diff] [blame] | 389 | #if 0 |
Douglas Gregor | 00ad0ef | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 390 | // FIXME: Once ownership is fully understood, we can enable this code |
| 391 | if (DeclContext *DC = dyn_cast<DeclContext>(this)) |
| 392 | DC->decls_begin()->Destroy(C); |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 393 | |
Douglas Gregor | 00ad0ef | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 394 | // Observe the unrolled recursion. By setting N->NextDeclInScope = 0x0 |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 395 | // within the loop, only the Destroy method for the first Decl |
| 396 | // will deallocate all of the Decls in a chain. |
| 397 | |
Douglas Gregor | 00ad0ef | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 398 | Decl* N = NextDeclInScope; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 399 | |
| 400 | while (N) { |
Douglas Gregor | 00ad0ef | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 401 | Decl* Tmp = N->NextDeclInScope; |
| 402 | N->NextDeclInScope = 0; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 403 | N->Destroy(C); |
| 404 | N = Tmp; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 405 | } |
Douglas Gregor | a0fc55f | 2009-01-13 19:47:12 +0000 | [diff] [blame] | 406 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 407 | this->~Decl(); |
| 408 | C.getAllocator().Deallocate((void *)this); |
Douglas Gregor | 00ad0ef | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 409 | #endif |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Argyrios Kyrtzidis | 42220c5 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 412 | Decl *Decl::castFromDeclContext (const DeclContext *D) { |
| 413 | return DeclContext::CastTo<Decl>(D); |
| 414 | } |
| 415 | |
| 416 | DeclContext *Decl::castToDeclContext(const Decl *D) { |
| 417 | return DeclContext::CastTo<DeclContext>(D); |
| 418 | } |
| 419 | |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 420 | //===----------------------------------------------------------------------===// |
| 421 | // DeclContext Implementation |
| 422 | //===----------------------------------------------------------------------===// |
| 423 | |
Argyrios Kyrtzidis | 20bc676 | 2008-11-19 17:36:39 +0000 | [diff] [blame] | 424 | const DeclContext *DeclContext::getParent() const { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 425 | if (const Decl *D = dyn_cast<Decl>(this)) |
| 426 | return D->getDeclContext(); |
| 427 | |
| 428 | return NULL; |
Eli Friedman | 56d2937 | 2008-06-07 16:52:53 +0000 | [diff] [blame] | 429 | } |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 430 | |
| 431 | const DeclContext *DeclContext::getLexicalParent() const { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 432 | if (const Decl *D = dyn_cast<Decl>(this)) |
| 433 | return D->getLexicalDeclContext(); |
| 434 | |
Argyrios Kyrtzidis | 051c13a | 2008-11-19 18:07:24 +0000 | [diff] [blame] | 435 | return getParent(); |
Argyrios Kyrtzidis | 77407b8 | 2008-11-19 18:01:13 +0000 | [diff] [blame] | 436 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 437 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 438 | // FIXME: We really want to use a DenseSet here to eliminate the |
| 439 | // redundant storage of the declaration names, but (1) it doesn't give |
| 440 | // us the ability to search based on DeclarationName, (2) we really |
| 441 | // need something more like a DenseMultiSet, and (3) it's |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 442 | // implemented in terms of DenseMap anyway. However, this data |
| 443 | // structure is really space-inefficient, so we'll have to do |
| 444 | // something. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 445 | typedef llvm::DenseMap<DeclarationName, std::vector<NamedDecl*> > |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 446 | StoredDeclsMap; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 447 | |
| 448 | DeclContext::~DeclContext() { |
| 449 | unsigned Size = LookupPtr.getInt(); |
| 450 | if (Size == LookupIsMap) { |
| 451 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer()); |
| 452 | delete Map; |
| 453 | } else { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 454 | NamedDecl **Array = static_cast<NamedDecl**>(LookupPtr.getPointer()); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 455 | delete [] Array; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | void DeclContext::DestroyDecls(ASTContext &C) { |
Douglas Gregor | 00ad0ef | 2009-01-20 04:25:11 +0000 | [diff] [blame] | 460 | for (decl_iterator D = decls_begin(); D != decls_end(); ) |
| 461 | (*D++)->Destroy(C); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 464 | bool DeclContext::isTransparentContext() const { |
| 465 | if (DeclKind == Decl::Enum) |
| 466 | return true; // FIXME: Check for C++0x scoped enums |
| 467 | else if (DeclKind == Decl::LinkageSpec) |
| 468 | return true; |
| 469 | else if (DeclKind == Decl::Record || DeclKind == Decl::CXXRecord) |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 470 | return cast<RecordDecl>(this)->isAnonymousStructOrUnion(); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 471 | else if (DeclKind == Decl::Namespace) |
| 472 | return false; // FIXME: Check for C++0x inline namespaces |
| 473 | |
| 474 | return false; |
| 475 | } |
| 476 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 477 | DeclContext *DeclContext::getPrimaryContext() { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 478 | switch (DeclKind) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 479 | case Decl::TranslationUnit: |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 480 | case Decl::LinkageSpec: |
| 481 | case Decl::Block: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 482 | // There is only one DeclContext for these entities. |
| 483 | return this; |
| 484 | |
| 485 | case Decl::Namespace: |
| 486 | // The original namespace is our primary context. |
| 487 | return static_cast<NamespaceDecl*>(this)->getOriginalNamespace(); |
| 488 | |
| 489 | case Decl::Enum: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 490 | case Decl::Record: |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 491 | case Decl::CXXRecord: |
| 492 | // If this is a tag type that has a definition or is currently |
| 493 | // being defined, that definition is our primary context. |
| 494 | if (TagType *TagT = cast_or_null<TagType>(cast<TagDecl>(this)->TypeForDecl)) |
| 495 | if (TagT->isBeingDefined() || |
| 496 | (TagT->getDecl() && TagT->getDecl()->isDefinition())) |
| 497 | return TagT->getDecl(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 498 | return this; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 499 | |
| 500 | case Decl::ObjCMethod: |
| 501 | return this; |
| 502 | |
| 503 | case Decl::ObjCInterface: |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 504 | case Decl::ObjCProtocol: |
| 505 | case Decl::ObjCCategory: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 506 | // FIXME: Can Objective-C interfaces be forward-declared? |
| 507 | return this; |
| 508 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 509 | case Decl::ObjCImplementation: |
| 510 | case Decl::ObjCCategoryImpl: |
| 511 | return this; |
| 512 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 513 | default: |
| 514 | assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast && |
| 515 | "Unknown DeclContext kind"); |
| 516 | return this; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | DeclContext *DeclContext::getNextContext() { |
| 521 | switch (DeclKind) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 522 | case Decl::TranslationUnit: |
| 523 | case Decl::Enum: |
| 524 | case Decl::Record: |
| 525 | case Decl::CXXRecord: |
| 526 | case Decl::ObjCMethod: |
| 527 | case Decl::ObjCInterface: |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 528 | case Decl::ObjCCategory: |
| 529 | case Decl::ObjCProtocol: |
| 530 | case Decl::ObjCImplementation: |
| 531 | case Decl::ObjCCategoryImpl: |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 532 | case Decl::LinkageSpec: |
| 533 | case Decl::Block: |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 534 | // There is only one DeclContext for these entities. |
| 535 | return 0; |
| 536 | |
| 537 | case Decl::Namespace: |
| 538 | // Return the next namespace |
| 539 | return static_cast<NamespaceDecl*>(this)->getNextNamespace(); |
| 540 | |
| 541 | default: |
| 542 | assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast && |
| 543 | "Unknown DeclContext kind"); |
| 544 | return 0; |
| 545 | } |
| 546 | } |
| 547 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 548 | void DeclContext::addDecl(Decl *D) { |
Douglas Gregor | a8cc8ce | 2009-01-09 18:51:29 +0000 | [diff] [blame] | 549 | assert(D->getLexicalDeclContext() == this && "Decl inserted into wrong lexical context"); |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 550 | assert(!D->NextDeclInScope && D != LastDecl && |
| 551 | "Decl already inserted into a DeclContext"); |
| 552 | |
| 553 | if (FirstDecl) { |
| 554 | LastDecl->NextDeclInScope = D; |
| 555 | LastDecl = D; |
| 556 | } else { |
| 557 | FirstDecl = LastDecl = D; |
| 558 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 559 | |
| 560 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 561 | ND->getDeclContext()->makeDeclVisibleInContext(ND); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 564 | /// buildLookup - Build the lookup data structure with all of the |
| 565 | /// declarations in DCtx (and any other contexts linked to it or |
| 566 | /// transparent contexts nested within it). |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 567 | void DeclContext::buildLookup(DeclContext *DCtx) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 568 | for (; DCtx; DCtx = DCtx->getNextContext()) { |
Douglas Gregor | 4f3b8f8 | 2009-01-06 07:17:58 +0000 | [diff] [blame] | 569 | for (decl_iterator D = DCtx->decls_begin(), DEnd = DCtx->decls_end(); |
| 570 | D != DEnd; ++D) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 571 | // Insert this declaration into the lookup structure |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 572 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 573 | makeDeclVisibleInContextImpl(ND); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 574 | |
| 575 | // If this declaration is itself a transparent declaration context, |
| 576 | // add its members (recursively). |
| 577 | if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D)) |
| 578 | if (InnerCtx->isTransparentContext()) |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 579 | buildLookup(InnerCtx->getPrimaryContext()); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | } |
| 583 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 584 | DeclContext::lookup_result |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 585 | DeclContext::lookup(DeclarationName Name) { |
| 586 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 587 | if (PrimaryContext != this) |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 588 | return PrimaryContext->lookup(Name); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 589 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 590 | /// If there is no lookup data structure, build one now by walking |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 591 | /// all of the linked DeclContexts (in declaration order!) and |
| 592 | /// inserting their values. |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 593 | if (LookupPtr.getPointer() == 0) |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 594 | buildLookup(this); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 595 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 596 | if (isLookupMap()) { |
| 597 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer()); |
| 598 | StoredDeclsMap::iterator Pos = Map->find(Name); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 599 | if (Pos != Map->end()) |
| 600 | return lookup_result(&Pos->second.front(), |
| 601 | &Pos->second.front() + Pos->second.size()); |
| 602 | return lookup_result(0, 0); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | // We have a small array. Look into it. |
| 606 | unsigned Size = LookupPtr.getInt(); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 607 | NamedDecl **Array = static_cast<NamedDecl**>(LookupPtr.getPointer()); |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 608 | for (unsigned Idx = 0; Idx != Size; ++Idx) |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 609 | if (Array[Idx]->getDeclName() == Name) { |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 610 | unsigned Last = Idx + 1; |
| 611 | while (Last != Size && Array[Last]->getDeclName() == Name) |
| 612 | ++Last; |
| 613 | return lookup_result(&Array[Idx], &Array[Last]); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 616 | return lookup_result(0, 0); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | DeclContext::lookup_const_result |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 620 | DeclContext::lookup(DeclarationName Name) const { |
| 621 | return const_cast<DeclContext*>(this)->lookup(Name); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Douglas Gregor | 17a9b9e | 2009-01-07 02:48:43 +0000 | [diff] [blame] | 624 | const DeclContext *DeclContext::getLookupContext() const { |
| 625 | const DeclContext *Ctx = this; |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 626 | // Skip through transparent contexts. |
Douglas Gregor | ce35607 | 2009-01-06 23:51:29 +0000 | [diff] [blame] | 627 | while (Ctx->isTransparentContext()) |
| 628 | Ctx = Ctx->getParent(); |
| 629 | return Ctx; |
| 630 | } |
| 631 | |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 632 | void DeclContext::makeDeclVisibleInContext(NamedDecl *D) { |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 633 | DeclContext *PrimaryContext = getPrimaryContext(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 634 | if (PrimaryContext != this) { |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 635 | PrimaryContext->makeDeclVisibleInContext(D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 636 | return; |
| 637 | } |
| 638 | |
| 639 | // If we already have a lookup data structure, perform the insertion |
| 640 | // into it. Otherwise, be lazy and don't build that structure until |
| 641 | // someone asks for it. |
| 642 | if (LookupPtr.getPointer()) |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 643 | makeDeclVisibleInContextImpl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 644 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 645 | // If we are a transparent context, insert into our parent context, |
| 646 | // too. This operation is recursive. |
| 647 | if (isTransparentContext()) |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 648 | getParent()->makeDeclVisibleInContext(D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 651 | void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 652 | // Skip unnamed declarations. |
| 653 | if (!D->getDeclName()) |
| 654 | return; |
| 655 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 656 | bool MayBeRedeclaration = true; |
| 657 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 658 | if (!isLookupMap()) { |
| 659 | unsigned Size = LookupPtr.getInt(); |
| 660 | |
| 661 | // The lookup data is stored as an array. Search through the array |
| 662 | // to find the insertion location. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 663 | NamedDecl **Array; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 664 | if (Size == 0) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 665 | Array = new NamedDecl*[LookupIsMap - 1]; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 666 | LookupPtr.setPointer(Array); |
| 667 | } else { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 668 | Array = static_cast<NamedDecl **>(LookupPtr.getPointer()); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | // We always keep declarations of the same name next to each other |
| 672 | // in the array, so that it is easy to return multiple results |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 673 | // from lookup(). |
| 674 | unsigned FirstMatch; |
| 675 | for (FirstMatch = 0; FirstMatch != Size; ++FirstMatch) |
| 676 | if (Array[FirstMatch]->getDeclName() == D->getDeclName()) |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 677 | break; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 678 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 679 | unsigned InsertPos = FirstMatch; |
| 680 | if (FirstMatch != Size) { |
| 681 | // We found another declaration with the same name. First |
| 682 | // determine whether this is a redeclaration of an existing |
| 683 | // declaration in this scope, in which case we will replace the |
| 684 | // existing declaration. |
| 685 | unsigned LastMatch = FirstMatch; |
| 686 | for (; LastMatch != Size; ++LastMatch) { |
| 687 | if (Array[LastMatch]->getDeclName() != D->getDeclName()) |
| 688 | break; |
| 689 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 690 | if (D->declarationReplaces(Array[LastMatch])) { |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 691 | // D is a redeclaration of an existing element in the |
| 692 | // array. Replace that element with D. |
| 693 | Array[LastMatch] = D; |
| 694 | return; |
| 695 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 698 | // [FirstMatch, LastMatch) contains the set of declarations that |
| 699 | // have the same name as this declaration. Determine where the |
| 700 | // declaration D will be inserted into this range. |
| 701 | if (D->getIdentifierNamespace() == Decl::IDNS_Tag) |
| 702 | InsertPos = LastMatch; |
| 703 | else if (Array[LastMatch-1]->getIdentifierNamespace() == Decl::IDNS_Tag) |
| 704 | InsertPos = LastMatch - 1; |
| 705 | else |
| 706 | InsertPos = LastMatch; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | if (Size < LookupIsMap - 1) { |
| 710 | // The new declaration will fit in the array. Insert the new |
| 711 | // declaration at the position Match in the array. |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 712 | for (unsigned Idx = Size; Idx > InsertPos; --Idx) |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 713 | Array[Idx] = Array[Idx-1]; |
| 714 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 715 | Array[InsertPos] = D; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 716 | LookupPtr.setInt(Size + 1); |
| 717 | return; |
| 718 | } |
| 719 | |
| 720 | // We've reached capacity in this array. Create a map and copy in |
| 721 | // all of the declarations that were stored in the array. |
| 722 | StoredDeclsMap *Map = new StoredDeclsMap(16); |
| 723 | LookupPtr.setPointer(Map); |
| 724 | LookupPtr.setInt(LookupIsMap); |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 725 | for (unsigned Idx = 0; Idx != LookupIsMap - 1; ++Idx) |
Douglas Gregor | 40f4e69 | 2009-01-20 16:54:50 +0000 | [diff] [blame] | 726 | makeDeclVisibleInContextImpl(Array[Idx]); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 727 | delete [] Array; |
| 728 | |
| 729 | // Fall through to perform insertion into the map. |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 730 | MayBeRedeclaration = false; |
| 731 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 732 | |
| 733 | // Insert this declaration into the map. |
| 734 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer()); |
| 735 | StoredDeclsMap::iterator Pos = Map->find(D->getDeclName()); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 736 | if (Pos != Map->end()) { |
| 737 | if (MayBeRedeclaration) { |
| 738 | // Determine if this declaration is actually a redeclaration. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 739 | std::vector<NamedDecl *>::iterator Redecl |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 740 | = std::find_if(Pos->second.begin(), Pos->second.end(), |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 741 | std::bind1st(std::mem_fun(&NamedDecl::declarationReplaces), |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 742 | D)); |
| 743 | if (Redecl != Pos->second.end()) { |
| 744 | *Redecl = D; |
| 745 | return; |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 746 | } |
| 747 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 748 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 749 | // Put this declaration into the appropriate slot. |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 750 | if (D->getIdentifierNamespace() == Decl::IDNS_Tag || Pos->second.empty()) |
| 751 | Pos->second.push_back(D); |
| 752 | else if (Pos->second.back()->getIdentifierNamespace() == Decl::IDNS_Tag) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 753 | NamedDecl *TagD = Pos->second.back(); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 754 | Pos->second.back() = D; |
| 755 | Pos->second.push_back(TagD); |
| 756 | } else |
| 757 | Pos->second.push_back(D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 758 | } else { |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 759 | (*Map)[D->getDeclName()].push_back(D); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 760 | } |
| 761 | } |