Skip to content

Commit

Permalink
Improve type checker revisiting mechanism (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer authored Aug 25, 2024
1 parent 8536bf4 commit 7e9749e
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/SourceFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ void SourceFile::runTypeCheckerPost() { // NOLINT(misc-no-recursion)
do {
typeCheckerRuns++;
totalTypeCheckerRuns++;
reVisitRequested = false;

// Type-check the current file first. Multiple times, if requested
timer.resume();
Expand All @@ -286,7 +287,7 @@ void SourceFile::runTypeCheckerPost() { // NOLINT(misc-no-recursion)
// Then type-check all dependencies
for (const auto &[importName, sourceFile] : dependencies)
sourceFile->runTypeCheckerPost();
} while (typeChecker.reVisitRequested);
} while (reVisitRequested);

checkForSoftErrors();

Expand Down
3 changes: 2 additions & 1 deletion src/SourceFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,13 @@ class SourceFile {
bool isMainFile = true;
bool alwaysKeepSymbolsOnNameCollision = false;
bool ignoreWarnings = false;
bool restoredFromCache = false;
bool reVisitRequested = false;
CompileStageType previousStage = NONE;
SourceFileAntlrCtx antlrCtx;
CompilerOutput compilerOutput;
SourceFile *parent;
std::string cacheKey;
bool restoredFromCache = false;
EntryNode *ast = nullptr;
std::unique_ptr<Scope> globalScope;
llvm::LLVMContext context;
Expand Down
4 changes: 1 addition & 3 deletions src/typechecker/OpRuleManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,9 +703,7 @@ ExprResult OpRuleManager::isOperatorOverloadingFctAvailable(ASTNode *node, const
opFctPointers.at(opIdx) = callee;

// Check if we need to request a re-visit, because the function body was not type-checked yet
const bool isCallToImportedSourceFile = callee->entry->scope->isImportedBy(typeChecker->rootScope);
if (!callee->alreadyTypeChecked && !isCallToImportedSourceFile)
typeChecker->reVisitRequested = true;
typeChecker->requestRevisitIfRequired(callee);

// Check if the called function has sufficient visibility
const bool isImported = calleeParentScope != nullptr && calleeParentScope->isImportedBy(typeChecker->rootScope);
Expand Down
5 changes: 2 additions & 3 deletions src/typechecker/TypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ TypeChecker::TypeChecker(GlobalResourceManager &resourceManager, SourceFile *sou
std::any TypeChecker::visitEntry(EntryNode *node) {
// Initialize
currentScope = rootScope;
reVisitRequested = false;

// Initialize AST nodes with size of 1
const bool isPrepare = typeCheckerMode == TC_MODE_PRE;
Expand Down Expand Up @@ -2667,9 +2666,9 @@ std::vector<const Function *> &TypeChecker::getOpFctPointers(ASTNode *node) cons
*
* @param fct Function to check
*/
void TypeChecker::requestRevisitIfRequired(const Function *fct) {
void TypeChecker::requestRevisitIfRequired(const Function *fct) const {
if (fct && !fct->alreadyTypeChecked && !fct->entry->scope->isImportedBy(rootScope))
reVisitRequested = true;
fct->entry->scope->sourceFile->reVisitRequested = true;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/typechecker/TypeChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ class TypeChecker final : CompilerPass, public ASTVisitor {
std::any visitCustomDataType(CustomDataTypeNode *node) override;
std::any visitFunctionDataType(FunctionDataTypeNode *node) override;

// Public members
bool reVisitRequested = false;

private:
// Private members
OpRuleManager opRuleManager = OpRuleManager(this);
Expand All @@ -147,7 +144,7 @@ class TypeChecker final : CompilerPass, public ASTVisitor {
[[nodiscard]] QualType mapImportedScopeTypeToLocalType(const Scope *sourceScope, const QualType &symbolType) const;
static void autoDeReference(QualType &symbolType);
std::vector<const Function *> &getOpFctPointers(ASTNode *node) const;
void requestRevisitIfRequired(const Function *fct);
void requestRevisitIfRequired(const Function *fct) const;
void softError(const ASTNode *node, SemanticErrorType errorType, const std::string &message) const;

// Implicit code generation
Expand Down
1 change: 1 addition & 0 deletions src/typechecker/TypeCheckerPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ std::any TypeChecker::visitExtDeclPrepare(ExtDeclNode *node) {
const Function spiceFunc(node->extFunctionName, node->entry, QualType(TY_DYN), returnType, argList, {}, node);
node->extFunction = FunctionManager::insert(currentScope, spiceFunc, &node->extFunctionManifestations);
node->extFunction->mangleFunctionName = false;
node->extFunction->alreadyTypeChecked = true;

// Check procedure attributes
if (node->attrs()) {
Expand Down

0 comments on commit 7e9749e

Please sign in to comment.