mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-15 08:08:31 +00:00
When building an expression referencing the result of name lookup, if the lookup result is non-constant then check we can access it from the current scope. * If it is a global, then use a new `GlobalRef` instruction to get the value of that global. In lowering, create global variables to hold the values of global `let` bindings so that `GlobalRef` can access those values. * If it is a local, walk the lexical scopes between it and the point of use and ensure it is usable through from all of them. Error if there's an intervening declarative scope such as a class, add a TODO to handle captures for intervening functions, and allow `typeof(...)` to name things it can't read the value of since it's unevaluated. Global `let`s (which includes `let`s at class scope) now also have their initializers collected into the global inits block. If the initializer is non-constant, the wrapper binding also lives in the global inits block; otherwise it continues to live in the file block and the variable emitted for it is a global constant. Assisted-by: Gemini and Claude via Antigravity
67 lines
2.5 KiB
C++
67 lines
2.5 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/class.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_pattern_id = SemIR::InstId::None,
|
|
.body_block_ids = {SemIR::InstBlockId::GlobalInit}}}));
|
|
}
|
|
|
|
auto UseGlobalInit(Context& context) -> bool {
|
|
return context.scope_stack().PeekIndex() == ScopeIndex::Package ||
|
|
InStaticClassScopeVar(context) || InClassScopeLet(context);
|
|
}
|
|
|
|
} // namespace Carbon::Check
|