global_init.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TOOLCHAIN_CHECK_GLOBAL_INIT_H_
  5. #define CARBON_TOOLCHAIN_CHECK_GLOBAL_INIT_H_
  6. #include "llvm/ADT/SmallVector.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. namespace Carbon::Check {
  9. class Context;
  10. // Tracks state for global initialization. Handles should `Resume` when entering
  11. // an expression that's used for global init, and `Suspend` when the expression
  12. // is finished. Instructions in the middle will be tracked for the
  13. // `__global_init` function.
  14. class GlobalInit {
  15. public:
  16. explicit GlobalInit(Context* context) : context_(context) {}
  17. // Resumes adding instructions to global init.
  18. auto Resume() -> void;
  19. // Suspends adding instructions to global init.
  20. auto Suspend() -> void;
  21. // Finalizes the global initialization state, creating `__global_init` if
  22. // needed. Only called once at the end of checking.
  23. auto Finalize() -> void;
  24. private:
  25. // The associated context. Stored for convenience.
  26. Context* context_;
  27. // The currently suspended global init block. The value may change as a result
  28. // of control flow in initialization.
  29. SemIR::InstBlockId block_id_ = SemIR::InstBlockId::GlobalInit;
  30. // The contents for the currently suspended global init block.
  31. llvm::SmallVector<SemIR::InstId> block_;
  32. };
  33. } // namespace Carbon::Check
  34. #endif // CARBON_TOOLCHAIN_CHECK_GLOBAL_INIT_H_