-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexpr.c
1351 lines (1153 loc) · 34 KB
/
expr.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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "expr.h"
#include "config.h"
#include <assert.h>
#include <err.h>
#include <string.h>
#include "libks/arena-buffer.h"
#include "libks/arena.h"
#include "libks/compiler.h"
#include "libks/consistency.h"
#include "libks/vector.h"
#include "clang.h"
#include "doc.h"
#include "lexer.h"
#include "ruler.h"
#include "simple-expr-printf.h"
#include "simple.h"
#include "style.h"
#include "token.h"
enum expr_mode {
EXPR_MODE_EXEC,
EXPR_MODE_PEEK,
};
/*
* Precedence, from lowest to highest.
*/
enum expr_pc {
PC0, /* {} literal */
PC1, /* , */
PC2, /* = += -= *= /= %= <<= >>= &= ^= |= */
PC3, /* ?: */
PC4, /* || */
PC5, /* && */
PC6, /* | */
PC7, /* ^ */
PC8, /* & */
PC9, /* == != */
PC10, /* < <= > >= */
PC11, /* << >> */
PC12, /* + - */
PC13, /* * / % */
PC14, /* ! ~ ++ -- - (cast) * & sizeof */
PC15, /* () [] -> . */
};
#define PCUNARY 0x80000000u
#define PC(pc) ((pc) & ~PCUNARY)
#define FOR_EXPR_TYPES(OP) \
OP(EXPR_UNARY) \
OP(EXPR_BINARY) \
OP(EXPR_TERNARY) \
OP(EXPR_PREFIX) \
OP(EXPR_POSTFIX) \
OP(EXPR_PARENS) \
OP(EXPR_SQUARES) \
OP(EXPR_FIELD) \
OP(EXPR_CALL) \
OP(EXPR_ARG) \
OP(EXPR_CAST) \
OP(EXPR_SIZEOF) \
OP(EXPR_CONCAT) \
OP(EXPR_LITERAL) \
OP(EXPR_RECOVER)
enum expr_type {
#define OP(type) type,
FOR_EXPR_TYPES(OP)
#undef OP
};
struct expr {
enum expr_type ex_type;
struct token *ex_tk;
struct expr *ex_lhs;
struct expr *ex_rhs;
struct token *ex_tokens[2];
union {
struct expr *ex_ternary;
struct doc *ex_dc;
VECTOR(struct expr *) ex_concat;
struct {
struct token *lparen;
struct token *rparen;
} ex_sizeof;
};
};
struct expr_state;
struct expr_rule {
enum expr_pc er_pc;
int er_rassoc;
int er_type;
struct expr *(*er_func)(struct expr_state *, struct expr *);
};
struct expr_state {
struct expr_exec_arg es_ea;
#define es_st es_ea.st
#define es_op es_ea.op
#define es_lx es_ea.lx
#define es_dc es_ea.dc
#define es_flags es_ea.flags
struct {
struct arena_scope *eternal_scope;
struct arena *scratch;
struct arena_scope *scratch_scope;
struct arena *buffer;
} es_arena;
const struct expr_rule *es_er;
struct token *es_tk;
enum expr_mode es_mode;
unsigned int es_depth;
unsigned int es_nassign; /* # nested binary assignments */
unsigned int es_ncalls; /* # nested calls */
unsigned int es_noparens; /* parens indent disabled */
unsigned int es_col; /* ruler column */
};
static struct expr *expr_exec1(struct expr_state *, enum expr_pc);
static struct expr *expr_exec_recover(struct expr_state *);
static struct expr *expr_exec_recover_cast(struct expr_state *);
static struct expr *expr_exec_binary(struct expr_state *, struct expr *);
static struct expr *expr_exec_concat(struct expr_state *, struct expr *);
static struct expr *expr_exec_field(struct expr_state *, struct expr *);
static struct expr *expr_exec_literal(struct expr_state *, struct expr *);
static struct expr *expr_exec_parens(struct expr_state *, struct expr *);
static struct expr *expr_exec_prepost(struct expr_state *, struct expr *);
static struct expr *expr_exec_sizeof(struct expr_state *, struct expr *);
static struct expr *expr_exec_squares(struct expr_state *, struct expr *);
static struct expr *expr_exec_ternary(struct expr_state *, struct expr *);
static struct expr *expr_exec_unary(struct expr_state *, struct expr *);
static int expr_exec_peek(struct expr_state *, struct token **);
static struct expr *expr_alloc(enum expr_type, const struct expr_state *);
static struct doc *expr_doc(struct expr *, struct expr_state *,
struct doc *);
static struct doc *expr_doc_unary(struct expr *, struct expr_state *,
struct doc *);
static struct doc *expr_doc_binary(struct expr *, struct expr_state *,
struct doc *);
static struct doc *expr_doc_parens(struct expr *, struct expr_state *,
struct doc *);
static struct doc *expr_doc_field(struct expr *, struct expr_state *,
struct doc *);
static struct doc *expr_doc_call(struct expr *, struct expr_state *,
struct doc *);
static struct doc *expr_doc_arg(struct expr *, struct expr_state *,
struct doc *);
static struct doc *expr_doc_sizeof(struct expr *, struct expr_state *,
struct doc *);
static struct doc *expr_doc_concat(struct expr *, struct expr_state *,
struct doc *);
static struct doc *expr_doc_ternary(struct expr *, struct expr_state *,
struct doc *);
static struct doc *expr_doc_recover(struct expr *, struct expr_state *,
struct doc *);
#define expr_doc_token(a, b, c) \
expr_doc_token_impl((a), (b), (c), __func__, __LINE__)
static struct doc *expr_doc_token_impl(struct expr_state *,
struct token *, struct doc *, const char *, int);
static void expr_free_concat(void *);
#define expr_doc_align(a, b, c, d) \
expr_doc_align_impl((a), (b), (c), (d), __func__, __LINE__)
static struct doc *expr_doc_align_impl(struct expr *,
struct expr_state *, struct doc *, unsigned int, const char *, int);
#define expr_doc_align_disable(a, b, c, d) \
expr_doc_align_disable_impl((a), (b), (c), (d), __func__, __LINE__)
static struct doc *expr_doc_align_disable_impl(struct expr *,
struct expr_state *, struct doc *, unsigned int, const char *, int);
static void expr_doc_align_init(struct expr_state *,
struct doc_minimize *, size_t);
static struct doc *expr_doc_indent_parens(const struct expr_state *,
struct doc *);
static int expr_doc_has_spaces(const struct expr *);
static unsigned int expr_doc_width(struct expr_state *,
const struct doc *);
#define expr_doc_soft(a, b, c, d) \
expr_doc_soft_impl((a), (b), (c), (d), __func__, __LINE__)
static struct doc *expr_doc_soft_impl(struct expr *, struct expr_state *,
struct doc *, int, const char *, int);
static void expr_state_init(struct expr_state *,
const struct expr_exec_arg *, enum expr_mode, struct arena_scope *);
static const struct expr_rule *expr_find_rule(const struct token *, int);
static void token_move_next_line(struct token *);
static void token_move_prev_line(struct token *);
static const char *expr_type_str(enum expr_type);
static const struct expr_rule rules[] = {
{ PC0 | PCUNARY, 0, TOKEN_IDENT, expr_exec_literal },
{ PC0 | PCUNARY, 0, TOKEN_LITERAL, expr_exec_literal },
{ PC0 | PCUNARY, 0, TOKEN_STRING, expr_exec_literal },
{ PC1, 0, TOKEN_COMMA, expr_exec_binary },
{ PC1, 0, TOKEN_ELLIPSIS, expr_exec_binary },
{ PC2, 1, TOKEN_EQUAL, expr_exec_binary },
{ PC2, 1, TOKEN_PLUSEQUAL, expr_exec_binary },
{ PC2, 1, TOKEN_MINUSEQUAL, expr_exec_binary },
{ PC2, 1, TOKEN_STAREQUAL, expr_exec_binary },
{ PC2, 1, TOKEN_SLASHEQUAL, expr_exec_binary },
{ PC2, 1, TOKEN_PERCENTEQUAL, expr_exec_binary },
{ PC2, 1, TOKEN_LESSLESSEQUAL, expr_exec_binary },
{ PC2, 1, TOKEN_GREATERGREATEREQUAL, expr_exec_binary },
{ PC2, 1, TOKEN_AMPEQUAL, expr_exec_binary },
{ PC2, 1, TOKEN_CARETEQUAL, expr_exec_binary },
{ PC2, 1, TOKEN_PIPEEQUAL, expr_exec_binary },
{ PC3, 1, TOKEN_QUESTION, expr_exec_ternary },
{ PC4, 0, TOKEN_PIPEPIPE, expr_exec_binary },
{ PC5, 0, TOKEN_AMPAMP, expr_exec_binary },
{ PC6, 0, TOKEN_PIPE, expr_exec_binary },
{ PC7, 0, TOKEN_CARET, expr_exec_binary },
{ PC8, 0, TOKEN_AMP, expr_exec_binary },
{ PC9, 0, TOKEN_EQUALEQUAL, expr_exec_binary },
{ PC9, 0, TOKEN_EXCLAIMEQUAL, expr_exec_binary },
{ PC10, 0, TOKEN_LESS, expr_exec_binary },
{ PC10, 0, TOKEN_LESSEQUAL, expr_exec_binary },
{ PC10, 0, TOKEN_GREATER, expr_exec_binary },
{ PC10, 0, TOKEN_GREATEREQUAL, expr_exec_binary },
{ PC11, 0, TOKEN_LESSLESS, expr_exec_binary },
{ PC11, 0, TOKEN_GREATERGREATER, expr_exec_binary },
{ PC12, 0, TOKEN_IDENT, expr_exec_concat },
{ PC12, 0, TOKEN_LITERAL, expr_exec_concat },
{ PC12, 0, TOKEN_STRING, expr_exec_concat },
{ PC12, 0, TOKEN_PLUS, expr_exec_binary },
{ PC12, 0, TOKEN_MINUS, expr_exec_binary },
{ PC13, 0, TOKEN_STAR, expr_exec_binary },
{ PC13, 0, TOKEN_SLASH, expr_exec_binary },
{ PC13, 0, TOKEN_PERCENT, expr_exec_binary },
{ PC14 | PCUNARY, 1, TOKEN_EXCLAIM, expr_exec_unary },
{ PC14 | PCUNARY, 1, TOKEN_TILDE, expr_exec_unary },
{ PC14, 1, TOKEN_PLUSPLUS, expr_exec_prepost },
{ PC14 | PCUNARY, 1, TOKEN_PLUSPLUS, expr_exec_prepost },
{ PC14 | PCUNARY, 1, TOKEN_PLUS, expr_exec_unary },
{ PC14, 1, TOKEN_MINUSMINUS, expr_exec_prepost },
{ PC14 | PCUNARY, 1, TOKEN_MINUSMINUS, expr_exec_prepost },
{ PC14 | PCUNARY, 1, TOKEN_MINUS, expr_exec_unary },
{ PC14 | PCUNARY, 1, TOKEN_STAR, expr_exec_unary },
{ PC14 | PCUNARY, 1, TOKEN_AMP, expr_exec_unary },
{ PC14 | PCUNARY, 1, TOKEN_SIZEOF, expr_exec_sizeof },
{ PC15, 0, TOKEN_LPAREN, expr_exec_parens },
{ PC15 | PCUNARY, 0, TOKEN_LPAREN, expr_exec_parens },
{ PC15, 0, TOKEN_LSQUARE, expr_exec_squares },
{ PC15, 0, TOKEN_ARROW, expr_exec_field },
{ PC15, 0, TOKEN_PERIOD, expr_exec_field },
};
/* Table for constant time expr rules lookup. */
static const struct expr_rule *table_rules[TOKEN_NONE + 1][2];
/*
* Weights for emitted softline(s) through expr_doc_soft(). Several softline(s)
* can be emitted per expression in which the one with highest weight is
* favored, removing the others.
*/
static const struct {
int arg;
int binary;
int call;
int call_args;
int ternary;
} soft_weights = {
#define SOFT_MAX 3
.arg = SOFT_MAX, /* after comma, before argument */
.binary = SOFT_MAX - 1, /* after binary operator */
.call = SOFT_MAX - 1, /* before call */
.call_args = SOFT_MAX - 1, /* after lparen, before call arguments */
.ternary = SOFT_MAX - 1, /* before ternary true/false expr */
};
void
expr_init(void)
{
size_t nrules = sizeof(rules) / sizeof(rules[0]);
size_t i;
for (i = 0; i < nrules; i++) {
const struct expr_rule *er = &rules[i];
table_rules[er->er_type][(er->er_pc & PCUNARY) ? 1 : 0] = er;
}
}
void
expr_shutdown(void)
{
}
struct doc *
expr_exec(const struct expr_exec_arg *ea)
{
struct expr_state es;
struct doc *dc, *expr, *indent, *optional;
struct expr *ex;
arena_scope(ea->arena.scratch, scratch_scope);
expr_state_init(&es, ea, EXPR_MODE_EXEC, &scratch_scope);
ex = expr_exec1(&es, PC0);
if (ex == NULL)
return NULL;
if (lexer_get_error(ea->lx))
return NULL;
dc = doc_max_lines(1, ea->dc);
dc = doc_alloc(DOC_SCOPE, dc);
dc = doc_alloc(DOC_GROUP, dc);
optional = doc_alloc(DOC_OPTIONAL, dc);
if (ea->align > 0)
indent = doc_indent(ea->align, optional);
else if (ea->indent > 0)
indent = doc_indent(ea->indent, optional);
else
indent = doc_alloc(DOC_CONCAT, optional);
if (ea->flags & EXPR_EXEC_LINE)
doc_alloc(DOC_LINE, indent);
if (ea->flags & EXPR_EXEC_SOFTLINE)
doc_alloc(DOC_SOFTLINE, indent);
if (ea->flags & EXPR_EXEC_HARDLINE) {
doc_alloc(DOC_HARDLINE, indent);
/* Needed since the hard line will disable optional line(s). */
indent = doc_alloc(DOC_OPTIONAL, indent);
}
expr = expr_doc(ex, &es, indent);
return expr;
}
int
expr_peek(const struct expr_exec_arg *ea, struct token **tk)
{
struct expr_state es;
struct lexer_state s;
struct expr *ex;
struct lexer *lx = ea->lx;
int peek = 0;
arena_scope(ea->arena.scratch, scratch_scope);
expr_state_init(&es, ea, EXPR_MODE_PEEK, &scratch_scope);
lexer_peek_enter(lx, &s);
ex = expr_exec1(&es, PC0);
if (ex != NULL && lexer_get_error(lx) == 0 && lexer_back(lx, tk))
peek = 1;
lexer_peek_leave(lx, &s);
return peek;
}
static struct expr *
expr_exec1(struct expr_state *es, enum expr_pc pc)
{
const struct expr_rule *er;
struct expr *ex = NULL;
struct token *tk;
if (!expr_exec_peek(es, &tk))
return NULL;
/* Only consider unary operators. */
er = expr_find_rule(tk, 1);
if (er == NULL || tk->tk_type == TOKEN_IDENT) {
/*
* Even if a literal operator was found, let the parser recover
* before continuing. Otherwise, pointer types can be
* erroneously treated as a multiplication expression.
*/
ex = expr_exec_recover(es);
if (ex == NULL && er == NULL)
return NULL;
}
if (ex == NULL) {
es->es_er = er;
if (!lexer_pop(es->es_lx, &es->es_tk))
return NULL;
ex = er->er_func(es, NULL);
}
if (ex == NULL)
return NULL;
for (;;) {
struct expr *tmp;
if (!expr_exec_peek(es, &tk))
break;
/* Only consider binary operators. */
er = expr_find_rule(tk, 0);
if (er == NULL)
break;
es->es_er = er;
if (pc >= PC(er->er_pc))
break;
if (!lexer_pop(es->es_lx, &es->es_tk))
break;
tmp = er->er_func(es, ex);
if (tmp == NULL)
return NULL;
if (lexer_get_error(es->es_lx))
return NULL;
ex = tmp;
}
return ex;
}
static struct expr *
expr_exec_recover(struct expr_state *es)
{
const struct expr_exec_arg *ea = &es->es_ea;
struct doc *dc;
struct expr *ex;
dc = ea->callbacks.recover(ea, ea->callbacks.arg);
if (dc == NULL)
return NULL;
ex = expr_alloc(EXPR_RECOVER, es);
ex->ex_dc = dc;
return ex;
}
static struct expr *
expr_exec_recover_cast(struct expr_state *es)
{
const struct expr_exec_arg *ea = &es->es_ea;
struct doc *dc;
struct expr *ex;
dc = ea->callbacks.recover_cast(ea, ea->callbacks.arg);
if (dc == NULL)
return NULL;
ex = expr_alloc(EXPR_RECOVER, es);
ex->ex_dc = dc;
return ex;
}
static struct expr *
expr_exec_binary(struct expr_state *es, struct expr *lhs)
{
struct expr *ex;
enum expr_pc pc;
int iscomma = es->es_tk->tk_type == TOKEN_COMMA;
pc = PC(es->es_er->er_pc);
if (es->es_er->er_rassoc)
pc--;
ex = expr_alloc(iscomma ? EXPR_ARG : EXPR_BINARY, es);
ex->ex_lhs = lhs;
ex->ex_rhs = expr_exec1(es, pc);
return ex;
}
static struct expr *
expr_exec_concat(struct expr_state *es, struct expr *lhs)
{
struct expr **dst;
struct expr *ex, *rhs;
assert(lhs != NULL);
if (lhs->ex_type == EXPR_CONCAT) {
ex = lhs;
} else {
ex = expr_alloc(EXPR_CONCAT, es);
arena_cleanup(es->es_arena.scratch_scope, expr_free_concat, ex);
if (VECTOR_INIT(ex->ex_concat))
err(1, NULL);
dst = VECTOR_ALLOC(ex->ex_concat);
if (dst == NULL)
err(1, NULL);
*dst = lhs;
}
rhs = expr_exec_literal(es, NULL);
dst = VECTOR_ALLOC(ex->ex_concat);
if (dst == NULL)
err(1, NULL);
*dst = rhs;
return ex;
}
static struct expr *
expr_exec_field(struct expr_state *es, struct expr *lhs)
{
struct expr *ex;
assert(lhs != NULL);
ex = expr_alloc(EXPR_FIELD, es);
ex->ex_lhs = lhs;
ex->ex_rhs = expr_exec1(es, PC(es->es_er->er_pc));
return ex;
}
static struct expr *
expr_exec_literal(struct expr_state *es, struct expr *NDEBUG_UNUSED(lhs))
{
assert(lhs == NULL);
return expr_alloc(EXPR_LITERAL, es);
}
static void
expr_exec_call(struct expr_state *es, struct expr *ex)
{
if (es->es_mode != EXPR_MODE_EXEC ||
ex->ex_lhs->ex_type != EXPR_LITERAL)
return;
simple_cookie(simple);
if (simple_enter(es->es_ea.si, SIMPLE_EXPR_PRINTF, 0, &simple)) {
simple_expr_printf(es->es_lx, ex->ex_lhs->ex_tk,
es->es_arena.eternal_scope);
}
}
static struct expr *
expr_exec_parens(struct expr_state *es, struct expr *lhs)
{
struct expr *ex;
struct token *tk = es->es_tk;
if (lhs == NULL) {
struct expr *cast;
cast = expr_exec_recover_cast(es);
if (cast != NULL) {
ex = expr_alloc(EXPR_CAST, es);
ex->ex_tokens[0] = tk; /* ( */
ex->ex_lhs = cast;
if (lexer_expect(es->es_lx, TOKEN_RPAREN, &tk))
ex->ex_tokens[1] = tk; /* ) */
ex->ex_rhs = expr_exec1(es, PC(es->es_er->er_pc));
return ex;
}
ex = expr_alloc(EXPR_PARENS, es);
ex->ex_lhs = expr_exec1(es, PC0);
} else {
ex = expr_alloc(EXPR_CALL, es);
ex->ex_lhs = lhs;
ex->ex_rhs = expr_exec1(es, PC0);
expr_exec_call(es, ex);
}
ex->ex_tokens[0] = tk; /* ( */
if (lexer_expect(es->es_lx, TOKEN_RPAREN, &tk))
ex->ex_tokens[1] = tk; /* ) */
return ex;
}
static struct expr *
expr_exec_prepost(struct expr_state *es, struct expr *lhs)
{
struct expr *ex;
if (lhs == NULL) {
ex = expr_alloc(EXPR_PREFIX, es);
ex->ex_lhs = expr_exec1(es, PC(es->es_er->er_pc));
} else {
ex = expr_alloc(EXPR_POSTFIX, es);
ex->ex_lhs = lhs;
}
return ex;
}
static struct expr *
expr_exec_sizeof(struct expr_state *es, struct expr *NDEBUG_UNUSED(lhs))
{
struct expr *ex;
struct token *tk;
assert(lhs == NULL);
ex = expr_alloc(EXPR_SIZEOF, es);
if (lexer_if(es->es_lx, TOKEN_LPAREN, &tk)) {
ex->ex_sizeof.lparen = tk;
ex->ex_lhs = expr_exec1(es, PC0);
if (lexer_expect(es->es_lx, TOKEN_RPAREN, &tk))
ex->ex_sizeof.rparen = tk;
} else {
ex->ex_lhs = expr_exec1(es, PC(es->es_er->er_pc));
}
return ex;
}
static struct expr *
expr_exec_squares(struct expr_state *es, struct expr *lhs)
{
struct expr *ex;
struct token *tk;
assert(lhs != NULL);
ex = expr_alloc(EXPR_SQUARES, es);
ex->ex_tokens[0] = es->es_tk; /* [ */
ex->ex_lhs = lhs;
ex->ex_rhs = expr_exec1(es, PC0);
if (lexer_expect(es->es_lx, TOKEN_RSQUARE, &tk))
ex->ex_tokens[1] = tk; /* ] */
return ex;
}
static struct expr *
expr_exec_ternary(struct expr_state *es, struct expr *lhs)
{
struct expr *ex;
struct token *tk;
ex = expr_alloc(EXPR_TERNARY, es);
ex->ex_tokens[0] = es->es_tk; /* ? */
ex->ex_lhs = lhs;
ex->ex_rhs = expr_exec1(es, PC0);
if (lexer_expect(es->es_lx, TOKEN_COLON, &tk))
ex->ex_tokens[1] = tk; /* : */
/*
* Use precedence that excludes comma to not include potential
* subsequent argument(s).
*/
ex->ex_ternary = expr_exec1(es, PC1);
if (ex->ex_ternary == NULL)
return NULL;
return ex;
}
static struct expr *
expr_exec_unary(struct expr_state *es, struct expr *NDEBUG_UNUSED(lhs))
{
struct expr *ex;
assert(lhs == NULL);
assert(es->es_er->er_pc & PCUNARY);
ex = expr_alloc(EXPR_UNARY, es);
ex->ex_lhs = expr_exec1(es, PC(es->es_er->er_pc));
return ex;
}
static int
expr_exec_peek(struct expr_state *es, struct token **tk)
{
struct lexer *lx = es->es_lx;
if (lexer_get_error(lx) ||
!lexer_peek(lx, &es->es_tk) ||
es->es_tk == es->es_ea.stop ||
es->es_tk->tk_type == LEXER_EOF)
return 0;
*tk = es->es_tk;
return 1;
}
static struct expr *
expr_alloc(enum expr_type type, const struct expr_state *es)
{
struct expr *ex;
ex = arena_calloc(es->es_arena.scratch_scope, 1, sizeof(*ex));
ex->ex_type = type;
ex->ex_tk = es->es_tk;
return ex;
}
static struct doc *
expr_doc(struct expr *ex, struct expr_state *es, struct doc *dc)
{
struct doc *concat, *group;
es->es_depth++;
group = doc_alloc(DOC_GROUP, dc);
doc_annotate(group, expr_type_str(ex->ex_type));
concat = doc_alloc(DOC_CONCAT, group);
/*
* Testing backdoor wrapping each expression in parenthesis used for
* validation of operator precedence.
*/
if ((es->es_flags & EXPR_EXEC_TEST) && ex->ex_type != EXPR_PARENS)
doc_literal("(", concat);
switch (ex->ex_type) {
case EXPR_UNARY:
concat = expr_doc_unary(ex, es, concat);
break;
case EXPR_BINARY:
concat = expr_doc_binary(ex, es, concat);
break;
case EXPR_TERNARY:
concat = expr_doc_ternary(ex, es, concat);
break;
case EXPR_PREFIX:
expr_doc_token(es, ex->ex_tk, concat);
if (ex->ex_lhs != NULL)
expr_doc(ex->ex_lhs, es, concat);
break;
case EXPR_POSTFIX:
if (ex->ex_lhs != NULL)
expr_doc(ex->ex_lhs, es, concat);
expr_doc_token(es, ex->ex_tk, concat);
break;
case EXPR_PARENS:
concat = expr_doc_parens(ex, es, concat);
break;
case EXPR_SQUARES:
if (ex->ex_lhs != NULL)
concat = expr_doc(ex->ex_lhs, es, concat);
if (ex->ex_tokens[0] != NULL)
expr_doc_token(es, ex->ex_tokens[0], concat); /* [ */
if (ex->ex_rhs != NULL)
concat = expr_doc(ex->ex_rhs, es, concat);
if (ex->ex_tokens[1] != NULL)
expr_doc_token(es, ex->ex_tokens[1], concat); /* ] */
break;
case EXPR_FIELD:
concat = expr_doc_field(ex, es, concat);
break;
case EXPR_CALL:
concat = expr_doc_call(ex, es, concat);
break;
case EXPR_ARG:
concat = expr_doc_arg(ex, es, concat);
break;
case EXPR_CAST:
if (ex->ex_tokens[0] != NULL)
expr_doc_token(es, ex->ex_tokens[0], concat); /* ( */
if (ex->ex_lhs != NULL)
expr_doc(ex->ex_lhs, es, concat);
if (ex->ex_tokens[1] != NULL)
expr_doc_token(es, ex->ex_tokens[1], concat); /* ) */
if (ex->ex_rhs != NULL)
expr_doc(ex->ex_rhs, es, concat);
break;
case EXPR_SIZEOF:
concat = expr_doc_sizeof(ex, es, concat);
break;
case EXPR_CONCAT:
concat = expr_doc_concat(ex, es, concat);
break;
case EXPR_LITERAL:
expr_doc_token(es, ex->ex_tk, concat);
break;
case EXPR_RECOVER:
concat = expr_doc_recover(ex, es, concat);
break;
}
/* Testing backdoor, see above. */
if ((es->es_flags & EXPR_EXEC_TEST) && ex->ex_type != EXPR_PARENS)
doc_literal(")", concat);
es->es_depth--;
return concat;
}
static struct doc *
expr_doc_unary(struct expr *ex, struct expr_state *es, struct doc *dc)
{
expr_doc_token(es, ex->ex_tk, dc);
if (ex->ex_lhs != NULL)
dc = expr_doc(ex->ex_lhs, es, dc);
return dc;
}
static struct doc *
expr_doc_binary(struct expr *ex, struct expr_state *es, struct doc *dc)
{
const struct style *st = es->es_st;
int doalign = style(st, AlignOperands) == Align;
if (ex->ex_tk->tk_flags & TOKEN_FLAG_ASSIGN) {
struct doc *lhs;
es->es_nassign++;
lhs = expr_doc(ex->ex_lhs, es, dc);
doc_literal(" ", lhs);
expr_doc_token(es, ex->ex_tk, lhs);
doc_literal(" ", lhs);
if (ex->ex_rhs != NULL) {
if (doalign) {
dc = token_has_line(ex->ex_tk, 1) ?
expr_doc_align_disable(ex, es, dc, 0) :
expr_doc_align(ex, es, dc, 0);
}
/*
* Same semantics as variable declarations, do not break
* after the assignment operator.
*/
if (es->es_nassign > 1) {
dc = expr_doc_soft(ex->ex_rhs, es, dc,
soft_weights.binary);
} else {
dc = expr_doc(ex->ex_rhs, es, dc);
}
}
es->es_nassign--;
} else if (style(st, BreakBeforeBinaryOperators) == NonAssignment ||
style(st, BreakBeforeBinaryOperators) == All) {
struct doc *lhs;
int dospace;
if (doalign)
dc = expr_doc_align(ex, es, dc, 0);
token_move_next_line(ex->ex_tk);
lhs = expr_doc(ex->ex_lhs, es, dc);
dospace = expr_doc_has_spaces(ex);
if (dospace)
doc_alloc(DOC_LINE, lhs);
dc = doc_alloc(DOC_CONCAT, doc_alloc(DOC_GROUP, dc));
doc_alloc(DOC_SOFTLINE, dc);
expr_doc_token(es, ex->ex_tk, dc);
if (dospace)
doc_literal(" ", dc);
if (ex->ex_rhs != NULL)
dc = expr_doc(ex->ex_rhs, es, dc);
} else {
struct doc *lhs;
int dospace;
if (doalign)
dc = expr_doc_align(ex, es, dc, 0);
token_move_prev_line(ex->ex_tk);
lhs = expr_doc(ex->ex_lhs, es, dc);
dospace = expr_doc_has_spaces(ex);
if (dospace)
doc_literal(" ", lhs);
expr_doc_token(es, ex->ex_tk, lhs);
dc = doc_alloc(DOC_CONCAT, doc_alloc(DOC_GROUP, dc));
/*
* If the operator is followed by a trailing comment and a new
* line, ensure that the new line is honored even when optional
* new line(s) are ignored.
*/
if (token_has_suffix(ex->ex_tk, TOKEN_COMMENT) &&
token_has_line(ex->ex_tk, 1))
doc_alloc(DOC_HARDLINE, lhs);
else if (dospace)
doc_alloc(DOC_LINE, dc);
if (ex->ex_rhs != NULL) {
dc = expr_doc_soft(ex->ex_rhs, es, dc,
soft_weights.binary);
}
}
return dc;
}
static struct doc *
expr_doc_parens(struct expr *ex, struct expr_state *es, struct doc *dc)
{
simple_cookie(simple);
if (es->es_depth == 1 &&
simple_enter(es->es_ea.si, SIMPLE_EXPR_PARENS, 0, &simple)) {
if (ex->ex_lhs != NULL)
dc = expr_doc(ex->ex_lhs, es, dc);
} else {
struct token *pv;
if (ex->ex_tokens[0] != NULL)
expr_doc_token(es, ex->ex_tokens[0], dc); /* ( */
if (ex->ex_tokens[1] != NULL &&
(pv = token_prev(ex->ex_tokens[1])) != NULL)
token_trim(pv);
if (style(es->es_st, AlignAfterOpenBracket) == Align)
dc = doc_indent(DOC_INDENT_WIDTH, dc);
else
dc = expr_doc_indent_parens(es, dc);
if (ex->ex_lhs != NULL)
dc = expr_doc(ex->ex_lhs, es, dc);
if (ex->ex_tokens[1] != NULL)
expr_doc_token(es, ex->ex_tokens[1], dc); /* ) */
}
return dc;
}
static struct doc *
expr_doc_field(struct expr *ex, struct expr_state *es, struct doc *dc)
{
dc = expr_doc(ex->ex_lhs, es, dc);
token_trim(ex->ex_tk);
expr_doc_token(es, ex->ex_tk, dc);
if (ex->ex_rhs != NULL)
dc = expr_doc(ex->ex_rhs, es, dc);
return dc;
}
static struct doc *
expr_doc_call(struct expr *ex, struct expr_state *es, struct doc *dc)
{
struct token *lparen = ex->ex_tokens[0];
struct token *rparen = ex->ex_tokens[1];
int fold_rparens = 0;
es->es_ncalls++;
es->es_noparens++;
if (es->es_ncalls > 1)
dc = expr_doc_soft(ex->ex_lhs, es, dc, soft_weights.call);
else
dc = expr_doc(ex->ex_lhs, es, dc);
es->es_noparens--;
expr_doc_token(es, lparen, dc);
if (ex->ex_rhs != NULL) {
if (rparen != NULL) {
struct token *pv;
/* Try to not break before the closing parens. */
pv = token_prev(rparen);
if (pv != NULL && !token_has_c99_comment(pv)) {
token_trim(pv);
fold_rparens = 1;
}
}
if (style(es->es_st, AlignAfterOpenBracket) == Align) {
unsigned int indent;
if (es->es_ncalls == 1 && es->es_ea.align == 0 &&
(es->es_ea.flags & EXPR_EXEC_HARDLINE) == 0)
indent = 0;
else
indent = es->es_ea.indent;
dc = token_has_line(lparen, 1) ?
expr_doc_align_disable(ex, es, dc, indent) :
expr_doc_align(ex, es, dc, indent);
}
dc = expr_doc_soft(ex->ex_rhs, es, dc, soft_weights.call_args);
}
if (rparen != NULL) {
if (fold_rparens) {
expr_doc_token(es, rparen, dc);
} else {
struct doc *dedent;
/*
* Must break before closing parens, indentation for the
* outer scope is expected.
*/
dedent = doc_dedent(es->es_ea.indent, dc);
expr_doc_token(es, rparen, dedent);
}
}
es->es_ncalls--;
return dc;
}
static struct doc *