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