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 22784
vcc_isconst(const struct expr *e)
63
{
64 22784
        AN(e->constant);
65 22784
        return (e->constant & EXPR_CONST);
66
}
67
68
static inline int
69 268628
vcc_islit(const struct expr *e)
70
{
71 268628
        AN(e->constant);
72 268628
        return (e->constant & EXPR_STR_CONST);
73
}
74
75
static const char *
76 268
vcc_utype(vcc_type_t t)
77
{
78 268
        if (t == STRINGS || t->stringform)
79 72
                t = STRING;
80 268
        return (t->name);
81
}
82
83
static vcc_type_t
84 2482856
vcc_stringstype(vcc_type_t t)
85
{
86 2482856
        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 2358528
vcc_new_expr(vcc_type_t fmt)
97
{
98
        struct expr *e;
99
100 2358528
        ALLOC_OBJ(e, EXPR_MAGIC);
101 2358528
        AN(e);
102 2358528
        e->vsb = VSB_new_auto();
103 2358528
        e->fmt = fmt;
104 2358528
        e->constant = EXPR_VAR;
105 2358528
        return (e);
106
}
107
108
static struct expr * v_printflike_(2, 3)
109 625680
vcc_mk_expr(vcc_type_t fmt, const char *str, ...)
110
{
111
        va_list ap;
112
        struct expr *e;
113
114 625680
        e = vcc_new_expr(fmt);
115 625680
        va_start(ap, str);
116 625680
        VSB_vprintf(e->vsb, str, ap);
117 625680
        va_end(ap);
118 625680
        AZ(VSB_finish(e->vsb));
119 625680
        return (e);
120
}
121
122
static void
123 3006120
vcc_delete_expr(struct expr *e)
124
{
125 3006120
        if (e == NULL)
126 651008
                return;
127 2355112
        CHECK_OBJ(e, EXPR_MAGIC);
128 2355112
        VSB_destroy(&e->vsb);
129 2355112
        FREE_OBJ(e);
130 3006120
}
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 200336
vcc_strands_edit(const struct expr *e1, const struct expr *e2)
159
{
160
161 200336
        assert(e2->fmt == STRANDS || e2->fmt == STRINGS);
162
163 200336
        if (e2->fmt == STRANDS)
164 16
                VSB_cat(e1->vsb, VSB_data(e2->vsb));
165 200320
        else if (e2->nstr == 0)
166 0
                VSB_printf(e1->vsb, "vrt_null_strands");
167 200320
        else if (e2->nstr == 1)
168 177256
                VSB_printf(e1->vsb, "TOSTRAND(%s)", VSB_data(e2->vsb));
169
        else {
170 46128
                VSB_printf(e1->vsb, "TOSTRANDS(%d,\v+\n%s\v-)",
171 23064
                   e2->nstr, VSB_data(e2->vsb));
172
        }
173 200336
}
174
175
static struct expr *
176 1321272
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 1321272
        int nl = 1;
181
182 1321272
        (void) tl;
183
184 1321272
        AN(e1);
185 1321272
        e = vcc_new_expr(fmt);
186 15543732
        while (*p != '\0') {
187 14222460
                if (*p != '\v') {
188 11795864
                        if (*p != '\n' || !nl)
189 11795672
                                VSB_putc(e->vsb, *p);
190 11795864
                        nl = (*p == '\n');
191 11795864
                        p++;
192 11795864
                        continue;
193
                }
194 2426596
                assert(*p == '\v');
195 2426596
                switch (*++p) {
196 211276
                case '+': VSB_cat(e->vsb, "\v+"); nl = 0; break;
197 223764
                case '-': VSB_cat(e->vsb, "\v-"); nl = 0; break;
198
                case 'S':
199
                case 's':
200 47800
                        e3 = (*p == 'S' ? e1 : e2);
201 47800
                        AN(e3);
202 47800
                        assert(e1->fmt == STRINGS);
203 47800
                        if (e3->nstr > 1) {
204 104
                                VSB_cat(e->vsb,
205
                                    "\nVRT_STRANDS_string(ctx,\v+\n");
206 104
                                vcc_strands_edit(e, e3);
207 104
                                VSB_cat(e->vsb,
208
                                    "\v-\n)\n");
209 104
                        } else {
210 47696
                                VSB_cat(e->vsb, VSB_data(e3->vsb));
211
                        }
212 47800
                        break;
213
                case 'T':
214
                case 't':
215 200232
                        e3 = (*p == 'T' ? e1 : e2);
216 200232
                        AN(e3);
217 200232
                        vcc_strands_edit(e, e3);
218 200232
                        break;
219
                case '1':
220 1073364
                        VSB_cat(e->vsb, VSB_data(e1->vsb));
221 1073364
                        break;
222
                case '2':
223 670160
                        AN(e2);
224 670160
                        VSB_cat(e->vsb, VSB_data(e2->vsb));
225 670160
                        break;
226
                default:
227 0
                        WRONG("Illegal edit in VCC expression");
228 0
                }
229 2426596
                p++;
230
        }
231 1321272
        AZ(VSB_finish(e->vsb));
232 1321272
        e->t1 = e1->t1;
233 1321272
        e->t2 = e1->t2;
234 1321272
        if (e2 != NULL)
235 670284
                e->t2 = e2->t2;
236 1321272
        vcc_delete_expr(e1);
237 1321272
        vcc_delete_expr(e2);
238 1321272
        return (e);
239
}
240
241
/*--------------------------------------------------------------------
242
 * Expand finished expression into C-source code
243
 */
244
245
static void
246 363548
vcc_expr_fmt(struct vsb *d, int ind, const struct expr *e1)
247
{
248
        char *p;
249
        int i;
250
251 363548
        if (!e1->fmt->noindent) {
252 3234400
                for (i = 0; i < ind; i++)
253 2893144
                        VSB_putc(d, ' ');
254 341256
        }
255 363548
        p = VSB_data(e1->vsb);
256 35571618
        while (*p != '\0') {
257 35208086
                if (*p == '\n') {
258 1069048
                        VSB_putc(d, '\n');
259 1069048
                        if (*++p == '\0')
260 16
                                break;
261 11454024
                        for (i = 0; i < ind; i++)
262 10384992
                                VSB_putc(d, ' ');
263 35208070
                } else if (*p != '\v') {
264 33645238
                        VSB_putc(d, *p++);
265 33645238
                } else {
266 493800
                        switch (*++p) {
267 246900
                        case '+': ind += INDENT; break;
268 246900
                        case '-': ind -= INDENT; break;
269 0
                        default:  WRONG("Illegal format in VCC expression");
270 0
                        }
271 493800
                        p++;
272
                }
273
        }
274 363548
}
275
276
/*--------------------------------------------------------------------
277
 */
278
279
static void
280 278404
vcc_expr_tobool(struct vcc *tl, struct expr **e)
281
{
282
283 278404
        if ((*e)->fmt == BOOL)
284 211324
                return;
285 67080
        if ((*e)->fmt == BACKEND || (*e)->fmt == INT)
286 40
                *e = vcc_expr_edit(tl, BOOL, "(\v1 != 0)", *e, NULL);
287 67040
        else if ((*e)->fmt == DURATION)
288 8
                *e = vcc_expr_edit(tl, BOOL, "(\v1 > 0)", *e, NULL);
289 67032
        else if ((*e)->fmt == STRINGS)
290 67008
                *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 278404
}
297
298
/*--------------------------------------------------------------------
299
 */
