inkwell/
comdat.rs

1//! A `Comdat` helps resolve linker errors for duplicate sections.
2// https://llvm.org/doxygen/IR_2Comdat_8h_source.html
3// https://stackoverflow.com/questions/1834597/what-is-the-comdat-section-used-for
4
5use llvm_sys::comdat::{LLVMComdatSelectionKind, LLVMGetComdatSelectionKind, LLVMSetComdatSelectionKind};
6use llvm_sys::prelude::LLVMComdatRef;
7
8#[llvm_enum(LLVMComdatSelectionKind)]
9#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
10/// Determines how linker conflicts are to be resolved.
11pub enum ComdatSelectionKind {
12    /// The linker may choose any COMDAT.
13    #[llvm_variant(LLVMAnyComdatSelectionKind)]
14    Any,
15    /// The data referenced by the COMDAT must be the same.
16    #[llvm_variant(LLVMExactMatchComdatSelectionKind)]
17    ExactMatch,
18    /// The linker will choose the largest COMDAT.
19    #[llvm_variant(LLVMLargestComdatSelectionKind)]
20    Largest,
21    /// No other Module may specify this COMDAT.
22    #[llvm_variant(LLVMNoDuplicatesComdatSelectionKind)]
23    NoDuplicates,
24    /// The data referenced by the COMDAT must be the same size.
25    #[llvm_variant(LLVMSameSizeComdatSelectionKind)]
26    SameSize,
27}
28
29/// A `Comdat` determines how to resolve duplicate sections when linking.
30#[derive(Debug, PartialEq, Eq, Copy, Clone)]
31pub struct Comdat(pub(crate) LLVMComdatRef);
32
33impl Comdat {
34    /// Creates a new `Comdat` type from a raw pointer.
35    pub unsafe fn new(comdat: LLVMComdatRef) -> Self {
36        debug_assert!(!comdat.is_null());
37
38        Comdat(comdat)
39    }
40
41    /// Acquires the underlying raw pointer belonging to this `Comdat` type.
42    pub fn as_mut_ptr(&self) -> LLVMComdatRef {
43        self.0
44    }
45
46    /// Gets what kind of `Comdat` this is.
47    pub fn get_selection_kind(self) -> ComdatSelectionKind {
48        let kind_ptr = unsafe { LLVMGetComdatSelectionKind(self.0) };
49
50        ComdatSelectionKind::new(kind_ptr)
51    }
52
53    /// Sets what kind of `Comdat` this should be.
54    pub fn set_selection_kind(self, kind: ComdatSelectionKind) {
55        unsafe { LLVMSetComdatSelectionKind(self.0, kind.into()) }
56    }
57}