1
use std::borrow::Borrow;
2
use std::cell::UnsafeCell;
3
use std::cmp::Ordering;
4
use std::collections::VecDeque;
5
use std::fmt;
6
use std::hash::Hash;
7
use std::hash::Hasher;
8
use std::marker::PhantomData;
9
use std::ops::Deref;
10
use std::sync::Arc;
11

            
12
use delegate::delegate;
13

            
14
use merc_collections::ProtectionIndex;
15
use merc_sharedmutex::RecursiveLockReadGuard;
16
use merc_unsafety::StablePointer;
17
use merc_utilities::MercError;
18
use merc_utilities::PhantomUnsend;
19

            
20
use crate::ATermIntRef;
21
use crate::ATermList;
22
use crate::Markable;
23
use crate::Symb;
24
use crate::SymbolRef;
25
use crate::is_empty_list_term;
26
use crate::is_int_term;
27
use crate::is_list_term;
28
use crate::storage::GlobalTermPool;
29
use crate::storage::Marker;
30
use crate::storage::SharedTerm;
31
use crate::storage::SharedTermProtection;
32
use crate::storage::THREAD_TERM_POOL;
33

            
34
/// The ATerm trait represents a first-order term in the ATerm library.
35
/// It provides methods to manipulate and access the term's properties.
36
///  
37
/// # Details
38
///
39
/// This trait is rather complicated with two lifetimes, but this is used
40
/// to support both the [ATerm], which has no lifetimes, and [ATermRef<'a>]
41
/// whose lifetime is bound by `'a`. Because now we can be require that `'b: 'a`
42
/// for the implementation of [Term<'a, 'b>] for [ATerm], we can safely return
43
/// [ATermRef<'a>] from methods of [Term<'a, 'b>]. Further explanation can be
44
/// found on the website.
45
pub trait Term<'a, 'b> {
46
    /// Protects the term from garbage collection
47
    fn protect(&self) -> ATerm;
48

            
49
    /// Returns the indexed argument of the term
50
    fn arg(&'b self, index: usize) -> ATermRef<'a>;
51

            
52
    /// Returns the list of arguments as a collection
53
    fn arguments(&'b self) -> ATermArgs<'a>;
54

            
55
    /// Makes a copy of the term with the same lifetime as itself.
56
    fn copy(&'b self) -> ATermRef<'a>;
57

            
58
    /// Returns the function of an ATermRef
59
    fn get_head_symbol(&'b self) -> SymbolRef<'a>;
60

            
61
    /// Returns an iterator over all arguments of the term that runs in pre order traversal of the term trees.
62
    fn iter(&'b self) -> TermIterator<'a>;
63

            
64
    /// Returns a unique index of the term in the term pool
65
    fn index(&self) -> usize;
66

            
67
    /// Returns the shared ptr of the term in the term pool
68
    fn shared(&self) -> &ATermIndex;
69

            
70
    /// Returns the annotation of the term
71
    fn annotation(&self) -> Option<usize>;
72
}
73

            
74
/// Type alias for [ATerm] indices, representing a non-zero index into the term pool.
75
pub type ATermIndex = StablePointer<SharedTerm>;
76

            
77
/// This represents a lifetime bound reference to an existing [ATerm].
78
#[derive(Hash, PartialEq, Eq, PartialOrd, Ord)]
79
pub struct ATermRef<'a> {
80
    shared: ATermIndex,
81
    marker: PhantomData<&'a ()>,
82
}
83

            
84
// /// Check that the ATermRef is the same size as a usize.
85
// #[cfg(not(debug_assertions))]
86
// const _: () = assert!(std::mem::size_of::<ATermRef>() == std::mem::size_of::<usize>());
87

            
88
// /// Since we have NonZero we can use a niche value optimisation for option.
89
// #[cfg(not(debug_assertions))]
90
// const _: () = assert!(std::mem::size_of::<Option<ATermRef>>() == std::mem::size_of::<usize>());
91

            
92
/// These are safe because terms are immutable. Garbage collection is
93
/// always performed with exclusive access, and reference terms have no thread-local state.
94
unsafe impl Send for ATermRef<'_> {}
95
unsafe impl Sync for ATermRef<'_> {}
96

            
97
impl ATermRef<'_> {
98
    /// Creates a new term reference from the given index.
99
    ///
100
    /// # Safety
101
    ///
102
    /// This function is unsafe because it does not check if the index is valid for the given lifetime.
103
2719786240
    pub unsafe fn from_index(shared: &ATermIndex) -> Self {
104
2719786240
        ATermRef {
105
2719786240
            shared: shared.copy(),
106
2719786240
            marker: PhantomData,
107
2719786240
        }
108
2719786240
    }
109
}
110

            
111
impl<'a, 'b> Term<'a, 'b> for ATermRef<'a> {
112
354663226
    fn protect(&self) -> ATerm {
113
354663226
        THREAD_TERM_POOL.with_borrow(|tp| tp.protect(&self.copy()))
114
354663226
    }
115

            
116
397630780
    fn arg(&self, index: usize) -> ATermRef<'a> {
117
397630780
        debug_assert!(
118
397630780
            index < self.get_head_symbol().arity(),
119
            "arg({index}) is not defined for term {self:?}"
120
        );
121

            
122
397630780
        self.shared().arguments()[index].borrow().copy()
123
397630780
    }
124

            
125
35888002
    fn arguments(&self) -> ATermArgs<'a> {
126
35888002
        ATermArgs::new(self.copy())
127
35888002
    }
128

            
129
2082751322
    fn copy(&self) -> ATermRef<'a> {
130
2082751322
        unsafe { ATermRef::from_index(self.shared()) }
131
2082751322
    }
