PTHManager.h revision f02f6f0ee3e05b958bcf67f00f4503671d67eccd
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 "llvm/ADT/DenseMap.h"
20#include <string>
21
22namespace llvm {
23  class MemoryBuffer;
24}
25
26namespace clang {
27
28class FileEntry;
29class IdentifierInfo;
30class IdentifierTable;
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 {
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<unsigned,PTHSpellingSearch*> SpellingMap;
66
67  /// IdMap - A lazily generated cache mapping from persistent identifiers to
68  ///  IdentifierInfo*.
69  IdentifierInfo** PerIDCache;
70
71  /// FileLookup - Abstract data structure used for mapping between files
72  ///  and token data in the PTH file.
73  void* FileLookup;
74
75  /// IdDataTable - Array representing the mapping from persistent IDs to the
76  ///  data offset within the PTH file containing the information to
77  ///  reconsitute an IdentifierInfo.
78  const char* IdDataTable;
79
80  /// ITable - The IdentifierTable used for the translation unit being lexed.
81  IdentifierTable& ITable;
82
83  /// PP - The Preprocessor object that will use this PTHManager to create
84  ///  PTHLexer objects.
85  Preprocessor& PP;
86
87  /// This constructor is intended to only be called by the static 'Create'
88  /// method.
89  PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
90             const char* idDataTable, IdentifierInfo** perIDCache,
91             Preprocessor& pp);
92
93  // Do not implement.
94  PTHManager();
95  void operator=(const PTHManager&);
96
97  /// GetIdentifierInfo - Used by PTHManager to reconstruct IdentifierInfo
98  ///  objects from the PTH file.
99  IdentifierInfo* GetIdentifierInfo(unsigned);
100
101  /// getSpellingAtPTHOffset - Used by PTHLexer classes to get the cached
102  ///  spelling for a token.
103  unsigned getSpellingAtPTHOffset(unsigned PTHOffset, const char*& Buffer);
104
105public:
106
107  ~PTHManager();
108
109  /// Create - This method creates PTHManager objects.  The 'file' argument
110  ///  is the name of the PTH file.  This method returns NULL upon failure.
111  static PTHManager* Create(const std::string& file, Preprocessor& PP);
112
113  /// CreateLexer - Return a PTHLexer that "lexes" the cached tokens for the
114  ///  specified file.  This method returns NULL if no cached tokens exist.
115  ///  It is the responsibility of the caller to 'delete' the returned object.
116  PTHLexer* CreateLexer(unsigned FileID, const FileEntry* FE);
117
118  unsigned getSpelling(unsigned FileID, unsigned fpos, const char *& Buffer);
119};
120
121}  // end namespace clang
122
123#endif
124