varnish-cache/lib/libvcc/vcc_expr.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2011 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 *
30
 */
31
32
#include "config.h"
33
34
#include <math.h>
35
#include <stdarg.h>
36
#include <stdlib.h>
37
#include <string.h>
38
39
#include "vcc_compile.h"
40
#include "vjsn.h"
41
42
struct expr {
43
        unsigned        magic;
44
#define EXPR_MAGIC      0x38c794ab
45
        vcc_type_t      fmt;
46
        struct vsb      *vsb;
47
        uint8_t         constant;
48
#define EXPR_VAR        (1<<0)
49
#define EXPR_CONST      (1<<1)
50
#define EXPR_STR_CONST  (1<<2)          // Last string elem is "..."
51
        struct token    *t1, *t2;
52
        struct symbol   *instance;
53
        int             nstr;
54
};
55
56
/*--------------------------------------------------------------------
57
 * Facility for carrying expressions around and do text-processing on
58
 * them.
59
 */
60
61
static inline int
62 5726
vcc_isconst(const struct expr *e)
63
{
64 5726
        AN(e->constant);
65 5726
        return (e->constant & EXPR_CONST);
66
}
67
68
static inline int
69 67519
vcc_islit(const struct expr *e)
70
{
71 67519
        AN(e->constant);
72 67519
        return (e->constant & EXPR_STR_CONST);
73
}
74
75
static const char *
76 67
vcc_utype(vcc_type_t t)
77
{
78 67
        if (t == STRINGS || t->stringform)
79 18
                t = STRING;
80 67
        return (t->name);
81
}
82
83
static vcc_type_t
84 624145
vcc_stringstype(vcc_type_t t)
85
{
86 624145
        return (t->stringform ? STRINGS : t);
87
}
88
89
90
static void vcc_expr0(struct vcc *tl, struct expr **e, vcc_type_t fmt);
91
static void vcc_expr_cor(struct vcc *tl, struct expr **e, vcc_type_t fmt);
92
static void vcc_expr_typecheck(struct vcc *tl, struct expr **e, vcc_type_t fmt,
93
    struct token *t1);
94
95
static struct expr *
96 592860
vcc_new_expr(vcc_type_t fmt)
97
{
98
        struct expr *e;
99
100 592860
        ALLOC_OBJ(e, EXPR_MAGIC);
101 592860
        AN(e);
102 592860
        e->vsb = VSB_new_auto();
103 592860
        e->fmt = fmt;
104 592860
        e->constant = EXPR_VAR;
105 592860
        return (e);
106
}
107
108
static struct expr * v_printflike_(2, 3)
109 157284
vcc_mk_expr(vcc_type_t fmt, const char *str, ...)
110
{
111
        va_list ap;
112
        struct expr *e;
113
114 157284
        e = vcc_new_expr(fmt);
115 157284
        va_start(ap, str);
116 157284
        VSB_vprintf(e->vsb, str, ap);
117 157284
        va_end(ap);
118 157284
        AZ(VSB_finish(e->vsb));
119 157284
        return (e);
120
}
121
122
static void
123 755655
vcc_delete_expr(struct expr *e)
124
{
125 755655
        if (e == NULL)
126 163649
                return;
127 592006
        CHECK_OBJ(e, EXPR_MAGIC);
128 592006
        VSB_destroy(&e->vsb);
129 592006
        FREE_OBJ(e);
130 755655
}
131
132
/*--------------------------------------------------------------------
133
 * We want to get the indentation right in the emitted C code so we have
134
 * to represent it symbolically until we are ready to render.
135
 *
136
 * Many of the operations have very schematic output syntaxes, so we
137
 * use the same facility to simplify the text-processing of emitting
138
 * a given operation on two subexpressions.
139
 *
140
 * We use '\v' as the magic escape character.
141
 *      \v1  insert subexpression 1
142
 *      \v2  insert subexpression 2
143
 *      \vS  insert subexpression 1(STRINGS) as STRING
144
 *      \vs  insert subexpression 2(STRINGS) as STRING
145
 *      \vT  insert subexpression 1(STRINGS) as STRANDS
146
 *      \vt  insert subexpression 2(STRINGS) as STRANDS
147
 *      \v+  increase indentation
148
 *      \v-  decrease indentation
149
 *      anything else is literal
150
 *
151
 * When editing, we check if any of the subexpressions contain a newline
152
 * and issue it as an indented block of so.
153
 *
154
 * XXX: check line lengths in edit, should pass indent in for this
155
 */
156
157
static void
158 50365
vcc_strands_edit(const struct expr *e1, const struct expr *e2)
159
{
160
161 50365
        assert(e2->fmt == STRANDS || e2->fmt == STRINGS);
162
163 50365
        if (e2->fmt == STRANDS)
164 4
                VSB_cat(e1->vsb, VSB_data(e2->vsb));
165 50361
        else if (e2->nstr == 0)
166 0
                VSB_printf(e1->vsb, "vrt_null_strands");
167 50361
        else if (e2->nstr == 1)
168 44564
                VSB_printf(e1->vsb, "TOSTRAND(%s)", VSB_data(e2->vsb));
169
        else {
170 11594
                VSB_printf(e1->vsb, "TOSTRANDS(%d,\v+\n%s\v-)",
171 5797
                   e2->nstr, VSB_data(e2->vsb));
172
        }
173 50365
}
174
175
static struct expr *
176 332125
vcc_expr_edit(struct vcc *tl, vcc_type_t fmt, const char *p, struct expr *e1,
177
    struct expr *e2)
178
{
179
        struct expr *e, *e3;
180 332125
        int nl = 1;
181
182 332125
        (void) tl;
183
184 332125
        AN(e1);
185 332125
        e = vcc_new_expr(fmt);
186 3907164
        while (*p != '\0') {
187 3575039
                if (*p != '\v') {
188 2965080
                        if (*p != '\n' || !nl)
189 2965032
                                VSB_putc(e->vsb, *p);
190 2965080
                        nl = (*p == '\n');
191 2965080
                        p++;
192 2965080
                        continue;
193
                }
194 609959
                assert(*p == '\v');
195 609959
                switch (*++p) {
196 53110
                case '+': VSB_cat(e->vsb, "\v+"); nl = 0; break;
197 56243
                case '-': VSB_cat(e->vsb, "\v-"); nl = 0; break;
198
                case 'S':
199
                case 's':
200 12018
                        e3 = (*p == 'S' ? e1 : e2);
201 12018
                        AN(e3);
202 12018
                        assert(e1->fmt == STRINGS);
203 12018
                        if (e3->nstr > 1) {
204 26
                                VSB_cat(e->vsb,
205
                                    "\nVRT_STRANDS_string(ctx,\v+\n");
206 26
                                vcc_strands_edit(e, e3);
207 26
                                VSB_cat(e->vsb,
208
                                    "\v-\n)\n");
209 26
                        } else {
210 11992
                                VSB_cat(e->vsb, VSB_data(e3->vsb));
211
                        }
212 12018
                        break;
213
                case 'T':
214
                case 't':
215 50339
                        e3 = (*p == 'T' ? e1 : e2);
216 50339
                        AN(e3);
217 50339
                        vcc_strands_edit(e, e3);
218 50339
                        break;
219
                case '1':
220 269799
                        VSB_cat(e->vsb, VSB_data(e1->vsb));
221 269799
                        break;
222
                case '2':
223 168450
                        AN(e2);
224 168450
                        VSB_cat(e->vsb, VSB_data(e2->vsb));
225 168450
                        break;
226
                default:
227 0
                        WRONG("Illegal edit in VCC expression");
228 0
                }
229 609959
                p++;
230
        }
231 332125
        AZ(VSB_finish(e->vsb));
232 332125
        e->t1 = e1->t1;
233 332125
        e->t2 = e1->t2;
234 332125
        if (e2 != NULL)
235 168481
                e->t2 = e2->t2;
236 332125
        vcc_delete_expr(e1);
237 332125
        vcc_delete_expr(e2);
238 332125
        return (e);
239
}
240
241
/*--------------------------------------------------------------------
242
 * Expand finished expression into C-source code
243
 */
244
245
static void
246 91398
vcc_expr_fmt(struct vsb *d, int ind, const struct expr *e1)
247
{
248
        char *p;
249
        int i;
250
251 91398
        if (!e1->fmt->noindent) {
252 813174
                for (i = 0; i < ind; i++)
253 727380
                        VSB_putc(d, ' ');
254 85794
        }
255 91398
        p = VSB_data(e1->vsb);
256 8941129
        while (*p != '\0') {
257 8849735
                if (*p == '\n') {
258 268688
                        VSB_putc(d, '\n');
259 268688
                        if (*++p == '\0')
260 4
                                break;
261 2878746
                        for (i = 0; i < ind; i++)
262 2610062
                                VSB_putc(d, ' ');
263 8849731
                } else if (*p != '\v') {
264 8456931
                        VSB_putc(d, *p++);
265 8456931
                } else {
266 124116
                        switch (*++p) {
267 62058
                        case '+': ind += INDENT; break;
268 62058
                        case '-': ind -= INDENT; break;
269 0
                        default:  WRONG("Illegal format in VCC expression");
270 0
                        }
271 124116
                        p++;
272
                }
273
        }
274 91398
}
275
276
/*--------------------------------------------------------------------
277
 */
