symboltablebuilder/lifecycle.spice
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | // Std imports | ||
| 4 | import "std/data/vector"; | ||
| 5 | |||
| 6 | public type LifecycleState enum { | ||
| 7 | DEAD, // The symbol is not alive yet (e.g. not declared yet) | ||
| 8 | DECLARED, // The symbol is declared but hasn't got a value yet (in debug mode declaration comes with default initialization) | ||
| 9 | INITIALIZED, // The symbol is initialized with a value | ||
| 10 | MOVED // The symbol was moved away (can still be accessed, but does not own its memory anymore) | ||
| 11 | } | ||
| 12 | |||
| 13 | /** | ||
| 14 | * A lifecycle event represents one change of a symbol in time. | ||
| 15 | * It keeps track of the state of the symbol at that point of time. | ||
| 16 | */ | ||
| 17 | public type LifecycleEvent struct { | ||
| 18 | public LifecycleState state | ||
| 19 | } | ||
| 20 | |||
| 21 | /** | ||
| 22 | * A lifecycle represents a collection of timely events that occur for a symbol. | ||
| 23 | * | ||
| 24 | * Usually a lifecycle looks e.g. like this: | ||
| 25 | * - dead | ||
| 26 | * - declared | ||
| 27 | * - initialized | ||
| 28 | * - dead | ||
| 29 | */ | ||
| 30 | 1188865 | public type Lifecycle struct { | |
| 31 | Vector<LifecycleEvent> events | ||
| 32 | } | ||
| 33 | |||
| 34 | /** | ||
| 35 | * Add an event to the lifecycle | ||
| 36 | * | ||
| 37 | * @param event New lifecycle event | ||
| 38 | */ | ||
| 39 | 473348 | public p Lifecycle.addEvent(const LifecycleEvent& event) { | |
| 40 | 473348 | this.events.pushBack(event); | |
| 41 | 473348 | } | |
| 42 | |||
| 43 | /** | ||
| 44 | * Retrieve the current state of a symbol | ||
| 45 | * | ||
| 46 | * @return Current state | ||
| 47 | */ | ||
| 48 | 942294 | public const f<LifecycleState> Lifecycle.getCurrentState() { | |
| 49 | 942294 | return this.events.isEmpty() ? LifecycleState::DEAD : this.events.back().state; | |
| 50 | } | ||
| 51 | |||
| 52 | /** | ||
| 53 | * Retrieve the name of the current state of a symbol | ||
| 54 | * | ||
| 55 | * @return Current state name | ||
| 56 | */ | ||
| 57 | 375876 | public const f<string> Lifecycle.getCurrentStateName() { | |
| 58 | 375876 | switch this.getCurrentState() { | |
| 59 | 250690 | case LifecycleState::DECLARED: { return "declared"; } | |
| 60 | 124213 | case LifecycleState::INITIALIZED: { return "initialized"; } | |
| 61 | 973 | case LifecycleState::MOVED: { return "moved"; } | |
| 62 | ✗ | default: { return "dead"; } | |
| 63 | } | ||
| 64 | ✗ | } | |
| 65 | |||
| 66 | /** | ||
| 67 | * Check if the symbol is declared | ||
| 68 | * | ||
| 69 | * @return Declared or not | ||
| 70 | */ | ||
| 71 | 620 | public const f<bool> Lifecycle.isDeclared() { | |
| 72 | 620 | return this.getCurrentState() == LifecycleState::DECLARED; | |
| 73 | } | ||
| 74 | |||
| 75 | /** | ||
| 76 | * Check if the symbol is initialized | ||
| 77 | * | ||
| 78 | * @return Initialized or not | ||
| 79 | */ | ||
| 80 | 88823 | public const f<bool> Lifecycle.isInitialized() { | |
| 81 | 88823 | return this.getCurrentState() == LifecycleState::INITIALIZED; | |
| 82 | } | ||
| 83 | |||
| 84 | /** | ||
| 85 | * Check if the symbol is in an owning state | ||
| 86 | * | ||
| 87 | * @return Owning state or not | ||
| 88 | */ | ||
| 89 | 620 | public const f<bool> Lifecycle.isInOwningState() { | |
| 90 | 620 | return this.isDeclared() || this.isInitialized(); | |
| 91 | } | ||
| 92 |