Skip to content

Commit

Permalink
Unify the "I need an expression of _exactly_ this type" paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
bsdphk committed Dec 12, 2017
1 parent 3d23688 commit 66e06b1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 42 deletions.
2 changes: 1 addition & 1 deletion bin/varnishtest/tests/m00000.vtc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ varnish v1 -errvcl {Wrong enum value. Expected one of:} {
}
}

varnish v1 -errvcl {Wrong argument type. Expected REAL. Got STRING.} {
varnish v1 -errvcl {Expression has type STRING, expected REAL} {
import std;
sub vcl_deliver {
set resp.http.who = std.random("foo", "bar");
Expand Down
2 changes: 1 addition & 1 deletion bin/varnishtest/tests/m00012.vtc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ varnish v1 -errvcl {BLOBs can only be used as arguments to VMOD functions.} {
}
}

varnish v1 -errvcl {Wrong argument type. Expected BLOB. Got STRING.} {
varnish v1 -errvcl {Expression has type STRING, expected BLOB} {

backend b1 {.host = "${bad_backend}";}

Expand Down
2 changes: 1 addition & 1 deletion bin/varnishtest/tests/r01212.vtc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
varnishtest "#1212 - Vmod with HEADER argument given a STRING asserts the VCL compiler"

varnish v1 -errvcl {Wrong argument type. Expected HEADER. Got STRING.} {
varnish v1 -errvcl {Expression has type STRING, expected HEADER} {
import std;
backend b { .host = "127.0.0.1"; }
sub vcl_recv {
Expand Down
71 changes: 33 additions & 38 deletions lib/libvcc/vcc_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,7 @@ vcc_Eval_Regsub(struct vcc *tl, struct expr **e, const struct symbol *sym,
SkipToken(tl, '(');

vcc_expr0(tl, &e2, STRING);
if (e2 == NULL)
return;
ERRCHK(tl);

SkipToken(tl, ',');
ExpectErr(tl, CSTR);
Expand All @@ -289,8 +288,7 @@ vcc_Eval_Regsub(struct vcc *tl, struct expr **e, const struct symbol *sym,

SkipToken(tl, ',');
vcc_expr0(tl, &e2, STRING);
if (e2 == NULL)
return;
ERRCHK(tl);
*e = vcc_expr_edit(STRING, "\v1,\n\v2)\v-", *e, e2);
SkipToken(tl, ')');
}
Expand Down Expand Up @@ -430,20 +428,7 @@ vcc_do_arg(struct vcc *tl, struct func_arg *fa)
} else {
vcc_expr0(tl, &e2, fa->type);
ERRCHK(tl);
if (e2->fmt != fa->type) {
VSB_printf(tl->sb, "Wrong argument type.");
VSB_printf(tl->sb,
" Expected %s.", vcc_utype(fa->type));
VSB_printf(tl->sb, " Got %s.\n", vcc_utype(e2->fmt));
vcc_ErrWhere2(tl, e2->t1, tl->t);
return;
}
assert(e2->fmt == fa->type);
if (e2->fmt == STRING_LIST) {
e2 = vcc_expr_edit(STRING_LIST,
"\v+\n\v1,\nvrt_magic_string_end\v-",
e2, NULL);
}
fa->result = e2;
}
}
Expand Down Expand Up @@ -1176,12 +1161,12 @@ vcc_expr_cand(struct vcc *tl, struct expr **e, vcc_type_t fmt)

/*--------------------------------------------------------------------
* SYNTAX:
* Expr0:
* Expr1:
* ExprCand { '||' ExprCand } *
*/

static void
vcc_expr0(struct vcc *tl, struct expr **e, vcc_type_t fmt)
vcc_expr1(struct vcc *tl, struct expr **e, vcc_type_t fmt)
{
struct expr *e2;
struct token *tk;
Expand Down Expand Up @@ -1213,6 +1198,32 @@ vcc_expr0(struct vcc *tl, struct expr **e, vcc_type_t fmt)
}
}

/*--------------------------------------------------------------------
* This function is the entry-point for getting an expression with
* a particular type, ready for inclusion in the VGC.
*/

static void
vcc_expr0(struct vcc *tl, struct expr **e, vcc_type_t fmt)
{
struct token *t1;

assert(fmt != VOID);
t1 = tl->t;
vcc_expr1(tl, e, fmt);
ERRCHK(tl);
if (fmt != (*e)->fmt) {
VSB_printf(tl->sb, "Expression has type %s, expected %s\n",
vcc_utype((*e)->fmt), vcc_utype(fmt));
tl->err = 1;
}
if ((*e)->fmt == STRING_LIST)
*e = vcc_expr_edit(STRING_LIST,
"\v+\n\v1,\nvrt_magic_string_end\v-", *e, NULL);
if (tl->err)
vcc_ErrWhere2(tl, t1, tl->t);
}

/*--------------------------------------------------------------------
* This function parses and emits the C-code to evaluate an expression
*
Expand All @@ -1223,29 +1234,13 @@ vcc_expr0(struct vcc *tl, struct expr **e, vcc_type_t fmt)
void
vcc_Expr(struct vcc *tl, vcc_type_t fmt)
{
struct expr *e;
struct token *t1;
struct expr *e = NULL;

assert(fmt != VOID);

t1 = tl->t;
vcc_expr0(tl, &e, fmt);
ERRCHK(tl);
e->t1 = t1;
if (!tl->err && fmt != e->fmt) {
VSB_printf(tl->sb, "Expression has type %s, expected %s\n",
vcc_utype(e->fmt), vcc_utype(fmt));
tl->err = 1;
}
if (!tl->err) {
if (e->fmt == STRING_LIST) {
e = vcc_expr_edit(STRING_LIST,
"\v+\n\v1,\nvrt_magic_string_end\v-", e, NULL);
}
vcc_expr_fmt(tl->fb, tl->indent, e);
VSB_putc(tl->fb, '\n');
} else if (t1 != tl->t)
vcc_ErrWhere2(tl, t1, tl->t);
vcc_expr_fmt(tl->fb, tl->indent, e);
VSB_printf(tl->fb, "\n");
vcc_delete_expr(e);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/libvcc/vcc_vmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ vcc_ParseImport(struct vcc *tl)
VSB_printf(ifp->fin,
"\t\t(void)%s(ctx, &vmod_priv_%.*s,\n"
"\t\t\t VCL_EVENT_DISCARD);\n", p, PF(mod));
VSB_printf(ifp->event, "\t%s(ctx, &vmod_priv_%.*s, ev)",
VSB_printf(ifp->event, "%s(ctx, &vmod_priv_%.*s, ev)",
p, PF(mod));
} else if (!strcmp(p, "$FUNC")) {
p += strlen(p) + 1;
Expand Down

0 comments on commit 66e06b1

Please sign in to comment.