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