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 11676
vcc_isconst(const struct expr *e)
63
{
64 11676
        AN(e->constant);
65 11676
        return (e->constant & EXPR_CONST);
66
}
67
68
static inline int
69 137686
vcc_islit(const struct expr *e)
70
{
71 137686
        AN(e->constant);
72 137686
        return (e->constant & EXPR_STR_CONST);
73
}
74
75
static const char *
76 134
vcc_utype(vcc_type_t t)
77
{
78 134
        if (t == STRINGS || t->stringform)
79 36
                t = STRING;
80 134
        return (t->name);
81
}
82
83
static vcc_type_t
84 1193038
vcc_stringstype(vcc_type_t t)
85
{
86 1193038
        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 1345434
vcc_new_expr(vcc_type_t fmt)
97
{
98
        struct expr *e;
99
100 1345434
        ALLOC_OBJ(e, EXPR_MAGIC);
101 1345434
        AN(e);
102 1345434
        e->vsb = VSB_new_auto();
103 1345434
        e->fmt = fmt;
104 1345434
        e->constant = EXPR_VAR;
105 1345434
        return (e);
106
}
107
108
static struct expr * v_printflike_(2, 3)
109 360430
vcc_mk_expr(vcc_type_t fmt, const char *str, ...)
110
{
111
        va_list ap;
112
        struct expr *e;
113
114 360430
        e = vcc_new_expr(fmt);
115 360430
        va_start(ap, str);
116 360430
        VSB_vprintf(e->vsb, str, ap);
117 360430
        va_end(ap);
118 360430
        AZ(VSB_finish(e->vsb));
119 360430
        return (e);
120
}
121
122
static void
123 1751666
vcc_delete_expr(struct expr *e)
124
{
125 1751666
        if (e == NULL)
126 407940
                return;
127 1343726
        CHECK_OBJ(e, EXPR_MAGIC);
128 1343726
        VSB_destroy(&e->vsb);
129 1343726
        FREE_OBJ(e);
130 1751666
}
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 119758
vcc_strands_edit(const struct expr *e1, const struct expr *e2)
159
{
160
161 119758
        assert(e2->fmt == STRANDS || e2->fmt == STRINGS);
162
163 119758
        if (e2->fmt == STRANDS)
164 8
                VSB_cat(e1->vsb, VSB_data(e2->vsb));
165 119750
        else if (e2->nstr == 0)
166 0
                VSB_printf(e1->vsb, "vrt_null_strands");
167 119750
        else if (e2->nstr == 1)
168 107936
                VSB_printf(e1->vsb, "TOSTRAND(%s)", VSB_data(e2->vsb));
169
        else {
170 23628
                VSB_printf(e1->vsb, "TOSTRANDS(%d,\v+\n%s\v-)",
171 11814
                   e2->nstr, VSB_data(e2->vsb));
172
        }
173 119758
}
174
175
static struct expr *
176 757026
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 757026
        int nl = 1;
181
182 757026
        (void) tl;
183
184 757026
        AN(e1);
185 757026
        e = vcc_new_expr(fmt);
186 8571824
        while (*p != '\0') {
187 7814798
                if (*p != '\v') {
188 6462978
                        if (*p != '\n' || !nl)
189 6462878
                                VSB_putc(e->vsb, *p);
190 6462978
                        nl = (*p == '\n');
191 6462978
                        p++;
192 6462978
                        continue;
193
                }
194 1351820
                assert(*p == '\v');
195 1351820
                switch (*++p) {
196 119690
                case '+': VSB_cat(e->vsb, "\v+"); nl = 0; break;
197 126008
                case '-': VSB_cat(e->vsb, "\v-"); nl = 0; break;
198
                case 'S':
199
                case 's':
200 41568
                        e3 = (*p == 'S' ? e1 : e2);
201 41568
                        AN(e3);
202 41568
                        assert(e1->fmt == STRINGS);
203 41568
                        if (e3->nstr > 1) {
204 52
                                VSB_cat(e->vsb,
205
                                    "\nVRT_STRANDS_string(ctx,\v+\n");
206 52
                                vcc_strands_edit(e, e3);
207 52
                                VSB_cat(e->vsb,
208
                                    "\v-\n)\n");
209 52
                        } else {
210 41516
                                VSB_cat(e->vsb, VSB_data(e3->vsb));
211
                        }
212 41568
                        break;
213
                case 'T':
214
                case 't':
215 119706
                        e3 = (*p == 'T' ? e1 : e2);
216 119706
                        AN(e3);
217 119706
                        vcc_strands_edit(e, e3);
218 119706
                        break;
219
                case '1':
220 595814
                        VSB_cat(e->vsb, VSB_data(e1->vsb));
221 595814
                        break;
222
                case '2':
223 349034
                        AN(e2);
224 349034
                        VSB_cat(e->vsb, VSB_data(e2->vsb));
225 349034
                        break;
226
                default:
227 0
                        WRONG("Illegal edit in VCC expression");
228 0
                }
229 1351820
                p++;
230
        }
231 757026
        AZ(VSB_finish(e->vsb));
232 757026
        e->t1 = e1->t1;
233 757026
        e->t2 = e1->t2;
234 757026
        if (e2 != NULL)
235 349096
                e->t2 = e2->t2;
236 757026
        vcc_delete_expr(e1);
237 757026
        vcc_delete_expr(e2);
238 757026
        return (e);
239
}
240
241
/*--------------------------------------------------------------------
242
 * Expand finished expression into C-source code
243
 */
244
245
static void
246 237600
vcc_expr_fmt(struct vsb *d, int ind, const struct expr *e1)
247
{
248
        char *p;
249
        int i;
250
251 237600
        if (!e1->fmt->noindent) {
252 2278572
                for (i = 0; i < ind; i++)
253 2052404
                        VSB_putc(d, ' ');
254 226168
        }
255 237600
        p = VSB_data(e1->vsb);
256 20104938
        while (*p != '\0') {
257 19867346
                if (*p == '\n') {
258 570774
                        VSB_putc(d, '\n');
259 570774
                        if (*++p == '\0')
260 8
                                break;
261 6100242
                        for (i = 0; i < ind; i++)
262 5529476
                                VSB_putc(d, ' ');
263 19867338
                } else if (*p != '\v') {
264 19020856
                        VSB_putc(d, *p++);
265 19020856
                } else {
266 275716
                        switch (*++p) {
267 137858
                        case '+': ind += INDENT; break;
268 137858
                        case '-': ind -= INDENT; break;
269 0
                        default:  WRONG("Illegal format in VCC expression");
270 0
                        }
271 275716
                        p++;
272
                }
273
        }
274 237600
}
275
276
/*--------------------------------------------------------------------
277
 */
278
279
static void
280 171180
vcc_expr_tobool(struct vcc *tl, struct expr **e)
281
{
282
283 171180
        if ((*e)->fmt == BOOL)
284 125406
                return;
285 45774
        if ((*e)->fmt == BACKEND || (*e)->fmt == INT)
286 20
                *e = vcc_expr_edit(tl, BOOL, "(\v1 != 0)", *e, NULL);
287 45754
        else if ((*e)->fmt == DURATION)
288 4
                *e = vcc_expr_edit(tl, BOOL, "(\v1 > 0)", *e, NULL);
289 45750
        else if ((*e)->fmt == STRINGS)
290 45738
                *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 171180
}
297
298
/*--------------------------------------------------------------------
299
 */
300
301
static void
302 123008
vcc_expr_tostring(struct vcc *tl, struct expr **e)
303
{
304
        const char *p;
305 123008
        uint8_t constant = EXPR_VAR;
306
307 123008
        CHECK_OBJ_NOTNULL(*e, EXPR_MAGIC);
308 123008
        assert((*e)->fmt != STRINGS);
309
310 123008
        p = (*e)->fmt->tostring;
311 123008
        if (p != NULL) {
312 123006
                AN(*p);
313 123006
                *e = vcc_expr_edit(tl, STRINGS, p, *e, NULL);
314 123006
                (*e)->constant = constant;
315 123006
                (*e)->nstr = 1;
316 123006
        } else {
317 4
                VSB_printf(tl->sb,
318
                    "Cannot convert %s to STRING.\n",
319 2
                    vcc_utype((*e)->fmt));
320 2
                vcc_ErrWhere2(tl, (*e)->t1, tl->t);
321
        }
322 123008
}
323
324
/*--------------------------------------------------------------------
325
 */
