|
|
@@ -6,15 +6,34 @@
|
|
6
|
6
|
|
|
7
|
7
|
#include "type.h"
|
|
8
|
8
|
|
|
|
9
|
+static void __sgr(FILE *fp, int sgr, const char *s)
|
|
|
10
|
+{
|
|
|
11
|
+ if (!s)
|
|
|
12
|
+ return;
|
|
|
13
|
+
|
|
|
14
|
+ fprintf(fp, "\e[%dm%s\e[0m", sgr, s);
|
|
|
15
|
+}
|
|
|
16
|
+
|
|
|
17
|
+static void __bold(FILE *fp, const char *s)
|
|
|
18
|
+{
|
|
|
19
|
+ __sgr(fp, 1, s);
|
|
|
20
|
+}
|
|
|
21
|
+
|
|
|
22
|
+static void __faint(FILE *fp, const char *s)
|
|
|
23
|
+{
|
|
|
24
|
+ __sgr(fp, 2, s);
|
|
|
25
|
+}
|
|
|
26
|
+
|
|
9
|
27
|
static void type_dump_func(struct type *t, const char *name, FILE *fp)
|
|
10
|
28
|
{
|
|
11
|
29
|
struct tfield *arg;
|
|
12
|
30
|
|
|
13
|
31
|
type_dump(t->func.type, NULL, fp);
|
|
14
|
|
- fprintf(fp, " (*%s)(", name ? : "");
|
|
|
32
|
+ fprintf(fp, " (*\e[1m%s\e[0m)(", name ? : "");
|
|
15
|
33
|
|
|
16
|
34
|
if (!t->func.args) {
|
|
17
|
|
- fputs("void)", fp);
|
|
|
35
|
+ __faint(fp, "void");
|
|
|
36
|
+ fputc(')', fp);
|
|
18
|
37
|
return;
|
|
19
|
38
|
}
|
|
20
|
39
|
|
|
|
@@ -37,33 +56,46 @@ void type_dump(struct type *t, const char *name, FILE *fp)
|
|
37
|
56
|
switch (t->ttype){
|
|
38
|
57
|
case T_VOID:
|
|
39
|
58
|
print_void:
|
|
40
|
|
- fprintf(fp, "\e[2mvoid\e[0m%s%s", name? " " : "", name ? : "");
|
|
|
59
|
+ __faint(fp, "void");
|
|
|
60
|
+ fputs(name ? " " : "", fp);
|
|
|
61
|
+ __bold(fp, name);
|
|
41
|
62
|
break;
|
|
42
|
63
|
case T_TYPEDEF:
|
|
43
|
|
- fprintf(fp, "\e[2m%s\e[0m%s%s", t->tdef.name, name? " " : "", name ? : "");
|
|
|
64
|
+ __faint(fp, t->tdef.name);
|
|
|
65
|
+ fputs(name ? " " : "", fp);
|
|
|
66
|
+ __bold(fp, name);
|
|
44
|
67
|
break;
|
|
45
|
68
|
case T_SCALAR:
|
|
46
|
|
- fprintf(fp, "\e[2m%s\e[0m%s%s", t->scalar.name, name? " " : "", name ? : "");
|
|
|
69
|
+ __faint(fp, t->scalar.name);
|
|
|
70
|
+ fputs(name ? " " : "", fp);
|
|
|
71
|
+ __bold(fp, name);
|
|
47
|
72
|
break;
|
|
48
|
73
|
case T_POINTER:
|
|
49
|
74
|
type_dump(t->ptr.type, NULL, fp);
|
|
50
|
|
- fprintf(fp, " *%s", name ? : "");
|
|
|
75
|
+ fputs(" *", fp);
|
|
|
76
|
+ __bold(fp, name);
|
|
51
|
77
|
break;
|
|
52
|
78
|
case T_ARRAY:
|
|
53
|
79
|
type_dump(t->array.type, NULL, fp);
|
|
54
|
|
- fprintf(fp, "%s%s[%zu]", name ? " " : "", name? : "", t->array.len);
|
|
|
80
|
+ fputs(name ? " " : "", fp);
|
|
|
81
|
+ __bold(fp, name);
|
|
|
82
|
+ fprintf(fp, "[%zu]", t->array.len);
|
|
55
|
83
|
break;
|
|
56
|
84
|
case T_STRUCT:
|
|
57
|
|
- fprintf(fp, "struct %s%s%s", t->sou.name, name? " " : "", name? : "");
|
|
|
85
|
+ fputs("struct ", fp);
|
|
|
86
|
+ __faint(fp, t->sou.name);
|
|
|
87
|
+ __bold(fp, name);
|
|
58
|
88
|
break;
|
|
59
|
89
|
case T_FUNC:
|
|
60
|
90
|
type_dump_func(t, name, fp);
|
|
61
|
91
|
break;
|
|
62
|
92
|
case T_MAP:
|
|
63
|
93
|
type_dump(t->map.vtype, NULL, fp);
|
|
64
|
|
- fprintf(fp, " %s{", name ? : "");
|
|
|
94
|
+ fputs(name ? " " : "", fp);
|
|
|
95
|
+ __bold(fp, name);
|
|
|
96
|
+ fputc('{', fp);
|
|
65
|
97
|
type_dump(t->map.ktype, NULL, fp);
|
|
66
|
|
- fputs("} ", fp);
|
|
|
98
|
+ fputc('}', fp);
|
|
67
|
99
|
break;
|
|
68
|
100
|
}
|
|
69
|
101
|
}
|
|
|
@@ -74,7 +106,7 @@ static void type_dump_decl_sou(struct type *t, FILE *fp)
|
|
74
|
106
|
|
|
75
|
107
|
type_dump(t, NULL, fp);
|
|
76
|
108
|
fputs(" {\n", fp);
|
|
77
|
|
- for (f = t->sou.fields; f->type; f++) {
|
|
|
109
|
+ for (f = t->sou.fields; f->type->ttype != T_VOID; f++) {
|
|
78
|
110
|
fputc('\t', fp);
|
|
79
|
111
|
type_dump(f->type, NULL, fp);
|
|
80
|
112
|
fprintf(fp, " %s;\n", f->name);
|
|
|
@@ -301,7 +333,21 @@ struct type_list {
|
|
301
|
333
|
} all_types;
|
|
302
|
334
|
|
|
303
|
335
|
#define types_foreach(_t) \
|
|
304
|
|
- for ((_t) = all_types.types[0]; (_t) <= all_types.types[all_types.len]; (_t)++)
|
|
|
336
|
+ for ((_t) = all_types.types; (_t) < &all_types.types[all_types.len]; (_t)++)
|
|
|
337
|
+
|
|
|
338
|
+void type_dump_decls(FILE *fp)
|
|
|
339
|
+{
|
|
|
340
|
+ struct type **ti, *t;
|
|
|
341
|
+
|
|
|
342
|
+ types_foreach(ti) {
|
|
|
343
|
+ t = *ti;
|
|
|
344
|
+ if (t->ttype == T_SCALAR)
|
|
|
345
|
+ continue;
|
|
|
346
|
+
|
|
|
347
|
+ type_dump_decl(t, fp);
|
|
|
348
|
+ fputc('\n', fp);
|
|
|
349
|
+ }
|
|
|
350
|
+}
|
|
305
|
351
|
|
|
306
|
352
|
int type_add(struct type *t)
|
|
307
|
353
|
{
|
|
|
@@ -334,9 +380,10 @@ int type_add_list(struct type **ts)
|
|
334
|
380
|
|
|
335
|
381
|
struct type *type_array_of(struct type *type, size_t len)
|
|
336
|
382
|
{
|
|
337
|
|
- struct type *t;
|
|
|
383
|
+ struct type **ti, *t;
|
|
338
|
384
|
|
|
339
|
|
- types_foreach(t) {
|
|
|
385
|
+ types_foreach(ti) {
|
|
|
386
|
+ t = *ti;
|
|
340
|
387
|
if ((t->ttype == T_ARRAY)
|
|
341
|
388
|
&& (t->array.type == type)
|
|
342
|
389
|
&& (t->array.len == len))
|
|
|
@@ -353,9 +400,10 @@ struct type *type_array_of(struct type *type, size_t len)
|
|
353
|
400
|
|
|
354
|
401
|
struct type *type_map_of(struct type *ktype, struct type *vtype)
|
|
355
|
402
|
{
|
|
356
|
|
- struct type *t;
|
|
|
403
|
+ struct type **ti, *t;
|
|
357
|
404
|
|
|
358
|
|
- types_foreach(t) {
|
|
|
405
|
+ types_foreach(ti) {
|
|
|
406
|
+ t = *ti;
|
|
359
|
407
|
if ((t->ttype == T_MAP)
|
|
360
|
408
|
&& (t->map.ktype == ktype)
|
|
361
|
409
|
&& (t->map.vtype == vtype))
|
|
|
@@ -372,9 +420,10 @@ struct type *type_map_of(struct type *ktype, struct type *vtype)
|
|
372
|
420
|
|
|
373
|
421
|
struct type *type_ptr_of(struct type *type)
|
|
374
|
422
|
{
|
|
375
|
|
- struct type *t;
|
|
|
423
|
+ struct type **ti, *t;
|
|
376
|
424
|
|
|
377
|
|
- types_foreach(t) {
|
|
|
425
|
+ types_foreach(ti) {
|
|
|
426
|
+ t = *ti;
|
|
378
|
427
|
if ((t->ttype == T_POINTER)
|
|
379
|
428
|
&& (t->ptr.type == type))
|
|
380
|
429
|
return t;
|