SourceLocation.h revision 321abd4583dc02a254489132c5ccbe125d18ce4f
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/Bitcode/SerializationFwd.h"
18#include <utility>
19#include <cassert>
20
21namespace llvm {
22  class MemoryBuffer;
23  class raw_ostream;
24  template <typename T> struct DenseMapInfo;
25}
26
27namespace clang {
28
29class SourceManager;
30class FileEntry;
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
125  /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into
126  /// a real SourceLocation.
127  static SourceLocation getFromRawEncoding(unsigned Encoding) {
128    SourceLocation X;
129    X.ID = Encoding;
130    return X;
131  }
132
133  /// Emit - Emit this SourceLocation object to Bitcode.
134  void Emit(llvm::Serializer& S) const;
135
136  /// ReadVal - Read a SourceLocation object from Bitcode.
137  static SourceLocation ReadVal(llvm::Deserializer& D);
138
139  void print(llvm::raw_ostream &OS, const SourceManager &SM) const;
140  void dump(const SourceManager &SM) const;
141};
142
143inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
144  return LHS.getRawEncoding() == RHS.getRawEncoding();
145}
146
147inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
148  return !(LHS == RHS);
149}
150
151inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) {
152  return LHS.getRawEncoding() < RHS.getRawEncoding();
153}
154
155/// SourceRange - a trival tuple used to represent a source range.
156class SourceRange {
157  SourceLocation B;
158  SourceLocation E;
159public:
160  SourceRange(): B(SourceLocation()), E(SourceLocation()) {}
161  SourceRange(SourceLocation loc) : B(loc), E(loc) {}
162  SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
163
164  SourceLocation getBegin() const { return B; }
165  SourceLocation getEnd() const { return E; }
166
167  void setBegin(SourceLocation b) { B = b; }
168  void setEnd(SourceLocation e) { E = e; }
169
170  bool isValid() const { return B.isValid() && E.isValid(); }
171
172  /// Emit - Emit this SourceRange object to Bitcode.
173  void Emit(llvm::Serializer& S) const;
174
175  /// ReadVal - Read a SourceRange object from Bitcode.
176  static SourceRange ReadVal(llvm::Deserializer& D);
177};
178
179/// FullSourceLoc - A SourceLocation and its associated SourceManager.  Useful
180/// for argument passing to functions that expect both objects.
181class FullSourceLoc : public SourceLocation {
182  SourceManager* SrcMgr;
183public:
184  /// Creates a FullSourceLoc where isValid() returns false.
185  explicit FullSourceLoc() : SrcMgr((SourceManager*) 0) {}
186
187  explicit FullSourceLoc(SourceLocation Loc, SourceManager &SM)
188    : SourceLocation(Loc), SrcMgr(&SM) {}
189
190  SourceManager &getManager() {
191    assert(SrcMgr && "SourceManager is NULL.");
192    return *SrcMgr;
193  }
194
195  const SourceManager &getManager() const {
196    assert(SrcMgr && "SourceManager is NULL.");
197    return *SrcMgr;
198  }
199
200  FileID getFileID() const;
201
202  FullSourceLoc getInstantiationLoc() const;
203  FullSourceLoc getSpellingLoc() const;
204
205  unsigned getInstantiationLineNumber() const;
206  unsigned getInstantiationColumnNumber() const;
207
208  unsigned getSpellingLineNumber() const;
209  unsigned getSpellingColumnNumber() const;
210
211  const char *getCharacterData() const;
212
213  const llvm::MemoryBuffer* getBuffer() const;
214
215  /// getBufferData - Return a pointer to the start and end of the source buffer
216  /// data for the specified FileID.
217  std::pair<const char*, const char*> getBufferData() const;
218
219  /// getDecomposedLoc - Decompose the specified location into a raw FileID +
220  /// Offset pair.  The first element is the FileID, the second is the
221  /// offset from the start of the buffer of the location.
222  std::pair<FileID, unsigned> getDecomposedLoc() const;
223
224  bool isInSystemHeader() const;
225
226  /// Prints information about this FullSourceLoc to stderr. Useful for
227  ///  debugging.
228  void dump() const { SourceLocation::dump(*SrcMgr); }
229
230  friend inline bool
231  operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
232    return LHS.getRawEncoding() == RHS.getRawEncoding() &&
233          LHS.SrcMgr == RHS.SrcMgr;
234  }
235
236  friend inline bool
237  operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
238    return !(LHS == RHS);
239  }
240
241};
242
243/// PresumedLoc - This class represents an unpacked "presumed" location which
244/// can be presented to the user.  A 'presumed' location can be modified by
245/// #line and GNU line marker directives and is always the instantiation point
246/// of a normal location.
247///
248/// You can get a PresumedLoc from a SourceLocation with SourceManager.
249class PresumedLoc {
250  const char *Filename;
251  unsigned Line, Col;
252  SourceLocation IncludeLoc;
253public:
254  PresumedLoc() : Filename(0) {}
255  PresumedLoc(const char *FN, unsigned Ln, unsigned Co, SourceLocation IL)
256    : Filename(FN), Line(Ln), Col(Co), IncludeLoc(IL) {
257  }
258
259  /// isInvalid - Return true if this object is invalid or uninitialized. This
260  /// occurs when created with invalid source locations or when walking off
261  /// the top of a #include stack.
262  bool isInvalid() const { return Filename == 0; }
263  bool isValid() const { return Filename != 0; }
264
265  /// getFilename - Return the presumed filename of this location.  This can be
266  /// affected by #line etc.
267  const char *getFilename() const { return Filename; }
268
269  /// getLine - Return the presumed line number of this location.  This can be
270  /// affected by #line etc.
271  unsigned getLine() const { return Line; }
272
273  /// getColumn - Return the presumed column number of this location.  This can
274  /// not be affected by #line, but is packaged here for convenience.
275  unsigned getColumn() const { return Col; }
276
277  /// getIncludeLoc - Return the presumed include location of this location.
278  /// This can be affected by GNU linemarker directives.
279  SourceLocation getIncludeLoc() const { return IncludeLoc; }
280};
281
282
283}  // end namespace clang
284
285namespace llvm {
286  /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
287  /// DenseSets.
288  template <>
289  struct DenseMapInfo<clang::FileID> {
290    static inline clang::FileID getEmptyKey() {
291      return clang::FileID();
292    }
293    static inline clang::FileID getTombstoneKey() {
294      return clang::FileID::getSentinel();
295    }
296
297    static unsigned getHashValue(clang::FileID S) {
298      return S.getHashValue();
299    }
300
301    static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
302      return LHS == RHS;
303    }
304
305    static bool isPod() { return true; }
306  };
307
308}  // end namespace llvm
309
310#endif
311