summaryrefslogtreecommitdiff
path: root/libbindgen/src/codegen/helpers.rs
diff options
context:
space:
mode:
authorbors-servo <lbergstrom+bors@mozilla.com>2016-11-16 15:12:52 -0600
committerGitHub <noreply@github.com>2016-11-16 15:12:52 -0600
commite74acce09bc0f643a0084334f56837bb09f9cf42 (patch)
tree784ee1e899accea78f75b7ba87bc58b5e4b6a84d /libbindgen/src/codegen/helpers.rs
parentc800ad46a7b7713ed16f20d85479a608faf63c50 (diff)
parent19d36d436ec8674604ea53a249fc8a952eab2d33 (diff)
Auto merge of #260 - emilio:macro-str, r=fitzgen
Constant variable improvements. Fixes #256. r? @fitzgen
Diffstat (limited to 'libbindgen/src/codegen/helpers.rs')
-rw-r--r--libbindgen/src/codegen/helpers.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/libbindgen/src/codegen/helpers.rs b/libbindgen/src/codegen/helpers.rs
index 6e5a6f0e..8c3d3cea 100644
--- a/libbindgen/src/codegen/helpers.rs
+++ b/libbindgen/src/codegen/helpers.rs
@@ -132,4 +132,37 @@ pub mod ast_ty {
expr.int(val)
}
}
+
+ pub fn byte_array_expr(bytes: &[u8]) -> P<ast::Expr> {
+ let mut vec = Vec::with_capacity(bytes.len() + 1);
+ for byte in bytes {
+ vec.push(int_expr(*byte as i64));
+ }
+ vec.push(int_expr(0));
+
+ let kind = ast::ExprKind::Vec(vec);
+
+ aster::AstBuilder::new().expr().build_expr_kind(kind)
+ }
+
+ pub fn cstr_expr(mut string: String) -> P<ast::Expr> {
+ string.push('\0');
+ aster::AstBuilder::new()
+ .expr()
+ .build_lit(aster::AstBuilder::new().lit().byte_str(string))
+ }
+
+ pub fn float_expr(f: f64) -> P<ast::Expr> {
+ use aster::str::ToInternedString;
+ let mut string = f.to_string();
+
+ // So it gets properly recognised as a floating point constant.
+ if !string.contains('.') {
+ string.push('.');
+ }
+
+ let interned_str = string.as_str().to_interned_string();
+ let kind = ast::LitKind::FloatUnsuffixed(interned_str);
+ aster::AstBuilder::new().expr().lit().build_lit(kind)
+ }
}