Skip to main content

inkwell/types/
metadata_type.rs

1use llvm_sys::prelude::LLVMTypeRef;
2
3use crate::context::ContextRef;
4use crate::support::{LLVMString, assert_niche};
5use crate::types::enums::BasicMetadataTypeEnum;
6use crate::types::traits::AsTypeRef;
7use crate::types::{FunctionType, Type};
8
9use std::fmt::{self, Display};
10
11/// A `MetadataType` is the type of a metadata.
12#[repr(transparent)]
13#[derive(Debug, PartialEq, Eq, Clone, Copy)]
14pub struct MetadataType<'ctx> {
15    metadata_type: Type<'ctx>,
16}
17const _: () = assert_niche::<MetadataType>();
18
19impl<'ctx> MetadataType<'ctx> {
20    /// Create `MetadataType` from [`LLVMTypeRef`]
21    ///
22    /// # Safety
23    /// Undefined behavior, if referenced type isn't metadata type
24    pub unsafe fn new(metadata_type: LLVMTypeRef) -> Self {
25        unsafe {
26            assert!(!metadata_type.is_null());
27
28            MetadataType {
29                metadata_type: Type::new(metadata_type),
30            }
31        }
32    }
33
34    /// Creates a `FunctionType` with this `MetadataType` for its return type.
35    ///
36    /// # Example
37    ///
38    /// ```no_run
39    /// use inkwell::context::Context;
40    ///
41    /// let context = Context::create();
42    /// let md_type = context.metadata_type();
43    /// let fn_type = md_type.fn_type(&[], false);
44    /// ```
45    pub fn fn_type(self, param_types: &[BasicMetadataTypeEnum<'ctx>], is_var_args: bool) -> FunctionType<'ctx> {
46        self.metadata_type.fn_type(param_types, is_var_args)
47    }
48
49    /// Gets a reference to the `Context` this `MetadataType` was created in.
50    ///
51    /// # Example
52    ///
53    /// ```no_run
54    /// use inkwell::context::Context;
55    ///
56    /// let context = Context::create();
57    /// let md_type = context.metadata_type();
58    ///
59    /// assert_eq!(md_type.get_context(), context);
60    /// ```
61    pub fn get_context(self) -> ContextRef<'ctx> {
62        self.metadata_type.get_context()
63    }
64
65    /// Print the definition of a `MetadataType` to `LLVMString`.
66    pub fn print_to_string(self) -> LLVMString {
67        self.metadata_type.print_to_string()
68    }
69}
70
71unsafe impl AsTypeRef for MetadataType<'_> {
72    fn as_type_ref(&self) -> LLVMTypeRef {
73        self.metadata_type.as_mut_ptr()
74    }
75}
76
77impl Display for MetadataType<'_> {
78    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79        write!(f, "{}", self.print_to_string())
80    }
81}