]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/tools/clang/include/clang/Basic/SourceLocation.h
Copy head to stable/9 as part of 9.0-RELEASE release cycle.
[FreeBSD/stable/9.git] / contrib / llvm / tools / clang / include / clang / Basic / SourceLocation.h
1 //===--- SourceLocation.h - Compact identifier for Source Files -*- 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 SourceLocation class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CLANG_SOURCELOCATION_H
15 #define LLVM_CLANG_SOURCELOCATION_H
16
17 #include "llvm/Support/PointerLikeTypeTraits.h"
18 #include <utility>
19 #include <functional>
20 #include <cassert>
21
22 namespace llvm {
23   class MemoryBuffer;
24   class raw_ostream;
25   class StringRef;
26   template <typename T> struct DenseMapInfo;
27   template <typename T> struct isPodLike;
28 }
29
30 namespace clang {
31
32 class SourceManager;
33
34 /// FileID - This is an opaque identifier used by SourceManager which refers to
35 /// a source file (MemoryBuffer) along with its #include path and #line data.
36 ///
37 class FileID {
38   /// ID - Opaque identifier, 0 is "invalid".
39   unsigned ID;
40 public:
41   FileID() : ID(0) {}
42
43   bool isInvalid() const { return ID == 0; }
44
45   bool operator==(const FileID &RHS) const { return ID == RHS.ID; }
46   bool operator<(const FileID &RHS) const { return ID < RHS.ID; }
47   bool operator<=(const FileID &RHS) const { return ID <= RHS.ID; }
48   bool operator!=(const FileID &RHS) const { return !(*this == RHS); }
49   bool operator>(const FileID &RHS) const { return RHS < *this; }
50   bool operator>=(const FileID &RHS) const { return RHS <= *this; }
51
52   static FileID getSentinel() { return get(~0U); }
53   unsigned getHashValue() const { return ID; }
54
55 private:
56   friend class SourceManager;
57   friend class ASTWriter;
58   friend class ASTReader;
59   
60   static FileID get(unsigned V) {
61     FileID F;
62     F.ID = V;
63     return F;
64   }
65   unsigned getOpaqueValue() const { return ID; }
66 };
67
68
69 /// SourceLocation - This is a carefully crafted 32-bit identifier that encodes
70 /// a full include stack, line and column number information for a position in
71 /// an input translation unit.
72 class SourceLocation {
73   unsigned ID;
74   friend class SourceManager;
75   enum {
76     MacroIDBit = 1U << 31
77   };
78 public:
79
80   SourceLocation() : ID(0) {}  // 0 is an invalid FileID.
81
82   bool isFileID() const  { return (ID & MacroIDBit) == 0; }
83   bool isMacroID() const { return (ID & MacroIDBit) != 0; }
84
85   /// isValid - Return true if this is a valid SourceLocation object.  Invalid
86   /// SourceLocations are often used when events have no corresponding location
87   /// in the source (e.g. a diagnostic is required for a command line option).
88   ///
89   bool isValid() const { return ID != 0; }
90   bool isInvalid() const { return ID == 0; }
91
92 private:
93   /// getOffset - Return the index for SourceManager's SLocEntryTable table,
94   /// note that this is not an index *into* it though.
95   unsigned getOffset() const {
96     return ID & ~MacroIDBit;
97   }
98
99   static SourceLocation getFileLoc(unsigned ID) {
100     assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
101     SourceLocation L;
102     L.ID = ID;
103     return L;
104   }
105
106   static SourceLocation getMacroLoc(unsigned ID) {
107     assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
108     SourceLocation L;
109     L.ID = MacroIDBit | ID;
110     return L;
111   }
112 public:
113
114   /// getFileLocWithOffset - Return a source location with the specified offset
115   /// from this file SourceLocation.
116   SourceLocation getFileLocWithOffset(int Offset) const {
117     assert(((getOffset()+Offset) & MacroIDBit) == 0 && "invalid location");
118     SourceLocation L;
119     L.ID = ID+Offset;
120     return L;
121   }
122
123   /// getRawEncoding - When a SourceLocation itself cannot be used, this returns
124   /// an (opaque) 32-bit integer encoding for it.  This should only be passed
125   /// to SourceLocation::getFromRawEncoding, it should not be inspected
126   /// directly.
127   unsigned getRawEncoding() const { return ID; }
128
129   /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into
130   /// a real SourceLocation.
131   static SourceLocation getFromRawEncoding(unsigned Encoding) {
132     SourceLocation X;
133     X.ID = Encoding;
134     return X;
135   }
136
137   /// getPtrEncoding - When a SourceLocation itself cannot be used, this returns
138   /// an (opaque) pointer encoding for it.  This should only be passed
139   /// to SourceLocation::getFromPtrEncoding, it should not be inspected
140   /// directly.
141   void* getPtrEncoding() const {
142     // Double cast to avoid a warning "cast to pointer from integer of different
143     // size".
144     return (void*)(uintptr_t)getRawEncoding();
145   }
146
147   /// getFromPtrEncoding - Turn a pointer encoding of a SourceLocation object
148   /// into a real SourceLocation.
149   static SourceLocation getFromPtrEncoding(void *Encoding) {
150     return getFromRawEncoding((unsigned)(uintptr_t)Encoding);
151   }
152
153   void print(llvm::raw_ostream &OS, const SourceManager &SM) const;
154   void dump(const SourceManager &SM) const;
155 };
156
157 inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
158   return LHS.getRawEncoding() == RHS.getRawEncoding();
159 }
160
161 inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
162   return !(LHS == RHS);
163 }
164
165 inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) {
166   return LHS.getRawEncoding() < RHS.getRawEncoding();
167 }
168
169 /// SourceRange - a trival tuple used to represent a source range.
170 class SourceRange {
171   SourceLocation B;
172   SourceLocation E;
173 public:
174   SourceRange(): B(SourceLocation()), E(SourceLocation()) {}
175   SourceRange(SourceLocation loc) : B(loc), E(loc) {}
176   SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
177
178   SourceLocation getBegin() const { return B; }
179   SourceLocation getEnd() const { return E; }
180
181   void setBegin(SourceLocation b) { B = b; }
182   void setEnd(SourceLocation e) { E = e; }
183
184   bool isValid() const { return B.isValid() && E.isValid(); }
185   bool isInvalid() const { return !isValid(); }
186
187   bool operator==(const SourceRange &X) const {
188     return B == X.B && E == X.E;
189   }
190
191   bool operator!=(const SourceRange &X) const {
192     return B != X.B || E != X.E;
193   }
194 };
195   
196 /// CharSourceRange - This class represents a character granular source range.
197 /// The underlying SourceRange can either specify the starting/ending character
198 /// of the range, or it can specify the start or the range and the start of the
199 /// last token of the range (a "token range").  In the token range case, the
200 /// size of the last token must be measured to determine the actual end of the
201 /// range.
202 class CharSourceRange { 
203   SourceRange Range;
204   bool IsTokenRange;
205 public:
206   CharSourceRange() : IsTokenRange(false) {}
207   CharSourceRange(SourceRange R, bool ITR) : Range(R),IsTokenRange(ITR){}
208
209   static CharSourceRange getTokenRange(SourceRange R) {
210     CharSourceRange Result;
211     Result.Range = R;
212     Result.IsTokenRange = true;
213     return Result;
214   }
215
216   static CharSourceRange getCharRange(SourceRange R) {
217     CharSourceRange Result;
218     Result.Range = R;
219     Result.IsTokenRange = false;
220     return Result;
221   }
222     
223   static CharSourceRange getTokenRange(SourceLocation B, SourceLocation E) {
224     return getTokenRange(SourceRange(B, E));
225   }
226   static CharSourceRange getCharRange(SourceLocation B, SourceLocation E) {
227     return getCharRange(SourceRange(B, E));
228   }
229   
230   /// isTokenRange - Return true if the end of this range specifies the start of
231   /// the last token.  Return false if the end of this range specifies the last
232   /// character in the range.
233   bool isTokenRange() const { return IsTokenRange; }
234   
235   SourceLocation getBegin() const { return Range.getBegin(); }
236   SourceLocation getEnd() const { return Range.getEnd(); }
237   const SourceRange &getAsRange() const { return Range; }
238  
239   void setBegin(SourceLocation b) { Range.setBegin(b); }
240   void setEnd(SourceLocation e) { Range.setEnd(e); }
241   
242   bool isValid() const { return Range.isValid(); }
243   bool isInvalid() const { return !isValid(); }
244 };
245
246 /// FullSourceLoc - A SourceLocation and its associated SourceManager.  Useful
247 /// for argument passing to functions that expect both objects.
248 class FullSourceLoc : public SourceLocation {
249   const SourceManager *SrcMgr;
250 public:
251   /// Creates a FullSourceLoc where isValid() returns false.
252   explicit FullSourceLoc() : SrcMgr(0) {}
253
254   explicit FullSourceLoc(SourceLocation Loc, const SourceManager &SM)
255     : SourceLocation(Loc), SrcMgr(&SM) {}
256
257   const SourceManager &getManager() const {
258     assert(SrcMgr && "SourceManager is NULL.");
259     return *SrcMgr;
260   }
261
262   FileID getFileID() const;
263
264   FullSourceLoc getInstantiationLoc() const;
265   FullSourceLoc getSpellingLoc() const;
266
267   unsigned getInstantiationLineNumber(bool *Invalid = 0) const;
268   unsigned getInstantiationColumnNumber(bool *Invalid = 0) const;
269
270   unsigned getSpellingLineNumber(bool *Invalid = 0) const;
271   unsigned getSpellingColumnNumber(bool *Invalid = 0) const;
272
273   const char *getCharacterData(bool *Invalid = 0) const;
274
275   const llvm::MemoryBuffer* getBuffer(bool *Invalid = 0) const;
276
277   /// getBufferData - Return a StringRef to the source buffer data for the
278   /// specified FileID.
279   llvm::StringRef getBufferData(bool *Invalid = 0) const;
280
281   /// getDecomposedLoc - Decompose the specified location into a raw FileID +
282   /// Offset pair.  The first element is the FileID, the second is the
283   /// offset from the start of the buffer of the location.
284   std::pair<FileID, unsigned> getDecomposedLoc() const;
285
286   bool isInSystemHeader() const;
287
288   /// \brief Determines the order of 2 source locations in the translation unit.
289   ///
290   /// \returns true if this source location comes before 'Loc', false otherwise.
291   bool isBeforeInTranslationUnitThan(SourceLocation Loc) const;
292
293   /// \brief Determines the order of 2 source locations in the translation unit.
294   ///
295   /// \returns true if this source location comes before 'Loc', false otherwise.
296   bool isBeforeInTranslationUnitThan(FullSourceLoc Loc) const {
297     assert(Loc.isValid());
298     assert(SrcMgr == Loc.SrcMgr && "Loc comes from another SourceManager!");
299     return isBeforeInTranslationUnitThan((SourceLocation)Loc);
300   }
301
302   /// \brief Comparison function class, useful for sorting FullSourceLocs.
303   struct BeforeThanCompare : public std::binary_function<FullSourceLoc,
304                                                          FullSourceLoc, bool> {
305     bool operator()(const FullSourceLoc& lhs, const FullSourceLoc& rhs) const {
306       return lhs.isBeforeInTranslationUnitThan(rhs);
307     }
308   };
309
310   /// Prints information about this FullSourceLoc to stderr. Useful for
311   ///  debugging.
312   void dump() const { SourceLocation::dump(*SrcMgr); }
313
314   friend inline bool
315   operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
316     return LHS.getRawEncoding() == RHS.getRawEncoding() &&
317           LHS.SrcMgr == RHS.SrcMgr;
318   }
319
320   friend inline bool
321   operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
322     return !(LHS == RHS);
323   }
324
325 };
326
327 /// PresumedLoc - This class represents an unpacked "presumed" location which
328 /// can be presented to the user.  A 'presumed' location can be modified by
329 /// #line and GNU line marker directives and is always the instantiation point
330 /// of a normal location.
331 ///
332 /// You can get a PresumedLoc from a SourceLocation with SourceManager.
333 class PresumedLoc {
334   const char *Filename;
335   unsigned Line, Col;
336   SourceLocation IncludeLoc;
337 public:
338   PresumedLoc() : Filename(0) {}
339   PresumedLoc(const char *FN, unsigned Ln, unsigned Co, SourceLocation IL)
340     : Filename(FN), Line(Ln), Col(Co), IncludeLoc(IL) {
341   }
342
343   /// isInvalid - Return true if this object is invalid or uninitialized. This
344   /// occurs when created with invalid source locations or when walking off
345   /// the top of a #include stack.
346   bool isInvalid() const { return Filename == 0; }
347   bool isValid() const { return Filename != 0; }
348
349   /// getFilename - Return the presumed filename of this location.  This can be
350   /// affected by #line etc.
351   const char *getFilename() const { return Filename; }
352
353   /// getLine - Return the presumed line number of this location.  This can be
354   /// affected by #line etc.
355   unsigned getLine() const { return Line; }
356
357   /// getColumn - Return the presumed column number of this location.  This can
358   /// not be affected by #line, but is packaged here for convenience.
359   unsigned getColumn() const { return Col; }
360
361   /// getIncludeLoc - Return the presumed include location of this location.
362   /// This can be affected by GNU linemarker directives.
363   SourceLocation getIncludeLoc() const { return IncludeLoc; }
364 };
365
366
367 }  // end namespace clang
368
369 namespace llvm {
370   /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
371   /// DenseSets.
372   template <>
373   struct DenseMapInfo<clang::FileID> {
374     static inline clang::FileID getEmptyKey() {
375       return clang::FileID();
376     }
377     static inline clang::FileID getTombstoneKey() {
378       return clang::FileID::getSentinel();
379     }
380
381     static unsigned getHashValue(clang::FileID S) {
382       return S.getHashValue();
383     }
384
385     static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
386       return LHS == RHS;
387     }
388   };
389   
390   template <>
391   struct isPodLike<clang::SourceLocation> { static const bool value = true; };
392   template <>
393   struct isPodLike<clang::FileID> { static const bool value = true; };
394
395   // Teach SmallPtrSet how to handle SourceLocation.
396   template<>
397   class PointerLikeTypeTraits<clang::SourceLocation> {
398   public:
399     static inline void *getAsVoidPointer(clang::SourceLocation L) {
400       return L.getPtrEncoding();
401     }
402     static inline clang::SourceLocation getFromVoidPointer(void *P) {
403       return clang::SourceLocation::getFromRawEncoding((unsigned)(uintptr_t)P);
404     }
405     enum { NumLowBitsAvailable = 0 };
406   };
407
408 }  // end namespace llvm
409
410 #endif