326
327
void v_matchproto_(sym_expr_t)
328 832
vcc_Eval_Handle(struct vcc *tl, struct expr **e, struct token *t,
329
    struct symbol *sym, vcc_type_t type)
330
{
331
332 832
        (void)t;
333 832
        (void)tl;
334 832
        AN(sym->rname);
335 832
        AZ(type->stringform);
336
337 842
        if (sym->type->tostring == NULL &&
338 10
            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 832
                *e = vcc_mk_expr(sym->type, "%s", sym->rname);
344 832
                (*e)->constant = EXPR_VAR;
345 832
                (*e)->nstr = 1;
346 832
                if ((*e)->fmt == STRING)
347 0
                        (*e)->fmt = STRINGS;
348
        }
349 832
}
350
351
void v_matchproto_(sym_expr_t)
352 46
vcc_Eval_Sub(struct vcc *tl, struct expr **e, struct token *t,
353
    struct symbol *sym, vcc_type_t type)
354
{
355
356 46
        (void)t;
357 46
        (void)tl;
358 46
        AN(sym->rname);
359 46
        AZ(type->stringform);
360
361 46
        assert (sym->type == SUB);
362
363 46
        if (type == SUB) {
364 40
                *e = vcc_mk_expr(sym->type, "%s", sym->rname);
365 40
                (*e)->constant = EXPR_CONST;
366 40
                return;
367
        }
368
369 12
        VSB_printf(tl->sb, "Symbol '%s' can only be used as a %s expression\n",
370 6
            sym->name, sym->type->name);
371 6
        vcc_ErrWhere(tl, tl->t);
372 46
}
373
374
/*--------------------------------------------------------------------
375
 */
376
377
void v_matchproto_(sym_expr_t)
378 277544
vcc_Eval_Var(struct vcc *tl, struct expr **e, struct token *t,
379
    struct symbol *sym, vcc_type_t type)
380
{
381
382 277544
        (void)type;
383 277544
        vcc_AddUses(tl, t, NULL, sym, XREF_READ);
384 277544
        ERRCHK(tl);
385 277544
        *e = vcc_mk_expr(sym->type, "%s", sym->rname);
386 277544
        (*e)->constant = EXPR_VAR;
387 277544
        (*e)->nstr = 1;
388 277544
        if ((*e)->fmt == STRING)
389 109248
                (*e)->fmt = STRINGS;
390 277544
}
391
392
void v_matchproto_(sym_expr_t)
393 22
vcc_Eval_ProtectedHeader(struct vcc *tl, struct expr **e, struct token *t,
394
    struct symbol *sym, vcc_type_t type)
395
{
396
397 22
        AN(sym);
398 22
        AZ(sym->lorev);
399
400 22
        vcc_Header_Fh(tl, sym);
401 22
        sym->eval = vcc_Eval_Var;
402 22
        vcc_Eval_Var(tl, e, t, sym, type);
403 22
}
404
405
/*--------------------------------------------------------------------
406
 */
407
408
static struct expr *
409 344
vcc_priv_arg(struct vcc *tl, const char *p, struct symbol *sym)
410
{
411
        char buf[64];
412
        struct inifin *ifp;
413 344
        const char *f = NULL;
414
415 344
        AN(sym);
416 344
        AN(sym->vmod_name);
417
418 344
        if (!strcmp(p, "PRIV_VCL"))
419 30
                return (vcc_mk_expr(VOID, "&vmod_priv_%s", sym->vmod_name));
420
421 314
        if (!strcmp(p, "PRIV_CALL")) {
422 44
                bprintf(buf, "vmod_priv_%u", tl->unique++);
423 44
                ifp = New_IniFin(tl);
424 44
                Fh(tl, 0, "static struct vmod_priv %s;\n", buf);
425 44
                VSB_printf(ifp->fin, "\tVRT_priv_fini(ctx, &%s);", buf);
426 44
                return (vcc_mk_expr(VOID, "&%s", buf));
427
        }
428
429 270
        if (!strcmp(p, "PRIV_TASK"))
430 254
                f = "task";
431 16
        else if (!strcmp(p, "PRIV_TOP")) {
432 16
                f = "top";
433 16
                sym->r_methods &= VCL_MET_TASK_C;
434 16
        } else {
435 0
                WRONG("Wrong PRIV_ type");
436
        }
437 270
        AN(f);
438
439 270
        return (vcc_mk_expr(VOID, "VRT_priv_%s(ctx, &VGC_vmod_%s)",
440 270
            f, sym->vmod_name));
441 344
}
442
443
struct func_arg {
444
        vcc_type_t              type;
445
        const struct vjsn_val   *enums;
446
        const char              *cname;
447
        const char              *name;
448
        const char              *val;
449
        struct expr             *result;
450
        int                     avail;
451
        int                     optional;
452
        VTAILQ_ENTRY(func_arg)  list;
453
};
454
455
static struct expr *
456 3638
vcc_do_enum(struct vcc *tl, const char *cfunc, int len, const char *ptr)
457
{
458
        const char *r;
459
460 3638
        (void)tl;
461 3638
        r = strchr(cfunc, '.');
462 3638
        AN(r);
463 3638
        return (vcc_mk_expr(VOID, "*%.*s.enum_%.*s",
464 3638
            (int)(r - cfunc), cfunc, len, ptr));
465
}
466
467
static void
468 7978
vcc_do_arg(struct vcc *tl, const char *cfunc, struct func_arg *fa)
469
{
470
        struct expr *e2;
471
        struct vjsn_val *vv;
472
473 7978
        if (fa->type == ENUM) {
474 2674
                ExpectErr(tl, ID);
475 2674
                ERRCHK(tl);
476 9142
                VTAILQ_FOREACH(vv, &fa->enums->children, list)
477 9138
                        if (vcc_IdIs(tl->t, vv->value))
478 2670
                                break;
479 2674
                if (vv == NULL) {
480 4
                        VSB_cat(tl->sb, "Wrong enum value.");
481 4
                        VSB_cat(tl->sb, "  Expected one of:\n");
482 20
                        VTAILQ_FOREACH(vv, &fa->enums->children, list)
483 16
                                VSB_printf(tl->sb, "\t%s\n", vv->value);
484 4
                        vcc_ErrWhere(tl, tl->t);
485 4
                        return;
486
                }
487 2670
                fa->result = vcc_do_enum(tl, cfunc, PF(tl->t));
488 2670
                SkipToken(tl, ID);
489 2670
        } else {
490 5304
                if (fa->type == SUB)
491 40
                        tl->subref++;
492 5304
                vcc_expr0(tl, &e2, fa->type);
493 5304
                ERRCHK(tl);
494 5288
                assert(e2->fmt == fa->type);
495 5288
                fa->result = e2;
496
        }
497 7958
        fa->avail = 1;
498 7978
}
499
500
static void
501 5530
vcc_func(struct vcc *tl, struct expr **e, const void *priv,
502
    const char *extra, struct symbol *sym)
