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 230960
vcc_isconst(const struct expr *e)
63
{
64 230960
        AN(e->constant);
65 230960
        return (e->constant & EXPR_CONST);
66
}
67
68
static inline int
69 2723000
vcc_islit(const struct expr *e)
70
{
71 2723000
        AN(e->constant);
72 2723000
        return (e->constant & EXPR_STR_CONST);
73
}
74
75
static const char *
76 2680
vcc_utype(vcc_type_t t)
77
{
78 2680
        if (t == STRINGS || t->stringform)
79 720
                t = STRING;
80 2680
        return (t->name);
81
}
82
83
static vcc_type_t
84 25169200
vcc_stringstype(vcc_type_t t)
85
{
86 25169200
        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 23908440
vcc_new_expr(vcc_type_t fmt)
97
{
98
        struct expr *e;
99
100 23908440
        ALLOC_OBJ(e, EXPR_MAGIC);
101 23908440
        AN(e);
102 23908440
        e->vsb = VSB_new_auto();
103 23908440
        e->fmt = fmt;
104 23908440
        e->constant = EXPR_VAR;
105 23908440
        return (e);
106
}
107
108
static struct expr * v_printflike_(2, 3)
109 6340800
vcc_mk_expr(vcc_type_t fmt, const char *str, ...)
110
{
111
        va_list ap;
112
        struct expr *e;
113
114 6340800
        e = vcc_new_expr(fmt);
115 6340800
        va_start(ap, str);
116 6340800
        VSB_vprintf(e->vsb, str, ap);
117 6340800
        va_end(ap);
118 6340800
        AZ(VSB_finish(e->vsb));
119 6340800
        return (e);
120
}
121
122
static void
123 30477840
vcc_delete_expr(struct expr *e)
124
{
125 30477840
        if (e == NULL)
126 6603560
                return;
127 23874280
        CHECK_OBJ(e, EXPR_MAGIC);
128 23874280
        VSB_destroy(&e->vsb);
129 23874280
        FREE_OBJ(e);
130 30477840
}
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 2030400
vcc_strands_edit(const struct expr *e1, const struct expr *e2)
159
{
160
161 2030400
        assert(e2->fmt == STRANDS || e2->fmt == STRINGS);
162
163 2030400
        if (e2->fmt == STRANDS)
164 160
                VSB_cat(e1->vsb, VSB_data(e2->vsb));
165 2030240
        else if (e2->nstr == 0)
166 0
                VSB_printf(e1->vsb, "vrt_null_strands");
167 2030240
        else if (e2->nstr == 1)
168 1796520
                VSB_printf(e1->vsb, "TOSTRAND(%s)", VSB_data(e2->vsb));
169
        else {
170 467440
                VSB_printf(e1->vsb, "TOSTRANDS(%d,\v+\n%s\v-)",
171 233720
                   e2->nstr, VSB_data(e2->vsb));
172
        }
173 2030400
}
174
175
static struct expr *
176 13396320
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 13396320
        int nl = 1;
181
182 13396320
        (void) tl;
183
184 13396320
        AN(e1);
185 13396320
        e = vcc_new_expr(fmt);
186 157676040
        while (*p != '\0') {
187 144279720
                if (*p != '\v') {
188 119681120
                        if (*p != '\n' || !nl)
189 119679120
                                VSB_putc(e->vsb, *p);
190 119681120
                        nl = (*p == '\n');
191 119681120
                        p++;
192 119681120
                        continue;
193
                }
194 24598600
                assert(*p == '\v');
195 24598600
                switch (*++p) {
196 2141880
                case '+': VSB_cat(e->vsb, "\v+"); nl = 0; break;
197 2267440
                case '-': VSB_cat(e->vsb, "\v-"); nl = 0; break;
198
                case 'S':
199
                case 's':
200 484520
                        e3 = (*p == 'S' ? e1 : e2);
201 484520
                        AN(e3);
202 484520
                        assert(e1->fmt == STRINGS);
203 484520
                        if (e3->nstr > 1) {
204 1040
                                VSB_cat(e->vsb,
205
                                    "\nVRT_STRANDS_string(ctx,\v+\n");
206 1040
                                vcc_strands_edit(e, e3);
207 1040
                                VSB_cat(e->vsb,
208
                                    "\v-\n)\n");
209 1040
                        } else {
210 483480
                                VSB_cat(e->vsb, VSB_data(e3->vsb));
211
                        }
212 484520
                        break;
213
                case 'T':
214
                case 't':
215 2029360
                        e3 = (*p == 'T' ? e1 : e2);
216 2029360
                        AN(e3);
217 2029360
                        vcc_strands_edit(e, e3);
218 2029360
                        break;
219
                case '1':
220 10883680
                        VSB_cat(e->vsb, VSB_data(e1->vsb));
221 10883680
                        break;
222
                case '2':
223 6791720
                        AN(e2);
224 6791720
                        VSB_cat(e->vsb, VSB_data(e2->vsb));
225 6791720
                        break;
226
                default:
227 0
                        WRONG("Illegal edit in VCC expression");
228 0
                }
229 24598600
                p++;
230
        }
231 13396320
        AZ(VSB_finish(e->vsb));
232 13396320
        e->t1 = e1->t1;
233 13396320
        e->t2 = e1->t2;
234 13396320
        if (e2 != NULL)
235 6792960
                e->t2 = e2->t2;
236 13396320
        vcc_delete_expr(e1);
237 13396320
        vcc_delete_expr(e2);
238 13396320
        return (e);
239
}
240
241
/*--------------------------------------------------------------------
242
 * Expand finished expression into C-source code
243
 */
244
245
static void
246 3684920
vcc_expr_fmt(struct vsb *d, int ind, const struct expr *e1)
247
{
248
        char *p;
249
        int i;
250
251 3684920
        if (!e1->fmt->noindent) {
252 32784040
                for (i = 0; i < ind; i++)
253 29325200
                        VSB_putc(d, ' ');
254 3458840
        }
255 3684920
        p = VSB_data(e1->vsb);
256 362950714
        while (*p != '\0') {
257 359265954
                if (*p == '\n') {
258 10838440
                        VSB_putc(d, '\n');
259 10838440
                        if (*++p == '\0')
260 160
                                break;
261 116137000
                        for (i = 0; i < ind; i++)
262 105298720
                                VSB_putc(d, ' ');
263 359265794
                } else if (*p != '\v') {
264 343423754
                        VSB_putc(d, *p++);
265 343423754
                } else {
266 5003760
                        switch (*++p) {
267 2501880
                        case '+': ind += INDENT; break;
268 2501880
                        case '-': ind -= INDENT; break;
269 0
                        default:  WRONG("Illegal format in VCC expression");
270 0
                        }
271 5003760
                        p++;
272
                }
273
        }
274 3684920
}
275
276
/*--------------------------------------------------------------------
277
 */
278
279
static void
280 2822320
vcc_expr_tobool(struct vcc *tl, struct expr **e)
281
{
282
283 2822320
        if ((*e)->fmt == BOOL)
284 2142360
                return;
285 679960
        if ((*e)->fmt == BACKEND || (*e)->fmt == INT)
286 400
                *e = vcc_expr_edit(tl, BOOL, "(\v1 != 0)", *e, NULL);
287 679560
        else if ((*e)->fmt == DURATION)
288 80
                *e = vcc_expr_edit(tl, BOOL, "(\v1 > 0)", *e, NULL);
289 679480
        else if ((*e)->fmt == STRINGS)
290 679240
                *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 2822320
}
297
298
/*--------------------------------------------------------------------
299
 */
