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 1887168
VCC_HandleKind(vcc_type_t fmt)
79
{
80 1887168
        if (fmt == ACL)         return (SYM_ACL);
81 1886172
        if (fmt == BACKEND)     return (SYM_BACKEND);
82 1849620
        if (fmt == PROBE)       return (SYM_PROBE);
83 1849344
        if (fmt == STEVEDORE)   return (SYM_STEVEDORE);
84 1849344
        if (fmt == SUB)         return (SYM_SUB);
85 4920
        if (fmt == INSTANCE)    return (SYM_INSTANCE);
86 0
        AZ(fmt->global_pfx);
87 0
        return (SYM_NONE);
88 1887168
}
89
90
void
91 1357368
VCC_PrintCName(struct vsb *vsb, const char *b, const char *e)
92
{
93
94 1357368
        AN(vsb);
95 1357368
        AN(b);
96
97 1357368
        if (e == NULL)
98 1357368
                e = strchr(b, '\0');
99 1357368
        assert(b < e);
100
101 21529344
        for (; b < e; b++)
102 40343952
                if (vct_isalnum(*b))
103 18209100
                        VSB_putc(vsb, *b);
104
                else
105 1962876
                        VSB_printf(vsb, "_%02x_", *b);
106 1357368
}
107
108
static void
109 5869536
vcc_symtabname(struct vsb *vsb, const struct symtab *st)
110
{
111 5869536
        if (st->parent != NULL && st->parent->parent != NULL) {
112 3262200
                vcc_symtabname(vsb, st->parent);
113 2563308
                VSB_putc(vsb, '.');
114 2563308
        }
115 5869536
        VSB_cat(vsb, st->name);
116 5869536
}
117
118
void
119 4005120
VCC_SymName(struct vsb *vsb, const struct symbol *sym)
120
{
121 3306228
        AN(vsb);
122 3306228
        CHECK_OBJ_NOTNULL(sym, SYMBOL_MAGIC);
123 3306228
        CHECK_OBJ_NOTNULL(sym->symtab, SYMTAB_MAGIC);
124 3306228
        vcc_symtabname(vsb, sym->symtab);
125 3306228
}
126 698892
127
static struct symtab *
128 7370472
vcc_symtab_new(const char *name)
129
{
130
        struct symtab *st;
131
132 7370472
        ALLOC_OBJ(st, SYMTAB_MAGIC);
133 7370472
        AN(st);
134 7370472
        st->name = name;
135 8069364
        st->nlen = strlen(st->name);
136 7370472
        VTAILQ_INIT(&st->children);
137 7370472
        VTAILQ_INIT(&st->symbols);
138 7370472
        return (st);
139
}
140
141
static struct symtab *
142 16582152
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 698892
        int i;
147
        const char *q;
148
149 15883260
        assert(tok == ID || tok == CSTR);
150 15883260
        if (e == NULL)
151 6757380
                e = strchr(b, '\0');
152 15883260
        q = e;
153
154 36810516
        while (b < e) {
155 20927256
                if (tok == ID) {
156 172300392
                        for (q = b; q < e && *q != '.'; q++)
157 151373256
                                continue;
158 20927136
                }
159 21626148
                l = pdiff(b, q);
160 327956064
                VTAILQ_FOREACH(st2, &st->children, list) {
161 323098716
                        i = strncasecmp(st2->name, b, l);
162 323098716
                        if (i < 0)
163 307028808
                                continue;
164 16768800
                        if (i == 0 && l == st2->nlen)
165 13626804
                                break;
166 2443104
                        st3 = vcc_symtab_new(vcc_Dup_be(b, q));
167 2443104
                        st3->parent = st;
168 2443104
                        VTAILQ_INSERT_BEFORE(st2, st3, list);
169 2443104
                        st2 = st3;
170 2443104
                        break;
171
                }
172 21626148
                if (st2 == NULL) {
173 4857348
                        st2 = vcc_symtab_new(vcc_Dup_be(b, q));
174 4857348
                        st2->parent = st;
175 4857348
                        VTAILQ_INSERT_TAIL(&st->children, st2, list);
176 4857348
                }
177 20927256
                st = st2;
178 20927256
                b = q + 1;
179 698892
        }
180 15883260
        return (st);
181
}
182
183
static struct symbol *
184 7140624
vcc_new_symbol(struct vcc *tl, struct symtab *st,
185 698892
    vcc_kind_t kind, int vlo, int vhi)