300
301
static void
302 217724
vcc_expr_tostring(struct vcc *tl, struct expr **e)
303
{
304
        const char *p;
305 217724
        uint8_t constant = EXPR_VAR;
306
307 217724
        CHECK_OBJ_NOTNULL(*e, EXPR_MAGIC);
308 217724
        assert((*e)->fmt != STRINGS);
309
310 217724
        p = (*e)->fmt->tostring;
311 217724
        if (p != NULL) {
312 217720
                AN(*p);
313 217720
                *e = vcc_expr_edit(tl, STRINGS, p, *e, NULL);
314 217720
                (*e)->constant = constant;
315 217720
                (*e)->nstr = 1;
316 217720
        } else {
317 8
                VSB_printf(tl->sb,
318
                    "Cannot convert %s to STRING.\n",
319 4
                    vcc_utype((*e)->fmt));
320 4
                vcc_ErrWhere2(tl, (*e)->t1, tl->t);
321
        }
322 217724
}
323
324
/*--------------------------------------------------------------------
325
 */
326
327
void v_matchproto_(sym_expr_t)
328 1640
vcc_Eval_Handle(struct vcc *tl, struct expr **e, struct token *t,
329
    struct symbol *sym, vcc_type_t type)
330
{
331
332 1640
        (void)t;
333 1640
        (void)tl;
334 1640
        AN(sym->rname);
335 1640
        AZ(type->stringform);
336
337 1660
        if (sym->type->tostring == NULL &&
338 20
            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 1640
                *e = vcc_mk_expr(sym->type, "%s", sym->rname);
344 1640
                (*e)->constant = EXPR_VAR;
345 1640
                (*e)->nstr = 1;
346 1640
                if ((*e)->fmt == STRING)
347 0
                        (*e)->fmt = STRINGS;
348
        }
349 1640
}
350
351
void v_matchproto_(sym_expr_t)
352 92
vcc_Eval_Sub(struct vcc *tl, struct expr **e, struct token *t,
353
    struct symbol *sym, vcc_type_t type)
354
{
355
356 92
        (void)t;
357 92
        (void)tl;
358 92
        AN(sym->rname);
359 92
        AZ(type->stringform);
360
361 92
        assert (sym->type == SUB);
362
363 92
        if (type == SUB) {
364 80
                *e = vcc_mk_expr(sym->type, "%s", sym->rname);
365 80
                (*e)->constant = EXPR_CONST;
366 80
                return;
367
        }
368
369 24
        VSB_printf(tl->sb, "Symbol '%s' can only be used as a %s expression\n",
370 12
            sym->name, sym->type->name);
371 12
        vcc_ErrWhere(tl, tl->t);
372 92
}
373
374
/*--------------------------------------------------------------------
375
 */
376
377
void v_matchproto_(sym_expr_t)
378 496912
vcc_Eval_Var(struct vcc *tl, struct expr **e, struct token *t,
379
    struct symbol *sym, vcc_type_t type)
380
{
381
382 496912
        (void)type;
383 496912
        vcc_AddUses(tl, t, NULL, sym, XREF_READ);
384 496912
        ERRCHK(tl);
385 496912
        *e = vcc_mk_expr(sym->type, "%s", sym->rname);
386 496912
        (*e)->constant = EXPR_VAR;
387 496912
        (*e)->nstr = 1;
388 496912
        if ((*e)->fmt == STRING)
389 224184
                (*e)->fmt = STRINGS;
390 496912
}
391
392
void v_matchproto_(sym_expr_t)
393 40
vcc_Eval_ProtectedHeader(struct vcc *tl, struct expr **e, struct token *t,
394
    struct symbol *sym, vcc_type_t type)
395
{
396
397 40
        AN(sym);
398 40
        AZ(sym->lorev);
399
400 40
        vcc_Header_Fh(tl, sym);
401 40
        sym->eval = vcc_Eval_Var;
402 40
        vcc_Eval_Var(tl, e, t, sym, type);
403 40
}
404
405
/*--------------------------------------------------------------------
406
 */
407
408
static struct expr *
409 688
vcc_priv_arg(struct vcc *tl, const char *p, struct symbol *sym)
410
{
411
        char buf[64];
412
        struct inifin *ifp;
413 688
        const char *f = NULL;
414
415 688
        AN(sym);
416 688
        AN(sym->vmod_name);
417
418 688
        if (!strcmp(p, "PRIV_VCL"))
419 60
                return (vcc_mk_expr(VOID, "&vmod_priv_%s", sym->vmod_name));
420
421 628
        if (!strcmp(p, "PRIV_CALL")) {
422 88
                bprintf(buf, "vmod_priv_%u", tl->unique++);
423 88
                ifp = New_IniFin(tl);
424 88
                Fh(tl, 0, "static struct vmod_priv %s;\n", buf);
425 88
                VSB_printf(ifp->fin, "\tVRT_priv_fini(ctx, &%s);", buf);
426 88
                return (vcc_mk_expr(VOID, "&%s", buf));
427
        }
428
429 540
        if (!strcmp(p, "PRIV_TASK"))
430 508
                f = "task";
431 32
        else if (!strcmp(p, "PRIV_TOP")) {
432 32
                f = "top";
433 32
                sym->r_methods &= VCL_MET_TASK_C;
434 32
        } else {
435 0
                WRONG("Wrong PRIV_ type");
436
        }
437 540
        AN(f);
438
439 540
        return (vcc_mk_expr(VOID, "VRT_priv_%s(ctx, &VGC_vmod_%s)",
440 540
            f, sym->vmod_name));
441 688
}
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 7268
vcc_do_enum(struct vcc *tl, const char *cfunc, int len, const char *ptr)
456
{
457
        const char *r;
458
459 7268
        (void)tl;
460 7268
        r = strchr(cfunc, '.');
461 7268
        AN(r);
462 7268
        return (vcc_mk_expr(VOID, "*%.*s.enum_%.*s",
463 7268
            (int)(r - cfunc), cfunc, len, ptr));
464
}
465
466
static void
467 15832
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 15832
        if (fa->type == ENUM) {
473 5340
                ExpectErr(tl, ID);
474 5340
                ERRCHK(tl);
475 18272
                VTAILQ_FOREACH(vv, &fa->enums->children, list)
476 18264
                        if (vcc_IdIs(tl->t, vv->value))
477 5332
                                break;
478 5340
                if (vv == NULL) {
479 8
                        VSB_cat(tl->sb, "Wrong enum value.");
480 8
                        VSB_cat(tl->sb, "  Expected one of:\n");
481 40
                        VTAILQ_FOREACH(vv, &fa->enums->children, list)
482 32
                                VSB_printf(tl->sb, "\t%s\n", vv->value);
483 8
                        vcc_ErrWhere(tl, tl->t);
484 8
                        return;
485
                }
486 5332
                fa->result = vcc_do_enum(tl, cfunc, PF(tl->t));
487 5332
                SkipToken(tl, ID);
488 5332
        } else {
489 10492
                if (fa->type == SUB)
490 80
                        tl->subref++;
491 10492
                vcc_expr0(tl, &e2, fa->type);
492 10492
                ERRCHK(tl);
493 10460
                assert(e2->fmt == fa->type);
494 10460
                fa->result = e2;
495
        }
