mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Add GlobalInit to blocks to emit initialization instructions for globals (#3687)
`GlobalInit` block is now static block within a `SemIR` which will be used to emit initialization instructions for variables in the `Package` scope. inst_block_stack now has additional methods to handle `GlobalInit` block separately, this block can be popped without being finalized allowing to accumulate between all instances of variables. At the end of the `check` phase, if this block is not empty , the function `__global_init` will be added with this block being inserted into it. This block is pushed to `inst_block_scope` at the end `BindName`, allowing instruction to be emitted into it, then popped at the semicolon (VariableDecl). This significantly changes the `SemIR` output, that's why this commit updates a lot of the test cases.
This commit is contained in:
@@ -502,6 +502,28 @@ auto Context::ParamOrArgEnd(Parse::NodeKind start_kind) -> SemIR::InstBlockId {
|
||||
return ParamOrArgPop();
|
||||
}
|
||||
|
||||
auto Context::FinalizeGlobalInit() -> void {
|
||||
inst_block_stack().PushGlobalInit();
|
||||
if (!inst_block_stack().PeekCurrentBlockContents().empty()) {
|
||||
AddInst({Parse::NodeId::Invalid, SemIR::Return{}});
|
||||
// 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),
|
||||
.enclosing_scope_id = SemIR::NameScopeId::Package,
|
||||
.implicit_param_refs_id = SemIR::InstBlockId::Empty,
|
||||
.param_refs_id = SemIR::InstBlockId::Empty,
|
||||
.return_type_id = SemIR::TypeId::Invalid,
|
||||
.return_slot_id = SemIR::InstId::Invalid,
|
||||
.body_block_ids = {SemIR::InstBlockId::GlobalInit}});
|
||||
} else {
|
||||
inst_block_stack().PopGlobalInit();
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
// Worklist-based type completion mechanism.
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user