186
{
187
        struct symbol *sym;
188
189 7140624
        sym = TlAlloc(tl, sizeof *sym);
190 7140624
        INIT_OBJ(sym, SYMBOL_MAGIC);
191 7839516
        AN(sym);
192 7140624
        sym->name = st->name;
193 7140624
        sym->symtab = st;
194 7140624
        sym->kind = kind;
195 7140624
        sym->type = VOID;
196 7140624
        sym->lorev = vlo;
197 7140624
        sym->hirev = vhi;
198 7140624
        VTAILQ_INSERT_TAIL(&st->symbols, sym, list);
199 7140624
        return (sym);
200
}
201
202 698892
static struct symbol *
203 15883572
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 16038204
        VTAILQ_FOREACH(sym, &st->symbols, list) {
210 8116716
                if (sym->lorev > vhi || sym->hirev < vlo)
211 154536
                        continue;
212 7962180
                if (kind == SYM_NONE && kind == sym->kind &&
213 398844
                    sym->wildcard == NULL)
214 0
                        continue;
215 8053080
                if (tl->syntax < VCL_41 && strcmp(sym->name, "default") &&
216 4533504
                     kind != SYM_NONE && kind != sym->kind &&
217 90900
                     sym->wildcard == NULL)
218 96
                        continue;
219 7962084
                return (sym);
220
        }
221 7921488
        pst = st->parent;
222 7921488
        if (pst == NULL)
223 0
                return (sym);
224 7921488
        psym = VTAILQ_FIRST(&pst->symbols);
225 7921488
        if (psym == NULL)
226 4066800
                return (sym);
227 3854688
        if (psym->wildcard == NULL)
228 3196500
                return (sym);
229
230 658188
        sym = vcc_new_symbol(tl, st, kind, vlo, vhi);
231 658188
        psym->wildcard(tl, psym, sym);
232 658188
        if (tl->err)
233 24
                return (NULL);
234 658164
        return (sym);
235 15883572
}
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 7440072
VCC_SymbolGet(struct vcc *tl, vcc_ns_t ns, vcc_kind_t kind,
268
    const struct symmode *e, const struct symxref *x)