132

            
133
7420232602
    fn get_head_symbol(&'b self) -> SymbolRef<'a> {
134
7420232602
        unsafe { std::mem::transmute::<SymbolRef<'b>, SymbolRef<'a>>(self.shared().symbol().copy()) }
135
7420232602
    }
136

            
137
34400
    fn iter(&self) -> TermIterator<'a> {
138
34400
        TermIterator::new(self.copy())
139
34400
    }
140

            
141
127938650
    fn index(&self) -> usize {
142
127938650
        self.shared.index()
143
127938650
    }
144

            
145
10947715697
    fn shared(&self) -> &ATermIndex {
146
10947715697
        &self.shared
147
10947715697
    }
148

            
149
3706814
    fn annotation(&self) -> Option<usize> {
150
3706814
        self.shared().annotation()
151
3706814
    }
152
}
153

            
154
impl Markable for ATermRef<'_> {
155
73862860
    fn mark(&self, marker: &mut Marker) {
156
73862860
        marker.mark(self);
157
73862860
    }
158

            
159
8089440
    fn contains_term(&self, term: &ATermRef<'_>) -> bool {
160
8089440
        term == self
161
8089440
    }
162

            
163
    fn contains_symbol(&self, symbol: &SymbolRef<'_>) -> bool {
164
        self.get_head_symbol() == *symbol
165
    }
166

            
167
    fn len(&self) -> usize {
168
        1
169
    }
170
}
171

            
172
impl fmt::Display for ATermRef<'_> {
173
104000
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
174
104000
        write!(f, "{self:?}")
175
104000
    }
176
}
177

            
178
impl fmt::Debug for ATermRef<'_> {
179
1358685
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180
1358685
        if is_int_term(self) {
181
            write!(f, "{}", Into::<ATermIntRef>::into(self.copy()))?;
182
1358685
        } else if is_list_term(self) || is_empty_list_term(self) {
183
            write!(f, "{}", Into::<ATermList<ATerm>>::into(self.copy()))?;
184
1358685
        } else if self.arguments().is_empty() {
185
595278
            write!(f, "{}", self.get_head_symbol().name())?;
186
        } else {
187
            // Format the term with its head symbol and arguments, avoiding trailing comma
188
763407
            write!(f, "{:?}(", self.get_head_symbol())?;
189

            
190
763407
            let mut args = self.arguments().peekable();
191
1920932
            while let Some(arg) = args.next() {
192
1157525
                write!(f, "{arg:?}")?;
193
1157525
                if args.peek().is_some() {
194
394118
                    write!(f, ", ")?;
195
763407
                }
196
            }
197

            
198
763407
            write!(f, ")")?;
199
        }
200

            
201
1358685
        Ok(())
202
1358685
    }
