Refactor Compiler.h.

This commit is contained in:
ykiko
2024-12-01 13:48:59 +08:00
parent 6ce2bfdf9d
commit 29fc40de6c
24 changed files with 331 additions and 703 deletions

View File

@@ -3,15 +3,6 @@
namespace clice {
void PCH::apply(Compiler& compiler) const {
bool endAtStart = preamble.ends_with('@');
auto size = preamble.size() - endAtStart;
if(size != 0) {
compiler.applyPCH(path, size, endAtStart);
}
}
struct Tracer {
std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
@@ -33,11 +24,19 @@ async::promise<void> Scheduler::updatePCH(llvm::StringRef filepath,
Tracer tracer;
clang::PreambleBounds bounds = {0, 0};
CompliationParams params;
params.path = filepath;
params.content = content;
params.args = args;
co_await async::schedule_task([&] {
Compiler compiler(filepath, content, args);
bounds = clang::Lexer::ComputePreamble(content, {}, false);
if(bounds.Size != 0) {
compiler.generatePCH(outpath, bounds.Size, bounds.PreambleEndsAtStartOfLine);
auto pch = buildPCH(params);
if(!pch) {
log::fatal("Failed to build PCH for {0}", filepath.str());
return;
}
}
});
@@ -93,16 +92,23 @@ async::promise<void> Scheduler::buildAST(llvm::StringRef filepath, llvm::StringR
Tracer tracer;
log::info("Start building AST for {0}", filepath.str());
auto task = [&path, &content, &args, pch = pchs.at(filepath)] {
CompliationParams params;
params.path = path;
params.content = content;
params.args = args;
// params.addPCH(pchs.at(filepath));
auto task = [&] {
/// FIXME: We cannot use reference capture the `pch` here, beacuse the reference may be
/// Invalid Because other changed the `pchs` map. We also cannot to retrieve the `pch` from
/// the `pchs` map in this task, beacuse it is called in thread pool which will result in
/// data race. So temporarily copy the `pch` here. There must be a better way to solve this
/// problem.
std::unique_ptr<Compiler> compiler = std::make_unique<Compiler>(path, content, args);
pch.apply(*compiler);
compiler->buildAST();
return compiler;
auto info = clice::buildAST(params);
if(!info) {
log::fatal("Failed to build AST for {0}", filepath.str());
}
return std::move(*info);
};
auto compiler = co_await async::schedule_task(std::move(task));