2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 University of Virginia,
4 ** Massachusetts Institute of Technology
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ** General Public License for more details.
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
27 ** Lexically scoped symbol table.
29 ** Unlike a normal symbol table, this table splits to reflect branches.
31 ** This is necessary since symbol table entries also reflect
32 ** some state information, like initialization, that may be
33 ** different on different branches.
35 ** For each control structure we split:
37 ** { } - a normal scope (no control) US_NORMAL
39 ** if <pred> { } - US_TBRANCH true branch
40 ** while <pred> { } US_FBRANCH else branch
42 ** switch <pred> { } - US_SWITCH
44 ** case x: - case scope US_CBRANCH
45 ** Should be kept in a separate table, because of
49 # include "splintMacros.nf"
51 # include "structNames.h"
52 # include "exprChecks.h"
53 # include "transferChecks.h"
55 /* Needed to install macros when loading libraries */
58 # include "cpperror.h"
62 ** Keep track of type definitions inside a function.
65 static uentryList functypes = uentryList_undefined;
67 static bool dbgfree = FALSE;
68 static bool dbgload = TRUE;
73 ** Hack to prevent shadow errors from appearing when function parameters
74 ** are entered into a new scope.
77 static bool noshadowerror = FALSE;
80 ** Constraint: static variables can live in 1 or > 2.
82 ** except for tags. All tags must be at the global level.
83 ** Static tags are handled in a kludgey way.
87 ** utab is the universal symbol table
88 ** globtab is the global environment of utab
89 ** oldtab is needed for conversions in dumping (after sorting the table)
91 ** there can be only one usymtab!
94 static /*@checkedstrict@*/ /*@owned@*/ /*@notnull@*/ usymtab utab;
96 /* Reachable environments from from utab */
97 static /*@checkedstrict@*/ /*@notnull@*/ /*@dependent@*/ usymtab globtab;
98 static /*@checkedstrict@*/ /*@dependent@*/ usymtab filetab;
100 /* Used for sorting for dumpage. */
101 static /*@checkedstrict@*/ /*@owned@*/ usymtab oldtab;
103 static int usymtab_lexicalLevel (void) /*@globals utab@*/ ;
104 static bool usymtab_isAltDefinitelyNull (sRef p_s) /*@globals utab@*/ ;
105 static void refTable_free (/*@only@*/ /*@null@*/ refTable p_x, int p_nentries);
106 static ctype usymtab_suFieldsType (uentryList p_f, bool p_isStruct) /*@globals globtab@*/ ;
108 extern int usymtab_getCurrentDepth (void) /*@globals utab@*/
110 return utab->lexlevel;
114 usymtab_freeLevel (/*@notnull@*/ /*@only@*/ usymtab p_u)
115 /*@globals globtab, utab, filetab@*/ /*@modifies p_u@*/ ;
117 static bool usymtab_isDefinitelyNullAux (sRef p_s) /*@globals utab@*/ ;
118 static /*@only@*/ cstring usymtab_unparseStackTab (usymtab p_t);
119 static /*@exposed@*/ /*@dependent@*/ uentry
120 usymtab_getRefTab (/*@notnull@*/ usymtab p_u, int p_level, usymId p_index);
123 /* These are not used anymore... */
124 static /*@unused@*/ /*@only@*/ cstring
125 usymtab_unparseLocalAux (/*@notnull@*/ usymtab p_s);
126 static /*@unused@*/ /*@only@*/ cstring
127 usymtab_unparseLocalList (/*@notnull@*/ usymtab p_s);
130 static /*@only@*/ cstring usymtab_typeName (/*@notnull@*/ usymtab p_t);
131 static void usymtab_handleParams (void)
132 /*@globals utab, globtab, filetab@*/
133 /*@modifies utab@*/ ;
135 static /*@exposed@*/ /*@dependent@*/ uentry
136 usymtab_addRefEntry (/*@notnull@*/ usymtab p_s, cstring p_k);
137 static /*@exposed@*/ /*@dependent@*/ usymtab
138 usymtab_dropEnv (/*@notnull@*/ usymtab p_s);
139 static /*@exposed@*/ /*@dependent@*/ uentry
140 usymtab_getRefNoisy (/*@notnull@*/ usymtab p_s, int p_level, usymId p_index);
142 static /*@exposed@*/ /*@dependent@*/ uentry
143 usymtab_lookupQuietAux (usymtab p_s, cstring p_k, bool p_noalt);
145 static /*@exposed@*/ /*@dependent@*/ uentry
146 usymtab_lookupQuiet (usymtab p_s, cstring p_k);
148 static /*@exposed@*/ /*@dependent@*/ uentry
149 usymtab_lookupQuietNoAlt (usymtab p_s, cstring p_k);
151 static void usymtab_printAllAux (usymtab p_s) /*@modifies g_msgstream@*/ ;
152 static int usymtab_getIndex (/*@notnull@*/ usymtab p_s, cstring p_k);
153 static /*@exposed@*/ uentry usymtab_fetchIndex (/*@notnull@*/ usymtab p_s, int p_i);
154 static /*@exposed@*/ uentry
155 usymtab_lookupAux (usymtab p_s, cstring p_k);
156 static /*@exposed@*/ /*@dependent@*/ /*@notnull@*/ usymtab
157 usymtab_getFileTab (void) /*@globals filetab@*/ ;
158 static int refTable_lookup (/*@notnull@*/ usymtab p_ut, int p_level, int p_index);
159 static bool usymtab_mustBreak (usymtab p_s);
160 static bool usymtab_mustEscape (usymtab p_s);
162 static void recordFunctionType (uentry ue)
164 llassert (uentry_isDatatype (ue) || uentry_isAnyTag (ue)
165 || uentry_isEnumConstant (ue));
166 DPRINTF (("Function type: %s", uentry_unparseFull (ue)));
168 functypes = uentryList_add (functypes, ue);
172 static void clearFunctionTypes (void)
173 /*@modifies globtab@*/
175 uentryList_elements (functypes, el)
177 if (cstring_isDefined (uentry_rawName (el)))
179 if (cstringTable_isDefined (globtab->htable))
181 cstringTable_remove (globtab->htable, uentry_rawName (el));
184 uentry_setName (el, cstring_undefined);
188 llassert (uentry_isValid (el));
189 el->sref = sRef_undefined;
190 /*@noaccess uentry@*/
191 } end_uentryList_elements ;
193 uentryList_clear (functypes);
196 static /*@falsewhennull@*/ bool usymtab_isBranch (usymtab u)
198 return (usymtab_isDefined (u) &&
199 (u->kind == US_TBRANCH || u->kind == US_FBRANCH
200 || u->kind == US_CBRANCH || u->kind == US_SWITCH));
203 static bool usymtab_mustBreak (usymtab s)
205 llassert (s != GLOBAL_ENV);
206 return (s->mustBreak);
209 static bool usymtab_mustEscape (usymtab s)
211 llassert (s != GLOBAL_ENV);
212 return (exitkind_mustEscape (s->exitCode));
215 void usymtab_setMustBreak ()
218 llassert (utab != GLOBAL_ENV);
219 utab->mustBreak = TRUE;
222 void usymtab_setExitCode (exitkind ex)
225 llassert (utab != GLOBAL_ENV);
229 if (exitkind_mustEscape (ex))
231 utab->mustBreak = TRUE;
235 bool usymtab_isAltDefinitelyNullDeep (sRef s)
237 return (sRef_deepPred (usymtab_isAltDefinitelyNull, s));
240 static bool usymtab_isAltDefinitelyNull (sRef s)
248 llassert (utab->env != NULL);
250 /*@-mods@*/ /* These modifications are cancelled. */
251 utab->guards = utab->env->guards;
252 utab->env->guards = t;
255 llassert (usymtab_isDefined (utab));
256 res = usymtab_isDefinitelyNull (s);
259 ** This reports a spurious error. It is okay, because of
260 ** the nesting relationship of usymtab environments.
263 /*@-mods@*/ /* Cancelling modifications. */
264 /*@i1@*/ utab->env->guards = utab->guards;
265 /*@i1@*/ utab->guards = t;
271 static /*@notnull@*/ /*@special@*/ usymtab
272 usymtab_create (uskind kind, /*@keep@*/ usymtab env, bool nextlevel)
274 /*@post:isnull result->htable, result->guards, result->aliases@*/
276 usymtab t = (usymtab) dmalloc (sizeof (*t));
279 t->nspace = CBASESIZE;
280 t->entries = (uentry *) dmalloc (sizeof (*t->entries) * CBASESIZE);
282 /* We only use a reftable for branch-level symbol tables.
285 t->reftable = (nextlevel
287 : (refentry *) dmalloc (sizeof (*t->reftable) * CBASESIZE));
290 t->lexlevel = (env == GLOBAL_ENV ? 0 : env->lexlevel) + (nextlevel ? 1 : 0);
295 t->guards = guardSet_undefined;
296 t->aliases = aliasTable_undefined;
298 t->mustBreak = FALSE;
299 t->exitCode = XK_NEVERESCAPE;
308 static /*@only@*/ /*@notnull@*/ usymtab
309 usymtab_createRoot (void) /*@modifies nothing@*/
311 usymtab u = (usymtab) dmalloc (sizeof (*u));
314 u->nspace = CGLOBBASESIZE;
315 u->entries = (uentry *) dmalloc (sizeof (*u->entries) * CGLOBBASESIZE);
318 u->htable = cstringTable_create (CGLOBHASHSIZE);
321 u->guards = guardSet_new ();
322 u->aliases = aliasTable_new ();
324 u->mustBreak = FALSE;
325 u->exitCode = XK_NEVERESCAPE;
332 usymtab_initMod (void)
333 /*@globals undef utab, undef oldtab, undef globtab, undef filetab @*/
334 /*@modifies utab, globtab, filetab, oldtab @*/
336 utab = usymtab_createRoot ();
338 filetab = usymtab_undefined;
339 oldtab = usymtab_undefined;
344 usymtab_initGlobalMarker () /*@globals globtab@*/
346 if (uentry_isValid (usymtab_lookupAux (globtab, GLOBAL_MARKER_NAME)))
348 ; /* Already entered from load table. */
352 usymtab_addGlobalEntry (uentry_makeGlobalMarker ());
357 ** utab should be empty? (requires?)
359 ** Adds bool types to symbol table (these are built in for now)
360 ** Only do this is there is no library!
366 if (context_getFlag (FLG_NOLIB))
368 ctype boolt = ctype_bool;
369 /* evs 2000-07-24: bool is now treated as abstract (always) */
371 uentry boolentry = uentry_makeBoolDatatype (YES);
372 usymtab_supGlobalEntry (boolentry);
373 context_addBoolAccess ();
376 ** We supply values 0 and 1 for the constants, so things like
377 ** while (TRUE) can be interpreted correctly.
380 usymtab_supGlobalEntry
381 (uentry_makeConstantValue (context_getFalseName (), boolt,
382 fileloc_getBuiltin (), FALSE,
383 multiVal_makeInt (0)));
385 usymtab_supGlobalEntry
386 (uentry_makeConstantValue (context_getTrueName (), boolt,
387 fileloc_getBuiltin (), FALSE,
388 multiVal_makeInt (1)));
397 usymtab_grow (/*@notnull@*/ usymtab s)
400 o_uentry *oldsyms = s->entries;
402 s->nspace = CBASESIZE;
403 s->entries = (uentry *) dmalloc (sizeof (*s->entries)
404 * (s->nentries + s->nspace));
406 for (i = 0; i < s->nentries; i++)
408 s->entries[i] = oldsyms[i];
413 if (s->reftable != NULL)
415 refTable oldRefs = s->reftable;
417 s->reftable = (refentry *) dmalloc (sizeof (*s->reftable)
418 * (s->nentries + CBASESIZE + 1));
420 for (i = 0; i < s->nentries; i++)
422 s->reftable[i] = oldRefs[i];
433 usymtab_addEntryQuiet (/*@notnull@*/ usymtab s, /*@keep@*/ uentry e)
435 if (!(s->lexlevel > fileScope || !sRef_modInFunction ()))
437 if (uentry_isDatatype (e)
438 || uentry_isFunction (e)
439 || uentry_isAnyTag (e)
440 || uentry_isEnumConstant (e)
441 || context_inMacro ())
444 ** Not a bug. Code like,
445 ** int f (void) { typedef int tint; ... }
449 /* was nothing here! */
450 /*@i@*/ e->sref = sRef_saveCopy (e->sref);
455 (cstring_makeLiteral ("Declaration outside function scope"));
457 llcontbug (message ("usymtab_addEntryQuiet: inconsistent state "
458 "(lexlevel = %d, modFunction = %s) adding: %q",
459 s->lexlevel, bool_unparse (sRef_modInFunction ()),
460 uentry_unparse (e)));
461 sRef_setGlobalScope ();
462 /* make sure the sRef is not bashed! */
463 /*@i@*/ e->sref = sRef_saveCopy (e->sref);
473 s->entries[s->nentries] = e;
476 if (s == globtab || s == filetab)
478 if (!fileloc_isLib (g_currentloc))
480 uentry_tallyAnnots (e, AN_UNKNOWN);
485 if (cstringTable_isDefined (s->htable))
487 cstringTable_insert (s->htable, cstring_copy (uentry_rawName (e)), s->nentries);
493 static /*@observer@*/ uentry /*@alt void@*/
494 usymtab_addEntryBase (/*@notnull@*/ usymtab s, /*@only@*/ uentry e)
497 ** In theory, we shouldn't need this test because it this is
498 ** only called when a library is being read, and it shouldn't
499 ** ever have a duplicate entry. In practice, its safer to
500 ** leave it in, though.
505 if (uentry_isValid (old = usymtab_lookupQuiet (s, uentry_rawName (e))))
507 DPRINTF (("Duplicate entry: %s", uentry_unparse (e)));
508 uentry_free (e); /* duplicate */
513 int thisentry = s->nentries;
515 if (uentry_isVar (e))
517 uentry_setSref (e, sRef_makeCvar (globScope, thisentry,
519 stateInfo_makeLoc (uentry_whereLast (e))));
522 usymtab_addEntryQuiet (s, e);
528 static /*@observer@*/ uentry /*@alt void@*/
529 usymtab_addEntryAlways (/*@notnull@*/ usymtab s, /*@only@*/ uentry e)
532 ** In theory, we shouldn't need this test because it this is
533 ** only called when a library is being read, and it shouldn't
534 ** ever have a duplicate entry. In practice, its safer to
535 ** leave it in, though.
539 int thisentry = s->nentries;
541 if (uentry_isValid (old = usymtab_lookupQuiet (s, uentry_rawName (e))))
544 (message ("Duplicate entry in load library: %s. "
545 "Old entry: %q. New entry: %q",
547 uentry_unparseFull (old),
548 uentry_unparseFull (e)));
550 uentry_setName (e, message ("__x_%s", uentry_rawName (e)));
551 /* This shouldn't happen...unless the library is bad! */
555 if (uentry_isVar (e) && !uentry_isGlobalMarker (e))
557 uentry_setSref (e, sRef_makeCvar (globScope, thisentry,
559 stateInfo_makeLoc (uentry_whereLast (e))));
562 usymtab_addEntryQuiet (s, e);
567 usymtab_addEntryAux (/*@notnull@*/ usymtab st, /*@keep@*/ uentry e, bool isSref)
568 /*@globals globtab@*/
571 usymId thisentry = st->nentries;
573 llassert (!uentry_isElipsisMarker (e));
577 ** llassert (usymtab_lookupSafe (uentry_rawName (e)) == uentry_undefined);
580 llassertprint (uentry_isAnyTag (e) ? st == globtab : TRUE,
581 ("non-global tag: %s / %d", uentry_unparseFull (e), st->lexlevel));
583 if (st->lexlevel == fileScope
584 && (!(uentry_isStatic (e)) || uentry_isAnyTag (e)))
591 ctype ct = uentry_getType (e);
593 if (uentry_isFunction (e) && ctype_isFunction (ct))
595 ct = ctype_getReturnType (ct);
598 if (uentry_isStatic (e))
600 sRef sr = sRef_makeCvar (st->lexlevel, thisentry, ct, stateInfo_makeLoc (uentry_whereLast (e)));
602 if (sRef_isStack (sr) || sRef_isLocalState (sr))
604 sRef_setAliasKind (sr, AK_STATIC, uentry_whereLast (e));
605 sRef_setDefined (sr, uentry_whereLast (e));
608 uentry_setSref (e, sr);
612 uentry_setSref (e, sRef_makeCvar (st->lexlevel, thisentry, ct, stateInfo_makeLoc (uentry_whereLast (e))));
616 if (uentry_isDatatype (e))
619 uentry_setDatatype (e, thisentry);
622 if (uentry_isFunction (e))
624 ctype ct = uentry_getType (e);
626 if (ctype_isFunction (ct)
627 && uentryList_isMissingParams (ctype_argsFunction (ct)))
629 if (uentry_isDeclared (e))
633 message ("Function %q declared without parameter list",
635 uentry_whereDeclared (e));
641 message ("Function %q specified without parameter list",
643 uentry_whereSpecified (e));
648 if (st == globtab && !uentry_isSpecified (e))
650 exprChecks_checkExport (e);
653 uentry_checkName (e);
655 usymtab_addEntryQuiet (st, e);
656 DPRINTF (("Adding entry: [%p] %s", e, uentry_unparseFull (e)));
661 usymtab_addEntry (uentry e)
662 /*@globals utab, globtab@*/
663 /*@modifies utab, e@*/
665 llassertprint (!usymtab_exists (uentry_rawName (e)),
666 ("Entry already exists: %s", uentry_unparse (e)));
668 return usymtab_addEntryAux (utab, e, FALSE);
672 usymtab_addGlobalEntry (uentry e)
673 /*@modifies globtab, e@*/
675 DPRINTF (("Add global: %s / %s", uentry_unparse (e), bool_unparse (uentry_isForward (e))));
677 (void) usymtab_addEntryAux (globtab, e, FALSE);
681 ** supercede and replace entry use elements of entries array, and
682 ** assign an element to a new value, and then free the old value.
683 ** Need -deparrays to avoid errors for this.
689 usymtab_supEntryAux (/*@notnull@*/ usymtab st,
690 /*@only@*/ uentry e, bool isSref)
691 /*@globals globtab, filetab@*/
692 /*@modifies st, globtab, e@*/
694 cstring ename = uentry_rawName (e);
695 bool staticEntry = FALSE;
698 DPRINTF (("Sup entry aux: %s", uentry_unparseFull (e)));
700 /* static tags in global scope */
701 if (st->lexlevel == fileScope
702 && (!(uentry_isStatic (e)) || uentry_isAnyTag (e)))
704 eindex = usymtab_getIndex (st, ename);
706 if (eindex != NOT_FOUND)
708 uentry ce = st->entries[eindex];
712 message ("%s %q shadows static declaration",
713 ekind_capName (uentry_getKind (e)),
715 uentry_whereDeclared (e)))
717 uentry_showWhereLast (ce);
720 if (eindex == st->nentries - 1)
726 st->entries[eindex] = st->entries[st->nentries - 1];
729 if (cstringTable_isDefined (st->htable))
731 cstringTable_replaceKey (st->htable, uentry_rawName (ce),
732 cstring_copy (uentry_rawName (e)));
742 if (uentry_isStatic (e)) {
743 if (uentry_isFunction (e)) {
745 ** Static function declarations are at the file level,
746 ** even if they are in a deeper scope.
749 st = usymtab_getFileTab ();
752 if (!uentry_isAnyTag (e) && st == globtab)
754 st = usymtab_getFileTab ();
756 DPRINTF (("Static entry!"));
761 DPRINTF (("Using symtab: %s", usymtab_unparseLocalAux (st)));
763 eindex = usymtab_getIndex (st, ename);
765 if (eindex != NOT_FOUND)
767 uentry ce = st->entries[eindex];
769 DPRINTF (("Found entry: %s", uentry_unparse (ce)));
771 if (uentry_isPriv (ce)
772 /* || (fileloc_isImport (uentry_whereSpecified (ce))) */
773 || (uentry_isStatic (ce)
774 && uentry_isAnyTag (ce)
775 && (uentry_isDeclared (ce)
776 && !fileloc_sameFile (uentry_whereDefined (ce),
777 uentry_whereDefined (e)))))
781 ** if overloading import should do some checks!
784 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
786 DPRINTF (("Overloading!"));
788 st->entries[eindex] = e;
790 if (uentry_isDatatype (e))
792 uentry_setDatatype (e, eindex);
795 if (st == globtab && !uentry_isSpecified (e))
797 exprChecks_checkExport (e);
800 if (cstringTable_isDefined (st->htable))
802 cstringTable_replaceKey (st->htable, uentry_rawName (ce),
803 cstring_copy (uentry_rawName (e)));
809 else if (uentry_isSpecified (ce))
811 if (uentry_isSpecified (e))
813 DPRINTF (("Here we are: %s", uentry_unparseFull (e)));
815 if (fileloc_isImport (uentry_whereSpecified (ce)))
817 if (cstringTable_isDefined (st->htable))
819 cstringTable_replaceKey (st->htable,
821 cstring_copy (uentry_rawName (e)));
825 st->entries[eindex] = e;
828 if (uentry_isDatatype (e)) uentry_setDatatype (e, eindex);
832 if (fileloc_isImport (uentry_whereSpecified (e)))
838 /* Respecification errors already reported */
839 DPRINTF (("Respecification: %s / %s",
840 fileloc_unparse (uentry_whereSpecified (e)),
841 bool_unparse (fileloc_isSpec (uentry_whereSpecified (e)))));
843 if (uentry_isDatatype (e))
845 uentry_setDatatype (e, eindex);
848 if (cstringTable_isDefined (st->htable))
850 cstringTable_replaceKey (st->htable,
852 cstring_copy (uentry_rawName (e)));
855 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
857 st->entries[eindex] = e;
862 else /* e not specified */
864 DPRINTF (("Merging..."));
866 if (uentry_isDeclared (ce))
869 No - this can happen for internal typedefs
870 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
873 DPRINTF (("Merge defn"));
874 uentry_mergeDefinition (ce, e);
879 No - this can happen for internal typedefs
880 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
883 DPRINTF (("Merge entries..."));
884 uentry_mergeEntries (ce, e);
885 DPRINTF (("After: %s", uentry_unparseFull (ce)));
889 else /* ce not specified */
891 if (!(st->lexlevel > fileScope || !sRef_modInFunction ()))
893 if (uentry_isDatatype (e) || uentry_isAnyTag (e)
894 || uentry_isEnumConstant (e))
897 ** Not a bug. Code like,
898 ** int f (void) { typedef int tint; ... }
904 llcontbug (message ("usymtab_supEntryAux: inconsistent state "
905 "(lexlevel = %d, modFunction = %s) adding: %q",
906 st->lexlevel, bool_unparse (sRef_modInFunction ()),
907 uentry_unparse (e)));
909 if (sRef_modInFunction ())
911 /* make sure the sRef is not bashed! */
912 /*@i@*/ e->sref = sRef_saveCopy (e->sref);
917 DPRINTF (("Merge.."));
918 uentry_mergeDefinition (ce, e);
923 ctype ct = uentry_getType (ce);
925 if (uentry_isFunction (ce) && ctype_isFunction (ct))
927 ct = ctype_getReturnType (ct);
930 uentry_setSref (ce, sRef_makeCvar (st->lexlevel, eindex, ct, stateInfo_makeLoc (uentry_whereLast (ce))));
933 else /* no previous entry */
937 if (st->lexlevel == globScope
938 && !uentry_isStatic (e)
939 && !uentry_isExtern (e)
940 && usymtab_isDefined (filetab))
943 ** check if there is a static entry: (i.e.,
950 eindex = usymtab_getIndex (filetab, ename);
952 if (eindex != NOT_FOUND)
954 uentry ce = filetab->entries[eindex];
956 uentry_setStatic (e);
957 uentry_mergeDefinition (ce, e);
963 outer = usymtab_lookupQuiet (st->env, ename);
965 DPRINTF (("New : [%p] %s", e, uentry_unparseFull (e)));
966 DPRINTF (("Outer: [%p] %s", outer, uentry_unparseFull (outer)));
969 ** no previous definition, add the new one
973 && uentry_isValid (outer)
974 && !(uentry_isYield (e) || uentry_isYield (outer))
975 && fileloc_isDefined (uentry_whereLast (e))
976 && !fileloc_isXHFile (uentry_whereLast (e))
977 && fileloc_isDefined (uentry_whereLast (outer))
978 && !fileloc_isXHFile (uentry_whereLast (outer)))
980 if (!uentry_sameKind (outer, e))
986 if (ctype_isUnknown (uentry_getType (outer))
987 || uentry_isForward (outer))
995 message ("%s %q shadows outer declaration",
996 ekind_capName (uentry_getKind (e)),
998 uentry_whereLast (e)))
1000 uentry_showWhereLast (outer);
1006 if (st == globtab && context_getFlag (FLG_NEWDECL))
1010 message ("New declaration: %q", uentry_getName (e)),
1011 uentry_whereLast (e));
1014 eindex = usymtab_addEntryAux (st, e, isSref);
1018 return (staticEntry ? USYMIDINVALID : eindex);
1023 usymtab_replaceEntryAux (/*@notnull@*/ usymtab st, /*@only@*/ uentry e)
1024 /*@globals globtab@*/ /*@modifies st, e@*/
1026 cstring ename = uentry_rawName (e);
1029 /* static tags in global scope */
1030 eindex = usymtab_getIndex (st, ename);
1032 if (eindex != NOT_FOUND)
1034 uentry ce = st->entries[eindex];
1036 if (cstringTable_isDefined (st->htable))
1038 cstringTable_replaceKey (st->htable, uentry_rawName (ce),
1039 cstring_copy (uentry_rawName (e)));
1043 st->entries[eindex] = e;
1047 eindex = usymtab_addEntryAux (st, e, FALSE);
1054 void usymtab_supEntry (uentry e)
1055 /*@globals utab, filetab, globtab@*/
1056 /*@modifies utab, globtab, e@*/
1058 (void) usymtab_supEntryAux (utab, e, FALSE);
1062 ** this should be lots more efficient!
1065 static /*@exposed@*/ uentry
1066 usymtab_supEntryReturnAux (/*@notnull@*/ usymtab tab,
1067 /*@only@*/ uentry e, bool isref)
1068 /*@globals globtab, filetab@*/
1069 /*@modifies tab, globtab, e@*/
1071 cstring rawName = cstring_copy (uentry_rawName (e));
1072 bool stat = (tab == globtab) && uentry_isStatic (e);
1075 (void) usymtab_supEntryAux (tab, e, isref);
1079 ret = usymtab_lookupAux (filetab, rawName);
1083 ret = usymtab_lookupAux (tab, rawName);
1085 if (uentry_isInvalid (ret) && usymtab_isDefined (filetab))
1087 ret = usymtab_lookupAux (filetab, rawName);
1091 cstring_free (rawName);
1095 /*@dependent@*/ /*@exposed@*/ uentry
1096 usymtab_supEntryReturn (/*@only@*/ uentry e)
1097 /*@globals utab, filetab, globtab@*/
1098 /*@modifies utab, globtab, e@*/
1100 return (usymtab_supEntryReturnAux (utab, e, FALSE));
1103 /*@dependent@*/ /*@exposed@*/ uentry
1104 usymtab_supEntrySrefReturn (/*@only@*/ uentry e)
1105 /*@globals utab, globtab, filetab@*/
1106 /*@modifies utab, globtab, e@*/
1108 return (usymtab_supEntryReturnAux (utab, e, TRUE));
1111 /*@dependent@*/ /*@exposed@*/ uentry
1112 usymtab_supGlobalEntryReturn (uentry e)
1113 /*@globals globtab, filetab@*/
1114 /*@modifies globtab, e@*/
1118 ret = usymtab_supEntryReturnAux (globtab, e, FALSE);
1121 ** We need to keep track of internal function declarations, so
1122 ** we can remove them from the symbol table after exiting this
1123 ** function. This is a bit bogus, of course.
1126 if (sRef_modInFunction ())
1128 recordFunctionType (ret);
1135 usymtab_supTypeEntry (/*@only@*/ uentry e)
1136 /*@globals globtab, filetab@*/
1137 /*@modifies globtab, e@*/
1142 if (uentry_isAbstractDatatype (e))
1144 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1145 ret = ctype_createAbstract (uid);
1149 uid = usymtab_supEntryAux (globtab, e, FALSE);
1150 ret = ctype_createUser (uid);
1153 if (sRef_modInFunction ())
1155 recordFunctionType (globtab->entries[uid]);
1162 usymtab_supReturnTypeEntry (/*@only@*/ uentry e)
1163 /*@globals globtab, filetab@*/
1164 /*@modifies globtab@*/
1168 DPRINTF (("Abstract? %s", uentry_unparseFull (e)));
1170 if (uentry_isAbstractDatatype (e))
1172 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1174 else if (uentry_isMaybeAbstract (e) && context_getFlag (FLG_IMPABSTRACT))
1176 bool maybeabs = TRUE;
1177 cstring sname = uentry_getName (e);
1178 uentry ue = usymtab_lookupGlobSafe (sname);
1179 cstring_free (sname);
1181 if (uentry_isValid (ue))
1183 DPRINTF (("Lookup: %s", uentry_unparseFull (ue)));
1185 if (uentry_isDatatype (ue))
1187 if (uentry_isMaybeAbstract (ue))
1198 DPRINTF (("Not datatype!"));
1205 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1206 ux = usymtab_getTypeEntry (uid);
1207 uentry_setAbstract (ux);
1211 uid = usymtab_supEntryAux (globtab, e, FALSE);
1212 e = usymtab_getTypeEntry (uid);
1214 if (uentry_isMaybeAbstract (e))
1216 uentry_setConcrete (e);
1222 uid = usymtab_supEntryAux (globtab, e, FALSE);
1223 e = usymtab_getTypeEntry (uid);
1225 if (uentry_isMaybeAbstract (e))
1227 uentry_setConcrete (e);
1231 if (sRef_modInFunction ())
1233 recordFunctionType (globtab->entries[uid]);
1236 return (globtab->entries[uid]);
1240 usymtab_supAbstractTypeEntry (/*@only@*/ uentry e, bool dodef)
1241 /*@globals globtab, filetab@*/
1242 /*@modifies globtab, e@*/
1247 uid = usymtab_supEntryAux (globtab, e, FALSE);
1248 ue = usymtab_getTypeEntry (uid);
1252 uentry_setDatatype (ue, uid);
1255 if (context_getFlag (FLG_ACCESSMODULE)) /* was accessfile */
1257 context_addFileAccessType (uid);
1260 if (sRef_modInFunction ())
1262 recordFunctionType (globtab->entries[uid]);
1270 usymtab_supExposedTypeEntry (/*@only@*/ uentry e, bool dodef)
1271 /*@globals globtab, filetab@*/
1272 /*@modifies globtab, e@*/
1276 uid = usymtab_supEntryAux (globtab, e, FALSE);
1280 uentry ue = usymtab_getTypeEntry (uid);
1282 uentry_setDatatype (ue, uid);
1285 if (sRef_modInFunction ())
1287 recordFunctionType (globtab->entries[uid]);
1295 usymtab_supForwardTypeEntry (/*@only@*/ uentry e)
1296 /*@globals globtab, filetab@*/
1297 /*@modifies globtab, e@*/
1299 usymId uid = usymtab_supEntryAux (globtab, e, FALSE);
1300 uentry ue = usymtab_getTypeEntry (uid);
1302 uentry_setDatatype (ue, uid);
1304 if (sRef_modInFunction ())
1306 recordFunctionType (globtab->entries[uid]);
1309 return (uentry_getAbstractType (ue));
1313 usymtab_supEntrySref (uentry e)
1314 /*@globals utab, globtab, filetab@*/
1315 /*@modifies utab, globtab, e@*/
1317 sRef old = uentry_getSref (e);
1320 if (sRef_isType (old))
1322 uentry ue = usymtab_supEntryReturnAux (utab, e, TRUE);
1325 if (uentry_isValid (ue))
1327 sRef uref = uentry_getSref (ue);
1329 sRef_mergeStateQuiet (uref, old);
1330 sRef_clearDerived (uref);
1332 /*@noaccess uentry@*/
1334 else if (sRef_isKnown (old))
1336 usymtab_supEntry (e);
1340 (void) usymtab_supEntryAux (utab, e, TRUE);
1344 void usymtab_supGlobalEntry (/*@only@*/ uentry e)
1345 /*@globals globtab, filetab@*/
1346 /*@modifies globtab, filetab, e@*/
1350 DPRINTF (("Sup global entry: %s", uentry_unparse (e)));
1352 uid = usymtab_supEntryAux (globtab, e, FALSE);
1354 if (sRef_modInFunction ())
1356 recordFunctionType (globtab->entries[uid]);
1361 usymtab_supReturnFileEntry (/*@only@*/ uentry e)
1362 /*@globals filetab, globtab@*/
1363 /*@modifies filetab, globtab, e@*/
1365 llassert (filetab != usymtab_undefined);
1366 DPRINTF (("File entry: %s", uentry_unparse (e)));
1367 return (usymtab_supEntryReturnAux (filetab, e, FALSE));
1375 usymtab_inDeepScope () /*@globals utab@*/
1377 return (utab->lexlevel > paramsScope);
1381 usymtab_getIndex (/*@notnull@*/ usymtab s, cstring k)
1385 DPRINTF (("Lookup %s", k));
1387 if (cstringTable_isDefined (s->htable))
1389 i = cstringTable_lookup (s->htable, k);
1394 for (i = 0; i < s->nentries; i++)
1396 uentry current = s->entries[i];
1398 DPRINTF (("Check %d: %s", i, uentry_rawName (current)));
1400 if (!uentry_isUndefined (current)
1401 && cstring_equal (uentry_rawName (current), k))
1412 usymtab_fetchIndex (/*@notnull@*/ usymtab s, int i)
1414 llassert (i >= 0 && i < s->nentries);
1415 return (s->entries[i]);
1419 usymtab_getTypeId (cstring k) /*@globals globtab@*/
1421 usymId uid = usymtab_getIndex (globtab, k);
1423 if (uid == NOT_FOUND) return USYMIDINVALID;
1425 if (!(uentry_isDatatype (usymtab_getTypeEntry (uid)))) {
1426 return USYMIDINVALID;
1432 /*@dependent@*/ uentry
1433 usymtab_lookupStructTag (cstring k)
1435 cstring sname = makeStruct (k);
1436 uentry ue = usymtab_lookupGlob (sname);
1438 cstring_free (sname);
1442 /*@dependent@*/ uentry
1443 usymtab_lookupUnionTag (cstring k)
1445 cstring uname = makeUnion (k);
1446 uentry res = usymtab_lookupGlob (uname);
1448 cstring_free (uname);
1452 /*@dependent@*/ uentry
1453 usymtab_lookupEnumTag (cstring k)
1455 cstring ename = makeEnum (k);
1456 uentry res = usymtab_lookupGlob (ename);
1458 cstring_free (ename);
1463 usymtab_getId (cstring k) /*@globals globtab@*/
1465 usymId uid = usymtab_getIndex (globtab, k);
1468 if (uid == NOT_FOUND)
1470 return USYMIDINVALID;
1473 ue = usymtab_getGlobalEntry (uid);
1475 if (uentry_isPriv (ue))
1477 return USYMIDINVALID;
1483 static /*@exposed@*/ uentry
1484 usymtab_getEntryAux (/*@notnull@*/ usymtab s, usymId uid)
1486 llassert (uid != USYMIDINVALID);
1488 if (uid < 0 || uid >= s->nentries)
1490 llcontbug (message ("usymtab_getEntry: out of range: level = %d [%d]",
1492 return uentry_undefined;
1495 llassertprint (uentry_isValid (s->entries[uid]),
1496 ("entry undefined: %d", uid));
1498 return s->entries[uid];
1501 /*@dependent@*/ /*@observer@*/ uentry
1502 usymtab_getGlobalEntry (usymId uid)
1503 /*@globals utab, globtab@*/
1505 if (dbgfree) return (uentry_undefined);
1507 if (utab->lexlevel > paramsScope)
1509 /* need to do this the awkward way, since it could be in conditional scope */
1510 return (usymtab_lookupSafe (uentry_rawName (globtab->entries[uid])));
1514 return (globtab->entries[uid]);
1518 /*@dependent@*/ /*@exposed@*/ uentry
1519 usymtab_getTypeEntry (usymId uid)
1520 /*@globals globtab@*/
1524 if (uid >= 0 && uid < globtab->nentries)
1526 return (globtab->entries[uid]);
1530 return (uentry_undefined);
1535 llassert (uid >= 0 && uid < globtab->nentries);
1536 return (globtab->entries[uid]);
1544 /*@dependent@*/ /*@exposed@*/ uentry
1545 usymtab_getTypeEntrySafe (usymId uid)
1546 /*@globals globtab@*/
1548 if (uid < 0 || uid >= globtab->nentries)
1550 return uentry_undefined;
1553 return (globtab->entries[uid]);
1557 usymtab_isBoolType (usymId uid)
1558 /*@globals globtab@*/
1560 llassert (uid >= 0 && uid < globtab->nentries);
1562 return (cstring_equal (uentry_rawName (globtab->entries[uid]),
1563 context_getBoolName ()));
1567 usymtab_getTypeEntryName (usymId uid)
1568 /*@globals globtab@*/
1574 return (cstring_makeLiteral ("<freetype>"));
1577 ue = usymtab_getTypeEntry (uid);
1579 if (dbgload && !uentry_isValid (ue))
1581 return (message ("<missing type: %d>", uid));
1584 llassertprint (uentry_isValid (ue), ("type undefined: %d", uid));
1586 return (uentry_getName (ue));
1590 /*@unused@*/ static void
1591 usymtab_rehash (/*@notnull@*/ usymtab s)
1595 if (cstringTable_isDefined (s->htable))
1597 cstringTable_free (s->htable);
1600 s->htable = cstringTable_create (LLHASHSIZE);
1602 for (i = 0; i < s->nentries; i++)
1604 cstringTable_insert (s->htable, cstring_copy (uentry_rawName (s->entries[i])), i);
1610 ** superficial copy of usymtab
1612 ** DO copy spec entries
1615 static /*@only@*/ /*@notnull@*/ usymtab
1616 usymtab_shallowCopy (/*@notnull@*/ usymtab s) /*@*/
1618 usymtab copytab = usymtab_createRoot ();
1621 for (i = 0; i < s->nentries; i++)
1623 usymtab_addEntryBase (copytab, s->entries[i]);
1630 usymtab_shallowFree (/*@only@*/ /*@notnull@*/ usymtab s)
1632 aliasTable_free (s->aliases);
1633 refTable_free (s->reftable, s->nentries);
1635 /*@-compdestroy@*/ sfree (s); /*@=compdestroy@*/
1639 ** converts usymId from old table to sorted one
1643 usymtab_convertId (usymId uid)
1644 /*@globals oldtab, utab@*/
1650 llassert (usymtab_isDefined (oldtab));
1652 ue = usymtab_getEntryAux (oldtab, uid);
1654 llassertprint (uentry_isValid (ue), ("convertId: undefined: %d", uid));
1656 name = uentry_rawName (ue);
1658 ret = usymtab_getIndex (utab, name);
1659 llassert (ret == uid); /*! for now, no rehash! */
1660 DPRINTF (("Convert: %s [%d] -> %s [%d]",
1661 uentry_unparse (ue), uid,
1662 uentry_unparse (utab->entries[ret]), ret));
1664 llassertprint (ret != USYMIDINVALID, ("convertId: return is invalid"));
1670 usymtab_prepareDump (void)
1671 /*@globals oldtab, utab@*/
1672 /*@modifies oldtab, utab@*/
1674 llassert (usymtab_inGlobalScope ());
1675 llassert (oldtab == usymtab_undefined);
1678 DPRINTF (("Preparing to dump:"));
1679 usymtab_printAll ();
1682 oldtab = usymtab_shallowCopy (utab);
1685 ** alpha compare - make sure order is same on different platforms
1686 ** error messages appear in same order
1690 qsort (utab->entries, (size_t)utab->nentries,
1691 sizeof (*utab->entries),
1692 (int (*)(const void *, const void *)) uentry_xcomparealpha);
1694 usymtab_rehash (utab);
1698 DPRINTF (("After rehash:"));
1699 usymtab_printAll ();
1703 void usymtab_dump (FILE *fout)
1704 /*@globals utab, oldtab@*/
1707 bool neednl = FALSE;
1708 uentry lastentry = uentry_undefined;
1709 ekind lastekind = KINVALID;
1713 ** must call prepareDump first
1716 llassert (oldtab != usymtab_undefined);
1718 for (i = 0; i < utab->nentries; i++)
1720 uentry thisentry = utab->entries[i];
1721 ekind thisekind = uentry_getKind (thisentry);
1723 if (!uentry_hasRealName (thisentry))
1725 llassert (uentry_isGlobalMarker (thisentry));
1729 check (fputc ('\n', fout) == (int) '\n');
1732 fprintf (fout, "*%d (GlobalMarker)\n", KGLOBALMARKER);
1733 lastekind = KINVALID;
1739 if (thisekind != lastekind)
1743 check (fputc ('\n', fout) == (int) '\n');
1747 lastentry = uentry_undefined;
1748 fprintf (fout, "*%d (%s)\n", ekind_toInt (thisekind),
1749 cstring_toCharsSafe (ekind_capName (thisekind)));
1750 lastekind = thisekind;
1755 ** evans - 2001-02-18 - added the - 48 fudge factor...
1756 ** extra characters dumped, but I haven't counded them carefully...
1759 if (uentry_isInvalid (lastentry) || !uentry_equiv (lastentry, thisentry)
1760 || (linelen > (MAX_DUMP_LINE_LENGTH - (2 * MAX_NAME_LENGTH) - 48)))
1764 DPRINTF (("Dumping entry: %d", i));
1765 cdump = message ("^%d %q", i, uentry_dump (thisentry));
1766 /* was: cdump = uentry_dump (thisentry)); */
1768 lastentry = thisentry;
1771 check (fputc ('\n', fout) == (int) '\n');
1775 linelen += cstring_length (cdump);
1777 /* no new line here! */
1778 if (cstring_length (cdump) > 0)
1780 check (fputs (cstring_toCharsSafe (cdump), fout) != EOF);
1783 cstring_free (cdump);
1788 cstring cdump = uentry_rawName (thisentry);
1789 DPRINTF (("Raw name: %s", cdump));
1790 linelen += (cstring_length (cdump) + 1);
1791 fprintf (fout, "#%s", cstring_toCharsSafe (cdump));
1798 check (fputc ('\n', fout) == (int) '\n');
1801 lastekind = KINVALID;
1803 fprintf(fout, ";; Library constraints\n");
1804 for (i = 0; i < utab->nentries; i++)
1806 uentry thisentry = utab->entries[i];
1808 if (uentry_isFunction (thisentry) )
1810 constraintList preconditions;
1811 constraintList postconditions;
1813 preconditions = uentry_getFcnPreconditions (thisentry);
1814 postconditions = uentry_getFcnPostconditions (thisentry);
1816 if ( constraintList_isDefined(preconditions) ||
1817 constraintList_isDefined(postconditions) )
1819 fprintf(fout,"%s\n", cstring_toCharsSafe (uentry_rawName(thisentry) ) );
1820 if (constraintList_isDefined(preconditions) )
1822 fprintf(fout,"pre:\n");
1823 constraintList_dump(preconditions, fout);
1824 fprintf (fout, ";; end precondition constraints\n" );
1825 constraintList_free(preconditions);
1829 fprintf(fout,"pre:EMPTY\n");
1831 if (constraintList_isDefined(postconditions) )
1833 fprintf(fout,"post:\n");
1834 constraintList_dump(postconditions, fout);
1835 fprintf (fout, ";; end precondition constraints\n" );
1836 constraintList_free(postconditions);
1840 fprintf(fout,"post:EMPTY\n");
1849 void usymtab_load (FILE *f)
1850 /*@globals utab, globtab@*/
1851 /*@modifies utab, *f@*/
1853 char *s = mstring_create (MAX_DUMP_LINE_LENGTH);
1855 ekind kind = KINVALID;
1856 fileloc loc = g_currentloc;
1862 llassert (utab == globtab);
1863 llassert (utab->nentries == 0);
1865 while (((s = reader_readLine (f, s, MAX_DUMP_LINE_LENGTH)) != NULL)
1868 /* ignore ; comments */ ;
1871 while (s != NULL && *s != ';')
1879 ek = reader_getInt (&s);
1881 if (ek == KGLOBALMARKER)
1883 uentry lue = uentry_makeGlobalMarker ();
1884 DPRINTF (("Adding global marker: %s", uentry_unparseFull (lue)));
1885 usymtab_addEntryAlways (utab, lue);
1891 kind = ekind_fromInt (ek);
1899 (cstring_makeLiteral
1900 ("Library is in obsolete format. Use splint +whichlib "
1901 "to see which library is being loaded."));
1904 if (reader_optCheckChar (&s, '^'))
1906 index = reader_getInt (&s);
1913 llassert (kind != KINVALID);
1914 ue = uentry_undump (kind, loc, &s);
1916 llassert (utab->nentries == index || index == -1);
1918 if (uentry_isValid (ue))
1920 int lastindex = utab->nentries;
1921 ue = usymtab_addEntryAlways (utab, ue);
1925 if (uentry_isConstant (ue)) /*@i23! isPreProcessorMacro */
1927 cstring uname = uentry_getName (ue);
1929 /* Also check its a macro... */
1930 DPRINTF (("Installing: %s", uname));
1932 cpphash_installMacro
1933 (mstring_copy (cstring_toCharsSafe (uname)),
1934 cstring_length (uname),
1935 cpplib_createDefinition (message ("%s 255", uname),
1938 cpphash_hashCode (cstring_toCharsSafe (uname),
1939 cstring_length (uname),
1942 DPRINTF (("After install: %s", uname));
1946 if (utab->nentries != lastindex + 1)
1948 DPRINTF (("No add: %s", uentry_unparseFull (ue)));
1956 ** now, any other names are the same uentry
1959 while (*(s++) == '#')
1961 cstring name = cstring_fromCharsO (reader_getWord (&s));
1962 uentry nue = uentry_nameCopy (name, ue);
1964 DPRINTF (("Name copy: %s", uentry_unparseFull (nue)));
1968 usymtab_addEntryAlways (utab, nue);
1971 while ((c = *s) != '\0' && (c !='\n'))
1973 if (c != ' ' || c != '\t')
1975 llbuglit ("Junk in load file");
1983 s = reader_readLine (f, os, MAX_DUMP_LINE_LENGTH);
1988 to handle reading of buffer overflow related constraints
1990 while (fgets (s, MAX_DUMP_LINE_LENGTH, f) != NULL
1993 ; /* ignore ;-comments */
1996 while (s != NULL && *s != ';')
1998 constraintList preconditions;
1999 constraintList postconditions;
2001 cstring name = cstring_fromChars(reader_getWord(&s) );
2003 ue = usymtab_lookup ( name );
2007 preconditions = constraintList_undefined;
2008 postconditions = constraintList_undefined;
2010 if (!uentry_isValid(ue) )
2012 llfatalbug ((message("Invalid uentry for %s library file may be corrupted", cstring_fromChars(s) ) ));
2014 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
2016 temp = cstring_fromChars (reader_getWord(&s) );
2018 if (cstring_compareLit (temp,"pre:") == 0 )
2020 preconditions = constraintList_undump (f);
2024 if (cstring_compareLit (temp,"pre:EMPTY") != 0 )
2025 llfatalbug ((message("Error reading library file pre:EMPTY expected but got %s", temp ) ));
2030 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
2032 temp = cstring_fromChars(reader_getWord(&s) );
2033 if (cstring_compareLit (temp, "post:") == 0 )
2035 postconditions = constraintList_undump (f);
2039 if (cstring_compareLit (temp, "post:EMPTY") != 0 )
2040 llfatalbug ((message("Error reading library file post:EMPTY expected but got %s", temp ) ));
2043 cstring_free (temp);
2045 uentry_setPreconditions (ue, functionConstraint_createBufferConstraint (preconditions));
2046 uentry_setPostconditions (ue, functionConstraint_createBufferConstraint (postconditions));
2048 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
2056 ** file scope for static variables
2060 usymtab_enterFile ()
2061 /*@globals utab, globtab, filetab@*/
2062 /*@modifies filetab@*/
2064 llassert (utab == globtab);
2069 usymtab_entries (globtab, ue)
2071 if (sRef_hasDerived (uentry_getSref (ue)))
2073 fprintf (g_msgstream, "Derived Global: %s\n", uentry_unparse (ue));
2074 fprintf (g_msgstream, "sRef: %s\n", sRef_unparseFull (ue->sref));
2076 } end_usymtab_entries ;
2080 usymtab_enterScope ();
2086 /*@globals utab, filetab@*/
2087 /*@modifies filetab, utab@*/
2090 llassert (utab->lexlevel == 1);
2092 usymtab_exitScope (exprNode_undefined);
2097 usymtab_enterScope ()
2098 /*@globals utab, globtab, filetab@*/
2101 usymtab t = usymtab_create (US_NORMAL, utab, TRUE);
2103 /* unconditional scope: optimize to avoid copy */
2104 t->aliases = aliasTable_copy (utab->aliases);
2107 llassert (usymtab_isDefined (t->env));
2109 if (t->env->lexlevel == paramsScope && context_inFunctionLike ())
2111 noshadowerror = TRUE;
2112 usymtab_handleParams ();
2113 noshadowerror = FALSE;
2118 ** setup external references:
2119 ** o only, unique params alias external args
2120 ** o other params may alias anything of their type
2124 usymtab_handleParams (void)
2125 /*@globals utab, globtab, filetab@*/
2126 /*@modifies utab, globtab@*/
2128 usymtab ptab = utab->env;
2129 uentry fcn = context_getHeader ();
2131 usymtab_entries (ptab, param)
2135 if (!uentry_isYield (param))
2138 sRef pref = uentry_getSref (param);
2140 /* Could be a global. */
2142 if (uentry_isAnyParam (param))
2144 ue = uentry_makeVariable (fixParamName (uentry_rawName (param)),
2145 uentry_getType (param),
2146 fileloc_copy (uentry_whereDeclared (param)),
2149 uentry_copyState (ue, param);
2150 uentry_setRefParam (ue);
2152 ue = usymtab_supEntrySrefReturn (ue);
2154 /* must be after supercede! */
2156 if (!sRef_stateKnown (pref))
2158 uentry_setDefState (ue, SS_DEFINED);
2159 uentry_setDefState (param, SS_DEFINED);
2163 if (sRef_isStateSpecial (pref))
2165 uentry_setDefState (ue, SS_SPECIAL); /* ALLOCATED); */
2166 /* evans 2002-01-01: should be unnecessary, the pre clauses
2167 ** set the state if necessary.
2172 uentry_setDefState (ue, sRef_getDefState (pref));
2176 uref = uentry_getSref (ue);
2178 if (sRef_isStack (uref))
2180 alkind pkind = sRef_getAliasKind (pref);
2182 if (alkind_isKnown (pkind) && !alkind_isLocal (pkind)
2183 && !alkind_isStack (pkind))
2185 sRef_setAliasKind (uref, pkind, fileloc_undefined);
2186 sRef_setOrigAliasKind (uref, pkind);
2190 sRef_setAliasKind (uref, AK_IMPTEMP, fileloc_undefined);
2191 sRef_setOrigAliasKind (uref, AK_IMPTEMP);
2193 if (uentry_isOut (param))
2199 sRef_setDefined (uref, fileloc_undefined);
2205 usymtab_addMustAlias (uref, pref);
2207 if (!(uentry_isOnly (param) || uentry_isUnique (param)))
2210 ** This is needed for detecting possibly aliased parameters.
2213 sRef s = sRef_makeExternal (uref);
2214 usymtab_addMustAlias (uref, s);
2217 if (sRef_isKillRef (pref))
2219 sRef_setAliasKind (uref, AK_NEWREF, fileloc_undefined);
2220 sRef_setOrigAliasKind (uref, AK_KILLREF);
2222 else if (sRef_isRefCounted (uref))
2224 sRef_setOrigAliasKind (uref, AK_REFCOUNTED);
2229 sRef_setOrigAliasKind (uref, AK_LOCAL);
2236 } end_usymtab_entries;
2239 if (uentry_hasStateClauseList (fcn))
2241 stateClauseList clauses = uentry_getStateClauseList (fcn);
2243 stateClauseList_preElements (clauses, cl)
2245 fileloc loc = stateClause_loc (cl);
2246 sRefSet osrs = sRefSet_undefined;
2249 if (stateClause_isGlobal (cl))
2251 DPRINTF (("Global Marker: %s",
2252 sRef_unparseFull (usymtab_lookupGlobalMarker ())));
2253 llassert (sRef_isGlobalMarker (usymtab_lookupGlobalMarker ()));
2254 srs = sRefSet_single (usymtab_lookupGlobalMarker ());
2259 srs = stateClause_getRefs (cl);
2262 sRefSet_elements (srs, el)
2264 sRef base = sRef_getRootBase (el);
2265 sRef sb = sRef_updateSref (el);
2267 if (sRef_isResult (base))
2269 ; /* nothing to do before */
2271 else if (sRef_isParam (base) || sRef_isGlobalMarker (base))
2273 if (stateClause_setsMetaState (cl))
2275 /* copied from exprNode.c:3040 */
2276 qual ql = stateClause_getMetaQual (cl);
2277 annotationInfo ainfo = qual_getAnnotationInfo (ql);
2278 metaStateInfo minfo = annotationInfo_getState (ainfo);
2279 cstring key = metaStateInfo_getName (minfo);
2280 int mvalue = annotationInfo_getValue (ainfo);
2282 DPRINTF (("Sets meta state! %s", stateClause_unparse (cl)));
2284 if (sRef_isResult (base))
2290 sRef_setMetaStateValueComplete (sb, key, mvalue, loc);
2295 sRefMod modf = stateClause_getEntryFunction (cl);
2299 sRefSet aliases = usymtab_allAliases (sb);
2303 sRefSet_elements (aliases, sr)
2306 } end_sRefSet_elements ;
2308 sRefSet_free (aliases);
2314 if (sRef_isValid (base))
2316 DPRINTF (("Base: %s", sRef_unparseFull (base)));
2320 } end_sRefSet_elements ;
2321 } end_stateClauseList_preElements ;
2326 usymtab_enterFunctionScope (uentry fcn)
2327 /*@globals utab, filetab, globtab@*/
2330 usymtab t = usymtab_create (US_NORMAL, utab, TRUE);
2332 if (utab->lexlevel != fileScope)
2334 if (utab->lexlevel > fileScope)
2336 llparseerror (cstring_makeLiteral ("New function scope inside function"));
2338 while (utab->lexlevel > fileScope)
2340 /*@i@*/ utab = usymtab_dropEnv (utab);
2347 llfatalbug (cstring_makeLiteral ("New function not inside file."));
2349 /*@-branchstate@*/ } /*@=branchstate@*/
2353 DPRINTF (("Globs: %s", globSet_unparse (uentry_getGlobs (fcn))));
2355 globSet_allElements (uentry_getGlobs (fcn), el)
2357 DPRINTF (("Here we go: %s", sRef_unparseFull (el)));
2359 if (sRef_isUndefGlob (el))
2361 int index = sRef_getScopeIndex (el);
2362 sRef sr = sRef_updateSref (el);
2363 fileloc loc = uentry_whereEarliest (fcn);
2365 DPRINTF (("update: %s", sRef_unparseFull (sr)));
2366 DPRINTF (("Undef!"));
2367 if (sRef_isFileStatic (el))
2369 ctype ct = sRef_getType (el);
2372 llassert (usymtab_isDefined (filetab));
2374 ue = usymtab_fetchIndex (filetab, index);
2376 if (ctype_isRealArray (ct) || ctype_isRealSU (ct))
2378 sRef_setAllocated (sr, loc);
2382 sRef_setUndefined (sr, loc);
2387 uentry ue = globtab->entries[index];
2388 ctype ct = uentry_getType (ue);
2390 if (ctype_isArray (ct) || ctype_isSU (ct))
2392 sRef_setAllocated (sr, loc);
2396 sRef_setUndefined (sr, loc);
2400 else if (sRef_isAllocated (el))
2402 sRef sr = sRef_updateSref (el);
2403 fileloc loc = uentry_whereEarliest (fcn);
2405 sRef_setAllocated (sr, loc);
2407 else if (sRef_isPartial (el))
2409 sRef sr = sRef_updateSref (el);
2410 fileloc loc = uentry_whereEarliest (fcn);
2412 sRef_setPartial (sr, loc);
2417 sRef sr = sRef_updateSref (el);
2418 fileloc loc = uentry_whereEarliest (fcn);
2420 sRef_setDefined (sr, loc);
2424 /* shouldn't need to do anything! */
2426 } end_globSet_allElements;
2428 DPRINTF (("Globs after: %s", globSet_unparse (uentry_getGlobs (fcn))));
2432 usymtab_caseBranch (void)
2435 usymtab t = usymtab_create (US_CBRANCH, utab, FALSE);
2440 usymtab_switchBranch (/*@unused@*/ exprNode s)
2443 usymtab t = usymtab_create (US_SWITCH, utab, FALSE);
2445 t->aliases = aliasTable_copy (utab->aliases);
2450 usymtab_trueBranch (/*@only@*/ guardSet guards)
2453 usymtab t = usymtab_create (US_TBRANCH, utab, FALSE);
2456 ** not true! (could be in a macro)
2458 ** llassertprint (utab->lexlevel > paramsScope,
2459 ** ("not in scope: %s", usymtab_unparseLocal ()));
2463 guardSet_free (t->guards);
2466 aliasTable_free (t->aliases);
2467 t->aliases = aliasTable_copy (utab->aliases);
2475 ** { int a; if (...) a = 3; < a may be undefined here!
2480 usymtab_popTrueBranch (exprNode pred, exprNode expr, clause cl) /*@modifies utab@*/
2483 ** add a false branch
2484 ** (could be done more efficiently as a special case, but
2485 ** it is better to only maintain one version of the code)
2488 if (utab->kind != US_TBRANCH
2489 && context_inIterDef ())
2491 usymtab_exitScope (expr);
2495 DPRINTF (("pop true branch.."));
2496 usymtab_altBranch (guardSet_invert (exprNode_getGuards (pred)));
2497 usymtab_popBranches (pred, expr, exprNode_undefined, TRUE, cl);
2502 usymtab_popCaseBranch () /*@modifies utab@*/
2504 llassert (utab->kind == US_CBRANCH);
2505 usymtab_quietPlainExitScope ();
2509 usymtab_popTrueExecBranch (exprNode pred, exprNode expr, clause cl)
2512 ** add a false branch that must return --- that is,
2513 ** the true branch is always executed!
2516 usymtab_altBranch (guardSet_invert (exprNode_getGuards (pred)));
2517 usymtab_popBranches (pred, expr, exprNode_makeMustExit (), FALSE, cl);
2521 usymtab_popOrBranch (exprNode pred, exprNode expr)
2525 usymtab env = utab->env;
2526 usymtab otab = utab;
2529 llassert (env != NULL);
2531 if (exprNode_isError (expr))
2537 mustReturn = exprNode_mustEscape (expr);
2541 llassert (utab->kind == US_TBRANCH);
2544 ** merge each entry in table with its original
2545 ** unless execution cannot continue after this branch
2548 for (i = 0; i < utab->nentries; i++)
2550 uentry current = utab->entries[i];
2551 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
2553 uentry_mergeState (old, current, exprNode_loc (expr),
2554 mustReturn, FALSE, TRUE, ORCLAUSE);
2560 env->guards = guardSet_levelUnionFree (env->guards,
2561 guardSet_invert (exprNode_getGuards (pred)),
2566 env->aliases = aliasTable_levelUnion (env->aliases, otab->aliases, env->lexlevel);
2569 /* env is now utab */
2570 usymtab_quietPlainExitScope ();
2574 ** case syntax in C is very unrestricted. This is unfortunate.
2576 ** A switch case is ended either by a new case or default, or
2577 ** a close } that may close the switch or some other control
2582 usymtab_newCase (/*@unused@*/ exprNode pred, exprNode last)
2585 bool mustBreak = usymtab_mustBreak (utab);
2586 bool mustReturn = usymtab_mustEscape (utab);
2587 usymtab stab = utab;
2590 ** Find last case (or outer switch)
2593 while (stab->kind != US_CBRANCH && stab->kind != US_SWITCH)
2596 llassert (stab != GLOBAL_ENV);
2601 while (stab->kind == US_CBRANCH)
2604 llassert (stab != GLOBAL_ENV);
2608 ** if its a fall through case, merge in outside entries and last case.
2613 ** case 1: x = 3; <fall through>
2614 ** case 2: << x may not be defined
2618 if (!mustBreak && !mustReturn && utab->kind == US_CBRANCH)
2620 llassert (stab->kind == US_SWITCH || stab->kind == US_NORMAL);
2622 usymtab_entries (utab, ue) /* but, keep track of used variables */
2624 uentry old = usymtab_lookupAux (stab, uentry_rawName (ue));
2626 llassert (uentry_isValid (old));
2628 /* modifies ue, not old */
2630 uentry_mergeState (ue, old, exprNode_loc (last),
2631 FALSE, FALSE, TRUE, CASECLAUSE);
2632 } end_usymtab_entries;
2634 utab->aliases = aliasTable_levelUnion (utab->aliases,
2635 stab->aliases, utab->lexlevel);
2638 ** No need for a new branch.
2645 usymtab_caseBranch ();
2646 /*@-mustfree@*/ /*< utab->aliases >*/
2647 utab->aliases = aliasTable_copy (stab->aliases);
2655 ** for && (both pred and expr are executed)
2659 usymtab_popAndBranch (exprNode pred, /*@unused@*/ exprNode expr)
2662 usymtab env = utab->env;
2666 llassert (utab->kind == US_TBRANCH);
2669 ** merge each entry in table with its original
2670 ** unless execution cannot continue after this branch
2673 for (i = 0; i < utab->nentries; i++)
2675 uentry current = utab->entries[i];
2676 sRef tref = uentry_getSref (current);
2677 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
2678 sRef oref = uentry_getSref (old);
2680 /* note that is current is in a nested branch,
2681 it may create a "new" old entry. */
2683 llassert (uentry_isValid (old));
2684 uentry_mergeState (old, current, exprNode_loc (expr),
2685 FALSE, FALSE, TRUE, ANDCLAUSE);
2688 ** if is it defined by the second clause, then it should be defined.
2691 if (sRef_isAnyDefined (tref)
2692 && (sRef_isAllocated (oref) || sRef_isStateUndefined (oref)))
2694 sRef_setDefined (oref, g_currentloc);
2698 utab->guards = guardSet_levelUnionFree (utab->guards,
2699 guardSet_invert (exprNode_getGuards (pred)),
2701 utab->aliases = aliasTable_levelUnion (utab->aliases, otab->aliases, utab->lexlevel);
2703 usymtab_quietPlainExitScope ();
2708 ** Stack should be [ US_CBRANCH+ US_SWITCH ]
2709 ** Only branches which do not return (except possibly the last branch) are included.
2711 ** Conditionally merge state from all CBRANCHes.
2713 ** If allpaths is TRUE, then all possible executions go through some switch
2714 ** case, and the original scope is not merged.
2718 usymtab_exitSwitch (/*@unused@*/ exprNode sw, bool allpaths)
2721 usymtab ttab = utab;
2722 usymtab stab = ttab;
2723 usymtab ltab = ttab;
2724 bool lastMustReturn = usymtab_mustEscape (utab);
2728 while (stab->kind == US_CBRANCH)
2731 llassert (stab != GLOBAL_ENV);
2734 while (stab->kind == US_NORMAL)
2737 llassert (stab != GLOBAL_ENV);
2740 llassert (stab->kind == US_SWITCH);
2742 /* go to the scope outside the switch (US_SWITCH is just a marker! */
2744 llassert (stab != GLOBAL_ENV);
2748 llassert (usymtab_isDefined (ttab));
2750 if (ttab->kind == US_CBRANCH)
2752 /* was quietPlainExitScope --- but, can't free it yet! */
2754 llassert (utab != GLOBAL_ENV);
2756 while (ttab->kind == US_CBRANCH)
2759 ** (from popTrueBranch)
2762 bool mustReturn = usymtab_mustEscape (ttab);
2763 bool mustBreak = usymtab_mustBreak (ttab);
2765 usymtab_entries (ttab, current)
2767 uentry old = /*@-compmempass@*/ usymtab_lookupAux (ltab, uentry_rawName (current));
2771 ** note that is this is in a nested branch,
2772 ** it may create a "new" old entry.
2775 if (uentry_isValid (old))
2779 uentry_mergeUses (current, old);
2780 uentry_setState (old, current);
2784 uentry_mergeState (old, current, exprNode_loc (sw),
2785 mustReturn, FALSE, TRUE, SWITCHCLAUSE);
2792 } end_usymtab_entries;
2795 ** if entry is not in symbol table for this case, merge with pre-switch
2799 if (!mustReturn && !mustBreak)
2801 usymtab_entries (stab, current)
2803 if (usymtab_getIndex (ttab, uentry_rawName (current)) == NOT_FOUND)
2805 uentry old = /*@-compmempass@*/
2806 usymtab_lookupAux (ltab, uentry_rawName (current));
2809 llassert (uentry_isValid (old));
2810 uentry_mergeState (old, current, exprNode_loc (sw),
2811 FALSE, FALSE, TRUE, SWITCHCLAUSE);
2813 } end_usymtab_entries;
2816 ltab->env = ttab->env;
2820 ** Suprious error, becuase of environments.
2823 /*@i1@*/ utab = ltab;
2825 lastMustReturn = FALSE;
2832 ** now, there is one US_CBRANCH. Merge this with the stab.
2836 for (i = 0; i < ltab->nentries; i++)
2838 uentry current = ltab->entries[i];
2839 uentry old = usymtab_lookupAux (stab, uentry_rawName (current));
2841 /* note that is this is in a nested branch,
2842 it may create a "new" old entry. */
2845 if (uentry_isValid (old))
2849 uentry_mergeUses (current, old);
2850 uentry_setState (old, current);
2854 uentry_mergeState (old, current, exprNode_loc (sw),
2855 FALSE, FALSE, TRUE, SWITCHCLAUSE);
2869 ** switch may or may not be followed by a new scope
2872 if (utab->kind == US_SWITCH)
2874 usymtab_quietPlainExitScope ();
2878 usymtab_quietPlainExitScope ();
2879 llassert (utab->kind == US_SWITCH);
2880 usymtab_quietPlainExitScope ();
2886 updateNullState (sRef el, /*@notnull@*/ usymtab ttab,
2887 /*@notnull@*/ usymtab ftab, bool trueGuard)
2889 sRef base = sRef_getRootBase (el);
2890 int level = sRef_lexLevel (base);
2892 if (sRef_isCvar (base))
2894 usymId index = sRef_getScopeIndex (base);
2895 uentry ue = usymtab_getRefTab (ttab, level, index);
2897 if (!uentry_isLset (ue))
2899 sRef sr = uentry_getSref (ue);
2903 sRef_setDerivNullState (sr, el, NS_NOTNULL);
2907 if (!guardSet_isGuarded (ttab->guards, el)
2908 && !sRef_isNotNull (sr))
2910 sRef_setDerivNullState (sr, el, NS_DEFNULL);
2918 ue = usymtab_getRefTab (ftab, level, index);
2920 if (!uentry_isLset (ue))
2922 sRef sr = uentry_getSref (ue);
2925 if (!trueGuard) /* yikes! forgot the ! */
2927 sRef_setDerivNullState (sr, el, NS_NOTNULL);
2932 if (!guardSet_isGuarded (ftab->guards, el)
2933 && !sRef_isNotNull (sr))
2935 sRef_setDerivNullState (sr, el, NS_DEFNULL);
2947 usymtab_popBranches (exprNode pred, exprNode tbranch, exprNode fbranch,
2948 bool isOpt, clause cl)
2952 usymtab ftab = utab;
2953 usymtab ttab = utab->env;
2957 guardSet guards = exprNode_getGuards (pred);
2958 sRefSet tguards = guardSet_getTrueGuards (guards);
2959 sRefSet fguards = guardSet_getFalseGuards (guards);
2960 bool mustReturnT = exprNode_mustEscape (tbranch);
2961 bool mustReturnF = exprNode_mustEscape (fbranch);
2963 DPRINTF (("Pop branches: %s [mustreturn: %s/%s]", exprNode_unparse (pred),
2964 bool_unparse (mustReturnT),
2965 bool_unparse (mustReturnF)));
2967 if (exprNode_isDefined (fbranch))
2969 loc = exprNode_loc (fbranch);
2973 loc = exprNode_loc (tbranch);
2976 llassert (usymtab_isDefined (ttab));
2980 llassert (usymtab_isDefined (env));
2981 llassert (ftab->kind == US_FBRANCH);
2982 llassert (ttab->kind == US_TBRANCH);
2985 ** For each element that is true guarded (i.e., if (x != NULL))
2986 ** make x = null in false branch,
2987 ** and x = notnull in true branch.
2988 ** unless x was set locally in that branch.
2989 ** For each element that is false guarded (x == NULL)
2990 ** make x = null in true, notnull in false.
2992 ** For each element that is either guarded (pred(x))
2996 sRefSet_allElements (tguards, el)
2998 updateNullState (el, ttab, ftab, TRUE);
2999 } end_sRefSet_allElements;
3001 sRefSet_allElements (fguards, el)
3003 updateNullState (el, ttab, ftab, FALSE);
3004 } end_sRefSet_allElements;
3008 ** if an entry is in both true and false, merge the entries,
3009 ** then replace original with new state.
3011 ** if an entry is in one table, merge it with the original.
3014 DPRINTF (("ftab: %d", ftab->nentries));
3016 for (i = 0; i < ftab->nentries; i++)
3018 uentry fthis = ftab->entries[i];
3019 uentry old = usymtab_lookupAux (env, uentry_rawName (fthis));
3020 int tindex = usymtab_getIndex (ttab, uentry_rawName (fthis));
3022 DPRINTF (("Entry: %s / %s", uentry_unparseFull (fthis), uentry_unparseFull (old)));
3024 if (uentry_isUndefined (old))
3026 /* possible entry was added as an undefined id */
3027 DPRINTF (("Undefined! %s", uentry_rawName (fthis)));
3031 if (tindex != NOT_FOUND)
3033 uentry tthis = ttab->entries[tindex];
3035 /* note that is this is in a nested branch,
3036 it may create a "new" old entry. */
3042 uentry_mergeState (fthis, tthis, loc,
3043 mustReturnT, FALSE, FALSE, cl);
3047 uentry_mergeUses (fthis, tthis);
3050 uentry_setState (old, fthis);
3057 uentry_setState (old, tthis);
3058 uentry_mergeState (old, fthis, loc, mustReturnF,
3062 ttab->entries[tindex] = uentry_undefined;
3063 uentry_free (tthis);
3067 uentry_mergeState (old, fthis, loc, mustReturnF, TRUE, FALSE, cl);
3071 for (i = 0; i < ttab->nentries; i++)
3073 uentry current = ttab->entries[i];
3075 DPRINTF (("ttab: %s", uentry_unparseFull (current)));
3077 if (!uentry_isUndefined (current))
3079 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
3081 DPRINTF (("Old: %s", uentry_unparseFull (old)));
3083 if (uentry_isUndefined (old))
3085 llcontbug (message ("Undefined entry: %s", uentry_rawName (current)));
3091 uentry_mergeUses (current, old);
3092 uentry_setState (old, current);
3097 ** Assumes false branch is a fall-through if
3098 ** fbranch is not defined. This is true, unless
3099 ** where was some greivous error in processing
3100 ** the else branch of an if-then, in which case
3101 ** this is probably the right thing to do anyway.
3104 uentry_mergeState (old, current, loc, mustReturnT,
3108 DPRINTF (("==> %s", uentry_unparseFull (old)));
3113 ** Plain levelUnion doesn't work, since we need to use the sRef's in env->aliases
3114 ** if they are present.
3117 llassert (NOALIAS (env->aliases, ttab->aliases));
3118 llassert (NOALIAS (env->aliases, ftab->aliases));
3120 aliasTable_free (env->aliases);
3122 env->aliases = aliasTable_levelUnionNew (ttab->aliases,
3123 ftab->aliases, env->lexlevel);
3125 aliasTable_fixSrefs (env->aliases);
3127 DPRINTF (("Aliases: %s", aliasTable_unparse (env->aliases)));
3129 /* exit true and false scopes */
3130 usymtab_quietPlainExitScope ();
3131 usymtab_quietPlainExitScope ();
3135 utab->guards = guardSet_levelUnionFree
3137 guardSet_invert (exprNode_getGuards (pred)),
3143 utab->guards = guardSet_levelUnion (utab->guards,
3144 exprNode_getGuards (pred),
3148 DPRINTF (("Here."));
3151 static void usymtab_fixCases (void) /*@modifies utab@*/ {
3152 while (utab->kind == US_CBRANCH)
3154 usymtab_quietPlainExitScope ();
3157 llassert (utab->kind != US_CBRANCH);
3161 usymtab_altBranch (/*@only@*/ guardSet guards)
3165 usymtab parent = utab->env;
3167 t = usymtab_create (US_FBRANCH, utab, FALSE);
3170 ** If we are in a case, need to close it. The C syntax
3171 ** is very liberal, so this kludge is necessary.
3174 usymtab_fixCases ();
3176 DPRINTF (("Current kind: %s", usymtab_unparseStack ()));
3178 llassert (utab->kind == US_TBRANCH);
3179 llassert (parent != GLOBAL_ENV);
3181 guardSet_free (t->guards);
3184 aliasTable_free (t->aliases);
3185 t->aliases = aliasTable_copy (parent->aliases);
3191 usymtab_allDefined (void)
3192 /*@globals utab, globtab@*/
3196 llassert (utab == globtab);
3198 for (i = 0; i < utab->nentries; i++)
3200 uentry e = utab->entries[i];
3202 if (uentry_isPriv (e))
3204 ; /* no need to define it */
3208 if (context_getFlag (FLG_SPECUNDECL))
3210 fileloc sloc = uentry_whereSpecified (e);
3211 fileloc dloc = uentry_whereDeclared (e);
3213 if (fileloc_isDefined (sloc)
3214 && !uentry_isFakeTag (e)
3215 && !fileloc_isDefined (dloc))
3219 message ("%s %q specified but not declared",
3220 ekind_capName (uentry_getKind (e)),
3221 uentry_getName (e)),
3226 if (!uentry_isCodeDefined (e))
3228 fileloc dloc = uentry_whereDeclared (e);
3230 if (fileloc_isLib (dloc) || fileloc_isXHFile (dloc))
3234 else if (fileloc_isDefined (dloc))
3236 if (!uentry_isAnyTag (e))
3238 if (fileloc_isUser (dloc))
3242 message ("%s %q declared but not defined",
3243 ekind_capName (uentry_getKind (e)),
3244 uentry_getName (e)),
3246 DPRINTF (("decl: %s", uentry_unparseFull (e)));
3252 fileloc sloc = uentry_whereSpecified (e);
3254 if (fileloc_isDefined (sloc)
3255 && !fileloc_isImport (sloc)
3256 && !fileloc_isLib (sloc)
3257 && !fileloc_isPreproc (sloc)
3258 && !uentry_isFakeTag (e))
3260 if (uentry_isVariable (e) || uentry_isFunction (e))
3264 message ("%s %q specified but not declared or defined",
3265 ekind_capName (uentry_getKind (e)),
3266 uentry_getName (e)),
3273 message ("%s %q specified but not defined",
3274 ekind_capName (uentry_getKind (e)),
3275 uentry_getName (e)),
3285 void usymtab_exportHeader (void)
3290 for (i = 0; i < utab->nentries; i++)
3292 uentry ce = utab->entries[i];
3294 if (!uentry_isDatatype (ce)
3295 && !uentry_isAnyTag (ce)
3296 && !uentry_isEitherConstant (ce) && !uentry_isStatic (ce)
3297 && !uentry_isExternal (ce)
3298 && !uentry_isForward (ce))
3300 fileloc fwhere = uentry_whereDeclared (ce);
3302 if (fileloc_isUndefined (fwhere)
3303 && uentry_isFunction (ce))
3305 fwhere = uentry_whereDefined (ce);
3308 if (fileloc_isDefined (fwhere)
3309 && !fileloc_isHeader (fwhere)
3310 && !fileloc_isXHFile (fwhere)
3311 && !(fileloc_isSpecialFile (fwhere)
3312 && !context_getFlag (FLG_UNUSEDSPECIAL)))
3314 if (uentry_isVariable (ce))
3317 (FLG_EXPORTHEADERVAR,
3318 message ("%s %q exported but not declared in header file",
3319 ekind_capName (uentry_getKind (ce)),
3320 uentry_getName (ce)),
3323 uentry_showDefSpecInfo (ce, fwhere);
3328 if (!uentry_isIter (ce)
3329 && !uentry_isEndIter (ce)
3330 && !uentry_isExpandedMacro (ce))
3332 if (uentry_isFunction (ce)
3333 && cstring_equalLit (uentry_rawName (ce), "main"))
3335 ; /* no error for main */
3341 message ("%s %q exported but not declared "
3343 ekind_capName (uentry_getKind (ce)),
3344 uentry_getName (ce)),
3347 uentry_showDefSpecInfo (ce, fwhere);
3357 void usymtab_exportLocal (void)
3362 for (i = 0; i < utab->nentries; i++)
3364 uentry ce = utab->entries[i];
3366 if (!uentry_isDatatype (ce) && !uentry_isAnyTag (ce)
3367 && !uentry_isEitherConstant (ce)
3368 && !uentry_isIter (ce)
3369 && !uentry_isEndIter (ce)
3370 && !uentry_isExpandedMacro (ce)
3371 && uentry_isUsed (ce))
3373 /* check static uses */
3374 filelocList fuses = uentry_getUses (ce);
3375 fileloc mod = uentry_whereDefined (ce);
3376 bool ok = filelocList_isEmpty (fuses);
3377 fileloc fwhere = uentry_whereDeclared (ce);
3379 if (fileloc_isSpecialFile (fwhere)
3380 && !context_getFlag (FLG_UNUSEDSPECIAL))
3382 ok = TRUE; /* no errors for special files */
3386 filelocList_elements (fuses, uloc)
3388 if (fileloc_isUndefined (uloc) || !fileloc_sameModule (uloc, mod))
3391 /*@innerbreak@*/ break;
3393 } end_filelocList_elements;
3400 message ("%s exported but not used outside %s: %q",
3401 ekind_capName (uentry_getKind (ce)),
3402 fileloc_getBase (mod),
3403 uentry_getName (ce)),
3406 uentry_showDefSpecInfo (ce, fwhere);
3414 usymtab_allUsed (void)
3418 bool isFileStatic = usymtab_inFileScope ();
3419 cstring last_file = cstring_undefined;
3421 for (i = 0; i < utab->nentries; i++)
3423 bool hasError = FALSE;
3424 uentry ce = utab->entries[i];
3425 fileloc fwhere = uentry_whereDeclared (ce);
3427 if (fileloc_isUndefined (fwhere))
3429 fwhere = uentry_whereDefined (ce);
3432 if (fileloc_isInvalid (fwhere)
3433 || fileloc_isLib (fwhere)
3434 || fileloc_isBuiltin (fwhere)
3435 || ((fileloc_isSpecialFile (fwhere)
3436 || fileloc_isSpecialFile (uentry_whereDefined (ce)))
3437 && !context_getFlag (FLG_UNUSEDSPECIAL)))
3441 else if (!uentry_wasUsed (ce) && !uentry_isAnyTag (ce))
3443 cstring fname = fileloc_filename (fwhere);
3445 if (cstring_isUndefined (last_file))
3449 else if (cstring_equal (fname, last_file))
3458 if (uentry_isParam (ce))
3460 if (context_inMacro ())
3462 sRef cref = uentry_getSref (ce);
3464 if (uentry_isYield (ce))
3466 ; /* no checks (for now) */
3468 else if (sRef_isSafe (cref))
3474 if (uentry_hasRealName (ce))
3477 optgenerror (FLG_MACROPARAMS,
3478 message ("Macro parameter %q not used",
3479 uentry_getName (ce)),
3486 if (cstring_equalFree (uentry_getName (ce),
3487 cstring_makeLiteral ("...")))
3493 hasError = optgenerror (FLG_PARAMUNUSED,
3494 message ("Parameter %q not used",
3495 uentry_getName (ce)),
3500 else if (uentry_isFunction (ce) || uentry_isIter (ce))
3502 if (fileloc_isUser (fwhere))
3504 hasError = optgenerror
3506 message ("%q %q declared but not used",
3508 (uentry_isIter (ce) ? "Iterator"
3509 : (isFileStatic ? "File static function" : "Function")),
3510 uentry_getName (ce)),
3514 else if (uentry_isEndIter (ce))
3516 ; /* no error (already reported for iter */
3518 else if (uentry_isEnumConstant (ce))
3520 if (fileloc_isUser (fwhere))
3522 hasError = optgenerror
3524 message ("Enum member %q not used",
3525 uentry_getName (ce)),
3529 else if (uentry_isConstant (ce))
3531 if (fileloc_isUser (fwhere))
3533 hasError = optgenerror
3535 message ("Constant %q declared but not used",
3536 uentry_getName (ce)),
3540 else if (uentry_isDatatype (ce))
3542 if (fileloc_isUser (fwhere))
3544 hasError = optgenerror
3546 message ("Type %q declared but not used",
3547 uentry_getName (ce)),
3551 else if (!uentry_isRefParam (ce) && !uentry_isExpandedMacro (ce))
3552 { /* errors for ref params will be reported in the next scope */
3553 llassertprint (uentry_isVar (ce),
3554 ("ce: %s", uentry_unparseFull (ce)));
3556 if (ctype_isFunction (uentry_getType (ce)))
3558 if (fileloc_isUser (fwhere))
3560 hasError = optgenerror
3562 message ("%q %q declared but not used",
3564 (isFileStatic ? "File static function"
3566 uentry_getName (ce)),
3572 if (fileloc_isUser (fwhere))
3576 hasError = optgenerror
3578 message ("%q %q declared but not used",
3580 (isFileStatic ? "File static variable"
3582 uentry_getName (ce)),
3592 else if ((uentry_isDatatype (ce) || uentry_isAnyTag (ce)))
3593 { /* check all fields */
3594 ctype ct = uentry_getRealType (ce);
3597 while (ctype_isAP (ct))
3599 ct = ctype_getBaseType (ct);
3602 if (ctype_isSU (ct))
3604 uentryList fields = ctype_getFields (ct);
3606 uentryList_elements (fields, field)
3608 if (!uentry_isUsed (field))
3610 if (uentry_hasName (ce))
3612 hasError |= optgenerror
3614 message ("Field %q of %s %q declared but not used",
3615 uentry_getName (field),
3616 cstring_makeLiteralTemp
3617 (ctype_isStruct (ct) ? "structure" : "union"),
3618 uentry_getName (ce)),
3619 uentry_whereEarliest (field));
3625 ** Can't report these errors for unnamed structs.
3626 ** No way to tell when there are multiple consistent
3627 ** unnamed structure types. (Could go through table
3628 ** and mark them all unused...)
3630 hasError |= optgenerror
3632 message ("Field %q of unnamed %s declared but not used",
3633 uentry_getName (field),
3634 cstring_makeLiteralTemp
3635 (ctype_isStruct (ct) ? "structure" : "union")),
3636 uentry_whereEarliest (field));
3641 uentry_setUsed (field, fileloc_undefined);
3643 } end_uentryList_elements;
3653 if (uentry_isParam (ce) && context_inMacro ())
3655 if (fileloc_isDefined (uentry_whereSpecified (ce)))
3657 uentry_showWhereSpecified (ce);
3662 uentry_showDefSpecInfo (ce, fwhere);
3665 uentry_setUsed (ce, fileloc_undefined);
3671 checkGlobalReturn (uentry glob, sRef orig)
3673 sRef sr = uentry_getSref (glob);
3675 DPRINTF (("Check global return: %s / orig: %s / sr: %s",
3676 uentry_unparseFull (glob),
3677 sRef_unparseFull (orig),
3678 sRef_unparseFull (sr)));
3680 DPRINTF (("Is killed: %s", bool_unparse (sRef_isKilledGlob (orig))));
3682 if (context_getFlag (FLG_GLOBSTATE))
3684 DPRINTF (("Is killed: %s", sRef_unparseFull (orig)));
3686 if (sRef_isKilledGlob (orig))
3688 if (sRef_isStateUndefined (sr)
3689 || sRef_isUnuseable (sr)
3690 || sRef_isStateUnknown (sr)
3691 || sRef_isDead (sr))
3697 ctype ct = ctype_realType (uentry_getType (glob));
3699 DPRINTF (("Check global destroyed: %s", uentry_unparseFull (glob)));
3701 if (ctype_isVisiblySharable (ct))
3706 ("Killed global %q (type %s) not released before return",
3707 uentry_getName (glob),
3708 ctype_unparse (ct)),
3711 sRef_showStateInfo (sr);
3716 sRef_protectDerivs ();
3717 (void) transferChecks_globalDestroyed (sr, g_currentloc);
3718 sRef_clearProtectDerivs ();
3724 if (sRef_isStateUndefined (sr))
3726 if (optgenerror (FLG_GLOBSTATE,
3728 ("Function returns with global %q undefined",
3729 uentry_getName (glob)),
3732 sRef_showStateInfo (sr);
3737 if (sRef_isDead (sr) || sRef_isKept (sr))
3741 message ("Function returns with global %q "
3742 "referencing %s storage",
3743 uentry_getName (glob),
3744 cstring_makeLiteralTemp (sRef_isDead (sr) ? "released" : "kept")),
3747 if (sRef_isKept (sr))
3749 sRef_showAliasInfo (sr);
3753 sRef_showStateInfo (sr);
3756 sRef_setDefState (sr, SS_UNKNOWN, fileloc_undefined);
3760 if (ctype_isRealPointer (uentry_getType (glob))
3761 && sRef_possiblyNull (sr)
3762 && !uentry_possiblyNull (glob))
3766 message ("Function returns with non-null global %q "
3767 "referencing null storage",
3768 uentry_getName (glob)),
3771 sRef_showNullInfo (sr);
3776 transferChecks_globalReturn (glob);
3784 ** remember: check alias globals
3787 void usymtab_checkFinalScope (bool isReturn)
3790 bool mustFree = context_getFlag (FLG_MUSTFREEONLY) || context_getFlag (FLG_MUSTFREEFRESH); /*@i423 remove this mustFree */
3791 bool mustDefine = context_getFlag (FLG_MUSTDEFINE);
3792 /* bool mustNotAlias = context_getFlag (FLG_MUSTNOTALIAS); */
3793 sRefSet checked = sRefSet_new ();
3794 usymtab stab = utab;
3798 ** need to check all scopes out to function parameters.
3803 for (i = 0; i < stab->nentries; i++)
3805 uentry ce = stab->entries[i];
3806 sRef sr = uentry_getSref (ce);
3807 sRef rb = sRef_getRootBase (sr);
3811 ** Shouldn't check if shadow checked in deeper scope:
3816 uentry oue = usymtab_lookupQuietNoAlt (utab, uentry_observeRealName (ce));
3818 if (!uentry_sameObject (ce, oue))
3820 DPRINTF (("Skipping outer entry: %s / %s", uentry_unparseFull (ce),
3821 uentry_unparseFull (oue)));
3822 /*@i32 what if it is one an alternate branch? */
3823 /*@innercontinue@*/ continue;
3827 DPRINTF (("Here check final scope: %s", uentry_unparseFull (ce)));
3829 if (ctype_isFunction (uentry_getType (ce)))
3831 /*@innercontinue@*/ continue;
3834 if (uentry_isAnyParam (ce)
3835 || uentry_isRefParam (ce)
3836 || sRef_isFileOrGlobalScope (rb))
3838 /* Don't do the loseref check...but should check state! */
3839 DPRINTF (("Skipping check 1"));
3841 else if (sRef_isDefinitelyNull (sr)
3842 || usymtab_isDefinitelyNull (sr))
3845 ** No state reference errors for definitely null references.
3848 DPRINTF (("Skipping check 2"));
3852 DPRINTF (("Lose ref: %s / %s", uentry_unparse (ce),
3853 sRef_unparseFull (sr)));
3855 tvalues = sRef_getValueTable (sr);
3857 valueTable_elements (tvalues, fkey, fval) {
3858 metaStateInfo minfo;
3859 cstring msg = cstring_undefined;
3862 minfo = context_lookupMetaStateInfo (fkey);
3863 llassert (metaStateInfo_isDefined (minfo));
3865 if (stateValue_isError (fval)
3866 || sRef_isStateUndefined (sr)) /* No errors for undefined state */
3868 DPRINTF (("Skipping check 3"));
3872 DPRINTF (("Check: %s / %s / %s", fkey,
3873 metaStateInfo_unparse (minfo),
3874 stateValue_unparse (fval)));
3876 minfo = context_lookupMetaStateInfo (fkey);
3878 nval = stateCombinationTable_lookupLoseReference
3879 (metaStateInfo_getTransferTable (minfo),
3880 stateValue_getValue (fval), &msg);
3882 if (cstring_isDefined (msg))
3884 /*@i32 print extra info for assignments@*/
3885 DPRINTF (("From: %s", sRef_unparseFull (sr)));
3886 DPRINTF (("Null? %s / %s",
3887 bool_unparse (sRef_isDefinitelyNull (sr)),
3888 bool_unparse (usymtab_isGuarded (sr))));
3893 ("%s loses reference %q in invalid state %q (%s)",
3894 cstring_makeLiteralTemp (isReturn ? "Return" : "Scope exit"),
3895 uentry_getName (ce),
3896 stateValue_unparseValue (fval, minfo),
3900 stateValue_show (fval, minfo);
3904 DPRINTF (("Suppressed transfer error: %s", msg));
3908 } end_valueTable_elements;
3911 DPRINTF (("Here 1"));
3915 DPRINTF (("Check mustfree entry: %s", uentry_unparseFull (ce)));
3917 if (!sRefSet_member (checked, sr) && !sRef_isFileOrGlobalScope (rb))
3919 if (ctype_isRealSU (uentry_getType (ce))
3920 && !uentry_isAnyParam (ce)
3921 && !uentry_isRefParam (ce)
3922 && !uentry_isStatic (ce)
3923 && !sRef_isDependent (sr)
3924 && !sRef_isOwned (sr))
3926 sRefSet als = usymtab_allAliases (sr);
3928 if (sRefSet_isEmpty (als))
3930 transferChecks_localDestroyed (sr, g_currentloc);
3934 /* aliased, no problem */ ;
3940 (!uentry_isStatic (ce)
3941 && ((sRef_isNewRef (sr))
3942 || (((sRef_isOnly (sr) || sRef_isFresh (sr)
3943 || sRef_isKeep (sr) || sRef_isOwned (sr))
3944 && !sRef_isDead (sr))
3945 && (!sRef_definitelyNull (sr))
3946 && (!usymtab_isDefinitelyNull (sr)))))
3948 bool hasError = TRUE;
3950 DPRINTF (("Checking: %s", sRef_unparseFull (sr)));
3953 ** If its a scope exit, check if there is an alias.
3954 ** If so, make it only. If not, there is an error.
3959 if (transferChecks_canLoseReference (sr, g_currentloc))
3961 DPRINTF (("Can lose!"));
3968 if (sRef_hasLastReference (sr))
3970 sRef ar = sRef_getAliasInfoRef (sr);
3973 (sRef_isFresh (ar) ? FLG_MUSTFREEFRESH : FLG_MUSTFREEONLY,
3975 ("Last reference %q to %s storage %qnot %q before %q",
3977 alkind_unparse (sRef_getAliasKind (sr)),
3978 sRef_unparseOpt (ar),
3979 cstring_makeLiteral (sRef_isKeep (sr)
3980 ? "transferred" : "released"),
3981 cstring_makeLiteral (isReturn
3982 ? "return" : "scope exit")),
3985 sRef_showRefLost (sr);
3988 else if (sRef_isNewRef (sr))
3991 (sRef_isFresh (sr) ? FLG_MUSTFREEFRESH : FLG_MUSTFREEONLY,
3993 ("%q %q not released before %q",
3995 (alkind_isKillRef (sRef_getOrigAliasKind (sr))
3996 ? "Kill reference parameter" : "New reference"),
3997 uentry_getName (ce),
3998 cstring_makeLiteral (isReturn
3999 ? "return" : "scope exit")),
4002 sRef_showAliasInfo (sr);
4007 if (ctype_isRealSU (sRef_getType (sr)))
4009 transferChecks_structDestroyed (sr, g_currentloc);
4013 DPRINTF (("Here we are: %s", sRef_unparseFull (sr)));
4016 (sRef_isFresh (sr) ? FLG_MUSTFREEFRESH : FLG_MUSTFREEONLY,
4018 ("%s storage %q not %q before %q",
4019 alkind_capName (sRef_getAliasKind (sr)),
4020 uentry_getName (ce),
4021 cstring_makeLiteral (sRef_isKeep (sr)
4022 ? "transferred" : "released"),
4023 cstring_makeLiteral (isReturn
4024 ? "return" : "scope exit")),
4027 sRef_showAliasInfo (sr);
4028 DPRINTF (("Storage: %s", sRef_unparseFull (sr)));
4041 if (mustDefine && uentry_isOut (ce))
4043 /* No error if its dead (either only or error already reported */
4044 if (!sRef_isReallyDefined (sr) && !sRef_isDead (sr))
4048 message ("Out storage %q not defined before %q",
4049 uentry_getName (ce),
4051 (isReturn ? "return" : "scope exit")),
4054 DPRINTF (("sr: %s", sRef_unparseFull (sr)));
4059 ** also check state is okay
4062 if (usymtab_lexicalLevel () > functionScope
4063 && uentry_isVariable (ce)
4064 && (sRef_isLocalVar (sr)
4065 && (sRef_isDependent (sr) || sRef_isLocalState (sr))))
4067 sRefSet ab = usymtab_aliasedBy (sr);
4069 /* should do something more efficient here */
4071 if (sRefSet_isEmpty (ab))
4073 /* and no local ref */
4074 DPRINTF (("Check lose ref: %s", uentry_unparseFull (ce)));
4075 transferChecks_loseReference (ce);
4089 checked = sRefSet_insert (checked, sr);
4092 llassert (usymtab_isDefined (stab->env));
4094 if (usymtab_isBranch (stab))
4096 stab = usymtab_dropEnv (stab);
4103 llassert (stab != usymtab_undefined);
4104 } while (isReturn && (stab->lexlevel >= paramsScope));
4106 sRefSet_free (checked);
4110 ** all globals are appropriately defined
4111 ** all parameters are appropriately defined
4112 ** special clauses are followed
4115 if (isReturn || (utab->lexlevel == paramsScope))
4117 uentry fcn = context_getHeader ();
4118 uentryList params = context_getParams ();
4119 globSet uglobs = context_getUsedGlobs ();
4120 globSet sglobs = context_getGlobs ();
4122 if (isReturn && context_maybeSet (FLG_GLOBALIAS))
4124 aliasTable_checkGlobs (utab->aliases);
4128 ** state clauses (ensures, defines, sets, allocates, releases)
4131 if (uentry_hasStateClauseList (fcn))
4133 stateClauseList clauses = uentry_getStateClauseList (fcn);
4135 stateClauseList_elements (clauses, cl)
4137 if (stateClause_isAfter (cl) && !stateClause_isGlobal (cl))
4139 if (stateClause_setsMetaState (cl))
4141 sRefSet rfs = stateClause_getRefs (cl);
4142 qual q = stateClause_getMetaQual (cl);
4143 annotationInfo ainfo = qual_getAnnotationInfo (q);
4144 metaStateInfo minfo = annotationInfo_getState (ainfo);
4145 cstring key = metaStateInfo_getName (minfo);
4146 int mvalue = annotationInfo_getValue (ainfo);
4148 DPRINTF (("Post meta state clause: %s", stateClause_unparse (cl)));
4150 sRefSet_elements (rfs, el)
4152 sRef base = sRef_getRootBase (el);
4154 if (sRef_isResult (base))
4157 ** This is checked for return transfers.
4161 else if (sRef_isParam (base) || sRef_isGlobalMarker (base))
4163 sRef sr = sRef_updateSref (base);
4164 sr = sRef_fixBase (el, sr);
4166 if (!sRef_checkMetaStateValue (sr, key, mvalue))
4171 ("Ensures clause not satisfied%q (state is %q): %q",
4172 sRef_isGlobalMarker (sr)
4174 : message (" by %q", sRef_unparse (sr)),
4175 stateValue_unparseValue (sRef_getMetaStateValue (sr, key),
4177 stateClause_unparse (cl)),
4180 sRef_showMetaStateInfo (sr, key);
4186 if (sRef_isMeaningful (el))
4191 } end_sRefSet_elements ;
4195 /* evs - 2000 07 10 - added this */
4196 sRefTest tst = stateClause_getPostTestFunction (cl);
4197 sRefSet rfs = stateClause_getRefs (cl);
4199 sRefSet_elements (rfs, el)
4201 sRef base = sRef_getRootBase (el);
4203 if (sRef_isResult (base))
4206 ** This is checked for return transfers.
4211 else if (sRef_isParam (base))
4213 sRef sr = sRef_updateSref (base);
4214 sr = sRef_fixBase (el, sr);
4216 if (tst != NULL && !tst (sr))
4219 (stateClause_postErrorCode (cl),
4220 message ("%s storage %qcorresponds to "
4221 "storage listed in %q clause",
4222 stateClause_postErrorString (cl, sr),
4223 sRef_unparseOpt (sr),
4224 stateClause_unparseKind (cl)),
4227 sRefShower ss = stateClause_getPostTestShower (cl);
4234 DPRINTF (("Storage: %s", sRef_unparseFull (sr)));
4240 if (sRef_isMeaningful (el))
4245 } end_sRefSet_elements ;
4248 } end_stateClauseList_elements ;
4252 ** check parameters on return
4255 uentryList_elements (params, arg)
4257 if (!uentry_isElipsisMarker (arg))
4259 ctype rt = ctype_realType (uentry_getType (arg));
4261 if (ctype_isMutable (rt) || ctype_isSU (rt))
4263 uentry param = usymtab_lookupQuiet (utab, uentry_rawName (arg));
4264 DPRINTF (("Check param return: %s", uentry_unparseFull (param)));
4265 transferChecks_paramReturn (param);
4268 } end_uentryList_elements;
4270 DPRINTF (("Check global return: %s",
4271 globSet_unparse (sglobs)));
4273 globSet_allElements (sglobs, el)
4275 sRef orig = el; /*! sRef_updateSref (el); */ /*!!!*/
4276 uentry current = sRef_getUentry (el);
4278 DPRINTF (("Check global return: %s / %s", sRef_unparseFull (el),
4279 uentry_unparseFull (current)));
4281 if (uentry_isVariable (current) && !uentry_isRealFunction (current))
4283 checkGlobalReturn (current, orig);
4285 } end_globSet_allElements;
4287 globSet_allElements (uglobs, el)
4289 if (!globSet_member (sglobs, el))
4291 uentry current = sRef_getUentry (el);
4293 if (uentry_isVariable (current)
4294 && !uentry_isRealFunction (current))
4296 checkGlobalReturn (current, sRef_undefined);
4299 } end_globSet_allElements;
4304 usymtab_quietExitScope (fileloc loc)
4305 /*@globals utab, globtab, filetab; @*/
4308 usymtab t = utab->env;
4310 if (utab->reftable != NULL)
4314 for (i = 0; i < utab->nentries; i++)
4316 uentry current = utab->entries[i];
4317 uentry old = usymtab_lookupAux (t, uentry_rawName (current));
4319 uentry_mergeState (old, current, loc, FALSE, FALSE, FALSE, NOCLAUSE);
4323 llassert (t != NULL);
4325 if (t->lexlevel > paramsScope)
4327 t->guards = guardSet_levelUnion (t->guards, utab->guards, t->lexlevel);
4328 t->aliases = aliasTable_levelUnionSeq (t->aliases, utab->aliases,
4330 utab->aliases = aliasTable_undefined;
4333 t->mustBreak = utab->mustBreak;
4334 t->exitCode = utab->exitCode;
4336 usymtab_freeLevel (utab);
4342 ** Exit a scope with no checking, lose alias states.
4343 ** (When should this be used?)
4346 void usymtab_quietPlainExitScope (void)
4347 /*@globals utab, globtab, filetab@*/
4350 usymtab t = utab->env;
4352 llassert (t != NULL);
4353 llassert (NOALIAS (utab->aliases, t->aliases));
4354 usymtab_freeLevel (utab);
4358 void usymtab_exitScope (exprNode expr)
4359 /*@globals utab, filetab, globtab@*/
4360 /*@modifies utab, globtab@*/
4362 usymtab ctab = usymtab_undefined;
4363 usymtab lctab = usymtab_undefined;
4364 bool mustReturn = exprNode_mustEscape (expr);
4366 DPRINTF (("Exit scope"));
4368 if (utab->kind == US_CBRANCH)
4371 ** save the case branches, remove the first non-cbranch
4376 while (utab->kind == US_CBRANCH)
4380 llassert (utab != GLOBAL_ENV);
4384 if (utab->kind == US_TBRANCH || utab->kind == US_FBRANCH
4385 || utab->kind == US_CBRANCH || utab->kind == US_SWITCH) {
4387 if (context_inMacro ()) {
4388 /* evs 2000-07-25 */
4389 /* Unparseable macro may end inside nested scope. Deal with it. */
4391 llerror (FLG_SYNTAX,
4392 message ("Problem parsing macro body of %s (unbalanced scopes). "
4393 "Attempting to recover, recommend /*@notfunction@*/ before "
4394 "macro definition.",
4395 context_inFunctionName ()));
4397 while (utab->kind == US_TBRANCH
4398 || utab->kind == US_FBRANCH
4399 || utab->kind == US_CBRANCH
4400 || utab->kind == US_SWITCH)
4403 llassert (utab != GLOBAL_ENV);
4406 llcontbug (message ("exitScope: in branch: %q", usymtab_unparseStack ()));
4408 } /*@=branchstate@*/
4412 ** check all variables in scope were used
4416 ** bogus errors if this is the normal inside a switch,
4417 ** since cases have not been merged yet. Should probably
4418 ** still check this, but I'm too lazy at the moment...
4421 llassertfatal (utab->env != GLOBAL_ENV);
4423 if (utab->env->kind != US_SWITCH)
4429 ** check aliasing: all only params are released (dead)
4430 ** definition: all out params are defined, all modified params
4431 ** are completely defined
4433 ** NOTE: note for exiting paramsScope, since checkReturn should be
4437 if (!mustReturn && (usymtab_lexicalLevel () > functionScope))
4440 ** should only call this is end of scope is reachable...
4443 usymtab_checkFinalScope (FALSE);
4446 if (usymtab_lexicalLevel () == paramsScope && context_inFunctionLike ())
4449 ** leaving a function, need to fix up globals
4452 uentryList params = context_getParams ();
4453 globSet globs = context_getUsedGlobs ();
4456 uentryList_elements (params, ue)
4458 uentry_fixupSref (ue);
4459 } end_uentryList_elements;
4461 clearFunctionTypes ();
4464 globSet_allElements (globs, el)
4466 if (sRef_isCvar (el))
4469 int index = sRef_getScopeIndex (el);
4471 if (sRef_isFileStatic (el))
4473 llassert (usymtab_isDefined (filetab));
4474 current = usymtab_fetchIndex (filetab, index);
4478 current = usymtab_fetchIndex (globtab, index);
4481 if (uentry_isVariable (current))
4483 uentry_fixupSref (current);
4487 sRef_clearDerived (uentry_getSref (current));
4490 } end_globSet_allElements;
4493 usymtab_quietExitScope (exprNode_loc (expr));
4495 if (lctab != usymtab_undefined)
4497 /*@i@*/ lctab->env = utab;
4498 /*@i@*/ utab = ctab;
4499 /*@-branchstate@*/ } /*@=branchstate@*/
4505 ** yikes! don't let the '170 kids see this one...
4509 uentry_directParamNo (uentry ue)
4511 if (uentry_isVar (ue))
4513 sRef sr = uentry_getSref (ue);
4515 if (sRef_lexLevel (sr) == functionScope)
4520 llassert (sr->info != NULL);
4521 llassert (sr->info->cvar != NULL);
4522 index = sr->info->cvar->index;
4525 if (index < uentryList_size (context_getParams ()))
4534 /*@dependent@*/ /*@exposed@*/ uentry
4535 usymtab_getParam (int paramno)
4539 ** requires in a function context (checked)
4541 ** depends on no nested functions --- the function
4542 ** parameters are ALWAYS one scope inside the global scope
4543 ** and entered in order!
4547 if (!context_inFunctionLike ())
4548 llfatalbug (message ("usymtab_getParam: not in function context: %q",
4549 context_unparse ()));
4551 while (s->lexlevel > paramsScope)
4556 llassert (usymtab_isDefined (s));
4558 if (paramno >= s->nentries)
4561 ** Parse errors lead to this.
4564 uentry err = uentry_makeVariableLoc (cstring_makeLiteralTemp ("<error>"),
4567 uentry_markOwned (err);
4571 return (s->entries[paramno]);
4574 static /*@dependent@*/ /*@exposed@*/ uentry
4575 usymtab_getRefTab (/*@notnull@*/ usymtab u, int level, usymId index)
4579 ue = usymtab_getRefNoisy (u, level, index);
4581 if (uentry_isUndefined (ue))
4583 llbug (message ("usymtab_getRef: out of range: %d. level = %d",
4590 static /*@dependent@*/ /*@exposed@*/ usymtab
4591 usymtab_dropEnv (/*@notnull@*/ usymtab s)
4593 if (s->kind == US_CBRANCH)
4601 llassert (s != GLOBAL_ENV);
4602 } while (s->kind == US_CBRANCH);
4603 /* drop all cases (except in nested scopes */
4606 llassert (s != GLOBAL_ENV);
4609 if (s->kind == US_FBRANCH)
4611 s = s->env; /* skip the true branch */
4612 llassert (usymtab_isDefined (s));
4613 llassert (s->kind == US_TBRANCH);
4616 llassert (s != GLOBAL_ENV);
4622 /*@dependent@*/ /*@exposed@*/ uentry
4623 usymtab_getRefQuiet (int level, usymId index)
4629 llassert (s != NULL);
4630 llassert (index >= 0);
4632 if (level > s->lexlevel)
4634 return uentry_undefined;
4637 llassertprint (level <= s->lexlevel, ("level: %d / lexlevel: %d",
4638 level, s->lexlevel));
4640 while (s->lexlevel > level)
4642 if (usymtab_isBranch (s))
4644 int eindex = refTable_lookup (s, level, index);
4646 if (eindex != NOT_FOUND)
4648 return (s->entries[eindex]);
4652 s = usymtab_dropEnv (s);
4655 while (usymtab_isBranch (s) && s->lexlevel == level)
4657 int eindex = refTable_lookup (s, level, index);
4659 if (eindex != NOT_FOUND)
4661 return (s->entries[eindex]);
4664 s = usymtab_dropEnv (s);
4667 if (index >= s->nentries)
4669 return uentry_undefined;
4672 llassert (!uentry_isUndefined (s->entries[index]));
4674 return s->entries[index];
4677 static /*@dependent@*/ /*@exposed@*/ uentry
4678 usymtab_getRefNoisy (/*@notnull@*/ usymtab s, int level, usymId index)
4681 uentry ue = uentry_undefined;
4683 llassert (index >= 0);
4685 while (s->lexlevel > level)
4687 if (usymtab_isBranch (s))
4689 int eindex = refTable_lookup (s, level, index);
4691 if (eindex != NOT_FOUND)
4693 ue = s->entries[eindex];
4697 while (!usymtab_isBranch (otab))
4699 otab = usymtab_dropEnv (otab);
4700 llassert (otab != GLOBAL_ENV);
4703 if (refTable_lookup (otab, level, index) == NOT_FOUND)
4705 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4717 s = usymtab_dropEnv (s);
4720 llassert (usymtab_isDefined (s));
4722 while (usymtab_isBranch (s) && s->lexlevel == level)
4724 int eindex = refTable_lookup (s, level, index);
4727 if (eindex != NOT_FOUND)
4729 ue = s->entries[eindex];
4733 while (!usymtab_isBranch (otab))
4735 otab = usymtab_dropEnv (otab);
4736 llassert (otab != GLOBAL_ENV);
4739 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4749 s = usymtab_dropEnv (s);
4752 if (s->lexlevel == level && (index < s->nentries))
4754 ue = s->entries[index];
4756 if (uentry_isValid (ue))
4760 while (!usymtab_isBranch (otab))
4762 otab = usymtab_dropEnv (otab);
4764 if (otab == GLOBAL_ENV)
4770 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4781 if (index >= s->nentries)
4783 return uentry_undefined;
4786 llassert (!uentry_isUndefined (s->entries[index]));
4788 return s->entries[index];
4792 ** looking up entries
4794 ** If entry is inside a branch, then copy it, and put it into
4795 ** the branch table.
4799 int refTable_lookup (/*@notnull@*/ usymtab ut, int level, int index)
4801 refTable rt = ut->reftable;
4804 llassert (rt != NULL);
4806 for (i = 0; i < ut->nentries; i++)
4808 if (rt[i]->level == level && rt[i]->index == index)
4818 /*@only@*/ refentry refentry_create (int level, int index)
4820 refentry r = (refentry) dmalloc (sizeof (*r));
4828 static /*@dependent@*/ /*@exposed@*/ uentry
4829 usymtab_addRefEntry (/*@notnull@*/ usymtab s, cstring k)
4834 if (ut->reftable == NULL)
4836 DPRINTF (("Adding ref entry without reftable: %s", k));
4837 return uentry_undefined;
4840 llassert (ut->reftable != NULL);
4842 while (s != GLOBAL_ENV)
4844 eindex = usymtab_getIndex (s, k);
4846 if (eindex != NOT_FOUND)
4848 uentry current = s->entries[eindex];
4850 if (uentry_isVar (current) && !ctype_isFunction (uentry_getType (current)))
4854 DPRINTF (("Here: copying %s", uentry_unparse (current)));
4855 ue = uentry_copy (current);
4856 DPRINTF (("Here: copying %s", uentry_unparse (ue)));
4857 usymtab_addEntryQuiet (ut, ue);
4858 DPRINTF (("Okay..."));
4860 if (s->reftable != NULL)
4862 refentry ref = s->reftable[eindex];
4864 ut->reftable[ut->nentries - 1]
4865 = refentry_create (ref->level, ref->index);
4869 ut->reftable[ut->nentries - 1]
4870 = refentry_create (s->lexlevel, eindex);
4881 s = usymtab_dropEnv (s);
4884 return uentry_undefined;
4887 static uentry usymtab_lookupAux (usymtab s, cstring k)
4889 DPRINTF (("Lookup: %s", k));
4891 while (s != GLOBAL_ENV)
4893 int eindex = usymtab_getIndex (s, k);
4895 if (eindex != NOT_FOUND)
4897 uentry ret = s->entries[eindex];
4901 if (s->kind == US_TBRANCH
4902 || s->kind == US_FBRANCH
4903 || s->kind == US_CBRANCH)
4904 /* uentry_isGlobalVariable (ret) && os->lexlevel > fileScope) */
4907 DPRINTF (("Adding global ref entry: %s", k));
4908 ret = usymtab_addRefEntry (os, k);
4909 DPRINTF (("Adding ref entry: %s", uentry_unparseFull (ret)));
4914 DPRINTF (("Found: %s", uentry_unparseFull (ret)));
4918 if (s->kind == US_TBRANCH || s->kind == US_FBRANCH
4919 || s->kind == US_CBRANCH)
4921 /* why isn't this os??? */
4922 uentry ret = usymtab_addRefEntry (s, k);
4923 DPRINTF (("Adding ref entry: %s", uentry_unparseFull (ret)));
4930 return uentry_undefined;
4933 static /*@dependent@*/ /*@exposed@*/ uentry
4934 usymtab_lookupQuietAux (usymtab s, cstring k, bool noalt)
4938 while (s != GLOBAL_ENV)
4940 eindex = usymtab_getIndex (s, k);
4942 if (eindex != NOT_FOUND)
4944 uentry ret = s->entries[eindex];
4948 if (noalt && usymtab_isBranch (s))
4950 s = usymtab_dropEnv (s);
4958 return uentry_undefined;
4961 static /*@exposed@*/ /*@dependent@*/ uentry
4962 usymtab_lookupQuiet (usymtab s, cstring k)
4964 return usymtab_lookupQuietAux (s, k, FALSE);
4967 static /*@exposed@*/ /*@dependent@*/ uentry
4968 usymtab_lookupQuietNoAlt (usymtab s, cstring k)
4970 return usymtab_lookupQuietAux (s, k, TRUE);
4973 /*@dependent@*/ /*@observer@*/ uentry
4974 usymtab_lookupSafe (cstring k)
4977 DPRINTF (("Lookup safe: %s", k));
4978 return (usymtab_lookupAux (utab, k));
4982 usymtab_lookupExpose (cstring k)
4985 uentry ce = usymtab_lookupAux (utab, k);
4987 if (uentry_isUndefined (ce))
4989 llfatalbug (message ("usymtab_lookup: not found: *%s*", k));
4992 if (uentry_isPriv (ce))
4994 llfatalbug (message ("usymtab_lookup: private: *%s*", k));
5000 uentry usymtab_lookupExposeGlob (cstring k)
5002 return (usymtab_lookupGlobSafe (k));
5005 uentry usymtab_lookupGlob (cstring k)
5006 /*@globals globtab@*/
5008 uentry ce = usymtab_lookupAux (globtab, k);
5010 if (uentry_isUndefined (ce))
5011 llfatalbug (message ("usymtab_lookup: not found: %s", k));
5013 if (uentry_isPriv (ce))
5014 llfatalbug (message ("usymtab_lookup: private: %s", k));
5019 /*@observer@*/ uentry
5020 usymtab_lookupGlobSafe (cstring k)
5021 /*@globals globtab@*/
5023 uentry ce = usymtab_lookupAux (globtab, k);
5028 uentry usymtab_lookupEither (cstring k)
5031 uentry ce = usymtab_lookupSafe (k);
5033 if (uentry_isUndefined (ce))
5034 llfatalerror (message ("usymtab_lookup: not found: %s", k));
5041 usymtab_lookupType (cstring k)
5042 /*@globals globtab@*/
5044 usymId uid = usymtab_getTypeId (k);
5046 if (uid == USYMIDINVALID)
5048 llcontbug (message ("usymtab_lookupType: not found: %s", k));
5049 return ctype_unknown;
5052 return (uentry_getRealType (usymtab_getTypeEntry (uid)));
5057 usymtab_lookupAbstractType (cstring k) /*@globals globtab@*/
5059 usymId uid = usymtab_getTypeId (k);
5061 if (uid == USYMIDINVALID)
5063 llcontbug (message ("usymtab_lookupType: not found: %s", k));
5064 return ctype_unknown;
5067 return (uentry_getAbstractType (usymtab_getTypeEntry (uid)));
5071 ** if there is an unnamed lcl-specified struct tag matching
5072 ** the uentryList, return its datatype. Otherwise, returns
5077 usymtab_structFieldsType (uentryList f)
5078 /*@globals globtab@*/
5080 return (usymtab_suFieldsType (f, TRUE));
5084 usymtab_unionFieldsType (uentryList f)
5085 /*@globals globtab@*/
5087 return (usymtab_suFieldsType (f, FALSE));
5091 usymtab_suFieldsType (uentryList f, bool isStruct)
5092 /*@globals globtab@*/
5096 DPRINTF (("Fields: %s", uentryList_unparse (f)));
5098 if (fileloc_isSpec (g_currentloc))
5100 return (ctype_undefined);
5103 for (i = 0; i < globtab->nentries; i++)
5105 uentry current = globtab->entries[i];
5108 ? uentry_isStructTag (current) : uentry_isUnionTag (current)))
5110 if (isFakeTag (uentry_rawName (current)))
5112 ctype ct = uentry_getType (current);
5114 DPRINTF (("Check: %s", ctype_unparse (ct)));
5116 if ((isStruct ? ctype_isStruct (ct) : ctype_isUnion (ct))
5118 (uentry_isSpecified (current)
5119 && uentryList_equivFields (f, ctype_getFields (ct))))
5121 return uentry_getAbstractType (current);
5131 return ctype_undefined;
5135 usymtab_enumEnumNameListType (enumNameList f)
5136 /*@globals globtab@*/
5140 for (i = 0; i < globtab->nentries; i++)
5142 uentry current = globtab->entries[i];
5144 if (uentry_isEnumTag (current))
5146 if (isFakeTag (uentry_rawName (current)))
5148 ctype ct = uentry_getType (current);
5150 if (ctype_isEnum (ct) && (enumNameList_match (f, ctype_elist (ct))))
5152 return uentry_getType (current);
5158 return ctype_undefined;
5162 usymtab_exists (cstring k)
5165 uentry ce = usymtab_lookupSafe (k);
5166 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5170 usymtab_existsReal (cstring k)
5173 uentry ce = usymtab_lookupSafe (k);
5175 return (!(uentry_isUndefined (ce))
5176 && !(uentry_isPriv (ce))
5177 && !(uentry_isExpandedMacro (ce)));
5181 usymtab_existsGlob (cstring k)
5182 /*@globals globtab@*/
5184 uentry ce = usymtab_lookupAux (globtab, k);
5186 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5191 usymtab_existsEither (cstring k)
5194 uentry ce = usymtab_lookupAux (utab, k);
5196 return (uentry_isValid (ce));
5200 usymtab_existsGlobEither (cstring k)
5201 /*@globals globtab@*/
5203 uentry ce = usymtab_lookupAux (globtab, k);
5205 return (uentry_isValid (ce));
5210 usymtab_existsType (cstring k)
5211 /*@globals globtab@*/
5213 uentry ce = usymtab_lookupAux (globtab, k);
5215 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)) && uentry_isDatatype (ce));
5219 usymtab_existsTypeEither (cstring k)
5220 /*@globals globtab@*/
5223 ce = usymtab_lookupAux (globtab, k);
5224 return (uentry_isValid (ce) && uentry_isDatatype (ce));
5228 usymtab_existsStructTag (cstring k) /*@globals globtab@*/
5230 cstring sname = makeStruct (k);
5231 uentry ce = usymtab_lookupAux (globtab, sname);
5232 cstring_free (sname);
5233 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5237 usymtab_existsUnionTag (cstring k) /*@globals globtab@*/
5239 cstring uname = makeUnion (k);
5240 uentry ce = usymtab_lookupAux (globtab, uname);
5242 cstring_free (uname);
5244 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5248 usymtab_existsEnumTag (cstring k) /*@globals globtab@*/
5250 cstring ename = makeEnum (k);
5251 uentry ce = usymtab_lookupAux (globtab, ename);
5253 cstring_free (ename);
5254 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5258 bool usymtab_existsVar (cstring k)
5261 uentry ce = usymtab_lookupSafe (k);
5263 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)) && (uentry_isVar (ce)));
5272 refTable_free (/*@only@*/ /*@null@*/ refTable x, int nentries)
5278 for (i = 0; i < nentries; i++)
5288 usymtab_freeLevel (/*@notnull@*/ /*@only@*/ usymtab u)
5289 /*@globals globtab, utab, filetab@*/
5293 aliasTable_free (u->aliases);
5295 refTable_free (u->reftable, u->nentries);
5297 if (u == filetab || u == globtab)
5299 for (i = 0; i < u->nentries; i++)
5301 uentry_freeComplete (u->entries[i]);
5306 for (i = 0; i < u->nentries; i++)
5308 uentry_free (u->entries[i]);
5312 guardSet_free (u->guards);
5319 llassert (!cstringTable_isDefined (u->htable));
5327 usymtab_freeAux (/*@only@*/ usymtab u)
5328 /*@globals globtab, utab, filetab@*/
5331 while (u != GLOBAL_ENV)
5334 usymtab_freeLevel (u);
5341 void usymtab_free ()
5342 /*@globals killed utab, globtab, filetab@*/
5346 usymtab_freeAux (utab);
5349 static int usymtab_lexicalLevel (void) /*@globals utab@*/
5351 return (utab->lexlevel);
5354 bool usymtab_inGlobalScope () /*@globals utab, globtab@*/
5356 return (utab == globtab);
5359 bool usymtab_inFileScope () /*@globals utab@*/
5361 return (utab->lexlevel == fileScope);
5364 bool usymtab_inFunctionScope () /*@globals utab@*/
5366 return (utab->lexlevel == functionScope);
5371 usymtab_replaceEntry (uentry s)
5372 /*@globals utab, globtab@*/
5373 /*@modifies utab, s@*/
5375 usymtab_replaceEntryAux (utab, s);
5380 usymtab_matchForwardStruct (usymId u1, usymId u2)
5381 /*@globals globtab@*/
5383 uentry ue1 = usymtab_getTypeEntry (u1);
5384 uentry ue2 = usymtab_getTypeEntry (u2);
5386 if (uentry_isAnyTag (ue2))
5388 ctype reptype = uentry_getType (ue1);
5390 if (ctype_isPointer (reptype))
5392 ctype repbase = ctype_getBaseType (reptype);
5394 if (ctype_isUA (repbase))
5396 typeId rtuid = ctype_typeId (repbase);
5398 if (u2 == rtuid) return TRUE;
5400 if (usymId_isValid (rtuid))
5402 reptype = uentry_getType (usymtab_getTypeEntry (rtuid));
5404 return (ctype_isUA (reptype) && (u2 == (ctype_typeId (reptype))));
5413 void usymtab_addGuards (guardSet guards)
5416 utab->guards = guardSet_union (utab->guards, guards);
5419 static bool usymtab_isGuardedAux (sRef s)
5423 sRef base = sRef_getRootBase (s);
5424 int lowlevel = paramsScope;
5425 int baselevel = sRef_lexLevel (base);
5427 if (sRef_isCvar (base))
5429 lowlevel = baselevel;
5430 if (lowlevel < paramsScope) lowlevel = paramsScope;
5433 while (tab->lexlevel >= lowlevel)
5435 DPRINTF (("Is guarded? [%s] %s",
5436 guardSet_unparse (tab->guards),
5437 sRef_unparseFull (s)));
5439 if (guardSet_isGuarded (tab->guards, s))
5442 if (!sRef_definitelyNull (s))
5444 sRef_setNotNull (s, fileloc_undefined);
5450 tab = usymtab_dropEnv (tab);
5456 void usymtab_unguard (sRef s) /*@modifies utab@*/
5459 sRef base = sRef_getRootBase (s);
5460 int lowlevel = paramsScope;
5461 int baselevel = sRef_lexLevel (base);
5463 if (sRef_isCvar (base))
5465 lowlevel = baselevel;
5466 if (lowlevel < paramsScope) lowlevel = paramsScope;
5469 while (tab->lexlevel >= lowlevel)
5471 if (guardSet_isGuarded (tab->guards, s))
5473 guardSet_delete (tab->guards, s);
5476 tab = usymtab_dropEnv (tab);
5480 bool usymtab_isGuarded (sRef s)
5482 DPRINTF (("Is guarded? %s", sRef_unparseFull (s)));
5483 return (sRef_aliasCompleteSimplePred (usymtab_isGuardedAux, s));
5486 bool usymtab_isDefinitelyNull (sRef s)
5488 return (sRef_aliasCheckSimplePred (usymtab_isDefinitelyNullAux, s));
5491 bool usymtab_isDefinitelyNullDeep (sRef s)
5493 return (sRef_deepPred (usymtab_isDefinitelyNull, s));
5496 static bool usymtab_isDefinitelyNullAux (sRef s)
5500 sRef base = sRef_getRootBase (s);
5501 int lowlevel = paramsScope;
5503 if (sRef_isCvar (base))
5505 lowlevel = sRef_lexLevel (base);
5506 if (lowlevel < paramsScope) lowlevel = paramsScope;
5509 while (tab->lexlevel >= lowlevel)
5511 if (guardSet_mustBeNull (tab->guards, s))
5516 while (tab->kind == US_CBRANCH)
5521 llassert (usymtab_isDefined (tab));
5523 if (tab->kind == US_FBRANCH)
5526 llassert (tab->kind == US_TBRANCH);
5536 usymtab_printGuards ()
5537 /*@globals utab, globtab@*/
5539 usymtab ttab = utab;
5541 while (ttab != globtab)
5543 llmsg (message ("Guards [%d]: %q", ttab->lexlevel,
5544 guardSet_unparse (ttab->guards)));
5550 usymtab_displayAllUses ()
5551 /*@globals utab, globtab@*/
5555 /* only in top scope */
5556 llassert (utab == globtab);
5558 /* need a copy, so order is not messed up by sort! */
5559 copy = usymtab_shallowCopy (globtab);
5561 qsort (copy->entries, (size_t)copy->nentries,
5562 sizeof (*copy->entries), (int (*)(const void *, const void *)) uentry_xcompareuses);
5564 usymtab_entries (copy, ue)
5566 if (uentry_isValid (ue))
5568 filelocList uses = uentry_getUses (ue);
5569 int size = filelocList_realSize (uses);
5571 if (fileloc_isDefined (uentry_whereDefined (ue))
5572 && !fileloc_isLib (uentry_whereDefined (ue))
5575 llmsg (message ("%q (%q), %d use%&:\n %q",
5576 uentry_getName (ue),
5577 fileloc_unparse (uentry_whereDefined (ue)),
5578 size, filelocList_unparseUses (uses)));
5581 } end_usymtab_entries;
5583 usymtab_shallowFree (copy);
5586 static /*@dependent@*/ /*@exposed@*/ usymtab
5587 usymtab_getFileTab ()
5588 /*@globals filetab@*/
5590 llassert (filetab != NULL);
5596 usymtab_unparseStack ()
5599 return (usymtab_unparseStackTab (utab));
5602 static /*@only@*/ cstring
5603 usymtab_unparseStackTab (usymtab t)
5605 bool firstOne = TRUE;
5606 cstring ret = cstring_makeLiteral ("[");
5608 while (t != GLOBAL_ENV)
5612 ret = message ("%q %q", ret, usymtab_typeName (t));
5617 ret = message ("%q, %q", ret, usymtab_typeName (t));
5622 ret = message ("%q ]", ret);
5626 static /*@only@*/ cstring
5627 usymtab_typeName (/*@notnull@*/ usymtab t)
5631 case US_GLOBAL: return cstring_makeLiteral ("global");
5632 case US_NORMAL: return cstring_makeLiteral ("normal");
5633 case US_TBRANCH: return cstring_makeLiteral ("true");
5634 case US_FBRANCH: return cstring_makeLiteral ("false");
5635 case US_CBRANCH: return cstring_makeLiteral ("case");
5636 case US_SWITCH: return cstring_makeLiteral ("switch");
5642 void usymtab_addMustAlias (/*@exposed@*/ sRef s, /*@exposed@*/ sRef al)
5645 if (!sRef_similar (s, al))
5647 usymtab_addForceMustAlias (s, al);
5652 ** Same as usymtab_addMustAlias, except does not check sRef_isSimilar.
5655 void usymtab_addForceMustAlias (/*@exposed@*/ sRef s, /*@exposed@*/ sRef al)
5658 if (sRef_isMeaningful (s)
5659 && sRef_isMeaningful (al)
5660 && !(sRef_isConst (s) || sRef_isConst (al))
5661 && !(sRef_isAddress (al) && sRef_isDirectParam (sRef_getBase (al))))
5663 utab->aliases = aliasTable_addMustAlias (utab->aliases, s, al);
5664 DPRINTF (("Must alias: %s", aliasTable_unparse (utab->aliases)));
5667 ** for local variable, aliasing is symmetric
5670 if (sRef_isLocalVar (s) && sRef_isLocalVar (al))
5672 utab->aliases = aliasTable_addMustAlias (utab->aliases, al, s);
5681 void usymtab_clearAlias (sRef s)
5682 /*@modifies utab, s@*/
5685 aliasTable_clearAliases (utab->aliases, s);
5688 sRefSet usymtab_allAliases (sRef s)
5691 if (sRef_isMeaningful (s))
5695 ret = sRefSet_unionFree (aliasTable_aliasedBy (utab->aliases, s),
5696 aliasTable_canAlias (utab->aliases, s));
5701 DPRINTF (("NOT A MEANINGFUL SREF!"));
5702 return sRefSet_undefined;
5706 /*@only@*/ sRefSet usymtab_canAlias (sRef s)
5709 if (sRef_isMeaningful (s))
5711 sRefSet res = aliasTable_canAlias (utab->aliases, s);
5715 return sRefSet_undefined;
5718 /*@only@*/ sRefSet usymtab_aliasedBy (sRef s)
5721 return (aliasTable_aliasedBy (utab->aliases, s));
5724 /*@only@*/ cstring usymtab_unparseAliases ()
5727 return (aliasTable_unparse (utab->aliases));
5731 ** Debugging routines:
5732 ** okay to leak storage here, only for debugging
5738 usymtab_printOut (void)
5744 char *ind = mstring_copy (" ");
5746 fprintf (g_msgstream, "<<< [symbol table] >>>\n");
5748 while (s != GLOBAL_ENV && s->env != GLOBAL_ENV)
5750 cstring tname = usymtab_typeName (s);
5754 ind[depth * 3 + 1] = '\0';
5757 fprintf (g_msgstream, "level: %d (%s)\n", s->lexlevel,
5758 cstring_toCharsSafe (tname));
5760 cstring_free (tname);
5762 for (i = 0; i < s->nentries; i++)
5764 cstring us = uentry_unparseFull (s->entries[i]);
5765 fprintf (g_msgstream, "%s\n", cstring_toCharsSafe (us));
5769 if (s->reftable != NULL && s->nentries > 0)
5771 fprintf (g_msgstream, "\t<< Ref table >>\n");
5773 for (i = 0; i < s->nentries; i++)
5775 fprintf (g_msgstream, "\t%s %3d: %d, %d\n", ind, i,
5776 s->reftable[i]->level,
5777 s->reftable[i]->index);
5781 ind[depth * 3 + 1] = ' ';
5785 fprintf (g_msgstream, "<<< end usymtab >>>\n");
5791 usymtab_printTypes ()
5792 /*@globals globtab@*/
5794 usymtab_printAllAux (globtab);
5798 usymtab_printAll (void)
5801 usymtab_printAllAux (utab);
5805 usymtab_printAllAux (usymtab s)
5806 /*@modifies g_msgstream@*/
5810 char *ind = mstring_copy (" ");
5812 printf ("[[[ usymtab ]]]");
5814 while (s != GLOBAL_ENV)
5817 ind[depth * 3 + 1] = '\0';
5819 if (s->env == GLOBAL_ENV)
5823 printf ("level: %d / break: %s / exit: %s\n", s->lexlevel,
5824 cstring_toCharsSafe (bool_unparse (s->mustBreak)),
5825 cstring_toCharsSafe (exitkind_unparse (s->exitCode)));
5829 for (i = looplow; i < s->nentries; i++)
5831 printf ("%s%3d. %s\n", ind, i,
5832 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5837 printf ("level: %d / break: %s / exit: %s\n", s->lexlevel,
5838 cstring_toCharsSafe (bool_unparse (s->mustBreak)),
5839 cstring_toCharsSafe (exitkind_unparse (s->exitCode)));
5841 for (i = 0; i < s->nentries; i++)
5843 printf ("%s%3d %s\n", ind, i,
5844 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5848 ind[depth * 3 + 1] = ' ';
5852 printf ("----------\n");
5856 usymtab_printComplete ()
5861 char *ind = mstring_copy (" ");
5864 while (s != GLOBAL_ENV)
5868 ind[depth * 3 + 1] = '\0';
5871 if (s->env == GLOBAL_ENV)
5875 printf ("level: %d\n", s->lexlevel);
5879 for (i = looplow; i < s->nentries; i++)
5881 printf ("%s%3d %s\n", ind, i,
5882 cstring_toCharsSafe (uentry_unparse (s->entries[i])));
5887 printf ("level: %d\n", s->lexlevel);
5888 for (i = 0; i < s->nentries; i++)
5890 printf ("%s%3d %s\n", ind, i,
5891 cstring_toCharsSafe (uentry_unparse (s->entries[i])));
5895 ind[depth * 3 + 1] = ' ';
5900 printf ("----------\n");
5905 static /*@only@*/ cstring /*@unused@*/
5906 usymtab_unparseLocalAux (/*@notnull@*/ usymtab s)
5908 cstring c = message ("lexlevel: %d\n", s->lexlevel);
5911 for (i = 0; i < s->nentries; i++)
5913 c = message ("%q\n%q", c, uentry_unparseFull (s->entries[i]));
5916 c = message ("%q\n=========", c);
5920 static cstring /*@unused@*/ /*@only@*/
5921 usymtab_unparseLocalList (/*@notnull@*/ usymtab s)
5923 cstring c = message ("[%d/%s/%s] ", s->lexlevel,
5924 bool_unparse (s->mustBreak),
5925 exitkind_unparse (s->exitCode));
5928 for (i = 0; i < s->nentries; i++)
5930 sRef sr = uentry_getSref (s->entries[i]);
5934 c = message ("%q: %q [%b]", c, uentry_getName (s->entries[i]),
5935 sRef_isStateDefined (sr));
5939 c = message ("%q, %q [%b]", c, uentry_getName (s->entries[i]),
5940 sRef_isStateDefined (sr));
5950 usymtab_printLocal (void)
5956 printf ("lexlevel: %d\n", s->lexlevel);
5958 for (i = 0; i < s->nentries; i++)
5960 printf ("%s\n", cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5963 while (s->lexlevel > 1)
5968 llassert (usymtab_isDefined (s));
5970 printf ("Params:\n");
5972 for (i = 0; i < s->nentries; i++)
5974 printf ("%d: %s\n", i,
5975 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5980 static bool checkDistinctExternalName (uentry e)
5981 /*@globals globtab@*/
5982 /*@modifies *g_msgstream@*/
5984 int checklen = context_getValue (FLG_EXTERNALNAMELEN);
5985 bool ignorecase = context_getFlag (FLG_EXTERNALNAMECASEINSENSITIVE);
5986 bool gotone = FALSE;
5987 bool extras = FALSE;
5988 bool hasError = FALSE;
5989 cstring name = uentry_rawName (e);
5990 usymtab st = globtab;
5998 if (uentry_isAnyTag (e))
6000 checklen++; /* the tag marker doesn't count */
6004 usymtab_entries (st, oe)
6006 if (uentry_sameObject (oe, e))
6013 if (cstring_equalCaseInsensitive (uentry_rawName (oe), name))
6022 (FLG_DISTINCTEXTERNALNAMES,
6024 ("External identifier %q is not distinguishable from %q "
6025 "because alphabetical case is ignored",
6027 uentry_getName (oe)),
6028 uentry_whereLast (e)))
6030 uentry_showWhereAny (oe);
6031 uentry_setHasNameError (oe);
6040 if (cstring_equalLenCaseInsensitive (uentry_rawName (oe),
6049 if (cstring_equalLen (uentry_rawName (oe), name, checklen))
6052 (FLG_DISTINCTEXTERNALNAMES,
6055 ("External identifier %q is not distinguishable from %q "
6056 "in the first %d characters (%q)",
6058 uentry_getName (oe),
6060 cstring_clip (uentry_getName (e), checklen)),
6062 uentry_whereLast (e)))
6064 uentry_showWhereAny (oe);
6065 uentry_setHasNameError (oe);
6078 (FLG_DISTINCTEXTERNALNAMES,
6080 ("External identifier %q is not distinguishable from %q "
6081 "in the first %d characters because alphabetical case "
6084 uentry_getName (oe),
6086 uentry_whereLast (e)))
6088 uentry_showWhereAny (oe);
6089 uentry_setHasNameError (oe);
6095 else if (cstring_equalLen (uentry_rawName (oe), name, checklen))
6104 (FLG_DISTINCTEXTERNALNAMES,
6107 ("External identifier %q is not distinguishable from %q "
6108 "in the first %d characters (%q)",
6110 uentry_getName (oe),
6112 cstring_clip (uentry_getName (e), checklen)),
6114 uentry_whereLast (e)))
6116 uentry_showWhereAny (oe);
6117 uentry_setHasNameError (oe);
6126 } end_usymtab_entries ;
6133 (cstring_makeLiteral ("One or more additional "
6134 "indistinguishable external "
6135 "names not reported"));
6141 static bool checkDistinctInternalName (uentry e)
6143 /*@modifies *g_msgstream@*/
6145 usymtab ttab = utab;
6146 cstring name = uentry_rawName (e);
6147 int numchars = context_getValue (FLG_INTERNALNAMELEN);
6148 bool caseinsensitive = context_getFlag (FLG_INTERNALNAMECASEINSENSITIVE);
6149 bool lookalike = context_getFlag (FLG_INTERNALNAMELOOKALIKE);
6151 if (uentry_isAnyTag (e) && (numchars != 0))
6153 numchars++; /* the tag marker doesn't count */
6156 while (usymtab_isDefined (ttab))
6158 usymtab_entries (ttab, oe)
6160 if (uentry_sameObject (oe, e))
6162 /*@innercontinue@*/ continue;
6165 switch (cstring_genericEqual
6166 (name, uentry_rawName (oe),
6167 numchars, caseinsensitive, lookalike))
6169 case CGE_DISTINCT: /* okay */
6173 if (cstring_equal (name, uentry_rawName (oe)))
6175 ; /* got a shadow error */
6180 (FLG_DISTINCTINTERNALNAMES,
6183 ("Internal identifier %q is not distinguishable from %q "
6184 "in the first %d characters (%q)",
6186 uentry_getName (oe),
6188 cstring_clip (uentry_getName (e), numchars)),
6190 uentry_whereLast (e)))
6192 uentry_showWhereAny (oe);
6193 uentry_setHasNameError (oe);
6201 || (cstring_length (name) <= numchars))
6204 (FLG_DISTINCTINTERNALNAMES,
6206 ("Internal identifier %q is not distinguishable from %q "
6207 "without case sensitivity",
6209 uentry_getName (oe)),
6210 uentry_whereLast (e)))
6212 uentry_showWhereAny (oe);
6213 uentry_setHasNameError (oe);
6220 (FLG_DISTINCTINTERNALNAMES,
6222 ("Internal identifier %q is not distinguishable from %q "
6223 "in the first %d characters without case sensitivity",
6225 uentry_getName (oe),
6227 uentry_whereLast (e)))
6229 uentry_showWhereAny (oe);
6230 uentry_setHasNameError (oe);
6238 || (cstring_length (name) <= numchars))
6241 (FLG_DISTINCTINTERNALNAMES,
6243 ("Internal identifier %q is not distinguishable from %q "
6244 "except by lookalike characters",
6246 uentry_getName (oe)),
6247 uentry_whereLast (e)))
6249 uentry_showWhereAny (oe);
6250 uentry_setHasNameError (oe);
6257 (FLG_DISTINCTINTERNALNAMES,
6259 ("Internal identifier %q is not distinguishable from %q "
6260 "in the first %d characters except by lookalike characters",
6262 uentry_getName (oe),
6264 uentry_whereLast (e)))
6266 uentry_showWhereAny (oe);
6267 uentry_setHasNameError (oe);
6272 } end_usymtab_entries ;
6280 void usymtab_checkDistinctName (uentry e, int scope)
6281 /*@globals utab, globtab@*/
6283 bool hasError = FALSE;
6284 fileloc where = uentry_whereLast (e);
6286 if (!fileloc_isPreproc (where) && !fileloc_isBuiltin (where))
6288 if (scope == globScope)
6290 if (context_getFlag (FLG_DISTINCTEXTERNALNAMES))
6292 hasError = checkDistinctExternalName (e);
6296 if (!hasError && context_getFlag (FLG_DISTINCTINTERNALNAMES))
6298 hasError = checkDistinctInternalName (e);
6304 uentry_setHasNameError (e);
6308 /*@exposed@*/ sRef usymtab_lookupGlobalMarker (void) /*@globals utab@*/
6312 ue = usymtab_lookupAux (utab, GLOBAL_MARKER_NAME);
6313 llassert (uentry_isValid (ue));
6315 return uentry_getSref (ue);