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