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