300
301
static void
302 2207040
vcc_expr_tostring(struct vcc *tl, struct expr **e)
303
{
304
        const char *p;
305 2207040
        uint8_t constant = EXPR_VAR;
306
307 2207040
        CHECK_OBJ_NOTNULL(*e, EXPR_MAGIC);
308 2207040
        assert((*e)->fmt != STRINGS);
309
310 2207040
        p = (*e)->fmt->tostring;
311 2207040
        if (p != NULL) {
312 2207000
                AN(*p);
313 2207000
                *e = vcc_expr_edit(tl, STRINGS, p, *e, NULL);
314 2207000
                (*e)->constant = constant;
315 2207000
                (*e)->nstr = 1;
316 2207000
        } else {
317 80
                VSB_printf(tl->sb,
318
                    "Cannot convert %s to STRING.\n",
319 40
                    vcc_utype((*e)->fmt));
320 40
                vcc_ErrWhere2(tl, (*e)->t1, tl->t);
321
        }
322 2207040
}
323
324
/*--------------------------------------------------------------------
325
 */
326
327
void v_matchproto_(sym_expr_t)
328 16640
vcc_Eval_Handle(struct vcc *tl, struct expr **e, struct token *t,
329
    struct symbol *sym, vcc_type_t type)
330
{
331
332 16640
        (void)t;
333 16640
        (void)tl;
334 16640
        AN(sym->rname);
335 16640
        AZ(type->stringform);
336
337 16840
        if (sym->type->tostring == NULL &&
338 200
            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 16640
                *e = vcc_mk_expr(sym->type, "%s", sym->rname);
344 16640
                (*e)->constant = EXPR_VAR;
345 16640
                (*e)->nstr = 1;
346 16640
                if ((*e)->fmt == STRING)
347 0
                        (*e)->fmt = STRINGS;
348
        }
349 16640
}
350
351
void v_matchproto_(sym_expr_t)
352 920
vcc_Eval_Sub(struct vcc *tl, struct expr **e, struct token *t,
353
    struct symbol *sym, vcc_type_t type)
354
{
355
356 920
        (void)t;
357 920
        (void)tl;
358 920
        AN(sym->rname);
359 920
        AZ(type->stringform);
360
361 920
        assert (sym->type == SUB);
362
363 920
        if (type == SUB) {
364 800
                *e = vcc_mk_expr(sym->type, "%s", sym->rname);
365 800
                (*e)->constant = EXPR_CONST;
366 800
                return;
367
        }
368
369 240
        VSB_printf(tl->sb, "Symbol '%s' can only be used as a %s expression\n",
370 120
            sym->name, sym->type->name);
371 120
        vcc_ErrWhere(tl, tl->t);
372 920
}
373
374
/*--------------------------------------------------------------------
375
 */
376
377
void v_matchproto_(sym_expr_t)
378 5150360
vcc_Eval_Var(struct vcc *tl, struct expr **e, struct token *t,
379
    struct symbol *sym, vcc_type_t type)
380
{
381
382 5150360
        (void)type;
383 5150360
        vcc_AddUses(tl, t, NULL, sym, XREF_READ);
384 5150360
        ERRCHK(tl);
385 5150360
        *e = vcc_mk_expr(sym->type, "%s", sym->rname);
386 5150360
        (*e)->constant = EXPR_VAR;
387 5150360
        (*e)->nstr = 1;
388 5150360
        if ((*e)->fmt == STRING)
389 2272960
                (*e)->fmt = STRINGS;
390 5150360
}
391
392
void v_matchproto_(sym_expr_t)
393 440
vcc_Eval_ProtectedHeader(struct vcc *tl, struct expr **e, struct token *t,
394
    struct symbol *sym, vcc_type_t type)
395
{
396
397 440
        AN(sym);
398 440
        AZ(sym->lorev);
399
400 440
        vcc_Header_Fh(tl, sym);
401 440
        sym->eval = vcc_Eval_Var;
402 440
        vcc_Eval_Var(tl, e, t, sym, type);
403 440
}
404
405
/*--------------------------------------------------------------------
406
 */
407
408
static struct expr *
409 6880
vcc_priv_arg(struct vcc *tl, const char *p, struct symbol *sym)
410
{
411
        char buf[64];
412
        struct inifin *ifp;
413 6880
        const char *f = NULL;
414
415 6880
        AN(sym);
416 6880
        AN(sym->vmod_name);
417
418 6880
        if (!strcmp(p, "PRIV_VCL"))
419 600
                return (vcc_mk_expr(VOID, "&vmod_priv_%s", sym->vmod_name));
420
421 6280
        if (!strcmp(p, "PRIV_CALL")) {
422 880
                bprintf(buf, "vmod_priv_%u", tl->unique++);
423 880
                ifp = New_IniFin(tl);
424 880
                Fh(tl, 0, "static struct vmod_priv %s;\n", buf);
425 880
                VSB_printf(ifp->fin, "\tVRT_priv_fini(ctx, &%s);", buf);
426 880
                return (vcc_mk_expr(VOID, "&%s", buf));
427
        }
428
429 5400
        if (!strcmp(p, "PRIV_TASK"))
430 5080
                f = "task";
431 320
        else if (!strcmp(p, "PRIV_TOP")) {
432 320
                f = "top";
433 320
                sym->r_methods &= VCL_MET_TASK_C;
434 320
        } else {
435 0
                WRONG("Wrong PRIV_ type");
436
        }
437 5400
        AN(f);
438
439 5400
        return (vcc_mk_expr(VOID, "VRT_priv_%s(ctx, &VGC_vmod_%s)",
440 5400
            f, sym->vmod_name));
441 6880
}
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 72760
vcc_do_enum(struct vcc *tl, const char *cfunc, int len, const char *ptr)
457
{
458
        const char *r;
459
460 72760
        (void)tl;
461 72760
        r = strchr(cfunc, '.');
462 72760
        AN(r);
463 72760
        return (vcc_mk_expr(VOID, "*%.*s.enum_%.*s",
464 72760
            (int)(r - cfunc), cfunc, len, ptr));
465
}
466
467
static void
468 159160
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 159160
        if (fa->type == ENUM) {
474 53480
                ExpectErr(tl, ID);
475 53480
                ERRCHK(tl);
476 182840
                VTAILQ_FOREACH(vv, &fa->enums->children, list)
477 182760
                        if (vcc_IdIs(tl->t, vv->value))
478 53400
                                break;
479 53480
                if (vv == NULL) {
480 80
                        VSB_cat(tl->sb, "Wrong enum value.");
481 80
                        VSB_cat(tl->sb, "  Expected one of:\n");
482 400
                        VTAILQ_FOREACH(vv, &fa->enums->children, list)
483 320
                                VSB_printf(tl->sb, "\t%s\n", vv->value);
484 80
                        vcc_ErrWhere(tl, tl->t);
485 80
                        return;
486
                }
487 53400
                fa->result = vcc_do_enum(tl, cfunc, PF(tl->t));
488 53400
                SkipToken(tl, ID);
489 53400
        } else {
490 105680
                if (fa->type == SUB)
491 800
                        tl->subref++;
492 105680
                vcc_expr0(tl, &e2, fa->type);
493 105680
                ERRCHK(tl);
494 105360
                assert(e2->fmt == fa->type);
495 105360
                fa->result = e2;
496
        }
