| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518 |
- #include <assert.h>
- #include <errno.h>
- #include <stdarg.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "printxf.h"
- #include "type.h"
- static void __sgr(FILE *fp, int sgr, const char *s)
- {
- if (!s)
- return;
- fprintf(fp, "\e[%dm%s\e[0m", sgr, s);
- }
- static void __bold(FILE *fp, const char *s)
- {
- __sgr(fp, 1, s);
- }
- static void __faint(FILE *fp, const char *s)
- {
- __sgr(fp, 2, s);
- }
- static void type_dump_func(struct type *t, const char *name, FILE *fp)
- {
- struct tfield *arg;
- type_dump(t->func.type, NULL, fp);
- fprintf(fp, " %s(*\e[1m%s\e[0m)(",
- t->func.aggregation ? "@" : "", name ? : "");
- if (!t->func.args) {
- __faint(fp, t->func.vargs ? "..." : "void");
- fputc(')', fp);
- return;
- }
- tfields_foreach(arg, t->func.args) {
- if (arg != t->func.args)
- fputs(", ", fp);
- type_dump(arg->type, NULL, fp);
- }
- if (t->func.vargs)
- __faint(fp, ", ...");
- fputc(')', fp);
- }
- void type_dump(struct type *t, const char *name, FILE *fp)
- {
- if (!t) {
- __faint(fp, "(none)");
- goto print_name;
- }
- switch (t->ttype){
- case T_VOID:
- __faint(fp, "void");
- break;
- case T_TYPEDEF:
- __faint(fp, t->tdef.name);
- break;
- case T_SCALAR:
- __faint(fp, t->scalar.name);
- break;
- case T_POINTER:
- type_dump(t->ptr.type, NULL, fp);
- fputs(" *", fp);
- __bold(fp, name);
- return;
- case T_ARRAY:
- type_dump(t->array.type, NULL, fp);
- fputs(name ? " " : "", fp);
- __bold(fp, name);
- fprintf(fp, "[%zu]", t->array.len);
- return;
- case T_STRUCT:
- fputs("struct ", fp);
- __faint(fp, t->sou.name);
- break;
- case T_FUNC:
- type_dump_func(t, name, fp);
- return;
- case T_MAP:
- type_dump(t->map.vtype, NULL, fp);
- fputs(name ? " " : "", fp);
- __bold(fp, name);
- fputc('{', fp);
- type_dump(t->map.ktype, NULL, fp);
- fputc('}', fp);
- return;
- }
- print_name:
- fputs(name ? " " : "", fp);
- __bold(fp, name);
- }
- static void type_dump_decl_sou(struct type *t, FILE *fp)
- {
- struct tfield *f;
- type_dump(t, NULL, fp);
- fputs(" {\n", fp);
- for (f = t->sou.fields; f->type->ttype != T_VOID; f++) {
- fputc('\t', fp);
- type_dump(f->type, NULL, fp);
- fprintf(fp, " %s;\n", f->name);
- }
- fputs("}", fp);
- }
- void type_dump_decl(struct type *t, FILE *fp)
- {
- switch (t->ttype) {
- case T_TYPEDEF:
- fputs("typedef ", fp);
- type_dump(t->tdef.type, NULL, fp);
- fprintf(fp, " %s", t->tdef.name);
- break;
- case T_STRUCT:
- type_dump_decl_sou(t, fp);
- break;
- case T_VOID:
- case T_SCALAR:
- case T_POINTER:
- case T_ARRAY:
- case T_MAP:
- case T_FUNC:
- type_dump(t, NULL, fp);
- break;
- }
- }
- int type_vfprintxf(struct printxf *pxf, FILE *fp, const char *spec, va_list ap)
- {
- struct type *t;
- t = va_arg(ap, struct type *);
- type_dump(t, NULL, fp);
- return 0;
- }
- struct type *type_normalize(struct type *t)
- {
- while (t->ttype == T_TYPEDEF)
- t = t->tdef.type;
- return t;
- }
- int type_equal(struct type *a, struct type *b)
- {
- /* TODO */
- return a == b;
- }
- int type_compatible(struct type *a, struct type *b)
- {
- a = type_normalize(a);
- b = type_normalize(b);
- if (a->ttype != b->ttype)
- return 0;
- switch (a->ttype){
- case T_VOID:
- case T_SCALAR:
- case T_POINTER:
- return 1;
- case T_ARRAY:
- if (a->array.len != b->array.len)
- return 0;
- return type_compatible(a->array.type, b->array.type);
- case T_STRUCT:
- /* case T_UNION: */
- return !strcmp(a->sou.name, b->sou.name);
- case T_FUNC:
- return type_compatible(a->func.type, b->func.type);
- case T_MAP:
- return type_compatible(a->map.vtype, b->map.vtype);
- case T_TYPEDEF:
- assert(0);
- }
- assert(0);
- return 0;
- }
- static ssize_t type_alignof_struct(struct type *t)
- {
- struct tfield *f;
- ssize_t falign, align = -EINVAL;
- if (t->sou.packed)
- return 1;
- tfields_foreach(f, t->sou.fields) {
- falign = type_alignof(f->type);
- if (falign < 0)
- return falign;
- if (falign > align)
- align = falign;
- }
- return align;
- }
- ssize_t type_alignof(struct type *t)
- {
- if (!t)
- return -EINVAL;
- switch (t->ttype){
- case T_VOID:
- case T_SCALAR:
- case T_POINTER:
- case T_FUNC:
- case T_MAP:
- return type_sizeof(t);
- case T_TYPEDEF:
- return type_alignof(t->tdef.type);
- case T_ARRAY:
- return type_alignof(t->array.type);
- case T_STRUCT:
- return type_alignof_struct(t);
- }
- return -EINVAL;
- }
- static size_t __padding(size_t offset, size_t align)
- {
- size_t pad = align - (offset & (align - 1));
- return (pad == align) ? 0 : pad;
- }
- ssize_t type_offset_size_of(struct type *t, const char *field)
- {
- struct tfield *f;
- size_t offset = 0;
- ssize_t fsize, falign;
- assert(t->ttype == T_STRUCT);
- if (!t->sou.fields)
- return -ENOENT;
- tfields_foreach(f, t->sou.fields) {
- fsize = type_sizeof(f->type);
- if (fsize < 0)
- return fsize;
- falign = type_alignof(f->type);
- if (falign < 0)
- return falign;
- if (!t->sou.packed)
- offset += __padding(offset, falign);
- if (field && !strcmp(f->name, field))
- return offset;
- offset += fsize;
- }
- if (field)
- return -ENOENT;
- if (!t->sou.packed)
- offset += __padding(offset, type_alignof(t));
- return offset;
-
- }
- ssize_t type_offsetof(struct type *t, const char *field)
- {
- if (!t)
- return -EINVAL;
- return type_offset_size_of(t, field);
- }
- ssize_t type_sizeof_struct(struct type *t)
- {
- return type_offset_size_of(t, NULL);
- }
- ssize_t type_sizeof(struct type *t)
- {
- if (!t)
- return -EINVAL;
- switch (t->ttype){
- case T_VOID:
- return sizeof(void);
- case T_SCALAR:
- return t->scalar.size;
- case T_TYPEDEF:
- return type_sizeof(t->tdef.type);
- case T_POINTER:
- case T_FUNC:
- return sizeof(void *);
- case T_ARRAY:
- return t->array.len * type_sizeof(t->array.type);
- case T_STRUCT:
- return type_sizeof_struct(t);
- case T_MAP:
- return sizeof(int);
- }
- return -EINVAL;
- }
- struct tfield *tfields_get(struct tfield *fields, const char *name)
- {
- struct tfield *f;
- tfields_foreach(f, fields) {
- if (!strcmp(f->name, name))
- return f;
- }
- return NULL;
- }
- int all_types_cmp(const void *_a, const void *_b)
- {
- const struct type *a = *((struct type **)_a);
- const struct type *b = *((struct type **)_b);
- return a - b;
- }
- struct type_list {
- struct type **types;
- size_t len;
- } all_types;
- #define types_foreach(_t) \
- for ((_t) = all_types.types; (_t) < &all_types.types[all_types.len]; (_t)++)
- void type_dump_decls(FILE *fp)
- {
- struct type **ti, *t;
- types_foreach(ti) {
- t = *ti;
- if (t->ttype == T_SCALAR)
- continue;
- type_dump_decl(t, fp);
- fputc('\n', fp);
- }
- }
- int type_add(struct type *t)
- {
- if (bsearch(t, all_types.types, all_types.len,
- sizeof(*all_types.types), all_types_cmp))
- return 0;
- /* type_size_set(t); */
- all_types.types = realloc(all_types.types,
- ++all_types.len * sizeof(*all_types.types));
- all_types.types[all_types.len - 1] = t;
- qsort(all_types.types, all_types.len, sizeof(*all_types.types), all_types_cmp);
- return 0;
- }
- int type_add_list(struct type **ts)
- {
- int err;
- for (; *ts; ts++) {
- err = type_add(*ts);
- if (err)
- return err;
- }
- return 0;
- }
- struct type *type_array_of(struct type *type, size_t len)
- {
- struct type **ti, *t;
- types_foreach(ti) {
- t = *ti;
- if ((t->ttype == T_ARRAY)
- && (t->array.type == type)
- && (t->array.len == len))
- return t;
- }
- t = calloc(1, sizeof(*t));
- t->ttype = T_ARRAY;
- t->array.type = type;
- t->array.len = len;
- type_add(t);
- return t;
- }
- struct type *type_map_of(struct type *ktype, struct type *vtype)
- {
- struct type **ti, *t;
- types_foreach(ti) {
- t = *ti;
- if ((t->ttype == T_MAP)
- && (t->map.ktype == ktype)
- && (t->map.vtype == vtype))
- return t;
- }
- t = calloc(1, sizeof(*t));
- t->ttype = T_MAP;
- t->map.vtype = vtype;
- t->map.ktype = ktype;
- type_add(t);
- return t;
- }
- struct type *type_ptr_of(struct type *type)
- {
- struct type **ti, *t;
- types_foreach(ti) {
- t = *ti;
- if ((t->ttype == T_POINTER)
- && (t->ptr.type == type))
- return t;
- }
- t = calloc(1, sizeof(*t));
- t->ttype = T_POINTER;
- t->ptr.type = type;
- type_add(t);
- return t;
- }
- #define is_signed(_t) (((_t)(-1)) < 0)
- #define builtin_scalar(_t) { \
- .ttype = T_SCALAR, \
- .scalar = { \
- .name = #_t, \
- .size = sizeof(_t), \
- .is_signed = is_signed(_t), \
- }, \
- }
- struct type t_void = { .ttype = T_VOID };
- #pragma GCC diagnostic ignored "-Wtype-limits"
- /* is_signed will generate a warning for unsigned types since the
- * expression can never be true. this is exactly what we're interested
- * in here though. it gets us out of having to specify scalar
- * signedness per architecture. */
- struct type t_char = builtin_scalar(char);
- struct type t_schar = builtin_scalar(signed char);
- struct type t_uchar = builtin_scalar(unsigned char);
- struct type t_short = builtin_scalar(short);
- struct type t_sshort = builtin_scalar(signed short);
- struct type t_ushort = builtin_scalar(unsigned short);
- struct type t_int = builtin_scalar(int);
- struct type t_sint = builtin_scalar(signed int);
- struct type t_uint = builtin_scalar(unsigned int);
- struct type t_long = builtin_scalar(long);
- struct type t_slong = builtin_scalar(signed long);
- struct type t_ulong = builtin_scalar(unsigned long);
- struct type t_llong = builtin_scalar(long long);
- struct type t_sllong = builtin_scalar(signed long long);
- struct type t_ullong = builtin_scalar(unsigned long long);
- #pragma GCC diagnostic pop
- struct type *builtin_types[] = {
- &t_void,
- &t_char, &t_schar, &t_uchar,
- &t_short, &t_sshort, &t_ushort,
- &t_int, &t_sint, &t_uint,
- &t_long, &t_slong, &t_ulong,
- &t_llong, &t_sllong, &t_ullong,
- NULL
- };
- __attribute__((constructor))
- static void type_init(void)
- {
- type_add_list(builtin_types);
- printxf_default.vfprintxf['T'] = type_vfprintxf;
- }
|