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