2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 University of Virginia,
4 ** Massachusetts Institute of Technology
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ** General Public License for more details.
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
28 ** storeRef's are kept in allRefs for each function scope, and all are
29 ** free'd at the end of the function. This relies on the constraint that
30 ** no storeRef created while checking a function is used outside that
33 ** storeRefs in the file and global scopes are free'd by the uentry.
37 # include "splintMacros.nf"
39 # include "exprChecks.h"
40 # include "transferChecks.h"
41 # include "sRefTable.h"
42 # include "structNames.h"
45 ** Predicate functions that evaluate both arguments in order.
49 # define OR(a,b) (a ? (b, TRUE) : b)
52 # define AND(a,b) (a ? b : (b, FALSE))
54 static void sRef_checkValidAux (sRef p_s, sRefSet p_checkedsofar) /*@modifies p_checkedsofar@*/ ;
56 static bool sRef_isDerived (sRef p_s) /*@*/ ;
58 static /*@exposed@*/ sRef sRef_fixDirectBase (sRef p_s, sRef p_base)
59 /*@modifies p_base@*/ ;
61 static void sRef_updateNullState (sRef p_res, sRef p_other) /*@modifies p_res@*/ ;
63 static bool sRef_isAllocatedStorage (sRef p_s) /*@*/ ;
64 static void sRef_setNullErrorLoc (sRef p_s, fileloc) /*@*/ ;
65 static void sRef_setNullStateN (sRef p_s, nstate p_n) /*@modifies p_s@*/ ;
67 static int sRef_depth (sRef p_s) /*@*/ ;
70 sRef_innerAliasSetComplete (void (p_predf) (sRef, fileloc), sRef p_s,
75 sRef_innerAliasSetCompleteParam (void (p_predf) (sRef, sRef), sRef p_s, sRef p_t)
78 static speckind speckind_fromInt (int p_i);
79 static bool sRef_equivalent (sRef p_s1, sRef p_s2);
80 static bool sRef_isDeepUnionField (sRef p_s);
81 static void sRef_addDeriv (/*@notnull@*/ sRef p_s, /*@notnull@*/ /*@exposed@*/ sRef p_t);
82 static bool sRef_checkModify (sRef p_s, sRefSet p_sl) /*@*/ ;
85 ** If s is definitely null, it has no memory state.
88 static void sRef_resetAliasKind (/*@notnull@*/ sRef s) /*@modifies s->aliaskind@*/
90 if (s->nullstate == NS_DEFNULL)
92 /* s->aliaskind = AK_ERROR; */
97 extern void sRef_checkCompletelyReasonable (sRef s) /*@*/
99 DPRINTF (("Check sRef: [%p] %s / %s", s, sRef_unparse (s),
100 sRefSet_unparse (sRef_derivedFields (s))));
102 if (sRef_isReasonable (s))
104 sRefSet_elements (s->deriv, el)
107 sRef_checkCompletelyReasonable (el);
108 } end_sRefSet_elements ;
113 /*@falsewhennull@*/ bool sRef_isReasonable (sRef s) /*@*/
115 if (sRef_isValid (s))
118 if (!bool_isReasonable (s->safe)
119 || !bool_isReasonable (s->modified)
120 || !bool_isReasonable (s->immut))
122 llcontbug (message ("Unreasonable sRef [%p]", s));
126 if (!sstate_isValid (s->defstate))
128 llcontbug (message ("Unreasonable sRef [%p]", s));
132 if (!nstate_isValid (s->nullstate))
134 llcontbug (message ("Unreasonable sRef [%p]", s));
138 if (!alkind_isValid (s->aliaskind)
139 || !alkind_isValid (s->oaliaskind))
141 llcontbug (message ("Unreasonable sRef [%p]", s));
145 if (!exkind_isValid (s->expkind)
146 || !exkind_isValid (s->oexpkind))
148 llcontbug (message ("Unreasonable sRef [%p]", s));
159 static /*@nullwhentrue@*/ bool sRef_isUnreasonable (sRef s) /*@*/
161 return (!sRef_isReasonable (s));
164 static void sRef_checkMutable (/*@unused@*/ sRef s)
166 if (sRef_isReasonable (s) && s->immut)
168 llcontbug (message ("Modification to sRef marked immutable: %q",
169 sRef_unparseFull (s)));
173 static bool skind_isSimple (skind sk)
177 case SK_PARAM: case SK_CVAR: case SK_CONST:
178 case SK_OBJECT: case SK_UNKNOWN: case SK_NEW:
185 static void sinfo_free (/*@special@*/ /*@temp@*/ /*@notnull@*/ sRef p_s)
186 /*@uses p_s->kind, p_s->info@*/
187 /*@releases p_s->info@*/ ;
189 static /*@null@*/ sinfo sinfo_copy (/*@notnull@*/ sRef p_s) /*@*/ ;
190 static void sRef_setPartsFromUentry (sRef p_s, uentry p_ue)
192 static bool checkDeadState (/*@notnull@*/ sRef p_el, /*@null@*/ sRef p_e2,
193 bool p_tbranch, fileloc p_loc);
194 static /*@dependent@*/ sRef sRef_constructPointerAux (/*@notnull@*/ /*@exposed@*/ sRef p_t) /*@*/ ;
197 sRef_combineExKinds (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other)
198 /*@modifies p_res@*/ ;
201 sRef_combineAliasKinds (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other,
202 clause p_cl, fileloc p_loc)
203 /*@modifies p_res@*/ ;
206 sRef_combineNullState (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other)
207 /*@modifies p_res@*/ ;
210 sRef_combineDefState (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other)
211 /*@modifies p_res@*/ ;
213 static void sRef_setStateFromAbstractUentry (sRef p_s, uentry p_ue)
217 sinfo_update (/*@notnull@*/ /*@exposed@*/ sRef p_res,
218 /*@notnull@*/ /*@exposed@*/ sRef p_other);
219 static void sRef_setDefinedAux (sRef p_s, fileloc p_loc, bool p_clear)
221 static void sRef_setDefinedNoClear (sRef p_s, fileloc p_loc)
223 static void sRef_setStateAux (sRef p_s, sstate p_ss, fileloc p_loc)
226 static /*@exposed@*/ sRef
227 sRef_buildNCField (/*@exposed@*/ sRef p_rec, /*@exposed@*/ cstring p_f);
230 sRef_mergeStateAux (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other,
231 clause p_cl, bool p_opt, fileloc p_loc,
233 /*@modifies p_res, p_other@*/ ;
235 static /*@null@*/ sinfo sinfo_fullCopy (/*@notnull@*/ sRef p_s);
236 static bool sRef_doModify (sRef p_s, sRefSet p_sl) /*@modifies p_s@*/ ;
237 static bool sRef_doModifyVal (sRef p_s, sRefSet p_sl) /*@modifies p_s@*/;
238 static bool sRef_checkModifyVal (sRef p_s, sRefSet p_sl) /*@*/ ;
240 static /*@only@*/ sRefSet
241 sRef_mergeDerivs (/*@only@*/ sRefSet p_res, sRefSet p_other,
242 bool p_opt, clause p_cl, fileloc p_loc);
244 static /*@only@*/ sRefSet
245 sRef_mergeUnionDerivs (/*@only@*/ sRefSet p_res,
246 /*@exposed@*/ sRefSet p_other,
247 bool p_opt, clause p_cl, fileloc p_loc);
249 static /*@only@*/ sRefSet
250 sRef_mergePdefinedDerivs (/*@only@*/ sRefSet p_res, sRefSet p_other, bool p_opt,
251 clause p_cl, fileloc p_loc);
253 static /*@only@*/ cstring sRef_unparseWithArgs (sRef p_s, uentryList p_args);
254 static /*@only@*/ cstring sRef_unparseNoArgs (sRef p_s);
256 static /*@exposed@*/ sRef sRef_findDerivedPointer (sRef p_s);
257 static /*@exposed@*/ sRef sRef_findDerivedArrayFetch (/*@notnull@*/ sRef, bool, int, bool) ;
258 static /*@exposed@*/ sRef sRef_findDerivedField (/*@notnull@*/ sRef p_rec, cstring p_f);
259 static /*@exposed@*/ sRef
260 sRef_getDeriv (/*@notnull@*/ /*@returned@*/ sRef p_set, sRef p_guide);
262 static bool inFunction = FALSE;
263 static /*@only@*/ sRefTable allRefs;
265 /* # define DEBUGREFS */
269 static totnsrefs = 0;
270 static maxnsrefs = 0;
271 static ntotrefers = 0;
275 static /*@checked@*/ bool protectDerivs = FALSE;
278 ** Result of sRef_alloc is dependent since allRefs may
279 ** reference it. It is only if !inFunction.
282 static /*@dependent@*/ /*@out@*/ /*@notnull@*/ sRef
285 sRef s = (sRef) dmalloc (sizeof (*s));
289 DPRINTF (("Alloc sref: [%p]", s));
293 allRefs = sRefTable_add (allRefs, s);
298 DPRINTF (("Not in function!"));
304 if (nsrefs >= maxnsrefs)
313 /*@-mustfree@*/ /*@-freshtrans@*/
315 /*@=mustfree@*/ /*@=freshtrans@*/
318 static void sRef_checkValidAux (sRef s, sRefSet checkedsofar)
322 if (!sRef_isReasonable (s)) return;
324 if (sRefSet_containsSameObject (checkedsofar, s))
330 checkedsofar = sRefSet_insert (checkedsofar, s);
331 /*@=temptrans@*/ /* checksofar will be destroyed before checkValid returns */
335 case SK_UNCONSTRAINED:
336 llassert (cstring_length (s->info->fname) < 100);
340 llassert (s->info->cvar->lexlevel >= 0);
341 /* llassert (s->info->cvar->lexlevel <= usymtab_getCurrentDepth ()); */
345 llassert (s->info->paramno >= -1);
346 llassert (s->info->paramno <= 999); /* sanity check */
350 sRef_checkValidAux (s->info->arrayfetch->arr, checkedsofar);
354 sRef_checkValidAux (s->info->field->rec, checkedsofar);
355 llassert (cstring_length (s->info->field->field) < 100);
359 sRef_checkValidAux (s->info->ref, checkedsofar);
363 sRef_checkValidAux (s->info->ref, checkedsofar);
367 /* check ctype s->info->object */
371 sRef_checkValidAux (s->info->conj->a, checkedsofar);
372 sRef_checkValidAux (s->info->conj->b, checkedsofar);
376 llassert (cstring_length (s->info->fname) < 100);
380 sRef_checkValidAux (s->info->ref, checkedsofar);
384 sRef_checkValidAux (s->info->ref, checkedsofar);
390 /* check ctyp s->type */
394 llassert (s->info->spec == SR_NOTHING
395 || s->info->spec == SR_INTERNAL
396 || s->info->spec == SR_SPECSTATE
397 || s->info->spec == SR_SYSTEM);
407 sRefSet_elements (s->deriv, el)
409 sRef_checkValidAux (el, checkedsofar);
410 } end_sRefSet_elements ;
413 void sRef_checkValid (/*@unused@*/ sRef s)
417 sRefSet checkedsofar = sRefSet_new ();
418 sRef_checkValidAux (s, checkedsofar);
422 static /*@dependent@*/ /*@notnull@*/ /*@special@*/ sRef
425 /*@ensures isnull result->aliasinfo, result->definfo,
426 result->expinfo, result->info, result->deriv, result->state@*/
428 sRef s = sRef_alloc ();
430 s->kind = SK_UNKNOWN;
434 s->val = multiVal_undefined;
436 s->type = ctype_unknown;
437 s->defstate = SS_UNKNOWN;
439 /* start modifications */
440 s->bufinfo.bufstate = BB_NOTNULLTERMINATED;
441 s->bufinfo.size = -1;
443 /* end modifications */
445 s->aliaskind = AK_UNKNOWN;
446 s->oaliaskind = AK_UNKNOWN;
448 s->nullstate = NS_UNKNOWN;
450 s->expkind = XO_UNKNOWN;
451 s->oexpkind = XO_UNKNOWN;
453 s->aliasinfo = stateInfo_undefined;
454 s->definfo = stateInfo_undefined;
455 s->nullinfo = stateInfo_undefined;
456 s->expinfo = stateInfo_undefined;
459 s->deriv = sRefSet_undefined;
461 s->state = valueTable_undefined;
466 static /*@dependent@*/ /*@notnull@*/ /*@special@*/ sRef
469 /*@ensures isnull result->aliasinfo, result->definfo,
470 result->expinfo, result->info, result->deriv@*/
472 sRef res = sRef_new ();
474 res->state = valueTable_undefined;
479 void sRef_protectDerivs (void) /*@modifies protectDerivs@*/
481 llassert (!protectDerivs);
482 protectDerivs = TRUE;
485 void sRef_clearProtectDerivs (void) /*@modifies protectDerivs@*/
487 llassert (protectDerivs);
488 protectDerivs = FALSE;
492 ** hmmm...here be kind of a hack. This function mysteriously appeared
493 ** in my code, but I'm sure I didn't write it.
497 sRef_isRecursiveField (sRef s)
499 if (sRef_isField (s))
501 if (sRef_depth (s) > 13)
506 fieldname = sRef_getField (s);
507 base = sRef_getBase (s);
509 while (sRef_isReasonable (base))
511 if (sRef_isField (base))
513 if (cstring_equal (fieldname, sRef_getField (base)))
519 base = sRef_getBaseSafe (base);
528 sRef_addDeriv (/*@notnull@*/ sRef s, /*@notnull@*/ /*@exposed@*/ sRef t)
530 if (!context_inProtectVars ()
532 && sRef_isReasonable (s)
533 && sRef_isReasonable (t)
534 && !sRef_isConst (s))
536 int sd = sRef_depth (s);
537 int td = sRef_depth (t);
544 /* This sometimes fails: (evans 2001-07-12)
545 if (sRef_isArrayFetch (t))
547 DPRINTF (("Derived fetch: %s / %s / %s",
548 sRef_unparseFull (s), sRef_unparseFull (t),
549 sRef_unparseFull (t->info->arrayfetch->arr)));
550 llassert (t->info->arrayfetch->arr == s);
554 if (sRef_isFileOrGlobalScope (s))
556 if (context_inFunctionLike ()
557 && ctype_isKnown (sRef_getType (s))
558 && !ctype_isFunction (sRef_getType (s)))
560 globSet g = context_getUsedGlobs ();
562 if (!globSet_member (g, s))
565 ** don't report as a bug
569 (message ("sRef_addDeriv: global variable not in used "
570 "globs: %q / %s / %q",
572 ctype_unparse (sRef_getType (s)),
573 sRefSet_unparse (s->deriv)));
578 DPRINTF (("Add deriv: [%p] %s / [%p] %s",
580 t, sRef_unparse (t)));
582 s->deriv = sRefSet_insert (s->deriv, t);
588 DPRINTF (("Add deriv: [%p] %s / [%p] %s",
590 t, sRef_unparse (t)));
592 s->deriv = sRefSet_insert (s->deriv, t);
598 sRef_deepPred (bool (predf) (sRef), sRef s)
600 if (sRef_isReasonable (s))
602 if ((*predf)(s)) return TRUE;
607 return (sRef_deepPred (predf, s->info->ref));
609 return (sRef_deepPred (predf, s->info->arrayfetch->arr));
611 return (sRef_deepPred (predf, s->info->field->rec));
613 return (sRef_deepPred (predf, s->info->conj->a)
614 || sRef_deepPred (predf, s->info->conj->b));
623 bool sRef_modInFunction (void)
628 void sRef_setStateFromType (sRef s, ctype ct)
630 if (sRef_isReasonable (s))
632 if (ctype_isUser (ct))
634 DPRINTF (("Here we are: %s", sRef_unparseFull (s)));
635 sRef_setStateFromUentry
636 (s, usymtab_getTypeEntry (ctype_typeId (ct)));
638 else if (ctype_isAbstract (ct))
640 DPRINTF (("Here we are: %s", sRef_unparseFull (s)));
641 sRef_setStateFromAbstractUentry
642 (s, usymtab_getTypeEntry (ctype_typeId (ct)));
646 ; /* not a user type */
651 static void sRef_setTypeState (sRef s)
653 if (sRef_isReasonable (s))
655 sRef_setStateFromType (s, s->type);
659 bool sRef_hasAliasInfoLoc (sRef s)
661 return (sRef_isReasonable (s) && (s->aliasinfo != NULL)
662 && (fileloc_isDefined (s->aliasinfo->loc)));
665 /*@falsewhennull@*/ bool sRef_hasStateInfoLoc (sRef s)
667 return (sRef_isReasonable (s) && (s->definfo != NULL)
668 && (fileloc_isDefined (s->definfo->loc)));
671 /*@falsewhennull@*/ bool sRef_hasExpInfoLoc (sRef s)
673 return (sRef_isReasonable (s)
674 && (s->expinfo != NULL) && (fileloc_isDefined (s->expinfo->loc)));
678 static /*@observer@*/ /*@unused@*/ stateInfo sRef_getInfo (sRef s, cstring key)
682 if (!sRef_isReasonable (s)) {
683 return stateInfo_undefined;
686 sv = valueTable_lookup (s->state, key);
688 if (stateValue_isDefined (sv))
690 return stateValue_getInfo (sv);
693 return stateInfo_undefined;
698 sRef_hasNullInfoLoc (sRef s)
700 return (sRef_isReasonable (s) && s->nullinfo != NULL
701 && (fileloc_isDefined (s->nullinfo->loc)));
705 sRef_hasAliasInfoRef (sRef s)
707 return (sRef_isReasonable (s) && (s->aliasinfo != NULL)
708 && (sRef_isReasonable (s->aliasinfo->ref)));
711 static /*@observer@*/ fileloc
712 sRef_getAliasInfoLoc (/*@exposed@*/ sRef s)
714 llassert (sRef_isReasonable (s) && s->aliasinfo != NULL
715 && (fileloc_isDefined (s->aliasinfo->loc)));
716 return (s->aliasinfo->loc);
719 static /*@observer@*/ fileloc
720 sRef_getStateInfoLoc (/*@exposed@*/ sRef s)
722 llassert (sRef_isReasonable (s) && s->definfo != NULL
723 && (fileloc_isDefined (s->definfo->loc)));
724 return (s->definfo->loc);
727 static /*@observer@*/ fileloc
728 sRef_getExpInfoLoc (/*@exposed@*/ sRef s)
730 llassert (sRef_isReasonable (s) && s->expinfo != NULL
731 && (fileloc_isDefined (s->expinfo->loc)));
732 return (s->expinfo->loc);
735 static /*@observer@*/ fileloc
736 sRef_getNullInfoLoc (/*@exposed@*/ sRef s)
738 llassert (sRef_isReasonable (s) && s->nullinfo != NULL
739 && (fileloc_isDefined (s->nullinfo->loc)));
740 return (s->nullinfo->loc);
744 sRef_getAliasInfoRef (/*@temp@*/ sRef s)
746 llassert (sRef_isReasonable (s) && s->aliasinfo != NULL);
747 return (s->aliasinfo->ref);
750 bool sRef_inGlobalScope ()
756 ** This function should be called before new sRefs are created
757 ** somewhere where they will have a lifetime greater than the
758 ** current function scope.
761 void sRef_setGlobalScope ()
763 llassert (inFunction);
764 DPRINTF (("leave function"));
768 void sRef_clearGlobalScope ()
770 llassert (!inFunction);
771 DPRINTF (("enter function"));
775 static bool oldInFunction = FALSE;
776 static int nestedScope = 0;
778 void sRef_setGlobalScopeSafe ()
780 if (nestedScope == 0)
782 oldInFunction = inFunction;
786 DPRINTF (("leave function safe"));
790 void sRef_clearGlobalScopeSafe ()
793 llassert (nestedScope >= 0);
795 if (nestedScope == 0)
797 inFunction = oldInFunction;
800 DPRINTF (("clear function: %s", bool_unparse (inFunction)));
803 void sRef_enterFunctionScope ()
805 /* evans 2001-09-09 - cleanup if we are in a macro! */
806 if (context_inMacro ())
809 sRef_exitFunctionScope ();
813 llassert (!inFunction);
814 llassert (sRefTable_isEmpty (allRefs));
816 DPRINTF (("enter function"));
819 void sRef_exitFunctionScope ()
823 DPRINTF (("Exit function scope."));
824 sRefTable_clear (allRefs);
825 DPRINTF (("Exit function scope done."));
830 llbuglit ("sRef_exitFunctionScope: not in function");
834 void sRef_destroyMod () /*@globals killed allRefs;@*/
837 llmsg (message ("Live: %d / %d ", nsrefs, totnsrefs));
840 sRefTable_free (allRefs);
844 static /*@notnull@*/ /*@exposed@*/ sRef
845 sRef_fixConj (/*@notnull@*/ sRef s)
850 s = sRef_getConjA (s);
851 } while (sRef_isConj (s));
853 llassert (sRef_isReasonable (s));
854 return s; /* don't need to ref */
863 sRef_isExternallyVisibleAux (sRef s)
866 sRef base = sRef_getRootBase (s);
868 if (sRef_isReasonable (base))
870 res = sRef_isParam (base) || sRef_isFileOrGlobalScope (base) || sRef_isExternal (base);
877 sRef_isExternallyVisible (sRef s)
879 return (sRef_aliasCheckSimplePred (sRef_isExternallyVisibleAux, s));
883 sRef_getBaseUentry (sRef s)
885 sRef base = sRef_getRootBase (s);
886 uentry res = uentry_undefined;
888 if (sRef_isReasonable (base))
893 res = usymtab_getRefQuiet (paramsScope, usymId_fromInt (base->info->paramno));
897 res = usymtab_getRefQuiet (base->info->cvar->lexlevel,
898 base->info->cvar->index);
910 ** lookup the current uentry corresponding to s, and return the corresponding sRef.
911 ** yuk yuk yuk yuk yuk yuk yuk yuk
915 sRef_updateSref (sRef s)
921 if (!sRef_isReasonable (s)) return sRef_undefined;
930 case SK_UNCONSTRAINED:
937 sRef r = sRef_updateSref (s->info->ref);
939 if (r != s->info->ref)
941 return sRef_makeExternal (r);
951 uentry ue = sRef_getUentry (s);
953 /* must be raw name! (need the marker) */
954 ue = usymtab_lookupSafe (uentry_rawName (ue));
956 if (uentry_isUndefined (ue))
962 DPRINTF (("Update sref: %s", uentry_unparseFull (ue)));
963 return (uentry_getSref (ue));
967 /* special case if ind known */
968 inner = s->info->arrayfetch->arr;
969 ret = sRef_updateSref (inner);
977 res = sRef_makeArrayFetch (ret);
983 inner = s->info->field->rec;
984 ret = sRef_updateSref (inner);
992 res = (sRef_makeField (ret, s->info->field->field));
997 inner = s->info->ref;
998 ret = sRef_updateSref (inner);
1005 res = sRef_makePointer (ret);
1011 inner = s->info->ref;
1012 ret = sRef_updateSref (inner);
1020 res = sRef_makeAddress (ret);
1027 sRef innera = s->info->conj->a;
1028 sRef innerb = s->info->conj->b;
1029 sRef reta = sRef_updateSref (innera);
1030 sRef retb = sRef_updateSref (innerb);
1032 if (innera == reta && innerb == retb)
1038 res = sRef_makeConj (reta, retb);
1049 sRef_getUentry (sRef s)
1051 llassert (sRef_isReasonable (s));
1056 return (usymtab_getRefQuiet (paramsScope, usymId_fromInt (s->info->paramno)));
1058 return (usymtab_getRefQuiet (s->info->cvar->lexlevel, s->info->cvar->index));
1061 if (sRef_isCvar (s->info->conj->a) || sRef_isParam (s->info->conj->a)
1062 || sRef_isConj (s->info->conj->a))
1064 return sRef_getUentry (s->info->conj->a);
1068 return sRef_getUentry (s->info->conj->b);
1071 case SK_FIELD: /* evans 2002-07-17: added case for SK_FIELD */
1074 return uentry_undefined;
1080 sRef_getParam (sRef s)
1082 llassert (sRef_isReasonable (s));
1083 llassert (s->kind == SK_PARAM);
1085 return s->info->paramno;
1089 sRef_isModified (sRef s)
1091 return (!sRef_isReasonable (s) || s->modified);
1094 void sRef_setModified (sRef s)
1096 if (sRef_isReasonable (s))
1101 if (sRef_isRefsField (s))
1103 sRef base = sRef_getBase (s);
1105 llassert (s->kind == SK_FIELD);
1107 if (sRef_isPointer (base))
1109 base = sRef_getBase (base);
1112 if (sRef_isRefCounted (base))
1114 base->aliaskind = AK_NEWREF;
1121 ** note: this side-effects sRefSet to set modified to TRUE
1122 ** for any sRef similar to s.
1126 sRef_canModifyVal (sRef s, sRefSet sl)
1128 if (context_getFlag (FLG_MUSTMOD))
1130 return (sRef_doModifyVal (s, sl));
1134 return (sRef_checkModifyVal (s, sl));
1139 sRef_canModify (sRef s, sRefSet sl)
1141 if (context_getFlag (FLG_MUSTMOD))
1143 return (sRef_doModify (s, sl));
1147 return (sRef_checkModify (s, sl));
1156 bool sRef_checkModifyVal (sRef s, sRefSet sl)
1158 DPRINTF (("Check modify val: %s", sRef_unparse (s)));
1160 if (sRef_isInvalid (s))
1167 case SK_UNCONSTRAINED:
1171 DPRINTF (("Modify var: %s", sRef_unparse (s)));
1173 if (sRef_isFileOrGlobalScope (s))
1175 if (context_checkGlobMod (s))
1177 return (sRefSet_member (sl, s));
1187 return (sRefSet_member (sl, s)
1188 || alkind_isOnly (sRef_getOrigAliasKind (s)));
1190 /* special case if ind known */
1191 return (sRefSet_member (sl, s) ||
1192 sRef_checkModifyVal (s->info->arrayfetch->arr, sl));
1194 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->field->rec, sl));
1196 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->ref, sl));
1198 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->ref, sl));
1200 return ((sRef_checkModifyVal (s->info->conj->a, sl)) &&
1201 (sRef_checkModifyVal (s->info->conj->b, sl)));
1212 switch (s->info->spec)
1214 case SR_NOTHING: return TRUE;
1216 if (context_getFlag (FLG_INTERNALGLOBS))
1218 return (sRefSet_member (sl, s));
1224 case SR_SPECSTATE: return TRUE;
1225 case SR_SYSTEM: return (sRefSet_member (sl, s));
1226 case SR_GLOBALMARKER: BADBRANCH;
1229 case SK_RESULT: BADBRANCH;
1235 ** this should probably be elsewhere...
1237 ** returns TRUE iff sl indicates that s can be modified
1240 static bool sRef_checkModify (sRef s, sRefSet sl)
1242 llassert (sRef_isReasonable (s));
1246 case SK_UNCONSTRAINED:
1250 if (sRef_isFileOrGlobalScope (s))
1252 if (context_checkGlobMod (s))
1254 return (sRefSet_member (sl, s));
1266 return (sRefSet_member (sl, s) ||
1267 sRef_checkModifyVal (s->info->arrayfetch->arr, sl));
1270 sRef sr = s->info->field->rec;
1272 if (sr->kind == SK_PARAM)
1273 return TRUE; /* structs are copied on call */
1275 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->field->rec, sl));
1281 sm = sRefSet_member (sl, s);
1286 return (sRef_checkModifyVal (s->info->ref, sl));
1289 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->ref, sl));
1291 return ((sRef_checkModify (s->info->conj->a, sl)) &&
1292 (sRef_checkModify (s->info->conj->b, sl)));
1302 switch (s->info->spec)
1304 case SR_NOTHING: return TRUE;
1306 if (context_getFlag (FLG_INTERNALGLOBS))
1308 return (sRefSet_member (sl, s));
1314 case SR_SPECSTATE: return TRUE;
1315 case SR_SYSTEM: return (sRefSet_member (sl, s));
1316 case SR_GLOBALMARKER: BADBRANCH;
1319 case SK_RESULT: BADBRANCH;
1324 cstring sRef_stateVerb (sRef s)
1326 if (sRef_isDead (s))
1328 return cstring_makeLiteralTemp ("released");
1330 else if (sRef_isKept (s))
1332 return cstring_makeLiteralTemp ("kept");
1334 else if (sRef_isDependent (s))
1336 return cstring_makeLiteralTemp ("dependent");
1344 cstring sRef_stateAltVerb (sRef s)
1346 if (sRef_isDead (s))
1348 return cstring_makeLiteralTemp ("live");
1350 else if (sRef_isKept (s))
1352 return cstring_makeLiteralTemp ("not kept");
1354 else if (sRef_isDependent (s))
1356 return cstring_makeLiteralTemp ("independent");
1365 bool sRef_doModifyVal (sRef s, sRefSet sl)
1367 llassert (sRef_isReasonable (s));
1372 case SK_UNCONSTRAINED:
1376 if (sRef_isFileOrGlobalScope (s))
1379 if (context_checkGlobMod (s))
1381 return (sRefSet_modifyMember (sl, s));
1385 (void) sRefSet_modifyMember (sl, s);
1395 return (sRefSet_modifyMember (sl, s)
1396 || alkind_isOnly (sRef_getOrigAliasKind (s)));
1398 /* special case if ind known */
1399 /* unconditional OR, need side effect */
1400 return (OR (sRefSet_modifyMember (sl, s),
1401 sRef_doModifyVal (s->info->arrayfetch->arr, sl)));
1403 return (OR (sRefSet_modifyMember (sl, s),
1404 sRef_doModifyVal (s->info->field->rec, sl)));
1406 return (OR (sRefSet_modifyMember (sl, s),
1407 sRef_doModifyVal (s->info->ref, sl)));
1409 return (OR (sRefSet_modifyMember (sl, s),
1410 sRef_doModifyVal (s->info->ref, sl)));
1412 return (AND (sRef_doModifyVal (s->info->conj->a, sl) ,
1413 sRef_doModifyVal (s->info->conj->b, sl)));
1423 switch (s->info->spec)
1425 case SR_NOTHING: return TRUE;
1427 if (context_getFlag (FLG_INTERNALGLOBS))
1429 return (sRefSet_modifyMember (sl, s));
1433 (void) sRefSet_modifyMember (sl, s);
1436 case SR_SPECSTATE: return TRUE;
1437 case SR_SYSTEM: return (sRefSet_modifyMember (sl, s));
1438 case SR_GLOBALMARKER: BADBRANCH;
1442 case SK_RESULT: BADBRANCH;
1448 ** this should probably be elsewhere...
1450 ** returns TRUE iff sl indicates that s can be modified
1454 bool sRef_doModify (sRef s, sRefSet sl)
1456 llassert (sRef_isReasonable (s));
1460 case SK_UNCONSTRAINED:
1464 if (sRef_isFileOrGlobalScope (s))
1466 if (context_checkGlobMod (s))
1468 return (sRefSet_modifyMember (sl, s));
1472 (void) sRefSet_modifyMember (sl, s);
1484 return (OR (sRefSet_modifyMember (sl, s),
1485 sRef_doModifyVal (s->info->arrayfetch->arr, sl)));
1488 sRef sr = s->info->field->rec;
1490 if (sr->kind == SK_PARAM)
1492 return TRUE; /* structs are shallow-copied on call */
1495 return (OR (sRefSet_modifyMember (sl, s),
1496 sRef_doModifyVal (s->info->field->rec, sl)));
1500 return (OR (sRefSet_modifyMember (sl, s),
1501 sRef_doModifyVal (s->info->ref, sl)));
1504 return (OR (sRefSet_modifyMember (sl, s),
1505 sRef_doModifyVal (s->info->ref, sl)));
1507 return (AND (sRef_doModify (s->info->conj->a, sl),
1508 (sRef_doModify (s->info->conj->b, sl))));
1519 switch (s->info->spec)
1521 case SR_NOTHING: return TRUE;
1522 case SR_INTERNAL: return TRUE;
1523 case SR_SPECSTATE: return TRUE;
1524 case SR_SYSTEM: return (sRefSet_modifyMember (sl, s));
1525 case SR_GLOBALMARKER: BADBRANCH;
1528 case SK_RESULT: BADBRANCH;
1533 static /*@exposed@*/ sRef
1534 sRef_leastCommon (/*@exposed@*/ sRef s1, sRef s2)
1536 llassert (sRef_similar (s1, s2));
1538 if (!sRef_isReasonable (s1)) return s1;
1539 if (!sRef_isReasonable (s2)) return s1;
1541 sRef_combineDefState (s1, s2);
1542 sRef_combineNullState (s1, s2);
1543 sRef_combineExKinds (s1, s2);
1545 if (s1->aliaskind != s2->aliaskind)
1547 if (s1->aliaskind == AK_UNKNOWN)
1549 s1->aliaskind = s2->aliaskind;
1551 else if (s2->aliaskind == AK_UNKNOWN)
1557 s1->aliaskind = AK_ERROR;
1564 int sRef_compare (sRef s1, sRef s2)
1566 if (s1 == s2) return 0;
1568 if (sRef_isInvalid (s1)) return -1;
1569 if (sRef_isInvalid (s2)) return 1;
1571 INTCOMPARERETURN (s1->kind, s2->kind);
1572 INTCOMPARERETURN (s1->defstate, s2->defstate);
1573 INTCOMPARERETURN (s1->aliaskind, s2->aliaskind);
1575 DPRINTF (("Compare null state: %s / %s",
1576 sRef_unparseFull (s1),
1577 sRef_unparseFull (s2)));
1579 COMPARERETURN (nstate_compare (sRef_getNullState (s1),
1580 sRef_getNullState (s2)));
1585 return (int_compare (s1->info->paramno, s2->info->paramno));
1588 COMPARERETURN (sRef_compare (s1->info->arrayfetch->arr,
1589 s2->info->arrayfetch->arr));
1591 if (s1->info->arrayfetch->indknown && s2->info->arrayfetch->indknown)
1593 return (int_compare (s1->info->arrayfetch->ind,
1594 s2->info->arrayfetch->ind));
1596 if (!s1->info->arrayfetch->indknown && !s2->info->arrayfetch->indknown)
1603 COMPARERETURN (sRef_compare (s1->info->field->rec, s2->info->field->rec));
1605 if (cstring_equal (s1->info->field->field, s2->info->field->field))
1612 return (sRef_compare (s1->info->ref, s2->info->ref));
1614 COMPARERETURN (sRef_compare (s1->info->conj->a, s2->info->conj->a));
1615 return (sRef_compare (s1->info->conj->b, s2->info->conj->b));
1616 case SK_UNCONSTRAINED:
1617 return (cstring_compare (s1->info->fname, s2->info->fname));
1629 return (generic_compare (s1->info->spec, s2->info->spec));
1634 static bool cref_equal (cref c1, cref c2)
1636 return ((c1->lexlevel == c2->lexlevel) &&
1637 (usymId_equal (c1->index, c2->index)));
1641 ** returns true if s1 could be the same storage as s2.
1642 ** i.e., a[?] ~ a[3]. Note its not symmetric ... s1
1643 ** should be more specific.
1647 ** like similar, but matches objects <-> non-objects
1651 sRef_uniqueReference (sRef s)
1653 return (sRef_isFresh (s) || sRef_isUnique (s)
1654 || sRef_isOnly (s) || sRef_isStack (s)
1655 || sRef_isAddress (s));
1659 sRef_similarRelaxedAux (sRef s1, sRef s2)
1663 if (sRef_isUnknownArrayFetch (s1))
1673 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
1675 if (sRef_isConj (s2))
1676 return (sRef_similarRelaxedAux (s1, sRef_getConjA (s2)) ||
1677 sRef_similarRelaxedAux (s1, sRef_getConjB (s2)));
1682 return ((s2->kind == SK_CVAR)
1683 && (cref_equal (s1->info->cvar, s2->info->cvar)));
1685 return ((s2->kind == SK_PARAM)
1686 && (s1->info->paramno == s2->info->paramno));
1688 if (s2->kind == SK_ARRAYFETCH)
1690 if (sRef_similarRelaxedAux (s1->info->arrayfetch->arr,
1691 s2->info->arrayfetch->arr))
1693 if (s1->info->arrayfetch->indknown)
1695 if (s2->info->arrayfetch->indknown)
1697 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
1712 return ((s2->kind == SK_FIELD
1713 && (sRef_similarRelaxedAux (s1->info->field->rec,
1714 s2->info->field->rec)
1715 && cstring_equal (s1->info->field->field,
1716 s2->info->field->field))));
1718 return ((s2->kind == SK_PTR)
1719 && sRef_similarRelaxedAux (s1->info->ref, s2->info->ref));
1721 return ((s2->kind == SK_ADR)
1722 && sRef_similarRelaxedAux (s1->info->ref, s2->info->ref));
1724 return ((sRef_similarRelaxedAux (s1->info->conj->a, s2) ||
1725 (sRef_similarRelaxedAux (s1->info->conj->b, s2))));
1727 return (s1->info->spec == s2->info->spec);
1728 case SK_UNCONSTRAINED:
1729 return (cstring_equal (s1->info->fname, s2->info->fname));
1744 sRef_similarRelaxed (sRef s1, sRef s2)
1750 if (sRef_isThroughArrayFetch (s1))
1760 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
1762 us1 = sRef_uniqueReference (s1);
1763 us2 = sRef_uniqueReference (s2);
1765 if ((s1->kind == SK_EXTERNAL && (s2->kind != SK_EXTERNAL && !us2))
1766 || (s2->kind == SK_EXTERNAL && (s1->kind != SK_EXTERNAL && !us1)))
1769 ** Previously, also:
1770 ** || (sRef_isExposed (s1) && !us2) || (sRef_isExposed (s2) && !us1)) ????
1772 ** No clue why this was there?!
1776 if (sRef_isExposed (s1) && sRef_isCvar (s1))
1778 uentry ue1 = sRef_getUentry (s1);
1780 if (uentry_isRefParam (ue1))
1782 return sRef_similarRelaxedAux (s1, s2);
1786 if (sRef_isExposed (s2) && sRef_isCvar (s2))
1788 uentry ue2 = sRef_getUentry (s2);
1790 if (uentry_isRefParam (ue2))
1792 return sRef_similarRelaxedAux (s1, s2);
1796 return (ctype_match (s1->type, s2->type));
1800 return sRef_similarRelaxedAux (s1, s2);
1805 sRef_similar (sRef s1, sRef s2)
1807 if (s1 == s2) return TRUE;
1808 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
1810 if (sRef_isConj (s2))
1812 return (sRef_similar (s1, sRef_getConjA (s2)) ||
1813 sRef_similar (s1, sRef_getConjB (s2)));
1816 if (sRef_isDerived (s2))
1818 return (sRef_includedBy (s1, s2->info->ref));
1824 return ((s2->kind == SK_CVAR)
1825 && (cref_equal (s1->info->cvar, s2->info->cvar)));
1827 return ((s2->kind == SK_PARAM)
1828 && (s1->info->paramno == s2->info->paramno));
1830 if (s2->kind == SK_ARRAYFETCH)
1832 if (sRef_similar (s1->info->arrayfetch->arr,
1833 s2->info->arrayfetch->arr))
1835 if (s1->info->arrayfetch->indknown)
1837 if (s2->info->arrayfetch->indknown)
1839 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
1854 if (s2->kind == SK_PTR)
1856 if (sRef_similar (s1->info->arrayfetch->arr,
1866 return ((s2->kind == SK_FIELD
1867 && (sRef_similar (s1->info->field->rec,
1868 s2->info->field->rec)
1869 && cstring_equal (s1->info->field->field,
1870 s2->info->field->field))));
1872 if (s2->kind == SK_PTR)
1874 return sRef_similar (s1->info->ref, s2->info->ref);
1878 if (s2->kind == SK_ARRAYFETCH)
1880 if (sRef_similar (s2->info->arrayfetch->arr,
1890 return ((s2->kind == SK_ADR)
1891 && sRef_similar (s1->info->ref, s2->info->ref));
1893 return ((sRef_similar (s1->info->conj->a, s2) ||
1894 (sRef_similar (s1->info->conj->b, s2))));
1896 return (sRef_includedBy (s2, s1->info->ref));
1897 case SK_UNCONSTRAINED:
1898 return (s2->kind == SK_UNCONSTRAINED
1899 && cstring_equal (s1->info->fname, s2->info->fname));
1909 return (s2->kind == SK_SPECIAL
1910 && (s1->info->spec == s2->info->spec));
1913 /*@notreached@*/ DPRINTF (("Fell through for: %s / %s", sRef_unparse (s1), sRef_unparse (s2)));
1918 ** return TRUE iff small can be derived from big.
1920 ** (e.g. x, x.a is includedBy x;
1921 ** x.a is included By x.a;
1925 sRef_includedBy (sRef small, sRef big)
1927 if (small == big) return TRUE;
1928 if (sRef_isInvalid (small) || sRef_isInvalid (big)) return FALSE;
1930 if (sRef_isConj (big))
1931 return (sRef_similar (small, sRef_getConjA (big)) ||
1932 sRef_similar (small, sRef_getConjB (big)));
1934 switch (small->kind)
1938 return (sRef_same (small, big));
1940 if (big->kind == SK_ARRAYFETCH)
1942 if (sRef_same (small->info->arrayfetch->arr, big->info->arrayfetch->arr))
1944 if (small->info->arrayfetch->indknown)
1946 if (big->info->arrayfetch->indknown)
1948 return (small->info->arrayfetch->ind == big->info->arrayfetch->ind);
1961 return (sRef_includedBy (small->info->arrayfetch->arr, big));
1963 if (big->kind == SK_FIELD)
1966 (sRef_same (small->info->field->rec, big->info->field->rec) &&
1967 cstring_equal (small->info->field->field, big->info->field->field));
1971 return (sRef_includedBy (small->info->field->rec, big));
1975 if (big->kind == SK_PTR)
1977 return sRef_same (small->info->ref, big->info->ref);
1981 return (sRef_includedBy (small->info->ref, big));
1985 return ((big->kind == SK_ADR) && sRef_similar (small->info->ref, big->info->ref));
1987 return ((sRef_includedBy (small->info->conj->a, big) ||
1988 (sRef_includedBy (small->info->conj->b, big))));
1990 return (sRef_includedBy (small->info->ref, big));
1991 case SK_UNCONSTRAINED:
2001 switch (small->info->spec)
2003 case SR_NOTHING: return TRUE;
2005 case SR_INTERNAL: return (sRef_isSpecInternalState (big) ||
2006 sRef_isFileStatic (big));
2007 case SR_SYSTEM: return (sRef_isSystemState (big));
2008 case SR_GLOBALMARKER: BADBRANCH;
2015 ** Same is similar to similar, but not quite the same.
2016 ** same and realSame aren't the same, but they are really similar.
2017 ** similarly, same is the same as same. but realSame is
2018 ** not really the same as same, or similar to similar.
2020 ** Similarly to similar, same checks if two sRefs are the same.
2021 ** The similarities end, however, when same compares arrays
2022 ** with unknown indexes. Similar returns false; same returns true.
2024 ** Similarly to similar and same, realSame is the same as same,
2025 ** except they do not behave the same when face with unknown
2026 ** sRefs. Same thinks they are not the same, but realSame thinks
2032 sRef_realSame (sRef s1, sRef s2)
2034 if (s1 == s2) return TRUE;
2036 if (sRef_isUnreasonable (s1)
2037 || sRef_isUnreasonable (s2))
2045 return ((s2->kind == SK_CVAR) && (cref_equal (s1->info->cvar, s2->info->cvar)));
2047 return ((s2->kind == SK_PARAM) && (s1->info->paramno == s2->info->paramno));
2049 if (s2->kind == SK_ARRAYFETCH)
2051 if (sRef_realSame (s1->info->arrayfetch->arr, s2->info->arrayfetch->arr))
2053 if (s1->info->arrayfetch->indknown && s2->info->arrayfetch->indknown)
2055 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
2057 if (!s1->info->arrayfetch->indknown && !s2->info->arrayfetch->indknown)
2066 return ((s2->kind == SK_FIELD &&
2067 (sRef_realSame (s1->info->field->rec, s2->info->field->rec) &&
2068 cstring_equal (s1->info->field->field, s2->info->field->field))));
2070 return ((s2->kind == SK_PTR) && sRef_realSame (s1->info->ref, s2->info->ref));
2072 return ((s2->kind == SK_ADR) && sRef_realSame (s1->info->ref, s2->info->ref));
2074 return ((sRef_realSame (s1->info->conj->a, s2) ||
2075 (sRef_realSame (s1->info->conj->b, s2))));
2077 return ((s2->kind == SK_OBJECT)
2078 && ctype_match (s1->info->object, s2->info->object));
2080 return ((s2->kind == SK_EXTERNAL)
2081 && sRef_realSame (s1->info->ref, s2->info->ref));
2083 return ((s2->kind == SK_SPECIAL) && s1->info->spec == s2->info->spec);
2085 return ((s2->kind == SK_DERIVED) && sRef_realSame (s1->info->ref, s2->info->ref));
2086 case SK_UNCONSTRAINED:
2087 return ((s2->kind == SK_UNCONSTRAINED)
2088 && (cstring_equal (s1->info->fname, s2->info->fname)));
2094 return TRUE; /* changed this! was false */
2100 sRef_sameObject (sRef s1, sRef s2)
2106 ** same is similar to similar, but not quite the same.
2108 ** Similarly to similar, same checks is two sRefs are the same.
2109 ** The similarities end, however, when same compares arrays
2110 ** with unknown indexes. Similar returns false; same returns true.
2114 sRef_same (sRef s1, sRef s2)
2116 if (s1 == s2) return TRUE;
2117 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
2122 return ((s2->kind == SK_CVAR) && (cref_equal (s1->info->cvar, s2->info->cvar)));
2124 return ((s2->kind == SK_PARAM) && (s1->info->paramno == s2->info->paramno));
2126 if (s2->kind == SK_ARRAYFETCH)
2128 llassert (s1->info->field->rec != s1);
2129 if (sRef_same (s1->info->arrayfetch->arr, s2->info->arrayfetch->arr))
2131 if (s1->info->arrayfetch->indknown && s2->info->arrayfetch->indknown)
2133 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
2141 llassert (s1->info->field->rec != s1);
2142 return ((s2->kind == SK_FIELD &&
2143 (sRef_same (s1->info->field->rec, s2->info->field->rec) &&
2144 cstring_equal (s1->info->field->field, s2->info->field->field))));
2148 llassert (s1->info->ref != s1);
2149 return ((s2->kind == SK_PTR) && sRef_same (s1->info->ref, s2->info->ref));
2153 llassert (s1->info->ref != s1);
2154 return ((s2->kind == SK_ADR) && sRef_same (s1->info->ref, s2->info->ref));
2157 llassert (s1->info->conj->a != s1);
2158 llassert (s1->info->conj->b != s1);
2159 return ((sRef_same (s1->info->conj->a, s2)) && /* or or and? */
2160 (sRef_same (s1->info->conj->b, s2)));
2162 return ((s2->kind == SK_SPECIAL) && s1->info->spec == s2->info->spec);
2164 llassert (s1->info->ref != s1);
2165 return ((s2->kind == SK_DERIVED) && sRef_same (s1->info->ref, s2->info->ref));
2167 case SK_UNCONSTRAINED:
2180 ** sort of similar, for use in def/use
2184 sRef_closeEnough (sRef s1, sRef s2)
2186 if (s1 == s2) return TRUE;
2187 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
2192 return (((s2->kind == SK_CVAR) &&
2193 (cref_equal (s1->info->cvar, s2->info->cvar))) ||
2194 (s2->kind == SK_UNCONSTRAINED && s1->info->cvar->lexlevel == 0));
2195 case SK_UNCONSTRAINED:
2196 return (s2->kind == SK_UNCONSTRAINED
2197 || ((s2->kind == SK_CVAR) && (s2->info->cvar->lexlevel == 0)));
2199 return ((s2->kind == SK_PARAM)
2200 && (s1->info->paramno == s2->info->paramno));
2202 if (s2->kind == SK_ARRAYFETCH)
2204 if (sRef_closeEnough (s1->info->arrayfetch->arr, s2->info->arrayfetch->arr))
2206 if (s1->info->arrayfetch->indknown && s2->info->arrayfetch->indknown)
2208 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
2215 return ((s2->kind == SK_FIELD &&
2216 (sRef_closeEnough (s1->info->field->rec, s2->info->field->rec) &&
2217 cstring_equal (s1->info->field->field, s2->info->field->field))));
2219 return ((s2->kind == SK_PTR) && sRef_closeEnough (s1->info->ref, s2->info->ref));
2221 return ((s2->kind == SK_ADR) && sRef_closeEnough (s1->info->ref, s2->info->ref));
2223 return ((s2->kind == SK_DERIVED) && sRef_closeEnough (s1->info->ref, s2->info->ref));
2225 return ((sRef_closeEnough (s1->info->conj->a, s2)) ||
2226 (sRef_closeEnough (s1->info->conj->b, s2)));
2228 return ((s2->kind == SK_SPECIAL) && s1->info->spec == s2->info->spec);
2244 s is an sRef of a formal paramenter in a function call constraint
2245 we trys to return a constraint expression derived from the actual parementer of a function call.
2248 /*@only@*/ constraintExpr sRef_fixConstraintParam (/*@observer@*/ sRef s, /*@observer@*/ /*@temp@*/ exprNodeList args)
2252 if (sRef_isInvalid (s))
2253 llfatalbug((message("Invalid sRef")));
2259 ce = constraintExpr_makeTermsRef (s);
2266 temp = (sRef_makeField (sRef_fixBaseParam (s->info->field->rec, args),
2267 s->info->field->field));
2268 ce = constraintExpr_makeTermsRef (temp);
2274 temp = (sRef_makePointer (sRef_fixBaseParam (s->info->ref, args)));
2275 ce = constraintExpr_makeTermsRef (temp);
2282 temp = sRef_saveCopy(s);
2283 temp = sRef_fixBaseParam (temp, args);
2284 ce = constraintExpr_makeTermsRef (temp);
2292 temp = sRef_saveCopy(s);
2293 ce = constraintExpr_makeTermsRef (temp);
2300 llassert (exprNodeList_size (args) > s->info->paramno);
2301 e = exprNodeList_nth (args, s->info->paramno);
2303 llassert (!(exprNode_isError (e)));
2304 ce = constraintExpr_makeExprNode (e);
2311 llcontbug (message ("Trying to do fixConstraintParam on nonparam, nonglobal: %q for function with arguments %q",
2312 sRef_unparse (s), exprNodeList_unparse(args)));
2313 temp = sRef_saveCopy(s);
2314 ce = constraintExpr_makeTermsRef (temp);
2323 sRef_fixBaseParam (/*@returned@*/ sRef s, exprNodeList args)
2325 if (sRef_isInvalid (s)) return (sRef_undefined);
2329 case SK_UNCONSTRAINED:
2334 if (exprNodeList_size (args) > s->info->paramno)
2336 exprNode e = exprNodeList_nth (args, s->info->paramno);
2338 if (exprNode_isError (e))
2340 return sRef_makeUnknown ();
2343 return (exprNode_getSref (e));
2347 return sRef_makeUnknown ();
2352 if (s->info->arrayfetch->indknown)
2354 return (sRef_makeArrayFetchKnown
2355 (sRef_fixBaseParam (s->info->arrayfetch->arr, args),
2356 s->info->arrayfetch->ind));
2360 return (sRef_makeArrayFetch
2361 (sRef_fixBaseParam (s->info->arrayfetch->arr, args)));
2366 DPRINTF (("Fix field: %s", sRef_unparseFull (s)));
2367 res = sRef_makeField (sRef_fixBaseParam (s->info->field->rec, args),
2368 s->info->field->field);
2369 DPRINTF (("Returns: %s", sRef_unparseFull (res)));
2373 return (sRef_makePointer (sRef_fixBaseParam (s->info->ref, args)));
2376 return (sRef_makeAddress (sRef_fixBaseParam (s->info->ref, args)));
2379 return (sRef_makeConj (sRef_fixBaseParam (s->info->conj->a, args),
2380 sRef_fixBaseParam (s->info->conj->b, args)));
2396 sRef_undumpGlobal (char **c)
2406 usymId uid = usymId_fromInt (reader_getInt (c));
2411 reader_checkChar (c, '@');
2412 defstate = sstate_fromInt (reader_getInt (c));
2414 reader_checkChar (c, '@');
2415 nullstate = nstate_fromInt (reader_getInt (c));
2417 ret = sRef_makeGlobal (uid, ctype_unknown, stateInfo_currentLoc ());
2418 sRef_setNullStateN (ret, nullstate);
2419 ret->defstate = defstate;
2424 int i = reader_getInt (c);
2425 speckind sk = speckind_fromInt (i);
2429 case SR_NOTHING: return (sRef_makeNothing ());
2430 case SR_INTERNAL: return (sRef_makeInternalState ());
2431 case SR_SPECSTATE: return (sRef_makeSpecState ());
2432 case SR_SYSTEM: return (sRef_makeSystemState ());
2433 case SR_GLOBALMARKER: BADBRANCH;
2438 return sRef_undefined;
2440 return sRef_makeUnknown ();
2442 return sRef_makeUnknown ();
2444 llfatalerror (message ("sRef_undumpGlobal: bad line: %s",
2445 cstring_fromChars (*c)));
2450 static /*@exposed@*/ sRef sRef_undumpBody (char **c)
2459 return (sRef_makeGlobal (usymId_fromInt (reader_getInt (c)),
2460 ctype_unknown, stateInfo_currentLoc ()));
2462 return (sRef_makeParam (reader_getInt (c), ctype_unknown,
2463 stateInfo_makeLoc (g_currentloc, SA_DECLARED)));
2465 return (sRef_makeResult (ctype_undump (c)));
2468 if ((**c >= '0' && **c <= '9') || **c == '-')
2470 int i = reader_getInt (c);
2471 sRef arr = sRef_undump (c);
2472 sRef ret = sRef_buildArrayFetchKnown (arr, i);
2478 sRef arr = sRef_undump (c);
2479 sRef ret = sRef_buildArrayFetch (arr);
2486 cstring fname = cstring_undefined;
2491 fname = cstring_appendChar (fname, **c);
2496 ret = sRef_buildField (sRef_undump (c), fname);
2497 cstring_markOwned (fname);
2502 int i = reader_getInt (c);
2503 speckind sk = speckind_fromInt (i);
2507 case SR_NOTHING: return (sRef_makeNothing ());
2508 case SR_INTERNAL: return (sRef_makeInternalState ());
2509 case SR_SPECSTATE: return (sRef_makeSpecState ());
2510 case SR_SYSTEM: return (sRef_makeSystemState ());
2511 case SR_GLOBALMARKER: BADBRANCH;
2517 sRef ptr = sRef_undump (c);
2518 sRef ret = sRef_makePointer (ptr);
2524 sRef adr = sRef_undump (c);
2525 sRef ret = sRef_makeAddress (adr);
2531 return (sRef_makeObject (ctype_undump (c)));
2535 sRef s1 = sRef_undump (c);
2536 sRef s2 = ((*c)++, sRef_undump (c));
2537 sRef ret = sRef_makeConj (s1, s2);
2542 return sRef_undefined;
2544 return sRef_makeUnknown ();
2546 return sRef_makeUnknown ();
2548 llfatalerror (message ("sRef_undump: bad line: %s", cstring_fromChars (*c)));
2553 /*@exposed@*/ sRef sRef_undump (char **c)
2555 sRef res = sRef_undumpBody (c);
2557 if (reader_optCheckChar (c, '='))
2559 multiVal mv = multiVal_undump (c);
2560 sRef_setValue (res, mv);
2561 reader_checkChar (c, '=');
2567 static /*@only@*/ cstring sRef_dumpBody (sRef s)
2569 if (sRef_isInvalid (s))
2571 return (cstring_makeLiteral ("-"));
2578 return (message ("p%d", s->info->paramno));
2580 if (s->info->arrayfetch->indknown)
2582 return (message ("a%d%q", s->info->arrayfetch->ind,
2583 sRef_dump (s->info->arrayfetch->arr)));
2587 return (message ("a%q", sRef_dump (s->info->arrayfetch->arr)));
2590 return (message ("f%s.%q", s->info->field->field,
2591 sRef_dump (s->info->field->rec)));
2593 return (message ("t%q", sRef_dump (s->info->ref)));
2595 return (message ("d%q", sRef_dump (s->info->ref)));
2597 return (message ("o%q", ctype_dump (s->info->object)));
2599 return (message ("s%d", (int) s->info->spec));
2601 return (message ("c%q.%q",
2602 sRef_dump (s->info->conj->a),
2603 sRef_dump (s->info->conj->b)));
2605 if (sRef_isFileOrGlobalScope (s))
2607 return (message ("g%d",
2608 usymtab_convertId (s->info->cvar->index)));
2612 llcontbug (message ("Dumping local variable: %q",
2613 sRef_unparseDebug (s)));
2614 return (cstring_makeLiteral ("u"));
2617 return (cstring_makeLiteral ("u"));
2619 return (message ("r%q", ctype_dump (s->type)));
2625 case SK_UNCONSTRAINED:
2626 llcontbug (message ("sRef_dump: bad kind: %q",
2627 sRef_unparseFull (s)));
2628 return (cstring_makeLiteral ("x"));
2635 /*@only@*/ cstring sRef_dump (sRef s)
2637 cstring res = sRef_dumpBody (s);
2639 if (sRef_hasValue (s))
2641 res = message ("%q=%q=", res, multiVal_dump (sRef_getValue (s)));
2647 cstring sRef_dumpGlobal (sRef s)
2649 llassert (!sRef_hasValue (s));
2651 if (sRef_isInvalid (s))
2653 return (cstring_makeLiteral ("-"));
2660 if (sRef_isFileOrGlobalScope (s))
2662 return (message ("g%d@%d@%d",
2663 usymtab_convertId (s->info->cvar->index),
2665 (int) sRef_getNullState (s)));
2669 llcontbug (message ("Dumping local variable: %q",
2670 sRef_unparseDebug (s)));
2671 return (cstring_makeLiteral ("u"));
2674 return (cstring_makeLiteral ("u"));
2676 return (message ("s%d", (int) s->info->spec));
2678 llcontbug (message ("sRef_dumpGlobal: bad kind: %q",
2679 sRef_unparseFull (s)));
2680 return (cstring_makeLiteral ("x"));
2688 sRef_deriveType (sRef s, uentryList cl)
2690 if (sRef_isInvalid (s)) return ctype_unknown;
2695 return (uentry_getType (usymtab_getRefQuiet (s->info->cvar->lexlevel,
2696 s->info->cvar->index)));
2697 case SK_UNCONSTRAINED:
2698 return (ctype_unknown);
2700 if (s->info->paramno >= 0)
2702 return uentry_getType (uentryList_getN (cl, s->info->paramno));
2706 return ctype_unknown;
2710 ctype ca = sRef_deriveType (s->info->arrayfetch->arr, cl);
2712 if (ctype_isArray (ca))
2714 return (ctype_baseArrayPtr (ca));
2716 else if (ctype_isUnknown (ca))
2722 llcontbuglit ("sRef_deriveType: inconsistent array type");
2728 ctype ct = sRef_deriveType (s->info->field->rec, cl);
2730 if (ctype_isStructorUnion (ct))
2732 uentry ue = uentryList_lookupField (ctype_getFields (ct),
2733 s->info->field->field);
2735 if (uentry_isValid (ue))
2737 return (uentry_getType (ue));
2741 llcontbuglit ("sRef_deriveType: bad field");
2742 return ctype_unknown;
2745 else if (ctype_isUnknown (ct))
2751 llcontbuglit ("sRef_deriveType: inconsistent field type");
2757 ctype ct = sRef_deriveType (s->info->ref, cl);
2759 if (ctype_isUnknown (ct)) return ct;
2760 if (ctype_isPointer (ct)) return (ctype_baseArrayPtr (ct));
2763 llcontbuglit ("sRef_deriveType: inconsistent pointer type");
2769 ctype ct = sRef_deriveType (s->info->ref, cl);
2771 if (ctype_isUnknown (ct)) return ct;
2772 return ctype_makePointer (ct);
2776 return sRef_deriveType (s->info->ref, cl);
2780 return (s->info->object);
2784 return (ctype_makeConj (sRef_deriveType (s->info->conj->a, cl),
2785 sRef_deriveType (s->info->conj->b, cl)));
2797 return ctype_unknown;
2803 sRef_getType (sRef s)
2805 if (sRef_isInvalid (s)) return ctype_unknown;
2811 sRef_unparseOpt (sRef s)
2813 sRef rb = sRef_getRootBase (s);
2815 if (sRef_isMeaningful (rb) && !sRef_isConst (rb))
2817 cstring ret = sRef_unparse (s);
2819 llassertprint (!cstring_equalLit (ret, "?"), ("print: %s", sRef_unparseDebug (s)));
2821 if (!cstring_isEmpty (ret))
2823 return (cstring_appendChar (ret, ' '));
2831 return cstring_undefined;
2835 sRef_unparsePreOpt (sRef s)
2837 sRef rb = sRef_getRootBase (s);
2839 if (sRef_isMeaningful (rb) && !sRef_isConst (rb))
2841 cstring ret = sRef_unparse (s);
2843 llassertprint (!cstring_equalLit (ret, "?"), ("print: %s", sRef_unparseDebug (s)));
2844 return (cstring_prependCharO (' ', ret));
2847 return cstring_undefined;
2851 sRef_unparse (sRef s)
2853 if (sRef_isInvalid (s)) return (cstring_makeLiteral ("?"));
2855 if (context_inFunctionLike ())
2857 return (sRef_unparseWithArgs (s, context_getParams ()));
2861 DPRINTF (("Not in function like: %s", context_unparse ()));
2862 return (sRef_unparseNoArgs (s));
2866 static /*@only@*/ cstring
2867 sRef_unparseWithArgs (sRef s, uentryList args)
2869 if (sRef_isInvalid (s))
2871 return (cstring_makeLiteral ("?"));
2877 return (uentry_getName (usymtab_getRefQuiet (s->info->cvar->lexlevel,
2878 s->info->cvar->index)));
2879 case SK_UNCONSTRAINED:
2880 return (cstring_copy (s->info->fname));
2883 if (s->info->paramno < uentryList_size (args)
2884 && s->info->paramno >= 0)
2886 uentry ue = uentryList_getN (args, s->info->paramno);
2888 if (uentry_isValid (ue))
2889 return uentry_getName (ue);
2892 return (message ("parameter %d", s->info->paramno + 1));
2895 if (s->info->arrayfetch->indknown)
2897 return (message ("%q[%d]", sRef_unparseWithArgs (s->info->arrayfetch->arr, args),
2898 s->info->arrayfetch->ind));
2902 return (message ("%q[]", sRef_unparseWithArgs (s->info->arrayfetch->arr, args)));
2905 if (s->info->field->rec->kind == SK_PTR)
2907 sRef ptr = s->info->field->rec;
2909 return (message ("%q->%s", sRef_unparseWithArgs (ptr->info->ref, args),
2910 s->info->field->field));
2912 return (message ("%q.%s", sRef_unparseWithArgs (s->info->field->rec, args),
2913 s->info->field->field));
2917 sRef ref = sRef_fixConj (s->info->ref);
2918 skind sk = ref->kind;
2923 ret = message ("storage pointed to by %q",
2924 sRef_unparseWithArgs (ref, args));
2926 else if (skind_isSimple (sk) || sk == SK_PTR)
2928 ret = message ("*%q", sRef_unparseWithArgs (ref, args));
2932 ret = message ("*(%q)", sRef_unparseWithArgs (ref, args));
2938 return (message ("&%q", sRef_unparseWithArgs (s->info->ref, args)));
2940 return (cstring_copy (ctype_unparse (s->info->object)));
2942 return (sRef_unparseWithArgs (sRef_getConjA (s), args));
2944 if (cstring_isDefined (s->info->fname))
2946 return (message ("[result of %s]", s->info->fname));
2950 return (cstring_makeLiteral ("<new>"));
2953 return (cstring_makeLiteral ("?"));
2955 return (message ("<derived %q>", sRef_unparse (s->info->ref)));
2957 return (message ("<external %q>", sRef_unparse (s->info->ref)));
2959 return (message ("<type %s>", ctype_unparse (s->type)));
2961 return (message ("<const %s>", ctype_unparse (s->type)));
2963 switch (s->info->spec)
2965 case SR_NOTHING: return cstring_makeLiteral ("nothing");
2966 case SR_INTERNAL: return cstring_makeLiteral ("internal state");
2967 case SR_SPECSTATE: return cstring_makeLiteral ("spec state");
2968 case SR_SYSTEM: return cstring_makeLiteral ("file system state");
2969 case SR_GLOBALMARKER: return cstring_makeLiteral ("<global marker>");
2973 return cstring_makeLiteral ("result");
2976 llbug (message ("Bad sref, kind = %d", (int) s->kind));
2984 sRef_unparseDebug (sRef s)
2986 if (sRef_isInvalid (s))
2988 return (cstring_makeLiteral ("<undef>"));
2994 case SK_UNCONSTRAINED:
2995 return (message ("<unconstrained %s>", s->info->fname));
3000 ce = usymtab_getRefQuiet (s->info->cvar->lexlevel, s->info->cvar->index);
3002 if (uentry_isInvalid (ce))
3004 return (message ("<scope: %d.%d *invalid*>",
3005 s->info->cvar->lexlevel,
3006 s->info->cvar->index));
3010 return (message ("<scope: %d.%d *%q*>",
3011 s->info->cvar->lexlevel,
3012 s->info->cvar->index,
3013 uentry_getName (ce)));
3019 return (message ("<parameter %d>", s->info->paramno + 1));
3022 if (s->info->arrayfetch->indknown)
3024 return (message ("%q[%d]", sRef_unparseDebug (s->info->arrayfetch->arr),
3025 s->info->arrayfetch->ind));
3029 return (message ("%q[]", sRef_unparseDebug (s->info->arrayfetch->arr)));
3032 return (message ("%q.%s", sRef_unparseDebug (s->info->field->rec),
3033 s->info->field->field));
3035 if (sRef_isField (s->info->ref))
3037 sRef fld = s->info->ref;
3039 return (message ("%q->%s", sRef_unparseDebug (fld->info->field->rec),
3040 fld->info->field->field));
3044 return (message ("*(%q)", sRef_unparseDebug (s->info->ref)));
3047 return (message ("&%q", sRef_unparseDebug (s->info->ref)));
3049 return (message ("<object type %s>", ctype_unparse (s->info->object)));
3051 return (message ("%q | %q", sRef_unparseDebug (s->info->conj->a),
3052 sRef_unparseDebug (s->info->conj->b)));
3054 return message ("<new: %s>", s->info->fname);
3056 return (message ("<derived %q>", sRef_unparseDebug (s->info->ref)));
3058 return (message ("<external %q>", sRef_unparseDebug (s->info->ref)));
3060 return (message ("<type %s>", ctype_unparse (s->type)));
3062 if (sRef_hasValue (s))
3064 return (message ("<const %s=%q>", ctype_unparse (s->type), multiVal_unparse (sRef_getValue (s))));
3068 return (message ("<const %s>", ctype_unparse (s->type)));
3071 return (message ("<result %s>", ctype_unparse (s->type)));
3073 return (message ("<spec %s>",
3074 cstring_makeLiteralTemp
3075 (s->info->spec == SR_NOTHING ? "nothing"
3076 : s->info->spec == SR_INTERNAL ? "internalState"
3077 : s->info->spec == SR_SPECSTATE ? "spec state"
3078 : s->info->spec == SR_SYSTEM ? "fileSystem"
3081 return cstring_makeLiteral ("<unknown>");
3087 static /*@only@*/ cstring
3088 sRef_unparseNoArgs (sRef s)
3090 if (sRef_isInvalid (s)) return (cstring_makeLiteral ("?"));
3094 case SK_UNCONSTRAINED:
3095 return (cstring_copy (s->info->fname));
3098 uentry ce = usymtab_getRefQuiet (s->info->cvar->lexlevel,
3099 s->info->cvar->index);
3101 if (uentry_isInvalid (ce))
3103 llcontbug (message ("sRef_unparseNoArgs: bad cvar: %q",
3104 sRef_unparseDebug (s)));
3105 return (sRef_unparseDebug (s));
3109 return (uentry_getName (ce));
3113 if (s->info->arrayfetch->indknown)
3115 return (message ("%q[%d]", sRef_unparseNoArgs (s->info->arrayfetch->arr),
3116 s->info->arrayfetch->ind));
3120 return (message ("%q[]", sRef_unparseNoArgs (s->info->arrayfetch->arr)));
3123 return (message ("%q.%s", sRef_unparseNoArgs (s->info->field->rec),
3124 s->info->field->field));
3127 sRef ref = sRef_fixConj (s->info->ref);
3128 skind sk = ref->kind;
3131 if (skind_isSimple (sk) || sk == SK_PTR)
3133 ret = message ("*%q", sRef_unparseNoArgs (ref));
3137 ret = message ("*(%q)", sRef_unparseNoArgs (ref));
3143 return (message ("&%q", sRef_unparseNoArgs (s->info->ref)));
3145 return (cstring_copy (ctype_unparse (s->info->object)));
3147 return (sRef_unparseNoArgs (s->info->conj->a));
3149 return (message ("result of %s", s->info->fname));
3151 return (message ("<der %q>", sRef_unparseNoArgs (s->info->ref)));
3153 return message ("<ext %q>", sRef_unparseNoArgs (s->info->ref));
3155 return (cstring_makeLiteral
3156 (s->info->spec == SR_NOTHING ? "nothing"
3157 : s->info->spec == SR_INTERNAL ? "internal state"
3158 : s->info->spec == SR_SPECSTATE ? "spec state"
3159 : s->info->spec == SR_SYSTEM ? "file system state"
3162 return cstring_makeLiteral ("result");
3166 return cstring_makeLiteral ("?");
3168 /* llcontbug (message ("sRef_unparseNoArgs: bad case: %q", sRef_unparseDebug (s))); */
3169 return (sRef_unparseDebug (s));
3174 /*@dependent@*/ sRef sRef_makeUnconstrained (cstring fname)
3176 sRef s = sRef_new ();
3178 s->kind = SK_UNCONSTRAINED;
3179 s->info = (sinfo) dmalloc (sizeof (*s->info));
3180 s->info->fname = cstring_copy (fname); /* evans 2002-07-12: this was exposed, causing memory errors */
3185 cstring sRef_unconstrainedName (sRef s)
3187 llassert (sRef_isUnconstrained (s));
3189 return s->info->fname;
3192 bool sRef_isUnconstrained (sRef s)
3194 return (sRef_isReasonable(s) && s->kind == SK_UNCONSTRAINED);
3197 static /*@dependent@*/ /*@notnull@*/ sRef
3198 sRef_makeCvarAux (int level, usymId index, ctype ct, /*@only@*/ stateInfo stinfo)
3200 sRef s = sRef_newRef ();
3203 s->info = (sinfo) dmalloc (sizeof (*s->info));
3205 s->info->cvar = (cref) dmalloc (sizeof (*s->info->cvar));
3206 s->info->cvar->lexlevel = level;
3207 s->info->cvar->index = index;
3209 /* for now, all globals are defined; all locals, aren't */
3211 if (level <= fileScope)
3213 s->defstate = SS_UNKNOWN;
3217 ctype rct = ctype_realType (ct);
3219 if (level != paramsScope
3220 && (ctype_isStructorUnion (rct) || ctype_isRealArray (rct)))
3222 s->defstate = SS_ALLOCATED;
3223 s->oaliaskind = s->aliaskind = AK_STACK;
3227 s->defstate = SS_UNDEFINED;
3228 s->oaliaskind = s->aliaskind = AK_LOCAL;
3234 llassert (level >= globScope);
3235 llassert (usymId_isValid (index));
3237 DPRINTF (("Made cvar: [%p] %s", s, sRef_unparseDebug (s)));
3238 llassert (valueTable_isUndefined (s->state));
3239 s->state = context_createValueTable (s, stinfo);
3243 /*@dependent@*/ sRef sRef_makeCvar (int level, usymId index, ctype ct, /*@only@*/ stateInfo stinfo)
3245 return (sRef_makeCvarAux (level, index, ct, stinfo));
3248 int sRef_lexLevel (sRef s)
3250 if (sRef_isReasonable (s))
3254 conj = sRef_fixConj (s);
3255 s = sRef_getRootBase (conj);
3257 if (sRef_isReasonable (s) && s->kind == SK_CVAR)
3259 return (s->info->cvar->lexlevel);
3267 sRef_makeGlobal (usymId l, ctype ct, /*@only@*/ stateInfo stinfo)
3269 return (sRef_makeCvar (globScope, l, ct, stinfo));
3273 sRef_setParamNo (sRef s, int l)
3275 llassert (sRef_isReasonable (s) && s->kind == SK_PARAM);
3276 s->info->paramno = l;
3280 /*@dependent@*/ sRef
3281 sRef_makeParam (int l, ctype ct, stateInfo stinfo)
3283 sRef s = sRef_new ();
3288 s->info = (sinfo) dmalloc (sizeof (*s->info));
3289 s->info->paramno = l;
3291 s->defstate = SS_UNKNOWN;
3292 /* (probably defined, unless its an out parameter) */
3294 llassert (valueTable_isUndefined (s->state));
3295 s->state = context_createValueTable (s, stinfo);
3300 sRef_isIndexKnown (sRef arr)
3304 llassert (sRef_isReasonable (arr));
3305 arr = sRef_fixConj (arr);
3307 llassert (arr->kind == SK_ARRAYFETCH);
3308 res = arr->info->arrayfetch->indknown;
3313 sRef_getIndex (sRef arr)
3317 llassert (sRef_isReasonable (arr));
3318 arr = sRef_fixConj (arr);
3320 llassert (arr->kind == SK_ARRAYFETCH);
3322 if (!arr->info->arrayfetch->indknown)
3324 llcontbug (message ("sRef_getIndex: unknown: %q", sRef_unparse (arr)));
3329 result = arr->info->arrayfetch->ind;
3335 static bool sRef_isZerothArrayFetch (/*@notnull@*/ sRef s)
3337 return (s->kind == SK_ARRAYFETCH
3338 && s->info->arrayfetch->indknown
3339 && (s->info->arrayfetch->ind == 0));
3342 /*@exposed@*/ sRef sRef_makeAddress (/*@exposed@*/ sRef t)
3345 if (sRef_isInvalid (t)) return sRef_undefined;
3347 if (sRef_isPointer (t))
3349 return (t->info->ref);
3351 else if (sRef_isZerothArrayFetch (t))
3353 return (t->info->arrayfetch->arr);
3357 sRef s = sRef_newRef ();
3360 s->type = ctype_makePointer (t->type);
3361 s->info = (sinfo) dmalloc (sizeof (*s->info));
3364 if (t->defstate == SS_UNDEFINED)
3365 /* no! it is allocated even still: && !ctype_isPointer (t->type)) */
3367 s->defstate = SS_ALLOCATED;
3371 s->defstate = t->defstate;
3374 if (t->aliaskind == AK_LOCAL)
3376 if (sRef_isLocalVar (t))
3378 s->aliaskind = AK_STACK;
3382 llassert (valueTable_isUndefined (s->state));
3383 s->state = context_createValueTable (s,
3384 stateInfo_makeLoc (g_currentloc, SA_CREATED));
3389 cstring sRef_getField (sRef s)
3393 llassert (sRef_isReasonable (s));
3394 s = sRef_fixConj (s);
3396 llassertprint (sRef_isReasonable (s) && (s->kind == SK_FIELD),
3397 ("s = %s", sRef_unparseDebug (s)));
3399 res = s->info->field->field;
3403 sRef sRef_getBase (sRef s)
3407 if (sRef_isInvalid (s)) return (sRef_undefined);
3409 s = sRef_fixConj (s);
3420 res = s->info->field->rec;
3424 res = s->info->arrayfetch->arr;
3428 res = sRef_undefined; /* shouldn't need it */
3435 ** same as getBase, except returns invalid
3436 ** (and doesn't use adr's)
3440 sRef_getBaseSafe (sRef s)
3444 if (sRef_isInvalid (s)) { return sRef_undefined; }
3446 s = sRef_fixConj (s);
3454 res = s->info->field->rec; break;
3456 res = s->info->arrayfetch->arr;
3459 res = sRef_undefined; break;
3465 /*@constant int MAXBASEDEPTH;@*/
3466 # define MAXBASEDEPTH 25
3468 static /*@exposed@*/ sRef
3469 sRef_getRootBaseAux (sRef s, int depth)
3471 if (sRef_isInvalid (s)) return sRef_undefined;
3473 if (depth > MAXBASEDEPTH)
3476 ("Warning: reference base limit exceeded for %q. "
3477 "This either means there is a variable with at least "
3478 "%d indirections from this reference, or "
3479 "there is a bug in Splint.",
3484 return sRef_undefined;
3491 return (sRef_getRootBaseAux (s->info->ref, depth + 1));
3493 return (sRef_getRootBaseAux (s->info->field->rec, depth + 1));
3495 return (sRef_getRootBaseAux (s->info->arrayfetch->arr, depth + 1));
3497 return (sRef_getRootBaseAux (sRef_fixConj (s), depth + 1));
3503 sRef sRef_getRootBase (sRef s)
3505 return (sRef_getRootBaseAux (s, 0));
3508 static bool sRef_isDeep (sRef s)
3510 if (sRef_isInvalid (s)) return FALSE;
3520 return (sRef_isDeep (sRef_fixConj (s)));
3526 static int sRef_depth (sRef s)
3528 if (sRef_isInvalid (s)) return 0;
3536 return 1 + sRef_depth (s->info->ref);
3538 return 1 + sRef_depth (s->info->field->rec);
3540 return 1 + sRef_depth (s->info->arrayfetch->arr);
3542 return (sRef_depth (sRef_fixConj (s)));
3549 sRef_makeObject (ctype o)
3551 sRef s = sRef_newRef ();
3553 s->kind = SK_OBJECT;
3554 s->info = (sinfo) dmalloc (sizeof (*s->info));
3555 s->info->object = o;
3556 llassert (valueTable_isUndefined (s->state));
3557 s->state = context_createValueTable (s, stateInfo_makeLoc (g_currentloc, SA_CREATED));
3562 ** This is used to represent storage referenced by a parameter.
3565 sRef sRef_makeExternal (sRef t)
3567 sRef s = sRef_newRef ();
3569 llassert (sRef_isReasonable (t));
3571 s->kind = SK_EXTERNAL;
3572 s->info = (sinfo) dmalloc (sizeof (*s->info));
3575 llassert (valueTable_isUndefined (s->state));
3576 s->state = context_createValueTable (s, stateInfo_makeLoc (g_currentloc, SA_DECLARED));
3580 /*@dependent@*/ sRef sRef_makeDerived (/*@exposed@*/ sRef t)
3582 if (sRef_isReasonable (t))
3584 sRef s = sRef_newRef ();
3586 s->kind = SK_DERIVED;
3587 s->info = (sinfo) dmalloc (sizeof (*s->info));
3591 llassert (valueTable_isUndefined (s->state));
3592 s->state = context_createValueTable (s, stateInfo_makeLoc (g_currentloc, SA_CREATED));
3597 return sRef_undefined;
3602 ** definitely NOT symmetric:
3604 ** res fills in unknown state information from other
3608 sRef_mergeStateQuiet (sRef res, sRef other)
3610 llassert (sRef_isReasonable (res));
3611 llassert (sRef_isReasonable (other));
3613 res->modified = res->modified || other->modified;
3614 res->safe = res->safe && other->safe;
3616 if (res->defstate == SS_UNKNOWN)
3618 res->defstate = other->defstate;
3619 res->definfo = stateInfo_update (res->definfo, other->definfo);
3622 if (res->aliaskind == AK_UNKNOWN ||
3623 (res->aliaskind == AK_LOCAL && alkind_isKnown (other->aliaskind)))
3625 res->aliaskind = other->aliaskind;
3626 res->oaliaskind = other->oaliaskind;
3627 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
3630 if (res->expkind == XO_UNKNOWN)
3632 res->expkind = other->expkind;
3633 res->oexpkind = other->oexpkind;
3634 res->expinfo = stateInfo_update (res->expinfo, other->expinfo);
3637 /* out takes precedence over implicitly defined */
3638 if (res->defstate == SS_DEFINED && other->defstate != SS_UNKNOWN)
3640 res->defstate = other->defstate;
3641 res->definfo = stateInfo_update (res->definfo, other->definfo);
3644 if (sRef_getNullState (other) == NS_ERROR || sRef_getNullState (res) == NS_ERROR)
3646 sRef_setNullState (res, NS_ERROR, fileloc_undefined);
3650 if (sRef_getNullState (other) != NS_UNKNOWN
3651 && (sRef_getNullState (res) == NS_UNKNOWN || sRef_getNullState (res) == NS_NOTNULL
3652 || sRef_getNullState (res) == NS_MNOTNULL))
3654 sRef_updateNullState (res, other);
3660 ** definitely NOT symmetric:
3662 ** res fills in known state information from other
3666 sRef_mergeStateQuietReverse (/*@dependent@*/ sRef res, /*@dependent@*/ sRef other)
3668 bool changed = FALSE;
3670 llassert (sRef_isReasonable (res));
3671 llassert (sRef_isReasonable (other));
3672 sRef_checkMutable (res);
3674 if (res->kind != other->kind)
3680 res->kind = other->kind;
3681 res->type = other->type;
3682 res->info = sinfo_fullCopy (other);
3686 if (!ctype_equal (res->type, other->type))
3689 res->type = other->type;
3692 sinfo_update (res, other);
3695 res->modified = res->modified || other->modified;
3696 res->safe = res->safe && other->safe;
3698 if (res->aliaskind != other->aliaskind
3699 && (res->aliaskind == AK_UNKNOWN
3700 || ((res->aliaskind == AK_LOCAL
3701 || (res->aliaskind == AK_REFCOUNTED
3702 && other->aliaskind != AK_LOCAL))
3703 && other->aliaskind != AK_UNKNOWN)))
3706 res->aliaskind = other->aliaskind;
3707 res->oaliaskind = other->oaliaskind;
3708 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
3711 if (other->expkind != XO_UNKNOWN && other->expkind != res->expkind)
3714 res->expkind = other->expkind;
3715 res->expinfo = stateInfo_update (res->expinfo, other->expinfo);
3718 if (other->oexpkind != XO_UNKNOWN)
3720 res->oexpkind = other->oexpkind;
3723 /* out takes precedence over implicitly defined */
3725 if (res->defstate != other->defstate)
3727 if (other->defstate != SS_UNKNOWN)
3729 res->defstate = other->defstate;
3730 res->definfo = stateInfo_update (res->definfo, other->definfo);
3734 if (sRef_getNullState (other) == NS_ERROR || sRef_getNullState (res) == NS_ERROR)
3736 if (sRef_getNullState (res) != NS_ERROR)
3738 sRef_setNullStateN (res, NS_ERROR);
3744 if (sRef_getNullState (other) != NS_UNKNOWN
3745 && sRef_getNullState (other) != sRef_getNullState (res))
3748 sRef_updateNullState (res, other);
3754 sRef_clearDerived (res);
3759 sRef_mergeState (sRef res, sRef other, clause cl, fileloc loc)
3761 if (sRef_isReasonable (res) && sRef_isReasonable (other))
3763 sRef_mergeStateAux (res, other, cl, FALSE, loc, TRUE);
3767 if (sRef_isInvalid (res))
3769 llbug (message ("sRef_mergeState: invalid res sRef: %q",
3770 sRef_unparseDebug (other)));
3774 llbug (message ("sRef_mergeState: invalid other sRef: %q",
3775 sRef_unparseDebug (res)));
3781 sRef_mergeOptState (sRef res, sRef other, clause cl, fileloc loc)
3783 if (sRef_isReasonable (res) && sRef_isReasonable (other))
3785 sRef_mergeStateAux (res, other, cl, TRUE, loc, TRUE);
3789 if (sRef_isInvalid (res))
3791 llbug (message ("sRef_mergeOptState: invalid res sRef: %q",
3792 sRef_unparseDebug (other)));
3796 llbug (message ("sRef_mergeOptState: invalid other sRef: %q",
3797 sRef_unparseDebug (res)));
3803 sRef_mergeStateAux (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other,
3804 clause cl, bool opt, fileloc loc,
3808 llassertfatal (sRef_isReasonable (res));
3809 llassertfatal (sRef_isReasonable (other));
3811 DPRINTF (("Merge aux: %s / %s",
3812 sRef_unparseFull (res),
3813 sRef_unparseFull (other)));
3815 sRef_checkMutable (res);
3816 sRef_checkMutable (other);
3818 res->modified = res->modified || other->modified;
3820 if (res->kind == other->kind
3821 || (other->kind == SK_UNKNOWN || res->kind == SK_UNKNOWN))
3823 sstate odef = other->defstate;
3824 sstate rdef = res->defstate;
3825 nstate onull = sRef_getNullState (other);
3828 ** yucky stuff to handle
3833 if (other->defstate == SS_DEAD
3834 && ((sRef_isOnly (res) && sRef_definitelyNull (res))
3835 || (res->defstate == SS_UNDEFINED
3836 || res->defstate == SS_UNUSEABLE)))
3838 if (res->defstate == SS_UNDEFINED
3839 || res->defstate == SS_UNUSEABLE)
3841 res->defstate = SS_UNUSEABLE;
3845 res->defstate = SS_DEAD;
3848 res->definfo = stateInfo_update (res->definfo, other->definfo);
3849 sRef_clearDerived (other);
3850 sRef_clearDerived (res);
3852 else if (res->defstate == SS_DEAD
3853 && ((sRef_isOnly (other) && sRef_definitelyNull (other))
3854 || (other->defstate == SS_UNDEFINED
3855 || other->defstate == SS_UNUSEABLE)))
3857 if (other->defstate == SS_UNDEFINED
3858 || other->defstate == SS_UNUSEABLE)
3860 res->defstate = SS_UNUSEABLE;
3864 res->defstate = SS_DEAD;
3867 res->definfo = stateInfo_update (res->definfo, other->definfo);
3868 sRef_clearDerived (other);
3869 sRef_clearDerived (res);
3871 else if (res->defstate == SS_DEFINED
3872 && (other->defstate == SS_ALLOCATED
3873 && sRef_definitelyNull (other)))
3875 other->defstate = SS_DEFINED; /* definitely null! */
3877 else if (other->defstate == SS_DEFINED
3878 && (res->defstate == SS_ALLOCATED && sRef_definitelyNull (res)))
3880 res->defstate = SS_DEFINED;
3881 res->definfo = stateInfo_update (res->definfo, other->definfo);
3888 if (res->defstate == SS_DEAD && other->defstate == SS_DEAD)
3890 sRef_clearDerived (other);
3891 sRef_clearDerived (res);
3895 ** only & dead isn't really an only!
3898 if (alkind_isOnly (other->aliaskind) && other->defstate == SS_DEAD)
3900 other->aliaskind = AK_UNKNOWN;
3903 if (alkind_isOnly (res->aliaskind) && res->defstate == SS_DEAD)
3905 res->aliaskind = AK_UNKNOWN;
3909 ** Dead and dependent -> dead
3912 if (alkind_isDependent (other->aliaskind) && res->defstate == SS_DEAD)
3914 other->aliaskind = AK_UNKNOWN;
3915 other->defstate = SS_DEAD;
3916 sRef_clearDerived (res);
3917 sRef_clearDerived (other);
3920 if (alkind_isDependent (res->aliaskind) && other->defstate == SS_DEAD)
3922 res->aliaskind = AK_UNKNOWN;
3923 res->defstate = SS_DEAD;
3924 sRef_clearDerived (res);
3925 sRef_clearDerived (other);
3929 ** must do alias combine first, since it depends on
3930 ** original values of state and null.
3933 sRef_combineAliasKinds (res, other, cl, loc);
3934 sRef_combineDefState (res, other);
3935 sRef_combineNullState (res, other);
3937 if (rdef == SS_ALLOCATED || rdef == SS_SPECIAL)
3939 if (odef == SS_DEFINED)
3941 if (onull == NS_DEFNULL || onull == NS_CONSTNULL)
3943 res->deriv = sRefSet_copyInto (res->deriv, other->deriv);
3944 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
3947 else if (odef == SS_ALLOCATED || odef == SS_SPECIAL)
3952 if (ctype_isUnion (ctype_realType (sRef_getType (res))))
3954 res->deriv = sRef_mergeUnionDerivs (res->deriv,
3957 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
3961 res->deriv = sRef_mergeDerivs (res->deriv, other->deriv,
3963 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
3971 res->deriv = sRef_mergeDerivs (res->deriv, other->deriv,
3973 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
3983 if (rdef == SS_PDEFINED
3984 || (rdef == SS_DEFINED && odef == SS_PDEFINED))
3988 res->deriv = sRef_mergePdefinedDerivs (res->deriv, other->deriv,
3990 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
3995 if ((rdef == SS_DEFINED || rdef == SS_UNKNOWN)
3996 && res->defstate == SS_ALLOCATED)
3998 res->deriv = sRefSet_copyInto (res->deriv, other->deriv);
4004 res->deriv = sRef_mergeDerivs (res->deriv, other->deriv,
4006 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
4013 sRef_combineExKinds (res, other);
4017 if (res->kind == SK_ARRAYFETCH && other->kind == SK_PTR)
4019 sRef nother = sRef_buildArrayFetchKnown (sRef_getBase (other), 0);
4021 sRef_copyState (nother, other);
4022 sRef_mergeStateAux (res, nother, cl, opt, loc, doDerivs);
4024 else if (res->kind == SK_PTR && other->kind == SK_ARRAYFETCH)
4026 sRef nother = sRef_buildPointer (sRef_getBase (other));
4028 if (sRef_isReasonable (nother))
4030 sRef_copyState (nother, other);
4031 sRef_mergeStateAux (res, nother, cl, opt, loc, doDerivs);
4036 llcontbug (message ("merge conj: %q / %q", sRef_unparseFull (res),
4037 sRef_unparseFull (other)));
4043 ** Merge value table states
4048 ** This doesn't do anything. And its broken too...
4051 valueTable_elements (res->state, key, sv)
4053 stateValue os = valueTable_lookup (other->state, key);
4054 /*@unused@*/ int val;
4055 /*@unused@*/ char *msg;
4057 llassert (stateValue_isDefined (os));
4059 DPRINTF (("Merge state: %s / %s",
4060 cstring_toCharsSafe (stateValue_unparse (sv)),
4061 cstring_toCharsSafe (stateValue_unparse (os))));
4063 val = valueMatix_lookup (key,
4064 stateValue_getValue (os),
4065 stateValue_getValue (sv),
4067 DPRINTF (("Val: %d / %s", val, msg));
4069 } end_valueTable_elements ;
4072 DPRINTF (("Merge aux: %s / %s",
4073 sRef_unparseFull (res),
4074 sRef_unparseFull (other)));
4078 sRef_mergeUnionDerivs (/*@only@*/ sRefSet res,
4079 /*@exposed@*/ sRefSet other, bool opt,
4080 clause cl, fileloc loc)
4082 if (sRefSet_isEmpty (res))
4084 return sRefSet_copyInto (res, other);
4088 sRefSet_allElements (other, el)
4090 if (sRef_isReasonable (el))
4092 sRef e2 = sRefSet_lookupMember (other, el);
4094 if (sRef_isReasonable (e2))
4096 sRef_mergeStateAux (el, e2, cl, opt, loc, FALSE);
4100 res = sRefSet_insert (res, el);
4103 } end_sRefSet_allElements ;
4109 static /*@only@*/ sRefSet
4110 sRef_mergeDerivs (/*@only@*/ sRefSet res, sRefSet other,
4111 bool opt, clause cl, fileloc loc)
4113 sRefSet ret = sRefSet_new ();
4115 sRefSet_allElements (res, el)
4117 if (sRef_isReasonable (el))
4119 sRef e2 = sRefSet_lookupMember (other, el);
4121 if (sRef_isReasonable (e2))
4123 if (el->defstate == SS_ALLOCATED &&
4124 e2->defstate == SS_PDEFINED)
4126 e2->defstate = SS_ALLOCATED;
4128 else if (e2->defstate == SS_ALLOCATED &&
4129 el->defstate == SS_PDEFINED)
4131 el->defstate = SS_ALLOCATED;
4132 el->definfo = stateInfo_update (el->definfo, e2->definfo);
4133 sRef_clearDerived (el);
4135 else if ((el->defstate == SS_DEAD || sRef_isKept (el)) &&
4136 (e2->defstate == SS_DEFINED && !sRef_isKept (e2)))
4138 DPRINTF (("Checking dead: %s / %s", sRef_unparseFull (el),
4139 sRef_unparseFull (e2)));
4141 if (checkDeadState (el, e2, TRUE, loc))
4143 if (sRef_isThroughArrayFetch (el))
4145 sRef_maybeKill (el, loc);
4146 sRef_maybeKill (e2, loc);
4150 else if ((e2->defstate == SS_DEAD || sRef_isKept (e2)) &&
4151 (el->defstate == SS_DEFINED && !sRef_isKept (el)))
4153 DPRINTF (("Checking dead: %s / %s", sRef_unparseFull (el),
4154 sRef_unparseFull (e2)));
4156 if (checkDeadState (e2, el, FALSE, loc))
4158 if (sRef_isThroughArrayFetch (el))
4160 sRef_maybeKill (el, loc);
4161 sRef_maybeKill (e2, loc);
4165 else if (el->defstate == SS_DEFINED &&
4166 e2->defstate == SS_PDEFINED)
4168 DPRINTF (("set pdefined: %s", sRef_unparseFull (el)));
4169 el->defstate = SS_PDEFINED;
4170 el->definfo = stateInfo_update (el->definfo, e2->definfo);
4172 else if (e2->defstate == SS_DEFINED &&
4173 el->defstate == SS_PDEFINED)
4175 DPRINTF (("set pdefined: %s", sRef_unparseFull (e2)));
4176 e2->defstate = SS_PDEFINED;
4177 e2->definfo = stateInfo_update (e2->definfo, el->definfo);
4184 if (ctype_isUnion (ctype_realType (sRef_getType (el))))
4186 el->deriv = sRef_mergeUnionDerivs (el->deriv, e2->deriv,
4191 el->deriv = sRef_mergeDerivs (el->deriv, e2->deriv, opt, cl, loc);
4194 if (sRef_equivalent (el, e2))
4196 ret = sRefSet_insert (ret, el);
4200 sRef sr = sRef_leastCommon (el, e2);
4202 if (sRef_isReasonable (sr))
4204 ret = sRefSet_insert (ret, sr);
4212 (void) sRefSet_delete (other, e2);
4214 else /* not defined */
4216 DPRINTF (("Checking dead: %s", sRef_unparseFull (el)));
4217 (void) checkDeadState (el, e2, TRUE, loc);
4220 } end_sRefSet_allElements;
4222 sRefSet_allElements (other, el)
4224 if (sRef_isReasonable (el))
4226 DPRINTF (("Checking dead: %s", sRef_unparseFull (el)));
4227 (void) checkDeadState (el, sRef_undefined, FALSE, loc);
4229 } end_sRefSet_allElements;
4236 ** Returns TRUE is there is an error.
4239 static bool checkDeadState (/*@notnull@*/ sRef el, sRef e2, bool tbranch, fileloc loc)
4242 ** usymtab_isGuarded --- the utab should still be in the
4243 ** state of the alternate branch.
4245 ** tbranch TRUE means el is released in the last branch, e.g.
4246 ** if (x != NULL) { ; } else { sfree (x); }
4247 ** so, if x is null in the other branch no error is reported.
4249 ** tbranch FALSE means this is the other branch:
4250 ** if (x != NULL) { sfree (x); } else { ; }
4251 ** so, if x is null in this branch there is no error.
4255 if ((sRef_isDead (el) || sRef_isKept (el))
4256 && !sRef_isDeepUnionField (el)
4257 && !sRef_isThroughArrayFetch (el))
4262 if (usymtab_isDefinitelyNullDeep (el))
4269 if (usymtab_isAltDefinitelyNullDeep (el))
4277 message ("Storage %q is %q in one path, but live in another.",
4279 cstring_makeLiteral (sRef_isKept (el)
4280 ? "kept" : "released")),
4283 if (sRef_isKept (el))
4285 sRef_showAliasInfo (el);
4289 sRef_showStateInfo (el);
4292 if (sRef_isValid (e2))
4294 if (sRef_isKept (e2))
4296 sRef_showAliasInfo (e2);
4300 sRef_showStateInfo (e2);
4304 /* prevent further errors */
4305 el->defstate = SS_UNKNOWN;
4306 sRef_setAliasKind (el, AK_ERROR, fileloc_undefined);
4315 checkDerivDeadState (/*@notnull@*/ sRef el, bool tbranch, fileloc loc)
4317 if (checkDeadState (el, sRef_undefined, tbranch, loc))
4319 sRefSet_allElements (el->deriv, t)
4321 if (sRef_isReasonable (t))
4323 checkDerivDeadState (t, tbranch, loc);
4325 } end_sRefSet_allElements;
4330 sRef_mergePdefinedDerivs (sRefSet res, sRefSet other, bool opt,
4331 clause cl, fileloc loc)
4333 sRefSet ret = sRefSet_new ();
4335 sRefSet_allElements (res, el)
4337 if (sRef_isReasonable (el))
4339 sRef e2 = sRefSet_lookupMember (other, el);
4341 if (sRef_isReasonable (e2))
4343 if (sRef_isAllocated (el) && !sRef_isAllocated (e2))
4347 else if (sRef_isAllocated (e2) && !sRef_isAllocated (el))
4349 el->deriv = sRefSet_copyInto (el->deriv, e2->deriv);
4353 el->deriv = sRef_mergePdefinedDerivs (el->deriv, e2->deriv,
4357 sRef_mergeStateAux (el, e2, cl, opt, loc, FALSE);
4359 ret = sRefSet_insert (ret, el);
4360 (void) sRefSet_delete (other, e2);
4366 checkDerivDeadState (el, (cl == FALSECLAUSE), loc);
4369 ret = sRefSet_insert (ret, el);
4372 } end_sRefSet_allElements;
4374 sRefSet_allElements (other, el)
4376 if (sRef_isReasonable (el))
4378 if (!sRefSet_member (ret, el))
4380 /* was cl == FALSECLAUSE */
4381 checkDerivDeadState (el, FALSE, loc);
4382 ret = sRefSet_insert (ret, el);
4387 ** it's okay --- member is a different equality test
4391 } end_sRefSet_allElements;
4397 sRef sRef_makeConj (/*@exposed@*/ /*@returned@*/ sRef a, /*@exposed@*/ sRef b)
4399 llassert (sRef_isReasonable (a));
4400 llassert (sRef_isReasonable (b));
4402 if (!sRef_equivalent (a, b))
4404 sRef s = sRef_newRef ();
4407 s->info = (sinfo) dmalloc (sizeof (*s->info));
4408 s->info->conj = (cjinfo) dmalloc (sizeof (*s->info->conj));
4409 s->info->conj->a = a;
4410 s->info->conj->b = b;
4412 if (ctype_equal (a->type, b->type)) s->type = a->type;
4413 else s->type = ctype_makeConj (a->type, b->type);
4415 if (a->defstate == b->defstate)
4417 s->defstate = a->defstate;
4418 s->definfo = stateInfo_update (s->definfo, a->definfo);
4419 s->definfo = stateInfo_update (s->definfo, b->definfo);
4423 s->defstate = SS_UNKNOWN;
4426 sRef_setNullStateN (s, NS_UNKNOWN);
4428 s->safe = a->safe && b->safe;
4429 s->aliaskind = alkind_resolve (a->aliaskind, b->aliaskind);
4431 llassert (valueTable_isUndefined (s->state));
4432 s->state = context_createValueTable (s, stateInfo_makeLoc (g_currentloc, SA_MERGED));
4437 /*@-exposetrans@*/ return a; /*@=exposetrans@*/
4441 /*@dependent@*/ sRef
4444 sRef s = sRef_new ();
4446 s->kind = SK_UNKNOWN;
4450 static /*@owned@*/ /*@notnull@*/ sRef
4451 sRef_makeSpecial (speckind sk) /*@*/
4453 sRef s = sRef_new ();
4455 s->kind = SK_SPECIAL;
4456 s->info = (sinfo) dmalloc (sizeof (*s->info));
4458 /*@-dependenttrans@*/
4460 /*@=dependenttrans@*/
4463 static /*@owned@*/ sRef srnothing = sRef_undefined;
4464 static /*@owned@*/ sRef srinternal = sRef_undefined;
4465 static /*@owned@*/ sRef srsystem = sRef_undefined;
4466 static /*@owned@*/ sRef srspec = sRef_undefined;
4468 /*@dependent@*/ sRef
4469 sRef_makeNothing (void)
4471 if (sRef_isInvalid (srnothing))
4473 srnothing = sRef_makeSpecial (SR_NOTHING);
4480 sRef_makeInternalState (void)
4482 if (sRef_isInvalid (srinternal))
4484 srinternal = sRef_makeSpecial (SR_INTERNAL);
4491 sRef_makeSpecState (void)
4493 if (sRef_isInvalid (srspec))
4495 srspec = sRef_makeSpecial (SR_SPECSTATE);
4502 sRef_makeSystemState (void)
4504 if (sRef_isInvalid (srsystem))
4506 srsystem = sRef_makeSpecial (SR_SYSTEM);
4513 sRef_makeGlobalMarker (void)
4515 sRef s = sRef_makeSpecial (SR_GLOBALMARKER);
4516 llassert (valueTable_isUndefined (s->state));
4517 s->state = context_createGlobalMarkerValueTable (stateInfo_undefined);
4522 sRef_makeResult (ctype c)
4524 sRef s = sRef_newRef ();
4526 s->kind = SK_RESULT;
4528 s->defstate = SS_UNKNOWN;
4529 s->aliaskind = AK_UNKNOWN;
4530 sRef_setNullStateN (s, NS_UNKNOWN);
4531 llassert (valueTable_isUndefined (s->state));
4532 s->state = context_createValueTable (s, stateInfo_makeLoc (g_currentloc, SA_DECLARED));
4534 DPRINTF (("Result: [%p] %s", s, sRef_unparseFull (s)));
4540 sRef_isNothing (sRef s)
4542 return (sRef_isKindSpecial (s) && s->info->spec == SR_NOTHING);
4546 sRef_isInternalState (sRef s)
4548 return (sRef_isKindSpecial (s) && s->info->spec == SR_INTERNAL);
4552 sRef_isSpecInternalState (sRef s)
4554 return (sRef_isKindSpecial (s)
4555 && (s->info->spec == SR_INTERNAL || s->info->spec == SR_SPECSTATE));
4559 sRef_isSpecState (sRef s)
4561 return (sRef_isKindSpecial (s) && s->info->spec == SR_SPECSTATE);
4565 sRef_isResult (sRef s)
4567 return (sRef_isReasonable (s) && s->kind == SK_RESULT);
4571 sRef_isSystemState (sRef s)
4573 return (sRef_isKindSpecial (s) && s->info->spec == SR_SYSTEM);
4577 sRef_isGlobalMarker (sRef s)
4579 return (sRef_isKindSpecial (s) && s->info->spec == SR_GLOBALMARKER);
4583 sRef_getScopeIndex (sRef s)
4585 llassert (sRef_isReasonable (s));
4586 llassert (sRef_isCvar (s));
4588 return (s->info->cvar->index);
4592 sRef_makeSafe (sRef s)
4594 if (sRef_isReasonable (s))
4601 sRef_makeUnsafe (sRef s)
4603 if (sRef_isReasonable (s))
4610 ** memory state operations
4613 /*@only@*/ cstring sRef_unparseFull (sRef s)
4615 if (sRef_isInvalid (s)) return (cstring_undefined);
4617 return (message ("[%p] %q - %q { %q } [%s] { %q } < %q >",
4619 sRef_unparseDebug (s),
4620 sRef_unparseState (s),
4621 stateInfo_unparse (s->definfo),
4622 exkind_unparse (s->oexpkind),
4623 sRefSet_unparseDebug (s->deriv),
4624 valueTable_unparse (s->state)));
4627 /*@unused@*/ cstring sRef_unparseDeep (sRef s)
4629 cstring st = cstring_undefined;
4631 st = message ("%q:", sRef_unparseFull (s));
4633 if (sRef_isReasonable (s))
4635 sRefSet_allElements (s->deriv, el)
4637 st = message("%q\n%q", st, sRef_unparseDeep (el));
4638 } end_sRefSet_allElements ;
4644 /*@only@*/ cstring sRef_unparseState (sRef s)
4646 if (sRef_isConj (s))
4648 return (message ("%q | %q",
4649 sRef_unparseState (s->info->conj->a),
4650 sRef_unparseState (s->info->conj->b)));
4653 if (sRef_isInvalid (s))
4655 return (cstring_makeLiteral ("<invalid>"));
4658 return (message ("%s.%s.%s.%s",
4659 alkind_unparse (s->aliaskind),
4660 nstate_unparse (sRef_getNullState (s)),
4661 exkind_unparse (s->expkind),
4662 sstate_unparse (s->defstate)));
4665 bool sRef_isNotUndefined (sRef s)
4667 return (sRef_isInvalid (s)
4668 || (s->defstate != SS_UNDEFINED
4669 && s->defstate != SS_UNUSEABLE
4670 && s->defstate != SS_DEAD));
4673 ynm sRef_isWriteable (sRef s)
4675 if (sRef_isInvalid (s)) return MAYBE;
4677 if (sRef_isConj (s) && s->defstate == SS_UNKNOWN)
4679 if (ynm_toBoolStrict (sRef_isWriteable (sRef_getConjA (s))))
4681 if (ynm_toBoolStrict (sRef_isWriteable (sRef_getConjB (s))))
4689 if (ynm_toBoolStrict (sRef_isWriteable (sRef_getConjB (s))))
4697 return (ynm_fromBool (s->defstate != SS_UNUSEABLE));
4700 bool sRef_hasNoStorage (sRef s)
4702 return (!sRef_isAllocatedStorage (s) || sRef_isDefinitelyNull (s));
4705 bool sRef_isStrictReadable (sRef s)
4707 return (ynm_toBoolStrict (sRef_isValidLvalue (s)));
4711 ** Is this what is does?
4712 ** Returns YES if s can be used as an rvalue,
4713 ** MAYBE if its not clear
4714 ** NO if s cannot be safely used as an rvalue.
4717 ynm sRef_isValidLvalue (sRef s)
4721 if (sRef_isInvalid (s)) return YES;
4725 if (sRef_isConj (s) && s->defstate == SS_UNKNOWN)
4727 if (ynm_toBoolStrict (sRef_isValidLvalue (sRef_getConjA (s))))
4729 if (ynm_toBoolStrict (sRef_isValidLvalue (sRef_getConjB (s))))
4737 if (ynm_toBoolStrict (sRef_isValidLvalue (sRef_getConjB (s))))
4744 else if (ss == SS_HOFFA)
4746 if (context_getFlag (FLG_STRICTUSERELEASED))
4757 return (ynm_fromBool (ss == SS_DEFINED
4760 || ss == SS_PDEFINED
4763 || ss == SS_ALLOCATED
4764 || ss == SS_KILLED /* evans 2001-05-26: added this for killed globals */
4765 || ss == SS_UNKNOWN));
4769 static /*@exposed@*/ sRef whatUndefined (/*@exposed@*/ sRef fref, int depth)
4774 if (depth > MAXDEPTH)
4777 ("Warning: check definition limit exceeded, checking %q. "
4778 "This either means there is a variable with at least "
4779 "%d indirections apparent in the program text, or "
4780 "there is a bug in Splint.",
4781 sRef_unparse (fref),
4785 return sRef_undefined;
4788 if (!sRef_isKnown (fref) || sRef_isAnyDefined (fref))
4790 return sRef_undefined;
4793 if (sRef_isUnuseable (fref) || sRef_isStateUndefined (fref))
4798 ct = ctype_realType (sRef_getType (fref));
4800 if (ctype_isUnknown (ct))
4802 return sRef_undefined;
4804 else if (ctype_isPointer (ct) || ctype_isArray (ct))
4806 if (sRef_isStateUnknown (fref))
4808 return sRef_undefined;
4812 sRef fptr = sRef_constructDeref (fref);
4814 return (whatUndefined (fptr, depth + 1));
4817 else if (ctype_isStruct (ct))
4819 bool hasOneDefined = FALSE;
4821 if (sRef_isStateUnknown (fref))
4826 if (sRef_isPdefined (fref) || sRef_isAnyDefined (fref))
4828 sRefSet_realElements (sRef_derivedFields (fref), sr)
4830 hasOneDefined = TRUE;
4832 if (sRef_isField (sr))
4834 cstring fieldname = sRef_getField (sr);
4835 sRef fldref = sRef_makeField (fref, fieldname);
4836 bool shouldCheck = !sRef_isRecursiveField (fldref);
4840 sRef wdef = whatUndefined (fldref, depth + 1);
4842 if (sRef_isReasonable (wdef))
4848 } end_sRefSet_realElements;
4850 else if (sRef_isAllocated (fref))
4853 ** for structures, each field must be completely defined
4856 uentryList fields = ctype_getFields (ct);
4858 uentryList_elements (fields, ue)
4860 cstring name = uentry_getRealName (ue);
4861 sRef ffield = sRef_makeField (fref, name);
4862 bool shouldCheck = !sRef_isRecursiveField (ffield);
4864 if (sRef_isRelDef (uentry_getSref (ue)))
4872 sRef wdef = whatUndefined (ffield, depth + 1);
4874 if (sRef_isInvalid (wdef))
4880 } end_uentryList_elements;
4887 else if (ctype_isUnion (ct))
4896 return sRef_undefined;
4899 static bool checkDefined (/*@temp@*/ sRef sr)
4901 /*@-temptrans@*/ /* the result from whatUndefined is lost */
4902 return (sRef_isInvalid (whatUndefined (sr, 0)));
4906 bool sRef_isReallyDefined (sRef s)
4908 if (sRef_isReasonable (s))
4910 if (sRef_isAnyDefined (s))
4916 if (sRef_isAllocated (s) || sRef_isPdefined (s))
4918 return checkDefined (s);
4932 void sRef_showNotReallyDefined (sRef s)
4934 if (sRef_isReasonable (s))
4936 if (sRef_isAnyDefined (s))
4942 if (sRef_isAllocated (s) || sRef_isPdefined (s))
4944 /*@-temptrans@*/ /* the result of whatUndefined is lost */
4945 sRef ref = whatUndefined (s, 0);
4947 llassert (sRef_isReasonable (ref));
4952 (message ("This sub-reference is %s: %q",
4953 sstate_unparse (sRef_getDefState (ref)),
4954 sRef_unparse (ref)));
4969 sstate sRef_getDefState (sRef s)
4971 if (sRef_isInvalid (s)) return (SS_UNKNOWN);
4972 return (s->defstate);
4975 void sRef_setDefState (sRef s, sstate defstate, fileloc loc)
4977 sRef_checkMutable (s);
4978 sRef_setStateAux (s, defstate, loc);
4981 static void sRef_clearAliasStateAux (sRef s, fileloc loc)
4983 sRef_checkMutable (s);
4984 sRef_setAliasKind (s, AK_ERROR, loc);
4987 void sRef_clearAliasState (sRef s, fileloc loc)
4989 sRef_checkMutable (s);
4990 sRef_aliasSetComplete (sRef_clearAliasStateAux, s, loc);
4993 void sRef_setAliasKindComplete (sRef s, alkind kind, fileloc loc)
4995 sRef_checkMutable (s);
4996 sRef_aliasSetCompleteAlkParam (sRef_setAliasKind, s, kind, loc);
4999 void sRef_setAliasKind (sRef s, alkind kind, fileloc loc)
5001 sRef_checkMutable (s);
5003 if (sRef_isReasonable (s))
5005 sRef_clearDerived (s);
5007 if ((kind != s->aliaskind && kind != s->oaliaskind)
5008 && fileloc_isDefined (loc))
5010 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, stateAction_fromAlkind (kind), loc);
5013 s->aliaskind = kind;
5017 void sRef_setOrigAliasKind (sRef s, alkind kind)
5019 sRef_checkMutable (s);
5021 if (sRef_isReasonable (s))
5023 s->oaliaskind = kind;
5027 exkind sRef_getExKind (sRef s)
5029 if (sRef_isReasonable (s))
5031 return (s->expkind);
5039 exkind sRef_getOrigExKind (sRef s)
5041 if (sRef_isReasonable (s))
5043 return (s->oexpkind);
5051 static void sRef_clearExKindAux (sRef s, fileloc loc)
5053 sRef_checkMutable (s);
5054 sRef_setExKind (s, XO_UNKNOWN, loc);
5057 void sRef_setObserver (sRef s, fileloc loc)
5059 sRef_checkMutable (s);
5060 sRef_setExKind (s, XO_OBSERVER, loc);
5063 void sRef_setExposed (sRef s, fileloc loc)
5065 sRef_checkMutable (s);
5066 sRef_setExKind (s, XO_EXPOSED, loc);
5069 void sRef_clearExKindComplete (sRef s, fileloc loc)
5071 (void) sRef_aliasSetComplete (sRef_clearExKindAux, s, loc);
5074 void sRef_setExKind (sRef s, exkind exp, fileloc loc)
5076 sRef_checkMutable (s);
5078 if (sRef_isReasonable (s))
5080 if (s->expkind != exp)
5082 s->expinfo = stateInfo_updateLoc (s->expinfo, stateAction_fromExkind (exp), loc);
5090 ** s1->derived = s2->derived
5093 static void sRef_copyRealDerived (sRef s1, sRef s2)
5095 DPRINTF (("Copy real: %s / %s", sRef_unparse (s1), sRef_unparse (s2)));
5096 sRef_checkMutable (s1);
5098 if (sRef_isReasonable (s1) && sRef_isReasonable (s2))
5100 sRef sb = sRef_getRootBase (s1);
5102 sRefSet_clear (s1->deriv);
5104 sRefSet_allElements (s2->deriv, el)
5106 if (sRef_isReasonable (el))
5108 sRef rb = sRef_getRootBase (el);
5110 if (!sRef_same (rb, sb))
5112 sRef fb = sRef_fixDirectBase (el, s1);
5114 if (sRef_isReasonable (fb))
5116 sRef_copyRealDerived (fb, el);
5117 sRef_addDeriv (s1, fb);
5122 sRef_addDeriv (s1, el);
5125 } end_sRefSet_allElements ;
5130 void sRef_copyRealDerivedComplete (sRef s1, sRef s2)
5132 sRef_innerAliasSetCompleteParam (sRef_copyRealDerived, s1, s2);
5135 void sRef_setUndefined (sRef s, fileloc loc)
5137 sRef_checkMutable (s);
5139 if (sRef_isReasonable (s))
5141 s->defstate = SS_UNDEFINED;
5142 s->definfo = stateInfo_updateLoc (s->definfo, SA_UNDEFINED, loc);
5144 sRef_clearDerived (s);
5148 static void sRef_setDefinedAux (sRef s, fileloc loc, bool clear)
5150 sRef_checkMutable (s);
5151 if (sRef_isInvalid (s)) return;
5153 DPRINTF (("Set defined: %s", sRef_unparseFull (s)));
5155 s->definfo = stateInfo_updateLoc (s->definfo, SA_DEFINED, loc);
5156 s->defstate = SS_DEFINED;
5158 DPRINTF (("Set defined: %s", sRef_unparseFull (s)));
5160 /* e.g., if x is allocated, *x = 3 defines x */
5162 if (s->kind == SK_PTR)
5164 sRef p = s->info->ref;
5167 if (p->defstate == SS_ALLOCATED
5168 || p->defstate == SS_SPECIAL) /* evans 2001-07-12: shouldn't need this */
5170 sRef_setDefinedAux (p, loc, clear);
5174 ** Defines a[0] also:
5177 arr = sRef_findDerivedArrayFetch (p, FALSE, 0, FALSE);
5179 if (sRef_isReasonable (arr))
5181 sRef_setDefinedAux (arr, loc, clear);
5184 else if (s->kind == SK_ARRAYFETCH)
5186 if (!s->info->arrayfetch->indknown
5187 || (s->info->arrayfetch->ind == 0))
5189 sRef p = s->info->arrayfetch->arr;
5190 sRef ptr = sRef_constructPointer (p);
5192 if (sRef_isReasonable (ptr))
5194 if (ptr->defstate == SS_ALLOCATED
5195 || ptr->defstate == SS_UNDEFINED
5196 || ptr->defstate == SS_SPECIAL) /* evans 2001-07-12: shouldn't need this */
5198 sRef_setDefinedAux (ptr, loc, clear);
5202 if (p->defstate == SS_RELDEF)
5206 else if (p->defstate == SS_ALLOCATED || p->defstate == SS_PDEFINED
5207 || p->defstate == SS_SPECIAL) /* evans 2001-07-12: shouldn't need this */
5209 p->defstate = SS_DEFINED;
5216 else if (s->kind == SK_FIELD)
5218 sRef parent = s->info->field->rec;
5220 if (sRef_isReasonable (parent))
5222 if (ctype_isUnion (ctype_realType (parent->type)))
5225 ** Should not clear derived from here.
5228 sRef_setDefinedNoClear (parent, loc);
5232 ; /* Nothing to do for structures. */
5244 sRef_clearDerived (s);
5248 /* evans 2001-07-12: need to define the derived references */
5249 sRefSet_elements (s->deriv, el)
5251 llassert (sRef_isReasonable (el));
5252 el->defstate = SS_DEFINED;
5253 } end_sRefSet_elements ;
5256 DPRINTF (("Set defined: %s", sRef_unparseFull (s)));
5259 static void sRef_setPartialDefined (sRef s, fileloc loc)
5261 sRef_checkMutable (s);
5263 if (!sRef_isPartial (s))
5265 sRef_setDefined (s, loc);
5269 void sRef_setPartialDefinedComplete (sRef s, fileloc loc)
5271 sRef_innerAliasSetComplete (sRef_setPartialDefined, s, loc);
5274 void sRef_setDefinedComplete (sRef s, fileloc loc)
5276 sRef_innerAliasSetComplete (sRef_setDefined, s, loc);
5279 void sRef_setDefinedCompleteDirect (sRef s, fileloc loc)
5283 aliases = usymtab_allAliases (s);
5284 DPRINTF (("Set defined complete: %s", sRef_unparseFull (s)));
5285 DPRINTF (("All aliases: %s", sRefSet_unparseFull (aliases)));
5287 sRef_setDefined (s, loc);
5289 sRefSet_realElements (aliases, current)
5291 if (sRef_isReasonable (current))
5293 current = sRef_updateSref (current);
5294 sRef_setDefined (current, loc);
5296 } end_sRefSet_realElements;
5298 sRefSet_free (aliases);
5299 sRef_innerAliasSetComplete (sRef_setDefined, s, loc);
5302 void sRef_setDefined (sRef s, fileloc loc)
5304 sRef_checkMutable (s);
5305 sRef_setDefinedAux (s, loc, TRUE);
5308 static void sRef_setDefinedNoClear (sRef s, fileloc loc)
5310 sRef_checkMutable (s);
5311 DPRINTF (("Defining: %s", sRef_unparseFull (s)));
5312 sRef_setDefinedAux (s, loc, FALSE);
5313 DPRINTF (("==> %s", sRef_unparseFull (s)));
5316 void sRef_setDefinedNCComplete (sRef s, fileloc loc)
5318 sRef_checkMutable (s);
5319 DPRINTF (("Set Defined Complete: %s", sRef_unparseFull (s)));
5320 sRef_innerAliasSetComplete (sRef_setDefinedNoClear, s, loc);
5321 DPRINTF (("==> %s", sRef_unparseFull (s)));
5324 static bool sRef_isDeepUnionField (sRef s)
5326 return (sRef_deepPred (sRef_isUnionField, s));
5329 bool sRef_isUnionField (sRef s)
5331 if (sRef_isReasonable (s) && s->kind == SK_FIELD)
5334 ** defining one field of a union defines the union
5337 sRef base = s->info->field->rec;
5339 if (sRef_isReasonable (base))
5341 return (ctype_isUnion (ctype_realType (base->type)));
5348 void sRef_setPdefined (sRef s, fileloc loc)
5350 sRef_checkMutable (s);
5351 if (sRef_isReasonable (s) && !sRef_isPartial (s))
5353 sRef base = sRef_getBaseSafe (s);
5355 if (s->defstate == SS_ALLOCATED)
5360 s->definfo = stateInfo_updateLoc (s->definfo, SA_PDEFINED, loc);
5361 s->defstate = SS_PDEFINED;
5363 /* e.g., if x is allocated, *x = 3 defines x */
5365 while (sRef_isReasonable (base) && sRef_isKnown (base))
5367 if (base->defstate == SS_DEFINED)
5371 DPRINTF (("set pdefined: %s", sRef_unparseFull (base)));
5372 base->defstate = SS_PDEFINED;
5373 nb = sRef_getBaseSafe (base);
5384 static void sRef_setStateAux (sRef s, sstate ss, fileloc loc)
5386 sRef_checkMutable (s);
5388 DPRINTF (("Set state: %s => %s", sRef_unparseFull (s), sstate_unparse (ss)));
5390 if (sRef_isReasonable (s))
5392 /* if (s->defstate == SS_RELDEF) return; */
5394 if (s->defstate != ss && fileloc_isDefined (loc))
5396 s->definfo = stateInfo_updateLoc (s->definfo,
5397 stateAction_fromSState (ss), loc);
5401 sRef_clearDerived (s);
5403 if (ss == SS_ALLOCATED)
5405 sRef base = sRef_getBaseSafe (s);
5407 while (sRef_isReasonable (base) && sRef_isKnown (base))
5409 if (base->defstate == SS_DEFINED)
5413 DPRINTF (("set pdefined: %s", sRef_unparseFull (s)));
5414 base->defstate = SS_PDEFINED;
5415 nb = sRef_getBaseSafe (base);
5427 void sRef_setAllocatedComplete (sRef s, fileloc loc)
5429 sRef_innerAliasSetComplete (sRef_setAllocated, s, loc);
5432 static void sRef_setAllocatedShallow (sRef s, fileloc loc)
5434 sRef_checkMutable (s);
5436 if (sRef_isReasonable (s))
5438 if (s->defstate == SS_DEAD || s->defstate == SS_UNDEFINED)
5440 s->defstate = SS_ALLOCATED;
5441 s->definfo = stateInfo_updateLoc (s->definfo, SA_ALLOCATED, loc);
5446 void sRef_setAllocatedShallowComplete (sRef s, fileloc loc)
5448 sRef_innerAliasSetComplete (sRef_setAllocatedShallow, s, loc);
5451 void sRef_setAllocated (sRef s, fileloc loc)
5453 sRef_checkMutable (s);
5454 sRef_setStateAux (s, SS_ALLOCATED, loc);
5457 void sRef_setPartial (sRef s, fileloc loc)
5459 sRef_checkMutable (s);
5460 sRef_setStateAux (s, SS_PARTIAL, loc);
5463 void sRef_setShared (sRef s, fileloc loc)
5465 sRef_checkMutable (s);
5467 if (sRef_isReasonable (s))
5469 if (s->aliaskind != AK_SHARED && fileloc_isDefined (loc))
5471 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, SA_SHARED, loc);
5474 s->aliaskind = AK_SHARED;
5475 /* don't! sRef_clearDerived (s); */
5479 void sRef_setLastReference (sRef s, /*@exposed@*/ sRef ref, fileloc loc)
5481 sRef_checkMutable (s);
5483 if (sRef_isReasonable (s))
5485 s->aliaskind = sRef_getAliasKind (ref);
5486 s->aliasinfo = stateInfo_updateRefLoc (s->aliasinfo, ref, stateAction_fromAlkind (s->aliaskind), loc);
5491 void sRef_setNullStateAux (/*@notnull@*/ sRef s, nstate ns, fileloc loc)
5493 sRef_checkMutable (s);
5495 sRef_resetAliasKind (s);
5497 if (fileloc_isDefined (loc))
5499 s->nullinfo = stateInfo_updateLoc (s->nullinfo, stateAction_fromNState (ns), loc);
5503 void sRef_setNotNull (sRef s, fileloc loc)
5505 if (sRef_isReasonable (s))
5507 sRef_setNullStateAux (s, NS_NOTNULL, loc);
5511 void sRef_setNullStateN (sRef s, nstate n)
5513 if (sRef_isReasonable (s))
5515 sRef_checkMutable (s);
5517 sRef_resetAliasKind (s);
5521 void sRef_setNullState (sRef s, nstate n, fileloc loc)
5523 if (sRef_isReasonable (s))
5525 sRef_setNullStateAux (s, n, loc);
5529 void sRef_setNullTerminatedStateInnerComplete (sRef s, struct s_bbufinfo b, /*@unused@*/ fileloc loc)
5531 switch (b.bufstate) {
5532 case BB_NULLTERMINATED:
5533 sRef_setNullTerminatedState (s);
5534 sRef_setLen (s, b.len);
5536 case BB_POSSIBLYNULLTERMINATED:
5537 sRef_setPossiblyNullTerminatedState(s);
5539 case BB_NOTNULLTERMINATED:
5540 sRef_setNotNullTerminatedState (s);
5544 sRef_setSize (s, b.size);
5546 /* PL: TO BE DONE : Aliases are not modified right now, have to be similar to
5547 * setNullStateInnerComplete.
5551 void sRef_setNullStateInnerComplete (sRef s, nstate n, fileloc loc)
5553 DPRINTF (("Set null state: %s", nstate_unparse (n)));
5555 sRef_setNullState (s, n, loc);
5560 sRef_innerAliasSetComplete (sRef_setPosNull, s, loc);
5563 sRef_innerAliasSetComplete (sRef_setDefNull, s, loc);
5566 sRef_innerAliasSetComplete (sRef_setNullUnknown, s, loc);
5569 sRef_innerAliasSetComplete (sRef_setNotNull, s, loc);
5572 sRef_innerAliasSetComplete (sRef_setNotNull, s, loc);
5575 sRef_innerAliasSetComplete (sRef_setNullUnknown, s, loc);
5578 sRef_innerAliasSetComplete (sRef_setDefNull, s, loc);
5581 sRef_innerAliasSetComplete (sRef_setNullUnknown, s, loc);
5584 sRef_innerAliasSetComplete (sRef_setNullErrorLoc, s, loc);
5589 void sRef_setPosNull (sRef s, fileloc loc)
5591 if (sRef_isReasonable (s))
5593 sRef_setNullStateAux (s, NS_POSNULL, loc);
5597 void sRef_setDefNull (sRef s, fileloc loc)
5599 if (sRef_isReasonable (s))
5601 sRef_setNullStateAux (s, NS_DEFNULL, loc);
5605 void sRef_setNullUnknown (sRef s, fileloc loc)
5607 if (sRef_isReasonable (s))
5609 sRef_setNullStateAux (s, NS_UNKNOWN, loc);
5613 void sRef_setNullError (sRef s)
5615 if (sRef_isReasonable (s) && !sRef_isConst (s))
5617 sRef_setNullStateAux (s, NS_UNKNOWN, fileloc_undefined);
5621 void sRef_setNullErrorLoc (sRef s, fileloc loc)
5623 if (sRef_isReasonable (s) && !sRef_isConst (s))
5625 sRef_setNullStateAux (s, NS_UNKNOWN, loc);
5629 void sRef_setOnly (sRef s, fileloc loc)
5631 if (sRef_isReasonable (s) && s->aliaskind != AK_ONLY)
5633 sRef_checkMutable (s);
5634 s->aliaskind = AK_ONLY;
5635 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, SA_ONLY, loc);
5639 void sRef_setDependent (sRef s, fileloc loc)
5641 if (sRef_isReasonable (s) && !sRef_isConst (s) && (s->aliaskind != AK_DEPENDENT))
5643 sRef_checkMutable (s);
5644 DPRINTF (("Setting dependent: %s", sRef_unparseFull (s)));
5645 s->aliaskind = AK_DEPENDENT;
5646 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, SA_DEPENDENT, loc);
5650 void sRef_setOwned (sRef s, fileloc loc)
5652 if (sRef_isReasonable (s) && !sRef_isConst (s) && (s->aliaskind != AK_OWNED))
5654 sRef_checkMutable (s);
5655 s->aliaskind = AK_OWNED;
5656 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, SA_OWNED, loc);
5660 void sRef_setKept (sRef s, fileloc loc)
5662 if (sRef_isReasonable (s) && !sRef_isConst (s) && (s->aliaskind != AK_KEPT))
5664 sRef base = sRef_getBaseSafe (s);
5666 while (sRef_isReasonable (base) && sRef_isKnown (base))
5668 if (base->defstate == SS_DEFINED)
5670 sRef_checkMutable (base);
5671 base->defstate = SS_PDEFINED;
5672 base = sRef_getBaseSafe (base);
5680 sRef_checkMutable (s);
5681 s->aliaskind = AK_KEPT;
5682 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, SA_KEPT, loc);
5686 static void sRef_setKeptAux (sRef s, fileloc loc)
5688 if (!sRef_isShared (s))
5690 sRef_setKept (s, loc);
5694 static void sRef_setDependentAux (sRef s, fileloc loc)
5696 if (!sRef_isShared (s))
5698 sRef_setDependent (s, loc);
5702 void sRef_setKeptComplete (sRef s, fileloc loc)
5704 sRef_aliasSetComplete (sRef_setKeptAux, s, loc);
5707 void sRef_setDependentComplete (sRef s, fileloc loc)
5709 sRef_aliasSetComplete (sRef_setDependentAux, s, loc);
5712 void sRef_setFresh (sRef s, fileloc loc)
5714 if (sRef_isReasonable (s) && !sRef_isConst (s))
5716 sRef_checkMutable (s);
5717 s->aliaskind = AK_FRESH;
5718 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, SA_CREATED, loc);
5719 DPRINTF (("SetFresh: %s", sRef_unparseFull (s)));
5723 void sRef_kill (sRef s, fileloc loc)
5725 DPRINTF (("Kill: %s", sRef_unparseFull (s)));
5727 if (sRef_isReasonable (s) && !sRef_isShared (s) && !sRef_isConst (s))
5729 sRef base = sRef_getBaseSafe (s);
5730 sRef_checkMutable (s);
5732 while (sRef_isReasonable (base) && sRef_isKnown (base))
5734 if (base->defstate == SS_DEFINED)
5736 sRef_checkMutable (base);
5737 base->defstate = SS_PDEFINED;
5738 base = sRef_getBaseSafe (base);
5746 s->aliaskind = s->oaliaskind;
5747 s->defstate = SS_DEAD;
5748 s->definfo = stateInfo_updateLoc (s->definfo, SA_KILLED, loc);
5749 DPRINTF (("State info: %s", stateInfo_unparse (s->definfo)));
5750 sRef_clearDerived (s);
5754 void sRef_maybeKill (sRef s, fileloc loc)
5756 if (sRef_isReasonable (s))
5758 sRef base = sRef_getBaseSafe (s);
5759 sRef_checkMutable (s);
5761 while (sRef_isReasonable (base) && sRef_isKnown (base))
5763 if (base->defstate == SS_DEFINED || base->defstate == SS_RELDEF)
5765 sRef_checkMutable (base);
5766 base->defstate = SS_PDEFINED;
5767 base = sRef_getBaseSafe (base);
5776 s->aliaskind = s->oaliaskind;
5777 s->defstate = SS_HOFFA;
5778 s->definfo = stateInfo_updateLoc (s->definfo, SA_PKILLED, loc);
5779 DPRINTF (("State info: %s / %s", sRef_unparse (s),
5780 stateInfo_unparse (s->definfo)));
5781 sRef_clearDerived (s);
5787 ** just for type checking...
5790 static void sRef_killAux (sRef s, fileloc loc)
5792 if (sRef_isReasonable (s) && !sRef_isShared (s))
5794 if (sRef_isUnknownArrayFetch (s))
5796 sRef_maybeKill (s, loc);
5806 ** kills s and all aliases to s
5809 void sRef_killComplete (sRef s, fileloc loc)
5811 DPRINTF (("Kill complete: %s", sRef_unparseFull (s)));
5812 sRef_aliasSetComplete (sRef_killAux, s, loc);
5815 static bool sRef_equivalent (sRef s1, sRef s2)
5817 return (sRef_compare (s1, s2) == 0);
5821 ** returns an sRef that will not be free'd on function exit.
5824 /*@only@*/ sRef sRef_saveCopy (sRef s)
5828 if (sRef_isReasonable (s))
5830 bool old = inFunction;
5833 ** Exit the function scope, so this sRef is not
5834 ** stored in the deallocation table.
5838 DPRINTF (("Copying sref: %s", sRef_unparseFull(s)));
5839 ret = sRef_copy (s);
5840 DPRINTF (("Copying ===>: %s", sRef_unparseFull(ret)));
5845 ret = sRef_undefined;
5848 /*@-dependenttrans@*/
5850 /*@=dependenttrans@*/
5853 sRef sRef_copy (sRef s)
5855 if (sRef_isKindSpecial (s) && !sRef_isGlobalMarker (s))
5858 return s; /* don't copy specials (except for global markers) */
5862 if (sRef_isReasonable (s))
5864 sRef t = sRef_alloc ();
5866 DPRINTF (("Copying: [%p] %s", s, sRef_unparse (s)));
5867 DPRINTF (("Full: %s", sRef_unparseFull (s)));
5871 t->modified = s->modified;
5872 t->immut = FALSE; /* Note mutability is not copied. */
5874 t->val = multiVal_copy (s->val);
5876 t->info = sinfo_copy (s);
5877 t->defstate = s->defstate;
5878 t->nullstate = s->nullstate;
5880 /* start modifications */
5881 t->bufinfo.bufstate = s->bufinfo.bufstate;
5882 t->bufinfo.len = s->bufinfo.len;
5883 t->bufinfo.size = s->bufinfo.size;
5884 /* end modifications */
5886 t->aliaskind = s->aliaskind;
5887 t->oaliaskind = s->oaliaskind;
5889 t->expkind = s->expkind;
5890 t->oexpkind = s->oexpkind;
5892 t->nullinfo = stateInfo_copy (s->nullinfo);
5893 t->aliasinfo = stateInfo_copy (s->aliasinfo);
5894 t->definfo = stateInfo_copy (s->definfo);
5895 t->expinfo = stateInfo_copy (s->expinfo);
5897 t->deriv = sRefSet_newDeepCopy (s->deriv);
5898 t->state = valueTable_copy (s->state);
5900 DPRINTF (("Made copy: %s => %s", sRef_unparseFull (s), sRef_unparseFull (t)));
5905 return sRef_undefined;
5910 # define PREDTEST(func,s) \
5911 do { if (sRef_isInvalid (s)) { return FALSE; } \
5912 else { if (sRef_isConj (s)) \
5913 { return (func (sRef_getConjA (s)) \
5914 || func (sRef_getConjB (s))); }}} while (FALSE);
5916 bool sRef_isAddress (sRef s)
5918 PREDTEST (sRef_isAddress, s);
5919 return (s->kind == SK_ADR);
5923 ** pretty weak... maybe a flag should control this.
5926 bool sRef_isThroughArrayFetch (sRef s)
5928 if (sRef_isReasonable (s))
5936 if (sRef_isArrayFetch (tref))
5941 lt = sRef_getBase (tref);
5943 } while (sRef_isReasonable (tref));
5949 bool sRef_isArrayFetch (sRef s)
5951 PREDTEST (sRef_isArrayFetch, s);
5952 return (s->kind == SK_ARRAYFETCH);
5955 bool sRef_isMacroParamRef (sRef s)
5957 if (context_inMacro () && sRef_isCvar (s))
5959 uentry ue = sRef_getUentry (s);
5960 cstring pname = makeParam (uentry_rawName (ue));
5961 uentry mac = usymtab_lookupSafe (pname);
5963 cstring_free (pname);
5964 return (uentry_isValid (mac));
5970 bool sRef_isCvar (sRef s)
5972 PREDTEST (sRef_isCvar, s);
5973 return (s->kind == SK_CVAR);
5976 bool sRef_isConst (sRef s)
5978 PREDTEST (sRef_isConst, s);
5979 return (s->kind == SK_CONST);
5982 bool sRef_isObject (sRef s)
5984 PREDTEST (sRef_isObject, s);
5985 return (s->kind == SK_OBJECT);
5988 bool sRef_isExternal (sRef s)
5990 PREDTEST (sRef_isExternal, s);
5991 return (s->kind == SK_EXTERNAL);
5994 static bool sRef_isDerived (sRef s)
5996 PREDTEST (sRef_isDerived, s);
5997 return (s->kind == SK_DERIVED);
6000 bool sRef_isField (sRef s)
6002 PREDTEST (sRef_isField, s);
6003 return (s->kind == SK_FIELD);
6006 static bool sRef_isIndex (sRef s)
6008 PREDTEST (sRef_isIndex, s);
6009 return (s->kind == SK_ARRAYFETCH);
6012 bool sRef_isAnyParam (sRef s)
6014 PREDTEST (sRef_isAnyParam, s);
6015 return (s->kind == SK_PARAM);
6018 bool sRef_isParam (sRef s)
6020 PREDTEST (sRef_isParam, s);
6021 return (s->kind == SK_PARAM);
6024 bool sRef_isDirectParam (sRef s)
6026 PREDTEST (sRef_isDirectParam, s);
6028 return ((s->kind == SK_CVAR) &&
6029 (s->info->cvar->lexlevel == functionScope) &&
6030 (context_inFunction () &&
6031 (s->info->cvar->index <= usymId_fromInt (uentryList_size (context_getParams ())))));
6034 bool sRef_isPointer (sRef s)
6036 PREDTEST (sRef_isPointer, s);
6037 return (s->kind == SK_PTR);
6041 ** returns true if storage referenced by s is visible
6044 bool sRef_isReference (sRef s)
6046 PREDTEST (sRef_isReference, s);
6048 return (sRef_isPointer (s) || sRef_isIndex (s) || sRef_isFileOrGlobalScope (s)
6049 || (sRef_isField (s) && (sRef_isReference (s->info->field->rec))));
6052 bool sRef_isIReference (sRef s)
6054 return (sRef_isPointer (s) || sRef_isAddress (s) || sRef_isIndex (s)
6055 || sRef_isField (s) || sRef_isArrayFetch (s));
6058 bool sRef_isFileOrGlobalScope (sRef s)
6060 return (sRef_isCvar (s) && (s->info->cvar->lexlevel <= fileScope));
6063 bool sRef_isRealGlobal (sRef s)
6065 return (sRef_isCvar (s) && (s->info->cvar->lexlevel == globScope));
6068 bool sRef_isFileStatic (sRef s)
6070 return (sRef_isCvar (s) && (s->info->cvar->lexlevel == fileScope));
6073 bool sRef_isAliasCheckedGlobal (sRef s)
6075 if (sRef_isFileOrGlobalScope (s))
6077 uentry ue = sRef_getUentry (s);
6079 return context_checkAliasGlob (ue);
6087 void sRef_free (/*@only@*/ sRef s)
6089 if (s != sRef_undefined && s->kind != SK_SPECIAL)
6091 DPRINTF (("Free sref: [%p]", s));
6093 sRef_checkValid (s);
6095 multiVal_free (s->val); /* evans 2002-07-12 */
6097 stateInfo_free (s->expinfo);
6098 stateInfo_free (s->aliasinfo);
6099 stateInfo_free (s->definfo);
6100 stateInfo_free (s->nullinfo);
6102 sRefSet_free (s->deriv);
6103 s->deriv = sRefSet_undefined;
6105 valueTable_free (s->state);
6109 /* drl added to help locate use after release*/
6110 s->expinfo = stateInfo_undefined;
6111 s->aliasinfo = stateInfo_undefined;
6112 s->definfo = stateInfo_undefined;
6113 s->nullinfo = stateInfo_undefined;
6119 void sRef_setType (sRef s, ctype t)
6121 if (sRef_isReasonable (s))
6123 sRef_checkMutable (s);
6128 void sRef_setTypeFull (sRef s, ctype t)
6130 if (sRef_isReasonable (s))
6132 sRef_checkMutable (s);
6135 sRefSet_allElements (s->deriv, current)
6137 sRef_setTypeFull (current, ctype_unknown);
6138 } end_sRefSet_allElements ;
6143 sRef_buildField (/*@exposed@*/ sRef rec, /*@dependent@*/ cstring f)
6145 return (sRef_buildNCField (rec, f));
6148 static /*@exposed@*/ sRef
6149 sRef_findDerivedField (/*@notnull@*/ sRef rec, cstring f)
6151 sRefSet_allElements (sRef_derivedFields (rec), sr)
6153 if (sRef_isReasonable (sr))
6155 if (sRef_isReasonable (sr))
6157 if (sr->info != NULL)
6159 if (sr->kind == SK_FIELD && cstring_equal (sr->info->field->field, f))
6167 llcontbug (message ("Invalid sRef as derived field of %s", sRef_unparse (rec)));
6170 } end_sRefSet_allElements;
6172 return sRef_undefined;
6175 /*@dependent@*/ /*@observer@*/ sRefSet sRef_derivedFields (/*@temp@*/ sRef rec)
6177 if (sRef_isReasonable (rec))
6185 return (sRefSet_undefined);
6189 static /*@exposed@*/ sRef
6190 sRef_findDerivedPointer (sRef s)
6192 if (sRef_isReasonable (s))
6194 sRefSet_realElements (s->deriv, sr)
6196 if (sRef_isReasonable (sr) && sr->kind == SK_PTR)
6200 } end_sRefSet_realElements;
6203 return sRef_undefined;
6207 sRef_isUnknownArrayFetch (sRef s)
6209 return (sRef_isReasonable (s)
6210 && s->kind == SK_ARRAYFETCH
6211 && !s->info->arrayfetch->indknown);
6214 static /*@exposed@*/ sRef
6215 sRef_findDerivedArrayFetch (/*@notnull@*/ sRef s, bool isknown, int idx, bool dead)
6220 sRefSet_realElements (s->deriv, sr)
6222 if (sRef_isReasonable (sr)
6223 && sr->kind == SK_ARRAYFETCH
6224 && sr->info->arrayfetch->indknown
6225 && (sr->info->arrayfetch->ind == idx))
6229 } end_sRefSet_realElements;
6233 sRefSet_realElements (s->deriv, sr)
6235 if (sRef_isReasonable (sr)
6236 && sr->kind == SK_ARRAYFETCH
6237 && (!sr->info->arrayfetch->indknown
6238 || (sr->info->arrayfetch->indknown &&
6239 sr->info->arrayfetch->ind == 0)))
6241 if (sRef_isDead (sr) || sRef_isKept (sr))
6243 if (dead || context_getFlag (FLG_STRICTUSERELEASED))
6253 } end_sRefSet_realElements;
6256 return sRef_undefined;
6259 static /*@exposed@*/ sRef
6260 sRef_buildNCField (/*@exposed@*/ sRef rec, /*@exposed@*/ cstring f)
6264 DPRINTF (("Build nc field: %s / %s",
6265 sRef_unparseFull (rec), f));
6267 if (sRef_isInvalid (rec))
6269 return sRef_undefined;
6273 ** check if the field already has been referenced
6276 s = sRef_findDerivedField (rec, f);
6278 if (sRef_isReasonable (s))
6284 ctype ct = ctype_realType (rec->type);
6286 DPRINTF (("Field of: %s", sRef_unparse (rec)));
6290 s->info = (sinfo) dmalloc (sizeof (*s->info));
6291 s->info->field = (fldinfo) dmalloc (sizeof (*s->info->field));
6292 s->info->field->rec = rec;
6293 s->info->field->field = f; /* doesn't copy f */
6295 if (ctype_isKnown (ct) && ctype_isSU (ct))
6297 uentry ue = uentryList_lookupField (ctype_getFields (ct), f);
6299 if (!uentry_isUndefined (ue))
6301 DPRINTF (("lookup: %s for %s", uentry_unparseFull (ue),
6302 ctype_unparse (ct)));
6304 s->type = uentry_getType (ue);
6306 if (ctype_isMutable (s->type)
6307 && rec->aliaskind != AK_STACK
6308 && !alkind_isStatic (rec->aliaskind))
6310 s->aliaskind = rec->aliaskind;
6314 s->aliaskind = AK_UNKNOWN;
6317 if (sRef_isStateDefined (rec) || sRef_isStateUnknown (rec)
6318 || sRef_isPdefined (rec))
6320 sRef_setStateFromUentry (s, ue);
6324 sRef_setPartsFromUentry (s, ue);
6327 s->oaliaskind = s->aliaskind;
6328 s->oexpkind = s->expkind;
6330 DPRINTF (("sref: %s", sRef_unparseFull (s)));
6335 Never report this as an error. It can happen whenever there
6336 is casting involved.
6340 llcontbug (message ("buildNCField --- no field %s: %q / %s",
6341 f, sRef_unparse (s), ctype_unparse (ct)));
6345 return sRef_undefined;
6349 if (rec->defstate == SS_DEFINED
6350 && (s->defstate == SS_UNDEFINED || s->defstate == SS_UNKNOWN))
6352 s->defstate = SS_DEFINED;
6354 else if (rec->defstate == SS_PARTIAL)
6356 s->defstate = SS_PARTIAL;
6358 else if (rec->defstate == SS_ALLOCATED)
6360 if (ctype_isStackAllocated (ct) && ctype_isStackAllocated (s->type))
6362 s->defstate = SS_ALLOCATED;
6366 s->defstate = SS_UNDEFINED;
6369 else if (s->defstate == SS_UNKNOWN)
6371 s->defstate = rec->defstate;
6378 if (s->defstate == SS_UNDEFINED)
6380 ctype rt = ctype_realType (s->type);
6382 if (ctype_isArray (rt) || ctype_isSU (rt))
6384 s->defstate = SS_ALLOCATED;
6388 sRef_addDeriv (rec, s);
6389 DPRINTF (("Add deriv: %s", sRef_unparseFull (rec)));
6391 if (ctype_isInt (s->type) && cstring_equal (f, REFSNAME))
6393 s->aliaskind = AK_REFS;
6394 s->oaliaskind = AK_REFS;
6397 DPRINTF (("Build field ==> %s", sRef_unparseFull (s)));
6403 sRef_isStackAllocated (sRef s)
6405 return (sRef_isReasonable(s)
6406 && s->defstate == SS_ALLOCATED && ctype_isStackAllocated (s->type));
6410 void sRef_setArrayFetchState (/*@notnull@*/ /*@exposed@*/ sRef s,
6411 /*@notnull@*/ /*@exposed@*/ sRef arr)
6413 sRef_checkMutable (s);
6415 if (ctype_isRealAP (arr->type))
6417 s->type = ctype_baseArrayPtr (arr->type);
6420 /* a hack, methinks... makeArrayFetch (&a[0]) ==> a[] */
6421 /* evans - 2001-08-27: not sure where this was necessary - it
6422 ** causes an assertion in in aliasCheckPred to fail.
6425 if (sRef_isAddress (arr))
6427 sRef t = arr->info->ref;
6429 if (sRef_isArrayFetch (t))
6431 s->info->arrayfetch->arr = t->info->arrayfetch->arr;
6434 else if (ctype_isRealPointer (arr->type))
6436 sRef sp = sRef_findDerivedPointer (arr);
6438 if (sRef_isReasonable (sp))
6441 if (ctype_isMutable (s->type))
6443 s->expkind = sRef_getExKind (sp);
6444 s->expinfo = stateInfo_copy (sp->expinfo);
6446 s->aliaskind = sp->aliaskind;
6447 s->aliasinfo = stateInfo_copy (sp->aliasinfo);
6450 s->defstate = sp->defstate;
6452 if (s->defstate == SS_DEFINED)
6454 if (!context_getFlag (FLG_STRICTDESTROY))
6456 s->defstate = SS_PARTIAL;
6460 DPRINTF (("Set null state: %s / %s", sRef_unparseFull (s), sRef_unparseFull (sp)));
6461 sRef_setNullStateN (s, sRef_getNullState (sp));
6465 if (arr->defstate == SS_UNDEFINED)
6467 s->defstate = SS_UNUSEABLE;
6469 else if ((arr->defstate == SS_ALLOCATED) && !ctype_isSU (s->type))
6471 s->defstate = SS_UNDEFINED;
6475 if (!context_getFlag (FLG_STRICTDESTROY))
6477 s->defstate = SS_PARTIAL;
6481 s->defstate = SS_DEFINED;
6485 ** Very weak checking for array elements.
6487 ** s->defstate = arr->defstate;
6491 s->expkind = sRef_getExKind (arr);
6492 s->expinfo = stateInfo_copy (arr->expinfo);
6494 if (arr->aliaskind == AK_LOCAL || arr->aliaskind == AK_FRESH)
6496 s->aliaskind = AK_LOCAL;
6500 s->aliaskind = AK_UNKNOWN;
6503 sRef_setTypeState (s);
6508 if (arr->defstate == SS_DEFINED)
6511 ** Very weak checking for array elements.
6513 ** s->defstate = arr->defstate;
6516 if (context_getFlag (FLG_STRICTDESTROY))
6518 s->defstate = SS_DEFINED;
6522 s->defstate = SS_PARTIAL;
6525 else if (arr->defstate == SS_ALLOCATED)
6527 if (ctype_isRealArray (s->type))
6529 s->defstate = SS_ALLOCATED;
6533 if (!s->info->arrayfetch->indknown)
6536 ** is index is unknown, elements is defined or
6537 ** allocated is any element is!
6540 s->defstate = SS_UNDEFINED;
6542 sRefSet_allElements (arr->deriv, sr)
6544 if (sRef_isReasonable (sr))
6546 if (sRef_isReasonable (sr))
6548 if (sr->defstate == SS_ALLOCATED)
6550 s->defstate = SS_ALLOCATED;
6554 if (sr->defstate == SS_DEFINED)
6556 if (context_getFlag (FLG_STRICTDESTROY))
6558 s->defstate = SS_DEFINED;
6562 s->defstate = SS_PARTIAL;
6571 llcontbug (message ("Invalid sRef as derived element of %s", sRef_unparse (arr)));
6574 } end_sRefSet_allElements;
6578 s->defstate = SS_UNDEFINED;
6584 s->defstate = arr->defstate;
6589 ** kludgey way to guess where aliaskind applies
6592 if (ctype_isMutable (s->type)
6593 && !ctype_isPointer (arr->type)
6594 && !alkind_isStatic (arr->aliaskind)
6595 && !alkind_isStack (arr->aliaskind)) /* evs - 2000-06-20: don't pass stack allocation to members */
6597 s->aliaskind = arr->aliaskind;
6601 s->aliaskind = AK_UNKNOWN;
6604 sRef_setTypeState (s);
6607 if (sRef_isObserver (arr))
6609 s->expkind = XO_OBSERVER;
6610 s->expinfo = stateInfo_copy (arr->expinfo);
6614 /*@exposed@*/ sRef sRef_buildArrayFetch (/*@exposed@*/ sRef arr)
6618 if (!sRef_isReasonable (arr)) {
6619 /*@-nullret@*/ return arr /*@=nullret@*/;
6622 if (ctype_isRealPointer (arr->type))
6624 (void) sRef_buildPointer (arr); /* do this to define arr! */
6627 s = sRef_findDerivedArrayFetch (arr, FALSE, 0, FALSE);
6629 if (sRef_isReasonable (s))
6631 /* evans 2001-07-12: this is bogus, clean-up hack */
6632 if (s->info->arrayfetch->arr != arr)
6635 check (sRefSet_delete (arr->deriv, s));
6636 res = sRef_buildArrayFetch (arr);
6637 sRef_copyState (res, s);
6638 llassert (res->info->arrayfetch->arr == arr);
6642 s->expkind = sRef_getExKind (arr);
6643 s->expinfo = stateInfo_copy (arr->expinfo);
6651 s->kind = SK_ARRAYFETCH;
6652 s->info = (sinfo) dmalloc (sizeof (*s->info));
6653 s->info->arrayfetch = (ainfo) dmalloc (sizeof (*s->info->arrayfetch));
6654 s->info->arrayfetch->indknown = FALSE;
6655 s->info->arrayfetch->ind = 0;
6656 s->info->arrayfetch->arr = arr;
6658 sRef_setArrayFetchState (s, arr);
6660 s->oaliaskind = s->aliaskind;
6661 s->oexpkind = s->expkind;
6663 if (!context_inProtectVars ())
6665 sRef_addDeriv (arr, s);
6668 if (valueTable_isUndefined (s->state))
6670 s->state = context_createValueTable
6671 (s, stateInfo_makeLoc (g_currentloc, SA_CREATED));
6679 sRef_buildArrayFetchKnown (/*@exposed@*/ sRef arr, int i)
6683 if (!sRef_isReasonable (arr)) {
6684 /*@-nullret@*/ return arr /*@=nullret@*/;
6687 if (ctype_isRealPointer (arr->type))
6689 (void) sRef_buildPointer (arr); /* do this to define arr! */
6692 s = sRef_findDerivedArrayFetch (arr, TRUE, i, FALSE);
6694 if (sRef_isReasonable (s))
6696 /* evans 2001-07-12: this is bogus, clean-up hack */
6697 if (s->info->arrayfetch->arr != arr)
6701 check (sRefSet_delete (arr->deriv, s));
6702 res = sRef_buildArrayFetchKnown (arr, i);
6704 llassert (res->info->arrayfetch->arr == arr);
6705 sRef_copyState (res, s);
6706 llassert (res->info->arrayfetch->arr == arr);
6710 s->expkind = sRef_getExKind (arr);
6711 s->expinfo = stateInfo_copy (arr->expinfo);
6713 llassert (s->info->arrayfetch->arr == arr);
6720 s->kind = SK_ARRAYFETCH;
6721 s->info = (sinfo) dmalloc (sizeof (*s->info));
6722 s->info->arrayfetch = (ainfo) dmalloc (sizeof (*s->info->arrayfetch));
6723 s->info->arrayfetch->arr = arr;
6724 s->info->arrayfetch->indknown = TRUE;
6725 s->info->arrayfetch->ind = i;
6727 sRef_setArrayFetchState (s, arr);
6728 /* evans 2001-08-27 no: can change this - llassert (s->info->arrayfetch->arr == arr); */
6730 s->oaliaskind = s->aliaskind;
6731 s->oexpkind = s->expkind;
6732 sRef_addDeriv (arr, s);
6734 llassert (valueTable_isUndefined (s->state));
6735 s->state = context_createValueTable (s, stateInfo_makeLoc (g_currentloc, SA_CREATED));
6741 ** sets everything except for defstate
6745 sRef_setPartsFromUentry (sRef s, uentry ue)
6747 sRef uref = uentry_getSref (ue);
6749 llassert (sRef_isReasonable (s));
6751 s->aliaskind = alkind_derive (s->aliaskind, uentry_getAliasKind (ue));
6752 s->oaliaskind = s->aliaskind;
6754 if (s->expkind == XO_UNKNOWN)
6756 s->expkind = uentry_getExpKind (ue);
6759 s->oexpkind = s->expkind;
6761 if (sRef_getNullState (s) == NS_UNKNOWN)
6763 DPRINTF (("Set null state: %s / %s", sRef_unparseFull (s), uentry_unparseFull (ue)));
6764 sRef_setNullStateN (s, sRef_getNullState (uentry_getSref (ue)));
6768 DPRINTF (("Skipping null null state!"));
6771 if (s->aliaskind == AK_IMPONLY && (sRef_isExposed (s) || sRef_isObserver (s)))
6773 s->oaliaskind = s->aliaskind = AK_IMPDEPENDENT;
6776 if (sRef_isReasonable (uref))
6778 valueTable utable = uref->state;
6779 valueTable_free (s->state);
6780 s->state = valueTable_copy (utable);
6785 sRef_setStateFromAbstractUentry (sRef s, uentry ue)
6787 llassert (sRef_isReasonable (s));
6788 sRef_checkMutable (s);
6790 sRef_setPartsFromUentry (s, ue);
6792 s->aliaskind = alkind_derive (s->aliaskind, uentry_getAliasKind (ue));
6793 s->oaliaskind = s->aliaskind;
6795 if (s->expkind == XO_UNKNOWN)
6797 s->expkind = uentry_getExpKind (ue);
6800 s->oexpkind = s->expkind;
6804 sRef_setStateFromUentry (sRef s, uentry ue)
6808 sRef_checkMutable (s);
6809 llassert (sRef_isReasonable (s));
6811 sRef_setPartsFromUentry (s, ue);
6813 defstate = uentry_getDefState (ue);
6815 if (sstate_isKnown (defstate))
6817 s->defstate = defstate;
6826 sRef_buildPointer (/*@exposed@*/ sRef t)
6828 DPRINTF (("build pointer: %s", sRef_unparse (t)));
6830 if (sRef_isInvalid (t)) return sRef_undefined;
6832 if (sRef_isAddress (t))
6834 DPRINTF (("Return ref: %s", sRef_unparse (t->info->ref)));
6835 return (t->info->ref);
6839 sRef s = sRef_findDerivedPointer (t);
6841 DPRINTF (("find derived: %s", sRef_unparse (s)));
6843 if (sRef_isReasonable (s))
6845 s->expkind = sRef_getExKind (t);
6846 s->expinfo = stateInfo_copy (t->expinfo);
6848 s->oaliaskind = s->aliaskind;
6849 s->oexpkind = s->expkind;
6855 s = sRef_constructPointerAux (t);
6857 DPRINTF (("construct: %s", sRef_unparse (s)));
6859 if (sRef_isReasonable (s))
6861 sRef_addDeriv (t, s);
6863 s->oaliaskind = s->aliaskind;
6864 s->oexpkind = s->expkind;
6873 sRef_constructPointer (/*@exposed@*/ sRef t)
6876 return sRef_buildPointer (t);
6879 static /*@exposed@*/ sRef sRef_constructDerefAux (sRef t, bool isdead)
6881 if (sRef_isReasonable (t))
6886 ** if there is a derived t[?], return that. Otherwise, *t.
6889 s = sRef_findDerivedArrayFetch (t, FALSE, 0, isdead);
6891 if (sRef_isReasonable (s))
6893 DPRINTF (("Found array fetch: %s", sRef_unparseFull (s)));
6898 sRef ret = sRef_constructPointer (t);
6900 DPRINTF (("Constructed pointer: %s", sRef_unparseFull (ret)));
6907 return sRef_undefined;
6911 sRef sRef_constructDeref (sRef t)
6913 return sRef_constructDerefAux (t, FALSE);
6916 sRef sRef_constructDeadDeref (sRef t)
6918 return sRef_constructDerefAux (t, TRUE);
6922 sRef_constructPointerAux (/*@notnull@*/ /*@exposed@*/ sRef t)
6924 sRef s = sRef_newRef ();
6928 llassert (valueTable_isUndefined (s->state));
6931 s->info = (sinfo) dmalloc (sizeof (*s->info));
6934 if (ctype_isRealAP (rt))
6936 s->type = ctype_baseArrayPtr (rt);
6939 st = ctype_realType (s->type);
6941 if (t->defstate == SS_UNDEFINED)
6943 s->defstate = SS_UNUSEABLE;
6944 s->definfo = stateInfo_copy (t->definfo);
6946 else if ((t->defstate == SS_ALLOCATED) && !ctype_isSU (st))
6948 s->defstate = SS_UNDEFINED;
6949 s->definfo = stateInfo_copy (t->definfo);
6953 s->defstate = t->defstate;
6954 s->definfo = stateInfo_copy (t->definfo);
6957 if (t->aliaskind == AK_LOCAL || t->aliaskind == AK_FRESH)
6959 s->aliaskind = AK_LOCAL;
6963 s->aliaskind = AK_UNKNOWN;
6966 s->expkind = sRef_getExKind (t);
6967 s->expinfo = stateInfo_copy (t->expinfo);
6969 sRef_setTypeState (s);
6971 s->oaliaskind = s->aliaskind;
6972 s->oexpkind = s->expkind;
6974 if (valueTable_isUndefined (s->state))
6976 s->state = context_createValueTable (s, stateInfo_makeLoc (g_currentloc, SA_CREATED));
6979 DPRINTF (("pointer: %s", sRef_unparseFull (s)));
6983 bool sRef_hasDerived (sRef s)
6985 return (sRef_isReasonable (s) && !sRefSet_isEmpty (s->deriv));
6989 sRef_clearDerived (sRef s)
6991 if (sRef_isReasonable (s))
6993 DPRINTF (("Clear derived: [%p] %s", s, sRef_unparseDebug (s)));
6994 sRefSet_clear (s->deriv);
6999 sRef_clearDerivedComplete (sRef s)
7001 if (sRef_isReasonable (s))
7003 sRef base = sRef_getBaseSafe (s);
7005 while (sRef_isReasonable (base))
7007 DPRINTF (("Clear derived: [%p] %s", base, sRef_unparse (base)));
7008 sRefSet_clear (base->deriv);
7009 base = sRef_getBaseSafe (base);
7012 DPRINTF (("Clear derived: [%p] %s", s, sRef_unparse (s)));
7013 sRefSet_clear (s->deriv);
7017 /*@exposed@*/ sRef sRef_makePointer (/*@exposed@*/ sRef s)
7020 sRef res = sRef_buildPointer (s);
7022 DPRINTF (("Res: %s", sRef_unparseFull (res)));
7027 ** &a[] => a (this is for out params)
7031 sRef_makeAnyArrayFetch (/*@exposed@*/ sRef arr)
7033 if (sRef_isAddress (arr))
7035 return (arr->info->ref);
7039 return (sRef_buildArrayFetch (arr));
7044 sRef_makeArrayFetch (/*@exposed@*/ sRef arr)
7046 return (sRef_buildArrayFetch (arr));
7050 sRef_makeArrayFetchKnown (/*@exposed@*/ sRef arr, int i)
7052 return (sRef_buildArrayFetchKnown (arr, i));
7056 sRef_makeField (sRef rec, /*@dependent@*/ cstring f)
7059 ret = sRef_buildField (rec, f);
7064 sRef_makeNCField (/*@exposed@*/ sRef rec, /*@dependent@*/ cstring f)
7066 return (sRef_buildNCField (rec, f));
7070 sRef_unparseKindName (sRef s)
7074 if (s == sRef_undefined) return cstring_makeLiteral ("<invalid>");
7076 s = sRef_fixConj (s);
7081 if (sRef_isLocalVar (s))
7083 result = cstring_makeLiteral ("Variable");
7087 result = cstring_makeLiteral ("Undef global");
7091 result = cstring_makeLiteral ("Out parameter");
7094 if (sRef_isAnyParam (s->info->arrayfetch->arr))
7096 result = cstring_makeLiteral ("Out parameter");
7098 else if (sRef_isIndexKnown (s))
7100 result = cstring_makeLiteral ("Array element");
7104 result = cstring_makeLiteral ("Value");
7108 if (sRef_isAnyParam (s->info->ref))
7110 result = cstring_makeLiteral ("Out parameter");
7114 result = cstring_makeLiteral ("Value");
7118 result = cstring_makeLiteral ("Value");
7121 result = cstring_makeLiteral ("Field");
7124 result = cstring_makeLiteral ("Object");
7126 case SK_UNCONSTRAINED:
7127 result = cstring_makeLiteral ("<anything>");
7136 result = cstring_makeLiteral ("<unknown>");
7139 result = cstring_makeLiteral ("<conj>");
7142 result = cstring_makeLiteral ("Storage");
7150 sRef_unparseKindNamePlain (sRef s)
7154 if (s == sRef_undefined) return cstring_makeLiteral ("<invalid>");
7156 s = sRef_fixConj (s);
7161 if (sRef_isLocalVar (s))
7163 result = cstring_makeLiteral ("Variable");
7167 result = cstring_makeLiteral ("Global");
7171 result = cstring_makeLiteral ("Parameter");
7174 if (sRef_isAnyParam (s->info->arrayfetch->arr))
7176 result = cstring_makeLiteral ("Parameter");
7178 else if (sRef_isIndexKnown (s))
7180 result = cstring_makeLiteral ("Array element");
7184 result = cstring_makeLiteral ("Value");
7188 if (sRef_isAnyParam (s->info->ref))
7190 result = cstring_makeLiteral ("Parameter");
7194 result = cstring_makeLiteral ("Value");
7198 result = cstring_makeLiteral ("Value");
7201 result = cstring_makeLiteral ("Field");
7204 result = cstring_makeLiteral ("Object");
7207 result = cstring_makeLiteral ("Storage");
7209 case SK_UNCONSTRAINED:
7210 result = cstring_makeLiteral ("<anything>");
7219 result = cstring_makeLiteral ("<unknown>");
7222 result = cstring_makeLiteral ("<conj>");
7234 sRef_copyState (sRef s1, sRef s2)
7236 if (sRef_isReasonable (s1) && sRef_isReasonable (s2))
7238 s1->defstate = s2->defstate;
7240 /* start modifications */
7241 s1->bufinfo.bufstate = s2->bufinfo.bufstate;
7242 s1->bufinfo.len = s2->bufinfo.len;
7243 s1->bufinfo.size = s2->bufinfo.size;
7244 /* end modifications */
7246 s1->aliaskind = s2->aliaskind;
7247 s1->aliasinfo = stateInfo_update (s1->aliasinfo, s2->aliasinfo);
7249 s1->expkind = s2->expkind;
7250 s1->expinfo = stateInfo_update (s1->expinfo, s2->expinfo);
7252 s1->nullstate = s2->nullstate;
7253 s1->nullinfo = stateInfo_update (s1->nullinfo, s2->nullinfo);
7255 valueTable_free (s1->state);
7256 s1->state = valueTable_copy (s2->state);
7257 s1->safe = s2->safe;
7262 sRef_makeNew (ctype ct, sRef t, cstring name)
7264 sRef s = sRef_newRef ();
7269 llassert (sRef_isReasonable (t));
7270 s->defstate = t->defstate;
7272 s->aliaskind = t->aliaskind;
7273 s->oaliaskind = s->aliaskind;
7274 s->nullstate = t->nullstate;
7276 s->expkind = t->expkind;
7277 s->oexpkind = s->expkind;
7279 s->info = (sinfo) dmalloc (sizeof (*s->info));
7280 s->info->fname = name;
7282 /* start modifications */
7283 s->bufinfo.bufstate = t->bufinfo.bufstate;
7284 /* end modifications */
7286 llassert (valueTable_isUndefined (s->state));
7287 s->state = valueTable_copy (t->state);
7289 DPRINTF (("==> Copying state: %s", valueTable_unparse (s->state)));
7290 DPRINTF (("==> new: %s", sRef_unparseFull (s)));
7295 sRef_makeType (ctype ct)
7297 sRef s = sRef_newRef ();
7299 sRef_checkMutable (s);
7304 s->defstate = SS_UNKNOWN;
7305 s->aliaskind = AK_UNKNOWN;
7306 sRef_setNullStateN (s, NS_UNKNOWN);
7308 /* start modification */
7309 s->bufinfo.bufstate = BB_NOTNULLTERMINATED;
7310 /* end modification */
7313 if (ctype_isUA (ct))
7315 typeId uid = ctype_typeId (ct);
7316 uentry ue = usymtab_getTypeEntrySafe (uid);
7318 if (uentry_isValid (ue))
7320 sRef_mergeStateQuiet (s, uentry_getSref (ue));
7324 s->oaliaskind = s->aliaskind;
7325 s->oexpkind = s->expkind;
7326 llassert (valueTable_isUndefined (s->state));
7327 s->state = context_createValueTable (s, stateInfo_makeLoc (g_currentloc, SA_CREATED));
7329 DPRINTF (("Create: %s", sRef_unparseFull (s)));
7334 sRef_makeConst (ctype ct)
7336 sRef s = sRef_newRef ();
7341 s->defstate = SS_UNKNOWN;
7342 s->aliaskind = AK_UNKNOWN;
7343 sRef_setNullStateN (s, NS_UNKNOWN);
7345 /* start modification */
7346 s->bufinfo.bufstate = BB_NULLTERMINATED;
7347 /* end modification */
7349 /* evans 2002-04-22: added isManifestBool to avoid errors for -boolfalse initializations */
7350 if (!ctype_isManifestBool (ct) && ctype_isUA (ct))
7352 typeId uid = ctype_typeId (ct);
7353 uentry te = usymtab_getTypeEntrySafe (uid);
7355 if (uentry_isValid (te))
7357 sRef_mergeStateQuiet (s, uentry_getSref (te));
7361 s->oaliaskind = s->aliaskind;
7362 s->oexpkind = s->expkind;
7364 llassert (valueTable_isUndefined (s->state));
7365 s->state = context_createValueTable (s, stateInfo_makeLoc (g_currentloc, SA_CREATED));
7370 bool sRef_hasName (sRef s)
7372 if (sRef_isInvalid (s))
7381 uentry u = usymtab_getRefQuiet (s->info->cvar->lexlevel,
7382 s->info->cvar->index);
7383 return (uentry_hasName (u));
7387 if (s->info->paramno >= 0)
7389 uentry u = uentryList_getN (context_getParams (),
7392 return (uentry_hasName (u));
7396 llassert (s->info->paramno == PARAMUNKNOWN);
7406 sRef_sameName (sRef s1, sRef s2)
7408 if (sRef_isInvalid (s1))
7410 return sRef_isInvalid (s2);
7413 if (sRef_isInvalid (s2))
7421 if (s2->kind == SK_CVAR)
7423 return (s1->info->cvar->lexlevel == s2->info->cvar->lexlevel
7424 && s1->info->cvar->index == s2->info->cvar->index);
7426 else if (s2->kind == SK_PARAM)
7428 if (context_inFunctionLike ())
7430 if (s2->info->paramno != PARAMUNKNOWN)
7432 uentry u1 = usymtab_getRefQuiet (s1->info->cvar->lexlevel,
7433 s1->info->cvar->index);
7434 uentry u2 = uentryList_getN (context_getParams (),
7437 return (cstring_equalFree (uentry_getName (u1),
7438 uentry_getName (u2)));
7442 return s1->info->paramno == PARAMUNKNOWN;
7456 if (s2->kind == SK_PARAM)
7458 return (s1->info->paramno == s2->info->paramno);
7460 else if (s2->kind == SK_CVAR)
7462 if (context_inFunctionLike ())
7464 if (s1->info->paramno == PARAMUNKNOWN)
7470 uentry u1 = uentryList_getN (context_getParams (),
7472 uentry u2 = usymtab_getRefQuiet (s2->info->cvar->lexlevel,
7473 s2->info->cvar->index);
7476 return (cstring_equalFree (uentry_getName (u1),
7477 uentry_getName (u2)));
7491 case SK_UNCONSTRAINED:
7495 if (s2->kind == SK_ARRAYFETCH)
7497 if (bool_equal (s1->info->arrayfetch->indknown,
7498 s2->info->arrayfetch->indknown))
7500 if (!s1->info->arrayfetch->indknown
7501 || (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind))
7503 return sRef_sameName (s1->info->arrayfetch->arr,
7504 s2->info->arrayfetch->arr);
7511 if (s2->kind == SK_FIELD)
7513 if (cstring_equal (s1->info->field->field,
7514 s2->info->field->field))
7516 return sRef_sameName (s1->info->field->rec,
7517 s2->info->field->rec);
7526 if (s2->kind == s1->kind)
7528 return sRef_sameName (s1->info->ref,
7536 return sRef_sameName (sRef_getConjA (s1), s2);
7540 return (s2->kind == SK_UNKNOWN);
7543 if (s2->kind == s1->kind)
7545 return (ctype_equal (s1->type, s2->type));
7550 if (s2->kind == SK_SPECIAL)
7552 return (s1->info->spec == s2->info->spec);
7556 return (s2->kind == SK_RESULT);
7564 sRef_fixOuterRef (/*@returned@*/ sRef s)
7566 sRef root = sRef_getRootBase (s);
7568 if (sRef_isCvar (root))
7570 uentry ue = usymtab_getRefQuiet (root->info->cvar->lexlevel,
7571 root->info->cvar->index);
7573 if (uentry_isValid (ue))
7575 sRef uref = uentry_getSref (ue);
7576 sRef sr = sRef_fixBase (s, uref);
7582 llcontbug (message ("sRef_fixOuterRef: undefined: %q", sRef_unparseDebug (s)));
7591 sRef_storeState (sRef s)
7593 if (sRef_isInvalid (s)) return;
7595 sRef_checkMutable (s);
7596 s->oaliaskind = s->aliaskind;
7597 s->oexpkind = s->expkind;
7600 static void sRef_resetStateAux (sRef s, /*@unused@*/ fileloc loc)
7602 sRef_resetState (s);
7606 sRef_resetState (sRef s)
7608 bool changed = FALSE;
7609 if (sRef_isInvalid (s)) return;
7612 if (s->oaliaskind == AK_KILLREF && !sRef_isParam (s))
7615 ** killref is used in a kludgey way, to save having to add
7616 ** another alias kind (see usymtab_handleParams)
7619 if (s->expkind != s->oexpkind)
7622 s->expkind = s->oexpkind;
7627 if (s->expkind != s->oexpkind)
7630 s->expkind = s->oexpkind;
7633 if (s->aliaskind != s->oaliaskind
7634 && s->aliaskind != AK_REFCOUNTED
7635 && s->aliaskind != AK_REFS)
7638 s->aliaskind = s->oaliaskind;
7644 sRef_clearDerived (s);
7650 sRef_resetStateComplete (sRef s)
7652 sRef_innerAliasSetComplete (sRef_resetStateAux, s, fileloc_undefined);
7656 sRef_fixBase (/*@returned@*/ sRef s, /*@returned@*/ sRef base)
7658 sRef tmp = sRef_undefined;
7661 if (sRef_isInvalid (s)) return s;
7662 if (sRef_isInvalid (base)) return base;
7672 tmp = sRef_fixBase (s->info->arrayfetch->arr, base);
7674 if (s->info->arrayfetch->indknown)
7676 ret = sRef_makeArrayFetchKnown (tmp, s->info->arrayfetch->ind);
7680 ret = sRef_makeArrayFetch (tmp);
7684 tmp = sRef_fixBase (s->info->field->rec, base);
7685 ret = sRef_buildNCField (tmp, s->info->field->field);
7688 tmp = sRef_fixBase (s->info->ref, base);
7689 ret = sRef_makePointer (tmp);
7692 tmp = sRef_fixBase (s->info->ref, base);
7693 ret = sRef_makeAddress (tmp);
7699 tmp = sRef_fixBase (s->info->conj->a, base);
7700 tmpb = sRef_fixBase (s->info->conj->b, base);
7702 ret = sRef_makeConj (tmp, tmpb);
7711 static /*@exposed@*/ sRef
7712 sRef_fixDirectBase (sRef s, sRef base)
7717 if (sRef_isInvalid (s))
7719 return sRef_undefined;
7725 if (s->info->arrayfetch->indknown)
7727 ret = sRef_makeArrayFetchKnown (base, s->info->arrayfetch->ind);
7731 ret = sRef_makeArrayFetch (base);
7735 ret = sRef_buildNCField (base, s->info->field->field);
7738 ret = sRef_makePointer (base);
7741 ret = sRef_makeAddress (base);
7747 tmpa = sRef_fixDirectBase (s->info->conj->a, base);
7748 tmpb = sRef_fixDirectBase (s->info->conj->b, base);
7750 ret = sRef_makeConj (tmpa, tmpb);
7756 sRef_copyState (ret, s);
7761 sRef_isAllocIndexRef (sRef s)
7763 return (sRef_isArrayFetch (s) && !(s->info->arrayfetch->indknown)
7764 && sRef_isAllocated (s->info->arrayfetch->arr));
7768 sRef_showRefLost (sRef s)
7770 if (sRef_hasAliasInfoLoc (s))
7772 llgenindentmsg (cstring_makeLiteral ("Original reference lost"),
7773 sRef_getAliasInfoLoc (s));
7778 sRef_showRefKilled (sRef s)
7780 if (sRef_isValid (s))
7782 DPRINTF (("Killed: %s", sRef_unparseFull (s)));
7783 if (context_getLocIndentSpaces () == 0) {
7784 stateInfo_display (s->definfo, message (" Storage %q", sRef_unparseOpt (s)));
7786 stateInfo_display (s->definfo, message ("Storage %q", sRef_unparseOpt (s)));
7792 sRef_showStateInconsistent (sRef s)
7794 if (sRef_hasStateInfoLoc (s))
7797 (message ("Storage %qbecomes inconsistent (released on one branch)",
7798 sRef_unparseOpt (s)),
7799 sRef_getStateInfoLoc (s));
7804 sRef_showStateInfo (sRef s)
7806 if (sRef_isValid (s)) {
7807 if (context_getLocIndentSpaces () == 0) {
7808 stateInfo_display (s->definfo, message (" Storage %q", sRef_unparseOpt (s)));
7810 stateInfo_display (s->definfo, message ("Storage %q", sRef_unparseOpt (s)));
7816 sRef_showExpInfo (sRef s)
7818 if (sRef_isValid (s)) {
7819 if (context_getLocIndentSpaces () == 0) {
7820 stateInfo_display (s->expinfo, message (" Storage %q", sRef_unparseOpt (s)));
7822 stateInfo_display (s->expinfo, message ("Storage %q", sRef_unparseOpt (s)));
7828 sRef_showMetaStateInfo (sRef s, cstring key)
7831 metaStateInfo minfo = context_lookupMetaStateInfo (key);
7833 llassert (sRef_isReasonable (s));
7834 llassert (valueTable_isDefined (s->state));
7835 llassert (metaStateInfo_isDefined (minfo));
7837 val = valueTable_lookup (s->state, key);
7839 if (stateValue_hasLoc (val))
7842 (message ("%qbecomes %q", sRef_unparseOpt (s),
7843 stateValue_unparseValue (val, minfo)),
7844 stateValue_getLoc (val));
7849 sRef_showNullInfo (sRef s)
7851 DPRINTF (("Show null info: %s", sRef_unparseFull (s)));
7853 if (sRef_hasNullInfoLoc (s) && sRef_isKnown (s))
7855 DPRINTF (("has null info: %s",
7856 fileloc_unparse (sRef_getNullInfoLoc (s))));
7858 switch (sRef_getNullState (s))
7862 fileloc loc = sRef_getNullInfoLoc (s);
7864 if (fileloc_isDefined (loc) && !fileloc_isLib (loc))
7867 (message ("Storage %qbecomes null", sRef_unparseOpt (s)),
7874 fileloc loc = sRef_getNullInfoLoc (s);
7876 if (fileloc_isDefined (loc) && !fileloc_isLib (loc))
7878 llgenindentmsg (message ("Storage %qbecomes null", sRef_unparseOpt (s)),
7886 (message ("Storage %qmay become null", sRef_unparseOpt (s)),
7887 sRef_getNullInfoLoc (s));
7892 (message ("Storage %qbecomes not null", sRef_unparseOpt (s)),
7893 sRef_getNullInfoLoc (s));
7897 (message ("Storage %qnull state becomes unknown",
7898 sRef_unparseOpt (s)),
7899 sRef_getNullInfoLoc (s));
7908 (message ("<error case> Storage %q becomes %s",
7910 nstate_unparse (sRef_getNullState (s))),
7911 sRef_getNullInfoLoc (s));
7919 sRef_showAliasInfo (sRef s)
7921 if (sRef_isValid (s))
7923 if (sRef_isFresh (s))
7925 if (context_getLocIndentSpaces () == 0) {
7926 stateInfo_display (s->aliasinfo, message (" Fresh storage %q", sRef_unparseOpt (s)));
7928 stateInfo_display (s->aliasinfo, message ("Fresh storage %q", sRef_unparseOpt (s)));
7933 if (context_getLocIndentSpaces () == 0) {
7934 stateInfo_display (s->aliasinfo, message (" Storage %q", sRef_unparseOpt (s)));
7936 stateInfo_display (s->aliasinfo, message ("Storage %q", sRef_unparseOpt (s)));
7943 sRef_mergeNullState (sRef s, nstate n)
7945 if (sRef_isReasonable (s))
7949 old = sRef_getNullState (s);
7951 if (n != old && n != NS_UNKNOWN)
7953 sRef_setNullState (s, n, g_currentloc);
7958 llbuglit ("sRef_mergeNullState: invalid");
7963 sRef_possiblyNull (sRef s)
7965 if (sRef_isReasonable (s))
7967 if (sRef_getNullState (s) == NS_ABSNULL)
7969 ctype rct = ctype_realType (s->type);
7971 if (ctype_isAbstract (rct))
7977 if (ctype_isUser (rct))
7979 uentry ue = usymtab_getTypeEntry (ctype_typeId (rct));
7981 return (nstate_possiblyNull
7982 (sRef_getNullState (uentry_getSref (ue))));
7992 return nstate_possiblyNull (sRef_getNullState (s));
8000 sRef_getScopeName (sRef s)
8002 sRef base = sRef_getRootBase (s);
8004 if (sRef_isRealGlobal (base))
8006 return (cstring_makeLiteralTemp ("Global"));
8008 else if (sRef_isFileStatic (base))
8010 return (cstring_makeLiteralTemp ("Static"));
8014 return (cstring_makeLiteralTemp ("Local"));
8019 sRef_unparseScope (sRef s)
8021 sRef base = sRef_getRootBase (s);
8023 if (sRef_isRealGlobal (base))
8025 return (cstring_makeLiteralTemp ("global"));
8027 else if (sRef_isFileStatic (base))
8029 return (cstring_makeLiteralTemp ("file static"));
8038 sRef_getScope (sRef s)
8040 llassert (sRef_isReasonable (s));
8042 if (sRef_isCvar (s))
8044 return s->info->cvar->lexlevel;
8046 else if (sRef_isParam (s))
8057 sRef_isDead (sRef s)
8059 return (sRef_isReasonable (s) && (s)->defstate == SS_DEAD);
8063 sRef_isDeadStorage (sRef s)
8065 if (sRef_isReasonable (s))
8067 if (s->defstate == SS_DEAD
8068 || s->defstate == SS_UNUSEABLE
8069 || s->defstate == SS_UNDEFINED
8070 || s->defstate == SS_UNKNOWN)
8076 return (sRef_isDefinitelyNull (s));
8086 sRef_isPossiblyDead (sRef s)
8088 return (sRef_isReasonable (s) && s->defstate == SS_HOFFA);
8091 bool sRef_isStateLive (sRef s)
8093 if (sRef_isReasonable (s))
8095 sstate ds = s->defstate;
8097 return (!(ds == SS_UNDEFINED
8099 || ds == SS_UNUSEABLE
8100 || ds == SS_HOFFA));
8109 bool sRef_isStateUndefined (sRef s)
8111 return ((sRef_isReasonable(s)) && ((s)->defstate == SS_UNDEFINED));
8114 bool sRef_isJustAllocated (sRef s)
8116 if (sRef_isAllocated (s))
8118 sRefSet_allElements (s->deriv, el)
8120 if (!(sRef_isStateUndefined (el) || sRef_isUnuseable (el)))
8124 } end_sRefSet_allElements ;
8133 sRef_isAllocatedStorage (sRef s)
8135 if (sRef_isReasonable (s) && ynm_toBoolStrict (sRef_isValidLvalue (s)))
8137 return (ctype_isVisiblySharable (sRef_getType (s)));
8146 sRef_isUnuseable (sRef s)
8148 return ((sRef_isValid(s)) && ((s)->defstate == SS_UNUSEABLE));
8152 sRef_perhapsNull (sRef s)
8154 if (sRef_isReasonable (s))
8156 if (sRef_getNullState (s) == NS_ABSNULL)
8158 ctype rct = ctype_realType (s->type);
8160 if (ctype_isAbstract (rct))
8166 if (ctype_isUser (rct))
8168 uentry ue = usymtab_getTypeEntry (ctype_typeId (rct));
8170 return (nstate_perhapsNull
8171 (sRef_getNullState (uentry_getSref (ue))));
8181 return nstate_perhapsNull (sRef_getNullState (s));
8189 ** definitelyNull --- called when TRUE is good
8193 sRef_definitelyNull (sRef s)
8195 return (sRef_isReasonable (s)
8196 && (sRef_getNullState (s) == NS_DEFNULL || sRef_getNullState (s) == NS_CONSTNULL));
8200 ** based on sRef_similar
8204 sRef_setDerivNullState (sRef set, sRef guide, nstate ns)
8206 if (sRef_isReasonable (set))
8208 sRef deriv = sRef_getDeriv (set, guide);
8210 if (sRef_isReasonable (deriv))
8212 sRef_setNullStateN (deriv, ns);
8217 static /*@exposed@*/ sRef
8218 sRef_getDeriv (/*@returned@*/ /*@notnull@*/ sRef set, sRef guide)
8220 llassert (sRef_isReasonable (set));
8221 llassert (sRef_isReasonable (guide));
8223 switch (guide->kind)
8226 llassert (set->kind == SK_CVAR);
8230 llassert (set->kind == guide->kind);
8231 llassert (set->info->paramno == guide->info->paramno);
8236 if (set->kind == SK_ARRAYFETCH
8237 && (sRef_similar (set->info->arrayfetch->arr,
8238 guide->info->arrayfetch->arr)))
8244 return (sRef_makeAnyArrayFetch
8245 (sRef_getDeriv (set, guide->info->arrayfetch->arr)));
8250 if (set->kind == SK_PTR && sRef_similar (set->info->ref, guide->info->ref))
8256 return (sRef_makePointer (sRef_getDeriv (set, guide->info->ref)));
8261 if ((set->kind == SK_FIELD &&
8262 (sRef_similar (set->info->field->rec, guide->info->field->rec) &&
8263 cstring_equal (set->info->field->field, guide->info->field->field))))
8269 return (sRef_makeField (sRef_getDeriv (set, guide->info->field->rec),
8270 guide->info->field->field));
8274 if ((set->kind == SK_ADR) && sRef_similar (set->info->ref, guide->info->ref))
8280 return (sRef_makeAddress (sRef_getDeriv (set, guide->info->ref)));
8285 return sRef_undefined;
8289 case SK_UNCONSTRAINED:
8297 return sRef_undefined;
8304 ** sRef_aliasCheckPred
8306 ** A confusing but spiffy function:
8308 ** Calls predf (s, e, text, <alias>) on s and all of s's aliases
8309 ** (unless checkAliases (s) is FALSE).
8311 ** For alias calls, calls as
8312 ** predf (alias, e, text, s)
8316 sRef_aliasCheckPred (bool (predf) (sRef, exprNode, sRef, exprNode),
8317 /*@null@*/ bool (checkAliases) (sRef),
8318 sRef s, exprNode e, exprNode err)
8320 bool error = (*predf)(s, e, sRef_undefined, err);
8323 if (checkAliases != NULL && !(checkAliases (s)))
8325 /* don't check aliases */
8329 sRefSet aliases = usymtab_allAliases (s);
8331 sRefSet_realElements (aliases, current)
8333 if (sRef_isReasonable (current))
8335 if (sRef_isReasonable (current))
8337 if (!sRef_similar (current, s)
8338 || (error && sRef_sameName (current, s)))
8340 (void) (*predf)(current, e, s, err);
8345 llcontbug (message ("Invalid sRef as alias field of %s", sRef_unparse (s)));
8348 } end_sRefSet_realElements;
8350 sRefSet_free (aliases);
8355 ** return TRUE iff predf (s) is true for s or any alias of s
8359 sRef_aliasCheckSimplePred (sRefTest predf, sRef s)
8370 aliases = usymtab_allAliases (s);
8372 sRefSet_realElements (aliases, current)
8374 if (sRef_isReasonable (current))
8376 sRef cref = sRef_updateSref (current);
8378 /* Whoa! a very kludgey way to make sure the right sref is used
8379 ** where there is a conditional symbol table. I am beginning
8380 ** to think that having a conditional symbol table wasn't such
8386 DPRINTF (("Checking alias: %s", sRef_unparseFull (cref)));
8387 sRefSet_free (aliases);
8391 } end_sRefSet_realElements;
8393 sRefSet_free (aliases);
8399 sRef_aliasCompleteSimplePred (bool (predf) (sRef), sRef s)
8402 bool result = FALSE;
8405 aliases = usymtab_allAliases (s);
8407 if ((*predf)(s)) result = TRUE;
8410 sRefSet_realElements (aliases, current)
8412 if (sRef_isReasonable (current))
8414 current = sRef_updateSref (current);
8415 if ((*predf)(current)) result = TRUE;
8417 } end_sRefSet_realElements;
8419 sRefSet_free (aliases);
8424 sRef_aliasSetComplete (void (predf) (sRef, fileloc), sRef s, fileloc loc)
8428 aliases = usymtab_allAliases (s);
8430 DPRINTF (("All aliases: %s", sRefSet_unparseFull (aliases)));
8434 sRefSet_realElements (aliases, current)
8436 if (sRef_isReasonable (current))
8438 current = sRef_updateSref (current);
8439 ((*predf)(current, loc));
8440 DPRINTF (("Killed: %s", sRef_unparseFull (current)));
8442 } end_sRefSet_realElements;
8444 sRefSet_free (aliases);
8448 sRef_aliasSetCompleteParam (void (predf) (sRef, int, fileloc), sRef s,
8449 int kind, fileloc loc)
8453 if (sRef_isDeep (s))
8455 aliases = usymtab_allAliases (s);
8459 aliases = usymtab_aliasedBy (s);
8462 (*predf)(s, kind, loc);
8464 sRefSet_realElements (aliases, current)
8466 if (sRef_isReasonable (current))
8468 current = sRef_updateSref (current);
8469 ((*predf)(current, kind, loc));
8471 } end_sRefSet_realElements;
8473 sRefSet_free (aliases);
8477 ** Version of aliasSetCompleteParam for alkind parameters
8481 sRef_aliasSetCompleteAlkParam (void (predf) (sRef, alkind, fileloc), sRef s,
8482 alkind kind, fileloc loc)
8486 if (sRef_isDeep (s))
8488 aliases = usymtab_allAliases (s);
8492 aliases = usymtab_aliasedBy (s);
8495 (*predf)(s, kind, loc);
8497 sRefSet_realElements (aliases, current)
8499 if (sRef_isReasonable (current))
8501 current = sRef_updateSref (current);
8502 ((*predf)(current, kind, loc));
8504 } end_sRefSet_realElements;
8506 sRefSet_free (aliases);
8510 sRef_innerAliasSetComplete (void (predf) (sRef, fileloc), sRef s, fileloc loc)
8516 if (!sRef_isReasonable (s)) return;
8519 ** Type equivalence checking is necessary --- there might be casting.
8526 case SK_UNCONSTRAINED:
8531 inner = s->info->ref;
8532 aliases = usymtab_allAliases (inner);
8533 ct = sRef_getType (inner);
8535 sRefSet_realElements (aliases, current)
8537 if (sRef_isReasonable (current))
8539 current = sRef_updateSref (current);
8541 if (ctype_equal (ct, sRef_getType (current)))
8543 sRef ptr = sRef_makePointer (current);
8544 ((*predf)(ptr, loc));
8547 } end_sRefSet_realElements;
8549 sRefSet_free (aliases);
8552 inner = s->info->arrayfetch->arr;
8553 aliases = usymtab_allAliases (inner);
8554 ct = sRef_getType (inner);
8556 DPRINTF (("Array fetch: %s", sRefSet_unparse (aliases)));
8558 sRefSet_realElements (aliases, current)
8560 if (sRef_isReasonable (current))
8562 current = sRef_updateSref (current);
8563 DPRINTF (("Current: %s", sRef_unparseFull (current)));
8565 if (ctype_equal (ct, sRef_getType (current)))
8567 if (s->info->arrayfetch->indknown)
8569 sRef af = sRef_makeArrayFetchKnown (current, s->info->arrayfetch->ind);
8570 DPRINTF (("Defining: %s", sRef_unparseFull (af)));
8571 /* evans 2001-08-27 This isn't true:
8572 llassert (af->info->arrayfetch->arr == current);
8573 see comments in buildArrayFetchKnown
8575 ((*predf)(af, loc));
8579 sRef af = sRef_makeArrayFetch (current);
8580 /* evans 2001-08-27 This isn't true:
8581 llassert (af->info->arrayfetch->arr == current);
8582 see comments in buildArrayFetch
8584 DPRINTF (("Defining: %s", sRef_unparseFull (af)));
8585 ((*predf)(af, loc));
8590 DPRINTF (("Type mismatch: %s / %s",
8592 ctype_unparse (sRef_getType (current))));
8595 } end_sRefSet_realElements;
8597 sRefSet_free (aliases);
8600 inner = s->info->field->rec;
8601 aliases = usymtab_allAliases (inner);
8602 ct = sRef_getType (inner);
8604 sRefSet_realElements (aliases, current)
8606 if (sRef_isReasonable (current))
8608 current = sRef_updateSref (current);
8610 if (ctype_equal (ct, sRef_getType (current)))
8612 sRef f = sRef_makeField (current, s->info->field->field);
8617 } end_sRefSet_realElements;
8619 sRefSet_free (aliases);
8622 sRef_innerAliasSetComplete (predf, s->info->conj->a, loc);
8623 sRef_innerAliasSetComplete (predf, s->info->conj->b, loc);
8640 sRef_innerAliasSetCompleteParam (void (predf) (sRef, sRef), sRef s, sRef t)
8646 if (!sRef_isReasonable (s)) return;
8649 ** Type equivalence checking is necessary --- there might be casting.
8656 case SK_UNCONSTRAINED:
8661 inner = s->info->ref;
8662 aliases = usymtab_allAliases (inner);
8663 ct = sRef_getType (inner);
8665 sRefSet_realElements (aliases, current)
8667 if (sRef_isReasonable (current))
8669 current = sRef_updateSref (current);
8671 if (ctype_equal (ct, sRef_getType (current)))
8673 sRef ptr = sRef_makePointer (current);
8678 } end_sRefSet_realElements;
8680 sRefSet_free (aliases);
8683 inner = s->info->arrayfetch->arr;
8684 aliases = usymtab_allAliases (inner);
8685 ct = sRef_getType (inner);
8687 sRefSet_realElements (aliases, current)
8689 if (sRef_isReasonable (current))
8691 current = sRef_updateSref (current);
8693 if (ctype_equal (ct, sRef_getType (current)))
8696 if (s->info->arrayfetch->indknown)
8698 sRef af = sRef_makeArrayFetchKnown (current, s->info->arrayfetch->ind);
8704 sRef af = sRef_makeArrayFetch (current);
8710 } end_sRefSet_realElements;
8712 sRefSet_free (aliases);
8715 inner = s->info->field->rec;
8716 aliases = usymtab_allAliases (inner);
8717 ct = sRef_getType (inner);
8720 sRefSet_realElements (aliases, current)
8722 if (sRef_isReasonable (current))
8724 current = sRef_updateSref (current);
8726 if (ctype_equal (ct, sRef_getType (current)))
8728 sRef f = sRef_makeField (current, s->info->field->field);
8733 } end_sRefSet_realElements;
8735 sRefSet_free (aliases);
8738 sRef_innerAliasSetCompleteParam (predf, s->info->conj->a, t);
8739 sRef_innerAliasSetCompleteParam (predf, s->info->conj->b, t);
8755 static void sRef_combineExKinds (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other)
8757 exkind a1 = sRef_getExKind (res);
8758 exkind a2 = sRef_getExKind (other);
8760 if (a1 == a2 || a2 == XO_UNKNOWN)
8764 else if (a1 == XO_UNKNOWN)
8766 res->expinfo = stateInfo_update (res->expinfo, other->expinfo);
8771 res->expkind = XO_OBSERVER;
8776 ** Currently, this is a very ad hoc implementation, with lots of fixes to
8777 ** make real code work okay. I need to come up with some more general
8778 ** rules or principles here.
8782 sRef_combineAliasKindsError (/*@notnull@*/ sRef res,
8783 /*@notnull@*/ sRef other,
8784 clause cl, fileloc loc)
8786 bool hasError = FALSE;
8787 alkind ares = sRef_getAliasKind (res);
8788 alkind aother = sRef_getAliasKind (other);
8790 sRef_checkMutable (res);
8792 if (alkind_isDependent (ares))
8794 if (aother == AK_KEPT)
8796 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8797 res->aliaskind = AK_KEPT;
8801 if (aother == AK_LOCAL || aother == AK_STATIC
8802 || alkind_isTemp (aother))
8804 DPRINTF (("Setting dependent: %s", sRef_unparseFull (res)));
8805 res->aliaskind = AK_DEPENDENT;
8809 else if (alkind_isDependent (aother))
8811 if (ares == AK_KEPT)
8813 res->aliaskind = AK_KEPT;
8817 if (ares == AK_LOCAL || ares == AK_STATIC || alkind_isTemp (ares))
8819 DPRINTF (("Setting dependent: %s", sRef_unparseFull (res)));
8820 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8821 res->aliaskind = AK_DEPENDENT;
8825 else if ((ares == AK_LOCAL || ares == AK_UNIQUE
8826 || ares == AK_STATIC || alkind_isTemp (ares))
8827 && sRef_isFresh (other))
8830 ** cases like: if (s == NULL) s = malloc...;
8831 ** don't generate errors
8834 if (usymtab_isAltDefinitelyNullDeep (res))
8836 res->aliaskind = ares;
8843 else if ((aother == AK_LOCAL || aother == AK_UNIQUE
8844 || aother == AK_STATIC || alkind_isTemp (aother))
8845 && sRef_isFresh (res))
8848 ** cases like: if (s == NULL) s = malloc...;
8849 ** don't generate errors
8852 if (usymtab_isDefinitelyNullDeep (other))
8854 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8855 res->aliaskind = aother;
8862 else if (ares == AK_NEWREF && aother == AK_REFCOUNTED
8863 && sRef_isConst (other))
8865 res->aliaskind = AK_NEWREF;
8867 else if (aother == AK_NEWREF && ares == AK_REFCOUNTED
8868 && sRef_isConst (res))
8870 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8871 res->aliaskind = AK_NEWREF;
8873 else if (sRef_isLocalVar (res)
8874 && ((ares == AK_KEPT && aother == AK_LOCAL)
8875 || (aother == AK_KEPT && ares == AK_LOCAL)))
8877 res->aliaskind = AK_KEPT;
8879 else if ((ares == AK_OWNED && aother == AK_ONLY)
8880 || (aother == AK_OWNED && ares == AK_ONLY))
8882 res->aliaskind = AK_OWNED;
8884 if (aother == AK_OWNED)
8886 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8896 if (sRef_isThroughArrayFetch (res))
8899 (FLG_BRANCHSTATE, FLG_STRICTBRANCHSTATE,
8901 ("Clauses exit with %q possibly referencing %s storage %s, "
8904 alkind_unparse (aother),
8905 clause_nameTaken (cl),
8906 alkind_unparse (ares),
8907 clause_nameAlternate (cl)),
8910 sRef_showAliasInfo (res);
8911 sRef_showAliasInfo (other);
8912 res->aliaskind = AK_ERROR;
8916 if (ares == AK_KEPT || aother == AK_KEPT)
8918 sRef_maybeKill (res, loc);
8926 message ("Clauses exit with %q referencing %s storage %s, "
8929 alkind_unparse (aother),
8930 clause_nameTaken (cl),
8931 alkind_unparse (ares),
8932 clause_nameAlternate (cl)),
8935 sRef_showAliasInfo (res);
8936 sRef_showAliasInfo (other);
8938 res->aliaskind = AK_ERROR;
8942 res->aliaskind = (sRef_isLocalVar (res) ? AK_LOCAL : AK_UNKNOWN);
8947 sRef_combineAliasKinds (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other,
8948 clause cl, fileloc loc)
8950 alkind ares = sRef_getAliasKind (res);
8951 alkind aother = sRef_getAliasKind (other);
8953 sRef_checkMutable (res);
8955 DPRINTF (("Combine alias kinds: \n\t%s / \n\t%s",
8956 sRef_unparseFull (res), sRef_unparseFull (other)));
8957 if (alkind_equal (ares, aother)
8958 || aother == AK_UNKNOWN
8959 || aother == AK_ERROR)
8961 ; /* keep current state */
8963 else if (sRef_isDead (res) || sRef_isDead (other))
8965 /* dead error reported (or storage is dead) */
8966 res ->aliaskind = AK_ERROR;
8968 else if (ares == AK_UNKNOWN || ares == AK_ERROR
8969 || sRef_isStateUndefined (res)
8970 || sRef_isDefinitelyNull (res))
8972 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8973 res->aliaskind = aother;
8975 else if (sRef_isStateUndefined (other)
8976 || sRef_isDefinitelyNull (other))
8980 else if (((ares == AK_UNIQUE || alkind_isTemp (ares))
8981 && aother == AK_LOCAL)
8982 || ((aother == AK_UNIQUE || alkind_isTemp (aother))
8983 && ares == AK_LOCAL))
8985 if (ares != AK_LOCAL)
8987 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8990 res->aliaskind = AK_LOCAL;
8992 else if ((ares == AK_OWNED && aother == AK_FRESH)
8993 || (aother == AK_OWNED && ares == AK_FRESH))
8995 if (ares != AK_FRESH)
8997 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
9000 res->aliaskind = AK_FRESH;
9002 else if ((ares == AK_KEEP && aother == AK_FRESH) ||
9003 (aother == AK_KEEP && ares == AK_FRESH))
9005 if (ares != AK_KEEP)
9007 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
9010 res->aliaskind = AK_KEEP;
9012 else if ((ares == AK_LOCAL && aother == AK_STACK) ||
9013 (aother == AK_LOCAL && ares == AK_STACK))
9015 if (ares != AK_STACK)
9017 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
9020 res->aliaskind = AK_STACK;
9022 else if ((ares == AK_LOCAL
9023 && (aother == AK_OWNED && sRef_isLocalVar (other)))
9024 || (aother == AK_LOCAL
9025 && (ares == AK_OWNED && sRef_isLocalVar (res))))
9027 if (ares != AK_LOCAL)
9029 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
9032 res->aliaskind = AK_LOCAL;
9034 else if ((ares == AK_FRESH && alkind_isOnly (aother))
9035 || (aother == AK_FRESH && alkind_isOnly (ares)))
9037 res->aliaskind = AK_FRESH;
9039 else if ((aother == AK_FRESH && sRef_definitelyNull (res))
9040 || (ares == AK_FRESH && sRef_definitelyNull (other)))
9042 if (ares != AK_FRESH)
9044 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
9045 res->aliaskind = AK_FRESH;
9048 else if ((sRef_isFresh (res) && sRef_isConst (other))
9049 || (sRef_isFresh (other) && sRef_isConst (res)))
9052 ** for NULL constantants
9056 if (!sRef_isFresh (res))
9058 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
9061 res->aliaskind = AK_FRESH;
9063 else if ((alkind_isStatic (aother) && sRef_isConst (res))
9064 || (alkind_isStatic (ares) && sRef_isConst (other)))
9066 if (!alkind_isStatic (ares))
9068 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
9069 res->aliaskind = AK_STATIC;
9074 sRef_combineAliasKindsError (res, other, cl, loc);
9078 static void sRef_combineDefState (/*@notnull@*/ sRef res,
9079 /*@notnull@*/ sRef other)
9081 sstate s1 = res->defstate;
9082 sstate s2 = other->defstate;
9085 sRef_checkMutable (res);
9087 if (s1 == s2 || s2 == SS_UNKNOWN)
9091 else if (s1 == SS_UNKNOWN)
9100 if (s2 == SS_DEFINED)
9106 llcontbuglit ("ssfixed: not implemented");
9115 flip = (s2 != SS_DEFINED);
9133 res->definfo = stateInfo_update (res->definfo, other->definfo);
9138 extern /*@notnull@*/ sRef sRef_getConjA (sRef s)
9141 llassert (sRef_isConj (s));
9143 ret = s->info->conj->a;
9144 llassert (ret != NULL);
9148 extern /*@notnull@*/ sRef sRef_getConjB (sRef s)
9151 llassert (sRef_isConj (s));
9153 ret = s->info->conj->b;
9154 llassert (ret != NULL);
9158 extern /*@exposed@*/ sRef sRef_makeArrow (sRef s, /*@dependent@*/ cstring f)
9163 p = sRef_makePointer (s);
9164 ret = sRef_makeField (p, f);
9165 DPRINTF (("Arrow: %s => %s",
9166 sRef_unparseFull (s), sRef_unparseFull (ret)));
9170 extern /*@exposed@*/ sRef sRef_buildArrow (sRef s, cstring f)
9175 p = sRef_buildPointer (s);
9176 ret = sRef_buildField (p, f);
9181 static /*@null@*/ sinfo sinfo_copy (/*@notnull@*/ sRef s)
9188 ret = (sinfo) dmalloc (sizeof (*ret));
9189 ret->cvar = (cref) dmalloc (sizeof (*ret->cvar));
9190 ret->cvar->lexlevel = s->info->cvar->lexlevel;
9191 ret->cvar->index = s->info->cvar->index;
9195 ret = (sinfo) dmalloc (sizeof (*ret));
9196 ret->paramno = s->info->paramno;
9197 llassert (ret->paramno >= -1);
9201 ret = (sinfo) dmalloc (sizeof (*ret));
9202 ret->arrayfetch = (ainfo) dmalloc (sizeof (*ret->arrayfetch));
9203 ret->arrayfetch->indknown = s->info->arrayfetch->indknown;
9204 ret->arrayfetch->ind = s->info->arrayfetch->ind;
9205 ret->arrayfetch->arr = s->info->arrayfetch->arr;
9209 ret = (sinfo) dmalloc (sizeof (*ret));
9210 ret->field = (fldinfo) dmalloc (sizeof (*ret->field));
9211 ret->field->rec = s->info->field->rec;
9212 ret->field->field = s->info->field->field;
9216 ret = (sinfo) dmalloc (sizeof (*ret));
9217 ret->object = s->info->object;
9224 ret = (sinfo) dmalloc (sizeof (*ret));
9225 ret->ref = s->info->ref; /* Ref_copy (s->info->ref); */
9229 ret = (sinfo) dmalloc (sizeof (*ret));
9230 ret->conj = (cjinfo) dmalloc (sizeof (*ret->conj));
9231 ret->conj->a = s->info->conj->a; /* sRef_copy (s->info->conj->a); */
9232 ret->conj->b = s->info->conj->b; /* sRef_copy (s->info->conj->b);*/
9235 ret = (sinfo) dmalloc (sizeof (*ret));
9236 ret->spec = s->info->spec;
9239 case SK_UNCONSTRAINED:
9241 ret = (sinfo) dmalloc (sizeof (*ret));
9242 ret->fname = s->info->fname;
9249 llassertprint (s->info == NULL, ("s = %s", sRef_unparse (s)));
9257 static /*@null@*/ sinfo sinfo_fullCopy (/*@notnull@*/ sRef s)
9262 ** Since its a full copy, only storage is assigned
9263 ** to dependent fields.
9270 ret = (sinfo) dmalloc (sizeof (*ret));
9271 ret->cvar = (cref) dmalloc (sizeof (*ret->cvar));
9272 ret->cvar->lexlevel = s->info->cvar->lexlevel;
9273 ret->cvar->index = s->info->cvar->index;
9277 ret = (sinfo) dmalloc (sizeof (*ret));
9278 ret->paramno = s->info->paramno;
9279 llassert (ret->paramno >= -1);
9283 ret = (sinfo) dmalloc (sizeof (*ret));
9284 ret->arrayfetch = (ainfo) dmalloc (sizeof (*ret->arrayfetch));
9285 ret->arrayfetch->indknown = s->info->arrayfetch->indknown;
9286 ret->arrayfetch->ind = s->info->arrayfetch->ind;
9287 ret->arrayfetch->arr = sRef_saveCopy (s->info->arrayfetch->arr);
9291 ret = (sinfo) dmalloc (sizeof (*ret));
9292 ret->field = (fldinfo) dmalloc (sizeof (*ret->field));
9293 ret->field->rec = sRef_saveCopy (s->info->field->rec);
9294 ret->field->field = s->info->field->field;
9298 ret = (sinfo) dmalloc (sizeof (*ret));
9299 ret->object = s->info->object;
9306 ret = (sinfo) dmalloc (sizeof (*ret));
9307 ret->ref = sRef_saveCopy (s->info->ref);
9311 ret = (sinfo) dmalloc (sizeof (*ret));
9312 ret->conj = (cjinfo) dmalloc (sizeof (*ret->conj));
9313 ret->conj->a = sRef_saveCopy (s->info->conj->a);
9314 ret->conj->b = sRef_saveCopy (s->info->conj->b);
9317 ret = (sinfo) dmalloc (sizeof (*ret));
9318 ret->spec = s->info->spec;
9321 case SK_UNCONSTRAINED:
9322 ret = (sinfo) dmalloc (sizeof (*ret));
9323 ret->fname = s->info->fname;
9329 llassert (s->info == NULL);
9339 sinfo_update (/*@notnull@*/ /*@exposed@*/ sRef res,
9340 /*@notnull@*/ /*@exposed@*/ sRef other)
9342 llassert (res->kind == other->kind);
9347 res->info->cvar->lexlevel = other->info->cvar->lexlevel;
9348 res->info->cvar->index = other->info->cvar->index;
9352 res->info->paramno = other->info->paramno;
9353 llassert (res->info->paramno >= -1);
9357 res->info->arrayfetch->indknown = other->info->arrayfetch->indknown;
9358 res->info->arrayfetch->ind = other->info->arrayfetch->ind;
9359 res->info->arrayfetch->arr = other->info->arrayfetch->arr;
9363 res->info->field->rec = other->info->field->rec;
9364 res->info->field->field = other->info->field->field;
9368 res->info->object = other->info->object;
9375 res->info->ref = other->info->ref;
9379 res->info->conj->a = other->info->conj->a;
9380 res->info->conj->b = other->info->conj->b;
9384 res->info->spec = other->info->spec;
9388 case SK_UNCONSTRAINED:
9389 res->info->fname = other->info->fname;
9396 llassert (res->info == NULL);
9401 static void sinfo_free (/*@special@*/ /*@temp@*/ /*@notnull@*/ sRef s)
9402 /*@uses s->kind, s->info@*/
9403 /*@releases s->info@*/
9408 DPRINTF (("Free sinfo: [%p]", s->info->cvar));
9409 sfree (s->info->cvar);
9416 DPRINTF (("Free sinfo: [%p]", s->info->arrayfetch));
9417 sfree (s->info->arrayfetch);
9421 DPRINTF (("Free sinfo: [%p]", s->info->field));
9422 sfree (s->info->field);
9431 case SK_EXTERNAL: /* is copy now! */
9435 DPRINTF (("Free sinfo: [%p]", s->info->conj));
9436 sfree (s->info->conj);
9439 case SK_UNCONSTRAINED:
9449 if (s->info != NULL) {
9450 DPRINTF (("Free sinfo: [%p]", s->info));
9456 bool sRef_isNSLocalVar (sRef s)
9458 if (sRef_isLocalVar (s))
9460 uentry ue = sRef_getUentry (s);
9462 return (!uentry_isStatic (ue));
9470 bool sRef_isLocalVar (sRef s)
9472 if (sRef_isValid(s))
9474 return (s->kind == SK_CVAR
9475 && (s->info->cvar->lexlevel > fileScope));
9481 bool sRef_isRealLocalVar (sRef s)
9483 if (sRef_isValid(s))
9485 if (s->kind == SK_CVAR)
9487 if (s->info->cvar->lexlevel == functionScope)
9489 uentry ue = sRef_getUentry (s);
9491 if (uentry_isAnyParam (ue)
9492 || uentry_isRefParam (ue))
9503 return (s->info->cvar->lexlevel > functionScope);
9511 bool sRef_isLocalParamVar (sRef s)
9513 if (sRef_isValid(s))
9515 return (s->kind == SK_PARAM
9516 || (s->kind == SK_CVAR
9517 && (s->info->cvar->lexlevel > fileScope)));
9523 static speckind speckind_fromInt (int i)
9526 llassert (i >= SR_NOTHING && i <= SR_SYSTEM);
9529 return ((speckind) i);
9533 static void sRef_updateNullState (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other)
9536 res->nullstate = other->nullstate;
9537 res->nullinfo = stateInfo_update (res->nullinfo, other->nullinfo);
9538 sRef_resetAliasKind (res);
9541 void sRef_combineNullState (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other)
9543 nstate n1 = sRef_getNullState (res);
9544 nstate n2 = sRef_getNullState (other);
9548 if (n1 == n2 || n2 == NS_UNKNOWN)
9554 /* note: n2 is not unknown or defnull */
9558 case NS_ERROR: nn = NS_ERROR; break;
9559 case NS_UNKNOWN: flip = TRUE; nn = n2; break;
9560 case NS_POSNULL: break;
9561 case NS_DEFNULL: nn = NS_POSNULL; break;
9562 case NS_RELNULL: break;
9564 if (n2 == NS_MNOTNULL)
9575 if (n2 == NS_NOTNULL)
9594 res->nullinfo = stateInfo_update (res->nullinfo, other->nullinfo);
9597 res->nullstate = nn;
9598 sRef_resetAliasKind (res);
9601 cstring sRef_nullMessage (sRef s)
9603 llassert (sRef_isReasonable (s));
9605 switch (sRef_getNullState (s))
9609 return (cstring_makeLiteralTemp ("null"));
9611 return (cstring_makeLiteralTemp ("possibly null"));
9616 /*@observer@*/ cstring sRef_ntMessage (sRef s)
9618 llassert (sRef_isReasonable (s));
9620 switch (s->nullstate)
9624 return (cstring_makeLiteralTemp ("not nullterminated"));
9626 return (cstring_makeLiteralTemp ("possibly non-nullterminated"));
9633 sRef sRef_fixResultType (/*@returned@*/ sRef s, ctype typ, uentry ue)
9635 sRef tmp = sRef_undefined;
9638 llassert (sRef_isReasonable (s));
9649 tmp = sRef_fixResultType (s->info->arrayfetch->arr, typ, ue);
9651 ct = ctype_realType (sRef_getType (tmp));
9654 if (ctype_isKnown (ct))
9656 if (ctype_isAP (ct))
9665 ("Special clause indexes non-array (%t): *%q",
9666 ct, sRef_unparse (s->info->arrayfetch->arr)),
9667 uentry_whereLast (ue));
9671 tmp = sRef_fixResultType (s->info->arrayfetch->arr, typ, ue);
9673 if (s->info->arrayfetch->indknown)
9675 ret = sRef_makeArrayFetchKnown (tmp, s->info->arrayfetch->ind);
9679 ret = sRef_makeArrayFetch (tmp);
9685 sRef rec = sRef_fixResultType (s->info->field->rec, typ, ue);
9686 ctype ct = ctype_realType (sRef_getType (rec));
9688 if (ctype_isKnown (ct))
9690 if (ctype_isSU (ct))
9692 if (uentry_isValid (uentryList_lookupField (ctype_getFields (ct),
9693 s->info->field->field)))
9702 ("Special clause accesses non-existent field of result: %q.%s",
9703 sRef_unparse (rec), s->info->field->field),
9704 uentry_whereLast (ue));
9712 ("Special clause accesses field of non-struct or union result (%t): %q.%s",
9713 ct, sRef_unparse (rec), s->info->field->field),
9714 uentry_whereLast (ue));
9718 ret = sRef_makeField (tmp, s->info->field->field);
9724 tmp = sRef_fixResultType (s->info->ref, typ, ue);
9726 ct = ctype_realType (sRef_getType (tmp));
9728 if (ctype_isKnown (ct))
9730 if (ctype_isAP (ct))
9739 ("Special clause dereferences non-pointer (%t): *%q",
9740 ct, sRef_unparse (s->info->ref)),
9741 uentry_whereLast (ue));
9745 ret = sRef_makePointer (tmp);
9752 ("Special clause uses & operator (not allowed): &%q", sRef_unparse (s->info->ref)),
9753 uentry_whereLast (ue));
9762 bool sRef_isOnly (sRef s)
9764 return (sRef_isValid(s) && alkind_isOnly (s->aliaskind));
9767 bool sRef_isDependent (sRef s)
9769 return (sRef_isValid(s) && alkind_isDependent (s->aliaskind));
9772 bool sRef_isOwned (sRef s)
9774 return (sRef_isReasonable (s) && (s->aliaskind == AK_OWNED));
9777 bool sRef_isKeep (sRef s)
9779 return (sRef_isReasonable (s) && (s->aliaskind == AK_KEEP));
9782 bool sRef_isTemp (sRef s)
9784 return (sRef_isReasonable (s) && alkind_isTemp (s->aliaskind));
9787 bool sRef_isLocalState (sRef s)
9789 return (sRef_isReasonable (s) && (s->aliaskind == AK_LOCAL));
9792 bool sRef_isUnique (sRef s)
9794 return (sRef_isReasonable (s) && (s->aliaskind == AK_UNIQUE));
9797 bool sRef_isShared (sRef s)
9799 return (sRef_isReasonable (s) && (s->aliaskind == AK_SHARED));
9802 bool sRef_isExposed (sRef s)
9804 return (sRef_isReasonable (s) && (s->expkind == XO_EXPOSED));
9807 bool sRef_isObserver (sRef s)
9809 return (sRef_isReasonable (s) && (s->expkind == XO_OBSERVER));
9812 bool sRef_isFresh (sRef s)
9814 return (sRef_isReasonable (s) && (s->aliaskind == AK_FRESH));
9817 bool sRef_isDefinitelyNull (sRef s)
9819 return (sRef_isReasonable (s) && (sRef_getNullState (s) == NS_DEFNULL
9820 || sRef_getNullState (s) == NS_CONSTNULL));
9823 bool sRef_isAllocated (sRef s)
9825 return (sRef_isReasonable (s) && (s->defstate == SS_ALLOCATED));
9828 bool sRef_isStack (sRef s)
9830 return (sRef_isReasonable (s) && (s->aliaskind == AK_STACK));
9833 bool sRef_isNotNull (sRef s)
9835 return (sRef_isValid(s) && (sRef_getNullState (s) == NS_MNOTNULL
9836 || sRef_getNullState (s) == NS_NOTNULL));
9839 alkind sRef_getAliasKind (sRef s)
9841 if (sRef_isValid(s)) {
9842 llassert (alkind_isValid (s->aliaskind));
9843 return s->aliaskind;
9849 nstate sRef_getNullState (sRef s)
9851 if (sRef_isReasonable (s)) {
9852 llassert (nstate_isValid (s->nullstate));
9853 return s->nullstate;
9859 void sRef_reflectAnnotation (sRef s, annotationInfo a, fileloc loc)
9861 if (sRef_isReasonable (s))
9863 if (!valueTable_isDefined (s->state))
9865 s->state = valueTable_create (1);
9868 cstring_copy (metaStateInfo_getName (annotationInfo_getState (a))),
9869 stateValue_create (annotationInfo_getValue (a),
9870 stateInfo_makeLoc (loc, SA_DECLARED)));
9874 DPRINTF (("reflect loc: %s", fileloc_unparse (loc)));
9877 metaStateInfo_getName (annotationInfo_getState (a)),
9878 stateValue_create (annotationInfo_getValue (a),
9879 stateInfo_makeLoc (loc, SA_DECLARED)));
9881 DPRINTF (("sref: %s", sRef_unparse (s)));
9882 DPRINTF (("sref: %s", sRef_unparseFull (s)));
9887 void sRef_setMetaStateValueComplete (sRef s, cstring key, int value, fileloc loc)
9889 sRefSet aliases = usymtab_allAliases (s);
9891 sRef_setMetaStateValue (s, key, value, loc);
9893 sRefSet_realElements (aliases, current)
9895 if (sRef_isReasonable (current))
9897 current = sRef_updateSref (current);
9898 sRef_setMetaStateValue (current, key, value, loc);
9900 } end_sRefSet_realElements ;
9902 sRefSet_free (aliases);
9905 void sRef_setMetaStateValue (sRef s, cstring key, int value, fileloc loc)
9907 sRef_checkMutable (s);
9909 if (sRef_isReasonable (s))
9911 if (!valueTable_isDefined (s->state))
9913 DPRINTF (("inserting state: %s: %s %d", sRef_unparse (s), key, value));
9914 s->state = valueTable_create (1);
9915 valueTable_insert (s->state, cstring_copy (key),
9916 stateValue_create (value,
9917 stateInfo_makeLoc (loc, SA_CHANGED)));
9921 DPRINTF (("Updating state: %s: %s %d / %s", sRef_unparse (s), key, value,
9922 fileloc_unparse (loc)));
9923 if (valueTable_contains (s->state, key))
9926 (s->state, key, stateValue_create (value,
9927 stateInfo_makeLoc (loc, SA_CHANGED)));
9932 (s->state, cstring_copy (key),
9933 stateValue_create (value, stateInfo_makeLoc (loc, SA_CHANGED)));
9936 DPRINTF (("After: %s", sRef_unparseFull (s)));
9941 bool sRef_checkMetaStateValue (sRef s, cstring key, int value)
9943 if (sRef_isReasonable (s))
9945 if (valueTable_isDefined (s->state))
9949 DPRINTF (("check state: %s: %s %d", sRef_unparse (s), key, value));
9951 val = valueTable_lookup (s->state, key);
9952 llassert (stateValue_isDefined (val));
9953 return (stateValue_isError (val)
9954 || stateValue_getValue (val) == value);
9967 /*@observer@*/ stateValue sRef_getMetaStateValue (sRef s, cstring key)
9969 if (sRef_isReasonable (s))
9971 if (valueTable_isDefined (s->state))
9975 val = valueTable_lookup (s->state, key);
9976 /* Okay if its not defined, just returns stateValue_undefined */
9981 return stateValue_undefined;
9986 return stateValue_undefined;
9990 /*@observer@*/ valueTable sRef_getValueTable (sRef s)
9992 DPRINTF (("Get value table: %s", sRef_unparse (s)));
9994 if (sRef_isReasonable (s))
9996 llassert (sRef_isReasonable (s));
9997 DPRINTF (("Value table: %s", valueTable_unparse (s->state)));
10002 DPRINTF (("No value table!"));
10003 return valueTable_undefined;
10007 bool sRef_makeStateSpecial (sRef s)
10010 ** Default defined state can be made special.
10013 llassert (sRef_isReasonable (s));
10015 if (s->defstate == SS_UNKNOWN || s->defstate == SS_DEFINED || s->defstate == SS_SPECIAL)
10017 /* s->aliaskind = AK_IMPTEMP; */ /* evans 2001-07-23 shouldn't effect alias state */
10018 s->defstate = SS_SPECIAL;
10019 DPRINTF (("Made special: %s", sRef_unparseFull (s)));
10024 /* s->aliaskind = AK_IMPTEMP; */
10025 s->defstate = SS_SPECIAL;
10030 void sRef_markImmutable (sRef s)
10032 if (sRef_isReasonable (s))
10034 DPRINTF (("Mark immutable: %s", sRef_unparseFull (s)));
10039 bool sRef_definitelyNullContext (sRef s)
10041 return (sRef_definitelyNull (s)
10042 || usymtab_isDefinitelyNullDeep (s));
10045 bool sRef_definitelyNullAltContext (sRef s)
10047 return (sRef_definitelyNull (s)
10048 || usymtab_isAltDefinitelyNullDeep (s));
10052 /* start modifications */
10053 struct s_bbufinfo sRef_getNullTerminatedState (sRef p_s) {
10054 struct s_bbufinfo BUFSTATE_UNKNOWN;
10055 BUFSTATE_UNKNOWN.bufstate = BB_NOTNULLTERMINATED;
10056 BUFSTATE_UNKNOWN.size = 0;
10057 BUFSTATE_UNKNOWN.len = 0;
10059 if (sRef_isValid(p_s))
10060 return p_s->bufinfo;
10061 return BUFSTATE_UNKNOWN;
10064 void sRef_setNullTerminatedState(sRef p_s) {
10065 if(sRef_isReasonable (p_s)) {
10066 p_s->bufinfo.bufstate = BB_NULLTERMINATED;
10068 llfatalbug( message("sRef_setNT passed a invalid sRef\n"));
10073 void sRef_setPossiblyNullTerminatedState(sRef p_s) {
10074 if( sRef_isReasonable (p_s)) {
10075 p_s->bufinfo.bufstate = BB_POSSIBLYNULLTERMINATED;
10077 llfatalbug( message("sRef_setPossNT passed a invalid sRef\n"));
10081 void sRef_setNotNullTerminatedState(sRef p_s) {
10082 if( sRef_isReasonable (p_s)) {
10083 p_s->bufinfo.bufstate = BB_NOTNULLTERMINATED;
10085 llfatalbug( message("sRef_unsetNT passed a invalid sRef\n"));
10089 void sRef_setLen(sRef p_s, int len) {
10090 if (sRef_isReasonable (p_s) && sRef_isNullTerminated(p_s))
10092 p_s->bufinfo.len = len;
10096 llfatalbug( message("sRef_setLen passed an invalid sRef\n"));
10101 void sRef_setSize(sRef p_s, int size) {
10102 if( sRef_isValid(p_s))
10104 p_s->bufinfo.size = size;
10108 llfatalbug( message("sRef_setSize passed a invalid sRef\n"));
10112 void sRef_resetLen(sRef p_s) {
10113 if (sRef_isReasonable (p_s))
10115 p_s->bufinfo.len = 0;
10119 llfatalbug (message ("sRef_setLen passed an invalid sRef\n"));
10123 /*drl7x 11/28/2000 */
10125 bool sRef_isFixedArray (sRef p_s) /*@*/ {
10127 c = sRef_getType (p_s);
10128 return ( ctype_isFixedArray (c) );
10131 size_t sRef_getArraySize (sRef p_s) /*@*/
10134 llassert (sRef_isFixedArray(p_s) );
10135 DPRINTF (( message ("sRef_getArraySize getting array size for %s", sRef_unparse(p_s) ) ));
10137 c = sRef_getType (p_s);
10138 return (ctype_getArraySize (c));
10141 void sRef_setValue (sRef s, multiVal val)
10143 if (!sRef_isReasonable (s))
10145 llcontbuglit ("Unreasonable sRef");
10146 llcontbug (message ("sRef: %s", sRef_unparse (s)));
10150 multiVal_free (s->val);
10154 bool sRef_hasValue (sRef s)
10156 return (sRef_isReasonable (s)
10157 && multiVal_isDefined (s->val));
10160 multiVal sRef_getValue (sRef s)
10162 if (sRef_isReasonable (s))
10167 return multiVal_undefined;