1 //===--- SourceLocation.h - Compact identifier for Source Files -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file defines the SourceLocation class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_SOURCELOCATION_H
15 #define LLVM_CLANG_SOURCELOCATION_H
21 /// SourceLocation - This is a carefully crafted 32-bit identifier that encodes
22 /// a full include stack, line and column number information for a position in
23 /// an input translation unit.
24 class SourceLocation {
29 FilePosBits = 32-1-FileIDBits,
32 MacroPhysOffsBits = 9,
36 SourceLocation() : ID(0) {} // 0 is an invalid FileID.
38 bool isFileID() const { return (ID >> 31) == 0; }
39 bool isMacroID() const { return (ID >> 31) != 0; }
41 static SourceLocation getFileLoc(unsigned FileID, unsigned FilePos) {
43 // If a FilePos is larger than (1<<FilePosBits), the SourceManager makes
44 // enough consequtive FileIDs that we have one for each chunk.
45 if (FilePos >= (1 << FilePosBits)) {
46 FileID += FilePos >> FilePosBits;
47 FilePos &= (1 << FilePosBits)-1;
50 // FIXME: Find a way to handle out of FileID bits! Maybe MaxFileID is an
51 // escape of some sort?
52 assert(FileID < (1 << FileIDBits) && "Out of fileid's");
54 L.ID = (FileID << FilePosBits) | FilePos;
58 static bool isValidMacroPhysOffs(int Val) {
60 return Val < (1 << (MacroPhysOffsBits-1));
61 return -Val < (1 << (MacroPhysOffsBits-1));
64 static SourceLocation getMacroLoc(unsigned MacroID, int PhysOffs,
66 assert(MacroID < (1 << MacroIDBits) && "Too many macros!");
67 assert(isValidMacroPhysOffs(PhysOffs) && "Physoffs too large!");
68 assert(LogOffs < (1 << MacroLogOffBits) && "Logical offs too large!");
70 PhysOffs &= (1 << MacroPhysOffsBits)-1;
73 L.ID = (1 << 31) | (MacroID << (MacroPhysOffsBits+MacroLogOffBits)) |
74 (PhysOffs << MacroLogOffBits) |
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).
84 bool isValid() const { return ID != 0; }
85 bool isInvalid() const { return ID == 0; }
87 /// getFileID - Return the file identifier for this SourceLocation. This
88 /// FileID can be used with the SourceManager object to obtain an entire
89 /// include stack for a file position reference.
90 unsigned getFileID() const {
91 assert(isFileID() && "can't get the file id of a non-file sloc!");
92 return ID >> FilePosBits;
95 /// getRawFilePos - Return the byte offset from the start of the file-chunk
96 /// referred to by FileID. This method should not be used to get the offset
97 /// from the start of the file, instead you should use
98 /// SourceManager::getFilePos. This method will be incorrect for large files.
99 unsigned getRawFilePos() const {
100 assert(isFileID() && "can't get the file id of a non-file sloc!");
101 return ID & ((1 << FilePosBits)-1);
104 unsigned getMacroID() const {
105 assert(isMacroID() && "Is not a macro id!");
106 return (ID >> (MacroPhysOffsBits+MacroLogOffBits)) & ((1 << MacroIDBits)-1);
109 int getMacroPhysOffs() const {
110 assert(isMacroID() && "Is not a macro id!");
111 int Val = (ID >> MacroLogOffBits) & ((1 << MacroPhysOffsBits)-1);
112 // Sign extend it properly.
113 unsigned ShAmt = sizeof(int)*8 - MacroPhysOffsBits;
114 return (Val << ShAmt) >> ShAmt;
117 unsigned getMacroLogOffs() const {
118 assert(isMacroID() && "Is not a macro id!");
119 return ID & ((1 << MacroLogOffBits)-1);
122 /// getFileLocWithOffset - Return a source location with the specified offset
123 /// from this file SourceLocation.
124 SourceLocation getFileLocWithOffset(int Offset) const {
125 return getFileLoc(getFileID(), getRawFilePos()+Offset);
128 /// getRawEncoding - When a SourceLocation itself cannot be used, this returns
129 /// an (opaque) 32-bit integer encoding for it. This should only be passed
130 /// to SourceLocation::getFromRawEncoding, it should not be inspected
132 unsigned getRawEncoding() const { return ID; }
134 /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into
135 /// a real SourceLocation.
136 static SourceLocation getFromRawEncoding(unsigned Encoding) {
143 inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
144 return LHS.getRawEncoding() == RHS.getRawEncoding();
147 inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
148 return !(LHS == RHS);
151 /// SourceRange - a trival tuple used to represent a source range.
156 SourceRange(): B(SourceLocation()), E(SourceLocation()) {}
157 SourceRange(SourceLocation loc) : B(loc), E(loc) {}
158 SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
160 SourceLocation Begin() const { return B; }
161 SourceLocation End() const { return E; }
163 void setBegin(SourceLocation b) { B = b; }
164 void setEnd(SourceLocation e) { E = e; }
166 bool isValid() const { return B.isValid() && E.isValid(); }
169 } // end namespace clang