1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
mod common;
mod convert_value_container;
pub mod get_datex_type;
mod serde_dif;
#[cfg(feature = "ast")]
mod to_datex_expression_data;
mod to_instructions;
use crate::{
prelude::*,
shared_values::{
ExternalSharedContainer, OwnedSharedContainer, PointerAddress,
ReferenceMutability, RemotePointerAddress, SharedContainerInner,
SharedContainerMutability,
base_shared_value_container::{
BaseSharedValueContainer, observers::ObserverData,
},
errors::{SharedValueCreationError, UnexpectedImmutableReferenceError},
traits::{_ExposeRcInternal, SharedContainerCommon},
weak_shared_container::WeakSharedContainer,
},
traits::{
identity::Identity, structural_eq::StructuralEq, value_eq::ValueEq,
},
types::type_definition::TypeDefinition,
utils::impl_display_for_datex_value::impl_display_for_datex_value,
values::value_container::ValueContainer,
};
use alloc::rc::Rc;
use core::{
cell::RefCell,
hash::{Hash, Hasher},
};
/// Wrapper struct for a reference to a shared value (i.e. `'shared X` or `'mut shared X`).
///
/// The inner value can either be a [SharedContainerInner::EndpointOwned] or [SharedContainerInner::External]
#[derive(Debug, Clone)]
pub struct ReferencedSharedContainer {
/// The inner container contains the actual value which can be shared between multiple owners.
/// This can either be a [SharedContainerInner::EndpointOwned] or a [SharedContainerInner::External]
pub(super) inner: Rc<RefCell<SharedContainerInner>>,
/// The mutability of the reference (either `'mut shared X` or `'shared X`)
pub(super) reference_mutability: ReferenceMutability,
pub(super) container_mutability: SharedContainerMutability,
/// Field used internally to indicate that this reference should be treated as a move in the context of the compiler
pub(super) move_indicator: bool, // FIXME must this be omitted on clone?
/// Flag indicating that the inner value is uninitialized. This must get reset to false when the inner value is updated.
/// When set to true, an update to the value is allowed even if the reference is immutable, since the value is not yet initialized.
/// TODO: better way to handle uninitialized values, maybe don't store on top level, but this is more efficient since we don't have to deref the inner value
pub(super) is_uninitialized: bool,
/// Observer data (e.g. observer list) for this shared container. Can be borrowed separately from the [SharedContainerInner]
pub(super) observer_data: Rc<RefCell<ObserverData>>,
}
impl ReferencedSharedContainer {
/// Creates a new mutable [ReferencedSharedContainer] from an existing mutable [Rc<RefCell<SharedContainerInner>>]
///
/// IMPORTANT: this method should only be called after validating that
/// the [SharedContainerMutability] of the inner container is mutable.
pub(crate) fn new_mutable_unchecked(
inner: Rc<RefCell<SharedContainerInner>>,
observer_data: Rc<RefCell<ObserverData>>,
) -> Self {
ReferencedSharedContainer {
inner,
reference_mutability: ReferenceMutability::Mutable,
container_mutability: SharedContainerMutability::Mutable,
move_indicator: false,
is_uninitialized: false,
observer_data,
}
}
/// Creates a new immutable [ReferencedSharedContainer] from an existing mutable or immutable [Rc<RefCell<SharedContainerInner>>]
pub(crate) fn new_immutable(
inner: Rc<RefCell<SharedContainerInner>>,
observer_data: Rc<RefCell<ObserverData>>,
) -> Self {
let container_mutability =
*inner.borrow().base_shared_container().mutability();
ReferencedSharedContainer {
inner,
reference_mutability: ReferenceMutability::Immutable,
container_mutability,
move_indicator: false,
is_uninitialized: false,
observer_data,
}
}
/// Downgrades the [ReferencedSharedContainer] to a [WeakSharedContainer],
/// which can be upgraded back to a strong reference if the inner value is still alive.
pub fn downgrade(&self) -> WeakSharedContainer {
WeakSharedContainer {
inner: Rc::downgrade(&self.inner),
reference_mutability: self.reference_mutability,
container_mutability: self.container_mutability,
move_indicator: self.move_indicator,
observer_data: self.observer_data.clone(),
}
}
/// Tries to create a new immutable [ReferencedSharedContainer] containing a [SharedContainerInner::External]
/// Returns an [Err] if the provided [ReferenceMutability] is [ReferenceMutability::Mutable] while
/// the [SharedContainerMutability] of the container is [SharedContainerMutability::Immutable]
/// # Safety
/// The caller must ensure that the [RemotePointerAddress] does not yet exist in the [SharedReferencesCache]
pub(crate) unsafe fn try_new_remote_from_base_container(
container: BaseSharedValueContainer,
address: RemotePointerAddress,
reference_mutability: ReferenceMutability,
) -> Result<Self, ()> {
let is_uninitialized = container.value_container().is_uninitialized();
let container_mutability = *container.mutability();
// invalid reference mutability
if reference_mutability == ReferenceMutability::Mutable
&& *container.mutability() == SharedContainerMutability::Immutable
{
return Err(());
}
Ok(ReferencedSharedContainer {
inner: Rc::new(RefCell::new(SharedContainerInner::External(
unsafe { ExternalSharedContainer::new(container, address) },
))),
reference_mutability,
container_mutability,
move_indicator: false,
is_uninitialized,
observer_data: Rc::new(RefCell::new(ObserverData::default())),
})
}
/// Creates a new immutable [ReferencedSharedContainer] containing a [SharedContainerInner::External]
/// with the provided [ValueContainer] and [RemotePointerAddress].
/// Automatically infers the allowed type of the shared container from the provided [ValueContainer].
/// # Safety
/// The caller must ensure that the [RemotePointerAddress] does not yet exist in the [SharedReferencesCache]
#[allow(unused)]
pub(crate) unsafe fn new_immutable_external_with_inferred_allowed_type(
value_container: ValueContainer,
address: RemotePointerAddress,
) -> Self {
unsafe {
ReferencedSharedContainer::try_new_remote_from_base_container(
BaseSharedValueContainer::new_with_inferred_allowed_type(
value_container,
SharedContainerMutability::Immutable,
),
address,
ReferenceMutability::Immutable,
)
.unwrap()
}
}
/// Tries to create a new immutable [ReferencedSharedContainer] containing a [SharedContainerInner::External]
/// with the provided [ValueContainer] and [RemotePointerAddress].
///
/// Sets the provided [SharedContainerMutability] and allowed [TypeDefinition].
/// If the allowed [TypeDefinition] is not a superset of the [ValueContainer]'s allowed type,
/// an error is returned
/// # Safety
/// The caller must ensure that the [RemotePointerAddress] does not yet exist in the [SharedReferencesCache]
#[allow(unused)]
pub(crate) unsafe fn try_new_external(
value_container: ValueContainer,
address: RemotePointerAddress,
mutability: SharedContainerMutability,
allowed_type: TypeDefinition,
) -> Result<Self, SharedValueCreationError> {
Ok(unsafe {
ReferencedSharedContainer::try_new_remote_from_base_container(
BaseSharedValueContainer::try_new(
value_container,
allowed_type,
mutability,
)?,
address,
ReferenceMutability::Immutable,
)
.unwrap()
})
}
/// Get the inner [PointerAddress].
pub fn pointer_address(&self) -> PointerAddress {
self.inner().pointer_address()
}
/// Creates a new immutable [ReferencedSharedContainer] pointing to the same inner value as self.
pub fn derive_immutable_reference(&self) -> ReferencedSharedContainer {
ReferencedSharedContainer {
inner: self.inner.clone(),
reference_mutability: ReferenceMutability::Immutable,
container_mutability: self.container_mutability(),
move_indicator: false,
is_uninitialized: false,
observer_data: self.observer_data.clone(),
}
}
/// Tries to create a new mutable [ReferencedSharedContainer] pointing to the same inner value as self.
/// Returns an [Err] if the current reference_mutability is [ReferenceMutability::Immutable]
pub fn try_derive_mutable_reference(
&self,
) -> Result<ReferencedSharedContainer, UnexpectedImmutableReferenceError>
{
match self.reference_mutability {
ReferenceMutability::Immutable => {
Err(UnexpectedImmutableReferenceError)
}
ReferenceMutability::Mutable => Ok(self.clone()),
}
}
/// Returns the [ReferenceMutability] of this reference
pub fn reference_mutability(&self) -> ReferenceMutability {
self.reference_mutability
}
/// Sets the move indicator flag that signals that this should be treated as a move in the context
/// of the compiler
/// Note: this should only be set on containers with an owned address
pub(super) unsafe fn set_move_indicator(&mut self) {
self.move_indicator = true;
}
/// Returns true if the move indicator is set
pub fn treat_as_move(&self) -> bool {
self.move_indicator
}
pub unsafe fn change_address(&self, new_address: PointerAddress) {
assert!(!self.move_indicator);
unsafe { self.inner_mut().change_address(new_address) }
}
pub fn to_string_omit_content(&self) -> String {
format!(
"{}(...)",
match self.reference_mutability {
ReferenceMutability::Immutable => "'",
ReferenceMutability::Mutable => "'mut ",
},
)
}
/// Tries to upgrade this [ReferencedSharedContainer] to an [OwnedSharedContainer].
/// If the move indicator is set, the inner value is moved into a new [OwnedSharedContainer] and returned.
/// If the move indicator is not set, an [Err] is returned with the original [ReferencedSharedContainer].
/// Panics if the move indicator is set but the address is not a [SelfOwnedPointerAddress].
///
/// # Safety
/// The caller must ensure that no other [OwnedSharedContainer] for the same inner value exists
/// if move_indicator is set.
pub unsafe fn try_upgrade_to_owned(
self,
) -> Result<OwnedSharedContainer, Self> {
if self.move_indicator {
Ok(unsafe {
OwnedSharedContainer::new_unchecked(
self.container_mutability,
self.inner.clone(),
self.observer_data.clone(),
)
})
} else {
Err(self)
}
}
/// Marks the shared container as uninitialized.
pub(crate) fn mark_uninitialized(&mut self) {
self.is_uninitialized = true;
}
/// Unmarks the shared container as uninitialized.
pub(crate) fn unset_uninitialized(&mut self) {
self.is_uninitialized = false;
}
pub fn mark_debug(&self, is_debug: bool) {
self.base_shared_container_mut().mark_debug(is_debug);
}
pub fn is_debug(&self) -> bool {
self.base_shared_container().is_debug()
}
}
impl_display_for_datex_value!(
ReferencedSharedContainer,
impl core::fmt::Display for ReferencedSharedContainer {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"{}{}",
match self.reference_mutability {
ReferenceMutability::Immutable => "'",
ReferenceMutability::Mutable => "'mut ",
},
self.inner().base_shared_container(),
)
}
}
);
impl _ExposeRcInternal for ReferencedSharedContainer {
type Shared = SharedContainerInner;
fn get_rc_internal(&self) -> &Rc<RefCell<Self::Shared>> {
&self.inner
}
}
impl Identity for ReferencedSharedContainer {
fn identical(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.inner, &other.inner)
}
}
impl Eq for ReferencedSharedContainer {}
/// PartialEq corresponds to pointer equality / identity for `Reference`.
impl PartialEq for ReferencedSharedContainer {
fn eq(&self, other: &Self) -> bool {
self.identical(other)
}
}
impl Hash for ReferencedSharedContainer {
fn hash<H: Hasher>(&self, state: &mut H) {
let ptr = Rc::as_ptr(self.get_rc_internal());
ptr.hash(state); // hash the address
}
}
impl StructuralEq for ReferencedSharedContainer {
fn structural_eq(&self, other: &Self) -> bool {
self.inner()
.base_shared_container()
.value_container()
.structural_eq(
other.inner().base_shared_container().value_container(),
)
}
}
impl ValueEq for ReferencedSharedContainer {
fn value_eq(&self, other: &Self) -> bool {
self.inner()
.base_shared_container()
.value_container()
.value_eq(other.inner().base_shared_container().value_container())
}
}