Skip to content

Commit 78990ff

Browse files
committed
[IDE] Pass ParameterizedDeclAttributeKind to parseSingleAttrOption rather than completion callback
1 parent a340dca commit 78990ff

File tree

1 file changed

+29
-69
lines changed

1 file changed

+29
-69
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 29 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,13 +2469,14 @@ Parser::parseMacroRoleAttribute(
24692469
/// \c Identifier() ; if false, diagnose a missing argument list as an error.
24702470
/// \param nonIdentifierDiagnostic The diagnostic to emit if something other than a
24712471
/// \c tok::identifier is used as an argument.
2472+
/// \param PDK Specific kind of the parameterized attribute being passed. Used when invoking code completion callbacks.
24722473
///
24732474
/// \returns \c None if an error was diagnosed; \c Identifier() if the argument list was permissibly
24742475
/// omitted; the identifier written by the user otherwise.
24752476
static std::optional<Identifier> parseSingleAttrOptionImpl(
2476-
Parser &P, SourceLoc Loc, SourceRange &AttrRange, StringRef AttrName, DeclAttrKind DK,
2477-
ParserStatus &Status, bool allowOmitted, DiagRef nonIdentifierDiagnostic,
2478-
llvm::function_ref<void()> codeCompletionCallback = {}) {
2477+
Parser &P, SourceLoc Loc, SourceRange &AttrRange, StringRef AttrName,
2478+
DeclAttrKind DK, bool allowOmitted, DiagRef nonIdentifierDiagnostic,
2479+
std::optional<ParameterizedDeclAttributeKind> PDK = std::nullopt) {
24792480
SWIFT_DEFER {
24802481
AttrRange = SourceRange(Loc, P.PreviousLoc);
24812482
};
@@ -2484,28 +2485,25 @@ static std::optional<Identifier> parseSingleAttrOptionImpl(
24842485
if (!P.Tok.is(tok::l_paren)) {
24852486
if (allowOmitted)
24862487
return Identifier();
2487-
2488-
Status.setIsParseError();
2488+
24892489
P.diagnose(Loc, diag::attr_expected_lparen, AttrName, isDeclModifier);
24902490
return std::nullopt;
24912491
}
24922492

24932493
P.consumeAttributeLParen();
24942494

2495-
if (P.Tok.is(tok::code_complete)) {
2496-
Status.setHasCodeCompletion();
2497-
codeCompletionCallback();
2495+
if (P.Tok.is(tok::code_complete) && P.CodeCompletionCallbacks && PDK) {
2496+
P.CodeCompletionCallbacks->completeDeclAttrParam(*PDK, 0, /*HasLabel=*/false);
2497+
P.consumeToken(tok::code_complete);
24982498
}
24992499

25002500
StringRef parsedName = P.Tok.getText();
25012501
if (!P.consumeIf(tok::identifier)) {
2502-
Status.setIsParseError();
25032502
P.diagnose(Loc, nonIdentifierDiagnostic);
25042503
return std::nullopt;
25052504
}
25062505

25072506
if (!P.consumeIf(tok::r_paren)) {
2508-
Status.setIsParseError();
25092507
P.diagnose(Loc, diag::attr_expected_rparen, AttrName, isDeclModifier);
25102508
return std::nullopt;
25112509
}
@@ -2599,11 +2597,11 @@ ParserResult<LifetimeAttr> Parser::parseLifetimeAttribute(SourceLoc atLoc,
25992597
/// \returns \c None if an error was diagnosed; \c Identifier() if the argument list was permissibly
26002598
/// omitted; the identifier written by the user otherwise.
26012599
static std::optional<Identifier>
2602-
parseSingleAttrOptionIdentifier(Parser &P, SourceLoc Loc, ParserStatus &Status,
2603-
SourceRange &AttrRange, StringRef AttrName,
2604-
DeclAttrKind DK, bool allowOmitted = false) {
2600+
parseSingleAttrOptionIdentifier(Parser &P, SourceLoc Loc, SourceRange &AttrRange,
2601+
StringRef AttrName, DeclAttrKind DK,
2602+
bool allowOmitted = false) {
26052603
return parseSingleAttrOptionImpl(
2606-
P, Loc, AttrRange, AttrName, DK, Status, allowOmitted,
2604+
P, Loc, AttrRange, AttrName, DK, allowOmitted,
26072605
{diag::attr_expected_option_identifier, {AttrName}});
26082606
}
26092607

@@ -2618,23 +2616,22 @@ parseSingleAttrOptionIdentifier(Parser &P, SourceLoc Loc, ParserStatus &Status,
26182616
/// \param options The set of permitted keywords and their corresponding values.
26192617
/// \param valueIfOmitted If present, treat a missing argument list as permitted and return
26202618
/// the provided value; if absent, diagnose a missing argument list as an error.
2619+
/// \param PDK Specific kind of the parameterized attribute being passed. Used when invoking code completion callbacks.
26212620
///
26222621
/// \returns \c None if an error was diagnosed; the value corresponding to the identifier written by the
26232622
/// user otherwise.
26242623
template <typename R>
26252624
static std::optional<R>
26262625
parseSingleAttrOption(Parser &P, SourceLoc Loc, SourceRange &AttrRange,
26272626
StringRef AttrName, DeclAttrKind DK,
2628-
ParserStatus& Status,
26292627
ArrayRef<std::pair<Identifier, R>> options,
26302628
std::optional<R> valueIfOmitted = std::nullopt,
2631-
llvm::function_ref<void()> codeCompletionCallback = {}) {
2629+
std::optional<ParameterizedDeclAttributeKind> PDK = std::nullopt) {
26322630
auto parsedIdentifier = parseSingleAttrOptionImpl(
2633-
P, Loc, AttrRange, AttrName, DK, Status,
2631+
P, Loc, AttrRange, AttrName, DK,
26342632
/*allowOmitted=*/valueIfOmitted.has_value(),
26352633
{diag::attr_expected_option_such_as,
2636-
{AttrName, options.front().first.str()}},
2637-
codeCompletionCallback);
2634+
{AttrName, options.front().first.str()}}, PDK);
26382635

26392636
if (!parsedIdentifier)
26402637
return std::nullopt;
@@ -2768,9 +2765,8 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
27682765
}
27692766

27702767
case DeclAttrKind::Inline: {
2771-
ParserStatus optionStatus;
27722768
auto kind = parseSingleAttrOption<InlineKind>(
2773-
*this, Loc, AttrRange, AttrName, DK, optionStatus, {
2769+
*this, Loc, AttrRange, AttrName, DK, {
27742770
{ Context.Id_never, InlineKind::Never },
27752771
{ Context.Id__always, InlineKind::Always }
27762772
});
@@ -2784,9 +2780,8 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
27842780
}
27852781

27862782
case DeclAttrKind::Optimize: {
2787-
ParserStatus optionStatus;
27882783
auto optMode = parseSingleAttrOption<OptimizationMode>(
2789-
*this, Loc, AttrRange, AttrName, DK, optionStatus, {
2784+
*this, Loc, AttrRange, AttrName, DK, {
27902785
{ Context.Id_speed, OptimizationMode::ForSpeed },
27912786
{ Context.Id_size, OptimizationMode::ForSize },
27922787
{ Context.Id_none, OptimizationMode::NoOptimization }
@@ -2801,9 +2796,8 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
28012796
}
28022797

28032798
case DeclAttrKind::Exclusivity: {
2804-
ParserStatus optionStatus;
28052799
auto mode = parseSingleAttrOption<ExclusivityAttr::Mode>(
2806-
*this, Loc, AttrRange, AttrName, DK, optionStatus, {
2800+
*this, Loc, AttrRange, AttrName, DK, {
28072801
{ Context.Id_checked, ExclusivityAttr::Mode::Checked },
28082802
{ Context.Id_unchecked, ExclusivityAttr::Mode::Unchecked }
28092803
});
@@ -2823,19 +2817,12 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
28232817

28242818
if (Kind == ReferenceOwnership::Unowned) {
28252819
// Parse an optional specifier after unowned.
2826-
ParserStatus optionStatus;
28272820
Kind = parseSingleAttrOption<ReferenceOwnership>(
2828-
*this, Loc, AttrRange, AttrName, DK, optionStatus, {
2821+
*this, Loc, AttrRange, AttrName, DK, {
28292822
{ Context.Id_unsafe, ReferenceOwnership::Unmanaged },
28302823
{ Context.Id_safe, ReferenceOwnership::Unowned }
28312824
}, ReferenceOwnership::Unowned,
2832-
[&] () {
2833-
if (CodeCompletionCallbacks) {
2834-
CodeCompletionCallbacks->completeDeclAttrParam(
2835-
ParameterizedDeclAttributeKind::Unowned, 0, /*HasLabel=*/false);
2836-
consumeToken(tok::code_complete);
2837-
}
2838-
})
2825+
ParameterizedDeclAttributeKind::Unowned)
28392826
// Recover from errors by going back to Unowned.
28402827
.value_or(ReferenceOwnership::Unowned);
28412828
}
@@ -2851,9 +2838,8 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
28512838
}
28522839

28532840
case DeclAttrKind::NonSendable: {
2854-
ParserStatus optionStatus;
28552841
auto kind = parseSingleAttrOption<NonSendableKind>(
2856-
*this, Loc, AttrRange, AttrName, DK, optionStatus, {
2842+
*this, Loc, AttrRange, AttrName, DK, {
28572843
{ Context.Id_assumed, NonSendableKind::Assumed }
28582844
}, NonSendableKind::Specific);
28592845
if (!kind)
@@ -3190,8 +3176,7 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
31903176

31913177
case DeclAttrKind::SwiftNativeObjCRuntimeBase: {
31923178
SourceRange range;
3193-
ParserStatus optionStatus;
3194-
auto name = parseSingleAttrOptionIdentifier(*this, Loc, optionStatus, range,
3179+
auto name = parseSingleAttrOptionIdentifier(*this, Loc, range,
31953180
AttrName, DK);
31963181
if (!name)
31973182
return makeParserSuccess();
@@ -3484,8 +3469,7 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
34843469
}
34853470
case DeclAttrKind::ObjCImplementation: {
34863471
SourceRange range;
3487-
ParserStatus optionStatus;
3488-
auto name = parseSingleAttrOptionIdentifier(*this, Loc, optionStatus, range, AttrName, DK,
3472+
auto name = parseSingleAttrOptionIdentifier(*this, Loc, range, AttrName, DK,
34893473
/*allowOmitted=*/true);
34903474
if (!name)
34913475
return makeParserSuccess();
@@ -3497,9 +3481,8 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
34973481
}
34983482
case DeclAttrKind::ObjCRuntimeName: {
34993483
SourceRange range;
3500-
ParserStatus optionStatus;
35013484
auto name =
3502-
parseSingleAttrOptionIdentifier(*this, Loc, optionStatus, range, AttrName, DK);
3485+
parseSingleAttrOptionIdentifier(*this, Loc, range, AttrName, DK);
35033486
if (!name)
35043487
return makeParserSuccess();
35053488

@@ -3656,9 +3639,8 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
36563639

36573640
case DeclAttrKind::ProjectedValueProperty: {
36583641
SourceRange range;
3659-
ParserStatus optionStatus;
36603642
auto name =
3661-
parseSingleAttrOptionIdentifier(*this, Loc, optionStatus, range, AttrName, DK);
3643+
parseSingleAttrOptionIdentifier(*this, Loc, range, AttrName, DK);
36623644
if (!name)
36633645
return makeParserSuccess();
36643646

@@ -3758,17 +3740,10 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
37583740
case DeclAttrKind::Nonisolated: {
37593741
std::optional<bool> isUnsafe(false);
37603742
if (EnableParameterizedNonisolated) {
3761-
ParserStatus optionStatus;
37623743
isUnsafe =
37633744
parseSingleAttrOption<bool>(*this, Loc, AttrRange, AttrName, DK,
3764-
optionStatus, {{Context.Id_unsafe, true}},
3765-
*isUnsafe, [&] () {
3766-
if (CodeCompletionCallbacks) {
3767-
CodeCompletionCallbacks->completeDeclAttrParam(
3768-
ParameterizedDeclAttributeKind::Nonisolated, 0, /*HasLabel=*/false);
3769-
consumeToken(tok::code_complete);
3770-
}
3771-
});
3745+
{{Context.Id_unsafe, true}}, *isUnsafe,
3746+
ParameterizedDeclAttributeKind::Nonisolated);
37723747
if (!isUnsafe) {
37733748
return makeParserSuccess();
37743749
}
@@ -3967,9 +3942,8 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
39673942
}
39683943

39693944
case DeclAttrKind::Execution: {
3970-
ParserStatus optionStatus;
39713945
auto behavior = parseSingleAttrOption<ExecutionKind>(
3972-
*this, Loc, AttrRange, AttrName, DK, optionStatus,
3946+
*this, Loc, AttrRange, AttrName, DK,
39733947
{{Context.Id_concurrent, ExecutionKind::Concurrent},
39743948
{Context.Id_caller, ExecutionKind::Caller}});
39753949
if (!behavior)
@@ -5674,7 +5648,6 @@ consumeIfParenthesizedModifier(Parser &P, StringRef name,
56745648
P.consumeToken(tok::identifier);
56755649
P.consumeToken(tok::l_paren);
56765650

5677-
const Token &Tok2 = P.peekToken();
56785651
if (P.consumeIf(tok::code_complete)) {
56795652
// If a code complete token is present, recover from missing/incorrect argument and missing '('
56805653
P.consumeIf(tok::identifier);
@@ -5683,19 +5656,6 @@ consumeIfParenthesizedModifier(Parser &P, StringRef name,
56835656
return true;
56845657
}
56855658

5686-
if (P.Tok.is(tok::identifier) && !P.Tok.isContextualDeclKeyword() && Tok2.is(tok::code_complete)) {
5687-
P.consumeToken(tok::identifier);
5688-
P.consumeToken(tok::code_complete);
5689-
5690-
// If a code complete is present with a non-keyword token before it, recover from missing/incorrect argument and missing '('
5691-
if (!P.Tok.isContextualDeclKeyword()) {
5692-
P.consumeIf(tok::identifier);
5693-
}
5694-
P.consumeIf(tok::r_paren);
5695-
backtrack.cancelBacktrack();
5696-
return true;
5697-
}
5698-
56995659
const bool argumentIsAllowed =
57005660
std::find(allowedArguments.begin(), allowedArguments.end(),
57015661
P.Tok.getText()) != allowedArguments.end();

0 commit comments

Comments
 (0)