clang API Documentation
00001 //===--- IdentifierTable.h - Hash table for identifier lookup ---*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the IdentifierInfo, IdentifierTable, and Selector 00011 // interfaces. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_CLANG_BASIC_IDENTIFIERTABLE_H 00016 #define LLVM_CLANG_BASIC_IDENTIFIERTABLE_H 00017 00018 #include "clang/Basic/OperatorKinds.h" 00019 #include "clang/Basic/TokenKinds.h" 00020 #include "clang/Basic/LLVM.h" 00021 #include "llvm/ADT/StringMap.h" 00022 #include "llvm/ADT/StringRef.h" 00023 #include "llvm/ADT/OwningPtr.h" 00024 #include "llvm/Support/PointerLikeTypeTraits.h" 00025 #include <cassert> 00026 #include <string> 00027 00028 namespace llvm { 00029 template <typename T> struct DenseMapInfo; 00030 } 00031 00032 namespace clang { 00033 class LangOptions; 00034 class IdentifierInfo; 00035 class IdentifierTable; 00036 class SourceLocation; 00037 class MultiKeywordSelector; // private class used by Selector 00038 class DeclarationName; // AST class that stores declaration names 00039 00040 /// IdentifierLocPair - A simple pair of identifier info and location. 00041 typedef std::pair<IdentifierInfo*, SourceLocation> IdentifierLocPair; 00042 00043 00044 /// IdentifierInfo - One of these records is kept for each identifier that 00045 /// is lexed. This contains information about whether the token was #define'd, 00046 /// is a language keyword, or if it is a front-end token of some sort (e.g. a 00047 /// variable or function name). The preprocessor keeps this information in a 00048 /// set, and all tok::identifier tokens have a pointer to one of these. 00049 class IdentifierInfo { 00050 unsigned TokenID : 9; // Front-end token ID or tok::identifier. 00051 // Objective-C keyword ('protocol' in '@protocol') or builtin (__builtin_inf). 00052 // First NUM_OBJC_KEYWORDS values are for Objective-C, the remaining values 00053 // are for builtins. 00054 unsigned ObjCOrBuiltinID :11; 00055 bool HasMacro : 1; // True if there is a #define for this. 00056 bool IsExtension : 1; // True if identifier is a lang extension. 00057 bool IsCXX11CompatKeyword : 1; // True if identifier is a keyword in C++11. 00058 bool IsPoisoned : 1; // True if identifier is poisoned. 00059 bool IsCPPOperatorKeyword : 1; // True if ident is a C++ operator keyword. 00060 bool NeedsHandleIdentifier : 1; // See "RecomputeNeedsHandleIdentifier". 00061 bool IsFromAST : 1; // True if identifier was loaded (at least 00062 // partially) from an AST file. 00063 bool ChangedAfterLoad : 1; // True if identifier has changed from the 00064 // definition loaded from an AST file. 00065 bool RevertedTokenID : 1; // True if RevertTokenIDToIdentifier was 00066 // called. 00067 bool OutOfDate : 1; // True if there may be additional 00068 // information about this identifier 00069 // stored externally. 00070 bool IsModulesImport : 1; // True if this is the 'import' contextual 00071 // keyword. 00072 // 1 bit left in 32-bit word. 00073 00074 void *FETokenInfo; // Managed by the language front-end. 00075 llvm::StringMapEntry<IdentifierInfo*> *Entry; 00076 00077 IdentifierInfo(const IdentifierInfo&); // NONCOPYABLE. 00078 void operator=(const IdentifierInfo&); // NONASSIGNABLE. 00079 00080 friend class IdentifierTable; 00081 00082 public: 00083 IdentifierInfo(); 00084 00085 00086 /// isStr - Return true if this is the identifier for the specified string. 00087 /// This is intended to be used for string literals only: II->isStr("foo"). 00088 template <std::size_t StrLen> 00089 bool isStr(const char (&Str)[StrLen]) const { 00090 return getLength() == StrLen-1 && !memcmp(getNameStart(), Str, StrLen-1); 00091 } 00092 00093 /// getNameStart - Return the beginning of the actual string for this 00094 /// identifier. The returned string is properly null terminated. 00095 /// 00096 const char *getNameStart() const { 00097 if (Entry) return Entry->getKeyData(); 00098 // FIXME: This is gross. It would be best not to embed specific details 00099 // of the PTH file format here. 00100 // The 'this' pointer really points to a 00101 // std::pair<IdentifierInfo, const char*>, where internal pointer 00102 // points to the external string data. 00103 typedef std::pair<IdentifierInfo, const char*> actualtype; 00104 return ((const actualtype*) this)->second; 00105 } 00106 00107 /// getLength - Efficiently return the length of this identifier info. 00108 /// 00109 unsigned getLength() const { 00110 if (Entry) return Entry->getKeyLength(); 00111 // FIXME: This is gross. It would be best not to embed specific details 00112 // of the PTH file format here. 00113 // The 'this' pointer really points to a 00114 // std::pair<IdentifierInfo, const char*>, where internal pointer 00115 // points to the external string data. 00116 typedef std::pair<IdentifierInfo, const char*> actualtype; 00117 const char* p = ((const actualtype*) this)->second - 2; 00118 return (((unsigned) p[0]) | (((unsigned) p[1]) << 8)) - 1; 00119 } 00120 00121 /// getName - Return the actual identifier string. 00122 StringRef getName() const { 00123 return StringRef(getNameStart(), getLength()); 00124 } 00125 00126 /// hasMacroDefinition - Return true if this identifier is #defined to some 00127 /// other value. 00128 bool hasMacroDefinition() const { 00129 return HasMacro; 00130 } 00131 void setHasMacroDefinition(bool Val) { 00132 if (HasMacro == Val) return; 00133 00134 HasMacro = Val; 00135 if (Val) 00136 NeedsHandleIdentifier = 1; 00137 else 00138 RecomputeNeedsHandleIdentifier(); 00139 } 00140 00141 /// getTokenID - If this is a source-language token (e.g. 'for'), this API 00142 /// can be used to cause the lexer to map identifiers to source-language 00143 /// tokens. 00144 tok::TokenKind getTokenID() const { return (tok::TokenKind)TokenID; } 00145 00146 /// \brief True if RevertTokenIDToIdentifier() was called. 00147 bool hasRevertedTokenIDToIdentifier() const { return RevertedTokenID; } 00148 00149 /// \brief Revert TokenID to tok::identifier; used for GNU libstdc++ 4.2 00150 /// compatibility. 00151 /// 00152 /// TokenID is normally read-only but there are 2 instances where we revert it 00153 /// to tok::identifier for libstdc++ 4.2. Keep track of when this happens 00154 /// using this method so we can inform serialization about it. 00155 void RevertTokenIDToIdentifier() { 00156 assert(TokenID != tok::identifier && "Already at tok::identifier"); 00157 TokenID = tok::identifier; 00158 RevertedTokenID = true; 00159 } 00160 00161 /// getPPKeywordID - Return the preprocessor keyword ID for this identifier. 00162 /// For example, "define" will return tok::pp_define. 00163 tok::PPKeywordKind getPPKeywordID() const; 00164 00165 /// getObjCKeywordID - Return the Objective-C keyword ID for the this 00166 /// identifier. For example, 'class' will return tok::objc_class if ObjC is 00167 /// enabled. 00168 tok::ObjCKeywordKind getObjCKeywordID() const { 00169 if (ObjCOrBuiltinID < tok::NUM_OBJC_KEYWORDS) 00170 return tok::ObjCKeywordKind(ObjCOrBuiltinID); 00171 else 00172 return tok::objc_not_keyword; 00173 } 00174 void setObjCKeywordID(tok::ObjCKeywordKind ID) { ObjCOrBuiltinID = ID; } 00175 00176 /// getBuiltinID - Return a value indicating whether this is a builtin 00177 /// function. 0 is not-built-in. 1 is builtin-for-some-nonprimary-target. 00178 /// 2+ are specific builtin functions. 00179 unsigned getBuiltinID() const { 00180 if (ObjCOrBuiltinID >= tok::NUM_OBJC_KEYWORDS) 00181 return ObjCOrBuiltinID - tok::NUM_OBJC_KEYWORDS; 00182 else 00183 return 0; 00184 } 00185 void setBuiltinID(unsigned ID) { 00186 ObjCOrBuiltinID = ID + tok::NUM_OBJC_KEYWORDS; 00187 assert(ObjCOrBuiltinID - unsigned(tok::NUM_OBJC_KEYWORDS) == ID 00188 && "ID too large for field!"); 00189 } 00190 00191 unsigned getObjCOrBuiltinID() const { return ObjCOrBuiltinID; } 00192 void setObjCOrBuiltinID(unsigned ID) { ObjCOrBuiltinID = ID; } 00193 00194 /// get/setExtension - Initialize information about whether or not this 00195 /// language token is an extension. This controls extension warnings, and is 00196 /// only valid if a custom token ID is set. 00197 bool isExtensionToken() const { return IsExtension; } 00198 void setIsExtensionToken(bool Val) { 00199 IsExtension = Val; 00200 if (Val) 00201 NeedsHandleIdentifier = 1; 00202 else 00203 RecomputeNeedsHandleIdentifier(); 00204 } 00205 00206 /// is/setIsCXX11CompatKeyword - Initialize information about whether or not 00207 /// this language token is a keyword in C++11. This controls compatibility 00208 /// warnings, and is only true when not parsing C++11. Once a compatibility 00209 /// problem has been diagnosed with this keyword, the flag will be cleared. 00210 bool isCXX11CompatKeyword() const { return IsCXX11CompatKeyword; } 00211 void setIsCXX11CompatKeyword(bool Val) { 00212 IsCXX11CompatKeyword = Val; 00213 if (Val) 00214 NeedsHandleIdentifier = 1; 00215 else 00216 RecomputeNeedsHandleIdentifier(); 00217 } 00218 00219 /// setIsPoisoned - Mark this identifier as poisoned. After poisoning, the 00220 /// Preprocessor will emit an error every time this token is used. 00221 void setIsPoisoned(bool Value = true) { 00222 IsPoisoned = Value; 00223 if (Value) 00224 NeedsHandleIdentifier = 1; 00225 else 00226 RecomputeNeedsHandleIdentifier(); 00227 } 00228 00229 /// isPoisoned - Return true if this token has been poisoned. 00230 bool isPoisoned() const { return IsPoisoned; } 00231 00232 /// isCPlusPlusOperatorKeyword/setIsCPlusPlusOperatorKeyword controls whether 00233 /// this identifier is a C++ alternate representation of an operator. 00234 void setIsCPlusPlusOperatorKeyword(bool Val = true) { 00235 IsCPPOperatorKeyword = Val; 00236 if (Val) 00237 NeedsHandleIdentifier = 1; 00238 else 00239 RecomputeNeedsHandleIdentifier(); 00240 } 00241 bool isCPlusPlusOperatorKeyword() const { return IsCPPOperatorKeyword; } 00242 00243 /// getFETokenInfo/setFETokenInfo - The language front-end is allowed to 00244 /// associate arbitrary metadata with this token. 00245 template<typename T> 00246 T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); } 00247 void setFETokenInfo(void *T) { FETokenInfo = T; } 00248 00249 /// isHandleIdentifierCase - Return true if the Preprocessor::HandleIdentifier 00250 /// must be called on a token of this identifier. If this returns false, we 00251 /// know that HandleIdentifier will not affect the token. 00252 bool isHandleIdentifierCase() const { return NeedsHandleIdentifier; } 00253 00254 /// isFromAST - Return true if the identifier in its current state was loaded 00255 /// from an AST file. 00256 bool isFromAST() const { return IsFromAST; } 00257 00258 void setIsFromAST() { IsFromAST = true; } 00259 00260 /// \brief Determine whether this identifier has changed since it was loaded 00261 /// from an AST file. 00262 bool hasChangedSinceDeserialization() const { 00263 return ChangedAfterLoad; 00264 } 00265 00266 /// \brief Note that this identifier has changed since it was loaded from 00267 /// an AST file. 00268 void setChangedSinceDeserialization() { 00269 ChangedAfterLoad = true; 00270 } 00271 00272 /// \brief Determine whether the information for this identifier is out of 00273 /// date with respect to the external source. 00274 bool isOutOfDate() const { return OutOfDate; } 00275 00276 /// \brief Set whether the information for this identifier is out of 00277 /// date with respect to the external source. 00278 void setOutOfDate(bool OOD) { 00279 OutOfDate = OOD; 00280 if (OOD) 00281 NeedsHandleIdentifier = true; 00282 else 00283 RecomputeNeedsHandleIdentifier(); 00284 } 00285 00286 /// \brief Determine whether this is the contextual keyword 00287 /// '__experimental_modules_import'. 00288 bool isModulesImport() const { return IsModulesImport; } 00289 00290 /// \brief Set whether this identifier is the contextual keyword 00291 /// '__experimental_modules_import'. 00292 void setModulesImport(bool I) { 00293 IsModulesImport = I; 00294 if (I) 00295 NeedsHandleIdentifier = true; 00296 else 00297 RecomputeNeedsHandleIdentifier(); 00298 } 00299 00300 private: 00301 /// RecomputeNeedsHandleIdentifier - The Preprocessor::HandleIdentifier does 00302 /// several special (but rare) things to identifiers of various sorts. For 00303 /// example, it changes the "for" keyword token from tok::identifier to 00304 /// tok::for. 00305 /// 00306 /// This method is very tied to the definition of HandleIdentifier. Any 00307 /// change to it should be reflected here. 00308 void RecomputeNeedsHandleIdentifier() { 00309 NeedsHandleIdentifier = 00310 (isPoisoned() | hasMacroDefinition() | isCPlusPlusOperatorKeyword() | 00311 isExtensionToken() | isCXX11CompatKeyword() || isOutOfDate() || 00312 isModulesImport()); 00313 } 00314 }; 00315 00316 /// \brief an RAII object for [un]poisoning an identifier 00317 /// within a certain scope. II is allowed to be null, in 00318 /// which case, objects of this type have no effect. 00319 class PoisonIdentifierRAIIObject { 00320 IdentifierInfo *const II; 00321 const bool OldValue; 00322 public: 00323 PoisonIdentifierRAIIObject(IdentifierInfo *II, bool NewValue) 00324 : II(II), OldValue(II ? II->isPoisoned() : false) { 00325 if(II) 00326 II->setIsPoisoned(NewValue); 00327 } 00328 00329 ~PoisonIdentifierRAIIObject() { 00330 if(II) 00331 II->setIsPoisoned(OldValue); 00332 } 00333 }; 00334 00335 /// \brief An iterator that walks over all of the known identifiers 00336 /// in the lookup table. 00337 /// 00338 /// Since this iterator uses an abstract interface via virtual 00339 /// functions, it uses an object-oriented interface rather than the 00340 /// more standard C++ STL iterator interface. In this OO-style 00341 /// iteration, the single function \c Next() provides dereference, 00342 /// advance, and end-of-sequence checking in a single 00343 /// operation. Subclasses of this iterator type will provide the 00344 /// actual functionality. 00345 class IdentifierIterator { 00346 private: 00347 IdentifierIterator(const IdentifierIterator&); // Do not implement 00348 IdentifierIterator &operator=(const IdentifierIterator&); // Do not implement 00349 00350 protected: 00351 IdentifierIterator() { } 00352 00353 public: 00354 virtual ~IdentifierIterator(); 00355 00356 /// \brief Retrieve the next string in the identifier table and 00357 /// advances the iterator for the following string. 00358 /// 00359 /// \returns The next string in the identifier table. If there is 00360 /// no such string, returns an empty \c StringRef. 00361 virtual StringRef Next() = 0; 00362 }; 00363 00364 /// IdentifierInfoLookup - An abstract class used by IdentifierTable that 00365 /// provides an interface for performing lookups from strings 00366 /// (const char *) to IdentiferInfo objects. 00367 class IdentifierInfoLookup { 00368 public: 00369 virtual ~IdentifierInfoLookup(); 00370 00371 /// get - Return the identifier token info for the specified named identifier. 00372 /// Unlike the version in IdentifierTable, this returns a pointer instead 00373 /// of a reference. If the pointer is NULL then the IdentifierInfo cannot 00374 /// be found. 00375 virtual IdentifierInfo* get(StringRef Name) = 0; 00376 00377 /// \brief Retrieve an iterator into the set of all identifiers 00378 /// known to this identifier lookup source. 00379 /// 00380 /// This routine provides access to all of the identifiers known to 00381 /// the identifier lookup, allowing access to the contents of the 00382 /// identifiers without introducing the overhead of constructing 00383 /// IdentifierInfo objects for each. 00384 /// 00385 /// \returns A new iterator into the set of known identifiers. The 00386 /// caller is responsible for deleting this iterator. 00387 virtual IdentifierIterator *getIdentifiers() const; 00388 }; 00389 00390 /// \brief An abstract class used to resolve numerical identifier 00391 /// references (meaningful only to some external source) into 00392 /// IdentifierInfo pointers. 00393 class ExternalIdentifierLookup { 00394 public: 00395 virtual ~ExternalIdentifierLookup(); 00396 00397 /// \brief Return the identifier associated with the given ID number. 00398 /// 00399 /// The ID 0 is associated with the NULL identifier. 00400 virtual IdentifierInfo *GetIdentifier(unsigned ID) = 0; 00401 }; 00402 00403 /// IdentifierTable - This table implements an efficient mapping from strings to 00404 /// IdentifierInfo nodes. It has no other purpose, but this is an 00405 /// extremely performance-critical piece of the code, as each occurrence of 00406 /// every identifier goes through here when lexed. 00407 class IdentifierTable { 00408 // Shark shows that using MallocAllocator is *much* slower than using this 00409 // BumpPtrAllocator! 00410 typedef llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator> HashTableTy; 00411 HashTableTy HashTable; 00412 00413 IdentifierInfoLookup* ExternalLookup; 00414 00415 public: 00416 /// IdentifierTable ctor - Create the identifier table, populating it with 00417 /// info about the language keywords for the language specified by LangOpts. 00418 IdentifierTable(const LangOptions &LangOpts, 00419 IdentifierInfoLookup* externalLookup = 0); 00420 00421 /// \brief Set the external identifier lookup mechanism. 00422 void setExternalIdentifierLookup(IdentifierInfoLookup *IILookup) { 00423 ExternalLookup = IILookup; 00424 } 00425 00426 /// \brief Retrieve the external identifier lookup object, if any. 00427 IdentifierInfoLookup *getExternalIdentifierLookup() const { 00428 return ExternalLookup; 00429 } 00430 00431 llvm::BumpPtrAllocator& getAllocator() { 00432 return HashTable.getAllocator(); 00433 } 00434 00435 /// get - Return the identifier token info for the specified named identifier. 00436 /// 00437 IdentifierInfo &get(StringRef Name) { 00438 llvm::StringMapEntry<IdentifierInfo*> &Entry = 00439 HashTable.GetOrCreateValue(Name); 00440 00441 IdentifierInfo *II = Entry.getValue(); 00442 if (II) return *II; 00443 00444 // No entry; if we have an external lookup, look there first. 00445 if (ExternalLookup) { 00446 II = ExternalLookup->get(Name); 00447 if (II) { 00448 // Cache in the StringMap for subsequent lookups. 00449 Entry.setValue(II); 00450 return *II; 00451 } 00452 } 00453 00454 // Lookups failed, make a new IdentifierInfo. 00455 void *Mem = getAllocator().Allocate<IdentifierInfo>(); 00456 II = new (Mem) IdentifierInfo(); 00457 Entry.setValue(II); 00458 00459 // Make sure getName() knows how to find the IdentifierInfo 00460 // contents. 00461 II->Entry = &Entry; 00462 00463 return *II; 00464 } 00465 00466 IdentifierInfo &get(StringRef Name, tok::TokenKind TokenCode) { 00467 IdentifierInfo &II = get(Name); 00468 II.TokenID = TokenCode; 00469 assert(II.TokenID == (unsigned) TokenCode && "TokenCode too large"); 00470 return II; 00471 } 00472 00473 /// \brief Gets an IdentifierInfo for the given name without consulting 00474 /// external sources. 00475 /// 00476 /// This is a version of get() meant for external sources that want to 00477 /// introduce or modify an identifier. If they called get(), they would 00478 /// likely end up in a recursion. 00479 IdentifierInfo &getOwn(StringRef Name) { 00480 llvm::StringMapEntry<IdentifierInfo*> &Entry = 00481 HashTable.GetOrCreateValue(Name); 00482 00483 IdentifierInfo *II = Entry.getValue(); 00484 if (!II) { 00485 00486 // Lookups failed, make a new IdentifierInfo. 00487 void *Mem = getAllocator().Allocate<IdentifierInfo>(); 00488 II = new (Mem) IdentifierInfo(); 00489 Entry.setValue(II); 00490 00491 // Make sure getName() knows how to find the IdentifierInfo 00492 // contents. 00493 II->Entry = &Entry; 00494 00495 // If this is the 'import' contextual keyword, mark it as such. 00496 if (Name.equals("import")) 00497 II->setModulesImport(true); 00498 } 00499 00500 return *II; 00501 } 00502 00503 typedef HashTableTy::const_iterator iterator; 00504 typedef HashTableTy::const_iterator const_iterator; 00505 00506 iterator begin() const { return HashTable.begin(); } 00507 iterator end() const { return HashTable.end(); } 00508 unsigned size() const { return HashTable.size(); } 00509 00510 /// PrintStats - Print some statistics to stderr that indicate how well the 00511 /// hashing is doing. 00512 void PrintStats() const; 00513 00514 void AddKeywords(const LangOptions &LangOpts); 00515 }; 00516 00517 /// ObjCMethodFamily - A family of Objective-C methods. These 00518 /// families have no inherent meaning in the language, but are 00519 /// nonetheless central enough in the existing implementations to 00520 /// merit direct AST support. While, in theory, arbitrary methods can 00521 /// be considered to form families, we focus here on the methods 00522 /// involving allocation and retain-count management, as these are the 00523 /// most "core" and the most likely to be useful to diverse clients 00524 /// without extra information. 00525 /// 00526 /// Both selectors and actual method declarations may be classified 00527 /// into families. Method families may impose additional restrictions 00528 /// beyond their selector name; for example, a method called '_init' 00529 /// that returns void is not considered to be in the 'init' family 00530 /// (but would be if it returned 'id'). It is also possible to 00531 /// explicitly change or remove a method's family. Therefore the 00532 /// method's family should be considered the single source of truth. 00533 enum ObjCMethodFamily { 00534 /// \brief No particular method family. 00535 OMF_None, 00536 00537 // Selectors in these families may have arbitrary arity, may be 00538 // written with arbitrary leading underscores, and may have 00539 // additional CamelCase "words" in their first selector chunk 00540 // following the family name. 00541 OMF_alloc, 00542 OMF_copy, 00543 OMF_init, 00544 OMF_mutableCopy, 00545 OMF_new, 00546 00547 // These families are singletons consisting only of the nullary 00548 // selector with the given name. 00549 OMF_autorelease, 00550 OMF_dealloc, 00551 OMF_finalize, 00552 OMF_release, 00553 OMF_retain, 00554 OMF_retainCount, 00555 OMF_self, 00556 00557 // performSelector families 00558 OMF_performSelector 00559 }; 00560 00561 /// Enough bits to store any enumerator in ObjCMethodFamily or 00562 /// InvalidObjCMethodFamily. 00563 enum { ObjCMethodFamilyBitWidth = 4 }; 00564 00565 /// An invalid value of ObjCMethodFamily. 00566 enum { InvalidObjCMethodFamily = (1 << ObjCMethodFamilyBitWidth) - 1 }; 00567 00568 /// Selector - This smart pointer class efficiently represents Objective-C 00569 /// method names. This class will either point to an IdentifierInfo or a 00570 /// MultiKeywordSelector (which is private). This enables us to optimize 00571 /// selectors that take no arguments and selectors that take 1 argument, which 00572 /// accounts for 78% of all selectors in Cocoa.h. 00573 class Selector { 00574 friend class Diagnostic; 00575 00576 enum IdentifierInfoFlag { 00577 // Empty selector = 0. 00578 ZeroArg = 0x1, 00579 OneArg = 0x2, 00580 MultiArg = 0x3, 00581 ArgFlags = ZeroArg|OneArg 00582 }; 00583 uintptr_t InfoPtr; // a pointer to the MultiKeywordSelector or IdentifierInfo. 00584 00585 Selector(IdentifierInfo *II, unsigned nArgs) { 00586 InfoPtr = reinterpret_cast<uintptr_t>(II); 00587 assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo"); 00588 assert(nArgs < 2 && "nArgs not equal to 0/1"); 00589 InfoPtr |= nArgs+1; 00590 } 00591 Selector(MultiKeywordSelector *SI) { 00592 InfoPtr = reinterpret_cast<uintptr_t>(SI); 00593 assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo"); 00594 InfoPtr |= MultiArg; 00595 } 00596 00597 IdentifierInfo *getAsIdentifierInfo() const { 00598 if (getIdentifierInfoFlag() < MultiArg) 00599 return reinterpret_cast<IdentifierInfo *>(InfoPtr & ~ArgFlags); 00600 return 0; 00601 } 00602 MultiKeywordSelector *getMultiKeywordSelector() const { 00603 return reinterpret_cast<MultiKeywordSelector *>(InfoPtr & ~ArgFlags); 00604 } 00605 00606 unsigned getIdentifierInfoFlag() const { 00607 return InfoPtr & ArgFlags; 00608 } 00609 00610 static ObjCMethodFamily getMethodFamilyImpl(Selector sel); 00611 00612 public: 00613 friend class SelectorTable; // only the SelectorTable can create these 00614 friend class DeclarationName; // and the AST's DeclarationName. 00615 00616 /// The default ctor should only be used when creating data structures that 00617 /// will contain selectors. 00618 Selector() : InfoPtr(0) {} 00619 Selector(uintptr_t V) : InfoPtr(V) {} 00620 00621 /// operator==/!= - Indicate whether the specified selectors are identical. 00622 bool operator==(Selector RHS) const { 00623 return InfoPtr == RHS.InfoPtr; 00624 } 00625 bool operator!=(Selector RHS) const { 00626 return InfoPtr != RHS.InfoPtr; 00627 } 00628 void *getAsOpaquePtr() const { 00629 return reinterpret_cast<void*>(InfoPtr); 00630 } 00631 00632 /// \brief Determine whether this is the empty selector. 00633 bool isNull() const { return InfoPtr == 0; } 00634 00635 // Predicates to identify the selector type. 00636 bool isKeywordSelector() const { 00637 return getIdentifierInfoFlag() != ZeroArg; 00638 } 00639 bool isUnarySelector() const { 00640 return getIdentifierInfoFlag() == ZeroArg; 00641 } 00642 unsigned getNumArgs() const; 00643 00644 00645 /// \brief Retrieve the identifier at a given position in the selector. 00646 /// 00647 /// Note that the identifier pointer returned may be NULL. Clients that only 00648 /// care about the text of the identifier string, and not the specific, 00649 /// uniqued identifier pointer, should use \c getNameForSlot(), which returns 00650 /// an empty string when the identifier pointer would be NULL. 00651 /// 00652 /// \param argIndex The index for which we want to retrieve the identifier. 00653 /// This index shall be less than \c getNumArgs() unless this is a keyword 00654 /// selector, in which case 0 is the only permissible value. 00655 /// 00656 /// \returns the uniqued identifier for this slot, or NULL if this slot has 00657 /// no corresponding identifier. 00658 IdentifierInfo *getIdentifierInfoForSlot(unsigned argIndex) const; 00659 00660 /// \brief Retrieve the name at a given position in the selector. 00661 /// 00662 /// \param argIndex The index for which we want to retrieve the name. 00663 /// This index shall be less than \c getNumArgs() unless this is a keyword 00664 /// selector, in which case 0 is the only permissible value. 00665 /// 00666 /// \returns the name for this slot, which may be the empty string if no 00667 /// name was supplied. 00668 StringRef getNameForSlot(unsigned argIndex) const; 00669 00670 /// getAsString - Derive the full selector name (e.g. "foo:bar:") and return 00671 /// it as an std::string. 00672 // FIXME: Add a print method that uses a raw_ostream. 00673 std::string getAsString() const; 00674 00675 /// getMethodFamily - Derive the conventional family of this method. 00676 ObjCMethodFamily getMethodFamily() const { 00677 return getMethodFamilyImpl(*this); 00678 } 00679 00680 static Selector getEmptyMarker() { 00681 return Selector(uintptr_t(-1)); 00682 } 00683 static Selector getTombstoneMarker() { 00684 return Selector(uintptr_t(-2)); 00685 } 00686 }; 00687 00688 /// SelectorTable - This table allows us to fully hide how we implement 00689 /// multi-keyword caching. 00690 class SelectorTable { 00691 void *Impl; // Actually a SelectorTableImpl 00692 SelectorTable(const SelectorTable&); // DISABLED: DO NOT IMPLEMENT 00693 void operator=(const SelectorTable&); // DISABLED: DO NOT IMPLEMENT 00694 public: 00695 SelectorTable(); 00696 ~SelectorTable(); 00697 00698 /// getSelector - This can create any sort of selector. NumArgs indicates 00699 /// whether this is a no argument selector "foo", a single argument selector 00700 /// "foo:" or multi-argument "foo:bar:". 00701 Selector getSelector(unsigned NumArgs, IdentifierInfo **IIV); 00702 00703 Selector getUnarySelector(IdentifierInfo *ID) { 00704 return Selector(ID, 1); 00705 } 00706 Selector getNullarySelector(IdentifierInfo *ID) { 00707 return Selector(ID, 0); 00708 } 00709 00710 /// Return the total amount of memory allocated for managing selectors. 00711 size_t getTotalMemory() const; 00712 00713 /// constructSetterName - Return the setter name for the given 00714 /// identifier, i.e. "set" + Name where the initial character of Name 00715 /// has been capitalized. 00716 static Selector constructSetterName(IdentifierTable &Idents, 00717 SelectorTable &SelTable, 00718 const IdentifierInfo *Name); 00719 }; 00720 00721 /// DeclarationNameExtra - Common base of the MultiKeywordSelector, 00722 /// CXXSpecialName, and CXXOperatorIdName classes, all of which are 00723 /// private classes that describe different kinds of names. 00724 class DeclarationNameExtra { 00725 public: 00726 /// ExtraKind - The kind of "extra" information stored in the 00727 /// DeclarationName. See @c ExtraKindOrNumArgs for an explanation of 00728 /// how these enumerator values are used. 00729 enum ExtraKind { 00730 CXXConstructor = 0, 00731 CXXDestructor, 00732 CXXConversionFunction, 00733 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 00734 CXXOperator##Name, 00735 #include "clang/Basic/OperatorKinds.def" 00736 CXXLiteralOperator, 00737 CXXUsingDirective, 00738 NUM_EXTRA_KINDS 00739 }; 00740 00741 /// ExtraKindOrNumArgs - Either the kind of C++ special name or 00742 /// operator-id (if the value is one of the CXX* enumerators of 00743 /// ExtraKind), in which case the DeclarationNameExtra is also a 00744 /// CXXSpecialName, (for CXXConstructor, CXXDestructor, or 00745 /// CXXConversionFunction) CXXOperatorIdName, or CXXLiteralOperatorName, 00746 /// it may be also name common to C++ using-directives (CXXUsingDirective), 00747 /// otherwise it is NUM_EXTRA_KINDS+NumArgs, where NumArgs is the number of 00748 /// arguments in the Objective-C selector, in which case the 00749 /// DeclarationNameExtra is also a MultiKeywordSelector. 00750 unsigned ExtraKindOrNumArgs; 00751 }; 00752 00753 } // end namespace clang 00754 00755 namespace llvm { 00756 /// Define DenseMapInfo so that Selectors can be used as keys in DenseMap and 00757 /// DenseSets. 00758 template <> 00759 struct DenseMapInfo<clang::Selector> { 00760 static inline clang::Selector getEmptyKey() { 00761 return clang::Selector::getEmptyMarker(); 00762 } 00763 static inline clang::Selector getTombstoneKey() { 00764 return clang::Selector::getTombstoneMarker(); 00765 } 00766 00767 static unsigned getHashValue(clang::Selector S); 00768 00769 static bool isEqual(clang::Selector LHS, clang::Selector RHS) { 00770 return LHS == RHS; 00771 } 00772 }; 00773 00774 template <> 00775 struct isPodLike<clang::Selector> { static const bool value = true; }; 00776 00777 template<> 00778 class PointerLikeTypeTraits<clang::Selector> { 00779 public: 00780 static inline const void *getAsVoidPointer(clang::Selector P) { 00781 return P.getAsOpaquePtr(); 00782 } 00783 static inline clang::Selector getFromVoidPointer(const void *P) { 00784 return clang::Selector(reinterpret_cast<uintptr_t>(P)); 00785 } 00786 enum { NumLowBitsAvailable = 0 }; 00787 }; 00788 00789 // Provide PointerLikeTypeTraits for IdentifierInfo pointers, which 00790 // are not guaranteed to be 8-byte aligned. 00791 template<> 00792 class PointerLikeTypeTraits<clang::IdentifierInfo*> { 00793 public: 00794 static inline void *getAsVoidPointer(clang::IdentifierInfo* P) { 00795 return P; 00796 } 00797 static inline clang::IdentifierInfo *getFromVoidPointer(void *P) { 00798 return static_cast<clang::IdentifierInfo*>(P); 00799 } 00800 enum { NumLowBitsAvailable = 1 }; 00801 }; 00802 00803 template<> 00804 class PointerLikeTypeTraits<const clang::IdentifierInfo*> { 00805 public: 00806 static inline const void *getAsVoidPointer(const clang::IdentifierInfo* P) { 00807 return P; 00808 } 00809 static inline const clang::IdentifierInfo *getFromVoidPointer(const void *P) { 00810 return static_cast<const clang::IdentifierInfo*>(P); 00811 } 00812 enum { NumLowBitsAvailable = 1 }; 00813 }; 00814 00815 } // end namespace llvm 00816 #endif