2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2000 University of Virginia,
4 ** Massachusetts Institute of Technology
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ** General Public License for more details.
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
20 ** For information on lclint: lclint-request@cs.virginia.edu
21 ** To report a bug: lclint-bug@cs.virginia.edu
22 ** For more information: http://lclint.cs.virginia.edu
28 ** storeRef's are kept in allRefs for each function scope, and all are
29 ** free'd at the end of the function. This relies on the constraint that
30 ** no storeRef created while checking a function is used outside that
33 ** storeRefs in the file and global scopes are free'd by the uentry.
37 # include "lclintMacros.nf"
39 # include "exprChecks.h"
40 # include "aliasChecks.h"
41 # include "sRefTable.h"
42 # include "structNames.h"
45 ** Predicate functions that evaluate both arguments in order.
49 # define OR(a,b) (a ? (b, TRUE) : b)
52 # define AND(a,b) (a ? b : (b, FALSE))
54 static bool sRef_isDerived (sRef p_s) /*@*/ ;
56 static /*@exposed@*/ sRef sRef_fixDirectBase (sRef p_s, sRef p_base)
57 /*@modifies p_base@*/ ;
59 static bool sRef_isAllocatedStorage (sRef p_s) /*@*/ ;
60 static void sRef_setNullErrorLoc (sRef p_s, fileloc) /*@*/ ;
63 sRef_aliasSetComplete (void (p_predf) (sRef, fileloc), sRef p_s, fileloc p_loc)
66 static int sRef_depth (sRef p_s) /*@*/ ;
69 sRef_innerAliasSetComplete (void (p_predf) (sRef, fileloc), sRef p_s,
74 sRef_innerAliasSetCompleteParam (void (p_predf) (sRef, sRef), sRef p_s, sRef p_t)
78 sRef_aliasSetCompleteParam (void (p_predf) (sRef, alkind, fileloc), sRef p_s,
79 alkind p_kind, fileloc p_loc)
82 static speckind speckind_fromInt (int p_i);
83 static bool sRef_equivalent (sRef p_s1, sRef p_s2);
84 static bool sRef_isDeepUnionField (sRef p_s);
85 static void sRef_addDeriv (/*@notnull@*/ sRef p_s, /*@notnull@*/ sRef p_t);
86 static sRef sRef_makeResultType (ctype p_ct) /*@*/ ;
87 static bool sRef_checkModify (sRef p_s, sRefSet p_sl) /*@*/ ;
89 static bool skind_isSimple (skind sk)
93 case SK_PARAM: case SK_CVAR: case SK_CONST:
94 case SK_OBJECT: case SK_UNKNOWN: case SK_NEW:
101 static void sinfo_free (/*@special@*/ /*@temp@*/ /*@notnull@*/ sRef p_s)
102 /*@uses p_s->kind, p_s->info@*/
103 /*@releases p_s->info@*/ ;
105 static /*@null@*/ sinfo sinfo_copy (/*@notnull@*/ sRef p_s) /*@*/ ;
106 static void sRef_setPartsFromUentry (sRef p_s, uentry p_ue)
108 static bool checkDeadState (/*@notnull@*/ sRef p_el, bool p_tbranch, fileloc p_loc);
109 static sRef sRef_constructPointerAux (/*@notnull@*/ /*@exposed@*/ sRef p_t) /*@*/ ;
112 sRef_combineExKinds (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other)
113 /*@modifies p_res@*/ ;
116 sRef_combineAliasKinds (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other,
117 clause p_cl, fileloc p_loc)
118 /*@modifies p_res@*/ ;
121 sRef_combineNullState (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other)
122 /*@modifies p_res@*/ ;
125 sRef_combineDefState (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other)
126 /*@modifies p_res@*/ ;
128 static void sRef_setStateFromAbstractUentry (sRef p_s, uentry p_ue)
132 sinfo_update (/*@notnull@*/ /*@exposed@*/ sRef p_res,
133 /*@notnull@*/ /*@exposed@*/ sRef p_other);
134 static /*@only@*/ alinfo alinfo_makeRefLoc (/*@exposed@*/ sRef p_ref, fileloc p_loc);
135 static void sRef_setDefinedAux (sRef p_s, fileloc p_loc, bool p_clear)
137 static void sRef_setDefinedNoClear (sRef p_s, fileloc p_loc)
139 static void sRef_setStateAux (sRef p_s, sstate p_ss, fileloc p_loc)
142 static /*@exposed@*/ sRef
143 sRef_buildNCField (/*@exposed@*/ sRef p_rec, /*@exposed@*/ cstring p_f);
146 sRef_mergeStateAux (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other,
147 clause p_cl, bool p_opt, fileloc p_loc,
149 /*@modifies p_res, p_other@*/ ;
151 static /*@null@*/ sinfo sinfo_fullCopy (/*@notnull@*/ sRef p_s);
152 static bool sRef_doModify (sRef p_s, sRefSet p_sl) /*@modifies p_s@*/ ;
153 static bool sRef_doModifyVal (sRef p_s, sRefSet p_sl) /*@modifies p_s@*/;
154 static bool sRef_checkModifyVal (sRef p_s, sRefSet p_sl) /*@*/ ;
156 static /*@only@*/ sRefSet
157 sRef_mergeDerivs (/*@only@*/ sRefSet p_res, sRefSet p_other,
158 bool p_opt, clause p_cl, fileloc p_loc);
160 static /*@only@*/ sRefSet
161 sRef_mergeUnionDerivs (/*@only@*/ sRefSet p_res,
162 /*@exposed@*/ sRefSet p_other,
163 bool p_opt, clause p_cl, fileloc p_loc);
165 static /*@only@*/ sRefSet
166 sRef_mergePdefinedDerivs (/*@only@*/ sRefSet p_res, sRefSet p_other, bool p_opt,
167 clause p_cl, fileloc p_loc);
169 static /*@only@*/ cstring sRef_unparseWithArgs (sRef p_s, uentryList p_args);
170 static /*@only@*/ cstring sRef_unparseNoArgs (sRef p_s);
172 static /*@exposed@*/ sRef sRef_findDerivedPointer (sRef p_s);
173 static /*@exposed@*/ sRef sRef_findDerivedField (/*@notnull@*/ sRef p_rec, cstring p_f);
174 static /*@exposed@*/ sRef
175 sRef_getDeriv (/*@notnull@*/ /*@returned@*/ sRef p_set, sRef p_guide);
177 static bool inFunction = FALSE;
178 static /*@only@*/ sRefTable allRefs;
180 /* # define DEBUGREFS */
184 static totnsrefs = 0;
185 static maxnsrefs = 0;
186 static ntotrefers = 0;
190 /*@constant null alinfo alinfo_undefined; @*/
191 # define alinfo_undefined ((alinfo) NULL)
193 static /*@only@*/ /*@notnull@*/ alinfo alinfo_makeLoc (fileloc p_loc);
194 static /*@only@*/ alinfo alinfo_copy (alinfo p_a);
196 static /*@checked@*/ bool protectDerivs = FALSE;
198 void sRef_protectDerivs (void) /*@modifies protectDerivs@*/
200 llassert (!protectDerivs);
201 protectDerivs = TRUE;
204 void sRef_clearProtectDerivs (void) /*@modifies protectDerivs@*/
206 llassert (protectDerivs);
207 protectDerivs = FALSE;
211 ** hmmm...here be kind of a hack. This function mysteriously appeared
212 ** in my code, but I'm sure I didn't write it.
216 sRef_isRecursiveField (sRef s)
218 if (sRef_isField (s))
220 if (sRef_depth (s) > 13)
225 fieldname = sRef_getField (s);
226 base = sRef_getBase (s);
228 while (sRef_isValid (base))
230 if (sRef_isField (base))
232 if (cstring_equal (fieldname, sRef_getField (base)))
238 base = sRef_getBaseSafe (base);
247 sRef_addDeriv (/*@notnull@*/ sRef s, /*@notnull@*/ sRef t)
249 if (!context_inProtectVars ()
253 && !sRef_isConst (s))
255 int sd = sRef_depth (s);
256 int td = sRef_depth (t);
263 if (sRef_isGlobal (s))
265 if (context_inFunctionLike ()
266 && ctype_isKnown (sRef_getType (s))
267 && !ctype_isFunction (sRef_getType (s)))
269 globSet g = context_getUsedGlobs ();
271 if (!globSet_member (g, s))
274 ** don't report as a bug
278 (message ("sRef_addDeriv: global variable not in used "
279 "globs: %q / %s / %q",
281 ctype_unparse (sRef_getType (s)),
282 sRefSet_unparse (s->deriv)));
287 s->deriv = sRefSet_insert (s->deriv, t);
293 s->deriv = sRefSet_insert (s->deriv, t);
299 sRef_deepPred (bool (predf) (sRef), sRef s)
301 if (sRef_isValid (s))
303 if ((*predf)(s)) return TRUE;
308 return (sRef_deepPred (predf, s->info->ref));
310 return (sRef_deepPred (predf, s->info->arrayfetch->arr));
312 return (sRef_deepPred (predf, s->info->field->rec));
314 return (sRef_deepPred (predf, s->info->conj->a)
315 || sRef_deepPred (predf, s->info->conj->b));
324 bool sRef_modInFunction (void)
329 void sRef_setStateFromType (sRef s, ctype ct)
331 if (sRef_isValid (s))
333 if (ctype_isUser (ct))
335 sRef_setStateFromUentry
336 (s, usymtab_getTypeEntry (ctype_typeId (ct)));
338 else if (ctype_isAbstract (ct))
340 sRef_setStateFromAbstractUentry
341 (s, usymtab_getTypeEntry (ctype_typeId (ct)));
345 ; /* not a user type */
350 static void sRef_setTypeState (sRef s)
352 if (sRef_isValid (s))
354 sRef_setStateFromType (s, s->type);
358 static void alinfo_free (/*@only@*/ alinfo a)
362 fileloc_free (a->loc);
367 static /*@only@*/ alinfo alinfo_update (/*@only@*/ alinfo old, alinfo newinfo)
369 ** returns an alinfo with the same value as new. May reuse the
370 ** storage of old. (i.e., same effect as copy, but more
376 old = alinfo_copy (newinfo);
378 else if (newinfo == NULL)
385 old->loc = fileloc_update (old->loc, newinfo->loc);
386 old->ref = newinfo->ref;
387 old->ue = newinfo->ue;
393 static /*@only@*/ alinfo alinfo_updateLoc (/*@only@*/ alinfo old, fileloc loc)
397 old = alinfo_makeLoc (loc);
401 old->loc = fileloc_update (old->loc, loc);
402 old->ue = uentry_undefined;
403 old->ref = sRef_undefined;
409 static /*@only@*/ alinfo
410 alinfo_updateRefLoc (/*@only@*/ alinfo old, /*@dependent@*/ sRef ref, fileloc loc)
414 old = alinfo_makeRefLoc (ref, loc);
418 old->loc = fileloc_update (old->loc, loc);
419 old->ue = uentry_undefined;
426 static /*@only@*/ alinfo
427 alinfo_copy (alinfo a)
435 alinfo ret = (alinfo) dmalloc (sizeof (*ret));
437 ret->loc = fileloc_copy (a->loc); /*< should report bug without copy! >*/
446 sRef_hasAliasInfoLoc (sRef s)
448 return (sRef_isValid (s) && (s->aliasinfo != NULL)
449 && (fileloc_isDefined (s->aliasinfo->loc)));
452 static /*@falsenull@*/ bool
453 sRef_hasStateInfoLoc (sRef s)
455 return (sRef_isValid (s) && (s->definfo != NULL)
456 && (fileloc_isDefined (s->definfo->loc)));
459 static /*@falsenull@*/ bool
460 sRef_hasExpInfoLoc (sRef s)
462 return (sRef_isValid (s)
463 && (s->expinfo != NULL) && (fileloc_isDefined (s->expinfo->loc)));
467 sRef_hasNullInfoLoc (sRef s)
469 return (sRef_isValid (s) && (s->nullinfo != NULL)
470 && (fileloc_isDefined (s->nullinfo->loc)));
474 sRef_hasAliasInfoRef (sRef s)
476 return (sRef_isValid (s) && (s->aliasinfo != NULL)
477 && (sRef_isValid (s->aliasinfo->ref)));
480 static /*@observer@*/ fileloc
481 sRef_getAliasInfoLoc (/*@exposed@*/ sRef s)
483 llassert (sRef_isValid (s) && s->aliasinfo != NULL
484 && (fileloc_isDefined (s->aliasinfo->loc)));
485 return (s->aliasinfo->loc);
488 static /*@observer@*/ fileloc
489 sRef_getStateInfoLoc (/*@exposed@*/ sRef s)
491 llassert (sRef_isValid (s) && s->definfo != NULL
492 && (fileloc_isDefined (s->definfo->loc)));
493 return (s->definfo->loc);
496 static /*@observer@*/ fileloc
497 sRef_getExpInfoLoc (/*@exposed@*/ sRef s)
499 llassert (sRef_isValid (s) && s->expinfo != NULL
500 && (fileloc_isDefined (s->expinfo->loc)));
501 return (s->expinfo->loc);
504 static /*@observer@*/ fileloc
505 sRef_getNullInfoLoc (/*@exposed@*/ sRef s)
507 llassert (sRef_isValid (s) && s->nullinfo != NULL
508 && (fileloc_isDefined (s->nullinfo->loc)));
509 return (s->nullinfo->loc);
513 sRef_getAliasInfoRef (/*@exposed@*/ sRef s)
515 llassert (sRef_isValid (s) && s->aliasinfo != NULL);
516 return (s->aliasinfo->ref);
519 static /*@only@*/ /*@notnull@*/ alinfo
520 alinfo_makeLoc (fileloc loc)
522 alinfo ret = (alinfo) dmalloc (sizeof (*ret));
524 ret->loc = fileloc_copy (loc); /* don't need to copy! */
525 ret->ue = uentry_undefined;
526 ret->ref = sRef_undefined;
531 static /*@only@*/ alinfo
532 alinfo_makeRefLoc (/*@exposed@*/ sRef ref, fileloc loc)
534 alinfo ret = (alinfo) dmalloc (sizeof (*ret));
536 ret->loc = fileloc_copy (loc);
538 ret->ue = uentry_undefined;
544 ** This function should be called before new sRefs are created
545 ** somewhere where they will have a lifetime greater than the
546 ** current function scope.
549 void sRef_setGlobalScope ()
551 llassert (inFunction);
555 void sRef_clearGlobalScope ()
557 llassert (!inFunction);
561 static bool oldInFunction = FALSE;
563 void sRef_setGlobalScopeSafe ()
565 oldInFunction = inFunction;
569 void sRef_clearGlobalScopeSafe ()
571 inFunction = oldInFunction;
574 void sRef_enterFunctionScope ()
576 llassert (!inFunction);
577 llassert (sRefTable_isEmpty (allRefs));
581 void sRef_exitFunctionScope ()
586 sRefTable_clear (allRefs);
591 llbuglit ("sRef_exitFunctionScope: not in function");
595 void sRef_destroyMod () /*@globals killed allRefs;@*/
598 llmsg (message ("Live: %d / %d ", nsrefs, totnsrefs));
601 sRefTable_free (allRefs);
605 ** Result of sRef_alloc is dependent since allRefs may
606 ** reference it. It is only if !inFunction.
609 static /*@dependent@*/ /*@out@*/ /*@notnull@*/ sRef
612 sRef s = (sRef) dmalloc (sizeof (*s));
616 allRefs = sRefTable_add (allRefs, s);
622 if (nsrefs >= maxnsrefs)
631 /*@-mustfree@*/ /*@-freshtrans@*/
633 /*@=mustfree@*/ /*@=freshtrans@*/
636 static /*@dependent@*/ /*@notnull@*/ /*@special@*/ sRef
639 /*@post:isnull result->aliasinfo, result->definfo, result->nullinfo,
640 result->expinfo, result->info, result->deriv@*/
642 sRef s = sRef_alloc ();
644 s->kind = SK_UNKNOWN;
647 s->type = ctype_unknown;
648 s->defstate = SS_UNKNOWN;
650 /* start modifications */
651 s->bufinfo.bufstate = BB_NOTNULLTERMINATED;
652 /* end modifications */
654 s->aliaskind = AK_UNKNOWN;
655 s->oaliaskind = AK_UNKNOWN;
657 s->nullstate = NS_UNKNOWN;
659 s->expkind = XO_UNKNOWN;
660 s->oexpkind = XO_UNKNOWN;
662 s->aliasinfo = alinfo_undefined;
663 s->definfo = alinfo_undefined;
664 s->nullinfo = alinfo_undefined;
665 s->expinfo = alinfo_undefined;
668 s->deriv = sRefSet_undefined;
673 static /*@notnull@*/ /*@exposed@*/ sRef
674 sRef_fixConj (/*@notnull@*/ sRef s)
679 s = sRef_getConjA (s);
680 } while (sRef_isConj (s));
682 llassert (sRef_isValid (s));
683 return s; /* don't need to ref */
692 sRef_isExternallyVisibleAux (sRef s)
695 sRef base = sRef_getRootBase (s);
697 if (sRef_isValid (base))
699 res = sRef_isParam (base) || sRef_isGlobal (base) || sRef_isExternal (base);
706 sRef_isExternallyVisible (sRef s)
708 return (sRef_aliasCheckSimplePred (sRef_isExternallyVisibleAux, s));
712 sRef_getBaseUentry (sRef s)
714 sRef base = sRef_getRootBase (s);
715 uentry res = uentry_undefined;
717 if (sRef_isValid (base))
722 res = usymtab_getRefQuiet (paramsScope, base->info->paramno);
726 res = usymtab_getRefQuiet (base->info->cvar->lexlevel,
727 base->info->cvar->index);
739 ** lookup the current uentry corresponding to s, and return the corresponding sRef.
740 ** yuk yuk yuk yuk yuk yuk yuk yuk
744 sRef_updateSref (sRef s)
750 if (!sRef_isValid (s)) return sRef_undefined;
761 case SK_UNCONSTRAINED:
769 uentry ue = sRef_getUentry (s);
771 /* must be raw name! (need the marker) */
772 ue = usymtab_lookupSafe (uentry_rawName (ue));
774 if (uentry_isUndefined (ue))
780 return (uentry_getSref (ue));
784 /* special case if ind known */
785 inner = s->info->arrayfetch->arr;
786 ret = sRef_updateSref (inner);
794 res = sRef_makeArrayFetch (ret);
800 inner = s->info->field->rec;
801 ret = sRef_updateSref (inner);
809 res = (sRef_makeField (ret, s->info->field->field));
814 inner = s->info->ref;
815 ret = sRef_updateSref (inner);
822 res = sRef_makePointer (ret);
828 inner = s->info->ref;
829 ret = sRef_updateSref (inner);
837 res = sRef_makeAddress (ret);
844 sRef innera = s->info->conj->a;
845 sRef innerb = s->info->conj->b;
846 sRef reta = sRef_updateSref (innera);
847 sRef retb = sRef_updateSref (innerb);
849 if (innera == reta && innerb == retb)
855 res = sRef_makeConj (reta, retb);
866 sRef_getUentry (sRef s)
868 llassert (sRef_isValid (s));
873 return (usymtab_getRefQuiet (paramsScope, s->info->paramno));
875 return (usymtab_getRefQuiet (s->info->cvar->lexlevel, s->info->cvar->index));
878 if (sRef_isCvar (s->info->conj->a) || sRef_isParam (s->info->conj->a)
879 || sRef_isConj (s->info->conj->a))
881 return sRef_getUentry (s->info->conj->a);
885 return sRef_getUentry (s->info->conj->b);
890 return uentry_undefined;
896 sRef_getParam (sRef s)
898 llassert (sRef_isValid (s));
899 llassert (s->kind == SK_PARAM);
901 return s->info->paramno;
905 sRef_isModified (sRef s)
907 return (!sRef_isValid (s) || s->modified);
910 void sRef_setModified (sRef s)
912 if (sRef_isValid (s))
917 if (sRef_isRefsField (s))
919 sRef base = sRef_getBase (s);
922 llassert (s->kind == SK_FIELD);
925 if (sRef_isPointer (base))
927 base = sRef_getBase (base);
930 if (sRef_isRefCounted (base))
932 base->aliaskind = AK_NEWREF;
940 ** note: this side-effects sRefSet to set modified to TRUE
941 ** for any sRef similar to s.
945 sRef_canModifyVal (sRef s, sRefSet sl)
947 if (context_getFlag (FLG_MUSTMOD))
949 return (sRef_doModifyVal (s, sl));
953 return (sRef_checkModifyVal (s, sl));
958 sRef_canModify (sRef s, sRefSet sl)
961 if (context_getFlag (FLG_MUSTMOD))
963 return (sRef_doModify (s, sl));
967 return (sRef_checkModify (s, sl));
976 bool sRef_checkModifyVal (sRef s, sRefSet sl)
978 if (sRef_isInvalid (s))
985 case SK_UNCONSTRAINED:
989 if (sRef_isGlobal (s))
991 if (context_checkGlobMod (s))
993 return (sRefSet_member (sl, s));
1003 return (sRefSet_member (sl, s)
1004 || alkind_isOnly (sRef_getOrigAliasKind (s)));
1006 /* special case if ind known */
1007 return (sRefSet_member (sl, s) ||
1008 sRef_checkModifyVal (s->info->arrayfetch->arr, sl));
1010 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->field->rec, sl));
1012 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->ref, sl));
1014 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->ref, sl));
1016 return ((sRef_checkModifyVal (s->info->conj->a, sl)) &&
1017 (sRef_checkModifyVal (s->info->conj->b, sl)));
1028 switch (s->info->spec)
1030 case SR_NOTHING: return TRUE;
1032 if (context_getFlag (FLG_INTERNALGLOBS))
1034 return (sRefSet_member (sl, s));
1040 case SR_SPECSTATE: return TRUE;
1041 case SR_SYSTEM: return (sRefSet_member (sl, s));
1044 case SK_RESULT: BADBRANCH;
1050 ** this should probably be elsewhere...
1052 ** returns TRUE iff sl indicates that s can be modified
1055 static bool sRef_checkModify (sRef s, sRefSet sl)
1057 llassert (sRef_isValid (s));
1061 case SK_UNCONSTRAINED:
1065 if (sRef_isGlobal (s))
1067 if (context_checkGlobMod (s))
1069 return (sRefSet_member (sl, s));
1081 return (sRefSet_member (sl, s) ||
1082 sRef_checkModifyVal (s->info->arrayfetch->arr, sl));
1085 sRef sr = s->info->field->rec;
1087 if (sr->kind == SK_PARAM)
1088 return TRUE; /* structs are copied on call */
1090 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->field->rec, sl));
1096 sm = sRefSet_member (sl, s);
1101 return (sRef_checkModifyVal (s->info->ref, sl));
1104 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->ref, sl));
1106 return ((sRef_checkModify (s->info->conj->a, sl)) &&
1107 (sRef_checkModify (s->info->conj->b, sl)));
1117 switch (s->info->spec)
1119 case SR_NOTHING: return TRUE;
1121 if (context_getFlag (FLG_INTERNALGLOBS))
1123 return (sRefSet_member (sl, s));
1129 case SR_SPECSTATE: return TRUE;
1130 case SR_SYSTEM: return (sRefSet_member (sl, s));
1133 case SK_RESULT: BADBRANCH;
1138 cstring sRef_stateVerb (sRef s)
1140 if (sRef_isDead (s))
1142 return cstring_makeLiteralTemp ("released");
1144 else if (sRef_isKept (s))
1146 return cstring_makeLiteralTemp ("kept");
1148 else if (sRef_isDependent (s))
1150 return cstring_makeLiteralTemp ("dependent");
1158 cstring sRef_stateAltVerb (sRef s)
1160 if (sRef_isDead (s))
1162 return cstring_makeLiteralTemp ("live");
1164 else if (sRef_isKept (s))
1166 return cstring_makeLiteralTemp ("not kept");
1168 else if (sRef_isDependent (s))
1170 return cstring_makeLiteralTemp ("independent");
1179 bool sRef_doModifyVal (sRef s, sRefSet sl)
1181 llassert (sRef_isValid (s));
1186 case SK_UNCONSTRAINED:
1190 if (sRef_isGlobal (s))
1193 if (context_checkGlobMod (s))
1195 return (sRefSet_modifyMember (sl, s));
1199 (void) sRefSet_modifyMember (sl, s);
1209 return (sRefSet_modifyMember (sl, s)
1210 || alkind_isOnly (sRef_getOrigAliasKind (s)));
1212 /* special case if ind known */
1213 /* unconditional OR, need side effect */
1214 return (OR (sRefSet_modifyMember (sl, s),
1215 sRef_doModifyVal (s->info->arrayfetch->arr, sl)));
1217 return (OR (sRefSet_modifyMember (sl, s),
1218 sRef_doModifyVal (s->info->field->rec, sl)));
1220 return (OR (sRefSet_modifyMember (sl, s),
1221 sRef_doModifyVal (s->info->ref, sl)));
1223 return (OR (sRefSet_modifyMember (sl, s),
1224 sRef_doModifyVal (s->info->ref, sl)));
1226 return (AND (sRef_doModifyVal (s->info->conj->a, sl) ,
1227 sRef_doModifyVal (s->info->conj->b, sl)));
1237 switch (s->info->spec)
1239 case SR_NOTHING: return TRUE;
1241 if (context_getFlag (FLG_INTERNALGLOBS))
1243 return (sRefSet_modifyMember (sl, s));
1247 (void) sRefSet_modifyMember (sl, s);
1256 return (sRefSet_modifyMember (sl, s));
1260 case SK_RESULT: BADBRANCH;
1266 ** this should probably be elsewhere...
1268 ** returns TRUE iff sl indicates that s can be modified
1272 bool sRef_doModify (sRef s, sRefSet sl)
1274 llassert (sRef_isValid (s));
1278 case SK_UNCONSTRAINED:
1282 if (sRef_isGlobal (s))
1284 if (context_checkGlobMod (s))
1286 return (sRefSet_modifyMember (sl, s));
1290 (void) sRefSet_modifyMember (sl, s);
1302 return (OR (sRefSet_modifyMember (sl, s),
1303 sRef_doModifyVal (s->info->arrayfetch->arr, sl)));
1306 sRef sr = s->info->field->rec;
1308 if (sr->kind == SK_PARAM)
1310 return TRUE; /* structs are shallow-copied on call */
1313 return (OR (sRefSet_modifyMember (sl, s),
1314 sRef_doModifyVal (s->info->field->rec, sl)));
1318 return (OR (sRefSet_modifyMember (sl, s),
1319 sRef_doModifyVal (s->info->ref, sl)));
1322 return (OR (sRefSet_modifyMember (sl, s),
1323 sRef_doModifyVal (s->info->ref, sl)));
1325 return (AND (sRef_doModify (s->info->conj->a, sl),
1326 (sRef_doModify (s->info->conj->b, sl))));
1337 switch (s->info->spec)
1339 case SR_NOTHING: return TRUE;
1340 case SR_INTERNAL: return TRUE;
1341 case SR_SPECSTATE: return TRUE;
1342 case SR_SYSTEM: return (sRefSet_modifyMember (sl, s));
1345 case SK_RESULT: BADBRANCH;
1350 static /*@exposed@*/ sRef
1351 sRef_leastCommon (/*@exposed@*/ sRef s1, sRef s2)
1353 llassert (sRef_similar (s1, s2));
1355 if (!sRef_isValid (s1)) return s1;
1356 if (!sRef_isValid (s2)) return s1;
1358 sRef_combineDefState (s1, s2);
1359 sRef_combineNullState (s1, s2);
1360 sRef_combineExKinds (s1, s2);
1362 if (s1->aliaskind != s2->aliaskind)
1364 if (s1->aliaskind == AK_UNKNOWN)
1366 s1->aliaskind = s2->aliaskind;
1368 else if (s2->aliaskind == AK_UNKNOWN)
1374 s1->aliaskind = AK_ERROR;
1381 int sRef_compare (sRef s1, sRef s2)
1383 if (s1 == s2) return 0;
1385 if (sRef_isInvalid (s1)) return -1;
1386 if (sRef_isInvalid (s2)) return 1;
1388 INTCOMPARERETURN (s1->kind, s2->kind);
1389 INTCOMPARERETURN (s1->defstate, s2->defstate);
1390 INTCOMPARERETURN (s1->aliaskind, s2->aliaskind);
1392 COMPARERETURN (nstate_compare (s1->nullstate, s2->nullstate));
1397 return (int_compare (s1->info->paramno, s2->info->paramno));
1400 COMPARERETURN (sRef_compare (s1->info->arrayfetch->arr,
1401 s2->info->arrayfetch->arr));
1403 if (s1->info->arrayfetch->indknown && s2->info->arrayfetch->indknown)
1405 return (int_compare (s1->info->arrayfetch->ind,
1406 s2->info->arrayfetch->ind));
1408 if (!s1->info->arrayfetch->indknown && !s2->info->arrayfetch->indknown)
1415 COMPARERETURN (sRef_compare (s1->info->field->rec, s2->info->field->rec));
1417 if (cstring_equal (s1->info->field->field, s2->info->field->field))
1424 return (sRef_compare (s1->info->ref, s2->info->ref));
1426 COMPARERETURN (sRef_compare (s1->info->conj->a, s2->info->conj->a));
1427 return (sRef_compare (s1->info->conj->b, s2->info->conj->b));
1428 case SK_UNCONSTRAINED:
1429 return (cstring_compare (s1->info->fname, s2->info->fname));
1441 return (generic_compare (s1->info->spec, s2->info->spec));
1446 static bool cref_equal (cref c1, cref c2)
1448 return ((c1->lexlevel == c2->lexlevel) &&
1449 (usymId_equal (c1->index, c2->index)));
1453 ** returns true if s1 could be the same storage as s2.
1454 ** i.e., a[?] ~ a[3]. Note its not symmetric ... s1
1455 ** should be more specific.
1459 ** like similar, but matches objects <-> non-objects
1463 sRef_uniqueReference (sRef s)
1465 return (sRef_isFresh (s) || sRef_isUnique (s)
1466 || sRef_isOnly (s) || sRef_isStack (s)
1467 || sRef_isAddress (s));
1471 sRef_similarRelaxedAux (sRef s1, sRef s2)
1475 if (sRef_isUnknownArrayFetch (s1))
1485 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
1487 if (sRef_isConj (s2))
1488 return (sRef_similarRelaxedAux (s1, sRef_getConjA (s2)) ||
1489 sRef_similarRelaxedAux (s1, sRef_getConjB (s2)));
1494 return ((s2->kind == SK_CVAR)
1495 && (cref_equal (s1->info->cvar, s2->info->cvar)));
1497 return ((s2->kind == SK_PARAM)
1498 && (s1->info->paramno == s2->info->paramno));
1500 if (s2->kind == SK_ARRAYFETCH)
1502 if (sRef_similarRelaxedAux (s1->info->arrayfetch->arr,
1503 s2->info->arrayfetch->arr))
1505 if (s1->info->arrayfetch->indknown)
1507 if (s2->info->arrayfetch->indknown)
1509 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
1524 return ((s2->kind == SK_FIELD
1525 && (sRef_similarRelaxedAux (s1->info->field->rec,
1526 s2->info->field->rec)
1527 && cstring_equal (s1->info->field->field,
1528 s2->info->field->field))));
1530 return ((s2->kind == SK_PTR)
1531 && sRef_similarRelaxedAux (s1->info->ref, s2->info->ref));
1533 return ((s2->kind == SK_ADR)
1534 && sRef_similarRelaxedAux (s1->info->ref, s2->info->ref));
1536 return ((sRef_similarRelaxedAux (s1->info->conj->a, s2) ||
1537 (sRef_similarRelaxedAux (s1->info->conj->b, s2))));
1539 return (s1->info->spec == s2->info->spec);
1540 case SK_UNCONSTRAINED:
1541 return (cstring_equal (s1->info->fname, s2->info->fname));
1556 sRef_similarRelaxed (sRef s1, sRef s2)
1562 if (sRef_isThroughArrayFetch (s1))
1572 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
1574 us1 = sRef_uniqueReference (s1);
1575 us2 = sRef_uniqueReference (s2);
1577 if ((s1->kind == SK_EXTERNAL && (s2->kind != SK_EXTERNAL && !us2))
1578 || (s2->kind == SK_EXTERNAL && (s1->kind != SK_EXTERNAL && !us1)))
1581 ** Previously, also:
1582 ** || (sRef_isExposed (s1) && !us2) || (sRef_isExposed (s2) && !us1)) ????
1584 ** No clue why this was there?!
1588 if (sRef_isExposed (s1) && sRef_isCvar (s1))
1590 uentry ue1 = sRef_getUentry (s1);
1592 if (uentry_isRefParam (ue1))
1594 return sRef_similarRelaxedAux (s1, s2);
1598 if (sRef_isExposed (s2) && sRef_isCvar (s2))
1600 uentry ue2 = sRef_getUentry (s2);
1602 if (uentry_isRefParam (ue2))
1604 return sRef_similarRelaxedAux (s1, s2);
1608 return (ctype_match (s1->type, s2->type));
1612 return sRef_similarRelaxedAux (s1, s2);
1617 sRef_similar (sRef s1, sRef s2)
1619 if (s1 == s2) return TRUE;
1620 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
1622 if (sRef_isConj (s2))
1624 return (sRef_similar (s1, sRef_getConjA (s2)) ||
1625 sRef_similar (s1, sRef_getConjB (s2)));
1628 if (sRef_isDerived (s2))
1630 return (sRef_includedBy (s1, s2->info->ref));
1636 return ((s2->kind == SK_CVAR)
1637 && (cref_equal (s1->info->cvar, s2->info->cvar)));
1639 return ((s2->kind == SK_PARAM)
1640 && (s1->info->paramno == s2->info->paramno));
1642 if (s2->kind == SK_ARRAYFETCH)
1644 if (sRef_similar (s1->info->arrayfetch->arr,
1645 s2->info->arrayfetch->arr))
1647 if (s1->info->arrayfetch->indknown)
1649 if (s2->info->arrayfetch->indknown)
1651 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
1666 if (s2->kind == SK_PTR)
1668 if (sRef_similar (s1->info->arrayfetch->arr,
1678 return ((s2->kind == SK_FIELD
1679 && (sRef_similar (s1->info->field->rec,
1680 s2->info->field->rec)
1681 && cstring_equal (s1->info->field->field,
1682 s2->info->field->field))));
1684 if (s2->kind == SK_PTR)
1686 return sRef_similar (s1->info->ref, s2->info->ref);
1690 if (s2->kind == SK_ARRAYFETCH)
1692 if (sRef_similar (s2->info->arrayfetch->arr,
1702 return ((s2->kind == SK_ADR)
1703 && sRef_similar (s1->info->ref, s2->info->ref));
1705 return ((sRef_similar (s1->info->conj->a, s2) ||
1706 (sRef_similar (s1->info->conj->b, s2))));
1708 return (sRef_includedBy (s2, s1->info->ref));
1709 case SK_UNCONSTRAINED:
1710 return (s2->kind == SK_UNCONSTRAINED
1711 && cstring_equal (s1->info->fname, s2->info->fname));
1721 return (s2->kind == SK_SPECIAL
1722 && (s1->info->spec == s2->info->spec));
1725 /*@notreached@*/ DPRINTF (("Fell through for: %s / %s", sRef_unparse (s1), sRef_unparse (s2)));
1730 ** return TRUE iff small can be derived from big.
1732 ** (e.g. x, x.a is includedBy x;
1733 ** x.a is included By x.a;
1737 sRef_includedBy (sRef small, sRef big)
1739 if (small == big) return TRUE;
1740 if (sRef_isInvalid (small) || sRef_isInvalid (big)) return FALSE;
1742 if (sRef_isConj (big))
1743 return (sRef_similar (small, sRef_getConjA (big)) ||
1744 sRef_similar (small, sRef_getConjB (big)));
1746 switch (small->kind)
1750 return (sRef_same (small, big));
1752 if (big->kind == SK_ARRAYFETCH)
1754 if (sRef_same (small->info->arrayfetch->arr, big->info->arrayfetch->arr))
1756 if (small->info->arrayfetch->indknown)
1758 if (big->info->arrayfetch->indknown)
1760 return (small->info->arrayfetch->ind == big->info->arrayfetch->ind);
1773 return (sRef_includedBy (small->info->arrayfetch->arr, big));
1775 if (big->kind == SK_FIELD)
1778 (sRef_same (small->info->field->rec, big->info->field->rec) &&
1779 cstring_equal (small->info->field->field, big->info->field->field));
1783 return (sRef_includedBy (small->info->field->rec, big));
1787 if (big->kind == SK_PTR)
1789 return sRef_same (small->info->ref, big->info->ref);
1793 return (sRef_includedBy (small->info->ref, big));
1797 return ((big->kind == SK_ADR) && sRef_similar (small->info->ref, big->info->ref));
1799 return ((sRef_includedBy (small->info->conj->a, big) ||
1800 (sRef_includedBy (small->info->conj->b, big))));
1802 return (sRef_includedBy (small->info->ref, big));
1803 case SK_UNCONSTRAINED:
1813 switch (small->info->spec)
1815 case SR_NOTHING: return TRUE;
1817 case SR_INTERNAL: return (sRef_isSpecInternalState (big) ||
1818 sRef_isFileStatic (big));
1819 case SR_SYSTEM: return (sRef_isSystemState (big));
1826 ** Same is similar to similar, but not quite the same.
1827 ** same and realSame aren't the same, but they are really similar.
1828 ** similarly, same is the same as same. but realSame is
1829 ** not really the same as same, or similar to similar.
1831 ** Similarly to similar, same checks if two sRefs are the same.
1832 ** The similarities end, however, when same compares arrays
1833 ** with unknown indexes. Similar returns false; same returns true.
1835 ** Similarly to similar and same, realSame is the same as same,
1836 ** except they do not behave the same when face with unknown
1837 ** sRefs. Same thinks they are not the same, but realSame thinks
1843 sRef_realSame (sRef s1, sRef s2)
1845 if (s1 == s2) return TRUE;
1846 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
1851 return ((s2->kind == SK_CVAR) && (cref_equal (s1->info->cvar, s2->info->cvar)));
1853 return ((s2->kind == SK_PARAM) && (s1->info->paramno == s2->info->paramno));
1855 if (s2->kind == SK_ARRAYFETCH)
1857 if (sRef_realSame (s1->info->arrayfetch->arr, s2->info->arrayfetch->arr))
1859 if (s1->info->arrayfetch->indknown && s2->info->arrayfetch->indknown)
1861 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
1863 if (!s1->info->arrayfetch->indknown && !s2->info->arrayfetch->indknown)
1872 return ((s2->kind == SK_FIELD &&
1873 (sRef_realSame (s1->info->field->rec, s2->info->field->rec) &&
1874 cstring_equal (s1->info->field->field, s2->info->field->field))));
1876 return ((s2->kind == SK_PTR) && sRef_realSame (s1->info->ref, s2->info->ref));
1878 return ((s2->kind == SK_ADR) && sRef_realSame (s1->info->ref, s2->info->ref));
1880 return ((sRef_realSame (s1->info->conj->a, s2) ||
1881 (sRef_realSame (s1->info->conj->b, s2))));
1883 return ((s2->kind == SK_OBJECT)
1884 && ctype_match (s1->info->object, s2->info->object));
1886 return ((s2->kind == SK_EXTERNAL)
1887 && sRef_realSame (s1->info->ref, s2->info->ref));
1889 return ((s2->kind == SK_SPECIAL) && s1->info->spec == s2->info->spec);
1891 return ((s2->kind == SK_DERIVED) && sRef_realSame (s1->info->ref, s2->info->ref));
1892 case SK_UNCONSTRAINED:
1893 return ((s2->kind == SK_UNCONSTRAINED)
1894 && (cstring_equal (s1->info->fname, s2->info->fname)));
1900 return TRUE; /* changed this! was false */
1906 ** same is similar to similar, but not quite the same.
1908 ** Similarly to similar, same checks is two sRefs are the same.
1909 ** The similarities end, however, when same compares arrays
1910 ** with unknown indexes. Similar returns false; same returns true.
1914 sRef_same (sRef s1, sRef s2)
1916 if (s1 == s2) return TRUE;
1917 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
1922 return ((s2->kind == SK_CVAR) && (cref_equal (s1->info->cvar, s2->info->cvar)));
1924 return ((s2->kind == SK_PARAM) && (s1->info->paramno == s2->info->paramno));
1926 if (s2->kind == SK_ARRAYFETCH)
1928 llassert (s1->info->field->rec != s1);
1929 if (sRef_same (s1->info->arrayfetch->arr, s2->info->arrayfetch->arr))
1931 if (s1->info->arrayfetch->indknown && s2->info->arrayfetch->indknown)
1933 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
1941 llassert (s1->info->field->rec != s1);
1942 return ((s2->kind == SK_FIELD &&
1943 (sRef_same (s1->info->field->rec, s2->info->field->rec) &&
1944 cstring_equal (s1->info->field->field, s2->info->field->field))));
1948 llassert (s1->info->ref != s1);
1949 return ((s2->kind == SK_PTR) && sRef_same (s1->info->ref, s2->info->ref));
1953 llassert (s1->info->ref != s1);
1954 return ((s2->kind == SK_ADR) && sRef_same (s1->info->ref, s2->info->ref));
1957 llassert (s1->info->conj->a != s1);
1958 llassert (s1->info->conj->b != s1);
1959 return ((sRef_same (s1->info->conj->a, s2)) && /* or or and? */
1960 (sRef_same (s1->info->conj->b, s2)));
1962 return ((s2->kind == SK_SPECIAL) && s1->info->spec == s2->info->spec);
1964 llassert (s1->info->ref != s1);
1965 return ((s2->kind == SK_DERIVED) && sRef_same (s1->info->ref, s2->info->ref));
1967 case SK_UNCONSTRAINED:
1980 ** sort of similar, for use in def/use
1984 sRef_closeEnough (sRef s1, sRef s2)
1986 if (s1 == s2) return TRUE;
1987 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
1992 return (((s2->kind == SK_CVAR) &&
1993 (cref_equal (s1->info->cvar, s2->info->cvar))) ||
1994 (s2->kind == SK_UNCONSTRAINED && s1->info->cvar->lexlevel == 0));
1995 case SK_UNCONSTRAINED:
1996 return (s2->kind == SK_UNCONSTRAINED
1997 || ((s2->kind == SK_CVAR) && (s2->info->cvar->lexlevel == 0)));
1999 return ((s2->kind == SK_PARAM)
2000 && (s1->info->paramno == s2->info->paramno));
2002 if (s2->kind == SK_ARRAYFETCH)
2004 if (sRef_closeEnough (s1->info->arrayfetch->arr, s2->info->arrayfetch->arr))
2006 if (s1->info->arrayfetch->indknown && s2->info->arrayfetch->indknown)
2008 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
2015 return ((s2->kind == SK_FIELD &&
2016 (sRef_closeEnough (s1->info->field->rec, s2->info->field->rec) &&
2017 cstring_equal (s1->info->field->field, s2->info->field->field))));
2019 return ((s2->kind == SK_PTR) && sRef_closeEnough (s1->info->ref, s2->info->ref));
2021 return ((s2->kind == SK_ADR) && sRef_closeEnough (s1->info->ref, s2->info->ref));
2023 return ((s2->kind == SK_DERIVED) && sRef_closeEnough (s1->info->ref, s2->info->ref));
2025 return ((sRef_closeEnough (s1->info->conj->a, s2)) ||
2026 (sRef_closeEnough (s1->info->conj->b, s2)));
2028 return ((s2->kind == SK_SPECIAL) && s1->info->spec == s2->info->spec);
2044 s is an sRef of a formal paramenter in a function call constraint
2045 we trys to return a constraint expression derived from the actual parementer of a function call.
2047 constraintExpr sRef_fixConstraintParam ( sRef s, exprNodeList args)
2051 if (sRef_isInvalid (s))
2052 llfatalbug(("Invalid sRef"));
2058 ce = constraintExpr_makeTermsRef (s);
2065 temp = (sRef_makeField (sRef_fixBaseParam (s->info->field->rec, args),
2066 s->info->field->field));
2067 ce = constraintExpr_makeTermsRef (temp);
2073 temp = (sRef_makePointer (sRef_fixBaseParam (s->info->ref, args)));
2074 ce = constraintExpr_makeTermsRef (temp);
2081 temp = sRef_fixBaseParam (s, args);
2082 ce = constraintExpr_makeTermsRef (temp);
2086 ce = constraintExpr_makeTermsRef (s);
2089 if (exprNodeList_size (args) > s->info->paramno)
2091 exprNode e = exprNodeList_nth (args, s->info->paramno);
2093 if (exprNode_isError (e))
2098 ce = constraintExpr_makeExprNode (e);
2106 llcontbug ((message("Trying to do fixConstraintParam on nonparam, nonglobal: %s for function with arguments %s", sRef_unparse (s), exprNodeList_unparse(args) ) ));
2107 ce = constraintExpr_makeTermsRef (s);
2116 sRef_fixBaseParam (/*@returned@*/ sRef s, exprNodeList args)
2118 if (sRef_isInvalid (s)) return (sRef_undefined);
2122 case SK_UNCONSTRAINED:
2127 if (exprNodeList_size (args) > s->info->paramno)
2129 exprNode e = exprNodeList_nth (args, s->info->paramno);
2131 if (exprNode_isError (e))
2133 return sRef_makeUnknown ();
2136 return (exprNode_getSref (e));
2140 return sRef_makeUnknown ();
2145 if (s->info->arrayfetch->indknown)
2147 return (sRef_makeArrayFetchKnown
2148 (sRef_fixBaseParam (s->info->arrayfetch->arr, args),
2149 s->info->arrayfetch->ind));
2153 return (sRef_makeArrayFetch
2154 (sRef_fixBaseParam (s->info->arrayfetch->arr, args)));
2157 return (sRef_makeField (sRef_fixBaseParam (s->info->field->rec, args),
2158 s->info->field->field));
2161 return (sRef_makePointer (sRef_fixBaseParam (s->info->ref, args)));
2164 return (sRef_makeAddress (sRef_fixBaseParam (s->info->ref, args)));
2167 return (sRef_makeConj (sRef_fixBaseParam (s->info->conj->a, args),
2168 sRef_fixBaseParam (s->info->conj->b, args)));
2184 sRef_undumpGlobal (char **c)
2194 usymId uid = usymId_fromInt (getInt (c));
2200 defstate = sstate_fromInt (getInt (c));
2203 nullstate = nstate_fromInt (getInt (c));
2205 ret = sRef_makeGlobal (uid, ctype_unknown);
2206 ret->nullstate = nullstate;
2207 ret->defstate = defstate;
2213 speckind sk = speckind_fromInt (i);
2217 case SR_NOTHING: return (sRef_makeNothing ());
2218 case SR_INTERNAL: return (sRef_makeInternalState ());
2219 case SR_SPECSTATE: return (sRef_makeSpecState ());
2220 case SR_SYSTEM: return (sRef_makeSystemState ());
2225 return sRef_undefined;
2227 return sRef_makeUnknown ();
2229 return sRef_makeUnknown ();
2231 llfatalerror (message ("sRef_undumpGlobal: bad line: %s",
2232 cstring_fromChars (*c)));
2238 sRef_undump (char **c)
2247 return (sRef_makeGlobal (usymId_fromInt (getInt (c)), ctype_unknown));
2249 return (sRef_makeParam (getInt (c), ctype_unknown));
2251 return (sRef_makeResultType (ctype_undump (c)));
2254 if ((**c >= '0' && **c <= '9') || **c == '-')
2257 sRef arr = sRef_undump (c);
2258 sRef ret = sRef_buildArrayFetchKnown (arr, i);
2264 sRef arr = sRef_undump (c);
2265 sRef ret = sRef_buildArrayFetch (arr);
2272 cstring fname = cstring_undefined;
2277 fname = cstring_appendChar (fname, **c);
2282 ret = sRef_buildField (sRef_undump (c), fname);
2283 cstring_markOwned (fname);
2289 speckind sk = speckind_fromInt (i);
2293 case SR_NOTHING: return (sRef_makeNothing ());
2294 case SR_INTERNAL: return (sRef_makeInternalState ());
2295 case SR_SPECSTATE: return (sRef_makeSpecState ());
2296 case SR_SYSTEM: return (sRef_makeSystemState ());
2302 sRef ptr = sRef_undump (c);
2303 sRef ret = sRef_makePointer (ptr);
2309 sRef adr = sRef_undump (c);
2310 sRef ret = sRef_makeAddress (adr);
2316 return (sRef_makeObject (ctype_undump (c)));
2320 sRef s1 = sRef_undump (c);
2321 sRef s2 = ((*c)++, sRef_undump (c));
2322 sRef ret = sRef_makeConj (s1, s2);
2327 return sRef_undefined;
2329 return sRef_makeUnknown ();
2331 return sRef_makeUnknown ();
2333 llfatalerror (message ("sRef_undump: bad line: %s", cstring_fromChars (*c)));
2341 if (sRef_isInvalid (s))
2343 return (cstring_makeLiteral ("-"));
2350 return (message ("p%d", s->info->paramno));
2352 if (s->info->arrayfetch->indknown)
2354 return (message ("a%d%q", s->info->arrayfetch->ind,
2355 sRef_dump (s->info->arrayfetch->arr)));
2359 return (message ("a%q", sRef_dump (s->info->arrayfetch->arr)));
2362 return (message ("f%s.%q", s->info->field->field,
2363 sRef_dump (s->info->field->rec)));
2365 return (message ("t%q", sRef_dump (s->info->ref)));
2367 return (message ("d%q", sRef_dump (s->info->ref)));
2369 return (message ("o%q", ctype_dump (s->info->object)));
2371 return (message ("s%d", (int) s->info->spec));
2373 return (message ("c%q.%q",
2374 sRef_dump (s->info->conj->a),
2375 sRef_dump (s->info->conj->b)));
2377 if (sRef_isGlobal (s))
2379 return (message ("g%d",
2380 usymtab_convertId (s->info->cvar->index)));
2384 llcontbug (message ("Dumping local variable: %q",
2385 sRef_unparseDebug (s)));
2386 return (cstring_makeLiteral ("u"));
2389 return (cstring_makeLiteral ("u"));
2391 return (message ("r%q", ctype_dump (s->type)));
2397 case SK_UNCONSTRAINED:
2398 llcontbug (message ("sRef_dump: bad kind: %q",
2399 sRef_unparseFull (s)));
2400 return (cstring_makeLiteral ("x"));
2407 cstring sRef_dumpGlobal (sRef s)
2409 if (sRef_isInvalid (s))
2411 return (cstring_makeLiteral ("-"));
2418 if (sRef_isGlobal (s))
2420 return (message ("g%d@%d@%d",
2421 usymtab_convertId (s->info->cvar->index),
2423 (int) s->nullstate));
2427 llcontbug (message ("Dumping local variable: %q",
2428 sRef_unparseDebug (s)));
2429 return (cstring_makeLiteral ("u"));
2432 return (cstring_makeLiteral ("u"));
2434 return (message ("s%d", (int) s->info->spec));
2436 llcontbug (message ("sRef_dumpGlobal: bad kind: %q",
2437 sRef_unparseFull (s)));
2438 return (cstring_makeLiteral ("x"));
2446 sRef_deriveType (sRef s, uentryList cl)
2448 if (sRef_isInvalid (s)) return ctype_unknown;
2453 return (uentry_getType (usymtab_getRefQuiet (s->info->cvar->lexlevel,
2454 s->info->cvar->index)));
2455 case SK_UNCONSTRAINED:
2456 return (ctype_unknown);
2458 return uentry_getType (uentryList_getN (cl, s->info->paramno));
2461 ctype ca = sRef_deriveType (s->info->arrayfetch->arr, cl);
2463 if (ctype_isArray (ca))
2465 return (ctype_baseArrayPtr (ca));
2467 else if (ctype_isUnknown (ca))
2473 llcontbuglit ("sRef_deriveType: inconsistent array type");
2479 ctype ct = sRef_deriveType (s->info->field->rec, cl);
2481 if (ctype_isStructorUnion (ct))
2483 uentry ue = uentryList_lookupField (ctype_getFields (ct),
2484 s->info->field->field);
2486 if (uentry_isValid (ue))
2488 return (uentry_getType (ue));
2492 llcontbuglit ("sRef_deriveType: bad field");
2493 return ctype_unknown;
2496 else if (ctype_isUnknown (ct))
2502 llcontbuglit ("sRef_deriveType: inconsistent field type");
2508 ctype ct = sRef_deriveType (s->info->ref, cl);
2510 if (ctype_isUnknown (ct)) return ct;
2511 if (ctype_isPointer (ct)) return (ctype_baseArrayPtr (ct));
2514 llcontbuglit ("sRef_deriveType: inconsistent pointer type");
2520 ctype ct = sRef_deriveType (s->info->ref, cl);
2522 if (ctype_isUnknown (ct)) return ct;
2523 return ctype_makePointer (ct);
2527 return sRef_deriveType (s->info->ref, cl);
2531 return (s->info->object);
2535 return (ctype_makeConj (sRef_deriveType (s->info->conj->a, cl),
2536 sRef_deriveType (s->info->conj->b, cl)));
2548 return ctype_unknown;
2554 sRef_getType (sRef s)
2556 if (sRef_isInvalid (s)) return ctype_unknown;
2562 sRef_unparseOpt (sRef s)
2564 sRef rb = sRef_getRootBase (s);
2566 if (sRef_isMeaningful (rb) && !sRef_isConst (rb))
2568 cstring ret = sRef_unparse (s);
2570 llassertprint (!cstring_equalLit (ret, "?"), ("print: %s", sRef_unparseDebug (s)));
2572 if (!cstring_isEmpty (ret))
2574 return (cstring_appendChar (ret, ' '));
2582 return cstring_undefined;
2586 sRef_unparsePreOpt (sRef s)
2588 sRef rb = sRef_getRootBase (s);
2590 if (sRef_isMeaningful (rb) && !sRef_isConst (rb))
2592 cstring ret = sRef_unparse (s);
2594 llassertprint (!cstring_equalLit (ret, "?"), ("print: %s", sRef_unparseDebug (s)));
2595 return (cstring_prependCharO (' ', ret));
2598 return cstring_undefined;
2602 sRef_unparse (sRef s)
2604 if (sRef_isInvalid (s)) return (cstring_makeLiteral ("?"));
2606 if (context_inFunctionLike ())
2608 return (sRef_unparseWithArgs (s, context_getParams ()));
2612 return (sRef_unparseNoArgs (s));
2616 static /*@only@*/ cstring
2617 sRef_unparseWithArgs (sRef s, uentryList args)
2619 if (sRef_isInvalid (s))
2621 return (cstring_makeLiteral ("?"));
2627 return (uentry_getName (usymtab_getRefQuiet (s->info->cvar->lexlevel,
2628 s->info->cvar->index)));
2629 case SK_UNCONSTRAINED:
2630 return (cstring_copy (s->info->fname));
2633 if (s->info->paramno < uentryList_size (args))
2635 uentry ue = uentryList_getN (args, s->info->paramno);
2637 if (uentry_isValid (ue))
2638 return uentry_getName (ue);
2641 return (message ("<bad param: %q / args %q",
2642 sRef_unparseDebug (s),
2643 uentryList_unparse (args)));
2646 if (s->info->arrayfetch->indknown)
2648 return (message ("%q[%d]", sRef_unparseWithArgs (s->info->arrayfetch->arr, args),
2649 s->info->arrayfetch->ind));
2653 return (message ("%q[]", sRef_unparseWithArgs (s->info->arrayfetch->arr, args)));
2656 if (s->info->field->rec->kind == SK_PTR)
2658 sRef ptr = s->info->field->rec;
2660 return (message ("%q->%s", sRef_unparseWithArgs (ptr->info->ref, args),
2661 s->info->field->field));
2663 return (message ("%q.%s", sRef_unparseWithArgs (s->info->field->rec, args),
2664 s->info->field->field));
2668 sRef ref = sRef_fixConj (s->info->ref);
2669 skind sk = ref->kind;
2674 ret = message ("storage pointed to by %q",
2675 sRef_unparseWithArgs (ref, args));
2677 else if (skind_isSimple (sk) || sk == SK_PTR)
2679 ret = message ("*%q", sRef_unparseWithArgs (ref, args));
2683 ret = message ("*(%q)", sRef_unparseWithArgs (ref, args));
2689 return (message ("&%q", sRef_unparseWithArgs (s->info->ref, args)));
2691 return (cstring_copy (ctype_unparse (s->info->object)));
2693 return (sRef_unparseWithArgs (sRef_getConjA (s), args));
2695 if (cstring_isDefined (s->info->fname))
2697 return (message ("[result of %s]", s->info->fname));
2701 return (cstring_makeLiteral ("<new>"));
2704 return (cstring_makeLiteral ("?"));
2706 return (message ("<derived %q>", sRef_unparse (s->info->ref)));
2708 return (message ("<external %q>", sRef_unparse (s->info->ref)));
2710 return (message ("<type %s>", ctype_unparse (s->type)));
2712 return (message ("<const %s>", ctype_unparse (s->type)));
2714 return (cstring_makeLiteral
2715 (s->info->spec == SR_NOTHING ? "nothing"
2716 : s->info->spec == SR_INTERNAL ? "internal state"
2717 : s->info->spec == SR_SPECSTATE ? "spec state"
2718 : s->info->spec == SR_SYSTEM ? "file system state"
2721 return cstring_makeLiteral ("result");
2724 llbug (message ("Bad sref, kind = %d", (int) s->kind));
2732 sRef_unparseDebug (sRef s)
2734 if (sRef_isInvalid (s)) return (cstring_makeLiteral ("<undef>"));
2738 case SK_UNCONSTRAINED:
2739 return (message ("<unconstrained %s>", s->info->fname));
2744 ce = usymtab_getRefQuiet (s->info->cvar->lexlevel, s->info->cvar->index);
2746 if (uentry_isInvalid (ce))
2748 return (message ("<scope: %d.%d *invalid*>",
2749 s->info->cvar->lexlevel,
2750 s->info->cvar->index));
2754 return (message ("<scope: %d.%d *%q*>",
2755 s->info->cvar->lexlevel,
2756 s->info->cvar->index,
2757 uentry_getName (ce)));
2763 return (message ("<parameter %d>", s->info->paramno + 1));
2766 if (s->info->arrayfetch->indknown)
2768 return (message ("%q[%d]", sRef_unparseDebug (s->info->arrayfetch->arr),
2769 s->info->arrayfetch->ind));
2773 return (message ("%q[]", sRef_unparseDebug (s->info->arrayfetch->arr)));
2776 return (message ("%q.%s", sRef_unparseDebug (s->info->field->rec),
2777 s->info->field->field));
2779 return (message ("*(%q)", sRef_unparseDebug (s->info->ref)));
2781 return (message ("&%q", sRef_unparseDebug (s->info->ref)));
2783 return (message ("<object type %s>", ctype_unparse (s->info->object)));
2785 return (message ("%q | %q", sRef_unparseDebug (s->info->conj->a),
2786 sRef_unparseDebug (s->info->conj->b)));
2788 return message ("<new: %s>", s->info->fname);
2790 return (message ("<derived %q>", sRef_unparseDebug (s->info->ref)));
2792 return (message ("<external %q>", sRef_unparseDebug (s->info->ref)));
2794 return (message ("<type %s>", ctype_unparse (s->type)));
2796 return (message ("<const %s>", ctype_unparse (s->type)));
2798 return (message ("<result %s>", ctype_unparse (s->type)));
2800 return (message ("<spec %s>",
2801 cstring_makeLiteralTemp
2802 (s->info->spec == SR_NOTHING ? "nothing"
2803 : s->info->spec == SR_INTERNAL ? "internalState"
2804 : s->info->spec == SR_SPECSTATE ? "spec state"
2805 : s->info->spec == SR_SYSTEM ? "fileSystem"
2808 return cstring_makeLiteral ("<unknown>");
2814 static /*@only@*/ cstring
2815 sRef_unparseNoArgs (sRef s)
2817 if (sRef_isInvalid (s)) return (cstring_makeLiteral ("?"));
2821 case SK_UNCONSTRAINED:
2822 return (cstring_copy (s->info->fname));
2825 uentry ce = usymtab_getRefQuiet (s->info->cvar->lexlevel,
2826 s->info->cvar->index);
2828 if (uentry_isInvalid (ce))
2830 llcontbug (message ("sRef_unparseNoArgs: bad cvar: %q", sRef_unparseDebug (s)));
2831 return (sRef_unparseDebug (s));
2835 return (uentry_getName (ce));
2839 if (s->info->arrayfetch->indknown)
2841 return (message ("%q[%d]", sRef_unparseNoArgs (s->info->arrayfetch->arr),
2842 s->info->arrayfetch->ind));
2846 return (message ("%q[]", sRef_unparseNoArgs (s->info->arrayfetch->arr)));
2849 return (message ("%q.%s", sRef_unparseNoArgs (s->info->field->rec),
2850 s->info->field->field));
2853 sRef ref = sRef_fixConj (s->info->ref);
2854 skind sk = ref->kind;
2857 if (skind_isSimple (sk) || sk == SK_PTR)
2859 ret = message ("*%q", sRef_unparseNoArgs (ref));
2863 ret = message ("*(%q)", sRef_unparseNoArgs (ref));
2869 return (message ("&%q", sRef_unparseNoArgs (s->info->ref)));
2871 return (cstring_copy (ctype_unparse (s->info->object)));
2873 return (sRef_unparseNoArgs (s->info->conj->a));
2875 return (message ("result of %s", s->info->fname));
2877 return (message ("<der %q>", sRef_unparseNoArgs (s->info->ref)));
2879 return message ("<ext %q>", sRef_unparseNoArgs (s->info->ref));
2881 return (cstring_makeLiteral
2882 (s->info->spec == SR_NOTHING ? "nothing"
2883 : s->info->spec == SR_INTERNAL ? "internal state"
2884 : s->info->spec == SR_SPECSTATE ? "spec state"
2885 : s->info->spec == SR_SYSTEM ? "file system state"
2888 return cstring_makeLiteral ("result");
2892 return cstring_makeLiteral ("?");
2894 /* llcontbug (message ("sRef_unparseNoArgs: bad case: %q", sRef_unparseDebug (s))); */
2895 return (sRef_unparseDebug (s));
2900 /*@dependent@*/ sRef sRef_makeUnconstrained (cstring fname)
2902 sRef s = sRef_new ();
2904 s->kind = SK_UNCONSTRAINED;
2905 s->info = (sinfo) dmalloc (sizeof (*s->info));
2906 s->info->fname = fname;
2911 cstring sRef_unconstrainedName (sRef s)
2913 llassert (sRef_isUnconstrained (s));
2915 return s->info->fname;
2918 bool sRef_isUnconstrained (sRef s)
2920 return (sRef_isValid(s) && s->kind == SK_UNCONSTRAINED);
2923 static /*@dependent@*/ /*@notnull@*/ sRef
2924 sRef_makeCvarAux (int level, usymId index, ctype ct)
2926 sRef s = sRef_new ();
2929 s->info = (sinfo) dmalloc (sizeof (*s->info));
2931 s->info->cvar = (cref) dmalloc (sizeof (*s->info->cvar));
2932 s->info->cvar->lexlevel = level;
2933 s->info->cvar->index = index;
2935 /* for now, all globals are defined; all locals, aren't */
2937 if (level <= fileScope)
2939 s->defstate = SS_UNKNOWN;
2943 ctype rct = ctype_realType (ct);
2945 if (level != paramsScope
2946 && (ctype_isStructorUnion (rct) || ctype_isRealArray (rct)))
2948 s->defstate = SS_ALLOCATED;
2949 s->oaliaskind = s->aliaskind = AK_STACK;
2953 s->defstate = SS_UNDEFINED;
2954 s->oaliaskind = s->aliaskind = AK_LOCAL;
2960 llassert (level >= globScope);
2961 llassert (usymId_isValid (index));
2966 /*@dependent@*/ sRef sRef_makeCvar (int level, usymId index, ctype ct)
2968 return (sRef_makeCvarAux (level, index, ct));
2971 int sRef_lexLevel (sRef s)
2973 if (sRef_isValid (s))
2977 conj = sRef_fixConj (s);
2978 s = sRef_getRootBase (conj);
2980 if (sRef_isValid (s) && s->kind == SK_CVAR)
2982 return (s->info->cvar->lexlevel);
2990 sRef_makeGlobal (usymId l, ctype ct)
2992 return (sRef_makeCvar (globScope, l, ct));
2996 sRef_setParamNo (sRef s, int l)
2998 llassert (sRef_isValid (s) && s->kind == SK_PARAM);
2999 s->info->paramno = l;
3002 /*@dependent@*/ sRef
3003 sRef_makeParam (int l, ctype ct)
3005 sRef s = sRef_new ();
3010 s->info = (sinfo) dmalloc (sizeof (*s->info));
3011 s->info->paramno = l;
3012 s->defstate = SS_UNKNOWN; /* (probably defined, unless its an out parameter) */
3018 sRef_isIndexKnown (sRef arr)
3022 llassert (sRef_isValid (arr));
3023 arr = sRef_fixConj (arr);
3025 llassert (arr->kind == SK_ARRAYFETCH);
3026 res = arr->info->arrayfetch->indknown;
3031 sRef_getIndex (sRef arr)
3035 llassert (sRef_isValid (arr));
3036 arr = sRef_fixConj (arr);
3038 llassert (arr->kind == SK_ARRAYFETCH);
3040 if (!arr->info->arrayfetch->indknown)
3042 llcontbug (message ("sRef_getIndex: unknown: %q", sRef_unparse (arr)));
3047 result = arr->info->arrayfetch->ind;
3053 static bool sRef_isZerothArrayFetch (/*@notnull@*/ sRef s)
3055 return (s->kind == SK_ARRAYFETCH
3056 && s->info->arrayfetch->indknown
3057 && (s->info->arrayfetch->ind == 0));
3060 /*@exposed@*/ sRef sRef_makeAddress (/*@exposed@*/ sRef t)
3063 if (sRef_isInvalid (t)) return sRef_undefined;
3065 if (sRef_isPointer (t))
3067 return (t->info->ref);
3069 else if (sRef_isZerothArrayFetch (t))
3071 return (t->info->arrayfetch->arr);
3075 sRef s = sRef_new ();
3078 s->type = ctype_makePointer (t->type);
3079 s->info = (sinfo) dmalloc (sizeof (*s->info));
3082 if (t->defstate == SS_UNDEFINED)
3083 /* no! it is allocated even still: && !ctype_isPointer (t->type)) */
3085 s->defstate = SS_ALLOCATED;
3089 s->defstate = t->defstate;
3092 if (t->aliaskind == AK_LOCAL)
3094 if (sRef_isLocalVar (t))
3096 s->aliaskind = AK_STACK;
3104 cstring sRef_getField (sRef s)
3108 llassert (sRef_isValid (s));
3109 s = sRef_fixConj (s);
3111 llassertprint (sRef_isValid (s) && (s->kind == SK_FIELD),
3112 ("s = %s", sRef_unparseDebug (s)));
3114 res = s->info->field->field;
3118 sRef sRef_getBase (sRef s)
3122 if (sRef_isInvalid (s)) return (sRef_undefined);
3124 s = sRef_fixConj (s);
3135 res = s->info->field->rec;
3139 res = s->info->arrayfetch->arr;
3143 res = sRef_undefined; /* shouldn't need it */
3150 ** same as getBase, except returns invalid
3151 ** (and doesn't use adr's)
3155 sRef_getBaseSafe (sRef s)
3159 if (sRef_isInvalid (s)) { return sRef_undefined; }
3161 s = sRef_fixConj (s);
3169 res = s->info->field->rec; break;
3171 res = s->info->arrayfetch->arr;
3174 res = sRef_undefined; break;
3180 /*@constant int MAXBASEDEPTH;@*/
3181 # define MAXBASEDEPTH 25
3183 static /*@exposed@*/ sRef
3184 sRef_getRootBaseAux (sRef s, int depth)
3186 if (sRef_isInvalid (s)) return sRef_undefined;
3188 if (depth > MAXBASEDEPTH)
3191 ("Warning: reference base limit exceeded for %q. "
3192 "This either means there is a variable with at least "
3193 "%d indirections from this reference, or "
3194 "there is a bug in LCLint.",
3199 return sRef_undefined;
3206 return (sRef_getRootBaseAux (s->info->ref, depth + 1));
3208 return (sRef_getRootBaseAux (s->info->field->rec, depth + 1));
3210 return (sRef_getRootBaseAux (s->info->arrayfetch->arr, depth + 1));
3212 return (sRef_getRootBaseAux (sRef_fixConj (s), depth + 1));
3218 sRef sRef_getRootBase (sRef s)
3220 return (sRef_getRootBaseAux (s, 0));
3223 static bool sRef_isDeep (sRef s)
3225 if (sRef_isInvalid (s)) return FALSE;
3235 return (sRef_isDeep (sRef_fixConj (s)));
3241 static int sRef_depth (sRef s)
3243 if (sRef_isInvalid (s)) return 0;
3251 return 1 + sRef_depth (s->info->ref);
3253 return 1 + sRef_depth (s->info->field->rec);
3255 return 1 + sRef_depth (s->info->arrayfetch->arr);
3257 return (sRef_depth (sRef_fixConj (s)));
3264 sRef_makeObject (ctype o)
3266 sRef s = sRef_new ();
3268 s->kind = SK_OBJECT;
3269 s->info = (sinfo) dmalloc (sizeof (*s->info));
3270 s->info->object = o;
3274 sRef sRef_makeExternal (/*@exposed@*/ sRef t)
3276 sRef s = sRef_new ();
3278 llassert (sRef_isValid (t));
3280 s->kind = SK_EXTERNAL;
3281 s->info = (sinfo) dmalloc (sizeof (*s->info));
3287 sRef sRef_makeDerived (/*@exposed@*/ sRef t)
3289 if (sRef_isValid (t))
3291 sRef s = sRef_new ();
3293 s->kind = SK_DERIVED;
3294 s->info = (sinfo) dmalloc (sizeof (*s->info));
3302 return sRef_undefined;
3307 ** definitely NOT symmetric:
3309 ** res fills in unknown state information from other
3313 sRef_mergeStateQuiet (sRef res, sRef other)
3315 llassert (sRef_isValid (res));
3316 llassert (sRef_isValid (other));
3318 res->modified = res->modified || other->modified;
3319 res->safe = res->safe && other->safe;
3321 if (res->defstate == SS_UNKNOWN)
3323 res->defstate = other->defstate;
3324 res->definfo = alinfo_update (res->definfo, other->definfo);
3327 if (res->aliaskind == AK_UNKNOWN ||
3328 (res->aliaskind == AK_LOCAL && alkind_isKnown (other->aliaskind)))
3330 res->aliaskind = other->aliaskind;
3331 res->oaliaskind = other->oaliaskind;
3332 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
3335 if (res->expkind == XO_UNKNOWN)
3337 res->expkind = other->expkind;
3338 res->oexpkind = other->oexpkind;
3339 res->expinfo = alinfo_update (res->expinfo, other->expinfo);
3342 /* out takes precedence over implicitly defined */
3343 if (res->defstate == SS_DEFINED && other->defstate != SS_UNKNOWN)
3345 res->defstate = other->defstate;
3346 res->definfo = alinfo_update (res->definfo, other->definfo);
3349 if (other->nullstate == NS_ERROR || res->nullstate == NS_ERROR)
3351 res->nullstate = NS_ERROR;
3355 if (other->nullstate != NS_UNKNOWN
3356 && (res->nullstate == NS_UNKNOWN || res->nullstate == NS_NOTNULL
3357 || res->nullstate == NS_MNOTNULL))
3359 res->nullstate = other->nullstate;
3360 res->nullinfo = alinfo_update (res->nullinfo, other->nullinfo);
3366 ** definitely NOT symmetric:
3368 ** res fills in known state information from other
3372 sRef_mergeStateQuietReverse (sRef res, sRef other)
3374 bool changed = FALSE;
3376 llassert (sRef_isValid (res));
3377 llassert (sRef_isValid (other));
3379 if (res->kind != other->kind)
3385 res->kind = other->kind;
3386 res->type = other->type;
3387 res->info = sinfo_fullCopy (other);
3391 if (!ctype_equal (res->type, other->type))
3394 res->type = other->type;
3397 sinfo_update (res, other);
3400 res->modified = res->modified || other->modified;
3401 res->safe = res->safe && other->safe;
3403 if (res->aliaskind != other->aliaskind
3404 && (res->aliaskind == AK_UNKNOWN
3405 || ((res->aliaskind == AK_LOCAL
3406 || (res->aliaskind == AK_REFCOUNTED
3407 && other->aliaskind != AK_LOCAL))
3408 && other->aliaskind != AK_UNKNOWN)))
3411 res->aliaskind = other->aliaskind;
3412 res->oaliaskind = other->oaliaskind;
3413 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
3416 if (other->expkind != XO_UNKNOWN && other->expkind != res->expkind)
3419 res->expkind = other->expkind;
3420 res->expinfo = alinfo_update (res->expinfo, other->expinfo);
3423 if (other->oexpkind != XO_UNKNOWN)
3425 res->oexpkind = other->oexpkind;
3428 /* out takes precedence over implicitly defined */
3430 if (res->defstate != other->defstate)
3432 if (other->defstate != SS_UNKNOWN)
3434 res->defstate = other->defstate;
3438 if (other->nullstate == NS_ERROR || res->nullstate == NS_ERROR)
3440 if (res->nullstate != NS_ERROR)
3442 res->nullstate = NS_ERROR;
3448 if (other->nullstate != NS_UNKNOWN && other->nullstate != res->nullstate)
3451 res->nullstate = other->nullstate;
3452 res->nullinfo = alinfo_update (res->nullinfo, other->nullinfo);
3458 sRef_clearDerived (res);
3463 sRef_mergeState (sRef res, sRef other, clause cl, fileloc loc)
3465 if (sRef_isValid (res) && sRef_isValid (other))
3467 sRef_mergeStateAux (res, other, cl, FALSE, loc, TRUE);
3471 if (sRef_isInvalid (res))
3473 llbug (message ("sRef_mergeState: invalid res sRef: %q",
3474 sRef_unparseDebug (other)));
3478 llbug (message ("sRef_mergeState: invalid other sRef: %q",
3479 sRef_unparseDebug (res)));
3485 sRef_mergeOptState (sRef res, sRef other, clause cl, fileloc loc)
3487 if (sRef_isValid (res) && sRef_isValid (other))
3489 sRef_mergeStateAux (res, other, cl, TRUE, loc, TRUE);
3493 if (sRef_isInvalid (res))
3495 llbug (message ("sRef_mergeOptState: invalid res sRef: %q",
3496 sRef_unparseDebug (other)));
3500 llbug (message ("sRef_mergeOptState: invalid other sRef: %q",
3501 sRef_unparseDebug (res)));
3507 sRef_mergeStateAux (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other,
3508 clause cl, bool opt, fileloc loc,
3512 llassertfatal (sRef_isValid (res));
3513 llassertfatal (sRef_isValid (other));
3515 res->modified = res->modified || other->modified;
3517 if (res->kind == other->kind
3518 || (other->kind == SK_UNKNOWN || res->kind == SK_UNKNOWN))
3520 sstate odef = other->defstate;
3521 sstate rdef = res->defstate;
3522 nstate onull = other->nullstate;
3525 ** yucky stuff to handle
3530 if (other->defstate == SS_DEAD
3531 && ((sRef_isOnly (res) && sRef_definitelyNull (res))
3532 || (res->defstate == SS_UNDEFINED
3533 || res->defstate == SS_UNUSEABLE)))
3535 if (res->defstate == SS_UNDEFINED
3536 || res->defstate == SS_UNUSEABLE)
3538 res->defstate = SS_UNUSEABLE;
3542 res->defstate = SS_DEAD;
3545 res->definfo = alinfo_update (res->definfo, other->definfo);
3546 sRef_clearDerived (other);
3547 sRef_clearDerived (res);
3549 else if (res->defstate == SS_DEAD
3550 && ((sRef_isOnly (other) && sRef_definitelyNull (other))
3551 || (other->defstate == SS_UNDEFINED
3552 || other->defstate == SS_UNUSEABLE)))
3554 if (other->defstate == SS_UNDEFINED
3555 || other->defstate == SS_UNUSEABLE)
3557 res->defstate = SS_UNUSEABLE;
3561 res->defstate = SS_DEAD;
3564 sRef_clearDerived (other);
3565 sRef_clearDerived (res);
3567 else if (res->defstate == SS_DEFINED
3568 && (other->defstate == SS_ALLOCATED
3569 && sRef_definitelyNull (other)))
3571 other->defstate = SS_DEFINED; /* definitely null! */
3573 else if (other->defstate == SS_DEFINED
3574 && (res->defstate == SS_ALLOCATED && sRef_definitelyNull (res)))
3576 res->defstate = SS_DEFINED;
3577 res->definfo = alinfo_update (res->definfo, other->definfo);
3584 if (res->defstate == SS_DEAD && other->defstate == SS_DEAD)
3586 sRef_clearDerived (other);
3587 sRef_clearDerived (res);
3591 ** only & dead isn't really an only!
3594 if (alkind_isOnly (other->aliaskind) && other->defstate == SS_DEAD)
3596 other->aliaskind = AK_UNKNOWN;
3599 if (alkind_isOnly (res->aliaskind) && res->defstate == SS_DEAD)
3601 res->aliaskind = AK_UNKNOWN;
3605 ** Dead and dependent -> dead
3608 if (alkind_isDependent (other->aliaskind) && res->defstate == SS_DEAD)
3610 other->aliaskind = AK_UNKNOWN;
3611 other->defstate = SS_DEAD;
3612 sRef_clearDerived (res);
3613 sRef_clearDerived (other);
3616 if (alkind_isDependent (res->aliaskind) && other->defstate == SS_DEAD)
3618 res->aliaskind = AK_UNKNOWN;
3619 res->defstate = SS_DEAD;
3620 sRef_clearDerived (res);
3621 sRef_clearDerived (other);
3625 ** must do alias combine first, since it depends on
3626 ** original values of state and null.
3629 sRef_combineAliasKinds (res, other, cl, loc);
3630 sRef_combineDefState (res, other);
3631 sRef_combineNullState (res, other);
3633 if (rdef == SS_ALLOCATED || rdef == SS_SPECIAL)
3635 if (odef == SS_DEFINED)
3637 if (onull == NS_DEFNULL || onull == NS_CONSTNULL)
3639 res->deriv = sRefSet_copy (res->deriv, other->deriv);
3644 else if (odef == SS_ALLOCATED
3645 || odef == SS_SPECIAL)
3650 if (ctype_isUnion (ctype_realType (sRef_getType (res))))
3652 res->deriv = sRef_mergeUnionDerivs (res->deriv,
3658 res->deriv = sRef_mergeDerivs (res->deriv, other->deriv,
3667 res->deriv = sRef_mergeDerivs (res->deriv, other->deriv,
3677 if (rdef == SS_PDEFINED
3678 || (rdef == SS_DEFINED && odef == SS_PDEFINED))
3682 res->deriv = sRef_mergePdefinedDerivs (res->deriv, other->deriv,
3688 if ((rdef == SS_DEFINED || rdef == SS_UNKNOWN)
3689 && res->defstate == SS_ALLOCATED)
3691 res->deriv = sRefSet_copy (res->deriv, other->deriv);
3697 res->deriv = sRef_mergeDerivs (res->deriv, other->deriv,
3705 sRef_combineExKinds (res, other);
3709 if (res->kind == SK_ARRAYFETCH && other->kind == SK_PTR)
3711 sRef nother = sRef_buildArrayFetchKnown (sRef_getBase (other), 0);
3713 sRef_copyState (nother, other);
3714 sRef_mergeStateAux (res, nother, cl, opt, loc, doDerivs);
3716 else if (res->kind == SK_PTR && other->kind == SK_ARRAYFETCH)
3718 sRef nother = sRef_buildPointer (sRef_getBase (other));
3720 if (sRef_isValid (nother))
3722 sRef_copyState (nother, other);
3723 sRef_mergeStateAux (res, nother, cl, opt, loc, doDerivs);
3728 llcontbug (message ("merge conj: %q / %q", sRef_unparseFull (res),
3729 sRef_unparseFull (other)));
3737 sRef_mergeUnionDerivs (/*@only@*/ sRefSet res,
3738 /*@exposed@*/ sRefSet other, bool opt,
3739 clause cl, fileloc loc)
3741 if (sRefSet_isEmpty (res))
3743 return sRefSet_copy (res, other);
3747 sRefSet_allElements (other, el)
3749 if (sRef_isValid (el))
3751 sRef e2 = sRefSet_lookupMember (other, el);
3753 if (sRef_isValid (e2))
3755 sRef_mergeStateAux (el, e2, cl, opt, loc, FALSE);
3759 res = sRefSet_insert (res, el);
3762 } end_sRefSet_allElements ;
3768 static /*@only@*/ sRefSet
3769 sRef_mergeDerivs (/*@only@*/ sRefSet res, sRefSet other,
3770 bool opt, clause cl, fileloc loc)
3772 sRefSet ret = sRefSet_new ();
3775 sRefSet_allElements (res, el)
3777 if (sRef_isValid (el))
3779 sRef e2 = sRefSet_lookupMember (other, el);
3781 if (sRef_isValid (e2))
3783 if (el->defstate == SS_ALLOCATED &&
3784 e2->defstate == SS_PDEFINED)
3786 e2->defstate = SS_ALLOCATED;
3788 else if (e2->defstate == SS_ALLOCATED &&
3789 el->defstate == SS_PDEFINED)
3791 el->defstate = SS_ALLOCATED;
3792 sRef_clearDerived (el);
3794 else if ((el->defstate == SS_DEAD || sRef_isKept (el)) &&
3795 (e2->defstate == SS_DEFINED && !sRef_isKept (e2)))
3798 if (checkDeadState (el, TRUE, loc))
3800 if (sRef_isThroughArrayFetch (el))
3802 sRef_maybeKill (el, loc);
3803 sRef_maybeKill (e2, loc);
3807 else if ((e2->defstate == SS_DEAD || sRef_isKept (e2)) &&
3808 (el->defstate == SS_DEFINED && !sRef_isKept (el)))
3811 if (checkDeadState (e2, FALSE, loc))
3813 if (sRef_isThroughArrayFetch (el))
3815 sRef_maybeKill (el, loc);
3816 sRef_maybeKill (e2, loc);
3820 else if (el->defstate == SS_DEFINED &&
3821 e2->defstate == SS_PDEFINED)
3823 el->defstate = SS_PDEFINED;
3825 else if (e2->defstate == SS_DEFINED &&
3826 el->defstate == SS_PDEFINED)
3828 e2->defstate = SS_PDEFINED;
3835 if (ctype_isUnion (ctype_realType (sRef_getType (el))))
3837 el->deriv = sRef_mergeUnionDerivs (el->deriv, e2->deriv,
3842 el->deriv = sRef_mergeDerivs (el->deriv, e2->deriv, opt, cl, loc);
3845 if (sRef_equivalent (el, e2))
3847 ret = sRefSet_insert (ret, el);
3851 sRef sr = sRef_leastCommon (el, e2);
3853 if (sRef_isValid (sr))
3855 ret = sRefSet_insert (ret, sr);
3863 (void) sRefSet_delete (other, e2);
3865 else /* not defined */
3867 (void) checkDeadState (el, TRUE, loc);
3870 } end_sRefSet_allElements;
3872 sRefSet_allElements (other, el)
3874 if (sRef_isValid (el))
3876 (void) checkDeadState (el, FALSE, loc);
3878 } end_sRefSet_allElements;
3885 ** Returns TRUE is there is an error.
3888 static bool checkDeadState (/*@notnull@*/ sRef el, bool tbranch, fileloc loc)
3891 ** usymtab_isGuarded --- the utab should still be in the
3892 ** state of the alternate branch.
3894 ** tbranch TRUE means el is released in the last branch, e.g.
3895 ** if (x != NULL) { ; } else { sfree (x); }
3896 ** so, if x is null in the other branch no error is reported.
3898 ** tbranch FALSE means this is the other branch:
3899 ** if (x != NULL) { sfree (x); } else { ; }
3900 ** so, if x is null in this branch there is no error.
3904 if ((sRef_isDead (el) || sRef_isKept (el))
3905 && !sRef_isDeepUnionField (el) && !sRef_isThroughArrayFetch (el))
3910 if (usymtab_isProbableDeepNull (el))
3917 if (usymtab_isAltProbablyDeepNull (el))
3925 message ("Storage %q is %q in one path, but live in another.",
3927 cstring_makeLiteral (sRef_isKept (el)
3928 ? "kept" : "released")),
3932 if (sRef_isKept (el))
3934 sRef_showAliasInfo (el);
3938 sRef_showStateInfo (el);
3941 /* prevent further errors */
3942 el->defstate = SS_UNKNOWN;
3943 sRef_setAliasKind (el, AK_ERROR, fileloc_undefined);
3953 checkDerivDeadState (/*@notnull@*/ sRef el, bool tbranch, fileloc loc)
3956 if (checkDeadState (el, tbranch, loc))
3958 sRefSet_allElements (el->deriv, t)
3960 if (sRef_isValid (t))
3962 checkDerivDeadState (t, tbranch, loc);
3964 } end_sRefSet_allElements;
3969 sRef_mergePdefinedDerivs (sRefSet res, sRefSet other, bool opt,
3970 clause cl, fileloc loc)
3972 sRefSet ret = sRefSet_new ();
3974 sRefSet_allElements (res, el)
3976 if (sRef_isValid (el))
3978 sRef e2 = sRefSet_lookupMember (other, el);
3980 if (sRef_isValid (e2))
3982 if (sRef_isAllocated (el) && !sRef_isAllocated (e2))
3986 else if (sRef_isAllocated (e2) && !sRef_isAllocated (el))
3988 el->deriv = sRefSet_copy (el->deriv, e2->deriv);
3992 el->deriv = sRef_mergePdefinedDerivs (el->deriv, e2->deriv,
3996 sRef_mergeStateAux (el, e2, cl, opt, loc, FALSE);
3998 ret = sRefSet_insert (ret, el);
3999 (void) sRefSet_delete (other, e2);
4005 checkDerivDeadState (el, (cl == FALSECLAUSE), loc);
4008 ret = sRefSet_insert (ret, el);
4011 } end_sRefSet_allElements;
4013 sRefSet_allElements (other, el)
4015 if (sRef_isValid (el))
4017 if (!sRefSet_member (ret, el))
4019 /* was cl == FALSECLAUSE */
4020 checkDerivDeadState (el, FALSE, loc);
4021 ret = sRefSet_insert (ret, el);
4026 ** it's okay --- member is a different equality test
4030 } end_sRefSet_allElements;
4036 sRef sRef_makeConj (/*@exposed@*/ /*@returned@*/ sRef a, /*@exposed@*/ sRef b)
4038 llassert (sRef_isValid (a));
4039 llassert (sRef_isValid (b));
4041 if (!sRef_equivalent (a, b))
4043 sRef s = sRef_new ();
4046 s->info = (sinfo) dmalloc (sizeof (*s->info));
4047 s->info->conj = (cjinfo) dmalloc (sizeof (*s->info->conj));
4048 s->info->conj->a = a;
4049 s->info->conj->b = b;
4051 if (ctype_equal (a->type, b->type)) s->type = a->type;
4052 else s->type = ctype_makeConj (a->type, b->type);
4054 if (a->defstate == b->defstate)
4056 s->defstate = a->defstate;
4060 s->defstate = SS_UNKNOWN;
4063 s->nullstate = NS_UNKNOWN;
4065 s->safe = a->safe && b->safe;
4066 s->aliaskind = alkind_resolve (a->aliaskind, b->aliaskind);
4072 /*@-exposetrans@*/ return a; /*@=exposetrans@*/
4079 sRef s = sRef_new ();
4081 s->kind = SK_UNKNOWN;
4086 sRef_makeSpecial (speckind sk) /*@*/
4088 sRef s = sRef_new ();
4090 s->kind = SK_SPECIAL;
4091 s->info = (sinfo) dmalloc (sizeof (*s->info));
4096 static sRef srnothing = sRef_undefined;
4097 static sRef srinternal = sRef_undefined;
4098 static sRef srsystem = sRef_undefined;
4099 static sRef srspec = sRef_undefined;
4102 sRef_makeNothing (void)
4104 if (sRef_isInvalid (srnothing))
4106 srnothing = sRef_makeSpecial (SR_NOTHING);
4115 sRef_makeInternalState (void)
4117 if (sRef_isInvalid (srinternal))
4119 srinternal = sRef_makeSpecial (SR_INTERNAL);
4128 sRef_makeSpecState (void)
4130 if (sRef_isInvalid (srspec))
4132 srspec = sRef_makeSpecial (SR_SPECSTATE);
4141 sRef_makeSystemState (void)
4143 if (sRef_isInvalid (srsystem))
4145 srsystem = sRef_makeSpecial (SR_SYSTEM);
4154 sRef_makeResultType (ctype ct)
4156 sRef res = sRef_makeResult ();
4165 sRef s = sRef_new ();
4167 s->kind = SK_RESULT;
4168 s->type = ctype_unknown;
4169 s->defstate = SS_UNKNOWN;
4170 s->aliaskind = AK_UNKNOWN;
4171 s->nullstate = NS_UNKNOWN;
4178 sRef_isNothing (sRef s)
4180 return (sRef_isKindSpecial (s) && s->info->spec == SR_NOTHING);
4184 sRef_isInternalState (sRef s)
4186 return (sRef_isKindSpecial (s) && s->info->spec == SR_INTERNAL);
4190 sRef_isSpecInternalState (sRef s)
4192 return (sRef_isKindSpecial (s)
4193 && (s->info->spec == SR_INTERNAL || s->info->spec == SR_SPECSTATE));
4197 sRef_isSpecState (sRef s)
4199 return (sRef_isKindSpecial (s) && s->info->spec == SR_SPECSTATE);
4203 sRef_isResult (sRef s)
4205 return (sRef_isValid (s) && s->kind == SK_RESULT);
4209 sRef_isSystemState (sRef s)
4211 return (sRef_isKindSpecial (s) && s->info->spec == SR_SYSTEM);
4215 sRef_getScopeIndex (sRef s)
4217 llassert (sRef_isValid (s));
4218 llassert (sRef_isCvar (s));
4220 return (s->info->cvar->index);
4224 sRef_makeSafe (sRef s)
4226 if (sRef_isValid (s))
4233 sRef_makeUnsafe (sRef s)
4235 if (sRef_isValid (s))
4242 ** memory state operations
4245 /*@only@*/ cstring sRef_unparseFull (sRef s)
4247 if (sRef_isInvalid (s)) return (cstring_undefined);
4249 return (message ("[%d] %q - %q [%s] { %q }",
4251 sRef_unparseDebug (s),
4252 sRef_unparseState (s),
4253 exkind_unparse (s->oexpkind),
4254 sRefSet_unparseDebug (s->deriv)));
4257 /*@unused@*/ cstring sRef_unparseDeep (sRef s)
4259 cstring st = cstring_undefined;
4261 st = message ("%q:", sRef_unparseFull (s));
4263 if (sRef_isValid (s))
4265 sRefSet_allElements (s->deriv, el)
4267 st = message("%q\n%q", st, sRef_unparseDeep (el));
4268 } end_sRefSet_allElements ;
4274 /*@only@*/ cstring sRef_unparseState (sRef s)
4276 if (sRef_isConj (s))
4278 return (message ("%q | %q",
4279 sRef_unparseState (s->info->conj->a),
4280 sRef_unparseState (s->info->conj->b)));
4283 if (sRef_isInvalid (s))
4285 return (cstring_makeLiteral ("<invalid>"));
4288 return (message ("%s.%s.%s.%s",
4289 alkind_unparse (s->aliaskind),
4290 nstate_unparse (s->nullstate),
4291 exkind_unparse (s->expkind),
4292 sstate_unparse (s->defstate)));
4295 bool sRef_isNotUndefined (sRef s)
4297 return (sRef_isInvalid (s)
4298 || (s->defstate != SS_UNDEFINED
4299 && s->defstate != SS_UNUSEABLE
4300 && s->defstate != SS_DEAD));
4303 ynm sRef_isWriteable (sRef s)
4305 if (sRef_isInvalid (s)) return MAYBE;
4307 if (sRef_isConj (s) && s->defstate == SS_UNKNOWN)
4309 if (ynm_toBoolStrict (sRef_isWriteable (sRef_getConjA (s))))
4311 if (ynm_toBoolStrict (sRef_isWriteable (sRef_getConjB (s))))
4319 if (ynm_toBoolStrict (sRef_isWriteable (sRef_getConjB (s))))
4327 return (ynm_fromBool (s->defstate != SS_UNUSEABLE));
4330 bool sRef_hasNoStorage (sRef s)
4332 return (!sRef_isAllocatedStorage (s) || sRef_isDefinitelyNull (s));
4335 bool sRef_isStrictReadable (sRef s)
4337 return (ynm_toBoolStrict (sRef_isReadable (s)));
4340 ynm sRef_isReadable (sRef s)
4344 if (sRef_isInvalid (s)) return YES;
4348 if (sRef_isConj (s) && s->defstate == SS_UNKNOWN)
4350 if (ynm_toBoolStrict (sRef_isReadable (sRef_getConjA (s))))
4352 if (ynm_toBoolStrict (sRef_isReadable (sRef_getConjB (s))))
4360 if (ynm_toBoolStrict (sRef_isReadable (sRef_getConjB (s))))
4367 else if (ss == SS_HOFFA)
4369 if (context_getFlag (FLG_STRICTUSERELEASED))
4380 return (ynm_fromBool (ss == SS_DEFINED
4383 || ss == SS_PDEFINED
4386 || ss == SS_ALLOCATED
4387 || ss == SS_UNKNOWN));
4391 static /*@exposed@*/ sRef whatUndefined (/*@exposed@*/ sRef fref, int depth)
4396 if (depth > MAXDEPTH)
4399 ("Warning: check definition limit exceeded, checking %q. "
4400 "This either means there is a variable with at least "
4401 "%d indirections apparent in the program text, or "
4402 "there is a bug in LCLint.",
4403 sRef_unparse (fref),
4407 return sRef_undefined;
4410 if (!sRef_isKnown (fref) || sRef_isAnyDefined (fref))
4412 return sRef_undefined;
4415 if (sRef_isUnuseable (fref) || sRef_isStateUndefined (fref))
4420 ct = ctype_realType (sRef_getType (fref));
4422 if (ctype_isUnknown (ct))
4424 return sRef_undefined;
4426 else if (ctype_isPointer (ct) || ctype_isArray (ct))
4428 if (sRef_isStateUnknown (fref))
4430 return sRef_undefined;
4434 sRef fptr = sRef_constructDeref (fref);
4436 return (whatUndefined (fptr, depth + 1));
4439 else if (ctype_isStruct (ct))
4441 bool hasOneDefined = FALSE;
4443 if (sRef_isStateUnknown (fref))
4448 if (sRef_isPdefined (fref) || sRef_isAnyDefined (fref))
4450 sRefSet_realElements (sRef_derivedFields (fref), sr)
4452 hasOneDefined = TRUE;
4454 if (sRef_isField (sr))
4456 cstring fieldname = sRef_getField (sr);
4457 sRef fldref = sRef_makeField (fref, fieldname);
4458 bool shouldCheck = !sRef_isRecursiveField (fldref);
4462 sRef wdef = whatUndefined (fldref, depth + 1);
4464 if (sRef_isValid (wdef))
4470 } end_sRefSet_realElements;
4472 else if (sRef_isAllocated (fref))
4475 ** for structures, each field must be completely defined
4478 uentryList fields = ctype_getFields (ct);
4480 uentryList_elements (fields, ue)
4482 cstring name = uentry_getRealName (ue);
4483 sRef ffield = sRef_makeField (fref, name);
4484 bool shouldCheck = !sRef_isRecursiveField (ffield);
4486 if (sRef_isRelDef (uentry_getSref (ue)))
4494 sRef wdef = whatUndefined (ffield, depth + 1);
4496 if (sRef_isInvalid (wdef))
4502 } end_uentryList_elements;
4509 else if (ctype_isUnion (ct))
4518 return sRef_undefined;
4521 static bool checkDefined (sRef sr)
4523 return (sRef_isInvalid (whatUndefined (sr, 0)));
4526 bool sRef_isReallyDefined (sRef s)
4528 if (sRef_isValid (s))
4530 if (sRef_isAnyDefined (s))
4536 if (sRef_isAllocated (s) || sRef_isPdefined (s))
4538 return checkDefined (s);
4552 void sRef_showNotReallyDefined (sRef s)
4554 if (sRef_isValid (s))
4556 if (sRef_isAnyDefined (s))
4562 if (sRef_isAllocated (s) || sRef_isPdefined (s))
4564 sRef ref = whatUndefined (s, 0);
4566 llassert (sRef_isValid (ref));
4571 (message ("This sub-reference is %s: %q",
4572 sstate_unparse (sRef_getDefState (ref)),
4573 sRef_unparse (ref)));
4588 sstate sRef_getDefState (sRef s)
4590 if (sRef_isInvalid (s)) return (SS_UNKNOWN);
4591 return (s->defstate);
4594 void sRef_setDefState (sRef s, sstate defstate, fileloc loc)
4596 sRef_setStateAux (s, defstate, loc);
4599 static void sRef_clearAliasStateAux (sRef s, fileloc loc)
4601 sRef_setAliasKind (s, AK_ERROR, loc);
4604 void sRef_clearAliasState (sRef s, fileloc loc)
4606 sRef_aliasSetComplete (sRef_clearAliasStateAux, s, loc);
4609 void sRef_setAliasKindComplete (sRef s, alkind kind, fileloc loc)
4611 sRef_aliasSetCompleteParam (sRef_setAliasKind, s, kind, loc);
4614 void sRef_setAliasKind (sRef s, alkind kind, fileloc loc)
4616 if (sRef_isValid (s))
4618 sRef_clearDerived (s);
4620 if ((kind != s->aliaskind && kind != s->oaliaskind)
4621 && fileloc_isDefined (loc))
4623 s->aliasinfo = alinfo_updateLoc (s->aliasinfo, loc);
4626 s->aliaskind = kind;
4630 void sRef_setOrigAliasKind (sRef s, alkind kind)
4632 if (sRef_isValid (s))
4634 s->oaliaskind = kind;
4638 exkind sRef_getExKind (sRef s)
4640 if (sRef_isValid (s))
4642 return (s->expkind);
4650 exkind sRef_getOrigExKind (sRef s)
4652 if (sRef_isValid (s))
4654 return (s->oexpkind);
4662 static void sRef_clearExKindAux (sRef s, fileloc loc)
4664 sRef_setExKind (s, XO_UNKNOWN, loc);
4667 void sRef_setObserver (sRef s, fileloc loc)
4669 sRef_setExKind (s, XO_OBSERVER, loc);
4672 void sRef_setExposed (sRef s, fileloc loc)
4674 sRef_setExKind (s, XO_EXPOSED, loc);
4677 void sRef_clearExKindComplete (sRef s, fileloc loc)
4679 (void) sRef_aliasSetComplete (sRef_clearExKindAux, s, loc);
4682 void sRef_setExKind (sRef s, exkind exp, fileloc loc)
4684 if (sRef_isValid (s))
4686 if (s->expkind != exp)
4688 s->expinfo = alinfo_updateLoc (s->expinfo, loc);
4696 ** s1->derived = s2->derived
4699 static void sRef_copyRealDerived (sRef s1, sRef s2)
4701 if (sRef_isValid (s1) && sRef_isValid (s2))
4703 sRef sb = sRef_getRootBase (s1);
4705 sRefSet_clear (s1->deriv);
4707 sRefSet_allElements (s2->deriv, el)
4709 if (sRef_isValid (el))
4711 sRef rb = sRef_getRootBase (el);
4713 if (!sRef_same (rb, sb))
4715 sRef fb = sRef_fixDirectBase (el, s1);
4717 if (sRef_isValid (fb))
4719 sRef_copyRealDerived (fb, el);
4720 sRef_addDeriv (s1, fb);
4725 sRef_addDeriv (s1, el);
4728 } end_sRefSet_allElements ;
4733 void sRef_copyRealDerivedComplete (sRef s1, sRef s2)
4735 sRef_innerAliasSetCompleteParam (sRef_copyRealDerived, s1, s2);
4738 void sRef_setUndefined (sRef s, fileloc loc)
4740 if (sRef_isValid (s))
4742 s->defstate = SS_UNDEFINED;
4744 if (fileloc_isDefined (loc))
4746 s->definfo = alinfo_updateLoc (s->definfo, loc);
4749 sRef_clearDerived (s);
4753 static void sRef_setDefinedAux (sRef s, fileloc loc, bool clear)
4755 if (sRef_isInvalid (s)) return;
4757 if (s->defstate != SS_DEFINED && fileloc_isDefined (loc))
4759 s->definfo = alinfo_updateLoc (s->definfo, loc);
4762 s->defstate = SS_DEFINED;
4764 /* e.g., if x is allocated, *x = 3 defines x */
4766 if (s->kind == SK_PTR)
4768 sRef p = s->info->ref;
4770 if (p->defstate == SS_ALLOCATED)
4772 sRef_setDefinedAux (p, loc, clear);
4775 else if (s->kind == SK_ARRAYFETCH)
4777 if (!s->info->arrayfetch->indknown
4778 || (s->info->arrayfetch->ind == 0))
4780 sRef p = s->info->arrayfetch->arr;
4781 sRef ptr = sRef_constructPointer (p);
4783 if (sRef_isValid (ptr))
4785 if (ptr->defstate == SS_ALLOCATED
4786 || ptr->defstate == SS_UNDEFINED)
4788 sRef_setDefinedAux (ptr, loc, clear);
4792 if (p->defstate == SS_RELDEF)
4796 else if (p->defstate == SS_ALLOCATED || p->defstate == SS_PDEFINED)
4798 p->defstate = SS_DEFINED;
4805 else if (s->kind == SK_FIELD)
4807 sRef parent = s->info->field->rec;
4809 if (sRef_isValid (parent))
4811 if (ctype_isUnion (ctype_realType (parent->type)))
4814 ** Should not clear derived from here.
4817 sRef_setDefinedNoClear (parent, loc);
4821 ; /* Nothing to do for structures. */
4833 sRef_clearDerived (s);
4837 static void sRef_setPartialDefined (sRef s, fileloc loc)
4839 if (!sRef_isPartial (s))
4841 sRef_setDefined (s, loc);
4845 void sRef_setPartialDefinedComplete (sRef s, fileloc loc)
4847 sRef_innerAliasSetComplete (sRef_setPartialDefined, s, loc);
4850 void sRef_setDefinedComplete (sRef s, fileloc loc)
4852 sRef_innerAliasSetComplete (sRef_setDefined, s, loc);
4855 void sRef_setDefined (sRef s, fileloc loc)
4857 sRef_setDefinedAux (s, loc, TRUE);
4860 static void sRef_setDefinedNoClear (sRef s, fileloc loc)
4862 DPRINTF (("Defining: %s", sRef_unparseFull (s)));
4863 sRef_setDefinedAux (s, loc, FALSE);
4864 DPRINTF (("==> %s", sRef_unparseFull (s)));
4867 void sRef_setDefinedNCComplete (sRef s, fileloc loc)
4869 DPRINTF (("Set Defined Complete: %s", sRef_unparseFull (s)));
4870 sRef_innerAliasSetComplete (sRef_setDefinedNoClear, s, loc);
4871 DPRINTF (("==> %s", sRef_unparseFull (s)));
4874 static bool sRef_isDeepUnionField (sRef s)
4876 return (sRef_deepPred (sRef_isUnionField, s));
4879 bool sRef_isUnionField (sRef s)
4881 if (sRef_isValid (s) && s->kind == SK_FIELD)
4884 ** defining one field of a union defines the union
4887 sRef base = s->info->field->rec;
4889 if (sRef_isValid (base))
4891 return (ctype_isUnion (ctype_realType (base->type)));
4898 void sRef_setPdefined (sRef s, fileloc loc)
4900 if (sRef_isValid (s) && !sRef_isPartial (s))
4902 sRef base = sRef_getBaseSafe (s);
4904 if (s->defstate == SS_ALLOCATED)
4909 if (s->defstate != SS_PDEFINED && fileloc_isDefined (loc))
4911 s->definfo = alinfo_updateLoc (s->definfo, loc);
4914 s->defstate = SS_PDEFINED;
4916 /* e.g., if x is allocated, *x = 3 defines x */
4918 while (sRef_isValid (base) && sRef_isKnown (base))
4920 if (base->defstate == SS_DEFINED)
4924 base->defstate = SS_PDEFINED;
4925 nb = sRef_getBaseSafe (base);
4936 static void sRef_setStateAux (sRef s, sstate ss, fileloc loc)
4938 if (sRef_isValid (s))
4940 /* if (s->defstate == SS_RELDEF) return; */
4942 if (s->defstate != ss && fileloc_isDefined (loc))
4944 s->definfo = alinfo_updateLoc (s->definfo, loc);
4948 sRef_clearDerived (s);
4950 if (ss == SS_ALLOCATED)
4952 sRef base = sRef_getBaseSafe (s);
4954 while (sRef_isValid (base) && sRef_isKnown (base))
4956 if (base->defstate == SS_DEFINED)
4960 base->defstate = SS_PDEFINED;
4962 nb = sRef_getBaseSafe (base);
4975 void sRef_setAllocatedComplete (sRef s, fileloc loc)
4977 sRef_innerAliasSetComplete (sRef_setAllocated, s, loc);
4980 static void sRef_setAllocatedShallow (sRef s, fileloc loc)
4982 if (sRef_isValid (s))
4984 if (s->defstate == SS_DEAD || s->defstate == SS_UNDEFINED)
4986 s->defstate = SS_ALLOCATED;
4988 if (fileloc_isDefined (loc))
4990 s->definfo = alinfo_updateLoc (s->definfo, loc);
4996 void sRef_setAllocatedShallowComplete (sRef s, fileloc loc)
4998 sRef_innerAliasSetComplete (sRef_setAllocatedShallow, s, loc);
5001 void sRef_setAllocated (sRef s, fileloc loc)
5003 sRef_setStateAux (s, SS_ALLOCATED, loc);
5006 void sRef_setPartial (sRef s, fileloc loc)
5008 sRef_setStateAux (s, SS_PARTIAL, loc);
5011 void sRef_setShared (sRef s, fileloc loc)
5013 if (sRef_isValid (s))
5015 if (s->aliaskind != AK_SHARED && fileloc_isDefined (loc))
5017 s->aliasinfo = alinfo_updateLoc (s->aliasinfo, loc);
5020 s->aliaskind = AK_SHARED;
5021 /* don't! sRef_clearDerived (s); */
5025 void sRef_setLastReference (sRef s, sRef ref, fileloc loc)
5027 if (sRef_isValid (s))
5029 s->aliaskind = sRef_getAliasKind (ref);
5030 s->aliasinfo = alinfo_updateRefLoc (s->aliasinfo, ref, loc);
5035 void sRef_setNullStateAux (/*@notnull@*/ sRef s, nstate ns, fileloc loc)
5039 if (fileloc_isDefined (loc))
5041 s->nullinfo = alinfo_updateLoc (s->nullinfo, loc);
5045 void sRef_setNotNull (sRef s, fileloc loc)
5047 if (sRef_isValid (s))
5049 sRef_setNullStateAux (s, NS_NOTNULL, loc);
5053 void sRef_setNullState (sRef s, nstate n, fileloc loc)
5055 if (sRef_isValid (s))
5057 sRef_setNullStateAux (s, n, loc);
5061 void sRef_setNullTerminatedStateInnerComplete (sRef s, struct _bbufinfo b, /*@unused@*/ fileloc loc) {
5063 switch (b.bufstate) {
5064 case BB_NULLTERMINATED:
5065 sRef_setNullTerminatedState (s);
5066 sRef_setLen (s, b.len);
5068 case BB_POSSIBLYNULLTERMINATED:
5069 sRef_setPossiblyNullTerminatedState(s);
5071 case BB_NOTNULLTERMINATED:
5072 sRef_setNotNullTerminatedState (s);
5075 sRef_setSize (s, b.size);
5077 /* PL: TO BE DONE : Aliases are not modified right now, have to be similar to
5078 * setNullStateInnerComplete.
5082 void sRef_setNullStateInnerComplete (sRef s, nstate n, fileloc loc)
5084 sRef_setNullState (s, n, loc);
5089 sRef_innerAliasSetComplete (sRef_setPosNull, s, loc);
5092 sRef_innerAliasSetComplete (sRef_setDefNull, s, loc);
5095 sRef_innerAliasSetComplete (sRef_setNullUnknown, s, loc);
5098 sRef_innerAliasSetComplete (sRef_setNotNull, s, loc);
5101 sRef_innerAliasSetComplete (sRef_setNotNull, s, loc);
5104 sRef_innerAliasSetComplete (sRef_setNullUnknown, s, loc);
5107 sRef_innerAliasSetComplete (sRef_setDefNull, s, loc);
5110 sRef_innerAliasSetComplete (sRef_setNullUnknown, s, loc);
5113 sRef_innerAliasSetComplete (sRef_setNullErrorLoc, s, loc);
5118 void sRef_setPosNull (sRef s, fileloc loc)
5120 if (sRef_isValid (s))
5122 sRef_setNullStateAux (s, NS_POSNULL, loc);
5126 void sRef_setDefNull (sRef s, fileloc loc)
5128 if (sRef_isValid (s))
5130 sRef_setNullStateAux (s, NS_DEFNULL, loc);
5134 void sRef_setNullUnknown (sRef s, fileloc loc)
5136 if (sRef_isValid (s))
5138 sRef_setNullStateAux (s, NS_UNKNOWN, loc);
5142 void sRef_setNullError (sRef s)
5144 if (sRef_isValid (s))
5146 sRef_setNullStateAux (s, NS_UNKNOWN, fileloc_undefined);
5150 void sRef_setNullErrorLoc (sRef s, /*@unused@*/ fileloc loc)
5152 sRef_setNullError (s);
5155 void sRef_setOnly (sRef s, fileloc loc)
5157 if (sRef_isValid (s) && s->aliaskind != AK_ONLY)
5159 s->aliaskind = AK_ONLY;
5160 s->aliasinfo = alinfo_updateLoc (s->aliasinfo, loc);
5164 void sRef_setDependent (sRef s, fileloc loc)
5166 if (sRef_isValid (s) && !sRef_isConst (s) && (s->aliaskind != AK_DEPENDENT))
5168 s->aliaskind = AK_DEPENDENT;
5169 s->aliasinfo = alinfo_updateLoc (s->aliasinfo, loc);
5173 void sRef_setOwned (sRef s, fileloc loc)
5175 if (sRef_isValid (s) && !sRef_isConst (s) && (s->aliaskind != AK_OWNED))
5177 s->aliaskind = AK_OWNED;
5178 s->aliasinfo = alinfo_updateLoc (s->aliasinfo, loc);
5182 void sRef_setKept (sRef s, fileloc loc)
5184 if (sRef_isValid (s) && !sRef_isConst (s) && (s->aliaskind != AK_KEPT))
5186 sRef base = sRef_getBaseSafe (s);
5188 while (sRef_isValid (base) && sRef_isKnown (base))
5190 if (base->defstate == SS_DEFINED)
5192 base->defstate = SS_PDEFINED;
5193 base = sRef_getBaseSafe (base);
5202 s->aliaskind = AK_KEPT;
5203 s->aliasinfo = alinfo_updateLoc (s->aliasinfo, loc);
5207 static void sRef_setKeptAux (sRef s, fileloc loc)
5209 if (!sRef_isShared (s))
5211 sRef_setKept (s, loc);
5215 static void sRef_setDependentAux (sRef s, fileloc loc)
5217 if (!sRef_isShared (s))
5219 sRef_setDependent (s, loc);
5223 void sRef_setKeptComplete (sRef s, fileloc loc)
5225 sRef_aliasSetComplete (sRef_setKeptAux, s, loc);
5228 void sRef_setDependentComplete (sRef s, fileloc loc)
5230 sRef_aliasSetComplete (sRef_setDependentAux, s, loc);
5233 void sRef_setFresh (sRef s, fileloc loc)
5235 if (sRef_isValid (s))
5237 s->aliaskind = AK_FRESH;
5238 s->aliasinfo = alinfo_updateLoc (s->aliasinfo, loc);
5242 void sRef_kill (sRef s, fileloc loc)
5244 DPRINTF (("Kill: %s", sRef_unparseFull (s)));
5246 if (sRef_isValid (s) && !sRef_isShared (s) && !sRef_isConst (s))
5248 sRef base = sRef_getBaseSafe (s);
5250 while (sRef_isValid (base) && sRef_isKnown (base))
5252 if (base->defstate == SS_DEFINED)
5254 base->defstate = SS_PDEFINED;
5255 base = sRef_getBaseSafe (base);
5264 s->aliaskind = s->oaliaskind;
5265 s->defstate = SS_DEAD;
5266 s->definfo = alinfo_updateLoc (s->definfo, loc);
5268 sRef_clearDerived (s);
5272 void sRef_maybeKill (sRef s, fileloc loc)
5275 if (sRef_isValid (s))
5277 sRef base = sRef_getBaseSafe (s);
5280 while (sRef_isValid (base) && sRef_isKnown (base))
5282 if (base->defstate == SS_DEFINED || base->defstate == SS_RELDEF)
5284 base->defstate = SS_PDEFINED;
5285 base = sRef_getBaseSafe (base);
5294 s->aliaskind = s->oaliaskind;
5295 s->defstate = SS_HOFFA;
5296 s->definfo = alinfo_updateLoc (s->definfo, loc);
5297 sRef_clearDerived (s);
5303 ** just for type checking...
5306 static void sRef_killAux (sRef s, fileloc loc)
5308 if (sRef_isValid (s) && !sRef_isShared (s))
5310 if (sRef_isUnknownArrayFetch (s))
5312 sRef_maybeKill (s, loc);
5322 ** kills s and all aliases to s
5325 void sRef_killComplete (sRef s, fileloc loc)
5327 DPRINTF (("Kill complete: %s", sRef_unparseFull (s)));
5328 sRef_aliasSetComplete (sRef_killAux, s, loc);
5331 static bool sRef_equivalent (sRef s1, sRef s2)
5333 return (sRef_compare (s1, s2) == 0);
5337 ** returns an sRef that will not be free'd on function exit.
5340 /*@only@*/ sRef sRef_saveCopy (sRef s)
5344 if (sRef_isValid (s))
5346 bool old = inFunction;
5349 ** Exit the function scope, so this sRef is not
5350 ** stored in the deallocation table.
5354 DPRINTF (("Copying sref: %s", sRef_unparseFull(s)));
5355 ret = sRef_copy (s);
5356 DPRINTF (("Copying ===>: %s", sRef_unparseFull(ret)));
5361 ret = sRef_undefined;
5364 /*@-dependenttrans@*/
5366 /*@=dependenttrans@*/
5369 sRef sRef_copy (sRef s)
5371 if (sRef_isKindSpecial (s))
5374 return s; /* don't copy specials */
5378 if (sRef_isValid (s))
5380 sRef t = sRef_alloc ();
5384 t->modified = s->modified;
5387 t->info = sinfo_copy (s);
5389 t->defstate = s->defstate;
5391 t->nullstate = s->nullstate;
5393 /* start modifications */
5394 t->bufinfo.bufstate = s->bufinfo.bufstate;
5395 t->bufinfo.len = s->bufinfo.len;
5396 t->bufinfo.size = s->bufinfo.size;
5397 /* end modifications */
5399 t->aliaskind = s->aliaskind;
5400 t->oaliaskind = s->oaliaskind;
5402 t->expkind = s->expkind;
5403 t->oexpkind = s->oexpkind;
5405 t->aliasinfo = alinfo_copy (s->aliasinfo);
5406 t->definfo = alinfo_copy (s->definfo);
5407 t->nullinfo = alinfo_copy (s->nullinfo);
5408 t->expinfo = alinfo_copy (s->expinfo);
5410 t->deriv = sRefSet_newDeepCopy (s->deriv);
5416 return sRef_undefined;
5421 # define PREDTEST(func,s) \
5422 do { if (sRef_isInvalid (s)) { return FALSE; } \
5423 else { if (sRef_isConj (s)) \
5424 { return (func (sRef_getConjA (s)) \
5425 || func (sRef_getConjB (s))); }}} while (FALSE);
5427 bool sRef_isAddress (sRef s)
5429 PREDTEST (sRef_isAddress, s);
5430 return (s->kind == SK_ADR);
5434 ** pretty weak... maybe a flag should control this.
5437 bool sRef_isThroughArrayFetch (sRef s)
5439 if (sRef_isValid (s))
5447 if (sRef_isArrayFetch (tref))
5452 lt = sRef_getBase (tref);
5454 } while (sRef_isValid (tref));
5460 bool sRef_isArrayFetch (sRef s)
5462 PREDTEST (sRef_isArrayFetch, s);
5463 return (s->kind == SK_ARRAYFETCH);
5466 bool sRef_isMacroParamRef (sRef s)
5468 if (context_inMacro () && sRef_isCvar (s))
5470 uentry ue = sRef_getUentry (s);
5471 cstring pname = makeParam (uentry_rawName (ue));
5472 uentry mac = usymtab_lookupSafe (pname);
5474 cstring_free (pname);
5475 return (uentry_isValid (mac));
5481 bool sRef_isCvar (sRef s)
5483 PREDTEST (sRef_isCvar, s);
5484 return (s->kind == SK_CVAR);
5487 bool sRef_isConst (sRef s)
5489 PREDTEST (sRef_isConst, s);
5490 return (s->kind == SK_CONST);
5493 bool sRef_isObject (sRef s)
5495 PREDTEST (sRef_isObject, s);
5496 return (s->kind == SK_OBJECT);
5499 bool sRef_isExternal (sRef s)
5501 PREDTEST (sRef_isExternal, s);
5502 return (s->kind == SK_EXTERNAL);
5505 static bool sRef_isDerived (sRef s)
5507 PREDTEST (sRef_isDerived, s);
5508 return (s->kind == SK_DERIVED);
5511 bool sRef_isField (sRef s)
5513 PREDTEST (sRef_isField, s);
5514 return (s->kind == SK_FIELD);
5517 static bool sRef_isIndex (sRef s)
5519 PREDTEST (sRef_isIndex, s);
5520 return (s->kind == SK_ARRAYFETCH);
5523 bool sRef_isAnyParam (sRef s)
5525 PREDTEST (sRef_isAnyParam, s);
5526 return (s->kind == SK_PARAM);
5529 bool sRef_isParam (sRef s)
5531 PREDTEST (sRef_isParam, s);
5532 return (s->kind == SK_PARAM);
5535 bool sRef_isDirectParam (sRef s)
5537 PREDTEST (sRef_isDirectParam, s);
5539 return ((s->kind == SK_CVAR) &&
5540 (s->info->cvar->lexlevel == functionScope) &&
5541 (context_inFunction () &&
5542 (s->info->cvar->index <= uentryList_size (context_getParams ()))));
5545 bool sRef_isPointer (sRef s)
5547 PREDTEST (sRef_isPointer, s);
5548 return (s->kind == SK_PTR);
5552 ** returns true if storage referenced by s is visible
5555 bool sRef_isReference (sRef s)
5557 PREDTEST (sRef_isReference, s);
5559 return (sRef_isPointer (s) || sRef_isIndex (s) || sRef_isGlobal (s)
5560 || (sRef_isField (s) && (sRef_isReference (s->info->field->rec))));
5563 bool sRef_isIReference (sRef s)
5565 return (sRef_isPointer (s) || sRef_isAddress (s) || sRef_isIndex (s)
5566 || sRef_isField (s) || sRef_isArrayFetch (s));
5569 bool sRef_isGlobal (sRef s)
5571 return (sRef_isCvar (s) && (s->info->cvar->lexlevel <= fileScope));
5574 bool sRef_isRealGlobal (sRef s)
5576 return (sRef_isCvar (s) && (s->info->cvar->lexlevel == globScope));
5579 bool sRef_isFileStatic (sRef s)
5581 return (sRef_isCvar (s) && (s->info->cvar->lexlevel == fileScope));
5584 bool sRef_isAliasCheckedGlobal (sRef s)
5586 if (sRef_isGlobal (s))
5588 uentry ue = sRef_getUentry (s);
5590 return context_checkAliasGlob (ue);
5598 void sRef_free (/*@only@*/ sRef s)
5600 if (s != sRef_undefined && s->kind != SK_SPECIAL)
5602 alinfo_free (s->expinfo);
5603 alinfo_free (s->aliasinfo);
5604 alinfo_free (s->definfo);
5605 alinfo_free (s->nullinfo);
5607 sRefSet_free (s->deriv);
5608 s->deriv = sRefSet_undefined;
5615 void sRef_setType (sRef s, ctype t)
5617 if (sRef_isValid (s))
5623 void sRef_setTypeFull (sRef s, ctype t)
5625 if (sRef_isValid (s))
5629 sRefSet_allElements (s->deriv, current)
5631 sRef_setTypeFull (current, ctype_unknown);
5632 } end_sRefSet_allElements ;
5637 sRef_buildField (sRef rec, /*@dependent@*/ cstring f)
5639 return (sRef_buildNCField (rec, f));
5642 static /*@exposed@*/ sRef
5643 sRef_findDerivedField (/*@notnull@*/ sRef rec, cstring f)
5645 sRefSet_allElements (rec->deriv, sr)
5647 if (sRef_isValid (sr))
5649 if (sr->kind == SK_FIELD && cstring_equal (sr->info->field->field, f))
5654 } end_sRefSet_allElements;
5656 return sRef_undefined;
5659 /*@dependent@*/ /*@observer@*/ sRefSet
5660 sRef_derivedFields (sRef rec)
5662 if (sRef_isValid (rec))
5670 return (sRefSet_undefined);
5674 static /*@exposed@*/ sRef
5675 sRef_findDerivedPointer (sRef s)
5677 if (sRef_isValid (s))
5679 sRefSet_realElements (s->deriv, sr)
5681 if (sRef_isValid (sr) && sr->kind == SK_PTR)
5685 } end_sRefSet_realElements;
5688 return sRef_undefined;
5692 sRef_isUnknownArrayFetch (sRef s)
5694 return (sRef_isValid (s)
5695 && s->kind == SK_ARRAYFETCH
5696 && !s->info->arrayfetch->indknown);
5699 static /*@exposed@*/ sRef
5700 sRef_findDerivedArrayFetch (/*@notnull@*/ sRef s, bool isknown, int idx, bool dead)
5705 sRefSet_realElements (s->deriv, sr)
5707 if (sRef_isValid (sr)
5708 && sr->kind == SK_ARRAYFETCH
5709 && sr->info->arrayfetch->indknown
5710 && (sr->info->arrayfetch->ind == idx))
5714 } end_sRefSet_realElements;
5718 sRefSet_realElements (s->deriv, sr)
5720 if (sRef_isValid (sr)
5721 && sr->kind == SK_ARRAYFETCH
5722 && (!sr->info->arrayfetch->indknown
5723 || (sr->info->arrayfetch->indknown &&
5724 sr->info->arrayfetch->ind == 0)))
5726 if (sRef_isDead (sr) || sRef_isKept (sr))
5728 if (dead || context_getFlag (FLG_STRICTUSERELEASED))
5738 } end_sRefSet_realElements;
5741 return sRef_undefined;
5744 static /*@exposed@*/ sRef
5745 sRef_buildNCField (/*@exposed@*/ sRef rec, /*@exposed@*/ cstring f)
5749 if (sRef_isInvalid (rec))
5751 return sRef_undefined;
5755 ** check if the field already has been referenced
5758 s = sRef_findDerivedField (rec, f);
5760 if (sRef_isValid (s))
5766 ctype ct = ctype_realType (rec->type);
5770 s->info = (sinfo) dmalloc (sizeof (*s->info));
5771 s->info->field = (fldinfo) dmalloc (sizeof (*s->info->field));
5772 s->info->field->rec = rec;
5773 s->info->field->field = f; /* doesn't copy f */
5776 if (ctype_isKnown (ct) && ctype_isSU (ct))
5778 uentry ue = uentryList_lookupField (ctype_getFields (ct), f);
5780 if (!uentry_isUndefined (ue))
5782 s->type = uentry_getType (ue);
5784 if (ctype_isMutable (s->type)
5785 && rec->aliaskind != AK_STACK
5786 && !alkind_isStatic (rec->aliaskind))
5788 s->aliaskind = rec->aliaskind;
5792 s->aliaskind = AK_UNKNOWN;
5795 if (sRef_isStateDefined (rec) || sRef_isStateUnknown (rec)
5796 || sRef_isPdefined (rec))
5798 sRef_setStateFromUentry (s, ue);
5802 sRef_setPartsFromUentry (s, ue);
5805 s->oaliaskind = s->aliaskind;
5806 s->oexpkind = s->expkind;
5811 Never report this as an error. It can happen whenever there
5812 is casting involved.
5816 llcontbug (message ("buildNCField --- no field %s: %q / %s",
5817 f, sRef_unparse (s), ctype_unparse (ct)));
5821 return sRef_undefined;
5825 if (rec->defstate == SS_DEFINED
5826 && (s->defstate == SS_UNDEFINED || s->defstate == SS_UNKNOWN))
5828 s->defstate = SS_DEFINED;
5830 else if (rec->defstate == SS_PARTIAL)
5832 s->defstate = SS_PARTIAL;
5834 else if (rec->defstate == SS_ALLOCATED)
5836 if (ctype_isStackAllocated (ct) && ctype_isStackAllocated (s->type))
5838 s->defstate = SS_ALLOCATED;
5842 s->defstate = SS_UNDEFINED;
5845 else if (s->defstate == SS_UNKNOWN)
5847 s->defstate = rec->defstate;
5854 if (s->defstate == SS_UNDEFINED)
5856 ctype rt = ctype_realType (s->type);
5858 if (ctype_isArray (rt) || ctype_isSU (rt))
5860 s->defstate = SS_ALLOCATED;
5864 sRef_addDeriv (rec, s);
5866 if (ctype_isInt (s->type) && cstring_equal (f, REFSNAME))
5868 s->aliaskind = AK_REFS;
5869 s->oaliaskind = AK_REFS;
5877 sRef_isStackAllocated (sRef s)
5879 return (sRef_isValid(s)
5880 && s->defstate == SS_ALLOCATED && ctype_isStackAllocated (s->type));
5884 void sRef_setArrayFetchState (/*@notnull@*/ /*@exposed@*/ sRef s,
5885 /*@notnull@*/ /*@exposed@*/ sRef arr)
5887 if (ctype_isRealAP (arr->type))
5889 s->type = ctype_baseArrayPtr (arr->type);
5892 /* a hack, methinks... makeArrayFetch (&a[0]) ==> a[] */
5893 if (sRef_isAddress (arr))
5895 sRef t = arr->info->ref;
5897 if (sRef_isArrayFetch (t))
5899 s->info->arrayfetch->arr = t->info->arrayfetch->arr;
5902 else if (ctype_isRealPointer (arr->type))
5904 sRef sp = sRef_findDerivedPointer (arr);
5907 if (sRef_isValid (sp))
5910 if (ctype_isMutable (s->type))
5912 sRef_setExKind (s, sRef_getExKind (sp), fileloc_undefined);
5915 s->aliaskind = sp->aliaskind;
5918 s->defstate = sp->defstate;
5920 if (s->defstate == SS_DEFINED)
5922 if (!context_getFlag (FLG_STRICTDESTROY))
5924 s->defstate = SS_PARTIAL;
5928 s->nullstate = sp->nullstate;
5932 if (arr->defstate == SS_UNDEFINED)
5934 s->defstate = SS_UNUSEABLE;
5936 else if ((arr->defstate == SS_ALLOCATED) && !ctype_isSU (s->type))
5938 s->defstate = SS_UNDEFINED;
5942 if (!context_getFlag (FLG_STRICTDESTROY))
5944 s->defstate = SS_PARTIAL;
5948 s->defstate = SS_DEFINED;
5952 ** Very weak checking for array elements.
5954 ** s->defstate = arr->defstate;
5958 sRef_setExKind (s, sRef_getExKind (arr), g_currentloc);
5960 if (arr->aliaskind == AK_LOCAL || arr->aliaskind == AK_FRESH)
5962 s->aliaskind = AK_LOCAL;
5966 s->aliaskind = AK_UNKNOWN;
5969 sRef_setTypeState (s);
5974 if (arr->defstate == SS_DEFINED)
5977 ** Very weak checking for array elements.
5979 ** s->defstate = arr->defstate;
5982 if (context_getFlag (FLG_STRICTDESTROY))
5984 s->defstate = SS_DEFINED;
5988 s->defstate = SS_PARTIAL;
5991 else if (arr->defstate == SS_ALLOCATED)
5993 if (ctype_isRealArray (s->type))
5995 s->defstate = SS_ALLOCATED;
5999 if (!s->info->arrayfetch->indknown)
6002 ** is index is unknown, elements is defined or
6003 ** allocated is any element is!
6006 s->defstate = SS_UNDEFINED;
6008 sRefSet_allElements (arr->deriv, sr)
6010 if (sRef_isValid (sr))
6012 if (sr->defstate == SS_ALLOCATED)
6014 s->defstate = SS_ALLOCATED;
6018 if (sr->defstate == SS_DEFINED)
6020 if (context_getFlag (FLG_STRICTDESTROY))
6022 s->defstate = SS_DEFINED;
6026 s->defstate = SS_PARTIAL;
6033 } end_sRefSet_allElements;
6038 s->defstate = SS_UNDEFINED;
6044 s->defstate = arr->defstate;
6049 ** kludgey way to guess where aliaskind applies
6052 if (ctype_isMutable (s->type)
6053 && !ctype_isPointer (arr->type)
6054 && !alkind_isStatic (arr->aliaskind)
6055 && !alkind_isStack (arr->aliaskind)) /* evs - 2000-06-20: don't pass stack allocation to members */
6057 s->aliaskind = arr->aliaskind;
6061 s->aliaskind = AK_UNKNOWN;
6064 sRef_setTypeState (s);
6067 if (sRef_isObserver (arr))
6069 s->expkind = XO_OBSERVER;
6073 /*@exposed@*/ sRef sRef_buildArrayFetch (/*@exposed@*/ sRef arr)
6077 if (!sRef_isValid (arr)) {
6078 /*@-nullret@*/ return arr /*@=nullret@*/;
6081 if (ctype_isRealPointer (arr->type))
6083 (void) sRef_buildPointer (arr); /* do this to define arr! */
6086 s = sRef_findDerivedArrayFetch (arr, FALSE, 0, FALSE);
6088 if (sRef_isValid (s))
6090 sRef_setExKind (s, sRef_getExKind (arr), g_currentloc);
6097 s->kind = SK_ARRAYFETCH;
6098 s->info = (sinfo) dmalloc (sizeof (*s->info));
6099 s->info->arrayfetch = (ainfo) dmalloc (sizeof (*s->info->arrayfetch));
6100 s->info->arrayfetch->indknown = FALSE;
6101 s->info->arrayfetch->ind = 0;
6102 s->info->arrayfetch->arr = arr;
6103 sRef_setArrayFetchState (s, arr);
6104 s->oaliaskind = s->aliaskind;
6105 s->oexpkind = s->expkind;
6107 if (!context_inProtectVars ())
6109 sRef_addDeriv (arr, s);
6117 sRef_buildArrayFetchKnown (/*@exposed@*/ sRef arr, int i)
6121 if (!sRef_isValid (arr)) {
6122 /*@-nullret@*/ return arr /*@=nullret@*/;
6125 if (ctype_isRealPointer (arr->type))
6127 (void) sRef_buildPointer (arr); /* do this to define arr! */
6130 s = sRef_findDerivedArrayFetch (arr, TRUE, i, FALSE);
6132 if (sRef_isValid (s))
6134 sRef_setExKind (s, sRef_getExKind (arr), g_currentloc);
6141 s->kind = SK_ARRAYFETCH;
6142 s->info = (sinfo) dmalloc (sizeof (*s->info));
6143 s->info->arrayfetch = (ainfo) dmalloc (sizeof (*s->info->arrayfetch));
6144 s->info->arrayfetch->arr = arr;
6145 s->info->arrayfetch->indknown = TRUE;
6146 s->info->arrayfetch->ind = i;
6148 sRef_setArrayFetchState (s, arr);
6150 s->oaliaskind = s->aliaskind;
6151 s->oexpkind = s->expkind;
6152 sRef_addDeriv (arr, s);
6159 ** sets everything except for defstate
6163 sRef_setPartsFromUentry (sRef s, uentry ue)
6166 llassert (sRef_isValid (s));
6168 s->aliaskind = alkind_derive (s->aliaskind, uentry_getAliasKind (ue));
6169 s->oaliaskind = s->aliaskind;
6171 if (s->expkind == XO_UNKNOWN)
6173 s->expkind = uentry_getExpKind (ue);
6176 s->oexpkind = s->expkind;
6178 if (s->nullstate == NS_UNKNOWN)
6180 s->nullstate = sRef_getNullState (uentry_getSref (ue));
6183 if (s->aliaskind == AK_IMPONLY
6184 && (sRef_isExposed (s) || sRef_isObserver (s)))
6186 s->oaliaskind = s->aliaskind = AK_IMPDEPENDENT;
6192 sRef_setStateFromAbstractUentry (sRef s, uentry ue)
6194 llassert (sRef_isValid (s));
6196 sRef_setPartsFromUentry (s, ue);
6198 s->aliaskind = alkind_derive (s->aliaskind, uentry_getAliasKind (ue));
6199 s->oaliaskind = s->aliaskind;
6201 if (s->expkind == XO_UNKNOWN)
6203 s->expkind = uentry_getExpKind (ue);
6206 s->oexpkind = s->expkind;
6210 sRef_setStateFromUentry (sRef s, uentry ue)
6214 llassert (sRef_isValid (s));
6216 sRef_setPartsFromUentry (s, ue);
6218 defstate = uentry_getDefState (ue);
6220 if (sstate_isKnown (defstate))
6222 s->defstate = defstate;
6231 sRef_buildPointer (/*@exposed@*/ sRef t)
6233 DPRINTF (("build pointer: %s", sRef_unparse (t)));
6235 if (sRef_isInvalid (t)) return sRef_undefined;
6237 if (sRef_isAddress (t))
6239 DPRINTF (("Return ref: %s", sRef_unparse (t->info->ref)));
6240 return (t->info->ref);
6244 sRef s = sRef_findDerivedPointer (t);
6246 DPRINTF (("find derived: %s", sRef_unparse (s)));
6248 if (sRef_isValid (s))
6251 sRef_setExKind (s, sRef_getExKind (t), g_currentloc);
6252 s->oaliaskind = s->aliaskind;
6253 s->oexpkind = s->expkind;
6259 s = sRef_constructPointerAux (t);
6261 DPRINTF (("construct: %s", sRef_unparse (s)));
6263 if (sRef_isValid (s))
6265 sRef_addDeriv (t, s);
6267 s->oaliaskind = s->aliaskind;
6268 s->oexpkind = s->expkind;
6277 sRef_constructPointer (sRef t)
6280 return sRef_buildPointer (t);
6283 static /*@exposed@*/ sRef sRef_constructDerefAux (sRef t, bool isdead)
6285 if (sRef_isValid (t))
6290 ** if there is a derived t[?], return that. Otherwise, *t.
6294 s = sRef_findDerivedArrayFetch (t, FALSE, 0, isdead);
6296 if (sRef_isValid (s))
6302 sRef ret = sRef_constructPointer (t);
6305 ** This is necessary to prevent infinite depth
6306 ** in checking complete destruction.
6312 /* ret->defstate = SS_UNKNOWN; */
6323 return sRef_undefined;
6327 sRef sRef_constructDeref (sRef t)
6329 return sRef_constructDerefAux (t, FALSE);
6332 sRef sRef_constructDeadDeref (sRef t)
6334 return sRef_constructDerefAux (t, TRUE);
6338 sRef_constructPointerAux (/*@notnull@*/ /*@exposed@*/ sRef t)
6340 sRef s = sRef_new ();
6345 s->info = (sinfo) dmalloc (sizeof (*s->info));
6348 if (ctype_isRealAP (rt))
6350 s->type = ctype_baseArrayPtr (rt);
6353 st = ctype_realType (s->type);
6356 if (t->defstate == SS_UNDEFINED)
6358 s->defstate = SS_UNUSEABLE;
6360 else if ((t->defstate == SS_ALLOCATED) && !ctype_isSU (st))
6362 s->defstate = SS_UNDEFINED;
6366 s->defstate = t->defstate;
6369 if (t->aliaskind == AK_LOCAL || t->aliaskind == AK_FRESH)
6371 s->aliaskind = AK_LOCAL;
6375 s->aliaskind = AK_UNKNOWN;
6378 sRef_setExKind (s, sRef_getExKind (t), fileloc_undefined);
6379 sRef_setTypeState (s);
6381 s->oaliaskind = s->aliaskind;
6382 s->oexpkind = s->expkind;
6387 bool sRef_hasDerived (sRef s)
6389 return (sRef_isValid (s) && !sRefSet_isEmpty (s->deriv));
6393 sRef_clearDerived (sRef s)
6395 if (sRef_isValid (s))
6397 sRefSet_clear (s->deriv);
6402 sRef_clearDerivedComplete (sRef s)
6405 if (sRef_isValid (s))
6407 sRef base = sRef_getBaseSafe (s);
6409 while (sRef_isValid (base))
6411 sRefSet_clear (base->deriv);
6412 base = sRef_getBaseSafe (base);
6415 sRefSet_clear (s->deriv);
6420 sRef_makePointer (sRef s)
6423 sRef res = sRef_buildPointer (s);
6425 DPRINTF (("Res: %s", sRef_unparse (res)));
6430 ** &a[] => a (this is for out params)
6434 sRef_makeAnyArrayFetch (/*@exposed@*/ sRef arr)
6437 if (sRef_isAddress (arr))
6439 return (arr->info->ref);
6443 return (sRef_buildArrayFetch (arr));
6448 sRef_makeArrayFetch (sRef arr)
6450 return (sRef_buildArrayFetch (arr));
6454 sRef_makeArrayFetchKnown (sRef arr, int i)
6456 return (sRef_buildArrayFetchKnown (arr, i));
6460 sRef_makeField (sRef rec, /*@dependent@*/ cstring f)
6463 ret = sRef_buildField (rec, f);
6468 sRef_makeNCField (sRef rec, /*@dependent@*/ cstring f)
6470 return (sRef_buildNCField (rec, f));
6474 sRef_unparseKindName (sRef s)
6478 if (s == sRef_undefined) return cstring_makeLiteral ("<invalid>");
6480 s = sRef_fixConj (s);
6485 if (sRef_isLocalVar (s))
6487 result = cstring_makeLiteral ("Variable");
6491 result = cstring_makeLiteral ("Undef global");
6495 result = cstring_makeLiteral ("Out parameter");
6498 if (sRef_isAnyParam (s->info->arrayfetch->arr))
6500 result = cstring_makeLiteral ("Out parameter");
6502 else if (sRef_isIndexKnown (s))
6504 result = cstring_makeLiteral ("Array element");
6508 result = cstring_makeLiteral ("Value");
6512 if (sRef_isAnyParam (s->info->ref))
6514 result = cstring_makeLiteral ("Out parameter");
6518 result = cstring_makeLiteral ("Value");
6522 result = cstring_makeLiteral ("Value");
6525 result = cstring_makeLiteral ("Field");
6528 result = cstring_makeLiteral ("Object");
6530 case SK_UNCONSTRAINED:
6531 result = cstring_makeLiteral ("<anything>");
6540 result = cstring_makeLiteral ("<unknown>");
6543 result = cstring_makeLiteral ("<conj>");
6546 result = cstring_makeLiteral ("Storage");
6554 sRef_unparseKindNamePlain (sRef s)
6558 if (s == sRef_undefined) return cstring_makeLiteral ("<invalid>");
6560 s = sRef_fixConj (s);
6565 if (sRef_isLocalVar (s))
6567 result = cstring_makeLiteral ("Variable");
6571 result = cstring_makeLiteral ("Global");
6575 result = cstring_makeLiteral ("Parameter");
6578 if (sRef_isAnyParam (s->info->arrayfetch->arr))
6580 result = cstring_makeLiteral ("Parameter");
6582 else if (sRef_isIndexKnown (s))
6584 result = cstring_makeLiteral ("Array element");
6588 result = cstring_makeLiteral ("Value");
6592 if (sRef_isAnyParam (s->info->ref))
6594 result = cstring_makeLiteral ("Parameter");
6598 result = cstring_makeLiteral ("Value");
6602 result = cstring_makeLiteral ("Value");
6605 result = cstring_makeLiteral ("Field");
6608 result = cstring_makeLiteral ("Object");
6611 result = cstring_makeLiteral ("Storage");
6613 case SK_UNCONSTRAINED:
6614 result = cstring_makeLiteral ("<anything>");
6623 result = cstring_makeLiteral ("<unknown>");
6626 result = cstring_makeLiteral ("<conj>");
6638 sRef_copyState (sRef s1, sRef s2)
6640 if (sRef_isValid (s1) && sRef_isValid (s2))
6642 s1->defstate = s2->defstate;
6644 s1->nullstate = s2->nullstate;
6645 s1->nullinfo = alinfo_update (s1->nullinfo, s2->nullinfo);
6647 /* start modifications */
6648 s1->bufinfo.bufstate = s2->bufinfo.bufstate;
6649 s1->bufinfo.len = s2->bufinfo.len;
6650 s1->bufinfo.size = s2->bufinfo.size;
6651 /* end modifications */
6653 s1->aliaskind = s2->aliaskind;
6654 s1->aliasinfo = alinfo_update (s1->aliasinfo, s2->aliasinfo);
6656 s1->expkind = s2->expkind;
6657 s1->expinfo = alinfo_update (s1->expinfo, s2->expinfo);
6659 s1->safe = s2->safe;
6664 sRef_makeNew (ctype ct, sRef t, cstring name)
6666 sRef s = sRef_new ();
6671 llassert (sRef_isValid (t));
6672 s->defstate = t->defstate;
6674 s->aliaskind = t->aliaskind;
6675 s->oaliaskind = s->aliaskind;
6677 s->nullstate = t->nullstate;
6679 s->expkind = t->expkind;
6680 s->oexpkind = s->expkind;
6682 s->info = (sinfo) dmalloc (sizeof (*s->info));
6683 s->info->fname = name;
6685 /* start modifications */
6686 s->bufinfo.bufstate = t->bufinfo.bufstate;
6687 /* end modifications */
6693 sRef_makeType (ctype ct)
6695 sRef s = sRef_new ();
6700 s->defstate = SS_UNKNOWN;
6701 s->aliaskind = AK_UNKNOWN;
6702 s->nullstate = NS_UNKNOWN;
6703 /* start modification */
6704 s->bufinfo.bufstate = BB_NOTNULLTERMINATED;
6705 /* end modification */
6708 if (ctype_isUA (ct))
6710 typeId uid = ctype_typeId (ct);
6711 uentry ue = usymtab_getTypeEntrySafe (uid);
6713 if (uentry_isValid (ue))
6715 sRef_mergeStateQuiet (s, uentry_getSref (ue));
6719 s->oaliaskind = s->aliaskind;
6720 s->oexpkind = s->expkind;
6726 sRef_makeConst (ctype ct)
6728 sRef s = sRef_new ();
6733 s->defstate = SS_UNKNOWN;
6734 s->aliaskind = AK_UNKNOWN;
6735 s->nullstate = NS_UNKNOWN;
6736 /* start modification */
6737 s->bufinfo.bufstate = BB_NULLTERMINATED;
6738 /* end modification */
6741 if (ctype_isUA (ct))
6743 typeId uid = ctype_typeId (ct);
6744 uentry te = usymtab_getTypeEntrySafe (uid);
6746 if (uentry_isValid (te))
6748 sRef_mergeStateQuiet (s, uentry_getSref (te));
6753 s->oaliaskind = s->aliaskind;
6754 s->oexpkind = s->expkind;
6759 bool sRef_hasName (sRef s)
6761 if (sRef_isInvalid (s))
6770 uentry u = usymtab_getRefQuiet (s->info->cvar->lexlevel,
6771 s->info->cvar->index);
6772 return (uentry_hasName (u));
6776 uentry u = uentryList_getN (context_getParams (),
6779 return (uentry_hasName (u));
6786 bool sRef_sameObject (sRef s1, sRef s2)
6788 return sRef_sameName(s1, s2);
6791 sRef_sameName (sRef s1, sRef s2)
6793 if (sRef_isInvalid (s1))
6795 return sRef_isInvalid (s2);
6798 if (sRef_isInvalid (s2))
6806 if (s2->kind == SK_CVAR)
6808 return (s1->info->cvar->lexlevel == s2->info->cvar->lexlevel
6809 && s1->info->cvar->index == s2->info->cvar->index);
6811 else if (s2->kind == SK_PARAM)
6813 if (context_inFunctionLike ())
6815 uentry u1 = usymtab_getRefQuiet (s1->info->cvar->lexlevel,
6816 s1->info->cvar->index);
6817 uentry u2 = uentryList_getN (context_getParams (),
6820 return (cstring_equalFree (uentry_getName (u1),
6821 uentry_getName (u2)));
6834 if (s2->kind == SK_PARAM)
6836 return (s1->info->paramno == s2->info->paramno);
6838 else if (s2->kind == SK_CVAR)
6840 if (context_inFunctionLike ())
6842 uentry u1 = uentryList_getN (context_getParams (),
6844 uentry u2 = usymtab_getRefQuiet (s2->info->cvar->lexlevel,
6845 s2->info->cvar->index);
6848 return (cstring_equalFree (uentry_getName (u1),
6849 uentry_getName (u2)));
6862 case SK_UNCONSTRAINED:
6866 if (s2->kind == SK_ARRAYFETCH)
6868 if (bool_equal (s1->info->arrayfetch->indknown,
6869 s2->info->arrayfetch->indknown))
6871 if (!s1->info->arrayfetch->indknown
6872 || (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind))
6874 return sRef_sameName (s1->info->arrayfetch->arr,
6875 s2->info->arrayfetch->arr);
6882 if (s2->kind == SK_FIELD)
6884 if (cstring_equal (s1->info->field->field,
6885 s2->info->field->field))
6887 return sRef_sameName (s1->info->field->rec,
6888 s2->info->field->rec);
6897 if (s2->kind == s1->kind)
6899 return sRef_sameName (s1->info->ref,
6907 return sRef_sameName (sRef_getConjA (s1), s2);
6911 return (s2->kind == SK_UNKNOWN);
6914 if (s2->kind == s1->kind)
6916 return (ctype_equal (s1->type, s2->type));
6921 if (s2->kind == SK_SPECIAL)
6923 return (s1->info->spec == s2->info->spec);
6927 return (s2->kind == SK_RESULT);
6935 sRef_fixOuterRef (/*@returned@*/ sRef s)
6937 sRef root = sRef_getRootBase (s);
6939 if (sRef_isCvar (root))
6941 uentry ue = usymtab_getRefQuiet (root->info->cvar->lexlevel,
6942 root->info->cvar->index);
6944 if (uentry_isValid (ue))
6946 sRef uref = uentry_getSref (ue);
6947 sRef sr = sRef_fixBase (s, uref);
6953 llcontbug (message ("sRef_fixOuterRef: undefined: %q", sRef_unparseDebug (s)));
6962 sRef_storeState (sRef s)
6964 if (sRef_isInvalid (s)) return;
6966 s->oaliaskind = s->aliaskind;
6967 s->oexpkind = s->expkind;
6970 static void sRef_resetStateAux (sRef s, /*@unused@*/ fileloc loc)
6972 sRef_resetState (s);
6976 sRef_resetState (sRef s)
6978 bool changed = FALSE;
6979 if (sRef_isInvalid (s)) return;
6982 if (s->oaliaskind == AK_KILLREF && !sRef_isParam (s))
6985 ** killref is used in a kludgey way, to save having to add
6986 ** another alias kind (see usymtab_handleParams)
6989 if (s->expkind != s->oexpkind)
6992 s->expkind = s->oexpkind;
6997 if (s->expkind != s->oexpkind)
7000 s->expkind = s->oexpkind;
7003 if (s->aliaskind != s->oaliaskind
7004 && s->aliaskind != AK_REFCOUNTED
7005 && s->aliaskind != AK_REFS)
7008 s->aliaskind = s->oaliaskind;
7014 sRef_clearDerived (s);
7020 sRef_resetStateComplete (sRef s)
7022 sRef_innerAliasSetComplete (sRef_resetStateAux, s, fileloc_undefined);
7026 sRef_fixBase (/*@returned@*/ sRef s, /*@returned@*/ sRef base)
7028 sRef tmp = sRef_undefined;
7031 if (sRef_isInvalid (s)) return s;
7032 if (sRef_isInvalid (base)) return base;
7042 tmp = sRef_fixBase (s->info->arrayfetch->arr, base);
7044 if (s->info->arrayfetch->indknown)
7046 ret = sRef_makeArrayFetchKnown (tmp, s->info->arrayfetch->ind);
7050 ret = sRef_makeArrayFetch (tmp);
7054 tmp = sRef_fixBase (s->info->field->rec, base);
7055 ret = sRef_buildNCField (tmp, s->info->field->field);
7058 tmp = sRef_fixBase (s->info->ref, base);
7059 ret = sRef_makePointer (tmp);
7062 tmp = sRef_fixBase (s->info->ref, base);
7063 ret = sRef_makeAddress (tmp);
7069 tmp = sRef_fixBase (s->info->conj->a, base);
7070 tmpb = sRef_fixBase (s->info->conj->b, base);
7072 ret = sRef_makeConj (tmp, tmpb);
7081 static /*@exposed@*/ sRef
7082 sRef_fixDirectBase (sRef s, sRef base)
7087 if (sRef_isInvalid (s))
7089 return sRef_undefined;
7095 if (s->info->arrayfetch->indknown)
7097 ret = sRef_makeArrayFetchKnown (base, s->info->arrayfetch->ind);
7101 ret = sRef_makeArrayFetch (base);
7105 ret = sRef_buildNCField (base, s->info->field->field);
7108 ret = sRef_makePointer (base);
7111 ret = sRef_makeAddress (base);
7117 tmpa = sRef_fixDirectBase (s->info->conj->a, base);
7118 tmpb = sRef_fixDirectBase (s->info->conj->b, base);
7120 ret = sRef_makeConj (tmpa, tmpb);
7126 sRef_copyState (ret, s);
7131 sRef_isAllocIndexRef (sRef s)
7133 return (sRef_isArrayFetch (s) && !(s->info->arrayfetch->indknown)
7134 && sRef_isAllocated (s->info->arrayfetch->arr));
7138 sRef_showRefLost (sRef s)
7140 if (sRef_hasAliasInfoLoc (s))
7142 llgenindentmsg (cstring_makeLiteral ("Original reference lost"),
7143 sRef_getAliasInfoLoc (s));
7148 sRef_showRefKilled (sRef s)
7150 if (sRef_hasStateInfoLoc (s))
7152 llgenindentmsg (message ("Storage %q released",
7153 sRef_unparse (s)), sRef_getStateInfoLoc (s));
7158 sRef_showStateInconsistent (sRef s)
7160 if (sRef_hasStateInfoLoc (s))
7163 (message ("Storage %qbecomes inconsistent (released on one branch)",
7164 sRef_unparseOpt (s)),
7165 sRef_getStateInfoLoc (s));
7170 sRef_showStateInfo (sRef s)
7172 if (sRef_hasStateInfoLoc (s))
7174 if (s->defstate == SS_DEAD)
7177 (message ("Storage %qis released", sRef_unparseOpt (s)),
7178 sRef_getStateInfoLoc (s));
7180 else if (s->defstate == SS_ALLOCATED || s->defstate == SS_DEFINED)
7183 (message ("Storage %qis %s", sRef_unparseOpt (s),
7184 sstate_unparse (s->defstate)),
7185 sRef_getStateInfoLoc (s));
7187 else if (s->defstate == SS_UNUSEABLE)
7190 (message ("Storage %qbecomes inconsistent (clauses merge with"
7191 "%qreleased on one branch)",
7192 sRef_unparseOpt (s),
7193 sRef_unparseOpt (s)),
7194 sRef_getStateInfoLoc (s));
7198 llgenindentmsg (message ("Storage %qbecomes %s",
7199 sRef_unparseOpt (s),
7200 sstate_unparse (s->defstate)),
7201 sRef_getStateInfoLoc (s));
7207 sRef_showExpInfo (sRef s)
7209 if (sRef_hasExpInfoLoc (s))
7211 llgenindentmsg (message ("Storage %qbecomes %s", sRef_unparseOpt (s),
7212 exkind_unparse (s->expkind)),
7213 sRef_getExpInfoLoc (s));
7218 sRef_showNullInfo (sRef s)
7220 if (sRef_hasNullInfoLoc (s) && sRef_isKnown (s))
7222 switch (s->nullstate)
7226 fileloc loc = sRef_getNullInfoLoc (s);
7228 if (fileloc_isDefined (loc) && !fileloc_isLib (loc))
7231 (message ("Storage %qbecomes null", sRef_unparseOpt (s)),
7238 fileloc loc = sRef_getNullInfoLoc (s);
7240 if (fileloc_isDefined (loc) && !fileloc_isLib (loc))
7242 llgenindentmsg (message ("Storage %qbecomes null", sRef_unparseOpt (s)),
7250 (message ("Storage %qmay become null", sRef_unparseOpt (s)),
7251 sRef_getNullInfoLoc (s));
7256 (message ("Storage %qbecomes not null", sRef_unparseOpt (s)),
7257 sRef_getNullInfoLoc (s));
7261 (message ("Storage %qnull state becomes unknown",
7262 sRef_unparseOpt (s)),
7263 sRef_getNullInfoLoc (s));
7272 (message ("<error case> Storage %q becomes %s",
7274 nstate_unparse (s->nullstate)),
7275 sRef_getNullInfoLoc (s));
7283 sRef_showAliasInfo (sRef s)
7285 if (sRef_hasAliasInfoLoc (s))
7287 if (sRef_isFresh (s))
7290 (message ("Fresh storage %qallocated", sRef_unparseOpt (s)),
7291 sRef_getAliasInfoLoc (s));
7295 if (!sRef_isRefCounted (s))
7298 (message ("Storage %qbecomes %s",
7299 sRef_unparseOpt (s),
7300 alkind_unparse (sRef_getAliasKind (s))),
7301 sRef_getAliasInfoLoc (s));
7308 sRef_mergeNullState (sRef s, nstate n)
7310 if (sRef_isValid (s))
7316 if (n != old && n != NS_UNKNOWN)
7320 s->nullinfo = alinfo_updateLoc (s->nullinfo, g_currentloc);
7325 llbuglit ("sRef_mergeNullState: invalid");
7330 sRef_possiblyNull (sRef s)
7332 if (sRef_isValid (s))
7334 if (s->nullstate == NS_ABSNULL)
7336 ctype rct = ctype_realType (s->type);
7338 if (ctype_isAbstract (rct))
7344 if (ctype_isUser (rct))
7346 uentry ue = usymtab_getTypeEntry (ctype_typeId (rct));
7348 return (nstate_possiblyNull
7349 (sRef_getNullState (uentry_getSref (ue))));
7359 return nstate_possiblyNull (s->nullstate);
7367 sRef_getScopeName (sRef s)
7369 sRef base = sRef_getRootBase (s);
7371 if (sRef_isRealGlobal (base))
7373 return (cstring_makeLiteralTemp ("Global"));
7375 else if (sRef_isFileStatic (base))
7377 return (cstring_makeLiteralTemp ("Static"));
7381 return (cstring_makeLiteralTemp ("Local"));
7386 sRef_unparseScope (sRef s)
7388 sRef base = sRef_getRootBase (s);
7390 if (sRef_isRealGlobal (base))
7392 return (cstring_makeLiteralTemp ("global"));
7394 else if (sRef_isFileStatic (base))
7396 return (cstring_makeLiteralTemp ("file static"));
7405 sRef_getScope (sRef s)
7407 llassert (sRef_isValid (s));
7409 if (sRef_isCvar (s))
7411 return s->info->cvar->lexlevel;
7413 else if (sRef_isParam (s))
7424 sRef_isDead (sRef s)
7426 return (sRef_isValid (s) && (s)->defstate == SS_DEAD);
7430 sRef_isDeadStorage (sRef s)
7432 if (sRef_isValid (s))
7434 if (s->defstate == SS_DEAD
7435 || s->defstate == SS_UNUSEABLE
7436 || s->defstate == SS_UNDEFINED
7437 || s->defstate == SS_UNKNOWN)
7443 return (sRef_isDefinitelyNull (s));
7453 sRef_isPossiblyDead (sRef s)
7455 return (sRef_isValid (s) && s->defstate == SS_HOFFA);
7458 bool sRef_isStateLive (sRef s)
7460 if (sRef_isValid (s))
7462 sstate ds = s->defstate;
7464 return (!(ds == SS_UNDEFINED
7466 || ds == SS_UNUSEABLE
7467 || ds == SS_HOFFA));
7476 bool sRef_isStateUndefined (sRef s)
7478 return ((sRef_isValid(s)) && ((s)->defstate == SS_UNDEFINED));
7481 bool sRef_isJustAllocated (sRef s)
7483 if (sRef_isAllocated (s))
7485 sRefSet_allElements (s->deriv, el)
7487 if (!(sRef_isStateUndefined (el) || sRef_isUnuseable (el)))
7491 } end_sRefSet_allElements ;
7500 sRef_isAllocatedStorage (sRef s)
7502 if (sRef_isValid (s) && ynm_toBoolStrict (sRef_isReadable (s)))
7504 return (ctype_isVisiblySharable (sRef_getType (s)));
7513 sRef_isUnuseable (sRef s)
7515 return ((sRef_isValid(s)) && ((s)->defstate == SS_UNUSEABLE));
7519 sRef_perhapsNull (sRef s)
7521 if (sRef_isValid (s))
7523 if (s->nullstate == NS_ABSNULL)
7525 ctype rct = ctype_realType (s->type);
7527 if (ctype_isAbstract (rct))
7533 if (ctype_isUser (rct))
7535 uentry ue = usymtab_getTypeEntry (ctype_typeId (rct));
7537 return (nstate_perhapsNull
7538 (sRef_getNullState (uentry_getSref (ue))));
7548 return nstate_perhapsNull (s->nullstate);
7556 ** definitelyNull --- called when TRUE is good
7560 sRef_definitelyNull (sRef s)
7562 return (sRef_isValid (s)
7563 && (s->nullstate == NS_DEFNULL || s->nullstate == NS_CONSTNULL));
7567 ** based on sRef_similar
7571 sRef_setDerivNullState (sRef set, sRef guide, nstate ns)
7573 if (sRef_isValid (set))
7575 sRef deriv = sRef_getDeriv (set, guide);
7577 if (sRef_isValid (deriv))
7579 deriv->nullstate = ns;
7584 static /*@exposed@*/ sRef
7585 sRef_getDeriv (/*@returned@*/ /*@notnull@*/ sRef set, sRef guide)
7587 llassert (sRef_isValid (set));
7588 llassert (sRef_isValid (guide));
7590 switch (guide->kind)
7593 llassert (set->kind == SK_CVAR);
7598 llassert (set->kind == guide->kind);
7599 llassert (set->info->paramno == guide->info->paramno);
7605 if (set->kind == SK_ARRAYFETCH
7606 && (sRef_similar (set->info->arrayfetch->arr,
7607 guide->info->arrayfetch->arr)))
7613 return (sRef_makeAnyArrayFetch
7614 (sRef_getDeriv (set, guide->info->arrayfetch->arr)));
7619 if (set->kind == SK_PTR && sRef_similar (set->info->ref, guide->info->ref))
7625 return (sRef_makePointer (sRef_getDeriv (set, guide->info->ref)));
7630 if ((set->kind == SK_FIELD &&
7631 (sRef_similar (set->info->field->rec, guide->info->field->rec) &&
7632 cstring_equal (set->info->field->field, guide->info->field->field))))
7638 return (sRef_makeField (sRef_getDeriv (set, guide->info->field->rec),
7639 guide->info->field->field));
7643 if ((set->kind == SK_ADR) && sRef_similar (set->info->ref, guide->info->ref))
7649 return (sRef_makeAddress (sRef_getDeriv (set, guide->info->ref)));
7654 return sRef_undefined;
7658 case SK_UNCONSTRAINED:
7666 return sRef_undefined;
7673 ** sRef_aliasCheckPred
7675 ** A confusing but spiffy function:
7677 ** Calls predf (s, e, text, <alias>) on s and all of s's aliases
7678 ** (unless checkAliases (s) is FALSE).
7680 ** For alias calls, calls as
7681 ** predf (alias, e, text, s)
7685 sRef_aliasCheckPred (bool (predf) (sRef, exprNode, sRef, exprNode),
7686 /*@null@*/ bool (checkAliases) (sRef),
7687 sRef s, exprNode e, exprNode err)
7689 bool error = (*predf)(s, e, sRef_undefined, err);
7692 if (checkAliases != NULL && !(checkAliases (s)))
7694 /* don't check aliases */
7698 sRefSet aliases = usymtab_allAliases (s);
7701 sRefSet_realElements (aliases, current)
7703 if (sRef_isValid (current))
7705 if (!sRef_similar (current, s)
7706 || (error && sRef_sameName (current, s)))
7708 (void) (*predf)(current, e, s, err);
7711 } end_sRefSet_realElements;
7713 sRefSet_free (aliases);
7718 ** return TRUE iff predf (s) is true for s or any alias of s
7722 sRef_aliasCheckSimplePred (sRefTest predf, sRef s)
7733 aliases = usymtab_allAliases (s);
7735 sRefSet_realElements (aliases, current)
7737 if (sRef_isValid (current))
7739 sRef cref = sRef_updateSref (current);
7741 /* Whoa! a very kludgey way to make sure the right sref is used
7742 ** where there is a conditional symbol table. I am beginning
7743 ** to think that having a conditional symbol table wasn't such
7750 sRefSet_free (aliases);
7754 } end_sRefSet_realElements;
7756 sRefSet_free (aliases);
7762 sRef_aliasCompleteSimplePred (bool (predf) (sRef), sRef s)
7765 bool result = FALSE;
7768 aliases = usymtab_allAliases (s);
7770 if ((*predf)(s)) result = TRUE;
7773 sRefSet_realElements (aliases, current)
7775 if (sRef_isValid (current))
7777 current = sRef_updateSref (current);
7778 if ((*predf)(current)) result = TRUE;
7780 } end_sRefSet_realElements;
7782 sRefSet_free (aliases);
7787 sRef_aliasSetComplete (void (predf) (sRef, fileloc), sRef s, fileloc loc)
7791 aliases = usymtab_allAliases (s);
7795 sRefSet_realElements (aliases, current)
7797 if (sRef_isValid (current))
7799 current = sRef_updateSref (current);
7800 ((*predf)(current, loc));
7802 } end_sRefSet_realElements;
7804 sRefSet_free (aliases);
7808 sRef_aliasSetCompleteParam (void (predf) (sRef, alkind, fileloc), sRef s,
7809 alkind kind, fileloc loc)
7814 if (sRef_isDeep (s))
7816 aliases = usymtab_allAliases (s);
7820 aliases = usymtab_aliasedBy (s);
7823 (*predf)(s, kind, loc);
7825 sRefSet_realElements (aliases, current)
7827 if (sRef_isValid (current))
7829 current = sRef_updateSref (current);
7830 ((*predf)(current, kind, loc));
7832 } end_sRefSet_realElements;
7834 sRefSet_free (aliases);
7838 sRef_innerAliasSetComplete (void (predf) (sRef, fileloc), sRef s, fileloc loc)
7844 if (!sRef_isValid (s)) return;
7848 ** Type equivalence checking is necessary --- there might be casting.
7855 case SK_UNCONSTRAINED:
7860 inner = s->info->ref;
7861 aliases = usymtab_allAliases (inner);
7862 ct = sRef_getType (inner);
7865 sRefSet_realElements (aliases, current)
7867 if (sRef_isValid (current))
7869 current = sRef_updateSref (current);
7871 if (ctype_equal (ct, sRef_getType (current)))
7873 sRef ptr = sRef_makePointer (current);
7875 ((*predf)(ptr, loc));
7878 } end_sRefSet_realElements;
7880 sRefSet_free (aliases);
7883 inner = s->info->arrayfetch->arr;
7884 aliases = usymtab_allAliases (inner);
7885 ct = sRef_getType (inner);
7887 sRefSet_realElements (aliases, current)
7889 if (sRef_isValid (current))
7891 current = sRef_updateSref (current);
7893 if (ctype_equal (ct, sRef_getType (current)))
7896 if (s->info->arrayfetch->indknown)
7898 sRef af = sRef_makeArrayFetchKnown (current, s->info->arrayfetch->ind);
7900 ((*predf)(af, loc));
7904 sRef af = sRef_makeArrayFetch (current);
7906 ((*predf)(af, loc));
7910 } end_sRefSet_realElements;
7912 sRefSet_free (aliases);
7915 inner = s->info->field->rec;
7916 aliases = usymtab_allAliases (inner);
7917 ct = sRef_getType (inner);
7920 sRefSet_realElements (aliases, current)
7922 if (sRef_isValid (current))
7924 current = sRef_updateSref (current);
7926 if (ctype_equal (ct, sRef_getType (current)))
7928 sRef f = sRef_makeField (current, s->info->field->field);
7933 } end_sRefSet_realElements;
7935 sRefSet_free (aliases);
7938 sRef_innerAliasSetComplete (predf, s->info->conj->a, loc);
7939 sRef_innerAliasSetComplete (predf, s->info->conj->b, loc);
7956 sRef_innerAliasSetCompleteParam (void (predf) (sRef, sRef), sRef s, sRef t)
7962 if (!sRef_isValid (s)) return;
7966 ** Type equivalence checking is necessary --- there might be casting.
7973 case SK_UNCONSTRAINED:
7978 inner = s->info->ref;
7979 aliases = usymtab_allAliases (inner);
7980 ct = sRef_getType (inner);
7983 sRefSet_realElements (aliases, current)
7985 if (sRef_isValid (current))
7987 current = sRef_updateSref (current);
7989 if (ctype_equal (ct, sRef_getType (current)))
7991 sRef ptr = sRef_makePointer (current);
7996 } end_sRefSet_realElements;
7998 sRefSet_free (aliases);
8001 inner = s->info->arrayfetch->arr;
8002 aliases = usymtab_allAliases (inner);
8003 ct = sRef_getType (inner);
8005 sRefSet_realElements (aliases, current)
8007 if (sRef_isValid (current))
8009 current = sRef_updateSref (current);
8011 if (ctype_equal (ct, sRef_getType (current)))
8014 if (s->info->arrayfetch->indknown)
8016 sRef af = sRef_makeArrayFetchKnown (current, s->info->arrayfetch->ind);
8022 sRef af = sRef_makeArrayFetch (current);
8028 } end_sRefSet_realElements;
8030 sRefSet_free (aliases);
8033 inner = s->info->field->rec;
8034 aliases = usymtab_allAliases (inner);
8035 ct = sRef_getType (inner);
8038 sRefSet_realElements (aliases, current)
8040 if (sRef_isValid (current))
8042 current = sRef_updateSref (current);
8044 if (ctype_equal (ct, sRef_getType (current)))
8046 sRef f = sRef_makeField (current, s->info->field->field);
8051 } end_sRefSet_realElements;
8053 sRefSet_free (aliases);
8056 sRef_innerAliasSetCompleteParam (predf, s->info->conj->a, t);
8057 sRef_innerAliasSetCompleteParam (predf, s->info->conj->b, t);
8073 static void sRef_combineExKinds (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other)
8075 exkind a1 = sRef_getExKind (res);
8076 exkind a2 = sRef_getExKind (other);
8078 if (a1 == a2 || a2 == XO_UNKNOWN)
8082 else if (a1 == XO_UNKNOWN)
8084 res->expinfo = alinfo_update (res->expinfo, other->expinfo);
8089 res->expkind = XO_OBSERVER;
8094 ** Currently, this is a very ad hoc implementation, with lots of fixes to
8095 ** make real code work okay. I need to come up with some more general
8096 ** rules or principles here.
8100 sRef_combineAliasKindsError (/*@notnull@*/ sRef res,
8101 /*@notnull@*/ sRef other,
8102 clause cl, fileloc loc)
8104 bool hasError = FALSE;
8105 alkind ares = sRef_getAliasKind (res);
8106 alkind aother = sRef_getAliasKind (other);
8108 if (alkind_isDependent (ares))
8110 if (aother == AK_KEPT)
8112 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8113 res->aliaskind = AK_KEPT;
8117 if (aother == AK_LOCAL || aother == AK_STATIC
8118 || alkind_isTemp (aother))
8120 res->aliaskind = AK_DEPENDENT;
8124 else if (alkind_isDependent (aother))
8126 if (ares == AK_KEPT)
8128 res->aliaskind = AK_KEPT;
8132 if (ares == AK_LOCAL || ares == AK_STATIC || alkind_isTemp (ares))
8134 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8135 res->aliaskind = AK_DEPENDENT;
8139 else if ((ares == AK_LOCAL || ares == AK_UNIQUE
8140 || ares == AK_STATIC || alkind_isTemp (ares))
8141 && sRef_isFresh (other))
8144 ** cases like: if (s == NULL) s = malloc...;
8145 ** don't generate errors
8148 if (usymtab_isAltProbablyDeepNull (res))
8150 res->aliaskind = ares;
8157 else if ((aother == AK_LOCAL || aother == AK_UNIQUE
8158 || aother == AK_STATIC || alkind_isTemp (aother))
8159 && sRef_isFresh (res))
8162 ** cases like: if (s == NULL) s = malloc...;
8163 ** don't generate errors
8166 if (usymtab_isProbableDeepNull (other))
8168 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8169 res->aliaskind = aother;
8176 else if (ares == AK_NEWREF && aother == AK_REFCOUNTED
8177 && sRef_isConst (other))
8179 res->aliaskind = AK_NEWREF;
8181 else if (aother == AK_NEWREF && ares == AK_REFCOUNTED
8182 && sRef_isConst (res))
8184 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8185 res->aliaskind = AK_NEWREF;
8187 else if (sRef_isLocalVar (res)
8188 && ((ares == AK_KEPT && aother == AK_LOCAL)
8189 || (aother == AK_KEPT && ares == AK_LOCAL)))
8191 res->aliaskind = AK_KEPT;
8200 if (sRef_isThroughArrayFetch (res))
8203 (FLG_BRANCHSTATE, FLG_STRICTBRANCHSTATE,
8205 ("Clauses exit with %q possibly referencing %s storage %s, "
8208 alkind_unparse (aother),
8209 clause_nameTaken (cl),
8210 alkind_unparse (ares),
8211 clause_nameAlternate (cl)),
8214 sRef_showAliasInfo (res);
8215 sRef_showAliasInfo (other);
8216 res->aliaskind = AK_ERROR;
8220 if (ares == AK_KEPT || aother == AK_KEPT)
8222 sRef_maybeKill (res, loc);
8230 message ("Clauses exit with %q referencing %s storage %s, "
8233 alkind_unparse (aother),
8234 clause_nameTaken (cl),
8235 alkind_unparse (ares),
8236 clause_nameAlternate (cl)),
8239 sRef_showAliasInfo (res);
8240 sRef_showAliasInfo (other);
8242 res->aliaskind = AK_ERROR;
8246 res->aliaskind = (sRef_isLocalVar (res) ? AK_LOCAL : AK_UNKNOWN);
8251 sRef_combineAliasKinds (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other,
8252 clause cl, fileloc loc)
8254 alkind ares = sRef_getAliasKind (res);
8255 alkind aother = sRef_getAliasKind (other);
8257 if (alkind_equal (ares, aother)
8258 || aother == AK_UNKNOWN
8259 || aother == AK_ERROR)
8261 ; /* keep current state */
8263 else if (sRef_isDead (res) || sRef_isDead (other))
8265 /* dead error reported (or storage is dead) */
8266 res ->aliaskind = AK_ERROR;
8268 else if (ares == AK_UNKNOWN || ares == AK_ERROR
8269 || sRef_isStateUndefined (res))
8271 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8272 res->aliaskind = aother;
8274 else if (sRef_isStateUndefined (other))
8278 else if (((ares == AK_UNIQUE || alkind_isTemp (ares))
8279 && aother == AK_LOCAL)
8280 || ((aother == AK_UNIQUE || alkind_isTemp (aother))
8281 && ares == AK_LOCAL))
8283 if (ares != AK_LOCAL)
8285 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8288 res->aliaskind = AK_LOCAL;
8290 else if ((ares == AK_OWNED && aother == AK_FRESH)
8291 || (aother == AK_OWNED && ares == AK_FRESH))
8293 if (ares != AK_FRESH)
8295 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8298 res->aliaskind = AK_FRESH;
8300 else if ((ares == AK_KEEP && aother == AK_FRESH) ||
8301 (aother == AK_KEEP && ares == AK_FRESH))
8303 if (ares != AK_KEEP)
8305 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8308 res->aliaskind = AK_KEEP;
8310 else if ((ares == AK_LOCAL && aother == AK_STACK) ||
8311 (aother == AK_LOCAL && ares == AK_STACK))
8313 if (ares != AK_STACK)
8315 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8318 res->aliaskind = AK_STACK;
8320 else if ((ares == AK_LOCAL
8321 && (aother == AK_OWNED && sRef_isLocalVar (other)))
8322 || (aother == AK_LOCAL
8323 && (ares == AK_OWNED && sRef_isLocalVar (res))))
8325 if (ares != AK_LOCAL)
8327 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8330 res->aliaskind = AK_LOCAL;
8332 else if ((ares == AK_FRESH && alkind_isOnly (aother))
8333 || (aother == AK_FRESH && alkind_isOnly (ares)))
8335 res->aliaskind = AK_FRESH;
8337 else if ((aother == AK_FRESH && sRef_definitelyNull (res))
8338 || (ares == AK_FRESH && sRef_definitelyNull (other)))
8340 if (ares != AK_FRESH)
8342 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8343 res->aliaskind = AK_FRESH;
8346 else if ((sRef_isFresh (res) && sRef_isConst (other))
8347 || (sRef_isFresh (other) && sRef_isConst (res)))
8350 ** for NULL constantants
8354 if (!sRef_isFresh (res))
8356 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8359 res->aliaskind = AK_FRESH;
8361 else if ((alkind_isStatic (aother) && sRef_isConst (res))
8362 || (alkind_isStatic (ares) && sRef_isConst (other)))
8364 if (!alkind_isStatic (ares))
8366 res->aliasinfo = alinfo_update (res->aliasinfo, other->aliasinfo);
8367 res->aliaskind = AK_STATIC;
8372 sRef_combineAliasKindsError (res, other, cl, loc);
8376 static void sRef_combineDefState (/*@notnull@*/ sRef res,
8377 /*@notnull@*/ sRef other)
8379 sstate s1 = res->defstate;
8380 sstate s2 = other->defstate;
8383 if (s1 == s2 || s2 == SS_UNKNOWN)
8387 else if (s1 == SS_UNKNOWN)
8396 if (s2 == SS_DEFINED)
8402 llcontbuglit ("ssfixed: not implemented");
8411 flip = (s2 != SS_DEFINED);
8429 res->definfo = alinfo_update (res->definfo, other->definfo);
8434 extern /*@notnull@*/ sRef sRef_getConjA (sRef s)
8437 llassert (sRef_isConj (s));
8439 ret = s->info->conj->a;
8440 llassert (ret != NULL);
8444 extern /*@notnull@*/ sRef sRef_getConjB (sRef s)
8447 llassert (sRef_isConj (s));
8449 ret = s->info->conj->b;
8450 llassert (ret != NULL);
8454 extern /*@exposed@*/ sRef sRef_makeArrow (sRef s, /*@dependent@*/ cstring f)
8459 p = sRef_makePointer (s);
8460 ret = sRef_makeField (p, f);
8464 extern /*@exposed@*/ sRef sRef_buildArrow (sRef s, cstring f)
8469 p = sRef_buildPointer (s);
8470 ret = sRef_buildField (p, f);
8475 static /*@null@*/ sinfo sinfo_copy (/*@notnull@*/ sRef s)
8482 ret = (sinfo) dmalloc (sizeof (*ret));
8483 ret->cvar = (cref) dmalloc (sizeof (*ret->cvar));
8484 ret->cvar->lexlevel = s->info->cvar->lexlevel;
8485 ret->cvar->index = s->info->cvar->index;
8489 ret = (sinfo) dmalloc (sizeof (*ret));
8490 ret->paramno = s->info->paramno;
8494 ret = (sinfo) dmalloc (sizeof (*ret));
8495 ret->arrayfetch = (ainfo) dmalloc (sizeof (*ret->arrayfetch));
8496 ret->arrayfetch->indknown = s->info->arrayfetch->indknown;
8497 ret->arrayfetch->ind = s->info->arrayfetch->ind;
8498 ret->arrayfetch->arr = s->info->arrayfetch->arr;
8502 ret = (sinfo) dmalloc (sizeof (*ret));
8503 ret->field = (fldinfo) dmalloc (sizeof (*ret->field));
8504 ret->field->rec = s->info->field->rec;
8505 ret->field->field = s->info->field->field;
8509 ret = (sinfo) dmalloc (sizeof (*ret));
8510 ret->object = s->info->object;
8517 ret = (sinfo) dmalloc (sizeof (*ret));
8518 ret->ref = s->info->ref;
8522 ret = (sinfo) dmalloc (sizeof (*ret));
8523 ret->conj = (cjinfo) dmalloc (sizeof (*ret->conj));
8524 ret->conj->a = s->info->conj->a;
8525 ret->conj->b = s->info->conj->b;
8528 ret = (sinfo) dmalloc (sizeof (*ret));
8529 ret->spec = s->info->spec;
8531 case SK_UNCONSTRAINED:
8533 ret = (sinfo) dmalloc (sizeof (*ret));
8534 ret->fname = s->info->fname;
8540 llassertprint (s->info == NULL, ("s = %s", sRef_unparse (s)));
8548 static /*@null@*/ sinfo sinfo_fullCopy (/*@notnull@*/ sRef s)
8553 ** Since its a full copy, only storage is assigned
8554 ** to dependent fields.
8561 ret = (sinfo) dmalloc (sizeof (*ret));
8562 ret->cvar = (cref) dmalloc (sizeof (*ret->cvar));
8563 ret->cvar->lexlevel = s->info->cvar->lexlevel;
8564 ret->cvar->index = s->info->cvar->index;
8568 ret = (sinfo) dmalloc (sizeof (*ret));
8569 ret->paramno = s->info->paramno;
8573 ret = (sinfo) dmalloc (sizeof (*ret));
8574 ret->arrayfetch = (ainfo) dmalloc (sizeof (*ret->arrayfetch));
8575 ret->arrayfetch->indknown = s->info->arrayfetch->indknown;
8576 ret->arrayfetch->ind = s->info->arrayfetch->ind;
8577 ret->arrayfetch->arr = sRef_saveCopy (s->info->arrayfetch->arr);
8581 ret = (sinfo) dmalloc (sizeof (*ret));
8582 ret->field = (fldinfo) dmalloc (sizeof (*ret->field));
8583 ret->field->rec = sRef_saveCopy (s->info->field->rec);
8584 ret->field->field = s->info->field->field;
8588 ret = (sinfo) dmalloc (sizeof (*ret));
8589 ret->object = s->info->object;
8596 ret = (sinfo) dmalloc (sizeof (*ret));
8597 ret->ref = sRef_saveCopy (s->info->ref);
8601 ret = (sinfo) dmalloc (sizeof (*ret));
8602 ret->conj = (cjinfo) dmalloc (sizeof (*ret->conj));
8603 ret->conj->a = sRef_saveCopy (s->info->conj->a);
8604 ret->conj->b = sRef_saveCopy (s->info->conj->b);
8607 ret = (sinfo) dmalloc (sizeof (*ret));
8608 ret->spec = s->info->spec;
8611 case SK_UNCONSTRAINED:
8612 ret = (sinfo) dmalloc (sizeof (*ret));
8613 ret->fname = s->info->fname;
8619 llassert (s->info == NULL);
8629 sinfo_update (/*@notnull@*/ /*@exposed@*/ sRef res,
8630 /*@notnull@*/ /*@exposed@*/ sRef other)
8632 llassert (res->kind == other->kind);
8637 res->info->cvar->lexlevel = other->info->cvar->lexlevel;
8638 res->info->cvar->index = other->info->cvar->index;
8642 res->info->paramno = other->info->paramno;
8646 res->info->arrayfetch->indknown = other->info->arrayfetch->indknown;
8647 res->info->arrayfetch->ind = other->info->arrayfetch->ind;
8648 res->info->arrayfetch->arr = other->info->arrayfetch->arr;
8652 res->info->field->rec = other->info->field->rec;
8653 res->info->field->field = other->info->field->field;
8657 res->info->object = other->info->object;
8664 res->info->ref = other->info->ref;
8668 res->info->conj->a = other->info->conj->a;
8669 res->info->conj->b = other->info->conj->b;
8673 res->info->spec = other->info->spec;
8677 case SK_UNCONSTRAINED:
8678 res->info->fname = other->info->fname;
8685 llassert (res->info == NULL);
8690 static void sinfo_free (/*@special@*/ /*@temp@*/ /*@notnull@*/ sRef s)
8691 /*@uses s->kind, s->info@*/
8692 /*@releases s->info@*/
8697 sfree (s->info->cvar);
8704 sfree (s->info->arrayfetch);
8708 sfree (s->info->field);
8721 sfree (s->info->conj);
8724 case SK_UNCONSTRAINED:
8737 bool sRef_isNSLocalVar (sRef s)
8739 if (sRef_isLocalVar (s))
8741 uentry ue = sRef_getUentry (s);
8743 return (!uentry_isStatic (ue));
8751 bool sRef_isLocalVar (sRef s)
8753 if (sRef_isValid(s))
8755 return (s->kind == SK_CVAR
8756 && (s->info->cvar->lexlevel > fileScope));
8762 bool sRef_isRealLocalVar (sRef s)
8764 if (sRef_isValid(s))
8766 if (s->kind == SK_CVAR)
8768 if (s->info->cvar->lexlevel == functionScope)
8770 uentry ue = sRef_getUentry (s);
8772 if (uentry_isAnyParam (ue)
8773 || uentry_isRefParam (ue))
8784 return (s->info->cvar->lexlevel > functionScope);
8792 bool sRef_isLocalParamVar (sRef s)
8794 if (sRef_isValid(s))
8796 return (s->kind == SK_PARAM
8797 || (s->kind == SK_CVAR
8798 && (s->info->cvar->lexlevel > fileScope)));
8804 static speckind speckind_fromInt (int i)
8807 llassert (i >= SR_NOTHING && i <= SR_SYSTEM);
8810 return ((speckind) i);
8813 void sRef_combineNullState (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other)
8815 nstate n1 = res->nullstate;
8816 nstate n2 = other->nullstate;
8820 if (n1 == n2 || n2 == NS_UNKNOWN)
8826 /* note: n2 is not unknown or defnull */
8830 case NS_ERROR: nn = NS_ERROR; break;
8831 case NS_UNKNOWN: flip = TRUE; nn = n2; break;
8832 case NS_POSNULL: break;
8833 case NS_DEFNULL: nn = NS_POSNULL; break;
8834 case NS_RELNULL: break;
8836 if (n2 == NS_MNOTNULL)
8847 if (n2 == NS_NOTNULL)
8866 res->nullinfo = alinfo_update (res->nullinfo, other->nullinfo);
8869 res->nullstate = nn;
8872 cstring sRef_nullMessage (sRef s)
8874 llassert (sRef_isValid (s));
8876 switch (s->nullstate)
8880 return (cstring_makeLiteralTemp ("null"));
8882 return (cstring_makeLiteralTemp ("possibly null"));
8887 /*@observer@*/ cstring sRef_ntMessage (sRef s)
8889 llassert (sRef_isValid (s));
8891 switch (s->nullstate)
8895 return (cstring_makeLiteralTemp ("not nullterminated"));
8897 return (cstring_makeLiteralTemp ("possibly non-nullterminated"));
8904 sRef sRef_fixResultType (/*@returned@*/ sRef s, ctype typ, uentry ue)
8906 sRef tmp = sRef_undefined;
8909 llassert (sRef_isValid (s));
8920 tmp = sRef_fixResultType (s->info->arrayfetch->arr, typ, ue);
8922 ct = ctype_realType (sRef_getType (tmp));
8925 if (ctype_isKnown (ct))
8927 if (ctype_isAP (ct))
8936 ("Special clause indexes non-array (%t): *%q",
8937 ct, sRef_unparse (s->info->arrayfetch->arr)),
8938 uentry_whereLast (ue));
8942 tmp = sRef_fixResultType (s->info->arrayfetch->arr, typ, ue);
8944 if (s->info->arrayfetch->indknown)
8946 ret = sRef_makeArrayFetchKnown (tmp, s->info->arrayfetch->ind);
8950 ret = sRef_makeArrayFetch (tmp);
8956 sRef rec = sRef_fixResultType (s->info->field->rec, typ, ue);
8957 ctype ct = ctype_realType (sRef_getType (rec));
8959 if (ctype_isKnown (ct))
8961 if (ctype_isSU (ct))
8963 if (uentry_isValid (uentryList_lookupField (ctype_getFields (ct),
8964 s->info->field->field)))
8973 ("Special clause accesses non-existent field of result: %q.%s",
8974 sRef_unparse (rec), s->info->field->field),
8975 uentry_whereLast (ue));
8983 ("Special clause accesses field of non-struct or union result (%t): %q.%s",
8984 ct, sRef_unparse (rec), s->info->field->field),
8985 uentry_whereLast (ue));
8989 ret = sRef_makeField (tmp, s->info->field->field);
8995 tmp = sRef_fixResultType (s->info->ref, typ, ue);
8997 ct = ctype_realType (sRef_getType (tmp));
8999 if (ctype_isKnown (ct))
9001 if (ctype_isAP (ct))
9010 ("Special clause dereferences non-pointer (%t): *%q",
9011 ct, sRef_unparse (s->info->ref)),
9012 uentry_whereLast (ue));
9016 ret = sRef_makePointer (tmp);
9023 ("Special clause uses & operator (not allowed): &%q", sRef_unparse (s->info->ref)),
9024 uentry_whereLast (ue));
9033 bool sRef_isOnly (sRef s)
9035 return (sRef_isValid(s) && alkind_isOnly (s->aliaskind));
9038 bool sRef_isDependent (sRef s)
9040 return (sRef_isValid(s) && alkind_isDependent (s->aliaskind));
9043 bool sRef_isOwned (sRef s)
9045 return (sRef_isValid (s) && (s->aliaskind == AK_OWNED));
9048 bool sRef_isKeep (sRef s)
9050 return (sRef_isValid (s) && (s->aliaskind == AK_KEEP));
9053 bool sRef_isTemp (sRef s)
9055 return (sRef_isValid (s) && alkind_isTemp (s->aliaskind));
9058 bool sRef_isLocalState (sRef s)
9060 return (sRef_isValid (s) && (s->aliaskind == AK_LOCAL));
9063 bool sRef_isUnique (sRef s)
9065 return (sRef_isValid (s) && (s->aliaskind == AK_UNIQUE));
9068 bool sRef_isShared (sRef s)
9070 return (sRef_isValid (s) && (s->aliaskind == AK_SHARED));
9073 bool sRef_isExposed (sRef s)
9075 return (sRef_isValid (s) && (s->expkind == XO_EXPOSED));
9078 bool sRef_isObserver (sRef s)
9080 return (sRef_isValid (s) && (s->expkind == XO_OBSERVER));
9083 bool sRef_isFresh (sRef s)
9085 return (sRef_isValid (s) && (s->aliaskind == AK_FRESH));
9088 bool sRef_isDefinitelyNull (sRef s)
9090 return (sRef_isValid (s) && (s->nullstate == NS_DEFNULL
9091 || s->nullstate == NS_CONSTNULL));
9094 bool sRef_isAllocated (sRef s)
9096 return (sRef_isValid (s) && (s->defstate == SS_ALLOCATED));
9099 bool sRef_isStack (sRef s)
9101 return (sRef_isValid (s) && (s->aliaskind == AK_STACK));
9104 extern bool sRef_isNotNull (sRef s)
9106 return (sRef_isValid(s) && (s->nullstate == NS_MNOTNULL
9107 || s->nullstate == NS_NOTNULL));
9110 /* start modifications */
9111 struct _bbufinfo sRef_getNullTerminatedState (sRef p_s) {
9112 struct _bbufinfo BUFSTATE_UNKNOWN;
9113 BUFSTATE_UNKNOWN.bufstate = BB_NOTNULLTERMINATED;
9114 BUFSTATE_UNKNOWN.size = 0;
9115 BUFSTATE_UNKNOWN.len = 0;
9117 if (sRef_isValid(p_s))
9118 return p_s->bufinfo;
9119 return BUFSTATE_UNKNOWN;
9122 void sRef_setNullTerminatedState(sRef p_s) {
9123 if(sRef_isValid (p_s)) {
9124 p_s->bufinfo.bufstate = BB_NULLTERMINATED;
9126 llfatalbug( message("sRef_setNT passed a invalid sRef\n"));
9131 void sRef_setPossiblyNullTerminatedState(sRef p_s) {
9132 if( sRef_isValid (p_s)) {
9133 p_s->bufinfo.bufstate = BB_POSSIBLYNULLTERMINATED;
9135 llfatalbug( message("sRef_setPossNT passed a invalid sRef\n"));
9139 void sRef_setNotNullTerminatedState(sRef p_s) {
9140 if( sRef_isValid (p_s)) {
9141 p_s->bufinfo.bufstate = BB_NOTNULLTERMINATED;
9143 llfatalbug( message("sRef_unsetNT passed a invalid sRef\n"));
9147 void sRef_setLen(sRef p_s, int len) {
9148 if( sRef_isValid (p_s) && sRef_isNullTerminated(p_s)) {
9149 p_s->bufinfo.len = len;
9151 llfatalbug( message("sRef_setLen passed a invalid sRef\n"));
9156 void sRef_setSize(sRef p_s, int size) {
9157 if( sRef_isValid(p_s)) {
9158 p_s->bufinfo.size = size;
9160 llfatalbug( message("sRef_setSize passed a invalid sRef\n"));
9164 void sRef_resetLen(sRef p_s) {
9165 if (sRef_isValid (p_s)) {
9166 p_s->bufinfo.len = 0;
9168 llfatalbug (message ("sRef_setLen passed an invalid sRef\n"));
9172 /*drl7x 11/28/2000 */
9174 bool sRef_isFixedArray (sRef p_s) {
9176 c = sRef_getType (p_s);
9177 return ( ctype_isFixedArray (c) );
9180 int sRef_getArraySize (sRef p_s) {
9182 llassert (sRef_isFixedArray(p_s) );
9184 c = sRef_getType (p_s);
9186 return (ctype_getArraySize (c) );