278
279
static void
280 69983
vcc_expr_tobool(struct vcc *tl, struct expr **e)
281
{
282
283 69983
        if ((*e)->fmt == BOOL)
284 53122
                return;
285 16861
        if ((*e)->fmt == BACKEND || (*e)->fmt == INT)
286 10
                *e = vcc_expr_edit(tl, BOOL, "(\v1 != 0)", *e, NULL);
287 16851
        else if ((*e)->fmt == DURATION)
288 2
                *e = vcc_expr_edit(tl, BOOL, "(\v1 > 0)", *e, NULL);
289 16849
        else if ((*e)->fmt == STRINGS)
290 16843
                *e = vcc_expr_edit(tl, BOOL, "VRT_Strands2Bool(\vT)", *e, NULL);
291
        /*
292
         * We do not provide automatic folding from REAL to BOOL
293
         * because comparing to zero is seldom an exact science
294
         * and we want to force people to explicitly get it right.
295
         */
296 69983
}
297
298
/*--------------------------------------------------------------------
299
 */
300
301
static void
302 54735
vcc_expr_tostring(struct vcc *tl, struct expr **e)
303
{
304
        const char *p;
305 54735
        uint8_t constant = EXPR_VAR;
306
307 54735
        CHECK_OBJ_NOTNULL(*e, EXPR_MAGIC);
308 54735
        assert((*e)->fmt != STRINGS);
309
310 54735
        p = (*e)->fmt->tostring;
311 54735
        if (p != NULL) {
312 54734
                AN(*p);
313 54734
                *e = vcc_expr_edit(tl, STRINGS, p, *e, NULL);
314 54734
                (*e)->constant = constant;
315 54734
                (*e)->nstr = 1;
316 54734
        } else {
317 2
                VSB_printf(tl->sb,
318
                    "Cannot convert %s to STRING.\n",
319 1
                    vcc_utype((*e)->fmt));
320 1
                vcc_ErrWhere2(tl, (*e)->t1, tl->t);
321
        }
322 54735
}
323
324
/*--------------------------------------------------------------------
325
 */
326
327
void v_matchproto_(sym_expr_t)
328 414
vcc_Eval_Handle(struct vcc *tl, struct expr **e, struct token *t,
329
    struct symbol *sym, vcc_type_t type)
330
{
331
332 414
        (void)t;
333 414
        (void)tl;
334 414
        AN(sym->rname);
335 414
        AZ(type->stringform);
336
337 419
        if (sym->type->tostring == NULL &&
338 5
            sym->type != STRING && type == STRINGS) {
339 0
                *e = vcc_mk_expr(STRINGS, "\"%s\"", sym->name);
340 0
                (*e)->nstr = 1;
341 0
                (*e)->constant |= EXPR_CONST | EXPR_STR_CONST;
342 0
        } else {
343 414
                *e = vcc_mk_expr(sym->type, "%s", sym->rname);
344 414
                (*e)->constant = EXPR_VAR;
345 414
                (*e)->nstr = 1;
346 414
                if ((*e)->fmt == STRING)
347 0
                        (*e)->fmt = STRINGS;
348
        }
349 414
}
350
351
void v_matchproto_(sym_expr_t)
352 23
vcc_Eval_Sub(struct vcc *tl, struct expr **e, struct token *t,
353
    struct symbol *sym, vcc_type_t type)
354
{
355
356 23
        (void)t;
357 23
        (void)tl;
358 23
        AN(sym->rname);
359 23
        AZ(type->stringform);
360
361 23
        assert (sym->type == SUB);
362
363 23
        if (type == SUB) {
364 20
                *e = vcc_mk_expr(sym->type, "%s", sym->rname);
365 20
                (*e)->constant = EXPR_CONST;
366 20
                return;
367
        }
368
369 6
        VSB_printf(tl->sb, "Symbol '%s' can only be used as a %s expression\n",
370 3
            sym->name, sym->type->name);
371 3
        vcc_ErrWhere(tl, tl->t);
372 23
}
373
374
/*--------------------------------------------------------------------
375
 */
376
377
void v_matchproto_(sym_expr_t)
378 124927
vcc_Eval_Var(struct vcc *tl, struct expr **e, struct token *t,
379
    struct symbol *sym, vcc_type_t type)
380
{
381
382 124927
        (void)type;
383 124927
        vcc_AddUses(tl, t, NULL, sym, XREF_READ);
384 124927
        ERRCHK(tl);
385 124927
        *e = vcc_mk_expr(sym->type, "%s", sym->rname);
386 124927
        (*e)->constant = EXPR_VAR;
387 124927
        (*e)->nstr = 1;
388 124927
        if ((*e)->fmt == STRING)
389 56362
                (*e)->fmt = STRINGS;
390 124927
}
391
392
void v_matchproto_(sym_expr_t)
393 11
vcc_Eval_ProtectedHeader(struct vcc *tl, struct expr **e, struct token *t,
394
    struct symbol *sym, vcc_type_t type)
395
{
396
397 11
        AN(sym);
398 11
        AZ(sym->lorev);
399
400 11
        vcc_Header_Fh(tl, sym);
401 11
        sym->eval = vcc_Eval_Var;
402 11
        vcc_Eval_Var(tl, e, t, sym, type);
403 11
}
404
405
/*--------------------------------------------------------------------
406
 */
407
408
static struct expr *
409 172
vcc_priv_arg(struct vcc *tl, const char *p, struct symbol *sym)
410
{
411
        char buf[64];
412
        struct inifin *ifp;
413 172
        const char *f = NULL;
414
415 172
        AN(sym);
416 172
        AN(sym->vmod_name);
417
418 172
        if (!strcmp(p, "PRIV_VCL"))
419 15
                return (vcc_mk_expr(VOID, "&vmod_priv_%s", sym->vmod_name));
420
421 157
        if (!strcmp(p, "PRIV_CALL")) {
422 22
                bprintf(buf, "vmod_priv_%u", tl->unique++);
423 22
                ifp = New_IniFin(tl);
424 22
                Fh(tl, 0, "static struct vmod_priv %s;\n", buf);
425 22
                VSB_printf(ifp->fin, "\tVRT_priv_fini(ctx, &%s);", buf);
426 22
                return (vcc_mk_expr(VOID, "&%s", buf));
427
        }
428
429 135
        if (!strcmp(p, "PRIV_TASK"))
430 127
                f = "task";
431 8
        else if (!strcmp(p, "PRIV_TOP")) {
432 8
                f = "top";
433 8
                sym->r_methods &= VCL_MET_TASK_C;
434 8
        } else {
435 0
                WRONG("Wrong PRIV_ type");
436
        }
437 135
        AN(f);
438
439 135
        return (vcc_mk_expr(VOID, "VRT_priv_%s(ctx, &VGC_vmod_%s)",
440 135
            f, sym->vmod_name));
441 172
}
442
443
struct func_arg {
444
        vcc_type_t              type;
445
        const struct vjsn_val   *enums;
446
        const char              *name;
447
        const char              *val;
448
        struct expr             *result;
449
        int                     avail;
450
        int                     optional;
451
        VTAILQ_ENTRY(func_arg)  list;
452
};
453
454
static struct expr *
455 1817
vcc_do_enum(struct vcc *tl, const char *cfunc, int len, const char *ptr)
456
{
457
        const char *r;
458
459 1817
        (void)tl;
460 1817
        r = strchr(cfunc, '.');
461 1817
        AN(r);
462 1817
        return (vcc_mk_expr(VOID, "*%.*s.enum_%.*s",
463 1817
            (int)(r - cfunc), cfunc, len, ptr));
464
}
465
466
static void
467 3973
vcc_do_arg(struct vcc *tl, const char *cfunc, struct func_arg *fa)
468
{
469
        struct expr *e2;
470
        struct vjsn_val *vv;
471
472 3973
        if (fa->type == ENUM) {
473 1335
                ExpectErr(tl, ID);
474 1335
                ERRCHK(tl);
475 4568
                VTAILQ_FOREACH(vv, &fa->enums->children, list)
476 4566
                        if (vcc_IdIs(tl->t, vv->value))
477 1333
                                break;
478 1335
                if (vv == NULL) {
479 2
                        VSB_cat(tl->sb, "Wrong enum value.");
480 2
                        VSB_cat(tl->sb, "  Expected one of:\n");
481 10
                        VTAILQ_FOREACH(vv, &fa->enums->children, list)
482 8
                                VSB_printf(tl->sb, "\t%s\n", vv->value);
483 2
                        vcc_ErrWhere(tl, tl->t);
484 2
                        return;
485
                }
486 1333
                fa->result = vcc_do_enum(tl, cfunc, PF(tl->t));
487 1333
                SkipToken(tl, ID);
488 1333
        } else {
489 2638
                if (fa->type == SUB)
490 20
                        tl->subref++;
491 2638
                vcc_expr0(tl, &e2, fa->type);
492 2638
                ERRCHK(tl);
493 2630
                assert(e2->fmt == fa->type);
494 2630
                fa->result = e2;
495
        }
496 3963
        fa->avail = 1;
497 3973
}
498
499
static void
500 2750
vcc_func(struct vcc *tl, struct expr **e, const void *priv,
501
    const char *extra, struct symbol *sym)
