]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/clang/Basic/SourceLocation.h
Update clang to r84119.
[FreeBSD/FreeBSD.git] / 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 <utility>
18 #include <cassert>
19
20 namespace llvm {
21   class MemoryBuffer;
22   class raw_ostream;
23   template <typename T> struct DenseMapInfo;
24 }
25
26 namespace clang {
27
28 class SourceManager;
29 class FileEntry;
30
31 /// FileID - This is an opaque identifier used by SourceManager which refers to
32 /// a source file (MemoryBuffer) along with its #include path and #line data.
33 ///
34 class FileID {
35   /// ID - Opaque identifier, 0 is "invalid".
36   unsigned ID;
37 public:
38   FileID() : ID(0) {}
39
40   bool isInvalid() const { return ID == 0; }
41
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 ID <= RHS.ID; }
45   bool operator!=(const FileID &RHS) const { return !(*this == RHS); }
46   bool operator>(const FileID &RHS) const { return RHS < *this; }
47   bool operator>=(const FileID &RHS) const { return RHS <= *this; }
48
49   static FileID getSentinel() { return get(~0U); }
50   unsigned getHashValue() const { return ID; }
51
52 private:
53   friend class SourceManager;
54   static FileID get(unsigned V) {
55     FileID F;
56     F.ID = V;
57     return F;
58   }
59   unsigned getOpaqueValue() const { return ID; }
60 };
61
62
63 /// SourceLocation - This is a carefully crafted 32-bit identifier that encodes
64 /// a full include stack, line and column number information for a position in
65 /// an input translation unit.
66 class SourceLocation {
67   unsigned ID;
68   friend class SourceManager;
69   enum {
70     MacroIDBit = 1U << 31
71   };
72 public:
73
74   SourceLocation() : ID(0) {}  // 0 is an invalid FileID.
75
76   bool isFileID() const  { return (ID & MacroIDBit) == 0; }
77   bool isMacroID() const { return (ID & MacroIDBit) != 0; }
78
79   /// isValid - Return true if this is a valid SourceLocation object.  Invalid
80   /// SourceLocations are often used when events have no corresponding location
81   /// in the source (e.g. a diagnostic is required for a command line option).
82   ///
83   bool isValid() const { return ID != 0; }
84   bool isInvalid() const { return ID == 0; }
85
86 private:
87   /// getOffset - Return the index for SourceManager's SLocEntryTable table,
88   /// note that this is not an index *into* it though.
89   unsigned getOffset() const {
90     return ID & ~MacroIDBit;
91   }
92
93   static SourceLocation getFileLoc(unsigned ID) {
94     assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
95     SourceLocation L;
96     L.ID = ID;
97     return L;
98   }
99
100   static SourceLocation getMacroLoc(unsigned ID) {
101     assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
102     SourceLocation L;
103     L.ID = MacroIDBit | ID;
104     return L;
105   }
106 public:
107
108   /// getFileLocWithOffset - Return a source location with the specified offset
109   /// from this file SourceLocation.
110   SourceLocation getFileLocWithOffset(int Offset) const {
111     assert(((getOffset()+Offset) & MacroIDBit) == 0 && "invalid location");
112     SourceLocation L;
113     L.ID = ID+Offset;
114     return L;
115   }
116
117   /// getRawEncoding - When a SourceLocation itself cannot be used, this returns
118   /// an (opaque) 32-bit integer encoding for it.  This should only be passed
119   /// to SourceLocation::getFromRawEncoding, it should not be inspected
120   /// directly.
121   unsigned getRawEncoding() const { return ID; }
122
123
124   /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into
125   /// a real SourceLocation.
126   static SourceLocation getFromRawEncoding(unsigned Encoding) {
127     SourceLocation X;
128     X.ID = Encoding;
129     return X;
130   }
131
132   void print(llvm::raw_ostream &OS, const SourceManager &SM) const;
133   void dump(const SourceManager &SM) const;
134 };
135
136 inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
137   return LHS.getRawEncoding() == RHS.getRawEncoding();
138 }
139
140 inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
141   return !(LHS == RHS);
142 }
143
144 inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) {
145   return LHS.getRawEncoding() < RHS.getRawEncoding();
146 }
147
148 /// SourceRange - a trival tuple used to represent a source range.
149 class SourceRange {
150   SourceLocation B;
151   SourceLocation E;
152 public:
153   SourceRange(): B(SourceLocation()), E(SourceLocation()) {}
154   SourceRange(SourceLocation loc) : B(loc), E(loc) {}
155   SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
156
157   SourceLocation getBegin() const { return B; }
158   SourceLocation getEnd() const { return E; }
159
160   void setBegin(SourceLocation b) { B = b; }
161   void setEnd(SourceLocation e) { E = e; }
162
163   bool isValid() const { return B.isValid() && E.isValid(); }
164
165   bool operator==(const SourceRange &X) const {
166     return B == X.B && E == X.E;
167   }
168
169   bool operator!=(const SourceRange &X) const {
170     return B != X.B || E != X.E;
171   }
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
200   unsigned getInstantiationLineNumber() const;
201   unsigned getInstantiationColumnNumber() const;
202
203   unsigned getSpellingLineNumber() const;
204   unsigned getSpellingColumnNumber() const;
205
206   const char *getCharacterData() const;
207
208   const llvm::MemoryBuffer* getBuffer() const;
209
210   /// getBufferData - Return a pointer to the start and end of the source buffer
211   /// data for the specified FileID.
212   std::pair<const char*, const char*> getBufferData() const;
213
214   /// getDecomposedLoc - Decompose the specified location into a raw FileID +
215   /// Offset pair.  The first element is the FileID, the second is the
216   /// offset from the start of the buffer of the location.
217   std::pair<FileID, unsigned> getDecomposedLoc() const;
218
219   bool isInSystemHeader() const;
220
221   /// Prints information about this FullSourceLoc to stderr. Useful for
222   ///  debugging.
223   void dump() const { SourceLocation::dump(*SrcMgr); }
224
225   friend inline bool
226   operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
227     return LHS.getRawEncoding() == RHS.getRawEncoding() &&
228           LHS.SrcMgr == RHS.SrcMgr;
229   }
230
231   friend inline bool
232   operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
233     return !(LHS == RHS);
234   }
235
236 };
237
238 /// PresumedLoc - This class represents an unpacked "presumed" location which
239 /// can be presented to the user.  A 'presumed' location can be modified by
240 /// #line and GNU line marker directives and is always the instantiation point
241 /// of a normal location.
242 ///
243 /// You can get a PresumedLoc from a SourceLocation with SourceManager.
244 class PresumedLoc {
245   const char *Filename;
246   unsigned Line, Col;
247   SourceLocation IncludeLoc;
248 public:
249   PresumedLoc() : Filename(0) {}
250   PresumedLoc(const char *FN, unsigned Ln, unsigned Co, SourceLocation IL)
251     : Filename(FN), Line(Ln), Col(Co), IncludeLoc(IL) {
252   }
253
254   /// isInvalid - Return true if this object is invalid or uninitialized. This
255   /// occurs when created with invalid source locations or when walking off
256   /// the top of a #include stack.
257   bool isInvalid() const { return Filename == 0; }
258   bool isValid() const { return Filename != 0; }
259
260   /// getFilename - Return the presumed filename of this location.  This can be
261   /// affected by #line etc.
262   const char *getFilename() const { return Filename; }
263
264   /// getLine - Return the presumed line number of this location.  This can be
265   /// affected by #line etc.
266   unsigned getLine() const { return Line; }
267
268   /// getColumn - Return the presumed column number of this location.  This can
269   /// not be affected by #line, but is packaged here for convenience.
270   unsigned getColumn() const { return Col; }
271
272   /// getIncludeLoc - Return the presumed include location of this location.
273   /// This can be affected by GNU linemarker directives.
274   SourceLocation getIncludeLoc() const { return IncludeLoc; }
275 };
276
277
278 }  // end namespace clang
279
280 namespace llvm {
281   /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
282   /// DenseSets.
283   template <>
284   struct DenseMapInfo<clang::FileID> {
285     static inline clang::FileID getEmptyKey() {
286       return clang::FileID();
287     }
288     static inline clang::FileID getTombstoneKey() {
289       return clang::FileID::getSentinel();
290     }
291
292     static unsigned getHashValue(clang::FileID S) {
293       return S.getHashValue();
294     }
295
296     static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
297       return LHS == RHS;
298     }
299
300     static bool isPod() { return true; }
301   };
302
303 }  // end namespace llvm
304
305 #endif