Line |
Branch |
Exec |
Source |
1 |
|
|
// Copyright (c) 2021-2025 ChilliBits. All rights reserved. |
2 |
|
|
|
3 |
|
|
#pragma once |
4 |
|
|
|
5 |
|
|
#include <cstdint> |
6 |
|
|
#include <vector> |
7 |
|
|
|
8 |
|
|
namespace spice::compiler { |
9 |
|
|
|
10 |
|
|
// Forward declarations |
11 |
|
|
class ASTNode; |
12 |
|
|
|
13 |
|
|
enum LifecycleState : uint8_t { |
14 |
|
|
DEAD, // The symbol is not alive yet (e.g. not declared yet) |
15 |
|
|
DECLARED, // The symbol is declared but hasn't got a value yet (in debug mode declaration comes with default initialization) |
16 |
|
|
INITIALIZED, // The symbol is initialized with a value |
17 |
|
|
MOVED, // The symbol was moved away (can still be accessed, but does not own its memory anymore) |
18 |
|
|
}; |
19 |
|
|
|
20 |
|
|
/** |
21 |
|
|
* A lifecycle event represents one change of a symbol in time. |
22 |
|
|
* It keeps track of the state of the symbol at that point of time and the event issuer AST node. |
23 |
|
|
*/ |
24 |
|
|
struct LifecycleEvent { |
25 |
|
|
LifecycleState state; |
26 |
|
|
const ASTNode *issuer; |
27 |
|
|
}; |
28 |
|
|
|
29 |
|
|
/** |
30 |
|
|
* A lifecycle represents a collection of timely events that occur for a symbol. |
31 |
|
|
* |
32 |
|
|
* Usually a lifecycle looks e.g. like this: |
33 |
|
|
* f<int |
34 |
|
|
*/ |
35 |
|
|
class Lifecycle { |
36 |
|
|
public: |
37 |
|
|
// Public methods |
38 |
|
|
void addEvent(const LifecycleEvent &event); |
39 |
|
|
[[nodiscard]] LifecycleState getCurrentState() const; |
40 |
|
|
[[nodiscard]] const char *getCurrentStateName() const; |
41 |
|
|
[[nodiscard]] [[maybe_unused]] bool isDead() const; |
42 |
|
|
[[nodiscard]] [[maybe_unused]] bool isDeclared() const; |
43 |
|
|
[[nodiscard]] bool isInitialized() const; |
44 |
|
|
[[nodiscard]] bool wasMoved() const; |
45 |
|
|
[[nodiscard]] bool isInOwningState() const; |
46 |
|
|
|
47 |
|
|
private: |
48 |
|
|
// Private members |
49 |
|
|
std::vector<LifecycleEvent> events; |
50 |
|
|
}; |
51 |
|
|
|
52 |
|
|
} // namespace spice::compiler |
53 |
|
|
|