496 15792
        fa->avail = 1;
497 15832
}
498
499
static void
500 10956
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 10956
        CAST_OBJ_NOTNULL(vv, priv, VJSN_VAL_MAGIC);
515 10956
        assert(vjsn_is_array(vv));
516 10956
        vv = VTAILQ_FIRST(&vv->children);
517 10956
        rfmt = VCC_Type(VTAILQ_FIRST(&vv->children)->value);
518 10956
        AN(rfmt);
519 10956
        vv = VTAILQ_NEXT(vv, list);
520 10956
        cfunc = vv->value;
521 10956
        vv = VTAILQ_NEXT(vv, list);
522 10956
        sa = vv->value;
523 10956
        if (*sa == '\0') {
524 9344
                sa = NULL;
525 9344
        }
526 10956
        vv = VTAILQ_NEXT(vv, list);
527 10956
        if (sym->kind == SYM_METHOD) {
528 3096
                if (*e == NULL) {
529 8
                        VSB_cat(tl->sb, "Syntax error.");
530 8
                        tl->err = 1;
531 8
                        return;
532
                }
533 3088
                vcc_NextToken(tl);
534 3088
                AZ(extra);
535 3088
                AN((*e)->instance);
536 3088
                extra = (*e)->instance->rname;
537 3088
        }
538 10948
        tf = VTAILQ_PREV(tl->t, tokenhead, list);
539 10948
        SkipToken(tl, '(');
540 10948
        if (extra == NULL) {
541 7056
                extra = "";
542 7056
                extra_sep = "";
543 7056
        } else {
544 3892
                AN(*extra);
545 3892
                extra_sep = ", ";
546
        }
547 10948
        VTAILQ_INIT(&head);
548 36728
        for (;vv != NULL; vv = VTAILQ_NEXT(vv, list)) {
549 25780
                assert(vjsn_is_array(vv));
550 25780
                fa = calloc(1, sizeof *fa);
551 25780
                AN(fa);
552 25780
                VTAILQ_INSERT_TAIL(&head, fa, list);
553
554 25780
                vvp = VTAILQ_FIRST(&vv->children);
555 25780
                if (!memcmp(vvp->value, "PRIV_", 5)) {
556 688
                        fa->result = vcc_priv_arg(tl, vvp->value, sym);
557 688
                        vvp = VTAILQ_NEXT(vvp, list);
558 688
                        if (vvp != NULL)
559 12
                                fa->name = vvp->value;
560 688
                        continue;
561
                }
562 25092
                fa->type = VCC_Type(vvp->value);
563 25092
                AN(fa->type);
564 25092
                vvp = VTAILQ_NEXT(vvp, list);
565 25092
                if (vvp != NULL) {
566 23520
                        fa->name = vvp->value;
567 23520
                        vvp = VTAILQ_NEXT(vvp, list);
568 23520
                        if (vvp != NULL) {
569 17276
                                fa->val = vvp->value;
570 17276
                                vvp = VTAILQ_NEXT(vvp, list);
571 17276
                                if (vvp != NULL) {
572 14060
                                        fa->enums = vvp;
573 14060
                                        vvp = VTAILQ_NEXT(vvp, list);
574 14060
                                }
575 17276
                        }
576 23520
                }
577 25092
                if (sa != NULL && vvp != NULL && vjsn_is_true(vvp)) {
578 7284
                        fa->optional = 1;
579 7284
                        vvp = VTAILQ_NEXT(vvp, list);
580 7284
                }
581 25092
                AZ(vvp);
582 25092
        }
583
584 17660
        VTAILQ_FOREACH(fa, &head, list) {
585 15836
                if (tl->t->tok == ')')
586 356
                        break;
587 15480
                if (fa->result != NULL)
588 492
                        continue;
589 14988
                if (tl->t->tok == ID) {
590 11032
                        t1 = VTAILQ_NEXT(tl->t, list);
591 11032
                        if (t1->tok == '=')
592 3008
                                break;
593 8024
                }
594 11980
                vcc_do_arg(tl, cfunc, fa);
595 11980
                if (tl->err)
596 80
                        VSB_printf(tl->sb, "Expected argument: %s %s\n\n",
597 40
                            fa->type->name,
598 40
                            fa->name ? fa->name : "(unnamed argument)");
599 11980
                ERRCHK(tl);
600 11940
                if (tl->t->tok == ')')
601 5720
                        break;
602 6220
                SkipToken(tl, ',');
603 6220
        }
604 11760
        while (tl->t->tok == ID) {
605 12536
                VTAILQ_FOREACH(fa, &head, list) {
606 12532
                        if (fa->name == NULL)
607 48
                                continue;
608 12484
                        if (vcc_IdIs(tl->t, fa->name))
609 3856
                                break;
610 8628
                }
611 3860
                if (fa == NULL) {
612 8
                        VSB_printf(tl->sb, "Unknown argument '%.*s'\n",
613 4
                            PF(tl->t));
614 4
                        vcc_ErrWhere(tl, tl->t);
615 4
                        return;
616
                }
617 3856
                if (fa->result != NULL) {
618 4
                        AN(fa->name);
619 8
                        VSB_printf(tl->sb, "Argument '%s' already used\n",
620 4
                            fa->name);
621 4
                        vcc_ErrWhere(tl, tl->t);
622 4
                        return;
623
                }
624 3852
                vcc_NextToken(tl);
625 3852
                SkipToken(tl, '=');
626 3852
                vcc_do_arg(tl, cfunc, fa);
627 3852
                ERRCHK(tl);
628 3852
                if (tl->t->tok == ')')
629 3000
                        break;
630 852
                SkipToken(tl, ',');
631
        }
632
633 10900
        if (sa != NULL)
634 3192
                e1 = vcc_mk_expr(rfmt, "%s(ctx%s%s,\v+\n&(%s)\v+ {\n",
635 1596
                    cfunc, extra_sep, extra, sa);
636
        else
637 18608
                e1 = vcc_mk_expr(rfmt, "%s(ctx%s%s\v+",
638 9304
                    cfunc, extra_sep, extra);
639 10900
        n = 0;
