varnish-cache/lib/libvcc/vcc_symb.c
1
/*-
2
 * Copyright (c) 2010 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 */
28
29
#include "config.h"
30
31
#include <ctype.h>
32
#include <stdarg.h>
33
#include <stdlib.h>
34
#include <string.h>
35
36
#include "vcc_compile.h"
37
38
#include "vct.h"
39
40
/*--------------------------------------------------------------------*/
41
#define VCC_KIND(U,l) const struct kind SYM_##U[1] = {{ KIND_MAGIC, #l}};
42
#include "tbl/symbol_kind.h"
43
/*--------------------------------------------------------------------*/
44
45
static vcc_kind_t
46 5476
VCC_HandleKind(vcc_type_t fmt)
47
{
48 5476
        if (fmt == ACL)         return(SYM_ACL);
49 5326
        if (fmt == BACKEND)     return(SYM_BACKEND);
50 740
        if (fmt == PROBE)       return(SYM_PROBE);
51 706
        if (fmt == STEVEDORE)   return(SYM_STEVEDORE);
52 706
        if (fmt == SUB)         return(SYM_SUB);
53 640
        if (fmt == INSTANCE)    return(SYM_INSTANCE);
54 0
        return(SYM_NONE);
55
}
56
57
void
58 25660
VCC_PrintCName(struct vsb *vsb, const char *b, const char *e)
59
{
60
61 25660
        AN(vsb);
62 25660
        AN(b);
63
64 25660
        if (e == NULL)
65 25660
                e = strchr(b, '\0');
66 25660
        assert(b < e);
67
68 260460
        for (; b < e; b++)
69 234800
                if (vct_isalnum(*b))
70 221384
                        VSB_putc(vsb, *b);
71
                else
72 13416
                        VSB_printf(vsb, "_%02x_", *b);
73 25660
}
74
75
static struct symbol *
76 335398
vcc_new_symbol(struct vcc *tl, const char *b, const char *e)
77
{
78
        struct symbol *sym;
79
80 335398
        AN(b);
81 335398
        if (e == NULL)
82 2146
                e = strchr(b, '\0');
83 335398
        AN(e);
84 335398
        assert(e > b);
85 335398
        sym = TlAlloc(tl, sizeof *sym);
86 335398
        INIT_OBJ(sym, SYMBOL_MAGIC);
87 335398
        AN(sym);
88 335398
        sym->name = TlAlloc(tl, (e - b) + 1L);
89 335398
        AN(sym->name);
90 335398
        memcpy(sym->name, b, (e - b));
91 335398
        sym->name[e - b] = '\0';
92 335398
        sym->nlen = e - b;
93 335398
        VTAILQ_INIT(&sym->children);
94 335398
        sym->kind = SYM_NONE;
95 335398
        sym->type = VOID;
96 335398
        sym->lorev = VCL_LOW;
97 335398
        sym->hirev = VCL_HIGH;
98 335398
        return (sym);
99
}
100
101
static struct symbol *
102 898312
VCC_Symbol(struct vcc *tl, struct symbol *parent,
103
    const char *b, const char *e, vcc_kind_t kind,
104
    int create, unsigned vlo, unsigned vhi)
