Skip to content

Nuke DeclChecker::IsFirstPass #15731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/Sema/CodeSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1413,8 +1413,7 @@ void TypeChecker::completePropertyBehaviorParameter(VarDecl *VD,
SourceLoc(), /*implicit*/ true);
Parameter->setBody(Body);

typeCheckDecl(Parameter, true);
typeCheckDecl(Parameter, false);
typeCheckDecl(Parameter);
addMemberToContextIfNeeded(Parameter, DC);

// Add the witnesses to the conformance.
Expand Down
6 changes: 2 additions & 4 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,10 +819,8 @@ namespace {
if (auto captureList = dyn_cast<CaptureListExpr>(expr)) {
// Validate the capture list.
for (auto capture : captureList->getCaptureList()) {
TC.typeCheckDecl(capture.Init, true);
TC.typeCheckDecl(capture.Init, false);
TC.typeCheckDecl(capture.Var, true);
TC.typeCheckDecl(capture.Var, false);
TC.typeCheckDecl(capture.Init);
TC.typeCheckDecl(capture.Var);
}

// Since closure expression is contained by capture list
Expand Down
97 changes: 11 additions & 86 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3959,23 +3959,15 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
public:
TypeChecker &TC;

// For library-style parsing, we need to make two passes over the global
// scope. These booleans indicate whether this is currently the first or
// second pass over the global scope (or neither, if we're in a context where
// we only visit each decl once).
unsigned IsFirstPass : 1;

DeclChecker(TypeChecker &TC, bool IsFirstPass)
: TC(TC), IsFirstPass(IsFirstPass) {}
explicit DeclChecker(TypeChecker &TC) : TC(TC) {}

void visit(Decl *decl) {
FrontendStatsTracer StatsTracer(TC.Context.Stats, "typecheck-decl", decl);
PrettyStackTraceDecl StackTrace("type-checking", decl);

DeclVisitor<DeclChecker>::visit(decl);

if (IsFirstPass)
TC.checkUnsupportedProtocolType(decl);
TC.checkUnsupportedProtocolType(decl);

if (auto VD = dyn_cast<ValueDecl>(decl)) {
checkRedeclaration(TC, VD);
Expand All @@ -3985,8 +3977,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
// expressions to mean something builtin to the language. We *do* allow
// these if they are escaped with backticks though.
auto &Context = TC.Context;
if (IsFirstPass &&
VD->getDeclContext()->isTypeContext() &&
if (VD->getDeclContext()->isTypeContext() &&
(VD->getFullName().isSimpleName(Context.Id_Type) ||
VD->getFullName().isSimpleName(Context.Id_Protocol)) &&
VD->getNameLoc().isValid() &&
Expand Down Expand Up @@ -4115,14 +4106,12 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
// (that were previously only validated at point of synthesis)
if (auto getter = VD->getGetter()) {
if (getter->hasBody()) {
TC.typeCheckDecl(getter, true);
TC.typeCheckDecl(getter, false);
TC.typeCheckDecl(getter);
}
}
if (auto setter = VD->getSetter()) {
if (setter->hasBody()) {
TC.typeCheckDecl(setter, true);
TC.typeCheckDecl(setter, false);
TC.typeCheckDecl(setter);
}
}

Expand All @@ -4135,9 +4124,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
}

void visitPatternBindingDecl(PatternBindingDecl *PBD) {
if (!IsFirstPass)
return;

if (PBD->isBeingValidated())
return;

Expand Down Expand Up @@ -4264,20 +4250,12 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
}

void visitSubscriptDecl(SubscriptDecl *SD) {
if (!IsFirstPass) {
return;
}

TC.validateDecl(SD);
TC.checkDeclAttributes(SD);
checkAccessControl(TC, SD);
}

void visitTypeAliasDecl(TypeAliasDecl *TAD) {
if (!IsFirstPass) {
return;
}

TC.checkDeclAttributesEarly(TAD);
TC.computeAccessLevel(TAD);

Expand All @@ -4287,10 +4265,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
}

void visitAssociatedTypeDecl(AssociatedTypeDecl *AT) {
if (!IsFirstPass) {
return;
}

TC.validateDecl(AT);

auto *proto = AT->getProtocol();
Expand Down Expand Up @@ -4350,14 +4324,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
}

void visitEnumDecl(EnumDecl *ED) {
if (!IsFirstPass) {
for (Decl *member : ED->getMembers())
visit(member);

TC.checkConformancesInContext(ED, ED);
return;
}

TC.checkDeclAttributesEarly(ED);
TC.computeAccessLevel(ED);

Expand Down Expand Up @@ -4387,17 +4353,10 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
}

TC.checkDeclCircularity(ED);
TC.ConformanceContexts.push_back(ED);
}

void visitStructDecl(StructDecl *SD) {
if (!IsFirstPass) {
for (Decl *Member : SD->getMembers())
visit(Member);

TC.checkConformancesInContext(SD, SD);
return;
}

TC.checkDeclAttributesEarly(SD);
TC.computeAccessLevel(SD);

Expand All @@ -4415,6 +4374,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
checkAccessControl(TC, SD);

TC.checkDeclCircularity(SD);
TC.ConformanceContexts.push_back(SD);
}

/// Check whether the given properties can be @NSManaged in this class.
Expand Down Expand Up @@ -4521,14 +4481,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {


void visitClassDecl(ClassDecl *CD) {
if (!IsFirstPass) {
for (Decl *Member : CD->getMembers())
visit(Member);

TC.checkConformancesInContext(CD, CD);
return;
}

TC.checkDeclAttributesEarly(CD);
TC.computeAccessLevel(CD);

Expand Down Expand Up @@ -4649,16 +4601,10 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
checkAccessControl(TC, CD);

TC.checkDeclCircularity(CD);
TC.ConformanceContexts.push_back(CD);
}

void visitProtocolDecl(ProtocolDecl *PD) {
if (!IsFirstPass) {
for (auto Member : PD->getMembers())
visit(Member);

return;
}

TC.checkDeclAttributesEarly(PD);
TC.computeAccessLevel(PD);

Expand Down Expand Up @@ -4771,9 +4717,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
}

void visitFuncDecl(FuncDecl *FD) {
if (!IsFirstPass)
return;

TC.validateDecl(FD);
checkAccessControl(TC, FD);

Expand Down Expand Up @@ -6185,22 +6128,12 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
}

void visitEnumElementDecl(EnumElementDecl *EED) {
if (!IsFirstPass) {
return;
}

TC.validateDecl(EED);
TC.checkDeclAttributes(EED);
checkAccessControl(TC, EED);
}

void visitExtensionDecl(ExtensionDecl *ED) {
if (!IsFirstPass) {
for (Decl *Member : ED->getMembers())
visit(Member);
return;
}

TC.validateExtension(ED);

TC.checkDeclAttributesEarly(ED);
Expand Down Expand Up @@ -6241,7 +6174,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
for (Decl *Member : ED->getMembers())
visit(Member);

TC.checkConformancesInContext(ED, ED);
TC.ConformanceContexts.push_back(ED);

if (!ED->isInvalid())
TC.checkDeclAttributes(ED);
Expand Down Expand Up @@ -6295,10 +6228,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
}

void visitConstructorDecl(ConstructorDecl *CD) {
if (!IsFirstPass) {
return;
}

TC.validateDecl(CD);

// If this initializer overrides a 'required' initializer, it must itself
Expand Down Expand Up @@ -6353,10 +6282,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
}

void visitDestructorDecl(DestructorDecl *DD) {
if (!IsFirstPass) {
return;
}

TC.validateDecl(DD);
TC.checkDeclAttributes(DD);

Expand Down Expand Up @@ -6426,9 +6351,9 @@ bool TypeChecker::isAvailabilitySafeForConformance(
return requirementInfo.isContainedIn(witnessInfo);
}

void TypeChecker::typeCheckDecl(Decl *D, bool isFirstPass) {
void TypeChecker::typeCheckDecl(Decl *D) {
checkForForbiddenPrefix(D);
DeclChecker(*this, isFirstPass).visit(D);
DeclChecker(*this).visit(D);
}

// A class is @objc if it does not have generic ancestry, and it either has
Expand Down
9 changes: 3 additions & 6 deletions lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
}

Stmt *visitDeferStmt(DeferStmt *DS) {
TC.typeCheckDecl(DS->getTempDecl(), /*isFirstPass*/true);
TC.typeCheckDecl(DS->getTempDecl(), /*isFirstPass*/false);
TC.typeCheckDecl(DS->getTempDecl());

Expr *theCall = DS->getCallExpr();
TC.typeCheckExpression(theCall, DC);
Expand Down Expand Up @@ -836,8 +835,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
// the list of raw cases.
for (auto node : S->getRawCases()) {
if (!node.is<Decl*>()) continue;
TC.typeCheckDecl(node.get<Decl*>(), /*isFirstPass*/true);
TC.typeCheckDecl(node.get<Decl*>(), /*isFirstPass*/false);
TC.typeCheckDecl(node.get<Decl*>());
}

auto cases = S->getCases();
Expand Down Expand Up @@ -1356,8 +1354,7 @@ Stmt *StmtChecker::visitBraceStmt(BraceStmt *BS) {
(Loc == EndTypeCheckLoc || SM.isBeforeInBuffer(EndTypeCheckLoc, Loc)))
break;

TC.typeCheckDecl(SubDecl, /*isFirstPass*/true);
TC.typeCheckDecl(SubDecl, /*isFirstPass*/false);
TC.typeCheckDecl(SubDecl);
}

return BS;
Expand Down
41 changes: 17 additions & 24 deletions lib/Sema/TypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,18 @@ static void typeCheckFunctionsAndExternalDecls(SourceFile &SF, TypeChecker &TC)
unsigned currentExternalDef = TC.Context.LastCheckedExternalDefinition;
unsigned currentSynthesizedDecl = SF.LastCheckedSynthesizedDecl;
do {
// Type check conformance contexts.
for (unsigned i = 0; i != TC.ConformanceContexts.size(); ++i) {
auto decl = TC.ConformanceContexts[i];
if (auto *ext = dyn_cast<ExtensionDecl>(decl))
TC.checkConformancesInContext(ext, ext);
else {
auto *ntd = cast<NominalTypeDecl>(decl);
TC.checkConformancesInContext(ntd, ntd);
}
}
TC.ConformanceContexts.clear();

// Type check the body of each of the function in turn. Note that outside
// functions must be visited before nested functions for type-checking to
// work correctly.
Expand All @@ -460,6 +472,7 @@ static void typeCheckFunctionsAndExternalDecls(SourceFile &SF, TypeChecker &TC)

if (auto *AFD = dyn_cast<AbstractFunctionDecl>(decl)) {
TC.typeCheckAbstractFunctionBody(AFD);
TC.checkFunctionErrorHandling(AFD);
continue;
}
if (isa<NominalTypeDecl>(decl)) {
Expand Down Expand Up @@ -498,8 +511,7 @@ static void typeCheckFunctionsAndExternalDecls(SourceFile &SF, TypeChecker &TC)
currentSynthesizedDecl != n;
++currentSynthesizedDecl) {
auto decl = SF.SynthesizedDecls[currentSynthesizedDecl];
TC.typeCheckDecl(decl, /*isFirstPass*/true);
TC.typeCheckDecl(decl, /*isFirstPass*/false);
TC.typeCheckDecl(decl);
}

// Ensure that the requirements of the given conformance are
Expand All @@ -522,6 +534,7 @@ static void typeCheckFunctionsAndExternalDecls(SourceFile &SF, TypeChecker &TC)
currentExternalDef < TC.Context.ExternalDefinitions.size() ||
currentSynthesizedDecl < SF.SynthesizedDecls.size() ||
!TC.DeclsToFinalize.empty() ||
!TC.ConformanceContexts.empty() ||
!TC.DelayedRequirementSignatures.empty() ||
!TC.UsedConformances.empty() ||
!TC.PartiallyCheckedConformances.empty());
Expand Down Expand Up @@ -559,11 +572,6 @@ static void typeCheckFunctionsAndExternalDecls(SourceFile &SF, TypeChecker &TC)
for (AbstractFunctionDecl *FD : TC.definedFunctions) {
TC.checkFunctionErrorHandling(FD);
}
for (auto decl : TC.Context.ExternalDefinitions) {
if (auto fn = dyn_cast<AbstractFunctionDecl>(decl)) {
TC.checkFunctionErrorHandling(fn);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

}
}
}

void swift::typeCheckExternalDefinitions(SourceFile &SF) {
Expand Down Expand Up @@ -654,30 +662,15 @@ void swift::performTypeChecking(SourceFile &SF, TopLevelContext &TLC,
}
});

// FIXME: Check for cycles in class inheritance here?

// Type check the top-level elements of the source file.
for (auto D : llvm::makeArrayRef(SF.Decls).slice(StartElem)) {
if (isa<TopLevelCodeDecl>(D))
continue;

TC.typeCheckDecl(D, /*isFirstPass*/true);
}

// At this point, we can perform general name lookup into any type.

// We don't know the types of all the global declarations in the first
// pass, which means we can't completely analyze everything. Perform the
// second pass now.

bool hasTopLevelCode = false;
for (auto D : llvm::makeArrayRef(SF.Decls).slice(StartElem)) {
if (auto *TLCD = dyn_cast<TopLevelCodeDecl>(D)) {
hasTopLevelCode = true;
// Immediately perform global name-binding etc.
TC.typeCheckTopLevelCodeDecl(TLCD);
} else {
TC.typeCheckDecl(D, /*isFirstPass*/false);
TC.typeCheckDecl(D);
}
}

Expand Down Expand Up @@ -811,7 +804,7 @@ bool swift::typeCheckCompletionDecl(Decl *D) {
if (auto ext = dyn_cast<ExtensionDecl>(D))
TC.validateExtension(ext);
else
TC.typeCheckDecl(D, true);
TC.typeCheckDecl(D);
return true;
}

Expand Down
Loading