640 36548
        VTAILQ_FOREACH_SAFE(fa, &head, list, fa2) {
641 25648
                n++;
642 25648
                if (fa->optional) {
643 7260
                        AN(fa->name);
644 7260
                        bprintf(ssa, "\v1.valid_%s = %d,\n",
645
                            fa->name, fa->avail);
646 7260
                        e1 = vcc_expr_edit(tl, e1->fmt, ssa, e1, NULL);
647 7260
                }
648 25648
                if (fa->result == NULL && fa->type == ENUM && fa->val != NULL)
649 1936
                        fa->result = vcc_do_enum(tl, cfunc, strlen(fa->val), fa->val);
650 25648
                if (fa->result == NULL && fa->val != NULL)
651 2372
                        fa->result = vcc_mk_expr(fa->type, "%s", fa->val);
652 25648
                if (fa->result != NULL && sa != NULL) {
653 3464
                        if (fa->name)
654 3380
                                bprintf(ssa, "\v1.%s = \v2,\n", fa->name);
655
                        else
656 84
                                bprintf(ssa, "\v1.arg%d = \v2,\n", n);
657 3464
                        e1 = vcc_expr_edit(tl, e1->fmt, ssa, e1, fa->result);
658 25648
                } else if (fa->result != NULL) {
659 34608
                        e1 = vcc_expr_edit(tl, e1->fmt, "\v1,\n\v2",
660 17304
                            e1, fa->result);
661 22184
                } else if (!fa->optional) {
662 4
                        if (fa->name)
663 8
                                VSB_printf(tl->sb, "Argument '%s' missing\n",
664 4
                                    fa->name);
665
                        else
666 0
                                VSB_printf(tl->sb, "Argument %d missing\n", n);
667 4
                        vcc_ErrWhere(tl, tl->t);
668 4
                }
669 25648
                free(fa);
670 25648
        }
671 10900
        if (sa != NULL) {
672 1596
                *e = vcc_expr_edit(tl, e1->fmt, "\v1\v-\n}\v-\n)", e1, NULL);
673 1596
        } else {
674 9304
                *e = vcc_expr_edit(tl, e1->fmt, "\v1\v-\n)", e1, NULL);
675
        }
676 10900
        SkipToken(tl, ')');
677 10896
        vcc_AddUses(tl, tf, NULL, sym, XREF_READ);
678 10956
}
679
680
681
/*--------------------------------------------------------------------
682
 */
683
684
void
685 804
vcc_Eval_Func(struct vcc *tl, const struct vjsn_val *spec,
686
    const char *extra, struct symbol *sym)
687
{
688 804
        struct expr *e = NULL;
689
690 804
        vcc_func(tl, &e, spec, extra, sym);
691 804
        if (tl->err)
692 0
                VSB_cat(tl->sb, "While compiling function call:\n");
693 804
        ERRCHK(tl);
694 804
        vcc_expr_fmt(tl->fb, tl->indent, e);
695 804
        VSB_cat(tl->fb, ";\n");
696 804
        vcc_delete_expr(e);
697 804
}
698
699
/*--------------------------------------------------------------------
700
 */
701
702
void v_matchproto_(sym_expr_t)
703 8284
vcc_Eval_SymFunc(struct vcc *tl, struct expr **e, struct token *t,
704
    struct symbol *sym, vcc_type_t fmt)
705
{
706
707 8284
        (void)t;
708 8284
        (void)fmt;
709 8284
        assert(sym->kind == SYM_FUNC || sym->kind == SYM_METHOD);
710 8284
        AN(sym->eval_priv);
711
712 8284
        vcc_func(tl, e, sym->eval_priv, sym->extra, sym);
713 8284
        ERRCHK(tl);
714 8244
        if ((*e)->fmt == STRING) {
715 3004
                (*e)->fmt = STRINGS;
716 3004
                (*e)->nstr = 1;
717 3004
        }
718 8284
}
719
720
/*--------------------------------------------------------------------
721
 */
722
723
static void
724 93632
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 93632
        assert(fmt != VOID);
731 93632
        if (fmt == BYTES) {
732 192
                vcc_ByteVal(tl, &vi);
733 192
                ERRCHK(tl);
734 184
                e1 = vcc_mk_expr(BYTES, "%ju", (intmax_t)vi);
735 184
        } else {
736 93440
                t = tl->t;
737 93440
                vcc_NextToken(tl);
738 93440
                if (tl->t->tok == ID) {
739 23140
                        e1 = vcc_mk_expr(DURATION, "%s%.3f * %g",
740 23140
                            sign, t->num, vcc_DurationUnit(tl));
741 23140
                        ERRCHK(tl);
742 93436
                } else if (fmt == REAL || t->tok == FNUM) {
743 616
                        e1 = vcc_mk_expr(REAL, "%s%.3f", sign, t->num);
744 616
                } else {
745 69684
                        e1 = vcc_mk_expr(INT, "%s%.0f", sign, t->num);
746
                }
747
        }
748 93620
        e1->constant = EXPR_CONST;
749 93620
        *e = e1;
750 93632
}
751
752
/*--------------------------------------------------------------------
753
 * SYNTAX:
754
 *    Expr5:
755
 *      '(' ExprCor ')'
756
 *      symbol
757
 *      CNUM
758
 *      FNUM
759
 *      CSTR
760
 *      CBLOB
761
 */
762
763
static void
764 1032804
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 1032804
        sign = "";
772 1032804
        *e = NULL;
773 1032804
        if (tl->t->tok == '(') {
774 11264
                SkipToken(tl, '(');
775 11264
                vcc_expr_cor(tl, &e2, fmt);
776 11264
                ERRCHK(tl);
777 11264
                SkipToken(tl, ')');
778 11264
                if (e2->fmt == STRINGS)
779 24
                        *e = e2;
780
                else
781 11240
                        *e = vcc_expr_edit(tl, e2->fmt, "(\v1)", e2, NULL);
782 11264
                return;
783
        }
784 1021540
        switch (tl->t->tok) {
785
        case ID:
786 519256
                t = tl->t;
787 519256
                t1 = vcc_PeekToken(tl);
788 519256
                AN(t1);
789 519256
                sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_NONE,
790
                    SYMTAB_PARTIAL_NOERR, XREF_REF);
791 519256
                if (sym == NULL && fmt->global_pfx != NULL && t1->tok != '.') {
792 36
                        sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_NONE,
793
                            SYMTAB_CREATE, XREF_REF);
794 36
                        ERRCHK(tl);
795 28
                        AN(sym);
796 28
                        VCC_GlobalSymbol(sym, fmt);
797 28
                }
798 519248
                ERRCHK(tl);
799 519248
                if (sym == NULL)
800 48
                        AZ(VCC_SymbolGet(tl, SYM_MAIN, SYM_NONE,
801
                            SYMTAB_PARTIAL, XREF_REF));
802 519248
                ERRCHK(tl);
803 519200
                AN(sym);
804 519200
                if (sym->kind == SYM_INSTANCE) {
805 3096
                        AZ(*e);
806 3096
                        *e = vcc_new_expr(sym->type);
807 3096
                        (*e)->instance = sym;
808 3096
                        return;
809
                }
810 516104
                if (sym->kind == SYM_FUNC && sym->type == VOID) {
811 4
                        VSB_cat(tl->sb, "Function returns VOID:\n");
812 4
                        vcc_ErrWhere(tl, tl->t);
813 4
                        return;
814
                }
815 516100
                if (sym->eval != NULL) {
816 516088
                        AN(sym->eval);
817 516088
                        AZ(*e);
818 516088
                        sym->eval(tl, e, t, sym, fmt);
819 516088
                        if (tl->err) {
820 64
                                VSB_cat(tl->sb,
821
                                    "While compiling function call:\n\n");
822 64
                                vcc_ErrWhere2(tl, t, tl->t);
823 64
                        }
824 516088
                        ERRCHK(tl);
825
                        /* Unless asked for a HEADER, fold to string here */
826 516024
                        if (*e && fmt != HEADER && (*e)->fmt == HEADER) {
827 137280
                                vcc_expr_tostring(tl, e);
828 137280
                                ERRCHK(tl);
829 137280
                        }
830 516024
                        return;
831
                }
