]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/llvm/tools/clang/include/clang/Lex/PreprocessingRecord.h
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / llvm / tools / clang / include / clang / Lex / PreprocessingRecord.h
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 "clang/Basic/IdentifierTable.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/Support/Allocator.h"
22 #include <vector>
23
24 namespace clang {
25   class IdentifierInfo;
26   class PreprocessingRecord;
27 }
28
29 /// \brief Allocates memory within a Clang preprocessing record.
30 void* operator new(size_t bytes, clang::PreprocessingRecord& PR,
31                    unsigned alignment = 8) throw();
32
33 /// \brief Frees memory allocated in a Clang preprocessing record.
34 void operator delete(void* ptr, clang::PreprocessingRecord& PR,
35                      unsigned) throw();
36
37 namespace clang {
38   class MacroDefinition;
39   class FileEntry;
40
41   /// \brief Base class that describes a preprocessed entity, which may be a
42   /// preprocessor directive or macro expansion.
43   class PreprocessedEntity {
44   public:
45     /// \brief The kind of preprocessed entity an object describes.
46     enum EntityKind {
47       /// \brief Indicates a problem trying to load the preprocessed entity.
48       InvalidKind,
49
50       /// \brief A macro expansion.
51       MacroExpansionKind,
52       
53       /// \defgroup Preprocessing directives
54       /// @{
55       
56       /// \brief A macro definition.
57       MacroDefinitionKind,
58       
59       /// \brief An inclusion directive, such as \c #include, \c
60       /// #import, or \c #include_next.
61       InclusionDirectiveKind,
62
63       /// @}
64
65       FirstPreprocessingDirective = MacroDefinitionKind,
66       LastPreprocessingDirective = InclusionDirectiveKind
67     };
68
69   private:
70     /// \brief The kind of preprocessed entity that this object describes.
71     EntityKind Kind;
72     
73     /// \brief The source range that covers this preprocessed entity.
74     SourceRange Range;
75     
76   protected:
77     PreprocessedEntity(EntityKind Kind, SourceRange Range)
78       : Kind(Kind), Range(Range) { }
79
80     friend class PreprocessingRecord;
81
82   public:
83     /// \brief Retrieve the kind of preprocessed entity stored in this object.
84     EntityKind getKind() const { return Kind; }
85     
86     /// \brief Retrieve the source range that covers this entire preprocessed 
87     /// entity.
88     SourceRange getSourceRange() const { return Range; }
89
90     /// \brief Returns true if there was a problem loading the preprocessed
91     /// entity.
92     bool isInvalid() const { return Kind == InvalidKind; }
93
94     // Implement isa/cast/dyncast/etc.
95     static bool classof(const PreprocessedEntity *) { return true; }
96
97     // Only allow allocation of preprocessed entities using the allocator 
98     // in PreprocessingRecord or by doing a placement new.
99     void* operator new(size_t bytes, PreprocessingRecord& PR,
100                        unsigned alignment = 8) throw() {
101       return ::operator new(bytes, PR, alignment);
102     }
103     
104     void* operator new(size_t bytes, void* mem) throw() {
105       return mem;
106     }
107     
108     void operator delete(void* ptr, PreprocessingRecord& PR, 
109                          unsigned alignment) throw() {
110       return ::operator delete(ptr, PR, alignment);
111     }
112     
113     void operator delete(void*, std::size_t) throw() { }
114     void operator delete(void*, void*) throw() { }
115     
116   private:
117     // Make vanilla 'new' and 'delete' illegal for preprocessed entities.
118     void* operator new(size_t bytes) throw();
119     void operator delete(void* data) throw();
120   };
121   
122   /// \brief Records the presence of a preprocessor directive.
123   class PreprocessingDirective : public PreprocessedEntity {
124   public:
125     PreprocessingDirective(EntityKind Kind, SourceRange Range) 
126       : PreprocessedEntity(Kind, Range) { }
127     
128     // Implement isa/cast/dyncast/etc.
129     static bool classof(const PreprocessedEntity *PD) { 
130       return PD->getKind() >= FirstPreprocessingDirective &&
131              PD->getKind() <= LastPreprocessingDirective;
132     }
133     static bool classof(const PreprocessingDirective *) { return true; }    
134   };
135   
136   /// \brief Record the location of a macro definition.
137   class MacroDefinition : public PreprocessingDirective {
138     /// \brief The name of the macro being defined.
139     const IdentifierInfo *Name;
140
141   public:
142     explicit MacroDefinition(const IdentifierInfo *Name, SourceRange Range)
143       : PreprocessingDirective(MacroDefinitionKind, Range), Name(Name) { }
144     
145     /// \brief Retrieve the name of the macro being defined.
146     const IdentifierInfo *getName() const { return Name; }
147     
148     /// \brief Retrieve the location of the macro name in the definition.
149     SourceLocation getLocation() const { return getSourceRange().getBegin(); }
150     
151     // Implement isa/cast/dyncast/etc.
152     static bool classof(const PreprocessedEntity *PE) {
153       return PE->getKind() == MacroDefinitionKind;
154     }
155     static bool classof(const MacroDefinition *) { return true; }
156   };
157   
158   /// \brief Records the location of a macro expansion.
159   class MacroExpansion : public PreprocessedEntity {
160     /// \brief The definition of this macro or the name of the macro if it is
161     /// a builtin macro.
162     llvm::PointerUnion<IdentifierInfo *, MacroDefinition *> NameOrDef; 
163
164   public:
165     MacroExpansion(IdentifierInfo *BuiltinName, SourceRange Range)
166       : PreprocessedEntity(MacroExpansionKind, Range),
167         NameOrDef(BuiltinName) { }
168
169     MacroExpansion(MacroDefinition *Definition, SourceRange Range)
170       : PreprocessedEntity(MacroExpansionKind, Range),
171         NameOrDef(Definition) { }
172
173     /// \brief True if it is a builtin macro.
174     bool isBuiltinMacro() const { return NameOrDef.is<IdentifierInfo *>(); }
175     
176     /// \brief The name of the macro being expanded.
177     const IdentifierInfo *getName() const {
178       if (MacroDefinition *Def = getDefinition())
179         return Def->getName();
180       return NameOrDef.get<IdentifierInfo*>();
181     }
182     
183     /// \brief The definition of the macro being expanded. May return null if
184     /// this is a builtin macro.
185     MacroDefinition *getDefinition() const {
186       return NameOrDef.dyn_cast<MacroDefinition *>();
187     }
188
189     // Implement isa/cast/dyncast/etc.
190     static bool classof(const PreprocessedEntity *PE) {
191       return PE->getKind() == MacroExpansionKind;
192     }
193     static bool classof(const MacroExpansion *) { return true; }
194   };
195
196   /// \brief Record the location of an inclusion directive, such as an
197   /// \c #include or \c #import statement.
198   class InclusionDirective : public PreprocessingDirective {
199   public:
200     /// \brief The kind of inclusion directives known to the
201     /// preprocessor.
202     enum InclusionKind {
203       /// \brief An \c #include directive.
204       Include,
205       /// \brief An Objective-C \c #import directive.
206       Import,
207       /// \brief A GNU \c #include_next directive.
208       IncludeNext,
209       /// \brief A Clang \c #__include_macros directive.
210       IncludeMacros
211     };
212
213   private:
214     /// \brief The name of the file that was included, as written in
215     /// the source.
216     StringRef FileName;
217
218     /// \brief Whether the file name was in quotation marks; otherwise, it was
219     /// in angle brackets.
220     unsigned InQuotes : 1;
221
222     /// \brief The kind of inclusion directive we have.
223     ///
224     /// This is a value of type InclusionKind.
225     unsigned Kind : 2;
226
227     /// \brief The file that was included.
228     const FileEntry *File;
229
230   public:
231     InclusionDirective(PreprocessingRecord &PPRec,
232                        InclusionKind Kind, StringRef FileName, 
233                        bool InQuotes, const FileEntry *File, SourceRange Range);
234     
235     /// \brief Determine what kind of inclusion directive this is.
236     InclusionKind getKind() const { return static_cast<InclusionKind>(Kind); }
237     
238     /// \brief Retrieve the included file name as it was written in the source.
239     StringRef getFileName() const { return FileName; }
240     
241     /// \brief Determine whether the included file name was written in quotes;
242     /// otherwise, it was written in angle brackets.
243     bool wasInQuotes() const { return InQuotes; }
244     
245     /// \brief Retrieve the file entry for the actual file that was included
246     /// by this directive.
247     const FileEntry *getFile() const { return File; }
248         
249     // Implement isa/cast/dyncast/etc.
250     static bool classof(const PreprocessedEntity *PE) {
251       return PE->getKind() == InclusionDirectiveKind;
252     }
253     static bool classof(const InclusionDirective *) { return true; }
254   };
255   
256   /// \brief An abstract class that should be subclassed by any external source
257   /// of preprocessing record entries.
258   class ExternalPreprocessingRecordSource {
259   public:
260     virtual ~ExternalPreprocessingRecordSource();
261     
262     /// \brief Read a preallocated preprocessed entity from the external source.
263     ///
264     /// \returns null if an error occurred that prevented the preprocessed
265     /// entity from being loaded.
266     virtual PreprocessedEntity *ReadPreprocessedEntity(unsigned Index) = 0;
267
268     /// \brief Returns a pair of [Begin, End) indices of preallocated
269     /// preprocessed entities that \arg Range encompasses.
270     virtual std::pair<unsigned, unsigned>
271         findPreprocessedEntitiesInRange(SourceRange Range) = 0;
272   };
273   
274   /// \brief A record of the steps taken while preprocessing a source file,
275   /// including the various preprocessing directives processed, macros 
276   /// expanded, etc.
277   class PreprocessingRecord : public PPCallbacks {
278     SourceManager &SourceMgr;
279
280     /// \brief Whether we should include nested macro expansions in
281     /// the preprocessing record.
282     bool IncludeNestedMacroExpansions;
283     
284     /// \brief Allocator used to store preprocessing objects.
285     llvm::BumpPtrAllocator BumpAlloc;
286
287     /// \brief The set of preprocessed entities in this record, in order they
288     /// were seen.
289     std::vector<PreprocessedEntity *> PreprocessedEntities;
290     
291     /// \brief The set of preprocessed entities in this record that have been
292     /// loaded from external sources.
293     ///
294     /// The entries in this vector are loaded lazily from the external source,
295     /// and are referenced by the iterator using negative indices.
296     std::vector<PreprocessedEntity *> LoadedPreprocessedEntities;
297
298     /// \brief Global (loaded or local) ID for a preprocessed entity.
299     /// Negative values are used to indicate preprocessed entities
300     /// loaded from the external source while non-negative values are used to
301     /// indicate preprocessed entities introduced by the current preprocessor.
302     /// If M is the number of loaded preprocessed entities, value -M
303     /// corresponds to element 0 in the loaded entities vector, position -M+1
304     /// corresponds to element 1 in the loaded entities vector, etc.
305     typedef int PPEntityID;
306
307     PPEntityID getPPEntityID(unsigned Index, bool isLoaded) const {
308       return isLoaded ? PPEntityID(Index) - LoadedPreprocessedEntities.size()
309                       : Index;
310     }
311
312     /// \brief Mapping from MacroInfo structures to their definitions.
313     llvm::DenseMap<const MacroInfo *, PPEntityID> MacroDefinitions;
314
315     /// \brief External source of preprocessed entities.
316     ExternalPreprocessingRecordSource *ExternalSource;
317
318     /// \brief Retrieve the preprocessed entity at the given ID.
319     PreprocessedEntity *getPreprocessedEntity(PPEntityID PPID);
320
321     /// \brief Retrieve the loaded preprocessed entity at the given index.
322     PreprocessedEntity *getLoadedPreprocessedEntity(unsigned Index);
323     
324     /// \brief Determine the number of preprocessed entities that were
325     /// loaded (or can be loaded) from an external source.
326     unsigned getNumLoadedPreprocessedEntities() const {
327       return LoadedPreprocessedEntities.size();
328     }
329
330     /// \brief Returns a pair of [Begin, End) indices of local preprocessed
331     /// entities that \arg Range encompasses.
332     std::pair<unsigned, unsigned>
333       findLocalPreprocessedEntitiesInRange(SourceRange Range) const;
334     unsigned findBeginLocalPreprocessedEntity(SourceLocation Loc) const;
335     unsigned findEndLocalPreprocessedEntity(SourceLocation Loc) const;
336
337     /// \brief Allocate space for a new set of loaded preprocessed entities.
338     ///
339     /// \returns The index into the set of loaded preprocessed entities, which
340     /// corresponds to the first newly-allocated entity.
341     unsigned allocateLoadedEntities(unsigned NumEntities);
342
343     /// \brief Register a new macro definition.
344     void RegisterMacroDefinition(MacroInfo *Macro, PPEntityID PPID);
345     
346   public:
347     /// \brief Construct a new preprocessing record.
348     PreprocessingRecord(SourceManager &SM, bool IncludeNestedMacroExpansions);
349     
350     /// \brief Allocate memory in the preprocessing record.
351     void *Allocate(unsigned Size, unsigned Align = 8) {
352       return BumpAlloc.Allocate(Size, Align);
353     }
354     
355     /// \brief Deallocate memory in the preprocessing record.
356     void Deallocate(void *Ptr) { }
357
358     size_t getTotalMemory() const;
359
360     SourceManager &getSourceManager() const { return SourceMgr; }
361
362     // Iteration over the preprocessed entities.
363     class iterator {
364       PreprocessingRecord *Self;
365       
366       /// \brief Position within the preprocessed entity sequence.
367       ///
368       /// In a complete iteration, the Position field walks the range [-M, N),
369       /// where negative values are used to indicate preprocessed entities
370       /// loaded from the external source while non-negative values are used to
371       /// indicate preprocessed entities introduced by the current preprocessor.
372       /// However, to provide iteration in source order (for, e.g., chained
373       /// precompiled headers), dereferencing the iterator flips the negative
374       /// values (corresponding to loaded entities), so that position -M 
375       /// corresponds to element 0 in the loaded entities vector, position -M+1
376       /// corresponds to element 1 in the loaded entities vector, etc. This
377       /// gives us a reasonably efficient, source-order walk.
378       PPEntityID Position;
379       
380     public:
381       typedef PreprocessedEntity *value_type;
382       typedef value_type&         reference;
383       typedef value_type*         pointer;
384       typedef std::random_access_iterator_tag iterator_category;
385       typedef int                 difference_type;
386       
387       iterator() : Self(0), Position(0) { }
388       
389       iterator(PreprocessingRecord *Self, int Position) 
390         : Self(Self), Position(Position) { }
391       
392       value_type operator*() const {
393         return Self->getPreprocessedEntity(Position);
394       }
395       
396       value_type operator[](difference_type D) {
397         return *(*this + D);
398       }
399       
400       iterator &operator++() {
401         ++Position;
402         return *this;
403       }
404       
405       iterator operator++(int) {
406         iterator Prev(*this);
407         ++Position;
408         return Prev;
409       }
410
411       iterator &operator--() {
412         --Position;
413         return *this;
414       }
415       
416       iterator operator--(int) {
417         iterator Prev(*this);
418         --Position;
419         return Prev;
420       }
421
422       friend bool operator==(const iterator &X, const iterator &Y) {
423         return X.Position == Y.Position;
424       }
425
426       friend bool operator!=(const iterator &X, const iterator &Y) {
427         return X.Position != Y.Position;
428       }
429       
430       friend bool operator<(const iterator &X, const iterator &Y) {
431         return X.Position < Y.Position;
432       }
433
434       friend bool operator>(const iterator &X, const iterator &Y) {
435         return X.Position > Y.Position;
436       }
437
438       friend bool operator<=(const iterator &X, const iterator &Y) {
439         return X.Position < Y.Position;
440       }
441       
442       friend bool operator>=(const iterator &X, const iterator &Y) {
443         return X.Position > Y.Position;
444       }
445
446       friend iterator& operator+=(iterator &X, difference_type D) {
447         X.Position += D;
448         return X;
449       }
450
451       friend iterator& operator-=(iterator &X, difference_type D) {
452         X.Position -= D;
453         return X;
454       }
455
456       friend iterator operator+(iterator X, difference_type D) {
457         X.Position += D;
458         return X;
459       }
460
461       friend iterator operator+(difference_type D, iterator X) {
462         X.Position += D;
463         return X;
464       }
465
466       friend difference_type operator-(const iterator &X, const iterator &Y) {
467         return X.Position - Y.Position;
468       }
469
470       friend iterator operator-(iterator X, difference_type D) {
471         X.Position -= D;
472         return X;
473       }
474     };
475     friend class iterator;
476
477     /// \brief Begin iterator for all preprocessed entities.
478     iterator begin() {
479       return iterator(this, -(int)LoadedPreprocessedEntities.size());
480     }
481
482     /// \brief End iterator for all preprocessed entities.
483     iterator end() {
484       return iterator(this, PreprocessedEntities.size());
485     }
486
487     /// \brief Begin iterator for local, non-loaded, preprocessed entities.
488     iterator local_begin() {
489       return iterator(this, 0);
490     }
491
492     /// \brief End iterator for local, non-loaded, preprocessed entities.
493     iterator local_end() {
494       return iterator(this, PreprocessedEntities.size());
495     }
496
497     /// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
498     /// that source range \arg R encompasses.
499     std::pair<iterator, iterator> getPreprocessedEntitiesInRange(SourceRange R);
500
501     /// \brief Add a new preprocessed entity to this record.
502     void addPreprocessedEntity(PreprocessedEntity *Entity);
503     
504     /// \brief Set the external source for preprocessed entities.
505     void SetExternalSource(ExternalPreprocessingRecordSource &Source);
506
507     /// \brief Retrieve the external source for preprocessed entities.
508     ExternalPreprocessingRecordSource *getExternalSource() const {
509       return ExternalSource;
510     }
511     
512     /// \brief Retrieve the macro definition that corresponds to the given
513     /// \c MacroInfo.
514     MacroDefinition *findMacroDefinition(const MacroInfo *MI);
515         
516     virtual void MacroExpands(const Token &Id, const MacroInfo* MI,
517                               SourceRange Range);
518     virtual void MacroDefined(const Token &Id, const MacroInfo *MI);
519     virtual void MacroUndefined(const Token &Id, const MacroInfo *MI);
520     virtual void InclusionDirective(SourceLocation HashLoc,
521                                     const Token &IncludeTok,
522                                     StringRef FileName,
523                                     bool IsAngled,
524                                     const FileEntry *File,
525                                     SourceLocation EndLoc,
526                                     StringRef SearchPath,
527                                     StringRef RelativePath);
528
529     friend class ASTReader;
530     friend class ASTWriter;
531   };
532 } // end namespace clang
533
534 inline void* operator new(size_t bytes, clang::PreprocessingRecord& PR,
535                           unsigned alignment) throw() {
536   return PR.Allocate(bytes, alignment);
537 }
538
539 inline void operator delete(void* ptr, clang::PreprocessingRecord& PR,
540                             unsigned) throw() {
541   PR.Deallocate(ptr);
542 }
543
544 #endif // LLVM_CLANG_LEX_PREPROCESSINGRECORD_H