Skip to content

Commit 022314a

Browse files
Merge pull request #28643 from kitaisreal/using-located-instead-of-pair
[Compiler]: Using Located<T> instead of std::pair<SourceLoc, T>
2 parents c5a22e1 + 76373b6 commit 022314a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+424
-316
lines changed

include/swift/AST/ASTContext.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "swift/AST/Types.h"
2626
#include "swift/AST/TypeAlignments.h"
2727
#include "swift/Basic/LangOptions.h"
28+
#include "swift/Basic/Located.h"
2829
#include "swift/Basic/Malloc.h"
2930
#include "llvm/ADT/ArrayRef.h"
3031
#include "llvm/ADT/DenseMap.h"
@@ -721,12 +722,12 @@ class ASTContext final {
721722
///
722723
/// Note that even if this check succeeds, errors may still occur if the
723724
/// module is loaded in full.
724-
bool canImportModule(std::pair<Identifier, SourceLoc> ModulePath);
725+
bool canImportModule(Located<Identifier> ModulePath);
725726

726727
/// \returns a module with a given name that was already loaded. If the
727728
/// module was not loaded, returns nullptr.
728729
ModuleDecl *getLoadedModule(
729-
ArrayRef<std::pair<Identifier, SourceLoc>> ModulePath) const;
730+
ArrayRef<Located<Identifier>> ModulePath) const;
730731

731732
ModuleDecl *getLoadedModule(Identifier ModuleName) const;
732733

