Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SourceLocation.h - Compact identifier for Source Files -*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 17 | #include <cassert> |
Ted Kremenek | 0a449ee | 2007-10-25 18:27:10 +0000 | [diff] [blame] | 18 | #include "llvm/Bitcode/SerializationFwd.h" |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 19 | |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 20 | namespace llvm { |
| 21 | class MemoryBuffer; |
| 22 | } |
| 23 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | namespace clang { |
Ted Kremenek | a9793ed | 2007-12-12 18:16:46 +0000 | [diff] [blame] | 25 | |
| 26 | class SourceManager; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 27 | class FileEntry; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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. |
| 32 | class SourceLocation { |
| 33 | unsigned ID; |
| 34 | public: |
| 35 | enum { |
Chris Lattner | b7489d8 | 2007-11-09 23:52:16 +0000 | [diff] [blame] | 36 | // FileID Layout: |
| 37 | // bit 31: 0 -> FileID, 1 -> MacroID (invalid for FileID) |
| 38 | // 30...17 -> FileID of source location, index into SourceManager table. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | FileIDBits = 14, |
Chris Lattner | b7489d8 | 2007-11-09 23:52:16 +0000 | [diff] [blame] | 40 | // 0...16 -> Index into the chunk of the specified FileID. |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 41 | FilePosBits = 32-1-FileIDBits, |
| 42 | |
Chris Lattner | b7489d8 | 2007-11-09 23:52:16 +0000 | [diff] [blame] | 43 | // MacroID Layout: |
| 44 | // bit 31: 1 -> MacroID, 0 -> FileID (invalid for MacroID) |
| 45 | |
Chris Lattner | f848454 | 2008-02-03 08:24:13 +0000 | [diff] [blame] | 46 | // bit 29,30: unused. |
| 47 | |
Chris Lattner | b7489d8 | 2007-11-09 23:52:16 +0000 | [diff] [blame] | 48 | // bits 28...9 -> MacroID number. |
Chris Lattner | d1623a8 | 2007-07-21 06:41:57 +0000 | [diff] [blame] | 49 | MacroIDBits = 20, |
Chris Lattner | b7489d8 | 2007-11-09 23:52:16 +0000 | [diff] [blame] | 50 | // bits 8...0 -> Macro Physical offset |
Chris Lattner | 31bb8be | 2007-07-20 18:00:12 +0000 | [diff] [blame] | 51 | MacroPhysOffsBits = 9, |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 52 | |
Chris Lattner | b7489d8 | 2007-11-09 23:52:16 +0000 | [diff] [blame] | 53 | |
| 54 | // Useful constants. |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 55 | ChunkSize = (1 << FilePosBits) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | SourceLocation() : ID(0) {} // 0 is an invalid FileID. |
| 59 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 60 | bool isFileID() const { return (ID >> 31) == 0; } |
| 61 | bool isMacroID() const { return (ID >> 31) != 0; } |
| 62 | |
Chris Lattner | b7489d8 | 2007-11-09 23:52:16 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 70 | static SourceLocation getFileLoc(unsigned FileID, unsigned FilePos) { |
| 71 | SourceLocation L; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 72 | // If a FilePos is larger than (1<<FilePosBits), the SourceManager makes |
| 73 | // enough consequtive FileIDs that we have one for each chunk. |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 74 | if (FilePos >= ChunkSize) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 75 | FileID += FilePos >> FilePosBits; |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 76 | FilePos &= ChunkSize-1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // FIXME: Find a way to handle out of FileID bits! Maybe MaxFileID is an |
| 80 | // escape of some sort? |
Chris Lattner | 4cabcfe | 2007-08-02 04:14:33 +0000 | [diff] [blame] | 81 | assert(FileID < (1 << FileIDBits) && "Out of fileid's"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 83 | L.ID = (FileID << FilePosBits) | FilePos; |
| 84 | return L; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Chris Lattner | d1623a8 | 2007-07-21 06:41:57 +0000 | [diff] [blame] | 87 | static bool isValidMacroPhysOffs(int Val) { |
| 88 | if (Val >= 0) |
| 89 | return Val < (1 << (MacroPhysOffsBits-1)); |
| 90 | return -Val < (1 << (MacroPhysOffsBits-1)); |
| 91 | } |
| 92 | |
Chris Lattner | f848454 | 2008-02-03 08:24:13 +0000 | [diff] [blame] | 93 | static SourceLocation getMacroLoc(unsigned MacroID, int PhysOffs){ |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 94 | assert(MacroID < (1 << MacroIDBits) && "Too many macros!"); |
Chris Lattner | d1623a8 | 2007-07-21 06:41:57 +0000 | [diff] [blame] | 95 | assert(isValidMacroPhysOffs(PhysOffs) && "Physoffs too large!"); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 96 | |
Chris Lattner | b7489d8 | 2007-11-09 23:52:16 +0000 | [diff] [blame] | 97 | // Mask off sign bits. |
Chris Lattner | d1623a8 | 2007-07-21 06:41:57 +0000 | [diff] [blame] | 98 | PhysOffs &= (1 << MacroPhysOffsBits)-1; |
| 99 | |
| 100 | SourceLocation L; |
Chris Lattner | b7489d8 | 2007-11-09 23:52:16 +0000 | [diff] [blame] | 101 | L.ID = (1 << 31) | |
Chris Lattner | b7489d8 | 2007-11-09 23:52:16 +0000 | [diff] [blame] | 102 | (MacroID << MacroPhysOffsBits) | |
| 103 | PhysOffs; |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 104 | return L; |
| 105 | } |
| 106 | |
| 107 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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. |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 111 | unsigned getFileID() const { |
| 112 | assert(isFileID() && "can't get the file id of a non-file sloc!"); |
| 113 | return ID >> FilePosBits; |
| 114 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 |
Ted Kremenek | 9f68fa5 | 2008-03-18 20:13:06 +0000 | [diff] [blame] | 119 | /// SourceManager::getDecomposedFileLoc. This method will be |
| 120 | // incorrect for large files. |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 121 | unsigned getRawFilePos() const { |
| 122 | assert(isFileID() && "can't get the file id of a non-file sloc!"); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 123 | return ID & (ChunkSize-1); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | unsigned getMacroID() const { |
| 127 | assert(isMacroID() && "Is not a macro id!"); |
Chris Lattner | b7489d8 | 2007-11-09 23:52:16 +0000 | [diff] [blame] | 128 | return (ID >> MacroPhysOffsBits) & ((1 << MacroIDBits)-1); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 129 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 130 | |
Chris Lattner | d1623a8 | 2007-07-21 06:41:57 +0000 | [diff] [blame] | 131 | int getMacroPhysOffs() const { |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 132 | assert(isMacroID() && "Is not a macro id!"); |
Chris Lattner | b7489d8 | 2007-11-09 23:52:16 +0000 | [diff] [blame] | 133 | int Val = ID & ((1 << MacroPhysOffsBits)-1); |
Chris Lattner | d1623a8 | 2007-07-21 06:41:57 +0000 | [diff] [blame] | 134 | // Sign extend it properly. |
| 135 | unsigned ShAmt = sizeof(int)*8 - MacroPhysOffsBits; |
| 136 | return (Val << ShAmt) >> ShAmt; |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 139 | /// getFileLocWithOffset - Return a source location with the specified offset |
| 140 | /// from this file SourceLocation. |
Chris Lattner | d1623a8 | 2007-07-21 06:41:57 +0000 | [diff] [blame] | 141 | SourceLocation getFileLocWithOffset(int Offset) const { |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 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); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 150 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 | /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into |
| 159 | /// a real SourceLocation. |
| 160 | static SourceLocation getFromRawEncoding(unsigned Encoding) { |
| 161 | SourceLocation X; |
| 162 | X.ID = Encoding; |
| 163 | return X; |
| 164 | } |
Ted Kremenek | beb7713 | 2007-11-01 22:25:41 +0000 | [diff] [blame] | 165 | |
| 166 | /// Emit - Emit this SourceLocation object to Bitcode. |
| 167 | void Emit(llvm::Serializer& S) const; |
| 168 | |
| 169 | /// ReadVal - Read a SourceLocation object from Bitcode. |
| 170 | static SourceLocation ReadVal(llvm::Deserializer& D); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) { |
| 174 | return LHS.getRawEncoding() == RHS.getRawEncoding(); |
| 175 | } |
| 176 | |
| 177 | inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) { |
| 178 | return !(LHS == RHS); |
| 179 | } |
| 180 | |
| 181 | /// SourceRange - a trival tuple used to represent a source range. |
| 182 | class SourceRange { |
| 183 | SourceLocation B; |
| 184 | SourceLocation E; |
| 185 | public: |
| 186 | SourceRange(): B(SourceLocation()), E(SourceLocation()) {} |
| 187 | SourceRange(SourceLocation loc) : B(loc), E(loc) {} |
| 188 | SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {} |
| 189 | |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 190 | SourceLocation getBegin() const { return B; } |
| 191 | SourceLocation getEnd() const { return E; } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 192 | |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 193 | void setBegin(SourceLocation b) { B = b; } |
| 194 | void setEnd(SourceLocation e) { E = e; } |
| 195 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 196 | bool isValid() const { return B.isValid() && E.isValid(); } |
Ted Kremenek | beb7713 | 2007-11-01 22:25:41 +0000 | [diff] [blame] | 197 | |
| 198 | /// Emit - Emit this SourceRange object to Bitcode. |
| 199 | void Emit(llvm::Serializer& S) const; |
| 200 | |
| 201 | /// ReadVal - Read a SourceRange object from Bitcode. |
| 202 | static SourceRange ReadVal(llvm::Deserializer& D); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 203 | }; |
| 204 | |
Ted Kremenek | e03a2f3 | 2007-12-12 18:32:04 +0000 | [diff] [blame] | 205 | /// FullSourceLoc - A tuple containing both a SourceLocation |
Ted Kremenek | a9793ed | 2007-12-12 18:16:46 +0000 | [diff] [blame] | 206 | /// and its associated SourceManager. Useful for argument passing to functions |
| 207 | /// that expect both objects. |
Ted Kremenek | e03a2f3 | 2007-12-12 18:32:04 +0000 | [diff] [blame] | 208 | class FullSourceLoc { |
Ted Kremenek | a9793ed | 2007-12-12 18:16:46 +0000 | [diff] [blame] | 209 | SourceLocation Loc; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 210 | SourceManager* SrcMgr; |
Ted Kremenek | a9793ed | 2007-12-12 18:16:46 +0000 | [diff] [blame] | 211 | public: |
Ted Kremenek | 1b924fd | 2007-12-12 18:54:21 +0000 | [diff] [blame] | 212 | // Creates a FullSourceLoc where isValid() returns false. |
Ted Kremenek | 21584fe | 2007-12-12 19:39:40 +0000 | [diff] [blame] | 213 | explicit FullSourceLoc() |
| 214 | : Loc(SourceLocation()), SrcMgr((SourceManager*) 0) {} |
Ted Kremenek | a9793ed | 2007-12-12 18:16:46 +0000 | [diff] [blame] | 215 | |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 216 | explicit FullSourceLoc(SourceLocation loc, SourceManager& smgr) |
| 217 | : Loc(loc), SrcMgr(&smgr) {} |
Ted Kremenek | 1b924fd | 2007-12-12 18:54:21 +0000 | [diff] [blame] | 218 | |
Ted Kremenek | 25bb23a | 2007-12-12 18:18:05 +0000 | [diff] [blame] | 219 | bool isValid() const { return Loc.isValid(); } |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 220 | bool isInvalid() const { return Loc.isInvalid(); } |
Ted Kremenek | a9793ed | 2007-12-12 18:16:46 +0000 | [diff] [blame] | 221 | |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 222 | SourceLocation getLocation() const { return Loc; } |
| 223 | |
| 224 | SourceManager& getManager() { |
| 225 | assert (SrcMgr && "SourceManager is NULL."); |
| 226 | return *SrcMgr; |
| 227 | } |
Ted Kremenek | a9793ed | 2007-12-12 18:16:46 +0000 | [diff] [blame] | 228 | |
Ted Kremenek | 1b924fd | 2007-12-12 18:54:21 +0000 | [diff] [blame] | 229 | const SourceManager& getManager() const { |
Ted Kremenek | a9793ed | 2007-12-12 18:16:46 +0000 | [diff] [blame] | 230 | assert (SrcMgr && "SourceManager is NULL."); |
| 231 | return *SrcMgr; |
| 232 | } |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 233 | |
| 234 | FullSourceLoc getLogicalLoc(); |
| 235 | FullSourceLoc getIncludeLoc(); |
| 236 | |
| 237 | unsigned getLineNumber(); |
| 238 | unsigned getColumnNumber(); |
| 239 | |
| 240 | const char *getCharacterData() const; |
| 241 | |
| 242 | const llvm::MemoryBuffer* getBuffer() const; |
| 243 | |
| 244 | const char* getSourceName() const; |
| 245 | const FileEntry* getFileEntryForLoc() const; |
| 246 | |
Ted Kremenek | ec5d81b | 2008-04-03 07:11:38 +0000 | [diff] [blame^] | 247 | bool isFileID() const { return Loc.isFileID(); } |
| 248 | |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 249 | bool operator==(const FullSourceLoc& RHS) const { |
| 250 | return SrcMgr == RHS.SrcMgr && Loc == RHS.Loc; |
| 251 | } |
| 252 | |
| 253 | bool operator!=(const FullSourceLoc& RHS) const { |
| 254 | return SrcMgr != RHS.SrcMgr || Loc != RHS.Loc; |
| 255 | } |
Ted Kremenek | a9793ed | 2007-12-12 18:16:46 +0000 | [diff] [blame] | 256 | }; |
| 257 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 258 | } // end namespace clang |
| 259 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 260 | #endif |