Files
carbon-lang/toolchain/check/global_init.h
T
Nicholas Bishop 773ecdfac6 Implement static var class fields (#7215)
This adds the `static` token to the lexer and parses it as a modifier.

In check, `FullPatternStack::Kind::FieldDecl` is now used for both
static and non-static vars. Static vars get treated basically the same
as `NameBindingDecl`s.

Global initialization is used for static var initializers. To make the
necessary stack information available to `pattern_match.cpp`, the
`full_pattern_stack` and `decl_introducer_state_stack` are now popped
later in `handle_let_and_var.cpp`.

In lowering, each class's body is checked for `VarStorage` insts and
lowered the same as global vars.
2026-05-18 20:09:17 +00:00

54 lines
1.7 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
#ifndef CARBON_TOOLCHAIN_CHECK_GLOBAL_INIT_H_
#define CARBON_TOOLCHAIN_CHECK_GLOBAL_INIT_H_
#include "llvm/ADT/SmallVector.h"
#include "toolchain/sem_ir/ids.h"
namespace Carbon::Check {
class Context;
// Tracks state for global initialization. Handles should `Resume` when entering
// an expression that's used for global init, and `Suspend` when the expression
// is finished. Instructions in the middle will be tracked for the
// `__global_init` function.
class GlobalInit {
public:
explicit GlobalInit(Context* context) : context_(context) {}
// Resumes adding instructions to global init.
auto Resume() -> void;
// Suspends adding instructions to global init.
auto Suspend() -> void;
// Finalizes the global initialization state, creating `__global_init` if
// needed. Only called once at the end of checking.
auto Finalize() -> void;
private:
// The associated context. Stored for convenience.
Context* context_;
// The currently suspended global init block. The value may change as a result
// of control flow in initialization.
SemIR::InstBlockId block_id_ = SemIR::InstBlockId::GlobalInit;
// The contents for the currently suspended global init block.
llvm::SmallVector<SemIR::InstId> block_;
};
// Whether to use global initialization for the initializer of a declaration.
//
// This returns true for a declaration in package scope, or for a static
// class field.
auto UseGlobalInit(Context& context) -> bool;
} // namespace Carbon::Check
#endif // CARBON_TOOLCHAIN_CHECK_GLOBAL_INIT_H_