PTHManager.h revision f056d92e182cbe4f62c8d14102544dc38066dabc
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 char* const TableBeg; 38 const char* const TableEnd; 39 40 const unsigned NumSpellings; 41 const 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, const char* tableBeg) 50 : PTHMgr(pm), 51 TableBeg(tableBeg), 52 TableEnd(tableBeg + numSpellings*SpellingEntrySize), 53 NumSpellings(numSpellings), 54 LinearItr(tableBeg) {} 55}; 56 57class PTHManager : public IdentifierInfoLookup { 58 friend class PTHLexer; 59 friend class PTHSpellingSearch; 60 61 /// The memory mapped PTH file. 62 const llvm::MemoryBuffer* Buf; 63 64 /// A map from FileIDs to SpellingSearch objects. 65 llvm::DenseMap<FileID, PTHSpellingSearch*> SpellingMap; 66 67 /// Alloc - Allocator used for IdentifierInfo objects. 68 llvm::BumpPtrAllocator Alloc; 69 70 /// IdMap - A lazily generated cache mapping from persistent identifiers to 71 /// IdentifierInfo*. 72 IdentifierInfo** PerIDCache; 73 74 /// FileLookup - Abstract data structure used for mapping between files 75 /// and token data in the PTH file. 76 void* FileLookup; 77 78 /// IdDataTable - Array representing the mapping from persistent IDs to the 79 /// data offset within the PTH file containing the information to 80 /// reconsitute an IdentifierInfo. 81 const char* const IdDataTable; 82 83 /// SortedIdTable - Array ordering persistent identifier IDs by the lexical 84 /// order of their corresponding strings. This is used by get(). 85 const char* const SortedIdTable; 86 87 /// NumIds - The number of identifiers in the PTH file. 88 const unsigned NumIds; 89 90 /// PP - The Preprocessor object that will use this PTHManager to create 91 /// PTHLexer objects. 92 Preprocessor* PP; 93 94 /// This constructor is intended to only be called by the static 'Create' 95 /// method. 96 PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup, 97 const char* idDataTable, IdentifierInfo** perIDCache, 98 const char* sortedIdTable, unsigned numIds); 99 100 // Do not implement. 101 PTHManager(); 102 void operator=(const PTHManager&); 103 104 /// getSpellingAtPTHOffset - Used by PTHLexer classes to get the cached 105 /// spelling for a token. 106 unsigned getSpellingAtPTHOffset(unsigned PTHOffset, const char*& Buffer); 107 108 109 /// GetIdentifierInfo - Used to reconstruct IdentifierInfo objects from the 110 /// PTH file. 111 IdentifierInfo* GetIdentifierInfo(unsigned); 112 113public: 114 ~PTHManager(); 115 116 /// get - Return the identifier token info for the specified named identifier. 117 /// Unlike the version in IdentifierTable, this returns a pointer instead 118 /// of a reference. If the pointer is NULL then the IdentifierInfo cannot 119 /// be found. 120 IdentifierInfo *get(const char *NameStart, const char *NameEnd); 121 122 /// Create - This method creates PTHManager objects. The 'file' argument 123 /// is the name of the PTH file. This method returns NULL upon failure. 124 static PTHManager *Create(const std::string& file); 125 126 void setPreprocessor(Preprocessor *pp) { PP = pp; } 127 128 /// CreateLexer - Return a PTHLexer that "lexes" the cached tokens for the 129 /// specified file. This method returns NULL if no cached tokens exist. 130 /// It is the responsibility of the caller to 'delete' the returned object. 131 PTHLexer *CreateLexer(FileID FID); 132 133 unsigned getSpelling(SourceLocation Loc, const char *&Buffer); 134private: 135 unsigned getSpelling(FileID FID, unsigned fpos, const char *& Buffer); 136}; 137 138} // end namespace clang 139 140#endif 141