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