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
2660687739
    pub unsafe fn from_index(shared: &ATermIndex) -> Self {
104
2660687739
        ATermRef {
105
2660687739
            shared: shared.copy(),
106
2660687739
            marker: PhantomData,
107
2660687739
        }
108
2660687739
    }
109
}
110

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

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

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

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

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

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

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

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

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

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

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

            
159
7913880
    fn contains_term(&self, term: &ATermRef<'_>) -> bool {
160
7913880
        term == self
161
7913880
    }
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
964596
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180
964596
        if is_int_term(self) {
181
            write!(f, "{}", Into::<ATermIntRef>::into(self.copy()))?;
182
964596
        } else if is_list_term(self) || is_empty_list_term(self) {
183
            write!(f, "{}", Into::<ATermList<ATerm>>::into(self.copy()))?;
184
964596
        } else if self.arguments().is_empty() {
185
398428
            write!(f, "{}", self.get_head_symbol().name())?;
186
        } else {
187
            // Format the term with its head symbol and arguments, avoiding trailing comma
188
566168
            write!(f, "{:?}(", self.get_head_symbol())?;
189

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

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

            
201
964596
        Ok(())
202
964596
    }
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
1173533
    pub fn with_args<'a, 'b>(
236
1173533
        symbol: &'b impl Symb<'a, 'b>,
237
1173533
        args: &'b [impl Term<'a, 'b>],
238
1173533
    ) -> Return<ATermRef<'static>> {
239
1173533
        THREAD_TERM_POOL.with_borrow(|tp| tp.create_term(symbol, args))
240
1173533
    }
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
195247
    pub fn try_with_iter<'a, 'b, 'c, 'd, I, T>(symbol: &'b impl Symb<'a, 'b>, iter: I) -> Result<ATerm, MercError>
253
195247
    where
254
195247
        I: IntoIterator<Item = Result<T, MercError>>,
255
195247
        T: Term<'c, 'd>,
256
    {
257
195247
        THREAD_TERM_POOL.with_borrow(|tp| tp.try_create_term_iter(symbol, iter))
258
195247
    }
259

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

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

            
278
    /// Constructs a term from the given string.
279
802
    pub fn from_string(text: &str) -> Result<ATerm, MercError> {
280
802
        THREAD_TERM_POOL.with_borrow(|tp| tp.from_string(text))
281
802
    }
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
405085780
    pub fn root(&self) -> ProtectionIndex {
290
405085780
        self.root
291
405085780
    }
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
405085774
    pub(crate) fn from_index(term: &ATermIndex, root: ProtectionIndex) -> ATerm {
310
        unsafe {
311
405085774
            ATerm {
312
405085774
                term: ATermRef::from_index(term),
313
405085774
                root,
314
405085774
                _marker: PhantomData,
315
405085774
            }
316
        }
317
405085774
    }
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
30878832
            fn arg(&self, index: usize) -> ATermRef<'a>;
328
1225686
            fn arguments(&self) -> ATermArgs<'a>;
329
421793012
            fn copy(&self) -> ATermRef<'a>;
330
477824512
            fn get_head_symbol(&self) -> SymbolRef<'a>;
331
3440
            fn iter(&self) -> TermIterator<'a>;
332
778448
            fn index(&self) -> usize;
333
1957849
            fn shared(&self) -> &ATermIndex;
334
1871694
            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
405085774
    fn drop(&mut self) {
359
405085774
        THREAD_TERM_POOL.with_borrow(|tp| tp.drop(self))
360
405085774
    }
361
}
362

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

            
369
impl<'a> Borrow<ATermRef<'a>> for ATerm {
370
3755858
    fn borrow(&self) -> &ATermRef<'a> {
371
3755858
        &self.term
372
3755858
    }
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
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
383
        write!(f, "{:?}", self.copy())
384
    }
385
}
386

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

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

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

            
405
impl Ord for ATerm {
406
159124390
    fn cmp(&self, other: &Self) -> Ordering {
407
159124390
        self.term.cmp(&other.term)
408
159124390
    }
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
2692515
    pub fn new(guard: RecursiveLockReadGuard<'static, GlobalTermPool>, term: T) -> Self {
490
2692515
        Return { term, guard }
491
2692515
    }
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
2692515
    fn deref(&self) -> &Self::Target {
506
2692515
        &self.term
507
2692515
    }
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
35428784
    fn new(term: ATermRef<'a>) -> ATermArgs<'a> {
546
35428784
        let arity = term.get_head_symbol().arity();
547
35428784
        ATermArgs {
548
35428784
            term: Some(term),
549
35428784
            arity,
550
35428784
            index: 0,
551
35428784
        }
552
35428784
    }
553

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

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

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

            
566
65433742
            self.index += 1;
567
65433742
            res
568
        } else {
569
32901418
            None
570
        }
571
98335160
    }
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
45628640
    fn len(&self) -> usize {
589
45628640
        self.arity - self.index
590
45628640
    }
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
7020834
    fn shared(&self) -> &ATermIndex {
657
7020834
        (*self).shared()
658
7020834
    }
659

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