Skip to content

Commit 1f6678a

Browse files
committed
[SourceKit] Merge RenameLoc and RenameLocation
It’s easier to understand the code if we don’t have these two nearly, but not quite, identical types.
1 parent b59f1e0 commit 1f6678a

File tree

11 files changed

+145
-153
lines changed

11 files changed

+145
-153
lines changed

include/swift/Refactoring/Refactoring.h

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#ifndef SWIFT_IDE_REFACTORING_H
14-
#define SWIFT_IDE_REFACTORING_H
13+
#ifndef SWIFT_REFACTORING_REFACTORING_H
14+
#define SWIFT_REFACTORING_REFACTORING_H
1515

1616
#include "swift/AST/DiagnosticConsumer.h"
1717
#include "swift/Basic/LLVM.h"
1818
#include "swift/Basic/StringExtras.h"
1919
#include "swift/IDE/CancellableResult.h"
2020
#include "swift/IDE/Utils.h"
21+
#include "swift/Refactoring/RenameLoc.h"
2122
#include "llvm/ADT/StringRef.h"
2223

2324
namespace swift {
@@ -61,21 +62,6 @@ struct RenameInfo {
6162

6263
llvm::Optional<RenameInfo> getRenameInfo(ResolvedCursorInfoPtr cursorInfo);
6364

64-
enum class NameUsage {
65-
Unknown,
66-
Reference,
67-
Definition,
68-
Call
69-
};
70-
71-
struct RenameLoc {
72-
unsigned Line;
73-
unsigned Column;
74-
NameUsage Usage;
75-
StringRef OldName;
76-
const bool IsFunctionLike;
77-
};
78-
7965
/// An array of \c RenameLoc that also keeps the underlying string storage of
8066
/// the \c StringRef inside the \c RenameLoc alive.
8167
class RenameLocs {
@@ -184,4 +170,4 @@ collectRefactorings(ResolvedCursorInfoPtr CursorInfo, bool ExcludeRename);
184170
} // namespace ide
185171
} // namespace swift
186172

187-
#endif // SWIFT_IDE_REFACTORING_H
173+
#endif // SWIFT_REFACTORING_REFACTORING_H

include/swift/Refactoring/RenameLoc.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 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+
#ifndef SWIFT_REFACTORING_RENAMELOC_H
14+
#define SWIFT_REFACTORING_RENAMELOC_H
15+
16+
#include "swift/Basic/LLVM.h"
17+
18+
namespace swift {
19+
namespace ide {
20+
21+
/// Describes how a `ResolvedLoc` is being used
22+
enum class RenameLocUsage {
23+
/// The definition of a function/subscript/variable/...
24+
Definition,
25+
26+
/// The symbol is being referenced.
27+
///
28+
/// This includes
29+
/// - References to variables
30+
/// - Unapplied references to functions (`myStruct.memberFunc`)
31+
/// - Calls to subscripts (`myArray[1]`, location is `[` here, length 1)
32+
Reference,
33+
34+
/// A function that is being called.
35+
Call,
36+
37+
/// Unknown name usage occurs if we don't have an entry in the index that
38+
/// tells us whether the location is a call, reference or a definition. The
39+
/// most common reasons why this happens is if the editor is adding syntactic
40+
/// results (eg. from comments or string literals).
41+
Unknown
42+
};
43+
44+
/// The input to `findSyntacticRenameRanges`.
45+
///
46+
/// Specifies the location of a base name for which `findSyntacticRenameRanges`
47+
/// should find the argument labels as well as some semantic information needed
48+
/// to resolve the rename ranges.
49+
struct RenameLoc {
50+
/// The line at which the base name starts (1-based).
51+
unsigned Line;
52+
53+
/// The column at which the base name (excluding trivia) starts (1-based).
54+
unsigned Column;
55+
56+
/// /// The offset at which the related name starts.
57+
unsigned Offset;
58+
59+
/// The length of the base name in the related identifier. For functions,
60+
/// this does not include the parameters/arguments.
61+
unsigned Length;
62+
63+
/// How the identifier is being used (call, reference, definition, unknown).
64+
///
65+
/// Used to decide whether a given occurance should be renamed and/or if its
66+
/// argument labels should be renamed.
67+
RenameLocUsage Usage;
68+
69+
/// The old decl name being renamed.
70+
///
71+
/// ### Examples
72+
/// - `foo(a:b:)` for a function with two parameters.
73+
/// - `foo` for a variable.
74+
/// - `foo(_:)` for a function with a single unnamed parameter
75+
/// - `foo()` for a function without parameters.
76+
StringRef OldName;
77+
78+
RenameLoc(unsigned Line, unsigned Column, RenameLocUsage Usage,
79+
StringRef OldName)
80+
: Line(Line), Column(Column), Usage(Usage), OldName(OldName) {}
81+
};
82+
83+
} // namespace ide
84+
} // namespace swift
85+
86+
#endif // SWIFT_REFACTORING_RENAMELOC_H

lib/Refactoring/ExtractFunction.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,8 @@ static SourceLoc getNewFuncInsertLoc(DeclContext *DC,
7777
return SourceLoc();
7878
}
7979

80-
static std::vector<NoteRegion> getNotableRegions(StringRef SourceText,
81-
unsigned NameOffset,
82-
StringRef Name,
83-
bool IsFunctionLike = false) {
80+
static std::vector<NoteRegion>
81+
getNotableRegions(StringRef SourceText, unsigned NameOffset, StringRef Name) {
8482
auto InputBuffer =
8583
llvm::MemoryBuffer::getMemBufferCopy(SourceText, "<extract>");
8684

@@ -106,8 +104,7 @@ static std::vector<NoteRegion> getNotableRegions(StringRef SourceText,
106104
assert(!Resolved.empty() && "Failed to resolve generated func name loc");
107105

108106
RenameLoc RenameConfig = {LineAndCol.first, LineAndCol.second,
109-
NameUsage::Definition, /*OldName=*/Name,
110-
IsFunctionLike};
107+
RenameLocUsage::Definition, /*OldName=*/Name};
111108
std::vector<RenameRangeDetail> Ranges =
112109
getSyntacticRenameRangeDetails(SM, Name, Resolved.back(), RenameConfig)
113110
.Ranges;
@@ -290,13 +287,11 @@ bool RefactoringActionExtractFunction::performChange() {
290287

291288
StringRef DeclStr(Buffer.begin() + FuncBegin, FuncEnd - FuncBegin);
292289
auto NotableFuncRegions =
293-
getNotableRegions(DeclStr, FuncNameOffset, ExtractedFuncName,
294-
/*IsFunctionLike=*/true);
290+
getNotableRegions(DeclStr, FuncNameOffset, ExtractedFuncName);
295291

296292
StringRef CallStr(Buffer.begin() + ReplaceBegin, ReplaceEnd - ReplaceBegin);
297293
auto NotableCallRegions =
298-
getNotableRegions(CallStr, CallNameOffset, ExtractedFuncName,
299-
/*IsFunctionLike=*/true);
294+
getNotableRegions(CallStr, CallNameOffset, ExtractedFuncName);
300295

301296
// Insert the new function's declaration.
302297
EditConsumer.accept(SM, InsertLoc, DeclStr, NotableFuncRegions);

lib/Refactoring/LocalRename.cpp

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ class RenameRangeCollector : public IndexDataConsumer {
219219
locations.push_back(std::move(*loc));
220220
} else {
221221
assert(existingLoc->OldName == loc->OldName &&
222-
existingLoc->IsFunctionLike == loc->IsFunctionLike &&
223222
"Asked to do a different rename for the same location?");
224223
}
225224
}
@@ -241,37 +240,19 @@ RenameRangeCollector::indexSymbolToRenameLoc(const index::IndexSymbol &symbol) {
241240
return llvm::None;
242241
}
243242

244-
NameUsage usage = NameUsage::Unknown;
243+
RenameLocUsage usage = RenameLocUsage::Unknown;
245244
if (symbol.roles & (unsigned)index::SymbolRole::Call) {
246-
usage = NameUsage::Call;
245+
usage = RenameLocUsage::Call;
247246
} else if (symbol.roles & (unsigned)index::SymbolRole::Definition) {
248-
usage = NameUsage::Definition;
247+
usage = RenameLocUsage::Definition;
249248
} else if (symbol.roles & (unsigned)index::SymbolRole::Reference) {
250-
usage = NameUsage::Reference;
249+
usage = RenameLocUsage::Reference;
251250
} else {
252251
llvm_unreachable("unexpected role");
253252
}
254253

255-
bool isFunctionLike = false;
256-
257-
switch (symbol.symInfo.Kind) {
258-
case index::SymbolKind::EnumConstant:
259-
case index::SymbolKind::Function:
260-
case index::SymbolKind::Constructor:
261-
case index::SymbolKind::ConversionFunction:
262-
case index::SymbolKind::InstanceMethod:
263-
case index::SymbolKind::ClassMethod:
264-
case index::SymbolKind::StaticMethod:
265-
isFunctionLike = true;
266-
break;
267-
case index::SymbolKind::Class:
268-
case index::SymbolKind::Enum:
269-
case index::SymbolKind::Struct:
270-
default:
271-
break;
272-
}
273254
StringRef oldName = stringStorage->copyString(symbol.name);
274-
return RenameLoc{symbol.line, symbol.column, usage, oldName, isFunctionLike};
255+
return RenameLoc{symbol.line, symbol.column, usage, oldName};
275256
}
276257

277258
/// Get the decl context that we need to walk when renaming \p VD.

lib/Refactoring/SyntacticRename.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ swift::ide::resolveRenameLocations(ArrayRef<RenameLoc> RenameLocs,
6363
return {};
6464
}
6565

66-
if (RenameLoc.Usage == NameUsage::Call && !RenameLoc.IsFunctionLike) {
66+
if (RenameLoc.Usage == RenameLocUsage::Call && !OldName.isFunction()) {
6767
Diags.diagnose(Location, diag::name_not_functionlike, NewName);
6868
return {};
6969
}

lib/Refactoring/SyntacticRenameRangeDetails.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -396,13 +396,13 @@ RegionType RenameRangeDetailCollector::addSyntacticRenameRanges(
396396
if (!resolved.range.isValid())
397397
return RegionType::Unmatched;
398398

399-
NameUsage usage = config.Usage;
399+
RenameLocUsage usage = config.Usage;
400400

401401
auto regionKind = getSyntacticRenameRegionType(resolved);
402402

403403
SpecialBaseName specialBaseName = specialBaseNameFor(Old);
404404

405-
if (usage == NameUsage::Unknown) {
405+
if (usage == RenameLocUsage::Unknown) {
406406
// Unknown name usage occurs if we don't have an entry in the index that
407407
// tells us whether the location is a call, reference or a definition. The
408408
// most common reasons why this happens is if the editor is adding syntactic
@@ -439,7 +439,8 @@ RegionType RenameRangeDetailCollector::addSyntacticRenameRanges(
439439
// be called as `myStruct()`, so even if the base fails to be renamed,
440440
// continue.
441441
// But the names do need to match for definitions and references.
442-
if (usage == NameUsage::Definition || usage == NameUsage::Reference) {
442+
if (usage == RenameLocUsage::Definition ||
443+
usage == RenameLocUsage::Reference) {
443444
return RegionType::Mismatch;
444445
}
445446
}
@@ -449,7 +450,7 @@ RegionType RenameRangeDetailCollector::addSyntacticRenameRanges(
449450
// Accesses to the subscript are modelled as references with `[` as the
450451
// base name, which does not match. Subscripts are never called in the
451452
// index.
452-
if (usage == NameUsage::Definition) {
453+
if (usage == RenameLocUsage::Definition) {
453454
if (renameBase(resolved.range, RefactoringRangeKind::KeywordBaseName)) {
454455
return RegionType::Mismatch;
455456
}
@@ -461,18 +462,18 @@ RegionType RenameRangeDetailCollector::addSyntacticRenameRanges(
461462
bool isCallSite = false;
462463
if (Old.isFunction()) {
463464
switch (usage) {
464-
case NameUsage::Call:
465+
case RenameLocUsage::Call:
465466
// All calls except for operators have argument labels that should be
466467
// renamed.
467468
handleLabels = !Lexer::isOperator(Old.base());
468469
isCallSite = true;
469470
break;
470-
case NameUsage::Definition:
471+
case RenameLocUsage::Definition:
471472
// All function definitions have argument labels that should be renamed.
472473
handleLabels = true;
473474
isCallSite = false;
474475
break;
475-
case NameUsage::Reference:
476+
case RenameLocUsage::Reference:
476477
if (resolved.labelType == LabelRangeType::CompoundName) {
477478
// If we have a compound name that specifies argument labels to
478479
// disambiguate functions with the same base name, we always need to
@@ -490,7 +491,7 @@ RegionType RenameRangeDetailCollector::addSyntacticRenameRanges(
490491
isCallSite = false;
491492
}
492493
break;
493-
case NameUsage::Unknown:
494+
case RenameLocUsage::Unknown:
494495
// If we don't know where the function is used, fall back to trying to
495496
// rename labels if there are some.
496497
handleLabels = resolved.labelType != LabelRangeType::None;
@@ -504,8 +505,8 @@ RegionType RenameRangeDetailCollector::addSyntacticRenameRanges(
504505
}
505506
if (renameLabels(resolved.labelRanges, resolved.firstTrailingLabel,
506507
resolved.labelType, isCallSite)) {
507-
return usage == NameUsage::Unknown ? RegionType::Unmatched
508-
: RegionType::Mismatch;
508+
return usage == RenameLocUsage::Unknown ? RegionType::Unmatched
509+
: RegionType::Mismatch;
509510
}
510511

511512
return regionKind;

tools/SourceKit/include/SourceKit/Core/LangSupport.h

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/AST/Type.h"
2020
#include "swift/IDE/CancellableResult.h"
2121
#include "swift/IDE/CodeCompletionResult.h"
22+
#include "swift/Refactoring/RenameLoc.h"
2223
#include "llvm/ADT/ArrayRef.h"
2324
#include "llvm/ADT/IntrusiveRefCntPtr.h"
2425
#include "llvm/ADT/Optional.h"
@@ -37,6 +38,7 @@ namespace llvm {
3738
namespace SourceKit {
3839
class GlobalConfig;
3940
using swift::ide::CancellableResult;
41+
using swift::ide::RenameLoc;
4042

4143
struct EntityInfo {
4244
UIdent Kind;
@@ -893,25 +895,6 @@ struct CategorizedRenameRanges {
893895
std::vector<RenameRangeDetail> Ranges;
894896
};
895897

896-
enum class RenameType {
897-
Unknown,
898-
Definition,
899-
Reference,
900-
Call
901-
};
902-
903-
struct RenameLocation {
904-
unsigned Line;
905-
unsigned Column;
906-
RenameType Type;
907-
};
908-
909-
struct RenameLocations {
910-
StringRef OldName;
911-
const bool IsFunctionLike;
912-
std::vector<RenameLocation> LineColumnLocs;
913-
};
914-
915898
struct IndexStoreOptions {
916899
std::string IndexStorePath;
917900
std::string IndexUnitOutputPath;
@@ -1192,7 +1175,7 @@ class LangSupport {
11921175

11931176
virtual CancellableResult<std::vector<CategorizedRenameRanges>>
11941177
findRenameRanges(llvm::MemoryBuffer *InputBuf,
1195-
ArrayRef<RenameLocations> RenameLocations,
1178+
ArrayRef<RenameLoc> RenameLocations,
11961179
ArrayRef<const char *> Args) = 0;
11971180
virtual void
11981181
findLocalRenameRanges(StringRef Filename, unsigned Line, unsigned Column,

0 commit comments

Comments
 (0)