1 //===--- PreprocessingRecord.h - Record of Preprocessing --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the PreprocessingRecord class, which maintains a record
11 // of what occurred during preprocessing.
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LEX_PREPROCESSINGRECORD_H
15 #define LLVM_CLANG_LEX_PREPROCESSINGRECORD_H
17 #include "clang/Lex/PPCallbacks.h"
18 #include "clang/Basic/SourceLocation.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/Support/Allocator.h"
25 class PreprocessingRecord;
28 /// \brief Allocates memory within a Clang preprocessing record.
29 void* operator new(size_t bytes, clang::PreprocessingRecord& PR,
30 unsigned alignment = 8) throw();
32 /// \brief Frees memory allocated in a Clang preprocessing record.
33 void operator delete(void* ptr, clang::PreprocessingRecord& PR,
37 class MacroDefinition;
39 /// \brief Base class that describes a preprocessed entity, which may be a
40 /// preprocessor directive or macro instantiation.
41 class PreprocessedEntity {
43 /// \brief The kind of preprocessed entity an object describes.
45 /// \brief A macro instantiation.
46 MacroInstantiationKind,
48 /// \brief A preprocessing directive whose kind is not specified.
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,
54 /// \brief A macro definition.
57 FirstPreprocessingDirective = PreprocessingDirectiveKind,
58 LastPreprocessingDirective = MacroDefinitionKind
62 /// \brief The kind of preprocessed entity that this object describes.
65 /// \brief The source range that covers this preprocessed entity.
69 PreprocessedEntity(EntityKind Kind, SourceRange Range)
70 : Kind(Kind), Range(Range) { }
73 /// \brief Retrieve the kind of preprocessed entity stored in this object.
74 EntityKind getKind() const { return Kind; }
76 /// \brief Retrieve the source range that covers this entire preprocessed
78 SourceRange getSourceRange() const { return Range; }
80 // Implement isa/cast/dyncast/etc.
81 static bool classof(const PreprocessedEntity *) { return true; }
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);
90 void* operator new(size_t bytes, void* mem) throw() {
94 void operator delete(void* ptr, PreprocessingRecord& PR,
95 unsigned alignment) throw() {
96 return ::operator delete(ptr, PR, alignment);
99 void operator delete(void*, std::size_t) throw() { }
100 void operator delete(void*, void*) throw() { }
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();
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;
113 /// \brief The definition of this macro.
114 MacroDefinition *Definition;
117 MacroInstantiation(IdentifierInfo *Name, SourceRange Range,
118 MacroDefinition *Definition)
119 : PreprocessedEntity(MacroInstantiationKind, Range), Name(Name),
120 Definition(Definition) { }
122 /// \brief The name of the macro being instantiated.
123 IdentifierInfo *getName() const { return Name; }
125 /// \brief The definition of the macro being instantiated.
126 MacroDefinition *getDefinition() const { return Definition; }
128 // Implement isa/cast/dyncast/etc.
129 static bool classof(const PreprocessedEntity *PE) {
130 return PE->getKind() == MacroInstantiationKind;
132 static bool classof(const MacroInstantiation *) { return true; }
136 /// \brief Records the presence of a preprocessor directive.
137 class PreprocessingDirective : public PreprocessedEntity {
139 PreprocessingDirective(EntityKind Kind, SourceRange Range)
140 : PreprocessedEntity(Kind, Range) { }
142 // Implement isa/cast/dyncast/etc.
143 static bool classof(const PreprocessedEntity *PD) {
144 return PD->getKind() >= FirstPreprocessingDirective &&
145 PD->getKind() <= LastPreprocessingDirective;
147 static bool classof(const PreprocessingDirective *) { return true; }
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;
155 /// \brief The location of the macro name in the macro definition.
156 SourceLocation Location;
159 explicit MacroDefinition(const IdentifierInfo *Name, SourceLocation Location,
161 : PreprocessingDirective(MacroDefinitionKind, Range), Name(Name),
162 Location(Location) { }
164 /// \brief Retrieve the name of the macro being defined.
165 const IdentifierInfo *getName() const { return Name; }
167 /// \brief Retrieve the location of the macro name in the definition.
168 SourceLocation getLocation() const { return Location; }
170 // Implement isa/cast/dyncast/etc.
171 static bool classof(const PreprocessedEntity *PE) {
172 return PE->getKind() == MacroDefinitionKind;
174 static bool classof(const MacroDefinition *) { return true; }
177 /// \brief An abstract class that should be subclassed by any external source
178 /// of preprocessing record entries.
179 class ExternalPreprocessingRecordSource {
181 virtual ~ExternalPreprocessingRecordSource();
183 /// \brief Read any preallocated preprocessed entities from the external
185 virtual void ReadPreprocessedEntities() = 0;
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;
195 /// \brief The set of preprocessed entities in this record, in order they
197 std::vector<PreprocessedEntity *> PreprocessedEntities;
199 /// \brief Mapping from MacroInfo structures to their definitions.
200 llvm::DenseMap<const MacroInfo *, MacroDefinition *> MacroDefinitions;
202 /// \brief External source of preprocessed entities.
203 ExternalPreprocessingRecordSource *ExternalSource;
205 /// \brief The number of preallocated entities (that are known to the
206 /// external source).
207 unsigned NumPreallocatedEntities;
209 /// \brief Whether we have already loaded all of the preallocated entities.
210 mutable bool LoadedPreallocatedEntities;
212 void MaybeLoadPreallocatedEntities() const ;
215 PreprocessingRecord();
217 /// \brief Allocate memory in the preprocessing record.
218 void *Allocate(unsigned Size, unsigned Align = 8) {
219 return BumpAlloc.Allocate(Size, Align);
222 /// \brief Deallocate memory in the preprocessing record.
223 void Deallocate(void *Ptr) { }
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;
233 /// \brief Add a new preprocessed entity to this record.
234 void addPreprocessedEntity(PreprocessedEntity *Entity);
236 /// \brief Set the external source for preprocessed entities.
237 void SetExternalSource(ExternalPreprocessingRecordSource &Source,
238 unsigned NumPreallocatedEntities);
240 /// \brief Set the preallocated entry at the given index to the given
241 /// preprocessed entity.
242 void SetPreallocatedEntity(unsigned Index, PreprocessedEntity *Entity);
244 /// \brief Register a new macro definition.
245 void RegisterMacroDefinition(MacroInfo *Macro, MacroDefinition *MD);
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];
254 /// \brief Retrieve the macro definition that corresponds to the given
256 MacroDefinition *findMacroDefinition(const MacroInfo *MI);
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);
262 } // end namespace clang
264 inline void* operator new(size_t bytes, clang::PreprocessingRecord& PR,
265 unsigned alignment) throw() {
266 return PR.Allocate(bytes, alignment);
269 inline void operator delete(void* ptr, clang::PreprocessingRecord& PR,
274 #endif // LLVM_CLANG_LEX_PREPROCESSINGRECORD_H