Skip to content

Commit efdc358

Browse files
authored
Merge pull request swiftlang#33806 from hamishknight/typed-def
2 parents aa0f8e6 + 28246a7 commit efdc358

17 files changed

+105
-53
lines changed

include/swift/AST/AnyFunctionRef.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ class AnyFunctionRef {
137137
return cast<AutoClosureExpr>(ACE)->getBody();
138138
}
139139

140-
void setBody(BraceStmt *stmt, bool isSingleExpression) {
140+
void setTypecheckedBody(BraceStmt *stmt, bool isSingleExpression) {
141141
if (auto *AFD = TheFunction.dyn_cast<AbstractFunctionDecl *>()) {
142-
AFD->setBody(stmt);
142+
AFD->setBody(stmt, AbstractFunctionDecl::BodyKind::TypeChecked);
143143
AFD->setHasSingleExpressionBody(isSingleExpression);
144144
return;
145145
}

include/swift/AST/Decl.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5835,6 +5835,7 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
58355835
};
58365836

58375837
friend class ParseAbstractFunctionBodyRequest;
5838+
friend class TypeCheckFunctionBodyRequest;
58385839

58395840
CaptureInfo Captures;
58405841

@@ -5968,7 +5969,12 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
59685969
/// \sa hasBody()
59695970
BraceStmt *getBody(bool canSynthesize = true) const;
59705971

5971-
void setBody(BraceStmt *S, BodyKind NewBodyKind = BodyKind::Parsed);
5972+
/// Retrieve the type-checked body of the given function, or \c nullptr if
5973+
/// there's no body available.
5974+
BraceStmt *getTypecheckedBody() const;
5975+
5976+
/// Set a new body for the function.
5977+
void setBody(BraceStmt *S, BodyKind NewBodyKind);
59725978

59735979
/// Note that the body was skipped for this function. Function body
59745980
/// cannot be attached after this call.

include/swift/AST/TypeCheckRequests.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -884,25 +884,25 @@ class LazyStoragePropertyRequest :
884884
bool isCached() const { return true; }
885885
};
886886

