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