SR-11889: Using Located<T> instead of std::pair<SourceLoc, T>

This commit is contained in:
Kita, Maksim
2019-12-08 22:51:48 +03:00
parent 06014e6226
commit b7cb3b67bf
38 changed files with 209 additions and 171 deletions

View File

@@ -26,6 +26,7 @@
#include "swift/AST/TypeAlignments.h"
#include "swift/Basic/LangOptions.h"
#include "swift/Basic/Malloc.h"
#include "swift/Basic/Located.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
@@ -718,12 +719,12 @@ public:
///
/// Note that even if this check succeeds, errors may still occur if the
/// module is loaded in full.
bool canImportModule(std::pair<Identifier, SourceLoc> ModulePath);
bool canImportModule(Located<Identifier> ModulePath);
/// \returns a module with a given name that was already loaded. If the
/// module was not loaded, returns nullptr.
ModuleDecl *getLoadedModule(
ArrayRef<std::pair<Identifier, SourceLoc>> ModulePath) const;
ArrayRef<Located<Identifier>> ModulePath) const;
ModuleDecl *getLoadedModule(Identifier ModuleName) const;
@@ -733,7 +734,7 @@ public:
/// be returned.
///
/// \returns The requested module, or NULL if the module cannot be found.
ModuleDecl *getModule(ArrayRef<std::pair<Identifier, SourceLoc>> ModulePath);
ModuleDecl *getModule(ArrayRef<Located<Identifier>> ModulePath);
ModuleDecl *getModuleByName(StringRef ModuleName);

View File