105
{
106
        const char *q;
107 898312
        struct symbol *sym, *sym2 = NULL;
108
        size_t l;
109
        int i;
110
111 898312
        assert(vlo <= vhi);
112 898312
        if (tl->symbols == NULL)
113 2146
                tl->symbols = vcc_new_symbol(tl, "<root>", NULL);
114 898312
        if (parent == NULL)
115 517200
                parent = tl->symbols;
116
117 898312
        AN(b);
118 898312
        assert(e == NULL || b < e);
119 898312
        if (e == NULL)
120 288960
                e = strchr(b, '\0');
121 898312
        assert(e > b);
122 898312
        if (e[-1] == '.')
123 12864
                e--;
124 898312
        assert(e > b);
125
126 898312
        q = strchr(b, '.');
127 898312
        if (q == NULL || q > e)
128 528014
                q = e;
129 898312
        l = q - b;
130 898312
        assert(l > 0);
131
132 9703460
        VTAILQ_FOREACH(sym, &parent->children, list) {
133 9591622
                i = strncasecmp(sym->name, b, l);
134 9591622
                if (i < 0)
135 8794804
                        continue;
136 796818
                if (i > 0 || l < sym->nlen) {
137 224882
                        sym2 = sym;
138 224882
                        sym = NULL;
139 224882
                        break;
140
                }
141 571936
                if (l > sym->nlen)
142 0
                        continue;
143 571936
                if (sym->lorev > vhi || sym->hirev < vlo)
144 10332
                        continue;
145 561604
                if (q < e)
146 340264
                        break;
147 221340
                if ((kind == SYM_NONE && kind == sym->kind))
148 2
                        continue;
149 221338
                if (tl->syntax < VCL_41 &&
150 2056
                     (kind != SYM_NONE && kind != sym->kind))
151 10
                        continue;
152 221328
                break;
153
        }
154 898312
        if (sym == NULL && create == 0 && parent->wildcard != NULL) {
155 23698
                AN(parent->wildcard);
156 23698
                sym2 = vcc_new_symbol(tl, b, e);
157 23698
                sym2->parent = parent;
158 23698
                parent->wildcard(tl, parent, sym2);
159 23698
                if (tl->err)
160 4
                        return (NULL);
161 52150
                VTAILQ_FOREACH(sym, &parent->children, list) {
162 39858
                        i = strncasecmp(sym->name, b, l);
163 39858
                        if (i > 0 || (i == 0 && l < sym->nlen))
164
                                break;
165
                }
166 23694
                sym2->lorev = vlo;
167 23694
                sym2->hirev = vhi;
168 23694
                if (sym == NULL)
169 12292
                        VTAILQ_INSERT_TAIL(&parent->children, sym2, list);
170
                else
171 11402
                        VTAILQ_INSERT_BEFORE(sym, sym2, list);
172 23694
                return (VCC_Symbol(tl, parent, b, e, kind, -1, vlo, vhi));
173
        }
174 874614
        if (sym == NULL && create < 1)
175 3468
                return (sym);
176 871146
        if (sym == NULL) {
177 309554
                sym = vcc_new_symbol(tl, b, q);
178 309554
                sym->parent = parent;
179 309554
                sym->lorev = vlo;
180 309554
                sym->hirev = vhi;
181 309554
                if (sym2 != NULL)
182 210132
                        VTAILQ_INSERT_BEFORE(sym2, sym, list);
183
                else
184 99422
                        VTAILQ_INSERT_TAIL(&parent->children, sym, list);
185 309554
                if (q == e)
186 292400
                        sym->kind = kind;
187
        }
188 871146
        if (q == e)
189 513728
                return (sym);
190 357418
        assert(*q == '.');
191 357418
        return (VCC_Symbol(tl, sym, ++q, e, kind, create, vlo, vhi));
192
}
193
194
const char XREF_NONE[] = "xref_none";
195
const char XREF_DEF[] = "xref_def";
196
const char XREF_REF[] = "xref_ref";
197
const char SYMTAB_NOERR[] = "sym_noerror";
198
const char SYMTAB_CREATE[] = "sym_create";
199
200
struct symbol *
201 228198
VCC_SymbolGet(struct vcc *tl, vcc_kind_t kind, const char *e, const char *x)
202
{
203
        struct symbol *sym;
204
205 228198
        AN(e);
206 231536
        if (tl->syntax >= VCL_41 && e == SYMTAB_CREATE && kind != SYM_SUB &&
207 6676
            (tl->t->b[0] == 'v'|| tl->t->b[0] == 'V') &&
208 228
            (tl->t->b[1] == 'c'|| tl->t->b[1] == 'C') &&
209 2
            (tl->t->b[2] == 'l'|| tl->t->b[2] == 'L')) {
210 2
                VSB_printf(tl->sb,
211
                    "Symbols named 'vcl_*' are reserved.\nAt:");
212 2
                vcc_ErrWhere(tl, tl->t);
213 2
                return (NULL);
214
        }
215
216 456392
        sym = VCC_Symbol(tl, NULL, tl->t->b, tl->t->e, kind,
217 456392
            e == SYMTAB_CREATE ? 1 : 0, tl->syntax, tl->syntax);
218 228196
        if (sym == NULL && e == SYMTAB_NOERR)
219 3402
                return (sym);
220 224794
        if (sym == NULL) {
221 44
                VSB_printf(tl->sb, "%s: ", e);
222 44
                vcc_ErrToken(tl, tl->t);
223 44
                sym = VCC_Symbol(tl, NULL, tl->t->b, tl->t->e, kind, 0,
224
                        VCL_LOW, VCL_HIGH);
225 44
                if (sym != NULL) {
226 18
                        VSB_printf(tl->sb, " (Only available when");
227 18
                        if (sym->lorev >= VCL_LOW)
228 14
                                VSB_printf(tl->sb, " %.1f <=", .1 * sym->lorev);
229 18
                        VSB_printf(tl->sb, " VCL syntax");
230 18
                        if (sym->hirev <= VCL_HIGH)
231 4
                                VSB_printf(tl->sb, " <= %.1f", .1 * sym->hirev);
232 18
                        VSB_printf(tl->sb, ")");
233
                }
234 44
                VSB_cat(tl->sb, "\nAt: ");
235 44
                vcc_ErrWhere(tl, tl->t);
236 44
                return (NULL);
237
        }
238 224750
        assert (sym->lorev <= tl->syntax && sym->hirev >= tl->syntax);
239 224750
        if (kind != SYM_NONE && kind != sym->kind) {
240 10
                VSB_printf(tl->sb, "Symbol ");
241 10
                vcc_ErrToken(tl, tl->t);
242 10
                if (sym->kind == SYM_NONE)
243 2
                        VSB_printf(tl->sb, " is a reserved word.");
244
                else
245 8
                        VSB_printf(tl->sb, " has wrong type (%s): ",
246 8
                                sym->kind->name);
247 10
                VSB_cat(tl->sb, "\nAt: ");
248 10
                vcc_ErrWhere(tl, tl->t);
249 10
                if (sym->def_b != NULL) {
250 4
                        VSB_printf(tl->sb, "Symbol was defined here: ");
251 4
                        vcc_ErrWhere(tl, sym->def_b);
252 6
                } else if (sym->ref_b != NULL) {
253 2
                        VSB_printf(tl->sb, "Symbol was declared here: ");
254 2
                        vcc_ErrWhere(tl, sym->ref_b);
255
                } else {
256 4
                        VSB_printf(tl->sb, "Symbol was builtin\n");
257
                }
258 10
                return (NULL);
259
        }
260 224740
        if (x == XREF_DEF) {
261 27626
                if (sym->def_b == NULL)
262 25580
                        sym->def_b = tl->t;
263 27626
                sym->ndef++;
264 197114
        } else if (x == XREF_REF) {
265 86450
                if (sym->ref_b == NULL)
266 45976
                        sym->ref_b = tl->t;
267 86450
                sym->nref++;
268
        } else {
269 110664
                assert (x == XREF_NONE);
270
        }
271 224740
        vcc_NextToken(tl);
272 224740
        return (sym);
273
}
274
275
struct symbol *
276 288960
VCC_MkSym(struct vcc *tl, const char *b, vcc_kind_t kind,
277
    unsigned vlo, unsigned vhi)