503
{
504
        vcc_type_t rfmt;
505
        const char *cfunc;
506
        struct expr *e1;
507
        struct func_arg *fa, *fa2;
508
        VTAILQ_HEAD(,func_arg) head;
509
        struct token *tf, *t1;
510
        const struct vjsn_val *vv, *vvp;
511
        const char *sa, *extra_sep;
512
        char ssa[64];
513
        int n;
514
515 5530
        CAST_OBJ_NOTNULL(vv, priv, VJSN_VAL_MAGIC);
516 5530
        assert(vjsn_is_array(vv));
517 5530
        vv = VTAILQ_FIRST(&vv->children);
518 5530
        rfmt = VCC_Type(VTAILQ_FIRST(&vv->children)->value);
519 5530
        AN(rfmt);
520 5530
        vv = VTAILQ_NEXT(vv, list);
521 5530
        cfunc = vv->value;
522 5530
        vv = VTAILQ_NEXT(vv, list);
523 5530
        sa = vv->value;
524 5530
        if (*sa == '\0') {
525 4702
                sa = NULL;
526 4702
        }
527 5530
        vv = VTAILQ_NEXT(vv, list);
528 5530
        if (sym->kind == SYM_METHOD) {
529 1548
                if (*e == NULL) {
530 4
                        VSB_cat(tl->sb, "Syntax error.");
531 4
                        tl->err = 1;
532 4
                        return;
533
                }
534 1544
                vcc_NextToken(tl);
535 1544
                AZ(extra);
536 1544
                AN((*e)->instance);
537 1544
                extra = (*e)->instance->rname;
538 1544
        }
539 5526
        tf = VTAILQ_PREV(tl->t, tokenhead, list);
540 5526
        SkipToken(tl, '(');
541 5526
        if (extra == NULL) {
542 3580
                extra = "";
543 3580
                extra_sep = "";
544 3580
        } else {
545 1946
                AN(*extra);
546 1946
                extra_sep = ", ";
547
        }
548 5526
        VTAILQ_INIT(&head);
549 18956
        for (;vv != NULL; vv = VTAILQ_NEXT(vv, list)) {
550 13430
                assert(vjsn_is_array(vv));
551 13430
                fa = calloc(1, sizeof *fa);
552 13430
                AN(fa);
553 13430
                VTAILQ_INSERT_TAIL(&head, fa, list);
554
555 13430
                vvp = VTAILQ_FIRST(&vv->children);
556 13430
                if (!memcmp(vvp->value, "PRIV_", 5)) {
557 344
                        fa->result = vcc_priv_arg(tl, vvp->value, sym);
558 344
                        vvp = VTAILQ_NEXT(vvp, list);
559 344
                        if (vvp != NULL)
560 344
                                fa->cname = fa->name = vvp->value;
561 344
                        continue;
562
                }
563 13086
                fa->type = VCC_Type(vvp->value);
564 13086
                AN(fa->type);
565 13086
                vvp = VTAILQ_NEXT(vvp, list);
566 13086
                if (vvp != NULL) {
567 13086
                        fa->name = vvp->value;
568 13086
                        vvp = VTAILQ_NEXT(vvp, list);
569 13086
                        AN(vvp); /* vmod_syntax 2.0 */
570 13086
                        fa->cname = vvp->value;
571 13086
                        vvp = VTAILQ_NEXT(vvp, list);
572 13086
                        if (vvp != NULL) {
573 9156
                                fa->val = vvp->value;
574 9156
                                vvp = VTAILQ_NEXT(vvp, list);
575 9156
                                if (vvp != NULL) {
576 7522
                                        fa->enums = vvp;
577 7522
                                        vvp = VTAILQ_NEXT(vvp, list);
578 7522
                                }
579 9156
                        }
580 13086
                }
581 13086
                if (sa != NULL && vvp != NULL && vjsn_is_true(vvp)) {
582 4130
                        fa->optional = 1;
583 4130
                        vvp = VTAILQ_NEXT(vvp, list);
584 4130
                }
585 13086
                AZ(vvp);
586 13086
        }
587
588 8880
        VTAILQ_FOREACH(fa, &head, list) {
589 7964
                if (tl->t->tok == ')')
590 178
                        break;
591 7786
                if (fa->result != NULL)
592 246
                        continue;
593 7540
                if (tl->t->tok == ID) {
594 5548
                        t1 = VTAILQ_NEXT(tl->t, list);
595 5548
                        if (t1->tok == '=')
596 1520
                                break;
597 4028
                }
598 6020
                vcc_do_arg(tl, cfunc, fa);
599 6020
                if (tl->err)
600 40
                        VSB_printf(tl->sb, "Expected argument: %s %s\n\n",
601 20
                            fa->type->name,
602 20
                            fa->name ? fa->name : "(unnamed argument)");
603 6020
                ERRCHK(tl);
604 6000
                if (tl->t->tok == ')')
605 2892
                        break;
606 3108
                SkipToken(tl, ',');
607 3108
        }
608 5948
        while (tl->t->tok == ID) {
609 6448
                VTAILQ_FOREACH(fa, &head, list) {
610 6446
                        if (fa->name == NULL)
611 24
                                continue;
612 6422
                        if (vcc_IdIs(tl->t, fa->name))
613 1960
                                break;
614 4462
                }
615 1962
                if (fa == NULL) {
616 4
                        VSB_printf(tl->sb, "Unknown argument '%.*s'\n",
617 2
                            PF(tl->t));
618 2
                        vcc_ErrWhere(tl, tl->t);
619 2
                        return;
620
                }
621 1960
                if (fa->result != NULL) {
622 2
                        AN(fa->name);
623 4
                        VSB_printf(tl->sb, "Argument '%s' already used\n",
624 2
                            fa->name);
625 2
                        vcc_ErrWhere(tl, tl->t);
626 2
                        return;
627
                }
628 1958
                vcc_NextToken(tl);
629 1958
                SkipToken(tl, '=');
630 1958
                vcc_do_arg(tl, cfunc, fa);
631 1958
                ERRCHK(tl);
632 1958
                if (tl->t->tok == ')')
633 1516
                        break;
634 442
                SkipToken(tl, ',');
635
        }
636
637 5502
        if (sa != NULL)
638 1640
                e1 = vcc_mk_expr(rfmt, "%s(ctx%s%s,\v+\n&(%s)\v+ {\n",
639 820
                    cfunc, extra_sep, extra, sa);
640
        else
641 9364
                e1 = vcc_mk_expr(rfmt, "%s(ctx%s%s\v+",
642 4682
                    cfunc, extra_sep, extra);
643 5502
        n = 0;
644 18790
        VTAILQ_FOREACH_SAFE(fa, &head, list, fa2) {
645 13288
                n++;
646 13288
                if (fa->optional) {
647 4042
                        AN(fa->cname);
648 4042
                        bprintf(ssa, "\v1.valid_%s = %d,\n",
649
                            fa->cname, fa->avail);
650 4042
                        e1 = vcc_expr_edit(tl, e1->fmt, ssa, e1, NULL);
651 4042
                }
652 13288
                if (fa->result == NULL && fa->type == ENUM && fa->val != NULL)
653 968
                        fa->result = vcc_do_enum(tl, cfunc, strlen(fa->val), fa->val);
654 13288
                if (fa->result == NULL && fa->val != NULL)
655 1212
                        fa->result = vcc_mk_expr(fa->type, "%s", fa->val);
656 13288
                if (fa->result != NULL && sa != NULL) {
657 1772
                        if (fa->cname)
658 1730
                                bprintf(ssa, "\v1.%s = \v2,\n", fa->cname);
659
                        else
660 42
                                bprintf(ssa, "\v1.arg%d = \v2,\n", n);
661 1772
                        e1 = vcc_expr_edit(tl, e1->fmt, ssa, e1, fa->result);
662 13288
                } else if (fa->result != NULL) {
663 17400
                        e1 = vcc_expr_edit(tl, e1->fmt, "\v1,\n\v2",
664 8700
                            e1, fa->result);
665 11516
                } else if (!fa->optional) {
666 2
                        if (fa->name)
667 4
                                VSB_printf(tl->sb, "Argument '%s' missing\n",
668 2
                                    fa->name);
669
                        else
670 0
                                VSB_printf(tl->sb, "Argument %d missing\n", n);
671 2
                        vcc_ErrWhere(tl, tl->t);
672 2
                }
673 13288
                free(fa);
674 13288
        }
675 5502
        if (sa != NULL) {
676 820
                *e = vcc_expr_edit(tl, e1->fmt, "\v1\v-\n}\v-\n)", e1, NULL);
677 820
        } else {
678 4682
                *e = vcc_expr_edit(tl, e1->fmt, "\v1\v-\n)", e1, NULL);
679
        }
680 5502
        SkipToken(tl, ')');
681 5500
        vcc_AddUses(tl, tf, NULL, sym, XREF_READ);
682 5530
}
683
684
685
/*--------------------------------------------------------------------
686
 */
687
688
void
689 402
vcc_Eval_Func(struct vcc *tl, const struct vjsn_val *spec,
690
    const char *extra, struct symbol *sym)
691
{
692 402
        struct expr *e = NULL;
693
694 402
        vcc_func(tl, &e, spec, extra, sym);
695 402
        if (tl->err)
696 0
                VSB_cat(tl->sb, "While compiling function call:\n");
697 402
        ERRCHK(tl);
698 402
        vcc_expr_fmt(tl->fb, tl->indent, e);
699 402
        VSB_cat(tl->fb, ";\n");
700 402
        vcc_delete_expr(e);
701 402
}
702
703
/*--------------------------------------------------------------------
704
 */
705
706
void v_matchproto_(sym_expr_t)
707 4162
vcc_Eval_SymFunc(struct vcc *tl, struct expr **e, struct token *t,
708
    struct symbol *sym, vcc_type_t fmt)
709
{
710
711 4162
        (void)t;
712 4162
        (void)fmt;
713 4162
        assert(sym->kind == SYM_FUNC || sym->kind == SYM_METHOD);
714 4162
        AN(sym->eval_priv);
715
716 4162
        vcc_func(tl, e, sym->eval_priv, sym->extra, sym);
717 4162
        ERRCHK(tl);
718 4142
        if ((*e)->fmt == STRING) {
719 1504
                (*e)->fmt = STRINGS;
720 1504
                (*e)->nstr = 1;
721 1504
        }
722 4162
}
723
724
/*--------------------------------------------------------------------
725
 */
