Files
carbon-lang/toolchain/check/global_init.cpp
T
Geoff RomerandJon Ross-Perkins 4f816dd03f Remove param_refs and implicit_param_refs (#4479)
This introduces `calling_convention_param_ids`, a single block that
consolidates all the information that was being used by consumers of
`param_refs` and `implicit_param_refs`, in a form that's easier to
produce and typically easier to consume.

See also [this Discord
discussion](https://discord.com/channels/655572317891461132/655578254970716160/1300545448909738125)
regarding the decision to keep the return slot last in the SemIR calling
convention, even though it goes first in the LLVM calling convention.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2024-11-22 18:34:21 +00:00

57 lines
2.1 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"
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();
context_->AddInst<SemIR::Return>(Parse::NodeId::Invalid, {});
// 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::Invalid,
.first_param_node_id = Parse::NodeId::Invalid,
.last_param_node_id = Parse::NodeId::Invalid,
.pattern_block_id = SemIR::InstBlockId::Empty,
.implicit_param_patterns_id = SemIR::InstBlockId::Invalid,
.param_patterns_id = SemIR::InstBlockId::Empty,
.call_params_id = SemIR::InstBlockId::Empty,
.is_extern = false,
.extern_library_id = SemIR::LibraryNameId::Invalid,
.non_owning_decl_id = SemIR::InstId::Invalid,
.first_owning_decl_id = SemIR::InstId::Invalid},
{.return_slot_pattern_id = SemIR::InstId::Invalid,
.return_slot_id = SemIR::InstId::Invalid,
.body_block_ids = {SemIR::InstBlockId::GlobalInit}}}));
}
} // namespace Carbon::Check