libbpf_rs/btf/
types.rs

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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
//! Wrappers representing concrete btf types.

use std::ffi::OsStr;
use std::fmt;
use std::fmt::Display;
use std::ops::Deref;

use super::BtfKind;
use super::BtfType;
use super::HasSize;
use super::ReferencesType;
use super::TypeId;

// Generate a btf type that doesn't have any fields, i.e. there is no data after the BtfType
// pointer.
macro_rules! gen_fieldless_concrete_type {
    (
        $(#[$docs:meta])*
        $name:ident $(with $trait:ident)?
    ) => {
        $(#[$docs])*
        #[derive(Clone, Copy, Debug)]
        pub struct $name<'btf> {
            source: BtfType<'btf>,
        }

        impl<'btf> TryFrom<BtfType<'btf>> for $name<'btf> {
            type Error = BtfType<'btf>;

            fn try_from(t: BtfType<'btf>) -> ::core::result::Result<Self, Self::Error> {
                if t.kind() == BtfKind::$name {
                    Ok($name { source: t })
                } else {
                    Err(t)
                }
            }
        }

        impl<'btf> ::std::ops::Deref for $name<'btf> {
            type Target = BtfType<'btf>;
            fn deref(&self) -> &Self::Target {
                &self.source
            }
        }

        $(
            impl super::sealed::Sealed for $name<'_> {}
            unsafe impl<'btf> $trait<'btf> for $name<'btf> {}
        )*
    };
}

// Generate a btf type that has at least one field, and as such, there is data following the
// btf_type pointer.
macro_rules! gen_concrete_type {
    (
        $(#[$docs:meta])*
        $libbpf_ty:ident as $name:ident $(with $trait:ident)?
    ) => {
        $(#[$docs])*
        #[derive(Clone, Copy, Debug)]
        pub struct $name<'btf> {
            source: BtfType<'btf>,
            ptr: &'btf libbpf_sys::$libbpf_ty,
        }

        impl<'btf> TryFrom<BtfType<'btf>> for $name<'btf> {
            type Error = BtfType<'btf>;

            fn try_from(t: BtfType<'btf>) -> ::core::result::Result<Self, Self::Error> {
                if t.kind() == BtfKind::$name {
                    let ptr = unsafe {
                        // SAFETY:
                        //
                        // It's in bounds to access the memory following this btf_type
                        // because we've checked the type
                        (t.ty as *const libbpf_sys::btf_type).offset(1)
                    };
                    let ptr = ptr.cast::<libbpf_sys::$libbpf_ty>();
                    Ok($name {
                        source: t,
                        // SAFETY:
                        //
                        // This pointer is aligned.
                        //      all fields of all struct have size and
                        //      alignment of u32, if t.ty was aligned, then this must be as well
                        //
                        // It's initialized
                        //      libbpf guarantees this since we've checked the type
                        //
                        // The lifetime will match the lifetime of the original t.ty reference.
                        ptr: unsafe { &*ptr },
                    })
                } else {
                    Err(t)
                }
            }
        }

        impl<'btf> ::std::ops::Deref for $name<'btf> {
            type Target = BtfType<'btf>;
            fn deref(&self) -> &Self::Target {
                &self.source
            }
        }

        $(
            impl super::sealed::Sealed for $name<'_> {}
            unsafe impl<'btf> $trait<'btf> for $name<'btf> {}
        )*
    };
}