497 158760
        fa->avail = 1;
498 159160
}
499
500
static void
501 110160
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 110160
        CAST_OBJ_NOTNULL(vv, priv, VJSN_VAL_MAGIC);
516 110160
        assert(vjsn_is_array(vv));
517 110160
        vv = VTAILQ_FIRST(&vv->children);
518 110160
        rfmt = VCC_Type(VTAILQ_FIRST(&vv->children)->value);
519 110160
        AN(rfmt);
520 110160
        vv = VTAILQ_NEXT(vv, list);
521 110160
        cfunc = vv->value;
522 110160
        vv = VTAILQ_NEXT(vv, list);
523 110160
        sa = vv->value;
524 110160
        if (*sa == '\0') {
525 93960
                sa = NULL;
526 93960
        }
527 110160
        vv = VTAILQ_NEXT(vv, list);
528 110160
        if (sym->kind == SYM_METHOD) {
529 30960
                if (*e == NULL) {
530 80
                        VSB_cat(tl->sb, "Syntax error.");
531 80
                        tl->err = 1;
532 80
                        return;
533
                }
534 30880
                vcc_NextToken(tl);
535 30880
                AZ(extra);
536 30880
                AN((*e)->instance);
537 30880
                extra = (*e)->instance->rname;
538 30880
        }
539 110080
        tf = VTAILQ_PREV(tl->t, tokenhead, list);
540 110080
        SkipToken(tl, '(');
541 110080
        if (extra == NULL) {
542 71160
                extra = "";
543 71160
                extra_sep = "";
544 71160
        } else {
545 38920
                AN(*extra);
546 38920
                extra_sep = ", ";
547
        }
548 110080
        VTAILQ_INIT(&head);
549 376760
        for (;vv != NULL; vv = VTAILQ_NEXT(vv, list)) {
550 266680
                assert(vjsn_is_array(vv));
551 266680
                fa = calloc(1, sizeof *fa);
552 266680
                AN(fa);
553 266680
                VTAILQ_INSERT_TAIL(&head, fa, list);
554
555 266680
                vvp = VTAILQ_FIRST(&vv->children);
556 266680
                if (!memcmp(vvp->value, "PRIV_", 5)) {
557 6880
                        fa->result = vcc_priv_arg(tl, vvp->value, sym);
558 6880
                        vvp = VTAILQ_NEXT(vvp, list);
559 6880
                        if (vvp != NULL)
560 6880
                                fa->cname = fa->name = vvp->value;
561 6880
                        continue;
562
                }
563 259800
                fa->type = VCC_Type(vvp->value);
564 259800
                AN(fa->type);
565 259800
                vvp = VTAILQ_NEXT(vvp, list);
566 259800
                if (vvp != NULL) {
567 259800
                        fa->name = vvp->value;
568 259800
                        vvp = VTAILQ_NEXT(vvp, list);
569 259800
                        AN(vvp); /* vmod_syntax 2.0 */
570 259800
                        fa->cname = vvp->value;
571 259800
                        vvp = VTAILQ_NEXT(vvp, list);
572 259800
                        if (vvp != NULL) {
573 180960
                                fa->val = vvp->value;
574 180960
                                vvp = VTAILQ_NEXT(vvp, list);
575 180960
                                if (vvp != NULL) {
576 148600
                                        fa->enums = vvp;
577 148600
                                        vvp = VTAILQ_NEXT(vvp, list);
578 148600
                                }
579 180960
                        }
580 259800
                }
581 259800
                if (sa != NULL && vvp != NULL && vjsn_is_true(vvp)) {
582 80760
                        fa->optional = 1;
583 80760
                        vvp = VTAILQ_NEXT(vvp, list);
584 80760
                }
585 259800
                AZ(vvp);
586 259800
        }
587
588 177480
        VTAILQ_FOREACH(fa, &head, list) {
589 159200
                if (tl->t->tok == ')')
590 3560
                        break;
591 155640
                if (fa->result != NULL)
592 4920
                        continue;
593 150720
                if (tl->t->tok == ID) {
594 110960
                        t1 = VTAILQ_NEXT(tl->t, list);
595 110960
                        if (t1->tok == '=')
596 30080
                                break;
597 80880
                }
598 120640
                vcc_do_arg(tl, cfunc, fa);
599 120640
                if (tl->err)
600 800
                        VSB_printf(tl->sb, "Expected argument: %s %s\n\n",
601 400
                            fa->type->name,
602 400
                            fa->name ? fa->name : "(unnamed argument)");
603 120640
                ERRCHK(tl);
604 120240
                if (tl->t->tok == ')')
605 57760
                        break;
606 62480
                SkipToken(tl, ',');
607 62480
        }
608 118200
        while (tl->t->tok == ID) {
609 126800
                VTAILQ_FOREACH(fa, &head, list) {
610 126760
                        if (fa->name == NULL)
611 480
                                continue;
612 126280
                        if (vcc_IdIs(tl->t, fa->name))
613 38560
                                break;
614 87720
                }
615 38600
                if (fa == NULL) {
616 80
                        VSB_printf(tl->sb, "Unknown argument '%.*s'\n",
617 40
                            PF(tl->t));
618 40
                        vcc_ErrWhere(tl, tl->t);
619 40
                        return;
620
                }
621 38560
                if (fa->result != NULL) {
622 40
                        AN(fa->name);
623 80
                        VSB_printf(tl->sb, "Argument '%s' already used\n",
624 40
                            fa->name);
625 40
                        vcc_ErrWhere(tl, tl->t);
626 40
                        return;
627
                }
628 38520
                vcc_NextToken(tl);
629 38520
                SkipToken(tl, '=');
630 38520
                vcc_do_arg(tl, cfunc, fa);
631 38520
                ERRCHK(tl);
632 38520
                if (tl->t->tok == ')')
633 30000
                        break;
634 8520
                SkipToken(tl, ',');
635
        }
636
637 109600
        if (sa != NULL)
638 32080
                e1 = vcc_mk_expr(rfmt, "%s(ctx%s%s,\v+\n&(%s)\v+ {\n",
639 16040
                    cfunc, extra_sep, extra, sa);
640
        else
641 187120
                e1 = vcc_mk_expr(rfmt, "%s(ctx%s%s\v+",
642 93560
                    cfunc, extra_sep, extra);
643 109600
        n = 0;
