rustc_codegen_llvm/llvm/
ffi.rs

1//! Bindings to the LLVM-C API (`LLVM*`), and to our own `extern "C"` wrapper
2//! functions around the unstable LLVM C++ API (`LLVMRust*`).
3//!
4//! ## Passing pointer/length strings as `*const c_uchar`
5//!
6//! Normally it's a good idea for Rust-side bindings to match the corresponding
7//! C-side function declarations as closely as possible. But when passing `&str`
8//! or `&[u8]` data as a pointer/length pair, it's more convenient to declare
9//! the Rust-side pointer as `*const c_uchar` instead of `*const c_char`.
10//! Both pointer types have the same ABI, and using `*const c_uchar` avoids
11//! the need for an extra cast from `*const u8` on the Rust side.
12
13#![allow(non_camel_case_types)]
14#![allow(non_upper_case_globals)]
15
16use std::fmt::Debug;
17use std::marker::PhantomData;
18use std::ptr;
19
20use bitflags::bitflags;
21use libc::{c_char, c_int, c_uchar, c_uint, c_ulonglong, c_void, size_t};
22use rustc_macros::TryFromU32;
23use rustc_target::spec::SymbolVisibility;
24
25use super::RustString;
26use super::debuginfo::{
27    DIArray, DIBasicType, DIBuilder, DICompositeType, DIDerivedType, DIDescriptor, DIEnumerator,
28    DIFile, DIFlags, DIGlobalVariableExpression, DILocation, DISPFlags, DIScope, DISubprogram,
29    DISubrange, DITemplateTypeParameter, DIType, DIVariable, DebugEmissionKind, DebugNameTableKind,
30};
31use crate::llvm;
32
33/// In the LLVM-C API, boolean values are passed as `typedef int LLVMBool`,
34/// which has a different ABI from Rust or C++ `bool`.
35pub(crate) type Bool = c_int;
36
37pub(crate) const True: Bool = 1 as Bool;
38pub(crate) const False: Bool = 0 as Bool;
39
40/// Wrapper for a raw enum value returned from LLVM's C APIs.
41///
42/// For C enums returned by LLVM, it's risky to use a Rust enum as the return
43/// type, because it would be UB if a later version of LLVM adds a new enum
44/// value and returns it. Instead, return this raw wrapper, then convert to the
45/// Rust-side enum explicitly.
46#[repr(transparent)]
47pub(crate) struct RawEnum<T> {
48    value: u32,
49    /// We don't own or consume a `T`, but we can produce one.
50    _rust_side_type: PhantomData<fn() -> T>,
51}
52
53impl<T: TryFrom<u32>> RawEnum<T> {
54    #[track_caller]
55    pub(crate) fn to_rust(self) -> T
56    where
57        T::Error: Debug,
58    {
59        // If this fails, the Rust-side enum is out of sync with LLVM's enum.
60        T::try_from(self.value).expect("enum value returned by LLVM should be known")
61    }
62}
63
64#[derive(Copy, Clone, PartialEq)]
65#[repr(C)]
66#[allow(dead_code)] // Variants constructed by C++.
67pub(crate) enum LLVMRustResult {
68    Success,
69    Failure,
70}
71
72/// Must match the layout of `LLVMRustModuleFlagMergeBehavior`.
73///
74/// When merging modules (e.g. during LTO), their metadata flags are combined. Conflicts are
75/// resolved according to the merge behaviors specified here. Flags differing only in merge
76/// behavior are still considered to be in conflict.
77///
78/// In order for Rust-C LTO to work, we must specify behaviors compatible with Clang. Notably,
79/// 'Error' and 'Warning' cannot be mixed for a given flag.
80///
81/// There is a stable LLVM-C version of this enum (`LLVMModuleFlagBehavior`),
82/// but as of LLVM 19 it does not support all of the enum values in the unstable
83/// C++ API.
84#[derive(Copy, Clone, PartialEq)]
85#[repr(C)]
86pub(crate) enum ModuleFlagMergeBehavior {
87    Error = 1,
88    Warning = 2,
89    Require = 3,
90    Override = 4,
91    Append = 5,
92    AppendUnique = 6,
93    Max = 7,
94    Min = 8,
95}
96
97// Consts for the LLVM CallConv type, pre-cast to usize.
98
99/// LLVM CallingConv::ID. Should we wrap this?
100///
101/// See <https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/IR/CallingConv.h>
102#[derive(Copy, Clone, PartialEq, Debug, TryFromU32)]
103#[repr(C)]
104pub(crate) enum CallConv {
105    CCallConv = 0,
106    FastCallConv = 8,
107    ColdCallConv = 9,
108    PreserveMost = 14,
109    PreserveAll = 15,
110    Tail = 18,
111    X86StdcallCallConv = 64,
112    X86FastcallCallConv = 65,
113    ArmAapcsCallConv = 67,
114    Msp430Intr = 69,
115    X86_ThisCall = 70,
116    PtxKernel = 71,
117    X86_64_SysV = 78,
118    X86_64_Win64 = 79,
119    X86_VectorCall = 80,
120    X86_Intr = 83,
121    AvrNonBlockingInterrupt = 84,
122    AvrInterrupt = 85,
123    AmdgpuKernel = 91,
124}
125
126/// Must match the layout of `LLVMLinkage`.
127#[derive(Copy, Clone, PartialEq, TryFromU32)]
128#[repr(C)]
129pub(crate) enum Linkage {
130    ExternalLinkage = 0,
131    AvailableExternallyLinkage = 1,
132    LinkOnceAnyLinkage = 2,
133    LinkOnceODRLinkage = 3,
134    #[deprecated = "marked obsolete by LLVM"]
135    LinkOnceODRAutoHideLinkage = 4,
136    WeakAnyLinkage = 5,
137    WeakODRLinkage = 6,
138    AppendingLinkage = 7,
139    InternalLinkage = 8,
140    PrivateLinkage = 9,
141    #[deprecated = "marked obsolete by LLVM"]
142    DLLImportLinkage = 10,
143    #[deprecated = "marked obsolete by LLVM"]
144    DLLExportLinkage = 11,
145    ExternalWeakLinkage = 12,
146    #[deprecated = "marked obsolete by LLVM"]
147    GhostLinkage = 13,
148    CommonLinkage = 14,
149    LinkerPrivateLinkage = 15,
150    LinkerPrivateWeakLinkage = 16,
151}
152
153/// Must match the layout of `LLVMVisibility`.
154#[repr(C)]
155#[derive(Copy, Clone, PartialEq, TryFromU32)]
156pub(crate) enum Visibility {
157    Default = 0,
158    Hidden = 1,
159    Protected = 2,
160}
161
162impl Visibility {
163    pub(crate) fn from_generic(visibility: SymbolVisibility) -> Self {
164        match visibility {
165            SymbolVisibility::Hidden => Visibility::Hidden,
166            SymbolVisibility::Protected => Visibility::Protected,
167            SymbolVisibility::Interposable => Visibility::Default,
168        }
169    }
170}
171
172/// LLVMUnnamedAddr
173#[repr(C)]
174pub(crate) enum UnnamedAddr {
175    No,
176    #[expect(dead_code)]
177    Local,
178    Global,
179}
180
181/// LLVMDLLStorageClass
182#[derive(Copy, Clone)]
183#[repr(C)]
184pub(crate) enum DLLStorageClass {
185    #[allow(dead_code)]
186    Default = 0,
187    DllImport = 1, // Function to be imported from DLL.
188    #[allow(dead_code)]
189    DllExport = 2, // Function to be accessible from DLL.
190}
191
192/// Must match the layout of `LLVMRustAttributeKind`.
193/// Semantically a subset of the C++ enum llvm::Attribute::AttrKind,
194/// though it is not ABI compatible (since it's a C++ enum)
195#[repr(C)]
196#[derive(Copy, Clone, Debug)]
197#[expect(dead_code, reason = "Some variants are unused, but are kept to match the C++")]
198pub(crate) enum AttributeKind {
199    AlwaysInline = 0,
200    ByVal = 1,
201    Cold = 2,
202    InlineHint = 3,
203    MinSize = 4,
204    Naked = 5,
205    NoAlias = 6,
206    NoCapture = 7,
207    NoInline = 8,
208    NonNull = 9,
209    NoRedZone = 10,
210    NoReturn = 11,
211    NoUnwind = 12,
212    OptimizeForSize = 13,
213    ReadOnly = 14,
214    SExt = 15,
215    StructRet = 16,
216    UWTable = 17,
217    ZExt = 18,
218    InReg = 19,
219    SanitizeThread = 20,
220    SanitizeAddress = 21,
221    SanitizeMemory = 22,
222    NonLazyBind = 23,
223    OptimizeNone = 24,
224    ReadNone = 26,
225    SanitizeHWAddress = 28,
226    WillReturn = 29,
227    StackProtectReq = 30,
228    StackProtectStrong = 31,
229    StackProtect = 32,
230    NoUndef = 33,
231    SanitizeMemTag = 34,
232    NoCfCheck = 35,
233    ShadowCallStack = 36,
234    AllocSize = 37,
235    AllocatedPointer = 38,
236    AllocAlign = 39,
237    SanitizeSafeStack = 40,
238    FnRetThunkExtern = 41,
239    Writable = 42,
240    DeadOnUnwind = 43,
241}
242
243/// LLVMIntPredicate
244#[derive(Copy, Clone)]
245#[repr(C)]
246pub(crate) enum IntPredicate {
247    IntEQ = 32,
248    IntNE = 33,
249    IntUGT = 34,
250    IntUGE = 35,
251    IntULT = 36,
252    IntULE = 37,
253    IntSGT = 38,
254    IntSGE = 39,
255    IntSLT = 40,
256    IntSLE = 41,
257}
258
259impl IntPredicate {
260    pub(crate) fn from_generic(intpre: rustc_codegen_ssa::common::IntPredicate) -> Self {
261        use rustc_codegen_ssa::common::IntPredicate as Common;
262        match intpre {
263            Common::IntEQ => Self::IntEQ,
264            Common::IntNE => Self::IntNE,
265            Common::IntUGT => Self::IntUGT,
266            Common::IntUGE => Self::IntUGE,
267            Common::IntULT => Self::IntULT,
268            Common::IntULE => Self::IntULE,
269            Common::IntSGT => Self::IntSGT,
270            Common::IntSGE => Self::IntSGE,
271            Common::IntSLT => Self::IntSLT,
272            Common::IntSLE => Self::IntSLE,
273        }
274    }
275}
276
277/// LLVMRealPredicate
278#[derive(Copy, Clone)]
279#[repr(C)]
280pub(crate) enum RealPredicate {
281    RealPredicateFalse = 0,
282    RealOEQ = 1,
283    RealOGT = 2,
284    RealOGE = 3,
285    RealOLT = 4,
286    RealOLE = 5,
287    RealONE = 6,
288    RealORD = 7,
289    RealUNO = 8,
290    RealUEQ = 9,
291    RealUGT = 10,
292    RealUGE = 11,
293    RealULT = 12,
294    RealULE = 13,
295    RealUNE = 14,
296    RealPredicateTrue = 15,
297}
298
299impl RealPredicate {
300    pub(crate) fn from_generic(realp: rustc_codegen_ssa::common::RealPredicate) -> Self {
301        use rustc_codegen_ssa::common::RealPredicate as Common;
302        match realp {
303            Common::RealPredicateFalse => Self::RealPredicateFalse,
304            Common::RealOEQ => Self::RealOEQ,
305            Common::RealOGT => Self::RealOGT,
306            Common::RealOGE => Self::RealOGE,
307            Common::RealOLT => Self::RealOLT,
308            Common::RealOLE => Self::RealOLE,
309            Common::RealONE => Self::RealONE,
310            Common::RealORD => Self::RealORD,
311            Common::RealUNO => Self::RealUNO,
312            Common::RealUEQ => Self::RealUEQ,
313            Common::RealUGT => Self::RealUGT,
314            Common::RealUGE => Self::RealUGE,
315            Common::RealULT => Self::RealULT,
316            Common::RealULE => Self::RealULE,
317            Common::RealUNE => Self::RealUNE,
318            Common::RealPredicateTrue => Self::RealPredicateTrue,
319        }
320    }
321}
322
323/// LLVMTypeKind
324#[derive(Copy, Clone, PartialEq, Debug)]
325#[repr(C)]
326#[expect(dead_code, reason = "Some variants are unused, but are kept to match LLVM-C")]
327pub(crate) enum TypeKind {
328    Void = 0,
329    Half = 1,
330    Float = 2,
331    Double = 3,
332    X86_FP80 = 4,
333    FP128 = 5,
334    PPC_FP128 = 6,
335    Label = 7,
336    Integer = 8,
337    Function = 9,
338    Struct = 10,
339    Array = 11,
340    Pointer = 12,
341    Vector = 13,
342    Metadata = 14,
343    Token = 16,
344    ScalableVector = 17,
345    BFloat = 18,
346    X86_AMX = 19,
347}
348
349impl TypeKind {
350    pub(crate) fn to_generic(self) -> rustc_codegen_ssa::common::TypeKind {
351        use rustc_codegen_ssa::common::TypeKind as Common;
352        match self {
353            Self::Void => Common::Void,
354            Self::Half => Common::Half,
355            Self::Float => Common::Float,
356            Self::Double => Common::Double,
357            Self::X86_FP80 => Common::X86_FP80,
358            Self::FP128 => Common::FP128,
359            Self::PPC_FP128 => Common::PPC_FP128,
360            Self::Label => Common::Label,
361            Self::Integer => Common::Integer,
362            Self::Function => Common::Function,
363            Self::Struct => Common::Struct,
364            Self::Array => Common::Array,
365            Self::Pointer => Common::Pointer,
366            Self::Vector => Common::Vector,
367            Self::Metadata => Common::Metadata,
368            Self::Token => Common::Token,
369            Self::ScalableVector => Common::ScalableVector,
370            Self::BFloat => Common::BFloat,
371            Self::X86_AMX => Common::X86_AMX,
372        }
373    }
374}
375
376/// LLVMAtomicRmwBinOp
377#[derive(Copy, Clone)]
378#[repr(C)]
379pub(crate) enum AtomicRmwBinOp {
380    AtomicXchg = 0,
381    AtomicAdd = 1,
382    AtomicSub = 2,
383    AtomicAnd = 3,
384    AtomicNand = 4,
385    AtomicOr = 5,
386    AtomicXor = 6,
387    AtomicMax = 7,
388    AtomicMin = 8,
389    AtomicUMax = 9,
390    AtomicUMin = 10,
391}
392
393impl AtomicRmwBinOp {
394    pub(crate) fn from_generic(op: rustc_codegen_ssa::common::AtomicRmwBinOp) -> Self {
395        use rustc_codegen_ssa::common::AtomicRmwBinOp as Common;
396        match op {
397            Common::AtomicXchg => Self::AtomicXchg,
398            Common::AtomicAdd => Self::AtomicAdd,
399            Common::AtomicSub => Self::AtomicSub,
400            Common::AtomicAnd => Self::AtomicAnd,
401            Common::AtomicNand => Self::AtomicNand,
402            Common::AtomicOr => Self::AtomicOr,
403            Common::AtomicXor => Self::AtomicXor,
404            Common::AtomicMax => Self::AtomicMax,
405            Common::AtomicMin => Self::AtomicMin,
406            Common::AtomicUMax => Self::AtomicUMax,
407            Common::AtomicUMin => Self::AtomicUMin,
408        }
409    }
410}
411
412/// LLVMAtomicOrdering
413#[derive(Copy, Clone)]
414#[repr(C)]
415pub(crate) enum AtomicOrdering {
416    #[allow(dead_code)]
417    NotAtomic = 0,
418    Unordered = 1,
419    Monotonic = 2,
420    // Consume = 3,  // Not specified yet.
421    Acquire = 4,
422    Release = 5,
423    AcquireRelease = 6,
424    SequentiallyConsistent = 7,
425}
426
427impl AtomicOrdering {
428    pub(crate) fn from_generic(ao: rustc_codegen_ssa::common::AtomicOrdering) -> Self {
429        use rustc_codegen_ssa::common::AtomicOrdering as Common;
430        match ao {
431            Common::Unordered => Self::Unordered,
432            Common::Relaxed => Self::Monotonic,
433            Common::Acquire => Self::Acquire,
434            Common::Release => Self::Release,
435            Common::AcquireRelease => Self::AcquireRelease,
436            Common::SequentiallyConsistent => Self::SequentiallyConsistent,
437        }
438    }
439}
440
441/// LLVMRustFileType
442#[derive(Copy, Clone)]
443#[repr(C)]
444pub(crate) enum FileType {
445    AssemblyFile,
446    ObjectFile,
447}
448
449/// LLVMMetadataType
450#[derive(Copy, Clone)]
451#[repr(C)]
452#[expect(dead_code, reason = "Some variants are unused, but are kept to match LLVM-C")]
453pub(crate) enum MetadataType {
454    MD_dbg = 0,
455    MD_tbaa = 1,
456    MD_prof = 2,
457    MD_fpmath = 3,
458    MD_range = 4,
459    MD_tbaa_struct = 5,
460    MD_invariant_load = 6,
461    MD_alias_scope = 7,
462    MD_noalias = 8,
463    MD_nontemporal = 9,
464    MD_mem_parallel_loop_access = 10,
465    MD_nonnull = 11,
466    MD_unpredictable = 15,
467    MD_align = 17,
468    MD_type = 19,
469    MD_vcall_visibility = 28,
470    MD_noundef = 29,
471    MD_kcfi_type = 36,
472}
473
474/// LLVMRustAsmDialect
475#[derive(Copy, Clone, PartialEq)]
476#[repr(C)]
477pub(crate) enum AsmDialect {
478    Att,
479    Intel,
480}
481
482/// LLVMRustCodeGenOptLevel
483#[derive(Copy, Clone, PartialEq)]
484#[repr(C)]
485pub(crate) enum CodeGenOptLevel {
486    None,
487    Less,
488    Default,
489    Aggressive,
490}
491
492/// LLVMRustPassBuilderOptLevel
493#[repr(C)]
494pub(crate) enum PassBuilderOptLevel {
495    O0,
496    O1,
497    O2,
498    O3,
499    Os,
500    Oz,
501}
502
503/// LLVMRustOptStage
504#[derive(PartialEq)]
505#[repr(C)]
506pub(crate) enum OptStage {
507    PreLinkNoLTO,
508    PreLinkThinLTO,
509    PreLinkFatLTO,
510    ThinLTO,
511    FatLTO,
512}
513
514/// LLVMRustSanitizerOptions
515#[repr(C)]
516pub(crate) struct SanitizerOptions {
517    pub sanitize_address: bool,
518    pub sanitize_address_recover: bool,
519    pub sanitize_cfi: bool,
520    pub sanitize_dataflow: bool,
521    pub sanitize_dataflow_abilist: *const *const c_char,
522    pub sanitize_dataflow_abilist_len: size_t,
523    pub sanitize_kcfi: bool,
524    pub sanitize_memory: bool,
525    pub sanitize_memory_recover: bool,
526    pub sanitize_memory_track_origins: c_int,
527    pub sanitize_thread: bool,
528    pub sanitize_hwaddress: bool,
529    pub sanitize_hwaddress_recover: bool,
530    pub sanitize_kernel_address: bool,
531    pub sanitize_kernel_address_recover: bool,
532}
533
534/// LLVMRustRelocModel
535#[derive(Copy, Clone, PartialEq)]
536#[repr(C)]
537pub(crate) enum RelocModel {
538    Static,
539    PIC,
540    DynamicNoPic,
541    ROPI,
542    RWPI,
543    ROPI_RWPI,
544}
545
546/// LLVMRustFloatABI
547#[derive(Copy, Clone, PartialEq)]
548#[repr(C)]
549pub(crate) enum FloatAbi {
550    Default,
551    Soft,
552    Hard,
553}
554
555/// LLVMRustCodeModel
556#[derive(Copy, Clone)]
557#[repr(C)]
558pub(crate) enum CodeModel {
559    Tiny,
560    Small,
561    Kernel,
562    Medium,
563    Large,
564    None,
565}
566
567/// LLVMRustDiagnosticKind
568#[derive(Copy, Clone)]
569#[repr(C)]
570#[allow(dead_code)] // Variants constructed by C++.
571pub(crate) enum DiagnosticKind {
572    Other,
573    InlineAsm,
574    StackSize,
575    DebugMetadataVersion,
576    SampleProfile,
577    OptimizationRemark,
578    OptimizationRemarkMissed,
579    OptimizationRemarkAnalysis,
580    OptimizationRemarkAnalysisFPCommute,
581    OptimizationRemarkAnalysisAliasing,
582    OptimizationRemarkOther,
583    OptimizationFailure,
584    PGOProfile,
585    Linker,
586    Unsupported,
587    SrcMgr,
588}
589
590/// LLVMRustDiagnosticLevel
591#[derive(Copy, Clone)]
592#[repr(C)]
593#[allow(dead_code)] // Variants constructed by C++.
594pub(crate) enum DiagnosticLevel {
595    Error,
596    Warning,
597    Note,
598    Remark,
599}
600
601/// LLVMRustArchiveKind
602#[derive(Copy, Clone)]
603#[repr(C)]
604pub(crate) enum ArchiveKind {
605    K_GNU,
606    K_BSD,
607    K_DARWIN,
608    K_COFF,
609    K_AIXBIG,
610}
611
612unsafe extern "C" {
613    // LLVMRustThinLTOData
614    pub(crate) type ThinLTOData;
615
616    // LLVMRustThinLTOBuffer
617    pub(crate) type ThinLTOBuffer;
618}
619
620/// LLVMRustThinLTOModule
621#[repr(C)]
622pub(crate) struct ThinLTOModule {
623    pub identifier: *const c_char,
624    pub data: *const u8,
625    pub len: usize,
626}
627
628/// LLVMThreadLocalMode
629#[derive(Copy, Clone)]
630#[repr(C)]
631pub(crate) enum ThreadLocalMode {
632    #[expect(dead_code)]
633    NotThreadLocal,
634    GeneralDynamic,
635    LocalDynamic,
636    InitialExec,
637    LocalExec,
638}
639
640/// LLVMRustChecksumKind
641#[derive(Copy, Clone)]
642#[repr(C)]
643pub(crate) enum ChecksumKind {
644    None,
645    MD5,
646    SHA1,
647    SHA256,
648}
649
650/// LLVMRustMemoryEffects
651#[derive(Copy, Clone)]
652#[repr(C)]
653pub(crate) enum MemoryEffects {
654    None,
655    ReadOnly,
656    InaccessibleMemOnly,
657}
658
659/// LLVMOpcode
660#[derive(Copy, Clone, PartialEq, Eq)]
661#[repr(C)]
662#[expect(dead_code, reason = "Some variants are unused, but are kept to match LLVM-C")]
663pub(crate) enum Opcode {
664    Ret = 1,
665    Br = 2,
666    Switch = 3,
667    IndirectBr = 4,
668    Invoke = 5,
669    Unreachable = 7,
670    CallBr = 67,
671    FNeg = 66,
672    Add = 8,
673    FAdd = 9,
674    Sub = 10,
675    FSub = 11,
676    Mul = 12,
677    FMul = 13,
678    UDiv = 14,
679    SDiv = 15,
680    FDiv = 16,
681    URem = 17,
682    SRem = 18,
683    FRem = 19,
684    Shl = 20,
685    LShr = 21,
686    AShr = 22,
687    And = 23,
688    Or = 24,
689    Xor = 25,
690    Alloca = 26,
691    Load = 27,
692    Store = 28,
693    GetElementPtr = 29,
694    Trunc = 30,
695    ZExt = 31,
696    SExt = 32,
697    FPToUI = 33,
698    FPToSI = 34,
699    UIToFP = 35,
700    SIToFP = 36,
701    FPTrunc = 37,
702    FPExt = 38,
703    PtrToInt = 39,
704    IntToPtr = 40,
705    BitCast = 41,
706    AddrSpaceCast = 60,
707    ICmp = 42,
708    FCmp = 43,
709    PHI = 44,
710    Call = 45,
711    Select = 46,
712    UserOp1 = 47,
713    UserOp2 = 48,
714    VAArg = 49,
715    ExtractElement = 50,
716    InsertElement = 51,
717    ShuffleVector = 52,
718    ExtractValue = 53,
719    InsertValue = 54,
720    Freeze = 68,
721    Fence = 55,
722    AtomicCmpXchg = 56,
723    AtomicRMW = 57,
724    Resume = 58,
725    LandingPad = 59,
726    CleanupRet = 61,
727    CatchRet = 62,
728    CatchPad = 63,
729    CleanupPad = 64,
730    CatchSwitch = 65,
731}
732
733unsafe extern "C" {
734    type Opaque;
735}
736#[repr(C)]
737struct InvariantOpaque<'a> {
738    _marker: PhantomData<&'a mut &'a ()>,
739    _opaque: Opaque,
740}
741
742// Opaque pointer types
743unsafe extern "C" {
744    pub(crate) type Module;
745    pub(crate) type Context;
746    pub(crate) type Type;
747    pub(crate) type Value;
748    pub(crate) type ConstantInt;
749    pub(crate) type Attribute;
750    pub(crate) type Metadata;
751    pub(crate) type BasicBlock;
752    pub(crate) type Comdat;
753}
754#[repr(C)]
755pub(crate) struct Builder<'a>(InvariantOpaque<'a>);
756#[repr(C)]
757pub(crate) struct PassManager<'a>(InvariantOpaque<'a>);
758unsafe extern "C" {
759    pub type TargetMachine;
760    pub(crate) type Archive;
761}
762#[repr(C)]
763pub(crate) struct ArchiveIterator<'a>(InvariantOpaque<'a>);
764#[repr(C)]
765pub(crate) struct ArchiveChild<'a>(InvariantOpaque<'a>);
766unsafe extern "C" {
767    pub(crate) type Twine;
768    pub(crate) type DiagnosticInfo;
769    pub(crate) type SMDiagnostic;
770}
771#[repr(C)]
772pub(crate) struct RustArchiveMember<'a>(InvariantOpaque<'a>);
773/// Opaque pointee of `LLVMOperandBundleRef`.
774#[repr(C)]
775pub(crate) struct OperandBundle<'a>(InvariantOpaque<'a>);
776#[repr(C)]
777pub(crate) struct Linker<'a>(InvariantOpaque<'a>);
778
779unsafe extern "C" {
780    pub(crate) type DiagnosticHandler;
781}
782
783pub(crate) type DiagnosticHandlerTy = unsafe extern "C" fn(&DiagnosticInfo, *mut c_void);
784
785pub(crate) mod debuginfo {
786    use std::ptr;
787
788    use bitflags::bitflags;
789
790    use super::{InvariantOpaque, Metadata};
791    use crate::llvm::{self, Module};
792
793    /// Opaque target type for references to an LLVM debuginfo builder.
794    ///
795    /// `&'_ DIBuilder<'ll>` corresponds to `LLVMDIBuilderRef`, which is the
796    /// LLVM-C wrapper for `DIBuilder *`.
797    ///
798    /// Debuginfo builders are created and destroyed during codegen, so the
799    /// builder reference typically has a shorter lifetime than the LLVM
800    /// session (`'ll`) that it participates in.
801    #[repr(C)]
802    pub(crate) struct DIBuilder<'ll>(InvariantOpaque<'ll>);
803
804    /// Owning pointer to a `DIBuilder<'ll>` that will dispose of the builder
805    /// when dropped. Use `.as_ref()` to get the underlying `&DIBuilder`
806    /// needed for debuginfo FFI calls.
807    pub(crate) struct DIBuilderBox<'ll> {
808        raw: ptr::NonNull<DIBuilder<'ll>>,
809    }
810
811    impl<'ll> DIBuilderBox<'ll> {
812        pub(crate) fn new(llmod: &'ll Module) -> Self {
813            let raw = unsafe { llvm::LLVMCreateDIBuilder(llmod) };
814            let raw = ptr::NonNull::new(raw).unwrap();
815            Self { raw }
816        }
817
818        pub(crate) fn as_ref(&self) -> &DIBuilder<'ll> {
819            // SAFETY: This is an owning pointer, so `&DIBuilder` is valid
820            // for as long as `&self` is.
821            unsafe { self.raw.as_ref() }
822        }
823    }
824
825    impl<'ll> Drop for DIBuilderBox<'ll> {
826        fn drop(&mut self) {
827            unsafe { llvm::LLVMDisposeDIBuilder(self.raw) };
828        }
829    }
830
831    pub(crate) type DIDescriptor = Metadata;
832    pub(crate) type DILocation = Metadata;
833    pub(crate) type DIScope = DIDescriptor;
834    pub(crate) type DIFile = DIScope;
835    pub(crate) type DILexicalBlock = DIScope;
836    pub(crate) type DISubprogram = DIScope;
837    pub(crate) type DIType = DIDescriptor;
838    pub(crate) type DIBasicType = DIType;
839    pub(crate) type DIDerivedType = DIType;
840    pub(crate) type DICompositeType = DIDerivedType;
841    pub(crate) type DIVariable = DIDescriptor;
842    pub(crate) type DIGlobalVariableExpression = DIDescriptor;
843    pub(crate) type DIArray = DIDescriptor;
844    pub(crate) type DISubrange = DIDescriptor;
845    pub(crate) type DIEnumerator = DIDescriptor;
846    pub(crate) type DITemplateTypeParameter = DIDescriptor;
847
848    bitflags! {
849        /// Must match the layout of `LLVMDIFlags` in the LLVM-C API.
850        ///
851        /// Each value declared here must also be covered by the static
852        /// assertions in `RustWrapper.cpp` used by `fromRust(LLVMDIFlags)`.
853        #[repr(transparent)]
854        #[derive(Clone, Copy, Default)]
855        pub(crate) struct DIFlags: u32 {
856            const FlagZero                = 0;
857            const FlagPrivate             = 1;
858            const FlagProtected           = 2;
859            const FlagPublic              = 3;
860            const FlagFwdDecl             = (1 << 2);
861            const FlagAppleBlock          = (1 << 3);
862            const FlagReservedBit4        = (1 << 4);
863            const FlagVirtual             = (1 << 5);
864            const FlagArtificial          = (1 << 6);
865            const FlagExplicit            = (1 << 7);
866            const FlagPrototyped          = (1 << 8);
867            const FlagObjcClassComplete   = (1 << 9);
868            const FlagObjectPointer       = (1 << 10);
869            const FlagVector              = (1 << 11);
870            const FlagStaticMember        = (1 << 12);
871            const FlagLValueReference     = (1 << 13);
872            const FlagRValueReference     = (1 << 14);
873            const FlagReserved            = (1 << 15);
874            const FlagSingleInheritance   = (1 << 16);
875            const FlagMultipleInheritance = (2 << 16);
876            const FlagVirtualInheritance  = (3 << 16);
877            const FlagIntroducedVirtual   = (1 << 18);
878            const FlagBitField            = (1 << 19);
879            const FlagNoReturn            = (1 << 20);
880            // The bit at (1 << 21) is unused, but was `LLVMDIFlagMainSubprogram`.
881            const FlagTypePassByValue     = (1 << 22);
882            const FlagTypePassByReference = (1 << 23);
883            const FlagEnumClass           = (1 << 24);
884            const FlagThunk               = (1 << 25);
885            const FlagNonTrivial          = (1 << 26);
886            const FlagBigEndian           = (1 << 27);
887            const FlagLittleEndian        = (1 << 28);
888        }
889    }
890
891    // These values **must** match with LLVMRustDISPFlags!!
892    bitflags! {
893        #[repr(transparent)]
894        #[derive(Clone, Copy, Default)]
895        pub(crate) struct DISPFlags: u32 {
896            const SPFlagZero              = 0;
897            const SPFlagVirtual           = 1;
898            const SPFlagPureVirtual       = 2;
899            const SPFlagLocalToUnit       = (1 << 2);
900            const SPFlagDefinition        = (1 << 3);
901            const SPFlagOptimized         = (1 << 4);
902            const SPFlagMainSubprogram    = (1 << 5);
903        }
904    }
905
906    /// LLVMRustDebugEmissionKind
907    #[derive(Copy, Clone)]
908    #[repr(C)]
909    pub(crate) enum DebugEmissionKind {
910        NoDebug,
911        FullDebug,
912        LineTablesOnly,
913        DebugDirectivesOnly,
914    }
915
916    impl DebugEmissionKind {
917        pub(crate) fn from_generic(kind: rustc_session::config::DebugInfo) -> Self {
918            // We should be setting LLVM's emission kind to `LineTablesOnly` if
919            // we are compiling with "limited" debuginfo. However, some of the
920            // existing tools relied on slightly more debuginfo being generated than
921            // would be the case with `LineTablesOnly`, and we did not want to break
922            // these tools in a "drive-by fix", without a good idea or plan about
923            // what limited debuginfo should exactly look like. So for now we are
924            // instead adding a new debuginfo option "line-tables-only" so as to
925            // not break anything and to allow users to have 'limited' debug info.
926            //
927            // See https://github.com/rust-lang/rust/issues/60020 for details.
928            use rustc_session::config::DebugInfo;
929            match kind {
930                DebugInfo::None => DebugEmissionKind::NoDebug,
931                DebugInfo::LineDirectivesOnly => DebugEmissionKind::DebugDirectivesOnly,
932                DebugInfo::LineTablesOnly => DebugEmissionKind::LineTablesOnly,
933                DebugInfo::Limited | DebugInfo::Full => DebugEmissionKind::FullDebug,
934            }
935        }
936    }
937
938    /// LLVMRustDebugNameTableKind
939    #[derive(Clone, Copy)]
940    #[repr(C)]
941    pub(crate) enum DebugNameTableKind {
942        Default,
943        #[expect(dead_code)]
944        Gnu,
945        None,
946    }
947}
948
949// These values **must** match with LLVMRustAllocKindFlags
950bitflags! {
951    #[repr(transparent)]
952    #[derive(Default)]
953    pub(crate) struct AllocKindFlags : u64 {
954        const Unknown = 0;
955        const Alloc = 1;
956        const Realloc = 1 << 1;
957        const Free = 1 << 2;
958        const Uninitialized = 1 << 3;
959        const Zeroed = 1 << 4;
960        const Aligned = 1 << 5;
961    }
962}
963
964// These values **must** match with LLVMGEPNoWrapFlags
965bitflags! {
966    #[repr(transparent)]
967    #[derive(Default)]
968    pub struct GEPNoWrapFlags : c_uint {
969        const InBounds = 1 << 0;
970        const NUSW = 1 << 1;
971        const NUW = 1 << 2;
972    }
973}
974
975unsafe extern "C" {
976    pub(crate) type ModuleBuffer;
977}
978
979pub(crate) type SelfProfileBeforePassCallback =
980    unsafe extern "C" fn(*mut c_void, *const c_char, *const c_char);
981pub(crate) type SelfProfileAfterPassCallback = unsafe extern "C" fn(*mut c_void);
982
983pub(crate) type GetSymbolsCallback =
984    unsafe extern "C" fn(*mut c_void, *const c_char) -> *mut c_void;
985pub(crate) type GetSymbolsErrorCallback = unsafe extern "C" fn(*const c_char) -> *mut c_void;
986
987#[derive(Copy, Clone)]
988#[repr(transparent)]
989pub(crate) struct MetadataKindId(c_uint);
990
991impl From<MetadataType> for MetadataKindId {
992    fn from(value: MetadataType) -> Self {
993        Self(value as c_uint)
994    }
995}
996
997unsafe extern "C" {
998    // Create and destroy contexts.
999    pub(crate) fn LLVMContextDispose(C: &'static mut Context);
1000    pub(crate) fn LLVMGetMDKindIDInContext(
1001        C: &Context,
1002        Name: *const c_char,
1003        SLen: c_uint,
1004    ) -> MetadataKindId;
1005
1006    // Create modules.
1007    pub(crate) fn LLVMModuleCreateWithNameInContext(
1008        ModuleID: *const c_char,
1009        C: &Context,
1010    ) -> &Module;
1011    pub(crate) fn LLVMCloneModule(M: &Module) -> &Module;
1012
1013    /// Data layout. See Module::getDataLayout.
1014    pub(crate) fn LLVMGetDataLayoutStr(M: &Module) -> *const c_char;
1015    pub(crate) fn LLVMSetDataLayout(M: &Module, Triple: *const c_char);
1016
1017    /// See Module::setModuleInlineAsm.
1018    pub(crate) fn LLVMAppendModuleInlineAsm(M: &Module, Asm: *const c_char, Len: size_t);
1019
1020    // Operations on integer types
1021    pub(crate) fn LLVMInt1TypeInContext(C: &Context) -> &Type;
1022    pub(crate) fn LLVMInt8TypeInContext(C: &Context) -> &Type;
1023    pub(crate) fn LLVMInt16TypeInContext(C: &Context) -> &Type;
1024    pub(crate) fn LLVMInt32TypeInContext(C: &Context) -> &Type;
1025    pub(crate) fn LLVMInt64TypeInContext(C: &Context) -> &Type;
1026    pub(crate) fn LLVMIntTypeInContext(C: &Context, NumBits: c_uint) -> &Type;
1027
1028    pub(crate) fn LLVMGetIntTypeWidth(IntegerTy: &Type) -> c_uint;
1029
1030    // Operations on real types
1031    pub(crate) fn LLVMHalfTypeInContext(C: &Context) -> &Type;
1032    pub(crate) fn LLVMFloatTypeInContext(C: &Context) -> &Type;
1033    pub(crate) fn LLVMDoubleTypeInContext(C: &Context) -> &Type;
1034    pub(crate) fn LLVMFP128TypeInContext(C: &Context) -> &Type;
1035
1036    // Operations on function types
1037    pub(crate) fn LLVMFunctionType<'a>(
1038        ReturnType: &'a Type,
1039        ParamTypes: *const &'a Type,
1040        ParamCount: c_uint,
1041        IsVarArg: Bool,
1042    ) -> &'a Type;
1043    pub(crate) fn LLVMCountParamTypes(FunctionTy: &Type) -> c_uint;
1044    pub(crate) fn LLVMGetParamTypes<'a>(FunctionTy: &'a Type, Dest: *mut &'a Type);
1045
1046    // Operations on struct types
1047    pub(crate) fn LLVMStructTypeInContext<'a>(
1048        C: &'a Context,
1049        ElementTypes: *const &'a Type,
1050        ElementCount: c_uint,
1051        Packed: Bool,
1052    ) -> &'a Type;
1053
1054    // Operations on array, pointer, and vector types (sequence types)
1055    pub(crate) fn LLVMPointerTypeInContext(C: &Context, AddressSpace: c_uint) -> &Type;
1056    pub(crate) fn LLVMVectorType(ElementType: &Type, ElementCount: c_uint) -> &Type;
1057
1058    pub(crate) fn LLVMGetElementType(Ty: &Type) -> &Type;
1059    pub(crate) fn LLVMGetVectorSize(VectorTy: &Type) -> c_uint;
1060
1061    // Operations on other types
1062    pub(crate) fn LLVMVoidTypeInContext(C: &Context) -> &Type;
1063    pub(crate) fn LLVMTokenTypeInContext(C: &Context) -> &Type;
1064    pub(crate) fn LLVMMetadataTypeInContext(C: &Context) -> &Type;
1065
1066    // Operations on all values
1067    pub(crate) fn LLVMTypeOf(Val: &Value) -> &Type;
1068    pub(crate) fn LLVMGetValueName2(Val: &Value, Length: *mut size_t) -> *const c_char;
1069    pub(crate) fn LLVMSetValueName2(Val: &Value, Name: *const c_char, NameLen: size_t);
1070    pub(crate) fn LLVMReplaceAllUsesWith<'a>(OldVal: &'a Value, NewVal: &'a Value);
1071    pub(crate) safe fn LLVMSetMetadata<'a>(Val: &'a Value, KindID: MetadataKindId, Node: &'a Value);
1072    pub(crate) fn LLVMGlobalSetMetadata<'a>(Val: &'a Value, KindID: c_uint, Metadata: &'a Metadata);
1073    pub(crate) safe fn LLVMValueAsMetadata(Node: &Value) -> &Metadata;
1074
1075    // Operations on constants of any type
1076    pub(crate) fn LLVMConstNull(Ty: &Type) -> &Value;
1077    pub(crate) fn LLVMGetUndef(Ty: &Type) -> &Value;
1078    pub(crate) fn LLVMGetPoison(Ty: &Type) -> &Value;
1079
1080    // Operations on metadata
1081    pub(crate) fn LLVMMDStringInContext2(
1082        C: &Context,
1083        Str: *const c_char,
1084        SLen: size_t,
1085    ) -> &Metadata;
1086    pub(crate) fn LLVMMDNodeInContext2<'a>(
1087        C: &'a Context,
1088        Vals: *const &'a Metadata,
1089        Count: size_t,
1090    ) -> &'a Metadata;
1091    pub(crate) fn LLVMAddNamedMetadataOperand<'a>(
1092        M: &'a Module,
1093        Name: *const c_char,
1094        Val: &'a Value,
1095    );
1096
1097    // Operations on scalar constants
1098    pub(crate) fn LLVMConstInt(IntTy: &Type, N: c_ulonglong, SignExtend: Bool) -> &Value;
1099    pub(crate) fn LLVMConstIntOfArbitraryPrecision(
1100        IntTy: &Type,
1101        Wn: c_uint,
1102        Ws: *const u64,
1103    ) -> &Value;
1104    pub(crate) fn LLVMConstReal(RealTy: &Type, N: f64) -> &Value;
1105
1106    // Operations on composite constants
1107    pub(crate) fn LLVMConstArray2<'a>(
1108        ElementTy: &'a Type,
1109        ConstantVals: *const &'a Value,
1110        Length: u64,
1111    ) -> &'a Value;
1112    pub(crate) fn LLVMArrayType2(ElementType: &Type, ElementCount: u64) -> &Type;
1113    pub(crate) fn LLVMConstStringInContext2(
1114        C: &Context,
1115        Str: *const c_char,
1116        Length: size_t,
1117        DontNullTerminate: Bool,
1118    ) -> &Value;
1119    pub(crate) fn LLVMConstStructInContext<'a>(
1120        C: &'a Context,
1121        ConstantVals: *const &'a Value,
1122        Count: c_uint,
1123        Packed: Bool,
1124    ) -> &'a Value;
1125    pub(crate) fn LLVMConstVector(ScalarConstantVals: *const &Value, Size: c_uint) -> &Value;
1126
1127    // Constant expressions
1128    pub(crate) fn LLVMConstInBoundsGEP2<'a>(
1129        ty: &'a Type,
1130        ConstantVal: &'a Value,
1131        ConstantIndices: *const &'a Value,
1132        NumIndices: c_uint,
1133    ) -> &'a Value;
1134    pub(crate) fn LLVMConstPtrToInt<'a>(ConstantVal: &'a Value, ToType: &'a Type) -> &'a Value;
1135    pub(crate) fn LLVMConstIntToPtr<'a>(ConstantVal: &'a Value, ToType: &'a Type) -> &'a Value;
1136    pub(crate) fn LLVMConstBitCast<'a>(ConstantVal: &'a Value, ToType: &'a Type) -> &'a Value;
1137    pub(crate) fn LLVMConstPointerCast<'a>(ConstantVal: &'a Value, ToType: &'a Type) -> &'a Value;
1138    pub(crate) fn LLVMGetAggregateElement(ConstantVal: &Value, Idx: c_uint) -> Option<&Value>;
1139    pub(crate) fn LLVMGetConstOpcode(ConstantVal: &Value) -> Opcode;
1140    pub(crate) fn LLVMIsAConstantExpr(Val: &Value) -> Option<&Value>;
1141
1142    // Operations on global variables, functions, and aliases (globals)
1143    pub(crate) fn LLVMIsDeclaration(Global: &Value) -> Bool;
1144    pub(crate) fn LLVMGetLinkage(Global: &Value) -> RawEnum<Linkage>;
1145    pub(crate) fn LLVMSetLinkage(Global: &Value, RustLinkage: Linkage);
1146    pub(crate) fn LLVMSetSection(Global: &Value, Section: *const c_char);
1147    pub(crate) fn LLVMGetVisibility(Global: &Value) -> RawEnum<Visibility>;
1148    pub(crate) fn LLVMSetVisibility(Global: &Value, Viz: Visibility);
1149    pub(crate) fn LLVMGetAlignment(Global: &Value) -> c_uint;
1150    pub(crate) fn LLVMSetAlignment(Global: &Value, Bytes: c_uint);
1151    pub(crate) fn LLVMSetDLLStorageClass(V: &Value, C: DLLStorageClass);
1152    pub(crate) fn LLVMGlobalGetValueType(Global: &Value) -> &Type;
1153
1154    // Operations on global variables
1155    pub(crate) fn LLVMIsAGlobalVariable(GlobalVar: &Value) -> Option<&Value>;
1156    pub(crate) fn LLVMAddGlobal<'a>(M: &'a Module, Ty: &'a Type, Name: *const c_char) -> &'a Value;
1157    pub(crate) fn LLVMGetNamedGlobal(M: &Module, Name: *const c_char) -> Option<&Value>;
1158    pub(crate) fn LLVMGetFirstGlobal(M: &Module) -> Option<&Value>;
1159    pub(crate) fn LLVMGetNextGlobal(GlobalVar: &Value) -> Option<&Value>;
1160    pub(crate) fn LLVMDeleteGlobal(GlobalVar: &Value);
1161    pub(crate) fn LLVMGetInitializer(GlobalVar: &Value) -> Option<&Value>;
1162    pub(crate) fn LLVMSetInitializer<'a>(GlobalVar: &'a Value, ConstantVal: &'a Value);
1163    pub(crate) fn LLVMIsThreadLocal(GlobalVar: &Value) -> Bool;
1164    pub(crate) fn LLVMSetThreadLocalMode(GlobalVar: &Value, Mode: ThreadLocalMode);
1165    pub(crate) fn LLVMIsGlobalConstant(GlobalVar: &Value) -> Bool;
1166    pub(crate) fn LLVMSetGlobalConstant(GlobalVar: &Value, IsConstant: Bool);
1167    pub(crate) safe fn LLVMSetTailCall(CallInst: &Value, IsTailCall: Bool);
1168
1169    // Operations on attributes
1170    pub(crate) fn LLVMCreateStringAttribute(
1171        C: &Context,
1172        Name: *const c_char,
1173        NameLen: c_uint,
1174        Value: *const c_char,
1175        ValueLen: c_uint,
1176    ) -> &Attribute;
1177
1178    // Operations on functions
1179    pub(crate) fn LLVMSetFunctionCallConv(Fn: &Value, CC: c_uint);
1180
1181    // Operations on parameters
1182    pub(crate) fn LLVMIsAArgument(Val: &Value) -> Option<&Value>;
1183    pub(crate) fn LLVMCountParams(Fn: &Value) -> c_uint;
1184    pub(crate) fn LLVMGetParam(Fn: &Value, Index: c_uint) -> &Value;
1185
1186    // Operations on basic blocks
1187    pub(crate) fn LLVMGetBasicBlockParent(BB: &BasicBlock) -> &Value;
1188    pub(crate) fn LLVMAppendBasicBlockInContext<'a>(
1189        C: &'a Context,
1190        Fn: &'a Value,
1191        Name: *const c_char,
1192    ) -> &'a BasicBlock;
1193
1194    // Operations on instructions
1195    pub(crate) fn LLVMIsAInstruction(Val: &Value) -> Option<&Value>;
1196    pub(crate) fn LLVMGetFirstBasicBlock(Fn: &Value) -> &BasicBlock;
1197    pub(crate) fn LLVMGetOperand(Val: &Value, Index: c_uint) -> Option<&Value>;
1198
1199    // Operations on call sites
1200    pub(crate) fn LLVMSetInstructionCallConv(Instr: &Value, CC: c_uint);
1201
1202    // Operations on load/store instructions (only)
1203    pub(crate) fn LLVMSetVolatile(MemoryAccessInst: &Value, volatile: Bool);
1204
1205    // Operations on phi nodes
1206    pub(crate) fn LLVMAddIncoming<'a>(
1207        PhiNode: &'a Value,
1208        IncomingValues: *const &'a Value,
1209        IncomingBlocks: *const &'a BasicBlock,
1210        Count: c_uint,
1211    );
1212
1213    // Instruction builders
1214    pub(crate) fn LLVMCreateBuilderInContext(C: &Context) -> &mut Builder<'_>;
1215    pub(crate) fn LLVMPositionBuilderAtEnd<'a>(Builder: &Builder<'a>, Block: &'a BasicBlock);
1216    pub(crate) fn LLVMGetInsertBlock<'a>(Builder: &Builder<'a>) -> &'a BasicBlock;
1217    pub(crate) fn LLVMDisposeBuilder<'a>(Builder: &'a mut Builder<'a>);
1218
1219    // Metadata
1220    pub(crate) fn LLVMSetCurrentDebugLocation2<'a>(Builder: &Builder<'a>, Loc: *const Metadata);
1221    pub(crate) fn LLVMGetCurrentDebugLocation2<'a>(Builder: &Builder<'a>) -> Option<&'a Metadata>;
1222
1223    // Terminators
1224    pub(crate) safe fn LLVMBuildRetVoid<'a>(B: &Builder<'a>) -> &'a Value;
1225    pub(crate) fn LLVMBuildRet<'a>(B: &Builder<'a>, V: &'a Value) -> &'a Value;
1226    pub(crate) fn LLVMBuildBr<'a>(B: &Builder<'a>, Dest: &'a BasicBlock) -> &'a Value;
1227    pub(crate) fn LLVMBuildCondBr<'a>(
1228        B: &Builder<'a>,
1229        If: &'a Value,
1230        Then: &'a BasicBlock,
1231        Else: &'a BasicBlock,
1232    ) -> &'a Value;
1233    pub(crate) fn LLVMBuildSwitch<'a>(
1234        B: &Builder<'a>,
1235        V: &'a Value,
1236        Else: &'a BasicBlock,
1237        NumCases: c_uint,
1238    ) -> &'a Value;
1239    pub(crate) fn LLVMBuildLandingPad<'a>(
1240        B: &Builder<'a>,
1241        Ty: &'a Type,
1242        PersFn: Option<&'a Value>,
1243        NumClauses: c_uint,
1244        Name: *const c_char,
1245    ) -> &'a Value;
1246    pub(crate) fn LLVMBuildResume<'a>(B: &Builder<'a>, Exn: &'a Value) -> &'a Value;
1247    pub(crate) fn LLVMBuildUnreachable<'a>(B: &Builder<'a>) -> &'a Value;
1248
1249    pub(crate) fn LLVMBuildCleanupPad<'a>(
1250        B: &Builder<'a>,
1251        ParentPad: Option<&'a Value>,
1252        Args: *const &'a Value,
1253        NumArgs: c_uint,
1254        Name: *const c_char,
1255    ) -> Option<&'a Value>;
1256    pub(crate) fn LLVMBuildCleanupRet<'a>(
1257        B: &Builder<'a>,
1258        CleanupPad: &'a Value,
1259        BB: Option<&'a BasicBlock>,
1260    ) -> Option<&'a Value>;
1261    pub(crate) fn LLVMBuildCatchPad<'a>(
1262        B: &Builder<'a>,
1263        ParentPad: &'a Value,
1264        Args: *const &'a Value,
1265        NumArgs: c_uint,
1266        Name: *const c_char,
1267    ) -> Option<&'a Value>;
1268    pub(crate) fn LLVMBuildCatchRet<'a>(
1269        B: &Builder<'a>,
1270        CatchPad: &'a Value,
1271        BB: &'a BasicBlock,
1272    ) -> Option<&'a Value>;
1273    pub(crate) fn LLVMBuildCatchSwitch<'a>(
1274        Builder: &Builder<'a>,
1275        ParentPad: Option<&'a Value>,
1276        UnwindBB: Option<&'a BasicBlock>,
1277        NumHandlers: c_uint,
1278        Name: *const c_char,
1279    ) -> Option<&'a Value>;
1280    pub(crate) fn LLVMAddHandler<'a>(CatchSwitch: &'a Value, Dest: &'a BasicBlock);
1281    pub(crate) fn LLVMSetPersonalityFn<'a>(Func: &'a Value, Pers: &'a Value);
1282
1283    // Add a case to the switch instruction
1284    pub(crate) fn LLVMAddCase<'a>(Switch: &'a Value, OnVal: &'a Value, Dest: &'a BasicBlock);
1285
1286    // Add a clause to the landing pad instruction
1287    pub(crate) fn LLVMAddClause<'a>(LandingPad: &'a Value, ClauseVal: &'a Value);
1288
1289    // Set the cleanup on a landing pad instruction
1290    pub(crate) fn LLVMSetCleanup(LandingPad: &Value, Val: Bool);
1291
1292    // Arithmetic
1293    pub(crate) fn LLVMBuildAdd<'a>(
1294        B: &Builder<'a>,
1295        LHS: &'a Value,
1296        RHS: &'a Value,
1297        Name: *const c_char,
1298    ) -> &'a Value;
1299    pub(crate) fn LLVMBuildFAdd<'a>(
1300        B: &Builder<'a>,
1301        LHS: &'a Value,
1302        RHS: &'a Value,
1303        Name: *const c_char,
1304    ) -> &'a Value;
1305    pub(crate) fn LLVMBuildSub<'a>(
1306        B: &Builder<'a>,
1307        LHS: &'a Value,
1308        RHS: &'a Value,
1309        Name: *const c_char,
1310    ) -> &'a Value;
1311    pub(crate) fn LLVMBuildFSub<'a>(
1312        B: &Builder<'a>,
1313        LHS: &'a Value,
1314        RHS: &'a Value,
1315        Name: *const c_char,
1316    ) -> &'a Value;
1317    pub(crate) fn LLVMBuildMul<'a>(
1318        B: &Builder<'a>,
1319        LHS: &'a Value,
1320        RHS: &'a Value,
1321        Name: *const c_char,
1322    ) -> &'a Value;
1323    pub(crate) fn LLVMBuildFMul<'a>(
1324        B: &Builder<'a>,
1325        LHS: &'a Value,
1326        RHS: &'a Value,
1327        Name: *const c_char,
1328    ) -> &'a Value;
1329    pub(crate) fn LLVMBuildUDiv<'a>(
1330        B: &Builder<'a>,
1331        LHS: &'a Value,
1332        RHS: &'a Value,
1333        Name: *const c_char,
1334    ) -> &'a Value;
1335    pub(crate) fn LLVMBuildExactUDiv<'a>(
1336        B: &Builder<'a>,
1337        LHS: &'a Value,
1338        RHS: &'a Value,
1339        Name: *const c_char,
1340    ) -> &'a Value;
1341    pub(crate) fn LLVMBuildSDiv<'a>(
1342        B: &Builder<'a>,
1343        LHS: &'a Value,
1344        RHS: &'a Value,
1345        Name: *const c_char,
1346    ) -> &'a Value;
1347    pub(crate) fn LLVMBuildExactSDiv<'a>(
1348        B: &Builder<'a>,
1349        LHS: &'a Value,
1350        RHS: &'a Value,
1351        Name: *const c_char,
1352    ) -> &'a Value;
1353    pub(crate) fn LLVMBuildFDiv<'a>(
1354        B: &Builder<'a>,
1355        LHS: &'a Value,
1356        RHS: &'a Value,
1357        Name: *const c_char,
1358    ) -> &'a Value;
1359    pub(crate) fn LLVMBuildURem<'a>(
1360        B: &Builder<'a>,
1361        LHS: &'a Value,
1362        RHS: &'a Value,
1363        Name: *const c_char,
1364    ) -> &'a Value;
1365    pub(crate) fn LLVMBuildSRem<'a>(
1366        B: &Builder<'a>,
1367        LHS: &'a Value,
1368        RHS: &'a Value,
1369        Name: *const c_char,
1370    ) -> &'a Value;
1371    pub(crate) fn LLVMBuildFRem<'a>(
1372        B: &Builder<'a>,
1373        LHS: &'a Value,
1374        RHS: &'a Value,
1375        Name: *const c_char,
1376    ) -> &'a Value;
1377    pub(crate) fn LLVMBuildShl<'a>(
1378        B: &Builder<'a>,
1379        LHS: &'a Value,
1380        RHS: &'a Value,
1381        Name: *const c_char,
1382    ) -> &'a Value;
1383    pub(crate) fn LLVMBuildLShr<'a>(
1384        B: &Builder<'a>,
1385        LHS: &'a Value,
1386        RHS: &'a Value,
1387        Name: *const c_char,
1388    ) -> &'a Value;
1389    pub(crate) fn LLVMBuildAShr<'a>(
1390        B: &Builder<'a>,
1391        LHS: &'a Value,
1392        RHS: &'a Value,
1393        Name: *const c_char,
1394    ) -> &'a Value;
1395    pub(crate) fn LLVMBuildNSWAdd<'a>(
1396        B: &Builder<'a>,
1397        LHS: &'a Value,
1398        RHS: &'a Value,
1399        Name: *const c_char,
1400    ) -> &'a Value;
1401    pub(crate) fn LLVMBuildNUWAdd<'a>(
1402        B: &Builder<'a>,
1403        LHS: &'a Value,
1404        RHS: &'a Value,
1405        Name: *const c_char,
1406    ) -> &'a Value;
1407    pub(crate) fn LLVMBuildNSWSub<'a>(
1408        B: &Builder<'a>,
1409        LHS: &'a Value,
1410        RHS: &'a Value,
1411        Name: *const c_char,
1412    ) -> &'a Value;
1413    pub(crate) fn LLVMBuildNUWSub<'a>(
1414        B: &Builder<'a>,
1415        LHS: &'a Value,
1416        RHS: &'a Value,
1417        Name: *const c_char,
1418    ) -> &'a Value;
1419    pub(crate) fn LLVMBuildNSWMul<'a>(
1420        B: &Builder<'a>,
1421        LHS: &'a Value,
1422        RHS: &'a Value,
1423        Name: *const c_char,
1424    ) -> &'a Value;
1425    pub(crate) fn LLVMBuildNUWMul<'a>(
1426        B: &Builder<'a>,
1427        LHS: &'a Value,
1428        RHS: &'a Value,
1429        Name: *const c_char,
1430    ) -> &'a Value;
1431    pub(crate) fn LLVMBuildAnd<'a>(
1432        B: &Builder<'a>,
1433        LHS: &'a Value,
1434        RHS: &'a Value,
1435        Name: *const c_char,
1436    ) -> &'a Value;
1437    pub(crate) fn LLVMBuildOr<'a>(
1438        B: &Builder<'a>,
1439        LHS: &'a Value,
1440        RHS: &'a Value,
1441        Name: *const c_char,
1442    ) -> &'a Value;
1443    pub(crate) fn LLVMBuildXor<'a>(
1444        B: &Builder<'a>,
1445        LHS: &'a Value,
1446        RHS: &'a Value,
1447        Name: *const c_char,
1448    ) -> &'a Value;
1449    pub(crate) fn LLVMBuildNeg<'a>(B: &Builder<'a>, V: &'a Value, Name: *const c_char)
1450    -> &'a Value;
1451    pub(crate) fn LLVMBuildFNeg<'a>(
1452        B: &Builder<'a>,
1453        V: &'a Value,
1454        Name: *const c_char,
1455    ) -> &'a Value;
1456    pub(crate) fn LLVMBuildNot<'a>(B: &Builder<'a>, V: &'a Value, Name: *const c_char)
1457    -> &'a Value;
1458
1459    // Extra flags on arithmetic
1460    pub(crate) fn LLVMSetIsDisjoint(Instr: &Value, IsDisjoint: Bool);
1461    pub(crate) fn LLVMSetNUW(ArithInst: &Value, HasNUW: Bool);
1462    pub(crate) fn LLVMSetNSW(ArithInst: &Value, HasNSW: Bool);
1463
1464    // Memory
1465    pub(crate) fn LLVMBuildAlloca<'a>(
1466        B: &Builder<'a>,
1467        Ty: &'a Type,
1468        Name: *const c_char,
1469    ) -> &'a Value;
1470    pub(crate) fn LLVMBuildArrayAlloca<'a>(
1471        B: &Builder<'a>,
1472        Ty: &'a Type,
1473        Val: &'a Value,
1474        Name: *const c_char,
1475    ) -> &'a Value;
1476    pub(crate) fn LLVMBuildLoad2<'a>(
1477        B: &Builder<'a>,
1478        Ty: &'a Type,
1479        PointerVal: &'a Value,
1480        Name: *const c_char,
1481    ) -> &'a Value;
1482
1483    pub(crate) fn LLVMBuildStore<'a>(B: &Builder<'a>, Val: &'a Value, Ptr: &'a Value) -> &'a Value;
1484
1485    pub(crate) fn LLVMBuildGEPWithNoWrapFlags<'a>(
1486        B: &Builder<'a>,
1487        Ty: &'a Type,
1488        Pointer: &'a Value,
1489        Indices: *const &'a Value,
1490        NumIndices: c_uint,
1491        Name: *const c_char,
1492        Flags: GEPNoWrapFlags,
1493    ) -> &'a Value;
1494
1495    // Casts
1496    pub(crate) fn LLVMBuildTrunc<'a>(
1497        B: &Builder<'a>,
1498        Val: &'a Value,
1499        DestTy: &'a Type,
1500        Name: *const c_char,
1501    ) -> &'a Value;
1502    pub(crate) fn LLVMBuildZExt<'a>(
1503        B: &Builder<'a>,
1504        Val: &'a Value,
1505        DestTy: &'a Type,
1506        Name: *const c_char,
1507    ) -> &'a Value;
1508    pub(crate) fn LLVMBuildSExt<'a>(
1509        B: &Builder<'a>,
1510        Val: &'a Value,
1511        DestTy: &'a Type,
1512        Name: *const c_char,
1513    ) -> &'a Value;
1514    pub(crate) fn LLVMBuildFPToUI<'a>(
1515        B: &Builder<'a>,
1516        Val: &'a Value,
1517        DestTy: &'a Type,
1518        Name: *const c_char,
1519    ) -> &'a Value;
1520    pub(crate) fn LLVMBuildFPToSI<'a>(
1521        B: &Builder<'a>,
1522        Val: &'a Value,
1523        DestTy: &'a Type,
1524        Name: *const c_char,
1525    ) -> &'a Value;
1526    pub(crate) fn LLVMBuildUIToFP<'a>(
1527        B: &Builder<'a>,
1528        Val: &'a Value,
1529        DestTy: &'a Type,
1530        Name: *const c_char,
1531    ) -> &'a Value;
1532    pub(crate) fn LLVMBuildSIToFP<'a>(
1533        B: &Builder<'a>,
1534        Val: &'a Value,
1535        DestTy: &'a Type,
1536        Name: *const c_char,
1537    ) -> &'a Value;
1538    pub(crate) fn LLVMBuildFPTrunc<'a>(
1539        B: &Builder<'a>,
1540        Val: &'a Value,
1541        DestTy: &'a Type,
1542        Name: *const c_char,
1543    ) -> &'a Value;
1544    pub(crate) fn LLVMBuildFPExt<'a>(
1545        B: &Builder<'a>,
1546        Val: &'a Value,
1547        DestTy: &'a Type,
1548        Name: *const c_char,
1549    ) -> &'a Value;
1550    pub(crate) fn LLVMBuildPtrToInt<'a>(
1551        B: &Builder<'a>,
1552        Val: &'a Value,
1553        DestTy: &'a Type,
1554        Name: *const c_char,
1555    ) -> &'a Value;
1556    pub(crate) fn LLVMBuildIntToPtr<'a>(
1557        B: &Builder<'a>,
1558        Val: &'a Value,
1559        DestTy: &'a Type,
1560        Name: *const c_char,
1561    ) -> &'a Value;
1562    pub(crate) fn LLVMBuildBitCast<'a>(
1563        B: &Builder<'a>,
1564        Val: &'a Value,
1565        DestTy: &'a Type,
1566        Name: *const c_char,
1567    ) -> &'a Value;
1568    pub(crate) fn LLVMBuildPointerCast<'a>(
1569        B: &Builder<'a>,
1570        Val: &'a Value,
1571        DestTy: &'a Type,
1572        Name: *const c_char,
1573    ) -> &'a Value;
1574    pub(crate) fn LLVMBuildIntCast2<'a>(
1575        B: &Builder<'a>,
1576        Val: &'a Value,
1577        DestTy: &'a Type,
1578        IsSigned: Bool,
1579        Name: *const c_char,
1580    ) -> &'a Value;
1581
1582    // Comparisons
1583    pub(crate) fn LLVMBuildICmp<'a>(
1584        B: &Builder<'a>,
1585        Op: c_uint,
1586        LHS: &'a Value,
1587        RHS: &'a Value,
1588        Name: *const c_char,
1589    ) -> &'a Value;
1590    pub(crate) fn LLVMBuildFCmp<'a>(
1591        B: &Builder<'a>,
1592        Op: c_uint,
1593        LHS: &'a Value,
1594        RHS: &'a Value,
1595        Name: *const c_char,
1596    ) -> &'a Value;
1597
1598    // Miscellaneous instructions
1599    pub(crate) fn LLVMBuildPhi<'a>(B: &Builder<'a>, Ty: &'a Type, Name: *const c_char)
1600    -> &'a Value;
1601    pub(crate) fn LLVMBuildSelect<'a>(
1602        B: &Builder<'a>,
1603        If: &'a Value,
1604        Then: &'a Value,
1605        Else: &'a Value,
1606        Name: *const c_char,
1607    ) -> &'a Value;
1608    pub(crate) fn LLVMBuildVAArg<'a>(
1609        B: &Builder<'a>,
1610        list: &'a Value,
1611        Ty: &'a Type,
1612        Name: *const c_char,
1613    ) -> &'a Value;
1614    pub(crate) fn LLVMBuildExtractElement<'a>(
1615        B: &Builder<'a>,
1616        VecVal: &'a Value,
1617        Index: &'a Value,
1618        Name: *const c_char,
1619    ) -> &'a Value;
1620    pub(crate) fn LLVMBuildInsertElement<'a>(
1621        B: &Builder<'a>,
1622        VecVal: &'a Value,
1623        EltVal: &'a Value,
1624        Index: &'a Value,
1625        Name: *const c_char,
1626    ) -> &'a Value;
1627    pub(crate) fn LLVMBuildShuffleVector<'a>(
1628        B: &Builder<'a>,
1629        V1: &'a Value,
1630        V2: &'a Value,
1631        Mask: &'a Value,
1632        Name: *const c_char,
1633    ) -> &'a Value;
1634    pub(crate) fn LLVMBuildExtractValue<'a>(
1635        B: &Builder<'a>,
1636        AggVal: &'a Value,
1637        Index: c_uint,
1638        Name: *const c_char,
1639    ) -> &'a Value;
1640    pub(crate) fn LLVMBuildInsertValue<'a>(
1641        B: &Builder<'a>,
1642        AggVal: &'a Value,
1643        EltVal: &'a Value,
1644        Index: c_uint,
1645        Name: *const c_char,
1646    ) -> &'a Value;
1647
1648    // Atomic Operations
1649    pub(crate) fn LLVMBuildAtomicCmpXchg<'a>(
1650        B: &Builder<'a>,
1651        LHS: &'a Value,
1652        CMP: &'a Value,
1653        RHS: &'a Value,
1654        Order: AtomicOrdering,
1655        FailureOrder: AtomicOrdering,
1656        SingleThreaded: Bool,
1657    ) -> &'a Value;
1658
1659    pub(crate) fn LLVMSetWeak(CmpXchgInst: &Value, IsWeak: Bool);
1660
1661    pub(crate) fn LLVMBuildAtomicRMW<'a>(
1662        B: &Builder<'a>,
1663        Op: AtomicRmwBinOp,
1664        LHS: &'a Value,
1665        RHS: &'a Value,
1666        Order: AtomicOrdering,
1667        SingleThreaded: Bool,
1668    ) -> &'a Value;
1669
1670    pub(crate) fn LLVMBuildFence<'a>(
1671        B: &Builder<'a>,
1672        Order: AtomicOrdering,
1673        SingleThreaded: Bool,
1674        Name: *const c_char,
1675    ) -> &'a Value;
1676
1677    /// Writes a module to the specified path. Returns 0 on success.
1678    pub(crate) fn LLVMWriteBitcodeToFile(M: &Module, Path: *const c_char) -> c_int;
1679
1680    /// Creates a legacy pass manager -- only used for final codegen.
1681    pub(crate) fn LLVMCreatePassManager<'a>() -> &'a mut PassManager<'a>;
1682
1683    pub(crate) fn LLVMAddAnalysisPasses<'a>(T: &'a TargetMachine, PM: &PassManager<'a>);
1684
1685    pub(crate) fn LLVMGetHostCPUFeatures() -> *mut c_char;
1686
1687    pub(crate) fn LLVMDisposeMessage(message: *mut c_char);
1688
1689    pub(crate) fn LLVMIsMultithreaded() -> Bool;
1690
1691    pub(crate) fn LLVMStructCreateNamed(C: &Context, Name: *const c_char) -> &Type;
1692
1693    pub(crate) fn LLVMStructSetBody<'a>(
1694        StructTy: &'a Type,
1695        ElementTypes: *const &'a Type,
1696        ElementCount: c_uint,
1697        Packed: Bool,
1698    );
1699
1700    pub(crate) safe fn LLVMMetadataAsValue<'a>(C: &'a Context, MD: &'a Metadata) -> &'a Value;
1701
1702    pub(crate) fn LLVMSetUnnamedAddress(Global: &Value, UnnamedAddr: UnnamedAddr);
1703
1704    pub(crate) fn LLVMIsAConstantInt(value_ref: &Value) -> Option<&ConstantInt>;
1705
1706    pub(crate) fn LLVMGetOrInsertComdat(M: &Module, Name: *const c_char) -> &Comdat;
1707    pub(crate) fn LLVMSetComdat(V: &Value, C: &Comdat);
1708
1709    pub(crate) fn LLVMCreateOperandBundle(
1710        Tag: *const c_char,
1711        TagLen: size_t,
1712        Args: *const &'_ Value,
1713        NumArgs: c_uint,
1714    ) -> *mut OperandBundle<'_>;
1715    pub(crate) fn LLVMDisposeOperandBundle(Bundle: ptr::NonNull<OperandBundle<'_>>);
1716
1717    pub(crate) fn LLVMBuildCallWithOperandBundles<'a>(
1718        B: &Builder<'a>,
1719        Ty: &'a Type,
1720        Fn: &'a Value,
1721        Args: *const &'a Value,
1722        NumArgs: c_uint,
1723        Bundles: *const &OperandBundle<'a>,
1724        NumBundles: c_uint,
1725        Name: *const c_char,
1726    ) -> &'a Value;
1727    pub(crate) fn LLVMBuildInvokeWithOperandBundles<'a>(
1728        B: &Builder<'a>,
1729        Ty: &'a Type,
1730        Fn: &'a Value,
1731        Args: *const &'a Value,
1732        NumArgs: c_uint,
1733        Then: &'a BasicBlock,
1734        Catch: &'a BasicBlock,
1735        Bundles: *const &OperandBundle<'a>,
1736        NumBundles: c_uint,
1737        Name: *const c_char,
1738    ) -> &'a Value;
1739    pub(crate) fn LLVMBuildCallBr<'a>(
1740        B: &Builder<'a>,
1741        Ty: &'a Type,
1742        Fn: &'a Value,
1743        DefaultDest: &'a BasicBlock,
1744        IndirectDests: *const &'a BasicBlock,
1745        NumIndirectDests: c_uint,
1746        Args: *const &'a Value,
1747        NumArgs: c_uint,
1748        Bundles: *const &OperandBundle<'a>,
1749        NumBundles: c_uint,
1750        Name: *const c_char,
1751    ) -> &'a Value;
1752}
1753
1754// FFI bindings for `DIBuilder` functions in the LLVM-C API.
1755// Try to keep these in the same order as in `llvm/include/llvm-c/DebugInfo.h`.
1756//
1757// FIXME(#134001): Audit all `Option` parameters, especially in lists, to check
1758// that they really are nullable on the C/C++ side. LLVM doesn't appear to
1759// actually document which ones are nullable.
1760unsafe extern "C" {
1761    pub(crate) fn LLVMCreateDIBuilder<'ll>(M: &'ll Module) -> *mut DIBuilder<'ll>;
1762    pub(crate) fn LLVMDisposeDIBuilder<'ll>(Builder: ptr::NonNull<DIBuilder<'ll>>);
1763
1764    pub(crate) fn LLVMDIBuilderFinalize<'ll>(Builder: &DIBuilder<'ll>);
1765
1766    pub(crate) fn LLVMDIBuilderCreateNameSpace<'ll>(
1767        Builder: &DIBuilder<'ll>,
1768        ParentScope: Option<&'ll Metadata>,
1769        Name: *const c_uchar,
1770        NameLen: size_t,
1771        ExportSymbols: llvm::Bool,
1772    ) -> &'ll Metadata;
1773
1774    pub(crate) fn LLVMDIBuilderCreateLexicalBlock<'ll>(
1775        Builder: &DIBuilder<'ll>,
1776        Scope: &'ll Metadata,
1777        File: &'ll Metadata,
1778        Line: c_uint,
1779        Column: c_uint,
1780    ) -> &'ll Metadata;
1781
1782    pub(crate) fn LLVMDIBuilderCreateLexicalBlockFile<'ll>(
1783        Builder: &DIBuilder<'ll>,
1784        Scope: &'ll Metadata,
1785        File: &'ll Metadata,
1786        Discriminator: c_uint, // (optional "DWARF path discriminator"; default is 0)
1787    ) -> &'ll Metadata;
1788
1789    pub(crate) fn LLVMDIBuilderCreateDebugLocation<'ll>(
1790        Ctx: &'ll Context,
1791        Line: c_uint,
1792        Column: c_uint,
1793        Scope: &'ll Metadata,
1794        InlinedAt: Option<&'ll Metadata>,
1795    ) -> &'ll Metadata;
1796}
1797
1798#[link(name = "llvm-wrapper", kind = "static")]
1799unsafe extern "C" {
1800    pub(crate) fn LLVMRustInstallErrorHandlers();
1801    pub(crate) fn LLVMRustDisableSystemDialogsOnCrash();
1802
1803    // Create and destroy contexts.
1804    pub(crate) fn LLVMRustContextCreate(shouldDiscardNames: bool) -> &'static mut Context;
1805
1806    /// See llvm::LLVMTypeKind::getTypeID.
1807    pub(crate) fn LLVMRustGetTypeKind(Ty: &Type) -> TypeKind;
1808
1809    // Operations on all values
1810    pub(crate) fn LLVMRustGlobalAddMetadata<'a>(
1811        Val: &'a Value,
1812        KindID: c_uint,
1813        Metadata: &'a Metadata,
1814    );
1815    pub(crate) fn LLVMRustIsNonGVFunctionPointerTy(Val: &Value) -> bool;
1816
1817    // Operations on scalar constants
1818    pub(crate) fn LLVMRustConstIntGetZExtValue(ConstantVal: &ConstantInt, Value: &mut u64) -> bool;
1819    pub(crate) fn LLVMRustConstInt128Get(
1820        ConstantVal: &ConstantInt,
1821        SExt: bool,
1822        high: &mut u64,
1823        low: &mut u64,
1824    ) -> bool;
1825
1826    // Operations on global variables, functions, and aliases (globals)
1827    pub(crate) fn LLVMRustSetDSOLocal(Global: &Value, is_dso_local: bool);
1828
1829    // Operations on global variables
1830    pub(crate) fn LLVMRustGetOrInsertGlobal<'a>(
1831        M: &'a Module,
1832        Name: *const c_char,
1833        NameLen: size_t,
1834        T: &'a Type,
1835    ) -> &'a Value;
1836    pub(crate) fn LLVMRustInsertPrivateGlobal<'a>(M: &'a Module, T: &'a Type) -> &'a Value;
1837    pub(crate) fn LLVMRustGetNamedValue(
1838        M: &Module,
1839        Name: *const c_char,
1840        NameLen: size_t,
1841    ) -> Option<&Value>;
1842
1843    // Operations on attributes
1844    pub(crate) fn LLVMRustCreateAttrNoValue(C: &Context, attr: AttributeKind) -> &Attribute;
1845    pub(crate) fn LLVMRustCreateAlignmentAttr(C: &Context, bytes: u64) -> &Attribute;
1846    pub(crate) fn LLVMRustCreateDereferenceableAttr(C: &Context, bytes: u64) -> &Attribute;
1847    pub(crate) fn LLVMRustCreateDereferenceableOrNullAttr(C: &Context, bytes: u64) -> &Attribute;
1848    pub(crate) fn LLVMRustCreateByValAttr<'a>(C: &'a Context, ty: &'a Type) -> &'a Attribute;
1849    pub(crate) fn LLVMRustCreateStructRetAttr<'a>(C: &'a Context, ty: &'a Type) -> &'a Attribute;
1850    pub(crate) fn LLVMRustCreateElementTypeAttr<'a>(C: &'a Context, ty: &'a Type) -> &'a Attribute;
1851    pub(crate) fn LLVMRustCreateUWTableAttr(C: &Context, async_: bool) -> &Attribute;
1852    pub(crate) fn LLVMRustCreateAllocSizeAttr(C: &Context, size_arg: u32) -> &Attribute;
1853    pub(crate) fn LLVMRustCreateAllocKindAttr(C: &Context, size_arg: u64) -> &Attribute;
1854    pub(crate) fn LLVMRustCreateMemoryEffectsAttr(
1855        C: &Context,
1856        effects: MemoryEffects,
1857    ) -> &Attribute;
1858    pub(crate) fn LLVMRustCreateRangeAttribute(
1859        C: &Context,
1860        num_bits: c_uint,
1861        lower_words: *const u64,
1862        upper_words: *const u64,
1863    ) -> &Attribute;
1864
1865    // Operations on functions
1866    pub(crate) fn LLVMRustGetOrInsertFunction<'a>(
1867        M: &'a Module,
1868        Name: *const c_char,
1869        NameLen: size_t,
1870        FunctionTy: &'a Type,
1871    ) -> &'a Value;
1872    pub(crate) fn LLVMRustAddFunctionAttributes<'a>(
1873        Fn: &'a Value,
1874        index: c_uint,
1875        Attrs: *const &'a Attribute,
1876        AttrsLen: size_t,
1877    );
1878
1879    // Operations on call sites
1880    pub(crate) fn LLVMRustAddCallSiteAttributes<'a>(
1881        Instr: &'a Value,
1882        index: c_uint,
1883        Attrs: *const &'a Attribute,
1884        AttrsLen: size_t,
1885    );
1886
1887    pub(crate) fn LLVMRustSetFastMath(Instr: &Value);
1888    pub(crate) fn LLVMRustSetAlgebraicMath(Instr: &Value);
1889    pub(crate) fn LLVMRustSetAllowReassoc(Instr: &Value);
1890
1891    // Miscellaneous instructions
1892    pub(crate) fn LLVMRustBuildMemCpy<'a>(
1893        B: &Builder<'a>,
1894        Dst: &'a Value,
1895        DstAlign: c_uint,
1896        Src: &'a Value,
1897        SrcAlign: c_uint,
1898        Size: &'a Value,
1899        IsVolatile: bool,
1900    ) -> &'a Value;
1901    pub(crate) fn LLVMRustBuildMemMove<'a>(
1902        B: &Builder<'a>,
1903        Dst: &'a Value,
1904        DstAlign: c_uint,
1905        Src: &'a Value,
1906        SrcAlign: c_uint,
1907        Size: &'a Value,
1908        IsVolatile: bool,
1909    ) -> &'a Value;
1910    pub(crate) fn LLVMRustBuildMemSet<'a>(
1911        B: &Builder<'a>,
1912        Dst: &'a Value,
1913        DstAlign: c_uint,
1914        Val: &'a Value,
1915        Size: &'a Value,
1916        IsVolatile: bool,
1917    ) -> &'a Value;
1918
1919    pub(crate) fn LLVMRustBuildVectorReduceFAdd<'a>(
1920        B: &Builder<'a>,
1921        Acc: &'a Value,
1922        Src: &'a Value,
1923    ) -> &'a Value;
1924    pub(crate) fn LLVMRustBuildVectorReduceFMul<'a>(
1925        B: &Builder<'a>,
1926        Acc: &'a Value,
1927        Src: &'a Value,
1928    ) -> &'a Value;
1929    pub(crate) fn LLVMRustBuildVectorReduceAdd<'a>(B: &Builder<'a>, Src: &'a Value) -> &'a Value;
1930    pub(crate) fn LLVMRustBuildVectorReduceMul<'a>(B: &Builder<'a>, Src: &'a Value) -> &'a Value;
1931    pub(crate) fn LLVMRustBuildVectorReduceAnd<'a>(B: &Builder<'a>, Src: &'a Value) -> &'a Value;
1932    pub(crate) fn LLVMRustBuildVectorReduceOr<'a>(B: &Builder<'a>, Src: &'a Value) -> &'a Value;
1933    pub(crate) fn LLVMRustBuildVectorReduceXor<'a>(B: &Builder<'a>, Src: &'a Value) -> &'a Value;
1934    pub(crate) fn LLVMRustBuildVectorReduceMin<'a>(
1935        B: &Builder<'a>,
1936        Src: &'a Value,
1937        IsSigned: bool,
1938    ) -> &'a Value;
1939    pub(crate) fn LLVMRustBuildVectorReduceMax<'a>(
1940        B: &Builder<'a>,
1941        Src: &'a Value,
1942        IsSigned: bool,
1943    ) -> &'a Value;
1944    pub(crate) fn LLVMRustBuildVectorReduceFMin<'a>(
1945        B: &Builder<'a>,
1946        Src: &'a Value,
1947        IsNaN: bool,
1948    ) -> &'a Value;
1949    pub(crate) fn LLVMRustBuildVectorReduceFMax<'a>(
1950        B: &Builder<'a>,
1951        Src: &'a Value,
1952        IsNaN: bool,
1953    ) -> &'a Value;
1954
1955    pub(crate) fn LLVMRustBuildMinNum<'a>(
1956        B: &Builder<'a>,
1957        LHS: &'a Value,
1958        LHS: &'a Value,
1959    ) -> &'a Value;
1960    pub(crate) fn LLVMRustBuildMaxNum<'a>(
1961        B: &Builder<'a>,
1962        LHS: &'a Value,
1963        LHS: &'a Value,
1964    ) -> &'a Value;
1965
1966    // Atomic Operations
1967    pub(crate) fn LLVMRustBuildAtomicLoad<'a>(
1968        B: &Builder<'a>,
1969        ElementType: &'a Type,
1970        PointerVal: &'a Value,
1971        Name: *const c_char,
1972        Order: AtomicOrdering,
1973    ) -> &'a Value;
1974
1975    pub(crate) fn LLVMRustBuildAtomicStore<'a>(
1976        B: &Builder<'a>,
1977        Val: &'a Value,
1978        Ptr: &'a Value,
1979        Order: AtomicOrdering,
1980    ) -> &'a Value;
1981
1982    pub(crate) fn LLVMRustTimeTraceProfilerInitialize();
1983
1984    pub(crate) fn LLVMRustTimeTraceProfilerFinishThread();
1985
1986    pub(crate) fn LLVMRustTimeTraceProfilerFinish(FileName: *const c_char);
1987
1988    /// Returns a string describing the last error caused by an LLVMRust* call.
1989    pub(crate) fn LLVMRustGetLastError() -> *const c_char;
1990
1991    /// Prints the timing information collected by `-Ztime-llvm-passes`.
1992    pub(crate) fn LLVMRustPrintPassTimings(OutStr: &RustString);
1993
1994    /// Prints the statistics collected by `-Zprint-codegen-stats`.
1995    pub(crate) fn LLVMRustPrintStatistics(OutStr: &RustString);
1996
1997    /// Prepares inline assembly.
1998    pub(crate) fn LLVMRustInlineAsm(
1999        Ty: &Type,
2000        AsmString: *const c_char,
2001        AsmStringLen: size_t,
2002        Constraints: *const c_char,
2003        ConstraintsLen: size_t,
2004        SideEffects: Bool,
2005        AlignStack: Bool,
2006        Dialect: AsmDialect,
2007        CanThrow: Bool,
2008    ) -> &Value;
2009    pub(crate) fn LLVMRustInlineAsmVerify(
2010        Ty: &Type,
2011        Constraints: *const c_char,
2012        ConstraintsLen: size_t,
2013    ) -> bool;
2014
2015    pub(crate) fn LLVMRustCoverageWriteFilenamesToBuffer(
2016        Filenames: *const *const c_char,
2017        FilenamesLen: size_t,
2018        Lengths: *const size_t,
2019        LengthsLen: size_t,
2020        BufferOut: &RustString,
2021    );
2022
2023    pub(crate) fn LLVMRustCoverageWriteFunctionMappingsToBuffer(
2024        VirtualFileMappingIDs: *const c_uint,
2025        NumVirtualFileMappingIDs: size_t,
2026        Expressions: *const crate::coverageinfo::ffi::CounterExpression,
2027        NumExpressions: size_t,
2028        CodeRegions: *const crate::coverageinfo::ffi::CodeRegion,
2029        NumCodeRegions: size_t,
2030        ExpansionRegions: *const crate::coverageinfo::ffi::ExpansionRegion,
2031        NumExpansionRegions: size_t,
2032        BranchRegions: *const crate::coverageinfo::ffi::BranchRegion,
2033        NumBranchRegions: size_t,
2034        MCDCBranchRegions: *const crate::coverageinfo::ffi::MCDCBranchRegion,
2035        NumMCDCBranchRegions: size_t,
2036        MCDCDecisionRegions: *const crate::coverageinfo::ffi::MCDCDecisionRegion,
2037        NumMCDCDecisionRegions: size_t,
2038        BufferOut: &RustString,
2039    );
2040
2041    pub(crate) fn LLVMRustCoverageCreatePGOFuncNameVar(
2042        F: &Value,
2043        FuncName: *const c_char,
2044        FuncNameLen: size_t,
2045    ) -> &Value;
2046    pub(crate) fn LLVMRustCoverageHashBytes(Bytes: *const c_char, NumBytes: size_t) -> u64;
2047
2048    pub(crate) fn LLVMRustCoverageWriteCovmapSectionNameToString(M: &Module, OutStr: &RustString);
2049
2050    pub(crate) fn LLVMRustCoverageWriteCovfunSectionNameToString(M: &Module, OutStr: &RustString);
2051
2052    pub(crate) fn LLVMRustCoverageWriteCovmapVarNameToString(OutStr: &RustString);
2053
2054    pub(crate) fn LLVMRustCoverageMappingVersion() -> u32;
2055    pub(crate) fn LLVMRustDebugMetadataVersion() -> u32;
2056    pub(crate) fn LLVMRustVersionMajor() -> u32;
2057    pub(crate) fn LLVMRustVersionMinor() -> u32;
2058    pub(crate) fn LLVMRustVersionPatch() -> u32;
2059
2060    /// Add LLVM module flags.
2061    ///
2062    /// In order for Rust-C LTO to work, module flags must be compatible with Clang. What
2063    /// "compatible" means depends on the merge behaviors involved.
2064    pub(crate) fn LLVMRustAddModuleFlagU32(
2065        M: &Module,
2066        MergeBehavior: ModuleFlagMergeBehavior,
2067        Name: *const c_char,
2068        NameLen: size_t,
2069        Value: u32,
2070    );
2071
2072    pub(crate) fn LLVMRustAddModuleFlagString(
2073        M: &Module,
2074        MergeBehavior: ModuleFlagMergeBehavior,
2075        Name: *const c_char,
2076        NameLen: size_t,
2077        Value: *const c_char,
2078        ValueLen: size_t,
2079    );
2080
2081    pub(crate) fn LLVMRustDIBuilderCreateCompileUnit<'a>(
2082        Builder: &DIBuilder<'a>,
2083        Lang: c_uint,
2084        File: &'a DIFile,
2085        Producer: *const c_char,
2086        ProducerLen: size_t,
2087        isOptimized: bool,
2088        Flags: *const c_char,
2089        RuntimeVer: c_uint,
2090        SplitName: *const c_char,
2091        SplitNameLen: size_t,
2092        kind: DebugEmissionKind,
2093        DWOId: u64,
2094        SplitDebugInlining: bool,
2095        DebugNameTableKind: DebugNameTableKind,
2096    ) -> &'a DIDescriptor;
2097
2098    pub(crate) fn LLVMRustDIBuilderCreateFile<'a>(
2099        Builder: &DIBuilder<'a>,
2100        Filename: *const c_char,
2101        FilenameLen: size_t,
2102        Directory: *const c_char,
2103        DirectoryLen: size_t,
2104        CSKind: ChecksumKind,
2105        Checksum: *const c_char,
2106        ChecksumLen: size_t,
2107        Source: *const c_char,
2108        SourceLen: size_t,
2109    ) -> &'a DIFile;
2110
2111    pub(crate) fn LLVMRustDIBuilderCreateSubroutineType<'a>(
2112        Builder: &DIBuilder<'a>,
2113        ParameterTypes: &'a DIArray,
2114    ) -> &'a DICompositeType;
2115
2116    pub(crate) fn LLVMRustDIBuilderCreateFunction<'a>(
2117        Builder: &DIBuilder<'a>,
2118        Scope: &'a DIDescriptor,
2119        Name: *const c_char,
2120        NameLen: size_t,
2121        LinkageName: *const c_char,
2122        LinkageNameLen: size_t,
2123        File: &'a DIFile,
2124        LineNo: c_uint,
2125        Ty: &'a DIType,
2126        ScopeLine: c_uint,
2127        Flags: DIFlags,
2128        SPFlags: DISPFlags,
2129        MaybeFn: Option<&'a Value>,
2130        TParam: &'a DIArray,
2131        Decl: Option<&'a DIDescriptor>,
2132    ) -> &'a DISubprogram;
2133
2134    pub(crate) fn LLVMRustDIBuilderCreateMethod<'a>(
2135        Builder: &DIBuilder<'a>,
2136        Scope: &'a DIDescriptor,
2137        Name: *const c_char,
2138        NameLen: size_t,
2139        LinkageName: *const c_char,
2140        LinkageNameLen: size_t,
2141        File: &'a DIFile,
2142        LineNo: c_uint,
2143        Ty: &'a DIType,
2144        Flags: DIFlags,
2145        SPFlags: DISPFlags,
2146        TParam: &'a DIArray,
2147    ) -> &'a DISubprogram;
2148
2149    pub(crate) fn LLVMRustDIBuilderCreateBasicType<'a>(
2150        Builder: &DIBuilder<'a>,
2151        Name: *const c_char,
2152        NameLen: size_t,
2153        SizeInBits: u64,
2154        Encoding: c_uint,
2155    ) -> &'a DIBasicType;
2156
2157    pub(crate) fn LLVMRustDIBuilderCreateTypedef<'a>(
2158        Builder: &DIBuilder<'a>,
2159        Type: &'a DIBasicType,
2160        Name: *const c_char,
2161        NameLen: size_t,
2162        File: &'a DIFile,
2163        LineNo: c_uint,
2164        Scope: Option<&'a DIScope>,
2165    ) -> &'a DIDerivedType;
2166
2167    pub(crate) fn LLVMRustDIBuilderCreatePointerType<'a>(
2168        Builder: &DIBuilder<'a>,
2169        PointeeTy: &'a DIType,
2170        SizeInBits: u64,
2171        AlignInBits: u32,
2172        AddressSpace: c_uint,
2173        Name: *const c_char,
2174        NameLen: size_t,
2175    ) -> &'a DIDerivedType;
2176
2177    pub(crate) fn LLVMRustDIBuilderCreateStructType<'a>(
2178        Builder: &DIBuilder<'a>,
2179        Scope: Option<&'a DIDescriptor>,
2180        Name: *const c_char,
2181        NameLen: size_t,
2182        File: &'a DIFile,
2183        LineNumber: c_uint,
2184        SizeInBits: u64,
2185        AlignInBits: u32,
2186        Flags: DIFlags,
2187        DerivedFrom: Option<&'a DIType>,
2188        Elements: &'a DIArray,
2189        RunTimeLang: c_uint,
2190        VTableHolder: Option<&'a DIType>,
2191        UniqueId: *const c_char,
2192        UniqueIdLen: size_t,
2193    ) -> &'a DICompositeType;
2194
2195    pub(crate) fn LLVMRustDIBuilderCreateMemberType<'a>(
2196        Builder: &DIBuilder<'a>,
2197        Scope: &'a DIDescriptor,
2198        Name: *const c_char,
2199        NameLen: size_t,
2200        File: &'a DIFile,
2201        LineNo: c_uint,
2202        SizeInBits: u64,
2203        AlignInBits: u32,
2204        OffsetInBits: u64,
2205        Flags: DIFlags,
2206        Ty: &'a DIType,
2207    ) -> &'a DIDerivedType;
2208
2209    pub(crate) fn LLVMRustDIBuilderCreateVariantMemberType<'a>(
2210        Builder: &DIBuilder<'a>,
2211        Scope: &'a DIScope,
2212        Name: *const c_char,
2213        NameLen: size_t,
2214        File: &'a DIFile,
2215        LineNumber: c_uint,
2216        SizeInBits: u64,
2217        AlignInBits: u32,
2218        OffsetInBits: u64,
2219        Discriminant: Option<&'a Value>,
2220        Flags: DIFlags,
2221        Ty: &'a DIType,
2222    ) -> &'a DIType;
2223
2224    pub(crate) fn LLVMRustDIBuilderCreateStaticMemberType<'a>(
2225        Builder: &DIBuilder<'a>,
2226        Scope: &'a DIDescriptor,
2227        Name: *const c_char,
2228        NameLen: size_t,
2229        File: &'a DIFile,
2230        LineNo: c_uint,
2231        Ty: &'a DIType,
2232        Flags: DIFlags,
2233        val: Option<&'a Value>,
2234        AlignInBits: u32,
2235    ) -> &'a DIDerivedType;
2236
2237    pub(crate) fn LLVMRustDIBuilderCreateQualifiedType<'a>(
2238        Builder: &DIBuilder<'a>,
2239        Tag: c_uint,
2240        Type: &'a DIType,
2241    ) -> &'a DIDerivedType;
2242
2243    pub(crate) fn LLVMRustDIBuilderCreateStaticVariable<'a>(
2244        Builder: &DIBuilder<'a>,
2245        Context: Option<&'a DIScope>,
2246        Name: *const c_char,
2247        NameLen: size_t,
2248        LinkageName: *const c_char,
2249        LinkageNameLen: size_t,
2250        File: &'a DIFile,
2251        LineNo: c_uint,
2252        Ty: &'a DIType,
2253        isLocalToUnit: bool,
2254        Val: &'a Value,
2255        Decl: Option<&'a DIDescriptor>,
2256        AlignInBits: u32,
2257    ) -> &'a DIGlobalVariableExpression;
2258
2259    pub(crate) fn LLVMRustDIBuilderCreateVariable<'a>(
2260        Builder: &DIBuilder<'a>,
2261        Tag: c_uint,
2262        Scope: &'a DIDescriptor,
2263        Name: *const c_char,
2264        NameLen: size_t,
2265        File: &'a DIFile,
2266        LineNo: c_uint,
2267        Ty: &'a DIType,
2268        AlwaysPreserve: bool,
2269        Flags: DIFlags,
2270        ArgNo: c_uint,
2271        AlignInBits: u32,
2272    ) -> &'a DIVariable;
2273
2274    pub(crate) fn LLVMRustDIBuilderCreateArrayType<'a>(
2275        Builder: &DIBuilder<'a>,
2276        Size: u64,
2277        AlignInBits: u32,
2278        Ty: &'a DIType,
2279        Subscripts: &'a DIArray,
2280    ) -> &'a DIType;
2281
2282    pub(crate) fn LLVMRustDIBuilderGetOrCreateSubrange<'a>(
2283        Builder: &DIBuilder<'a>,
2284        Lo: i64,
2285        Count: i64,
2286    ) -> &'a DISubrange;
2287
2288    pub(crate) fn LLVMRustDIBuilderGetOrCreateArray<'a>(
2289        Builder: &DIBuilder<'a>,
2290        Ptr: *const Option<&'a DIDescriptor>,
2291        Count: c_uint,
2292    ) -> &'a DIArray;
2293
2294    pub(crate) fn LLVMRustDIBuilderInsertDeclareAtEnd<'a>(
2295        Builder: &DIBuilder<'a>,
2296        Val: &'a Value,
2297        VarInfo: &'a DIVariable,
2298        AddrOps: *const u64,
2299        AddrOpsCount: c_uint,
2300        DL: &'a DILocation,
2301        InsertAtEnd: &'a BasicBlock,
2302    );
2303
2304    pub(crate) fn LLVMRustDIBuilderCreateEnumerator<'a>(
2305        Builder: &DIBuilder<'a>,
2306        Name: *const c_char,
2307        NameLen: size_t,
2308        Value: *const u64,
2309        SizeInBits: c_uint,
2310        IsUnsigned: bool,
2311    ) -> &'a DIEnumerator;
2312
2313    pub(crate) fn LLVMRustDIBuilderCreateEnumerationType<'a>(
2314        Builder: &DIBuilder<'a>,
2315        Scope: &'a DIScope,
2316        Name: *const c_char,
2317        NameLen: size_t,
2318        File: &'a DIFile,
2319        LineNumber: c_uint,
2320        SizeInBits: u64,
2321        AlignInBits: u32,
2322        Elements: &'a DIArray,
2323        ClassType: &'a DIType,
2324        IsScoped: bool,
2325    ) -> &'a DIType;
2326
2327    pub(crate) fn LLVMRustDIBuilderCreateUnionType<'a>(
2328        Builder: &DIBuilder<'a>,
2329        Scope: Option<&'a DIScope>,
2330        Name: *const c_char,
2331        NameLen: size_t,
2332        File: &'a DIFile,
2333        LineNumber: c_uint,
2334        SizeInBits: u64,
2335        AlignInBits: u32,
2336        Flags: DIFlags,
2337        Elements: Option<&'a DIArray>,
2338        RunTimeLang: c_uint,
2339        UniqueId: *const c_char,
2340        UniqueIdLen: size_t,
2341    ) -> &'a DIType;
2342
2343    pub(crate) fn LLVMRustDIBuilderCreateVariantPart<'a>(
2344        Builder: &DIBuilder<'a>,
2345        Scope: &'a DIScope,
2346        Name: *const c_char,
2347        NameLen: size_t,
2348        File: &'a DIFile,
2349        LineNo: c_uint,
2350        SizeInBits: u64,
2351        AlignInBits: u32,
2352        Flags: DIFlags,
2353        Discriminator: Option<&'a DIDerivedType>,
2354        Elements: &'a DIArray,
2355        UniqueId: *const c_char,
2356        UniqueIdLen: size_t,
2357    ) -> &'a DIDerivedType;
2358
2359    pub(crate) fn LLVMRustDIBuilderCreateTemplateTypeParameter<'a>(
2360        Builder: &DIBuilder<'a>,
2361        Scope: Option<&'a DIScope>,
2362        Name: *const c_char,
2363        NameLen: size_t,
2364        Ty: &'a DIType,
2365    ) -> &'a DITemplateTypeParameter;
2366
2367    pub(crate) fn LLVMRustDICompositeTypeReplaceArrays<'a>(
2368        Builder: &DIBuilder<'a>,
2369        CompositeType: &'a DIType,
2370        Elements: Option<&'a DIArray>,
2371        Params: Option<&'a DIArray>,
2372    );
2373
2374    pub(crate) fn LLVMRustDILocationCloneWithBaseDiscriminator<'a>(
2375        Location: &'a DILocation,
2376        BD: c_uint,
2377    ) -> Option<&'a DILocation>;
2378
2379    pub(crate) fn LLVMRustWriteTypeToString(Type: &Type, s: &RustString);
2380    pub(crate) fn LLVMRustWriteValueToString(value_ref: &Value, s: &RustString);
2381
2382    pub(crate) fn LLVMRustHasFeature(T: &TargetMachine, s: *const c_char) -> bool;
2383
2384    pub(crate) fn LLVMRustPrintTargetCPUs(TM: &TargetMachine, OutStr: &RustString);
2385    pub(crate) fn LLVMRustGetTargetFeaturesCount(T: &TargetMachine) -> size_t;
2386    pub(crate) fn LLVMRustGetTargetFeature(
2387        T: &TargetMachine,
2388        Index: size_t,
2389        Feature: &mut *const c_char,
2390        Desc: &mut *const c_char,
2391    );
2392
2393    pub(crate) fn LLVMRustGetHostCPUName(LenOut: &mut size_t) -> *const u8;
2394
2395    // This function makes copies of pointed to data, so the data's lifetime may end after this
2396    // function returns.
2397    pub(crate) fn LLVMRustCreateTargetMachine(
2398        Triple: *const c_char,
2399        CPU: *const c_char,
2400        Features: *const c_char,
2401        Abi: *const c_char,
2402        Model: CodeModel,
2403        Reloc: RelocModel,
2404        Level: CodeGenOptLevel,
2405        FloatABIType: FloatAbi,
2406        FunctionSections: bool,
2407        DataSections: bool,
2408        UniqueSectionNames: bool,
2409        TrapUnreachable: bool,
2410        Singlethread: bool,
2411        VerboseAsm: bool,
2412        EmitStackSizeSection: bool,
2413        RelaxELFRelocations: bool,
2414        UseInitArray: bool,
2415        SplitDwarfFile: *const c_char,
2416        OutputObjFile: *const c_char,
2417        DebugInfoCompression: *const c_char,
2418        UseEmulatedTls: bool,
2419        ArgsCstrBuff: *const c_char,
2420        ArgsCstrBuffLen: usize,
2421    ) -> *mut TargetMachine;
2422
2423    pub(crate) fn LLVMRustDisposeTargetMachine(T: *mut TargetMachine);
2424    pub(crate) fn LLVMRustAddLibraryInfo<'a>(
2425        PM: &PassManager<'a>,
2426        M: &'a Module,
2427        DisableSimplifyLibCalls: bool,
2428    );
2429    pub(crate) fn LLVMRustWriteOutputFile<'a>(
2430        T: &'a TargetMachine,
2431        PM: *mut PassManager<'a>,
2432        M: &'a Module,
2433        Output: *const c_char,
2434        DwoOutput: *const c_char,
2435        FileType: FileType,
2436        VerifyIR: bool,
2437    ) -> LLVMRustResult;
2438    pub(crate) fn LLVMRustOptimize<'a>(
2439        M: &'a Module,
2440        TM: &'a TargetMachine,
2441        OptLevel: PassBuilderOptLevel,
2442        OptStage: OptStage,
2443        IsLinkerPluginLTO: bool,
2444        NoPrepopulatePasses: bool,
2445        VerifyIR: bool,
2446        LintIR: bool,
2447        ThinLTOBuffer: Option<&mut *mut ThinLTOBuffer>,
2448        EmitThinLTO: bool,
2449        EmitThinLTOSummary: bool,
2450        MergeFunctions: bool,
2451        UnrollLoops: bool,
2452        SLPVectorize: bool,
2453        LoopVectorize: bool,
2454        DisableSimplifyLibCalls: bool,
2455        EmitLifetimeMarkers: bool,
2456        RunEnzyme: bool,
2457        SanitizerOptions: Option<&SanitizerOptions>,
2458        PGOGenPath: *const c_char,
2459        PGOUsePath: *const c_char,
2460        InstrumentCoverage: bool,
2461        InstrProfileOutput: *const c_char,
2462        PGOSampleUsePath: *const c_char,
2463        DebugInfoForProfiling: bool,
2464        llvm_selfprofiler: *mut c_void,
2465        begin_callback: SelfProfileBeforePassCallback,
2466        end_callback: SelfProfileAfterPassCallback,
2467        ExtraPasses: *const c_char,
2468        ExtraPassesLen: size_t,
2469        LLVMPlugins: *const c_char,
2470        LLVMPluginsLen: size_t,
2471    ) -> LLVMRustResult;
2472    pub(crate) fn LLVMRustPrintModule(
2473        M: &Module,
2474        Output: *const c_char,
2475        Demangle: extern "C" fn(*const c_char, size_t, *mut c_char, size_t) -> size_t,
2476    ) -> LLVMRustResult;
2477    pub(crate) fn LLVMRustSetLLVMOptions(Argc: c_int, Argv: *const *const c_char);
2478    pub(crate) fn LLVMRustPrintPasses();
2479    pub(crate) fn LLVMRustSetNormalizedTarget(M: &Module, triple: *const c_char);
2480    pub(crate) fn LLVMRustRunRestrictionPass(M: &Module, syms: *const *const c_char, len: size_t);
2481
2482    pub(crate) fn LLVMRustOpenArchive(path: *const c_char) -> Option<&'static mut Archive>;
2483    pub(crate) fn LLVMRustArchiveIteratorNew(AR: &Archive) -> &mut ArchiveIterator<'_>;
2484    pub(crate) fn LLVMRustArchiveIteratorNext<'a>(
2485        AIR: &ArchiveIterator<'a>,
2486    ) -> Option<&'a mut ArchiveChild<'a>>;
2487    pub(crate) fn LLVMRustArchiveChildName(
2488        ACR: &ArchiveChild<'_>,
2489        size: &mut size_t,
2490    ) -> *const c_char;
2491    pub(crate) fn LLVMRustArchiveChildFree<'a>(ACR: &'a mut ArchiveChild<'a>);
2492    pub(crate) fn LLVMRustArchiveIteratorFree<'a>(AIR: &'a mut ArchiveIterator<'a>);
2493    pub(crate) fn LLVMRustDestroyArchive(AR: &'static mut Archive);
2494
2495    pub(crate) fn LLVMRustWriteTwineToString(T: &Twine, s: &RustString);
2496
2497    pub(crate) fn LLVMRustUnpackOptimizationDiagnostic<'a>(
2498        DI: &'a DiagnosticInfo,
2499        pass_name_out: &RustString,
2500        function_out: &mut Option<&'a Value>,
2501        loc_line_out: &mut c_uint,
2502        loc_column_out: &mut c_uint,
2503        loc_filename_out: &RustString,
2504        message_out: &RustString,
2505    );
2506
2507    pub(crate) fn LLVMRustUnpackInlineAsmDiagnostic<'a>(
2508        DI: &'a DiagnosticInfo,
2509        level_out: &mut DiagnosticLevel,
2510        cookie_out: &mut u64,
2511        message_out: &mut Option<&'a Twine>,
2512    );
2513
2514    pub(crate) fn LLVMRustWriteDiagnosticInfoToString(DI: &DiagnosticInfo, s: &RustString);
2515    pub(crate) fn LLVMRustGetDiagInfoKind(DI: &DiagnosticInfo) -> DiagnosticKind;
2516
2517    pub(crate) fn LLVMRustGetSMDiagnostic<'a>(
2518        DI: &'a DiagnosticInfo,
2519        cookie_out: &mut u64,
2520    ) -> &'a SMDiagnostic;
2521
2522    pub(crate) fn LLVMRustUnpackSMDiagnostic(
2523        d: &SMDiagnostic,
2524        message_out: &RustString,
2525        buffer_out: &RustString,
2526        level_out: &mut DiagnosticLevel,
2527        loc_out: &mut c_uint,
2528        ranges_out: *mut c_uint,
2529        num_ranges: &mut usize,
2530    ) -> bool;
2531
2532    pub(crate) fn LLVMRustWriteArchive(
2533        Dst: *const c_char,
2534        NumMembers: size_t,
2535        Members: *const &RustArchiveMember<'_>,
2536        WriteSymbtab: bool,
2537        Kind: ArchiveKind,
2538        isEC: bool,
2539    ) -> LLVMRustResult;
2540    pub(crate) fn LLVMRustArchiveMemberNew<'a>(
2541        Filename: *const c_char,
2542        Name: *const c_char,
2543        Child: Option<&ArchiveChild<'a>>,
2544    ) -> &'a mut RustArchiveMember<'a>;
2545    pub(crate) fn LLVMRustArchiveMemberFree<'a>(Member: &'a mut RustArchiveMember<'a>);
2546
2547    pub(crate) fn LLVMRustSetDataLayoutFromTargetMachine<'a>(M: &'a Module, TM: &'a TargetMachine);
2548
2549    pub(crate) fn LLVMRustPositionBuilderAtStart<'a>(B: &Builder<'a>, BB: &'a BasicBlock);
2550
2551    pub(crate) fn LLVMRustSetModulePICLevel(M: &Module);
2552    pub(crate) fn LLVMRustSetModulePIELevel(M: &Module);
2553    pub(crate) fn LLVMRustSetModuleCodeModel(M: &Module, Model: CodeModel);
2554    pub(crate) fn LLVMRustModuleBufferCreate(M: &Module) -> &'static mut ModuleBuffer;
2555    pub(crate) fn LLVMRustModuleBufferPtr(p: &ModuleBuffer) -> *const u8;
2556    pub(crate) fn LLVMRustModuleBufferLen(p: &ModuleBuffer) -> usize;
2557    pub(crate) fn LLVMRustModuleBufferFree(p: &'static mut ModuleBuffer);
2558    pub(crate) fn LLVMRustModuleCost(M: &Module) -> u64;
2559    pub(crate) fn LLVMRustModuleInstructionStats(M: &Module, Str: &RustString);
2560
2561    pub(crate) fn LLVMRustThinLTOBufferCreate(
2562        M: &Module,
2563        is_thin: bool,
2564        emit_summary: bool,
2565    ) -> &'static mut ThinLTOBuffer;
2566    pub(crate) fn LLVMRustThinLTOBufferFree(M: &'static mut ThinLTOBuffer);
2567    pub(crate) fn LLVMRustThinLTOBufferPtr(M: &ThinLTOBuffer) -> *const c_char;
2568    pub(crate) fn LLVMRustThinLTOBufferLen(M: &ThinLTOBuffer) -> size_t;
2569    pub(crate) fn LLVMRustThinLTOBufferThinLinkDataPtr(M: &ThinLTOBuffer) -> *const c_char;
2570    pub(crate) fn LLVMRustThinLTOBufferThinLinkDataLen(M: &ThinLTOBuffer) -> size_t;
2571    pub(crate) fn LLVMRustCreateThinLTOData(
2572        Modules: *const ThinLTOModule,
2573        NumModules: size_t,
2574        PreservedSymbols: *const *const c_char,
2575        PreservedSymbolsLen: size_t,
2576    ) -> Option<&'static mut ThinLTOData>;
2577    pub(crate) fn LLVMRustPrepareThinLTORename(
2578        Data: &ThinLTOData,
2579        Module: &Module,
2580        Target: &TargetMachine,
2581    );
2582    pub(crate) fn LLVMRustPrepareThinLTOResolveWeak(Data: &ThinLTOData, Module: &Module) -> bool;
2583    pub(crate) fn LLVMRustPrepareThinLTOInternalize(Data: &ThinLTOData, Module: &Module) -> bool;
2584    pub(crate) fn LLVMRustPrepareThinLTOImport(
2585        Data: &ThinLTOData,
2586        Module: &Module,
2587        Target: &TargetMachine,
2588    ) -> bool;
2589    pub(crate) fn LLVMRustFreeThinLTOData(Data: &'static mut ThinLTOData);
2590    pub(crate) fn LLVMRustParseBitcodeForLTO(
2591        Context: &Context,
2592        Data: *const u8,
2593        len: usize,
2594        Identifier: *const c_char,
2595    ) -> Option<&Module>;
2596    pub(crate) fn LLVMRustGetSliceFromObjectDataByName(
2597        data: *const u8,
2598        len: usize,
2599        name: *const u8,
2600        name_len: usize,
2601        out_len: &mut usize,
2602    ) -> *const u8;
2603
2604    pub(crate) fn LLVMRustLinkerNew(M: &Module) -> &mut Linker<'_>;
2605    pub(crate) fn LLVMRustLinkerAdd(
2606        linker: &Linker<'_>,
2607        bytecode: *const c_char,
2608        bytecode_len: usize,
2609    ) -> bool;
2610    pub(crate) fn LLVMRustLinkerFree<'a>(linker: &'a mut Linker<'a>);
2611    pub(crate) fn LLVMRustComputeLTOCacheKey(
2612        key_out: &RustString,
2613        mod_id: *const c_char,
2614        data: &ThinLTOData,
2615    );
2616
2617    pub(crate) fn LLVMRustContextGetDiagnosticHandler(
2618        Context: &Context,
2619    ) -> Option<&DiagnosticHandler>;
2620    pub(crate) fn LLVMRustContextSetDiagnosticHandler(
2621        context: &Context,
2622        diagnostic_handler: Option<&DiagnosticHandler>,
2623    );
2624    pub(crate) fn LLVMRustContextConfigureDiagnosticHandler(
2625        context: &Context,
2626        diagnostic_handler_callback: DiagnosticHandlerTy,
2627        diagnostic_handler_context: *mut c_void,
2628        remark_all_passes: bool,
2629        remark_passes: *const *const c_char,
2630        remark_passes_len: usize,
2631        remark_file: *const c_char,
2632        pgo_available: bool,
2633    );
2634
2635    pub(crate) fn LLVMRustGetMangledName(V: &Value, out: &RustString);
2636
2637    pub(crate) fn LLVMRustGetElementTypeArgIndex(CallSite: &Value) -> i32;
2638
2639    pub(crate) fn LLVMRustLLVMHasZlibCompressionForDebugSymbols() -> bool;
2640
2641    pub(crate) fn LLVMRustLLVMHasZstdCompressionForDebugSymbols() -> bool;
2642
2643    pub(crate) fn LLVMRustGetSymbols(
2644        buf_ptr: *const u8,
2645        buf_len: usize,
2646        state: *mut c_void,
2647        callback: GetSymbolsCallback,
2648        error_callback: GetSymbolsErrorCallback,
2649    ) -> *mut c_void;
2650
2651    pub(crate) fn LLVMRustIs64BitSymbolicFile(buf_ptr: *const u8, buf_len: usize) -> bool;
2652
2653    pub(crate) fn LLVMRustIsECObject(buf_ptr: *const u8, buf_len: usize) -> bool;
2654
2655    pub(crate) fn LLVMRustSetNoSanitizeAddress(Global: &Value);
2656    pub(crate) fn LLVMRustSetNoSanitizeHWAddress(Global: &Value);
2657}