726
727
static void
728 65082
vcc_number(struct vcc *tl, struct expr **e, vcc_type_t fmt, const char *sign)
729
{
730
        VCL_INT vi;
731
        struct expr *e1;
732
        struct token *t;
733
734 65082
        assert(fmt != VOID);
735 65082
        if (fmt == BYTES) {
736 100
                vcc_ByteVal(tl, &vi);
737 100
                ERRCHK(tl);
738 96
                e1 = vcc_mk_expr(BYTES, "%ju", (intmax_t)vi);
739 96
        } else {
740 64982
                t = tl->t;
741 64982
                vcc_NextToken(tl);
742 64982
                if (tl->t->tok == ID) {
743 6208
                        e1 = vcc_mk_expr(DURATION, "%s%.3f * %g",
744 6208
                            sign, t->num, vcc_DurationUnit(tl));
745 6208
                        ERRCHK(tl);
746 64980
                } else if (fmt == REAL || t->tok == FNUM) {
747 308
                        e1 = vcc_mk_expr(REAL, "%s%.3f", sign, t->num);
748 308
                } else {
749 58466
                        e1 = vcc_mk_expr(INT, "%s%.0f", sign, t->num);
750
                }
751
        }
752 65076
        e1->constant = EXPR_CONST;
753 65076
        *e = e1;
754 65082
}
755
756
/*--------------------------------------------------------------------
757
 * SYNTAX:
758
 *    Expr5:
759
 *      '(' ExprCor ')'
760
 *      symbol
761
 *      CNUM
762
 *      FNUM
763
 *      CSTR
764
 *      CBLOB
765
 */
766
767
static void
768 586264
vcc_expr5(struct vcc *tl, struct expr **e, vcc_type_t fmt)
769
{
770
        struct expr *e1, *e2;
771
        const char *ip, *sign;
772
        struct token *t, *t1;
773
        struct symbol *sym;
774
775 586264
        sign = "";
776 586264
        *e = NULL;
777 586264
        if (tl->t->tok == '(') {
778 5776
                SkipToken(tl, '(');
779 5776
                vcc_expr_cor(tl, &e2, fmt);
780 5776
                ERRCHK(tl);
781 5776
                SkipToken(tl, ')');
782 5776
                if (e2->fmt == STRINGS)
783 12
                        *e = e2;
784
                else
785 5764
                        *e = vcc_expr_edit(tl, e2->fmt, "(\v1)", e2, NULL);
786 5776
                return;
787
        }
788 580488
        switch (tl->t->tok) {
789
        case ID:
790 288890
                t = tl->t;
791 288890
                t1 = vcc_PeekToken(tl);
792 288890
                AN(t1);
793 288890
                sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_NONE,
794
                    SYMTAB_PARTIAL_NOERR, XREF_REF);
795 288890
                if (sym == NULL && fmt->global_pfx != NULL && t1->tok != '.') {
796 18
                        sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_NONE,
797
                            SYMTAB_CREATE, XREF_REF);
798 18
                        ERRCHK(tl);
799 14
                        AN(sym);
800 14
                        VCC_GlobalSymbol(sym, fmt);
801 14
                }
802 288886
                ERRCHK(tl);
803 288886
                if (sym == NULL)
804 24
                        AZ(VCC_SymbolGet(tl, SYM_MAIN, SYM_NONE,
805
                            SYMTAB_PARTIAL, XREF_REF));
806 288886
                ERRCHK(tl);
807 288862
                AN(sym);
808 288862
                if (sym->kind == SYM_INSTANCE) {
809 1548
                        AZ(*e);
810 1548
                        *e = vcc_new_expr(sym->type);
811 1548
                        (*e)->instance = sym;
812 1548
                        return;
813
                }
814 287314
                if (sym->kind == SYM_FUNC && sym->type == VOID) {
815 2
                        VSB_cat(tl->sb, "Function returns VOID:\n");
816 2
                        vcc_ErrWhere(tl, tl->t);
817 2
                        return;
818
                }
819 287312
                if (sym->eval != NULL) {
820 287306
                        AN(sym->eval);
821 287306
                        AZ(*e);
822 287306
                        sym->eval(tl, e, t, sym, fmt);
823 287306
                        if (tl->err) {
824 32
                                VSB_cat(tl->sb,
825
                                    "While compiling function call:\n\n");
826 32
                                vcc_ErrWhere2(tl, t, tl->t);
827 32
                        }
828 287306
                        ERRCHK(tl);
829
                        /* Unless asked for a HEADER, fold to string here */
830 287274
                        if (*e && fmt != HEADER && (*e)->fmt == HEADER) {
831 81738
                                vcc_expr_tostring(tl, e);
832 81738
                                ERRCHK(tl);
833 81738
                        }
834 287274
                        return;
835
                }
836 12
                VSB_printf(tl->sb,
837
                    "Symbol '%.*s' type (%s) cannot be used in expression.\n",
838 6
                    PF(t), sym->kind->name);
839 6
                vcc_ErrWhere(tl, t);
840 6
                if (sym->def_b != NULL) {
841 2
                        VSB_cat(tl->sb, "That symbol was defined here:\n");
842 2
                        vcc_ErrWhere(tl, sym->def_b);
843 2
                }
844 6
                return;
845
        case CSTR:
846 226486
                assert(fmt != VOID);
847 226486
                if (fmt == IP) {
848 60
                        if (*tl->t->dec == '/') {
849
                                /*
850
                                 * On some platforms (e.g. FreeBSD),
851
                                 * getaddrinfo(3) may resolve a path to a
852
                                 * sockaddr_un if it happens to exist and
853
                                 * is a socket. So don't let that happen.
854
                                 */
855 2
                                VSB_cat(tl->sb,
856
                                    "Cannot convert to an IP address: ");
857 2
                                vcc_ErrToken(tl, tl->t);
858 2
                                vcc_ErrWhere(tl, tl->t);
859 2
                                return;
860
                        }
861 116
                        Resolve_Sockaddr(tl, tl->t->dec, "80",
862
                            &ip, NULL, &ip, NULL, NULL, 1,
863 58
                            tl->t, "IP constant");
864 58
                        ERRCHK(tl);
865 52
                        e1 = vcc_mk_expr(IP, "%s", ip);
866 52
                        ERRCHK(tl);
867 226478
                } else if (fmt == REGEX) {
868 17344
                        e1 = vcc_new_expr(REGEX);
869 17344
                        vcc_regexp(tl, e1->vsb);
870 17344
                        AZ(VSB_finish(e1->vsb));
871 17344
                } else {
872 209082
                        e1 = vcc_new_expr(STRINGS);
873 209082
                        EncToken(e1->vsb, tl->t);
874 209082
                        AZ(VSB_finish(e1->vsb));
875 209082
                        e1->constant |= EXPR_STR_CONST;
876 209082
                        e1->nstr = 1;
877
                }
878 226478
                e1->t1 = tl->t;
879 226478
                e1->constant |= EXPR_CONST;
880 226478
                vcc_NextToken(tl);
881 226478
                *e = e1;
882 226478
                return;
883
        case '-':
884 124
                if (fmt != INT &&
885 32
                    fmt != REAL &&
886 8
                    fmt != DURATION &&
887 2
                    fmt != STRINGS)
888 0
                        break;
889 122
                vcc_NextToken(tl);
890 122
                if (tl->t->tok != FNUM && tl->t->tok != CNUM) {
891 18
                        vcc_expr_cor(tl, &e1, fmt);
892 18
                        ERRCHK(tl);
893 18
                        *e = vcc_expr_edit(tl, e1->fmt, "-(\v1)", e1, NULL);
894 18
                        return;
895
                }
896 104
                sign = "-";
897
                /* FALLTHROUGH */
898
        case FNUM:
899
        case CNUM:
900 65082
                vcc_number(tl, e, fmt, sign);
901 65082
                return;
902
        case CBLOB:
903 4
                e1 = vcc_new_expr(BLOB);
904 4
                VSB_printf(e1->vsb, "%s", tl->t->dec);
905 4
                AZ(VSB_finish(e1->vsb));
906 4
                e1->constant |= EXPR_STR_CONST;
907 4
                e1->t1 = tl->t;
908 4
                vcc_NextToken(tl);
