35
35
#include " swift/AST/PrettyStackTrace.h"
36
36
#include " swift/AST/ProtocolConformance.h"
37
37
#include " swift/AST/SourceFile.h"
38
+ #include " swift/AST/TypeCheckRequests.h"
38
39
#include " swift/Basic/Statistic.h"
39
40
#include " swift/Basic/STLExtras.h"
40
41
#include " swift/Basic/Timer.h"
@@ -310,26 +311,35 @@ static void typeCheckFunctionsAndExternalDecls(SourceFile &SF, TypeChecker &TC)
310
311
}
311
312
312
313
void swift::performTypeChecking (SourceFile &SF, unsigned StartElem) {
313
- if (SF.ASTStage == SourceFile::TypeChecked)
314
- return ;
314
+ return (void )evaluateOrDefault (SF.getASTContext ().evaluator ,
315
+ TypeCheckSourceFileRequest{&SF, StartElem},
316
+ false );
317
+ }
318
+
319
+ llvm::Expected<bool >
320
+ TypeCheckSourceFileRequest::evaluate (Evaluator &eval,
321
+ SourceFile *SF, unsigned StartElem) const {
322
+ assert (SF && " Source file cannot be null!" );
323
+ assert (SF->ASTStage != SourceFile::TypeChecked &&
324
+ " Should not be re-typechecking this file!" );
315
325
316
326
// Eagerly build the top-level scopes tree before type checking
317
327
// because type-checking expressions mutates the AST and that throws off the
318
328
// scope-based lookups. Only the top-level scopes because extensions have not
319
329
// been bound yet.
320
- auto &Ctx = SF. getASTContext ();
321
- if (Ctx.LangOpts .EnableASTScopeLookup && SF. isSuitableForASTScopes ())
322
- SF. getScope ()
330
+ auto &Ctx = SF-> getASTContext ();
331
+ if (Ctx.LangOpts .EnableASTScopeLookup && SF-> isSuitableForASTScopes ())
332
+ SF-> getScope ()
323
333
.buildEnoughOfTreeForTopLevelExpressionsButDontRequestGenericsOrExtendedNominals ();
324
334
325
- BufferIndirectlyCausingDiagnosticRAII cpr (SF);
335
+ BufferIndirectlyCausingDiagnosticRAII cpr (* SF);
326
336
327
337
// Make sure we have a type checker.
328
338
TypeChecker &TC = createTypeChecker (Ctx);
329
339
330
340
// Make sure that name binding has been completed before doing any type
331
341
// checking.
332
- performNameBinding (SF, StartElem);
342
+ performNameBinding (* SF, StartElem);
333
343
334
344
// Could build scope maps here because the AST is stable now.
335
345
@@ -340,22 +350,22 @@ void swift::performTypeChecking(SourceFile &SF, unsigned StartElem) {
340
350
// Disable this optimization if we're compiling SwiftOnoneSupport, because
341
351
// we _definitely_ need to look inside every declaration to figure out
342
352
// what gets prespecialized.
343
- if (SF. getParentModule ()->isOnoneSupportModule ())
353
+ if (SF-> getParentModule ()->isOnoneSupportModule ())
344
354
Ctx.TypeCheckerOpts .SkipNonInlinableFunctionBodies = false ;
345
355
346
356
if (!Ctx.LangOpts .DisableAvailabilityChecking ) {
347
357
// Build the type refinement hierarchy for the primary
348
358
// file before type checking.
349
- TypeChecker::buildTypeRefinementContextHierarchy (SF, StartElem);
359
+ TypeChecker::buildTypeRefinementContextHierarchy (* SF, StartElem);
350
360
}
351
361
352
362
// Resolve extensions. This has to occur first during type checking,
353
363
// because the extensions need to be wired into the AST for name lookup
354
364
// to work.
355
- ::bindExtensions (SF);
365
+ ::bindExtensions (* SF);
356
366
357
367
// Type check the top-level elements of the source file.
358
- for (auto D : llvm::makeArrayRef (SF. Decls ).slice (StartElem)) {
368
+ for (auto D : llvm::makeArrayRef (SF-> Decls ).slice (StartElem)) {
359
369
if (auto *TLCD = dyn_cast<TopLevelCodeDecl>(D)) {
360
370
// Immediately perform global name-binding etc.
361
371
TypeChecker::typeCheckTopLevelCodeDecl (TLCD);
@@ -367,44 +377,18 @@ void swift::performTypeChecking(SourceFile &SF, unsigned StartElem) {
367
377
368
378
// If we're in REPL mode, inject temporary result variables and other stuff
369
379
// that the REPL needs to synthesize.
370
- if (SF. Kind == SourceFileKind::REPL && !Ctx.hadError ())
371
- TypeChecker::processREPLTopLevel (SF, StartElem);
380
+ if (SF-> Kind == SourceFileKind::REPL && !Ctx.hadError ())
381
+ TypeChecker::processREPLTopLevel (* SF, StartElem);
372
382
373
- typeCheckFunctionsAndExternalDecls (SF, TC);
383
+ typeCheckFunctionsAndExternalDecls (* SF, TC);
374
384
}
375
385
376
386
// Checking that benefits from having the whole module available.
377
387
if (!Ctx.TypeCheckerOpts .DelayWholeModuleChecking ) {
378
- performWholeModuleTypeChecking (SF);
388
+ performWholeModuleTypeChecking (* SF);
379
389
}
380
390
381
- // Verify that we've checked types correctly.
382
- SF.ASTStage = SourceFile::TypeChecked;
383
-
384
- {
385
- FrontendStatsTracer tracer (Ctx.Stats , " AST verification" );
386
- // Verify the SourceFile.
387
- verify (SF);
388
-
389
- // Verify imported modules.
390
- //
391
- // Skip per-file verification in whole-module mode. Verifying imports
392
- // between files could cause the importer to cache declarations without
393
- // adding them to the ASTContext. This happens when the importer registers a
394
- // declaration without a valid TypeChecker instance, as is the case during
395
- // verification. A subsequent file may require that declaration to be fully
396
- // imported (e.g. to synthesized a function body), but since it has already
397
- // been cached, it will never be added to the ASTContext. The solution is to
398
- // skip verification and avoid caching it.
399
- #ifndef NDEBUG
400
- if (!Ctx.TypeCheckerOpts .DelayWholeModuleChecking &&
401
- SF.Kind != SourceFileKind::REPL &&
402
- SF.Kind != SourceFileKind::SIL &&
403
- !Ctx.LangOpts .DebuggerSupport ) {
404
- Ctx.verifyAllLoadedModules ();
405
- }
406
- #endif
407
- }
391
+ return true ;
408
392
}
409
393
410
394
void swift::performWholeModuleTypeChecking (SourceFile &SF) {
0 commit comments