PTHManager.h revision 77ecb3a28f21496ecfdbb3d5f5b66b0d2abf48c9
1//===--- PTHManager.h - Manager object for PTH processing -------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the PTHManager interface. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_PTHMANAGER_H 15#define LLVM_CLANG_PTHMANAGER_H 16 17#include "clang/Lex/PTHLexer.h" 18#include "clang/Basic/LangOptions.h" 19#include "clang/Basic/IdentifierTable.h" 20#include "llvm/ADT/DenseMap.h" 21#include "llvm/Support/Allocator.h" 22#include <string> 23 24namespace llvm { 25 class MemoryBuffer; 26} 27 28namespace clang { 29 30class FileEntry; 31class PTHLexer; 32class PTHManager; 33 34class PTHSpellingSearch { 35 PTHManager& PTHMgr; 36 37 const unsigned char* const TableBeg; 38 const unsigned char* const TableEnd; 39 40 const unsigned NumSpellings; 41 const unsigned char* LinearItr; 42 43public: 44 enum { SpellingEntrySize = 4*2 }; 45 46 unsigned getSpellingBinarySearch(unsigned fpos, const char *&Buffer); 47 unsigned getSpellingLinearSearch(unsigned fpos, const char *&Buffer); 48 49 PTHSpellingSearch(PTHManager& pm, unsigned numSpellings, 50 const unsigned char* tableBeg) 51 : PTHMgr(pm), 52 TableBeg(tableBeg), 53 TableEnd(tableBeg + numSpellings*SpellingEntrySize), 54 NumSpellings(numSpellings), 55 LinearItr(tableBeg) {} 56}; 57 58class PTHManager : public IdentifierInfoLookup { 59 friend class PTHLexer; 60 friend class PTHSpellingSearch; 61 62 /// The memory mapped PTH file. 63 const llvm::MemoryBuffer* Buf; 64 65 /// A map from FileIDs to SpellingSearch objects. 66 llvm::DenseMap<FileID, PTHSpellingSearch*> SpellingMap; 67 68 /// Alloc - Allocator used for IdentifierInfo objects. 69 llvm::BumpPtrAllocator Alloc; 70 71 /// IdMap - A lazily generated cache mapping from persistent identifiers to 72 /// IdentifierInfo*. 73 IdentifierInfo** PerIDCache; 74 75 /// FileLookup - Abstract data structure used for mapping between files 76 /// and token data in the PTH file. 77 void* FileLookup; 78 79 /// IdDataTable - Array representing the mapping from persistent IDs to the 80 /// data offset within the PTH file containing the information to 81 /// reconsitute an IdentifierInfo. 82 const unsigned char* const IdDataTable; 83 84 /// SortedIdTable - Array ordering persistent identifier IDs by the lexical 85 /// order of their corresponding strings. This is used by get(). 86 const unsigned char* const SortedIdTable; 87 88 /// NumIds - The number of identifiers in the PTH file. 89 const unsigned NumIds; 90 91 /// PP - The Preprocessor object that will use this PTHManager to create 92 /// PTHLexer objects. 93 Preprocessor* PP; 94 95 /// This constructor is intended to only be called by the static 'Create' 96 /// method. 97 PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup, 98 const unsigned char* idDataTable, IdentifierInfo** perIDCache, 99 const unsigned char* sortedIdTable, unsigned numIds); 100 101 // Do not implement. 102 PTHManager(); 103 void operator=(const PTHManager&); 104 105 /// getSpellingAtPTHOffset - Used by PTHLexer classes to get the cached 106 /// spelling for a token. 107 unsigned getSpellingAtPTHOffset(unsigned PTHOffset, const char*& Buffer); 108 109 110 /// GetIdentifierInfo - Used to reconstruct IdentifierInfo objects from the 111 /// PTH file. 112 inline IdentifierInfo* GetIdentifierInfo(unsigned PersistentID) { 113 // Check if the IdentifierInfo has already been resolved. 114 if (IdentifierInfo* II = PerIDCache[PersistentID]) 115 return II; 116 return LazilyCreateIdentifierInfo(PersistentID); 117 } 118 IdentifierInfo* LazilyCreateIdentifierInfo(unsigned PersistentID); 119 120public: 121 ~PTHManager(); 122 123 /// get - Return the identifier token info for the specified named identifier. 124 /// Unlike the version in IdentifierTable, this returns a pointer instead 125 /// of a reference. If the pointer is NULL then the IdentifierInfo cannot 126 /// be found. 127 IdentifierInfo *get(const char *NameStart, const char *NameEnd); 128 129 /// Create - This method creates PTHManager objects. The 'file' argument 130 /// is the name of the PTH file. This method returns NULL upon failure. 131 static PTHManager *Create(const std::string& file); 132 133 void setPreprocessor(Preprocessor *pp) { PP = pp; } 134 135 /// CreateLexer - Return a PTHLexer that "lexes" the cached tokens for the 136 /// specified file. This method returns NULL if no cached tokens exist. 137 /// It is the responsibility of the caller to 'delete' the returned object. 138 PTHLexer *CreateLexer(FileID FID); 139 140 unsigned getSpelling(SourceLocation Loc, const char *&Buffer); 141private: 142 unsigned getSpelling(FileID FID, unsigned fpos, const char *& Buffer); 143}; 144 145} // end namespace clang 146 147#endif 148