832 24
                VSB_printf(tl->sb,
833
                    "Symbol '%.*s' type (%s) cannot be used in expression.\n",
834 12
                    PF(t), sym->kind->name);
835 12
                vcc_ErrWhere(tl, t);
836 12
                if (sym->def_b != NULL) {
837 4
                        VSB_cat(tl->sb, "That symbol was defined here:\n");
838 4
                        vcc_ErrWhere(tl, sym->def_b);
839 4
                }
840 12
                return;
841
        case CSTR:
842 408592
                assert(fmt != VOID);
843 408592
                if (fmt == IP) {
844 120
                        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 4
                                VSB_cat(tl->sb,
852
                                    "Cannot convert to an IP address: ");
853 4
                                vcc_ErrToken(tl, tl->t);
854 4
                                vcc_ErrWhere(tl, tl->t);
855 4
                                return;
856
                        }
857 232
                        Resolve_Sockaddr(tl, tl->t->dec, "80",
858
                            &ip, NULL, &ip, NULL, NULL, 1,
859 116
                            tl->t, "IP constant");
860 116
                        ERRCHK(tl);
861 104
                        e1 = vcc_mk_expr(IP, "%s", ip);
862 104
                        ERRCHK(tl);
863 408576
                } else if (fmt == REGEX) {
864 33832
                        e1 = vcc_new_expr(REGEX);
865 33832
                        vcc_regexp(tl, e1->vsb);
866 33832
                        AZ(VSB_finish(e1->vsb));
867 33832
                } else {
868 374640
                        e1 = vcc_new_expr(STRINGS);
869 374640
                        EncToken(e1->vsb, tl->t);
870 374640
                        AZ(VSB_finish(e1->vsb));
871 374640
                        e1->constant |= EXPR_STR_CONST;
872 374640
                        e1->nstr = 1;
873
                }
874 408576
                e1->t1 = tl->t;
875 408576
                e1->constant |= EXPR_CONST;
876 408576
                vcc_NextToken(tl);
877 408576
                *e = e1;
878 408576
                return;
879
        case '-':
880 248
                if (fmt != INT &&
881 64
                    fmt != REAL &&
882 16
                    fmt != DURATION &&
883 4
                    fmt != STRINGS)
884 0
                        break;
885 244
                vcc_NextToken(tl);
886 244
                if (tl->t->tok != FNUM && tl->t->tok != CNUM) {
887 36
                        vcc_expr_cor(tl, &e1, fmt);
888 36
                        ERRCHK(tl);
889 36
                        *e = vcc_expr_edit(tl, e1->fmt, "-(\v1)", e1, NULL);
890 36
                        return;
891
                }
892 208
                sign = "-";
893
                /* FALLTHROUGH */
894
        case FNUM:
895
        case CNUM:
896 93632
                vcc_number(tl, e, fmt, sign);
897 93632
                return;
898
        case CBLOB:
899 8
                e1 = vcc_new_expr(BLOB);
900 8
                VSB_printf(e1->vsb, "%s", tl->t->dec);
901 8
                AZ(VSB_finish(e1->vsb));
902 8
                e1->constant |= EXPR_STR_CONST;
903 8
                e1->t1 = tl->t;
904 8
                vcc_NextToken(tl);
905 8
                *e = e1;
906 8
                return;
907
        default:
908 16
                break;
909
        }
910 16
        VSB_cat(tl->sb, "Unknown token ");
911 16
        vcc_ErrToken(tl, tl->t);
912 16
        VSB_printf(tl->sb, " when looking for %s\n\n", vcc_utype(fmt));
913 16
        vcc_ErrWhere(tl, tl->t);
914 1032804
}
915
916
/*--------------------------------------------------------------------
917
 * SYNTAX:
918
 *    Expr4:
919
 *      Expr5 [ '.' (type_attribute | type_method()) ]*
920
 */
921
922
void
923 11188
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 11188
        (void)t;
929 11188
        impl = VCC_Type_EvalMethod(tl, sym);
930 11188
        ERRCHK(tl);
931 11188
        AN(impl);
932 11188
        *e = vcc_expr_edit(tl, fmt, impl, *e, NULL);
933 11188
}
934
935
static void
936 1032804
vcc_expr4(struct vcc *tl, struct expr **e, vcc_type_t fmt)
937
{
938
        struct symbol *sym;
939
940 1032804
        *e = NULL;
941 1032804
        vcc_expr5(tl, e, fmt);
942 1032804
        ERRCHK(tl);
943 1032612
        AN(*e);
944 1046888
        while (tl->t->tok == '.') {
945 14288
                vcc_NextToken(tl);
946 14288
                ExpectErr(tl, ID);
947
948 14284
                sym = VCC_TypeSymbol(tl, SYM_METHOD, (*e)->fmt);
949 14284
                if (sym == NULL) {
950 8
                        VSB_cat(tl->sb, "Unknown property ");
951 8
                        vcc_ErrToken(tl, tl->t);
952 8
                        VSB_printf(tl->sb, " for type %s\n", (*e)->fmt->name);
953 8
                        vcc_ErrWhere(tl, tl->t);
954 8
                        return;
955
                }
956
957 14276
                AN(sym->eval);
958 14276
                sym->eval(tl, e, tl->t, sym, sym->type);
959 14276
                ERRCHK(tl);
960 14276
                if ((*e)->fmt == STRING) {
961 11144
                        (*e)->fmt = STRINGS;
962 11144
                        (*e)->nstr = 1;
963 11144
                }
964
        }
965 1032804
}
966
967
/*--------------------------------------------------------------------
968
 * SYNTAX:
969
 *    ExprMul:
970
 *      Expr4 { {'*'|'/'|'%'} Expr4 } *
971
 */
972
973
static void
974 997492
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 997492
        *e = NULL;
982 997492
        vcc_expr4(tl, e, fmt);
983 997492
        ERRCHK(tl);
984 997316
        AN(*e);
985
986 997464
        while (tl->t->tok == '*' || tl->t->tok == '/' || tl->t->tok == '%') {
987 164
                if (tl->t->tok == '%' && ((*e)->fmt != INT)) {
988 4
                        VSB_cat(tl->sb, "Operator % only possible on INT.\n");
989 4
                        vcc_ErrWhere(tl, tl->t);
990 4
                        return;
991
                }
992 160
                f2 = (*e)->fmt->multype;
993 160
                if (f2 == NULL) {
994 8
                        VSB_printf(tl->sb,
995
                            "Operator %.*s not possible on type %s.\n",
996 4
                            PF(tl->t), vcc_utype((*e)->fmt));
997 4
                        vcc_ErrWhere(tl, tl->t);
998 4
                        return;
999
                }
1000 156
                tk = tl->t;
1001 156
                vcc_NextToken(tl);
1002 156
                vcc_expr4(tl, &e2, f2);
1003 156
                ERRCHK(tl);
1004 156
                if (e2->fmt != INT && e2->fmt != f2) {
1005 16
                        VSB_printf(tl->sb, "%s %.*s %s not possible.\n",
1006 8
                            vcc_utype((*e)->fmt), PF(tk), vcc_utype(e2->fmt));
1007 8
                        vcc_ErrWhere(tl, tk);
1008 8
                        return;
1009
                }
1010 148
                bprintf(buf, "(\v1%c\v2)", tk->tok);
1011 148
                *e = vcc_expr_edit(tl, (*e)->fmt, buf, *e, e2);
1012
        }
