varnish-cache/lib/libvcc/vcc_symb.c
0
/*-
1
 * Copyright (c) 2010 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29
/*lint -save -esym(759, SYMTAB_NOERR) -esym(765, SYMTAB_NOERR)*/
30
31
#include "config.h"
32
33
#include <ctype.h>
34
#include <stdlib.h>
35
#include <string.h>
36
37
#include "vcc_compile.h"
38
39
#include "vct.h"
40
41
/*--------------------------------------------------------------------*/
42
43
#define VCC_KIND(U,l) const struct kind SYM_##U[1] = {{ KIND_MAGIC, #l}};
44
#include "tbl/symbol_kind.h"
45
46
/*--------------------------------------------------------------------*/
47
48
struct vcc_namespace {
49
        unsigned                magic;
50
#define VCC_NAMESPACE_MAGIC     0x27b842f4
51
        const char              *name;
52
        enum vcc_namespace_e    id;
53
};
54
55
#define VCC_NAMESPACE(U, l)                             \
56
        static const struct vcc_namespace sym_##l = {   \
57
                VCC_NAMESPACE_MAGIC,                    \
58
                #l,                                     \
59
                VCC_NAMESPACE_##U                       \
60
        };                                              \
61
        vcc_ns_t SYM_##U = &sym_##l;
62
#include "vcc_namespace.h"
63
64
/*--------------------------------------------------------------------*/
65
66
struct symtab {
67
        unsigned                        magic;
68
#define SYMTAB_MAGIC                    0x084d9c8a
69
        unsigned                        nlen;
70
        const char                      *name;
71
        const struct symtab             *parent;
72
        VTAILQ_ENTRY(symtab)            list;
73
        VTAILQ_HEAD(,symtab)            children;
74
        VTAILQ_HEAD(,symbol)            symbols;
75
};
76
77
vcc_kind_t
78 6689160
VCC_HandleKind(vcc_type_t fmt)
79
{
80 6689160
        if (fmt == ACL)         return (SYM_ACL);
81 6685840
        if (fmt == BACKEND)     return (SYM_BACKEND);
82 6561120
        if (fmt == PROBE)       return (SYM_PROBE);
83 6560200
        if (fmt == STEVEDORE)   return (SYM_STEVEDORE);
84 6560200
        if (fmt == SUB)         return (SYM_SUB);
85 16400
        if (fmt == INSTANCE)    return (SYM_INSTANCE);
86 0
        AZ(fmt->global_pfx);
87 0
        return (SYM_NONE);
88 6689160
}
89
90
void
91 4873600
VCC_PrintCName(struct vsb *vsb, const char *b, const char *e)
92
{
93
94 4873600
        AN(vsb);
95 4873600
        AN(b);
96
97 4873600
        if (e == NULL)
98 4873600
                e = strchr(b, '\0');
99 4873600
        assert(b < e);
100
101 77242320
        for (; b < e; b++)
102 144737440
                if (vct_isalnum(*b))
103 65304680
                        VSB_putc(vsb, *b);
104
                else
105 7064040
                        VSB_printf(vsb, "_%02x_", *b);
106 4873600
}
107
108
static void
109 20724320
vcc_symtabname(struct vsb *vsb, const struct symtab *st)
110
{
111 20724320
        if (st->parent != NULL && st->parent->parent != NULL) {
112 9064520
                vcc_symtabname(vsb, st->parent);
113 9064520
                VSB_putc(vsb, '.');
114 9064520
        }
115 20724320
        VSB_cat(vsb, st->name);
116 20724320
}
117
118
void
119 11659800
VCC_SymName(struct vsb *vsb, const struct symbol *sym)
120
{
121 11659800
        AN(vsb);
122 11659800
        CHECK_OBJ_NOTNULL(sym, SYMBOL_MAGIC);
123 11659800
        CHECK_OBJ_NOTNULL(sym->symtab, SYMTAB_MAGIC);
124 11659800
        vcc_symtabname(vsb, sym->symtab);
125 11659800
}
126
127
static struct symtab *
128 25738360
vcc_symtab_new(const char *name)
129
{
130
        struct symtab *st;
131
132 25738360
        ALLOC_OBJ(st, SYMTAB_MAGIC);
133 25738360
        AN(st);
134 25738360
        st->name = name;
135 25738360
        st->nlen = strlen(st->name);
136 25738360
        VTAILQ_INIT(&st->children);
137 25738360
        VTAILQ_INIT(&st->symbols);
138 25738360
        return (st);
139
}
140
141
static struct symtab *
142 56201720
vcc_symtab_str(struct symtab *st, const char *b, const char *e, unsigned tok)
143
{
144
        struct symtab *st2, *st3;
145
        size_t l;
146
        int i;
147
        const char *q;
148
149 56201720
        assert(tok == ID || tok == CSTR);
150 56201720
        if (e == NULL)
151 21020280
                e = strchr(b, '\0');
152 56201720
        q = e;
153
154 129953360
        while (b < e) {
155 73751640
                if (tok == ID) {
156 608435440
                        for (q = b; q < e && *q != '.'; q++)
157 534684200
                                continue;
158 73751240
                }
159 73751640
                l = pdiff(b, q);
160 1166179640
                VTAILQ_FOREACH(st2, &st->children, list) {
161 1149311960
                        i = strncasecmp(st2->name, b, l);
162 1149311960
                        if (i < 0)
163 1092428000
                                continue;
164 56883960
                        if (i == 0 && l == st2->nlen)
165 48252920
                                break;
166 8631040
                        st3 = vcc_symtab_new(vcc_Dup_be(b, q));
167 8631040
                        st3->parent = st;
168 8631040
                        VTAILQ_INSERT_BEFORE(st2, st3, list);
169 8631040
                        st2 = st3;
170 8631040
                        break;
171
                }
172 73751640
                if (st2 == NULL) {
173 16867680
                        st2 = vcc_symtab_new(vcc_Dup_be(b, q));
174 16867680
                        st2->parent = st;
175 16867680
                        VTAILQ_INSERT_TAIL(&st->children, st2, list);
176 16867680
                }
177 73751640
                st = st2;
178 73751640
                b = q + 1;
179
        }
180 56201720
        return (st);
181
}
182
183
static struct symbol *
184 24951920
vcc_new_symbol(struct vcc *tl, struct symtab *st,
185
    vcc_kind_t kind, int vlo, int vhi)
