Skip to content

Commit b7cb3b6

Browse files
committed
SR-11889: Using Located<T> instead of std::pair<SourceLoc, T>
1 parent 06014e6 commit b7cb3b6

38 files changed

+209
-171
lines changed

include/swift/AST/ASTContext.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "swift/AST/TypeAlignments.h"
2727
#include "swift/Basic/LangOptions.h"
2828
#include "swift/Basic/Malloc.h"
29+
#include "swift/Basic/Located.h"
2930
#include "llvm/ADT/ArrayRef.h"
3031
#include "llvm/ADT/DenseMap.h"
3132
#include "llvm/ADT/IntrusiveRefCntPtr.h"
@@ -718,12 +719,12 @@ class ASTContext final {
718719
///
719720
/// Note that even if this check succeeds, errors may still occur if the
720721
/// module is loaded in full.
721-
bool canImportModule(std::pair<Identifier, SourceLoc> ModulePath);
722+
bool canImportModule(Located<Identifier> ModulePath);
722723

723724
/// \returns a module with a given name that was already loaded. If the
724725
/// module was not loaded, returns nullptr.
725726
ModuleDecl *getLoadedModule(
726-
ArrayRef<std::pair<Identifier, SourceLoc>> ModulePath) const;
727+
ArrayRef<Located<Identifier>> ModulePath) const;
727728

728729
ModuleDecl *getLoadedModule(Identifier ModuleName) const;
729730

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

738739
ModuleDecl *getModuleByName(StringRef ModuleName);
739740

include/swift/AST/Decl.h