644 373440
        VTAILQ_FOREACH_SAFE(fa, &head, list, fa2) {
645 263840
                n++;
646 263840
                if (fa->optional) {
647 79000
                        AN(fa->cname);
648 79000
                        bprintf(ssa, "\v1.valid_%s = %d,\n",
649
                            fa->cname, fa->avail);
650 79000
                        e1 = vcc_expr_edit(tl, e1->fmt, ssa, e1, NULL);
651 79000
                }
652 263840
                if (fa->result == NULL && fa->type == ENUM && fa->val != NULL)
653 19360
                        fa->result = vcc_do_enum(tl, cfunc, strlen(fa->val), fa->val);
654 263840
                if (fa->result == NULL && fa->val != NULL)
655 23920
                        fa->result = vcc_mk_expr(fa->type, "%s", fa->val);
656 263840
                if (fa->result != NULL && sa != NULL) {
657 34720
                        if (fa->cname)
658 33880
                                bprintf(ssa, "\v1.%s = \v2,\n", fa->cname);
659
                        else
660 840
                                bprintf(ssa, "\v1.arg%d = \v2,\n", n);
661 34720
                        e1 = vcc_expr_edit(tl, e1->fmt, ssa, e1, fa->result);
662 263840
                } else if (fa->result != NULL) {
663 348000
                        e1 = vcc_expr_edit(tl, e1->fmt, "\v1,\n\v2",
664 174000
                            e1, fa->result);
665 229120
                } else if (!fa->optional) {
666 40
                        if (fa->name)
667 80
                                VSB_printf(tl->sb, "Argument '%s' missing\n",
668 40
                                    fa->name);
669
                        else
670 0
                                VSB_printf(tl->sb, "Argument %d missing\n", n);
671 40
                        vcc_ErrWhere(tl, tl->t);
672 40
                }
673 263840
                free(fa);
674 263840
        }
675 109600
        if (sa != NULL) {
676 16040
                *e = vcc_expr_edit(tl, e1->fmt, "\v1\v-\n}\v-\n)", e1, NULL);
677 16040
        } else {
678 93560
                *e = vcc_expr_edit(tl, e1->fmt, "\v1\v-\n)", e1, NULL);
679
        }
680 109600
        SkipToken(tl, ')');
681 109560
        vcc_AddUses(tl, tf, NULL, sym, XREF_READ);
682 110160
}
683
684
685
/*--------------------------------------------------------------------
686
 */
687
688
void
689 8040
vcc_Eval_Func(struct vcc *tl, const struct vjsn_val *spec,
690
    const char *extra, struct symbol *sym)
691
{
692 8040
        struct expr *e = NULL;
693
694 8040
        vcc_func(tl, &e, spec, extra, sym);
695 8040
        if (tl->err)
696 0
                VSB_cat(tl->sb, "While compiling function call:\n");
697 8040
        ERRCHK(tl);
698 8040
        vcc_expr_fmt(tl->fb, tl->indent, e);
699 8040
        VSB_cat(tl->fb, ";\n");
700 8040
        vcc_delete_expr(e);
701 8040
}
702
703
/*--------------------------------------------------------------------
704
 */
705
706
void v_matchproto_(sym_expr_t)
707 83160
vcc_Eval_SymFunc(struct vcc *tl, struct expr **e, struct token *t,
708
    struct symbol *sym, vcc_type_t fmt)
709
{
710
711 83160
        (void)t;
712 83160
        (void)fmt;
713 83160
        assert(sym->kind == SYM_FUNC || sym->kind == SYM_METHOD);
714 83160
        AN(sym->eval_priv);
715
716 83160
        vcc_func(tl, e, sym->eval_priv, sym->extra, sym);
717 83160
        ERRCHK(tl);
718 82760
        if ((*e)->fmt == STRING) {
719 30080
                (*e)->fmt = STRINGS;
720 30080
                (*e)->nstr = 1;
721 30080
        }
722 83160
}
723
724
/*--------------------------------------------------------------------
725
 */
726
727
static void
728 836400
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 836400
        assert(fmt != VOID);
735 836400
        if (fmt == BYTES) {
736 1920
                vcc_ByteVal(tl, &vi);
737 1920
                ERRCHK(tl);
738 1840
                e1 = vcc_mk_expr(BYTES, "%ju", (intmax_t)vi);
739 1840
        } else {
740 834480
                t = tl->t;
741 834480
                vcc_NextToken(tl);
742 834480
                if (tl->t->tok == ID) {
743 122040
                        e1 = vcc_mk_expr(DURATION, "%s%.3f * %g",
744 122040
                            sign, t->num, vcc_DurationUnit(tl));
745 122040
                        ERRCHK(tl);
746 834440
                } else if (fmt == REAL || t->tok == FNUM) {
747 6160
                        e1 = vcc_mk_expr(REAL, "%s%.3f", sign, t->num);
748 6160
                } else {
749 706280
                        e1 = vcc_mk_expr(INT, "%s%.0f", sign, t->num);
750
                }
751
        }
752 836280
        e1->constant = EXPR_CONST;
753 836280
        *e = e1;
754 836400
}
755
756
/*--------------------------------------------------------------------
757
 * SYNTAX:
758
 *    Expr5:
759
 *      '(' ExprCor ')'
760
 *      symbol
761
 *      CNUM
762
 *      FNUM
763
 *      CSTR
764
 *      CBLOB
765
 */
766
767
static void
768 10468640
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 10468640
        sign = "";
776 10468640
        *e = NULL;
777 10468640
        if (tl->t->tok == '(') {
778 114240
                SkipToken(tl, '(');
779 114240
                vcc_expr_cor(tl, &e2, fmt);
780 114240
                ERRCHK(tl);
781 114240
                SkipToken(tl, ')');
782 114240
                if (e2->fmt == STRINGS)
783 240
                        *e = e2;
784
                else
785 114000
                        *e = vcc_expr_edit(tl, e2->fmt, "(\v1)", e2, NULL);
786 114240
                return;
787
        }
788 10354400
        switch (tl->t->tok) {
789
        case ID:
790 5375920
                t = tl->t;
791 5375920
                t1 = vcc_PeekToken(tl);
792 5375920
                AN(t1);
793 5375920
                sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_NONE,
794
                    SYMTAB_PARTIAL_NOERR, XREF_REF);
795 5375920
                if (sym == NULL && fmt->global_pfx != NULL && t1->tok != '.') {
796 360
                        sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_NONE,
797
                            SYMTAB_CREATE, XREF_REF);
798 360
                        ERRCHK(tl);
799 280
                        AN(sym);
800 280
                        VCC_GlobalSymbol(sym, fmt);
801 280
                }
802 5375840
                ERRCHK(tl);
803 5375840
                if (sym == NULL)
804 480
                        AZ(VCC_SymbolGet(tl, SYM_MAIN, SYM_NONE,
805
                            SYMTAB_PARTIAL, XREF_REF));
806 5375840
                ERRCHK(tl);
807 5375360
                AN(sym);
808 5375360
                if (sym->kind == SYM_INSTANCE) {
809 30960
                        AZ(*e);
810 30960
                        *e = vcc_new_expr(sym->type);
811 30960
                        (*e)->instance = sym;
812 30960
                        return;
813
                }
814 5344400
                if (sym->kind == SYM_FUNC && sym->type == VOID) {
815 40
                        VSB_cat(tl->sb, "Function returns VOID:\n");
816 40
                        vcc_ErrWhere(tl, tl->t);
817 40
                        return;
818
                }
819 5344360
                if (sym->eval != NULL) {
820 5344240
                        AN(sym->eval);
821 5344240
                        AZ(*e);
822 5344240
                        sym->eval(tl, e, t, sym, fmt);
823 5344240
                        if (tl->err) {
824 640
                                VSB_cat(tl->sb,
825
                                    "While compiling function call:\n\n");
826 640
                                vcc_ErrWhere2(tl, t, tl->t);
827 640
                        }
828 5344240
                        ERRCHK(tl);
829
                        /* Unless asked for a HEADER, fold to string here */
830 5343600
                        if (*e && fmt != HEADER && (*e)->fmt == HEADER) {
831 1391400
                                vcc_expr_tostring(tl, e);
832 1391400
                                ERRCHK(tl);
833 1391400
                        }
834 5343600
                        return;
835
                }