186
{
187
        struct symbol *sym;
188
189 24951920
        sym = TlAlloc(tl, sizeof *sym);
190 24951920
        INIT_OBJ(sym, SYMBOL_MAGIC);
191 24951920
        AN(sym);
192 24951920
        sym->name = st->name;
193 24951920
        sym->symtab = st;
194 24951920
        sym->kind = kind;
195 24951920
        sym->type = VOID;
196 24951920
        sym->lorev = vlo;
197 24951920
        sym->hirev = vhi;
198 24951920
        VTAILQ_INSERT_TAIL(&st->symbols, sym, list);
199 24951920
        return (sym);
200
}
201
202
static struct symbol *
203 56202760
vcc_sym_in_tab(struct vcc *tl, struct symtab *st,
204
    vcc_kind_t kind, int vlo, int vhi)
205
{
206
        const struct symtab *pst;
207
        struct symbol *sym, *psym;
208
209 56732240
        VTAILQ_FOREACH(sym, &st->symbols, list) {
210 28992880
                if (sym->lorev > vhi || sym->hirev < vlo)
211 529160
                        continue;
212 28463720
                if (kind == SYM_NONE && kind == sym->kind &&
213 1367720
                    sym->wildcard == NULL)
214 0
                        continue;
215 28836000
                if (tl->syntax < VCL_41 && strcmp(sym->name, "default") &&
216 16179760
                     kind != SYM_NONE && kind != sym->kind &&
217 372280
                     sym->wildcard == NULL)
218 320
                        continue;
219 28463400
                return (sym);
220
        }
221 27739360
        pst = st->parent;
222 27739360
        if (pst == NULL)
223 0
                return (sym);
224 27739360
        psym = VTAILQ_FIRST(&pst->symbols);
225 27739360
        if (psym == NULL)
226 14149720
                return (sym);
227 13589640
        if (psym->wildcard == NULL)
228 11224120
                return (sym);
229
230 2365520
        sym = vcc_new_symbol(tl, st, kind, vlo, vhi);
231 2365520
        psym->wildcard(tl, psym, sym);
232 2365520
        if (tl->err)
233 80
                return (NULL);
234 2365440
        return (sym);
235 56202760
}
236
237
238
const struct symxref XREF_NONE[1] = {{"xref_none"}};
239
const struct symxref XREF_DEF[1] = {{"xref_def"}};
240
const struct symxref XREF_REF[1] = {{"xref_ref"}};
241
242
const struct symmode SYMTAB_NOERR[1] = {{
243
        .name = "sym_noerror",
244
        .noerr = 1
245
}};
246
247
const struct symmode SYMTAB_CREATE[1] = {{
248
        .name = "sym_create"
249
}};
250
251
const struct symmode SYMTAB_EXISTING[1] = {{
252
        .name = "Symbol not found"
253
}};
254
255
const struct symmode SYMTAB_PARTIAL[1] = {{
256
        .name = "Symbol not found",
257
        .partial = 1
258
}};
259
260
const struct symmode SYMTAB_PARTIAL_NOERR[1] = {{
261
        .name = "Symbol not found",
262
        .partial = 1,
263
        .noerr = 1
264
}};
265
266
struct symbol *
267 26564080
VCC_SymbolGet(struct vcc *tl, vcc_ns_t ns, vcc_kind_t kind,
268
    const struct symmode *e, const struct symxref *x)
