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_warningstream@*/ ;
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 eindex = usymtab_getIndex (st, ename);
763 if (eindex != NOT_FOUND)
765 uentry ce = st->entries[eindex];
767 DPRINTF (("Found entry: %s", uentry_unparse (ce)));
769 if (uentry_isPriv (ce)
770 /* || (fileloc_isImport (uentry_whereSpecified (ce))) */
771 || (uentry_isStatic (ce)
772 && uentry_isAnyTag (ce)
773 && (uentry_isDeclared (ce)
774 && !fileloc_sameFile (uentry_whereDefined (ce),
775 uentry_whereDefined (e)))))
779 ** if overloading import should do some checks!
782 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
784 DPRINTF (("Overloading!"));
786 st->entries[eindex] = e;
788 if (uentry_isDatatype (e))
790 uentry_setDatatype (e, eindex);
793 if (st == globtab && !uentry_isSpecified (e))
795 exprChecks_checkExport (e);
798 if (cstringTable_isDefined (st->htable))
800 cstringTable_replaceKey (st->htable, uentry_rawName (ce),
801 cstring_copy (uentry_rawName (e)));
807 else if (uentry_isSpecified (ce))
809 if (uentry_isSpecified (e))
811 DPRINTF (("Here we are: %s", uentry_unparseFull (e)));
813 if (fileloc_isImport (uentry_whereSpecified (ce)))
815 if (cstringTable_isDefined (st->htable))
817 cstringTable_replaceKey (st->htable,
819 cstring_copy (uentry_rawName (e)));
823 st->entries[eindex] = e;
826 if (uentry_isDatatype (e)) uentry_setDatatype (e, eindex);
830 if (fileloc_isImport (uentry_whereSpecified (e)))
836 /* Respecification errors already reported */
837 DPRINTF (("Respecification: %s / %s",
838 fileloc_unparse (uentry_whereSpecified (e)),
839 bool_unparse (fileloc_isSpec (uentry_whereSpecified (e)))));
841 if (uentry_isDatatype (e))
843 uentry_setDatatype (e, eindex);
846 if (cstringTable_isDefined (st->htable))
848 cstringTable_replaceKey (st->htable,
850 cstring_copy (uentry_rawName (e)));
853 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
855 st->entries[eindex] = e;
860 else /* e not specified */
862 DPRINTF (("Merging..."));
864 if (uentry_isDeclared (ce))
867 No - this can happen for internal typedefs
868 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
871 DPRINTF (("Merge defn"));
872 uentry_mergeDefinition (ce, e);
877 No - this can happen for internal typedefs
878 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
881 DPRINTF (("Merge entries..."));
882 uentry_mergeEntries (ce, e);
883 DPRINTF (("After: %s", uentry_unparseFull (ce)));
887 else /* ce not specified */
889 if (!(st->lexlevel > fileScope || !sRef_modInFunction ()))
891 if (uentry_isDatatype (e) || uentry_isAnyTag (e)
892 || uentry_isEnumConstant (e))
895 ** Not a bug. Code like,
896 ** int f (void) { typedef int tint; ... }
902 llcontbug (message ("usymtab_supEntryAux: inconsistent state "
903 "(lexlevel = %d, modFunction = %s) adding: %q",
904 st->lexlevel, bool_unparse (sRef_modInFunction ()),
905 uentry_unparse (e)));
907 if (sRef_modInFunction ())
909 /* make sure the sRef is not bashed! */
910 /*@i@*/ e->sref = sRef_saveCopy (e->sref);
915 DPRINTF (("Merge.."));
916 uentry_mergeDefinition (ce, e);
921 ctype ct = uentry_getType (ce);
923 if (uentry_isFunction (ce) && ctype_isFunction (ct))
925 ct = ctype_getReturnType (ct);
928 uentry_setSref (ce, sRef_makeCvar (st->lexlevel, eindex, ct, stateInfo_makeLoc (uentry_whereLast (ce))));
931 else /* no previous entry */
935 if (st->lexlevel == globScope
936 && !uentry_isStatic (e)
937 && !uentry_isExtern (e)
938 && usymtab_isDefined (filetab))
941 ** check if there is a static entry: (i.e.,
948 eindex = usymtab_getIndex (filetab, ename);
950 if (eindex != NOT_FOUND)
952 uentry ce = filetab->entries[eindex];
954 uentry_setStatic (e);
955 uentry_mergeDefinition (ce, e);
961 outer = usymtab_lookupQuiet (st->env, ename);
963 DPRINTF (("New : [%p] %s", e, uentry_unparseFull (e)));
964 DPRINTF (("Outer: [%p] %s", outer, uentry_unparseFull (outer)));
967 ** no previous definition, add the new one
971 && uentry_isValid (outer)
972 && !(uentry_isYield (e) || uentry_isYield (outer))
973 && fileloc_isDefined (uentry_whereLast (e))
974 && !fileloc_isXHFile (uentry_whereLast (e))
975 && fileloc_isDefined (uentry_whereLast (outer))
976 && !fileloc_isXHFile (uentry_whereLast (outer)))
978 if (!uentry_sameKind (outer, e))
984 if (ctype_isUnknown (uentry_getType (outer))
985 || uentry_isForward (outer))
993 message ("%s %q shadows outer declaration",
994 ekind_capName (uentry_getKind (e)),
996 uentry_whereLast (e)))
998 uentry_showWhereLast (outer);
1004 if (st == globtab && context_getFlag (FLG_NEWDECL))
1008 message ("New declaration: %q", uentry_getName (e)),
1009 uentry_whereLast (e));
1012 eindex = usymtab_addEntryAux (st, e, isSref);
1016 return (staticEntry ? USYMIDINVALID : eindex);
1021 usymtab_replaceEntryAux (/*@notnull@*/ usymtab st, /*@only@*/ uentry e)
1022 /*@globals globtab@*/ /*@modifies st, e@*/
1024 cstring ename = uentry_rawName (e);
1027 /* static tags in global scope */
1028 eindex = usymtab_getIndex (st, ename);
1030 if (eindex != NOT_FOUND)
1032 uentry ce = st->entries[eindex];
1034 if (cstringTable_isDefined (st->htable))
1036 cstringTable_replaceKey (st->htable, uentry_rawName (ce),
1037 cstring_copy (uentry_rawName (e)));
1041 st->entries[eindex] = e;
1045 eindex = usymtab_addEntryAux (st, e, FALSE);
1052 void usymtab_supEntry (uentry e)
1053 /*@globals utab, filetab, globtab@*/
1054 /*@modifies utab, globtab, e@*/
1056 (void) usymtab_supEntryAux (utab, e, FALSE);
1060 ** this should be lots more efficient!
1063 static /*@exposed@*/ uentry
1064 usymtab_supEntryReturnAux (/*@notnull@*/ usymtab tab,
1065 /*@only@*/ uentry e, bool isref)
1066 /*@globals globtab, filetab@*/
1067 /*@modifies tab, globtab, e@*/
1069 cstring rawName = cstring_copy (uentry_rawName (e));
1070 bool stat = (tab == globtab) && uentry_isStatic (e);
1073 (void) usymtab_supEntryAux (tab, e, isref);
1077 ret = usymtab_lookupAux (filetab, rawName);
1081 ret = usymtab_lookupAux (tab, rawName);
1083 if (uentry_isInvalid (ret) && usymtab_isDefined (filetab))
1085 ret = usymtab_lookupAux (filetab, rawName);
1089 cstring_free (rawName);
1093 /*@dependent@*/ /*@exposed@*/ uentry
1094 usymtab_supEntryReturn (/*@only@*/ uentry e)
1095 /*@globals utab, filetab, globtab@*/
1096 /*@modifies utab, globtab, e@*/
1098 return (usymtab_supEntryReturnAux (utab, e, FALSE));
1101 /*@dependent@*/ /*@exposed@*/ uentry
1102 usymtab_supEntrySrefReturn (/*@only@*/ uentry e)
1103 /*@globals utab, globtab, filetab@*/
1104 /*@modifies utab, globtab, e@*/
1106 return (usymtab_supEntryReturnAux (utab, e, TRUE));
1109 /*@dependent@*/ /*@exposed@*/ uentry
1110 usymtab_supGlobalEntryReturn (uentry e)
1111 /*@globals globtab, filetab@*/
1112 /*@modifies globtab, e@*/
1116 ret = usymtab_supEntryReturnAux (globtab, e, FALSE);
1119 ** We need to keep track of internal function declarations, so
1120 ** we can remove them from the symbol table after exiting this
1121 ** function. This is a bit bogus, of course.
1124 if (sRef_modInFunction ())
1126 recordFunctionType (ret);
1133 usymtab_supTypeEntry (/*@only@*/ uentry e)
1134 /*@globals globtab, filetab@*/
1135 /*@modifies globtab, e@*/
1140 if (uentry_isAbstractDatatype (e))
1142 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1143 ret = ctype_createAbstract (uid);
1147 uid = usymtab_supEntryAux (globtab, e, FALSE);
1148 ret = ctype_createUser (uid);
1151 if (sRef_modInFunction ())
1153 recordFunctionType (globtab->entries[uid]);
1160 usymtab_supReturnTypeEntry (/*@only@*/ uentry e)
1161 /*@globals globtab, filetab@*/
1162 /*@modifies globtab@*/
1166 DPRINTF (("Abstract? %s", uentry_unparseFull (e)));
1168 if (uentry_isAbstractDatatype (e))
1170 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1172 else if (uentry_isMaybeAbstract (e) && context_getFlag (FLG_IMPABSTRACT))
1174 bool maybeabs = TRUE;
1175 cstring sname = uentry_getName (e);
1176 uentry ue = usymtab_lookupGlobSafe (sname);
1177 cstring_free (sname);
1179 if (uentry_isValid (ue))
1181 DPRINTF (("Lookup: %s", uentry_unparseFull (ue)));
1183 if (uentry_isDatatype (ue))
1185 if (uentry_isMaybeAbstract (ue))
1196 DPRINTF (("Not datatype!"));
1203 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1204 ux = usymtab_getTypeEntry (uid);
1205 uentry_setAbstract (ux);
1209 uid = usymtab_supEntryAux (globtab, e, FALSE);
1210 e = usymtab_getTypeEntry (uid);
1212 if (uentry_isMaybeAbstract (e))
1214 uentry_setConcrete (e);
1220 uid = usymtab_supEntryAux (globtab, e, FALSE);
1221 e = usymtab_getTypeEntry (uid);
1223 if (uentry_isMaybeAbstract (e))
1225 uentry_setConcrete (e);
1229 if (sRef_modInFunction ())
1231 recordFunctionType (globtab->entries[uid]);
1234 return (globtab->entries[uid]);
1238 usymtab_supAbstractTypeEntry (/*@only@*/ uentry e, bool dodef)
1239 /*@globals globtab, filetab@*/
1240 /*@modifies globtab, e@*/
1245 uid = usymtab_supEntryAux (globtab, e, FALSE);
1246 ue = usymtab_getTypeEntry (uid);
1250 uentry_setDatatype (ue, uid);
1253 if (context_getFlag (FLG_ACCESSMODULE)) /* was accessfile */
1255 context_addFileAccessType (uid);
1258 if (sRef_modInFunction ())
1260 recordFunctionType (globtab->entries[uid]);
1268 usymtab_supExposedTypeEntry (/*@only@*/ uentry e, bool dodef)
1269 /*@globals globtab, filetab@*/
1270 /*@modifies globtab, e@*/
1274 uid = usymtab_supEntryAux (globtab, e, FALSE);
1278 uentry ue = usymtab_getTypeEntry (uid);
1280 uentry_setDatatype (ue, uid);
1283 if (sRef_modInFunction ())
1285 recordFunctionType (globtab->entries[uid]);
1293 usymtab_supForwardTypeEntry (/*@only@*/ uentry e)
1294 /*@globals globtab, filetab@*/
1295 /*@modifies globtab, e@*/
1297 usymId uid = usymtab_supEntryAux (globtab, e, FALSE);
1298 uentry ue = usymtab_getTypeEntry (uid);
1300 uentry_setDatatype (ue, uid);
1302 if (sRef_modInFunction ())
1304 recordFunctionType (globtab->entries[uid]);
1307 return (uentry_getAbstractType (ue));
1311 usymtab_supEntrySref (uentry e)
1312 /*@globals utab, globtab, filetab@*/
1313 /*@modifies utab, globtab, e@*/
1315 sRef old = uentry_getSref (e);
1318 if (sRef_isType (old))
1320 uentry ue = usymtab_supEntryReturnAux (utab, e, TRUE);
1323 if (uentry_isValid (ue))
1325 sRef uref = uentry_getSref (ue);
1327 sRef_mergeStateQuiet (uref, old);
1328 sRef_clearDerived (uref);
1330 /*@noaccess uentry@*/
1332 else if (sRef_isKnown (old))
1334 usymtab_supEntry (e);
1338 (void) usymtab_supEntryAux (utab, e, TRUE);
1342 void usymtab_supGlobalEntry (/*@only@*/ uentry e)
1343 /*@globals globtab, filetab@*/
1344 /*@modifies globtab, filetab, e@*/
1348 DPRINTF (("Sup global entry: %s", uentry_unparse (e)));
1350 uid = usymtab_supEntryAux (globtab, e, FALSE);
1352 if (sRef_modInFunction ())
1354 recordFunctionType (globtab->entries[uid]);
1359 usymtab_supReturnFileEntry (/*@only@*/ uentry e)
1360 /*@globals filetab, globtab@*/
1361 /*@modifies filetab, globtab, e@*/
1363 llassert (filetab != usymtab_undefined);
1364 DPRINTF (("File entry: %s", uentry_unparse (e)));
1365 return (usymtab_supEntryReturnAux (filetab, e, FALSE));
1373 usymtab_inDeepScope () /*@globals utab@*/
1375 return (utab->lexlevel > paramsScope);
1379 usymtab_getIndex (/*@notnull@*/ usymtab s, cstring k)
1383 DPRINTF (("Lookup %s", k));
1385 if (cstringTable_isDefined (s->htable))
1387 i = cstringTable_lookup (s->htable, k);
1392 for (i = 0; i < s->nentries; i++)
1394 uentry current = s->entries[i];
1396 DPRINTF (("Check %d: %s", i, uentry_rawName (current)));
1398 if (!uentry_isUndefined (current)
1399 && cstring_equal (uentry_rawName (current), k))
1410 usymtab_fetchIndex (/*@notnull@*/ usymtab s, int i)
1412 llassert (i >= 0 && i < s->nentries);
1413 return (s->entries[i]);
1417 usymtab_getTypeId (cstring k) /*@globals globtab@*/
1419 usymId uid = usymtab_getIndex (globtab, k);
1421 if (uid == NOT_FOUND) return USYMIDINVALID;
1423 if (!(uentry_isDatatype (usymtab_getTypeEntry (uid)))) {
1424 return USYMIDINVALID;
1430 /*@dependent@*/ uentry
1431 usymtab_lookupStructTag (cstring k)
1433 cstring sname = makeStruct (k);
1434 uentry ue = usymtab_lookupGlob (sname);
1436 cstring_free (sname);
1440 /*@dependent@*/ uentry
1441 usymtab_lookupUnionTag (cstring k)
1443 cstring uname = makeUnion (k);
1444 uentry res = usymtab_lookupGlob (uname);
1446 cstring_free (uname);
1450 /*@dependent@*/ uentry
1451 usymtab_lookupEnumTag (cstring k)
1453 cstring ename = makeEnum (k);
1454 uentry res = usymtab_lookupGlob (ename);
1456 cstring_free (ename);
1461 usymtab_getId (cstring k) /*@globals globtab@*/
1463 usymId uid = usymtab_getIndex (globtab, k);
1466 if (uid == NOT_FOUND)
1468 return USYMIDINVALID;
1471 ue = usymtab_getGlobalEntry (uid);
1473 if (uentry_isPriv (ue))
1475 return USYMIDINVALID;
1481 static /*@exposed@*/ uentry
1482 usymtab_getEntryAux (/*@notnull@*/ usymtab s, usymId uid)
1484 llassert (uid != USYMIDINVALID);
1486 if (uid < 0 || uid >= s->nentries)
1488 llcontbug (message ("usymtab_getEntry: out of range: level = %d [%d]",
1490 return uentry_undefined;
1493 llassertprint (uentry_isValid (s->entries[uid]),
1494 ("entry undefined: %d", uid));
1496 return s->entries[uid];
1499 /*@dependent@*/ /*@observer@*/ uentry
1500 usymtab_getGlobalEntry (usymId uid)
1501 /*@globals utab, globtab@*/
1503 if (dbgfree) return (uentry_undefined);
1505 if (utab->lexlevel > paramsScope)
1507 /* need to do this the awkward way, since it could be in conditional scope */
1508 return (usymtab_lookupSafe (uentry_rawName (globtab->entries[uid])));
1512 return (globtab->entries[uid]);
1516 /*@dependent@*/ /*@exposed@*/ uentry
1517 usymtab_getTypeEntry (usymId uid)
1518 /*@globals globtab@*/
1522 if (uid >= 0 && uid < globtab->nentries)
1524 return (globtab->entries[uid]);
1528 return (uentry_undefined);
1533 llassert (uid >= 0 && uid < globtab->nentries);
1534 return (globtab->entries[uid]);
1542 /*@dependent@*/ /*@exposed@*/ uentry
1543 usymtab_getTypeEntrySafe (usymId uid)
1544 /*@globals globtab@*/
1546 if (uid < 0 || uid >= globtab->nentries)
1548 return uentry_undefined;
1551 return (globtab->entries[uid]);
1555 usymtab_isBoolType (usymId uid)
1556 /*@globals globtab@*/
1558 llassert (uid >= 0 && uid < globtab->nentries);
1560 return (cstring_equal (uentry_rawName (globtab->entries[uid]),
1561 context_getBoolName ()));
1565 usymtab_getTypeEntryName (usymId uid)
1566 /*@globals globtab@*/
1572 return (cstring_makeLiteral ("<freetype>"));
1575 ue = usymtab_getTypeEntry (uid);
1577 if (dbgload && !uentry_isValid (ue))
1579 return (message ("<missing type: %d>", uid));
1582 llassertprint (uentry_isValid (ue), ("type undefined: %d", uid));
1584 return (uentry_getName (ue));
1588 /*@unused@*/ static void
1589 usymtab_rehash (/*@notnull@*/ usymtab s)
1593 if (cstringTable_isDefined (s->htable))
1595 cstringTable_free (s->htable);
1598 s->htable = cstringTable_create (LLHASHSIZE);
1600 for (i = 0; i < s->nentries; i++)
1602 cstringTable_insert (s->htable, cstring_copy (uentry_rawName (s->entries[i])), i);
1608 ** superficial copy of usymtab
1610 ** DO copy spec entries
1613 static /*@only@*/ /*@notnull@*/ usymtab
1614 usymtab_shallowCopy (/*@notnull@*/ usymtab s) /*@*/
1616 usymtab copytab = usymtab_createRoot ();
1619 for (i = 0; i < s->nentries; i++)
1621 usymtab_addEntryBase (copytab, s->entries[i]);
1628 usymtab_shallowFree (/*@only@*/ /*@notnull@*/ usymtab s)
1630 aliasTable_free (s->aliases);
1631 refTable_free (s->reftable, s->nentries);
1633 /*@-compdestroy@*/ sfree (s); /*@=compdestroy@*/
1637 ** converts usymId from old table to sorted one
1641 usymtab_convertId (usymId uid)
1642 /*@globals oldtab, utab@*/
1648 llassert (usymtab_isDefined (oldtab));
1650 ue = usymtab_getEntryAux (oldtab, uid);
1652 llassertprint (uentry_isValid (ue), ("convertId: undefined: %d", uid));
1654 name = uentry_rawName (ue);
1656 ret = usymtab_getIndex (utab, name);
1657 llassert (ret == uid); /*! for now, no rehash! */
1658 DPRINTF (("Convert: %s [%d] -> %s [%d]",
1659 uentry_unparse (ue), uid,
1660 uentry_unparse (utab->entries[ret]), ret));
1662 llassertprint (ret != USYMIDINVALID, ("convertId: return is invalid"));
1668 usymtab_prepareDump (void)
1669 /*@globals oldtab, utab@*/
1670 /*@modifies oldtab, utab@*/
1672 llassert (usymtab_inGlobalScope ());
1673 llassert (oldtab == usymtab_undefined);
1676 DPRINTF (("Preparing to dump:"));
1677 usymtab_printAll ();
1680 oldtab = usymtab_shallowCopy (utab);
1683 ** alpha compare - make sure order is same on different platforms
1684 ** error messages appear in same order
1688 qsort (utab->entries, (size_t)utab->nentries,
1689 sizeof (*utab->entries),
1690 (int (*)(const void *, const void *)) uentry_xcomparealpha);
1692 usymtab_rehash (utab);
1696 DPRINTF (("After rehash:"));
1697 usymtab_printAll ();
1701 void usymtab_dump (FILE *fout)
1702 /*@globals utab, oldtab@*/
1705 bool neednl = FALSE;
1706 uentry lastentry = uentry_undefined;
1707 ekind lastekind = KINVALID;
1711 ** must call prepareDump first
1714 llassert (oldtab != usymtab_undefined);
1716 for (i = 0; i < utab->nentries; i++)
1718 uentry thisentry = utab->entries[i];
1719 ekind thisekind = uentry_getKind (thisentry);
1721 if (!uentry_hasRealName (thisentry))
1723 llassert (uentry_isGlobalMarker (thisentry));
1727 check (fputc ('\n', fout) == (int) '\n');
1730 fprintf (fout, "*%d (GlobalMarker)\n", KGLOBALMARKER);
1731 lastekind = KINVALID;
1737 if (thisekind != lastekind)
1741 check (fputc ('\n', fout) == (int) '\n');
1745 lastentry = uentry_undefined;
1746 fprintf (fout, "*%d (%s)\n", ekind_toInt (thisekind),
1747 cstring_toCharsSafe (ekind_capName (thisekind)));
1748 lastekind = thisekind;
1753 ** evans - 2001-02-18 - added the - 48 fudge factor...
1754 ** extra characters dumped, but I haven't counded them carefully...
1757 if (uentry_isInvalid (lastentry) || !uentry_equiv (lastentry, thisentry)
1758 || (linelen > (MAX_DUMP_LINE_LENGTH - (2 * MAX_NAME_LENGTH) - 48)))
1762 DPRINTF (("Dumping entry: %d", i));
1763 cdump = message ("^%d %q", i, uentry_dump (thisentry));
1764 /* was: cdump = uentry_dump (thisentry)); */
1766 lastentry = thisentry;
1769 check (fputc ('\n', fout) == (int) '\n');
1773 linelen += cstring_length (cdump);
1775 /* no new line here! */
1776 if (cstring_length (cdump) > 0)
1778 check (fputs (cstring_toCharsSafe (cdump), fout) != EOF);
1781 cstring_free (cdump);
1786 cstring cdump = uentry_rawName (thisentry);
1787 DPRINTF (("Raw name: %s", cdump));
1788 linelen += (cstring_length (cdump) + 1);
1789 fprintf (fout, "#%s", cstring_toCharsSafe (cdump));
1796 check (fputc ('\n', fout) == (int) '\n');
1799 lastekind = KINVALID;
1801 fprintf(fout, ";; Library constraints\n");
1802 for (i = 0; i < utab->nentries; i++)
1804 uentry thisentry = utab->entries[i];
1806 if (uentry_isFunction (thisentry) )
1808 constraintList preconditions;
1809 constraintList postconditions;
1811 preconditions = uentry_getFcnPreconditions (thisentry);
1812 postconditions = uentry_getFcnPostconditions (thisentry);
1814 if ( constraintList_isDefined(preconditions) ||
1815 constraintList_isDefined(postconditions) )
1817 fprintf(fout,"%s\n", cstring_toCharsSafe (uentry_rawName(thisentry) ) );
1818 if (constraintList_isDefined(preconditions) )
1820 fprintf(fout,"pre:\n");
1821 constraintList_dump(preconditions, fout);
1822 fprintf (fout, ";; end precondition constraints\n" );
1823 constraintList_free(preconditions);
1827 fprintf(fout,"pre:EMPTY\n");
1829 if (constraintList_isDefined(postconditions) )
1831 fprintf(fout,"post:\n");
1832 constraintList_dump(postconditions, fout);
1833 fprintf (fout, ";; end precondition constraints\n" );
1834 constraintList_free(postconditions);
1838 fprintf(fout,"post:EMPTY\n");
1847 void usymtab_load (FILE *f)
1848 /*@globals utab, globtab@*/
1849 /*@modifies utab, *f@*/
1851 char *s = mstring_create (MAX_DUMP_LINE_LENGTH);
1853 ekind kind = KINVALID;
1854 fileloc loc = g_currentloc;
1860 llassert (utab == globtab);
1861 llassert (utab->nentries == 0);
1863 while (((s = reader_readLine (f, s, MAX_DUMP_LINE_LENGTH)) != NULL)
1866 /* ignore ; comments */ ;
1869 while (s != NULL && *s != ';')
1877 ek = reader_getInt (&s);
1879 if (ek == KGLOBALMARKER)
1881 uentry lue = uentry_makeGlobalMarker ();
1882 DPRINTF (("Adding global marker: %s", uentry_unparseFull (lue)));
1883 usymtab_addEntryAlways (utab, lue);
1889 kind = ekind_fromInt (ek);
1897 (cstring_makeLiteral
1898 ("Library is in obsolete format. Use splint +whichlib "
1899 "to see which library is being loaded."));
1902 if (reader_optCheckChar (&s, '^'))
1904 index = reader_getInt (&s);
1911 llassert (kind != KINVALID);
1912 ue = uentry_undump (kind, loc, &s);
1914 llassert (utab->nentries == index || index == -1);
1916 if (uentry_isValid (ue))
1918 int lastindex = utab->nentries;
1919 ue = usymtab_addEntryAlways (utab, ue);
1923 if (uentry_isConstant (ue)) /*@i23! isPreProcessorMacro */
1925 cstring uname = uentry_getName (ue);
1927 /* Also check its a macro... */
1928 DPRINTF (("Installing: %s", uname));
1930 cpphash_installMacro
1931 (mstring_copy (cstring_toCharsSafe (uname)),
1932 cstring_length (uname),
1933 cpplib_createDefinition (message ("%s 255", uname),
1936 cpphash_hashCode (cstring_toCharsSafe (uname),
1937 cstring_length (uname),
1940 DPRINTF (("After install: %s", uname));
1944 if (utab->nentries != lastindex + 1)
1946 DPRINTF (("No add: %s", uentry_unparseFull (ue)));
1954 ** now, any other names are the same uentry
1957 while (*(s++) == '#')
1959 cstring name = cstring_fromCharsO (reader_getWord (&s));
1960 uentry nue = uentry_nameCopy (name, ue);
1962 DPRINTF (("Name copy: %s", uentry_unparseFull (nue)));
1966 usymtab_addEntryAlways (utab, nue);
1969 while ((c = *s) != '\0' && (c !='\n'))
1971 if (c != ' ' || c != '\t')
1973 llbuglit ("Junk in load file");
1981 s = reader_readLine (f, os, MAX_DUMP_LINE_LENGTH);
1986 to handle reading of buffer overflow related constraints
1988 while (fgets (s, MAX_DUMP_LINE_LENGTH, f) != NULL
1991 ; /* ignore ;-comments */
1994 while (s != NULL && *s != ';')
1996 constraintList preconditions;
1997 constraintList postconditions;
1999 cstring name = cstring_fromChars(reader_getWord(&s) );
2001 ue = usymtab_lookup ( name );
2005 preconditions = constraintList_undefined;
2006 postconditions = constraintList_undefined;
2008 if (!uentry_isValid(ue) )
2010 llfatalbug ((message("Invalid uentry for %s library file may be corrupted", cstring_fromChars(s) ) ));
2012 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
2014 temp = cstring_fromChars (reader_getWord(&s) );
2016 if (cstring_compareLit (temp,"pre:") == 0 )
2018 preconditions = constraintList_undump (f);
2022 if (cstring_compareLit (temp,"pre:EMPTY") != 0 )
2023 llfatalbug ((message("Error reading library file pre:EMPTY expected but got %s", temp ) ));
2028 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
2030 temp = cstring_fromChars(reader_getWord(&s) );
2031 if (cstring_compareLit (temp, "post:") == 0 )
2033 postconditions = constraintList_undump (f);
2037 if (cstring_compareLit (temp, "post:EMPTY") != 0 )
2038 llfatalbug ((message("Error reading library file post:EMPTY expected but got %s", temp ) ));
2041 cstring_free (temp);
2043 uentry_setPreconditions (ue, functionConstraint_createBufferConstraint (preconditions));
2044 uentry_setPostconditions (ue, functionConstraint_createBufferConstraint (postconditions));
2046 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
2054 ** file scope for static variables
2058 usymtab_enterFile ()
2059 /*@globals utab, globtab, filetab@*/
2060 /*@modifies filetab@*/
2062 llassert (utab == globtab);
2067 usymtab_entries (globtab, ue)
2069 if (sRef_hasDerived (uentry_getSref (ue)))
2071 fprintf (g_warningstream, "Derived Global: %s\n", uentry_unparse (ue));
2072 fprintf (g_warningstream, "sRef: %s\n", sRef_unparseFull (ue->sref));
2074 } end_usymtab_entries ;
2078 usymtab_enterScope ();
2084 /*@globals utab, filetab@*/
2085 /*@modifies filetab, utab@*/
2088 llassert (utab->lexlevel == 1);
2090 usymtab_exitScope (exprNode_undefined);
2095 usymtab_enterScope ()
2096 /*@globals utab, globtab, filetab@*/
2099 usymtab t = usymtab_create (US_NORMAL, utab, TRUE);
2101 /* unconditional scope: optimize to avoid copy */
2102 t->aliases = aliasTable_copy (utab->aliases);
2105 llassert (usymtab_isDefined (t->env));
2107 if (t->env->lexlevel == paramsScope && context_inFunctionLike ())
2109 noshadowerror = TRUE;
2110 usymtab_handleParams ();
2111 noshadowerror = FALSE;
2116 ** setup external references:
2117 ** o only, unique params alias external args
2118 ** o other params may alias anything of their type
2122 usymtab_handleParams (void)
2123 /*@globals utab, globtab, filetab@*/
2124 /*@modifies utab, globtab@*/
2126 usymtab ptab = utab->env;
2127 uentry fcn = context_getHeader ();
2129 usymtab_entries (ptab, param)
2133 if (!uentry_isYield (param))
2136 sRef pref = uentry_getSref (param);
2138 /* Could be a global. */
2140 if (uentry_isAnyParam (param))
2142 ue = uentry_makeVariable (fixParamName (uentry_rawName (param)),
2143 uentry_getType (param),
2144 fileloc_copy (uentry_whereDeclared (param)),
2147 uentry_copyState (ue, param);
2148 uentry_setRefParam (ue);
2150 ue = usymtab_supEntrySrefReturn (ue);
2152 /* must be after supercede! */
2154 if (!sRef_stateKnown (pref))
2156 uentry_setDefState (ue, SS_DEFINED);
2157 uentry_setDefState (param, SS_DEFINED);
2161 if (sRef_isStateSpecial (pref))
2163 uentry_setDefState (ue, SS_SPECIAL); /* ALLOCATED); */
2164 /* evans 2002-01-01: should be unnecessary, the pre clauses
2165 ** set the state if necessary.
2170 uentry_setDefState (ue, sRef_getDefState (pref));
2174 uref = uentry_getSref (ue);
2176 if (sRef_isStack (uref))
2178 alkind pkind = sRef_getAliasKind (pref);
2180 if (alkind_isKnown (pkind) && !alkind_isLocal (pkind)
2181 && !alkind_isStack (pkind))
2183 sRef_setAliasKind (uref, pkind, fileloc_undefined);
2184 sRef_setOrigAliasKind (uref, pkind);
2188 sRef_setAliasKind (uref, AK_IMPTEMP, fileloc_undefined);
2189 sRef_setOrigAliasKind (uref, AK_IMPTEMP);
2191 if (uentry_isOut (param))
2197 sRef_setDefined (uref, fileloc_undefined);
2203 usymtab_addMustAlias (uref, pref);
2205 if (!(uentry_isOnly (param) || uentry_isUnique (param)))
2208 ** This is needed for detecting possibly aliased parameters.
2211 sRef s = sRef_makeExternal (uref);
2212 usymtab_addMustAlias (uref, s);
2215 if (sRef_isKillRef (pref))
2217 sRef_setAliasKind (uref, AK_NEWREF, fileloc_undefined);
2218 sRef_setOrigAliasKind (uref, AK_KILLREF);
2220 else if (sRef_isRefCounted (uref))
2222 sRef_setOrigAliasKind (uref, AK_REFCOUNTED);
2227 sRef_setOrigAliasKind (uref, AK_LOCAL);
2234 } end_usymtab_entries;
2237 if (uentry_hasStateClauseList (fcn))
2239 stateClauseList clauses = uentry_getStateClauseList (fcn);
2241 stateClauseList_preElements (clauses, cl)
2243 fileloc loc = stateClause_loc (cl);
2244 sRefSet osrs = sRefSet_undefined;
2247 if (stateClause_isGlobal (cl))
2249 DPRINTF (("Global Marker: %s",
2250 sRef_unparseFull (usymtab_lookupGlobalMarker ())));
2251 llassert (sRef_isGlobalMarker (usymtab_lookupGlobalMarker ()));
2252 srs = sRefSet_single (usymtab_lookupGlobalMarker ());
2257 srs = stateClause_getRefs (cl);
2260 sRefSet_elements (srs, el)
2262 sRef base = sRef_getRootBase (el);
2263 sRef sb = sRef_updateSref (el);
2265 if (sRef_isResult (base))
2267 ; /* nothing to do before */
2269 else if (sRef_isParam (base) || sRef_isGlobalMarker (base))
2271 if (stateClause_setsMetaState (cl))
2273 /* copied from exprNode.c:3040 */
2274 qual ql = stateClause_getMetaQual (cl);
2275 annotationInfo ainfo = qual_getAnnotationInfo (ql);
2276 metaStateInfo minfo = annotationInfo_getState (ainfo);
2277 cstring key = metaStateInfo_getName (minfo);
2278 int mvalue = annotationInfo_getValue (ainfo);
2280 DPRINTF (("Sets meta state! %s", stateClause_unparse (cl)));
2282 if (sRef_isResult (base))
2288 sRef_setMetaStateValueComplete (sb, key, mvalue, loc);
2293 sRefMod modf = stateClause_getEntryFunction (cl);
2297 sRefSet aliases = usymtab_allAliases (sb);
2301 sRefSet_elements (aliases, sr)
2304 } end_sRefSet_elements ;
2306 sRefSet_free (aliases);
2312 if (sRef_isValid (base))
2314 DPRINTF (("Base: %s", sRef_unparseFull (base)));
2318 } end_sRefSet_elements ;
2319 } end_stateClauseList_preElements ;
2324 usymtab_enterFunctionScope (uentry fcn)
2325 /*@globals utab, filetab, globtab@*/
2328 usymtab t = usymtab_create (US_NORMAL, utab, TRUE);
2330 if (utab->lexlevel != fileScope)
2332 if (utab->lexlevel > fileScope)
2334 llparseerror (cstring_makeLiteral ("New function scope inside function"));
2336 while (utab->lexlevel > fileScope)
2338 /*@i@*/ utab = usymtab_dropEnv (utab);
2345 llfatalbug (cstring_makeLiteral ("New function not inside file."));
2347 /*@-branchstate@*/ } /*@=branchstate@*/
2351 DPRINTF (("Globs: %s", globSet_unparse (uentry_getGlobs (fcn))));
2353 globSet_allElements (uentry_getGlobs (fcn), el)
2355 DPRINTF (("Here we go: %s", sRef_unparseFull (el)));
2357 if (sRef_isUndefGlob (el))
2359 int index = sRef_getScopeIndex (el);
2360 sRef sr = sRef_updateSref (el);
2361 fileloc loc = uentry_whereEarliest (fcn);
2363 DPRINTF (("update: %s", sRef_unparseFull (sr)));
2364 DPRINTF (("Undef!"));
2365 if (sRef_isFileStatic (el))
2367 ctype ct = sRef_getType (el);
2370 llassert (usymtab_isDefined (filetab));
2372 ue = usymtab_fetchIndex (filetab, index);
2374 if (ctype_isRealArray (ct) || ctype_isRealSU (ct))
2376 sRef_setAllocated (sr, loc);
2380 sRef_setUndefined (sr, loc);
2385 uentry ue = globtab->entries[index];
2386 ctype ct = uentry_getType (ue);
2388 if (ctype_isArray (ct) || ctype_isSU (ct))
2390 sRef_setAllocated (sr, loc);
2394 sRef_setUndefined (sr, loc);
2398 else if (sRef_isAllocated (el))
2400 sRef sr = sRef_updateSref (el);
2401 fileloc loc = uentry_whereEarliest (fcn);
2403 sRef_setAllocated (sr, loc);
2405 else if (sRef_isPartial (el))
2407 sRef sr = sRef_updateSref (el);
2408 fileloc loc = uentry_whereEarliest (fcn);
2410 sRef_setPartial (sr, loc);
2415 sRef sr = sRef_updateSref (el);
2416 fileloc loc = uentry_whereEarliest (fcn);
2418 sRef_setDefined (sr, loc);
2422 /* shouldn't need to do anything! */
2424 } end_globSet_allElements;
2426 DPRINTF (("Globs after: %s", globSet_unparse (uentry_getGlobs (fcn))));
2430 usymtab_caseBranch (void)
2433 usymtab t = usymtab_create (US_CBRANCH, utab, FALSE);
2438 usymtab_switchBranch (/*@unused@*/ exprNode s)
2441 usymtab t = usymtab_create (US_SWITCH, utab, FALSE);
2443 t->aliases = aliasTable_copy (utab->aliases);
2448 usymtab_trueBranch (/*@only@*/ guardSet guards)
2451 usymtab t = usymtab_create (US_TBRANCH, utab, FALSE);
2454 ** not true! (could be in a macro)
2456 ** llassertprint (utab->lexlevel > paramsScope,
2457 ** ("not in scope: %s", usymtab_unparseLocal ()));
2461 guardSet_free (t->guards);
2464 aliasTable_free (t->aliases);
2465 t->aliases = aliasTable_copy (utab->aliases);
2473 ** { int a; if (...) a = 3; < a may be undefined here!
2478 usymtab_popTrueBranch (exprNode pred, exprNode expr, clause cl) /*@modifies utab@*/
2481 ** add a false branch
2482 ** (could be done more efficiently as a special case, but
2483 ** it is better to only maintain one version of the code)
2486 if (utab->kind != US_TBRANCH
2487 && context_inIterDef ())
2489 usymtab_exitScope (expr);
2493 DPRINTF (("pop true branch.."));
2494 usymtab_altBranch (guardSet_invert (exprNode_getGuards (pred)));
2495 usymtab_popBranches (pred, expr, exprNode_undefined, TRUE, cl);
2500 usymtab_popCaseBranch () /*@modifies utab@*/
2502 llassert (utab->kind == US_CBRANCH);
2503 usymtab_quietPlainExitScope ();
2507 usymtab_popTrueExecBranch (exprNode pred, exprNode expr, clause cl)
2510 ** add a false branch that must return --- that is,
2511 ** the true branch is always executed!
2514 usymtab_altBranch (guardSet_invert (exprNode_getGuards (pred)));
2515 usymtab_popBranches (pred, expr, exprNode_makeMustExit (), FALSE, cl);
2519 usymtab_popOrBranch (exprNode pred, exprNode expr)
2523 usymtab env = utab->env;
2524 usymtab otab = utab;
2527 llassert (env != NULL);
2529 if (exprNode_isError (expr))
2535 mustReturn = exprNode_mustEscape (expr);
2539 llassert (utab->kind == US_TBRANCH);
2542 ** merge each entry in table with its original
2543 ** unless execution cannot continue after this branch
2546 for (i = 0; i < utab->nentries; i++)
2548 uentry current = utab->entries[i];
2549 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
2551 uentry_mergeState (old, current, exprNode_loc (expr),
2552 mustReturn, FALSE, TRUE, ORCLAUSE);
2558 env->guards = guardSet_levelUnionFree (env->guards,
2559 guardSet_invert (exprNode_getGuards (pred)),
2564 env->aliases = aliasTable_levelUnion (env->aliases, otab->aliases, env->lexlevel);
2567 /* env is now utab */
2568 usymtab_quietPlainExitScope ();
2572 ** case syntax in C is very unrestricted. This is unfortunate.
2574 ** A switch case is ended either by a new case or default, or
2575 ** a close } that may close the switch or some other control
2580 usymtab_newCase (/*@unused@*/ exprNode pred, exprNode last)
2583 bool mustBreak = usymtab_mustBreak (utab);
2584 bool mustReturn = usymtab_mustEscape (utab);
2585 usymtab stab = utab;
2587 DPRINTF (("New case!"));
2590 ** Find last case (or outer switch)
2593 while (stab->kind != US_CBRANCH && stab->kind != US_SWITCH)
2596 llassert (stab != GLOBAL_ENV);
2599 while (stab->kind == US_CBRANCH)
2602 llassert (stab != GLOBAL_ENV);
2606 ** if its a fall through case, merge in outside entries and last case.
2611 ** case 1: x = 3; <fall through>
2612 ** case 2: << x may not be defined
2616 if (!mustBreak && !mustReturn && utab->kind == US_CBRANCH)
2618 llassert (stab->kind == US_SWITCH || stab->kind == US_NORMAL);
2620 usymtab_entries (utab, ue) /* but, keep track of used variables */
2622 uentry old = usymtab_lookupAux (stab, uentry_rawName (ue));
2624 llassert (uentry_isValid (old));
2626 /* modifies ue, not old */
2628 uentry_mergeState (ue, old, exprNode_loc (last),
2629 FALSE, FALSE, TRUE, CASECLAUSE);
2630 } end_usymtab_entries;
2632 utab->aliases = aliasTable_levelUnion (utab->aliases,
2633 stab->aliases, utab->lexlevel);
2636 ** No need for a new branch.
2643 usymtab_caseBranch ();
2644 /*@-mustfree@*/ /*< utab->aliases >*/
2645 utab->aliases = aliasTable_copy (stab->aliases);
2653 ** for && (both pred and expr are executed)
2657 usymtab_popAndBranch (exprNode pred, /*@unused@*/ exprNode expr)
2660 usymtab env = utab->env;
2664 llassert (utab->kind == US_TBRANCH);
2667 ** merge each entry in table with its original
2668 ** unless execution cannot continue after this branch
2671 for (i = 0; i < utab->nentries; i++)
2673 uentry current = utab->entries[i];
2674 sRef tref = uentry_getSref (current);
2675 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
2676 sRef oref = uentry_getSref (old);
2678 /* note that is current is in a nested branch,
2679 it may create a "new" old entry. */
2681 llassert (uentry_isValid (old));
2682 uentry_mergeState (old, current, exprNode_loc (expr),
2683 FALSE, FALSE, TRUE, ANDCLAUSE);
2686 ** if is it defined by the second clause, then it should be defined.
2689 if (sRef_isAnyDefined (tref)
2690 && (sRef_isAllocated (oref) || sRef_isStateUndefined (oref)))
2692 sRef_setDefined (oref, g_currentloc);
2696 utab->guards = guardSet_levelUnionFree (utab->guards,
2697 guardSet_invert (exprNode_getGuards (pred)),
2699 utab->aliases = aliasTable_levelUnion (utab->aliases, otab->aliases, utab->lexlevel);
2701 usymtab_quietPlainExitScope ();
2706 ** Stack should be [ US_CBRANCH+ US_SWITCH ]
2707 ** Only branches which do not return (except possibly the last branch) are included.
2709 ** Conditionally merge state from all CBRANCHes.
2711 ** If allpaths is TRUE, then all possible executions go through some switch
2712 ** case, and the original scope is not merged.
2716 usymtab_exitSwitch (/*@unused@*/ exprNode sw, bool allpaths)
2719 usymtab ttab = utab;
2720 usymtab stab = ttab;
2721 usymtab ltab = ttab;
2722 bool lastMustReturn = usymtab_mustEscape (utab);
2726 while (stab->kind == US_CBRANCH)
2729 llassert (stab != GLOBAL_ENV);
2732 while (stab->kind == US_NORMAL)
2735 llassert (stab != GLOBAL_ENV);
2738 llassert (stab->kind == US_SWITCH);
2740 /* go to the scope outside the switch (US_SWITCH is just a marker! */
2742 llassert (stab != GLOBAL_ENV);
2746 llassert (usymtab_isDefined (ttab));
2748 if (ttab->kind == US_CBRANCH)
2750 /* was quietPlainExitScope --- but, can't free it yet! */
2752 llassert (utab != GLOBAL_ENV);
2754 while (ttab->kind == US_CBRANCH)
2757 ** (from popTrueBranch)
2760 bool mustReturn = usymtab_mustEscape (ttab);
2761 bool mustBreak = usymtab_mustBreak (ttab);
2763 usymtab_entries (ttab, current)
2765 uentry old = /*@-compmempass@*/ usymtab_lookupAux (ltab, uentry_rawName (current));
2769 ** note that is this is in a nested branch,
2770 ** it may create a "new" old entry.
2773 if (uentry_isValid (old))
2777 uentry_mergeUses (current, old);
2778 uentry_setState (old, current);
2782 uentry_mergeState (old, current, exprNode_loc (sw),
2783 mustReturn, FALSE, TRUE, SWITCHCLAUSE);
2790 } end_usymtab_entries;
2793 ** if entry is not in symbol table for this case, merge with pre-switch
2797 if (!mustReturn && !mustBreak)
2799 usymtab_entries (stab, current)
2801 if (usymtab_getIndex (ttab, uentry_rawName (current)) == NOT_FOUND)
2803 uentry old = /*@-compmempass@*/
2804 usymtab_lookupAux (ltab, uentry_rawName (current));
2807 llassert (uentry_isValid (old));
2808 uentry_mergeState (old, current, exprNode_loc (sw),
2809 FALSE, FALSE, TRUE, SWITCHCLAUSE);
2811 } end_usymtab_entries;
2814 ltab->env = ttab->env;
2818 ** Suprious error, becuase of environments.
2821 /*@i1@*/ utab = ltab;
2823 lastMustReturn = FALSE;
2830 ** now, there is one US_CBRANCH. Merge this with the stab.
2834 for (i = 0; i < ltab->nentries; i++)
2836 uentry current = ltab->entries[i];
2837 uentry old = usymtab_lookupAux (stab, uentry_rawName (current));
2839 /* note that is this is in a nested branch,
2840 it may create a "new" old entry. */
2843 if (uentry_isValid (old))
2847 uentry_mergeUses (current, old);
2848 uentry_setState (old, current);
2852 uentry_mergeState (old, current, exprNode_loc (sw),
2853 FALSE, FALSE, TRUE, SWITCHCLAUSE);
2867 ** switch may or may not be followed by a new scope
2870 if (utab->kind == US_SWITCH)
2872 usymtab_quietPlainExitScope ();
2876 usymtab_quietPlainExitScope ();
2877 llassert (utab->kind == US_SWITCH);
2878 usymtab_quietPlainExitScope ();
2884 updateNullState (sRef el, /*@notnull@*/ usymtab ttab,
2885 /*@notnull@*/ usymtab ftab, bool trueGuard)
2887 sRef base = sRef_getRootBase (el);
2888 int level = sRef_lexLevel (base);
2890 if (sRef_isCvar (base))
2892 usymId index = sRef_getScopeIndex (base);
2893 uentry ue = usymtab_getRefTab (ttab, level, index);
2895 if (!uentry_isLset (ue))
2897 sRef sr = uentry_getSref (ue);
2901 sRef_setDerivNullState (sr, el, NS_NOTNULL);
2905 if (!guardSet_isGuarded (ttab->guards, el)
2906 && !sRef_isNotNull (sr))
2908 DPRINTF (("Here! %s / %s",
2909 sRef_unparseFull (sr),
2910 sRef_unparseFull (el)));
2911 sRef_setDerivNullState (sr, el, NS_DEFNULL);
2920 ue = usymtab_getRefTab (ftab, level, index);
2922 if (!uentry_isLset (ue))
2924 sRef sr = uentry_getSref (ue);
2926 if (!trueGuard) /* yikes! forgot the ! */
2928 sRef_setDerivNullState (sr, el, NS_NOTNULL);
2933 if (!guardSet_isGuarded (ftab->guards, el)
2934 && !sRef_isNotNull (sr))
2936 sRef_setDerivNullState (sr, el, NS_DEFNULL);
2948 usymtab_popBranches (exprNode pred, exprNode tbranch, exprNode fbranch,
2949 bool isOpt, clause cl)
2953 usymtab ftab = utab;
2954 usymtab ttab = utab->env;
2958 guardSet guards = exprNode_getGuards (pred);
2959 sRefSet tguards = guardSet_getTrueGuards (guards);
2960 sRefSet fguards = guardSet_getFalseGuards (guards);
2961 bool mustReturnT = exprNode_mustEscape (tbranch);
2962 bool mustReturnF = exprNode_mustEscape (fbranch);
2964 DPRINTF (("Pop branches: %s [mustreturn: %s/%s]", exprNode_unparse (pred),
2965 bool_unparse (mustReturnT),
2966 bool_unparse (mustReturnF)));
2968 if (exprNode_isDefined (fbranch))
2970 loc = exprNode_loc (fbranch);
2974 loc = exprNode_loc (tbranch);
2977 llassert (usymtab_isDefined (ttab));
2981 llassert (usymtab_isDefined (env));
2982 llassert (ftab->kind == US_FBRANCH);
2983 llassert (ttab->kind == US_TBRANCH);
2986 ** For each element that is true guarded (i.e., if (x != NULL))
2987 ** make x = null in false branch,
2988 ** and x = notnull in true branch.
2989 ** unless x was set locally in that branch.
2990 ** For each element that is false guarded (x == NULL)
2991 ** make x = null in true, notnull in false.
2993 ** For each element that is either guarded (pred(x))
2997 sRefSet_allElements (tguards, el)
2999 updateNullState (el, ttab, ftab, TRUE);
3000 } end_sRefSet_allElements;
3002 sRefSet_allElements (fguards, el)
3004 updateNullState (el, ttab, ftab, FALSE);
3005 } end_sRefSet_allElements;
3009 ** if an entry is in both true and false, merge the entries,
3010 ** then replace original with new state.
3012 ** if an entry is in one table, merge it with the original.
3015 DPRINTF (("ftab: %d", ftab->nentries));
3017 for (i = 0; i < ftab->nentries; i++)
3019 uentry fthis = ftab->entries[i];
3020 uentry old = usymtab_lookupAux (env, uentry_rawName (fthis));
3021 int tindex = usymtab_getIndex (ttab, uentry_rawName (fthis));
3023 DPRINTF (("Entry: %s / %s", uentry_unparseFull (fthis), uentry_unparseFull (old)));
3025 if (uentry_isUndefined (old))
3027 /* possible entry was added as an undefined id */
3028 DPRINTF (("Undefined! %s", uentry_rawName (fthis)));
3032 if (tindex != NOT_FOUND)
3034 uentry tthis = ttab->entries[tindex];
3036 /* note that is this is in a nested branch,
3037 it may create a "new" old entry. */
3043 uentry_mergeState (fthis, tthis, loc,
3044 mustReturnT, FALSE, FALSE, cl);
3048 uentry_mergeUses (fthis, tthis);
3051 uentry_setState (old, fthis);
3058 uentry_setState (old, tthis);
3059 uentry_mergeState (old, fthis, loc, mustReturnF,
3063 ttab->entries[tindex] = uentry_undefined;
3064 uentry_free (tthis);
3068 uentry_mergeState (old, fthis, loc, mustReturnF, TRUE, FALSE, cl);
3072 for (i = 0; i < ttab->nentries; i++)
3074 uentry current = ttab->entries[i];
3076 DPRINTF (("ttab: %s", uentry_unparseFull (current)));
3078 if (!uentry_isUndefined (current))
3080 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
3082 DPRINTF (("Old: %s", uentry_unparseFull (old)));
3084 if (uentry_isUndefined (old))
3086 llcontbug (message ("Undefined entry: %s", uentry_rawName (current)));
3092 uentry_mergeUses (current, old);
3093 uentry_setState (old, current);
3098 ** Assumes false branch is a fall-through if
3099 ** fbranch is not defined. This is true, unless
3100 ** where was some greivous error in processing
3101 ** the else branch of an if-then, in which case
3102 ** this is probably the right thing to do anyway.
3105 uentry_mergeState (old, current, loc, mustReturnT,
3109 DPRINTF (("==> %s", uentry_unparseFull (old)));
3114 ** Plain levelUnion doesn't work, since we need to use the sRef's in env->aliases
3115 ** if they are present.
3118 llassert (NOALIAS (env->aliases, ttab->aliases));
3119 llassert (NOALIAS (env->aliases, ftab->aliases));
3121 aliasTable_free (env->aliases);
3123 env->aliases = aliasTable_levelUnionNew (ttab->aliases,
3124 ftab->aliases, env->lexlevel);
3126 aliasTable_fixSrefs (env->aliases);
3128 DPRINTF (("Aliases: %s", aliasTable_unparse (env->aliases)));
3130 /* exit true and false scopes */
3131 usymtab_quietPlainExitScope ();
3132 usymtab_quietPlainExitScope ();
3136 utab->guards = guardSet_levelUnionFree
3138 guardSet_invert (exprNode_getGuards (pred)),
3144 utab->guards = guardSet_levelUnion (utab->guards,
3145 exprNode_getGuards (pred),
3149 DPRINTF (("Here."));
3152 static void usymtab_fixCases (void) /*@modifies utab@*/ {
3153 while (utab->kind == US_CBRANCH)
3155 usymtab_quietPlainExitScope ();
3158 llassert (utab->kind != US_CBRANCH);
3162 usymtab_altBranch (/*@only@*/ guardSet guards)
3166 usymtab parent = utab->env;
3168 t = usymtab_create (US_FBRANCH, utab, FALSE);
3171 ** If we are in a case, need to close it. The C syntax
3172 ** is very liberal, so this kludge is necessary.
3175 usymtab_fixCases ();
3177 DPRINTF (("Current kind: %s", usymtab_unparseStack ()));
3179 llassert (utab->kind == US_TBRANCH);
3180 llassert (parent != GLOBAL_ENV);
3182 guardSet_free (t->guards);
3185 aliasTable_free (t->aliases);
3186 t->aliases = aliasTable_copy (parent->aliases);
3192 usymtab_allDefined (void)
3193 /*@globals utab, globtab@*/
3197 llassert (utab == globtab);
3199 for (i = 0; i < utab->nentries; i++)
3201 uentry e = utab->entries[i];
3203 if (uentry_isPriv (e))
3205 ; /* no need to define it */
3209 if (context_getFlag (FLG_SPECUNDECL))
3211 fileloc sloc = uentry_whereSpecified (e);
3212 fileloc dloc = uentry_whereDeclared (e);
3214 if (fileloc_isDefined (sloc)
3215 && !uentry_isFakeTag (e)
3216 && !fileloc_isDefined (dloc))
3220 message ("%s %q specified but not declared",
3221 ekind_capName (uentry_getKind (e)),
3222 uentry_getName (e)),
3227 if (!uentry_isCodeDefined (e))
3229 fileloc dloc = uentry_whereDeclared (e);
3231 if (fileloc_isLib (dloc) || fileloc_isXHFile (dloc))
3235 else if (fileloc_isDefined (dloc))
3237 if (!uentry_isAnyTag (e))
3239 if (fileloc_isUser (dloc))
3243 message ("%s %q declared but not defined",
3244 ekind_capName (uentry_getKind (e)),
3245 uentry_getName (e)),
3247 DPRINTF (("decl: %s", uentry_unparseFull (e)));
3253 fileloc sloc = uentry_whereSpecified (e);
3255 if (fileloc_isDefined (sloc)
3256 && !fileloc_isImport (sloc)
3257 && !fileloc_isLib (sloc)
3258 && !fileloc_isPreproc (sloc)
3259 && !uentry_isFakeTag (e))
3261 if (uentry_isVariable (e) || uentry_isFunction (e))
3265 message ("%s %q specified but not declared or defined",
3266 ekind_capName (uentry_getKind (e)),
3267 uentry_getName (e)),
3274 message ("%s %q specified but not defined",
3275 ekind_capName (uentry_getKind (e)),
3276 uentry_getName (e)),
3286 void usymtab_exportHeader (void)
3291 for (i = 0; i < utab->nentries; i++)
3293 uentry ce = utab->entries[i];
3295 if (!uentry_isDatatype (ce)
3296 && !uentry_isAnyTag (ce)
3297 && !uentry_isEitherConstant (ce) && !uentry_isStatic (ce)
3298 && !uentry_isExternal (ce)
3299 && !uentry_isForward (ce))
3301 fileloc fwhere = uentry_whereDeclared (ce);
3303 if (fileloc_isUndefined (fwhere)
3304 && uentry_isFunction (ce))
3306 fwhere = uentry_whereDefined (ce);
3309 if (fileloc_isDefined (fwhere)
3310 && !fileloc_isHeader (fwhere)
3311 && !fileloc_isXHFile (fwhere)
3312 && !(fileloc_isSpecialFile (fwhere)
3313 && !context_getFlag (FLG_UNUSEDSPECIAL)))
3315 if (uentry_isVariable (ce))
3318 (FLG_EXPORTHEADERVAR,
3319 message ("%s %q exported but not declared in header file",
3320 ekind_capName (uentry_getKind (ce)),
3321 uentry_getName (ce)),
3324 uentry_showDefSpecInfo (ce, fwhere);
3329 if (!uentry_isIter (ce)
3330 && !uentry_isEndIter (ce)
3331 && !uentry_isExpandedMacro (ce))
3333 if (uentry_isFunction (ce)
3334 && cstring_equalLit (uentry_rawName (ce), "main"))
3336 ; /* no error for main */
3342 message ("%s %q exported but not declared "
3344 ekind_capName (uentry_getKind (ce)),
3345 uentry_getName (ce)),
3348 uentry_showDefSpecInfo (ce, fwhere);
3358 void usymtab_exportLocal (void)
3363 for (i = 0; i < utab->nentries; i++)
3365 uentry ce = utab->entries[i];
3367 if (!uentry_isDatatype (ce) && !uentry_isAnyTag (ce)
3368 && !uentry_isEitherConstant (ce)
3369 && !uentry_isIter (ce)
3370 && !uentry_isEndIter (ce)
3371 && !uentry_isExpandedMacro (ce)
3372 && uentry_isUsed (ce))
3374 /* check static uses */
3375 filelocList fuses = uentry_getUses (ce);
3376 fileloc mod = uentry_whereDefined (ce);
3377 bool ok = filelocList_isEmpty (fuses);
3378 fileloc fwhere = uentry_whereDeclared (ce);
3380 if (fileloc_isSpecialFile (fwhere)
3381 && !context_getFlag (FLG_UNUSEDSPECIAL))
3383 ok = TRUE; /* no errors for special files */
3387 filelocList_elements (fuses, uloc)
3389 if (fileloc_isUndefined (uloc) || !fileloc_sameModule (uloc, mod))
3392 /*@innerbreak@*/ break;
3394 } end_filelocList_elements;
3401 message ("%s exported but not used outside %s: %q",
3402 ekind_capName (uentry_getKind (ce)),
3403 fileloc_getBase (mod),
3404 uentry_getName (ce)),
3407 uentry_showDefSpecInfo (ce, fwhere);
3415 usymtab_allUsed (void)
3419 bool isFileStatic = usymtab_inFileScope ();
3420 cstring last_file = cstring_undefined;
3422 for (i = 0; i < utab->nentries; i++)
3424 bool hasError = FALSE;
3425 uentry ce = utab->entries[i];
3426 fileloc fwhere = uentry_whereDeclared (ce);
3428 if (fileloc_isUndefined (fwhere))
3430 fwhere = uentry_whereDefined (ce);
3433 if (fileloc_isInvalid (fwhere)
3434 || fileloc_isLib (fwhere)
3435 || fileloc_isBuiltin (fwhere)
3436 || ((fileloc_isSpecialFile (fwhere)
3437 || fileloc_isSpecialFile (uentry_whereDefined (ce)))
3438 && !context_getFlag (FLG_UNUSEDSPECIAL)))
3442 else if (!uentry_wasUsed (ce) && !uentry_isAnyTag (ce))
3444 cstring fname = fileloc_filename (fwhere);
3446 if (cstring_isUndefined (last_file))
3450 else if (cstring_equal (fname, last_file))
3459 if (uentry_isParam (ce))
3461 if (context_inMacro ())
3463 sRef cref = uentry_getSref (ce);
3465 if (uentry_isYield (ce))
3467 ; /* no checks (for now) */
3469 else if (sRef_isSafe (cref))
3475 if (uentry_hasRealName (ce))
3478 optgenerror (FLG_MACROPARAMS,
3479 message ("Macro parameter %q not used",
3480 uentry_getName (ce)),
3487 if (cstring_equalFree (uentry_getName (ce),
3488 cstring_makeLiteral ("...")))
3494 hasError = optgenerror (FLG_PARAMUNUSED,
3495 message ("Parameter %q not used",
3496 uentry_getName (ce)),
3501 else if (uentry_isFunction (ce) || uentry_isIter (ce))
3503 if (fileloc_isUser (fwhere))
3505 hasError = optgenerror
3507 message ("%q %q declared but not used",
3509 (uentry_isIter (ce) ? "Iterator"
3510 : (isFileStatic ? "File static function" : "Function")),
3511 uentry_getName (ce)),
3515 else if (uentry_isEndIter (ce))
3517 ; /* no error (already reported for iter */
3519 else if (uentry_isEnumConstant (ce))
3521 if (fileloc_isUser (fwhere))
3523 hasError = optgenerror
3525 message ("Enum member %q not used",
3526 uentry_getName (ce)),
3530 else if (uentry_isConstant (ce))
3532 if (fileloc_isUser (fwhere))
3534 hasError = optgenerror
3536 message ("Constant %q declared but not used",
3537 uentry_getName (ce)),
3541 else if (uentry_isDatatype (ce))
3543 if (fileloc_isUser (fwhere))
3545 hasError = optgenerror
3547 message ("Type %q declared but not used",
3548 uentry_getName (ce)),
3552 else if (!uentry_isRefParam (ce) && !uentry_isExpandedMacro (ce))
3553 { /* errors for ref params will be reported in the next scope */
3554 llassertprint (uentry_isVar (ce),
3555 ("ce: %s", uentry_unparseFull (ce)));
3557 if (ctype_isFunction (uentry_getType (ce)))
3559 if (fileloc_isUser (fwhere))
3561 hasError = optgenerror
3563 message ("%q %q declared but not used",
3565 (isFileStatic ? "File static function"
3567 uentry_getName (ce)),
3573 if (fileloc_isUser (fwhere))
3577 hasError = optgenerror
3579 message ("%q %q declared but not used",
3581 (isFileStatic ? "File static variable"
3583 uentry_getName (ce)),
3593 else if ((uentry_isDatatype (ce) || uentry_isAnyTag (ce)))
3594 { /* check all fields */
3595 ctype ct = uentry_getRealType (ce);
3598 while (ctype_isAP (ct))
3600 ct = ctype_getBaseType (ct);
3603 if (ctype_isSU (ct))
3605 uentryList fields = ctype_getFields (ct);
3607 uentryList_elements (fields, field)
3609 if (!uentry_isUsed (field))
3611 if (uentry_hasName (ce))
3613 hasError |= optgenerror
3615 message ("Field %q of %s %q declared but not used",
3616 uentry_getName (field),
3617 cstring_makeLiteralTemp
3618 (ctype_isStruct (ct) ? "structure" : "union"),
3619 uentry_getName (ce)),
3620 uentry_whereEarliest (field));
3626 ** Can't report these errors for unnamed structs.
3627 ** No way to tell when there are multiple consistent
3628 ** unnamed structure types. (Could go through table
3629 ** and mark them all unused...)
3631 hasError |= optgenerror
3633 message ("Field %q of unnamed %s declared but not used",
3634 uentry_getName (field),
3635 cstring_makeLiteralTemp
3636 (ctype_isStruct (ct) ? "structure" : "union")),
3637 uentry_whereEarliest (field));
3642 uentry_setUsed (field, fileloc_undefined);
3644 } end_uentryList_elements;
3654 if (uentry_isParam (ce) && context_inMacro ())
3656 if (fileloc_isDefined (uentry_whereSpecified (ce)))
3658 uentry_showWhereSpecified (ce);
3663 uentry_showDefSpecInfo (ce, fwhere);
3666 uentry_setUsed (ce, fileloc_undefined);
3672 checkGlobalReturn (uentry glob, sRef orig)
3674 sRef sr = uentry_getSref (glob);
3676 DPRINTF (("Check global return: %s / orig: %s / sr: %s",
3677 uentry_unparseFull (glob),
3678 sRef_unparseFull (orig),
3679 sRef_unparseFull (sr)));
3681 DPRINTF (("Is killed: %s", bool_unparse (sRef_isKilledGlob (orig))));
3683 if (context_getFlag (FLG_GLOBSTATE))
3685 DPRINTF (("Is killed: %s", sRef_unparseFull (orig)));
3687 if (sRef_isKilledGlob (orig))
3689 if (sRef_isStateUndefined (sr)
3690 || sRef_isUnuseable (sr)
3691 || sRef_isStateUnknown (sr)
3692 || sRef_isDead (sr))
3698 ctype ct = ctype_realType (uentry_getType (glob));
3700 DPRINTF (("Check global destroyed: %s", uentry_unparseFull (glob)));
3702 if (ctype_isVisiblySharable (ct))
3707 ("Killed global %q (type %s) not released before return",
3708 uentry_getName (glob),
3709 ctype_unparse (ct)),
3712 sRef_showStateInfo (sr);
3717 sRef_protectDerivs ();
3718 (void) transferChecks_globalDestroyed (sr, g_currentloc);
3719 sRef_clearProtectDerivs ();
3725 if (sRef_isStateUndefined (sr))
3727 if (optgenerror (FLG_GLOBSTATE,
3729 ("Function returns with global %q undefined",
3730 uentry_getName (glob)),
3733 sRef_showStateInfo (sr);
3738 if (sRef_isDead (sr) || sRef_isKept (sr))
3742 message ("Function returns with global %q "
3743 "referencing %s storage",
3744 uentry_getName (glob),
3745 cstring_makeLiteralTemp (sRef_isDead (sr) ? "released" : "kept")),
3748 if (sRef_isKept (sr))
3750 sRef_showAliasInfo (sr);
3754 sRef_showStateInfo (sr);
3757 sRef_setDefState (sr, SS_UNKNOWN, fileloc_undefined);
3761 if (ctype_isRealPointer (uentry_getType (glob))
3762 && sRef_possiblyNull (sr)
3763 && !uentry_possiblyNull (glob))
3767 message ("Function returns with non-null global %q "
3768 "referencing null storage",
3769 uentry_getName (glob)),
3772 sRef_showNullInfo (sr);
3777 DPRINTF (("Check transfer: %s", uentry_unparseFull (glob)));
3778 transferChecks_globalReturn (glob);
3786 ** remember: check alias globals
3789 void usymtab_checkFinalScope (bool isReturn)
3792 bool mustFree = context_getFlag (FLG_MUSTFREEONLY) || context_getFlag (FLG_MUSTFREEFRESH); /*@i423 remove this mustFree */
3793 bool mustDefine = context_getFlag (FLG_MUSTDEFINE);
3794 /* bool mustNotAlias = context_getFlag (FLG_MUSTNOTALIAS); */
3795 sRefSet checked = sRefSet_new ();
3796 usymtab stab = utab;
3800 ** need to check all scopes out to function parameters.
3805 for (i = 0; i < stab->nentries; i++)
3807 uentry ce = stab->entries[i];
3808 sRef sr = uentry_getSref (ce);
3809 sRef rb = sRef_getRootBase (sr);
3813 ** Shouldn't check if shadow checked in deeper scope:
3818 uentry oue = usymtab_lookupQuietNoAlt (utab, uentry_observeRealName (ce));
3820 if (!uentry_sameObject (ce, oue))
3822 DPRINTF (("Skipping outer entry: %s / %s", uentry_unparseFull (ce),
3823 uentry_unparseFull (oue)));
3824 /*@i32 what if it is one an alternate branch? */
3825 /*@innercontinue@*/ continue;
3829 DPRINTF (("Here check final scope: %s", uentry_unparseFull (ce)));
3831 if (ctype_isFunction (uentry_getType (ce)))
3833 /*@innercontinue@*/ continue;
3836 if (uentry_isAnyParam (ce)
3837 || uentry_isRefParam (ce)
3838 || sRef_isFileOrGlobalScope (rb))
3840 /* Don't do the loseref check...but should check state! */
3841 DPRINTF (("Skipping check 1"));
3843 else if (sRef_isDefinitelyNull (sr)
3844 || usymtab_isDefinitelyNull (sr))
3847 ** No state reference errors for definitely null references.
3850 DPRINTF (("Skipping check 2"));
3854 DPRINTF (("Lose ref: %s / %s", uentry_unparse (ce),
3855 sRef_unparseFull (sr)));
3857 tvalues = sRef_getValueTable (sr);
3859 valueTable_elements (tvalues, fkey, fval) {
3860 metaStateInfo minfo;
3861 cstring msg = cstring_undefined;
3864 minfo = context_lookupMetaStateInfo (fkey);
3865 llassert (metaStateInfo_isDefined (minfo));
3867 if (stateValue_isError (fval)
3868 || sRef_isStateUndefined (sr)) /* No errors for undefined state */
3870 DPRINTF (("Skipping check 3"));
3874 DPRINTF (("Check: %s / %s / %s", fkey,
3875 metaStateInfo_unparse (minfo),
3876 stateValue_unparse (fval)));
3878 minfo = context_lookupMetaStateInfo (fkey);
3880 nval = stateCombinationTable_lookupLoseReference
3881 (metaStateInfo_getTransferTable (minfo),
3882 stateValue_getValue (fval), &msg);
3884 if (cstring_isDefined (msg))
3886 /*@i32 print extra info for assignments@*/
3887 DPRINTF (("From: %s", sRef_unparseFull (sr)));
3888 DPRINTF (("Null? %s / %s",
3889 bool_unparse (sRef_isDefinitelyNull (sr)),
3890 bool_unparse (usymtab_isGuarded (sr))));
3895 ("%s loses reference %q in invalid state %q (%s)",
3896 cstring_makeLiteralTemp (isReturn ? "Return" : "Scope exit"),
3897 uentry_getName (ce),
3898 stateValue_unparseValue (fval, minfo),
3902 stateValue_show (fval, minfo);
3906 DPRINTF (("Suppressed transfer error: %s", msg));
3910 } end_valueTable_elements;
3913 DPRINTF (("Here 1"));
3917 DPRINTF (("Check mustfree entry: %s", uentry_unparseFull (ce)));
3919 if (!sRefSet_member (checked, sr) && !sRef_isFileOrGlobalScope (rb))
3921 if (ctype_isRealSU (uentry_getType (ce))
3922 && !uentry_isAnyParam (ce)
3923 && !uentry_isRefParam (ce)
3924 && !uentry_isStatic (ce)
3925 && !sRef_isDependent (sr)
3926 && !sRef_isOwned (sr))
3928 sRefSet als = usymtab_allAliases (sr);
3930 if (sRefSet_isEmpty (als))
3932 transferChecks_localDestroyed (sr, g_currentloc);
3936 /* aliased, no problem */ ;
3942 (!uentry_isStatic (ce)
3943 && ((sRef_isNewRef (sr))
3944 || (((sRef_isOnly (sr) || sRef_isFresh (sr)
3945 || sRef_isKeep (sr) || sRef_isOwned (sr))
3946 && !sRef_isDead (sr))
3947 && (!sRef_definitelyNull (sr))
3948 && (!usymtab_isDefinitelyNull (sr)))))
3950 bool hasError = TRUE;
3952 DPRINTF (("Checking: %s", sRef_unparseFull (sr)));
3955 ** If its a scope exit, check if there is an alias.
3956 ** If so, make it only. If not, there is an error.
3961 if (transferChecks_canLoseReference (sr, g_currentloc))
3963 DPRINTF (("Can lose!"));
3970 if (sRef_hasLastReference (sr))
3972 sRef ar = sRef_getAliasInfoRef (sr);
3975 (sRef_isFresh (ar) ? FLG_MUSTFREEFRESH : FLG_MUSTFREEONLY,
3977 ("Last reference %q to %s storage %qnot %q before %q",
3979 alkind_unparse (sRef_getAliasKind (sr)),
3980 sRef_unparseOpt (ar),
3981 cstring_makeLiteral (sRef_isKeep (sr)
3982 ? "transferred" : "released"),
3983 cstring_makeLiteral (isReturn
3984 ? "return" : "scope exit")),
3987 sRef_showRefLost (sr);
3990 else if (sRef_isNewRef (sr))
3993 (sRef_isFresh (sr) ? FLG_MUSTFREEFRESH : FLG_MUSTFREEONLY,
3995 ("%q %q not released before %q",
3997 (alkind_isKillRef (sRef_getOrigAliasKind (sr))
3998 ? "Kill reference parameter" : "New reference"),
3999 uentry_getName (ce),
4000 cstring_makeLiteral (isReturn
4001 ? "return" : "scope exit")),
4004 sRef_showAliasInfo (sr);
4009 if (ctype_isRealSU (sRef_getType (sr)))
4011 transferChecks_structDestroyed (sr, g_currentloc);
4015 DPRINTF (("Here we are: %s", sRef_unparseFull (sr)));
4018 (sRef_isFresh (sr) ? FLG_MUSTFREEFRESH : FLG_MUSTFREEONLY,
4020 ("%s storage %q not %q before %q",
4021 alkind_capName (sRef_getAliasKind (sr)),
4022 uentry_getName (ce),
4023 cstring_makeLiteral (sRef_isKeep (sr)
4024 ? "transferred" : "released"),
4025 cstring_makeLiteral (isReturn
4026 ? "return" : "scope exit")),
4029 sRef_showAliasInfo (sr);
4030 DPRINTF (("Storage: %s", sRef_unparseFull (sr)));
4043 if (mustDefine && uentry_isOut (ce))
4045 /* No error if its dead (either only or error already reported */
4046 if (!sRef_isReallyDefined (sr) && !sRef_isDead (sr))
4050 message ("Out storage %q not defined before %q",
4051 uentry_getName (ce),
4053 (isReturn ? "return" : "scope exit")),
4056 DPRINTF (("sr: %s", sRef_unparseFull (sr)));
4061 ** also check state is okay
4064 if (usymtab_lexicalLevel () > functionScope
4065 && uentry_isVariable (ce)
4066 && (sRef_isLocalVar (sr)
4067 && (sRef_isDependent (sr) || sRef_isLocalState (sr))))
4069 sRefSet ab = usymtab_aliasedBy (sr);
4071 /* should do something more efficient here */
4073 if (sRefSet_isEmpty (ab))
4075 /* and no local ref */
4076 DPRINTF (("Check lose ref: %s", uentry_unparseFull (ce)));
4077 transferChecks_loseReference (ce);
4091 checked = sRefSet_insert (checked, sr);
4094 llassert (usymtab_isDefined (stab->env));
4096 if (usymtab_isBranch (stab))
4098 stab = usymtab_dropEnv (stab);
4105 llassert (stab != usymtab_undefined);
4106 } while (isReturn && (stab->lexlevel >= paramsScope));
4108 sRefSet_free (checked);
4112 ** all globals are appropriately defined
4113 ** all parameters are appropriately defined
4114 ** special clauses are followed
4117 if (isReturn || (utab->lexlevel == paramsScope))
4119 uentry fcn = context_getHeader ();
4120 uentryList params = context_getParams ();
4121 globSet uglobs = context_getUsedGlobs ();
4122 globSet sglobs = context_getGlobs ();
4124 if (isReturn && context_maybeSet (FLG_GLOBALIAS))
4126 aliasTable_checkGlobs (utab->aliases);
4130 ** state clauses (ensures, defines, sets, allocates, releases)
4133 if (uentry_hasStateClauseList (fcn))
4135 stateClauseList clauses = uentry_getStateClauseList (fcn);
4137 stateClauseList_elements (clauses, cl)
4139 if (stateClause_isAfter (cl) && !stateClause_isGlobal (cl))
4141 if (stateClause_setsMetaState (cl))
4143 sRefSet rfs = stateClause_getRefs (cl);
4144 qual q = stateClause_getMetaQual (cl);
4145 annotationInfo ainfo = qual_getAnnotationInfo (q);
4146 metaStateInfo minfo = annotationInfo_getState (ainfo);
4147 cstring key = metaStateInfo_getName (minfo);
4148 int mvalue = annotationInfo_getValue (ainfo);
4150 DPRINTF (("Post meta state clause: %s", stateClause_unparse (cl)));
4152 sRefSet_elements (rfs, el)
4154 sRef base = sRef_getRootBase (el);
4156 if (sRef_isResult (base))
4159 ** This is checked for return transfers.
4163 else if (sRef_isParam (base) || sRef_isGlobalMarker (base))
4165 sRef sr = sRef_updateSref (base);
4166 sr = sRef_fixBase (el, sr);
4168 if (!sRef_checkMetaStateValue (sr, key, mvalue))
4173 ("Ensures clause not satisfied%q (state is %q): %q",
4174 sRef_isGlobalMarker (sr)
4176 : message (" by %q", sRef_unparse (sr)),
4177 stateValue_unparseValue (sRef_getMetaStateValue (sr, key),
4179 stateClause_unparse (cl)),
4182 sRef_showMetaStateInfo (sr, key);
4188 if (sRef_isMeaningful (el))
4193 } end_sRefSet_elements ;
4197 /* evs - 2000 07 10 - added this */
4198 sRefTest tst = stateClause_getPostTestFunction (cl);
4199 sRefSet rfs = stateClause_getRefs (cl);
4201 sRefSet_elements (rfs, el)
4203 sRef base = sRef_getRootBase (el);
4205 if (sRef_isResult (base))
4208 ** This is checked for return transfers.
4213 else if (sRef_isParam (base))
4215 sRef sr = sRef_updateSref (base);
4216 sr = sRef_fixBase (el, sr);
4218 if (tst != NULL && !tst (sr))
4221 (stateClause_postErrorCode (cl),
4222 message ("%s storage %qcorresponds to "
4223 "storage listed in %q clause",
4224 stateClause_postErrorString (cl, sr),
4225 sRef_unparseOpt (sr),
4226 stateClause_unparseKind (cl)),
4229 sRefShower ss = stateClause_getPostTestShower (cl);
4236 DPRINTF (("Storage: %s", sRef_unparseFull (sr)));
4242 if (sRef_isMeaningful (el))
4247 } end_sRefSet_elements ;
4250 } end_stateClauseList_elements ;
4254 ** check parameters on return
4257 uentryList_elements (params, arg)
4259 if (!uentry_isElipsisMarker (arg))
4261 ctype rt = ctype_realType (uentry_getType (arg));
4263 if (ctype_isMutable (rt) || ctype_isSU (rt))
4265 uentry param = usymtab_lookupQuiet (utab, uentry_rawName (arg));
4266 DPRINTF (("Check param return: %s", uentry_unparseFull (param)));
4267 transferChecks_paramReturn (param);
4270 } end_uentryList_elements;
4272 DPRINTF (("Check global return: %s",
4273 globSet_unparse (sglobs)));
4275 globSet_allElements (sglobs, el)
4277 sRef orig = el; /*! sRef_updateSref (el); */ /*!!!*/
4278 uentry current = sRef_getUentry (el);
4280 DPRINTF (("Check global return: %s / %s", sRef_unparseFull (el),
4281 uentry_unparseFull (current)));
4283 if (uentry_isVariable (current) && !uentry_isRealFunction (current))
4285 checkGlobalReturn (current, orig);
4287 } end_globSet_allElements;
4289 globSet_allElements (uglobs, el)
4291 if (!globSet_member (sglobs, el))
4293 uentry current = sRef_getUentry (el);
4295 if (uentry_isVariable (current)
4296 && !uentry_isRealFunction (current))
4298 checkGlobalReturn (current, sRef_undefined);
4301 } end_globSet_allElements;
4306 usymtab_quietExitScope (fileloc loc)
4307 /*@globals utab, globtab, filetab; @*/
4310 usymtab t = utab->env;
4312 if (utab->reftable != NULL)
4316 for (i = 0; i < utab->nentries; i++)
4318 uentry current = utab->entries[i];
4319 uentry old = usymtab_lookupAux (t, uentry_rawName (current));
4321 uentry_mergeState (old, current, loc, FALSE, FALSE, FALSE, NOCLAUSE);
4325 llassert (t != NULL);
4327 if (t->lexlevel > paramsScope)
4329 t->guards = guardSet_levelUnion (t->guards, utab->guards, t->lexlevel);
4330 t->aliases = aliasTable_levelUnionSeq (t->aliases, utab->aliases,
4332 utab->aliases = aliasTable_undefined;
4335 t->mustBreak = utab->mustBreak;
4336 t->exitCode = utab->exitCode;
4338 usymtab_freeLevel (utab);
4344 ** Exit a scope with no checking, lose alias states.
4345 ** (When should this be used?)
4348 void usymtab_quietPlainExitScope (void)
4349 /*@globals utab, globtab, filetab@*/
4352 usymtab t = utab->env;
4354 llassert (t != NULL);
4355 llassert (NOALIAS (utab->aliases, t->aliases));
4356 usymtab_freeLevel (utab);
4360 void usymtab_exitScope (exprNode expr)
4361 /*@globals utab, filetab, globtab@*/
4362 /*@modifies utab, globtab@*/
4364 usymtab ctab = usymtab_undefined;
4365 usymtab lctab = usymtab_undefined;
4366 bool mustReturn = exprNode_mustEscape (expr);
4368 DPRINTF (("Exit scope"));
4370 if (utab->kind == US_CBRANCH)
4373 ** save the case branches, remove the first non-cbranch
4378 while (utab->kind == US_CBRANCH)
4382 llassert (utab != GLOBAL_ENV);
4386 if (utab->kind == US_TBRANCH || utab->kind == US_FBRANCH
4387 || utab->kind == US_CBRANCH || utab->kind == US_SWITCH) {
4389 if (context_inMacro ()) {
4390 /* evs 2000-07-25 */
4391 /* Unparseable macro may end inside nested scope. Deal with it. */
4393 llerror (FLG_SYNTAX,
4394 message ("Problem parsing macro body of %s (unbalanced scopes). "
4395 "Attempting to recover, recommend /*@notfunction@*/ before "
4396 "macro definition.",
4397 context_inFunctionName ()));
4399 while (utab->kind == US_TBRANCH
4400 || utab->kind == US_FBRANCH
4401 || utab->kind == US_CBRANCH
4402 || utab->kind == US_SWITCH)
4405 llassert (utab != GLOBAL_ENV);
4408 llcontbug (message ("exitScope: in branch: %q", usymtab_unparseStack ()));
4410 } /*@=branchstate@*/
4414 ** check all variables in scope were used
4418 ** bogus errors if this is the normal inside a switch,
4419 ** since cases have not been merged yet. Should probably
4420 ** still check this, but I'm too lazy at the moment...
4423 llassertfatal (utab->env != GLOBAL_ENV);
4425 if (utab->env->kind != US_SWITCH)
4431 ** check aliasing: all only params are released (dead)
4432 ** definition: all out params are defined, all modified params
4433 ** are completely defined
4435 ** NOTE: note for exiting paramsScope, since checkReturn should be
4439 if (!mustReturn && (usymtab_lexicalLevel () > functionScope))
4442 ** should only call this is end of scope is reachable...
4445 usymtab_checkFinalScope (FALSE);
4448 if (usymtab_lexicalLevel () == paramsScope && context_inFunctionLike ())
4451 ** leaving a function, need to fix up globals
4454 uentryList params = context_getParams ();
4455 globSet globs = context_getUsedGlobs ();
4458 uentryList_elements (params, ue)
4460 uentry_fixupSref (ue);
4461 } end_uentryList_elements;
4463 clearFunctionTypes ();
4466 globSet_allElements (globs, el)
4468 if (sRef_isCvar (el))
4471 int index = sRef_getScopeIndex (el);
4473 if (sRef_isFileStatic (el))
4475 llassert (usymtab_isDefined (filetab));
4476 current = usymtab_fetchIndex (filetab, index);
4480 current = usymtab_fetchIndex (globtab, index);
4483 if (uentry_isVariable (current))
4485 uentry_fixupSref (current);
4489 sRef_clearDerived (uentry_getSref (current));
4492 } end_globSet_allElements;
4495 usymtab_quietExitScope (exprNode_loc (expr));
4497 if (lctab != usymtab_undefined)
4499 /*@i@*/ lctab->env = utab;
4500 /*@i@*/ utab = ctab;
4501 /*@-branchstate@*/ } /*@=branchstate@*/
4507 ** yikes! don't let the '170 kids see this one...
4511 uentry_directParamNo (uentry ue)
4513 if (uentry_isVar (ue))
4515 sRef sr = uentry_getSref (ue);
4517 if (sRef_lexLevel (sr) == functionScope)
4519 int index = sRef_getScopeIndex (sr);
4521 if (index < uentryList_size (context_getParams ()))
4530 /*@dependent@*/ /*@exposed@*/ uentry
4531 usymtab_getParam (int paramno)
4535 ** requires in a function context (checked)
4537 ** depends on no nested functions --- the function
4538 ** parameters are ALWAYS one scope inside the global scope
4539 ** and entered in order!
4543 if (!context_inFunctionLike ())
4544 llfatalbug (message ("usymtab_getParam: not in function context: %q",
4545 context_unparse ()));
4547 while (s->lexlevel > paramsScope)
4552 llassert (usymtab_isDefined (s));
4554 if (paramno >= s->nentries)
4557 ** Parse errors lead to this.
4560 uentry err = uentry_makeVariableLoc (cstring_makeLiteralTemp ("<error>"),
4563 uentry_markOwned (err);
4567 return (s->entries[paramno]);
4570 static /*@dependent@*/ /*@exposed@*/ uentry
4571 usymtab_getRefTab (/*@notnull@*/ usymtab u, int level, usymId index)
4575 ue = usymtab_getRefNoisy (u, level, index);
4577 if (uentry_isUndefined (ue))
4579 llbug (message ("usymtab_getRef: out of range: %d. level = %d",
4586 static /*@dependent@*/ /*@exposed@*/ usymtab
4587 usymtab_dropEnv (/*@notnull@*/ usymtab s)
4589 if (s->kind == US_CBRANCH)
4597 llassert (s != GLOBAL_ENV);
4598 } while (s->kind == US_CBRANCH);
4599 /* drop all cases (except in nested scopes */
4602 llassert (s != GLOBAL_ENV);
4605 if (s->kind == US_FBRANCH)
4607 s = s->env; /* skip the true branch */
4608 llassert (usymtab_isDefined (s));
4609 llassert (s->kind == US_TBRANCH);
4612 llassert (s != GLOBAL_ENV);
4618 /*@dependent@*/ /*@exposed@*/ uentry
4619 usymtab_getRefQuiet (int level, usymId index)
4625 llassert (s != NULL);
4626 llassert (index >= 0);
4628 if (level > s->lexlevel)
4630 return uentry_undefined;
4633 llassertprint (level <= s->lexlevel, ("level: %d / lexlevel: %d",
4634 level, s->lexlevel));
4636 while (s->lexlevel > level)
4638 if (usymtab_isBranch (s))
4640 int eindex = refTable_lookup (s, level, index);
4642 if (eindex != NOT_FOUND)
4644 return (s->entries[eindex]);
4648 s = usymtab_dropEnv (s);
4651 while (usymtab_isBranch (s) && s->lexlevel == level)
4653 int eindex = refTable_lookup (s, level, index);
4655 if (eindex != NOT_FOUND)
4657 return (s->entries[eindex]);
4660 s = usymtab_dropEnv (s);
4663 if (index >= s->nentries)
4665 return uentry_undefined;
4668 llassert (!uentry_isUndefined (s->entries[index]));
4670 return s->entries[index];
4673 static /*@dependent@*/ /*@exposed@*/ uentry
4674 usymtab_getRefNoisy (/*@notnull@*/ usymtab s, int level, usymId index)
4677 uentry ue = uentry_undefined;
4679 llassert (index >= 0);
4681 while (s->lexlevel > level)
4683 if (usymtab_isBranch (s))
4685 int eindex = refTable_lookup (s, level, index);
4687 if (eindex != NOT_FOUND)
4689 ue = s->entries[eindex];
4693 while (!usymtab_isBranch (otab))
4695 otab = usymtab_dropEnv (otab);
4696 llassert (otab != GLOBAL_ENV);
4699 if (refTable_lookup (otab, level, index) == NOT_FOUND)
4701 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4713 s = usymtab_dropEnv (s);
4716 llassert (usymtab_isDefined (s));
4718 while (usymtab_isBranch (s) && s->lexlevel == level)
4720 int eindex = refTable_lookup (s, level, index);
4723 if (eindex != NOT_FOUND)
4725 ue = s->entries[eindex];
4729 while (!usymtab_isBranch (otab))
4731 otab = usymtab_dropEnv (otab);
4732 llassert (otab != GLOBAL_ENV);
4735 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4745 s = usymtab_dropEnv (s);
4748 if (s->lexlevel == level && (index < s->nentries))
4750 ue = s->entries[index];
4752 if (uentry_isValid (ue))
4756 while (!usymtab_isBranch (otab))
4758 otab = usymtab_dropEnv (otab);
4760 if (otab == GLOBAL_ENV)
4766 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4777 if (index >= s->nentries)
4779 return uentry_undefined;
4782 llassert (!uentry_isUndefined (s->entries[index]));
4784 return s->entries[index];
4788 ** looking up entries
4790 ** If entry is inside a branch, then copy it, and put it into
4791 ** the branch table.
4795 int refTable_lookup (/*@notnull@*/ usymtab ut, int level, int index)
4797 refTable rt = ut->reftable;
4800 llassert (rt != NULL);
4802 for (i = 0; i < ut->nentries; i++)
4804 if (rt[i]->level == level && rt[i]->index == index)
4814 /*@only@*/ refentry refentry_create (int level, int index)
4816 refentry r = (refentry) dmalloc (sizeof (*r));
4824 static /*@dependent@*/ /*@exposed@*/ uentry
4825 usymtab_addRefEntry (/*@notnull@*/ usymtab s, cstring k)
4830 if (ut->reftable == NULL)
4832 DPRINTF (("Adding ref entry without reftable: %s", k));
4833 return uentry_undefined;
4836 llassert (ut->reftable != NULL);
4838 while (s != GLOBAL_ENV)
4840 eindex = usymtab_getIndex (s, k);
4842 if (eindex != NOT_FOUND)
4844 uentry current = s->entries[eindex];
4846 if (uentry_isVar (current) && !ctype_isFunction (uentry_getType (current)))
4850 DPRINTF (("Here: copying %s", uentry_unparse (current)));
4851 ue = uentry_copy (current);
4852 DPRINTF (("Here: copying %s", uentry_unparse (ue)));
4853 usymtab_addEntryQuiet (ut, ue);
4854 DPRINTF (("Okay..."));
4856 if (s->reftable != NULL)
4858 refentry ref = s->reftable[eindex];
4860 ut->reftable[ut->nentries - 1]
4861 = refentry_create (ref->level, ref->index);
4865 ut->reftable[ut->nentries - 1]
4866 = refentry_create (s->lexlevel, eindex);
4877 s = usymtab_dropEnv (s);
4880 return uentry_undefined;
4883 static uentry usymtab_lookupAux (usymtab s, cstring k)
4885 DPRINTF (("Lookup: %s", k));
4887 while (s != GLOBAL_ENV)
4889 int eindex = usymtab_getIndex (s, k);
4891 if (eindex != NOT_FOUND)
4893 uentry ret = s->entries[eindex];
4897 if (s->kind == US_TBRANCH
4898 || s->kind == US_FBRANCH
4899 || s->kind == US_CBRANCH)
4900 /* uentry_isGlobalVariable (ret) && os->lexlevel > fileScope) */
4903 DPRINTF (("Adding global ref entry: %s", k));
4904 ret = usymtab_addRefEntry (os, k);
4905 DPRINTF (("Adding ref entry: %s", uentry_unparseFull (ret)));
4910 DPRINTF (("Found: %s", uentry_unparseFull (ret)));
4914 if (s->kind == US_TBRANCH || s->kind == US_FBRANCH
4915 || s->kind == US_CBRANCH)
4917 /* why isn't this os??? */
4918 uentry ret = usymtab_addRefEntry (s, k);
4919 DPRINTF (("Adding ref entry: %s", uentry_unparseFull (ret)));
4926 return uentry_undefined;
4929 static /*@dependent@*/ /*@exposed@*/ uentry
4930 usymtab_lookupQuietAux (usymtab s, cstring k, bool noalt)
4934 while (s != GLOBAL_ENV)
4936 eindex = usymtab_getIndex (s, k);
4938 if (eindex != NOT_FOUND)
4940 uentry ret = s->entries[eindex];
4944 if (noalt && usymtab_isBranch (s))
4946 s = usymtab_dropEnv (s);
4950 llassert (s != NULL); /*@i523 should not need this? */
4955 return uentry_undefined;
4958 static /*@exposed@*/ /*@dependent@*/ uentry
4959 usymtab_lookupQuiet (usymtab s, cstring k)
4961 return usymtab_lookupQuietAux (s, k, FALSE);
4964 static /*@exposed@*/ /*@dependent@*/ uentry
4965 usymtab_lookupQuietNoAlt (usymtab s, cstring k)
4967 return usymtab_lookupQuietAux (s, k, TRUE);
4970 /*@dependent@*/ /*@observer@*/ uentry
4971 usymtab_lookupSafe (cstring k)
4974 DPRINTF (("Lookup safe: %s", k));
4975 return (usymtab_lookupAux (utab, k));
4979 usymtab_lookupExpose (cstring k)
4982 uentry ce = usymtab_lookupAux (utab, k);
4984 if (uentry_isUndefined (ce))
4986 llfatalbug (message ("usymtab_lookup: not found: *%s*", k));
4989 if (uentry_isPriv (ce))
4991 llfatalbug (message ("usymtab_lookup: private: *%s*", k));
4997 uentry usymtab_lookupExposeGlob (cstring k)
4999 return (usymtab_lookupGlobSafe (k));
5002 uentry usymtab_lookupGlob (cstring k)
5003 /*@globals globtab@*/
5005 uentry ce = usymtab_lookupAux (globtab, k);
5007 if (uentry_isUndefined (ce))
5008 llfatalbug (message ("usymtab_lookup: not found: %s", k));
5010 if (uentry_isPriv (ce))
5011 llfatalbug (message ("usymtab_lookup: private: %s", k));
5016 /*@observer@*/ uentry
5017 usymtab_lookupGlobSafe (cstring k)
5018 /*@globals globtab@*/
5020 uentry ce = usymtab_lookupAux (globtab, k);
5025 uentry usymtab_lookupEither (cstring k)
5028 uentry ce = usymtab_lookupSafe (k);
5030 if (uentry_isUndefined (ce))
5031 llfatalerror (message ("usymtab_lookup: not found: %s", k));
5038 usymtab_lookupType (cstring k)
5039 /*@globals globtab@*/
5041 usymId uid = usymtab_getTypeId (k);
5043 if (uid == USYMIDINVALID)
5045 llcontbug (message ("usymtab_lookupType: not found: %s", k));
5046 return ctype_unknown;
5049 return (uentry_getRealType (usymtab_getTypeEntry (uid)));
5054 usymtab_lookupAbstractType (cstring k) /*@globals globtab@*/
5056 usymId uid = usymtab_getTypeId (k);
5058 if (uid == USYMIDINVALID)
5060 llcontbug (message ("usymtab_lookupType: not found: %s", k));
5061 return ctype_unknown;
5064 return (uentry_getAbstractType (usymtab_getTypeEntry (uid)));
5068 ** if there is an unnamed lcl-specified struct tag matching
5069 ** the uentryList, return its datatype. Otherwise, returns
5074 usymtab_structFieldsType (uentryList f)
5075 /*@globals globtab@*/
5077 return (usymtab_suFieldsType (f, TRUE));
5081 usymtab_unionFieldsType (uentryList f)
5082 /*@globals globtab@*/
5084 return (usymtab_suFieldsType (f, FALSE));
5088 usymtab_suFieldsType (uentryList f, bool isStruct)
5089 /*@globals globtab@*/
5093 DPRINTF (("Fields: %s", uentryList_unparse (f)));
5095 if (fileloc_isSpec (g_currentloc))
5097 return (ctype_undefined);
5100 for (i = 0; i < globtab->nentries; i++)
5102 uentry current = globtab->entries[i];
5105 ? uentry_isStructTag (current) : uentry_isUnionTag (current)))
5107 if (isFakeTag (uentry_rawName (current)))
5109 ctype ct = uentry_getType (current);
5111 DPRINTF (("Check: %s", ctype_unparse (ct)));
5113 if ((isStruct ? ctype_isStruct (ct) : ctype_isUnion (ct))
5115 (uentry_isSpecified (current)
5116 && uentryList_equivFields (f, ctype_getFields (ct))))
5118 return uentry_getAbstractType (current);
5128 return ctype_undefined;
5132 usymtab_enumEnumNameListType (enumNameList f)
5133 /*@globals globtab@*/
5137 for (i = 0; i < globtab->nentries; i++)
5139 uentry current = globtab->entries[i];
5141 if (uentry_isEnumTag (current))
5143 if (isFakeTag (uentry_rawName (current)))
5145 ctype ct = uentry_getType (current);
5147 if (ctype_isEnum (ct) && (enumNameList_match (f, ctype_elist (ct))))
5149 return uentry_getType (current);
5155 return ctype_undefined;
5159 usymtab_exists (cstring k)
5162 uentry ce = usymtab_lookupSafe (k);
5163 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5167 usymtab_existsReal (cstring k)
5170 uentry ce = usymtab_lookupSafe (k);
5172 return (!(uentry_isUndefined (ce))
5173 && !(uentry_isPriv (ce))
5174 && !(uentry_isExpandedMacro (ce)));
5178 usymtab_existsGlob (cstring k)
5179 /*@globals globtab@*/
5181 uentry ce = usymtab_lookupAux (globtab, k);
5183 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5188 usymtab_existsEither (cstring k)
5191 uentry ce = usymtab_lookupAux (utab, k);
5193 return (uentry_isValid (ce));
5197 usymtab_existsGlobEither (cstring k)
5198 /*@globals globtab@*/
5200 uentry ce = usymtab_lookupAux (globtab, k);
5202 return (uentry_isValid (ce));
5207 usymtab_existsType (cstring k)
5208 /*@globals globtab@*/
5210 uentry ce = usymtab_lookupAux (globtab, k);
5212 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)) && uentry_isDatatype (ce));
5216 usymtab_existsTypeEither (cstring k)
5217 /*@globals globtab@*/
5220 ce = usymtab_lookupAux (globtab, k);
5221 return (uentry_isValid (ce) && uentry_isDatatype (ce));
5225 usymtab_existsStructTag (cstring k) /*@globals globtab@*/
5227 cstring sname = makeStruct (k);
5228 uentry ce = usymtab_lookupAux (globtab, sname);
5229 cstring_free (sname);
5230 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5234 usymtab_existsUnionTag (cstring k) /*@globals globtab@*/
5236 cstring uname = makeUnion (k);
5237 uentry ce = usymtab_lookupAux (globtab, uname);
5239 cstring_free (uname);
5241 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5245 usymtab_existsEnumTag (cstring k) /*@globals globtab@*/
5247 cstring ename = makeEnum (k);
5248 uentry ce = usymtab_lookupAux (globtab, ename);
5250 cstring_free (ename);
5251 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5255 bool usymtab_existsVar (cstring k)
5258 uentry ce = usymtab_lookupSafe (k);
5260 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)) && (uentry_isVar (ce)));
5269 refTable_free (/*@only@*/ /*@null@*/ refTable x, int nentries)
5275 for (i = 0; i < nentries; i++)
5285 usymtab_freeLevel (/*@notnull@*/ /*@only@*/ usymtab u)
5286 /*@globals globtab, utab, filetab@*/
5290 aliasTable_free (u->aliases);
5292 refTable_free (u->reftable, u->nentries);
5294 if (u == filetab || u == globtab)
5296 for (i = 0; i < u->nentries; i++)
5298 uentry_freeComplete (u->entries[i]);
5303 for (i = 0; i < u->nentries; i++)
5305 uentry_free (u->entries[i]);
5309 guardSet_free (u->guards);
5316 llassert (!cstringTable_isDefined (u->htable));
5324 usymtab_freeAux (/*@only@*/ usymtab u)
5325 /*@globals globtab, utab, filetab@*/
5328 while (u != GLOBAL_ENV)
5331 usymtab_freeLevel (u);
5338 void usymtab_free ()
5339 /*@globals killed utab, globtab, filetab@*/
5343 usymtab_freeAux (utab);
5346 static int usymtab_lexicalLevel (void) /*@globals utab@*/
5348 return (utab->lexlevel);
5351 bool usymtab_inGlobalScope () /*@globals utab, globtab@*/
5353 return (utab == globtab);
5356 bool usymtab_inFileScope () /*@globals utab@*/
5358 return (utab->lexlevel == fileScope);
5361 bool usymtab_inFunctionScope () /*@globals utab@*/
5363 return (utab->lexlevel == functionScope);
5368 usymtab_replaceEntry (uentry s)
5369 /*@globals utab, globtab@*/
5370 /*@modifies utab, s@*/
5372 usymtab_replaceEntryAux (utab, s);
5377 usymtab_matchForwardStruct (usymId u1, usymId u2)
5378 /*@globals globtab@*/
5380 uentry ue1 = usymtab_getTypeEntry (u1);
5381 uentry ue2 = usymtab_getTypeEntry (u2);
5383 if (uentry_isAnyTag (ue2))
5385 ctype reptype = uentry_getType (ue1);
5387 if (ctype_isPointer (reptype))
5389 ctype repbase = ctype_getBaseType (reptype);
5391 if (ctype_isUA (repbase))
5393 typeId rtuid = ctype_typeId (repbase);
5395 if (u2 == rtuid) return TRUE;
5397 if (usymId_isValid (rtuid))
5399 reptype = uentry_getType (usymtab_getTypeEntry (rtuid));
5401 return (ctype_isUA (reptype) && (u2 == (ctype_typeId (reptype))));
5410 void usymtab_addGuards (guardSet guards)
5413 utab->guards = guardSet_union (utab->guards, guards);
5416 static bool usymtab_isGuardedAux (sRef s)
5420 sRef base = sRef_getRootBase (s);
5421 int lowlevel = paramsScope;
5422 int baselevel = sRef_lexLevel (base);
5424 if (sRef_isCvar (base))
5426 lowlevel = baselevel;
5427 if (lowlevel < paramsScope) lowlevel = paramsScope;
5430 while (tab->lexlevel >= lowlevel)
5432 DPRINTF (("Is guarded? [%s] %s",
5433 guardSet_unparse (tab->guards),
5434 sRef_unparseFull (s)));
5436 if (guardSet_isGuarded (tab->guards, s))
5439 if (!sRef_definitelyNull (s))
5441 sRef_setNotNull (s, fileloc_undefined);
5447 tab = usymtab_dropEnv (tab);
5453 void usymtab_unguard (sRef s) /*@modifies utab@*/
5456 sRef base = sRef_getRootBase (s);
5457 int lowlevel = paramsScope;
5458 int baselevel = sRef_lexLevel (base);
5460 if (sRef_isCvar (base))
5462 lowlevel = baselevel;
5463 if (lowlevel < paramsScope) lowlevel = paramsScope;
5466 while (tab->lexlevel >= lowlevel)
5468 if (guardSet_isGuarded (tab->guards, s))
5470 guardSet_delete (tab->guards, s);
5473 tab = usymtab_dropEnv (tab);
5477 bool usymtab_isGuarded (sRef s)
5479 DPRINTF (("Is guarded? %s", sRef_unparseFull (s)));
5480 return (sRef_aliasCompleteSimplePred (usymtab_isGuardedAux, s));
5483 bool usymtab_isDefinitelyNull (sRef s)
5485 return (sRef_aliasCheckSimplePred (usymtab_isDefinitelyNullAux, s));
5488 bool usymtab_isDefinitelyNullDeep (sRef s)
5490 return (sRef_deepPred (usymtab_isDefinitelyNull, s));
5493 static bool usymtab_isDefinitelyNullAux (sRef s)
5497 sRef base = sRef_getRootBase (s);
5498 int lowlevel = paramsScope;
5500 if (sRef_isCvar (base))
5502 lowlevel = sRef_lexLevel (base);
5503 if (lowlevel < paramsScope) lowlevel = paramsScope;
5506 while (tab->lexlevel >= lowlevel)
5508 if (guardSet_mustBeNull (tab->guards, s))
5513 while (tab->kind == US_CBRANCH)
5518 llassert (usymtab_isDefined (tab));
5520 if (tab->kind == US_FBRANCH)
5523 llassert (tab->kind == US_TBRANCH);
5533 usymtab_printGuards ()
5534 /*@globals utab, globtab@*/
5536 usymtab ttab = utab;
5538 while (ttab != globtab)
5540 llmsg (message ("Guards [%d]: %q", ttab->lexlevel,
5541 guardSet_unparse (ttab->guards)));
5547 usymtab_displayAllUses ()
5548 /*@globals utab, globtab@*/
5552 /* only in top scope */
5553 llassert (utab == globtab);
5555 /* need a copy, so order is not messed up by sort! */
5556 copy = usymtab_shallowCopy (globtab);
5558 qsort (copy->entries, (size_t)copy->nentries,
5559 sizeof (*copy->entries), (int (*)(const void *, const void *)) uentry_xcompareuses);
5561 usymtab_entries (copy, ue)
5563 if (uentry_isValid (ue))
5565 filelocList uses = uentry_getUses (ue);
5566 int size = filelocList_realSize (uses);
5568 if (fileloc_isDefined (uentry_whereDefined (ue))
5569 && !fileloc_isLib (uentry_whereDefined (ue))
5572 llmsg (message ("%q (%q), %d use%&:\n %q",
5573 uentry_getName (ue),
5574 fileloc_unparse (uentry_whereDefined (ue)),
5575 size, filelocList_unparseUses (uses)));
5578 } end_usymtab_entries;
5580 usymtab_shallowFree (copy);
5583 static /*@dependent@*/ /*@exposed@*/ usymtab
5584 usymtab_getFileTab ()
5585 /*@globals filetab@*/
5587 llassert (filetab != NULL);
5593 usymtab_unparseStack ()
5596 return (usymtab_unparseStackTab (utab));
5599 static /*@only@*/ cstring
5600 usymtab_unparseStackTab (usymtab t)
5602 bool firstOne = TRUE;
5603 cstring ret = cstring_makeLiteral ("[");
5605 while (t != GLOBAL_ENV)
5609 ret = message ("%q %q", ret, usymtab_typeName (t));
5614 ret = message ("%q, %q", ret, usymtab_typeName (t));
5619 ret = message ("%q ]", ret);
5623 static /*@only@*/ cstring
5624 usymtab_typeName (/*@notnull@*/ usymtab t)
5628 case US_GLOBAL: return cstring_makeLiteral ("global");
5629 case US_NORMAL: return cstring_makeLiteral ("normal");
5630 case US_TBRANCH: return cstring_makeLiteral ("true");
5631 case US_FBRANCH: return cstring_makeLiteral ("false");
5632 case US_CBRANCH: return cstring_makeLiteral ("case");
5633 case US_SWITCH: return cstring_makeLiteral ("switch");
5639 void usymtab_addMustAlias (/*@exposed@*/ sRef s, /*@exposed@*/ sRef al)
5642 if (!sRef_similar (s, al))
5644 usymtab_addForceMustAlias (s, al);
5649 ** Same as usymtab_addMustAlias, except does not check sRef_isSimilar.
5652 void usymtab_addForceMustAlias (/*@exposed@*/ sRef s, /*@exposed@*/ sRef al)
5655 /* evans 2002-03-3: was sRef_isMeaningful -- but we need to keep aliases for new storage also! */
5656 if (sRef_isMeaningful (s)
5657 && sRef_isMeaningful (al)
5658 && !(sRef_isConst (s) || sRef_isConst (al))
5659 && !(sRef_isAddress (al) && sRef_isDirectParam (sRef_getBase (al))))
5661 utab->aliases = aliasTable_addMustAlias (utab->aliases, s, al);
5662 DPRINTF (("Must alias: %s", aliasTable_unparse (utab->aliases)));
5665 ** for local variable, aliasing is symmetric
5668 if (sRef_isLocalVar (s) && sRef_isLocalVar (al))
5670 utab->aliases = aliasTable_addMustAlias (utab->aliases, al, s);
5675 DPRINTF (("Not aliasing! %s / %s", sRef_unparseFull (s), sRef_unparseFull (al)));
5676 DPRINTF (("meaningful: %d %d", sRef_isMeaningful (s), sRef_isMeaningful (al)));
5680 void usymtab_addReallyForceMustAlias (/*@exposed@*/ sRef s, /*@exposed@*/ sRef al)
5683 utab->aliases = aliasTable_addMustAlias (utab->aliases, s, al);
5686 void usymtab_clearAlias (sRef s)
5687 /*@modifies utab, s@*/
5690 aliasTable_clearAliases (utab->aliases, s);
5693 sRefSet usymtab_allAliases (sRef s)
5696 if (sRef_isSomewhatMeaningful (s))
5700 ret = sRefSet_unionFree (aliasTable_aliasedBy (utab->aliases, s),
5701 aliasTable_canAlias (utab->aliases, s));
5706 DPRINTF (("NOT A MEANINGFUL SREF!"));
5707 return sRefSet_undefined;
5711 /*@only@*/ sRefSet usymtab_canAlias (sRef s)
5714 if (sRef_isSomewhatMeaningful (s))
5716 sRefSet res = aliasTable_canAlias (utab->aliases, s);
5720 return sRefSet_undefined;
5723 /*@only@*/ sRefSet usymtab_aliasedBy (sRef s)
5726 return (aliasTable_aliasedBy (utab->aliases, s));
5729 /*@only@*/ cstring usymtab_unparseAliases ()
5732 return (aliasTable_unparse (utab->aliases));
5736 ** Debugging routines:
5737 ** okay to leak storage here, only for debugging
5743 usymtab_printOut (void)
5749 char *ind = mstring_copy (" ");
5751 fprintf (g_warningstream, "<<< [symbol table] >>>\n");
5753 while (s != GLOBAL_ENV && s->env != GLOBAL_ENV)
5755 cstring tname = usymtab_typeName (s);
5759 ind[depth * 3 + 1] = '\0';
5762 fprintf (g_warningstream, "level: %d (%s)\n", s->lexlevel,
5763 cstring_toCharsSafe (tname));
5765 cstring_free (tname);
5767 for (i = 0; i < s->nentries; i++)
5769 cstring us = uentry_unparseFull (s->entries[i]);
5770 fprintf (g_warningstream, "%s\n", cstring_toCharsSafe (us));
5774 if (s->reftable != NULL && s->nentries > 0)
5776 fprintf (g_warningstream, "\t<< Ref table >>\n");
5778 for (i = 0; i < s->nentries; i++)
5780 fprintf (g_warningstream, "\t%s %3d: %d, %d\n", ind, i,
5781 s->reftable[i]->level,
5782 s->reftable[i]->index);
5786 ind[depth * 3 + 1] = ' ';
5790 fprintf (g_warningstream, "<<< end usymtab >>>\n");
5796 usymtab_printTypes ()
5797 /*@globals globtab@*/
5799 usymtab_printAllAux (globtab);
5803 usymtab_printAll (void)
5806 usymtab_printAllAux (utab);
5810 usymtab_printAllAux (usymtab s)
5811 /*@modifies g_warningstream@*/
5815 char *ind = mstring_copy (" ");
5817 printf ("[[[ usymtab ]]]");
5819 while (s != GLOBAL_ENV)
5822 ind[depth * 3 + 1] = '\0';
5824 if (s->env == GLOBAL_ENV)
5828 printf ("level: %d / break: %s / exit: %s\n", s->lexlevel,
5829 cstring_toCharsSafe (bool_unparse (s->mustBreak)),
5830 cstring_toCharsSafe (exitkind_unparse (s->exitCode)));
5834 for (i = looplow; i < s->nentries; i++)
5836 printf ("%s%3d. %s\n", ind, i,
5837 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5842 printf ("level: %d / break: %s / exit: %s\n", s->lexlevel,
5843 cstring_toCharsSafe (bool_unparse (s->mustBreak)),
5844 cstring_toCharsSafe (exitkind_unparse (s->exitCode)));
5846 for (i = 0; i < s->nentries; i++)
5848 printf ("%s%3d %s\n", ind, i,
5849 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5853 ind[depth * 3 + 1] = ' ';
5857 printf ("----------\n");
5861 usymtab_printComplete ()
5866 char *ind = mstring_copy (" ");
5869 while (s != GLOBAL_ENV)
5873 ind[depth * 3 + 1] = '\0';
5876 if (s->env == GLOBAL_ENV)
5880 printf ("level: %d\n", s->lexlevel);
5884 for (i = looplow; i < s->nentries; i++)
5886 printf ("%s%3d %s\n", ind, i,
5887 cstring_toCharsSafe (uentry_unparse (s->entries[i])));
5892 printf ("level: %d\n", s->lexlevel);
5893 for (i = 0; i < s->nentries; i++)
5895 printf ("%s%3d %s\n", ind, i,
5896 cstring_toCharsSafe (uentry_unparse (s->entries[i])));
5900 ind[depth * 3 + 1] = ' ';
5905 printf ("----------\n");
5910 static /*@only@*/ cstring /*@unused@*/
5911 usymtab_unparseLocalAux (/*@notnull@*/ usymtab s)
5913 cstring c = message ("lexlevel: %d\n", s->lexlevel);
5916 for (i = 0; i < s->nentries; i++)
5918 c = message ("%q\n%q", c, uentry_unparseFull (s->entries[i]));
5921 c = message ("%q\n=========", c);
5925 static cstring /*@unused@*/ /*@only@*/
5926 usymtab_unparseLocalList (/*@notnull@*/ usymtab s)
5928 cstring c = message ("[%d/%s/%s] ", s->lexlevel,
5929 bool_unparse (s->mustBreak),
5930 exitkind_unparse (s->exitCode));
5933 for (i = 0; i < s->nentries; i++)
5935 sRef sr = uentry_getSref (s->entries[i]);
5939 c = message ("%q: %q [%b]", c, uentry_getName (s->entries[i]),
5940 sRef_isStateDefined (sr));
5944 c = message ("%q, %q [%b]", c, uentry_getName (s->entries[i]),
5945 sRef_isStateDefined (sr));
5955 usymtab_printLocal (void)
5961 printf ("lexlevel: %d\n", s->lexlevel);
5963 for (i = 0; i < s->nentries; i++)
5965 printf ("%s\n", cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5968 while (s->lexlevel > 1)
5973 llassert (usymtab_isDefined (s));
5975 printf ("Params:\n");
5977 for (i = 0; i < s->nentries; i++)
5979 printf ("%d: %s\n", i,
5980 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5985 static bool checkDistinctExternalName (uentry e)
5986 /*@globals globtab@*/
5987 /*@modifies *g_warningstream@*/
5989 size_t checklen = size_fromInt (context_getValue (FLG_EXTERNALNAMELEN));
5990 bool ignorecase = context_getFlag (FLG_EXTERNALNAMECASEINSENSITIVE);
5991 bool gotone = FALSE;
5992 bool extras = FALSE;
5993 bool hasError = FALSE;
5994 cstring name = uentry_rawName (e);
5995 usymtab st = globtab;
6003 if (uentry_isAnyTag (e))
6005 checklen++; /* the tag marker doesn't count */
6009 usymtab_entries (st, oe)
6011 if (uentry_sameObject (oe, e))
6018 if (cstring_equalCaseInsensitive (uentry_rawName (oe), name))
6027 (FLG_DISTINCTEXTERNALNAMES,
6029 ("External identifier %q is not distinguishable from %q "
6030 "because alphabetical case is ignored",
6032 uentry_getName (oe)),
6033 uentry_whereLast (e)))
6035 uentry_showWhereAny (oe);
6036 uentry_setHasNameError (oe);
6045 if (cstring_equalLenCaseInsensitive (uentry_rawName (oe),
6054 if (cstring_equalLen (uentry_rawName (oe), name, checklen))
6057 (FLG_DISTINCTEXTERNALNAMES,
6060 ("External identifier %q is not distinguishable from %q "
6061 "in the first %d characters (%q)",
6063 uentry_getName (oe),
6064 size_toInt (checklen),
6065 cstring_clip (uentry_getName (e), checklen)),
6067 uentry_whereLast (e)))
6069 uentry_showWhereAny (oe);
6070 uentry_setHasNameError (oe);
6083 (FLG_DISTINCTEXTERNALNAMES,
6085 ("External identifier %q is not distinguishable from %q "
6086 "in the first %d characters because alphabetical case "
6089 uentry_getName (oe),
6090 size_toInt (checklen)),
6091 uentry_whereLast (e)))
6093 uentry_showWhereAny (oe);
6094 uentry_setHasNameError (oe);
6100 else if (cstring_equalLen (uentry_rawName (oe), name, checklen))
6109 (FLG_DISTINCTEXTERNALNAMES,
6112 ("External identifier %q is not distinguishable from %q "
6113 "in the first %d characters (%q)",
6115 uentry_getName (oe),
6116 size_toInt (checklen),
6117 cstring_clip (uentry_getName (e), checklen)),
6119 uentry_whereLast (e)))
6121 uentry_showWhereAny (oe);
6122 uentry_setHasNameError (oe);
6131 } end_usymtab_entries ;
6138 (cstring_makeLiteral ("One or more additional "
6139 "indistinguishable external "
6140 "names not reported"));
6146 static bool checkDistinctInternalName (uentry e)
6148 /*@modifies *g_warningstream@*/
6150 usymtab ttab = utab;
6151 cstring name = uentry_rawName (e);
6152 size_t numchars = size_fromInt (context_getValue (FLG_INTERNALNAMELEN));
6153 bool caseinsensitive = context_getFlag (FLG_INTERNALNAMECASEINSENSITIVE);
6154 bool lookalike = context_getFlag (FLG_INTERNALNAMELOOKALIKE);
6156 if (uentry_isAnyTag (e) && (numchars != 0))
6158 numchars++; /* the tag marker doesn't count */
6161 while (usymtab_isDefined (ttab))
6163 usymtab_entries (ttab, oe)
6165 if (uentry_sameObject (oe, e))
6167 /*@innercontinue@*/ continue;
6170 switch (cstring_genericEqual
6171 (name, uentry_rawName (oe),
6172 numchars, caseinsensitive, lookalike))
6174 case CGE_DISTINCT: /* okay */
6178 if (cstring_equal (name, uentry_rawName (oe)))
6180 ; /* got a shadow error */
6185 (FLG_DISTINCTINTERNALNAMES,
6188 ("Internal identifier %q is not distinguishable from %q "
6189 "in the first %d characters (%q)",
6191 uentry_getName (oe),
6192 size_toInt (numchars),
6193 cstring_clip (uentry_getName (e), numchars)),
6195 uentry_whereLast (e)))
6197 uentry_showWhereAny (oe);
6198 uentry_setHasNameError (oe);
6205 if (numchars == 0 || (cstring_length (name) <= numchars))
6208 (FLG_DISTINCTINTERNALNAMES,
6210 ("Internal identifier %q is not distinguishable from %q "
6211 "without case sensitivity",
6213 uentry_getName (oe)),
6214 uentry_whereLast (e)))
6216 uentry_showWhereAny (oe);
6217 uentry_setHasNameError (oe);
6224 (FLG_DISTINCTINTERNALNAMES,
6226 ("Internal identifier %q is not distinguishable from %q "
6227 "in the first %d characters without case sensitivity",
6229 uentry_getName (oe),
6230 size_toInt (numchars)),
6231 uentry_whereLast (e)))
6233 uentry_showWhereAny (oe);
6234 uentry_setHasNameError (oe);
6242 || (cstring_length (name) <= numchars))
6245 (FLG_DISTINCTINTERNALNAMES,
6247 ("Internal identifier %q is not distinguishable from %q "
6248 "except by lookalike characters",
6250 uentry_getName (oe)),
6251 uentry_whereLast (e)))
6253 uentry_showWhereAny (oe);
6254 uentry_setHasNameError (oe);
6261 (FLG_DISTINCTINTERNALNAMES,
6263 ("Internal identifier %q is not distinguishable from %q "
6264 "in the first %d characters except by lookalike characters",
6266 uentry_getName (oe),
6267 size_toInt (numchars)),
6268 uentry_whereLast (e)))
6270 uentry_showWhereAny (oe);
6271 uentry_setHasNameError (oe);
6276 } end_usymtab_entries ;
6284 void usymtab_checkDistinctName (uentry e, int scope)
6285 /*@globals utab, globtab@*/
6287 bool hasError = FALSE;
6288 fileloc where = uentry_whereLast (e);
6290 if (!fileloc_isPreproc (where) && !fileloc_isBuiltin (where))
6292 if (scope == globScope)
6294 if (context_getFlag (FLG_DISTINCTEXTERNALNAMES))
6296 hasError = checkDistinctExternalName (e);
6300 if (!hasError && context_getFlag (FLG_DISTINCTINTERNALNAMES))
6302 hasError = checkDistinctInternalName (e);
6308 uentry_setHasNameError (e);
6312 /*@exposed@*/ sRef usymtab_lookupGlobalMarker (void) /*@globals utab@*/
6316 ue = usymtab_lookupAux (utab, GLOBAL_MARKER_NAME);
6317 llassert (uentry_isValid (ue));
6319 return uentry_getSref (ue);