+5-4
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"
@@ -1503,11 +1504,11 @@ enum class ImportKind : uint8_t {
15031504
/// import Swift
15041505
/// import typealias Swift.Int
15051506
class ImportDecl final : public Decl,
1506-
private llvm::TrailingObjects<ImportDecl, std::pair<Identifier,SourceLoc>> {
1507+
private llvm::TrailingObjects<ImportDecl, Located<Identifier>> {
15071508
friend TrailingObjects;
15081509
friend class Decl;
15091510
public:
1510-
typedef std::pair<Identifier, SourceLoc> AccessPathElement;
1511+
typedef Located<Identifier> AccessPathElement;
15111512

15121513
private:
15131514
SourceLoc ImportLoc;
@@ -1577,9 +1578,9 @@ class ImportDecl final : public Decl,
15771578
}
15781579

15791580
SourceLoc getStartLoc() const { return ImportLoc; }
1580-
SourceLoc getLocFromSource() const { return getFullAccessPath().front().second; }
1581+
SourceLoc getLocFromSource() const { return getFullAccessPath().front().loc; }
15811582
SourceRange getSourceRange() const {
1582-
return SourceRange(ImportLoc, getFullAccessPath().back().second);
1583+
return SourceRange(ImportLoc, getFullAccessPath().back().loc);
15831584
}
15841585
SourceLoc getKindLoc() const { return KindLoc; }
15851586

include/swift/AST/Module.h

+2-2
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

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/ADT/SetVector.h"
2424
#include "llvm/ADT/SmallSet.h"
2525
#include "llvm/ADT/TinyPtrVector.h"
26+
#include "swift/Basic/Located.h"
2627

2728
namespace llvm {
2829
class FileCollector;
@@ -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/Basic/Located.h

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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) 2014 - 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+
// This file forward declares and imports various common LLVM datatypes that
14+
// swift wants to use unqualified.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
19+
#ifndef SWIFT_BASIC_LOCATED_H
20+
#define SWIFT_BASIC_LOCATED_H
21+
#include "swift/Basic/SourceLoc.h"
22+
23+
namespace swift {
24+
25+
template<typename T>
26+
struct Located {
27+
28+
T item;
29+
30+
SourceLoc loc;
31+
32+
template<typename U>
33+
friend bool operator ==(const Located<U> lhs, const Located<U> rhs) {
34+
return lhs.item == rhs.item && lhs.loc == rhs.loc;
35+
}
36+
};
37+
}
38+
39+
#endif // SWIFT_BASIC_LOCATED_H

include/swift/ClangImporter/ClangImporter.h

+3-3
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/Parse/CodeCompletionCallbacks.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class CodeCompletionCallbacks {
194194

195195
/// Complete the import decl with importable modules.
196196
virtual void
197-
completeImportDecl(std::vector<std::pair<Identifier, SourceLoc>> &Path) {};
197+
completeImportDecl(std::vector<Located<Identifier>> &Path) {};
198198

199199
/// Complete unresolved members after dot.
200200
virtual void completeUnresolvedMember(CodeCompletionExpr *E,

include/swift/Sema/SourceLoader.h

+2-2
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

+5-5
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

lib/AST/ASTContext.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -1468,12 +1468,12 @@ ClangModuleLoader *ASTContext::getDWARFModuleLoader() const {
14681468
}
14691469

14701470
ModuleDecl *ASTContext::getLoadedModule(
1471-
ArrayRef<std::pair<Identifier, SourceLoc>> ModulePath) const {
1471+
ArrayRef<Located<Identifier>> ModulePath) const {
14721472
assert(!ModulePath.empty());
14731473

14741474
// TODO: Swift submodules.
14751475
if (ModulePath.size() == 1) {
1476-
return getLoadedModule(ModulePath[0].first);
1476+
return getLoadedModule(ModulePath[0].item);
14771477
}
14781478
return nullptr;
14791479
}
@@ -1723,13 +1723,13 @@ bool ASTContext::shouldPerformTypoCorrection() {
17231723
return NumTypoCorrections <= LangOpts.TypoCorrectionLimit;
17241724
}
17251725

1726-
bool ASTContext::canImportModule(std::pair<Identifier, SourceLoc> ModulePath) {
1726+
bool ASTContext::canImportModule(Located<Identifier> ModulePath) {
17271727
// If this module has already been successfully imported, it is importable.
17281728
if (getLoadedModule(ModulePath) != nullptr)
17291729
return true;
17301730

17311731
// If we've failed loading this module before, don't look for it again.
1732-
if (FailedModuleImportNames.count(ModulePath.first))
1732+
if (FailedModuleImportNames.count(ModulePath.item))
17331733
return false;
17341734

17351735
// Otherwise, ask the module loaders.
@@ -1739,20 +1739,20 @@ bool ASTContext::canImportModule(std::pair<Identifier, SourceLoc> ModulePath) {
17391739
}
17401740
}
17411741

1742-
FailedModuleImportNames.insert(ModulePath.first);
1742+
FailedModuleImportNames.insert(ModulePath.item);
17431743
return false;
17441744
}
17451745

17461746
ModuleDecl *
1747-
ASTContext::getModule(ArrayRef<std::pair<Identifier, SourceLoc>> ModulePath) {
1747+
ASTContext::getModule(ArrayRef<Located<Identifier>> ModulePath) {
17481748
assert(!ModulePath.empty());
17491749

17501750
if (auto *M = getLoadedModule(ModulePath))
17511751
return M;
17521752

17531753
auto moduleID = ModulePath[0];
17541754
for (auto &importer : getImpl().ModuleLoaders) {
1755-
if (ModuleDecl *M = importer->loadModule(moduleID.second, ModulePath)) {
1755+
if (ModuleDecl *M = importer->loadModule(moduleID.loc, ModulePath)) {
17561756
return M;
17571757
}
17581758
}
@@ -1761,7 +1761,7 @@ ASTContext::getModule(ArrayRef<std::pair<Identifier, SourceLoc>> ModulePath) {
17611761
}
17621762

17631763
ModuleDecl *ASTContext::getModuleByName(StringRef ModuleName) {
1764-
SmallVector<std::pair<Identifier, SourceLoc>, 4>
1764+
SmallVector<Located<Identifier>, 4>
17651765
AccessPath;
17661766
while (!ModuleName.empty()) {
17671767
StringRef SubModuleName;
@@ -1778,7 +1778,7 @@ ModuleDecl *ASTContext::getStdlibModule(bool loadIfAbsent) {
17781778
if (loadIfAbsent) {
17791779
auto mutableThis = const_cast<ASTContext*>(this);
17801780
TheStdlibModule =
1781-
mutableThis->getModule({ std::make_pair(StdlibModuleName, SourceLoc()) });
1781+
mutableThis->getModule({{ StdlibModuleName, SourceLoc() }});
17821782
} else {
17831783
TheStdlibModule = getLoadedModule(StdlibModuleName);
17841784
}

lib/AST/ASTDumper.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ namespace {
589589
OS << " '";
590590
interleave(ID->getFullAccessPath(),
591591
[&](const ImportDecl::AccessPathElement &Elem) {
592-
OS << Elem.first;
592+
OS << Elem.item;
593593
},
594594
[&] { OS << '.'; });
595595
OS << "')";

lib/AST/ASTPrinter.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2097,10 +2097,10 @@ void PrintAST::visitImportDecl(ImportDecl *decl) {
20972097
interleave(decl->getFullAccessPath(),
20982098
[&](const ImportDecl::AccessPathElement &Elem) {
20992099
if (!Mods.empty()) {
2100-
Printer.printModuleRef(Mods.front(), Elem.first);
2100+
Printer.printModuleRef(Mods.front(), Elem.item);
21012101
Mods = Mods.slice(1);
21022102
} else {
2103-
Printer << Elem.first.str();
2103+
Printer << Elem.item.str();
21042104
}
21052105
},
21062106
[&] { Printer << "."; });

lib/AST/ImportCache.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void ImportSet::Profile(
5757
for (auto import : topLevelImports) {
5858
ID.AddInteger(import.first.size());
5959
for (auto accessPathElt : import.first) {
60-
ID.AddPointer(accessPathElt.first.getAsOpaquePointer());
60+
ID.AddPointer(accessPathElt.item.getAsOpaquePointer());
6161
}
6262
ID.AddPointer(import.second);
6363
}

0 commit comments

Comments
 (0)