203
}
204

            
205
/// The protected version of [ATermRef], mostly derived from it.
206
///
207
/// # Safety
208
///
209
/// Note that terms use thread-local state for their protection mechanism, so
210
/// [ATerm] is not [Send]. Moreover, this means that terms cannot be stored in
211
/// thread-local storage themselves, or at least must be destroyed before the
212
/// thread exists, because the order in which thread-local destructors are
213
/// called is undefined. For this purpose one can use `ManuallyDrop` to simply
214
/// never drop thread local terms, since exiting the thread will clean up the
215
/// protection sets anyway.
216
///
217
/// We do not mark term access as unsafe, since that would make their use
218
/// cumbersome. An alternative would be to required
219
/// THREAD_TERM_POOL.with_borrow(|tp| ...) around every access, but that would
220
/// be very verbose.
221
pub struct ATerm {
222
    term: ATermRef<'static>,
223

            
224
    /// The root of the term in the protection set
225
    root: ProtectionIndex,
226

            
227
    // ATerm is not Send because it uses thread-local state for its protection
228
    // mechanism. However, it can be Sync since terms are immutable, and unlike
229
    // `Rc` cloning results in a local protected copy.
230
    _marker: PhantomUnsend,
231
}
232

            
233
impl ATerm {
234
    /// Creates a new term with the given symbol and arguments.
235
1208797
    pub fn with_args<'a, 'b>(
236
1208797
        symbol: &'b impl Symb<'a, 'b>,
237
1208797
        args: &'b [impl Term<'a, 'b>],
238
1208797
    ) -> Return<ATermRef<'static>> {
239
1208797
        THREAD_TERM_POOL.with_borrow(|tp| tp.create_term(symbol, args))
240
1208797
    }
241

            
242
    /// Creates a new term with the given symbol and an iterator over the arguments.
243
21704
    pub fn with_iter<'a, 'b, 'c, 'd, I, T>(symbol: &'b impl Symb<'a, 'b>, iter: I) -> ATerm
244
21704
    where
245
21704
        I: IntoIterator<Item = T>,
246
21704
        T: Term<'c, 'd>,
247
    {
248
21704
        THREAD_TERM_POOL.with_borrow(|tp| tp.create_term_iter(symbol, iter))
249
21704
    }
250

            
251
    /// Creates a new term with the given symbol and an iterator over the arguments.
252
193859
    pub fn try_with_iter<'a, 'b, 'c, 'd, I, T>(symbol: &'b impl Symb<'a, 'b>, iter: I) -> Result<ATerm, MercError>
253
193859
    where
254
193859
        I: IntoIterator<Item = Result<T, MercError>>,
255
193859
        T: Term<'c, 'd>,
256
    {
257
193859
        THREAD_TERM_POOL.with_borrow(|tp| tp.try_create_term_iter(symbol, iter))
258
193859
    }
259

            
260
    /// Creates a new term with the given symbol and a head term, along with a list of arguments.
261
4319782
    pub fn with_iter_head<'a, 'b, 'c, 'd, 'e, 'f, I, T>(
262
4319782
        symbol: &'b impl Symb<'a, 'b>,
263
4319782
        head: &'d impl Term<'c, 'd>,
264
4319782
        iter: I,
265
4319782
    ) -> ATerm
266
4319782
    where
267
4319782
        I: IntoIterator<Item = T>,
268
4319782
        T: Term<'e, 'f>,
269
    {
270
4319782
        THREAD_TERM_POOL.with_borrow(|tp| tp.create_term_iter_head(symbol, head, iter))
271
4319782
    }
272

            
273
    /// Creates a new term using the pool
274
1869009
    pub fn constant(symbol: &SymbolRef<'_>) -> ATerm {
275
1869009
        THREAD_TERM_POOL.with_borrow(|tp| tp.create_constant(symbol))
276
1869009
    }
277

            
278
    /// Constructs a term from the given string.
279
2842
    pub fn from_string(text: &str) -> Result<ATerm, MercError> {
280
2842
        THREAD_TERM_POOL.with_borrow(|tp| tp.from_string(text))
281
2842
    }
282

            
283
    /// Returns a borrow from the term
284
1000
    pub fn get(&self) -> ATermRef<'_> {
285
1000
        self.term.copy()
286
1000
    }
287

            
288
    /// Returns the root of the term
289
405300952
    pub fn root(&self) -> ProtectionIndex {
290
405300952
        self.root
291
405300952
    }
292

            
293
    /// Replace this term by the given term in place.
294
    pub fn replace<'a, 'b, T>(&mut self, value: Return<T>)
295
    where
296
        T: Term<'a, 'b>,
297
        'b: 'a,
298
    {
299
        // Replace the current term in the protection set by the value.
300
        let index = value.shared().copy();
301
        THREAD_TERM_POOL.with_borrow(|tp| tp.replace(value.guard, self.root, index.copy()));
302

            
303
        // Set the term itself.
304
        self.term = unsafe { ATermRef::from_index(&index) };
305
    }
306

            
307
    /// Creates a new term from the given reference and protection set root
308
    /// entry.
309
405300946
    pub(crate) fn from_index(term: &ATermIndex, root: ProtectionIndex) -> ATerm {
310
        unsafe {
311
405300946
            ATerm {
312
405300946
                term: ATermRef::from_index(term),
313
405300946
                root,
314
405300946
                _marker: PhantomData,
315
405300946
            }
316
        }
317
405300946
    }
318
}
319

            
320
impl<'a, 'b> Term<'a, 'b> for ATerm
321
where
322
    'b: 'a,
