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