502
{
503
        vcc_type_t rfmt;
504
        const char *cfunc;
505
        struct expr *e1;
506
        struct func_arg *fa, *fa2;
507
        VTAILQ_HEAD(,func_arg) head;
508
        struct token *tf, *t1;
509
        const struct vjsn_val *vv, *vvp;
510
        const char *sa, *extra_sep;
511
        char ssa[64];
512
        int n;
513
514 2750
        CAST_OBJ_NOTNULL(vv, priv, VJSN_VAL_MAGIC);
515 2750
        assert(vjsn_is_array(vv));
516 2750
        vv = VTAILQ_FIRST(&vv->children);
517 2750
        rfmt = VCC_Type(VTAILQ_FIRST(&vv->children)->value);
518 2750
        AN(rfmt);
519 2750
        vv = VTAILQ_NEXT(vv, list);
520 2750
        cfunc = vv->value;
521 2750
        vv = VTAILQ_NEXT(vv, list);
522 2750
        sa = vv->value;
523 2750
        if (*sa == '\0') {
524 2347
                sa = NULL;
525 2347
        }
526 2750
        vv = VTAILQ_NEXT(vv, list);
527 2750
        if (sym->kind == SYM_METHOD) {
528 774
                if (*e == NULL) {
529 2
                        VSB_cat(tl->sb, "Syntax error.");
530 2
                        tl->err = 1;
531 2
                        return;
532
                }
533 772
                vcc_NextToken(tl);
534 772
                AZ(extra);
535 772
                AN((*e)->instance);
536 772
                extra = (*e)->instance->rname;
537 772
        }
538 2748
        tf = VTAILQ_PREV(tl->t, tokenhead, list);
539 2748
        SkipToken(tl, '(');
540 2748
        if (extra == NULL) {
541 1775
                extra = "";
542 1775
                extra_sep = "";
543 1775
        } else {
544 973
                AN(*extra);
545 973
                extra_sep = ", ";
546
        }
547 2748
        VTAILQ_INIT(&head);
548 9213
        for (;vv != NULL; vv = VTAILQ_NEXT(vv, list)) {
549 6465
                assert(vjsn_is_array(vv));
550 6465
                fa = calloc(1, sizeof *fa);
551 6465
                AN(fa);
552 6465
                VTAILQ_INSERT_TAIL(&head, fa, list);
553
554 6465
                vvp = VTAILQ_FIRST(&vv->children);
555 6465
                if (!memcmp(vvp->value, "PRIV_", 5)) {
556 172
                        fa->result = vcc_priv_arg(tl, vvp->value, sym);
557 172
                        vvp = VTAILQ_NEXT(vvp, list);
558 172
                        if (vvp != NULL)
559 3
                                fa->name = vvp->value;
560 172
                        continue;
561
                }
562 6293
                fa->type = VCC_Type(vvp->value);
563 6293
                AN(fa->type);
564 6293
                vvp = VTAILQ_NEXT(vvp, list);
565 6293
                if (vvp != NULL) {
566 5900
                        fa->name = vvp->value;
567 5900
                        vvp = VTAILQ_NEXT(vvp, list);
568 5900
                        if (vvp != NULL) {
569 4324
                                fa->val = vvp->value;
570 4324
                                vvp = VTAILQ_NEXT(vvp, list);
571 4324
                                if (vvp != NULL) {
572 3515
                                        fa->enums = vvp;
573 3515
                                        vvp = VTAILQ_NEXT(vvp, list);
574 3515
                                }
575 4324
                        }
576 5900
                }
577 6293
                if (sa != NULL && vvp != NULL && vjsn_is_true(vvp)) {
578 1821
                        fa->optional = 1;
579 1821
                        vvp = VTAILQ_NEXT(vvp, list);
580 1821
                }
581 6293
                AZ(vvp);
582 6293
        }
583
584 4431
        VTAILQ_FOREACH(fa, &head, list) {
585 3974
                if (tl->t->tok == ')')
586 89
                        break;
587 3885
                if (fa->result != NULL)
588 123
                        continue;
589 3762
                if (tl->t->tok == ID) {
590 2768
                        t1 = VTAILQ_NEXT(tl->t, list);
591 2768
                        if (t1->tok == '=')
592 752
                                break;
593 2016
                }
594 3010
                vcc_do_arg(tl, cfunc, fa);
595 3010
                if (tl->err)
596 20
                        VSB_printf(tl->sb, "Expected argument: %s %s\n\n",
597 10
                            fa->type->name,
598 10
                            fa->name ? fa->name : "(unnamed argument)");
599 3010
                ERRCHK(tl);
600 3000
                if (tl->t->tok == ')')
601 1440
                        break;
602 1560
                SkipToken(tl, ',');
603 1560
        }
604 2951
        while (tl->t->tok == ID) {
605 3134
                VTAILQ_FOREACH(fa, &head, list) {
606 3133
                        if (fa->name == NULL)
607 12
                                continue;
608 3121
                        if (vcc_IdIs(tl->t, fa->name))
609 964
                                break;
610 2157
                }
611 965
                if (fa == NULL) {
612 2
                        VSB_printf(tl->sb, "Unknown argument '%.*s'\n",
613 1
                            PF(tl->t));
614 1
                        vcc_ErrWhere(tl, tl->t);
615 1
                        return;
616
                }
617 964
                if (fa->result != NULL) {
618 1
                        AN(fa->name);
619 2
                        VSB_printf(tl->sb, "Argument '%s' already used\n",
620 1
                            fa->name);
621 1
                        vcc_ErrWhere(tl, tl->t);
622 1
                        return;
623
                }
624 963
                vcc_NextToken(tl);
625 963
                SkipToken(tl, '=');
626 963
                vcc_do_arg(tl, cfunc, fa);
627 963
                ERRCHK(tl);
628 963
                if (tl->t->tok == ')')
629 750
                        break;
630 213
                SkipToken(tl, ',');
631
        }
632
633 2736
        if (sa != NULL)
634 798
                e1 = vcc_mk_expr(rfmt, "%s(ctx%s%s,\v+\n&(%s)\v+ {\n",
635 399
                    cfunc, extra_sep, extra, sa);
636
        else
637 4674
                e1 = vcc_mk_expr(rfmt, "%s(ctx%s%s\v+",
638 2337
                    cfunc, extra_sep, extra);
639 2736
        n = 0;
640 9168
        VTAILQ_FOREACH_SAFE(fa, &head, list, fa2) {
641 6432
                n++;
642 6432
                if (fa->optional) {
643 1815
                        AN(fa->name);
644 1815
                        bprintf(ssa, "\v1.valid_%s = %d,\n",
645
                            fa->name, fa->avail);
646 1815
                        e1 = vcc_expr_edit(tl, e1->fmt, ssa, e1, NULL);
647 1815
                }
648 6432
                if (fa->result == NULL && fa->type == ENUM && fa->val != NULL)
649 484
                        fa->result = vcc_do_enum(tl, cfunc, strlen(fa->val), fa->val);
650 6432
                if (fa->result == NULL && fa->val != NULL)
651 598
                        fa->result = vcc_mk_expr(fa->type, "%s", fa->val);
652 6432
                if (fa->result != NULL && sa != NULL) {
653 866
                        if (fa->name)
654 845
                                bprintf(ssa, "\v1.%s = \v2,\n", fa->name);
655
                        else
656 21
                                bprintf(ssa, "\v1.arg%d = \v2,\n", n);
657 866
                        e1 = vcc_expr_edit(tl, e1->fmt, ssa, e1, fa->result);
658 6432
                } else if (fa->result != NULL) {
659 8692
                        e1 = vcc_expr_edit(tl, e1->fmt, "\v1,\n\v2",
660 4346
                            e1, fa->result);
661 5566
                } else if (!fa->optional) {
662 1
                        if (fa->name)
663 2
                                VSB_printf(tl->sb, "Argument '%s' missing\n",
664 1
                                    fa->name);
665
                        else
666 0
                                VSB_printf(tl->sb, "Argument %d missing\n", n);
667 1
                        vcc_ErrWhere(tl, tl->t);
668 1
                }
669 6432
                free(fa);
670 6432
        }
671 2736
        if (sa != NULL) {
672 399
                *e = vcc_expr_edit(tl, e1->fmt, "\v1\v-\n}\v-\n)", e1, NULL);
673 399
        } else {
674 2337
                *e = vcc_expr_edit(tl, e1->fmt, "\v1\v-\n)", e1, NULL);
675
        }
676 2736
        SkipToken(tl, ')');
677 2735
        vcc_AddUses(tl, tf, NULL, sym, XREF_READ);
678 2750
}
679
680
681
/*--------------------------------------------------------------------
682
 */
