summaryrefslogtreecommitdiffstatshomepage
path: root/py/obj.h
diff options
context:
space:
mode:
Diffstat (limited to 'py/obj.h')
-rw-r--r--py/obj.h61
1 files changed, 35 insertions, 26 deletions
diff --git a/py/obj.h b/py/obj.h
index 5eed90b0ab..1ab4609cfc 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -81,12 +81,12 @@ typedef struct _mp_obj_base_t mp_obj_base_t;
#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A
-static inline bool MP_OBJ_IS_SMALL_INT(mp_const_obj_t o)
+static inline bool mp_obj_is_small_int(mp_const_obj_t o)
{ return ((((mp_int_t)(o)) & 1) != 0); }
#define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 1)
#define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_uint_t)(small_int)) << 1) | 1))
-static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o)
+static inline bool mp_obj_is_qstr(mp_const_obj_t o)
{ return ((((mp_int_t)(o)) & 3) == 2); }
#define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 2)
#define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 2) | 2))
@@ -97,22 +97,22 @@ static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o)
extern const struct _mp_obj_float_t mp_const_float_e_obj;
extern const struct _mp_obj_float_t mp_const_float_pi_obj;
-#define mp_obj_is_float(o) MP_OBJ_IS_TYPE((o), &mp_type_float)
+#define mp_obj_is_float(o) mp_obj_is_type((o), &mp_type_float)
mp_float_t mp_obj_float_get(mp_obj_t self_in);
mp_obj_t mp_obj_new_float(mp_float_t value);
#endif
-static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o)
+static inline bool mp_obj_is_obj(mp_const_obj_t o)
{ return ((((mp_int_t)(o)) & 3) == 0); }
#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B
-static inline bool MP_OBJ_IS_SMALL_INT(mp_const_obj_t o)
+static inline bool mp_obj_is_small_int(mp_const_obj_t o)
{ return ((((mp_int_t)(o)) & 3) == 1); }
#define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 2)
#define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_uint_t)(small_int)) << 2) | 1))
-static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o)
+static inline bool mp_obj_is_qstr(mp_const_obj_t o)
{ return ((((mp_int_t)(o)) & 3) == 3); }
#define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 2)
#define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 2) | 3))
@@ -123,17 +123,17 @@ static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o)
extern const struct _mp_obj_float_t mp_const_float_e_obj;
extern const struct _mp_obj_float_t mp_const_float_pi_obj;
-#define mp_obj_is_float(o) MP_OBJ_IS_TYPE((o), &mp_type_float)
+#define mp_obj_is_float(o) mp_obj_is_type((o), &mp_type_float)
mp_float_t mp_obj_float_get(mp_obj_t self_in);
mp_obj_t mp_obj_new_float(mp_float_t value);
#endif
-static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o)
+static inline bool mp_obj_is_obj(mp_const_obj_t o)
{ return ((((mp_int_t)(o)) & 1) == 0); }
#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C
-static inline bool MP_OBJ_IS_SMALL_INT(mp_const_obj_t o)
+static inline bool mp_obj_is_small_int(mp_const_obj_t o)
{ return ((((mp_int_t)(o)) & 1) != 0); }
#define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 1)
#define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_uint_t)(small_int)) << 1) | 1))
@@ -160,22 +160,22 @@ static inline mp_obj_t mp_obj_new_float(mp_float_t f) {
}
#endif
-static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o)
+static inline bool mp_obj_is_qstr(mp_const_obj_t o)
{ return (((mp_uint_t)(o)) & 0xff800007) == 0x00000006; }
#define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 3)
#define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 3) | 0x00000006))
-static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o)
+static inline bool mp_obj_is_obj(mp_const_obj_t o)
{ return ((((mp_int_t)(o)) & 3) == 0); }
#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
-static inline bool MP_OBJ_IS_SMALL_INT(mp_const_obj_t o)
+static inline bool mp_obj_is_small_int(mp_const_obj_t o)
{ return ((((uint64_t)(o)) & 0xffff000000000000) == 0x0001000000000000); }
#define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)((o) << 16)) >> 17)
#define MP_OBJ_NEW_SMALL_INT(small_int) (((((uint64_t)(small_int)) & 0x7fffffffffff) << 1) | 0x0001000000000001)
-static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o)
+static inline bool mp_obj_is_qstr(mp_const_obj_t o)
{ return ((((uint64_t)(o)) & 0xffff000000000000) == 0x0002000000000000); }
#define MP_OBJ_QSTR_VALUE(o) ((((uint32_t)(o)) >> 1) & 0xffffffff)
#define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 1) | 0x0002000000000001))
@@ -208,7 +208,7 @@ static inline mp_obj_t mp_obj_new_float(mp_float_t f) {
}
#endif
-static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o)
+static inline bool mp_obj_is_obj(mp_const_obj_t o)
{ return ((((uint64_t)(o)) & 0xffff000000000000) == 0x0000000000000000); }
#define MP_OBJ_TO_PTR(o) ((void*)(uintptr_t)(o))
#define MP_OBJ_FROM_PTR(p) ((mp_obj_t)((uintptr_t)(p)))
@@ -255,17 +255,6 @@ typedef struct _mp_rom_obj_t { mp_const_obj_t o; } mp_rom_obj_t;
*/
#endif
-// The macros below are derived from the ones above and are used to
-// check for more specific object types.
-// Note: these are kept as macros because inline functions sometimes use much
-// more code space than the equivalent macros, depending on the compiler.
-
-#define MP_OBJ_IS_TYPE(o, t) (MP_OBJ_IS_OBJ(o) && (((mp_obj_base_t*)MP_OBJ_TO_PTR(o))->type == (t))) // this does not work for checking int, str or fun; use below macros for that
-#define MP_OBJ_IS_INT(o) (MP_OBJ_IS_SMALL_INT(o) || MP_OBJ_IS_TYPE(o, &mp_type_int))
-#define MP_OBJ_IS_STR(o) (MP_OBJ_IS_QSTR(o) || MP_OBJ_IS_TYPE(o, &mp_type_str))
-#define MP_OBJ_IS_STR_OR_BYTES(o) (MP_OBJ_IS_QSTR(o) || (MP_OBJ_IS_OBJ(o) && ((mp_obj_base_t*)MP_OBJ_TO_PTR(o))->type->binary_op == mp_obj_str_binary_op))
-#define MP_OBJ_IS_FUN(o) (MP_OBJ_IS_OBJ(o) && (((mp_obj_base_t*)MP_OBJ_TO_PTR(o))->type->name == MP_QSTR_function))
-
// These macros are used to declare and define constant function objects
// You can put "static" in front of the definitions to make them local
@@ -625,6 +614,16 @@ extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj;
// General API for objects
+// These macros are derived from more primitive ones and are used to
+// check for more specific object types.
+// Note: these are kept as macros because inline functions sometimes use much
+// more code space than the equivalent macros, depending on the compiler.
+#define mp_obj_is_type(o, t) (mp_obj_is_obj(o) && (((mp_obj_base_t*)MP_OBJ_TO_PTR(o))->type == (t))) // this does not work for checking int, str or fun; use below macros for that
+#define mp_obj_is_int(o) (mp_obj_is_small_int(o) || mp_obj_is_type(o, &mp_type_int))
+#define mp_obj_is_str(o) (mp_obj_is_qstr(o) || mp_obj_is_type(o, &mp_type_str))
+#define mp_obj_is_str_or_bytes(o) (mp_obj_is_qstr(o) || (mp_obj_is_obj(o) && ((mp_obj_base_t*)MP_OBJ_TO_PTR(o))->type->binary_op == mp_obj_str_binary_op))
+#define mp_obj_is_fun(o) (mp_obj_is_obj(o) && (((mp_obj_base_t*)MP_OBJ_TO_PTR(o))->type->name == MP_QSTR_function))
+
mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict);
static inline mp_obj_t mp_obj_new_bool(mp_int_t x) { return x ? mp_const_true : mp_const_false; }
mp_obj_t mp_obj_new_cell(mp_obj_t obj);
@@ -677,7 +676,7 @@ bool mp_obj_is_true(mp_obj_t arg);
bool mp_obj_is_callable(mp_obj_t o_in);
bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2);
-static inline bool mp_obj_is_integer(mp_const_obj_t o) { return MP_OBJ_IS_INT(o) || MP_OBJ_IS_TYPE(o, &mp_type_bool); } // returns true if o is bool, small int or long int
+static inline bool mp_obj_is_integer(mp_const_obj_t o) { return mp_obj_is_int(o) || mp_obj_is_type(o, &mp_type_bool); } // returns true if o is bool, small int or long int
mp_int_t mp_obj_get_int(mp_const_obj_t arg);
mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg);
bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value);
@@ -864,4 +863,14 @@ mp_obj_t mp_seq_extract_slice(size_t len, const mp_obj_t *seq, mp_bound_slice_t
memmove(((char*)dest) + (beg + slice_len) * (item_sz), ((char*)dest) + (end) * (item_sz), ((dest_len) + (len_adj) - ((beg) + (slice_len))) * (item_sz)); \
memmove(((char*)dest) + (beg) * (item_sz), slice, slice_len * (item_sz));
+// Provide translation for legacy API
+#define MP_OBJ_IS_SMALL_INT mp_obj_is_small_int
+#define MP_OBJ_IS_QSTR mp_obj_is_qstr
+#define MP_OBJ_IS_OBJ mp_obj_is_obj
+#define MP_OBJ_IS_INT mp_obj_is_int
+#define MP_OBJ_IS_TYPE mp_obj_is_type
+#define MP_OBJ_IS_STR mp_obj_is_str
+#define MP_OBJ_IS_STR_OR_BYTES mp_obj_is_str_or_bytes
+#define MP_OBJ_IS_FUN mp_obj_is_fun
+
#endif // MICROPY_INCLUDED_PY_OBJ_H