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