1013 997492
}
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 728752
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 728752
        *e = NULL;
1055 728752
        vcc_expr_mul(tl, e, fmt);
1056 728752
        ERRCHK(tl);
1057
1058 997256
        while (tl->t->tok == '+' || tl->t->tok == '-') {
1059 268740
                tk = tl->t;
1060 4296824
                for (ap = vcc_adds; ap->op != EOI; ap++)
1061 4028384
                        if (tk->tok == ap->op && (*e)->fmt == ap->a)
1062 300
                                break;
1063 268740
                vcc_NextToken(tl);
1064 268740
                if (ap->op == EOI && fmt == STRINGS)
1065 2008
                        vcc_expr_mul(tl, &e2, STRINGS);
1066
                else
1067 266732
                        vcc_expr_mul(tl, &e2, (*e)->fmt);
1068 268740
                ERRCHK(tl);
1069
1070 4297312
                for (ap = vcc_adds; ap->op != EOI; ap++)
1071 4028832
                        if (tk->tok == ap->op && (*e)->fmt == ap->a &&
1072 4028832
                            e2->fmt == ap->b)
1073 260
                                break;
1074
1075 268740
                if (ap->op == '+') {
1076 180
                        *e = vcc_expr_edit(tl, ap->fmt, "(\v1 + \v2)", *e, e2);
1077 268740
                } else if (ap->op == '-') {
1078 80
                        *e = vcc_expr_edit(tl, ap->fmt, "(\v1 - \v2)", *e, e2);
1079 268620
                } else if (tk->tok == '+' &&
1080 268476
                    ((*e)->fmt == STRINGS || fmt == STRINGS)) {
1081 268436
                        if ((*e)->fmt != STRINGS)
1082 20
                                vcc_expr_tostring(tl, e);
1083 268436
                        if (e2->fmt != STRINGS)
1084 66824
                                vcc_expr_tostring(tl, &e2);
1085 268436
                        if (vcc_islit(*e) && vcc_isconst(e2)) {
1086 192
                                lit = vcc_islit(e2);
1087 384
                                *e = vcc_expr_edit(tl, STRINGS,
1088 192
                                    "\v1\n\v2", *e, e2);
1089 192
                                (*e)->constant = EXPR_CONST;
1090 192
                                (*e)->nstr = 1;
1091 192
                                if (lit)
1092 192
                                        (*e)->constant |= EXPR_STR_CONST;
1093 192
                        } else {
1094 268244
                                n = (*e)->nstr + e2->nstr;
1095 536488
                                *e = vcc_expr_edit(tl, STRINGS,
1096 268244
                                    "\v1,\n\v2", *e, e2);
1097 268244
                                (*e)->constant = EXPR_VAR;
1098 268244
                                (*e)->nstr = n;
1099
                        }
1100 268436
                } else {
1101 88
                        VSB_printf(tl->sb, "%s %.*s %s not possible.\n",
1102 44
                            vcc_utype((*e)->fmt), PF(tk), vcc_utype(e2->fmt));
1103 44
                        vcc_ErrWhere2(tl, tk, tl->t);
1104 44
                        return;
1105
                }
1106
        }
1107 728752
}
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 44952
cmp_simple(struct vcc *tl, struct expr **e, const struct cmps *cp)
1135
{
1136
        struct expr *e2;
1137
        struct token *tk;
1138
1139 44952
        tk = tl->t;
1140 44952
        vcc_NextToken(tl);
1141 44952
        vcc_expr_add(tl, &e2, (*e)->fmt);
1142 44952
        ERRCHK(tl);
1143
1144 44940
        if (e2->fmt != (*e)->fmt) {
1145 16
                VSB_printf(tl->sb,
1146
                    "Comparison of different types: %s '%.*s' %s\n",
1147 8
                    vcc_utype((*e)->fmt), PF(tk), vcc_utype(e2->fmt));
1148 8
                vcc_ErrWhere(tl, tk);
1149 8
        } else
1150 44932
                *e = vcc_expr_edit(tl, BOOL, cp->emit, *e, e2);
1151 44952
}
1152
1153
static void v_matchproto_(cmp_f)
1154 33652
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 33652
        *e = vcc_expr_edit(tl, STRING, "\vS", *e, NULL);
1161 33652
        vcc_NextToken(tl);
1162 33652
        t1 = tl->t;
1163 33652
        vcc_expr4(tl, &e2, REGEX);
1164 33652
        ERRCHK(tl);
1165 33636
        vcc_expr_typecheck(tl, &e2, REGEX, t1);
1166 33636
        ERRCHK(tl);
1167 33632
        bprintf(buf, "%sVRT_re_match(ctx, \v1, \v2)", cp->emit);
1168 33632
        *e = vcc_expr_edit(tl, BOOL, buf, *e, e2);
1169 33652
}
1170
1171
static void v_matchproto_(cmp_f)
1172 172
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 172
        vcc_NextToken(tl);
1179 172
        t1 = tl->t;
1180 172
        vcc_expr4(tl, &e2, ACL);
1181 172
        ERRCHK(tl);
1182 160
        vcc_expr_typecheck(tl, &e2, ACL, t1);
1183 160
        ERRCHK(tl);
1184 148
        bprintf(buf, "%sVRT_acl_match(ctx, \v1, \v2)", cp->emit);
1185 148
        *e = vcc_expr_edit(tl, BOOL, buf, e2, *e);
1186 172
}
1187
1188
static void v_matchproto_(cmp_f)
1189 157272
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 157272
        tk = tl->t;
1196 157272
        vcc_NextToken(tl);
1197 157272
        vcc_expr_add(tl, &e2, STRINGS);
1198 157272
        ERRCHK(tl);
1199 157272
        if (vcc_stringstype(e2->fmt) != STRINGS) {
1200 24
                VSB_printf(tl->sb,
1201
                    "Comparison of different types: %s '%.*s' %s\n",
1202 12
                    vcc_utype((*e)->fmt), PF(tk), vcc_utype(e2->fmt));
1203 12
                vcc_ErrWhere(tl, tk);
1204 157272
        } else if ((*e)->nstr == 1 && e2->nstr == 1) {
1205 157136
                bprintf(buf, "(%s VRT_strcmp(\v1, \v2))", cp->emit);
1206 157136
                *e = vcc_expr_edit(tl, BOOL, buf, *e, e2);
1207 157136
        } else {
1208 124
                bprintf(buf, "(%s VRT_CompareStrands(\vT, \vt))", cp->emit);
1209 124
                *e = vcc_expr_edit(tl, BOOL, buf, *e, e2);
1210
        }
1211 157272
}
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 526528
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 526528
        *e = NULL;
1268 526528
        vcc_expr_add(tl, e, fmt);
1269 526528
        ERRCHK(tl);
1270 526304
        tk = tl->t;
