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 32496
VCC_HandleKind(vcc_type_t fmt)
47
{
48 32496
        if (fmt == ACL)         return(SYM_ACL);
49 31596
        if (fmt == BACKEND)     return(SYM_BACKEND);
50 4344
        if (fmt == PROBE)       return(SYM_PROBE);
51 4164
        if (fmt == STEVEDORE)   return(SYM_STEVEDORE);
52 4164
        if (fmt == SUB)         return(SYM_SUB);
53 3792
        if (fmt == INSTANCE)    return(SYM_INSTANCE);
54 0
        return(SYM_NONE);
55
}
56
57
void
58 151872
VCC_PrintCName(struct vsb *vsb, const char *b, const char *e)
59
{
60
61 151872
        AN(vsb);
62 151872
        AN(b);
63
64 151872
        if (e == NULL)
65 151872
                e = strchr(b, '\0');
66 151872
        assert(b < e);
67
68 1542732
        for (; b < e; b++)
69 1390860
                if (vct_isalnum(*b))
70 1311600
                        VSB_putc(vsb, *b);
71
                else
72 79260
                        VSB_printf(vsb, "_%02x_", *b);
73 151872
}
74
75
static struct symbol *
76 1994592
vcc_new_symbol(struct vcc *tl, const char *b, const char *e)
77
{
78
        struct symbol *sym;
79
80 1994592
        AN(b);
81 1994592
        if (e == NULL)
82 12768
                e = strchr(b, '\0');
83 1994592
        AN(e);
84 1994592
        assert(e > b);
85 1994592
        sym = TlAlloc(tl, sizeof *sym);
86 1994592
        INIT_OBJ(sym, SYMBOL_MAGIC);
87 1994592
        AN(sym);
88 1994592
        sym->name = TlAlloc(tl, (e - b) + 1L);
89 1994592
        AN(sym->name);
90 1994592
        memcpy(sym->name, b, (e - b));
91 1994592
        sym->name[e - b] = '\0';
92 1994592
        sym->nlen = e - b;
93 1994592
        VTAILQ_INIT(&sym->children);
94 1994592
        sym->kind = SYM_NONE;
95 1994592
        sym->type = VOID;
96 1994592
        sym->lorev = VCL_LOW;
97 1994592
        sym->hirev = VCL_HIGH;
98 1994592
        return (sym);
99
}
100
101
static struct symbol *
102 5333688
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 5333688
        struct symbol *sym, *sym2 = NULL;
108
        size_t l;
109
        int i;
110
111 5333688
        assert(vlo <= vhi);
112 5333688
        if (tl->symbols == NULL)
113 12768
                tl->symbols = vcc_new_symbol(tl, "<root>", NULL);
114 5333688
        if (parent == NULL)
115 3071748
                parent = tl->symbols;
116
117 5333688
        AN(b);
118 5333688
        assert(e == NULL || b < e);
119 5333688
        if (e == NULL)
120 1719264
                e = strchr(b, '\0');
121 5333688
        assert(e > b);
122 5333688
        if (e[-1] == '.')
123 76536
                e--;
124 5333688
        assert(e > b);
125
126 5333688
        q = strchr(b, '.');
127 5333688
        if (q == NULL || q > e)
128 3135240
                q = e;
129 5333688
        l = q - b;
130 5333688
        assert(l > 0);
131
132 57569604
        VTAILQ_FOREACH(sym, &parent->children, list) {
133 56904588
                i = strncasecmp(sym->name, b, l);
134 56904588
                if (i < 0)
135 52174404
                        continue;
136 4730184
                if (i > 0 || l < sym->nlen) {
137 1337340
                        sym2 = sym;
138 1337340
                        sym = NULL;
139 1337340
                        break;
140
                }
141 3392844
                if (l > sym->nlen)
142 0
                        continue;
143 3392844
                if (sym->lorev > vhi || sym->hirev < vlo)
144 61440
                        continue;
145 3331404
                if (q < e)
146 2019756
                        break;
147 1311648
                if ((kind == SYM_NONE && kind == sym->kind))
148 12
                        continue;
149 1311636
                if (tl->syntax < VCL_41 &&
150 12336
                     (kind != SYM_NONE && kind != sym->kind))
151 60
                        continue;
152 1311576
                break;
153
        }
154 5333688
        if (sym == NULL && create == 0 && parent->wildcard != NULL) {
155 140148
                AN(parent->wildcard);
156 140148
                sym2 = vcc_new_symbol(tl, b, e);
157 140148
                sym2->parent = parent;
158 140148
                parent->wildcard(tl, parent, sym2);
159 140148
                if (tl->err)
160 24
                        return (NULL);
161 306540
                VTAILQ_FOREACH(sym, &parent->children, list) {
162 233760
                        i = strncasecmp(sym->name, b, l);
163 233760
                        if (i > 0 || (i == 0 && l < sym->nlen))
164
                                break;
165
                }
166 140124
                sym2->lorev = vlo;
167 140124
                sym2->hirev = vhi;
168 140124
                if (sym == NULL)
169 72780
                        VTAILQ_INSERT_TAIL(&parent->children, sym2, list);
170
                else
171 67344
                        VTAILQ_INSERT_BEFORE(sym, sym2, list);
172 140124
                return (VCC_Symbol(tl, parent, b, e, kind, -1, vlo, vhi));
173
        }
174 5193540
        if (sym == NULL && create < 1)
175 20532
                return (sym);
176 5173008
        if (sym == NULL) {
177 1841676
                sym = vcc_new_symbol(tl, b, q);
178 1841676
                sym->parent = parent;
179 1841676
                sym->lorev = vlo;
180 1841676
                sym->hirev = vhi;
181 1841676
                if (sym2 != NULL)
182 1250160
                        VTAILQ_INSERT_BEFORE(sym2, sym, list);
183
                else
184 591516
                        VTAILQ_INSERT_TAIL(&parent->children, sym, list);
185 1841676
                if (q == e)
186 1739616
                        sym->kind = kind;
187
        }
188 5173008
        if (q == e)
189 3051192
                return (sym);
190 2121816
        assert(*q == '.');
191 2121816
        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 1352232
VCC_SymbolGet(struct vcc *tl, vcc_kind_t kind, const char *e, const char *x)
202
{
203
        struct symbol *sym;
204
205 1352232
        AN(e);
206 1371984
        if (tl->syntax >= VCL_41 && e == SYMTAB_CREATE && kind != SYM_SUB &&
207 39504
            (tl->t->b[0] == 'v'|| tl->t->b[0] == 'V') &&
208 1320
            (tl->t->b[1] == 'c'|| tl->t->b[1] == 'C') &&
209 12
            (tl->t->b[2] == 'l'|| tl->t->b[2] == 'L')) {
210 12
                VSB_printf(tl->sb,
211
                    "Symbols named 'vcl_*' are reserved.\nAt:");
212 12
                vcc_ErrWhere(tl, tl->t);
213 12
                return (NULL);
214
        }
215
216 2704440
        sym = VCC_Symbol(tl, NULL, tl->t->b, tl->t->e, kind,
217 2704440
            e == SYMTAB_CREATE ? 1 : 0, tl->syntax, tl->syntax);
218 1352220
        if (sym == NULL && e == SYMTAB_NOERR)
219 20136
                return (sym);
220 1332084
        if (sym == NULL) {
221 264
                VSB_printf(tl->sb, "%s: ", e);
222 264
                vcc_ErrToken(tl, tl->t);
223 264
                sym = VCC_Symbol(tl, NULL, tl->t->b, tl->t->e, kind, 0,
224
                        VCL_LOW, VCL_HIGH);
225 264
                if (sym != NULL) {
226 108
                        VSB_printf(tl->sb, " (Only available when");
227 108
                        if (sym->lorev >= VCL_LOW)
228 84
                                VSB_printf(tl->sb, " %.1f <=", .1 * sym->lorev);
229 108
                        VSB_printf(tl->sb, " VCL syntax");
230 108
                        if (sym->hirev <= VCL_HIGH)
231 24
                                VSB_printf(tl->sb, " <= %.1f", .1 * sym->hirev);
232 108
                        VSB_printf(tl->sb, ")");
233
                }
234 264
                VSB_cat(tl->sb, "\nAt: ");
235 264
                vcc_ErrWhere(tl, tl->t);
236 264
                return (NULL);
237
        }
238 1331820
        assert (sym->lorev <= tl->syntax && sym->hirev >= tl->syntax);
239 1331820
        if (kind != SYM_NONE && kind != sym->kind) {
240 60
                VSB_printf(tl->sb, "Symbol ");
241 60
                vcc_ErrToken(tl, tl->t);
242 60
                if (sym->kind == SYM_NONE)
243 12
                        VSB_printf(tl->sb, " is a reserved word.");
244
                else
245 48
                        VSB_printf(tl->sb, " has wrong type (%s): ",
246 48
                                sym->kind->name);
247 60
                VSB_cat(tl->sb, "\nAt: ");
248 60
                vcc_ErrWhere(tl, tl->t);
249 60
                if (sym->def_b != NULL) {
250 24
                        VSB_printf(tl->sb, "Symbol was defined here: ");
251 24
                        vcc_ErrWhere(tl, sym->def_b);
252 36
                } else if (sym->ref_b != NULL) {
253 12
                        VSB_printf(tl->sb, "Symbol was declared here: ");
254 12
                        vcc_ErrWhere(tl, sym->ref_b);
255
                } else {
256 24
                        VSB_printf(tl->sb, "Symbol was builtin\n");
257
                }
258 60
                return (NULL);
259
        }
260 1331760
        if (x == XREF_DEF) {
261 163884
                if (sym->def_b == NULL)
262 151788
                        sym->def_b = tl->t;
263 163884
                sym->ndef++;
264 1167876
        } else if (x == XREF_REF) {
265 511896
                if (sym->ref_b == NULL)
266 272532
                        sym->ref_b = tl->t;
267 511896
                sym->nref++;
268
        } else {
269 655980
                assert (x == XREF_NONE);
270
        }
271 1331760
        vcc_NextToken(tl);
272 1331760
        return (sym);
273
}
274
275
struct symbol *
276 1719264
VCC_MkSym(struct vcc *tl, const char *b, vcc_kind_t kind,
277
    unsigned vlo, unsigned vhi)
