22//
33// This source file is part of the Swift.org open source project
44//
5- // Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+ // Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66// Licensed under Apache License v2.0 with Runtime Library Exception
77//
88// See https://swift.org/LICENSE.txt for license information
2626#include < assert.h>
2727#include < functional>
2828
29+ namespace llvm {
30+ class SMLoc ;
31+ }
32+
2933namespace swift {
30- class SourceManager ;
34+ class SourceManager ;
3135
32- // / SourceLoc in swift is just an SMLoc . We define it as a different type
33- // / (instead of as a typedef) just to remove the "getFromPointer" methods and
34- // / enforce purity in the Swift codebase.
36+ // / ` SourceLoc` just wraps a `const char *` . We define it as a different type
37+ // / (instead of as a typedef) from `llvm::SMLoc` to enforce purity in the
38+ // / Swift codebase.
3539class SourceLoc {
3640 friend class SourceManager ;
3741 friend class SourceRange ;
3842 friend class CharSourceRange ;
3943 friend class DiagnosticConsumer ;
4044
41- llvm::SMLoc Value ;
45+ const char *Pointer = nullptr ;
4246
4347public:
4448 SourceLoc () {}
45- explicit SourceLoc (llvm::SMLoc Value) : Value(Value) {}
46-
47- bool isValid () const { return Value.isValid (); }
49+
50+ static SourceLoc getFromPointer (const char *Pointer) {
51+ SourceLoc Loc;
52+ Loc.Pointer = Pointer;
53+ return Loc;
54+ }
55+
56+ const char *getPointer () const { return Pointer; }
57+
58+ bool isValid () const { return Pointer != nullptr ; }
4859 bool isInvalid () const { return !isValid (); }
4960
5061 // / An explicit bool operator so one can check if a SourceLoc is valid in an
@@ -53,14 +64,15 @@ class SourceLoc {
5364 // / if (auto x = getSourceLoc()) { ... }
5465 explicit operator bool () const { return isValid (); }
5566
56- bool operator ==(const SourceLoc &RHS) const { return RHS.Value == Value; }
67+ operator llvm::SMLoc () const { return llvm::SMLoc::getFromPointer (Pointer); }
68+
69+ bool operator ==(const SourceLoc &RHS) const { return RHS.Pointer == Pointer; }
5770 bool operator !=(const SourceLoc &RHS) const { return !operator ==(RHS); }
58-
71+
5972 // / Return a source location advanced a specified number of bytes.
6073 SourceLoc getAdvancedLoc (int ByteOffset) const {
6174 assert (isValid () && " Can't advance an invalid location" );
62- return SourceLoc (
63- llvm::SMLoc::getFromPointer (Value.getPointer () + ByteOffset));
75+ return SourceLoc::getFromPointer (Pointer + ByteOffset);
6476 }
6577
6678 SourceLoc getAdvancedLocOrInvalid (int ByteOffset) const {
@@ -69,7 +81,7 @@ class SourceLoc {
6981 return SourceLoc ();
7082 }
7183
72- const void *getOpaquePointerValue () const { return Value. getPointer () ; }
84+ const void *getOpaquePointerValue () const { return Pointer ; }
7385
7486 // / Print out the SourceLoc. If this location is in the same buffer
7587 // / as specified by \c LastBufferID, then we don't print the filename. If
@@ -88,13 +100,13 @@ class SourceLoc {
88100
89101 SWIFT_DEBUG_DUMPER (dump(const SourceManager &SM));
90102
91- friend size_t hash_value (SourceLoc loc) {
92- return reinterpret_cast <uintptr_t >(loc.getOpaquePointerValue ());
93- }
103+ friend size_t hash_value (SourceLoc loc) {
104+ return reinterpret_cast <uintptr_t >(loc.getOpaquePointerValue ());
105+ }
94106
95- friend void simple_display (raw_ostream &OS, const SourceLoc &loc) {
96- // Nothing meaningful to print.
97- }
107+ friend void simple_display (raw_ostream &OS, const SourceLoc &loc) {
108+ // Nothing meaningful to print.
109+ }
98110};
99111
100112// / SourceRange in swift is a pair of locations. However, note that the end
@@ -210,27 +222,27 @@ class CharSourceRange {
210222 bool contains (SourceLoc loc) const {
211223 auto less = std::less<const char *>();
212224 auto less_equal = std::less_equal<const char *>();
213- return less_equal (getStart ().Value . getPointer () , loc.Value . getPointer () ) &&
214- less (loc.Value . getPointer () , getEnd ().Value . getPointer () );
225+ return less_equal (getStart ().Pointer , loc.Pointer ) &&
226+ less (loc.Pointer , getEnd ().Pointer );
215227 }
216228
217229 bool contains (CharSourceRange Other) const {
218230 auto less_equal = std::less_equal<const char *>();
219231 return contains (Other.getStart ()) &&
220- less_equal (Other.getEnd ().Value . getPointer () , getEnd ().Value . getPointer () );
232+ less_equal (Other.getEnd ().Pointer , getEnd ().Pointer );
221233 }
222234
223235 // / expands *this to cover Other
224236 void widen (CharSourceRange Other) {
225- auto Diff = Other.getEnd ().Value . getPointer () - getEnd ().Value . getPointer () ;
237+ auto Diff = Other.getEnd ().Pointer - getEnd ().Pointer ;
226238 if (Diff > 0 ) {
227239 ByteLength += Diff;
228240 }
229- const auto MyStartPtr = getStart ().Value . getPointer () ;
230- Diff = MyStartPtr - Other.getStart ().Value . getPointer () ;
241+ const auto MyStartPtr = getStart ().Pointer ;
242+ Diff = MyStartPtr - Other.getStart ().Pointer ;
231243 if (Diff > 0 ) {
232244 ByteLength += Diff;
233- Start = SourceLoc ( llvm::SMLoc:: getFromPointer (MyStartPtr - Diff) );
245+ Start = SourceLoc:: getFromPointer (MyStartPtr - Diff);
234246 }
235247 }
236248
@@ -239,9 +251,7 @@ class CharSourceRange {
239251 return contains (Other.getStart ()) || Other.contains (getStart ());
240252 }
241253
242- StringRef str () const {
243- return StringRef (Start.Value .getPointer (), ByteLength);
244- }
254+ StringRef str () const { return StringRef (Start.Pointer , ByteLength); }
245255
246256 // / Return the length of this valid range in bytes. Can be zero.
247257 unsigned getByteLength () const {
@@ -272,15 +282,15 @@ template <typename T, typename Enable> struct DenseMapInfo;
272282
273283template <> struct DenseMapInfo <swift::SourceLoc> {
274284 static swift::SourceLoc getEmptyKey () {
275- return swift::SourceLoc (
276- SMLoc::getFromPointer ( DenseMapInfo<const char *>::getEmptyKey () ));
285+ return swift::SourceLoc::getFromPointer (
286+ DenseMapInfo<const char *>::getEmptyKey ());
277287 }
278288
279289 static swift::SourceLoc getTombstoneKey () {
280290 // Make this different from empty key. See for context:
281291 // http://lists.llvm.org/pipermail/llvm-dev/2015-July/088744.html
282- return swift::SourceLoc (
283- SMLoc::getFromPointer ( DenseMapInfo<const char *>::getTombstoneKey () ));
292+ return swift::SourceLoc::getFromPointer (
293+ DenseMapInfo<const char *>::getTombstoneKey ());
284294 }
285295
286296 static unsigned getHashValue (const swift::SourceLoc &Val) {
@@ -296,15 +306,15 @@ template <> struct DenseMapInfo<swift::SourceLoc> {
296306
297307template <> struct DenseMapInfo <swift::SourceRange> {
298308 static swift::SourceRange getEmptyKey () {
299- return swift::SourceRange (swift::SourceLoc (
300- SMLoc::getFromPointer ( DenseMapInfo<const char *>::getEmptyKey () )));
309+ return swift::SourceRange (swift::SourceLoc::getFromPointer (
310+ DenseMapInfo<const char *>::getEmptyKey ()));
301311 }
302312
303313 static swift::SourceRange getTombstoneKey () {
304314 // Make this different from empty key. See for context:
305315 // http://lists.llvm.org/pipermail/llvm-dev/2015-July/088744.html
306- return swift::SourceRange (swift::SourceLoc (
307- SMLoc::getFromPointer ( DenseMapInfo<const char *>::getTombstoneKey () )));
316+ return swift::SourceRange (swift::SourceLoc::getFromPointer (
317+ DenseMapInfo<const char *>::getTombstoneKey ()));
308318 }
309319
310320 static unsigned getHashValue (const swift::SourceRange &Val) {
0 commit comments