PTHManager.h revision 277faca30c9f8f72b79f55695cbe3395ec246e7c
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;
32
33class PTHManager : public IdentifierInfoLookup {
34  friend class PTHLexer;
35
36  /// The memory mapped PTH file.
37  const llvm::MemoryBuffer* Buf;
38
39  /// Alloc - Allocator used for IdentifierInfo objects.
40  llvm::BumpPtrAllocator Alloc;
41
42  /// IdMap - A lazily generated cache mapping from persistent identifiers to
43  ///  IdentifierInfo*.
44  IdentifierInfo** PerIDCache;
45
46  /// FileLookup - Abstract data structure used for mapping between files
47  ///  and token data in the PTH file.
48  void* FileLookup;
49
50  /// IdDataTable - Array representing the mapping from persistent IDs to the
51  ///  data offset within the PTH file containing the information to
52  ///  reconsitute an IdentifierInfo.
53  const unsigned char* const IdDataTable;
54
55  /// SortedIdTable - Array ordering persistent identifier IDs by the lexical
56  ///  order of their corresponding strings.  This is used by get().
57  const unsigned char* const SortedIdTable;
58
59  /// NumIds - The number of identifiers in the PTH file.
60  const unsigned NumIds;
61
62  /// PP - The Preprocessor object that will use this PTHManager to create
63  ///  PTHLexer objects.
64  Preprocessor* PP;
65
66  /// SpellingBase - The base offset within the PTH memory buffer that
67  ///  contains the cached spellings for literals.
68  const unsigned char* const SpellingBase;
69
70  /// This constructor is intended to only be called by the static 'Create'
71  /// method.
72  PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
73             const unsigned char* idDataTable, IdentifierInfo** perIDCache,
74             const unsigned char* sortedIdTable, unsigned numIds,
75             const unsigned char* spellingBase);
76
77  // Do not implement.
78  PTHManager();
79  void operator=(const PTHManager&);
80
81  /// getSpellingAtPTHOffset - Used by PTHLexer classes to get the cached
82  ///  spelling for a token.
83  unsigned getSpellingAtPTHOffset(unsigned PTHOffset, const char*& Buffer);
84
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 = 1 };
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);
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