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