//===--- 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 that should be used instead // of std::pair. // //===----------------------------------------------------------------------===// #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 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 bool operator ==(const Located &lhs, const Located &rhs) { return lhs.Item == rhs.Item && lhs.Loc == rhs.Loc; } } // end namespace swift namespace llvm { template struct DenseMapInfo; template struct DenseMapInfo> { static inline swift::Located getEmptyKey() { return swift::Located(DenseMapInfo::getEmptyKey(), DenseMapInfo::getEmptyKey()); } static inline swift::Located getTombstoneKey() { return swift::Located(DenseMapInfo::getTombstoneKey(), DenseMapInfo::getTombstoneKey()); } static unsigned getHashValue(const swift::Located &LocatedVal) { return combineHashValue(DenseMapInfo::getHashValue(LocatedVal.Item), DenseMapInfo::getHashValue(LocatedVal.Loc)); } static bool isEqual(const swift::Located &LHS, const swift::Located &RHS) { return DenseMapInfo::isEqual(LHS.Item, RHS.Item) && DenseMapInfo::isEqual(LHS.Loc, RHS.Loc); } }; } // namespace llvm #endif // SWIFT_BASIC_LOCATED_H