SourceLocation.h revision bcc2a67e5180612417727cbdd8afd0f79fdf726d
1//===--- SourceLocation.h - Compact identifier for Source Files -*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// 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 { 21 class MemoryBuffer; 22 template <typename T> struct DenseMapInfo; 23} 24 25namespace clang { 26 27class SourceManager; 28class FileEntry; 29 30/// FileID - This is an opaque identifier used by SourceManager which refers to 31/// a source file (MemoryBuffer) along with its #include path and #line data. 32/// 33class FileID { 34 /// ID - Opaque identifier, 0 is "invalid". 35 unsigned ID; 36public: 37 FileID() : ID(0) {} 38 39 bool isInvalid() const { return ID == 0; } 40 41 bool operator==(const FileID &RHS) const { return ID == RHS.ID; } 42 bool operator<(const FileID &RHS) const { return ID < RHS.ID; } 43 bool operator<=(const FileID &RHS) const { return ID <= RHS.ID; } 44 bool operator!=(const FileID &RHS) const { return !(*this == RHS); } 45 bool operator>(const FileID &RHS) const { return RHS < *this; } 46 bool operator>=(const FileID &RHS) const { return RHS <= *this; } 47 48 static FileID getSentinel() { return Create(~0U); } 49 unsigned getHashValue() const { return ID; } 50 51private: 52 friend class SourceManager; 53 static FileID Create(unsigned V) { 54 FileID F; 55 F.ID = V; 56 return F; 57 } 58 unsigned getOpaqueValue() const { return ID; } 59}; 60 61 62/// SourceLocation - This is a carefully crafted 32-bit identifier that encodes 63/// a full include stack, line and column number information for a position in 64/// an input translation unit. 65class SourceLocation { 66 unsigned ID; 67 friend class SourceManager; 68public: 69 enum { 70 // FileID Layout: 71 // bit 31: 0 -> FileID, 1 -> MacroID (invalid for FileID) 72 // 30...17 -> ChunkID of location, index into SourceManager table. 73 ChunkIDBits = 14, 74 // 0...16 -> Index into the chunk of the specified ChunkID. 75 FilePosBits = 32-1-ChunkIDBits, 76 77 // MacroID Layout: 78 // bit 31: 1 -> MacroID, 0 -> FileID (invalid for MacroID) 79 80 // bit 29,30: unused. 81 82 // bits 28...9 -> MacroID number. 83 MacroIDBits = 20, 84 // bits 8...0 -> Macro spelling offset 85 MacroSpellingOffsBits = 9, 86 87 88 // Useful constants. 89 ChunkSize = (1 << FilePosBits) 90 }; 91 92 SourceLocation() : ID(0) {} // 0 is an invalid FileID. 93 94 bool isFileID() const { return (ID >> 31) == 0; } 95 bool isMacroID() const { return (ID >> 31) != 0; } 96 97 /// isValid - Return true if this is a valid SourceLocation object. Invalid 98 /// SourceLocations are often used when events have no corresponding location 99 /// in the source (e.g. a diagnostic is required for a command line option). 100 /// 101 bool isValid() const { return ID != 0; } 102 bool isInvalid() const { return ID == 0; } 103 104private: 105 static SourceLocation getFileLoc(unsigned ChunkID, unsigned FilePos) { 106 SourceLocation L; 107 // If a FilePos is larger than (1<<FilePosBits), the SourceManager makes 108 // enough consequtive ChunkIDs that we have one for each chunk. 109 if (FilePos >= ChunkSize) { 110 ChunkID += FilePos >> FilePosBits; 111 FilePos &= ChunkSize-1; 112 } 113 114 // FIXME: Find a way to handle out of ChunkID bits! Maybe MaxFileID is an 115 // escape of some sort? 116 assert(ChunkID < (1 << ChunkIDBits) && "Out of ChunkID's"); 117 118 L.ID = (ChunkID << FilePosBits) | FilePos; 119 return L; 120 } 121public: 122 123 static bool isValidMacroSpellingOffs(int Val) { 124 if (Val >= 0) 125 return Val < (1 << (MacroSpellingOffsBits-1)); 126 return -Val <= (1 << (MacroSpellingOffsBits-1)); 127 } 128 129 static SourceLocation getMacroLoc(unsigned MacroID, int SpellingOffs) { 130 assert(MacroID < (1 << MacroIDBits) && "Too many macros!"); 131 assert(isValidMacroSpellingOffs(SpellingOffs) &&"spelling offs too large!"); 132 133 // Mask off sign bits. 134 SpellingOffs &= (1 << MacroSpellingOffsBits)-1; 135 136 SourceLocation L; 137 L.ID = (1 << 31) | 138 (MacroID << MacroSpellingOffsBits) | 139 SpellingOffs; 140 return L; 141 } 142 143 144 /// getChunkID - Return the chunk identifier for this SourceLocation. This 145 /// ChunkID can be used with the SourceManager object to obtain an entire 146 /// include stack for a file position reference. 147 unsigned getChunkID() const { 148 assert(isFileID() && "can't get the file id of a non-file sloc!"); 149 return ID >> FilePosBits; 150 } 151 152 /// getRawFilePos - Return the byte offset from the start of the file-chunk 153 /// referred to by ChunkID. This method should not be used to get the offset 154 /// from the start of the file, instead you should use 155 /// SourceManager::getDecomposedFileLoc. This method will be 156 // incorrect for large files. 157 unsigned getRawFilePos() const { 158 assert(isFileID() && "can't get the file id of a non-file sloc!"); 159 return ID & (ChunkSize-1); 160 } 161 162 unsigned getMacroID() const { 163 assert(isMacroID() && "Is not a macro id!"); 164 return (ID >> MacroSpellingOffsBits) & ((1 << MacroIDBits)-1); 165 } 166 167 int getMacroSpellingOffs() const { 168 assert(isMacroID() && "Is not a macro id!"); 169 int Val = ID & ((1 << MacroSpellingOffsBits)-1); 170 // Sign extend it properly. 171 unsigned ShAmt = sizeof(int)*8 - MacroSpellingOffsBits; 172 return (Val << ShAmt) >> ShAmt; 173 } 174 175 /// getFileLocWithOffset - Return a source location with the specified offset 176 /// from this file SourceLocation. 177 SourceLocation getFileLocWithOffset(int Offset) const { 178 unsigned ChunkID = getChunkID(); 179 Offset += getRawFilePos(); 180 // Handle negative offsets correctly. 181 while (Offset < 0) { 182 --ChunkID; 183 Offset += ChunkSize; 184 } 185 return getFileLoc(ChunkID, Offset); 186 } 187 188 /// getRawEncoding - When a SourceLocation itself cannot be used, this returns 189 /// an (opaque) 32-bit integer encoding for it. This should only be passed 190 /// to SourceLocation::getFromRawEncoding, it should not be inspected 191 /// directly. 192 unsigned getRawEncoding() const { return ID; } 193 194 195 bool operator<(const SourceLocation &RHS) const { 196 return ID < RHS.ID; 197 } 198 199 /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into 200 /// a real SourceLocation. 201 static SourceLocation getFromRawEncoding(unsigned Encoding) { 202 SourceLocation X; 203 X.ID = Encoding; 204 return X; 205 } 206 207 /// Emit - Emit this SourceLocation object to Bitcode. 208 void Emit(llvm::Serializer& S) const; 209 210 /// ReadVal - Read a SourceLocation object from Bitcode. 211 static SourceLocation ReadVal(llvm::Deserializer& D); 212}; 213 214inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) { 215 return LHS.getRawEncoding() == RHS.getRawEncoding(); 216} 217 218inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) { 219 return !(LHS == RHS); 220} 221 222/// SourceRange - a trival tuple used to represent a source range. 223class SourceRange { 224 SourceLocation B; 225 SourceLocation E; 226public: 227 SourceRange(): B(SourceLocation()), E(SourceLocation()) {} 228 SourceRange(SourceLocation loc) : B(loc), E(loc) {} 229 SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {} 230 231 SourceLocation getBegin() const { return B; } 232 SourceLocation getEnd() const { return E; } 233 234 void setBegin(SourceLocation b) { B = b; } 235 void setEnd(SourceLocation e) { E = e; } 236 237 bool isValid() const { return B.isValid() && E.isValid(); } 238 239 /// Emit - Emit this SourceRange object to Bitcode. 240 void Emit(llvm::Serializer& S) const; 241 242 /// ReadVal - Read a SourceRange object from Bitcode. 243 static SourceRange ReadVal(llvm::Deserializer& D); 244}; 245 246/// FullSourceLoc - A SourceLocation and its associated SourceManager. Useful 247/// for argument passing to functions that expect both objects. 248class FullSourceLoc : public SourceLocation { 249 SourceManager* SrcMgr; 250public: 251 // Creates a FullSourceLoc where isValid() returns false. 252 explicit FullSourceLoc() : SrcMgr((SourceManager*) 0) {} 253 254 explicit FullSourceLoc(SourceLocation Loc, SourceManager &SM) 255 : SourceLocation(Loc), SrcMgr(&SM) {} 256 257 SourceManager& getManager() { 258 assert (SrcMgr && "SourceManager is NULL."); 259 return *SrcMgr; 260 } 261 262 const SourceManager& getManager() const { 263 assert (SrcMgr && "SourceManager is NULL."); 264 return *SrcMgr; 265 } 266 267 FileID getFileID() const; 268 269 FullSourceLoc getInstantiationLoc() const; 270 FullSourceLoc getSpellingLoc() const; 271 FullSourceLoc getIncludeLoc() const; 272 273 unsigned getLineNumber() const; 274 unsigned getColumnNumber() const; 275 276 unsigned getInstantiationLineNumber() const; 277 unsigned getInstantiationColumnNumber() const; 278 279 unsigned getSpellingLineNumber() const; 280 unsigned getSpellingColumnNumber() const; 281 282 const char *getCharacterData() const; 283 284 const llvm::MemoryBuffer* getBuffer() const; 285 286 const char* getSourceName() const; 287 const FileEntry* getFileEntryForLoc() const; 288 289 bool isInSystemHeader() const; 290 291 /// Prints information about this FullSourceLoc to stderr. Useful for 292 /// debugging. 293 void dump() const; 294 295 friend inline bool 296 operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) { 297 return LHS.getRawEncoding() == RHS.getRawEncoding() && 298 LHS.SrcMgr == RHS.SrcMgr; 299 } 300 301 friend inline bool 302 operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) { 303 return !(LHS == RHS); 304 } 305 306}; 307 308} // end namespace clang 309 310namespace llvm { 311 /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and 312 /// DenseSets. 313 template <> 314 struct DenseMapInfo<clang::FileID> { 315 static inline clang::FileID getEmptyKey() { 316 return clang::FileID(); 317 } 318 static inline clang::FileID getTombstoneKey() { 319 return clang::FileID::getSentinel(); 320 } 321 322 static unsigned getHashValue(clang::FileID S) { 323 return S.getHashValue(); 324 } 325 326 static bool isEqual(clang::FileID LHS, clang::FileID RHS) { 327 return LHS == RHS; 328 } 329 330 static bool isPod() { return true; } 331 }; 332 333} // end namespace llvm 334 335#endif 336