/
toctype.c
255 lines (224 loc) · 7.13 KB
/
toctype.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright (c) 1999-2013 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
// License for redistribution is by either the Artistic License
// in artistic.txt, or the GNU General Public License in gnu.txt.
// See the included readme.txt for details.
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include "mars.h"
#include "module.h"
#include "mtype.h"
#include "declaration.h"
#include "enum.h"
#include "aggregate.h"
#include "cc.h"
#include "global.h"
#include "type.h"
void slist_add(Symbol *s);
void slist_reset();
/***************************************
* Convert from D type to C type.
* This is done so C debug info can be generated.
*/
type *Type_toCtype(Type *t);
class ToCtypeVisitor : public Visitor
{
public:
ToCtypeVisitor() {}
void visit(Type *t)
{
t->ctype = type_fake(t->totym());
t->ctype->Tcount++;
}
void visit(TypeSArray *t)
{
t->ctype = type_static_array(t->dim->toInteger(), Type_toCtype(t->next));
}
void visit(TypeDArray *t)
{
t->ctype = type_dyn_array(Type_toCtype(t->next));
t->ctype->Tident = t->toChars(); // needed to generate sensible debug info for cv8
}
void visit(TypeAArray *t)
{
t->ctype = type_assoc_array(Type_toCtype(t->index), Type_toCtype(t->next));
}
void visit(TypePointer *t)
{
//printf("TypePointer::toCtype() %s\n", t->toChars());
t->ctype = type_pointer(Type_toCtype(t->next));
}
void visit(TypeFunction *t)
{
size_t nparams = Parameter::dim(t->parameters);
type *tmp[10];
type **ptypes = tmp;
if (nparams > 10)
ptypes = (type **)malloc(sizeof(type*) * nparams);
for (size_t i = 0; i < nparams; i++)
{
Parameter *arg = Parameter::getNth(t->parameters, i);
type *tp = Type_toCtype(arg->type);
if (arg->storageClass & (STCout | STCref))
tp = type_allocn(TYref, tp);
ptypes[i] = tp;
}
t->ctype = type_function(t->totym(), ptypes, nparams, t->varargs == 1, Type_toCtype(t->next));
if (nparams > 10)
free(ptypes);
}
void visit(TypeDelegate *t)
{
t->ctype = type_delegate(Type_toCtype(t->next));
}
void visit(TypeStruct *t)
{
//printf("TypeStruct::toCtype() '%s'\n", t->sym->toChars());
Type *tm = t->mutableOf();
if (tm->ctype)
{
Symbol *s = tm->ctype->Ttag;
t->ctype = type_alloc(TYstruct);
t->ctype->Ttag = (Classsym *)s; // structure tag name
t->ctype->Tcount++;
// Add modifiers
switch (t->mod)
{
case 0:
assert(0);
break;
case MODconst:
case MODwild:
case MODwildconst:
t->ctype->Tty |= mTYconst;
break;
case MODshared:
t->ctype->Tty |= mTYshared;
break;
case MODshared | MODconst:
case MODshared | MODwild:
case MODshared | MODwildconst:
t->ctype->Tty |= mTYshared | mTYconst;
break;
case MODimmutable:
t->ctype->Tty |= mTYimmutable;
break;
default:
assert(0);
}
}
else
{
StructDeclaration *sym = t->sym;
t->ctype = type_struct_class(sym->toPrettyChars(), sym->alignsize, sym->structsize,
sym->arg1type ? Type_toCtype(sym->arg1type) : NULL,
sym->arg2type ? Type_toCtype(sym->arg2type) : NULL,
sym->isUnionDeclaration() != 0,
false,
sym->isPOD() != 0);
tm->ctype = t->ctype;
/* Add in fields of the struct
* (after setting ctype to avoid infinite recursion)
*/
if (global.params.symdebug)
{
for (size_t i = 0; i < sym->fields.dim; i++)
{
VarDeclaration *v = sym->fields[i];
symbol_struct_addField(t->ctype->Ttag, v->ident->toChars(), Type_toCtype(v->type), v->offset);
}
}
}
//printf("t = %p, Tflags = x%x\n", ctype, ctype->Tflags);
}
void visit(TypeEnum *t)
{
//printf("TypeEnum::toCtype() '%s'\n", t->sym->toChars());
Type *tm = t->mutableOf();
if (tm->ctype && tybasic(tm->ctype->Tty) == TYenum)
{
Symbol *s = tm->ctype->Ttag;
assert(s);
t->ctype = type_alloc(TYenum);
t->ctype->Ttag = (Classsym *)s; // enum tag name
t->ctype->Tcount++;
t->ctype->Tnext = tm->ctype->Tnext;
t->ctype->Tnext->Tcount++;
// Add modifiers
switch (t->mod)
{
case 0:
assert(0);
break;
case MODconst:
case MODwild:
case MODwildconst:
t->ctype->Tty |= mTYconst;
break;
case MODshared:
t->ctype->Tty |= mTYshared;
break;
case MODshared | MODconst:
case MODshared | MODwild:
case MODshared | MODwildconst:
t->ctype->Tty |= mTYshared | mTYconst;
break;
case MODimmutable:
t->ctype->Tty |= mTYimmutable;
break;
default:
assert(0);
}
}
else if (t->sym->memtype->toBasetype()->ty == Tint32)
{
t->ctype = type_enum(t->sym->toPrettyChars(), Type_toCtype(t->sym->memtype));
tm->ctype = t->ctype;
}
else
{
t->ctype = Type_toCtype(t->sym->memtype);
}
//printf("t = %p, Tflags = x%x\n", t, t->Tflags);
}
void visit(TypeTypedef *t)
{
t->ctype = Type_toCtype(t->sym->basetype);
}
void visit(TypeClass *t)
{
//printf("TypeClass::toCtype() %s\n", toChars());
type *tc = type_struct_class(t->sym->toPrettyChars(), t->sym->alignsize, t->sym->structsize,
NULL,
NULL,
false,
true,
true);
t->ctype = type_pointer(tc);
/* Add in fields of the class
* (after setting ctype to avoid infinite recursion)
*/
if (global.params.symdebug)
{
for (size_t i = 0; i < t->sym->fields.dim; i++)
{
VarDeclaration *v = t->sym->fields[i];
symbol_struct_addField(tc->Ttag, v->ident->toChars(), Type_toCtype(v->type), v->offset);
}
}
}
};
type *Type_toCtype(Type *t)
{
if (!t->ctype)
{
ToCtypeVisitor v;
t->accept(&v);
}
return t->ctype;
}