909 4
                *e = e1;
910 4
                return;
911
        default:
912 8
                break;
913
        }
914 8
        VSB_cat(tl->sb, "Unknown token ");
915 8
        vcc_ErrToken(tl, tl->t);
916 8
        VSB_printf(tl->sb, " when looking for %s\n\n", vcc_utype(fmt));
917 8
        vcc_ErrWhere(tl, tl->t);
918 586264
}
919
920
/*--------------------------------------------------------------------
921
 * SYNTAX:
922
 *    Expr4:
923
 *      Expr5 [ '.' (type_attribute | type_method()) ]*
924
 */
925
926
void
927 5734
vcc_Eval_TypeMethod(struct vcc *tl, struct expr **e, struct token *t,
928
    struct symbol *sym, vcc_type_t fmt)
929
{
930
        const char *impl;
931
932 5734
        (void)t;
933 5734
        impl = VCC_Type_EvalMethod(tl, sym);
934 5734
        ERRCHK(tl);
935 5734
        AN(impl);
936 5734
        *e = vcc_expr_edit(tl, fmt, impl, *e, NULL);
937 5734
}
938
939
static void
940 586264
vcc_expr4(struct vcc *tl, struct expr **e, vcc_type_t fmt)
941
{
942
        struct symbol *sym;
943
944 586264
        *e = NULL;
945 586264
        vcc_expr5(tl, e, fmt);
946 586264
        ERRCHK(tl);
947 586168
        AN(*e);
948 593446
        while (tl->t->tok == '.') {
949 7284
                vcc_NextToken(tl);
950 7284
                ExpectErr(tl, ID);
951
952 7282
                sym = VCC_TypeSymbol(tl, SYM_METHOD, (*e)->fmt);
953 7282
                if (sym == NULL) {
954 4
                        VSB_cat(tl->sb, "Unknown property ");
955 4
                        vcc_ErrToken(tl, tl->t);
956 4
                        VSB_printf(tl->sb, " for type %s\n", (*e)->fmt->name);
957 4
                        vcc_ErrWhere(tl, tl->t);
958 4
                        return;
959
                }
960
961 7278
                AN(sym->eval);
962 7278
                sym->eval(tl, e, tl->t, sym, sym->type);
963 7278
                ERRCHK(tl);
964 7278
                if ((*e)->fmt == STRING) {
965 5712
                        (*e)->fmt = STRINGS;
966 5712
                        (*e)->nstr = 1;
967 5712
                }
968
        }
969 586264
}
970
971
/*--------------------------------------------------------------------
972
 * SYNTAX:
973
 *    ExprMul:
974
 *      Expr4 { {'*'|'/'|'%'} Expr4 } *
975
 */
976
977
static void
978 568174
vcc_expr_mul(struct vcc *tl, struct expr **e, vcc_type_t fmt)
979
{
980
        struct expr *e2;
981
        vcc_type_t f2;
982
        struct token *tk;
983
        char buf[24];
984
985 568174
        *e = NULL;
986 568174
        vcc_expr4(tl, e, fmt);
987 568174
        ERRCHK(tl);
988 568086
        AN(*e);
989
990 568160
        while (tl->t->tok == '*' || tl->t->tok == '/' || tl->t->tok == '%') {
991 82
                if (tl->t->tok == '%' && ((*e)->fmt != INT)) {
992 2
                        VSB_cat(tl->sb, "Operator % only possible on INT.\n");
993 2
                        vcc_ErrWhere(tl, tl->t);
994 2
                        return;
995
                }
996 80
                f2 = (*e)->fmt->multype;
997 80
                if (f2 == NULL) {
998 4
                        VSB_printf(tl->sb,
999
                            "Operator %.*s not possible on type %s.\n",
1000 2
                            PF(tl->t), vcc_utype((*e)->fmt));
1001 2
                        vcc_ErrWhere(tl, tl->t);
1002 2
                        return;
1003
                }
1004 78
                tk = tl->t;
1005 78
                vcc_NextToken(tl);
1006 78
                vcc_expr4(tl, &e2, f2);
1007 78
                ERRCHK(tl);
1008 78
                if (e2->fmt != INT && e2->fmt != f2) {
1009 8
                        VSB_printf(tl->sb, "%s %.*s %s not possible.\n",
1010 4
                            vcc_utype((*e)->fmt), PF(tk), vcc_utype(e2->fmt));
1011 4
                        vcc_ErrWhere(tl, tk);
1012 4
                        return;
1013
                }
1014 74
                bprintf(buf, "(\v1%c\v2)", tk->tok);
1015 74
                *e = vcc_expr_edit(tl, (*e)->fmt, buf, *e, e2);
1016
        }
1017 568174
}
1018
1019
/*--------------------------------------------------------------------
1020
 * SYNTAX:
1021
 *    ExprAdd:
1022
 *      ExprMul { {'+'|'-'} ExprMul } *
1023
 */
1024
1025
static const struct adds {
1026
        unsigned        op;
1027
        vcc_type_t      a;
1028
        vcc_type_t      b;
1029
        vcc_type_t      fmt;
1030
} vcc_adds[] = {
1031
        { '+', BYTES,           BYTES,          BYTES },
1032
        { '-', BYTES,           BYTES,          BYTES },
1033
        { '+', DURATION,        DURATION,       DURATION },
1034
        { '-', DURATION,        DURATION,       DURATION },
1035
        { '+', INT,             INT,            INT },
1036
        { '-', INT,             INT,            INT },
1037
        { '+', INT,             REAL,           REAL },
1038
        { '-', INT,             REAL,           REAL },
1039
        { '+', REAL,            INT,            REAL },
1040
        { '-', REAL,            INT,            REAL },
1041
        { '+', REAL,            REAL,           REAL },
1042
        { '-', REAL,            REAL,           REAL },
1043
        { '-', TIME,            TIME,           DURATION },
1044
        { '+', TIME,            DURATION,       TIME },
1045
        { '-', TIME,            DURATION,       TIME },
1046
1047
        { EOI, VOID,            VOID,           VOID }
1048
};
1049
1050
static void
1051 430436
vcc_expr_add(struct vcc *tl, struct expr **e, vcc_type_t fmt)
1052
{
1053
        const struct adds *ap;
1054
        struct expr  *e2;
1055
        struct token *tk;
1056
        int lit, n;
1057
1058 430436
        *e = NULL;
1059 430436
        vcc_expr_mul(tl, e, fmt);
1060 430436
        ERRCHK(tl);
1061
1062 568056
        while (tl->t->tok == '+' || tl->t->tok == '-') {
1063 137738
                tk = tl->t;
1064 2202300
                for (ap = vcc_adds; ap->op != EOI; ap++)
1065 2064712
                        if (tk->tok == ap->op && (*e)->fmt == ap->a)
1066 150
                                break;
1067 137738
                vcc_NextToken(tl);
1068 137738
                if (ap->op == EOI && fmt == STRINGS)
1069 1008
                        vcc_expr_mul(tl, &e2, STRINGS);
1070
                else
1071 136730
                        vcc_expr_mul(tl, &e2, (*e)->fmt);
1072 137738
                ERRCHK(tl);
1073
1074 2202544
                for (ap = vcc_adds; ap->op != EOI; ap++)
1075 2064936
                        if (tk->tok == ap->op && (*e)->fmt == ap->a &&
1076 2064936
                            e2->fmt == ap->b)
1077 130
                                break;
1078
1079 137738
                if (ap->op == '+') {
1080 90
                        *e = vcc_expr_edit(tl, ap->fmt, "(\v1 + \v2)", *e, e2);
1081 137738
                } else if (ap->op == '-') {
1082 40
                        *e = vcc_expr_edit(tl, ap->fmt, "(\v1 - \v2)", *e, e2);
1083 137678
                } else if (tk->tok == '+' &&
1084 137606
                    ((*e)->fmt == STRINGS || fmt == STRINGS)) {
1085 137586
                        if ((*e)->fmt != STRINGS)
1086 10
                                vcc_expr_tostring(tl, e);
1087 137586
                        if (e2->fmt != STRINGS)
1088 34252
                                vcc_expr_tostring(tl, &e2);
1089 137586
                        if (vcc_islit(*e) && vcc_isconst(e2)) {
1090 100
                                lit = vcc_islit(e2);
1091 200
                                *e = vcc_expr_edit(tl, STRINGS,
1092 100
                                    "\v1\n\v2", *e, e2);
1093 100
                                (*e)->constant = EXPR_CONST;
1094 100
                                (*e)->nstr = 1;
1095 100
                                if (lit)
1096 100
                                        (*e)->constant |= EXPR_STR_CONST;
1097 100
                        } else {
1098 137486
                                n = (*e)->nstr + e2->nstr;
1099 274972
                                *e = vcc_expr_edit(tl, STRINGS,
1100 137486
                                    "\v1,\n\v2", *e, e2);
1101 137486
                                (*e)->constant = EXPR_VAR;
1102 137486
                                (*e)->nstr = n;
1103
                        }
1104 137586
                } else {
1105 44
                        VSB_printf(tl->sb, "%s %.*s %s not possible.\n",
1106 22
                            vcc_utype((*e)->fmt), PF(tk), vcc_utype(e2->fmt));
1107 22
                        vcc_ErrWhere2(tl, tk, tl->t);
1108 22
                        return;
1109
                }
1110
        }
1111 430436
}
1112
1113
/*--------------------------------------------------------------------
1114
 * SYNTAX:
1115
 *    ExprCmp:
1116
 *      ExprAdd
1117
 *      ExprAdd Relation ExprAdd
1118
 *      ExprAdd(STRING) '~' CString
1119
 *      ExprAdd(STRING) '!~' CString
1120
 *      ExprAdd(IP) '==' ExprAdd(IP)
1121
 *      ExprAdd(IP) '!=' ExprAdd(IP)
1122
 *      ExprAdd(IP) '~' ACL
1123
 *      ExprAdd(IP) '!~' ACL
1124
 */