macro_rules! gen_collection_members_concrete_type {
    (
        $libbpf_ty:ident as $name:ident $(with $trait:ident)?;

        $(#[$docs:meta])*
        struct $member_name:ident $(<$lt:lifetime>)? {
            $(
                $(#[$field_docs:meta])*
                pub $field:ident : $type:ty
            ),* $(,)?
        }

        |$btf:ident, $member:ident $(, $kind_flag:ident)?| $convert:expr
    ) => {
        impl<'btf> ::std::ops::Deref for $name<'btf> {
            type Target = BtfType<'btf>;
            fn deref(&self) -> &Self::Target {
                &self.source
            }
        }

        impl<'btf> $name<'btf> {
            /// Whether this type has no members
            #[inline]
            pub fn is_empty(&self) -> bool {
                self.members.is_empty()
            }

            #[doc = ::core::concat!("How many members this [`", ::core::stringify!($name), "`] has")]
            #[inline]
            pub fn len(&self) -> usize {
                self.members.len()
            }

            #[doc = ::core::concat!("Get a [`", ::core::stringify!($member_name), "`] at a given index")]
            /// # Errors
            ///
            /// This function returns [`None`] when the index is out of bounds.
            pub fn get(&self, index: usize) -> Option<$member_name$(<$lt>)*> {
                self.members.get(index).map(|m| self.c_to_rust_member(m))
            }

            #[doc = ::core::concat!("Returns an iterator over the [`", ::core::stringify!($member_name), "`]'s of the [`", ::core::stringify!($name), "`]")]
            pub fn iter(&'btf self) -> impl ExactSizeIterator<Item = $member_name$(<$lt>)*> + 'btf {
                self.members.iter().map(|m| self.c_to_rust_member(m))
            }

            fn c_to_rust_member(&self, member: &libbpf_sys::$libbpf_ty) -> $member_name$(<$lt>)* {
                let $btf = self.source.source;
                let $member = member;
                $(let $kind_flag = self.source.kind_flag();)*
                $convert
            }
        }

        $(#[$docs])*
        #[derive(Clone, Copy, Debug)]
        pub struct $member_name $(<$lt>)? {
            $(
                $(#[$field_docs])*
                pub $field: $type
            ),*
        }

        $(
            impl $crate::btf::sealed::Sealed for $name<'_> {}
            unsafe impl<'btf> $trait<'btf> for $name<'btf> {}
        )*
    };
}

macro_rules! gen_collection_concrete_type {
    (
        $(#[$docs:meta])*
        $libbpf_ty:ident as $name:ident $(with $trait:ident)?;

        $($rest:tt)+
    ) => {
        $(#[$docs])*
        #[derive(Clone, Copy, Debug)]
        pub struct $name<'btf> {
            source: BtfType<'btf>,
            members: &'btf [libbpf_sys::$libbpf_ty],
        }

        impl<'btf> TryFrom<BtfType<'btf>> for $name<'btf> {
            type Error = BtfType<'btf>;

            fn try_from(t: BtfType<'btf>) -> ::core::result::Result<Self, Self::Error> {
                if t.kind() == BtfKind::$name {
                    let base_ptr = unsafe {
                        // SAFETY:
                        //
                        // It's in bounds to access the memory following this btf_type
                        // because we've checked the type
                        (t.ty as *const libbpf_sys::btf_type).offset(1)
                    };
                    let members = unsafe {
                        // SAFETY:
                        //
                        // This pointer is aligned.
                        //      all fields of all struct have size and
                        //      alignment of u32, if t.ty was aligned, then this must be as well
                        //
                        // It's initialized
                        //      libbpf guarantees this since we've checked the type
                        //
                        // The lifetime will match the lifetime of the original t.ty reference.
                        //
                        // The docs specify the length of the array is stored in vlen.
                        std::slice::from_raw_parts(base_ptr.cast(), t.vlen() as usize)
                    };
                    Ok(Self { source: t, members })
                } else {
                    Err(t)
                }
            }
        }

        gen_collection_members_concrete_type!{
            $libbpf_ty as $name $(with $trait)?;
            $($rest)*
        }
    };
}

/// The attributes of a member.
#[derive(Clone, Copy, Debug)]
pub enum MemberAttr {
    /// Member is a normal field.
    Normal {
        /// The offset of this member in the struct/union.
        offset: u32,
    },
    /// Member is a bitfield.
    BitField {
        /// The size of the bitfield.
        size: u8,
        /// The offset of the bitfield.
        offset: u32,
    },
}

impl MemberAttr {
    #[inline]
    fn new(kflag: bool, offset: u32) -> Self {
        if kflag {
            let size = (offset >> 24) as u8;
            if size != 0 {
                Self::BitField {
                    size,
                    offset: offset & 0x00_ff_ff_ff,
                }
            } else {
                Self::Normal { offset }
            }
        } else {
            Self::Normal { offset }
        }
    }
}

/// The kind of linkage a variable of function can have.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum Linkage {
    /// Static linkage
    Static = 0,
    /// Global linkage
    Global,
    /// External linkage
    Extern,
    /// Unknown
    Unknown,
}

impl From<u32> for Linkage {
    fn from(value: u32) -> Self {
        use Linkage::*;

        match value {
            x if x == Static as u32 => Static,
            x if x == Global as u32 => Global,
            x if x == Extern as u32 => Extern,
            _ => Unknown,
        }
    }
}

impl From<Linkage> for u32 {
    fn from(value: Linkage) -> Self {
        value as u32
    }
}

impl Display for Linkage {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Linkage::Static => "static",
                Linkage::Global => "global",
                Linkage::Extern => "extern",
                Linkage::Unknown => "(unknown)",
            }
        )
    }
}

// Void
gen_fieldless_concrete_type! {
    /// The representation of the c_void type.
    Void
}

// Int

/// An integer.
///
/// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-int)
#[derive(Clone, Copy, Debug)]
pub struct Int<'btf> {
    source: BtfType<'btf>,
    /// The encoding of the number.
    pub encoding: IntEncoding,
    /// The offset in bits where the value of this integer starts. Mostly usefull for bitfields in
    /// structs.
    pub offset: u8,
    /// The number of bits in the int. (For example, an u8 has 8 bits).
    pub bits: u8,
}

