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