1125
1126
struct cmps;
1127
1128
typedef void cmp_f(struct vcc *, struct expr **, const struct cmps *);
1129
1130
struct cmps {
1131
        vcc_type_t              fmt;
1132
        unsigned                token;
1133
        cmp_f                   *func;
1134
        const char              *emit;
1135
};
1136
1137
static void v_matchproto_(cmp_f)
1138 28734
cmp_simple(struct vcc *tl, struct expr **e, const struct cmps *cp)
1139
{
1140
        struct expr *e2;
1141
        struct token *tk;
1142
1143 28734
        tk = tl->t;
1144 28734
        vcc_NextToken(tl);
1145 28734
        vcc_expr_add(tl, &e2, (*e)->fmt);
1146 28734
        ERRCHK(tl);
1147
1148 28728
        if (e2->fmt != (*e)->fmt) {
1149 8
                VSB_printf(tl->sb,
1150
                    "Comparison of different types: %s '%.*s' %s\n",
1151 4
                    vcc_utype((*e)->fmt), PF(tk), vcc_utype(e2->fmt));
1152 4
                vcc_ErrWhere(tl, tk);
1153 4
        } else
1154 28724
                *e = vcc_expr_edit(tl, BOOL, cp->emit, *e, e2);
1155 28734
}
1156
1157
static void v_matchproto_(cmp_f)
1158 17254
cmp_regexp(struct vcc *tl, struct expr **e, const struct cmps *cp)
1159
{
1160
        struct token *t1;
1161
        struct expr *e2;
1162
        char buf[128];
1163
1164 17254
        *e = vcc_expr_edit(tl, STRING, "\vS", *e, NULL);
1165 17254
        vcc_NextToken(tl);
1166 17254
        t1 = tl->t;
1167 17254
        vcc_expr4(tl, &e2, REGEX);
1168 17254
        ERRCHK(tl);
1169 17246
        vcc_expr_typecheck(tl, &e2, REGEX, t1);
1170 17246
        ERRCHK(tl);
1171 17244
        bprintf(buf, "%sVRT_re_match(ctx, \v1, \v2)", cp->emit);
1172 17244
        *e = vcc_expr_edit(tl, BOOL, buf, *e, e2);
1173 17254
}
1174
1175
static void v_matchproto_(cmp_f)
1176 92
cmp_acl(struct vcc *tl, struct expr **e, const struct cmps *cp)
1177
{
1178
        struct token *t1;
1179
        struct expr *e2;
1180
        char buf[256];
1181
1182 92
        vcc_NextToken(tl);
1183 92
        t1 = tl->t;
1184 92
        vcc_expr4(tl, &e2, ACL);
1185 92
        ERRCHK(tl);
1186 86
        vcc_expr_typecheck(tl, &e2, ACL, t1);
1187 86
        ERRCHK(tl);
1188 80
        bprintf(buf, "%sVRT_acl_match(ctx, \v1, \v2)", cp->emit);
1189 80
        *e = vcc_expr_edit(tl, BOOL, buf, e2, *e);
1190 92
}
1191
1192
static void v_matchproto_(cmp_f)
1193 74926
cmp_string(struct vcc *tl, struct expr **e, const struct cmps *cp)
1194
{
1195
        struct expr *e2;
1196
        struct token *tk;
1197
        char buf[128];
1198
1199 74926
        tk = tl->t;
1200 74926
        vcc_NextToken(tl);
1201 74926
        vcc_expr_add(tl, &e2, STRINGS);
1202 74926
        ERRCHK(tl);
1203 74926
        if (vcc_stringstype(e2->fmt) != STRINGS) {
1204 12
                VSB_printf(tl->sb,
1205
                    "Comparison of different types: %s '%.*s' %s\n",
1206 6
                    vcc_utype((*e)->fmt), PF(tk), vcc_utype(e2->fmt));
1207 6
                vcc_ErrWhere(tl, tk);
1208 74926
        } else if ((*e)->nstr == 1 && e2->nstr == 1) {
1209 74858
                bprintf(buf, "(%s VRT_strcmp(\v1, \v2))", cp->emit);
1210 74858
                *e = vcc_expr_edit(tl, BOOL, buf, *e, e2);
1211 74858
        } else {
1212 62
                bprintf(buf, "(%s VRT_CompareStrands(\vT, \vt))", cp->emit);
1213 62
                *e = vcc_expr_edit(tl, BOOL, buf, *e, e2);
1214
        }
1215 74926
}
1216
1217
#define IDENT_REL(typ)                                                  \
1218
        {typ,           T_EQ,           cmp_simple, "(\v1 == \v2)" },   \
1219
        {typ,           T_NEQ,          cmp_simple, "(\v1 != \v2)" }
1220
1221
#define NUM_REL(typ)                                                    \
1222
        IDENT_REL(typ),                                                 \
1223
        {typ,           T_LEQ,          cmp_simple, "(\v1 <= \v2)" },   \
1224
        {typ,           T_GEQ,          cmp_simple, "(\v1 >= \v2)" },   \
1225
        {typ,           '<',            cmp_simple, "(\v1 < \v2)" },    \
1226
        {typ,           '>',            cmp_simple, "(\v1 > \v2)" }
1227
1228
static const struct cmps vcc_cmps[] = {
1229
        NUM_REL(INT),
1230
        NUM_REL(DURATION),
1231
        NUM_REL(BYTES),
1232
        NUM_REL(REAL),
1233
        NUM_REL(TIME),
1234
        IDENT_REL(BACKEND),
1235
        IDENT_REL(ACL),
1236
        IDENT_REL(PROBE),
1237
        IDENT_REL(STEVEDORE),
1238
        IDENT_REL(SUB),
1239
        IDENT_REL(INSTANCE),
1240
1241
        {BOOL,          T_EQ,           cmp_simple, "((!(\v1)) == (!(\v2)))" },
1242
        {BOOL,          T_NEQ,          cmp_simple, "((!(\v1)) != (!(\v2)))" },
1243
        {IP,            T_EQ,           cmp_simple, "!VRT_ipcmp(ctx, \v1, \v2)" },
1244
        {IP,            T_NEQ,          cmp_simple, "VRT_ipcmp(ctx, \v1, \v2)" },
1245
1246
        {IP,            '~',            cmp_acl, "" },
1247
        {IP,            T_NOMATCH,      cmp_acl, "!" },
1248
1249
        {STRINGS,       T_EQ,           cmp_string, "0 =="},
1250
        {STRINGS,       T_NEQ,          cmp_string, "0 !="},
1251
        {STRINGS,       '<',            cmp_string, "0 > "},
1252
        {STRINGS,       '>',            cmp_string, "0 < "},
1253
        {STRINGS,       T_LEQ,          cmp_string, "0 >="},
1254
        {STRINGS,       T_GEQ,          cmp_string, "0 <="},
1255
1256
        {STRINGS,       '~',            cmp_regexp, "" },
1257
        {STRINGS,       T_NOMATCH,      cmp_regexp, "!" },
1258
1259
        {VOID,          0,              NULL, NULL}
1260
};
1261
1262
#undef IDENT_REL
1263
#undef NUM_REL
1264
1265
static void
1266 326776
vcc_expr_cmp(struct vcc *tl, struct expr **e, vcc_type_t fmt)
1267
{
1268
        const struct cmps *cp;
1269
        struct token *tk;
1270
1271 326776
        *e = NULL;
1272 326776
        vcc_expr_add(tl, e, fmt);
1273 326776
        ERRCHK(tl);
1274 326664
        tk = tl->t;
1275
1276 16497658
        for (cp = vcc_cmps; cp->fmt != VOID; cp++) {
1277 16292000
                if (tl->t->tok != cp->token)
1278 15173888
                        continue;
1279 1118112
                if (vcc_stringstype((*e)->fmt) != cp->fmt)
1280 997106
                        continue;
1281 121006
                AN(cp->func);
1282 121006
                cp->func(tl, e, cp);
1283 121006
                return;
1284
        }
1285
1286 205658
        switch (tk->tok) {
1287
        case T_EQ:
1288
        case T_NEQ:
1289
        case '<':
1290
        case T_LEQ:
1291
        case '>':
1292
        case T_GEQ:
1293
        case '~':
1294
        case T_NOMATCH:
1295 8
                VSB_printf(tl->sb, "Operator %.*s not possible on %s\n",
1296 4
                    PF(tl->t), vcc_utype((*e)->fmt));
1297 4
                vcc_ErrWhere(tl, tl->t);
1298 4
                return;
1299
        default:
1300 205654
                break;
1301
        }
1302 326776
}
1303
1304
/*--------------------------------------------------------------------
1305
 * SYNTAX:
1306
 *    ExprNot:
1307
 *      '!' ExprCmp
1308
 */