/// The kinds of ways a btf [Int] can be encoded.
#[derive(Clone, Copy, Debug)]
pub enum IntEncoding {
    /// No encoding.
    None,
    /// Signed.
    Signed,
    /// It's a c_char.
    Char,
    /// It's a bool.
    Bool,
}

impl<'btf> TryFrom<BtfType<'btf>> for Int<'btf> {
    type Error = BtfType<'btf>;

    fn try_from(t: BtfType<'btf>) -> Result<Self, Self::Error> {
        if t.kind() == BtfKind::Int {
            let int = {
                let base_ptr = t.ty as *const libbpf_sys::btf_type;
                let u32_ptr = unsafe {
                    // SAFETY:
                    //
                    // It's in bounds to access the memory following this btf_type
                    // because we've checked the type
                    base_ptr.offset(1).cast::<u32>()
                };
                unsafe {
                    // SAFETY:
                    //
                    // This pointer is aligned.
                    //      all fields of all struct have size and
                    //      alignment of u32, if t.ty was aligned, then this must be as well
                    //
                    // It's initialized
                    //      libbpf guarantees this since we've checked the type
                    //
                    // The lifetime will match the lifetime of the original t.ty reference.
                    *u32_ptr
                }
            };
            let encoding = match (int & 0x0f_00_00_00) >> 24 {
                0b1 => IntEncoding::Signed,
                0b10 => IntEncoding::Char,
                0b100 => IntEncoding::Bool,
                _ => IntEncoding::None,
            };
            Ok(Self {
                source: t,
                encoding,
                offset: ((int & 0x00_ff_00_00) >> 24) as u8,
                bits: (int & 0x00_00_00_ff) as u8,
            })
        } else {
            Err(t)
        }
    }
}

impl<'btf> Deref for Int<'btf> {
    type Target = BtfType<'btf>;
    fn deref(&self) -> &Self::Target {
        &self.source
    }
}

// SAFETY: Int has the .size field set.
impl super::sealed::Sealed for Int<'_> {}
unsafe impl<'btf> HasSize<'btf> for Int<'btf> {}

// Ptr
gen_fieldless_concrete_type! {
    /// A pointer.
    ///
    /// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-ptr)
    Ptr with ReferencesType
}

