2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2000 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 "aliasChecks.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 bool sRef_isDerived (sRef p_s) /*@*/ ;
56 static /*@exposed@*/ sRef sRef_fixDirectBase (sRef p_s, sRef p_base)
57 /*@modifies p_base@*/ ;
59 static bool sRef_isAllocatedStorage (sRef p_s) /*@*/ ;
60 static void sRef_setNullErrorLoc (sRef p_s, fileloc) /*@*/ ;
63 sRef_aliasSetComplete (void (p_predf) (sRef, fileloc), sRef p_s, fileloc p_loc)
66 static int sRef_depth (sRef p_s) /*@*/ ;
69 sRef_innerAliasSetComplete (void (p_predf) (sRef, fileloc), sRef p_s,
74 sRef_innerAliasSetCompleteParam (void (p_predf) (sRef, sRef), sRef p_s, sRef p_t)
78 sRef_aliasSetCompleteParam (void (p_predf) (sRef, alkind, fileloc), sRef p_s,
79 alkind p_kind, fileloc p_loc)
82 static speckind speckind_fromInt (int p_i);
83 static bool sRef_equivalent (sRef p_s1, sRef p_s2);
84 static bool sRef_isDeepUnionField (sRef p_s);
85 static void sRef_addDeriv (/*@notnull@*/ sRef p_s, /*@notnull@*/ sRef p_t);
86 static sRef sRef_makeResultType (ctype p_ct) /*@*/ ;
87 static bool sRef_checkModify (sRef p_s, sRefSet p_sl) /*@*/ ;
89 static bool skind_isSimple (skind sk)
93 case SK_PARAM: case SK_CVAR: case SK_CONST:
94 case SK_OBJECT: case SK_UNKNOWN: case SK_NEW:
101 static void sinfo_free (/*@special@*/ /*@temp@*/ /*@notnull@*/ sRef p_s)
102 /*@uses p_s->kind, p_s->info@*/
103 /*@releases p_s->info@*/ ;
105 static /*@null@*/ sinfo sinfo_copy (/*@notnull@*/ sRef p_s) /*@*/ ;
106 static void sRef_setPartsFromUentry (sRef p_s, uentry p_ue)
108 static bool checkDeadState (/*@notnull@*/ sRef p_el, bool p_tbranch, fileloc p_loc);
109 static sRef sRef_constructPointerAux (/*@notnull@*/ /*@exposed@*/ sRef p_t) /*@*/ ;
112 sRef_combineExKinds (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other)
113 /*@modifies p_res@*/ ;
116 sRef_combineAliasKinds (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other,
117 clause p_cl, fileloc p_loc)
118 /*@modifies p_res@*/ ;
121 sRef_combineNullState (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other)
122 /*@modifies p_res@*/ ;
125 sRef_combineDefState (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other)
126 /*@modifies p_res@*/ ;
128 static void sRef_setStateFromAbstractUentry (sRef p_s, uentry p_ue)
132 sinfo_update (/*@notnull@*/ /*@exposed@*/ sRef p_res,
133 /*@notnull@*/ /*@exposed@*/ sRef p_other);
134 static /*@only@*/ alinfo alinfo_makeRefLoc (/*@exposed@*/ sRef p_ref, fileloc p_loc);
135 static void sRef_setDefinedAux (sRef p_s, fileloc p_loc, bool p_clear)
137 static void sRef_setDefinedNoClear (sRef p_s, fileloc p_loc)
139 static void sRef_setStateAux (sRef p_s, sstate p_ss, fileloc p_loc)
142 static /*@exposed@*/ sRef
143 sRef_buildNCField (/*@exposed@*/ sRef p_rec, /*@exposed@*/ cstring p_f);
146 sRef_mergeStateAux (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other,
147 clause p_cl, bool p_opt, fileloc p_loc,
149 /*@modifies p_res, p_other@*/ ;
151 static /*@null@*/ sinfo sinfo_fullCopy (/*@notnull@*/ sRef p_s);
152 static bool sRef_doModify (sRef p_s, sRefSet p_sl) /*@modifies p_s@*/ ;
153 static bool sRef_doModifyVal (sRef p_s, sRefSet p_sl) /*@modifies p_s@*/;
154 static bool sRef_checkModifyVal (sRef p_s, sRefSet p_sl) /*@*/ ;
156 static /*@only@*/ sRefSet
157 sRef_mergeDerivs (/*@only@*/ sRefSet p_res, sRefSet p_other,
158 bool p_opt, clause p_cl, fileloc p_loc);
160 static /*@only@*/ sRefSet
161 sRef_mergeUnionDerivs (/*@only@*/ sRefSet p_res,
162 /*@exposed@*/ sRefSet p_other,
163 bool p_opt, clause p_cl, fileloc p_loc);
165 static /*@only@*/ sRefSet
166 sRef_mergePdefinedDerivs (/*@only@*/ sRefSet p_res, sRefSet p_other, bool p_opt,
167 clause p_cl, fileloc p_loc);
169 static /*@only@*/ cstring sRef_unparseWithArgs (sRef p_s, uentryList p_args);
170 static /*@only@*/ cstring sRef_unparseNoArgs (sRef p_s);
172 static /*@exposed@*/ sRef sRef_findDerivedPointer (sRef p_s);
173 static /*@exposed@*/ sRef sRef_findDerivedField (/*@notnull@*/ sRef p_rec, cstring p_f);
174 static /*@exposed@*/ sRef
175 sRef_getDeriv (/*@notnull@*/ /*@returned@*/ sRef p_set, sRef p_guide);
177 static bool inFunction = FALSE;
178 static /*@only@*/ sRefTable allRefs;
180 /* # define DEBUGREFS */
184 static totnsrefs = 0;
185 static maxnsrefs = 0;
186 static ntotrefers = 0;
190 /*@constant null alinfo alinfo_undefined; @*/
191 # define alinfo_undefined ((alinfo) NULL)
193 static /*@only@*/ /*@notnull@*/ alinfo alinfo_makeLoc (fileloc p_loc);
194 static /*@only@*/ alinfo alinfo_copy (alinfo p_a);
196 static /*@checked@*/ bool protectDerivs = FALSE;
198 void sRef_protectDerivs (void) /*@modifies protectDerivs@*/
200 llassert (!protectDerivs);
201 protectDerivs = TRUE;
204 void sRef_clearProtectDerivs (void) /*@modifies protectDerivs@*/
206 llassert (protectDerivs);
207 protectDerivs = FALSE;
211 ** hmmm...here be kind of a hack. This function mysteriously appeared
212 ** in my code, but I'm sure I didn't write it.
216 sRef_isRecursiveField (sRef s)
218 if (sRef_isField (s))
220 if (sRef_depth (s) > 13)
225 fieldname = sRef_getField (s);
226 base = sRef_getBase (s);
228 while (sRef_isValid (base))
230 if (sRef_isField (base))
232 if (cstring_equal (fieldname, sRef_getField (base)))
238 base = sRef_getBaseSafe (base);
247 sRef_addDeriv (/*@notnull@*/ sRef s, /*@notnull@*/ sRef t)
249 if (!context_inProtectVars ()
253 && !sRef_isConst (s))
255 int sd = sRef_depth (s);
256 int td = sRef_depth (t);
263 if (sRef_isGlobal (s))
265 if (context_inFunctionLike ()
266 && ctype_isKnown (sRef_getType (s))
267 && !ctype_isFunction (sRef_getType (s)))
269 globSet g = context_getUsedGlobs ();
271 if (!globSet_member (g, s))
274 ** don't report as a bug
278 (message ("sRef_addDeriv: global variable not in used "
279 "globs: %q / %s / %q",
281 ctype_unparse (sRef_getType (s)),
282 sRefSet_unparse (s->deriv)));
287 s->deriv = sRefSet_insert (s->deriv, t);
293 s->deriv = sRefSet_insert (s->deriv, t);
299 sRef_deepPred (bool (predf) (sRef), sRef s)
301 if (sRef_isValid (s))
303 if ((*predf)(s)) return TRUE;
308 return (sRef_deepPred (predf, s->info->ref));
310 return (sRef_deepPred (predf, s->info->arrayfetch->arr));
312 return (sRef_deepPred (predf, s->info->field->rec));
314 return (sRef_deepPred (predf, s->info->conj->a)
315 || sRef_deepPred (predf, s->info->conj->b));
324 bool sRef_modInFunction (void)
329 void sRef_setStateFromType (sRef s, ctype ct)
331 if (sRef_isValid (s))
333 if (ctype_isUser (ct))
335 sRef_setStateFromUentry
336 (s, usymtab_getTypeEntry (ctype_typeId (ct)));
338 else if (ctype_isAbstract (ct))
340 sRef_setStateFromAbstractUentry
341 (s, usymtab_getTypeEntry (ctype_typeId (ct)));
345 ; /* not a user type */
350 static void sRef_setTypeState (sRef s)
352 if (sRef_isValid (s))
354 sRef_setStateFromType (s, s->type);
358 static void alinfo_free (/*@only@*/ alinfo a)
362 fileloc_free (a->loc);
367 static /*@only@*/ alinfo alinfo_update (/*@only@*/ alinfo old, alinfo newinfo)
369 ** returns an alinfo with the same value as new. May reuse the
370 ** storage of old. (i.e., same effect as copy, but more
376 old = alinfo_copy (newinfo);
378 else if (newinfo == NULL)
385 old->loc = fileloc_update (old->loc, newinfo->loc);
386 old->ref = newinfo->ref;
387 old->ue = newinfo->ue;
393 static /*@only@*/ alinfo alinfo_updateLoc (/*@only@*/ alinfo old, fileloc loc)
397 old = alinfo_makeLoc (loc);
401 old->loc = fileloc_update (old->loc, loc);
402 old->ue = uentry_undefined;
403 old->ref = sRef_undefined;
409 static /*@only@*/ alinfo
410 alinfo_updateRefLoc (/*@only@*/ alinfo old, /*@dependent@*/ sRef ref, fileloc loc)
414 old = alinfo_makeRefLoc (ref, loc);
418 old->loc = fileloc_update (old->loc, loc);
419 old->ue = uentry_undefined;
426 static /*@only@*/ alinfo
427 alinfo_copy (alinfo a)
435 alinfo ret = (alinfo) dmalloc (sizeof (*ret));
437 ret->loc = fileloc_copy (a->loc); /*< should report bug without copy! >*/
446 sRef_hasAliasInfoLoc (sRef s)
448 return (sRef_isValid (s) && (s->aliasinfo != NULL)
449 && (fileloc_isDefined (s->aliasinfo->loc)));
452 static /*@falsenull@*/ bool
453 sRef_hasStateInfoLoc (sRef s)
455 return (sRef_isValid (s) && (s->definfo != NULL)
456 && (fileloc_isDefined (s->definfo->loc)));
459 static /*@falsenull@*/ bool
460 sRef_hasExpInfoLoc (sRef s)
462 return (sRef_isValid (s)
463 && (s->expinfo != NULL) && (fileloc_isDefined (s->expinfo->loc)));
467 sRef_hasNullInfoLoc (sRef s)
469 return (sRef_isValid (s) && (s->nullinfo != NULL)
470 && (fileloc_isDefined (s->nullinfo->loc)));
474 sRef_hasAliasInfoRef (sRef s)
476 return (sRef_isValid (s) && (s->aliasinfo != NULL)
477 && (sRef_isValid (s->aliasinfo->ref)));
480 static /*@observer@*/ fileloc
481 sRef_getAliasInfoLoc (/*@exposed@*/ sRef s)
483 llassert (sRef_isValid (s) && s->aliasinfo != NULL
484 && (fileloc_isDefined (s->aliasinfo->loc)));
485 return (s->aliasinfo->loc);
488 static /*@observer@*/ fileloc
489 sRef_getStateInfoLoc (/*@exposed@*/ sRef s)
491 llassert (sRef_isValid (s) && s->definfo != NULL
492 && (fileloc_isDefined (s->definfo->loc)));
493 return (s->definfo->loc);
496 static /*@observer@*/ fileloc
497 sRef_getExpInfoLoc (/*@exposed@*/ sRef s)
499 llassert (sRef_isValid (s) && s->expinfo != NULL
500 && (fileloc_isDefined (s->expinfo->loc)));
501 return (s->expinfo->loc);
504 static /*@observer@*/ fileloc
505 sRef_getNullInfoLoc (/*@exposed@*/ sRef s)
507 llassert (sRef_isValid (s) && s->nullinfo != NULL
508 && (fileloc_isDefined (s->nullinfo->loc)));
509 return (s->nullinfo->loc);
513 sRef_getAliasInfoRef (/*@exposed@*/ sRef s)
515 llassert (sRef_isValid (s) && s->aliasinfo != NULL);
516 return (s->aliasinfo->ref);
519 static /*@only@*/ /*@notnull@*/ alinfo
520 alinfo_makeLoc (fileloc loc)
522 alinfo ret = (alinfo) dmalloc (sizeof (*ret));
524 ret->loc = fileloc_copy (loc); /* don't need to copy! */
525 ret->ue = uentry_undefined;
526 ret->ref = sRef_undefined;
531 static /*@only@*/ alinfo
532 alinfo_makeRefLoc (/*@exposed@*/ sRef ref, fileloc loc)
534 alinfo ret = (alinfo) dmalloc (sizeof (*ret));
536 ret->loc = fileloc_copy (loc);
538 ret->ue = uentry_undefined;
544 ** This function should be called before new sRefs are created
545 ** somewhere where they will have a lifetime greater than the
546 ** current function scope.
549 void sRef_setGlobalScope ()
551 llassert (inFunction);
555 void sRef_clearGlobalScope ()
557 llassert (!inFunction);
561 static bool oldInFunction = FALSE;
563 void sRef_setGlobalScopeSafe ()
565 oldInFunction = inFunction;
569 void sRef_clearGlobalScopeSafe ()
571 inFunction = oldInFunction;
574 void sRef_enterFunctionScope ()
576 llassert (!inFunction);
577 llassert (sRefTable_isEmpty (allRefs));
581 void sRef_exitFunctionScope ()
586 sRefTable_clear (allRefs);
591 llbuglit ("sRef_exitFunctionScope: not in function");
595 void sRef_destroyMod () /*@globals killed allRefs;@*/
598 llmsg (message ("Live: %d / %d ", nsrefs, totnsrefs));
601 sRefTable_free (allRefs);
605 ** Result of sRef_alloc is dependent since allRefs may
606 ** reference it. It is only if !inFunction.
609 static /*@dependent@*/ /*@out@*/ /*@notnull@*/ sRef
612 sRef s = (sRef) dmalloc (sizeof (*s));
616 allRefs = sRefTable_add (allRefs, s);
622 if (nsrefs >= maxnsrefs)
631 /*@-mustfree@*/ /*@-freshtrans@*/
633 /*@=mustfree@*/ /*@=freshtrans@*/
636 static /*@dependent@*/ /*@notnull@*/ /*@special@*/ sRef
639 /*@post:isnull result->aliasinfo, result->definfo, result->nullinfo,
640 result->expinfo, result->info, result->deriv@*/
642 sRef s = sRef_alloc ();
644 s->kind = SK_UNKNOWN;
647 s->type = ctype_unknown;
648 s->defstate = SS_UNKNOWN;
650 /* start modifications */
651 s->bufinfo.bufstate = BB_NOTNULLTERMINATED;
652 /* end modifications */
654 s->aliaskind = AK_UNKNOWN;
655 s->oaliaskind = AK_UNKNOWN;
657 s->nullstate = NS_UNKNOWN;
659 s->expkind = XO_UNKNOWN;
660 s->oexpkind = XO_UNKNOWN;
662 s->aliasinfo = alinfo_undefined;
663 s->definfo = alinfo_undefined;
664 s->nullinfo = alinfo_undefined;
665 s->expinfo = alinfo_undefined;
668 s->deriv = sRefSet_undefined;
673 static /*@notnull@*/ /*@exposed@*/ sRef
674 sRef_fixConj (/*@notnull@*/ sRef s)
679 s = sRef_getConjA (s);
680 } while (sRef_isConj (s));
682 llassert (sRef_isValid (s));
683 return s; /* don't need to ref */
692 sRef_isExternallyVisibleAux (sRef s)
695 sRef base = sRef_getRootBase (s);
697 if (sRef_isValid (base))
699 res = sRef_isParam (base) || sRef_isGlobal (base) || sRef_isExternal (base);
706 sRef_isExternallyVisible (sRef s)
708 return (sRef_aliasCheckSimplePred (sRef_isExternallyVisibleAux, s));
712 sRef_getBaseUentry (sRef s)
714 sRef base = sRef_getRootBase (s);
715 uentry res = uentry_undefined;
717 if (sRef_isValid (base))
722 res = usymtab_getRefQuiet (paramsScope, base->info->paramno);
726 res = usymtab_getRefQuiet (base->info->cvar->lexlevel,
727 base->info->cvar->index);
739 ** lookup the current uentry corresponding to s, and return the corresponding sRef.
740 ** yuk yuk yuk yuk yuk yuk yuk yuk
744 sRef_updateSref (sRef s)
750 if (!sRef_isValid (s)) return sRef_undefined;
761 case SK_UNCONSTRAINED:
769 uentry ue = sRef_getUentry (s);
771 /* must be raw name! (need the marker) */
772 ue = usymtab_lookupSafe (uentry_rawName (ue));
774 if (uentry_isUndefined (ue))
780 return (uentry_getSref (ue));
784 /* special case if ind known */
785 inner = s->info->arrayfetch->arr;
786 ret = sRef_updateSref (inner);
794 res = sRef_makeArrayFetch (ret);
800 inner = s->info->field->rec;
801 ret = sRef_updateSref (inner);
809 res = (sRef_makeField (ret, s->info->field->field));
814 inner = s->info->ref;
815 ret = sRef_updateSref (inner);
822 res = sRef_makePointer (ret);
828 inner = s->info->ref;
829 ret = sRef_updateSref (inner);
837 res = sRef_makeAddress (ret);
844 sRef innera = s->info->conj->a;
845 sRef innerb = s->info->conj->b;
846 sRef reta = sRef_updateSref (innera);
847 sRef retb = sRef_updateSref (innerb);
849 if (innera == reta && innerb == retb)
855 res = sRef_makeConj (reta, retb);
866 sRef_getUentry (sRef s)
868 llassert (sRef_isValid (s));
873 return (usymtab_getRefQuiet (paramsScope, s->info->paramno));
875 return (usymtab_getRefQuiet (s->info->cvar->lexlevel, s->info->cvar->index));
878 if (sRef_isCvar (s->info->conj->a) || sRef_isParam (s->info->conj->a)
879 || sRef_isConj (s->info->conj->a))
881 return sRef_getUentry (s->info->conj->a);
885 return sRef_getUentry (s->info->conj->b);
890 return uentry_undefined;
896 sRef_getParam (sRef s)
898 llassert (sRef_isValid (s));
899 llassert (s->kind == SK_PARAM);
901 return s->info->paramno;
905 sRef_isModified (sRef s)
907 return (!sRef_isValid (s) || s->modified);
910 void sRef_setModified (sRef s)
912 if (sRef_isValid (s))
917 if (sRef_isRefsField (s))
919 sRef base = sRef_getBase (s);
922 llassert (s->kind == SK_FIELD);
925 if (sRef_isPointer (base))
927 base = sRef_getBase (base);
930 if (sRef_isRefCounted (base))
932 base->aliaskind = AK_NEWREF;
940 ** note: this side-effects sRefSet to set modified to TRUE
941 ** for any sRef similar to s.
945 sRef_canModifyVal (sRef s, sRefSet sl)
947 if (context_getFlag (FLG_MUSTMOD))
949 return (sRef_doModifyVal (s, sl));
953 return (sRef_checkModifyVal (s, sl));
958 sRef_canModify (sRef s, sRefSet sl)
961 if (context_getFlag (FLG_MUSTMOD))
963 return (sRef_doModify (s, sl));
967 return (sRef_checkModify (s, sl));
976 bool sRef_checkModifyVal (sRef s, sRefSet sl)
978 if (sRef_isInvalid (s))
985 case SK_UNCONSTRAINED:
989 if (sRef_isGlobal (s))
991 if (context_checkGlobMod (s))
993 return (sRefSet_member (sl, s));
1003 return (sRefSet_member (sl, s)
1004 || alkind_isOnly (sRef_getOrigAliasKind (s)));
1006 /* special case if ind known */
1007 return (sRefSet_member (sl, s) ||
1008 sRef_checkModifyVal (s->info->arrayfetch->arr, sl));
1010 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->field->rec, sl));
1012 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->ref, sl));
1014 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->ref, sl));
1016 return ((sRef_checkModifyVal (s->info->conj->a, sl)) &&
1017 (sRef_checkModifyVal (s->info->conj->b, sl)));
1028 switch (s->info->spec)
1030 case SR_NOTHING: return TRUE;
1032 if (context_getFlag (FLG_INTERNALGLOBS))
1034 return (sRefSet_member (sl, s));
1040 case SR_SPECSTATE: return TRUE;
1041 case SR_SYSTEM: return (sRefSet_member (sl, s));
1044 case SK_RESULT: BADBRANCH;
1050 ** this should probably be elsewhere...
1052 ** returns TRUE iff sl indicates that s can be modified
1055 static bool sRef_checkModify (sRef s, sRefSet sl)
1057 llassert (sRef_isValid (s));
1061 case SK_UNCONSTRAINED:
1065 if (sRef_isGlobal (s))
1067 if (context_checkGlobMod (s))
1069 return (sRefSet_member (sl, s));
1081 return (sRefSet_member (sl, s) ||
1082 sRef_checkModifyVal (s->info->arrayfetch->arr, sl));
1085 sRef sr = s->info->field->rec;
1087 if (sr->kind == SK_PARAM)
1088 return TRUE; /* structs are copied on call */
1090 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->field->rec, sl));
1096 sm = sRefSet_member (sl, s);
1101 return (sRef_checkModifyVal (s->info->ref, sl));
1104 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->ref, sl));
1106 return ((sRef_checkModify (s->info->conj->a, sl)) &&
1107 (sRef_checkModify (s->info->conj->b, sl)));
1117 switch (s->info->spec)
1119 case SR_NOTHING: return TRUE;
1121 if (context_getFlag (FLG_INTERNALGLOBS))
1123 return (sRefSet_member (sl, s));
1129 case SR_SPECSTATE: return TRUE;
1130 case SR_SYSTEM: return (sRefSet_member (sl, s));
1133 case SK_RESULT: BADBRANCH;
1138 cstring sRef_stateVerb (sRef s)
1140 if (sRef_isDead (s))
1142 return cstring_makeLiteralTemp ("released");
1144 else if (sRef_isKept (s))
1146 return cstring_makeLiteralTemp ("kept");
1148 else if (sRef_isDependent (s))
1150 return cstring_makeLiteralTemp ("dependent");
1158 cstring sRef_stateAltVerb (sRef s)
1160 if (sRef_isDead (s))
1162 return cstring_makeLiteralTemp ("live");
1164 else if (sRef_isKept (s))
1166 return cstring_makeLiteralTemp ("not kept");
1168 else if (sRef_isDependent (s))
1170 return cstring_makeLiteralTemp ("independent");
1179 bool sRef_doModifyVal (sRef s, sRefSet sl)
1181 llassert (sRef_isValid (s));
1186 case SK_UNCONSTRAINED:
1190 if (sRef_isGlobal (s))
1193 if (context_checkGlobMod (s))
1195 return (sRefSet_modifyMember (sl, s));
1199 (void) sRefSet_modifyMember (sl, s);
1209 return (sRefSet_modifyMember (sl, s)
1210 || alkind_isOnly (sRef_getOrigAliasKind (s)));
1212 /* special case if ind known */
1213 /* unconditional OR, need side effect */
1214 return (OR (sRefSet_modifyMember (sl, s),
1215 sRef_doModifyVal (s->info->arrayfetch->arr, sl)));
1217 return (OR (sRefSet_modifyMember (sl, s),
1218 sRef_doModifyVal (s->info->field->rec, sl)));
1220 return (OR (sRefSet_modifyMember (sl, s),
1221 sRef_doModifyVal (s->info->ref, sl)));
1223 return (OR (sRefSet_modifyMember (sl, s),
1224 sRef_doModifyVal (s->info->ref, sl)));
1226 return (AND (sRef_doModifyVal (s->info->conj->a, sl) ,
1227 sRef_doModifyVal (s->info->conj->b, sl)));
1237 switch (s->info->spec)
1239 case SR_NOTHING: return TRUE;
1241 if (context_getFlag (FLG_INTERNALGLOBS))
1243 return (sRefSet_modifyMember (sl, s));
1247 (void) sRefSet_modifyMember (sl, s);
1256 return (sRefSet_modifyMember (sl, s));
1260 case SK_RESULT: BADBRANCH;
1266 ** this should probably be elsewhere...
1268 ** returns TRUE iff sl indicates that s can be modified
1272 bool sRef_doModify (sRef s, sRefSet sl)
1274 llassert (sRef_isValid (s));
1278 case SK_UNCONSTRAINED:
1282 if (sRef_isGlobal (s))
1284 if (context_checkGlobMod (s))
1286 return (sRefSet_modifyMember (sl, s));
1290 (void) sRefSet_modifyMember (sl, s);
1302 return (OR (sRefSet_modifyMember (sl, s),
1303 sRef_doModifyVal (s->info->arrayfetch->arr, sl)));
1306 sRef sr = s->info->field->rec;
1308 if (sr->kind == SK_PARAM)
1310 return TRUE; /* structs are shallow-copied on call */
1313 return (OR (sRefSet_modifyMember (sl, s),
1314 sRef_doModifyVal (s->info->field->rec, sl)));
1318 return (OR (sRefSet_modifyMember (sl, s),
1319 sRef_doModifyVal (s->info->ref, sl)));
1322 return (OR (sRefSet_modifyMember (sl, s),
1323 sRef_doModifyVal (s->info->ref, sl)));
1325 return (AND (sRef_doModify (s->info->conj->a, sl),
1326 (sRef_doModify (s->info->conj->b, sl))));
1337 switch (s->info->spec)
1339 case SR_NOTHING: return TRUE;
1340 case SR_INTERNAL: return TRUE;
1341 case SR_SPECSTATE: return TRUE;
1342 case SR_SYSTEM: return (sRefSet_modifyMember (sl, s));
1345 case SK_RESULT: BADBRANCH;
1350 static /*@exposed@*/ sRef
1351 sRef_leastCommon (/*@exposed@*/ sRef s1, sRef s2)
1353 llassert (sRef_similar (s1, s2));
1355 if (!sRef_isValid (s1)) return s1;
1356 if (!sRef_isValid (s2)) return s1;
1358 sRef_combineDefState (s1, s2);
1359 sRef_combineNullState (s1, s2);
1360 sRef_combineExKinds (s1, s2);
1362 if (s1->aliaskind != s2->aliaskind)
1364 if (s1->aliaskind == AK_UNKNOWN)
1366 s1->aliaskind = s2->aliaskind;
1368 else if (s2->aliaskind == AK_UNKNOWN)
1374 s1->aliaskind = AK_ERROR;
1381 int sRef_compare (sRef s1, sRef s2)
1383 if (s1 == s2) return 0;
1385 if (sRef_isInvalid (s1)) return -1;
1386 if (sRef_isInvalid (s2)) return 1;
1388 INTCOMPARERETURN (s1->kind, s2->kind);
1389 INTCOMPARERETURN (s1->defstate, s2->defstate);
1390 INTCOMPARERETURN (s1->aliaskind, s2->aliaskind);
1392 COMPARERETURN (nstate_compare (s1->nullstate, s2->nullstate));
1397 return (int_compare (s1->info->paramno, s2->info->paramno));
1400 COMPARERETURN (sRef_compare (s1->info->arrayfetch->arr,
1401 s2->info->arrayfetch->arr));
1403 if (s1->info->arrayfetch->indknown && s2->info->arrayfetch->indknown)
1405 return (int_compare (s1->info->arrayfetch->ind,
1406 s2->info->arrayfetch->ind));
1408 if (!s1->info->arrayfetch->indknown && !s2->info->arrayfetch->indknown)
1415 COMPARERETURN (sRef_compare (s1->info->field->rec, s2->info->field->rec));
1417 if (cstring_equal (s1->info->field->field, s2->info->field->field))
1424 return (sRef_compare (s1->info->ref, s2->info->ref));
1426 COMPARERETURN (sRef_compare (s1->info->conj->a, s2->info->conj->a));
1427 return (sRef_compare (s1->info->conj->b, s2->info->conj->b));
1428 case SK_UNCONSTRAINED:
1429 return (cstring_compare (s1->info->fname, s2->info->fname));
1441 return (generic_compare (s1->info->spec, s2->info->spec));
1446 static bool cref_equal (cref c1, cref c2)
1448 return ((c1->lexlevel == c2->lexlevel) &&
1449 (usymId_equal (c1->index, c2->index)));
1453 ** returns true if s1 could be the same storage as s2.
1454 ** i.e., a[?] ~ a[3]. Note its not symmetric ... s1
1455 ** should be more specific.
1459 ** like similar, but matches objects <-> non-objects
1463 sRef_uniqueReference (sRef s)
1465 return (sRef_isFresh (s) || sRef_isUnique (s)
1466 || sRef_isOnly (s) || sRef_isStack (s)
1467 || sRef_isAddress (s));
1471 sRef_similarRelaxedAux (sRef s1, sRef s2)
1475 if (sRef_isUnknownArrayFetch (s1))
1485 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
1487 if (sRef_isConj (s2))
1488 return (sRef_similarRelaxedAux (s1, sRef_getConjA (s2)) ||
1489 sRef_similarRelaxedAux (s1, sRef_getConjB (s2)));
1494 return ((s2->kind == SK_CVAR)
1495 && (cref_equal (s1->info->cvar, s2->info->cvar)));
1497 return ((s2->kind == SK_PARAM)
1498 && (s1->info->paramno == s2->info->paramno));
1500 if (s2->kind == SK_ARRAYFETCH)
1502 if (sRef_similarRelaxedAux (s1->info->arrayfetch->arr,
1503 s2->info->arrayfetch->arr))
1505 if (s1->info->arrayfetch->indknown)
1507 if (s2->info->arrayfetch->indknown)
1509 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
1524 return ((s2->kind == SK_FIELD
1525 && (sRef_similarRelaxedAux (s1->info->field->rec,
1526 s2->info->field->rec)
1527 && cstring_equal (s1->info->field->field,
1528 s2->info->field->field))));
1530 return ((s2->kind == SK_PTR)
1531 && sRef_similarRelaxedAux (s1->info->ref, s2->info->ref));
1533 return ((s2->kind == SK_ADR)
1534 && sRef_similarRelaxedAux (s1->info->ref, s2->info->ref));
1536 return ((sRef_similarRelaxedAux (s1->info->conj->a, s2) ||
1537 (sRef_similarRelaxedAux (s1->info->conj->b, s2))));
1539 return (s1->info->spec == s2->info->spec);
1540 case SK_UNCONSTRAINED:
1541 return (cstring_equal (s1->info->fname, s2->info->fname));
1556 sRef_similarRelaxed (sRef s1, sRef s2)
1562 if (sRef_isThroughArrayFetch (s1))
1572 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
1574 us1 = sRef_uniqueReference (s1);
1575 us2 = sRef_uniqueReference (s2);
1577 if ((s1->kind == SK_EXTERNAL && (s2->kind != SK_EXTERNAL && !us2))
1578 || (s2->kind == SK_EXTERNAL && (s1->kind != SK_EXTERNAL && !us1)))
1581 ** Previously, also:
1582 ** || (sRef_isExposed (s1) && !us2) || (sRef_isExposed (s2) && !us1)) ????
1584 ** No clue why this was there?!
1588 if (sRef_isExposed (s1) && sRef_isCvar (s1))
1590 uentry ue1 = sRef_getUentry (s1);
1592 if (uentry_isRefParam (ue1))
1594 return sRef_similarRelaxedAux (s1, s2);
1598 if (sRef_isExposed (s2) && sRef_isCvar (s2))
1600 uentry ue2 = sRef_getUentry (s2);
1602 if (uentry_isRefParam (ue2))
1604 return sRef_similarRelaxedAux (s1, s2);
1608 return (ctype_match (s1->type, s2->type));
1612 return sRef_similarRelaxedAux (s1, s2);
1617 sRef_similar (sRef s1, sRef s2)
1619 if (s1 == s2) return TRUE;
1620 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
1622 if (sRef_isConj (s2))
1624 return (sRef_similar (s1, sRef_getConjA (s2)) ||
1625 sRef_similar (s1, sRef_getConjB (s2)));
1628 if (sRef_isDerived (s2))
1630 return (sRef_includedBy (s1, s2->info->ref));
1636 return ((s2->kind == SK_CVAR)
1637 && (cref_equal (s1->info->cvar, s2->info->cvar)));
1639 return ((s2->kind == SK_PARAM)
1640 && (s1->info->paramno == s2->info->paramno));
1642 if (s2->kind == SK_ARRAYFETCH)
1644 if (sRef_similar (s1->info->arrayfetch->arr,
1645 s2->info->arrayfetch->arr))
1647 if (s1->info->arrayfetch->indknown)
1649 if (s2->info->arrayfetch->indknown)
1651 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
1666 if (s2->kind == SK_PTR)
1668 if (sRef_similar (s1->info->arrayfetch->arr,
1678 return ((s2->kind == SK_FIELD
1679 && (sRef_similar (s1->info->field->rec,
1680 s2->info->field->rec)
1681 && cstring_equal (s1->info->field->field,
1682 s2->info->field->field))));
1684 if (s2->kind == SK_PTR)
1686 return sRef_similar (s1->info->ref, s2->info->ref);
1690 if (s2->kind == SK_ARRAYFETCH)
1692 if (sRef_similar (s2->info->arrayfetch->arr,
1702 return ((s2->kind == SK_ADR)
1703 && sRef_similar (s1->info->ref, s2->info->ref));
1705 return ((sRef_similar (s1->info->conj->a, s2) ||
1706 (sRef_similar (s1->info->conj->b, s2))));
1708 return (sRef_includedBy (s2, s1->info->ref));
1709 case SK_UNCONSTRAINED:
1710 return (s2->kind == SK_UNCONSTRAINED
1711 && cstring_equal (s1->info->fname, s2->info->fname));
1721 return (s2->kind == SK_SPECIAL
1722 && (s1->info->spec == s2->info->spec));
1725 /*@notreached@*/ DPRINTF (("Fell through for: %s / %s", sRef_unparse (s1), sRef_unparse (s2)));
1730 ** return TRUE iff small can be derived from big.
1732 ** (e.g. x, x.a is includedBy x;
1733 ** x.a is included By x.a;
1737 sRef_includedBy (sRef small, sRef big)
1739 if (small == big) return TRUE;
1740 if (sRef_isInvalid (small) || sRef_isInvalid (big)) return FALSE;
1742 if (sRef_isConj (big))
1743 return (sRef_similar (small, sRef_getConjA (big)) ||
1744 sRef_similar (small, sRef_getConjB (big)));
1746 switch (small->kind)
1750 return (sRef_same (small, big));
1752 if (big->kind == SK_ARRAYFETCH)
1754 if (sRef_same (small->info->arrayfetch->arr, big->info->arrayfetch->arr))
1756 if (small->info->arrayfetch->indknown)
1758 if (big->info->arrayfetch->indknown)
1760 return (small->info->arrayfetch->ind == big->info->arrayfetch->ind);
1773 return (sRef_includedBy (small->info->arrayfetch->arr, big));
1775 if (big->kind == SK_FIELD)
1778 (sRef_same (small->info->field->rec, big->info->field->rec) &&
1779 cstring_equal (small->info->field->field, big->info->field->field));
1783 return (sRef_includedBy (small->info->field->rec, big));
1787 if (big->kind == SK_PTR)
1789 return sRef_same (small->info->ref, big->info->ref);
1793 return (sRef_includedBy (small->info->ref, big));
1797 return ((big->kind == SK_ADR) && sRef_similar (small->info->ref, big->info->ref));
1799 return ((sRef_includedBy (small->info->conj->a, big) ||
1800 (sRef_includedBy (small->info->conj->b, big))));
1802 return (sRef_includedBy (small->info->ref, big));
1803 case SK_UNCONSTRAINED:
1813 switch (small->info->spec)
1815 case SR_NOTHING: return TRUE;
1817 case SR_INTERNAL: return (sRef_isSpecInternalState (big) ||
1818 sRef_isFileStatic (big));
1819 case SR_SYSTEM: return (sRef_isSystemState (big));
1826 ** Same is similar to similar, but not quite the same.
1827 ** same and realSame aren't the same, but they are really similar.
1828 ** similarly, same is the same as same. but realSame is
1829 ** not really the same as same, or similar to similar.
1831 ** Similarly to similar, same checks if two sRefs are the same.
1832 ** The similarities end, however, when same compares arrays
1833 ** with unknown indexes. Similar returns false; same returns true.
1835 ** Similarly to similar and same, realSame is the same as same,
1836 ** except they do not behave the same when face with unknown
1837 ** sRefs. Same thinks they are not the same, but realSame thinks
1843 sRef_realSame (sRef s1, sRef s2)
1845 if (s1 == s2) return TRUE;
1846 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
1851 return ((s2->kind == SK_CVAR) && (cref_equal (s1->info->cvar, s2->info->cvar)));
1853 return ((s2->kind == SK_PARAM) && (s1->info->paramno == s2->info->paramno));
1855 if (s2->kind == SK_ARRAYFETCH)
1857 if (sRef_realSame (s1->info->arrayfetch->arr, s2->info->arrayfetch->arr))
1859 if (s1->info->arrayfetch->indknown && s2->info->arrayfetch->indknown)
1861 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
1863 if (!s1->info->arrayfetch->indknown && !s2->info->arrayfetch->indknown)
1872 return ((s2->kind == SK_FIELD &&
1873 (sRef_realSame (s1->info->field->rec, s2->info->field->rec) &&
1874 cstring_equal (s1->info->field->field, s2->info->field->field))));
1876 return ((s2->kind == SK_PTR) && sRef_realSame (s1->info->ref, s2->info->ref));
1878 return ((s2->kind == SK_ADR) && sRef_realSame (s1->info->ref, s2->info->ref));
1880 return ((sRef_realSame (s1->info->conj->a, s2) ||
1881 (sRef_realSame (s1->info->conj->b, s2))));
1883 return ((s2->kind == SK_OBJECT)
1884 && ctype_match (s1->info->object, s2->info->object));
1886 return ((s2->kind == SK_EXTERNAL)
1887 && sRef_realSame (s1->info->ref, s2->info->ref));
1889 return ((s2->kind == SK_SPECIAL) && s1->info->spec == s2->info->spec);
1891 return ((s2->kind == SK_DERIVED) && sRef_realSame (s1->info->ref, s2->info->ref));
1892 case SK_UNCONSTRAINED:
1893 return ((s2->kind == SK_UNCONSTRAINED)
1894 && (cstring_equal (s1->info->fname, s2->info->fname)));
1900 return TRUE; /* changed this! was false */
1906 ** same is similar to similar, but not quite the same.
1908 ** Similarly to similar, same checks is two sRefs are the same.
1909 ** The similarities end, however, when same compares arrays
1910 ** with unknown indexes. Similar returns false; same returns true.
1914 sRef_same (sRef s1, sRef s2)
1916 if (s1 == s2) return TRUE;
1917 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
1922 return ((s2->kind == SK_CVAR) && (cref_equal (s1->info->cvar, s2->info->cvar)));
1924 return ((s2->kind == SK_PARAM) && (s1->info->paramno == s2->info->paramno));
1926 if (s2->kind == SK_ARRAYFETCH)
1928 llassert (s1->info->field->rec != s1);
1929 if (sRef_same (s1->info->arrayfetch->arr, s2->info->arrayfetch->arr))
1931 if (s1->info->arrayfetch->indknown && s2->info->arrayfetch->indknown)
1933 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
1941 llassert (s1->info->field->rec != s1);
1942 return ((s2->kind == SK_FIELD &&
1943 (sRef_same (s1->info->field->rec, s2->info->field->rec) &&
1944 cstring_equal (s1->info->field->field, s2->info->field->field))));
1948 llassert (s1->info->ref != s1);
1949 return ((s2->kind == SK_PTR) && sRef_same (s1->info->ref, s2->info->ref));
1953 llassert (s1->info->ref != s1);
1954 return ((s2->kind == SK_ADR) && sRef_same (s1->info->ref, s2->info->ref));
1957 llassert (s1->info->conj->a != s1);
1958 llassert (s1->info->conj->b != s1);
1959 return ((sRef_same (s1->info->conj->a, s2)) && /* or or and? */
1960 (sRef_same (s1->info->conj->b, s2)));
1962 return ((s2->kind == SK_SPECIAL) && s1->info->spec == s2->info->spec);
1964 llassert (s1->info->ref != s1);
1965 return ((s2->kind == SK_DERIVED) && sRef_same (s1->info->ref, s2->info->ref));
1967 case SK_UNCONSTRAINED:
1980 ** sort of similar, for use in def/use
1984 sRef_closeEnough (sRef s1, sRef s2)
1986 if (s1 == s2) return TRUE;
1987 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
1992 return (((s2->kind == SK_CVAR) &&
1993 (cref_equal (s1->info->cvar, s2->info->cvar))) ||
1994 (s2->kind == SK_UNCONSTRAINED && s1->info->cvar->lexlevel == 0));
1995 case SK_UNCONSTRAINED:
1996 return (s2->kind == SK_UNCONSTRAINED
1997 || ((s2->kind == SK_CVAR) && (s2->info->cvar->lexlevel == 0)));
1999 return ((s2->kind == SK_PARAM)
2000 && (s1->info->paramno == s2->info->paramno));
2002 if (s2->kind == SK_ARRAYFETCH)
2004 if (sRef_closeEnough (s1->info->arrayfetch->arr, s2->info->arrayfetch->arr))
2006 if (s1->info->arrayfetch->indknown && s2->info->arrayfetch->indknown)
2008 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
2015 return ((s2->kind == SK_FIELD &&
2016 (sRef_closeEnough (s1->info->field->rec, s2->info->field->rec) &&
2017 cstring_equal (s1->info->field->field, s2->info->field->field))));
2019 return ((s2->kind == SK_PTR) && sRef_closeEnough (s1->info->ref, s2->info->ref));
2021 return ((s2->kind == SK_ADR) && sRef_closeEnough (s1->info->ref, s2->info->ref));
2023 return ((s2->kind == SK_DERIVED) && sRef_closeEnough (s1->info->ref, s2->info->ref));
2025 return ((sRef_closeEnough (s1->info->conj->a, s2)) ||
2026 (sRef_closeEnough (s1->info->conj->b, s2)));
2028 return ((s2->kind == SK_SPECIAL) && s1->info->spec == s2->info->spec);
2044 s is an sRef of a formal paramenter in a function call constraint
2045 we trys to return a constraint expression derived from the actual parementer of a function call.
2047 /*@only@*/ constraintExpr sRef_fixConstraintParam (/*@observer@*/ sRef s, /*@observer@*/ exprNodeList args)
2051 if (sRef_isInvalid (s))
2052 llfatalbug((message("Invalid sRef")));
2058 s = sRef_saveCopy(s);
2059 ce = constraintExpr_makeTermsRef (s);
2066 temp = (sRef_makeField (sRef_fixBaseParam (s->info->field->rec, args),
2067 s->info->field->field));
2068 ce = constraintExpr_makeTermsRef (sRef_saveCopy(temp));
2074 temp = (sRef_makePointer (sRef_fixBaseParam (s->info->ref, args)));
2075 ce = constraintExpr_makeTermsRef (temp);
2082 temp = sRef_fixBaseParam (s, args);
2083 ce = constraintExpr_makeTermsRef (temp);
2087 ce = constraintExpr_makeTermsRef (s);
2090 llassert(exprNodeList_size (args) > s->info->paramno);
2092 exprNode e = exprNodeList_nth (args, s->info->paramno);
2094 llassert( !(exprNode_isError (e)) );
2095 ce = constraintExpr_makeExprNode (e);
2100 llcontbug ((message("Trying to do fixConstraintParam on nonparam, nonglobal: %s for function with arguments %s", sRef_unparse (s), exprNodeList_unparse(args) ) ));
2101 ce = constraintExpr_makeTermsRef (s);
2110 sRef_fixBaseParam (/*@returned@*/ sRef s, exprNodeList args)
2112 if (sRef_isInvalid (s)) return (sRef_undefined);
2116 case SK_UNCONSTRAINED:
2121 if (exprNodeList_size (args) > s->info->paramno)
2123 exprNode e = exprNodeList_nth (args, s->info->paramno);
2125 if (exprNode_isError (e))
2127 return sRef_makeUnknown ();
2130 return (exprNode_getSref (e));
2134 return sRef_makeUnknown ();
2139 if (s->info->arrayfetch->indknown)
2141 return (sRef_makeArrayFetchKnown
2142 (sRef_fixBaseParam (s->info->arrayfetch->arr, args),
2143 s->info->arrayfetch->ind));
2147 return (sRef_makeArrayFetch
2148 (sRef_fixBaseParam (s->info->arrayfetch->arr, args)));
2151 return (sRef_makeField (sRef_fixBaseParam (s->info->field->rec, args),
2152 s->info->field->field));
2155 return (sRef_makePointer (sRef_fixBaseParam (s->info->ref, args)));
2158 return (sRef_makeAddress (sRef_fixBaseParam (s->info->ref, args)));
2161 return (sRef_makeConj (sRef_fixBaseParam (s->info->conj->a, args),
2162 sRef_fixBaseParam (s->info->conj->b, args)));
2178 sRef_undumpGlobal (char **c)
2188 usymId uid = usymId_fromInt (getInt (c));
2194 defstate = sstate_fromInt (getInt (c));
2197 nullstate = nstate_fromInt (getInt (c));
2199 ret = sRef_makeGlobal (uid, ctype_unknown);
2200 ret->nullstate = nullstate;
2201 ret->defstate = defstate;
2207 speckind sk = speckind_fromInt (i);
2211 case SR_NOTHING: return (sRef_makeNothing ());
2212 case SR_INTERNAL: return (sRef_makeInternalState ());
2213 case SR_SPECSTATE: return (sRef_makeSpecState ());
2214 case SR_SYSTEM: return (sRef_makeSystemState ());
2219 return sRef_undefined;
2221 return sRef_makeUnknown ();
2223 return sRef_makeUnknown ();
2225 llfatalerror (message ("sRef_undumpGlobal: bad line: %s",
2226 cstring_fromChars (*c)));
2232 sRef_undump (char **c)
2241 return (sRef_makeGlobal (usymId_fromInt (getInt (c)), ctype_unknown));
2243 return (sRef_makeParam (getInt (c), ctype_unknown));
2245 return (sRef_makeResultType (ctype_undump (c)));
2248 if ((**c >= '0' && **c <= '9') || **c == '-')
2251 sRef arr = sRef_undump (c);
2252 sRef ret = sRef_buildArrayFetchKnown (arr, i);
2258 sRef arr = sRef_undump (c);
2259 sRef ret = sRef_buildArrayFetch (arr);
2266 cstring fname = cstring_undefined;
2271 fname = cstring_appendChar (fname, **c);
2276 ret = sRef_buildField (sRef_undump (c), fname);
2277 cstring_markOwned (fname);
2283 speckind sk = speckind_fromInt (i);
2287 case SR_NOTHING: return (sRef_makeNothing ());
2288 case SR_INTERNAL: return (sRef_makeInternalState ());
2289 case SR_SPECSTATE: return (sRef_makeSpecState ());
2290 case SR_SYSTEM: return (sRef_makeSystemState ());
2296 sRef ptr = sRef_undump (c);
2297 sRef ret = sRef_makePointer (ptr);
2303 sRef adr = sRef_undump (c);
2304 sRef ret = sRef_makeAddress (adr);
2310 return (sRef_makeObject (ctype_undump (c)));
2314 sRef s1 = sRef_undump (c);
2315 sRef s2 = ((*c)++, sRef_undump (c));
2316 sRef ret = sRef_makeConj (s1, s2);
2321 return sRef_undefined;
2323 return sRef_makeUnknown ();
2325 return sRef_makeUnknown ();
2327 llfatalerror (message ("sRef_undump: bad line: %s", cstring_fromChars (*c)));
2335 if (sRef_isInvalid (s))
2337 return (cstring_makeLiteral ("-"));
2344 return (message ("p%d", s->info->paramno));
2346 if (s->info->arrayfetch->indknown)
2348 return (message ("a%d%q", s->info->arrayfetch->ind,
2349 sRef_dump (s->info->arrayfetch->arr)));
2353 return (message ("a%q", sRef_dump (s->info->arrayfetch->arr)));
2356 return (message ("f%s.%q", s->info->field->field,
2357 sRef_dump (s->info->field->rec)));
2359 return (message ("t%q", sRef_dump (s->info->ref)));
2361 return (message ("d%q", sRef_dump (s->info->ref)));
2363 return (message ("o%q", ctype_dump (s->info->object)));
2365 return (message ("s%d", (int) s->info->spec));
2367 return (message ("c%q.%q",
2368 sRef_dump (s->info->conj->a),
2369 sRef_dump (s->info->conj->b)));
2371 if (sRef_isGlobal (s))
2373 return (message ("g%d",
2374 usymtab_convertId (s->info->cvar->index)));
2378 llcontbug (message ("Dumping local variable: %q",
2379 sRef_unparseDebug (s)));
2380 return (cstring_makeLiteral ("u"));
2383 return (cstring_makeLiteral ("u"));
2385 return (message ("r%q", ctype_dump (s->type)));
2391 case SK_UNCONSTRAINED:
2392 llcontbug (message ("sRef_dump: bad kind: %q",
2393 sRef_unparseFull (s)));
2394 return (cstring_makeLiteral ("x"));
2401 cstring sRef_dumpGlobal (sRef s)
2403 if (sRef_isInvalid (s))
2405 return (cstring_makeLiteral ("-"));
2412 if (sRef_isGlobal (s))
2414 return (message ("g%d@%d@%d",
2415 usymtab_convertId (s->info->cvar->index),
2417 (int) s->nullstate));
2421 llcontbug (message ("Dumping local variable: %q",
2422 sRef_unparseDebug (s)));
2423 return (cstring_makeLiteral ("u"));
2426 return (cstring_makeLiteral ("u"));
2428 return (message ("s%d", (int) s->info->spec));
2430 llcontbug (message ("sRef_dumpGlobal: bad kind: %q",
2431 sRef_unparseFull (s)));
2432 return (cstring_makeLiteral ("x"));
2440 sRef_deriveType (sRef s, uentryList cl)
2442 if (sRef_isInvalid (s)) return ctype_unknown;
2447 return (uentry_getType (usymtab_getRefQuiet (s->info->cvar->lexlevel,
2448 s->info->cvar->index)));
2449 case SK_UNCONSTRAINED:
2450 return (ctype_unknown);
2452 return uentry_getType (uentryList_getN (cl, s->info->paramno));
2455 ctype ca = sRef_deriveType (s->info->arrayfetch->arr, cl);
2457 if (ctype_isArray (ca))
2459 return (ctype_baseArrayPtr (ca));
2461 else if (ctype_isUnknown (ca))
2467 llcontbuglit ("sRef_deriveType: inconsistent array type");
2473 ctype ct = sRef_deriveType (s->info->field->rec, cl);
2475 if (ctype_isStructorUnion (ct))
2477 uentry ue = uentryList_lookupField (ctype_getFields (ct),
2478 s->info->field->field);
2480 if (uentry_isValid (ue))
2482 return (uentry_getType (ue));
2486 llcontbuglit ("sRef_deriveType: bad field");
2487 return ctype_unknown;
2490 else if (ctype_isUnknown (ct))
2496 llcontbuglit ("sRef_deriveType: inconsistent field type");
2502 ctype ct = sRef_deriveType (s->info->ref, cl);
2504 if (ctype_isUnknown (ct)) return ct;
2505 if (ctype_isPointer (ct)) return (ctype_baseArrayPtr (ct));
2508 llcontbuglit ("sRef_deriveType: inconsistent pointer type");
2514 ctype ct = sRef_deriveType (s->info->ref, cl);
2516 if (ctype_isUnknown (ct)) return ct;
2517 return ctype_makePointer (ct);
2521 return sRef_deriveType (s->info->ref, cl);
2525 return (s->info->object);
2529 return (ctype_makeConj (sRef_deriveType (s->info->conj->a, cl),
2530 sRef_deriveType (s->info->conj->b, cl)));
2542 return ctype_unknown;
2548 sRef_getType (sRef s)
2550 if (sRef_isInvalid (s)) return ctype_unknown;
2556 sRef_unparseOpt (sRef s)
2558 sRef rb = sRef_getRootBase (s);
2560 if (sRef_isMeaningful (rb) && !sRef_isConst (rb))
2562 cstring ret = sRef_unparse (s);
2564 llassertprint (!cstring_equalLit (ret, "?"), ("print: %s", sRef_unparseDebug (s)));
2566 if (!cstring_isEmpty (ret))
2568 return (cstring_appendChar (ret, ' '));
2576 return cstring_undefined;
2580 sRef_unparsePreOpt (sRef s)
2582 sRef rb = sRef_getRootBase (s);
2584 if (sRef_isMeaningful (rb) && !sRef_isConst (rb))
2586 cstring ret = sRef_unparse (s);
2588 llassertprint (!cstring_equalLit (ret, "?"), ("print: %s", sRef_unparseDebug (s)));
2589 return (cstring_prependCharO (' ', ret));
2592 return cstring_undefined;
2596 sRef_unparse (sRef s)
2598 if (sRef_isInvalid (s)) return (cstring_makeLiteral ("?"));
2600 if (context_inFunctionLike ())
2602 return (sRef_unparseWithArgs (s, context_getParams ()));
2606 return (sRef_unparseNoArgs (s));
2610 static /*@only@*/ cstring
2611 sRef_unparseWithArgs (sRef s, uentryList args)
2613 if (sRef_isInvalid (s))
2615 return (cstring_makeLiteral ("?"));
2621 return (uentry_getName (usymtab_getRefQuiet (s->info->cvar->lexlevel,
2622 s->info->cvar->index)));
2623 case SK_UNCONSTRAINED:
2624 return (cstring_copy (s->info->fname));
2627 if (s->info->paramno < uentryList_size (args))
2629 uentry ue = uentryList_getN (args, s->info->paramno);
2631 if (uentry_isValid (ue))
2632 return uentry_getName (ue);
2635 return (message ("<bad param: %q / args %q",
2636 sRef_unparseDebug (s),
2637 uentryList_unparse (args)));
2640 if (s->info->arrayfetch->indknown)
2642 return (message ("%q[%d]", sRef_unparseWithArgs (s->info->arrayfetch->arr, args),
2643 s->info->arrayfetch->ind));
2647 return (message ("%q[]", sRef_unparseWithArgs (s->info->arrayfetch->arr, args)));
2650 if (s->info->field->rec->kind == SK_PTR)
2652 sRef ptr = s->info->field->rec;
2654 return (message ("%q->%s", sRef_unparseWithArgs (ptr->info->ref, args),
2655 s->info->field->field));
2657 return (message ("%q.%s", sRef_unparseWithArgs (s->info->field->rec, args),
2658 s->info->field->field));
2662 sRef ref = sRef_fixConj (s->info->ref);
2663 skind sk = ref->kind;
2668 ret = message ("storage pointed to by %q",
2669 sRef_unparseWithArgs (ref, args));
2671 else if (skind_isSimple (sk) || sk == SK_PTR)
2673 ret = message ("*%q", sRef_unparseWithArgs (ref, args));
2677 ret = message ("*(%q)", sRef_unparseWithArgs (ref, args));
2683 return (message ("&%q", sRef_unparseWithArgs (s->info->ref, args)));
2685 return (cstring_copy (ctype_unparse (s->info->object)));
2687 return (sRef_unparseWithArgs (sRef_getConjA (s), args));
2689 if (cstring_isDefined (s->info->fname))
2691 return (message ("[result of %s]", s->info->fname));
2695 return (cstring_makeLiteral ("<new>"));
2698 return (cstring_makeLiteral ("?"));
2700 return (message ("<derived %q>", sRef_unparse (s->info->ref)));
2702 return (message ("<external %q>", sRef_unparse (s->info->ref)));
2704 return (message ("<type %s>", ctype_unparse (s->type)));
2706 return (message ("<const %s>", ctype_unparse (s->type)));
2708 return (cstring_makeLiteral
2709 (s->info->spec == SR_NOTHING ? "nothing"
2710 : s->info->spec == SR_INTERNAL ? "internal state"
2711 : s->info->spec == SR_SPECSTATE ? "spec state"
2712 : s->info->spec == SR_SYSTEM ? "file system state"
2715 return cstring_makeLiteral ("result");
2718 llbug (message ("Bad sref, kind = %d", (int) s->kind));
2726 sRef_unparseDebug (sRef s)
2728 if (sRef_isInvalid (s)) return (cstring_makeLiteral ("<undef>"));
2732 case SK_UNCONSTRAINED:
2733 return (message ("<unconstrained %s>", s->info->fname));
2738 ce = usymtab_getRefQuiet (s->info->cvar->lexlevel, s->info->cvar->index);
2740 if (uentry_isInvalid (ce))
2742 return (message ("<scope: %d.%d *invalid*>",
2743 s->info->cvar->lexlevel,
2744 s->info->cvar->index));
2748 return (message ("<scope: %d.%d *%q*>",
2749 s->info->cvar->lexlevel,
2750 s->info->cvar->index,
2751 uentry_getName (ce)));
2757 return (message ("<parameter %d>", s->info->paramno + 1));
2760 if (s->info->arrayfetch->indknown)
2762 return (message ("%q[%d]", sRef_unparseDebug (s->info->arrayfetch->arr),
2763 s->info->arrayfetch->ind));
2767 return (message ("%q[]", sRef_unparseDebug (s->info->arrayfetch->arr)));
2770 return (message ("%q.%s", sRef_unparseDebug (s->info->field->rec),
2771 s->info->field->field));
2773 return (message ("*(%q)", sRef_unparseDebug (s->info->ref)));
2775 return (message ("&%q", sRef_unparseDebug (s->info->ref)));
2777 return (message ("<object type %s>", ctype_unparse (s->info->object)));
2779 return (message ("%q | %q", sRef_unparseDebug (s->info->conj->a),
2780 sRef_unparseDebug (s->info->conj->b)));
2782 return message ("<new: %s>", s->info->fname);
2784 return (message ("<derived %q>", sRef_unparseDebug (s->info->ref)));
2786 return (message ("<external %q>", sRef_unparseDebug (s->info->ref)));
2788 return (message ("<type %s>", ctype_unparse (s->type)));
2790 return (message ("<const %s>", ctype_unparse (s->type)));
2792 return (message ("<result %s>", ctype_unparse (s->type)));
2794 return (message ("<spec %s>",
2795 cstring_makeLiteralTemp
2796 (s->info->spec == SR_NOTHING ? "nothing"
2797 : s->info->spec == SR_INTERNAL ? "internalState"
2798 : s->info->spec == SR_SPECSTATE ? "spec state"
2799 : s->info->spec == SR_SYSTEM ? "fileSystem"
2802 return cstring_makeLiteral ("<unknown>");
2808 static /*@only@*/ cstring
2809 sRef_unparseNoArgs (sRef s)
2811 if (sRef_isInvalid (s)) return (cstring_makeLiteral ("?"));
2815 case SK_UNCONSTRAINED:
2816 return (cstring_copy (s->info->fname));
2819 uentry ce = usymtab_getRefQuiet (s->info->cvar->lexlevel,
2820 s->info->cvar->index);
2822 if (uentry_isInvalid (ce))
2824 llcontbug (message ("sRef_unparseNoArgs: bad cvar: %q", sRef_unparseDebug (s)));
2825 return (sRef_unparseDebug (s));
2829 return (uentry_getName (ce));
2833 if (s->info->arrayfetch->indknown)
2835 return (message ("%q[%d]", sRef_unparseNoArgs (s->info->arrayfetch->arr),
2836 s->info->arrayfetch->ind));
2840 return (message ("%q[]", sRef_unparseNoArgs (s->info->arrayfetch->arr)));
2843 return (message ("%q.%s", sRef_unparseNoArgs (s->info->field->rec),
2844 s->info->field->field));
2847 sRef ref = sRef_fixConj (s->info->ref);
2848 skind sk = ref->kind;
2851 if (skind_isSimple (sk) || sk == SK_PTR)
2853 ret = message ("*%q", sRef_unparseNoArgs (ref));
2857 ret = message ("*(%q)", sRef_unparseNoArgs (ref));
2863 return (message ("&%q", sRef_unparseNoArgs (s->info->ref)));
2865 return (cstring_copy (ctype_unparse (s->info->object)));
2867 return (sRef_unparseNoArgs (s->info->conj->a));
2869 return (message ("result of %s", s->info->fname));
2871 return (message ("<der %q>", sRef_unparseNoArgs (s->info->ref)));
2873 return message ("<ext %q>", sRef_unparseNoArgs (s->info->ref));
2875 return (cstring_makeLiteral
2876 (s->info->spec == SR_NOTHING ? "nothing"
2877 : s->info->spec == SR_INTERNAL ? "internal state"
2878 : s->info->spec == SR_SPECSTATE ? "spec state"
2879 : s->info->spec == SR_SYSTEM ? "file system state"
2882 return cstring_makeLiteral ("result");
2886 return cstring_makeLiteral ("?");
2888 /* llcontbug (message ("sRef_unparseNoArgs: bad case: %q", sRef_unparseDebug (s))); */
2889 return (sRef_unparseDebug (s));
2894 /*@dependent@*/ sRef sRef_makeUnconstrained (cstring fname)
2896 sRef s = sRef_new ();
2898 s->kind = SK_UNCONSTRAINED;
2899 s->info = (sinfo) dmalloc (sizeof (*s->info));
2900 s->info->fname = fname;
2905 cstring sRef_unconstrainedName (sRef s)
2907 llassert (sRef_isUnconstrained (s));
2909 return s->info->fname;
2912 bool sRef_isUnconstrained (sRef s)
2914 return (sRef_isValid(s) && s->kind == SK_UNCONSTRAINED);
2917 static /*@dependent@*/ /*@notnull@*/ sRef
2918 sRef_makeCvarAux (int level, usymId index, ctype ct)
2920 sRef s = sRef_new ();
2923 s->info = (sinfo) dmalloc (sizeof (*s->info));
2925 s->info->cvar = (cref) dmalloc (sizeof (*s->info->cvar));
2926 s->info->cvar->lexlevel = level;
2927 s->info->cvar->index = index;
2929 /* for now, all globals are defined; all locals, aren't */
2931 if (level <= fileScope)
2933 s->defstate = SS_UNKNOWN;
2937 ctype rct = ctype_realType (ct);
2939 if (level != paramsScope
2940 && (ctype_isStructorUnion (rct) || ctype_isRealArray (rct)))
2942 s->defstate = SS_ALLOCATED;
2943 s->oaliaskind = s->aliaskind = AK_STACK;
2947 s->defstate = SS_UNDEFINED;
2948 s->oaliaskind = s->aliaskind = AK_LOCAL;
2954 llassert (level >= globScope);
2955 llassert (usymId_isValid (index));
2960 /*@dependent@*/ sRef sRef_makeCvar (int level, usymId index, ctype ct)
2962 return (sRef_makeCvarAux (level, index, ct));
2965 int sRef_lexLevel (sRef s)
2967 if (sRef_isValid (s))
2971 conj = sRef_fixConj (s);
2972 s = sRef_getRootBase (conj);
2974 if (sRef_isValid (s) && s->kind == SK_CVAR)
2976 return (s->info->cvar->lexlevel);
2984 sRef_makeGlobal (usymId l, ctype ct)
2986 return (sRef_makeCvar (globScope, l, ct));
2990 sRef_setParamNo (sRef s, int l)
2992 llassert (sRef_isValid (s) && s->kind == SK_PARAM);
2993 s->info->paramno = l;
2996 /*@dependent@*/ sRef
2997 sRef_makeParam (int l, ctype ct)
2999 sRef s = sRef_new ();
3004 s->info = (sinfo) dmalloc (sizeof (*s->info));
3005 s->info->paramno = l;
3006 s->defstate = SS_UNKNOWN; /* (probably defined, unless its an out parameter) */
3012 sRef_isIndexKnown (sRef arr)
3016 llassert (sRef_isValid (arr));
3017 arr = sRef_fixConj (arr);
3019 llassert (arr->kind == SK_ARRAYFETCH);
3020 res = arr->info->arrayfetch->indknown;
3025 sRef_getIndex (sRef arr)
3029 llassert (sRef_isValid (arr));
3030 arr = sRef_fixConj (arr);
3032 llassert (arr->kind == SK_ARRAYFETCH);
3034 if (!arr->info->arrayfetch->indknown)
3036 llcontbug (message ("sRef_getIndex: unknown: %q", sRef_unparse (arr)));
3041 result = arr->info->arrayfetch->ind;
3047 static bool sRef_isZerothArrayFetch (/*@notnull@*/ sRef s)
3049 return (s->kind == SK_ARRAYFETCH
3050 && s->info->arrayfetch->indknown
3051 && (s->info->arrayfetch->ind == 0));
3054 /*@exposed@*/ sRef sRef_makeAddress (/*@exposed@*/ sRef t)
3057 if (sRef_isInvalid (t)) return sRef_undefined;
3059 if (sRef_isPointer (t))
3061 return (t->info->ref);
3063 else if (sRef_isZerothArrayFetch (t))
3065 return (t->info->arrayfetch->arr);
3069 sRef s = sRef_new ();
3072 s->type = ctype_makePointer (t->type);
3073 s->info = (sinfo) dmalloc (sizeof (*s->info));
3076 if (t->defstate == SS_UNDEFINED)
3077 /* no! it is allocated even still: && !ctype_isPointer (t->type)) */
3079 s->defstate = SS_ALLOCATED;
3083 s->defstate = t->defstate;
3086 if (t->aliaskind == AK_LOCAL)
3088 if (sRef_isLocalVar (t))
3090 s->aliaskind = AK_STACK;
3098 cstring sRef_getField (sRef s)
3102 llassert (sRef_isValid (s));
3103 s = sRef_fixConj (s);
3105 llassertprint (sRef_isValid (s) && (s->kind == SK_FIELD),
3106 ("s = %s", sRef_unparseDebug (s)));
3108 res = s->info->field->field;
3112 sRef sRef_getBase (sRef s)
3116 if (sRef_isInvalid (s)) return (sRef_undefined);
3118 s = sRef_fixConj (s);
3129 res = s->info->field->rec;
3133 res = s->info->arrayfetch->arr;
3137 res = sRef_undefined; /* shouldn't need it */
3144 ** same as getBase, except returns invalid
3145 ** (and doesn't use adr's)
3149 sRef_getBaseSafe (sRef s)
3153 if (sRef_isInvalid (s)) { return sRef_undefined; }
3155 s = sRef_fixConj (s);
3163 res = s->info->field->rec; break;
3165 res = s->info->arrayfetch->arr;
3168 res = sRef_undefined; break;
3174 /*@constant int MAXBASEDEPTH;@*/
3175 # define MAXBASEDEPTH 25
3177 static /*@exposed@*/ sRef
3178 sRef_getRootBaseAux (sRef s, int depth)
3180 if (sRef_isInvalid (s)) return sRef_undefined;
3182 if (depth > MAXBASEDEPTH)
3185 ("Warning: reference base limit exceeded for %q. "
3186 "This either means there is a variable with at least "
3187 "%d indirections from this reference, or "
3188 "there is a bug in LCLint.",
3193 return sRef_undefined;
3200 return (sRef_getRootBaseAux (s->info->ref, depth + 1));
3202 return (sRef_getRootBaseAux (s->info->field->rec, depth + 1));
3204 return (sRef_getRootBaseAux (s->info->arrayfetch->arr, depth + 1));
3206 return (sRef_getRootBaseAux (sRef_fixConj (s), depth + 1));
3212 sRef sRef_getRootBase (sRef s)
3214 return (sRef_getRootBaseAux (s, 0));
3217 static bool sRef_isDeep (sRef s)
3219 if (sRef_isInvalid (s)) return FALSE;
3229 return (sRef_isDeep (sRef_fixConj (s)));
3235 static int sRef_depth (sRef s)
3237 if (sRef_isInvalid (s)) return 0;
3245 return 1 + sRef_depth (s->info->ref);
3247 return 1 + sRef_depth (s->info->field->rec);
3249 return 1 + sRef_depth (s->info->arrayfetch->arr);
3251 return (sRef_depth (sRef_fixConj (s)));
3258 sRef_makeObject (ctype o)
3260 sRef s = sRef_new ();
3262 s->kind = SK_OBJECT;
3263 s->info = (sinfo) dmalloc (sizeof (*s->info));
3264 s->info->object = o;
3268 sRef sRef_makeExternal (/*@exposed@*/ sRef t)
3270 sRef s = sRef_new ();
3272 llassert (sRef_isValid (t));
3274 s->kind = SK_EXTERNAL;
3275 s->info = (sinfo) dmalloc (sizeof (*s->info));
3281 sRef sRef_makeDerived (/*@exposed@*/ sRef t)
3283 if (sRef_isValid (t))
3285 sRef s = sRef_new ();
3287 s->kind = SK_DERIVED;
3288 s->info = (sinfo) dmalloc (sizeof (*s->info));
3296 return sRef_undefined;
3301 ** definitely NOT symmetric:
3303 ** res fills in unknown state information from other
3307 sRef_mergeStateQuiet (sRef res, sRef other)
3309 llassert (sRef_isValid (res));
3310 llassert (sRef_isValid (other));
3312 res->modified = res->modified || other->modified;
3313 res->safe = res->safe && other->safe;
3315 if (res->defstate == SS_UNKNOWN)
3317 res->defstate = other->defstate;
3318 res->definfo = alinfo_update (res->definfo, other->definfo);
3321 if (res->aliaskind == AK_UNKNOWN ||
3322 (res->aliaskind == AK_LOCAL && alkind_isKnown (other->aliaskind)))
3324 res->aliaskind = other->aliaskind;
3325 res->oaliaskind = other->oaliaskind;
3326 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
3329 if (res->expkind == XO_UNKNOWN)
3331 res->expkind = other->expkind;
3332 res->oexpkind = other->oexpkind;
3333 res->expinfo = alinfo_update (res->expinfo, other->expinfo);
3336 /* out takes precedence over implicitly defined */
3337 if (res->defstate == SS_DEFINED && other->defstate != SS_UNKNOWN)
3339 res->defstate = other->defstate;
3340 res->definfo = alinfo_update (res->definfo, other->definfo);
3343 if (other->nullstate == NS_ERROR || res->nullstate == NS_ERROR)
3345 res->nullstate = NS_ERROR;
3349 if (other->nullstate != NS_UNKNOWN
3350 && (res->nullstate == NS_UNKNOWN || res->nullstate == NS_NOTNULL
3351 || res->nullstate == NS_MNOTNULL))
3353 res->nullstate = other->nullstate;
3354 res->nullinfo = alinfo_update (res->nullinfo, other->nullinfo);
3360 ** definitely NOT symmetric:
3362 ** res fills in known state information from other
3366 sRef_mergeStateQuietReverse (sRef res, sRef other)
3368 bool changed = FALSE;
3370 llassert (sRef_isValid (res));
3371 llassert (sRef_isValid (other));
3373 if (res->kind != other->kind)
3379 res->kind = other->kind;
3380 res->type = other->type;
3381 res->info = sinfo_fullCopy (other);
3385 if (!ctype_equal (res->type, other->type))
3388 res->type = other->type;
3391 sinfo_update (res, other);
3394 res->modified = res->modified || other->modified;
3395 res->safe = res->safe && other->safe;
3397 if (res->aliaskind != other->aliaskind
3398 && (res->aliaskind == AK_UNKNOWN
3399 || ((res->aliaskind == AK_LOCAL
3400 || (res->aliaskind == AK_REFCOUNTED
3401 && other->aliaskind != AK_LOCAL))
3402 && other->aliaskind != AK_UNKNOWN)))
3405 res->aliaskind = other->aliaskind;
3406 res->oaliaskind = other->oaliaskind;
3407 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
3410 if (other->expkind != XO_UNKNOWN && other->expkind != res->expkind)
3413 res->expkind = other->expkind;
3414 res->expinfo = alinfo_update (res->expinfo, other->expinfo);
3417 if (other->oexpkind != XO_UNKNOWN)
3419 res->oexpkind = other->oexpkind;
3422 /* out takes precedence over implicitly defined */
3424 if (res->defstate != other->defstate)
3426 if (other->defstate != SS_UNKNOWN)
3428 res->defstate = other->defstate;
3432 if (other->nullstate == NS_ERROR || res->nullstate == NS_ERROR)
3434 if (res->nullstate != NS_ERROR)
3436 res->nullstate = NS_ERROR;
3442 if (other->nullstate != NS_UNKNOWN && other->nullstate != res->nullstate)
3445 res->nullstate = other->nullstate;
3446 res->nullinfo = alinfo_update (res->nullinfo, other->nullinfo);
3452 sRef_clearDerived (res);
3457 sRef_mergeState (sRef res, sRef other, clause cl, fileloc loc)
3459 if (sRef_isValid (res) && sRef_isValid (other))
3461 sRef_mergeStateAux (res, other, cl, FALSE, loc, TRUE);
3465 if (sRef_isInvalid (res))
3467 llbug (message ("sRef_mergeState: invalid res sRef: %q",
3468 sRef_unparseDebug (other)));
3472 llbug (message ("sRef_mergeState: invalid other sRef: %q",
3473 sRef_unparseDebug (res)));
3479 sRef_mergeOptState (sRef res, sRef other, clause cl, fileloc loc)
3481 if (sRef_isValid (res) && sRef_isValid (other))
3483 sRef_mergeStateAux (res, other, cl, TRUE, loc, TRUE);
3487 if (sRef_isInvalid (res))
3489 llbug (message ("sRef_mergeOptState: invalid res sRef: %q",
3490 sRef_unparseDebug (other)));
3494 llbug (message ("sRef_mergeOptState: invalid other sRef: %q",
3495 sRef_unparseDebug (res)));
3501 sRef_mergeStateAux (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other,
3502 clause cl, bool opt, fileloc loc,
3506 llassertfatal (sRef_isValid (res));
3507 llassertfatal (sRef_isValid (other));
3509 res->modified = res->modified || other->modified;
3511 if (res->kind == other->kind
3512 || (other->kind == SK_UNKNOWN || res->kind == SK_UNKNOWN))
3514 sstate odef = other->defstate;
3515 sstate rdef = res->defstate;
3516 nstate onull = other->nullstate;
3519 ** yucky stuff to handle
3524 if (other->defstate == SS_DEAD
3525 && ((sRef_isOnly (res) && sRef_definitelyNull (res))
3526 || (res->defstate == SS_UNDEFINED
3527 || res->defstate == SS_UNUSEABLE)))
3529 if (res->defstate == SS_UNDEFINED
3530 || res->defstate == SS_UNUSEABLE)
3532 res->defstate = SS_UNUSEABLE;
3536 res->defstate = SS_DEAD;
3539 res->definfo = alinfo_update (res->definfo, other->definfo);
3540 sRef_clearDerived (other);
3541 sRef_clearDerived (res);
3543 else if (res->defstate == SS_DEAD
3544 && ((sRef_isOnly (other) && sRef_definitelyNull (other))
3545 || (other->defstate == SS_UNDEFINED
3546 || other->defstate == SS_UNUSEABLE)))
3548 if (other->defstate == SS_UNDEFINED
3549 || other->defstate == SS_UNUSEABLE)
3551 res->defstate = SS_UNUSEABLE;
3555 res->defstate = SS_DEAD;
3558 sRef_clearDerived (other);
3559 sRef_clearDerived (res);
3561 else if (res->defstate == SS_DEFINED
3562 && (other->defstate == SS_ALLOCATED
3563 && sRef_definitelyNull (other)))
3565 other->defstate = SS_DEFINED; /* definitely null! */
3567 else if (other->defstate == SS_DEFINED
3568 && (res->defstate == SS_ALLOCATED && sRef_definitelyNull (res)))
3570 res->defstate = SS_DEFINED;
3571 res->definfo = alinfo_update (res->definfo, other->definfo);
3578 if (res->defstate == SS_DEAD && other->defstate == SS_DEAD)
3580 sRef_clearDerived (other);
3581 sRef_clearDerived (res);
3585 ** only & dead isn't really an only!
3588 if (alkind_isOnly (other->aliaskind) && other->defstate == SS_DEAD)
3590 other->aliaskind = AK_UNKNOWN;
3593 if (alkind_isOnly (res->aliaskind) && res->defstate == SS_DEAD)
3595 res->aliaskind = AK_UNKNOWN;
3599 ** Dead and dependent -> dead
3602 if (alkind_isDependent (other->aliaskind) && res->defstate == SS_DEAD)
3604 other->aliaskind = AK_UNKNOWN;
3605 other->defstate = SS_DEAD;
3606 sRef_clearDerived (res);
3607 sRef_clearDerived (other);
3610 if (alkind_isDependent (res->aliaskind) && other->defstate == SS_DEAD)
3612 res->aliaskind = AK_UNKNOWN;
3613 res->defstate = SS_DEAD;
3614 sRef_clearDerived (res);
3615 sRef_clearDerived (other);
3619 ** must do alias combine first, since it depends on
3620 ** original values of state and null.
3623 sRef_combineAliasKinds (res, other, cl, loc);
3624 sRef_combineDefState (res, other);
3625 sRef_combineNullState (res, other);
3627 if (rdef == SS_ALLOCATED || rdef == SS_SPECIAL)
3629 if (odef == SS_DEFINED)
3631 if (onull == NS_DEFNULL || onull == NS_CONSTNULL)
3633 res->deriv = sRefSet_copy (res->deriv, other->deriv);
3638 else if (odef == SS_ALLOCATED
3639 || odef == SS_SPECIAL)
3644 if (ctype_isUnion (ctype_realType (sRef_getType (res))))
3646 res->deriv = sRef_mergeUnionDerivs (res->deriv,
3652 res->deriv = sRef_mergeDerivs (res->deriv, other->deriv,
3661 res->deriv = sRef_mergeDerivs (res->deriv, other->deriv,
3671 if (rdef == SS_PDEFINED
3672 || (rdef == SS_DEFINED && odef == SS_PDEFINED))
3676 res->deriv = sRef_mergePdefinedDerivs (res->deriv, other->deriv,
3682 if ((rdef == SS_DEFINED || rdef == SS_UNKNOWN)
3683 && res->defstate == SS_ALLOCATED)
3685 res->deriv = sRefSet_copy (res->deriv, other->deriv);
3691 res->deriv = sRef_mergeDerivs (res->deriv, other->deriv,
3699 sRef_combineExKinds (res, other);
3703 if (res->kind == SK_ARRAYFETCH && other->kind == SK_PTR)
3705 sRef nother = sRef_buildArrayFetchKnown (sRef_getBase (other), 0);
3707 sRef_copyState (nother, other);
3708 sRef_mergeStateAux (res, nother, cl, opt, loc, doDerivs);
3710 else if (res->kind == SK_PTR && other->kind == SK_ARRAYFETCH)
3712 sRef nother = sRef_buildPointer (sRef_getBase (other));
3714 if (sRef_isValid (nother))
3716 sRef_copyState (nother, other);
3717 sRef_mergeStateAux (res, nother, cl, opt, loc, doDerivs);
3722 llcontbug (message ("merge conj: %q / %q", sRef_unparseFull (res),
3723 sRef_unparseFull (other)));
3731 sRef_mergeUnionDerivs (/*@only@*/ sRefSet res,
3732 /*@exposed@*/ sRefSet other, bool opt,
3733 clause cl, fileloc loc)
3735 if (sRefSet_isEmpty (res))
3737 return sRefSet_copy (res, other);
3741 sRefSet_allElements (other, el)
3743 if (sRef_isValid (el))
3745 sRef e2 = sRefSet_lookupMember (other, el);
3747 if (sRef_isValid (e2))
3749 sRef_mergeStateAux (el, e2, cl, opt, loc, FALSE);
3753 res = sRefSet_insert (res, el);
3756 } end_sRefSet_allElements ;
3762 static /*@only@*/ sRefSet
3763 sRef_mergeDerivs (/*@only@*/ sRefSet res, sRefSet other,
3764 bool opt, clause cl, fileloc loc)
3766 sRefSet ret = sRefSet_new ();
3769 sRefSet_allElements (res, el)
3771 if (sRef_isValid (el))
3773 sRef e2 = sRefSet_lookupMember (other, el);
3775 if (sRef_isValid (e2))
3777 if (el->defstate == SS_ALLOCATED &&
3778 e2->defstate == SS_PDEFINED)
3780 e2->defstate = SS_ALLOCATED;
3782 else if (e2->defstate == SS_ALLOCATED &&
3783 el->defstate == SS_PDEFINED)
3785 el->defstate = SS_ALLOCATED;
3786 sRef_clearDerived (el);
3788 else if ((el->defstate == SS_DEAD || sRef_isKept (el)) &&
3789 (e2->defstate == SS_DEFINED && !sRef_isKept (e2)))
3792 if (checkDeadState (el, TRUE, loc))
3794 if (sRef_isThroughArrayFetch (el))
3796 sRef_maybeKill (el, loc);
3797 sRef_maybeKill (e2, loc);
3801 else if ((e2->defstate == SS_DEAD || sRef_isKept (e2)) &&
3802 (el->defstate == SS_DEFINED && !sRef_isKept (el)))
3805 if (checkDeadState (e2, FALSE, loc))
3807 if (sRef_isThroughArrayFetch (el))
3809 sRef_maybeKill (el, loc);
3810 sRef_maybeKill (e2, loc);
3814 else if (el->defstate == SS_DEFINED &&
3815 e2->defstate == SS_PDEFINED)
3817 el->defstate = SS_PDEFINED;
3819 else if (e2->defstate == SS_DEFINED &&
3820 el->defstate == SS_PDEFINED)
3822 e2->defstate = SS_PDEFINED;
3829 if (ctype_isUnion (ctype_realType (sRef_getType (el))))
3831 el->deriv = sRef_mergeUnionDerivs (el->deriv, e2->deriv,
3836 el->deriv = sRef_mergeDerivs (el->deriv, e2->deriv, opt, cl, loc);
3839 if (sRef_equivalent (el, e2))
3841 ret = sRefSet_insert (ret, el);
3845 sRef sr = sRef_leastCommon (el, e2);
3847 if (sRef_isValid (sr))
3849 ret = sRefSet_insert (ret, sr);
3857 (void) sRefSet_delete (other, e2);
3859 else /* not defined */
3861 (void) checkDeadState (el, TRUE, loc);
3864 } end_sRefSet_allElements;
3866 sRefSet_allElements (other, el)
3868 if (sRef_isValid (el))
3870 (void) checkDeadState (el, FALSE, loc);
3872 } end_sRefSet_allElements;
3879 ** Returns TRUE is there is an error.
3882 static bool checkDeadState (/*@notnull@*/ sRef el, bool tbranch, fileloc loc)
3885 ** usymtab_isGuarded --- the utab should still be in the
3886 ** state of the alternate branch.
3888 ** tbranch TRUE means el is released in the last branch, e.g.
3889 ** if (x != NULL) { ; } else { sfree (x); }
3890 ** so, if x is null in the other branch no error is reported.
3892 ** tbranch FALSE means this is the other branch:
3893 ** if (x != NULL) { sfree (x); } else { ; }
3894 ** so, if x is null in this branch there is no error.
3898 if ((sRef_isDead (el) || sRef_isKept (el))
3899 && !sRef_isDeepUnionField (el) && !sRef_isThroughArrayFetch (el))
3904 if (usymtab_isProbableDeepNull (el))
3911 if (usymtab_isAltProbablyDeepNull (el))
3919 message ("Storage %q is %q in one path, but live in another.",
3921 cstring_makeLiteral (sRef_isKept (el)
3922 ? "kept" : "released")),
3926 if (sRef_isKept (el))
3928 sRef_showAliasInfo (el);
3932 sRef_showStateInfo (el);
3935 /* prevent further errors */
3936 el->defstate = SS_UNKNOWN;
3937 sRef_setAliasKind (el, AK_ERROR, fileloc_undefined);
3947 checkDerivDeadState (/*@notnull@*/ sRef el, bool tbranch, fileloc loc)
3950 if (checkDeadState (el, tbranch, loc))
3952 sRefSet_allElements (el->deriv, t)
3954 if (sRef_isValid (t))
3956 checkDerivDeadState (t, tbranch, loc);
3958 } end_sRefSet_allElements;
3963 sRef_mergePdefinedDerivs (sRefSet res, sRefSet other, bool opt,
3964 clause cl, fileloc loc)
3966 sRefSet ret = sRefSet_new ();
3968 sRefSet_allElements (res, el)
3970 if (sRef_isValid (el))
3972 sRef e2 = sRefSet_lookupMember (other, el);
3974 if (sRef_isValid (e2))
3976 if (sRef_isAllocated (el) && !sRef_isAllocated (e2))
3980 else if (sRef_isAllocated (e2) && !sRef_isAllocated (el))
3982 el->deriv = sRefSet_copy (el->deriv, e2->deriv);
3986 el->deriv = sRef_mergePdefinedDerivs (el->deriv, e2->deriv,
3990 sRef_mergeStateAux (el, e2, cl, opt, loc, FALSE);
3992 ret = sRefSet_insert (ret, el);
3993 (void) sRefSet_delete (other, e2);
3999 checkDerivDeadState (el, (cl == FALSECLAUSE), loc);
4002 ret = sRefSet_insert (ret, el);
4005 } end_sRefSet_allElements;
4007 sRefSet_allElements (other, el)
4009 if (sRef_isValid (el))
4011 if (!sRefSet_member (ret, el))
4013 /* was cl == FALSECLAUSE */
4014 checkDerivDeadState (el, FALSE, loc);
4015 ret = sRefSet_insert (ret, el);
4020 ** it's okay --- member is a different equality test
4024 } end_sRefSet_allElements;
4030 sRef sRef_makeConj (/*@exposed@*/ /*@returned@*/ sRef a, /*@exposed@*/ sRef b)
4032 llassert (sRef_isValid (a));
4033 llassert (sRef_isValid (b));
4035 if (!sRef_equivalent (a, b))
4037 sRef s = sRef_new ();
4040 s->info = (sinfo) dmalloc (sizeof (*s->info));
4041 s->info->conj = (cjinfo) dmalloc (sizeof (*s->info->conj));
4042 s->info->conj->a = a;
4043 s->info->conj->b = b;
4045 if (ctype_equal (a->type, b->type)) s->type = a->type;
4046 else s->type = ctype_makeConj (a->type, b->type);
4048 if (a->defstate == b->defstate)
4050 s->defstate = a->defstate;
4054 s->defstate = SS_UNKNOWN;
4057 s->nullstate = NS_UNKNOWN;
4059 s->safe = a->safe && b->safe;
4060 s->aliaskind = alkind_resolve (a->aliaskind, b->aliaskind);
4066 /*@-exposetrans@*/ return a; /*@=exposetrans@*/
4073 sRef s = sRef_new ();
4075 s->kind = SK_UNKNOWN;
4080 sRef_makeSpecial (speckind sk) /*@*/
4082 sRef s = sRef_new ();
4084 s->kind = SK_SPECIAL;
4085 s->info = (sinfo) dmalloc (sizeof (*s->info));
4090 static sRef srnothing = sRef_undefined;
4091 static sRef srinternal = sRef_undefined;
4092 static sRef srsystem = sRef_undefined;
4093 static sRef srspec = sRef_undefined;
4096 sRef_makeNothing (void)
4098 if (sRef_isInvalid (srnothing))
4100 srnothing = sRef_makeSpecial (SR_NOTHING);
4109 sRef_makeInternalState (void)
4111 if (sRef_isInvalid (srinternal))
4113 srinternal = sRef_makeSpecial (SR_INTERNAL);
4122 sRef_makeSpecState (void)
4124 if (sRef_isInvalid (srspec))
4126 srspec = sRef_makeSpecial (SR_SPECSTATE);
4135 sRef_makeSystemState (void)
4137 if (sRef_isInvalid (srsystem))
4139 srsystem = sRef_makeSpecial (SR_SYSTEM);
4148 sRef_makeResultType (ctype ct)
4150 sRef res = sRef_makeResult ();
4159 sRef s = sRef_new ();
4161 s->kind = SK_RESULT;
4162 s->type = ctype_unknown;
4163 s->defstate = SS_UNKNOWN;
4164 s->aliaskind = AK_UNKNOWN;
4165 s->nullstate = NS_UNKNOWN;
4172 sRef_isNothing (sRef s)
4174 return (sRef_isKindSpecial (s) && s->info->spec == SR_NOTHING);
4178 sRef_isInternalState (sRef s)
4180 return (sRef_isKindSpecial (s) && s->info->spec == SR_INTERNAL);
4184 sRef_isSpecInternalState (sRef s)
4186 return (sRef_isKindSpecial (s)
4187 && (s->info->spec == SR_INTERNAL || s->info->spec == SR_SPECSTATE));
4191 sRef_isSpecState (sRef s)
4193 return (sRef_isKindSpecial (s) && s->info->spec == SR_SPECSTATE);
4197 sRef_isResult (sRef s)
4199 return (sRef_isValid (s) && s->kind == SK_RESULT);
4203 sRef_isSystemState (sRef s)
4205 return (sRef_isKindSpecial (s) && s->info->spec == SR_SYSTEM);
4209 sRef_getScopeIndex (sRef s)
4211 llassert (sRef_isValid (s));
4212 llassert (sRef_isCvar (s));
4214 return (s->info->cvar->index);
4218 sRef_makeSafe (sRef s)
4220 if (sRef_isValid (s))
4227 sRef_makeUnsafe (sRef s)
4229 if (sRef_isValid (s))
4236 ** memory state operations
4239 /*@only@*/ cstring sRef_unparseFull (sRef s)
4241 if (sRef_isInvalid (s)) return (cstring_undefined);
4243 return (message ("[%d] %q - %q [%s] { %q }",
4245 sRef_unparseDebug (s),
4246 sRef_unparseState (s),
4247 exkind_unparse (s->oexpkind),
4248 sRefSet_unparseDebug (s->deriv)));
4251 /*@unused@*/ cstring sRef_unparseDeep (sRef s)
4253 cstring st = cstring_undefined;
4255 st = message ("%q:", sRef_unparseFull (s));
4257 if (sRef_isValid (s))
4259 sRefSet_allElements (s->deriv, el)
4261 st = message("%q\n%q", st, sRef_unparseDeep (el));
4262 } end_sRefSet_allElements ;
4268 /*@only@*/ cstring sRef_unparseState (sRef s)
4270 if (sRef_isConj (s))
4272 return (message ("%q | %q",
4273 sRef_unparseState (s->info->conj->a),
4274 sRef_unparseState (s->info->conj->b)));
4277 if (sRef_isInvalid (s))
4279 return (cstring_makeLiteral ("<invalid>"));
4282 return (message ("%s.%s.%s.%s",
4283 alkind_unparse (s->aliaskind),
4284 nstate_unparse (s->nullstate),
4285 exkind_unparse (s->expkind),
4286 sstate_unparse (s->defstate)));
4289 bool sRef_isNotUndefined (sRef s)
4291 return (sRef_isInvalid (s)
4292 || (s->defstate != SS_UNDEFINED
4293 && s->defstate != SS_UNUSEABLE
4294 && s->defstate != SS_DEAD));
4297 ynm sRef_isWriteable (sRef s)
4299 if (sRef_isInvalid (s)) return MAYBE;
4301 if (sRef_isConj (s) && s->defstate == SS_UNKNOWN)
4303 if (ynm_toBoolStrict (sRef_isWriteable (sRef_getConjA (s))))
4305 if (ynm_toBoolStrict (sRef_isWriteable (sRef_getConjB (s))))
4313 if (ynm_toBoolStrict (sRef_isWriteable (sRef_getConjB (s))))
4321 return (ynm_fromBool (s->defstate != SS_UNUSEABLE));
4324 bool sRef_hasNoStorage (sRef s)
4326 return (!sRef_isAllocatedStorage (s) || sRef_isDefinitelyNull (s));
4329 bool sRef_isStrictReadable (sRef s)
4331 return (ynm_toBoolStrict (sRef_isReadable (s)));
4334 ynm sRef_isReadable (sRef s)
4338 if (sRef_isInvalid (s)) return YES;
4342 if (sRef_isConj (s) && s->defstate == SS_UNKNOWN)
4344 if (ynm_toBoolStrict (sRef_isReadable (sRef_getConjA (s))))
4346 if (ynm_toBoolStrict (sRef_isReadable (sRef_getConjB (s))))
4354 if (ynm_toBoolStrict (sRef_isReadable (sRef_getConjB (s))))
4361 else if (ss == SS_HOFFA)
4363 if (context_getFlag (FLG_STRICTUSERELEASED))
4374 return (ynm_fromBool (ss == SS_DEFINED
4377 || ss == SS_PDEFINED
4380 || ss == SS_ALLOCATED
4381 || ss == SS_UNKNOWN));
4385 static /*@exposed@*/ sRef whatUndefined (/*@exposed@*/ sRef fref, int depth)
4390 if (depth > MAXDEPTH)
4393 ("Warning: check definition limit exceeded, checking %q. "
4394 "This either means there is a variable with at least "
4395 "%d indirections apparent in the program text, or "
4396 "there is a bug in LCLint.",
4397 sRef_unparse (fref),
4401 return sRef_undefined;
4404 if (!sRef_isKnown (fref) || sRef_isAnyDefined (fref))
4406 return sRef_undefined;
4409 if (sRef_isUnuseable (fref) || sRef_isStateUndefined (fref))
4414 ct = ctype_realType (sRef_getType (fref));
4416 if (ctype_isUnknown (ct))
4418 return sRef_undefined;
4420 else if (ctype_isPointer (ct) || ctype_isArray (ct))
4422 if (sRef_isStateUnknown (fref))
4424 return sRef_undefined;
4428 sRef fptr = sRef_constructDeref (fref);
4430 return (whatUndefined (fptr, depth + 1));
4433 else if (ctype_isStruct (ct))
4435 bool hasOneDefined = FALSE;
4437 if (sRef_isStateUnknown (fref))
4442 if (sRef_isPdefined (fref) || sRef_isAnyDefined (fref))
4444 sRefSet_realElements (sRef_derivedFields (fref), sr)
4446 hasOneDefined = TRUE;
4448 if (sRef_isField (sr))
4450 cstring fieldname = sRef_getField (sr);
4451 sRef fldref = sRef_makeField (fref, fieldname);
4452 bool shouldCheck = !sRef_isRecursiveField (fldref);
4456 sRef wdef = whatUndefined (fldref, depth + 1);
4458 if (sRef_isValid (wdef))
4464 } end_sRefSet_realElements;
4466 else if (sRef_isAllocated (fref))
4469 ** for structures, each field must be completely defined
4472 uentryList fields = ctype_getFields (ct);
4474 uentryList_elements (fields, ue)
4476 cstring name = uentry_getRealName (ue);
4477 sRef ffield = sRef_makeField (fref, name);
4478 bool shouldCheck = !sRef_isRecursiveField (ffield);
4480 if (sRef_isRelDef (uentry_getSref (ue)))
4488 sRef wdef = whatUndefined (ffield, depth + 1);
4490 if (sRef_isInvalid (wdef))
4496 } end_uentryList_elements;
4503 else if (ctype_isUnion (ct))
4512 return sRef_undefined;
4515 static bool checkDefined (sRef sr)
4517 return (sRef_isInvalid (whatUndefined (sr, 0)));
4520 bool sRef_isReallyDefined (sRef s)
4522 if (sRef_isValid (s))
4524 if (sRef_isAnyDefined (s))
4530 if (sRef_isAllocated (s) || sRef_isPdefined (s))
4532 return checkDefined (s);
4546 void sRef_showNotReallyDefined (sRef s)
4548 if (sRef_isValid (s))
4550 if (sRef_isAnyDefined (s))
4556 if (sRef_isAllocated (s) || sRef_isPdefined (s))
4558 sRef ref = whatUndefined (s, 0);
4560 llassert (sRef_isValid (ref));
4565 (message ("This sub-reference is %s: %q",
4566 sstate_unparse (sRef_getDefState (ref)),
4567 sRef_unparse (ref)));
4582 sstate sRef_getDefState (sRef s)
4584 if (sRef_isInvalid (s)) return (SS_UNKNOWN);
4585 return (s->defstate);
4588 void sRef_setDefState (sRef s, sstate defstate, fileloc loc)
4590 sRef_setStateAux (s, defstate, loc);
4593 static void sRef_clearAliasStateAux (sRef s, fileloc loc)
4595 sRef_setAliasKind (s, AK_ERROR, loc);
4598 void sRef_clearAliasState (sRef s, fileloc loc)
4600 sRef_aliasSetComplete (sRef_clearAliasStateAux, s, loc);
4603 void sRef_setAliasKindComplete (sRef s, alkind kind, fileloc loc)
4605 sRef_aliasSetCompleteParam (sRef_setAliasKind, s, kind, loc);
4608 void sRef_setAliasKind (sRef s, alkind kind, fileloc loc)
4610 if (sRef_isValid (s))
4612 sRef_clearDerived (s);
4614 if ((kind != s->aliaskind && kind != s->oaliaskind)
4615 && fileloc_isDefined (loc))
4617 s->aliasinfo = alinfo_updateLoc (s->aliasinfo, loc);
4620 s->aliaskind = kind;
4624 void sRef_setOrigAliasKind (sRef s, alkind kind)
4626 if (sRef_isValid (s))
4628 s->oaliaskind = kind;
4632 exkind sRef_getExKind (sRef s)
4634 if (sRef_isValid (s))
4636 return (s->expkind);
4644 exkind sRef_getOrigExKind (sRef s)
4646 if (sRef_isValid (s))
4648 return (s->oexpkind);
4656 static void sRef_clearExKindAux (sRef s, fileloc loc)
4658 sRef_setExKind (s, XO_UNKNOWN, loc);
4661 void sRef_setObserver (sRef s, fileloc loc)
4663 sRef_setExKind (s, XO_OBSERVER, loc);
4666 void sRef_setExposed (sRef s, fileloc loc)
4668 sRef_setExKind (s, XO_EXPOSED, loc);
4671 void sRef_clearExKindComplete (sRef s, fileloc loc)
4673 (void) sRef_aliasSetComplete (sRef_clearExKindAux, s, loc);
4676 void sRef_setExKind (sRef s, exkind exp, fileloc loc)
4678 if (sRef_isValid (s))
4680 if (s->expkind != exp)
4682 s->expinfo = alinfo_updateLoc (s->expinfo, loc);
4690 ** s1->derived = s2->derived
4693 static void sRef_copyRealDerived (sRef s1, sRef s2)
4695 if (sRef_isValid (s1) && sRef_isValid (s2))
4697 sRef sb = sRef_getRootBase (s1);
4699 sRefSet_clear (s1->deriv);
4701 sRefSet_allElements (s2->deriv, el)
4703 if (sRef_isValid (el))
4705 sRef rb = sRef_getRootBase (el);
4707 if (!sRef_same (rb, sb))
4709 sRef fb = sRef_fixDirectBase (el, s1);
4711 if (sRef_isValid (fb))
4713 sRef_copyRealDerived (fb, el);
4714 sRef_addDeriv (s1, fb);
4719 sRef_addDeriv (s1, el);
4722 } end_sRefSet_allElements ;
4727 void sRef_copyRealDerivedComplete (sRef s1, sRef s2)
4729 sRef_innerAliasSetCompleteParam (sRef_copyRealDerived, s1, s2);
4732 void sRef_setUndefined (sRef s, fileloc loc)
4734 if (sRef_isValid (s))
4736 s->defstate = SS_UNDEFINED;
4738 if (fileloc_isDefined (loc))
4740 s->definfo = alinfo_updateLoc (s->definfo, loc);
4743 sRef_clearDerived (s);
4747 static void sRef_setDefinedAux (sRef s, fileloc loc, bool clear)
4749 if (sRef_isInvalid (s)) return;
4751 if (s->defstate != SS_DEFINED && fileloc_isDefined (loc))
4753 s->definfo = alinfo_updateLoc (s->definfo, loc);
4756 s->defstate = SS_DEFINED;
4758 /* e.g., if x is allocated, *x = 3 defines x */
4760 if (s->kind == SK_PTR)
4762 sRef p = s->info->ref;
4764 if (p->defstate == SS_ALLOCATED)
4766 sRef_setDefinedAux (p, loc, clear);
4769 else if (s->kind == SK_ARRAYFETCH)
4771 if (!s->info->arrayfetch->indknown
4772 || (s->info->arrayfetch->ind == 0))
4774 sRef p = s->info->arrayfetch->arr;
4775 sRef ptr = sRef_constructPointer (p);
4777 if (sRef_isValid (ptr))
4779 if (ptr->defstate == SS_ALLOCATED
4780 || ptr->defstate == SS_UNDEFINED)
4782 sRef_setDefinedAux (ptr, loc, clear);
4786 if (p->defstate == SS_RELDEF)
4790 else if (p->defstate == SS_ALLOCATED || p->defstate == SS_PDEFINED)
4792 p->defstate = SS_DEFINED;
4799 else if (s->kind == SK_FIELD)
4801 sRef parent = s->info->field->rec;
4803 if (sRef_isValid (parent))
4805 if (ctype_isUnion (ctype_realType (parent->type)))
4808 ** Should not clear derived from here.
4811 sRef_setDefinedNoClear (parent, loc);
4815 ; /* Nothing to do for structures. */
4827 sRef_clearDerived (s);
4831 static void sRef_setPartialDefined (sRef s, fileloc loc)
4833 if (!sRef_isPartial (s))
4835 sRef_setDefined (s, loc);
4839 void sRef_setPartialDefinedComplete (sRef s, fileloc loc)
4841 sRef_innerAliasSetComplete (sRef_setPartialDefined, s, loc);
4844 void sRef_setDefinedComplete (sRef s, fileloc loc)
4846 sRef_innerAliasSetComplete (sRef_setDefined, s, loc);
4849 void sRef_setDefined (sRef s, fileloc loc)
4851 sRef_setDefinedAux (s, loc, TRUE);
4854 static void sRef_setDefinedNoClear (sRef s, fileloc loc)
4856 DPRINTF (("Defining: %s", sRef_unparseFull (s)));
4857 sRef_setDefinedAux (s, loc, FALSE);
4858 DPRINTF (("==> %s", sRef_unparseFull (s)));
4861 void sRef_setDefinedNCComplete (sRef s, fileloc loc)
4863 DPRINTF (("Set Defined Complete: %s", sRef_unparseFull (s)));
4864 sRef_innerAliasSetComplete (sRef_setDefinedNoClear, s, loc);
4865 DPRINTF (("==> %s", sRef_unparseFull (s)));
4868 static bool sRef_isDeepUnionField (sRef s)
4870 return (sRef_deepPred (sRef_isUnionField, s));
4873 bool sRef_isUnionField (sRef s)
4875 if (sRef_isValid (s) && s->kind == SK_FIELD)
4878 ** defining one field of a union defines the union
4881 sRef base = s->info->field->rec;
4883 if (sRef_isValid (base))
4885 return (ctype_isUnion (ctype_realType (base->type)));
4892 void sRef_setPdefined (sRef s, fileloc loc)
4894 if (sRef_isValid (s) && !sRef_isPartial (s))
4896 sRef base = sRef_getBaseSafe (s);
4898 if (s->defstate == SS_ALLOCATED)
4903 if (s->defstate != SS_PDEFINED && fileloc_isDefined (loc))
4905 s->definfo = alinfo_updateLoc (s->definfo, loc);
4908 s->defstate = SS_PDEFINED;
4910 /* e.g., if x is allocated, *x = 3 defines x */
4912 while (sRef_isValid (base) && sRef_isKnown (base))
4914 if (base->defstate == SS_DEFINED)
4918 base->defstate = SS_PDEFINED;
4919 nb = sRef_getBaseSafe (base);
4930 static void sRef_setStateAux (sRef s, sstate ss, fileloc loc)
4932 if (sRef_isValid (s))
4934 /* if (s->defstate == SS_RELDEF) return; */
4936 if (s->defstate != ss && fileloc_isDefined (loc))
4938 s->definfo = alinfo_updateLoc (s->definfo, loc);
4942 sRef_clearDerived (s);
4944 if (ss == SS_ALLOCATED)
4946 sRef base = sRef_getBaseSafe (s);
4948 while (sRef_isValid (base) && sRef_isKnown (base))
4950 if (base->defstate == SS_DEFINED)
4954 base->defstate = SS_PDEFINED;
4956 nb = sRef_getBaseSafe (base);
4969 void sRef_setAllocatedComplete (sRef s, fileloc loc)
4971 sRef_innerAliasSetComplete (sRef_setAllocated, s, loc);
4974 static void sRef_setAllocatedShallow (sRef s, fileloc loc)
4976 if (sRef_isValid (s))
4978 if (s->defstate == SS_DEAD || s->defstate == SS_UNDEFINED)
4980 s->defstate = SS_ALLOCATED;
4982 if (fileloc_isDefined (loc))
4984 s->definfo = alinfo_updateLoc (s->definfo, loc);
4990 void sRef_setAllocatedShallowComplete (sRef s, fileloc loc)
4992 sRef_innerAliasSetComplete (sRef_setAllocatedShallow, s, loc);
4995 void sRef_setAllocated (sRef s, fileloc loc)
4997 sRef_setStateAux (s, SS_ALLOCATED, loc);
5000 void sRef_setPartial (sRef s, fileloc loc)
5002 sRef_setStateAux (s, SS_PARTIAL, loc);
5005 void sRef_setShared (sRef s, fileloc loc)
5007 if (sRef_isValid (s))
5009 if (s->aliaskind != AK_SHARED && fileloc_isDefined (loc))
5011 s->aliasinfo = alinfo_updateLoc (s->aliasinfo, loc);
5014 s->aliaskind = AK_SHARED;
5015 /* don't! sRef_clearDerived (s); */
5019 void sRef_setLastReference (sRef s, sRef ref, fileloc loc)
5021 if (sRef_isValid (s))
5023 s->aliaskind = sRef_getAliasKind (ref);
5024 s->aliasinfo = alinfo_updateRefLoc (s->aliasinfo, ref, loc);
5029 void sRef_setNullStateAux (/*@notnull@*/ sRef s, nstate ns, fileloc loc)
5033 if (fileloc_isDefined (loc))
5035 s->nullinfo = alinfo_updateLoc (s->nullinfo, loc);
5039 void sRef_setNotNull (sRef s, fileloc loc)
5041 if (sRef_isValid (s))
5043 sRef_setNullStateAux (s, NS_NOTNULL, loc);
5047 void sRef_setNullState (sRef s, nstate n, fileloc loc)
5049 if (sRef_isValid (s))
5051 sRef_setNullStateAux (s, n, loc);
5055 void sRef_setNullTerminatedStateInnerComplete (sRef s, struct _bbufinfo b, /*@unused@*/ fileloc loc) {
5057 switch (b.bufstate) {
5058 case BB_NULLTERMINATED:
5059 sRef_setNullTerminatedState (s);
5060 sRef_setLen (s, b.len);
5062 case BB_POSSIBLYNULLTERMINATED:
5063 sRef_setPossiblyNullTerminatedState(s);
5065 case BB_NOTNULLTERMINATED:
5066 sRef_setNotNullTerminatedState (s);
5069 sRef_setSize (s, b.size);
5071 /* PL: TO BE DONE : Aliases are not modified right now, have to be similar to
5072 * setNullStateInnerComplete.
5076 void sRef_setNullStateInnerComplete (sRef s, nstate n, fileloc loc)
5078 sRef_setNullState (s, n, loc);
5083 sRef_innerAliasSetComplete (sRef_setPosNull, s, loc);
5086 sRef_innerAliasSetComplete (sRef_setDefNull, s, loc);
5089 sRef_innerAliasSetComplete (sRef_setNullUnknown, s, loc);
5092 sRef_innerAliasSetComplete (sRef_setNotNull, s, loc);
5095 sRef_innerAliasSetComplete (sRef_setNotNull, s, loc);
5098 sRef_innerAliasSetComplete (sRef_setNullUnknown, s, loc);
5101 sRef_innerAliasSetComplete (sRef_setDefNull, s, loc);
5104 sRef_innerAliasSetComplete (sRef_setNullUnknown, s, loc);
5107 sRef_innerAliasSetComplete (sRef_setNullErrorLoc, s, loc);
5112 void sRef_setPosNull (sRef s, fileloc loc)
5114 if (sRef_isValid (s))
5116 sRef_setNullStateAux (s, NS_POSNULL, loc);
5120 void sRef_setDefNull (sRef s, fileloc loc)
5122 if (sRef_isValid (s))
5124 sRef_setNullStateAux (s, NS_DEFNULL, loc);
5128 void sRef_setNullUnknown (sRef s, fileloc loc)
5130 if (sRef_isValid (s))
5132 sRef_setNullStateAux (s, NS_UNKNOWN, loc);
5136 void sRef_setNullError (sRef s)
5138 if (sRef_isValid (s))
5140 sRef_setNullStateAux (s, NS_UNKNOWN, fileloc_undefined);
5144 void sRef_setNullErrorLoc (sRef s, /*@unused@*/ fileloc loc)
5146 sRef_setNullError (s);
5149 void sRef_setOnly (sRef s, fileloc loc)
5151 if (sRef_isValid (s) && s->aliaskind != AK_ONLY)
5153 s->aliaskind = AK_ONLY;
5154 s->aliasinfo = alinfo_updateLoc (s->aliasinfo, loc);
5158 void sRef_setDependent (sRef s, fileloc loc)
5160 if (sRef_isValid (s) && !sRef_isConst (s) && (s->aliaskind != AK_DEPENDENT))
5162 s->aliaskind = AK_DEPENDENT;
5163 s->aliasinfo = alinfo_updateLoc (s->aliasinfo, loc);
5167 void sRef_setOwned (sRef s, fileloc loc)
5169 if (sRef_isValid (s) && !sRef_isConst (s) && (s->aliaskind != AK_OWNED))
5171 s->aliaskind = AK_OWNED;
5172 s->aliasinfo = alinfo_updateLoc (s->aliasinfo, loc);
5176 void sRef_setKept (sRef s, fileloc loc)
5178 if (sRef_isValid (s) && !sRef_isConst (s) && (s->aliaskind != AK_KEPT))
5180 sRef base = sRef_getBaseSafe (s);
5182 while (sRef_isValid (base) && sRef_isKnown (base))
5184 if (base->defstate == SS_DEFINED)
5186 base->defstate = SS_PDEFINED;
5187 base = sRef_getBaseSafe (base);
5196 s->aliaskind = AK_KEPT;
5197 s->aliasinfo = alinfo_updateLoc (s->aliasinfo, loc);
5201 static void sRef_setKeptAux (sRef s, fileloc loc)
5203 if (!sRef_isShared (s))
5205 sRef_setKept (s, loc);
5209 static void sRef_setDependentAux (sRef s, fileloc loc)
5211 if (!sRef_isShared (s))
5213 sRef_setDependent (s, loc);
5217 void sRef_setKeptComplete (sRef s, fileloc loc)
5219 sRef_aliasSetComplete (sRef_setKeptAux, s, loc);
5222 void sRef_setDependentComplete (sRef s, fileloc loc)
5224 sRef_aliasSetComplete (sRef_setDependentAux, s, loc);
5227 void sRef_setFresh (sRef s, fileloc loc)
5229 if (sRef_isValid (s))
5231 s->aliaskind = AK_FRESH;
5232 s->aliasinfo = alinfo_updateLoc (s->aliasinfo, loc);
5236 void sRef_kill (sRef s, fileloc loc)
5238 DPRINTF (("Kill: %s", sRef_unparseFull (s)));
5240 if (sRef_isValid (s) && !sRef_isShared (s) && !sRef_isConst (s))
5242 sRef base = sRef_getBaseSafe (s);
5244 while (sRef_isValid (base) && sRef_isKnown (base))
5246 if (base->defstate == SS_DEFINED)
5248 base->defstate = SS_PDEFINED;
5249 base = sRef_getBaseSafe (base);
5258 s->aliaskind = s->oaliaskind;
5259 s->defstate = SS_DEAD;
5260 s->definfo = alinfo_updateLoc (s->definfo, loc);
5262 sRef_clearDerived (s);
5266 void sRef_maybeKill (sRef s, fileloc loc)
5269 if (sRef_isValid (s))
5271 sRef base = sRef_getBaseSafe (s);
5274 while (sRef_isValid (base) && sRef_isKnown (base))
5276 if (base->defstate == SS_DEFINED || base->defstate == SS_RELDEF)
5278 base->defstate = SS_PDEFINED;
5279 base = sRef_getBaseSafe (base);
5288 s->aliaskind = s->oaliaskind;
5289 s->defstate = SS_HOFFA;
5290 s->definfo = alinfo_updateLoc (s->definfo, loc);
5291 sRef_clearDerived (s);
5297 ** just for type checking...
5300 static void sRef_killAux (sRef s, fileloc loc)
5302 if (sRef_isValid (s) && !sRef_isShared (s))
5304 if (sRef_isUnknownArrayFetch (s))
5306 sRef_maybeKill (s, loc);
5316 ** kills s and all aliases to s
5319 void sRef_killComplete (sRef s, fileloc loc)
5321 DPRINTF (("Kill complete: %s", sRef_unparseFull (s)));
5322 sRef_aliasSetComplete (sRef_killAux, s, loc);
5325 static bool sRef_equivalent (sRef s1, sRef s2)
5327 return (sRef_compare (s1, s2) == 0);
5331 ** returns an sRef that will not be free'd on function exit.
5334 /*@only@*/ sRef sRef_saveCopy (sRef s)
5338 if (sRef_isValid (s))
5340 bool old = inFunction;
5343 ** Exit the function scope, so this sRef is not
5344 ** stored in the deallocation table.
5348 DPRINTF (("Copying sref: %s", sRef_unparseFull(s)));
5349 ret = sRef_copy (s);
5350 DPRINTF (("Copying ===>: %s", sRef_unparseFull(ret)));
5355 ret = sRef_undefined;
5358 /*@-dependenttrans@*/
5360 /*@=dependenttrans@*/
5363 sRef sRef_copy (sRef s)
5365 if (sRef_isKindSpecial (s))
5368 return s; /* don't copy specials */
5372 if (sRef_isValid (s))
5374 sRef t = sRef_alloc ();
5378 t->modified = s->modified;
5381 t->info = sinfo_copy (s);
5383 t->defstate = s->defstate;
5385 t->nullstate = s->nullstate;
5387 /* start modifications */
5388 t->bufinfo.bufstate = s->bufinfo.bufstate;
5389 t->bufinfo.len = s->bufinfo.len;
5390 t->bufinfo.size = s->bufinfo.size;
5391 /* end modifications */
5393 t->aliaskind = s->aliaskind;
5394 t->oaliaskind = s->oaliaskind;
5396 t->expkind = s->expkind;
5397 t->oexpkind = s->oexpkind;
5399 t->aliasinfo = alinfo_copy (s->aliasinfo);
5400 t->definfo = alinfo_copy (s->definfo);
5401 t->nullinfo = alinfo_copy (s->nullinfo);
5402 t->expinfo = alinfo_copy (s->expinfo);
5404 t->deriv = sRefSet_newDeepCopy (s->deriv);
5410 return sRef_undefined;
5415 # define PREDTEST(func,s) \
5416 do { if (sRef_isInvalid (s)) { return FALSE; } \
5417 else { if (sRef_isConj (s)) \
5418 { return (func (sRef_getConjA (s)) \
5419 || func (sRef_getConjB (s))); }}} while (FALSE);
5421 bool sRef_isAddress (sRef s)
5423 PREDTEST (sRef_isAddress, s);
5424 return (s->kind == SK_ADR);
5428 ** pretty weak... maybe a flag should control this.
5431 bool sRef_isThroughArrayFetch (sRef s)
5433 if (sRef_isValid (s))
5441 if (sRef_isArrayFetch (tref))
5446 lt = sRef_getBase (tref);
5448 } while (sRef_isValid (tref));
5454 bool sRef_isArrayFetch (sRef s)
5456 PREDTEST (sRef_isArrayFetch, s);
5457 return (s->kind == SK_ARRAYFETCH);
5460 bool sRef_isMacroParamRef (sRef s)
5462 if (context_inMacro () && sRef_isCvar (s))
5464 uentry ue = sRef_getUentry (s);
5465 cstring pname = makeParam (uentry_rawName (ue));
5466 uentry mac = usymtab_lookupSafe (pname);
5468 cstring_free (pname);
5469 return (uentry_isValid (mac));
5475 bool sRef_isCvar (sRef s)
5477 PREDTEST (sRef_isCvar, s);
5478 return (s->kind == SK_CVAR);
5481 bool sRef_isConst (sRef s)
5483 PREDTEST (sRef_isConst, s);
5484 return (s->kind == SK_CONST);
5487 bool sRef_isObject (sRef s)
5489 PREDTEST (sRef_isObject, s);
5490 return (s->kind == SK_OBJECT);
5493 bool sRef_isExternal (sRef s)
5495 PREDTEST (sRef_isExternal, s);
5496 return (s->kind == SK_EXTERNAL);
5499 static bool sRef_isDerived (sRef s)
5501 PREDTEST (sRef_isDerived, s);
5502 return (s->kind == SK_DERIVED);
5505 bool sRef_isField (sRef s)
5507 PREDTEST (sRef_isField, s);
5508 return (s->kind == SK_FIELD);
5511 static bool sRef_isIndex (sRef s)
5513 PREDTEST (sRef_isIndex, s);
5514 return (s->kind == SK_ARRAYFETCH);
5517 bool sRef_isAnyParam (sRef s)
5519 PREDTEST (sRef_isAnyParam, s);
5520 return (s->kind == SK_PARAM);
5523 bool sRef_isParam (sRef s)
5525 PREDTEST (sRef_isParam, s);
5526 return (s->kind == SK_PARAM);
5529 bool sRef_isDirectParam (sRef s)
5531 PREDTEST (sRef_isDirectParam, s);
5533 return ((s->kind == SK_CVAR) &&
5534 (s->info->cvar->lexlevel == functionScope) &&
5535 (context_inFunction () &&
5536 (s->info->cvar->index <= uentryList_size (context_getParams ()))));
5539 bool sRef_isPointer (sRef s)
5541 PREDTEST (sRef_isPointer, s);
5542 return (s->kind == SK_PTR);
5546 ** returns true if storage referenced by s is visible
5549 bool sRef_isReference (sRef s)
5551 PREDTEST (sRef_isReference, s);
5553 return (sRef_isPointer (s) || sRef_isIndex (s) || sRef_isGlobal (s)
5554 || (sRef_isField (s) && (sRef_isReference (s->info->field->rec))));
5557 bool sRef_isIReference (sRef s)
5559 return (sRef_isPointer (s) || sRef_isAddress (s) || sRef_isIndex (s)
5560 || sRef_isField (s) || sRef_isArrayFetch (s));
5563 bool sRef_isGlobal (sRef s)
5565 return (sRef_isCvar (s) && (s->info->cvar->lexlevel <= fileScope));
5568 bool sRef_isRealGlobal (sRef s)
5570 return (sRef_isCvar (s) && (s->info->cvar->lexlevel == globScope));
5573 bool sRef_isFileStatic (sRef s)
5575 return (sRef_isCvar (s) && (s->info->cvar->lexlevel == fileScope));
5578 bool sRef_isAliasCheckedGlobal (sRef s)
5580 if (sRef_isGlobal (s))
5582 uentry ue = sRef_getUentry (s);
5584 return context_checkAliasGlob (ue);
5592 void sRef_free (/*@only@*/ sRef s)
5594 if (s != sRef_undefined && s->kind != SK_SPECIAL)
5596 alinfo_free (s->expinfo);
5597 alinfo_free (s->aliasinfo);
5598 alinfo_free (s->definfo);
5599 alinfo_free (s->nullinfo);
5601 sRefSet_free (s->deriv);
5602 s->deriv = sRefSet_undefined;
5609 void sRef_setType (sRef s, ctype t)
5611 if (sRef_isValid (s))
5617 void sRef_setTypeFull (sRef s, ctype t)
5619 if (sRef_isValid (s))
5623 sRefSet_allElements (s->deriv, current)
5625 sRef_setTypeFull (current, ctype_unknown);
5626 } end_sRefSet_allElements ;
5631 sRef_buildField (sRef rec, /*@dependent@*/ cstring f)
5633 return (sRef_buildNCField (rec, f));
5636 static /*@exposed@*/ sRef
5637 sRef_findDerivedField (/*@notnull@*/ sRef rec, cstring f)
5639 sRefSet_allElements (rec->deriv, sr)
5641 if (sRef_isValid (sr))
5643 if (sr->kind == SK_FIELD && cstring_equal (sr->info->field->field, f))
5648 } end_sRefSet_allElements;
5650 return sRef_undefined;
5653 /*@dependent@*/ /*@observer@*/ sRefSet
5654 sRef_derivedFields (sRef rec)
5656 if (sRef_isValid (rec))
5664 return (sRefSet_undefined);
5668 static /*@exposed@*/ sRef
5669 sRef_findDerivedPointer (sRef s)
5671 if (sRef_isValid (s))
5673 sRefSet_realElements (s->deriv, sr)
5675 if (sRef_isValid (sr) && sr->kind == SK_PTR)
5679 } end_sRefSet_realElements;
5682 return sRef_undefined;
5686 sRef_isUnknownArrayFetch (sRef s)
5688 return (sRef_isValid (s)
5689 && s->kind == SK_ARRAYFETCH
5690 && !s->info->arrayfetch->indknown);
5693 static /*@exposed@*/ sRef
5694 sRef_findDerivedArrayFetch (/*@notnull@*/ sRef s, bool isknown, int idx, bool dead)
5699 sRefSet_realElements (s->deriv, sr)
5701 if (sRef_isValid (sr)
5702 && sr->kind == SK_ARRAYFETCH
5703 && sr->info->arrayfetch->indknown
5704 && (sr->info->arrayfetch->ind == idx))
5708 } end_sRefSet_realElements;
5712 sRefSet_realElements (s->deriv, sr)
5714 if (sRef_isValid (sr)
5715 && sr->kind == SK_ARRAYFETCH
5716 && (!sr->info->arrayfetch->indknown
5717 || (sr->info->arrayfetch->indknown &&
5718 sr->info->arrayfetch->ind == 0)))
5720 if (sRef_isDead (sr) || sRef_isKept (sr))
5722 if (dead || context_getFlag (FLG_STRICTUSERELEASED))
5732 } end_sRefSet_realElements;
5735 return sRef_undefined;
5738 static /*@exposed@*/ sRef
5739 sRef_buildNCField (/*@exposed@*/ sRef rec, /*@exposed@*/ cstring f)
5743 if (sRef_isInvalid (rec))
5745 return sRef_undefined;
5749 ** check if the field already has been referenced
5752 s = sRef_findDerivedField (rec, f);
5754 if (sRef_isValid (s))
5760 ctype ct = ctype_realType (rec->type);
5764 s->info = (sinfo) dmalloc (sizeof (*s->info));
5765 s->info->field = (fldinfo) dmalloc (sizeof (*s->info->field));
5766 s->info->field->rec = rec;
5767 s->info->field->field = f; /* doesn't copy f */
5770 if (ctype_isKnown (ct) && ctype_isSU (ct))
5772 uentry ue = uentryList_lookupField (ctype_getFields (ct), f);
5774 if (!uentry_isUndefined (ue))
5776 s->type = uentry_getType (ue);
5778 if (ctype_isMutable (s->type)
5779 && rec->aliaskind != AK_STACK
5780 && !alkind_isStatic (rec->aliaskind))
5782 s->aliaskind = rec->aliaskind;
5786 s->aliaskind = AK_UNKNOWN;
5789 if (sRef_isStateDefined (rec) || sRef_isStateUnknown (rec)
5790 || sRef_isPdefined (rec))
5792 sRef_setStateFromUentry (s, ue);
5796 sRef_setPartsFromUentry (s, ue);
5799 s->oaliaskind = s->aliaskind;
5800 s->oexpkind = s->expkind;
5805 Never report this as an error. It can happen whenever there
5806 is casting involved.
5810 llcontbug (message ("buildNCField --- no field %s: %q / %s",
5811 f, sRef_unparse (s), ctype_unparse (ct)));
5815 return sRef_undefined;
5819 if (rec->defstate == SS_DEFINED
5820 && (s->defstate == SS_UNDEFINED || s->defstate == SS_UNKNOWN))
5822 s->defstate = SS_DEFINED;
5824 else if (rec->defstate == SS_PARTIAL)
5826 s->defstate = SS_PARTIAL;
5828 else if (rec->defstate == SS_ALLOCATED)
5830 if (ctype_isStackAllocated (ct) && ctype_isStackAllocated (s->type))
5832 s->defstate = SS_ALLOCATED;
5836 s->defstate = SS_UNDEFINED;
5839 else if (s->defstate == SS_UNKNOWN)
5841 s->defstate = rec->defstate;
5848 if (s->defstate == SS_UNDEFINED)
5850 ctype rt = ctype_realType (s->type);
5852 if (ctype_isArray (rt) || ctype_isSU (rt))
5854 s->defstate = SS_ALLOCATED;
5858 sRef_addDeriv (rec, s);
5860 if (ctype_isInt (s->type) && cstring_equal (f, REFSNAME))
5862 s->aliaskind = AK_REFS;
5863 s->oaliaskind = AK_REFS;
5871 sRef_isStackAllocated (sRef s)
5873 return (sRef_isValid(s)
5874 && s->defstate == SS_ALLOCATED && ctype_isStackAllocated (s->type));
5878 void sRef_setArrayFetchState (/*@notnull@*/ /*@exposed@*/ sRef s,
5879 /*@notnull@*/ /*@exposed@*/ sRef arr)
5881 if (ctype_isRealAP (arr->type))
5883 s->type = ctype_baseArrayPtr (arr->type);
5886 /* a hack, methinks... makeArrayFetch (&a[0]) ==> a[] */
5887 if (sRef_isAddress (arr))
5889 sRef t = arr->info->ref;
5891 if (sRef_isArrayFetch (t))
5893 s->info->arrayfetch->arr = t->info->arrayfetch->arr;
5896 else if (ctype_isRealPointer (arr->type))
5898 sRef sp = sRef_findDerivedPointer (arr);
5901 if (sRef_isValid (sp))
5904 if (ctype_isMutable (s->type))
5906 sRef_setExKind (s, sRef_getExKind (sp), fileloc_undefined);
5909 s->aliaskind = sp->aliaskind;
5912 s->defstate = sp->defstate;
5914 if (s->defstate == SS_DEFINED)
5916 if (!context_getFlag (FLG_STRICTDESTROY))
5918 s->defstate = SS_PARTIAL;
5922 s->nullstate = sp->nullstate;
5926 if (arr->defstate == SS_UNDEFINED)
5928 s->defstate = SS_UNUSEABLE;
5930 else if ((arr->defstate == SS_ALLOCATED) && !ctype_isSU (s->type))
5932 s->defstate = SS_UNDEFINED;
5936 if (!context_getFlag (FLG_STRICTDESTROY))
5938 s->defstate = SS_PARTIAL;
5942 s->defstate = SS_DEFINED;
5946 ** Very weak checking for array elements.
5948 ** s->defstate = arr->defstate;
5952 sRef_setExKind (s, sRef_getExKind (arr), g_currentloc);
5954 if (arr->aliaskind == AK_LOCAL || arr->aliaskind == AK_FRESH)
5956 s->aliaskind = AK_LOCAL;
5960 s->aliaskind = AK_UNKNOWN;
5963 sRef_setTypeState (s);
5968 if (arr->defstate == SS_DEFINED)
5971 ** Very weak checking for array elements.
5973 ** s->defstate = arr->defstate;
5976 if (context_getFlag (FLG_STRICTDESTROY))
5978 s->defstate = SS_DEFINED;
5982 s->defstate = SS_PARTIAL;
5985 else if (arr->defstate == SS_ALLOCATED)
5987 if (ctype_isRealArray (s->type))
5989 s->defstate = SS_ALLOCATED;
5993 if (!s->info->arrayfetch->indknown)
5996 ** is index is unknown, elements is defined or
5997 ** allocated is any element is!
6000 s->defstate = SS_UNDEFINED;
6002 sRefSet_allElements (arr->deriv, sr)
6004 if (sRef_isValid (sr))
6006 if (sr->defstate == SS_ALLOCATED)
6008 s->defstate = SS_ALLOCATED;
6012 if (sr->defstate == SS_DEFINED)
6014 if (context_getFlag (FLG_STRICTDESTROY))
6016 s->defstate = SS_DEFINED;
6020 s->defstate = SS_PARTIAL;
6027 } end_sRefSet_allElements;
6032 s->defstate = SS_UNDEFINED;
6038 s->defstate = arr->defstate;
6043 ** kludgey way to guess where aliaskind applies
6046 if (ctype_isMutable (s->type)
6047 && !ctype_isPointer (arr->type)
6048 && !alkind_isStatic (arr->aliaskind)
6049 && !alkind_isStack (arr->aliaskind)) /* evs - 2000-06-20: don't pass stack allocation to members */
6051 s->aliaskind = arr->aliaskind;
6055 s->aliaskind = AK_UNKNOWN;
6058 sRef_setTypeState (s);
6061 if (sRef_isObserver (arr))
6063 s->expkind = XO_OBSERVER;
6067 /*@exposed@*/ sRef sRef_buildArrayFetch (/*@exposed@*/ sRef arr)
6071 if (!sRef_isValid (arr)) {
6072 /*@-nullret@*/ return arr /*@=nullret@*/;
6075 if (ctype_isRealPointer (arr->type))
6077 (void) sRef_buildPointer (arr); /* do this to define arr! */
6080 s = sRef_findDerivedArrayFetch (arr, FALSE, 0, FALSE);
6082 if (sRef_isValid (s))
6084 sRef_setExKind (s, sRef_getExKind (arr), g_currentloc);
6091 s->kind = SK_ARRAYFETCH;
6092 s->info = (sinfo) dmalloc (sizeof (*s->info));
6093 s->info->arrayfetch = (ainfo) dmalloc (sizeof (*s->info->arrayfetch));
6094 s->info->arrayfetch->indknown = FALSE;
6095 s->info->arrayfetch->ind = 0;
6096 s->info->arrayfetch->arr = arr;
6097 sRef_setArrayFetchState (s, arr);
6098 s->oaliaskind = s->aliaskind;
6099 s->oexpkind = s->expkind;
6101 if (!context_inProtectVars ())
6103 sRef_addDeriv (arr, s);
6111 sRef_buildArrayFetchKnown (/*@exposed@*/ sRef arr, int i)
6115 if (!sRef_isValid (arr)) {
6116 /*@-nullret@*/ return arr /*@=nullret@*/;
6119 if (ctype_isRealPointer (arr->type))
6121 (void) sRef_buildPointer (arr); /* do this to define arr! */
6124 s = sRef_findDerivedArrayFetch (arr, TRUE, i, FALSE);
6126 if (sRef_isValid (s))
6128 sRef_setExKind (s, sRef_getExKind (arr), g_currentloc);
6135 s->kind = SK_ARRAYFETCH;
6136 s->info = (sinfo) dmalloc (sizeof (*s->info));
6137 s->info->arrayfetch = (ainfo) dmalloc (sizeof (*s->info->arrayfetch));
6138 s->info->arrayfetch->arr = arr;
6139 s->info->arrayfetch->indknown = TRUE;
6140 s->info->arrayfetch->ind = i;
6142 sRef_setArrayFetchState (s, arr);
6144 s->oaliaskind = s->aliaskind;
6145 s->oexpkind = s->expkind;
6146 sRef_addDeriv (arr, s);
6153 ** sets everything except for defstate
6157 sRef_setPartsFromUentry (sRef s, uentry ue)
6160 llassert (sRef_isValid (s));
6162 s->aliaskind = alkind_derive (s->aliaskind, uentry_getAliasKind (ue));
6163 s->oaliaskind = s->aliaskind;
6165 if (s->expkind == XO_UNKNOWN)
6167 s->expkind = uentry_getExpKind (ue);
6170 s->oexpkind = s->expkind;
6172 if (s->nullstate == NS_UNKNOWN)
6174 s->nullstate = sRef_getNullState (uentry_getSref (ue));
6177 if (s->aliaskind == AK_IMPONLY
6178 && (sRef_isExposed (s) || sRef_isObserver (s)))
6180 s->oaliaskind = s->aliaskind = AK_IMPDEPENDENT;
6186 sRef_setStateFromAbstractUentry (sRef s, uentry ue)
6188 llassert (sRef_isValid (s));
6190 sRef_setPartsFromUentry (s, ue);
6192 s->aliaskind = alkind_derive (s->aliaskind, uentry_getAliasKind (ue));
6193 s->oaliaskind = s->aliaskind;
6195 if (s->expkind == XO_UNKNOWN)
6197 s->expkind = uentry_getExpKind (ue);
6200 s->oexpkind = s->expkind;
6204 sRef_setStateFromUentry (sRef s, uentry ue)
6208 llassert (sRef_isValid (s));
6210 sRef_setPartsFromUentry (s, ue);
6212 defstate = uentry_getDefState (ue);
6214 if (sstate_isKnown (defstate))
6216 s->defstate = defstate;
6225 sRef_buildPointer (/*@exposed@*/ sRef t)
6227 DPRINTF (("build pointer: %s", sRef_unparse (t)));
6229 if (sRef_isInvalid (t)) return sRef_undefined;
6231 if (sRef_isAddress (t))
6233 DPRINTF (("Return ref: %s", sRef_unparse (t->info->ref)));
6234 return (t->info->ref);
6238 sRef s = sRef_findDerivedPointer (t);
6240 DPRINTF (("find derived: %s", sRef_unparse (s)));
6242 if (sRef_isValid (s))
6245 sRef_setExKind (s, sRef_getExKind (t), g_currentloc);
6246 s->oaliaskind = s->aliaskind;
6247 s->oexpkind = s->expkind;
6253 s = sRef_constructPointerAux (t);
6255 DPRINTF (("construct: %s", sRef_unparse (s)));
6257 if (sRef_isValid (s))
6259 sRef_addDeriv (t, s);
6261 s->oaliaskind = s->aliaskind;
6262 s->oexpkind = s->expkind;
6271 sRef_constructPointer (sRef t)
6274 return sRef_buildPointer (t);
6277 static /*@exposed@*/ sRef sRef_constructDerefAux (sRef t, bool isdead)
6279 if (sRef_isValid (t))
6284 ** if there is a derived t[?], return that. Otherwise, *t.
6288 s = sRef_findDerivedArrayFetch (t, FALSE, 0, isdead);
6290 if (sRef_isValid (s))
6296 sRef ret = sRef_constructPointer (t);
6299 ** This is necessary to prevent infinite depth
6300 ** in checking complete destruction.
6306 /* ret->defstate = SS_UNKNOWN; */
6317 return sRef_undefined;
6321 sRef sRef_constructDeref (sRef t)
6323 return sRef_constructDerefAux (t, FALSE);
6326 sRef sRef_constructDeadDeref (sRef t)
6328 return sRef_constructDerefAux (t, TRUE);
6332 sRef_constructPointerAux (/*@notnull@*/ /*@exposed@*/ sRef t)
6334 sRef s = sRef_new ();
6339 s->info = (sinfo) dmalloc (sizeof (*s->info));
6342 if (ctype_isRealAP (rt))
6344 s->type = ctype_baseArrayPtr (rt);
6347 st = ctype_realType (s->type);
6350 if (t->defstate == SS_UNDEFINED)
6352 s->defstate = SS_UNUSEABLE;
6354 else if ((t->defstate == SS_ALLOCATED) && !ctype_isSU (st))
6356 s->defstate = SS_UNDEFINED;
6360 s->defstate = t->defstate;
6363 if (t->aliaskind == AK_LOCAL || t->aliaskind == AK_FRESH)
6365 s->aliaskind = AK_LOCAL;
6369 s->aliaskind = AK_UNKNOWN;
6372 sRef_setExKind (s, sRef_getExKind (t), fileloc_undefined);
6373 sRef_setTypeState (s);
6375 s->oaliaskind = s->aliaskind;
6376 s->oexpkind = s->expkind;
6381 bool sRef_hasDerived (sRef s)
6383 return (sRef_isValid (s) && !sRefSet_isEmpty (s->deriv));
6387 sRef_clearDerived (sRef s)
6389 if (sRef_isValid (s))
6391 sRefSet_clear (s->deriv);
6396 sRef_clearDerivedComplete (sRef s)
6399 if (sRef_isValid (s))
6401 sRef base = sRef_getBaseSafe (s);
6403 while (sRef_isValid (base))
6405 sRefSet_clear (base->deriv);
6406 base = sRef_getBaseSafe (base);
6409 sRefSet_clear (s->deriv);
6414 sRef_makePointer (sRef s)
6417 sRef res = sRef_buildPointer (s);
6419 DPRINTF (("Res: %s", sRef_unparse (res)));
6424 ** &a[] => a (this is for out params)
6428 sRef_makeAnyArrayFetch (/*@exposed@*/ sRef arr)
6431 if (sRef_isAddress (arr))
6433 return (arr->info->ref);
6437 return (sRef_buildArrayFetch (arr));
6442 sRef_makeArrayFetch (sRef arr)
6444 return (sRef_buildArrayFetch (arr));
6448 sRef_makeArrayFetchKnown (sRef arr, int i)
6450 return (sRef_buildArrayFetchKnown (arr, i));
6454 sRef_makeField (sRef rec, /*@dependent@*/ cstring f)
6457 ret = sRef_buildField (rec, f);
6462 sRef_makeNCField (sRef rec, /*@dependent@*/ cstring f)
6464 return (sRef_buildNCField (rec, f));
6468 sRef_unparseKindName (sRef s)
6472 if (s == sRef_undefined) return cstring_makeLiteral ("<invalid>");
6474 s = sRef_fixConj (s);
6479 if (sRef_isLocalVar (s))
6481 result = cstring_makeLiteral ("Variable");
6485 result = cstring_makeLiteral ("Undef global");
6489 result = cstring_makeLiteral ("Out parameter");
6492 if (sRef_isAnyParam (s->info->arrayfetch->arr))
6494 result = cstring_makeLiteral ("Out parameter");
6496 else if (sRef_isIndexKnown (s))
6498 result = cstring_makeLiteral ("Array element");
6502 result = cstring_makeLiteral ("Value");
6506 if (sRef_isAnyParam (s->info->ref))
6508 result = cstring_makeLiteral ("Out parameter");
6512 result = cstring_makeLiteral ("Value");
6516 result = cstring_makeLiteral ("Value");
6519 result = cstring_makeLiteral ("Field");
6522 result = cstring_makeLiteral ("Object");
6524 case SK_UNCONSTRAINED:
6525 result = cstring_makeLiteral ("<anything>");
6534 result = cstring_makeLiteral ("<unknown>");
6537 result = cstring_makeLiteral ("<conj>");
6540 result = cstring_makeLiteral ("Storage");
6548 sRef_unparseKindNamePlain (sRef s)
6552 if (s == sRef_undefined) return cstring_makeLiteral ("<invalid>");
6554 s = sRef_fixConj (s);
6559 if (sRef_isLocalVar (s))
6561 result = cstring_makeLiteral ("Variable");
6565 result = cstring_makeLiteral ("Global");
6569 result = cstring_makeLiteral ("Parameter");
6572 if (sRef_isAnyParam (s->info->arrayfetch->arr))
6574 result = cstring_makeLiteral ("Parameter");
6576 else if (sRef_isIndexKnown (s))
6578 result = cstring_makeLiteral ("Array element");
6582 result = cstring_makeLiteral ("Value");
6586 if (sRef_isAnyParam (s->info->ref))
6588 result = cstring_makeLiteral ("Parameter");
6592 result = cstring_makeLiteral ("Value");
6596 result = cstring_makeLiteral ("Value");
6599 result = cstring_makeLiteral ("Field");
6602 result = cstring_makeLiteral ("Object");
6605 result = cstring_makeLiteral ("Storage");
6607 case SK_UNCONSTRAINED:
6608 result = cstring_makeLiteral ("<anything>");
6617 result = cstring_makeLiteral ("<unknown>");
6620 result = cstring_makeLiteral ("<conj>");
6632 sRef_copyState (sRef s1, sRef s2)
6634 if (sRef_isValid (s1) && sRef_isValid (s2))
6636 s1->defstate = s2->defstate;
6638 s1->nullstate = s2->nullstate;
6639 s1->nullinfo = alinfo_update (s1->nullinfo, s2->nullinfo);
6641 /* start modifications */
6642 s1->bufinfo.bufstate = s2->bufinfo.bufstate;
6643 s1->bufinfo.len = s2->bufinfo.len;
6644 s1->bufinfo.size = s2->bufinfo.size;
6645 /* end modifications */
6647 s1->aliaskind = s2->aliaskind;
6648 s1->aliasinfo = alinfo_update (s1->aliasinfo, s2->aliasinfo);
6650 s1->expkind = s2->expkind;
6651 s1->expinfo = alinfo_update (s1->expinfo, s2->expinfo);
6653 s1->safe = s2->safe;
6658 sRef_makeNew (ctype ct, sRef t, cstring name)
6660 sRef s = sRef_new ();
6665 llassert (sRef_isValid (t));
6666 s->defstate = t->defstate;
6668 s->aliaskind = t->aliaskind;
6669 s->oaliaskind = s->aliaskind;
6671 s->nullstate = t->nullstate;
6673 s->expkind = t->expkind;
6674 s->oexpkind = s->expkind;
6676 s->info = (sinfo) dmalloc (sizeof (*s->info));
6677 s->info->fname = name;
6679 /* start modifications */
6680 s->bufinfo.bufstate = t->bufinfo.bufstate;
6681 /* end modifications */
6687 sRef_makeType (ctype ct)
6689 sRef s = sRef_new ();
6694 s->defstate = SS_UNKNOWN;
6695 s->aliaskind = AK_UNKNOWN;
6696 s->nullstate = NS_UNKNOWN;
6697 /* start modification */
6698 s->bufinfo.bufstate = BB_NOTNULLTERMINATED;
6699 /* end modification */
6702 if (ctype_isUA (ct))
6704 typeId uid = ctype_typeId (ct);
6705 uentry ue = usymtab_getTypeEntrySafe (uid);
6707 if (uentry_isValid (ue))
6709 sRef_mergeStateQuiet (s, uentry_getSref (ue));
6713 s->oaliaskind = s->aliaskind;
6714 s->oexpkind = s->expkind;
6720 sRef_makeConst (ctype ct)
6722 sRef s = sRef_new ();
6727 s->defstate = SS_UNKNOWN;
6728 s->aliaskind = AK_UNKNOWN;
6729 s->nullstate = NS_UNKNOWN;
6730 /* start modification */
6731 s->bufinfo.bufstate = BB_NULLTERMINATED;
6732 /* end modification */
6735 if (ctype_isUA (ct))
6737 typeId uid = ctype_typeId (ct);
6738 uentry te = usymtab_getTypeEntrySafe (uid);
6740 if (uentry_isValid (te))
6742 sRef_mergeStateQuiet (s, uentry_getSref (te));
6747 s->oaliaskind = s->aliaskind;
6748 s->oexpkind = s->expkind;
6753 bool sRef_hasName (sRef s)
6755 if (sRef_isInvalid (s))
6764 uentry u = usymtab_getRefQuiet (s->info->cvar->lexlevel,
6765 s->info->cvar->index);
6766 return (uentry_hasName (u));
6770 uentry u = uentryList_getN (context_getParams (),
6773 return (uentry_hasName (u));
6780 bool sRef_sameObject (sRef s1, sRef s2)
6782 return sRef_sameName(s1, s2);
6785 sRef_sameName (sRef s1, sRef s2)
6787 if (sRef_isInvalid (s1))
6789 return sRef_isInvalid (s2);
6792 if (sRef_isInvalid (s2))
6800 if (s2->kind == SK_CVAR)
6802 return (s1->info->cvar->lexlevel == s2->info->cvar->lexlevel
6803 && s1->info->cvar->index == s2->info->cvar->index);
6805 else if (s2->kind == SK_PARAM)
6807 if (context_inFunctionLike ())
6809 uentry u1 = usymtab_getRefQuiet (s1->info->cvar->lexlevel,
6810 s1->info->cvar->index);
6811 uentry u2 = uentryList_getN (context_getParams (),
6814 return (cstring_equalFree (uentry_getName (u1),
6815 uentry_getName (u2)));
6828 if (s2->kind == SK_PARAM)
6830 return (s1->info->paramno == s2->info->paramno);
6832 else if (s2->kind == SK_CVAR)
6834 if (context_inFunctionLike ())
6836 uentry u1 = uentryList_getN (context_getParams (),
6838 uentry u2 = usymtab_getRefQuiet (s2->info->cvar->lexlevel,
6839 s2->info->cvar->index);
6842 return (cstring_equalFree (uentry_getName (u1),
6843 uentry_getName (u2)));
6856 case SK_UNCONSTRAINED:
6860 if (s2->kind == SK_ARRAYFETCH)
6862 if (bool_equal (s1->info->arrayfetch->indknown,
6863 s2->info->arrayfetch->indknown))
6865 if (!s1->info->arrayfetch->indknown
6866 || (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind))
6868 return sRef_sameName (s1->info->arrayfetch->arr,
6869 s2->info->arrayfetch->arr);
6876 if (s2->kind == SK_FIELD)
6878 if (cstring_equal (s1->info->field->field,
6879 s2->info->field->field))
6881 return sRef_sameName (s1->info->field->rec,
6882 s2->info->field->rec);
6891 if (s2->kind == s1->kind)
6893 return sRef_sameName (s1->info->ref,
6901 return sRef_sameName (sRef_getConjA (s1), s2);
6905 return (s2->kind == SK_UNKNOWN);
6908 if (s2->kind == s1->kind)
6910 return (ctype_equal (s1->type, s2->type));
6915 if (s2->kind == SK_SPECIAL)
6917 return (s1->info->spec == s2->info->spec);
6921 return (s2->kind == SK_RESULT);
6929 sRef_fixOuterRef (/*@returned@*/ sRef s)
6931 sRef root = sRef_getRootBase (s);
6933 if (sRef_isCvar (root))
6935 uentry ue = usymtab_getRefQuiet (root->info->cvar->lexlevel,
6936 root->info->cvar->index);
6938 if (uentry_isValid (ue))
6940 sRef uref = uentry_getSref (ue);
6941 sRef sr = sRef_fixBase (s, uref);
6947 llcontbug (message ("sRef_fixOuterRef: undefined: %q", sRef_unparseDebug (s)));
6956 sRef_storeState (sRef s)
6958 if (sRef_isInvalid (s)) return;
6960 s->oaliaskind = s->aliaskind;
6961 s->oexpkind = s->expkind;
6964 static void sRef_resetStateAux (sRef s, /*@unused@*/ fileloc loc)
6966 sRef_resetState (s);
6970 sRef_resetState (sRef s)
6972 bool changed = FALSE;
6973 if (sRef_isInvalid (s)) return;
6976 if (s->oaliaskind == AK_KILLREF && !sRef_isParam (s))
6979 ** killref is used in a kludgey way, to save having to add
6980 ** another alias kind (see usymtab_handleParams)
6983 if (s->expkind != s->oexpkind)
6986 s->expkind = s->oexpkind;
6991 if (s->expkind != s->oexpkind)
6994 s->expkind = s->oexpkind;
6997 if (s->aliaskind != s->oaliaskind
6998 && s->aliaskind != AK_REFCOUNTED
6999 && s->aliaskind != AK_REFS)
7002 s->aliaskind = s->oaliaskind;
7008 sRef_clearDerived (s);
7014 sRef_resetStateComplete (sRef s)
7016 sRef_innerAliasSetComplete (sRef_resetStateAux, s, fileloc_undefined);
7020 sRef_fixBase (/*@returned@*/ sRef s, /*@returned@*/ sRef base)
7022 sRef tmp = sRef_undefined;
7025 if (sRef_isInvalid (s)) return s;
7026 if (sRef_isInvalid (base)) return base;
7036 tmp = sRef_fixBase (s->info->arrayfetch->arr, base);
7038 if (s->info->arrayfetch->indknown)
7040 ret = sRef_makeArrayFetchKnown (tmp, s->info->arrayfetch->ind);
7044 ret = sRef_makeArrayFetch (tmp);
7048 tmp = sRef_fixBase (s->info->field->rec, base);
7049 ret = sRef_buildNCField (tmp, s->info->field->field);
7052 tmp = sRef_fixBase (s->info->ref, base);
7053 ret = sRef_makePointer (tmp);
7056 tmp = sRef_fixBase (s->info->ref, base);
7057 ret = sRef_makeAddress (tmp);
7063 tmp = sRef_fixBase (s->info->conj->a, base);
7064 tmpb = sRef_fixBase (s->info->conj->b, base);
7066 ret = sRef_makeConj (tmp, tmpb);
7075 static /*@exposed@*/ sRef
7076 sRef_fixDirectBase (sRef s, sRef base)
7081 if (sRef_isInvalid (s))
7083 return sRef_undefined;
7089 if (s->info->arrayfetch->indknown)
7091 ret = sRef_makeArrayFetchKnown (base, s->info->arrayfetch->ind);
7095 ret = sRef_makeArrayFetch (base);
7099 ret = sRef_buildNCField (base, s->info->field->field);
7102 ret = sRef_makePointer (base);
7105 ret = sRef_makeAddress (base);
7111 tmpa = sRef_fixDirectBase (s->info->conj->a, base);
7112 tmpb = sRef_fixDirectBase (s->info->conj->b, base);
7114 ret = sRef_makeConj (tmpa, tmpb);
7120 sRef_copyState (ret, s);
7125 sRef_isAllocIndexRef (sRef s)
7127 return (sRef_isArrayFetch (s) && !(s->info->arrayfetch->indknown)
7128 && sRef_isAllocated (s->info->arrayfetch->arr));
7132 sRef_showRefLost (sRef s)
7134 if (sRef_hasAliasInfoLoc (s))
7136 llgenindentmsg (cstring_makeLiteral ("Original reference lost"),
7137 sRef_getAliasInfoLoc (s));
7142 sRef_showRefKilled (sRef s)
7144 if (sRef_hasStateInfoLoc (s))
7146 llgenindentmsg (message ("Storage %q released",
7147 sRef_unparse (s)), sRef_getStateInfoLoc (s));
7152 sRef_showStateInconsistent (sRef s)
7154 if (sRef_hasStateInfoLoc (s))
7157 (message ("Storage %qbecomes inconsistent (released on one branch)",
7158 sRef_unparseOpt (s)),
7159 sRef_getStateInfoLoc (s));
7164 sRef_showStateInfo (sRef s)
7166 if (sRef_hasStateInfoLoc (s))
7168 if (s->defstate == SS_DEAD)
7171 (message ("Storage %qis released", sRef_unparseOpt (s)),
7172 sRef_getStateInfoLoc (s));
7174 else if (s->defstate == SS_ALLOCATED || s->defstate == SS_DEFINED)
7177 (message ("Storage %qis %s", sRef_unparseOpt (s),
7178 sstate_unparse (s->defstate)),
7179 sRef_getStateInfoLoc (s));
7181 else if (s->defstate == SS_UNUSEABLE)
7184 (message ("Storage %qbecomes inconsistent (clauses merge with"
7185 "%qreleased on one branch)",
7186 sRef_unparseOpt (s),
7187 sRef_unparseOpt (s)),
7188 sRef_getStateInfoLoc (s));
7192 llgenindentmsg (message ("Storage %qbecomes %s",
7193 sRef_unparseOpt (s),
7194 sstate_unparse (s->defstate)),
7195 sRef_getStateInfoLoc (s));
7201 sRef_showExpInfo (sRef s)
7203 if (sRef_hasExpInfoLoc (s))
7205 llgenindentmsg (message ("Storage %qbecomes %s", sRef_unparseOpt (s),
7206 exkind_unparse (s->expkind)),
7207 sRef_getExpInfoLoc (s));
7212 sRef_showNullInfo (sRef s)
7214 if (sRef_hasNullInfoLoc (s) && sRef_isKnown (s))
7216 switch (s->nullstate)
7220 fileloc loc = sRef_getNullInfoLoc (s);
7222 if (fileloc_isDefined (loc) && !fileloc_isLib (loc))
7225 (message ("Storage %qbecomes null", sRef_unparseOpt (s)),
7232 fileloc loc = sRef_getNullInfoLoc (s);
7234 if (fileloc_isDefined (loc) && !fileloc_isLib (loc))
7236 llgenindentmsg (message ("Storage %qbecomes null", sRef_unparseOpt (s)),
7244 (message ("Storage %qmay become null", sRef_unparseOpt (s)),
7245 sRef_getNullInfoLoc (s));
7250 (message ("Storage %qbecomes not null", sRef_unparseOpt (s)),
7251 sRef_getNullInfoLoc (s));
7255 (message ("Storage %qnull state becomes unknown",
7256 sRef_unparseOpt (s)),
7257 sRef_getNullInfoLoc (s));
7266 (message ("<error case> Storage %q becomes %s",
7268 nstate_unparse (s->nullstate)),
7269 sRef_getNullInfoLoc (s));
7277 sRef_showAliasInfo (sRef s)
7279 if (sRef_hasAliasInfoLoc (s))
7281 if (sRef_isFresh (s))
7284 (message ("Fresh storage %qallocated", sRef_unparseOpt (s)),
7285 sRef_getAliasInfoLoc (s));
7289 if (!sRef_isRefCounted (s))
7292 (message ("Storage %qbecomes %s",
7293 sRef_unparseOpt (s),
7294 alkind_unparse (sRef_getAliasKind (s))),
7295 sRef_getAliasInfoLoc (s));
7302 sRef_mergeNullState (sRef s, nstate n)
7304 if (sRef_isValid (s))
7310 if (n != old && n != NS_UNKNOWN)
7314 s->nullinfo = alinfo_updateLoc (s->nullinfo, g_currentloc);
7319 llbuglit ("sRef_mergeNullState: invalid");
7324 sRef_possiblyNull (sRef s)
7326 if (sRef_isValid (s))
7328 if (s->nullstate == NS_ABSNULL)
7330 ctype rct = ctype_realType (s->type);
7332 if (ctype_isAbstract (rct))
7338 if (ctype_isUser (rct))
7340 uentry ue = usymtab_getTypeEntry (ctype_typeId (rct));
7342 return (nstate_possiblyNull
7343 (sRef_getNullState (uentry_getSref (ue))));
7353 return nstate_possiblyNull (s->nullstate);
7361 sRef_getScopeName (sRef s)
7363 sRef base = sRef_getRootBase (s);
7365 if (sRef_isRealGlobal (base))
7367 return (cstring_makeLiteralTemp ("Global"));
7369 else if (sRef_isFileStatic (base))
7371 return (cstring_makeLiteralTemp ("Static"));
7375 return (cstring_makeLiteralTemp ("Local"));
7380 sRef_unparseScope (sRef s)
7382 sRef base = sRef_getRootBase (s);
7384 if (sRef_isRealGlobal (base))
7386 return (cstring_makeLiteralTemp ("global"));
7388 else if (sRef_isFileStatic (base))
7390 return (cstring_makeLiteralTemp ("file static"));
7399 sRef_getScope (sRef s)
7401 llassert (sRef_isValid (s));
7403 if (sRef_isCvar (s))
7405 return s->info->cvar->lexlevel;
7407 else if (sRef_isParam (s))
7418 sRef_isDead (sRef s)
7420 return (sRef_isValid (s) && (s)->defstate == SS_DEAD);
7424 sRef_isDeadStorage (sRef s)
7426 if (sRef_isValid (s))
7428 if (s->defstate == SS_DEAD
7429 || s->defstate == SS_UNUSEABLE
7430 || s->defstate == SS_UNDEFINED
7431 || s->defstate == SS_UNKNOWN)
7437 return (sRef_isDefinitelyNull (s));
7447 sRef_isPossiblyDead (sRef s)
7449 return (sRef_isValid (s) && s->defstate == SS_HOFFA);
7452 bool sRef_isStateLive (sRef s)
7454 if (sRef_isValid (s))
7456 sstate ds = s->defstate;
7458 return (!(ds == SS_UNDEFINED
7460 || ds == SS_UNUSEABLE
7461 || ds == SS_HOFFA));
7470 bool sRef_isStateUndefined (sRef s)
7472 return ((sRef_isValid(s)) && ((s)->defstate == SS_UNDEFINED));
7475 bool sRef_isJustAllocated (sRef s)
7477 if (sRef_isAllocated (s))
7479 sRefSet_allElements (s->deriv, el)
7481 if (!(sRef_isStateUndefined (el) || sRef_isUnuseable (el)))
7485 } end_sRefSet_allElements ;
7494 sRef_isAllocatedStorage (sRef s)
7496 if (sRef_isValid (s) && ynm_toBoolStrict (sRef_isReadable (s)))
7498 return (ctype_isVisiblySharable (sRef_getType (s)));
7507 sRef_isUnuseable (sRef s)
7509 return ((sRef_isValid(s)) && ((s)->defstate == SS_UNUSEABLE));
7513 sRef_perhapsNull (sRef s)
7515 if (sRef_isValid (s))
7517 if (s->nullstate == NS_ABSNULL)
7519 ctype rct = ctype_realType (s->type);
7521 if (ctype_isAbstract (rct))
7527 if (ctype_isUser (rct))
7529 uentry ue = usymtab_getTypeEntry (ctype_typeId (rct));
7531 return (nstate_perhapsNull
7532 (sRef_getNullState (uentry_getSref (ue))));
7542 return nstate_perhapsNull (s->nullstate);
7550 ** definitelyNull --- called when TRUE is good
7554 sRef_definitelyNull (sRef s)
7556 return (sRef_isValid (s)
7557 && (s->nullstate == NS_DEFNULL || s->nullstate == NS_CONSTNULL));
7561 ** based on sRef_similar
7565 sRef_setDerivNullState (sRef set, sRef guide, nstate ns)
7567 if (sRef_isValid (set))
7569 sRef deriv = sRef_getDeriv (set, guide);
7571 if (sRef_isValid (deriv))
7573 deriv->nullstate = ns;
7578 static /*@exposed@*/ sRef
7579 sRef_getDeriv (/*@returned@*/ /*@notnull@*/ sRef set, sRef guide)
7581 llassert (sRef_isValid (set));
7582 llassert (sRef_isValid (guide));
7584 switch (guide->kind)
7587 llassert (set->kind == SK_CVAR);
7592 llassert (set->kind == guide->kind);
7593 llassert (set->info->paramno == guide->info->paramno);
7599 if (set->kind == SK_ARRAYFETCH
7600 && (sRef_similar (set->info->arrayfetch->arr,
7601 guide->info->arrayfetch->arr)))
7607 return (sRef_makeAnyArrayFetch
7608 (sRef_getDeriv (set, guide->info->arrayfetch->arr)));
7613 if (set->kind == SK_PTR && sRef_similar (set->info->ref, guide->info->ref))
7619 return (sRef_makePointer (sRef_getDeriv (set, guide->info->ref)));
7624 if ((set->kind == SK_FIELD &&
7625 (sRef_similar (set->info->field->rec, guide->info->field->rec) &&
7626 cstring_equal (set->info->field->field, guide->info->field->field))))
7632 return (sRef_makeField (sRef_getDeriv (set, guide->info->field->rec),
7633 guide->info->field->field));
7637 if ((set->kind == SK_ADR) && sRef_similar (set->info->ref, guide->info->ref))
7643 return (sRef_makeAddress (sRef_getDeriv (set, guide->info->ref)));
7648 return sRef_undefined;
7652 case SK_UNCONSTRAINED:
7660 return sRef_undefined;
7667 ** sRef_aliasCheckPred
7669 ** A confusing but spiffy function:
7671 ** Calls predf (s, e, text, <alias>) on s and all of s's aliases
7672 ** (unless checkAliases (s) is FALSE).
7674 ** For alias calls, calls as
7675 ** predf (alias, e, text, s)
7679 sRef_aliasCheckPred (bool (predf) (sRef, exprNode, sRef, exprNode),
7680 /*@null@*/ bool (checkAliases) (sRef),
7681 sRef s, exprNode e, exprNode err)
7683 bool error = (*predf)(s, e, sRef_undefined, err);
7686 if (checkAliases != NULL && !(checkAliases (s)))
7688 /* don't check aliases */
7692 sRefSet aliases = usymtab_allAliases (s);
7695 sRefSet_realElements (aliases, current)
7697 if (sRef_isValid (current))
7699 if (!sRef_similar (current, s)
7700 || (error && sRef_sameName (current, s)))
7702 (void) (*predf)(current, e, s, err);
7705 } end_sRefSet_realElements;
7707 sRefSet_free (aliases);
7712 ** return TRUE iff predf (s) is true for s or any alias of s
7716 sRef_aliasCheckSimplePred (sRefTest predf, sRef s)
7727 aliases = usymtab_allAliases (s);
7729 sRefSet_realElements (aliases, current)
7731 if (sRef_isValid (current))
7733 sRef cref = sRef_updateSref (current);
7735 /* Whoa! a very kludgey way to make sure the right sref is used
7736 ** where there is a conditional symbol table. I am beginning
7737 ** to think that having a conditional symbol table wasn't such
7744 sRefSet_free (aliases);
7748 } end_sRefSet_realElements;
7750 sRefSet_free (aliases);
7756 sRef_aliasCompleteSimplePred (bool (predf) (sRef), sRef s)
7759 bool result = FALSE;
7762 aliases = usymtab_allAliases (s);
7764 if ((*predf)(s)) result = TRUE;
7767 sRefSet_realElements (aliases, current)
7769 if (sRef_isValid (current))
7771 current = sRef_updateSref (current);
7772 if ((*predf)(current)) result = TRUE;
7774 } end_sRefSet_realElements;
7776 sRefSet_free (aliases);
7781 sRef_aliasSetComplete (void (predf) (sRef, fileloc), sRef s, fileloc loc)
7785 aliases = usymtab_allAliases (s);
7789 sRefSet_realElements (aliases, current)
7791 if (sRef_isValid (current))
7793 current = sRef_updateSref (current);
7794 ((*predf)(current, loc));
7796 } end_sRefSet_realElements;
7798 sRefSet_free (aliases);
7802 sRef_aliasSetCompleteParam (void (predf) (sRef, alkind, fileloc), sRef s,
7803 alkind kind, fileloc loc)
7808 if (sRef_isDeep (s))
7810 aliases = usymtab_allAliases (s);
7814 aliases = usymtab_aliasedBy (s);
7817 (*predf)(s, kind, loc);
7819 sRefSet_realElements (aliases, current)
7821 if (sRef_isValid (current))
7823 current = sRef_updateSref (current);
7824 ((*predf)(current, kind, loc));
7826 } end_sRefSet_realElements;
7828 sRefSet_free (aliases);
7832 sRef_innerAliasSetComplete (void (predf) (sRef, fileloc), sRef s, fileloc loc)
7838 if (!sRef_isValid (s)) return;
7842 ** Type equivalence checking is necessary --- there might be casting.
7849 case SK_UNCONSTRAINED:
7854 inner = s->info->ref;
7855 aliases = usymtab_allAliases (inner);
7856 ct = sRef_getType (inner);
7859 sRefSet_realElements (aliases, current)
7861 if (sRef_isValid (current))
7863 current = sRef_updateSref (current);
7865 if (ctype_equal (ct, sRef_getType (current)))
7867 sRef ptr = sRef_makePointer (current);
7869 ((*predf)(ptr, loc));
7872 } end_sRefSet_realElements;
7874 sRefSet_free (aliases);
7877 inner = s->info->arrayfetch->arr;
7878 aliases = usymtab_allAliases (inner);
7879 ct = sRef_getType (inner);
7881 sRefSet_realElements (aliases, current)
7883 if (sRef_isValid (current))
7885 current = sRef_updateSref (current);
7887 if (ctype_equal (ct, sRef_getType (current)))
7890 if (s->info->arrayfetch->indknown)
7892 sRef af = sRef_makeArrayFetchKnown (current, s->info->arrayfetch->ind);
7894 ((*predf)(af, loc));
7898 sRef af = sRef_makeArrayFetch (current);
7900 ((*predf)(af, loc));
7904 } end_sRefSet_realElements;
7906 sRefSet_free (aliases);
7909 inner = s->info->field->rec;
7910 aliases = usymtab_allAliases (inner);
7911 ct = sRef_getType (inner);
7914 sRefSet_realElements (aliases, current)
7916 if (sRef_isValid (current))
7918 current = sRef_updateSref (current);
7920 if (ctype_equal (ct, sRef_getType (current)))
7922 sRef f = sRef_makeField (current, s->info->field->field);
7927 } end_sRefSet_realElements;
7929 sRefSet_free (aliases);
7932 sRef_innerAliasSetComplete (predf, s->info->conj->a, loc);
7933 sRef_innerAliasSetComplete (predf, s->info->conj->b, loc);
7950 sRef_innerAliasSetCompleteParam (void (predf) (sRef, sRef), sRef s, sRef t)
7956 if (!sRef_isValid (s)) return;
7960 ** Type equivalence checking is necessary --- there might be casting.
7967 case SK_UNCONSTRAINED:
7972 inner = s->info->ref;
7973 aliases = usymtab_allAliases (inner);
7974 ct = sRef_getType (inner);
7977 sRefSet_realElements (aliases, current)
7979 if (sRef_isValid (current))
7981 current = sRef_updateSref (current);
7983 if (ctype_equal (ct, sRef_getType (current)))
7985 sRef ptr = sRef_makePointer (current);
7990 } end_sRefSet_realElements;
7992 sRefSet_free (aliases);
7995 inner = s->info->arrayfetch->arr;
7996 aliases = usymtab_allAliases (inner);
7997 ct = sRef_getType (inner);
7999 sRefSet_realElements (aliases, current)
8001 if (sRef_isValid (current))
8003 current = sRef_updateSref (current);
8005 if (ctype_equal (ct, sRef_getType (current)))
8008 if (s->info->arrayfetch->indknown)
8010 sRef af = sRef_makeArrayFetchKnown (current, s->info->arrayfetch->ind);
8016 sRef af = sRef_makeArrayFetch (current);
8022 } end_sRefSet_realElements;
8024 sRefSet_free (aliases);
8027 inner = s->info->field->rec;
8028 aliases = usymtab_allAliases (inner);
8029 ct = sRef_getType (inner);
8032 sRefSet_realElements (aliases, current)
8034 if (sRef_isValid (current))
8036 current = sRef_updateSref (current);
8038 if (ctype_equal (ct, sRef_getType (current)))
8040 sRef f = sRef_makeField (current, s->info->field->field);
8045 } end_sRefSet_realElements;
8047 sRefSet_free (aliases);
8050 sRef_innerAliasSetCompleteParam (predf, s->info->conj->a, t);
8051 sRef_innerAliasSetCompleteParam (predf, s->info->conj->b, t);
8067 static void sRef_combineExKinds (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other)
8069 exkind a1 = sRef_getExKind (res);
8070 exkind a2 = sRef_getExKind (other);
8072 if (a1 == a2 || a2 == XO_UNKNOWN)
8076 else if (a1 == XO_UNKNOWN)
8078 res->expinfo = alinfo_update (res->expinfo, other->expinfo);
8083 res->expkind = XO_OBSERVER;
8088 ** Currently, this is a very ad hoc implementation, with lots of fixes to
8089 ** make real code work okay. I need to come up with some more general
8090 ** rules or principles here.
8094 sRef_combineAliasKindsError (/*@notnull@*/ sRef res,
8095 /*@notnull@*/ sRef other,
8096 clause cl, fileloc loc)
8098 bool hasError = FALSE;
8099 alkind ares = sRef_getAliasKind (res);
8100 alkind aother = sRef_getAliasKind (other);
8102 if (alkind_isDependent (ares))
8104 if (aother == AK_KEPT)
8106 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8107 res->aliaskind = AK_KEPT;
8111 if (aother == AK_LOCAL || aother == AK_STATIC
8112 || alkind_isTemp (aother))
8114 res->aliaskind = AK_DEPENDENT;
8118 else if (alkind_isDependent (aother))
8120 if (ares == AK_KEPT)
8122 res->aliaskind = AK_KEPT;
8126 if (ares == AK_LOCAL || ares == AK_STATIC || alkind_isTemp (ares))
8128 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8129 res->aliaskind = AK_DEPENDENT;
8133 else if ((ares == AK_LOCAL || ares == AK_UNIQUE
8134 || ares == AK_STATIC || alkind_isTemp (ares))
8135 && sRef_isFresh (other))
8138 ** cases like: if (s == NULL) s = malloc...;
8139 ** don't generate errors
8142 if (usymtab_isAltProbablyDeepNull (res))
8144 res->aliaskind = ares;
8151 else if ((aother == AK_LOCAL || aother == AK_UNIQUE
8152 || aother == AK_STATIC || alkind_isTemp (aother))
8153 && sRef_isFresh (res))
8156 ** cases like: if (s == NULL) s = malloc...;
8157 ** don't generate errors
8160 if (usymtab_isProbableDeepNull (other))
8162 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8163 res->aliaskind = aother;
8170 else if (ares == AK_NEWREF && aother == AK_REFCOUNTED
8171 && sRef_isConst (other))
8173 res->aliaskind = AK_NEWREF;
8175 else if (aother == AK_NEWREF && ares == AK_REFCOUNTED
8176 && sRef_isConst (res))
8178 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8179 res->aliaskind = AK_NEWREF;
8181 else if (sRef_isLocalVar (res)
8182 && ((ares == AK_KEPT && aother == AK_LOCAL)
8183 || (aother == AK_KEPT && ares == AK_LOCAL)))
8185 res->aliaskind = AK_KEPT;
8194 if (sRef_isThroughArrayFetch (res))
8197 (FLG_BRANCHSTATE, FLG_STRICTBRANCHSTATE,
8199 ("Clauses exit with %q possibly referencing %s storage %s, "
8202 alkind_unparse (aother),
8203 clause_nameTaken (cl),
8204 alkind_unparse (ares),
8205 clause_nameAlternate (cl)),
8208 sRef_showAliasInfo (res);
8209 sRef_showAliasInfo (other);
8210 res->aliaskind = AK_ERROR;
8214 if (ares == AK_KEPT || aother == AK_KEPT)
8216 sRef_maybeKill (res, loc);
8224 message ("Clauses exit with %q referencing %s storage %s, "
8227 alkind_unparse (aother),
8228 clause_nameTaken (cl),
8229 alkind_unparse (ares),
8230 clause_nameAlternate (cl)),
8233 sRef_showAliasInfo (res);
8234 sRef_showAliasInfo (other);
8236 res->aliaskind = AK_ERROR;
8240 res->aliaskind = (sRef_isLocalVar (res) ? AK_LOCAL : AK_UNKNOWN);
8245 sRef_combineAliasKinds (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other,
8246 clause cl, fileloc loc)
8248 alkind ares = sRef_getAliasKind (res);
8249 alkind aother = sRef_getAliasKind (other);
8251 if (alkind_equal (ares, aother)
8252 || aother == AK_UNKNOWN
8253 || aother == AK_ERROR)
8255 ; /* keep current state */
8257 else if (sRef_isDead (res) || sRef_isDead (other))
8259 /* dead error reported (or storage is dead) */
8260 res ->aliaskind = AK_ERROR;
8262 else if (ares == AK_UNKNOWN || ares == AK_ERROR
8263 || sRef_isStateUndefined (res))
8265 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8266 res->aliaskind = aother;
8268 else if (sRef_isStateUndefined (other))
8272 else if (((ares == AK_UNIQUE || alkind_isTemp (ares))
8273 && aother == AK_LOCAL)
8274 || ((aother == AK_UNIQUE || alkind_isTemp (aother))
8275 && ares == AK_LOCAL))
8277 if (ares != AK_LOCAL)
8279 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8282 res->aliaskind = AK_LOCAL;
8284 else if ((ares == AK_OWNED && aother == AK_FRESH)
8285 || (aother == AK_OWNED && ares == AK_FRESH))
8287 if (ares != AK_FRESH)
8289 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8292 res->aliaskind = AK_FRESH;
8294 else if ((ares == AK_KEEP && aother == AK_FRESH) ||
8295 (aother == AK_KEEP && ares == AK_FRESH))
8297 if (ares != AK_KEEP)
8299 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8302 res->aliaskind = AK_KEEP;
8304 else if ((ares == AK_LOCAL && aother == AK_STACK) ||
8305 (aother == AK_LOCAL && ares == AK_STACK))
8307 if (ares != AK_STACK)
8309 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8312 res->aliaskind = AK_STACK;
8314 else if ((ares == AK_LOCAL
8315 && (aother == AK_OWNED && sRef_isLocalVar (other)))
8316 || (aother == AK_LOCAL
8317 && (ares == AK_OWNED && sRef_isLocalVar (res))))
8319 if (ares != AK_LOCAL)
8321 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8324 res->aliaskind = AK_LOCAL;
8326 else if ((ares == AK_FRESH && alkind_isOnly (aother))
8327 || (aother == AK_FRESH && alkind_isOnly (ares)))
8329 res->aliaskind = AK_FRESH;
8331 else if ((aother == AK_FRESH && sRef_definitelyNull (res))
8332 || (ares == AK_FRESH && sRef_definitelyNull (other)))
8334 if (ares != AK_FRESH)
8336 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8337 res->aliaskind = AK_FRESH;
8340 else if ((sRef_isFresh (res) && sRef_isConst (other))
8341 || (sRef_isFresh (other) && sRef_isConst (res)))
8344 ** for NULL constantants
8348 if (!sRef_isFresh (res))
8350 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8353 res->aliaskind = AK_FRESH;
8355 else if ((alkind_isStatic (aother) && sRef_isConst (res))
8356 || (alkind_isStatic (ares) && sRef_isConst (other)))
8358 if (!alkind_isStatic (ares))
8360 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8361 res->aliaskind = AK_STATIC;
8366 sRef_combineAliasKindsError (res, other, cl, loc);
8370 static void sRef_combineDefState (/*@notnull@*/ sRef res,
8371 /*@notnull@*/ sRef other)
8373 sstate s1 = res->defstate;
8374 sstate s2 = other->defstate;
8377 if (s1 == s2 || s2 == SS_UNKNOWN)
8381 else if (s1 == SS_UNKNOWN)
8390 if (s2 == SS_DEFINED)
8396 llcontbuglit ("ssfixed: not implemented");
8405 flip = (s2 != SS_DEFINED);
8423 res->definfo = alinfo_update (res->definfo, other->definfo);
8428 extern /*@notnull@*/ sRef sRef_getConjA (sRef s)
8431 llassert (sRef_isConj (s));
8433 ret = s->info->conj->a;
8434 llassert (ret != NULL);
8438 extern /*@notnull@*/ sRef sRef_getConjB (sRef s)
8441 llassert (sRef_isConj (s));
8443 ret = s->info->conj->b;
8444 llassert (ret != NULL);
8448 extern /*@exposed@*/ sRef sRef_makeArrow (sRef s, /*@dependent@*/ cstring f)
8453 p = sRef_makePointer (s);
8454 ret = sRef_makeField (p, f);
8458 extern /*@exposed@*/ sRef sRef_buildArrow (sRef s, cstring f)
8463 p = sRef_buildPointer (s);
8464 ret = sRef_buildField (p, f);
8469 static /*@null@*/ sinfo sinfo_copy (/*@notnull@*/ sRef s)
8476 ret = (sinfo) dmalloc (sizeof (*ret));
8477 ret->cvar = (cref) dmalloc (sizeof (*ret->cvar));
8478 ret->cvar->lexlevel = s->info->cvar->lexlevel;
8479 ret->cvar->index = s->info->cvar->index;
8483 ret = (sinfo) dmalloc (sizeof (*ret));
8484 ret->paramno = s->info->paramno;
8488 ret = (sinfo) dmalloc (sizeof (*ret));
8489 ret->arrayfetch = (ainfo) dmalloc (sizeof (*ret->arrayfetch));
8490 ret->arrayfetch->indknown = s->info->arrayfetch->indknown;
8491 ret->arrayfetch->ind = s->info->arrayfetch->ind;
8492 ret->arrayfetch->arr = s->info->arrayfetch->arr;
8496 ret = (sinfo) dmalloc (sizeof (*ret));
8497 ret->field = (fldinfo) dmalloc (sizeof (*ret->field));
8498 ret->field->rec = s->info->field->rec;
8499 ret->field->field = s->info->field->field;
8503 ret = (sinfo) dmalloc (sizeof (*ret));
8504 ret->object = s->info->object;
8511 ret = (sinfo) dmalloc (sizeof (*ret));
8512 ret->ref = s->info->ref;
8516 ret = (sinfo) dmalloc (sizeof (*ret));
8517 ret->conj = (cjinfo) dmalloc (sizeof (*ret->conj));
8518 ret->conj->a = s->info->conj->a;
8519 ret->conj->b = s->info->conj->b;
8522 ret = (sinfo) dmalloc (sizeof (*ret));
8523 ret->spec = s->info->spec;
8525 case SK_UNCONSTRAINED:
8527 ret = (sinfo) dmalloc (sizeof (*ret));
8528 ret->fname = s->info->fname;
8534 llassertprint (s->info == NULL, ("s = %s", sRef_unparse (s)));
8542 static /*@null@*/ sinfo sinfo_fullCopy (/*@notnull@*/ sRef s)
8547 ** Since its a full copy, only storage is assigned
8548 ** to dependent fields.
8555 ret = (sinfo) dmalloc (sizeof (*ret));
8556 ret->cvar = (cref) dmalloc (sizeof (*ret->cvar));
8557 ret->cvar->lexlevel = s->info->cvar->lexlevel;
8558 ret->cvar->index = s->info->cvar->index;
8562 ret = (sinfo) dmalloc (sizeof (*ret));
8563 ret->paramno = s->info->paramno;
8567 ret = (sinfo) dmalloc (sizeof (*ret));
8568 ret->arrayfetch = (ainfo) dmalloc (sizeof (*ret->arrayfetch));
8569 ret->arrayfetch->indknown = s->info->arrayfetch->indknown;
8570 ret->arrayfetch->ind = s->info->arrayfetch->ind;
8571 ret->arrayfetch->arr = sRef_saveCopy (s->info->arrayfetch->arr);
8575 ret = (sinfo) dmalloc (sizeof (*ret));
8576 ret->field = (fldinfo) dmalloc (sizeof (*ret->field));
8577 ret->field->rec = sRef_saveCopy (s->info->field->rec);
8578 ret->field->field = s->info->field->field;
8582 ret = (sinfo) dmalloc (sizeof (*ret));
8583 ret->object = s->info->object;
8590 ret = (sinfo) dmalloc (sizeof (*ret));
8591 ret->ref = sRef_saveCopy (s->info->ref);
8595 ret = (sinfo) dmalloc (sizeof (*ret));
8596 ret->conj = (cjinfo) dmalloc (sizeof (*ret->conj));
8597 ret->conj->a = sRef_saveCopy (s->info->conj->a);
8598 ret->conj->b = sRef_saveCopy (s->info->conj->b);
8601 ret = (sinfo) dmalloc (sizeof (*ret));
8602 ret->spec = s->info->spec;
8605 case SK_UNCONSTRAINED:
8606 ret = (sinfo) dmalloc (sizeof (*ret));
8607 ret->fname = s->info->fname;
8613 llassert (s->info == NULL);
8623 sinfo_update (/*@notnull@*/ /*@exposed@*/ sRef res,
8624 /*@notnull@*/ /*@exposed@*/ sRef other)
8626 llassert (res->kind == other->kind);
8631 res->info->cvar->lexlevel = other->info->cvar->lexlevel;
8632 res->info->cvar->index = other->info->cvar->index;
8636 res->info->paramno = other->info->paramno;
8640 res->info->arrayfetch->indknown = other->info->arrayfetch->indknown;
8641 res->info->arrayfetch->ind = other->info->arrayfetch->ind;
8642 res->info->arrayfetch->arr = other->info->arrayfetch->arr;
8646 res->info->field->rec = other->info->field->rec;
8647 res->info->field->field = other->info->field->field;
8651 res->info->object = other->info->object;
8658 res->info->ref = other->info->ref;
8662 res->info->conj->a = other->info->conj->a;
8663 res->info->conj->b = other->info->conj->b;
8667 res->info->spec = other->info->spec;
8671 case SK_UNCONSTRAINED:
8672 res->info->fname = other->info->fname;
8679 llassert (res->info == NULL);
8684 static void sinfo_free (/*@special@*/ /*@temp@*/ /*@notnull@*/ sRef s)
8685 /*@uses s->kind, s->info@*/
8686 /*@releases s->info@*/
8691 sfree (s->info->cvar);
8698 sfree (s->info->arrayfetch);
8702 sfree (s->info->field);
8715 sfree (s->info->conj);
8718 case SK_UNCONSTRAINED:
8731 bool sRef_isNSLocalVar (sRef s)
8733 if (sRef_isLocalVar (s))
8735 uentry ue = sRef_getUentry (s);
8737 return (!uentry_isStatic (ue));
8745 bool sRef_isLocalVar (sRef s)
8747 if (sRef_isValid(s))
8749 return (s->kind == SK_CVAR
8750 && (s->info->cvar->lexlevel > fileScope));
8756 bool sRef_isRealLocalVar (sRef s)
8758 if (sRef_isValid(s))
8760 if (s->kind == SK_CVAR)
8762 if (s->info->cvar->lexlevel == functionScope)
8764 uentry ue = sRef_getUentry (s);
8766 if (uentry_isAnyParam (ue)
8767 || uentry_isRefParam (ue))
8778 return (s->info->cvar->lexlevel > functionScope);
8786 bool sRef_isLocalParamVar (sRef s)
8788 if (sRef_isValid(s))
8790 return (s->kind == SK_PARAM
8791 || (s->kind == SK_CVAR
8792 && (s->info->cvar->lexlevel > fileScope)));
8798 static speckind speckind_fromInt (int i)
8801 llassert (i >= SR_NOTHING && i <= SR_SYSTEM);
8804 return ((speckind) i);
8807 void sRef_combineNullState (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other)
8809 nstate n1 = res->nullstate;
8810 nstate n2 = other->nullstate;
8814 if (n1 == n2 || n2 == NS_UNKNOWN)
8820 /* note: n2 is not unknown or defnull */
8824 case NS_ERROR: nn = NS_ERROR; break;
8825 case NS_UNKNOWN: flip = TRUE; nn = n2; break;
8826 case NS_POSNULL: break;
8827 case NS_DEFNULL: nn = NS_POSNULL; break;
8828 case NS_RELNULL: break;
8830 if (n2 == NS_MNOTNULL)
8841 if (n2 == NS_NOTNULL)
8860 res->nullinfo = alinfo_update (res->nullinfo, other->nullinfo);
8863 res->nullstate = nn;
8866 cstring sRef_nullMessage (sRef s)
8868 llassert (sRef_isValid (s));
8870 switch (s->nullstate)
8874 return (cstring_makeLiteralTemp ("null"));
8876 return (cstring_makeLiteralTemp ("possibly null"));
8881 /*@observer@*/ cstring sRef_ntMessage (sRef s)
8883 llassert (sRef_isValid (s));
8885 switch (s->nullstate)
8889 return (cstring_makeLiteralTemp ("not nullterminated"));
8891 return (cstring_makeLiteralTemp ("possibly non-nullterminated"));
8898 sRef sRef_fixResultType (/*@returned@*/ sRef s, ctype typ, uentry ue)
8900 sRef tmp = sRef_undefined;
8903 llassert (sRef_isValid (s));
8914 tmp = sRef_fixResultType (s->info->arrayfetch->arr, typ, ue);
8916 ct = ctype_realType (sRef_getType (tmp));
8919 if (ctype_isKnown (ct))
8921 if (ctype_isAP (ct))
8930 ("Special clause indexes non-array (%t): *%q",
8931 ct, sRef_unparse (s->info->arrayfetch->arr)),
8932 uentry_whereLast (ue));
8936 tmp = sRef_fixResultType (s->info->arrayfetch->arr, typ, ue);
8938 if (s->info->arrayfetch->indknown)
8940 ret = sRef_makeArrayFetchKnown (tmp, s->info->arrayfetch->ind);
8944 ret = sRef_makeArrayFetch (tmp);
8950 sRef rec = sRef_fixResultType (s->info->field->rec, typ, ue);
8951 ctype ct = ctype_realType (sRef_getType (rec));
8953 if (ctype_isKnown (ct))
8955 if (ctype_isSU (ct))
8957 if (uentry_isValid (uentryList_lookupField (ctype_getFields (ct),
8958 s->info->field->field)))
8967 ("Special clause accesses non-existent field of result: %q.%s",
8968 sRef_unparse (rec), s->info->field->field),
8969 uentry_whereLast (ue));
8977 ("Special clause accesses field of non-struct or union result (%t): %q.%s",
8978 ct, sRef_unparse (rec), s->info->field->field),
8979 uentry_whereLast (ue));
8983 ret = sRef_makeField (tmp, s->info->field->field);
8989 tmp = sRef_fixResultType (s->info->ref, typ, ue);
8991 ct = ctype_realType (sRef_getType (tmp));
8993 if (ctype_isKnown (ct))
8995 if (ctype_isAP (ct))
9004 ("Special clause dereferences non-pointer (%t): *%q",
9005 ct, sRef_unparse (s->info->ref)),
9006 uentry_whereLast (ue));
9010 ret = sRef_makePointer (tmp);
9017 ("Special clause uses & operator (not allowed): &%q", sRef_unparse (s->info->ref)),
9018 uentry_whereLast (ue));
9027 bool sRef_isOnly (sRef s)
9029 return (sRef_isValid(s) && alkind_isOnly (s->aliaskind));
9032 bool sRef_isDependent (sRef s)
9034 return (sRef_isValid(s) && alkind_isDependent (s->aliaskind));
9037 bool sRef_isOwned (sRef s)
9039 return (sRef_isValid (s) && (s->aliaskind == AK_OWNED));
9042 bool sRef_isKeep (sRef s)
9044 return (sRef_isValid (s) && (s->aliaskind == AK_KEEP));
9047 bool sRef_isTemp (sRef s)
9049 return (sRef_isValid (s) && alkind_isTemp (s->aliaskind));
9052 bool sRef_isLocalState (sRef s)
9054 return (sRef_isValid (s) && (s->aliaskind == AK_LOCAL));
9057 bool sRef_isUnique (sRef s)
9059 return (sRef_isValid (s) && (s->aliaskind == AK_UNIQUE));
9062 bool sRef_isShared (sRef s)
9064 return (sRef_isValid (s) && (s->aliaskind == AK_SHARED));
9067 bool sRef_isExposed (sRef s)
9069 return (sRef_isValid (s) && (s->expkind == XO_EXPOSED));
9072 bool sRef_isObserver (sRef s)
9074 return (sRef_isValid (s) && (s->expkind == XO_OBSERVER));
9077 bool sRef_isFresh (sRef s)
9079 return (sRef_isValid (s) && (s->aliaskind == AK_FRESH));
9082 bool sRef_isDefinitelyNull (sRef s)
9084 return (sRef_isValid (s) && (s->nullstate == NS_DEFNULL
9085 || s->nullstate == NS_CONSTNULL));
9088 bool sRef_isAllocated (sRef s)
9090 return (sRef_isValid (s) && (s->defstate == SS_ALLOCATED));
9093 bool sRef_isStack (sRef s)
9095 return (sRef_isValid (s) && (s->aliaskind == AK_STACK));
9098 extern bool sRef_isNotNull (sRef s)
9100 return (sRef_isValid(s) && (s->nullstate == NS_MNOTNULL
9101 || s->nullstate == NS_NOTNULL));
9104 /* start modifications */
9105 struct _bbufinfo sRef_getNullTerminatedState (sRef p_s) {
9106 struct _bbufinfo BUFSTATE_UNKNOWN;
9107 BUFSTATE_UNKNOWN.bufstate = BB_NOTNULLTERMINATED;
9108 BUFSTATE_UNKNOWN.size = 0;
9109 BUFSTATE_UNKNOWN.len = 0;
9111 if (sRef_isValid(p_s))
9112 return p_s->bufinfo;
9113 return BUFSTATE_UNKNOWN;
9116 void sRef_setNullTerminatedState(sRef p_s) {
9117 if(sRef_isValid (p_s)) {
9118 p_s->bufinfo.bufstate = BB_NULLTERMINATED;
9120 llfatalbug( message("sRef_setNT passed a invalid sRef\n"));
9125 void sRef_setPossiblyNullTerminatedState(sRef p_s) {
9126 if( sRef_isValid (p_s)) {
9127 p_s->bufinfo.bufstate = BB_POSSIBLYNULLTERMINATED;
9129 llfatalbug( message("sRef_setPossNT passed a invalid sRef\n"));
9133 void sRef_setNotNullTerminatedState(sRef p_s) {
9134 if( sRef_isValid (p_s)) {
9135 p_s->bufinfo.bufstate = BB_NOTNULLTERMINATED;
9137 llfatalbug( message("sRef_unsetNT passed a invalid sRef\n"));
9141 void sRef_setLen(sRef p_s, int len) {
9142 if( sRef_isValid (p_s) && sRef_isNullTerminated(p_s)) {
9143 p_s->bufinfo.len = len;
9145 llfatalbug( message("sRef_setLen passed a invalid sRef\n"));
9150 void sRef_setSize(sRef p_s, int size) {
9151 if( sRef_isValid(p_s)) {
9152 p_s->bufinfo.size = size;
9154 llfatalbug( message("sRef_setSize passed a invalid sRef\n"));
9158 void sRef_resetLen(sRef p_s) {
9159 if (sRef_isValid (p_s)) {
9160 p_s->bufinfo.len = 0;
9162 llfatalbug (message ("sRef_setLen passed an invalid sRef\n"));
9166 /*drl7x 11/28/2000 */
9168 bool sRef_isFixedArray (sRef p_s) /*@*/ {
9170 c = sRef_getType (p_s);
9171 return ( ctype_isFixedArray (c) );
9174 long int sRef_getArraySize (sRef p_s) /*@*/ {
9176 llassert (sRef_isFixedArray(p_s) );
9177 DPRINTF (( message ("sRef_getArraySize getting array size for %s", sRef_unparse(p_s) ) ));
9179 c = sRef_getType (p_s);
9181 return (ctype_getArraySize (c) );