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 | // |
| 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 | namespace clang { |
| 18 | |
| 19 | /// SourceLocation - This is a carefully crafted 32-bit identifier that encodes |
| 20 | /// a full include stack, line and column number information for a position in |
| 21 | /// an input translation unit. |
| 22 | class SourceLocation { |
| 23 | unsigned ID; |
| 24 | public: |
| 25 | enum { |
| 26 | FileIDBits = 14, |
| 27 | FilePosBits = 32-FileIDBits |
| 28 | }; |
| 29 | |
| 30 | SourceLocation() : ID(0) {} // 0 is an invalid FileID. |
| 31 | |
| 32 | /// SourceLocation constructor - Create a new SourceLocation object with the |
| 33 | /// specified FileID and FilePos. |
| 34 | SourceLocation(unsigned FileID, unsigned FilePos) { |
| 35 | // If a FilePos is larger than (1<<FilePosBits), the SourceManager makes |
| 36 | // enough consequtive FileIDs that we have one for each chunk. |
| 37 | if (FilePos >= (1 << FilePosBits)) { |
| 38 | FileID += FilePos >> FilePosBits; |
| 39 | FilePos &= (1 << FilePosBits)-1; |
| 40 | } |
| 41 | |
| 42 | // FIXME: Find a way to handle out of FileID bits! Maybe MaxFileID is an |
| 43 | // escape of some sort? |
| 44 | if (FileID >= (1 << FileIDBits)) |
| 45 | FileID = (1 << FileIDBits)-1; |
| 46 | |
| 47 | ID = (FileID << FilePosBits) | FilePos; |
| 48 | } |
| 49 | |
| 50 | /// isValid - Return true if this is a valid SourceLocation object. Invalid |
| 51 | /// SourceLocations are often used when events have no corresponding location |
| 52 | /// in the source (e.g. a diagnostic is required for a command line option). |
| 53 | /// |
| 54 | bool isValid() const { return ID != 0; } |
| 55 | |
| 56 | /// getFileID - Return the file identifier for this SourceLocation. This |
| 57 | /// FileID can be used with the SourceManager object to obtain an entire |
| 58 | /// include stack for a file position reference. |
| 59 | unsigned getFileID() const { return ID >> FilePosBits; } |
| 60 | |
| 61 | /// getRawFilePos - Return the byte offset from the start of the file-chunk |
| 62 | /// referred to by FileID. This method should not be used to get the offset |
| 63 | /// from the start of the file, instead you should use |
| 64 | /// SourceManager::getFilePos. This method will be incorrect for large files. |
| 65 | unsigned getRawFilePos() const { return ID & ((1 << FilePosBits)-1); } |
| 66 | |
| 67 | |
| 68 | /// getRawEncoding - When a SourceLocation itself cannot be used, this returns |
| 69 | /// an (opaque) 32-bit integer encoding for it. This should only be passed |
| 70 | /// to SourceLocation::getFromRawEncoding, it should not be inspected |
| 71 | /// directly. |
| 72 | unsigned getRawEncoding() const { return ID; } |
| 73 | |
| 74 | /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into |
| 75 | /// a real SourceLocation. |
| 76 | static SourceLocation getFromRawEncoding(unsigned Encoding) { |
| 77 | SourceLocation X; |
| 78 | X.ID = Encoding; |
| 79 | return X; |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) { |
| 84 | return LHS.getRawEncoding() == RHS.getRawEncoding(); |
| 85 | } |
| 86 | |
| 87 | inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) { |
| 88 | return !(LHS == RHS); |
| 89 | } |
| 90 | |
| 91 | /// SourceRange - a trival tuple used to represent a source range. |
| 92 | class SourceRange { |
| 93 | SourceLocation B; |
| 94 | SourceLocation E; |
| 95 | public: |
| 96 | SourceRange(): B(SourceLocation()), E(SourceLocation()) {} |
| 97 | SourceRange(SourceLocation loc) : B(loc), E(loc) {} |
| 98 | SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {} |
| 99 | |
| 100 | SourceLocation Begin() const { return B; } |
| 101 | SourceLocation End() const { return E; } |
| 102 | |
| 103 | bool isValid() const { return B.isValid() && E.isValid(); } |
| 104 | }; |
| 105 | |
| 106 | } // end namespace clang |
| 107 | |
| 108 | #endif |