278
{
279
        struct symbol *sym;
280
281 288960
        sym = VCC_Symbol(tl, NULL, b, NULL, kind, 1, vlo, vhi);
282 288960
        sym->noref = 1;
283 288960
        return (sym);
284
}
285
286
287
static void
288 1420752
vcc_walksymbols(struct vcc *tl, const struct symbol *root,
289
    symwalk_f *func, vcc_kind_t kind)
290
{
291 1420752
        struct symbol *sym, *sym2 = NULL;
292
293 2832540
        VTAILQ_FOREACH(sym, &root->children, list) {
294 1411820
                if (sym2 != NULL)
295 1236758
                        assert(strcasecmp(sym->name, sym2->name) >= 0);
296 1411820
                sym2 = sym;
297 1411820
                if (kind == SYM_NONE || kind == sym->kind)
298 639512
                        func(tl, sym);
299 1411820
                ERRCHK(tl);
300 1411788
                vcc_walksymbols(tl, sym, func, kind);
301
        }
302
}
303
304
void
305 8964
VCC_WalkSymbols(struct vcc *tl, symwalk_f *func, vcc_kind_t kind)
306
{
307
308 8964
        vcc_walksymbols(tl, tl->symbols, func, kind);
309 8964
}
310
311
void
312 2786
VCC_GlobalSymbol(struct symbol *sym, vcc_type_t type, const char *pfx)
313
{
314
        struct vsb *vsb;
315
316 2786
        CHECK_OBJ_NOTNULL(sym, SYMBOL_MAGIC);
317 2786
        AN(pfx);
318
319 2786
        vsb = VSB_new_auto();
320 2786
        AN(vsb);
321 2786
        VSB_printf(vsb, "%s_", pfx);
322 2786
        VCC_PrintCName(vsb, sym->name, NULL);
323 2786
        AZ(VSB_finish(vsb));
324 2786
        sym->rname = strdup(VSB_data(vsb));
325 2786
        AN(sym->rname);
326 2786
        VSB_destroy(&vsb);
327
328 2786
        sym->type = type;
329 2786
        sym->kind = VCC_HandleKind(sym->type);
330 2786
        if (sym->kind != SYM_NONE) {
331 2786
                AZ(VCT_invalid_name(sym->rname, NULL));
332 2786
                sym->eval = vcc_Eval_Handle;
333
        } else {
334 0
                WRONG("Wrong kind of global symbol");
335
        }
336
337
#define VCL_MET_MAC(l,u,t,b)   sym->r_methods |= VCL_MET_##u;
338
#include "tbl/vcl_returns.h"
339 2786
}
340
341
struct symbol *
342 2690
VCC_HandleSymbol(struct vcc *tl, vcc_type_t fmt, const char *pfx)
343
{
344
        struct symbol *sym;
345
        vcc_kind_t kind;
346
        struct token *t;
347
        const char *p;
348
349 2690
        kind = VCC_HandleKind(fmt);
350 2690
        assert(kind != SYM_NONE);
351
352 2690
        t = tl->t;
353 2690
        sym = VCC_SymbolGet(tl, SYM_NONE, SYMTAB_NOERR, XREF_NONE);
354 2690
        if (sym != NULL && sym->def_b != NULL && kind == sym->kind) {
355 6
                p = sym->kind->name;
356 18
                VSB_printf(tl->sb, "%c%s '%.*s' redefined.\n",
357 12
                    toupper(*p), p + 1, PF(t));
358 6
                vcc_ErrWhere(tl, t);
359 6
                VSB_printf(tl->sb, "First definition:\n");
360 6
                AN(sym->def_b);
361 6
                vcc_ErrWhere(tl, sym->def_b);
362 6
                return (sym);
363 2684
        } else if (sym != NULL && sym->def_b != NULL) {
364 4
                VSB_printf(tl->sb, "Name '%.*s' already defined.\n", PF(t));
365 4
                vcc_ErrWhere(tl, t);
366 4
                VSB_printf(tl->sb, "First definition:\n");
367 4
                AN(sym->def_b);
368 4
                vcc_ErrWhere(tl, sym->def_b);
369 4
                return (sym);
370 2680
        } else if (sym != NULL && sym->kind != kind) {
371 4
                VSB_printf(tl->sb,
372
                    "Name %.*s must have type '%s'.\n",
373 4
                    PF(t), sym->kind->name);
374 2
                vcc_ErrWhere(tl, t);
375 2
                return (sym);
376
        }
377 2678
        if (sym == NULL)
378 2676
                sym = VCC_SymbolGet(tl, kind, SYMTAB_CREATE, XREF_NONE);
379 2678
        if (sym == NULL)
380 0
                return (NULL);
381 2678
        AN(sym);
382 2678
        AZ(sym->ndef);
383 2678
        VCC_GlobalSymbol(sym, fmt, pfx);
384 2678
        sym->ndef = 1;
385 2678
        if (sym->def_b == NULL)
386 2678
                sym->def_b = t;
387 2678
        return (sym);
388
}