1309
1310
static void
1311 326776
vcc_expr_not(struct vcc *tl, struct expr **e, vcc_type_t fmt)
1312
{
1313
        struct token *tk;
1314
1315 326776
        *e = NULL;
1316 326776
        tk = tl->t;
1317 326776
        if (tl->t->tok == '!')
1318 28610
                vcc_NextToken(tl);
1319 326776
        vcc_expr_cmp(tl, e, fmt);
1320 326776
        ERRCHK(tl);
1321 326622
        if (tk->tok != '!')
1322 298014
                return;
1323 28608
        vcc_expr_tobool(tl, e);
1324 28608
        ERRCHK(tl);
1325 28608
        if ((*e)->fmt != BOOL) {
1326 2
                VSB_cat(tl->sb, "'!' must be followed by BOOL, found ");
1327 2
                VSB_printf(tl->sb, "%s.\n", vcc_utype((*e)->fmt));
1328 2
                vcc_ErrWhere2(tl, tk, tl->t);
1329 2
        } else {
1330 28606
                *e = vcc_expr_edit(tl, BOOL, "!(\v1)", *e, NULL);
1331
        }
1332 326776
}
1333
1334
/*--------------------------------------------------------------------
1335
 * CAND and COR are identical save for a few details, but they are
1336
 * stacked so handling them in the same function is not simpler.
1337
 * Instead have them both call this helper function to do everything.
1338
 */
1339
1340
typedef void upfunc(struct vcc *tl, struct expr **e, vcc_type_t fmt);
1341
1342
static void
1343 499820
vcc_expr_bin_bool(struct vcc *tl, struct expr **e, vcc_type_t fmt,
1344
    unsigned ourtok, upfunc *up, const char *tokstr)
1345
{
1346
        struct expr *e2;
1347
        struct token *tk;
1348
        char buf[32];
1349
1350 499820
        *e = NULL;
1351 499820
        tk = tl->t;
1352 499820
        up(tl, e, fmt);
1353 499820
        ERRCHK(tl);
1354 499504
        if (tl->t->tok != ourtok)
1355 459604
                return;
1356 39900
        vcc_expr_tobool(tl, e);
1357 39900
        ERRCHK(tl);
1358 39900
        if ((*e)->fmt != BOOL) {
1359 8
                VSB_printf(tl->sb,
1360
                    "'%s' must be preceded by BOOL,"
1361 4
                    " found %s.\n", tokstr, vcc_utype((*e)->fmt));
1362 4
                vcc_ErrWhere2(tl, tk, tl->t);
1363 4
                return;
1364
        }
1365 39896
        *e = vcc_expr_edit(tl, BOOL, "(\v+\n\v1", *e, NULL);
1366 119618
        while (tl->t->tok == ourtok) {
1367 79726
                vcc_NextToken(tl);
1368 79726
                tk = tl->t;
1369 79726
                up(tl, &e2, fmt);
1370 79726
                ERRCHK(tl);
1371 79726
                vcc_expr_tobool(tl, &e2);
1372 79726
                ERRCHK(tl);
1373 79726
                if (e2->fmt != BOOL) {
1374 8
                        VSB_printf(tl->sb,
1375
                            "'%s' must be followed by BOOL,"
1376 4
                            " found %s.\n", tokstr, vcc_utype(e2->fmt));
1377 4
                        vcc_ErrWhere2(tl, tk, tl->t);
1378 4
                        vcc_delete_expr(e2);
1379 4
                        return;
1380
                }
1381 79722
                bprintf(buf, "\v1\v-\n%s\v+\n\v2", tokstr);
1382 79722
                *e = vcc_expr_edit(tl, BOOL, buf, *e, e2);
1383
        }
1384 39892
        *e = vcc_expr_edit(tl, BOOL, "\v1\v-\n)", *e, NULL);
1385 499820
}
1386
1387
/*--------------------------------------------------------------------
1388
 * SYNTAX:
1389
 *    ExprCand:
1390
 *      ExprNot { '&&' ExprNot } *
1391
 */
1392
1393
static void
1394 252770
vcc_expr_cand(struct vcc *tl, struct expr **e, vcc_type_t fmt)
1395
{
1396
1397 252770
        vcc_expr_bin_bool(tl, e, fmt, T_CAND, vcc_expr_not, "&&");
1398 252770
}
1399
1400
/*--------------------------------------------------------------------
1401
 * SYNTAX:
1402
 *    ExprCOR:
1403
 *      ExprCand { '||' ExprCand } *
1404
 */
1405
1406
static void
1407 247050
vcc_expr_cor(struct vcc *tl, struct expr **e, vcc_type_t fmt)
1408
{
1409
1410 247050
        vcc_expr_bin_bool(tl, e, fmt, T_COR, vcc_expr_cand, "||");
1411 247050
}
1412
1413
/*--------------------------------------------------------------------
1414
 * This function is the entry-point for getting an expression with
1415
 * a particular type, ready for inclusion in the VGC.
1416
 */
1417
1418
static void
1419 241256
vcc_expr0(struct vcc *tl, struct expr **e, vcc_type_t fmt)
1420
{
1421
        struct token *t1;
1422
1423 241256
        assert(fmt != VOID);
1424 241256
        assert(fmt != STRINGS);
1425 241256
        *e = NULL;
1426 241256
        t1 = tl->t;
1427 241256
        if (fmt->stringform)
1428 81088
                vcc_expr_cor(tl, e, STRINGS);
1429
        else
1430 160168
                vcc_expr_cor(tl, e, fmt);
1431 241256
        ERRCHK(tl);
1432
1433 241092
        if ((*e)->fmt == fmt)
1434 125684
                return;
1435
1436 115408
        if ((*e)->fmt != STRINGS && fmt->stringform)
1437 7006
                vcc_expr_tostring(tl, e);
1438
1439 115408
        if ((*e)->fmt->stringform) {
1440 0
                VSB_printf(tl->sb, "Cannot convert type %s(%s) to %s(%s)\n",
1441 0
                    vcc_utype((*e)->fmt), (*e)->fmt->name,
1442 0
                    vcc_utype(fmt), fmt->name);
1443 0
                vcc_ErrWhere2(tl, t1, tl->t);
1444 0
                return;
1445
        }
1446
1447 115408
        if (fmt == BODY && !(*e)->fmt->bodyform)
1448 2
                vcc_expr_tostring(tl, e);
1449
1450 115408
        if (fmt == BODY && (*e)->fmt->bodyform) {
1451 11432
                if ((*e)->fmt == STRINGS)
1452 11424
                        *e = vcc_expr_edit(tl, BODY, "STRING, 0, \vT", *e, NULL);
1453 8
                else if ((*e)->fmt == BLOB)
1454 8
                        *e = vcc_expr_edit(tl, BODY, "BLOB, 0, \v1", *e, NULL);
1455
                else
1456 0
                        WRONG("Unhandled bodyform");
1457 11432
        }
1458
1459 115408
        if ((*e)->fmt == STRINGS && fmt->stringform) {
1460 81022
                if (fmt == STRING)
1461 24314
                        *e = vcc_expr_edit(tl, STRING, "\vS", *e, NULL);
1462 56708
                else if (fmt == STRANDS)
1463 56708
                        *e = vcc_expr_edit(tl, STRANDS, "\vT", (*e), NULL);
1464
                else
1465 0
                        WRONG("Unhandled stringform");
1466 81022
        }
1467
1468 115408
        if (fmt == BOOL) {
1469 22946
                vcc_expr_tobool(tl, e);
1470 22946
                ERRCHK(tl);
1471 22946
        }
1472
1473 115408
        vcc_expr_typecheck(tl, e, fmt, t1);
1474 241256
}
1475
1476
static void
1477 132740
vcc_expr_typecheck(struct vcc *tl, struct expr **e, vcc_type_t fmt,
1478
    struct token *t1)