269
{
270 26564080
        struct symtab *st, *st2 = NULL;
271 26564080
        struct symbol *sym = NULL, *sym2 = NULL;
272 26564080
        struct token *t0, *tn, *tn1, *tn2 = NULL;
273
274 26564080
        AN(tl);
275 26564080
        CHECK_OBJ_NOTNULL(ns, VCC_NAMESPACE_MAGIC);
276 26564080
        AN(ns->name);
277 26564080
        CHECK_OBJ_NOTNULL(kind, KIND_MAGIC);
278 26564080
        AN(e);
279 26564080
        AN(x);
280 26564080
        AN(x->name);
281 26564080
        if (tl->syntax >= VCL_41 && e == SYMTAB_CREATE && kind != SYM_SUB &&
282 93960
            vcc_Has_vcl_prefix(tl->t->b)) {
283 80
                VSB_cat(tl->sb, "Symbols named 'vcl_*' are reserved.\nAt:");
284 80
                vcc_ErrWhere(tl, tl->t);
285 80
                return (NULL);
286
        }
287
288 26564000
        st = tl->syms[ns->id];
289 26564000
        t0 = tl->t;
290 26564000
        tn = tl->t;
291 26564000
        assert(tn->tok == ID);
292 35181880
        while (1) {
293 35181880
                assert(tn->tok == ID || tn->tok == CSTR);
294 35181880
                if (tn->tok == CSTR && tl->syntax < VCL_41) {
295 40
                        VSB_cat(tl->sb,
296
                            "Quoted headers are available for VCL >= 4.1.\n"
297
                            "At:");
298 40
                        vcc_ErrWhere(tl, tn);
299 40
                        return (NULL);
300
                }
301 35181840
                if (tn->tok == ID)
302 35181440
                        st = vcc_symtab_str(st, tn->b, tn->e, tn->tok);
303
                else
304 400
                        st = vcc_symtab_str(st, tn->dec, NULL, tn->tok);
305 35181840
                sym2 = vcc_sym_in_tab(tl, st, kind, tl->syntax, tl->syntax);
306 35181840
                if (sym2 != NULL) {
307 29864360
                        sym = sym2;
308 29864360
                        st2 = st;
309 29864360
                        tn2 = tn;
310 29864360
                }
311 35181840
                tn1 = vcc_PeekTokenFrom(tl, tn);
312 35181840
                if (tn1->tok != '.')
313 26563880
                        break;
314 8617960
                tn1 = vcc_PeekTokenFrom(tl, tn1);
315 8617960
                if (tn1->tok == CSTR && (sym == NULL || sym->wildcard == NULL))
316 40
                        break;
317 8617920
                if (tn1->tok != CSTR && tn1->tok != ID)
318 40
                        break;
319 8617880
                tn = tn1;
320
        }
321 26563960
        if (sym != NULL && sym->kind == SYM_ALIAS) {
322 80
                assert(ns == SYM_MAIN);
323 80
                st = vcc_symtab_str(tl->syms[ns->id], sym->eval_priv, NULL, ID);
324 80
                AN(st);
325 80
                st2 = st;
326 80
                sym = vcc_sym_in_tab(tl, st, SYM_NONE, sym->lorev, sym->hirev);
327 80
                AN(sym);
328 80
        }
329 26563960
        if (sym != NULL && sym->kind == SYM_VMOD && e->partial)
330 40
                e = SYMTAB_EXISTING;
331 26563960
        if (sym != NULL && e->partial) {
332 14118480
                st = st2;
333 14118480
                tn = tn2;
334 26563960
        } else if (st != st2) {
335 5039680
                sym = NULL;
336 5039680
        }
337 26563960
        if (tl->err || (sym == NULL && e->noerr))
338 2508040
                return (sym);
339 24055920
        AN(st);
340 24055920
        AN(tn);
341 24055920
        if (sym == NULL && e == SYMTAB_CREATE)
342 2530600
                sym = vcc_new_symbol(tl, st, kind, tl->syntax, tl->syntax);
343 24055920
        tl->t = vcc_PeekTokenFrom(tl, tn);
344 24055920
        if (tl->err)
345 0
                return (NULL);
346 24055920
        if (sym == NULL) {
347 1040
                VSB_printf(tl->sb, "%s: '", e->name);
348 1040
                vcc_PrintTokens(tl, t0, tl->t);
349 1040
                VSB_cat(tl->sb, "'");
350 1040
                sym = vcc_sym_in_tab(tl, st, kind, VCL_LOW, VCL_HIGH);
351 1040
                if (sym != NULL && sym->kind != SYM_OBJECT &&
352 400
                    sym->kind != SYM_INSTANCE) { /* XXX: too specific */
353 400
                        VSB_cat(tl->sb, " (Only available when");
354 400
                        if (sym->lorev >= VCL_LOW)
355 320
                                VSB_printf(tl->sb, " %.1f <=", .1 * sym->lorev);
356 400
                        VSB_cat(tl->sb, " VCL syntax");
357 400
                        if (sym->hirev <= VCL_HIGH)
358 80
                                VSB_printf(tl->sb, " <= %.1f", .1 * sym->hirev);
359 400
                        VSB_cat(tl->sb, ")");
360 400
                }
361 1040
                VSB_cat(tl->sb, "\nAt: ");
362 1040
                vcc_ErrWhere2(tl, t0, tl->t);
363 1040
                return (NULL);
364
        }
365 24054880
        if (kind != SYM_NONE && kind != sym->kind && sym->type != DEFAULT) {
366 120
                VSB_cat(tl->sb, "Symbol '");
367 120
                vcc_PrintTokens(tl, t0, tl->t);
368 240
                VSB_printf(tl->sb, "' has wrong type (%s), expected %s:",
369 120
                    sym->kind->name, kind->name);
370 120
                VSB_cat(tl->sb, "\nAt: ");
371 120
                vcc_ErrWhere2(tl, t0, tl->t);
372 120
                if (sym->def_b != NULL) {
373 80
                        VSB_cat(tl->sb, "Symbol was defined here: ");
374 80
                        vcc_ErrWhere(tl, sym->def_b);
375 120
                } else if (sym->ref_b != NULL) {
376 40
                        VSB_cat(tl->sb, "Symbol was declared here: ");
377 40
                        vcc_ErrWhere(tl, sym->ref_b);
378 40
                } else {
379 0
                        VSB_cat(tl->sb, "Symbol was builtin\n");
380
                }
381 120
                return (NULL);
382
        }
383 24054760
        if (x == XREF_DEF) {
384 4044840
                if (sym->def_b == NULL)
385 3986680
                        sym->def_b = t0;
386 4044840
                sym->ndef++;
387 24054760
        } else if (x == XREF_REF) {
388 7939680
                if (sym->ref_b == NULL)
389 4937120
                        sym->ref_b = t0;
390 7939680
                sym->nref++;
391 7939680
        } else {
392 12070240
                assert (x == XREF_NONE);
393
        }
394 24054760
        return (sym);
395 26564080
}
396
397
static struct symbol *
398 142360
vcc_TypeSymbol(struct vcc *tl, vcc_ns_t ns, vcc_kind_t kind, vcc_type_t type)
399
{
400
        struct token t[1], *t0;
401
        struct symbol *sym;
402
        struct vsb *buf;
403
404 142360
        buf = VSB_new_auto();
405 142360
        AN(buf);
406 142360
        VSB_printf(buf, "%s.%.*s", type->name, PF(tl->t));
407 142360
        AZ(VSB_finish(buf));
408
409
        /* NB: we create a fake token but errors are handled by the caller. */
410 142360
        memcpy(t, tl->t, sizeof *t);
411 142360
        t->b = VSB_data(buf);
412 142360
        t->e = t->b + VSB_len(buf);
413
414 142360
        t0 = tl->t;
415 142360
        tl->t = t;
416 142360
        sym = VCC_SymbolGet(tl, ns, kind, SYMTAB_NOERR, XREF_NONE);
417 142360
        tl->t = t0;
418 142360
        VSB_destroy(&buf);
419
420 142360
        return (sym);
421
}
422
423
struct symbol *
424 142360
VCC_TypeSymbol(struct vcc *tl, vcc_kind_t kind, vcc_type_t type)
425
{
426
427 142360
        if (strchr(type->name, '.') == NULL)
428 111480
                return (vcc_TypeSymbol(tl, SYM_TYPE, kind, type));
429
430
        /* NB: type imported from a VMOD */
431 30880
        return (vcc_TypeSymbol(tl, SYM_MAIN, kind, type));
432 142360
}
433
434
struct symbol *
435 21014200
VCC_MkSym(struct vcc *tl, const char *b, vcc_ns_t ns, vcc_kind_t kind,
436
    int vlo, int vhi)
437
{
438
        struct symtab *st;
439
        struct symbol *sym;
440
        const struct symbol *parent;
441
442 21014200
        AN(tl);
443 21014200
        AN(b);
444 21014200
        CHECK_OBJ_NOTNULL(ns, VCC_NAMESPACE_MAGIC);
445 21014200
        CHECK_OBJ_NOTNULL(kind, KIND_MAGIC);
446 21014200
        assert(vlo <= vhi);
447
448 21014200
        if (tl->syms[ns->id] == NULL)
449 239640
                tl->syms[ns->id] = vcc_symtab_new("");
450 21014200
        st = vcc_symtab_str(tl->syms[ns->id], b, NULL, ID);
451 21014200
        AN(st);
452 21014200
        sym = vcc_sym_in_tab(tl, st, kind, vlo, vhi);
453 21014200
        if (sym != NULL) {
454 958400
                assert(sym->kind == SYM_VAR);
455 958400
                parent = sym->eval_priv;
456 958400
                AN(parent);
457 958400
                AN(parent->wildcard);
458 958400
                assert(sym->type == parent->type);
459 958400
                return (sym);
460
        }
461 20055800
        AZ(sym);
462 20055800
        sym = vcc_new_symbol(tl, st, kind, vlo, vhi);
463 20055800
        AN(sym);
464 20055800
        sym->noref = 1;
465 20055800
        return (sym);
466 21014200
}
467
468
struct symbol *
469 5600
VCC_MkSymAlias(struct vcc *tl, const char *alias, const char *name)
470
{
471
        struct symbol *sym_alias, *sym;
472
        struct symtab *st;
473
474 5600
        AN(tl);
475 5600
        AN(alias);
476 5600
        AN(name);
477
478 5600
        st = vcc_symtab_str(tl->syms[SYM_MAIN->id], name, NULL, ID);
479 5600
        AN(st);
480 5600
        sym = vcc_sym_in_tab(tl, st, SYM_NONE, VCL_LOW, VCL_HIGH);
481 5600
        AN(sym);
482 5600
        assert(sym->kind != SYM_ALIAS);
483 11200
        sym_alias = VCC_MkSym(tl, alias, SYM_MAIN, SYM_ALIAS, sym->lorev,
484 5600
            sym->hirev);
485 5600
        AN(sym_alias);
486 5600
        sym_alias->eval_priv = strdup(name);
487 5600
        AN(sym_alias->eval_priv);
488 5600
        return (sym);
489
}
490
491
static void
492 123813520
vcc_walksymbols(struct vcc *tl, const struct symtab *root,
493
    symwalk_f *func, vcc_kind_t kind)
494
{
495
        struct symbol *sym;
496 123813520
        struct symtab *st1, *st2 = NULL;
497
498 244175840
        VTAILQ_FOREACH(sym, &root->symbols, list) {
499 120364480
                if (kind == SYM_NONE || kind == sym->kind)
500 55597720
                        func(tl, sym);
501 120364480
                ERRCHK(tl);
502 120362320
        }
503 246941760
        VTAILQ_FOREACH(st1, &root->children, list) {
504 123132560
                if (st2 != NULL)
505 111342680
                        assert(strcasecmp(st1->name, st2->name) >= 0);
506 123132560
                st2 = st1;
507 123132560
                vcc_walksymbols(tl, st1, func, kind);
508 123132560
                ERRCHK(tl);
509 123130400
        }
510 123813520
}
511
512
void
513 680960
VCC_WalkSymbols(struct vcc *tl, symwalk_f *func, vcc_ns_t ns, vcc_kind_t kind)
514
{
515
516 680960
        CHECK_OBJ_NOTNULL(ns, VCC_NAMESPACE_MAGIC);
517 680960
        vcc_walksymbols(tl, tl->syms[ns->id], func, kind);
518 680960
}
519
520
void
521 4938720
VCC_GlobalSymbol(struct symbol *sym, vcc_type_t type)
522
{
523
        vcc_kind_t kind;
524
        struct vsb *vsb;
525
526 4938720
        CHECK_OBJ_NOTNULL(sym, SYMBOL_MAGIC);
527 4938720
        AN(type);
528 4938720
        AN(type->global_pfx);
529
530 4938720
        kind = VCC_HandleKind(type);
531
532 4938720
        if (sym->lname != NULL) {
533 2431600
                AN(sym->rname);
534 2431600
                assert(sym->type == type);
535 2431600
                assert(sym->kind == kind);
536 2431600
                return;
537
        }
538
539 2507120
        vsb = VSB_new_auto();
540 2507120
        AN(vsb);
541 2507120
        VSB_printf(vsb, "%s_", type->global_pfx);
542 2507120
        VCC_PrintCName(vsb, sym->name, NULL);
543 2507120
        AZ(VSB_finish(vsb));
544 2507120
        sym->lname = strdup(VSB_data(vsb));
545 2507120
        AN(sym->lname);
546 2507120
        if (type == SUB) {
547 2434520
                VSB_clear(vsb);
548 2434520
                VSB_printf(vsb, "sub_%s", sym->lname);
549 2434520
                AZ(VSB_finish(vsb));
550 2434520
        }
551 2507120
        sym->rname = strdup(VSB_data(vsb));
552 2507120
        AN(sym->rname);
553 2507120
        VSB_destroy(&vsb);
554
555 2507120
        sym->type = type;
556 2507120
        sym->kind = kind;
557 2507120
        if (sym->kind != SYM_NONE) {
558 2507120
                AZ(VCT_invalid_name(sym->rname, NULL));
559 2507120
                if (type == SUB)
560 2434520
                        sym->eval = vcc_Eval_Sub;
561
                else
562 72600
                        sym->eval = vcc_Eval_Handle;
563 2507120
        } else {
564 0
                WRONG("Wrong kind of global symbol");
565
        }
566
567
#define VCL_MET_MAC(l,u,t,b)   sym->r_methods |= VCL_MET_##u;
568
#include "tbl/vcl_returns.h"
569
}
570
571
struct symbol *
572 75880
VCC_HandleSymbol(struct vcc *tl, vcc_type_t fmt)
573
{
574
        struct symbol *sym;
575
        vcc_kind_t kind;
576
        struct token *t;
577
        const char *p;
578
579 75880
        if (vcc_IdIs(tl->t, "default") && fmt->default_sym != NULL) {
580 3200
                vcc_NextToken(tl);
581 3200
                return (fmt->default_sym);
582
        }
583
584 72680
        kind = VCC_HandleKind(fmt);
585 72680
        assert(kind != SYM_NONE);
586
587 72680
        t = tl->t;
588 72680
        sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_NONE, SYMTAB_NOERR, XREF_NONE);
