Skip to content

Commit 2eb1197

Browse files
committed
py: Change constant func wrappers to have a struct type per func type.
To prevent warnings about casting functions when building with C++.
1 parent a0ac040 commit 2eb1197

File tree

2 files changed

+55
-63
lines changed

2 files changed

+55
-63
lines changed

inc/py/obj.h

+45-53
Original file line numberDiff line numberDiff line change
@@ -270,62 +270,38 @@ static inline bool mp_obj_is_integer(mp_const_obj_t o) { return MP_OBJ_IS_INT(o)
270270
// These macros are used to declare and define constant function objects
271271
// You can put "static" in front of the definitions to make them local
272272

273-
#define MP_DECLARE_CONST_FUN_OBJ_0(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
274-
#define MP_DECLARE_CONST_FUN_OBJ_1(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
275-
#define MP_DECLARE_CONST_FUN_OBJ_2(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
276-
#define MP_DECLARE_CONST_FUN_OBJ_3(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
273+
// If using a C++ compiler then we can only initialise a const union with the
274+
// first member, and so we use distinct struct types for all possible cases
275+
276+
#define MP_DECLARE_CONST_FUN_OBJ_0(obj_name) extern const mp_obj_fun_builtin_fixed0_t obj_name
277+
#define MP_DECLARE_CONST_FUN_OBJ_1(obj_name) extern const mp_obj_fun_builtin_fixed1_t obj_name
278+
#define MP_DECLARE_CONST_FUN_OBJ_2(obj_name) extern const mp_obj_fun_builtin_fixed2_t obj_name
279+
#define MP_DECLARE_CONST_FUN_OBJ_3(obj_name) extern const mp_obj_fun_builtin_fixed3_t obj_name
277280
#define MP_DECLARE_CONST_FUN_OBJ_VAR(obj_name) extern const mp_obj_fun_builtin_var_t obj_name
278281
#define MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name) extern const mp_obj_fun_builtin_var_t obj_name
279-
#define MP_DECLARE_CONST_FUN_OBJ_KW(obj_name) extern const mp_obj_fun_builtin_var_t obj_name
282+
#define MP_DECLARE_CONST_FUN_OBJ_KW(obj_name) extern const mp_obj_fun_builtin_kw_t obj_name
280283

281-
#ifdef __cplusplus
282-
// If using a C++ compiler then we can only initialise a const union with the
283-
// first member, and so we use a cast to get the function pointer to initialise.
284284
#define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) \
285-
const mp_obj_fun_builtin_fixed_t obj_name = \
285+
const mp_obj_fun_builtin_fixed0_t obj_name = \
286286
{{&mp_type_fun_builtin_0}, fun_name}
287287
#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) \
288-
const mp_obj_fun_builtin_fixed_t obj_name = \
289-
{{&mp_type_fun_builtin_1}, (mp_fun_0_t)fun_name}
288+
const mp_obj_fun_builtin_fixed1_t obj_name = \
289+
{{&mp_type_fun_builtin_1}, fun_name}
290290
#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) \
291-
const mp_obj_fun_builtin_fixed_t obj_name = \
292-
{{&mp_type_fun_builtin_2}, (mp_fun_0_t)fun_name}
291+
const mp_obj_fun_builtin_fixed2_t obj_name = \
292+
{{&mp_type_fun_builtin_2}, fun_name}
293293
#define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) \
294-
const mp_obj_fun_builtin_fixed_t obj_name = \
295-
{{&mp_type_fun_builtin_3}, (mp_fun_0_t)fun_name}
294+
const mp_obj_fun_builtin_fixed3_t obj_name = \
295+
{{&mp_type_fun_builtin_3}, fun_name}
296296
#define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) \
297297
const mp_obj_fun_builtin_var_t obj_name = \
298298
{{&mp_type_fun_builtin_var}, false, n_args_min, MP_OBJ_FUN_ARGS_MAX, fun_name}
299299
#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) \
300300
const mp_obj_fun_builtin_var_t obj_name = \
301301
{{&mp_type_fun_builtin_var}, false, n_args_min, n_args_max, fun_name}
302302
#define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, n_args_min, fun_name) \
303-
const mp_obj_fun_builtin_var_t obj_name = \
304-
{{&mp_type_fun_builtin_var}, true, n_args_min, MP_OBJ_FUN_ARGS_MAX, (mp_fun_var_t)fun_name}
305-
#else
306-
// If using a C compiler then we can specify the member of a const union.
307-
#define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) \
308-
const mp_obj_fun_builtin_fixed_t obj_name = \
309-
{{&mp_type_fun_builtin_0}, .fun._0 = fun_name}
310-
#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) \
311-
const mp_obj_fun_builtin_fixed_t obj_name = \
312-
{{&mp_type_fun_builtin_1}, .fun._1 = fun_name}
313-
#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) \
314-
const mp_obj_fun_builtin_fixed_t obj_name = \
315-
{{&mp_type_fun_builtin_2}, .fun._2 = fun_name}
316-
#define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) \
317-
const mp_obj_fun_builtin_fixed_t obj_name = \
318-
{{&mp_type_fun_builtin_3}, .fun._3 = fun_name}
319-
#define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) \
320-
const mp_obj_fun_builtin_var_t obj_name = \
321-
{{&mp_type_fun_builtin_var}, false, n_args_min, MP_OBJ_FUN_ARGS_MAX, .fun.var = fun_name}
322-
#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) \
323-
const mp_obj_fun_builtin_var_t obj_name = \
324-
{{&mp_type_fun_builtin_var}, false, n_args_min, n_args_max, .fun.var = fun_name}
325-
#define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, n_args_min, fun_name) \
326-
const mp_obj_fun_builtin_var_t obj_name = \
327-
{{&mp_type_fun_builtin_var}, true, n_args_min, MP_OBJ_FUN_ARGS_MAX, .fun.kw = fun_name}
328-
#endif
303+
const mp_obj_fun_builtin_kw_t obj_name = \
304+
{{&mp_type_fun_builtin_var}, true, n_args_min, MP_OBJ_FUN_ARGS_MAX, fun_name}
329305

330306
// These macros are used to define constant map/dict objects
331307
// You can put "static" in front of the definition to make it local
@@ -805,28 +781,44 @@ void mp_obj_slice_get(mp_obj_t self_in, mp_obj_t *start, mp_obj_t *stop, mp_obj_
805781

806782
// functions
807783

808-
typedef struct _mp_obj_fun_builtin_fixed_t {
784+
typedef struct _mp_obj_fun_builtin_fixed0_t {
809785
mp_obj_base_t base;
810-
union {
811-
mp_fun_0_t _0;
812-
mp_fun_1_t _1;
813-
mp_fun_2_t _2;
814-
mp_fun_3_t _3;
815-
} fun;
816-
} mp_obj_fun_builtin_fixed_t;
786+
mp_fun_0_t fun;
787+
} mp_obj_fun_builtin_fixed0_t;
788+
789+
typedef struct _mp_obj_fun_builtin_fixed1_t {
790+
mp_obj_base_t base;
791+
mp_fun_1_t fun;
792+
} mp_obj_fun_builtin_fixed1_t;
793+
794+
typedef struct _mp_obj_fun_builtin_fixed2_t {
795+
mp_obj_base_t base;
796+
mp_fun_2_t fun;
797+
} mp_obj_fun_builtin_fixed2_t;
798+
799+
typedef struct _mp_obj_fun_builtin_fixed3_t {
800+
mp_obj_base_t base;
801+
mp_fun_3_t fun;
802+
} mp_obj_fun_builtin_fixed3_t;
817803

818804
#define MP_OBJ_FUN_ARGS_MAX (0xffff) // to set maximum value in n_args_max below
805+
819806
typedef struct _mp_obj_fun_builtin_var_t {
820807
mp_obj_base_t base;
821808
bool is_kw : 1;
822809
mp_uint_t n_args_min : 15; // inclusive
823810
mp_uint_t n_args_max : 16; // inclusive
824-
union {
825-
mp_fun_var_t var;
826-
mp_fun_kw_t kw;
827-
} fun;
811+
mp_fun_var_t fun;
828812
} mp_obj_fun_builtin_var_t;
829813

814+
typedef struct _mp_obj_fun_builtin_kw_t {
815+
mp_obj_base_t base;
816+
bool is_kw : 1;
817+
mp_uint_t n_args_min : 15; // inclusive
818+
mp_uint_t n_args_max : 16; // inclusive
819+
mp_fun_kw_t fun;
820+
} mp_obj_fun_builtin_kw_t;
821+
830822
qstr mp_obj_fun_get_name(mp_const_obj_t fun);
831823
qstr mp_obj_code_get_name(const byte *code_info);
832824

source/py/objfun.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@
5353
STATIC mp_obj_t fun_builtin_0_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
5454
(void)args;
5555
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin_0));
56-
mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
56+
mp_obj_fun_builtin_fixed0_t *self = MP_OBJ_TO_PTR(self_in);
5757
mp_arg_check_num(n_args, n_kw, 0, 0, false);
58-
return self->fun._0();
58+
return self->fun();
5959
}
6060

