Skip to content

Handle unsigned integer constants greater than u32::MAX in codegen #1081

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Oct 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,19 @@ impl CodeGenerator for Var {
});
}
VarType::Int(val) => {
let val = helpers::ast_ty::int_expr(val);
let int_kind = self.ty()
.into_resolver()
.through_type_aliases()
.through_type_refs()
.resolve(ctx)
.expect_type()
.as_integer()
.unwrap();
let val = if int_kind.is_signed() {
helpers::ast_ty::int_expr(val)
} else {
helpers::ast_ty::uint_expr(val as _)
};
result.push(quote! {
pub const #canonical_ident : #ty = #val ;
});
Expand Down
9 changes: 9 additions & 0 deletions src/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,15 @@ impl Type {
}
}

/// Cast this type to an integer kind, or `None` if it is not an integer
/// type.
pub fn as_integer(&self) -> Option<IntKind> {
match self.kind {
TypeKind::Int(int_kind) => Some(int_kind),
_ => None,
}
}

/// Is this a `const` qualified type?
pub fn is_const(&self) -> bool {
self.is_const
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* automatically generated by rust-bindgen */


#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]


pub const a: u32 = 18446744073709551611;
7 changes: 7 additions & 0 deletions tests/expectations/tests/issue-1040.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* automatically generated by rust-bindgen */


#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]


pub const g_107: ::std::os::raw::c_ulonglong = 18446744073709551615;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
typedef unsigned int uint32_t;

uint32_t a = 18446744073709551611;
1 change: 1 addition & 0 deletions tests/headers/issue-1040.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
unsigned long long g_107 = 18446744073709551615UL;