2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2001 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 lclint: lclint-request@cs.virginia.edu
21 ** To report a bug: lclint-bug@cs.virginia.edu
22 ** For more information: http://lclint.cs.virginia.edu
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 "lclintMacros.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) /*@*/ ;
67 sRef_aliasSetComplete (void (p_predf) (sRef, fileloc), sRef p_s, fileloc p_loc)
70 static int sRef_depth (sRef p_s) /*@*/ ;
73 sRef_innerAliasSetComplete (void (p_predf) (sRef, fileloc), sRef p_s,
78 sRef_innerAliasSetCompleteParam (void (p_predf) (sRef, sRef), sRef p_s, sRef p_t)
82 sRef_aliasSetCompleteParam (void (p_predf) (sRef, alkind, fileloc), sRef p_s,
83 alkind p_kind, fileloc p_loc)
86 static speckind speckind_fromInt (int p_i);
87 static bool sRef_equivalent (sRef p_s1, sRef p_s2);
88 static bool sRef_isDeepUnionField (sRef p_s);
89 static void sRef_addDeriv (/*@notnull@*/ sRef p_s, /*@notnull@*/ /*@exposed@*/ sRef p_t);
90 static /*@dependent@*/ sRef sRef_makeResultType (ctype p_ct) /*@*/ ;
91 static bool sRef_checkModify (sRef p_s, sRefSet p_sl) /*@*/ ;
93 static void sRef_checkMutable (/*@unused@*/ sRef s)
96 if (sRef_isValid (s) && s->immut)
98 llcontbug (message ("Modification to sRef marked immutable: %q",
99 sRef_unparseFull (s)));
103 static bool skind_isSimple (skind sk)
107 case SK_PARAM: case SK_CVAR: case SK_CONST:
108 case SK_OBJECT: case SK_UNKNOWN: case SK_NEW:
115 static void sinfo_free (/*@special@*/ /*@temp@*/ /*@notnull@*/ sRef p_s)
116 /*@uses p_s->kind, p_s->info@*/
117 /*@releases p_s->info@*/ ;
119 static /*@null@*/ sinfo sinfo_copy (/*@notnull@*/ sRef p_s) /*@*/ ;
120 static void sRef_setPartsFromUentry (sRef p_s, uentry p_ue)
122 static bool checkDeadState (/*@notnull@*/ sRef p_el, bool p_tbranch, fileloc p_loc);
123 static /*@dependent@*/ sRef sRef_constructPointerAux (/*@notnull@*/ /*@exposed@*/ sRef p_t) /*@*/ ;
126 sRef_combineExKinds (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other)
127 /*@modifies p_res@*/ ;
130 sRef_combineAliasKinds (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other,
131 clause p_cl, fileloc p_loc)
132 /*@modifies p_res@*/ ;
135 sRef_combineNullState (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other)
136 /*@modifies p_res@*/ ;
139 sRef_combineDefState (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other)
140 /*@modifies p_res@*/ ;
142 static void sRef_setStateFromAbstractUentry (sRef p_s, uentry p_ue)
146 sinfo_update (/*@notnull@*/ /*@exposed@*/ sRef p_res,
147 /*@notnull@*/ /*@exposed@*/ sRef p_other);
148 static void sRef_setDefinedAux (sRef p_s, fileloc p_loc, bool p_clear)
150 static void sRef_setDefinedNoClear (sRef p_s, fileloc p_loc)
152 static void sRef_setStateAux (sRef p_s, sstate p_ss, fileloc p_loc)
155 static /*@exposed@*/ sRef
156 sRef_buildNCField (/*@exposed@*/ sRef p_rec, /*@exposed@*/ cstring p_f);
159 sRef_mergeStateAux (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other,
160 clause p_cl, bool p_opt, fileloc p_loc,
162 /*@modifies p_res, p_other@*/ ;
164 static /*@null@*/ sinfo sinfo_fullCopy (/*@notnull@*/ sRef p_s);
165 static bool sRef_doModify (sRef p_s, sRefSet p_sl) /*@modifies p_s@*/ ;
166 static bool sRef_doModifyVal (sRef p_s, sRefSet p_sl) /*@modifies p_s@*/;
167 static bool sRef_checkModifyVal (sRef p_s, sRefSet p_sl) /*@*/ ;
169 static /*@only@*/ sRefSet
170 sRef_mergeDerivs (/*@only@*/ sRefSet p_res, sRefSet p_other,
171 bool p_opt, clause p_cl, fileloc p_loc);
173 static /*@only@*/ sRefSet
174 sRef_mergeUnionDerivs (/*@only@*/ sRefSet p_res,
175 /*@exposed@*/ sRefSet p_other,
176 bool p_opt, clause p_cl, fileloc p_loc);
178 static /*@only@*/ sRefSet
179 sRef_mergePdefinedDerivs (/*@only@*/ sRefSet p_res, sRefSet p_other, bool p_opt,
180 clause p_cl, fileloc p_loc);
182 static /*@only@*/ cstring sRef_unparseWithArgs (sRef p_s, uentryList p_args);
183 static /*@only@*/ cstring sRef_unparseNoArgs (sRef p_s);
185 static /*@exposed@*/ sRef sRef_findDerivedPointer (sRef p_s);
186 static /*@exposed@*/ sRef sRef_findDerivedField (/*@notnull@*/ sRef p_rec, cstring p_f);
187 static /*@exposed@*/ sRef
188 sRef_getDeriv (/*@notnull@*/ /*@returned@*/ sRef p_set, sRef p_guide);
190 static bool inFunction = FALSE;
191 static /*@only@*/ sRefTable allRefs;
193 /* # define DEBUGREFS */
197 static totnsrefs = 0;
198 static maxnsrefs = 0;
199 static ntotrefers = 0;
203 static /*@checked@*/ bool protectDerivs = FALSE;
206 ** Result of sRef_alloc is dependent since allRefs may
207 ** reference it. It is only if !inFunction.
210 static /*@dependent@*/ /*@out@*/ /*@notnull@*/ sRef
213 sRef s = (sRef) dmalloc (sizeof (*s));
217 DPRINTF (("Alloc sref: [%p]", s));
221 allRefs = sRefTable_add (allRefs, s);
226 DPRINTF (("Not in function!"));
232 if (nsrefs >= maxnsrefs)
241 /*@-mustfree@*/ /*@-freshtrans@*/
243 /*@=mustfree@*/ /*@=freshtrans@*/
246 static void sRef_checkValidAux (sRef s, sRefSet checkedsofar)
250 if (!sRef_isValid (s)) return;
252 if (sRefSet_containsSameObject (checkedsofar, s))
258 checkedsofar = sRefSet_insert (checkedsofar, s);
259 /*@=temptrans@*/ /* checksofar will be destroyed before checkValid returns */
263 case SK_UNCONSTRAINED:
264 llassert (cstring_length (s->info->fname) < 100);
268 llassert (s->info->cvar->lexlevel >= 0);
269 /* llassert (s->info->cvar->lexlevel <= usymtab_getCurrentDepth ()); */
273 llassert (s->info->paramno >= -1);
274 llassert (s->info->paramno <= 50); /*@i32 bogus...*/
278 sRef_checkValidAux (s->info->arrayfetch->arr, checkedsofar);
282 sRef_checkValidAux (s->info->field->rec, checkedsofar);
283 llassert (cstring_length (s->info->field->field) < 100);
287 sRef_checkValidAux (s->info->ref, checkedsofar);
291 sRef_checkValidAux (s->info->ref, checkedsofar);
295 /* check ctype s->info->object */
299 sRef_checkValidAux (s->info->conj->a, checkedsofar);
300 sRef_checkValidAux (s->info->conj->b, checkedsofar);
304 llassert (cstring_length (s->info->fname) < 100);
308 sRef_checkValidAux (s->info->ref, checkedsofar);
312 sRef_checkValidAux (s->info->ref, checkedsofar);
318 /* check ctyp s->type */
322 llassert (s->info->spec == SR_NOTHING
323 || s->info->spec == SR_INTERNAL
324 || s->info->spec == SR_SPECSTATE
325 || s->info->spec == SR_SYSTEM);
335 sRefSet_elements (s->deriv, el)
337 sRef_checkValidAux (el, checkedsofar);
338 } end_sRefSet_elements ;
341 void sRef_checkValid (/*@unused@*/ sRef s)
345 sRefSet checkedsofar = sRefSet_new ();
346 sRef_checkValidAux (s, checkedsofar);
350 static /*@dependent@*/ /*@notnull@*/ /*@special@*/ sRef
353 /*@ensures isnull result->aliasinfo, result->definfo,
354 result->expinfo, result->info, result->deriv, result->state@*/
356 sRef s = sRef_alloc ();
358 s->kind = SK_UNKNOWN;
363 s->type = ctype_unknown;
364 s->defstate = SS_UNKNOWN;
366 /* start modifications */
367 s->bufinfo.bufstate = BB_NOTNULLTERMINATED;
368 /* end modifications */
370 s->aliaskind = AK_UNKNOWN;
371 s->oaliaskind = AK_UNKNOWN;
373 s->nullstate = NS_UNKNOWN;
375 s->expkind = XO_UNKNOWN;
376 s->oexpkind = XO_UNKNOWN;
378 s->aliasinfo = stateInfo_undefined;
379 s->definfo = stateInfo_undefined;
380 s->nullinfo = stateInfo_undefined;
381 s->expinfo = stateInfo_undefined;
384 s->deriv = sRefSet_undefined;
386 s->state = valueTable_undefined;
391 static /*@dependent@*/ /*@notnull@*/ /*@special@*/ sRef
394 /*@ensures isnull result->aliasinfo, result->definfo,
395 result->expinfo, result->info, result->deriv@*/
397 sRef res = sRef_new ();
399 res->state = valueTable_undefined;
404 void sRef_protectDerivs (void) /*@modifies protectDerivs@*/
406 llassert (!protectDerivs);
407 protectDerivs = TRUE;
410 void sRef_clearProtectDerivs (void) /*@modifies protectDerivs@*/
412 llassert (protectDerivs);
413 protectDerivs = FALSE;
417 ** hmmm...here be kind of a hack. This function mysteriously appeared
418 ** in my code, but I'm sure I didn't write it.
422 sRef_isRecursiveField (sRef s)
424 if (sRef_isField (s))
426 if (sRef_depth (s) > 13)
431 fieldname = sRef_getField (s);
432 base = sRef_getBase (s);
434 while (sRef_isValid (base))
436 if (sRef_isField (base))
438 if (cstring_equal (fieldname, sRef_getField (base)))
444 base = sRef_getBaseSafe (base);
453 sRef_addDeriv (/*@notnull@*/ sRef s, /*@notnull@*/ /*@exposed@*/ sRef t)
455 if (!context_inProtectVars ()
459 && !sRef_isConst (s))
461 int sd = sRef_depth (s);
462 int td = sRef_depth (t);
469 if (sRef_isFileOrGlobalScope (s))
471 if (context_inFunctionLike ()
472 && ctype_isKnown (sRef_getType (s))
473 && !ctype_isFunction (sRef_getType (s)))
475 globSet g = context_getUsedGlobs ();
477 if (!globSet_member (g, s))
480 ** don't report as a bug
484 (message ("sRef_addDeriv: global variable not in used "
485 "globs: %q / %s / %q",
487 ctype_unparse (sRef_getType (s)),
488 sRefSet_unparse (s->deriv)));
493 s->deriv = sRefSet_insert (s->deriv, t);
499 DPRINTF (("Add deriv: [%p] %s / [%p] %s",
501 t, sRef_unparse (t)));
503 s->deriv = sRefSet_insert (s->deriv, t);
509 sRef_deepPred (bool (predf) (sRef), sRef s)
511 if (sRef_isValid (s))
513 if ((*predf)(s)) return TRUE;
518 return (sRef_deepPred (predf, s->info->ref));
520 return (sRef_deepPred (predf, s->info->arrayfetch->arr));
522 return (sRef_deepPred (predf, s->info->field->rec));
524 return (sRef_deepPred (predf, s->info->conj->a)
525 || sRef_deepPred (predf, s->info->conj->b));
534 bool sRef_modInFunction (void)
539 void sRef_setStateFromType (sRef s, ctype ct)
541 if (sRef_isValid (s))
543 if (ctype_isUser (ct))
545 DPRINTF (("Here we are: %s", sRef_unparseFull (s)));
546 sRef_setStateFromUentry
547 (s, usymtab_getTypeEntry (ctype_typeId (ct)));
549 else if (ctype_isAbstract (ct))
551 DPRINTF (("Here we are: %s", sRef_unparseFull (s)));
552 sRef_setStateFromAbstractUentry
553 (s, usymtab_getTypeEntry (ctype_typeId (ct)));
557 ; /* not a user type */
562 static void sRef_setTypeState (sRef s)
564 if (sRef_isValid (s))
566 sRef_setStateFromType (s, s->type);
571 sRef_hasAliasInfoLoc (sRef s)
573 return (sRef_isValid (s) && (s->aliasinfo != NULL)
574 && (fileloc_isDefined (s->aliasinfo->loc)));
577 static /*@falsenull@*/ bool
578 sRef_hasStateInfoLoc (sRef s)
580 return (sRef_isValid (s) && (s->definfo != NULL)
581 && (fileloc_isDefined (s->definfo->loc)));
584 static /*@falsenull@*/ bool
585 sRef_hasExpInfoLoc (sRef s)
587 return (sRef_isValid (s)
588 && (s->expinfo != NULL) && (fileloc_isDefined (s->expinfo->loc)));
591 static /*@observer@*/ /*@unused@*/ stateInfo sRef_getInfo (sRef s, cstring key)
595 if (!sRef_isValid (s)) {
596 return stateInfo_undefined;
599 sv = valueTable_lookup (s->state, key);
601 if (stateValue_isDefined (sv))
603 return stateValue_getInfo (sv);
606 return stateInfo_undefined;
611 sRef_hasNullInfoLoc (sRef s)
613 return (sRef_isValid (s) && s->nullinfo != NULL
614 && (fileloc_isDefined (s->nullinfo->loc)));
618 sRef_hasAliasInfoRef (sRef s)
620 return (sRef_isValid (s) && (s->aliasinfo != NULL)
621 && (sRef_isValid (s->aliasinfo->ref)));
624 static /*@observer@*/ fileloc
625 sRef_getAliasInfoLoc (/*@exposed@*/ sRef s)
627 llassert (sRef_isValid (s) && s->aliasinfo != NULL
628 && (fileloc_isDefined (s->aliasinfo->loc)));
629 return (s->aliasinfo->loc);
632 static /*@observer@*/ fileloc
633 sRef_getStateInfoLoc (/*@exposed@*/ sRef s)
635 llassert (sRef_isValid (s) && s->definfo != NULL
636 && (fileloc_isDefined (s->definfo->loc)));
637 return (s->definfo->loc);
640 static /*@observer@*/ fileloc
641 sRef_getExpInfoLoc (/*@exposed@*/ sRef s)
643 llassert (sRef_isValid (s) && s->expinfo != NULL
644 && (fileloc_isDefined (s->expinfo->loc)));
645 return (s->expinfo->loc);
648 static /*@observer@*/ fileloc
649 sRef_getNullInfoLoc (/*@exposed@*/ sRef s)
651 llassert (sRef_isValid (s) && s->nullinfo != NULL
652 && (fileloc_isDefined (s->nullinfo->loc)));
653 return (s->nullinfo->loc);
657 sRef_getAliasInfoRef (/*@temp@*/ sRef s)
659 llassert (sRef_isValid (s) && s->aliasinfo != NULL);
660 return (s->aliasinfo->ref);
663 bool sRef_inGlobalScope ()
669 ** This function should be called before new sRefs are created
670 ** somewhere where they will have a lifetime greater than the
671 ** current function scope.
674 void sRef_setGlobalScope ()
676 llassert (inFunction);
677 DPRINTF (("leave function"));
681 void sRef_clearGlobalScope ()
683 llassert (!inFunction);
684 DPRINTF (("enter function"));
688 static bool oldInFunction = FALSE;
690 void sRef_setGlobalScopeSafe ()
692 oldInFunction = inFunction;
693 DPRINTF (("leave function"));
697 void sRef_clearGlobalScopeSafe ()
699 inFunction = oldInFunction;
700 DPRINTF (("clear function: %s", bool_unparse (inFunction)));
703 void sRef_enterFunctionScope ()
705 llassert (!inFunction);
706 llassert (sRefTable_isEmpty (allRefs));
708 DPRINTF (("enter function"));
711 void sRef_exitFunctionScope ()
716 DPRINTF (("Exit function scope."));
717 sRefTable_clear (allRefs);
722 llbuglit ("sRef_exitFunctionScope: not in function");
726 void sRef_destroyMod () /*@globals killed allRefs;@*/
729 llmsg (message ("Live: %d / %d ", nsrefs, totnsrefs));
732 sRefTable_free (allRefs);
736 static /*@notnull@*/ /*@exposed@*/ sRef
737 sRef_fixConj (/*@notnull@*/ sRef s)
742 s = sRef_getConjA (s);
743 } while (sRef_isConj (s));
745 llassert (sRef_isValid (s));
746 return s; /* don't need to ref */
755 sRef_isExternallyVisibleAux (sRef s)
758 sRef base = sRef_getRootBase (s);
760 if (sRef_isValid (base))
762 res = sRef_isParam (base) || sRef_isFileOrGlobalScope (base) || sRef_isExternal (base);
769 sRef_isExternallyVisible (sRef s)
771 return (sRef_aliasCheckSimplePred (sRef_isExternallyVisibleAux, s));
775 sRef_getBaseUentry (sRef s)
777 sRef base = sRef_getRootBase (s);
778 uentry res = uentry_undefined;
780 if (sRef_isValid (base))
785 res = usymtab_getRefQuiet (paramsScope, base->info->paramno);
789 res = usymtab_getRefQuiet (base->info->cvar->lexlevel,
790 base->info->cvar->index);
802 ** lookup the current uentry corresponding to s, and return the corresponding sRef.
803 ** yuk yuk yuk yuk yuk yuk yuk yuk
807 sRef_updateSref (sRef s)
813 if (!sRef_isValid (s)) return sRef_undefined;
822 case SK_UNCONSTRAINED:
829 sRef r = sRef_updateSref (s->info->ref);
831 if (r != s->info->ref)
833 return sRef_makeExternal (r);
843 uentry ue = sRef_getUentry (s);
845 /* must be raw name! (need the marker) */
846 ue = usymtab_lookupSafe (uentry_rawName (ue));
848 if (uentry_isUndefined (ue))
854 DPRINTF (("Update sref: %s", uentry_unparseFull (ue)));
855 return (uentry_getSref (ue));
859 /* special case if ind known */
860 inner = s->info->arrayfetch->arr;
861 ret = sRef_updateSref (inner);
869 res = sRef_makeArrayFetch (ret);
875 inner = s->info->field->rec;
876 ret = sRef_updateSref (inner);
884 res = (sRef_makeField (ret, s->info->field->field));
889 inner = s->info->ref;
890 ret = sRef_updateSref (inner);
897 res = sRef_makePointer (ret);
903 inner = s->info->ref;
904 ret = sRef_updateSref (inner);
912 res = sRef_makeAddress (ret);
919 sRef innera = s->info->conj->a;
920 sRef innerb = s->info->conj->b;
921 sRef reta = sRef_updateSref (innera);
922 sRef retb = sRef_updateSref (innerb);
924 if (innera == reta && innerb == retb)
930 res = sRef_makeConj (reta, retb);
941 sRef_getUentry (sRef s)
943 llassert (sRef_isValid (s));
948 return (usymtab_getRefQuiet (paramsScope, s->info->paramno));
950 return (usymtab_getRefQuiet (s->info->cvar->lexlevel, s->info->cvar->index));
953 if (sRef_isCvar (s->info->conj->a) || sRef_isParam (s->info->conj->a)
954 || sRef_isConj (s->info->conj->a))
956 return sRef_getUentry (s->info->conj->a);
960 return sRef_getUentry (s->info->conj->b);
965 return uentry_undefined;
971 sRef_getParam (sRef s)
973 llassert (sRef_isValid (s));
974 llassert (s->kind == SK_PARAM);
976 return s->info->paramno;
980 sRef_isModified (sRef s)
982 return (!sRef_isValid (s) || s->modified);
985 void sRef_setModified (sRef s)
987 if (sRef_isValid (s))
992 if (sRef_isRefsField (s))
994 sRef base = sRef_getBase (s);
997 llassert (s->kind == SK_FIELD);
1000 if (sRef_isPointer (base))
1002 base = sRef_getBase (base);
1005 if (sRef_isRefCounted (base))
1007 base->aliaskind = AK_NEWREF;
1014 ** note: this side-effects sRefSet to set modified to TRUE
1015 ** for any sRef similar to s.
1019 sRef_canModifyVal (sRef s, sRefSet sl)
1021 if (context_getFlag (FLG_MUSTMOD))
1023 return (sRef_doModifyVal (s, sl));
1027 return (sRef_checkModifyVal (s, sl));
1032 sRef_canModify (sRef s, sRefSet sl)
1035 if (context_getFlag (FLG_MUSTMOD))
1037 return (sRef_doModify (s, sl));
1041 return (sRef_checkModify (s, sl));
1050 bool sRef_checkModifyVal (sRef s, sRefSet sl)
1052 DPRINTF (("Check modify val: %s", sRef_unparse (s)));
1054 if (sRef_isInvalid (s))
1061 case SK_UNCONSTRAINED:
1065 DPRINTF (("Modify var: %s", sRef_unparse (s)));
1067 if (sRef_isFileOrGlobalScope (s))
1069 if (context_checkGlobMod (s))
1071 return (sRefSet_member (sl, s));
1081 return (sRefSet_member (sl, s)
1082 || alkind_isOnly (sRef_getOrigAliasKind (s)));
1084 /* special case if ind known */
1085 return (sRefSet_member (sl, s) ||
1086 sRef_checkModifyVal (s->info->arrayfetch->arr, sl));
1088 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->field->rec, sl));
1090 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->ref, sl));
1092 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->ref, sl));
1094 return ((sRef_checkModifyVal (s->info->conj->a, sl)) &&
1095 (sRef_checkModifyVal (s->info->conj->b, sl)));
1106 switch (s->info->spec)
1108 case SR_NOTHING: return TRUE;
1110 if (context_getFlag (FLG_INTERNALGLOBS))
1112 return (sRefSet_member (sl, s));
1118 case SR_SPECSTATE: return TRUE;
1119 case SR_SYSTEM: return (sRefSet_member (sl, s));
1120 case SR_GLOBALMARKER: BADBRANCH;
1123 case SK_RESULT: BADBRANCH;
1129 ** this should probably be elsewhere...
1131 ** returns TRUE iff sl indicates that s can be modified
1134 static bool sRef_checkModify (sRef s, sRefSet sl)
1136 llassert (sRef_isValid (s));
1140 case SK_UNCONSTRAINED:
1144 if (sRef_isFileOrGlobalScope (s))
1146 if (context_checkGlobMod (s))
1148 return (sRefSet_member (sl, s));
1160 return (sRefSet_member (sl, s) ||
1161 sRef_checkModifyVal (s->info->arrayfetch->arr, sl));
1164 sRef sr = s->info->field->rec;
1166 if (sr->kind == SK_PARAM)
1167 return TRUE; /* structs are copied on call */
1169 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->field->rec, sl));
1175 sm = sRefSet_member (sl, s);
1180 return (sRef_checkModifyVal (s->info->ref, sl));
1183 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->ref, sl));
1185 return ((sRef_checkModify (s->info->conj->a, sl)) &&
1186 (sRef_checkModify (s->info->conj->b, sl)));
1196 switch (s->info->spec)
1198 case SR_NOTHING: return TRUE;
1200 if (context_getFlag (FLG_INTERNALGLOBS))
1202 return (sRefSet_member (sl, s));
1208 case SR_SPECSTATE: return TRUE;
1209 case SR_SYSTEM: return (sRefSet_member (sl, s));
1210 case SR_GLOBALMARKER: BADBRANCH;
1213 case SK_RESULT: BADBRANCH;
1218 cstring sRef_stateVerb (sRef s)
1220 if (sRef_isDead (s))
1222 return cstring_makeLiteralTemp ("released");
1224 else if (sRef_isKept (s))
1226 return cstring_makeLiteralTemp ("kept");
1228 else if (sRef_isDependent (s))
1230 return cstring_makeLiteralTemp ("dependent");
1238 cstring sRef_stateAltVerb (sRef s)
1240 if (sRef_isDead (s))
1242 return cstring_makeLiteralTemp ("live");
1244 else if (sRef_isKept (s))
1246 return cstring_makeLiteralTemp ("not kept");
1248 else if (sRef_isDependent (s))
1250 return cstring_makeLiteralTemp ("independent");
1259 bool sRef_doModifyVal (sRef s, sRefSet sl)
1261 llassert (sRef_isValid (s));
1266 case SK_UNCONSTRAINED:
1270 if (sRef_isFileOrGlobalScope (s))
1273 if (context_checkGlobMod (s))
1275 return (sRefSet_modifyMember (sl, s));
1279 (void) sRefSet_modifyMember (sl, s);
1289 return (sRefSet_modifyMember (sl, s)
1290 || alkind_isOnly (sRef_getOrigAliasKind (s)));
1292 /* special case if ind known */
1293 /* unconditional OR, need side effect */
1294 return (OR (sRefSet_modifyMember (sl, s),
1295 sRef_doModifyVal (s->info->arrayfetch->arr, sl)));
1297 return (OR (sRefSet_modifyMember (sl, s),
1298 sRef_doModifyVal (s->info->field->rec, sl)));
1300 return (OR (sRefSet_modifyMember (sl, s),
1301 sRef_doModifyVal (s->info->ref, sl)));
1303 return (OR (sRefSet_modifyMember (sl, s),
1304 sRef_doModifyVal (s->info->ref, sl)));
1306 return (AND (sRef_doModifyVal (s->info->conj->a, sl) ,
1307 sRef_doModifyVal (s->info->conj->b, sl)));
1317 switch (s->info->spec)
1319 case SR_NOTHING: return TRUE;
1321 if (context_getFlag (FLG_INTERNALGLOBS))
1323 return (sRefSet_modifyMember (sl, s));
1327 (void) sRefSet_modifyMember (sl, s);
1330 case SR_SPECSTATE: return TRUE;
1331 case SR_SYSTEM: return (sRefSet_modifyMember (sl, s));
1332 case SR_GLOBALMARKER: BADBRANCH;
1336 case SK_RESULT: BADBRANCH;
1342 ** this should probably be elsewhere...
1344 ** returns TRUE iff sl indicates that s can be modified
1348 bool sRef_doModify (sRef s, sRefSet sl)
1350 llassert (sRef_isValid (s));
1354 case SK_UNCONSTRAINED:
1358 if (sRef_isFileOrGlobalScope (s))
1360 if (context_checkGlobMod (s))
1362 return (sRefSet_modifyMember (sl, s));
1366 (void) sRefSet_modifyMember (sl, s);
1378 return (OR (sRefSet_modifyMember (sl, s),
1379 sRef_doModifyVal (s->info->arrayfetch->arr, sl)));
1382 sRef sr = s->info->field->rec;
1384 if (sr->kind == SK_PARAM)
1386 return TRUE; /* structs are shallow-copied on call */
1389 return (OR (sRefSet_modifyMember (sl, s),
1390 sRef_doModifyVal (s->info->field->rec, sl)));
1394 return (OR (sRefSet_modifyMember (sl, s),
1395 sRef_doModifyVal (s->info->ref, sl)));
1398 return (OR (sRefSet_modifyMember (sl, s),
1399 sRef_doModifyVal (s->info->ref, sl)));
1401 return (AND (sRef_doModify (s->info->conj->a, sl),
1402 (sRef_doModify (s->info->conj->b, sl))));
1413 switch (s->info->spec)
1415 case SR_NOTHING: return TRUE;
1416 case SR_INTERNAL: return TRUE;
1417 case SR_SPECSTATE: return TRUE;
1418 case SR_SYSTEM: return (sRefSet_modifyMember (sl, s));
1419 case SR_GLOBALMARKER: BADBRANCH;
1422 case SK_RESULT: BADBRANCH;
1427 static /*@exposed@*/ sRef
1428 sRef_leastCommon (/*@exposed@*/ sRef s1, sRef s2)
1430 llassert (sRef_similar (s1, s2));
1432 if (!sRef_isValid (s1)) return s1;
1433 if (!sRef_isValid (s2)) return s1;
1435 sRef_combineDefState (s1, s2);
1436 sRef_combineNullState (s1, s2);
1437 sRef_combineExKinds (s1, s2);
1439 if (s1->aliaskind != s2->aliaskind)
1441 if (s1->aliaskind == AK_UNKNOWN)
1443 s1->aliaskind = s2->aliaskind;
1445 else if (s2->aliaskind == AK_UNKNOWN)
1451 s1->aliaskind = AK_ERROR;
1458 int sRef_compare (sRef s1, sRef s2)
1460 if (s1 == s2) return 0;
1462 if (sRef_isInvalid (s1)) return -1;
1463 if (sRef_isInvalid (s2)) return 1;
1465 INTCOMPARERETURN (s1->kind, s2->kind);
1466 INTCOMPARERETURN (s1->defstate, s2->defstate);
1467 INTCOMPARERETURN (s1->aliaskind, s2->aliaskind);
1469 DPRINTF (("Compare null state: %s / %s",
1470 sRef_unparseFull (s1),
1471 sRef_unparseFull (s2)));
1473 COMPARERETURN (nstate_compare (sRef_getNullState (s1),
1474 sRef_getNullState (s2)));
1479 return (int_compare (s1->info->paramno, s2->info->paramno));
1482 COMPARERETURN (sRef_compare (s1->info->arrayfetch->arr,
1483 s2->info->arrayfetch->arr));
1485 if (s1->info->arrayfetch->indknown && s2->info->arrayfetch->indknown)
1487 return (int_compare (s1->info->arrayfetch->ind,
1488 s2->info->arrayfetch->ind));
1490 if (!s1->info->arrayfetch->indknown && !s2->info->arrayfetch->indknown)
1497 COMPARERETURN (sRef_compare (s1->info->field->rec, s2->info->field->rec));
1499 if (cstring_equal (s1->info->field->field, s2->info->field->field))
1506 return (sRef_compare (s1->info->ref, s2->info->ref));
1508 COMPARERETURN (sRef_compare (s1->info->conj->a, s2->info->conj->a));
1509 return (sRef_compare (s1->info->conj->b, s2->info->conj->b));
1510 case SK_UNCONSTRAINED:
1511 return (cstring_compare (s1->info->fname, s2->info->fname));
1523 return (generic_compare (s1->info->spec, s2->info->spec));
1528 static bool cref_equal (cref c1, cref c2)
1530 return ((c1->lexlevel == c2->lexlevel) &&
1531 (usymId_equal (c1->index, c2->index)));
1535 ** returns true if s1 could be the same storage as s2.
1536 ** i.e., a[?] ~ a[3]. Note its not symmetric ... s1
1537 ** should be more specific.
1541 ** like similar, but matches objects <-> non-objects
1545 sRef_uniqueReference (sRef s)
1547 return (sRef_isFresh (s) || sRef_isUnique (s)
1548 || sRef_isOnly (s) || sRef_isStack (s)
1549 || sRef_isAddress (s));
1553 sRef_similarRelaxedAux (sRef s1, sRef s2)
1557 if (sRef_isUnknownArrayFetch (s1))
1567 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
1569 if (sRef_isConj (s2))
1570 return (sRef_similarRelaxedAux (s1, sRef_getConjA (s2)) ||
1571 sRef_similarRelaxedAux (s1, sRef_getConjB (s2)));
1576 return ((s2->kind == SK_CVAR)
1577 && (cref_equal (s1->info->cvar, s2->info->cvar)));
1579 return ((s2->kind == SK_PARAM)
1580 && (s1->info->paramno == s2->info->paramno));
1582 if (s2->kind == SK_ARRAYFETCH)
1584 if (sRef_similarRelaxedAux (s1->info->arrayfetch->arr,
1585 s2->info->arrayfetch->arr))
1587 if (s1->info->arrayfetch->indknown)
1589 if (s2->info->arrayfetch->indknown)
1591 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
1606 return ((s2->kind == SK_FIELD
1607 && (sRef_similarRelaxedAux (s1->info->field->rec,
1608 s2->info->field->rec)
1609 && cstring_equal (s1->info->field->field,
1610 s2->info->field->field))));
1612 return ((s2->kind == SK_PTR)
1613 && sRef_similarRelaxedAux (s1->info->ref, s2->info->ref));
1615 return ((s2->kind == SK_ADR)
1616 && sRef_similarRelaxedAux (s1->info->ref, s2->info->ref));
1618 return ((sRef_similarRelaxedAux (s1->info->conj->a, s2) ||
1619 (sRef_similarRelaxedAux (s1->info->conj->b, s2))));
1621 return (s1->info->spec == s2->info->spec);
1622 case SK_UNCONSTRAINED:
1623 return (cstring_equal (s1->info->fname, s2->info->fname));
1638 sRef_similarRelaxed (sRef s1, sRef s2)
1644 if (sRef_isThroughArrayFetch (s1))
1654 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
1656 us1 = sRef_uniqueReference (s1);
1657 us2 = sRef_uniqueReference (s2);
1659 if ((s1->kind == SK_EXTERNAL && (s2->kind != SK_EXTERNAL && !us2))
1660 || (s2->kind == SK_EXTERNAL && (s1->kind != SK_EXTERNAL && !us1)))
1663 ** Previously, also:
1664 ** || (sRef_isExposed (s1) && !us2) || (sRef_isExposed (s2) && !us1)) ????
1666 ** No clue why this was there?!
1670 if (sRef_isExposed (s1) && sRef_isCvar (s1))
1672 uentry ue1 = sRef_getUentry (s1);
1674 if (uentry_isRefParam (ue1))
1676 return sRef_similarRelaxedAux (s1, s2);
1680 if (sRef_isExposed (s2) && sRef_isCvar (s2))
1682 uentry ue2 = sRef_getUentry (s2);
1684 if (uentry_isRefParam (ue2))
1686 return sRef_similarRelaxedAux (s1, s2);
1690 return (ctype_match (s1->type, s2->type));
1694 return sRef_similarRelaxedAux (s1, s2);
1699 sRef_similar (sRef s1, sRef s2)
1701 if (s1 == s2) return TRUE;
1702 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
1704 if (sRef_isConj (s2))
1706 return (sRef_similar (s1, sRef_getConjA (s2)) ||
1707 sRef_similar (s1, sRef_getConjB (s2)));
1710 if (sRef_isDerived (s2))
1712 return (sRef_includedBy (s1, s2->info->ref));
1718 return ((s2->kind == SK_CVAR)
1719 && (cref_equal (s1->info->cvar, s2->info->cvar)));
1721 return ((s2->kind == SK_PARAM)
1722 && (s1->info->paramno == s2->info->paramno));
1724 if (s2->kind == SK_ARRAYFETCH)
1726 if (sRef_similar (s1->info->arrayfetch->arr,
1727 s2->info->arrayfetch->arr))
1729 if (s1->info->arrayfetch->indknown)
1731 if (s2->info->arrayfetch->indknown)
1733 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
1748 if (s2->kind == SK_PTR)
1750 if (sRef_similar (s1->info->arrayfetch->arr,
1760 return ((s2->kind == SK_FIELD
1761 && (sRef_similar (s1->info->field->rec,
1762 s2->info->field->rec)
1763 && cstring_equal (s1->info->field->field,
1764 s2->info->field->field))));
1766 if (s2->kind == SK_PTR)
1768 return sRef_similar (s1->info->ref, s2->info->ref);
1772 if (s2->kind == SK_ARRAYFETCH)
1774 if (sRef_similar (s2->info->arrayfetch->arr,
1784 return ((s2->kind == SK_ADR)
1785 && sRef_similar (s1->info->ref, s2->info->ref));
1787 return ((sRef_similar (s1->info->conj->a, s2) ||
1788 (sRef_similar (s1->info->conj->b, s2))));
1790 return (sRef_includedBy (s2, s1->info->ref));
1791 case SK_UNCONSTRAINED:
1792 return (s2->kind == SK_UNCONSTRAINED
1793 && cstring_equal (s1->info->fname, s2->info->fname));
1803 return (s2->kind == SK_SPECIAL
1804 && (s1->info->spec == s2->info->spec));
1807 /*@notreached@*/ DPRINTF (("Fell through for: %s / %s", sRef_unparse (s1), sRef_unparse (s2)));
1812 ** return TRUE iff small can be derived from big.
1814 ** (e.g. x, x.a is includedBy x;
1815 ** x.a is included By x.a;
1819 sRef_includedBy (sRef small, sRef big)
1821 if (small == big) return TRUE;
1822 if (sRef_isInvalid (small) || sRef_isInvalid (big)) return FALSE;
1824 if (sRef_isConj (big))
1825 return (sRef_similar (small, sRef_getConjA (big)) ||
1826 sRef_similar (small, sRef_getConjB (big)));
1828 switch (small->kind)
1832 return (sRef_same (small, big));
1834 if (big->kind == SK_ARRAYFETCH)
1836 if (sRef_same (small->info->arrayfetch->arr, big->info->arrayfetch->arr))
1838 if (small->info->arrayfetch->indknown)
1840 if (big->info->arrayfetch->indknown)
1842 return (small->info->arrayfetch->ind == big->info->arrayfetch->ind);
1855 return (sRef_includedBy (small->info->arrayfetch->arr, big));
1857 if (big->kind == SK_FIELD)
1860 (sRef_same (small->info->field->rec, big->info->field->rec) &&
1861 cstring_equal (small->info->field->field, big->info->field->field));
1865 return (sRef_includedBy (small->info->field->rec, big));
1869 if (big->kind == SK_PTR)
1871 return sRef_same (small->info->ref, big->info->ref);
1875 return (sRef_includedBy (small->info->ref, big));
1879 return ((big->kind == SK_ADR) && sRef_similar (small->info->ref, big->info->ref));
1881 return ((sRef_includedBy (small->info->conj->a, big) ||
1882 (sRef_includedBy (small->info->conj->b, big))));
1884 return (sRef_includedBy (small->info->ref, big));
1885 case SK_UNCONSTRAINED:
1895 switch (small->info->spec)
1897 case SR_NOTHING: return TRUE;
1899 case SR_INTERNAL: return (sRef_isSpecInternalState (big) ||
1900 sRef_isFileStatic (big));
1901 case SR_SYSTEM: return (sRef_isSystemState (big));
1902 case SR_GLOBALMARKER: BADBRANCH;
1909 ** Same is similar to similar, but not quite the same.
1910 ** same and realSame aren't the same, but they are really similar.
1911 ** similarly, same is the same as same. but realSame is
1912 ** not really the same as same, or similar to similar.
1914 ** Similarly to similar, same checks if two sRefs are the same.
1915 ** The similarities end, however, when same compares arrays
1916 ** with unknown indexes. Similar returns false; same returns true.
1918 ** Similarly to similar and same, realSame is the same as same,
1919 ** except they do not behave the same when face with unknown
1920 ** sRefs. Same thinks they are not the same, but realSame thinks
1926 sRef_realSame (sRef s1, sRef s2)
1928 if (s1 == s2) return TRUE;
1929 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
1934 return ((s2->kind == SK_CVAR) && (cref_equal (s1->info->cvar, s2->info->cvar)));
1936 return ((s2->kind == SK_PARAM) && (s1->info->paramno == s2->info->paramno));
1938 if (s2->kind == SK_ARRAYFETCH)
1940 if (sRef_realSame (s1->info->arrayfetch->arr, s2->info->arrayfetch->arr))
1942 if (s1->info->arrayfetch->indknown && s2->info->arrayfetch->indknown)
1944 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
1946 if (!s1->info->arrayfetch->indknown && !s2->info->arrayfetch->indknown)
1955 return ((s2->kind == SK_FIELD &&
1956 (sRef_realSame (s1->info->field->rec, s2->info->field->rec) &&
1957 cstring_equal (s1->info->field->field, s2->info->field->field))));
1959 return ((s2->kind == SK_PTR) && sRef_realSame (s1->info->ref, s2->info->ref));
1961 return ((s2->kind == SK_ADR) && sRef_realSame (s1->info->ref, s2->info->ref));
1963 return ((sRef_realSame (s1->info->conj->a, s2) ||
1964 (sRef_realSame (s1->info->conj->b, s2))));
1966 return ((s2->kind == SK_OBJECT)
1967 && ctype_match (s1->info->object, s2->info->object));
1969 return ((s2->kind == SK_EXTERNAL)
1970 && sRef_realSame (s1->info->ref, s2->info->ref));
1972 return ((s2->kind == SK_SPECIAL) && s1->info->spec == s2->info->spec);
1974 return ((s2->kind == SK_DERIVED) && sRef_realSame (s1->info->ref, s2->info->ref));
1975 case SK_UNCONSTRAINED:
1976 return ((s2->kind == SK_UNCONSTRAINED)
1977 && (cstring_equal (s1->info->fname, s2->info->fname)));
1983 return TRUE; /* changed this! was false */
1989 sRef_sameObject (sRef s1, sRef s2)
1995 ** same is similar to similar, but not quite the same.
1997 ** Similarly to similar, same checks is two sRefs are the same.
1998 ** The similarities end, however, when same compares arrays
1999 ** with unknown indexes. Similar returns false; same returns true.
2003 sRef_same (sRef s1, sRef s2)
2005 if (s1 == s2) return TRUE;
2006 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
2011 return ((s2->kind == SK_CVAR) && (cref_equal (s1->info->cvar, s2->info->cvar)));
2013 return ((s2->kind == SK_PARAM) && (s1->info->paramno == s2->info->paramno));
2015 if (s2->kind == SK_ARRAYFETCH)
2017 llassert (s1->info->field->rec != s1);
2018 if (sRef_same (s1->info->arrayfetch->arr, s2->info->arrayfetch->arr))
2020 if (s1->info->arrayfetch->indknown && s2->info->arrayfetch->indknown)
2022 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
2030 llassert (s1->info->field->rec != s1);
2031 return ((s2->kind == SK_FIELD &&
2032 (sRef_same (s1->info->field->rec, s2->info->field->rec) &&
2033 cstring_equal (s1->info->field->field, s2->info->field->field))));
2037 llassert (s1->info->ref != s1);
2038 return ((s2->kind == SK_PTR) && sRef_same (s1->info->ref, s2->info->ref));
2042 llassert (s1->info->ref != s1);
2043 return ((s2->kind == SK_ADR) && sRef_same (s1->info->ref, s2->info->ref));
2046 llassert (s1->info->conj->a != s1);
2047 llassert (s1->info->conj->b != s1);
2048 return ((sRef_same (s1->info->conj->a, s2)) && /* or or and? */
2049 (sRef_same (s1->info->conj->b, s2)));
2051 return ((s2->kind == SK_SPECIAL) && s1->info->spec == s2->info->spec);
2053 llassert (s1->info->ref != s1);
2054 return ((s2->kind == SK_DERIVED) && sRef_same (s1->info->ref, s2->info->ref));
2056 case SK_UNCONSTRAINED:
2069 ** sort of similar, for use in def/use
2073 sRef_closeEnough (sRef s1, sRef s2)
2075 if (s1 == s2) return TRUE;
2076 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
2081 return (((s2->kind == SK_CVAR) &&
2082 (cref_equal (s1->info->cvar, s2->info->cvar))) ||
2083 (s2->kind == SK_UNCONSTRAINED && s1->info->cvar->lexlevel == 0));
2084 case SK_UNCONSTRAINED:
2085 return (s2->kind == SK_UNCONSTRAINED
2086 || ((s2->kind == SK_CVAR) && (s2->info->cvar->lexlevel == 0)));
2088 return ((s2->kind == SK_PARAM)
2089 && (s1->info->paramno == s2->info->paramno));
2091 if (s2->kind == SK_ARRAYFETCH)
2093 if (sRef_closeEnough (s1->info->arrayfetch->arr, s2->info->arrayfetch->arr))
2095 if (s1->info->arrayfetch->indknown && s2->info->arrayfetch->indknown)
2097 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
2104 return ((s2->kind == SK_FIELD &&
2105 (sRef_closeEnough (s1->info->field->rec, s2->info->field->rec) &&
2106 cstring_equal (s1->info->field->field, s2->info->field->field))));
2108 return ((s2->kind == SK_PTR) && sRef_closeEnough (s1->info->ref, s2->info->ref));
2110 return ((s2->kind == SK_ADR) && sRef_closeEnough (s1->info->ref, s2->info->ref));
2112 return ((s2->kind == SK_DERIVED) && sRef_closeEnough (s1->info->ref, s2->info->ref));
2114 return ((sRef_closeEnough (s1->info->conj->a, s2)) ||
2115 (sRef_closeEnough (s1->info->conj->b, s2)));
2117 return ((s2->kind == SK_SPECIAL) && s1->info->spec == s2->info->spec);
2133 s is an sRef of a formal paramenter in a function call constraint
2134 we trys to return a constraint expression derived from the actual parementer of a function call.
2136 /*@only@*/ constraintExpr sRef_fixConstraintParam (/*@observer@*/ sRef s, /*@observer@*/ /*@temp@*/ exprNodeList args)
2140 if (sRef_isInvalid (s))
2141 llfatalbug((message("Invalid sRef")));
2147 // s = sRef_saveCopy(s);
2148 ce = constraintExpr_makeTermsRef (s);
2155 temp = (sRef_makeField (sRef_fixBaseParam (s->info->field->rec, args),
2156 s->info->field->field));
2157 ce = constraintExpr_makeTermsRef (temp);
2163 temp = (sRef_makePointer (sRef_fixBaseParam (s->info->ref, args)));
2164 //temp = sRef_saveCopy(temp);
2165 ce = constraintExpr_makeTermsRef (temp);
2172 temp = sRef_saveCopy(s);
2173 temp = sRef_fixBaseParam (temp, args);
2174 ce = constraintExpr_makeTermsRef (temp);
2182 temp = sRef_saveCopy(s);
2183 ce = constraintExpr_makeTermsRef (temp);
2188 llassert(exprNodeList_size (args) > s->info->paramno);
2190 exprNode e = exprNodeList_nth (args, s->info->paramno);
2192 llassert( !(exprNode_isError (e)) );
2193 ce = constraintExpr_makeExprNode (e);
2200 llcontbug ((message("Trying to do fixConstraintParam on nonparam, nonglobal: %q for function with arguments %q", sRef_unparse (s), exprNodeList_unparse(args) ) ));
2201 temp = sRef_saveCopy(s);
2202 ce = constraintExpr_makeTermsRef (temp);
2214 sRef_fixBaseParam (/*@returned@*/ sRef s, exprNodeList args)
2216 if (sRef_isInvalid (s)) return (sRef_undefined);
2220 case SK_UNCONSTRAINED:
2225 if (exprNodeList_size (args) > s->info->paramno)
2227 exprNode e = exprNodeList_nth (args, s->info->paramno);
2229 if (exprNode_isError (e))
2231 return sRef_makeUnknown ();
2234 return (exprNode_getSref (e));
2238 return sRef_makeUnknown ();
2243 if (s->info->arrayfetch->indknown)
2245 return (sRef_makeArrayFetchKnown
2246 (sRef_fixBaseParam (s->info->arrayfetch->arr, args),
2247 s->info->arrayfetch->ind));
2251 return (sRef_makeArrayFetch
2252 (sRef_fixBaseParam (s->info->arrayfetch->arr, args)));
2255 return (sRef_makeField (sRef_fixBaseParam (s->info->field->rec, args),
2256 s->info->field->field));
2259 return (sRef_makePointer (sRef_fixBaseParam (s->info->ref, args)));
2262 return (sRef_makeAddress (sRef_fixBaseParam (s->info->ref, args)));
2265 return (sRef_makeConj (sRef_fixBaseParam (s->info->conj->a, args),
2266 sRef_fixBaseParam (s->info->conj->b, args)));
2282 sRef_undumpGlobal (char **c)
2292 usymId uid = usymId_fromInt (reader_getInt (c));
2297 reader_checkChar (c, '@');
2298 defstate = sstate_fromInt (reader_getInt (c));
2300 reader_checkChar (c, '@');
2301 nullstate = nstate_fromInt (reader_getInt (c));
2303 ret = sRef_makeGlobal (uid, ctype_unknown);
2304 sRef_setNullStateN (ret, nullstate);
2305 ret->defstate = defstate;
2310 int i = reader_getInt (c);
2311 speckind sk = speckind_fromInt (i);
2315 case SR_NOTHING: return (sRef_makeNothing ());
2316 case SR_INTERNAL: return (sRef_makeInternalState ());
2317 case SR_SPECSTATE: return (sRef_makeSpecState ());
2318 case SR_SYSTEM: return (sRef_makeSystemState ());
2319 case SR_GLOBALMARKER: BADBRANCH;
2324 return sRef_undefined;
2326 return sRef_makeUnknown ();
2328 return sRef_makeUnknown ();
2330 llfatalerror (message ("sRef_undumpGlobal: bad line: %s",
2331 cstring_fromChars (*c)));
2337 sRef_undump (char **c)
2346 return (sRef_makeGlobal (usymId_fromInt (reader_getInt (c)), ctype_unknown));
2348 return (sRef_makeParam (reader_getInt (c), ctype_unknown));
2350 return (sRef_makeResultType (ctype_undump (c)));
2353 if ((**c >= '0' && **c <= '9') || **c == '-')
2355 int i = reader_getInt (c);
2356 sRef arr = sRef_undump (c);
2357 sRef ret = sRef_buildArrayFetchKnown (arr, i);
2363 sRef arr = sRef_undump (c);
2364 sRef ret = sRef_buildArrayFetch (arr);
2371 cstring fname = cstring_undefined;
2376 fname = cstring_appendChar (fname, **c);
2381 ret = sRef_buildField (sRef_undump (c), fname);
2382 cstring_markOwned (fname);
2387 int i = reader_getInt (c);
2388 speckind sk = speckind_fromInt (i);
2392 case SR_NOTHING: return (sRef_makeNothing ());
2393 case SR_INTERNAL: return (sRef_makeInternalState ());
2394 case SR_SPECSTATE: return (sRef_makeSpecState ());
2395 case SR_SYSTEM: return (sRef_makeSystemState ());
2396 case SR_GLOBALMARKER: BADBRANCH;
2402 sRef ptr = sRef_undump (c);
2403 sRef ret = sRef_makePointer (ptr);
2409 sRef adr = sRef_undump (c);
2410 sRef ret = sRef_makeAddress (adr);
2416 return (sRef_makeObject (ctype_undump (c)));
2420 sRef s1 = sRef_undump (c);
2421 sRef s2 = ((*c)++, sRef_undump (c));
2422 sRef ret = sRef_makeConj (s1, s2);
2427 return sRef_undefined;
2429 return sRef_makeUnknown ();
2431 return sRef_makeUnknown ();
2433 llfatalerror (message ("sRef_undump: bad line: %s", cstring_fromChars (*c)));
2441 if (sRef_isInvalid (s))
2443 return (cstring_makeLiteral ("-"));
2450 return (message ("p%d", s->info->paramno));
2452 if (s->info->arrayfetch->indknown)
2454 return (message ("a%d%q", s->info->arrayfetch->ind,
2455 sRef_dump (s->info->arrayfetch->arr)));
2459 return (message ("a%q", sRef_dump (s->info->arrayfetch->arr)));
2462 return (message ("f%s.%q", s->info->field->field,
2463 sRef_dump (s->info->field->rec)));
2465 return (message ("t%q", sRef_dump (s->info->ref)));
2467 return (message ("d%q", sRef_dump (s->info->ref)));
2469 return (message ("o%q", ctype_dump (s->info->object)));
2471 return (message ("s%d", (int) s->info->spec));
2473 return (message ("c%q.%q",
2474 sRef_dump (s->info->conj->a),
2475 sRef_dump (s->info->conj->b)));
2477 if (sRef_isFileOrGlobalScope (s))
2479 return (message ("g%d",
2480 usymtab_convertId (s->info->cvar->index)));
2484 llcontbug (message ("Dumping local variable: %q",
2485 sRef_unparseDebug (s)));
2486 return (cstring_makeLiteral ("u"));
2489 return (cstring_makeLiteral ("u"));
2491 return (message ("r%q", ctype_dump (s->type)));
2497 case SK_UNCONSTRAINED:
2498 llcontbug (message ("sRef_dump: bad kind: %q",
2499 sRef_unparseFull (s)));
2500 return (cstring_makeLiteral ("x"));
2507 cstring sRef_dumpGlobal (sRef s)
2509 if (sRef_isInvalid (s))
2511 return (cstring_makeLiteral ("-"));
2518 if (sRef_isFileOrGlobalScope (s))
2520 return (message ("g%d@%d@%d",
2521 usymtab_convertId (s->info->cvar->index),
2523 (int) sRef_getNullState (s)));
2527 llcontbug (message ("Dumping local variable: %q",
2528 sRef_unparseDebug (s)));
2529 return (cstring_makeLiteral ("u"));
2532 return (cstring_makeLiteral ("u"));
2534 return (message ("s%d", (int) s->info->spec));
2536 llcontbug (message ("sRef_dumpGlobal: bad kind: %q",
2537 sRef_unparseFull (s)));
2538 return (cstring_makeLiteral ("x"));
2546 sRef_deriveType (sRef s, uentryList cl)
2548 if (sRef_isInvalid (s)) return ctype_unknown;
2553 return (uentry_getType (usymtab_getRefQuiet (s->info->cvar->lexlevel,
2554 s->info->cvar->index)));
2555 case SK_UNCONSTRAINED:
2556 return (ctype_unknown);
2558 if (s->info->paramno >= 0)
2560 return uentry_getType (uentryList_getN (cl, s->info->paramno));
2564 return ctype_unknown;
2568 ctype ca = sRef_deriveType (s->info->arrayfetch->arr, cl);
2570 if (ctype_isArray (ca))
2572 return (ctype_baseArrayPtr (ca));
2574 else if (ctype_isUnknown (ca))
2580 llcontbuglit ("sRef_deriveType: inconsistent array type");
2586 ctype ct = sRef_deriveType (s->info->field->rec, cl);
2588 if (ctype_isStructorUnion (ct))
2590 uentry ue = uentryList_lookupField (ctype_getFields (ct),
2591 s->info->field->field);
2593 if (uentry_isValid (ue))
2595 return (uentry_getType (ue));
2599 llcontbuglit ("sRef_deriveType: bad field");
2600 return ctype_unknown;
2603 else if (ctype_isUnknown (ct))
2609 llcontbuglit ("sRef_deriveType: inconsistent field type");
2615 ctype ct = sRef_deriveType (s->info->ref, cl);
2617 if (ctype_isUnknown (ct)) return ct;
2618 if (ctype_isPointer (ct)) return (ctype_baseArrayPtr (ct));
2621 llcontbuglit ("sRef_deriveType: inconsistent pointer type");
2627 ctype ct = sRef_deriveType (s->info->ref, cl);
2629 if (ctype_isUnknown (ct)) return ct;
2630 return ctype_makePointer (ct);
2634 return sRef_deriveType (s->info->ref, cl);
2638 return (s->info->object);
2642 return (ctype_makeConj (sRef_deriveType (s->info->conj->a, cl),
2643 sRef_deriveType (s->info->conj->b, cl)));
2655 return ctype_unknown;
2661 sRef_getType (sRef s)
2663 if (sRef_isInvalid (s)) return ctype_unknown;
2669 sRef_unparseOpt (sRef s)
2671 sRef rb = sRef_getRootBase (s);
2673 if (sRef_isMeaningful (rb) && !sRef_isConst (rb))
2675 cstring ret = sRef_unparse (s);
2677 llassertprint (!cstring_equalLit (ret, "?"), ("print: %s", sRef_unparseDebug (s)));
2679 if (!cstring_isEmpty (ret))
2681 return (cstring_appendChar (ret, ' '));
2689 return cstring_undefined;
2693 sRef_unparsePreOpt (sRef s)
2695 sRef rb = sRef_getRootBase (s);
2697 if (sRef_isMeaningful (rb) && !sRef_isConst (rb))
2699 cstring ret = sRef_unparse (s);
2701 llassertprint (!cstring_equalLit (ret, "?"), ("print: %s", sRef_unparseDebug (s)));
2702 return (cstring_prependCharO (' ', ret));
2705 return cstring_undefined;
2709 sRef_unparse (sRef s)
2711 if (sRef_isInvalid (s)) return (cstring_makeLiteral ("?"));
2713 if (context_inFunctionLike ())
2715 return (sRef_unparseWithArgs (s, context_getParams ()));
2719 DPRINTF (("Not in function like: %s", context_unparse ()));
2720 return (sRef_unparseNoArgs (s));
2724 static /*@only@*/ cstring
2725 sRef_unparseWithArgs (sRef s, uentryList args)
2727 if (sRef_isInvalid (s))
2729 return (cstring_makeLiteral ("?"));
2735 return (uentry_getName (usymtab_getRefQuiet (s->info->cvar->lexlevel,
2736 s->info->cvar->index)));
2737 case SK_UNCONSTRAINED:
2738 return (cstring_copy (s->info->fname));
2741 if (s->info->paramno < uentryList_size (args)
2742 && s->info->paramno >= 0)
2744 uentry ue = uentryList_getN (args, s->info->paramno);
2746 if (uentry_isValid (ue))
2747 return uentry_getName (ue);
2750 return (message ("<bad param: %q / args %q",
2751 sRef_unparseDebug (s),
2752 uentryList_unparse (args)));
2755 if (s->info->arrayfetch->indknown)
2757 return (message ("%q[%d]", sRef_unparseWithArgs (s->info->arrayfetch->arr, args),
2758 s->info->arrayfetch->ind));
2762 return (message ("%q[]", sRef_unparseWithArgs (s->info->arrayfetch->arr, args)));
2765 if (s->info->field->rec->kind == SK_PTR)
2767 sRef ptr = s->info->field->rec;
2769 return (message ("%q->%s", sRef_unparseWithArgs (ptr->info->ref, args),
2770 s->info->field->field));
2772 return (message ("%q.%s", sRef_unparseWithArgs (s->info->field->rec, args),
2773 s->info->field->field));
2777 sRef ref = sRef_fixConj (s->info->ref);
2778 skind sk = ref->kind;
2783 ret = message ("storage pointed to by %q",
2784 sRef_unparseWithArgs (ref, args));
2786 else if (skind_isSimple (sk) || sk == SK_PTR)
2788 ret = message ("*%q", sRef_unparseWithArgs (ref, args));
2792 ret = message ("*(%q)", sRef_unparseWithArgs (ref, args));
2798 return (message ("&%q", sRef_unparseWithArgs (s->info->ref, args)));
2800 return (cstring_copy (ctype_unparse (s->info->object)));
2802 return (sRef_unparseWithArgs (sRef_getConjA (s), args));
2804 if (cstring_isDefined (s->info->fname))
2806 return (message ("[result of %s]", s->info->fname));
2810 return (cstring_makeLiteral ("<new>"));
2813 return (cstring_makeLiteral ("?"));
2815 return (message ("<derived %q>", sRef_unparse (s->info->ref)));
2817 return (message ("<external %q>", sRef_unparse (s->info->ref)));
2819 return (message ("<type %s>", ctype_unparse (s->type)));
2821 return (message ("<const %s>", ctype_unparse (s->type)));
2823 switch (s->info->spec)
2825 case SR_NOTHING: return cstring_makeLiteral ("nothing");
2826 case SR_INTERNAL: return cstring_makeLiteral ("internal state");
2827 case SR_SPECSTATE: return cstring_makeLiteral ("spec state");
2828 case SR_SYSTEM: return cstring_makeLiteral ("file system state");
2829 case SR_GLOBALMARKER: return cstring_makeLiteral ("<global marker>");
2833 return cstring_makeLiteral ("result");
2836 llbug (message ("Bad sref, kind = %d", (int) s->kind));
2844 sRef_unparseDebug (sRef s)
2846 if (sRef_isInvalid (s))
2848 return (cstring_makeLiteral ("<undef>"));
2854 case SK_UNCONSTRAINED:
2855 return (message ("<unconstrained %s>", s->info->fname));
2860 ce = usymtab_getRefQuiet (s->info->cvar->lexlevel, s->info->cvar->index);
2862 if (uentry_isInvalid (ce))
2864 return (message ("<scope: %d.%d *invalid*>",
2865 s->info->cvar->lexlevel,
2866 s->info->cvar->index));
2870 return (message ("<scope: %d.%d *%q*>",
2871 s->info->cvar->lexlevel,
2872 s->info->cvar->index,
2873 uentry_getName (ce)));
2879 return (message ("<parameter %d>", s->info->paramno + 1));
2882 if (s->info->arrayfetch->indknown)
2884 return (message ("%q[%d]", sRef_unparseDebug (s->info->arrayfetch->arr),
2885 s->info->arrayfetch->ind));
2889 return (message ("%q[]", sRef_unparseDebug (s->info->arrayfetch->arr)));
2892 return (message ("%q.%s", sRef_unparseDebug (s->info->field->rec),
2893 s->info->field->field));
2895 if (sRef_isField (s->info->ref))
2897 sRef fld = s->info->ref;
2899 return (message ("%q->%s", sRef_unparseDebug (fld->info->field->rec),
2900 fld->info->field->field));
2904 return (message ("*(%q)", sRef_unparseDebug (s->info->ref)));
2907 return (message ("&%q", sRef_unparseDebug (s->info->ref)));
2909 return (message ("<object type %s>", ctype_unparse (s->info->object)));
2911 return (message ("%q | %q", sRef_unparseDebug (s->info->conj->a),
2912 sRef_unparseDebug (s->info->conj->b)));
2914 return message ("<new: %s>", s->info->fname);
2916 return (message ("<derived %q>", sRef_unparseDebug (s->info->ref)));
2918 return (message ("<external %q>", sRef_unparseDebug (s->info->ref)));
2920 return (message ("<type %s>", ctype_unparse (s->type)));
2922 return (message ("<const %s>", ctype_unparse (s->type)));
2924 return (message ("<result %s>", ctype_unparse (s->type)));
2926 return (message ("<spec %s>",
2927 cstring_makeLiteralTemp
2928 (s->info->spec == SR_NOTHING ? "nothing"
2929 : s->info->spec == SR_INTERNAL ? "internalState"
2930 : s->info->spec == SR_SPECSTATE ? "spec state"
2931 : s->info->spec == SR_SYSTEM ? "fileSystem"
2934 return cstring_makeLiteral ("<unknown>");
2940 static /*@only@*/ cstring
2941 sRef_unparseNoArgs (sRef s)
2943 if (sRef_isInvalid (s)) return (cstring_makeLiteral ("?"));
2947 case SK_UNCONSTRAINED:
2948 return (cstring_copy (s->info->fname));
2951 uentry ce = usymtab_getRefQuiet (s->info->cvar->lexlevel,
2952 s->info->cvar->index);
2954 if (uentry_isInvalid (ce))
2956 llcontbug (message ("sRef_unparseNoArgs: bad cvar: %q",
2957 sRef_unparseDebug (s)));
2958 return (sRef_unparseDebug (s));
2962 return (uentry_getName (ce));
2966 if (s->info->arrayfetch->indknown)
2968 return (message ("%q[%d]", sRef_unparseNoArgs (s->info->arrayfetch->arr),
2969 s->info->arrayfetch->ind));
2973 return (message ("%q[]", sRef_unparseNoArgs (s->info->arrayfetch->arr)));
2976 return (message ("%q.%s", sRef_unparseNoArgs (s->info->field->rec),
2977 s->info->field->field));
2980 sRef ref = sRef_fixConj (s->info->ref);
2981 skind sk = ref->kind;
2984 if (skind_isSimple (sk) || sk == SK_PTR)
2986 ret = message ("*%q", sRef_unparseNoArgs (ref));
2990 ret = message ("*(%q)", sRef_unparseNoArgs (ref));
2996 return (message ("&%q", sRef_unparseNoArgs (s->info->ref)));
2998 return (cstring_copy (ctype_unparse (s->info->object)));
3000 return (sRef_unparseNoArgs (s->info->conj->a));
3002 return (message ("result of %s", s->info->fname));
3004 return (message ("<der %q>", sRef_unparseNoArgs (s->info->ref)));
3006 return message ("<ext %q>", sRef_unparseNoArgs (s->info->ref));
3008 return (cstring_makeLiteral
3009 (s->info->spec == SR_NOTHING ? "nothing"
3010 : s->info->spec == SR_INTERNAL ? "internal state"
3011 : s->info->spec == SR_SPECSTATE ? "spec state"
3012 : s->info->spec == SR_SYSTEM ? "file system state"
3015 return cstring_makeLiteral ("result");
3019 return cstring_makeLiteral ("?");
3021 /* llcontbug (message ("sRef_unparseNoArgs: bad case: %q", sRef_unparseDebug (s))); */
3022 return (sRef_unparseDebug (s));
3027 /*@dependent@*/ sRef sRef_makeUnconstrained (cstring fname)
3029 sRef s = sRef_new ();
3031 s->kind = SK_UNCONSTRAINED;
3032 s->info = (sinfo) dmalloc (sizeof (*s->info));
3033 s->info->fname = fname;
3038 cstring sRef_unconstrainedName (sRef s)
3040 llassert (sRef_isUnconstrained (s));
3042 return s->info->fname;
3045 bool sRef_isUnconstrained (sRef s)
3047 return (sRef_isValid(s) && s->kind == SK_UNCONSTRAINED);
3050 static /*@dependent@*/ /*@notnull@*/ sRef
3051 sRef_makeCvarAux (int level, usymId index, ctype ct)
3053 sRef s = sRef_newRef ();
3056 s->info = (sinfo) dmalloc (sizeof (*s->info));
3058 s->info->cvar = (cref) dmalloc (sizeof (*s->info->cvar));
3059 s->info->cvar->lexlevel = level;
3060 s->info->cvar->index = index;
3062 /* for now, all globals are defined; all locals, aren't */
3064 if (level <= fileScope)
3066 s->defstate = SS_UNKNOWN;
3070 ctype rct = ctype_realType (ct);
3072 if (level != paramsScope
3073 && (ctype_isStructorUnion (rct) || ctype_isRealArray (rct)))
3075 s->defstate = SS_ALLOCATED;
3076 s->oaliaskind = s->aliaskind = AK_STACK;
3080 s->defstate = SS_UNDEFINED;
3081 s->oaliaskind = s->aliaskind = AK_LOCAL;
3087 llassert (level >= globScope);
3088 llassert (usymId_isValid (index));
3090 DPRINTF (("Made cvar: [%p] %s", s, sRef_unparseDebug (s)));
3091 llassert (valueTable_isUndefined (s->state));
3092 s->state = context_createValueTable (s);
3096 /*@dependent@*/ sRef sRef_makeCvar (int level, usymId index, ctype ct)
3098 return (sRef_makeCvarAux (level, index, ct));
3101 int sRef_lexLevel (sRef s)
3103 if (sRef_isValid (s))
3107 conj = sRef_fixConj (s);
3108 s = sRef_getRootBase (conj);
3110 if (sRef_isValid (s) && s->kind == SK_CVAR)
3112 return (s->info->cvar->lexlevel);
3120 sRef_makeGlobal (usymId l, ctype ct)
3122 return (sRef_makeCvar (globScope, l, ct));
3126 sRef_setParamNo (sRef s, int l)
3128 llassert (sRef_isValid (s) && s->kind == SK_PARAM);
3129 s->info->paramno = l;
3133 /*@dependent@*/ sRef
3134 sRef_makeParam (int l, ctype ct)
3136 sRef s = sRef_new ();
3141 s->info = (sinfo) dmalloc (sizeof (*s->info));
3142 s->info->paramno = l;
3144 s->defstate = SS_UNKNOWN;
3145 /* (probably defined, unless its an out parameter) */
3147 llassert (valueTable_isUndefined (s->state));
3148 s->state = context_createValueTable (s);
3153 sRef_isIndexKnown (sRef arr)
3157 llassert (sRef_isValid (arr));
3158 arr = sRef_fixConj (arr);
3160 llassert (arr->kind == SK_ARRAYFETCH);
3161 res = arr->info->arrayfetch->indknown;
3166 sRef_getIndex (sRef arr)
3170 llassert (sRef_isValid (arr));
3171 arr = sRef_fixConj (arr);
3173 llassert (arr->kind == SK_ARRAYFETCH);
3175 if (!arr->info->arrayfetch->indknown)
3177 llcontbug (message ("sRef_getIndex: unknown: %q", sRef_unparse (arr)));
3182 result = arr->info->arrayfetch->ind;
3188 static bool sRef_isZerothArrayFetch (/*@notnull@*/ sRef s)
3190 return (s->kind == SK_ARRAYFETCH
3191 && s->info->arrayfetch->indknown
3192 && (s->info->arrayfetch->ind == 0));
3195 /*@exposed@*/ sRef sRef_makeAddress (/*@exposed@*/ sRef t)
3198 if (sRef_isInvalid (t)) return sRef_undefined;
3200 if (sRef_isPointer (t))
3202 return (t->info->ref);
3204 else if (sRef_isZerothArrayFetch (t))
3206 return (t->info->arrayfetch->arr);
3210 sRef s = sRef_newRef ();
3213 s->type = ctype_makePointer (t->type);
3214 s->info = (sinfo) dmalloc (sizeof (*s->info));
3215 s->info->ref = t; /* sRef_copy (t); */ /*@i32@*/
3217 if (t->defstate == SS_UNDEFINED)
3218 /* no! it is allocated even still: && !ctype_isPointer (t->type)) */
3220 s->defstate = SS_ALLOCATED;
3224 s->defstate = t->defstate;
3227 if (t->aliaskind == AK_LOCAL)
3229 if (sRef_isLocalVar (t))
3231 s->aliaskind = AK_STACK;
3235 llassert (valueTable_isUndefined (s->state));
3236 s->state = context_createValueTable (s);
3241 cstring sRef_getField (sRef s)
3245 llassert (sRef_isValid (s));
3246 s = sRef_fixConj (s);
3248 llassertprint (sRef_isValid (s) && (s->kind == SK_FIELD),
3249 ("s = %s", sRef_unparseDebug (s)));
3251 res = s->info->field->field;
3255 sRef sRef_getBase (sRef s)
3259 if (sRef_isInvalid (s)) return (sRef_undefined);
3261 s = sRef_fixConj (s);
3272 res = s->info->field->rec;
3276 res = s->info->arrayfetch->arr;
3280 res = sRef_undefined; /* shouldn't need it */
3287 ** same as getBase, except returns invalid
3288 ** (and doesn't use adr's)
3292 sRef_getBaseSafe (sRef s)
3296 if (sRef_isInvalid (s)) { return sRef_undefined; }
3298 s = sRef_fixConj (s);
3306 res = s->info->field->rec; break;
3308 res = s->info->arrayfetch->arr;
3311 res = sRef_undefined; break;
3317 /*@constant int MAXBASEDEPTH;@*/
3318 # define MAXBASEDEPTH 25
3320 static /*@exposed@*/ sRef
3321 sRef_getRootBaseAux (sRef s, int depth)
3323 if (sRef_isInvalid (s)) return sRef_undefined;
3325 if (depth > MAXBASEDEPTH)
3328 ("Warning: reference base limit exceeded for %q. "
3329 "This either means there is a variable with at least "
3330 "%d indirections from this reference, or "
3331 "there is a bug in LCLint.",
3336 return sRef_undefined;
3343 return (sRef_getRootBaseAux (s->info->ref, depth + 1));
3345 return (sRef_getRootBaseAux (s->info->field->rec, depth + 1));
3347 return (sRef_getRootBaseAux (s->info->arrayfetch->arr, depth + 1));
3349 return (sRef_getRootBaseAux (sRef_fixConj (s), depth + 1));
3355 sRef sRef_getRootBase (sRef s)
3357 return (sRef_getRootBaseAux (s, 0));
3360 static bool sRef_isDeep (sRef s)
3362 if (sRef_isInvalid (s)) return FALSE;
3372 return (sRef_isDeep (sRef_fixConj (s)));
3378 static int sRef_depth (sRef s)
3380 if (sRef_isInvalid (s)) return 0;
3388 return 1 + sRef_depth (s->info->ref);
3390 return 1 + sRef_depth (s->info->field->rec);
3392 return 1 + sRef_depth (s->info->arrayfetch->arr);
3394 return (sRef_depth (sRef_fixConj (s)));
3401 sRef_makeObject (ctype o)
3403 sRef s = sRef_newRef (); /*@i423 same line is bad...@*/
3405 s->kind = SK_OBJECT;
3406 s->info = (sinfo) dmalloc (sizeof (*s->info));
3407 s->info->object = o;
3408 llassert (valueTable_isUndefined (s->state));
3409 s->state = context_createValueTable (s);
3414 ** This is used to represent storage referenced by a parameter.
3417 sRef sRef_makeExternal (sRef t)
3419 sRef s = sRef_newRef ();
3421 llassert (sRef_isValid (t));
3423 s->kind = SK_EXTERNAL;
3424 s->info = (sinfo) dmalloc (sizeof (*s->info));
3426 s->info->ref = t; /* sRef_copy (t); */ /*@i32 was exposed@*/
3427 llassert (valueTable_isUndefined (s->state));
3428 s->state = context_createValueTable (s);
3432 /*@dependent@*/ sRef sRef_makeDerived (/*@exposed@*/ sRef t)
3434 if (sRef_isValid (t))
3436 sRef s = sRef_newRef ();
3438 s->kind = SK_DERIVED;
3439 s->info = (sinfo) dmalloc (sizeof (*s->info));
3440 s->info->ref = t; /* sRef_copy (t); */ /*@i32@*/
3443 llassert (valueTable_isUndefined (s->state));
3444 s->state = context_createValueTable (s);
3449 return sRef_undefined;
3454 ** definitely NOT symmetric:
3456 ** res fills in unknown state information from other
3460 sRef_mergeStateQuiet (sRef res, sRef other)
3462 llassert (sRef_isValid (res));
3463 llassert (sRef_isValid (other));
3465 res->modified = res->modified || other->modified;
3466 res->safe = res->safe && other->safe;
3468 if (res->defstate == SS_UNKNOWN)
3470 res->defstate = other->defstate;
3471 res->definfo = stateInfo_update (res->definfo, other->definfo);
3474 if (res->aliaskind == AK_UNKNOWN ||
3475 (res->aliaskind == AK_LOCAL && alkind_isKnown (other->aliaskind)))
3477 res->aliaskind = other->aliaskind;
3478 res->oaliaskind = other->oaliaskind;
3479 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
3482 if (res->expkind == XO_UNKNOWN)
3484 res->expkind = other->expkind;
3485 res->oexpkind = other->oexpkind;
3486 res->expinfo = stateInfo_update (res->expinfo, other->expinfo);
3489 /* out takes precedence over implicitly defined */
3490 if (res->defstate == SS_DEFINED && other->defstate != SS_UNKNOWN)
3492 res->defstate = other->defstate;
3493 res->definfo = stateInfo_update (res->definfo, other->definfo);
3496 if (sRef_getNullState (other) == NS_ERROR || sRef_getNullState (res) == NS_ERROR)
3498 sRef_setNullState (res, NS_ERROR, fileloc_undefined);
3502 if (sRef_getNullState (other) != NS_UNKNOWN
3503 && (sRef_getNullState (res) == NS_UNKNOWN || sRef_getNullState (res) == NS_NOTNULL
3504 || sRef_getNullState (res) == NS_MNOTNULL))
3506 sRef_updateNullState (res, other);
3512 ** definitely NOT symmetric:
3514 ** res fills in known state information from other
3518 sRef_mergeStateQuietReverse (/*@dependent@*/ sRef res, /*@dependent@*/ sRef other)
3520 bool changed = FALSE;
3522 llassert (sRef_isValid (res));
3523 llassert (sRef_isValid (other));
3524 sRef_checkMutable (res);
3526 if (res->kind != other->kind)
3532 res->kind = other->kind;
3533 res->type = other->type;
3534 res->info = sinfo_fullCopy (other);
3538 if (!ctype_equal (res->type, other->type))
3541 res->type = other->type;
3544 sinfo_update (res, other);
3547 res->modified = res->modified || other->modified;
3548 res->safe = res->safe && other->safe;
3550 if (res->aliaskind != other->aliaskind
3551 && (res->aliaskind == AK_UNKNOWN
3552 || ((res->aliaskind == AK_LOCAL
3553 || (res->aliaskind == AK_REFCOUNTED
3554 && other->aliaskind != AK_LOCAL))
3555 && other->aliaskind != AK_UNKNOWN)))
3558 res->aliaskind = other->aliaskind;
3559 res->oaliaskind = other->oaliaskind;
3560 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
3563 if (other->expkind != XO_UNKNOWN && other->expkind != res->expkind)
3566 res->expkind = other->expkind;
3567 res->expinfo = stateInfo_update (res->expinfo, other->expinfo);
3570 if (other->oexpkind != XO_UNKNOWN)
3572 res->oexpkind = other->oexpkind;
3575 /* out takes precedence over implicitly defined */
3577 if (res->defstate != other->defstate)
3579 if (other->defstate != SS_UNKNOWN)
3581 res->defstate = other->defstate;
3585 if (sRef_getNullState (other) == NS_ERROR || sRef_getNullState (res) == NS_ERROR)
3587 if (sRef_getNullState (res) != NS_ERROR)
3589 sRef_setNullStateN (res, NS_ERROR);
3595 if (sRef_getNullState (other) != NS_UNKNOWN && sRef_getNullState (other) != sRef_getNullState (res))
3598 sRef_updateNullState (res, other);
3604 sRef_clearDerived (res);
3609 sRef_mergeState (sRef res, sRef other, clause cl, fileloc loc)
3611 if (sRef_isValid (res) && sRef_isValid (other))
3613 sRef_mergeStateAux (res, other, cl, FALSE, loc, TRUE);
3617 if (sRef_isInvalid (res))
3619 llbug (message ("sRef_mergeState: invalid res sRef: %q",
3620 sRef_unparseDebug (other)));
3624 llbug (message ("sRef_mergeState: invalid other sRef: %q",
3625 sRef_unparseDebug (res)));
3631 sRef_mergeOptState (sRef res, sRef other, clause cl, fileloc loc)
3633 if (sRef_isValid (res) && sRef_isValid (other))
3635 sRef_mergeStateAux (res, other, cl, TRUE, loc, TRUE);
3639 if (sRef_isInvalid (res))
3641 llbug (message ("sRef_mergeOptState: invalid res sRef: %q",
3642 sRef_unparseDebug (other)));
3646 llbug (message ("sRef_mergeOptState: invalid other sRef: %q",
3647 sRef_unparseDebug (res)));
3653 sRef_mergeStateAux (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other,
3654 clause cl, bool opt, fileloc loc,
3658 llassertfatal (sRef_isValid (res));
3659 llassertfatal (sRef_isValid (other));
3661 sRef_checkMutable (res);
3662 sRef_checkMutable (other);
3664 res->modified = res->modified || other->modified;
3666 if (res->kind == other->kind
3667 || (other->kind == SK_UNKNOWN || res->kind == SK_UNKNOWN))
3669 sstate odef = other->defstate;
3670 sstate rdef = res->defstate;
3671 nstate onull = sRef_getNullState (other);
3674 ** yucky stuff to handle
3679 if (other->defstate == SS_DEAD
3680 && ((sRef_isOnly (res) && sRef_definitelyNull (res))
3681 || (res->defstate == SS_UNDEFINED
3682 || res->defstate == SS_UNUSEABLE)))
3684 if (res->defstate == SS_UNDEFINED
3685 || res->defstate == SS_UNUSEABLE)
3687 res->defstate = SS_UNUSEABLE;
3691 res->defstate = SS_DEAD;
3694 res->definfo = stateInfo_update (res->definfo, other->definfo);
3695 sRef_clearDerived (other);
3696 sRef_clearDerived (res);
3698 else if (res->defstate == SS_DEAD
3699 && ((sRef_isOnly (other) && sRef_definitelyNull (other))
3700 || (other->defstate == SS_UNDEFINED
3701 || other->defstate == SS_UNUSEABLE)))
3703 if (other->defstate == SS_UNDEFINED
3704 || other->defstate == SS_UNUSEABLE)
3706 res->defstate = SS_UNUSEABLE;
3710 res->defstate = SS_DEAD;
3713 sRef_clearDerived (other);
3714 sRef_clearDerived (res);
3716 else if (res->defstate == SS_DEFINED
3717 && (other->defstate == SS_ALLOCATED
3718 && sRef_definitelyNull (other)))
3720 other->defstate = SS_DEFINED; /* definitely null! */
3722 else if (other->defstate == SS_DEFINED
3723 && (res->defstate == SS_ALLOCATED && sRef_definitelyNull (res)))
3725 res->defstate = SS_DEFINED;
3726 res->definfo = stateInfo_update (res->definfo, other->definfo);
3733 if (res->defstate == SS_DEAD && other->defstate == SS_DEAD)
3735 sRef_clearDerived (other);
3736 sRef_clearDerived (res);
3740 ** only & dead isn't really an only!
3743 if (alkind_isOnly (other->aliaskind) && other->defstate == SS_DEAD)
3745 other->aliaskind = AK_UNKNOWN;
3748 if (alkind_isOnly (res->aliaskind) && res->defstate == SS_DEAD)
3750 res->aliaskind = AK_UNKNOWN;
3754 ** Dead and dependent -> dead
3757 if (alkind_isDependent (other->aliaskind) && res->defstate == SS_DEAD)
3759 other->aliaskind = AK_UNKNOWN;
3760 other->defstate = SS_DEAD;
3761 sRef_clearDerived (res);
3762 sRef_clearDerived (other);
3765 if (alkind_isDependent (res->aliaskind) && other->defstate == SS_DEAD)
3767 res->aliaskind = AK_UNKNOWN;
3768 res->defstate = SS_DEAD;
3769 sRef_clearDerived (res);
3770 sRef_clearDerived (other);
3774 ** must do alias combine first, since it depends on
3775 ** original values of state and null.
3778 sRef_combineAliasKinds (res, other, cl, loc);
3779 sRef_combineDefState (res, other);
3780 sRef_combineNullState (res, other);
3782 if (rdef == SS_ALLOCATED || rdef == SS_SPECIAL)
3784 if (odef == SS_DEFINED)
3786 if (onull == NS_DEFNULL || onull == NS_CONSTNULL)
3788 res->deriv = sRefSet_copyInto (res->deriv, other->deriv);
3789 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
3792 else if (odef == SS_ALLOCATED || odef == SS_SPECIAL)
3797 if (ctype_isUnion (ctype_realType (sRef_getType (res))))
3799 res->deriv = sRef_mergeUnionDerivs (res->deriv,
3802 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
3806 res->deriv = sRef_mergeDerivs (res->deriv, other->deriv,
3808 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
3816 res->deriv = sRef_mergeDerivs (res->deriv, other->deriv,
3818 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
3828 if (rdef == SS_PDEFINED
3829 || (rdef == SS_DEFINED && odef == SS_PDEFINED))
3833 res->deriv = sRef_mergePdefinedDerivs (res->deriv, other->deriv,
3835 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
3840 if ((rdef == SS_DEFINED || rdef == SS_UNKNOWN)
3841 && res->defstate == SS_ALLOCATED)
3843 res->deriv = sRefSet_copyInto (res->deriv, other->deriv);
3849 res->deriv = sRef_mergeDerivs (res->deriv, other->deriv,
3851 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
3858 sRef_combineExKinds (res, other);
3862 if (res->kind == SK_ARRAYFETCH && other->kind == SK_PTR)
3864 sRef nother = sRef_buildArrayFetchKnown (sRef_getBase (other), 0);
3866 sRef_copyState (nother, other);
3867 sRef_mergeStateAux (res, nother, cl, opt, loc, doDerivs);
3869 else if (res->kind == SK_PTR && other->kind == SK_ARRAYFETCH)
3871 sRef nother = sRef_buildPointer (sRef_getBase (other));
3873 if (sRef_isValid (nother))
3875 sRef_copyState (nother, other);
3876 sRef_mergeStateAux (res, nother, cl, opt, loc, doDerivs);
3881 llcontbug (message ("merge conj: %q / %q", sRef_unparseFull (res),
3882 sRef_unparseFull (other)));
3888 ** Merge value table states
3891 valueTable_elements (res->state, key, sv)
3893 stateValue os = valueTable_lookup (other->state, key);
3894 /*@unused@*/ int val;
3895 /*@unused@*/ char *msg;
3897 llassert (stateValue_isDefined (os));
3899 DPRINTF (("Merge state: %s / %s",
3900 cstring_toCharsSafe (stateValue_unparse (sv)),
3901 cstring_toCharsSafe (stateValue_unparse (os))));
3903 val = valueMatix_lookup (key,
3904 stateValue_getValue (os),
3905 stateValue_getValue (sv),
3907 DPRINTF (("Val: %d / %s", val, msg));
3909 } end_valueTable_elements ;
3913 sRef_mergeUnionDerivs (/*@only@*/ sRefSet res,
3914 /*@exposed@*/ sRefSet other, bool opt,
3915 clause cl, fileloc loc)
3917 if (sRefSet_isEmpty (res))
3919 return sRefSet_copyInto (res, other);
3923 sRefSet_allElements (other, el)
3925 if (sRef_isValid (el))
3927 sRef e2 = sRefSet_lookupMember (other, el);
3929 if (sRef_isValid (e2))
3931 sRef_mergeStateAux (el, e2, cl, opt, loc, FALSE);
3935 res = sRefSet_insert (res, el);
3938 } end_sRefSet_allElements ;
3944 static /*@only@*/ sRefSet
3945 sRef_mergeDerivs (/*@only@*/ sRefSet res, sRefSet other,
3946 bool opt, clause cl, fileloc loc)
3948 sRefSet ret = sRefSet_new ();
3950 sRefSet_allElements (res, el)
3952 if (sRef_isValid (el))
3954 sRef e2 = sRefSet_lookupMember (other, el);
3956 if (sRef_isValid (e2))
3958 if (el->defstate == SS_ALLOCATED &&
3959 e2->defstate == SS_PDEFINED)
3961 e2->defstate = SS_ALLOCATED;
3963 else if (e2->defstate == SS_ALLOCATED &&
3964 el->defstate == SS_PDEFINED)
3966 el->defstate = SS_ALLOCATED;
3967 sRef_clearDerived (el);
3969 else if ((el->defstate == SS_DEAD || sRef_isKept (el)) &&
3970 (e2->defstate == SS_DEFINED && !sRef_isKept (e2)))
3973 if (checkDeadState (el, TRUE, loc))
3975 if (sRef_isThroughArrayFetch (el))
3977 sRef_maybeKill (el, loc);
3978 sRef_maybeKill (e2, loc);
3982 else if ((e2->defstate == SS_DEAD || sRef_isKept (e2)) &&
3983 (el->defstate == SS_DEFINED && !sRef_isKept (el)))
3986 if (checkDeadState (e2, FALSE, loc))
3988 if (sRef_isThroughArrayFetch (el))
3990 sRef_maybeKill (el, loc);
3991 sRef_maybeKill (e2, loc);
3995 else if (el->defstate == SS_DEFINED &&
3996 e2->defstate == SS_PDEFINED)
3998 DPRINTF (("set pdefined: %s", sRef_unparseFull (el)));
3999 el->defstate = SS_PDEFINED;
4001 else if (e2->defstate == SS_DEFINED &&
4002 el->defstate == SS_PDEFINED)
4004 DPRINTF (("set pdefined: %s", sRef_unparseFull (e2)));
4005 e2->defstate = SS_PDEFINED;
4012 if (ctype_isUnion (ctype_realType (sRef_getType (el))))
4014 el->deriv = sRef_mergeUnionDerivs (el->deriv, e2->deriv,
4019 el->deriv = sRef_mergeDerivs (el->deriv, e2->deriv, opt, cl, loc);
4022 if (sRef_equivalent (el, e2))
4024 ret = sRefSet_insert (ret, el);
4028 sRef sr = sRef_leastCommon (el, e2);
4030 if (sRef_isValid (sr))
4032 ret = sRefSet_insert (ret, sr);
4040 (void) sRefSet_delete (other, e2);
4042 else /* not defined */
4044 (void) checkDeadState (el, TRUE, loc);
4047 } end_sRefSet_allElements;
4049 sRefSet_allElements (other, el)
4051 if (sRef_isValid (el))
4053 (void) checkDeadState (el, FALSE, loc);
4055 } end_sRefSet_allElements;
4062 ** Returns TRUE is there is an error.
4065 static bool checkDeadState (/*@notnull@*/ sRef el, bool tbranch, fileloc loc)
4068 ** usymtab_isGuarded --- the utab should still be in the
4069 ** state of the alternate branch.
4071 ** tbranch TRUE means el is released in the last branch, e.g.
4072 ** if (x != NULL) { ; } else { sfree (x); }
4073 ** so, if x is null in the other branch no error is reported.
4075 ** tbranch FALSE means this is the other branch:
4076 ** if (x != NULL) { sfree (x); } else { ; }
4077 ** so, if x is null in this branch there is no error.
4081 if ((sRef_isDead (el) || sRef_isKept (el))
4082 && !sRef_isDeepUnionField (el) && !sRef_isThroughArrayFetch (el))
4087 if (usymtab_isDefinitelyNullDeep (el))
4094 if (usymtab_isAltDefinitelyNullDeep (el))
4102 message ("Storage %q is %q in one path, but live in another.",
4104 cstring_makeLiteral (sRef_isKept (el)
4105 ? "kept" : "released")),
4109 if (sRef_isKept (el))
4111 sRef_showAliasInfo (el);
4115 sRef_showStateInfo (el);
4118 /* prevent further errors */
4119 el->defstate = SS_UNKNOWN;
4120 sRef_setAliasKind (el, AK_ERROR, fileloc_undefined);
4130 checkDerivDeadState (/*@notnull@*/ sRef el, bool tbranch, fileloc loc)
4133 if (checkDeadState (el, tbranch, loc))
4135 sRefSet_allElements (el->deriv, t)
4137 if (sRef_isValid (t))
4139 checkDerivDeadState (t, tbranch, loc);
4141 } end_sRefSet_allElements;
4146 sRef_mergePdefinedDerivs (sRefSet res, sRefSet other, bool opt,
4147 clause cl, fileloc loc)
4149 sRefSet ret = sRefSet_new ();
4151 sRefSet_allElements (res, el)
4153 if (sRef_isValid (el))
4155 sRef e2 = sRefSet_lookupMember (other, el);
4157 if (sRef_isValid (e2))
4159 if (sRef_isAllocated (el) && !sRef_isAllocated (e2))
4163 else if (sRef_isAllocated (e2) && !sRef_isAllocated (el))
4165 el->deriv = sRefSet_copyInto (el->deriv, e2->deriv);
4169 el->deriv = sRef_mergePdefinedDerivs (el->deriv, e2->deriv,
4173 sRef_mergeStateAux (el, e2, cl, opt, loc, FALSE);
4175 ret = sRefSet_insert (ret, el);
4176 (void) sRefSet_delete (other, e2);
4182 checkDerivDeadState (el, (cl == FALSECLAUSE), loc);
4185 ret = sRefSet_insert (ret, el);
4188 } end_sRefSet_allElements;
4190 sRefSet_allElements (other, el)
4192 if (sRef_isValid (el))
4194 if (!sRefSet_member (ret, el))
4196 /* was cl == FALSECLAUSE */
4197 checkDerivDeadState (el, FALSE, loc);
4198 ret = sRefSet_insert (ret, el);
4203 ** it's okay --- member is a different equality test
4207 } end_sRefSet_allElements;
4213 sRef sRef_makeConj (/*@exposed@*/ /*@returned@*/ sRef a, /*@exposed@*/ sRef b)
4215 llassert (sRef_isValid (a));
4216 llassert (sRef_isValid (b));
4218 if (!sRef_equivalent (a, b))
4220 sRef s = sRef_newRef ();
4223 s->info = (sinfo) dmalloc (sizeof (*s->info));
4224 s->info->conj = (cjinfo) dmalloc (sizeof (*s->info->conj));
4225 s->info->conj->a = a; /* sRef_copy (a) */ /*@i32*/ ;
4226 s->info->conj->b = b; /* sRef_copy (b);*/ /*@i32@*/ ;
4228 if (ctype_equal (a->type, b->type)) s->type = a->type;
4229 else s->type = ctype_makeConj (a->type, b->type);
4231 if (a->defstate == b->defstate)
4233 s->defstate = a->defstate;
4237 s->defstate = SS_UNKNOWN;
4240 sRef_setNullStateN (s, NS_UNKNOWN);
4242 s->safe = a->safe && b->safe;
4243 s->aliaskind = alkind_resolve (a->aliaskind, b->aliaskind);
4245 llassert (valueTable_isUndefined (s->state));
4246 s->state = context_createValueTable (s);
4251 /*@-exposetrans@*/ return a; /*@=exposetrans@*/
4255 /*@dependent@*/ sRef
4258 sRef s = sRef_new ();
4260 s->kind = SK_UNKNOWN;
4264 static /*@owned@*/ sRef
4265 sRef_makeSpecial (speckind sk) /*@*/
4267 sRef s = sRef_new ();
4269 s->kind = SK_SPECIAL;
4270 s->info = (sinfo) dmalloc (sizeof (*s->info));
4272 /*@-dependenttrans@*/
4274 /*@=dependenttrans@*/
4277 static /*@owned@*/ sRef srnothing = sRef_undefined;
4278 static /*@owned@*/ sRef srinternal = sRef_undefined;
4279 static /*@owned@*/ sRef srsystem = sRef_undefined;
4280 static /*@owned@*/ sRef srspec = sRef_undefined;
4282 /*@dependent@*/ sRef
4283 sRef_makeNothing (void)
4285 if (sRef_isInvalid (srnothing))
4287 srnothing = sRef_makeSpecial (SR_NOTHING);
4294 sRef_makeInternalState (void)
4296 if (sRef_isInvalid (srinternal))
4298 srinternal = sRef_makeSpecial (SR_INTERNAL);
4305 sRef_makeSpecState (void)
4307 if (sRef_isInvalid (srspec))
4309 srspec = sRef_makeSpecial (SR_SPECSTATE);
4316 sRef_makeSystemState (void)
4318 if (sRef_isInvalid (srsystem))
4320 srsystem = sRef_makeSpecial (SR_SYSTEM);
4327 sRef_makeGlobalMarker (void)
4329 sRef s = sRef_makeSpecial (SR_GLOBALMARKER);
4330 llassert (valueTable_isUndefined (s->state));
4331 s->state = context_createGlobalMarkerValueTable ();
4336 sRef_makeResultType (ctype ct)
4338 sRef res = sRef_makeResult ();
4347 sRef s = sRef_newRef ();
4349 s->kind = SK_RESULT;
4350 s->type = ctype_unknown;
4351 s->defstate = SS_UNKNOWN;
4352 s->aliaskind = AK_UNKNOWN;
4353 sRef_setNullStateN (s, NS_UNKNOWN);
4354 llassert (valueTable_isUndefined (s->state));
4355 s->state = context_createValueTable (s);
4357 DPRINTF (("Result: [%p] %s", s, sRef_unparseFull (s)));
4363 sRef_isNothing (sRef s)
4365 return (sRef_isKindSpecial (s) && s->info->spec == SR_NOTHING);
4369 sRef_isInternalState (sRef s)
4371 return (sRef_isKindSpecial (s) && s->info->spec == SR_INTERNAL);
4375 sRef_isSpecInternalState (sRef s)
4377 return (sRef_isKindSpecial (s)
4378 && (s->info->spec == SR_INTERNAL || s->info->spec == SR_SPECSTATE));
4382 sRef_isSpecState (sRef s)
4384 return (sRef_isKindSpecial (s) && s->info->spec == SR_SPECSTATE);
4388 sRef_isResult (sRef s)
4390 return (sRef_isValid (s) && s->kind == SK_RESULT);
4394 sRef_isSystemState (sRef s)
4396 return (sRef_isKindSpecial (s) && s->info->spec == SR_SYSTEM);
4400 sRef_isGlobalMarker (sRef s)
4402 return (sRef_isKindSpecial (s) && s->info->spec == SR_GLOBALMARKER);
4406 sRef_getScopeIndex (sRef s)
4408 llassert (sRef_isValid (s));
4409 llassert (sRef_isCvar (s));
4411 return (s->info->cvar->index);
4415 sRef_makeSafe (sRef s)
4417 if (sRef_isValid (s))
4424 sRef_makeUnsafe (sRef s)
4426 if (sRef_isValid (s))
4433 ** memory state operations
4436 /*@only@*/ cstring sRef_unparseFull (sRef s)
4438 if (sRef_isInvalid (s)) return (cstring_undefined);
4440 return (message ("[%d] %q - %q [%s] { %q } < %q >",
4442 sRef_unparseDebug (s),
4443 sRef_unparseState (s),
4444 exkind_unparse (s->oexpkind),
4445 sRefSet_unparseDebug (s->deriv),
4446 valueTable_unparse (s->state)));
4449 /*@unused@*/ cstring sRef_unparseDeep (sRef s)
4451 cstring st = cstring_undefined;
4453 st = message ("%q:", sRef_unparseFull (s));
4455 if (sRef_isValid (s))
4457 sRefSet_allElements (s->deriv, el)
4459 st = message("%q\n%q", st, sRef_unparseDeep (el));
4460 } end_sRefSet_allElements ;
4466 /*@only@*/ cstring sRef_unparseState (sRef s)
4468 if (sRef_isConj (s))
4470 return (message ("%q | %q",
4471 sRef_unparseState (s->info->conj->a),
4472 sRef_unparseState (s->info->conj->b)));
4475 if (sRef_isInvalid (s))
4477 return (cstring_makeLiteral ("<invalid>"));
4480 return (message ("%s.%s.%s.%s",
4481 alkind_unparse (s->aliaskind),
4482 nstate_unparse (sRef_getNullState (s)),
4483 exkind_unparse (s->expkind),
4484 sstate_unparse (s->defstate)));
4487 bool sRef_isNotUndefined (sRef s)
4489 return (sRef_isInvalid (s)
4490 || (s->defstate != SS_UNDEFINED
4491 && s->defstate != SS_UNUSEABLE
4492 && s->defstate != SS_DEAD));
4495 ynm sRef_isWriteable (sRef s)
4497 if (sRef_isInvalid (s)) return MAYBE;
4499 if (sRef_isConj (s) && s->defstate == SS_UNKNOWN)
4501 if (ynm_toBoolStrict (sRef_isWriteable (sRef_getConjA (s))))
4503 if (ynm_toBoolStrict (sRef_isWriteable (sRef_getConjB (s))))
4511 if (ynm_toBoolStrict (sRef_isWriteable (sRef_getConjB (s))))
4519 return (ynm_fromBool (s->defstate != SS_UNUSEABLE));
4522 bool sRef_hasNoStorage (sRef s)
4524 return (!sRef_isAllocatedStorage (s) || sRef_isDefinitelyNull (s));
4527 bool sRef_isStrictReadable (sRef s)
4529 return (ynm_toBoolStrict (sRef_isReadable (s)));
4532 ynm sRef_isReadable (sRef s)
4536 if (sRef_isInvalid (s)) return YES;
4540 if (sRef_isConj (s) && s->defstate == SS_UNKNOWN)
4542 if (ynm_toBoolStrict (sRef_isReadable (sRef_getConjA (s))))
4544 if (ynm_toBoolStrict (sRef_isReadable (sRef_getConjB (s))))
4552 if (ynm_toBoolStrict (sRef_isReadable (sRef_getConjB (s))))
4559 else if (ss == SS_HOFFA)
4561 if (context_getFlag (FLG_STRICTUSERELEASED))
4572 return (ynm_fromBool (ss == SS_DEFINED
4575 || ss == SS_PDEFINED
4578 || ss == SS_ALLOCATED
4579 || ss == SS_KILLED /* evans 2001-05-26: added this for killed globals */
4580 || ss == SS_UNKNOWN));
4584 static /*@exposed@*/ sRef whatUndefined (/*@exposed@*/ sRef fref, int depth)
4589 if (depth > MAXDEPTH)
4592 ("Warning: check definition limit exceeded, checking %q. "
4593 "This either means there is a variable with at least "
4594 "%d indirections apparent in the program text, or "
4595 "there is a bug in LCLint.",
4596 sRef_unparse (fref),
4600 return sRef_undefined;
4603 if (!sRef_isKnown (fref) || sRef_isAnyDefined (fref))
4605 return sRef_undefined;
4608 if (sRef_isUnuseable (fref) || sRef_isStateUndefined (fref))
4613 ct = ctype_realType (sRef_getType (fref));
4615 if (ctype_isUnknown (ct))
4617 return sRef_undefined;
4619 else if (ctype_isPointer (ct) || ctype_isArray (ct))
4621 if (sRef_isStateUnknown (fref))
4623 return sRef_undefined;
4627 sRef fptr = sRef_constructDeref (fref);
4629 return (whatUndefined (fptr, depth + 1));
4632 else if (ctype_isStruct (ct))
4634 bool hasOneDefined = FALSE;
4636 if (sRef_isStateUnknown (fref))
4641 if (sRef_isPdefined (fref) || sRef_isAnyDefined (fref))
4643 sRefSet_realElements (sRef_derivedFields (fref), sr)
4645 hasOneDefined = TRUE;
4647 if (sRef_isField (sr))
4649 cstring fieldname = sRef_getField (sr);
4650 sRef fldref = sRef_makeField (fref, fieldname);
4651 bool shouldCheck = !sRef_isRecursiveField (fldref);
4655 sRef wdef = whatUndefined (fldref, depth + 1);
4657 if (sRef_isValid (wdef))
4663 } end_sRefSet_realElements;
4665 else if (sRef_isAllocated (fref))
4668 ** for structures, each field must be completely defined
4671 uentryList fields = ctype_getFields (ct);
4673 uentryList_elements (fields, ue)
4675 cstring name = uentry_getRealName (ue);
4676 sRef ffield = sRef_makeField (fref, name);
4677 bool shouldCheck = !sRef_isRecursiveField (ffield);
4679 if (sRef_isRelDef (uentry_getSref (ue)))
4687 sRef wdef = whatUndefined (ffield, depth + 1);
4689 if (sRef_isInvalid (wdef))
4695 } end_uentryList_elements;
4702 else if (ctype_isUnion (ct))
4711 return sRef_undefined;
4714 static bool checkDefined (/*@temp@*/ sRef sr)
4716 /*@-temptrans@*/ /* the result from whatUndefined is lost */
4717 return (sRef_isInvalid (whatUndefined (sr, 0)));
4721 bool sRef_isReallyDefined (sRef s)
4723 if (sRef_isValid (s))
4725 if (sRef_isAnyDefined (s))
4731 if (sRef_isAllocated (s) || sRef_isPdefined (s))
4733 return checkDefined (s);
4747 void sRef_showNotReallyDefined (sRef s)
4749 if (sRef_isValid (s))
4751 if (sRef_isAnyDefined (s))
4757 if (sRef_isAllocated (s) || sRef_isPdefined (s))
4759 /*@-temptrans@*/ /* the result of whatUndefined is lost */
4760 sRef ref = whatUndefined (s, 0);
4762 llassert (sRef_isValid (ref));
4767 (message ("This sub-reference is %s: %q",
4768 sstate_unparse (sRef_getDefState (ref)),
4769 sRef_unparse (ref)));
4784 sstate sRef_getDefState (sRef s)
4786 if (sRef_isInvalid (s)) return (SS_UNKNOWN);
4787 return (s->defstate);
4790 void sRef_setDefState (sRef s, sstate defstate, fileloc loc)
4792 sRef_checkMutable (s);
4793 sRef_setStateAux (s, defstate, loc);
4796 static void sRef_clearAliasStateAux (sRef s, fileloc loc)
4798 sRef_checkMutable (s);
4799 sRef_setAliasKind (s, AK_ERROR, loc);
4802 void sRef_clearAliasState (sRef s, fileloc loc)
4804 sRef_checkMutable (s);
4805 sRef_aliasSetComplete (sRef_clearAliasStateAux, s, loc);
4808 void sRef_setAliasKindComplete (sRef s, alkind kind, fileloc loc)
4810 sRef_checkMutable (s);
4811 sRef_aliasSetCompleteParam (sRef_setAliasKind, s, kind, loc);
4814 void sRef_setAliasKind (sRef s, alkind kind, fileloc loc)
4816 sRef_checkMutable (s);
4818 if (sRef_isValid (s))
4820 sRef_clearDerived (s);
4822 if ((kind != s->aliaskind && kind != s->oaliaskind)
4823 && fileloc_isDefined (loc))
4825 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, loc);
4828 s->aliaskind = kind;
4832 void sRef_setOrigAliasKind (sRef s, alkind kind)
4834 sRef_checkMutable (s);
4836 if (sRef_isValid (s))
4838 s->oaliaskind = kind;
4842 exkind sRef_getExKind (sRef s)
4844 if (sRef_isValid (s))
4846 return (s->expkind);
4854 exkind sRef_getOrigExKind (sRef s)
4856 if (sRef_isValid (s))
4858 return (s->oexpkind);
4866 static void sRef_clearExKindAux (sRef s, fileloc loc)
4868 sRef_checkMutable (s);
4869 sRef_setExKind (s, XO_UNKNOWN, loc);
4872 void sRef_setObserver (sRef s, fileloc loc)
4874 sRef_checkMutable (s);
4875 sRef_setExKind (s, XO_OBSERVER, loc);
4878 void sRef_setExposed (sRef s, fileloc loc)
4880 sRef_checkMutable (s);
4881 sRef_setExKind (s, XO_EXPOSED, loc);
4884 void sRef_clearExKindComplete (sRef s, fileloc loc)
4886 (void) sRef_aliasSetComplete (sRef_clearExKindAux, s, loc);
4889 void sRef_setExKind (sRef s, exkind exp, fileloc loc)
4891 sRef_checkMutable (s);
4893 if (sRef_isValid (s))
4895 if (s->expkind != exp)
4897 s->expinfo = stateInfo_updateLoc (s->expinfo, loc);
4905 ** s1->derived = s2->derived
4908 static void sRef_copyRealDerived (sRef s1, sRef s2)
4910 DPRINTF (("Copy real: %s / %s", sRef_unparse (s1), sRef_unparse (s2)));
4911 sRef_checkMutable (s1);
4913 if (sRef_isValid (s1) && sRef_isValid (s2))
4915 sRef sb = sRef_getRootBase (s1);
4917 sRefSet_clear (s1->deriv);
4919 sRefSet_allElements (s2->deriv, el)
4921 if (sRef_isValid (el))
4923 sRef rb = sRef_getRootBase (el);
4925 if (!sRef_same (rb, sb))
4927 sRef fb = sRef_fixDirectBase (el, s1);
4929 if (sRef_isValid (fb))
4931 sRef_copyRealDerived (fb, el);
4932 sRef_addDeriv (s1, fb);
4937 sRef_addDeriv (s1, el);
4940 } end_sRefSet_allElements ;
4945 void sRef_copyRealDerivedComplete (sRef s1, sRef s2)
4947 sRef_innerAliasSetCompleteParam (sRef_copyRealDerived, s1, s2);
4950 void sRef_setUndefined (sRef s, fileloc loc)
4952 sRef_checkMutable (s);
4954 if (sRef_isValid (s))
4956 s->defstate = SS_UNDEFINED;
4958 if (fileloc_isDefined (loc))
4960 s->definfo = stateInfo_updateLoc (s->definfo, loc);
4963 sRef_clearDerived (s);
4967 static void sRef_setDefinedAux (sRef s, fileloc loc, bool clear)
4969 sRef_checkMutable (s);
4970 if (sRef_isInvalid (s)) return;
4972 if (s->defstate != SS_DEFINED && fileloc_isDefined (loc))
4974 s->definfo = stateInfo_updateLoc (s->definfo, loc);
4977 s->defstate = SS_DEFINED;
4979 /* e.g., if x is allocated, *x = 3 defines x */
4981 if (s->kind == SK_PTR)
4983 sRef p = s->info->ref;
4985 if (p->defstate == SS_ALLOCATED)
4987 sRef_setDefinedAux (p, loc, clear);
4990 else if (s->kind == SK_ARRAYFETCH)
4992 if (!s->info->arrayfetch->indknown
4993 || (s->info->arrayfetch->ind == 0))
4995 sRef p = s->info->arrayfetch->arr;
4996 sRef ptr = sRef_constructPointer (p);
4998 if (sRef_isValid (ptr))
5000 if (ptr->defstate == SS_ALLOCATED
5001 || ptr->defstate == SS_UNDEFINED)
5003 sRef_setDefinedAux (ptr, loc, clear);
5007 if (p->defstate == SS_RELDEF)
5011 else if (p->defstate == SS_ALLOCATED || p->defstate == SS_PDEFINED)
5013 p->defstate = SS_DEFINED;
5020 else if (s->kind == SK_FIELD)
5022 sRef parent = s->info->field->rec;
5024 if (sRef_isValid (parent))
5026 if (ctype_isUnion (ctype_realType (parent->type)))
5029 ** Should not clear derived from here.
5032 sRef_setDefinedNoClear (parent, loc);
5036 ; /* Nothing to do for structures. */
5048 sRef_clearDerived (s);
5052 static void sRef_setPartialDefined (sRef s, fileloc loc)
5054 sRef_checkMutable (s);
5056 if (!sRef_isPartial (s))
5058 sRef_setDefined (s, loc);
5062 void sRef_setPartialDefinedComplete (sRef s, fileloc loc)
5064 sRef_innerAliasSetComplete (sRef_setPartialDefined, s, loc);
5067 void sRef_setDefinedComplete (sRef s, fileloc loc)
5069 sRef_innerAliasSetComplete (sRef_setDefined, s, loc);
5072 void sRef_setDefined (sRef s, fileloc loc)
5074 sRef_checkMutable (s);
5075 sRef_setDefinedAux (s, loc, TRUE);
5078 static void sRef_setDefinedNoClear (sRef s, fileloc loc)
5080 sRef_checkMutable (s);
5081 DPRINTF (("Defining: %s", sRef_unparseFull (s)));
5082 sRef_setDefinedAux (s, loc, FALSE);
5083 DPRINTF (("==> %s", sRef_unparseFull (s)));
5086 void sRef_setDefinedNCComplete (sRef s, fileloc loc)
5088 sRef_checkMutable (s);
5089 DPRINTF (("Set Defined Complete: %s", sRef_unparseFull (s)));
5090 sRef_innerAliasSetComplete (sRef_setDefinedNoClear, s, loc);
5091 DPRINTF (("==> %s", sRef_unparseFull (s)));
5094 static bool sRef_isDeepUnionField (sRef s)
5096 return (sRef_deepPred (sRef_isUnionField, s));
5099 bool sRef_isUnionField (sRef s)
5101 if (sRef_isValid (s) && s->kind == SK_FIELD)
5104 ** defining one field of a union defines the union
5107 sRef base = s->info->field->rec;
5109 if (sRef_isValid (base))
5111 return (ctype_isUnion (ctype_realType (base->type)));
5118 void sRef_setPdefined (sRef s, fileloc loc)
5120 sRef_checkMutable (s);
5121 if (sRef_isValid (s) && !sRef_isPartial (s))
5123 sRef base = sRef_getBaseSafe (s);
5125 if (s->defstate == SS_ALLOCATED)
5130 if (s->defstate != SS_PDEFINED && fileloc_isDefined (loc))
5132 s->definfo = stateInfo_updateLoc (s->definfo, loc);
5135 DPRINTF (("set pdefined: %s", sRef_unparseFull (s)));
5136 s->defstate = SS_PDEFINED;
5138 /* e.g., if x is allocated, *x = 3 defines x */
5140 while (sRef_isValid (base) && sRef_isKnown (base))
5142 if (base->defstate == SS_DEFINED)
5146 base->defstate = SS_PDEFINED;
5147 nb = sRef_getBaseSafe (base);
5158 static void sRef_setStateAux (sRef s, sstate ss, fileloc loc)
5160 sRef_checkMutable (s);
5162 if (sRef_isValid (s))
5164 /* if (s->defstate == SS_RELDEF) return; */
5166 if (s->defstate != ss && fileloc_isDefined (loc))
5168 s->definfo = stateInfo_updateLoc (s->definfo, loc);
5172 sRef_clearDerived (s);
5174 if (ss == SS_ALLOCATED)
5176 sRef base = sRef_getBaseSafe (s);
5178 while (sRef_isValid (base) && sRef_isKnown (base))
5180 if (base->defstate == SS_DEFINED)
5184 base->defstate = SS_PDEFINED;
5186 nb = sRef_getBaseSafe (base);
5199 void sRef_setAllocatedComplete (sRef s, fileloc loc)
5201 sRef_innerAliasSetComplete (sRef_setAllocated, s, loc);
5204 static void sRef_setAllocatedShallow (sRef s, fileloc loc)
5206 sRef_checkMutable (s);
5208 if (sRef_isValid (s))
5210 if (s->defstate == SS_DEAD || s->defstate == SS_UNDEFINED)
5212 s->defstate = SS_ALLOCATED;
5214 if (fileloc_isDefined (loc))
5216 s->definfo = stateInfo_updateLoc (s->definfo, loc);
5222 void sRef_setAllocatedShallowComplete (sRef s, fileloc loc)
5224 sRef_innerAliasSetComplete (sRef_setAllocatedShallow, s, loc);
5227 void sRef_setAllocated (sRef s, fileloc loc)
5229 sRef_checkMutable (s);
5230 sRef_setStateAux (s, SS_ALLOCATED, loc);
5233 void sRef_setPartial (sRef s, fileloc loc)
5235 sRef_checkMutable (s);
5236 sRef_setStateAux (s, SS_PARTIAL, loc);
5239 void sRef_setShared (sRef s, fileloc loc)
5241 sRef_checkMutable (s);
5243 if (sRef_isValid (s))
5245 if (s->aliaskind != AK_SHARED && fileloc_isDefined (loc))
5247 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, loc);
5250 s->aliaskind = AK_SHARED;
5251 /* don't! sRef_clearDerived (s); */
5255 void sRef_setLastReference (sRef s, /*@exposed@*/ sRef ref, fileloc loc)
5257 sRef_checkMutable (s);
5259 if (sRef_isValid (s))
5261 s->aliaskind = sRef_getAliasKind (ref);
5262 s->aliasinfo = stateInfo_updateRefLoc (s->aliasinfo, ref, loc);
5267 void sRef_setNullStateAux (/*@notnull@*/ sRef s, nstate ns, fileloc loc)
5269 DPRINTF (("Set null state: %s / %s", sRef_unparse (s), nstate_unparse (ns)));
5270 sRef_checkMutable (s);
5273 if (fileloc_isDefined (loc))
5275 s->nullinfo = stateInfo_updateLoc (s->nullinfo, loc);
5279 void sRef_setNotNull (sRef s, fileloc loc)
5281 if (sRef_isValid (s))
5283 sRef_setNullStateAux (s, NS_NOTNULL, loc);
5287 void sRef_setNullStateN (sRef s, nstate n)
5289 sRef_checkMutable (s);
5293 void sRef_setNullState (sRef s, nstate n, fileloc loc)
5295 if (sRef_isValid (s))
5297 sRef_setNullStateAux (s, n, loc);
5301 void sRef_setNullTerminatedStateInnerComplete (sRef s, struct s_bbufinfo b, /*@unused@*/ fileloc loc) {
5303 switch (b.bufstate) {
5304 case BB_NULLTERMINATED:
5305 sRef_setNullTerminatedState (s);
5306 sRef_setLen (s, b.len);
5308 case BB_POSSIBLYNULLTERMINATED:
5309 sRef_setPossiblyNullTerminatedState(s);
5311 case BB_NOTNULLTERMINATED:
5312 sRef_setNotNullTerminatedState (s);
5315 sRef_setSize (s, b.size);
5317 /* PL: TO BE DONE : Aliases are not modified right now, have to be similar to
5318 * setNullStateInnerComplete.
5322 void sRef_setNullStateInnerComplete (sRef s, nstate n, fileloc loc)
5324 DPRINTF (("Set null state: %s", nstate_unparse (n)));
5326 sRef_setNullState (s, n, loc);
5331 sRef_innerAliasSetComplete (sRef_setPosNull, s, loc);
5334 sRef_innerAliasSetComplete (sRef_setDefNull, s, loc);
5337 sRef_innerAliasSetComplete (sRef_setNullUnknown, s, loc);
5340 sRef_innerAliasSetComplete (sRef_setNotNull, s, loc);
5343 sRef_innerAliasSetComplete (sRef_setNotNull, s, loc);
5346 sRef_innerAliasSetComplete (sRef_setNullUnknown, s, loc);
5349 sRef_innerAliasSetComplete (sRef_setDefNull, s, loc);
5352 sRef_innerAliasSetComplete (sRef_setNullUnknown, s, loc);
5355 sRef_innerAliasSetComplete (sRef_setNullErrorLoc, s, loc);
5360 void sRef_setPosNull (sRef s, fileloc loc)
5362 if (sRef_isValid (s))
5364 sRef_setNullStateAux (s, NS_POSNULL, loc);
5368 void sRef_setDefNull (sRef s, fileloc loc)
5370 if (sRef_isValid (s))
5372 sRef_setNullStateAux (s, NS_DEFNULL, loc);
5376 void sRef_setNullUnknown (sRef s, fileloc loc)
5378 if (sRef_isValid (s))
5380 sRef_setNullStateAux (s, NS_UNKNOWN, loc);
5384 void sRef_setNullError (sRef s)
5386 if (sRef_isValid (s))
5388 sRef_setNullStateAux (s, NS_UNKNOWN, fileloc_undefined);
5392 void sRef_setNullErrorLoc (sRef s, /*@unused@*/ fileloc loc)
5394 sRef_setNullError (s);
5397 void sRef_setOnly (sRef s, fileloc loc)
5399 sRef_checkMutable (s);
5401 if (sRef_isValid (s) && s->aliaskind != AK_ONLY)
5403 s->aliaskind = AK_ONLY;
5404 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, loc);
5408 void sRef_setDependent (sRef s, fileloc loc)
5410 sRef_checkMutable (s);
5412 if (sRef_isValid (s) && !sRef_isConst (s) && (s->aliaskind != AK_DEPENDENT))
5414 DPRINTF (("Setting dependent: %s", sRef_unparseFull (s)));
5415 s->aliaskind = AK_DEPENDENT;
5416 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, loc);
5420 void sRef_setOwned (sRef s, fileloc loc)
5422 sRef_checkMutable (s);
5424 if (sRef_isValid (s) && !sRef_isConst (s) && (s->aliaskind != AK_OWNED))
5426 s->aliaskind = AK_OWNED;
5427 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, loc);
5431 void sRef_setKept (sRef s, fileloc loc)
5433 sRef_checkMutable (s);
5435 if (sRef_isValid (s) && !sRef_isConst (s) && (s->aliaskind != AK_KEPT))
5437 sRef base = sRef_getBaseSafe (s);
5439 while (sRef_isValid (base) && sRef_isKnown (base))
5441 if (base->defstate == SS_DEFINED)
5443 base->defstate = SS_PDEFINED;
5444 base = sRef_getBaseSafe (base);
5453 s->aliaskind = AK_KEPT;
5454 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, loc);
5458 static void sRef_setKeptAux (sRef s, fileloc loc)
5460 if (!sRef_isShared (s))
5462 sRef_setKept (s, loc);
5466 static void sRef_setDependentAux (sRef s, fileloc loc)
5468 if (!sRef_isShared (s))
5470 sRef_setDependent (s, loc);
5474 void sRef_setKeptComplete (sRef s, fileloc loc)
5476 sRef_aliasSetComplete (sRef_setKeptAux, s, loc);
5479 void sRef_setDependentComplete (sRef s, fileloc loc)
5481 sRef_aliasSetComplete (sRef_setDependentAux, s, loc);
5484 void sRef_setFresh (sRef s, fileloc loc)
5486 sRef_checkMutable (s);
5488 if (sRef_isValid (s))
5490 s->aliaskind = AK_FRESH;
5491 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, loc);
5495 void sRef_kill (sRef s, fileloc loc)
5497 DPRINTF (("Kill: %s", sRef_unparseFull (s)));
5498 sRef_checkMutable (s);
5500 if (sRef_isValid (s) && !sRef_isShared (s) && !sRef_isConst (s))
5502 sRef base = sRef_getBaseSafe (s);
5504 while (sRef_isValid (base) && sRef_isKnown (base))
5506 if (base->defstate == SS_DEFINED)
5508 base->defstate = SS_PDEFINED;
5509 base = sRef_getBaseSafe (base);
5517 s->aliaskind = s->oaliaskind;
5518 s->defstate = SS_DEAD;
5519 s->definfo = stateInfo_updateLoc (s->definfo, loc);
5521 sRef_clearDerived (s);
5525 void sRef_maybeKill (sRef s, fileloc loc)
5527 sRef_checkMutable (s);
5529 if (sRef_isValid (s))
5531 sRef base = sRef_getBaseSafe (s);
5534 while (sRef_isValid (base) && sRef_isKnown (base))
5536 if (base->defstate == SS_DEFINED || base->defstate == SS_RELDEF)
5538 base->defstate = SS_PDEFINED;
5539 base = sRef_getBaseSafe (base);
5548 s->aliaskind = s->oaliaskind;
5549 s->defstate = SS_HOFFA;
5550 s->definfo = stateInfo_updateLoc (s->definfo, loc);
5551 sRef_clearDerived (s);
5557 ** just for type checking...
5560 static void sRef_killAux (sRef s, fileloc loc)
5562 if (sRef_isValid (s) && !sRef_isShared (s))
5564 if (sRef_isUnknownArrayFetch (s))
5566 sRef_maybeKill (s, loc);
5576 ** kills s and all aliases to s
5579 void sRef_killComplete (sRef s, fileloc loc)
5581 DPRINTF (("Kill complete: %s", sRef_unparseFull (s)));
5582 sRef_aliasSetComplete (sRef_killAux, s, loc);
5585 static bool sRef_equivalent (sRef s1, sRef s2)
5587 return (sRef_compare (s1, s2) == 0);
5591 ** returns an sRef that will not be free'd on function exit.
5594 /*@only@*/ sRef sRef_saveCopy (sRef s)
5598 if (sRef_isValid (s))
5600 bool old = inFunction;
5603 ** Exit the function scope, so this sRef is not
5604 ** stored in the deallocation table.
5608 DPRINTF (("Copying sref: %s", sRef_unparseFull(s)));
5609 ret = sRef_copy (s);
5610 DPRINTF (("Copying ===>: %s", sRef_unparseFull(ret)));
5615 ret = sRef_undefined;
5618 /*@-dependenttrans@*/
5620 /*@=dependenttrans@*/
5623 sRef sRef_copy (sRef s)
5625 if (sRef_isKindSpecial (s) && !sRef_isGlobalMarker (s))
5628 return s; /* don't copy specials (except for global markers) */
5632 if (sRef_isValid (s))
5634 sRef t = sRef_alloc ();
5636 DPRINTF (("Copying: [%p] %s", s, sRef_unparse (s)));
5637 DPRINTF (("Full: %s", sRef_unparseFull (s)));
5641 t->modified = s->modified;
5642 t->immut = FALSE; /* Note mutability is not copied. */
5645 t->info = sinfo_copy (s);
5646 t->defstate = s->defstate;
5647 t->nullstate = s->nullstate;
5649 /* start modifications */
5650 t->bufinfo.bufstate = s->bufinfo.bufstate;
5651 t->bufinfo.len = s->bufinfo.len;
5652 t->bufinfo.size = s->bufinfo.size;
5653 /* end modifications */
5655 t->aliaskind = s->aliaskind;
5656 t->oaliaskind = s->oaliaskind;
5658 t->expkind = s->expkind;
5659 t->oexpkind = s->oexpkind;
5661 t->nullinfo = stateInfo_copy (s->nullinfo);
5662 t->aliasinfo = stateInfo_copy (s->aliasinfo);
5663 t->definfo = stateInfo_copy (s->definfo);
5664 t->expinfo = stateInfo_copy (s->expinfo);
5666 t->deriv = sRefSet_newDeepCopy (s->deriv);
5667 t->state = valueTable_copy (s->state);
5669 DPRINTF (("Made copy: [%p] %s", t, sRef_unparse (t)));
5674 return sRef_undefined;
5679 # define PREDTEST(func,s) \
5680 do { if (sRef_isInvalid (s)) { return FALSE; } \
5681 else { if (sRef_isConj (s)) \
5682 { return (func (sRef_getConjA (s)) \
5683 || func (sRef_getConjB (s))); }}} while (FALSE);
5685 bool sRef_isAddress (sRef s)
5687 PREDTEST (sRef_isAddress, s);
5688 return (s->kind == SK_ADR);
5692 ** pretty weak... maybe a flag should control this.
5695 bool sRef_isThroughArrayFetch (sRef s)
5697 if (sRef_isValid (s))
5705 if (sRef_isArrayFetch (tref))
5710 lt = sRef_getBase (tref);
5712 } while (sRef_isValid (tref));
5718 bool sRef_isArrayFetch (sRef s)
5720 PREDTEST (sRef_isArrayFetch, s);
5721 return (s->kind == SK_ARRAYFETCH);
5724 bool sRef_isMacroParamRef (sRef s)
5726 if (context_inMacro () && sRef_isCvar (s))
5728 uentry ue = sRef_getUentry (s);
5729 cstring pname = makeParam (uentry_rawName (ue));
5730 uentry mac = usymtab_lookupSafe (pname);
5732 cstring_free (pname);
5733 return (uentry_isValid (mac));
5739 bool sRef_isCvar (sRef s)
5741 PREDTEST (sRef_isCvar, s);
5742 return (s->kind == SK_CVAR);
5745 bool sRef_isConst (sRef s)
5747 PREDTEST (sRef_isConst, s);
5748 return (s->kind == SK_CONST);
5751 bool sRef_isObject (sRef s)
5753 PREDTEST (sRef_isObject, s);
5754 return (s->kind == SK_OBJECT);
5757 bool sRef_isExternal (sRef s)
5759 PREDTEST (sRef_isExternal, s);
5760 return (s->kind == SK_EXTERNAL);
5763 static bool sRef_isDerived (sRef s)
5765 PREDTEST (sRef_isDerived, s);
5766 return (s->kind == SK_DERIVED);
5769 bool sRef_isField (sRef s)
5771 PREDTEST (sRef_isField, s);
5772 return (s->kind == SK_FIELD);
5775 static bool sRef_isIndex (sRef s)
5777 PREDTEST (sRef_isIndex, s);
5778 return (s->kind == SK_ARRAYFETCH);
5781 bool sRef_isAnyParam (sRef s)
5783 PREDTEST (sRef_isAnyParam, s);
5784 return (s->kind == SK_PARAM);
5787 bool sRef_isParam (sRef s)
5789 PREDTEST (sRef_isParam, s);
5790 return (s->kind == SK_PARAM);
5793 bool sRef_isDirectParam (sRef s)
5795 PREDTEST (sRef_isDirectParam, s);
5797 return ((s->kind == SK_CVAR) &&
5798 (s->info->cvar->lexlevel == functionScope) &&
5799 (context_inFunction () &&
5800 (s->info->cvar->index <= uentryList_size (context_getParams ()))));
5803 bool sRef_isPointer (sRef s)
5805 PREDTEST (sRef_isPointer, s);
5806 return (s->kind == SK_PTR);
5810 ** returns true if storage referenced by s is visible
5813 bool sRef_isReference (sRef s)
5815 PREDTEST (sRef_isReference, s);
5817 return (sRef_isPointer (s) || sRef_isIndex (s) || sRef_isFileOrGlobalScope (s)
5818 || (sRef_isField (s) && (sRef_isReference (s->info->field->rec))));
5821 bool sRef_isIReference (sRef s)
5823 return (sRef_isPointer (s) || sRef_isAddress (s) || sRef_isIndex (s)
5824 || sRef_isField (s) || sRef_isArrayFetch (s));
5827 bool sRef_isFileOrGlobalScope (sRef s)
5829 return (sRef_isCvar (s) && (s->info->cvar->lexlevel <= fileScope));
5832 bool sRef_isRealGlobal (sRef s)
5834 return (sRef_isCvar (s) && (s->info->cvar->lexlevel == globScope));
5837 bool sRef_isFileStatic (sRef s)
5839 return (sRef_isCvar (s) && (s->info->cvar->lexlevel == fileScope));
5842 bool sRef_isAliasCheckedGlobal (sRef s)
5844 if (sRef_isFileOrGlobalScope (s))
5846 uentry ue = sRef_getUentry (s);
5848 return context_checkAliasGlob (ue);
5856 void sRef_free (/*@only@*/ sRef s)
5858 if (s != sRef_undefined && s->kind != SK_SPECIAL)
5860 DPRINTF (("Free sref: [%p]", s));
5862 sRef_checkValid (s);
5864 stateInfo_free (s->expinfo);
5865 stateInfo_free (s->aliasinfo);
5866 stateInfo_free (s->definfo);
5867 stateInfo_free (s->nullinfo);
5869 sRefSet_free (s->deriv);
5870 s->deriv = sRefSet_undefined;
5872 /*@i43@*/ /* valueTable_free (s->state); */
5876 /* drl added to help locate use after release*/
5877 s->expinfo = stateInfo_undefined;
5878 s->aliasinfo = stateInfo_undefined;
5879 s->definfo = stateInfo_undefined;
5880 s->nullinfo = stateInfo_undefined;
5882 /*@i32@*/ sfree (s);
5886 void sRef_setType (sRef s, ctype t)
5888 sRef_checkMutable (s);
5890 if (sRef_isValid (s))
5896 void sRef_setTypeFull (sRef s, ctype t)
5898 sRef_checkMutable (s);
5900 if (sRef_isValid (s))
5904 sRefSet_allElements (s->deriv, current)
5906 sRef_setTypeFull (current, ctype_unknown);
5907 } end_sRefSet_allElements ;
5912 sRef_buildField (/*@exposed@*/ sRef rec, /*@dependent@*/ cstring f)
5914 return (sRef_buildNCField (rec, f));
5917 static /*@exposed@*/ sRef
5918 sRef_findDerivedField (/*@notnull@*/ sRef rec, cstring f)
5920 sRefSet_allElements (rec->deriv, sr)
5922 if (sRef_isValid (sr))
5924 if (sr->info != NULL)
5926 if (sr->kind == SK_FIELD && cstring_equal (sr->info->field->field, f))
5932 } end_sRefSet_allElements;
5934 return sRef_undefined;
5937 /*@dependent@*/ /*@observer@*/ sRefSet sRef_derivedFields (/*@temp@*/ sRef rec)
5939 if (sRef_isValid (rec))
5947 return (sRefSet_undefined);
5951 static /*@exposed@*/ sRef
5952 sRef_findDerivedPointer (sRef s)
5954 if (sRef_isValid (s))
5956 sRefSet_realElements (s->deriv, sr)
5958 if (sRef_isValid (sr) && sr->kind == SK_PTR)
5962 } end_sRefSet_realElements;
5965 return sRef_undefined;
5969 sRef_isUnknownArrayFetch (sRef s)
5971 return (sRef_isValid (s)
5972 && s->kind == SK_ARRAYFETCH
5973 && !s->info->arrayfetch->indknown);
5976 static /*@exposed@*/ sRef
5977 sRef_findDerivedArrayFetch (/*@notnull@*/ sRef s, bool isknown, int idx, bool dead)
5982 sRefSet_realElements (s->deriv, sr)
5984 if (sRef_isValid (sr)
5985 && sr->kind == SK_ARRAYFETCH
5986 && sr->info->arrayfetch->indknown
5987 && (sr->info->arrayfetch->ind == idx))
5991 } end_sRefSet_realElements;
5995 sRefSet_realElements (s->deriv, sr)
5997 if (sRef_isValid (sr)
5998 && sr->kind == SK_ARRAYFETCH
5999 && (!sr->info->arrayfetch->indknown
6000 || (sr->info->arrayfetch->indknown &&
6001 sr->info->arrayfetch->ind == 0)))
6003 if (sRef_isDead (sr) || sRef_isKept (sr))
6005 if (dead || context_getFlag (FLG_STRICTUSERELEASED))
6015 } end_sRefSet_realElements;
6018 return sRef_undefined;
6021 static /*@exposed@*/ sRef
6022 sRef_buildNCField (/*@exposed@*/ sRef rec, /*@exposed@*/ cstring f)
6026 DPRINTF (("Build nc field: %s / %s",
6027 sRef_unparseFull (rec), f));
6029 if (sRef_isInvalid (rec))
6031 return sRef_undefined;
6035 ** check if the field already has been referenced
6038 s = sRef_findDerivedField (rec, f);
6040 if (sRef_isValid (s))
6046 ctype ct = ctype_realType (rec->type);
6048 DPRINTF (("Field of: %s", sRef_unparse (rec)));
6052 s->info = (sinfo) dmalloc (sizeof (*s->info));
6053 s->info->field = (fldinfo) dmalloc (sizeof (*s->info->field));
6054 s->info->field->rec = rec; /* sRef_copy (rec); */ /*@i32@*/
6055 s->info->field->field = f; /* doesn't copy f */
6057 if (ctype_isKnown (ct) && ctype_isSU (ct))
6059 uentry ue = uentryList_lookupField (ctype_getFields (ct), f);
6061 if (!uentry_isUndefined (ue))
6063 DPRINTF (("lookup: %s for %s", uentry_unparseFull (ue),
6064 ctype_unparse (ct)));
6066 s->type = uentry_getType (ue);
6068 if (ctype_isMutable (s->type)
6069 && rec->aliaskind != AK_STACK
6070 && !alkind_isStatic (rec->aliaskind))
6072 s->aliaskind = rec->aliaskind;
6076 s->aliaskind = AK_UNKNOWN;
6079 if (sRef_isStateDefined (rec) || sRef_isStateUnknown (rec)
6080 || sRef_isPdefined (rec))
6082 sRef_setStateFromUentry (s, ue);
6086 sRef_setPartsFromUentry (s, ue);
6089 s->oaliaskind = s->aliaskind;
6090 s->oexpkind = s->expkind;
6092 DPRINTF (("sref: %s", sRef_unparseFull (s)));
6097 Never report this as an error. It can happen whenever there
6098 is casting involved.
6102 llcontbug (message ("buildNCField --- no field %s: %q / %s",
6103 f, sRef_unparse (s), ctype_unparse (ct)));
6107 return sRef_undefined;
6111 if (rec->defstate == SS_DEFINED
6112 && (s->defstate == SS_UNDEFINED || s->defstate == SS_UNKNOWN))
6114 s->defstate = SS_DEFINED;
6116 else if (rec->defstate == SS_PARTIAL)
6118 s->defstate = SS_PARTIAL;
6120 else if (rec->defstate == SS_ALLOCATED)
6122 if (ctype_isStackAllocated (ct) && ctype_isStackAllocated (s->type))
6124 s->defstate = SS_ALLOCATED;
6128 s->defstate = SS_UNDEFINED;
6131 else if (s->defstate == SS_UNKNOWN)
6133 s->defstate = rec->defstate;
6140 if (s->defstate == SS_UNDEFINED)
6142 ctype rt = ctype_realType (s->type);
6144 if (ctype_isArray (rt) || ctype_isSU (rt))
6146 s->defstate = SS_ALLOCATED;
6150 sRef_addDeriv (rec, s);
6151 DPRINTF (("Add deriv: %s", sRef_unparseFull (rec)));
6153 if (ctype_isInt (s->type) && cstring_equal (f, REFSNAME))
6155 s->aliaskind = AK_REFS;
6156 s->oaliaskind = AK_REFS;
6159 DPRINTF (("Build field ==> %s", sRef_unparseFull (s)));
6165 sRef_isStackAllocated (sRef s)
6167 return (sRef_isValid(s)
6168 && s->defstate == SS_ALLOCATED && ctype_isStackAllocated (s->type));
6172 void sRef_setArrayFetchState (/*@notnull@*/ /*@exposed@*/ sRef s,
6173 /*@notnull@*/ /*@exposed@*/ sRef arr)
6175 sRef_checkMutable (s);
6177 if (ctype_isRealAP (arr->type))
6179 s->type = ctype_baseArrayPtr (arr->type);
6182 /* a hack, methinks... makeArrayFetch (&a[0]) ==> a[] */
6183 if (sRef_isAddress (arr))
6185 sRef t = arr->info->ref;
6187 if (sRef_isArrayFetch (t))
6189 s->info->arrayfetch->arr = t->info->arrayfetch->arr;
6192 else if (ctype_isRealPointer (arr->type))
6194 sRef sp = sRef_findDerivedPointer (arr);
6196 if (sRef_isValid (sp))
6199 if (ctype_isMutable (s->type))
6201 sRef_setExKind (s, sRef_getExKind (sp), fileloc_undefined);
6203 s->aliaskind = sp->aliaskind;
6206 s->defstate = sp->defstate;
6208 if (s->defstate == SS_DEFINED)
6210 if (!context_getFlag (FLG_STRICTDESTROY))
6212 s->defstate = SS_PARTIAL;
6216 sRef_setNullStateN (s, sRef_getNullState (sp));
6220 if (arr->defstate == SS_UNDEFINED)
6222 s->defstate = SS_UNUSEABLE;
6224 else if ((arr->defstate == SS_ALLOCATED) && !ctype_isSU (s->type))
6226 s->defstate = SS_UNDEFINED;
6230 if (!context_getFlag (FLG_STRICTDESTROY))
6232 s->defstate = SS_PARTIAL;
6236 s->defstate = SS_DEFINED;
6240 ** Very weak checking for array elements.
6242 ** s->defstate = arr->defstate;
6246 sRef_setExKind (s, sRef_getExKind (arr), g_currentloc);
6248 if (arr->aliaskind == AK_LOCAL || arr->aliaskind == AK_FRESH)
6250 s->aliaskind = AK_LOCAL;
6254 s->aliaskind = AK_UNKNOWN;
6257 sRef_setTypeState (s);
6262 if (arr->defstate == SS_DEFINED)
6265 ** Very weak checking for array elements.
6267 ** s->defstate = arr->defstate;
6270 if (context_getFlag (FLG_STRICTDESTROY))
6272 s->defstate = SS_DEFINED;
6276 s->defstate = SS_PARTIAL;
6279 else if (arr->defstate == SS_ALLOCATED)
6281 if (ctype_isRealArray (s->type))
6283 s->defstate = SS_ALLOCATED;
6287 if (!s->info->arrayfetch->indknown)
6290 ** is index is unknown, elements is defined or
6291 ** allocated is any element is!
6294 s->defstate = SS_UNDEFINED;
6296 sRefSet_allElements (arr->deriv, sr)
6298 if (sRef_isValid (sr))
6300 if (sr->defstate == SS_ALLOCATED)
6302 s->defstate = SS_ALLOCATED;
6306 if (sr->defstate == SS_DEFINED)
6308 if (context_getFlag (FLG_STRICTDESTROY))
6310 s->defstate = SS_DEFINED;
6314 s->defstate = SS_PARTIAL;
6321 } end_sRefSet_allElements;
6326 s->defstate = SS_UNDEFINED;
6332 s->defstate = arr->defstate;
6337 ** kludgey way to guess where aliaskind applies
6340 if (ctype_isMutable (s->type)
6341 && !ctype_isPointer (arr->type)
6342 && !alkind_isStatic (arr->aliaskind)
6343 && !alkind_isStack (arr->aliaskind)) /* evs - 2000-06-20: don't pass stack allocation to members */
6345 s->aliaskind = arr->aliaskind;
6349 s->aliaskind = AK_UNKNOWN;
6352 sRef_setTypeState (s);
6355 if (sRef_isObserver (arr))
6357 s->expkind = XO_OBSERVER;
6361 /*@exposed@*/ sRef sRef_buildArrayFetch (/*@exposed@*/ sRef arr)
6365 if (!sRef_isValid (arr)) {
6366 /*@-nullret@*/ return arr /*@=nullret@*/;
6369 if (ctype_isRealPointer (arr->type))
6371 (void) sRef_buildPointer (arr); /* do this to define arr! */
6374 s = sRef_findDerivedArrayFetch (arr, FALSE, 0, FALSE);
6376 if (sRef_isValid (s))
6378 sRef_setExKind (s, sRef_getExKind (arr), g_currentloc);
6385 s->kind = SK_ARRAYFETCH;
6386 s->info = (sinfo) dmalloc (sizeof (*s->info));
6387 s->info->arrayfetch = (ainfo) dmalloc (sizeof (*s->info->arrayfetch));
6388 s->info->arrayfetch->indknown = FALSE;
6389 s->info->arrayfetch->ind = 0;
6390 s->info->arrayfetch->arr = arr; /* sRef_copy (arr); */ /*@i32@*/
6391 sRef_setArrayFetchState (s, arr);
6392 s->oaliaskind = s->aliaskind;
6393 s->oexpkind = s->expkind;
6395 if (!context_inProtectVars ())
6397 sRef_addDeriv (arr, s);
6400 llassert (valueTable_isUndefined (s->state));
6401 s->state = context_createValueTable (s);
6408 sRef_buildArrayFetchKnown (/*@exposed@*/ sRef arr, int i)
6412 if (!sRef_isValid (arr)) {
6413 /*@-nullret@*/ return arr /*@=nullret@*/;
6416 if (ctype_isRealPointer (arr->type))
6418 (void) sRef_buildPointer (arr); /* do this to define arr! */
6421 s = sRef_findDerivedArrayFetch (arr, TRUE, i, FALSE);
6423 if (sRef_isValid (s))
6425 sRef_setExKind (s, sRef_getExKind (arr), g_currentloc);
6432 s->kind = SK_ARRAYFETCH;
6433 s->info = (sinfo) dmalloc (sizeof (*s->info));
6434 s->info->arrayfetch = (ainfo) dmalloc (sizeof (*s->info->arrayfetch));
6435 s->info->arrayfetch->arr = arr; /* sRef_copy (arr); */ /*@i32@*/
6436 s->info->arrayfetch->indknown = TRUE;
6437 s->info->arrayfetch->ind = i;
6439 sRef_setArrayFetchState (s, arr);
6441 s->oaliaskind = s->aliaskind;
6442 s->oexpkind = s->expkind;
6443 sRef_addDeriv (arr, s);
6445 llassert (valueTable_isUndefined (s->state));
6446 s->state = context_createValueTable (s);
6453 ** sets everything except for defstate
6457 sRef_setPartsFromUentry (sRef s, uentry ue)
6459 sRef uref = uentry_getSref (ue);
6461 llassert (sRef_isValid (s));
6463 s->aliaskind = alkind_derive (s->aliaskind, uentry_getAliasKind (ue));
6464 s->oaliaskind = s->aliaskind;
6466 if (s->expkind == XO_UNKNOWN)
6468 s->expkind = uentry_getExpKind (ue);
6471 s->oexpkind = s->expkind;
6473 if (sRef_getNullState (s) == NS_UNKNOWN)
6475 DPRINTF (("Setting null state!"));
6476 sRef_setNullStateN (s, sRef_getNullState (uentry_getSref (ue)));
6480 DPRINTF (("Skipping null null state!"));
6483 if (s->aliaskind == AK_IMPONLY && (sRef_isExposed (s) || sRef_isObserver (s)))
6485 s->oaliaskind = s->aliaskind = AK_IMPDEPENDENT;
6488 if (sRef_isValid (uref))
6490 valueTable utable = uref->state;
6491 valueTable_free (s->state);
6492 s->state = valueTable_copy (utable);
6497 sRef_setStateFromAbstractUentry (sRef s, uentry ue)
6499 llassert (sRef_isValid (s));
6500 sRef_checkMutable (s);
6502 sRef_setPartsFromUentry (s, ue);
6504 s->aliaskind = alkind_derive (s->aliaskind, uentry_getAliasKind (ue));
6505 s->oaliaskind = s->aliaskind;
6507 if (s->expkind == XO_UNKNOWN)
6509 s->expkind = uentry_getExpKind (ue);
6512 s->oexpkind = s->expkind;
6516 sRef_setStateFromUentry (sRef s, uentry ue)
6520 sRef_checkMutable (s);
6521 llassert (sRef_isValid (s));
6523 sRef_setPartsFromUentry (s, ue);
6525 defstate = uentry_getDefState (ue);
6527 if (sstate_isKnown (defstate))
6529 s->defstate = defstate;
6538 sRef_buildPointer (/*@exposed@*/ sRef t)
6540 DPRINTF (("build pointer: %s", sRef_unparse (t)));
6542 if (sRef_isInvalid (t)) return sRef_undefined;
6544 if (sRef_isAddress (t))
6546 DPRINTF (("Return ref: %s", sRef_unparse (t->info->ref)));
6547 return (t->info->ref);
6551 sRef s = sRef_findDerivedPointer (t);
6553 DPRINTF (("find derived: %s", sRef_unparse (s)));
6555 if (sRef_isValid (s))
6558 sRef_setExKind (s, sRef_getExKind (t), g_currentloc);
6559 s->oaliaskind = s->aliaskind;
6560 s->oexpkind = s->expkind;
6566 s = sRef_constructPointerAux (t);
6568 DPRINTF (("construct: %s", sRef_unparse (s)));
6570 if (sRef_isValid (s))
6572 sRef_addDeriv (t, s);
6574 s->oaliaskind = s->aliaskind;
6575 s->oexpkind = s->expkind;
6584 sRef_constructPointer (/*@exposed@*/ sRef t)
6587 return sRef_buildPointer (t);
6590 static /*@exposed@*/ sRef sRef_constructDerefAux (sRef t, bool isdead)
6592 if (sRef_isValid (t))
6597 ** if there is a derived t[?], return that. Otherwise, *t.
6600 s = sRef_findDerivedArrayFetch (t, FALSE, 0, isdead);
6602 if (sRef_isValid (s))
6608 sRef ret = sRef_constructPointer (t);
6611 ** This is necessary to prevent infinite depth
6612 ** in checking complete destruction.
6617 /* ret->defstate = SS_UNKNOWN; */
6628 return sRef_undefined;
6632 sRef sRef_constructDeref (sRef t)
6634 return sRef_constructDerefAux (t, FALSE);
6637 sRef sRef_constructDeadDeref (sRef t)
6639 return sRef_constructDerefAux (t, TRUE);
6643 sRef_constructPointerAux (/*@notnull@*/ /*@exposed@*/ sRef t)
6645 sRef s = sRef_newRef ();
6650 s->info = (sinfo) dmalloc (sizeof (*s->info));
6651 s->info->ref = t; /* sRef_copy (t); */ /*@i32*/
6653 if (ctype_isRealAP (rt))
6655 s->type = ctype_baseArrayPtr (rt);
6658 st = ctype_realType (s->type);
6660 if (t->defstate == SS_UNDEFINED)
6662 s->defstate = SS_UNUSEABLE;
6664 else if ((t->defstate == SS_ALLOCATED) && !ctype_isSU (st))
6666 s->defstate = SS_UNDEFINED;
6670 s->defstate = t->defstate;
6673 if (t->aliaskind == AK_LOCAL || t->aliaskind == AK_FRESH)
6675 s->aliaskind = AK_LOCAL;
6679 s->aliaskind = AK_UNKNOWN;
6682 sRef_setExKind (s, sRef_getExKind (t), fileloc_undefined);
6683 sRef_setTypeState (s);
6685 s->oaliaskind = s->aliaskind;
6686 s->oexpkind = s->expkind;
6688 llassert (valueTable_isUndefined (s->state));
6689 s->state = context_createValueTable (s);
6693 bool sRef_hasDerived (sRef s)
6695 return (sRef_isValid (s) && !sRefSet_isEmpty (s->deriv));
6699 sRef_clearDerived (sRef s)
6701 if (sRef_isValid (s))
6703 sRefSet_clear (s->deriv);
6708 sRef_clearDerivedComplete (sRef s)
6711 if (sRef_isValid (s))
6713 sRef base = sRef_getBaseSafe (s);
6715 while (sRef_isValid (base))
6717 sRefSet_clear (base->deriv);
6718 base = sRef_getBaseSafe (base);
6721 sRefSet_clear (s->deriv);
6725 /*@exposed@*/ sRef sRef_makePointer (/*@exposed@*/ sRef s)
6728 sRef res = sRef_buildPointer (s);
6730 DPRINTF (("Res: %s", sRef_unparse (res)));
6735 ** &a[] => a (this is for out params)
6739 sRef_makeAnyArrayFetch (/*@exposed@*/ sRef arr)
6742 if (sRef_isAddress (arr))
6744 return (arr->info->ref);
6748 return (sRef_buildArrayFetch (arr));
6753 sRef_makeArrayFetch (/*@exposed@*/ sRef arr)
6755 return (sRef_buildArrayFetch (arr));
6759 sRef_makeArrayFetchKnown (/*@exposed@*/ sRef arr, int i)
6761 return (sRef_buildArrayFetchKnown (arr, i));
6765 sRef_makeField (sRef rec, /*@dependent@*/ cstring f)
6768 ret = sRef_buildField (rec, f);
6773 sRef_makeNCField (/*@exposed@*/ sRef rec, /*@dependent@*/ cstring f)
6775 return (sRef_buildNCField (rec, f));
6779 sRef_unparseKindName (sRef s)
6783 if (s == sRef_undefined) return cstring_makeLiteral ("<invalid>");
6785 s = sRef_fixConj (s);
6790 if (sRef_isLocalVar (s))
6792 result = cstring_makeLiteral ("Variable");
6796 result = cstring_makeLiteral ("Undef global");
6800 result = cstring_makeLiteral ("Out parameter");
6803 if (sRef_isAnyParam (s->info->arrayfetch->arr))
6805 result = cstring_makeLiteral ("Out parameter");
6807 else if (sRef_isIndexKnown (s))
6809 result = cstring_makeLiteral ("Array element");
6813 result = cstring_makeLiteral ("Value");
6817 if (sRef_isAnyParam (s->info->ref))
6819 result = cstring_makeLiteral ("Out parameter");
6823 result = cstring_makeLiteral ("Value");
6827 result = cstring_makeLiteral ("Value");
6830 result = cstring_makeLiteral ("Field");
6833 result = cstring_makeLiteral ("Object");
6835 case SK_UNCONSTRAINED:
6836 result = cstring_makeLiteral ("<anything>");
6845 result = cstring_makeLiteral ("<unknown>");
6848 result = cstring_makeLiteral ("<conj>");
6851 result = cstring_makeLiteral ("Storage");
6859 sRef_unparseKindNamePlain (sRef s)
6863 if (s == sRef_undefined) return cstring_makeLiteral ("<invalid>");
6865 s = sRef_fixConj (s);
6870 if (sRef_isLocalVar (s))
6872 result = cstring_makeLiteral ("Variable");
6876 result = cstring_makeLiteral ("Global");
6880 result = cstring_makeLiteral ("Parameter");
6883 if (sRef_isAnyParam (s->info->arrayfetch->arr))
6885 result = cstring_makeLiteral ("Parameter");
6887 else if (sRef_isIndexKnown (s))
6889 result = cstring_makeLiteral ("Array element");
6893 result = cstring_makeLiteral ("Value");
6897 if (sRef_isAnyParam (s->info->ref))
6899 result = cstring_makeLiteral ("Parameter");
6903 result = cstring_makeLiteral ("Value");
6907 result = cstring_makeLiteral ("Value");
6910 result = cstring_makeLiteral ("Field");
6913 result = cstring_makeLiteral ("Object");
6916 result = cstring_makeLiteral ("Storage");
6918 case SK_UNCONSTRAINED:
6919 result = cstring_makeLiteral ("<anything>");
6928 result = cstring_makeLiteral ("<unknown>");
6931 result = cstring_makeLiteral ("<conj>");
6943 sRef_copyState (sRef s1, sRef s2)
6945 if (sRef_isValid (s1) && sRef_isValid (s2))
6947 s1->defstate = s2->defstate;
6949 /* start modifications */
6950 s1->bufinfo.bufstate = s2->bufinfo.bufstate;
6951 s1->bufinfo.len = s2->bufinfo.len;
6952 s1->bufinfo.size = s2->bufinfo.size;
6953 /* end modifications */
6955 s1->aliaskind = s2->aliaskind;
6956 s1->aliasinfo = stateInfo_update (s1->aliasinfo, s2->aliasinfo);
6958 s1->expkind = s2->expkind;
6959 s1->expinfo = stateInfo_update (s1->expinfo, s2->expinfo);
6961 s1->nullstate = s2->nullstate;
6962 s1->nullinfo = stateInfo_update (s1->nullinfo, s2->nullinfo);
6965 /*@i834 don't free it: valueTable_free (s1->state); */
6966 /*@i32@*/ s1->state = valueTable_copy (s2->state);
6968 s1->safe = s2->safe;
6973 sRef_makeNew (ctype ct, sRef t, cstring name)
6975 sRef s = sRef_newRef ();
6980 llassert (sRef_isValid (t));
6981 s->defstate = t->defstate;
6983 s->aliaskind = t->aliaskind;
6984 s->oaliaskind = s->aliaskind;
6985 s->nullstate = t->nullstate;
6987 s->expkind = t->expkind;
6988 s->oexpkind = s->expkind;
6990 s->info = (sinfo) dmalloc (sizeof (*s->info));
6991 s->info->fname = name;
6993 /* start modifications */
6994 s->bufinfo.bufstate = t->bufinfo.bufstate;
6995 /* end modifications */
6997 llassert (valueTable_isUndefined (s->state));
6998 s->state = valueTable_copy (t->state);
7000 DPRINTF (("==> Copying state: %s", valueTable_unparse (s->state)));
7001 DPRINTF (("==> new: %s", sRef_unparseFull (s)));
7006 sRef_makeType (ctype ct)
7008 sRef s = sRef_newRef ();
7010 sRef_checkMutable (s);
7015 s->defstate = SS_UNKNOWN;
7016 s->aliaskind = AK_UNKNOWN;
7017 sRef_setNullStateN (s, NS_UNKNOWN);
7019 /* start modification */
7020 s->bufinfo.bufstate = BB_NOTNULLTERMINATED;
7021 /* end modification */
7024 if (ctype_isUA (ct))
7026 typeId uid = ctype_typeId (ct);
7027 uentry ue = usymtab_getTypeEntrySafe (uid);
7029 if (uentry_isValid (ue))
7031 sRef_mergeStateQuiet (s, uentry_getSref (ue));
7035 s->oaliaskind = s->aliaskind;
7036 s->oexpkind = s->expkind;
7037 llassert (valueTable_isUndefined (s->state));
7038 s->state = context_createValueTable (s);
7040 DPRINTF (("Create: %s", sRef_unparseFull (s)));
7045 sRef_makeConst (ctype ct)
7047 sRef s = sRef_newRef ();
7052 s->defstate = SS_UNKNOWN;
7053 s->aliaskind = AK_UNKNOWN;
7054 sRef_setNullStateN (s, NS_UNKNOWN);
7056 /* start modification */
7057 s->bufinfo.bufstate = BB_NULLTERMINATED;
7058 /* end modification */
7061 if (ctype_isUA (ct))
7063 typeId uid = ctype_typeId (ct);
7064 uentry te = usymtab_getTypeEntrySafe (uid);
7066 if (uentry_isValid (te))
7068 sRef_mergeStateQuiet (s, uentry_getSref (te));
7073 s->oaliaskind = s->aliaskind;
7074 s->oexpkind = s->expkind;
7076 llassert (valueTable_isUndefined (s->state));
7077 s->state = context_createValueTable (s);
7082 bool sRef_hasName (sRef s)
7084 if (sRef_isInvalid (s))
7093 uentry u = usymtab_getRefQuiet (s->info->cvar->lexlevel,
7094 s->info->cvar->index);
7095 return (uentry_hasName (u));
7099 if (s->info->paramno >= 0)
7101 uentry u = uentryList_getN (context_getParams (),
7104 return (uentry_hasName (u));
7108 llassert (s->info->paramno == PARAMUNKNOWN);
7118 sRef_sameName (sRef s1, sRef s2)
7120 if (sRef_isInvalid (s1))
7122 return sRef_isInvalid (s2);
7125 if (sRef_isInvalid (s2))
7133 if (s2->kind == SK_CVAR)
7135 return (s1->info->cvar->lexlevel == s2->info->cvar->lexlevel
7136 && s1->info->cvar->index == s2->info->cvar->index);
7138 else if (s2->kind == SK_PARAM)
7140 if (context_inFunctionLike ())
7142 if (s2->info->paramno != PARAMUNKNOWN)
7144 uentry u1 = usymtab_getRefQuiet (s1->info->cvar->lexlevel,
7145 s1->info->cvar->index);
7146 uentry u2 = uentryList_getN (context_getParams (),
7149 return (cstring_equalFree (uentry_getName (u1),
7150 uentry_getName (u2)));
7154 return s1->info->paramno == PARAMUNKNOWN;
7168 if (s2->kind == SK_PARAM)
7170 return (s1->info->paramno == s2->info->paramno);
7172 else if (s2->kind == SK_CVAR)
7174 if (context_inFunctionLike ())
7176 if (s1->info->paramno == PARAMUNKNOWN)
7182 uentry u1 = uentryList_getN (context_getParams (),
7184 uentry u2 = usymtab_getRefQuiet (s2->info->cvar->lexlevel,
7185 s2->info->cvar->index);
7188 return (cstring_equalFree (uentry_getName (u1),
7189 uentry_getName (u2)));
7203 case SK_UNCONSTRAINED:
7207 if (s2->kind == SK_ARRAYFETCH)
7209 if (bool_equal (s1->info->arrayfetch->indknown,
7210 s2->info->arrayfetch->indknown))
7212 if (!s1->info->arrayfetch->indknown
7213 || (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind))
7215 return sRef_sameName (s1->info->arrayfetch->arr,
7216 s2->info->arrayfetch->arr);
7223 if (s2->kind == SK_FIELD)
7225 if (cstring_equal (s1->info->field->field,
7226 s2->info->field->field))
7228 return sRef_sameName (s1->info->field->rec,
7229 s2->info->field->rec);
7238 if (s2->kind == s1->kind)
7240 return sRef_sameName (s1->info->ref,
7248 return sRef_sameName (sRef_getConjA (s1), s2);
7252 return (s2->kind == SK_UNKNOWN);
7255 if (s2->kind == s1->kind)
7257 return (ctype_equal (s1->type, s2->type));
7262 if (s2->kind == SK_SPECIAL)
7264 return (s1->info->spec == s2->info->spec);
7268 return (s2->kind == SK_RESULT);
7276 sRef_fixOuterRef (/*@returned@*/ sRef s)
7278 sRef root = sRef_getRootBase (s);
7280 if (sRef_isCvar (root))
7282 uentry ue = usymtab_getRefQuiet (root->info->cvar->lexlevel,
7283 root->info->cvar->index);
7285 if (uentry_isValid (ue))
7287 sRef uref = uentry_getSref (ue);
7288 sRef sr = sRef_fixBase (s, uref);
7294 llcontbug (message ("sRef_fixOuterRef: undefined: %q", sRef_unparseDebug (s)));
7303 sRef_storeState (sRef s)
7305 if (sRef_isInvalid (s)) return;
7307 sRef_checkMutable (s);
7308 s->oaliaskind = s->aliaskind;
7309 s->oexpkind = s->expkind;
7312 static void sRef_resetStateAux (sRef s, /*@unused@*/ fileloc loc)
7314 sRef_resetState (s);
7318 sRef_resetState (sRef s)
7320 bool changed = FALSE;
7321 if (sRef_isInvalid (s)) return;
7324 if (s->oaliaskind == AK_KILLREF && !sRef_isParam (s))
7327 ** killref is used in a kludgey way, to save having to add
7328 ** another alias kind (see usymtab_handleParams)
7331 if (s->expkind != s->oexpkind)
7334 s->expkind = s->oexpkind;
7339 if (s->expkind != s->oexpkind)
7342 s->expkind = s->oexpkind;
7345 if (s->aliaskind != s->oaliaskind
7346 && s->aliaskind != AK_REFCOUNTED
7347 && s->aliaskind != AK_REFS)
7350 s->aliaskind = s->oaliaskind;
7356 sRef_clearDerived (s);
7362 sRef_resetStateComplete (sRef s)
7364 sRef_innerAliasSetComplete (sRef_resetStateAux, s, fileloc_undefined);
7368 sRef_fixBase (/*@returned@*/ sRef s, /*@returned@*/ sRef base)
7370 sRef tmp = sRef_undefined;
7373 if (sRef_isInvalid (s)) return s;
7374 if (sRef_isInvalid (base)) return base;
7384 tmp = sRef_fixBase (s->info->arrayfetch->arr, base);
7386 if (s->info->arrayfetch->indknown)
7388 ret = sRef_makeArrayFetchKnown (tmp, s->info->arrayfetch->ind);
7392 ret = sRef_makeArrayFetch (tmp);
7396 tmp = sRef_fixBase (s->info->field->rec, base);
7397 ret = sRef_buildNCField (tmp, s->info->field->field);
7400 tmp = sRef_fixBase (s->info->ref, base);
7401 ret = sRef_makePointer (tmp);
7404 tmp = sRef_fixBase (s->info->ref, base);
7405 ret = sRef_makeAddress (tmp);
7411 tmp = sRef_fixBase (s->info->conj->a, base);
7412 tmpb = sRef_fixBase (s->info->conj->b, base);
7414 ret = sRef_makeConj (tmp, tmpb);
7423 static /*@exposed@*/ sRef
7424 sRef_fixDirectBase (sRef s, sRef base)
7429 if (sRef_isInvalid (s))
7431 return sRef_undefined;
7437 if (s->info->arrayfetch->indknown)
7439 ret = sRef_makeArrayFetchKnown (base, s->info->arrayfetch->ind);
7443 ret = sRef_makeArrayFetch (base);
7447 ret = sRef_buildNCField (base, s->info->field->field);
7450 ret = sRef_makePointer (base);
7453 ret = sRef_makeAddress (base);
7459 tmpa = sRef_fixDirectBase (s->info->conj->a, base);
7460 tmpb = sRef_fixDirectBase (s->info->conj->b, base);
7462 ret = sRef_makeConj (tmpa, tmpb);
7468 sRef_copyState (ret, s);
7473 sRef_isAllocIndexRef (sRef s)
7475 return (sRef_isArrayFetch (s) && !(s->info->arrayfetch->indknown)
7476 && sRef_isAllocated (s->info->arrayfetch->arr));
7480 sRef_showRefLost (sRef s)
7482 if (sRef_hasAliasInfoLoc (s))
7484 llgenindentmsg (cstring_makeLiteral ("Original reference lost"),
7485 sRef_getAliasInfoLoc (s));
7490 sRef_showRefKilled (sRef s)
7492 if (sRef_hasStateInfoLoc (s))
7494 llgenindentmsg (message ("Storage %q released",
7495 sRef_unparse (s)), sRef_getStateInfoLoc (s));
7500 sRef_showStateInconsistent (sRef s)
7502 if (sRef_hasStateInfoLoc (s))
7505 (message ("Storage %qbecomes inconsistent (released on one branch)",
7506 sRef_unparseOpt (s)),
7507 sRef_getStateInfoLoc (s));
7512 sRef_showStateInfo (sRef s)
7514 if (sRef_hasStateInfoLoc (s))
7516 if (s->defstate == SS_DEAD)
7519 (message ("Storage %qis released", sRef_unparseOpt (s)),
7520 sRef_getStateInfoLoc (s));
7522 else if (s->defstate == SS_ALLOCATED || s->defstate == SS_DEFINED)
7525 (message ("Storage %qis %s", sRef_unparseOpt (s),
7526 sstate_unparse (s->defstate)),
7527 sRef_getStateInfoLoc (s));
7529 else if (s->defstate == SS_UNUSEABLE)
7532 (message ("Storage %qbecomes inconsistent (clauses merge with"
7533 "%qreleased on one branch)",
7534 sRef_unparseOpt (s),
7535 sRef_unparseOpt (s)),
7536 sRef_getStateInfoLoc (s));
7540 llgenindentmsg (message ("Storage %qbecomes %s",
7541 sRef_unparseOpt (s),
7542 sstate_unparse (s->defstate)),
7543 sRef_getStateInfoLoc (s));
7549 sRef_showExpInfo (sRef s)
7551 if (sRef_hasExpInfoLoc (s))
7553 llgenindentmsg (message ("Storage %qbecomes %s", sRef_unparseOpt (s),
7554 exkind_unparse (s->expkind)),
7555 sRef_getExpInfoLoc (s));
7560 sRef_showMetaStateInfo (sRef s, cstring key)
7563 metaStateInfo minfo = context_lookupMetaStateInfo (key);
7565 llassert (sRef_isValid (s));
7566 llassert (valueTable_isDefined (s->state));
7567 llassert (metaStateInfo_isDefined (minfo));
7569 val = valueTable_lookup (s->state, key);
7571 if (stateValue_hasLoc (val))
7574 (message ("Meta state %qbecomes %s", sRef_unparseOpt (s),
7575 stateValue_unparseValue (val, minfo)),
7576 stateValue_getLoc (val));
7581 sRef_showNullInfo (sRef s)
7583 DPRINTF (("Show null info: %s", sRef_unparseFull (s)));
7585 if (sRef_hasNullInfoLoc (s) && sRef_isKnown (s))
7587 DPRINTF (("has null info: %s",
7588 fileloc_unparse (sRef_getNullInfoLoc (s))));
7590 switch (sRef_getNullState (s))
7594 fileloc loc = sRef_getNullInfoLoc (s);
7596 if (fileloc_isDefined (loc) && !fileloc_isLib (loc))
7599 (message ("Storage %qbecomes null", sRef_unparseOpt (s)),
7606 fileloc loc = sRef_getNullInfoLoc (s);
7608 if (fileloc_isDefined (loc) && !fileloc_isLib (loc))
7610 llgenindentmsg (message ("Storage %qbecomes null", sRef_unparseOpt (s)),
7618 (message ("Storage %qmay become null", sRef_unparseOpt (s)),
7619 sRef_getNullInfoLoc (s));
7624 (message ("Storage %qbecomes not null", sRef_unparseOpt (s)),
7625 sRef_getNullInfoLoc (s));
7629 (message ("Storage %qnull state becomes unknown",
7630 sRef_unparseOpt (s)),
7631 sRef_getNullInfoLoc (s));
7640 (message ("<error case> Storage %q becomes %s",
7642 nstate_unparse (sRef_getNullState (s))),
7643 sRef_getNullInfoLoc (s));
7651 sRef_showAliasInfo (sRef s)
7653 if (sRef_hasAliasInfoLoc (s))
7655 if (sRef_isFresh (s))
7658 (message ("Fresh storage %qallocated", sRef_unparseOpt (s)),
7659 sRef_getAliasInfoLoc (s));
7663 if (!sRef_isRefCounted (s))
7666 (message ("Storage %qbecomes %s",
7667 sRef_unparseOpt (s),
7668 alkind_unparse (sRef_getAliasKind (s))),
7669 sRef_getAliasInfoLoc (s));
7676 sRef_mergeNullState (sRef s, nstate n)
7678 if (sRef_isValid (s))
7682 old = sRef_getNullState (s);
7684 if (n != old && n != NS_UNKNOWN)
7686 sRef_setNullState (s, n, g_currentloc);
7691 llbuglit ("sRef_mergeNullState: invalid");
7696 sRef_possiblyNull (sRef s)
7698 if (sRef_isValid (s))
7700 if (sRef_getNullState (s) == NS_ABSNULL)
7702 ctype rct = ctype_realType (s->type);
7704 if (ctype_isAbstract (rct))
7710 if (ctype_isUser (rct))
7712 uentry ue = usymtab_getTypeEntry (ctype_typeId (rct));
7714 return (nstate_possiblyNull
7715 (sRef_getNullState (uentry_getSref (ue))));
7725 return nstate_possiblyNull (sRef_getNullState (s));
7733 sRef_getScopeName (sRef s)
7735 sRef base = sRef_getRootBase (s);
7737 if (sRef_isRealGlobal (base))
7739 return (cstring_makeLiteralTemp ("Global"));
7741 else if (sRef_isFileStatic (base))
7743 return (cstring_makeLiteralTemp ("Static"));
7747 return (cstring_makeLiteralTemp ("Local"));
7752 sRef_unparseScope (sRef s)
7754 sRef base = sRef_getRootBase (s);
7756 if (sRef_isRealGlobal (base))
7758 return (cstring_makeLiteralTemp ("global"));
7760 else if (sRef_isFileStatic (base))
7762 return (cstring_makeLiteralTemp ("file static"));
7771 sRef_getScope (sRef s)
7773 llassert (sRef_isValid (s));
7775 if (sRef_isCvar (s))
7777 return s->info->cvar->lexlevel;
7779 else if (sRef_isParam (s))
7790 sRef_isDead (sRef s)
7792 return (sRef_isValid (s) && (s)->defstate == SS_DEAD);
7796 sRef_isDeadStorage (sRef s)
7798 if (sRef_isValid (s))
7800 if (s->defstate == SS_DEAD
7801 || s->defstate == SS_UNUSEABLE
7802 || s->defstate == SS_UNDEFINED
7803 || s->defstate == SS_UNKNOWN)
7809 return (sRef_isDefinitelyNull (s));
7819 sRef_isPossiblyDead (sRef s)
7821 return (sRef_isValid (s) && s->defstate == SS_HOFFA);
7824 bool sRef_isStateLive (sRef s)
7826 if (sRef_isValid (s))
7828 sstate ds = s->defstate;
7830 return (!(ds == SS_UNDEFINED
7832 || ds == SS_UNUSEABLE
7833 || ds == SS_HOFFA));
7842 bool sRef_isStateUndefined (sRef s)
7844 return ((sRef_isValid(s)) && ((s)->defstate == SS_UNDEFINED));
7847 bool sRef_isJustAllocated (sRef s)
7849 if (sRef_isAllocated (s))
7851 sRefSet_allElements (s->deriv, el)
7853 if (!(sRef_isStateUndefined (el) || sRef_isUnuseable (el)))
7857 } end_sRefSet_allElements ;
7866 sRef_isAllocatedStorage (sRef s)
7868 if (sRef_isValid (s) && ynm_toBoolStrict (sRef_isReadable (s)))
7870 return (ctype_isVisiblySharable (sRef_getType (s)));
7879 sRef_isUnuseable (sRef s)
7881 return ((sRef_isValid(s)) && ((s)->defstate == SS_UNUSEABLE));
7885 sRef_perhapsNull (sRef s)
7887 if (sRef_isValid (s))
7889 if (sRef_getNullState (s) == NS_ABSNULL)
7891 ctype rct = ctype_realType (s->type);
7893 if (ctype_isAbstract (rct))
7899 if (ctype_isUser (rct))
7901 uentry ue = usymtab_getTypeEntry (ctype_typeId (rct));
7903 return (nstate_perhapsNull
7904 (sRef_getNullState (uentry_getSref (ue))));
7914 return nstate_perhapsNull (sRef_getNullState (s));
7922 ** definitelyNull --- called when TRUE is good
7926 sRef_definitelyNull (sRef s)
7928 return (sRef_isValid (s)
7929 && (sRef_getNullState (s) == NS_DEFNULL || sRef_getNullState (s) == NS_CONSTNULL));
7933 ** based on sRef_similar
7937 sRef_setDerivNullState (sRef set, sRef guide, nstate ns)
7939 if (sRef_isValid (set))
7941 sRef deriv = sRef_getDeriv (set, guide);
7943 if (sRef_isValid (deriv))
7945 sRef_setNullStateN (deriv, ns);
7950 static /*@exposed@*/ sRef
7951 sRef_getDeriv (/*@returned@*/ /*@notnull@*/ sRef set, sRef guide)
7953 llassert (sRef_isValid (set));
7954 llassert (sRef_isValid (guide));
7956 switch (guide->kind)
7959 llassert (set->kind == SK_CVAR);
7964 llassert (set->kind == guide->kind);
7965 llassert (set->info->paramno == guide->info->paramno);
7971 if (set->kind == SK_ARRAYFETCH
7972 && (sRef_similar (set->info->arrayfetch->arr,
7973 guide->info->arrayfetch->arr)))
7979 return (sRef_makeAnyArrayFetch
7980 (sRef_getDeriv (set, guide->info->arrayfetch->arr)));
7985 if (set->kind == SK_PTR && sRef_similar (set->info->ref, guide->info->ref))
7991 return (sRef_makePointer (sRef_getDeriv (set, guide->info->ref)));
7996 if ((set->kind == SK_FIELD &&
7997 (sRef_similar (set->info->field->rec, guide->info->field->rec) &&
7998 cstring_equal (set->info->field->field, guide->info->field->field))))
8004 return (sRef_makeField (sRef_getDeriv (set, guide->info->field->rec),
8005 guide->info->field->field));
8009 if ((set->kind == SK_ADR) && sRef_similar (set->info->ref, guide->info->ref))
8015 return (sRef_makeAddress (sRef_getDeriv (set, guide->info->ref)));
8020 return sRef_undefined;
8024 case SK_UNCONSTRAINED:
8032 return sRef_undefined;
8039 ** sRef_aliasCheckPred
8041 ** A confusing but spiffy function:
8043 ** Calls predf (s, e, text, <alias>) on s and all of s's aliases
8044 ** (unless checkAliases (s) is FALSE).
8046 ** For alias calls, calls as
8047 ** predf (alias, e, text, s)
8051 sRef_aliasCheckPred (bool (predf) (sRef, exprNode, sRef, exprNode),
8052 /*@null@*/ bool (checkAliases) (sRef),
8053 sRef s, exprNode e, exprNode err)
8055 bool error = (*predf)(s, e, sRef_undefined, err);
8058 if (checkAliases != NULL && !(checkAliases (s)))
8060 /* don't check aliases */
8064 sRefSet aliases = usymtab_allAliases (s);
8067 sRefSet_realElements (aliases, current)
8069 if (sRef_isValid (current))
8071 if (!sRef_similar (current, s)
8072 || (error && sRef_sameName (current, s)))
8074 (void) (*predf)(current, e, s, err);
8077 } end_sRefSet_realElements;
8079 sRefSet_free (aliases);
8084 ** return TRUE iff predf (s) is true for s or any alias of s
8088 sRef_aliasCheckSimplePred (sRefTest predf, sRef s)
8099 aliases = usymtab_allAliases (s);
8101 sRefSet_realElements (aliases, current)
8103 if (sRef_isValid (current))
8105 sRef cref = sRef_updateSref (current);
8107 /* Whoa! a very kludgey way to make sure the right sref is used
8108 ** where there is a conditional symbol table. I am beginning
8109 ** to think that having a conditional symbol table wasn't such
8115 DPRINTF (("Checking alias: %s", sRef_unparseFull (cref)));
8116 sRefSet_free (aliases);
8120 } end_sRefSet_realElements;
8122 sRefSet_free (aliases);
8128 sRef_aliasCompleteSimplePred (bool (predf) (sRef), sRef s)
8131 bool result = FALSE;
8134 aliases = usymtab_allAliases (s);
8136 if ((*predf)(s)) result = TRUE;
8139 sRefSet_realElements (aliases, current)
8141 if (sRef_isValid (current))
8143 current = sRef_updateSref (current);
8144 if ((*predf)(current)) result = TRUE;
8146 } end_sRefSet_realElements;
8148 sRefSet_free (aliases);
8153 sRef_aliasSetComplete (void (predf) (sRef, fileloc), sRef s, fileloc loc)
8157 aliases = usymtab_allAliases (s);
8159 DPRINTF (("All aliases: %s", sRefSet_unparseFull (aliases)));
8163 sRefSet_realElements (aliases, current)
8165 if (sRef_isValid (current))
8167 current = sRef_updateSref (current);
8168 ((*predf)(current, loc));
8170 } end_sRefSet_realElements;
8172 sRefSet_free (aliases);
8176 sRef_aliasSetCompleteParam (void (predf) (sRef, alkind, fileloc), sRef s,
8177 alkind kind, fileloc loc)
8182 if (sRef_isDeep (s))
8184 aliases = usymtab_allAliases (s);
8188 aliases = usymtab_aliasedBy (s);
8191 (*predf)(s, kind, loc);
8193 sRefSet_realElements (aliases, current)
8195 if (sRef_isValid (current))
8197 current = sRef_updateSref (current);
8198 ((*predf)(current, kind, loc));
8200 } end_sRefSet_realElements;
8202 sRefSet_free (aliases);
8206 sRef_innerAliasSetComplete (void (predf) (sRef, fileloc), sRef s, fileloc loc)
8212 if (!sRef_isValid (s)) return;
8216 ** Type equivalence checking is necessary --- there might be casting.
8223 case SK_UNCONSTRAINED:
8228 inner = s->info->ref;
8229 aliases = usymtab_allAliases (inner);
8230 ct = sRef_getType (inner);
8233 sRefSet_realElements (aliases, current)
8235 if (sRef_isValid (current))
8237 current = sRef_updateSref (current);
8239 if (ctype_equal (ct, sRef_getType (current)))
8241 sRef ptr = sRef_makePointer (current);
8242 ((*predf)(ptr, loc));
8245 } end_sRefSet_realElements;
8247 sRefSet_free (aliases);
8250 inner = s->info->arrayfetch->arr;
8251 aliases = usymtab_allAliases (inner);
8252 ct = sRef_getType (inner);
8254 sRefSet_realElements (aliases, current)
8256 if (sRef_isValid (current))
8258 current = sRef_updateSref (current);
8260 if (ctype_equal (ct, sRef_getType (current)))
8263 if (s->info->arrayfetch->indknown)
8265 sRef af = sRef_makeArrayFetchKnown (current, s->info->arrayfetch->ind);
8267 ((*predf)(af, loc));
8271 sRef af = sRef_makeArrayFetch (current);
8273 ((*predf)(af, loc));
8277 } end_sRefSet_realElements;
8279 sRefSet_free (aliases);
8282 inner = s->info->field->rec;
8283 aliases = usymtab_allAliases (inner);
8284 ct = sRef_getType (inner);
8287 sRefSet_realElements (aliases, current)
8289 if (sRef_isValid (current))
8291 current = sRef_updateSref (current);
8293 if (ctype_equal (ct, sRef_getType (current)))
8295 sRef f = sRef_makeField (current, s->info->field->field);
8300 } end_sRefSet_realElements;
8302 sRefSet_free (aliases);
8305 sRef_innerAliasSetComplete (predf, s->info->conj->a, loc);
8306 sRef_innerAliasSetComplete (predf, s->info->conj->b, loc);
8323 sRef_innerAliasSetCompleteParam (void (predf) (sRef, sRef), sRef s, sRef t)
8329 if (!sRef_isValid (s)) return;
8333 ** Type equivalence checking is necessary --- there might be casting.
8340 case SK_UNCONSTRAINED:
8345 inner = s->info->ref;
8346 aliases = usymtab_allAliases (inner);
8347 ct = sRef_getType (inner);
8350 sRefSet_realElements (aliases, current)
8352 if (sRef_isValid (current))
8354 current = sRef_updateSref (current);
8356 if (ctype_equal (ct, sRef_getType (current)))
8358 sRef ptr = sRef_makePointer (current);
8363 } end_sRefSet_realElements;
8365 sRefSet_free (aliases);
8368 inner = s->info->arrayfetch->arr;
8369 aliases = usymtab_allAliases (inner);
8370 ct = sRef_getType (inner);
8372 sRefSet_realElements (aliases, current)
8374 if (sRef_isValid (current))
8376 current = sRef_updateSref (current);
8378 if (ctype_equal (ct, sRef_getType (current)))
8381 if (s->info->arrayfetch->indknown)
8383 sRef af = sRef_makeArrayFetchKnown (current, s->info->arrayfetch->ind);
8389 sRef af = sRef_makeArrayFetch (current);
8395 } end_sRefSet_realElements;
8397 sRefSet_free (aliases);
8400 inner = s->info->field->rec;
8401 aliases = usymtab_allAliases (inner);
8402 ct = sRef_getType (inner);
8405 sRefSet_realElements (aliases, current)
8407 if (sRef_isValid (current))
8409 current = sRef_updateSref (current);
8411 if (ctype_equal (ct, sRef_getType (current)))
8413 sRef f = sRef_makeField (current, s->info->field->field);
8418 } end_sRefSet_realElements;
8420 sRefSet_free (aliases);
8423 sRef_innerAliasSetCompleteParam (predf, s->info->conj->a, t);
8424 sRef_innerAliasSetCompleteParam (predf, s->info->conj->b, t);
8440 static void sRef_combineExKinds (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other)
8442 exkind a1 = sRef_getExKind (res);
8443 exkind a2 = sRef_getExKind (other);
8445 if (a1 == a2 || a2 == XO_UNKNOWN)
8449 else if (a1 == XO_UNKNOWN)
8451 res->expinfo = stateInfo_update (res->expinfo, other->expinfo);
8456 res->expkind = XO_OBSERVER;
8461 ** Currently, this is a very ad hoc implementation, with lots of fixes to
8462 ** make real code work okay. I need to come up with some more general
8463 ** rules or principles here.
8467 sRef_combineAliasKindsError (/*@notnull@*/ sRef res,
8468 /*@notnull@*/ sRef other,
8469 clause cl, fileloc loc)
8471 bool hasError = FALSE;
8472 alkind ares = sRef_getAliasKind (res);
8473 alkind aother = sRef_getAliasKind (other);
8475 sRef_checkMutable (res);
8477 if (alkind_isDependent (ares))
8479 if (aother == AK_KEPT)
8481 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8482 res->aliaskind = AK_KEPT;
8486 if (aother == AK_LOCAL || aother == AK_STATIC
8487 || alkind_isTemp (aother))
8489 DPRINTF (("Setting dependent: %s", sRef_unparseFull (res)));
8490 res->aliaskind = AK_DEPENDENT;
8494 else if (alkind_isDependent (aother))
8496 if (ares == AK_KEPT)
8498 res->aliaskind = AK_KEPT;
8502 if (ares == AK_LOCAL || ares == AK_STATIC || alkind_isTemp (ares))
8504 DPRINTF (("Setting dependent: %s", sRef_unparseFull (res)));
8505 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8506 res->aliaskind = AK_DEPENDENT;
8510 else if ((ares == AK_LOCAL || ares == AK_UNIQUE
8511 || ares == AK_STATIC || alkind_isTemp (ares))
8512 && sRef_isFresh (other))
8515 ** cases like: if (s == NULL) s = malloc...;
8516 ** don't generate errors
8519 if (usymtab_isAltDefinitelyNullDeep (res))
8521 res->aliaskind = ares;
8528 else if ((aother == AK_LOCAL || aother == AK_UNIQUE
8529 || aother == AK_STATIC || alkind_isTemp (aother))
8530 && sRef_isFresh (res))
8533 ** cases like: if (s == NULL) s = malloc...;
8534 ** don't generate errors
8537 if (usymtab_isDefinitelyNullDeep (other))
8539 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8540 res->aliaskind = aother;
8547 else if (ares == AK_NEWREF && aother == AK_REFCOUNTED
8548 && sRef_isConst (other))
8550 res->aliaskind = AK_NEWREF;
8552 else if (aother == AK_NEWREF && ares == AK_REFCOUNTED
8553 && sRef_isConst (res))
8555 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8556 res->aliaskind = AK_NEWREF;
8558 else if (sRef_isLocalVar (res)
8559 && ((ares == AK_KEPT && aother == AK_LOCAL)
8560 || (aother == AK_KEPT && ares == AK_LOCAL)))
8562 res->aliaskind = AK_KEPT;
8571 if (sRef_isThroughArrayFetch (res))
8574 (FLG_BRANCHSTATE, FLG_STRICTBRANCHSTATE,
8576 ("Clauses exit with %q possibly referencing %s storage %s, "
8579 alkind_unparse (aother),
8580 clause_nameTaken (cl),
8581 alkind_unparse (ares),
8582 clause_nameAlternate (cl)),
8585 sRef_showAliasInfo (res);
8586 sRef_showAliasInfo (other);
8587 res->aliaskind = AK_ERROR;
8591 if (ares == AK_KEPT || aother == AK_KEPT)
8593 sRef_maybeKill (res, loc);
8601 message ("Clauses exit with %q referencing %s storage %s, "
8604 alkind_unparse (aother),
8605 clause_nameTaken (cl),
8606 alkind_unparse (ares),
8607 clause_nameAlternate (cl)),
8610 sRef_showAliasInfo (res);
8611 sRef_showAliasInfo (other);
8613 res->aliaskind = AK_ERROR;
8617 res->aliaskind = (sRef_isLocalVar (res) ? AK_LOCAL : AK_UNKNOWN);
8622 sRef_combineAliasKinds (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other,
8623 clause cl, fileloc loc)
8625 alkind ares = sRef_getAliasKind (res);
8626 alkind aother = sRef_getAliasKind (other);
8628 sRef_checkMutable (res);
8630 if (alkind_equal (ares, aother)
8631 || aother == AK_UNKNOWN
8632 || aother == AK_ERROR)
8634 ; /* keep current state */
8636 else if (sRef_isDead (res) || sRef_isDead (other))
8638 /* dead error reported (or storage is dead) */
8639 res ->aliaskind = AK_ERROR;
8641 else if (ares == AK_UNKNOWN || ares == AK_ERROR
8642 || sRef_isStateUndefined (res))
8644 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8645 res->aliaskind = aother;
8647 else if (sRef_isStateUndefined (other))
8651 else if (((ares == AK_UNIQUE || alkind_isTemp (ares))
8652 && aother == AK_LOCAL)
8653 || ((aother == AK_UNIQUE || alkind_isTemp (aother))
8654 && ares == AK_LOCAL))
8656 if (ares != AK_LOCAL)
8658 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8661 res->aliaskind = AK_LOCAL;
8663 else if ((ares == AK_OWNED && aother == AK_FRESH)
8664 || (aother == AK_OWNED && ares == AK_FRESH))
8666 if (ares != AK_FRESH)
8668 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8671 res->aliaskind = AK_FRESH;
8673 else if ((ares == AK_KEEP && aother == AK_FRESH) ||
8674 (aother == AK_KEEP && ares == AK_FRESH))
8676 if (ares != AK_KEEP)
8678 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8681 res->aliaskind = AK_KEEP;
8683 else if ((ares == AK_LOCAL && aother == AK_STACK) ||
8684 (aother == AK_LOCAL && ares == AK_STACK))
8686 if (ares != AK_STACK)
8688 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8691 res->aliaskind = AK_STACK;
8693 else if ((ares == AK_LOCAL
8694 && (aother == AK_OWNED && sRef_isLocalVar (other)))
8695 || (aother == AK_LOCAL
8696 && (ares == AK_OWNED && sRef_isLocalVar (res))))
8698 if (ares != AK_LOCAL)
8700 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8703 res->aliaskind = AK_LOCAL;
8705 else if ((ares == AK_FRESH && alkind_isOnly (aother))
8706 || (aother == AK_FRESH && alkind_isOnly (ares)))
8708 res->aliaskind = AK_FRESH;
8710 else if ((aother == AK_FRESH && sRef_definitelyNull (res))
8711 || (ares == AK_FRESH && sRef_definitelyNull (other)))
8713 if (ares != AK_FRESH)
8715 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8716 res->aliaskind = AK_FRESH;
8719 else if ((sRef_isFresh (res) && sRef_isConst (other))
8720 || (sRef_isFresh (other) && sRef_isConst (res)))
8723 ** for NULL constantants
8727 if (!sRef_isFresh (res))
8729 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8732 res->aliaskind = AK_FRESH;
8734 else if ((alkind_isStatic (aother) && sRef_isConst (res))
8735 || (alkind_isStatic (ares) && sRef_isConst (other)))
8737 if (!alkind_isStatic (ares))
8739 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
8740 res->aliaskind = AK_STATIC;
8745 sRef_combineAliasKindsError (res, other, cl, loc);
8749 static void sRef_combineDefState (/*@notnull@*/ sRef res,
8750 /*@notnull@*/ sRef other)
8752 sstate s1 = res->defstate;
8753 sstate s2 = other->defstate;
8756 sRef_checkMutable (res);
8758 if (s1 == s2 || s2 == SS_UNKNOWN)
8762 else if (s1 == SS_UNKNOWN)
8771 if (s2 == SS_DEFINED)
8777 llcontbuglit ("ssfixed: not implemented");
8786 flip = (s2 != SS_DEFINED);
8804 res->definfo = stateInfo_update (res->definfo, other->definfo);
8809 extern /*@notnull@*/ sRef sRef_getConjA (sRef s)
8812 llassert (sRef_isConj (s));
8814 ret = s->info->conj->a;
8815 llassert (ret != NULL);
8819 extern /*@notnull@*/ sRef sRef_getConjB (sRef s)
8822 llassert (sRef_isConj (s));
8824 ret = s->info->conj->b;
8825 llassert (ret != NULL);
8829 extern /*@exposed@*/ sRef sRef_makeArrow (sRef s, /*@dependent@*/ cstring f)
8834 p = sRef_makePointer (s);
8835 ret = sRef_makeField (p, f);
8839 extern /*@exposed@*/ sRef sRef_buildArrow (sRef s, cstring f)
8844 p = sRef_buildPointer (s);
8845 ret = sRef_buildField (p, f);
8850 static /*@null@*/ sinfo sinfo_copy (/*@notnull@*/ sRef s)
8857 ret = (sinfo) dmalloc (sizeof (*ret));
8858 ret->cvar = (cref) dmalloc (sizeof (*ret->cvar));
8859 ret->cvar->lexlevel = s->info->cvar->lexlevel;
8860 ret->cvar->index = s->info->cvar->index;
8864 ret = (sinfo) dmalloc (sizeof (*ret));
8865 ret->paramno = s->info->paramno;
8866 llassert (ret->paramno >= -1);
8870 ret = (sinfo) dmalloc (sizeof (*ret));
8871 ret->arrayfetch = (ainfo) dmalloc (sizeof (*ret->arrayfetch));
8872 ret->arrayfetch->indknown = s->info->arrayfetch->indknown;
8873 ret->arrayfetch->ind = s->info->arrayfetch->ind;
8874 ret->arrayfetch->arr = s->info->arrayfetch->arr; /* sRef_copy (s->info->arrayfetch->arr); */ /*@i32@*/
8878 ret = (sinfo) dmalloc (sizeof (*ret));
8879 ret->field = (fldinfo) dmalloc (sizeof (*ret->field));
8880 ret->field->rec = s->info->field->rec; /* sRef_copy (s->info->field->rec); */ /*@i32@*/
8881 ret->field->field = s->info->field->field;
8885 ret = (sinfo) dmalloc (sizeof (*ret));
8886 ret->object = s->info->object;
8893 ret = (sinfo) dmalloc (sizeof (*ret));
8894 ret->ref = s->info->ref; /* Ref_copy (s->info->ref); */
8898 ret = (sinfo) dmalloc (sizeof (*ret));
8899 ret->conj = (cjinfo) dmalloc (sizeof (*ret->conj));
8900 ret->conj->a = s->info->conj->a; /* sRef_copy (s->info->conj->a); */
8901 ret->conj->b = s->info->conj->b; /* sRef_copy (s->info->conj->b);*/
8904 ret = (sinfo) dmalloc (sizeof (*ret));
8905 ret->spec = s->info->spec;
8907 case SK_UNCONSTRAINED:
8909 ret = (sinfo) dmalloc (sizeof (*ret));
8910 ret->fname = s->info->fname;
8916 llassertprint (s->info == NULL, ("s = %s", sRef_unparse (s)));
8924 static /*@null@*/ sinfo sinfo_fullCopy (/*@notnull@*/ sRef s)
8929 ** Since its a full copy, only storage is assigned
8930 ** to dependent fields.
8937 ret = (sinfo) dmalloc (sizeof (*ret));
8938 ret->cvar = (cref) dmalloc (sizeof (*ret->cvar));
8939 ret->cvar->lexlevel = s->info->cvar->lexlevel;
8940 ret->cvar->index = s->info->cvar->index;
8944 ret = (sinfo) dmalloc (sizeof (*ret));
8945 ret->paramno = s->info->paramno;
8946 llassert (ret->paramno >= -1);
8950 ret = (sinfo) dmalloc (sizeof (*ret));
8951 ret->arrayfetch = (ainfo) dmalloc (sizeof (*ret->arrayfetch));
8952 ret->arrayfetch->indknown = s->info->arrayfetch->indknown;
8953 ret->arrayfetch->ind = s->info->arrayfetch->ind;
8954 ret->arrayfetch->arr = sRef_saveCopy (s->info->arrayfetch->arr);
8958 ret = (sinfo) dmalloc (sizeof (*ret));
8959 ret->field = (fldinfo) dmalloc (sizeof (*ret->field));
8960 ret->field->rec = sRef_saveCopy (s->info->field->rec);
8961 ret->field->field = s->info->field->field;
8965 ret = (sinfo) dmalloc (sizeof (*ret));
8966 ret->object = s->info->object;
8973 ret = (sinfo) dmalloc (sizeof (*ret));
8974 ret->ref = sRef_saveCopy (s->info->ref);
8978 ret = (sinfo) dmalloc (sizeof (*ret));
8979 ret->conj = (cjinfo) dmalloc (sizeof (*ret->conj));
8980 ret->conj->a = sRef_saveCopy (s->info->conj->a);
8981 ret->conj->b = sRef_saveCopy (s->info->conj->b);
8984 ret = (sinfo) dmalloc (sizeof (*ret));
8985 ret->spec = s->info->spec;
8988 case SK_UNCONSTRAINED:
8989 ret = (sinfo) dmalloc (sizeof (*ret));
8990 ret->fname = s->info->fname;
8996 llassert (s->info == NULL);
9006 sinfo_update (/*@notnull@*/ /*@exposed@*/ sRef res,
9007 /*@notnull@*/ /*@exposed@*/ sRef other)
9009 llassert (res->kind == other->kind);
9014 res->info->cvar->lexlevel = other->info->cvar->lexlevel;
9015 res->info->cvar->index = other->info->cvar->index;
9019 res->info->paramno = other->info->paramno;
9020 llassert (res->info->paramno >= -1);
9024 res->info->arrayfetch->indknown = other->info->arrayfetch->indknown;
9025 res->info->arrayfetch->ind = other->info->arrayfetch->ind;
9026 res->info->arrayfetch->arr = other->info->arrayfetch->arr;
9030 res->info->field->rec = other->info->field->rec;
9031 res->info->field->field = other->info->field->field;
9035 res->info->object = other->info->object;
9042 res->info->ref = other->info->ref;
9046 res->info->conj->a = other->info->conj->a;
9047 res->info->conj->b = other->info->conj->b;
9051 res->info->spec = other->info->spec;
9055 case SK_UNCONSTRAINED:
9056 res->info->fname = other->info->fname;
9063 llassert (res->info == NULL);
9068 static void sinfo_free (/*@special@*/ /*@temp@*/ /*@notnull@*/ sRef s)
9069 /*@uses s->kind, s->info@*/
9070 /*@releases s->info@*/
9075 DPRINTF (("Free sinfo: [%p]", s->info->cvar));
9076 sfree (s->info->cvar);
9083 DPRINTF (("Free sinfo: [%p]", s->info->arrayfetch));
9084 sfree (s->info->arrayfetch);
9088 DPRINTF (("Free sinfo: [%p]", s->info->field));
9089 sfree (s->info->field);
9098 case SK_EXTERNAL: /*@i32 is copy now! */
9102 DPRINTF (("Free sinfo: [%p]", s->info->conj));
9103 sfree (s->info->conj);
9106 case SK_UNCONSTRAINED:
9116 if (s->info != NULL) {
9117 DPRINTF (("Free sinfo: [%p]", s->info));
9123 bool sRef_isNSLocalVar (sRef s)
9125 if (sRef_isLocalVar (s))
9127 uentry ue = sRef_getUentry (s);
9129 return (!uentry_isStatic (ue));
9137 bool sRef_isLocalVar (sRef s)
9139 if (sRef_isValid(s))
9141 return (s->kind == SK_CVAR
9142 && (s->info->cvar->lexlevel > fileScope));
9148 bool sRef_isRealLocalVar (sRef s)
9150 if (sRef_isValid(s))
9152 if (s->kind == SK_CVAR)
9154 if (s->info->cvar->lexlevel == functionScope)
9156 uentry ue = sRef_getUentry (s);
9158 if (uentry_isAnyParam (ue)
9159 || uentry_isRefParam (ue))
9170 return (s->info->cvar->lexlevel > functionScope);
9178 bool sRef_isLocalParamVar (sRef s)
9180 if (sRef_isValid(s))
9182 return (s->kind == SK_PARAM
9183 || (s->kind == SK_CVAR
9184 && (s->info->cvar->lexlevel > fileScope)));
9190 static speckind speckind_fromInt (int i)
9193 llassert (i >= SR_NOTHING && i <= SR_SYSTEM);
9196 return ((speckind) i);
9200 static void sRef_updateNullState (sRef res, sRef other)
9203 res->nullstate = other->nullstate;
9204 res->nullinfo = stateInfo_update (res->nullinfo, other->nullinfo);
9207 void sRef_combineNullState (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other)
9209 nstate n1 = sRef_getNullState (res);
9210 nstate n2 = sRef_getNullState (other);
9214 if (n1 == n2 || n2 == NS_UNKNOWN)
9220 /* note: n2 is not unknown or defnull */
9224 case NS_ERROR: nn = NS_ERROR; break;
9225 case NS_UNKNOWN: flip = TRUE; nn = n2; break;
9226 case NS_POSNULL: break;
9227 case NS_DEFNULL: nn = NS_POSNULL; break;
9228 case NS_RELNULL: break;
9230 if (n2 == NS_MNOTNULL)
9241 if (n2 == NS_NOTNULL)
9260 res->nullinfo = stateInfo_update (res->nullinfo, other->nullinfo);
9263 res->nullstate = nn;
9266 cstring sRef_nullMessage (sRef s)
9268 llassert (sRef_isValid (s));
9270 switch (sRef_getNullState (s))
9274 return (cstring_makeLiteralTemp ("null"));
9276 return (cstring_makeLiteralTemp ("possibly null"));
9281 /*@observer@*/ cstring sRef_ntMessage (sRef s)
9283 llassert (sRef_isValid (s));
9285 switch (s->nullstate)
9289 return (cstring_makeLiteralTemp ("not nullterminated"));
9291 return (cstring_makeLiteralTemp ("possibly non-nullterminated"));
9298 sRef sRef_fixResultType (/*@returned@*/ sRef s, ctype typ, uentry ue)
9300 sRef tmp = sRef_undefined;
9303 llassert (sRef_isValid (s));
9314 tmp = sRef_fixResultType (s->info->arrayfetch->arr, typ, ue);
9316 ct = ctype_realType (sRef_getType (tmp));
9319 if (ctype_isKnown (ct))
9321 if (ctype_isAP (ct))
9330 ("Special clause indexes non-array (%t): *%q",
9331 ct, sRef_unparse (s->info->arrayfetch->arr)),
9332 uentry_whereLast (ue));
9336 tmp = sRef_fixResultType (s->info->arrayfetch->arr, typ, ue);
9338 if (s->info->arrayfetch->indknown)
9340 ret = sRef_makeArrayFetchKnown (tmp, s->info->arrayfetch->ind);
9344 ret = sRef_makeArrayFetch (tmp);
9350 sRef rec = sRef_fixResultType (s->info->field->rec, typ, ue);
9351 ctype ct = ctype_realType (sRef_getType (rec));
9353 if (ctype_isKnown (ct))
9355 if (ctype_isSU (ct))
9357 if (uentry_isValid (uentryList_lookupField (ctype_getFields (ct),
9358 s->info->field->field)))
9367 ("Special clause accesses non-existent field of result: %q.%s",
9368 sRef_unparse (rec), s->info->field->field),
9369 uentry_whereLast (ue));
9377 ("Special clause accesses field of non-struct or union result (%t): %q.%s",
9378 ct, sRef_unparse (rec), s->info->field->field),
9379 uentry_whereLast (ue));
9383 ret = sRef_makeField (tmp, s->info->field->field);
9389 tmp = sRef_fixResultType (s->info->ref, typ, ue);
9391 ct = ctype_realType (sRef_getType (tmp));
9393 if (ctype_isKnown (ct))
9395 if (ctype_isAP (ct))
9404 ("Special clause dereferences non-pointer (%t): *%q",
9405 ct, sRef_unparse (s->info->ref)),
9406 uentry_whereLast (ue));
9410 ret = sRef_makePointer (tmp);
9417 ("Special clause uses & operator (not allowed): &%q", sRef_unparse (s->info->ref)),
9418 uentry_whereLast (ue));
9427 bool sRef_isOnly (sRef s)
9429 return (sRef_isValid(s) && alkind_isOnly (s->aliaskind));
9432 bool sRef_isDependent (sRef s)
9434 return (sRef_isValid(s) && alkind_isDependent (s->aliaskind));
9437 bool sRef_isOwned (sRef s)
9439 return (sRef_isValid (s) && (s->aliaskind == AK_OWNED));
9442 bool sRef_isKeep (sRef s)
9444 return (sRef_isValid (s) && (s->aliaskind == AK_KEEP));
9447 bool sRef_isTemp (sRef s)
9449 return (sRef_isValid (s) && alkind_isTemp (s->aliaskind));
9452 bool sRef_isLocalState (sRef s)
9454 return (sRef_isValid (s) && (s->aliaskind == AK_LOCAL));
9457 bool sRef_isUnique (sRef s)
9459 return (sRef_isValid (s) && (s->aliaskind == AK_UNIQUE));
9462 bool sRef_isShared (sRef s)
9464 return (sRef_isValid (s) && (s->aliaskind == AK_SHARED));
9467 bool sRef_isExposed (sRef s)
9469 return (sRef_isValid (s) && (s->expkind == XO_EXPOSED));
9472 bool sRef_isObserver (sRef s)
9474 return (sRef_isValid (s) && (s->expkind == XO_OBSERVER));
9477 bool sRef_isFresh (sRef s)
9479 return (sRef_isValid (s) && (s->aliaskind == AK_FRESH));
9482 bool sRef_isDefinitelyNull (sRef s)
9484 return (sRef_isValid (s) && (sRef_getNullState (s) == NS_DEFNULL
9485 || sRef_getNullState (s) == NS_CONSTNULL));
9488 bool sRef_isAllocated (sRef s)
9490 return (sRef_isValid (s) && (s->defstate == SS_ALLOCATED));
9493 bool sRef_isStack (sRef s)
9495 return (sRef_isValid (s) && (s->aliaskind == AK_STACK));
9498 bool sRef_isNotNull (sRef s)
9500 return (sRef_isValid(s) && (sRef_getNullState (s) == NS_MNOTNULL
9501 || sRef_getNullState (s) == NS_NOTNULL));
9504 alkind sRef_getAliasKind (sRef s)
9506 if (sRef_isValid(s)) {
9507 llassert (alkind_isValid (s->aliaskind));
9508 return s->aliaskind;
9514 nstate sRef_getNullState (sRef s)
9516 if (sRef_isValid (s)) {
9517 llassert (nstate_isValid (s->nullstate));
9518 return s->nullstate;
9524 void sRef_reflectAnnotation (sRef s, annotationInfo a, fileloc loc)
9526 if (sRef_isValid (s))
9528 if (!valueTable_isDefined (s->state))
9530 s->state = valueTable_create (1);
9531 valueTable_insert (s->state,
9532 cstring_copy (metaStateInfo_getName (annotationInfo_getState (a))),
9533 stateValue_create (annotationInfo_getValue (a), stateInfo_makeLoc (loc)));
9537 DPRINTF (("reflect loc: %s", fileloc_unparse (loc)));
9540 metaStateInfo_getName (annotationInfo_getState (a)),
9541 stateValue_create (annotationInfo_getValue (a), stateInfo_makeLoc (loc)));
9542 DPRINTF (("state info: %s", stateInfo_unparse (stateInfo_makeLoc (loc))));
9543 DPRINTF (("sref: %s", sRef_unparse (s)));
9544 DPRINTF (("sref: %s", sRef_unparseFull (s)));
9549 void sRef_setMetaStateValueComplete (sRef s, cstring key, int value, fileloc loc)
9551 sRefSet aliases = usymtab_allAliases (s);
9553 sRef_setMetaStateValue (s, key, value, loc);
9555 sRefSet_realElements (aliases, current)
9557 if (sRef_isValid (current))
9559 current = sRef_updateSref (current);
9560 sRef_setMetaStateValue (current, key, value, loc);
9562 } end_sRefSet_realElements ;
9564 sRefSet_free (aliases);
9567 void sRef_setMetaStateValue (sRef s, cstring key, int value, fileloc loc)
9569 sRef_checkMutable (s);
9571 if (sRef_isValid (s))
9573 if (!valueTable_isDefined (s->state))
9575 DPRINTF (("inserting state: %s: %s %d", sRef_unparse (s), key, value));
9576 s->state = valueTable_create (1);
9577 valueTable_insert (s->state, cstring_copy (key),
9578 stateValue_create (value, stateInfo_makeLoc (loc)));
9582 DPRINTF (("Updating state: %s: %s %d / %s", sRef_unparse (s), key, value,
9583 fileloc_unparse (loc)));
9584 if (valueTable_contains (s->state, key))
9587 (s->state, key, stateValue_create (value, stateInfo_makeLoc (loc)));
9592 (s->state, cstring_copy (key), stateValue_create (value, stateInfo_makeLoc (loc)));
9595 DPRINTF (("After: %s", sRef_unparseFull (s)));
9600 bool sRef_checkMetaStateValue (sRef s, cstring key, int value)
9602 if (sRef_isValid (s))
9604 if (valueTable_isDefined (s->state))
9608 DPRINTF (("check state: %s: %s %d", sRef_unparse (s), key, value));
9610 val = valueTable_lookup (s->state, key);
9611 llassert (stateValue_isDefined (val));
9612 return (stateValue_isError (val)
9613 || stateValue_getValue (val) == value);
9626 /*@observer@*/ stateValue sRef_getMetaStateValue (sRef s, cstring key)
9628 if (sRef_isValid (s))
9630 if (valueTable_isDefined (s->state))
9634 val = valueTable_lookup (s->state, key);
9635 /* Okay if its not defined, just returns stateValue_undefined */
9640 return stateValue_undefined;
9645 return stateValue_undefined;
9649 /*@observer@*/ valueTable sRef_getValueTable (sRef s)
9651 DPRINTF (("Get value table: %s", sRef_unparse (s)));
9653 if (sRef_isValid (s))
9655 llassert (sRef_isValid (s));
9656 DPRINTF (("Value table: %s", valueTable_unparse (s->state)));
9661 DPRINTF (("No value table!"));
9662 return valueTable_undefined;
9666 bool sRef_makeStateSpecial (sRef s)
9669 ** Default defined state can be made special.
9672 llassert (sRef_isValid (s)); /*@i523 why doesn't null-checking work!??? */
9674 if (s->defstate == SS_UNKNOWN || s->defstate == SS_DEFINED || s->defstate == SS_SPECIAL)
9676 s->aliaskind = AK_IMPTEMP;
9677 s->defstate = SS_SPECIAL;
9678 DPRINTF (("Made special: %s", sRef_unparseFull (s)));
9683 s->aliaskind = AK_IMPTEMP;
9684 s->defstate = SS_SPECIAL;
9689 void sRef_markImmutable (sRef s)
9691 if (sRef_isValid (s))
9693 DPRINTF (("Mark immutable: %s", sRef_unparseFull (s)));
9698 bool sRef_definitelyNullContext (sRef s)
9700 return (sRef_definitelyNull (s)
9701 || usymtab_isDefinitelyNullDeep (s));
9704 bool sRef_definitelyNullAltContext (sRef s)
9706 return (sRef_definitelyNull (s)
9707 || usymtab_isAltDefinitelyNullDeep (s));
9711 /* start modifications */
9712 struct s_bbufinfo sRef_getNullTerminatedState (sRef p_s) {
9713 struct s_bbufinfo BUFSTATE_UNKNOWN;
9714 BUFSTATE_UNKNOWN.bufstate = BB_NOTNULLTERMINATED;
9715 BUFSTATE_UNKNOWN.size = 0;
9716 BUFSTATE_UNKNOWN.len = 0;
9718 if (sRef_isValid(p_s))
9719 return p_s->bufinfo;
9720 return BUFSTATE_UNKNOWN;
9723 void sRef_setNullTerminatedState(sRef p_s) {
9724 if(sRef_isValid (p_s)) {
9725 p_s->bufinfo.bufstate = BB_NULLTERMINATED;
9727 llfatalbug( message("sRef_setNT passed a invalid sRef\n"));
9732 void sRef_setPossiblyNullTerminatedState(sRef p_s) {
9733 if( sRef_isValid (p_s)) {
9734 p_s->bufinfo.bufstate = BB_POSSIBLYNULLTERMINATED;
9736 llfatalbug( message("sRef_setPossNT passed a invalid sRef\n"));
9740 void sRef_setNotNullTerminatedState(sRef p_s) {
9741 if( sRef_isValid (p_s)) {
9742 p_s->bufinfo.bufstate = BB_NOTNULLTERMINATED;
9744 llfatalbug( message("sRef_unsetNT passed a invalid sRef\n"));
9748 void sRef_setLen(sRef p_s, int len) {
9749 if( sRef_isValid (p_s) && sRef_isNullTerminated(p_s)) {
9750 p_s->bufinfo.len = len;
9752 llfatalbug( message("sRef_setLen passed a invalid sRef\n"));
9757 void sRef_setSize(sRef p_s, int size) {
9758 if( sRef_isValid(p_s)) {
9759 p_s->bufinfo.size = size;
9761 llfatalbug( message("sRef_setSize passed a invalid sRef\n"));
9765 void sRef_resetLen(sRef p_s) {
9766 if (sRef_isValid (p_s)) {
9767 p_s->bufinfo.len = 0;
9769 llfatalbug (message ("sRef_setLen passed an invalid sRef\n"));
9773 /*drl7x 11/28/2000 */
9775 bool sRef_isFixedArray (sRef p_s) /*@*/ {
9777 c = sRef_getType (p_s);
9778 return ( ctype_isFixedArray (c) );
9781 long int sRef_getArraySize (sRef p_s) /*@*/ {
9783 llassert (sRef_isFixedArray(p_s) );
9784 DPRINTF (( message ("sRef_getArraySize getting array size for %s", sRef_unparse(p_s) ) ));
9786 c = sRef_getType (p_s);
9788 return (ctype_getArraySize (c) );