683
684
void
685 201
vcc_Eval_Func(struct vcc *tl, const struct vjsn_val *spec,
686
    const char *extra, struct symbol *sym)
687
{
688 201
        struct expr *e = NULL;
689
690 201
        vcc_func(tl, &e, spec, extra, sym);
691 201
        if (tl->err)
692 0
                VSB_cat(tl->sb, "While compiling function call:\n");
693 201
        ERRCHK(tl);
694 201
        vcc_expr_fmt(tl->fb, tl->indent, e);
695 201
        VSB_cat(tl->fb, ";\n");
696 201
        vcc_delete_expr(e);
697 201
}
698
699
/*--------------------------------------------------------------------
700
 */
701
702
void v_matchproto_(sym_expr_t)
703 2077
vcc_Eval_SymFunc(struct vcc *tl, struct expr **e, struct token *t,
704
    struct symbol *sym, vcc_type_t fmt)
705
{
706
707 2077
        (void)t;
708 2077
        (void)fmt;
709 2077
        assert(sym->kind == SYM_FUNC || sym->kind == SYM_METHOD);
710 2077
        AN(sym->eval_priv);
711
712 2077
        vcc_func(tl, e, sym->eval_priv, sym->extra, sym);
713 2077
        ERRCHK(tl);
714 2067
        if ((*e)->fmt == STRING) {
715 752
                (*e)->fmt = STRINGS;
716 752
                (*e)->nstr = 1;
717 752
        }
718 2077
}
719
720
/*--------------------------------------------------------------------
721
 */
722
723
static void
724 23537
vcc_number(struct vcc *tl, struct expr **e, vcc_type_t fmt, const char *sign)
725
{
726
        VCL_INT vi;
727
        struct expr *e1;
728
        struct token *t;
729
730 23537
        assert(fmt != VOID);
731 23537
        if (fmt == BYTES) {
732 48
                vcc_ByteVal(tl, &vi);
733 48
                ERRCHK(tl);
734 46
                e1 = vcc_mk_expr(BYTES, "%ju", (intmax_t)vi);
735 46
        } else {
736 23489
                t = tl->t;
737 23489
                vcc_NextToken(tl);
738 23489
                if (tl->t->tok == ID) {
739 5818
                        e1 = vcc_mk_expr(DURATION, "%s%.3f * %g",
740 5818
                            sign, t->num, vcc_DurationUnit(tl));
741 5818
                        ERRCHK(tl);
742 23488
                } else if (fmt == REAL || t->tok == FNUM) {
743 154
                        e1 = vcc_mk_expr(REAL, "%s%.3f", sign, t->num);
744 154
                } else {
745 17517
                        e1 = vcc_mk_expr(INT, "%s%.0f", sign, t->num);
746
                }
747
        }
748 23534
        e1->constant = EXPR_CONST;
749 23534
        *e = e1;
750 23537
}
751
752
/*--------------------------------------------------------------------
753
 * SYNTAX:
754
 *    Expr5:
755
 *      '(' ExprCor ')'
756
 *      symbol
757
 *      CNUM
758
 *      FNUM
759
 *      CSTR
760
 *      CBLOB
761
 */
762
763
static void
764 259629
vcc_expr5(struct vcc *tl, struct expr **e, vcc_type_t fmt)
765
{
766
        struct expr *e1, *e2;
767
        const char *ip, *sign;
768
        struct token *t, *t1;
769
        struct symbol *sym;
770
771 259629
        sign = "";
772 259629
        *e = NULL;
773 259629
        if (tl->t->tok == '(') {
774 2833
                SkipToken(tl, '(');
775 2833
                vcc_expr_cor(tl, &e2, fmt);
776 2833
                ERRCHK(tl);
777 2833
                SkipToken(tl, ')');
778 2833
                if (e2->fmt == STRINGS)
779 6
                        *e = e2;
780
                else
781 2827
                        *e = vcc_expr_edit(tl, e2->fmt, "(\v1)", e2, NULL);
782 2833
                return;
783
        }
784 256796
        switch (tl->t->tok) {
785
        case ID:
786 130539
                t = tl->t;
787 130539
                t1 = vcc_PeekToken(tl);
788 130539
                AN(t1);
789 130539
                sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_NONE,
790
                    SYMTAB_PARTIAL_NOERR, XREF_REF);
791 130539
                if (sym == NULL && fmt->global_pfx != NULL && t1->tok != '.') {
792 9
                        sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_NONE,
793
                            SYMTAB_CREATE, XREF_REF);
794 9
                        ERRCHK(tl);
795 7
                        AN(sym);
796 7
                        VCC_GlobalSymbol(sym, fmt);
797 7
                }
798 130537
                ERRCHK(tl);
799 130537
                if (sym == NULL)
800 12
                        AZ(VCC_SymbolGet(tl, SYM_MAIN, SYM_NONE,
801
                            SYMTAB_PARTIAL, XREF_REF));
802 130537
                ERRCHK(tl);
803 130525
                AN(sym);
804 130525
                if (sym->kind == SYM_INSTANCE) {
805 774
                        AZ(*e);
806 774
                        *e = vcc_new_expr(sym->type);
807 774
                        (*e)->instance = sym;
808 774
                        return;
809
                }
810 129751
                if (sym->kind == SYM_FUNC && sym->type == VOID) {
811 1
                        VSB_cat(tl->sb, "Function returns VOID:\n");
812 1
                        vcc_ErrWhere(tl, tl->t);
813 1
                        return;
814
                }
815 129750
                if (sym->eval != NULL) {
816 129747
                        AN(sym->eval);
817 129747
                        AZ(*e);
818 129747
                        sym->eval(tl, e, t, sym, fmt);
819 129747
                        if (tl->err) {
820 16
                                VSB_cat(tl->sb,
821
                                    "While compiling function call:\n\n");
822 16
                                vcc_ErrWhere2(tl, t, tl->t);
823 16
                        }
824 129747
                        ERRCHK(tl);
825
                        /* Unless asked for a HEADER, fold to string here */
826 129731
                        if (*e && fmt != HEADER && (*e)->fmt == HEADER) {
827 34507
                                vcc_expr_tostring(tl, e);
828 34507
                                ERRCHK(tl);
829 34507
                        }
830 129731
                        return;
831
                }
832 6
                VSB_printf(tl->sb,
833
                    "Symbol '%.*s' type (%s) cannot be used in expression.\n",
834 3
                    PF(t), sym->kind->name);
835 3
                vcc_ErrWhere(tl, t);
836 3
                if (sym->def_b != NULL) {
837 1
                        VSB_cat(tl->sb, "That symbol was defined here:\n");
838 1
                        vcc_ErrWhere(tl, sym->def_b);
839 1
                }
840 3
                return;
841
        case CSTR:
842 102705
                assert(fmt != VOID);
843 102705
                if (fmt == IP) {
844 30
                        if (*tl->t->dec == '/') {
845
                                /*
846
                                 * On some platforms (e.g. FreeBSD),
847
                                 * getaddrinfo(3) may resolve a path to a
848
                                 * sockaddr_un if it happens to exist and
849
                                 * is a socket. So don't let that happen.
850
                                 */
851 1
                                VSB_cat(tl->sb,
852
                                    "Cannot convert to an IP address: ");
853 1
                                vcc_ErrToken(tl, tl->t);
854 1
                                vcc_ErrWhere(tl, tl->t);
855 1
                                return;
856
                        }
857 58
                        Resolve_Sockaddr(tl, tl->t->dec, "80",
858
                            &ip, NULL, &ip, NULL, NULL, 1,
859 29
                            tl->t, "IP constant");
860 29
                        ERRCHK(tl);
861 26
                        e1 = vcc_mk_expr(IP, "%s", ip);
862 26
                        ERRCHK(tl);
863 102701
                } else if (fmt == REGEX) {
864 8506
                        e1 = vcc_new_expr(REGEX);
865 8506
                        vcc_regexp(tl, e1->vsb);
866 8506
                        AZ(VSB_finish(e1->vsb));
867 8506
                } else {
868 94169
                        e1 = vcc_new_expr(STRINGS);
869 94169
                        EncToken(e1->vsb, tl->t);
870 94169
                        AZ(VSB_finish(e1->vsb));
871 94169
                        e1->constant |= EXPR_STR_CONST;
872 94169
                        e1->nstr = 1;
873
                }
