Skip to main content

golar/
common.rs

1#![allow(non_snake_case)]
2
3use std::iter::FusedIterator;
4use std::marker;
5use std::slice;
6use std::str;
7
8use crate::ast_generated::*;
9use crate::flags_generated::*;
10
11type TypeId = u32;
12
13#[repr(C)]
14#[derive(Debug, Copy, Clone)]
15pub struct TextRange {
16    pub pos: i32,
17    pub end: i32,
18}
19
20#[repr(C)]
21#[derive(Copy, Clone)]
22pub struct GoSlice<T> {
23    pub data: *const T,
24    pub len: isize,
25    pub cap: isize,
26}
27
28#[repr(C)]
29#[derive(Copy, Clone)]
30pub struct GoItab {
31    pub _inter: *const u8,
32    pub _type: *const u8,
33    pub hash: u32,
34}
35
36#[repr(C)]
37#[derive(Copy, Clone)]
38pub struct GoIface {
39    pub itab: *const GoItab,
40    pub data: *const u8,
41}
42
43#[repr(C)]
44#[derive(Copy, Clone)]
45pub struct GoString {
46    pub data: *const u8,
47    pub len: isize,
48}
49
50pub unsafe trait SliceItem<'a, Raw>: Sized {
51    unsafe fn from_raw_unchecked(raw: *const Raw) -> Self;
52}
53
54#[derive(Copy, Clone)]
55pub struct SliceIter<'a, Raw, Wrapped> {
56    data: *const *const Raw,
57    len: usize,
58    index: usize,
59    _marker: marker::PhantomData<(&'a (), Wrapped)>,
60}
61
62impl<'a, Raw, Wrapped> SliceIter<'a, Raw, Wrapped>
63where
64    Wrapped: SliceItem<'a, Raw>,
65{
66    #[inline(always)]
67    pub fn new(raw: GoSlice<*const Raw>) -> Self {
68        Self {
69            data: raw.data,
70            len: raw.len as usize,
71            index: 0,
72            _marker: marker::PhantomData,
73        }
74    }
75}
76
77impl<'a, Raw, Wrapped> Iterator for SliceIter<'a, Raw, Wrapped>
78where
79    Wrapped: SliceItem<'a, Raw>,
80{
81    type Item = Wrapped;
82
83    #[inline(always)]
84    fn next(&mut self) -> Option<Self::Item> {
85        if self.index >= self.len {
86            return None;
87        }
88
89        let raw = unsafe { *self.data.add(self.index) };
90        self.index += 1;
91        Some(unsafe { Wrapped::from_raw_unchecked(raw) })
92    }
93
94    #[inline(always)]
95    fn size_hint(&self) -> (usize, Option<usize>) {
96        let remaining = self.len();
97        (remaining, Some(remaining))
98    }
99}
100
101impl<'a, Raw, Wrapped> ExactSizeIterator for SliceIter<'a, Raw, Wrapped>
102where
103    Wrapped: SliceItem<'a, Raw>,
104{
105    #[inline(always)]
106    fn len(&self) -> usize {
107        self.len - self.index
108    }
109}
110
111impl<'a, Raw, Wrapped> FusedIterator for SliceIter<'a, Raw, Wrapped> where
112    Wrapped: SliceItem<'a, Raw>
113{
114}
115
116#[inline(always)]
117pub fn slice_iter<'a, Raw, Wrapped>(raw: GoSlice<*const Raw>) -> SliceIter<'a, Raw, Wrapped>
118where
119    Wrapped: SliceItem<'a, Raw>,
120{
121    SliceIter::new(raw)
122}
123
124impl GoString {
125    #[inline(always)]
126    pub unsafe fn as_bytes<'a>(&self) -> &'a [u8] {
127        unsafe { slice::from_raw_parts(self.data, self.len as usize) }
128    }
129
130    #[inline(always)]
131    pub unsafe fn as_str<'a>(&self) -> &'a str {
132        unsafe { str::from_utf8_unchecked(self.as_bytes()) }
133    }
134}
135
136#[repr(C)]
137#[derive(Copy, Clone)]
138pub struct RawNode {
139    pub kind: Kind,
140    pub flags: u32,
141    pub loc: TextRange,
142    pub parent: *const RawNode,
143    pub data: GoIface,
144    pub sourceFileId: u32,
145    pub id: u32,
146}
147
148#[repr(C)]
149#[derive(Copy, Clone)]
150pub struct RawNodeList {
151    pub loc: TextRange,
152    pub nodes: GoSlice<*const RawNode>,
153}
154
155#[repr(C)]
156#[derive(Copy, Clone)]
157pub struct RawModifierList {
158    pub nodeList: RawNodeList,
159    pub modifierFlags: ModifierFlags,
160}
161
162#[repr(C)]
163#[derive(Copy, Clone)]
164pub struct RawSymbol {
165    pub flags: SymbolFlags,
166    pub checkFlags: CheckFlags,
167    pub name: GoString,
168    pub declarations: GoSlice<*const RawNode>,
169    pub valueDeclaration: *const RawNode,
170    pub members: *const u8,
171    pub exports: *const u8,
172    pub id: u64,
173    pub parent: *const RawSymbol,
174    pub exportSymbol: *const RawSymbol,
175    pub assignmentDeclarationMembers: *const u8,
176    pub globalExports: *const u8,
177}
178
179#[repr(C)]
180#[derive(Copy, Clone)]
181pub struct RawTypeAlias {
182    pub symbol: *const RawSymbol,
183    pub typeArguments: GoSlice<*const RawType>,
184}
185
186#[repr(C)]
187#[derive(Copy, Clone)]
188pub struct RawType {
189    pub flags: TypeFlags,
190    pub objectFlags: ObjectFlags,
191    pub id: TypeId,
192    pub _pad0: [u8; 4],
193    pub symbol: *const RawSymbol,
194    pub alias: *const RawTypeAlias,
195    pub checker: *const u8,
196    pub data: GoIface,
197}
198
199#[repr(C)]
200#[derive(Copy, Clone)]
201pub struct RawCompositeSignature {
202    pub isUnion: u8,
203    pub _pad0: [u8; 7],
204    pub signatures: GoSlice<*const RawSignature>,
205}
206
207#[repr(C)]
208#[derive(Copy, Clone)]
209pub struct RawCheckerTypePredicate {
210    pub kind: TypePredicateKind,
211    pub parameterIndex: i32,
212    pub parameterName: GoString,
213    pub t: *const RawType,
214}
215
216#[repr(C)]
217#[derive(Copy, Clone)]
218pub struct RawIndexInfo {
219    pub keyType: *const RawType,
220    pub valueType: *const RawType,
221    pub isReadonly: u8,
222    pub _pad0: [u8; 7],
223    pub declaration: *const RawNode,
224    pub indexSymbol: *const RawSymbol,
225    pub components: GoSlice<*const RawNode>,
226}
227
228#[repr(C)]
229#[derive(Copy, Clone)]
230pub struct RawTupleElementInfo {
231    pub flags: ElementFlags,
232    pub _pad0: [u8; 4],
233    pub labeledDeclaration: *const RawNode,
234}
235
236#[repr(C)]
237#[derive(Copy, Clone)]
238pub struct RawConditionalRoot {
239    pub node: *const RawNode,
240    pub checkType: *const RawType,
241    pub extendsType: *const RawType,
242    pub isDistributive: u8,
243    pub _pad0: [u8; 7],
244    pub inferTypeParameters: GoSlice<*const RawType>,
245    pub outerTypeParameters: GoSlice<*const RawType>,
246    pub instantiations: *const u8,
247    pub alias: *const RawTypeAlias,
248}
249
250#[repr(C)]
251#[derive(Copy, Clone)]
252pub struct RawSignature {
253    pub flags: SignatureFlags,
254    pub minArgumentCount: i32,
255    pub resolvedMinArgumentCount: i32,
256    pub _pad0: [u8; 4],
257    pub declaration: *const RawNode,
258    pub typeParameters: GoSlice<*const RawType>,
259    pub parameters: GoSlice<*const RawSymbol>,
260    pub thisParameter: *const RawSymbol,
261    pub resolvedReturnType: *const RawType,
262    pub resolvedTypePredicate: *const RawCheckerTypePredicate,
263    pub target: *const RawSignature,
264    pub mapper: *const u8,
265    pub isolatedSignatureType: *const RawType,
266    pub composite: *const RawCompositeSignature,
267}
268
269pub trait FromNode<'a>: Sized {
270    fn matches(kind: Kind) -> bool;
271    unsafe fn from_node_unchecked(node: Node<'a>) -> Self;
272}
273
274pub trait FromType<'a>: Sized {
275    fn matches(flags: TypeFlags, object_flags: ObjectFlags) -> bool;
276    unsafe fn from_type_unchecked(t: Type<'a>) -> Self;
277}
278
279#[repr(transparent)]
280#[derive(Copy, Clone)]
281pub struct NodeList<'a> {
282    raw: *const RawNodeList,
283    _marker: marker::PhantomData<&'a ()>,
284}
285
286impl<'a> NodeList<'a> {
287    #[inline(always)]
288    pub fn from_raw(raw: *const RawNodeList) -> Self {
289        Self {
290            raw,
291            _marker: marker::PhantomData,
292        }
293    }
294
295    #[inline(always)]
296    pub fn len(self) -> usize {
297        unsafe { (*self.raw).nodes.len as usize }
298    }
299
300    #[inline(always)]
301    pub fn is_empty(self) -> bool {
302        self.len() == 0
303    }
304
305    #[inline(always)]
306    pub fn get(self, idx: usize) -> Option<Node<'a>> {
307        unsafe {
308            if idx < (*self.raw).nodes.len as usize {
309                node_from_raw(*(*self.raw).nodes.data.add(idx))
310            } else {
311                None
312            }
313        }
314    }
315}
316
317#[repr(transparent)]
318#[derive(Copy, Clone)]
319pub struct ModifierList<'a> {
320    raw: *const RawModifierList,
321    _marker: marker::PhantomData<&'a ()>,
322}
323
324impl<'a> ModifierList<'a> {
325    #[inline(always)]
326    pub fn from_raw(raw: *const RawModifierList) -> Self {
327        Self {
328            raw,
329            _marker: marker::PhantomData,
330        }
331    }
332
333    #[inline(always)]
334    pub fn as_node_list(self) -> NodeList<'a> {
335        NodeList::from_raw(self.raw.cast())
336    }
337
338    #[inline(always)]
339    pub fn modifier_flags(self) -> ModifierFlags {
340        unsafe { (*self.raw).modifierFlags }
341    }
342
343    #[inline(always)]
344    pub fn len(self) -> usize {
345        self.as_node_list().len()
346    }
347
348    #[inline(always)]
349    pub fn get(self, idx: usize) -> Option<Node<'a>> {
350        self.as_node_list().get(idx)
351    }
352}
353
354#[repr(transparent)]
355#[derive(Copy, Clone)]
356pub struct Node<'a> {
357    pub(crate) raw: *const RawNode,
358    _marker: marker::PhantomData<&'a ()>,
359}
360
361impl<'a> Node<'a> {
362    #[inline(always)]
363    pub fn from_raw(raw: *const RawNode) -> Self {
364        Self {
365            raw,
366            _marker: marker::PhantomData,
367        }
368    }
369
370    #[inline(always)]
371    pub fn kind(self) -> Kind {
372        unsafe { (*self.raw).kind }
373    }
374
375    #[inline(always)]
376    pub fn pos(self) -> i32 {
377        unsafe { (*self.raw).loc.pos }
378    }
379
380    #[inline(always)]
381    pub fn end(self) -> i32 {
382        unsafe { (*self.raw).loc.end }
383    }
384
385    #[inline(always)]
386    pub fn parent(self) -> Option<Node<'a>> {
387        node_from_raw(unsafe { (*self.raw).parent })
388    }
389
390    #[inline(always)]
391    pub fn source_file_id(self) -> u32 {
392        unsafe { (*self.raw).sourceFileId }
393    }
394
395    #[inline(always)]
396    pub fn id(self) -> u32 {
397        unsafe { (*self.raw).id }
398    }
399
400    #[inline(always)]
401    pub fn cast<T: FromNode<'a>>(self) -> Option<T> {
402        if T::matches(self.kind()) {
403            Some(unsafe { T::from_node_unchecked(self) })
404        } else {
405            None
406        }
407    }
408
409    #[inline(always)]
410    pub fn for_each_child<F>(self, f: &mut F) -> bool
411    where
412        F: FnMut(Node<'a>) -> bool,
413    {
414        for_each_child(f, self)
415    }
416}
417
418#[repr(transparent)]
419#[derive(Copy, Clone)]
420pub struct Symbol<'a> {
421    raw: *const RawSymbol,
422    _marker: marker::PhantomData<&'a ()>,
423}
424
425impl<'a> Symbol<'a> {
426    #[inline(always)]
427    pub fn from_raw(raw: *const RawSymbol) -> Self {
428        Self {
429            raw,
430            _marker: marker::PhantomData,
431        }
432    }
433
434    #[inline(always)]
435    pub fn flags(self) -> SymbolFlags {
436        unsafe { (*self.raw).flags }
437    }
438
439    #[inline(always)]
440    pub fn check_flags(self) -> CheckFlags {
441        unsafe { (*self.raw).checkFlags }
442    }
443
444    #[inline(always)]
445    pub fn name(self) -> &'a str {
446        unsafe { (*self.raw).name.as_str() }
447    }
448
449    #[inline(always)]
450    pub fn declarations(self) -> SliceIter<'a, RawNode, Node<'a>> {
451        slice_iter(unsafe { (*self.raw).declarations })
452    }
453
454    #[inline(always)]
455    pub fn value_declaration(self) -> Option<Node<'a>> {
456        node_from_raw(unsafe { (*self.raw).valueDeclaration })
457    }
458
459    #[inline(always)]
460    pub fn parent(self) -> Option<Symbol<'a>> {
461        symbol_from_raw(unsafe { (*self.raw).parent })
462    }
463
464    #[inline(always)]
465    pub fn export_symbol(self) -> Option<Symbol<'a>> {
466        symbol_from_raw(unsafe { (*self.raw).exportSymbol })
467    }
468}
469
470#[repr(transparent)]
471#[derive(Copy, Clone)]
472pub struct TypeAlias<'a> {
473    raw: *const RawTypeAlias,
474    _marker: marker::PhantomData<&'a ()>,
475}
476
477impl<'a> TypeAlias<'a> {
478    #[inline(always)]
479    pub fn from_raw(raw: *const RawTypeAlias) -> Self {
480        Self {
481            raw,
482            _marker: marker::PhantomData,
483        }
484    }
485
486    #[inline(always)]
487    pub fn symbol(self) -> Option<Symbol<'a>> {
488        symbol_from_raw(unsafe { (*self.raw).symbol })
489    }
490
491    #[inline(always)]
492    pub fn type_arguments(self) -> SliceIter<'a, RawType, Type<'a>> {
493        slice_iter(unsafe { (*self.raw).typeArguments })
494    }
495}
496
497#[repr(transparent)]
498#[derive(Copy, Clone)]
499pub struct Type<'a> {
500    pub(crate) raw: *const RawType,
501    _marker: marker::PhantomData<&'a ()>,
502}
503
504impl<'a> Type<'a> {
505    #[inline(always)]
506    pub fn from_raw(raw: *const RawType) -> Self {
507        Self {
508            raw,
509            _marker: marker::PhantomData,
510        }
511    }
512
513    #[inline(always)]
514    pub fn flags(self) -> TypeFlags {
515        unsafe { (*self.raw).flags }
516    }
517
518    #[inline(always)]
519    pub fn object_flags(self) -> ObjectFlags {
520        unsafe { (*self.raw).objectFlags }
521    }
522
523    #[inline(always)]
524    pub fn id(self) -> TypeId {
525        unsafe { (*self.raw).id }
526    }
527
528    #[inline(always)]
529    pub fn symbol(self) -> Option<Symbol<'a>> {
530        symbol_from_raw(unsafe { (*self.raw).symbol })
531    }
532
533    #[inline(always)]
534    pub fn alias(self) -> Option<TypeAlias<'a>> {
535        type_alias_from_raw(unsafe { (*self.raw).alias })
536    }
537
538    #[inline(always)]
539    pub fn cast<T: FromType<'a>>(self) -> Option<T> {
540        if T::matches(self.flags(), self.object_flags()) {
541            Some(unsafe { T::from_type_unchecked(self) })
542        } else {
543            None
544        }
545    }
546}
547
548#[repr(transparent)]
549#[derive(Copy, Clone)]
550pub struct CompositeSignature<'a> {
551    raw: *const RawCompositeSignature,
552    _marker: marker::PhantomData<&'a ()>,
553}
554
555impl<'a> CompositeSignature<'a> {
556    #[inline(always)]
557    pub fn from_raw(raw: *const RawCompositeSignature) -> Self {
558        Self {
559            raw,
560            _marker: marker::PhantomData,
561        }
562    }
563
564    #[inline(always)]
565    pub fn is_union(self) -> bool {
566        unsafe { (*self.raw).isUnion != 0 }
567    }
568
569    #[inline(always)]
570    pub fn signatures(self) -> SliceIter<'a, RawSignature, Signature<'a>> {
571        slice_iter(unsafe { (*self.raw).signatures })
572    }
573}
574
575#[repr(transparent)]
576#[derive(Copy, Clone)]
577pub struct CheckerTypePredicate<'a> {
578    raw: *const RawCheckerTypePredicate,
579    _marker: marker::PhantomData<&'a ()>,
580}
581
582impl<'a> CheckerTypePredicate<'a> {
583    #[inline(always)]
584    pub fn from_raw(raw: *const RawCheckerTypePredicate) -> Self {
585        Self {
586            raw,
587            _marker: marker::PhantomData,
588        }
589    }
590
591    #[inline(always)]
592    pub fn kind(self) -> TypePredicateKind {
593        unsafe { (*self.raw).kind }
594    }
595
596    #[inline(always)]
597    pub fn parameter_index(self) -> i32 {
598        unsafe { (*self.raw).parameterIndex }
599    }
600
601    #[inline(always)]
602    pub fn parameter_name(self) -> &'a str {
603        unsafe { (*self.raw).parameterName.as_str() }
604    }
605
606    #[inline(always)]
607    pub fn type_(self) -> Option<Type<'a>> {
608        type_from_raw(unsafe { (*self.raw).t })
609    }
610}
611
612#[repr(transparent)]
613#[derive(Copy, Clone)]
614pub struct IndexInfo<'a> {
615    raw: *const RawIndexInfo,
616    _marker: marker::PhantomData<&'a ()>,
617}
618
619impl<'a> IndexInfo<'a> {
620    #[inline(always)]
621    pub fn from_raw(raw: *const RawIndexInfo) -> Self {
622        Self {
623            raw,
624            _marker: marker::PhantomData,
625        }
626    }
627
628    #[inline(always)]
629    pub fn key_type(self) -> Option<Type<'a>> {
630        type_from_raw(unsafe { (*self.raw).keyType })
631    }
632
633    #[inline(always)]
634    pub fn value_type(self) -> Option<Type<'a>> {
635        type_from_raw(unsafe { (*self.raw).valueType })
636    }
637
638    #[inline(always)]
639    pub fn is_readonly(self) -> bool {
640        unsafe { (*self.raw).isReadonly != 0 }
641    }
642
643    #[inline(always)]
644    pub fn declaration(self) -> Option<Node<'a>> {
645        node_from_raw(unsafe { (*self.raw).declaration })
646    }
647
648    #[inline(always)]
649    pub fn index_symbol(self) -> Option<Symbol<'a>> {
650        symbol_from_raw(unsafe { (*self.raw).indexSymbol })
651    }
652
653    #[inline(always)]
654    pub fn components(self) -> SliceIter<'a, RawNode, Node<'a>> {
655        slice_iter(unsafe { (*self.raw).components })
656    }
657}
658
659#[repr(transparent)]
660#[derive(Copy, Clone)]
661pub struct ConditionalRoot<'a> {
662    raw: *const RawConditionalRoot,
663    _marker: marker::PhantomData<&'a ()>,
664}
665
666impl<'a> ConditionalRoot<'a> {
667    #[inline(always)]
668    pub fn from_raw(raw: *const RawConditionalRoot) -> Self {
669        Self {
670            raw,
671            _marker: marker::PhantomData,
672        }
673    }
674
675    #[inline(always)]
676    pub fn node(self) -> Option<Node<'a>> {
677        node_from_raw(unsafe { (*self.raw).node })
678    }
679
680    #[inline(always)]
681    pub fn check_type(self) -> Option<Type<'a>> {
682        type_from_raw(unsafe { (*self.raw).checkType })
683    }
684
685    #[inline(always)]
686    pub fn extends_type(self) -> Option<Type<'a>> {
687        type_from_raw(unsafe { (*self.raw).extendsType })
688    }
689
690    #[inline(always)]
691    pub fn is_distributive(self) -> bool {
692        unsafe { (*self.raw).isDistributive != 0 }
693    }
694
695    #[inline(always)]
696    pub fn infer_type_parameters(self) -> SliceIter<'a, RawType, Type<'a>> {
697        slice_iter(unsafe { (*self.raw).inferTypeParameters })
698    }
699
700    #[inline(always)]
701    pub fn outer_type_parameters(self) -> SliceIter<'a, RawType, Type<'a>> {
702        slice_iter(unsafe { (*self.raw).outerTypeParameters })
703    }
704
705    #[inline(always)]
706    pub fn alias(self) -> Option<TypeAlias<'a>> {
707        type_alias_from_raw(unsafe { (*self.raw).alias })
708    }
709}
710
711#[repr(transparent)]
712#[derive(Copy, Clone)]
713pub struct Signature<'a> {
714    raw: *const RawSignature,
715    _marker: marker::PhantomData<&'a ()>,
716}
717
718impl<'a> Signature<'a> {
719    #[inline(always)]
720    pub fn from_raw(raw: *const RawSignature) -> Self {
721        Self {
722            raw,
723            _marker: marker::PhantomData,
724        }
725    }
726
727    #[inline(always)]
728    pub fn flags(self) -> SignatureFlags {
729        unsafe { (*self.raw).flags }
730    }
731
732    #[inline(always)]
733    pub fn min_argument_count(self) -> i32 {
734        unsafe { (*self.raw).minArgumentCount }
735    }
736
737    #[inline(always)]
738    pub fn resolved_min_argument_count(self) -> i32 {
739        unsafe { (*self.raw).resolvedMinArgumentCount }
740    }
741
742    #[inline(always)]
743    pub fn declaration(self) -> Option<Node<'a>> {
744        node_from_raw(unsafe { (*self.raw).declaration })
745    }
746
747    #[inline(always)]
748    pub fn type_parameters(self) -> SliceIter<'a, RawType, Type<'a>> {
749        slice_iter(unsafe { (*self.raw).typeParameters })
750    }
751
752    #[inline(always)]
753    pub fn parameters(self) -> SliceIter<'a, RawSymbol, Symbol<'a>> {
754        slice_iter(unsafe { (*self.raw).parameters })
755    }
756
757    #[inline(always)]
758    pub fn this_parameter(self) -> Option<Symbol<'a>> {
759        symbol_from_raw(unsafe { (*self.raw).thisParameter })
760    }
761
762    #[inline(always)]
763    pub fn resolved_return_type(self) -> Option<Type<'a>> {
764        type_from_raw(unsafe { (*self.raw).resolvedReturnType })
765    }
766
767    #[inline(always)]
768    pub fn resolved_type_predicate(self) -> Option<CheckerTypePredicate<'a>> {
769        type_predicate_from_raw(unsafe { (*self.raw).resolvedTypePredicate })
770    }
771
772    #[inline(always)]
773    pub fn target(self) -> Option<Signature<'a>> {
774        signature_from_raw(unsafe { (*self.raw).target })
775    }
776
777    #[inline(always)]
778    pub fn has_rest_parameter(self) -> bool {
779        self.flags().contains(SignatureFlags::HAS_REST_PARAMETER)
780    }
781
782    #[inline(always)]
783    pub fn isolated_signature_type(self) -> Option<Type<'a>> {
784        type_from_raw(unsafe { (*self.raw).isolatedSignatureType })
785    }
786
787    #[inline(always)]
788    pub fn composite(self) -> Option<CompositeSignature<'a>> {
789        composite_signature_from_raw(unsafe { (*self.raw).composite })
790    }
791}
792
793unsafe impl<'a> SliceItem<'a, RawNode> for Node<'a> {
794    #[inline(always)]
795    unsafe fn from_raw_unchecked(raw: *const RawNode) -> Self {
796        Self::from_raw(raw)
797    }
798}
799
800unsafe impl<'a> SliceItem<'a, RawNodeList> for NodeList<'a> {
801    #[inline(always)]
802    unsafe fn from_raw_unchecked(raw: *const RawNodeList) -> Self {
803        Self::from_raw(raw)
804    }
805}
806
807unsafe impl<'a> SliceItem<'a, RawModifierList> for ModifierList<'a> {
808    #[inline(always)]
809    unsafe fn from_raw_unchecked(raw: *const RawModifierList) -> Self {
810        Self::from_raw(raw)
811    }
812}
813
814unsafe impl<'a> SliceItem<'a, RawSymbol> for Symbol<'a> {
815    #[inline(always)]
816    unsafe fn from_raw_unchecked(raw: *const RawSymbol) -> Self {
817        Self::from_raw(raw)
818    }
819}
820
821unsafe impl<'a> SliceItem<'a, RawTypeAlias> for TypeAlias<'a> {
822    #[inline(always)]
823    unsafe fn from_raw_unchecked(raw: *const RawTypeAlias) -> Self {
824        Self::from_raw(raw)
825    }
826}
827
828unsafe impl<'a> SliceItem<'a, RawType> for Type<'a> {
829    #[inline(always)]
830    unsafe fn from_raw_unchecked(raw: *const RawType) -> Self {
831        Self::from_raw(raw)
832    }
833}
834
835unsafe impl<'a> SliceItem<'a, RawCompositeSignature> for CompositeSignature<'a> {
836    #[inline(always)]
837    unsafe fn from_raw_unchecked(raw: *const RawCompositeSignature) -> Self {
838        Self::from_raw(raw)
839    }
840}
841
842unsafe impl<'a> SliceItem<'a, RawCheckerTypePredicate> for CheckerTypePredicate<'a> {
843    #[inline(always)]
844    unsafe fn from_raw_unchecked(raw: *const RawCheckerTypePredicate) -> Self {
845        Self::from_raw(raw)
846    }
847}
848
849unsafe impl<'a> SliceItem<'a, RawIndexInfo> for IndexInfo<'a> {
850    #[inline(always)]
851    unsafe fn from_raw_unchecked(raw: *const RawIndexInfo) -> Self {
852        Self::from_raw(raw)
853    }
854}
855
856unsafe impl<'a> SliceItem<'a, RawConditionalRoot> for ConditionalRoot<'a> {
857    #[inline(always)]
858    unsafe fn from_raw_unchecked(raw: *const RawConditionalRoot) -> Self {
859        Self::from_raw(raw)
860    }
861}
862
863unsafe impl<'a> SliceItem<'a, RawSignature> for Signature<'a> {
864    #[inline(always)]
865    unsafe fn from_raw_unchecked(raw: *const RawSignature) -> Self {
866        Self::from_raw(raw)
867    }
868}
869
870#[inline(always)]
871pub fn node_from_raw<'a>(raw: *const RawNode) -> Option<Node<'a>> {
872    if raw.is_null() {
873        None
874    } else {
875        Some(Node::from_raw(raw))
876    }
877}
878
879#[inline(always)]
880pub fn node_list_from_raw<'a>(raw: *const RawNodeList) -> Option<NodeList<'a>> {
881    if raw.is_null() {
882        None
883    } else {
884        Some(NodeList::from_raw(raw))
885    }
886}
887
888#[inline(always)]
889pub fn modifier_list_from_raw<'a>(raw: *const RawModifierList) -> Option<ModifierList<'a>> {
890    if raw.is_null() {
891        None
892    } else {
893        Some(ModifierList::from_raw(raw))
894    }
895}
896
897#[inline(always)]
898pub fn symbol_from_raw<'a>(raw: *const RawSymbol) -> Option<Symbol<'a>> {
899    if raw.is_null() {
900        None
901    } else {
902        Some(Symbol::from_raw(raw))
903    }
904}
905
906#[inline(always)]
907pub fn type_alias_from_raw<'a>(raw: *const RawTypeAlias) -> Option<TypeAlias<'a>> {
908    if raw.is_null() {
909        None
910    } else {
911        Some(TypeAlias::from_raw(raw))
912    }
913}
914
915#[inline(always)]
916pub fn type_from_raw<'a>(raw: *const RawType) -> Option<Type<'a>> {
917    if raw.is_null() {
918        None
919    } else {
920        Some(Type::from_raw(raw))
921    }
922}
923
924#[inline(always)]
925pub fn composite_signature_from_raw<'a>(
926    raw: *const RawCompositeSignature,
927) -> Option<CompositeSignature<'a>> {
928    if raw.is_null() {
929        None
930    } else {
931        Some(CompositeSignature::from_raw(raw))
932    }
933}
934
935#[inline(always)]
936pub fn signature_from_raw<'a>(raw: *const RawSignature) -> Option<Signature<'a>> {
937    if raw.is_null() {
938        None
939    } else {
940        Some(Signature::from_raw(raw))
941    }
942}
943
944#[inline(always)]
945pub fn type_predicate_from_raw<'a>(
946    raw: *const RawCheckerTypePredicate,
947) -> Option<CheckerTypePredicate<'a>> {
948    if raw.is_null() {
949        None
950    } else {
951        Some(CheckerTypePredicate::from_raw(raw))
952    }
953}
954
955#[inline(always)]
956pub fn index_info_from_raw<'a>(raw: *const RawIndexInfo) -> Option<IndexInfo<'a>> {
957    if raw.is_null() {
958        None
959    } else {
960        Some(IndexInfo::from_raw(raw))
961    }
962}
963
964#[inline(always)]
965pub fn conditional_root_from_raw<'a>(
966    raw: *const RawConditionalRoot,
967) -> Option<ConditionalRoot<'a>> {
968    if raw.is_null() {
969        None
970    } else {
971        Some(ConditionalRoot::from_raw(raw))
972    }
973}
974
975#[inline(always)]
976pub fn visit_node<'a, F>(f: &mut F, node: *const RawNode) -> bool
977where
978    F: FnMut(Node<'a>) -> bool,
979{
980    if let Some(node) = node_from_raw(node) {
981        f(node)
982    } else {
983        false
984    }
985}
986
987#[inline(always)]
988pub fn visit_node_list<'a, F>(f: &mut F, node_list: *const RawNodeList) -> bool
989where
990    F: FnMut(Node<'a>) -> bool,
991{
992    if node_list.is_null() {
993        return false;
994    }
995
996    let node_list = NodeList::from_raw(node_list);
997    for idx in 0..node_list.len() {
998        if f(node_list.get(idx).unwrap()) {
999            return true;
1000        }
1001    }
1002
1003    false
1004}