836 240
                VSB_printf(tl->sb,
837
                    "Symbol '%.*s' type (%s) cannot be used in expression.\n",
838 120
                    PF(t), sym->kind->name);
839 120
                vcc_ErrWhere(tl, t);
840 120
                if (sym->def_b != NULL) {
841 40
                        VSB_cat(tl->sb, "That symbol was defined here:\n");
842 40
                        vcc_ErrWhere(tl, sym->def_b);
843 40
                }
844 120
                return;
845
        case CSTR:
846 4141480
                assert(fmt != VOID);
847 4141480
                if (fmt == IP) {
848 1200
                        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 40
                                VSB_cat(tl->sb,
856
                                    "Cannot convert to an IP address: ");
857 40
                                vcc_ErrToken(tl, tl->t);
858 40
                                vcc_ErrWhere(tl, tl->t);
859 40
                                return;
860
                        }
861 2320
                        Resolve_Sockaddr(tl, tl->t->dec, "80",
862
                            &ip, NULL, &ip, NULL, NULL, 1,
863 1160
                            tl->t, "IP constant");
864 1160
                        ERRCHK(tl);
865 1040
                        e1 = vcc_mk_expr(IP, "%s", ip);
866 1040
                        ERRCHK(tl);
867 4141320
                } else if (fmt == REGEX) {
868 343040
                        e1 = vcc_new_expr(REGEX);
869 343040
                        vcc_regexp(tl, e1->vsb);
870 343040
                        AZ(VSB_finish(e1->vsb));
871 343040
                } else {
872 3797240
                        e1 = vcc_new_expr(STRINGS);
873 3797240
                        EncToken(e1->vsb, tl->t);
874 3797240
                        AZ(VSB_finish(e1->vsb));
875 3797240
                        e1->constant |= EXPR_STR_CONST;
876 3797240
                        e1->nstr = 1;
877
                }
878 4141320
                e1->t1 = tl->t;
879 4141320
                e1->constant |= EXPR_CONST;
880 4141320
                vcc_NextToken(tl);
881 4141320
                *e = e1;
882 4141320
                return;
883
        case '-':
884 2480
                if (fmt != INT &&
885 640
                    fmt != REAL &&
886 160
                    fmt != DURATION &&
887 40
                    fmt != STRINGS)
888 0
                        break;
889 2440
                vcc_NextToken(tl);
890 2440
                if (tl->t->tok != FNUM && tl->t->tok != CNUM) {
891 360
                        vcc_expr_cor(tl, &e1, fmt);
892 360
                        ERRCHK(tl);
893 360
                        *e = vcc_expr_edit(tl, e1->fmt, "-(\v1)", e1, NULL);
894 360
                        return;
895
                }
896 2080
                sign = "-";
897
                /* FALLTHROUGH */
898
        case FNUM:
899
        case CNUM:
900 836400
                vcc_number(tl, e, fmt, sign);
901 836400
                return;
902
        case CBLOB:
903 80
                e1 = vcc_new_expr(BLOB);
904 80
                VSB_printf(e1->vsb, "%s", tl->t->dec);
905 80
                AZ(VSB_finish(e1->vsb));
906 80
                e1->constant |= EXPR_STR_CONST;
907 80
                e1->t1 = tl->t;
908 80
                vcc_NextToken(tl);
909 80
                *e = e1;
910 80
                return;
911
        default:
912 160
                break;
913
        }
914 160
        VSB_cat(tl->sb, "Unknown token ");
915 160
        vcc_ErrToken(tl, tl->t);
916 160
        VSB_printf(tl->sb, " when looking for %s\n\n", vcc_utype(fmt));
917 160
        vcc_ErrWhere(tl, tl->t);
918 10468640
}
919
920
/*--------------------------------------------------------------------
921
 * SYNTAX:
922
 *    Expr4:
923
 *      Expr5 [ '.' (type_attribute | type_method()) ]*
924
 */
925
926
void
927 113400
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 113400
        (void)t;
933 113400
        impl = VCC_Type_EvalMethod(tl, sym);
934 113400
        ERRCHK(tl);
935 113400
        AN(impl);
936 113400
        *e = vcc_expr_edit(tl, fmt, impl, *e, NULL);
937 113400
}
938
939
static void
940 10468640
vcc_expr4(struct vcc *tl, struct expr **e, vcc_type_t fmt)
941
{
942
        struct symbol *sym;
943
944 10468640
        *e = NULL;
945 10468640
        vcc_expr5(tl, e, fmt);
946 10468640
        ERRCHK(tl);
947 10466720
        AN(*e);
948 10611000
        while (tl->t->tok == '.') {
949 144400
                vcc_NextToken(tl);
950 144400
                ExpectErr(tl, ID);
951
952 144360
                sym = VCC_TypeSymbol(tl, SYM_METHOD, (*e)->fmt);
953 144360
                if (sym == NULL) {
954 80
                        VSB_cat(tl->sb, "Unknown property ");
955 80
                        vcc_ErrToken(tl, tl->t);
956 80
                        VSB_printf(tl->sb, " for type %s\n", (*e)->fmt->name);
957 80
                        vcc_ErrWhere(tl, tl->t);
958 80
                        return;
959
                }
960
961 144280
                AN(sym->eval);
962 144280
                sym->eval(tl, e, tl->t, sym, sym->type);
963 144280
                ERRCHK(tl);
964 144280
                if ((*e)->fmt == STRING) {
965 112960
                        (*e)->fmt = STRINGS;
966 112960
                        (*e)->nstr = 1;
967 112960
                }
968
        }
969 10468640
}
970
971
/*--------------------------------------------------------------------
972
 * SYNTAX:
973
 *    ExprMul:
974
 *      Expr4 { {'*'|'/'|'%'} Expr4 } *
975
 */
976
977
static void
978 10110720
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 10110720
        *e = NULL;
986 10110720
        vcc_expr4(tl, e, fmt);
987 10110720
        ERRCHK(tl);
988 10108960
        AN(*e);
989
990 10110440
        while (tl->t->tok == '*' || tl->t->tok == '/' || tl->t->tok == '%') {
991 1640
                if (tl->t->tok == '%' && ((*e)->fmt != INT)) {
992 40
                        VSB_cat(tl->sb, "Operator % only possible on INT.\n");
993 40
                        vcc_ErrWhere(tl, tl->t);
994 40
                        return;
995
                }
996 1600
                f2 = (*e)->fmt->multype;
997 1600
                if (f2 == NULL) {
998 80
                        VSB_printf(tl->sb,
999
                            "Operator %.*s not possible on type %s.\n",
1000 40
                            PF(tl->t), vcc_utype((*e)->fmt));
1001 40
                        vcc_ErrWhere(tl, tl->t);
1002 40
                        return;
1003
                }
1004 1560
                tk = tl->t;
1005 1560
                vcc_NextToken(tl);
1006 1560
                vcc_expr4(tl, &e2, f2);
1007 1560
                ERRCHK(tl);
1008 1560
                if (e2->fmt != INT && e2->fmt != f2) {
1009 160
                        VSB_printf(tl->sb, "%s %.*s %s not possible.\n",
1010 80
                            vcc_utype((*e)->fmt), PF(tk), vcc_utype(e2->fmt));
1011 80
                        vcc_ErrWhere(tl, tk);
1012 80
                        return;
1013
                }
1014 1480
                bprintf(buf, "(\v1%c\v2)", tk->tok);
1015 1480
                *e = vcc_expr_edit(tl, (*e)->fmt, buf, *e, e2);
1016
        }
