-
Notifications
You must be signed in to change notification settings - Fork 7
/
tpexpr.cxx
454 lines (380 loc) · 11.3 KB
/
tpexpr.cxx
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#include <string.h>
#include "nmtbl.h"
#include "token.h"
#include "trnod.h"
#include "util.h"
tpexpr void_type(tp_void, NULL, "void");
tpexpr any_type(tp_any, NULL, "void*");
tpexpr integer_type(tp_integer, NULL, "integer");
tpexpr longint_type(tp_longint, NULL, "longint");
tpexpr real_type(tp_real, NULL, "real");
tpexpr double_type(tp_real, NULL, "double");
tpexpr char_type(tp_char, NULL, "char");
tpexpr bool_type(tp_bool, NULL, "boolean");
ref_tp pointer_type(&void_type);
string_tp string_type;
varying_string_tp varying_string_type;
text_tp text_type;
//---------------------------------------------------------------------
token *tpexpr::insert_before(token *t)
{
return (name != NULL)
? t->prepend(name)
: t->copy(tpd->f_tkn, tpd->l_tkn);
}
tpexpr* tpexpr::get_typedef() { return this; }
bool tpexpr::is_reference() { return FALSE; }
bool tpexpr::is_array() { return FALSE; }
//---------------------------------------------------------------------
tpexpr* simple_tp::get_typedef() { return alias->get_typedef(); }
bool simple_tp::is_reference() { return alias->is_reference(); }
bool simple_tp::is_array() { return alias->is_array(); }
//---------------------------------------------------------------------
ref_tp::ref_tp(tpexpr* tp, tpd_node* tpd)
: tpexpr(tp_ref, tpd), base_type(tp)
{
if (tp != NULL && tp->name != NULL) {
name = dprintf("%s*", tp->name);
}
}
bool ref_tp::is_reference() { return TRUE; }
//---------------------------------------------------------------------
fwd_ref_tp::fwd_ref_tp(token* t)
{
tag = tp_fwd_ref;
ident = t;
name = dprintf("struct %s*", t->out_text);
}
tpexpr* fwd_ref_tp::get_typedef()
{
if (tag == tp_fwd_ref) {
symbol *sym = b_ring::search_cur(ident);
if (sym != NULL) {
base_type = sym->type;
tag = tp_ref;
}
}
return this;
}
//---------------------------------------------------------------------
void enum_tp::set_bounds(symbol* var)
{
max = dprintf("last_%s", var->out_name->text);
}
void enum_tp::set_enumeration_name(tpexpr* type)
{
symbol* sym = last;
do {
sym->type = type;
if (sym == first) break;
sym = sym->next;
} while (TRUE);
}
//---------------------------------------------------------------------
void range_tp::set_bounds(symbol* var)
{
max = dprintf("max_%s", var->out_name->text);
min = dprintf("min_%s", var->out_name->text);
}
//---------------------------------------------------------------------
array_tp::array_tp(tpexpr* tp, tpd_node* tpd)
: tpexpr(tp_array, tpd), elem_type(tp)
{
flags |= tp->get_typedef()->flags & tp_need_init;
low = high = NULL;
low_expr = high_expr = NULL;
base = -1;
}
bool array_tp::is_array() { return TRUE; }
void array_tp::insert_bounds_definition(symbol* array, token* block, int n)
{
if (elem_type->tag == tp_dynarray || n > 0) n += 1;
block->prepend(dprintf("const int %s = %s.%s%.0d;\n",
low, array->out_name->text, "low", n));
block->prepend(dprintf("const int %s = %s.%s%.0d;\n",
high, array->out_name->text, "high", n));
if (elem_type->tag == tp_dynarray) {
((array_tp*)elem_type->get_typedef())->
insert_bounds_definition(array, block, n);
}
}
void array_tp::set_dim(char *low, char *high,
expr_node *low_expr, expr_node *high_expr)
{
this->low = low;
this->high = high;
this->low_expr = low_expr;
this->high_expr = high_expr;
if (low != NULL && strcmp(low, "0") == 0) {
base = 0;
} else if (low_expr != NULL && low_expr->is_const_literal()) {
base = low_expr->value;
}
}
void array_tp::set_conformant_dim(symbol* low_sym, symbol* high_sym)
{
low_var = low_sym;
high_var = high_sym;
low = low_sym->out_name->text;
high = high_sym->out_name->text;
low_expr = high_expr = NULL;
tag = tp_dynarray;
}
void array_tp::insert_dimensions(expr_node* e, array_tp* conf_arr, int n)
{
token *t = e->f_tkn;
if (low == NULL) {
assert(low_expr != NULL);
t->copy(low_expr->f_tkn, low_expr->l_tkn);
} else {
t->prepend(low);
}
t->prepend(", ");
if (!e->is_parameter() && (base == 0 || base == 1)) {
t->prepend(dprintf("items(%.*s", n, "****************"));
t->copy(e->f_tkn, e->l_tkn);
t->prepend(base == 0 ? ")-1" : ")");
} else {
if (high == NULL) {
assert(high_expr != NULL);
t->copy(high_expr->f_tkn, high_expr->l_tkn);
} else {
t->prepend(high);
}
}
t->prepend(", ");
if (conf_arr && conf_arr->elem_type->tag == tp_dynarray) {
if (elem_type->tag != tp_array) {
warning(e->f_tkn, "array is incompatible with conformant scheme");
} else {
((array_tp*)elem_type->get_typedef())->
insert_dimensions(e, (array_tp*)conf_arr->elem_type, n + 1);
}
if (tag != tp_dynarray) {
e->f_tkn->prepend("*");
}
}
}
void array_tp::insert_length(token* before)
{
if (base == 0 || no_index_decrement) {
if (high != NULL) {
before->prepend(dprintf("(%s+1)", high));
} else {
assert(high_expr != NULL);
if (high_expr->is_const_literal()) {
before->prepend(dprintf("%d", high_expr->value + 1));
} else {
before->prepend("(");
before->copy(high_expr->f_tkn, high_expr->l_tkn);
before->prepend("+1)");
}
}
} else if (base == 1) {
if (high != NULL) {
before->prepend(high);
} else {
assert(high_expr != NULL);
before->copy(high_expr->f_tkn, high_expr->l_tkn);
}
} else {
if (high != NULL && low != NULL) {
before->prepend(dprintf("(%s-%s+1)", high, low));
} else {
assert(high_expr != NULL && low_expr != NULL);
before->prepend("((");
before->copy(high_expr->f_tkn, high_expr->l_tkn);
before->prepend(")-(");
before->copy(low_expr->f_tkn, low_expr->l_tkn);
before->prepend(")+1)");
}
}
if (elem_type->tag == tp_dynarray) {
((array_tp*)elem_type)->insert_bound_params(before);
}
}
void array_tp::insert_bound_params(token* before)
{
assert(low != NULL && high != NULL);
before->prepend(dprintf("const integer %s, const integer %s, ",
low, high));
if (elem_type->tag == tp_dynarray) {
((array_tp*)elem_type)->insert_bound_params(before);
}
}
void array_tp::add_proc_param(proc_tp* proc)
{
proc->add_extra_param(low_var);
proc->add_extra_param(high_var);
if (elem_type->tag == tp_dynarray) {
((array_tp*)elem_type)->add_proc_param(proc);
}
}
//---------------------------------------------------------------------
text_tp::text_tp() : file_tp(&char_type) {
tag = tp_text;
name = "text";
}
//---------------------------------------------------------------------
void record_tp::calc_flags()
{
for (symbol *sym = syms; sym != NULL; sym = sym->next) {
flags |= sym->type->get_typedef()->flags;
}
}
//---------------------------------------------------------------------
object_tp::object_tp(tpd_node *tpd, object_tp* super) : record_tp(tpd)
{
tag = tp_object;
class_name = NULL;
inherite = super;
}
//---------------------------------------------------------------------
string_tp::string_tp() : array_tp(&char_type)
{
tag = tp_string;
base = 1;
low = "1";
name = "char*";
}
void string_tp::insert_dimensions(expr_node* e, array_tp* conf_arr, int n)
{
e->f_tkn->prepend("array(");
e->l_tkn->append(")");
}
//---------------------------------------------------------------------
varying_string_tp::varying_string_tp() : array_tp(&char_type)
{
tag = tp_varying_string;
base = 1;
low = "1";
name = "string";
}
//---------------------------------------------------------------------
proc_tp::proc_tp(tpexpr* rtype, tpd_node* tpd)
: tpexpr(tp_proc, tpd), b_ring(b_ring::proc)
{
params = last = NULL;
extra_params = NULL;
callers = NULL;
res_type = rtype;
forward = NULL;
proc_name = NULL;
define_list = NULL;
last_temp = temp_list = NULL;
n_temp = 0;
n_subproc = 0;
make_all_constants_global = FALSE;
is_extern_c = FALSE;
is_constructor = FALSE;
is_destructor = FALSE;
}
void proc_tp::add_param(symbol *var)
{
if (last == NULL) {
params = last = new param_spec(var);
} else {
last = last->next = new param_spec(var);
}
}
void proc_tp::add_extra_param(symbol *var)
{
param_spec **pp, *p;
if (var->ring == this) return;
if (var->ring->scope == b_ring::proc && var->type
&& !var->type->is_scalar() && var->type->get_typedef() == var->type)
{
((proc_tp*)var->ring)->make_all_constants_global = TRUE;
}
if (language_c && var->type->tag == tp_dynarray) {
((array_tp*)var->type)->add_proc_param(this);
}
for (pp = &extra_params; (p = *pp) != NULL; pp = &p->next) {
if (p->var == var) return;
}
*pp = new param_spec(var);
var->flags |= symbol::f_exported;
for (caller_spec *f = callers; f != NULL; f = f->next) {
f->caller->add_extra_param(var);
}
}
void proc_tp::add_caller(proc_tp* f)
{
caller_spec *cp, **cpp;
if (f == this || f == NULL || proc_name == NULL) return;
for (cpp = &callers; (cp = *cpp) != NULL; cpp = &cp->next) {
if (cp->caller == f) return;
}
*cpp = new caller_spec(f);
if (use_call_graph && call_graph_file != NULL && f->proc_name != NULL) {
fprintf(call_graph_file, "%s -> %s\n", f->proc_name, proc_name);
}
for (param_spec *p = extra_params; p != NULL; p = p->next) {
f->add_extra_param(p->var);
}
}
void proc_tp::declare_conformant_array_bounds(token* section)
{
param_spec* prm;
tpexpr* prev_type = NULL;
for (prm = params; prm != NULL; prm = prm->next) {
if (prm->var->type->tag == tp_dynarray) {
if (prm->var->type != prev_type) {
((array_tp*)prm->var->type)->insert_bounds_definition
(prm->var, section);
prev_type = prm->var->type;
}
if ((prm->var->flags & symbol::f_val_param)
&& (copy_array || (prm->var->flags & symbol::f_lvalue)))
{
section->prepend(dprintf("copy_conformant_array(%s);\n",
prm->var->out_name->text));
}
}
}
for (prm = extra_params; prm != NULL; prm = prm->next) {
if (prm->var->type->tag == tp_dynarray) {
if (prm->var->type != prev_type) {
((array_tp*)prm->var->type)->insert_bounds_definition
(prm->var, section);
prev_type = prm->var->type;
}
if ((prm->var->flags & symbol::f_val_param)
&& (copy_array || (prm->var->flags & symbol::f_lvalue)))
{
section->prepend(dprintf("copy_conformant_array(%s);\n",
prm->var->out_name->text));
}
}
}
}
void proc_tp::add_define(symbol* s)
{
define_list = new define_spec(s, define_list);
}
void proc_tp::undefine(token* t)
{
if (define_list != NULL) {
t = t->append("\n\n");
}
for (define_spec* def = define_list; def != NULL; def = def->next) {
t = t->append(dprintf("#undef %s\n", def->sym->out_name->text));
}
}
char* proc_tp::add_temp(tpexpr* type)
{
if (last_temp == NULL) {
temp_list = last_temp = new temp_spec(type);
} else {
last_temp = last_temp->next = new temp_spec(type);
}
return dprintf("temp%d", ++n_temp);
}
void proc_tp::insert_temporaries(token* t)
{
int n = 0;
for (temp_spec* ts = temp_list; ts != NULL; ts = ts->next) {
assert(ts->type->name != NULL);
t->prepend(dprintf("%s temp%d;\n", ts->type->name, ++n));
}
}