1271
1272 26381336
        for (cp = vcc_cmps; cp->fmt != VOID; cp++) {
1273 26091080
                if (tl->t->tok != cp->token)
1274 23765496
                        continue;
1275 2325584
                if (vcc_stringstype((*e)->fmt) != cp->fmt)
1276 2089536
                        continue;
1277 236048
                AN(cp->func);
1278 236048
                cp->func(tl, e, cp);
1279 236048
                return;
1280
        }
1281
1282 290256
        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 16
                VSB_printf(tl->sb, "Operator %.*s not possible on %s\n",
1292 8
                    PF(tl->t), vcc_utype((*e)->fmt));
1293 8
                vcc_ErrWhere(tl, tl->t);
1294 8
                return;
1295
        default:
1296 290248
                break;
1297
        }
1298 526528
}
1299
1300
/*--------------------------------------------------------------------
1301
 * SYNTAX:
1302
 *    ExprNot:
1303
 *      '!' ExprCmp
1304
 */
1305
1306
static void
1307 526528
vcc_expr_not(struct vcc *tl, struct expr **e, vcc_type_t fmt)
1308
{
1309
        struct token *tk;
1310
1311 526528
        *e = NULL;
1312 526528
        tk = tl->t;
1313 526528
        if (tl->t->tok == '!')
1314 22516
                vcc_NextToken(tl);
1315 526528
        vcc_expr_cmp(tl, e, fmt);
1316 526528
        ERRCHK(tl);
1317 526220
        if (tk->tok != '!')
1318 503708
                return;
1319 22512
        vcc_expr_tobool(tl, e);
1320 22512
        ERRCHK(tl);
1321 22512
        if ((*e)->fmt != BOOL) {
1322 4
                VSB_cat(tl->sb, "'!' must be followed by BOOL, found ");
1323 4
                VSB_printf(tl->sb, "%s.\n", vcc_utype((*e)->fmt));
1324 4
                vcc_ErrWhere2(tl, tk, tl->t);
1325 4
        } else {
1326 22508
                *e = vcc_expr_edit(tl, BOOL, "!(\v1)", *e, NULL);
1327
        }
1328 526528
}
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 775376
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 775376
        *e = NULL;
1347 775376
        tk = tl->t;
1348 775376
        up(tl, e, fmt);
1349 775376
        ERRCHK(tl);
1350 774744
        if (tl->t->tok != ourtok)
1351 708016
                return;
1352 66728
        vcc_expr_tobool(tl, e);
1353 66728
        ERRCHK(tl);
1354 66728
        if ((*e)->fmt != BOOL) {
1355 16
                VSB_printf(tl->sb,
1356
                    "'%s' must be preceded by BOOL,"
1357 8
                    " found %s.\n", tokstr, vcc_utype((*e)->fmt));
1358 8
                vcc_ErrWhere2(tl, tk, tl->t);
1359 8
                return;
1360
        }
1361 66720
        *e = vcc_expr_edit(tl, BOOL, "(\v+\n\v1", *e, NULL);
1362 211132
        while (tl->t->tok == ourtok) {
1363 144420
                vcc_NextToken(tl);
1364 144420
                tk = tl->t;
1365 144420
                up(tl, &e2, fmt);
1366 144420
                ERRCHK(tl);
1367 144420
                vcc_expr_tobool(tl, &e2);
1368 144420
                ERRCHK(tl);
1369 144420
                if (e2->fmt != BOOL) {
1370 16
                        VSB_printf(tl->sb,
1371
                            "'%s' must be followed by BOOL,"
1372 8
                            " found %s.\n", tokstr, vcc_utype(e2->fmt));
1373 8
                        vcc_ErrWhere2(tl, tk, tl->t);
1374 8
                        vcc_delete_expr(e2);
1375 8
                        return;
1376
                }
1377 144412
                bprintf(buf, "\v1\v-\n%s\v+\n\v2", tokstr);
1378 144412
                *e = vcc_expr_edit(tl, BOOL, buf, *e, e2);
1379
        }
1380 66712
        *e = vcc_expr_edit(tl, BOOL, "\v1\v-\n)", *e, NULL);
1381 775376
}
1382
1383
/*--------------------------------------------------------------------
1384
 * SYNTAX:
1385
 *    ExprCand:
1386
 *      ExprNot { '&&' ExprNot } *
1387
 */
1388
1389
static void
1390 393268
vcc_expr_cand(struct vcc *tl, struct expr **e, vcc_type_t fmt)
1391
{
1392
1393 393268
        vcc_expr_bin_bool(tl, e, fmt, T_CAND, vcc_expr_not, "&&");
1394 393268
}
1395
1396
/*--------------------------------------------------------------------
1397
 * SYNTAX:
1398
 *    ExprCOR:
1399
 *      ExprCand { '||' ExprCand } *
1400
 */
1401
1402
static void
1403 382108
vcc_expr_cor(struct vcc *tl, struct expr **e, vcc_type_t fmt)
1404
{
1405
1406 382108
        vcc_expr_bin_bool(tl, e, fmt, T_COR, vcc_expr_cand, "||");
1407 382108
}
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 370808
vcc_expr0(struct vcc *tl, struct expr **e, vcc_type_t fmt)
1416
{
1417
        struct token *t1;
1418
1419 370808
        assert(fmt != VOID);
1420 370808
        assert(fmt != STRINGS);
1421 370808
        *e = NULL;
1422 370808
        t1 = tl->t;
1423 370808
        if (fmt->stringform)
1424 113832
                vcc_expr_cor(tl, e, STRINGS);
1425
        else
1426 256976
                vcc_expr_cor(tl, e, fmt);
1427 370808
        ERRCHK(tl);
1428
1429 370480
        if ((*e)->fmt == fmt)
1430 189724
                return;
1431
1432 180756
        if ((*e)->fmt != STRINGS && fmt->stringform)
1433 13596
                vcc_expr_tostring(tl, e);
1434
1435 180756
        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 180756
        if (fmt == BODY && !(*e)->fmt->bodyform)
1444 4
                vcc_expr_tostring(tl, e);
1445
1446 180756
        if (fmt == BODY && (*e)->fmt->bodyform) {
1447 22292
                if ((*e)->fmt == STRINGS)
1448 22276
                        *e = vcc_expr_edit(tl, BODY, "STRING, 0, \vT", *e, NULL);
1449 16
                else if ((*e)->fmt == BLOB)
1450 16
                        *e = vcc_expr_edit(tl, BODY, "BLOB, 0, \v1", *e, NULL);
1451
                else
1452 0
                        WRONG("Unhandled bodyform");
1453 22292
        }
1454
1455 180756
        if ((*e)->fmt == STRINGS && fmt->stringform) {
1456 113704
                if (fmt == STRING)
1457 14148
                        *e = vcc_expr_edit(tl, STRING, "\vS", *e, NULL);
1458 99556
                else if (fmt == STRANDS)
1459 99556
                        *e = vcc_expr_edit(tl, STRANDS, "\vT", (*e), NULL);
1460
                else
1461 0
                        WRONG("Unhandled stringform");
1462 113704
        }
1463
1464 180756
        if (fmt == BOOL) {
1465 44744
                vcc_expr_tobool(tl, e);
1466 44744
                ERRCHK(tl);
1467 44744
        }
1468
1469 180756
        vcc_expr_typecheck(tl, e, fmt, t1);
1470 370808
}
1471
1472
static void
1473 214552
vcc_expr_typecheck(struct vcc *tl, struct expr **e, vcc_type_t fmt,
1474
    struct token *t1)