6161
const mp_obj_type_t mp_type_fun_builtin_0 = {
@@ -67,9 +67,9 @@ const mp_obj_type_t mp_type_fun_builtin_0 = {
6767

6868
STATIC mp_obj_t fun_builtin_1_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
6969
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin_1));
70-
mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
70+
mp_obj_fun_builtin_fixed1_t *self = MP_OBJ_TO_PTR(self_in);
7171
mp_arg_check_num(n_args, n_kw, 1, 1, false);
72-
return self->fun._1(args[0]);
72+
return self->fun(args[0]);
7373
}
7474

7575
const mp_obj_type_t mp_type_fun_builtin_1 = {
@@ -81,9 +81,9 @@ const mp_obj_type_t mp_type_fun_builtin_1 = {
8181

8282
STATIC mp_obj_t fun_builtin_2_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
8383
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin_2));
84-
mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
84+
mp_obj_fun_builtin_fixed2_t *self = MP_OBJ_TO_PTR(self_in);
8585
mp_arg_check_num(n_args, n_kw, 2, 2, false);
86-
return self->fun._2(args[0], args[1]);
86+
return self->fun(args[0], args[1]);
8787
}
8888

8989
const mp_obj_type_t mp_type_fun_builtin_2 = {
@@ -95,9 +95,9 @@ const mp_obj_type_t mp_type_fun_builtin_2 = {
9595

9696
STATIC mp_obj_t fun_builtin_3_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
9797
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin_3));
98-
mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
98+
mp_obj_fun_builtin_fixed3_t *self = MP_OBJ_TO_PTR(self_in);
9999
mp_arg_check_num(n_args, n_kw, 3, 3, false);
100-
return self->fun._3(args[0], args[1], args[2]);
100+
return self->fun(args[0], args[1], args[2]);
101101
}
102102

103103
const mp_obj_type_t mp_type_fun_builtin_3 = {
@@ -121,12 +121,12 @@ STATIC mp_obj_t fun_builtin_var_call(mp_obj_t self_in, size_t n_args, size_t n_k
121121
mp_map_t kw_args;
122122
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
123123

124-
return self->fun.kw(n_args, args, &kw_args);
124+
return ((mp_obj_fun_builtin_kw_t*)self)->fun(n_args, args, &kw_args);
125125

126126
} else {
127127
// function takes a variable number of arguments, but no keywords
128128

129-
return self->fun.var(n_args, args);
129+
return self->fun(n_args, args);
130130
}
131131
}
132132

0 commit comments

Comments
 (0)