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);
2043 sRef_fixBaseParam (/*@returned@*/ sRef s, exprNodeList args)
2045 if (sRef_isInvalid (s)) return (sRef_undefined);
2049 case SK_UNCONSTRAINED:
2054 if (exprNodeList_size (args) > s->info->paramno)
2056 exprNode e = exprNodeList_nth (args, s->info->paramno);
2058 if (exprNode_isError (e))
2060 return sRef_makeUnknown ();
2063 return (exprNode_getSref (e));
2067 return sRef_makeUnknown ();
2072 if (s->info->arrayfetch->indknown)
2074 return (sRef_makeArrayFetchKnown
2075 (sRef_fixBaseParam (s->info->arrayfetch->arr, args),
2076 s->info->arrayfetch->ind));
2080 return (sRef_makeArrayFetch
2081 (sRef_fixBaseParam (s->info->arrayfetch->arr, args)));
2084 return (sRef_makeField (sRef_fixBaseParam (s->info->field->rec, args),
2085 s->info->field->field));
2088 return (sRef_makePointer (sRef_fixBaseParam (s->info->ref, args)));
2091 return (sRef_makeAddress (sRef_fixBaseParam (s->info->ref, args)));
2094 return (sRef_makeConj (sRef_fixBaseParam (s->info->conj->a, args),
2095 sRef_fixBaseParam (s->info->conj->b, args)));
2111 sRef_undumpGlobal (char **c)
2121 usymId uid = usymId_fromInt (getInt (c));
2127 defstate = sstate_fromInt (getInt (c));
2130 nullstate = nstate_fromInt (getInt (c));
2132 ret = sRef_makeGlobal (uid, ctype_unknown);
2133 ret->nullstate = nullstate;
2134 ret->defstate = defstate;
2140 speckind sk = speckind_fromInt (i);
2144 case SR_NOTHING: return (sRef_makeNothing ());
2145 case SR_INTERNAL: return (sRef_makeInternalState ());
2146 case SR_SPECSTATE: return (sRef_makeSpecState ());
2147 case SR_SYSTEM: return (sRef_makeSystemState ());
2152 return sRef_undefined;
2154 return sRef_makeUnknown ();
2156 return sRef_makeUnknown ();
2158 llfatalerror (message ("sRef_undumpGlobal: bad line: %s",
2159 cstring_fromChars (*c)));
2165 sRef_undump (char **c)
2174 return (sRef_makeGlobal (usymId_fromInt (getInt (c)), ctype_unknown));
2176 return (sRef_makeParam (getInt (c), ctype_unknown));
2178 return (sRef_makeResultType (ctype_undump (c)));
2181 if ((**c >= '0' && **c <= '9') || **c == '-')
2184 sRef arr = sRef_undump (c);
2185 sRef ret = sRef_buildArrayFetchKnown (arr, i);
2191 sRef arr = sRef_undump (c);
2192 sRef ret = sRef_buildArrayFetch (arr);
2199 cstring fname = cstring_undefined;
2204 fname = cstring_appendChar (fname, **c);
2209 ret = sRef_buildField (sRef_undump (c), fname);
2210 cstring_markOwned (fname);
2216 speckind sk = speckind_fromInt (i);
2220 case SR_NOTHING: return (sRef_makeNothing ());
2221 case SR_INTERNAL: return (sRef_makeInternalState ());
2222 case SR_SPECSTATE: return (sRef_makeSpecState ());
2223 case SR_SYSTEM: return (sRef_makeSystemState ());
2229 sRef ptr = sRef_undump (c);
2230 sRef ret = sRef_makePointer (ptr);
2236 sRef adr = sRef_undump (c);
2237 sRef ret = sRef_makeAddress (adr);
2243 return (sRef_makeObject (ctype_undump (c)));
2247 sRef s1 = sRef_undump (c);
2248 sRef s2 = ((*c)++, sRef_undump (c));
2249 sRef ret = sRef_makeConj (s1, s2);
2254 return sRef_undefined;
2256 return sRef_makeUnknown ();
2258 return sRef_makeUnknown ();
2260 llfatalerror (message ("sRef_undump: bad line: %s", cstring_fromChars (*c)));
2268 if (sRef_isInvalid (s))
2270 return (cstring_makeLiteral ("-"));
2277 return (message ("p%d", s->info->paramno));
2279 if (s->info->arrayfetch->indknown)
2281 return (message ("a%d%q", s->info->arrayfetch->ind,
2282 sRef_dump (s->info->arrayfetch->arr)));
2286 return (message ("a%q", sRef_dump (s->info->arrayfetch->arr)));
2289 return (message ("f%s.%q", s->info->field->field,
2290 sRef_dump (s->info->field->rec)));
2292 return (message ("t%q", sRef_dump (s->info->ref)));
2294 return (message ("d%q", sRef_dump (s->info->ref)));
2296 return (message ("o%q", ctype_dump (s->info->object)));
2298 return (message ("s%d", (int) s->info->spec));
2300 return (message ("c%q.%q",
2301 sRef_dump (s->info->conj->a),
2302 sRef_dump (s->info->conj->b)));
2304 if (sRef_isGlobal (s))
2306 return (message ("g%d",
2307 usymtab_convertId (s->info->cvar->index)));
2311 llcontbug (message ("Dumping local variable: %q",
2312 sRef_unparseDebug (s)));
2313 return (cstring_makeLiteral ("u"));
2316 return (cstring_makeLiteral ("u"));
2318 return (message ("r%q", ctype_dump (s->type)));
2324 case SK_UNCONSTRAINED:
2325 llcontbug (message ("sRef_dump: bad kind: %q",
2326 sRef_unparseFull (s)));
2327 return (cstring_makeLiteral ("x"));
2334 cstring sRef_dumpGlobal (sRef s)
2336 if (sRef_isInvalid (s))
2338 return (cstring_makeLiteral ("-"));
2345 if (sRef_isGlobal (s))
2347 return (message ("g%d@%d@%d",
2348 usymtab_convertId (s->info->cvar->index),
2350 (int) s->nullstate));
2354 llcontbug (message ("Dumping local variable: %q",
2355 sRef_unparseDebug (s)));
2356 return (cstring_makeLiteral ("u"));
2359 return (cstring_makeLiteral ("u"));
2361 return (message ("s%d", (int) s->info->spec));
2363 llcontbug (message ("sRef_dumpGlobal: bad kind: %q",
2364 sRef_unparseFull (s)));
2365 return (cstring_makeLiteral ("x"));
2373 sRef_deriveType (sRef s, uentryList cl)
2375 if (sRef_isInvalid (s)) return ctype_unknown;
2380 return (uentry_getType (usymtab_getRefQuiet (s->info->cvar->lexlevel,
2381 s->info->cvar->index)));
2382 case SK_UNCONSTRAINED:
2383 return (ctype_unknown);
2385 return uentry_getType (uentryList_getN (cl, s->info->paramno));
2388 ctype ca = sRef_deriveType (s->info->arrayfetch->arr, cl);
2390 if (ctype_isArray (ca))
2392 return (ctype_baseArrayPtr (ca));
2394 else if (ctype_isUnknown (ca))
2400 llcontbuglit ("sRef_deriveType: inconsistent array type");
2406 ctype ct = sRef_deriveType (s->info->field->rec, cl);
2408 if (ctype_isStructorUnion (ct))
2410 uentry ue = uentryList_lookupField (ctype_getFields (ct),
2411 s->info->field->field);
2413 if (uentry_isValid (ue))
2415 return (uentry_getType (ue));
2419 llcontbuglit ("sRef_deriveType: bad field");
2420 return ctype_unknown;
2423 else if (ctype_isUnknown (ct))
2429 llcontbuglit ("sRef_deriveType: inconsistent field type");
2435 ctype ct = sRef_deriveType (s->info->ref, cl);
2437 if (ctype_isUnknown (ct)) return ct;
2438 if (ctype_isPointer (ct)) return (ctype_baseArrayPtr (ct));
2441 llcontbuglit ("sRef_deriveType: inconsistent pointer type");
2447 ctype ct = sRef_deriveType (s->info->ref, cl);
2449 if (ctype_isUnknown (ct)) return ct;
2450 return ctype_makePointer (ct);
2454 return sRef_deriveType (s->info->ref, cl);
2458 return (s->info->object);
2462 return (ctype_makeConj (sRef_deriveType (s->info->conj->a, cl),
2463 sRef_deriveType (s->info->conj->b, cl)));
2475 return ctype_unknown;
2481 sRef_getType (sRef s)
2483 if (sRef_isInvalid (s)) return ctype_unknown;
2489 sRef_unparseOpt (sRef s)
2491 sRef rb = sRef_getRootBase (s);
2493 if (sRef_isMeaningful (rb) && !sRef_isConst (rb))
2495 cstring ret = sRef_unparse (s);
2497 llassertprint (!cstring_equalLit (ret, "?"), ("print: %s", sRef_unparseDebug (s)));
2499 if (!cstring_isEmpty (ret))
2501 return (cstring_appendChar (ret, ' '));
2509 return cstring_undefined;
2513 sRef_unparsePreOpt (sRef s)
2515 sRef rb = sRef_getRootBase (s);
2517 if (sRef_isMeaningful (rb) && !sRef_isConst (rb))
2519 cstring ret = sRef_unparse (s);
2521 llassertprint (!cstring_equalLit (ret, "?"), ("print: %s", sRef_unparseDebug (s)));
2522 return (cstring_prependCharO (' ', ret));
2525 return cstring_undefined;
2529 sRef_unparse (sRef s)
2531 if (sRef_isInvalid (s)) return (cstring_makeLiteral ("?"));
2533 if (context_inFunctionLike ())
2535 return (sRef_unparseWithArgs (s, context_getParams ()));
2539 return (sRef_unparseNoArgs (s));
2543 static /*@only@*/ cstring
2544 sRef_unparseWithArgs (sRef s, uentryList args)
2546 if (sRef_isInvalid (s))
2548 return (cstring_makeLiteral ("?"));
2554 return (uentry_getName (usymtab_getRefQuiet (s->info->cvar->lexlevel,
2555 s->info->cvar->index)));
2556 case SK_UNCONSTRAINED:
2557 return (cstring_copy (s->info->fname));
2560 if (s->info->paramno < uentryList_size (args))
2562 uentry ue = uentryList_getN (args, s->info->paramno);
2564 if (uentry_isValid (ue))
2565 return uentry_getName (ue);
2568 return (message ("<bad param: %q / args %q",
2569 sRef_unparseDebug (s),
2570 uentryList_unparse (args)));
2573 if (s->info->arrayfetch->indknown)
2575 return (message ("%q[%d]", sRef_unparseWithArgs (s->info->arrayfetch->arr, args),
2576 s->info->arrayfetch->ind));
2580 return (message ("%q[]", sRef_unparseWithArgs (s->info->arrayfetch->arr, args)));
2583 if (s->info->field->rec->kind == SK_PTR)
2585 sRef ptr = s->info->field->rec;
2587 return (message ("%q->%s", sRef_unparseWithArgs (ptr->info->ref, args),
2588 s->info->field->field));
2590 return (message ("%q.%s", sRef_unparseWithArgs (s->info->field->rec, args),
2591 s->info->field->field));
2595 sRef ref = sRef_fixConj (s->info->ref);
2596 skind sk = ref->kind;
2601 ret = message ("storage pointed to by %q",
2602 sRef_unparseWithArgs (ref, args));
2604 else if (skind_isSimple (sk) || sk == SK_PTR)
2606 ret = message ("*%q", sRef_unparseWithArgs (ref, args));
2610 ret = message ("*(%q)", sRef_unparseWithArgs (ref, args));
2616 return (message ("&%q", sRef_unparseWithArgs (s->info->ref, args)));
2618 return (cstring_copy (ctype_unparse (s->info->object)));
2620 return (sRef_unparseWithArgs (sRef_getConjA (s), args));
2622 if (cstring_isDefined (s->info->fname))
2624 return (message ("[result of %s]", s->info->fname));
2628 return (cstring_makeLiteral ("<new>"));
2631 return (cstring_makeLiteral ("?"));
2633 return (message ("<derived %q>", sRef_unparse (s->info->ref)));
2635 return (message ("<external %q>", sRef_unparse (s->info->ref)));
2637 return (message ("<type %s>", ctype_unparse (s->type)));
2639 return (message ("<const %s>", ctype_unparse (s->type)));
2641 return (cstring_makeLiteral
2642 (s->info->spec == SR_NOTHING ? "nothing"
2643 : s->info->spec == SR_INTERNAL ? "internal state"
2644 : s->info->spec == SR_SPECSTATE ? "spec state"
2645 : s->info->spec == SR_SYSTEM ? "file system state"
2648 return cstring_makeLiteral ("result");
2651 llbug (message ("Bad sref, kind = %d", (int) s->kind));
2659 sRef_unparseDebug (sRef s)
2661 if (sRef_isInvalid (s)) return (cstring_makeLiteral ("<undef>"));
2665 case SK_UNCONSTRAINED:
2666 return (message ("<unconstrained %s>", s->info->fname));
2671 ce = usymtab_getRefQuiet (s->info->cvar->lexlevel, s->info->cvar->index);
2673 if (uentry_isInvalid (ce))
2675 return (message ("<scope: %d.%d *invalid*>",
2676 s->info->cvar->lexlevel,
2677 s->info->cvar->index));
2681 return (message ("<scope: %d.%d *%q*>",
2682 s->info->cvar->lexlevel,
2683 s->info->cvar->index,
2684 uentry_getName (ce)));
2690 return (message ("<parameter %d>", s->info->paramno + 1));
2693 if (s->info->arrayfetch->indknown)
2695 return (message ("%q[%d]", sRef_unparseDebug (s->info->arrayfetch->arr),
2696 s->info->arrayfetch->ind));
2700 return (message ("%q[]", sRef_unparseDebug (s->info->arrayfetch->arr)));
2703 return (message ("%q.%s", sRef_unparseDebug (s->info->field->rec),
2704 s->info->field->field));
2706 return (message ("*(%q)", sRef_unparseDebug (s->info->ref)));
2708 return (message ("&%q", sRef_unparseDebug (s->info->ref)));
2710 return (message ("<object type %s>", ctype_unparse (s->info->object)));
2712 return (message ("%q | %q", sRef_unparseDebug (s->info->conj->a),
2713 sRef_unparseDebug (s->info->conj->b)));
2715 return message ("<new: %s>", s->info->fname);
2717 return (message ("<derived %q>", sRef_unparseDebug (s->info->ref)));
2719 return (message ("<external %q>", sRef_unparseDebug (s->info->ref)));
2721 return (message ("<type %s>", ctype_unparse (s->type)));
2723 return (message ("<const %s>", ctype_unparse (s->type)));
2725 return (message ("<result %s>", ctype_unparse (s->type)));
2727 return (message ("<spec %s>",
2728 cstring_makeLiteralTemp
2729 (s->info->spec == SR_NOTHING ? "nothing"
2730 : s->info->spec == SR_INTERNAL ? "internalState"
2731 : s->info->spec == SR_SPECSTATE ? "spec state"
2732 : s->info->spec == SR_SYSTEM ? "fileSystem"
2735 return cstring_makeLiteral ("<unknown>");
2741 static /*@only@*/ cstring
2742 sRef_unparseNoArgs (sRef s)
2744 if (sRef_isInvalid (s)) return (cstring_makeLiteral ("?"));
2748 case SK_UNCONSTRAINED:
2749 return (cstring_copy (s->info->fname));
2752 uentry ce = usymtab_getRefQuiet (s->info->cvar->lexlevel,
2753 s->info->cvar->index);
2755 if (uentry_isInvalid (ce))
2757 llcontbug (message ("sRef_unparseNoArgs: bad cvar: %q", sRef_unparseDebug (s)));
2758 return (sRef_unparseDebug (s));
2762 return (uentry_getName (ce));
2766 if (s->info->arrayfetch->indknown)
2768 return (message ("%q[%d]", sRef_unparseNoArgs (s->info->arrayfetch->arr),
2769 s->info->arrayfetch->ind));
2773 return (message ("%q[]", sRef_unparseNoArgs (s->info->arrayfetch->arr)));
2776 return (message ("%q.%s", sRef_unparseNoArgs (s->info->field->rec),
2777 s->info->field->field));
2780 sRef ref = sRef_fixConj (s->info->ref);
2781 skind sk = ref->kind;
2784 if (skind_isSimple (sk) || sk == SK_PTR)
2786 ret = message ("*%q", sRef_unparseNoArgs (ref));
2790 ret = message ("*(%q)", sRef_unparseNoArgs (ref));
2796 return (message ("&%q", sRef_unparseNoArgs (s->info->ref)));
2798 return (cstring_copy (ctype_unparse (s->info->object)));
2800 return (sRef_unparseNoArgs (s->info->conj->a));
2802 return (message ("result of %s", s->info->fname));
2804 return (message ("<der %q>", sRef_unparseNoArgs (s->info->ref)));
2806 return message ("<ext %q>", sRef_unparseNoArgs (s->info->ref));
2808 return (cstring_makeLiteral
2809 (s->info->spec == SR_NOTHING ? "nothing"
2810 : s->info->spec == SR_INTERNAL ? "internal state"
2811 : s->info->spec == SR_SPECSTATE ? "spec state"
2812 : s->info->spec == SR_SYSTEM ? "file system state"
2815 return cstring_makeLiteral ("result");
2819 return cstring_makeLiteral ("?");
2821 llcontbug (message ("sRef_unparseNoArgs: bad case: %q", sRef_unparseDebug (s)));
2822 return (sRef_unparseDebug (s));
2827 /*@dependent@*/ sRef sRef_makeUnconstrained (cstring fname)
2829 sRef s = sRef_new ();
2831 s->kind = SK_UNCONSTRAINED;
2832 s->info = (sinfo) dmalloc (sizeof (*s->info));
2833 s->info->fname = fname;
2838 cstring sRef_unconstrainedName (sRef s)
2840 llassert (sRef_isUnconstrained (s));
2842 return s->info->fname;
2845 bool sRef_isUnconstrained (sRef s)
2847 return (sRef_isValid(s) && s->kind == SK_UNCONSTRAINED);
2850 static /*@dependent@*/ /*@notnull@*/ sRef
2851 sRef_makeCvarAux (int level, usymId index, ctype ct)
2853 sRef s = sRef_new ();
2856 s->info = (sinfo) dmalloc (sizeof (*s->info));
2858 s->info->cvar = (cref) dmalloc (sizeof (*s->info->cvar));
2859 s->info->cvar->lexlevel = level;
2860 s->info->cvar->index = index;
2862 /* for now, all globals are defined; all locals, aren't */
2864 if (level <= fileScope)
2866 s->defstate = SS_UNKNOWN;
2870 ctype rct = ctype_realType (ct);
2872 if (level != paramsScope
2873 && (ctype_isStructorUnion (rct) || ctype_isRealArray (rct)))
2875 s->defstate = SS_ALLOCATED;
2876 s->oaliaskind = s->aliaskind = AK_STACK;
2880 s->defstate = SS_UNDEFINED;
2881 s->oaliaskind = s->aliaskind = AK_LOCAL;
2887 llassert (level >= globScope);
2888 llassert (usymId_isValid (index));
2893 /*@dependent@*/ sRef sRef_makeCvar (int level, usymId index, ctype ct)
2895 return (sRef_makeCvarAux (level, index, ct));
2898 int sRef_lexLevel (sRef s)
2900 if (sRef_isValid (s))
2904 conj = sRef_fixConj (s);
2905 s = sRef_getRootBase (conj);
2907 if (sRef_isValid (s) && s->kind == SK_CVAR)
2909 return (s->info->cvar->lexlevel);
2917 sRef_makeGlobal (usymId l, ctype ct)
2919 return (sRef_makeCvar (globScope, l, ct));
2923 sRef_setParamNo (sRef s, int l)
2925 llassert (sRef_isValid (s) && s->kind == SK_PARAM);
2926 s->info->paramno = l;
2929 /*@dependent@*/ sRef
2930 sRef_makeParam (int l, ctype ct)
2932 sRef s = sRef_new ();
2937 s->info = (sinfo) dmalloc (sizeof (*s->info));
2938 s->info->paramno = l;
2939 s->defstate = SS_UNKNOWN; /* (probably defined, unless its an out parameter) */
2945 sRef_isIndexKnown (sRef arr)
2949 llassert (sRef_isValid (arr));
2950 arr = sRef_fixConj (arr);
2952 llassert (arr->kind == SK_ARRAYFETCH);
2953 res = arr->info->arrayfetch->indknown;
2958 sRef_getIndex (sRef arr)
2962 llassert (sRef_isValid (arr));
2963 arr = sRef_fixConj (arr);
2965 llassert (arr->kind == SK_ARRAYFETCH);
2967 if (!arr->info->arrayfetch->indknown)
2969 llcontbug (message ("sRef_getIndex: unknown: %q", sRef_unparse (arr)));
2974 result = arr->info->arrayfetch->ind;
2980 static bool sRef_isZerothArrayFetch (/*@notnull@*/ sRef s)
2982 return (s->kind == SK_ARRAYFETCH
2983 && s->info->arrayfetch->indknown
2984 && (s->info->arrayfetch->ind == 0));
2987 /*@exposed@*/ sRef sRef_makeAddress (/*@exposed@*/ sRef t)
2990 if (sRef_isInvalid (t)) return sRef_undefined;
2992 if (sRef_isPointer (t))
2994 return (t->info->ref);
2996 else if (sRef_isZerothArrayFetch (t))
2998 return (t->info->arrayfetch->arr);
3002 sRef s = sRef_new ();
3005 s->type = ctype_makePointer (t->type);
3006 s->info = (sinfo) dmalloc (sizeof (*s->info));
3009 if (t->defstate == SS_UNDEFINED)
3010 /* no! it is allocated even still: && !ctype_isPointer (t->type)) */
3012 s->defstate = SS_ALLOCATED;
3016 s->defstate = t->defstate;
3019 if (t->aliaskind == AK_LOCAL)
3021 if (sRef_isLocalVar (t))
3023 s->aliaskind = AK_STACK;
3031 cstring sRef_getField (sRef s)
3035 llassert (sRef_isValid (s));
3036 s = sRef_fixConj (s);
3038 llassertprint (sRef_isValid (s) && (s->kind == SK_FIELD),
3039 ("s = %s", sRef_unparseDebug (s)));
3041 res = s->info->field->field;
3045 sRef sRef_getBase (sRef s)
3049 if (sRef_isInvalid (s)) return (sRef_undefined);
3051 s = sRef_fixConj (s);
3062 res = s->info->field->rec;
3066 res = s->info->arrayfetch->arr;
3070 res = sRef_undefined; /* shouldn't need it */
3077 ** same as getBase, except returns invalid
3078 ** (and doesn't use adr's)
3082 sRef_getBaseSafe (sRef s)
3086 if (sRef_isInvalid (s)) { return sRef_undefined; }
3088 s = sRef_fixConj (s);
3096 res = s->info->field->rec; break;
3098 res = s->info->arrayfetch->arr;
3101 res = sRef_undefined; break;
3107 /*@constant int MAXBASEDEPTH;@*/
3108 # define MAXBASEDEPTH 25
3110 static /*@exposed@*/ sRef
3111 sRef_getRootBaseAux (sRef s, int depth)
3113 if (sRef_isInvalid (s)) return sRef_undefined;
3115 if (depth > MAXBASEDEPTH)
3118 ("Warning: reference base limit exceeded for %q. "
3119 "This either means there is a variable with at least "
3120 "%d indirections from this reference, or "
3121 "there is a bug in LCLint.",
3126 return sRef_undefined;
3133 return (sRef_getRootBaseAux (s->info->ref, depth + 1));
3135 return (sRef_getRootBaseAux (s->info->field->rec, depth + 1));
3137 return (sRef_getRootBaseAux (s->info->arrayfetch->arr, depth + 1));
3139 return (sRef_getRootBaseAux (sRef_fixConj (s), depth + 1));
3145 sRef sRef_getRootBase (sRef s)
3147 return (sRef_getRootBaseAux (s, 0));
3150 static bool sRef_isDeep (sRef s)
3152 if (sRef_isInvalid (s)) return FALSE;
3162 return (sRef_isDeep (sRef_fixConj (s)));
3168 static int sRef_depth (sRef s)
3170 if (sRef_isInvalid (s)) return 0;
3178 return 1 + sRef_depth (s->info->ref);
3180 return 1 + sRef_depth (s->info->field->rec);
3182 return 1 + sRef_depth (s->info->arrayfetch->arr);
3184 return (sRef_depth (sRef_fixConj (s)));
3191 sRef_makeObject (ctype o)
3193 sRef s = sRef_new ();
3195 s->kind = SK_OBJECT;
3196 s->info = (sinfo) dmalloc (sizeof (*s->info));
3197 s->info->object = o;
3201 sRef sRef_makeExternal (/*@exposed@*/ sRef t)
3203 sRef s = sRef_new ();
3205 llassert (sRef_isValid (t));
3207 s->kind = SK_EXTERNAL;
3208 s->info = (sinfo) dmalloc (sizeof (*s->info));
3214 sRef sRef_makeDerived (/*@exposed@*/ sRef t)
3216 if (sRef_isValid (t))
3218 sRef s = sRef_new ();
3220 s->kind = SK_DERIVED;
3221 s->info = (sinfo) dmalloc (sizeof (*s->info));
3229 return sRef_undefined;
3234 ** definitely NOT symmetric:
3236 ** res fills in unknown state information from other
3240 sRef_mergeStateQuiet (sRef res, sRef other)
3242 llassert (sRef_isValid (res));
3243 llassert (sRef_isValid (other));
3245 res->modified = res->modified || other->modified;
3246 res->safe = res->safe && other->safe;
3248 if (res->defstate == SS_UNKNOWN)
3250 res->defstate = other->defstate;
3251 res->definfo = alinfo_update (res->definfo, other->definfo);
3254 if (res->aliaskind == AK_UNKNOWN ||
3255 (res->aliaskind == AK_LOCAL && alkind_isKnown (other->aliaskind)))
3257 res->aliaskind = other->aliaskind;
3258 res->oaliaskind = other->oaliaskind;
3259 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
3262 if (res->expkind == XO_UNKNOWN)
3264 res->expkind = other->expkind;
3265 res->oexpkind = other->oexpkind;
3266 res->expinfo = alinfo_update (res->expinfo, other->expinfo);
3269 /* out takes precedence over implicitly defined */
3270 if (res->defstate == SS_DEFINED && other->defstate != SS_UNKNOWN)
3272 res->defstate = other->defstate;
3273 res->definfo = alinfo_update (res->definfo, other->definfo);
3276 if (other->nullstate == NS_ERROR || res->nullstate == NS_ERROR)
3278 res->nullstate = NS_ERROR;
3282 if (other->nullstate != NS_UNKNOWN
3283 && (res->nullstate == NS_UNKNOWN || res->nullstate == NS_NOTNULL
3284 || res->nullstate == NS_MNOTNULL))
3286 res->nullstate = other->nullstate;
3287 res->nullinfo = alinfo_update (res->nullinfo, other->nullinfo);
3293 ** definitely NOT symmetric:
3295 ** res fills in known state information from other
3299 sRef_mergeStateQuietReverse (sRef res, sRef other)
3301 bool changed = FALSE;
3303 llassert (sRef_isValid (res));
3304 llassert (sRef_isValid (other));
3306 if (res->kind != other->kind)
3312 res->kind = other->kind;
3313 res->type = other->type;
3314 res->info = sinfo_fullCopy (other);
3318 if (!ctype_equal (res->type, other->type))
3321 res->type = other->type;
3324 sinfo_update (res, other);
3327 res->modified = res->modified || other->modified;
3328 res->safe = res->safe && other->safe;
3330 if (res->aliaskind != other->aliaskind
3331 && (res->aliaskind == AK_UNKNOWN
3332 || ((res->aliaskind == AK_LOCAL
3333 || (res->aliaskind == AK_REFCOUNTED
3334 && other->aliaskind != AK_LOCAL))
3335 && other->aliaskind != AK_UNKNOWN)))
3338 res->aliaskind = other->aliaskind;
3339 res->oaliaskind = other->oaliaskind;
3340 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
3343 if (other->expkind != XO_UNKNOWN && other->expkind != res->expkind)
3346 res->expkind = other->expkind;
3347 res->expinfo = alinfo_update (res->expinfo, other->expinfo);
3350 if (other->oexpkind != XO_UNKNOWN)
3352 res->oexpkind = other->oexpkind;
3355 /* out takes precedence over implicitly defined */
3357 if (res->defstate != other->defstate)
3359 if (other->defstate != SS_UNKNOWN)
3361 res->defstate = other->defstate;
3365 if (other->nullstate == NS_ERROR || res->nullstate == NS_ERROR)
3367 if (res->nullstate != NS_ERROR)
3369 res->nullstate = NS_ERROR;
3375 if (other->nullstate != NS_UNKNOWN && other->nullstate != res->nullstate)
3378 res->nullstate = other->nullstate;
3379 res->nullinfo = alinfo_update (res->nullinfo, other->nullinfo);
3385 sRef_clearDerived (res);
3390 sRef_mergeState (sRef res, sRef other, clause cl, fileloc loc)
3392 if (sRef_isValid (res) && sRef_isValid (other))
3394 sRef_mergeStateAux (res, other, cl, FALSE, loc, TRUE);
3398 if (sRef_isInvalid (res))
3400 llbug (message ("sRef_mergeState: invalid res sRef: %q",
3401 sRef_unparseDebug (other)));
3405 llbug (message ("sRef_mergeState: invalid other sRef: %q",
3406 sRef_unparseDebug (res)));
3412 sRef_mergeOptState (sRef res, sRef other, clause cl, fileloc loc)
3414 if (sRef_isValid (res) && sRef_isValid (other))
3416 sRef_mergeStateAux (res, other, cl, TRUE, loc, TRUE);
3420 if (sRef_isInvalid (res))
3422 llbug (message ("sRef_mergeOptState: invalid res sRef: %q",
3423 sRef_unparseDebug (other)));
3427 llbug (message ("sRef_mergeOptState: invalid other sRef: %q",
3428 sRef_unparseDebug (res)));
3434 sRef_mergeStateAux (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other,
3435 clause cl, bool opt, fileloc loc,
3439 llassertfatal (sRef_isValid (res));
3440 llassertfatal (sRef_isValid (other));
3442 res->modified = res->modified || other->modified;
3444 if (res->kind == other->kind
3445 || (other->kind == SK_UNKNOWN || res->kind == SK_UNKNOWN))
3447 sstate odef = other->defstate;
3448 sstate rdef = res->defstate;
3449 nstate onull = other->nullstate;
3452 ** yucky stuff to handle
3457 if (other->defstate == SS_DEAD
3458 && ((sRef_isOnly (res) && sRef_definitelyNull (res))
3459 || (res->defstate == SS_UNDEFINED
3460 || res->defstate == SS_UNUSEABLE)))
3462 if (res->defstate == SS_UNDEFINED
3463 || res->defstate == SS_UNUSEABLE)
3465 res->defstate = SS_UNUSEABLE;
3469 res->defstate = SS_DEAD;
3472 res->definfo = alinfo_update (res->definfo, other->definfo);
3473 sRef_clearDerived (other);
3474 sRef_clearDerived (res);
3476 else if (res->defstate == SS_DEAD
3477 && ((sRef_isOnly (other) && sRef_definitelyNull (other))
3478 || (other->defstate == SS_UNDEFINED
3479 || other->defstate == SS_UNUSEABLE)))
3481 if (other->defstate == SS_UNDEFINED
3482 || other->defstate == SS_UNUSEABLE)
3484 res->defstate = SS_UNUSEABLE;
3488 res->defstate = SS_DEAD;
3491 sRef_clearDerived (other);
3492 sRef_clearDerived (res);
3494 else if (res->defstate == SS_DEFINED
3495 && (other->defstate == SS_ALLOCATED
3496 && sRef_definitelyNull (other)))
3498 other->defstate = SS_DEFINED; /* definitely null! */
3500 else if (other->defstate == SS_DEFINED
3501 && (res->defstate == SS_ALLOCATED && sRef_definitelyNull (res)))
3503 res->defstate = SS_DEFINED;
3504 res->definfo = alinfo_update (res->definfo, other->definfo);
3511 if (res->defstate == SS_DEAD && other->defstate == SS_DEAD)
3513 sRef_clearDerived (other);
3514 sRef_clearDerived (res);
3518 ** only & dead isn't really an only!
3521 if (alkind_isOnly (other->aliaskind) && other->defstate == SS_DEAD)
3523 other->aliaskind = AK_UNKNOWN;
3526 if (alkind_isOnly (res->aliaskind) && res->defstate == SS_DEAD)
3528 res->aliaskind = AK_UNKNOWN;
3532 ** Dead and dependent -> dead
3535 if (alkind_isDependent (other->aliaskind) && res->defstate == SS_DEAD)
3537 other->aliaskind = AK_UNKNOWN;
3538 other->defstate = SS_DEAD;
3539 sRef_clearDerived (res);
3540 sRef_clearDerived (other);
3543 if (alkind_isDependent (res->aliaskind) && other->defstate == SS_DEAD)
3545 res->aliaskind = AK_UNKNOWN;
3546 res->defstate = SS_DEAD;
3547 sRef_clearDerived (res);
3548 sRef_clearDerived (other);
3552 ** must do alias combine first, since it depends on
3553 ** original values of state and null.
3556 sRef_combineAliasKinds (res, other, cl, loc);
3557 sRef_combineDefState (res, other);
3558 sRef_combineNullState (res, other);
3560 if (rdef == SS_ALLOCATED || rdef == SS_SPECIAL)
3562 if (odef == SS_DEFINED)
3564 if (onull == NS_DEFNULL || onull == NS_CONSTNULL)
3566 res->deriv = sRefSet_copy (res->deriv, other->deriv);
3571 else if (odef == SS_ALLOCATED
3572 || odef == SS_SPECIAL)
3577 if (ctype_isUnion (ctype_realType (sRef_getType (res))))
3579 res->deriv = sRef_mergeUnionDerivs (res->deriv,
3585 res->deriv = sRef_mergeDerivs (res->deriv, other->deriv,
3594 res->deriv = sRef_mergeDerivs (res->deriv, other->deriv,
3604 if (rdef == SS_PDEFINED
3605 || (rdef == SS_DEFINED && odef == SS_PDEFINED))
3609 res->deriv = sRef_mergePdefinedDerivs (res->deriv, other->deriv,
3615 if ((rdef == SS_DEFINED || rdef == SS_UNKNOWN)
3616 && res->defstate == SS_ALLOCATED)
3618 res->deriv = sRefSet_copy (res->deriv, other->deriv);
3624 res->deriv = sRef_mergeDerivs (res->deriv, other->deriv,
3632 sRef_combineExKinds (res, other);
3636 if (res->kind == SK_ARRAYFETCH && other->kind == SK_PTR)
3638 sRef nother = sRef_buildArrayFetchKnown (sRef_getBase (other), 0);
3640 sRef_copyState (nother, other);
3641 sRef_mergeStateAux (res, nother, cl, opt, loc, doDerivs);
3643 else if (res->kind == SK_PTR && other->kind == SK_ARRAYFETCH)
3645 sRef nother = sRef_buildPointer (sRef_getBase (other));
3647 if (sRef_isValid (nother))
3649 sRef_copyState (nother, other);
3650 sRef_mergeStateAux (res, nother, cl, opt, loc, doDerivs);
3655 llcontbug (message ("merge conj: %q / %q", sRef_unparseFull (res),
3656 sRef_unparseFull (other)));
3664 sRef_mergeUnionDerivs (/*@only@*/ sRefSet res,
3665 /*@exposed@*/ sRefSet other, bool opt,
3666 clause cl, fileloc loc)
3668 if (sRefSet_isEmpty (res))
3670 return sRefSet_copy (res, other);
3674 sRefSet_allElements (other, el)
3676 if (sRef_isValid (el))
3678 sRef e2 = sRefSet_lookupMember (other, el);
3680 if (sRef_isValid (e2))
3682 sRef_mergeStateAux (el, e2, cl, opt, loc, FALSE);
3686 res = sRefSet_insert (res, el);
3689 } end_sRefSet_allElements ;
3695 static /*@only@*/ sRefSet
3696 sRef_mergeDerivs (/*@only@*/ sRefSet res, sRefSet other,
3697 bool opt, clause cl, fileloc loc)
3699 sRefSet ret = sRefSet_new ();
3702 sRefSet_allElements (res, el)
3704 if (sRef_isValid (el))
3706 sRef e2 = sRefSet_lookupMember (other, el);
3708 if (sRef_isValid (e2))
3710 if (el->defstate == SS_ALLOCATED &&
3711 e2->defstate == SS_PDEFINED)
3713 e2->defstate = SS_ALLOCATED;
3715 else if (e2->defstate == SS_ALLOCATED &&
3716 el->defstate == SS_PDEFINED)
3718 el->defstate = SS_ALLOCATED;
3719 sRef_clearDerived (el);
3721 else if ((el->defstate == SS_DEAD || sRef_isKept (el)) &&
3722 (e2->defstate == SS_DEFINED && !sRef_isKept (e2)))
3725 if (checkDeadState (el, TRUE, loc))
3727 if (sRef_isThroughArrayFetch (el))
3729 sRef_maybeKill (el, loc);
3730 sRef_maybeKill (e2, loc);
3734 else if ((e2->defstate == SS_DEAD || sRef_isKept (e2)) &&
3735 (el->defstate == SS_DEFINED && !sRef_isKept (el)))
3738 if (checkDeadState (e2, FALSE, loc))
3740 if (sRef_isThroughArrayFetch (el))
3742 sRef_maybeKill (el, loc);
3743 sRef_maybeKill (e2, loc);
3747 else if (el->defstate == SS_DEFINED &&
3748 e2->defstate == SS_PDEFINED)
3750 el->defstate = SS_PDEFINED;
3752 else if (e2->defstate == SS_DEFINED &&
3753 el->defstate == SS_PDEFINED)
3755 e2->defstate = SS_PDEFINED;
3762 if (ctype_isUnion (ctype_realType (sRef_getType (el))))
3764 el->deriv = sRef_mergeUnionDerivs (el->deriv, e2->deriv,
3769 el->deriv = sRef_mergeDerivs (el->deriv, e2->deriv, opt, cl, loc);
3772 if (sRef_equivalent (el, e2))
3774 ret = sRefSet_insert (ret, el);
3778 sRef sr = sRef_leastCommon (el, e2);
3780 if (sRef_isValid (sr))
3782 ret = sRefSet_insert (ret, sr);
3790 (void) sRefSet_delete (other, e2);
3792 else /* not defined */
3794 (void) checkDeadState (el, TRUE, loc);
3797 } end_sRefSet_allElements;
3799 sRefSet_allElements (other, el)
3801 if (sRef_isValid (el))
3803 (void) checkDeadState (el, FALSE, loc);
3805 } end_sRefSet_allElements;
3812 ** Returns TRUE is there is an error.
3815 static bool checkDeadState (/*@notnull@*/ sRef el, bool tbranch, fileloc loc)
3818 ** usymtab_isGuarded --- the utab should still be in the
3819 ** state of the alternate branch.
3821 ** tbranch TRUE means el is released in the last branch, e.g.
3822 ** if (x != NULL) { ; } else { sfree (x); }
3823 ** so, if x is null in the other branch no error is reported.
3825 ** tbranch FALSE means this is the other branch:
3826 ** if (x != NULL) { sfree (x); } else { ; }
3827 ** so, if x is null in this branch there is no error.
3831 if ((sRef_isDead (el) || sRef_isKept (el))
3832 && !sRef_isDeepUnionField (el) && !sRef_isThroughArrayFetch (el))
3837 if (usymtab_isProbableDeepNull (el))
3844 if (usymtab_isAltProbablyDeepNull (el))
3852 message ("Storage %q is %q in one path, but live in another.",
3854 cstring_makeLiteral (sRef_isKept (el)
3855 ? "kept" : "released")),
3859 if (sRef_isKept (el))
3861 sRef_showAliasInfo (el);
3865 sRef_showStateInfo (el);
3868 /* prevent further errors */
3869 el->defstate = SS_UNKNOWN;
3870 sRef_setAliasKind (el, AK_ERROR, fileloc_undefined);
3880 checkDerivDeadState (/*@notnull@*/ sRef el, bool tbranch, fileloc loc)
3883 if (checkDeadState (el, tbranch, loc))
3885 sRefSet_allElements (el->deriv, t)
3887 if (sRef_isValid (t))
3889 checkDerivDeadState (t, tbranch, loc);
3891 } end_sRefSet_allElements;
3896 sRef_mergePdefinedDerivs (sRefSet res, sRefSet other, bool opt,
3897 clause cl, fileloc loc)
3899 sRefSet ret = sRefSet_new ();
3901 sRefSet_allElements (res, el)
3903 if (sRef_isValid (el))
3905 sRef e2 = sRefSet_lookupMember (other, el);
3907 if (sRef_isValid (e2))
3909 if (sRef_isAllocated (el) && !sRef_isAllocated (e2))
3913 else if (sRef_isAllocated (e2) && !sRef_isAllocated (el))
3915 el->deriv = sRefSet_copy (el->deriv, e2->deriv);
3919 el->deriv = sRef_mergePdefinedDerivs (el->deriv, e2->deriv,
3923 sRef_mergeStateAux (el, e2, cl, opt, loc, FALSE);
3925 ret = sRefSet_insert (ret, el);
3926 (void) sRefSet_delete (other, e2);
3932 checkDerivDeadState (el, (cl == FALSECLAUSE), loc);
3935 ret = sRefSet_insert (ret, el);
3938 } end_sRefSet_allElements;
3940 sRefSet_allElements (other, el)
3942 if (sRef_isValid (el))
3944 if (!sRefSet_member (ret, el))
3946 /* was cl == FALSECLAUSE */
3947 checkDerivDeadState (el, FALSE, loc);
3948 ret = sRefSet_insert (ret, el);
3953 ** it's okay --- member is a different equality test
3957 } end_sRefSet_allElements;
3963 sRef sRef_makeConj (/*@exposed@*/ /*@returned@*/ sRef a, /*@exposed@*/ sRef b)
3965 llassert (sRef_isValid (a));
3966 llassert (sRef_isValid (b));
3968 if (!sRef_equivalent (a, b))
3970 sRef s = sRef_new ();
3973 s->info = (sinfo) dmalloc (sizeof (*s->info));
3974 s->info->conj = (cjinfo) dmalloc (sizeof (*s->info->conj));
3975 s->info->conj->a = a;
3976 s->info->conj->b = b;
3978 if (ctype_equal (a->type, b->type)) s->type = a->type;
3979 else s->type = ctype_makeConj (a->type, b->type);
3981 if (a->defstate == b->defstate)
3983 s->defstate = a->defstate;
3987 s->defstate = SS_UNKNOWN;
3990 s->nullstate = NS_UNKNOWN;
3992 s->safe = a->safe && b->safe;
3993 s->aliaskind = alkind_resolve (a->aliaskind, b->aliaskind);
3999 /*@-exposetrans@*/ return a; /*@=exposetrans@*/
4006 sRef s = sRef_new ();
4008 s->kind = SK_UNKNOWN;
4013 sRef_makeSpecial (speckind sk) /*@*/
4015 sRef s = sRef_new ();
4017 s->kind = SK_SPECIAL;
4018 s->info = (sinfo) dmalloc (sizeof (*s->info));
4023 static sRef srnothing = sRef_undefined;
4024 static sRef srinternal = sRef_undefined;
4025 static sRef srsystem = sRef_undefined;
4026 static sRef srspec = sRef_undefined;
4029 sRef_makeNothing (void)
4031 if (sRef_isInvalid (srnothing))
4033 srnothing = sRef_makeSpecial (SR_NOTHING);
4042 sRef_makeInternalState (void)
4044 if (sRef_isInvalid (srinternal))
4046 srinternal = sRef_makeSpecial (SR_INTERNAL);
4055 sRef_makeSpecState (void)
4057 if (sRef_isInvalid (srspec))
4059 srspec = sRef_makeSpecial (SR_SPECSTATE);
4068 sRef_makeSystemState (void)
4070 if (sRef_isInvalid (srsystem))
4072 srsystem = sRef_makeSpecial (SR_SYSTEM);
4081 sRef_makeResultType (ctype ct)
4083 sRef res = sRef_makeResult ();
4092 sRef s = sRef_new ();
4094 s->kind = SK_RESULT;
4095 s->type = ctype_unknown;
4096 s->defstate = SS_UNKNOWN;
4097 s->aliaskind = AK_UNKNOWN;
4098 s->nullstate = NS_UNKNOWN;
4105 sRef_isNothing (sRef s)
4107 return (sRef_isKindSpecial (s) && s->info->spec == SR_NOTHING);
4111 sRef_isInternalState (sRef s)
4113 return (sRef_isKindSpecial (s) && s->info->spec == SR_INTERNAL);
4117 sRef_isSpecInternalState (sRef s)
4119 return (sRef_isKindSpecial (s)
4120 && (s->info->spec == SR_INTERNAL || s->info->spec == SR_SPECSTATE));
4124 sRef_isSpecState (sRef s)
4126 return (sRef_isKindSpecial (s) && s->info->spec == SR_SPECSTATE);
4130 sRef_isResult (sRef s)
4132 return (sRef_isValid (s) && s->kind == SK_RESULT);
4136 sRef_isSystemState (sRef s)
4138 return (sRef_isKindSpecial (s) && s->info->spec == SR_SYSTEM);
4142 sRef_getScopeIndex (sRef s)
4144 llassert (sRef_isValid (s));
4145 llassert (sRef_isCvar (s));
4147 return (s->info->cvar->index);
4151 sRef_makeSafe (sRef s)
4153 if (sRef_isValid (s))
4160 sRef_makeUnsafe (sRef s)
4162 if (sRef_isValid (s))
4169 ** memory state operations
4172 /*@only@*/ cstring sRef_unparseFull (sRef s)
4174 if (sRef_isInvalid (s)) return (cstring_undefined);
4176 return (message ("[%d] %q - %q [%s] { %q }",
4178 sRef_unparseDebug (s),
4179 sRef_unparseState (s),
4180 exkind_unparse (s->oexpkind),
4181 sRefSet_unparseDebug (s->deriv)));
4184 /*@unused@*/ cstring sRef_unparseDeep (sRef s)
4186 cstring st = cstring_undefined;
4188 st = message ("%q:", sRef_unparseFull (s));
4190 if (sRef_isValid (s))
4192 sRefSet_allElements (s->deriv, el)
4194 st = message("%q\n%q", st, sRef_unparseDeep (el));
4195 } end_sRefSet_allElements ;
4201 /*@only@*/ cstring sRef_unparseState (sRef s)
4203 if (sRef_isConj (s))
4205 return (message ("%q | %q",
4206 sRef_unparseState (s->info->conj->a),
4207 sRef_unparseState (s->info->conj->b)));
4210 if (sRef_isInvalid (s))
4212 return (cstring_makeLiteral ("<invalid>"));
4215 return (message ("%s.%s.%s.%s",
4216 alkind_unparse (s->aliaskind),
4217 nstate_unparse (s->nullstate),
4218 exkind_unparse (s->expkind),
4219 sstate_unparse (s->defstate)));
4222 bool sRef_isNotUndefined (sRef s)
4224 return (sRef_isInvalid (s)
4225 || (s->defstate != SS_UNDEFINED
4226 && s->defstate != SS_UNUSEABLE
4227 && s->defstate != SS_DEAD));
4230 ynm sRef_isWriteable (sRef s)
4232 if (sRef_isInvalid (s)) return MAYBE;
4234 if (sRef_isConj (s) && s->defstate == SS_UNKNOWN)
4236 if (ynm_toBoolStrict (sRef_isWriteable (sRef_getConjA (s))))
4238 if (ynm_toBoolStrict (sRef_isWriteable (sRef_getConjB (s))))
4246 if (ynm_toBoolStrict (sRef_isWriteable (sRef_getConjB (s))))
4254 return (ynm_fromBool (s->defstate != SS_UNUSEABLE));
4257 bool sRef_hasNoStorage (sRef s)
4259 return (!sRef_isAllocatedStorage (s) || sRef_isDefinitelyNull (s));
4262 bool sRef_isStrictReadable (sRef s)
4264 return (ynm_toBoolStrict (sRef_isReadable (s)));
4267 ynm sRef_isReadable (sRef s)
4271 if (sRef_isInvalid (s)) return YES;
4275 if (sRef_isConj (s) && s->defstate == SS_UNKNOWN)
4277 if (ynm_toBoolStrict (sRef_isReadable (sRef_getConjA (s))))
4279 if (ynm_toBoolStrict (sRef_isReadable (sRef_getConjB (s))))
4287 if (ynm_toBoolStrict (sRef_isReadable (sRef_getConjB (s))))
4294 else if (ss == SS_HOFFA)
4296 if (context_getFlag (FLG_STRICTUSERELEASED))
4307 return (ynm_fromBool (ss == SS_DEFINED
4310 || ss == SS_PDEFINED
4313 || ss == SS_ALLOCATED
4314 || ss == SS_UNKNOWN));
4318 static /*@exposed@*/ sRef whatUndefined (/*@exposed@*/ sRef fref, int depth)
4323 if (depth > MAXDEPTH)
4326 ("Warning: check definition limit exceeded, checking %q. "
4327 "This either means there is a variable with at least "
4328 "%d indirections apparent in the program text, or "
4329 "there is a bug in LCLint.",
4330 sRef_unparse (fref),
4334 return sRef_undefined;
4337 if (!sRef_isKnown (fref) || sRef_isAnyDefined (fref))
4339 return sRef_undefined;
4342 if (sRef_isUnuseable (fref) || sRef_isStateUndefined (fref))
4347 ct = ctype_realType (sRef_getType (fref));
4349 if (ctype_isUnknown (ct))
4351 return sRef_undefined;
4353 else if (ctype_isPointer (ct) || ctype_isArray (ct))
4355 if (sRef_isStateUnknown (fref))
4357 return sRef_undefined;
4361 sRef fptr = sRef_constructDeref (fref);
4363 return (whatUndefined (fptr, depth + 1));
4366 else if (ctype_isStruct (ct))
4368 bool hasOneDefined = FALSE;
4370 if (sRef_isStateUnknown (fref))
4375 if (sRef_isPdefined (fref) || sRef_isAnyDefined (fref))
4377 sRefSet_realElements (sRef_derivedFields (fref), sr)
4379 hasOneDefined = TRUE;
4381 if (sRef_isField (sr))
4383 cstring fieldname = sRef_getField (sr);
4384 sRef fldref = sRef_makeField (fref, fieldname);
4385 bool shouldCheck = !sRef_isRecursiveField (fldref);
4389 sRef wdef = whatUndefined (fldref, depth + 1);
4391 if (sRef_isValid (wdef))
4397 } end_sRefSet_realElements;
4399 else if (sRef_isAllocated (fref))
4402 ** for structures, each field must be completely defined
4405 uentryList fields = ctype_getFields (ct);
4407 uentryList_elements (fields, ue)
4409 cstring name = uentry_getRealName (ue);
4410 sRef ffield = sRef_makeField (fref, name);
4411 bool shouldCheck = !sRef_isRecursiveField (ffield);
4413 if (sRef_isRelDef (uentry_getSref (ue)))
4421 sRef wdef = whatUndefined (ffield, depth + 1);
4423 if (sRef_isInvalid (wdef))
4429 } end_uentryList_elements;
4436 else if (ctype_isUnion (ct))
4445 return sRef_undefined;
4448 static bool checkDefined (sRef sr)
4450 return (sRef_isInvalid (whatUndefined (sr, 0)));
4453 bool sRef_isReallyDefined (sRef s)
4455 if (sRef_isValid (s))
4457 if (sRef_isAnyDefined (s))
4463 if (sRef_isAllocated (s) || sRef_isPdefined (s))
4465 return checkDefined (s);
4479 void sRef_showNotReallyDefined (sRef s)
4481 if (sRef_isValid (s))
4483 if (sRef_isAnyDefined (s))
4489 if (sRef_isAllocated (s) || sRef_isPdefined (s))
4491 sRef ref = whatUndefined (s, 0);
4493 llassert (sRef_isValid (ref));
4498 (message ("This sub-reference is %s: %q",
4499 sstate_unparse (sRef_getDefState (ref)),
4500 sRef_unparse (ref)));
4515 sstate sRef_getDefState (sRef s)
4517 if (sRef_isInvalid (s)) return (SS_UNKNOWN);
4518 return (s->defstate);
4521 void sRef_setDefState (sRef s, sstate defstate, fileloc loc)
4523 sRef_setStateAux (s, defstate, loc);
4526 static void sRef_clearAliasStateAux (sRef s, fileloc loc)
4528 sRef_setAliasKind (s, AK_ERROR, loc);
4531 void sRef_clearAliasState (sRef s, fileloc loc)
4533 sRef_aliasSetComplete (sRef_clearAliasStateAux, s, loc);
4536 void sRef_setAliasKindComplete (sRef s, alkind kind, fileloc loc)
4538 sRef_aliasSetCompleteParam (sRef_setAliasKind, s, kind, loc);
4541 void sRef_setAliasKind (sRef s, alkind kind, fileloc loc)
4543 if (sRef_isValid (s))
4545 sRef_clearDerived (s);
4547 if ((kind != s->aliaskind && kind != s->oaliaskind)
4548 && fileloc_isDefined (loc))
4550 s->aliasinfo = alinfo_updateLoc (s->aliasinfo, loc);
4553 s->aliaskind = kind;
4557 void sRef_setOrigAliasKind (sRef s, alkind kind)
4559 if (sRef_isValid (s))
4561 s->oaliaskind = kind;
4565 exkind sRef_getExKind (sRef s)
4567 if (sRef_isValid (s))
4569 return (s->expkind);
4577 exkind sRef_getOrigExKind (sRef s)
4579 if (sRef_isValid (s))
4581 return (s->oexpkind);
4589 static void sRef_clearExKindAux (sRef s, fileloc loc)
4591 sRef_setExKind (s, XO_UNKNOWN, loc);
4594 void sRef_setObserver (sRef s, fileloc loc)
4596 sRef_setExKind (s, XO_OBSERVER, loc);
4599 void sRef_setExposed (sRef s, fileloc loc)
4601 sRef_setExKind (s, XO_EXPOSED, loc);
4604 void sRef_clearExKindComplete (sRef s, fileloc loc)
4606 (void) sRef_aliasSetComplete (sRef_clearExKindAux, s, loc);
4609 void sRef_setExKind (sRef s, exkind exp, fileloc loc)
4611 if (sRef_isValid (s))
4613 if (s->expkind != exp)
4615 s->expinfo = alinfo_updateLoc (s->expinfo, loc);
4623 ** s1->derived = s2->derived
4626 static void sRef_copyRealDerived (sRef s1, sRef s2)
4628 if (sRef_isValid (s1) && sRef_isValid (s2))
4630 sRef sb = sRef_getRootBase (s1);
4632 sRefSet_clear (s1->deriv);
4634 sRefSet_allElements (s2->deriv, el)
4636 if (sRef_isValid (el))
4638 sRef rb = sRef_getRootBase (el);
4640 if (!sRef_same (rb, sb))
4642 sRef fb = sRef_fixDirectBase (el, s1);
4644 if (sRef_isValid (fb))
4646 sRef_copyRealDerived (fb, el);
4647 sRef_addDeriv (s1, fb);
4652 sRef_addDeriv (s1, el);
4655 } end_sRefSet_allElements ;
4660 void sRef_copyRealDerivedComplete (sRef s1, sRef s2)
4662 sRef_innerAliasSetCompleteParam (sRef_copyRealDerived, s1, s2);
4665 void sRef_setUndefined (sRef s, fileloc loc)
4667 if (sRef_isValid (s))
4669 s->defstate = SS_UNDEFINED;
4671 if (fileloc_isDefined (loc))
4673 s->definfo = alinfo_updateLoc (s->definfo, loc);
4676 sRef_clearDerived (s);
4680 static void sRef_setDefinedAux (sRef s, fileloc loc, bool clear)
4682 if (sRef_isInvalid (s)) return;
4684 if (s->defstate != SS_DEFINED && fileloc_isDefined (loc))
4686 s->definfo = alinfo_updateLoc (s->definfo, loc);
4689 s->defstate = SS_DEFINED;
4691 /* e.g., if x is allocated, *x = 3 defines x */
4693 if (s->kind == SK_PTR)
4695 sRef p = s->info->ref;
4697 if (p->defstate == SS_ALLOCATED)
4699 sRef_setDefinedAux (p, loc, clear);
4702 else if (s->kind == SK_ARRAYFETCH)
4704 if (!s->info->arrayfetch->indknown
4705 || (s->info->arrayfetch->ind == 0))
4707 sRef p = s->info->arrayfetch->arr;
4708 sRef ptr = sRef_constructPointer (p);
4710 if (sRef_isValid (ptr))
4712 if (ptr->defstate == SS_ALLOCATED
4713 || ptr->defstate == SS_UNDEFINED)
4715 sRef_setDefinedAux (ptr, loc, clear);
4719 if (p->defstate == SS_RELDEF)
4723 else if (p->defstate == SS_ALLOCATED || p->defstate == SS_PDEFINED)
4725 p->defstate = SS_DEFINED;
4732 else if (s->kind == SK_FIELD)
4734 sRef parent = s->info->field->rec;
4736 if (sRef_isValid (parent))
4738 if (ctype_isUnion (ctype_realType (parent->type)))
4741 ** Should not clear derived from here.
4744 sRef_setDefinedNoClear (parent, loc);
4748 ; /* Nothing to do for structures. */
4760 sRef_clearDerived (s);
4764 static void sRef_setPartialDefined (sRef s, fileloc loc)
4766 if (!sRef_isPartial (s))
4768 sRef_setDefined (s, loc);
4772 void sRef_setPartialDefinedComplete (sRef s, fileloc loc)
4774 sRef_innerAliasSetComplete (sRef_setPartialDefined, s, loc);
4777 void sRef_setDefinedComplete (sRef s, fileloc loc)
4779 sRef_innerAliasSetComplete (sRef_setDefined, s, loc);
4782 void sRef_setDefined (sRef s, fileloc loc)
4784 sRef_setDefinedAux (s, loc, TRUE);
4787 static void sRef_setDefinedNoClear (sRef s, fileloc loc)
4789 DPRINTF (("Defining: %s", sRef_unparseFull (s)));
4790 sRef_setDefinedAux (s, loc, FALSE);
4791 DPRINTF (("==> %s", sRef_unparseFull (s)));
4794 void sRef_setDefinedNCComplete (sRef s, fileloc loc)
4796 DPRINTF (("Set Defined Complete: %s", sRef_unparseFull (s)));
4797 sRef_innerAliasSetComplete (sRef_setDefinedNoClear, s, loc);
4798 DPRINTF (("==> %s", sRef_unparseFull (s)));
4801 static bool sRef_isDeepUnionField (sRef s)
4803 return (sRef_deepPred (sRef_isUnionField, s));
4806 bool sRef_isUnionField (sRef s)
4808 if (sRef_isValid (s) && s->kind == SK_FIELD)
4811 ** defining one field of a union defines the union
4814 sRef base = s->info->field->rec;
4816 if (sRef_isValid (base))
4818 return (ctype_isUnion (ctype_realType (base->type)));
4825 void sRef_setPdefined (sRef s, fileloc loc)
4827 if (sRef_isValid (s) && !sRef_isPartial (s))
4829 sRef base = sRef_getBaseSafe (s);
4831 if (s->defstate == SS_ALLOCATED)
4836 if (s->defstate != SS_PDEFINED && fileloc_isDefined (loc))
4838 s->definfo = alinfo_updateLoc (s->definfo, loc);
4841 s->defstate = SS_PDEFINED;
4843 /* e.g., if x is allocated, *x = 3 defines x */
4845 while (sRef_isValid (base) && sRef_isKnown (base))
4847 if (base->defstate == SS_DEFINED)
4851 base->defstate = SS_PDEFINED;
4852 nb = sRef_getBaseSafe (base);
4863 static void sRef_setStateAux (sRef s, sstate ss, fileloc loc)
4865 if (sRef_isValid (s))
4867 /* if (s->defstate == SS_RELDEF) return; */
4869 if (s->defstate != ss && fileloc_isDefined (loc))
4871 s->definfo = alinfo_updateLoc (s->definfo, loc);
4875 sRef_clearDerived (s);
4877 if (ss == SS_ALLOCATED)
4879 sRef base = sRef_getBaseSafe (s);
4881 while (sRef_isValid (base) && sRef_isKnown (base))
4883 if (base->defstate == SS_DEFINED)
4887 base->defstate = SS_PDEFINED;
4889 nb = sRef_getBaseSafe (base);
4902 void sRef_setAllocatedComplete (sRef s, fileloc loc)
4904 sRef_innerAliasSetComplete (sRef_setAllocated, s, loc);
4907 static void sRef_setAllocatedShallow (sRef s, fileloc loc)
4909 if (sRef_isValid (s))
4911 if (s->defstate == SS_DEAD || s->defstate == SS_UNDEFINED)
4913 s->defstate = SS_ALLOCATED;
4915 if (fileloc_isDefined (loc))
4917 s->definfo = alinfo_updateLoc (s->definfo, loc);
4923 void sRef_setAllocatedShallowComplete (sRef s, fileloc loc)
4925 sRef_innerAliasSetComplete (sRef_setAllocatedShallow, s, loc);
4928 void sRef_setAllocated (sRef s, fileloc loc)
4930 sRef_setStateAux (s, SS_ALLOCATED, loc);
4933 void sRef_setPartial (sRef s, fileloc loc)
4935 sRef_setStateAux (s, SS_PARTIAL, loc);
4938 void sRef_setShared (sRef s, fileloc loc)
4940 if (sRef_isValid (s))
4942 if (s->aliaskind != AK_SHARED && fileloc_isDefined (loc))
4944 s->aliasinfo = alinfo_updateLoc (s->aliasinfo, loc);
4947 s->aliaskind = AK_SHARED;
4948 /* don't! sRef_clearDerived (s); */
4952 void sRef_setLastReference (sRef s, sRef ref, fileloc loc)
4954 if (sRef_isValid (s))
4956 s->aliaskind = sRef_getAliasKind (ref);
4957 s->aliasinfo = alinfo_updateRefLoc (s->aliasinfo, ref, loc);
4962 void sRef_setNullStateAux (/*@notnull@*/ sRef s, nstate ns, fileloc loc)
4966 if (fileloc_isDefined (loc))
4968 s->nullinfo = alinfo_updateLoc (s->nullinfo, loc);
4972 void sRef_setNotNull (sRef s, fileloc loc)
4974 if (sRef_isValid (s))
4976 sRef_setNullStateAux (s, NS_NOTNULL, loc);
4980 void sRef_setNullState (sRef s, nstate n, fileloc loc)
4982 if (sRef_isValid (s))
4984 sRef_setNullStateAux (s, n, loc);
4988 void sRef_setNullTerminatedStateInnerComplete (sRef s, struct _bbufinfo b, /*@unused@*/ fileloc loc) {
4990 switch (b.bufstate) {
4991 case BB_NULLTERMINATED:
4992 sRef_setNullTerminatedState (s);
4993 sRef_setLen (s, b.len);
4995 case BB_POSSIBLYNULLTERMINATED:
4996 sRef_setPossiblyNullTerminatedState(s);
4998 case BB_NOTNULLTERMINATED:
4999 sRef_setNotNullTerminatedState (s);
5002 sRef_setSize (s, b.size);
5004 /* PL: TO BE DONE : Aliases are not modified right now, have to be similar to
5005 * setNullStateInnerComplete.
5009 void sRef_setNullStateInnerComplete (sRef s, nstate n, fileloc loc)
5011 sRef_setNullState (s, n, loc);
5016 sRef_innerAliasSetComplete (sRef_setPosNull, s, loc);
5019 sRef_innerAliasSetComplete (sRef_setDefNull, s, loc);
5022 sRef_innerAliasSetComplete (sRef_setNullUnknown, s, loc);
5025 sRef_innerAliasSetComplete (sRef_setNotNull, s, loc);
5028 sRef_innerAliasSetComplete (sRef_setNotNull, s, loc);
5031 sRef_innerAliasSetComplete (sRef_setNullUnknown, s, loc);
5034 sRef_innerAliasSetComplete (sRef_setDefNull, s, loc);
5037 sRef_innerAliasSetComplete (sRef_setNullUnknown, s, loc);
5040 sRef_innerAliasSetComplete (sRef_setNullErrorLoc, s, loc);
5045 void sRef_setPosNull (sRef s, fileloc loc)
5047 if (sRef_isValid (s))
5049 sRef_setNullStateAux (s, NS_POSNULL, loc);
5053 void sRef_setDefNull (sRef s, fileloc loc)
5055 if (sRef_isValid (s))
5057 sRef_setNullStateAux (s, NS_DEFNULL, loc);
5061 void sRef_setNullUnknown (sRef s, fileloc loc)
5063 if (sRef_isValid (s))
5065 sRef_setNullStateAux (s, NS_UNKNOWN, loc);
5069 void sRef_setNullError (sRef s)
5071 if (sRef_isValid (s))
5073 sRef_setNullStateAux (s, NS_UNKNOWN, fileloc_undefined);
5077 void sRef_setNullErrorLoc (sRef s, /*@unused@*/ fileloc loc)
5079 sRef_setNullError (s);
5082 void sRef_setOnly (sRef s, fileloc loc)
5084 if (sRef_isValid (s) && s->aliaskind != AK_ONLY)
5086 s->aliaskind = AK_ONLY;
5087 s->aliasinfo = alinfo_updateLoc (s->aliasinfo, loc);
5091 void sRef_setDependent (sRef s, fileloc loc)
5093 if (sRef_isValid (s) && !sRef_isConst (s) && (s->aliaskind != AK_DEPENDENT))
5095 s->aliaskind = AK_DEPENDENT;
5096 s->aliasinfo = alinfo_updateLoc (s->aliasinfo, loc);
5100 void sRef_setOwned (sRef s, fileloc loc)
5102 if (sRef_isValid (s) && !sRef_isConst (s) && (s->aliaskind != AK_OWNED))
5104 s->aliaskind = AK_OWNED;
5105 s->aliasinfo = alinfo_updateLoc (s->aliasinfo, loc);
5109 void sRef_setKept (sRef s, fileloc loc)
5111 if (sRef_isValid (s) && !sRef_isConst (s) && (s->aliaskind != AK_KEPT))
5113 sRef base = sRef_getBaseSafe (s);
5115 while (sRef_isValid (base) && sRef_isKnown (base))
5117 if (base->defstate == SS_DEFINED)
5119 base->defstate = SS_PDEFINED;
5120 base = sRef_getBaseSafe (base);
5129 s->aliaskind = AK_KEPT;
5130 s->aliasinfo = alinfo_updateLoc (s->aliasinfo, loc);
5134 static void sRef_setKeptAux (sRef s, fileloc loc)
5136 if (!sRef_isShared (s))
5138 sRef_setKept (s, loc);
5142 static void sRef_setDependentAux (sRef s, fileloc loc)
5144 if (!sRef_isShared (s))
5146 sRef_setDependent (s, loc);
5150 void sRef_setKeptComplete (sRef s, fileloc loc)
5152 sRef_aliasSetComplete (sRef_setKeptAux, s, loc);
5155 void sRef_setDependentComplete (sRef s, fileloc loc)
5157 sRef_aliasSetComplete (sRef_setDependentAux, s, loc);
5160 void sRef_setFresh (sRef s, fileloc loc)
5162 if (sRef_isValid (s))
5164 s->aliaskind = AK_FRESH;
5165 s->aliasinfo = alinfo_updateLoc (s->aliasinfo, loc);
5169 void sRef_kill (sRef s, fileloc loc)
5171 DPRINTF (("Kill: %s", sRef_unparseFull (s)));
5173 if (sRef_isValid (s) && !sRef_isShared (s) && !sRef_isConst (s))
5175 sRef base = sRef_getBaseSafe (s);
5177 while (sRef_isValid (base) && sRef_isKnown (base))
5179 if (base->defstate == SS_DEFINED)
5181 base->defstate = SS_PDEFINED;
5182 base = sRef_getBaseSafe (base);
5191 s->aliaskind = s->oaliaskind;
5192 s->defstate = SS_DEAD;
5193 s->definfo = alinfo_updateLoc (s->definfo, loc);
5195 sRef_clearDerived (s);
5199 void sRef_maybeKill (sRef s, fileloc loc)
5202 if (sRef_isValid (s))
5204 sRef base = sRef_getBaseSafe (s);
5207 while (sRef_isValid (base) && sRef_isKnown (base))
5209 if (base->defstate == SS_DEFINED || base->defstate == SS_RELDEF)
5211 base->defstate = SS_PDEFINED;
5212 base = sRef_getBaseSafe (base);
5221 s->aliaskind = s->oaliaskind;
5222 s->defstate = SS_HOFFA;
5223 s->definfo = alinfo_updateLoc (s->definfo, loc);
5224 sRef_clearDerived (s);
5230 ** just for type checking...
5233 static void sRef_killAux (sRef s, fileloc loc)
5235 if (sRef_isValid (s) && !sRef_isShared (s))
5237 if (sRef_isUnknownArrayFetch (s))
5239 sRef_maybeKill (s, loc);
5249 ** kills s and all aliases to s
5252 void sRef_killComplete (sRef s, fileloc loc)
5254 DPRINTF (("Kill complete: %s", sRef_unparseFull (s)));
5255 sRef_aliasSetComplete (sRef_killAux, s, loc);
5258 static bool sRef_equivalent (sRef s1, sRef s2)
5260 return (sRef_compare (s1, s2) == 0);
5264 ** returns an sRef that will not be free'd on function exit.
5267 /*@only@*/ sRef sRef_saveCopy (sRef s)
5271 if (sRef_isValid (s))
5273 bool old = inFunction;
5276 ** Exit the function scope, so this sRef is not
5277 ** stored in the deallocation table.
5281 DPRINTF (("Copying sref: %s", sRef_unparseFull(s)));
5282 ret = sRef_copy (s);
5283 DPRINTF (("Copying ===>: %s", sRef_unparseFull(ret)));
5288 ret = sRef_undefined;
5291 /*@-dependenttrans@*/
5293 /*@=dependenttrans@*/
5296 sRef sRef_copy (sRef s)
5298 if (sRef_isKindSpecial (s))
5301 return s; /* don't copy specials */
5305 if (sRef_isValid (s))
5307 sRef t = sRef_alloc ();
5311 t->modified = s->modified;
5314 t->info = sinfo_copy (s);
5316 t->defstate = s->defstate;
5318 t->nullstate = s->nullstate;
5320 /* start modifications */
5321 t->bufinfo.bufstate = s->bufinfo.bufstate;
5322 t->bufinfo.len = s->bufinfo.len;
5323 t->bufinfo.size = s->bufinfo.size;
5324 /* end modifications */
5326 t->aliaskind = s->aliaskind;
5327 t->oaliaskind = s->oaliaskind;
5329 t->expkind = s->expkind;
5330 t->oexpkind = s->oexpkind;
5332 t->aliasinfo = alinfo_copy (s->aliasinfo);
5333 t->definfo = alinfo_copy (s->definfo);
5334 t->nullinfo = alinfo_copy (s->nullinfo);
5335 t->expinfo = alinfo_copy (s->expinfo);
5337 t->deriv = sRefSet_newDeepCopy (s->deriv);
5343 return sRef_undefined;
5348 # define PREDTEST(func,s) \
5349 do { if (sRef_isInvalid (s)) { return FALSE; } \
5350 else { if (sRef_isConj (s)) \
5351 { return (func (sRef_getConjA (s)) \
5352 || func (sRef_getConjB (s))); }}} while (FALSE);
5354 bool sRef_isAddress (sRef s)
5356 PREDTEST (sRef_isAddress, s);
5357 return (s->kind == SK_ADR);
5361 ** pretty weak... maybe a flag should control this.
5364 bool sRef_isThroughArrayFetch (sRef s)
5366 if (sRef_isValid (s))
5374 if (sRef_isArrayFetch (tref))
5379 lt = sRef_getBase (tref);
5381 } while (sRef_isValid (tref));
5387 bool sRef_isArrayFetch (sRef s)
5389 PREDTEST (sRef_isArrayFetch, s);
5390 return (s->kind == SK_ARRAYFETCH);
5393 bool sRef_isMacroParamRef (sRef s)
5395 if (context_inMacro () && sRef_isCvar (s))
5397 uentry ue = sRef_getUentry (s);
5398 cstring pname = makeParam (uentry_rawName (ue));
5399 uentry mac = usymtab_lookupSafe (pname);
5401 cstring_free (pname);
5402 return (uentry_isValid (mac));
5408 bool sRef_isCvar (sRef s)
5410 PREDTEST (sRef_isCvar, s);
5411 return (s->kind == SK_CVAR);
5414 bool sRef_isConst (sRef s)
5416 PREDTEST (sRef_isConst, s);
5417 return (s->kind == SK_CONST);
5420 bool sRef_isObject (sRef s)
5422 PREDTEST (sRef_isObject, s);
5423 return (s->kind == SK_OBJECT);
5426 bool sRef_isExternal (sRef s)
5428 PREDTEST (sRef_isExternal, s);
5429 return (s->kind == SK_EXTERNAL);
5432 static bool sRef_isDerived (sRef s)
5434 PREDTEST (sRef_isDerived, s);
5435 return (s->kind == SK_DERIVED);
5438 bool sRef_isField (sRef s)
5440 PREDTEST (sRef_isField, s);
5441 return (s->kind == SK_FIELD);
5444 static bool sRef_isIndex (sRef s)
5446 PREDTEST (sRef_isIndex, s);
5447 return (s->kind == SK_ARRAYFETCH);
5450 bool sRef_isAnyParam (sRef s)
5452 PREDTEST (sRef_isAnyParam, s);
5453 return (s->kind == SK_PARAM);
5456 bool sRef_isParam (sRef s)
5458 PREDTEST (sRef_isParam, s);
5459 return (s->kind == SK_PARAM);
5462 bool sRef_isDirectParam (sRef s)
5464 PREDTEST (sRef_isDirectParam, s);
5466 return ((s->kind == SK_CVAR) &&
5467 (s->info->cvar->lexlevel == functionScope) &&
5468 (context_inFunction () &&
5469 (s->info->cvar->index <= uentryList_size (context_getParams ()))));
5472 bool sRef_isPointer (sRef s)
5474 PREDTEST (sRef_isPointer, s);
5475 return (s->kind == SK_PTR);
5479 ** returns true if storage referenced by s is visible
5482 bool sRef_isReference (sRef s)
5484 PREDTEST (sRef_isReference, s);
5486 return (sRef_isPointer (s) || sRef_isIndex (s) || sRef_isGlobal (s)
5487 || (sRef_isField (s) && (sRef_isReference (s->info->field->rec))));
5490 bool sRef_isIReference (sRef s)
5492 return (sRef_isPointer (s) || sRef_isAddress (s) || sRef_isIndex (s)
5493 || sRef_isField (s) || sRef_isArrayFetch (s));
5496 bool sRef_isGlobal (sRef s)
5498 return (sRef_isCvar (s) && (s->info->cvar->lexlevel <= fileScope));
5501 bool sRef_isRealGlobal (sRef s)
5503 return (sRef_isCvar (s) && (s->info->cvar->lexlevel == globScope));
5506 bool sRef_isFileStatic (sRef s)
5508 return (sRef_isCvar (s) && (s->info->cvar->lexlevel == fileScope));
5511 bool sRef_isAliasCheckedGlobal (sRef s)
5513 if (sRef_isGlobal (s))
5515 uentry ue = sRef_getUentry (s);
5517 return context_checkAliasGlob (ue);
5525 void sRef_free (/*@only@*/ sRef s)
5527 if (s != sRef_undefined && s->kind != SK_SPECIAL)
5529 alinfo_free (s->expinfo);
5530 alinfo_free (s->aliasinfo);
5531 alinfo_free (s->definfo);
5532 alinfo_free (s->nullinfo);
5534 sRefSet_free (s->deriv);
5535 s->deriv = sRefSet_undefined;
5542 void sRef_setType (sRef s, ctype t)
5544 if (sRef_isValid (s))
5550 void sRef_setTypeFull (sRef s, ctype t)
5552 if (sRef_isValid (s))
5556 sRefSet_allElements (s->deriv, current)
5558 sRef_setTypeFull (current, ctype_unknown);
5559 } end_sRefSet_allElements ;
5564 sRef_buildField (sRef rec, /*@dependent@*/ cstring f)
5566 return (sRef_buildNCField (rec, f));
5569 static /*@exposed@*/ sRef
5570 sRef_findDerivedField (/*@notnull@*/ sRef rec, cstring f)
5572 sRefSet_allElements (rec->deriv, sr)
5574 if (sRef_isValid (sr))
5576 if (sr->kind == SK_FIELD && cstring_equal (sr->info->field->field, f))
5581 } end_sRefSet_allElements;
5583 return sRef_undefined;
5586 /*@dependent@*/ /*@observer@*/ sRefSet
5587 sRef_derivedFields (sRef rec)
5589 if (sRef_isValid (rec))
5597 return (sRefSet_undefined);
5601 static /*@exposed@*/ sRef
5602 sRef_findDerivedPointer (sRef s)
5604 if (sRef_isValid (s))
5606 sRefSet_realElements (s->deriv, sr)
5608 if (sRef_isValid (sr) && sr->kind == SK_PTR)
5612 } end_sRefSet_realElements;
5615 return sRef_undefined;
5619 sRef_isUnknownArrayFetch (sRef s)
5621 return (sRef_isValid (s)
5622 && s->kind == SK_ARRAYFETCH
5623 && !s->info->arrayfetch->indknown);
5626 static /*@exposed@*/ sRef
5627 sRef_findDerivedArrayFetch (/*@notnull@*/ sRef s, bool isknown, int idx, bool dead)
5632 sRefSet_realElements (s->deriv, sr)
5634 if (sRef_isValid (sr)
5635 && sr->kind == SK_ARRAYFETCH
5636 && sr->info->arrayfetch->indknown
5637 && (sr->info->arrayfetch->ind == idx))
5641 } end_sRefSet_realElements;
5645 sRefSet_realElements (s->deriv, sr)
5647 if (sRef_isValid (sr)
5648 && sr->kind == SK_ARRAYFETCH
5649 && (!sr->info->arrayfetch->indknown
5650 || (sr->info->arrayfetch->indknown &&
5651 sr->info->arrayfetch->ind == 0)))
5653 if (sRef_isDead (sr) || sRef_isKept (sr))
5655 if (dead || context_getFlag (FLG_STRICTUSERELEASED))
5665 } end_sRefSet_realElements;
5668 return sRef_undefined;
5671 static /*@exposed@*/ sRef
5672 sRef_buildNCField (/*@exposed@*/ sRef rec, /*@exposed@*/ cstring f)
5676 if (sRef_isInvalid (rec))
5678 return sRef_undefined;
5682 ** check if the field already has been referenced
5685 s = sRef_findDerivedField (rec, f);
5687 if (sRef_isValid (s))
5693 ctype ct = ctype_realType (rec->type);
5697 s->info = (sinfo) dmalloc (sizeof (*s->info));
5698 s->info->field = (fldinfo) dmalloc (sizeof (*s->info->field));
5699 s->info->field->rec = rec;
5700 s->info->field->field = f; /* doesn't copy f */
5703 if (ctype_isKnown (ct) && ctype_isSU (ct))
5705 uentry ue = uentryList_lookupField (ctype_getFields (ct), f);
5707 if (!uentry_isUndefined (ue))
5709 s->type = uentry_getType (ue);
5711 if (ctype_isMutable (s->type)
5712 && rec->aliaskind != AK_STACK
5713 && !alkind_isStatic (rec->aliaskind))
5715 s->aliaskind = rec->aliaskind;
5719 s->aliaskind = AK_UNKNOWN;
5722 if (sRef_isStateDefined (rec) || sRef_isStateUnknown (rec)
5723 || sRef_isPdefined (rec))
5725 sRef_setStateFromUentry (s, ue);
5729 sRef_setPartsFromUentry (s, ue);
5732 s->oaliaskind = s->aliaskind;
5733 s->oexpkind = s->expkind;
5738 Never report this as an error. It can happen whenever there
5739 is casting involved.
5743 llcontbug (message ("buildNCField --- no field %s: %q / %s",
5744 f, sRef_unparse (s), ctype_unparse (ct)));
5748 return sRef_undefined;
5752 if (rec->defstate == SS_DEFINED
5753 && (s->defstate == SS_UNDEFINED || s->defstate == SS_UNKNOWN))
5755 s->defstate = SS_DEFINED;
5757 else if (rec->defstate == SS_PARTIAL)
5759 s->defstate = SS_PARTIAL;
5761 else if (rec->defstate == SS_ALLOCATED)
5763 if (ctype_isStackAllocated (ct) && ctype_isStackAllocated (s->type))
5765 s->defstate = SS_ALLOCATED;
5769 s->defstate = SS_UNDEFINED;
5772 else if (s->defstate == SS_UNKNOWN)
5774 s->defstate = rec->defstate;
5781 if (s->defstate == SS_UNDEFINED)
5783 ctype rt = ctype_realType (s->type);
5785 if (ctype_isArray (rt) || ctype_isSU (rt))
5787 s->defstate = SS_ALLOCATED;
5791 sRef_addDeriv (rec, s);
5793 if (ctype_isInt (s->type) && cstring_equal (f, REFSNAME))
5795 s->aliaskind = AK_REFS;
5796 s->oaliaskind = AK_REFS;
5804 sRef_isStackAllocated (sRef s)
5806 return (sRef_isValid(s)
5807 && s->defstate == SS_ALLOCATED && ctype_isStackAllocated (s->type));
5811 void sRef_setArrayFetchState (/*@notnull@*/ /*@exposed@*/ sRef s,
5812 /*@notnull@*/ /*@exposed@*/ sRef arr)
5814 if (ctype_isRealAP (arr->type))
5816 s->type = ctype_baseArrayPtr (arr->type);
5819 /* a hack, methinks... makeArrayFetch (&a[0]) ==> a[] */
5820 if (sRef_isAddress (arr))
5822 sRef t = arr->info->ref;
5824 if (sRef_isArrayFetch (t))
5826 s->info->arrayfetch->arr = t->info->arrayfetch->arr;
5829 else if (ctype_isRealPointer (arr->type))
5831 sRef sp = sRef_findDerivedPointer (arr);
5834 if (sRef_isValid (sp))
5837 if (ctype_isMutable (s->type))
5839 sRef_setExKind (s, sRef_getExKind (sp), fileloc_undefined);
5842 s->aliaskind = sp->aliaskind;
5845 s->defstate = sp->defstate;
5847 if (s->defstate == SS_DEFINED)
5849 if (!context_getFlag (FLG_STRICTDESTROY))
5851 s->defstate = SS_PARTIAL;
5855 s->nullstate = sp->nullstate;
5859 if (arr->defstate == SS_UNDEFINED)
5861 s->defstate = SS_UNUSEABLE;
5863 else if ((arr->defstate == SS_ALLOCATED) && !ctype_isSU (s->type))
5865 s->defstate = SS_UNDEFINED;
5869 if (!context_getFlag (FLG_STRICTDESTROY))
5871 s->defstate = SS_PARTIAL;
5875 s->defstate = SS_DEFINED;
5879 ** Very weak checking for array elements.
5881 ** s->defstate = arr->defstate;
5885 sRef_setExKind (s, sRef_getExKind (arr), g_currentloc);
5887 if (arr->aliaskind == AK_LOCAL || arr->aliaskind == AK_FRESH)
5889 s->aliaskind = AK_LOCAL;
5893 s->aliaskind = AK_UNKNOWN;
5896 sRef_setTypeState (s);
5901 if (arr->defstate == SS_DEFINED)
5904 ** Very weak checking for array elements.
5906 ** s->defstate = arr->defstate;
5909 if (context_getFlag (FLG_STRICTDESTROY))
5911 s->defstate = SS_DEFINED;
5915 s->defstate = SS_PARTIAL;
5918 else if (arr->defstate == SS_ALLOCATED)
5920 if (ctype_isRealArray (s->type))
5922 s->defstate = SS_ALLOCATED;
5926 if (!s->info->arrayfetch->indknown)
5929 ** is index is unknown, elements is defined or
5930 ** allocated is any element is!
5933 s->defstate = SS_UNDEFINED;
5935 sRefSet_allElements (arr->deriv, sr)
5937 if (sRef_isValid (sr))
5939 if (sr->defstate == SS_ALLOCATED)
5941 s->defstate = SS_ALLOCATED;
5945 if (sr->defstate == SS_DEFINED)
5947 if (context_getFlag (FLG_STRICTDESTROY))
5949 s->defstate = SS_DEFINED;
5953 s->defstate = SS_PARTIAL;
5960 } end_sRefSet_allElements;
5965 s->defstate = SS_UNDEFINED;
5971 s->defstate = arr->defstate;
5976 ** kludgey way to guess where aliaskind applies
5979 if (ctype_isMutable (s->type)
5980 && !ctype_isPointer (arr->type)
5981 && !alkind_isStatic (arr->aliaskind)
5982 && !alkind_isStack (arr->aliaskind)) /* evs - 2000-06-20: don't pass stack allocation to members */
5984 s->aliaskind = arr->aliaskind;
5988 s->aliaskind = AK_UNKNOWN;
5991 sRef_setTypeState (s);
5994 if (sRef_isObserver (arr))
5996 s->expkind = XO_OBSERVER;
6000 /*@exposed@*/ sRef sRef_buildArrayFetch (/*@exposed@*/ sRef arr)
6004 if (!sRef_isValid (arr)) {
6005 /*@-nullret@*/ return arr /*@=nullret@*/;
6008 if (ctype_isRealPointer (arr->type))
6010 (void) sRef_buildPointer (arr); /* do this to define arr! */
6013 s = sRef_findDerivedArrayFetch (arr, FALSE, 0, FALSE);
6015 if (sRef_isValid (s))
6017 sRef_setExKind (s, sRef_getExKind (arr), g_currentloc);
6024 s->kind = SK_ARRAYFETCH;
6025 s->info = (sinfo) dmalloc (sizeof (*s->info));
6026 s->info->arrayfetch = (ainfo) dmalloc (sizeof (*s->info->arrayfetch));
6027 s->info->arrayfetch->indknown = FALSE;
6028 s->info->arrayfetch->ind = 0;
6029 s->info->arrayfetch->arr = arr;
6030 sRef_setArrayFetchState (s, arr);
6031 s->oaliaskind = s->aliaskind;
6032 s->oexpkind = s->expkind;
6034 if (!context_inProtectVars ())
6036 sRef_addDeriv (arr, s);
6044 sRef_buildArrayFetchKnown (/*@exposed@*/ sRef arr, int i)
6048 if (!sRef_isValid (arr)) {
6049 /*@-nullret@*/ return arr /*@=nullret@*/;
6052 if (ctype_isRealPointer (arr->type))
6054 (void) sRef_buildPointer (arr); /* do this to define arr! */
6057 s = sRef_findDerivedArrayFetch (arr, TRUE, i, FALSE);
6059 if (sRef_isValid (s))
6061 sRef_setExKind (s, sRef_getExKind (arr), g_currentloc);
6068 s->kind = SK_ARRAYFETCH;
6069 s->info = (sinfo) dmalloc (sizeof (*s->info));
6070 s->info->arrayfetch = (ainfo) dmalloc (sizeof (*s->info->arrayfetch));
6071 s->info->arrayfetch->arr = arr;
6072 s->info->arrayfetch->indknown = TRUE;
6073 s->info->arrayfetch->ind = i;
6075 sRef_setArrayFetchState (s, arr);
6077 s->oaliaskind = s->aliaskind;
6078 s->oexpkind = s->expkind;
6079 sRef_addDeriv (arr, s);
6086 ** sets everything except for defstate
6090 sRef_setPartsFromUentry (sRef s, uentry ue)
6093 llassert (sRef_isValid (s));
6095 s->aliaskind = alkind_derive (s->aliaskind, uentry_getAliasKind (ue));
6096 s->oaliaskind = s->aliaskind;
6098 if (s->expkind == XO_UNKNOWN)
6100 s->expkind = uentry_getExpKind (ue);
6103 s->oexpkind = s->expkind;
6105 if (s->nullstate == NS_UNKNOWN)
6107 s->nullstate = sRef_getNullState (uentry_getSref (ue));
6110 if (s->aliaskind == AK_IMPONLY
6111 && (sRef_isExposed (s) || sRef_isObserver (s)))
6113 s->oaliaskind = s->aliaskind = AK_IMPDEPENDENT;
6119 sRef_setStateFromAbstractUentry (sRef s, uentry ue)
6121 llassert (sRef_isValid (s));
6123 sRef_setPartsFromUentry (s, ue);
6125 s->aliaskind = alkind_derive (s->aliaskind, uentry_getAliasKind (ue));
6126 s->oaliaskind = s->aliaskind;
6128 if (s->expkind == XO_UNKNOWN)
6130 s->expkind = uentry_getExpKind (ue);
6133 s->oexpkind = s->expkind;
6137 sRef_setStateFromUentry (sRef s, uentry ue)
6141 llassert (sRef_isValid (s));
6143 sRef_setPartsFromUentry (s, ue);
6145 defstate = uentry_getDefState (ue);
6147 if (sstate_isKnown (defstate))
6149 s->defstate = defstate;
6158 sRef_buildPointer (/*@exposed@*/ sRef t)
6160 DPRINTF (("build pointer: %s", sRef_unparse (t)));
6162 if (sRef_isInvalid (t)) return sRef_undefined;
6164 if (sRef_isAddress (t))
6166 DPRINTF (("Return ref: %s", sRef_unparse (t->info->ref)));
6167 return (t->info->ref);
6171 sRef s = sRef_findDerivedPointer (t);
6173 DPRINTF (("find derived: %s", sRef_unparse (s)));
6175 if (sRef_isValid (s))
6178 sRef_setExKind (s, sRef_getExKind (t), g_currentloc);
6179 s->oaliaskind = s->aliaskind;
6180 s->oexpkind = s->expkind;
6186 s = sRef_constructPointerAux (t);
6188 DPRINTF (("construct: %s", sRef_unparse (s)));
6190 if (sRef_isValid (s))
6192 sRef_addDeriv (t, s);
6194 s->oaliaskind = s->aliaskind;
6195 s->oexpkind = s->expkind;
6204 sRef_constructPointer (sRef t)
6207 return sRef_buildPointer (t);
6210 static /*@exposed@*/ sRef sRef_constructDerefAux (sRef t, bool isdead)
6212 if (sRef_isValid (t))
6217 ** if there is a derived t[?], return that. Otherwise, *t.
6221 s = sRef_findDerivedArrayFetch (t, FALSE, 0, isdead);
6223 if (sRef_isValid (s))
6229 sRef ret = sRef_constructPointer (t);
6232 ** This is necessary to prevent infinite depth
6233 ** in checking complete destruction.
6239 /* ret->defstate = SS_UNKNOWN; */
6250 return sRef_undefined;
6254 sRef sRef_constructDeref (sRef t)
6256 return sRef_constructDerefAux (t, FALSE);
6259 sRef sRef_constructDeadDeref (sRef t)
6261 return sRef_constructDerefAux (t, TRUE);
6265 sRef_constructPointerAux (/*@notnull@*/ /*@exposed@*/ sRef t)
6267 sRef s = sRef_new ();
6272 s->info = (sinfo) dmalloc (sizeof (*s->info));
6275 if (ctype_isRealAP (rt))
6277 s->type = ctype_baseArrayPtr (rt);
6280 st = ctype_realType (s->type);
6283 if (t->defstate == SS_UNDEFINED)
6285 s->defstate = SS_UNUSEABLE;
6287 else if ((t->defstate == SS_ALLOCATED) && !ctype_isSU (st))
6289 s->defstate = SS_UNDEFINED;
6293 s->defstate = t->defstate;
6296 if (t->aliaskind == AK_LOCAL || t->aliaskind == AK_FRESH)
6298 s->aliaskind = AK_LOCAL;
6302 s->aliaskind = AK_UNKNOWN;
6305 sRef_setExKind (s, sRef_getExKind (t), fileloc_undefined);
6306 sRef_setTypeState (s);
6308 s->oaliaskind = s->aliaskind;
6309 s->oexpkind = s->expkind;
6314 bool sRef_hasDerived (sRef s)
6316 return (sRef_isValid (s) && !sRefSet_isEmpty (s->deriv));
6320 sRef_clearDerived (sRef s)
6322 if (sRef_isValid (s))
6324 sRefSet_clear (s->deriv);
6329 sRef_clearDerivedComplete (sRef s)
6332 if (sRef_isValid (s))
6334 sRef base = sRef_getBaseSafe (s);
6336 while (sRef_isValid (base))
6338 sRefSet_clear (base->deriv);
6339 base = sRef_getBaseSafe (base);
6342 sRefSet_clear (s->deriv);
6347 sRef_makePointer (sRef s)
6350 sRef res = sRef_buildPointer (s);
6352 DPRINTF (("Res: %s", sRef_unparse (res)));
6357 ** &a[] => a (this is for out params)
6361 sRef_makeAnyArrayFetch (/*@exposed@*/ sRef arr)
6364 if (sRef_isAddress (arr))
6366 return (arr->info->ref);
6370 return (sRef_buildArrayFetch (arr));
6375 sRef_makeArrayFetch (sRef arr)
6377 return (sRef_buildArrayFetch (arr));
6381 sRef_makeArrayFetchKnown (sRef arr, int i)
6383 return (sRef_buildArrayFetchKnown (arr, i));
6387 sRef_makeField (sRef rec, /*@dependent@*/ cstring f)
6390 ret = sRef_buildField (rec, f);
6395 sRef_makeNCField (sRef rec, /*@dependent@*/ cstring f)
6397 return (sRef_buildNCField (rec, f));
6401 sRef_unparseKindName (sRef s)
6405 if (s == sRef_undefined) return cstring_makeLiteral ("<invalid>");
6407 s = sRef_fixConj (s);
6412 if (sRef_isLocalVar (s))
6414 result = cstring_makeLiteral ("Variable");
6418 result = cstring_makeLiteral ("Undef global");
6422 result = cstring_makeLiteral ("Out parameter");
6425 if (sRef_isAnyParam (s->info->arrayfetch->arr))
6427 result = cstring_makeLiteral ("Out parameter");
6429 else if (sRef_isIndexKnown (s))
6431 result = cstring_makeLiteral ("Array element");
6435 result = cstring_makeLiteral ("Value");
6439 if (sRef_isAnyParam (s->info->ref))
6441 result = cstring_makeLiteral ("Out parameter");
6445 result = cstring_makeLiteral ("Value");
6449 result = cstring_makeLiteral ("Value");
6452 result = cstring_makeLiteral ("Field");
6455 result = cstring_makeLiteral ("Object");
6457 case SK_UNCONSTRAINED:
6458 result = cstring_makeLiteral ("<anything>");
6467 result = cstring_makeLiteral ("<unknown>");
6470 result = cstring_makeLiteral ("<conj>");
6473 result = cstring_makeLiteral ("Storage");
6481 sRef_unparseKindNamePlain (sRef s)
6485 if (s == sRef_undefined) return cstring_makeLiteral ("<invalid>");
6487 s = sRef_fixConj (s);
6492 if (sRef_isLocalVar (s))
6494 result = cstring_makeLiteral ("Variable");
6498 result = cstring_makeLiteral ("Global");
6502 result = cstring_makeLiteral ("Parameter");
6505 if (sRef_isAnyParam (s->info->arrayfetch->arr))
6507 result = cstring_makeLiteral ("Parameter");
6509 else if (sRef_isIndexKnown (s))
6511 result = cstring_makeLiteral ("Array element");
6515 result = cstring_makeLiteral ("Value");
6519 if (sRef_isAnyParam (s->info->ref))
6521 result = cstring_makeLiteral ("Parameter");
6525 result = cstring_makeLiteral ("Value");
6529 result = cstring_makeLiteral ("Value");
6532 result = cstring_makeLiteral ("Field");
6535 result = cstring_makeLiteral ("Object");
6538 result = cstring_makeLiteral ("Storage");
6540 case SK_UNCONSTRAINED:
6541 result = cstring_makeLiteral ("<anything>");
6550 result = cstring_makeLiteral ("<unknown>");
6553 result = cstring_makeLiteral ("<conj>");
6565 sRef_copyState (sRef s1, sRef s2)
6567 if (sRef_isValid (s1) && sRef_isValid (s2))
6569 s1->defstate = s2->defstate;
6571 s1->nullstate = s2->nullstate;
6572 s1->nullinfo = alinfo_update (s1->nullinfo, s2->nullinfo);
6574 /* start modifications */
6575 s1->bufinfo.bufstate = s2->bufinfo.bufstate;
6576 s1->bufinfo.len = s2->bufinfo.len;
6577 s1->bufinfo.size = s2->bufinfo.size;
6578 /* end modifications */
6580 s1->aliaskind = s2->aliaskind;
6581 s1->aliasinfo = alinfo_update (s1->aliasinfo, s2->aliasinfo);
6583 s1->expkind = s2->expkind;
6584 s1->expinfo = alinfo_update (s1->expinfo, s2->expinfo);
6586 s1->safe = s2->safe;
6591 sRef_makeNew (ctype ct, sRef t, cstring name)
6593 sRef s = sRef_new ();
6598 llassert (sRef_isValid (t));
6599 s->defstate = t->defstate;
6601 s->aliaskind = t->aliaskind;
6602 s->oaliaskind = s->aliaskind;
6604 s->nullstate = t->nullstate;
6606 s->expkind = t->expkind;
6607 s->oexpkind = s->expkind;
6609 s->info = (sinfo) dmalloc (sizeof (*s->info));
6610 s->info->fname = name;
6612 /* start modifications */
6613 s->bufinfo.bufstate = t->bufinfo.bufstate;
6614 /* end modifications */
6620 sRef_makeType (ctype ct)
6622 sRef s = sRef_new ();
6627 s->defstate = SS_UNKNOWN;
6628 s->aliaskind = AK_UNKNOWN;
6629 s->nullstate = NS_UNKNOWN;
6630 /* start modification */
6631 s->bufinfo.bufstate = BB_NOTNULLTERMINATED;
6632 /* end modification */
6635 if (ctype_isUA (ct))
6637 typeId uid = ctype_typeId (ct);
6638 uentry ue = usymtab_getTypeEntrySafe (uid);
6640 if (uentry_isValid (ue))
6642 sRef_mergeStateQuiet (s, uentry_getSref (ue));
6646 s->oaliaskind = s->aliaskind;
6647 s->oexpkind = s->expkind;
6653 sRef_makeConst (ctype ct)
6655 sRef s = sRef_new ();
6660 s->defstate = SS_UNKNOWN;
6661 s->aliaskind = AK_UNKNOWN;
6662 s->nullstate = NS_UNKNOWN;
6663 /* start modification */
6664 s->bufinfo.bufstate = BB_NULLTERMINATED;
6665 /* end modification */
6668 if (ctype_isUA (ct))
6670 typeId uid = ctype_typeId (ct);
6671 uentry te = usymtab_getTypeEntrySafe (uid);
6673 if (uentry_isValid (te))
6675 sRef_mergeStateQuiet (s, uentry_getSref (te));
6680 s->oaliaskind = s->aliaskind;
6681 s->oexpkind = s->expkind;
6686 bool sRef_hasName (sRef s)
6688 if (sRef_isInvalid (s))
6697 uentry u = usymtab_getRefQuiet (s->info->cvar->lexlevel,
6698 s->info->cvar->index);
6699 return (uentry_hasName (u));
6703 uentry u = uentryList_getN (context_getParams (),
6706 return (uentry_hasName (u));
6714 sRef_sameName (sRef s1, sRef s2)
6716 if (sRef_isInvalid (s1))
6718 return sRef_isInvalid (s2);
6721 if (sRef_isInvalid (s2))
6729 if (s2->kind == SK_CVAR)
6731 return (s1->info->cvar->lexlevel == s2->info->cvar->lexlevel
6732 && s1->info->cvar->index == s2->info->cvar->index);
6734 else if (s2->kind == SK_PARAM)
6736 if (context_inFunctionLike ())
6738 uentry u1 = usymtab_getRefQuiet (s1->info->cvar->lexlevel,
6739 s1->info->cvar->index);
6740 uentry u2 = uentryList_getN (context_getParams (),
6743 return (cstring_equalFree (uentry_getName (u1),
6744 uentry_getName (u2)));
6757 if (s2->kind == SK_PARAM)
6759 return (s1->info->paramno == s2->info->paramno);
6761 else if (s2->kind == SK_CVAR)
6763 if (context_inFunctionLike ())
6765 uentry u1 = uentryList_getN (context_getParams (),
6767 uentry u2 = usymtab_getRefQuiet (s2->info->cvar->lexlevel,
6768 s2->info->cvar->index);
6771 return (cstring_equalFree (uentry_getName (u1),
6772 uentry_getName (u2)));
6785 case SK_UNCONSTRAINED:
6789 if (s2->kind == SK_ARRAYFETCH)
6791 if (bool_equal (s1->info->arrayfetch->indknown,
6792 s2->info->arrayfetch->indknown))
6794 if (!s1->info->arrayfetch->indknown
6795 || (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind))
6797 return sRef_sameName (s1->info->arrayfetch->arr,
6798 s2->info->arrayfetch->arr);
6805 if (s2->kind == SK_FIELD)
6807 if (cstring_equal (s1->info->field->field,
6808 s2->info->field->field))
6810 return sRef_sameName (s1->info->field->rec,
6811 s2->info->field->rec);
6820 if (s2->kind == s1->kind)
6822 return sRef_sameName (s1->info->ref,
6830 return sRef_sameName (sRef_getConjA (s1), s2);
6834 return (s2->kind == SK_UNKNOWN);
6837 if (s2->kind == s1->kind)
6839 return (ctype_equal (s1->type, s2->type));
6844 if (s2->kind == SK_SPECIAL)
6846 return (s1->info->spec == s2->info->spec);
6850 return (s2->kind == SK_RESULT);
6858 sRef_fixOuterRef (/*@returned@*/ sRef s)
6860 sRef root = sRef_getRootBase (s);
6862 if (sRef_isCvar (root))
6864 uentry ue = usymtab_getRefQuiet (root->info->cvar->lexlevel,
6865 root->info->cvar->index);
6867 if (uentry_isValid (ue))
6869 sRef uref = uentry_getSref (ue);
6870 sRef sr = sRef_fixBase (s, uref);
6876 llcontbug (message ("sRef_fixOuterRef: undefined: %q", sRef_unparseDebug (s)));
6885 sRef_storeState (sRef s)
6887 if (sRef_isInvalid (s)) return;
6889 s->oaliaskind = s->aliaskind;
6890 s->oexpkind = s->expkind;
6893 static void sRef_resetStateAux (sRef s, /*@unused@*/ fileloc loc)
6895 sRef_resetState (s);
6899 sRef_resetState (sRef s)
6901 bool changed = FALSE;
6902 if (sRef_isInvalid (s)) return;
6905 if (s->oaliaskind == AK_KILLREF && !sRef_isParam (s))
6908 ** killref is used in a kludgey way, to save having to add
6909 ** another alias kind (see usymtab_handleParams)
6912 if (s->expkind != s->oexpkind)
6915 s->expkind = s->oexpkind;
6920 if (s->expkind != s->oexpkind)
6923 s->expkind = s->oexpkind;
6926 if (s->aliaskind != s->oaliaskind
6927 && s->aliaskind != AK_REFCOUNTED
6928 && s->aliaskind != AK_REFS)
6931 s->aliaskind = s->oaliaskind;
6937 sRef_clearDerived (s);
6943 sRef_resetStateComplete (sRef s)
6945 sRef_innerAliasSetComplete (sRef_resetStateAux, s, fileloc_undefined);
6949 sRef_fixBase (/*@returned@*/ sRef s, /*@returned@*/ sRef base)
6951 sRef tmp = sRef_undefined;
6954 if (sRef_isInvalid (s)) return s;
6955 if (sRef_isInvalid (base)) return base;
6965 tmp = sRef_fixBase (s->info->arrayfetch->arr, base);
6967 if (s->info->arrayfetch->indknown)
6969 ret = sRef_makeArrayFetchKnown (tmp, s->info->arrayfetch->ind);
6973 ret = sRef_makeArrayFetch (tmp);
6977 tmp = sRef_fixBase (s->info->field->rec, base);
6978 ret = sRef_buildNCField (tmp, s->info->field->field);
6981 tmp = sRef_fixBase (s->info->ref, base);
6982 ret = sRef_makePointer (tmp);
6985 tmp = sRef_fixBase (s->info->ref, base);
6986 ret = sRef_makeAddress (tmp);
6992 tmp = sRef_fixBase (s->info->conj->a, base);
6993 tmpb = sRef_fixBase (s->info->conj->b, base);
6995 ret = sRef_makeConj (tmp, tmpb);
7004 static /*@exposed@*/ sRef
7005 sRef_fixDirectBase (sRef s, sRef base)
7010 if (sRef_isInvalid (s))
7012 return sRef_undefined;
7018 if (s->info->arrayfetch->indknown)
7020 ret = sRef_makeArrayFetchKnown (base, s->info->arrayfetch->ind);
7024 ret = sRef_makeArrayFetch (base);
7028 ret = sRef_buildNCField (base, s->info->field->field);
7031 ret = sRef_makePointer (base);
7034 ret = sRef_makeAddress (base);
7040 tmpa = sRef_fixDirectBase (s->info->conj->a, base);
7041 tmpb = sRef_fixDirectBase (s->info->conj->b, base);
7043 ret = sRef_makeConj (tmpa, tmpb);
7049 sRef_copyState (ret, s);
7054 sRef_isAllocIndexRef (sRef s)
7056 return (sRef_isArrayFetch (s) && !(s->info->arrayfetch->indknown)
7057 && sRef_isAllocated (s->info->arrayfetch->arr));
7061 sRef_showRefLost (sRef s)
7063 if (sRef_hasAliasInfoLoc (s))
7065 llgenindentmsg (cstring_makeLiteral ("Original reference lost"),
7066 sRef_getAliasInfoLoc (s));
7071 sRef_showRefKilled (sRef s)
7073 if (sRef_hasStateInfoLoc (s))
7075 llgenindentmsg (message ("Storage %q released",
7076 sRef_unparse (s)), sRef_getStateInfoLoc (s));
7081 sRef_showStateInconsistent (sRef s)
7083 if (sRef_hasStateInfoLoc (s))
7086 (message ("Storage %qbecomes inconsistent (released on one branch)",
7087 sRef_unparseOpt (s)),
7088 sRef_getStateInfoLoc (s));
7093 sRef_showStateInfo (sRef s)
7095 if (sRef_hasStateInfoLoc (s))
7097 if (s->defstate == SS_DEAD)
7100 (message ("Storage %qis released", sRef_unparseOpt (s)),
7101 sRef_getStateInfoLoc (s));
7103 else if (s->defstate == SS_ALLOCATED || s->defstate == SS_DEFINED)
7106 (message ("Storage %qis %s", sRef_unparseOpt (s),
7107 sstate_unparse (s->defstate)),
7108 sRef_getStateInfoLoc (s));
7110 else if (s->defstate == SS_UNUSEABLE)
7113 (message ("Storage %qbecomes inconsistent (clauses merge with"
7114 "%qreleased on one branch)",
7115 sRef_unparseOpt (s),
7116 sRef_unparseOpt (s)),
7117 sRef_getStateInfoLoc (s));
7121 llgenindentmsg (message ("Storage %qbecomes %s",
7122 sRef_unparseOpt (s),
7123 sstate_unparse (s->defstate)),
7124 sRef_getStateInfoLoc (s));
7130 sRef_showExpInfo (sRef s)
7132 if (sRef_hasExpInfoLoc (s))
7134 llgenindentmsg (message ("Storage %qbecomes %s", sRef_unparseOpt (s),
7135 exkind_unparse (s->expkind)),
7136 sRef_getExpInfoLoc (s));
7141 sRef_showNullInfo (sRef s)
7143 if (sRef_hasNullInfoLoc (s) && sRef_isKnown (s))
7145 switch (s->nullstate)
7149 fileloc loc = sRef_getNullInfoLoc (s);
7151 if (fileloc_isDefined (loc) && !fileloc_isLib (loc))
7154 (message ("Storage %qbecomes null", sRef_unparseOpt (s)),
7161 fileloc loc = sRef_getNullInfoLoc (s);
7163 if (fileloc_isDefined (loc) && !fileloc_isLib (loc))
7165 llgenindentmsg (message ("Storage %qbecomes null", sRef_unparseOpt (s)),
7173 (message ("Storage %qmay become null", sRef_unparseOpt (s)),
7174 sRef_getNullInfoLoc (s));
7179 (message ("Storage %qbecomes not null", sRef_unparseOpt (s)),
7180 sRef_getNullInfoLoc (s));
7184 (message ("Storage %qnull state becomes unknown",
7185 sRef_unparseOpt (s)),
7186 sRef_getNullInfoLoc (s));
7195 (message ("<error case> Storage %q becomes %s",
7197 nstate_unparse (s->nullstate)),
7198 sRef_getNullInfoLoc (s));
7206 sRef_showAliasInfo (sRef s)
7208 if (sRef_hasAliasInfoLoc (s))
7210 if (sRef_isFresh (s))
7213 (message ("Fresh storage %qallocated", sRef_unparseOpt (s)),
7214 sRef_getAliasInfoLoc (s));
7218 if (!sRef_isRefCounted (s))
7221 (message ("Storage %qbecomes %s",
7222 sRef_unparseOpt (s),
7223 alkind_unparse (sRef_getAliasKind (s))),
7224 sRef_getAliasInfoLoc (s));
7231 sRef_mergeNullState (sRef s, nstate n)
7233 if (sRef_isValid (s))
7239 if (n != old && n != NS_UNKNOWN)
7243 s->nullinfo = alinfo_updateLoc (s->nullinfo, g_currentloc);
7248 llbuglit ("sRef_mergeNullState: invalid");
7253 sRef_possiblyNull (sRef s)
7255 if (sRef_isValid (s))
7257 if (s->nullstate == NS_ABSNULL)
7259 ctype rct = ctype_realType (s->type);
7261 if (ctype_isAbstract (rct))
7267 if (ctype_isUser (rct))
7269 uentry ue = usymtab_getTypeEntry (ctype_typeId (rct));
7271 return (nstate_possiblyNull
7272 (sRef_getNullState (uentry_getSref (ue))));
7282 return nstate_possiblyNull (s->nullstate);
7290 sRef_getScopeName (sRef s)
7292 sRef base = sRef_getRootBase (s);
7294 if (sRef_isRealGlobal (base))
7296 return (cstring_makeLiteralTemp ("Global"));
7298 else if (sRef_isFileStatic (base))
7300 return (cstring_makeLiteralTemp ("Static"));
7304 return (cstring_makeLiteralTemp ("Local"));
7309 sRef_unparseScope (sRef s)
7311 sRef base = sRef_getRootBase (s);
7313 if (sRef_isRealGlobal (base))
7315 return (cstring_makeLiteralTemp ("global"));
7317 else if (sRef_isFileStatic (base))
7319 return (cstring_makeLiteralTemp ("file static"));
7328 sRef_getScope (sRef s)
7330 llassert (sRef_isValid (s));
7332 if (sRef_isCvar (s))
7334 return s->info->cvar->lexlevel;
7336 else if (sRef_isParam (s))
7347 sRef_isDead (sRef s)
7349 return (sRef_isValid (s) && (s)->defstate == SS_DEAD);
7353 sRef_isDeadStorage (sRef s)
7355 if (sRef_isValid (s))
7357 if (s->defstate == SS_DEAD
7358 || s->defstate == SS_UNUSEABLE
7359 || s->defstate == SS_UNDEFINED
7360 || s->defstate == SS_UNKNOWN)
7366 return (sRef_isDefinitelyNull (s));
7376 sRef_isPossiblyDead (sRef s)
7378 return (sRef_isValid (s) && s->defstate == SS_HOFFA);
7381 bool sRef_isStateLive (sRef s)
7383 if (sRef_isValid (s))
7385 sstate ds = s->defstate;
7387 return (!(ds == SS_UNDEFINED
7389 || ds == SS_UNUSEABLE
7390 || ds == SS_HOFFA));
7399 bool sRef_isStateUndefined (sRef s)
7401 return ((sRef_isValid(s)) && ((s)->defstate == SS_UNDEFINED));
7404 bool sRef_isJustAllocated (sRef s)
7406 if (sRef_isAllocated (s))
7408 sRefSet_allElements (s->deriv, el)
7410 if (!(sRef_isStateUndefined (el) || sRef_isUnuseable (el)))
7414 } end_sRefSet_allElements ;
7423 sRef_isAllocatedStorage (sRef s)
7425 if (sRef_isValid (s) && ynm_toBoolStrict (sRef_isReadable (s)))
7427 return (ctype_isVisiblySharable (sRef_getType (s)));
7436 sRef_isUnuseable (sRef s)
7438 return ((sRef_isValid(s)) && ((s)->defstate == SS_UNUSEABLE));
7442 sRef_perhapsNull (sRef s)
7444 if (sRef_isValid (s))
7446 if (s->nullstate == NS_ABSNULL)
7448 ctype rct = ctype_realType (s->type);
7450 if (ctype_isAbstract (rct))
7456 if (ctype_isUser (rct))
7458 uentry ue = usymtab_getTypeEntry (ctype_typeId (rct));
7460 return (nstate_perhapsNull
7461 (sRef_getNullState (uentry_getSref (ue))));
7471 return nstate_perhapsNull (s->nullstate);
7479 ** definitelyNull --- called when TRUE is good
7483 sRef_definitelyNull (sRef s)
7485 return (sRef_isValid (s)
7486 && (s->nullstate == NS_DEFNULL || s->nullstate == NS_CONSTNULL));
7490 ** based on sRef_similar
7494 sRef_setDerivNullState (sRef set, sRef guide, nstate ns)
7496 if (sRef_isValid (set))
7498 sRef deriv = sRef_getDeriv (set, guide);
7500 if (sRef_isValid (deriv))
7502 deriv->nullstate = ns;
7507 static /*@exposed@*/ sRef
7508 sRef_getDeriv (/*@returned@*/ /*@notnull@*/ sRef set, sRef guide)
7510 llassert (sRef_isValid (set));
7511 llassert (sRef_isValid (guide));
7513 switch (guide->kind)
7516 llassert (set->kind == SK_CVAR);
7521 llassert (set->kind == guide->kind);
7522 llassert (set->info->paramno == guide->info->paramno);
7528 if (set->kind == SK_ARRAYFETCH
7529 && (sRef_similar (set->info->arrayfetch->arr,
7530 guide->info->arrayfetch->arr)))
7536 return (sRef_makeAnyArrayFetch
7537 (sRef_getDeriv (set, guide->info->arrayfetch->arr)));
7542 if (set->kind == SK_PTR && sRef_similar (set->info->ref, guide->info->ref))
7548 return (sRef_makePointer (sRef_getDeriv (set, guide->info->ref)));
7553 if ((set->kind == SK_FIELD &&
7554 (sRef_similar (set->info->field->rec, guide->info->field->rec) &&
7555 cstring_equal (set->info->field->field, guide->info->field->field))))
7561 return (sRef_makeField (sRef_getDeriv (set, guide->info->field->rec),
7562 guide->info->field->field));
7566 if ((set->kind == SK_ADR) && sRef_similar (set->info->ref, guide->info->ref))
7572 return (sRef_makeAddress (sRef_getDeriv (set, guide->info->ref)));
7577 return sRef_undefined;
7581 case SK_UNCONSTRAINED:
7589 return sRef_undefined;
7596 ** sRef_aliasCheckPred
7598 ** A confusing but spiffy function:
7600 ** Calls predf (s, e, text, <alias>) on s and all of s's aliases
7601 ** (unless checkAliases (s) is FALSE).
7603 ** For alias calls, calls as
7604 ** predf (alias, e, text, s)
7608 sRef_aliasCheckPred (bool (predf) (sRef, exprNode, sRef, exprNode),
7609 /*@null@*/ bool (checkAliases) (sRef),
7610 sRef s, exprNode e, exprNode err)
7612 bool error = (*predf)(s, e, sRef_undefined, err);
7615 if (checkAliases != NULL && !(checkAliases (s)))
7617 /* don't check aliases */
7621 sRefSet aliases = usymtab_allAliases (s);
7624 sRefSet_realElements (aliases, current)
7626 if (sRef_isValid (current))
7628 if (!sRef_similar (current, s)
7629 || (error && sRef_sameName (current, s)))
7631 (void) (*predf)(current, e, s, err);
7634 } end_sRefSet_realElements;
7636 sRefSet_free (aliases);
7641 ** return TRUE iff predf (s) is true for s or any alias of s
7645 sRef_aliasCheckSimplePred (sRefTest predf, sRef s)
7656 aliases = usymtab_allAliases (s);
7658 sRefSet_realElements (aliases, current)
7660 if (sRef_isValid (current))
7662 sRef cref = sRef_updateSref (current);
7664 /* Whoa! a very kludgey way to make sure the right sref is used
7665 ** where there is a conditional symbol table. I am beginning
7666 ** to think that having a conditional symbol table wasn't such
7673 sRefSet_free (aliases);
7677 } end_sRefSet_realElements;
7679 sRefSet_free (aliases);
7685 sRef_aliasCompleteSimplePred (bool (predf) (sRef), sRef s)
7688 bool result = FALSE;
7691 aliases = usymtab_allAliases (s);
7693 if ((*predf)(s)) result = TRUE;
7696 sRefSet_realElements (aliases, current)
7698 if (sRef_isValid (current))
7700 current = sRef_updateSref (current);
7701 if ((*predf)(current)) result = TRUE;
7703 } end_sRefSet_realElements;
7705 sRefSet_free (aliases);
7710 sRef_aliasSetComplete (void (predf) (sRef, fileloc), sRef s, fileloc loc)
7714 aliases = usymtab_allAliases (s);
7718 sRefSet_realElements (aliases, current)
7720 if (sRef_isValid (current))
7722 current = sRef_updateSref (current);
7723 ((*predf)(current, loc));
7725 } end_sRefSet_realElements;
7727 sRefSet_free (aliases);
7731 sRef_aliasSetCompleteParam (void (predf) (sRef, alkind, fileloc), sRef s,
7732 alkind kind, fileloc loc)
7737 if (sRef_isDeep (s))
7739 aliases = usymtab_allAliases (s);
7743 aliases = usymtab_aliasedBy (s);
7746 (*predf)(s, kind, loc);
7748 sRefSet_realElements (aliases, current)
7750 if (sRef_isValid (current))
7752 current = sRef_updateSref (current);
7753 ((*predf)(current, kind, loc));
7755 } end_sRefSet_realElements;
7757 sRefSet_free (aliases);
7761 sRef_innerAliasSetComplete (void (predf) (sRef, fileloc), sRef s, fileloc loc)
7767 if (!sRef_isValid (s)) return;
7771 ** Type equivalence checking is necessary --- there might be casting.
7778 case SK_UNCONSTRAINED:
7783 inner = s->info->ref;
7784 aliases = usymtab_allAliases (inner);
7785 ct = sRef_getType (inner);
7788 sRefSet_realElements (aliases, current)
7790 if (sRef_isValid (current))
7792 current = sRef_updateSref (current);
7794 if (ctype_equal (ct, sRef_getType (current)))
7796 sRef ptr = sRef_makePointer (current);
7798 ((*predf)(ptr, loc));
7801 } end_sRefSet_realElements;
7803 sRefSet_free (aliases);
7806 inner = s->info->arrayfetch->arr;
7807 aliases = usymtab_allAliases (inner);
7808 ct = sRef_getType (inner);
7810 sRefSet_realElements (aliases, current)
7812 if (sRef_isValid (current))
7814 current = sRef_updateSref (current);
7816 if (ctype_equal (ct, sRef_getType (current)))
7819 if (s->info->arrayfetch->indknown)
7821 sRef af = sRef_makeArrayFetchKnown (current, s->info->arrayfetch->ind);
7823 ((*predf)(af, loc));
7827 sRef af = sRef_makeArrayFetch (current);
7829 ((*predf)(af, loc));
7833 } end_sRefSet_realElements;
7835 sRefSet_free (aliases);
7838 inner = s->info->field->rec;
7839 aliases = usymtab_allAliases (inner);
7840 ct = sRef_getType (inner);
7843 sRefSet_realElements (aliases, current)
7845 if (sRef_isValid (current))
7847 current = sRef_updateSref (current);
7849 if (ctype_equal (ct, sRef_getType (current)))
7851 sRef f = sRef_makeField (current, s->info->field->field);
7856 } end_sRefSet_realElements;
7858 sRefSet_free (aliases);
7861 sRef_innerAliasSetComplete (predf, s->info->conj->a, loc);
7862 sRef_innerAliasSetComplete (predf, s->info->conj->b, loc);
7879 sRef_innerAliasSetCompleteParam (void (predf) (sRef, sRef), sRef s, sRef t)
7885 if (!sRef_isValid (s)) return;
7889 ** Type equivalence checking is necessary --- there might be casting.
7896 case SK_UNCONSTRAINED:
7901 inner = s->info->ref;
7902 aliases = usymtab_allAliases (inner);
7903 ct = sRef_getType (inner);
7906 sRefSet_realElements (aliases, current)
7908 if (sRef_isValid (current))
7910 current = sRef_updateSref (current);
7912 if (ctype_equal (ct, sRef_getType (current)))
7914 sRef ptr = sRef_makePointer (current);
7919 } end_sRefSet_realElements;
7921 sRefSet_free (aliases);
7924 inner = s->info->arrayfetch->arr;
7925 aliases = usymtab_allAliases (inner);
7926 ct = sRef_getType (inner);
7928 sRefSet_realElements (aliases, current)
7930 if (sRef_isValid (current))
7932 current = sRef_updateSref (current);
7934 if (ctype_equal (ct, sRef_getType (current)))
7937 if (s->info->arrayfetch->indknown)
7939 sRef af = sRef_makeArrayFetchKnown (current, s->info->arrayfetch->ind);
7945 sRef af = sRef_makeArrayFetch (current);
7951 } end_sRefSet_realElements;
7953 sRefSet_free (aliases);
7956 inner = s->info->field->rec;
7957 aliases = usymtab_allAliases (inner);
7958 ct = sRef_getType (inner);
7961 sRefSet_realElements (aliases, current)
7963 if (sRef_isValid (current))
7965 current = sRef_updateSref (current);
7967 if (ctype_equal (ct, sRef_getType (current)))
7969 sRef f = sRef_makeField (current, s->info->field->field);
7974 } end_sRefSet_realElements;
7976 sRefSet_free (aliases);
7979 sRef_innerAliasSetCompleteParam (predf, s->info->conj->a, t);
7980 sRef_innerAliasSetCompleteParam (predf, s->info->conj->b, t);
7996 static void sRef_combineExKinds (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other)
7998 exkind a1 = sRef_getExKind (res);
7999 exkind a2 = sRef_getExKind (other);
8001 if (a1 == a2 || a2 == XO_UNKNOWN)
8005 else if (a1 == XO_UNKNOWN)
8007 res->expinfo = alinfo_update (res->expinfo, other->expinfo);
8012 res->expkind = XO_OBSERVER;
8017 ** Currently, this is a very ad hoc implementation, with lots of fixes to
8018 ** make real code work okay. I need to come up with some more general
8019 ** rules or principles here.
8023 sRef_combineAliasKindsError (/*@notnull@*/ sRef res,
8024 /*@notnull@*/ sRef other,
8025 clause cl, fileloc loc)
8027 bool hasError = FALSE;
8028 alkind ares = sRef_getAliasKind (res);
8029 alkind aother = sRef_getAliasKind (other);
8031 if (alkind_isDependent (ares))
8033 if (aother == AK_KEPT)
8035 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8036 res->aliaskind = AK_KEPT;
8040 if (aother == AK_LOCAL || aother == AK_STATIC
8041 || alkind_isTemp (aother))
8043 res->aliaskind = AK_DEPENDENT;
8047 else if (alkind_isDependent (aother))
8049 if (ares == AK_KEPT)
8051 res->aliaskind = AK_KEPT;
8055 if (ares == AK_LOCAL || ares == AK_STATIC || alkind_isTemp (ares))
8057 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8058 res->aliaskind = AK_DEPENDENT;
8062 else if ((ares == AK_LOCAL || ares == AK_UNIQUE
8063 || ares == AK_STATIC || alkind_isTemp (ares))
8064 && sRef_isFresh (other))
8067 ** cases like: if (s == NULL) s = malloc...;
8068 ** don't generate errors
8071 if (usymtab_isAltProbablyDeepNull (res))
8073 res->aliaskind = ares;
8080 else if ((aother == AK_LOCAL || aother == AK_UNIQUE
8081 || aother == AK_STATIC || alkind_isTemp (aother))
8082 && sRef_isFresh (res))
8085 ** cases like: if (s == NULL) s = malloc...;
8086 ** don't generate errors
8089 if (usymtab_isProbableDeepNull (other))
8091 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8092 res->aliaskind = aother;
8099 else if (ares == AK_NEWREF && aother == AK_REFCOUNTED
8100 && sRef_isConst (other))
8102 res->aliaskind = AK_NEWREF;
8104 else if (aother == AK_NEWREF && ares == AK_REFCOUNTED
8105 && sRef_isConst (res))
8107 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8108 res->aliaskind = AK_NEWREF;
8110 else if (sRef_isLocalVar (res)
8111 && ((ares == AK_KEPT && aother == AK_LOCAL)
8112 || (aother == AK_KEPT && ares == AK_LOCAL)))
8114 res->aliaskind = AK_KEPT;
8123 if (sRef_isThroughArrayFetch (res))
8126 (FLG_BRANCHSTATE, FLG_STRICTBRANCHSTATE,
8128 ("Clauses exit with %q possibly referencing %s storage %s, "
8131 alkind_unparse (aother),
8132 clause_nameTaken (cl),
8133 alkind_unparse (ares),
8134 clause_nameAlternate (cl)),
8137 sRef_showAliasInfo (res);
8138 sRef_showAliasInfo (other);
8139 res->aliaskind = AK_ERROR;
8143 if (ares == AK_KEPT || aother == AK_KEPT)
8145 sRef_maybeKill (res, loc);
8153 message ("Clauses exit with %q referencing %s storage %s, "
8156 alkind_unparse (aother),
8157 clause_nameTaken (cl),
8158 alkind_unparse (ares),
8159 clause_nameAlternate (cl)),
8162 sRef_showAliasInfo (res);
8163 sRef_showAliasInfo (other);
8165 res->aliaskind = AK_ERROR;
8169 res->aliaskind = (sRef_isLocalVar (res) ? AK_LOCAL : AK_UNKNOWN);
8174 sRef_combineAliasKinds (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other,
8175 clause cl, fileloc loc)
8177 alkind ares = sRef_getAliasKind (res);
8178 alkind aother = sRef_getAliasKind (other);
8180 if (alkind_equal (ares, aother)
8181 || aother == AK_UNKNOWN
8182 || aother == AK_ERROR)
8184 ; /* keep current state */
8186 else if (sRef_isDead (res) || sRef_isDead (other))
8188 /* dead error reported (or storage is dead) */
8189 res ->aliaskind = AK_ERROR;
8191 else if (ares == AK_UNKNOWN || ares == AK_ERROR
8192 || sRef_isStateUndefined (res))
8194 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8195 res->aliaskind = aother;
8197 else if (sRef_isStateUndefined (other))
8201 else if (((ares == AK_UNIQUE || alkind_isTemp (ares))
8202 && aother == AK_LOCAL)
8203 || ((aother == AK_UNIQUE || alkind_isTemp (aother))
8204 && ares == AK_LOCAL))
8206 if (ares != AK_LOCAL)
8208 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8211 res->aliaskind = AK_LOCAL;
8213 else if ((ares == AK_OWNED && aother == AK_FRESH)
8214 || (aother == AK_OWNED && ares == AK_FRESH))
8216 if (ares != AK_FRESH)
8218 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8221 res->aliaskind = AK_FRESH;
8223 else if ((ares == AK_KEEP && aother == AK_FRESH) ||
8224 (aother == AK_KEEP && ares == AK_FRESH))
8226 if (ares != AK_KEEP)
8228 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8231 res->aliaskind = AK_KEEP;
8233 else if ((ares == AK_LOCAL && aother == AK_STACK) ||
8234 (aother == AK_LOCAL && ares == AK_STACK))
8236 if (ares != AK_STACK)
8238 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8241 res->aliaskind = AK_STACK;
8243 else if ((ares == AK_LOCAL
8244 && (aother == AK_OWNED && sRef_isLocalVar (other)))
8245 || (aother == AK_LOCAL
8246 && (ares == AK_OWNED && sRef_isLocalVar (res))))
8248 if (ares != AK_LOCAL)
8250 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8253 res->aliaskind = AK_LOCAL;
8255 else if ((ares == AK_FRESH && alkind_isOnly (aother))
8256 || (aother == AK_FRESH && alkind_isOnly (ares)))
8258 res->aliaskind = AK_FRESH;
8260 else if ((aother == AK_FRESH && sRef_definitelyNull (res))
8261 || (ares == AK_FRESH && sRef_definitelyNull (other)))
8263 if (ares != AK_FRESH)
8265 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8266 res->aliaskind = AK_FRESH;
8269 else if ((sRef_isFresh (res) && sRef_isConst (other))
8270 || (sRef_isFresh (other) && sRef_isConst (res)))
8273 ** for NULL constantants
8277 if (!sRef_isFresh (res))
8279 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8282 res->aliaskind = AK_FRESH;
8284 else if ((alkind_isStatic (aother) && sRef_isConst (res))
8285 || (alkind_isStatic (ares) && sRef_isConst (other)))
8287 if (!alkind_isStatic (ares))
8289 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8290 res->aliaskind = AK_STATIC;
8295 sRef_combineAliasKindsError (res, other, cl, loc);
8299 static void sRef_combineDefState (/*@notnull@*/ sRef res,
8300 /*@notnull@*/ sRef other)
8302 sstate s1 = res->defstate;
8303 sstate s2 = other->defstate;
8306 if (s1 == s2 || s2 == SS_UNKNOWN)
8310 else if (s1 == SS_UNKNOWN)
8319 if (s2 == SS_DEFINED)
8325 llcontbuglit ("ssfixed: not implemented");
8334 flip = (s2 != SS_DEFINED);
8352 res->definfo = alinfo_update (res->definfo, other->definfo);
8357 extern /*@notnull@*/ sRef sRef_getConjA (sRef s)
8360 llassert (sRef_isConj (s));
8362 ret = s->info->conj->a;
8363 llassert (ret != NULL);
8367 extern /*@notnull@*/ sRef sRef_getConjB (sRef s)
8370 llassert (sRef_isConj (s));
8372 ret = s->info->conj->b;
8373 llassert (ret != NULL);
8377 extern /*@exposed@*/ sRef sRef_makeArrow (sRef s, /*@dependent@*/ cstring f)
8382 p = sRef_makePointer (s);
8383 ret = sRef_makeField (p, f);
8387 extern /*@exposed@*/ sRef sRef_buildArrow (sRef s, cstring f)
8392 p = sRef_buildPointer (s);
8393 ret = sRef_buildField (p, f);
8398 static /*@null@*/ sinfo sinfo_copy (/*@notnull@*/ sRef s)
8405 ret = (sinfo) dmalloc (sizeof (*ret));
8406 ret->cvar = (cref) dmalloc (sizeof (*ret->cvar));
8407 ret->cvar->lexlevel = s->info->cvar->lexlevel;
8408 ret->cvar->index = s->info->cvar->index;
8412 ret = (sinfo) dmalloc (sizeof (*ret));
8413 ret->paramno = s->info->paramno;
8417 ret = (sinfo) dmalloc (sizeof (*ret));
8418 ret->arrayfetch = (ainfo) dmalloc (sizeof (*ret->arrayfetch));
8419 ret->arrayfetch->indknown = s->info->arrayfetch->indknown;
8420 ret->arrayfetch->ind = s->info->arrayfetch->ind;
8421 ret->arrayfetch->arr = s->info->arrayfetch->arr;
8425 ret = (sinfo) dmalloc (sizeof (*ret));
8426 ret->field = (fldinfo) dmalloc (sizeof (*ret->field));
8427 ret->field->rec = s->info->field->rec;
8428 ret->field->field = s->info->field->field;
8432 ret = (sinfo) dmalloc (sizeof (*ret));
8433 ret->object = s->info->object;
8440 ret = (sinfo) dmalloc (sizeof (*ret));
8441 ret->ref = s->info->ref;
8445 ret = (sinfo) dmalloc (sizeof (*ret));
8446 ret->conj = (cjinfo) dmalloc (sizeof (*ret->conj));
8447 ret->conj->a = s->info->conj->a;
8448 ret->conj->b = s->info->conj->b;
8451 ret = (sinfo) dmalloc (sizeof (*ret));
8452 ret->spec = s->info->spec;
8454 case SK_UNCONSTRAINED:
8456 ret = (sinfo) dmalloc (sizeof (*ret));
8457 ret->fname = s->info->fname;
8463 llassertprint (s->info == NULL, ("s = %s", sRef_unparse (s)));
8471 static /*@null@*/ sinfo sinfo_fullCopy (/*@notnull@*/ sRef s)
8476 ** Since its a full copy, only storage is assigned
8477 ** to dependent fields.
8484 ret = (sinfo) dmalloc (sizeof (*ret));
8485 ret->cvar = (cref) dmalloc (sizeof (*ret->cvar));
8486 ret->cvar->lexlevel = s->info->cvar->lexlevel;
8487 ret->cvar->index = s->info->cvar->index;
8491 ret = (sinfo) dmalloc (sizeof (*ret));
8492 ret->paramno = s->info->paramno;
8496 ret = (sinfo) dmalloc (sizeof (*ret));
8497 ret->arrayfetch = (ainfo) dmalloc (sizeof (*ret->arrayfetch));
8498 ret->arrayfetch->indknown = s->info->arrayfetch->indknown;
8499 ret->arrayfetch->ind = s->info->arrayfetch->ind;
8500 ret->arrayfetch->arr = sRef_saveCopy (s->info->arrayfetch->arr);
8504 ret = (sinfo) dmalloc (sizeof (*ret));
8505 ret->field = (fldinfo) dmalloc (sizeof (*ret->field));
8506 ret->field->rec = sRef_saveCopy (s->info->field->rec);
8507 ret->field->field = s->info->field->field;
8511 ret = (sinfo) dmalloc (sizeof (*ret));
8512 ret->object = s->info->object;
8519 ret = (sinfo) dmalloc (sizeof (*ret));
8520 ret->ref = sRef_saveCopy (s->info->ref);
8524 ret = (sinfo) dmalloc (sizeof (*ret));
8525 ret->conj = (cjinfo) dmalloc (sizeof (*ret->conj));
8526 ret->conj->a = sRef_saveCopy (s->info->conj->a);
8527 ret->conj->b = sRef_saveCopy (s->info->conj->b);
8530 ret = (sinfo) dmalloc (sizeof (*ret));
8531 ret->spec = s->info->spec;
8534 case SK_UNCONSTRAINED:
8535 ret = (sinfo) dmalloc (sizeof (*ret));
8536 ret->fname = s->info->fname;
8542 llassert (s->info == NULL);
8552 sinfo_update (/*@notnull@*/ /*@exposed@*/ sRef res,
8553 /*@notnull@*/ /*@exposed@*/ sRef other)
8555 llassert (res->kind == other->kind);
8560 res->info->cvar->lexlevel = other->info->cvar->lexlevel;
8561 res->info->cvar->index = other->info->cvar->index;
8565 res->info->paramno = other->info->paramno;
8569 res->info->arrayfetch->indknown = other->info->arrayfetch->indknown;
8570 res->info->arrayfetch->ind = other->info->arrayfetch->ind;
8571 res->info->arrayfetch->arr = other->info->arrayfetch->arr;
8575 res->info->field->rec = other->info->field->rec;
8576 res->info->field->field = other->info->field->field;
8580 res->info->object = other->info->object;
8587 res->info->ref = other->info->ref;
8591 res->info->conj->a = other->info->conj->a;
8592 res->info->conj->b = other->info->conj->b;
8596 res->info->spec = other->info->spec;
8600 case SK_UNCONSTRAINED:
8601 res->info->fname = other->info->fname;
8608 llassert (res->info == NULL);
8613 static void sinfo_free (/*@special@*/ /*@temp@*/ /*@notnull@*/ sRef s)
8614 /*@uses s->kind, s->info@*/
8615 /*@releases s->info@*/
8620 sfree (s->info->cvar);
8627 sfree (s->info->arrayfetch);
8631 sfree (s->info->field);
8644 sfree (s->info->conj);
8647 case SK_UNCONSTRAINED:
8660 bool sRef_isNSLocalVar (sRef s)
8662 if (sRef_isLocalVar (s))
8664 uentry ue = sRef_getUentry (s);
8666 return (!uentry_isStatic (ue));
8674 bool sRef_isLocalVar (sRef s)
8676 if (sRef_isValid(s))
8678 return (s->kind == SK_CVAR
8679 && (s->info->cvar->lexlevel > fileScope));
8685 bool sRef_isRealLocalVar (sRef s)
8687 if (sRef_isValid(s))
8689 if (s->kind == SK_CVAR)
8691 if (s->info->cvar->lexlevel == functionScope)
8693 uentry ue = sRef_getUentry (s);
8695 if (uentry_isAnyParam (ue)
8696 || uentry_isRefParam (ue))
8707 return (s->info->cvar->lexlevel > functionScope);
8715 bool sRef_isLocalParamVar (sRef s)
8717 if (sRef_isValid(s))
8719 return (s->kind == SK_PARAM
8720 || (s->kind == SK_CVAR
8721 && (s->info->cvar->lexlevel > fileScope)));
8727 static speckind speckind_fromInt (int i)
8730 llassert (i >= SR_NOTHING && i <= SR_SYSTEM);
8733 return ((speckind) i);
8736 void sRef_combineNullState (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other)
8738 nstate n1 = res->nullstate;
8739 nstate n2 = other->nullstate;
8743 if (n1 == n2 || n2 == NS_UNKNOWN)
8749 /* note: n2 is not unknown or defnull */
8753 case NS_ERROR: nn = NS_ERROR; break;
8754 case NS_UNKNOWN: flip = TRUE; nn = n2; break;
8755 case NS_POSNULL: break;
8756 case NS_DEFNULL: nn = NS_POSNULL; break;
8757 case NS_RELNULL: break;
8759 if (n2 == NS_MNOTNULL)
8770 if (n2 == NS_NOTNULL)
8789 res->nullinfo = alinfo_update (res->nullinfo, other->nullinfo);
8792 res->nullstate = nn;
8795 cstring sRef_nullMessage (sRef s)
8797 llassert (sRef_isValid (s));
8799 switch (s->nullstate)
8803 return (cstring_makeLiteralTemp ("null"));
8805 return (cstring_makeLiteralTemp ("possibly null"));
8810 /*@observer@*/ cstring sRef_ntMessage (sRef s)
8812 llassert (sRef_isValid (s));
8814 switch (s->nullstate)
8818 return (cstring_makeLiteralTemp ("not nullterminated"));
8820 return (cstring_makeLiteralTemp ("possibly non-nullterminated"));
8827 sRef sRef_fixResultType (/*@returned@*/ sRef s, ctype typ, uentry ue)
8829 sRef tmp = sRef_undefined;
8832 llassert (sRef_isValid (s));
8843 tmp = sRef_fixResultType (s->info->arrayfetch->arr, typ, ue);
8845 ct = ctype_realType (sRef_getType (tmp));
8848 if (ctype_isKnown (ct))
8850 if (ctype_isAP (ct))
8859 ("Special clause indexes non-array (%t): *%q",
8860 ct, sRef_unparse (s->info->arrayfetch->arr)),
8861 uentry_whereLast (ue));
8865 tmp = sRef_fixResultType (s->info->arrayfetch->arr, typ, ue);
8867 if (s->info->arrayfetch->indknown)
8869 ret = sRef_makeArrayFetchKnown (tmp, s->info->arrayfetch->ind);
8873 ret = sRef_makeArrayFetch (tmp);
8879 sRef rec = sRef_fixResultType (s->info->field->rec, typ, ue);
8880 ctype ct = ctype_realType (sRef_getType (rec));
8882 if (ctype_isKnown (ct))
8884 if (ctype_isSU (ct))
8886 if (uentry_isValid (uentryList_lookupField (ctype_getFields (ct),
8887 s->info->field->field)))
8896 ("Special clause accesses non-existent field of result: %q.%s",
8897 sRef_unparse (rec), s->info->field->field),
8898 uentry_whereLast (ue));
8906 ("Special clause accesses field of non-struct or union result (%t): %q.%s",
8907 ct, sRef_unparse (rec), s->info->field->field),
8908 uentry_whereLast (ue));
8912 ret = sRef_makeField (tmp, s->info->field->field);
8918 tmp = sRef_fixResultType (s->info->ref, typ, ue);
8920 ct = ctype_realType (sRef_getType (tmp));
8922 if (ctype_isKnown (ct))
8924 if (ctype_isAP (ct))
8933 ("Special clause dereferences non-pointer (%t): *%q",
8934 ct, sRef_unparse (s->info->ref)),
8935 uentry_whereLast (ue));
8939 ret = sRef_makePointer (tmp);
8946 ("Special clause uses & operator (not allowed): &%q", sRef_unparse (s->info->ref)),
8947 uentry_whereLast (ue));
8956 bool sRef_isOnly (sRef s)
8958 return (sRef_isValid(s) && alkind_isOnly (s->aliaskind));
8961 bool sRef_isDependent (sRef s)
8963 return (sRef_isValid(s) && alkind_isDependent (s->aliaskind));
8966 bool sRef_isOwned (sRef s)
8968 return (sRef_isValid (s) && (s->aliaskind == AK_OWNED));
8971 bool sRef_isKeep (sRef s)
8973 return (sRef_isValid (s) && (s->aliaskind == AK_KEEP));
8976 bool sRef_isTemp (sRef s)
8978 return (sRef_isValid (s) && alkind_isTemp (s->aliaskind));
8981 bool sRef_isLocalState (sRef s)
8983 return (sRef_isValid (s) && (s->aliaskind == AK_LOCAL));
8986 bool sRef_isUnique (sRef s)
8988 return (sRef_isValid (s) && (s->aliaskind == AK_UNIQUE));
8991 bool sRef_isShared (sRef s)
8993 return (sRef_isValid (s) && (s->aliaskind == AK_SHARED));
8996 bool sRef_isExposed (sRef s)
8998 return (sRef_isValid (s) && (s->expkind == XO_EXPOSED));
9001 bool sRef_isObserver (sRef s)
9003 return (sRef_isValid (s) && (s->expkind == XO_OBSERVER));
9006 bool sRef_isFresh (sRef s)
9008 return (sRef_isValid (s) && (s->aliaskind == AK_FRESH));
9011 bool sRef_isDefinitelyNull (sRef s)
9013 return (sRef_isValid (s) && (s->nullstate == NS_DEFNULL
9014 || s->nullstate == NS_CONSTNULL));
9017 bool sRef_isAllocated (sRef s)
9019 return (sRef_isValid (s) && (s->defstate == SS_ALLOCATED));
9022 bool sRef_isStack (sRef s)
9024 return (sRef_isValid (s) && (s->aliaskind == AK_STACK));
9027 extern bool sRef_isNotNull (sRef s)
9029 return (sRef_isValid(s) && (s->nullstate == NS_MNOTNULL
9030 || s->nullstate == NS_NOTNULL));
9033 /* start modifications */
9034 struct _bbufinfo sRef_getNullTerminatedState (sRef p_s) {
9035 struct _bbufinfo BUFSTATE_UNKNOWN;
9036 BUFSTATE_UNKNOWN.bufstate = BB_NOTNULLTERMINATED;
9037 BUFSTATE_UNKNOWN.size = 0;
9038 BUFSTATE_UNKNOWN.len = 0;
9040 if (sRef_isValid(p_s))
9041 return p_s->bufinfo;
9042 return BUFSTATE_UNKNOWN;
9045 void sRef_setNullTerminatedState(sRef p_s) {
9046 if(sRef_isValid (p_s)) {
9047 p_s->bufinfo.bufstate = BB_NULLTERMINATED;
9049 llfatalbug( message("sRef_setNT passed a invalid sRef\n"));
9054 void sRef_setPossiblyNullTerminatedState(sRef p_s) {
9055 if( sRef_isValid (p_s)) {
9056 p_s->bufinfo.bufstate = BB_POSSIBLYNULLTERMINATED;
9058 llfatalbug( message("sRef_setPossNT passed a invalid sRef\n"));
9062 void sRef_setNotNullTerminatedState(sRef p_s) {
9063 if( sRef_isValid (p_s)) {
9064 p_s->bufinfo.bufstate = BB_NOTNULLTERMINATED;
9066 llfatalbug( message("sRef_unsetNT passed a invalid sRef\n"));
9070 void sRef_setLen(sRef p_s, int len) {
9071 if( sRef_isValid (p_s) && sRef_isNullTerminated(p_s)) {
9072 p_s->bufinfo.len = len;
9074 llfatalbug( message("sRef_setLen passed a invalid sRef\n"));
9079 void sRef_setSize(sRef p_s, int size) {
9080 if( sRef_isValid(p_s)) {
9081 p_s->bufinfo.size = size;
9083 llfatalbug( message("sRef_setSize passed a invalid sRef\n"));
9087 void sRef_resetLen(sRef p_s) {
9088 if (sRef_isValid (p_s)) {
9089 p_s->bufinfo.len = 0;
9091 llfatalbug (message ("sRef_setLen passed an invalid sRef\n"));
9095 /*drl7x 11/28/2000 */
9097 bool sRef_isFixedArray (sRef p_s) {
9099 c = sRef_getType (p_s);
9100 return ( ctype_isFixedArray (c) );
9103 int sRef_getArraySize (sRef p_s) {
9105 llassert (sRef_isFixedArray(p_s) );
9107 c = sRef_getType (p_s);
9109 return (ctype_getArraySize (c) );