[qfcc] Use pr_type_names to generate is_TYPE

This means basic types will always have a check function automatically.
This commit is contained in:
Bill Currie 2022-01-19 17:48:46 +09:00
parent 380cb3ba7f
commit e11fa77a34
8 changed files with 32 additions and 148 deletions

View file

@ -149,26 +149,17 @@ void dump_dot_type (void *t, const char *filename);
const char *encode_params (const type_t *type); const char *encode_params (const type_t *type);
void encode_type (struct dstring_s *encoding, const type_t *type); void encode_type (struct dstring_s *encoding, const type_t *type);
const char *type_get_encoding (const type_t *type); const char *type_get_encoding (const type_t *type);
int is_void (const type_t *type) __attribute__((pure));
#define EV_TYPE(t) int is_##t (const type_t *type) __attribute__((pure));
#include "QF/progs/pr_type_names.h"
int is_enum (const type_t *type) __attribute__((pure)); int is_enum (const type_t *type) __attribute__((pure));
int is_int (const type_t *type) __attribute__((pure));
int is_uint (const type_t *type) __attribute__((pure));
int is_short (const type_t *type) __attribute__((pure));
int is_integral (const type_t *type) __attribute__((pure)); int is_integral (const type_t *type) __attribute__((pure));
int is_double (const type_t *type) __attribute__((pure));
int is_float (const type_t *type) __attribute__((pure));
int is_scalar (const type_t *type) __attribute__((pure)); int is_scalar (const type_t *type) __attribute__((pure));
int is_vector (const type_t *type) __attribute__((pure));
int is_quaternion (const type_t *type) __attribute__((pure));
int is_math (const type_t *type) __attribute__((pure)); int is_math (const type_t *type) __attribute__((pure));
int is_pointer (const type_t *type) __attribute__((pure));
int is_field (const type_t *type) __attribute__((pure));
int is_entity (const type_t *type) __attribute__((pure));
int is_struct (const type_t *type) __attribute__((pure)); int is_struct (const type_t *type) __attribute__((pure));
int is_array (const type_t *type) __attribute__((pure)); int is_array (const type_t *type) __attribute__((pure));
int is_structural (const type_t *type) __attribute__((pure)); int is_structural (const type_t *type) __attribute__((pure));
int is_func (const type_t *type) __attribute__((pure));
int is_string (const type_t *type) __attribute__((pure));
int type_compatible (const type_t *dst, const type_t *src) __attribute__((pure)); int type_compatible (const type_t *dst, const type_t *src) __attribute__((pure));
int type_assignable (const type_t *dst, const type_t *src); int type_assignable (const type_t *dst, const type_t *src);
int type_same (const type_t *dst, const type_t *src) __attribute__((pure)); int type_same (const type_t *dst, const type_t *src) __attribute__((pure));

View file

