summaryrefslogtreecommitdiff
path: root/src/codegen/helpers.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/codegen/helpers.rs')
-rw-r--r--src/codegen/helpers.rs42
1 files changed, 35 insertions, 7 deletions
diff --git a/src/codegen/helpers.rs b/src/codegen/helpers.rs
index 4ff398c4..99054305 100644
--- a/src/codegen/helpers.rs
+++ b/src/codegen/helpers.rs
@@ -1,7 +1,9 @@
//! Helpers for code generation that don't need macro expansion.
+use ir::context::BindgenContext;
use ir::layout::Layout;
use quote;
+use std::mem;
pub mod attributes {
use quote;
@@ -86,6 +88,39 @@ pub fn blob(layout: Layout) -> quote::Tokens {
}
}
+/// Integer type of the same size as the given `Layout`.
+pub fn integer_type(layout: Layout) -> Option<quote::Tokens> {
+ // This guard can be weakened when Rust implements u128.
+ if layout.size > mem::size_of::<u64>() {
+ None
+ } else {
+ Some(blob(layout))
+ }
+}
+
+/// Generates a bitfield allocation unit type for a type with the given `Layout`.
+pub fn bitfield_unit(ctx: &BindgenContext, layout: Layout) -> quote::Tokens {
+ let mut tokens = quote! {};
+
+ if ctx.options().enable_cxx_namespaces {
+ tokens.append(quote! { root:: });
+ }
+
+ let align = match layout.align {
+ n if n >= 8 => quote! { u64 },
+ 4 => quote! { u32 },
+ 2 => quote! { u16 },
+ _ => quote! { u8 },
+ };
+
+ let size = layout.size;
+ tokens.append(quote! {
+ __BindgenBitfieldUnit<[u8; #size], #align>
+ });
+
+ tokens
+}
+
pub mod ast_ty {
use ir::context::BindgenContext;
use ir::function::FunctionSig;
@@ -143,13 +178,6 @@ pub mod ast_ty {
tokens
}
- /// Returns hex representation of the given value.
- pub fn hex_expr(val: u64) -> quote::Tokens {
- let mut tokens = quote! {};
- tokens.append(format!("{:#x}", val));
- tokens
- }
-
pub fn byte_array_expr(bytes: &[u8]) -> quote::Tokens {
let mut bytes: Vec<_> = bytes.iter().cloned().collect();
bytes.push(0);