Skip to content

Commit b8ec778

Browse files
committed
[Refactoring] Replace usage of constant strings with tok:: wherever possible
The code moved from `LegacyAlternativeBodyCreator` was using constant strings a lot. Use `tok::` instead to match the style of `AsyncConverter`.
1 parent 8903190 commit b8ec778

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

lib/IDE/Refactoring.cpp

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4857,31 +4857,30 @@ class AsyncConverter : private SourceEntityWalker {
48574857
FuncDecl *FD = cast<FuncDecl>(StartNode.get<Decl *>());
48584858
Identifier CompletionHandlerName = TopHandler.Handler->getParameterName();
48594859

4860-
OS << "{\n"; // start function body
4861-
OS << "async {\n";
4860+
OS << tok::l_brace << "\n"; // start function body
4861+
OS << "async " << tok::l_brace << "\n";
48624862
if (TopHandler.HasError) {
4863-
OS << "do {\n";
4863+
addDo();
48644864
if (!TopHandler.willAsyncReturnVoid()) {
4865-
OS << "let result";
4865+
OS << tok::kw_let << " result";
48664866
addResultTypeAnnotationIfNecessary(FD, TopHandler);
4867-
OS << " = ";
4867+
OS << " " << tok::equal << " ";
48684868
}
4869-
OS << "try await ";
4869+
OS << tok::kw_try << " await ";
48704870
addCallToAsyncMethod(FD, TopHandler);
48714871
OS << "\n";
48724872
addCallToCompletionHandler(/*HasResult=*/true, CompletionHandlerName, FD,
48734873
TopHandler);
48744874
OS << "\n"
4875-
<< "} catch {\n";
4875+
<< tok::r_brace << " " << tok::kw_catch << " " << tok::l_brace << "\n";
48764876
addCallToCompletionHandler(/*HasResult=*/false, CompletionHandlerName, FD,
48774877
TopHandler);
4878-
OS << "\n"
4879-
<< "}\n"; // end catch
4878+
OS << "\n" << tok::r_brace << "\n"; // end catch
48804879
} else {
48814880
if (!TopHandler.willAsyncReturnVoid()) {
4882-
OS << "let result";
4881+
OS << tok::kw_let << " result";
48834882
addResultTypeAnnotationIfNecessary(FD, TopHandler);
4884-
OS << " = ";
4883+
OS << " " << tok::equal << " ";
48854884
}
48864885
OS << "await ";
48874886
addCallToAsyncMethod(FD, TopHandler);
@@ -4890,8 +4889,8 @@ class AsyncConverter : private SourceEntityWalker {
48904889
TopHandler);
48914890
OS << "\n";
48924891
}
4893-
OS << "}\n"; // end 'async'
4894-
OS << "}\n"; // end function body
4892+
OS << tok::r_brace << "\n"; // end 'async'
4893+
OS << tok::r_brace << "\n"; // end function body
48954894
return true;
48964895
}
48974896