874 102701
                e1->t1 = tl->t;
875 102701
                e1->constant |= EXPR_CONST;
876 102701
                vcc_NextToken(tl);
877 102701
                *e = e1;
878 102701
                return;
879
        case '-':
880 62
                if (fmt != INT &&
881 16
                    fmt != REAL &&
882 4
                    fmt != DURATION &&
883 1
                    fmt != STRINGS)
884 0
                        break;
885 61
                vcc_NextToken(tl);
886 61
                if (tl->t->tok != FNUM && tl->t->tok != CNUM) {
887 9
                        vcc_expr_cor(tl, &e1, fmt);
888 9
                        ERRCHK(tl);
889 9
                        *e = vcc_expr_edit(tl, e1->fmt, "-(\v1)", e1, NULL);
890 9
                        return;
891
                }
892 52
                sign = "-";
893
                /* FALLTHROUGH */
894
        case FNUM:
895
        case CNUM:
896 23537
                vcc_number(tl, e, fmt, sign);
897 23537
                return;
898
        case CBLOB:
899 2
                e1 = vcc_new_expr(BLOB);
900 2
                VSB_printf(e1->vsb, "%s", tl->t->dec);
901 2
                AZ(VSB_finish(e1->vsb));
902 2
                e1->constant |= EXPR_STR_CONST;
903 2
                e1->t1 = tl->t;
904 2
                vcc_NextToken(tl);
905 2
                *e = e1;
906 2
                return;
907
        default:
908 4
                break;
909
        }
910 4
        VSB_cat(tl->sb, "Unknown token ");
911 4
        vcc_ErrToken(tl, tl->t);
912 4
        VSB_printf(tl->sb, " when looking for %s\n\n", vcc_utype(fmt));
913 4
        vcc_ErrWhere(tl, tl->t);
914 259629
}
915
916
/*--------------------------------------------------------------------
917
 * SYNTAX:
918
 *    Expr4:
919
 *      Expr5 [ '.' (type_attribute | type_method()) ]*
920
 */
921
922
void
923 2812
vcc_Eval_TypeMethod(struct vcc *tl, struct expr **e, struct token *t,
924
    struct symbol *sym, vcc_type_t fmt)
925
{
926
        const char *impl;
927
928 2812
        (void)t;
929 2812
        impl = VCC_Type_EvalMethod(tl, sym);
930 2812
        ERRCHK(tl);
931 2812
        AN(impl);
932 2812
        *e = vcc_expr_edit(tl, fmt, impl, *e, NULL);
933 2812
}
934
935
static void
936 259629
vcc_expr4(struct vcc *tl, struct expr **e, vcc_type_t fmt)
937
{
938
        struct symbol *sym;
939
940 259629
        *e = NULL;
941 259629
        vcc_expr5(tl, e, fmt);
942 259629
        ERRCHK(tl);
943 259581
        AN(*e);
944 263165
        while (tl->t->tok == '.') {
945 3587
                vcc_NextToken(tl);
946 3587
                ExpectErr(tl, ID);
947
948 3586
                sym = VCC_TypeSymbol(tl, SYM_METHOD, (*e)->fmt);
949 3586
                if (sym == NULL) {
950 2
                        VSB_cat(tl->sb, "Unknown property ");
951 2
                        vcc_ErrToken(tl, tl->t);
952 2
                        VSB_printf(tl->sb, " for type %s\n", (*e)->fmt->name);
953 2
                        vcc_ErrWhere(tl, tl->t);
954 2
                        return;
955
                }
956
957 3584
                AN(sym->eval);
958 3584
                sym->eval(tl, e, tl->t, sym, sym->type);
959 3584
                ERRCHK(tl);
960 3584
                if ((*e)->fmt == STRING) {
961 2801
                        (*e)->fmt = STRINGS;
962 2801
                        (*e)->nstr = 1;
963 2801
                }
964
        }
965 259629
}
966
967
/*--------------------------------------------------------------------
968
 * SYNTAX:
969
 *    ExprMul:
970
 *      Expr4 { {'*'|'/'|'%'} Expr4 } *
971
 */
972
973
static void
974 250751
vcc_expr_mul(struct vcc *tl, struct expr **e, vcc_type_t fmt)
975
{
976
        struct expr *e2;
977
        vcc_type_t f2;
978
        struct token *tk;
979
        char buf[24];
980
981 250751
        *e = NULL;
982 250751
        vcc_expr4(tl, e, fmt);
983 250751
        ERRCHK(tl);
984 250707
        AN(*e);
985
986 250744
        while (tl->t->tok == '*' || tl->t->tok == '/' || tl->t->tok == '%') {
987 41
                if (tl->t->tok == '%' && ((*e)->fmt != INT)) {
988 1
                        VSB_cat(tl->sb, "Operator % only possible on INT.\n");
989 1
                        vcc_ErrWhere(tl, tl->t);
990 1
                        return;
991
                }
992 40
                f2 = (*e)->fmt->multype;
993 40
                if (f2 == NULL) {
994 2
                        VSB_printf(tl->sb,
995
                            "Operator %.*s not possible on type %s.\n",
996 1
                            PF(tl->t), vcc_utype((*e)->fmt));
997 1
                        vcc_ErrWhere(tl, tl->t);
998 1
                        return;
999
                }
1000 39
                tk = tl->t;
1001 39
                vcc_NextToken(tl);
1002 39
                vcc_expr4(tl, &e2, f2);
1003 39
                ERRCHK(tl);
1004 39
                if (e2->fmt != INT && e2->fmt != f2) {
1005 4
                        VSB_printf(tl->sb, "%s %.*s %s not possible.\n",
1006 2
                            vcc_utype((*e)->fmt), PF(tk), vcc_utype(e2->fmt));
1007 2
                        vcc_ErrWhere(tl, tk);
1008 2
                        return;
1009
                }
1010 37
                bprintf(buf, "(\v1%c\v2)", tk->tok);
1011 37
                *e = vcc_expr_edit(tl, (*e)->fmt, buf, *e, e2);
1012
        }
1013 250751
}
1014
1015
/*--------------------------------------------------------------------
1016
 * SYNTAX:
1017
 *    ExprAdd:
1018
 *      ExprMul { {'+'|'-'} ExprMul } *
1019
 */
1020
1021
static const struct adds {
1022
        unsigned        op;
1023
        vcc_type_t      a;
1024
        vcc_type_t      b;
1025
        vcc_type_t      fmt;
1026
} vcc_adds[] = {
1027
        { '+', BYTES,           BYTES,          BYTES },
1028
        { '-', BYTES,           BYTES,          BYTES },
1029
        { '+', DURATION,        DURATION,       DURATION },
1030
        { '-', DURATION,        DURATION,       DURATION },
1031
        { '+', INT,             INT,            INT },
1032
        { '-', INT,             INT,            INT },
1033
        { '+', INT,             REAL,           REAL },
1034
        { '-', INT,             REAL,           REAL },
1035
        { '+', REAL,            INT,            REAL },
1036
        { '-', REAL,            INT,            REAL },
1037
        { '+', REAL,            REAL,           REAL },
1038
        { '-', REAL,            REAL,           REAL },
1039
        { '-', TIME,            TIME,           DURATION },
1040
        { '+', TIME,            DURATION,       TIME },
1041
        { '-', TIME,            DURATION,       TIME },
1042
1043
        { EOI, VOID,            VOID,           VOID }
1044
};
1045
1046
static void
1047 183204
vcc_expr_add(struct vcc *tl, struct expr **e, vcc_type_t fmt)
1048
{
1049
        const struct adds *ap;
1050
        struct expr  *e2;
1051
        struct token *tk;
1052
        int lit, n;
1053
1054 183204
        *e = NULL;
1055 183204
        vcc_expr_mul(tl, e, fmt);
1056 183204
        ERRCHK(tl);
1057
1058 250692
        while (tl->t->tok == '+' || tl->t->tok == '-') {
1059 67547
                tk = tl->t;
1060 1079998
                for (ap = vcc_adds; ap->op != EOI; ap++)
1061 1012526
                        if (tk->tok == ap->op && (*e)->fmt == ap->a)
1062 75
                                break;
1063 67547
                vcc_NextToken(tl);
1064 67547
                if (ap->op == EOI && fmt == STRINGS)
1065 504
                        vcc_expr_mul(tl, &e2, STRINGS);
1066
                else
1067 67043
                        vcc_expr_mul(tl, &e2, (*e)->fmt);
1068 67547
                ERRCHK(tl);
1069
1070 1080120
                for (ap = vcc_adds; ap->op != EOI; ap++)
1071 1012638
                        if (tk->tok == ap->op && (*e)->fmt == ap->a &&
1072 1012638
                            e2->fmt == ap->b)
1073 65
                                break;
1074
1075 67547
                if (ap->op == '+') {
1076 45
                        *e = vcc_expr_edit(tl, ap->fmt, "(\v1 + \v2)", *e, e2);
1077 67547
                } else if (ap->op == '-') {
1078 20
                        *e = vcc_expr_edit(tl, ap->fmt, "(\v1 - \v2)", *e, e2);
1079 67517
                } else if (tk->tok == '+' &&
1080 67481
                    ((*e)->fmt == STRINGS || fmt == STRINGS)) {
1081 67471
                        if ((*e)->fmt != STRINGS)
1082 5
                                vcc_expr_tostring(tl, e);
1083 67471
                        if (e2->fmt != STRINGS)
1084 16796
                                vcc_expr_tostring(tl, &e2);
1085 67471
                        if (vcc_islit(*e) && vcc_isconst(e2)) {
1086 48
                                lit = vcc_islit(e2);
1087 96
                                *e = vcc_expr_edit(tl, STRINGS,
1088 48
                                    "\v1\n\v2", *e, e2);
1089 48
                                (*e)->constant = EXPR_CONST;
1090 48
                                (*e)->nstr = 1;
1091 48
                                if (lit)
1092 48
                                        (*e)->constant |= EXPR_STR_CONST;
1093 48
                        } else {
1094 67423
                                n = (*e)->nstr + e2->nstr;
1095 134846
                                *e = vcc_expr_edit(tl, STRINGS,
1096 67423
                                    "\v1,\n\v2", *e, e2);
1097 67423
                                (*e)->constant = EXPR_VAR;
1098 67423
                                (*e)->nstr = n;
1099
                        }
1100 67471
                } else {
1101 22
                        VSB_printf(tl->sb, "%s %.*s %s not possible.\n",
1102 11
                            vcc_utype((*e)->fmt), PF(tk), vcc_utype(e2->fmt));
1103 11
                        vcc_ErrWhere2(tl, tk, tl->t);
1104 11
                        return;
1105
                }
1106
        }
1107 183204
}
1108
1109
/*--------------------------------------------------------------------
1110
 * SYNTAX:
1111
 *    ExprCmp:
1112
 *      ExprAdd
1113
 *      ExprAdd Relation ExprAdd
1114
 *      ExprAdd(STRING) '~' CString
1115
 *      ExprAdd(STRING) '!~' CString
1116
 *      ExprAdd(IP) '==' ExprAdd(IP)
1117
 *      ExprAdd(IP) '!=' ExprAdd(IP)
1118
 *      ExprAdd(IP) '~' ACL
1119
 *      ExprAdd(IP) '!~' ACL
1120
 */
