Skip to content

Commit 7733308

Browse files
author
Harlan Haskins
committed
Convert TypeChecker::checkDefaultArguments to static function
This doesn’t need to be a method, since it’s now only going to be used in the Decl checker
1 parent 02b5e56 commit 7733308

File tree

3 files changed

+83
-82
lines changed

3 files changed

+83
-82
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,82 @@ static void validatePrecedenceGroup(PrecedenceGroupDecl *PGD) {
17651765
}
17661766
}
17671767

1768+
static Optional<unsigned>
1769+
getParamIndex(const ParameterList *paramList, const ParamDecl *decl) {
1770+
ArrayRef<ParamDecl *> params = paramList->getArray();
1771+
for (unsigned i = 0; i < params.size(); ++i) {
1772+
if (params[i] == decl) return i;
1773+
}
1774+
return None;
1775+
}
1776+
1777+
static void
1778+
checkInheritedDefaultValueRestrictions(TypeChecker &TC, ParamDecl *PD) {
1779+
if (PD->getDefaultArgumentKind() != DefaultArgumentKind::Inherited)
1780+
return;
1781+
1782+
auto *DC = PD->getInnermostDeclContext();
1783+
const SourceFile *SF = DC->getParentSourceFile();
1784+
assert((SF && SF->Kind == SourceFileKind::Interface || PD->isImplicit()) &&
1785+
"explicit inherited default argument outside of a module interface?");
1786+
1787+
// The containing decl should be a designated initializer.
1788+
auto ctor = dyn_cast<ConstructorDecl>(DC);
1789+
if (!ctor || ctor->isConvenienceInit()) {
1790+
TC.diagnose(
1791+
PD, diag::inherited_default_value_not_in_designated_constructor);
1792+
return;
1793+
}
1794+
1795+
// The decl it overrides should also be a designated initializer.
1796+
auto overridden = ctor->getOverriddenDecl();
1797+
if (!overridden || overridden->isConvenienceInit()) {
1798+
TC.diagnose(
1799+
PD, diag::inherited_default_value_used_in_non_overriding_constructor);
1800+
if (overridden)
1801+
TC.diagnose(overridden, diag::overridden_here);
1802+
return;
1803+
}
1804+
1805+
// The corresponding parameter should have a default value.
1806+
Optional<unsigned> idx = getParamIndex(ctor->getParameters(), PD);
1807+
assert(idx && "containing decl does not contain param?");
1808+
ParamDecl *equivalentParam = overridden->getParameters()->get(*idx);
1809+
if (equivalentParam->getDefaultArgumentKind() == DefaultArgumentKind::None) {
1810+
TC.diagnose(PD, diag::corresponding_param_not_defaulted);
1811+
TC.diagnose(equivalentParam, diag::inherited_default_param_here);
1812+
}
1813+
}
1814+
1815+
/// Check the default arguments that occur within this pattern.
1816+
static void checkDefaultArguments(TypeChecker &tc, ParameterList *params,
1817+
ValueDecl *VD) {
1818+
for (auto *param : *params) {
1819+
checkInheritedDefaultValueRestrictions(tc, param);
1820+
if (!param->getDefaultValue() ||
1821+
!param->hasInterfaceType() ||
1822+
param->getInterfaceType()->hasError())
1823+
continue;
1824+
1825+
Expr *e = param->getDefaultValue();
1826+
auto *initContext = param->getDefaultArgumentInitContext();
1827+
1828+
auto resultTy =
1829+
tc.typeCheckParameterDefault(e, initContext, param->getType(),
1830+
/*isAutoClosure=*/param->isAutoClosure());
1831+
1832+
if (resultTy) {
1833+
param->setDefaultValue(e);
1834+
}
1835+
1836+
tc.checkInitializerErrorHandling(initContext, e);
1837+
1838+
// Walk the checked initializer and contextualize any closures
1839+
// we saw there.
1840+
(void)tc.contextualizeInitializer(initContext, e);
1841+
}
1842+
}
1843+
17681844
PrecedenceGroupDecl *TypeChecker::lookupPrecedenceGroup(DeclContext *dc,
17691845
Identifier name,
17701846
SourceLoc nameLoc) {
@@ -2384,7 +2460,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
23842460
(void) SD->getImplInfo();
23852461

23862462
TC.checkParameterAttributes(SD->getIndices());
2387-
TC.checkDefaultArguments(SD->getIndices(), SD);
2463+
checkDefaultArguments(TC, SD->getIndices(), SD);
23882464

23892465
if (SD->getDeclContext()->getSelfClassDecl()) {
23902466
checkDynamicSelfType(SD, SD->getValueInterfaceType());
@@ -2982,6 +3058,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
29823058

29833059
if (FD->getDeclContext()->getSelfClassDecl())
29843060
checkDynamicSelfType(FD, FD->getResultInterfaceType());
3061+
3062+
checkDefaultArguments(TC, FD->getParameters(), FD);
29853063
}
29863064

29873065
void visitModuleDecl(ModuleDecl *) { }
@@ -2997,7 +3075,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
29973075

29983076
if (auto *PL = EED->getParameterList()) {
29993077
TC.checkParameterAttributes(PL);
3000-
TC.checkDefaultArguments(PL, EED);
3078+
checkDefaultArguments(TC, PL, EED);
30013079
}
30023080

30033081
// Yell if our parent doesn't have a raw type but we have a raw value.
@@ -3252,6 +3330,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
32523330
} else {
32533331
TC.definedFunctions.push_back(CD);
32543332
}
3333+
3334+
checkDefaultArguments(TC, CD->getParameters(), CD);
32553335
}
32563336

