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