1017 10110720
}
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 7386680
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 7386680
        *e = NULL;
1059 7386680
        vcc_expr_mul(tl, e, fmt);
1060 7386680
        ERRCHK(tl);
1061
1062 10108360
        while (tl->t->tok == '+' || tl->t->tok == '-') {
1063 2724040
                tk = tl->t;
1064 43554480
                for (ap = vcc_adds; ap->op != EOI; ap++)
1065 40833440
                        if (tk->tok == ap->op && (*e)->fmt == ap->a)
1066 3000
                                break;
1067 2724040
                vcc_NextToken(tl);
1068 2724040
                if (ap->op == EOI && fmt == STRINGS)
1069 20160
                        vcc_expr_mul(tl, &e2, STRINGS);
1070
                else
1071 2703880
                        vcc_expr_mul(tl, &e2, (*e)->fmt);
1072 2724040
                ERRCHK(tl);
1073
1074 43559360
                for (ap = vcc_adds; ap->op != EOI; ap++)
1075 40837920
                        if (tk->tok == ap->op && (*e)->fmt == ap->a &&
1076 40837920
                            e2->fmt == ap->b)
1077 2600
                                break;
1078
1079 2724040
                if (ap->op == '+') {
1080 1800
                        *e = vcc_expr_edit(tl, ap->fmt, "(\v1 + \v2)", *e, e2);
1081 2724040
                } else if (ap->op == '-') {
1082 800
                        *e = vcc_expr_edit(tl, ap->fmt, "(\v1 - \v2)", *e, e2);
1083 2722840
                } else if (tk->tok == '+' &&
1084 2721400
                    ((*e)->fmt == STRINGS || fmt == STRINGS)) {
1085 2721000
                        if ((*e)->fmt != STRINGS)
1086 200
                                vcc_expr_tostring(tl, e);
1087 2721000
                        if (e2->fmt != STRINGS)
1088 677360
                                vcc_expr_tostring(tl, &e2);
1089 2721000
                        if (vcc_islit(*e) && vcc_isconst(e2)) {
1090 2000
                                lit = vcc_islit(e2);
1091 4000
                                *e = vcc_expr_edit(tl, STRINGS,
1092 2000
                                    "\v1\n\v2", *e, e2);
1093 2000
                                (*e)->constant = EXPR_CONST;
1094 2000
                                (*e)->nstr = 1;
1095 2000
                                if (lit)
1096 2000
                                        (*e)->constant |= EXPR_STR_CONST;
1097 2000
                        } else {
1098 2719000
                                n = (*e)->nstr + e2->nstr;
1099 5438000
                                *e = vcc_expr_edit(tl, STRINGS,
1100 2719000
                                    "\v1,\n\v2", *e, e2);
1101 2719000
                                (*e)->constant = EXPR_VAR;
1102 2719000
                                (*e)->nstr = n;
1103
                        }
1104 2721000
                } else {
1105 880
                        VSB_printf(tl->sb, "%s %.*s %s not possible.\n",
1106 440
                            vcc_utype((*e)->fmt), PF(tk), vcc_utype(e2->fmt));
1107 440
                        vcc_ErrWhere2(tl, tk, tl->t);
1108 440
                        return;
1109
                }
1110
        }
1111 7386680
}
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 455720
cmp_simple(struct vcc *tl, struct expr **e, const struct cmps *cp)
1139
{
1140
        struct expr *e2;
1141
        struct token *tk;
1142
1143 455720
        tk = tl->t;
1144 455720
        vcc_NextToken(tl);
1145 455720
        vcc_expr_add(tl, &e2, (*e)->fmt);
1146 455720
        ERRCHK(tl);
1147
1148 455600
        if (e2->fmt != (*e)->fmt) {
1149 160
                VSB_printf(tl->sb,
1150
                    "Comparison of different types: %s '%.*s' %s\n",
1151 80
                    vcc_utype((*e)->fmt), PF(tk), vcc_utype(e2->fmt));
1152 80
                vcc_ErrWhere(tl, tk);
1153 80
        } else
1154 455520
                *e = vcc_expr_edit(tl, BOOL, cp->emit, *e, e2);
1155 455720
}
1156
1157
static void v_matchproto_(cmp_f)
1158 341240
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 341240
        *e = vcc_expr_edit(tl, STRING, "\vS", *e, NULL);
1165 341240
        vcc_NextToken(tl);
1166 341240
        t1 = tl->t;
1167 341240
        vcc_expr4(tl, &e2, REGEX);
1168 341240
        ERRCHK(tl);
1169 341080
        vcc_expr_typecheck(tl, &e2, REGEX, t1);
1170 341080
        ERRCHK(tl);
1171 341040
        bprintf(buf, "%sVRT_re_match(ctx, \v1, \v2)", cp->emit);
1172 341040
        *e = vcc_expr_edit(tl, BOOL, buf, *e, e2);
1173 341240
}
1174
1175
static void v_matchproto_(cmp_f)
1176 1800
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 1800
        vcc_NextToken(tl);
1183 1800
        t1 = tl->t;
1184 1800
        vcc_expr4(tl, &e2, ACL);
1185 1800
        ERRCHK(tl);
1186 1680
        vcc_expr_typecheck(tl, &e2, ACL, t1);
1187 1680
        ERRCHK(tl);
1188 1560
        bprintf(buf, "%sVRT_acl_match(ctx, \v1, \v2)", cp->emit);
1189 1560
        *e = vcc_expr_edit(tl, BOOL, buf, e2, *e);
1190 1800
}
1191
1192
static void v_matchproto_(cmp_f)
1193 1594280
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 1594280
        tk = tl->t;
1200 1594280
        vcc_NextToken(tl);
1201 1594280
        vcc_expr_add(tl, &e2, STRINGS);
1202 1594280
        ERRCHK(tl);
1203 1594280
        if (vcc_stringstype(e2->fmt) != STRINGS) {
1204 240
                VSB_printf(tl->sb,
1205
                    "Comparison of different types: %s '%.*s' %s\n",
1206 120
                    vcc_utype((*e)->fmt), PF(tk), vcc_utype(e2->fmt));
1207 120
                vcc_ErrWhere(tl, tk);
1208 1594280
        } else if ((*e)->nstr == 1 && e2->nstr == 1) {
1209 1592920
                bprintf(buf, "(%s VRT_strcmp(\v1, \v2))", cp->emit);
1210 1592920
                *e = vcc_expr_edit(tl, BOOL, buf, *e, e2);
1211 1592920
        } else {
1212 1240
                bprintf(buf, "(%s VRT_CompareStrands(\vT, \vt))", cp->emit);
1213 1240
                *e = vcc_expr_edit(tl, BOOL, buf, *e, e2);
1214
        }
1215 1594280
}
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 5336680
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 5336680
        *e = NULL;
1272 5336680
        vcc_expr_add(tl, e, fmt);
1273 5336680
        ERRCHK(tl);
1274 5334440
        tk = tl->t;