// Array
gen_concrete_type! {
    /// An array.
    ///
    /// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-array)
    btf_array as Array
}

impl<'s> Array<'s> {
    /// The type id of the stored type.
    #[inline]
    pub fn ty(&self) -> TypeId {
        self.ptr.type_.into()
    }

    /// The type of index used.
    #[inline]
    pub fn index_ty(&self) -> TypeId {
        self.ptr.index_type.into()
    }

    /// The capacity of the array.
    #[inline]
    pub fn capacity(&self) -> usize {
        self.ptr.nelems as usize
    }

    /// The type contained in this array.
    #[inline]
    pub fn contained_type(&self) -> BtfType<'s> {
        self.source
            .source
            .type_by_id(self.ty())
            .expect("arrays should always reference an existing type")
    }
}

// Struct
gen_collection_concrete_type! {
    /// A struct.
    ///
    /// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-struct)
    btf_member as Struct with HasSize;

    /// A member of a [Struct]
    struct StructMember<'btf> {
        /// The member's name
        pub name: Option<&'btf OsStr>,
        /// The member's type
        pub ty: TypeId,
        /// The attributes of this member.
        pub attr: MemberAttr,
    }

    |btf, member, kflag| StructMember {
        name: btf.name_at(member.name_off),
        ty: member.type_.into(),
        attr: MemberAttr::new(kflag, member.offset),
    }
}

// Union
gen_collection_concrete_type! {
    /// A Union.
    ///
    /// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-union)
    btf_member as Union with HasSize;

    /// A member of an [Union]
    struct UnionMember<'btf> {
        /// The member's name
        pub name: Option<&'btf OsStr>,
        /// The member's type
        pub ty: TypeId,
        /// The attributes of this member.
        pub attr: MemberAttr,
    }

    |btf, member, kflag| UnionMember {
        name: btf.name_at(member.name_off),
        ty: member.type_.into(),
        attr: MemberAttr::new(kflag, member.offset),
    }
}

/// A Composite type, which can be one of a [`Struct`] or a [`Union`].
///
/// Sometimes it's not useful to distinguish them, in that case, one can use this
/// type to inspect any of them.
#[derive(Clone, Copy, Debug)]
pub struct Composite<'btf> {
    source: BtfType<'btf>,
    /// Whether this type is a struct.
    pub is_struct: bool,
    members: &'btf [libbpf_sys::btf_member],
}

impl<'btf> From<Struct<'btf>> for Composite<'btf> {
    fn from(s: Struct<'btf>) -> Self {
        Self {
            source: s.source,
            is_struct: true,
            members: s.members,
        }
    }
}

impl<'btf> From<Union<'btf>> for Composite<'btf> {
    fn from(s: Union<'btf>) -> Self {
        Self {
            source: s.source,
            is_struct: false,
            members: s.members,
        }
    }
}

impl<'btf> TryFrom<BtfType<'btf>> for Composite<'btf> {
    type Error = BtfType<'btf>;

    fn try_from(t: BtfType<'btf>) -> Result<Self, Self::Error> {
        Struct::try_from(t)
            .map(Self::from)
            .or_else(|_| Union::try_from(t).map(Self::from))
    }
}

impl<'btf> TryFrom<Composite<'btf>> for Struct<'btf> {
    type Error = Composite<'btf>;

    fn try_from(value: Composite<'btf>) -> Result<Self, Self::Error> {
        if value.is_struct {
            Ok(Self {
                source: value.source,
                members: value.members,
            })
        } else {
            Err(value)
        }
    }
}

impl<'btf> TryFrom<Composite<'btf>> for Union<'btf> {
    type Error = Composite<'btf>;

    fn try_from(value: Composite<'btf>) -> Result<Self, Self::Error> {
        if !value.is_struct {
            Ok(Self {
                source: value.source,
                members: value.members,
            })
        } else {
            Err(value)
        }
    }
}

