SourceLocation.h revision 0b7a158d120ac8d78c114a823e17eedfec6b6658
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
20namespace llvm {
21class MemoryBuffer;
22}
23
24namespace clang {
25
26class SourceManager;
27class FileEntry;
28
29/// SourceLocation - This is a carefully crafted 32-bit identifier that encodes
30/// a full include stack, line and column number information for a position in
31/// an input translation unit.
32class SourceLocation {
33  unsigned ID;
34public:
35  enum {
36    // FileID Layout:
37    // bit 31: 0 -> FileID, 1 -> MacroID (invalid for FileID)
38    //     30...17 -> FileID of source location, index into SourceManager table.
39    FileIDBits  = 14,
40    //      0...16 -> Index into the chunk of the specified FileID.
41    FilePosBits = 32-1-FileIDBits,
42
43    // MacroID Layout:
44    // bit 31: 1 -> MacroID, 0 -> FileID (invalid for MacroID)
45
46    // bit 29,30: unused.
47
48    // bits 28...9 -> MacroID number.
49    MacroIDBits       = 20,
50    // bits 8...0  -> Macro spelling offset
51    MacroSpellingOffsBits = 9,
52
53
54    // Useful constants.
55    ChunkSize = (1 << FilePosBits)
56  };
57
58  SourceLocation() : ID(0) {}  // 0 is an invalid FileID.
59
60  bool isFileID() const { return (ID >> 31) == 0; }
61  bool isMacroID() const { return (ID >> 31) != 0; }
62
63  /// isValid - Return true if this is a valid SourceLocation object.  Invalid
64  /// SourceLocations are often used when events have no corresponding location
65  /// in the source (e.g. a diagnostic is required for a command line option).
66  ///
67  bool isValid() const { return ID != 0; }
68  bool isInvalid() const { return ID == 0; }
69
70  static SourceLocation getFileLoc(unsigned FileID, unsigned FilePos) {
71    SourceLocation L;
72    // If a FilePos is larger than (1<<FilePosBits), the SourceManager makes
73    // enough consequtive FileIDs that we have one for each chunk.
74    if (FilePos >= ChunkSize) {
75      FileID += FilePos >> FilePosBits;
76      FilePos &= ChunkSize-1;
77    }
78
79    // FIXME: Find a way to handle out of FileID bits!  Maybe MaxFileID is an
80    // escape of some sort?
81    assert(FileID < (1 << FileIDBits) && "Out of fileid's");
82
83    L.ID = (FileID << FilePosBits) | FilePos;
84    return L;
85  }
86
87  static bool isValidMacroSpellingOffs(int Val) {
88    if (Val >= 0)
89      return Val < (1 << (MacroSpellingOffsBits-1));
90    return -Val <= (1 << (MacroSpellingOffsBits-1));
91  }
92
93  static SourceLocation getMacroLoc(unsigned MacroID, int SpellingOffs) {
94    assert(MacroID < (1 << MacroIDBits) && "Too many macros!");
95    assert(isValidMacroSpellingOffs(SpellingOffs) &&"spelling offs too large!");
96
97    // Mask off sign bits.
98    SpellingOffs &= (1 << MacroSpellingOffsBits)-1;
99
100    SourceLocation L;
101    L.ID = (1 << 31) |
102           (MacroID << MacroSpellingOffsBits) |
103           SpellingOffs;
104    return L;
105  }
106
107
108  /// getFileID - Return the file identifier for this SourceLocation.  This
109  /// FileID can be used with the SourceManager object to obtain an entire
110  /// include stack for a file position reference.
111  unsigned getFileID() const {
112    assert(isFileID() && "can't get the file id of a non-file sloc!");
113    return ID >> FilePosBits;
114  }
115
116  /// getRawFilePos - Return the byte offset from the start of the file-chunk
117  /// referred to by FileID.  This method should not be used to get the offset
118  /// from the start of the file, instead you should use
119  /// SourceManager::getDecomposedFileLoc.  This method will be
120  //  incorrect for large files.
121  unsigned getRawFilePos() const {
122    assert(isFileID() && "can't get the file id of a non-file sloc!");
123    return ID & (ChunkSize-1);
124  }
125
126  unsigned getMacroID() const {
127    assert(isMacroID() && "Is not a macro id!");
128    return (ID >> MacroSpellingOffsBits) & ((1 << MacroIDBits)-1);
129  }
130
131  int getMacroSpellingOffs() const {
132    assert(isMacroID() && "Is not a macro id!");
133    int Val = ID & ((1 << MacroSpellingOffsBits)-1);
134    // Sign extend it properly.
135    unsigned ShAmt = sizeof(int)*8 - MacroSpellingOffsBits;
136    return (Val << ShAmt) >> ShAmt;
137  }
138
139  /// getFileLocWithOffset - Return a source location with the specified offset
140  /// from this file SourceLocation.
141  SourceLocation getFileLocWithOffset(int Offset) const {
142    unsigned FileID = getFileID();
143    Offset += getRawFilePos();
144    // Handle negative offsets correctly.
145    while (Offset < 0) {
146      --FileID;
147      Offset += ChunkSize;
148    }
149    return getFileLoc(FileID, Offset);
150  }
151
152  /// getRawEncoding - When a SourceLocation itself cannot be used, this returns
153  /// an (opaque) 32-bit integer encoding for it.  This should only be passed
154  /// to SourceLocation::getFromRawEncoding, it should not be inspected
155  /// directly.
156  unsigned getRawEncoding() const { return ID; }
157
158
159  bool operator<(const SourceLocation &RHS) const {
160    return ID < RHS.ID;
161  }
162
163  /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into
164  /// a real SourceLocation.
165  static SourceLocation getFromRawEncoding(unsigned Encoding) {
166    SourceLocation X;
167    X.ID = Encoding;
168    return X;
169  }
170
171  /// Emit - Emit this SourceLocation object to Bitcode.
172  void Emit(llvm::Serializer& S) const;
173
174  /// ReadVal - Read a SourceLocation object from Bitcode.
175  static SourceLocation ReadVal(llvm::Deserializer& D);
176};
177
178inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
179  return LHS.getRawEncoding() == RHS.getRawEncoding();
180}
181
182inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
183  return !(LHS == RHS);
184}
185
186/// SourceRange - a trival tuple used to represent a source range.
187class SourceRange {
188  SourceLocation B;
189  SourceLocation E;
190public:
191  SourceRange(): B(SourceLocation()), E(SourceLocation()) {}
192  SourceRange(SourceLocation loc) : B(loc), E(loc) {}
193  SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
194
195  SourceLocation getBegin() const { return B; }
196  SourceLocation getEnd() const { return E; }
197
198  void setBegin(SourceLocation b) { B = b; }
199  void setEnd(SourceLocation e) { E = e; }
200
201  bool isValid() const { return B.isValid() && E.isValid(); }
202
203  /// Emit - Emit this SourceRange object to Bitcode.
204  void Emit(llvm::Serializer& S) const;
205
206  /// ReadVal - Read a SourceRange object from Bitcode.
207  static SourceRange ReadVal(llvm::Deserializer& D);
208};
209
210/// FullSourceLoc - A SourceLocation and its associated SourceManager.  Useful
211/// for argument passing to functions that expect both objects.
212class FullSourceLoc : public SourceLocation {
213  SourceManager* SrcMgr;
214public:
215  // Creates a FullSourceLoc where isValid() returns false.
216  explicit FullSourceLoc() : SrcMgr((SourceManager*) 0) {}
217
218  explicit FullSourceLoc(SourceLocation Loc, SourceManager &SM)
219    : SourceLocation(Loc), SrcMgr(&SM) {}
220
221  SourceManager& getManager() {
222    assert (SrcMgr && "SourceManager is NULL.");
223    return *SrcMgr;
224  }
225
226  const SourceManager& getManager() const {
227    assert (SrcMgr && "SourceManager is NULL.");
228    return *SrcMgr;
229  }
230
231  FullSourceLoc getInstantiationLoc() const;
232  FullSourceLoc getSpellingLoc() const;
233  FullSourceLoc getIncludeLoc() const;
234
235  unsigned getLineNumber() const;
236  unsigned getColumnNumber() const;
237
238  unsigned getInstantiationLineNumber() const;
239  unsigned getInstantiationColumnNumber() const;
240
241  unsigned getSpellingLineNumber() const;
242  unsigned getSpellingColumnNumber() const;
243
244  const char *getCharacterData() const;
245
246  const llvm::MemoryBuffer* getBuffer() const;
247
248  const char* getSourceName() const;
249  const FileEntry* getFileEntryForLoc() const;
250
251  bool isInSystemHeader() const;
252
253  /// Prints information about this FullSourceLoc to stderr. Useful for
254  ///  debugging.
255  void dump() const;
256
257  friend inline bool
258  operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
259    return LHS.getRawEncoding() == RHS.getRawEncoding() &&
260          LHS.SrcMgr == RHS.SrcMgr;
261  }
262
263  friend inline bool
264  operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
265    return !(LHS == RHS);
266  }
267
268};
269
270}  // end namespace clang
271
272#endif
273