xref: /original-bsd/usr.bin/pascal/pdx/sym/which.c (revision 1a56dd2c)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #ifndef lint
8 static char sccsid[] = "@(#)which.c	5.1 (Berkeley) 06/06/85";
9 #endif not lint
10 
11 /*
12  * Routines to distinguish symbols of the same name.
13  */
14 
15 #include "defs.h"
16 #include "sym.h"
17 #include "classes.h"
18 #include "symtab.h"
19 #include "mappings.h"
20 #include "machine.h"
21 #include "sym.rep"
22 
23 /*
24  * Figure out the "current" symbol being referred to,
25  * this is either the active one or the most visible from the
26  * current scope.
27  *
28  * Fields are purposely ignored; these can be gotten to via "findclass".
29  */
30 
31 SYM *which(s)
32 SYM *s;
33 {
34 	register SYM *p, *t, *f;
35 
36 	if (s == program || isbuiltin(s)) {
37 		return(s);
38 	}
39 	if (!isactive(program)) {
40 		f = program;
41 	} else {
42 		f = whatblock(pc);
43 		if (f == NIL) {
44 			panic("no block for addr 0x%x", pc);
45 		}
46 	}
47 	for (p = f; p != NIL; p = p->func) {
48 		if ((t = findsym(s, p)) != NIL) {
49 			break;
50 		}
51 	}
52 	if (t == NIL) {
53 		error("\"%s\" is not known in \"%s\"", s->symbol, f->symbol);
54 	}
55 	return(t);
56 }
57 
58 /*
59  * Find a (non-field) symbol with name s->symbol belonging to block f.
60  *
61  * Parameters to the main program are purposely "not found" because
62  * pi gives them no type.
63  */
64 
65 SYM *findsym(s, f)
66 SYM *s;
67 SYM *f;
68 {
69 	register SYM *t;
70 
71 	if (!isblock(f)) {
72 		error("%s is not a block", f->symbol);
73 	}
74 	for (t = s; t != NIL; t = t->next_sym) {
75 		if (t->func == f && !(f == program && isparam(t)) &&
76 		  t->class != FIELD && streq(t->symbol, s->symbol)) {
77 			break;
78 		}
79 	}
80 	return(t);
81 }
82 
83 /*
84  * Find the symbol which is has the same name and scope as the
85  * given symbol but is of the given field.  Return NIL if there is none.
86  */
87 
88 SYM *findclass(s, cl)
89 SYM *s;
90 char cl;
91 {
92 	register SYM *t;
93 
94 	if (s->class == cl) {
95 		return(s);
96 	}
97 	t = st_lookup(symtab, s->symbol);
98 	while (t != NIL && (t->class != cl || t->func != s->func ||
99 	  !streq(s->symbol, t->symbol))) {
100 		t = t->next_sym;
101 	}
102 	return(t);
103 }
104