blob: a77eadd27190678c16ff3882773ddcfe17e140d6 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SourceLocation.h - Compact identifier for Source Files -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
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 Lattner9dc1f532007-07-20 16:37:10 +000017#include <cassert>
Ted Kremenek0a449ee2007-10-25 18:27:10 +000018#include "llvm/Bitcode/SerializationFwd.h"
Chris Lattner9dc1f532007-07-20 16:37:10 +000019
Ted Kremenek9c728dc2007-12-12 22:39:36 +000020namespace llvm {
21class MemoryBuffer;
22}
23
Reid Spencer5f016e22007-07-11 17:01:13 +000024namespace clang {
Ted Kremeneka9793ed2007-12-12 18:16:46 +000025
26class SourceManager;
Ted Kremenek9c728dc2007-12-12 22:39:36 +000027class FileEntry;
Reid Spencer5f016e22007-07-11 17:01:13 +000028
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.
32class SourceLocation {
33 unsigned ID;
34public:
35 enum {
Chris Lattnerb7489d82007-11-09 23:52:16 +000036 // FileID Layout:
37 // bit 31: 0 -> FileID, 1 -> MacroID (invalid for FileID)
38 // 30...17 -> FileID of source location, index into SourceManager table.
Reid Spencer5f016e22007-07-11 17:01:13 +000039 FileIDBits = 14,
Chris Lattnerb7489d82007-11-09 23:52:16 +000040 // 0...16 -> Index into the chunk of the specified FileID.
Chris Lattner9dc1f532007-07-20 16:37:10 +000041 FilePosBits = 32-1-FileIDBits,
42
Chris Lattnerb7489d82007-11-09 23:52:16 +000043 // MacroID Layout:
44 // bit 31: 1 -> MacroID, 0 -> FileID (invalid for MacroID)
45
Chris Lattnerf8484542008-02-03 08:24:13 +000046 // bit 29,30: unused.
47
Chris Lattnerb7489d82007-11-09 23:52:16 +000048 // bits 28...9 -> MacroID number.
Chris Lattnerd1623a82007-07-21 06:41:57 +000049 MacroIDBits = 20,
Chris Lattnerdf7c17a2009-01-16 07:00:02 +000050 // bits 8...0 -> Macro spelling offset
51 MacroSpellingOffsBits = 9,
Chris Lattner2c64b7b2007-10-16 21:07:07 +000052
Chris Lattnerb7489d82007-11-09 23:52:16 +000053
54 // Useful constants.
Chris Lattner2c64b7b2007-10-16 21:07:07 +000055 ChunkSize = (1 << FilePosBits)
Reid Spencer5f016e22007-07-11 17:01:13 +000056 };
57
58 SourceLocation() : ID(0) {} // 0 is an invalid FileID.
59
Chris Lattner9dc1f532007-07-20 16:37:10 +000060 bool isFileID() const { return (ID >> 31) == 0; }
61 bool isMacroID() const { return (ID >> 31) != 0; }
62
Chris Lattnerb7489d82007-11-09 23:52:16 +000063 /// 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 Lattner9dc1f532007-07-20 16:37:10 +000070 static SourceLocation getFileLoc(unsigned FileID, unsigned FilePos) {
71 SourceLocation L;
Reid Spencer5f016e22007-07-11 17:01:13 +000072 // If a FilePos is larger than (1<<FilePosBits), the SourceManager makes
73 // enough consequtive FileIDs that we have one for each chunk.
Chris Lattner2c64b7b2007-10-16 21:07:07 +000074 if (FilePos >= ChunkSize) {
Reid Spencer5f016e22007-07-11 17:01:13 +000075 FileID += FilePos >> FilePosBits;
Chris Lattner2c64b7b2007-10-16 21:07:07 +000076 FilePos &= ChunkSize-1;
Reid Spencer5f016e22007-07-11 17:01:13 +000077 }
78
79 // FIXME: Find a way to handle out of FileID bits! Maybe MaxFileID is an
80 // escape of some sort?
Chris Lattner4cabcfe2007-08-02 04:14:33 +000081 assert(FileID < (1 << FileIDBits) && "Out of fileid's");
Reid Spencer5f016e22007-07-11 17:01:13 +000082
Chris Lattner9dc1f532007-07-20 16:37:10 +000083 L.ID = (FileID << FilePosBits) | FilePos;
84 return L;
Reid Spencer5f016e22007-07-11 17:01:13 +000085 }
86
Chris Lattnerdf7c17a2009-01-16 07:00:02 +000087 static bool isValidMacroSpellingOffs(int Val) {
Chris Lattnerd1623a82007-07-21 06:41:57 +000088 if (Val >= 0)
Chris Lattnerdf7c17a2009-01-16 07:00:02 +000089 return Val < (1 << (MacroSpellingOffsBits-1));
90 return -Val <= (1 << (MacroSpellingOffsBits-1));
Chris Lattnerd1623a82007-07-21 06:41:57 +000091 }
92
Chris Lattnerdf7c17a2009-01-16 07:00:02 +000093 static SourceLocation getMacroLoc(unsigned MacroID, int SpellingOffs) {
Chris Lattner9dc1f532007-07-20 16:37:10 +000094 assert(MacroID < (1 << MacroIDBits) && "Too many macros!");
Chris Lattnerdf7c17a2009-01-16 07:00:02 +000095 assert(isValidMacroSpellingOffs(SpellingOffs) &&"spelling offs too large!");
Chris Lattner9dc1f532007-07-20 16:37:10 +000096
Chris Lattnerb7489d82007-11-09 23:52:16 +000097 // Mask off sign bits.
Chris Lattnerdf7c17a2009-01-16 07:00:02 +000098 SpellingOffs &= (1 << MacroSpellingOffsBits)-1;
Chris Lattnerd1623a82007-07-21 06:41:57 +000099
100 SourceLocation L;
Chris Lattnerb7489d82007-11-09 23:52:16 +0000101 L.ID = (1 << 31) |
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000102 (MacroID << MacroSpellingOffsBits) |
103 SpellingOffs;
Chris Lattner9dc1f532007-07-20 16:37:10 +0000104 return L;
105 }
106
107
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 /// 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 Lattner9dc1f532007-07-20 16:37:10 +0000111 unsigned getFileID() const {
112 assert(isFileID() && "can't get the file id of a non-file sloc!");
113 return ID >> FilePosBits;
114 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000115
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 Kremenek9f68fa52008-03-18 20:13:06 +0000119 /// SourceManager::getDecomposedFileLoc. This method will be
120 // incorrect for large files.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000121 unsigned getRawFilePos() const {
122 assert(isFileID() && "can't get the file id of a non-file sloc!");
Chris Lattner2c64b7b2007-10-16 21:07:07 +0000123 return ID & (ChunkSize-1);
Chris Lattner9dc1f532007-07-20 16:37:10 +0000124 }
125
126 unsigned getMacroID() const {
127 assert(isMacroID() && "Is not a macro id!");
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000128 return (ID >> MacroSpellingOffsBits) & ((1 << MacroIDBits)-1);
Chris Lattner9dc1f532007-07-20 16:37:10 +0000129 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000130
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000131 int getMacroSpellingOffs() const {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000132 assert(isMacroID() && "Is not a macro id!");
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000133 int Val = ID & ((1 << MacroSpellingOffsBits)-1);
Chris Lattnerd1623a82007-07-21 06:41:57 +0000134 // Sign extend it properly.
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000135 unsigned ShAmt = sizeof(int)*8 - MacroSpellingOffsBits;
Chris Lattnerd1623a82007-07-21 06:41:57 +0000136 return (Val << ShAmt) >> ShAmt;
Chris Lattner9dc1f532007-07-20 16:37:10 +0000137 }
138
Chris Lattner9dc1f532007-07-20 16:37:10 +0000139 /// getFileLocWithOffset - Return a source location with the specified offset
140 /// from this file SourceLocation.
Chris Lattnerd1623a82007-07-21 06:41:57 +0000141 SourceLocation getFileLocWithOffset(int Offset) const {
Chris Lattner2c64b7b2007-10-16 21:07:07 +0000142 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 Lattner9dc1f532007-07-20 16:37:10 +0000150 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000151
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
Chris Lattnercff9cc92008-10-12 05:44:03 +0000158
159 bool operator<(const SourceLocation &RHS) const {
160 return ID < RHS.ID;
161 }
162
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into
164 /// a real SourceLocation.
165 static SourceLocation getFromRawEncoding(unsigned Encoding) {
166 SourceLocation X;
167 X.ID = Encoding;
168 return X;
169 }
Ted Kremenekbeb77132007-11-01 22:25:41 +0000170
171 /// Emit - Emit this SourceLocation object to Bitcode.
172 void Emit(llvm::Serializer& S) const;
173
174 /// ReadVal - Read a SourceLocation object from Bitcode.
175 static SourceLocation ReadVal(llvm::Deserializer& D);
Reid Spencer5f016e22007-07-11 17:01:13 +0000176};
177
178inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
179 return LHS.getRawEncoding() == RHS.getRawEncoding();
180}
181
182inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
183 return !(LHS == RHS);
184}
185
186/// SourceRange - a trival tuple used to represent a source range.
187class SourceRange {
188 SourceLocation B;
189 SourceLocation E;
190public:
191 SourceRange(): B(SourceLocation()), E(SourceLocation()) {}
192 SourceRange(SourceLocation loc) : B(loc), E(loc) {}
193 SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
194
Chris Lattner311ff022007-10-16 22:36:42 +0000195 SourceLocation getBegin() const { return B; }
196 SourceLocation getEnd() const { return E; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000197
Chris Lattnere80a59c2007-07-25 00:24:17 +0000198 void setBegin(SourceLocation b) { B = b; }
199 void setEnd(SourceLocation e) { E = e; }
200
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 bool isValid() const { return B.isValid() && E.isValid(); }
Ted Kremenekbeb77132007-11-01 22:25:41 +0000202
203 /// Emit - Emit this SourceRange object to Bitcode.
204 void Emit(llvm::Serializer& S) const;
205
206 /// ReadVal - Read a SourceRange object from Bitcode.
207 static SourceRange ReadVal(llvm::Deserializer& D);
Reid Spencer5f016e22007-07-11 17:01:13 +0000208};
209
Ted Kremeneke03a2f32007-12-12 18:32:04 +0000210/// FullSourceLoc - A tuple containing both a SourceLocation
Ted Kremeneka9793ed2007-12-12 18:16:46 +0000211/// and its associated SourceManager. Useful for argument passing to functions
212/// that expect both objects.
Ted Kremeneke03a2f32007-12-12 18:32:04 +0000213class FullSourceLoc {
Ted Kremeneka9793ed2007-12-12 18:16:46 +0000214 SourceLocation Loc;
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000215 SourceManager* SrcMgr;
Ted Kremeneka9793ed2007-12-12 18:16:46 +0000216public:
Ted Kremenek1b924fd2007-12-12 18:54:21 +0000217 // Creates a FullSourceLoc where isValid() returns false.
Ted Kremenek21584fe2007-12-12 19:39:40 +0000218 explicit FullSourceLoc()
219 : Loc(SourceLocation()), SrcMgr((SourceManager*) 0) {}
Ted Kremeneka9793ed2007-12-12 18:16:46 +0000220
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000221 explicit FullSourceLoc(SourceLocation loc, SourceManager& smgr)
222 : Loc(loc), SrcMgr(&smgr) {}
Ted Kremenek1b924fd2007-12-12 18:54:21 +0000223
Ted Kremenek25bb23a2007-12-12 18:18:05 +0000224 bool isValid() const { return Loc.isValid(); }
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000225 bool isInvalid() const { return Loc.isInvalid(); }
Ted Kremeneka9793ed2007-12-12 18:16:46 +0000226
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000227 SourceLocation getLocation() const { return Loc; }
228
229 SourceManager& getManager() {
230 assert (SrcMgr && "SourceManager is NULL.");
231 return *SrcMgr;
232 }
Ted Kremeneka9793ed2007-12-12 18:16:46 +0000233
Ted Kremenek1b924fd2007-12-12 18:54:21 +0000234 const SourceManager& getManager() const {
Ted Kremeneka9793ed2007-12-12 18:16:46 +0000235 assert (SrcMgr && "SourceManager is NULL.");
236 return *SrcMgr;
237 }
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000238
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000239 FullSourceLoc getInstantiationLoc() const;
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000240 FullSourceLoc getSpellingLoc() const;
Chris Lattner5c38b632008-09-29 21:46:13 +0000241 FullSourceLoc getIncludeLoc() const;
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000242
Ted Kremenek1758b072008-04-03 17:55:15 +0000243 unsigned getLineNumber() const;
244 unsigned getColumnNumber() const;
245
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000246 unsigned getInstantiationLineNumber() const;
247 unsigned getInstantiationColumnNumber() const;
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000248
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000249 unsigned getSpellingLineNumber() const;
250 unsigned getSpellingColumnNumber() const;
Chris Lattner5c38b632008-09-29 21:46:13 +0000251
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000252 const char *getCharacterData() const;
253
254 const llvm::MemoryBuffer* getBuffer() const;
255
256 const char* getSourceName() const;
257 const FileEntry* getFileEntryForLoc() const;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000258
259 bool isInSystemHeader() const;
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000260
Ted Kremenekec5d81b2008-04-03 07:11:38 +0000261 bool isFileID() const { return Loc.isFileID(); }
262
Ted Kremenek9fd87b12008-04-14 21:04:18 +0000263 unsigned getCanonicalFileID() const;
264
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000265 bool operator==(const FullSourceLoc& RHS) const {
266 return SrcMgr == RHS.SrcMgr && Loc == RHS.Loc;
267 }
268
269 bool operator!=(const FullSourceLoc& RHS) const {
270 return SrcMgr != RHS.SrcMgr || Loc != RHS.Loc;
271 }
Chris Lattner5c38b632008-09-29 21:46:13 +0000272
273 /// Prints information about this FullSourceLoc to stderr. Useful for
274 /// debugging.
275 void dump() const;
Ted Kremeneka9793ed2007-12-12 18:16:46 +0000276};
277
Reid Spencer5f016e22007-07-11 17:01:13 +0000278} // end namespace clang
279
Reid Spencer5f016e22007-07-11 17:01:13 +0000280#endif