7f64f863b67673ed2096f35797fcf5e82f537fdc
[cparser] / type.c
1 #include <config.h>
2
3 #include "type_t.h"
4 #include "adt/error.h"
5
6 static struct obstack  _type_obst;
7 struct obstack        *type_obst = &_type_obst;
8
9 void init_type_module()
10 {
11         obstack_init(type_obst);
12 }
13
14 void exit_type_module()
15 {
16         obstack_free(type_obst, NULL);
17 }
18
19 static
20 void print_atomic_type(FILE *out, const atomic_type_t *type)
21 {
22         switch(type->atype) {
23         case ATOMIC_TYPE_INVALID:   fputs("INVALIDATOMIC", out); break;
24         case ATOMIC_TYPE_BOOL:      fputs("bool", out); break;
25         case ATOMIC_TYPE_CHAR:      fputs("char", out); break;
26         case ATOMIC_TYPE_SCHAR:     fputs("signed char", out); break;
27         case ATOMIC_TYPE_UCHAR:     fputs("unsigned char", out); break;
28         case ATOMIC_TYPE_INT:       fputs("int", out); break;
29         case ATOMIC_TYPE_UINT:      fputs("unsigned int", out); break;
30         case ATOMIC_TYPE_SHORT:     fputs("short", out); break;
31         case ATOMIC_TYPE_USHORT:    fputs("unsigned short", out); break;
32         case ATOMIC_TYPE_LONG:      fputs("long", out); break;
33         case ATOMIC_TYPE_ULONG:     fputs("unsigned long", out); break;
34         case ATOMIC_TYPE_LONGLONG:  fputs("long long", out); break;
35         case ATOMIC_TYPE_ULONGLONG: fputs("unsigned long long", out); break;
36         case ATOMIC_TYPE_FLOAT:     fputs("float", out); break;
37         case ATOMIC_TYPE_DOUBLE:    fputs("double", out); break;
38         default:                    fputs("UNKNOWNATOMIC", out); break;
39         }
40 }
41
42 static
43 void print_method_type(FILE *out, const method_type_t *type)
44 {
45         fputs("<", out);
46         print_type(out, type->result_type);
47         fputs(" ", out);
48
49         if(type->abi_style != NULL) {
50                 fprintf(out, "\"%s\" ", type->abi_style);
51         }
52         fputs("method(", out);
53         method_parameter_type_t *param_type = type->parameter_types;
54         int first = 1;
55         while(param_type != NULL) {
56                 if(first) {
57                         first = 0;
58                 } else {
59                         fputs(", ", out);
60                 }
61                 print_type(out, param_type->type);
62                 param_type = param_type->next;
63         }
64         fputs(")>", out);
65 }
66
67 static
68 void print_pointer_type(FILE *out, const pointer_type_t *type)
69 {
70         print_type(out, type->points_to);
71         fputs("*", out);
72 }
73
74 void print_type(FILE *out, const type_t *type)
75 {
76         if(type == NULL) {
77                 fputs("nil type", out);
78                 return;
79         }
80
81         switch(type->type) {
82         case TYPE_INVALID:
83                 fputs("invalid", out);
84                 return;
85         case TYPE_ENUM:
86         case TYPE_TYPEDEF:
87                 fputs("TODO", out);
88                 return;
89         case TYPE_ATOMIC:
90                 print_atomic_type(out, (const atomic_type_t*) type);
91                 return;
92         case TYPE_COMPOUND_STRUCT:
93         case TYPE_COMPOUND_UNION:
94                 fprintf(out, "%s", ((const compound_type_t*) type)->symbol->string);
95                 return;
96         case TYPE_METHOD:
97                 print_method_type(out, (const method_type_t*) type);
98                 return;
99         case TYPE_POINTER:
100                 print_pointer_type(out, (const pointer_type_t*) type);
101                 return;
102         }
103         fputs("unknown", out);
104 }
105
106 int type_valid(const type_t *type)
107 {
108         return type->type != TYPE_INVALID;
109 }
110
111 int is_type_int(const type_t *type)
112 {
113         if(type->type != TYPE_ATOMIC)
114                 return 0;
115
116         atomic_type_t *atomic_type = (atomic_type_t*) type;
117         switch(atomic_type->atype) {
118         case ATOMIC_TYPE_CHAR:
119         case ATOMIC_TYPE_SCHAR:
120         case ATOMIC_TYPE_UCHAR:
121         case ATOMIC_TYPE_SHORT:
122         case ATOMIC_TYPE_USHORT:
123         case ATOMIC_TYPE_INT:
124         case ATOMIC_TYPE_UINT:
125         case ATOMIC_TYPE_LONG:
126         case ATOMIC_TYPE_ULONG:
127         case ATOMIC_TYPE_LONGLONG:
128         case ATOMIC_TYPE_ULONGLONG:
129                 return 1;
130         default:
131                 return 0;
132         }
133 }
134
135 static __attribute__((unused))
136 void dbg_type(const type_t *type)
137 {
138         print_type(stdout,type);
139         puts("\n");
140         fflush(stdout);
141 }