SourceLocation.h revision 9c728dc4d8da89c73fcae74c9e72d7a83ffd7b6d
1//===--- SourceLocation.h - Compact identifier for Source Files -*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source 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 30: 1 -> Start of macro expansion marker.
47    MacroStartOfExpansionBit = 30,
48    // bit 29: 1 -> End of macro expansion marker.
49    MacroEndOfExpansionBit = 29,
50    // bits 28...9 -> MacroID number.
51    MacroIDBits       = 20,
52    // bits 8...0  -> Macro Physical offset
53    MacroPhysOffsBits = 9,
54
55
56    // Useful constants.
57    ChunkSize = (1 << FilePosBits)
58  };
59
60  SourceLocation() : ID(0) {}  // 0 is an invalid FileID.
61
62  bool isFileID() const { return (ID >> 31) == 0; }
63  bool isMacroID() const { return (ID >> 31) != 0; }
64
65  /// isValid - Return true if this is a valid SourceLocation object.  Invalid
66  /// SourceLocations are often used when events have no corresponding location
67  /// in the source (e.g. a diagnostic is required for a command line option).
68  ///
69  bool isValid() const { return ID != 0; }
70  bool isInvalid() const { return ID == 0; }
71
72  static SourceLocation getFileLoc(unsigned FileID, unsigned FilePos) {
73    SourceLocation L;
74    // If a FilePos is larger than (1<<FilePosBits), the SourceManager makes
75    // enough consequtive FileIDs that we have one for each chunk.
76    if (FilePos >= ChunkSize) {
77      FileID += FilePos >> FilePosBits;
78      FilePos &= ChunkSize-1;
79    }
80
81    // FIXME: Find a way to handle out of FileID bits!  Maybe MaxFileID is an
82    // escape of some sort?
83    assert(FileID < (1 << FileIDBits) && "Out of fileid's");
84
85    L.ID = (FileID << FilePosBits) | FilePos;
86    return L;
87  }
88
89  static bool isValidMacroPhysOffs(int Val) {
90    if (Val >= 0)
91      return Val < (1 << (MacroPhysOffsBits-1));
92    return -Val < (1 << (MacroPhysOffsBits-1));
93  }
94
95  static SourceLocation getMacroLoc(unsigned MacroID, int PhysOffs,
96                                    bool isExpansionStart, bool isExpansionEnd){
97    assert(MacroID < (1 << MacroIDBits) && "Too many macros!");
98    assert(isValidMacroPhysOffs(PhysOffs) && "Physoffs too large!");
99
100    // Mask off sign bits.
101    PhysOffs &= (1 << MacroPhysOffsBits)-1;
102
103    SourceLocation L;
104    L.ID = (1 << 31) |
105           (isExpansionStart << MacroStartOfExpansionBit) |
106           (isExpansionEnd << MacroEndOfExpansionBit) |
107           (MacroID << MacroPhysOffsBits) |
108           PhysOffs;
109    return L;
110  }
111
112
113  /// getFileID - Return the file identifier for this SourceLocation.  This
114  /// FileID can be used with the SourceManager object to obtain an entire
115  /// include stack for a file position reference.
116  unsigned getFileID() const {
117    assert(isFileID() && "can't get the file id of a non-file sloc!");
118    return ID >> FilePosBits;
119  }
120
121  /// getRawFilePos - Return the byte offset from the start of the file-chunk
122  /// referred to by FileID.  This method should not be used to get the offset
123  /// from the start of the file, instead you should use
124  /// SourceManager::getFilePos.  This method will be incorrect for large files.
125  unsigned getRawFilePos() const {
126    assert(isFileID() && "can't get the file id of a non-file sloc!");
127    return ID & (ChunkSize-1);
128  }
129
130  unsigned getMacroID() const {
131    assert(isMacroID() && "Is not a macro id!");
132    return (ID >> MacroPhysOffsBits) & ((1 << MacroIDBits)-1);
133  }
134
135  int getMacroPhysOffs() const {
136    assert(isMacroID() && "Is not a macro id!");
137    int Val = ID & ((1 << MacroPhysOffsBits)-1);
138    // Sign extend it properly.
139    unsigned ShAmt = sizeof(int)*8 - MacroPhysOffsBits;
140    return (Val << ShAmt) >> ShAmt;
141  }
142
143  /// getFileLocWithOffset - Return a source location with the specified offset
144  /// from this file SourceLocation.
145  SourceLocation getFileLocWithOffset(int Offset) const {
146    unsigned FileID = getFileID();
147    Offset += getRawFilePos();
148    // Handle negative offsets correctly.
149    while (Offset < 0) {
150      --FileID;
151      Offset += ChunkSize;
152    }
153    return getFileLoc(FileID, Offset);
154  }
155
156  /// getRawEncoding - When a SourceLocation itself cannot be used, this returns
157  /// an (opaque) 32-bit integer encoding for it.  This should only be passed
158  /// to SourceLocation::getFromRawEncoding, it should not be inspected
159  /// directly.
160  unsigned getRawEncoding() const { return ID; }
161
162  /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into
163  /// a real SourceLocation.
164  static SourceLocation getFromRawEncoding(unsigned Encoding) {
165    SourceLocation X;
166    X.ID = Encoding;
167    return X;
168  }
169
170  /// Emit - Emit this SourceLocation object to Bitcode.
171  void Emit(llvm::Serializer& S) const;
172
173  /// ReadVal - Read a SourceLocation object from Bitcode.
174  static SourceLocation ReadVal(llvm::Deserializer& D);
175};
176
177inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
178  return LHS.getRawEncoding() == RHS.getRawEncoding();
179}
180
181inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
182  return !(LHS == RHS);
183}
184
185/// SourceRange - a trival tuple used to represent a source range.
186class SourceRange {
187  SourceLocation B;
188  SourceLocation E;
189public:
190  SourceRange(): B(SourceLocation()), E(SourceLocation()) {}
191  SourceRange(SourceLocation loc) : B(loc), E(loc) {}
192  SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
193
194  SourceLocation getBegin() const { return B; }
195  SourceLocation getEnd() const { return E; }
196
197  void setBegin(SourceLocation b) { B = b; }
198  void setEnd(SourceLocation e) { E = e; }
199
200  bool isValid() const { return B.isValid() && E.isValid(); }
201
202  /// Emit - Emit this SourceRange object to Bitcode.
203  void Emit(llvm::Serializer& S) const;
204
205  /// ReadVal - Read a SourceRange object from Bitcode.
206  static SourceRange ReadVal(llvm::Deserializer& D);
207};
208
209/// FullSourceLoc - A tuple containing both a SourceLocation
210///  and its associated SourceManager.  Useful for argument passing to functions
211///  that expect both objects.
212class FullSourceLoc {
213  SourceLocation Loc;
214  SourceManager* SrcMgr;
215public:
216  // Creates a FullSourceLoc where isValid() returns false.
217  explicit FullSourceLoc()
218    : Loc(SourceLocation()), SrcMgr((SourceManager*) 0) {}
219
220  explicit FullSourceLoc(SourceLocation loc, SourceManager& smgr)
221    : Loc(loc), SrcMgr(&smgr) {}
222
223  bool isValid() const { return Loc.isValid(); }
224  bool isInvalid() const { return Loc.isInvalid(); }
225
226  SourceLocation getLocation() const { return Loc; }
227
228  SourceManager& getManager() {
229    assert (SrcMgr && "SourceManager is NULL.");
230    return *SrcMgr;
231  }
232
233  const SourceManager& getManager() const {
234    assert (SrcMgr && "SourceManager is NULL.");
235    return *SrcMgr;
236  }
237
238  FullSourceLoc getLogicalLoc();
239  FullSourceLoc getIncludeLoc();
240
241  unsigned getLineNumber();
242  unsigned getColumnNumber();
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 operator==(const FullSourceLoc& RHS) const {
252    return SrcMgr == RHS.SrcMgr && Loc == RHS.Loc;
253  }
254
255  bool operator!=(const FullSourceLoc& RHS) const {
256    return SrcMgr != RHS.SrcMgr || Loc != RHS.Loc;
257  }
258};
259
260}  // end namespace clang
261
262#endif
263