PTHManager.h revision a4bd8eb4d6d4b625f6bbb62fc180b02eab6433ed
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 Diagnostic;
33
34class PTHManager : public IdentifierInfoLookup {
35  friend class PTHLexer;
36
37  /// The memory mapped PTH file.
38  const llvm::MemoryBuffer* Buf;
39
40  /// Alloc - Allocator used for IdentifierInfo objects.
41  llvm::BumpPtrAllocator Alloc;
42
43  /// IdMap - A lazily generated cache mapping from persistent identifiers to
44  ///  IdentifierInfo*.
45  IdentifierInfo** PerIDCache;
46
47  /// FileLookup - Abstract data structure used for mapping between files
48  ///  and token data in the PTH file.
49  void* FileLookup;
50
51  /// IdDataTable - Array representing the mapping from persistent IDs to the
52  ///  data offset within the PTH file containing the information to
53  ///  reconsitute an IdentifierInfo.
54  const unsigned char* const IdDataTable;
55
56  /// SortedIdTable - Abstract data structure mapping from strings to
57  ///  persistent IDs.  This is used by get().
58  void* StringIdLookup;
59
60  /// NumIds - The number of identifiers in the PTH file.
61  const unsigned NumIds;
62
63  /// PP - The Preprocessor object that will use this PTHManager to create
64  ///  PTHLexer objects.
65  Preprocessor* PP;
66
67  /// SpellingBase - The base offset within the PTH memory buffer that
68  ///  contains the cached spellings for literals.
69  const unsigned char* const SpellingBase;
70
71  /// This constructor is intended to only be called by the static 'Create'
72  /// method.
73  PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
74             const unsigned char* idDataTable, IdentifierInfo** perIDCache,
75             void* stringIdLookup, unsigned numIds,
76             const unsigned char* spellingBase);
77
78  // Do not implement.
79  PTHManager();
80  void operator=(const PTHManager&);
81
82  /// getSpellingAtPTHOffset - Used by PTHLexer classes to get the cached
83  ///  spelling for a token.
84  unsigned getSpellingAtPTHOffset(unsigned PTHOffset, const char*& Buffer);
85
86  /// GetIdentifierInfo - Used to reconstruct IdentifierInfo objects from the
87  ///  PTH file.
88  inline IdentifierInfo* GetIdentifierInfo(unsigned PersistentID) {
89    // Check if the IdentifierInfo has already been resolved.
90    if (IdentifierInfo* II = PerIDCache[PersistentID])
91      return II;
92    return LazilyCreateIdentifierInfo(PersistentID);
93  }
94  IdentifierInfo* LazilyCreateIdentifierInfo(unsigned PersistentID);
95
96public:
97  // The current PTH version.
98  enum { Version = 6 };
99
100  ~PTHManager();
101
102  /// get - Return the identifier token info for the specified named identifier.
103  ///  Unlike the version in IdentifierTable, this returns a pointer instead
104  ///  of a reference.  If the pointer is NULL then the IdentifierInfo cannot
105  ///  be found.
106  IdentifierInfo *get(const char *NameStart, const char *NameEnd);
107
108  /// Create - This method creates PTHManager objects.  The 'file' argument
109  ///  is the name of the PTH file.  This method returns NULL upon failure.
110  static PTHManager *Create(const std::string& file, Diagnostic* Diags = 0);
111
112  void setPreprocessor(Preprocessor *pp) { PP = pp; }
113
114  /// CreateLexer - Return a PTHLexer that "lexes" the cached tokens for the
115  ///  specified file.  This method returns NULL if no cached tokens exist.
116  ///  It is the responsibility of the caller to 'delete' the returned object.
117  PTHLexer *CreateLexer(FileID FID);
118};
119
120}  // end namespace clang
121
122#endif
123