PreprocessingRecord.h revision 1b058e8956ea29a7fb7649fdee2b80581dcbdf39
1//===--- PreprocessingRecord.h - Record of Preprocessing --------*- 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 PreprocessingRecord class, which maintains a record
11//  of what occurred during preprocessing.
12//
13//===----------------------------------------------------------------------===//
14#ifndef LLVM_CLANG_LEX_PREPROCESSINGRECORD_H
15#define LLVM_CLANG_LEX_PREPROCESSINGRECORD_H
16
17#include "clang/Lex/PPCallbacks.h"
18#include "clang/Basic/SourceLocation.h"
19#include "llvm/ADT/DenseMap.h"
20#include "llvm/Support/Allocator.h"
21#include <vector>
22
23namespace clang {
24  class IdentifierInfo;
25  class PreprocessingRecord;
26}
27
28/// \brief Allocates memory within a Clang preprocessing record.
29void* operator new(size_t bytes, clang::PreprocessingRecord& PR,
30                   unsigned alignment = 8) throw();
31
32/// \brief Frees memory allocated in a Clang preprocessing record.
33void operator delete(void* ptr, clang::PreprocessingRecord& PR,
34                     unsigned) throw();
35
36namespace clang {
37  class MacroDefinition;
38
39  /// \brief Base class that describes a preprocessed entity, which may be a
40  /// preprocessor directive or macro instantiation.
41  class PreprocessedEntity {
42  public:
43    /// \brief The kind of preprocessed entity an object describes.
44    enum EntityKind {
45      /// \brief A macro instantiation.
46      MacroInstantiationKind,
47
48      /// \brief A preprocessing directive whose kind is not specified.
49      ///
50      /// This kind will be used for any preprocessing directive that does not
51      /// have a more specific kind within the \c DirectiveKind enumeration.
52      PreprocessingDirectiveKind,
53
54      /// \brief A macro definition.
55      MacroDefinitionKind,
56
57      FirstPreprocessingDirective = PreprocessingDirectiveKind,
58      LastPreprocessingDirective = MacroDefinitionKind
59    };
60
61  private:
62    /// \brief The kind of preprocessed entity that this object describes.
63    EntityKind Kind;
64
65    /// \brief The source range that covers this preprocessed entity.
66    SourceRange Range;
67
68  protected:
69    PreprocessedEntity(EntityKind Kind, SourceRange Range)
70      : Kind(Kind), Range(Range) { }
71
72  public:
73    /// \brief Retrieve the kind of preprocessed entity stored in this object.
74    EntityKind getKind() const { return Kind; }
75
76    /// \brief Retrieve the source range that covers this entire preprocessed
77    /// entity.
78    SourceRange getSourceRange() const { return Range; }
79
80    // Implement isa/cast/dyncast/etc.
81    static bool classof(const PreprocessedEntity *) { return true; }
82
83    // Only allow allocation of preprocessed entities using the allocator
84    // in PreprocessingRecord or by doing a placement new.
85    void* operator new(size_t bytes, PreprocessingRecord& PR,
86                       unsigned alignment = 8) throw() {
87      return ::operator new(bytes, PR, alignment);
88    }
89
90    void* operator new(size_t bytes, void* mem) throw() {
91      return mem;
92    }
93
94    void operator delete(void* ptr, PreprocessingRecord& PR,
95                         unsigned alignment) throw() {
96      return ::operator delete(ptr, PR, alignment);
97    }
98
99    void operator delete(void*, std::size_t) throw() { }
100    void operator delete(void*, void*) throw() { }
101
102  private:
103    // Make vanilla 'new' and 'delete' illegal for preprocessed entities.
104    void* operator new(size_t bytes) throw();
105    void operator delete(void* data) throw();
106  };
107
108  /// \brief Records the location of a macro instantiation.
109  class MacroInstantiation : public PreprocessedEntity {
110    /// \brief The name of the macro being instantiation.
111    IdentifierInfo *Name;
112
113    /// \brief The definition of this macro.
114    MacroDefinition *Definition;
115
116  public:
117    MacroInstantiation(IdentifierInfo *Name, SourceRange Range,
118                       MacroDefinition *Definition)
119      : PreprocessedEntity(MacroInstantiationKind, Range), Name(Name),
120        Definition(Definition) { }
121
122    /// \brief The name of the macro being instantiated.
123    IdentifierInfo *getName() const { return Name; }
124
125    /// \brief The definition of the macro being instantiated.
126    MacroDefinition *getDefinition() const { return Definition; }
127
128    // Implement isa/cast/dyncast/etc.
129    static bool classof(const PreprocessedEntity *PE) {
130      return PE->getKind() == MacroInstantiationKind;
131    }
132    static bool classof(const MacroInstantiation *) { return true; }
133
134  };
135
136  /// \brief Records the presence of a preprocessor directive.
137  class PreprocessingDirective : public PreprocessedEntity {
138  public:
139    PreprocessingDirective(EntityKind Kind, SourceRange Range)
140      : PreprocessedEntity(Kind, Range) { }
141
142    // Implement isa/cast/dyncast/etc.
143    static bool classof(const PreprocessedEntity *PD) {
144      return PD->getKind() >= FirstPreprocessingDirective &&
145             PD->getKind() <= LastPreprocessingDirective;
146    }
147    static bool classof(const PreprocessingDirective *) { return true; }
148  };
149
150  /// \brief Record the location of a macro definition.
151  class MacroDefinition : public PreprocessingDirective {
152    /// \brief The name of the macro being defined.
153    const IdentifierInfo *Name;
154
155    /// \brief The location of the macro name in the macro definition.
156    SourceLocation Location;
157
158  public:
159    explicit MacroDefinition(const IdentifierInfo *Name, SourceLocation Location,
160                             SourceRange Range)
161      : PreprocessingDirective(MacroDefinitionKind, Range), Name(Name),
162        Location(Location) { }
163
164    /// \brief Retrieve the name of the macro being defined.
165    const IdentifierInfo *getName() const { return Name; }
166
167    /// \brief Retrieve the location of the macro name in the definition.
168    SourceLocation getLocation() const { return Location; }
169
170    // Implement isa/cast/dyncast/etc.
171    static bool classof(const PreprocessedEntity *PE) {
172      return PE->getKind() == MacroDefinitionKind;
173    }
174    static bool classof(const MacroDefinition *) { return true; }
175  };
176
177  /// \brief An abstract class that should be subclassed by any external source
178  /// of preprocessing record entries.
179  class ExternalPreprocessingRecordSource {
180  public:
181    virtual ~ExternalPreprocessingRecordSource();
182
183    /// \brief Read any preallocated preprocessed entities from the external
184    /// source.
185    virtual void ReadPreprocessedEntities() = 0;
186  };
187
188  /// \brief A record of the steps taken while preprocessing a source file,
189  /// including the various preprocessing directives processed, macros
190  /// instantiated, etc.
191  class PreprocessingRecord : public PPCallbacks {
192    /// \brief Allocator used to store preprocessing objects.
193    llvm::BumpPtrAllocator BumpAlloc;
194
195    /// \brief The set of preprocessed entities in this record, in order they
196    /// were seen.
197    std::vector<PreprocessedEntity *> PreprocessedEntities;
198
199    /// \brief Mapping from MacroInfo structures to their definitions.
200    llvm::DenseMap<const MacroInfo *, MacroDefinition *> MacroDefinitions;
201
202    /// \brief External source of preprocessed entities.
203    ExternalPreprocessingRecordSource *ExternalSource;
204
205    /// \brief The number of preallocated entities (that are known to the
206    /// external source).
207    unsigned NumPreallocatedEntities;
208
209    /// \brief Whether we have already loaded all of the preallocated entities.
210    mutable bool LoadedPreallocatedEntities;
211
212    void MaybeLoadPreallocatedEntities() const ;
213
214  public:
215    PreprocessingRecord();
216
217    /// \brief Allocate memory in the preprocessing record.
218    void *Allocate(unsigned Size, unsigned Align = 8) {
219      return BumpAlloc.Allocate(Size, Align);
220    }
221
222    /// \brief Deallocate memory in the preprocessing record.
223    void Deallocate(void *Ptr) { }
224
225    // Iteration over the preprocessed entities.
226    typedef std::vector<PreprocessedEntity *>::iterator iterator;
227    typedef std::vector<PreprocessedEntity *>::const_iterator const_iterator;
228    iterator begin(bool OnlyLocalEntities = false);
229    iterator end(bool OnlyLocalEntities = false);
230    const_iterator begin(bool OnlyLocalEntities = false) const;
231    const_iterator end(bool OnlyLocalEntities = false) const;
232
233    /// \brief Add a new preprocessed entity to this record.
234    void addPreprocessedEntity(PreprocessedEntity *Entity);
235
236    /// \brief Set the external source for preprocessed entities.
237    void SetExternalSource(ExternalPreprocessingRecordSource &Source,
238                           unsigned NumPreallocatedEntities);
239
240    /// \brief Set the preallocated entry at the given index to the given
241    /// preprocessed entity.
242    void SetPreallocatedEntity(unsigned Index, PreprocessedEntity *Entity);
243
244    /// \brief Register a new macro definition.
245    void RegisterMacroDefinition(MacroInfo *Macro, MacroDefinition *MD);
246
247    /// \brief Retrieve the preprocessed entity at the given index.
248    PreprocessedEntity *getPreprocessedEntity(unsigned Index) {
249      assert(Index < PreprocessedEntities.size() &&
250             "Out-of-bounds preprocessed entity");
251      return PreprocessedEntities[Index];
252    }
253
254    /// \brief Retrieve the macro definition that corresponds to the given
255    /// \c MacroInfo.
256    MacroDefinition *findMacroDefinition(const MacroInfo *MI);
257
258    virtual void MacroExpands(const Token &Id, const MacroInfo* MI);
259    virtual void MacroDefined(const IdentifierInfo *II, const MacroInfo *MI);
260    virtual void MacroUndefined(const IdentifierInfo *II, const MacroInfo *MI);
261  };
262} // end namespace clang
263
264inline void* operator new(size_t bytes, clang::PreprocessingRecord& PR,
265                          unsigned alignment) throw() {
266  return PR.Allocate(bytes, alignment);
267}
268
269inline void operator delete(void* ptr, clang::PreprocessingRecord& PR,
270                            unsigned) throw() {
271  PR.Deallocate(ptr);
272}
273
274#endif // LLVM_CLANG_LEX_PREPROCESSINGRECORD_H
275