read.c revision 1.9 1 1.9 lukem /* $NetBSD: read.c,v 1.9 2001/05/28 12:40:38 lukem Exp $ */
2 1.2 cgd
3 1.1 cgd /*
4 1.3 cgd * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
5 1.1 cgd * Copyright (c) 1994, 1995 Jochen Pohl
6 1.1 cgd * All Rights Reserved.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by Jochen Pohl for
19 1.1 cgd * The NetBSD Project.
20 1.1 cgd * 4. The name of the author may not be used to endorse or promote products
21 1.1 cgd * derived from this software without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.5 christos #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.9 lukem __RCSID("$NetBSD: read.c,v 1.9 2001/05/28 12:40:38 lukem Exp $");
38 1.1 cgd #endif
39 1.1 cgd
40 1.1 cgd #include <stdio.h>
41 1.1 cgd #include <stdlib.h>
42 1.1 cgd #include <string.h>
43 1.1 cgd #include <ctype.h>
44 1.1 cgd #include <limits.h>
45 1.1 cgd #include <err.h>
46 1.1 cgd
47 1.1 cgd #include "lint2.h"
48 1.1 cgd
49 1.1 cgd
50 1.1 cgd /* index of current (included) source file */
51 1.1 cgd static int srcfile;
52 1.1 cgd
53 1.1 cgd /*
54 1.1 cgd * The array pointed to by inpfns maps the file name indices of input files
55 1.1 cgd * to the file name indices used in lint2
56 1.1 cgd */
57 1.1 cgd static short *inpfns;
58 1.1 cgd static size_t ninpfns;
59 1.1 cgd
60 1.1 cgd /*
61 1.1 cgd * The array pointed to by *fnames maps file name indizes to file names.
62 1.1 cgd * Indices of type short are used instead of pointers to save memory.
63 1.1 cgd */
64 1.1 cgd const char **fnames;
65 1.1 cgd static size_t nfnames;
66 1.1 cgd
67 1.1 cgd /*
68 1.1 cgd * Types are shared (to save memory for the types itself) and accessed
69 1.1 cgd * via indices (to save memory for references to types (indices are short)).
70 1.1 cgd * To share types, a equal type must be located fast. This is done by a
71 1.1 cgd * hash table. Access by indices is done via an array of pointers to the
72 1.1 cgd * types.
73 1.1 cgd */
74 1.1 cgd typedef struct thtab {
75 1.1 cgd const char *th_name;
76 1.1 cgd u_short th_idx;
77 1.1 cgd struct thtab *th_nxt;
78 1.1 cgd } thtab_t;
79 1.1 cgd static thtab_t **thtab; /* hash table */
80 1.1 cgd type_t **tlst; /* array for indexed access */
81 1.1 cgd static size_t tlstlen; /* length of tlst */
82 1.1 cgd
83 1.4 cgd static hte_t **renametab;
84 1.4 cgd
85 1.1 cgd /* index of current C source file (as spezified at the command line) */
86 1.1 cgd static int csrcfile;
87 1.1 cgd
88 1.1 cgd
89 1.9 lukem static void inperr(void);
90 1.9 lukem static void setsrc(const char *);
91 1.9 lukem static void setfnid(int, const char *);
92 1.9 lukem static void funccall(pos_t *, const char *);
93 1.9 lukem static void decldef(pos_t *, const char *);
94 1.9 lukem static void usedsym(pos_t *, const char *);
95 1.9 lukem static u_short inptype(const char *, const char **);
96 1.9 lukem static int gettlen(const char *, const char **);
97 1.9 lukem static u_short findtype(const char *, size_t, int);
98 1.9 lukem static u_short storetyp(type_t *, const char *, size_t, int);
99 1.9 lukem static int thash(const char *, size_t);
100 1.9 lukem static char *inpqstrg(const char *, const char **);
101 1.9 lukem static const char *inpname(const char *, const char **);
102 1.9 lukem static int getfnidx(const char *);
103 1.1 cgd
104 1.1 cgd void
105 1.9 lukem readfile(const char *name)
106 1.1 cgd {
107 1.1 cgd FILE *inp;
108 1.1 cgd size_t len;
109 1.1 cgd const char *cp;
110 1.5 christos char *line, *eptr, rt = '\0';
111 1.1 cgd int cline, isrc, iline;
112 1.1 cgd pos_t pos;
113 1.1 cgd
114 1.1 cgd if (inpfns == NULL)
115 1.1 cgd inpfns = xcalloc(ninpfns = 128, sizeof (short));
116 1.1 cgd if (fnames == NULL)
117 1.1 cgd fnames = xcalloc(nfnames = 256, sizeof (char *));
118 1.1 cgd if (tlstlen == 0)
119 1.1 cgd tlst = xcalloc(tlstlen = 256, sizeof (type_t *));
120 1.1 cgd if (thtab == NULL)
121 1.1 cgd thtab = xcalloc(THSHSIZ2, sizeof (thtab_t));
122 1.1 cgd
123 1.4 cgd _inithash(&renametab);
124 1.4 cgd
125 1.1 cgd srcfile = getfnidx(name);
126 1.1 cgd
127 1.1 cgd if ((inp = fopen(name, "r")) == NULL)
128 1.1 cgd err(1, "cannot open %s", name);
129 1.1 cgd
130 1.1 cgd while ((line = fgetln(inp, &len)) != NULL) {
131 1.1 cgd
132 1.1 cgd if (len == 0 || line[len - 1] != '\n')
133 1.1 cgd inperr();
134 1.1 cgd line[len - 1] = '\0';
135 1.1 cgd cp = line;
136 1.1 cgd
137 1.1 cgd /* line number in csrcfile */
138 1.1 cgd cline = (int)strtol(cp, &eptr, 10);
139 1.1 cgd if (cp == eptr) {
140 1.1 cgd cline = -1;
141 1.1 cgd } else {
142 1.1 cgd cp = eptr;
143 1.1 cgd }
144 1.1 cgd
145 1.1 cgd /* record type */
146 1.1 cgd if (*cp != '\0') {
147 1.1 cgd rt = *cp++;
148 1.1 cgd } else {
149 1.1 cgd inperr();
150 1.1 cgd }
151 1.1 cgd
152 1.1 cgd if (rt == 'S') {
153 1.1 cgd setsrc(cp);
154 1.1 cgd continue;
155 1.1 cgd } else if (rt == 's') {
156 1.1 cgd setfnid(cline, cp);
157 1.1 cgd continue;
158 1.1 cgd }
159 1.1 cgd
160 1.1 cgd /*
161 1.1 cgd * Index of (included) source file. If this index is
162 1.1 cgd * different from csrcfile, it refers to an included
163 1.1 cgd * file.
164 1.1 cgd */
165 1.1 cgd isrc = (int)strtol(cp, &eptr, 10);
166 1.1 cgd if (cp == eptr)
167 1.1 cgd inperr();
168 1.1 cgd cp = eptr;
169 1.1 cgd isrc = inpfns[isrc];
170 1.1 cgd
171 1.1 cgd /* line number in isrc */
172 1.1 cgd if (*cp++ != '.')
173 1.1 cgd inperr();
174 1.1 cgd iline = (int)strtol(cp, &eptr, 10);
175 1.1 cgd if (cp == eptr)
176 1.1 cgd inperr();
177 1.1 cgd cp = eptr;
178 1.1 cgd
179 1.1 cgd pos.p_src = (u_short)csrcfile;
180 1.1 cgd pos.p_line = (u_short)cline;
181 1.1 cgd pos.p_isrc = (u_short)isrc;
182 1.1 cgd pos.p_iline = (u_short)iline;
183 1.1 cgd
184 1.1 cgd /* process rest of this record */
185 1.1 cgd switch (rt) {
186 1.1 cgd case 'c':
187 1.1 cgd funccall(&pos, cp);
188 1.1 cgd break;
189 1.1 cgd case 'd':
190 1.1 cgd decldef(&pos, cp);
191 1.1 cgd break;
192 1.1 cgd case 'u':
193 1.1 cgd usedsym(&pos, cp);
194 1.1 cgd break;
195 1.1 cgd default:
196 1.1 cgd inperr();
197 1.1 cgd }
198 1.1 cgd
199 1.1 cgd }
200 1.1 cgd
201 1.4 cgd _destroyhash(renametab);
202 1.4 cgd
203 1.1 cgd if (ferror(inp))
204 1.1 cgd err(1, "read error on %s", name);
205 1.1 cgd
206 1.1 cgd (void)fclose(inp);
207 1.1 cgd }
208 1.1 cgd
209 1.1 cgd
210 1.1 cgd static void
211 1.9 lukem inperr(void)
212 1.1 cgd {
213 1.9 lukem
214 1.1 cgd errx(1, "input file error: %s", fnames[srcfile]);
215 1.1 cgd }
216 1.1 cgd
217 1.1 cgd /*
218 1.1 cgd * Set the name of the C source file of the .ln file which is
219 1.1 cgd * currently read.
220 1.1 cgd */
221 1.1 cgd static void
222 1.9 lukem setsrc(const char *cp)
223 1.1 cgd {
224 1.9 lukem
225 1.1 cgd csrcfile = getfnidx(cp);
226 1.1 cgd }
227 1.1 cgd
228 1.1 cgd /*
229 1.1 cgd * setfnid() gets as input an index as used in an input file and the
230 1.1 cgd * associated file name. If neccessary, it creates a new lint2 file
231 1.1 cgd * name index for this file name and creates the mapping of the index
232 1.1 cgd * as used in the input file to the index used in lint2.
233 1.1 cgd */
234 1.1 cgd static void
235 1.9 lukem setfnid(int fid, const char *cp)
236 1.1 cgd {
237 1.9 lukem
238 1.1 cgd if (fid == -1)
239 1.1 cgd inperr();
240 1.1 cgd
241 1.1 cgd if (fid >= ninpfns) {
242 1.1 cgd inpfns = xrealloc(inpfns, (ninpfns * 2) * sizeof (short));
243 1.1 cgd (void)memset(inpfns + ninpfns, 0, ninpfns * sizeof (short));
244 1.1 cgd ninpfns *= 2;
245 1.1 cgd }
246 1.1 cgd /*
247 1.1 cgd * Should always be true because indices written in the output
248 1.1 cgd * file by lint1 are always the previous index + 1.
249 1.1 cgd */
250 1.1 cgd if (fid >= ninpfns)
251 1.1 cgd errx(1, "internal error: setfnid()");
252 1.1 cgd inpfns[fid] = (u_short)getfnidx(cp);
253 1.1 cgd }
254 1.1 cgd
255 1.1 cgd /*
256 1.1 cgd * Process a function call record (c-record).
257 1.1 cgd */
258 1.1 cgd static void
259 1.9 lukem funccall(pos_t *posp, const char *cp)
260 1.1 cgd {
261 1.1 cgd arginf_t *ai, **lai;
262 1.1 cgd char c, *eptr;
263 1.1 cgd int rused, rdisc;
264 1.1 cgd hte_t *hte;
265 1.1 cgd fcall_t *fcall;
266 1.4 cgd const char *name;
267 1.1 cgd
268 1.1 cgd fcall = xalloc(sizeof (fcall_t));
269 1.1 cgd STRUCT_ASSIGN(fcall->f_pos, *posp);
270 1.1 cgd
271 1.1 cgd /* read flags */
272 1.1 cgd rused = rdisc = 0;
273 1.1 cgd lai = &fcall->f_args;
274 1.1 cgd while ((c = *cp) == 'u' || c == 'i' || c == 'd' ||
275 1.1 cgd c == 'z' || c == 'p' || c == 'n' || c == 's') {
276 1.1 cgd cp++;
277 1.1 cgd switch (c) {
278 1.1 cgd case 'u':
279 1.1 cgd if (rused || rdisc)
280 1.1 cgd inperr();
281 1.1 cgd rused = 1;
282 1.1 cgd break;
283 1.1 cgd case 'i':
284 1.1 cgd if (rused || rdisc)
285 1.1 cgd inperr();
286 1.1 cgd break;
287 1.1 cgd case 'd':
288 1.1 cgd if (rused || rdisc)
289 1.1 cgd inperr();
290 1.1 cgd rdisc = 1;
291 1.1 cgd break;
292 1.1 cgd case 'z':
293 1.1 cgd case 'p':
294 1.1 cgd case 'n':
295 1.1 cgd case 's':
296 1.1 cgd ai = xalloc(sizeof (arginf_t));
297 1.1 cgd ai->a_num = (int)strtol(cp, &eptr, 10);
298 1.1 cgd if (cp == eptr)
299 1.1 cgd inperr();
300 1.1 cgd cp = eptr;
301 1.1 cgd if (c == 'z') {
302 1.1 cgd ai->a_pcon = ai->a_zero = 1;
303 1.1 cgd } else if (c == 'p') {
304 1.1 cgd ai->a_pcon = 1;
305 1.1 cgd } else if (c == 'n') {
306 1.1 cgd ai->a_ncon = 1;
307 1.1 cgd } else {
308 1.1 cgd ai->a_fmt = 1;
309 1.1 cgd ai->a_fstrg = inpqstrg(cp, &cp);
310 1.1 cgd }
311 1.1 cgd *lai = ai;
312 1.1 cgd lai = &ai->a_nxt;
313 1.1 cgd break;
314 1.1 cgd }
315 1.1 cgd }
316 1.1 cgd fcall->f_rused = rused;
317 1.1 cgd fcall->f_rdisc = rdisc;
318 1.1 cgd
319 1.1 cgd /* read name of function */
320 1.4 cgd name = inpname(cp, &cp);
321 1.4 cgd
322 1.4 cgd /* first look it up in the renaming table, then in the normal table */
323 1.4 cgd hte = _hsearch(renametab, name, 0);
324 1.4 cgd if (hte != NULL)
325 1.4 cgd hte = hte->h_hte;
326 1.4 cgd else
327 1.4 cgd hte = hsearch(name, 1);
328 1.1 cgd hte->h_used = 1;
329 1.1 cgd
330 1.1 cgd fcall->f_type = inptype(cp, &cp);
331 1.1 cgd
332 1.1 cgd *hte->h_lcall = fcall;
333 1.1 cgd hte->h_lcall = &fcall->f_nxt;
334 1.1 cgd
335 1.1 cgd if (*cp != '\0')
336 1.1 cgd inperr();
337 1.1 cgd }
338 1.1 cgd
339 1.1 cgd /*
340 1.1 cgd * Process a declaration or definition (d-record).
341 1.1 cgd */
342 1.1 cgd static void
343 1.9 lukem decldef(pos_t *posp, const char *cp)
344 1.1 cgd {
345 1.1 cgd sym_t *symp, sym;
346 1.4 cgd char c, *ep, *pos1;
347 1.4 cgd int used, renamed;
348 1.5 christos hte_t *hte, *renamehte = NULL;
349 1.4 cgd const char *name, *rename;
350 1.1 cgd
351 1.1 cgd (void)memset(&sym, 0, sizeof (sym));
352 1.1 cgd STRUCT_ASSIGN(sym.s_pos, *posp);
353 1.1 cgd sym.s_def = NODECL;
354 1.1 cgd
355 1.1 cgd used = 0;
356 1.1 cgd
357 1.1 cgd while ((c = *cp) == 't' || c == 'd' || c == 'e' || c == 'u' ||
358 1.1 cgd c == 'r' || c == 'o' || c == 's' || c == 'v' ||
359 1.1 cgd c == 'P' || c == 'S') {
360 1.1 cgd cp++;
361 1.1 cgd switch (c) {
362 1.1 cgd case 't':
363 1.1 cgd if (sym.s_def != NODECL)
364 1.1 cgd inperr();
365 1.1 cgd sym.s_def = TDEF;
366 1.1 cgd break;
367 1.1 cgd case 'd':
368 1.1 cgd if (sym.s_def != NODECL)
369 1.1 cgd inperr();
370 1.1 cgd sym.s_def = DEF;
371 1.1 cgd break;
372 1.1 cgd case 'e':
373 1.1 cgd if (sym.s_def != NODECL)
374 1.1 cgd inperr();
375 1.1 cgd sym.s_def = DECL;
376 1.1 cgd break;
377 1.1 cgd case 'u':
378 1.1 cgd if (used)
379 1.1 cgd inperr();
380 1.1 cgd used = 1;
381 1.1 cgd break;
382 1.1 cgd case 'r':
383 1.1 cgd if (sym.s_rval)
384 1.1 cgd inperr();
385 1.1 cgd sym.s_rval = 1;
386 1.1 cgd break;
387 1.1 cgd case 'o':
388 1.1 cgd if (sym.s_osdef)
389 1.1 cgd inperr();
390 1.1 cgd sym.s_osdef = 1;
391 1.1 cgd break;
392 1.1 cgd case 's':
393 1.1 cgd if (sym.s_static)
394 1.1 cgd inperr();
395 1.1 cgd sym.s_static = 1;
396 1.1 cgd break;
397 1.1 cgd case 'v':
398 1.1 cgd if (sym.s_va)
399 1.1 cgd inperr();
400 1.1 cgd sym.s_va = 1;
401 1.1 cgd sym.s_nva = (short)strtol(cp, &ep, 10);
402 1.1 cgd if (cp == ep)
403 1.1 cgd inperr();
404 1.1 cgd cp = ep;
405 1.1 cgd break;
406 1.1 cgd case 'P':
407 1.1 cgd if (sym.s_prfl)
408 1.1 cgd inperr();
409 1.1 cgd sym.s_prfl = 1;
410 1.1 cgd sym.s_nprfl = (short)strtol(cp, &ep, 10);
411 1.1 cgd if (cp == ep)
412 1.1 cgd inperr();
413 1.1 cgd cp = ep;
414 1.1 cgd break;
415 1.1 cgd case 'S':
416 1.1 cgd if (sym.s_scfl)
417 1.1 cgd inperr();
418 1.1 cgd sym.s_scfl = 1;
419 1.1 cgd sym.s_nscfl = (short)strtol(cp, &ep, 10);
420 1.1 cgd if (cp == ep)
421 1.1 cgd inperr();
422 1.1 cgd cp = ep;
423 1.1 cgd break;
424 1.1 cgd }
425 1.1 cgd }
426 1.1 cgd
427 1.4 cgd /* read symbol name, doing renaming if necessary */
428 1.4 cgd name = inpname(cp, &cp);
429 1.4 cgd renamed = 0;
430 1.4 cgd if (*cp == 'r') {
431 1.4 cgd cp++;
432 1.4 cgd name = xstrdup(name);
433 1.4 cgd rename = inpname(cp, &cp);
434 1.4 cgd
435 1.4 cgd /* enter it and see if it's already been renamed */
436 1.4 cgd renamehte = _hsearch(renametab, name, 1);
437 1.4 cgd if (renamehte->h_hte == NULL) {
438 1.4 cgd hte = hsearch(rename, 1);
439 1.4 cgd renamehte->h_hte = hte;
440 1.4 cgd renamed = 1;
441 1.4 cgd } else if (strcmp((hte = renamehte->h_hte)->h_name, rename)) {
442 1.4 cgd pos1 = xstrdup(mkpos(&renamehte->h_syms->s_pos));
443 1.4 cgd /* %s renamed multiple times\t%s :: %s */
444 1.4 cgd msg(18, name, pos1, mkpos(&sym.s_pos));
445 1.4 cgd free(pos1);
446 1.4 cgd }
447 1.4 cgd free((char *)name);
448 1.4 cgd } else {
449 1.4 cgd /* it might be a previously-done rename */
450 1.4 cgd hte = _hsearch(renametab, name, 0);
451 1.4 cgd if (hte != NULL)
452 1.4 cgd hte = hte->h_hte;
453 1.4 cgd else
454 1.4 cgd hte = hsearch(name, 1);
455 1.4 cgd }
456 1.1 cgd hte->h_used |= used;
457 1.1 cgd if (sym.s_def == DEF || sym.s_def == TDEF)
458 1.1 cgd hte->h_def = 1;
459 1.1 cgd
460 1.1 cgd sym.s_type = inptype(cp, &cp);
461 1.1 cgd
462 1.1 cgd /*
463 1.1 cgd * Allocate memory for this symbol only if it was not already
464 1.1 cgd * declared or tentatively defined at the same location with
465 1.1 cgd * the same type. Works only for symbols with external linkage,
466 1.1 cgd * because static symbols, tentatively defined at the same location
467 1.1 cgd * but in different translation units are really different symbols.
468 1.1 cgd */
469 1.1 cgd for (symp = hte->h_syms; symp != NULL; symp = symp->s_nxt) {
470 1.1 cgd if (symp->s_pos.p_isrc == sym.s_pos.p_isrc &&
471 1.1 cgd symp->s_pos.p_iline == sym.s_pos.p_iline &&
472 1.1 cgd symp->s_type == sym.s_type &&
473 1.1 cgd ((symp->s_def == DECL && sym.s_def == DECL) ||
474 1.1 cgd (!sflag && symp->s_def == TDEF && sym.s_def == TDEF)) &&
475 1.1 cgd !symp->s_static && !sym.s_static) {
476 1.1 cgd break;
477 1.1 cgd }
478 1.1 cgd }
479 1.1 cgd
480 1.1 cgd if (symp == NULL) {
481 1.1 cgd /* allocsym reserviert keinen Platz fuer s_nva */
482 1.1 cgd if (sym.s_va || sym.s_prfl || sym.s_scfl) {
483 1.1 cgd symp = xalloc(sizeof (sym_t));
484 1.1 cgd STRUCT_ASSIGN(*symp, sym);
485 1.1 cgd } else {
486 1.1 cgd symp = xalloc(sizeof (symp->s_s));
487 1.1 cgd STRUCT_ASSIGN(symp->s_s, sym.s_s);
488 1.1 cgd }
489 1.1 cgd *hte->h_lsym = symp;
490 1.1 cgd hte->h_lsym = &symp->s_nxt;
491 1.4 cgd
492 1.4 cgd /* XXX hack so we can remember where a symbol was renamed */
493 1.4 cgd if (renamed)
494 1.4 cgd renamehte->h_syms = symp;
495 1.1 cgd }
496 1.1 cgd
497 1.1 cgd if (*cp != '\0')
498 1.1 cgd inperr();
499 1.1 cgd }
500 1.1 cgd
501 1.1 cgd /*
502 1.1 cgd * Read an u-record (emited by lint1 if a symbol was used).
503 1.1 cgd */
504 1.1 cgd static void
505 1.9 lukem usedsym(pos_t *posp, const char *cp)
506 1.1 cgd {
507 1.1 cgd usym_t *usym;
508 1.1 cgd hte_t *hte;
509 1.4 cgd const char *name;
510 1.1 cgd
511 1.1 cgd usym = xalloc(sizeof (usym_t));
512 1.1 cgd STRUCT_ASSIGN(usym->u_pos, *posp);
513 1.1 cgd
514 1.1 cgd /* needed as delimiter between two numbers */
515 1.1 cgd if (*cp++ != 'x')
516 1.1 cgd inperr();
517 1.1 cgd
518 1.4 cgd name = inpname(cp, &cp);
519 1.4 cgd hte = _hsearch(renametab, name, 0);
520 1.4 cgd if (hte != NULL)
521 1.4 cgd hte = hte->h_hte;
522 1.4 cgd else
523 1.4 cgd hte = hsearch(name, 1);
524 1.1 cgd hte->h_used = 1;
525 1.1 cgd
526 1.1 cgd *hte->h_lusym = usym;
527 1.1 cgd hte->h_lusym = &usym->u_nxt;
528 1.1 cgd }
529 1.1 cgd
530 1.1 cgd /*
531 1.1 cgd * Read a type and return the index of this type.
532 1.1 cgd */
533 1.1 cgd static u_short
534 1.9 lukem inptype(const char *cp, const char **epp)
535 1.1 cgd {
536 1.1 cgd char c, s, *eptr;
537 1.1 cgd const char *ep;
538 1.1 cgd type_t *tp;
539 1.5 christos int narg, i, osdef = 0;
540 1.1 cgd size_t tlen;
541 1.1 cgd u_short tidx;
542 1.1 cgd int h;
543 1.1 cgd
544 1.1 cgd /* If we have this type already, return it's index. */
545 1.1 cgd tlen = gettlen(cp, &ep);
546 1.1 cgd h = thash(cp, tlen);
547 1.1 cgd if ((tidx = findtype(cp, tlen, h)) != 0) {
548 1.1 cgd *epp = ep;
549 1.1 cgd return (tidx);
550 1.1 cgd }
551 1.1 cgd
552 1.1 cgd /* No, we must create a new type. */
553 1.1 cgd tp = xalloc(sizeof (type_t));
554 1.1 cgd
555 1.1 cgd tidx = storetyp(tp, cp, tlen, h);
556 1.1 cgd
557 1.1 cgd c = *cp++;
558 1.1 cgd
559 1.1 cgd while (c == 'c' || c == 'v') {
560 1.1 cgd if (c == 'c') {
561 1.1 cgd tp->t_const = 1;
562 1.1 cgd } else {
563 1.1 cgd tp->t_volatile = 1;
564 1.1 cgd }
565 1.1 cgd c = *cp++;
566 1.1 cgd }
567 1.1 cgd
568 1.1 cgd if (c == 's' || c == 'u' || c == 'l' || c == 'e') {
569 1.1 cgd s = c;
570 1.1 cgd c = *cp++;
571 1.1 cgd } else {
572 1.1 cgd s = '\0';
573 1.1 cgd }
574 1.1 cgd
575 1.1 cgd switch (c) {
576 1.1 cgd case 'C':
577 1.1 cgd tp->t_tspec = s == 's' ? SCHAR : (s == 'u' ? UCHAR : CHAR);
578 1.1 cgd break;
579 1.1 cgd case 'S':
580 1.1 cgd tp->t_tspec = s == 'u' ? USHORT : SHORT;
581 1.1 cgd break;
582 1.1 cgd case 'I':
583 1.1 cgd tp->t_tspec = s == 'u' ? UINT : INT;
584 1.1 cgd break;
585 1.1 cgd case 'L':
586 1.1 cgd tp->t_tspec = s == 'u' ? ULONG : LONG;
587 1.1 cgd break;
588 1.1 cgd case 'Q':
589 1.1 cgd tp->t_tspec = s == 'u' ? UQUAD : QUAD;
590 1.1 cgd break;
591 1.1 cgd case 'D':
592 1.1 cgd tp->t_tspec = s == 's' ? FLOAT : (s == 'l' ? LDOUBLE : DOUBLE);
593 1.1 cgd break;
594 1.1 cgd case 'V':
595 1.1 cgd tp->t_tspec = VOID;
596 1.1 cgd break;
597 1.1 cgd case 'P':
598 1.1 cgd tp->t_tspec = PTR;
599 1.1 cgd break;
600 1.1 cgd case 'A':
601 1.1 cgd tp->t_tspec = ARRAY;
602 1.1 cgd break;
603 1.1 cgd case 'F':
604 1.1 cgd case 'f':
605 1.1 cgd osdef = c == 'f';
606 1.1 cgd tp->t_tspec = FUNC;
607 1.1 cgd break;
608 1.1 cgd case 'T':
609 1.1 cgd tp->t_tspec = s == 'e' ? ENUM : (s == 's' ? STRUCT : UNION);
610 1.1 cgd break;
611 1.1 cgd }
612 1.1 cgd
613 1.1 cgd switch (tp->t_tspec) {
614 1.1 cgd case ARRAY:
615 1.1 cgd tp->t_dim = (int)strtol(cp, &eptr, 10);
616 1.1 cgd cp = eptr;
617 1.1 cgd tp->t_subt = TP(inptype(cp, &cp));
618 1.1 cgd break;
619 1.1 cgd case PTR:
620 1.1 cgd tp->t_subt = TP(inptype(cp, &cp));
621 1.1 cgd break;
622 1.1 cgd case FUNC:
623 1.1 cgd c = *cp;
624 1.1 cgd if (isdigit((u_char)c)) {
625 1.1 cgd if (!osdef)
626 1.1 cgd tp->t_proto = 1;
627 1.1 cgd narg = (int)strtol(cp, &eptr, 10);
628 1.1 cgd cp = eptr;
629 1.1 cgd tp->t_args = xcalloc((size_t)(narg + 1),
630 1.1 cgd sizeof (type_t *));
631 1.1 cgd for (i = 0; i < narg; i++) {
632 1.1 cgd if (i == narg - 1 && *cp == 'E') {
633 1.1 cgd tp->t_vararg = 1;
634 1.1 cgd cp++;
635 1.1 cgd } else {
636 1.1 cgd tp->t_args[i] = TP(inptype(cp, &cp));
637 1.1 cgd }
638 1.1 cgd }
639 1.1 cgd }
640 1.1 cgd tp->t_subt = TP(inptype(cp, &cp));
641 1.1 cgd break;
642 1.1 cgd case ENUM:
643 1.1 cgd tp->t_tspec = INT;
644 1.1 cgd tp->t_isenum = 1;
645 1.1 cgd /* FALLTHROUGH */
646 1.1 cgd case STRUCT:
647 1.1 cgd case UNION:
648 1.1 cgd switch (*cp++) {
649 1.1 cgd case '1':
650 1.1 cgd tp->t_istag = 1;
651 1.1 cgd tp->t_tag = hsearch(inpname(cp, &cp), 1);
652 1.1 cgd break;
653 1.1 cgd case '2':
654 1.1 cgd tp->t_istynam = 1;
655 1.1 cgd tp->t_tynam = hsearch(inpname(cp, &cp), 1);
656 1.1 cgd break;
657 1.3 cgd case '3':
658 1.3 cgd tp->t_isuniqpos = 1;
659 1.3 cgd tp->t_uniqpos.p_line = strtol(cp, &eptr, 10);
660 1.3 cgd cp = eptr;
661 1.3 cgd cp++;
662 1.3 cgd /* xlate to 'global' file name. */
663 1.3 cgd tp->t_uniqpos.p_file =
664 1.3 cgd addoutfile(inpfns[strtol(cp, &eptr, 10)]);
665 1.3 cgd cp = eptr;
666 1.3 cgd cp++;
667 1.3 cgd tp->t_uniqpos.p_uniq = strtol(cp, &eptr, 10);
668 1.3 cgd cp = eptr;
669 1.3 cgd break;
670 1.1 cgd }
671 1.1 cgd break;
672 1.1 cgd /* LINTED (enumeration value(s) not handled in switch) */
673 1.5 christos case LONG:
674 1.5 christos case VOID:
675 1.5 christos case LDOUBLE:
676 1.5 christos case DOUBLE:
677 1.5 christos case FLOAT:
678 1.5 christos case UQUAD:
679 1.5 christos case QUAD:
680 1.5 christos case ULONG:
681 1.5 christos case UINT:
682 1.5 christos case INT:
683 1.5 christos case USHORT:
684 1.5 christos case SHORT:
685 1.5 christos case UCHAR:
686 1.5 christos case SCHAR:
687 1.5 christos case CHAR:
688 1.5 christos case UNSIGN:
689 1.5 christos case SIGNED:
690 1.5 christos case NOTSPEC:
691 1.5 christos break;
692 1.1 cgd }
693 1.1 cgd
694 1.1 cgd *epp = cp;
695 1.1 cgd return (tidx);
696 1.1 cgd }
697 1.1 cgd
698 1.1 cgd /*
699 1.1 cgd * Get the length of a type string.
700 1.1 cgd */
701 1.1 cgd static int
702 1.9 lukem gettlen(const char *cp, const char **epp)
703 1.1 cgd {
704 1.1 cgd const char *cp1;
705 1.1 cgd char c, s, *eptr;
706 1.1 cgd tspec_t t;
707 1.1 cgd int narg, i, cm, vm;
708 1.1 cgd
709 1.1 cgd cp1 = cp;
710 1.1 cgd
711 1.1 cgd c = *cp++;
712 1.1 cgd
713 1.1 cgd cm = vm = 0;
714 1.1 cgd
715 1.1 cgd while (c == 'c' || c == 'v') {
716 1.1 cgd if (c == 'c') {
717 1.1 cgd if (cm)
718 1.1 cgd inperr();
719 1.1 cgd cm = 1;
720 1.1 cgd } else {
721 1.1 cgd if (vm)
722 1.1 cgd inperr();
723 1.1 cgd vm = 1;
724 1.1 cgd }
725 1.1 cgd c = *cp++;
726 1.1 cgd }
727 1.1 cgd
728 1.1 cgd if (c == 's' || c == 'u' || c == 'l' || c == 'e') {
729 1.1 cgd s = c;
730 1.1 cgd c = *cp++;
731 1.1 cgd } else {
732 1.1 cgd s = '\0';
733 1.1 cgd }
734 1.1 cgd
735 1.1 cgd t = NOTSPEC;
736 1.1 cgd
737 1.1 cgd switch (c) {
738 1.1 cgd case 'C':
739 1.1 cgd if (s == 's') {
740 1.1 cgd t = SCHAR;
741 1.1 cgd } else if (s == 'u') {
742 1.1 cgd t = UCHAR;
743 1.1 cgd } else if (s == '\0') {
744 1.1 cgd t = CHAR;
745 1.1 cgd }
746 1.1 cgd break;
747 1.1 cgd case 'S':
748 1.1 cgd if (s == 'u') {
749 1.1 cgd t = USHORT;
750 1.1 cgd } else if (s == '\0') {
751 1.1 cgd t = SHORT;
752 1.1 cgd }
753 1.1 cgd break;
754 1.1 cgd case 'I':
755 1.1 cgd if (s == 'u') {
756 1.1 cgd t = UINT;
757 1.1 cgd } else if (s == '\0') {
758 1.1 cgd t = INT;
759 1.1 cgd }
760 1.1 cgd break;
761 1.1 cgd case 'L':
762 1.1 cgd if (s == 'u') {
763 1.1 cgd t = ULONG;
764 1.1 cgd } else if (s == '\0') {
765 1.1 cgd t = LONG;
766 1.1 cgd }
767 1.1 cgd break;
768 1.1 cgd case 'Q':
769 1.1 cgd if (s == 'u') {
770 1.1 cgd t = UQUAD;
771 1.1 cgd } else if (s == '\0') {
772 1.1 cgd t = QUAD;
773 1.1 cgd }
774 1.1 cgd break;
775 1.1 cgd case 'D':
776 1.1 cgd if (s == 's') {
777 1.1 cgd t = FLOAT;
778 1.1 cgd } else if (s == 'l') {
779 1.1 cgd t = LDOUBLE;
780 1.1 cgd } else if (s == '\0') {
781 1.1 cgd t = DOUBLE;
782 1.1 cgd }
783 1.1 cgd break;
784 1.1 cgd case 'V':
785 1.1 cgd if (s == '\0')
786 1.1 cgd t = VOID;
787 1.1 cgd break;
788 1.1 cgd case 'P':
789 1.1 cgd if (s == '\0')
790 1.1 cgd t = PTR;
791 1.1 cgd break;
792 1.1 cgd case 'A':
793 1.1 cgd if (s == '\0')
794 1.1 cgd t = ARRAY;
795 1.1 cgd break;
796 1.1 cgd case 'F':
797 1.1 cgd case 'f':
798 1.1 cgd if (s == '\0')
799 1.1 cgd t = FUNC;
800 1.1 cgd break;
801 1.1 cgd case 'T':
802 1.1 cgd if (s == 'e') {
803 1.1 cgd t = ENUM;
804 1.1 cgd } else if (s == 's') {
805 1.1 cgd t = STRUCT;
806 1.1 cgd } else if (s == 'u') {
807 1.1 cgd t = UNION;
808 1.1 cgd }
809 1.1 cgd break;
810 1.1 cgd default:
811 1.1 cgd inperr();
812 1.1 cgd }
813 1.1 cgd
814 1.1 cgd if (t == NOTSPEC)
815 1.1 cgd inperr();
816 1.1 cgd
817 1.1 cgd switch (t) {
818 1.1 cgd case ARRAY:
819 1.1 cgd (void)strtol(cp, &eptr, 10);
820 1.1 cgd if (cp == eptr)
821 1.1 cgd inperr();
822 1.1 cgd cp = eptr;
823 1.1 cgd (void)gettlen(cp, &cp);
824 1.1 cgd break;
825 1.1 cgd case PTR:
826 1.1 cgd (void)gettlen(cp, &cp);
827 1.1 cgd break;
828 1.1 cgd case FUNC:
829 1.1 cgd c = *cp;
830 1.1 cgd if (isdigit((u_char)c)) {
831 1.1 cgd narg = (int)strtol(cp, &eptr, 10);
832 1.1 cgd cp = eptr;
833 1.1 cgd for (i = 0; i < narg; i++) {
834 1.1 cgd if (i == narg - 1 && *cp == 'E') {
835 1.1 cgd cp++;
836 1.1 cgd } else {
837 1.1 cgd (void)gettlen(cp, &cp);
838 1.1 cgd }
839 1.1 cgd }
840 1.1 cgd }
841 1.1 cgd (void)gettlen(cp, &cp);
842 1.1 cgd break;
843 1.1 cgd case ENUM:
844 1.1 cgd case STRUCT:
845 1.1 cgd case UNION:
846 1.1 cgd switch (*cp++) {
847 1.1 cgd case '1':
848 1.1 cgd (void)inpname(cp, &cp);
849 1.1 cgd break;
850 1.1 cgd case '2':
851 1.1 cgd (void)inpname(cp, &cp);
852 1.3 cgd break;
853 1.3 cgd case '3':
854 1.3 cgd /* unique position: line.file.uniquifier */
855 1.3 cgd (void)strtol(cp, &eptr, 10);
856 1.3 cgd if (cp == eptr)
857 1.3 cgd inperr();
858 1.3 cgd cp = eptr;
859 1.3 cgd if (*cp++ != '.')
860 1.3 cgd inperr();
861 1.3 cgd (void)strtol(cp, &eptr, 10);
862 1.3 cgd if (cp == eptr)
863 1.3 cgd inperr();
864 1.3 cgd cp = eptr;
865 1.3 cgd if (*cp++ != '.')
866 1.3 cgd inperr();
867 1.3 cgd (void)strtol(cp, &eptr, 10);
868 1.3 cgd if (cp == eptr)
869 1.3 cgd inperr();
870 1.3 cgd cp = eptr;
871 1.1 cgd break;
872 1.1 cgd default:
873 1.1 cgd inperr();
874 1.1 cgd }
875 1.1 cgd break;
876 1.1 cgd /* LINTED (enumeration value(s) not handled in switch) */
877 1.5 christos case FLOAT:
878 1.5 christos case USHORT:
879 1.5 christos case SHORT:
880 1.5 christos case UCHAR:
881 1.5 christos case SCHAR:
882 1.5 christos case CHAR:
883 1.5 christos case UNSIGN:
884 1.5 christos case SIGNED:
885 1.5 christos case NOTSPEC:
886 1.5 christos case INT:
887 1.5 christos case UINT:
888 1.5 christos case DOUBLE:
889 1.5 christos case LDOUBLE:
890 1.5 christos case VOID:
891 1.5 christos case ULONG:
892 1.5 christos case QUAD:
893 1.5 christos case UQUAD:
894 1.5 christos case LONG:
895 1.5 christos break;
896 1.1 cgd }
897 1.1 cgd
898 1.1 cgd *epp = cp;
899 1.1 cgd return (cp - cp1);
900 1.1 cgd }
901 1.1 cgd
902 1.1 cgd /*
903 1.1 cgd * Search a type by it's type string.
904 1.1 cgd */
905 1.1 cgd static u_short
906 1.9 lukem findtype(const char *cp, size_t len, int h)
907 1.1 cgd {
908 1.1 cgd thtab_t *thte;
909 1.1 cgd
910 1.1 cgd for (thte = thtab[h]; thte != NULL; thte = thte->th_nxt) {
911 1.1 cgd if (strncmp(thte->th_name, cp, len) != 0)
912 1.1 cgd continue;
913 1.1 cgd if (thte->th_name[len] == '\0')
914 1.1 cgd return (thte->th_idx);
915 1.1 cgd }
916 1.1 cgd
917 1.1 cgd return (0);
918 1.1 cgd }
919 1.1 cgd
920 1.1 cgd /*
921 1.1 cgd * Store a type and it's type string so we can later share this type
922 1.1 cgd * if we read the same type string from the input file.
923 1.1 cgd */
924 1.1 cgd static u_short
925 1.9 lukem storetyp(type_t *tp, const char *cp, size_t len, int h)
926 1.1 cgd {
927 1.9 lukem static u_int tidx = 1; /* 0 is reserved */
928 1.1 cgd thtab_t *thte;
929 1.1 cgd char *name;
930 1.1 cgd
931 1.1 cgd if (tidx >= USHRT_MAX)
932 1.1 cgd errx(1, "sorry, too many types");
933 1.1 cgd
934 1.1 cgd if (tidx == tlstlen - 1) {
935 1.1 cgd tlst = xrealloc(tlst, (tlstlen * 2) * sizeof (type_t *));
936 1.1 cgd (void)memset(tlst + tlstlen, 0, tlstlen * sizeof (type_t *));
937 1.1 cgd tlstlen *= 2;
938 1.1 cgd }
939 1.1 cgd
940 1.1 cgd tlst[tidx] = tp;
941 1.1 cgd
942 1.1 cgd /* create a hash table entry */
943 1.1 cgd name = xalloc(len + 1);
944 1.1 cgd (void)memcpy(name, cp, len);
945 1.1 cgd name[len] = '\0';
946 1.1 cgd
947 1.1 cgd thte = xalloc(sizeof (thtab_t));
948 1.1 cgd thte->th_name = name;
949 1.1 cgd thte->th_idx = tidx;
950 1.1 cgd thte->th_nxt = thtab[h];
951 1.1 cgd thtab[h] = thte;
952 1.1 cgd
953 1.1 cgd return ((u_short)tidx++);
954 1.1 cgd }
955 1.1 cgd
956 1.1 cgd /*
957 1.1 cgd * Hash function for types
958 1.1 cgd */
959 1.1 cgd static int
960 1.9 lukem thash(const char *s, size_t len)
961 1.1 cgd {
962 1.1 cgd u_int v;
963 1.1 cgd
964 1.1 cgd v = 0;
965 1.1 cgd while (len-- != 0) {
966 1.1 cgd v = (v << sizeof (v)) + (u_char)*s++;
967 1.1 cgd v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
968 1.1 cgd }
969 1.1 cgd return (v % THSHSIZ2);
970 1.1 cgd }
971 1.1 cgd
972 1.1 cgd /*
973 1.1 cgd * Read a string enclosed by "". This string may contain quoted chars.
974 1.1 cgd */
975 1.1 cgd static char *
976 1.9 lukem inpqstrg(const char *src, const char **epp)
977 1.1 cgd {
978 1.1 cgd char *strg, *dst;
979 1.1 cgd size_t slen;
980 1.1 cgd int c;
981 1.1 cgd int v;
982 1.1 cgd
983 1.1 cgd dst = strg = xmalloc(slen = 32);
984 1.1 cgd
985 1.1 cgd if ((c = *src++) != '"')
986 1.1 cgd inperr();
987 1.1 cgd if ((c = *src++) == '\0')
988 1.1 cgd inperr();
989 1.1 cgd
990 1.1 cgd while (c != '"') {
991 1.1 cgd if (c == '\\') {
992 1.1 cgd if ((c = *src++) == '\0')
993 1.1 cgd inperr();
994 1.1 cgd switch (c) {
995 1.1 cgd case 'n':
996 1.1 cgd c = '\n';
997 1.1 cgd break;
998 1.1 cgd case 't':
999 1.1 cgd c = '\t';
1000 1.1 cgd break;
1001 1.1 cgd case 'v':
1002 1.1 cgd c = '\v';
1003 1.1 cgd break;
1004 1.1 cgd case 'b':
1005 1.1 cgd c = '\b';
1006 1.1 cgd break;
1007 1.1 cgd case 'r':
1008 1.1 cgd c = '\r';
1009 1.1 cgd break;
1010 1.1 cgd case 'f':
1011 1.1 cgd c = '\f';
1012 1.1 cgd break;
1013 1.1 cgd case 'a':
1014 1.1 cgd c = '\a';
1015 1.1 cgd break;
1016 1.1 cgd case '\\':
1017 1.1 cgd c = '\\';
1018 1.1 cgd break;
1019 1.1 cgd case '"':
1020 1.1 cgd c = '"';
1021 1.1 cgd break;
1022 1.1 cgd case '\'':
1023 1.1 cgd c = '\'';
1024 1.1 cgd break;
1025 1.1 cgd case '0': case '1': case '2': case '3':
1026 1.1 cgd v = (c - '0') << 6;
1027 1.1 cgd if ((c = *src++) < '0' || c > '7')
1028 1.1 cgd inperr();
1029 1.1 cgd v |= (c - '0') << 3;
1030 1.1 cgd if ((c = *src++) < '0' || c > '7')
1031 1.1 cgd inperr();
1032 1.1 cgd v |= c - '0';
1033 1.1 cgd c = (u_char)v;
1034 1.1 cgd break;
1035 1.1 cgd default:
1036 1.1 cgd inperr();
1037 1.1 cgd }
1038 1.1 cgd }
1039 1.1 cgd /* keep space for trailing '\0' */
1040 1.1 cgd if (dst - strg == slen - 1) {
1041 1.1 cgd strg = xrealloc(strg, slen * 2);
1042 1.1 cgd dst = strg + (slen - 1);
1043 1.1 cgd slen *= 2;
1044 1.1 cgd }
1045 1.1 cgd *dst++ = (char)c;
1046 1.1 cgd if ((c = *src++) == '\0')
1047 1.1 cgd inperr();
1048 1.1 cgd }
1049 1.1 cgd *dst = '\0';
1050 1.1 cgd
1051 1.1 cgd *epp = src;
1052 1.1 cgd return (strg);
1053 1.1 cgd }
1054 1.1 cgd
1055 1.1 cgd /*
1056 1.1 cgd * Read the name of a symbol in static memory.
1057 1.1 cgd */
1058 1.1 cgd static const char *
1059 1.9 lukem inpname(const char *cp, const char **epp)
1060 1.1 cgd {
1061 1.1 cgd static char *buf;
1062 1.1 cgd static size_t blen = 0;
1063 1.1 cgd size_t len, i;
1064 1.1 cgd char *eptr, c;
1065 1.1 cgd
1066 1.1 cgd len = (int)strtol(cp, &eptr, 10);
1067 1.1 cgd if (cp == eptr)
1068 1.1 cgd inperr();
1069 1.1 cgd cp = eptr;
1070 1.1 cgd if (len + 1 > blen)
1071 1.1 cgd buf = xrealloc(buf, blen = len + 1);
1072 1.1 cgd for (i = 0; i < len; i++) {
1073 1.1 cgd c = *cp++;
1074 1.7 christos if (!isalnum((unsigned char)c) && c != '_')
1075 1.1 cgd inperr();
1076 1.1 cgd buf[i] = c;
1077 1.1 cgd }
1078 1.1 cgd buf[i] = '\0';
1079 1.1 cgd
1080 1.1 cgd *epp = cp;
1081 1.1 cgd return (buf);
1082 1.1 cgd }
1083 1.1 cgd
1084 1.1 cgd /*
1085 1.1 cgd * Return the index of a file name. If the name cannot be found, create
1086 1.1 cgd * a new entry and return the index of the newly created entry.
1087 1.1 cgd */
1088 1.1 cgd static int
1089 1.9 lukem getfnidx(const char *fn)
1090 1.1 cgd {
1091 1.1 cgd int i;
1092 1.1 cgd
1093 1.1 cgd /* 0 ist reserved */
1094 1.1 cgd for (i = 1; fnames[i] != NULL; i++) {
1095 1.1 cgd if (strcmp(fnames[i], fn) == 0)
1096 1.1 cgd break;
1097 1.1 cgd }
1098 1.1 cgd if (fnames[i] != NULL)
1099 1.1 cgd return (i);
1100 1.1 cgd
1101 1.1 cgd if (i == nfnames - 1) {
1102 1.1 cgd fnames = xrealloc(fnames, (nfnames * 2) * sizeof (char *));
1103 1.1 cgd (void)memset(fnames + nfnames, 0, nfnames * sizeof (char *));
1104 1.1 cgd nfnames *= 2;
1105 1.1 cgd }
1106 1.1 cgd
1107 1.1 cgd fnames[i] = xstrdup(fn);
1108 1.1 cgd return (i);
1109 1.1 cgd }
1110 1.1 cgd
1111 1.1 cgd /*
1112 1.1 cgd * Separate symbols with static and external linkage.
1113 1.1 cgd */
1114 1.1 cgd void
1115 1.9 lukem mkstatic(hte_t *hte)
1116 1.1 cgd {
1117 1.1 cgd sym_t *sym1, **symp, *sym;
1118 1.1 cgd fcall_t **callp, *call;
1119 1.1 cgd usym_t **usymp, *usym;
1120 1.1 cgd hte_t *nhte;
1121 1.1 cgd int ofnd;
1122 1.1 cgd
1123 1.1 cgd /* Look for first static definition */
1124 1.1 cgd for (sym1 = hte->h_syms; sym1 != NULL; sym1 = sym1->s_nxt) {
1125 1.1 cgd if (sym1->s_static)
1126 1.1 cgd break;
1127 1.1 cgd }
1128 1.1 cgd if (sym1 == NULL)
1129 1.1 cgd return;
1130 1.1 cgd
1131 1.1 cgd /* Do nothing if this name is used only in one translation unit. */
1132 1.1 cgd ofnd = 0;
1133 1.1 cgd for (sym = hte->h_syms; sym != NULL && !ofnd; sym = sym->s_nxt) {
1134 1.1 cgd if (sym->s_pos.p_src != sym1->s_pos.p_src)
1135 1.1 cgd ofnd = 1;
1136 1.1 cgd }
1137 1.1 cgd for (call = hte->h_calls; call != NULL && !ofnd; call = call->f_nxt) {
1138 1.1 cgd if (call->f_pos.p_src != sym1->s_pos.p_src)
1139 1.1 cgd ofnd = 1;
1140 1.1 cgd }
1141 1.1 cgd for (usym = hte->h_usyms; usym != NULL && !ofnd; usym = usym->u_nxt) {
1142 1.1 cgd if (usym->u_pos.p_src != sym1->s_pos.p_src)
1143 1.1 cgd ofnd = 1;
1144 1.1 cgd }
1145 1.1 cgd if (!ofnd) {
1146 1.1 cgd hte->h_used = 1;
1147 1.1 cgd /* errors about undef. static symbols are printed in lint1 */
1148 1.1 cgd hte->h_def = 1;
1149 1.1 cgd hte->h_static = 1;
1150 1.1 cgd return;
1151 1.1 cgd }
1152 1.1 cgd
1153 1.1 cgd /*
1154 1.1 cgd * Create a new hash table entry
1155 1.1 cgd *
1156 1.1 cgd * XXX this entry should be put at the beginning of the list to
1157 1.1 cgd * avoid to process the same symbol twice.
1158 1.1 cgd */
1159 1.9 lukem for (nhte = hte; nhte->h_link != NULL; nhte = nhte->h_link)
1160 1.9 lukem continue;
1161 1.4 cgd nhte->h_link = xmalloc(sizeof (hte_t));
1162 1.1 cgd nhte = nhte->h_link;
1163 1.1 cgd nhte->h_name = hte->h_name;
1164 1.1 cgd nhte->h_used = 1;
1165 1.1 cgd nhte->h_def = 1; /* error in lint1 */
1166 1.6 mycroft nhte->h_static = 1;
1167 1.6 mycroft nhte->h_syms = NULL;
1168 1.1 cgd nhte->h_lsym = &nhte->h_syms;
1169 1.6 mycroft nhte->h_calls = NULL;
1170 1.1 cgd nhte->h_lcall = &nhte->h_calls;
1171 1.6 mycroft nhte->h_usyms = NULL;
1172 1.1 cgd nhte->h_lusym = &nhte->h_usyms;
1173 1.6 mycroft nhte->h_link = NULL;
1174 1.6 mycroft nhte->h_hte = NULL;
1175 1.1 cgd
1176 1.1 cgd /*
1177 1.1 cgd * move all symbols used in this translation unit into the new
1178 1.1 cgd * hash table entry.
1179 1.1 cgd */
1180 1.1 cgd for (symp = &hte->h_syms; (sym = *symp) != NULL; ) {
1181 1.1 cgd if (sym->s_pos.p_src == sym1->s_pos.p_src) {
1182 1.1 cgd sym->s_static = 1;
1183 1.1 cgd (*symp) = sym->s_nxt;
1184 1.1 cgd if (hte->h_lsym == &sym->s_nxt)
1185 1.1 cgd hte->h_lsym = symp;
1186 1.1 cgd sym->s_nxt = NULL;
1187 1.1 cgd *nhte->h_lsym = sym;
1188 1.1 cgd nhte->h_lsym = &sym->s_nxt;
1189 1.1 cgd } else {
1190 1.1 cgd symp = &sym->s_nxt;
1191 1.1 cgd }
1192 1.1 cgd }
1193 1.1 cgd for (callp = &hte->h_calls; (call = *callp) != NULL; ) {
1194 1.1 cgd if (call->f_pos.p_src == sym1->s_pos.p_src) {
1195 1.1 cgd (*callp) = call->f_nxt;
1196 1.1 cgd if (hte->h_lcall == &call->f_nxt)
1197 1.1 cgd hte->h_lcall = callp;
1198 1.1 cgd call->f_nxt = NULL;
1199 1.1 cgd *nhte->h_lcall = call;
1200 1.1 cgd nhte->h_lcall = &call->f_nxt;
1201 1.1 cgd } else {
1202 1.1 cgd callp = &call->f_nxt;
1203 1.1 cgd }
1204 1.1 cgd }
1205 1.1 cgd for (usymp = &hte->h_usyms; (usym = *usymp) != NULL; ) {
1206 1.1 cgd if (usym->u_pos.p_src == sym1->s_pos.p_src) {
1207 1.1 cgd (*usymp) = usym->u_nxt;
1208 1.1 cgd if (hte->h_lusym == &usym->u_nxt)
1209 1.1 cgd hte->h_lusym = usymp;
1210 1.1 cgd usym->u_nxt = NULL;
1211 1.1 cgd *nhte->h_lusym = usym;
1212 1.1 cgd nhte->h_lusym = &usym->u_nxt;
1213 1.1 cgd } else {
1214 1.1 cgd usymp = &usym->u_nxt;
1215 1.1 cgd }
1216 1.1 cgd }
1217 1.1 cgd
1218 1.1 cgd /* h_def must be recalculated for old hte */
1219 1.1 cgd hte->h_def = nhte->h_def = 0;
1220 1.1 cgd for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
1221 1.1 cgd if (sym->s_def == DEF || sym->s_def == TDEF) {
1222 1.1 cgd hte->h_def = 1;
1223 1.1 cgd break;
1224 1.1 cgd }
1225 1.1 cgd }
1226 1.1 cgd
1227 1.1 cgd mkstatic(hte);
1228 1.1 cgd }
1229