| | 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 |
3934450 |
VCC_HandleKind(vcc_type_t fmt) |
79 |
|
{ |
80 |
3934450 |
if (fmt == ACL) return (SYM_ACL); |
81 |
3932375 |
if (fmt == BACKEND) return (SYM_BACKEND); |
82 |
3856175 |
if (fmt == PROBE) return (SYM_PROBE); |
83 |
3855600 |
if (fmt == STEVEDORE) return (SYM_STEVEDORE); |
84 |
3855600 |
if (fmt == SUB) return (SYM_SUB); |
85 |
10250 |
if (fmt == INSTANCE) return (SYM_INSTANCE); |
86 |
0 |
AZ(fmt->global_pfx); |
87 |
0 |
return (SYM_NONE); |
88 |
3934450 |
} |
89 |
|
|
90 |
|
void |
91 |
2829875 |
VCC_PrintCName(struct vsb *vsb, const char *b, const char *e) |
92 |
|
{ |
93 |
|
|
94 |
2829875 |
AN(vsb); |
95 |
2829875 |
AN(b); |
96 |
|
|
97 |
2829875 |
if (e == NULL) |
98 |
2829875 |
e = strchr(b, '\0'); |
99 |
2829875 |
assert(b < e); |
100 |
|
|
101 |
44885275 |
for (; b < e; b++) |
102 |
84110800 |
if (vct_isalnum(*b)) |
103 |
37963075 |
VSB_putc(vsb, *b); |
104 |
|
else |
105 |
4092325 |
VSB_printf(vsb, "_%02x_", *b); |
106 |
2829875 |
} |
107 |
|
|
108 |
|
static void |
109 |
12237125 |
vcc_symtabname(struct vsb *vsb, const struct symtab *st) |
110 |
|
{ |
111 |
12237125 |
if (st->parent != NULL && st->parent->parent != NULL) { |
112 |
6801250 |
vcc_symtabname(vsb, st->parent); |
113 |
5344150 |
VSB_putc(vsb, '.'); |
114 |
5344150 |
} |
115 |
12237125 |
VSB_cat(vsb, st->name); |
116 |
12237125 |
} |
117 |
|
|
118 |
|
void |
119 |
8350075 |
VCC_SymName(struct vsb *vsb, const struct symbol *sym) |
120 |
|
{ |
121 |
6892975 |
AN(vsb); |
122 |
6892975 |
CHECK_OBJ_NOTNULL(sym, SYMBOL_MAGIC); |
123 |
6892975 |
CHECK_OBJ_NOTNULL(sym->symtab, SYMTAB_MAGIC); |
124 |
6892975 |
vcc_symtabname(vsb, sym->symtab); |
125 |
6892975 |
} |
126 |
1457100 |
|
127 |
|
static struct symtab * |
128 |
15365400 |
vcc_symtab_new(const char *name) |
129 |
|
{ |
130 |
|
struct symtab *st; |
131 |
|
|
132 |
15365400 |
ALLOC_OBJ(st, SYMTAB_MAGIC); |
133 |
15365400 |
AN(st); |
134 |
15365400 |
st->name = name; |
135 |
16822500 |
st->nlen = strlen(st->name); |
136 |
15365400 |
VTAILQ_INIT(&st->children); |
137 |
15365400 |
VTAILQ_INIT(&st->symbols); |
138 |
15365400 |
return (st); |
139 |
|
} |
140 |
|
|
141 |
|
static struct symtab * |
142 |
34570575 |
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 |
1457100 |
int i; |
147 |
|
const char *q; |
148 |
|
|
149 |
33113475 |
assert(tok == ID || tok == CSTR); |
150 |
33113475 |
if (e == NULL) |
151 |
14087250 |
e = strchr(b, '\0'); |
152 |
33113475 |
q = e; |
153 |
|
|
154 |
76742025 |
while (b < e) { |
155 |
43628550 |
if (tok == ID) { |
156 |
359210600 |
for (q = b; q < e && *q != '.'; q++) |
157 |
315582300 |
continue; |
158 |
43628300 |
} |
159 |
45085650 |
l = pdiff(b, q); |
160 |
683726200 |
VTAILQ_FOREACH(st2, &st->children, list) { |
161 |
673599875 |
i = strncasecmp(st2->name, b, l); |
162 |
673599875 |
if (i < 0) |
163 |
640097650 |
continue; |
164 |
34959325 |
if (i == 0 && l == st2->nlen) |
165 |
28409125 |
break; |
166 |
5093100 |
st3 = vcc_symtab_new(vcc_Dup_be(b, q)); |
167 |
5093100 |
st3->parent = st; |
168 |
5093100 |
VTAILQ_INSERT_BEFORE(st2, st3, list); |
169 |
5093100 |
st2 = st3; |
170 |
5093100 |
break; |
171 |
|
} |
172 |
45085650 |
if (st2 == NULL) { |
173 |
10126325 |
st2 = vcc_symtab_new(vcc_Dup_be(b, q)); |
174 |
10126325 |
st2->parent = st; |
175 |
10126325 |
VTAILQ_INSERT_TAIL(&st->children, st2, list); |
176 |
10126325 |
} |
177 |
43628550 |
st = st2; |
178 |
43628550 |
b = q + 1; |
179 |
1457100 |
} |
180 |
33113475 |
return (st); |
181 |
|
} |
182 |
|
|
183 |
|
static struct symbol * |
184 |
14886225 |
vcc_new_symbol(struct vcc *tl, struct symtab *st, |
185 |
1457100 |
vcc_kind_t kind, int vlo, int vhi) |
186 |
|
{ |
187 |
|
struct symbol *sym; |
188 |
|
|
189 |
14886225 |
sym = TlAlloc(tl, sizeof *sym); |
190 |
14886225 |
INIT_OBJ(sym, SYMBOL_MAGIC); |
191 |
16343325 |
AN(sym); |
192 |
14886225 |
sym->name = st->name; |
193 |
14886225 |
sym->symtab = st; |
194 |
14886225 |
sym->kind = kind; |
195 |
14886225 |
sym->type = VOID; |
196 |
14886225 |
sym->lorev = vlo; |
197 |
14886225 |
sym->hirev = vhi; |
198 |
14886225 |
VTAILQ_INSERT_TAIL(&st->symbols, sym, list); |
199 |
14886225 |
return (sym); |
200 |
|
} |
201 |
|
|
202 |
1457100 |
static struct symbol * |
203 |
33114125 |
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 |
33436500 |
VTAILQ_FOREACH(sym, &st->symbols, list) { |
210 |
16922275 |
if (sym->lorev > vhi || sym->hirev < vlo) |
211 |
322175 |
continue; |
212 |
16600100 |
if (kind == SYM_NONE && kind == sym->kind && |
213 |
831525 |
sym->wildcard == NULL) |
214 |
0 |
continue; |
215 |
16789600 |
if (tl->syntax < VCL_41 && strcmp(sym->name, "default") && |
216 |
9451275 |
kind != SYM_NONE && kind != sym->kind && |
217 |
189500 |
sym->wildcard == NULL) |
218 |
200 |
continue; |
219 |
16599900 |
return (sym); |
220 |
|
} |
221 |
16514225 |
pst = st->parent; |
222 |
16514225 |
if (pst == NULL) |
223 |
0 |
return (sym); |
224 |
16514225 |
psym = VTAILQ_FIRST(&pst->symbols); |
225 |
16514225 |
if (psym == NULL) |
226 |
8478475 |
return (sym); |
227 |
8035750 |
if (psym->wildcard == NULL) |
228 |
6663575 |
return (sym); |
229 |
|
|
230 |
1372175 |
sym = vcc_new_symbol(tl, st, kind, vlo, vhi); |
231 |
1372175 |
psym->wildcard(tl, psym, sym); |
232 |
1372175 |
if (tl->err) |
233 |
50 |
return (NULL); |
234 |
1372125 |
return (sym); |
235 |
33114125 |
} |
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 |
15511650 |
VCC_SymbolGet(struct vcc *tl, vcc_ns_t ns, vcc_kind_t kind, |
268 |
|
const struct symmode *e, const struct symxref *x) |
269 |
|
{ |
270 |
15511650 |
struct symtab *st, *st2 = NULL; |
271 |
15511650 |
struct symbol *sym = NULL, *sym2 = NULL; |
272 |
15511650 |
struct token *t0, *tn, *tn1, *tn2 = NULL; |
273 |
|
|
274 |
15511650 |
AN(tl); |
275 |
15511650 |
CHECK_OBJ_NOTNULL(ns, VCC_NAMESPACE_MAGIC); |
276 |
15511650 |
AN(ns->name); |
277 |
15511650 |
CHECK_OBJ_NOTNULL(kind, KIND_MAGIC); |
278 |
15511650 |
AN(e); |
279 |
15511650 |
AN(x); |
280 |
15511650 |
AN(x->name); |
281 |
15511650 |
if (tl->syntax >= VCL_41 && e == SYMTAB_CREATE && kind != SYM_SUB && |
282 |
57350 |
vcc_Has_vcl_prefix(tl->t->b)) { |
283 |
50 |
VSB_cat(tl->sb, "Symbols named 'vcl_*' are reserved.\nAt:"); |
284 |
50 |
vcc_ErrWhere(tl, tl->t); |
285 |
50 |
return (NULL); |
286 |
|
} |
287 |
|
|
288 |
15511600 |
st = tl->syms[ns->id]; |
289 |
15511600 |
t0 = tl->t; |
290 |
15511600 |
tn = tl->t; |
291 |
15511600 |
assert(tn->tok == ID); |
292 |
20483600 |
while (1) { |
293 |
20483600 |
assert(tn->tok == ID || tn->tok == CSTR); |
294 |
20483600 |
if (tn->tok == CSTR && tl->syntax < VCL_41) { |
295 |
25 |
VSB_cat(tl->sb, |
296 |
|
"Quoted headers are available for VCL >= 4.1.\n" |
297 |
|
"At:"); |
298 |
25 |
vcc_ErrWhere(tl, tn); |
299 |
25 |
return (NULL); |
300 |
|
} |
301 |
20483575 |
if (tn->tok == ID) |
302 |
20483325 |
st = vcc_symtab_str(st, tn->b, tn->e, tn->tok); |
303 |
|
else |
304 |
250 |
st = vcc_symtab_str(st, tn->dec, NULL, tn->tok); |
305 |
20483575 |
sym2 = vcc_sym_in_tab(tl, st, kind, tl->syntax, tl->syntax); |
306 |
20483575 |
if (sym2 != NULL) { |
307 |
17384650 |
sym = sym2; |
308 |
17384650 |
st2 = st; |
309 |
17384650 |
tn2 = tn; |
310 |
17384650 |
} |
311 |
20483575 |
tn1 = vcc_PeekTokenFrom(tl, tn); |
312 |
20483575 |
if (tn1->tok != '.') |
313 |
15511525 |
break; |
314 |
4972050 |
tn1 = vcc_PeekTokenFrom(tl, tn1); |
315 |
4972050 |
if (tn1->tok == CSTR && (sym == NULL || sym->wildcard == NULL)) |
316 |
25 |
break; |
317 |
4972025 |
if (tn1->tok != CSTR && tn1->tok != ID) |
318 |
25 |
break; |
319 |
4972000 |
tn = tn1; |
320 |
|
} |
321 |
15511575 |
if (sym != NULL && sym->kind == SYM_ALIAS) { |
322 |
50 |
assert(ns == SYM_MAIN); |
323 |
50 |
st = vcc_symtab_str(tl->syms[ns->id], sym->eval_priv, NULL, ID); |
324 |
50 |
AN(st); |
325 |
50 |
st2 = st; |
326 |
50 |
sym = vcc_sym_in_tab(tl, st, SYM_NONE, sym->lorev, sym->hirev); |
327 |
50 |
AN(sym); |
328 |
50 |
} |
329 |
15511575 |
if (sym != NULL && sym->kind == SYM_VMOD && e->partial) |
330 |
25 |
e = SYMTAB_EXISTING; |
331 |
15511575 |
if (sym != NULL && e->partial) { |
332 |
8247475 |
st = st2; |
333 |
8247475 |
tn = tn2; |
334 |
15511575 |
} else if (st != st2) { |
335 |
2929625 |
sym = NULL; |
336 |
2929625 |
} |
337 |
15511575 |
if (tl->err || (sym == NULL && e->noerr)) |
338 |
1457700 |
return (sym); |
339 |
14053875 |
AN(st); |
340 |
14053875 |
AN(tn); |
341 |
14053875 |
if (sym == NULL && e == SYMTAB_CREATE) |
342 |
1471275 |
sym = vcc_new_symbol(tl, st, kind, tl->syntax, tl->syntax); |
343 |
14053875 |
tl->t = vcc_PeekTokenFrom(tl, tn); |
344 |
14053875 |
if (tl->err) |
345 |
0 |
return (NULL); |
346 |
14053875 |
if (sym == NULL) { |
347 |
650 |
VSB_printf(tl->sb, "%s: '", e->name); |
348 |
650 |
vcc_PrintTokens(tl, t0, tl->t); |
349 |
650 |
VSB_cat(tl->sb, "'"); |
350 |
650 |
sym = vcc_sym_in_tab(tl, st, kind, VCL_LOW, VCL_HIGH); |
351 |
650 |
if (sym != NULL && sym->kind != SYM_OBJECT && |
352 |
250 |
sym->kind != SYM_INSTANCE) { /* XXX: too specific */ |
353 |
250 |
VSB_cat(tl->sb, " (Only available when"); |
354 |
250 |
if (sym->lorev >= VCL_LOW) |
355 |
200 |
VSB_printf(tl->sb, " %.1f <=", .1 * sym->lorev); |
356 |
250 |
VSB_cat(tl->sb, " VCL syntax"); |
357 |
250 |
if (sym->hirev <= VCL_HIGH) |
358 |
50 |
VSB_printf(tl->sb, " <= %.1f", .1 * sym->hirev); |
359 |
250 |
VSB_cat(tl->sb, ")"); |
360 |
250 |
} |
361 |
650 |
VSB_cat(tl->sb, "\nAt: "); |
362 |
650 |
vcc_ErrWhere2(tl, t0, tl->t); |
363 |
650 |
return (NULL); |
364 |
|
} |
365 |
14053225 |
if (kind != SYM_NONE && kind != sym->kind) { |
366 |
75 |
VSB_cat(tl->sb, "Symbol '"); |
367 |
75 |
vcc_PrintTokens(tl, t0, tl->t); |
368 |
150 |
VSB_printf(tl->sb, "' has wrong type (%s), expected %s:", |
369 |
75 |
sym->kind->name, kind->name); |
370 |
75 |
VSB_cat(tl->sb, "\nAt: "); |
371 |
75 |
vcc_ErrWhere2(tl, t0, tl->t); |
372 |
75 |
if (sym->def_b != NULL) { |
373 |
50 |
VSB_cat(tl->sb, "Symbol was defined here: "); |
374 |
50 |
vcc_ErrWhere(tl, sym->def_b); |
375 |
75 |
} else if (sym->ref_b != NULL) { |
376 |
25 |
VSB_cat(tl->sb, "Symbol was declared here: "); |
377 |
25 |
vcc_ErrWhere(tl, sym->ref_b); |
378 |
25 |
} else { |
379 |
0 |
VSB_cat(tl->sb, "Symbol was builtin\n"); |
380 |
|
} |
381 |
75 |
return (NULL); |
382 |
|
} |
383 |
14053150 |
if (x == XREF_DEF) { |
384 |
2391400 |
if (sym->def_b == NULL) |
385 |
2356100 |
sym->def_b = t0; |
386 |
2391400 |
sym->ndef++; |
387 |
14053150 |
} else if (x == XREF_REF) { |
388 |
4625925 |
if (sym->ref_b == NULL) |
389 |
2933925 |
sym->ref_b = t0; |
390 |
4625925 |
sym->nref++; |
391 |
4625925 |
} else { |
392 |
7035825 |
assert (x == XREF_NONE); |
393 |
|
} |
394 |
14053150 |
return (sym); |
395 |
15511650 |
} |
396 |
|
|
397 |
|
static struct symbol * |
398 |
87075 |
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 |
87075 |
buf = VSB_new_auto(); |
405 |
87075 |
AN(buf); |
406 |
87075 |
VSB_printf(buf, "%s.%.*s", type->name, PF(tl->t)); |
407 |
87075 |
AZ(VSB_finish(buf)); |
408 |
|
|
409 |
|
/* NB: we create a fake token but errors are handled by the caller. */ |
410 |
87075 |
memcpy(t, tl->t, sizeof *t); |
411 |
87075 |
t->b = VSB_data(buf); |
412 |
87075 |
t->e = t->b + VSB_len(buf); |
413 |
|
|
414 |
87075 |
t0 = tl->t; |
415 |
87075 |
tl->t = t; |
416 |
87075 |
sym = VCC_SymbolGet(tl, ns, kind, SYMTAB_NOERR, XREF_NONE); |
417 |
87075 |
tl->t = t0; |
418 |
87075 |
VSB_destroy(&buf); |
419 |
|
|
420 |
87075 |
return (sym); |
421 |
|
} |
422 |
|
|
423 |
|
struct symbol * |
424 |
87075 |
VCC_TypeSymbol(struct vcc *tl, vcc_kind_t kind, vcc_type_t type) |
425 |
|
{ |
426 |
|
|
427 |
87075 |
if (strchr(type->name, '.') == NULL) |
428 |
67775 |
return (vcc_TypeSymbol(tl, SYM_TYPE, kind, type)); |
429 |
|
|
430 |
|
/* NB: type imported from a VMOD */ |
431 |
19300 |
return (vcc_TypeSymbol(tl, SYM_MAIN, kind, type)); |
432 |
87075 |
} |
433 |
|
|
434 |
|
struct symbol * |
435 |
12626575 |
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 |
12626575 |
AN(tl); |
443 |
12626575 |
AN(b); |
444 |
12626575 |
CHECK_OBJ_NOTNULL(ns, VCC_NAMESPACE_MAGIC); |
445 |
12626575 |
CHECK_OBJ_NOTNULL(kind, KIND_MAGIC); |
446 |
12626575 |
assert(vlo <= vhi); |
447 |
|
|
448 |
12626575 |
if (tl->syms[ns->id] == NULL) |
449 |
145975 |
tl->syms[ns->id] = vcc_symtab_new(""); |
450 |
12626575 |
st = vcc_symtab_str(tl->syms[ns->id], b, NULL, ID); |
451 |
12626575 |
AN(st); |
452 |
12626575 |
sym = vcc_sym_in_tab(tl, st, kind, vlo, vhi); |
453 |
12626575 |
if (sym != NULL) { |
454 |
583800 |
assert(sym->kind == SYM_VAR); |
455 |
583800 |
parent = sym->eval_priv; |
456 |
583800 |
AN(parent); |
457 |
583800 |
AN(parent->wildcard); |
458 |
583800 |
assert(sym->type == parent->type); |
459 |
583800 |
return (sym); |
460 |
|
} |
461 |
12042775 |
AZ(sym); |
462 |
12042775 |
sym = vcc_new_symbol(tl, st, kind, vlo, vhi); |
463 |
12042775 |
AN(sym); |
464 |
12042775 |
sym->noref = 1; |
465 |
12042775 |
return (sym); |
466 |
12626575 |
} |
467 |
|
|
468 |
|
struct symbol * |
469 |
3275 |
VCC_MkSymAlias(struct vcc *tl, const char *alias, const char *name) |
470 |
|
{ |
471 |
|
struct symbol *sym_alias, *sym; |
472 |
|
struct symtab *st; |
473 |
|
|
474 |
3275 |
AN(tl); |
475 |
3275 |
AN(alias); |
476 |
3275 |
AN(name); |
477 |
|
|
478 |
3275 |
st = vcc_symtab_str(tl->syms[SYM_MAIN->id], name, NULL, ID); |
479 |
3275 |
AN(st); |
480 |
3275 |
sym = vcc_sym_in_tab(tl, st, SYM_NONE, VCL_LOW, VCL_HIGH); |
481 |
3275 |
AN(sym); |
482 |
3275 |
assert(sym->kind != SYM_ALIAS); |
483 |
6550 |
sym_alias = VCC_MkSym(tl, alias, SYM_MAIN, SYM_ALIAS, sym->lorev, |
484 |
3275 |
sym->hirev); |
485 |
3275 |
AN(sym_alias); |
486 |
3275 |
sym_alias->eval_priv = strdup(name); |
487 |
3275 |
AN(sym_alias->eval_priv); |
488 |
3275 |
return (sym); |
489 |
|
} |
490 |
|
|
491 |
|
static void |
492 |
73571050 |
vcc_walksymbols(struct vcc *tl, const struct symtab *root, |
493 |
|
symwalk_f *func, vcc_kind_t kind) |
494 |
|
{ |
495 |
|
struct symbol *sym; |
496 |
73571050 |
struct symtab *st1, *st2 = NULL; |
497 |
|
|
498 |
145045900 |
VTAILQ_FOREACH(sym, &root->symbols, list) { |
499 |
71476200 |
if (kind == SYM_NONE || kind == sym->kind) |
500 |
32962175 |
func(tl, sym); |
501 |
71476200 |
ERRCHK(tl); |
502 |
71474850 |
} |
503 |
146726150 |
VTAILQ_FOREACH(st1, &root->children, list) { |
504 |
73157800 |
if (st2 != NULL) |
505 |
66007750 |
assert(strcasecmp(st1->name, st2->name) >= 0); |
506 |
73157800 |
st2 = st1; |
507 |
73157800 |
vcc_walksymbols(tl, st1, func, kind); |
508 |
73157800 |
ERRCHK(tl); |
509 |
73156450 |
} |
510 |
73571050 |
} |
511 |
|
|
512 |
|
void |
513 |
413250 |
VCC_WalkSymbols(struct vcc *tl, symwalk_f *func, vcc_ns_t ns, vcc_kind_t kind) |
514 |
|
{ |
515 |
|
|
516 |
413250 |
CHECK_OBJ_NOTNULL(ns, VCC_NAMESPACE_MAGIC); |
517 |
413250 |
vcc_walksymbols(tl, tl->syms[ns->id], func, kind); |
518 |
413250 |
} |
519 |
|
|
520 |
|
void |
521 |
2867900 |
VCC_GlobalSymbol(struct symbol *sym, vcc_type_t type) |
522 |
|
{ |
523 |
|
vcc_kind_t kind; |
524 |
|
struct vsb *vsb; |
525 |
|
|
526 |
2867900 |
CHECK_OBJ_NOTNULL(sym, SYMBOL_MAGIC); |
527 |
2867900 |
AN(type); |
528 |
2867900 |
AN(type->global_pfx); |
529 |
|
|
530 |
2867900 |
kind = VCC_HandleKind(type); |
531 |
|
|
532 |
2867900 |
if (sym->lname != NULL) { |
533 |
1410800 |
AN(sym->rname); |
534 |
1410800 |
assert(sym->type == type); |
535 |
1410800 |
assert(sym->kind == kind); |
536 |
1410800 |
return; |
537 |
|
} |
538 |
|
|
539 |
1457100 |
vsb = VSB_new_auto(); |
540 |
1457100 |
AN(vsb); |
541 |
1457100 |
VSB_printf(vsb, "%s_", type->global_pfx); |
542 |
1457100 |
VCC_PrintCName(vsb, sym->name, NULL); |
543 |
1457100 |
AZ(VSB_finish(vsb)); |
544 |
1457100 |
sym->lname = strdup(VSB_data(vsb)); |
545 |
1457100 |
AN(sym->lname); |
546 |
1457100 |
if (type == SUB) { |
547 |
1412600 |
VSB_clear(vsb); |
548 |
1412600 |
VSB_printf(vsb, "sub_%s", sym->lname); |
549 |
1412600 |
AZ(VSB_finish(vsb)); |
550 |
1412600 |
} |
551 |
1457100 |
sym->rname = strdup(VSB_data(vsb)); |
552 |
1457100 |
AN(sym->rname); |
553 |
1457100 |
VSB_destroy(&vsb); |
554 |
|
|
555 |
1457100 |
sym->type = type; |
556 |
1457100 |
sym->kind = kind; |
557 |
1457100 |
if (sym->kind != SYM_NONE) { |
558 |
1457100 |
AZ(VCT_invalid_name(sym->rname, NULL)); |
559 |
1457100 |
if (type == SUB) |
560 |
1412600 |
sym->eval = vcc_Eval_Sub; |
561 |
|
else |
562 |
44500 |
sym->eval = vcc_Eval_Handle; |
563 |
1457100 |
} 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 |
2867900 |
} |
570 |
|
|
571 |
|
struct symbol * |
572 |
44550 |
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 |
44550 |
kind = VCC_HandleKind(fmt); |
580 |
44550 |
assert(kind != SYM_NONE); |
581 |
|
|
582 |
44550 |
t = tl->t; |
583 |
44550 |
sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_NONE, SYMTAB_NOERR, XREF_NONE); |
584 |
44550 |
if (sym != NULL && sym->def_b != NULL && kind == sym->kind) { |
585 |
75 |
p = sym->kind->name; |
586 |
150 |
VSB_printf(tl->sb, "%c%s '%.*s' redefined.\n", |
587 |
75 |
toupper(*p), p + 1, PF(t)); |
588 |
75 |
vcc_ErrWhere(tl, t); |
589 |
75 |
VSB_cat(tl->sb, "First definition:\n"); |
590 |
75 |
AN(sym->def_b); |
591 |
75 |
vcc_ErrWhere(tl, sym->def_b); |
592 |
75 |
return (sym); |
593 |
44475 |
} else if (sym != NULL && sym->def_b != NULL) { |
594 |
50 |
VSB_printf(tl->sb, "Name '%.*s' already defined.\n", PF(t)); |
595 |
50 |
vcc_ErrWhere(tl, t); |
596 |
50 |
VSB_cat(tl->sb, "First definition:\n"); |
597 |
50 |
AN(sym->def_b); |
598 |
50 |
vcc_ErrWhere(tl, sym->def_b); |
599 |
50 |
return (sym); |
600 |
44425 |
} else if (sym != NULL && sym->kind != kind) { |
601 |
50 |
VSB_printf(tl->sb, |
602 |
|
"Name %.*s must have type '%s'.\n", |
603 |
25 |
PF(t), sym->kind->name); |
604 |
25 |
vcc_ErrWhere(tl, t); |
605 |
25 |
return (sym); |
606 |
|
} |
607 |
44400 |
if (sym == NULL) |
608 |
44350 |
sym = VCC_SymbolGet(tl, SYM_MAIN, kind, SYMTAB_CREATE, |
609 |
|
XREF_NONE); |
610 |
44400 |
if (sym == NULL) |
611 |
0 |
return (NULL); |
612 |
44400 |
AN(sym); |
613 |
44400 |
AZ(sym->ndef); |
614 |
44400 |
VCC_GlobalSymbol(sym, fmt); |
615 |
44400 |
sym->ndef = 1; |
616 |
44400 |
if (sym->def_b == NULL) |
617 |
44400 |
sym->def_b = t; |
618 |
44400 |
return (sym); |
619 |
44550 |
} |
620 |
|
/*lint -restore */ |