1275
1276 267383640
        for (cp = vcc_cmps; cp->fmt != VOID; cp++) {
1277 264442240
                if (tl->t->tok != cp->token)
1278 240867320
                        continue;
1279 23574920
                if (vcc_stringstype((*e)->fmt) != cp->fmt)
1280 21181880
                        continue;
1281 2393040
                AN(cp->func);
1282 2393040
                cp->func(tl, e, cp);
1283 2393040
                return;
1284
        }
1285
1286 2941400
        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 160
                VSB_printf(tl->sb, "Operator %.*s not possible on %s\n",
1296 80
                    PF(tl->t), vcc_utype((*e)->fmt));
1297 80
                vcc_ErrWhere(tl, tl->t);
1298 80
                return;
1299
        default:
1300 2941320
                break;
1301
        }
1302 5336680
}
1303
1304
/*--------------------------------------------------------------------
1305
 * SYNTAX:
1306
 *    ExprNot:
1307
 *      '!' ExprCmp
1308
 */
1309
1310
static void
1311 5336680
vcc_expr_not(struct vcc *tl, struct expr **e, vcc_type_t fmt)
1312
{
1313
        struct token *tk;
1314
1315 5336680
        *e = NULL;
1316 5336680
        tk = tl->t;
1317 5336680
        if (tl->t->tok == '!')
1318 228200
                vcc_NextToken(tl);
1319 5336680
        vcc_expr_cmp(tl, e, fmt);
1320 5336680
        ERRCHK(tl);
1321 5333600
        if (tk->tok != '!')
1322 5105440
                return;
1323 228160
        vcc_expr_tobool(tl, e);
1324 228160
        ERRCHK(tl);
1325 228160
        if ((*e)->fmt != BOOL) {
1326 40
                VSB_cat(tl->sb, "'!' must be followed by BOOL, found ");
1327 40
                VSB_printf(tl->sb, "%s.\n", vcc_utype((*e)->fmt));
1328 40
                vcc_ErrWhere2(tl, tk, tl->t);
1329 40
        } else {
1330 228120
                *e = vcc_expr_edit(tl, BOOL, "!(\v1)", *e, NULL);
1331
        }
1332 5336680
}
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 7858320
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 7858320
        *e = NULL;
1351 7858320
        tk = tl->t;
1352 7858320
        up(tl, e, fmt);
1353 7858320
        ERRCHK(tl);
1354 7852000
        if (tl->t->tok != ourtok)
1355 7175480
                return;
1356 676520
        vcc_expr_tobool(tl, e);
1357 676520
        ERRCHK(tl);
1358 676520
        if ((*e)->fmt != BOOL) {
1359 160
                VSB_printf(tl->sb,
1360
                    "'%s' must be preceded by BOOL,"
1361 80
                    " found %s.\n", tokstr, vcc_utype((*e)->fmt));
1362 80
                vcc_ErrWhere2(tl, tk, tl->t);
1363 80
                return;
1364
        }
1365 676440
        *e = vcc_expr_edit(tl, BOOL, "(\v+\n\v1", *e, NULL);
1366 2140440
        while (tl->t->tok == ourtok) {
1367 1464080
                vcc_NextToken(tl);
1368 1464080
                tk = tl->t;
1369 1464080
                up(tl, &e2, fmt);
1370 1464080
                ERRCHK(tl);
1371 1464080
                vcc_expr_tobool(tl, &e2);
1372 1464080
                ERRCHK(tl);
1373 1464080
                if (e2->fmt != BOOL) {
1374 160
                        VSB_printf(tl->sb,
1375
                            "'%s' must be followed by BOOL,"
1376 80
                            " found %s.\n", tokstr, vcc_utype(e2->fmt));
1377 80
                        vcc_ErrWhere2(tl, tk, tl->t);
1378 80
                        vcc_delete_expr(e2);
1379 80
                        return;
1380
                }
1381 1464000
                bprintf(buf, "\v1\v-\n%s\v+\n\v2", tokstr);
1382 1464000
                *e = vcc_expr_edit(tl, BOOL, buf, *e, e2);
1383
        }
1384 676360
        *e = vcc_expr_edit(tl, BOOL, "\v1\v-\n)", *e, NULL);
1385 7858320
}
1386
1387
/*--------------------------------------------------------------------
1388
 * SYNTAX:
1389
 *    ExprCand:
1390
 *      ExprNot { '&&' ExprNot } *
1391
 */
1392
1393
static void
1394 3985720
vcc_expr_cand(struct vcc *tl, struct expr **e, vcc_type_t fmt)
1395
{
1396
1397 3985720
        vcc_expr_bin_bool(tl, e, fmt, T_CAND, vcc_expr_not, "&&");
1398 3985720
}
1399
1400
/*--------------------------------------------------------------------
1401
 * SYNTAX:
1402
 *    ExprCOR:
1403
 *      ExprCand { '||' ExprCand } *
1404
 */
1405
1406
static void
1407 3872600
vcc_expr_cor(struct vcc *tl, struct expr **e, vcc_type_t fmt)
1408
{
1409
1410 3872600
        vcc_expr_bin_bool(tl, e, fmt, T_COR, vcc_expr_cand, "||");
1411 3872600
}
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 3758000
vcc_expr0(struct vcc *tl, struct expr **e, vcc_type_t fmt)
1420
{
1421
        struct token *t1;
1422
1423 3758000
        assert(fmt != VOID);
1424 3758000
        assert(fmt != STRINGS);
1425 3758000
        *e = NULL;
1426 3758000
        t1 = tl->t;
1427 3758000
        if (fmt->stringform)
1428 1153360
                vcc_expr_cor(tl, e, STRINGS);
1429
        else
1430 2604640
                vcc_expr_cor(tl, e, fmt);
1431 3758000
        ERRCHK(tl);
1432
1433 3754720
        if ((*e)->fmt == fmt)
1434 1922880
                return;
1435
1436 1831840
        if ((*e)->fmt != STRINGS && fmt->stringform)
1437 138040
                vcc_expr_tostring(tl, e);
1438
1439 1831840
        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 1831840
        if (fmt == BODY && !(*e)->fmt->bodyform)
1448 40
                vcc_expr_tostring(tl, e);
1449
1450 1831840
        if (fmt == BODY && (*e)->fmt->bodyform) {
1451 226080
                if ((*e)->fmt == STRINGS)
1452 225920
                        *e = vcc_expr_edit(tl, BODY, "STRING, 0, \vT", *e, NULL);
1453 160
                else if ((*e)->fmt == BLOB)
1454 160
                        *e = vcc_expr_edit(tl, BODY, "BLOB, 0, \v1", *e, NULL);
1455
                else
1456 0
                        WRONG("Unhandled bodyform");
1457 226080
        }
1458
1459 1831840
        if ((*e)->fmt == STRINGS && fmt->stringform) {
1460 1152040
                if (fmt == STRING)
1461 143280
                        *e = vcc_expr_edit(tl, STRING, "\vS", *e, NULL);
1462 1008760
                else if (fmt == STRANDS)
1463 1008760
                        *e = vcc_expr_edit(tl, STRANDS, "\vT", (*e), NULL);
1464
                else
1465 0
                        WRONG("Unhandled stringform");
1466 1152040
        }
1467
1468 1831840
        if (fmt == BOOL) {
1469 453560
                vcc_expr_tobool(tl, e);
1470 453560
                ERRCHK(tl);
1471 453560
        }
1472
1473 1831840
        vcc_expr_typecheck(tl, e, fmt, t1);
1474 3758000
}
1475
1476
static void
1477 2174600
vcc_expr_typecheck(struct vcc *tl, struct expr **e, vcc_type_t fmt,
1478
    struct token *t1)