@ -391,7 +391,7 @@ is_method_description (const type_t *type)
static protocollist_t * static protocollist_t *
obj_get_class_protos (const type_t *type) obj_get_class_protos (const type_t *type)
{ {
if (is_pointer (type)) if (is_ptr (type))
type = type->t.fldptr.type; type = type->t.fldptr.type;
if (is_class (type)) if (is_class (type))
return type->t.class->protocols; return type->t.class->protocols;
@ -401,7 +401,7 @@ obj_get_class_protos (const type_t *type)
static protocollist_t * static protocollist_t *
obj_get_protos (const type_t *type) obj_get_protos (const type_t *type)
{ {
if (is_pointer (type)) if (is_ptr (type))
type = type->t.fldptr.type; type = type->t.fldptr.type;
return type->protos; return type->protos;
} }
@ -409,7 +409,7 @@ obj_get_protos (const type_t *type)
static category_t * static category_t *
obj_get_categories (const type_t *type) obj_get_categories (const type_t *type)
{ {
if (is_pointer (type)) if (is_ptr (type))
type = type->t.fldptr.type; type = type->t.fldptr.type;
if (is_class (type)) if (is_class (type))
return type->t.class->categories; return type->t.class->categories;
@ -430,7 +430,7 @@ obj_classname (const type_t *type)
} else if (is_Class (type)) { } else if (is_Class (type)) {
dstring_copystr (str, "Class"); dstring_copystr (str, "Class");
} else { } else {
if (is_pointer (type)) if (is_ptr (type))
type = type->t.fldptr.type; type = type->t.fldptr.type;
if (is_class (type)) if (is_class (type))
dstring_copystr (str, type->t.class->name); dstring_copystr (str, type->t.class->name);
@ -1738,7 +1738,7 @@ emit_protocol (protocol_t *protocol)
static void static void
emit_protocol_next (def_t *def, void *data, int index) emit_protocol_next (def_t *def, void *data, int index)
{ {
if (!is_pointer(def->type)) { if (!is_ptr(def->type)) {
internal_error (0, "%s: expected pointer def", __FUNCTION__); internal_error (0, "%s: expected pointer def", __FUNCTION__);
} }
D_INT (def) = 0; D_INT (def) = 0;
@ -1761,7 +1761,7 @@ emit_protocol_list_item (def_t *def, void *data, int index)
protocollist_t *protocols = (protocollist_t *) data; protocollist_t *protocols = (protocollist_t *) data;
protocol_t *protocol = protocols->list[index]; protocol_t *protocol = protocols->list[index];
if (!is_array (def->type) || !is_pointer(def->type->t.array.type)) { if (!is_array (def->type) || !is_ptr(def->type->t.array.type)) {
internal_error (0, "%s: expected array of pointer def", __FUNCTION__); internal_error (0, "%s: expected array of pointer def", __FUNCTION__);
} }
if (index < 0 || index >= protocols->count) { if (index < 0 || index >= protocols->count) {

View file

@ -90,8 +90,7 @@ get_value_def (expr_t *expr, ex_value_t *value, type_t *type)
def->offset = value->v.short_val; def->offset = value->v.short_val;
return def; return def;
} }
if (is_pointer (type) && value->v.pointer.tempop if (is_ptr (type) && value->v.pointer.tempop && !value->v.pointer.def) {
&& !value->v.pointer.def) {
value->v.pointer.def = get_tempop_def (expr, value->v.pointer.tempop, value->v.pointer.def = get_tempop_def (expr, value->v.pointer.tempop,
type->t.fldptr.type); type->t.fldptr.type);
} }

View file

@ -2816,15 +2816,14 @@ cast_expr (type_t *dstType, expr_t *e)
if ((dstType == type_default && is_enum (srcType)) if ((dstType == type_default && is_enum (srcType))
|| (is_enum (dstType) && srcType == type_default)) || (is_enum (dstType) && srcType == type_default))
return e; return e;
if ((is_pointer (dstType) && is_string (srcType)) if ((is_ptr (dstType) && is_string (srcType))
|| (is_string (dstType) && is_pointer (srcType))) { || (is_string (dstType) && is_ptr (srcType))) {
c = new_alias_expr (dstType, e); c = new_alias_expr (dstType, e);
return c; return c;
} }
if (!(is_pointer (dstType) if (!(is_ptr (dstType) && (is_ptr (srcType) || is_integral (srcType)
&& (is_pointer (srcType) || is_integral (srcType)
|| is_array (srcType))) || is_array (srcType)))
&& !(is_integral (dstType) && is_pointer (srcType)) && !(is_integral (dstType) && is_ptr (srcType))
&& !(is_func (dstType) && is_func (srcType)) && !(is_func (dstType) && is_func (srcType))
&& !(is_scalar (dstType) && is_scalar (srcType))) { && !(is_scalar (dstType) && is_scalar (srcType))) {
return cast_error (e, srcType, dstType); return cast_error (e, srcType, dstType);

View file

@ -329,7 +329,7 @@ assign_expr (expr_t *dst, expr_t *src)
internal_error (src, "src_type broke in assign_expr"); internal_error (src, "src_type broke in assign_expr");
} }
if (is_pointer (dst_type) && is_array (src_type)) { if (is_ptr (dst_type) && is_array (src_type)) {
// assigning an array to a pointer is the same as taking the address of // assigning an array to a pointer is the same as taking the address of
// the array but using the type of the array elements // the array but using the type of the array elements
src = address_expr (src, 0, src_type->t.fldptr.type); src = address_expr (src, 0, src_type->t.fldptr.type);

View file

@ -646,10 +646,10 @@ pointer_arithmetic (int op, expr_t *e1, expr_t *e2)
expr_t *psize; expr_t *psize;
type_t *ptype; type_t *ptype;
if (!is_pointer (t1) && !is_pointer (t2)) { if (!is_ptr (t1) && !is_ptr (t2)) {
internal_error (e1, "pointer arithmetic on non-pointers"); internal_error (e1, "pointer arithmetic on non-pointers");
} }
if (is_pointer (t1) && is_pointer (t2)) { if (is_ptr (t1) && is_ptr (t2)) {
if (op != '-') { if (op != '-') {
return error (e2, "invalid pointer operation"); return error (e2, "invalid pointer operation");
} }
@ -661,11 +661,11 @@ pointer_arithmetic (int op, expr_t *e1, expr_t *e2)
e2 = cast_expr (&type_int, e2); e2 = cast_expr (&type_int, e2);
psize = new_int_expr (type_size (t1->t.fldptr.type)); psize = new_int_expr (type_size (t1->t.fldptr.type));
return binary_expr ('/', binary_expr ('-', e1, e2), psize); return binary_expr ('/', binary_expr ('-', e1, e2), psize);
} else if (is_pointer (t1)) { } else if (is_ptr (t1)) {
offset = cast_expr (&type_int, e2); offset = cast_expr (&type_int, e2);
ptr = cast_expr (&type_int, e1); ptr = cast_expr (&type_int, e1);
ptype = t1; ptype = t1;
} else if (is_pointer (t2)) { } else if (is_ptr (t2)) {
offset = cast_expr (&type_int, e1); offset = cast_expr (&type_int, e1);
ptr = cast_expr (&type_int, e2); ptr = cast_expr (&type_int, e2);
ptype = t2; ptype = t2;

View file

@ -533,7 +533,7 @@ emit_selectors (void)
static void static void
emit_methods_next (def_t *def, void *data, int index) emit_methods_next (def_t *def, void *data, int index)
{ {
if (!is_pointer(def->type)) if (!is_ptr(def->type))
internal_error (0, "%s: expected pointer def", __FUNCTION__); internal_error (0, "%s: expected pointer def", __FUNCTION__);
D_INT (def) = 0; D_INT (def) = 0;
} }

View file

@ -554,7 +554,7 @@ unalias_type (const type_t *type)
const type_t * const type_t *
dereference_type (const type_t *type) dereference_type (const type_t *type)
{ {
if (!is_pointer (type) && !is_field (type)) { if (!is_ptr (type) && !is_field (type)) {
internal_error (0, "dereference non pointer/field type"); internal_error (0, "dereference non pointer/field type");
} }
if (type->meta == ty_alias) { if (type->meta == ty_alias) {
@ -836,12 +836,13 @@ encode_type (dstring_t *encoding, const type_t *type)
internal_error (0, "bad type meta:type %d:%d", type->meta, type->type); internal_error (0, "bad type meta:type %d:%d", type->meta, type->type);
} }
int #define EV_TYPE(t) \
is_void (const type_t *type) int is_##t (const type_t *type) \
{ { \
type = unalias_type (type); type = unalias_type (type); \
return type->type == ev_void; return type->type == ev_##t; \
} }
#include "QF/progs/pr_type_names.h"
int int
is_enum (const type_t *type) is_enum (const type_t *type)
@ -852,39 +853,6 @@ is_enum (const type_t *type)
return 0; return 0;
} }
int
is_int (const type_t *type)
{
type = unalias_type (type);
etype_t t = type->type;
if (t == ev_int)
return 1;
return is_enum (type);
}
int
is_uint (const type_t *type)
{
type = unalias_type (type);
etype_t t = type->type;
if (t == ev_uint)
return 1;
return is_enum (type);
}
int
is_short (const type_t *type)
{
type = unalias_type (type);
etype_t t = type->type;
if (t == ev_short)
return 1;
return is_enum (type);
}
int int
is_integral (const type_t *type) is_integral (const type_t *type)
{ {
@ -894,20 +862,6 @@ is_integral (const type_t *type)
return is_enum (type); return is_enum (type);
} }
int
is_double (const type_t *type)
{
type = unalias_type (type);
return type->type == ev_double;
}
int
is_float (const type_t *type)
{
type = unalias_type (type);
return type->type == ev_float;
}
int int
is_scalar (const type_t *type) is_scalar (const type_t *type)
{ {
@ -915,20 +869,6 @@ is_scalar (const type_t *type)
return is_float (type) || is_integral (type) || is_double (type); return is_float (type) || is_integral (type) || is_double (type);
} }
int
is_vector (const type_t *type)
{
type = unalias_type (type);
return type->type == ev_vector;
}
int
is_quaternion (const type_t *type)
{
type = unalias_type (type);
return type->type == ev_quaternion;
}
int int
is_math (const type_t *type) is_math (const type_t *type)
{ {
@ -948,33 +888,6 @@ is_struct (const type_t *type)
return 0; return 0;
} }
int
is_pointer (const type_t *type)
{
type = unalias_type (type);
if (type->type == ev_ptr)
return 1;
return 0;
}
int
is_field (const type_t *type)
{
type = unalias_type (type);
if (type->type == ev_field)
return 1;
return 0;
}
int
is_entity (const type_t *type)
{
type = unalias_type (type);
if (type->type == ev_entity)
return 1;
return 0;
}
int int
is_array (const type_t *type) is_array (const type_t *type)
{ {
@ -984,15 +897,6 @@ is_array (const type_t *type)
return 0; return 0;
} }
int
is_func (const type_t *type)
{
type = unalias_type (type);
if (type->type == ev_func)
return 1;
return 0;
}
int int
is_structural (const type_t *type) is_structural (const type_t *type)
{ {
@ -1000,15 +904,6 @@ is_structural (const type_t *type)
return is_struct (type) || is_array (type); return is_struct (type) || is_array (type);
} }
int
is_string (const type_t *type)
{
type = unalias_type (type);
if (type->type == ev_string)
return 1;
return 0;
}
int int
type_compatible (const type_t *dst, const type_t *src) type_compatible (const type_t *dst, const type_t *src)
{ {
@ -1022,7 +917,7 @@ type_compatible (const type_t *dst, const type_t *src)
if (is_func (dst) && is_func (src)) { if (is_func (dst) && is_func (src)) {
return 1; return 1;
} }
if (is_pointer (dst) && is_pointer (src)) { if (is_ptr (dst) && is_ptr (src)) {
return 1; return 1;
} }
return 0; return 0;
@ -1042,12 +937,12 @@ type_assignable (const type_t *dst, const type_t *src)
if (dst->type == ev_field && src->type == ev_field) if (dst->type == ev_field && src->type == ev_field)
return 1; return 1;
// pointer = array // pointer = array
if (is_pointer (dst) && is_array (src)) { if (is_ptr (dst) && is_array (src)) {
if (dst->t.fldptr.type == src->t.array.type) if (dst->t.fldptr.type == src->t.array.type)
return 1; return 1;
return 0; return 0;
} }
if (!is_pointer (dst) || !is_pointer (src)) if (!is_ptr (dst) || !is_ptr (src))
return is_scalar (dst) && is_scalar (src); return is_scalar (dst) && is_scalar (src);
// pointer = pointer // pointer = pointer