impl Composite<'_> {
    /// Returns whether this composite type is a `union {}`.
    pub fn is_empty_union(&self) -> bool {
        !self.is_struct && self.is_empty()
    }
}

// Composite
gen_collection_members_concrete_type! {
    btf_member as Composite with HasSize;

    /// A member of a [Struct]
    struct CompositeMember<'btf> {
        /// The member's name
        pub name: Option<&'btf OsStr>,
        /// The member's type
        pub ty: TypeId,
        /// If this member is a bifield, these are it's attributes.
        pub attr: MemberAttr
    }

    |btf, member, kflag| CompositeMember {
        name: btf.name_at(member.name_off),
        ty: member.type_.into(),
        attr: MemberAttr::new(kflag, member.offset),
    }
}

// Enum
gen_collection_concrete_type! {
    /// An Enum of at most 32 bits.
    ///
    /// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-enum)
    btf_enum as Enum with HasSize;

    /// A member of an [Enum]
    struct EnumMember<'btf> {
        /// The name of this enum variant.
        pub name: Option<&'btf OsStr>,
        /// The numeric value of this enum variant.
        pub value: i32,
    }

    |btf, member| EnumMember {
        name: btf.name_at(member.name_off),
        value: member.val,
    }
}

// Fwd
gen_fieldless_concrete_type! {
    /// A forward declared C type.
    ///
    /// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-fwd)
    Fwd
}

impl Fwd<'_> {
    /// The kind of C type that is forwardly declared.
    pub fn kind(&self) -> FwdKind {
        if self.source.kind_flag() {
            FwdKind::Union
        } else {
            FwdKind::Struct
        }
    }
}

/// The kinds of types that can be forward declared.
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
pub enum FwdKind {
    /// A struct.
    Struct,
    /// A union.
    Union,
}

// Typedef
gen_fieldless_concrete_type! {
    /// A C typedef.
    ///
    /// References the original type.
    ///
    /// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-typedef)
    Typedef with ReferencesType
}

// Volatile
gen_fieldless_concrete_type! {
    /// The volatile modifier.
    ///
    /// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-volatile)
    Volatile with ReferencesType
}

// Const
gen_fieldless_concrete_type! {
    /// The const modifier.
    ///
    /// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-const)
    Const with ReferencesType
}

// Restrict
gen_fieldless_concrete_type! {
    /// The restrict modifier.
    ///
    /// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-restrict)
    Restrict with ReferencesType
}

// Func
gen_fieldless_concrete_type! {
    /// A function.
    ///
    /// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-func)
    Func with ReferencesType
}

impl Func<'_> {
    /// This function's linkage.
    #[inline]
    pub fn linkage(&self) -> Linkage {
        self.source.vlen().into()
    }
}

// FuncProto
gen_collection_concrete_type! {
    /// A function prototype.
    ///
    /// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-func-proto)
    btf_param as FuncProto with ReferencesType;

    /// A parameter of a [FuncProto].
    struct FuncProtoParam<'btf> {
        /// The parameter's name
        pub name: Option<&'btf OsStr>,
        /// The parameter's type
        pub ty: TypeId,
    }

    |btf, member| FuncProtoParam {
        name: btf.name_at(member.name_off),
        ty: member.type_.into()
    }
}

// Var
gen_concrete_type! {
    /// A global variable.
    ///
    /// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-var)
    btf_var as Var with ReferencesType
}

impl Var<'_> {
    /// The kind of linkage this variable has.
    #[inline]
    pub fn linkage(&self) -> Linkage {
        self.ptr.linkage.into()
    }
}

// DataSec
gen_collection_concrete_type! {
    /// An ELF's data section, such as `.data`, `.bss` or `.rodata`.
    ///
    /// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-datasec)
    btf_var_secinfo as DataSec with HasSize;

    /// Describes the btf var in a section.
    ///
    /// See [`DataSec`].
    struct VarSecInfo {
        /// The type id of the var
        pub ty: TypeId,
        /// The offset in the section
        pub offset: u32,
        /// The size of the type.
        pub size: usize,
    }

    |_btf, member| VarSecInfo {
        ty: member.type_.into(),
        offset: member.offset,
        size: member.size as usize
    }
}