1479
{
1480
1481 132740
        assert(fmt != VOID);
1482 132740
        assert(fmt != STRINGS);
1483
1484 132740
        if (fmt != (*e)->fmt)  {
1485 36
                VSB_printf(tl->sb, "Expression has type %s, expected %s\n",
1486 18
                    vcc_utype((*e)->fmt), vcc_utype(fmt));
1487 18
                vcc_ErrWhere2(tl, t1, tl->t);
1488 18
        }
1489 132740
}
1490
1491
/*--------------------------------------------------------------------
1492
 * This function parses and emits the C-code to evaluate an expression
1493
 *
1494
 * We know up front what kind of type we want the expression to be,
1495
 * and this function is the backstop if that doesn't succeed.
1496
 */
1497
1498
void
1499 235732
vcc_Expr(struct vcc *tl, vcc_type_t fmt)
1500
{
1501 235732
        struct expr *e = NULL;
1502
1503 235732
        assert(fmt != VOID);
1504 235732
        assert(fmt != STRINGS);
1505 235732
        vcc_expr0(tl, &e, fmt);
1506 235732
        ERRCHK(tl);
1507 235576
        assert(e->fmt == fmt);
1508
1509 235576
        vcc_expr_fmt(tl->fb, tl->indent, e);
1510 235576
        VSB_cat(tl->fb, "\n");
1511 235576
        vcc_delete_expr(e);
1512 235732
}
1513
1514
/*--------------------------------------------------------------------
1515
 */
1516
1517
void v_matchproto_(sym_act_f)
1518 966
vcc_Act_Call(struct vcc *tl, struct token *t, struct symbol *sym)
1519
{
1520
1521
        struct expr *e;
1522
1523 966
        e = NULL;
1524 966
        vcc_func(tl, &e, sym->eval_priv, sym->extra, sym);
1525 966
        if (!tl->err) {
1526 956
                vcc_expr_fmt(tl->fb, tl->indent, e);
1527 956
                SkipToken(tl, ';');
1528 956
                VSB_cat(tl->fb, ";\n");
1529 966
        } else if (t != tl->t) {
1530 10
                VSB_cat(tl->sb, "While compiling function call:\n\n");
1531 10
                vcc_ErrWhere2(tl, t, tl->t);
1532 10
        }
1533 966
        vcc_delete_expr(e);
1534 966
}
1535
1536
void v_matchproto_(sym_act_f)
1537 668
vcc_Act_Obj(struct vcc *tl, struct token *t, struct symbol *sym)
1538
{
1539
1540 668
        struct expr *e = NULL;
1541
1542 668
        assert(sym->kind == SYM_INSTANCE);
1543 668
        ExpectErr(tl, '.');
1544 666
        tl->t = t;
1545 666
        vcc_expr4(tl, &e, sym->type);
1546 666
        ERRCHK(tl);
1547 666
        vcc_expr_fmt(tl->fb, tl->indent, e);
1548 666
        vcc_delete_expr(e);
1549 666
        SkipToken(tl, ';');
1550 666
        VSB_cat(tl->fb, ";\n");
1551 668
}
1552
1553
/*--------------------------------------------------------------------
1554
 */
1555
1556
static void v_matchproto_(sym_expr_t)
1557 74
vcc_Eval_Regsub(struct vcc *tl, struct expr **e, struct token *t,
1558
    struct symbol *sym, vcc_type_t fmt)
1559
{
1560
        struct expr *e2, *e3;
1561 74
        int all = sym->eval_priv == NULL ? 0 : 1;
1562
        char buf[128];
1563
1564 74
        (void)t;
1565 74
        (void)fmt;
1566 74
        SkipToken(tl, '(');
1567 74
        vcc_expr0(tl, &e2, STRING);
1568 74
        ERRCHK(tl);
1569 74
        SkipToken(tl, ',');
1570 74
        vcc_expr0(tl, &e3, REGEX);
1571 74
        ERRCHK(tl);
1572
1573 72
        bprintf(buf, "VRT_regsub(ctx, %d,\v+\n\v1,\n\v2", all);
1574 72
        *e = vcc_expr_edit(tl, STRING, buf, e2, e3);
1575 72
        SkipToken(tl, ',');
1576 72
        vcc_expr0(tl, &e2, STRING);
1577 72
        ERRCHK(tl);
1578 72
        *e = vcc_expr_edit(tl, STRINGS, "\v1,\n\v2)\v-", *e, e2);
1579 72
        (*e)->nstr = 1;
1580 72
        SkipToken(tl, ')');
1581 74
}
1582
1583
/*--------------------------------------------------------------------
1584
 */
1585
1586
static void v_matchproto_(sym_expr_t)
1587 6184
vcc_Eval_BoolConst(struct vcc *tl, struct expr **e, struct token *t,
1588
    struct symbol *sym, vcc_type_t fmt)
1589
{
1590
1591 6184
        (void)t;
1592 6184
        (void)tl;
1593 6184
        (void)fmt;
1594 6184
        *e = vcc_mk_expr(BOOL, "(0==%d)", sym->eval_priv == NULL ? 1 : 0);
1595 6184
        (*e)->constant = EXPR_CONST;
1596 6184
}
1597
1598
/*--------------------------------------------------------------------
1599
 */
1600
1601
static void v_matchproto_(sym_expr_t)
1602 8
vcc_Eval_Default(struct vcc *tl, struct expr **e, struct token *t,
1603
    struct symbol *sym, vcc_type_t fmt)
1604
{
1605 8
        (void)e;
1606 8
        (void)fmt;
1607 8
        (void)sym;
1608 8
        (void)t;
1609
1610 8
        if (fmt->default_sym == NULL) {
1611 4
                VSB_cat(tl->sb, "Symbol 'default' is a reserved word.\n");
1612 4
                vcc_ErrWhere(tl, t);
1613 4
                return;
1614
        }
1615
1616 4
        *e = vcc_mk_expr(fmt, "%s", fmt->default_sym->rname);
1617 8
}
1618
1619
/*--------------------------------------------------------------------
1620
 */
1621
1622
void
1623 6162
vcc_Expr_Init(struct vcc *tl)
1624
{
1625
        struct symbol *sym;
1626
1627 6162
        sym = VCC_MkSym(tl, "regsub", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH);
1628 6162
        AN(sym);
1629 6162
        sym->type = STRING;
1630 6162
        sym->eval = vcc_Eval_Regsub;
1631 6162
        sym->eval_priv = NULL;
1632
1633 6162
        sym = VCC_MkSym(tl, "regsuball", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH);
1634 6162
        AN(sym);
1635 6162
        sym->type = STRING;
1636 6162
        sym->eval = vcc_Eval_Regsub;
1637 6162
        sym->eval_priv = sym;
1638
1639 6162
        sym = VCC_MkSym(tl, "true", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH);
1640 6162
        AN(sym);
1641 6162
        sym->type = BOOL;
1642 6162
        sym->eval = vcc_Eval_BoolConst;
1643 6162
        sym->eval_priv = sym;
1644
1645 6162
        sym = VCC_MkSym(tl, "false", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH);
1646 6162
        AN(sym);
1647 6162
        sym->type = BOOL;
1648 6162
        sym->eval = vcc_Eval_BoolConst;
1649 6162
        sym->eval_priv = NULL;
1650
1651 6162
        sym = VCC_MkSym(tl, "default", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH);
1652 6162
        AN(sym);
1653 6162
        sym->type = DEFAULT;
1654 6162
        sym->eval = vcc_Eval_Default;
1655 6162
}