SourceLocation.h revision a9793ed6a77946c988ee38035baf4cde6ff2e864
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 clang {
21
22class SourceManager;
23
24/// SourceLocation - This is a carefully crafted 32-bit identifier that encodes
25/// a full include stack, line and column number information for a position in
26/// an input translation unit.
27class SourceLocation {
28  unsigned ID;
29public:
30  enum {
31    // FileID Layout:
32    // bit 31: 0 -> FileID, 1 -> MacroID (invalid for FileID)
33    //     30...17 -> FileID of source location, index into SourceManager table.
34    FileIDBits  = 14,
35    //      0...16 -> Index into the chunk of the specified FileID.
36    FilePosBits = 32-1-FileIDBits,
37
38    // MacroID Layout:
39    // bit 31: 1 -> MacroID, 0 -> FileID (invalid for MacroID)
40
41    // bit 30: 1 -> Start of macro expansion marker.
42    MacroStartOfExpansionBit = 30,
43    // bit 29: 1 -> End of macro expansion marker.
44    MacroEndOfExpansionBit = 29,
45    // bits 28...9 -> MacroID number.
46    MacroIDBits       = 20,
47    // bits 8...0  -> Macro Physical offset
48    MacroPhysOffsBits = 9,
49
50
51    // Useful constants.
52    ChunkSize = (1 << FilePosBits)
53  };
54
55  SourceLocation() : ID(0) {}  // 0 is an invalid FileID.
56
57  bool isFileID() const { return (ID >> 31) == 0; }
58  bool isMacroID() const { return (ID >> 31) != 0; }
59
60  /// isValid - Return true if this is a valid SourceLocation object.  Invalid
61  /// SourceLocations are often used when events have no corresponding location
62  /// in the source (e.g. a diagnostic is required for a command line option).
63  ///
64  bool isValid() const { return ID != 0; }
65  bool isInvalid() const { return ID == 0; }
66
67  static SourceLocation getFileLoc(unsigned FileID, unsigned FilePos) {
68    SourceLocation L;
69    // If a FilePos is larger than (1<<FilePosBits), the SourceManager makes
70    // enough consequtive FileIDs that we have one for each chunk.
71    if (FilePos >= ChunkSize) {
72      FileID += FilePos >> FilePosBits;
73      FilePos &= ChunkSize-1;
74    }
75
76    // FIXME: Find a way to handle out of FileID bits!  Maybe MaxFileID is an
77    // escape of some sort?
78    assert(FileID < (1 << FileIDBits) && "Out of fileid's");
79
80    L.ID = (FileID << FilePosBits) | FilePos;
81    return L;
82  }
83
84  static bool isValidMacroPhysOffs(int Val) {
85    if (Val >= 0)
86      return Val < (1 << (MacroPhysOffsBits-1));
87    return -Val < (1 << (MacroPhysOffsBits-1));
88  }
89
90  static SourceLocation getMacroLoc(unsigned MacroID, int PhysOffs,
91                                    bool isExpansionStart, bool isExpansionEnd){
92    assert(MacroID < (1 << MacroIDBits) && "Too many macros!");
93    assert(isValidMacroPhysOffs(PhysOffs) && "Physoffs too large!");
94
95    // Mask off sign bits.
96    PhysOffs &= (1 << MacroPhysOffsBits)-1;
97
98    SourceLocation L;
99    L.ID = (1 << 31) |
100           (isExpansionStart << MacroStartOfExpansionBit) |
101           (isExpansionEnd << MacroEndOfExpansionBit) |
102           (MacroID << MacroPhysOffsBits) |
103           PhysOffs;
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::getFilePos.  This method will be incorrect for large files.
120  unsigned getRawFilePos() const {
121    assert(isFileID() && "can't get the file id of a non-file sloc!");
122    return ID & (ChunkSize-1);
123  }
124
125  unsigned getMacroID() const {
126    assert(isMacroID() && "Is not a macro id!");
127    return (ID >> MacroPhysOffsBits) & ((1 << MacroIDBits)-1);
128  }
129
130  int getMacroPhysOffs() const {
131    assert(isMacroID() && "Is not a macro id!");
132    int Val = ID & ((1 << MacroPhysOffsBits)-1);
133    // Sign extend it properly.
134    unsigned ShAmt = sizeof(int)*8 - MacroPhysOffsBits;
135    return (Val << ShAmt) >> ShAmt;
136  }
137
138  /// getFileLocWithOffset - Return a source location with the specified offset
139  /// from this file SourceLocation.
140  SourceLocation getFileLocWithOffset(int Offset) const {
141    unsigned FileID = getFileID();
142    Offset += getRawFilePos();
143    // Handle negative offsets correctly.
144    while (Offset < 0) {
145      --FileID;
146      Offset += ChunkSize;
147    }
148    return getFileLoc(FileID, Offset);
149  }
150
151  /// getRawEncoding - When a SourceLocation itself cannot be used, this returns
152  /// an (opaque) 32-bit integer encoding for it.  This should only be passed
153  /// to SourceLocation::getFromRawEncoding, it should not be inspected
154  /// directly.
155  unsigned getRawEncoding() const { return ID; }
156
157  /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into
158  /// a real SourceLocation.
159  static SourceLocation getFromRawEncoding(unsigned Encoding) {
160    SourceLocation X;
161    X.ID = Encoding;
162    return X;
163  }
164
165  /// Emit - Emit this SourceLocation object to Bitcode.
166  void Emit(llvm::Serializer& S) const;
167
168  /// ReadVal - Read a SourceLocation object from Bitcode.
169  static SourceLocation ReadVal(llvm::Deserializer& D);
170};
171
172inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
173  return LHS.getRawEncoding() == RHS.getRawEncoding();
174}
175
176inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
177  return !(LHS == RHS);
178}
179
180/// SourceRange - a trival tuple used to represent a source range.
181class SourceRange {
182  SourceLocation B;
183  SourceLocation E;
184public:
185  SourceRange(): B(SourceLocation()), E(SourceLocation()) {}
186  SourceRange(SourceLocation loc) : B(loc), E(loc) {}
187  SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
188
189  SourceLocation getBegin() const { return B; }
190  SourceLocation getEnd() const { return E; }
191
192  void setBegin(SourceLocation b) { B = b; }
193  void setEnd(SourceLocation e) { E = e; }
194
195  bool isValid() const { return B.isValid() && E.isValid(); }
196
197  /// Emit - Emit this SourceRange object to Bitcode.
198  void Emit(llvm::Serializer& S) const;
199
200  /// ReadVal - Read a SourceRange object from Bitcode.
201  static SourceRange ReadVal(llvm::Deserializer& D);
202};
203
204/// FullContextSourceLocation - A tuple containing both a SourceLocation
205///  and its associated SourceManager.  Useful for argument passing to functions
206///  that expect both objects.
207class FullContextSourceLocation {
208  SourceLocation Loc;
209  SourceManager* SrcMgr;
210public:
211  explicit FullContextSourceLocation(SourceLocation loc)
212    : Loc(loc), SrcMgr(NULL) {}
213
214  explicit FullContextSourceLocation(SourceLocation loc, SourceManager& smgr)
215    : Loc(loc), SrcMgr(&smgr) {}
216
217  bool isValid() { return Loc.isValid(); }
218
219  SourceLocation getSourceLocation() const { return Loc; }
220  operator SourceLocation() const { return Loc; }
221
222  SourceManager& getSourceManager() {
223    assert (SrcMgr && "SourceManager is NULL.");
224    return *SrcMgr;
225  }
226
227  const SourceManager& getSourceManager() const {
228    assert (SrcMgr && "SourceManager is NULL.");
229    return *SrcMgr;
230  }
231};
232
233}  // end namespace clang
234
235#endif
236