1 /* Copyright (c) 1982 Regents of the University of California */
2 
3 static char sccsid[] = "@(#)predicates.c 1.2 01/19/82";
4 
5 /*
6  * The basic tests on a symbol.
7  */
8 
9 #include "defs.h"
10 #include "sym.h"
11 #include "symtab.h"
12 #include "btypes.h"
13 #include "classes.h"
14 #include "sym.rep"
15 
16 /*
17  * Test if a symbol is a parameter.  This is true if there
18  * is a cycle from s->func to s via chain pointers.
19  */
20 
21 BOOLEAN isparam(s)
22 SYM *s;
23 {
24 	register SYM *t;
25 
26 	for (t = s->func; t != NIL; t = t->chain) {
27 		if (t == s) {
28 			return(TRUE);
29 		}
30 	}
31 	return(FALSE);
32 }
33 
34 /*
35  * Test if a symbol is a var parameter, i.e. has class REF.
36  */
37 
38 BOOLEAN isvarparam(s)
39 SYM *s;
40 {
41 	return (BOOLEAN) s->class == REF;
42 }
43 
44 /*
45  * Test if a symbol is a block, e.g. function, procedure, or the
46  * main program.
47  */
48 
49 BOOLEAN isblock(s)
50 register SYM *s;
51 {
52 	return(s->class == FUNC || s->class == PROC || s->class == PROG);
53 }
54 
55 /*
56  * Test if a symbol is builtin, that is, a predefined type or
57  * reserved word.
58  */
59 
60 BOOLEAN isbuiltin(s)
61 SYM *s;
62 {
63 	return(s->blkno == 0 && s->class != PROG && s->class != VAR);
64 }
65 
66 /*
67  * Compatible tests if two types are compatible.  The issue
68  * is complicated a bit by ranges.
69  *
70  * Integers and reals are not compatible since they cannot always be mixed.
71  */
72 
73 BOOLEAN compatible(t1, t2)
74 register SYM *t1, *t2;
75 {
76 	if (t1 == t2) {
77 		return(TRUE);
78 	}
79 	t1 = rtype(t1);
80 	t2 = rtype(t2);
81 	if (t1->type == t2->type) {
82 		if (t1->class == RANGE && t2->class == RANGE) {
83 			return TRUE;
84 		}
85 		if ((t1->class == SCAL || t1->class == CONST) &&
86 		  (t2->class == SCAL || t2->class == CONST)) {
87 			return TRUE;
88 		}
89 		if (t1->type == t_char && t1->class == ARRAY && t2->class == ARRAY) {
90 			return TRUE;
91 		}
92 	}
93 /*
94  * A kludge here for "nil".  Should be handled better.
95  * Opens a pandora's box for integer/pointer compatibility.
96  */
97 	if (t1->class == RANGE && t2->class == PTR) {
98 		return TRUE;
99 	}
100 	if (t2->class == RANGE && t1->class == PTR) {
101 		return TRUE;
102 	}
103 	return(FALSE);
104 }
105 
106 /*
107  * Predicate to test if a symbol should be printed.  We don't print
108  * files, for example, simply because there's no good way to do it.
109  * The symbol must be within the given function.
110  */
111 
112 BOOLEAN should_print(s, f)
113 SYM *s;
114 SYM *f;
115 {
116 	SYM *t;
117 
118 	if (s->func != f || (s->class != VAR && s->class != FVAR)) {
119 		return(FALSE);
120 	} else if (s->chain != NIL) {
121 		return(FALSE);
122 	} else {
123 		t = rtype(s->type);
124 		if (t == NIL || t->class == FILET || t->class == SET) {
125 			return(FALSE);
126 		} else {
127 			return(TRUE);
128 		}
129 	}
130 }
131 
132 /*
133  * Test if the name of a symbol is uniquely defined or not.
134  */
135 
136 BOOLEAN isambiguous(s)
137 SYM *s;
138 {
139 	SYM *t;
140 
141 	t = st_lookup(symtab, s->symbol);
142 	if (t == NIL) {
143 		panic("symbol name vanished");
144 	}
145 	while (t != NIL && (s == t || !streq(t->symbol, s->symbol))) {
146 		t = t->next_sym;
147 	}
148 	return t != NIL;
149 }
150