32573337
void visitDestructorDecl(DestructorDecl *DD) {

lib/Sema/TypeCheckStmt.cpp

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,82 +1835,6 @@ Stmt *StmtChecker::visitBraceStmt(BraceStmt *BS) {
18351835
return BS;
18361836
}
18371837

1838-
static Optional<unsigned>
1839-
getParamIndex(const ParameterList *paramList, const ParamDecl *decl) {
1840-
ArrayRef<ParamDecl *> params = paramList->getArray();
1841-
for (unsigned i = 0; i < params.size(); ++i) {
1842-
if (params[i] == decl) return i;
1843-
}
1844-
return None;
1845-
}
1846-
1847-
static void
1848-
checkInheritedDefaultValueRestrictions(TypeChecker &TC, ParamDecl *PD) {
1849-
if (PD->getDefaultArgumentKind() != DefaultArgumentKind::Inherited)
1850-
return;
1851-
1852-
auto *DC = PD->getInnermostDeclContext();
1853-
const SourceFile *SF = DC->getParentSourceFile();
1854-
assert((SF && SF->Kind == SourceFileKind::Interface || PD->isImplicit()) &&
1855-
"explicit inherited default argument outside of a module interface?");
1856-
1857-
// The containing decl should be a designated initializer.
1858-
auto ctor = dyn_cast<ConstructorDecl>(DC);
1859-
if (!ctor || ctor->isConvenienceInit()) {
1860-
TC.diagnose(
1861-
PD, diag::inherited_default_value_not_in_designated_constructor);
1862-
return;
1863-
}
1864-
1865-
// The decl it overrides should also be a designated initializer.
1866-
auto overridden = ctor->getOverriddenDecl();
1867-
if (!overridden || overridden->isConvenienceInit()) {
1868-
TC.diagnose(
1869-
PD, diag::inherited_default_value_used_in_non_overriding_constructor);
1870-
if (overridden)
1871-
TC.diagnose(overridden, diag::overridden_here);
1872-
return;
1873-
}
1874-
1875-
// The corresponding parameter should have a default value.
1876-
Optional<unsigned> idx = getParamIndex(ctor->getParameters(), PD);
1877-
assert(idx && "containing decl does not contain param?");
1878-
ParamDecl *equivalentParam = overridden->getParameters()->get(*idx);
1879-
if (equivalentParam->getDefaultArgumentKind() == DefaultArgumentKind::None) {
1880-
TC.diagnose(PD, diag::corresponding_param_not_defaulted);
1881-
TC.diagnose(equivalentParam, diag::inherited_default_param_here);
1882-
}
1883-
}
1884-
1885-
/// Check the default arguments that occur within this pattern.
1886-
void TypeChecker::checkDefaultArguments(ParameterList *params,
1887-
ValueDecl *VD) {
1888-
for (auto *param : *params) {
1889-
checkInheritedDefaultValueRestrictions(*this, param);
1890-
if (!param->getDefaultValue() ||
1891-
!param->hasInterfaceType() ||
1892-
param->getInterfaceType()->hasError())
1893-
continue;
1894-
1895-
Expr *e = param->getDefaultValue();
1896-
auto *initContext = param->getDefaultArgumentInitContext();
1897-
1898-
auto resultTy =
1899-
typeCheckParameterDefault(e, initContext, param->getType(),
1900-
/*isAutoClosure=*/param->isAutoClosure());
1901-
1902-
if (resultTy) {
1903-
param->setDefaultValue(e);
1904-
}
1905-
1906-
checkInitializerErrorHandling(initContext, e);
1907-
1908-
// Walk the checked initializer and contextualize any closures
1909-
// we saw there.
1910-
(void)contextualizeInitializer(initContext, e);
1911-
}
1912-
}
1913-
19141838
void TypeChecker::checkRawValueExpr(EnumDecl *ED, EnumElementDecl *EED) {
19151839
Type rawTy = ED->getRawType();
19161840
Expr *rawValue = EED->getRawValueExpr();
@@ -2159,7 +2083,6 @@ TypeCheckFunctionBodyUntilRequest::evaluate(Evaluator &evaluator,
21592083

21602084
// FIXME(InterfaceTypeRequest): Remove this.
21612085
(void)AFD->getInterfaceType();
2162-
tc.checkDefaultArguments(AFD->getParameters(), AFD);
21632086

21642087
BraceStmt *body = AFD->getBody();
21652088
if (!body || AFD->isBodyTypeChecked())

lib/Sema/TypeChecker.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,11 +1006,9 @@ class TypeChecker final : public LazyResolver {
10061006
Type checkReferenceOwnershipAttr(VarDecl *D, Type interfaceType,
10071007
ReferenceOwnershipAttr *attr);
10081008

1009-
/// Check the default arguments that occur within this value decl.
1010-
void checkDefaultArguments(ParameterList *params, ValueDecl *VD);
10111009
/// Check the raw value expression in this enum element.
10121010
void checkRawValueExpr(EnumDecl *parent, EnumElementDecl *Elt);
1013-
1011+
10141012
virtual void resolveDeclSignature(ValueDecl *VD) override {
10151013
validateDecl(VD);
10161014
}

0 commit comments

Comments
 (0)