1121
1122
struct cmps;
1123
1124
typedef void cmp_f(struct vcc *, struct expr **, const struct cmps *);
1125
1126
struct cmps {
1127
        vcc_type_t              fmt;
1128
        unsigned                token;
1129
        cmp_f                   *func;
1130
        const char              *emit;
1131
};
1132
1133
static void v_matchproto_(cmp_f)
1134 11301
cmp_simple(struct vcc *tl, struct expr **e, const struct cmps *cp)
1135
{
1136
        struct expr *e2;
1137
        struct token *tk;
1138
1139 11301
        tk = tl->t;
1140 11301
        vcc_NextToken(tl);
1141 11301
        vcc_expr_add(tl, &e2, (*e)->fmt);
1142 11301
        ERRCHK(tl);
1143
1144 11298
        if (e2->fmt != (*e)->fmt) {
1145 4
                VSB_printf(tl->sb,
1146
                    "Comparison of different types: %s '%.*s' %s\n",
1147 2
                    vcc_utype((*e)->fmt), PF(tk), vcc_utype(e2->fmt));
1148 2
                vcc_ErrWhere(tl, tk);
1149 2
        } else
1150 11296
                *e = vcc_expr_edit(tl, BOOL, cp->emit, *e, e2);
1151 11301
}
1152
1153
static void v_matchproto_(cmp_f)
1154 8461
cmp_regexp(struct vcc *tl, struct expr **e, const struct cmps *cp)
1155
{
1156
        struct token *t1;
1157
        struct expr *e2;
1158
        char buf[128];
1159
1160 8461
        *e = vcc_expr_edit(tl, STRING, "\vS", *e, NULL);
1161 8461
        vcc_NextToken(tl);
1162 8461
        t1 = tl->t;
1163 8461
        vcc_expr4(tl, &e2, REGEX);
1164 8461
        ERRCHK(tl);
1165 8457
        vcc_expr_typecheck(tl, &e2, REGEX, t1);
1166 8457
        ERRCHK(tl);
1167 8456
        bprintf(buf, "%sVRT_re_match(ctx, \v1, \v2)", cp->emit);
1168 8456
        *e = vcc_expr_edit(tl, BOOL, buf, *e, e2);
1169 8461
}
1170
1171
static void v_matchproto_(cmp_f)
1172 45
cmp_acl(struct vcc *tl, struct expr **e, const struct cmps *cp)
1173
{
1174
        struct token *t1;
1175
        struct expr *e2;
1176
        char buf[256];
1177
1178 45
        vcc_NextToken(tl);
1179 45
        t1 = tl->t;
1180 45
        vcc_expr4(tl, &e2, ACL);
1181 45
        ERRCHK(tl);
1182 42
        vcc_expr_typecheck(tl, &e2, ACL, t1);
1183 42
        ERRCHK(tl);
1184 39
        bprintf(buf, "%sVRT_acl_match(ctx, \v1, \v2)", cp->emit);
1185 39
        *e = vcc_expr_edit(tl, BOOL, buf, e2, *e);
1186 45
}
1187
1188
static void v_matchproto_(cmp_f)
1189 39535
cmp_string(struct vcc *tl, struct expr **e, const struct cmps *cp)
1190
{
1191
        struct expr *e2;
1192
        struct token *tk;
1193
        char buf[128];
1194
1195 39535
        tk = tl->t;
1196 39535
        vcc_NextToken(tl);
1197 39535
        vcc_expr_add(tl, &e2, STRINGS);
1198 39535
        ERRCHK(tl);
1199 39535
        if (vcc_stringstype(e2->fmt) != STRINGS) {
1200 6
                VSB_printf(tl->sb,
1201
                    "Comparison of different types: %s '%.*s' %s\n",
1202 3
                    vcc_utype((*e)->fmt), PF(tk), vcc_utype(e2->fmt));
1203 3
                vcc_ErrWhere(tl, tk);
1204 39535
        } else if ((*e)->nstr == 1 && e2->nstr == 1) {
1205 39501
                bprintf(buf, "(%s VRT_strcmp(\v1, \v2))", cp->emit);
1206 39501
                *e = vcc_expr_edit(tl, BOOL, buf, *e, e2);
1207 39501
        } else {
1208 31
                bprintf(buf, "(%s VRT_CompareStrands(\vT, \vt))", cp->emit);
1209 31
                *e = vcc_expr_edit(tl, BOOL, buf, *e, e2);
1210
        }
1211 39535
}
1212
1213
#define IDENT_REL(typ)                                                  \
1214
        {typ,           T_EQ,           cmp_simple, "(\v1 == \v2)" },   \
1215
        {typ,           T_NEQ,          cmp_simple, "(\v1 != \v2)" }
1216
1217
#define NUM_REL(typ)                                                    \
1218
        IDENT_REL(typ),                                                 \
1219
        {typ,           T_LEQ,          cmp_simple, "(\v1 <= \v2)" },   \
1220
        {typ,           T_GEQ,          cmp_simple, "(\v1 >= \v2)" },   \
1221
        {typ,           '<',            cmp_simple, "(\v1 < \v2)" },    \
1222
        {typ,           '>',            cmp_simple, "(\v1 > \v2)" }
