inkwell/types/void_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 `VoidType` is a special type with no possible direct instances. It's only
12/// useful as a function return type.
13#[derive(Debug, PartialEq, Eq, Clone, Copy)]
14pub struct VoidType<'ctx> {
15 void_type: Type<'ctx>,
16}
17
18impl<'ctx> VoidType<'ctx> {
19 /// Create `VoidType` from [`LLVMTypeRef`]
20 ///
21 /// # Safety
22 /// Undefined behavior, if referenced type isn't void type
23 pub unsafe fn new(void_type: LLVMTypeRef) -> Self {
24 assert!(!void_type.is_null());
25
26 VoidType {
27 void_type: Type::new(void_type),
28 }
29 }
30
31 // REVIEW: Always false -> const fn?
32 /// Gets whether or not this `VoidType` is sized or not. This may always
33 /// be false and as such this function may be removed in the future.
34 ///
35 /// # Example
36 ///
37 /// ```no_run
38 /// use inkwell::context::Context;
39 ///
40 /// let context = Context::create();
41 /// let void_type = context.void_type();
42 ///
43 /// assert!(void_type.is_sized());
44 /// ```
45 pub fn is_sized(self) -> bool {
46 self.void_type.is_sized()
47 }
48
49 /// Gets a reference to the `Context` this `VoidType` was created in.
50 ///
51 /// # Example
52 ///
53 /// ```no_run
54 /// use inkwell::context::Context;
55 ///
56 /// let context = Context::create();
57 /// let void_type = context.void_type();
58 ///
59 /// assert_eq!(void_type.get_context(), context);
60 /// ```
61 pub fn get_context(self) -> ContextRef<'ctx> {
62 self.void_type.get_context()
63 }
64
65 /// Creates a `FunctionType` with this `VoidType` for its return type.
66 /// This means the function does not return.
67 ///
68 /// # Example
69 ///
70 /// ```no_run
71 /// use inkwell::context::Context;
72 ///
73 /// let context = Context::create();
74 /// let void_type = context.void_type();
75 /// let fn_type = void_type.fn_type(&[], false);
76 /// ```
77 pub fn fn_type(self, param_types: &[BasicMetadataTypeEnum<'ctx>], is_var_args: bool) -> FunctionType<'ctx> {
78 self.void_type.fn_type(param_types, is_var_args)
79 }
80
81 /// Print the definition of a `VoidType` to `LLVMString`.
82 pub fn print_to_string(self) -> LLVMString {
83 self.void_type.print_to_string()
84 }
85}
86
87unsafe impl AsTypeRef for VoidType<'_> {
88 fn as_type_ref(&self) -> LLVMTypeRef {
89 self.void_type.ty
90 }
91}
92
93impl Display for VoidType<'_> {
94 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95 write!(f, "{}", self.print_to_string())
96 }
97}