blob: e671af9839149de6b8d590dd6f7a6cb5f8e22a7f [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- IdentifierTable.cpp - Hash table for identifier lookup -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the IdentifierInfo, IdentifierVisitor, and
11// IdentifierTable interfaces.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Lex/IdentifierTable.h"
16#include "clang/Lex/MacroInfo.h"
17#include "clang/Basic/LangOptions.h"
18using namespace clang;
19
20//===----------------------------------------------------------------------===//
21// IdentifierInfo Implementation
22//===----------------------------------------------------------------------===//
23
24IdentifierInfo::IdentifierInfo() {
25 Macro = 0;
26 TokenID = tok::identifier;
27 PPID = tok::pp_not_keyword;
28 ObjCID = tok::objc_not_keyword;
29 BuiltinID = 0;
30 IsExtension = false;
31 IsPoisoned = false;
32 IsOtherTargetMacro = false;
33 IsCPPOperatorKeyword = false;
34 FETokenInfo = 0;
35}
36
37IdentifierInfo::~IdentifierInfo() {
38 delete Macro;
39}
40
41//===----------------------------------------------------------------------===//
42// IdentifierTable Implementation
43//===----------------------------------------------------------------------===//
44
45IdentifierTable::IdentifierTable(const LangOptions &LangOpts)
46 // Start with space for 8K identifiers.
47 : HashTable(8192) {
48
49 // Populate the identifier table with info about keywords for the current
50 // language.
51 AddKeywords(LangOpts);
52}
53
54//===----------------------------------------------------------------------===//
55// Language Keyword Implementation
56//===----------------------------------------------------------------------===//
57
58/// AddKeyword - This method is used to associate a token ID with specific
59/// identifiers because they are language keywords. This causes the lexer to
60/// automatically map matching identifiers to specialized token codes.
61///
62/// The C90/C99/CPP flags are set to 0 if the token should be enabled in the
63/// specified langauge, set to 1 if it is an extension in the specified
64/// language, and set to 2 if disabled in the specified language.
65static void AddKeyword(const char *Keyword, unsigned KWLen,
66 tok::TokenKind TokenCode,
67 int C90, int C99, int CXX,
68 const LangOptions &LangOpts, IdentifierTable &Table) {
69 int Flags = LangOpts.CPlusPlus ? CXX : (LangOpts.C99 ? C99 : C90);
70
71 // Don't add this keyword if disabled in this language or if an extension
72 // and extensions are disabled.
73 if (Flags + LangOpts.NoExtensions >= 2) return;
74
75 IdentifierInfo &Info = Table.get(Keyword, Keyword+KWLen);
76 Info.setTokenID(TokenCode);
77 Info.setIsExtensionToken(Flags == 1);
78}
79
80static void AddAlias(const char *Keyword, unsigned KWLen,
81 const char *AliaseeKeyword, unsigned AliaseeKWLen,
82 const LangOptions &LangOpts, IdentifierTable &Table) {
83 IdentifierInfo &AliasInfo = Table.get(Keyword, Keyword+KWLen);
84 IdentifierInfo &AliaseeInfo = Table.get(AliaseeKeyword,
85 AliaseeKeyword+AliaseeKWLen);
86 AliasInfo.setTokenID(AliaseeInfo.getTokenID());
87 AliasInfo.setIsExtensionToken(AliaseeInfo.isExtensionToken());
88}
89
90/// AddPPKeyword - Register a preprocessor keyword like "define" "undef" or
91/// "elif".
92static void AddPPKeyword(tok::PPKeywordKind PPID,
93 const char *Name, unsigned NameLen,
94 IdentifierTable &Table) {
95 Table.get(Name, Name+NameLen).setPPKeywordID(PPID);
96}
97
98/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
99/// representations.
100static void AddCXXOperatorKeyword(const char *Keyword, unsigned KWLen,
101 tok::TokenKind TokenCode,
102 IdentifierTable &Table) {
103 IdentifierInfo &Info = Table.get(Keyword, Keyword + KWLen);
104 Info.setTokenID(TokenCode);
105 Info.setIsCPlusplusOperatorKeyword();
106}
107
108/// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or
109/// "property".
110static void AddObjCKeyword(tok::ObjCKeywordKind ObjCID,
111 const char *Name, unsigned NameLen,
112 IdentifierTable &Table) {
113 Table.get(Name, Name+NameLen).setObjCKeywordID(ObjCID);
114}
115
116/// AddKeywords - Add all keywords to the symbol table.
117///
118void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
119 enum {
120 C90Shift = 0,
121 EXTC90 = 1 << C90Shift,
122 NOTC90 = 2 << C90Shift,
123 C99Shift = 2,
124 EXTC99 = 1 << C99Shift,
125 NOTC99 = 2 << C99Shift,
126 CPPShift = 4,
127 EXTCPP = 1 << CPPShift,
128 NOTCPP = 2 << CPPShift,
129 Mask = 3
130 };
131
132 // Add keywords and tokens for the current language.
133#define KEYWORD(NAME, FLAGS) \
134 AddKeyword(#NAME, strlen(#NAME), tok::kw_ ## NAME, \
135 ((FLAGS) >> C90Shift) & Mask, \
136 ((FLAGS) >> C99Shift) & Mask, \
137 ((FLAGS) >> CPPShift) & Mask, LangOpts, *this);
138#define ALIAS(NAME, TOK) \
139 AddAlias(NAME, strlen(NAME), #TOK, strlen(#TOK), LangOpts, *this);
140#define PPKEYWORD(NAME) \
141 AddPPKeyword(tok::pp_##NAME, #NAME, strlen(#NAME), *this);
142#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
143 if (LangOpts.CXXOperatorNames) \
144 AddCXXOperatorKeyword(#NAME, strlen(#NAME), tok::ALIAS, *this);
145#define OBJC1_AT_KEYWORD(NAME) \
146 if (LangOpts.ObjC1) \
147 AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this);
148#define OBJC2_AT_KEYWORD(NAME) \
149 if (LangOpts.ObjC2) \
150 AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this);
151#include "clang/Basic/TokenKinds.def"
152}
153
154
155//===----------------------------------------------------------------------===//
156// Stats Implementation
157//===----------------------------------------------------------------------===//
158
159/// PrintStats - Print statistics about how well the identifier table is doing
160/// at hashing identifiers.
161void IdentifierTable::PrintStats() const {
162 unsigned NumBuckets = HashTable.getNumBuckets();
163 unsigned NumIdentifiers = HashTable.getNumItems();
164 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
165 unsigned AverageIdentifierSize = 0;
166 unsigned MaxIdentifierLength = 0;
167
168 // TODO: Figure out maximum times an identifier had to probe for -stats.
169 for (llvm::StringMap<IdentifierInfo, llvm::BumpPtrAllocator>::const_iterator
170 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
171 unsigned IdLen = I->getKeyLength();
172 AverageIdentifierSize += IdLen;
173 if (MaxIdentifierLength < IdLen)
174 MaxIdentifierLength = IdLen;
175 }
176
177 fprintf(stderr, "\n*** Identifier Table Stats:\n");
178 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
179 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
180 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
181 NumIdentifiers/(double)NumBuckets);
182 fprintf(stderr, "Ave identifier length: %f\n",
183 (AverageIdentifierSize/(double)NumIdentifiers));
184 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
185
186 // Compute statistics about the memory allocated for identifiers.
187 HashTable.getAllocator().PrintStats();
188}