323
{
324
    delegate! {
325
        to self.term {
326
3
            fn protect(&self) -> ATerm;
327
30888075
            fn arg(&self, index: usize) -> ATermRef<'a>;
328
1220164
            fn arguments(&self) -> ATermArgs<'a>;
329
422049477
            fn copy(&self) -> ATermRef<'a>;
330
524647236
            fn get_head_symbol(&self) -> SymbolRef<'a>;
331
3440
            fn iter(&self) -> TermIterator<'a>;
332
778448
            fn index(&self) -> usize;
333
2011510
            fn shared(&self) -> &ATermIndex;
334
1854064
            fn annotation(&self) -> Option<usize>;
335
        }
336
    }
337
}
338

            
339
impl Markable for ATerm {
340
    fn mark(&self, marker: &mut Marker) {
341
        marker.mark(&self.term);
342
    }
343

            
344
    fn contains_term(&self, term: &ATermRef<'_>) -> bool {
345
        *term == self.term
346
    }
347

            
348
    fn contains_symbol(&self, symbol: &SymbolRef<'_>) -> bool {
349
        self.get_head_symbol() == *symbol
350
    }
351

            
352
    fn len(&self) -> usize {
353
        1
354
    }
355
}
356

            
357
impl Drop for ATerm {
358
405300946
    fn drop(&mut self) {
359
405300946
        THREAD_TERM_POOL.with_borrow(|tp| tp.drop(self))
360
405300946
    }
361
}
362

            
363
impl Clone for ATerm {
364
318826054
    fn clone(&self) -> Self {
365
318826054
        self.copy().protect()
366
318826054
    }
367
}
368

            
369
impl<'a> Borrow<ATermRef<'a>> for ATerm {
370
3718347
    fn borrow(&self) -> &ATermRef<'a> {
371
3718347
        &self.term
372
3718347
    }
373
}
374

            
375
impl fmt::Display for ATerm {
376
104000
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
377
104000
        write!(f, "{}", self.copy())
378
104000
    }
379
}
380

            
381
impl fmt::Debug for ATerm {
382
97160
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
383
97160
        write!(f, "{:?}", self.copy())
384
97160
    }
385
}
386

            
387
impl Hash for ATerm {
388
28653634
    fn hash<H: Hasher>(&self, state: &mut H) {
389
28653634
        self.term.hash(state)
390
28653634
    }
391
}
392

            
393
impl PartialEq for ATerm {
394
278543266
    fn eq(&self, other: &Self) -> bool {
395
278543266
        self.term.eq(&other.term)
396
278543266
    }
397
}
398

            
399
impl PartialOrd for ATerm {
400
157785870
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
401
157785870
        Some(self.cmp(other))
402
157785870
    }
