-
Notifications
You must be signed in to change notification settings - Fork 3
/
simple-decl.c
614 lines (512 loc) · 13.5 KB
/
simple-decl.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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#include "simple-decl.h"
#include "config.h"
#include <assert.h>
#include <err.h>
#include <stdio.h>
#include <string.h>
#include "libks/arena-buffer.h"
#include "libks/arena.h"
#include "libks/buffer.h"
#include "libks/vector.h"
#include "clang.h"
#include "lexer.h"
#include "token.h"
#include "trace-types.h"
#include "trace.h"
#define simple_trace(sd, fmt, ...) \
trace(TRACE_SIMPLE, (sd)->op, (fmt), __VA_ARGS__)
struct token_range {
struct token *tr_beg;
struct token *tr_end;
};
#define TOKEN_RANGE_FOREACH(var, tr, tvar) \
for ((var) = (tr)->tr_beg, (tvar) = token_next((var)); \
(var) != NULL; \
(var) = (var) == (tr)->tr_end ? NULL : (tvar), \
(tvar) = (var) ? token_next((var)) : NULL)
static const char *token_range_str(const struct token_range *,
struct arena_scope *);
/*
* Represents a single declaration from the source code.
*/
struct decl {
/* Type slots to be placed after this declaration. */
VECTOR(unsigned int) slots;
/* Tokens covering the whole declaration, including the semicolon. */
struct token_range tr;
/* Number of variables of the declaration that cannot be moved. */
int nkeep;
};
static void decl_free(struct decl *);
struct decl_var {
struct token_range dv_ident;
struct token *dv_sort;
struct token *dv_delim;
};
static int decl_var_cmp(const struct decl_var *, const struct decl_var *);
static int decl_var_is_empty(const struct decl_var *);
struct decl_type {
struct token_range dt_tr;
VECTOR(struct decl_type_vars) dt_slots;
const char *dt_str;
};
static struct decl_type_vars *decl_type_slot(struct decl_type *,
unsigned int);
/*
* Variables associated with a declaration type.
*/
struct decl_type_vars {
VECTOR(struct decl_var) vars;
struct {
/* Associated with kept declaration. */
struct token *kept;
/* Associated with declaration to be removed (i.e. empty). */
struct token *fallback;
} semi;
};
struct simple_decl {
/*
* Duplicate type declarations ending up empty due to variables of the
* same type being grouped into one declaration.
*/
VECTOR(struct decl) empty_decls;
/* All seen type declarations. */
VECTOR(struct decl_type) types;
struct arena_scope *eternal_scope;
/* Current type declaration. */
struct decl_type *dt;
struct decl_var dv;
struct lexer *lx;
const struct options *op;
};
static struct decl_type *simple_decl_type_create(struct simple_decl *,
const char *, const struct token_range *);
static struct decl_type *simple_decl_type_find(struct simple_decl *,
const char *);
static struct token *simple_decl_move_vars(struct simple_decl *,
struct decl_type *, struct decl_type_vars *, struct token *);
static struct decl_var *simple_decl_var_init(struct simple_decl *);
static struct decl_var *simple_decl_var_end(struct simple_decl *,
struct token *);
static void associate_semi(struct decl *, struct decl_type *,
struct token *);
static int classify(const struct token_range *, unsigned int *);
struct simple_decl *
simple_decl_enter(struct lexer *lx, struct arena_scope *eternal_scope,
const struct options *op)
{
struct simple_decl *sd;
sd = arena_calloc(eternal_scope, 1, sizeof(*sd));
if (VECTOR_INIT(sd->empty_decls))
err(1, NULL);
if (VECTOR_INIT(sd->types))
err(1, NULL);
sd->eternal_scope = eternal_scope;
sd->lx = lx;
sd->op = op;
return sd;
}
void
simple_decl_leave(struct simple_decl *sd)
{
struct lexer *lx = sd->lx;
struct token *tk, *tmp;
size_t i;
for (i = 0; i < VECTOR_LENGTH(sd->types); i++) {
struct decl_type *dt = &sd->types[i];
struct token *after;
unsigned int slot;
for (slot = 0; slot < VECTOR_LENGTH(dt->dt_slots); slot++) {
struct decl_type_vars *ds = &dt->dt_slots[slot];
struct token *semi;
if (VECTOR_LENGTH(ds->vars) == 0)
continue;
/* Sort the variables in alphabetical order. */
VECTOR_SORT(ds->vars, decl_var_cmp);
semi = ds->semi.kept != NULL ? ds->semi.kept :
ds->semi.fallback;
assert(semi != NULL && semi->tk_type == TOKEN_SEMI);
after = semi;
after = simple_decl_move_vars(sd, dt, ds, after);
/* Move line break(s) to the new semicolon. */
token_move_suffixes_if(semi, after, TOKEN_SPACE);
}
}
/* Remove by now empty declarations. */
for (i = 0; i < VECTOR_LENGTH(sd->empty_decls); i++) {
struct decl *dc = &sd->empty_decls[i];
TOKEN_RANGE_FOREACH(tk, &dc->tr, tmp)
lexer_remove(lx, tk);
}
}
void
simple_decl_free(struct simple_decl *sd)
{
if (sd == NULL)
return;
while (!VECTOR_EMPTY(sd->empty_decls)) {
struct decl *dc;
dc = VECTOR_POP(sd->empty_decls);
decl_free(dc);
}
VECTOR_FREE(sd->empty_decls);
while (!VECTOR_EMPTY(sd->types)) {
struct decl_type *dt;
dt = VECTOR_POP(sd->types);
while (!VECTOR_EMPTY(dt->dt_slots)) {
struct decl_type_vars *ds = VECTOR_POP(dt->dt_slots);
VECTOR_FREE(ds->vars);
}
VECTOR_FREE(dt->dt_slots);
}
VECTOR_FREE(sd->types);
}
static int
is_type_valid(struct token *beg, struct token *end)
{
struct token_range tr = {
.tr_beg = beg,
.tr_end = end,
};
struct token *tk, *tmp;
/* Ignore anonymous struct and union. */
if (beg == end &&
(token_is_decl(beg, TOKEN_STRUCT) ||
token_is_decl(beg, TOKEN_UNION)))
return 0;
TOKEN_RANGE_FOREACH(tk, &tr, tmp) {
if (!token_is_moveable(tk))
return 0;
/* Ignore type declarations making use of cpp macros. */
switch (tk->tk_type) {
case TOKEN_LPAREN:
case TOKEN_LBRACE:
return 0;
}
}
return 1;
}
void
simple_decl_type(struct simple_decl *sd, struct token *beg, struct token *end)
{
struct token_range tr = {
.tr_beg = beg,
.tr_end = end,
};
struct decl *dc;
struct decl_var *dv;
const char *type;
if (!is_type_valid(beg, end))
return;
type = token_range_str(&tr, sd->eternal_scope);
sd->dt = simple_decl_type_create(sd, type, &tr);
dv = simple_decl_var_init(sd);
/* Pointer(s) are part of the variable. */
while (end->tk_type == TOKEN_STAR)
end = token_prev(end);
dv->dv_ident.tr_beg = token_next(end);
dc = VECTOR_CALLOC(sd->empty_decls);
if (dc == NULL)
err(1, NULL);
if (VECTOR_INIT(dc->slots))
err(1, NULL);
dc->tr = tr;
}
void
simple_decl_semi(struct simple_decl *sd, struct token *semi)
{
struct decl *dc = VECTOR_LAST(sd->empty_decls);
if (decl_var_is_empty(&sd->dv))
return;
simple_decl_var_end(sd, semi);
/*
* If the type declaration is empty, ensure deletion of everything
* including the semicolon.
*/
if (dc->nkeep == 0) {
dc->tr.tr_end = semi;
} else {
decl_free(dc);
VECTOR_POP(sd->empty_decls);
}
simple_decl_var_init(sd);
sd->dt = NULL;
}
void
simple_decl_comma(struct simple_decl *sd, struct token *comma)
{
struct decl_var *dv;
struct token *delim = NULL;
if (decl_var_is_empty(&sd->dv))
return;
dv = simple_decl_var_end(sd, comma);
if (dv != NULL && dv->dv_delim == NULL) {
dv->dv_delim = comma;
} else if (token_is_moveable(comma)) {
/*
* If the next variable ends up being moved, the preceding
* comma must be removed.
*/
delim = comma;
}
/* Another variable after the comma is expected. */
dv = simple_decl_var_init(sd);
dv->dv_ident.tr_beg = token_next(comma);
dv->dv_delim = delim;
}
static const char *
token_range_str(const struct token_range *tr, struct arena_scope *s)
{
struct buffer *bf;
struct token *tk, *tmp;
int ntokens = 0;
bf = arena_buffer_alloc(s, 64);
TOKEN_RANGE_FOREACH(tk, tr, tmp) {
if (tk->tk_type == TOKEN_STAR)
continue;
if (ntokens++ > 0)
buffer_putc(bf, ' ');
buffer_puts(bf, tk->tk_str, tk->tk_len);
}
return buffer_str(bf);
}
static void
decl_free(struct decl *dc)
{
if (dc == NULL)
return;
VECTOR_FREE(dc->slots);
}
static int
decl_var_cmp(const struct decl_var *a, const struct decl_var *b)
{
return token_strcmp(a->dv_sort, b->dv_sort);
}
static int
decl_var_is_empty(const struct decl_var *dv)
{
return dv->dv_ident.tr_beg == NULL;
}
static struct decl_type_vars *
decl_type_slot(struct decl_type *dt, unsigned int n)
{
while (VECTOR_LENGTH(dt->dt_slots) <= n) {
struct decl_type_vars *ds;
ds = VECTOR_CALLOC(dt->dt_slots);
if (ds == NULL)
err(1, NULL);
if (VECTOR_INIT(ds->vars))
err(1, NULL);
}
return &dt->dt_slots[n];
}
/*
* Create or find the given type.
*/
static struct decl_type *
simple_decl_type_create(struct simple_decl *sd, const char *type,
const struct token_range *tr)
{
struct decl_type *dt;
struct token *end = NULL;
struct token *tk, *tmp;
dt = simple_decl_type_find(sd, type);
if (dt != NULL)
return dt;
dt = VECTOR_CALLOC(sd->types);
if (dt == NULL)
err(1, NULL);
dt->dt_tr = *tr;
/* Pointer(s) are not part of the type. */
TOKEN_RANGE_FOREACH(tk, tr, tmp) {
if (tk->tk_type != TOKEN_STAR)
end = tk;
}
assert(end != NULL);
dt->dt_tr.tr_end = end;
if (VECTOR_INIT(dt->dt_slots))
err(1, NULL);
dt->dt_str = type;
simple_trace(sd, "new type \"%s\"", dt->dt_str);
return dt;
}
static struct decl_type *
simple_decl_type_find(struct simple_decl *sd, const char *type)
{
size_t i;
for (i = 0; i < VECTOR_LENGTH(sd->types); i++) {
struct decl_type *dt = &sd->types[i];
if (strcmp(dt->dt_str, type) == 0)
return dt;
}
return NULL;
}
static struct token *
simple_decl_move_vars(struct simple_decl *sd, struct decl_type *dt,
struct decl_type_vars *ds, struct token *after)
{
struct lexer *lx = sd->lx;
struct token *tk, *tmp;
size_t i;
/* Create new type declaration. */
TOKEN_RANGE_FOREACH(tk, &dt->dt_tr, tmp)
after = lexer_copy_after(lx, after, tk);
/* Move variables to the new type declaration. */
for (i = 0; i < VECTOR_LENGTH(ds->vars); i++) {
struct decl_var *dv = &ds->vars[i];
struct token *ident;
if (dv->dv_delim != NULL)
lexer_remove(lx, dv->dv_delim);
if (i > 0) {
after = lexer_insert_after(lx, after,
clang_keyword_token(TOKEN_COMMA));
}
TOKEN_RANGE_FOREACH(ident, &dv->dv_ident, tmp)
after = lexer_move_after(lx, after, ident);
}
return lexer_insert_after(lx, after, clang_keyword_token(TOKEN_SEMI));
}
static struct decl_var *
simple_decl_var_init(struct simple_decl *sd)
{
memset(&sd->dv, 0, sizeof(sd->dv));
return &sd->dv;
}
static struct decl_var *
simple_decl_var_end(struct simple_decl *sd, struct token *end)
{
struct decl *dc = VECTOR_LAST(sd->empty_decls);
struct decl_var *dv = &sd->dv;
struct decl_var *dst;
struct decl_type_vars *ds;
struct token *sort, *tmp;
unsigned int slot;
int keep = 0;
assert(dv->dv_ident.tr_end == NULL);
/* The delimiter is not part of the identifier. */
dv->dv_ident.tr_end = token_prev(end);
if (!classify(&dv->dv_ident, &slot) || !token_is_moveable(end)) {
dc->nkeep++;
keep = 1;
}
/*
* Allocate the slot even if the variable is kept since there could
* be other variables of the same type that we want to place after this
* kept declaration.
*/
ds = decl_type_slot(sd->dt, slot);
/*
* If the declaration is kept due to a non-moveable variable associated
* with the current slot, place all other moveable variables associated
* with the same slot after this declaration. We do this even if the
* slot already have a associated semi, causing variables to be placed
* after the last kept declaration.
*/
if (keep) {
unsigned int *newslot;
newslot = VECTOR_ALLOC(dc->slots);
if (newslot == NULL)
err(1, NULL);
*newslot = slot;
}
if (end->tk_type == TOKEN_SEMI)
associate_semi(dc, sd->dt, end);
if (keep)
return NULL;
/* Find the identifier token used while sorting. */
TOKEN_RANGE_FOREACH(sort, &dv->dv_ident, tmp) {
if (sort->tk_type == TOKEN_IDENT)
break;
}
dv->dv_sort = sort;
dst = VECTOR_CALLOC(ds->vars);
if (dst == NULL)
err(1, NULL);
*dst = *dv;
simple_trace(sd, "type \"%s\", slot %u, ident %s",
sd->dt->dt_str, slot, lexer_serialize(sd->lx, dv->dv_sort));
return dst;
}
static void
associate_semi(struct decl *dc, struct decl_type *dt, struct token *semi)
{
struct decl_type_vars *slots = dt->dt_slots;
size_t i, nslots;
while (!VECTOR_EMPTY(dc->slots)) {
unsigned int *slot;
slot = VECTOR_POP(dc->slots);
slots[*slot].semi.kept = semi;
}
/*
* New slot(s) could have been added while handling the current
* declaration. Evaluate if any previously added variables of the same
* type are better placed after the current declaration.
*/
nslots = VECTOR_LENGTH(slots);
for (i = 0; i < nslots; i++) {
struct token *newsemi = NULL;
size_t j;
if (slots[i].semi.kept != NULL)
continue;
for (j = i + 1; j < nslots; j++) {
newsemi = slots[j].semi.kept;
if (newsemi != NULL)
break;
}
if (newsemi != NULL)
slots[i].semi.kept = newsemi;
else if (slots[i].semi.fallback == NULL)
slots[i].semi.fallback = semi;
}
}
static int
classify(const struct token_range *tr, unsigned int *slot)
{
struct token *tk, *tmp;
unsigned int nident = 0;
unsigned int nstars = 0;
unsigned int nsquares = 0;
int error = 0;
int before_assign = 1;
TOKEN_RANGE_FOREACH(tk, tr, tmp) {
switch (tk->tk_type) {
case TOKEN_STAR:
nstars++;
break;
case TOKEN_IDENT:
nident++;
break;
case TOKEN_LSQUARE:
nsquares++;
error = 1;
break;
case TOKEN_EQUAL:
/*
* Token(s) part of the assignment must not influence
* the classification.
*/
before_assign = 0;
error = 1;
break;
default:
error = 1;
}
if (!token_is_moveable(tk))
error = 1;
if (!before_assign)
break;
}
if (nsquares > 0) {
/*
* Although array variables are kept, there might be an
* opportunity to place other pointers of the same type after
* this declaration. The slot choice is somewhat arbitrary and
* assumes that usage of more than three level pointers is
* uncommon.
*/
*slot = 4;
} else {
*slot = nstars;
}
return !error && nident > 0;
}