269
{
270 7440072
        struct symtab *st, *st2 = NULL;
271 7440072
        struct symbol *sym = NULL, *sym2 = NULL;
272 7440072
        struct token *t0, *tn, *tn1, *tn2 = NULL;
273
274 7440072
        AN(tl);
275 7440072
        CHECK_OBJ_NOTNULL(ns, VCC_NAMESPACE_MAGIC);
276 7440072
        AN(ns->name);
277 7440072
        CHECK_OBJ_NOTNULL(kind, KIND_MAGIC);
278 7440072
        AN(e);
279 7440072
        AN(x);
280 7440072
        AN(x->name);
281 7440072
        if (tl->syntax >= VCL_41 && e == SYMTAB_CREATE && kind != SYM_SUB &&
282 27516
            vcc_Has_vcl_prefix(tl->t->b)) {
283 24
                VSB_cat(tl->sb, "Symbols named 'vcl_*' are reserved.\nAt:");
284 24
                vcc_ErrWhere(tl, tl->t);
285 24
                return (NULL);
286
        }
287
288 7440048
        st = tl->syms[ns->id];
289 7440048
        t0 = tl->t;
290 7440048
        tn = tl->t;
291 7440048
        assert(tn->tok == ID);
292 9824904
        while (1) {
293 9824904
                assert(tn->tok == ID || tn->tok == CSTR);
294 9824904
                if (tn->tok == CSTR && tl->syntax < VCL_41) {
295 12
                        VSB_cat(tl->sb,
296
                            "Quoted headers are available for VCL >= 4.1.\n"
297
                            "At:");
298 12
                        vcc_ErrWhere(tl, tn);
299 12
                        return (NULL);
300
                }
301 9824892
                if (tn->tok == ID)
302 9824772
                        st = vcc_symtab_str(st, tn->b, tn->e, tn->tok);
303
                else
304 120
                        st = vcc_symtab_str(st, tn->dec, NULL, tn->tok);
305 9824892
                sym2 = vcc_sym_in_tab(tl, st, kind, tl->syntax, tl->syntax);
306 9824892
                if (sym2 != NULL) {
307 8338500
                        sym = sym2;
308 8338500
                        st2 = st;
309 8338500
                        tn2 = tn;
310 8338500
                }
311 9824892
                tn1 = vcc_PeekTokenFrom(tl, tn);
312 9824892
                if (tn1->tok != '.')
313 7440012
                        break;
314 2384880
                tn1 = vcc_PeekTokenFrom(tl, tn1);
315 2384880
                if (tn1->tok == CSTR && (sym == NULL || sym->wildcard == NULL))
316 12
                        break;
317 2384868
                if (tn1->tok != CSTR && tn1->tok != ID)
318 12
                        break;
319 2384856
                tn = tn1;
320
        }
321 7440036
        if (sym != NULL && sym->kind == SYM_ALIAS) {
322 24
                assert(ns == SYM_MAIN);
323 24
                st = vcc_symtab_str(tl->syms[ns->id], sym->eval_priv, NULL, ID);
324 24
                AN(st);
325 24
                st2 = st;
326 24
                sym = vcc_sym_in_tab(tl, st, SYM_NONE, sym->lorev, sym->hirev);
327 24
                AN(sym);
328 24
        }
329 7440036
        if (sym != NULL && sym->kind == SYM_VMOD && e->partial)
330 12
                e = SYMTAB_EXISTING;
331 7440036
        if (sym != NULL && e->partial) {
332 3955884
                st = st2;
333 3955884
                tn = tn2;
334 7440036
        } else if (st != st2) {
335 1405188
                sym = NULL;
336 1405188
        }
337 7440036
        if (tl->err || (sym == NULL && e->noerr))
338 699180
                return (sym);
339 6740856
        AN(st);
340 6740856
        AN(tn);
341 6740856
        if (sym == NULL && e == SYMTAB_CREATE)
342 705696
                sym = vcc_new_symbol(tl, st, kind, tl->syntax, tl->syntax);
343 6740856
        tl->t = vcc_PeekTokenFrom(tl, tn);
344 6740856
        if (tl->err)
345 0
                return (NULL);
346 6740856
        if (sym == NULL) {
347 312
                VSB_printf(tl->sb, "%s: '", e->name);
348 312
                vcc_PrintTokens(tl, t0, tl->t);
349 312
                VSB_cat(tl->sb, "'");
350 312
                sym = vcc_sym_in_tab(tl, st, kind, VCL_LOW, VCL_HIGH);
351 312
                if (sym != NULL && sym->kind != SYM_OBJECT &&
352 120
                    sym->kind != SYM_INSTANCE) { /* XXX: too specific */
353 120
                        VSB_cat(tl->sb, " (Only available when");
354 120
                        if (sym->lorev >= VCL_LOW)
355 96
                                VSB_printf(tl->sb, " %.1f <=", .1 * sym->lorev);
356 120
                        VSB_cat(tl->sb, " VCL syntax");
357 120
                        if (sym->hirev <= VCL_HIGH)
358 24
                                VSB_printf(tl->sb, " <= %.1f", .1 * sym->hirev);
359 120
                        VSB_cat(tl->sb, ")");
360 120
                }
361 312
                VSB_cat(tl->sb, "\nAt: ");
362 312
                vcc_ErrWhere2(tl, t0, tl->t);
363 312
                return (NULL);
364
        }
365 6740544
        if (kind != SYM_NONE && kind != sym->kind) {
366 36
                VSB_cat(tl->sb, "Symbol '");
367 36
                vcc_PrintTokens(tl, t0, tl->t);
368 72
                VSB_printf(tl->sb, "' has wrong type (%s), expected %s:",
369 36
                    sym->kind->name, kind->name);
370 36
                VSB_cat(tl->sb, "\nAt: ");
371 36
                vcc_ErrWhere2(tl, t0, tl->t);
372 36
                if (sym->def_b != NULL) {
373 24
                        VSB_cat(tl->sb, "Symbol was defined here: ");
374 24
                        vcc_ErrWhere(tl, sym->def_b);
375 36
                } else if (sym->ref_b != NULL) {
376 12
                        VSB_cat(tl->sb, "Symbol was declared here: ");
377 12
                        vcc_ErrWhere(tl, sym->ref_b);
378 12
                } else {
379 0
                        VSB_cat(tl->sb, "Symbol was builtin\n");
380
                }
381 36
                return (NULL);
382
        }
383 6740508
        if (x == XREF_DEF) {
384 1147008
                if (sym->def_b == NULL)
385 1130088
                        sym->def_b = t0;
386 1147008
                sym->ndef++;
387 6740508
        } else if (x == XREF_REF) {
388 2218824
                if (sym->ref_b == NULL)
389 1407240
                        sym->ref_b = t0;
390 2218824
                sym->nref++;
391 2218824
        } else {
392 3374676
                assert (x == XREF_NONE);
393
        }
394 6740508
        return (sym);
395 7440072
}
396
397
static struct symbol *
398 41772
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 41772
        buf = VSB_new_auto();