// Float
gen_fieldless_concrete_type! {
    /// A floating point number.
    ///
    /// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-float)
    Float with HasSize
}

// DeclTag
gen_concrete_type! {
    /// A declaration tag.
    ///
    /// A custom tag the programmer can attach to a symbol.
    ///
    /// See the [clang docs](https://clang.llvm.org/docs/AttributeReference.html#btf-decl-tag) on
    /// it.
    /// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-decl-tag)
    btf_decl_tag as DeclTag with ReferencesType
}

impl DeclTag<'_> {
    /// The component index is present only when the tag points to a struct/union member or a
    /// function argument.
    /// And component_idx indicates which member or argument, this decl tag refers to.
    #[inline]
    pub fn component_index(&self) -> Option<u32> {
        self.ptr.component_idx.try_into().ok()
    }
}

// TypeTag
gen_fieldless_concrete_type! {
    /// A type tag.
    ///
    /// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-type-tag)
    TypeTag with ReferencesType
}

// Enum64
gen_collection_concrete_type! {
    /// An Enum of 64 bits.
    ///
    /// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-enum64)
    btf_enum64 as Enum64 with HasSize;

    /// A member of an [Enum64].
    struct Enum64Member<'btf> {
        /// The name of this enum variant.
        pub name: Option<&'btf OsStr>,
        /// The numeric value of this enum variant.
        pub value: u64,
    }

    |btf, member| Enum64Member {
        name: btf.name_at(member.name_off),
        value: {
            let hi: u64 = member.val_hi32.into();
            let lo: u64 = member.val_lo32.into();
            hi << 32 | lo
        },
    }
}

/// A macro that allows matching on the type of a [`BtfType`] as if it was an enum.
///
/// Each pattern can be of two types.
///
/// ```no_run
/// use libbpf_rs::btf::BtfType;
/// use libbpf_rs::btf_type_match;
///
/// # fn do_something_with_an_int(i: libbpf_rs::btf::types::Int) -> &'static str { "" }
/// let ty: BtfType;
/// # ty = todo!();
/// btf_type_match!(match ty {
///     BtfKind::Int(i) => do_something_with_an_int(i),
///     BtfKind::Struct => "it's a struct",
///     BtfKind::Union => {
///         "it's a union"
///     },
///     _ => "default",
/// });
/// ```
///
/// Variable Binding.
///
/// ```compile_fail
///     BtfKind::Int(i) => {
///         // we can use i here and it will be an `Int`
///     }
/// ```
///
/// NonBinding.
///
/// ```compile_fail
///     BtfKind::Int => {
///         // we don't have access to the variable, but we know the scrutinee is an Int
///     }
/// ```
///
/// Multiple Variants
/// ```compile_fail
///     BtfKind::Struct | BtfKind::Union => {
///         // we don't have access to the variable,
///         // but we know the scrutinee is either a Struct or a Union
///     }
/// ```
///
/// Special case for [`Struct`] and [`Union`]: [`Composite`]
/// ```compile_fail
///     BtfKind::Composite(c) => {
///         // we can use `c` as an instance of `Composite`.
///         // this branch will match if the type is either a Struct or a Union.
///     }
/// ```
// $(BtfKind::$name:ident $(($var:ident))? => $action:expr $(,)?)+
#[macro_export]
macro_rules! btf_type_match {
    // base rule
    (
        match $ty:ident {
            $($pattern:tt)+
        }
    ) => {{
        let ty: $crate::btf::BtfType<'_> = $ty;
        $crate::__btf_type_match!(match ty.kind() { } $($pattern)*)
    }};
}

