Skip to main content

inkwell/types/
metadata_type.rs

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