1e6606823Sbostic /*-
2*d5ae9fceSbostic  * Copyright (c) 1980, 1993
3*d5ae9fceSbostic  *	The Regents of the University of California.  All rights reserved.
4e6606823Sbostic  *
5e6606823Sbostic  * %sccs.include.redist.c%
659d23892Sdist  */
7c9677cbaSlinton 
859d23892Sdist #ifndef lint
9*d5ae9fceSbostic static char sccsid[] = "@(#)predicates.c	8.1 (Berkeley) 06/06/93";
10e6606823Sbostic #endif /* not lint */
11e6606823Sbostic 
12c9677cbaSlinton /*
13c9677cbaSlinton  * The basic tests on a symbol.
14c9677cbaSlinton  */
15c9677cbaSlinton 
16c9677cbaSlinton #include "defs.h"
17c9677cbaSlinton #include "sym.h"
18c9677cbaSlinton #include "symtab.h"
195aa7b387Slinton #include "btypes.h"
20c9677cbaSlinton #include "classes.h"
21c9677cbaSlinton #include "sym.rep"
22c9677cbaSlinton 
23c9677cbaSlinton /*
24c9677cbaSlinton  * Test if a symbol is a parameter.  This is true if there
25c9677cbaSlinton  * is a cycle from s->func to s via chain pointers.
26c9677cbaSlinton  */
27c9677cbaSlinton 
isparam(s)28c9677cbaSlinton BOOLEAN isparam(s)
29c9677cbaSlinton SYM *s;
30c9677cbaSlinton {
31c9677cbaSlinton     register SYM *t;
32c9677cbaSlinton 
33c9677cbaSlinton     for (t = s->func; t != NIL; t = t->chain) {
34c9677cbaSlinton 	if (t == s) {
35c9677cbaSlinton 	    return(TRUE);
36c9677cbaSlinton 	}
37c9677cbaSlinton     }
38c9677cbaSlinton     return(FALSE);
39c9677cbaSlinton }
40c9677cbaSlinton 
41c9677cbaSlinton /*
42c9677cbaSlinton  * Test if a symbol is a var parameter, i.e. has class REF.
43c9677cbaSlinton  */
44c9677cbaSlinton 
isvarparam(s)45c9677cbaSlinton BOOLEAN isvarparam(s)
46c9677cbaSlinton SYM *s;
47c9677cbaSlinton {
48c9677cbaSlinton     return (BOOLEAN) s->class == REF;
49c9677cbaSlinton }
50c9677cbaSlinton 
51c9677cbaSlinton /*
524f2e96f3Slinton  * Test if a symbol is a variable (actually any addressible quantity
534f2e96f3Slinton  * with do).
544f2e96f3Slinton  */
554f2e96f3Slinton 
isvariable(s)564f2e96f3Slinton BOOLEAN isvariable(s)
574f2e96f3Slinton SYM *s;
584f2e96f3Slinton {
594f2e96f3Slinton     return s->class == VAR || s->class == FVAR || s->class == REF;
604f2e96f3Slinton }
614f2e96f3Slinton 
624f2e96f3Slinton /*
63c9677cbaSlinton  * Test if a symbol is a block, e.g. function, procedure, or the
64c9677cbaSlinton  * main program.
65c9677cbaSlinton  */
66c9677cbaSlinton 
isblock(s)67c9677cbaSlinton BOOLEAN isblock(s)
68c9677cbaSlinton register SYM *s;
69c9677cbaSlinton {
70c9677cbaSlinton     return(s->class == FUNC || s->class == PROC || s->class == PROG);
71c9677cbaSlinton }
72c9677cbaSlinton 
73c9677cbaSlinton /*
74c9677cbaSlinton  * Test if a symbol is builtin, that is, a predefined type or
75c9677cbaSlinton  * reserved word.
76c9677cbaSlinton  */
77c9677cbaSlinton 
isbuiltin(s)78c9677cbaSlinton BOOLEAN isbuiltin(s)
79c9677cbaSlinton SYM *s;
80c9677cbaSlinton {
81c9677cbaSlinton     return(s->blkno == 0 && s->class != PROG && s->class != VAR);
82c9677cbaSlinton }
83c9677cbaSlinton 
84c9677cbaSlinton /*
85c9677cbaSlinton  * Compatible tests if two types are compatible.  The issue
86c9677cbaSlinton  * is complicated a bit by ranges.
87c9677cbaSlinton  *
88c9677cbaSlinton  * Integers and reals are not compatible since they cannot always be mixed.
89c9677cbaSlinton  */
90c9677cbaSlinton 
compatible(t1,t2)91c9677cbaSlinton BOOLEAN compatible(t1, t2)
92c9677cbaSlinton register SYM *t1, *t2;
93c9677cbaSlinton {
9467ed027bSlinton     register BOOLEAN b;
9567ed027bSlinton 
9667ed027bSlinton     if (isvariable(t1)) {
9767ed027bSlinton 	t1 = t1->type;
98c9677cbaSlinton     }
9967ed027bSlinton     if (isvariable(t2)) {
10067ed027bSlinton 	t2 = t2->type;
10167ed027bSlinton     }
10267ed027bSlinton     if (t1 == t2) {
10367ed027bSlinton 	b = TRUE;
10467ed027bSlinton     } else {
105c9677cbaSlinton 	t1 = rtype(t1);
106c9677cbaSlinton 	t2 = rtype(t2);
107c9677cbaSlinton 	if (t1->type == t2->type) {
108c9677cbaSlinton 	    if (t1->class == RANGE && t2->class == RANGE) {
10967ed027bSlinton 		b = TRUE;
11067ed027bSlinton 	    } else if ((t1->class == SCAL || t1->class == CONST) &&
111c9677cbaSlinton 	      (t2->class == SCAL || t2->class == CONST)) {
11267ed027bSlinton 		b = TRUE;
11367ed027bSlinton 	    } else if (t1->type == t_char &&
11467ed027bSlinton 	      t1->class == ARRAY && t2->class == ARRAY) {
11567ed027bSlinton 		b = TRUE;
11667ed027bSlinton 	    } else {
11767ed027bSlinton 		b = FALSE;
118c9677cbaSlinton 	    }
119c9677cbaSlinton     /*
120c9677cbaSlinton      * A kludge here for "nil".  Should be handled better.
121c9677cbaSlinton      * Opens a pandora's box for integer/pointer compatibility.
122c9677cbaSlinton      */
12367ed027bSlinton 	} else if ((t1->class == RANGE && t2->class == PTR) ||
12467ed027bSlinton 	  (t2->class == RANGE && t1->class == PTR)) {
12567ed027bSlinton 	    b = TRUE;
12667ed027bSlinton 	} else {
12767ed027bSlinton 	    b = FALSE;
128c9677cbaSlinton 	}
129c9677cbaSlinton     }
13067ed027bSlinton     return b;
131c9677cbaSlinton }
132c9677cbaSlinton 
133c9677cbaSlinton /*
134c9677cbaSlinton  * Predicate to test if a symbol should be printed.  We don't print
135c9677cbaSlinton  * files, for example, simply because there's no good way to do it.
136c9677cbaSlinton  * The symbol must be within the given function.
137c9677cbaSlinton  */
138c9677cbaSlinton 
should_print(s,f)139c9677cbaSlinton BOOLEAN should_print(s, f)
140c9677cbaSlinton SYM *s;
141c9677cbaSlinton SYM *f;
142c9677cbaSlinton {
143c9677cbaSlinton     SYM *t;
144c9677cbaSlinton 
145c9677cbaSlinton     if (s->func != f || (s->class != VAR && s->class != FVAR)) {
146c9677cbaSlinton 	return(FALSE);
147c9677cbaSlinton     } else if (s->chain != NIL) {
148c9677cbaSlinton 	return(FALSE);
149c9677cbaSlinton     } else {
150c9677cbaSlinton 	t = rtype(s->type);
1515aa7b387Slinton 	if (t == NIL || t->class == FILET || t->class == SET) {
152c9677cbaSlinton 	    return(FALSE);
153c9677cbaSlinton 	} else {
154c9677cbaSlinton 	    return(TRUE);
155c9677cbaSlinton 	}
156c9677cbaSlinton     }
157c9677cbaSlinton }
158c9677cbaSlinton 
159c9677cbaSlinton /*
160c9677cbaSlinton  * Test if the name of a symbol is uniquely defined or not.
161c9677cbaSlinton  */
162c9677cbaSlinton 
isambiguous(s)163c9677cbaSlinton BOOLEAN isambiguous(s)
164c9677cbaSlinton SYM *s;
165c9677cbaSlinton {
166c9677cbaSlinton     SYM *t;
167c9677cbaSlinton 
168c9677cbaSlinton     t = st_lookup(symtab, s->symbol);
169c9677cbaSlinton     if (t == NIL) {
170c9677cbaSlinton 	panic("symbol name vanished");
171c9677cbaSlinton     }
172c9677cbaSlinton     while (t != NIL && (s == t || !streq(t->symbol, s->symbol))) {
173c9677cbaSlinton 	t = t->next_sym;
174c9677cbaSlinton     }
175c9677cbaSlinton     return t != NIL;
176c9677cbaSlinton }
177