403
}
404

            
405
impl Ord for ATerm {
406
157785870
    fn cmp(&self, other: &Self) -> Ordering {
407
157785870
        self.term.cmp(&other.term)
408
157785870
    }
409
}
410

            
411
impl Eq for ATerm {}
412

            
413
/// A sendable variant of an `ATerm`.
414
///
415
/// # Details
416
///
417
/// Keeps track of an internal reference to the protection set it was protected from to ensure proper cleanup.
418
pub struct ATermSend {
419
    term: ATermRef<'static>,
420

            
421
    /// The root of the term in the protection set
422
    root: ProtectionIndex,
423

            
424
    /// A shared reference to the protection set that this term was created in.
425
    protection_set: Arc<UnsafeCell<SharedTermProtection>>,
426
}
427

            
428
unsafe impl Send for ATermSend {}
429
unsafe impl Sync for ATermSend {}
430

            
431
impl ATermSend {
432
    /// Takes ownership of an `ATerm` and makes it send.
433
    pub fn from(term: ATerm) -> Self {
434
        // Copy the information from the term, but forget it since we are taking over the `Drop` responsibility.
435
        let root = term.root;
436
        let term_ref: ATermRef<'static> = unsafe { ATermRef::from_index(&term.term.shared) };
437

            
438
        std::mem::forget(term);
439

            
440
        Self {
441
            term: term_ref,
442
            root,
443
            protection_set: THREAD_TERM_POOL.with_borrow(|tp| tp.get_protection_set().clone()),
444
        }
445
    }
446
}
447

            
448
impl Drop for ATermSend {
449
    fn drop(&mut self) {
450
        THREAD_TERM_POOL.with_borrow(|tp| {
451
            let _guard = tp.term_pool().read_recursive().expect("Lock poisoned!");
452

            
453
            unsafe { &mut *self.protection_set.get() }
454
                .protection_set
455
                .unprotect(self.root);
456
        });
457
    }
458
}
459

            
460
impl<'a, 'b> Term<'a, 'b> for ATermSend
461
where
462
    'b: 'a,
463
{
464
    delegate! {
465
        to self.term {
466
            fn protect(&self) -> ATerm;
467
            fn arg(&self, index: usize) -> ATermRef<'a>;
468
            fn arguments(&self) -> ATermArgs<'a>;
469
            fn copy(&self) -> ATermRef<'a>;
470
            fn get_head_symbol(&self) -> SymbolRef<'a>;
471
            fn iter(&self) -> TermIterator<'a>;
472
            fn index(&self) -> usize;
473
            fn shared(&self) -> &ATermIndex;
474
            fn annotation(&self) -> Option<usize>;
475
        }
476
    }
477
}
478

            
479
/// This is a wrapper around a term that indicates it is being returned from a function.
480
///
481
/// The resulting term can have a lifetime tied to the thread-local term pool.
482
pub struct Return<T> {
483
    term: T,
484
    guard: RecursiveLockReadGuard<'static, GlobalTermPool>,
485
}
486

            
487
impl<T> Return<T> {
488
    /// Creates a new return value wrapping the given term.
489
2727779
    pub fn new(guard: RecursiveLockReadGuard<'static, GlobalTermPool>, term: T) -> Self {
490
2727779
        Return { term, guard }
491
2727779
    }
492

            
493
    /// Consumes the return value and returns the inner term.
494
    pub fn into(self) -> T {
495
        self.term
496
    }
497
}
498

            
499
impl<'a, 'b, T> Deref for Return<T>
500
where
501
    T: Term<'a, 'b>,
502
{
503
    type Target = T;
504

            
505
2727779
    fn deref(&self) -> &Self::Target {
506
2727779
        &self.term
507
2727779
    }
508
}
509

            
510
impl<'a, 'b, T: Term<'a, 'b>> Term<'a, 'b> for &'b Return<T>
511
where
512
    'b: 'a,
513
{
514
    delegate! {
515
        to self.term {
516
            fn protect(&self) -> ATerm;
517
            fn arg(&self, index: usize) -> ATermRef<'a>;
518
            fn arguments(&self) -> ATermArgs<'a>;
519
            fn copy(&self) -> ATermRef<'a>;
520
            fn get_head_symbol(&self) -> SymbolRef<'a>;
521
            fn iter(&self) -> TermIterator<'a>;
522
            fn index(&self) -> usize;
523
            fn shared(&self) -> &ATermIndex;
524
            fn annotation(&self) -> Option<usize>;
525
        }
526
    }
527
}
528

            
529
/// An iterator over the arguments of a term.
530
pub struct ATermArgs<'a> {
531
    term: Option<ATermRef<'a>>,
