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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
use core::fmt;
use crate::{
Block, BlockRef, EntityListCursor, EntityListCursorMut, EntityMut, EntityRef, Operation,
OperationRef, Spanned,
entity::{EntityProjection, EntityProjectionMut},
};
/// [ProgramPoint] represents a specific location in the execution of a program.
///
/// A program point consists of two parts:
///
/// * An anchor, either a block or operation
/// * A position, i.e. the direction relative to the anchor to which the program point refers
///
/// A program point can be reified as a cursor within a block, such that an operation inserted at
/// the cursor will be placed at the specified position relative to the anchor.
#[derive(Default, Copy, Clone)]
pub enum ProgramPoint {
/// A program point which refers to nothing, and is always invalid if used
#[default]
Invalid,
/// A program point referring to the entry or exit of a block
Block {
/// The block this program point refers to
block: BlockRef,
/// The placement of the cursor relative to `block`
position: Position,
},
/// A program point referring to the entry or exit of an operation
Op {
/// The operation this program point refers to
op: OperationRef,
/// The placement of the cursor relative to `op`
position: Position,
},
}
/// Represents the placement of inserted items relative to a [ProgramPoint]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Position {
/// New items will be inserted before the current program point
Before,
/// New items will be inserted after the current program point
After,
}
impl<T> From<EntityRef<'_, T>> for ProgramPoint
where
for<'a> ProgramPoint: From<&'a T>,
{
#[inline]
fn from(entity: EntityRef<'_, T>) -> Self {
Self::from(&*entity)
}
}
impl<T> From<EntityMut<'_, T>> for ProgramPoint
where
for<'a> ProgramPoint: From<&'a T>,
{
#[inline]
fn from(entity: EntityMut<'_, T>) -> Self {
Self::from(&*entity)
}
}
/// Construct a ProgramPoint referring to the point at entry to `op`
impl From<&Operation> for ProgramPoint {
#[inline]
fn from(op: &Operation) -> Self {
Self::from(op.as_operation_ref())
}
}
/// Construct a ProgramPoint referring to the point at entry to `op`
impl From<OperationRef> for ProgramPoint {
#[inline]
fn from(op: OperationRef) -> Self {
Self::Op {
op,
position: Position::Before,
}
}
}
/// Construct a ProgramPoint referring to the point at entry to `block`
impl From<&Block> for ProgramPoint {
#[inline]
fn from(block: &Block) -> Self {
Self::at_start_of(block.as_block_ref())
}
}
/// Construct a ProgramPoint referring to the point at entry to `block`
impl From<BlockRef> for ProgramPoint {
#[inline]
fn from(block: BlockRef) -> Self {
Self::Block {
block,
position: Position::Before,
}
}
}
#[doc(hidden)]
#[derive(Copy, Clone)]
pub struct BlockPoint {
block: BlockRef,
point: Position,
}
impl From<BlockPoint> for ProgramPoint {
fn from(point: BlockPoint) -> Self {
ProgramPoint::Block {
block: point.block,
position: point.point,
}
}
}
impl From<BlockRef> for BlockPoint {
fn from(block: BlockRef) -> Self {
Self {
block,
point: Position::Before,
}
}
}
impl From<&Block> for BlockPoint {
fn from(block: &Block) -> Self {
Self {
block: block.as_block_ref(),
point: Position::Before,
}
}
}
impl ProgramPoint {
/// Create a [ProgramPoint] at entry to `entity`, i.e. "before"
#[inline]
pub fn before(entity: impl Into<ProgramPoint>) -> Self {
entity.into()
}
/// Create a [ProgramPoint] at exit from `entity`, i.e. "after"
pub fn after(entity: impl Into<ProgramPoint>) -> Self {
let mut pp = entity.into();
match &mut pp {
Self::Invalid => (),
Self::Op {
position: point, ..
}
| Self::Block {
position: point, ..
} => {
*point = Position::After;
}
}
pp
}
/// Create a [ProgramPoint] at entry to `block`, i.e. "before"
pub fn at_start_of(block: impl Into<BlockPoint>) -> Self {
let BlockPoint { block, .. } = block.into();
Self::Block {
block,
position: Position::Before,
}
}
/// Create a [ProgramPoint] at exit from `block`, i.e. "after"
pub fn at_end_of(block: impl Into<BlockPoint>) -> Self {
let BlockPoint { block, .. } = block.into();
Self::Block {
block,
position: Position::After,
}
}
/// Returns true if this program point is at the start of the containing block
pub fn is_at_block_start(&self) -> bool {
self.operation().is_some_and(|op| {
op.parent().is_some() && op.prev().is_none() && self.placement() == Position::Before
}) || matches!(self, Self::Block { position: Position::Before, block, .. } if block.borrow().body().is_empty())
}
/// Returns true if this program point is at the end of the containing block
pub fn is_at_block_end(&self) -> bool {
self.operation().is_some_and(|op| {
op.parent().is_some() && op.next().is_none() && self.placement() == Position::After
}) || matches!(self, Self::Block { position: Position::After, block, .. } if block.borrow().body().is_empty())
}
/// Returns the block of the program point anchor.
///
/// Returns `None`, if the program point is either invalid, or pointing to an orphaned operation
pub fn block(&self) -> Option<BlockRef> {
match self {
Self::Invalid => None,
Self::Block { block, .. } => Some(*block),
Self::Op { op, .. } => op.parent(),
}
}
/// Returns the program point anchor as an operation.
///
/// Returns `None` if the program point is either invalid, or not pointing to a specific op
pub fn operation(&self) -> Option<OperationRef> {
match self {
Self::Invalid => None,
Self::Block {
position: Position::Before,
block,
..
} => block.borrow().body().front().as_pointer(),
Self::Block {
position: Position::After,
block,
..
} => block.borrow().body().back().as_pointer(),
Self::Op { op, .. } => Some(*op),
}
}
/// Returns the operation after [Self::operation], relative to this program point.
///
/// If the current program point is in an orphaned operation, this will return the current op.
///
/// Returns `None` if the program point is either invalid, or not pointing to a specific op
#[track_caller]
pub fn next_operation(&self) -> Option<OperationRef> {
assert!(!self.is_at_block_end());
match self {
Self::Op {
position: Position::After,
op,
..
} if op.parent().is_some() => op.next(),
Self::Op { op, .. } => Some(*op),
Self::Block {
position: Position::Before,
block,
} => block.borrow().front(),
Self::Block { .. } | Self::Invalid => None,
}
}
/// Returns the operation preceding [Self::operation], relative to this program point.
///
/// If the current program point is in an orphaned operation, this will return the current op.
///
/// Returns `None` if the program point is either invalid, or not pointing to a specific op
#[track_caller]
pub fn prev_operation(&self) -> Option<OperationRef> {
assert!(!self.is_at_block_start());
match self {
Self::Op {
position: Position::Before,
op,
..
} if op.parent().is_some() => op.prev(),
Self::Op { op, .. } => Some(*op),
Self::Block {
position: Position::After,
block,
} => block.borrow().back(),
Self::Block { .. } | Self::Invalid => None,
}
}
/// Returns true if this program point refers to a valid program point
#[inline]
pub fn is_valid(&self) -> bool {
!self.is_unset()
}
/// Returns true if this program point is invalid/unset
#[inline]
pub fn is_unset(&self) -> bool {
matches!(self, Self::Invalid)
}
/// The positioning relative to the program point anchor
pub fn placement(&self) -> Position {
match self {
Self::Invalid => Position::After,
Self::Block {
position: point, ..
}
| Self::Op {
position: point, ..
} => *point,
}
}
/// Obtain an immutable cursor in the block corresponding to this program point.
///
/// The resulting cursor can have `as_pointer` or `get` called on it to get the operation to
/// which this point is relative. The intuition around where the cursor is placed for a given
/// program point can be understood as answering the question of "where does the cursor need
/// to be, such that if I inserted an op at that cursor, that the insertion would be placed at
/// the referenced program point (semantically before or after an operation or block). The
/// specific rules are as follows:
///
/// * If "before" a block, the resulting cursor is the null cursor for the containing block,
/// since an insertion at the null cursor will be placed at the start of the block.
/// * If "after" a block, the cursor is placed on the last operation in the block, as insertion
/// will place the inserted op at the end of the block
/// * If "before" an operation, the cursor is placed on the operation immediately preceding
/// `self`, or a null cursor is returned. In both cases, an insertion at the returned cursor
/// would be placed immediately before `self`
/// * If "after" an operation, the cursor is placed on the operation in `self`, so that
/// insertion will place the inserted op immediately after `self`.
///
/// NOTE: The block to which this program point refers will be borrowed for the lifetime of the
/// returned [EntityProjection].
pub fn cursor<'a, 'b: 'a, 'c: 'b>(
&'c self,
) -> Option<EntityProjection<'b, EntityListCursor<'a, Operation>>> {
match self {
Self::Invalid => None,
Self::Block {
block,
position: point,
} => Some(EntityRef::project(block.borrow(), |block| match point {
Position::Before => block.body().front(),
Position::After => block.body().back(),
})),
Self::Op {
op,
position: point,
} => {
let block = op.parent()?;
Some(EntityRef::project(block.borrow(), |block| match point {
Position::Before => {
if let Some(placement) = op.prev() {
unsafe { block.body().cursor_from_ptr(placement) }
} else {
block.body().cursor()
}
}
Position::After => unsafe { block.body().cursor_from_ptr(*op) },
}))
}
}
}
/// Same as [Self::cursor], but obtains a mutable cursor instead.
///
/// NOTE: The block to which this program point refers will be borrowed mutably for the lifetime
/// of the returned [EntityProjectionMut].
pub fn cursor_mut<'a, 'b: 'a, 'c: 'b>(
&'c mut self,
) -> Option<EntityProjectionMut<'b, EntityListCursorMut<'a, Operation>>> {
match self {
Self::Invalid => None,
Self::Block {
block,
position: point,
} => Some(EntityMut::project(block.borrow_mut(), |block| match point {
Position::Before => block.body_mut().cursor_mut(),
Position::After => block.body_mut().back_mut(),
})),
Self::Op {
op,
position: point,
} => {
let mut block = op.parent()?;
Some(EntityMut::project(block.borrow_mut(), |block| match point {
Position::Before => {
if let Some(placement) = op.prev() {
unsafe { block.body_mut().cursor_mut_from_ptr(placement) }
} else {
block.body_mut().cursor_mut()
}
}
Position::After => unsafe { block.body_mut().cursor_mut_from_ptr(*op) },
}))
}
}
}
}
impl Eq for ProgramPoint {}
impl PartialEq for ProgramPoint {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Invalid, Self::Invalid) => true,
(Self::Invalid, _) | (_, Self::Invalid) => false,
(
Self::Block {
block: x,
position: xp,
},
Self::Block {
block: y,
position: yp,
},
) => x == y && xp == yp,
(
Self::Op {
op: x,
position: xp,
..
},
Self::Op {
op: y,
position: yp,
..
},
) => x == y && xp == yp,
(..) => false,
}
}
}
impl core::hash::Hash for ProgramPoint {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
core::mem::discriminant(self).hash(state);
match self {
Self::Invalid => (),
Self::Block {
block,
position: point,
} => {
core::ptr::hash(BlockRef::as_ptr(block), state);
point.hash(state);
}
Self::Op {
op,
position: point,
..
} => {
core::ptr::hash(OperationRef::as_ptr(op), state);
point.hash(state);
}
}
}
}
impl Spanned for ProgramPoint {
fn span(&self) -> crate::SourceSpan {
use crate::SourceSpan;
match self {
Self::Invalid => SourceSpan::UNKNOWN,
Self::Block {
block,
position: point,
} => match point {
Position::Before => {
block.borrow().body().front().get().map(|op| op.span()).unwrap_or_default()
}
Position::After => {
block.borrow().body().back().get().map(|op| op.span()).unwrap_or_default()
}
},
Self::Op { op, .. } => op.borrow().span(),
}
}
}
impl fmt::Display for ProgramPoint {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
use crate::EntityWithId;
match self {
Self::Invalid => f.write_str("<invalid>"),
Self::Block {
block,
position: point,
} => match point {
Position::Before => write!(f, "start({})", &block.borrow().id()),
Position::After => write!(f, "end({})", &block.borrow().id()),
},
Self::Op {
op,
position: point,
} => {
use crate::formatter::{const_text, display};
let block = op
.parent()
.map(|blk| display(blk.borrow().id()))
.unwrap_or_else(|| const_text("null"));
match point {
Position::Before => {
write!(f, "before({} in {block})", &op.borrow().name())
}
Position::After => {
write!(f, "after({} in {block})", &op.borrow().name())
}
}
}
}
}
}
impl fmt::Debug for ProgramPoint {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
use crate::EntityWithId;
match self {
Self::Invalid => f.write_str("Invalid"),
Self::Block {
block,
position: point,
} => f
.debug_struct("Block")
.field("block", &block.borrow().id())
.field("point", point)
.finish(),
Self::Op {
op,
position: point,
} => f
.debug_struct("Op")
.field("block", &op.parent().map(|blk| blk.borrow().id()))
.field("point", point)
.field("op", &op.borrow())
.finish(),
}
}
}