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
18 #include "llvm/Bitcode/SerializationFwd.h"
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.
27 class SourceLocation {
32 // bit 31: 0 -> FileID, 1 -> MacroID (invalid for FileID)
33 // 30...17 -> FileID of source location, index into SourceManager table.
35 // 0...16 -> Index into the chunk of the specified FileID.
36 FilePosBits = 32-1-FileIDBits,
39 // bit 31: 1 -> MacroID, 0 -> FileID (invalid for MacroID)
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.
47 // bits 8...0 -> Macro Physical offset
48 MacroPhysOffsBits = 9,
52 ChunkSize = (1 << FilePosBits)
55 SourceLocation() : ID(0) {} // 0 is an invalid FileID.
57 bool isFileID() const { return (ID >> 31) == 0; }
58 bool isMacroID() const { return (ID >> 31) != 0; }
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).
64 bool isValid() const { return ID != 0; }
65 bool isInvalid() const { return ID == 0; }
67 static SourceLocation getFileLoc(unsigned FileID, unsigned FilePos) {
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;
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");
80 L.ID = (FileID << FilePosBits) | FilePos;
84 static bool isValidMacroPhysOffs(int Val) {
86 return Val < (1 << (MacroPhysOffsBits-1));
87 return -Val < (1 << (MacroPhysOffsBits-1));
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!");
95 // Mask off sign bits.
96 PhysOffs &= (1 << MacroPhysOffsBits)-1;
100 (isExpansionStart << MacroStartOfExpansionBit) |
101 (isExpansionEnd << MacroEndOfExpansionBit) |
102 (MacroID << MacroPhysOffsBits) |
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;
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);
125 unsigned getMacroID() const {
126 assert(isMacroID() && "Is not a macro id!");
127 return (ID >> MacroPhysOffsBits) & ((1 << MacroIDBits)-1);
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;
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.
148 return getFileLoc(FileID, Offset);
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
155 unsigned getRawEncoding() const { return ID; }
157 /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into
158 /// a real SourceLocation.
159 static SourceLocation getFromRawEncoding(unsigned Encoding) {
165 /// Emit - Emit this SourceLocation object to Bitcode.
166 void Emit(llvm::Serializer& S) const;
168 /// ReadVal - Read a SourceLocation object from Bitcode.
169 static SourceLocation ReadVal(llvm::Deserializer& D);
172 inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
173 return LHS.getRawEncoding() == RHS.getRawEncoding();
176 inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
177 return !(LHS == RHS);
180 /// SourceRange - a trival tuple used to represent a source range.
185 SourceRange(): B(SourceLocation()), E(SourceLocation()) {}
186 SourceRange(SourceLocation loc) : B(loc), E(loc) {}
187 SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
189 SourceLocation getBegin() const { return B; }
190 SourceLocation getEnd() const { return E; }
192 void setBegin(SourceLocation b) { B = b; }
193 void setEnd(SourceLocation e) { E = e; }
195 bool isValid() const { return B.isValid() && E.isValid(); }
197 /// Emit - Emit this SourceRange object to Bitcode.
198 void Emit(llvm::Serializer& S) const;
200 /// ReadVal - Read a SourceRange object from Bitcode.
201 static SourceRange ReadVal(llvm::Deserializer& D);
204 /// FullSourceLoc - A tuple containing both a SourceLocation
205 /// and its associated SourceManager. Useful for argument passing to functions
206 /// that expect both objects.
207 class FullSourceLoc {
209 const SourceManager* SrcMgr;
211 // Creates a FullSourceLoc where isValid() returns false.
212 explicit FullSourceLoc() : Loc(SourceLocation()), SrcMgr(NULL) {}
214 explicit FullSourceLoc(SourceLocation loc, const SourceManager& smgr)
215 : Loc(loc), SrcMgr(&smgr) {
216 assert (loc.isValid() && "SourceLocation must be valid!");
219 bool isValid() const { return Loc.isValid(); }
221 SourceLocation getSourceLocation() const { return Loc; }
223 const SourceManager& getManager() const {
224 assert (SrcMgr && "SourceManager is NULL.");
229 } // end namespace clang