Split global init out from InstBlockStack. (#4101)

Creates a `GlobalInit` class for storing relevant values, pulling
functions off `InstBlockStack` and `Context`. Adds a `Context` pointer
just so that it doesn't need to be passed in on each call (`Finalize` in
particular uses several members).

Note we have several different `InstBlockStack` instances, so several
copies of the relevant members were simply unused.
This commit is contained in:
Jon Ross-Perkins
2024-07-03 17:49:47 +00:00
committed by GitHub
parent 5ebcbae2e8
commit cf389bf5d3
9 changed files with 120 additions and 60 deletions
+2 -26
View File
@@ -45,7 +45,8 @@ Context::Context(const Lex::TokenizedBuffer& tokens, DiagnosticEmitter& emitter,
param_and_arg_refs_stack_(sem_ir, vlog_stream, node_stack_),
args_type_info_stack_("args_type_info_stack_", sem_ir, vlog_stream),
decl_name_stack_(this),
scope_stack_(sem_ir_->identifiers()) {
scope_stack_(sem_ir_->identifiers()),
global_init_(this) {
// Map the builtin `<error>` and `type` type constants to their corresponding
// special `TypeId` values.
type_ids_for_type_constants_.Insert(
@@ -632,31 +633,6 @@ auto Context::is_current_position_reachable() -> bool {
SemIR::TerminatorKind::Terminator;
}
auto Context::FinalizeGlobalInit() -> void {
inst_block_stack().PushGlobalInit();
if (!inst_block_stack().PeekCurrentBlockContents().empty()) {
AddInst<SemIR::Return>(Parse::NodeId::Invalid, {});
// Pop the GlobalInit block here to finalize it.
inst_block_stack().Pop();
// __global_init is only added if there are initialization instructions.
auto name_id = sem_ir().identifiers().Add("__global_init");
sem_ir().functions().Add(
{.name_id = SemIR::NameId::ForIdentifier(name_id),
.parent_scope_id = SemIR::NameScopeId::Package,
.decl_id = SemIR::InstId::Invalid,
.generic_id = SemIR::GenericId::Invalid,
.implicit_param_refs_id = SemIR::InstBlockId::Invalid,
.param_refs_id = SemIR::InstBlockId::Empty,
.return_storage_id = SemIR::InstId::Invalid,
.is_extern = false,
.return_slot = SemIR::Function::ReturnSlot::Absent,
.body_block_ids = {SemIR::InstBlockId::GlobalInit}});
} else {
inst_block_stack().PopGlobalInit();
}
}
namespace {
// Worklist-based type completion mechanism.
//