]> andersk Git - splint.git/blame - src/sRef.c
Fixed Makefile
[splint.git] / src / sRef.c
CommitLineData
616915dd 1/*
2** LCLint - annotation-assisted static program checker
28bf4b0b 3** Copyright (C) 1994-2001 University of Virginia,
616915dd 4** Massachusetts Institute of Technology
5**
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.
10**
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.
15**
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.
19**
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
23*/
24/*
25** storeRef.c
26**
27** Memory management:
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
31** function.
32**
33** storeRefs in the file and global scopes are free'd by the uentry.
34**
35*/
36
37# include "lclintMacros.nf"
38# include "basic.h"
39# include "exprChecks.h"
28bf4b0b 40# include "transferChecks.h"
616915dd 41# include "sRefTable.h"
42# include "structNames.h"
43
44/*
45** Predicate functions that evaluate both arguments in order.
46*/
47
48/*@notfunction@*/
49# define OR(a,b) (a ? (b, TRUE) : b)
50
51/*@notfunction@*/
52# define AND(a,b) (a ? b : (b, FALSE))
53
28bf4b0b 54static void sRef_checkValidAux (sRef p_s, sRefSet p_checkedsofar) /*@modifies p_checkedsofar@*/ ;
55
616915dd 56static bool sRef_isDerived (sRef p_s) /*@*/ ;
57
58static /*@exposed@*/ sRef sRef_fixDirectBase (sRef p_s, sRef p_base)
59 /*@modifies p_base@*/ ;
60
28bf4b0b 61static void sRef_updateNullState (sRef p_res, sRef p_other) /*@modifies p_res@*/ ;
62
616915dd 63static bool sRef_isAllocatedStorage (sRef p_s) /*@*/ ;
64static void sRef_setNullErrorLoc (sRef p_s, fileloc) /*@*/ ;
65
66static void
67 sRef_aliasSetComplete (void (p_predf) (sRef, fileloc), sRef p_s, fileloc p_loc)
68 /*@modifies p_s@*/ ;
69
70static int sRef_depth (sRef p_s) /*@*/ ;
71
72static void
73 sRef_innerAliasSetComplete (void (p_predf) (sRef, fileloc), sRef p_s,
74 fileloc p_loc)
75 /*@modifies p_s@*/ ;
76
77static void
28bf4b0b 78sRef_innerAliasSetCompleteParam (void (p_predf) (sRef, sRef), sRef p_s, sRef p_t)
79 /*@modifies p_s@*/ ;
80
616915dd 81static void
82 sRef_aliasSetCompleteParam (void (p_predf) (sRef, alkind, fileloc), sRef p_s,
83 alkind p_kind, fileloc p_loc)
84 /*@modifies p_s@*/ ;
85
86static speckind speckind_fromInt (int p_i);
87static bool sRef_equivalent (sRef p_s1, sRef p_s2);
88static bool sRef_isDeepUnionField (sRef p_s);
28bf4b0b 89static void sRef_addDeriv (/*@notnull@*/ sRef p_s, /*@notnull@*/ /*@exposed@*/ sRef p_t);
90static /*@dependent@*/ sRef sRef_makeResultType (ctype p_ct) /*@*/ ;
616915dd 91static bool sRef_checkModify (sRef p_s, sRefSet p_sl) /*@*/ ;
92
28bf4b0b 93static void sRef_checkMutable (/*@unused@*/ sRef s)
94{
95 /*@i235@*/
96 if (sRef_isValid (s) && s->immut)
97 {
98 llcontbug (message ("Modification to sRef marked immutable: %q",
99 sRef_unparseFull (s)));
100 }
101}
102
616915dd 103static bool skind_isSimple (skind sk)
104{
105 switch (sk)
106 {
107 case SK_PARAM: case SK_CVAR: case SK_CONST:
108 case SK_OBJECT: case SK_UNKNOWN: case SK_NEW:
109 return TRUE;
110 default:
111 return FALSE;
112 }
113}
114
115static void sinfo_free (/*@special@*/ /*@temp@*/ /*@notnull@*/ sRef p_s)
116 /*@uses p_s->kind, p_s->info@*/
117 /*@releases p_s->info@*/ ;
118
119static /*@null@*/ sinfo sinfo_copy (/*@notnull@*/ sRef p_s) /*@*/ ;
120static void sRef_setPartsFromUentry (sRef p_s, uentry p_ue)
121 /*@modifies p_s@*/ ;
122static bool checkDeadState (/*@notnull@*/ sRef p_el, bool p_tbranch, fileloc p_loc);
28bf4b0b 123static /*@dependent@*/ sRef sRef_constructPointerAux (/*@notnull@*/ /*@exposed@*/ sRef p_t) /*@*/ ;
616915dd 124
125static void
126 sRef_combineExKinds (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other)
127 /*@modifies p_res@*/ ;
128
129static void
130 sRef_combineAliasKinds (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other,
131 clause p_cl, fileloc p_loc)
132 /*@modifies p_res@*/ ;
133
134static void
135 sRef_combineNullState (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other)
136 /*@modifies p_res@*/ ;
137
138static void
139 sRef_combineDefState (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other)
140 /*@modifies p_res@*/ ;
141
142static void sRef_setStateFromAbstractUentry (sRef p_s, uentry p_ue)
143 /*@modifies p_s@*/ ;
144
145static void
146 sinfo_update (/*@notnull@*/ /*@exposed@*/ sRef p_res,
147 /*@notnull@*/ /*@exposed@*/ sRef p_other);
616915dd 148static void sRef_setDefinedAux (sRef p_s, fileloc p_loc, bool p_clear)
149 /*@modifies p_s@*/ ;
150static void sRef_setDefinedNoClear (sRef p_s, fileloc p_loc)
151 /*@modifies p_s@*/ ;
152static void sRef_setStateAux (sRef p_s, sstate p_ss, fileloc p_loc)
153 /*@modifies p_s@*/;
154
155static /*@exposed@*/ sRef
156 sRef_buildNCField (/*@exposed@*/ sRef p_rec, /*@exposed@*/ cstring p_f);
157
158static void
159 sRef_mergeStateAux (/*@notnull@*/ sRef p_res, /*@notnull@*/ sRef p_other,
160 clause p_cl, bool p_opt, fileloc p_loc,
161 bool p_doDerivs)
162 /*@modifies p_res, p_other@*/ ;
163
164static /*@null@*/ sinfo sinfo_fullCopy (/*@notnull@*/ sRef p_s);
165static bool sRef_doModify (sRef p_s, sRefSet p_sl) /*@modifies p_s@*/ ;
166static bool sRef_doModifyVal (sRef p_s, sRefSet p_sl) /*@modifies p_s@*/;
167static bool sRef_checkModifyVal (sRef p_s, sRefSet p_sl) /*@*/ ;
168
169static /*@only@*/ sRefSet
170 sRef_mergeDerivs (/*@only@*/ sRefSet p_res, sRefSet p_other,
171 bool p_opt, clause p_cl, fileloc p_loc);
172
173static /*@only@*/ sRefSet
174 sRef_mergeUnionDerivs (/*@only@*/ sRefSet p_res,
175 /*@exposed@*/ sRefSet p_other,
176 bool p_opt, clause p_cl, fileloc p_loc);
177
178static /*@only@*/ sRefSet
179 sRef_mergePdefinedDerivs (/*@only@*/ sRefSet p_res, sRefSet p_other, bool p_opt,
180 clause p_cl, fileloc p_loc);
181
182static /*@only@*/ cstring sRef_unparseWithArgs (sRef p_s, uentryList p_args);
183static /*@only@*/ cstring sRef_unparseNoArgs (sRef p_s);
184
185static /*@exposed@*/ sRef sRef_findDerivedPointer (sRef p_s);
186static /*@exposed@*/ sRef sRef_findDerivedField (/*@notnull@*/ sRef p_rec, cstring p_f);
187static /*@exposed@*/ sRef
188 sRef_getDeriv (/*@notnull@*/ /*@returned@*/ sRef p_set, sRef p_guide);
189
190static bool inFunction = FALSE;
191static /*@only@*/ sRefTable allRefs;
192
193/* # define DEBUGREFS */
194
195# ifdef DEBUGREFS
196static nsrefs = 0;
197static totnsrefs = 0;
198static maxnsrefs = 0;
199static ntotrefers = 0;
200static nrefers = 0;
201# endif
202
28bf4b0b 203static /*@checked@*/ bool protectDerivs = FALSE;
616915dd 204
28bf4b0b 205/*
206** Result of sRef_alloc is dependent since allRefs may
207** reference it. It is only if !inFunction.
208*/
209
210static /*@dependent@*/ /*@out@*/ /*@notnull@*/ sRef
211sRef_alloc (void)
212{
213 sRef s = (sRef) dmalloc (sizeof (*s));
214
215 s->immut = FALSE;
216
217 DPRINTF (("Alloc sref: [%p]", s));
218
219 if (inFunction)
220 {
221 allRefs = sRefTable_add (allRefs, s);
222 /*@-branchstate@*/
223 }
224 else
225 {
226 DPRINTF (("Not in function!"));
227 }
228
229 /*@=branchstate@*/
230
231# ifdef DEBUGREFS
232 if (nsrefs >= maxnsrefs)
233 {
234 maxnsrefs = nsrefs;
235 }
236
237 totnsrefs++;
238 nsrefs++;
239# endif
240
241 /*@-mustfree@*/ /*@-freshtrans@*/
242 return s;
243 /*@=mustfree@*/ /*@=freshtrans@*/
244}
245
246static void sRef_checkValidAux (sRef s, sRefSet checkedsofar)
247{
248 llassert (FALSE);
249
250 if (!sRef_isValid (s)) return;
251
252 if (sRefSet_containsSameObject (checkedsofar, s))
253 {
254 return;
255 }
256
257 /*@-temptrans@*/
258 checkedsofar = sRefSet_insert (checkedsofar, s);
259 /*@=temptrans@*/ /* checksofar will be destroyed before checkValid returns */
260
261 switch (s->kind)
262 {
263 case SK_UNCONSTRAINED:
264 llassert (cstring_length (s->info->fname) < 100);
265 break;
266
267 case SK_CVAR:
268 llassert (s->info->cvar->lexlevel >= 0);
269 /* llassert (s->info->cvar->lexlevel <= usymtab_getCurrentDepth ()); */
270 break;
271
272 case SK_PARAM:
273 llassert (s->info->paramno >= -1);
274 llassert (s->info->paramno <= 50); /*@i32 bogus...*/
275 break;
276
277 case SK_ARRAYFETCH:
278 sRef_checkValidAux (s->info->arrayfetch->arr, checkedsofar);
279 break;
280
281 case SK_FIELD:
282 sRef_checkValidAux (s->info->field->rec, checkedsofar);
283 llassert (cstring_length (s->info->field->field) < 100);
284 break;
285
286 case SK_PTR:
287 sRef_checkValidAux (s->info->ref, checkedsofar);
288 break;
289
290 case SK_ADR:
291 sRef_checkValidAux (s->info->ref, checkedsofar);
292 break;
293
294 case SK_OBJECT:
295 /* check ctype s->info->object */
296 break;
297
298 case SK_CONJ:
299 sRef_checkValidAux (s->info->conj->a, checkedsofar);
300 sRef_checkValidAux (s->info->conj->b, checkedsofar);
301 break;
302
303 case SK_NEW:
304 llassert (cstring_length (s->info->fname) < 100);
305 break;
306
307 case SK_DERIVED:
308 sRef_checkValidAux (s->info->ref, checkedsofar);
309 break;
310
311 case SK_EXTERNAL:
312 sRef_checkValidAux (s->info->ref, checkedsofar);
313 break;
314
315 case SK_TYPE:
316 case SK_CONST:
317 case SK_RESULT:
318 /* check ctyp s->type */
319 break;
320
321 case SK_SPECIAL:
322 llassert (s->info->spec == SR_NOTHING
323 || s->info->spec == SR_INTERNAL
324 || s->info->spec == SR_SPECSTATE
325 || s->info->spec == SR_SYSTEM);
326 break;
327
328 case SK_UNKNOWN:
329 break;
330
331 BADDEFAULT;
332 }
333
334
335 sRefSet_elements (s->deriv, el)
336 {
337 sRef_checkValidAux (el, checkedsofar);
338 } end_sRefSet_elements ;
339}
340
341void sRef_checkValid (/*@unused@*/ sRef s)
342{
343 return;
344 /*
345 sRefSet checkedsofar = sRefSet_new ();
346 sRef_checkValidAux (s, checkedsofar);
347 */
348}
349
350static /*@dependent@*/ /*@notnull@*/ /*@special@*/ sRef
351 sRef_new (void)
352 /*@defines result@*/
353 /*@ensures isnull result->aliasinfo, result->definfo,
354 result->expinfo, result->info, result->deriv, result->state@*/
355{
356 sRef s = sRef_alloc ();
357
358 s->kind = SK_UNKNOWN;
359 s->safe = TRUE;
360 s->modified = FALSE;
361 s->immut = FALSE;
362
363 s->type = ctype_unknown;
364 s->defstate = SS_UNKNOWN;
365
366 /* start modifications */
367 s->bufinfo.bufstate = BB_NOTNULLTERMINATED;
368 /* end modifications */
369
370 s->aliaskind = AK_UNKNOWN;
371 s->oaliaskind = AK_UNKNOWN;
372
373 s->nullstate = NS_UNKNOWN;
374
375 s->expkind = XO_UNKNOWN;
376 s->oexpkind = XO_UNKNOWN;
377
378 s->aliasinfo = stateInfo_undefined;
379 s->definfo = stateInfo_undefined;
380 s->nullinfo = stateInfo_undefined;
381 s->expinfo = stateInfo_undefined;
382
383 s->info = NULL;
384 s->deriv = sRefSet_undefined;
385
386 s->state = valueTable_undefined;
387
388 return s;
389}
390
391static /*@dependent@*/ /*@notnull@*/ /*@special@*/ sRef
392 sRef_newRef (void)
393 /*@defines result@*/
394 /*@ensures isnull result->aliasinfo, result->definfo,
395 result->expinfo, result->info, result->deriv@*/
396{
397 sRef res = sRef_new ();
398 res->immut = FALSE;
399 res->state = valueTable_undefined;
400 return res;
401}
616915dd 402
616915dd 403
404void sRef_protectDerivs (void) /*@modifies protectDerivs@*/
405{
406 llassert (!protectDerivs);
407 protectDerivs = TRUE;
408}
409
410void sRef_clearProtectDerivs (void) /*@modifies protectDerivs@*/
411{
412 llassert (protectDerivs);
413 protectDerivs = FALSE;
414}
415
416/*
417** hmmm...here be kind of a hack. This function mysteriously appeared
418** in my code, but I'm sure I didn't write it.
419*/
420
421bool
422sRef_isRecursiveField (sRef s)
423{
424 if (sRef_isField (s))
425 {
426 if (sRef_depth (s) > 13)
427 {
428 sRef base;
429 cstring fieldname;
430
431 fieldname = sRef_getField (s);
432 base = sRef_getBase (s);
433
434 while (sRef_isValid (base))
435 {
436 if (sRef_isField (base))
437 {
438 if (cstring_equal (fieldname, sRef_getField (base)))
439 {
440 return TRUE;
441 }
442 }
443
444 base = sRef_getBaseSafe (base);
445 }
446 }
447 }
448
449 return FALSE;
450}
451
452static void
28bf4b0b 453sRef_addDeriv (/*@notnull@*/ sRef s, /*@notnull@*/ /*@exposed@*/ sRef t)
616915dd 454{
455 if (!context_inProtectVars ()
456 && !protectDerivs
457 && sRef_isValid (s)
458 && sRef_isValid (t)
459 && !sRef_isConst (s))
460 {
461 int sd = sRef_depth (s);
462 int td = sRef_depth (t);
463
464 if (sd >= td)
465 {
466 return;
467 }
468
28bf4b0b 469 if (sRef_isFileOrGlobalScope (s))
616915dd 470 {
471 if (context_inFunctionLike ()
472 && ctype_isKnown (sRef_getType (s))
473 && !ctype_isFunction (sRef_getType (s)))
474 {
475 globSet g = context_getUsedGlobs ();
476
477 if (!globSet_member (g, s))
478 {
479 /*
480 ** don't report as a bug
481 **
482
483 llcontbug
484 (message ("sRef_addDeriv: global variable not in used "
485 "globs: %q / %s / %q",
486 sRef_unparse (s),
487 ctype_unparse (sRef_getType (s)),
488 sRefSet_unparse (s->deriv)));
489 */
490 }
491 else
492 {
493 s->deriv = sRefSet_insert (s->deriv, t);
494 }
495 }
496 }
497 else
498 {
28bf4b0b 499 DPRINTF (("Add deriv: [%p] %s / [%p] %s",
500 s, sRef_unparse (s),
501 t, sRef_unparse (t)));
502
616915dd 503 s->deriv = sRefSet_insert (s->deriv, t);
504 }
505 }
506}
507
508bool
509sRef_deepPred (bool (predf) (sRef), sRef s)
510{
511 if (sRef_isValid (s))
512 {
513 if ((*predf)(s)) return TRUE;
514
28bf4b0b 515 switch (s->kind)
616915dd 516 {
517 case SK_PTR:
518 return (sRef_deepPred (predf, s->info->ref));
519 case SK_ARRAYFETCH:
520 return (sRef_deepPred (predf, s->info->arrayfetch->arr));
521 case SK_FIELD:
522 return (sRef_deepPred (predf, s->info->field->rec));
523 case SK_CONJ:
524 return (sRef_deepPred (predf, s->info->conj->a)
525 || sRef_deepPred (predf, s->info->conj->b));
526 default:
527 return FALSE;
528 }
529 }
530
531 return FALSE;
532}
533
534bool sRef_modInFunction (void)
535{
536 return inFunction;
537}
538
539void sRef_setStateFromType (sRef s, ctype ct)
540{
541 if (sRef_isValid (s))
542 {
543 if (ctype_isUser (ct))
544 {
28bf4b0b 545 DPRINTF (("Here we are: %s", sRef_unparseFull (s)));
616915dd 546 sRef_setStateFromUentry
547 (s, usymtab_getTypeEntry (ctype_typeId (ct)));
548 }
549 else if (ctype_isAbstract (ct))
550 {
28bf4b0b 551 DPRINTF (("Here we are: %s", sRef_unparseFull (s)));
616915dd 552 sRef_setStateFromAbstractUentry
553 (s, usymtab_getTypeEntry (ctype_typeId (ct)));
554 }
555 else
556 {
557 ; /* not a user type */
558 }
559 }
560}
561
562static void sRef_setTypeState (sRef s)
563{
564 if (sRef_isValid (s))
565 {
566 sRef_setStateFromType (s, s->type);
567 }
568}
569
616915dd 570static bool
571 sRef_hasAliasInfoLoc (sRef s)
572{
573 return (sRef_isValid (s) && (s->aliasinfo != NULL)
574 && (fileloc_isDefined (s->aliasinfo->loc)));
575}
576
577static /*@falsenull@*/ bool
578sRef_hasStateInfoLoc (sRef s)
579{
580 return (sRef_isValid (s) && (s->definfo != NULL)
581 && (fileloc_isDefined (s->definfo->loc)));
582}
583
584static /*@falsenull@*/ bool
585sRef_hasExpInfoLoc (sRef s)
586{
587 return (sRef_isValid (s)
588 && (s->expinfo != NULL) && (fileloc_isDefined (s->expinfo->loc)));
589}
590
28bf4b0b 591static /*@observer@*/ /*@unused@*/ stateInfo sRef_getInfo (sRef s, cstring key)
592{
593 stateValue sv;
594
595 if (!sRef_isValid (s)) {
596 return stateInfo_undefined;
597 }
598
599 sv = valueTable_lookup (s->state, key);
600
601 if (stateValue_isDefined (sv))
602 {
603 return stateValue_getInfo (sv);
604 }
605
606 return stateInfo_undefined;
607}
608
609
616915dd 610static bool
611sRef_hasNullInfoLoc (sRef s)
612{
28bf4b0b 613 return (sRef_isValid (s) && s->nullinfo != NULL
616915dd 614 && (fileloc_isDefined (s->nullinfo->loc)));
615}
616
617bool
618sRef_hasAliasInfoRef (sRef s)
619{
620 return (sRef_isValid (s) && (s->aliasinfo != NULL)
621 && (sRef_isValid (s->aliasinfo->ref)));
622}
623
624static /*@observer@*/ fileloc
625sRef_getAliasInfoLoc (/*@exposed@*/ sRef s)
626{
627 llassert (sRef_isValid (s) && s->aliasinfo != NULL
628 && (fileloc_isDefined (s->aliasinfo->loc)));
629 return (s->aliasinfo->loc);
630}
631
632static /*@observer@*/ fileloc
633sRef_getStateInfoLoc (/*@exposed@*/ sRef s)
634{
635 llassert (sRef_isValid (s) && s->definfo != NULL
636 && (fileloc_isDefined (s->definfo->loc)));
637 return (s->definfo->loc);
638}
639
640static /*@observer@*/ fileloc
641sRef_getExpInfoLoc (/*@exposed@*/ sRef s)
642{
643 llassert (sRef_isValid (s) && s->expinfo != NULL
644 && (fileloc_isDefined (s->expinfo->loc)));
645 return (s->expinfo->loc);
646}
647
648static /*@observer@*/ fileloc
649sRef_getNullInfoLoc (/*@exposed@*/ sRef s)
650{
651 llassert (sRef_isValid (s) && s->nullinfo != NULL
652 && (fileloc_isDefined (s->nullinfo->loc)));
653 return (s->nullinfo->loc);
654}
655
656/*@observer@*/ sRef
28bf4b0b 657 sRef_getAliasInfoRef (/*@temp@*/ sRef s)
616915dd 658{
659 llassert (sRef_isValid (s) && s->aliasinfo != NULL);
660 return (s->aliasinfo->ref);
661}
662
28bf4b0b 663bool sRef_inGlobalScope ()
616915dd 664{
28bf4b0b 665 return !inFunction;
616915dd 666}
667
668/*
669** This function should be called before new sRefs are created
670** somewhere where they will have a lifetime greater than the
671** current function scope.
672*/
673
674void sRef_setGlobalScope ()
675{
676 llassert (inFunction);
28bf4b0b 677 DPRINTF (("leave function"));
616915dd 678 inFunction = FALSE;
679}
680
681void sRef_clearGlobalScope ()
682{
683 llassert (!inFunction);
28bf4b0b 684 DPRINTF (("enter function"));
616915dd 685 inFunction = TRUE;
686}
687
688static bool oldInFunction = FALSE;
689
690void sRef_setGlobalScopeSafe ()
691{
28bf4b0b 692 oldInFunction = inFunction;
693 DPRINTF (("leave function"));
616915dd 694 inFunction = FALSE;
695}
696
697void sRef_clearGlobalScopeSafe ()
698{
699 inFunction = oldInFunction;
28bf4b0b 700 DPRINTF (("clear function: %s", bool_unparse (inFunction)));
616915dd 701}
702
703void sRef_enterFunctionScope ()
704{
705 llassert (!inFunction);
706 llassert (sRefTable_isEmpty (allRefs));
707 inFunction = TRUE;
28bf4b0b 708 DPRINTF (("enter function"));
616915dd 709}
710
711void sRef_exitFunctionScope ()
712{
713
714 if (inFunction)
715 {
28bf4b0b 716 DPRINTF (("Exit function scope."));
616915dd 717 sRefTable_clear (allRefs);
718 inFunction = FALSE;
719 }
720 else
721 {
722 llbuglit ("sRef_exitFunctionScope: not in function");
723 }
724}
725
726void sRef_destroyMod () /*@globals killed allRefs;@*/
727{
728# ifdef DEBUGREFS
729 llmsg (message ("Live: %d / %d ", nsrefs, totnsrefs));
730# endif
731
732 sRefTable_free (allRefs);
733}
734
616915dd 735
736static /*@notnull@*/ /*@exposed@*/ sRef
737sRef_fixConj (/*@notnull@*/ sRef s)
738{
739 if (sRef_isConj (s))
740 {
741 do {
742 s = sRef_getConjA (s);
743 } while (sRef_isConj (s));
744
745 llassert (sRef_isValid (s));
746 return s; /* don't need to ref */
747 }
748 else
749 {
750 return s;
751 }
752}
753
754static bool
755sRef_isExternallyVisibleAux (sRef s)
756{
757 bool res = FALSE;
758 sRef base = sRef_getRootBase (s);
759
760 if (sRef_isValid (base))
761 {
28bf4b0b 762 res = sRef_isParam (base) || sRef_isFileOrGlobalScope (base) || sRef_isExternal (base);
616915dd 763 }
764
765 return res;
766}
767
768bool
769 sRef_isExternallyVisible (sRef s)
770{
771 return (sRef_aliasCheckSimplePred (sRef_isExternallyVisibleAux, s));
772}
773
774/*@exposed@*/ uentry
775sRef_getBaseUentry (sRef s)
776{
777 sRef base = sRef_getRootBase (s);
778 uentry res = uentry_undefined;
779
780 if (sRef_isValid (base))
781 {
782 switch (base->kind)
783 {
784 case SK_PARAM:
785 res = usymtab_getRefQuiet (paramsScope, base->info->paramno);
786 break;
787
788 case SK_CVAR:
789 res = usymtab_getRefQuiet (base->info->cvar->lexlevel,
790 base->info->cvar->index);
791 break;
792
793 default:
794 break;
795 }
796 }
797
798 return res;
799}
800
801/*
802** lookup the current uentry corresponding to s, and return the corresponding sRef.
803** yuk yuk yuk yuk yuk yuk yuk yuk
804*/
805
806/*@exposed@*/ sRef
807sRef_updateSref (sRef s)
808{
809 sRef inner;
810 sRef ret;
811 sRef res;
812
813 if (!sRef_isValid (s)) return sRef_undefined;
616915dd 814
815 switch (s->kind)
816 {
817 case SK_UNKNOWN:
818 case SK_OBJECT:
819 case SK_NEW:
820 case SK_TYPE:
616915dd 821 case SK_DERIVED:
822 case SK_UNCONSTRAINED:
823 case SK_CONST:
824 case SK_SPECIAL:
825 case SK_RESULT:
826 return s;
28bf4b0b 827 case SK_EXTERNAL:
828 {
829 sRef r = sRef_updateSref (s->info->ref);
830
831 if (r != s->info->ref)
832 {
833 return sRef_makeExternal (r);
834 }
835 else
836 {
837 return s;
838 }
839 }
616915dd 840 case SK_PARAM:
841 case SK_CVAR:
842 {
843 uentry ue = sRef_getUentry (s);
844
845 /* must be raw name! (need the marker) */
846 ue = usymtab_lookupSafe (uentry_rawName (ue));
847
848 if (uentry_isUndefined (ue))
849 {
28bf4b0b 850 return s;
616915dd 851 }
852 else
853 {
28bf4b0b 854 DPRINTF (("Update sref: %s", uentry_unparseFull (ue)));
855 return (uentry_getSref (ue));
616915dd 856 }
857 }
858 case SK_ARRAYFETCH:
859 /* special case if ind known */
860 inner = s->info->arrayfetch->arr;
861 ret = sRef_updateSref (inner);
862
863 if (ret == inner)
864 {
865 res = s;
866 }
867 else
868 {
869 res = sRef_makeArrayFetch (ret);
870 }
871
872 return res;
873
874 case SK_FIELD:
875 inner = s->info->field->rec;
876 ret = sRef_updateSref (inner);
877
878 if (ret == inner)
879 {
880 res = s;
881 }
882 else
883 {
884 res = (sRef_makeField (ret, s->info->field->field));
885 }
886
887 return (res);
888 case SK_PTR:
889 inner = s->info->ref;
890 ret = sRef_updateSref (inner);
891 if (ret == inner)
892 {
893 res = s;
894 }
895 else
896 {
897 res = sRef_makePointer (ret);
898 }
899
900 return (res);
901
902 case SK_ADR:
903 inner = s->info->ref;
904 ret = sRef_updateSref (inner);
905
906 if (ret == inner)
907 {
908 res = s;
909 }
910 else
911 {
912 res = sRef_makeAddress (ret);
913 }
914
915 return (res);
916
917 case SK_CONJ:
918 {
919 sRef innera = s->info->conj->a;
920 sRef innerb = s->info->conj->b;
921 sRef reta = sRef_updateSref (innera);
922 sRef retb = sRef_updateSref (innerb);
923
924 if (innera == reta && innerb == retb)
925 {
926 res = s;
927 }
928 else
929 {
930 res = sRef_makeConj (reta, retb);
931 }
932
933 return (res);
934 }
935 }
936
937 BADEXIT;
938}
939
940uentry
941sRef_getUentry (sRef s)
942{
943 llassert (sRef_isValid (s));
944
945 switch (s->kind)
946 {
947 case SK_PARAM:
948 return (usymtab_getRefQuiet (paramsScope, s->info->paramno));
949 case SK_CVAR:
950 return (usymtab_getRefQuiet (s->info->cvar->lexlevel, s->info->cvar->index));
951 case SK_CONJ:
952 {
953 if (sRef_isCvar (s->info->conj->a) || sRef_isParam (s->info->conj->a)
954 || sRef_isConj (s->info->conj->a))
955 {
956 return sRef_getUentry (s->info->conj->a);
957 }
958 else
959 {
960 return sRef_getUentry (s->info->conj->b);
961 }
962 }
963 case SK_UNKNOWN:
964 case SK_SPECIAL:
965 return uentry_undefined;
966 BADDEFAULT;
967 }
968}
969
970int
971sRef_getParam (sRef s)
972{
973 llassert (sRef_isValid (s));
974 llassert (s->kind == SK_PARAM);
975
976 return s->info->paramno;
977}
978
979bool
980sRef_isModified (sRef s)
981{
982 return (!sRef_isValid (s) || s->modified);
983}
984
985void sRef_setModified (sRef s)
986{
987 if (sRef_isValid (s))
988 {
989 s->modified = TRUE;
990
991
992 if (sRef_isRefsField (s))
993 {
994 sRef base = sRef_getBase (s);
28bf4b0b 995
616915dd 996
997 llassert (s->kind == SK_FIELD);
28bf4b0b 998
616915dd 999
1000 if (sRef_isPointer (base))
1001 {
1002 base = sRef_getBase (base);
28bf4b0b 1003 }
1004
616915dd 1005 if (sRef_isRefCounted (base))
1006 {
1007 base->aliaskind = AK_NEWREF;
28bf4b0b 1008 }
616915dd 1009 }
28bf4b0b 1010 }
616915dd 1011}
1012
1013/*
1014** note: this side-effects sRefSet to set modified to TRUE
1015** for any sRef similar to s.
1016*/
1017
1018bool
1019sRef_canModifyVal (sRef s, sRefSet sl)
1020{
1021 if (context_getFlag (FLG_MUSTMOD))
1022 {
1023 return (sRef_doModifyVal (s, sl));
1024 }
1025 else
1026 {
1027 return (sRef_checkModifyVal (s, sl));
1028 }
1029}
1030
1031bool
1032sRef_canModify (sRef s, sRefSet sl)
1033{
1034
1035 if (context_getFlag (FLG_MUSTMOD))
1036 {
1037 return (sRef_doModify (s, sl));
1038 }
1039 else
1040 {
1041 return (sRef_checkModify (s, sl));
1042 }
1043}
1044
1045/*
1046** No side-effects
1047*/
1048
1049static
1050bool sRef_checkModifyVal (sRef s, sRefSet sl)
1051{
28bf4b0b 1052 DPRINTF (("Check modify val: %s", sRef_unparse (s)));
1053
616915dd 1054 if (sRef_isInvalid (s))
1055 {
1056 return TRUE;
1057 }
1058
1059 switch (s->kind)
1060 {
1061 case SK_UNCONSTRAINED:
1062 case SK_CONST:
1063 return TRUE;
1064 case SK_CVAR:
28bf4b0b 1065 DPRINTF (("Modify var: %s", sRef_unparse (s)));
1066
1067 if (sRef_isFileOrGlobalScope (s))
616915dd 1068 {
1069 if (context_checkGlobMod (s))
1070 {
1071 return (sRefSet_member (sl, s));
1072 }
1073
1074 return TRUE;
1075 }
1076 else
1077 {
1078 return TRUE;
1079 }
1080 case SK_PARAM:
1081 return (sRefSet_member (sl, s)
1082 || alkind_isOnly (sRef_getOrigAliasKind (s)));
1083 case SK_ARRAYFETCH:
1084 /* special case if ind known */
1085 return (sRefSet_member (sl, s) ||
1086 sRef_checkModifyVal (s->info->arrayfetch->arr, sl));
1087 case SK_FIELD:
1088 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->field->rec, sl));
1089 case SK_PTR:
1090 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->ref, sl));
1091 case SK_ADR:
1092 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->ref, sl));
1093 case SK_CONJ:
1094 return ((sRef_checkModifyVal (s->info->conj->a, sl)) &&
1095 (sRef_checkModifyVal (s->info->conj->b, sl)));
1096 case SK_UNKNOWN:
1097 case SK_OBJECT:
1098 case SK_NEW:
1099 case SK_TYPE:
1100 case SK_DERIVED:
1101 return TRUE;
1102 case SK_EXTERNAL:
1103 return TRUE;
1104 case SK_SPECIAL:
1105 {
1106 switch (s->info->spec)
1107 {
1108 case SR_NOTHING: return TRUE;
1109 case SR_INTERNAL:
1110 if (context_getFlag (FLG_INTERNALGLOBS))
1111 {
1112 return (sRefSet_member (sl, s));
1113 }
1114 else
1115 {
1116 return TRUE;
1117 }
1118 case SR_SPECSTATE: return TRUE;
1119 case SR_SYSTEM: return (sRefSet_member (sl, s));
28bf4b0b 1120 case SR_GLOBALMARKER: BADBRANCH;
616915dd 1121 }
1122 }
1123 case SK_RESULT: BADBRANCH;
1124 }
1125 BADEXIT;
1126}
1127
1128/*
1129** this should probably be elsewhere...
1130**
1131** returns TRUE iff sl indicates that s can be modified
1132*/
1133
1134static bool sRef_checkModify (sRef s, sRefSet sl)
1135{
1136 llassert (sRef_isValid (s));
1137
1138 switch (s->kind)
1139 {
1140 case SK_UNCONSTRAINED:
1141 case SK_CONST:
1142 return TRUE;
1143 case SK_CVAR:
28bf4b0b 1144 if (sRef_isFileOrGlobalScope (s))
616915dd 1145 {
1146 if (context_checkGlobMod (s))
1147 {
1148 return (sRefSet_member (sl, s));
1149 }
1150
1151 return TRUE;
1152 }
1153 else
1154 {
1155 return TRUE;
1156 }
1157 case SK_PARAM:
1158 return TRUE;
1159 case SK_ARRAYFETCH:
1160 return (sRefSet_member (sl, s) ||
1161 sRef_checkModifyVal (s->info->arrayfetch->arr, sl));
1162 case SK_FIELD:
1163 {
1164 sRef sr = s->info->field->rec;
1165
1166 if (sr->kind == SK_PARAM)
1167 return TRUE; /* structs are copied on call */
1168
1169 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->field->rec, sl));
1170 }
1171 case SK_PTR:
1172 {
1173 bool sm;
1174
1175 sm = sRefSet_member (sl, s);
1176
1177 if (sm)
1178 return TRUE;
1179 else
1180 return (sRef_checkModifyVal (s->info->ref, sl));
1181 }
1182 case SK_ADR:
1183 return (sRefSet_member (sl, s) || sRef_checkModifyVal (s->info->ref, sl));
1184 case SK_CONJ:
1185 return ((sRef_checkModify (s->info->conj->a, sl)) &&
1186 (sRef_checkModify (s->info->conj->b, sl)));
1187 case SK_NEW:
1188 case SK_OBJECT:
1189 case SK_UNKNOWN:
1190 case SK_TYPE:
1191 case SK_DERIVED:
1192 case SK_EXTERNAL:
1193 return TRUE;
1194 case SK_SPECIAL:
1195 {
1196 switch (s->info->spec)
1197 {
1198 case SR_NOTHING: return TRUE;
1199 case SR_INTERNAL:
1200 if (context_getFlag (FLG_INTERNALGLOBS))
1201 {
1202 return (sRefSet_member (sl, s));
1203 }
1204 else
1205 {
1206 return TRUE;
1207 }
1208 case SR_SPECSTATE: return TRUE;
1209 case SR_SYSTEM: return (sRefSet_member (sl, s));
28bf4b0b 1210 case SR_GLOBALMARKER: BADBRANCH;
616915dd 1211 }
1212 }
1213 case SK_RESULT: BADBRANCH;
1214 }
1215 BADEXIT;
1216}
1217
1218cstring sRef_stateVerb (sRef s)
1219{
1220 if (sRef_isDead (s))
1221 {
1222 return cstring_makeLiteralTemp ("released");
1223 }
1224 else if (sRef_isKept (s))
1225 {
1226 return cstring_makeLiteralTemp ("kept");
1227 }
1228 else if (sRef_isDependent (s))
1229 {
1230 return cstring_makeLiteralTemp ("dependent");
1231 }
1232 else
1233 {
1234 BADEXIT;
1235 }
1236}
1237
1238cstring sRef_stateAltVerb (sRef s)
1239{
1240 if (sRef_isDead (s))
1241 {
1242 return cstring_makeLiteralTemp ("live");
1243 }
1244 else if (sRef_isKept (s))
1245 {
1246 return cstring_makeLiteralTemp ("not kept");
1247 }
1248 else if (sRef_isDependent (s))
1249 {
1250 return cstring_makeLiteralTemp ("independent");
1251 }
1252 else
1253 {
1254 BADEXIT;
1255 }
1256}
1257
1258static
1259bool sRef_doModifyVal (sRef s, sRefSet sl)
1260{
1261 llassert (sRef_isValid (s));
1262
1263
1264 switch (s->kind)
1265 {
1266 case SK_UNCONSTRAINED:
1267 case SK_CONST:
1268 return TRUE;
1269 case SK_CVAR:
28bf4b0b 1270 if (sRef_isFileOrGlobalScope (s))
616915dd 1271 {
1272
1273 if (context_checkGlobMod (s))
1274 {
1275 return (sRefSet_modifyMember (sl, s));
1276 }
1277 else
1278 {
1279 (void) sRefSet_modifyMember (sl, s);
1280 }
1281
1282 return TRUE;
1283 }
1284 else
1285 {
1286 return TRUE;
1287 }
1288 case SK_PARAM:
1289 return (sRefSet_modifyMember (sl, s)
1290 || alkind_isOnly (sRef_getOrigAliasKind (s)));
1291 case SK_ARRAYFETCH:
1292 /* special case if ind known */
1293 /* unconditional OR, need side effect */
1294 return (OR (sRefSet_modifyMember (sl, s),
1295 sRef_doModifyVal (s->info->arrayfetch->arr, sl)));
1296 case SK_FIELD:
1297 return (OR (sRefSet_modifyMember (sl, s),
1298 sRef_doModifyVal (s->info->field->rec, sl)));
1299 case SK_PTR:
1300 return (OR (sRefSet_modifyMember (sl, s),
1301 sRef_doModifyVal (s->info->ref, sl)));
1302 case SK_ADR:
1303 return (OR (sRefSet_modifyMember (sl, s),
1304 sRef_doModifyVal (s->info->ref, sl)));
1305 case SK_CONJ:
1306 return (AND (sRef_doModifyVal (s->info->conj->a, sl) ,
1307 sRef_doModifyVal (s->info->conj->b, sl)));
1308 case SK_OBJECT:
1309 case SK_DERIVED:
1310 case SK_EXTERNAL:
1311 case SK_UNKNOWN:
1312 case SK_NEW:
1313 case SK_TYPE:
1314 return TRUE;
1315 case SK_SPECIAL:
1316 {
1317 switch (s->info->spec)
1318 {
1319 case SR_NOTHING: return TRUE;
1320 case SR_INTERNAL:
1321 if (context_getFlag (FLG_INTERNALGLOBS))
1322 {
1323 return (sRefSet_modifyMember (sl, s));
1324 }
1325 else
1326 {
1327 (void) sRefSet_modifyMember (sl, s);
1328 return TRUE;
1329 }
28bf4b0b 1330 case SR_SPECSTATE: return TRUE;
1331 case SR_SYSTEM: return (sRefSet_modifyMember (sl, s));
1332 case SR_GLOBALMARKER: BADBRANCH;
1333
616915dd 1334 }
1335 }
1336 case SK_RESULT: BADBRANCH;
1337 }
1338 BADEXIT;
1339}
1340
1341/*
1342** this should probably be elsewhere...
1343**
1344** returns TRUE iff sl indicates that s can be modified
1345*/
1346
1347static
1348bool sRef_doModify (sRef s, sRefSet sl)
1349{
28bf4b0b 1350 llassert (sRef_isValid (s));
616915dd 1351
1352 switch (s->kind)
1353 {
1354 case SK_UNCONSTRAINED:
1355 case SK_CONST:
1356 return TRUE;
1357 case SK_CVAR:
28bf4b0b 1358 if (sRef_isFileOrGlobalScope (s))
616915dd 1359 {
1360 if (context_checkGlobMod (s))
1361 {
1362 return (sRefSet_modifyMember (sl, s));
1363 }
1364 else
1365 {
1366 (void) sRefSet_modifyMember (sl, s);
1367 }
1368
1369 return TRUE;
1370 }
1371 else
1372 {
1373 return TRUE;
1374 }
1375 case SK_PARAM:
1376 return TRUE;
1377 case SK_ARRAYFETCH:
1378 return (OR (sRefSet_modifyMember (sl, s),
1379 sRef_doModifyVal (s->info->arrayfetch->arr, sl)));
1380 case SK_FIELD:
1381 {
1382 sRef sr = s->info->field->rec;
1383
1384 if (sr->kind == SK_PARAM)
1385 {
1386 return TRUE; /* structs are shallow-copied on call */
1387 }
1388
1389 return (OR (sRefSet_modifyMember (sl, s),
1390 sRef_doModifyVal (s->info->field->rec, sl)));
1391 }
1392 case SK_PTR:
1393 {
1394 return (OR (sRefSet_modifyMember (sl, s),
1395 sRef_doModifyVal (s->info->ref, sl)));
1396 }
1397 case SK_ADR:
1398 return (OR (sRefSet_modifyMember (sl, s),
1399 sRef_doModifyVal (s->info->ref, sl)));
1400 case SK_CONJ:
1401 return (AND (sRef_doModify (s->info->conj->a, sl),
1402 (sRef_doModify (s->info->conj->b, sl))));
1403 case SK_UNKNOWN:
1404 case SK_NEW:
1405 case SK_TYPE:
1406 return TRUE;
1407 case SK_OBJECT:
1408 case SK_DERIVED:
1409 case SK_EXTERNAL:
1410 return TRUE;
1411 case SK_SPECIAL:
1412 {
1413 switch (s->info->spec)
1414 {
1415 case SR_NOTHING: return TRUE;
1416 case SR_INTERNAL: return TRUE;
1417 case SR_SPECSTATE: return TRUE;
1418 case SR_SYSTEM: return (sRefSet_modifyMember (sl, s));
28bf4b0b 1419 case SR_GLOBALMARKER: BADBRANCH;
616915dd 1420 }
1421 }
1422 case SK_RESULT: BADBRANCH;
1423 }
1424 BADEXIT;
1425}
1426
1427static /*@exposed@*/ sRef
1428 sRef_leastCommon (/*@exposed@*/ sRef s1, sRef s2)
1429{
1430 llassert (sRef_similar (s1, s2));
1431
1432 if (!sRef_isValid (s1)) return s1;
1433 if (!sRef_isValid (s2)) return s1;
1434
1435 sRef_combineDefState (s1, s2);
1436 sRef_combineNullState (s1, s2);
1437 sRef_combineExKinds (s1, s2);
1438
1439 if (s1->aliaskind != s2->aliaskind)
1440 {
1441 if (s1->aliaskind == AK_UNKNOWN)
1442 {
1443 s1->aliaskind = s2->aliaskind;
1444 }
1445 else if (s2->aliaskind == AK_UNKNOWN)
1446 {
1447 ;
1448 }
1449 else
1450 {
1451 s1->aliaskind = AK_ERROR;
1452 }
1453 }
1454
1455 return s1;
1456}
1457
1458int sRef_compare (sRef s1, sRef s2)
1459{
1460 if (s1 == s2) return 0;
1461
1462 if (sRef_isInvalid (s1)) return -1;
1463 if (sRef_isInvalid (s2)) return 1;
1464
1465 INTCOMPARERETURN (s1->kind, s2->kind);
1466 INTCOMPARERETURN (s1->defstate, s2->defstate);
1467 INTCOMPARERETURN (s1->aliaskind, s2->aliaskind);
1468
28bf4b0b 1469 DPRINTF (("Compare null state: %s / %s",
1470 sRef_unparseFull (s1),
1471 sRef_unparseFull (s2)));
1472
1473 COMPARERETURN (nstate_compare (sRef_getNullState (s1),
1474 sRef_getNullState (s2)));
616915dd 1475
1476 switch (s1->kind)
1477 {
1478 case SK_PARAM:
1479 return (int_compare (s1->info->paramno, s2->info->paramno));
1480 case SK_ARRAYFETCH:
1481 {
1482 COMPARERETURN (sRef_compare (s1->info->arrayfetch->arr,
1483 s2->info->arrayfetch->arr));
1484
1485 if (s1->info->arrayfetch->indknown && s2->info->arrayfetch->indknown)
1486 {
1487 return (int_compare (s1->info->arrayfetch->ind,
1488 s2->info->arrayfetch->ind));
1489 }
1490 if (!s1->info->arrayfetch->indknown && !s2->info->arrayfetch->indknown)
1491 return 0;
1492
1493 return 1;
1494 }
1495 case SK_FIELD:
1496 {
1497 COMPARERETURN (sRef_compare (s1->info->field->rec, s2->info->field->rec));
1498
1499 if (cstring_equal (s1->info->field->field, s2->info->field->field))
1500 return 0;
1501
1502 return 1;
1503 }
1504 case SK_PTR:
1505 case SK_ADR:
1506 return (sRef_compare (s1->info->ref, s2->info->ref));
1507 case SK_CONJ:
1508 COMPARERETURN (sRef_compare (s1->info->conj->a, s2->info->conj->a));
1509 return (sRef_compare (s1->info->conj->b, s2->info->conj->b));
1510 case SK_UNCONSTRAINED:
1511 return (cstring_compare (s1->info->fname, s2->info->fname));
1512 case SK_NEW:
1513 case SK_CVAR:
1514 case SK_UNKNOWN:
1515 case SK_OBJECT:
1516 case SK_TYPE:
1517 case SK_DERIVED:
1518 case SK_EXTERNAL:
1519 case SK_CONST:
1520 case SK_RESULT:
1521 return 0;
1522 case SK_SPECIAL:
1523 return (generic_compare (s1->info->spec, s2->info->spec));
1524 }
1525 BADEXIT;
1526}
1527
1528static bool cref_equal (cref c1, cref c2)
1529{
1530 return ((c1->lexlevel == c2->lexlevel) &&
1531 (usymId_equal (c1->index, c2->index)));
1532}
1533
1534/*
1535** returns true if s1 could be the same storage as s2.
1536** i.e., a[?] ~ a[3]. Note its not symmetric ... s1
1537** should be more specific.
1538*/
1539
1540/*
1541** like similar, but matches objects <-> non-objects
1542*/
1543
1544static bool
1545sRef_uniqueReference (sRef s)
1546{
1547 return (sRef_isFresh (s) || sRef_isUnique (s)
1548 || sRef_isOnly (s) || sRef_isStack (s)
1549 || sRef_isAddress (s));
1550}
1551
1552static bool
1553sRef_similarRelaxedAux (sRef s1, sRef s2)
1554{
1555 if (s1 == s2)
1556 {
1557 if (sRef_isUnknownArrayFetch (s1))
1558 {
1559 return FALSE;
1560 }
1561 else
1562 {
1563 return TRUE;
1564 }
1565 }
1566
1567 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
1568
1569 if (sRef_isConj (s2))
1570 return (sRef_similarRelaxedAux (s1, sRef_getConjA (s2)) ||
1571 sRef_similarRelaxedAux (s1, sRef_getConjB (s2)));
1572
1573 switch (s1->kind)
1574 {
1575 case SK_CVAR:
1576 return ((s2->kind == SK_CVAR)
1577 && (cref_equal (s1->info->cvar, s2->info->cvar)));
1578 case SK_PARAM:
1579 return ((s2->kind == SK_PARAM)
1580 && (s1->info->paramno == s2->info->paramno));
1581 case SK_ARRAYFETCH:
1582 if (s2->kind == SK_ARRAYFETCH)
1583 {
1584 if (sRef_similarRelaxedAux (s1->info->arrayfetch->arr,
1585 s2->info->arrayfetch->arr))
1586 {
1587 if (s1->info->arrayfetch->indknown)
1588 {
1589 if (s2->info->arrayfetch->indknown)
1590 {
1591 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
1592 }
1593 else
1594 {
1595 return FALSE;
1596 }
1597 }
1598 else
1599 {
1600 return FALSE;
1601 }
1602 }
1603 }
1604 return FALSE;
1605 case SK_FIELD:
1606 return ((s2->kind == SK_FIELD
1607 && (sRef_similarRelaxedAux (s1->info->field->rec,
1608 s2->info->field->rec)
1609 && cstring_equal (s1->info->field->field,
1610 s2->info->field->field))));
1611 case SK_PTR:
1612 return ((s2->kind == SK_PTR)
1613 && sRef_similarRelaxedAux (s1->info->ref, s2->info->ref));
1614 case SK_ADR:
1615 return ((s2->kind == SK_ADR)
1616 && sRef_similarRelaxedAux (s1->info->ref, s2->info->ref));
1617 case SK_CONJ:
1618 return ((sRef_similarRelaxedAux (s1->info->conj->a, s2) ||
1619 (sRef_similarRelaxedAux (s1->info->conj->b, s2))));
1620 case SK_SPECIAL:
1621 return (s1->info->spec == s2->info->spec);
1622 case SK_UNCONSTRAINED:
1623 return (cstring_equal (s1->info->fname, s2->info->fname));
1624 case SK_DERIVED:
1625 case SK_CONST:
1626 case SK_TYPE:
1627 case SK_NEW:
1628 case SK_UNKNOWN:
1629 case SK_OBJECT:
1630 case SK_EXTERNAL:
1631 case SK_RESULT:
1632 return FALSE;
1633 }
1634 BADEXIT;
1635}
1636
1637bool
1638sRef_similarRelaxed (sRef s1, sRef s2)
1639{
1640 bool us1, us2;
1641
1642 if (s1 == s2)
1643 {
1644 if (sRef_isThroughArrayFetch (s1))
1645 {
1646 return FALSE;
1647 }
1648 else
1649 {
1650 return TRUE;
1651 }
1652 }
1653
1654 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
1655
1656 us1 = sRef_uniqueReference (s1);
1657 us2 = sRef_uniqueReference (s2);
1658
1659 if ((s1->kind == SK_EXTERNAL && (s2->kind != SK_EXTERNAL && !us2))
1660 || (s2->kind == SK_EXTERNAL && (s1->kind != SK_EXTERNAL && !us1)))
1661 {
1662 /*
1663 ** Previously, also:
1664 ** || (sRef_isExposed (s1) && !us2) || (sRef_isExposed (s2) && !us1)) ????
1665 **
1666 ** No clue why this was there?!
1667 */
1668
1669
1670 if (sRef_isExposed (s1) && sRef_isCvar (s1))
1671 {
1672 uentry ue1 = sRef_getUentry (s1);
1673
1674 if (uentry_isRefParam (ue1))
1675 {
1676 return sRef_similarRelaxedAux (s1, s2);
1677 }
1678 }
1679
1680 if (sRef_isExposed (s2) && sRef_isCvar (s2))
1681 {
1682 uentry ue2 = sRef_getUentry (s2);
1683
1684 if (uentry_isRefParam (ue2))
1685 {
1686 return sRef_similarRelaxedAux (s1, s2);
1687 }
1688 }
1689
1690 return (ctype_match (s1->type, s2->type));
1691 }
1692 else
1693 {
1694 return sRef_similarRelaxedAux (s1, s2);
1695 }
1696}
1697
1698bool
1699sRef_similar (sRef s1, sRef s2)
1700{
1701 if (s1 == s2) return TRUE;
1702 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
1703
1704 if (sRef_isConj (s2))
1705 {
1706 return (sRef_similar (s1, sRef_getConjA (s2)) ||
1707 sRef_similar (s1, sRef_getConjB (s2)));
1708 }
1709
1710 if (sRef_isDerived (s2))
1711 {
1712 return (sRef_includedBy (s1, s2->info->ref));
1713 }
1714
1715 switch (s1->kind)
1716 {
1717 case SK_CVAR:
1718 return ((s2->kind == SK_CVAR)
1719 && (cref_equal (s1->info->cvar, s2->info->cvar)));
1720 case SK_PARAM:
1721 return ((s2->kind == SK_PARAM)
1722 && (s1->info->paramno == s2->info->paramno));
1723 case SK_ARRAYFETCH:
1724 if (s2->kind == SK_ARRAYFETCH)
1725 {
1726 if (sRef_similar (s1->info->arrayfetch->arr,
1727 s2->info->arrayfetch->arr))
1728 {
1729 if (s1->info->arrayfetch->indknown)
1730 {
1731 if (s2->info->arrayfetch->indknown)
1732 {
1733 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
1734 }
1735 else
1736 {
1737 return TRUE;
1738 }
1739 }
1740 else
1741 {
1742 return TRUE;
1743 }
1744 }
1745 }
1746 else
1747 {
1748 if (s2->kind == SK_PTR)
1749 {
1750 if (sRef_similar (s1->info->arrayfetch->arr,
1751 s2->info->ref))
1752 {
1753 return TRUE;
1754 }
1755 }
1756 }
1757
1758 return FALSE;
1759 case SK_FIELD:
1760 return ((s2->kind == SK_FIELD
1761 && (sRef_similar (s1->info->field->rec,
1762 s2->info->field->rec)
1763 && cstring_equal (s1->info->field->field,
1764 s2->info->field->field))));
1765 case SK_PTR:
1766 if (s2->kind == SK_PTR)
1767 {
1768 return sRef_similar (s1->info->ref, s2->info->ref);
1769 }
1770 else
1771 {
1772 if (s2->kind == SK_ARRAYFETCH)
1773 {
1774 if (sRef_similar (s2->info->arrayfetch->arr,
1775 s1->info->ref))
1776 {
1777 return TRUE;
1778 }
1779 }
1780 }
1781
1782 return FALSE;
1783 case SK_ADR:
1784 return ((s2->kind == SK_ADR)
1785 && sRef_similar (s1->info->ref, s2->info->ref));
1786 case SK_CONJ:
1787 return ((sRef_similar (s1->info->conj->a, s2) ||
1788 (sRef_similar (s1->info->conj->b, s2))));
1789 case SK_DERIVED:
1790 return (sRef_includedBy (s2, s1->info->ref));
1791 case SK_UNCONSTRAINED:
1792 return (s2->kind == SK_UNCONSTRAINED
1793 && cstring_equal (s1->info->fname, s2->info->fname));
1794 case SK_CONST:
1795 case SK_TYPE:
1796 case SK_NEW:
1797 case SK_UNKNOWN:
1798 case SK_OBJECT:
1799 case SK_EXTERNAL:
1800 case SK_RESULT:
1801 return FALSE;
1802 case SK_SPECIAL:
1803 return (s2->kind == SK_SPECIAL
1804 && (s1->info->spec == s2->info->spec));
1805 }
1806
1807 /*@notreached@*/ DPRINTF (("Fell through for: %s / %s", sRef_unparse (s1), sRef_unparse (s2)));
1808 BADEXIT;
1809}
1810
1811/*
1812** return TRUE iff small can be derived from big.
1813**
1814** (e.g. x, x.a is includedBy x;
1815** x.a is included By x.a;
1816*/
1817
1818bool
1819sRef_includedBy (sRef small, sRef big)
1820{
1821 if (small == big) return TRUE;
1822 if (sRef_isInvalid (small) || sRef_isInvalid (big)) return FALSE;
1823
1824 if (sRef_isConj (big))
1825 return (sRef_similar (small, sRef_getConjA (big)) ||
1826 sRef_similar (small, sRef_getConjB (big)));
1827
1828 switch (small->kind)
1829 {
1830 case SK_CVAR:
1831 case SK_PARAM:
1832 return (sRef_same (small, big));
1833 case SK_ARRAYFETCH:
1834 if (big->kind == SK_ARRAYFETCH)
1835 {
1836 if (sRef_same (small->info->arrayfetch->arr, big->info->arrayfetch->arr))
1837 {
1838 if (small->info->arrayfetch->indknown)
1839 {
1840 if (big->info->arrayfetch->indknown)
1841 {
1842 return (small->info->arrayfetch->ind == big->info->arrayfetch->ind);
1843 }
1844 else
1845 {
1846 return TRUE;
1847 }
1848 }
1849 else
1850 {
1851 return TRUE;
1852 }
1853 }
1854 }
1855 return (sRef_includedBy (small->info->arrayfetch->arr, big));
1856 case SK_FIELD:
1857 if (big->kind == SK_FIELD)
1858 {
1859 return
1860 (sRef_same (small->info->field->rec, big->info->field->rec) &&
1861 cstring_equal (small->info->field->field, big->info->field->field));
1862 }
1863 else
1864 {
1865 return (sRef_includedBy (small->info->field->rec, big));
1866 }
1867
1868 case SK_PTR:
1869 if (big->kind == SK_PTR)
1870 {
1871 return sRef_same (small->info->ref, big->info->ref);
1872 }
1873 else
1874 {
1875 return (sRef_includedBy (small->info->ref, big));
1876 }
1877
1878 case SK_ADR:
1879 return ((big->kind == SK_ADR) && sRef_similar (small->info->ref, big->info->ref));
1880 case SK_CONJ:
1881 return ((sRef_includedBy (small->info->conj->a, big) ||
1882 (sRef_includedBy (small->info->conj->b, big))));
1883 case SK_DERIVED:
1884 return (sRef_includedBy (small->info->ref, big));
1885 case SK_UNCONSTRAINED:
1886 case SK_CONST:
1887 case SK_TYPE:
1888 case SK_NEW:
1889 case SK_UNKNOWN:
1890 case SK_OBJECT:
1891 case SK_EXTERNAL:
1892 case SK_RESULT:
1893 return FALSE;
1894 case SK_SPECIAL:
1895 switch (small->info->spec)
1896 {
1897 case SR_NOTHING: return TRUE;
1898 case SR_SPECSTATE:
1899 case SR_INTERNAL: return (sRef_isSpecInternalState (big) ||
1900 sRef_isFileStatic (big));
1901 case SR_SYSTEM: return (sRef_isSystemState (big));
28bf4b0b 1902 case SR_GLOBALMARKER: BADBRANCH;
616915dd 1903 }
1904 }
1905 BADEXIT;
1906}
1907
1908/*
1909** Same is similar to similar, but not quite the same.
1910** same and realSame aren't the same, but they are really similar.
1911** similarly, same is the same as same. but realSame is
1912** not really the same as same, or similar to similar.
1913**
1914** Similarly to similar, same checks if two sRefs are the same.
1915** The similarities end, however, when same compares arrays
1916** with unknown indexes. Similar returns false; same returns true.
1917**
1918** Similarly to similar and same, realSame is the same as same,
1919** except they do not behave the same when face with unknown
1920** sRefs. Same thinks they are not the same, but realSame thinks
1921** the are.
1922**
1923*/
1924
1925bool
1926sRef_realSame (sRef s1, sRef s2)
1927{
1928 if (s1 == s2) return TRUE;
1929 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
1930
1931 switch (s1->kind)
1932 {
1933 case SK_CVAR:
1934 return ((s2->kind == SK_CVAR) && (cref_equal (s1->info->cvar, s2->info->cvar)));
1935 case SK_PARAM:
1936 return ((s2->kind == SK_PARAM) && (s1->info->paramno == s2->info->paramno));
1937 case SK_ARRAYFETCH:
1938 if (s2->kind == SK_ARRAYFETCH)
1939 {
1940 if (sRef_realSame (s1->info->arrayfetch->arr, s2->info->arrayfetch->arr))
1941 {
1942 if (s1->info->arrayfetch->indknown && s2->info->arrayfetch->indknown)
1943 {
1944 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
1945 }
1946 if (!s1->info->arrayfetch->indknown && !s2->info->arrayfetch->indknown)
1947 {
1948 return TRUE;
1949 }
1950 return FALSE;
1951 }
1952 }
1953 return FALSE;
1954 case SK_FIELD:
1955 return ((s2->kind == SK_FIELD &&
1956 (sRef_realSame (s1->info->field->rec, s2->info->field->rec) &&
1957 cstring_equal (s1->info->field->field, s2->info->field->field))));
1958 case SK_PTR:
1959 return ((s2->kind == SK_PTR) && sRef_realSame (s1->info->ref, s2->info->ref));
1960 case SK_ADR:
1961 return ((s2->kind == SK_ADR) && sRef_realSame (s1->info->ref, s2->info->ref));
1962 case SK_CONJ:
1963 return ((sRef_realSame (s1->info->conj->a, s2) ||
1964 (sRef_realSame (s1->info->conj->b, s2))));
1965 case SK_OBJECT:
1966 return ((s2->kind == SK_OBJECT)
1967 && ctype_match (s1->info->object, s2->info->object));
1968 case SK_EXTERNAL:
1969 return ((s2->kind == SK_EXTERNAL)
1970 && sRef_realSame (s1->info->ref, s2->info->ref));
1971 case SK_SPECIAL:
1972 return ((s2->kind == SK_SPECIAL) && s1->info->spec == s2->info->spec);
1973 case SK_DERIVED:
1974 return ((s2->kind == SK_DERIVED) && sRef_realSame (s1->info->ref, s2->info->ref));
1975 case SK_UNCONSTRAINED:
1976 return ((s2->kind == SK_UNCONSTRAINED)
1977 && (cstring_equal (s1->info->fname, s2->info->fname)));
1978 case SK_TYPE:
1979 case SK_CONST:
1980 case SK_NEW:
1981 case SK_UNKNOWN:
1982 case SK_RESULT:
1983 return TRUE; /* changed this! was false */
1984 }
1985 BADEXIT;
1986}
1987
28bf4b0b 1988bool
1989sRef_sameObject (sRef s1, sRef s2)
1990{
1991 return (s1 == s2);
1992}
1993
616915dd 1994/*
1995** same is similar to similar, but not quite the same.
1996**
1997** Similarly to similar, same checks is two sRefs are the same.
1998** The similarities end, however, when same compares arrays
1999** with unknown indexes. Similar returns false; same returns true.
2000*/
2001
2002bool
2003sRef_same (sRef s1, sRef s2)
2004{
2005 if (s1 == s2) return TRUE;
2006 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
2007
2008 switch (s1->kind)
2009 {
2010 case SK_CVAR:
2011 return ((s2->kind == SK_CVAR) && (cref_equal (s1->info->cvar, s2->info->cvar)));
2012 case SK_PARAM:
2013 return ((s2->kind == SK_PARAM) && (s1->info->paramno == s2->info->paramno));
2014 case SK_ARRAYFETCH:
2015 if (s2->kind == SK_ARRAYFETCH)
2016 {
2017 llassert (s1->info->field->rec != s1);
2018 if (sRef_same (s1->info->arrayfetch->arr, s2->info->arrayfetch->arr))
2019 {
2020 if (s1->info->arrayfetch->indknown && s2->info->arrayfetch->indknown)
2021 {
2022 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
2023 }
2024 return TRUE;
2025 }
2026 }
2027 return FALSE;
2028 case SK_FIELD:
2029 {
2030 llassert (s1->info->field->rec != s1);
2031 return ((s2->kind == SK_FIELD &&
2032 (sRef_same (s1->info->field->rec, s2->info->field->rec) &&
2033 cstring_equal (s1->info->field->field, s2->info->field->field))));
2034 }
2035 case SK_PTR:
2036 {
2037 llassert (s1->info->ref != s1);
2038 return ((s2->kind == SK_PTR) && sRef_same (s1->info->ref, s2->info->ref));
2039 }
2040 case SK_ADR:
2041 {
2042 llassert (s1->info->ref != s1);
2043 return ((s2->kind == SK_ADR) && sRef_same (s1->info->ref, s2->info->ref));
2044 }
2045 case SK_CONJ:
2046 llassert (s1->info->conj->a != s1);
2047 llassert (s1->info->conj->b != s1);
2048 return ((sRef_same (s1->info->conj->a, s2)) && /* or or and? */
2049 (sRef_same (s1->info->conj->b, s2)));
2050 case SK_SPECIAL:
2051 return ((s2->kind == SK_SPECIAL) && s1->info->spec == s2->info->spec);
2052 case SK_DERIVED:
2053 llassert (s1->info->ref != s1);
2054 return ((s2->kind == SK_DERIVED) && sRef_same (s1->info->ref, s2->info->ref));
2055 case SK_CONST:
2056 case SK_UNCONSTRAINED:
2057 case SK_TYPE:
2058 case SK_UNKNOWN:
2059 case SK_NEW:
2060 case SK_OBJECT:
2061 case SK_EXTERNAL:
2062 case SK_RESULT:
2063 return FALSE;
2064 }
2065 BADEXIT;
2066}
2067
2068/*
2069** sort of similar, for use in def/use
2070*/
2071
2072static bool
2073sRef_closeEnough (sRef s1, sRef s2)
2074{
2075 if (s1 == s2) return TRUE;
2076 if (sRef_isInvalid (s1) || sRef_isInvalid (s2)) return FALSE;
2077
2078 switch (s1->kind)
2079 {
2080 case SK_CVAR:
2081 return (((s2->kind == SK_CVAR) &&
2082 (cref_equal (s1->info->cvar, s2->info->cvar))) ||
2083 (s2->kind == SK_UNCONSTRAINED && s1->info->cvar->lexlevel == 0));
2084 case SK_UNCONSTRAINED:
2085 return (s2->kind == SK_UNCONSTRAINED
2086 || ((s2->kind == SK_CVAR) && (s2->info->cvar->lexlevel == 0)));
2087 case SK_PARAM:
2088 return ((s2->kind == SK_PARAM)
2089 && (s1->info->paramno == s2->info->paramno));
2090 case SK_ARRAYFETCH:
2091 if (s2->kind == SK_ARRAYFETCH)
2092 {
2093 if (sRef_closeEnough (s1->info->arrayfetch->arr, s2->info->arrayfetch->arr))
2094 {
2095 if (s1->info->arrayfetch->indknown && s2->info->arrayfetch->indknown)
2096 {
2097 return (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind);
2098 }
2099 return TRUE;
2100 }
2101 }
2102 return FALSE;
2103 case SK_FIELD:
2104 return ((s2->kind == SK_FIELD &&
2105 (sRef_closeEnough (s1->info->field->rec, s2->info->field->rec) &&
2106 cstring_equal (s1->info->field->field, s2->info->field->field))));
2107 case SK_PTR:
2108 return ((s2->kind == SK_PTR) && sRef_closeEnough (s1->info->ref, s2->info->ref));
2109 case SK_ADR:
2110 return ((s2->kind == SK_ADR) && sRef_closeEnough (s1->info->ref, s2->info->ref));
2111 case SK_DERIVED:
2112 return ((s2->kind == SK_DERIVED) && sRef_closeEnough (s1->info->ref, s2->info->ref));
2113 case SK_CONJ:
2114 return ((sRef_closeEnough (s1->info->conj->a, s2)) ||
2115 (sRef_closeEnough (s1->info->conj->b, s2)));
2116 case SK_SPECIAL:
2117 return ((s2->kind == SK_SPECIAL) && s1->info->spec == s2->info->spec);
2118 case SK_TYPE:
2119 case SK_CONST:
2120 case SK_UNKNOWN:
2121 case SK_NEW:
2122 case SK_OBJECT:
2123 case SK_EXTERNAL:
2124 case SK_RESULT:
2125
2126 return FALSE;
2127 }
2128 BADEXIT;
2129}
2130
2131/*
2132 drl add 12/24/2000
2133 s is an sRef of a formal paramenter in a function call constraint
2134 we trys to return a constraint expression derived from the actual parementer of a function call.
2135*/
4ab867d6 2136/*@only@*/ constraintExpr sRef_fixConstraintParam (/*@observer@*/ sRef s, /*@observer@*/ /*@temp@*/ exprNodeList args)
616915dd 2137{
2138 constraintExpr ce;
2139
2140 if (sRef_isInvalid (s))
dc92450f 2141 llfatalbug((message("Invalid sRef")));
616915dd 2142
9280addf 2143 switch (s->kind)
616915dd 2144 {
9280addf 2145 case SK_RESULT:
2146 {
4ab867d6 2147 // s = sRef_saveCopy(s);
9280addf 2148 ce = constraintExpr_makeTermsRef (s);
2149 return ce;
2150 }
2151 case SK_FIELD:
2152 {
2153 sRef temp;
2154
2155 temp = (sRef_makeField (sRef_fixBaseParam (s->info->field->rec, args),
2156 s->info->field->field));
4ab867d6 2157 ce = constraintExpr_makeTermsRef (temp);
9280addf 2158 return ce;
dc92450f 2159 }
9280addf 2160 case SK_PTR:
2161 {
2162 sRef temp;
2163 temp = (sRef_makePointer (sRef_fixBaseParam (s->info->ref, args)));
4ab867d6 2164 //temp = sRef_saveCopy(temp);
9280addf 2165 ce = constraintExpr_makeTermsRef (temp);
2166 return ce;
2167 }
2168
2169 case SK_ARRAYFETCH:
2170 {
2171 sRef temp;
bb25bea6 2172 temp = sRef_saveCopy(s);
2173 temp = sRef_fixBaseParam (temp, args);
9280addf 2174 ce = constraintExpr_makeTermsRef (temp);
28bf4b0b 2175
2176 sRef_free(temp);
9280addf 2177 return ce;
2178 }
2179 case SK_CVAR:
bb25bea6 2180 {
2181 sRef temp;
2182 temp = sRef_saveCopy(s);
2183 ce = constraintExpr_makeTermsRef (temp);
28bf4b0b 2184 sRef_free(temp);
bb25bea6 2185 return ce;
2186 }
9280addf 2187 case SK_PARAM:
dc92450f 2188 llassert(exprNodeList_size (args) > s->info->paramno);
9280addf 2189 {
2190 exprNode e = exprNodeList_nth (args, s->info->paramno);
dc92450f 2191
2192 llassert( !(exprNode_isError (e)) );
9280addf 2193 ce = constraintExpr_makeExprNode (e);
dc92450f 2194 return ce;
9280addf 2195 }
dc92450f 2196
9280addf 2197 default:
bb25bea6 2198 {
2199 sRef temp;
2200 llcontbug ((message("Trying to do fixConstraintParam on nonparam, nonglobal: %q for function with arguments %q", sRef_unparse (s), exprNodeList_unparse(args) ) ));
2201 temp = sRef_saveCopy(s);
2202 ce = constraintExpr_makeTermsRef (temp);
28bf4b0b 2203
2204 sRef_free(temp);
616915dd 2205 return ce;
bb25bea6 2206 }
616915dd 2207 }
9280addf 2208
616915dd 2209
9280addf 2210
616915dd 2211}
2212
2213/*@exposed@*/ sRef
2214sRef_fixBaseParam (/*@returned@*/ sRef s, exprNodeList args)
2215{
2216 if (sRef_isInvalid (s)) return (sRef_undefined);
2217
2218 switch (s->kind)
2219 {
2220 case SK_UNCONSTRAINED:
2221 case SK_CVAR:
2222 return s;
2223 case SK_PARAM:
2224 {
2225 if (exprNodeList_size (args) > s->info->paramno)
2226 {
2227 exprNode e = exprNodeList_nth (args, s->info->paramno);
2228
2229 if (exprNode_isError (e))
2230 {
2231 return sRef_makeUnknown ();
2232 }
2233
2234 return (exprNode_getSref (e));
2235 }
2236 else
2237 {
2238 return sRef_makeUnknown ();
2239 }
2240 }
2241 case SK_ARRAYFETCH:
2242
2243 if (s->info->arrayfetch->indknown)
2244 {
2245 return (sRef_makeArrayFetchKnown
2246 (sRef_fixBaseParam (s->info->arrayfetch->arr, args),
2247 s->info->arrayfetch->ind));
2248 }
2249 else
2250 {
2251 return (sRef_makeArrayFetch
2252 (sRef_fixBaseParam (s->info->arrayfetch->arr, args)));
2253 }
2254 case SK_FIELD:
2255 return (sRef_makeField (sRef_fixBaseParam (s->info->field->rec, args),
2256 s->info->field->field));
2257
2258 case SK_PTR:
2259 return (sRef_makePointer (sRef_fixBaseParam (s->info->ref, args)));
2260
2261 case SK_ADR:
2262 return (sRef_makeAddress (sRef_fixBaseParam (s->info->ref, args)));
2263
2264 case SK_CONJ:
2265 return (sRef_makeConj (sRef_fixBaseParam (s->info->conj->a, args),
2266 sRef_fixBaseParam (s->info->conj->b, args)));
2267 case SK_DERIVED:
2268 case SK_SPECIAL:
2269 case SK_TYPE:
2270 case SK_CONST:
2271 case SK_NEW:
2272 case SK_UNKNOWN:
2273 case SK_OBJECT:
2274 case SK_EXTERNAL:
2275 case SK_RESULT:
2276 return s;
2277 }
2278 BADEXIT;
2279}
2280
2281/*@exposed@*/ sRef
2282sRef_undumpGlobal (char **c)
2283{
2284 char p = **c;
2285
2286 (*c)++;
2287
2288 switch (p)
2289 {
2290 case 'g':
2291 {
28bf4b0b 2292 usymId uid = usymId_fromInt (reader_getInt (c));
616915dd 2293 sstate defstate;
2294 nstate nullstate;
2295 sRef ret;
2296
28bf4b0b 2297 reader_checkChar (c, '@');
2298 defstate = sstate_fromInt (reader_getInt (c));
616915dd 2299
28bf4b0b 2300 reader_checkChar (c, '@');
2301 nullstate = nstate_fromInt (reader_getInt (c));
616915dd 2302
2303 ret = sRef_makeGlobal (uid, ctype_unknown);
28bf4b0b 2304 sRef_setNullStateN (ret, nullstate);
616915dd 2305 ret->defstate = defstate;
2306 return ret;
2307 }
2308 case 's':
2309 {
28bf4b0b 2310 int i = reader_getInt (c);
616915dd 2311 speckind sk = speckind_fromInt (i);
2312
2313 switch (sk)
2314 {
2315 case SR_NOTHING: return (sRef_makeNothing ());
2316 case SR_INTERNAL: return (sRef_makeInternalState ());
2317 case SR_SPECSTATE: return (sRef_makeSpecState ());
2318 case SR_SYSTEM: return (sRef_makeSystemState ());
28bf4b0b 2319 case SR_GLOBALMARKER: BADBRANCH;
616915dd 2320 }
2321 BADEXIT;
2322 }
2323 case '-':
2324 return sRef_undefined;
2325 case 'u':
2326 return sRef_makeUnknown ();
2327 case 'x':
2328 return sRef_makeUnknown ();
2329 default:
2330 llfatalerror (message ("sRef_undumpGlobal: bad line: %s",
2331 cstring_fromChars (*c)));
2332 }
2333 BADEXIT;
2334}
2335
2336/*@exposed@*/ sRef
2337sRef_undump (char **c)
2338{
2339 char p = **c;
2340
2341 (*c)++;
2342
2343 switch (p)
2344 {
2345 case 'g':
28bf4b0b 2346 return (sRef_makeGlobal (usymId_fromInt (reader_getInt (c)), ctype_unknown));
616915dd 2347 case 'p':
28bf4b0b 2348 return (sRef_makeParam (reader_getInt (c), ctype_unknown));
616915dd 2349 case 'r':
2350 return (sRef_makeResultType (ctype_undump (c)));
2351 case 'a':
2352 {
2353 if ((**c >= '0' && **c <= '9') || **c == '-')
2354 {
28bf4b0b 2355 int i = reader_getInt (c);
616915dd 2356 sRef arr = sRef_undump (c);
2357 sRef ret = sRef_buildArrayFetchKnown (arr, i);
2358
2359 return ret;
2360 }
2361 else
2362 {
2363 sRef arr = sRef_undump (c);
2364 sRef ret = sRef_buildArrayFetch (arr);
2365
2366 return ret;
2367 }
2368 }
2369 case 'f':
2370 {
2371 cstring fname = cstring_undefined;
2372 sRef ret;
2373
2374 while (**c != '.')
2375 {
2376 fname = cstring_appendChar (fname, **c);
2377 (*c)++;
2378 }
2379 (*c)++;
2380
2381 ret = sRef_buildField (sRef_undump (c), fname);
2382 cstring_markOwned (fname);
2383 return (ret);
2384 }
2385 case 's':
2386 {
28bf4b0b 2387 int i = reader_getInt (c);
616915dd 2388 speckind sk = speckind_fromInt (i);
2389
2390 switch (sk)
2391 {
2392 case SR_NOTHING: return (sRef_makeNothing ());
2393 case SR_INTERNAL: return (sRef_makeInternalState ());
2394 case SR_SPECSTATE: return (sRef_makeSpecState ());
2395 case SR_SYSTEM: return (sRef_makeSystemState ());
28bf4b0b 2396 case SR_GLOBALMARKER: BADBRANCH;
616915dd 2397 }
2398 BADEXIT;
2399 }
2400 case 't':
2401 {
2402 sRef ptr = sRef_undump (c);
2403 sRef ret = sRef_makePointer (ptr);
2404
2405 return (ret);
2406 }
2407 case 'd':
2408 {
2409 sRef adr = sRef_undump (c);
2410 sRef ret = sRef_makeAddress (adr);
2411
2412 return (ret);
2413 }
2414 case 'o':
2415 {
2416 return (sRef_makeObject (ctype_undump (c)));
2417 }
2418 case 'c':
2419 {
2420 sRef s1 = sRef_undump (c);
2421 sRef s2 = ((*c)++, sRef_undump (c));
2422 sRef ret = sRef_makeConj (s1, s2);
2423
2424 return (ret);
2425 }
2426 case '-':
2427 return sRef_undefined;
2428 case 'u':
2429 return sRef_makeUnknown ();
2430 case 'x':
2431 return sRef_makeUnknown ();
2432 default:
2433 llfatalerror (message ("sRef_undump: bad line: %s", cstring_fromChars (*c)));
2434 }
2435 BADEXIT;
2436}
2437
2438/*@only@*/ cstring
2439sRef_dump (sRef s)
2440{
2441 if (sRef_isInvalid (s))
2442 {
2443 return (cstring_makeLiteral ("-"));
2444 }
2445 else
2446 {
2447 switch (s->kind)
2448 {
2449 case SK_PARAM:
2450 return (message ("p%d", s->info->paramno));
2451 case SK_ARRAYFETCH:
2452 if (s->info->arrayfetch->indknown)
2453 {
2454 return (message ("a%d%q", s->info->arrayfetch->ind,
2455 sRef_dump (s->info->arrayfetch->arr)));
2456 }
2457 else
2458 {
2459 return (message ("a%q", sRef_dump (s->info->arrayfetch->arr)));
2460 }
2461 case SK_FIELD:
2462 return (message ("f%s.%q", s->info->field->field,
2463 sRef_dump (s->info->field->rec)));
2464 case SK_PTR:
2465 return (message ("t%q", sRef_dump (s->info->ref)));
2466 case SK_ADR:
2467 return (message ("d%q", sRef_dump (s->info->ref)));
2468 case SK_OBJECT:
2469 return (message ("o%q", ctype_dump (s->info->object)));
2470 case SK_SPECIAL:
2471 return (message ("s%d", (int) s->info->spec));
2472 case SK_CONJ:
2473 return (message ("c%q.%q",
2474 sRef_dump (s->info->conj->a),
2475 sRef_dump (s->info->conj->b)));
2476 case SK_CVAR:
28bf4b0b 2477 if (sRef_isFileOrGlobalScope (s))
616915dd 2478 {
2479 return (message ("g%d",
2480 usymtab_convertId (s->info->cvar->index)));
2481 }
2482 else
2483 {
2484 llcontbug (message ("Dumping local variable: %q",
2485 sRef_unparseDebug (s)));
2486 return (cstring_makeLiteral ("u"));
2487 }
2488 case SK_UNKNOWN:
2489 return (cstring_makeLiteral ("u"));
2490 case SK_RESULT:
2491 return (message ("r%q", ctype_dump (s->type)));
2492 case SK_TYPE:
2493 case SK_CONST:
2494 case SK_EXTERNAL:
2495 case SK_DERIVED:
2496 case SK_NEW:
2497 case SK_UNCONSTRAINED:
2498 llcontbug (message ("sRef_dump: bad kind: %q",
2499 sRef_unparseFull (s)));
2500 return (cstring_makeLiteral ("x"));
2501 }
2502 }
2503
2504 BADEXIT;
2505}
2506
2507cstring sRef_dumpGlobal (sRef s)
2508{
2509 if (sRef_isInvalid (s))
2510 {
2511 return (cstring_makeLiteral ("-"));
2512 }
2513 else
2514 {
2515 switch (s->kind)
2516 {
2517 case SK_CVAR:
28bf4b0b 2518 if (sRef_isFileOrGlobalScope (s))
616915dd 2519 {
2520 return (message ("g%d@%d@%d",
2521 usymtab_convertId (s->info->cvar->index),
2522 (int) s->defstate,
28bf4b0b 2523 (int) sRef_getNullState (s)));
616915dd 2524 }
2525 else
2526 {
2527 llcontbug (message ("Dumping local variable: %q",
2528 sRef_unparseDebug (s)));
2529 return (cstring_makeLiteral ("u"));
2530 }
2531 case SK_UNKNOWN:
2532 return (cstring_makeLiteral ("u"));
2533 case SK_SPECIAL:
2534 return (message ("s%d", (int) s->info->spec));
2535 default:
2536 llcontbug (message ("sRef_dumpGlobal: bad kind: %q",
2537 sRef_unparseFull (s)));
2538 return (cstring_makeLiteral ("x"));
2539 }
2540 }
2541
2542 BADEXIT;
2543}
2544
2545ctype
2546sRef_deriveType (sRef s, uentryList cl)
2547{
2548 if (sRef_isInvalid (s)) return ctype_unknown;
2549
2550 switch (s->kind)
2551 {
2552 case SK_CVAR:
2553 return (uentry_getType (usymtab_getRefQuiet (s->info->cvar->lexlevel,
2554 s->info->cvar->index)));
2555 case SK_UNCONSTRAINED:
2556 return (ctype_unknown);
2557 case SK_PARAM:
28bf4b0b 2558 if (s->info->paramno >= 0)
2559 {
2560 return uentry_getType (uentryList_getN (cl, s->info->paramno));
2561 }
2562 else
2563 {
2564 return ctype_unknown;
2565 }
616915dd 2566 case SK_ARRAYFETCH:
2567 {
2568 ctype ca = sRef_deriveType (s->info->arrayfetch->arr, cl);
2569
2570 if (ctype_isArray (ca))
2571 {
2572 return (ctype_baseArrayPtr (ca));
2573 }
2574 else if (ctype_isUnknown (ca))
2575 {
2576 return (ca);
2577 }
2578 else
2579 {
2580 llcontbuglit ("sRef_deriveType: inconsistent array type");
2581 return ca;
2582 }
2583 }
2584 case SK_FIELD:
2585 {
2586 ctype ct = sRef_deriveType (s->info->field->rec, cl);
2587
2588 if (ctype_isStructorUnion (ct))
2589 {
2590 uentry ue = uentryList_lookupField (ctype_getFields (ct),
2591 s->info->field->field);
2592
2593 if (uentry_isValid (ue))
2594 {
2595 return (uentry_getType (ue));
2596 }
2597 else
2598 {
2599 llcontbuglit ("sRef_deriveType: bad field");
2600 return ctype_unknown;
2601 }
2602 }
2603 else if (ctype_isUnknown (ct))
2604 {
2605 return (ct);
2606 }
2607 else
2608 {
2609 llcontbuglit ("sRef_deriveType: inconsistent field type");
2610 return (ct);
2611 }
2612 }
2613 case SK_PTR:
2614 {
2615 ctype ct = sRef_deriveType (s->info->ref, cl);
2616
2617 if (ctype_isUnknown (ct)) return ct;
2618 if (ctype_isPointer (ct)) return (ctype_baseArrayPtr (ct));
2619 else
2620 {
2621 llcontbuglit ("sRef_deriveType: inconsistent pointer type");
2622 return (ct);
2623 }
2624 }
2625 case SK_ADR:
2626 {
2627 ctype ct = sRef_deriveType (s->info->ref, cl);
2628
2629 if (ctype_isUnknown (ct)) return ct;
2630 return ctype_makePointer (ct);
2631 }
2632 case SK_DERIVED:
2633 {
2634 return sRef_deriveType (s->info->ref, cl);
2635 }
2636 case SK_OBJECT:
2637 {
2638 return (s->info->object);
2639 }
2640 case SK_CONJ:
2641 {
2642 return (ctype_makeConj (sRef_deriveType (s->info->conj->a, cl),
2643 sRef_deriveType (s->info->conj->b, cl)));
2644 }
2645 case SK_RESULT:
2646 case SK_CONST:
2647 case SK_TYPE:
2648 {
2649 return (s->type);
2650 }
2651 case SK_SPECIAL:
2652 case SK_UNKNOWN:
2653 case SK_EXTERNAL:
2654 case SK_NEW:
2655 return ctype_unknown;
2656 }
2657 BADEXIT;
2658}
2659
2660ctype
2661sRef_getType (sRef s)
2662{
2663 if (sRef_isInvalid (s)) return ctype_unknown;
2664 return s->type;
2665}
2666
2667
2668/*@only@*/ cstring
2669sRef_unparseOpt (sRef s)
2670{
2671 sRef rb = sRef_getRootBase (s);
2672
2673 if (sRef_isMeaningful (rb) && !sRef_isConst (rb))
2674 {
2675 cstring ret = sRef_unparse (s);
2676
2677 llassertprint (!cstring_equalLit (ret, "?"), ("print: %s", sRef_unparseDebug (s)));
2678
2679 if (!cstring_isEmpty (ret))
2680 {
2681 return (cstring_appendChar (ret, ' '));
2682 }
2683 else
2684 {
2685 return ret;
2686 }
2687 }
2688
2689 return cstring_undefined;
2690}
2691
2692cstring
2693sRef_unparsePreOpt (sRef s)
2694{
2695 sRef rb = sRef_getRootBase (s);
2696
2697 if (sRef_isMeaningful (rb) && !sRef_isConst (rb))
2698 {
2699 cstring ret = sRef_unparse (s);
2700
2701 llassertprint (!cstring_equalLit (ret, "?"), ("print: %s", sRef_unparseDebug (s)));
2702 return (cstring_prependCharO (' ', ret));
2703 }
2704
2705 return cstring_undefined;
2706}
2707
2708/*@only@*/ cstring
2709sRef_unparse (sRef s)
2710{
2711 if (sRef_isInvalid (s)) return (cstring_makeLiteral ("?"));
2712
2713 if (context_inFunctionLike ())
2714 {
2715 return (sRef_unparseWithArgs (s, context_getParams ()));
2716 }
2717 else
2718 {
28bf4b0b 2719 DPRINTF (("Not in function like: %s", context_unparse ()));
616915dd 2720 return (sRef_unparseNoArgs (s));
2721 }
2722}
2723
2724static /*@only@*/ cstring
2725sRef_unparseWithArgs (sRef s, uentryList args)
2726{
2727 if (sRef_isInvalid (s))
2728 {
2729 return (cstring_makeLiteral ("?"));
2730 }
2731
2732 switch (s->kind)
2733 {
2734 case SK_CVAR:
2735 return (uentry_getName (usymtab_getRefQuiet (s->info->cvar->lexlevel,
2736 s->info->cvar->index)));
2737 case SK_UNCONSTRAINED:
2738 return (cstring_copy (s->info->fname));
2739 case SK_PARAM:
2740 {
28bf4b0b 2741 if (s->info->paramno < uentryList_size (args)
2742 && s->info->paramno >= 0)
616915dd 2743 {
2744 uentry ue = uentryList_getN (args, s->info->paramno);
2745
2746 if (uentry_isValid (ue))
2747 return uentry_getName (ue);
2748 }
2749
2750 return (message ("<bad param: %q / args %q",
2751 sRef_unparseDebug (s),
2752 uentryList_unparse (args)));
2753 }
2754 case SK_ARRAYFETCH:
2755 if (s->info->arrayfetch->indknown)
2756 {
2757 return (message ("%q[%d]", sRef_unparseWithArgs (s->info->arrayfetch->arr, args),
2758 s->info->arrayfetch->ind));
2759 }
2760 else
2761 {
2762 return (message ("%q[]", sRef_unparseWithArgs (s->info->arrayfetch->arr, args)));
2763 }
2764 case SK_FIELD:
2765 if (s->info->field->rec->kind == SK_PTR)
2766 {
2767 sRef ptr = s->info->field->rec;
2768
2769 return (message ("%q->%s", sRef_unparseWithArgs (ptr->info->ref, args),
2770 s->info->field->field));
2771 }
2772 return (message ("%q.%s", sRef_unparseWithArgs (s->info->field->rec, args),
2773 s->info->field->field));
2774
2775 case SK_PTR:
2776 {
2777 sRef ref = sRef_fixConj (s->info->ref);
2778 skind sk = ref->kind;
2779 cstring ret;
2780
2781 if (sk == SK_NEW)
2782 {
2783 ret = message ("storage pointed to by %q",
2784 sRef_unparseWithArgs (ref, args));
2785 }
2786 else if (skind_isSimple (sk) || sk == SK_PTR)
2787 {
2788 ret = message ("*%q", sRef_unparseWithArgs (ref, args));
2789 }
2790 else
2791 {
2792 ret = message ("*(%q)", sRef_unparseWithArgs (ref, args));
2793 }
2794
2795 return ret;
2796 }
2797 case SK_ADR:
2798 return (message ("&%q", sRef_unparseWithArgs (s->info->ref, args)));
2799 case SK_OBJECT:
2800 return (cstring_copy (ctype_unparse (s->info->object)));
2801 case SK_CONJ:
2802 return (sRef_unparseWithArgs (sRef_getConjA (s), args));
2803 case SK_NEW:
2804 if (cstring_isDefined (s->info->fname))
2805 {
2806 return (message ("[result of %s]", s->info->fname));
2807 }
2808 else
2809 {
2810 return (cstring_makeLiteral ("<new>"));
2811 }
2812 case SK_UNKNOWN:
2813 return (cstring_makeLiteral ("?"));
2814 case SK_DERIVED:
2815 return (message ("<derived %q>", sRef_unparse (s->info->ref)));
2816 case SK_EXTERNAL:
2817 return (message ("<external %q>", sRef_unparse (s->info->ref)));
2818 case SK_TYPE:
2819 return (message ("<type %s>", ctype_unparse (s->type)));
2820 case SK_CONST:
2821 return (message ("<const %s>", ctype_unparse (s->type)));
2822 case SK_SPECIAL:
28bf4b0b 2823 switch (s->info->spec)
2824 {
2825 case SR_NOTHING: return cstring_makeLiteral ("nothing");
2826 case SR_INTERNAL: return cstring_makeLiteral ("internal state");
2827 case SR_SPECSTATE: return cstring_makeLiteral ("spec state");
2828 case SR_SYSTEM: return cstring_makeLiteral ("file system state");
2829 case SR_GLOBALMARKER: return cstring_makeLiteral ("<global marker>");
2830 }
2831 BADBRANCH;
616915dd 2832 case SK_RESULT:
2833 return cstring_makeLiteral ("result");
2834 default:
2835 {
2836 llbug (message ("Bad sref, kind = %d", (int) s->kind));
2837 }
2838 }
2839
2840 BADEXIT;
2841}
2842
2843/*@only@*/ cstring
2844sRef_unparseDebug (sRef s)
2845{
28bf4b0b 2846 if (sRef_isInvalid (s))
2847 {
2848 return (cstring_makeLiteral ("<undef>"));
2849 }
2850
616915dd 2851
2852 switch (s->kind)
2853 {
2854 case SK_UNCONSTRAINED:
2855 return (message ("<unconstrained %s>", s->info->fname));
2856 case SK_CVAR:
2857 {
2858 uentry ce;
2859
2860 ce = usymtab_getRefQuiet (s->info->cvar->lexlevel, s->info->cvar->index);
2861
2862 if (uentry_isInvalid (ce))
2863 {
2864 return (message ("<scope: %d.%d *invalid*>",
2865 s->info->cvar->lexlevel,
2866 s->info->cvar->index));
2867 }
2868 else
2869 {
2870 return (message ("<scope: %d.%d *%q*>",
2871 s->info->cvar->lexlevel,
2872 s->info->cvar->index,
2873 uentry_getName (ce)));
2874 }
2875
2876 }
2877 case SK_PARAM:
2878 {
2879 return (message ("<parameter %d>", s->info->paramno + 1));
2880 }
2881 case SK_ARRAYFETCH:
2882 if (s->info->arrayfetch->indknown)
2883 {
2884 return (message ("%q[%d]", sRef_unparseDebug (s->info->arrayfetch->arr),
2885 s->info->arrayfetch->ind));
2886 }
2887 else
2888 {
2889 return (message ("%q[]", sRef_unparseDebug (s->info->arrayfetch->arr)));
2890 }
2891 case SK_FIELD:
2892 return (message ("%q.%s", sRef_unparseDebug (s->info->field->rec),
2893 s->info->field->field));
2894 case SK_PTR:
28bf4b0b 2895 if (sRef_isField (s->info->ref))
2896 {
2897 sRef fld = s->info->ref;
2898
2899 return (message ("%q->%s", sRef_unparseDebug (fld->info->field->rec),
2900 fld->info->field->field));
2901 }
2902 else
2903 {
2904 return (message ("*(%q)", sRef_unparseDebug (s->info->ref)));
2905 }
616915dd 2906 case SK_ADR:
2907 return (message ("&%q", sRef_unparseDebug (s->info->ref)));
2908 case SK_OBJECT:
2909 return (message ("<object type %s>", ctype_unparse (s->info->object)));
2910 case SK_CONJ:
2911 return (message ("%q | %q", sRef_unparseDebug (s->info->conj->a),
2912 sRef_unparseDebug (s->info->conj->b)));
2913 case SK_NEW:
2914 return message ("<new: %s>", s->info->fname);
2915 case SK_DERIVED:
2916 return (message ("<derived %q>", sRef_unparseDebug (s->info->ref)));
2917 case SK_EXTERNAL:
2918 return (message ("<external %q>", sRef_unparseDebug (s->info->ref)));
2919 case SK_TYPE:
2920 return (message ("<type %s>", ctype_unparse (s->type)));
2921 case SK_CONST:
2922 return (message ("<const %s>", ctype_unparse (s->type)));
2923 case SK_RESULT:
2924 return (message ("<result %s>", ctype_unparse (s->type)));
2925 case SK_SPECIAL:
2926 return (message ("<spec %s>",
2927 cstring_makeLiteralTemp
2928 (s->info->spec == SR_NOTHING ? "nothing"
2929 : s->info->spec == SR_INTERNAL ? "internalState"
2930 : s->info->spec == SR_SPECSTATE ? "spec state"
2931 : s->info->spec == SR_SYSTEM ? "fileSystem"
2932 : "error")));
2933 case SK_UNKNOWN:
2934 return cstring_makeLiteral ("<unknown>");
2935 }
2936
2937 BADEXIT;
2938}
2939
2940static /*@only@*/ cstring
2941sRef_unparseNoArgs (sRef s)
2942{
2943 if (sRef_isInvalid (s)) return (cstring_makeLiteral ("?"));
2944
2945 switch (s->kind)
2946 {
2947 case SK_UNCONSTRAINED:
2948 return (cstring_copy (s->info->fname));
2949 case SK_CVAR:
2950 {
2951 uentry ce = usymtab_getRefQuiet (s->info->cvar->lexlevel,
2952 s->info->cvar->index);
2953
2954 if (uentry_isInvalid (ce))
2955 {
28bf4b0b 2956 llcontbug (message ("sRef_unparseNoArgs: bad cvar: %q",
2957 sRef_unparseDebug (s)));
616915dd 2958 return (sRef_unparseDebug (s));
2959 }
2960 else
2961 {
2962 return (uentry_getName (ce));
2963 }
2964 }
2965 case SK_ARRAYFETCH:
2966 if (s->info->arrayfetch->indknown)
2967 {
2968 return (message ("%q[%d]", sRef_unparseNoArgs (s->info->arrayfetch->arr),
2969 s->info->arrayfetch->ind));
2970 }
2971 else
2972 {
2973 return (message ("%q[]", sRef_unparseNoArgs (s->info->arrayfetch->arr)));
2974 }
2975 case SK_FIELD:
2976 return (message ("%q.%s", sRef_unparseNoArgs (s->info->field->rec),
2977 s->info->field->field));
2978 case SK_PTR:
2979 {
2980 sRef ref = sRef_fixConj (s->info->ref);
2981 skind sk = ref->kind;
2982 cstring ret;
2983
2984 if (skind_isSimple (sk) || sk == SK_PTR)
2985 {
2986 ret = message ("*%q", sRef_unparseNoArgs (ref));
2987 }
2988 else
2989 {
2990 ret = message ("*(%q)", sRef_unparseNoArgs (ref));
2991 }
2992
2993 return (ret);
2994 }
2995 case SK_ADR:
2996 return (message ("&%q", sRef_unparseNoArgs (s->info->ref)));
2997 case SK_OBJECT:
2998 return (cstring_copy (ctype_unparse (s->info->object)));
2999 case SK_CONJ:
3000 return (sRef_unparseNoArgs (s->info->conj->a));
3001 case SK_NEW:
3002 return (message ("result of %s", s->info->fname));
3003 case SK_DERIVED:
3004 return (message ("<der %q>", sRef_unparseNoArgs (s->info->ref)));
3005 case SK_EXTERNAL:
3006 return message ("<ext %q>", sRef_unparseNoArgs (s->info->ref));
3007 case SK_SPECIAL:
3008 return (cstring_makeLiteral
3009 (s->info->spec == SR_NOTHING ? "nothing"
3010 : s->info->spec == SR_INTERNAL ? "internal state"
3011 : s->info->spec == SR_SPECSTATE ? "spec state"
3012 : s->info->spec == SR_SYSTEM ? "file system state"
3013 : "<spec error>"));
3014 case SK_RESULT:
3015 return cstring_makeLiteral ("result");
3016 case SK_CONST:
3017 case SK_TYPE:
3018 case SK_UNKNOWN:
3019 return cstring_makeLiteral ("?");
3020 case SK_PARAM:
3021 /* llcontbug (message ("sRef_unparseNoArgs: bad case: %q", sRef_unparseDebug (s))); */
3022 return (sRef_unparseDebug (s));
3023 }
3024 BADEXIT;
3025}
3026
3027/*@dependent@*/ sRef sRef_makeUnconstrained (cstring fname)
3028{
3029 sRef s = sRef_new ();
3030
3031 s->kind = SK_UNCONSTRAINED;
3032 s->info = (sinfo) dmalloc (sizeof (*s->info));
3033 s->info->fname = fname;
3034
3035 return (s);
3036}
3037
3038cstring sRef_unconstrainedName (sRef s)
3039{
3040 llassert (sRef_isUnconstrained (s));
3041
3042 return s->info->fname;
3043}
3044
3045bool sRef_isUnconstrained (sRef s)
3046{
3047 return (sRef_isValid(s) && s->kind == SK_UNCONSTRAINED);
3048}
3049
3050static /*@dependent@*/ /*@notnull@*/ sRef
3051 sRef_makeCvarAux (int level, usymId index, ctype ct)
3052{
28bf4b0b 3053 sRef s = sRef_newRef ();
3054
3055 s->kind = SK_CVAR;
616915dd 3056 s->info = (sinfo) dmalloc (sizeof (*s->info));
3057
3058 s->info->cvar = (cref) dmalloc (sizeof (*s->info->cvar));
3059 s->info->cvar->lexlevel = level;
3060 s->info->cvar->index = index;
3061
3062 /* for now, all globals are defined; all locals, aren't */
3063
3064 if (level <= fileScope)
3065 {
3066 s->defstate = SS_UNKNOWN;
3067 }
3068 else
3069 {
3070 ctype rct = ctype_realType (ct);
3071
3072 if (level != paramsScope
3073 && (ctype_isStructorUnion (rct) || ctype_isRealArray (rct)))
3074 {
3075 s->defstate = SS_ALLOCATED;
3076 s->oaliaskind = s->aliaskind = AK_STACK;
3077 }
3078 else
3079 {
3080 s->defstate = SS_UNDEFINED;
3081 s->oaliaskind = s->aliaskind = AK_LOCAL;
3082 }
3083 }
3084
3085 s->type = ct;
3086
3087 llassert (level >= globScope);
3088 llassert (usymId_isValid (index));
3089
28bf4b0b 3090 DPRINTF (("Made cvar: [%p] %s", s, sRef_unparseDebug (s)));
3091 llassert (valueTable_isUndefined (s->state));
3092 s->state = context_createValueTable (s);
616915dd 3093 return s;
3094}
3095
3096/*@dependent@*/ sRef sRef_makeCvar (int level, usymId index, ctype ct)
3097{
3098 return (sRef_makeCvarAux (level, index, ct));
3099}
3100
3101int sRef_lexLevel (sRef s)
3102{
3103 if (sRef_isValid (s))
3104 {
3105 sRef conj;
3106
3107 conj = sRef_fixConj (s);
3108 s = sRef_getRootBase (conj);
3109
3110 if (sRef_isValid (s) && s->kind == SK_CVAR)
3111 {
3112 return (s->info->cvar->lexlevel);
3113 }
3114 }
3115
3116 return globScope;
3117}
3118
3119sRef
3120sRef_makeGlobal (usymId l, ctype ct)
3121{
3122 return (sRef_makeCvar (globScope, l, ct));
3123}
3124
3125void
3126sRef_setParamNo (sRef s, int l)
3127{
3128 llassert (sRef_isValid (s) && s->kind == SK_PARAM);
3129 s->info->paramno = l;
28bf4b0b 3130 llassert (l >= -1);
616915dd 3131}
3132
3133/*@dependent@*/ sRef
3134sRef_makeParam (int l, ctype ct)
3135{
3136 sRef s = sRef_new ();
3137
3138 s->kind = SK_PARAM;
3139 s->type = ct;
3140
3141 s->info = (sinfo) dmalloc (sizeof (*s->info));
3142 s->info->paramno = l;
28bf4b0b 3143 llassert (l >= -1);
3144 s->defstate = SS_UNKNOWN;
3145 /* (probably defined, unless its an out parameter) */
616915dd 3146
28bf4b0b 3147 llassert (valueTable_isUndefined (s->state));
3148 s->state = context_createValueTable (s);
616915dd 3149 return s;
3150}
3151
3152bool
3153sRef_isIndexKnown (sRef arr)
3154{
3155 bool res;
3156
3157 llassert (sRef_isValid (arr));
3158 arr = sRef_fixConj (arr);
3159
3160 llassert (arr->kind == SK_ARRAYFETCH);
3161 res = arr->info->arrayfetch->indknown;
3162 return (res);
3163}
3164
3165int
3166sRef_getIndex (sRef arr)
3167{
3168 int result;
3169
3170 llassert (sRef_isValid (arr));
3171 arr = sRef_fixConj (arr);
3172
3173 llassert (arr->kind == SK_ARRAYFETCH);
3174
3175 if (!arr->info->arrayfetch->indknown)
3176 {
3177 llcontbug (message ("sRef_getIndex: unknown: %q", sRef_unparse (arr)));
3178 result = 0;
3179 }
3180 else
3181 {
3182 result = arr->info->arrayfetch->ind;
3183 }
3184
3185 return result;
3186}
3187
3188static bool sRef_isZerothArrayFetch (/*@notnull@*/ sRef s)
3189{
3190 return (s->kind == SK_ARRAYFETCH
3191 && s->info->arrayfetch->indknown
3192 && (s->info->arrayfetch->ind == 0));
3193}
3194
3195/*@exposed@*/ sRef sRef_makeAddress (/*@exposed@*/ sRef t)
3196{
3197
3198 if (sRef_isInvalid (t)) return sRef_undefined;
3199
3200 if (sRef_isPointer (t))
3201 {
3202 return (t->info->ref);
3203 }
3204 else if (sRef_isZerothArrayFetch (t))
3205 {
3206 return (t->info->arrayfetch->arr);
3207 }
3208 else
3209 {
28bf4b0b 3210 sRef s = sRef_newRef ();
616915dd 3211
3212 s->kind = SK_ADR;
3213 s->type = ctype_makePointer (t->type);
3214 s->info = (sinfo) dmalloc (sizeof (*s->info));
28bf4b0b 3215 s->info->ref = t; /* sRef_copy (t); */ /*@i32@*/
616915dd 3216
3217 if (t->defstate == SS_UNDEFINED)
3218 /* no! it is allocated even still: && !ctype_isPointer (t->type)) */
3219 {
3220 s->defstate = SS_ALLOCATED;
3221 }
3222 else
3223 {
3224 s->defstate = t->defstate;
3225 }
3226
3227 if (t->aliaskind == AK_LOCAL)
3228 {
3229 if (sRef_isLocalVar (t))
3230 {
3231 s->aliaskind = AK_STACK;
3232 }
3233 }
3234
28bf4b0b 3235 llassert (valueTable_isUndefined (s->state));
3236 s->state = context_createValueTable (s);
616915dd 3237 return s;
3238 }
3239}
3240
3241cstring sRef_getField (sRef s)
3242{
3243 cstring res;
3244
3245 llassert (sRef_isValid (s));
3246 s = sRef_fixConj (s);
3247
3248 llassertprint (sRef_isValid (s) && (s->kind == SK_FIELD),
3249 ("s = %s", sRef_unparseDebug (s)));
3250
3251 res = s->info->field->field;
3252 return (res);
3253}
3254
3255sRef sRef_getBase (sRef s)
3256{
3257 sRef res;
3258
3259 if (sRef_isInvalid (s)) return (sRef_undefined);
3260
3261 s = sRef_fixConj (s);
3262
3263 switch (s->kind)
3264 {
3265 case SK_ADR:
3266 case SK_PTR:
3267 case SK_DERIVED:
3268 case SK_EXTERNAL:
3269 res = s->info->ref;
3270 break;
3271 case SK_FIELD:
3272 res = s->info->field->rec;
3273 break;
3274
3275 case SK_ARRAYFETCH:
3276 res = s->info->arrayfetch->arr;
3277 break;
3278
3279 default:
3280 res = sRef_undefined; /* shouldn't need it */
3281 }
3282
3283 return (res);
3284}
3285
3286/*
3287** same as getBase, except returns invalid
3288** (and doesn't use adr's)
3289*/
3290
3291sRef
3292sRef_getBaseSafe (sRef s)
3293{
3294 sRef res;
3295
3296 if (sRef_isInvalid (s)) { return sRef_undefined; }
3297
3298 s = sRef_fixConj (s);
3299
3300 switch (s->kind)
3301 {
3302 case SK_PTR:
3303 res = s->info->ref;
3304 break;
3305 case SK_FIELD:
3306 res = s->info->field->rec; break;
3307 case SK_ARRAYFETCH:
3308 res = s->info->arrayfetch->arr;
3309 break;
3310 default:
3311 res = sRef_undefined; break;
3312 }
3313
3314 return res;
3315}
3316
3317/*@constant int MAXBASEDEPTH;@*/
3318# define MAXBASEDEPTH 25
3319
3320static /*@exposed@*/ sRef
3321sRef_getRootBaseAux (sRef s, int depth)
3322{
3323 if (sRef_isInvalid (s)) return sRef_undefined;
3324
3325 if (depth > MAXBASEDEPTH)
3326 {
3327 llgenmsg (message
3328 ("Warning: reference base limit exceeded for %q. "
3329 "This either means there is a variable with at least "
3330 "%d indirections from this reference, or "
3331 "there is a bug in LCLint.",
3332 sRef_unparse (s),
3333 MAXBASEDEPTH),
3334 g_currentloc);
3335
3336 return sRef_undefined;
3337 }
3338
3339 switch (s->kind)
3340 {
3341 case SK_ADR:
3342 case SK_PTR:
3343 return (sRef_getRootBaseAux (s->info->ref, depth + 1));
3344 case SK_FIELD:
3345 return (sRef_getRootBaseAux (s->info->field->rec, depth + 1));
3346 case SK_ARRAYFETCH:
3347 return (sRef_getRootBaseAux (s->info->arrayfetch->arr, depth + 1));
3348 case SK_CONJ:
3349 return (sRef_getRootBaseAux (sRef_fixConj (s), depth + 1));
3350 default:
3351 return s;
3352 }
3353}
3354
3355sRef sRef_getRootBase (sRef s)
3356{
3357 return (sRef_getRootBaseAux (s, 0));
3358}
3359
3360static bool sRef_isDeep (sRef s)
3361{
3362 if (sRef_isInvalid (s)) return FALSE;
3363
3364 switch (s->kind)
3365 {
3366 case SK_ADR:
3367 case SK_PTR:
3368 case SK_FIELD:
3369 case SK_ARRAYFETCH:
3370 return TRUE;
3371 case SK_CONJ:
3372 return (sRef_isDeep (sRef_fixConj (s)));
3373 default:
3374 return FALSE;
3375 }
3376}
3377
3378static int sRef_depth (sRef s)
3379{
3380 if (sRef_isInvalid (s)) return 0;
3381
3382 switch (s->kind)
3383 {
3384 case SK_ADR:
3385 case SK_PTR:
3386 case SK_DERIVED:
3387 case SK_EXTERNAL:
3388 return 1 + sRef_depth (s->info->ref);
3389 case SK_FIELD:
3390 return 1 + sRef_depth (s->info->field->rec);
3391 case SK_ARRAYFETCH:
3392 return 1 + sRef_depth (s->info->arrayfetch->arr);
3393 case SK_CONJ:
3394 return (sRef_depth (sRef_fixConj (s)));
3395 default:
3396 return 1;
3397 }
3398}
3399
3400sRef
3401sRef_makeObject (ctype o)
3402{
28bf4b0b 3403 sRef s = sRef_newRef (); /*@i423 same line is bad...@*/
616915dd 3404
3405 s->kind = SK_OBJECT;
3406 s->info = (sinfo) dmalloc (sizeof (*s->info));
3407 s->info->object = o;
28bf4b0b 3408 llassert (valueTable_isUndefined (s->state));
3409 s->state = context_createValueTable (s);
616915dd 3410 return s;
3411}
3412
28bf4b0b 3413/*
3414** This is used to represent storage referenced by a parameter.
3415*/
3416
3417sRef sRef_makeExternal (sRef t)
616915dd 3418{
28bf4b0b 3419 sRef s = sRef_newRef ();
616915dd 3420
3421 llassert (sRef_isValid (t));
3422
3423 s->kind = SK_EXTERNAL;
3424 s->info = (sinfo) dmalloc (sizeof (*s->info));
3425 s->type = t->type;
28bf4b0b 3426 s->info->ref = t; /* sRef_copy (t); */ /*@i32 was exposed@*/
3427 llassert (valueTable_isUndefined (s->state));
3428 s->state = context_createValueTable (s);
616915dd 3429 return s;
3430}
3431
28bf4b0b 3432/*@dependent@*/ sRef sRef_makeDerived (/*@exposed@*/ sRef t)
616915dd 3433{
3434 if (sRef_isValid (t))
3435 {
28bf4b0b 3436 sRef s = sRef_newRef ();
616915dd 3437
3438 s->kind = SK_DERIVED;
3439 s->info = (sinfo) dmalloc (sizeof (*s->info));
28bf4b0b 3440 s->info->ref = t; /* sRef_copy (t); */ /*@i32@*/
616915dd 3441
3442 s->type = t->type;
28bf4b0b 3443 llassert (valueTable_isUndefined (s->state));
3444 s->state = context_createValueTable (s);
616915dd 3445 return s;
3446 }
3447 else
3448 {
3449 return sRef_undefined;
3450 }
3451}
3452
3453/*
3454** definitely NOT symmetric:
3455**
3456** res fills in unknown state information from other
3457*/
3458
3459void
3460sRef_mergeStateQuiet (sRef res, sRef other)
3461{
3462 llassert (sRef_isValid (res));
3463 llassert (sRef_isValid (other));
3464
3465 res->modified = res->modified || other->modified;
3466 res->safe = res->safe && other->safe;
3467
3468 if (res->defstate == SS_UNKNOWN)
3469 {
3470 res->defstate = other->defstate;
28bf4b0b 3471 res->definfo = stateInfo_update (res->definfo, other->definfo);
616915dd 3472 }
3473
3474 if (res->aliaskind == AK_UNKNOWN ||
3475 (res->aliaskind == AK_LOCAL && alkind_isKnown (other->aliaskind)))
3476 {
3477 res->aliaskind = other->aliaskind;
3478 res->oaliaskind = other->oaliaskind;
28bf4b0b 3479 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
616915dd 3480 }
3481
3482 if (res->expkind == XO_UNKNOWN)
3483 {
3484 res->expkind = other->expkind;
3485 res->oexpkind = other->oexpkind;
28bf4b0b 3486 res->expinfo = stateInfo_update (res->expinfo, other->expinfo);
616915dd 3487 }
3488
3489 /* out takes precedence over implicitly defined */
3490 if (res->defstate == SS_DEFINED && other->defstate != SS_UNKNOWN)
3491 {
3492 res->defstate = other->defstate;
28bf4b0b 3493 res->definfo = stateInfo_update (res->definfo, other->definfo);
616915dd 3494 }
3495
28bf4b0b 3496 if (sRef_getNullState (other) == NS_ERROR || sRef_getNullState (res) == NS_ERROR)
616915dd 3497 {
28bf4b0b 3498 sRef_setNullState (res, NS_ERROR, fileloc_undefined);
616915dd 3499 }
3500 else
3501 {
28bf4b0b 3502 if (sRef_getNullState (other) != NS_UNKNOWN
3503 && (sRef_getNullState (res) == NS_UNKNOWN || sRef_getNullState (res) == NS_NOTNULL
3504 || sRef_getNullState (res) == NS_MNOTNULL))
616915dd 3505 {
28bf4b0b 3506 sRef_updateNullState (res, other);
616915dd 3507 }
3508 }
3509}
3510
3511/*
3512** definitely NOT symmetric:
3513**
3514** res fills in known state information from other
3515*/
3516
3517void
28bf4b0b 3518sRef_mergeStateQuietReverse (/*@dependent@*/ sRef res, /*@dependent@*/ sRef other)
616915dd 3519{
3520 bool changed = FALSE;
3521
3522 llassert (sRef_isValid (res));
3523 llassert (sRef_isValid (other));
28bf4b0b 3524 sRef_checkMutable (res);
616915dd 3525
3526 if (res->kind != other->kind)
3527 {
3528 changed = TRUE;
3529
3530 sinfo_free (res);
3531
3532 res->kind = other->kind;
3533 res->type = other->type;
3534 res->info = sinfo_fullCopy (other);
3535 }
3536 else
3537 {
3538 if (!ctype_equal (res->type, other->type))
3539 {
3540 changed = TRUE;
3541 res->type = other->type;
3542 }
3543
3544 sinfo_update (res, other);
3545 }
3546
3547 res->modified = res->modified || other->modified;
3548 res->safe = res->safe && other->safe;
3549
3550 if (res->aliaskind != other->aliaskind
3551 && (res->aliaskind == AK_UNKNOWN
3552 || ((res->aliaskind == AK_LOCAL
3553 || (res->aliaskind == AK_REFCOUNTED
3554 && other->aliaskind != AK_LOCAL))
3555 && other->aliaskind != AK_UNKNOWN)))
3556 {
3557 changed = TRUE;
3558 res->aliaskind = other->aliaskind;
3559 res->oaliaskind = other->oaliaskind;
28bf4b0b 3560 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
3561 }
616915dd 3562
3563 if (other->expkind != XO_UNKNOWN && other->expkind != res->expkind)
3564 {
3565 changed = TRUE;
3566 res->expkind = other->expkind;
28bf4b0b 3567 res->expinfo = stateInfo_update (res->expinfo, other->expinfo);
616915dd 3568 }
3569
3570 if (other->oexpkind != XO_UNKNOWN)
3571 {
3572 res->oexpkind = other->oexpkind;
3573 }
3574
3575 /* out takes precedence over implicitly defined */
3576
3577 if (res->defstate != other->defstate)
3578 {
3579 if (other->defstate != SS_UNKNOWN)
3580 {
3581 res->defstate = other->defstate;
3582 }
3583 }
3584
28bf4b0b 3585 if (sRef_getNullState (other) == NS_ERROR || sRef_getNullState (res) == NS_ERROR)
616915dd 3586 {
28bf4b0b 3587 if (sRef_getNullState (res) != NS_ERROR)
616915dd 3588 {
28bf4b0b 3589 sRef_setNullStateN (res, NS_ERROR);
616915dd 3590 changed = TRUE;
3591 }
3592 }
3593 else
3594 {
28bf4b0b 3595 if (sRef_getNullState (other) != NS_UNKNOWN && sRef_getNullState (other) != sRef_getNullState (res))
616915dd 3596 {
3597 changed = TRUE;
28bf4b0b 3598 sRef_updateNullState (res, other);
616915dd 3599 }
3600 }
3601
3602 if (changed)
3603 {
3604 sRef_clearDerived (res);
3605 }
3606}
3607
3608void
3609sRef_mergeState (sRef res, sRef other, clause cl, fileloc loc)
3610{
3611 if (sRef_isValid (res) && sRef_isValid (other))
3612 {
3613 sRef_mergeStateAux (res, other, cl, FALSE, loc, TRUE);
3614 }
3615 else
3616 {
3617 if (sRef_isInvalid (res))
3618 {
3619 llbug (message ("sRef_mergeState: invalid res sRef: %q",
3620 sRef_unparseDebug (other)));
3621 }
3622 else
3623 {
3624 llbug (message ("sRef_mergeState: invalid other sRef: %q",
3625 sRef_unparseDebug (res)));
3626 }
3627 }
3628}
3629
3630void
3631sRef_mergeOptState (sRef res, sRef other, clause cl, fileloc loc)
3632{
3633 if (sRef_isValid (res) && sRef_isValid (other))
3634 {
3635 sRef_mergeStateAux (res, other, cl, TRUE, loc, TRUE);
3636 }
3637 else
3638 {
3639 if (sRef_isInvalid (res))
3640 {
3641 llbug (message ("sRef_mergeOptState: invalid res sRef: %q",
3642 sRef_unparseDebug (other)));
3643 }
3644 else
3645 {
3646 llbug (message ("sRef_mergeOptState: invalid other sRef: %q",
3647 sRef_unparseDebug (res)));
3648 }
3649 }
3650}
3651
3652static void
3653sRef_mergeStateAux (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other,
3654 clause cl, bool opt, fileloc loc,
3655 bool doDerivs)
3656 /*@modifies res@*/
3657{
3658 llassertfatal (sRef_isValid (res));
3659 llassertfatal (sRef_isValid (other));
3660
28bf4b0b 3661 sRef_checkMutable (res);
3662 sRef_checkMutable (other);
3663
616915dd 3664 res->modified = res->modified || other->modified;
3665
3666 if (res->kind == other->kind
3667 || (other->kind == SK_UNKNOWN || res->kind == SK_UNKNOWN))
3668 {
3669 sstate odef = other->defstate;
3670 sstate rdef = res->defstate;
28bf4b0b 3671 nstate onull = sRef_getNullState (other);
616915dd 3672
3673 /*
3674 ** yucky stuff to handle
3675 **
3676 ** if (s) free (s);
3677 */
3678
3679 if (other->defstate == SS_DEAD
3680 && ((sRef_isOnly (res) && sRef_definitelyNull (res))
3681 || (res->defstate == SS_UNDEFINED
3682 || res->defstate == SS_UNUSEABLE)))
3683 {
3684 if (res->defstate == SS_UNDEFINED
3685 || res->defstate == SS_UNUSEABLE)
3686 {
3687 res->defstate = SS_UNUSEABLE;
3688 }
3689 else
3690 {
3691 res->defstate = SS_DEAD;
3692 }
3693
28bf4b0b 3694 res->definfo = stateInfo_update (res->definfo, other->definfo);
616915dd 3695 sRef_clearDerived (other);
3696 sRef_clearDerived (res);
3697 }
3698 else if (res->defstate == SS_DEAD
3699 && ((sRef_isOnly (other) && sRef_definitelyNull (other))
3700 || (other->defstate == SS_UNDEFINED
3701 || other->defstate == SS_UNUSEABLE)))
3702 {
3703 if (other->defstate == SS_UNDEFINED
3704 || other->defstate == SS_UNUSEABLE)
3705 {
3706 res->defstate = SS_UNUSEABLE;
3707 }
3708 else
3709 {
3710 res->defstate = SS_DEAD;
3711 }
3712
3713 sRef_clearDerived (other);
3714 sRef_clearDerived (res);
3715 }
3716 else if (res->defstate == SS_DEFINED
3717 && (other->defstate == SS_ALLOCATED
3718 && sRef_definitelyNull (other)))
3719 {
3720 other->defstate = SS_DEFINED; /* definitely null! */
3721 }
3722 else if (other->defstate == SS_DEFINED
3723 && (res->defstate == SS_ALLOCATED && sRef_definitelyNull (res)))
3724 {
3725 res->defstate = SS_DEFINED;
28bf4b0b 3726 res->definfo = stateInfo_update (res->definfo, other->definfo);
616915dd 3727 }
3728 else
3729 {
3730 ; /* okay */
3731 }
3732
3733 if (res->defstate == SS_DEAD && other->defstate == SS_DEAD)
3734 {
3735 sRef_clearDerived (other);
3736 sRef_clearDerived (res);
3737 }
3738
3739 /*
3740 ** only & dead isn't really an only!
3741 */
3742
3743 if (alkind_isOnly (other->aliaskind) && other->defstate == SS_DEAD)
3744 {
3745 other->aliaskind = AK_UNKNOWN;
3746 }
3747
3748 if (alkind_isOnly (res->aliaskind) && res->defstate == SS_DEAD)
3749 {
3750 res->aliaskind = AK_UNKNOWN;
3751 }
3752
3753 /*
3754 ** Dead and dependent -> dead
3755 */
3756
3757 if (alkind_isDependent (other->aliaskind) && res->defstate == SS_DEAD)
3758 {
3759 other->aliaskind = AK_UNKNOWN;
3760 other->defstate = SS_DEAD;
3761 sRef_clearDerived (res);
3762 sRef_clearDerived (other);
28bf4b0b 3763 }
3764
616915dd 3765 if (alkind_isDependent (res->aliaskind) && other->defstate == SS_DEAD)
3766 {
3767 res->aliaskind = AK_UNKNOWN;
3768 res->defstate = SS_DEAD;
3769 sRef_clearDerived (res);
3770 sRef_clearDerived (other);
3771 }
3772
3773 /*
3774 ** must do alias combine first, since it depends on
3775 ** original values of state and null.
3776 */
3777
3778 sRef_combineAliasKinds (res, other, cl, loc);
3779 sRef_combineDefState (res, other);
3780 sRef_combineNullState (res, other);
3781
3782 if (rdef == SS_ALLOCATED || rdef == SS_SPECIAL)
3783 {
3784 if (odef == SS_DEFINED)
3785 {
3786 if (onull == NS_DEFNULL || onull == NS_CONSTNULL)
3787 {
28bf4b0b 3788 res->deriv = sRefSet_copyInto (res->deriv, other->deriv);
3789 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
3790 }
616915dd 3791 }
28bf4b0b 3792 else if (odef == SS_ALLOCATED || odef == SS_SPECIAL)
616915dd 3793 {
3794
3795 if (doDerivs)
3796 {
3797 if (ctype_isUnion (ctype_realType (sRef_getType (res))))
3798 {
3799 res->deriv = sRef_mergeUnionDerivs (res->deriv,
3800 other->deriv,
3801 opt, cl, loc);
28bf4b0b 3802 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
616915dd 3803 }
3804 else
3805 {
28bf4b0b 3806 res->deriv = sRef_mergeDerivs (res->deriv, other->deriv,
616915dd 3807 opt, cl, loc);
28bf4b0b 3808 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
616915dd 3809 }
3810 }
3811 }
3812 else
3813 {
3814 if (doDerivs)
3815 {
28bf4b0b 3816 res->deriv = sRef_mergeDerivs (res->deriv, other->deriv,
616915dd 3817 opt, cl, loc);
28bf4b0b 3818 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
616915dd 3819 }
3820 else
3821 {
28bf4b0b 3822 ;
3823 }
616915dd 3824 }
3825 }
3826 else
3827 {
3828 if (rdef == SS_PDEFINED
3829 || (rdef == SS_DEFINED && odef == SS_PDEFINED))
3830 {
28bf4b0b 3831 if (doDerivs)
3832 {
3833 res->deriv = sRef_mergePdefinedDerivs (res->deriv, other->deriv,
3834 opt, cl, loc);
3835 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
3836 }
616915dd 3837 }
3838 else
3839 {
3840 if ((rdef == SS_DEFINED || rdef == SS_UNKNOWN)
3841 && res->defstate == SS_ALLOCATED)
3842 {
28bf4b0b 3843 res->deriv = sRefSet_copyInto (res->deriv, other->deriv);
616915dd 3844 }
3845 else
3846 {
3847 if (doDerivs)
3848 {
28bf4b0b 3849 res->deriv = sRef_mergeDerivs (res->deriv, other->deriv,
616915dd 3850 opt, cl, loc);
28bf4b0b 3851 DPRINTF (("Copy derivs: %s", sRef_unparseFull (res)));
616915dd 3852 }
3853 }
3854 }
3855 }
28bf4b0b 3856
616915dd 3857
3858 sRef_combineExKinds (res, other);
3859 }
3860 else
3861 {
3862 if (res->kind == SK_ARRAYFETCH && other->kind == SK_PTR)
3863 {
3864 sRef nother = sRef_buildArrayFetchKnown (sRef_getBase (other), 0);
3865
3866 sRef_copyState (nother, other);
3867 sRef_mergeStateAux (res, nother, cl, opt, loc, doDerivs);
3868 }
3869 else if (res->kind == SK_PTR && other->kind == SK_ARRAYFETCH)
3870 {
3871 sRef nother = sRef_buildPointer (sRef_getBase (other));
3872
3873 if (sRef_isValid (nother))
3874 {
3875 sRef_copyState (nother, other);
3876 sRef_mergeStateAux (res, nother, cl, opt, loc, doDerivs);
3877 }
3878 }
3879 else
3880 {
3881 llcontbug (message ("merge conj: %q / %q", sRef_unparseFull (res),
3882 sRef_unparseFull (other)));
3883
3884 }
3885 }
28bf4b0b 3886
3887 /*
3888 ** Merge value table states
3889 */
3890
3891 valueTable_elements (res->state, key, sv)
3892 {
3893 stateValue os = valueTable_lookup (other->state, key);
3894 /*@unused@*/ int val;
3895 /*@unused@*/ char *msg;
3896
3897 llassert (stateValue_isDefined (os));
3898
3899 DPRINTF (("Merge state: %s / %s",
3900 cstring_toCharsSafe (stateValue_unparse (sv)),
3901 cstring_toCharsSafe (stateValue_unparse (os))));
3902 /*
3903 val = valueMatix_lookup (key,
3904 stateValue_getValue (os),
3905 stateValue_getValue (sv),
3906 &msg);
3907 DPRINTF (("Val: %d / %s", val, msg));
3908 */
3909 } end_valueTable_elements ;
3910}
616915dd 3911
3912static sRefSet
3913sRef_mergeUnionDerivs (/*@only@*/ sRefSet res,
3914 /*@exposed@*/ sRefSet other, bool opt,
3915 clause cl, fileloc loc)
3916{
3917 if (sRefSet_isEmpty (res))
3918 {
28bf4b0b 3919 return sRefSet_copyInto (res, other);
616915dd 3920 }
3921 else
3922 {
3923 sRefSet_allElements (other, el)
3924 {
3925 if (sRef_isValid (el))
3926 {
3927 sRef e2 = sRefSet_lookupMember (other, el);
3928
3929 if (sRef_isValid (e2))
3930 {
3931 sRef_mergeStateAux (el, e2, cl, opt, loc, FALSE);
3932 }
3933 else
3934 {
3935 res = sRefSet_insert (res, el);
3936 }
3937 }
3938 } end_sRefSet_allElements ;
3939
3940 return res;
3941 }
3942}
3943
3944static /*@only@*/ sRefSet
3945sRef_mergeDerivs (/*@only@*/ sRefSet res, sRefSet other,
3946 bool opt, clause cl, fileloc loc)
3947{
3948 sRefSet ret = sRefSet_new ();
28bf4b0b 3949
616915dd 3950 sRefSet_allElements (res, el)
3951 {
3952 if (sRef_isValid (el))
3953 {
3954 sRef e2 = sRefSet_lookupMember (other, el);
3955
3956 if (sRef_isValid (e2))
3957 {
3958 if (el->defstate == SS_ALLOCATED &&
3959 e2->defstate == SS_PDEFINED)
3960 {
3961 e2->defstate = SS_ALLOCATED;
3962 }
3963 else if (e2->defstate == SS_ALLOCATED &&
3964 el->defstate == SS_PDEFINED)
3965 {
3966 el->defstate = SS_ALLOCATED;
3967 sRef_clearDerived (el);
3968 }
3969 else if ((el->defstate == SS_DEAD || sRef_isKept (el)) &&
3970 (e2->defstate == SS_DEFINED && !sRef_isKept (e2)))
3971 {
3972
3973 if (checkDeadState (el, TRUE, loc))
3974 {
3975 if (sRef_isThroughArrayFetch (el))
3976 {
3977 sRef_maybeKill (el, loc);
3978 sRef_maybeKill (e2, loc);
3979 }
3980 }
3981 }
3982 else if ((e2->defstate == SS_DEAD || sRef_isKept (e2)) &&
3983 (el->defstate == SS_DEFINED && !sRef_isKept (el)))
3984 {
3985
3986 if (checkDeadState (e2, FALSE, loc))
3987 {
3988 if (sRef_isThroughArrayFetch (el))
3989 {
3990 sRef_maybeKill (el, loc);
3991 sRef_maybeKill (e2, loc);
3992 }
3993 }
3994 }
3995 else if (el->defstate == SS_DEFINED &&
3996 e2->defstate == SS_PDEFINED)
3997 {
28bf4b0b 3998 DPRINTF (("set pdefined: %s", sRef_unparseFull (el)));
616915dd 3999 el->defstate = SS_PDEFINED;
4000 }
4001 else if (e2->defstate == SS_DEFINED &&
4002 el->defstate == SS_PDEFINED)
4003 {
28bf4b0b 4004 DPRINTF (("set pdefined: %s", sRef_unparseFull (e2)));
616915dd 4005 e2->defstate = SS_PDEFINED;
4006 }
4007 else
4008 {
4009 ; /* okay */
4010 }
4011
4012 if (ctype_isUnion (ctype_realType (sRef_getType (el))))
4013 {
4014 el->deriv = sRef_mergeUnionDerivs (el->deriv, e2->deriv,
4015 opt, cl, loc);
4016 }
4017 else
4018 {
4019 el->deriv = sRef_mergeDerivs (el->deriv, e2->deriv, opt, cl, loc);
4020 }
4021
4022 if (sRef_equivalent (el, e2))
4023 {
28bf4b0b 4024 ret = sRefSet_insert (ret, el);
616915dd 4025 }
4026 else
4027 {
4028 sRef sr = sRef_leastCommon (el, e2);
4029
4030 if (sRef_isValid (sr))
4031 {
4032 ret = sRefSet_insert (ret, sr);
4033 }
4034 else
4035 {
4036 ;
4037 }
4038 }
4039
4040 (void) sRefSet_delete (other, e2);
4041 }
4042 else /* not defined */
4043 {
28bf4b0b 4044 (void) checkDeadState (el, TRUE, loc);
616915dd 4045 }
4046 }
4047 } end_sRefSet_allElements;
4048
4049 sRefSet_allElements (other, el)
4050 {
4051 if (sRef_isValid (el))
4052 {
4053 (void) checkDeadState (el, FALSE, loc);
4054 }
4055 } end_sRefSet_allElements;
28bf4b0b 4056
616915dd 4057 sRefSet_free (res);
4058 return (ret);
4059}
4060
4061/*
4062** Returns TRUE is there is an error.
4063*/
4064
4065static bool checkDeadState (/*@notnull@*/ sRef el, bool tbranch, fileloc loc)
4066{
4067 /*
4068 ** usymtab_isGuarded --- the utab should still be in the
4069 ** state of the alternate branch.
4070 **
4071 ** tbranch TRUE means el is released in the last branch, e.g.
4072 ** if (x != NULL) { ; } else { sfree (x); }
4073 ** so, if x is null in the other branch no error is reported.
4074 **
4075 ** tbranch FALSE means this is the other branch:
4076 ** if (x != NULL) { sfree (x); } else { ; }
4077 ** so, if x is null in this branch there is no error.
4078 */
4079
4080
4081 if ((sRef_isDead (el) || sRef_isKept (el))
4082 && !sRef_isDeepUnionField (el) && !sRef_isThroughArrayFetch (el))
4083 {
4084
4085 if (!tbranch)
4086 {
28bf4b0b 4087 if (usymtab_isDefinitelyNullDeep (el))
616915dd 4088 {
28bf4b0b 4089 return TRUE;
616915dd 4090 }
4091 }
4092 else
4093 {
28bf4b0b 4094 if (usymtab_isAltDefinitelyNullDeep (el))
616915dd 4095 {
28bf4b0b 4096 return TRUE;
616915dd 4097 }
4098 }
28bf4b0b 4099
616915dd 4100 if (optgenerror
4101 (FLG_BRANCHSTATE,
4102 message ("Storage %q is %q in one path, but live in another.",
4103 sRef_unparse (el),
4104 cstring_makeLiteral (sRef_isKept (el)
4105 ? "kept" : "released")),
4106 loc))
4107 {
4108
4109 if (sRef_isKept (el))
4110 {
4111 sRef_showAliasInfo (el);
4112 }
4113 else
4114 {
4115 sRef_showStateInfo (el);
4116 }
4117
4118 /* prevent further errors */
4119 el->defstate = SS_UNKNOWN;
4120 sRef_setAliasKind (el, AK_ERROR, fileloc_undefined);
4121
4122 return FALSE;
4123 }
4124 }
4125
4126 return TRUE;
4127}
4128
4129static void
4130checkDerivDeadState (/*@notnull@*/ sRef el, bool tbranch, fileloc loc)
4131{
4132
4133 if (checkDeadState (el, tbranch, loc))
4134 {
4135 sRefSet_allElements (el->deriv, t)
4136 {
4137 if (sRef_isValid (t))
4138 {
4139 checkDerivDeadState (t, tbranch, loc);
4140 }
4141 } end_sRefSet_allElements;
4142 }
4143}
4144
4145static sRefSet
4146 sRef_mergePdefinedDerivs (sRefSet res, sRefSet other, bool opt,
4147 clause cl, fileloc loc)
4148{
4149 sRefSet ret = sRefSet_new ();
4150
4151 sRefSet_allElements (res, el)
4152 {
4153 if (sRef_isValid (el))
4154 {
4155 sRef e2 = sRefSet_lookupMember (other, el);
4156
4157 if (sRef_isValid (e2))
4158 {
4159 if (sRef_isAllocated (el) && !sRef_isAllocated (e2))
4160 {
4161 ;
4162 }
4163 else if (sRef_isAllocated (e2) && !sRef_isAllocated (el))
4164 {
28bf4b0b 4165 el->deriv = sRefSet_copyInto (el->deriv, e2->deriv);
616915dd 4166 }
4167 else
4168 {
4169 el->deriv = sRef_mergePdefinedDerivs (el->deriv, e2->deriv,
4170 opt, cl, loc);
4171 }
4172
4173 sRef_mergeStateAux (el, e2, cl, opt, loc, FALSE);
4174
4175 ret = sRefSet_insert (ret, el);
4176 (void) sRefSet_delete (other, e2);
4177 }
4178 else
4179 {
4180 if (!opt)
4181 {
4182 checkDerivDeadState (el, (cl == FALSECLAUSE), loc);
4183 }
4184
4185 ret = sRefSet_insert (ret, el);
4186 }
4187 }
4188 } end_sRefSet_allElements;
4189
4190 sRefSet_allElements (other, el)
4191 {
4192 if (sRef_isValid (el))
4193 {
4194 if (!sRefSet_member (ret, el))
4195 {
4196 /* was cl == FALSECLAUSE */
4197 checkDerivDeadState (el, FALSE, loc);
4198 ret = sRefSet_insert (ret, el);
4199 }
4200 else
4201 {
4202 /*
4203 ** it's okay --- member is a different equality test
4204 */
4205 }
4206 }
4207 } end_sRefSet_allElements;
4208
4209 sRefSet_free (res);
4210 return (ret);
4211}
4212
4213sRef sRef_makeConj (/*@exposed@*/ /*@returned@*/ sRef a, /*@exposed@*/ sRef b)
4214{
4215 llassert (sRef_isValid (a));
4216 llassert (sRef_isValid (b));
4217
4218 if (!sRef_equivalent (a, b))
4219 {
28bf4b0b 4220 sRef s = sRef_newRef ();
616915dd 4221
4222 s->kind = SK_CONJ;
4223 s->info = (sinfo) dmalloc (sizeof (*s->info));
4224 s->info->conj = (cjinfo) dmalloc (sizeof (*s->info->conj));
28bf4b0b 4225 s->info->conj->a = a; /* sRef_copy (a) */ /*@i32*/ ;
4226 s->info->conj->b = b; /* sRef_copy (b);*/ /*@i32@*/ ;
616915dd 4227
4228 if (ctype_equal (a->type, b->type)) s->type = a->type;
4229 else s->type = ctype_makeConj (a->type, b->type);
4230
4231 if (a->defstate == b->defstate)
4232 {
4233 s->defstate = a->defstate;
4234 }
4235 else
4236 {
4237 s->defstate = SS_UNKNOWN;
4238 }
4239
28bf4b0b 4240 sRef_setNullStateN (s, NS_UNKNOWN);
616915dd 4241
4242 s->safe = a->safe && b->safe;
4243 s->aliaskind = alkind_resolve (a->aliaskind, b->aliaskind);
4244
28bf4b0b 4245 llassert (valueTable_isUndefined (s->state));
4246 s->state = context_createValueTable (s);
616915dd 4247 return s;
4248 }
4249 else
4250 {
4251 /*@-exposetrans@*/ return a; /*@=exposetrans@*/
4252 }
4253}
4254
28bf4b0b 4255/*@dependent@*/ sRef
616915dd 4256sRef_makeUnknown ()
4257{
4258 sRef s = sRef_new ();
4259
4260 s->kind = SK_UNKNOWN;
4261 return s;
4262}
4263
28bf4b0b 4264static /*@owned@*/ sRef
616915dd 4265sRef_makeSpecial (speckind sk) /*@*/
4266{
4267 sRef s = sRef_new ();
4268
4269 s->kind = SK_SPECIAL;
4270 s->info = (sinfo) dmalloc (sizeof (*s->info));
4271 s->info->spec = sk;
28bf4b0b 4272 /*@-dependenttrans@*/
616915dd 4273 return s;
28bf4b0b 4274 /*@=dependenttrans@*/
616915dd 4275}
4276
28bf4b0b 4277static /*@owned@*/ sRef srnothing = sRef_undefined;
4278static /*@owned@*/ sRef srinternal = sRef_undefined;
4279static /*@owned@*/ sRef srsystem = sRef_undefined;
4280static /*@owned@*/ sRef srspec = sRef_undefined;
616915dd 4281
28bf4b0b 4282/*@dependent@*/ sRef
616915dd 4283sRef_makeNothing (void)
4284{
4285 if (sRef_isInvalid (srnothing))
4286 {
4287 srnothing = sRef_makeSpecial (SR_NOTHING);
4288 }
4289
616915dd 4290 return srnothing;
616915dd 4291}
4292
4293sRef
4294sRef_makeInternalState (void)
4295{
4296 if (sRef_isInvalid (srinternal))
4297 {
4298 srinternal = sRef_makeSpecial (SR_INTERNAL);
4299 }
4300
616915dd 4301 return srinternal;
616915dd 4302}
4303
4304sRef
4305sRef_makeSpecState (void)
4306{
4307 if (sRef_isInvalid (srspec))
4308 {
4309 srspec = sRef_makeSpecial (SR_SPECSTATE);
4310 }
4311
616915dd 4312 return srspec;
616915dd 4313}
4314
4315sRef
4316sRef_makeSystemState (void)
4317{
4318 if (sRef_isInvalid (srsystem))
4319 {
4320 srsystem = sRef_makeSpecial (SR_SYSTEM);
4321 }
4322
28bf4b0b 4323 return srsystem;
4324}
4325
4326sRef
4327sRef_makeGlobalMarker (void)
4328{
4329 sRef s = sRef_makeSpecial (SR_GLOBALMARKER);
4330 llassert (valueTable_isUndefined (s->state));
4331 s->state = context_createGlobalMarkerValueTable ();
4332 return s;
616915dd 4333}
4334
4335static sRef
4336sRef_makeResultType (ctype ct)
4337{
4338 sRef res = sRef_makeResult ();
4339
4340 res->type = ct;
4341 return res;
4342}
4343
4344sRef
4345sRef_makeResult ()
4346{
28bf4b0b 4347 sRef s = sRef_newRef ();
616915dd 4348
4349 s->kind = SK_RESULT;
4350 s->type = ctype_unknown;
4351 s->defstate = SS_UNKNOWN;
4352 s->aliaskind = AK_UNKNOWN;
28bf4b0b 4353 sRef_setNullStateN (s, NS_UNKNOWN);
4354 llassert (valueTable_isUndefined (s->state));
4355 s->state = context_createValueTable (s);
4356
4357 DPRINTF (("Result: [%p] %s", s, sRef_unparseFull (s)));
616915dd 4358 return s;
4359}
4360
4361
4362bool
4363sRef_isNothing (sRef s)
4364{
4365 return (sRef_isKindSpecial (s) && s->info->spec == SR_NOTHING);
4366}
4367
4368bool
4369sRef_isInternalState (sRef s)
4370{
4371 return (sRef_isKindSpecial (s) && s->info->spec == SR_INTERNAL);
4372}
4373
4374bool
4375sRef_isSpecInternalState (sRef s)
4376{
4377 return (sRef_isKindSpecial (s)
4378 && (s->info->spec == SR_INTERNAL || s->info->spec == SR_SPECSTATE));
4379}
4380
4381bool
4382sRef_isSpecState (sRef s)
4383{
4384 return (sRef_isKindSpecial (s) && s->info->spec == SR_SPECSTATE);
4385}
4386
4387bool
4388sRef_isResult (sRef s)
4389{
4390 return (sRef_isValid (s) && s->kind == SK_RESULT);
4391}
4392
4393bool
4394sRef_isSystemState (sRef s)
4395{
4396 return (sRef_isKindSpecial (s) && s->info->spec == SR_SYSTEM);
4397}
4398
28bf4b0b 4399bool
4400sRef_isGlobalMarker (sRef s)
4401{
4402 return (sRef_isKindSpecial (s) && s->info->spec == SR_GLOBALMARKER);
4403}
4404
616915dd 4405usymId
4406sRef_getScopeIndex (sRef s)
4407{
4408 llassert (sRef_isValid (s));
4409 llassert (sRef_isCvar (s));
4410
4411 return (s->info->cvar->index);
4412}
4413
4414void
4415sRef_makeSafe (sRef s)
4416{
4417 if (sRef_isValid (s))
4418 {
4419 s->safe = TRUE;
4420 }
4421}
4422
4423void
4424sRef_makeUnsafe (sRef s)
4425{
4426 if (sRef_isValid (s))
4427 {
4428 s->safe = FALSE;
4429 }
4430}
4431
4432/*
4433** memory state operations
4434*/
4435
4436/*@only@*/ cstring sRef_unparseFull (sRef s)
4437{
4438 if (sRef_isInvalid (s)) return (cstring_undefined);
4439
28bf4b0b 4440 return (message ("[%d] %q - %q [%s] { %q } < %q >",
616915dd 4441 (int) s,
4442 sRef_unparseDebug (s),
4443 sRef_unparseState (s),
4444 exkind_unparse (s->oexpkind),
28bf4b0b 4445 sRefSet_unparseDebug (s->deriv),
4446 valueTable_unparse (s->state)));
616915dd 4447}
4448
4449/*@unused@*/ cstring sRef_unparseDeep (sRef s)
4450{
4451 cstring st = cstring_undefined;
4452
4453 st = message ("%q:", sRef_unparseFull (s));
4454
4455 if (sRef_isValid (s))
4456 {
4457 sRefSet_allElements (s->deriv, el)
4458 {
4459 st = message("%q\n%q", st, sRef_unparseDeep (el));
4460 } end_sRefSet_allElements ;
4461 }
4462
4463 return st;
4464}
4465
4466/*@only@*/ cstring sRef_unparseState (sRef s)
4467{
4468 if (sRef_isConj (s))
4469 {
4470 return (message ("%q | %q",
4471 sRef_unparseState (s->info->conj->a),
4472 sRef_unparseState (s->info->conj->b)));
4473 }
4474
4475 if (sRef_isInvalid (s))
4476 {
4477 return (cstring_makeLiteral ("<invalid>"));
4478 }
4479
4480 return (message ("%s.%s.%s.%s",
4481 alkind_unparse (s->aliaskind),
28bf4b0b 4482 nstate_unparse (sRef_getNullState (s)),
616915dd 4483 exkind_unparse (s->expkind),
4484 sstate_unparse (s->defstate)));
4485}
4486
4487bool sRef_isNotUndefined (sRef s)
4488{
4489 return (sRef_isInvalid (s)
4490 || (s->defstate != SS_UNDEFINED
4491 && s->defstate != SS_UNUSEABLE
4492 && s->defstate != SS_DEAD));
4493}
4494
4495ynm sRef_isWriteable (sRef s)
4496{
4497 if (sRef_isInvalid (s)) return MAYBE;
4498
4499 if (sRef_isConj (s) && s->defstate == SS_UNKNOWN)
4500 {
4501 if (ynm_toBoolStrict (sRef_isWriteable (sRef_getConjA (s))))
4502 {
4503 if (ynm_toBoolStrict (sRef_isWriteable (sRef_getConjB (s))))
4504 {
4505 return YES;
4506 }
4507 return MAYBE;
4508 }
4509 else
4510 {
4511 if (ynm_toBoolStrict (sRef_isWriteable (sRef_getConjB (s))))
4512 {
4513 return MAYBE;
4514 }
4515 return NO;
4516 }
4517 }
4518
4519 return (ynm_fromBool (s->defstate != SS_UNUSEABLE));
4520}
4521
4522bool sRef_hasNoStorage (sRef s)
4523{
4524 return (!sRef_isAllocatedStorage (s) || sRef_isDefinitelyNull (s));
4525}
4526
4527bool sRef_isStrictReadable (sRef s)
4528{
4529 return (ynm_toBoolStrict (sRef_isReadable (s)));
4530}
4531
4532ynm sRef_isReadable (sRef s)
4533{
4534 sstate ss;
4535
4536 if (sRef_isInvalid (s)) return YES;
4537
4538 ss = s->defstate;
4539
4540 if (sRef_isConj (s) && s->defstate == SS_UNKNOWN)
4541 {
4542 if (ynm_toBoolStrict (sRef_isReadable (sRef_getConjA (s))))
4543 {
4544 if (ynm_toBoolStrict (sRef_isReadable (sRef_getConjB (s))))
4545 {
4546 return YES;
4547 }
4548 return MAYBE;
4549 }
4550 else
4551 {
4552 if (ynm_toBoolStrict (sRef_isReadable (sRef_getConjB (s))))
4553 {
4554 return MAYBE;
4555 }
4556 return NO;
4557 }
4558 }
4559 else if (ss == SS_HOFFA)
4560 {
4561 if (context_getFlag (FLG_STRICTUSERELEASED))
4562 {
4563 return MAYBE;
4564 }
4565 else
4566 {
4567 return YES;
4568 }
4569 }
4570 else
4571 {
4572 return (ynm_fromBool (ss == SS_DEFINED
4573 || ss == SS_FIXED
4574 || ss == SS_RELDEF
4575 || ss == SS_PDEFINED
4576 || ss == SS_PARTIAL
4577 || ss == SS_SPECIAL
4578 || ss == SS_ALLOCATED
28bf4b0b 4579 || ss == SS_KILLED /* evans 2001-05-26: added this for killed globals */
616915dd 4580 || ss == SS_UNKNOWN));
4581 }
4582}
4583
4584static /*@exposed@*/ sRef whatUndefined (/*@exposed@*/ sRef fref, int depth)
4585{
4586 ctype ct;
4587
4588
4589 if (depth > MAXDEPTH)
4590 {
4591 llgenmsg (message
4592 ("Warning: check definition limit exceeded, checking %q. "
4593 "This either means there is a variable with at least "
4594 "%d indirections apparent in the program text, or "
4595 "there is a bug in LCLint.",
4596 sRef_unparse (fref),
4597 MAXDEPTH),
4598 g_currentloc);
4599
4600 return sRef_undefined;
4601 }
4602
4603 if (!sRef_isKnown (fref) || sRef_isAnyDefined (fref))
4604 {
4605 return sRef_undefined;
4606 }
4607
4608 if (sRef_isUnuseable (fref) || sRef_isStateUndefined (fref))
4609 {
4610 return fref;
4611 }
4612
4613 ct = ctype_realType (sRef_getType (fref));
4614
4615 if (ctype_isUnknown (ct))
4616 {
4617 return sRef_undefined;
4618 }
4619 else if (ctype_isPointer (ct) || ctype_isArray (ct))
4620 {
4621 if (sRef_isStateUnknown (fref))
4622 {
4623 return sRef_undefined;
4624 }
4625 else
4626 {
4627 sRef fptr = sRef_constructDeref (fref);
4628
4629 return (whatUndefined (fptr, depth + 1));
4630 }
4631 }
4632 else if (ctype_isStruct (ct))
4633 {
4634 bool hasOneDefined = FALSE;
4635
4636 if (sRef_isStateUnknown (fref))
4637 {
4638 return fref;
4639 }
4640
4641 if (sRef_isPdefined (fref) || sRef_isAnyDefined (fref))
4642 {
4643 sRefSet_realElements (sRef_derivedFields (fref), sr)
4644 {
4645 hasOneDefined = TRUE;
4646
4647 if (sRef_isField (sr))
4648 {
4649 cstring fieldname = sRef_getField (sr);
4650 sRef fldref = sRef_makeField (fref, fieldname);
4651 bool shouldCheck = !sRef_isRecursiveField (fldref);
4652
4653 if (shouldCheck)
4654 {
4655 sRef wdef = whatUndefined (fldref, depth + 1);
4656
4657 if (sRef_isValid (wdef))
4658 {
4659 return wdef;
4660 }
4661 }
4662 }
4663 } end_sRefSet_realElements;
4664 }
4665 else if (sRef_isAllocated (fref))
4666 {
4667 /*
4668 ** for structures, each field must be completely defined
4669 */
4670
4671 uentryList fields = ctype_getFields (ct);
4672
4673 uentryList_elements (fields, ue)
4674 {
4675 cstring name = uentry_getRealName (ue);
4676 sRef ffield = sRef_makeField (fref, name);
4677 bool shouldCheck = !sRef_isRecursiveField (ffield);
4678
4679 if (sRef_isRelDef (uentry_getSref (ue)))
4680 {
4681 ; /* no error */
4682 }
4683 else
4684 {
4685 if (shouldCheck)
4686 {
4687 sRef wdef = whatUndefined (ffield, depth + 1);
4688
4689 if (sRef_isInvalid (wdef))
4690 {
4691 return wdef;
4692 }
4693 }
4694 }
4695 } end_uentryList_elements;
4696 }
4697 else
4698 {
4699 ;
4700 }
4701 }
4702 else if (ctype_isUnion (ct))
4703 {
4704 ;
4705 }
4706 else
4707 {
4708 ;
4709 }
4710
4711 return sRef_undefined;
4712}
4713
28bf4b0b 4714static bool checkDefined (/*@temp@*/ sRef sr)
616915dd 4715{
28bf4b0b 4716 /*@-temptrans@*/ /* the result from whatUndefined is lost */
616915dd 4717 return (sRef_isInvalid (whatUndefined (sr, 0)));
28bf4b0b 4718 /*@=temptrans@*/
616915dd 4719}
4720
4721bool sRef_isReallyDefined (sRef s)
4722{
4723 if (sRef_isValid (s))
4724 {
4725 if (sRef_isAnyDefined (s))
4726 {
4727 return TRUE;
4728 }
4729 else
4730 {
4731 if (sRef_isAllocated (s) || sRef_isPdefined (s))
4732 {
4733 return checkDefined (s);
4734 }
4735 else
4736 {
4737 return FALSE;
4738 }
4739 }
4740 }
4741 else
4742 {
4743 return TRUE;
4744 }
4745}
4746
4747void sRef_showNotReallyDefined (sRef s)
4748{
4749 if (sRef_isValid (s))
4750 {
4751 if (sRef_isAnyDefined (s))
4752 {
4753 BADBRANCH;
4754 }
4755 else
4756 {
4757 if (sRef_isAllocated (s) || sRef_isPdefined (s))
4758 {
28bf4b0b 4759 /*@-temptrans@*/ /* the result of whatUndefined is lost */
616915dd 4760 sRef ref = whatUndefined (s, 0);
4761
4762 llassert (sRef_isValid (ref));
4763
4764 if (ref != s)
4765 {
4766 llgenindentmsgnoloc
4767 (message ("This sub-reference is %s: %q",
4768 sstate_unparse (sRef_getDefState (ref)),
4769 sRef_unparse (ref)));
4770 }
4771 }
4772 else
4773 {
4774 ;
4775 }
4776 }
4777 }
4778 else
4779 {
4780 BADBRANCH;
4781 }
4782}
4783
4784sstate sRef_getDefState (sRef s)
4785{
4786 if (sRef_isInvalid (s)) return (SS_UNKNOWN);
4787 return (s->defstate);
4788}
4789
4790void sRef_setDefState (sRef s, sstate defstate, fileloc loc)
4791{
28bf4b0b 4792 sRef_checkMutable (s);
616915dd 4793 sRef_setStateAux (s, defstate, loc);
4794}
4795
4796static void sRef_clearAliasStateAux (sRef s, fileloc loc)
4797{
28bf4b0b 4798 sRef_checkMutable (s);
616915dd 4799 sRef_setAliasKind (s, AK_ERROR, loc);
4800}
4801
4802void sRef_clearAliasState (sRef s, fileloc loc)
4803{
28bf4b0b 4804 sRef_checkMutable (s);
616915dd 4805 sRef_aliasSetComplete (sRef_clearAliasStateAux, s, loc);
4806}
4807
4808void sRef_setAliasKindComplete (sRef s, alkind kind, fileloc loc)
4809{
28bf4b0b 4810 sRef_checkMutable (s);
616915dd 4811 sRef_aliasSetCompleteParam (sRef_setAliasKind, s, kind, loc);
4812}
4813
4814void sRef_setAliasKind (sRef s, alkind kind, fileloc loc)
4815{
28bf4b0b 4816 sRef_checkMutable (s);
4817
616915dd 4818 if (sRef_isValid (s))
4819 {
4820 sRef_clearDerived (s);
4821
4822 if ((kind != s->aliaskind && kind != s->oaliaskind)
4823 && fileloc_isDefined (loc))
4824 {
28bf4b0b 4825 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, loc);
616915dd 4826 }
4827
4828 s->aliaskind = kind;
4829 }
4830}
4831
4832void sRef_setOrigAliasKind (sRef s, alkind kind)
4833{
28bf4b0b 4834 sRef_checkMutable (s);
4835
616915dd 4836 if (sRef_isValid (s))
4837 {
4838 s->oaliaskind = kind;
4839 }
4840}
4841
4842exkind sRef_getExKind (sRef s)
4843{
4844 if (sRef_isValid (s))
4845 {
4846 return (s->expkind);
4847 }
4848 else
4849 {
4850 return XO_UNKNOWN;
4851 }
4852}
4853
4854exkind sRef_getOrigExKind (sRef s)
4855{
4856 if (sRef_isValid (s))
4857 {
4858 return (s->oexpkind);
4859 }
4860 else
4861 {
4862 return XO_UNKNOWN;
4863 }
4864}
4865
4866static void sRef_clearExKindAux (sRef s, fileloc loc)
4867{
28bf4b0b 4868 sRef_checkMutable (s);
616915dd 4869 sRef_setExKind (s, XO_UNKNOWN, loc);
4870}
4871
4872void sRef_setObserver (sRef s, fileloc loc)
4873{
28bf4b0b 4874 sRef_checkMutable (s);
616915dd 4875 sRef_setExKind (s, XO_OBSERVER, loc);
4876}
4877
4878void sRef_setExposed (sRef s, fileloc loc)
4879{
28bf4b0b 4880 sRef_checkMutable (s);
616915dd 4881 sRef_setExKind (s, XO_EXPOSED, loc);
4882}
4883
4884void sRef_clearExKindComplete (sRef s, fileloc loc)
4885{
4886 (void) sRef_aliasSetComplete (sRef_clearExKindAux, s, loc);
4887}
4888
4889void sRef_setExKind (sRef s, exkind exp, fileloc loc)
4890{
28bf4b0b 4891 sRef_checkMutable (s);
4892
616915dd 4893 if (sRef_isValid (s))
4894 {
4895 if (s->expkind != exp)
4896 {
28bf4b0b 4897 s->expinfo = stateInfo_updateLoc (s->expinfo, loc);
616915dd 4898 }
4899
4900 s->expkind = exp;
4901 }
4902}
4903
4904/*
4905** s1->derived = s2->derived
4906*/
4907
4908static void sRef_copyRealDerived (sRef s1, sRef s2)
4909{
28bf4b0b 4910 DPRINTF (("Copy real: %s / %s", sRef_unparse (s1), sRef_unparse (s2)));
4911 sRef_checkMutable (s1);
4912
616915dd 4913 if (sRef_isValid (s1) && sRef_isValid (s2))
4914 {
4915 sRef sb = sRef_getRootBase (s1);
4916
4917 sRefSet_clear (s1->deriv);
4918
4919 sRefSet_allElements (s2->deriv, el)
4920 {
4921 if (sRef_isValid (el))
4922 {
4923 sRef rb = sRef_getRootBase (el);
4924
4925 if (!sRef_same (rb, sb))
4926 {
4927 sRef fb = sRef_fixDirectBase (el, s1);
4928
4929 if (sRef_isValid (fb))
4930 {
4931 sRef_copyRealDerived (fb, el);
4932 sRef_addDeriv (s1, fb);
4933 }
4934 }
4935 else
4936 {
4937 sRef_addDeriv (s1, el);
4938 }
4939 }
4940 } end_sRefSet_allElements ;
4941 }
4942
4943 }
4944
4945void sRef_copyRealDerivedComplete (sRef s1, sRef s2)
4946{
4947 sRef_innerAliasSetCompleteParam (sRef_copyRealDerived, s1, s2);
4948}
4949
4950void sRef_setUndefined (sRef s, fileloc loc)
4951{
28bf4b0b 4952 sRef_checkMutable (s);
4953
616915dd 4954 if (sRef_isValid (s))
4955 {
4956 s->defstate = SS_UNDEFINED;
4957
4958 if (fileloc_isDefined (loc))
4959 {
28bf4b0b 4960 s->definfo = stateInfo_updateLoc (s->definfo, loc);
616915dd 4961 }
4962
4963 sRef_clearDerived (s);
4964 }
4965}
4966
4967static void sRef_setDefinedAux (sRef s, fileloc loc, bool clear)
4968{
28bf4b0b 4969 sRef_checkMutable (s);
616915dd 4970 if (sRef_isInvalid (s)) return;
4971
4972 if (s->defstate != SS_DEFINED && fileloc_isDefined (loc))
4973 {
28bf4b0b 4974 s->definfo = stateInfo_updateLoc (s->definfo, loc);
616915dd 4975 }
4976
4977 s->defstate = SS_DEFINED;
4978
4979 /* e.g., if x is allocated, *x = 3 defines x */
4980
4981 if (s->kind == SK_PTR)
4982 {
4983 sRef p = s->info->ref;
4984
4985 if (p->defstate == SS_ALLOCATED)
4986 {
4987 sRef_setDefinedAux (p, loc, clear);
4988 }
4989 }
4990 else if (s->kind == SK_ARRAYFETCH)
4991 {
4992 if (!s->info->arrayfetch->indknown
4993 || (s->info->arrayfetch->ind == 0))
4994 {
4995 sRef p = s->info->arrayfetch->arr;
4996 sRef ptr = sRef_constructPointer (p);
4997
4998 if (sRef_isValid (ptr))
4999 {
5000 if (ptr->defstate == SS_ALLOCATED
5001 || ptr->defstate == SS_UNDEFINED)
5002 {
5003 sRef_setDefinedAux (ptr, loc, clear);
5004 }
5005 }
5006
5007 if (p->defstate == SS_RELDEF)
5008 {
5009 ;
5010 }
5011 else if (p->defstate == SS_ALLOCATED || p->defstate == SS_PDEFINED)
5012 {
5013 p->defstate = SS_DEFINED;
5014 }
5015 else
5016 {
5017 }
5018 }
5019 }
5020 else if (s->kind == SK_FIELD)
5021 {
5022 sRef parent = s->info->field->rec;
5023
5024 if (sRef_isValid (parent))
5025 {
5026 if (ctype_isUnion (ctype_realType (parent->type)))
5027 {
5028 /*
5029 ** Should not clear derived from here.
5030 */
5031
5032 sRef_setDefinedNoClear (parent, loc);
5033 }
5034 else
5035 {
5036 ; /* Nothing to do for structures. */
5037 }
5038 }
5039
5040 }
5041 else
5042 {
5043 ;
5044 }
5045
5046 if (clear)
5047 {
5048 sRef_clearDerived (s);
5049 }
5050}
5051
5052static void sRef_setPartialDefined (sRef s, fileloc loc)
5053{
28bf4b0b 5054 sRef_checkMutable (s);
5055
616915dd 5056 if (!sRef_isPartial (s))
5057 {
5058 sRef_setDefined (s, loc);
5059 }
5060}
5061
5062void sRef_setPartialDefinedComplete (sRef s, fileloc loc)
5063{
5064 sRef_innerAliasSetComplete (sRef_setPartialDefined, s, loc);
5065}
5066
5067void sRef_setDefinedComplete (sRef s, fileloc loc)
5068{
5069 sRef_innerAliasSetComplete (sRef_setDefined, s, loc);
5070}
5071
5072void sRef_setDefined (sRef s, fileloc loc)
5073{
28bf4b0b 5074 sRef_checkMutable (s);
616915dd 5075 sRef_setDefinedAux (s, loc, TRUE);
5076}
5077
5078static void sRef_setDefinedNoClear (sRef s, fileloc loc)
5079{
28bf4b0b 5080 sRef_checkMutable (s);
616915dd 5081 DPRINTF (("Defining: %s", sRef_unparseFull (s)));
5082 sRef_setDefinedAux (s, loc, FALSE);
5083 DPRINTF (("==> %s", sRef_unparseFull (s)));
5084}
5085
5086void sRef_setDefinedNCComplete (sRef s, fileloc loc)
5087{
28bf4b0b 5088 sRef_checkMutable (s);
616915dd 5089 DPRINTF (("Set Defined Complete: %s", sRef_unparseFull (s)));
5090 sRef_innerAliasSetComplete (sRef_setDefinedNoClear, s, loc);
5091 DPRINTF (("==> %s", sRef_unparseFull (s)));
5092}
5093
5094static bool sRef_isDeepUnionField (sRef s)
5095{
5096 return (sRef_deepPred (sRef_isUnionField, s));
5097}
5098
5099bool sRef_isUnionField (sRef s)
5100{
5101 if (sRef_isValid (s) && s->kind == SK_FIELD)
5102 {
5103 /*
5104 ** defining one field of a union defines the union
5105 */
5106
5107 sRef base = s->info->field->rec;
5108
5109 if (sRef_isValid (base))
5110 {
5111 return (ctype_isUnion (ctype_realType (base->type)));
5112 }
5113 }
5114
5115 return FALSE;
5116}
5117
5118void sRef_setPdefined (sRef s, fileloc loc)
5119{
28bf4b0b 5120 sRef_checkMutable (s);
616915dd 5121 if (sRef_isValid (s) && !sRef_isPartial (s))
5122 {
5123 sRef base = sRef_getBaseSafe (s);
5124
5125 if (s->defstate == SS_ALLOCATED)
5126 {
5127 return;
5128 }
5129
5130 if (s->defstate != SS_PDEFINED && fileloc_isDefined (loc))
5131 {
28bf4b0b 5132 s->definfo = stateInfo_updateLoc (s->definfo, loc);
616915dd 5133 }
5134
28bf4b0b 5135 DPRINTF (("set pdefined: %s", sRef_unparseFull (s)));
616915dd 5136 s->defstate = SS_PDEFINED;
5137
5138 /* e.g., if x is allocated, *x = 3 defines x */
5139
5140 while (sRef_isValid (base) && sRef_isKnown (base))
5141 {
5142 if (base->defstate == SS_DEFINED)
5143 {
5144 sRef nb;
28bf4b0b 5145
5146 base->defstate = SS_PDEFINED;
616915dd 5147 nb = sRef_getBaseSafe (base);
5148 base = nb;
5149 }
5150 else
5151 {
5152 break;
5153 }
5154 }
5155 }
5156}
5157
5158static void sRef_setStateAux (sRef s, sstate ss, fileloc loc)
5159{
28bf4b0b 5160 sRef_checkMutable (s);
5161
616915dd 5162 if (sRef_isValid (s))
5163 {
5164 /* if (s->defstate == SS_RELDEF) return; */
5165
5166 if (s->defstate != ss && fileloc_isDefined (loc))
5167 {
28bf4b0b 5168 s->definfo = stateInfo_updateLoc (s->definfo, loc);
616915dd 5169 }
5170
5171 s->defstate = ss;
5172 sRef_clearDerived (s);
5173
5174 if (ss == SS_ALLOCATED)
5175 {
5176 sRef base = sRef_getBaseSafe (s);
5177
5178 while (sRef_isValid (base) && sRef_isKnown (base))
5179 {
5180 if (base->defstate == SS_DEFINED)
5181 {
5182 sRef nb;
5183
5184 base->defstate = SS_PDEFINED;
5185
5186 nb = sRef_getBaseSafe (base);
5187 base = nb;
5188 }
5189 else
5190 {
5191 break;
5192 }
5193 }
5194 }
5195
5196 }
5197}
5198
5199void sRef_setAllocatedComplete (sRef s, fileloc loc)
5200{
5201 sRef_innerAliasSetComplete (sRef_setAllocated, s, loc);
5202}
5203
5204static void sRef_setAllocatedShallow (sRef s, fileloc loc)
5205{
28bf4b0b 5206 sRef_checkMutable (s);
5207
616915dd 5208 if (sRef_isValid (s))
5209 {
5210 if (s->defstate == SS_DEAD || s->defstate == SS_UNDEFINED)
5211 {
5212 s->defstate = SS_ALLOCATED;
5213
5214 if (fileloc_isDefined (loc))
5215 {
28bf4b0b 5216 s->definfo = stateInfo_updateLoc (s->definfo, loc);
616915dd 5217 }
5218 }
5219 }
5220}
5221
5222void sRef_setAllocatedShallowComplete (sRef s, fileloc loc)
5223{
5224 sRef_innerAliasSetComplete (sRef_setAllocatedShallow, s, loc);
5225}
5226
5227void sRef_setAllocated (sRef s, fileloc loc)
5228{
28bf4b0b 5229 sRef_checkMutable (s);
616915dd 5230 sRef_setStateAux (s, SS_ALLOCATED, loc);
28bf4b0b 5231}
616915dd 5232
5233void sRef_setPartial (sRef s, fileloc loc)
5234{
28bf4b0b 5235 sRef_checkMutable (s);
616915dd 5236 sRef_setStateAux (s, SS_PARTIAL, loc);
28bf4b0b 5237}
616915dd 5238
5239void sRef_setShared (sRef s, fileloc loc)
5240{
28bf4b0b 5241 sRef_checkMutable (s);
5242
616915dd 5243 if (sRef_isValid (s))
5244 {
5245 if (s->aliaskind != AK_SHARED && fileloc_isDefined (loc))
5246 {
28bf4b0b 5247 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, loc);
616915dd 5248 }
5249
5250 s->aliaskind = AK_SHARED;
5251 /* don't! sRef_clearDerived (s); */
5252 }
5253}
5254
28bf4b0b 5255void sRef_setLastReference (sRef s, /*@exposed@*/ sRef ref, fileloc loc)
616915dd 5256{
28bf4b0b 5257 sRef_checkMutable (s);
5258
616915dd 5259 if (sRef_isValid (s))
5260 {
5261 s->aliaskind = sRef_getAliasKind (ref);
28bf4b0b 5262 s->aliasinfo = stateInfo_updateRefLoc (s->aliasinfo, ref, loc);
616915dd 5263 }
5264}
5265
5266static
5267void sRef_setNullStateAux (/*@notnull@*/ sRef s, nstate ns, fileloc loc)
5268{
28bf4b0b 5269 DPRINTF (("Set null state: %s / %s", sRef_unparse (s), nstate_unparse (ns)));
5270 sRef_checkMutable (s);
5271 s->nullstate = ns;
5272
5273 if (fileloc_isDefined (loc))
5274 {
5275 s->nullinfo = stateInfo_updateLoc (s->nullinfo, loc);
5276 }
616915dd 5277}
5278
5279void sRef_setNotNull (sRef s, fileloc loc)
5280{
5281 if (sRef_isValid (s))
5282 {
5283 sRef_setNullStateAux (s, NS_NOTNULL, loc);
5284 }
5285}
5286
28bf4b0b 5287void sRef_setNullStateN (sRef s, nstate n)
5288{
5289 sRef_checkMutable (s);
5290 s->nullstate = n;
5291}
5292
616915dd 5293void sRef_setNullState (sRef s, nstate n, fileloc loc)
5294{
5295 if (sRef_isValid (s))
5296 {
5297 sRef_setNullStateAux (s, n, loc);
5298 }
5299}
5300
28bf4b0b 5301void sRef_setNullTerminatedStateInnerComplete (sRef s, struct s_bbufinfo b, /*@unused@*/ fileloc loc) {
616915dd 5302
5303 switch (b.bufstate) {
5304 case BB_NULLTERMINATED:
5305 sRef_setNullTerminatedState (s);
5306 sRef_setLen (s, b.len);
5307 break;
5308 case BB_POSSIBLYNULLTERMINATED:
5309 sRef_setPossiblyNullTerminatedState(s);
5310 break;
5311 case BB_NOTNULLTERMINATED:
5312 sRef_setNotNullTerminatedState (s);
5313 break;
5314 }
5315 sRef_setSize (s, b.size);
5316
5317 /* PL: TO BE DONE : Aliases are not modified right now, have to be similar to
5318 * setNullStateInnerComplete.
5319 */
5320}
5321
5322void sRef_setNullStateInnerComplete (sRef s, nstate n, fileloc loc)
5323{
28bf4b0b 5324 DPRINTF (("Set null state: %s", nstate_unparse (n)));
5325
616915dd 5326 sRef_setNullState (s, n, loc);
28bf4b0b 5327
616915dd 5328 switch (n)
5329 {
5330 case NS_POSNULL:
5331 sRef_innerAliasSetComplete (sRef_setPosNull, s, loc);
5332 break;
5333 case NS_DEFNULL:
5334 sRef_innerAliasSetComplete (sRef_setDefNull, s, loc);
5335 break;
5336 case NS_UNKNOWN:
5337 sRef_innerAliasSetComplete (sRef_setNullUnknown, s, loc);
5338 break;
5339 case NS_NOTNULL:
5340 sRef_innerAliasSetComplete (sRef_setNotNull, s, loc);
5341 break;
5342 case NS_MNOTNULL:
5343 sRef_innerAliasSetComplete (sRef_setNotNull, s, loc);
5344 break;
5345 case NS_RELNULL:
5346 sRef_innerAliasSetComplete (sRef_setNullUnknown, s, loc);
5347 break;
5348 case NS_CONSTNULL:
5349 sRef_innerAliasSetComplete (sRef_setDefNull, s, loc);
5350 break;
5351 case NS_ABSNULL:
5352 sRef_innerAliasSetComplete (sRef_setNullUnknown, s, loc);
5353 break;
5354 case NS_ERROR:
5355 sRef_innerAliasSetComplete (sRef_setNullErrorLoc, s, loc);
5356 break;
5357 }
5358}
5359
5360void sRef_setPosNull (sRef s, fileloc loc)
5361{
5362 if (sRef_isValid (s))
5363 {
5364 sRef_setNullStateAux (s, NS_POSNULL, loc);
5365 }
5366}
5367
5368void sRef_setDefNull (sRef s, fileloc loc)
5369{
5370 if (sRef_isValid (s))
5371 {
5372 sRef_setNullStateAux (s, NS_DEFNULL, loc);
5373 }
5374}
5375
5376void sRef_setNullUnknown (sRef s, fileloc loc)
5377{
5378 if (sRef_isValid (s))
5379 {
5380 sRef_setNullStateAux (s, NS_UNKNOWN, loc);
5381 }
5382}
5383
5384void sRef_setNullError (sRef s)
5385{
5386 if (sRef_isValid (s))
5387 {
5388 sRef_setNullStateAux (s, NS_UNKNOWN, fileloc_undefined);
5389 }
5390}
5391
5392void sRef_setNullErrorLoc (sRef s, /*@unused@*/ fileloc loc)
5393{
5394 sRef_setNullError (s);
5395}
5396
5397void sRef_setOnly (sRef s, fileloc loc)
5398{
28bf4b0b 5399 sRef_checkMutable (s);
5400
616915dd 5401 if (sRef_isValid (s) && s->aliaskind != AK_ONLY)
5402 {
5403 s->aliaskind = AK_ONLY;
28bf4b0b 5404 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, loc);
616915dd 5405 }
5406}
5407
5408void sRef_setDependent (sRef s, fileloc loc)
5409{
28bf4b0b 5410 sRef_checkMutable (s);
5411
616915dd 5412 if (sRef_isValid (s) && !sRef_isConst (s) && (s->aliaskind != AK_DEPENDENT))
5413 {
28bf4b0b 5414 DPRINTF (("Setting dependent: %s", sRef_unparseFull (s)));
616915dd 5415 s->aliaskind = AK_DEPENDENT;
28bf4b0b 5416 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, loc);
5417 }
616915dd 5418}
5419
5420void sRef_setOwned (sRef s, fileloc loc)
5421{
28bf4b0b 5422 sRef_checkMutable (s);
5423
616915dd 5424 if (sRef_isValid (s) && !sRef_isConst (s) && (s->aliaskind != AK_OWNED))
5425 {
5426 s->aliaskind = AK_OWNED;
28bf4b0b 5427 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, loc);
5428 }
616915dd 5429}
5430
5431void sRef_setKept (sRef s, fileloc loc)
5432{
28bf4b0b 5433 sRef_checkMutable (s);
5434
616915dd 5435 if (sRef_isValid (s) && !sRef_isConst (s) && (s->aliaskind != AK_KEPT))
5436 {
5437 sRef base = sRef_getBaseSafe (s);
5438
5439 while (sRef_isValid (base) && sRef_isKnown (base))
5440 {
5441 if (base->defstate == SS_DEFINED)
5442 {
5443 base->defstate = SS_PDEFINED;
5444 base = sRef_getBaseSafe (base);
5445 }
5446 else
5447 {
5448 break;
5449 }
5450
5451 }
5452
5453 s->aliaskind = AK_KEPT;
28bf4b0b 5454 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, loc);
5455 }
616915dd 5456}
5457
5458static void sRef_setKeptAux (sRef s, fileloc loc)
5459{
5460 if (!sRef_isShared (s))
5461 {
5462 sRef_setKept (s, loc);
5463 }
5464}
5465
5466static void sRef_setDependentAux (sRef s, fileloc loc)
5467{
5468 if (!sRef_isShared (s))
5469 {
5470 sRef_setDependent (s, loc);
5471 }
5472}
5473
5474void sRef_setKeptComplete (sRef s, fileloc loc)
5475{
5476 sRef_aliasSetComplete (sRef_setKeptAux, s, loc);
5477}
5478
5479void sRef_setDependentComplete (sRef s, fileloc loc)
5480{
5481 sRef_aliasSetComplete (sRef_setDependentAux, s, loc);
5482}
5483
5484void sRef_setFresh (sRef s, fileloc loc)
5485{
28bf4b0b 5486 sRef_checkMutable (s);
5487
616915dd 5488 if (sRef_isValid (s))
5489 {
5490 s->aliaskind = AK_FRESH;
28bf4b0b 5491 s->aliasinfo = stateInfo_updateLoc (s->aliasinfo, loc);
616915dd 5492 }
5493}
5494
5495void sRef_kill (sRef s, fileloc loc)
5496{
5497 DPRINTF (("Kill: %s", sRef_unparseFull (s)));
28bf4b0b 5498 sRef_checkMutable (s);
616915dd 5499
5500 if (sRef_isValid (s) && !sRef_isShared (s) && !sRef_isConst (s))
5501 {
5502 sRef base = sRef_getBaseSafe (s);
5503
5504 while (sRef_isValid (base) && sRef_isKnown (base))
5505 {
5506 if (base->defstate == SS_DEFINED)
5507 {
5508 base->defstate = SS_PDEFINED;
5509 base = sRef_getBaseSafe (base);
5510 }
5511 else
5512 {
5513 break;
5514 }
616915dd 5515 }
5516
5517 s->aliaskind = s->oaliaskind;
5518 s->defstate = SS_DEAD;
28bf4b0b 5519 s->definfo = stateInfo_updateLoc (s->definfo, loc);
616915dd 5520
5521 sRef_clearDerived (s);
5522 }
5523}
5524
5525void sRef_maybeKill (sRef s, fileloc loc)
5526{
28bf4b0b 5527 sRef_checkMutable (s);
5528
616915dd 5529 if (sRef_isValid (s))
5530 {
5531 sRef base = sRef_getBaseSafe (s);
5532
5533
5534 while (sRef_isValid (base) && sRef_isKnown (base))
5535 {
5536 if (base->defstate == SS_DEFINED || base->defstate == SS_RELDEF)
5537 {
5538 base->defstate = SS_PDEFINED;
28bf4b0b 5539 base = sRef_getBaseSafe (base);
616915dd 5540 }
5541 else
5542 {
28bf4b0b 5543 break;
616915dd 5544 }
5545
5546 }
5547
5548 s->aliaskind = s->oaliaskind;
5549 s->defstate = SS_HOFFA;
28bf4b0b 5550 s->definfo = stateInfo_updateLoc (s->definfo, loc);
616915dd 5551 sRef_clearDerived (s);
5552 }
5553
5554 }
5555
5556/*
5557** just for type checking...
5558*/
5559
5560static void sRef_killAux (sRef s, fileloc loc)
5561{
5562 if (sRef_isValid (s) && !sRef_isShared (s))
5563 {
5564 if (sRef_isUnknownArrayFetch (s))
5565 {
5566 sRef_maybeKill (s, loc);
5567 }
5568 else
5569 {
28bf4b0b 5570 sRef_kill (s, loc);
5571 }
616915dd 5572 }
5573}
5574
5575/*
5576** kills s and all aliases to s
5577*/
5578
5579void sRef_killComplete (sRef s, fileloc loc)
5580{
5581 DPRINTF (("Kill complete: %s", sRef_unparseFull (s)));
5582 sRef_aliasSetComplete (sRef_killAux, s, loc);
5583}
5584
5585static bool sRef_equivalent (sRef s1, sRef s2)
5586{
5587 return (sRef_compare (s1, s2) == 0);
5588}
5589
5590/*
5591** returns an sRef that will not be free'd on function exit.
5592*/
5593
5594/*@only@*/ sRef sRef_saveCopy (sRef s)
5595{
5596 sRef ret;
5597
5598 if (sRef_isValid (s))
5599 {
5600 bool old = inFunction;
5601
5602 /*
5603 ** Exit the function scope, so this sRef is not
5604 ** stored in the deallocation table.
5605 */
5606
5607 inFunction = FALSE;
5608 DPRINTF (("Copying sref: %s", sRef_unparseFull(s)));
5609 ret = sRef_copy (s);
5610 DPRINTF (("Copying ===>: %s", sRef_unparseFull(ret)));
5611 inFunction = old;
5612 }
5613 else
5614 {
5615 ret = sRef_undefined;
5616 }
5617
5618 /*@-dependenttrans@*/
5619 return ret;
5620 /*@=dependenttrans@*/
5621}
5622
5623sRef sRef_copy (sRef s)
5624{
28bf4b0b 5625 if (sRef_isKindSpecial (s) && !sRef_isGlobalMarker (s))
616915dd 5626 {
5627 /*@-retalias@*/
28bf4b0b 5628 return s; /* don't copy specials (except for global markers) */
616915dd 5629 /*@=retalias@*/
5630 }
28bf4b0b 5631
616915dd 5632 if (sRef_isValid (s))
5633 {
5634 sRef t = sRef_alloc ();
5635
28bf4b0b 5636 DPRINTF (("Copying: [%p] %s", s, sRef_unparse (s)));
5637 DPRINTF (("Full: %s", sRef_unparseFull (s)));
5638
616915dd 5639 t->kind = s->kind;
5640 t->safe = s->safe;
5641 t->modified = s->modified;
28bf4b0b 5642 t->immut = FALSE; /* Note mutability is not copied. */
616915dd 5643 t->type = s->type;
5644
28bf4b0b 5645 t->info = sinfo_copy (s);
616915dd 5646 t->defstate = s->defstate;
616915dd 5647 t->nullstate = s->nullstate;
5648
5649 /* start modifications */
5650 t->bufinfo.bufstate = s->bufinfo.bufstate;
5651 t->bufinfo.len = s->bufinfo.len;
5652 t->bufinfo.size = s->bufinfo.size;
5653 /* end modifications */
5654
5655 t->aliaskind = s->aliaskind;
5656 t->oaliaskind = s->oaliaskind;
5657
5658 t->expkind = s->expkind;
5659 t->oexpkind = s->oexpkind;
5660
28bf4b0b 5661 t->nullinfo = stateInfo_copy (s->nullinfo);
5662 t->aliasinfo = stateInfo_copy (s->aliasinfo);
5663 t->definfo = stateInfo_copy (s->definfo);
5664 t->expinfo = stateInfo_copy (s->expinfo);
616915dd 5665
5666 t->deriv = sRefSet_newDeepCopy (s->deriv);
28bf4b0b 5667 t->state = valueTable_copy (s->state);
5668
5669 DPRINTF (("Made copy: [%p] %s", t, sRef_unparse (t)));
616915dd 5670 return t;
5671 }
5672 else
5673 {
5674 return sRef_undefined;
5675 }
5676}
5677
5678/*@notfunction@*/
5679# define PREDTEST(func,s) \
5680 do { if (sRef_isInvalid (s)) { return FALSE; } \
5681 else { if (sRef_isConj (s)) \
5682 { return (func (sRef_getConjA (s)) \
5683 || func (sRef_getConjB (s))); }}} while (FALSE);
5684
5685bool sRef_isAddress (sRef s)
5686{
5687 PREDTEST (sRef_isAddress, s);
5688 return (s->kind == SK_ADR);
5689}
5690
5691/*
5692** pretty weak... maybe a flag should control this.
5693*/
5694
5695bool sRef_isThroughArrayFetch (sRef s)
5696{
5697 if (sRef_isValid (s))
5698 {
5699 sRef tref = s;
5700
5701 do
5702 {
5703 sRef lt;
5704
5705 if (sRef_isArrayFetch (tref))
5706 {
28bf4b0b 5707 return TRUE;
616915dd 5708 }
5709
5710 lt = sRef_getBase (tref);
5711 tref = lt;
5712 } while (sRef_isValid (tref));
5713 }
5714
5715 return FALSE;
5716}
5717
5718bool sRef_isArrayFetch (sRef s)
5719{
5720 PREDTEST (sRef_isArrayFetch, s);
5721 return (s->kind == SK_ARRAYFETCH);
5722}
5723
5724bool sRef_isMacroParamRef (sRef s)
5725{
5726 if (context_inMacro () && sRef_isCvar (s))
5727 {
5728 uentry ue = sRef_getUentry (s);
5729 cstring pname = makeParam (uentry_rawName (ue));
5730 uentry mac = usymtab_lookupSafe (pname);
5731
5732 cstring_free (pname);
5733 return (uentry_isValid (mac));
5734 }
5735
5736 return FALSE;
5737}
5738
5739bool sRef_isCvar (sRef s)
5740{
5741 PREDTEST (sRef_isCvar, s);
5742 return (s->kind == SK_CVAR);
5743}
5744
5745bool sRef_isConst (sRef s)
5746{
5747 PREDTEST (sRef_isConst, s);
5748 return (s->kind == SK_CONST);
5749}
5750
5751bool sRef_isObject (sRef s)
5752{
5753 PREDTEST (sRef_isObject, s);
5754 return (s->kind == SK_OBJECT);
5755}
5756
5757bool sRef_isExternal (sRef s)
5758{
5759 PREDTEST (sRef_isExternal, s);
5760 return (s->kind == SK_EXTERNAL);
5761}
5762
5763static bool sRef_isDerived (sRef s)
5764{
5765 PREDTEST (sRef_isDerived, s);
5766 return (s->kind == SK_DERIVED);
5767}
5768
5769bool sRef_isField (sRef s)
5770{
5771 PREDTEST (sRef_isField, s);
5772 return (s->kind == SK_FIELD);
5773}
5774
5775static bool sRef_isIndex (sRef s)
5776{
5777 PREDTEST (sRef_isIndex, s);
5778 return (s->kind == SK_ARRAYFETCH);
5779}
5780
5781bool sRef_isAnyParam (sRef s)
5782{
5783 PREDTEST (sRef_isAnyParam, s);
5784 return (s->kind == SK_PARAM);
5785}
5786
5787bool sRef_isParam (sRef s)
5788{
5789 PREDTEST (sRef_isParam, s);
5790 return (s->kind == SK_PARAM);
5791}
5792
5793bool sRef_isDirectParam (sRef s)
5794{
5795 PREDTEST (sRef_isDirectParam, s);
5796
5797 return ((s->kind == SK_CVAR) &&
5798 (s->info->cvar->lexlevel == functionScope) &&
5799 (context_inFunction () &&
5800 (s->info->cvar->index <= uentryList_size (context_getParams ()))));
5801}
5802
5803bool sRef_isPointer (sRef s)
5804{
5805 PREDTEST (sRef_isPointer, s);
5806 return (s->kind == SK_PTR);
5807}
5808
5809/*
5810** returns true if storage referenced by s is visible
5811*/
5812
5813bool sRef_isReference (sRef s)
5814{
5815 PREDTEST (sRef_isReference, s);
5816
28bf4b0b 5817 return (sRef_isPointer (s) || sRef_isIndex (s) || sRef_isFileOrGlobalScope (s)
616915dd 5818 || (sRef_isField (s) && (sRef_isReference (s->info->field->rec))));
5819}
5820
5821bool sRef_isIReference (sRef s)
5822{
5823 return (sRef_isPointer (s) || sRef_isAddress (s) || sRef_isIndex (s)
5824 || sRef_isField (s) || sRef_isArrayFetch (s));
5825}
5826
28bf4b0b 5827bool sRef_isFileOrGlobalScope (sRef s)
616915dd 5828{
5829 return (sRef_isCvar (s) && (s->info->cvar->lexlevel <= fileScope));
5830}
5831
5832bool sRef_isRealGlobal (sRef s)
5833{
5834 return (sRef_isCvar (s) && (s->info->cvar->lexlevel == globScope));
5835}
5836
5837bool sRef_isFileStatic (sRef s)
5838{
5839 return (sRef_isCvar (s) && (s->info->cvar->lexlevel == fileScope));
5840}
5841
5842bool sRef_isAliasCheckedGlobal (sRef s)
5843{
28bf4b0b 5844 if (sRef_isFileOrGlobalScope (s))
616915dd 5845 {
5846 uentry ue = sRef_getUentry (s);
5847
5848 return context_checkAliasGlob (ue);
5849 }
5850 else
5851 {
5852 return FALSE;
5853 }
5854}
5855
5856void sRef_free (/*@only@*/ sRef s)
5857{
5858 if (s != sRef_undefined && s->kind != SK_SPECIAL)
5859 {
28bf4b0b 5860 DPRINTF (("Free sref: [%p]", s));
5861
5862 sRef_checkValid (s);
5863
5864 stateInfo_free (s->expinfo);
5865 stateInfo_free (s->aliasinfo);
5866 stateInfo_free (s->definfo);
5867 stateInfo_free (s->nullinfo);
5868
616915dd 5869 sRefSet_free (s->deriv);
5870 s->deriv = sRefSet_undefined;
28bf4b0b 5871
5872 /*@i43@*/ /* valueTable_free (s->state); */
616915dd 5873 sinfo_free (s);
5874
28bf4b0b 5875 /*@i32@*/ sfree (s);
616915dd 5876 }
5877}
5878
5879void sRef_setType (sRef s, ctype t)
5880{
28bf4b0b 5881 sRef_checkMutable (s);
5882
616915dd 5883 if (sRef_isValid (s))
5884 {
5885 s->type = t;
5886 }
5887}
5888
5889void sRef_setTypeFull (sRef s, ctype t)
5890{
28bf4b0b 5891 sRef_checkMutable (s);
5892
616915dd 5893 if (sRef_isValid (s))
5894 {
5895 s->type = t;
5896
5897 sRefSet_allElements (s->deriv, current)
5898 {
5899 sRef_setTypeFull (current, ctype_unknown);
5900 } end_sRefSet_allElements ;
5901 }
5902}
5903
5904/*@exposed@*/ sRef
28bf4b0b 5905 sRef_buildField (/*@exposed@*/ sRef rec, /*@dependent@*/ cstring f)
616915dd 5906{
5907 return (sRef_buildNCField (rec, f));
5908}
5909
5910static /*@exposed@*/ sRef
5911sRef_findDerivedField (/*@notnull@*/ sRef rec, cstring f)
5912{
5913 sRefSet_allElements (rec->deriv, sr)
5914 {
5915 if (sRef_isValid (sr))
5916 {
28bf4b0b 5917 if (sr->info != NULL)
616915dd 5918 {
28bf4b0b 5919 if (sr->kind == SK_FIELD && cstring_equal (sr->info->field->field, f))
5920 {
5921 return sr;
5922 }
616915dd 5923 }
5924 }
5925 } end_sRefSet_allElements;
5926
5927 return sRef_undefined;
5928}
5929
28bf4b0b 5930/*@dependent@*/ /*@observer@*/ sRefSet sRef_derivedFields (/*@temp@*/ sRef rec)
616915dd 5931{
5932 if (sRef_isValid (rec))
5933 {
5934 sRefSet ret;
5935 ret = rec->deriv;
5936 return (ret);
5937 }
5938 else
5939 {
5940 return (sRefSet_undefined);
5941 }
5942}
5943
5944static /*@exposed@*/ sRef
5945 sRef_findDerivedPointer (sRef s)
5946{
5947 if (sRef_isValid (s))
5948 {
5949 sRefSet_realElements (s->deriv, sr)
5950 {
5951 if (sRef_isValid (sr) && sr->kind == SK_PTR)
5952 {
5953 return sr;
5954 }
5955 } end_sRefSet_realElements;
5956 }
5957
5958 return sRef_undefined;
5959}
5960
5961bool
5962sRef_isUnknownArrayFetch (sRef s)
5963{
5964 return (sRef_isValid (s)
5965 && s->kind == SK_ARRAYFETCH
5966 && !s->info->arrayfetch->indknown);
5967}
5968
5969static /*@exposed@*/ sRef
5970sRef_findDerivedArrayFetch (/*@notnull@*/ sRef s, bool isknown, int idx, bool dead)
5971{
5972
5973 if (isknown)
5974 {
5975 sRefSet_realElements (s->deriv, sr)
5976 {
5977 if (sRef_isValid (sr)
5978 && sr->kind == SK_ARRAYFETCH
5979 && sr->info->arrayfetch->indknown
5980 && (sr->info->arrayfetch->ind == idx))
5981 {
5982 return sr;
5983 }
5984 } end_sRefSet_realElements;
5985 }
5986 else
5987 {
5988 sRefSet_realElements (s->deriv, sr)
5989 {
5990 if (sRef_isValid (sr)
5991 && sr->kind == SK_ARRAYFETCH
5992 && (!sr->info->arrayfetch->indknown
5993 || (sr->info->arrayfetch->indknown &&
5994 sr->info->arrayfetch->ind == 0)))
5995 {
5996 if (sRef_isDead (sr) || sRef_isKept (sr))
5997 {
5998 if (dead || context_getFlag (FLG_STRICTUSERELEASED))
5999 {
6000 return sr;
6001 }
6002 }
6003 else
6004 {
6005 return sr;
6006 }
6007 }
6008 } end_sRefSet_realElements;
6009 }
6010
6011 return sRef_undefined;
6012}
6013
6014static /*@exposed@*/ sRef
6015sRef_buildNCField (/*@exposed@*/ sRef rec, /*@exposed@*/ cstring f)
6016{
6017 sRef s;
6018
28bf4b0b 6019 DPRINTF (("Build nc field: %s / %s",
6020 sRef_unparseFull (rec), f));
6021
616915dd 6022 if (sRef_isInvalid (rec))
6023 {
6024 return sRef_undefined;
6025 }
6026
6027 /*
6028 ** check if the field already has been referenced
6029 */
6030
6031 s = sRef_findDerivedField (rec, f);
6032
6033 if (sRef_isValid (s))
6034 {
28bf4b0b 6035 return s;
616915dd 6036 }
6037 else
6038 {
6039 ctype ct = ctype_realType (rec->type);
6040
28bf4b0b 6041 DPRINTF (("Field of: %s", sRef_unparse (rec)));
6042
6043 s = sRef_newRef ();
616915dd 6044 s->kind = SK_FIELD;
6045 s->info = (sinfo) dmalloc (sizeof (*s->info));
6046 s->info->field = (fldinfo) dmalloc (sizeof (*s->info->field));
28bf4b0b 6047 s->info->field->rec = rec; /* sRef_copy (rec); */ /*@i32@*/
616915dd 6048 s->info->field->field = f; /* doesn't copy f */
6049
616915dd 6050 if (ctype_isKnown (ct) && ctype_isSU (ct))
6051 {
6052 uentry ue = uentryList_lookupField (ctype_getFields (ct), f);
6053
6054 if (!uentry_isUndefined (ue))
6055 {
28bf4b0b 6056 DPRINTF (("lookup: %s for %s", uentry_unparseFull (ue),
6057 ctype_unparse (ct)));
6058
616915dd 6059 s->type = uentry_getType (ue);
6060
6061 if (ctype_isMutable (s->type)
6062 && rec->aliaskind != AK_STACK
6063 && !alkind_isStatic (rec->aliaskind))
6064 {
6065 s->aliaskind = rec->aliaskind;
6066 }
6067 else
6068 {
6069 s->aliaskind = AK_UNKNOWN;
6070 }
6071
6072 if (sRef_isStateDefined (rec) || sRef_isStateUnknown (rec)
6073 || sRef_isPdefined (rec))
6074 {
28bf4b0b 6075 sRef_setStateFromUentry (s, ue);
6076 }
616915dd 6077 else
6078 {
6079 sRef_setPartsFromUentry (s, ue);
28bf4b0b 6080 }
6081
616915dd 6082 s->oaliaskind = s->aliaskind;
6083 s->oexpkind = s->expkind;
28bf4b0b 6084
6085 DPRINTF (("sref: %s", sRef_unparseFull (s)));
616915dd 6086 }
6087 else
6088 {
6089 /*
6090 Never report this as an error. It can happen whenever there
6091 is casting involved.
6092
6093 if (report)
6094 {
6095 llcontbug (message ("buildNCField --- no field %s: %q / %s",
6096 f, sRef_unparse (s), ctype_unparse (ct)));
6097 }
6098 */
6099
6100 return sRef_undefined;
6101 }
6102 }
6103
6104 if (rec->defstate == SS_DEFINED
6105 && (s->defstate == SS_UNDEFINED || s->defstate == SS_UNKNOWN))
6106 {
6107 s->defstate = SS_DEFINED;
6108 }
6109 else if (rec->defstate == SS_PARTIAL)
6110 {
6111 s->defstate = SS_PARTIAL;
6112 }
6113 else if (rec->defstate == SS_ALLOCATED)
6114 {
6115 if (ctype_isStackAllocated (ct) && ctype_isStackAllocated (s->type))
6116 {
6117 s->defstate = SS_ALLOCATED;
6118 }
6119 else
6120 {
6121 s->defstate = SS_UNDEFINED;
6122 }
6123 }
6124 else if (s->defstate == SS_UNKNOWN)
6125 {
6126 s->defstate = rec->defstate;
6127 }
6128 else
6129 {
6130 ; /* no change */
6131 }
6132
6133 if (s->defstate == SS_UNDEFINED)
6134 {
6135 ctype rt = ctype_realType (s->type);
6136
6137 if (ctype_isArray (rt) || ctype_isSU (rt))
6138 {
28bf4b0b 6139 s->defstate = SS_ALLOCATED;
616915dd 6140 }
6141 }
6142
6143 sRef_addDeriv (rec, s);
28bf4b0b 6144 DPRINTF (("Add deriv: %s", sRef_unparseFull (rec)));
616915dd 6145
6146 if (ctype_isInt (s->type) && cstring_equal (f, REFSNAME))
6147 {
6148 s->aliaskind = AK_REFS;
6149 s->oaliaskind = AK_REFS;
6150 }
6151
28bf4b0b 6152 DPRINTF (("Build field ==> %s", sRef_unparseFull (s)));
6153 return s;
616915dd 6154 }
6155}
6156
6157bool
6158sRef_isStackAllocated (sRef s)
6159{
6160 return (sRef_isValid(s)
6161 && s->defstate == SS_ALLOCATED && ctype_isStackAllocated (s->type));
6162}
6163
6164static
6165void sRef_setArrayFetchState (/*@notnull@*/ /*@exposed@*/ sRef s,
6166 /*@notnull@*/ /*@exposed@*/ sRef arr)
6167{
28bf4b0b 6168 sRef_checkMutable (s);
6169
616915dd 6170 if (ctype_isRealAP (arr->type))
6171 {
6172 s->type = ctype_baseArrayPtr (arr->type);
6173 }
6174
6175 /* a hack, methinks... makeArrayFetch (&a[0]) ==> a[] */
6176 if (sRef_isAddress (arr))
6177 {
6178 sRef t = arr->info->ref;
6179
6180 if (sRef_isArrayFetch (t))
6181 {
6182 s->info->arrayfetch->arr = t->info->arrayfetch->arr;
6183 }
6184 }
6185 else if (ctype_isRealPointer (arr->type))
6186 {
6187 sRef sp = sRef_findDerivedPointer (arr);
616915dd 6188
6189 if (sRef_isValid (sp))
6190 {
6191
6192 if (ctype_isMutable (s->type))
6193 {
6194 sRef_setExKind (s, sRef_getExKind (sp), fileloc_undefined);
616915dd 6195
6196 s->aliaskind = sp->aliaskind;
6197 }
6198
6199 s->defstate = sp->defstate;
6200
6201 if (s->defstate == SS_DEFINED)
6202 {
6203 if (!context_getFlag (FLG_STRICTDESTROY))
6204 {
6205 s->defstate = SS_PARTIAL;
6206 }
6207 }
6208
28bf4b0b 6209 sRef_setNullStateN (s, sRef_getNullState (sp));
616915dd 6210 }
6211 else
6212 {
6213 if (arr->defstate == SS_UNDEFINED)
6214 {
6215 s->defstate = SS_UNUSEABLE;
6216 }
6217 else if ((arr->defstate == SS_ALLOCATED) && !ctype_isSU (s->type))
6218 {
6219 s->defstate = SS_UNDEFINED;
6220 }
6221 else
6222 {
6223 if (!context_getFlag (FLG_STRICTDESTROY))
6224 {
6225 s->defstate = SS_PARTIAL;
6226 }
6227 else
6228 {
6229 s->defstate = SS_DEFINED;
6230 }
6231
6232 /*
6233 ** Very weak checking for array elements.
6234 ** Was:
6235 ** s->defstate = arr->defstate;
6236 */
6237 }
6238
6239 sRef_setExKind (s, sRef_getExKind (arr), g_currentloc);
6240
6241 if (arr->aliaskind == AK_LOCAL || arr->aliaskind == AK_FRESH)
6242 {
6243 s->aliaskind = AK_LOCAL;
6244 }
6245 else
6246 {
6247 s->aliaskind = AK_UNKNOWN;
6248 }
6249
6250 sRef_setTypeState (s);
6251 }
6252 }
6253 else
6254 {
6255 if (arr->defstate == SS_DEFINED)
6256 {
6257 /*
6258 ** Very weak checking for array elements.
6259 ** Was:
6260 ** s->defstate = arr->defstate;
6261 */
6262
6263 if (context_getFlag (FLG_STRICTDESTROY))
6264 {
6265 s->defstate = SS_DEFINED;
6266 }
6267 else
6268 {
6269 s->defstate = SS_PARTIAL;
6270 }
6271 }
6272 else if (arr->defstate == SS_ALLOCATED)
6273 {
6274 if (ctype_isRealArray (s->type))
6275 {
6276 s->defstate = SS_ALLOCATED;
6277 }
6278 else
6279 {
6280 if (!s->info->arrayfetch->indknown)
6281 {
6282 /*
6283 ** is index is unknown, elements is defined or
6284 ** allocated is any element is!
6285 */
6286
6287 s->defstate = SS_UNDEFINED;
6288
6289 sRefSet_allElements (arr->deriv, sr)
6290 {
6291 if (sRef_isValid (sr))
6292 {
6293 if (sr->defstate == SS_ALLOCATED)
6294 {
6295 s->defstate = SS_ALLOCATED;
6296 }
6297 else
6298 {
6299 if (sr->defstate == SS_DEFINED)
6300 {
6301 if (context_getFlag (FLG_STRICTDESTROY))
6302 {
6303 s->defstate = SS_DEFINED;
6304 }
6305 else
6306 {
6307 s->defstate = SS_PARTIAL;
6308 }
6309
6310 break;
6311 }
6312 }
6313 }
6314 } end_sRefSet_allElements;
6315
6316 }
6317 else
6318 {
6319 s->defstate = SS_UNDEFINED;
6320 }
6321 }
6322 }
6323 else
6324 {
6325 s->defstate = arr->defstate;
6326 }
6327
6328
6329 /*
6330 ** kludgey way to guess where aliaskind applies
6331 */
6332
6333 if (ctype_isMutable (s->type)
6334 && !ctype_isPointer (arr->type)
6335 && !alkind_isStatic (arr->aliaskind)
6336 && !alkind_isStack (arr->aliaskind)) /* evs - 2000-06-20: don't pass stack allocation to members */
6337 {
6338 s->aliaskind = arr->aliaskind;
6339 }
6340 else
6341 {
6342 s->aliaskind = AK_UNKNOWN;
6343 }
6344
6345 sRef_setTypeState (s);
6346 }
6347
6348 if (sRef_isObserver (arr))
6349 {
6350 s->expkind = XO_OBSERVER;
6351 }
6352}
6353
6354/*@exposed@*/ sRef sRef_buildArrayFetch (/*@exposed@*/ sRef arr)
6355{
6356 sRef s;
6357
6358 if (!sRef_isValid (arr)) {
6359 /*@-nullret@*/ return arr /*@=nullret@*/;
6360 }
6361
6362 if (ctype_isRealPointer (arr->type))
6363 {
6364 (void) sRef_buildPointer (arr); /* do this to define arr! */
6365 }
6366
6367 s = sRef_findDerivedArrayFetch (arr, FALSE, 0, FALSE);
6368
6369 if (sRef_isValid (s))
6370 {
6371 sRef_setExKind (s, sRef_getExKind (arr), g_currentloc);
6372 return s;
6373 }
6374 else
6375 {
28bf4b0b 6376 s = sRef_newRef ();
616915dd 6377
6378 s->kind = SK_ARRAYFETCH;
6379 s->info = (sinfo) dmalloc (sizeof (*s->info));
6380 s->info->arrayfetch = (ainfo) dmalloc (sizeof (*s->info->arrayfetch));
6381 s->info->arrayfetch->indknown = FALSE;
6382 s->info->arrayfetch->ind = 0;
28bf4b0b 6383 s->info->arrayfetch->arr = arr; /* sRef_copy (arr); */ /*@i32@*/
616915dd 6384 sRef_setArrayFetchState (s, arr);
6385 s->oaliaskind = s->aliaskind;
6386 s->oexpkind = s->expkind;
6387
6388 if (!context_inProtectVars ())
6389 {
6390 sRef_addDeriv (arr, s);
6391 }
6392
28bf4b0b 6393 llassert (valueTable_isUndefined (s->state));
6394 s->state = context_createValueTable (s);
6395
616915dd 6396 return (s);
6397 }
6398}
6399
6400/*@exposed@*/ sRef
6401 sRef_buildArrayFetchKnown (/*@exposed@*/ sRef arr, int i)
6402{
6403 sRef s;
6404
6405 if (!sRef_isValid (arr)) {
6406 /*@-nullret@*/ return arr /*@=nullret@*/;
6407 }
6408
6409 if (ctype_isRealPointer (arr->type))
6410 {
6411 (void) sRef_buildPointer (arr); /* do this to define arr! */
6412 }
6413
6414 s = sRef_findDerivedArrayFetch (arr, TRUE, i, FALSE);
6415
6416 if (sRef_isValid (s))
6417 {
6418 sRef_setExKind (s, sRef_getExKind (arr), g_currentloc);
6419 return s;
6420 }
6421 else
6422 {
28bf4b0b 6423 s = sRef_newRef ();
616915dd 6424
6425 s->kind = SK_ARRAYFETCH;
6426 s->info = (sinfo) dmalloc (sizeof (*s->info));
6427 s->info->arrayfetch = (ainfo) dmalloc (sizeof (*s->info->arrayfetch));
28bf4b0b 6428 s->info->arrayfetch->arr = arr; /* sRef_copy (arr); */ /*@i32@*/
616915dd 6429 s->info->arrayfetch->indknown = TRUE;
6430 s->info->arrayfetch->ind = i;
6431
6432 sRef_setArrayFetchState (s, arr);
6433
6434 s->oaliaskind = s->aliaskind;
6435 s->oexpkind = s->expkind;
6436 sRef_addDeriv (arr, s);
6437
28bf4b0b 6438 llassert (valueTable_isUndefined (s->state));
6439 s->state = context_createValueTable (s);
6440
616915dd 6441 return (s);
6442 }
6443}
6444
6445/*
6446** sets everything except for defstate
6447*/
6448
6449static void
6450sRef_setPartsFromUentry (sRef s, uentry ue)
28bf4b0b 6451{
6452 sRef uref = uentry_getSref (ue);
6453
616915dd 6454 llassert (sRef_isValid (s));
6455
6456 s->aliaskind = alkind_derive (s->aliaskind, uentry_getAliasKind (ue));
6457 s->oaliaskind = s->aliaskind;
6458
6459 if (s->expkind == XO_UNKNOWN)
6460 {
6461 s->expkind = uentry_getExpKind (ue);
6462 }
28bf4b0b 6463
616915dd 6464 s->oexpkind = s->expkind;
28bf4b0b 6465
6466 if (sRef_getNullState (s) == NS_UNKNOWN)
6467 {
6468 DPRINTF (("Setting null state!"));
6469 sRef_setNullStateN (s, sRef_getNullState (uentry_getSref (ue)));
6470 }
6471 else
616915dd 6472 {
28bf4b0b 6473 DPRINTF (("Skipping null null state!"));
616915dd 6474 }
6475
28bf4b0b 6476 if (s->aliaskind == AK_IMPONLY && (sRef_isExposed (s) || sRef_isObserver (s)))
616915dd 6477 {
6478 s->oaliaskind = s->aliaskind = AK_IMPDEPENDENT;
28bf4b0b 6479 }
616915dd 6480
28bf4b0b 6481 if (sRef_isValid (uref))
6482 {
6483 valueTable utable = uref->state;
6484 valueTable_free (s->state);
6485 s->state = valueTable_copy (utable);
6486 }
616915dd 6487}
6488
6489static void
6490sRef_setStateFromAbstractUentry (sRef s, uentry ue)
6491{
6492 llassert (sRef_isValid (s));
28bf4b0b 6493 sRef_checkMutable (s);
6494
616915dd 6495 sRef_setPartsFromUentry (s, ue);
6496
6497 s->aliaskind = alkind_derive (s->aliaskind, uentry_getAliasKind (ue));
6498 s->oaliaskind = s->aliaskind;
6499
6500 if (s->expkind == XO_UNKNOWN)
6501 {
6502 s->expkind = uentry_getExpKind (ue);
6503 }
6504
6505 s->oexpkind = s->expkind;
6506}
6507
6508void
6509sRef_setStateFromUentry (sRef s, uentry ue)
6510{
6511 sstate defstate;
6512
28bf4b0b 6513 sRef_checkMutable (s);
616915dd 6514 llassert (sRef_isValid (s));
6515
6516 sRef_setPartsFromUentry (s, ue);
6517
6518 defstate = uentry_getDefState (ue);
6519
6520 if (sstate_isKnown (defstate))
6521 {
6522 s->defstate = defstate;
6523 }
6524 else
6525 {
6526 ;
6527 }
6528}
6529
6530/*@exposed@*/ sRef
6531 sRef_buildPointer (/*@exposed@*/ sRef t)
6532{
6533 DPRINTF (("build pointer: %s", sRef_unparse (t)));
6534
6535 if (sRef_isInvalid (t)) return sRef_undefined;
6536
6537 if (sRef_isAddress (t))
6538 {
6539 DPRINTF (("Return ref: %s", sRef_unparse (t->info->ref)));
6540 return (t->info->ref);
6541 }
6542 else
6543 {
6544 sRef s = sRef_findDerivedPointer (t);
6545
6546 DPRINTF (("find derived: %s", sRef_unparse (s)));
6547
6548 if (sRef_isValid (s))
6549 {
6550
6551 sRef_setExKind (s, sRef_getExKind (t), g_currentloc);
6552 s->oaliaskind = s->aliaskind;
6553 s->oexpkind = s->expkind;
6554
6555 return s;
6556 }
6557 else
6558 {
6559 s = sRef_constructPointerAux (t);
6560
6561 DPRINTF (("construct: %s", sRef_unparse (s)));
6562
6563 if (sRef_isValid (s))
6564 {
6565 sRef_addDeriv (t, s);
6566
6567 s->oaliaskind = s->aliaskind;
6568 s->oexpkind = s->expkind;
6569 }
6570
6571 return s;
6572 }
6573 }
6574}
6575
6576/*@exposed@*/ sRef
28bf4b0b 6577sRef_constructPointer (/*@exposed@*/ sRef t)
616915dd 6578 /*@modifies t@*/
6579{
6580 return sRef_buildPointer (t);
6581}
6582
6583static /*@exposed@*/ sRef sRef_constructDerefAux (sRef t, bool isdead)
6584{
6585 if (sRef_isValid (t))
6586 {
6587 sRef s;
6588
6589 /*
6590 ** if there is a derived t[?], return that. Otherwise, *t.
6591 */
6592
616915dd 6593 s = sRef_findDerivedArrayFetch (t, FALSE, 0, isdead);
6594
6595 if (sRef_isValid (s))
6596 {
28bf4b0b 6597 return s;
616915dd 6598 }
6599 else
6600 {
6601 sRef ret = sRef_constructPointer (t);
6602
6603 /*
6604 ** This is necessary to prevent infinite depth
6605 ** in checking complete destruction.
6606 */
6607
616915dd 6608 if (isdead)
6609 {
6610 /* ret->defstate = SS_UNKNOWN; */
6611 return ret;
6612 }
6613 else
6614 {
6615 return ret;
6616 }
6617 }
6618 }
6619 else
6620 {
6621 return sRef_undefined;
6622 }
6623}
6624
6625sRef sRef_constructDeref (sRef t)
6626{
6627 return sRef_constructDerefAux (t, FALSE);
6628}
6629
6630sRef sRef_constructDeadDeref (sRef t)
6631{
6632 return sRef_constructDerefAux (t, TRUE);
6633}
6634
6635static sRef
6636sRef_constructPointerAux (/*@notnull@*/ /*@exposed@*/ sRef t)
6637{
28bf4b0b 6638 sRef s = sRef_newRef ();
616915dd 6639 ctype rt = t->type;
6640 ctype st;
6641
6642 s->kind = SK_PTR;
6643 s->info = (sinfo) dmalloc (sizeof (*s->info));
28bf4b0b 6644 s->info->ref = t; /* sRef_copy (t); */ /*@i32*/
616915dd 6645
6646 if (ctype_isRealAP (rt))
6647 {
6648 s->type = ctype_baseArrayPtr (rt);
6649 }
6650
28bf4b0b 6651 st = ctype_realType (s->type);
616915dd 6652
6653 if (t->defstate == SS_UNDEFINED)
6654 {
6655 s->defstate = SS_UNUSEABLE;
6656 }
6657 else if ((t->defstate == SS_ALLOCATED) && !ctype_isSU (st))
6658 {
6659 s->defstate = SS_UNDEFINED;
6660 }
6661 else
6662 {
6663 s->defstate = t->defstate;
6664 }
6665
6666 if (t->aliaskind == AK_LOCAL || t->aliaskind == AK_FRESH)
6667 {
6668 s->aliaskind = AK_LOCAL;
6669 }
6670 else
6671 {
6672 s->aliaskind = AK_UNKNOWN;
6673 }
6674
6675 sRef_setExKind (s, sRef_getExKind (t), fileloc_undefined);
6676 sRef_setTypeState (s);
6677
6678 s->oaliaskind = s->aliaskind;
6679 s->oexpkind = s->expkind;
6680
28bf4b0b 6681 llassert (valueTable_isUndefined (s->state));
6682 s->state = context_createValueTable (s);
616915dd 6683 return s;
6684}
6685
6686bool sRef_hasDerived (sRef s)
6687{
6688 return (sRef_isValid (s) && !sRefSet_isEmpty (s->deriv));
6689}
6690
6691void
6692sRef_clearDerived (sRef s)
6693{
6694 if (sRef_isValid (s))
6695 {
28bf4b0b 6696 sRefSet_clear (s->deriv);
616915dd 6697 }
6698}
6699
6700void
6701sRef_clearDerivedComplete (sRef s)
6702{
6703
6704 if (sRef_isValid (s))
6705 {
6706 sRef base = sRef_getBaseSafe (s);
6707
6708 while (sRef_isValid (base))
6709 {
6710 sRefSet_clear (base->deriv);
6711 base = sRef_getBaseSafe (base);
6712 }
6713
6714 sRefSet_clear (s->deriv);
6715 }
6716}
6717
28bf4b0b 6718/*@exposed@*/ sRef sRef_makePointer (/*@exposed@*/ sRef s)
6719 /*@modifies s@*/
616915dd 6720{
6721 sRef res = sRef_buildPointer (s);
6722
6723 DPRINTF (("Res: %s", sRef_unparse (res)));
6724 return res;
6725}
6726
6727/*
6728** &a[] => a (this is for out params)
6729*/
6730
6731/*@exposed@*/ sRef
6732sRef_makeAnyArrayFetch (/*@exposed@*/ sRef arr)
6733{
6734
6735 if (sRef_isAddress (arr))
6736 {
28bf4b0b 6737 return (arr->info->ref);
616915dd 6738 }
6739 else
6740 {
6741 return (sRef_buildArrayFetch (arr));
6742 }
6743}
6744
6745/*@exposed@*/ sRef
28bf4b0b 6746sRef_makeArrayFetch (/*@exposed@*/ sRef arr)
616915dd 6747{
6748 return (sRef_buildArrayFetch (arr));
6749}
6750
6751/*@exposed@*/ sRef
28bf4b0b 6752sRef_makeArrayFetchKnown (/*@exposed@*/ sRef arr, int i)
616915dd 6753{
6754 return (sRef_buildArrayFetchKnown (arr, i));
6755}
6756
6757/*@exposed@*/ sRef
6758sRef_makeField (sRef rec, /*@dependent@*/ cstring f)
6759{
6760 sRef ret;
6761 ret = sRef_buildField (rec, f);
6762 return ret;
6763}
6764
6765/*@exposed@*/ sRef
28bf4b0b 6766sRef_makeNCField (/*@exposed@*/ sRef rec, /*@dependent@*/ cstring f)
616915dd 6767{
28bf4b0b 6768 return (sRef_buildNCField (rec, f));
616915dd 6769}
6770
6771/*@only@*/ cstring
6772sRef_unparseKindName (sRef s)
6773{
6774 cstring result;
6775
6776 if (s == sRef_undefined) return cstring_makeLiteral ("<invalid>");
6777
6778 s = sRef_fixConj (s);
6779
6780 switch (s->kind)
6781 {
6782 case SK_CVAR:
6783 if (sRef_isLocalVar (s))
6784 {
6785 result = cstring_makeLiteral ("Variable");
6786 }
6787 else
6788 {
6789 result = cstring_makeLiteral ("Undef global");
6790 }
6791 break;
6792 case SK_PARAM:
6793 result = cstring_makeLiteral ("Out parameter");
6794 break;
6795 case SK_ARRAYFETCH:
6796 if (sRef_isAnyParam (s->info->arrayfetch->arr))
6797 {
6798 result = cstring_makeLiteral ("Out parameter");
6799 }
6800 else if (sRef_isIndexKnown (s))
6801 {
6802 result = cstring_makeLiteral ("Array element");
6803 }
6804 else
6805 {
6806 result = cstring_makeLiteral ("Value");
6807 }
6808 break;
6809 case SK_PTR:
6810 if (sRef_isAnyParam (s->info->ref))
6811 {
6812 result = cstring_makeLiteral ("Out parameter");
6813 }
6814 else
6815 {
6816 result = cstring_makeLiteral ("Value");
6817 }
6818 break;
6819 case SK_ADR:
6820 result = cstring_makeLiteral ("Value");
6821 break;
6822 case SK_FIELD:
6823 result = cstring_makeLiteral ("Field");
6824 break;
6825 case SK_OBJECT:
6826 result = cstring_makeLiteral ("Object");
6827 break;
6828 case SK_UNCONSTRAINED:
6829 result = cstring_makeLiteral ("<anything>");
6830 break;
6831 case SK_RESULT:
6832 case SK_SPECIAL:
6833 case SK_UNKNOWN:
6834 case SK_EXTERNAL:
6835 case SK_DERIVED:
6836 case SK_CONST:
6837 case SK_TYPE:
6838 result = cstring_makeLiteral ("<unknown>");
6839 break;
6840 case SK_CONJ:
6841 result = cstring_makeLiteral ("<conj>");
6842 break;
6843 case SK_NEW:
6844 result = cstring_makeLiteral ("Storage");
6845 break;
6846 }
6847
6848 return result;
6849}
6850
6851/*@only@*/ cstring
6852sRef_unparseKindNamePlain (sRef s)
6853{
6854 cstring result;
6855
6856 if (s == sRef_undefined) return cstring_makeLiteral ("<invalid>");
6857
6858 s = sRef_fixConj (s);
6859
6860 switch (s->kind)
6861 {
6862 case SK_CVAR:
6863 if (sRef_isLocalVar (s))
6864 {
6865 result = cstring_makeLiteral ("Variable");
6866 }
6867 else
6868 {
6869 result = cstring_makeLiteral ("Global");
6870 }
6871 break;
6872 case SK_PARAM:
6873 result = cstring_makeLiteral ("Parameter");
6874 break;
6875 case SK_ARRAYFETCH:
6876 if (sRef_isAnyParam (s->info->arrayfetch->arr))
6877 {
6878 result = cstring_makeLiteral ("Parameter");
6879 }
6880 else if (sRef_isIndexKnown (s))
6881 {
6882 result = cstring_makeLiteral ("Array element");
6883 }
6884 else
6885 {
6886 result = cstring_makeLiteral ("Value");
6887 }
6888 break;
6889 case SK_PTR:
6890 if (sRef_isAnyParam (s->info->ref))
6891 {
6892 result = cstring_makeLiteral ("Parameter");
6893 }
6894 else
6895 {
6896 result = cstring_makeLiteral ("Value");
6897 }
6898 break;
6899 case SK_ADR:
6900 result = cstring_makeLiteral ("Value");
6901 break;
6902 case SK_FIELD:
6903 result = cstring_makeLiteral ("Field");
6904 break;
6905 case SK_OBJECT:
6906 result = cstring_makeLiteral ("Object");
6907 break;
6908 case SK_NEW:
6909 result = cstring_makeLiteral ("Storage");
6910 break;
6911 case SK_UNCONSTRAINED:
6912 result = cstring_makeLiteral ("<anything>");
6913 break;
6914 case SK_RESULT:
6915 case SK_TYPE:
6916 case SK_CONST:
6917 case SK_EXTERNAL:
6918 case SK_DERIVED:
6919 case SK_UNKNOWN:
6920 case SK_SPECIAL:
6921 result = cstring_makeLiteral ("<unknown>");
6922 break;
6923 case SK_CONJ:
6924 result = cstring_makeLiteral ("<conj>");
6925 break;
6926 }
6927
6928 return result;
6929}
6930
6931/*
6932** s1 <- s2
6933*/
6934
6935void
6936sRef_copyState (sRef s1, sRef s2)
6937{
6938 if (sRef_isValid (s1) && sRef_isValid (s2))
6939 {
6940 s1->defstate = s2->defstate;
6941
616915dd 6942 /* start modifications */
6943 s1->bufinfo.bufstate = s2->bufinfo.bufstate;
6944 s1->bufinfo.len = s2->bufinfo.len;
6945 s1->bufinfo.size = s2->bufinfo.size;
6946 /* end modifications */
6947
6948 s1->aliaskind = s2->aliaskind;
28bf4b0b 6949 s1->aliasinfo = stateInfo_update (s1->aliasinfo, s2->aliasinfo);
616915dd 6950
6951 s1->expkind = s2->expkind;
28bf4b0b 6952 s1->expinfo = stateInfo_update (s1->expinfo, s2->expinfo);
6953
6954 s1->nullstate = s2->nullstate;
6955 s1->nullinfo = stateInfo_update (s1->nullinfo, s2->nullinfo);
616915dd 6956
28bf4b0b 6957 /*@-mustfree@*/
6958 /*@i834 don't free it: valueTable_free (s1->state); */
6959 /*@i32@*/ s1->state = valueTable_copy (s2->state);
6960 /*@=mustfree@*/
616915dd 6961 s1->safe = s2->safe;
28bf4b0b 6962 }
616915dd 6963}
6964
6965sRef
6966sRef_makeNew (ctype ct, sRef t, cstring name)
6967{
28bf4b0b 6968 sRef s = sRef_newRef ();
616915dd 6969
6970 s->kind = SK_NEW;
6971 s->type = ct;
6972
6973 llassert (sRef_isValid (t));
6974 s->defstate = t->defstate;
6975
6976 s->aliaskind = t->aliaskind;
6977 s->oaliaskind = s->aliaskind;
616915dd 6978 s->nullstate = t->nullstate;
28bf4b0b 6979
616915dd 6980 s->expkind = t->expkind;
6981 s->oexpkind = s->expkind;
28bf4b0b 6982
616915dd 6983 s->info = (sinfo) dmalloc (sizeof (*s->info));
6984 s->info->fname = name;
6985
6986 /* start modifications */
6987 s->bufinfo.bufstate = t->bufinfo.bufstate;
6988 /* end modifications */
28bf4b0b 6989
6990 llassert (valueTable_isUndefined (s->state));
6991 s->state = valueTable_copy (t->state);
616915dd 6992
28bf4b0b 6993 DPRINTF (("==> Copying state: %s", valueTable_unparse (s->state)));
6994 DPRINTF (("==> new: %s", sRef_unparseFull (s)));
6995 return s;
616915dd 6996}
6997
6998sRef
6999sRef_makeType (ctype ct)
7000{
28bf4b0b 7001 sRef s = sRef_newRef ();
7002
7003 sRef_checkMutable (s);
7004
616915dd 7005 s->kind = SK_TYPE;
7006 s->type = ct;
7007
7008 s->defstate = SS_UNKNOWN;
7009 s->aliaskind = AK_UNKNOWN;
28bf4b0b 7010 sRef_setNullStateN (s, NS_UNKNOWN);
7011
616915dd 7012 /* start modification */
7013 s->bufinfo.bufstate = BB_NOTNULLTERMINATED;
7014 /* end modification */
7015
7016
7017 if (ctype_isUA (ct))
7018 {
7019 typeId uid = ctype_typeId (ct);
7020 uentry ue = usymtab_getTypeEntrySafe (uid);
7021
7022 if (uentry_isValid (ue))
7023 {
7024 sRef_mergeStateQuiet (s, uentry_getSref (ue));
7025 }
7026 }
28bf4b0b 7027
7028 s->oaliaskind = s->aliaskind;
616915dd 7029 s->oexpkind = s->expkind;
28bf4b0b 7030 llassert (valueTable_isUndefined (s->state));
7031 s->state = context_createValueTable (s);
616915dd 7032
28bf4b0b 7033 DPRINTF (("Create: %s", sRef_unparseFull (s)));
616915dd 7034 return s;
7035}
7036
7037sRef
7038sRef_makeConst (ctype ct)
7039{
28bf4b0b 7040 sRef s = sRef_newRef ();
616915dd 7041
7042 s->kind = SK_CONST;
7043 s->type = ct;
7044
7045 s->defstate = SS_UNKNOWN;
7046 s->aliaskind = AK_UNKNOWN;
28bf4b0b 7047 sRef_setNullStateN (s, NS_UNKNOWN);
7048
616915dd 7049 /* start modification */
7050 s->bufinfo.bufstate = BB_NULLTERMINATED;
7051 /* end modification */
7052
7053
7054 if (ctype_isUA (ct))
7055 {
7056 typeId uid = ctype_typeId (ct);
7057 uentry te = usymtab_getTypeEntrySafe (uid);
7058
7059 if (uentry_isValid (te))
7060 {
7061 sRef_mergeStateQuiet (s, uentry_getSref (te));
7062 }
7063 }
28bf4b0b 7064
616915dd 7065
7066 s->oaliaskind = s->aliaskind;
7067 s->oexpkind = s->expkind;
7068
28bf4b0b 7069 llassert (valueTable_isUndefined (s->state));
7070 s->state = context_createValueTable (s);
7071
616915dd 7072 return s;
7073}
7074
7075bool sRef_hasName (sRef s)
7076{
7077 if (sRef_isInvalid (s))
7078 {
7079 return (FALSE);
7080 }
7081
7082 switch (s->kind)
7083 {
7084 case SK_CVAR:
7085 {
7086 uentry u = usymtab_getRefQuiet (s->info->cvar->lexlevel,
7087 s->info->cvar->index);
7088 return (uentry_hasName (u));
7089 }
7090 case SK_PARAM:
7091 {
28bf4b0b 7092 if (s->info->paramno >= 0)
7093 {
7094 uentry u = uentryList_getN (context_getParams (),
7095 s->info->paramno);
7096
7097 return (uentry_hasName (u));
7098 }
7099 else
7100 {
7101 llassert (s->info->paramno == PARAMUNKNOWN);
7102 return FALSE;
7103 }
616915dd 7104 }
7105 default:
7106 return TRUE;
7107 }
7108}
7109
616915dd 7110bool
7111sRef_sameName (sRef s1, sRef s2)
7112{
7113 if (sRef_isInvalid (s1))
7114 {
7115 return sRef_isInvalid (s2);
7116 }
7117
7118 if (sRef_isInvalid (s2))
7119 {
7120 return (FALSE);
7121 }
7122
7123 switch (s1->kind)
7124 {
7125 case SK_CVAR:
7126 if (s2->kind == SK_CVAR)
7127 {
7128 return (s1->info->cvar->lexlevel == s2->info->cvar->lexlevel
7129 && s1->info->cvar->index == s2->info->cvar->index);
7130 }
7131 else if (s2->kind == SK_PARAM)
7132 {
7133 if (context_inFunctionLike ())
7134 {
28bf4b0b 7135 if (s2->info->paramno != PARAMUNKNOWN)
7136 {
7137 uentry u1 = usymtab_getRefQuiet (s1->info->cvar->lexlevel,
7138 s1->info->cvar->index);
7139 uentry u2 = uentryList_getN (context_getParams (),
7140 s2->info->paramno);
7141
7142 return (cstring_equalFree (uentry_getName (u1),
7143 uentry_getName (u2)));
7144 }
7145 else
7146 {
7147 return s1->info->paramno == PARAMUNKNOWN;
7148 }
616915dd 7149 }
7150 else
7151 {
7152 return FALSE;
7153 }
7154 }
7155 else
7156 {
7157 return FALSE;
7158 }
7159 case SK_PARAM:
7160 {
7161 if (s2->kind == SK_PARAM)
7162 {
7163 return (s1->info->paramno == s2->info->paramno);
7164 }
7165 else if (s2->kind == SK_CVAR)
7166 {
7167 if (context_inFunctionLike ())
7168 {
28bf4b0b 7169 if (s1->info->paramno == PARAMUNKNOWN)
7170 {
7171 return FALSE;
7172 }
7173 else
7174 {
7175 uentry u1 = uentryList_getN (context_getParams (),
7176 s1->info->paramno);
7177 uentry u2 = usymtab_getRefQuiet (s2->info->cvar->lexlevel,
7178 s2->info->cvar->index);
7179
7180
7181 return (cstring_equalFree (uentry_getName (u1),
7182 uentry_getName (u2)));
7183 }
616915dd 7184 }
7185 else
7186 {
7187 return FALSE;
7188 }
7189 }
7190 else
7191 {
7192 return FALSE;
7193 }
7194 }
7195
7196 case SK_UNCONSTRAINED:
7197 return FALSE;
7198
7199 case SK_ARRAYFETCH:
7200 if (s2->kind == SK_ARRAYFETCH)
7201 {
7202 if (bool_equal (s1->info->arrayfetch->indknown,
7203 s2->info->arrayfetch->indknown))
7204 {
7205 if (!s1->info->arrayfetch->indknown
7206 || (s1->info->arrayfetch->ind == s2->info->arrayfetch->ind))
7207 {
7208 return sRef_sameName (s1->info->arrayfetch->arr,
7209 s2->info->arrayfetch->arr);
7210 }
7211 }
7212 }
7213
7214 return FALSE;
7215 case SK_FIELD:
7216 if (s2->kind == SK_FIELD)
7217 {
7218 if (cstring_equal (s1->info->field->field,
7219 s2->info->field->field))
7220 {
7221 return sRef_sameName (s1->info->field->rec,
7222 s2->info->field->rec);
7223 }
7224
7225 }
7226 return FALSE;
7227 case SK_PTR:
7228 case SK_ADR:
7229 case SK_DERIVED:
7230 case SK_EXTERNAL:
7231 if (s2->kind == s1->kind)
7232 {
7233 return sRef_sameName (s1->info->ref,
7234 s2->info->ref);
7235 }
7236
7237 return FALSE;
7238 case SK_OBJECT:
7239 return FALSE;
7240 case SK_CONJ:
7241 return sRef_sameName (sRef_getConjA (s1), s2);
7242 case SK_NEW:
7243 return FALSE;
7244 case SK_UNKNOWN:
7245 return (s2->kind == SK_UNKNOWN);
7246 case SK_TYPE:
7247 case SK_CONST:
7248 if (s2->kind == s1->kind)
7249 {
7250 return (ctype_equal (s1->type, s2->type));
7251 }
7252
7253 return FALSE;
7254 case SK_SPECIAL:
7255 if (s2->kind == SK_SPECIAL)
7256 {
7257 return (s1->info->spec == s2->info->spec);
7258 }
7259 return FALSE;
7260 case SK_RESULT:
7261 return (s2->kind == SK_RESULT);
7262 default:
7263 return FALSE;
7264 }
7265 BADEXIT;
7266}
7267
7268sRef
7269sRef_fixOuterRef (/*@returned@*/ sRef s)
7270{
7271 sRef root = sRef_getRootBase (s);
7272
7273 if (sRef_isCvar (root))
7274 {
7275 uentry ue = usymtab_getRefQuiet (root->info->cvar->lexlevel,
7276 root->info->cvar->index);
7277
7278 if (uentry_isValid (ue))
7279 {
7280 sRef uref = uentry_getSref (ue);
7281 sRef sr = sRef_fixBase (s, uref);
7282
7283 return (sr);
7284 }
7285 else
7286 {
7287 llcontbug (message ("sRef_fixOuterRef: undefined: %q", sRef_unparseDebug (s)));
7288 return (s);
7289 }
7290 }
7291
7292 return (s);
7293}
7294
7295void
7296sRef_storeState (sRef s)
7297{
7298 if (sRef_isInvalid (s)) return;
7299
28bf4b0b 7300 sRef_checkMutable (s);
616915dd 7301 s->oaliaskind = s->aliaskind;
7302 s->oexpkind = s->expkind;
7303}
7304
7305static void sRef_resetStateAux (sRef s, /*@unused@*/ fileloc loc)
7306{
7307 sRef_resetState (s);
7308}
7309
7310void
7311sRef_resetState (sRef s)
7312{
7313 bool changed = FALSE;
7314 if (sRef_isInvalid (s)) return;
7315
7316
7317 if (s->oaliaskind == AK_KILLREF && !sRef_isParam (s))
7318 {
7319 /*
7320 ** killref is used in a kludgey way, to save having to add
7321 ** another alias kind (see usymtab_handleParams)
7322 */
7323
7324 if (s->expkind != s->oexpkind)
7325 {
7326 changed = TRUE;
7327 s->expkind = s->oexpkind;
7328 }
7329 }
7330 else
7331 {
7332 if (s->expkind != s->oexpkind)
7333 {
7334 changed = TRUE;
7335 s->expkind = s->oexpkind;
7336 }
7337
7338 if (s->aliaskind != s->oaliaskind
7339 && s->aliaskind != AK_REFCOUNTED
7340 && s->aliaskind != AK_REFS)
7341 {
7342 changed = TRUE;
7343 s->aliaskind = s->oaliaskind;
28bf4b0b 7344 }
616915dd 7345 }
7346
7347 if (changed)
7348 {
7349 sRef_clearDerived (s);
7350 }
7351
7352 }
7353
7354void
7355sRef_resetStateComplete (sRef s)
7356{
7357 sRef_innerAliasSetComplete (sRef_resetStateAux, s, fileloc_undefined);
7358}
7359
7360/*@exposed@*/ sRef
7361sRef_fixBase (/*@returned@*/ sRef s, /*@returned@*/ sRef base)
7362{
7363 sRef tmp = sRef_undefined;
7364 sRef ret;
7365
7366 if (sRef_isInvalid (s)) return s;
7367 if (sRef_isInvalid (base)) return base;
7368
7369 switch (s->kind)
7370 {
7371 case SK_RESULT:
7372 case SK_PARAM:
7373 case SK_CVAR:
7374 ret = base;
7375 break;
7376 case SK_ARRAYFETCH:
7377 tmp = sRef_fixBase (s->info->arrayfetch->arr, base);
7378
7379 if (s->info->arrayfetch->indknown)
7380 {
7381 ret = sRef_makeArrayFetchKnown (tmp, s->info->arrayfetch->ind);
7382 }
7383 else
7384 {
7385 ret = sRef_makeArrayFetch (tmp);
7386 }
7387 break;
7388 case SK_FIELD:
7389 tmp = sRef_fixBase (s->info->field->rec, base);
7390 ret = sRef_buildNCField (tmp, s->info->field->field);
7391 break;
7392 case SK_PTR:
7393 tmp = sRef_fixBase (s->info->ref, base);
7394 ret = sRef_makePointer (tmp);
7395 break;
7396 case SK_ADR:
7397 tmp = sRef_fixBase (s->info->ref, base);
7398 ret = sRef_makeAddress (tmp);
7399 break;
7400 case SK_CONJ:
7401 {
7402 sRef tmpb;
7403
7404 tmp = sRef_fixBase (s->info->conj->a, base);
7405 tmpb = sRef_fixBase (s->info->conj->b, base);
7406
7407 ret = sRef_makeConj (tmp, tmpb);
7408 break;
7409 }
7410 BADDEFAULT;
7411 }
7412
7413 return ret;
7414}
7415
7416static /*@exposed@*/ sRef
7417sRef_fixDirectBase (sRef s, sRef base)
7418{
7419 sRef ret;
7420
7421
7422 if (sRef_isInvalid (s))
7423 {
28bf4b0b 7424 return sRef_undefined;
616915dd 7425 }
28bf4b0b 7426
616915dd 7427 switch (s->kind)
7428 {
7429 case SK_ARRAYFETCH:
7430 if (s->info->arrayfetch->indknown)
7431 {
7432 ret = sRef_makeArrayFetchKnown (base, s->info->arrayfetch->ind);
7433 }
7434 else
7435 {
7436 ret = sRef_makeArrayFetch (base);
7437 }
7438 break;
7439 case SK_FIELD:
7440 ret = sRef_buildNCField (base, s->info->field->field);
7441 break;
7442 case SK_PTR:
7443 ret = sRef_makePointer (base);
7444 break;
7445 case SK_ADR:
7446 ret = sRef_makeAddress (base);
7447 break;
7448 case SK_CONJ:
7449 {
7450 sRef tmpa, tmpb;
7451
7452 tmpa = sRef_fixDirectBase (s->info->conj->a, base);
7453 tmpb = sRef_fixDirectBase (s->info->conj->b, base);
7454
7455 ret = sRef_makeConj (tmpa, tmpb);
7456 break;
7457 }
7458 BADDEFAULT;
7459 }
7460
7461 sRef_copyState (ret, s);
7462 return ret;
7463}
7464
7465bool
7466sRef_isAllocIndexRef (sRef s)
7467{
7468 return (sRef_isArrayFetch (s) && !(s->info->arrayfetch->indknown)
7469 && sRef_isAllocated (s->info->arrayfetch->arr));
7470}
7471
7472void
7473sRef_showRefLost (sRef s)
7474{
7475 if (sRef_hasAliasInfoLoc (s))
7476 {
7477 llgenindentmsg (cstring_makeLiteral ("Original reference lost"),
28bf4b0b 7478 sRef_getAliasInfoLoc (s));
616915dd 7479 }
7480}
7481
7482void
7483sRef_showRefKilled (sRef s)
7484{
7485 if (sRef_hasStateInfoLoc (s))
7486 {
7487 llgenindentmsg (message ("Storage %q released",
7488 sRef_unparse (s)), sRef_getStateInfoLoc (s));
7489 }
7490}
7491
7492void
7493sRef_showStateInconsistent (sRef s)
7494{
7495 if (sRef_hasStateInfoLoc (s))
7496 {
7497 llgenindentmsg
7498 (message ("Storage %qbecomes inconsistent (released on one branch)",
7499 sRef_unparseOpt (s)),
7500 sRef_getStateInfoLoc (s));
7501 }
7502}
7503
7504void
7505sRef_showStateInfo (sRef s)
7506{
7507 if (sRef_hasStateInfoLoc (s))
7508 {
7509 if (s->defstate == SS_DEAD)
7510 {
7511 llgenindentmsg
7512 (message ("Storage %qis released", sRef_unparseOpt (s)),
7513 sRef_getStateInfoLoc (s));
7514 }
7515 else if (s->defstate == SS_ALLOCATED || s->defstate == SS_DEFINED)
7516 {
7517 llgenindentmsg
7518 (message ("Storage %qis %s", sRef_unparseOpt (s),
7519 sstate_unparse (s->defstate)),
7520 sRef_getStateInfoLoc (s));
7521 }
7522 else if (s->defstate == SS_UNUSEABLE)
7523 {
7524 llgenindentmsg
7525 (message ("Storage %qbecomes inconsistent (clauses merge with"
7526 "%qreleased on one branch)",
7527 sRef_unparseOpt (s),
7528 sRef_unparseOpt (s)),
7529 sRef_getStateInfoLoc (s));
7530 }
7531 else
7532 {
7533 llgenindentmsg (message ("Storage %qbecomes %s",
7534 sRef_unparseOpt (s),
7535 sstate_unparse (s->defstate)),
7536 sRef_getStateInfoLoc (s));
7537 }
7538 }
7539}
7540
7541void
7542sRef_showExpInfo (sRef s)
7543{
7544 if (sRef_hasExpInfoLoc (s))
7545 {
7546 llgenindentmsg (message ("Storage %qbecomes %s", sRef_unparseOpt (s),
7547 exkind_unparse (s->expkind)),
7548 sRef_getExpInfoLoc (s));
7549 }
7550}
7551
28bf4b0b 7552void
7553sRef_showMetaStateInfo (sRef s, cstring key)
7554{
7555 stateValue val;
7556 metaStateInfo minfo = context_lookupMetaStateInfo (key);
7557
7558 llassert (sRef_isValid (s));
7559 llassert (valueTable_isDefined (s->state));
7560 llassert (metaStateInfo_isDefined (minfo));
7561
7562 val = valueTable_lookup (s->state, key);
7563
7564 if (stateValue_hasLoc (val))
7565 {
7566 llgenindentmsg
7567 (message ("Meta state %qbecomes %s", sRef_unparseOpt (s),
7568 metaStateInfo_unparseValue (minfo, stateValue_getValue (val))),
7569 stateValue_getLoc (val));
7570 }
7571}
7572
616915dd 7573void
7574sRef_showNullInfo (sRef s)
7575{
28bf4b0b 7576 DPRINTF (("Show null info: %s", sRef_unparseFull (s)));
7577
616915dd 7578 if (sRef_hasNullInfoLoc (s) && sRef_isKnown (s))
7579 {
28bf4b0b 7580 DPRINTF (("has null info: %s",
7581 fileloc_unparse (sRef_getNullInfoLoc (s))));
7582
7583 switch (sRef_getNullState (s))
616915dd 7584 {
7585 case NS_CONSTNULL:
7586 {
7587 fileloc loc = sRef_getNullInfoLoc (s);
7588
7589 if (fileloc_isDefined (loc) && !fileloc_isLib (loc))
7590 {
7591 llgenindentmsg
7592 (message ("Storage %qbecomes null", sRef_unparseOpt (s)),
7593 loc);
7594 }
7595 break;
7596 }
7597 case NS_DEFNULL:
7598 {
7599 fileloc loc = sRef_getNullInfoLoc (s);
7600
7601 if (fileloc_isDefined (loc) && !fileloc_isLib (loc))
7602 {
7603 llgenindentmsg (message ("Storage %qbecomes null", sRef_unparseOpt (s)),
7604 loc);
7605 }
7606 break;
7607 }
7608 case NS_ABSNULL:
7609 case NS_POSNULL:
7610 llgenindentmsg
7611 (message ("Storage %qmay become null", sRef_unparseOpt (s)),
7612 sRef_getNullInfoLoc (s));
7613 break;
7614 case NS_NOTNULL:
7615 case NS_MNOTNULL:
7616 llgenindentmsg
7617 (message ("Storage %qbecomes not null", sRef_unparseOpt (s)),
7618 sRef_getNullInfoLoc (s));
7619 break;
7620 case NS_UNKNOWN:
7621 llgenindentmsg
7622 (message ("Storage %qnull state becomes unknown",
7623 sRef_unparseOpt (s)),
7624 sRef_getNullInfoLoc (s));
7625 break;
7626
7627 case NS_ERROR:
7628 BADBRANCHCONT;
7629 break;
7630
7631 default:
7632 llgenindentmsg
7633 (message ("<error case> Storage %q becomes %s",
7634 sRef_unparse (s),
28bf4b0b 7635 nstate_unparse (sRef_getNullState (s))),
616915dd 7636 sRef_getNullInfoLoc (s));
7637
7638 break;
7639 }
7640 }
7641}
7642
7643void
7644sRef_showAliasInfo (sRef s)
7645{
7646 if (sRef_hasAliasInfoLoc (s))
7647 {
7648 if (sRef_isFresh (s))
7649 {
7650 llgenindentmsg
7651 (message ("Fresh storage %qallocated", sRef_unparseOpt (s)),
7652 sRef_getAliasInfoLoc (s));
7653 }
7654 else
7655 {
7656 if (!sRef_isRefCounted (s))
7657 {
7658 llgenindentmsg
7659 (message ("Storage %qbecomes %s",
7660 sRef_unparseOpt (s),
7661 alkind_unparse (sRef_getAliasKind (s))),
7662 sRef_getAliasInfoLoc (s));
7663 }
7664 }
7665 }
7666}
7667
7668void
7669sRef_mergeNullState (sRef s, nstate n)
7670{
7671 if (sRef_isValid (s))
7672 {
7673 nstate old;
7674
28bf4b0b 7675 old = sRef_getNullState (s);
616915dd 7676
7677 if (n != old && n != NS_UNKNOWN)
28bf4b0b 7678 {
7679 sRef_setNullState (s, n, g_currentloc);
616915dd 7680 }
7681 }
7682 else
7683 {
7684 llbuglit ("sRef_mergeNullState: invalid");
7685 }
7686}
7687
7688bool
7689sRef_possiblyNull (sRef s)
7690{
7691 if (sRef_isValid (s))
28bf4b0b 7692 {
7693 if (sRef_getNullState (s) == NS_ABSNULL)
616915dd 7694 {
7695 ctype rct = ctype_realType (s->type);
7696
7697 if (ctype_isAbstract (rct))
7698 {
7699 return FALSE;
7700 }
7701 else
7702 {
7703 if (ctype_isUser (rct))
7704 {
7705 uentry ue = usymtab_getTypeEntry (ctype_typeId (rct));
7706
7707 return (nstate_possiblyNull
7708 (sRef_getNullState (uentry_getSref (ue))));
7709 }
7710 else
7711 {
7712 return FALSE;
7713 }
7714 }
7715 }
7716 else
7717 {
28bf4b0b 7718 return nstate_possiblyNull (sRef_getNullState (s));
616915dd 7719 }
7720 }
7721
7722 return FALSE;
7723}
7724
7725cstring
7726sRef_getScopeName (sRef s)
7727{
7728 sRef base = sRef_getRootBase (s);
7729
7730 if (sRef_isRealGlobal (base))
7731 {
7732 return (cstring_makeLiteralTemp ("Global"));
7733 }
7734 else if (sRef_isFileStatic (base))
7735 {
7736 return (cstring_makeLiteralTemp ("Static"));
7737 }
7738 else
7739 {
7740 return (cstring_makeLiteralTemp ("Local"));
7741 }
7742}
7743
7744cstring
7745sRef_unparseScope (sRef s)
7746{
7747 sRef base = sRef_getRootBase (s);
7748
7749 if (sRef_isRealGlobal (base))
7750 {
7751 return (cstring_makeLiteralTemp ("global"));
7752 }
7753 else if (sRef_isFileStatic (base))
7754 {
7755 return (cstring_makeLiteralTemp ("file static"));
7756 }
7757 else
7758 {
7759 BADEXIT;
7760 }
7761}
7762
7763int
7764sRef_getScope (sRef s)
7765{
7766 llassert (sRef_isValid (s));
7767
7768 if (sRef_isCvar (s))
7769 {
7770 return s->info->cvar->lexlevel;
7771 }
7772 else if (sRef_isParam (s))
7773 {
7774 return paramsScope;
7775 }
7776 else
7777 {
7778 return fileScope;
7779 }
7780}
7781
7782bool
7783sRef_isDead (sRef s)
7784{
7785 return (sRef_isValid (s) && (s)->defstate == SS_DEAD);
7786}
7787
7788bool
7789sRef_isDeadStorage (sRef s)
7790{
7791 if (sRef_isValid (s))
7792 {
7793 if (s->defstate == SS_DEAD
7794 || s->defstate == SS_UNUSEABLE
7795 || s->defstate == SS_UNDEFINED
7796 || s->defstate == SS_UNKNOWN)
7797 {
7798 return TRUE;
7799 }
7800 else
7801 {
7802 return (sRef_isDefinitelyNull (s));
7803 }
7804 }
7805 else
7806 {
7807 return FALSE;
7808 }
7809}
7810
7811bool
7812sRef_isPossiblyDead (sRef s)
7813{
7814 return (sRef_isValid (s) && s->defstate == SS_HOFFA);
7815}
7816
7817bool sRef_isStateLive (sRef s)
7818{
7819 if (sRef_isValid (s))
7820 {
7821 sstate ds = s->defstate;
7822
7823 return (!(ds == SS_UNDEFINED
7824 || ds == SS_DEAD
7825 || ds == SS_UNUSEABLE
7826 || ds == SS_HOFFA));
7827 }
7828 else
7829 {
7830 return FALSE;
7831 }
7832}
7833
7834
7835bool sRef_isStateUndefined (sRef s)
7836{
7837 return ((sRef_isValid(s)) && ((s)->defstate == SS_UNDEFINED));
7838}
7839
7840bool sRef_isJustAllocated (sRef s)
7841{
7842 if (sRef_isAllocated (s))
7843 {
7844 sRefSet_allElements (s->deriv, el)
7845 {
7846 if (!(sRef_isStateUndefined (el) || sRef_isUnuseable (el)))
7847 {
7848 return FALSE;
7849 }
7850 } end_sRefSet_allElements ;
7851
7852 return TRUE;
7853 }
7854
7855 return FALSE;
7856}
7857
7858static bool
7859sRef_isAllocatedStorage (sRef s)
7860{
7861 if (sRef_isValid (s) && ynm_toBoolStrict (sRef_isReadable (s)))
7862 {
7863 return (ctype_isVisiblySharable (sRef_getType (s)));
7864 }
7865 else
7866 {
7867 return FALSE;
7868 }
7869}
7870
7871bool
7872sRef_isUnuseable (sRef s)
7873{
7874 return ((sRef_isValid(s)) && ((s)->defstate == SS_UNUSEABLE));
7875}
7876
7877bool
7878sRef_perhapsNull (sRef s)
7879{
7880 if (sRef_isValid (s))
7881 {
28bf4b0b 7882 if (sRef_getNullState (s) == NS_ABSNULL)
616915dd 7883 {
7884 ctype rct = ctype_realType (s->type);
7885
7886 if (ctype_isAbstract (rct))
7887 {
7888 return FALSE;
7889 }
7890 else
7891 {
7892 if (ctype_isUser (rct))
7893 {
7894 uentry ue = usymtab_getTypeEntry (ctype_typeId (rct));
7895
7896 return (nstate_perhapsNull
7897 (sRef_getNullState (uentry_getSref (ue))));
7898 }
7899 else
7900 {
7901 return FALSE;
7902 }
7903 }
7904 }
7905 else
7906 {
28bf4b0b 7907 return nstate_perhapsNull (sRef_getNullState (s));
616915dd 7908 }
7909 }
7910
7911 return FALSE;
7912}
7913
7914/*
7915** definitelyNull --- called when TRUE is good
7916*/
7917
7918bool
7919sRef_definitelyNull (sRef s)
7920{
7921 return (sRef_isValid (s)
28bf4b0b 7922 && (sRef_getNullState (s) == NS_DEFNULL || sRef_getNullState (s) == NS_CONSTNULL));
616915dd 7923}
7924
7925/*
7926** based on sRef_similar
7927*/
7928
7929void
7930sRef_setDerivNullState (sRef set, sRef guide, nstate ns)
7931{
7932 if (sRef_isValid (set))
7933 {
7934 sRef deriv = sRef_getDeriv (set, guide);
7935
7936 if (sRef_isValid (deriv))
7937 {
28bf4b0b 7938 sRef_setNullStateN (deriv, ns);
616915dd 7939 }
7940 }
7941}
7942
7943static /*@exposed@*/ sRef
7944sRef_getDeriv (/*@returned@*/ /*@notnull@*/ sRef set, sRef guide)
7945{
7946 llassert (sRef_isValid (set));
7947 llassert (sRef_isValid (guide));
7948
7949 switch (guide->kind)
7950 {
7951 case SK_CVAR:
7952 llassert (set->kind == SK_CVAR);
7953
7954 return set;
7955
7956 case SK_PARAM:
7957 llassert (set->kind == guide->kind);
7958 llassert (set->info->paramno == guide->info->paramno);
7959
7960 return set;
7961
7962 case SK_ARRAYFETCH:
7963
7964 if (set->kind == SK_ARRAYFETCH
7965 && (sRef_similar (set->info->arrayfetch->arr,
7966 guide->info->arrayfetch->arr)))
7967 {
7968 return set;
7969 }
7970 else
7971 {
7972 return (sRef_makeAnyArrayFetch
7973 (sRef_getDeriv (set, guide->info->arrayfetch->arr)));
7974 }
7975
7976 case SK_PTR:
7977
7978 if (set->kind == SK_PTR && sRef_similar (set->info->ref, guide->info->ref))
7979 {
7980 return set;
7981 }
7982 else
7983 {
7984 return (sRef_makePointer (sRef_getDeriv (set, guide->info->ref)));
7985 }
7986
7987 case SK_FIELD:
7988
7989 if ((set->kind == SK_FIELD &&
7990 (sRef_similar (set->info->field->rec, guide->info->field->rec) &&
7991 cstring_equal (set->info->field->field, guide->info->field->field))))
7992 {
7993 return set;
7994 }
7995 else
7996 {
7997 return (sRef_makeField (sRef_getDeriv (set, guide->info->field->rec),
7998 guide->info->field->field));
7999 }
8000 case SK_ADR:
8001
8002 if ((set->kind == SK_ADR) && sRef_similar (set->info->ref, guide->info->ref))
8003 {
8004 return set;
8005 }
8006 else
8007 {
8008 return (sRef_makeAddress (sRef_getDeriv (set, guide->info->ref)));
8009 }
8010
8011 case SK_CONJ:
8012
8013 return sRef_undefined;
8014
8015 case SK_RESULT:
8016 case SK_SPECIAL:
8017 case SK_UNCONSTRAINED:
8018 case SK_TYPE:
8019 case SK_CONST:
8020 case SK_NEW:
8021 case SK_UNKNOWN:
8022 case SK_OBJECT:
8023 case SK_DERIVED:
8024 case SK_EXTERNAL:
8025 return sRef_undefined;
8026 }
8027
8028 BADEXIT;
8029}
8030
8031/*
8032** sRef_aliasCheckPred
8033**
8034** A confusing but spiffy function:
8035**
8036** Calls predf (s, e, text, <alias>) on s and all of s's aliases
8037** (unless checkAliases (s) is FALSE).
8038**
8039** For alias calls, calls as
8040** predf (alias, e, text, s)
8041*/
8042
8043void
8044sRef_aliasCheckPred (bool (predf) (sRef, exprNode, sRef, exprNode),
8045 /*@null@*/ bool (checkAliases) (sRef),
8046 sRef s, exprNode e, exprNode err)
8047{
8048 bool error = (*predf)(s, e, sRef_undefined, err);
8049
8050
8051 if (checkAliases != NULL && !(checkAliases (s)))
8052 {
8053 /* don't check aliases */
8054 }
8055 else
8056 {
8057 sRefSet aliases = usymtab_allAliases (s);
8058
8059
8060 sRefSet_realElements (aliases, current)
8061 {
8062 if (sRef_isValid (current))
8063 {
8064 if (!sRef_similar (current, s)
8065 || (error && sRef_sameName (current, s)))
8066 {
8067 (void) (*predf)(current, e, s, err);
8068 }
8069 }
8070 } end_sRefSet_realElements;
8071
8072 sRefSet_free (aliases);
8073 }
8074}
8075
8076/*
8077** return TRUE iff predf (s) is true for s or any alias of s
8078*/
8079
8080bool
8081sRef_aliasCheckSimplePred (sRefTest predf, sRef s)
8082{
8083
8084 if ((*predf)(s))
8085 {
8086 return TRUE;
8087 }
8088 else
8089 {
8090 sRefSet aliases;
8091
8092 aliases = usymtab_allAliases (s);
8093
8094 sRefSet_realElements (aliases, current)
8095 {
8096 if (sRef_isValid (current))
8097 {
8098 sRef cref = sRef_updateSref (current);
8099
8100 /* Whoa! a very kludgey way to make sure the right sref is used
8101 ** where there is a conditional symbol table. I am beginning
8102 ** to think that having a conditional symbol table wasn't such
8103 ** a great idea. ;(
8104 */
8105
616915dd 8106 if ((*predf)(cref))
8107 {
28bf4b0b 8108 DPRINTF (("Checking alias: %s", sRef_unparseFull (cref)));
616915dd 8109 sRefSet_free (aliases);
8110 return TRUE;
8111 }
8112 }
8113 } end_sRefSet_realElements;
8114
8115 sRefSet_free (aliases);
8116 }
8117 return FALSE;
8118}
8119
8120bool
8121sRef_aliasCompleteSimplePred (bool (predf) (sRef), sRef s)
8122{
8123 sRefSet aliases;
8124 bool result = FALSE;
8125
8126
8127 aliases = usymtab_allAliases (s);
8128
8129 if ((*predf)(s)) result = TRUE;
8130
8131
8132 sRefSet_realElements (aliases, current)
8133 {
8134 if (sRef_isValid (current))
8135 {
28bf4b0b 8136 current = sRef_updateSref (current);
8137 if ((*predf)(current)) result = TRUE;
616915dd 8138 }
8139 } end_sRefSet_realElements;
8140
8141 sRefSet_free (aliases);
8142 return result;
8143}
8144
8145static void
8146sRef_aliasSetComplete (void (predf) (sRef, fileloc), sRef s, fileloc loc)
8147{
8148 sRefSet aliases;
8149
8150 aliases = usymtab_allAliases (s);
8151
28bf4b0b 8152 DPRINTF (("All aliases: %s", sRefSet_unparseFull (aliases)));
8153
616915dd 8154 (*predf)(s, loc);
8155
8156 sRefSet_realElements (aliases, current)
8157 {
8158 if (sRef_isValid (current))
8159 {
8160 current = sRef_updateSref (current);
28bf4b0b 8161 ((*predf)(current, loc));
616915dd 8162 }
8163 } end_sRefSet_realElements;
8164
8165 sRefSet_free (aliases);
8166}
8167
8168static void
8169sRef_aliasSetCompleteParam (void (predf) (sRef, alkind, fileloc), sRef s,
8170 alkind kind, fileloc loc)
8171{
8172 sRefSet aliases;
8173
8174
8175 if (sRef_isDeep (s))
8176 {
8177 aliases = usymtab_allAliases (s);
8178 }
8179 else
8180 {
8181 aliases = usymtab_aliasedBy (s);
8182 }
8183
8184 (*predf)(s, kind, loc);
8185
8186 sRefSet_realElements (aliases, current)
8187 {
8188 if (sRef_isValid (current))
8189 {
8190 current = sRef_updateSref (current);
28bf4b0b 8191 ((*predf)(current, kind, loc));
616915dd 8192 }
8193 } end_sRefSet_realElements;
8194
8195 sRefSet_free (aliases);
8196}
8197
8198static void
8199sRef_innerAliasSetComplete (void (predf) (sRef, fileloc), sRef s, fileloc loc)
8200{
8201 sRef inner;
8202 sRefSet aliases;
8203 ctype ct;
8204
8205 if (!sRef_isValid (s)) return;
8206
8207
8208 /*
8209 ** Type equivalence checking is necessary --- there might be casting.
8210 */
8211
8212 (*predf)(s, loc);
8213
8214 switch (s->kind)
8215 {
8216 case SK_UNCONSTRAINED:
8217 case SK_CVAR:
8218 case SK_PARAM:
8219 break;
8220 case SK_PTR:
8221 inner = s->info->ref;
8222 aliases = usymtab_allAliases (inner);
8223 ct = sRef_getType (inner);
8224
8225
8226 sRefSet_realElements (aliases, current)
8227 {
8228 if (sRef_isValid (current))
8229 {
8230 current = sRef_updateSref (current);
8231
8232 if (ctype_equal (ct, sRef_getType (current)))
8233 {
8234 sRef ptr = sRef_makePointer (current);
616915dd 8235 ((*predf)(ptr, loc));
8236 }
8237 }
8238 } end_sRefSet_realElements;
8239
8240 sRefSet_free (aliases);
8241 break;
8242 case SK_ARRAYFETCH:
8243 inner = s->info->arrayfetch->arr;
8244 aliases = usymtab_allAliases (inner);
8245 ct = sRef_getType (inner);
8246
8247 sRefSet_realElements (aliases, current)
8248 {
8249 if (sRef_isValid (current))
8250 {
8251 current = sRef_updateSref (current);
8252
8253 if (ctype_equal (ct, sRef_getType (current)))
8254 {
8255
8256 if (s->info->arrayfetch->indknown)
8257 {
8258 sRef af = sRef_makeArrayFetchKnown (current, s->info->arrayfetch->ind);
8259
8260 ((*predf)(af, loc));
8261 }
8262 else
8263 {
8264 sRef af = sRef_makeArrayFetch (current);
8265
8266 ((*predf)(af, loc));
8267 }
8268 }
8269 }
8270 } end_sRefSet_realElements;
8271
8272 sRefSet_free (aliases);
8273 break;
8274 case SK_FIELD:
8275 inner = s->info->field->rec;
8276 aliases = usymtab_allAliases (inner);
8277 ct = sRef_getType (inner);
8278
8279
8280 sRefSet_realElements (aliases, current)
8281 {
8282 if (sRef_isValid (current))
8283 {
8284 current = sRef_updateSref (current);
8285
8286 if (ctype_equal (ct, sRef_getType (current)))
8287 {
8288 sRef f = sRef_makeField (current, s->info->field->field);
8289
8290 ((*predf)(f, loc));
8291 }
8292 }
8293 } end_sRefSet_realElements;
8294
8295 sRefSet_free (aliases);
8296 break;
8297 case SK_CONJ:
8298 sRef_innerAliasSetComplete (predf, s->info->conj->a, loc);
8299 sRef_innerAliasSetComplete (predf, s->info->conj->b, loc);
8300 break;
8301 case SK_SPECIAL:
8302 case SK_ADR:
8303 case SK_TYPE:
8304 case SK_CONST:
8305 case SK_NEW:
8306 case SK_UNKNOWN:
8307 case SK_OBJECT:
8308 case SK_DERIVED:
8309 case SK_EXTERNAL:
8310 case SK_RESULT:
8311 break;
8312 }
8313}
8314
8315static void
8316sRef_innerAliasSetCompleteParam (void (predf) (sRef, sRef), sRef s, sRef t)
8317{
8318 sRef inner;
8319 sRefSet aliases;
8320 ctype ct;
8321
8322 if (!sRef_isValid (s)) return;
8323
8324
8325 /*
8326 ** Type equivalence checking is necessary --- there might be casting.
8327 */
8328
8329 (*predf)(s, t);
8330
8331 switch (s->kind)
8332 {
8333 case SK_UNCONSTRAINED:
8334 case SK_CVAR:
8335 case SK_PARAM:
8336 break;
8337 case SK_PTR:
8338 inner = s->info->ref;
8339 aliases = usymtab_allAliases (inner);
8340 ct = sRef_getType (inner);
8341
8342
8343 sRefSet_realElements (aliases, current)
8344 {
8345 if (sRef_isValid (current))
8346 {
8347 current = sRef_updateSref (current);
8348
8349 if (ctype_equal (ct, sRef_getType (current)))
8350 {
8351 sRef ptr = sRef_makePointer (current);
8352
8353 ((*predf)(ptr, t));
8354 }
8355 }
8356 } end_sRefSet_realElements;
8357
8358 sRefSet_free (aliases);
8359 break;
8360 case SK_ARRAYFETCH:
8361 inner = s->info->arrayfetch->arr;
8362 aliases = usymtab_allAliases (inner);
8363 ct = sRef_getType (inner);
8364
8365 sRefSet_realElements (aliases, current)
8366 {
8367 if (sRef_isValid (current))
8368 {
8369 current = sRef_updateSref (current);
8370
8371 if (ctype_equal (ct, sRef_getType (current)))
8372 {
8373
8374 if (s->info->arrayfetch->indknown)
8375 {
8376 sRef af = sRef_makeArrayFetchKnown (current, s->info->arrayfetch->ind);
8377
8378 ((*predf)(af, t));
8379 }
8380 else
8381 {
8382 sRef af = sRef_makeArrayFetch (current);
8383
8384 ((*predf)(af, t));
8385 }
8386 }
8387 }
8388 } end_sRefSet_realElements;
8389
8390 sRefSet_free (aliases);
8391 break;
8392 case SK_FIELD:
8393 inner = s->info->field->rec;
8394 aliases = usymtab_allAliases (inner);
8395 ct = sRef_getType (inner);
8396
8397
8398 sRefSet_realElements (aliases, current)
8399 {
8400 if (sRef_isValid (current))
8401 {
8402 current = sRef_updateSref (current);
8403
8404 if (ctype_equal (ct, sRef_getType (current)))
8405 {
8406 sRef f = sRef_makeField (current, s->info->field->field);
8407
8408 ((*predf)(f, t));
8409 }
8410 }
8411 } end_sRefSet_realElements;
8412
8413 sRefSet_free (aliases);
8414 break;
8415 case SK_CONJ:
8416 sRef_innerAliasSetCompleteParam (predf, s->info->conj->a, t);
8417 sRef_innerAliasSetCompleteParam (predf, s->info->conj->b, t);
8418 break;
8419 case SK_SPECIAL:
8420 case SK_ADR:
8421 case SK_TYPE:
8422 case SK_CONST:
8423 case SK_NEW:
8424 case SK_UNKNOWN:
8425 case SK_OBJECT:
8426 case SK_DERIVED:
8427 case SK_EXTERNAL:
8428 case SK_RESULT:
8429 break;
8430 }
8431}
8432
8433static void sRef_combineExKinds (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other)
8434{
8435 exkind a1 = sRef_getExKind (res);
8436 exkind a2 = sRef_getExKind (other);
8437
8438 if (a1 == a2 || a2 == XO_UNKNOWN)
8439 {
8440 ;
8441 }
8442 else if (a1 == XO_UNKNOWN)
8443 {
28bf4b0b 8444 res->expinfo = stateInfo_update (res->expinfo, other->expinfo);
616915dd 8445 res->expkind = a2;
8446 }
8447 else
8448 {
8449 res->expkind = XO_OBSERVER;
8450 }
8451}
8452
8453/*
8454** Currently, this is a very ad hoc implementation, with lots of fixes to
8455** make real code work okay. I need to come up with some more general
8456** rules or principles here.
8457*/
8458
8459static void
8460 sRef_combineAliasKindsError (/*@notnull@*/ sRef res,
8461 /*@notnull@*/ sRef other,
8462 clause cl, fileloc loc)
8463{
8464 bool hasError = FALSE;
8465 alkind ares = sRef_getAliasKind (res);
8466 alkind aother = sRef_getAliasKind (other);
8467
28bf4b0b 8468 sRef_checkMutable (res);
8469
616915dd 8470 if (alkind_isDependent (ares))
8471 {
8472 if (aother == AK_KEPT)
8473 {
28bf4b0b 8474 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
616915dd 8475 res->aliaskind = AK_KEPT;
8476 }
8477 else
8478 {
8479 if (aother == AK_LOCAL || aother == AK_STATIC
8480 || alkind_isTemp (aother))
8481 {
28bf4b0b 8482 DPRINTF (("Setting dependent: %s", sRef_unparseFull (res)));
616915dd 8483 res->aliaskind = AK_DEPENDENT;
8484 }
8485 }
8486 }
8487 else if (alkind_isDependent (aother))
8488 {
8489 if (ares == AK_KEPT)
8490 {
8491 res->aliaskind = AK_KEPT;
8492 }
8493 else
8494 {
8495 if (ares == AK_LOCAL || ares == AK_STATIC || alkind_isTemp (ares))
8496 {
28bf4b0b 8497 DPRINTF (("Setting dependent: %s", sRef_unparseFull (res)));
8498 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
616915dd 8499 res->aliaskind = AK_DEPENDENT;
8500 }
8501 }
8502 }
8503 else if ((ares == AK_LOCAL || ares == AK_UNIQUE
8504 || ares == AK_STATIC || alkind_isTemp (ares))
8505 && sRef_isFresh (other))
8506 {
8507 /*
8508 ** cases like: if (s == NULL) s = malloc...;
8509 ** don't generate errors
8510 */
8511
28bf4b0b 8512 if (usymtab_isAltDefinitelyNullDeep (res))
616915dd 8513 {
8514 res->aliaskind = ares;
8515 }
8516 else
8517 {
8518 hasError = TRUE;
8519 }
8520 }
8521 else if ((aother == AK_LOCAL || aother == AK_UNIQUE
8522 || aother == AK_STATIC || alkind_isTemp (aother))
8523 && sRef_isFresh (res))
8524 {
8525 /*
8526 ** cases like: if (s == NULL) s = malloc...;
8527 ** don't generate errors
8528 */
8529
28bf4b0b 8530 if (usymtab_isDefinitelyNullDeep (other))
616915dd 8531 {
28bf4b0b 8532 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
616915dd 8533 res->aliaskind = aother;
8534 }
8535 else
8536 {
8537 hasError = TRUE;
8538 }
8539 }
8540 else if (ares == AK_NEWREF && aother == AK_REFCOUNTED
8541 && sRef_isConst (other))
8542 {
8543 res->aliaskind = AK_NEWREF;
8544 }
8545 else if (aother == AK_NEWREF && ares == AK_REFCOUNTED
8546 && sRef_isConst (res))
8547 {
28bf4b0b 8548 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
616915dd 8549 res->aliaskind = AK_NEWREF;
8550 }
8551 else if (sRef_isLocalVar (res)
8552 && ((ares == AK_KEPT && aother == AK_LOCAL)
8553 || (aother == AK_KEPT && ares == AK_LOCAL)))
8554 {
8555 res->aliaskind = AK_KEPT;
8556 }
8557 else
8558 {
8559 hasError = TRUE;
8560 }
8561
8562 if (hasError)
8563 {
8564 if (sRef_isThroughArrayFetch (res))
8565 {
8566 if (optgenerror2
8567 (FLG_BRANCHSTATE, FLG_STRICTBRANCHSTATE,
8568 message
8569 ("Clauses exit with %q possibly referencing %s storage %s, "
8570 "%s storage %s",
8571 sRef_unparse (res),
8572 alkind_unparse (aother),
8573 clause_nameTaken (cl),
8574 alkind_unparse (ares),
8575 clause_nameAlternate (cl)),
8576 loc))
8577 {
8578 sRef_showAliasInfo (res);
8579 sRef_showAliasInfo (other);
8580 res->aliaskind = AK_ERROR;
8581 }
8582 else
8583 {
8584 if (ares == AK_KEPT || aother == AK_KEPT)
8585 {
8586 sRef_maybeKill (res, loc);
28bf4b0b 8587 }
616915dd 8588 }
8589 }
8590 else
8591 {
8592 if (optgenerror
8593 (FLG_BRANCHSTATE,
8594 message ("Clauses exit with %q referencing %s storage %s, "
8595 "%s storage %s",
8596 sRef_unparse (res),
8597 alkind_unparse (aother),
8598 clause_nameTaken (cl),
8599 alkind_unparse (ares),
8600 clause_nameAlternate (cl)),
8601 loc))
8602 {
8603 sRef_showAliasInfo (res);
8604 sRef_showAliasInfo (other);
8605
8606 res->aliaskind = AK_ERROR;
8607 }
8608 }
8609
8610 res->aliaskind = (sRef_isLocalVar (res) ? AK_LOCAL : AK_UNKNOWN);
8611 }
8612}
8613
8614static void
8615 sRef_combineAliasKinds (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other,
8616 clause cl, fileloc loc)
8617{
8618 alkind ares = sRef_getAliasKind (res);
8619 alkind aother = sRef_getAliasKind (other);
8620
28bf4b0b 8621 sRef_checkMutable (res);
8622
616915dd 8623 if (alkind_equal (ares, aother)
8624 || aother == AK_UNKNOWN
8625 || aother == AK_ERROR)
8626 {
8627 ; /* keep current state */
8628 }
8629 else if (sRef_isDead (res) || sRef_isDead (other))
8630 {
8631 /* dead error reported (or storage is dead) */
8632 res ->aliaskind = AK_ERROR;
8633 }
8634 else if (ares == AK_UNKNOWN || ares == AK_ERROR
8635 || sRef_isStateUndefined (res))
8636 {
28bf4b0b 8637 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
616915dd 8638 res->aliaskind = aother;
8639 }
8640 else if (sRef_isStateUndefined (other))
8641 {
8642 ;
8643 }
8644 else if (((ares == AK_UNIQUE || alkind_isTemp (ares))
8645 && aother == AK_LOCAL)
8646 || ((aother == AK_UNIQUE || alkind_isTemp (aother))
8647 && ares == AK_LOCAL))
8648 {
8649 if (ares != AK_LOCAL)
8650 {
28bf4b0b 8651 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
616915dd 8652 }
8653
8654 res->aliaskind = AK_LOCAL;
8655 }
8656 else if ((ares == AK_OWNED && aother == AK_FRESH)
8657 || (aother == AK_OWNED && ares == AK_FRESH))
8658 {
8659 if (ares != AK_FRESH)
8660 {
28bf4b0b 8661 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
616915dd 8662 }
8663
8664 res->aliaskind = AK_FRESH;
8665 }
8666 else if ((ares == AK_KEEP && aother == AK_FRESH) ||
8667 (aother == AK_KEEP && ares == AK_FRESH))
8668 {
8669 if (ares != AK_KEEP)
8670 {
28bf4b0b 8671 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
616915dd 8672 }
8673
8674 res->aliaskind = AK_KEEP;
8675 }
8676 else if ((ares == AK_LOCAL && aother == AK_STACK) ||
8677 (aother == AK_LOCAL && ares == AK_STACK))
8678 {
8679 if (ares != AK_STACK)
8680 {
28bf4b0b 8681 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
616915dd 8682 }
8683
8684 res->aliaskind = AK_STACK;
8685 }
8686 else if ((ares == AK_LOCAL
8687 && (aother == AK_OWNED && sRef_isLocalVar (other)))
8688 || (aother == AK_LOCAL
8689 && (ares == AK_OWNED && sRef_isLocalVar (res))))
8690 {
8691 if (ares != AK_LOCAL)
8692 {
28bf4b0b 8693 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
616915dd 8694 }
8695
8696 res->aliaskind = AK_LOCAL;
8697 }
8698 else if ((ares == AK_FRESH && alkind_isOnly (aother))
8699 || (aother == AK_FRESH && alkind_isOnly (ares)))
8700 {
8701 res->aliaskind = AK_FRESH;
8702 }
8703 else if ((aother == AK_FRESH && sRef_definitelyNull (res))
8704 || (ares == AK_FRESH && sRef_definitelyNull (other)))
8705 {
8706 if (ares != AK_FRESH)
8707 {
28bf4b0b 8708 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
616915dd 8709 res->aliaskind = AK_FRESH;
8710 }
8711 }
8712 else if ((sRef_isFresh (res) && sRef_isConst (other))
8713 || (sRef_isFresh (other) && sRef_isConst (res)))
8714 {
8715 /*
8716 ** for NULL constantants
8717 ** this is bogus!
8718 */
8719
8720 if (!sRef_isFresh (res))
8721 {
28bf4b0b 8722 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
616915dd 8723 }
8724
8725 res->aliaskind = AK_FRESH;
8726 }
8727 else if ((alkind_isStatic (aother) && sRef_isConst (res))
8728 || (alkind_isStatic (ares) && sRef_isConst (other)))
8729 {
8730 if (!alkind_isStatic (ares))
8731 {
28bf4b0b 8732 res->aliasinfo = stateInfo_update (res->aliasinfo, other->aliasinfo);
616915dd 8733 res->aliaskind = AK_STATIC;
8734 }
8735 }
8736 else
8737 {
8738 sRef_combineAliasKindsError (res, other, cl, loc);
8739 }
8740}
8741
8742static void sRef_combineDefState (/*@notnull@*/ sRef res,
8743 /*@notnull@*/ sRef other)
8744{
8745 sstate s1 = res->defstate;
8746 sstate s2 = other->defstate;
8747 bool flip = FALSE;
8748
28bf4b0b 8749 sRef_checkMutable (res);
8750
616915dd 8751 if (s1 == s2 || s2 == SS_UNKNOWN)
8752 {
8753 ;
8754 }
8755 else if (s1 == SS_UNKNOWN)
8756 {
8757 flip = TRUE;
8758 }
8759 else
8760 {
8761 switch (s1)
8762 {
8763 case SS_FIXED:
8764 if (s2 == SS_DEFINED)
8765 {
8766 break;
8767 }
8768 else
8769 {
8770 llcontbuglit ("ssfixed: not implemented");
8771 flip = TRUE;
8772 }
8773 break;
8774 case SS_DEFINED:
8775 flip = TRUE;
8776 break;
8777 case SS_PDEFINED:
8778 case SS_ALLOCATED:
8779 flip = (s2 != SS_DEFINED);
8780 break;
8781 case SS_HOFFA:
8782 case SS_RELDEF:
8783 case SS_UNUSEABLE:
8784 case SS_UNDEFINED:
8785 case SS_PARTIAL:
8786 case SS_UNDEFGLOB:
8787 case SS_KILLED:
8788 case SS_DEAD:
8789 case SS_SPECIAL:
8790 break;
8791 BADDEFAULT;
8792 }
8793 }
8794
8795 if (flip)
8796 {
28bf4b0b 8797 res->definfo = stateInfo_update (res->definfo, other->definfo);
616915dd 8798 res->defstate = s2;
8799 }
8800}
8801
8802extern /*@notnull@*/ sRef sRef_getConjA (sRef s)
8803{
8804 sRef ret;
8805 llassert (sRef_isConj (s));
8806
8807 ret = s->info->conj->a;
8808 llassert (ret != NULL);
8809 return ret;
8810}
8811
8812extern /*@notnull@*/ sRef sRef_getConjB (sRef s)
8813{
8814 sRef ret;
8815 llassert (sRef_isConj (s));
8816
8817 ret = s->info->conj->b;
8818 llassert (ret != NULL);
8819 return ret;
8820}
8821
8822extern /*@exposed@*/ sRef sRef_makeArrow (sRef s, /*@dependent@*/ cstring f)
8823{
8824 sRef p;
8825 sRef ret;
28bf4b0b 8826
8827 p = sRef_makePointer (s);
616915dd 8828 ret = sRef_makeField (p, f);
28bf4b0b 8829 return ret;
616915dd 8830}
8831
8832extern /*@exposed@*/ sRef sRef_buildArrow (sRef s, cstring f)
8833{
8834 sRef p;
8835 sRef ret;
8836
8837 p = sRef_buildPointer (s);
8838 ret = sRef_buildField (p, f);
8839
8840 return ret;
8841}
8842
8843static /*@null@*/ sinfo sinfo_copy (/*@notnull@*/ sRef s)
8844{
8845 sinfo ret;
8846
8847 switch (s->kind)
8848 {
8849 case SK_CVAR:
8850 ret = (sinfo) dmalloc (sizeof (*ret));
8851 ret->cvar = (cref) dmalloc (sizeof (*ret->cvar));
8852 ret->cvar->lexlevel = s->info->cvar->lexlevel;
8853 ret->cvar->index = s->info->cvar->index;
8854 break;
8855
8856 case SK_PARAM:
8857 ret = (sinfo) dmalloc (sizeof (*ret));
8858 ret->paramno = s->info->paramno;
28bf4b0b 8859 llassert (ret->paramno >= -1);
616915dd 8860 break;
8861
8862 case SK_ARRAYFETCH:
8863 ret = (sinfo) dmalloc (sizeof (*ret));
8864 ret->arrayfetch = (ainfo) dmalloc (sizeof (*ret->arrayfetch));
8865 ret->arrayfetch->indknown = s->info->arrayfetch->indknown;
8866 ret->arrayfetch->ind = s->info->arrayfetch->ind;
28bf4b0b 8867 ret->arrayfetch->arr = s->info->arrayfetch->arr; /* sRef_copy (s->info->arrayfetch->arr); */ /*@i32@*/
616915dd 8868 break;
8869
8870 case SK_FIELD:
8871 ret = (sinfo) dmalloc (sizeof (*ret));
8872 ret->field = (fldinfo) dmalloc (sizeof (*ret->field));
28bf4b0b 8873 ret->field->rec = s->info->field->rec; /* sRef_copy (s->info->field->rec); */ /*@i32@*/
616915dd 8874 ret->field->field = s->info->field->field;
8875 break;
8876
8877 case SK_OBJECT:
8878 ret = (sinfo) dmalloc (sizeof (*ret));
8879 ret->object = s->info->object;
8880 break;
8881
8882 case SK_PTR:
8883 case SK_ADR:
8884 case SK_DERIVED:
8885 case SK_EXTERNAL:
8886 ret = (sinfo) dmalloc (sizeof (*ret));
28bf4b0b 8887 ret->ref = s->info->ref; /* Ref_copy (s->info->ref); */
616915dd 8888 break;
8889
8890 case SK_CONJ:
8891 ret = (sinfo) dmalloc (sizeof (*ret));
8892 ret->conj = (cjinfo) dmalloc (sizeof (*ret->conj));
28bf4b0b 8893 ret->conj->a = s->info->conj->a; /* sRef_copy (s->info->conj->a); */
8894 ret->conj->b = s->info->conj->b; /* sRef_copy (s->info->conj->b);*/
616915dd 8895 break;
8896 case SK_SPECIAL:
8897 ret = (sinfo) dmalloc (sizeof (*ret));
8898 ret->spec = s->info->spec;
8899 break;
8900 case SK_UNCONSTRAINED:
8901 case SK_NEW:
8902 ret = (sinfo) dmalloc (sizeof (*ret));
8903 ret->fname = s->info->fname;
8904 break;
8905 case SK_RESULT:
8906 case SK_CONST:
8907 case SK_TYPE:
8908 case SK_UNKNOWN:
8909 llassertprint (s->info == NULL, ("s = %s", sRef_unparse (s)));
8910 ret = NULL;
8911 break;
8912 }
8913
8914 return ret;
8915}
8916
8917static /*@null@*/ sinfo sinfo_fullCopy (/*@notnull@*/ sRef s)
8918{
8919 sinfo ret;
8920
8921 /*
8922 ** Since its a full copy, only storage is assigned
8923 ** to dependent fields.
8924 */
8925 /*@-onlytrans@*/
8926
8927 switch (s->kind)
8928 {
8929 case SK_CVAR:
8930 ret = (sinfo) dmalloc (sizeof (*ret));
8931 ret->cvar = (cref) dmalloc (sizeof (*ret->cvar));
8932 ret->cvar->lexlevel = s->info->cvar->lexlevel;
8933 ret->cvar->index = s->info->cvar->index;
8934 break;
8935
8936 case SK_PARAM:
8937 ret = (sinfo) dmalloc (sizeof (*ret));
8938 ret->paramno = s->info->paramno;
28bf4b0b 8939 llassert (ret->paramno >= -1);
616915dd 8940 break;
8941
8942 case SK_ARRAYFETCH:
8943 ret = (sinfo) dmalloc (sizeof (*ret));
8944 ret->arrayfetch = (ainfo) dmalloc (sizeof (*ret->arrayfetch));
8945 ret->arrayfetch->indknown = s->info->arrayfetch->indknown;
8946 ret->arrayfetch->ind = s->info->arrayfetch->ind;
8947 ret->arrayfetch->arr = sRef_saveCopy (s->info->arrayfetch->arr);
8948 break;
8949
8950 case SK_FIELD:
8951 ret = (sinfo) dmalloc (sizeof (*ret));
8952 ret->field = (fldinfo) dmalloc (sizeof (*ret->field));
8953 ret->field->rec = sRef_saveCopy (s->info->field->rec);
8954 ret->field->field = s->info->field->field;
8955 break;
8956
8957 case SK_OBJECT:
8958 ret = (sinfo) dmalloc (sizeof (*ret));
8959 ret->object = s->info->object;
8960 break;
8961
8962 case SK_PTR:
8963 case SK_ADR:
8964 case SK_DERIVED:
8965 case SK_EXTERNAL:
8966 ret = (sinfo) dmalloc (sizeof (*ret));
8967 ret->ref = sRef_saveCopy (s->info->ref);
8968 break;
8969
8970 case SK_CONJ:
8971 ret = (sinfo) dmalloc (sizeof (*ret));
8972 ret->conj = (cjinfo) dmalloc (sizeof (*ret->conj));
8973 ret->conj->a = sRef_saveCopy (s->info->conj->a);
8974 ret->conj->b = sRef_saveCopy (s->info->conj->b);
8975 break;
8976 case SK_SPECIAL:
8977 ret = (sinfo) dmalloc (sizeof (*ret));
8978 ret->spec = s->info->spec;
8979 break;
8980 case SK_NEW:
8981 case SK_UNCONSTRAINED:
8982 ret = (sinfo) dmalloc (sizeof (*ret));
8983 ret->fname = s->info->fname;
8984 break;
8985 case SK_CONST:
8986 case SK_TYPE:
8987 case SK_RESULT:
8988 case SK_UNKNOWN:
8989 llassert (s->info == NULL);
8990 ret = NULL;
8991 break;
8992 }
8993 /*@=onlytrans@*/
8994 return ret;
8995}
8996
8997
8998static void
8999 sinfo_update (/*@notnull@*/ /*@exposed@*/ sRef res,
9000 /*@notnull@*/ /*@exposed@*/ sRef other)
9001{
9002 llassert (res->kind == other->kind);
9003
9004 switch (res->kind)
9005 {
9006 case SK_CVAR:
9007 res->info->cvar->lexlevel = other->info->cvar->lexlevel;
9008 res->info->cvar->index = other->info->cvar->index;
9009 break;
9010
9011 case SK_PARAM:
9012 res->info->paramno = other->info->paramno;
28bf4b0b 9013 llassert (res->info->paramno >= -1);
616915dd 9014 break;
9015
9016 case SK_ARRAYFETCH:
9017 res->info->arrayfetch->indknown = other->info->arrayfetch->indknown;
9018 res->info->arrayfetch->ind = other->info->arrayfetch->ind;
9019 res->info->arrayfetch->arr = other->info->arrayfetch->arr;
9020 break;
9021
9022 case SK_FIELD:
9023 res->info->field->rec = other->info->field->rec;
9024 res->info->field->field = other->info->field->field;
9025 break;
9026
9027 case SK_OBJECT:
9028 res->info->object = other->info->object;
9029 break;
9030
9031 case SK_PTR:
9032 case SK_ADR:
9033 case SK_DERIVED:
9034 case SK_EXTERNAL:
9035 res->info->ref = other->info->ref;
9036 break;
9037
9038 case SK_CONJ:
9039 res->info->conj->a = other->info->conj->a;
9040 res->info->conj->b = other->info->conj->b;
9041 break;
9042
9043 case SK_SPECIAL:
9044 res->info->spec = other->info->spec;
9045 break;
9046
9047 case SK_NEW:
9048 case SK_UNCONSTRAINED:
9049 res->info->fname = other->info->fname;
9050 break;
9051
9052 case SK_CONST:
9053 case SK_TYPE:
9054 case SK_UNKNOWN:
9055 case SK_RESULT:
9056 llassert (res->info == NULL);
9057 break;
9058 }
9059}
9060
9061static void sinfo_free (/*@special@*/ /*@temp@*/ /*@notnull@*/ sRef s)
9062 /*@uses s->kind, s->info@*/
9063 /*@releases s->info@*/
9064{
9065 switch (s->kind)
9066 {
9067 case SK_CVAR:
28bf4b0b 9068 DPRINTF (("Free sinfo: [%p]", s->info->cvar));
616915dd 9069 sfree (s->info->cvar);
9070 break;
9071
9072 case SK_PARAM:
9073 break;
9074
9075 case SK_ARRAYFETCH:
28bf4b0b 9076 DPRINTF (("Free sinfo: [%p]", s->info->arrayfetch));
616915dd 9077 sfree (s->info->arrayfetch);
9078 break;
9079
9080 case SK_FIELD:
28bf4b0b 9081 DPRINTF (("Free sinfo: [%p]", s->info->field));
616915dd 9082 sfree (s->info->field);
9083 break;
9084
9085 case SK_OBJECT:
9086 break;
9087
9088 case SK_PTR:
9089 case SK_ADR:
9090 case SK_DERIVED:
28bf4b0b 9091 case SK_EXTERNAL: /*@i32 is copy now! */
616915dd 9092 break;
9093
9094 case SK_CONJ:
28bf4b0b 9095 DPRINTF (("Free sinfo: [%p]", s->info->conj));
616915dd 9096 sfree (s->info->conj);
9097 break;
9098
9099 case SK_UNCONSTRAINED:
9100 case SK_SPECIAL:
9101 case SK_CONST:
9102 case SK_NEW:
9103 case SK_TYPE:
9104 case SK_UNKNOWN:
9105 case SK_RESULT:
9106 break;
9107 }
9108
28bf4b0b 9109 if (s->info != NULL) {
9110 DPRINTF (("Free sinfo: [%p]", s->info));
9111 }
9112
616915dd 9113 sfree (s->info);
9114}
9115
9116bool sRef_isNSLocalVar (sRef s)
9117{
9118 if (sRef_isLocalVar (s))
9119 {
9120 uentry ue = sRef_getUentry (s);
9121
9122 return (!uentry_isStatic (ue));
9123 }
9124 else
9125 {
9126 return FALSE;
9127 }
9128}
9129
9130bool sRef_isLocalVar (sRef s)
9131{
9132 if (sRef_isValid(s))
9133 {
9134 return (s->kind == SK_CVAR
9135 && (s->info->cvar->lexlevel > fileScope));
9136 }
9137
9138 return FALSE;
9139}
9140
9141bool sRef_isRealLocalVar (sRef s)
9142{
9143 if (sRef_isValid(s))
9144 {
9145 if (s->kind == SK_CVAR)
9146 {
9147 if (s->info->cvar->lexlevel == functionScope)
9148 {
9149 uentry ue = sRef_getUentry (s);
9150
9151 if (uentry_isAnyParam (ue)
9152 || uentry_isRefParam (ue))
9153 {
9154 return FALSE;
9155 }
9156 else
9157 {
9158 return TRUE;
9159 }
9160 }
9161 else
9162 {
9163 return (s->info->cvar->lexlevel > functionScope);
9164 }
9165 }
9166 }
9167
9168 return FALSE;
9169}
9170
9171bool sRef_isLocalParamVar (sRef s)
9172{
9173 if (sRef_isValid(s))
9174 {
9175 return (s->kind == SK_PARAM
9176 || (s->kind == SK_CVAR
9177 && (s->info->cvar->lexlevel > fileScope)));
9178 }
9179
9180 return FALSE;
9181}
9182
9183static speckind speckind_fromInt (int i)
9184{
9185 /*@+enumint@*/
9186 llassert (i >= SR_NOTHING && i <= SR_SYSTEM);
9187 /*@=enumint@*/
9188
9189 return ((speckind) i);
9190}
9191
28bf4b0b 9192
9193static void sRef_updateNullState (sRef res, sRef other)
9194 /*@modifies res@*/
9195{
9196 res->nullstate = other->nullstate;
9197 res->nullinfo = stateInfo_update (res->nullinfo, other->nullinfo);
9198}
9199
616915dd 9200void sRef_combineNullState (/*@notnull@*/ sRef res, /*@notnull@*/ sRef other)
9201{
28bf4b0b 9202 nstate n1 = sRef_getNullState (res);
9203 nstate n2 = sRef_getNullState (other);
616915dd 9204 bool flip = FALSE;
9205 nstate nn = n1;
9206
9207 if (n1 == n2 || n2 == NS_UNKNOWN)
9208 {
9209 ;
9210 }
9211 else
9212 {
9213 /* note: n2 is not unknown or defnull */
9214
9215 switch (n1)
9216 {
9217 case NS_ERROR: nn = NS_ERROR; break;
9218 case NS_UNKNOWN: flip = TRUE; nn = n2; break;
9219 case NS_POSNULL: break;
9220 case NS_DEFNULL: nn = NS_POSNULL; break;
9221 case NS_RELNULL: break;
9222 case NS_NOTNULL:
9223 if (n2 == NS_MNOTNULL)
9224 {
9225 ;
9226 }
9227 else
9228 {
9229 flip = TRUE;
9230 nn = NS_POSNULL;
9231 }
9232 break;
9233 case NS_MNOTNULL:
9234 if (n2 == NS_NOTNULL)
9235 {
9236 nn = NS_NOTNULL;
9237 }
9238 else
9239 {
9240 flip = TRUE;
9241 nn = NS_POSNULL;
9242 }
9243 break;
9244 case NS_CONSTNULL:
9245 case NS_ABSNULL:
9246 flip = TRUE;
9247 nn = n2;
9248 }
9249 }
9250
9251 if (flip)
9252 {
28bf4b0b 9253 res->nullinfo = stateInfo_update (res->nullinfo, other->nullinfo);
616915dd 9254 }
9255
9256 res->nullstate = nn;
9257}
9258
9259cstring sRef_nullMessage (sRef s)
9260{
9261 llassert (sRef_isValid (s));
9262
28bf4b0b 9263 switch (sRef_getNullState (s))
616915dd 9264 {
9265 case NS_DEFNULL:
9266 case NS_CONSTNULL:
9267 return (cstring_makeLiteralTemp ("null"));
9268 default:
9269 return (cstring_makeLiteralTemp ("possibly null"));
9270 }
9271 BADEXIT;
9272}
9273
9274/*@observer@*/ cstring sRef_ntMessage (sRef s)
9275{
9276 llassert (sRef_isValid (s));
9277
9278 switch (s->nullstate)
9279 {
9280 case NS_DEFNULL:
9281 case NS_CONSTNULL:
9282 return (cstring_makeLiteralTemp ("not nullterminated"));
9283 default:
9284 return (cstring_makeLiteralTemp ("possibly non-nullterminated"));
9285 }
9286 BADEXIT;
9287}
9288
9289
9290
9291sRef sRef_fixResultType (/*@returned@*/ sRef s, ctype typ, uentry ue)
9292{
9293 sRef tmp = sRef_undefined;
9294 sRef ret;
9295
9296 llassert (sRef_isValid (s));
9297
9298 switch (s->kind)
9299 {
9300 case SK_RESULT:
9301 s->type = typ;
9302 ret = s;
9303 break;
9304 case SK_ARRAYFETCH:
9305 {
9306 ctype ct;
9307 tmp = sRef_fixResultType (s->info->arrayfetch->arr, typ, ue);
9308
9309 ct = ctype_realType (sRef_getType (tmp));
9310
9311
9312 if (ctype_isKnown (ct))
9313 {
9314 if (ctype_isAP (ct))
9315 {
9316 ;
9317 }
9318 else
9319 {
9320 voptgenerror
9321 (FLG_TYPE,
9322 message
9323 ("Special clause indexes non-array (%t): *%q",
9324 ct, sRef_unparse (s->info->arrayfetch->arr)),
9325 uentry_whereLast (ue));
9326 }
9327 }
9328
9329 tmp = sRef_fixResultType (s->info->arrayfetch->arr, typ, ue);
9330
9331 if (s->info->arrayfetch->indknown)
9332 {
9333 ret = sRef_makeArrayFetchKnown (tmp, s->info->arrayfetch->ind);
9334 }
9335 else
9336 {
9337 ret = sRef_makeArrayFetch (tmp);
9338 }
9339 }
9340 break;
9341 case SK_FIELD:
9342 {
9343 sRef rec = sRef_fixResultType (s->info->field->rec, typ, ue);
9344 ctype ct = ctype_realType (sRef_getType (rec));
9345
9346 if (ctype_isKnown (ct))
9347 {
9348 if (ctype_isSU (ct))
9349 {
9350 if (uentry_isValid (uentryList_lookupField (ctype_getFields (ct),
9351 s->info->field->field)))
9352 {
9353 ;
9354 }
9355 else
9356 {
9357 voptgenerror
9358 (FLG_TYPE,
9359 message
9360 ("Special clause accesses non-existent field of result: %q.%s",
9361 sRef_unparse (rec), s->info->field->field),
9362 uentry_whereLast (ue));
9363 }
9364 }
9365 else
9366 {
9367 voptgenerror
9368 (FLG_TYPE,
9369 message
9370 ("Special clause accesses field of non-struct or union result (%t): %q.%s",
9371 ct, sRef_unparse (rec), s->info->field->field),
9372 uentry_whereLast (ue));
9373 }
9374 }
9375
9376 ret = sRef_makeField (tmp, s->info->field->field);
9377 break;
9378 }
9379 case SK_PTR:
9380 {
9381 ctype ct;
9382 tmp = sRef_fixResultType (s->info->ref, typ, ue);
9383
9384 ct = ctype_realType (sRef_getType (tmp));
9385
9386 if (ctype_isKnown (ct))
9387 {
9388 if (ctype_isAP (ct))
9389 {
9390 ;
9391 }
9392 else
9393 {
9394 voptgenerror
9395 (FLG_TYPE,
9396 message
9397 ("Special clause dereferences non-pointer (%t): *%q",
9398 ct, sRef_unparse (s->info->ref)),
9399 uentry_whereLast (ue));
9400 }
9401 }
9402
9403 ret = sRef_makePointer (tmp);
9404 break;
9405 }
9406 case SK_ADR:
9407 voptgenerror
9408 (FLG_TYPE,
9409 message
9410 ("Special clause uses & operator (not allowed): &%q", sRef_unparse (s->info->ref)),
9411 uentry_whereLast (ue));
9412 ret = s;
9413 break;
9414 BADDEFAULT;
9415 }
9416
9417 return ret;
9418}
9419
9420bool sRef_isOnly (sRef s)
9421{
9422 return (sRef_isValid(s) && alkind_isOnly (s->aliaskind));
9423}
9424
9425bool sRef_isDependent (sRef s)
9426{
9427 return (sRef_isValid(s) && alkind_isDependent (s->aliaskind));
9428}
9429
9430bool sRef_isOwned (sRef s)
9431{
9432 return (sRef_isValid (s) && (s->aliaskind == AK_OWNED));
9433}
9434
9435bool sRef_isKeep (sRef s)
9436{
9437 return (sRef_isValid (s) && (s->aliaskind == AK_KEEP));
9438}
9439
9440bool sRef_isTemp (sRef s)
9441{
9442 return (sRef_isValid (s) && alkind_isTemp (s->aliaskind));
9443}
9444
9445bool sRef_isLocalState (sRef s)
9446{
9447 return (sRef_isValid (s) && (s->aliaskind == AK_LOCAL));
9448}
9449
9450bool sRef_isUnique (sRef s)
9451{
9452 return (sRef_isValid (s) && (s->aliaskind == AK_UNIQUE));
9453}
9454
9455bool sRef_isShared (sRef s)
9456{
9457 return (sRef_isValid (s) && (s->aliaskind == AK_SHARED));
9458}
9459
9460bool sRef_isExposed (sRef s)
9461{
9462 return (sRef_isValid (s) && (s->expkind == XO_EXPOSED));
9463}
9464
9465bool sRef_isObserver (sRef s)
9466{
9467 return (sRef_isValid (s) && (s->expkind == XO_OBSERVER));
9468}
9469
9470bool sRef_isFresh (sRef s)
9471{
9472 return (sRef_isValid (s) && (s->aliaskind == AK_FRESH));
9473}
9474
9475bool sRef_isDefinitelyNull (sRef s)
9476{
28bf4b0b 9477 return (sRef_isValid (s) && (sRef_getNullState (s) == NS_DEFNULL
9478 || sRef_getNullState (s) == NS_CONSTNULL));
616915dd 9479}
9480
9481bool sRef_isAllocated (sRef s)
9482{
9483 return (sRef_isValid (s) && (s->defstate == SS_ALLOCATED));
9484}
9485
9486bool sRef_isStack (sRef s)
9487{
9488 return (sRef_isValid (s) && (s->aliaskind == AK_STACK));
9489}
9490
28bf4b0b 9491bool sRef_isNotNull (sRef s)
9492{
9493 return (sRef_isValid(s) && (sRef_getNullState (s) == NS_MNOTNULL
9494 || sRef_getNullState (s) == NS_NOTNULL));
9495}
9496
9497alkind sRef_getAliasKind (sRef s)
9498{
9499 if (sRef_isValid(s)) {
9500 llassert (alkind_isValid (s->aliaskind));
9501 return s->aliaskind;
9502 }
9503
9504 return AK_ERROR;
9505}
9506
9507nstate sRef_getNullState (sRef s)
9508{
9509 if (sRef_isValid (s)) {
9510 llassert (nstate_isValid (s->nullstate));
9511 return s->nullstate;
9512 }
9513
9514 return NS_UNKNOWN;
9515}
9516
9517void sRef_reflectAnnotation (sRef s, annotationInfo a, fileloc loc)
9518{
9519 if (sRef_isValid (s))
9520 {
9521 if (!valueTable_isDefined (s->state))
9522 {
9523 s->state = valueTable_create (1);
9524 valueTable_insert (s->state,
9525 cstring_copy (metaStateInfo_getName (annotationInfo_getState (a))),
9526 stateValue_create (annotationInfo_getValue (a), stateInfo_makeLoc (loc)));
9527 }
9528 else
9529 {
9530 DPRINTF (("reflect loc: %s", fileloc_unparse (loc)));
9531 valueTable_update
9532 (s->state,
9533 metaStateInfo_getName (annotationInfo_getState (a)),
9534 stateValue_create (annotationInfo_getValue (a), stateInfo_makeLoc (loc)));
9535 DPRINTF (("state info: %s", stateInfo_unparse (stateInfo_makeLoc (loc))));
9536 DPRINTF (("sref: %s", sRef_unparse (s)));
9537 DPRINTF (("sref: %s", sRef_unparseFull (s)));
9538 }
9539 }
9540}
9541
9542void sRef_setMetaStateValueComplete (sRef s, cstring key, int value, fileloc loc)
9543{
9544 sRefSet aliases = usymtab_allAliases (s);
9545
9546 sRef_setMetaStateValue (s, key, value, loc);
9547
9548 sRefSet_realElements (aliases, current)
9549 {
9550 if (sRef_isValid (current))
9551 {
9552 current = sRef_updateSref (current);
9553 sRef_setMetaStateValue (current, key, value, loc);
9554 }
9555 } end_sRefSet_realElements ;
9556
9557 sRefSet_free (aliases);
9558}
9559
9560void sRef_setMetaStateValue (sRef s, cstring key, int value, fileloc loc)
9561{
9562 sRef_checkMutable (s);
9563
9564 if (sRef_isValid (s))
9565 {
9566 if (!valueTable_isDefined (s->state))
9567 {
9568 DPRINTF (("inserting state: %s: %s %d", sRef_unparse (s), key, value));
9569 s->state = valueTable_create (1);
9570 valueTable_insert (s->state, cstring_copy (key),
9571 stateValue_create (value, stateInfo_makeLoc (loc)));
9572 }
9573 else
9574 {
9575 DPRINTF (("Updating state: %s: %s %d / %s", sRef_unparse (s), key, value,
9576 fileloc_unparse (loc)));
9577 valueTable_update
9578 (s->state, key, stateValue_create (value, stateInfo_makeLoc (loc)));
9579 DPRINTF (("After: %s", sRef_unparseFull (s)));
9580 }
9581 }
9582}
9583
9584bool sRef_checkMetaStateValue (sRef s, cstring key, int value)
9585{
9586 if (sRef_isValid (s))
9587 {
9588 if (valueTable_isDefined (s->state))
9589 {
9590 stateValue val;
9591
9592 DPRINTF (("check state: %s: %s %d", sRef_unparse (s), key, value));
9593
9594 val = valueTable_lookup (s->state, key);
9595 llassert (stateValue_isDefined (val));
9596 return (stateValue_isError (val)
9597 || stateValue_getValue (val) == value);
9598 }
9599 else
9600 {
9601 return TRUE;
9602 }
9603 }
9604 else
9605 {
9606 return TRUE;
9607 }
9608}
9609
9610/*@observer@*/ stateValue sRef_getMetaStateValue (sRef s, cstring key)
9611{
9612 if (sRef_isValid (s))
9613 {
9614 if (valueTable_isDefined (s->state))
9615 {
9616 stateValue val;
9617
9618 val = valueTable_lookup (s->state, key);
9619 llassert (stateValue_isDefined (val));
9620 return val;
9621 }
9622 else
9623 {
9624 return stateValue_undefined;
9625 }
9626 }
9627 else
9628 {
9629 return stateValue_undefined;
9630 }
9631}
9632
9633/*@observer@*/ valueTable sRef_getValueTable (sRef s)
9634{
9635 DPRINTF (("Get value table: %s", sRef_unparse (s)));
9636
9637 if (sRef_isValid (s))
9638 {
9639 llassert (sRef_isValid (s));
9640 DPRINTF (("Value table: %s", valueTable_unparse (s->state)));
9641 return s->state;
9642 }
9643 else
9644 {
9645 DPRINTF (("No value table!"));
9646 return valueTable_undefined;
9647 }
9648}
9649
9650bool sRef_makeStateSpecial (sRef s)
9651{
9652 /*
9653 ** Default defined state can be made special.
9654 */
9655
9656 llassert (sRef_isValid (s)); /*@i523 why doesn't null-checking work!??? */
9657
9658 if (s->defstate == SS_UNKNOWN || s->defstate == SS_DEFINED || s->defstate == SS_SPECIAL)
9659 {
9660 s->aliaskind = AK_IMPTEMP;
9661 s->defstate = SS_SPECIAL;
9662 DPRINTF (("Made special: %s", sRef_unparseFull (s)));
9663 return TRUE;
9664 }
9665 else
9666 {
9667 s->aliaskind = AK_IMPTEMP;
9668 s->defstate = SS_SPECIAL;
9669 return FALSE;
9670 }
9671}
9672
9673void sRef_markImmutable (sRef s)
616915dd 9674{
28bf4b0b 9675 if (sRef_isValid (s))
9676 {
9677 DPRINTF (("Mark immutable: %s", sRef_unparseFull (s)));
9678 s->immut = TRUE;
9679 }
9680}
9681
9682bool sRef_definitelyNullContext (sRef s)
9683{
9684 return (sRef_definitelyNull (s)
9685 || usymtab_isDefinitelyNullDeep (s));
9686}
9687
9688bool sRef_definitelyNullAltContext (sRef s)
9689{
9690 return (sRef_definitelyNull (s)
9691 || usymtab_isAltDefinitelyNullDeep (s));
616915dd 9692}
9693
28bf4b0b 9694
616915dd 9695/* start modifications */
28bf4b0b 9696struct s_bbufinfo sRef_getNullTerminatedState (sRef p_s) {
9697 struct s_bbufinfo BUFSTATE_UNKNOWN;
616915dd 9698 BUFSTATE_UNKNOWN.bufstate = BB_NOTNULLTERMINATED;
9699 BUFSTATE_UNKNOWN.size = 0;
9700 BUFSTATE_UNKNOWN.len = 0;
9701
9702 if (sRef_isValid(p_s))
9703 return p_s->bufinfo;
9704 return BUFSTATE_UNKNOWN;
9705}
9706
9707void sRef_setNullTerminatedState(sRef p_s) {
9708 if(sRef_isValid (p_s)) {
9709 p_s->bufinfo.bufstate = BB_NULLTERMINATED;
9710 } else {
9711 llfatalbug( message("sRef_setNT passed a invalid sRef\n"));
9712 }
9713}
9714
9715
9716void sRef_setPossiblyNullTerminatedState(sRef p_s) {
9717 if( sRef_isValid (p_s)) {
9718 p_s->bufinfo.bufstate = BB_POSSIBLYNULLTERMINATED;
9719 } else {
9720 llfatalbug( message("sRef_setPossNT passed a invalid sRef\n"));
9721 }
9722}
9723
9724void sRef_setNotNullTerminatedState(sRef p_s) {
9725 if( sRef_isValid (p_s)) {
9726 p_s->bufinfo.bufstate = BB_NOTNULLTERMINATED;
9727 } else {
9728 llfatalbug( message("sRef_unsetNT passed a invalid sRef\n"));
9729 }
9730}
9731
9732void sRef_setLen(sRef p_s, int len) {
9733 if( sRef_isValid (p_s) && sRef_isNullTerminated(p_s)) {
9734 p_s->bufinfo.len = len;
9735 } else {
9736 llfatalbug( message("sRef_setLen passed a invalid sRef\n"));
9737 }
9738}
9739
9740
9741void sRef_setSize(sRef p_s, int size) {
9742 if( sRef_isValid(p_s)) {
9743 p_s->bufinfo.size = size;
9744 } else {
9745 llfatalbug( message("sRef_setSize passed a invalid sRef\n"));
9746 }
9747}
9748
9749void sRef_resetLen(sRef p_s) {
9750 if (sRef_isValid (p_s)) {
9751 p_s->bufinfo.len = 0;
9752 } else {
9753 llfatalbug (message ("sRef_setLen passed an invalid sRef\n"));
9754 }
9755}
9756
9757/*drl7x 11/28/2000 */
9758
dc92450f 9759bool sRef_isFixedArray (sRef p_s) /*@*/ {
616915dd 9760 ctype c;
9761 c = sRef_getType (p_s);
9762 return ( ctype_isFixedArray (c) );
9763}
9764
dc92450f 9765long int sRef_getArraySize (sRef p_s) /*@*/ {
616915dd 9766 ctype c;
9767 llassert (sRef_isFixedArray(p_s) );
470b7798 9768 DPRINTF (( message ("sRef_getArraySize getting array size for %s", sRef_unparse(p_s) ) ));
9769
616915dd 9770 c = sRef_getType (p_s);
9771
9772 return (ctype_getArraySize (c) );
9773}
9774
9775
9776
28bf4b0b 9777
9778
9779
9780
9781
9782
This page took 1.696091 seconds and 5 git commands to generate.