]> granicus.if.org Git - clang/blob - include/clang/Basic/SourceLocation.h
Check in the long promised SourceLocation rewrite. This lays the
[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 <cassert>
18 #include "llvm/Bitcode/SerializationFwd.h"
19
20 namespace llvm {
21   class MemoryBuffer;
22   template <typename T> struct DenseMapInfo;
23 }
24
25 namespace clang {
26   
27 class SourceManager;
28 class FileEntry;
29   
30 /// FileID - This is an opaque identifier used by SourceManager which refers to
31 /// a source file (MemoryBuffer) along with its #include path and #line data.
32 ///
33 class FileID {
34   /// ID - Opaque identifier, 0 is "invalid".
35   unsigned ID;
36 public:
37   FileID() : ID(0) {}
38   
39   bool isInvalid() const { return ID == 0; }
40   
41   bool operator==(const FileID &RHS) const { return ID == RHS.ID; }
42   bool operator<(const FileID &RHS) const { return ID < RHS.ID; }
43   bool operator<=(const FileID &RHS) const { return ID <= RHS.ID; }
44   bool operator!=(const FileID &RHS) const { return !(*this == RHS); }
45   bool operator>(const FileID &RHS) const { return RHS < *this; }
46   bool operator>=(const FileID &RHS) const { return RHS <= *this; }
47   
48   static FileID getSentinel() { return get(~0U); }
49   unsigned getHashValue() const { return ID; }
50   
51 private:
52   friend class SourceManager;
53   static FileID get(unsigned V) {
54     FileID F;
55     F.ID = V;
56     return F;
57   }
58   unsigned getOpaqueValue() const { return ID; }
59 };
60   
61     
62 /// SourceLocation - This is a carefully crafted 32-bit identifier that encodes
63 /// a full include stack, line and column number information for a position in
64 /// an input translation unit.
65 class SourceLocation {
66   unsigned ID;
67   friend class SourceManager;
68   enum {
69     MacroIDBit = 1U << 31
70   };
71 public:
72
73   SourceLocation() : ID(0) {}  // 0 is an invalid FileID.
74   
75   bool isFileID() const  { return (ID & MacroIDBit) == 0; }
76   bool isMacroID() const { return (ID & MacroIDBit) != 0; }
77   
78   /// isValid - Return true if this is a valid SourceLocation object.  Invalid
79   /// SourceLocations are often used when events have no corresponding location
80   /// in the source (e.g. a diagnostic is required for a command line option).
81   ///
82   bool isValid() const { return ID != 0; }
83   bool isInvalid() const { return ID == 0; }
84   
85 private:
86   /// getOffset - Return the index for SourceManager's SLocEntryTable table,
87   /// note that this is not an index *into* it though.
88   unsigned getOffset() const {
89     return ID & ~MacroIDBit;
90   }
91
92   static SourceLocation getFileLoc(unsigned ID) {
93     assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
94     SourceLocation L;
95     L.ID = ID;
96     return L;
97   }
98   
99   static SourceLocation getMacroLoc(unsigned ID) {
100     assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
101     SourceLocation L;
102     L.ID = MacroIDBit | ID;
103     return L;
104   }
105 public:
106   
107   /// getFileLocWithOffset - Return a source location with the specified offset
108   /// from this file SourceLocation.
109   SourceLocation getFileLocWithOffset(int Offset) const {
110     assert(((getOffset()+Offset) & MacroIDBit) == 0 && "invalid location");
111     SourceLocation L;
112     L.ID = ID+Offset;
113     return L;
114   }
115   
116   /// getRawEncoding - When a SourceLocation itself cannot be used, this returns
117   /// an (opaque) 32-bit integer encoding for it.  This should only be passed
118   /// to SourceLocation::getFromRawEncoding, it should not be inspected
119   /// directly.
120   unsigned getRawEncoding() const { return ID; }
121   
122   
123   /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into
124   /// a real SourceLocation.
125   static SourceLocation getFromRawEncoding(unsigned Encoding) {
126     SourceLocation X;
127     X.ID = Encoding;
128     return X;
129   }
130   
131   /// Emit - Emit this SourceLocation object to Bitcode.
132   void Emit(llvm::Serializer& S) const;
133   
134   /// ReadVal - Read a SourceLocation object from Bitcode.
135   static SourceLocation ReadVal(llvm::Deserializer& D);
136 };
137
138 inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
139   return LHS.getRawEncoding() == RHS.getRawEncoding();
140 }
141
142 inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
143   return !(LHS == RHS);
144 }
145   
146 inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) {
147   return LHS.getRawEncoding() < RHS.getRawEncoding();
148 }
149
150 /// SourceRange - a trival tuple used to represent a source range.
151 class SourceRange {
152   SourceLocation B;
153   SourceLocation E;
154 public:
155   SourceRange(): B(SourceLocation()), E(SourceLocation()) {}
156   SourceRange(SourceLocation loc) : B(loc), E(loc) {}
157   SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
158     
159   SourceLocation getBegin() const { return B; }
160   SourceLocation getEnd() const { return E; }
161   
162   void setBegin(SourceLocation b) { B = b; }
163   void setEnd(SourceLocation e) { E = e; }
164   
165   bool isValid() const { return B.isValid() && E.isValid(); }
166   
167   /// Emit - Emit this SourceRange object to Bitcode.
168   void Emit(llvm::Serializer& S) const;    
169
170   /// ReadVal - Read a SourceRange object from Bitcode.
171   static SourceRange ReadVal(llvm::Deserializer& D);
172 };
173   
174 /// FullSourceLoc - A SourceLocation and its associated SourceManager.  Useful
175 /// for argument passing to functions that expect both objects.
176 class FullSourceLoc : public SourceLocation {
177   SourceManager* SrcMgr;
178 public:
179   // Creates a FullSourceLoc where isValid() returns false.
180   explicit FullSourceLoc() : SrcMgr((SourceManager*) 0) {}
181
182   explicit FullSourceLoc(SourceLocation Loc, SourceManager &SM) 
183     : SourceLocation(Loc), SrcMgr(&SM) {}
184     
185   SourceManager& getManager() {
186     assert (SrcMgr && "SourceManager is NULL.");
187     return *SrcMgr;
188   }
189   
190   const SourceManager& getManager() const {
191     assert (SrcMgr && "SourceManager is NULL.");
192     return *SrcMgr;
193   }
194   
195   FileID getFileID() const;
196   
197   FullSourceLoc getInstantiationLoc() const;
198   FullSourceLoc getSpellingLoc() const;
199   FullSourceLoc getIncludeLoc() const;
200
201   unsigned getLineNumber() const;
202   unsigned getColumnNumber() const;
203   
204   unsigned getInstantiationLineNumber() const;
205   unsigned getInstantiationColumnNumber() const;
206
207   unsigned getSpellingLineNumber() const;
208   unsigned getSpellingColumnNumber() const;
209
210   const char *getCharacterData() const;
211   
212   const llvm::MemoryBuffer* getBuffer() const;
213   
214   const char* getSourceName() const;
215
216   bool isInSystemHeader() const;
217   
218   /// Prints information about this FullSourceLoc to stderr. Useful for
219   ///  debugging.
220   void dump() const;
221
222   friend inline bool 
223   operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
224     return LHS.getRawEncoding() == RHS.getRawEncoding() &&
225           LHS.SrcMgr == RHS.SrcMgr;
226   }
227
228   friend inline bool 
229   operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
230     return !(LHS == RHS);
231   }
232
233 };
234  
235 }  // end namespace clang
236
237 namespace llvm {
238   /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
239   /// DenseSets.
240   template <>
241   struct DenseMapInfo<clang::FileID> {
242     static inline clang::FileID getEmptyKey() {
243       return clang::FileID();
244     }
245     static inline clang::FileID getTombstoneKey() {
246       return clang::FileID::getSentinel(); 
247     }
248     
249     static unsigned getHashValue(clang::FileID S) {
250       return S.getHashValue();
251     }
252     
253     static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
254       return LHS == RHS;
255     }
256     
257     static bool isPod() { return true; }
258   };
259   
260 }  // end namespace llvm
261
262 #endif