1475
{
1476
1477 214552
        assert(fmt != VOID);
1478 214552
        assert(fmt != STRINGS);
1479
1480 214552
        if (fmt != (*e)->fmt)  {
1481 72
                VSB_printf(tl->sb, "Expression has type %s, expected %s\n",
1482 36
                    vcc_utype((*e)->fmt), vcc_utype(fmt));
1483 36
                vcc_ErrWhere2(tl, t1, tl->t);
1484 36
        }
1485 214552
}
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 359876
vcc_Expr(struct vcc *tl, vcc_type_t fmt)
1496
{
1497 359876
        struct expr *e = NULL;
1498
1499 359876
        assert(fmt != VOID);
1500 359876
        assert(fmt != STRINGS);
1501 359876
        vcc_expr0(tl, &e, fmt);
1502 359876
        ERRCHK(tl);
1503 359564
        assert(e->fmt == fmt);
1504
1505 359564
        vcc_expr_fmt(tl->fb, tl->indent, e);
1506 359564
        VSB_cat(tl->fb, "\n");
1507 359564
        vcc_delete_expr(e);
1508 359876
}
1509
1510
/*--------------------------------------------------------------------
1511
 */
1512
1513
void v_matchproto_(sym_act_f)
1514 1868
vcc_Act_Call(struct vcc *tl, struct token *t, struct symbol *sym)
1515
{
1516
1517
        struct expr *e;
1518
1519 1868
        e = NULL;
1520 1868
        vcc_func(tl, &e, sym->eval_priv, sym->extra, sym);
1521 1868
        if (!tl->err) {
1522 1848
                vcc_expr_fmt(tl->fb, tl->indent, e);
1523 1848
                SkipToken(tl, ';');
1524 1848
                VSB_cat(tl->fb, ";\n");
1525 1868
        } else if (t != tl->t) {
1526 20
                VSB_cat(tl->sb, "While compiling function call:\n\n");
1527 20
                vcc_ErrWhere2(tl, t, tl->t);
1528 20
        }
1529 1868
        vcc_delete_expr(e);
1530 1868
}
1531
1532
void v_matchproto_(sym_act_f)
1533 1336
vcc_Act_Obj(struct vcc *tl, struct token *t, struct symbol *sym)
1534
{
1535
1536 1336
        struct expr *e = NULL;
1537
1538 1336
        assert(sym->kind == SYM_INSTANCE);
1539 1336
        ExpectErr(tl, '.');
1540 1332
        tl->t = t;
1541 1332
        vcc_expr4(tl, &e, sym->type);
1542 1332
        ERRCHK(tl);
1543 1332
        vcc_expr_fmt(tl->fb, tl->indent, e);
1544 1332
        vcc_delete_expr(e);
1545 1332
        SkipToken(tl, ';');
1546 1332
        VSB_cat(tl->fb, ";\n");
1547 1336
}
1548
1549
/*--------------------------------------------------------------------
1550
 */
1551
1552
static void v_matchproto_(sym_expr_t)
1553 148
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 148
        int all = sym->eval_priv == NULL ? 0 : 1;
1558
        char buf[128];
1559
1560 148
        (void)t;
1561 148
        (void)fmt;
1562 148
        SkipToken(tl, '(');
1563 148
        vcc_expr0(tl, &e2, STRING);
1564 148
        ERRCHK(tl);
1565 148
        SkipToken(tl, ',');
1566 148
        vcc_expr0(tl, &e3, REGEX);
1567 148
        ERRCHK(tl);
1568
1569 144
        bprintf(buf, "VRT_regsub(ctx, %d,\v+\n\v1,\n\v2", all);
1570 144
        *e = vcc_expr_edit(tl, STRING, buf, e2, e3);
1571 144
        SkipToken(tl, ',');
1572 144
        vcc_expr0(tl, &e2, STRING);
1573 144
        ERRCHK(tl);
1574 144
        *e = vcc_expr_edit(tl, STRINGS, "\v1,\n\v2)\v-", *e, e2);
1575 144
        (*e)->nstr = 1;
1576 144
        SkipToken(tl, ')');
1577 148
}
1578
1579
/*--------------------------------------------------------------------
1580
 */
1581
1582
static void v_matchproto_(sym_expr_t)
1583 12084
vcc_Eval_BoolConst(struct vcc *tl, struct expr **e, struct token *t,
1584
    struct symbol *sym, vcc_type_t fmt)
1585
{
1586
1587 12084
        (void)t;
1588 12084
        (void)tl;
1589 12084
        (void)fmt;
1590 12084
        *e = vcc_mk_expr(BOOL, "(0==%d)", sym->eval_priv == NULL ? 1 : 0);
1591 12084
        (*e)->constant = EXPR_CONST;
1592 12084
}
1593
1594
/*--------------------------------------------------------------------
1595
 */
1596
1597
static void v_matchproto_(sym_expr_t)
1598 16
vcc_Eval_Default(struct vcc *tl, struct expr **e, struct token *t,
1599
    struct symbol *sym, vcc_type_t fmt)
1600
{
1601 16
        (void)e;
1602 16
        (void)fmt;
1603 16
        (void)sym;
1604 16
        (void)t;
1605
1606 16
        if (fmt->default_sym == NULL) {
1607 8
                VSB_cat(tl->sb, "Symbol 'default' is a reserved word.\n");
1608 8
                vcc_ErrWhere(tl, t);
1609 8
                return;
1610
        }
1611
1612 8
        *e = vcc_mk_expr(fmt, "%s", fmt->default_sym->rname);
1613 16
}
1614
1615
/*--------------------------------------------------------------------
1616
 */
1617
1618
void
1619 12028
vcc_Expr_Init(struct vcc *tl)
1620
{
1621
        struct symbol *sym;
1622
1623 12028
        sym = VCC_MkSym(tl, "regsub", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH);
1624 12028
        AN(sym);
1625 12028
        sym->type = STRING;
1626 12028
        sym->eval = vcc_Eval_Regsub;
1627 12028
        sym->eval_priv = NULL;
1628
1629 12028
        sym = VCC_MkSym(tl, "regsuball", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH);
1630 12028
        AN(sym);
1631 12028
        sym->type = STRING;
1632 12028
        sym->eval = vcc_Eval_Regsub;
1633 12028
        sym->eval_priv = sym;
1634
1635 12028
        sym = VCC_MkSym(tl, "true", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH);
1636 12028
        AN(sym);
1637 12028
        sym->type = BOOL;
1638 12028
        sym->eval = vcc_Eval_BoolConst;
1639 12028
        sym->eval_priv = sym;
1640
1641 12028
        sym = VCC_MkSym(tl, "false", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH);
1642 12028
        AN(sym);
1643 12028
        sym->type = BOOL;
1644 12028
        sym->eval = vcc_Eval_BoolConst;
1645 12028
        sym->eval_priv = NULL;
1646
1647 12028
        sym = VCC_MkSym(tl, "default", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH);
1648 12028
        AN(sym);
1649 12028
        sym->type = DEFAULT;
1650 12028
        sym->eval = vcc_Eval_Default;
1651 12028
}