405 41772
        AN(buf);
406 41772
        VSB_printf(buf, "%s.%.*s", type->name, PF(tl->t));
407 41772
        AZ(VSB_finish(buf));
408
409
        /* NB: we create a fake token but errors are handled by the caller. */
410 41772
        memcpy(t, tl->t, sizeof *t);
411 41772
        t->b = VSB_data(buf);
412 41772
        t->e = t->b + VSB_len(buf);
413
414 41772
        t0 = tl->t;
415 41772
        tl->t = t;
416 41772
        sym = VCC_SymbolGet(tl, ns, kind, SYMTAB_NOERR, XREF_NONE);
417 41772
        tl->t = t0;
418 41772
        VSB_destroy(&buf);
419
420 41772
        return (sym);
421
}
422
423
struct symbol *
424 41772
VCC_TypeSymbol(struct vcc *tl, vcc_kind_t kind, vcc_type_t type)
425
{
426
427 41772
        if (strchr(type->name, '.') == NULL)
428 32508
                return (vcc_TypeSymbol(tl, SYM_TYPE, kind, type));
429
430
        /* NB: type imported from a VMOD */
431 9264
        return (vcc_TypeSymbol(tl, SYM_MAIN, kind, type));
432 41772
}
433
434
struct symbol *
435 6056772
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 6056772
        AN(tl);
443 6056772
        AN(b);
444 6056772
        CHECK_OBJ_NOTNULL(ns, VCC_NAMESPACE_MAGIC);
445 6056772
        CHECK_OBJ_NOTNULL(kind, KIND_MAGIC);
446 6056772
        assert(vlo <= vhi);
447
448 6056772
        if (tl->syms[ns->id] == NULL)
449 70020
                tl->syms[ns->id] = vcc_symtab_new("");
450 6056772
        st = vcc_symtab_str(tl->syms[ns->id], b, NULL, ID);
451 6056772
        AN(st);
452 6056772
        sym = vcc_sym_in_tab(tl, st, kind, vlo, vhi);
453 6056772
        if (sym != NULL) {
454 280032
                assert(sym->kind == SYM_VAR);
455 280032
                parent = sym->eval_priv;
456 280032
                AN(parent);
457 280032
                AN(parent->wildcard);
458 280032
                assert(sym->type == parent->type);
459 280032
                return (sym);
460
        }
461 5776740
        AZ(sym);
462 5776740
        sym = vcc_new_symbol(tl, st, kind, vlo, vhi);
463 5776740
        AN(sym);
464 5776740
        sym->noref = 1;
465 5776740
        return (sym);
466 6056772
}
467
468
struct symbol *
469 1572
VCC_MkSymAlias(struct vcc *tl, const char *alias, const char *name)
470
{
471
        struct symbol *sym_alias, *sym;
472
        struct symtab *st;
473
474 1572
        AN(tl);
475 1572
        AN(alias);
476 1572
        AN(name);
477
478 1572
        st = vcc_symtab_str(tl->syms[SYM_MAIN->id], name, NULL, ID);
479 1572
        AN(st);
480 1572
        sym = vcc_sym_in_tab(tl, st, SYM_NONE, VCL_LOW, VCL_HIGH);
481 1572
        AN(sym);
482 1572
        assert(sym->kind != SYM_ALIAS);
483 3144
        sym_alias = VCC_MkSym(tl, alias, SYM_MAIN, SYM_ALIAS, sym->lorev,
484 1572
            sym->hirev);
485 1572
        AN(sym_alias);
486 1572
        sym_alias->eval_priv = strdup(name);
487 1572
        AN(sym_alias->eval_priv);
488 1572
        return (sym);
489
}
490
491
static void
492 35288052
vcc_walksymbols(struct vcc *tl, const struct symtab *root,
493
    symwalk_f *func, vcc_kind_t kind)
