Author: Armin Rigo <[email protected]>
Branch:
Changeset: r76:5f6346bf551d
Date: 2014-11-18 18:02 +0100
http://bitbucket.org/cffi/creflect/changeset/5f6346bf551d/
Log: Global function pointers
diff --git a/creflect/model.py b/creflect/model.py
--- a/creflect/model.py
+++ b/creflect/model.py
@@ -209,10 +209,11 @@
inspect.flush()
- # limitations so far:
- assert isinstance(inspect, TypeInspector)
- assert inspect.started, "XXX"
- assert inspect.levels == ['*'], "XXX"
+ if (isinstance(inspect, TypeInspector) and inspect.started and
+ inspect.levels == ['*']): # common case
+ decl = '%s f' % inspect.typename
+ else:
+ decl = self.get_c_name('(*f)')
toplevel = block.crx_top_level
extraname = block._get_next_name('f')
@@ -220,7 +221,7 @@
wrline = toplevel.writeline
wrline('static void %s(void *func, void *args[], void *result) {' % (
crx_func_name,))
- wrline(' %s f = func;' % inspect.typename)
+ wrline(' %s = func;' % decl)
wrline(self._get_c_call_sequence('f'))
wrline('}')
wrline('')
@@ -317,7 +318,7 @@
" constant */" % (inspect.varname,
inspect.varname))
else:
- decl = self.totype.get_c_name("**p1")
+ decl = self.totype.get_c_name("(**p1)")
block.writeline("%s = &%s; /* check that '%s' is of type '%s'"
" */" % (decl, inspect.varname,
inspect.varname, self.get_c_name()))
diff --git a/test/codegen/func-003.c b/test/codegen/func-003.c
--- a/test/codegen/func-003.c
+++ b/test/codegen/func-003.c
@@ -2,7 +2,7 @@
# ____________________________________________________________
-static void testfunc_003__f4(void *func, void *args[], void *result) {
+static void testfunc_003__f1(void *func, void *args[], void *result) {
func_t f = func;
*(int *)result = f(*(long *)args[0], *(long *)args[1], *(int *)args[2]);
}
@@ -20,7 +20,7 @@
a3[0] = t2;
a3[1] = t2;
a3[2] = t1;
- t4 = cb->get_function_type(cb, t1, a3, 3, &testfunc_003__f4);
+ t4 = cb->get_function_type(cb, t1, a3, 3, &testfunc_003__f1);
t5 = cb->get_pointer_type(cb, t4);
cb->define_type(cb, "func_t", t5);
#expect TYPEDEF func_t = PTR FUNC( long -> long -> int -> int )
diff --git a/test/codegen/glob-002d.c b/test/codegen/glob-002d.c
--- a/test/codegen/glob-002d.c
+++ b/test/codegen/glob-002d.c
@@ -6,7 +6,7 @@
{
crx_type_t *t1, *t2;
{
- int **p1 = &someglob; /* check that 'someglob' is of type 'int *' */
+ int (**p1) = &someglob; /* check that 'someglob' is of type 'int *' */
(void)p1;
t1 = cb->get_signed_type(cb, sizeof(int), "int");
t2 = cb->get_pointer_type(cb, t1);
diff --git a/test/codegen/glob-004.c b/test/codegen/glob-004.c
--- a/test/codegen/glob-004.c
+++ b/test/codegen/glob-004.c
@@ -2,20 +2,24 @@
# ____________________________________________________________
-static void __creflect_t1(void *func, void *args[], void *result) {
- int(*f)(long, long) = func;
+static void testglob_004__f1(void *func, void *args[], void *result) {
+ int (*f)(long, long) = func;
*(int *)result = f(*(long *)args[0], *(long *)args[1]);
}
-int testglob_004(char *r)
+void testglob_004(crx_builder_t *cb)
{
- if (!r)
- return 100;
+ crx_type_t *t1, *t2, *a3[2], *t4, *t5;
{
- int (**p1)(long, long) = &someglob; /* check the exact type of
'someglob' */
+ int (**p1)(long, long) = &someglob; /* check that 'someglob' is of
type 'int (*)(long, long)' */
(void)p1;
- r += sprintf(r, "/*%p*/", &someglob);
- r += sprintf(r, "int (*someglob)(long, long)/*%p*/;\n",
&__creflect_t1);
+ t1 = cb->get_signed_type(cb, sizeof(int), "int");
+ t2 = cb->get_signed_type(cb, sizeof(long), "long");
+ a3[0] = t2;
+ a3[1] = t2;
+ t4 = cb->get_function_type(cb, t1, a3, 2, &testglob_004__f1);
+ t5 = cb->get_pointer_type(cb, t4);
+ cb->define_var(cb, "someglob", t5, &someglob);
+#expect VAR someglob: PTR FUNC( long -> long -> int )
}
- return 0;
}
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit