Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- IdentifierTable.cpp - Hash table for identifier lookup -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the IdentifierInfo, IdentifierVisitor, and |
| 11 | // IdentifierTable interfaces. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | c7229c3 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 15 | #include "clang/Basic/IdentifierTable.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/Basic/LangOptions.h" |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/FoldingSet.h" |
Chris Lattner | 8599426 | 2007-10-05 20:15:24 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | 3daed52 | 2009-03-02 22:20:04 +0000 | [diff] [blame] | 19 | #include <cstdio> |
Ted Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 20 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // IdentifierInfo Implementation |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
Ted Kremenek | ea9c26b | 2009-01-20 23:28:34 +0000 | [diff] [blame] | 27 | IdentifierInfo::IdentifierInfo() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | TokenID = tok::identifier; |
Douglas Gregor | 5142af3 | 2008-11-06 16:32:23 +0000 | [diff] [blame] | 29 | ObjCOrBuiltinID = 0; |
Chris Lattner | 4365a7e | 2007-10-07 07:09:52 +0000 | [diff] [blame] | 30 | HasMacro = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | IsExtension = false; |
| 32 | IsPoisoned = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | IsCPPOperatorKeyword = false; |
Chris Lattner | 6a170eb | 2009-01-21 07:43:11 +0000 | [diff] [blame] | 34 | NeedsHandleIdentifier = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | FETokenInfo = 0; |
Ted Kremenek | ea9c26b | 2009-01-20 23:28:34 +0000 | [diff] [blame] | 36 | Entry = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
| 40 | // IdentifierTable Implementation |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 43 | IdentifierInfoLookup::~IdentifierInfoLookup() {} |
| 44 | |
Douglas Gregor | 8c5a760 | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 45 | ExternalIdentifierLookup::~ExternalIdentifierLookup() {} |
| 46 | |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 47 | IdentifierTable::IdentifierTable(const LangOptions &LangOpts, |
| 48 | IdentifierInfoLookup* externalLookup) |
| 49 | : HashTable(8192), // Start with space for 8K identifiers. |
| 50 | ExternalLookup(externalLookup) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 51 | |
| 52 | // Populate the identifier table with info about keywords for the current |
| 53 | // language. |
| 54 | AddKeywords(LangOpts); |
| 55 | } |
| 56 | |
| 57 | //===----------------------------------------------------------------------===// |
| 58 | // Language Keyword Implementation |
| 59 | //===----------------------------------------------------------------------===// |
| 60 | |
| 61 | /// AddKeyword - This method is used to associate a token ID with specific |
| 62 | /// identifiers because they are language keywords. This causes the lexer to |
| 63 | /// automatically map matching identifiers to specialized token codes. |
| 64 | /// |
Chris Lattner | d4b80f1 | 2007-07-16 04:18:29 +0000 | [diff] [blame] | 65 | /// The C90/C99/CPP/CPP0x flags are set to 0 if the token should be |
| 66 | /// enabled in the specified langauge, set to 1 if it is an extension |
| 67 | /// in the specified language, and set to 2 if disabled in the |
| 68 | /// specified language. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 69 | static void AddKeyword(const char *Keyword, unsigned KWLen, |
| 70 | tok::TokenKind TokenCode, |
Nate Begeman | 8aebcb7 | 2007-11-15 07:30:50 +0000 | [diff] [blame] | 71 | int C90, int C99, int CXX, int CXX0x, int BoolSupport, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 72 | const LangOptions &LangOpts, IdentifierTable &Table) { |
Nate Begeman | 8aebcb7 | 2007-11-15 07:30:50 +0000 | [diff] [blame] | 73 | int Flags = 0; |
| 74 | if (BoolSupport != 0) { |
Douglas Gregor | 7de3d79 | 2008-09-11 12:06:59 +0000 | [diff] [blame] | 75 | Flags = LangOpts.CPlusPlus? 0 : LangOpts.Boolean ? BoolSupport : 2; |
Nate Begeman | 8aebcb7 | 2007-11-15 07:30:50 +0000 | [diff] [blame] | 76 | } else if (LangOpts.CPlusPlus) { |
| 77 | Flags = LangOpts.CPlusPlus0x ? CXX0x : CXX; |
| 78 | } else if (LangOpts.C99) { |
| 79 | Flags = C99; |
| 80 | } else { |
| 81 | Flags = C90; |
| 82 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 83 | |
| 84 | // Don't add this keyword if disabled in this language or if an extension |
| 85 | // and extensions are disabled. |
| 86 | if (Flags + LangOpts.NoExtensions >= 2) return; |
| 87 | |
| 88 | IdentifierInfo &Info = Table.get(Keyword, Keyword+KWLen); |
| 89 | Info.setTokenID(TokenCode); |
| 90 | Info.setIsExtensionToken(Flags == 1); |
| 91 | } |
| 92 | |
| 93 | static void AddAlias(const char *Keyword, unsigned KWLen, |
Chris Lattner | 49581f4 | 2008-02-19 06:46:10 +0000 | [diff] [blame] | 94 | tok::TokenKind AliaseeID, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 95 | const char *AliaseeKeyword, unsigned AliaseeKWLen, |
| 96 | const LangOptions &LangOpts, IdentifierTable &Table) { |
| 97 | IdentifierInfo &AliasInfo = Table.get(Keyword, Keyword+KWLen); |
| 98 | IdentifierInfo &AliaseeInfo = Table.get(AliaseeKeyword, |
| 99 | AliaseeKeyword+AliaseeKWLen); |
Chris Lattner | 49581f4 | 2008-02-19 06:46:10 +0000 | [diff] [blame] | 100 | AliasInfo.setTokenID(AliaseeID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 101 | AliasInfo.setIsExtensionToken(AliaseeInfo.isExtensionToken()); |
| 102 | } |
| 103 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 104 | /// AddCXXOperatorKeyword - Register a C++ operator keyword alternative |
| 105 | /// representations. |
| 106 | static void AddCXXOperatorKeyword(const char *Keyword, unsigned KWLen, |
| 107 | tok::TokenKind TokenCode, |
| 108 | IdentifierTable &Table) { |
| 109 | IdentifierInfo &Info = Table.get(Keyword, Keyword + KWLen); |
| 110 | Info.setTokenID(TokenCode); |
Ted Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 111 | Info.setIsCPlusPlusOperatorKeyword(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or |
| 115 | /// "property". |
| 116 | static void AddObjCKeyword(tok::ObjCKeywordKind ObjCID, |
| 117 | const char *Name, unsigned NameLen, |
| 118 | IdentifierTable &Table) { |
| 119 | Table.get(Name, Name+NameLen).setObjCKeywordID(ObjCID); |
| 120 | } |
| 121 | |
| 122 | /// AddKeywords - Add all keywords to the symbol table. |
| 123 | /// |
| 124 | void IdentifierTable::AddKeywords(const LangOptions &LangOpts) { |
| 125 | enum { |
| 126 | C90Shift = 0, |
| 127 | EXTC90 = 1 << C90Shift, |
| 128 | NOTC90 = 2 << C90Shift, |
| 129 | C99Shift = 2, |
| 130 | EXTC99 = 1 << C99Shift, |
| 131 | NOTC99 = 2 << C99Shift, |
| 132 | CPPShift = 4, |
| 133 | EXTCPP = 1 << CPPShift, |
| 134 | NOTCPP = 2 << CPPShift, |
Chris Lattner | d4b80f1 | 2007-07-16 04:18:29 +0000 | [diff] [blame] | 135 | CPP0xShift = 6, |
| 136 | EXTCPP0x = 1 << CPP0xShift, |
| 137 | NOTCPP0x = 2 << CPP0xShift, |
Nate Begeman | 8aebcb7 | 2007-11-15 07:30:50 +0000 | [diff] [blame] | 138 | BoolShift = 8, |
| 139 | BOOLSUPPORT = 1 << BoolShift, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 140 | Mask = 3 |
| 141 | }; |
| 142 | |
| 143 | // Add keywords and tokens for the current language. |
| 144 | #define KEYWORD(NAME, FLAGS) \ |
| 145 | AddKeyword(#NAME, strlen(#NAME), tok::kw_ ## NAME, \ |
| 146 | ((FLAGS) >> C90Shift) & Mask, \ |
| 147 | ((FLAGS) >> C99Shift) & Mask, \ |
Chris Lattner | d4b80f1 | 2007-07-16 04:18:29 +0000 | [diff] [blame] | 148 | ((FLAGS) >> CPPShift) & Mask, \ |
Nate Begeman | 8aebcb7 | 2007-11-15 07:30:50 +0000 | [diff] [blame] | 149 | ((FLAGS) >> CPP0xShift) & Mask, \ |
| 150 | ((FLAGS) >> BoolShift) & Mask, LangOpts, *this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 151 | #define ALIAS(NAME, TOK) \ |
Chris Lattner | 49581f4 | 2008-02-19 06:46:10 +0000 | [diff] [blame] | 152 | AddAlias(NAME, strlen(NAME), tok::kw_ ## TOK, #TOK, strlen(#TOK), \ |
| 153 | LangOpts, *this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 154 | #define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \ |
| 155 | if (LangOpts.CXXOperatorNames) \ |
| 156 | AddCXXOperatorKeyword(#NAME, strlen(#NAME), tok::ALIAS, *this); |
| 157 | #define OBJC1_AT_KEYWORD(NAME) \ |
| 158 | if (LangOpts.ObjC1) \ |
| 159 | AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this); |
| 160 | #define OBJC2_AT_KEYWORD(NAME) \ |
| 161 | if (LangOpts.ObjC2) \ |
| 162 | AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this); |
| 163 | #include "clang/Basic/TokenKinds.def" |
| 164 | } |
| 165 | |
Chris Lattner | 387b98d | 2007-10-07 07:52:34 +0000 | [diff] [blame] | 166 | tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const { |
| 167 | // We use a perfect hash function here involving the length of the keyword, |
| 168 | // the first and third character. For preprocessor ID's there are no |
| 169 | // collisions (if there were, the switch below would complain about duplicate |
| 170 | // case values). Note that this depends on 'if' being null terminated. |
| 171 | |
| 172 | #define HASH(LEN, FIRST, THIRD) \ |
| 173 | (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31) |
| 174 | #define CASE(LEN, FIRST, THIRD, NAME) \ |
| 175 | case HASH(LEN, FIRST, THIRD): \ |
| 176 | return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME |
| 177 | |
| 178 | unsigned Len = getLength(); |
Chris Lattner | a31f030 | 2007-10-10 20:59:57 +0000 | [diff] [blame] | 179 | if (Len < 2) return tok::pp_not_keyword; |
Chris Lattner | 387b98d | 2007-10-07 07:52:34 +0000 | [diff] [blame] | 180 | const char *Name = getName(); |
| 181 | switch (HASH(Len, Name[0], Name[2])) { |
| 182 | default: return tok::pp_not_keyword; |
| 183 | CASE( 2, 'i', '\0', if); |
| 184 | CASE( 4, 'e', 'i', elif); |
| 185 | CASE( 4, 'e', 's', else); |
| 186 | CASE( 4, 'l', 'n', line); |
| 187 | CASE( 4, 's', 'c', sccs); |
| 188 | CASE( 5, 'e', 'd', endif); |
| 189 | CASE( 5, 'e', 'r', error); |
| 190 | CASE( 5, 'i', 'e', ident); |
| 191 | CASE( 5, 'i', 'd', ifdef); |
| 192 | CASE( 5, 'u', 'd', undef); |
| 193 | |
| 194 | CASE( 6, 'a', 's', assert); |
| 195 | CASE( 6, 'd', 'f', define); |
| 196 | CASE( 6, 'i', 'n', ifndef); |
| 197 | CASE( 6, 'i', 'p', import); |
| 198 | CASE( 6, 'p', 'a', pragma); |
| 199 | |
| 200 | CASE( 7, 'd', 'f', defined); |
| 201 | CASE( 7, 'i', 'c', include); |
| 202 | CASE( 7, 'w', 'r', warning); |
| 203 | |
| 204 | CASE( 8, 'u', 'a', unassert); |
| 205 | CASE(12, 'i', 'c', include_next); |
Chris Lattner | b8e240e | 2009-04-08 18:24:34 +0000 | [diff] [blame] | 206 | |
| 207 | CASE(16, '_', 'i', __include_macros); |
Chris Lattner | 387b98d | 2007-10-07 07:52:34 +0000 | [diff] [blame] | 208 | #undef CASE |
| 209 | #undef HASH |
| 210 | } |
| 211 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 212 | |
| 213 | //===----------------------------------------------------------------------===// |
| 214 | // Stats Implementation |
| 215 | //===----------------------------------------------------------------------===// |
| 216 | |
| 217 | /// PrintStats - Print statistics about how well the identifier table is doing |
| 218 | /// at hashing identifiers. |
| 219 | void IdentifierTable::PrintStats() const { |
| 220 | unsigned NumBuckets = HashTable.getNumBuckets(); |
| 221 | unsigned NumIdentifiers = HashTable.getNumItems(); |
| 222 | unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers; |
| 223 | unsigned AverageIdentifierSize = 0; |
| 224 | unsigned MaxIdentifierLength = 0; |
| 225 | |
| 226 | // TODO: Figure out maximum times an identifier had to probe for -stats. |
Ted Kremenek | ea9c26b | 2009-01-20 23:28:34 +0000 | [diff] [blame] | 227 | for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 228 | I = HashTable.begin(), E = HashTable.end(); I != E; ++I) { |
| 229 | unsigned IdLen = I->getKeyLength(); |
| 230 | AverageIdentifierSize += IdLen; |
| 231 | if (MaxIdentifierLength < IdLen) |
| 232 | MaxIdentifierLength = IdLen; |
| 233 | } |
| 234 | |
| 235 | fprintf(stderr, "\n*** Identifier Table Stats:\n"); |
| 236 | fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers); |
| 237 | fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets); |
| 238 | fprintf(stderr, "Hash density (#identifiers per bucket): %f\n", |
| 239 | NumIdentifiers/(double)NumBuckets); |
| 240 | fprintf(stderr, "Ave identifier length: %f\n", |
| 241 | (AverageIdentifierSize/(double)NumIdentifiers)); |
| 242 | fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength); |
| 243 | |
| 244 | // Compute statistics about the memory allocated for identifiers. |
| 245 | HashTable.getAllocator().PrintStats(); |
| 246 | } |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 247 | |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 248 | //===----------------------------------------------------------------------===// |
| 249 | // SelectorTable Implementation |
| 250 | //===----------------------------------------------------------------------===// |
| 251 | |
Chris Lattner | 8599426 | 2007-10-05 20:15:24 +0000 | [diff] [blame] | 252 | unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) { |
| 253 | return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr()); |
| 254 | } |
| 255 | |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 256 | namespace clang { |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 257 | /// MultiKeywordSelector - One of these variable length records is kept for each |
| 258 | /// selector containing more than one keyword. We use a folding set |
| 259 | /// to unique aggregate names (keyword selectors in ObjC parlance). Access to |
| 260 | /// this class is provided strictly through Selector. |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 261 | class MultiKeywordSelector |
| 262 | : public DeclarationNameExtra, public llvm::FoldingSetNode { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 263 | MultiKeywordSelector(unsigned nKeys) { |
| 264 | ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys; |
| 265 | } |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 266 | public: |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 267 | // Constructor for keyword selectors. |
| 268 | MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) { |
| 269 | assert((nKeys > 1) && "not a multi-keyword selector"); |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 270 | ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys; |
Ted Kremenek | bdbb285 | 2007-11-30 22:46:56 +0000 | [diff] [blame] | 271 | |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 272 | // Fill in the trailing keyword array. |
| 273 | IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1); |
| 274 | for (unsigned i = 0; i != nKeys; ++i) |
| 275 | KeyInfo[i] = IIV[i]; |
Ted Kremenek | bdbb285 | 2007-11-30 22:46:56 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Chris Lattner | f836e3f | 2007-10-07 01:33:16 +0000 | [diff] [blame] | 278 | // getName - Derive the full selector name and return it. |
| 279 | std::string getName() const; |
| 280 | |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 281 | unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; } |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 282 | |
| 283 | typedef IdentifierInfo *const *keyword_iterator; |
| 284 | keyword_iterator keyword_begin() const { |
| 285 | return reinterpret_cast<keyword_iterator>(this+1); |
| 286 | } |
| 287 | keyword_iterator keyword_end() const { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 288 | return keyword_begin()+getNumArgs(); |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 289 | } |
Chris Lattner | f836e3f | 2007-10-07 01:33:16 +0000 | [diff] [blame] | 290 | IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 291 | assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index"); |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 292 | return keyword_begin()[i]; |
| 293 | } |
| 294 | static void Profile(llvm::FoldingSetNodeID &ID, |
| 295 | keyword_iterator ArgTys, unsigned NumArgs) { |
| 296 | ID.AddInteger(NumArgs); |
Chris Lattner | f836e3f | 2007-10-07 01:33:16 +0000 | [diff] [blame] | 297 | for (unsigned i = 0; i != NumArgs; ++i) |
| 298 | ID.AddPointer(ArgTys[i]); |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 299 | } |
| 300 | void Profile(llvm::FoldingSetNodeID &ID) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 301 | Profile(ID, keyword_begin(), getNumArgs()); |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 302 | } |
| 303 | }; |
Chris Lattner | 8599426 | 2007-10-05 20:15:24 +0000 | [diff] [blame] | 304 | } // end namespace clang. |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 305 | |
| 306 | unsigned Selector::getNumArgs() const { |
| 307 | unsigned IIF = getIdentifierInfoFlag(); |
| 308 | if (IIF == ZeroArg) |
| 309 | return 0; |
| 310 | if (IIF == OneArg) |
| 311 | return 1; |
| 312 | // We point to a MultiKeywordSelector (pointer doesn't contain any flags). |
| 313 | MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr); |
| 314 | return SI->getNumArgs(); |
| 315 | } |
| 316 | |
Chris Lattner | f836e3f | 2007-10-07 01:33:16 +0000 | [diff] [blame] | 317 | IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const { |
| 318 | if (IdentifierInfo *II = getAsIdentifierInfo()) { |
| 319 | assert(argIndex == 0 && "illegal keyword index"); |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 320 | return II; |
| 321 | } |
| 322 | // We point to a MultiKeywordSelector (pointer doesn't contain any flags). |
| 323 | MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr); |
| 324 | return SI->getIdentifierInfoForSlot(argIndex); |
| 325 | } |
| 326 | |
Chris Lattner | f836e3f | 2007-10-07 01:33:16 +0000 | [diff] [blame] | 327 | std::string MultiKeywordSelector::getName() const { |
| 328 | std::string Result; |
| 329 | unsigned Length = 0; |
| 330 | for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) { |
| 331 | if (*I) |
| 332 | Length += (*I)->getLength(); |
| 333 | ++Length; // : |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 334 | } |
Chris Lattner | f836e3f | 2007-10-07 01:33:16 +0000 | [diff] [blame] | 335 | |
| 336 | Result.reserve(Length); |
| 337 | |
| 338 | for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) { |
| 339 | if (*I) |
| 340 | Result.insert(Result.end(), (*I)->getName(), |
| 341 | (*I)->getName()+(*I)->getLength()); |
| 342 | Result.push_back(':'); |
| 343 | } |
| 344 | |
| 345 | return Result; |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 348 | std::string Selector::getAsString() const { |
Ted Kremenek | f5ed396 | 2009-03-06 23:36:28 +0000 | [diff] [blame] | 349 | if (InfoPtr & ArgFlags) { |
| 350 | IdentifierInfo *II = getAsIdentifierInfo(); |
Chris Lattner | f836e3f | 2007-10-07 01:33:16 +0000 | [diff] [blame] | 351 | |
Ted Kremenek | 150ec29 | 2009-03-07 01:22:02 +0000 | [diff] [blame] | 352 | // If the number of arguments is 0 then II is guaranteed to not be null. |
Ted Kremenek | f5ed396 | 2009-03-06 23:36:28 +0000 | [diff] [blame] | 353 | if (getNumArgs() == 0) |
Ted Kremenek | 150ec29 | 2009-03-07 01:22:02 +0000 | [diff] [blame] | 354 | return II->getName(); |
Ted Kremenek | f5ed396 | 2009-03-06 23:36:28 +0000 | [diff] [blame] | 355 | |
| 356 | std::string Res = II ? II->getName() : ""; |
Chris Lattner | f836e3f | 2007-10-07 01:33:16 +0000 | [diff] [blame] | 357 | Res += ":"; |
| 358 | return Res; |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 359 | } |
Chris Lattner | f836e3f | 2007-10-07 01:33:16 +0000 | [diff] [blame] | 360 | |
| 361 | // We have a multiple keyword selector (no embedded flags). |
| 362 | return reinterpret_cast<MultiKeywordSelector *>(InfoPtr)->getName(); |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | |
Chris Lattner | 5f7d228 | 2009-03-04 05:35:38 +0000 | [diff] [blame] | 366 | namespace { |
| 367 | struct SelectorTableImpl { |
| 368 | llvm::FoldingSet<MultiKeywordSelector> Table; |
| 369 | llvm::BumpPtrAllocator Allocator; |
| 370 | }; |
| 371 | } // end anonymous namespace. |
| 372 | |
| 373 | static SelectorTableImpl &getSelectorTableImpl(void *P) { |
| 374 | return *static_cast<SelectorTableImpl*>(P); |
| 375 | } |
| 376 | |
| 377 | |
Chris Lattner | ff38491 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 378 | Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) { |
| 379 | if (nKeys < 2) |
| 380 | return Selector(IIV[0], nKeys); |
Chris Lattner | f836e3f | 2007-10-07 01:33:16 +0000 | [diff] [blame] | 381 | |
Chris Lattner | 5f7d228 | 2009-03-04 05:35:38 +0000 | [diff] [blame] | 382 | SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl); |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 383 | |
| 384 | // Unique selector, to guarantee there is one per name. |
| 385 | llvm::FoldingSetNodeID ID; |
| 386 | MultiKeywordSelector::Profile(ID, IIV, nKeys); |
| 387 | |
| 388 | void *InsertPos = 0; |
Chris Lattner | 5f7d228 | 2009-03-04 05:35:38 +0000 | [diff] [blame] | 389 | if (MultiKeywordSelector *SI = |
| 390 | SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 391 | return Selector(SI); |
Chris Lattner | f836e3f | 2007-10-07 01:33:16 +0000 | [diff] [blame] | 392 | |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 393 | // MultiKeywordSelector objects are not allocated with new because they have a |
| 394 | // variable size array (for parameter types) at the end of them. |
Chris Lattner | 5f7d228 | 2009-03-04 05:35:38 +0000 | [diff] [blame] | 395 | unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *); |
| 396 | MultiKeywordSelector *SI = |
| 397 | (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size, |
| 398 | llvm::alignof<MultiKeywordSelector>()); |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 399 | new (SI) MultiKeywordSelector(nKeys, IIV); |
Chris Lattner | 5f7d228 | 2009-03-04 05:35:38 +0000 | [diff] [blame] | 400 | SelTabImpl.Table.InsertNode(SI, InsertPos); |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 401 | return Selector(SI); |
| 402 | } |
| 403 | |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 404 | SelectorTable::SelectorTable() { |
Chris Lattner | 5f7d228 | 2009-03-04 05:35:38 +0000 | [diff] [blame] | 405 | Impl = new SelectorTableImpl(); |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | SelectorTable::~SelectorTable() { |
Chris Lattner | 5f7d228 | 2009-03-04 05:35:38 +0000 | [diff] [blame] | 409 | delete &getSelectorTableImpl(Impl); |
Steve Naroff | 29238a0 | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 410 | } |
| 411 | |