Files
carbon-lang/toolchain/check/global_init.cpp
T
21291b4cc3 Remove InitForm::index (#6817)
This ensures that equal forms always have equal representations (because
the index depends on how the form is used, not on the value of the form
itself).

As a byproduct, also remove `NextCallParamIndex`.

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Bishop <nicholasbishop@google.com>
Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
Co-authored-by: Dana Jansens <danakj@orodu.net>
Co-authored-by: Boaz Brickner <brickner@google.com>
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
Co-authored-by: Carbon Infra Bot <carbon-external-infra@google.com>
Co-authored-by: MK4070 <60286678+MK4070@users.noreply.github.com>
Co-authored-by: Christopher Di Bella <cjdb@google.com>
2026-03-06 17:35:48 +00:00

61 lines
2.3 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/check/global_init.h"
#include "toolchain/check/context.h"
#include "toolchain/check/inst.h"
namespace Carbon::Check {
auto GlobalInit::Resume() -> void {
context_->inst_block_stack().Push(block_id_, block_);
}
auto GlobalInit::Suspend() -> void {
// TODO: Consider splicing together blocks in order to avoid sizable copies
// here.
auto contents = context_->inst_block_stack().PeekCurrentBlockContents();
block_.assign(contents.begin(), contents.end());
block_id_ = context_->inst_block_stack().PeekOrAdd();
context_->inst_block_stack().PopAndDiscard();
}
auto GlobalInit::Finalize() -> void {
// __global_init is only added if there are initialization instructions.
if (block_.empty() && block_id_ == SemIR::InstBlockId::GlobalInit) {
return;
}
Resume();
AddInst<SemIR::Return>(*context_, Parse::NodeId::None, {});
// Pop the GlobalInit block here to finalize it.
context_->inst_block_stack().Pop();
auto name_id = context_->sem_ir().identifiers().Add("__global_init");
context_->sem_ir().set_global_ctor_id(context_->sem_ir().functions().Add(
{{.name_id = SemIR::NameId::ForIdentifier(name_id),
.parent_scope_id = SemIR::NameScopeId::Package,
.generic_id = SemIR::GenericId::None,
.first_param_node_id = Parse::NodeId::None,
.last_param_node_id = Parse::NodeId::None,
.pattern_block_id = SemIR::InstBlockId::Empty,
.implicit_param_patterns_id = SemIR::InstBlockId::None,
.param_patterns_id = SemIR::InstBlockId::Empty,
.is_extern = false,
.extern_library_id = SemIR::LibraryNameId::None,
.non_owning_decl_id = SemIR::InstId::None,
.first_owning_decl_id = SemIR::InstId::None},
{.call_param_patterns_id = SemIR::InstBlockId::Empty,
.call_params_id = SemIR::InstBlockId::Empty,
.call_param_ranges = SemIR::Function::CallParamIndexRanges::Empty,
.return_type_inst_id = SemIR::TypeInstId::None,
.return_form_inst_id = SemIR::InstId::None,
.return_patterns_id = SemIR::InstBlockId::None,
.body_block_ids = {SemIR::InstBlockId::GlobalInit}}}));
}
} // namespace Carbon::Check