Introduce then statements

These allow multi-statement `if`/`switch` expression
branches that can produce a value at the end by
saying `then <expr>`. This is gated behind
`-enable-experimental-feature ThenStatements`
pending evolution discussion.
This commit is contained in:
Hamish Knight
2023-09-01 14:32:14 +01:00
parent 99a36b3f00
commit 6ee44f09b4
47 changed files with 1063 additions and 196 deletions

View File

@@ -293,6 +293,7 @@ public:
}
void completeReturnStmt(CodeCompletionExpr *E) override;
void completeThenStmt(CodeCompletionExpr *E) override;
void completeYieldStmt(CodeCompletionExpr *E,
llvm::Optional<unsigned> yieldIndex) override;
void completeAfterPoundExpr(CodeCompletionExpr *E,
@@ -591,6 +592,12 @@ void CodeCompletionCallbacksImpl::completeReturnStmt(CodeCompletionExpr *E) {
Kind = CompletionKind::ReturnStmtExpr;
}
void CodeCompletionCallbacksImpl::completeThenStmt(CodeCompletionExpr *E) {
CurDeclContext = P.CurDeclContext;
CodeCompleteTokenExpr = E;
Kind = CompletionKind::ThenStmtExpr;
}
void CodeCompletionCallbacksImpl::completeYieldStmt(
CodeCompletionExpr *E, llvm::Optional<unsigned> index) {
CurDeclContext = P.CurDeclContext;
@@ -1052,6 +1059,7 @@ void CodeCompletionCallbacksImpl::addKeywords(CodeCompletionResultSink &Sink,
case CompletionKind::ReturnStmtExpr:
addKeywordsAfterReturn(Sink, CurDeclContext);
LLVM_FALLTHROUGH;
case CompletionKind::ThenStmtExpr:
case CompletionKind::YieldStmtExpr:
case CompletionKind::ForEachSequence:
addSuperKeyword(Sink, CurDeclContext);
@@ -1561,11 +1569,19 @@ bool CodeCompletionCallbacksImpl::trySolverCompletion(bool MaybeFuncBody) {
case CompletionKind::PostfixExprBeginning:
case CompletionKind::StmtOrExpr:
case CompletionKind::ReturnStmtExpr:
case CompletionKind::YieldStmtExpr: {
case CompletionKind::YieldStmtExpr:
case CompletionKind::ThenStmtExpr: {
assert(CurDeclContext);
bool AddUnresolvedMemberCompletions =
(Kind == CompletionKind::CaseStmtBeginning);
bool AddUnresolvedMemberCompletions = false;
switch (Kind) {
case CompletionKind::CaseStmtBeginning:
case CompletionKind::ThenStmtExpr:
AddUnresolvedMemberCompletions = true;
break;
default:
break;
}
ExprTypeCheckCompletionCallback Lookup(
CodeCompleteTokenExpr, CurDeclContext, AddUnresolvedMemberCompletions);
if (CodeCompleteTokenExpr) {
@@ -1735,6 +1751,7 @@ void CodeCompletionCallbacksImpl::doneParsing(SourceFile *SrcFile) {
case CompletionKind::PostfixExpr:
case CompletionKind::ReturnStmtExpr:
case CompletionKind::YieldStmtExpr:
case CompletionKind::ThenStmtExpr:
llvm_unreachable("should be already handled");
return;