| | 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 |
132618 |
vcc_isconst(const struct expr *e) |
63 |
|
{ |
64 |
132618 |
AN(e->constant); |
65 |
132618 |
return (e->constant & EXPR_CONST); |
66 |
|
} |
67 |
|
|
68 |
|
static inline int |
69 |
1563517 |
vcc_islit(const struct expr *e) |
70 |
|
{ |
71 |
1563517 |
AN(e->constant); |
72 |
1563517 |
return (e->constant & EXPR_STR_CONST); |
73 |
|
} |
74 |
|
|
75 |
|
static const char * |
76 |
1541 |
vcc_utype(vcc_type_t t) |
77 |
|
{ |
78 |
1541 |
if (t == STRINGS || t->stringform) |
79 |
414 |
t = STRING; |
80 |
1541 |
return (t->name); |
81 |
|
} |
82 |
|
|
83 |
|
static vcc_type_t |
84 |
14451958 |
vcc_stringstype(vcc_type_t t) |
85 |
|
{ |
86 |
14451958 |
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 |
13725158 |
vcc_new_expr(vcc_type_t fmt) |
97 |
|
{ |
98 |
|
struct expr *e; |
99 |
|
|
100 |
13725158 |
ALLOC_OBJ(e, EXPR_MAGIC); |
101 |
13725158 |
AN(e); |
102 |
13725158 |
e->vsb = VSB_new_auto(); |
103 |
13725158 |
e->fmt = fmt; |
104 |
13725158 |
e->constant = EXPR_VAR; |
105 |
13725158 |
return (e); |
106 |
|
} |
107 |
|
|
108 |
|
static struct expr * v_printflike_(2, 3) |
109 |
3641015 |
vcc_mk_expr(vcc_type_t fmt, const char *str, ...) |
110 |
|
{ |
111 |
|
va_list ap; |
112 |
|
struct expr *e; |
113 |
|
|
114 |
3641015 |
e = vcc_new_expr(fmt); |
115 |
3641015 |
va_start(ap, str); |
116 |
3641015 |
VSB_vprintf(e->vsb, str, ap); |
117 |
3641015 |
va_end(ap); |
118 |
3641015 |
AZ(VSB_finish(e->vsb)); |
119 |
3641015 |
return (e); |
120 |
|
} |
121 |
|
|
122 |
|
static void |
123 |
17493961 |
vcc_delete_expr(struct expr *e) |
124 |
|
{ |
125 |
17493961 |
if (e == NULL) |
126 |
3788445 |
return; |
127 |
13705516 |
CHECK_OBJ(e, EXPR_MAGIC); |
128 |
13705516 |
VSB_destroy(&e->vsb); |
129 |
13705516 |
FREE_OBJ(e); |
130 |
17493961 |
} |
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 |
1165893 |
vcc_strands_edit(const struct expr *e1, const struct expr *e2) |
159 |
|
{ |
160 |
|
|
161 |
1165893 |
assert(e2->fmt == STRANDS || e2->fmt == STRINGS); |
162 |
|
|
163 |
1165893 |
if (e2->fmt == STRANDS) |
164 |
92 |
VSB_cat(e1->vsb, VSB_data(e2->vsb)); |
165 |
1165801 |
else if (e2->nstr == 0) |
166 |
0 |
VSB_printf(e1->vsb, "vrt_null_strands"); |
167 |
1165801 |
else if (e2->nstr == 1) |
168 |
1031596 |
VSB_printf(e1->vsb, "TOSTRAND(%s)", VSB_data(e2->vsb)); |
169 |
|
else { |
170 |
268410 |
VSB_printf(e1->vsb, "TOSTRANDS(%d,\v+\n%s\v-)", |
171 |
134205 |
e2->nstr, VSB_data(e2->vsb)); |
172 |
|
} |
173 |
1165893 |
} |
174 |
|
|
175 |
|
static struct expr * |
176 |
7688946 |
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 |
7688946 |
int nl = 1; |
181 |
|
|
182 |
7688946 |
(void) tl; |
183 |
|
|
184 |
7688946 |
AN(e1); |
185 |
7688946 |
e = vcc_new_expr(fmt); |
186 |
90467027 |
while (*p != '\0') { |
187 |
82778081 |
if (*p != '\v') { |
188 |
68656656 |
if (*p != '\n' || !nl) |
189 |
68655506 |
VSB_putc(e->vsb, *p); |
190 |
68656656 |
nl = (*p == '\n'); |
191 |
68656656 |
p++; |
192 |
68656656 |
continue; |
193 |
|
} |
194 |
14121425 |
assert(*p == '\v'); |
195 |
14121425 |
switch (*++p) { |
196 |
1229833 |
case '+': VSB_cat(e->vsb, "\v+"); nl = 0; break; |
197 |
1302030 |
case '-': VSB_cat(e->vsb, "\v-"); nl = 0; break; |
198 |
|
case 'S': |
199 |
|
case 's': |
200 |
278231 |
e3 = (*p == 'S' ? e1 : e2); |
201 |
278231 |
AN(e3); |
202 |
278231 |
assert(e1->fmt == STRINGS); |
203 |
278231 |
if (e3->nstr > 1) { |
204 |
598 |
VSB_cat(e->vsb, |
205 |
|
"\nVRT_STRANDS_string(ctx,\v+\n"); |
206 |
598 |
vcc_strands_edit(e, e3); |
207 |
598 |
VSB_cat(e->vsb, |
208 |
|
"\v-\n)\n"); |
209 |
598 |
} else { |
210 |
277633 |
VSB_cat(e->vsb, VSB_data(e3->vsb)); |
211 |
|
} |
212 |
278231 |
break; |
213 |
|
case 'T': |
214 |
|
case 't': |
215 |
1165295 |
e3 = (*p == 'T' ? e1 : e2); |
216 |
1165295 |
AN(e3); |
217 |
1165295 |
vcc_strands_edit(e, e3); |
218 |
1165295 |
break; |
219 |
|
case '1': |
220 |
6246133 |
VSB_cat(e->vsb, VSB_data(e1->vsb)); |
221 |
6246133 |
break; |
222 |
|
case '2': |
223 |
3899903 |
AN(e2); |
224 |
3899903 |
VSB_cat(e->vsb, VSB_data(e2->vsb)); |
225 |
3899903 |
break; |
226 |
|
default: |
227 |
0 |
WRONG("Illegal edit in VCC expression"); |
228 |
0 |
} |
229 |
14121425 |
p++; |
230 |
|
} |
231 |
7688946 |
AZ(VSB_finish(e->vsb)); |
232 |
7688946 |
e->t1 = e1->t1; |
233 |
7688946 |
e->t2 = e1->t2; |
234 |
7688946 |
if (e2 != NULL) |
235 |
3900616 |
e->t2 = e2->t2; |
236 |
7688946 |
vcc_delete_expr(e1); |
237 |
7688946 |
vcc_delete_expr(e2); |
238 |
7688946 |
return (e); |
239 |
|
} |
240 |
|
|
241 |
|
/*-------------------------------------------------------------------- |
242 |
|
* Expand finished expression into C-source code |
243 |
|
*/ |
244 |
|
|
245 |
|
static void |
246 |
2115908 |
vcc_expr_fmt(struct vsb *d, int ind, const struct expr *e1) |
247 |
|
{ |
248 |
|
char *p; |
249 |
|
int i; |
250 |
|
|
251 |
2115908 |
if (!e1->fmt->noindent) { |
252 |
18824902 |
for (i = 0; i < ind; i++) |
253 |
16838806 |
VSB_putc(d, ' '); |
254 |
1986096 |
} |
255 |
2115908 |
p = VSB_data(e1->vsb); |
256 |
206998872 |
while (*p != '\0') { |
257 |
204883056 |
if (*p == '\n') { |
258 |
6220580 |
VSB_putc(d, '\n'); |
259 |
6220580 |
if (*++p == '\0') |
260 |
92 |
break; |
261 |
66648664 |
for (i = 0; i < ind; i++) |
262 |
60428176 |
VSB_putc(d, ' '); |
263 |
204882964 |
} else if (*p != '\v') { |
264 |
195789178 |
VSB_putc(d, *p++); |
265 |
195789178 |
} else { |
266 |
2873298 |
switch (*++p) { |
267 |
1436649 |
case '+': ind += INDENT; break; |
268 |
1436649 |
case '-': ind -= INDENT; break; |
269 |
0 |
default: WRONG("Illegal format in VCC expression"); |
270 |
0 |
} |
271 |
2873298 |
p++; |
272 |
|
} |
273 |
|
} |
274 |
2115908 |
} |
275 |
|
|
276 |
|
/*-------------------------------------------------------------------- |
277 |
|
*/ |
278 |
|
|
279 |
|
static void |
280 |
1620534 |
vcc_expr_tobool(struct vcc *tl, struct expr **e) |
281 |
|
{ |
282 |
|
|
283 |
1620534 |
if ((*e)->fmt == BOOL) |
284 |
1230109 |
return; |
285 |
390425 |
if ((*e)->fmt == BACKEND || (*e)->fmt == INT) |
286 |
230 |
*e = vcc_expr_edit(tl, BOOL, "(\v1 != 0)", *e, NULL); |
287 |
390195 |
else if ((*e)->fmt == DURATION) |
288 |
46 |
*e = vcc_expr_edit(tl, BOOL, "(\v1 > 0)", *e, NULL); |
289 |
390149 |
else if ((*e)->fmt == STRINGS) |
290 |
390011 |
*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 |
1620534 |
} |
297 |
|
|
298 |
|
/*-------------------------------------------------------------------- |
299 |
|
*/ |
300 |
|
|
301 |
|
static void |
302 |
1267277 |
vcc_expr_tostring(struct vcc *tl, struct expr **e) |
303 |
|
{ |
304 |
|
const char *p; |
305 |
1267277 |
uint8_t constant = EXPR_VAR; |
306 |
|
|
307 |
1267277 |
CHECK_OBJ_NOTNULL(*e, EXPR_MAGIC); |
308 |
1267277 |
assert((*e)->fmt != STRINGS); |
309 |
|
|
310 |
1267277 |
p = (*e)->fmt->tostring; |
311 |
1267277 |
if (p != NULL) { |
312 |
1267254 |
AN(*p); |
313 |
1267254 |
*e = vcc_expr_edit(tl, STRINGS, p, *e, NULL); |
314 |
1267254 |
(*e)->constant = constant; |
315 |
1267254 |
(*e)->nstr = 1; |
316 |
1267254 |
} else { |
317 |
46 |
VSB_printf(tl->sb, |
318 |
|
"Cannot convert %s to STRING.\n", |
319 |
23 |
vcc_utype((*e)->fmt)); |
320 |
23 |
vcc_ErrWhere2(tl, (*e)->t1, tl->t); |
321 |
|
} |
322 |
1267277 |
} |
323 |
|
|
324 |
|
/*-------------------------------------------------------------------- |
325 |
|
*/ |
326 |
|
|
327 |
|
void v_matchproto_(sym_expr_t) |
328 |
9568 |
vcc_Eval_Handle(struct vcc *tl, struct expr **e, struct token *t, |
329 |
|
struct symbol *sym, vcc_type_t type) |
330 |
|
{ |
331 |
|
|
332 |
9568 |
(void)t; |
333 |
9568 |
(void)tl; |
334 |
9568 |
AN(sym->rname); |
335 |
9568 |
AZ(type->stringform); |
336 |
|
|
337 |
9683 |
if (sym->type->tostring == NULL && |
338 |
115 |
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 |
9568 |
*e = vcc_mk_expr(sym->type, "%s", sym->rname); |
344 |
9568 |
(*e)->constant = EXPR_VAR; |
345 |
9568 |
(*e)->nstr = 1; |
346 |
9568 |
if ((*e)->fmt == STRING) |
347 |
0 |
(*e)->fmt = STRINGS; |
348 |
|
} |
349 |
9568 |
} |
350 |
|
|
351 |
|
void v_matchproto_(sym_expr_t) |
352 |
529 |
vcc_Eval_Sub(struct vcc *tl, struct expr **e, struct token *t, |
353 |
|
struct symbol *sym, vcc_type_t type) |
354 |
|
{ |
355 |
|
|
356 |
529 |
(void)t; |
357 |
529 |
(void)tl; |
358 |
529 |
AN(sym->rname); |
359 |
529 |
AZ(type->stringform); |
360 |
|
|
361 |
529 |
assert (sym->type == SUB); |
362 |
|
|
363 |
529 |
if (type == SUB) { |
364 |
460 |
*e = vcc_mk_expr(sym->type, "%s", sym->rname); |
365 |
460 |
(*e)->constant = EXPR_CONST; |
366 |
460 |
return; |
367 |
|
} |
368 |
|
|
369 |
138 |
VSB_printf(tl->sb, "Symbol '%s' can only be used as a %s expression\n", |
370 |
69 |
sym->name, sym->type->name); |
371 |
69 |
vcc_ErrWhere(tl, tl->t); |
372 |
529 |
} |
373 |
|
|
374 |
|
/*-------------------------------------------------------------------- |
375 |
|
*/ |
376 |
|
|
377 |
|
void v_matchproto_(sym_expr_t) |
378 |
2892664 |
vcc_Eval_Var(struct vcc *tl, struct expr **e, struct token *t, |
379 |
|
struct symbol *sym, vcc_type_t type) |
380 |
|
{ |
381 |
|
|
382 |
2892664 |
(void)type; |
383 |
2892664 |
vcc_AddUses(tl, t, NULL, sym, XREF_READ); |
384 |
2892664 |
ERRCHK(tl); |
385 |
2892664 |
*e = vcc_mk_expr(sym->type, "%s", sym->rname); |
386 |
2892664 |
(*e)->constant = EXPR_VAR; |
387 |
2892664 |
(*e)->nstr = 1; |
388 |
2892664 |
if ((*e)->fmt == STRING) |
389 |
1305112 |
(*e)->fmt = STRINGS; |
390 |
2892664 |
} |
391 |
|
|
392 |
|
void v_matchproto_(sym_expr_t) |
393 |
253 |
vcc_Eval_ProtectedHeader(struct vcc *tl, struct expr **e, struct token *t, |
394 |
|
struct symbol *sym, vcc_type_t type) |
395 |
|
{ |
396 |
|
|
397 |
253 |
AN(sym); |
398 |
253 |
AZ(sym->lorev); |
399 |
|
|
400 |
253 |
vcc_Header_Fh(tl, sym); |
401 |
253 |
sym->eval = vcc_Eval_Var; |
402 |
253 |
vcc_Eval_Var(tl, e, t, sym, type); |
403 |
253 |
} |
404 |
|
|
405 |
|
/*-------------------------------------------------------------------- |
406 |
|
*/ |
407 |
|
|
408 |
|
static struct expr * |
409 |
3956 |
vcc_priv_arg(struct vcc *tl, const char *p, struct symbol *sym) |
410 |
|
{ |
411 |
|
char buf[64]; |
412 |
|
struct inifin *ifp; |
413 |
3956 |
const char *f = NULL; |
414 |
|
|
415 |
3956 |
AN(sym); |
416 |
3956 |
AN(sym->vmod_name); |
417 |
|
|
418 |
3956 |
if (!strcmp(p, "PRIV_VCL")) |
419 |
345 |
return (vcc_mk_expr(VOID, "&vmod_priv_%s", sym->vmod_name)); |
420 |
|
|
421 |
3611 |
if (!strcmp(p, "PRIV_CALL")) { |
422 |
506 |
bprintf(buf, "vmod_priv_%u", tl->unique++); |
423 |
506 |
ifp = New_IniFin(tl); |
424 |
506 |
Fh(tl, 0, "static struct vmod_priv %s;\n", buf); |
425 |
506 |
VSB_printf(ifp->fin, "\tVRT_priv_fini(ctx, &%s);", buf); |
426 |
506 |
return (vcc_mk_expr(VOID, "&%s", buf)); |
427 |
|
} |
428 |
|
|
429 |
3105 |
if (!strcmp(p, "PRIV_TASK")) |
430 |
2921 |
f = "task"; |
431 |
184 |
else if (!strcmp(p, "PRIV_TOP")) { |
432 |
184 |
f = "top"; |
433 |
184 |
sym->r_methods &= VCL_MET_TASK_C; |
434 |
184 |
} else { |
435 |
0 |
WRONG("Wrong PRIV_ type"); |
436 |
|
} |
437 |
3105 |
AN(f); |
438 |
|
|
439 |
3105 |
return (vcc_mk_expr(VOID, "VRT_priv_%s(ctx, &VGC_vmod_%s)", |
440 |
3105 |
f, sym->vmod_name)); |
441 |
3956 |
} |
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 |
41837 |
vcc_do_enum(struct vcc *tl, const char *cfunc, int len, const char *ptr) |
457 |
|
{ |
458 |
|
const char *r; |
459 |
|
|
460 |
41837 |
(void)tl; |
461 |
41837 |
r = strchr(cfunc, '.'); |
462 |
41837 |
AN(r); |
463 |
41837 |
return (vcc_mk_expr(VOID, "*%.*s.enum_%.*s", |
464 |
41837 |
(int)(r - cfunc), cfunc, len, ptr)); |
465 |
|
} |
466 |
|
|
467 |
|
static void |
468 |
91517 |
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 |
91517 |
if (fa->type == ENUM) { |
474 |
30751 |
ExpectErr(tl, ID); |
475 |
30751 |
ERRCHK(tl); |
476 |
105133 |
VTAILQ_FOREACH(vv, &fa->enums->children, list) |
477 |
105087 |
if (vcc_IdIs(tl->t, vv->value)) |
478 |
30705 |
break; |
479 |
30751 |
if (vv == NULL) { |
480 |
46 |
VSB_cat(tl->sb, "Wrong enum value."); |
481 |
46 |
VSB_cat(tl->sb, " Expected one of:\n"); |
482 |
230 |
VTAILQ_FOREACH(vv, &fa->enums->children, list) |
483 |
184 |
VSB_printf(tl->sb, "\t%s\n", vv->value); |
484 |
46 |
vcc_ErrWhere(tl, tl->t); |
485 |
46 |
return; |
486 |
|
} |
487 |
30705 |
fa->result = vcc_do_enum(tl, cfunc, PF(tl->t)); |
488 |
30705 |
SkipToken(tl, ID); |
489 |
30705 |
} else { |
490 |
60766 |
if (fa->type == SUB) |
491 |
460 |
tl->subref++; |
492 |
60766 |
vcc_expr0(tl, &e2, fa->type); |
493 |
60766 |
ERRCHK(tl); |
494 |
60582 |
assert(e2->fmt == fa->type); |
495 |
60582 |
fa->result = e2; |
496 |
|
} |
497 |
91287 |
fa->avail = 1; |
498 |
91517 |
} |
499 |
|
|
500 |
|
static void |
501 |
63342 |
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 |
63342 |
CAST_OBJ_NOTNULL(vv, priv, VJSN_VAL_MAGIC); |
516 |
63342 |
assert(vjsn_is_array(vv)); |
517 |
63342 |
vv = VTAILQ_FIRST(&vv->children); |
518 |
63342 |
rfmt = VCC_Type(VTAILQ_FIRST(&vv->children)->value); |
519 |
63342 |
AN(rfmt); |
520 |
63342 |
vv = VTAILQ_NEXT(vv, list); |
521 |
63342 |
cfunc = vv->value; |
522 |
63342 |
vv = VTAILQ_NEXT(vv, list); |
523 |
63342 |
sa = vv->value; |
524 |
63342 |
if (*sa == '\0') { |
525 |
54027 |
sa = NULL; |
526 |
54027 |
} |
527 |
63342 |
vv = VTAILQ_NEXT(vv, list); |
528 |
63342 |
if (sym->kind == SYM_METHOD) { |
529 |
17802 |
if (*e == NULL) { |
530 |
46 |
VSB_cat(tl->sb, "Syntax error."); |
531 |
46 |
tl->err = 1; |
532 |
46 |
return; |
533 |
|
} |
534 |
17756 |
vcc_NextToken(tl); |
535 |
17756 |
AZ(extra); |
536 |
17756 |
AN((*e)->instance); |
537 |
17756 |
extra = (*e)->instance->rname; |
538 |
17756 |
} |
539 |
63296 |
tf = VTAILQ_PREV(tl->t, tokenhead, list); |
540 |
63296 |
SkipToken(tl, '('); |
541 |
63296 |
if (extra == NULL) { |
542 |
40917 |
extra = ""; |
543 |
40917 |
extra_sep = ""; |
544 |
40917 |
} else { |
545 |
22379 |
AN(*extra); |
546 |
22379 |
extra_sep = ", "; |
547 |
|
} |
548 |
63296 |
VTAILQ_INIT(&head); |
549 |
212267 |
for (;vv != NULL; vv = VTAILQ_NEXT(vv, list)) { |
550 |
148971 |
assert(vjsn_is_array(vv)); |
551 |
148971 |
fa = calloc(1, sizeof *fa); |
552 |
148971 |
AN(fa); |
553 |
148971 |
VTAILQ_INSERT_TAIL(&head, fa, list); |
554 |
|
|
555 |
148971 |
vvp = VTAILQ_FIRST(&vv->children); |
556 |
148971 |
if (!memcmp(vvp->value, "PRIV_", 5)) { |
557 |
3956 |
fa->result = vcc_priv_arg(tl, vvp->value, sym); |
558 |
3956 |
vvp = VTAILQ_NEXT(vvp, list); |
559 |
3956 |
if (vvp != NULL) |
560 |
3956 |
fa->cname = fa->name = vvp->value; |
561 |
3956 |
continue; |
562 |
|
} |
563 |
145015 |
fa->type = VCC_Type(vvp->value); |
564 |
145015 |
AN(fa->type); |
565 |
145015 |
vvp = VTAILQ_NEXT(vvp, list); |
566 |
145015 |
if (vvp != NULL) { |
567 |
145015 |
fa->name = vvp->value; |
568 |
145015 |
vvp = VTAILQ_NEXT(vvp, list); |
569 |
145015 |
AN(vvp); /* vmod_syntax 2.0 */ |
570 |
145015 |
fa->cname = vvp->value; |
571 |
145015 |
vvp = VTAILQ_NEXT(vvp, list); |
572 |
145015 |
if (vvp != NULL) { |
573 |
99682 |
fa->val = vvp->value; |
574 |
99682 |
vvp = VTAILQ_NEXT(vvp, list); |
575 |
99682 |
if (vvp != NULL) { |
576 |
81075 |
fa->enums = vvp; |
577 |
81075 |
vvp = VTAILQ_NEXT(vvp, list); |
578 |
81075 |
} |
579 |
99682 |
} |
580 |
145015 |
} |
581 |
145015 |
if (sa != NULL && vvp != NULL && vjsn_is_true(vvp)) { |
582 |
42067 |
fa->optional = 1; |
583 |
42067 |
vvp = VTAILQ_NEXT(vvp, list); |
584 |
42067 |
} |
585 |
145015 |
AZ(vvp); |
586 |
145015 |
} |
587 |
|
|
588 |
102051 |
VTAILQ_FOREACH(fa, &head, list) { |
589 |
91540 |
if (tl->t->tok == ')') |
590 |
2047 |
break; |
591 |
89493 |
if (fa->result != NULL) |
592 |
2829 |
continue; |
593 |
86664 |
if (tl->t->tok == ID) { |
594 |
63802 |
t1 = VTAILQ_NEXT(tl->t, list); |
595 |
63802 |
if (t1->tok == '=') |
596 |
17296 |
break; |
597 |
46506 |
} |
598 |
69368 |
vcc_do_arg(tl, cfunc, fa); |
599 |
69368 |
if (tl->err) |
600 |
460 |
VSB_printf(tl->sb, "Expected argument: %s %s\n\n", |
601 |
230 |
fa->type->name, |
602 |
230 |
fa->name ? fa->name : "(unnamed argument)"); |
603 |
69368 |
ERRCHK(tl); |
604 |
69138 |
if (tl->t->tok == ')') |
605 |
33212 |
break; |
606 |
35926 |
SkipToken(tl, ','); |
607 |
35926 |
} |
608 |
67965 |
while (tl->t->tok == ID) { |
609 |
72082 |
VTAILQ_FOREACH(fa, &head, list) { |
610 |
72059 |
if (fa->name == NULL) |
611 |
276 |
continue; |
612 |
71783 |
if (vcc_IdIs(tl->t, fa->name)) |
613 |
22172 |
break; |
614 |
49611 |
} |
615 |
22195 |
if (fa == NULL) { |
616 |
46 |
VSB_printf(tl->sb, "Unknown argument '%.*s'\n", |
617 |
23 |
PF(tl->t)); |
618 |
23 |
vcc_ErrWhere(tl, tl->t); |
619 |
23 |
return; |
620 |
|
} |
621 |
22172 |
if (fa->result != NULL) { |
622 |
23 |
AN(fa->name); |
623 |
46 |
VSB_printf(tl->sb, "Argument '%s' already used\n", |
624 |
23 |
fa->name); |
625 |
23 |
vcc_ErrWhere(tl, tl->t); |
626 |
23 |
return; |
627 |
|
} |
628 |
22149 |
vcc_NextToken(tl); |
629 |
22149 |
SkipToken(tl, '='); |
630 |
22149 |
vcc_do_arg(tl, cfunc, fa); |
631 |
22149 |
ERRCHK(tl); |
632 |
22149 |
if (tl->t->tok == ')') |
633 |
17250 |
break; |
634 |
4899 |
SkipToken(tl, ','); |
635 |
|
} |
636 |
|
|
637 |
63020 |
if (sa != NULL) |
638 |
18446 |
e1 = vcc_mk_expr(rfmt, "%s(ctx%s%s,\v+\n&(%s)\v+ {\n", |
639 |
9223 |
cfunc, extra_sep, extra, sa); |
640 |
|
else |
641 |
107594 |
e1 = vcc_mk_expr(rfmt, "%s(ctx%s%s\v+", |
642 |
53797 |
cfunc, extra_sep, extra); |
643 |
63020 |
n = 0; |
644 |
211232 |
VTAILQ_FOREACH_SAFE(fa, &head, list, fa2) { |
645 |
148212 |
n++; |
646 |
148212 |
if (fa->optional) { |
647 |
41929 |
AN(fa->cname); |
648 |
41929 |
bprintf(ssa, "\v1.valid_%s = %d,\n", |
649 |
|
fa->cname, fa->avail); |
650 |
41929 |
e1 = vcc_expr_edit(tl, e1->fmt, ssa, e1, NULL); |
651 |
41929 |
} |
652 |
148212 |
if (fa->result == NULL && fa->type == ENUM && fa->val != NULL) |
653 |
11132 |
fa->result = vcc_do_enum(tl, cfunc, strlen(fa->val), fa->val); |
654 |
148212 |
if (fa->result == NULL && fa->val != NULL) |
655 |
13754 |
fa->result = vcc_mk_expr(fa->type, "%s", fa->val); |
656 |
148212 |
if (fa->result != NULL && sa != NULL) { |
657 |
19964 |
if (fa->cname) |
658 |
19481 |
bprintf(ssa, "\v1.%s = \v2,\n", fa->cname); |
659 |
|
else |
660 |
483 |
bprintf(ssa, "\v1.arg%d = \v2,\n", n); |
661 |
19964 |
e1 = vcc_expr_edit(tl, e1->fmt, ssa, e1, fa->result); |
662 |
148212 |
} else if (fa->result != NULL) { |
663 |
200100 |
e1 = vcc_expr_edit(tl, e1->fmt, "\v1,\n\v2", |
664 |
100050 |
e1, fa->result); |
665 |
128248 |
} else if (!fa->optional) { |
666 |
23 |
if (fa->name) |
667 |
46 |
VSB_printf(tl->sb, "Argument '%s' missing\n", |
668 |
23 |
fa->name); |
669 |
|
else |
670 |
0 |
VSB_printf(tl->sb, "Argument %d missing\n", n); |
671 |
23 |
vcc_ErrWhere(tl, tl->t); |
672 |
23 |
} |
673 |
148212 |
free(fa); |
674 |
148212 |
} |
675 |
63020 |
if (sa != NULL) { |
676 |
9223 |
*e = vcc_expr_edit(tl, e1->fmt, "\v1\v-\n}\v-\n)", e1, NULL); |
677 |
9223 |
} else { |
678 |
53797 |
*e = vcc_expr_edit(tl, e1->fmt, "\v1\v-\n)", e1, NULL); |
679 |
|
} |
680 |
63020 |
SkipToken(tl, ')'); |
681 |
62997 |
vcc_AddUses(tl, tf, NULL, sym, XREF_READ); |
682 |
63342 |
} |
683 |
|
|
684 |
|
|
685 |
|
/*-------------------------------------------------------------------- |
686 |
|
*/ |
687 |
|
|
688 |
|
void |
689 |
4623 |
vcc_Eval_Func(struct vcc *tl, const struct vjsn_val *spec, |
690 |
|
const char *extra, struct symbol *sym) |
691 |
|
{ |
692 |
4623 |
struct expr *e = NULL; |
693 |
|
|
694 |
4623 |
vcc_func(tl, &e, spec, extra, sym); |
695 |
4623 |
if (tl->err) |
696 |
0 |
VSB_cat(tl->sb, "While compiling function call:\n"); |
697 |
4623 |
ERRCHK(tl); |
698 |
4623 |
vcc_expr_fmt(tl->fb, tl->indent, e); |
699 |
4623 |
VSB_cat(tl->fb, ";\n"); |
700 |
4623 |
vcc_delete_expr(e); |
701 |
4623 |
} |
702 |
|
|
703 |
|
/*-------------------------------------------------------------------- |
704 |
|
*/ |
705 |
|
|
706 |
|
void v_matchproto_(sym_expr_t) |
707 |
47817 |
vcc_Eval_SymFunc(struct vcc *tl, struct expr **e, struct token *t, |
708 |
|
struct symbol *sym, vcc_type_t fmt) |
709 |
|
{ |
710 |
|
|
711 |
47817 |
(void)t; |
712 |
47817 |
(void)fmt; |
713 |
47817 |
assert(sym->kind == SYM_FUNC || sym->kind == SYM_METHOD); |
714 |
47817 |
AN(sym->eval_priv); |
715 |
|
|
716 |
47817 |
vcc_func(tl, e, sym->eval_priv, sym->extra, sym); |
717 |
47817 |
ERRCHK(tl); |
718 |
47587 |
if ((*e)->fmt == STRING) { |
719 |
17296 |
(*e)->fmt = STRINGS; |
720 |
17296 |
(*e)->nstr = 1; |
721 |
17296 |
} |
722 |
47817 |
} |
723 |
|
|
724 |
|
/*-------------------------------------------------------------------- |
725 |
|
*/ |
726 |
|
|
727 |
|
static void |
728 |
544870 |
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 |
544870 |
assert(fmt != VOID); |
735 |
544870 |
if (fmt == BYTES) { |
736 |
1104 |
vcc_ByteVal(tl, &vi); |
737 |
1104 |
ERRCHK(tl); |
738 |
1058 |
e1 = vcc_mk_expr(BYTES, "%ju", (intmax_t)vi); |
739 |
1058 |
} else { |
740 |
543766 |
t = tl->t; |
741 |
543766 |
vcc_NextToken(tl); |
742 |
543766 |
if (tl->t->tok == ID) { |
743 |
134688 |
e1 = vcc_mk_expr(DURATION, "%s%.3f * %g", |
744 |
134688 |
sign, t->num, vcc_DurationUnit(tl)); |
745 |
134688 |
ERRCHK(tl); |
746 |
543743 |
} else if (fmt == REAL || t->tok == FNUM) { |
747 |
3542 |
e1 = vcc_mk_expr(REAL, "%s%.3f", sign, t->num); |
748 |
3542 |
} else { |
749 |
405536 |
e1 = vcc_mk_expr(INT, "%s%.0f", sign, t->num); |
750 |
|
} |
751 |
|
} |
752 |
544801 |
e1->constant = EXPR_CONST; |
753 |
544801 |
*e = e1; |
754 |
544870 |
} |
755 |
|
|
756 |
|
/*-------------------------------------------------------------------- |
757 |
|
* SYNTAX: |
758 |
|
* Expr5: |
759 |
|
* '(' ExprCor ')' |
760 |
|
* symbol |
761 |
|
* CNUM |
762 |
|
* FNUM |
763 |
|
* CSTR |
764 |
|
* CBLOB |
765 |
|
*/ |
766 |
|
|
767 |
|
static void |
768 |
6011119 |
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 |
6011119 |
sign = ""; |
776 |
6011119 |
*e = NULL; |
777 |
6011119 |
if (tl->t->tok == '(') { |
778 |
65596 |
SkipToken(tl, '('); |
779 |
65596 |
vcc_expr_cor(tl, &e2, fmt); |
780 |
65596 |
ERRCHK(tl); |
781 |
65596 |
SkipToken(tl, ')'); |
782 |
65596 |
if (e2->fmt == STRINGS) |
783 |
138 |
*e = e2; |
784 |
|
else |
785 |
65458 |
*e = vcc_expr_edit(tl, e2->fmt, "(\v1)", e2, NULL); |
786 |
65596 |
return; |
787 |
|
} |
788 |
5945523 |
switch (tl->t->tok) { |
789 |
|
case ID: |
790 |
3022269 |
t = tl->t; |
791 |
3022269 |
t1 = vcc_PeekToken(tl); |
792 |
3022269 |
AN(t1); |
793 |
3022269 |
sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_NONE, |
794 |
|
SYMTAB_PARTIAL_NOERR, XREF_REF); |
795 |
3022269 |
if (sym == NULL && fmt->global_pfx != NULL && t1->tok != '.') { |
796 |
207 |
sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_NONE, |
797 |
|
SYMTAB_CREATE, XREF_REF); |
798 |
207 |
ERRCHK(tl); |
799 |
161 |
AN(sym); |
800 |
161 |
VCC_GlobalSymbol(sym, fmt); |
801 |
161 |
} |
802 |
3022223 |
ERRCHK(tl); |
803 |
3022223 |
if (sym == NULL) |
804 |
276 |
AZ(VCC_SymbolGet(tl, SYM_MAIN, SYM_NONE, |
805 |
|
SYMTAB_PARTIAL, XREF_REF)); |
806 |
3022223 |
ERRCHK(tl); |
807 |
3021947 |
AN(sym); |
808 |
3021947 |
if (sym->kind == SYM_INSTANCE) { |
809 |
17802 |
AZ(*e); |
810 |
17802 |
*e = vcc_new_expr(sym->type); |
811 |
17802 |
(*e)->instance = sym; |
812 |
17802 |
return; |
813 |
|
} |
814 |
3004145 |
if (sym->kind == SYM_FUNC && sym->type == VOID) { |
815 |
23 |
VSB_cat(tl->sb, "Function returns VOID:\n"); |
816 |
23 |
vcc_ErrWhere(tl, tl->t); |
817 |
23 |
return; |
818 |
|
} |
819 |
3004122 |
if (sym->eval != NULL) { |
820 |
3004053 |
AN(sym->eval); |
821 |
3004053 |
AZ(*e); |
822 |
3004053 |
sym->eval(tl, e, t, sym, fmt); |
823 |
3004053 |
if (tl->err) { |
824 |
368 |
VSB_cat(tl->sb, |
825 |
|
"While compiling function call:\n\n"); |
826 |
368 |
vcc_ErrWhere2(tl, t, tl->t); |
827 |
368 |
} |
828 |
3004053 |
ERRCHK(tl); |
829 |
|
/* Unless asked for a HEADER, fold to string here */ |
830 |
3003685 |
if (*e && fmt != HEADER && (*e)->fmt == HEADER) { |
831 |
798951 |
vcc_expr_tostring(tl, e); |
832 |
798951 |
ERRCHK(tl); |
833 |
798951 |
} |
834 |
3003685 |
return; |
835 |
|
} |
836 |
138 |
VSB_printf(tl->sb, |
837 |
|
"Symbol '%.*s' type (%s) cannot be used in expression.\n", |
838 |
69 |
PF(t), sym->kind->name); |
839 |
69 |
vcc_ErrWhere(tl, t); |
840 |
69 |
if (sym->def_b != NULL) { |
841 |
23 |
VSB_cat(tl->sb, "That symbol was defined here:\n"); |
842 |
23 |
vcc_ErrWhere(tl, sym->def_b); |
843 |
23 |
} |
844 |
69 |
return; |
845 |
|
case CSTR: |
846 |
2378039 |
assert(fmt != VOID); |
847 |
2378039 |
if (fmt == IP) { |
848 |
690 |
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 |
23 |
VSB_cat(tl->sb, |
856 |
|
"Cannot convert to an IP address: "); |
857 |
23 |
vcc_ErrToken(tl, tl->t); |
858 |
23 |
vcc_ErrWhere(tl, tl->t); |
859 |
23 |
return; |
860 |
|
} |
861 |
1334 |
Resolve_Sockaddr(tl, tl->t->dec, "80", |
862 |
|
&ip, NULL, &ip, NULL, NULL, 1, |
863 |
667 |
tl->t, "IP constant"); |
864 |
667 |
ERRCHK(tl); |
865 |
598 |
e1 = vcc_mk_expr(IP, "%s", ip); |
866 |
598 |
ERRCHK(tl); |
867 |
2377947 |
} else if (fmt == REGEX) { |
868 |
196972 |
e1 = vcc_new_expr(REGEX); |
869 |
196972 |
vcc_regexp(tl, e1->vsb); |
870 |
196972 |
AZ(VSB_finish(e1->vsb)); |
871 |
196972 |
} else { |
872 |
2180377 |
e1 = vcc_new_expr(STRINGS); |
873 |
2180377 |
EncToken(e1->vsb, tl->t); |
874 |
2180377 |
AZ(VSB_finish(e1->vsb)); |
875 |
2180377 |
e1->constant |= EXPR_STR_CONST; |
876 |
2180377 |
e1->nstr = 1; |
877 |
|
} |
878 |
2377947 |
e1->t1 = tl->t; |
879 |
2377947 |
e1->constant |= EXPR_CONST; |
880 |
2377947 |
vcc_NextToken(tl); |
881 |
2377947 |
*e = e1; |
882 |
2377947 |
return; |
883 |
|
case '-': |
884 |
1426 |
if (fmt != INT && |
885 |
368 |
fmt != REAL && |
886 |
92 |
fmt != DURATION && |
887 |
23 |
fmt != STRINGS) |
888 |
0 |
break; |
889 |
1403 |
vcc_NextToken(tl); |
890 |
1403 |
if (tl->t->tok != FNUM && tl->t->tok != CNUM) { |
891 |
207 |
vcc_expr_cor(tl, &e1, fmt); |
892 |
207 |
ERRCHK(tl); |
893 |
207 |
*e = vcc_expr_edit(tl, e1->fmt, "-(\v1)", e1, NULL); |
894 |
207 |
return; |
895 |
|
} |
896 |
1196 |
sign = "-"; |
897 |
|
/* FALLTHROUGH */ |
898 |
|
case FNUM: |
899 |
|
case CNUM: |
900 |
544870 |
vcc_number(tl, e, fmt, sign); |
901 |
544870 |
return; |
902 |
|
case CBLOB: |
903 |
46 |
e1 = vcc_new_expr(BLOB); |
904 |
46 |
VSB_printf(e1->vsb, "%s", tl->t->dec); |
905 |
46 |
AZ(VSB_finish(e1->vsb)); |
906 |
46 |
e1->constant |= EXPR_STR_CONST; |
907 |
46 |
e1->t1 = tl->t; |
908 |
46 |
vcc_NextToken(tl); |
909 |
46 |
*e = e1; |
910 |
46 |
return; |
911 |
|
default: |
912 |
92 |
break; |
913 |
|
} |
914 |
92 |
VSB_cat(tl->sb, "Unknown token "); |
915 |
92 |
vcc_ErrToken(tl, tl->t); |
916 |
92 |
VSB_printf(tl->sb, " when looking for %s\n\n", vcc_utype(fmt)); |
917 |
92 |
vcc_ErrWhere(tl, tl->t); |
918 |
6011119 |
} |
919 |
|
|
920 |
|
/*-------------------------------------------------------------------- |
921 |
|
* SYNTAX: |
922 |
|
* Expr4: |
923 |
|
* Expr5 [ '.' (type_attribute | type_method()) ]* |
924 |
|
*/ |
925 |
|
|
926 |
|
void |
927 |
65113 |
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 |
65113 |
(void)t; |
933 |
65113 |
impl = VCC_Type_EvalMethod(tl, sym); |
934 |
65113 |
ERRCHK(tl); |
935 |
65113 |
AN(impl); |
936 |
65113 |
*e = vcc_expr_edit(tl, fmt, impl, *e, NULL); |
937 |
65113 |
} |
938 |
|
|
939 |
|
static void |
940 |
6011119 |
vcc_expr4(struct vcc *tl, struct expr **e, vcc_type_t fmt) |
941 |
|
{ |
942 |
|
struct symbol *sym; |
943 |
|
|
944 |
6011119 |
*e = NULL; |
945 |
6011119 |
vcc_expr5(tl, e, fmt); |
946 |
6011119 |
ERRCHK(tl); |
947 |
6010015 |
AN(*e); |
948 |
6092884 |
while (tl->t->tok == '.') { |
949 |
82938 |
vcc_NextToken(tl); |
950 |
82938 |
ExpectErr(tl, ID); |
951 |
|
|
952 |
82915 |
sym = VCC_TypeSymbol(tl, SYM_METHOD, (*e)->fmt); |
953 |
82915 |
if (sym == NULL) { |
954 |
46 |
VSB_cat(tl->sb, "Unknown property "); |
955 |
46 |
vcc_ErrToken(tl, tl->t); |
956 |
46 |
VSB_printf(tl->sb, " for type %s\n", (*e)->fmt->name); |
957 |
46 |
vcc_ErrWhere(tl, tl->t); |
958 |
46 |
return; |
959 |
|
} |
960 |
|
|
961 |
82869 |
AN(sym->eval); |
962 |
82869 |
sym->eval(tl, e, tl->t, sym, sym->type); |
963 |
82869 |
ERRCHK(tl); |
964 |
82869 |
if ((*e)->fmt == STRING) { |
965 |
64860 |
(*e)->fmt = STRINGS; |
966 |
64860 |
(*e)->nstr = 1; |
967 |
64860 |
} |
968 |
|
} |
969 |
6011119 |
} |
970 |
|
|
971 |
|
/*-------------------------------------------------------------------- |
972 |
|
* SYNTAX: |
973 |
|
* ExprMul: |
974 |
|
* Expr4 { {'*'|'/'|'%'} Expr4 } * |
975 |
|
*/ |
976 |
|
|
977 |
|
static void |
978 |
5805591 |
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 |
5805591 |
*e = NULL; |
986 |
5805591 |
vcc_expr4(tl, e, fmt); |
987 |
5805591 |
ERRCHK(tl); |
988 |
5804579 |
AN(*e); |
989 |
|
|
990 |
5805430 |
while (tl->t->tok == '*' || tl->t->tok == '/' || tl->t->tok == '%') { |
991 |
943 |
if (tl->t->tok == '%' && ((*e)->fmt != INT)) { |
992 |
23 |
VSB_cat(tl->sb, "Operator % only possible on INT.\n"); |
993 |
23 |
vcc_ErrWhere(tl, tl->t); |
994 |
23 |
return; |
995 |
|
} |
996 |
920 |
f2 = (*e)->fmt->multype; |
997 |
920 |
if (f2 == NULL) { |
998 |
46 |
VSB_printf(tl->sb, |
999 |
|
"Operator %.*s not possible on type %s.\n", |
1000 |
23 |
PF(tl->t), vcc_utype((*e)->fmt)); |
1001 |
23 |
vcc_ErrWhere(tl, tl->t); |
1002 |
23 |
return; |
1003 |
|
} |
1004 |
897 |
tk = tl->t; |
1005 |
897 |
vcc_NextToken(tl); |
1006 |
897 |
vcc_expr4(tl, &e2, f2); |
1007 |
897 |
ERRCHK(tl); |
1008 |
897 |
if (e2->fmt != INT && e2->fmt != f2) { |
1009 |
92 |
VSB_printf(tl->sb, "%s %.*s %s not possible.\n", |
1010 |
46 |
vcc_utype((*e)->fmt), PF(tk), vcc_utype(e2->fmt)); |
1011 |
46 |
vcc_ErrWhere(tl, tk); |
1012 |
46 |
return; |
1013 |
|
} |
1014 |
851 |
bprintf(buf, "(\v1%c\v2)", tk->tok); |
1015 |
851 |
*e = vcc_expr_edit(tl, (*e)->fmt, buf, *e, e2); |
1016 |
|
} |
1017 |
5805591 |
} |
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 |
4241476 |
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 |
4241476 |
*e = NULL; |
1059 |
4241476 |
vcc_expr_mul(tl, e, fmt); |
1060 |
4241476 |
ERRCHK(tl); |
1061 |
|
|
1062 |
5804234 |
while (tl->t->tok == '+' || tl->t->tok == '-') { |
1063 |
1564115 |
tk = tl->t; |
1064 |
25008498 |
for (ap = vcc_adds; ap->op != EOI; ap++) |
1065 |
23446108 |
if (tk->tok == ap->op && (*e)->fmt == ap->a) |
1066 |
1725 |
break; |
1067 |
1564115 |
vcc_NextToken(tl); |
1068 |
1564115 |
if (ap->op == EOI && fmt == STRINGS) |
1069 |
11592 |
vcc_expr_mul(tl, &e2, STRINGS); |
1070 |
|
else |
1071 |
1552523 |
vcc_expr_mul(tl, &e2, (*e)->fmt); |
1072 |
1564115 |
ERRCHK(tl); |
1073 |
|
|
1074 |
25011304 |
for (ap = vcc_adds; ap->op != EOI; ap++) |
1075 |
23448684 |
if (tk->tok == ap->op && (*e)->fmt == ap->a && |
1076 |
23448684 |
e2->fmt == ap->b) |
1077 |
1495 |
break; |
1078 |
|
|
1079 |
1564115 |
if (ap->op == '+') { |
1080 |
1035 |
*e = vcc_expr_edit(tl, ap->fmt, "(\v1 + \v2)", *e, e2); |
1081 |
1564115 |
} else if (ap->op == '-') { |
1082 |
460 |
*e = vcc_expr_edit(tl, ap->fmt, "(\v1 - \v2)", *e, e2); |
1083 |
1563425 |
} else if (tk->tok == '+' && |
1084 |
1562597 |
((*e)->fmt == STRINGS || fmt == STRINGS)) { |
1085 |
1562367 |
if ((*e)->fmt != STRINGS) |
1086 |
115 |
vcc_expr_tostring(tl, e); |
1087 |
1562367 |
if (e2->fmt != STRINGS) |
1088 |
388930 |
vcc_expr_tostring(tl, &e2); |
1089 |
1562367 |
if (vcc_islit(*e) && vcc_isconst(e2)) { |
1090 |
1150 |
lit = vcc_islit(e2); |
1091 |
2300 |
*e = vcc_expr_edit(tl, STRINGS, |
1092 |
1150 |
"\v1\n\v2", *e, e2); |
1093 |
1150 |
(*e)->constant = EXPR_CONST; |
1094 |
1150 |
(*e)->nstr = 1; |
1095 |
1150 |
if (lit) |
1096 |
1150 |
(*e)->constant |= EXPR_STR_CONST; |
1097 |
1150 |
} else { |
1098 |
1561217 |
n = (*e)->nstr + e2->nstr; |
1099 |
3122434 |
*e = vcc_expr_edit(tl, STRINGS, |
1100 |
1561217 |
"\v1,\n\v2", *e, e2); |
1101 |
1561217 |
(*e)->constant = EXPR_VAR; |
1102 |
1561217 |
(*e)->nstr = n; |
1103 |
|
} |
1104 |
1562367 |
} else { |
1105 |
506 |
VSB_printf(tl->sb, "%s %.*s %s not possible.\n", |
1106 |
253 |
vcc_utype((*e)->fmt), PF(tk), vcc_utype(e2->fmt)); |
1107 |
253 |
vcc_ErrWhere2(tl, tk, tl->t); |
1108 |
253 |
return; |
1109 |
|
} |
1110 |
|
} |
1111 |
4241476 |
} |
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 |
261671 |
cmp_simple(struct vcc *tl, struct expr **e, const struct cmps *cp) |
1139 |
|
{ |
1140 |
|
struct expr *e2; |
1141 |
|
struct token *tk; |
1142 |
|
|
1143 |
261671 |
tk = tl->t; |
1144 |
261671 |
vcc_NextToken(tl); |
1145 |
261671 |
vcc_expr_add(tl, &e2, (*e)->fmt); |
1146 |
261671 |
ERRCHK(tl); |
1147 |
|
|
1148 |
261602 |
if (e2->fmt != (*e)->fmt) { |
1149 |
92 |
VSB_printf(tl->sb, |
1150 |
|
"Comparison of different types: %s '%.*s' %s\n", |
1151 |
46 |
vcc_utype((*e)->fmt), PF(tk), vcc_utype(e2->fmt)); |
1152 |
46 |
vcc_ErrWhere(tl, tk); |
1153 |
46 |
} else |
1154 |
261556 |
*e = vcc_expr_edit(tl, BOOL, cp->emit, *e, e2); |
1155 |
261671 |
} |
1156 |
|
|
1157 |
|
static void v_matchproto_(cmp_f) |
1158 |
195937 |
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 |
195937 |
*e = vcc_expr_edit(tl, STRING, "\vS", *e, NULL); |
1165 |
195937 |
vcc_NextToken(tl); |
1166 |
195937 |
t1 = tl->t; |
1167 |
195937 |
vcc_expr4(tl, &e2, REGEX); |
1168 |
195937 |
ERRCHK(tl); |
1169 |
195845 |
vcc_expr_typecheck(tl, &e2, REGEX, t1); |
1170 |
195845 |
ERRCHK(tl); |
1171 |
195822 |
bprintf(buf, "%sVRT_re_match(ctx, \v1, \v2)", cp->emit); |
1172 |
195822 |
*e = vcc_expr_edit(tl, BOOL, buf, *e, e2); |
1173 |
195937 |
} |
1174 |
|
|
1175 |
|
static void v_matchproto_(cmp_f) |
1176 |
1035 |
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 |
1035 |
vcc_NextToken(tl); |
1183 |
1035 |
t1 = tl->t; |
1184 |
1035 |
vcc_expr4(tl, &e2, ACL); |
1185 |
1035 |
ERRCHK(tl); |
1186 |
966 |
vcc_expr_typecheck(tl, &e2, ACL, t1); |
1187 |
966 |
ERRCHK(tl); |
1188 |
897 |
bprintf(buf, "%sVRT_acl_match(ctx, \v1, \v2)", cp->emit); |
1189 |
897 |
*e = vcc_expr_edit(tl, BOOL, buf, e2, *e); |
1190 |
1035 |
} |
1191 |
|
|
1192 |
|
static void v_matchproto_(cmp_f) |
1193 |
915423 |
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 |
915423 |
tk = tl->t; |
1200 |
915423 |
vcc_NextToken(tl); |
1201 |
915423 |
vcc_expr_add(tl, &e2, STRINGS); |
1202 |
915423 |
ERRCHK(tl); |
1203 |
915423 |
if (vcc_stringstype(e2->fmt) != STRINGS) { |
1204 |
138 |
VSB_printf(tl->sb, |
1205 |
|
"Comparison of different types: %s '%.*s' %s\n", |
1206 |
69 |
vcc_utype((*e)->fmt), PF(tk), vcc_utype(e2->fmt)); |
1207 |
69 |
vcc_ErrWhere(tl, tk); |
1208 |
915423 |
} else if ((*e)->nstr == 1 && e2->nstr == 1) { |
1209 |
914641 |
bprintf(buf, "(%s VRT_strcmp(\v1, \v2))", cp->emit); |
1210 |
914641 |
*e = vcc_expr_edit(tl, BOOL, buf, *e, e2); |
1211 |
914641 |
} else { |
1212 |
713 |
bprintf(buf, "(%s VRT_CompareStrands(\vT, \vt))", cp->emit); |
1213 |
713 |
*e = vcc_expr_edit(tl, BOOL, buf, *e, e2); |
1214 |
|
} |
1215 |
915423 |
} |
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 |
3064382 |
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 |
3064382 |
*e = NULL; |
1272 |
3064382 |
vcc_expr_add(tl, e, fmt); |
1273 |
3064382 |
ERRCHK(tl); |
1274 |
3063094 |
tk = tl->t; |
1275 |
|
|
1276 |
153535304 |
for (cp = vcc_cmps; cp->fmt != VOID; cp++) { |
1277 |
151846276 |
if (tl->t->tok != cp->token) |
1278 |
138309741 |
continue; |
1279 |
13536535 |
if (vcc_stringstype((*e)->fmt) != cp->fmt) |
1280 |
12162469 |
continue; |
1281 |
1374066 |
AN(cp->func); |
1282 |
1374066 |
cp->func(tl, e, cp); |
1283 |
1374066 |
return; |
1284 |
|
} |
1285 |
|
|
1286 |
1689028 |
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 |
92 |
VSB_printf(tl->sb, "Operator %.*s not possible on %s\n", |
1296 |
46 |
PF(tl->t), vcc_utype((*e)->fmt)); |
1297 |
46 |
vcc_ErrWhere(tl, tl->t); |
1298 |
46 |
return; |
1299 |
|
default: |
1300 |
1688982 |
break; |
1301 |
|
} |
1302 |
3064382 |
} |
1303 |
|
|
1304 |
|
/*-------------------------------------------------------------------- |
1305 |
|
* SYNTAX: |
1306 |
|
* ExprNot: |
1307 |
|
* '!' ExprCmp |
1308 |
|
*/ |
1309 |
|
|
1310 |
|
static void |
1311 |
3064382 |
vcc_expr_not(struct vcc *tl, struct expr **e, vcc_type_t fmt) |
1312 |
|
{ |
1313 |
|
struct token *tk; |
1314 |
|
|
1315 |
3064382 |
*e = NULL; |
1316 |
3064382 |
tk = tl->t; |
1317 |
3064382 |
if (tl->t->tok == '!') |
1318 |
131031 |
vcc_NextToken(tl); |
1319 |
3064382 |
vcc_expr_cmp(tl, e, fmt); |
1320 |
3064382 |
ERRCHK(tl); |
1321 |
3062611 |
if (tk->tok != '!') |
1322 |
2931603 |
return; |
1323 |
131008 |
vcc_expr_tobool(tl, e); |
1324 |
131008 |
ERRCHK(tl); |
1325 |
131008 |
if ((*e)->fmt != BOOL) { |
1326 |
23 |
VSB_cat(tl->sb, "'!' must be followed by BOOL, found "); |
1327 |
23 |
VSB_printf(tl->sb, "%s.\n", vcc_utype((*e)->fmt)); |
1328 |
23 |
vcc_ErrWhere2(tl, tk, tl->t); |
1329 |
23 |
} else { |
1330 |
130985 |
*e = vcc_expr_edit(tl, BOOL, "!(\v1)", *e, NULL); |
1331 |
|
} |
1332 |
3064382 |
} |
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 |
4512416 |
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 |
4512416 |
*e = NULL; |
1351 |
4512416 |
tk = tl->t; |
1352 |
4512416 |
up(tl, e, fmt); |
1353 |
4512416 |
ERRCHK(tl); |
1354 |
4508782 |
if (tl->t->tok != ourtok) |
1355 |
4120335 |
return; |
1356 |
388447 |
vcc_expr_tobool(tl, e); |
1357 |
388447 |
ERRCHK(tl); |
1358 |
388447 |
if ((*e)->fmt != BOOL) { |
1359 |
92 |
VSB_printf(tl->sb, |
1360 |
|
"'%s' must be preceded by BOOL," |
1361 |
46 |
" found %s.\n", tokstr, vcc_utype((*e)->fmt)); |
1362 |
46 |
vcc_ErrWhere2(tl, tk, tl->t); |
1363 |
46 |
return; |
1364 |
|
} |
1365 |
388401 |
*e = vcc_expr_edit(tl, BOOL, "(\v+\n\v1", *e, NULL); |
1366 |
1229005 |
while (tl->t->tok == ourtok) { |
1367 |
840650 |
vcc_NextToken(tl); |
1368 |
840650 |
tk = tl->t; |
1369 |
840650 |
up(tl, &e2, fmt); |
1370 |
840650 |
ERRCHK(tl); |
1371 |
840650 |
vcc_expr_tobool(tl, &e2); |
1372 |
840650 |
ERRCHK(tl); |
1373 |
840650 |
if (e2->fmt != BOOL) { |
1374 |
92 |
VSB_printf(tl->sb, |
1375 |
|
"'%s' must be followed by BOOL," |
1376 |
46 |
" found %s.\n", tokstr, vcc_utype(e2->fmt)); |
1377 |
46 |
vcc_ErrWhere2(tl, tk, tl->t); |
1378 |
46 |
vcc_delete_expr(e2); |
1379 |
46 |
return; |
1380 |
|
} |
1381 |
840604 |
bprintf(buf, "\v1\v-\n%s\v+\n\v2", tokstr); |
1382 |
840604 |
*e = vcc_expr_edit(tl, BOOL, buf, *e, e2); |
1383 |
|
} |
1384 |
388355 |
*e = vcc_expr_edit(tl, BOOL, "\v1\v-\n)", *e, NULL); |
1385 |
4512416 |
} |
1386 |
|
|
1387 |
|
/*-------------------------------------------------------------------- |
1388 |
|
* SYNTAX: |
1389 |
|
* ExprCand: |
1390 |
|
* ExprNot { '&&' ExprNot } * |
1391 |
|
*/ |
1392 |
|
|
1393 |
|
static void |
1394 |
2288684 |
vcc_expr_cand(struct vcc *tl, struct expr **e, vcc_type_t fmt) |
1395 |
|
{ |
1396 |
|
|
1397 |
2288684 |
vcc_expr_bin_bool(tl, e, fmt, T_CAND, vcc_expr_not, "&&"); |
1398 |
2288684 |
} |
1399 |
|
|
1400 |
|
/*-------------------------------------------------------------------- |
1401 |
|
* SYNTAX: |
1402 |
|
* ExprCOR: |
1403 |
|
* ExprCand { '||' ExprCand } * |
1404 |
|
*/ |
1405 |
|
|
1406 |
|
static void |
1407 |
2223732 |
vcc_expr_cor(struct vcc *tl, struct expr **e, vcc_type_t fmt) |
1408 |
|
{ |
1409 |
|
|
1410 |
2223732 |
vcc_expr_bin_bool(tl, e, fmt, T_COR, vcc_expr_cand, "||"); |
1411 |
2223732 |
} |
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 |
2157929 |
vcc_expr0(struct vcc *tl, struct expr **e, vcc_type_t fmt) |
1420 |
|
{ |
1421 |
|
struct token *t1; |
1422 |
|
|
1423 |
2157929 |
assert(fmt != VOID); |
1424 |
2157929 |
assert(fmt != STRINGS); |
1425 |
2157929 |
*e = NULL; |
1426 |
2157929 |
t1 = tl->t; |
1427 |
2157929 |
if (fmt->stringform) |
1428 |
662331 |
vcc_expr_cor(tl, e, STRINGS); |
1429 |
|
else |
1430 |
1495598 |
vcc_expr_cor(tl, e, fmt); |
1431 |
2157929 |
ERRCHK(tl); |
1432 |
|
|
1433 |
2156043 |
if ((*e)->fmt == fmt) |
1434 |
1104138 |
return; |
1435 |
|
|
1436 |
1051905 |
if ((*e)->fmt != STRINGS && fmt->stringform) |
1437 |
79258 |
vcc_expr_tostring(tl, e); |
1438 |
|
|
1439 |
1051905 |
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 |
1051905 |
if (fmt == BODY && !(*e)->fmt->bodyform) |
1448 |
23 |
vcc_expr_tostring(tl, e); |
1449 |
|
|
1450 |
1051905 |
if (fmt == BODY && (*e)->fmt->bodyform) { |
1451 |
129812 |
if ((*e)->fmt == STRINGS) |
1452 |
129720 |
*e = vcc_expr_edit(tl, BODY, "STRING, 0, \vT", *e, NULL); |
1453 |
92 |
else if ((*e)->fmt == BLOB) |
1454 |
92 |
*e = vcc_expr_edit(tl, BODY, "BLOB, 0, \v1", *e, NULL); |
1455 |
|
else |
1456 |
0 |
WRONG("Unhandled bodyform"); |
1457 |
129812 |
} |
1458 |
|
|
1459 |
1051905 |
if ((*e)->fmt == STRINGS && fmt->stringform) { |
1460 |
661572 |
if (fmt == STRING) |
1461 |
82294 |
*e = vcc_expr_edit(tl, STRING, "\vS", *e, NULL); |
1462 |
579278 |
else if (fmt == STRANDS) |
1463 |
579278 |
*e = vcc_expr_edit(tl, STRANDS, "\vT", (*e), NULL); |
1464 |
|
else |
1465 |
0 |
WRONG("Unhandled stringform"); |
1466 |
661572 |
} |
1467 |
|
|
1468 |
1051905 |
if (fmt == BOOL) { |
1469 |
260429 |
vcc_expr_tobool(tl, e); |
1470 |
260429 |
ERRCHK(tl); |
1471 |
260429 |
} |
1472 |
|
|
1473 |
1051905 |
vcc_expr_typecheck(tl, e, fmt, t1); |
1474 |
2157929 |
} |
1475 |
|
|
1476 |
|
static void |
1477 |
1248716 |
vcc_expr_typecheck(struct vcc *tl, struct expr **e, vcc_type_t fmt, |
1478 |
|
struct token *t1) |
1479 |
|
{ |
1480 |
|
|
1481 |
1248716 |
assert(fmt != VOID); |
1482 |
1248716 |
assert(fmt != STRINGS); |
1483 |
|
|
1484 |
1248716 |
if (fmt != (*e)->fmt) { |
1485 |
414 |
VSB_printf(tl->sb, "Expression has type %s, expected %s\n", |
1486 |
207 |
vcc_utype((*e)->fmt), vcc_utype(fmt)); |
1487 |
207 |
vcc_ErrWhere2(tl, t1, tl->t); |
1488 |
207 |
} |
1489 |
1248716 |
} |
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 |
2094633 |
vcc_Expr(struct vcc *tl, vcc_type_t fmt) |
1500 |
|
{ |
1501 |
2094633 |
struct expr *e = NULL; |
1502 |
|
|
1503 |
2094633 |
assert(fmt != VOID); |
1504 |
2094633 |
assert(fmt != STRINGS); |
1505 |
2094633 |
vcc_expr0(tl, &e, fmt); |
1506 |
2094633 |
ERRCHK(tl); |
1507 |
2092839 |
assert(e->fmt == fmt); |
1508 |
|
|
1509 |
2092839 |
vcc_expr_fmt(tl->fb, tl->indent, e); |
1510 |
2092839 |
VSB_cat(tl->fb, "\n"); |
1511 |
2092839 |
vcc_delete_expr(e); |
1512 |
2094633 |
} |
1513 |
|
|
1514 |
|
/*-------------------------------------------------------------------- |
1515 |
|
*/ |
1516 |
|
|
1517 |
|
void v_matchproto_(sym_act_f) |
1518 |
10902 |
vcc_Act_Call(struct vcc *tl, struct token *t, struct symbol *sym) |
1519 |
|
{ |
1520 |
|
|
1521 |
|
struct expr *e; |
1522 |
|
|
1523 |
10902 |
e = NULL; |
1524 |
10902 |
vcc_func(tl, &e, sym->eval_priv, sym->extra, sym); |
1525 |
10902 |
if (!tl->err) { |
1526 |
10787 |
vcc_expr_fmt(tl->fb, tl->indent, e); |
1527 |
10787 |
SkipToken(tl, ';'); |
1528 |
10787 |
VSB_cat(tl->fb, ";\n"); |
1529 |
10902 |
} else if (t != tl->t) { |
1530 |
115 |
VSB_cat(tl->sb, "While compiling function call:\n\n"); |
1531 |
115 |
vcc_ErrWhere2(tl, t, tl->t); |
1532 |
115 |
} |
1533 |
10902 |
vcc_delete_expr(e); |
1534 |
10902 |
} |
1535 |
|
|
1536 |
|
void v_matchproto_(sym_act_f) |
1537 |
7682 |
vcc_Act_Obj(struct vcc *tl, struct token *t, struct symbol *sym) |
1538 |
|
{ |
1539 |
|
|
1540 |
7682 |
struct expr *e = NULL; |
1541 |
|
|
1542 |
7682 |
assert(sym->kind == SYM_INSTANCE); |
1543 |
7682 |
ExpectErr(tl, '.'); |
1544 |
7659 |
tl->t = t; |
1545 |
7659 |
vcc_expr4(tl, &e, sym->type); |
1546 |
7659 |
ERRCHK(tl); |
1547 |
7659 |
vcc_expr_fmt(tl->fb, tl->indent, e); |
1548 |
7659 |
vcc_delete_expr(e); |
1549 |
7659 |
SkipToken(tl, ';'); |
1550 |
7659 |
VSB_cat(tl->fb, ";\n"); |
1551 |
7682 |
} |
1552 |
|
|
1553 |
|
/*-------------------------------------------------------------------- |
1554 |
|
*/ |
1555 |
|
|
1556 |
|
static void v_matchproto_(sym_expr_t) |
1557 |
851 |
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 |
851 |
int all = sym->eval_priv == NULL ? 0 : 1; |
1562 |
|
char buf[128]; |
1563 |
|
|
1564 |
851 |
(void)t; |
1565 |
851 |
(void)fmt; |
1566 |
851 |
SkipToken(tl, '('); |
1567 |
851 |
vcc_expr0(tl, &e2, STRING); |
1568 |
851 |
ERRCHK(tl); |
1569 |
851 |
SkipToken(tl, ','); |
1570 |
851 |
vcc_expr0(tl, &e3, REGEX); |
1571 |
851 |
ERRCHK(tl); |
1572 |
|
|
1573 |
828 |
bprintf(buf, "VRT_regsub(ctx, %d,\v+\n\v1,\n\v2", all); |
1574 |
828 |
*e = vcc_expr_edit(tl, STRING, buf, e2, e3); |
1575 |
828 |
SkipToken(tl, ','); |
1576 |
828 |
vcc_expr0(tl, &e2, STRING); |
1577 |
828 |
ERRCHK(tl); |
1578 |
828 |
*e = vcc_expr_edit(tl, STRINGS, "\v1,\n\v2)\v-", *e, e2); |
1579 |
828 |
(*e)->nstr = 1; |
1580 |
828 |
SkipToken(tl, ')'); |
1581 |
851 |
} |
1582 |
|
|
1583 |
|
/*-------------------------------------------------------------------- |
1584 |
|
*/ |
1585 |
|
|
1586 |
|
static void v_matchproto_(sym_expr_t) |
1587 |
70288 |
vcc_Eval_BoolConst(struct vcc *tl, struct expr **e, struct token *t, |
1588 |
|
struct symbol *sym, vcc_type_t fmt) |
1589 |
|
{ |
1590 |
|
|
1591 |
70288 |
(void)t; |
1592 |
70288 |
(void)tl; |
1593 |
70288 |
(void)fmt; |
1594 |
70288 |
*e = vcc_mk_expr(BOOL, "(0==%d)", sym->eval_priv == NULL ? 1 : 0); |
1595 |
70288 |
(*e)->constant = EXPR_CONST; |
1596 |
70288 |
} |
1597 |
|
|
1598 |
|
/*-------------------------------------------------------------------- |
1599 |
|
*/ |
1600 |
|
|
1601 |
|
static void v_matchproto_(sym_expr_t) |
1602 |
92 |
vcc_Eval_Default(struct vcc *tl, struct expr **e, struct token *t, |
1603 |
|
struct symbol *sym, vcc_type_t fmt) |
1604 |
|
{ |
1605 |
92 |
(void)e; |
1606 |
92 |
(void)fmt; |
1607 |
92 |
(void)sym; |
1608 |
92 |
(void)t; |
1609 |
|
|
1610 |
92 |
if (fmt->default_sym == NULL) { |
1611 |
46 |
VSB_cat(tl->sb, "Symbol 'default' is a reserved word.\n"); |
1612 |
46 |
vcc_ErrWhere(tl, t); |
1613 |
46 |
return; |
1614 |
|
} |
1615 |
|
|
1616 |
46 |
*e = vcc_mk_expr(fmt, "%s", fmt->default_sym->rname); |
1617 |
92 |
} |
1618 |
|
|
1619 |
|
/*-------------------------------------------------------------------- |
1620 |
|
*/ |
1621 |
|
|
1622 |
|
void |
1623 |
70035 |
vcc_Expr_Init(struct vcc *tl) |
1624 |
|
{ |
1625 |
|
struct symbol *sym; |
1626 |
|
|
1627 |
70035 |
sym = VCC_MkSym(tl, "regsub", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH); |
1628 |
70035 |
AN(sym); |
1629 |
70035 |
sym->type = STRING; |
1630 |
70035 |
sym->eval = vcc_Eval_Regsub; |
1631 |
70035 |
sym->eval_priv = NULL; |
1632 |
|
|
1633 |
70035 |
sym = VCC_MkSym(tl, "regsuball", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH); |
1634 |
70035 |
AN(sym); |
1635 |
70035 |
sym->type = STRING; |
1636 |
70035 |
sym->eval = vcc_Eval_Regsub; |
1637 |
70035 |
sym->eval_priv = sym; |
1638 |
|
|
1639 |
70035 |
sym = VCC_MkSym(tl, "true", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH); |
1640 |
70035 |
AN(sym); |
1641 |
70035 |
sym->type = BOOL; |
1642 |
70035 |
sym->eval = vcc_Eval_BoolConst; |
1643 |
70035 |
sym->eval_priv = sym; |
1644 |
|
|
1645 |
70035 |
sym = VCC_MkSym(tl, "false", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH); |
1646 |
70035 |
AN(sym); |
1647 |
70035 |
sym->type = BOOL; |
1648 |
70035 |
sym->eval = vcc_Eval_BoolConst; |
1649 |
70035 |
sym->eval_priv = NULL; |
1650 |
|
|
1651 |
70035 |
sym = VCC_MkSym(tl, "default", SYM_MAIN, SYM_FUNC, VCL_LOW, VCL_HIGH); |
1652 |
70035 |
AN(sym); |
1653 |
70035 |
sym->type = DEFAULT; |
1654 |
70035 |
sym->eval = vcc_Eval_Default; |
1655 |
70035 |
} |