532
    arity: usize,
533
    index: usize,
534
}
535

            
536
impl<'a> ATermArgs<'a> {
537
    pub fn empty() -> ATermArgs<'static> {
538
        ATermArgs {
539
            term: None,
540
            arity: 0,
541
            index: 0,
542
        }
543
    }
544

            
545
35888002
    fn new(term: ATermRef<'a>) -> ATermArgs<'a> {
546
35888002
        let arity = term.get_head_symbol().arity();
547
35888002
        ATermArgs {
548
35888002
            term: Some(term),
549
35888002
            arity,
550
35888002
            index: 0,
551
35888002
        }
552
35888002
    }
553

            
554
1358685
    pub fn is_empty(&self) -> bool {
555
1358685
        self.arity == 0
556
1358685
    }
557
}
558

            
559
impl<'a> Iterator for ATermArgs<'a> {
560
    type Item = ATermRef<'a>;
561

            
562
98482364
    fn next(&mut self) -> Option<Self::Item> {
563
98482364
        if self.index < self.arity {
564
65515817
            let res = Some(self.term.as_ref().unwrap().arg(self.index));
565

            
566
65515817
            self.index += 1;
567
65515817
            res
568
        } else {
569
32966547
            None
570
        }
571
98482364
    }
572
}
573

            
574
impl DoubleEndedIterator for ATermArgs<'_> {
575
985160
    fn next_back(&mut self) -> Option<Self::Item> {
576
985160
        if self.index < self.arity {
577
475380
            let res = Some(self.term.as_ref().unwrap().arg(self.arity - 1));
578

            
579
475380
            self.arity -= 1;
580
475380
            res
581
        } else {
582
509780
            None
583
        }
584
985160
    }
585
}
586

            
587
impl ExactSizeIterator for ATermArgs<'_> {
588
45466200
    fn len(&self) -> usize {
589
45466200
        self.arity - self.index
590
45466200
    }
591
}
592

            
593
/// An iterator over all subterms of the given [ATerm] in preorder traversal, i.e.,
594
/// for f(g(a), b) we visit f(g(a), b), g(a), a, b.
595
pub struct TermIterator<'a> {
596
    queue: VecDeque<ATermRef<'a>>,
597
}
598

            
599
impl TermIterator<'_> {
600
34400
    pub fn new(t: ATermRef) -> TermIterator {
601
34400
        TermIterator {
602
34400
            queue: VecDeque::from([t]),
603
34400
        }
604
34400
    }
605
}
606

            
607
impl<'a> Iterator for TermIterator<'a> {
608
    type Item = ATermRef<'a>;
609

            
610
544180
    fn next(&mut self) -> Option<Self::Item> {
611
544180
        match self.queue.pop_back() {
612
509780
            Some(term) => {
613
                // Put subterms in the queue
614
509780
                for argument in term.arguments().rev() {
615
475380
                    self.queue.push_back(argument);
616
475380
                }
617

            
618
509780
                Some(term)
619
            }
620
34400
            None => None,
621
        }
622
544180
    }
623
}
624

            
625
/// Blanket implementation allowing passing borrowed terms as references.
626
/// TODO: Why is this necessary.
627
impl<'a, 'b, T: Term<'a, 'b>> Term<'a, 'b> for &'b T {
628
    fn protect(&self) -> ATerm {
629
        (*self).protect()
630
    }
631

            
632
    fn arg(&self, index: usize) -> ATermRef<'a> {
633
        (*self).arg(index)
634
    }
635

            
636
    fn arguments(&self) -> ATermArgs<'a> {
637
        (*self).arguments()
638
    }
639

            
640
    fn copy(&self) -> ATermRef<'a> {
641
        (*self).copy()
642
    }
643

            
644
    fn get_head_symbol(&self) -> SymbolRef<'a> {
645
        (*self).get_head_symbol()
646
    }
647

            
648
    fn iter(&self) -> TermIterator<'a> {
649
        (*self).iter()
650
    }
651

            
652
    fn index(&self) -> usize {
653
        (*self).index()
654
    }
655

            
656
7028033
    fn shared(&self) -> &ATermIndex {
657
7028033
        (*self).shared()
658
7028033
    }
659

            
660
    fn annotation(&self) -> Option<usize> {
661
        (*self).annotation()
662
    }
663
}