1479
{
1480
1481 2174600
        assert(fmt != VOID);
1482 2174600
        assert(fmt != STRINGS);
1483
1484 2174600
        if (fmt != (*e)->fmt)  {
1485 720
                VSB_printf(tl->sb, "Expression has type %s, expected %s\n",
1486 360
                    vcc_utype((*e)->fmt), vcc_utype(fmt));
1487 360
                vcc_ErrWhere2(tl, t1, tl->t);
1488 360
        }
1489 2174600
}
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 3647920
vcc_Expr(struct vcc *tl, vcc_type_t fmt)
1500
{
1501 3647920
        struct expr *e = NULL;
1502
1503 3647920
        assert(fmt != VOID);
1504 3647920
        assert(fmt != STRINGS);
1505 3647920
        vcc_expr0(tl, &e, fmt);
1506 3647920
        ERRCHK(tl);
1507 3644800
        assert(e->fmt == fmt);
1508
1509 3644800
        vcc_expr_fmt(tl->fb, tl->indent, e);
1510 3644800
        VSB_cat(tl->fb, "\n");
1511 3644800
        vcc_delete_expr(e);
1512 3647920
}
1513
1514
/*--------------------------------------------------------------------
1515
 */
1516
1517
void v_matchproto_(sym_act_f)
1518 18960
vcc_Act_Call(struct vcc *tl, struct token *t, struct symbol *sym)
1519
{
1520
1521
        struct expr *e;
1522
1523 18960
        e = NULL;
1524 18960
        vcc_func(tl, &e, sym->eval_priv, sym->extra, sym);
1525 18960
        if (!tl->err) {
1526 18760
                vcc_expr_fmt(tl->fb, tl->indent, e);
1527 18760
                SkipToken(tl, ';');
1528 18760
                VSB_cat(tl->fb, ";\n");
1529 18960
        } else if (t != tl->t) {
1530 200
                VSB_cat(tl->sb, "While compiling function call:\n\n");
1531 200
                vcc_ErrWhere2(tl, t, tl->t);
1532 200
        }
1533 18960
        vcc_delete_expr(e);
1534 18960
}
1535
1536
void v_matchproto_(sym_act_f)
1537 13360
vcc_Act_Obj(struct vcc *tl, struct token *t, struct symbol *sym)
1538
{
1539
1540 13360
        struct expr *e = NULL;
1541
1542 13360
        assert(sym->kind == SYM_INSTANCE);
1543 13360
        ExpectErr(tl, '.');
1544 13320
        tl->t = t;
1545 13320
        vcc_expr4(tl, &e, sym->type);
1546 13320
        ERRCHK(tl);
1547 13320
        vcc_expr_fmt(tl->fb, tl->indent, e);
1548 13320
        vcc_delete_expr(e);
1549 13320
        SkipToken(tl, ';');
1550 13320
        VSB_cat(tl->fb, ";\n");
1551 13360
}
1552
1553
/*--------------------------------------------------------------------
1554
 */
1555
1556
static void v_matchproto_(sym_expr_t)
1557 1480
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 1480
        int all = sym->eval_priv == NULL ? 0 : 1;
1562
        char buf[128];
1563
1564 1480
        (void)t;
1565 1480
        (void)fmt;
1566 1480
        SkipToken(tl, '(');
1567 1480
        vcc_expr0(tl, &e2, STRING);
1568 1480
        ERRCHK(tl);
1569 1480
        SkipToken(tl, ',');
1570 1480
        vcc_expr0(tl, &e3, REGEX);
1571 1480
        ERRCHK(tl);
1572
1573 1440
        bprintf(buf, "VRT_regsub(ctx, %d,\v+\n\v1,\n\v2", all);
1574 1440
        *e = vcc_expr_edit(tl, STRING, buf, e2, e3);
1575 1440
        SkipToken(tl, ',');
1576 1440
        vcc_expr0(tl, &e2, STRING);
1577 1440
        ERRCHK(tl);
1578 1440
        *e = vcc_expr_edit(tl, STRINGS, "\v1,\n\v2)\v-", *e, e2);
1579 1440
        (*e)->nstr = 1;
1580 1440
        SkipToken(tl, ')');
1581 1480
}
1582
1583
/*--------------------------------------------------------------------
1584
 */
1585
1586
static void v_matchproto_(sym_expr_t)
1587 122400
vcc_Eval_BoolConst(struct vcc *tl, struct expr **e, struct token *t,
1588
    struct symbol *sym, vcc_type_t fmt)
1589
{
1590
1591 122400
        (void)t;
1592 122400
        (void)tl;
1593 122400
        (void)fmt;
1594 122400
        *e = vcc_mk_expr(BOOL, "(0==%d)", sym->eval_priv == NULL ? 1 : 0);
1595 122400
        (*e)->constant = EXPR_CONST;
1596 122400
}
1597
1598
/*--------------------------------------------------------------------
1599
 */
1600
1601
static void v_matchproto_(sym_expr_t)
1602 160
vcc_Eval_Default(struct vcc *tl, struct expr **e, struct token *t,
1603
    struct symbol *sym, vcc_type_t fmt)
1604
{
1605 160
        (void)e;
1606 160
        (void)fmt;
1607 160
        (void)sym;
1608 160
        (void)t;
1609
1610 160
        if (fmt->default_sym == NULL) {
1611 80
                VSB_cat(tl->sb, "Symbol 'default' is a reserved word.\n");
1612 80
                vcc_ErrWhere(tl, t);
1613 80
                return;
1614
        }
1615
1616 80
        *e = vcc_mk_expr(fmt, "%s", fmt->default_sym->rname);
1617 160
}
1618
1619
/*--------------------------------------------------------------------
1620
 */
1621
1622
void
1623 121960
vcc_Expr_Init(struct vcc *tl)
1624
{
1625
        struct symbol *sym;
1626
1627 121960
        sym = VCC_MkSym(tl, "regsub", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH);
1628 121960
        AN(sym);
1629 121960
        sym->type = STRING;
1630 121960
        sym->eval = vcc_Eval_Regsub;
1631 121960
        sym->eval_priv = NULL;
1632
1633 121960
        sym = VCC_MkSym(tl, "regsuball", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH);
1634 121960
        AN(sym);
1635 121960
        sym->type = STRING;
1636 121960
        sym->eval = vcc_Eval_Regsub;
1637 121960
        sym->eval_priv = sym;
1638
1639 121960
        sym = VCC_MkSym(tl, "true", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH);
1640 121960
        AN(sym);
1641 121960
        sym->type = BOOL;
1642 121960
        sym->eval = vcc_Eval_BoolConst;
1643 121960
        sym->eval_priv = sym;
1644
1645 121960
        sym = VCC_MkSym(tl, "false", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH);
1646 121960
        AN(sym);
1647 121960
        sym->type = BOOL;
1648 121960
        sym->eval = vcc_Eval_BoolConst;
1649 121960
        sym->eval_priv = NULL;
1650
1651 121960
        sym = VCC_MkSym(tl, "default", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH);
1652 121960
        AN(sym);
1653 121960
        sym->type = DEFAULT;
1654 121960
        sym->eval = vcc_Eval_Default;
1655 121960
}