589 72680
        if (sym != NULL && sym->def_b != NULL && kind == sym->kind) {
590 120
                p = sym->kind->name;
591 240
                VSB_printf(tl->sb, "%c%s '%.*s' redefined.\n",
592 120
                    toupper(*p), p + 1, PF(t));
593 120
                vcc_ErrWhere(tl, t);
594 120
                VSB_cat(tl->sb, "First definition:\n");
595 120
                AN(sym->def_b);
596 120
                vcc_ErrWhere(tl, sym->def_b);
597 120
                return (sym);
598 72560
        } else if (sym != NULL && sym->def_b != NULL) {
599 80
                VSB_printf(tl->sb, "Name '%.*s' already defined.\n", PF(t));
600 80
                vcc_ErrWhere(tl, t);
601 80
                VSB_cat(tl->sb, "First definition:\n");
602 80
                AN(sym->def_b);
603 80
                vcc_ErrWhere(tl, sym->def_b);
604 80
                return (sym);
605 72480
        } else if (sym != NULL && sym->kind != kind) {
606 80
                VSB_printf(tl->sb,
607
                    "Name %.*s must have type '%s'.\n",
608 40
                    PF(t), sym->kind->name);
609 40
                vcc_ErrWhere(tl, t);
610 40
                return (sym);
611
        }
612 72440
        if (sym == NULL)
613 72360
                sym = VCC_SymbolGet(tl, SYM_MAIN, kind, SYMTAB_CREATE,
614
                    XREF_NONE);
615 72440
        if (sym == NULL)
616 0
                return (NULL);
617 72440
        AN(sym);
618 72440
        AZ(sym->ndef);
619 72440
        VCC_GlobalSymbol(sym, fmt);
620 72440
        sym->ndef = 1;
621 72440
        if (sym->def_b == NULL)
622 72440
                sym->def_b = t;
623 72440
        return (sym);
624 75880
}
625
/*lint -restore */