278
{
279
        struct symbol *sym;
280
281 1719264
        sym = VCC_Symbol(tl, NULL, b, NULL, kind, 1, vlo, vhi);
282 1719264
        sym->noref = 1;
283 1719264
        return (sym);
284
}
285
286
287
static void
288 8426952
vcc_walksymbols(struct vcc *tl, const struct symbol *root,
289
    symwalk_f *func, vcc_kind_t kind)
290
{
291 8426952
        struct symbol *sym, *sym2 = NULL;
292
293 16800720
        VTAILQ_FOREACH(sym, &root->children, list) {
294 8373960
                if (sym2 != NULL)
295 7335588
                        assert(strcasecmp(sym->name, sym2->name) >= 0);
296 8373960
                sym2 = sym;
297 8373960
                if (kind == SYM_NONE || kind == sym->kind)
298 3793212
                        func(tl, sym);
299 8373960
                ERRCHK(tl);
300 8373768
                vcc_walksymbols(tl, sym, func, kind);
301
        }
302
}
303
304
void
305 53184
VCC_WalkSymbols(struct vcc *tl, symwalk_f *func, vcc_kind_t kind)
306
{
307
308 53184
        vcc_walksymbols(tl, tl->symbols, func, kind);
309 53184
}
310
311
void
312 16524
VCC_GlobalSymbol(struct symbol *sym, vcc_type_t type, const char *pfx)
313
{
314
        struct vsb *vsb;
315
316 16524
        CHECK_OBJ_NOTNULL(sym, SYMBOL_MAGIC);
317 16524
        AN(pfx);
318
319 16524
        vsb = VSB_new_auto();
320 16524
        AN(vsb);
321 16524
        VSB_printf(vsb, "%s_", pfx);
322 16524
        VCC_PrintCName(vsb, sym->name, NULL);
323 16524
        AZ(VSB_finish(vsb));
324 16524
        sym->rname = strdup(VSB_data(vsb));
325 16524
        AN(sym->rname);
326 16524
        VSB_destroy(&vsb);
327
328 16524
        sym->type = type;
329 16524
        sym->kind = VCC_HandleKind(sym->type);
330 16524
        if (sym->kind != SYM_NONE) {
331 16524
                AZ(VCT_invalid_name(sym->rname, NULL));
332 16524
                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 16524
}
340
341
struct symbol *
342 15972
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 15972
        kind = VCC_HandleKind(fmt);
350 15972
        assert(kind != SYM_NONE);
351
352 15972
        t = tl->t;
353 15972
        sym = VCC_SymbolGet(tl, SYM_NONE, SYMTAB_NOERR, XREF_NONE);
354 15972
        if (sym != NULL && sym->def_b != NULL && kind == sym->kind) {
355 36
                p = sym->kind->name;
356 108
                VSB_printf(tl->sb, "%c%s '%.*s' redefined.\n",
357 72
                    toupper(*p), p + 1, PF(t));
358 36
                vcc_ErrWhere(tl, t);
359 36
                VSB_printf(tl->sb, "First definition:\n");
360 36
                AN(sym->def_b);
361 36
                vcc_ErrWhere(tl, sym->def_b);
362 36
                return (sym);
363 15936
        } else if (sym != NULL && sym->def_b != NULL) {
364 24
                VSB_printf(tl->sb, "Name '%.*s' already defined.\n", PF(t));
365 24
                vcc_ErrWhere(tl, t);
366 24
                VSB_printf(tl->sb, "First definition:\n");
367 24
                AN(sym->def_b);
368 24
                vcc_ErrWhere(tl, sym->def_b);
369 24
                return (sym);
370 15912
        } else if (sym != NULL && sym->kind != kind) {
371 24
                VSB_printf(tl->sb,
372
                    "Name %.*s must have type '%s'.\n",
373 24
                    PF(t), sym->kind->name);
374 12
                vcc_ErrWhere(tl, t);
375 12
                return (sym);
376
        }
377 15900
        if (sym == NULL)
378 15888
                sym = VCC_SymbolGet(tl, kind, SYMTAB_CREATE, XREF_NONE);
379 15900
        if (sym == NULL)
380 0
                return (NULL);
381 15900
        AN(sym);
382 15900
        AZ(sym->ndef);
383 15900
        VCC_GlobalSymbol(sym, fmt, pfx);
384 15900
        sym->ndef = 1;
385 15900
        if (sym->def_b == NULL)
386 15900
                sym->def_b = t;
387 15900
        return (sym);
388
}