494
{
495
        struct symbol *sym;
496 35288052
        struct symtab *st1, *st2 = NULL;
497
498 69570696
        VTAILQ_FOREACH(sym, &root->symbols, list) {
499 34283292
                if (kind == SYM_NONE || kind == sym->kind)
500 15809976
                        func(tl, sym);
501 34283292
                ERRCHK(tl);
502 34282644
        }
503 70376604
        VTAILQ_FOREACH(st1, &root->children, list) {
504 35089848
                if (st2 != NULL)
505 31660380
                        assert(strcasecmp(st1->name, st2->name) >= 0);
506 35089848
                st2 = st1;
507 35089848
                vcc_walksymbols(tl, st1, func, kind);
508 35089848
                ERRCHK(tl);
509 35089200
        }
510 35288052
}
511
512
void
513 198204
VCC_WalkSymbols(struct vcc *tl, symwalk_f *func, vcc_ns_t ns, vcc_kind_t kind)
514
{
515
516 198204
        CHECK_OBJ_NOTNULL(ns, VCC_NAMESPACE_MAGIC);
517 198204
        vcc_walksymbols(tl, tl->syms[ns->id], func, kind);
518 198204
}
519
520
void
521 1375572
VCC_GlobalSymbol(struct symbol *sym, vcc_type_t type)
522
{
523
        vcc_kind_t kind;
524
        struct vsb *vsb;
525
526 1375572
        CHECK_OBJ_NOTNULL(sym, SYMBOL_MAGIC);
527 1375572
        AN(type);
528 1375572
        AN(type->global_pfx);
529
530 1375572
        kind = VCC_HandleKind(type);
531
532 1375572
        if (sym->lname != NULL) {
533 676680
                AN(sym->rname);
534 676680
                assert(sym->type == type);
535 676680
                assert(sym->kind == kind);
536 676680
                return;
537
        }
538
539 698892
        vsb = VSB_new_auto();
540 698892
        AN(vsb);
541 698892
        VSB_printf(vsb, "%s_", type->global_pfx);
542 698892
        VCC_PrintCName(vsb, sym->name, NULL);
543 698892
        AZ(VSB_finish(vsb));
544 698892
        sym->lname = strdup(VSB_data(vsb));
545 698892
        AN(sym->lname);
546 698892
        if (type == SUB) {
547 677544
                VSB_clear(vsb);
548 677544
                VSB_printf(vsb, "sub_%s", sym->lname);
549 677544
                AZ(VSB_finish(vsb));
550 677544
        }
551 698892
        sym->rname = strdup(VSB_data(vsb));
552 698892
        AN(sym->rname);
553 698892
        VSB_destroy(&vsb);
554
555 698892
        sym->type = type;
556 698892
        sym->kind = kind;
557 698892
        if (sym->kind != SYM_NONE) {
558 698892
                AZ(VCT_invalid_name(sym->rname, NULL));
559 698892
                if (type == SUB)
560 677544
                        sym->eval = vcc_Eval_Sub;
561
                else
562 21348
                        sym->eval = vcc_Eval_Handle;
563 698892
        } 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 1375572
}
570
571
struct symbol *
572 21372
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 21372
        kind = VCC_HandleKind(fmt);
580 21372
        assert(kind != SYM_NONE);
581
582 21372
        t = tl->t;
583 21372
        sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_NONE, SYMTAB_NOERR, XREF_NONE);
584 21372
        if (sym != NULL && sym->def_b != NULL && kind == sym->kind) {
585 36
                p = sym->kind->name;
586 72
                VSB_printf(tl->sb, "%c%s '%.*s' redefined.\n",
587 36
                    toupper(*p), p + 1, PF(t));
588 36
                vcc_ErrWhere(tl, t);
589 36
                VSB_cat(tl->sb, "First definition:\n");
590 36
                AN(sym->def_b);
591 36
                vcc_ErrWhere(tl, sym->def_b);
592 36
                return (sym);
593 21336
        } else if (sym != NULL && sym->def_b != NULL) {
594 24
                VSB_printf(tl->sb, "Name '%.*s' already defined.\n", PF(t));
595 24
                vcc_ErrWhere(tl, t);
596 24
                VSB_cat(tl->sb, "First definition:\n");
597 24
                AN(sym->def_b);
598 24
                vcc_ErrWhere(tl, sym->def_b);
599 24
                return (sym);
600 21312
        } else if (sym != NULL && sym->kind != kind) {
601 24
                VSB_printf(tl->sb,
602
                    "Name %.*s must have type '%s'.\n",
603 12
                    PF(t), sym->kind->name);
604 12
                vcc_ErrWhere(tl, t);
605 12
                return (sym);
606
        }
607 21300
        if (sym == NULL)
608 21276
                sym = VCC_SymbolGet(tl, SYM_MAIN, kind, SYMTAB_CREATE,
609
                    XREF_NONE);
610 21300
        if (sym == NULL)
611 0
                return (NULL);
612 21300
        AN(sym);
613 21300
        AZ(sym->ndef);
614 21300
        VCC_GlobalSymbol(sym, fmt);
615 21300
        sym->ndef = 1;
616 21300
        if (sym->def_b == NULL)
617 21300
                sym->def_b = t;
618 21300
        return (sym);
619 21372
}
620
/*lint -restore */