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    #[llvm_versions(6..)]
23    pub unsafe fn new(metadata_type: LLVMTypeRef) -> Self {
24        assert!(!metadata_type.is_null());
25
26        MetadataType {
27            metadata_type: Type::new(metadata_type),
28        }
29    }
30
31    /// Creates a `FunctionType` with this `MetadataType` for its return type.
32    ///
33    /// # Example
34    ///
35    /// ```no_run
36    /// use inkwell::context::Context;
37    ///
38    /// let context = Context::create();
39    /// let md_type = context.metadata_type();
40    /// let fn_type = md_type.fn_type(&[], false);
41    /// ```
42    #[llvm_versions(6..)]
43    pub fn fn_type(self, param_types: &[BasicMetadataTypeEnum<'ctx>], is_var_args: bool) -> FunctionType<'ctx> {
44        self.metadata_type.fn_type(param_types, is_var_args)
45    }
46
47    /// Gets a reference to the `Context` this `MetadataType` was created in.
48    ///
49    /// # Example
50    ///
51    /// ```no_run
52    /// use inkwell::context::Context;
53    ///
54    /// let context = Context::create();
55    /// let md_type = context.metadata_type();
56    ///
57    /// assert_eq!(md_type.get_context(), context);
58    /// ```
59    #[llvm_versions(6..)]
60    pub fn get_context(self) -> ContextRef<'ctx> {
61        self.metadata_type.get_context()
62    }
63
64    /// Print the definition of a `MetadataType` to `LLVMString`.
65    pub fn print_to_string(self) -> LLVMString {
66        self.metadata_type.print_to_string()
67    }
68}
69
70unsafe impl AsTypeRef for MetadataType<'_> {
71    #[llvm_versions(6..)]
72    fn as_type_ref(&self) -> LLVMTypeRef {
73        self.metadata_type.ty
74    }
75
76    #[llvm_versions(..=5)]
77    fn as_type_ref(&self) -> LLVMTypeRef {
78        unimplemented!("MetadataType is only available in LLVM > 6.0")
79    }
80}
81
82impl Display for MetadataType<'_> {
83    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84        write!(f, "{}", self.print_to_string())
85    }
86}