[go: up one dir, main page]

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