SourceLocation.h revision 7ef5c27eb6e8ebe58b52013246c06753c3613263
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
20namespace llvm {
21  class MemoryBuffer;
22  class raw_ostream;
23  class StringRef;
24  template <typename T> struct DenseMapInfo;
25  template <typename T> struct isPodLike;
26}
27
28namespace clang {
29
30class SourceManager;
31
32/// FileID - This is an opaque identifier used by SourceManager which refers to
33/// a source file (MemoryBuffer) along with its #include path and #line data.
34///
35class FileID {
36  /// ID - Opaque identifier, 0 is "invalid".
37  unsigned ID;
38public:
39  FileID() : ID(0) {}
40
41  bool isInvalid() const { return ID == 0; }
42
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 ID <= RHS.ID; }
46  bool operator!=(const FileID &RHS) const { return !(*this == RHS); }
47  bool operator>(const FileID &RHS) const { return RHS < *this; }
48  bool operator>=(const FileID &RHS) const { return RHS <= *this; }
49
50  static FileID getSentinel() { return get(~0U); }
51  unsigned getHashValue() const { return ID; }
52
53private:
54  friend class SourceManager;
55  static FileID get(unsigned V) {
56    FileID F;
57    F.ID = V;
58    return F;
59  }
60  unsigned getOpaqueValue() const { return ID; }
61};
62
63
64/// SourceLocation - This is a carefully crafted 32-bit identifier that encodes
65/// a full include stack, line and column number information for a position in
66/// an input translation unit.
67class SourceLocation {
68  unsigned ID;
69  friend class SourceManager;
70  enum {
71    MacroIDBit = 1U << 31
72  };
73public:
74
75  SourceLocation() : ID(0) {}  // 0 is an invalid FileID.
76
77  bool isFileID() const  { return (ID & MacroIDBit) == 0; }
78  bool isMacroID() const { return (ID & MacroIDBit) != 0; }
79
80  /// isValid - Return true if this is a valid SourceLocation object.  Invalid
81  /// SourceLocations are often used when events have no corresponding location
82  /// in the source (e.g. a diagnostic is required for a command line option).
83  ///
84  bool isValid() const { return ID != 0; }
85  bool isInvalid() const { return ID == 0; }
86
87private:
88  /// getOffset - Return the index for SourceManager's SLocEntryTable table,
89  /// note that this is not an index *into* it though.
90  unsigned getOffset() const {
91    return ID & ~MacroIDBit;
92  }
93
94  static SourceLocation getFileLoc(unsigned ID) {
95    assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
96    SourceLocation L;
97    L.ID = ID;
98    return L;
99  }
100
101  static SourceLocation getMacroLoc(unsigned ID) {
102    assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
103    SourceLocation L;
104    L.ID = MacroIDBit | ID;
105    return L;
106  }
107public:
108
109  /// getFileLocWithOffset - Return a source location with the specified offset
110  /// from this file SourceLocation.
111  SourceLocation getFileLocWithOffset(int Offset) const {
112    assert(((getOffset()+Offset) & MacroIDBit) == 0 && "invalid location");
113    SourceLocation L;
114    L.ID = ID+Offset;
115    return L;
116  }
117
118  /// getRawEncoding - When a SourceLocation itself cannot be used, this returns
119  /// an (opaque) 32-bit integer encoding for it.  This should only be passed
120  /// to SourceLocation::getFromRawEncoding, it should not be inspected
121  /// directly.
122  unsigned getRawEncoding() const { return ID; }
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
136inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
137  return LHS.getRawEncoding() == RHS.getRawEncoding();
138}
139
140inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
141  return !(LHS == RHS);
142}
143
144inline 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.
149class SourceRange {
150  SourceLocation B;
151  SourceLocation E;
152public:
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  bool isInvalid() const { return !isValid(); }
165
166  bool operator==(const SourceRange &X) const {
167    return B == X.B && E == X.E;
168  }
169
170  bool operator!=(const SourceRange &X) const {
171    return B != X.B || E != X.E;
172  }
173};
174
175/// CharSourceRange - This class represents a character granular source range.
176/// The underlying SourceRange can either specify the starting/ending character
177/// of the range, or it can specify the start or the range and the start of the
178/// last token of the range (a "token range").  In the token range case, the
179/// size of the last token must be measured to determine the actual end of the
180/// range.
181class CharSourceRange {
182  SourceRange Range;
183  bool IsTokenRange;
184public:
185  CharSourceRange() : IsTokenRange(false) {}
186  CharSourceRange(SourceRange R, bool ITR) : Range(R),IsTokenRange(ITR){}
187
188  static CharSourceRange getTokenRange(SourceRange R) {
189    CharSourceRange Result;
190    Result.Range = R;
191    Result.IsTokenRange = true;
192    return Result;
193  }
194
195  static CharSourceRange getCharRange(SourceRange R) {
196    CharSourceRange Result;
197    Result.Range = R;
198    Result.IsTokenRange = false;
199    return Result;
200  }
201
202  static CharSourceRange getTokenRange(SourceLocation B, SourceLocation E) {
203    return getTokenRange(SourceRange(B, E));
204  }
205  static CharSourceRange getCharRange(SourceLocation B, SourceLocation E) {
206    return getCharRange(SourceRange(B, E));
207  }
208
209  /// isTokenRange - Return true if the end of this range specifies the start of
210  /// the last token.  Return false if the end of this range specifies the last
211  /// character in the range.
212  bool isTokenRange() const { return IsTokenRange; }
213
214  SourceLocation getBegin() const { return Range.getBegin(); }
215  SourceLocation getEnd() const { return Range.getEnd(); }
216  const SourceRange &getAsRange() const { return Range; }
217
218  void setBegin(SourceLocation b) { Range.setBegin(b); }
219  void setEnd(SourceLocation e) { Range.setEnd(e); }
220
221  bool isValid() const { return Range.isValid(); }
222  bool isInvalid() const { return !isValid(); }
223};
224
225/// FullSourceLoc - A SourceLocation and its associated SourceManager.  Useful
226/// for argument passing to functions that expect both objects.
227class FullSourceLoc : public SourceLocation {
228  const SourceManager *SrcMgr;
229public:
230  /// Creates a FullSourceLoc where isValid() returns false.
231  explicit FullSourceLoc() : SrcMgr(0) {}
232
233  explicit FullSourceLoc(SourceLocation Loc, const SourceManager &SM)
234    : SourceLocation(Loc), SrcMgr(&SM) {}
235
236  const SourceManager &getManager() const {
237    assert(SrcMgr && "SourceManager is NULL.");
238    return *SrcMgr;
239  }
240
241  FileID getFileID() const;
242
243  FullSourceLoc getInstantiationLoc() const;
244  FullSourceLoc getSpellingLoc() const;
245
246  unsigned getInstantiationLineNumber(bool *Invalid = 0) const;
247  unsigned getInstantiationColumnNumber(bool *Invalid = 0) const;
248
249  unsigned getSpellingLineNumber(bool *Invalid = 0) const;
250  unsigned getSpellingColumnNumber(bool *Invalid = 0) const;
251
252  const char *getCharacterData(bool *Invalid = 0) const;
253
254  const llvm::MemoryBuffer* getBuffer(bool *Invalid = 0) const;
255
256  /// getBufferData - Return a StringRef to the source buffer data for the
257  /// specified FileID.
258  llvm::StringRef getBufferData(bool *Invalid = 0) const;
259
260  /// getDecomposedLoc - Decompose the specified location into a raw FileID +
261  /// Offset pair.  The first element is the FileID, the second is the
262  /// offset from the start of the buffer of the location.
263  std::pair<FileID, unsigned> getDecomposedLoc() const;
264
265  bool isInSystemHeader() const;
266
267  /// Prints information about this FullSourceLoc to stderr. Useful for
268  ///  debugging.
269  void dump() const { SourceLocation::dump(*SrcMgr); }
270
271  friend inline bool
272  operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
273    return LHS.getRawEncoding() == RHS.getRawEncoding() &&
274          LHS.SrcMgr == RHS.SrcMgr;
275  }
276
277  friend inline bool
278  operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
279    return !(LHS == RHS);
280  }
281
282};
283
284/// PresumedLoc - This class represents an unpacked "presumed" location which
285/// can be presented to the user.  A 'presumed' location can be modified by
286/// #line and GNU line marker directives and is always the instantiation point
287/// of a normal location.
288///
289/// You can get a PresumedLoc from a SourceLocation with SourceManager.
290class PresumedLoc {
291  const char *Filename;
292  unsigned Line, Col;
293  SourceLocation IncludeLoc;
294public:
295  PresumedLoc() : Filename(0) {}
296  PresumedLoc(const char *FN, unsigned Ln, unsigned Co, SourceLocation IL)
297    : Filename(FN), Line(Ln), Col(Co), IncludeLoc(IL) {
298  }
299
300  /// isInvalid - Return true if this object is invalid or uninitialized. This
301  /// occurs when created with invalid source locations or when walking off
302  /// the top of a #include stack.
303  bool isInvalid() const { return Filename == 0; }
304  bool isValid() const { return Filename != 0; }
305
306  /// getFilename - Return the presumed filename of this location.  This can be
307  /// affected by #line etc.
308  const char *getFilename() const { return Filename; }
309
310  /// getLine - Return the presumed line number of this location.  This can be
311  /// affected by #line etc.
312  unsigned getLine() const { return Line; }
313
314  /// getColumn - Return the presumed column number of this location.  This can
315  /// not be affected by #line, but is packaged here for convenience.
316  unsigned getColumn() const { return Col; }
317
318  /// getIncludeLoc - Return the presumed include location of this location.
319  /// This can be affected by GNU linemarker directives.
320  SourceLocation getIncludeLoc() const { return IncludeLoc; }
321};
322
323
324}  // end namespace clang
325
326namespace llvm {
327  /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
328  /// DenseSets.
329  template <>
330  struct DenseMapInfo<clang::FileID> {
331    static inline clang::FileID getEmptyKey() {
332      return clang::FileID();
333    }
334    static inline clang::FileID getTombstoneKey() {
335      return clang::FileID::getSentinel();
336    }
337
338    static unsigned getHashValue(clang::FileID S) {
339      return S.getHashValue();
340    }
341
342    static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
343      return LHS == RHS;
344    }
345  };
346
347  template <>
348  struct isPodLike<clang::SourceLocation> { static const bool value = true; };
349  template <>
350  struct isPodLike<clang::FileID> { static const bool value = true; };
351
352}  // end namespace llvm
353
354#endif
355