1223
1224
static const struct cmps vcc_cmps[] = {
1225
        NUM_REL(INT),
1226
        NUM_REL(DURATION),
1227
        NUM_REL(BYTES),
1228
        NUM_REL(REAL),
1229
        NUM_REL(TIME),
1230
        IDENT_REL(BACKEND),
1231
        IDENT_REL(ACL),
1232
        IDENT_REL(PROBE),
1233
        IDENT_REL(STEVEDORE),
1234
        IDENT_REL(SUB),
1235
        IDENT_REL(INSTANCE),
1236
1237
        {BOOL,          T_EQ,           cmp_simple, "((!(\v1)) == (!(\v2)))" },
1238
        {BOOL,          T_NEQ,          cmp_simple, "((!(\v1)) != (!(\v2)))" },
1239
        {IP,            T_EQ,           cmp_simple, "!VRT_ipcmp(ctx, \v1, \v2)" },
1240
        {IP,            T_NEQ,          cmp_simple, "VRT_ipcmp(ctx, \v1, \v2)" },
1241
1242
        {IP,            '~',            cmp_acl, "" },
1243
        {IP,            T_NOMATCH,      cmp_acl, "!" },
1244
1245
        {STRINGS,       T_EQ,           cmp_string, "0 =="},
1246
        {STRINGS,       T_NEQ,          cmp_string, "0 !="},
1247
        {STRINGS,       '<',            cmp_string, "0 > "},
1248
        {STRINGS,       '>',            cmp_string, "0 < "},
1249
        {STRINGS,       T_LEQ,          cmp_string, "0 >="},
1250
        {STRINGS,       T_GEQ,          cmp_string, "0 <="},
1251
1252
        {STRINGS,       '~',            cmp_regexp, "" },
1253
        {STRINGS,       T_NOMATCH,      cmp_regexp, "!" },
1254
1255
        {VOID,          0,              NULL, NULL}
1256
};
1257
1258
#undef IDENT_REL
1259
#undef NUM_REL
1260
1261
static void
1262 132368
vcc_expr_cmp(struct vcc *tl, struct expr **e, vcc_type_t fmt)
1263
{
1264
        const struct cmps *cp;
1265
        struct token *tk;
1266
1267 132368
        *e = NULL;
1268 132368
        vcc_expr_add(tl, e, fmt);
1269 132368
        ERRCHK(tl);
1270 132312
        tk = tl->t;
1271
1272 6632206
        for (cp = vcc_cmps; cp->fmt != VOID; cp++) {
1273 6559236
                if (tl->t->tok != cp->token)
1274 5974626
                        continue;
1275 584610
                if (vcc_stringstype((*e)->fmt) != cp->fmt)
1276 525268
                        continue;
1277 59342
                AN(cp->func);
1278 59342
                cp->func(tl, e, cp);
1279 59342
                return;
1280
        }
1281
1282 72970
        switch (tk->tok) {
1283
        case T_EQ:
1284
        case T_NEQ:
1285
        case '<':
1286
        case T_LEQ:
1287
        case '>':
1288
        case T_GEQ:
1289
        case '~':
1290
        case T_NOMATCH:
1291 4
                VSB_printf(tl->sb, "Operator %.*s not possible on %s\n",
1292 2
                    PF(tl->t), vcc_utype((*e)->fmt));
1293 2
                vcc_ErrWhere(tl, tl->t);
1294 2
                return;
1295
        default:
1296 72968
                break;
1297
        }
1298 132368
}
1299
1300
/*--------------------------------------------------------------------
1301
 * SYNTAX:
1302
 *    ExprNot:
1303
 *      '!' ExprCmp
1304
 */
1305
1306
static void
1307 132368
vcc_expr_not(struct vcc *tl, struct expr **e, vcc_type_t fmt)
1308
{
1309
        struct token *tk;
1310
1311 132368
        *e = NULL;
1312 132368
        tk = tl->t;
1313 132368
        if (tl->t->tok == '!')
1314 5659
                vcc_NextToken(tl);
1315 132368
        vcc_expr_cmp(tl, e, fmt);
1316 132368
        ERRCHK(tl);
1317 132291
        if (tk->tok != '!')
1318 126633
                return;
1319 5658
        vcc_expr_tobool(tl, e);
1320 5658
        ERRCHK(tl);
1321 5658
        if ((*e)->fmt != BOOL) {
1322 1
                VSB_cat(tl->sb, "'!' must be followed by BOOL, found ");
1323 1
                VSB_printf(tl->sb, "%s.\n", vcc_utype((*e)->fmt));
1324 1
                vcc_ErrWhere2(tl, tk, tl->t);
1325 1
        } else {
1326 5657
                *e = vcc_expr_edit(tl, BOOL, "!(\v1)", *e, NULL);
1327
        }
1328 132368
}
1329
1330
/*--------------------------------------------------------------------
1331
 * CAND and COR are identical save for a few details, but they are
1332
 * stacked so handling them in the same function is not simpler.
1333
 * Instead have them both call this helper function to do everything.
1334
 */
1335
1336
typedef void upfunc(struct vcc *tl, struct expr **e, vcc_type_t fmt);
1337
1338
static void
1339 194935
vcc_expr_bin_bool(struct vcc *tl, struct expr **e, vcc_type_t fmt,
1340
    unsigned ourtok, upfunc *up, const char *tokstr)
1341
{
1342
        struct expr *e2;
1343
        struct token *tk;
1344
        char buf[32];
1345
1346 194935
        *e = NULL;
1347 194935
        tk = tl->t;
1348 194935
        up(tl, e, fmt);
1349 194935
        ERRCHK(tl);
1350 194777
        if (tl->t->tok != ourtok)
1351 178002
                return;
1352 16775
        vcc_expr_tobool(tl, e);
1353 16775
        ERRCHK(tl);
1354 16775
        if ((*e)->fmt != BOOL) {
1355 4
                VSB_printf(tl->sb,
1356
                    "'%s' must be preceded by BOOL,"
1357 2
                    " found %s.\n", tokstr, vcc_utype((*e)->fmt));
1358 2
                vcc_ErrWhere2(tl, tk, tl->t);
1359 2
                return;
1360
        }
1361 16773
        *e = vcc_expr_edit(tl, BOOL, "(\v+\n\v1", *e, NULL);
1362 53074
        while (tl->t->tok == ourtok) {
1363 36303
                vcc_NextToken(tl);
1364 36303
                tk = tl->t;
1365 36303
                up(tl, &e2, fmt);
1366 36303
                ERRCHK(tl);
1367 36303
                vcc_expr_tobool(tl, &e2);
1368 36303
                ERRCHK(tl);
1369 36303
                if (e2->fmt != BOOL) {
1370 4
                        VSB_printf(tl->sb,
1371
                            "'%s' must be followed by BOOL,"
1372 2
                            " found %s.\n", tokstr, vcc_utype(e2->fmt));
1373 2
                        vcc_ErrWhere2(tl, tk, tl->t);
1374 2
                        vcc_delete_expr(e2);
1375 2
                        return;
1376
                }
1377 36301
                bprintf(buf, "\v1\v-\n%s\v+\n\v2", tokstr);
1378 36301
                *e = vcc_expr_edit(tl, BOOL, buf, *e, e2);
1379
        }
1380 16771
        *e = vcc_expr_edit(tl, BOOL, "\v1\v-\n)", *e, NULL);
1381 194935
}
1382
1383
/*--------------------------------------------------------------------
1384
 * SYNTAX:
1385
 *    ExprCand:
1386
 *      ExprNot { '&&' ExprNot } *
1387
 */
1388
1389
static void
1390 98870
vcc_expr_cand(struct vcc *tl, struct expr **e, vcc_type_t fmt)
1391
{
1392
1393 98870
        vcc_expr_bin_bool(tl, e, fmt, T_CAND, vcc_expr_not, "&&");
1394 98870
}
1395
1396
/*--------------------------------------------------------------------
1397
 * SYNTAX:
1398
 *    ExprCOR:
1399
 *      ExprCand { '||' ExprCand } *
1400
 */
1401
1402
static void
1403 96065
vcc_expr_cor(struct vcc *tl, struct expr **e, vcc_type_t fmt)
1404
{
1405
1406 96065
        vcc_expr_bin_bool(tl, e, fmt, T_COR, vcc_expr_cand, "||");
1407 96065
}
1408
1409
/*--------------------------------------------------------------------
1410
 * This function is the entry-point for getting an expression with
1411
 * a particular type, ready for inclusion in the VGC.
1412
 */
1413
1414
static void
1415 93223
vcc_expr0(struct vcc *tl, struct expr **e, vcc_type_t fmt)
1416
{
1417
        struct token *t1;
1418
1419 93223
        assert(fmt != VOID);
1420 93223
        assert(fmt != STRINGS);
1421 93223
        *e = NULL;
1422 93223
        t1 = tl->t;
1423 93223
        if (fmt->stringform)
1424 28623
                vcc_expr_cor(tl, e, STRINGS);
1425
        else
1426 64600
                vcc_expr_cor(tl, e, fmt);
1427 93223
        ERRCHK(tl);
1428
1429 93141
        if ((*e)->fmt == fmt)
1430 47696
                return;
1431
1432 45445
        if ((*e)->fmt != STRINGS && fmt->stringform)
1433 3426
                vcc_expr_tostring(tl, e);
1434
1435 45445
        if ((*e)->fmt->stringform) {
1436 0
                VSB_printf(tl->sb, "Cannot convert type %s(%s) to %s(%s)\n",
1437 0
                    vcc_utype((*e)->fmt), (*e)->fmt->name,
1438 0
                    vcc_utype(fmt), fmt->name);
1439 0
                vcc_ErrWhere2(tl, t1, tl->t);
1440 0
                return;
1441
        }
1442
1443 45445
        if (fmt == BODY && !(*e)->fmt->bodyform)
1444 1
                vcc_expr_tostring(tl, e);
1445
1446 45445
        if (fmt == BODY && (*e)->fmt->bodyform) {
1447 5604
                if ((*e)->fmt == STRINGS)
1448 5600
                        *e = vcc_expr_edit(tl, BODY, "STRING, 0, \vT", *e, NULL);
1449 4
                else if ((*e)->fmt == BLOB)
1450 4
                        *e = vcc_expr_edit(tl, BODY, "BLOB, 0, \v1", *e, NULL);
1451
                else
1452 0
                        WRONG("Unhandled bodyform");
1453 5604
        }
1454
1455 45445
        if ((*e)->fmt == STRINGS && fmt->stringform) {
1456 28590
                if (fmt == STRING)
1457 3557
                        *e = vcc_expr_edit(tl, STRING, "\vS", *e, NULL);
1458 25033
                else if (fmt == STRANDS)
1459 25033
                        *e = vcc_expr_edit(tl, STRANDS, "\vT", (*e), NULL);
1460
                else
1461 0
                        WRONG("Unhandled stringform");
1462 28590
        }
1463
1464 45445
        if (fmt == BOOL) {
1465 11247
                vcc_expr_tobool(tl, e);
1466 11247
                ERRCHK(tl);
1467 11247
        }
1468
1469 45445
        vcc_expr_typecheck(tl, e, fmt, t1);
1470 93223
}
1471
1472
static void
1473 53944
vcc_expr_typecheck(struct vcc *tl, struct expr **e, vcc_type_t fmt,
1474
    struct token *t1)