@@ -5510,24 +5509,24 @@ class AsyncConverter : private SourceEntityWalker {
55105509
/// 'await' keyword.
55115510
void addCallToAsyncMethod(const FuncDecl *FD,
55125511
const AsyncHandlerDesc &HandlerDesc) {
5513-
OS << FD->getBaseName() << "(";
5512+
OS << FD->getBaseName() << tok::l_paren;
55145513
bool FirstParam = true;
55155514
for (auto Param : *FD->getParameters()) {
55165515
if (Param == HandlerDesc.Handler) {
55175516
/// We don't need to pass the completion handler to the async method.
55185517
continue;
55195518
}
55205519
if (!FirstParam) {
5521-
OS << ", ";
5520+
OS << tok::comma << " ";
55225521
} else {
55235522
FirstParam = false;
55245523
}
55255524
if (!Param->getArgumentName().empty()) {
5526-
OS << Param->getArgumentName() << ": ";
5525+
OS << Param->getArgumentName() << tok::colon << " ";
55275526
}
55285527
OS << Param->getParameterName();
55295528
}
5530-
OS << ")";
5529+
OS << tok::r_paren;
55315530
}
55325531

55335532
/// If the error type of \p HandlerDesc is more specialized than \c Error,
@@ -5537,7 +5536,7 @@ class AsyncConverter : private SourceEntityWalker {
55375536
const ASTContext &Ctx) {
55385537
auto ErrorType = *HandlerDesc.getErrorType();
55395538
if (ErrorType->getCanonicalType() != Ctx.getExceptionType()) {
5540-
OS << " as! ";
5539+
OS << " " << tok::kw_as << tok::exclaim_postfix << " ";
55415540
ErrorType->lookThroughSingleOptionalType()->print(OS);
55425541
}
55435542
}
@@ -5559,18 +5558,18 @@ class AsyncConverter : private SourceEntityWalker {
55595558
OS << "error";
55605559
addCastToCustomErrorTypeIfNecessary(HandlerDesc, FD->getASTContext());
55615560
} else {
5562-
OS << "nil";
5561+
OS << tok::kw_nil;
55635562
}
55645563
} else {
55655564
if (!HasResult) {
5566-
OS << "nil";
5565+
OS << tok::kw_nil;
55675566
} else if (HandlerDesc
55685567
.getSuccessParamAsyncReturnType(
55695568
HandlerDesc.params()[Index].getPlainType())
55705569
->isVoid()) {
55715570
// Void return types are not returned by the async function, synthesize
55725571
// a Void instance.
5573-
OS << "()";
5572+
OS << tok::l_paren << tok::r_paren;
55745573
} else if (HandlerDesc.getSuccessParams().size() > 1) {
55755574
// If the async method returns a tuple, we need to pass its elements to
55765575
// the completion handler separately. For example:
@@ -5585,7 +5584,7 @@ class AsyncConverter : private SourceEntityWalker {
55855584
// completion(result.0, result.1)
55865585
// }
55875586
// }
5588-
OS << "result." << Index;
5587+
OS << "result" << tok::period << Index;
55895588
} else {
55905589
OS << "result";
55915590
}
@@ -5601,7 +5600,7 @@ class AsyncConverter : private SourceEntityWalker {
56015600
void addCallToCompletionHandler(bool HasResult, Identifier HandlerName,
56025601
const FuncDecl *FD,
56035602
const AsyncHandlerDesc &HandlerDesc) {
5604-
OS << HandlerName << "(";
5603+
OS << HandlerName << tok::l_paren;
56055604

56065605
// Construct arguments to pass to the completion handler
56075606
switch (HandlerDesc.Type) {
@@ -5611,24 +5610,25 @@ class AsyncConverter : private SourceEntityWalker {
56115610
case HandlerType::PARAMS: {
56125611
for (size_t I = 0; I < HandlerDesc.params().size(); ++I) {
56135612
if (I > 0) {
5614-
OS << ", ";
5613+
OS << tok::comma << " ";
56155614
}
56165615
addCompletionHandlerArgument(I, HasResult, FD, HandlerDesc);
56175616
}
56185617
break;
56195618
}
56205619
case HandlerType::RESULT: {
56215620
if (HasResult) {
5622-
OS << ".success(result)";
5621+
OS << tok::period_prefix << "success" << tok::l_paren << "result"
5622+
<< tok::r_paren;
56235623
} else {
5624-
OS << ".failure(error";
5624+
OS << tok::period_prefix << "failure" << tok::l_paren << "error";
56255625
addCastToCustomErrorTypeIfNecessary(HandlerDesc, FD->getASTContext());
5626-
OS << ")";
5626+
OS << tok::r_paren;
56275627
}
56285628
break;
56295629
}
56305630
}
5631-
OS << ")"; // Close the call to the completion handler
5631+
OS << tok::r_paren; // Close the call to the completion handler
56325632
}
56335633

56345634
/// Adds the result type of a refactored async function that previously
@@ -5637,14 +5637,15 @@ class AsyncConverter : private SourceEntityWalker {
56375637
SmallVector<Type, 2> Scratch;
56385638
auto ReturnTypes = HandlerDesc.getAsyncReturnTypes(Scratch);
56395639
if (ReturnTypes.size() > 1) {
5640-
OS << "(";
5640+
OS << tok::l_paren;
56415641
}
56425642

56435643
llvm::interleave(
5644-
ReturnTypes, [&](Type Ty) { Ty->print(OS); }, [&]() { OS << ", "; });
5644+
ReturnTypes, [&](Type Ty) { Ty->print(OS); },
5645+
[&]() { OS << tok::comma << " "; });
56455646

56465647
if (ReturnTypes.size() > 1) {
5647-
OS << ")";
5648+
OS << tok::r_paren;
56485649
}
56495650
}
56505651

@@ -5669,7 +5670,7 @@ class AsyncConverter : private SourceEntityWalker {
56695670
void addResultTypeAnnotationIfNecessary(const FuncDecl *FD,
56705671
const AsyncHandlerDesc &HandlerDesc) {
56715672
if (FD->isGeneric()) {
5672-
OS << ": ";
5673+
OS << tok::colon << " ";
56735674
addAsyncFuncReturnType(HandlerDesc);
56745675
}
56755676
}

0 commit comments

Comments
 (0)