@@ -40,6 +40,7 @@
#include "swift/Basic/NullablePtr.h"
#include "swift/Basic/OptionalEnum.h"
#include "swift/Basic/Range.h"
#include "swift/Basic/Located.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/Support/TrailingObjects.h"
@@ -1503,11 +1504,11 @@ enum class ImportKind : uint8_t {
/// import Swift
/// import typealias Swift.Int
class ImportDecl final : public Decl,
private llvm::TrailingObjects<ImportDecl, std::pair<Identifier,SourceLoc>> {
private llvm::TrailingObjects<ImportDecl, Located<Identifier>> {
friend TrailingObjects;
friend class Decl;
public:
typedef std::pair<Identifier, SourceLoc> AccessPathElement;
typedef Located<Identifier> AccessPathElement;
private:
SourceLoc ImportLoc;
@@ -1577,9 +1578,9 @@ public:
}
SourceLoc getStartLoc() const { return ImportLoc; }
SourceLoc getLocFromSource() const { return getFullAccessPath().front().second; }
SourceLoc getLocFromSource() const { return getFullAccessPath().front().loc; }
SourceRange getSourceRange() const {
return SourceRange(ImportLoc, getFullAccessPath().back().second);
return SourceRange(ImportLoc, getFullAccessPath().back().loc);
}
SourceLoc getKindLoc() const { return KindLoc; }

View File

@@ -132,14 +132,14 @@ enum class ResilienceStrategy : unsigned {
/// \sa FileUnit
class ModuleDecl : public DeclContext, public TypeDecl {
public:
typedef ArrayRef<std::pair<Identifier, SourceLoc>> AccessPathTy;
typedef ArrayRef<Located<Identifier>> AccessPathTy;
typedef std::pair<ModuleDecl::AccessPathTy, ModuleDecl*> ImportedModule;
static bool matchesAccessPath(AccessPathTy AccessPath, DeclName Name) {
assert(AccessPath.size() <= 1 && "can only refer to top-level decls");
return AccessPath.empty()
|| DeclName(AccessPath.front().first).matchesRef(Name);
|| DeclName(AccessPath.front().item).matchesRef(Name);
}
/// Arbitrarily orders ImportedModule records, for inclusion in sets and such.

View File

@@ -23,6 +23,7 @@
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/TinyPtrVector.h"
#include "swift/Basic/Located.h"
namespace llvm {
class FileCollector;
@@ -100,7 +101,7 @@ public:
///
/// Note that even if this check succeeds, errors may still occur if the
/// module is loaded in full.
virtual bool canImportModule(std::pair<Identifier, SourceLoc> named) = 0;
virtual bool canImportModule(Located<Identifier> named) = 0;
/// Import a module with the given module path.
///
@@ -113,7 +114,7 @@ public:
/// emits a diagnostic and returns NULL.
virtual
ModuleDecl *loadModule(SourceLoc importLoc,
ArrayRef<std::pair<Identifier, SourceLoc>> path) = 0;
ArrayRef<Located<Identifier>> path) = 0;
/// Load extensions to the given nominal type.
///

View File

@@ -0,0 +1,39 @@
//===--- Located.h - Source Location and Associated Value ----------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 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
//
//===----------------------------------------------------------------------===//
//
// This file forward declares and imports various common LLVM datatypes that
// swift wants to use unqualified.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_BASIC_LOCATED_H
#define SWIFT_BASIC_LOCATED_H
#include "swift/Basic/SourceLoc.h"
namespace swift {
template<typename T>
struct Located {
T item;
SourceLoc loc;
template<typename U>
friend bool operator ==(const Located<U> lhs, const Located<U> rhs) {
return lhs.item == rhs.item && lhs.loc == rhs.loc;
}
};
}
#endif // SWIFT_BASIC_LOCATED_H

View File

@@ -173,7 +173,7 @@ public:
///
/// Note that even if this check succeeds, errors may still occur if the
/// module is loaded in full.
virtual bool canImportModule(std::pair<Identifier, SourceLoc> named) override;
virtual bool canImportModule(Located<Identifier> named) override;
/// Import a module with the given module path.
///
@@ -189,7 +189,7 @@ public:
/// emits a diagnostic and returns NULL.
virtual ModuleDecl *loadModule(
SourceLoc importLoc,
ArrayRef<std::pair<Identifier, SourceLoc>> path)
ArrayRef<Located<Identifier>> path)
override;
/// Determine whether \c overlayDC is within an overlay module for the
@@ -399,7 +399,7 @@ public:
/// Given the path of a Clang module, collect the names of all its submodules.
/// Calling this function does not load the module.
void collectSubModuleNames(
ArrayRef<std::pair<Identifier, SourceLoc>> path,
ArrayRef<Located<Identifier>> path,
std::vector<std::string> &names) const;
/// Given a Clang module, decide whether this module is imported already.

View File

@@ -194,7 +194,7 @@ public:
/// Complete the import decl with importable modules.
virtual void
completeImportDecl(std::vector<std::pair<Identifier, SourceLoc>> &Path) {};
completeImportDecl(std::vector<Located<Identifier>> &Path) {};
/// Complete unresolved members after dot.
virtual void completeUnresolvedMember(CodeCompletionExpr *E,

View File

@@ -56,7 +56,7 @@ public:
///
/// Note that even if this check succeeds, errors may still occur if the
/// module is loaded in full.
virtual bool canImportModule(std::pair<Identifier, SourceLoc> named) override;
virtual bool canImportModule(Located<Identifier> named) override;
/// Import a module with the given module path.
///
@@ -69,7 +69,7 @@ public:
/// returns NULL.
virtual ModuleDecl *
loadModule(SourceLoc importLoc,
ArrayRef<std::pair<Identifier, SourceLoc>> path) override;
ArrayRef<Located<Identifier>> path) override;
/// Load extensions to the given nominal type.
///

View File

@@ -50,7 +50,7 @@ protected:
void collectVisibleTopLevelModuleNamesImpl(SmallVectorImpl<Identifier> &names,
StringRef extension) const;
using AccessPathElem = std::pair<Identifier, SourceLoc>;
using AccessPathElem = Located<Identifier>;
bool findModule(AccessPathElem moduleID,
SmallVectorImpl<char> *moduleInterfacePath,
std::unique_ptr<llvm::MemoryBuffer> *moduleBuffer,
@@ -140,7 +140,7 @@ public:
///
/// Note that even if this check succeeds, errors may still occur if the
/// module is loaded in full.
virtual bool canImportModule(std::pair<Identifier, SourceLoc> named) override;
virtual bool canImportModule(Located<Identifier> named) override;
/// Import a module with the given module path.
///
@@ -153,7 +153,7 @@ public:
/// emits a diagnostic and returns a FailedImportModule object.
virtual ModuleDecl *
loadModule(SourceLoc importLoc,
ArrayRef<std::pair<Identifier, SourceLoc>> path) override;
ArrayRef<Located<Identifier>> path) override;
virtual void loadExtensions(NominalTypeDecl *nominal,
@@ -240,10 +240,10 @@ class MemoryBufferSerializedModuleLoader : public SerializedModuleLoaderBase {
public:
virtual ~MemoryBufferSerializedModuleLoader();
bool canImportModule(std::pair<Identifier, SourceLoc> named) override;
bool canImportModule(Located<Identifier> named) override;
ModuleDecl *
loadModule(SourceLoc importLoc,
ArrayRef<std::pair<Identifier, SourceLoc>> path) override;
ArrayRef<Located<Identifier>> path) override;
/// Register a memory buffer that contains the serialized module for the given
/// access path. This API is intended to be used by LLDB to add swiftmodules