1475
{
1476
1477 53944
        assert(fmt != VOID);
1478 53944
        assert(fmt != STRINGS);
1479
1480 53944
        if (fmt != (*e)->fmt)  {
1481 18
                VSB_printf(tl->sb, "Expression has type %s, expected %s\n",
1482 9
                    vcc_utype((*e)->fmt), vcc_utype(fmt));
1483 9
                vcc_ErrWhere2(tl, t1, tl->t);
1484 9
        }
1485 53944
}
1486
1487
/*--------------------------------------------------------------------
1488
 * This function parses and emits the C-code to evaluate an expression
1489
 *
1490
 * We know up front what kind of type we want the expression to be,
1491
 * and this function is the backstop if that doesn't succeed.
1492
 */
1493
1494
void
1495 90475
vcc_Expr(struct vcc *tl, vcc_type_t fmt)
1496
{
1497 90475
        struct expr *e = NULL;
1498
1499 90475
        assert(fmt != VOID);
1500 90475
        assert(fmt != STRINGS);
1501 90475
        vcc_expr0(tl, &e, fmt);
1502 90475
        ERRCHK(tl);
1503 90397
        assert(e->fmt == fmt);
1504
1505 90397
        vcc_expr_fmt(tl->fb, tl->indent, e);
1506 90397
        VSB_cat(tl->fb, "\n");
1507 90397
        vcc_delete_expr(e);
1508 90475
}
1509
1510
/*--------------------------------------------------------------------
1511
 */
1512
1513
void v_matchproto_(sym_act_f)
1514 472
vcc_Act_Call(struct vcc *tl, struct token *t, struct symbol *sym)
1515
{
1516
1517
        struct expr *e;
1518
1519 472
        e = NULL;
1520 472
        vcc_func(tl, &e, sym->eval_priv, sym->extra, sym);
1521 472
        if (!tl->err) {
1522 467
                vcc_expr_fmt(tl->fb, tl->indent, e);
1523 467
                SkipToken(tl, ';');
1524 467
                VSB_cat(tl->fb, ";\n");
1525 472
        } else if (t != tl->t) {
1526 5
                VSB_cat(tl->sb, "While compiling function call:\n\n");
1527 5
                vcc_ErrWhere2(tl, t, tl->t);
1528 5
        }
1529 472
        vcc_delete_expr(e);
1530 472
}
1531
1532
void v_matchproto_(sym_act_f)
1533 334
vcc_Act_Obj(struct vcc *tl, struct token *t, struct symbol *sym)
1534
{
1535
1536 334
        struct expr *e = NULL;
1537
1538 334
        assert(sym->kind == SYM_INSTANCE);
1539 334
        ExpectErr(tl, '.');
1540 333
        tl->t = t;
1541 333
        vcc_expr4(tl, &e, sym->type);
1542 333
        ERRCHK(tl);
1543 333
        vcc_expr_fmt(tl->fb, tl->indent, e);
1544 333
        vcc_delete_expr(e);
1545 333
        SkipToken(tl, ';');
1546 333
        VSB_cat(tl->fb, ";\n");
1547 334
}
1548
1549
/*--------------------------------------------------------------------
1550
 */
1551
1552
static void v_matchproto_(sym_expr_t)
1553 37
vcc_Eval_Regsub(struct vcc *tl, struct expr **e, struct token *t,
1554
    struct symbol *sym, vcc_type_t fmt)
1555
{
1556
        struct expr *e2, *e3;
1557 37
        int all = sym->eval_priv == NULL ? 0 : 1;
1558
        char buf[128];
1559
1560 37
        (void)t;
1561 37
        (void)fmt;
1562 37
        SkipToken(tl, '(');
1563 37
        vcc_expr0(tl, &e2, STRING);
1564 37
        ERRCHK(tl);
1565 37
        SkipToken(tl, ',');
1566 37
        vcc_expr0(tl, &e3, REGEX);
1567 37
        ERRCHK(tl);
1568
1569 36
        bprintf(buf, "VRT_regsub(ctx, %d,\v+\n\v1,\n\v2", all);
1570 36
        *e = vcc_expr_edit(tl, STRING, buf, e2, e3);
1571 36
        SkipToken(tl, ',');
1572 36
        vcc_expr0(tl, &e2, STRING);
1573 36
        ERRCHK(tl);
1574 36
        *e = vcc_expr_edit(tl, STRINGS, "\v1,\n\v2)\v-", *e, e2);
1575 36
        (*e)->nstr = 1;
1576 36
        SkipToken(tl, ')');
1577 37
}
1578
1579
/*--------------------------------------------------------------------
1580
 */
1581
1582
static void v_matchproto_(sym_expr_t)
1583 3037
vcc_Eval_BoolConst(struct vcc *tl, struct expr **e, struct token *t,
1584
    struct symbol *sym, vcc_type_t fmt)
1585
{
1586
1587 3037
        (void)t;
1588 3037
        (void)tl;
1589 3037
        (void)fmt;
1590 3037
        *e = vcc_mk_expr(BOOL, "(0==%d)", sym->eval_priv == NULL ? 1 : 0);
1591 3037
        (*e)->constant = EXPR_CONST;
1592 3037
}
1593
1594
/*--------------------------------------------------------------------
1595
 */
1596
1597
static void v_matchproto_(sym_expr_t)
1598 4
vcc_Eval_Default(struct vcc *tl, struct expr **e, struct token *t,
1599
    struct symbol *sym, vcc_type_t fmt)
1600
{
1601 4
        (void)e;
1602 4
        (void)fmt;
1603 4
        (void)sym;
1604 4
        (void)t;
1605
1606 4
        if (fmt->default_sym == NULL) {
1607 2
                VSB_cat(tl->sb, "Symbol 'default' is a reserved word.\n");
1608 2
                vcc_ErrWhere(tl, t);
1609 2
                return;
1610
        }
1611
1612 2
        *e = vcc_mk_expr(fmt, "%s", fmt->default_sym->rname);
1613 4
}
1614
1615
/*--------------------------------------------------------------------
1616
 */
1617
1618
void
1619 3025
vcc_Expr_Init(struct vcc *tl)
1620
{
1621
        struct symbol *sym;
1622
1623 3025
        sym = VCC_MkSym(tl, "regsub", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH);
1624 3025
        AN(sym);
1625 3025
        sym->type = STRING;
1626 3025
        sym->eval = vcc_Eval_Regsub;
1627 3025
        sym->eval_priv = NULL;
1628
1629 3025
        sym = VCC_MkSym(tl, "regsuball", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH);
1630 3025
        AN(sym);
1631 3025
        sym->type = STRING;
1632 3025
        sym->eval = vcc_Eval_Regsub;
1633 3025
        sym->eval_priv = sym;
1634
1635 3025
        sym = VCC_MkSym(tl, "true", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH);
1636 3025
        AN(sym);
1637 3025
        sym->type = BOOL;
1638 3025
        sym->eval = vcc_Eval_BoolConst;
1639 3025
        sym->eval_priv = sym;
1640
1641 3025
        sym = VCC_MkSym(tl, "false", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH);
1642 3025
        AN(sym);
1643 3025
        sym->type = BOOL;
1644 3025
        sym->eval = vcc_Eval_BoolConst;
1645 3025
        sym->eval_priv = NULL;
1646
1647 3025
        sym = VCC_MkSym(tl, "default", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH);
1648 3025
        AN(sym);
1649 3025
        sym->type = DEFAULT;
1650 3025
        sym->eval = vcc_Eval_Default;
1651 3025
}