#[doc(hidden)]
#[macro_export]
macro_rules! __btf_type_match {
    /*
     * Composite special case
     *
     * This is similar to simple-match but it's hardcoded for composite which matches both structs
     * and unions.
     */
    (
        match $ty:ident.kind() { $($p:pat => $a:expr),* }
        BtfKind::Composite $( ($var:ident) )? => $action:expr,
        $($rest:tt)*
    ) => {
        $crate::__btf_type_match!(match $ty.kind() { $($p => $a,)* }
            BtfKind::Composite $( ($var) )* => { $action }
            $($rest)*
        )
    };
    (
        match $ty:ident.kind() { $($p:pat => $a:expr),* }
        BtfKind::Composite $(($var:ident))? => $action:block
        $($rest:tt)*
    ) => {
        $crate::__btf_type_match!(match $ty.kind() {
            $($p => $a,)*
            $crate::btf::BtfKind::Struct | $crate::btf::BtfKind::Union => {
                $(let $var = $crate::btf::types::Composite::try_from($ty).unwrap();)*
                $action
            }
        }
             $($rest)*
        )
    };
    // simple-match: match on simple patterns that use an expression followed by a comma
    (
        match $ty:ident.kind() { $($p:pat => $a:expr),* }
        BtfKind::$name:ident $(($var:ident))? => $action:expr,
        $($rest:tt)*
    ) => {
        $crate::__btf_type_match!(
            match $ty.kind() { $($p => $a),* }
            BtfKind::$name $(($var))? => { $action }
            $($rest)*
        )
    };
    // simple-match: match on simple patterns that use a block without a comma
    (
        match $ty:ident.kind() { $($p:pat => $a:expr),* }
        BtfKind::$name:ident $(($var:ident))? => $action:block
        $($rest:tt)*
    ) => {
        $crate::__btf_type_match!(match $ty.kind() {
            $($p => $a,)*
            $crate::btf::BtfKind::$name => {
                $(let $var = $crate::btf::types::$name::try_from($ty).unwrap();)*
                $action
            }
        }
             $($rest)*
        )
    };
    // or-pattern: match on one or more variants without capturing a variable and using an
    //             expression followed by a comma.
    (
        match $ty:ident.kind() { $($p:pat => $a:expr),* }
        $(BtfKind::$name:ident)|+  => $action:expr,
        $($rest:tt)*
    ) => {
        $crate::__btf_type_match!(
            match $ty.kind() { $($p => $a),* }
            $(BtfKind::$name)|* => { $action }
            $($rest)*
        )
    };
    (
        match $ty:ident.kind() { $($p:pat => $a:expr),* }
        $(BtfKind::$name:ident)|+  => $action:block
        $($rest:tt)*
    ) => {
        $crate::__btf_type_match!(match $ty.kind() {
            $($p => $a,)*
            $($crate::btf::BtfKind::$name)|* => {
                $action
            }
        }
             $($rest)*
        )
    };
    // default match case
    //
    // we only need the expression case here because this case is not followed by a $rest:tt like
    // the others, which let's us use the $(,)? pattern.
    (
        match $ty:ident.kind() { $($p:pat => $a:expr),* }
        _ => $action:expr $(,)?
    ) => {
        $crate::__btf_type_match!(match $ty.kind() {
            $($p => $a,)*
            _ => { $action }
        }

        )
    };
    // stop case, where the code is actually generated
    (match $ty:ident.kind() { $($p:pat => $a:expr),*  } ) => {
        match $ty.kind() {
            $($p => $a),*
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    // creates a dummy btftype, not it's not safe to use this type, but it is safe to match on it,
    // which is all we need for these tests.
    macro_rules! dummy_type {
        ($ty:ident) => {
            let btf = $crate::Btf {
                ptr: std::ptr::NonNull::dangling(),
                drop_policy: $crate::btf::DropPolicy::Nothing,
                _marker: std::marker::PhantomData,
            };
            let $ty = BtfType {
                type_id: $crate::btf::TypeId::from(1),
                name: None,
                source: &btf,
                ty: &libbpf_sys::btf_type::default(),
            };
        };
    }

    fn foo(_: super::Int<'_>) -> &'static str {
        "int"
    }

    #[test]
    fn full_switch_case() {
        dummy_type!(ty);
        btf_type_match!(match ty {
            BtfKind::Int(i) => foo(i),
            BtfKind::Struct => "it's a struct",
            BtfKind::Void => "",
            BtfKind::Ptr => "",
            BtfKind::Array => "",
            BtfKind::Union => "",
            BtfKind::Enum => "",
            BtfKind::Fwd => "",
            BtfKind::Typedef => "",
            BtfKind::Volatile => "",
            BtfKind::Const => "",
            BtfKind::Restrict => "",
            BtfKind::Func => "",
            BtfKind::FuncProto => "",
            BtfKind::Var => "",
            BtfKind::DataSec => "",
            BtfKind::Float => "",
            BtfKind::DeclTag => "",
            BtfKind::TypeTag => "",
            BtfKind::Enum64 => "",
        });
    }

    #[test]
    fn partial_match() {
        dummy_type!(ty);
        btf_type_match!(match ty {
            BtfKind::Int => "int",
            _ => "default",
        });
    }

    #[test]
    fn or_pattern_match() {
        dummy_type!(ty);
        // we ask rustfmt to not format this block so that we can keep the trailing `,` in the
        // const | restrict branch.
        #[rustfmt::skip]
        btf_type_match!(match ty {
            BtfKind::Int => "int",
            BtfKind::Struct | BtfKind::Union => "composite",
            BtfKind::Typedef | BtfKind::Volatile => {
                "qualifier"
            }
            BtfKind::Const | BtfKind::Restrict => {
                "const or restrict"
            },
            _ => "default",
        });
    }

    #[test]
    fn match_arm_with_brackets() {
        dummy_type!(ty);
        // we ask rustfmt to not format this block so that we can keep the trailing `,` in the int
        // branch.
        #[rustfmt::skip]
        btf_type_match!(match ty {
            BtfKind::Void => {
                "void"
            }
            BtfKind::Int => {
                "int"
            },
            BtfKind::Struct => "struct",
            _ => "default",
        });
    }

    #[test]
    fn match_on_composite() {
        dummy_type!(ty);
        btf_type_match!(match ty {
            BtfKind::Composite(c) => c.is_struct,
            _ => false,
        });
        btf_type_match!(match ty {
            BtfKind::Composite(c) => {
                c.is_struct
            }
            _ => false,
        });
        // we ask rustfmt to not format this block so that we can keep the trailing `,` in the
        // composite branch.
        #[rustfmt::skip]
        btf_type_match!(match ty {
            BtfKind::Composite(c) => {
                c.is_struct
            },
            _ => false,
        });
    }

    #[test]
    fn match_arm_with_multiple_statements() {
        dummy_type!(ty);

        btf_type_match!(match ty {
            BtfKind::Int(i) => {
                let _ = i;
                "int"
            }
            _ => {
                let _ = 1;
                "default"
            }
        });
    }

    #[test]
    fn non_expression_guards() {
        dummy_type!(ty);

        btf_type_match!(match ty {
            BtfKind::Int => {
                let _ = 1;
                "int"
            }
            BtfKind::Typedef | BtfKind::Const => {
                let _ = 1;
                "qualifier"
            }
            _ => {
                let _ = 1;
                "default"
            }
        });

        btf_type_match!(match ty {
            BtfKind::Int => {
                let _ = 1;
            }
            BtfKind::Typedef | BtfKind::Const => {
                let _ = 1;
            }
            _ => {
                let _ = 1;
            }
        });
    }

    #[test]
    fn linkage_type() {
        use std::mem::discriminant;
        use Linkage::*;

        for t in [Static, Global, Extern, Unknown] {
            // check if discriminants match after a roundtrip conversion
            assert_eq!(discriminant(&t), discriminant(&Linkage::from(t as u32)));
        }
    }
}