15527Slinton /* Copyright (c) 1982 Regents of the University of California */
25527Slinton 
3*5885Slinton static char sccsid[] = "@(#)predicates.c 1.3 02/17/82";
45527Slinton 
55527Slinton /*
65527Slinton  * The basic tests on a symbol.
75527Slinton  */
85527Slinton 
95527Slinton #include "defs.h"
105527Slinton #include "sym.h"
115527Slinton #include "symtab.h"
125564Slinton #include "btypes.h"
135527Slinton #include "classes.h"
145527Slinton #include "sym.rep"
155527Slinton 
165527Slinton /*
175527Slinton  * Test if a symbol is a parameter.  This is true if there
185527Slinton  * is a cycle from s->func to s via chain pointers.
195527Slinton  */
205527Slinton 
215527Slinton BOOLEAN isparam(s)
225527Slinton SYM *s;
235527Slinton {
24*5885Slinton     register SYM *t;
255527Slinton 
26*5885Slinton     for (t = s->func; t != NIL; t = t->chain) {
27*5885Slinton 	if (t == s) {
28*5885Slinton 	    return(TRUE);
295527Slinton 	}
30*5885Slinton     }
31*5885Slinton     return(FALSE);
325527Slinton }
335527Slinton 
345527Slinton /*
355527Slinton  * Test if a symbol is a var parameter, i.e. has class REF.
365527Slinton  */
375527Slinton 
385527Slinton BOOLEAN isvarparam(s)
395527Slinton SYM *s;
405527Slinton {
41*5885Slinton     return (BOOLEAN) s->class == REF;
425527Slinton }
435527Slinton 
445527Slinton /*
45*5885Slinton  * Test if a symbol is a variable (actually any addressible quantity
46*5885Slinton  * with do).
47*5885Slinton  */
48*5885Slinton 
49*5885Slinton BOOLEAN isvariable(s)
50*5885Slinton SYM *s;
51*5885Slinton {
52*5885Slinton     return s->class == VAR || s->class == FVAR || s->class == REF;
53*5885Slinton }
54*5885Slinton 
55*5885Slinton /*
565527Slinton  * Test if a symbol is a block, e.g. function, procedure, or the
575527Slinton  * main program.
585527Slinton  */
595527Slinton 
605527Slinton BOOLEAN isblock(s)
615527Slinton register SYM *s;
625527Slinton {
63*5885Slinton     return(s->class == FUNC || s->class == PROC || s->class == PROG);
645527Slinton }
655527Slinton 
665527Slinton /*
675527Slinton  * Test if a symbol is builtin, that is, a predefined type or
685527Slinton  * reserved word.
695527Slinton  */
705527Slinton 
715527Slinton BOOLEAN isbuiltin(s)
725527Slinton SYM *s;
735527Slinton {
74*5885Slinton     return(s->blkno == 0 && s->class != PROG && s->class != VAR);
755527Slinton }
765527Slinton 
775527Slinton /*
785527Slinton  * Compatible tests if two types are compatible.  The issue
795527Slinton  * is complicated a bit by ranges.
805527Slinton  *
815527Slinton  * Integers and reals are not compatible since they cannot always be mixed.
825527Slinton  */
835527Slinton 
845527Slinton BOOLEAN compatible(t1, t2)
855527Slinton register SYM *t1, *t2;
865527Slinton {
87*5885Slinton     if (t1 == t2) {
88*5885Slinton 	return(TRUE);
89*5885Slinton     }
90*5885Slinton     t1 = rtype(t1);
91*5885Slinton     t2 = rtype(t2);
92*5885Slinton     if (t1->type == t2->type) {
93*5885Slinton 	if (t1->class == RANGE && t2->class == RANGE) {
94*5885Slinton 	    return TRUE;
955527Slinton 	}
96*5885Slinton 	if ((t1->class == SCAL || t1->class == CONST) &&
97*5885Slinton 	  (t2->class == SCAL || t2->class == CONST)) {
98*5885Slinton 	    return TRUE;
995527Slinton 	}
100*5885Slinton 	if (t1->type == t_char && t1->class == ARRAY && t2->class == ARRAY) {
101*5885Slinton 	    return TRUE;
102*5885Slinton 	}
103*5885Slinton     }
1045527Slinton /*
1055527Slinton  * A kludge here for "nil".  Should be handled better.
1065527Slinton  * Opens a pandora's box for integer/pointer compatibility.
1075527Slinton  */
108*5885Slinton     if (t1->class == RANGE && t2->class == PTR) {
109*5885Slinton 	return TRUE;
110*5885Slinton     }
111*5885Slinton     if (t2->class == RANGE && t1->class == PTR) {
112*5885Slinton 	return TRUE;
113*5885Slinton     }
114*5885Slinton     return(FALSE);
1155527Slinton }
1165527Slinton 
1175527Slinton /*
1185527Slinton  * Predicate to test if a symbol should be printed.  We don't print
1195527Slinton  * files, for example, simply because there's no good way to do it.
1205527Slinton  * The symbol must be within the given function.
1215527Slinton  */
1225527Slinton 
1235527Slinton BOOLEAN should_print(s, f)
1245527Slinton SYM *s;
1255527Slinton SYM *f;
1265527Slinton {
127*5885Slinton     SYM *t;
1285527Slinton 
129*5885Slinton     if (s->func != f || (s->class != VAR && s->class != FVAR)) {
130*5885Slinton 	return(FALSE);
131*5885Slinton     } else if (s->chain != NIL) {
132*5885Slinton 	return(FALSE);
133*5885Slinton     } else {
134*5885Slinton 	t = rtype(s->type);
135*5885Slinton 	if (t == NIL || t->class == FILET || t->class == SET) {
136*5885Slinton 	    return(FALSE);
1375527Slinton 	} else {
138*5885Slinton 	    return(TRUE);
1395527Slinton 	}
140*5885Slinton     }
1415527Slinton }
1425527Slinton 
1435527Slinton /*
1445527Slinton  * Test if the name of a symbol is uniquely defined or not.
1455527Slinton  */
1465527Slinton 
1475527Slinton BOOLEAN isambiguous(s)
1485527Slinton SYM *s;
1495527Slinton {
150*5885Slinton     SYM *t;
1515527Slinton 
152*5885Slinton     t = st_lookup(symtab, s->symbol);
153*5885Slinton     if (t == NIL) {
154*5885Slinton 	panic("symbol name vanished");
155*5885Slinton     }
156*5885Slinton     while (t != NIL && (s == t || !streq(t->symbol, s->symbol))) {
157*5885Slinton 	t = t->next_sym;
158*5885Slinton     }
159*5885Slinton     return t != NIL;
1605527Slinton }
161