blob: c12fdef20305dc255a0728cc2fd84c27d170da70 [file] [log] [blame]
Chris Lattnerec659fc2006-10-29 22:09:44 +00001//===--- IdentifierTable.cpp - Hash table for identifier lookup -----------===//
Chris Lattner22eb9722006-06-18 05:43:12 +00002//
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//
Chris Lattnerc79f6fb2006-07-04 17:53:21 +000010// This file implements the IdentifierInfo, IdentifierVisitor, and
Chris Lattner91cbf112006-07-03 04:28:52 +000011// IdentifierTable interfaces.
Chris Lattner22eb9722006-06-18 05:43:12 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Lex/IdentifierTable.h"
16#include "clang/Lex/MacroInfo.h"
Chris Lattner25e0d542006-10-18 06:07:05 +000017#include "clang/Basic/LangOptions.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000018using namespace clang;
19
20//===----------------------------------------------------------------------===//
Chris Lattnerc79f6fb2006-07-04 17:53:21 +000021// IdentifierInfo Implementation
Chris Lattner22eb9722006-06-18 05:43:12 +000022//===----------------------------------------------------------------------===//
23
Chris Lattner3bc804e2006-10-28 23:46:24 +000024IdentifierInfo::IdentifierInfo() {
25 Macro = 0;
26 TokenID = tok::identifier;
27 PPID = tok::pp_not_keyword;
28 ObjCID = tok::objc_not_keyword;
Chris Lattner9561a0b2007-01-28 08:20:04 +000029 BuiltinID = 0;
Chris Lattner3bc804e2006-10-28 23:46:24 +000030 IsExtension = false;
31 IsPoisoned = false;
32 IsOtherTargetMacro = false;
Chris Lattner5b9f4892006-11-21 17:23:33 +000033 IsCPPOperatorKeyword = false;
Chris Lattner5e629292007-07-19 00:11:19 +000034 IsNonPortableBuiltin = false;
Chris Lattner3bc804e2006-10-28 23:46:24 +000035 FETokenInfo = 0;
36}
37
38IdentifierInfo::~IdentifierInfo() {
Chris Lattner22eb9722006-06-18 05:43:12 +000039 delete Macro;
40}
41
Chris Lattner91cbf112006-07-03 04:28:52 +000042//===----------------------------------------------------------------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +000043// IdentifierTable Implementation
44//===----------------------------------------------------------------------===//
45
Chris Lattner2b9e19b2006-10-29 23:43:13 +000046IdentifierTable::IdentifierTable(const LangOptions &LangOpts)
47 // Start with space for 8K identifiers.
48 : HashTable(8192) {
Chris Lattnerda933aa2006-10-29 23:49:15 +000049
Chris Lattnerf2e3ac32006-10-27 03:59:10 +000050 // Populate the identifier table with info about keywords for the current
51 // language.
Chris Lattner25e0d542006-10-18 06:07:05 +000052 AddKeywords(LangOpts);
Chris Lattner91cbf112006-07-03 04:28:52 +000053}
Chris Lattner22eb9722006-06-18 05:43:12 +000054
Chris Lattner25e0d542006-10-18 06:07:05 +000055//===----------------------------------------------------------------------===//
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 Lattner539007a2007-07-16 04:18:29 +000063/// 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.
Chris Lattner2b9e19b2006-10-29 23:43:13 +000067static void AddKeyword(const char *Keyword, unsigned KWLen,
68 tok::TokenKind TokenCode,
Chris Lattner539007a2007-07-16 04:18:29 +000069 int C90, int C99, int CXX, int CXX0x,
Chris Lattner25e0d542006-10-18 06:07:05 +000070 const LangOptions &LangOpts, IdentifierTable &Table) {
Chris Lattner539007a2007-07-16 04:18:29 +000071 int Flags = LangOpts.CPlusPlus ? (LangOpts.CPlusPlus0x? CXX0x : CXX)
72 : (LangOpts.C99 ? C99 : C90);
Chris Lattner25e0d542006-10-18 06:07:05 +000073
74 // Don't add this keyword if disabled in this language or if an extension
75 // and extensions are disabled.
76 if (Flags + LangOpts.NoExtensions >= 2) return;
77
Chris Lattner2b9e19b2006-10-29 23:43:13 +000078 IdentifierInfo &Info = Table.get(Keyword, Keyword+KWLen);
Chris Lattner25e0d542006-10-18 06:07:05 +000079 Info.setTokenID(TokenCode);
80 Info.setIsExtensionToken(Flags == 1);
81}
82
Chris Lattnere8b27242007-06-08 17:27:55 +000083static void AddAlias(const char *Keyword, unsigned KWLen,
84 const char *AliaseeKeyword, unsigned AliaseeKWLen,
85 const LangOptions &LangOpts, IdentifierTable &Table) {
86 IdentifierInfo &AliasInfo = Table.get(Keyword, Keyword+KWLen);
87 IdentifierInfo &AliaseeInfo = Table.get(AliaseeKeyword,
88 AliaseeKeyword+AliaseeKWLen);
89 AliasInfo.setTokenID(AliaseeInfo.getTokenID());
90 AliasInfo.setIsExtensionToken(AliaseeInfo.isExtensionToken());
91}
92
Chris Lattner25e0d542006-10-18 06:07:05 +000093/// AddPPKeyword - Register a preprocessor keyword like "define" "undef" or
94/// "elif".
95static void AddPPKeyword(tok::PPKeywordKind PPID,
96 const char *Name, unsigned NameLen,
97 IdentifierTable &Table) {
98 Table.get(Name, Name+NameLen).setPPKeywordID(PPID);
99}
100
Chris Lattner5b9f4892006-11-21 17:23:33 +0000101/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
102/// representations.
103static void AddCXXOperatorKeyword(const char *Keyword, unsigned KWLen,
104 tok::TokenKind TokenCode,
105 IdentifierTable &Table) {
106 IdentifierInfo &Info = Table.get(Keyword, Keyword + KWLen);
107 Info.setTokenID(TokenCode);
108 Info.setIsCPlusplusOperatorKeyword();
109}
110
Chris Lattner25e0d542006-10-18 06:07:05 +0000111/// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or
112/// "property".
113static void AddObjCKeyword(tok::ObjCKeywordKind ObjCID,
114 const char *Name, unsigned NameLen,
115 IdentifierTable &Table) {
116 Table.get(Name, Name+NameLen).setObjCKeywordID(ObjCID);
117}
118
119/// AddKeywords - Add all keywords to the symbol table.
120///
121void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
122 enum {
123 C90Shift = 0,
124 EXTC90 = 1 << C90Shift,
125 NOTC90 = 2 << C90Shift,
126 C99Shift = 2,
127 EXTC99 = 1 << C99Shift,
128 NOTC99 = 2 << C99Shift,
129 CPPShift = 4,
130 EXTCPP = 1 << CPPShift,
131 NOTCPP = 2 << CPPShift,
Chris Lattner539007a2007-07-16 04:18:29 +0000132 CPP0xShift = 6,
133 EXTCPP0x = 1 << CPP0xShift,
134 NOTCPP0x = 2 << CPP0xShift,
Chris Lattner25e0d542006-10-18 06:07:05 +0000135 Mask = 3
136 };
137
138 // Add keywords and tokens for the current language.
139#define KEYWORD(NAME, FLAGS) \
Chris Lattner2b9e19b2006-10-29 23:43:13 +0000140 AddKeyword(#NAME, strlen(#NAME), tok::kw_ ## NAME, \
Chris Lattner25e0d542006-10-18 06:07:05 +0000141 ((FLAGS) >> C90Shift) & Mask, \
142 ((FLAGS) >> C99Shift) & Mask, \
Chris Lattner539007a2007-07-16 04:18:29 +0000143 ((FLAGS) >> CPPShift) & Mask, \
144 ((FLAGS) >> CPP0xShift) & Mask, LangOpts, *this);
Chris Lattner25e0d542006-10-18 06:07:05 +0000145#define ALIAS(NAME, TOK) \
Chris Lattnere8b27242007-06-08 17:27:55 +0000146 AddAlias(NAME, strlen(NAME), #TOK, strlen(#TOK), LangOpts, *this);
Chris Lattner25e0d542006-10-18 06:07:05 +0000147#define PPKEYWORD(NAME) \
148 AddPPKeyword(tok::pp_##NAME, #NAME, strlen(#NAME), *this);
Chris Lattner5b9f4892006-11-21 17:23:33 +0000149#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
Chris Lattner3e7592e2006-12-04 07:48:37 +0000150 if (LangOpts.CXXOperatorNames) \
Chris Lattner5b9f4892006-11-21 17:23:33 +0000151 AddCXXOperatorKeyword(#NAME, strlen(#NAME), tok::ALIAS, *this);
Chris Lattner25e0d542006-10-18 06:07:05 +0000152#define OBJC1_AT_KEYWORD(NAME) \
153 if (LangOpts.ObjC1) \
154 AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this);
155#define OBJC2_AT_KEYWORD(NAME) \
156 if (LangOpts.ObjC2) \
157 AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this);
158#include "clang/Basic/TokenKinds.def"
159}
160
161
162//===----------------------------------------------------------------------===//
163// Stats Implementation
164//===----------------------------------------------------------------------===//
165
Chris Lattner22eb9722006-06-18 05:43:12 +0000166/// PrintStats - Print statistics about how well the identifier table is doing
167/// at hashing identifiers.
168void IdentifierTable::PrintStats() const {
Chris Lattner2b9e19b2006-10-29 23:43:13 +0000169 unsigned NumBuckets = HashTable.getNumBuckets();
170 unsigned NumIdentifiers = HashTable.getNumItems();
171 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
Chris Lattner22eb9722006-06-18 05:43:12 +0000172 unsigned AverageIdentifierSize = 0;
173 unsigned MaxIdentifierLength = 0;
174
Chris Lattner2b9e19b2006-10-29 23:43:13 +0000175 // TODO: Figure out maximum times an identifier had to probe for -stats.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000176 for (llvm::StringMap<IdentifierInfo, llvm::BumpPtrAllocator>::const_iterator
Chris Lattnerb055f2d2007-02-11 08:19:57 +0000177 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
178 unsigned IdLen = I->getKeyLength();
179 AverageIdentifierSize += IdLen;
180 if (MaxIdentifierLength < IdLen)
181 MaxIdentifierLength = IdLen;
182 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000183
Chris Lattner23b7eb62007-06-15 23:05:46 +0000184 fprintf(stderr, "\n*** Identifier Table Stats:\n");
185 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
186 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
187 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
188 NumIdentifiers/(double)NumBuckets);
189 fprintf(stderr, "Ave identifier length: %f\n",
190 (AverageIdentifierSize/(double)NumIdentifiers));
191 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Chris Lattner22eb9722006-06-18 05:43:12 +0000192
193 // Compute statistics about the memory allocated for identifiers.
Chris Lattner2b9e19b2006-10-29 23:43:13 +0000194 HashTable.getAllocator().PrintStats();
Chris Lattner22eb9722006-06-18 05:43:12 +0000195}