1 //===--- SourceLocation.h - Compact identifier for Source Files -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the SourceLocation class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_SOURCELOCATION_H
15 #define LLVM_CLANG_SOURCELOCATION_H
18 #include "llvm/Bitcode/SerializationFwd.h"
22 template <typename T> struct DenseMapInfo;
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.
34 /// ID - Opaque identifier, 0 is "invalid".
39 bool isInvalid() const { return ID == 0; }
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; }
48 static FileID getSentinel() { return get(~0U); }
49 unsigned getHashValue() const { return ID; }
52 friend class SourceManager;
53 static FileID get(unsigned V) {
58 unsigned getOpaqueValue() const { return ID; }
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.
65 class SourceLocation {
67 friend class SourceManager;
73 SourceLocation() : ID(0) {} // 0 is an invalid FileID.
75 bool isFileID() const { return (ID & MacroIDBit) == 0; }
76 bool isMacroID() const { return (ID & MacroIDBit) != 0; }
78 /// isValid - Return true if this is a valid SourceLocation object. Invalid
79 /// SourceLocations are often used when events have no corresponding location
80 /// in the source (e.g. a diagnostic is required for a command line option).
82 bool isValid() const { return ID != 0; }
83 bool isInvalid() const { return ID == 0; }
86 /// getOffset - Return the index for SourceManager's SLocEntryTable table,
87 /// note that this is not an index *into* it though.
88 unsigned getOffset() const {
89 return ID & ~MacroIDBit;
92 static SourceLocation getFileLoc(unsigned ID) {
93 assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
99 static SourceLocation getMacroLoc(unsigned ID) {
100 assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
102 L.ID = MacroIDBit | ID;
107 /// getFileLocWithOffset - Return a source location with the specified offset
108 /// from this file SourceLocation.
109 SourceLocation getFileLocWithOffset(int Offset) const {
110 assert(((getOffset()+Offset) & MacroIDBit) == 0 && "invalid location");
116 /// getRawEncoding - When a SourceLocation itself cannot be used, this returns
117 /// an (opaque) 32-bit integer encoding for it. This should only be passed
118 /// to SourceLocation::getFromRawEncoding, it should not be inspected
120 unsigned getRawEncoding() const { return ID; }
123 /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into
124 /// a real SourceLocation.
125 static SourceLocation getFromRawEncoding(unsigned Encoding) {
131 /// Emit - Emit this SourceLocation object to Bitcode.
132 void Emit(llvm::Serializer& S) const;
134 /// ReadVal - Read a SourceLocation object from Bitcode.
135 static SourceLocation ReadVal(llvm::Deserializer& D);
138 inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
139 return LHS.getRawEncoding() == RHS.getRawEncoding();
142 inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
143 return !(LHS == RHS);
146 inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) {
147 return LHS.getRawEncoding() < RHS.getRawEncoding();
150 /// SourceRange - a trival tuple used to represent a source range.
155 SourceRange(): B(SourceLocation()), E(SourceLocation()) {}
156 SourceRange(SourceLocation loc) : B(loc), E(loc) {}
157 SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
159 SourceLocation getBegin() const { return B; }
160 SourceLocation getEnd() const { return E; }
162 void setBegin(SourceLocation b) { B = b; }
163 void setEnd(SourceLocation e) { E = e; }
165 bool isValid() const { return B.isValid() && E.isValid(); }
167 /// Emit - Emit this SourceRange object to Bitcode.
168 void Emit(llvm::Serializer& S) const;
170 /// ReadVal - Read a SourceRange object from Bitcode.
171 static SourceRange ReadVal(llvm::Deserializer& D);
174 /// FullSourceLoc - A SourceLocation and its associated SourceManager. Useful
175 /// for argument passing to functions that expect both objects.
176 class FullSourceLoc : public SourceLocation {
177 SourceManager* SrcMgr;
179 // Creates a FullSourceLoc where isValid() returns false.
180 explicit FullSourceLoc() : SrcMgr((SourceManager*) 0) {}
182 explicit FullSourceLoc(SourceLocation Loc, SourceManager &SM)
183 : SourceLocation(Loc), SrcMgr(&SM) {}
185 SourceManager& getManager() {
186 assert (SrcMgr && "SourceManager is NULL.");
190 const SourceManager& getManager() const {
191 assert (SrcMgr && "SourceManager is NULL.");
195 FileID getFileID() const;
197 FullSourceLoc getInstantiationLoc() const;
198 FullSourceLoc getSpellingLoc() const;
199 FullSourceLoc getIncludeLoc() const;
201 unsigned getLineNumber() const;
202 unsigned getColumnNumber() const;
204 unsigned getInstantiationLineNumber() const;
205 unsigned getInstantiationColumnNumber() const;
207 unsigned getSpellingLineNumber() const;
208 unsigned getSpellingColumnNumber() const;
210 const char *getCharacterData() const;
212 const llvm::MemoryBuffer* getBuffer() const;
214 const char* getSourceName() const;
216 bool isInSystemHeader() const;
218 /// Prints information about this FullSourceLoc to stderr. Useful for
223 operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
224 return LHS.getRawEncoding() == RHS.getRawEncoding() &&
225 LHS.SrcMgr == RHS.SrcMgr;
229 operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
230 return !(LHS == RHS);
235 } // end namespace clang
238 /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
241 struct DenseMapInfo<clang::FileID> {
242 static inline clang::FileID getEmptyKey() {
243 return clang::FileID();
245 static inline clang::FileID getTombstoneKey() {
246 return clang::FileID::getSentinel();
249 static unsigned getHashValue(clang::FileID S) {
250 return S.getHashValue();
253 static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
257 static bool isPod() { return true; }
260 } // end namespace llvm