Skip to content

Commit 0a03095

Browse files
committed
[derive] Fix the tests for function reflection
1 parent 8b9d35e commit 0a03095

File tree

4 files changed

+127
-35
lines changed

4 files changed

+127
-35
lines changed

lib/derive-internals/src/func.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,14 @@ fn emit_def_from_signature(
228228
FnArg::Typed(ref item) => {
229229
let ty = &item.ty;
230230
static_arg_types.push(quote!(#ty));
231-
argument_types.push(quote!(<#ty as duckasm_repr::AsmRepr>::STATIC_TYPE))
231+
argument_types.push(quote!(<#ty as static_reflect::StaticReflect>::TYPE_INFO))
232232
},
233233
}
234234
}
235235
let return_type = match item.output {
236-
ReturnType::Default => quote!(&duckasm_repr::types::AsmType::Unit),
236+
ReturnType::Default => quote!(&static_reflect::types::TypeInfo::Unit),
237237
ReturnType::Type(_, ref ty) => {
238-
quote!(&<#ty as duckasm_repr::AsmRepr>::STATIC_TYPE)
238+
quote!(&<#ty as static_reflect::StaticReflect>::TYPE_INFO)
239239
},
240240
};
241241
let signature = StaticSignatureDef { argument_types, return_type };
@@ -285,8 +285,9 @@ impl StaticFunctionDef {
285285
quote! {
286286
#[doc(hidden)]
287287
#[allow(non_snake_case)]
288-
pub const #const_name: duckasm_repr::funcs::FunctionDeclaration<#return_type, #arg_types> = {
289-
#(<#verify_types as duckasm_repr::NativeRepr>::_VERIFY_TRANSPARENT_REPR;)*
288+
pub const #const_name: static_reflect::funcs::FunctionDeclaration<#return_type, #arg_types> = {
289+
// Verify all the types implement [StaticReflect]
290+
#(let _ = <#verify_types as static_reflect::StaticReflect>::TYPE_INFO;)*
290291
#def
291292
};
292293
}
@@ -317,7 +318,7 @@ impl ToTokens for StaticFunctionDef {
317318
ref static_return_type,
318319
static_arg_types: ref staitc_arg_types
319320
} = *self;
320-
tokens.append_all(quote!(duckasm_repr::funcs::FunctionDeclaration::<'static, #static_return_type, #staitc_arg_types> {
321+
tokens.append_all(quote!(static_reflect::funcs::FunctionDeclaration::<'static, #static_return_type, #staitc_arg_types> {
321322
name: #name,
322323
is_unsafe: #is_unsafe,
323324
signature: #signature,
@@ -332,13 +333,13 @@ impl ToTokens for FunctionLocation {
332333
fn to_tokens(&self, tokens: &mut TokenStream) {
333334
tokens.append_all(match *self {
334335
FunctionLocation::DynamicallyLinked { link_name: None } => {
335-
quote!(duckasm_repr::funcs::FunctionLocation::DynamicallyLinked { link_name: None })
336+
quote!(Some(static_reflect::funcs::FunctionLocation::DynamicallyLinked { link_name: None }))
336337
},
337338
FunctionLocation::DynamicallyLinked { link_name: Some(ref name) } => {
338-
quote!(duckasm_repr::funcs::FunctionLocation::DynamicallyLinked { link_name: Some(#name) })
339+
quote!(Some(static_reflect::funcs::FunctionLocation::DynamicallyLinked { link_name: Some(#name) }))
339340
},
340341
FunctionLocation::AbsoluteAddress(ref value) => {
341-
quote!(duckasm_repr::funcs::FunctionLocation::AbsoluteAddress(#value))
342+
quote!(Some(static_reflect::funcs::FunctionLocation::AbsoluteAddress(#value)))
342343
},
343344
});
344345
}
@@ -350,11 +351,11 @@ impl ToTokens for StaticSignatureDef {
350351
ref argument_types,
351352
ref return_type
352353
} = *self;
353-
tokens.append_all(quote!(duckasm_repr::funcs::SignatureDef {
354+
tokens.append_all(quote!(static_reflect::funcs::SignatureDef {
354355
argument_types: &[#(#argument_types),*],
355356
return_type: #return_type,
356357
// We use C FFI
357-
calling_convention: duckasm_repr::funcs::CallingConvention::StandardC
358+
calling_convention: static_reflect::funcs::CallingConvention::StandardC
358359
}))
359360
}
360361
}

lib/derive/tests/funcs.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
#![feature(const_raw_ptr_to_usize_cast)]
22
use std::os::raw::c_void;
33

4-
use duckasm_derive::duckasm_func;
5-
use static_repr::AsmRepr;
6-
use duckasm_repr::funcs::{FunctionDeclaration, FunctionLocation, SignatureDef};
7-
use duckasm_repr::types::AsmType;
4+
use static_reflect_derive::reflect_func;
5+
use static_reflect::StaticReflect;
6+
use static_reflect::funcs::{FunctionDeclaration, FunctionLocation, SignatureDef};
7+
use static_reflect::types::{TypeInfo, FloatSize};
88
use std::marker::PhantomData;
99

10-
#[duckasm_func]
10+
#[reflect_func]
1111
#[export_name = "better_name"]
1212
extern "C" fn stupid_name(first: f32, second: f32) {
1313
eprintln!("Test {}: {}", first, second);
1414
}
1515

1616
#[no_mangle]
17-
#[duckasm_func]
17+
#[reflect_func]
1818
unsafe extern "C" fn dynamically_linked(first: u32, second: *mut String) -> f32 {
1919
eprintln!("Test {}: {}", first, &*second);
2020
3.14
2121
}
2222

23-
#[duckasm_func(
23+
#[reflect_func(
2424
absolute // NOTE: This removes the requirement for #[no_mangle]
2525
)]
2626
extern "C" fn absolute_address_example(x: f64, y: f64) -> f64 {
2727
(x * x + y * y).sqrt()
2828
}
2929

30-
#[duckasm_func]
30+
#[reflect_func]
3131
extern "C" {
3232
/*
3333
* TODO: These are considered 'dead' even though DuckAsm uses them
3434
* Just because they're not invoked directly by Rust code,
35-
* doesn't mean their are actually unused.....
35+
* doesn't mean they are actually unused.....
3636
*/
3737
#[allow(dead_code)]
3838
#[link_name = "sqrtf"]
@@ -48,10 +48,10 @@ fn extern_block() {
4848
FunctionDeclaration::<*mut c_void, (usize,)> {
4949
name: "malloc",
5050
is_unsafe: true, // Foreign functions are always unsafe (in spite of lack of keyword)
51-
location: FunctionLocation::DynamicallyLinked { link_name: None },
51+
location: Some(FunctionLocation::DynamicallyLinked { link_name: None }),
5252
signature: SignatureDef {
53-
argument_types: &[usize::STATIC_TYPE],
54-
return_type: &AsmType::Pointer,
53+
argument_types: &[usize::TYPE_INFO],
54+
return_type: &TypeInfo::Pointer,
5555
calling_convention: Default::default()
5656
},
5757
return_type: PhantomData,
@@ -63,10 +63,10 @@ fn extern_block() {
6363
FunctionDeclaration::<f32, (f32,)> {
6464
name: "sqrt",
6565
is_unsafe: true, // NOTE: Foreign function
66-
location: FunctionLocation::DynamicallyLinked { link_name: Some("sqrtf".into()) },
66+
location: Some(FunctionLocation::DynamicallyLinked { link_name: Some("sqrtf".into()) }),
6767
signature: SignatureDef {
68-
argument_types: &[f32::STATIC_TYPE],
69-
return_type: &f32::STATIC_TYPE,
68+
argument_types: &[f32::TYPE_INFO],
69+
return_type: &f32::TYPE_INFO,
7070
calling_convention: Default::default()
7171
},
7272
return_type: PhantomData,
@@ -84,10 +84,10 @@ fn rust_funcs() {
8484
FunctionDeclaration::<f32, (u32, *mut String)> {
8585
name: "dynamically_linked",
8686
is_unsafe: true,
87-
location: FunctionLocation::DynamicallyLinked { link_name: None },
87+
location: Some(FunctionLocation::DynamicallyLinked { link_name: None }),
8888
signature: SignatureDef {
89-
argument_types: &[u32::STATIC_TYPE, AsmType::Pointer],
90-
return_type: &AsmType::Float { bytes: 4 },
89+
argument_types: &[u32::TYPE_INFO, TypeInfo::Pointer],
90+
return_type: &TypeInfo::F32,
9191
calling_convention: Default::default()
9292
},
9393
return_type: PhantomData,
@@ -99,10 +99,10 @@ fn rust_funcs() {
9999
FunctionDeclaration::<(), (f32, f32)> {
100100
name: "stupid_name",
101101
is_unsafe: false,
102-
location: FunctionLocation::DynamicallyLinked { link_name: Some("better_name".into()) },
102+
location: Some(FunctionLocation::DynamicallyLinked { link_name: Some("better_name".into()) }),
103103
signature: SignatureDef {
104-
argument_types: &[f32::STATIC_TYPE, AsmType::Float { bytes: 4 }],
105-
return_type: &AsmType::Unit,
104+
argument_types: &[f32::TYPE_INFO, TypeInfo::Float { size: FloatSize::Single }],
105+
return_type: &TypeInfo::Unit,
106106
calling_convention: Default::default()
107107
},
108108
return_type: PhantomData,
@@ -114,10 +114,10 @@ fn rust_funcs() {
114114
FunctionDeclaration::<f64, (f64, f64)> {
115115
name: "absolute_address_example",
116116
is_unsafe: false,
117-
location: FunctionLocation::AbsoluteAddress(absolute_address_example as *const ()),
117+
location: Some(FunctionLocation::AbsoluteAddress(absolute_address_example as *const ())),
118118
signature: SignatureDef {
119-
argument_types: &[f64::STATIC_TYPE, f64::STATIC_TYPE],
120-
return_type: &f64::STATIC_TYPE,
119+
argument_types: &[f64::TYPE_INFO, f64::TYPE_INFO],
120+
return_type: &f64::TYPE_INFO,
121121
calling_convention: Default::default()
122122
},
123123
return_type: PhantomData,

src/funcs.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//! Reflection information on function declarations
2+
use crate::types::{TypeInfo};
3+
use std::marker::PhantomData;
4+
5+
/// The declaration of a function whose information
6+
/// is known to the static reflection system
7+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
8+
pub struct FunctionDeclaration<'a, R = (), Args = ()> {
9+
/// The name of the function, as declared in the
10+
/// source code.
11+
pub name: &'a str,
12+
/// If the function is unsafe
13+
pub is_unsafe: bool,
14+
/// The location of the function (if known)
15+
///
16+
/// Not all supported functions have a known location.
17+
pub location: Option<FunctionLocation>,
18+
/// The signature of the function, including
19+
/// its arguments and return types
20+
///
21+
/// Unlike the [PhantomData], this is actually retained at runtime.
22+
pub signature: SignatureDef<'a>,
23+
/// PhantomData: The return type of the function
24+
pub return_type: PhantomData<*mut R>,
25+
/// PhantomData: The argument types of the function
26+
pub arg_types: PhantomData<*mut Args>,
27+
}
28+
impl<'a, R, Args> FunctionDeclaration<'a, R, Args> {
29+
/// If the function has a known location at runtime
30+
///
31+
/// If this is false, it wont actually be possible
32+
/// to call the function later. If it is true,
33+
/// then you can.
34+
#[inline]
35+
pub fn has_known_location(&self) -> bool {
36+
self.location.is_some()
37+
}
38+
/// Erase all statically known type information
39+
#[inline]
40+
pub fn erase(&'a self) -> &'a FunctionDeclaration<(), ()> {
41+
unsafe { &*(self as *const Self as *const FunctionDeclaration<(), ()>) }
42+
}
43+
}
44+
/// The definition of a function's signature
45+
///
46+
/// Includes its argument types, return type, and calling convention.
47+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
48+
pub struct SignatureDef<'tp> {
49+
/// A list of argument types to the function
50+
pub argument_types: &'tp [TypeInfo<'tp>],
51+
/// The return type of the function
52+
pub return_type: &'tp TypeInfo<'tp>,
53+
/// The calling convention
54+
pub calling_convention: CallingConvention
55+
}
56+
57+
58+
/// The convention used to call code.
59+
///
60+
/// Currently, only the C calling convention is supported
61+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
62+
pub enum CallingConvention {
63+
/// Matches the target's C calling convention `extern "C"`
64+
///
65+
/// This is the default calling convention.
66+
/// It has a stable ABI and is used to call external functions
67+
StandardC,
68+
}
69+
impl Default for CallingConvention {
70+
#[inline]
71+
fn default() -> Self {
72+
CallingConvention::StandardC
73+
}
74+
}
75+
76+
/// The location of the function
77+
///
78+
/// Gives specific information on which function to invoke
79+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
80+
pub enum FunctionLocation {
81+
/// The function is in a dynamically linked library,
82+
/// which will need to be resolved by the linker
83+
DynamicallyLinked {
84+
/// The name to be linked against,
85+
/// or `None` if it's the same as the function's name
86+
link_name: Option<&'static str>
87+
},
88+
/// The function is referred to by an absolute (hardcoded) address
89+
AbsoluteAddress(*const ()),
90+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#[cfg(feature = "builtins")]
2020
pub mod builtins;
2121
pub mod types;
22+
pub mod funcs;
2223

2324
mod core;
2425

0 commit comments

Comments
 (0)