887-
/// Request to type check the body of the given function.
888-
///
889-
/// Produces true if an error occurred, false otherwise.
890-
/// FIXME: it would be far better to return the type-checked body.
891-
class TypeCheckFunctionBodyRequest :
892-
public SimpleRequest<TypeCheckFunctionBodyRequest,
893-
bool(AbstractFunctionDecl *),
894-
RequestFlags::Cached|RequestFlags::DependencySource> {
887+
/// Request to retrieve the type-checked body of the given function.
888+
class TypeCheckFunctionBodyRequest
889+
: public SimpleRequest<
890+
TypeCheckFunctionBodyRequest, BraceStmt *(AbstractFunctionDecl *),
891+
RequestFlags::SeparatelyCached | RequestFlags::DependencySource> {
895892
public:
896893
using SimpleRequest::SimpleRequest;
897894

898895
private:
899896
friend SimpleRequest;
900897

901898
// Evaluation.
902-
bool evaluate(Evaluator &evaluator, AbstractFunctionDecl *func) const;
899+
BraceStmt *evaluate(Evaluator &evaluator, AbstractFunctionDecl *func) const;
903900

904901
public:
902+
// Separate caching.
905903
bool isCached() const { return true; }
904+
Optional<BraceStmt *> getCachedResult() const;
905+
void cacheResult(BraceStmt *body) const;
906906

907907
public:
908908
// Incremental dependencies.

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ SWIFT_REQUEST(TypeChecker, SynthesizeAccessorRequest,
208208
SWIFT_REQUEST(TypeChecker, TangentStoredPropertyRequest,
209209
llvm::Expected<VarDecl *>(VarDecl *, CanType), Cached, NoLocationInfo)
210210
SWIFT_REQUEST(TypeChecker, TypeCheckFunctionBodyRequest,
211-
bool(AbstractFunctionDecl *), Cached, NoLocationInfo)
211+
BraceStmt *(AbstractFunctionDecl *), SeparatelyCached,
212+
NoLocationInfo)
212213
SWIFT_REQUEST(TypeChecker, TypeCheckASTNodeAtLocRequest,
213214
bool(DeclContext *, SourceLoc),
214215
Uncached, NoLocationInfo)

lib/AST/Decl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6786,6 +6786,13 @@ BraceStmt *AbstractFunctionDecl::getBody(bool canSynthesize) const {
67866786
nullptr);
67876787
}
67886788

6789+
BraceStmt *AbstractFunctionDecl::getTypecheckedBody() const {
6790+
auto &ctx = getASTContext();
6791+
auto *mutableThis = const_cast<AbstractFunctionDecl *>(this);
6792+
return evaluateOrDefault(
6793+
ctx.evaluator, TypeCheckFunctionBodyRequest{mutableThis}, nullptr);
6794+
}
6795+
67896796
void AbstractFunctionDecl::setBody(BraceStmt *S, BodyKind NewBodyKind) {
67906797
assert(getBodyKind() != BodyKind::Skipped &&
67916798
"cannot set a body if it was skipped");

lib/AST/TypeCheckRequests.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,33 @@ void TypeCheckSourceFileRequest::cacheResult(evaluator::SideEffect) const {
13991399
// TypeCheckFunctionBodyRequest computation.
14001400
//----------------------------------------------------------------------------//
14011401

1402+
Optional<BraceStmt *> TypeCheckFunctionBodyRequest::getCachedResult() const {
1403+
using BodyKind = AbstractFunctionDecl::BodyKind;
1404+
auto *afd = std::get<0>(getStorage());
1405+
switch (afd->getBodyKind()) {
1406+
case BodyKind::Deserialized:
1407+
case BodyKind::MemberwiseInitializer:
1408+
case BodyKind::None:
1409+
case BodyKind::Skipped:
1410+
// These cases don't have any body available.
1411+
return nullptr;
1412+
1413+
case BodyKind::TypeChecked:
1414+
return afd->Body;
1415+
1416+
case BodyKind::Synthesize:
1417+
case BodyKind::Parsed:
1418+
case BodyKind::Unparsed:
1419+
return None;
1420+
}
1421+
llvm_unreachable("Unhandled BodyKind in switch");
1422+
}
1423+
1424+
void TypeCheckFunctionBodyRequest::cacheResult(BraceStmt *body) const {
1425+
auto *afd = std::get<0>(getStorage());
1426+
afd->setBody(body, AbstractFunctionDecl::BodyKind::TypeChecked);
1427+
}
1428+
14021429
evaluator::DependencySource
14031430
TypeCheckFunctionBodyRequest::readDependencySource(
14041431
const evaluator::DependencyRecorder &e) const {

lib/SILGen/SILGenConstructor.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,9 @@ void SILGenFunction::emitValueConstructor(ConstructorDecl *ctor) {
393393
emitMemberInitializers(ctor, selfDecl, nominal);
394394
}
395395

396-
emitProfilerIncrement(ctor->getBody());
396+
emitProfilerIncrement(ctor->getTypecheckedBody());
397397
// Emit the constructor body.
398-
emitStmt(ctor->getBody());
398+
emitStmt(ctor->getTypecheckedBody());
399399

400400

401401
// Build a custom epilog block, since the AST representation of the
@@ -622,7 +622,7 @@ void SILGenFunction::emitClassConstructorAllocator(ConstructorDecl *ctor) {
622622
SILValue initedSelfValue = emitApplyWithRethrow(Loc, initVal.forward(*this),
623623
initTy, subMap, args);
624624

625-
emitProfilerIncrement(ctor->getBody());
625+
emitProfilerIncrement(ctor->getTypecheckedBody());
626626

627627
// Return the initialized 'self'.
628628
B.createReturn(ImplicitReturnLocation::getImplicitReturnLoc(Loc),
@@ -632,7 +632,7 @@ void SILGenFunction::emitClassConstructorAllocator(ConstructorDecl *ctor) {
632632
void SILGenFunction::emitClassConstructorInitializer(ConstructorDecl *ctor) {
633633
MagicFunctionName = SILGenModule::getMagicFunctionName(ctor);
634634

635-
assert(ctor->getBody() && "Class constructor without a body?");
635+
assert(ctor->getTypecheckedBody() && "Class constructor without a body?");
636636

637637
// True if this constructor delegates to a peer constructor with self.init().
638638
bool isDelegating = false;
@@ -775,9 +775,9 @@ void SILGenFunction::emitClassConstructorInitializer(ConstructorDecl *ctor) {
775775
emitMemberInitializers(ctor, selfDecl, selfClassDecl);
776776
}
777777

778-
emitProfilerIncrement(ctor->getBody());
778+
emitProfilerIncrement(ctor->getTypecheckedBody());
779779
// Emit the constructor body.
780-
emitStmt(ctor->getBody());
780+
emitStmt(ctor->getTypecheckedBody());
781781

782782
// Emit the call to super.init() right before exiting from the initializer.
783783
if (NeedsBoxForSelf) {

lib/SILGen/SILGenDestructor.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ void SILGenFunction::emitDestroyingDestructor(DestructorDecl *dd) {
3535
// We won't actually emit the block until we finish with the destructor body.
3636
prepareEpilog(false, false, CleanupLocation::get(Loc));
3737

38-
emitProfilerIncrement(dd->getBody());
38+
emitProfilerIncrement(dd->getTypecheckedBody());
3939
// Emit the destructor body.
40-
emitStmt(dd->getBody());
40+
emitStmt(dd->getTypecheckedBody());
4141

4242
Optional<SILValue> maybeReturnValue;
4343
SILLocation returnLoc(Loc);
@@ -151,7 +151,7 @@ void SILGenFunction::emitDeallocatingDestructor(DestructorDecl *dd) {
151151
selfForDealloc = B.createUncheckedRefCast(loc, selfForDealloc, classTy);
152152
B.createDeallocRef(loc, selfForDealloc, false);
153153

154-
emitProfilerIncrement(dd->getBody());
154+
emitProfilerIncrement(dd->getTypecheckedBody());
155155

156156
// Return.
157157
B.createReturn(loc, emitEmptyTuple(loc));
@@ -212,9 +212,9 @@ void SILGenFunction::emitObjCDestructor(SILDeclRef dtor) {
212212
// We won't actually emit the block until we finish with the destructor body.
213213
prepareEpilog(false, false, CleanupLocation::get(loc));
214214

215-
emitProfilerIncrement(dd->getBody());
215+
emitProfilerIncrement(dd->getTypecheckedBody());
216216
// Emit the destructor body.
217-
emitStmt(dd->getBody());
217+
emitStmt(dd->getTypecheckedBody());
218218

219219
Optional<SILValue> maybeReturnValue;
220220
SILLocation returnLoc(loc);

lib/SILGen/SILGenFunction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,8 @@ void SILGenFunction::emitFunction(FuncDecl *fd) {
510510
fd->getResultInterfaceType(), fd->hasThrows(), fd->getThrowsLoc());
511511
prepareEpilog(true, fd->hasThrows(), CleanupLocation(fd));
512512

513-
emitProfilerIncrement(fd->getBody());
514-
emitStmt(fd->getBody());
513+
emitProfilerIncrement(fd->getTypecheckedBody());
514+
emitStmt(fd->getTypecheckedBody());
515515

516516
emitEpilog(fd);
517517

lib/Sema/CSClosure.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ SolutionApplicationToFunctionResult ConstraintSystem::applySolution(
329329
if (!newBody)
330330
return SolutionApplicationToFunctionResult::Failure;
331331

332-
fn.setBody(newBody, /*isSingleExpression=*/false);
332+
fn.setTypecheckedBody(newBody, /*isSingleExpression=*/false);
333333
if (closure) {
334334
solution.setExprTypes(closure);
335335
}

0 commit comments

Comments
 (0)