Skip to content

Commit 623e6f3

Browse files
committed
Sema: Refactor TypeChecker::typeCheckStmtConditionElement().
Use a switch statement to cover all the possible kinds of condition elements and implement diagnostics in separate functions for each kind of condition element. NFC.
1 parent 7a57bd8 commit 623e6f3

File tree

1 file changed

+45
-34
lines changed

1 file changed

+45
-34
lines changed

lib/Sema/TypeCheckStmt.cpp

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -784,46 +784,39 @@ ConcreteDeclRef TypeChecker::getReferencedDeclForHasSymbolCondition(Expr *E) {
784784
return ConcreteDeclRef();
785785
}
786786

787-
bool TypeChecker::typeCheckStmtConditionElement(StmtConditionElement &elt,
788-
bool &isFalsable,
789-
DeclContext *dc) {
790-
auto &Context = dc->getASTContext();
791-
792-
// Typecheck a #available or #unavailable condition.
793-
if (elt.getKind() == StmtConditionElement::CK_Availability) {
794-
isFalsable = true;
787+
static bool typeCheckHasSymbolStmtConditionElement(StmtConditionElement &elt,
788+
DeclContext *dc) {
789+
auto Info = elt.getHasSymbolInfo();
790+
auto E = Info->getSymbolExpr();
791+
if (!E)
795792
return false;
796-
}
797793

798-
// Typecheck a #_hasSymbol condition.
799-
if (elt.getKind() == StmtConditionElement::CK_HasSymbol) {
800-
isFalsable = true;
794+
auto exprTy = TypeChecker::typeCheckExpression(E, dc);
795+
Info->setSymbolExpr(E);
801796

802-
auto Info = elt.getHasSymbolInfo();
803-
auto E = Info->getSymbolExpr();
804-
if (!E)
805-
return false;
806-
807-
auto exprTy = TypeChecker::typeCheckExpression(E, dc);
808-
Info->setSymbolExpr(E);
797+
if (!exprTy) {
798+
Info->setInvalid();
799+
return true;
800+
}
809801

810-
if (!exprTy) {
811-
Info->setInvalid();
812-
return true;
813-
}
802+
Info->setReferencedDecl(
803+
TypeChecker::getReferencedDeclForHasSymbolCondition(E));
804+
return false;
805+
}
814806

815-
Info->setReferencedDecl(getReferencedDeclForHasSymbolCondition(E));
816-
return false;
817-
}
807+
static bool typeCheckBooleanStmtConditionElement(StmtConditionElement &elt,
808+
DeclContext *dc) {
809+
Expr *E = elt.getBoolean();
810+
assert(!E->getType() && "the bool condition is already type checked");
811+
bool hadError = TypeChecker::typeCheckCondition(E, dc);
812+
elt.setBoolean(E);
813+
return hadError;
814+
}
818815

819-
if (auto E = elt.getBooleanOrNull()) {
820-
assert(!E->getType() && "the bool condition is already type checked");
821-
bool hadError = TypeChecker::typeCheckCondition(E, dc);
822-
elt.setBoolean(E);
823-
isFalsable = true;
824-
return hadError;
825-
}
826-
assert(elt.getKind() != StmtConditionElement::CK_Boolean);
816+
static bool
817+
typeCheckPatternBindingStmtConditionElement(StmtConditionElement &elt,
818+
bool &isFalsable, DeclContext *dc) {
819+
auto &Context = dc->getASTContext();
827820

828821
// This is cleanup goop run on the various paths where type checking of the
829822
// pattern binding fails.
@@ -871,6 +864,24 @@ bool TypeChecker::typeCheckStmtConditionElement(StmtConditionElement &elt,
871864
return hadError;
872865
}
873866

867+
bool TypeChecker::typeCheckStmtConditionElement(StmtConditionElement &elt,
868+
bool &isFalsable,
869+
DeclContext *dc) {
870+
switch (elt.getKind()) {
871+
case StmtConditionElement::CK_Availability:
872+
isFalsable = true;
873+
return false;
874+
case StmtConditionElement::CK_HasSymbol:
875+
isFalsable = true;
876+
return typeCheckHasSymbolStmtConditionElement(elt, dc);
877+
case StmtConditionElement::CK_Boolean:
878+
isFalsable = true;
879+
return typeCheckBooleanStmtConditionElement(elt, dc);
880+
case StmtConditionElement::CK_PatternBinding:
881+
return typeCheckPatternBindingStmtConditionElement(elt, isFalsable, dc);
882+
}
883+
}
884+
874885
/// Type check the given 'if', 'while', or 'guard' statement condition.
875886
///
876887
/// \param stmt The conditional statement to type-check, which will be modified

0 commit comments

Comments
 (0)