@@ -736,7 +737,7 @@ class ASTContext final {
736737
/// be returned.
737738
///
738739
/// \returns The requested module, or NULL if the module cannot be found.
739-
ModuleDecl *getModule(ArrayRef<std::pair<Identifier, SourceLoc>> ModulePath);
740+
ModuleDecl *getModule(ArrayRef<Located<Identifier>> ModulePath);
740741

741742
ModuleDecl *getModuleByName(StringRef ModuleName);
742743

include/swift/AST/Attr.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "swift/Basic/Range.h"
2626
#include "swift/Basic/OptimizationMode.h"
2727
#include "swift/Basic/Version.h"
28+
#include "swift/Basic/Located.h"
2829
#include "swift/AST/Identifier.h"
2930
#include "swift/AST/AttrKind.h"
3031
#include "swift/AST/AutoDiff.h"
@@ -69,16 +70,13 @@ class TypeAttributes {
6970
struct Convention {
7071
StringRef Name = {};
7172
DeclNameRef WitnessMethodProtocol = {};
72-
StringRef ClangType = {};
73-
// Carry the source location for diagnostics.
74-
SourceLoc ClangTypeLoc = {};
75-
73+
Located<StringRef> ClangType = Located<StringRef>(StringRef(), {});
7674
/// Convenience factory function to create a Swift convention.
7775
///
7876
/// Don't use this function if you are creating a C convention as you
7977
/// probably need a ClangType field as well.
8078
static Convention makeSwiftConvention(StringRef name) {
81-
return {name, DeclNameRef(), "", {}};
79+
return {name, DeclNameRef(), Located<StringRef>("", {})};
8280
}
8381
};
8482

include/swift/AST/Decl.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "swift/Basic/NullablePtr.h"
4141
#include "swift/Basic/OptionalEnum.h"
4242
#include "swift/Basic/Range.h"
43+
#include "swift/Basic/Located.h"
4344
#include "llvm/ADT/DenseMap.h"
4445
#include "llvm/ADT/DenseSet.h"
4546
#include "llvm/Support/TrailingObjects.h"
@@ -1508,11 +1509,11 @@ enum class ImportKind : uint8_t {
15081509
/// import Swift
15091510
/// import typealias Swift.Int
15101511
class ImportDecl final : public Decl,
1511-
private llvm::TrailingObjects<ImportDecl, std::pair<Identifier,SourceLoc>> {
1512+
private llvm::TrailingObjects<ImportDecl, Located<Identifier>> {
15121513
friend TrailingObjects;
15131514
friend class Decl;
15141515
public:
1515-
typedef std::pair<Identifier, SourceLoc> AccessPathElement;
1516+
typedef Located<Identifier> AccessPathElement;
15161517

15171518
private:
15181519
SourceLoc ImportLoc;
@@ -1582,9 +1583,9 @@ class ImportDecl final : public Decl,
15821583
}
15831584

15841585
SourceLoc getStartLoc() const { return ImportLoc; }
1585-
SourceLoc getLocFromSource() const { return getFullAccessPath().front().second; }
1586+
SourceLoc getLocFromSource() const { return getFullAccessPath().front().Loc; }
15861587
SourceRange getSourceRange() const {
1587-
return SourceRange(ImportLoc, getFullAccessPath().back().second);
1588+
return SourceRange(ImportLoc, getFullAccessPath().back().Loc);
15881589
}
15891590
SourceLoc getKindLoc() const { return KindLoc; }
15901591

include/swift/AST/Module.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,14 @@ enum class ResilienceStrategy : unsigned {
132132
/// \sa FileUnit
133133
class ModuleDecl : public DeclContext, public TypeDecl {
134134
public:
135-
typedef ArrayRef<std::pair<Identifier, SourceLoc>> AccessPathTy;
135+
typedef ArrayRef<Located<Identifier>> AccessPathTy;
136136
typedef std::pair<ModuleDecl::AccessPathTy, ModuleDecl*> ImportedModule;
137137

138138
static bool matchesAccessPath(AccessPathTy AccessPath, DeclName Name) {
139139
assert(AccessPath.size() <= 1 && "can only refer to top-level decls");
140140

141141
return AccessPath.empty()
142-
|| DeclName(AccessPath.front().first).matchesRef(Name);
142+
|| DeclName(AccessPath.front().Item).matchesRef(Name);
143143
}
144144

145145
/// Arbitrarily orders ImportedModule records, for inclusion in sets and such.

include/swift/AST/ModuleLoader.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "swift/AST/Identifier.h"
2121
#include "swift/Basic/LLVM.h"
22+
#include "swift/Basic/Located.h"
2223
#include "swift/Basic/SourceLoc.h"
2324
#include "llvm/ADT/SetVector.h"
2425
#include "llvm/ADT/SmallSet.h"
@@ -100,7 +101,7 @@ class ModuleLoader {
100101
///
101102
/// Note that even if this check succeeds, errors may still occur if the
102103
/// module is loaded in full.
103-
virtual bool canImportModule(std::pair<Identifier, SourceLoc> named) = 0;
104+
virtual bool canImportModule(Located<Identifier> named) = 0;
104105

105106
/// Import a module with the given module path.
106107
///
@@ -113,7 +114,7 @@ class ModuleLoader {
113114
/// emits a diagnostic and returns NULL.
114115
virtual
115116
ModuleDecl *loadModule(SourceLoc importLoc,
116-
ArrayRef<std::pair<Identifier, SourceLoc>> path) = 0;
117+
ArrayRef<Located<Identifier>> path) = 0;
117118

118119
/// Load extensions to the given nominal type.
119120
///

include/swift/AST/NameLookup.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,15 +495,15 @@ void recordLookupOfTopLevelName(DeclContext *topLevelContext, DeclName name,
495495
void getDirectlyInheritedNominalTypeDecls(
496496
llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl,
497497
unsigned i,
498-
llvm::SmallVectorImpl<std::pair<SourceLoc, NominalTypeDecl *>> &result,
498+
llvm::SmallVectorImpl<Located<NominalTypeDecl *>> &result,
499499
bool &anyObject);
500500

501501
/// Retrieve the set of nominal type declarations that are directly
502502
/// "inherited" by the given declaration, looking through typealiases
503503
/// and splitting out the components of compositions.
504504
///
505505
/// If we come across the AnyObject type, set \c anyObject true.
506-
SmallVector<std::pair<SourceLoc, NominalTypeDecl *>, 4>
506+
SmallVector<Located<NominalTypeDecl *>, 4>
507507
getDirectlyInheritedNominalTypeDecls(
508508
llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl,
509509
bool &anyObject);

include/swift/AST/TypeRepr.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm/ADT/PointerUnion.h"
2727
#include "llvm/ADT/STLExtras.h"
2828
#include "swift/Basic/Debug.h"
29+
#include "swift/Basic/Located.h"
2930
#include "swift/Basic/InlineBitfield.h"
3031
#include "llvm/Support/ErrorHandling.h"
3132
#include "llvm/Support/TrailingObjects.h"
@@ -672,9 +673,9 @@ struct TupleTypeReprElement {
672673
/// \endcode
673674
class TupleTypeRepr final : public TypeRepr,
674675
private llvm::TrailingObjects<TupleTypeRepr, TupleTypeReprElement,
675-
std::pair<SourceLoc, unsigned>> {
676+
Located<unsigned>> {
676677
friend TrailingObjects;
677-
typedef std::pair<SourceLoc, unsigned> SourceLocAndIdx;
678+
typedef Located<unsigned> SourceLocAndIdx;
678679

679680
SourceRange Parens;
680681

@@ -745,21 +746,21 @@ class TupleTypeRepr final : public TypeRepr,
745746

746747
SourceLoc getEllipsisLoc() const {
747748
return hasEllipsis() ?
748-
getTrailingObjects<SourceLocAndIdx>()[0].first : SourceLoc();
749+
getTrailingObjects<SourceLocAndIdx>()[0].Loc : SourceLoc();
749750
}
750751

751752
unsigned getEllipsisIndex() const {
752753
return hasEllipsis() ?
753-
getTrailingObjects<SourceLocAndIdx>()[0].second :
754+
getTrailingObjects<SourceLocAndIdx>()[0].Item :
754755
Bits.TupleTypeRepr.NumElements;
755756
}
756757

757758
void removeEllipsis() {
758759
if (hasEllipsis()) {
759760
Bits.TupleTypeRepr.HasEllipsis = false;
760761
getTrailingObjects<SourceLocAndIdx>()[0] = {
761-
SourceLoc(),
762-
getNumElements()
762+
getNumElements(),
763+
SourceLoc()
763764
};
764765
}
765766
}

include/swift/Basic/Located.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//===--- Located.h - Source Location and Associated Value ----------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2019 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// Provides a currency data type Located<T> that should be used instead
14+
// of std::pair<T, SourceLoc>.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
19+
#ifndef SWIFT_BASIC_LOCATED_H
20+
#define SWIFT_BASIC_LOCATED_H
21+
#include "swift/Basic/Debug.h"
22+
#include "swift/Basic/LLVM.h"
23+
#include "swift/Basic/SourceLoc.h"
24+
25+
namespace swift {
26+
27+
/// A currency type for keeping track of items which were found in the source code.
28+
/// Several parts of the compiler need to keep track of a `SourceLoc` corresponding
29+
/// to an item, in case they need to report some diagnostics later. For example,
30+
/// the ClangImporter needs to keep track of where imports were originally written.
31+
/// Located makes it easy to do so while making the code more readable, compared to
32+
/// using `std::pair`.
33+
template<typename T>
34+
struct Located {
35+
36+
/// The main item whose source location is being tracked.
37+
T Item;
38+
39+
/// The original source location from which the item was parsed.
40+
SourceLoc Loc;
41+
42+
Located() : Item(), Loc() {}
43+
44+
Located(T Item, SourceLoc loc) : Item(Item), Loc(loc) {}
45+
46+
SWIFT_DEBUG_DUMP;
47+
void dump(raw_ostream &os) const;
48+
49+
template<typename U>
50+
friend bool operator ==(const Located<U> &lhs, const Located<U> &rhs) {
51+
return lhs.Item == rhs.Item && lhs.Loc == rhs.Loc;
52+
}
53+
};
54+
55+
} // end namespace swift
56+
57+
namespace llvm {
58+
59+
template <typename T> struct DenseMapInfo;
60+
61+
template<typename T>
62+
struct DenseMapInfo<swift::Located<T>> {
63+
64+
static inline swift::Located<T> getEmptyKey() {
65+
return swift::Located<T>(DenseMapInfo<T>::getEmptyKey(),
66+
DenseMapInfo<swift::SourceLoc>::getEmptyKey());
67+
}
68+
69+
static inline swift::Located<T> getTombstoneKey() {
70+
return swift::Located<T>(DenseMapInfo<T>::getTombstoneKey(),
71+
DenseMapInfo<swift::SourceLoc>::getTombstoneKey());
72+
}
73+
74+
static unsigned getHashValue(const swift::Located<T> &LocatedVal) {
75+
return combineHashValue(DenseMapInfo<T>::getHashValue(LocatedVal.Item),
76+
DenseMapInfo<swift::SourceLoc>::getHashValue(LocatedVal.Loc));
77+
}
78+
79+
static bool isEqual(const swift::Located<T> &LHS, const swift::Located<T> &RHS) {
80+
return DenseMapInfo<T>::isEqual(LHS.Item, RHS.Item) &&
81+
DenseMapInfo<T>::isEqual(LHS.Loc, RHS.Loc);
82+
}
83+
};
84+
} // namespace llvm
85+
86+
#endif // SWIFT_BASIC_LOCATED_H

include/swift/ClangImporter/ClangImporter.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class ClangImporter final : public ClangModuleLoader {
173173
///
174174
/// Note that even if this check succeeds, errors may still occur if the
175175
/// module is loaded in full.
176-
virtual bool canImportModule(std::pair<Identifier, SourceLoc> named) override;
176+
virtual bool canImportModule(Located<Identifier> named) override;
177177

178178
/// Import a module with the given module path.
179179
///
@@ -189,7 +189,7 @@ class ClangImporter final : public ClangModuleLoader {
189189
/// emits a diagnostic and returns NULL.
190190
virtual ModuleDecl *loadModule(
191191
SourceLoc importLoc,
192-
ArrayRef<std::pair<Identifier, SourceLoc>> path)
192+
ArrayRef<Located<Identifier>> path)
193193
override;
194194

195195
/// Determine whether \c overlayDC is within an overlay module for the
@@ -399,7 +399,7 @@ class ClangImporter final : public ClangModuleLoader {
399399
/// Given the path of a Clang module, collect the names of all its submodules.
400400
/// Calling this function does not load the module.
401401
void collectSubModuleNames(
402-
ArrayRef<std::pair<Identifier, SourceLoc>> path,
402+
ArrayRef<Located<Identifier>> path,
403403
std::vector<std::string> &names) const;
404404

405405
/// Given a Clang module, decide whether this module is imported already.

include/swift/IDE/Utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ class NameMatcher: public ASTWalker {
240240

241241
/// The \c Expr argument of a parent \c CustomAttr (if one exists) and
242242
/// the \c SourceLoc of the type name it applies to.
243-
llvm::Optional<std::pair<SourceLoc, Expr *>> CustomAttrArg;
243+
llvm::Optional<Located<Expr *>> CustomAttrArg;
244244
unsigned InactiveConfigRegionNestings = 0;
245245
unsigned SelectorNestings = 0;
246246

include/swift/Parse/CodeCompletionCallbacks.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class CodeCompletionCallbacks {
190190

191191
/// Complete the import decl with importable modules.
192192
virtual void
193-
completeImportDecl(std::vector<std::pair<Identifier, SourceLoc>> &Path) {};
193+
completeImportDecl(std::vector<Located<Identifier>> &Path) {};
194194

195195
/// Complete unresolved members after dot.
196196
virtual void completeUnresolvedMember(CodeCompletionExpr *E,

include/swift/Parse/Parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class Parser {
120120
DeclContext *CurDeclContext;
121121
ASTContext &Context;
122122
CodeCompletionCallbacks *CodeCompletion = nullptr;
123-
std::vector<std::pair<SourceLoc, std::vector<ParamDecl*>>> AnonClosureVars;
123+
std::vector<Located<std::vector<ParamDecl*>>> AnonClosureVars;
124124

125125
bool IsParsingInterfaceTokens = false;
126126

include/swift/Sema/SourceLoader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class SourceLoader : public ModuleLoader {
5656
///
5757
/// Note that even if this check succeeds, errors may still occur if the
5858
/// module is loaded in full.
59-
virtual bool canImportModule(std::pair<Identifier, SourceLoc> named) override;
59+
virtual bool canImportModule(Located<Identifier> named) override;
6060

6161
/// Import a module with the given module path.
6262
///
@@ -69,7 +69,7 @@ class SourceLoader : public ModuleLoader {
6969
/// returns NULL.
7070
virtual ModuleDecl *
7171
loadModule(SourceLoc importLoc,
72-
ArrayRef<std::pair<Identifier, SourceLoc>> path) override;
72+
ArrayRef<Located<Identifier>> path) override;
7373

7474
/// Load extensions to the given nominal type.
7575
///

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class SerializedModuleLoaderBase : public ModuleLoader {
5050
void collectVisibleTopLevelModuleNamesImpl(SmallVectorImpl<Identifier> &names,
5151
StringRef extension) const;
5252

53-
using AccessPathElem = std::pair<Identifier, SourceLoc>;
53+
using AccessPathElem = Located<Identifier>;
5454
bool findModule(AccessPathElem moduleID,
5555
SmallVectorImpl<char> *moduleInterfacePath,
5656
std::unique_ptr<llvm::MemoryBuffer> *moduleBuffer,
@@ -140,7 +140,7 @@ class SerializedModuleLoaderBase : public ModuleLoader {
140140
///
141141
/// Note that even if this check succeeds, errors may still occur if the
142142
/// module is loaded in full.
143-
virtual bool canImportModule(std::pair<Identifier, SourceLoc> named) override;
143+
virtual bool canImportModule(Located<Identifier> named) override;
144144

145145
/// Import a module with the given module path.
146146
///
@@ -153,7 +153,7 @@ class SerializedModuleLoaderBase : public ModuleLoader {
153153
/// emits a diagnostic and returns a FailedImportModule object.
154154
virtual ModuleDecl *
155155
loadModule(SourceLoc importLoc,
156-
ArrayRef<std::pair<Identifier, SourceLoc>> path) override;
156+
ArrayRef<Located<Identifier>> path) override;
157157

158158

159159
virtual void loadExtensions(NominalTypeDecl *nominal,
@@ -240,10 +240,10 @@ class MemoryBufferSerializedModuleLoader : public SerializedModuleLoaderBase {
240240
public:
241241
virtual ~MemoryBufferSerializedModuleLoader();
242242

243-
bool canImportModule(std::pair<Identifier, SourceLoc> named) override;
243+
bool canImportModule(Located<Identifier> named) override;
244244
ModuleDecl *
245245
loadModule(SourceLoc importLoc,
246-
ArrayRef<std::pair<Identifier, SourceLoc>> path) override;
246+
ArrayRef<Located<Identifier>> path) override;
247247

248248
/// Register a memory buffer that contains the serialized module for the given
249249
/// access path. This API is intended to be used by LLDB to add swiftmodules

0 commit comments

Comments
 (0)