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 <= 50); /*@i32 bogus...*/
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; /*@i24 unknown@*/
442 s->bufinfo.len = -1; /*@i24 unknown@*/
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, 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, 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);
2298 llassert(exprNodeList_size (args) > s->info->paramno);
2300 exprNode e = exprNodeList_nth (args, s->info->paramno);
2302 llassert( !(exprNode_isError (e)) );
2303 ce = constraintExpr_makeExprNode (e);
2310 llcontbug (message ("Trying to do fixConstraintParam on nonparam, nonglobal: %q for function with arguments %q",
2311 sRef_unparse (s), exprNodeList_unparse(args)));
2312 temp = sRef_saveCopy(s);
2313 ce = constraintExpr_makeTermsRef (temp);
2322 sRef_fixBaseParam (/*@returned@*/ sRef s, exprNodeList args)
2324 if (sRef_isInvalid (s)) return (sRef_undefined);
2328 case SK_UNCONSTRAINED:
2333 if (exprNodeList_size (args) > s->info->paramno)
2335 exprNode e = exprNodeList_nth (args, s->info->paramno);
2337 if (exprNode_isError (e))
2339 return sRef_makeUnknown ();
2342 return (exprNode_getSref (e));
2346 return sRef_makeUnknown ();
2351 if (s->info->arrayfetch->indknown)
2353 return (sRef_makeArrayFetchKnown
2354 (sRef_fixBaseParam (s->info->arrayfetch->arr, args),
2355 s->info->arrayfetch->ind));
2359 return (sRef_makeArrayFetch
2360 (sRef_fixBaseParam (s->info->arrayfetch->arr, args)));
2365 DPRINTF (("Fix field: %s", sRef_unparseFull (s)));
2366 res = sRef_makeField (sRef_fixBaseParam (s->info->field->rec, args),
2367 s->info->field->field);
2368 DPRINTF (("Returns: %s", sRef_unparseFull (res)));
2372 return (sRef_makePointer (sRef_fixBaseParam (s->info->ref, args)));
2375 return (sRef_makeAddress (sRef_fixBaseParam (s->info->ref, args)));
2378 return (sRef_makeConj (sRef_fixBaseParam (s->info->conj->a, args),
2379 sRef_fixBaseParam (s->info->conj->b, args)));
2395 sRef_undumpGlobal (char **c)
2405 usymId uid = usymId_fromInt (reader_getInt (c));
2410 reader_checkChar (c, '@');
2411 defstate = sstate_fromInt (reader_getInt (c));
2413 reader_checkChar (c, '@');
2414 nullstate = nstate_fromInt (reader_getInt (c));
2416 ret = sRef_makeGlobal (uid, ctype_unknown, stateInfo_currentLoc ());
2417 sRef_setNullStateN (ret, nullstate);
2418 ret->defstate = defstate;
2423 int i = reader_getInt (c);
2424 speckind sk = speckind_fromInt (i);
2428 case SR_NOTHING: return (sRef_makeNothing ());
2429 case SR_INTERNAL: return (sRef_makeInternalState ());
2430 case SR_SPECSTATE: return (sRef_makeSpecState ());
2431 case SR_SYSTEM: return (sRef_makeSystemState ());
2432 case SR_GLOBALMARKER: BADBRANCH;
2437 return sRef_undefined;
2439 return sRef_makeUnknown ();
2441 return sRef_makeUnknown ();
2443 llfatalerror (message ("sRef_undumpGlobal: bad line: %s",
2444 cstring_fromChars (*c)));
2449 static /*@exposed@*/ sRef sRef_undumpBody (char **c)
2458 return (sRef_makeGlobal (usymId_fromInt (reader_getInt (c)),
2459 ctype_unknown, stateInfo_currentLoc ()));
2461 return (sRef_makeParam (reader_getInt (c), ctype_unknown,
2462 stateInfo_makeLoc (g_currentloc, SA_DECLARED)));
2464 return (sRef_makeResult (ctype_undump (c)));
2467 if ((**c >= '0' && **c <= '9') || **c == '-')
2469 int i = reader_getInt (c);
2470 sRef arr = sRef_undump (c);
2471 sRef ret = sRef_buildArrayFetchKnown (arr, i);
2477 sRef arr = sRef_undump (c);
2478 sRef ret = sRef_buildArrayFetch (arr);
2485 cstring fname = cstring_undefined;
2490 fname = cstring_appendChar (fname, **c);
2495 ret = sRef_buildField (sRef_undump (c), fname);
2496 cstring_markOwned (fname);
2501 int i = reader_getInt (c);
2502 speckind sk = speckind_fromInt (i);
2506 case SR_NOTHING: return (sRef_makeNothing ());
2507 case SR_INTERNAL: return (sRef_makeInternalState ());
2508 case SR_SPECSTATE: return (sRef_makeSpecState ());
2509 case SR_SYSTEM: return (sRef_makeSystemState ());
2510 case SR_GLOBALMARKER: BADBRANCH;
2516 sRef ptr = sRef_undump (c);
2517 sRef ret = sRef_makePointer (ptr);
2523 sRef adr = sRef_undump (c);
2524 sRef ret = sRef_makeAddress (adr);
2530 return (sRef_makeObject (ctype_undump (c)));
2534 sRef s1 = sRef_undump (c);
2535 sRef s2 = ((*c)++, sRef_undump (c));
2536 sRef ret = sRef_makeConj (s1, s2);
2541 return sRef_undefined;
2543 return sRef_makeUnknown ();
2545 return sRef_makeUnknown ();
2547 llfatalerror (message ("sRef_undump: bad line: %s", cstring_fromChars (*c)));
2552 /*@exposed@*/ sRef sRef_undump (char **c)
2554 sRef res = sRef_undumpBody (c);
2556 if (reader_optCheckChar (c, '='))
2558 multiVal mv = multiVal_undump (c);
2559 sRef_setValue (res, mv);
2560 reader_checkChar (c, '=');
2566 static /*@only@*/ cstring sRef_dumpBody (sRef s)
2568 if (sRef_isInvalid (s))
2570 return (cstring_makeLiteral ("-"));
2577 return (message ("p%d", s->info->paramno));
2579 if (s->info->arrayfetch->indknown)
2581 return (message ("a%d%q", s->info->arrayfetch->ind,
2582 sRef_dump (s->info->arrayfetch->arr)));
2586 return (message ("a%q", sRef_dump (s->info->arrayfetch->arr)));
2589 return (message ("f%s.%q", s->info->field->field,
2590 sRef_dump (s->info->field->rec)));
2592 return (message ("t%q", sRef_dump (s->info->ref)));
2594 return (message ("d%q", sRef_dump (s->info->ref)));
2596 return (message ("o%q", ctype_dump (s->info->object)));
2598 return (message ("s%d", (int) s->info->spec));
2600 return (message ("c%q.%q",
2601 sRef_dump (s->info->conj->a),
2602 sRef_dump (s->info->conj->b)));
2604 if (sRef_isFileOrGlobalScope (s))
2606 return (message ("g%d",
2607 usymtab_convertId (s->info->cvar->index)));
2611 llcontbug (message ("Dumping local variable: %q",
2612 sRef_unparseDebug (s)));
2613 return (cstring_makeLiteral ("u"));
2616 return (cstring_makeLiteral ("u"));
2618 return (message ("r%q", ctype_dump (s->type)));
2624 case SK_UNCONSTRAINED:
2625 llcontbug (message ("sRef_dump: bad kind: %q",
2626 sRef_unparseFull (s)));
2627 return (cstring_makeLiteral ("x"));
2634 /*@only@*/ cstring sRef_dump (sRef s)
2636 cstring res = sRef_dumpBody (s);
2638 if (sRef_hasValue (s))
2640 res = message ("%q=%q=", res, multiVal_dump (sRef_getValue (s)));
2646 cstring sRef_dumpGlobal (sRef s)
2648 llassert (!sRef_hasValue (s));
2650 if (sRef_isInvalid (s))
2652 return (cstring_makeLiteral ("-"));
2659 if (sRef_isFileOrGlobalScope (s))
2661 return (message ("g%d@%d@%d",
2662 usymtab_convertId (s->info->cvar->index),
2664 (int) sRef_getNullState (s)));
2668 llcontbug (message ("Dumping local variable: %q",
2669 sRef_unparseDebug (s)));
2670 return (cstring_makeLiteral ("u"));
2673 return (cstring_makeLiteral ("u"));
2675 return (message ("s%d", (int) s->info->spec));
2677 llcontbug (message ("sRef_dumpGlobal: bad kind: %q",
2678 sRef_unparseFull (s)));
2679 return (cstring_makeLiteral ("x"));
2687 sRef_deriveType (sRef s, uentryList cl)
2689 if (sRef_isInvalid (s)) return ctype_unknown;
2694 return (uentry_getType (usymtab_getRefQuiet (s->info->cvar->lexlevel,
2695 s->info->cvar->index)));
2696 case SK_UNCONSTRAINED:
2697 return (ctype_unknown);
2699 if (s->info->paramno >= 0)
2701 return uentry_getType (uentryList_getN (cl, s->info->paramno));
2705 return ctype_unknown;
2709 ctype ca = sRef_deriveType (s->info->arrayfetch->arr, cl);
2711 if (ctype_isArray (ca))
2713 return (ctype_baseArrayPtr (ca));
2715 else if (ctype_isUnknown (ca))
2721 llcontbuglit ("sRef_deriveType: inconsistent array type");
2727 ctype ct = sRef_deriveType (s->info->field->rec, cl);
2729 if (ctype_isStructorUnion (ct))
2731 uentry ue = uentryList_lookupField (ctype_getFields (ct),
2732 s->info->field->field);
2734 if (uentry_isValid (ue))
2736 return (uentry_getType (ue));
2740 llcontbuglit ("sRef_deriveType: bad field");
2741 return ctype_unknown;
2744 else if (ctype_isUnknown (ct))
2750 llcontbuglit ("sRef_deriveType: inconsistent field type");
2756 ctype ct = sRef_deriveType (s->info->ref, cl);
2758 if (ctype_isUnknown (ct)) return ct;
2759 if (ctype_isPointer (ct)) return (ctype_baseArrayPtr (ct));
2762 llcontbuglit ("sRef_deriveType: inconsistent pointer type");
2768 ctype ct = sRef_deriveType (s->info->ref, cl);
2770 if (ctype_isUnknown (ct)) return ct;
2771 return ctype_makePointer (ct);
2775 return sRef_deriveType (s->info->ref, cl);
2779 return (s->info->object);
2783 return (ctype_makeConj (sRef_deriveType (s->info->conj->a, cl),
2784 sRef_deriveType (s->info->conj->b, cl)));
2796 return ctype_unknown;
2802 sRef_getType (sRef s)
2804 if (sRef_isInvalid (s)) return ctype_unknown;
2810 sRef_unparseOpt (sRef s)
2812 sRef rb = sRef_getRootBase (s);
2814 if (sRef_isMeaningful (rb) && !sRef_isConst (rb))
2816 cstring ret = sRef_unparse (s);
2818 llassertprint (!cstring_equalLit (ret, "?"), ("print: %s", sRef_unparseDebug (s)));
2820 if (!cstring_isEmpty (ret))
2822 return (cstring_appendChar (ret, ' '));
2830 return cstring_undefined;
2834 sRef_unparsePreOpt (sRef s)
2836 sRef rb = sRef_getRootBase (s);
2838 if (sRef_isMeaningful (rb) && !sRef_isConst (rb))
2840 cstring ret = sRef_unparse (s);
2842 llassertprint (!cstring_equalLit (ret, "?"), ("print: %s", sRef_unparseDebug (s)));
2843 return (cstring_prependCharO (' ', ret));
2846 return cstring_undefined;
2850 sRef_unparse (sRef s)
2852 if (sRef_isInvalid (s)) return (cstring_makeLiteral ("?"));
2854 if (context_inFunctionLike ())
2856 return (sRef_unparseWithArgs (s, context_getParams ()));
2860 DPRINTF (("Not in function like: %s", context_unparse ()));
2861 return (sRef_unparseNoArgs (s));
2865 static /*@only@*/ cstring
2866 sRef_unparseWithArgs (sRef s, uentryList args)
2868 if (sRef_isInvalid (s))
2870 return (cstring_makeLiteral ("?"));
2876 return (uentry_getName (usymtab_getRefQuiet (s->info->cvar->lexlevel,
2877 s->info->cvar->index)));
2878 case SK_UNCONSTRAINED:
2879 return (cstring_copy (s->info->fname));
2882 if (s->info->paramno < uentryList_size (args)
2883 && s->info->paramno >= 0)
2885 uentry ue = uentryList_getN (args, s->info->paramno);
2887 if (uentry_isValid (ue))
2888 return uentry_getName (ue);
2891 return (message ("parameter %d", s->info->paramno + 1));
2894 if (s->info->arrayfetch->indknown)
2896 return (message ("%q[%d]", sRef_unparseWithArgs (s->info->arrayfetch->arr, args),
2897 s->info->arrayfetch->ind));
2901 return (message ("%q[]", sRef_unparseWithArgs (s->info->arrayfetch->arr, args)));
2904 if (s->info->field->rec->kind == SK_PTR)
2906 sRef ptr = s->info->field->rec;
2908 return (message ("%q->%s", sRef_unparseWithArgs (ptr->info->ref, args),
2909 s->info->field->field));
2911 return (message ("%q.%s", sRef_unparseWithArgs (s->info->field->rec, args),
2912 s->info->field->field));
2916 sRef ref = sRef_fixConj (s->info->ref);
2917 skind sk = ref->kind;
2922 ret = message ("storage pointed to by %q",
2923 sRef_unparseWithArgs (ref, args));
2925 else if (skind_isSimple (sk) || sk == SK_PTR)
2927 ret = message ("*%q", sRef_unparseWithArgs (ref, args));
2931 ret = message ("*(%q)", sRef_unparseWithArgs (ref, args));
2937 return (message ("&%q", sRef_unparseWithArgs (s->info->ref, args)));
2939 return (cstring_copy (ctype_unparse (s->info->object)));
2941 return (sRef_unparseWithArgs (sRef_getConjA (s), args));
2943 if (cstring_isDefined (s->info->fname))
2945 return (message ("[result of %s]", s->info->fname));
2949 return (cstring_makeLiteral ("<new>"));
2952 return (cstring_makeLiteral ("?"));
2954 return (message ("<derived %q>", sRef_unparse (s->info->ref)));
2956 return (message ("<external %q>", sRef_unparse (s->info->ref)));
2958 return (message ("<type %s>", ctype_unparse (s->type)));
2960 return (message ("<const %s>", ctype_unparse (s->type)));
2962 switch (s->info->spec)
2964 case SR_NOTHING: return cstring_makeLiteral ("nothing");
2965 case SR_INTERNAL: return cstring_makeLiteral ("internal state");
2966 case SR_SPECSTATE: return cstring_makeLiteral ("spec state");
2967 case SR_SYSTEM: return cstring_makeLiteral ("file system state");
2968 case SR_GLOBALMARKER: return cstring_makeLiteral ("<global marker>");
2972 return cstring_makeLiteral ("result");
2975 llbug (message ("Bad sref, kind = %d", (int) s->kind));
2983 sRef_unparseDebug (sRef s)
2985 if (sRef_isInvalid (s))
2987 return (cstring_makeLiteral ("<undef>"));
2993 case SK_UNCONSTRAINED:
2994 return (message ("<unconstrained %s>", s->info->fname));
2999 ce = usymtab_getRefQuiet (s->info->cvar->lexlevel, s->info->cvar->index);
3001 if (uentry_isInvalid (ce))
3003 return (message ("<scope: %d.%d *invalid*>",
3004 s->info->cvar->lexlevel,
3005 s->info->cvar->index));
3009 return (message ("<scope: %d.%d *%q*>",
3010 s->info->cvar->lexlevel,
3011 s->info->cvar->index,
3012 uentry_getName (ce)));
3018 return (message ("<parameter %d>", s->info->paramno + 1));
3021 if (s->info->arrayfetch->indknown)
3023 return (message ("%q[%d]", sRef_unparseDebug (s->info->arrayfetch->arr),
3024 s->info->arrayfetch->ind));
3028 return (message ("%q[]", sRef_unparseDebug (s->info->arrayfetch->arr)));
3031 return (message ("%q.%s", sRef_unparseDebug (s->info->field->rec),
3032 s->info->field->field));
3034 if (sRef_isField (s->info->ref))
3036 sRef fld = s->info->ref;
3038 return (message ("%q->%s", sRef_unparseDebug (fld->info->field->rec),
3039 fld->info->field->field));
3043 return (message ("*(%q)", sRef_unparseDebug (s->info->ref)));
3046 return (message ("&%q", sRef_unparseDebug (s->info->ref)));
3048 return (message ("<object type %s>", ctype_unparse (s->info->object)));
3050 return (message ("%q | %q", sRef_unparseDebug (s->info->conj->a),
3051 sRef_unparseDebug (s->info->conj->b)));
3053 return message ("<new: %s>", s->info->fname);
3055 return (message ("<derived %q>", sRef_unparseDebug (s->info->ref)));
3057 return (message ("<external %q>", sRef_unparseDebug (s->info->ref)));
3059 return (message ("<type %s>", ctype_unparse (s->type)));
3061 if (sRef_hasValue (s))
3063 return (message ("<const %s=%q>", ctype_unparse (s->type), multiVal_unparse (sRef_getValue (s))));
3067 return (message ("<const %s>", ctype_unparse (s->type)));
3070 return (message ("<result %s>", ctype_unparse (s->type)));
3072 return (message ("<spec %s>",
3073 cstring_makeLiteralTemp
3074 (s->info->spec == SR_NOTHING ? "nothing"
3075 : s->info->spec == SR_INTERNAL ? "internalState"
3076 : s->info->spec == SR_SPECSTATE ? "spec state"
3077 : s->info->spec == SR_SYSTEM ? "fileSystem"
3080 return cstring_makeLiteral ("<unknown>");
3086 static /*@only@*/ cstring
3087 sRef_unparseNoArgs (sRef s)
3089 if (sRef_isInvalid (s)) return (cstring_makeLiteral ("?"));
3093 case SK_UNCONSTRAINED:
3094 return (cstring_copy (s->info->fname));
3097 uentry ce = usymtab_getRefQuiet (s->info->cvar->lexlevel,
3098 s->info->cvar->index);
3100 if (uentry_isInvalid (ce))
3102 llcontbug (message ("sRef_unparseNoArgs: bad cvar: %q",
3103 sRef_unparseDebug (s)));
3104 return (sRef_unparseDebug (s));
3108 return (uentry_getName (ce));
3112 if (s->info->arrayfetch->indknown)
3114 return (message ("%q[%d]", sRef_unparseNoArgs (s->info->arrayfetch->arr),
3115 s->info->arrayfetch->ind));
3119 return (message ("%q[]", sRef_unparseNoArgs (s->info->arrayfetch->arr)));
3122 return (message ("%q.%s", sRef_unparseNoArgs (s->info->field->rec),
3123 s->info->field->field));
3126 sRef ref = sRef_fixConj (s->info->ref);
3127 skind sk = ref->kind;
3130 if (skind_isSimple (sk) || sk == SK_PTR)
3132 ret = message ("*%q", sRef_unparseNoArgs (ref));
3136 ret = message ("*(%q)", sRef_unparseNoArgs (ref));
3142 return (message ("&%q", sRef_unparseNoArgs (s->info->ref)));
3144 return (cstring_copy (ctype_unparse (s->info->object)));
3146 return (sRef_unparseNoArgs (s->info->conj->a));
3148 return (message ("result of %s", s->info->fname));
3150 return (message ("<der %q>", sRef_unparseNoArgs (s->info->ref)));
3152 return message ("<ext %q>", sRef_unparseNoArgs (s->info->ref));
3154 return (cstring_makeLiteral
3155 (s->info->spec == SR_NOTHING ? "nothing"
3156 : s->info->spec == SR_INTERNAL ? "internal state"
3157 : s->info->spec == SR_SPECSTATE ? "spec state"
3158 : s->info->spec == SR_SYSTEM ? "file system state"
3161 return cstring_makeLiteral ("result");
3165 return cstring_makeLiteral ("?");
3167 /* llcontbug (message ("sRef_unparseNoArgs: bad case: %q", sRef_unparseDebug (s))); */
3168 return (sRef_unparseDebug (s));
3173 /*@dependent@*/ sRef sRef_makeUnconstrained (cstring fname)
3175 sRef s = sRef_new ();
3177 s->kind = SK_UNCONSTRAINED;
3178 s->info = (sinfo) dmalloc (sizeof (*s->info));
3179 s->info->fname = cstring_copy (fname); /* evans 2002-07-12: this was exposed, causing memory errors */
3184 cstring sRef_unconstrainedName (sRef s)
3186 llassert (sRef_isUnconstrained (s));
3188 return s->info->fname;
3191 bool sRef_isUnconstrained (sRef s)
3193 return (sRef_isReasonable(s) && s->kind == SK_UNCONSTRAINED);
3196 static /*@dependent@*/ /*@notnull@*/ sRef
3197 sRef_makeCvarAux (int level, usymId index, ctype ct, /*@only@*/ stateInfo stinfo)
3199 sRef s = sRef_newRef ();
3202 s->info = (sinfo) dmalloc (sizeof (*s->info));
3204 s->info->cvar = (cref) dmalloc (sizeof (*s->info->cvar));
3205 s->info->cvar->lexlevel = level;
3206 s->info->cvar->index = index;
3208 /* for now, all globals are defined; all locals, aren't */
3210 if (level <= fileScope)
3212 s->defstate = SS_UNKNOWN;
3216 ctype rct = ctype_realType (ct);
3218 if (level != paramsScope
3219 && (ctype_isStructorUnion (rct) || ctype_isRealArray (rct)))
3221 s->defstate = SS_ALLOCATED;
3222 s->oaliaskind = s->aliaskind = AK_STACK;
3226 s->defstate = SS_UNDEFINED;
3227 s->oaliaskind = s->aliaskind = AK_LOCAL;
3233 llassert (level >= globScope);
3234 llassert (usymId_isValid (index));
3236 DPRINTF (("Made cvar: [%p] %s", s, sRef_unparseDebug (s)));
3237 llassert (valueTable_isUndefined (s->state));
3238 s->state = context_createValueTable (s, stinfo);
3242 /*@dependent@*/ sRef sRef_makeCvar (int level, usymId index, ctype ct, /*@only@*/ stateInfo stinfo)
3244 return (sRef_makeCvarAux (level, index, ct, stinfo));
3247 int sRef_lexLevel (sRef s)
3249 if (sRef_isReasonable (s))
3253 conj = sRef_fixConj (s);
3254 s = sRef_getRootBase (conj);
3256 if (sRef_isReasonable (s) && s->kind == SK_CVAR)
3258 return (s->info->cvar->lexlevel);
3266 sRef_makeGlobal (usymId l, ctype ct, /*@only@*/ stateInfo stinfo)
3268 return (sRef_makeCvar (globScope, l, ct, stinfo));
3272 sRef_setParamNo (sRef s, int l)
3274 llassert (sRef_isReasonable (s) && s->kind == SK_PARAM);
3275 s->info->paramno = l;
3279 /*@dependent@*/ sRef
3280 sRef_makeParam (int l, ctype ct, stateInfo stinfo)
3282 sRef s = sRef_new ();
3287 s->info = (sinfo) dmalloc (sizeof (*s->info));
3288 s->info->paramno = l;
3290 s->defstate = SS_UNKNOWN;
3291 /* (probably defined, unless its an out parameter) */
3293 llassert (valueTable_isUndefined (s->state));
3294 s->state = context_createValueTable (s, stinfo);
3299 sRef_isIndexKnown (sRef arr)
3303 llassert (sRef_isReasonable (arr));
3304 arr = sRef_fixConj (arr);
3306 llassert (arr->kind == SK_ARRAYFETCH);
3307 res = arr->info->arrayfetch->indknown;
3312 sRef_getIndex (sRef arr)
3316 llassert (sRef_isReasonable (arr));
3317 arr = sRef_fixConj (arr);
3319 llassert (arr->kind == SK_ARRAYFETCH);
3321 if (!arr->info->arrayfetch->indknown)
3323 llcontbug (message ("sRef_getIndex: unknown: %q", sRef_unparse (arr)));
3328 result = arr->info->arrayfetch->ind;
3334 static bool sRef_isZerothArrayFetch (/*@notnull@*/ sRef s)
3336 return (s->kind == SK_ARRAYFETCH
3337 && s->info->arrayfetch->indknown
3338 && (s->info->arrayfetch->ind == 0));
3341 /*@exposed@*/ sRef sRef_makeAddress (/*@exposed@*/ sRef t)
3344 if (sRef_isInvalid (t)) return sRef_undefined;
3346 if (sRef_isPointer (t))
3348 return (t->info->ref);
3350 else if (sRef_isZerothArrayFetch (t))
3352 return (t->info->arrayfetch->arr);
3356 sRef s = sRef_newRef ();
3359 s->type = ctype_makePointer (t->type);
3360 s->info = (sinfo) dmalloc (sizeof (*s->info));
3363 if (t->defstate == SS_UNDEFINED)
3364 /* no! it is allocated even still: && !ctype_isPointer (t->type)) */
3366 s->defstate = SS_ALLOCATED;
3370 s->defstate = t->defstate;
3373 if (t->aliaskind == AK_LOCAL)
3375 if (sRef_isLocalVar (t))
3377 s->aliaskind = AK_STACK;
3381 llassert (valueTable_isUndefined (s->state));
3382 s->state = context_createValueTable (s,
3383 stateInfo_makeLoc (g_currentloc, SA_CREATED));
3388 cstring sRef_getField (sRef s)
3392 llassert (sRef_isReasonable (s));
3393 s = sRef_fixConj (s);
3395 llassertprint (sRef_isReasonable (s) && (s->kind == SK_FIELD),
3396 ("s = %s", sRef_unparseDebug (s)));
3398 res = s->info->field->field;
3402 sRef sRef_getBase (sRef s)
3406 if (sRef_isInvalid (s)) return (sRef_undefined);
3408 s = sRef_fixConj (s);
3419 res = s->info->field->rec;
3423 res = s->info->arrayfetch->arr;
3427 res = sRef_undefined; /* shouldn't need it */
3434 ** same as getBase, except returns invalid
3435 ** (and doesn't use adr's)
3439 sRef_getBaseSafe (sRef s)
3443 if (sRef_isInvalid (s)) { return sRef_undefined; }
3445 s = sRef_fixConj (s);
3453 res = s->info->field->rec; break;
3455 res = s->info->arrayfetch->arr;
3458 res = sRef_undefined; break;
3464 /*@constant int MAXBASEDEPTH;@*/
3465 # define MAXBASEDEPTH 25
3467 static /*@exposed@*/ sRef
3468 sRef_getRootBaseAux (sRef s, int depth)
3470 if (sRef_isInvalid (s)) return sRef_undefined;
3472 if (depth > MAXBASEDEPTH)
3475 ("Warning: reference base limit exceeded for %q. "
3476 "This either means there is a variable with at least "
3477 "%d indirections from this reference, or "
3478 "there is a bug in Splint.",
3483 return sRef_undefined;
3490 return (sRef_getRootBaseAux (s->info->ref, depth + 1));
3492 return (sRef_getRootBaseAux (s->info->field->rec, depth + 1));
3494 return (sRef_getRootBaseAux (s->info->arrayfetch->arr, depth + 1));
3496 return (sRef_getRootBaseAux (sRef_fixConj (s), depth + 1));
3502 sRef sRef_getRootBase (sRef s)
3504 return (sRef_getRootBaseAux (s, 0));
3507 static bool sRef_isDeep (sRef s)
3509 if (sRef_isInvalid (s)) return FALSE;
3519 return (sRef_isDeep (sRef_fixConj (s)));
3525 static int sRef_depth (sRef s)
3527 if (sRef_isInvalid (s)) return 0;
3535 return 1 + sRef_depth (s->info->ref);
3537 return 1 + sRef_depth (s->info->field->rec);
3539 return 1 + sRef_depth (s->info->arrayfetch->arr);
3541 return (sRef_depth (sRef_fixConj (s)));
3548 sRef_makeObject (ctype o)
3550 sRef s = sRef_newRef (); /*@i423 same line is bad...@*/
3552 s->kind = SK_OBJECT;
3553 s->info = (sinfo) dmalloc (sizeof (*s->info));
3554 s->info->object = o;
3555 llassert (valueTable_isUndefined (s->state));
3556 s->state = context_createValueTable (s, stateInfo_makeLoc (g_currentloc, SA_CREATED));
3561 ** This is used to represent storage referenced by a parameter.
3564 sRef sRef_makeExternal (sRef t)
3566 sRef s = sRef_newRef ();
3568 llassert (sRef_isReasonable (t));
3570 s->kind = SK_EXTERNAL;
3571 s->info = (sinfo) dmalloc (sizeof (*s->info));
3573 s->info->ref = t; /* sRef_copy (t); */ /*@i32 was exposed@*/
3574 llassert (valueTable_isUndefined (s->state));
3575 s->state = context_createValueTable (s, stateInfo_makeLoc (g_currentloc, SA_DECLARED));
3579 /*@dependent@*/ sRef sRef_makeDerived (/*@exposed@*/ sRef t)
3581 if (sRef_isReasonable (t))
3583 sRef s = sRef_newRef ();
3585 s->kind = SK_DERIVED;
3586 s->info = (sinfo) dmalloc (sizeof (*s->info));
3587 s->info->ref = t; /* sRef_copy (t); */ /*@i32@*/
3590 llassert (valueTable_isUndefined (s->state));
3591 s->state = context_createValueTable (s, stateInfo_makeLoc (g_currentloc, SA_CREATED));
3596 return sRef_undefined;
3601 ** definitely NOT symmetric:
3603 ** res fills in unknown state information from other
3607 sRef_mergeStateQuiet (sRef res, sRef other)
3609 llassert (sRef_isReasonable (res));
3610 llassert (sRef_isReasonable (other));
3612 res->modified = res->modified || other->modified;
3613 res->safe = res->safe && other->safe;
3615 if (res->defstate == SS_UNKNOWN)
3617 res->defstate = other->defstate;
3618 res->definfo = stateInfo_update (res->definfo, other->definfo);
3621 if (res->aliaskind == AK_UNKNOWN ||
3622 (res->aliaskind == AK_LOCAL && alkind_isKnown (other->aliaskind)))
3624 res->aliaskind = other->aliaskind;
3625 res->oaliaskind = other->oaliaskind;
3626 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
3629 if (res->expkind == XO_UNKNOWN)
3631 res->expkind = other->expkind;
3632 res->oexpkind = other->oexpkind;
3633 res->expinfo = stateInfo_update (res->expinfo, other->expinfo);
3636 /* out takes precedence over implicitly defined */
3637 if (res->defstate == SS_DEFINED && other->defstate != SS_UNKNOWN)
3639 res->defstate = other->defstate;
3640 res->definfo = stateInfo_update (res->definfo, other->definfo);
3643 if (sRef_getNullState (other) == NS_ERROR || sRef_getNullState (res) == NS_ERROR)
3645 sRef_setNullState (res, NS_ERROR, fileloc_undefined);
3649 if (sRef_getNullState (other) != NS_UNKNOWN
3650 && (sRef_getNullState (res) == NS_UNKNOWN || sRef_getNullState (res) == NS_NOTNULL
3651 || sRef_getNullState (res) == NS_MNOTNULL))
3653 sRef_updateNullState (res, other);
3659 ** definitely NOT symmetric:
3661 ** res fills in known state information from other
3665 sRef_mergeStateQuietReverse (/*@dependent@*/ sRef res, /*@dependent@*/ sRef other)
3667 bool changed = FALSE;
3669 llassert (sRef_isReasonable (res));
3670 llassert (sRef_isReasonable (other));
3671 sRef_checkMutable (res);
3673 if (res->kind != other->kind)
3679 res->kind = other->kind;
3680 res->type = other->type;
3681 res->info = sinfo_fullCopy (other);
3685 if (!ctype_equal (res->type, other->type))
3688 res->type = other->type;
3691 sinfo_update (res, other);
3694 res->modified = res->modified || other->modified;
3695 res->safe = res->safe && other->safe;
3697 if (res->aliaskind != other->aliaskind
3698 && (res->aliaskind == AK_UNKNOWN
3699 || ((res->aliaskind == AK_LOCAL
3700 || (res->aliaskind == AK_REFCOUNTED
3701 && other->aliaskind != AK_LOCAL))
3702 && other->aliaskind != AK_UNKNOWN)))
3705 res->aliaskind = other->aliaskind;
3706 res->oaliaskind = other->oaliaskind;
3707 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
3710 if (other->expkind != XO_UNKNOWN && other->expkind != res->expkind)
3713 res->expkind = other->expkind;
3714 res->expinfo = stateInfo_update (res->expinfo, other->expinfo);
3717 if (other->oexpkind != XO_UNKNOWN)
3719 res->oexpkind = other->oexpkind;
3722 /* out takes precedence over implicitly defined */
3724 if (res->defstate != other->defstate)
3726 if (other->defstate != SS_UNKNOWN)
3728 res->defstate = other->defstate;
3729 res->definfo = stateInfo_update (res->definfo, other->definfo);
3733 if (sRef_getNullState (other) == NS_ERROR || sRef_getNullState (res) == NS_ERROR)
3735 if (sRef_getNullState (res) != NS_ERROR)
3737 sRef_setNullStateN (res, NS_ERROR);
3743 if (sRef_getNullState (other) != NS_UNKNOWN
3744 && sRef_getNullState (other) != sRef_getNullState (res))
3747 sRef_updateNullState (res, other);
3753 sRef_clearDerived (res);
3758 sRef_mergeState (sRef res, sRef other, clause cl, fileloc loc)
3760 if (sRef_isReasonable (res) && sRef_isReasonable (other))
3762 sRef_mergeStateAux (res, other, cl, FALSE, loc, TRUE);
3766 if (sRef_isInvalid (res))
3768 llbug (message ("sRef_mergeState: invalid res sRef: %q",
3769 sRef_unparseDebug (other)));
3773 llbug (message ("sRef_mergeState: invalid other sRef: %q",
3774 sRef_unparseDebug (res)));
3780 sRef_mergeOptState (sRef res, sRef other, clause cl, fileloc loc)
3782 if (sRef_isReasonable (res) && sRef_isReasonable (other))
3784 sRef_mergeStateAux (res, other, cl, TRUE, loc, TRUE);
3788 if (sRef_isInvalid (res))
3790 llbug (message ("sRef_mergeOptState: invalid res sRef: %q",
3791 sRef_unparseDebug (other)));
3795 llbug (message ("sRef_mergeOptState: invalid other sRef: %q",
3796 sRef_unparseDebug (res)));
3802 sRef_mergeStateAux (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other,
3803 clause cl, bool opt, fileloc loc,
3807 llassertfatal (sRef_isReasonable (res));
3808 llassertfatal (sRef_isReasonable (other));
3810 DPRINTF (("Merge aux: %s / %s",
3811 sRef_unparseFull (res),
3812 sRef_unparseFull (other)));
3814 sRef_checkMutable (res);
3815 sRef_checkMutable (other);
3817 res->modified = res->modified || other->modified;
3819 if (res->kind == other->kind
3820 || (other->kind == SK_UNKNOWN || res->kind == SK_UNKNOWN))
3822 sstate odef = other->defstate;
3823 sstate rdef = res->defstate;
3824 nstate onull = sRef_getNullState (other);
3827 ** yucky stuff to handle
3832 if (other->defstate == SS_DEAD
3833 && ((sRef_isOnly (res) && sRef_definitelyNull (res))
3834 || (res->defstate == SS_UNDEFINED
3835 || res->defstate == SS_UNUSEABLE)))
3837 if (res->defstate == SS_UNDEFINED
3838 || res->defstate == SS_UNUSEABLE)
3840 res->defstate = SS_UNUSEABLE;
3844 res->defstate = SS_DEAD;
3847 res->definfo = stateInfo_update (res->definfo, other->definfo);
3848 sRef_clearDerived (other);
3849 sRef_clearDerived (res);
3851 else if (res->defstate == SS_DEAD
3852 && ((sRef_isOnly (other) && sRef_definitelyNull (other))
3853 || (other->defstate == SS_UNDEFINED
3854 || other->defstate == SS_UNUSEABLE)))
3856 if (other->defstate == SS_UNDEFINED
3857 || other->defstate == SS_UNUSEABLE)
3859 res->defstate = SS_UNUSEABLE;
3863 res->defstate = SS_DEAD;
3866 res->definfo = stateInfo_update (res->definfo, other->definfo);
3867 sRef_clearDerived (other);
3868 sRef_clearDerived (res);
3870 else if (res->defstate == SS_DEFINED
3871 && (other->defstate == SS_ALLOCATED
3872 && sRef_definitelyNull (other)))
3874 other->defstate = SS_DEFINED; /* definitely null! */
3876 else if (other->defstate == SS_DEFINED
3877 && (res->defstate == SS_ALLOCATED && sRef_definitelyNull (res)))
3879 res->defstate = SS_DEFINED;
3880 res->definfo = stateInfo_update (res->definfo, other->definfo);
3887 if (res->defstate == SS_DEAD && other->defstate == SS_DEAD)
3889 sRef_clearDerived (other);
3890 sRef_clearDerived (res);
3894 ** only & dead isn't really an only!
3897 if (alkind_isOnly (other->aliaskind) && other->defstate == SS_DEAD)
3899 other->aliaskind = AK_UNKNOWN;
3902 if (alkind_isOnly (res->aliaskind) && res->defstate == SS_DEAD)
3904 res->aliaskind = AK_UNKNOWN;
3908 ** Dead and dependent -> dead
3911 if (alkind_isDependent (other->aliaskind) && res->defstate == SS_DEAD)
3913 other->aliaskind = AK_UNKNOWN;
3914 other->defstate = SS_DEAD;
3915 sRef_clearDerived (res);
3916 sRef_clearDerived (other);
3919 if (alkind_isDependent (res->aliaskind) && other->defstate == SS_DEAD)
3921 res->aliaskind = AK_UNKNOWN;
3922 res->defstate = SS_DEAD;
3923 sRef_clearDerived (res);
3924 sRef_clearDerived (other);
3928 ** must do alias combine first, since it depends on
3929 ** original values of state and null.
3932 sRef_combineAliasKinds (res, other, cl, loc);
3933 sRef_combineDefState (res, other);
3934 sRef_combineNullState (res, other);
3936 if (rdef == SS_ALLOCATED || rdef == SS_SPECIAL)
3938 if (odef == SS_DEFINED)
3940 if (onull == NS_DEFNULL || onull == NS_CONSTNULL)
3942 res->deriv = sRefSet_copyInto (res->deriv, other->deriv);
3943 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
3946 else if (odef == SS_ALLOCATED || odef == SS_SPECIAL)
3951 if (ctype_isUnion (ctype_realType (sRef_getType (res))))
3953 res->deriv = sRef_mergeUnionDerivs (res->deriv,
3956 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
3960 res->deriv = sRef_mergeDerivs (res->deriv, other->deriv,
3962 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
3970 res->deriv = sRef_mergeDerivs (res->deriv, other->deriv,
3972 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
3982 if (rdef == SS_PDEFINED
3983 || (rdef == SS_DEFINED && odef == SS_PDEFINED))
3987 res->deriv = sRef_mergePdefinedDerivs (res->deriv, other->deriv,
3989 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
3994 if ((rdef == SS_DEFINED || rdef == SS_UNKNOWN)
3995 && res->defstate == SS_ALLOCATED)
3997 res->deriv = sRefSet_copyInto (res->deriv, other->deriv);
4003 res->deriv = sRef_mergeDerivs (res->deriv, other->deriv,
4005 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
4012 sRef_combineExKinds (res, other);
4016 if (res->kind == SK_ARRAYFETCH && other->kind == SK_PTR)
4018 sRef nother = sRef_buildArrayFetchKnown (sRef_getBase (other), 0);
4020 sRef_copyState (nother, other);
4021 sRef_mergeStateAux (res, nother, cl, opt, loc, doDerivs);
4023 else if (res->kind == SK_PTR && other->kind == SK_ARRAYFETCH)
4025 sRef nother = sRef_buildPointer (sRef_getBase (other));
4027 if (sRef_isReasonable (nother))
4029 sRef_copyState (nother, other);
4030 sRef_mergeStateAux (res, nother, cl, opt, loc, doDerivs);
4035 llcontbug (message ("merge conj: %q / %q", sRef_unparseFull (res),
4036 sRef_unparseFull (other)));
4042 ** Merge value table states
4049 ** This doesn't do anything. And its broken too...
4052 valueTable_elements (res->state, key, sv)
4054 stateValue os = valueTable_lookup (other->state, key);
4055 /*@unused@*/ int val;
4056 /*@unused@*/ char *msg;
4058 llassert (stateValue_isDefined (os));
4060 DPRINTF (("Merge state: %s / %s",
4061 cstring_toCharsSafe (stateValue_unparse (sv)),
4062 cstring_toCharsSafe (stateValue_unparse (os))));
4064 val = valueMatix_lookup (key,
4065 stateValue_getValue (os),
4066 stateValue_getValue (sv),
4068 DPRINTF (("Val: %d / %s", val, msg));
4070 } end_valueTable_elements ;
4073 DPRINTF (("Merge aux: %s / %s",
4074 sRef_unparseFull (res),
4075 sRef_unparseFull (other)));
4079 sRef_mergeUnionDerivs (/*@only@*/ sRefSet res,
4080 /*@exposed@*/ sRefSet other, bool opt,
4081 clause cl, fileloc loc)
4083 if (sRefSet_isEmpty (res))
4085 return sRefSet_copyInto (res, other);
4089 sRefSet_allElements (other, el)
4091 if (sRef_isReasonable (el))
4093 sRef e2 = sRefSet_lookupMember (other, el);
4095 if (sRef_isReasonable (e2))
4097 sRef_mergeStateAux (el, e2, cl, opt, loc, FALSE);
4101 res = sRefSet_insert (res, el);
4104 } end_sRefSet_allElements ;
4110 static /*@only@*/ sRefSet
4111 sRef_mergeDerivs (/*@only@*/ sRefSet res, sRefSet other,
4112 bool opt, clause cl, fileloc loc)
4114 sRefSet ret = sRefSet_new ();
4116 sRefSet_allElements (res, el)
4118 if (sRef_isReasonable (el))
4120 sRef e2 = sRefSet_lookupMember (other, el);
4122 if (sRef_isReasonable (e2))
4124 if (el->defstate == SS_ALLOCATED &&
4125 e2->defstate == SS_PDEFINED)
4127 e2->defstate = SS_ALLOCATED;
4129 else if (e2->defstate == SS_ALLOCATED &&
4130 el->defstate == SS_PDEFINED)
4132 el->defstate = SS_ALLOCATED;
4133 el->definfo = stateInfo_update (el->definfo, e2->definfo);
4134 sRef_clearDerived (el);
4136 else if ((el->defstate == SS_DEAD || sRef_isKept (el)) &&
4137 (e2->defstate == SS_DEFINED && !sRef_isKept (e2)))
4139 DPRINTF (("Checking dead: %s / %s", sRef_unparseFull (el),
4140 sRef_unparseFull (e2)));
4142 if (checkDeadState (el, e2, TRUE, loc))
4144 if (sRef_isThroughArrayFetch (el))
4146 sRef_maybeKill (el, loc);
4147 sRef_maybeKill (e2, loc);
4151 else if ((e2->defstate == SS_DEAD || sRef_isKept (e2)) &&
4152 (el->defstate == SS_DEFINED && !sRef_isKept (el)))
4154 DPRINTF (("Checking dead: %s / %s", sRef_unparseFull (el),
4155 sRef_unparseFull (e2)));
4157 if (checkDeadState (e2, el, FALSE, loc))
4159 if (sRef_isThroughArrayFetch (el))
4161 sRef_maybeKill (el, loc);
4162 sRef_maybeKill (e2, loc);
4166 else if (el->defstate == SS_DEFINED &&
4167 e2->defstate == SS_PDEFINED)
4169 DPRINTF (("set pdefined: %s", sRef_unparseFull (el)));
4170 el->defstate = SS_PDEFINED;
4171 el->definfo = stateInfo_update (el->definfo, e2->definfo);
4173 else if (e2->defstate == SS_DEFINED &&
4174 el->defstate == SS_PDEFINED)
4176 DPRINTF (("set pdefined: %s", sRef_unparseFull (e2)));
4177 e2->defstate = SS_PDEFINED;
4178 e2->definfo = stateInfo_update (e2->definfo, el->definfo);
4185 if (ctype_isUnion (ctype_realType (sRef_getType (el))))
4187 el->deriv = sRef_mergeUnionDerivs (el->deriv, e2->deriv,
4192 el->deriv = sRef_mergeDerivs (el->deriv, e2->deriv, opt, cl, loc);
4195 if (sRef_equivalent (el, e2))
4197 ret = sRefSet_insert (ret, el);
4201 sRef sr = sRef_leastCommon (el, e2);
4203 if (sRef_isReasonable (sr))
4205 ret = sRefSet_insert (ret, sr);
4213 (void) sRefSet_delete (other, e2);
4215 else /* not defined */
4217 DPRINTF (("Checking dead: %s", sRef_unparseFull (el)));
4218 (void) checkDeadState (el, e2, TRUE, loc);
4221 } end_sRefSet_allElements;
4223 sRefSet_allElements (other, el)
4225 if (sRef_isReasonable (el))
4227 DPRINTF (("Checking dead: %s", sRef_unparseFull (el)));
4228 (void) checkDeadState (el, sRef_undefined, FALSE, loc);
4230 } end_sRefSet_allElements;
4237 ** Returns TRUE is there is an error.
4240 static bool checkDeadState (/*@notnull@*/ sRef el, sRef e2, bool tbranch, fileloc loc)
4243 ** usymtab_isGuarded --- the utab should still be in the
4244 ** state of the alternate branch.
4246 ** tbranch TRUE means el is released in the last branch, e.g.
4247 ** if (x != NULL) { ; } else { sfree (x); }
4248 ** so, if x is null in the other branch no error is reported.
4250 ** tbranch FALSE means this is the other branch:
4251 ** if (x != NULL) { sfree (x); } else { ; }
4252 ** so, if x is null in this branch there is no error.
4256 if ((sRef_isDead (el) || sRef_isKept (el))
4257 && !sRef_isDeepUnionField (el)
4258 && !sRef_isThroughArrayFetch (el))
4263 if (usymtab_isDefinitelyNullDeep (el))
4270 if (usymtab_isAltDefinitelyNullDeep (el))
4278 message ("Storage %q is %q in one path, but live in another.",
4280 cstring_makeLiteral (sRef_isKept (el)
4281 ? "kept" : "released")),
4284 if (sRef_isKept (el))
4286 sRef_showAliasInfo (el);
4290 sRef_showStateInfo (el);
4293 if (sRef_isValid (e2))
4295 if (sRef_isKept (e2))
4297 sRef_showAliasInfo (e2);
4301 sRef_showStateInfo (e2);
4305 /* prevent further errors */
4306 el->defstate = SS_UNKNOWN;
4307 sRef_setAliasKind (el, AK_ERROR, fileloc_undefined);
4316 checkDerivDeadState (/*@notnull@*/ sRef el, bool tbranch, fileloc loc)
4318 if (checkDeadState (el, sRef_undefined, tbranch, loc))
4320 sRefSet_allElements (el->deriv, t)
4322 if (sRef_isReasonable (t))
4324 checkDerivDeadState (t, tbranch, loc);
4326 } end_sRefSet_allElements;
4331 sRef_mergePdefinedDerivs (sRefSet res, sRefSet other, bool opt,
4332 clause cl, fileloc loc)
4334 sRefSet ret = sRefSet_new ();
4336 sRefSet_allElements (res, el)
4338 if (sRef_isReasonable (el))
4340 sRef e2 = sRefSet_lookupMember (other, el);
4342 if (sRef_isReasonable (e2))
4344 if (sRef_isAllocated (el) && !sRef_isAllocated (e2))
4348 else if (sRef_isAllocated (e2) && !sRef_isAllocated (el))
4350 el->deriv = sRefSet_copyInto (el->deriv, e2->deriv);
4354 el->deriv = sRef_mergePdefinedDerivs (el->deriv, e2->deriv,
4358 sRef_mergeStateAux (el, e2, cl, opt, loc, FALSE);
4360 ret = sRefSet_insert (ret, el);
4361 (void) sRefSet_delete (other, e2);
4367 checkDerivDeadState (el, (cl == FALSECLAUSE), loc);
4370 ret = sRefSet_insert (ret, el);
4373 } end_sRefSet_allElements;
4375 sRefSet_allElements (other, el)
4377 if (sRef_isReasonable (el))
4379 if (!sRefSet_member (ret, el))
4381 /* was cl == FALSECLAUSE */
4382 checkDerivDeadState (el, FALSE, loc);
4383 ret = sRefSet_insert (ret, el);
4388 ** it's okay --- member is a different equality test
4392 } end_sRefSet_allElements;
4398 sRef sRef_makeConj (/*@exposed@*/ /*@returned@*/ sRef a, /*@exposed@*/ sRef b)
4400 llassert (sRef_isReasonable (a));
4401 llassert (sRef_isReasonable (b));
4403 if (!sRef_equivalent (a, b))
4405 sRef s = sRef_newRef ();
4408 s->info = (sinfo) dmalloc (sizeof (*s->info));
4409 s->info->conj = (cjinfo) dmalloc (sizeof (*s->info->conj));
4410 s->info->conj->a = a; /* sRef_copy (a) */ /*@i32*/ ;
4411 s->info->conj->b = b; /* sRef_copy (b);*/ /*@i32@*/ ;
4413 if (ctype_equal (a->type, b->type)) s->type = a->type;
4414 else s->type = ctype_makeConj (a->type, b->type);
4416 if (a->defstate == b->defstate)
4418 s->defstate = a->defstate;
4419 s->definfo = stateInfo_update (s->definfo, a->definfo);
4420 s->definfo = stateInfo_update (s->definfo, b->definfo);
4424 s->defstate = SS_UNKNOWN;
4427 sRef_setNullStateN (s, NS_UNKNOWN);
4429 s->safe = a->safe && b->safe;
4430 s->aliaskind = alkind_resolve (a->aliaskind, b->aliaskind);
4432 llassert (valueTable_isUndefined (s->state));
4433 s->state = context_createValueTable (s, stateInfo_makeLoc (g_currentloc, SA_MERGED));
4438 /*@-exposetrans@*/ return a; /*@=exposetrans@*/
4442 /*@dependent@*/ sRef
4445 sRef s = sRef_new ();
4447 s->kind = SK_UNKNOWN;
4451 static /*@owned@*/ /*@notnull@*/ sRef
4452 sRef_makeSpecial (speckind sk) /*@*/
4454 sRef s = sRef_new ();
4456 s->kind = SK_SPECIAL;
4457 s->info = (sinfo) dmalloc (sizeof (*s->info));
4459 /*@-dependenttrans@*/
4461 /*@=dependenttrans@*/
4464 static /*@owned@*/ sRef srnothing = sRef_undefined;
4465 static /*@owned@*/ sRef srinternal = sRef_undefined;
4466 static /*@owned@*/ sRef srsystem = sRef_undefined;
4467 static /*@owned@*/ sRef srspec = sRef_undefined;
4469 /*@dependent@*/ sRef
4470 sRef_makeNothing (void)
4472 if (sRef_isInvalid (srnothing))
4474 srnothing = sRef_makeSpecial (SR_NOTHING);
4481 sRef_makeInternalState (void)
4483 if (sRef_isInvalid (srinternal))
4485 srinternal = sRef_makeSpecial (SR_INTERNAL);
4492 sRef_makeSpecState (void)
4494 if (sRef_isInvalid (srspec))
4496 srspec = sRef_makeSpecial (SR_SPECSTATE);
4503 sRef_makeSystemState (void)
4505 if (sRef_isInvalid (srsystem))
4507 srsystem = sRef_makeSpecial (SR_SYSTEM);
4514 sRef_makeGlobalMarker (void)
4516 sRef s = sRef_makeSpecial (SR_GLOBALMARKER);
4517 llassert (valueTable_isUndefined (s->state));
4518 s->state = context_createGlobalMarkerValueTable (stateInfo_undefined);
4523 sRef_makeResult (ctype c)
4525 sRef s = sRef_newRef ();
4527 s->kind = SK_RESULT;
4529 s->defstate = SS_UNKNOWN;
4530 s->aliaskind = AK_UNKNOWN;
4531 sRef_setNullStateN (s, NS_UNKNOWN);
4532 llassert (valueTable_isUndefined (s->state));
4533 s->state = context_createValueTable (s, stateInfo_makeLoc (g_currentloc, SA_DECLARED));
4535 DPRINTF (("Result: [%p] %s", s, sRef_unparseFull (s)));
4541 sRef_isNothing (sRef s)
4543 return (sRef_isKindSpecial (s) && s->info->spec == SR_NOTHING);
4547 sRef_isInternalState (sRef s)
4549 return (sRef_isKindSpecial (s) && s->info->spec == SR_INTERNAL);
4553 sRef_isSpecInternalState (sRef s)
4555 return (sRef_isKindSpecial (s)
4556 && (s->info->spec == SR_INTERNAL || s->info->spec == SR_SPECSTATE));
4560 sRef_isSpecState (sRef s)
4562 return (sRef_isKindSpecial (s) && s->info->spec == SR_SPECSTATE);
4566 sRef_isResult (sRef s)
4568 return (sRef_isReasonable (s) && s->kind == SK_RESULT);
4572 sRef_isSystemState (sRef s)
4574 return (sRef_isKindSpecial (s) && s->info->spec == SR_SYSTEM);
4578 sRef_isGlobalMarker (sRef s)
4580 return (sRef_isKindSpecial (s) && s->info->spec == SR_GLOBALMARKER);
4584 sRef_getScopeIndex (sRef s)
4586 llassert (sRef_isReasonable (s));
4587 llassert (sRef_isCvar (s));
4589 return (s->info->cvar->index);
4593 sRef_makeSafe (sRef s)
4595 if (sRef_isReasonable (s))
4602 sRef_makeUnsafe (sRef s)
4604 if (sRef_isReasonable (s))
4611 ** memory state operations
4614 /*@only@*/ cstring sRef_unparseFull (sRef s)
4616 if (sRef_isInvalid (s)) return (cstring_undefined);
4618 return (message ("[%p] %q - %q { %q } [%s] { %q } < %q >",
4620 sRef_unparseDebug (s),
4621 sRef_unparseState (s),
4622 stateInfo_unparse (s->definfo),
4623 exkind_unparse (s->oexpkind),
4624 sRefSet_unparseDebug (s->deriv),
4625 valueTable_unparse (s->state)));
4628 /*@unused@*/ cstring sRef_unparseDeep (sRef s)
4630 cstring st = cstring_undefined;
4632 st = message ("%q:", sRef_unparseFull (s));
4634 if (sRef_isReasonable (s))
4636 sRefSet_allElements (s->deriv, el)
4638 st = message("%q\n%q", st, sRef_unparseDeep (el));
4639 } end_sRefSet_allElements ;
4645 /*@only@*/ cstring sRef_unparseState (sRef s)
4647 if (sRef_isConj (s))
4649 return (message ("%q | %q",
4650 sRef_unparseState (s->info->conj->a),
4651 sRef_unparseState (s->info->conj->b)));
4654 if (sRef_isInvalid (s))
4656 return (cstring_makeLiteral ("<invalid>"));
4659 return (message ("%s.%s.%s.%s",
4660 alkind_unparse (s->aliaskind),
4661 nstate_unparse (sRef_getNullState (s)),
4662 exkind_unparse (s->expkind),
4663 sstate_unparse (s->defstate)));
4666 bool sRef_isNotUndefined (sRef s)
4668 return (sRef_isInvalid (s)
4669 || (s->defstate != SS_UNDEFINED
4670 && s->defstate != SS_UNUSEABLE
4671 && s->defstate != SS_DEAD));
4674 ynm sRef_isWriteable (sRef s)
4676 if (sRef_isInvalid (s)) return MAYBE;
4678 if (sRef_isConj (s) && s->defstate == SS_UNKNOWN)
4680 if (ynm_toBoolStrict (sRef_isWriteable (sRef_getConjA (s))))
4682 if (ynm_toBoolStrict (sRef_isWriteable (sRef_getConjB (s))))
4690 if (ynm_toBoolStrict (sRef_isWriteable (sRef_getConjB (s))))
4698 return (ynm_fromBool (s->defstate != SS_UNUSEABLE));
4701 bool sRef_hasNoStorage (sRef s)
4703 return (!sRef_isAllocatedStorage (s) || sRef_isDefinitelyNull (s));
4706 bool sRef_isStrictReadable (sRef s)
4708 return (ynm_toBoolStrict (sRef_isValidLvalue (s)));
4712 ** Is this what is does?
4713 ** Returns YES if s can be used as an rvalue,
4714 ** MAYBE if its not clear
4715 ** NO if s cannot be safely used as an rvalue.
4718 ynm sRef_isValidLvalue (sRef s)
4722 if (sRef_isInvalid (s)) return YES;
4726 if (sRef_isConj (s) && s->defstate == SS_UNKNOWN)
4728 if (ynm_toBoolStrict (sRef_isValidLvalue (sRef_getConjA (s))))
4730 if (ynm_toBoolStrict (sRef_isValidLvalue (sRef_getConjB (s))))
4738 if (ynm_toBoolStrict (sRef_isValidLvalue (sRef_getConjB (s))))
4745 else if (ss == SS_HOFFA)
4747 if (context_getFlag (FLG_STRICTUSERELEASED))
4758 return (ynm_fromBool (ss == SS_DEFINED
4761 || ss == SS_PDEFINED
4764 || ss == SS_ALLOCATED
4765 || ss == SS_KILLED /* evans 2001-05-26: added this for killed globals */
4766 || ss == SS_UNKNOWN));
4770 static /*@exposed@*/ sRef whatUndefined (/*@exposed@*/ sRef fref, int depth)
4775 if (depth > MAXDEPTH)
4778 ("Warning: check definition limit exceeded, checking %q. "
4779 "This either means there is a variable with at least "
4780 "%d indirections apparent in the program text, or "
4781 "there is a bug in Splint.",
4782 sRef_unparse (fref),
4786 return sRef_undefined;
4789 if (!sRef_isKnown (fref) || sRef_isAnyDefined (fref))
4791 return sRef_undefined;
4794 if (sRef_isUnuseable (fref) || sRef_isStateUndefined (fref))
4799 ct = ctype_realType (sRef_getType (fref));
4801 if (ctype_isUnknown (ct))
4803 return sRef_undefined;
4805 else if (ctype_isPointer (ct) || ctype_isArray (ct))
4807 if (sRef_isStateUnknown (fref))
4809 return sRef_undefined;
4813 sRef fptr = sRef_constructDeref (fref);
4815 return (whatUndefined (fptr, depth + 1));
4818 else if (ctype_isStruct (ct))
4820 bool hasOneDefined = FALSE;
4822 if (sRef_isStateUnknown (fref))
4827 if (sRef_isPdefined (fref) || sRef_isAnyDefined (fref))
4829 sRefSet_realElements (sRef_derivedFields (fref), sr)
4831 hasOneDefined = TRUE;
4833 if (sRef_isField (sr))
4835 cstring fieldname = sRef_getField (sr);
4836 sRef fldref = sRef_makeField (fref, fieldname);
4837 bool shouldCheck = !sRef_isRecursiveField (fldref);
4841 sRef wdef = whatUndefined (fldref, depth + 1);
4843 if (sRef_isReasonable (wdef))
4849 } end_sRefSet_realElements;
4851 else if (sRef_isAllocated (fref))
4854 ** for structures, each field must be completely defined
4857 uentryList fields = ctype_getFields (ct);
4859 uentryList_elements (fields, ue)
4861 cstring name = uentry_getRealName (ue);
4862 sRef ffield = sRef_makeField (fref, name);
4863 bool shouldCheck = !sRef_isRecursiveField (ffield);
4865 if (sRef_isRelDef (uentry_getSref (ue)))
4873 sRef wdef = whatUndefined (ffield, depth + 1);
4875 if (sRef_isInvalid (wdef))
4881 } end_uentryList_elements;
4888 else if (ctype_isUnion (ct))
4897 return sRef_undefined;
4900 static bool checkDefined (/*@temp@*/ sRef sr)
4902 /*@-temptrans@*/ /* the result from whatUndefined is lost */
4903 return (sRef_isInvalid (whatUndefined (sr, 0)));
4907 bool sRef_isReallyDefined (sRef s)
4909 if (sRef_isReasonable (s))
4911 if (sRef_isAnyDefined (s))
4917 if (sRef_isAllocated (s) || sRef_isPdefined (s))
4919 return checkDefined (s);
4933 void sRef_showNotReallyDefined (sRef s)
4935 if (sRef_isReasonable (s))
4937 if (sRef_isAnyDefined (s))
4943 if (sRef_isAllocated (s) || sRef_isPdefined (s))
4945 /*@-temptrans@*/ /* the result of whatUndefined is lost */
4946 sRef ref = whatUndefined (s, 0);
4948 llassert (sRef_isReasonable (ref));
4953 (message ("This sub-reference is %s: %q",
4954 sstate_unparse (sRef_getDefState (ref)),
4955 sRef_unparse (ref)));
4970 sstate sRef_getDefState (sRef s)
4972 if (sRef_isInvalid (s)) return (SS_UNKNOWN);
4973 return (s->defstate);
4976 void sRef_setDefState (sRef s, sstate defstate, fileloc loc)
4978 sRef_checkMutable (s);
4979 sRef_setStateAux (s, defstate, loc);
4982 static void sRef_clearAliasStateAux (sRef s, fileloc loc)
4984 sRef_checkMutable (s);
4985 sRef_setAliasKind (s, AK_ERROR, loc);
4988 void sRef_clearAliasState (sRef s, fileloc loc)
4990 sRef_checkMutable (s);
4991 sRef_aliasSetComplete (sRef_clearAliasStateAux, s, loc);
4994 void sRef_setAliasKindComplete (sRef s, alkind kind, fileloc loc)
4996 sRef_checkMutable (s);
4997 sRef_aliasSetCompleteAlkParam (sRef_setAliasKind, s, kind, loc);
5000 void sRef_setAliasKind (sRef s, alkind kind, fileloc loc)
5002 sRef_checkMutable (s);
5004 if (sRef_isReasonable (s))
5006 sRef_clearDerived (s);
5008 if ((kind != s->aliaskind && kind != s->oaliaskind)
5009 && fileloc_isDefined (loc))
5011 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, stateAction_fromAlkind (kind), loc);
5014 s->aliaskind = kind;
5018 void sRef_setOrigAliasKind (sRef s, alkind kind)
5020 sRef_checkMutable (s);
5022 if (sRef_isReasonable (s))
5024 s->oaliaskind = kind;
5028 exkind sRef_getExKind (sRef s)
5030 if (sRef_isReasonable (s))
5032 return (s->expkind);
5040 exkind sRef_getOrigExKind (sRef s)
5042 if (sRef_isReasonable (s))
5044 return (s->oexpkind);
5052 static void sRef_clearExKindAux (sRef s, fileloc loc)
5054 sRef_checkMutable (s);
5055 sRef_setExKind (s, XO_UNKNOWN, loc);
5058 void sRef_setObserver (sRef s, fileloc loc)
5060 sRef_checkMutable (s);
5061 sRef_setExKind (s, XO_OBSERVER, loc);
5064 void sRef_setExposed (sRef s, fileloc loc)
5066 sRef_checkMutable (s);
5067 sRef_setExKind (s, XO_EXPOSED, loc);
5070 void sRef_clearExKindComplete (sRef s, fileloc loc)
5072 (void) sRef_aliasSetComplete (sRef_clearExKindAux, s, loc);
5075 void sRef_setExKind (sRef s, exkind exp, fileloc loc)
5077 sRef_checkMutable (s);
5079 if (sRef_isReasonable (s))
5081 if (s->expkind != exp)
5083 s->expinfo = stateInfo_updateLoc (s->expinfo, stateAction_fromExkind (exp), loc);
5091 ** s1->derived = s2->derived
5094 static void sRef_copyRealDerived (sRef s1, sRef s2)
5096 DPRINTF (("Copy real: %s / %s", sRef_unparse (s1), sRef_unparse (s2)));
5097 sRef_checkMutable (s1);
5099 if (sRef_isReasonable (s1) && sRef_isReasonable (s2))
5101 sRef sb = sRef_getRootBase (s1);
5103 sRefSet_clear (s1->deriv);
5105 sRefSet_allElements (s2->deriv, el)
5107 if (sRef_isReasonable (el))
5109 sRef rb = sRef_getRootBase (el);
5111 if (!sRef_same (rb, sb))
5113 sRef fb = sRef_fixDirectBase (el, s1);
5115 if (sRef_isReasonable (fb))
5117 sRef_copyRealDerived (fb, el);
5118 sRef_addDeriv (s1, fb);
5123 sRef_addDeriv (s1, el);
5126 } end_sRefSet_allElements ;
5131 void sRef_copyRealDerivedComplete (sRef s1, sRef s2)
5133 sRef_innerAliasSetCompleteParam (sRef_copyRealDerived, s1, s2);
5136 void sRef_setUndefined (sRef s, fileloc loc)
5138 sRef_checkMutable (s);
5140 if (sRef_isReasonable (s))
5142 s->defstate = SS_UNDEFINED;
5143 s->definfo = stateInfo_updateLoc (s->definfo, SA_UNDEFINED, loc);
5145 sRef_clearDerived (s);
5149 static void sRef_setDefinedAux (sRef s, fileloc loc, bool clear)
5151 sRef_checkMutable (s);
5152 if (sRef_isInvalid (s)) return;
5154 DPRINTF (("Set defined: %s", sRef_unparseFull (s)));
5156 s->definfo = stateInfo_updateLoc (s->definfo, SA_DEFINED, loc);
5157 s->defstate = SS_DEFINED;
5159 DPRINTF (("Set defined: %s", sRef_unparseFull (s)));
5161 /* e.g., if x is allocated, *x = 3 defines x */
5163 if (s->kind == SK_PTR)
5165 sRef p = s->info->ref;
5168 if (p->defstate == SS_ALLOCATED
5169 || p->defstate == SS_SPECIAL) /* evans 2001-07-12: shouldn't need this */
5171 sRef_setDefinedAux (p, loc, clear);
5175 ** Defines a[0] also:
5178 arr = sRef_findDerivedArrayFetch (p, FALSE, 0, FALSE);
5180 if (sRef_isReasonable (arr))
5182 sRef_setDefinedAux (arr, loc, clear);
5185 else if (s->kind == SK_ARRAYFETCH)
5187 if (!s->info->arrayfetch->indknown
5188 || (s->info->arrayfetch->ind == 0))
5190 sRef p = s->info->arrayfetch->arr;
5191 sRef ptr = sRef_constructPointer (p);
5193 if (sRef_isReasonable (ptr))
5195 if (ptr->defstate == SS_ALLOCATED
5196 || ptr->defstate == SS_UNDEFINED
5197 || ptr->defstate == SS_SPECIAL) /* evans 2001-07-12: shouldn't need this */
5199 sRef_setDefinedAux (ptr, loc, clear);
5203 if (p->defstate == SS_RELDEF)
5207 else if (p->defstate == SS_ALLOCATED || p->defstate == SS_PDEFINED
5208 || p->defstate == SS_SPECIAL) /* evans 2001-07-12: shouldn't need this */
5210 p->defstate = SS_DEFINED;
5217 else if (s->kind == SK_FIELD)
5219 sRef parent = s->info->field->rec;
5221 if (sRef_isReasonable (parent))
5223 if (ctype_isUnion (ctype_realType (parent->type)))
5226 ** Should not clear derived from here.
5229 sRef_setDefinedNoClear (parent, loc);
5233 ; /* Nothing to do for structures. */
5245 sRef_clearDerived (s);
5249 /* evans 2001-07-12: need to define the derived references */
5250 sRefSet_elements (s->deriv, el)
5252 llassert (sRef_isReasonable (el));
5253 el->defstate = SS_DEFINED;
5254 } end_sRefSet_elements ;
5257 DPRINTF (("Set defined: %s", sRef_unparseFull (s)));
5260 static void sRef_setPartialDefined (sRef s, fileloc loc)
5262 sRef_checkMutable (s);
5264 if (!sRef_isPartial (s))
5266 sRef_setDefined (s, loc);
5270 void sRef_setPartialDefinedComplete (sRef s, fileloc loc)
5272 sRef_innerAliasSetComplete (sRef_setPartialDefined, s, loc);
5275 void sRef_setDefinedComplete (sRef s, fileloc loc)
5277 sRef_innerAliasSetComplete (sRef_setDefined, s, loc);
5280 void sRef_setDefinedCompleteDirect (sRef s, fileloc loc)
5284 aliases = usymtab_allAliases (s);
5285 DPRINTF (("Set defined complete: %s", sRef_unparseFull (s)));
5286 DPRINTF (("All aliases: %s", sRefSet_unparseFull (aliases)));
5288 sRef_setDefined (s, loc);
5290 sRefSet_realElements (aliases, current)
5292 if (sRef_isReasonable (current))
5294 current = sRef_updateSref (current);
5295 sRef_setDefined (current, loc);
5297 } end_sRefSet_realElements;
5299 sRefSet_free (aliases);
5300 sRef_innerAliasSetComplete (sRef_setDefined, s, loc);
5303 void sRef_setDefined (sRef s, fileloc loc)
5305 sRef_checkMutable (s);
5306 sRef_setDefinedAux (s, loc, TRUE);
5309 static void sRef_setDefinedNoClear (sRef s, fileloc loc)
5311 sRef_checkMutable (s);
5312 DPRINTF (("Defining: %s", sRef_unparseFull (s)));
5313 sRef_setDefinedAux (s, loc, FALSE);
5314 DPRINTF (("==> %s", sRef_unparseFull (s)));
5317 void sRef_setDefinedNCComplete (sRef s, fileloc loc)
5319 sRef_checkMutable (s);
5320 DPRINTF (("Set Defined Complete: %s", sRef_unparseFull (s)));
5321 sRef_innerAliasSetComplete (sRef_setDefinedNoClear, s, loc);
5322 DPRINTF (("==> %s", sRef_unparseFull (s)));
5325 static bool sRef_isDeepUnionField (sRef s)
5327 return (sRef_deepPred (sRef_isUnionField, s));
5330 bool sRef_isUnionField (sRef s)
5332 if (sRef_isReasonable (s) && s->kind == SK_FIELD)
5335 ** defining one field of a union defines the union
5338 sRef base = s->info->field->rec;
5340 if (sRef_isReasonable (base))
5342 return (ctype_isUnion (ctype_realType (base->type)));
5349 void sRef_setPdefined (sRef s, fileloc loc)
5351 sRef_checkMutable (s);
5352 if (sRef_isReasonable (s) && !sRef_isPartial (s))
5354 sRef base = sRef_getBaseSafe (s);
5356 if (s->defstate == SS_ALLOCATED)
5361 s->definfo = stateInfo_updateLoc (s->definfo, SA_PDEFINED, loc);
5362 s->defstate = SS_PDEFINED;
5364 /* e.g., if x is allocated, *x = 3 defines x */
5366 while (sRef_isReasonable (base) && sRef_isKnown (base))
5368 if (base->defstate == SS_DEFINED)
5372 DPRINTF (("set pdefined: %s", sRef_unparseFull (base)));
5373 base->defstate = SS_PDEFINED;
5374 nb = sRef_getBaseSafe (base);
5385 static void sRef_setStateAux (sRef s, sstate ss, fileloc loc)
5387 sRef_checkMutable (s);
5389 DPRINTF (("Set state: %s => %s", sRef_unparseFull (s), sstate_unparse (ss)));
5391 if (sRef_isReasonable (s))
5393 /* if (s->defstate == SS_RELDEF) return; */
5395 if (s->defstate != ss && fileloc_isDefined (loc))
5397 s->definfo = stateInfo_updateLoc (s->definfo,
5398 stateAction_fromSState (ss), loc);
5402 sRef_clearDerived (s);
5404 if (ss == SS_ALLOCATED)
5406 sRef base = sRef_getBaseSafe (s);
5408 while (sRef_isReasonable (base) && sRef_isKnown (base))
5410 if (base->defstate == SS_DEFINED)
5414 DPRINTF (("set pdefined: %s", sRef_unparseFull (s)));
5415 base->defstate = SS_PDEFINED;
5416 nb = sRef_getBaseSafe (base);
5428 void sRef_setAllocatedComplete (sRef s, fileloc loc)
5430 sRef_innerAliasSetComplete (sRef_setAllocated, s, loc);
5433 static void sRef_setAllocatedShallow (sRef s, fileloc loc)
5435 sRef_checkMutable (s);
5437 if (sRef_isReasonable (s))
5439 if (s->defstate == SS_DEAD || s->defstate == SS_UNDEFINED)
5441 s->defstate = SS_ALLOCATED;
5442 s->definfo = stateInfo_updateLoc (s->definfo, SA_ALLOCATED, loc);
5447 void sRef_setAllocatedShallowComplete (sRef s, fileloc loc)
5449 sRef_innerAliasSetComplete (sRef_setAllocatedShallow, s, loc);
5452 void sRef_setAllocated (sRef s, fileloc loc)
5454 sRef_checkMutable (s);
5455 sRef_setStateAux (s, SS_ALLOCATED, loc);
5458 void sRef_setPartial (sRef s, fileloc loc)
5460 sRef_checkMutable (s);
5461 sRef_setStateAux (s, SS_PARTIAL, loc);
5464 void sRef_setShared (sRef s, fileloc loc)
5466 sRef_checkMutable (s);
5468 if (sRef_isReasonable (s))
5470 if (s->aliaskind != AK_SHARED && fileloc_isDefined (loc))
5472 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, SA_SHARED, loc);
5475 s->aliaskind = AK_SHARED;
5476 /* don't! sRef_clearDerived (s); */
5480 void sRef_setLastReference (sRef s, /*@exposed@*/ sRef ref, fileloc loc)
5482 sRef_checkMutable (s);
5484 if (sRef_isReasonable (s))
5486 s->aliaskind = sRef_getAliasKind (ref);
5487 s->aliasinfo = stateInfo_updateRefLoc (s->aliasinfo, ref, stateAction_fromAlkind (s->aliaskind), loc);
5492 void sRef_setNullStateAux (/*@notnull@*/ sRef s, nstate ns, fileloc loc)
5494 sRef_checkMutable (s);
5496 sRef_resetAliasKind (s);
5498 if (fileloc_isDefined (loc))
5500 s->nullinfo = stateInfo_updateLoc (s->nullinfo, stateAction_fromNState (ns), loc);
5504 void sRef_setNotNull (sRef s, fileloc loc)
5506 if (sRef_isReasonable (s))
5508 sRef_setNullStateAux (s, NS_NOTNULL, loc);
5512 void sRef_setNullStateN (sRef s, nstate n)
5514 if (sRef_isReasonable (s))
5516 sRef_checkMutable (s);
5518 sRef_resetAliasKind (s);
5522 void sRef_setNullState (sRef s, nstate n, fileloc loc)
5524 if (sRef_isReasonable (s))
5526 sRef_setNullStateAux (s, n, loc);
5530 void sRef_setNullTerminatedStateInnerComplete (sRef s, struct s_bbufinfo b, /*@unused@*/ fileloc loc)
5532 switch (b.bufstate) {
5533 case BB_NULLTERMINATED:
5534 sRef_setNullTerminatedState (s);
5535 sRef_setLen (s, b.len);
5537 case BB_POSSIBLYNULLTERMINATED:
5538 sRef_setPossiblyNullTerminatedState(s);
5540 case BB_NOTNULLTERMINATED:
5541 sRef_setNotNullTerminatedState (s);
5545 sRef_setSize (s, b.size);
5547 /* PL: TO BE DONE : Aliases are not modified right now, have to be similar to
5548 * setNullStateInnerComplete.
5552 void sRef_setNullStateInnerComplete (sRef s, nstate n, fileloc loc)
5554 DPRINTF (("Set null state: %s", nstate_unparse (n)));
5556 sRef_setNullState (s, n, loc);
5561 sRef_innerAliasSetComplete (sRef_setPosNull, s, loc);
5564 sRef_innerAliasSetComplete (sRef_setDefNull, s, loc);
5567 sRef_innerAliasSetComplete (sRef_setNullUnknown, s, loc);
5570 sRef_innerAliasSetComplete (sRef_setNotNull, s, loc);
5573 sRef_innerAliasSetComplete (sRef_setNotNull, s, loc);
5576 sRef_innerAliasSetComplete (sRef_setNullUnknown, s, loc);
5579 sRef_innerAliasSetComplete (sRef_setDefNull, s, loc);
5582 sRef_innerAliasSetComplete (sRef_setNullUnknown, s, loc);
5585 sRef_innerAliasSetComplete (sRef_setNullErrorLoc, s, loc);
5590 void sRef_setPosNull (sRef s, fileloc loc)
5592 if (sRef_isReasonable (s))
5594 sRef_setNullStateAux (s, NS_POSNULL, loc);
5598 void sRef_setDefNull (sRef s, fileloc loc)
5600 if (sRef_isReasonable (s))
5602 sRef_setNullStateAux (s, NS_DEFNULL, loc);
5606 void sRef_setNullUnknown (sRef s, fileloc loc)
5608 if (sRef_isReasonable (s))
5610 sRef_setNullStateAux (s, NS_UNKNOWN, loc);
5614 void sRef_setNullError (sRef s)
5616 if (sRef_isReasonable (s) && !sRef_isConst (s))
5618 sRef_setNullStateAux (s, NS_UNKNOWN, fileloc_undefined);
5622 void sRef_setNullErrorLoc (sRef s, fileloc loc)
5624 if (sRef_isReasonable (s) && !sRef_isConst (s))
5626 sRef_setNullStateAux (s, NS_UNKNOWN, loc);
5630 void sRef_setOnly (sRef s, fileloc loc)
5632 if (sRef_isReasonable (s) && s->aliaskind != AK_ONLY)
5634 sRef_checkMutable (s);
5635 s->aliaskind = AK_ONLY;
5636 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, SA_ONLY, loc);
5640 void sRef_setDependent (sRef s, fileloc loc)
5642 if (sRef_isReasonable (s) && !sRef_isConst (s) && (s->aliaskind != AK_DEPENDENT))
5644 sRef_checkMutable (s);
5645 DPRINTF (("Setting dependent: %s", sRef_unparseFull (s)));
5646 s->aliaskind = AK_DEPENDENT;
5647 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, SA_DEPENDENT, loc);
5651 void sRef_setOwned (sRef s, fileloc loc)
5653 if (sRef_isReasonable (s) && !sRef_isConst (s) && (s->aliaskind != AK_OWNED))
5655 sRef_checkMutable (s);
5656 s->aliaskind = AK_OWNED;
5657 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, SA_OWNED, loc);
5661 void sRef_setKept (sRef s, fileloc loc)
5663 if (sRef_isReasonable (s) && !sRef_isConst (s) && (s->aliaskind != AK_KEPT))
5665 sRef base = sRef_getBaseSafe (s);
5667 while (sRef_isReasonable (base) && sRef_isKnown (base))
5669 if (base->defstate == SS_DEFINED)
5671 sRef_checkMutable (base);
5672 base->defstate = SS_PDEFINED;
5673 base = sRef_getBaseSafe (base);
5681 sRef_checkMutable (s);
5682 s->aliaskind = AK_KEPT;
5683 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, SA_KEPT, loc);
5687 static void sRef_setKeptAux (sRef s, fileloc loc)
5689 if (!sRef_isShared (s))
5691 sRef_setKept (s, loc);
5695 static void sRef_setDependentAux (sRef s, fileloc loc)
5697 if (!sRef_isShared (s))
5699 sRef_setDependent (s, loc);
5703 void sRef_setKeptComplete (sRef s, fileloc loc)
5705 sRef_aliasSetComplete (sRef_setKeptAux, s, loc);
5708 void sRef_setDependentComplete (sRef s, fileloc loc)
5710 sRef_aliasSetComplete (sRef_setDependentAux, s, loc);
5713 void sRef_setFresh (sRef s, fileloc loc)
5715 if (sRef_isReasonable (s) && !sRef_isConst (s))
5717 sRef_checkMutable (s);
5718 s->aliaskind = AK_FRESH;
5719 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, SA_CREATED, loc);
5720 DPRINTF (("SetFresh: %s", sRef_unparseFull (s)));
5724 void sRef_kill (sRef s, fileloc loc)
5726 DPRINTF (("Kill: %s", sRef_unparseFull (s)));
5728 if (sRef_isReasonable (s) && !sRef_isShared (s) && !sRef_isConst (s))
5730 sRef base = sRef_getBaseSafe (s);
5731 sRef_checkMutable (s);
5733 while (sRef_isReasonable (base) && sRef_isKnown (base))
5735 if (base->defstate == SS_DEFINED)
5737 sRef_checkMutable (base);
5738 base->defstate = SS_PDEFINED;
5739 base = sRef_getBaseSafe (base);
5747 s->aliaskind = s->oaliaskind;
5748 s->defstate = SS_DEAD;
5749 s->definfo = stateInfo_updateLoc (s->definfo, SA_KILLED, loc);
5750 DPRINTF (("State info: %s", stateInfo_unparse (s->definfo)));
5751 sRef_clearDerived (s);
5755 void sRef_maybeKill (sRef s, fileloc loc)
5757 if (sRef_isReasonable (s))
5759 sRef base = sRef_getBaseSafe (s);
5760 sRef_checkMutable (s);
5762 while (sRef_isReasonable (base) && sRef_isKnown (base))
5764 if (base->defstate == SS_DEFINED || base->defstate == SS_RELDEF)
5766 sRef_checkMutable (base);
5767 base->defstate = SS_PDEFINED;
5768 base = sRef_getBaseSafe (base);
5777 s->aliaskind = s->oaliaskind;
5778 s->defstate = SS_HOFFA;
5779 s->definfo = stateInfo_updateLoc (s->definfo, SA_PKILLED, loc);
5780 DPRINTF (("State info: %s / %s", sRef_unparse (s),
5781 stateInfo_unparse (s->definfo)));
5782 sRef_clearDerived (s);
5788 ** just for type checking...
5791 static void sRef_killAux (sRef s, fileloc loc)
5793 if (sRef_isReasonable (s) && !sRef_isShared (s))
5795 if (sRef_isUnknownArrayFetch (s))
5797 sRef_maybeKill (s, loc);
5807 ** kills s and all aliases to s
5810 void sRef_killComplete (sRef s, fileloc loc)
5812 DPRINTF (("Kill complete: %s", sRef_unparseFull (s)));
5813 sRef_aliasSetComplete (sRef_killAux, s, loc);
5816 static bool sRef_equivalent (sRef s1, sRef s2)
5818 return (sRef_compare (s1, s2) == 0);
5822 ** returns an sRef that will not be free'd on function exit.
5825 /*@only@*/ sRef sRef_saveCopy (sRef s)
5829 if (sRef_isReasonable (s))
5831 bool old = inFunction;
5834 ** Exit the function scope, so this sRef is not
5835 ** stored in the deallocation table.
5839 DPRINTF (("Copying sref: %s", sRef_unparseFull(s)));
5840 ret = sRef_copy (s);
5841 DPRINTF (("Copying ===>: %s", sRef_unparseFull(ret)));
5846 ret = sRef_undefined;
5849 /*@-dependenttrans@*/
5851 /*@=dependenttrans@*/
5854 sRef sRef_copy (sRef s)
5856 if (sRef_isKindSpecial (s) && !sRef_isGlobalMarker (s))
5859 return s; /* don't copy specials (except for global markers) */
5863 if (sRef_isReasonable (s))
5865 sRef t = sRef_alloc ();
5867 DPRINTF (("Copying: [%p] %s", s, sRef_unparse (s)));
5868 DPRINTF (("Full: %s", sRef_unparseFull (s)));
5872 t->modified = s->modified;
5873 t->immut = FALSE; /* Note mutability is not copied. */
5875 t->val = multiVal_copy (s->val);
5877 t->info = sinfo_copy (s);
5878 t->defstate = s->defstate;
5879 t->nullstate = s->nullstate;
5881 /* start modifications */
5882 t->bufinfo.bufstate = s->bufinfo.bufstate;
5883 t->bufinfo.len = s->bufinfo.len;
5884 t->bufinfo.size = s->bufinfo.size;
5885 /* end modifications */
5887 t->aliaskind = s->aliaskind;
5888 t->oaliaskind = s->oaliaskind;
5890 t->expkind = s->expkind;
5891 t->oexpkind = s->oexpkind;
5893 t->nullinfo = stateInfo_copy (s->nullinfo);
5894 t->aliasinfo = stateInfo_copy (s->aliasinfo);
5895 t->definfo = stateInfo_copy (s->definfo);
5896 t->expinfo = stateInfo_copy (s->expinfo);
5898 t->deriv = sRefSet_newDeepCopy (s->deriv);
5899 t->state = valueTable_copy (s->state);
5901 DPRINTF (("Made copy: %s => %s", sRef_unparseFull (s), sRef_unparseFull (t)));
5906 return sRef_undefined;
5911 # define PREDTEST(func,s) \
5912 do { if (sRef_isInvalid (s)) { return FALSE; } \
5913 else { if (sRef_isConj (s)) \
5914 { return (func (sRef_getConjA (s)) \
5915 || func (sRef_getConjB (s))); }}} while (FALSE);
5917 bool sRef_isAddress (sRef s)
5919 PREDTEST (sRef_isAddress, s);
5920 return (s->kind == SK_ADR);
5924 ** pretty weak... maybe a flag should control this.
5927 bool sRef_isThroughArrayFetch (sRef s)
5929 if (sRef_isReasonable (s))
5937 if (sRef_isArrayFetch (tref))
5942 lt = sRef_getBase (tref);
5944 } while (sRef_isReasonable (tref));
5950 bool sRef_isArrayFetch (sRef s)
5952 PREDTEST (sRef_isArrayFetch, s);
5953 return (s->kind == SK_ARRAYFETCH);
5956 bool sRef_isMacroParamRef (sRef s)
5958 if (context_inMacro () && sRef_isCvar (s))
5960 uentry ue = sRef_getUentry (s);
5961 cstring pname = makeParam (uentry_rawName (ue));
5962 uentry mac = usymtab_lookupSafe (pname);
5964 cstring_free (pname);
5965 return (uentry_isValid (mac));
5971 bool sRef_isCvar (sRef s)
5973 PREDTEST (sRef_isCvar, s);
5974 return (s->kind == SK_CVAR);
5977 bool sRef_isConst (sRef s)
5979 PREDTEST (sRef_isConst, s);
5980 return (s->kind == SK_CONST);
5983 bool sRef_isObject (sRef s)
5985 PREDTEST (sRef_isObject, s);
5986 return (s->kind == SK_OBJECT);
5989 bool sRef_isExternal (sRef s)
5991 PREDTEST (sRef_isExternal, s);
5992 return (s->kind == SK_EXTERNAL);
5995 static bool sRef_isDerived (sRef s)
5997 PREDTEST (sRef_isDerived, s);
5998 return (s->kind == SK_DERIVED);
6001 bool sRef_isField (sRef s)
6003 PREDTEST (sRef_isField, s);
6004 return (s->kind == SK_FIELD);
6007 static bool sRef_isIndex (sRef s)
6009 PREDTEST (sRef_isIndex, s);
6010 return (s->kind == SK_ARRAYFETCH);
6013 bool sRef_isAnyParam (sRef s)
6015 PREDTEST (sRef_isAnyParam, s);
6016 return (s->kind == SK_PARAM);
6019 bool sRef_isParam (sRef s)
6021 PREDTEST (sRef_isParam, s);
6022 return (s->kind == SK_PARAM);
6025 bool sRef_isDirectParam (sRef s)
6027 PREDTEST (sRef_isDirectParam, s);
6029 return ((s->kind == SK_CVAR) &&
6030 (s->info->cvar->lexlevel == functionScope) &&
6031 (context_inFunction () &&
6032 (s->info->cvar->index <= uentryList_size (context_getParams ()))));
6035 bool sRef_isPointer (sRef s)
6037 PREDTEST (sRef_isPointer, s);
6038 return (s->kind == SK_PTR);
6042 ** returns true if storage referenced by s is visible
6045 bool sRef_isReference (sRef s)
6047 PREDTEST (sRef_isReference, s);
6049 return (sRef_isPointer (s) || sRef_isIndex (s) || sRef_isFileOrGlobalScope (s)
6050 || (sRef_isField (s) && (sRef_isReference (s->info->field->rec))));
6053 bool sRef_isIReference (sRef s)
6055 return (sRef_isPointer (s) || sRef_isAddress (s) || sRef_isIndex (s)
6056 || sRef_isField (s) || sRef_isArrayFetch (s));
6059 bool sRef_isFileOrGlobalScope (sRef s)
6061 return (sRef_isCvar (s) && (s->info->cvar->lexlevel <= fileScope));
6064 bool sRef_isRealGlobal (sRef s)
6066 return (sRef_isCvar (s) && (s->info->cvar->lexlevel == globScope));
6069 bool sRef_isFileStatic (sRef s)
6071 return (sRef_isCvar (s) && (s->info->cvar->lexlevel == fileScope));
6074 bool sRef_isAliasCheckedGlobal (sRef s)
6076 if (sRef_isFileOrGlobalScope (s))
6078 uentry ue = sRef_getUentry (s);
6080 return context_checkAliasGlob (ue);
6088 void sRef_free (/*@only@*/ sRef s)
6090 if (s != sRef_undefined && s->kind != SK_SPECIAL)
6092 DPRINTF (("Free sref: [%p]", s));
6094 sRef_checkValid (s);
6096 multiVal_free (s->val); /* evans 2002-07-12 */
6098 stateInfo_free (s->expinfo);
6099 stateInfo_free (s->aliasinfo);
6100 stateInfo_free (s->definfo);
6101 stateInfo_free (s->nullinfo);
6103 sRefSet_free (s->deriv);
6104 s->deriv = sRefSet_undefined;
6106 valueTable_free (s->state);
6110 /* drl added to help locate use after release*/
6111 s->expinfo = stateInfo_undefined;
6112 s->aliasinfo = stateInfo_undefined;
6113 s->definfo = stateInfo_undefined;
6114 s->nullinfo = stateInfo_undefined;
6116 /*@i32@*/ sfree (s);
6120 void sRef_setType (sRef s, ctype t)
6122 if (sRef_isReasonable (s))
6124 sRef_checkMutable (s);
6129 void sRef_setTypeFull (sRef s, ctype t)
6131 if (sRef_isReasonable (s))
6133 sRef_checkMutable (s);
6136 sRefSet_allElements (s->deriv, current)
6138 sRef_setTypeFull (current, ctype_unknown);
6139 } end_sRefSet_allElements ;
6144 sRef_buildField (/*@exposed@*/ sRef rec, /*@dependent@*/ cstring f)
6146 return (sRef_buildNCField (rec, f));
6149 static /*@exposed@*/ sRef
6150 sRef_findDerivedField (/*@notnull@*/ sRef rec, cstring f)
6152 sRefSet_allElements (sRef_derivedFields (rec), sr)
6154 if (sRef_isReasonable (sr))
6156 if (sRef_isReasonable (sr))
6158 if (sr->info != NULL)
6160 if (sr->kind == SK_FIELD && cstring_equal (sr->info->field->field, f))
6168 llcontbug (message ("Invalid sRef as derived field of %s", sRef_unparse (rec)));
6171 } end_sRefSet_allElements;
6173 return sRef_undefined;
6176 /*@dependent@*/ /*@observer@*/ sRefSet sRef_derivedFields (/*@temp@*/ sRef rec)
6178 if (sRef_isReasonable (rec))
6186 return (sRefSet_undefined);
6190 static /*@exposed@*/ sRef
6191 sRef_findDerivedPointer (sRef s)
6193 if (sRef_isReasonable (s))
6195 sRefSet_realElements (s->deriv, sr)
6197 if (sRef_isReasonable (sr) && sr->kind == SK_PTR)
6201 } end_sRefSet_realElements;
6204 return sRef_undefined;
6208 sRef_isUnknownArrayFetch (sRef s)
6210 return (sRef_isReasonable (s)
6211 && s->kind == SK_ARRAYFETCH
6212 && !s->info->arrayfetch->indknown);
6215 static /*@exposed@*/ sRef
6216 sRef_findDerivedArrayFetch (/*@notnull@*/ sRef s, bool isknown, int idx, bool dead)
6221 sRefSet_realElements (s->deriv, sr)
6223 if (sRef_isReasonable (sr)
6224 && sr->kind == SK_ARRAYFETCH
6225 && sr->info->arrayfetch->indknown
6226 && (sr->info->arrayfetch->ind == idx))
6230 } end_sRefSet_realElements;
6234 sRefSet_realElements (s->deriv, sr)
6236 if (sRef_isReasonable (sr)
6237 && sr->kind == SK_ARRAYFETCH
6238 && (!sr->info->arrayfetch->indknown
6239 || (sr->info->arrayfetch->indknown &&
6240 sr->info->arrayfetch->ind == 0)))
6242 if (sRef_isDead (sr) || sRef_isKept (sr))
6244 if (dead || context_getFlag (FLG_STRICTUSERELEASED))
6254 } end_sRefSet_realElements;
6257 return sRef_undefined;
6260 static /*@exposed@*/ sRef
6261 sRef_buildNCField (/*@exposed@*/ sRef rec, /*@exposed@*/ cstring f)
6265 DPRINTF (("Build nc field: %s / %s",
6266 sRef_unparseFull (rec), f));
6268 if (sRef_isInvalid (rec))
6270 return sRef_undefined;
6274 ** check if the field already has been referenced
6277 s = sRef_findDerivedField (rec, f);
6279 if (sRef_isReasonable (s))
6285 ctype ct = ctype_realType (rec->type);
6287 DPRINTF (("Field of: %s", sRef_unparse (rec)));
6291 s->info = (sinfo) dmalloc (sizeof (*s->info));
6292 s->info->field = (fldinfo) dmalloc (sizeof (*s->info->field));
6293 s->info->field->rec = rec; /* sRef_copy (rec); */ /*@i32@*/
6294 s->info->field->field = f; /* doesn't copy f */
6296 if (ctype_isKnown (ct) && ctype_isSU (ct))
6298 uentry ue = uentryList_lookupField (ctype_getFields (ct), f);
6300 if (!uentry_isUndefined (ue))
6302 DPRINTF (("lookup: %s for %s", uentry_unparseFull (ue),
6303 ctype_unparse (ct)));
6305 s->type = uentry_getType (ue);
6307 if (ctype_isMutable (s->type)
6308 && rec->aliaskind != AK_STACK
6309 && !alkind_isStatic (rec->aliaskind))
6311 s->aliaskind = rec->aliaskind;
6315 s->aliaskind = AK_UNKNOWN;
6318 if (sRef_isStateDefined (rec) || sRef_isStateUnknown (rec)
6319 || sRef_isPdefined (rec))
6321 sRef_setStateFromUentry (s, ue);
6325 sRef_setPartsFromUentry (s, ue);
6328 s->oaliaskind = s->aliaskind;
6329 s->oexpkind = s->expkind;
6331 DPRINTF (("sref: %s", sRef_unparseFull (s)));
6336 Never report this as an error. It can happen whenever there
6337 is casting involved.
6341 llcontbug (message ("buildNCField --- no field %s: %q / %s",
6342 f, sRef_unparse (s), ctype_unparse (ct)));
6346 return sRef_undefined;
6350 if (rec->defstate == SS_DEFINED
6351 && (s->defstate == SS_UNDEFINED || s->defstate == SS_UNKNOWN))
6353 s->defstate = SS_DEFINED;
6355 else if (rec->defstate == SS_PARTIAL)
6357 s->defstate = SS_PARTIAL;
6359 else if (rec->defstate == SS_ALLOCATED)
6361 if (ctype_isStackAllocated (ct) && ctype_isStackAllocated (s->type))
6363 s->defstate = SS_ALLOCATED;
6367 s->defstate = SS_UNDEFINED;
6370 else if (s->defstate == SS_UNKNOWN)
6372 s->defstate = rec->defstate;
6379 if (s->defstate == SS_UNDEFINED)
6381 ctype rt = ctype_realType (s->type);
6383 if (ctype_isArray (rt) || ctype_isSU (rt))
6385 s->defstate = SS_ALLOCATED;
6389 sRef_addDeriv (rec, s);
6390 DPRINTF (("Add deriv: %s", sRef_unparseFull (rec)));
6392 if (ctype_isInt (s->type) && cstring_equal (f, REFSNAME))
6394 s->aliaskind = AK_REFS;
6395 s->oaliaskind = AK_REFS;
6398 DPRINTF (("Build field ==> %s", sRef_unparseFull (s)));
6404 sRef_isStackAllocated (sRef s)
6406 return (sRef_isReasonable(s)
6407 && s->defstate == SS_ALLOCATED && ctype_isStackAllocated (s->type));
6411 void sRef_setArrayFetchState (/*@notnull@*/ /*@exposed@*/ sRef s,
6412 /*@notnull@*/ /*@exposed@*/ sRef arr)
6414 sRef_checkMutable (s);
6416 if (ctype_isRealAP (arr->type))
6418 s->type = ctype_baseArrayPtr (arr->type);
6421 /* a hack, methinks... makeArrayFetch (&a[0]) ==> a[] */
6422 /* evans - 2001-08-27: not sure where this was necessary - it
6423 ** causes an assertion in in aliasCheckPred to fail.
6426 if (sRef_isAddress (arr))
6428 sRef t = arr->info->ref;
6430 if (sRef_isArrayFetch (t))
6432 s->info->arrayfetch->arr = t->info->arrayfetch->arr;
6435 else if (ctype_isRealPointer (arr->type))
6437 sRef sp = sRef_findDerivedPointer (arr);
6439 if (sRef_isReasonable (sp))
6442 if (ctype_isMutable (s->type))
6444 s->expkind = sRef_getExKind (sp);
6445 s->expinfo = stateInfo_copy (sp->expinfo);
6447 s->aliaskind = sp->aliaskind;
6448 s->aliasinfo = stateInfo_copy (sp->aliasinfo);
6451 s->defstate = sp->defstate;
6453 if (s->defstate == SS_DEFINED)
6455 if (!context_getFlag (FLG_STRICTDESTROY))
6457 s->defstate = SS_PARTIAL;
6461 DPRINTF (("Set null state: %s / %s", sRef_unparseFull (s), sRef_unparseFull (sp)));
6462 sRef_setNullStateN (s, sRef_getNullState (sp));
6466 if (arr->defstate == SS_UNDEFINED)
6468 s->defstate = SS_UNUSEABLE;
6470 else if ((arr->defstate == SS_ALLOCATED) && !ctype_isSU (s->type))
6472 s->defstate = SS_UNDEFINED;
6476 if (!context_getFlag (FLG_STRICTDESTROY))
6478 s->defstate = SS_PARTIAL;
6482 s->defstate = SS_DEFINED;
6486 ** Very weak checking for array elements.
6488 ** s->defstate = arr->defstate;
6492 s->expkind = sRef_getExKind (arr);
6493 s->expinfo = stateInfo_copy (arr->expinfo);
6495 if (arr->aliaskind == AK_LOCAL || arr->aliaskind == AK_FRESH)
6497 s->aliaskind = AK_LOCAL;
6501 s->aliaskind = AK_UNKNOWN;
6504 sRef_setTypeState (s);
6509 if (arr->defstate == SS_DEFINED)
6512 ** Very weak checking for array elements.
6514 ** s->defstate = arr->defstate;
6517 if (context_getFlag (FLG_STRICTDESTROY))
6519 s->defstate = SS_DEFINED;
6523 s->defstate = SS_PARTIAL;
6526 else if (arr->defstate == SS_ALLOCATED)
6528 if (ctype_isRealArray (s->type))
6530 s->defstate = SS_ALLOCATED;
6534 if (!s->info->arrayfetch->indknown)
6537 ** is index is unknown, elements is defined or
6538 ** allocated is any element is!
6541 s->defstate = SS_UNDEFINED;
6543 sRefSet_allElements (arr->deriv, sr)
6545 if (sRef_isReasonable (sr))
6547 if (sRef_isReasonable (sr))
6549 if (sr->defstate == SS_ALLOCATED)
6551 s->defstate = SS_ALLOCATED;
6555 if (sr->defstate == SS_DEFINED)
6557 if (context_getFlag (FLG_STRICTDESTROY))
6559 s->defstate = SS_DEFINED;
6563 s->defstate = SS_PARTIAL;
6572 llcontbug (message ("Invalid sRef as derived element of %s", sRef_unparse (arr)));
6575 } end_sRefSet_allElements;
6579 s->defstate = SS_UNDEFINED;
6585 s->defstate = arr->defstate;
6590 ** kludgey way to guess where aliaskind applies
6593 if (ctype_isMutable (s->type)
6594 && !ctype_isPointer (arr->type)
6595 && !alkind_isStatic (arr->aliaskind)
6596 && !alkind_isStack (arr->aliaskind)) /* evs - 2000-06-20: don't pass stack allocation to members */
6598 s->aliaskind = arr->aliaskind;
6602 s->aliaskind = AK_UNKNOWN;
6605 sRef_setTypeState (s);
6608 if (sRef_isObserver (arr))
6610 s->expkind = XO_OBSERVER;
6611 s->expinfo = stateInfo_copy (arr->expinfo);
6615 /*@exposed@*/ sRef sRef_buildArrayFetch (/*@exposed@*/ sRef arr)
6619 if (!sRef_isReasonable (arr)) {
6620 /*@-nullret@*/ return arr /*@=nullret@*/;
6623 if (ctype_isRealPointer (arr->type))
6625 (void) sRef_buildPointer (arr); /* do this to define arr! */
6628 s = sRef_findDerivedArrayFetch (arr, FALSE, 0, FALSE);
6630 if (sRef_isReasonable (s))
6632 /* evans 2001-07-12: this is bogus, clean-up hack */
6633 if (s->info->arrayfetch->arr != arr)
6636 check (sRefSet_delete (arr->deriv, s));
6637 res = sRef_buildArrayFetch (arr);
6638 sRef_copyState (res, s);
6639 llassert (res->info->arrayfetch->arr == arr);
6643 s->expkind = sRef_getExKind (arr);
6644 s->expinfo = stateInfo_copy (arr->expinfo);
6652 s->kind = SK_ARRAYFETCH;
6653 s->info = (sinfo) dmalloc (sizeof (*s->info));
6654 s->info->arrayfetch = (ainfo) dmalloc (sizeof (*s->info->arrayfetch));
6655 s->info->arrayfetch->indknown = FALSE;
6656 s->info->arrayfetch->ind = 0;
6657 s->info->arrayfetch->arr = arr; /* sRef_copy (arr); */ /*@i32@*/
6659 sRef_setArrayFetchState (s, arr);
6661 s->oaliaskind = s->aliaskind;
6662 s->oexpkind = s->expkind;
6664 if (!context_inProtectVars ())
6666 sRef_addDeriv (arr, s);
6669 if (valueTable_isUndefined (s->state))
6671 s->state = context_createValueTable
6672 (s, stateInfo_makeLoc (g_currentloc, SA_CREATED));
6680 sRef_buildArrayFetchKnown (/*@exposed@*/ sRef arr, int i)
6684 if (!sRef_isReasonable (arr)) {
6685 /*@-nullret@*/ return arr /*@=nullret@*/;
6688 if (ctype_isRealPointer (arr->type))
6690 (void) sRef_buildPointer (arr); /* do this to define arr! */
6693 s = sRef_findDerivedArrayFetch (arr, TRUE, i, FALSE);
6695 if (sRef_isReasonable (s))
6697 /* evans 2001-07-12: this is bogus, clean-up hack */
6698 if (s->info->arrayfetch->arr != arr)
6702 check (sRefSet_delete (arr->deriv, s));
6703 res = sRef_buildArrayFetchKnown (arr, i);
6705 llassert (res->info->arrayfetch->arr == arr);
6706 sRef_copyState (res, s);
6707 llassert (res->info->arrayfetch->arr == arr);
6711 s->expkind = sRef_getExKind (arr);
6712 s->expinfo = stateInfo_copy (arr->expinfo);
6714 llassert (s->info->arrayfetch->arr == arr);
6721 s->kind = SK_ARRAYFETCH;
6722 s->info = (sinfo) dmalloc (sizeof (*s->info));
6723 s->info->arrayfetch = (ainfo) dmalloc (sizeof (*s->info->arrayfetch));
6724 s->info->arrayfetch->arr = arr; /* sRef_copy (arr); */ /*@i32@*/
6725 s->info->arrayfetch->indknown = TRUE;
6726 s->info->arrayfetch->ind = i;
6728 sRef_setArrayFetchState (s, arr);
6729 /* evans 2001-08-27 no: can change this - llassert (s->info->arrayfetch->arr == arr); */
6731 s->oaliaskind = s->aliaskind;
6732 s->oexpkind = s->expkind;
6733 sRef_addDeriv (arr, s);
6735 llassert (valueTable_isUndefined (s->state));
6736 s->state = context_createValueTable (s, stateInfo_makeLoc (g_currentloc, SA_CREATED));
6742 ** sets everything except for defstate
6746 sRef_setPartsFromUentry (sRef s, uentry ue)
6748 sRef uref = uentry_getSref (ue);
6750 llassert (sRef_isReasonable (s));
6752 s->aliaskind = alkind_derive (s->aliaskind, uentry_getAliasKind (ue));
6753 s->oaliaskind = s->aliaskind;
6755 if (s->expkind == XO_UNKNOWN)
6757 s->expkind = uentry_getExpKind (ue);
6760 s->oexpkind = s->expkind;
6762 if (sRef_getNullState (s) == NS_UNKNOWN)
6764 DPRINTF (("Set null state: %s / %s", sRef_unparseFull (s), uentry_unparseFull (ue)));
6765 sRef_setNullStateN (s, sRef_getNullState (uentry_getSref (ue)));
6769 DPRINTF (("Skipping null null state!"));
6772 if (s->aliaskind == AK_IMPONLY && (sRef_isExposed (s) || sRef_isObserver (s)))
6774 s->oaliaskind = s->aliaskind = AK_IMPDEPENDENT;
6777 if (sRef_isReasonable (uref))
6779 valueTable utable = uref->state;
6780 valueTable_free (s->state);
6781 s->state = valueTable_copy (utable);
6786 sRef_setStateFromAbstractUentry (sRef s, uentry ue)
6788 llassert (sRef_isReasonable (s));
6789 sRef_checkMutable (s);
6791 sRef_setPartsFromUentry (s, ue);
6793 s->aliaskind = alkind_derive (s->aliaskind, uentry_getAliasKind (ue));
6794 s->oaliaskind = s->aliaskind;
6796 if (s->expkind == XO_UNKNOWN)
6798 s->expkind = uentry_getExpKind (ue);
6801 s->oexpkind = s->expkind;
6805 sRef_setStateFromUentry (sRef s, uentry ue)
6809 sRef_checkMutable (s);
6810 llassert (sRef_isReasonable (s));
6812 sRef_setPartsFromUentry (s, ue);
6814 defstate = uentry_getDefState (ue);
6816 if (sstate_isKnown (defstate))
6818 s->defstate = defstate;
6827 sRef_buildPointer (/*@exposed@*/ sRef t)
6829 DPRINTF (("build pointer: %s", sRef_unparse (t)));
6831 if (sRef_isInvalid (t)) return sRef_undefined;
6833 if (sRef_isAddress (t))
6835 DPRINTF (("Return ref: %s", sRef_unparse (t->info->ref)));
6836 return (t->info->ref);
6840 sRef s = sRef_findDerivedPointer (t);
6842 DPRINTF (("find derived: %s", sRef_unparse (s)));
6844 if (sRef_isReasonable (s))
6846 s->expkind = sRef_getExKind (t);
6847 s->expinfo = stateInfo_copy (t->expinfo);
6849 s->oaliaskind = s->aliaskind;
6850 s->oexpkind = s->expkind;
6856 s = sRef_constructPointerAux (t);
6858 DPRINTF (("construct: %s", sRef_unparse (s)));
6860 if (sRef_isReasonable (s))
6862 sRef_addDeriv (t, s);
6864 s->oaliaskind = s->aliaskind;
6865 s->oexpkind = s->expkind;
6874 sRef_constructPointer (/*@exposed@*/ sRef t)
6877 return sRef_buildPointer (t);
6880 static /*@exposed@*/ sRef sRef_constructDerefAux (sRef t, bool isdead)
6882 if (sRef_isReasonable (t))
6887 ** if there is a derived t[?], return that. Otherwise, *t.
6890 s = sRef_findDerivedArrayFetch (t, FALSE, 0, isdead);
6892 if (sRef_isReasonable (s))
6894 DPRINTF (("Found array fetch: %s", sRef_unparseFull (s)));
6899 sRef ret = sRef_constructPointer (t);
6901 DPRINTF (("Constructed pointer: %s", sRef_unparseFull (ret)));
6908 return sRef_undefined;
6912 sRef sRef_constructDeref (sRef t)
6914 return sRef_constructDerefAux (t, FALSE);
6917 sRef sRef_constructDeadDeref (sRef t)
6919 return sRef_constructDerefAux (t, TRUE);
6923 sRef_constructPointerAux (/*@notnull@*/ /*@exposed@*/ sRef t)
6925 sRef s = sRef_newRef ();
6929 llassert (valueTable_isUndefined (s->state));
6932 s->info = (sinfo) dmalloc (sizeof (*s->info));
6933 s->info->ref = t; /* sRef_copy (t); */ /*@i32*/
6935 if (ctype_isRealAP (rt))
6937 s->type = ctype_baseArrayPtr (rt);
6940 st = ctype_realType (s->type);
6942 if (t->defstate == SS_UNDEFINED)
6944 s->defstate = SS_UNUSEABLE;
6945 s->definfo = stateInfo_copy (t->definfo);
6947 else if ((t->defstate == SS_ALLOCATED) && !ctype_isSU (st))
6949 s->defstate = SS_UNDEFINED;
6950 s->definfo = stateInfo_copy (t->definfo);
6954 s->defstate = t->defstate;
6955 s->definfo = stateInfo_copy (t->definfo);
6958 if (t->aliaskind == AK_LOCAL || t->aliaskind == AK_FRESH)
6960 s->aliaskind = AK_LOCAL;
6964 s->aliaskind = AK_UNKNOWN;
6967 s->expkind = sRef_getExKind (t);
6968 s->expinfo = stateInfo_copy (t->expinfo);
6970 sRef_setTypeState (s);
6972 s->oaliaskind = s->aliaskind;
6973 s->oexpkind = s->expkind;
6975 if (valueTable_isUndefined (s->state))
6977 s->state = context_createValueTable (s, stateInfo_makeLoc (g_currentloc, SA_CREATED));
6980 DPRINTF (("pointer: %s", sRef_unparseFull (s)));
6984 bool sRef_hasDerived (sRef s)
6986 return (sRef_isReasonable (s) && !sRefSet_isEmpty (s->deriv));
6990 sRef_clearDerived (sRef s)
6992 if (sRef_isReasonable (s))
6994 DPRINTF (("Clear derived: [%p] %s", s, sRef_unparseDebug (s)));
6995 sRefSet_clear (s->deriv);
7000 sRef_clearDerivedComplete (sRef s)
7002 if (sRef_isReasonable (s))
7004 sRef base = sRef_getBaseSafe (s);
7006 while (sRef_isReasonable (base))
7008 DPRINTF (("Clear derived: [%p] %s", base, sRef_unparse (base)));
7009 sRefSet_clear (base->deriv);
7010 base = sRef_getBaseSafe (base);
7013 DPRINTF (("Clear derived: [%p] %s", s, sRef_unparse (s)));
7014 sRefSet_clear (s->deriv);
7018 /*@exposed@*/ sRef sRef_makePointer (/*@exposed@*/ sRef s)
7021 sRef res = sRef_buildPointer (s);
7023 DPRINTF (("Res: %s", sRef_unparseFull (res)));
7028 ** &a[] => a (this is for out params)
7032 sRef_makeAnyArrayFetch (/*@exposed@*/ sRef arr)
7034 if (sRef_isAddress (arr))
7036 return (arr->info->ref);
7040 return (sRef_buildArrayFetch (arr));
7045 sRef_makeArrayFetch (/*@exposed@*/ sRef arr)
7047 return (sRef_buildArrayFetch (arr));
7051 sRef_makeArrayFetchKnown (/*@exposed@*/ sRef arr, int i)
7053 return (sRef_buildArrayFetchKnown (arr, i));
7057 sRef_makeField (sRef rec, /*@dependent@*/ cstring f)
7060 ret = sRef_buildField (rec, f);
7065 sRef_makeNCField (/*@exposed@*/ sRef rec, /*@dependent@*/ cstring f)
7067 return (sRef_buildNCField (rec, f));
7071 sRef_unparseKindName (sRef s)
7075 if (s == sRef_undefined) return cstring_makeLiteral ("<invalid>");
7077 s = sRef_fixConj (s);
7082 if (sRef_isLocalVar (s))
7084 result = cstring_makeLiteral ("Variable");
7088 result = cstring_makeLiteral ("Undef global");
7092 result = cstring_makeLiteral ("Out parameter");
7095 if (sRef_isAnyParam (s->info->arrayfetch->arr))
7097 result = cstring_makeLiteral ("Out parameter");
7099 else if (sRef_isIndexKnown (s))
7101 result = cstring_makeLiteral ("Array element");
7105 result = cstring_makeLiteral ("Value");
7109 if (sRef_isAnyParam (s->info->ref))
7111 result = cstring_makeLiteral ("Out parameter");
7115 result = cstring_makeLiteral ("Value");
7119 result = cstring_makeLiteral ("Value");
7122 result = cstring_makeLiteral ("Field");
7125 result = cstring_makeLiteral ("Object");
7127 case SK_UNCONSTRAINED:
7128 result = cstring_makeLiteral ("<anything>");
7137 result = cstring_makeLiteral ("<unknown>");
7140 result = cstring_makeLiteral ("<conj>");
7143 result = cstring_makeLiteral ("Storage");
7151 sRef_unparseKindNamePlain (sRef s)
7155 if (s == sRef_undefined) return cstring_makeLiteral ("<invalid>");
7157 s = sRef_fixConj (s);
7162 if (sRef_isLocalVar (s))
7164 result = cstring_makeLiteral ("Variable");
7168 result = cstring_makeLiteral ("Global");
7172 result = cstring_makeLiteral ("Parameter");
7175 if (sRef_isAnyParam (s->info->arrayfetch->arr))
7177 result = cstring_makeLiteral ("Parameter");
7179 else if (sRef_isIndexKnown (s))
7181 result = cstring_makeLiteral ("Array element");
7185 result = cstring_makeLiteral ("Value");
7189 if (sRef_isAnyParam (s->info->ref))
7191 result = cstring_makeLiteral ("Parameter");
7195 result = cstring_makeLiteral ("Value");
7199 result = cstring_makeLiteral ("Value");
7202 result = cstring_makeLiteral ("Field");
7205 result = cstring_makeLiteral ("Object");
7208 result = cstring_makeLiteral ("Storage");
7210 case SK_UNCONSTRAINED:
7211 result = cstring_makeLiteral ("<anything>");
7220 result = cstring_makeLiteral ("<unknown>");
7223 result = cstring_makeLiteral ("<conj>");
7235 sRef_copyState (sRef s1, sRef s2)
7237 if (sRef_isReasonable (s1) && sRef_isReasonable (s2))
7239 s1->defstate = s2->defstate;
7241 /* start modifications */
7242 s1->bufinfo.bufstate = s2->bufinfo.bufstate;
7243 s1->bufinfo.len = s2->bufinfo.len;
7244 s1->bufinfo.size = s2->bufinfo.size;
7245 /* end modifications */
7247 s1->aliaskind = s2->aliaskind;
7248 s1->aliasinfo = stateInfo_update (s1->aliasinfo, s2->aliasinfo);
7250 s1->expkind = s2->expkind;
7251 s1->expinfo = stateInfo_update (s1->expinfo, s2->expinfo);
7253 s1->nullstate = s2->nullstate;
7254 s1->nullinfo = stateInfo_update (s1->nullinfo, s2->nullinfo);
7257 /*@i834 don't free it: valueTable_free (s1->state); */
7258 /*@i32@*/ s1->state = valueTable_copy (s2->state);
7260 s1->safe = s2->safe;
7265 sRef_makeNew (ctype ct, sRef t, cstring name)
7267 sRef s = sRef_newRef ();
7272 llassert (sRef_isReasonable (t));
7273 s->defstate = t->defstate;
7275 s->aliaskind = t->aliaskind;
7276 s->oaliaskind = s->aliaskind;
7277 s->nullstate = t->nullstate;
7279 s->expkind = t->expkind;
7280 s->oexpkind = s->expkind;
7282 s->info = (sinfo) dmalloc (sizeof (*s->info));
7283 s->info->fname = name;
7285 /* start modifications */
7286 s->bufinfo.bufstate = t->bufinfo.bufstate;
7287 /* end modifications */
7289 llassert (valueTable_isUndefined (s->state));
7290 s->state = valueTable_copy (t->state);
7292 DPRINTF (("==> Copying state: %s", valueTable_unparse (s->state)));
7293 DPRINTF (("==> new: %s", sRef_unparseFull (s)));
7298 sRef_makeType (ctype ct)
7300 sRef s = sRef_newRef ();
7302 sRef_checkMutable (s);
7307 s->defstate = SS_UNKNOWN;
7308 s->aliaskind = AK_UNKNOWN;
7309 sRef_setNullStateN (s, NS_UNKNOWN);
7311 /* start modification */
7312 s->bufinfo.bufstate = BB_NOTNULLTERMINATED;
7313 /* end modification */
7316 if (ctype_isUA (ct))
7318 typeId uid = ctype_typeId (ct);
7319 uentry ue = usymtab_getTypeEntrySafe (uid);
7321 if (uentry_isValid (ue))
7323 sRef_mergeStateQuiet (s, uentry_getSref (ue));
7327 s->oaliaskind = s->aliaskind;
7328 s->oexpkind = s->expkind;
7329 llassert (valueTable_isUndefined (s->state));
7330 s->state = context_createValueTable (s, stateInfo_makeLoc (g_currentloc, SA_CREATED));
7332 DPRINTF (("Create: %s", sRef_unparseFull (s)));
7337 sRef_makeConst (ctype ct)
7339 sRef s = sRef_newRef ();
7344 s->defstate = SS_UNKNOWN;
7345 s->aliaskind = AK_UNKNOWN;
7346 sRef_setNullStateN (s, NS_UNKNOWN);
7348 /* start modification */
7349 s->bufinfo.bufstate = BB_NULLTERMINATED;
7350 /* end modification */
7352 /* evans 2002-04-22: added isManifestBool to avoid errors for -boolfalse initializations */
7353 if (!ctype_isManifestBool (ct) && ctype_isUA (ct))
7355 typeId uid = ctype_typeId (ct);
7356 uentry te = usymtab_getTypeEntrySafe (uid);
7358 if (uentry_isValid (te))
7360 sRef_mergeStateQuiet (s, uentry_getSref (te));
7364 s->oaliaskind = s->aliaskind;
7365 s->oexpkind = s->expkind;
7367 llassert (valueTable_isUndefined (s->state));
7368 s->state = context_createValueTable (s, stateInfo_makeLoc (g_currentloc, SA_CREATED));
7373 bool sRef_hasName (sRef s)
7375 if (sRef_isInvalid (s))
7384 uentry u = usymtab_getRefQuiet (s->info->cvar->lexlevel,
7385 s->info->cvar->index);
7386 return (uentry_hasName (u));
7390 if (s->info->paramno >= 0)
7392 uentry u = uentryList_getN (context_getParams (),
7395 return (uentry_hasName (u));
7399 llassert (s->info->paramno == PARAMUNKNOWN);
7409 sRef_sameName (sRef s1, sRef s2)
7411 if (sRef_isInvalid (s1))
7413 return sRef_isInvalid (s2);
7416 if (sRef_isInvalid (s2))
7424 if (s2->kind == SK_CVAR)
7426 return (s1->info->cvar->lexlevel == s2->info->cvar->lexlevel
7427 && s1->info->cvar->index == s2->info->cvar->index);
7429 else if (s2->kind == SK_PARAM)
7431 if (context_inFunctionLike ())
7433 if (s2->info->paramno != PARAMUNKNOWN)
7435 uentry u1 = usymtab_getRefQuiet (s1->info->cvar->lexlevel,
7436 s1->info->cvar->index);
7437 uentry u2 = uentryList_getN (context_getParams (),
7440 return (cstring_equalFree (uentry_getName (u1),
7441 uentry_getName (u2)));
7445 return s1->info->paramno == PARAMUNKNOWN;
7459 if (s2->kind == SK_PARAM)
7461 return (s1->info->paramno == s2->info->paramno);
7463 else if (s2->kind == SK_CVAR)
7465 if (context_inFunctionLike ())
7467 if (s1->info->paramno == PARAMUNKNOWN)
7473 uentry u1 = uentryList_getN (context_getParams (),
7475 uentry u2 = usymtab_getRefQuiet (s2->info->cvar->lexlevel,
7476 s2->info->cvar->index);
7479 return (cstring_equalFree (uentry_getName (u1),
7480 uentry_getName (u2)));
7494 case SK_UNCONSTRAINED:
7498 if (s2->kind == SK_ARRAYFETCH)
7500 if (bool_equal (s1->info->arrayfetch->indknown,
7501 s2->info->arrayfetch->indknown))
7503 if (!s1->info->arrayfetch->indknown
7504 || (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind))
7506 return sRef_sameName (s1->info->arrayfetch->arr,
7507 s2->info->arrayfetch->arr);
7514 if (s2->kind == SK_FIELD)
7516 if (cstring_equal (s1->info->field->field,
7517 s2->info->field->field))
7519 return sRef_sameName (s1->info->field->rec,
7520 s2->info->field->rec);
7529 if (s2->kind == s1->kind)
7531 return sRef_sameName (s1->info->ref,
7539 return sRef_sameName (sRef_getConjA (s1), s2);
7543 return (s2->kind == SK_UNKNOWN);
7546 if (s2->kind == s1->kind)
7548 return (ctype_equal (s1->type, s2->type));
7553 if (s2->kind == SK_SPECIAL)
7555 return (s1->info->spec == s2->info->spec);
7559 return (s2->kind == SK_RESULT);
7567 sRef_fixOuterRef (/*@returned@*/ sRef s)
7569 sRef root = sRef_getRootBase (s);
7571 if (sRef_isCvar (root))
7573 uentry ue = usymtab_getRefQuiet (root->info->cvar->lexlevel,
7574 root->info->cvar->index);
7576 if (uentry_isValid (ue))
7578 sRef uref = uentry_getSref (ue);
7579 sRef sr = sRef_fixBase (s, uref);
7585 llcontbug (message ("sRef_fixOuterRef: undefined: %q", sRef_unparseDebug (s)));
7594 sRef_storeState (sRef s)
7596 if (sRef_isInvalid (s)) return;
7598 sRef_checkMutable (s);
7599 s->oaliaskind = s->aliaskind;
7600 s->oexpkind = s->expkind;
7603 static void sRef_resetStateAux (sRef s, /*@unused@*/ fileloc loc)
7605 sRef_resetState (s);
7609 sRef_resetState (sRef s)
7611 bool changed = FALSE;
7612 if (sRef_isInvalid (s)) return;
7615 if (s->oaliaskind == AK_KILLREF && !sRef_isParam (s))
7618 ** killref is used in a kludgey way, to save having to add
7619 ** another alias kind (see usymtab_handleParams)
7622 if (s->expkind != s->oexpkind)
7625 s->expkind = s->oexpkind;
7630 if (s->expkind != s->oexpkind)
7633 s->expkind = s->oexpkind;
7636 if (s->aliaskind != s->oaliaskind
7637 && s->aliaskind != AK_REFCOUNTED
7638 && s->aliaskind != AK_REFS)
7641 s->aliaskind = s->oaliaskind;
7647 sRef_clearDerived (s);
7653 sRef_resetStateComplete (sRef s)
7655 sRef_innerAliasSetComplete (sRef_resetStateAux, s, fileloc_undefined);
7659 sRef_fixBase (/*@returned@*/ sRef s, /*@returned@*/ sRef base)
7661 sRef tmp = sRef_undefined;
7664 if (sRef_isInvalid (s)) return s;
7665 if (sRef_isInvalid (base)) return base;
7675 tmp = sRef_fixBase (s->info->arrayfetch->arr, base);
7677 if (s->info->arrayfetch->indknown)
7679 ret = sRef_makeArrayFetchKnown (tmp, s->info->arrayfetch->ind);
7683 ret = sRef_makeArrayFetch (tmp);
7687 tmp = sRef_fixBase (s->info->field->rec, base);
7688 ret = sRef_buildNCField (tmp, s->info->field->field);
7691 tmp = sRef_fixBase (s->info->ref, base);
7692 ret = sRef_makePointer (tmp);
7695 tmp = sRef_fixBase (s->info->ref, base);
7696 ret = sRef_makeAddress (tmp);
7702 tmp = sRef_fixBase (s->info->conj->a, base);
7703 tmpb = sRef_fixBase (s->info->conj->b, base);
7705 ret = sRef_makeConj (tmp, tmpb);
7714 static /*@exposed@*/ sRef
7715 sRef_fixDirectBase (sRef s, sRef base)
7720 if (sRef_isInvalid (s))
7722 return sRef_undefined;
7728 if (s->info->arrayfetch->indknown)
7730 ret = sRef_makeArrayFetchKnown (base, s->info->arrayfetch->ind);
7734 ret = sRef_makeArrayFetch (base);
7738 ret = sRef_buildNCField (base, s->info->field->field);
7741 ret = sRef_makePointer (base);
7744 ret = sRef_makeAddress (base);
7750 tmpa = sRef_fixDirectBase (s->info->conj->a, base);
7751 tmpb = sRef_fixDirectBase (s->info->conj->b, base);
7753 ret = sRef_makeConj (tmpa, tmpb);
7759 sRef_copyState (ret, s);
7764 sRef_isAllocIndexRef (sRef s)
7766 return (sRef_isArrayFetch (s) && !(s->info->arrayfetch->indknown)
7767 && sRef_isAllocated (s->info->arrayfetch->arr));
7771 sRef_showRefLost (sRef s)
7773 if (sRef_hasAliasInfoLoc (s))
7775 llgenindentmsg (cstring_makeLiteral ("Original reference lost"),
7776 sRef_getAliasInfoLoc (s));
7781 sRef_showRefKilled (sRef s)
7783 if (sRef_isValid (s))
7785 DPRINTF (("Killed: %s", sRef_unparseFull (s)));
7786 if (context_getLocIndentSpaces () == 0) {
7787 stateInfo_display (s->definfo, message (" Storage %q", sRef_unparseOpt (s)));
7789 stateInfo_display (s->definfo, message ("Storage %q", sRef_unparseOpt (s)));
7795 sRef_showStateInconsistent (sRef s)
7797 if (sRef_hasStateInfoLoc (s))
7800 (message ("Storage %qbecomes inconsistent (released on one branch)",
7801 sRef_unparseOpt (s)),
7802 sRef_getStateInfoLoc (s));
7807 sRef_showStateInfo (sRef s)
7809 if (sRef_isValid (s)) {
7810 if (context_getLocIndentSpaces () == 0) {
7811 stateInfo_display (s->definfo, message (" Storage %q", sRef_unparseOpt (s)));
7813 stateInfo_display (s->definfo, message ("Storage %q", sRef_unparseOpt (s)));
7819 sRef_showExpInfo (sRef s)
7821 if (sRef_isValid (s)) {
7822 if (context_getLocIndentSpaces () == 0) {
7823 stateInfo_display (s->expinfo, message (" Storage %q", sRef_unparseOpt (s)));
7825 stateInfo_display (s->expinfo, message ("Storage %q", sRef_unparseOpt (s)));
7831 sRef_showMetaStateInfo (sRef s, cstring key)
7834 metaStateInfo minfo = context_lookupMetaStateInfo (key);
7836 llassert (sRef_isReasonable (s));
7837 llassert (valueTable_isDefined (s->state));
7838 llassert (metaStateInfo_isDefined (minfo));
7840 val = valueTable_lookup (s->state, key);
7842 if (stateValue_hasLoc (val))
7845 (message ("%qbecomes %q", sRef_unparseOpt (s),
7846 stateValue_unparseValue (val, minfo)),
7847 stateValue_getLoc (val));
7852 sRef_showNullInfo (sRef s)
7854 DPRINTF (("Show null info: %s", sRef_unparseFull (s)));
7856 if (sRef_hasNullInfoLoc (s) && sRef_isKnown (s))
7858 DPRINTF (("has null info: %s",
7859 fileloc_unparse (sRef_getNullInfoLoc (s))));
7861 switch (sRef_getNullState (s))
7865 fileloc loc = sRef_getNullInfoLoc (s);
7867 if (fileloc_isDefined (loc) && !fileloc_isLib (loc))
7870 (message ("Storage %qbecomes null", sRef_unparseOpt (s)),
7877 fileloc loc = sRef_getNullInfoLoc (s);
7879 if (fileloc_isDefined (loc) && !fileloc_isLib (loc))
7881 llgenindentmsg (message ("Storage %qbecomes null", sRef_unparseOpt (s)),
7889 (message ("Storage %qmay become null", sRef_unparseOpt (s)),
7890 sRef_getNullInfoLoc (s));
7895 (message ("Storage %qbecomes not null", sRef_unparseOpt (s)),
7896 sRef_getNullInfoLoc (s));
7900 (message ("Storage %qnull state becomes unknown",
7901 sRef_unparseOpt (s)),
7902 sRef_getNullInfoLoc (s));
7911 (message ("<error case> Storage %q becomes %s",
7913 nstate_unparse (sRef_getNullState (s))),
7914 sRef_getNullInfoLoc (s));
7922 sRef_showAliasInfo (sRef s)
7924 if (sRef_isValid (s))
7926 if (sRef_isFresh (s))
7928 if (context_getLocIndentSpaces () == 0) {
7929 stateInfo_display (s->aliasinfo, message (" Fresh storage %q", sRef_unparseOpt (s)));
7931 stateInfo_display (s->aliasinfo, message ("Fresh storage %q", sRef_unparseOpt (s)));
7936 if (context_getLocIndentSpaces () == 0) {
7937 stateInfo_display (s->aliasinfo, message (" Storage %q", sRef_unparseOpt (s)));
7939 stateInfo_display (s->aliasinfo, message ("Storage %q", sRef_unparseOpt (s)));
7946 sRef_mergeNullState (sRef s, nstate n)
7948 if (sRef_isReasonable (s))
7952 old = sRef_getNullState (s);
7954 if (n != old && n != NS_UNKNOWN)
7956 sRef_setNullState (s, n, g_currentloc);
7961 llbuglit ("sRef_mergeNullState: invalid");
7966 sRef_possiblyNull (sRef s)
7968 if (sRef_isReasonable (s))
7970 if (sRef_getNullState (s) == NS_ABSNULL)
7972 ctype rct = ctype_realType (s->type);
7974 if (ctype_isAbstract (rct))
7980 if (ctype_isUser (rct))
7982 uentry ue = usymtab_getTypeEntry (ctype_typeId (rct));
7984 return (nstate_possiblyNull
7985 (sRef_getNullState (uentry_getSref (ue))));
7995 return nstate_possiblyNull (sRef_getNullState (s));
8003 sRef_getScopeName (sRef s)
8005 sRef base = sRef_getRootBase (s);
8007 if (sRef_isRealGlobal (base))
8009 return (cstring_makeLiteralTemp ("Global"));
8011 else if (sRef_isFileStatic (base))
8013 return (cstring_makeLiteralTemp ("Static"));
8017 return (cstring_makeLiteralTemp ("Local"));
8022 sRef_unparseScope (sRef s)
8024 sRef base = sRef_getRootBase (s);
8026 if (sRef_isRealGlobal (base))
8028 return (cstring_makeLiteralTemp ("global"));
8030 else if (sRef_isFileStatic (base))
8032 return (cstring_makeLiteralTemp ("file static"));
8041 sRef_getScope (sRef s)
8043 llassert (sRef_isReasonable (s));
8045 if (sRef_isCvar (s))
8047 return s->info->cvar->lexlevel;
8049 else if (sRef_isParam (s))
8060 sRef_isDead (sRef s)
8062 return (sRef_isReasonable (s) && (s)->defstate == SS_DEAD);
8066 sRef_isDeadStorage (sRef s)
8068 if (sRef_isReasonable (s))
8070 if (s->defstate == SS_DEAD
8071 || s->defstate == SS_UNUSEABLE
8072 || s->defstate == SS_UNDEFINED
8073 || s->defstate == SS_UNKNOWN)
8079 return (sRef_isDefinitelyNull (s));
8089 sRef_isPossiblyDead (sRef s)
8091 return (sRef_isReasonable (s) && s->defstate == SS_HOFFA);
8094 bool sRef_isStateLive (sRef s)
8096 if (sRef_isReasonable (s))
8098 sstate ds = s->defstate;
8100 return (!(ds == SS_UNDEFINED
8102 || ds == SS_UNUSEABLE
8103 || ds == SS_HOFFA));
8112 bool sRef_isStateUndefined (sRef s)
8114 return ((sRef_isReasonable(s)) && ((s)->defstate == SS_UNDEFINED));
8117 bool sRef_isJustAllocated (sRef s)
8119 if (sRef_isAllocated (s))
8121 sRefSet_allElements (s->deriv, el)
8123 if (!(sRef_isStateUndefined (el) || sRef_isUnuseable (el)))
8127 } end_sRefSet_allElements ;
8136 sRef_isAllocatedStorage (sRef s)
8138 if (sRef_isReasonable (s) && ynm_toBoolStrict (sRef_isValidLvalue (s)))
8140 return (ctype_isVisiblySharable (sRef_getType (s)));
8149 sRef_isUnuseable (sRef s)
8151 return ((sRef_isValid(s)) && ((s)->defstate == SS_UNUSEABLE));
8155 sRef_perhapsNull (sRef s)
8157 if (sRef_isReasonable (s))
8159 if (sRef_getNullState (s) == NS_ABSNULL)
8161 ctype rct = ctype_realType (s->type);
8163 if (ctype_isAbstract (rct))
8169 if (ctype_isUser (rct))
8171 uentry ue = usymtab_getTypeEntry (ctype_typeId (rct));
8173 return (nstate_perhapsNull
8174 (sRef_getNullState (uentry_getSref (ue))));
8184 return nstate_perhapsNull (sRef_getNullState (s));
8192 ** definitelyNull --- called when TRUE is good
8196 sRef_definitelyNull (sRef s)
8198 return (sRef_isReasonable (s)
8199 && (sRef_getNullState (s) == NS_DEFNULL || sRef_getNullState (s) == NS_CONSTNULL));
8203 ** based on sRef_similar
8207 sRef_setDerivNullState (sRef set, sRef guide, nstate ns)
8209 if (sRef_isReasonable (set))
8211 sRef deriv = sRef_getDeriv (set, guide);
8213 if (sRef_isReasonable (deriv))
8215 sRef_setNullStateN (deriv, ns);
8220 static /*@exposed@*/ sRef
8221 sRef_getDeriv (/*@returned@*/ /*@notnull@*/ sRef set, sRef guide)
8223 llassert (sRef_isReasonable (set));
8224 llassert (sRef_isReasonable (guide));
8226 switch (guide->kind)
8229 llassert (set->kind == SK_CVAR);
8233 llassert (set->kind == guide->kind);
8234 llassert (set->info->paramno == guide->info->paramno);
8239 if (set->kind == SK_ARRAYFETCH
8240 && (sRef_similar (set->info->arrayfetch->arr,
8241 guide->info->arrayfetch->arr)))
8247 return (sRef_makeAnyArrayFetch
8248 (sRef_getDeriv (set, guide->info->arrayfetch->arr)));
8253 if (set->kind == SK_PTR && sRef_similar (set->info->ref, guide->info->ref))
8259 return (sRef_makePointer (sRef_getDeriv (set, guide->info->ref)));
8264 if ((set->kind == SK_FIELD &&
8265 (sRef_similar (set->info->field->rec, guide->info->field->rec) &&
8266 cstring_equal (set->info->field->field, guide->info->field->field))))
8272 return (sRef_makeField (sRef_getDeriv (set, guide->info->field->rec),
8273 guide->info->field->field));
8277 if ((set->kind == SK_ADR) && sRef_similar (set->info->ref, guide->info->ref))
8283 return (sRef_makeAddress (sRef_getDeriv (set, guide->info->ref)));
8288 return sRef_undefined;
8292 case SK_UNCONSTRAINED:
8300 return sRef_undefined;
8307 ** sRef_aliasCheckPred
8309 ** A confusing but spiffy function:
8311 ** Calls predf (s, e, text, <alias>) on s and all of s's aliases
8312 ** (unless checkAliases (s) is FALSE).
8314 ** For alias calls, calls as
8315 ** predf (alias, e, text, s)
8319 sRef_aliasCheckPred (bool (predf) (sRef, exprNode, sRef, exprNode),
8320 /*@null@*/ bool (checkAliases) (sRef),
8321 sRef s, exprNode e, exprNode err)
8323 bool error = (*predf)(s, e, sRef_undefined, err);
8326 if (checkAliases != NULL && !(checkAliases (s)))
8328 /* don't check aliases */
8332 sRefSet aliases = usymtab_allAliases (s);
8334 sRefSet_realElements (aliases, current)
8336 if (sRef_isReasonable (current))
8338 if (sRef_isReasonable (current))
8340 if (!sRef_similar (current, s)
8341 || (error && sRef_sameName (current, s)))
8343 (void) (*predf)(current, e, s, err);
8348 llcontbug (message ("Invalid sRef as alias field of %s", sRef_unparse (s)));
8351 } end_sRefSet_realElements;
8353 sRefSet_free (aliases);
8358 ** return TRUE iff predf (s) is true for s or any alias of s
8362 sRef_aliasCheckSimplePred (sRefTest predf, sRef s)
8373 aliases = usymtab_allAliases (s);
8375 sRefSet_realElements (aliases, current)
8377 if (sRef_isReasonable (current))
8379 sRef cref = sRef_updateSref (current);
8381 /* Whoa! a very kludgey way to make sure the right sref is used
8382 ** where there is a conditional symbol table. I am beginning
8383 ** to think that having a conditional symbol table wasn't such
8389 DPRINTF (("Checking alias: %s", sRef_unparseFull (cref)));
8390 sRefSet_free (aliases);
8394 } end_sRefSet_realElements;
8396 sRefSet_free (aliases);
8402 sRef_aliasCompleteSimplePred (bool (predf) (sRef), sRef s)
8405 bool result = FALSE;
8408 aliases = usymtab_allAliases (s);
8410 if ((*predf)(s)) result = TRUE;
8413 sRefSet_realElements (aliases, current)
8415 if (sRef_isReasonable (current))
8417 current = sRef_updateSref (current);
8418 if ((*predf)(current)) result = TRUE;
8420 } end_sRefSet_realElements;
8422 sRefSet_free (aliases);
8427 sRef_aliasSetComplete (void (predf) (sRef, fileloc), sRef s, fileloc loc)
8431 aliases = usymtab_allAliases (s);
8433 DPRINTF (("All aliases: %s", sRefSet_unparseFull (aliases)));
8437 sRefSet_realElements (aliases, current)
8439 if (sRef_isReasonable (current))
8441 current = sRef_updateSref (current);
8442 ((*predf)(current, loc));
8443 DPRINTF (("Killed: %s", sRef_unparseFull (current)));
8445 } end_sRefSet_realElements;
8447 sRefSet_free (aliases);
8451 sRef_aliasSetCompleteParam (void (predf) (sRef, int, fileloc), sRef s,
8452 int kind, fileloc loc)
8456 if (sRef_isDeep (s))
8458 aliases = usymtab_allAliases (s);
8462 aliases = usymtab_aliasedBy (s);
8465 (*predf)(s, kind, loc);
8467 sRefSet_realElements (aliases, current)
8469 if (sRef_isReasonable (current))
8471 current = sRef_updateSref (current);
8472 ((*predf)(current, kind, loc));
8474 } end_sRefSet_realElements;
8476 sRefSet_free (aliases);
8480 ** Version of aliasSetCompleteParam for alkind parameters
8484 sRef_aliasSetCompleteAlkParam (void (predf) (sRef, alkind, fileloc), sRef s,
8485 alkind kind, fileloc loc)
8489 if (sRef_isDeep (s))
8491 aliases = usymtab_allAliases (s);
8495 aliases = usymtab_aliasedBy (s);
8498 (*predf)(s, kind, loc);
8500 sRefSet_realElements (aliases, current)
8502 if (sRef_isReasonable (current))
8504 current = sRef_updateSref (current);
8505 ((*predf)(current, kind, loc));
8507 } end_sRefSet_realElements;
8509 sRefSet_free (aliases);
8513 sRef_innerAliasSetComplete (void (predf) (sRef, fileloc), sRef s, fileloc loc)
8519 if (!sRef_isReasonable (s)) return;
8522 ** Type equivalence checking is necessary --- there might be casting.
8529 case SK_UNCONSTRAINED:
8534 inner = s->info->ref;
8535 aliases = usymtab_allAliases (inner);
8536 ct = sRef_getType (inner);
8538 sRefSet_realElements (aliases, current)
8540 if (sRef_isReasonable (current))
8542 current = sRef_updateSref (current);
8544 if (ctype_equal (ct, sRef_getType (current)))
8546 sRef ptr = sRef_makePointer (current);
8547 ((*predf)(ptr, loc));
8550 } end_sRefSet_realElements;
8552 sRefSet_free (aliases);
8555 inner = s->info->arrayfetch->arr;
8556 aliases = usymtab_allAliases (inner);
8557 ct = sRef_getType (inner);
8559 DPRINTF (("Array fetch: %s", sRefSet_unparse (aliases)));
8561 sRefSet_realElements (aliases, current)
8563 if (sRef_isReasonable (current))
8565 current = sRef_updateSref (current);
8566 DPRINTF (("Current: %s", sRef_unparseFull (current)));
8568 if (ctype_equal (ct, sRef_getType (current)))
8570 if (s->info->arrayfetch->indknown)
8572 sRef af = sRef_makeArrayFetchKnown (current, s->info->arrayfetch->ind);
8573 DPRINTF (("Defining: %s", sRef_unparseFull (af)));
8574 /* evans 2001-08-27 This isn't true:
8575 llassert (af->info->arrayfetch->arr == current);
8576 see comments in buildArrayFetchKnown
8578 ((*predf)(af, loc));
8582 sRef af = sRef_makeArrayFetch (current);
8583 /* evans 2001-08-27 This isn't true:
8584 llassert (af->info->arrayfetch->arr == current);
8585 see comments in buildArrayFetch
8587 DPRINTF (("Defining: %s", sRef_unparseFull (af)));
8588 ((*predf)(af, loc));
8593 DPRINTF (("Type mismatch: %s / %s",
8595 ctype_unparse (sRef_getType (current))));
8598 } end_sRefSet_realElements;
8600 sRefSet_free (aliases);
8603 inner = s->info->field->rec;
8604 aliases = usymtab_allAliases (inner);
8605 ct = sRef_getType (inner);
8607 sRefSet_realElements (aliases, current)
8609 if (sRef_isReasonable (current))
8611 current = sRef_updateSref (current);
8613 if (ctype_equal (ct, sRef_getType (current)))
8615 sRef f = sRef_makeField (current, s->info->field->field);
8620 } end_sRefSet_realElements;
8622 sRefSet_free (aliases);
8625 sRef_innerAliasSetComplete (predf, s->info->conj->a, loc);
8626 sRef_innerAliasSetComplete (predf, s->info->conj->b, loc);
8643 sRef_innerAliasSetCompleteParam (void (predf) (sRef, sRef), sRef s, sRef t)
8649 if (!sRef_isReasonable (s)) return;
8652 ** Type equivalence checking is necessary --- there might be casting.
8659 case SK_UNCONSTRAINED:
8664 inner = s->info->ref;
8665 aliases = usymtab_allAliases (inner);
8666 ct = sRef_getType (inner);
8668 sRefSet_realElements (aliases, current)
8670 if (sRef_isReasonable (current))
8672 current = sRef_updateSref (current);
8674 if (ctype_equal (ct, sRef_getType (current)))
8676 sRef ptr = sRef_makePointer (current);
8681 } end_sRefSet_realElements;
8683 sRefSet_free (aliases);
8686 inner = s->info->arrayfetch->arr;
8687 aliases = usymtab_allAliases (inner);
8688 ct = sRef_getType (inner);
8690 sRefSet_realElements (aliases, current)
8692 if (sRef_isReasonable (current))
8694 current = sRef_updateSref (current);
8696 if (ctype_equal (ct, sRef_getType (current)))
8699 if (s->info->arrayfetch->indknown)
8701 sRef af = sRef_makeArrayFetchKnown (current, s->info->arrayfetch->ind);
8707 sRef af = sRef_makeArrayFetch (current);
8713 } end_sRefSet_realElements;
8715 sRefSet_free (aliases);
8718 inner = s->info->field->rec;
8719 aliases = usymtab_allAliases (inner);
8720 ct = sRef_getType (inner);
8723 sRefSet_realElements (aliases, current)
8725 if (sRef_isReasonable (current))
8727 current = sRef_updateSref (current);
8729 if (ctype_equal (ct, sRef_getType (current)))
8731 sRef f = sRef_makeField (current, s->info->field->field);
8736 } end_sRefSet_realElements;
8738 sRefSet_free (aliases);
8741 sRef_innerAliasSetCompleteParam (predf, s->info->conj->a, t);
8742 sRef_innerAliasSetCompleteParam (predf, s->info->conj->b, t);
8758 static void sRef_combineExKinds (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other)
8760 exkind a1 = sRef_getExKind (res);
8761 exkind a2 = sRef_getExKind (other);
8763 if (a1 == a2 || a2 == XO_UNKNOWN)
8767 else if (a1 == XO_UNKNOWN)
8769 res->expinfo = stateInfo_update (res->expinfo, other->expinfo);
8774 res->expkind = XO_OBSERVER;
8779 ** Currently, this is a very ad hoc implementation, with lots of fixes to
8780 ** make real code work okay. I need to come up with some more general
8781 ** rules or principles here.
8785 sRef_combineAliasKindsError (/*@notnull@*/ sRef res,
8786 /*@notnull@*/ sRef other,
8787 clause cl, fileloc loc)
8789 bool hasError = FALSE;
8790 alkind ares = sRef_getAliasKind (res);
8791 alkind aother = sRef_getAliasKind (other);
8793 sRef_checkMutable (res);
8795 if (alkind_isDependent (ares))
8797 if (aother == AK_KEPT)
8799 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8800 res->aliaskind = AK_KEPT;
8804 if (aother == AK_LOCAL || aother == AK_STATIC
8805 || alkind_isTemp (aother))
8807 DPRINTF (("Setting dependent: %s", sRef_unparseFull (res)));
8808 res->aliaskind = AK_DEPENDENT;
8812 else if (alkind_isDependent (aother))
8814 if (ares == AK_KEPT)
8816 res->aliaskind = AK_KEPT;
8820 if (ares == AK_LOCAL || ares == AK_STATIC || alkind_isTemp (ares))
8822 DPRINTF (("Setting dependent: %s", sRef_unparseFull (res)));
8823 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8824 res->aliaskind = AK_DEPENDENT;
8828 else if ((ares == AK_LOCAL || ares == AK_UNIQUE
8829 || ares == AK_STATIC || alkind_isTemp (ares))
8830 && sRef_isFresh (other))
8833 ** cases like: if (s == NULL) s = malloc...;
8834 ** don't generate errors
8837 if (usymtab_isAltDefinitelyNullDeep (res))
8839 res->aliaskind = ares;
8846 else if ((aother == AK_LOCAL || aother == AK_UNIQUE
8847 || aother == AK_STATIC || alkind_isTemp (aother))
8848 && sRef_isFresh (res))
8851 ** cases like: if (s == NULL) s = malloc...;
8852 ** don't generate errors
8855 if (usymtab_isDefinitelyNullDeep (other))
8857 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8858 res->aliaskind = aother;
8865 else if (ares == AK_NEWREF && aother == AK_REFCOUNTED
8866 && sRef_isConst (other))
8868 res->aliaskind = AK_NEWREF;
8870 else if (aother == AK_NEWREF && ares == AK_REFCOUNTED
8871 && sRef_isConst (res))
8873 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8874 res->aliaskind = AK_NEWREF;
8876 else if (sRef_isLocalVar (res)
8877 && ((ares == AK_KEPT && aother == AK_LOCAL)
8878 || (aother == AK_KEPT && ares == AK_LOCAL)))
8880 res->aliaskind = AK_KEPT;
8882 else if ((ares == AK_OWNED && aother == AK_ONLY)
8883 || (aother == AK_OWNED && ares == AK_ONLY))
8885 res->aliaskind = AK_OWNED;
8887 if (aother == AK_OWNED)
8889 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8899 if (sRef_isThroughArrayFetch (res))
8902 (FLG_BRANCHSTATE, FLG_STRICTBRANCHSTATE,
8904 ("Clauses exit with %q possibly referencing %s storage %s, "
8907 alkind_unparse (aother),
8908 clause_nameTaken (cl),
8909 alkind_unparse (ares),
8910 clause_nameAlternate (cl)),
8913 sRef_showAliasInfo (res);
8914 sRef_showAliasInfo (other);
8915 res->aliaskind = AK_ERROR;
8919 if (ares == AK_KEPT || aother == AK_KEPT)
8921 sRef_maybeKill (res, loc);
8929 message ("Clauses exit with %q referencing %s storage %s, "
8932 alkind_unparse (aother),
8933 clause_nameTaken (cl),
8934 alkind_unparse (ares),
8935 clause_nameAlternate (cl)),
8938 sRef_showAliasInfo (res);
8939 sRef_showAliasInfo (other);
8941 res->aliaskind = AK_ERROR;
8945 res->aliaskind = (sRef_isLocalVar (res) ? AK_LOCAL : AK_UNKNOWN);
8950 sRef_combineAliasKinds (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other,
8951 clause cl, fileloc loc)
8953 alkind ares = sRef_getAliasKind (res);
8954 alkind aother = sRef_getAliasKind (other);
8956 sRef_checkMutable (res);
8958 DPRINTF (("Combine alias kinds: \n\t%s / \n\t%s",
8959 sRef_unparseFull (res), sRef_unparseFull (other)));
8960 if (alkind_equal (ares, aother)
8961 || aother == AK_UNKNOWN
8962 || aother == AK_ERROR)
8964 ; /* keep current state */
8966 else if (sRef_isDead (res) || sRef_isDead (other))
8968 /* dead error reported (or storage is dead) */
8969 res ->aliaskind = AK_ERROR;
8971 else if (ares == AK_UNKNOWN || ares == AK_ERROR
8972 || sRef_isStateUndefined (res)
8973 || sRef_isDefinitelyNull (res))
8975 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8976 res->aliaskind = aother;
8978 else if (sRef_isStateUndefined (other)
8979 || sRef_isDefinitelyNull (other))
8983 else if (((ares == AK_UNIQUE || alkind_isTemp (ares))
8984 && aother == AK_LOCAL)
8985 || ((aother == AK_UNIQUE || alkind_isTemp (aother))
8986 && ares == AK_LOCAL))
8988 if (ares != AK_LOCAL)
8990 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8993 res->aliaskind = AK_LOCAL;
8995 else if ((ares == AK_OWNED && aother == AK_FRESH)
8996 || (aother == AK_OWNED && ares == AK_FRESH))
8998 if (ares != AK_FRESH)
9000 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
9003 res->aliaskind = AK_FRESH;
9005 else if ((ares == AK_KEEP && aother == AK_FRESH) ||
9006 (aother == AK_KEEP && ares == AK_FRESH))
9008 if (ares != AK_KEEP)
9010 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
9013 res->aliaskind = AK_KEEP;
9015 else if ((ares == AK_LOCAL && aother == AK_STACK) ||
9016 (aother == AK_LOCAL && ares == AK_STACK))
9018 if (ares != AK_STACK)
9020 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
9023 res->aliaskind = AK_STACK;
9025 else if ((ares == AK_LOCAL
9026 && (aother == AK_OWNED && sRef_isLocalVar (other)))
9027 || (aother == AK_LOCAL
9028 && (ares == AK_OWNED && sRef_isLocalVar (res))))
9030 if (ares != AK_LOCAL)
9032 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
9035 res->aliaskind = AK_LOCAL;
9037 else if ((ares == AK_FRESH && alkind_isOnly (aother))
9038 || (aother == AK_FRESH && alkind_isOnly (ares)))
9040 res->aliaskind = AK_FRESH;
9042 else if ((aother == AK_FRESH && sRef_definitelyNull (res))
9043 || (ares == AK_FRESH && sRef_definitelyNull (other)))
9045 if (ares != AK_FRESH)
9047 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
9048 res->aliaskind = AK_FRESH;
9051 else if ((sRef_isFresh (res) && sRef_isConst (other))
9052 || (sRef_isFresh (other) && sRef_isConst (res)))
9055 ** for NULL constantants
9059 if (!sRef_isFresh (res))
9061 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
9064 res->aliaskind = AK_FRESH;
9066 else if ((alkind_isStatic (aother) && sRef_isConst (res))
9067 || (alkind_isStatic (ares) && sRef_isConst (other)))
9069 if (!alkind_isStatic (ares))
9071 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
9072 res->aliaskind = AK_STATIC;
9077 sRef_combineAliasKindsError (res, other, cl, loc);
9081 static void sRef_combineDefState (/*@notnull@*/ sRef res,
9082 /*@notnull@*/ sRef other)
9084 sstate s1 = res->defstate;
9085 sstate s2 = other->defstate;
9088 sRef_checkMutable (res);
9090 if (s1 == s2 || s2 == SS_UNKNOWN)
9094 else if (s1 == SS_UNKNOWN)
9103 if (s2 == SS_DEFINED)
9109 llcontbuglit ("ssfixed: not implemented");
9118 flip = (s2 != SS_DEFINED);
9136 res->definfo = stateInfo_update (res->definfo, other->definfo);
9141 extern /*@notnull@*/ sRef sRef_getConjA (sRef s)
9144 llassert (sRef_isConj (s));
9146 ret = s->info->conj->a;
9147 llassert (ret != NULL);
9151 extern /*@notnull@*/ sRef sRef_getConjB (sRef s)
9154 llassert (sRef_isConj (s));
9156 ret = s->info->conj->b;
9157 llassert (ret != NULL);
9161 extern /*@exposed@*/ sRef sRef_makeArrow (sRef s, /*@dependent@*/ cstring f)
9166 p = sRef_makePointer (s);
9167 ret = sRef_makeField (p, f);
9168 DPRINTF (("Arrow: %s => %s",
9169 sRef_unparseFull (s), sRef_unparseFull (ret)));
9173 extern /*@exposed@*/ sRef sRef_buildArrow (sRef s, cstring f)
9178 p = sRef_buildPointer (s);
9179 ret = sRef_buildField (p, f);
9184 static /*@null@*/ sinfo sinfo_copy (/*@notnull@*/ sRef s)
9191 ret = (sinfo) dmalloc (sizeof (*ret));
9192 ret->cvar = (cref) dmalloc (sizeof (*ret->cvar));
9193 ret->cvar->lexlevel = s->info->cvar->lexlevel;
9194 ret->cvar->index = s->info->cvar->index;
9198 ret = (sinfo) dmalloc (sizeof (*ret));
9199 ret->paramno = s->info->paramno;
9200 llassert (ret->paramno >= -1);
9204 ret = (sinfo) dmalloc (sizeof (*ret));
9205 ret->arrayfetch = (ainfo) dmalloc (sizeof (*ret->arrayfetch));
9206 ret->arrayfetch->indknown = s->info->arrayfetch->indknown;
9207 ret->arrayfetch->ind = s->info->arrayfetch->ind;
9208 ret->arrayfetch->arr = s->info->arrayfetch->arr; /* sRef_copy (s->info->arrayfetch->arr); */ /*@i32@*/
9212 ret = (sinfo) dmalloc (sizeof (*ret));
9213 ret->field = (fldinfo) dmalloc (sizeof (*ret->field));
9214 ret->field->rec = s->info->field->rec; /* sRef_copy (s->info->field->rec); */ /*@i32@*/
9215 ret->field->field = s->info->field->field;
9219 ret = (sinfo) dmalloc (sizeof (*ret));
9220 ret->object = s->info->object;
9227 ret = (sinfo) dmalloc (sizeof (*ret));
9228 ret->ref = s->info->ref; /* Ref_copy (s->info->ref); */
9232 ret = (sinfo) dmalloc (sizeof (*ret));
9233 ret->conj = (cjinfo) dmalloc (sizeof (*ret->conj));
9234 ret->conj->a = s->info->conj->a; /* sRef_copy (s->info->conj->a); */
9235 ret->conj->b = s->info->conj->b; /* sRef_copy (s->info->conj->b);*/
9238 ret = (sinfo) dmalloc (sizeof (*ret));
9239 ret->spec = s->info->spec;
9242 case SK_UNCONSTRAINED:
9244 ret = (sinfo) dmalloc (sizeof (*ret));
9245 ret->fname = s->info->fname;
9252 llassertprint (s->info == NULL, ("s = %s", sRef_unparse (s)));
9260 static /*@null@*/ sinfo sinfo_fullCopy (/*@notnull@*/ sRef s)
9265 ** Since its a full copy, only storage is assigned
9266 ** to dependent fields.
9273 ret = (sinfo) dmalloc (sizeof (*ret));
9274 ret->cvar = (cref) dmalloc (sizeof (*ret->cvar));
9275 ret->cvar->lexlevel = s->info->cvar->lexlevel;
9276 ret->cvar->index = s->info->cvar->index;
9280 ret = (sinfo) dmalloc (sizeof (*ret));
9281 ret->paramno = s->info->paramno;
9282 llassert (ret->paramno >= -1);
9286 ret = (sinfo) dmalloc (sizeof (*ret));
9287 ret->arrayfetch = (ainfo) dmalloc (sizeof (*ret->arrayfetch));
9288 ret->arrayfetch->indknown = s->info->arrayfetch->indknown;
9289 ret->arrayfetch->ind = s->info->arrayfetch->ind;
9290 ret->arrayfetch->arr = sRef_saveCopy (s->info->arrayfetch->arr);
9294 ret = (sinfo) dmalloc (sizeof (*ret));
9295 ret->field = (fldinfo) dmalloc (sizeof (*ret->field));
9296 ret->field->rec = sRef_saveCopy (s->info->field->rec);
9297 ret->field->field = s->info->field->field;
9301 ret = (sinfo) dmalloc (sizeof (*ret));
9302 ret->object = s->info->object;
9309 ret = (sinfo) dmalloc (sizeof (*ret));
9310 ret->ref = sRef_saveCopy (s->info->ref);
9314 ret = (sinfo) dmalloc (sizeof (*ret));
9315 ret->conj = (cjinfo) dmalloc (sizeof (*ret->conj));
9316 ret->conj->a = sRef_saveCopy (s->info->conj->a);
9317 ret->conj->b = sRef_saveCopy (s->info->conj->b);
9320 ret = (sinfo) dmalloc (sizeof (*ret));
9321 ret->spec = s->info->spec;
9324 case SK_UNCONSTRAINED:
9325 ret = (sinfo) dmalloc (sizeof (*ret));
9326 ret->fname = s->info->fname;
9332 llassert (s->info == NULL);
9342 sinfo_update (/*@notnull@*/ /*@exposed@*/ sRef res,
9343 /*@notnull@*/ /*@exposed@*/ sRef other)
9345 llassert (res->kind == other->kind);
9350 res->info->cvar->lexlevel = other->info->cvar->lexlevel;
9351 res->info->cvar->index = other->info->cvar->index;
9355 res->info->paramno = other->info->paramno;
9356 llassert (res->info->paramno >= -1);
9360 res->info->arrayfetch->indknown = other->info->arrayfetch->indknown;
9361 res->info->arrayfetch->ind = other->info->arrayfetch->ind;
9362 res->info->arrayfetch->arr = other->info->arrayfetch->arr;
9366 res->info->field->rec = other->info->field->rec;
9367 res->info->field->field = other->info->field->field;
9371 res->info->object = other->info->object;
9378 res->info->ref = other->info->ref;
9382 res->info->conj->a = other->info->conj->a;
9383 res->info->conj->b = other->info->conj->b;
9387 res->info->spec = other->info->spec;
9391 case SK_UNCONSTRAINED:
9392 res->info->fname = other->info->fname;
9399 llassert (res->info == NULL);
9404 static void sinfo_free (/*@special@*/ /*@temp@*/ /*@notnull@*/ sRef s)
9405 /*@uses s->kind, s->info@*/
9406 /*@releases s->info@*/
9411 DPRINTF (("Free sinfo: [%p]", s->info->cvar));
9412 sfree (s->info->cvar);
9419 DPRINTF (("Free sinfo: [%p]", s->info->arrayfetch));
9420 sfree (s->info->arrayfetch);
9424 DPRINTF (("Free sinfo: [%p]", s->info->field));
9425 sfree (s->info->field);
9434 case SK_EXTERNAL: /*@i32 is copy now! */
9438 DPRINTF (("Free sinfo: [%p]", s->info->conj));
9439 sfree (s->info->conj);
9442 case SK_UNCONSTRAINED:
9452 if (s->info != NULL) {
9453 DPRINTF (("Free sinfo: [%p]", s->info));
9459 bool sRef_isNSLocalVar (sRef s)
9461 if (sRef_isLocalVar (s))
9463 uentry ue = sRef_getUentry (s);
9465 return (!uentry_isStatic (ue));
9473 bool sRef_isLocalVar (sRef s)
9475 if (sRef_isValid(s))
9477 return (s->kind == SK_CVAR
9478 && (s->info->cvar->lexlevel > fileScope));
9484 bool sRef_isRealLocalVar (sRef s)
9486 if (sRef_isValid(s))
9488 if (s->kind == SK_CVAR)
9490 if (s->info->cvar->lexlevel == functionScope)
9492 uentry ue = sRef_getUentry (s);
9494 if (uentry_isAnyParam (ue)
9495 || uentry_isRefParam (ue))
9506 return (s->info->cvar->lexlevel > functionScope);
9514 bool sRef_isLocalParamVar (sRef s)
9516 if (sRef_isValid(s))
9518 return (s->kind == SK_PARAM
9519 || (s->kind == SK_CVAR
9520 && (s->info->cvar->lexlevel > fileScope)));
9526 static speckind speckind_fromInt (int i)
9529 llassert (i >= SR_NOTHING && i <= SR_SYSTEM);
9532 return ((speckind) i);
9536 static void sRef_updateNullState (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other)
9539 res->nullstate = other->nullstate;
9540 res->nullinfo = stateInfo_update (res->nullinfo, other->nullinfo);
9541 sRef_resetAliasKind (res);
9544 void sRef_combineNullState (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other)
9546 nstate n1 = sRef_getNullState (res);
9547 nstate n2 = sRef_getNullState (other);
9551 if (n1 == n2 || n2 == NS_UNKNOWN)
9557 /* note: n2 is not unknown or defnull */
9561 case NS_ERROR: nn = NS_ERROR; break;
9562 case NS_UNKNOWN: flip = TRUE; nn = n2; break;
9563 case NS_POSNULL: break;
9564 case NS_DEFNULL: nn = NS_POSNULL; break;
9565 case NS_RELNULL: break;
9567 if (n2 == NS_MNOTNULL)
9578 if (n2 == NS_NOTNULL)
9597 res->nullinfo = stateInfo_update (res->nullinfo, other->nullinfo);
9600 res->nullstate = nn;
9601 sRef_resetAliasKind (res);
9604 cstring sRef_nullMessage (sRef s)
9606 llassert (sRef_isReasonable (s));
9608 switch (sRef_getNullState (s))
9612 return (cstring_makeLiteralTemp ("null"));
9614 return (cstring_makeLiteralTemp ("possibly null"));
9619 /*@observer@*/ cstring sRef_ntMessage (sRef s)
9621 llassert (sRef_isReasonable (s));
9623 switch (s->nullstate)
9627 return (cstring_makeLiteralTemp ("not nullterminated"));
9629 return (cstring_makeLiteralTemp ("possibly non-nullterminated"));
9636 sRef sRef_fixResultType (/*@returned@*/ sRef s, ctype typ, uentry ue)
9638 sRef tmp = sRef_undefined;
9641 llassert (sRef_isReasonable (s));
9652 tmp = sRef_fixResultType (s->info->arrayfetch->arr, typ, ue);
9654 ct = ctype_realType (sRef_getType (tmp));
9657 if (ctype_isKnown (ct))
9659 if (ctype_isAP (ct))
9668 ("Special clause indexes non-array (%t): *%q",
9669 ct, sRef_unparse (s->info->arrayfetch->arr)),
9670 uentry_whereLast (ue));
9674 tmp = sRef_fixResultType (s->info->arrayfetch->arr, typ, ue);
9676 if (s->info->arrayfetch->indknown)
9678 ret = sRef_makeArrayFetchKnown (tmp, s->info->arrayfetch->ind);
9682 ret = sRef_makeArrayFetch (tmp);
9688 sRef rec = sRef_fixResultType (s->info->field->rec, typ, ue);
9689 ctype ct = ctype_realType (sRef_getType (rec));
9691 if (ctype_isKnown (ct))
9693 if (ctype_isSU (ct))
9695 if (uentry_isValid (uentryList_lookupField (ctype_getFields (ct),
9696 s->info->field->field)))
9705 ("Special clause accesses non-existent field of result: %q.%s",
9706 sRef_unparse (rec), s->info->field->field),
9707 uentry_whereLast (ue));
9715 ("Special clause accesses field of non-struct or union result (%t): %q.%s",
9716 ct, sRef_unparse (rec), s->info->field->field),
9717 uentry_whereLast (ue));
9721 ret = sRef_makeField (tmp, s->info->field->field);
9727 tmp = sRef_fixResultType (s->info->ref, typ, ue);
9729 ct = ctype_realType (sRef_getType (tmp));
9731 if (ctype_isKnown (ct))
9733 if (ctype_isAP (ct))
9742 ("Special clause dereferences non-pointer (%t): *%q",
9743 ct, sRef_unparse (s->info->ref)),
9744 uentry_whereLast (ue));
9748 ret = sRef_makePointer (tmp);
9755 ("Special clause uses & operator (not allowed): &%q", sRef_unparse (s->info->ref)),
9756 uentry_whereLast (ue));
9765 bool sRef_isOnly (sRef s)
9767 return (sRef_isValid(s) && alkind_isOnly (s->aliaskind));
9770 bool sRef_isDependent (sRef s)
9772 return (sRef_isValid(s) && alkind_isDependent (s->aliaskind));
9775 bool sRef_isOwned (sRef s)
9777 return (sRef_isReasonable (s) && (s->aliaskind == AK_OWNED));
9780 bool sRef_isKeep (sRef s)
9782 return (sRef_isReasonable (s) && (s->aliaskind == AK_KEEP));
9785 bool sRef_isTemp (sRef s)
9787 return (sRef_isReasonable (s) && alkind_isTemp (s->aliaskind));
9790 bool sRef_isLocalState (sRef s)
9792 return (sRef_isReasonable (s) && (s->aliaskind == AK_LOCAL));
9795 bool sRef_isUnique (sRef s)
9797 return (sRef_isReasonable (s) && (s->aliaskind == AK_UNIQUE));
9800 bool sRef_isShared (sRef s)
9802 return (sRef_isReasonable (s) && (s->aliaskind == AK_SHARED));
9805 bool sRef_isExposed (sRef s)
9807 return (sRef_isReasonable (s) && (s->expkind == XO_EXPOSED));
9810 bool sRef_isObserver (sRef s)
9812 return (sRef_isReasonable (s) && (s->expkind == XO_OBSERVER));
9815 bool sRef_isFresh (sRef s)
9817 return (sRef_isReasonable (s) && (s->aliaskind == AK_FRESH));
9820 bool sRef_isDefinitelyNull (sRef s)
9822 return (sRef_isReasonable (s) && (sRef_getNullState (s) == NS_DEFNULL
9823 || sRef_getNullState (s) == NS_CONSTNULL));
9826 bool sRef_isAllocated (sRef s)
9828 return (sRef_isReasonable (s) && (s->defstate == SS_ALLOCATED));
9831 bool sRef_isStack (sRef s)
9833 return (sRef_isReasonable (s) && (s->aliaskind == AK_STACK));
9836 bool sRef_isNotNull (sRef s)
9838 return (sRef_isValid(s) && (sRef_getNullState (s) == NS_MNOTNULL
9839 || sRef_getNullState (s) == NS_NOTNULL));
9842 alkind sRef_getAliasKind (sRef s)
9844 if (sRef_isValid(s)) {
9845 llassert (alkind_isValid (s->aliaskind));
9846 return s->aliaskind;
9852 nstate sRef_getNullState (sRef s)
9854 if (sRef_isReasonable (s)) {
9855 llassert (nstate_isValid (s->nullstate));
9856 return s->nullstate;
9862 void sRef_reflectAnnotation (sRef s, annotationInfo a, fileloc loc)
9864 if (sRef_isReasonable (s))
9866 if (!valueTable_isDefined (s->state))
9868 s->state = valueTable_create (1);
9871 cstring_copy (metaStateInfo_getName (annotationInfo_getState (a))),
9872 stateValue_create (annotationInfo_getValue (a),
9873 stateInfo_makeLoc (loc, SA_DECLARED)));
9877 DPRINTF (("reflect loc: %s", fileloc_unparse (loc)));
9880 metaStateInfo_getName (annotationInfo_getState (a)),
9881 stateValue_create (annotationInfo_getValue (a),
9882 stateInfo_makeLoc (loc, SA_DECLARED)));
9884 DPRINTF (("sref: %s", sRef_unparse (s)));
9885 DPRINTF (("sref: %s", sRef_unparseFull (s)));
9890 void sRef_setMetaStateValueComplete (sRef s, cstring key, int value, fileloc loc)
9892 sRefSet aliases = usymtab_allAliases (s);
9894 sRef_setMetaStateValue (s, key, value, loc);
9896 sRefSet_realElements (aliases, current)
9898 if (sRef_isReasonable (current))
9900 current = sRef_updateSref (current);
9901 sRef_setMetaStateValue (current, key, value, loc);
9903 } end_sRefSet_realElements ;
9905 sRefSet_free (aliases);
9908 void sRef_setMetaStateValue (sRef s, cstring key, int value, fileloc loc)
9910 sRef_checkMutable (s);
9912 if (sRef_isReasonable (s))
9914 if (!valueTable_isDefined (s->state))
9916 DPRINTF (("inserting state: %s: %s %d", sRef_unparse (s), key, value));
9917 s->state = valueTable_create (1);
9918 valueTable_insert (s->state, cstring_copy (key),
9919 stateValue_create (value,
9920 stateInfo_makeLoc (loc, SA_CHANGED)));
9924 DPRINTF (("Updating state: %s: %s %d / %s", sRef_unparse (s), key, value,
9925 fileloc_unparse (loc)));
9926 if (valueTable_contains (s->state, key))
9929 (s->state, key, stateValue_create (value,
9930 stateInfo_makeLoc (loc, SA_CHANGED)));
9935 (s->state, cstring_copy (key),
9936 stateValue_create (value, stateInfo_makeLoc (loc, SA_CHANGED)));
9939 DPRINTF (("After: %s", sRef_unparseFull (s)));
9944 bool sRef_checkMetaStateValue (sRef s, cstring key, int value)
9946 if (sRef_isReasonable (s))
9948 if (valueTable_isDefined (s->state))
9952 DPRINTF (("check state: %s: %s %d", sRef_unparse (s), key, value));
9954 val = valueTable_lookup (s->state, key);
9955 llassert (stateValue_isDefined (val));
9956 return (stateValue_isError (val)
9957 || stateValue_getValue (val) == value);
9970 /*@observer@*/ stateValue sRef_getMetaStateValue (sRef s, cstring key)
9972 if (sRef_isReasonable (s))
9974 if (valueTable_isDefined (s->state))
9978 val = valueTable_lookup (s->state, key);
9979 /* Okay if its not defined, just returns stateValue_undefined */
9984 return stateValue_undefined;
9989 return stateValue_undefined;
9993 /*@observer@*/ valueTable sRef_getValueTable (sRef s)
9995 DPRINTF (("Get value table: %s", sRef_unparse (s)));
9997 if (sRef_isReasonable (s))
9999 llassert (sRef_isReasonable (s));
10000 DPRINTF (("Value table: %s", valueTable_unparse (s->state)));
10005 DPRINTF (("No value table!"));
10006 return valueTable_undefined;
10010 bool sRef_makeStateSpecial (sRef s)
10013 ** Default defined state can be made special.
10016 llassert (sRef_isReasonable (s)); /*@i523 why doesn't null-checking work!??? */
10018 if (s->defstate == SS_UNKNOWN || s->defstate == SS_DEFINED || s->defstate == SS_SPECIAL)
10020 /* s->aliaskind = AK_IMPTEMP; */ /* evans 2001-07-23 shouldn't effect alias state */
10021 s->defstate = SS_SPECIAL;
10022 DPRINTF (("Made special: %s", sRef_unparseFull (s)));
10027 /* s->aliaskind = AK_IMPTEMP; */
10028 s->defstate = SS_SPECIAL;
10033 void sRef_markImmutable (sRef s)
10035 if (sRef_isReasonable (s))
10037 DPRINTF (("Mark immutable: %s", sRef_unparseFull (s)));
10042 bool sRef_definitelyNullContext (sRef s)
10044 return (sRef_definitelyNull (s)
10045 || usymtab_isDefinitelyNullDeep (s));
10048 bool sRef_definitelyNullAltContext (sRef s)
10050 return (sRef_definitelyNull (s)
10051 || usymtab_isAltDefinitelyNullDeep (s));
10055 /* start modifications */
10056 struct s_bbufinfo sRef_getNullTerminatedState (sRef p_s) {
10057 struct s_bbufinfo BUFSTATE_UNKNOWN;
10058 BUFSTATE_UNKNOWN.bufstate = BB_NOTNULLTERMINATED;
10059 BUFSTATE_UNKNOWN.size = 0;
10060 BUFSTATE_UNKNOWN.len = 0;
10062 if (sRef_isValid(p_s))
10063 return p_s->bufinfo;
10064 return BUFSTATE_UNKNOWN;
10067 void sRef_setNullTerminatedState(sRef p_s) {
10068 if(sRef_isReasonable (p_s)) {
10069 p_s->bufinfo.bufstate = BB_NULLTERMINATED;
10071 llfatalbug( message("sRef_setNT passed a invalid sRef\n"));
10076 void sRef_setPossiblyNullTerminatedState(sRef p_s) {
10077 if( sRef_isReasonable (p_s)) {
10078 p_s->bufinfo.bufstate = BB_POSSIBLYNULLTERMINATED;
10080 llfatalbug( message("sRef_setPossNT passed a invalid sRef\n"));
10084 void sRef_setNotNullTerminatedState(sRef p_s) {
10085 if( sRef_isReasonable (p_s)) {
10086 p_s->bufinfo.bufstate = BB_NOTNULLTERMINATED;
10088 llfatalbug( message("sRef_unsetNT passed a invalid sRef\n"));
10092 void sRef_setLen(sRef p_s, int len) {
10093 if (sRef_isReasonable (p_s) && sRef_isNullTerminated(p_s))
10095 p_s->bufinfo.len = len;
10099 llfatalbug( message("sRef_setLen passed an invalid sRef\n"));
10104 void sRef_setSize(sRef p_s, int size) {
10105 if( sRef_isValid(p_s))
10107 p_s->bufinfo.size = size;
10111 llfatalbug( message("sRef_setSize passed a invalid sRef\n"));
10115 void sRef_resetLen(sRef p_s) {
10116 if (sRef_isReasonable (p_s))
10118 p_s->bufinfo.len = 0;
10122 llfatalbug (message ("sRef_setLen passed an invalid sRef\n"));
10126 /*drl7x 11/28/2000 */
10128 bool sRef_isFixedArray (sRef p_s) /*@*/ {
10130 c = sRef_getType (p_s);
10131 return ( ctype_isFixedArray (c) );
10134 size_t sRef_getArraySize (sRef p_s) /*@*/
10137 llassert (sRef_isFixedArray(p_s) );
10138 DPRINTF (( message ("sRef_getArraySize getting array size for %s", sRef_unparse(p_s) ) ));
10140 c = sRef_getType (p_s);
10141 return (ctype_getArraySize (c));
10144 void sRef_setValue (sRef s, multiVal val)
10146 if (!sRef_isReasonable (s))
10148 llcontbuglit ("Unreasonable sRef");
10149 llcontbug (message ("sRef: %s", sRef_unparse (s)));
10153 multiVal_free (s->val);
10157 bool sRef_hasValue (sRef s)
10159 return (sRef_isReasonable (s)
10160 && multiVal_isDefined (s->val));
10163 multiVal sRef_getValue (sRef s)
10165 if (sRef_isReasonable (s))
10170 return multiVal_undefined;