Skip to content

Commit c52a218

Browse files
authored
Merge pull request #23 from GuillaumeGomez/non-null
Add support for multiple integers value for attribute and `NonNull`
2 parents 0b158c6 + 387d12b commit c52a218

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

gccjit_sys/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ pub enum gcc_jit_fn_attribute
270270
GCC_JIT_FN_ATTRIBUTE_PURE,
271271
GCC_JIT_FN_ATTRIBUTE_CONST,
272272
GCC_JIT_FN_ATTRIBUTE_WEAK,
273+
GCC_JIT_FN_ATTRIBUTE_NONNULL,
273274
}
274275

275276
#[cfg(feature="master")]
@@ -619,6 +620,9 @@ extern {
619620
#[cfg(feature="master")]
620621
pub fn gcc_jit_function_add_string_attribute(func: *mut gcc_jit_function, attribute: gcc_jit_fn_attribute, value: *const c_char);
621622

623+
#[cfg(feature="master")]
624+
pub fn gcc_jit_function_add_integer_array_attribute(func: *mut gcc_jit_function, attribute: gcc_jit_fn_attribute, value: *const c_int, length: size_t);
625+
622626
#[cfg(feature="master")]
623627
pub fn gcc_jit_lvalue_add_attribute(variable: *mut gcc_jit_lvalue, attribute: gcc_jit_variable_attribute, value: *const c_char);
624628

src/function.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pub enum FnAttribute<'a> {
6161
Pure,
6262
Const,
6363
Weak,
64+
NonNull(Vec<std::ffi::c_int>),
6465
}
6566

6667
#[cfg(feature="master")]
@@ -78,6 +79,13 @@ impl<'a> FnAttribute<'a> {
7879
| FnAttribute::Pure
7980
| FnAttribute::Const
8081
| FnAttribute::Weak => AttributeValue::None,
82+
FnAttribute::NonNull(ref value) => {
83+
debug_assert!(
84+
value.iter().all(|attr| *attr > 0),
85+
"all values must be > 0 for non-null attribute",
86+
);
87+
AttributeValue::IntArray(value)
88+
}
8189
}
8290
}
8391

@@ -95,6 +103,7 @@ impl<'a> FnAttribute<'a> {
95103
FnAttribute::Pure => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_PURE,
96104
FnAttribute::Const => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_CONST,
97105
FnAttribute::Weak => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_WEAK,
106+
FnAttribute::NonNull(_) => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_NONNULL,
98107
}
99108
}
100109
}
@@ -208,7 +217,29 @@ impl<'ctx> Function<'ctx> {
208217
pub fn add_attribute<'a>(&self, attribute: FnAttribute<'a>) {
209218
let value = attribute.get_value();
210219
match value {
211-
AttributeValue::Int(_) => unimplemented!(),
220+
AttributeValue::Int(value) => {
221+
// Basically the same as `IntArray` but for only one element.
222+
let value = &[value];
223+
unsafe {
224+
gccjit_sys::gcc_jit_function_add_integer_array_attribute(
225+
self.ptr,
226+
attribute.as_sys(),
227+
value.as_ptr(),
228+
value.len() as _,
229+
);
230+
}
231+
232+
}
233+
AttributeValue::IntArray(value) => {
234+
unsafe {
235+
gccjit_sys::gcc_jit_function_add_integer_array_attribute(
236+
self.ptr,
237+
attribute.as_sys(),
238+
value.as_ptr(),
239+
value.len() as _,
240+
);
241+
}
242+
}
212243
AttributeValue::None => {
213244
unsafe {
214245
gccjit_sys::gcc_jit_function_add_attribute(self.ptr, attribute.as_sys());

src/lvalue.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub enum AttributeValue<'a> {
3939
Int(i32),
4040
None,
4141
String(&'a str),
42+
IntArray(&'a [std::ffi::c_int]),
4243
}
4344

4445
#[cfg(feature="master")]
@@ -51,7 +52,7 @@ pub enum VarAttribute {
5152
impl VarAttribute {
5253
fn get_value(&self) -> AttributeValue {
5354
match *self {
54-
VarAttribute::Visibility(visibility) => AttributeValue::String(visibility.as_str()),
55+
Self::Visibility(visibility) => AttributeValue::String(visibility.as_str()),
5556
}
5657
}
5758

@@ -221,6 +222,7 @@ impl<'ctx> LValue<'ctx> {
221222
let value = attribute.get_value();
222223
match value {
223224
AttributeValue::Int(_) => unimplemented!(),
225+
AttributeValue::IntArray(_) => unimplemented!(),
224226
AttributeValue::None => unimplemented!(),
225227
AttributeValue::String(string) => {
226228
let cstr = CString::new(string).unwrap();

0 commit comments

Comments
 (0)