-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Compiler]: Using Located<T> instead of std::pair<SourceLoc, T> #28643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b7cb3b6
c1444de
c88ac85
5fdea64
ea6a2dc
b4b3f98
c70bd2b
a2cce2b
646e9ac
76373b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
//===--- Located.h - Source Location and Associated Value ----------*- C++ -*-===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2019 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Provides a currency data type Located<T> that should be used instead | ||
// of std::pair<T, SourceLoc>. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
|
||
#ifndef SWIFT_BASIC_LOCATED_H | ||
#define SWIFT_BASIC_LOCATED_H | ||
#include "swift/Basic/Debug.h" | ||
#include "swift/Basic/LLVM.h" | ||
#include "swift/Basic/SourceLoc.h" | ||
|
||
namespace swift { | ||
|
||
/// A currency type for keeping track of items which were found in the source code. | ||
/// Several parts of the compiler need to keep track of a `SourceLoc` corresponding | ||
/// to an item, in case they need to report some diagnostics later. For example, | ||
/// the ClangImporter needs to keep track of where imports were originally written. | ||
/// Located makes it easy to do so while making the code more readable, compared to | ||
/// using `std::pair`. | ||
template<typename T> | ||
struct Located { | ||
|
||
/// The main item whose source location is being tracked. | ||
T Item; | ||
|
||
/// The original source location from which the item was parsed. | ||
SourceLoc Loc; | ||
|
||
Located() : Item(), Loc() {} | ||
|
||
Located(T Item, SourceLoc loc) : Item(Item), Loc(loc) {} | ||
|
||
SWIFT_DEBUG_DUMP; | ||
void dump(raw_ostream &os) const; | ||
|
||
template<typename U> | ||
CodaFi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
friend bool operator ==(const Located<U> &lhs, const Located<U> &rhs) { | ||
return lhs.Item == rhs.Item && lhs.Loc == rhs.Loc; | ||
} | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a You could add a new file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kitaisreal what do you think about this point? I think it might be useful to have this method for debugging. As to what is should print, perhaps something that prints the SourceLoc first and then the item, leaving a space in between (say) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @varungandhi-apple I agree, that we should introduce such function. Added. |
||
|
||
} // end namespace swift | ||
|
||
namespace llvm { | ||
|
||
template <typename T> struct DenseMapInfo; | ||
|
||
template<typename T> | ||
struct DenseMapInfo<swift::Located<T>> { | ||
|
||
static inline swift::Located<T> getEmptyKey() { | ||
return swift::Located<T>(DenseMapInfo<T>::getEmptyKey(), | ||
DenseMapInfo<swift::SourceLoc>::getEmptyKey()); | ||
} | ||
|
||
static inline swift::Located<T> getTombstoneKey() { | ||
return swift::Located<T>(DenseMapInfo<T>::getTombstoneKey(), | ||
DenseMapInfo<swift::SourceLoc>::getTombstoneKey()); | ||
} | ||
|
||
static unsigned getHashValue(const swift::Located<T> &LocatedVal) { | ||
return combineHashValue(DenseMapInfo<T>::getHashValue(LocatedVal.Item), | ||
DenseMapInfo<swift::SourceLoc>::getHashValue(LocatedVal.Loc)); | ||
} | ||
|
||
static bool isEqual(const swift::Located<T> &LHS, const swift::Located<T> &RHS) { | ||
return DenseMapInfo<T>::isEqual(LHS.Item, RHS.Item) && | ||
DenseMapInfo<T>::isEqual(LHS.Loc, RHS.Loc); | ||
} | ||
}; | ||
} // namespace llvm | ||
|
||
#endif // SWIFT_BASIC_LOCATED_H |
Uh oh!
There was an error while loading. Please reload this page.