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 DPRINTF (("Init bool!"));
368 if (context_getFlag (FLG_NOLIB))
370 ctype boolt = ctype_bool;
371 /* evs 2000-07-24: bool is now treated as abstract (always) */
373 uentry boolentry = uentry_makeBoolDatatype (YES);
374 usymtab_supGlobalEntry (boolentry);
375 context_addBoolAccess ();
378 ** We supply values 0 and 1 for the constants, so things like
379 ** while (TRUE) can be interpreted correctly.
382 usymtab_supGlobalEntry
383 (uentry_makeConstantValue (context_getFalseName (), boolt,
384 fileloc_getBuiltin (), FALSE,
385 multiVal_makeInt (0)));
387 usymtab_supGlobalEntry
388 (uentry_makeConstantValue (context_getTrueName (), boolt,
389 fileloc_getBuiltin (), FALSE,
390 multiVal_makeInt (1)));
399 usymtab_grow (/*@notnull@*/ usymtab s)
402 o_uentry *oldsyms = s->entries;
404 s->nspace = CBASESIZE;
405 s->entries = (uentry *) dmalloc (sizeof (*s->entries)
406 * (s->nentries + s->nspace));
408 for (i = 0; i < s->nentries; i++)
410 s->entries[i] = oldsyms[i];
415 if (s->reftable != NULL)
417 refTable oldRefs = s->reftable;
419 s->reftable = (refentry *) dmalloc (sizeof (*s->reftable)
420 * (s->nentries + CBASESIZE + 1));
422 for (i = 0; i < s->nentries; i++)
424 s->reftable[i] = oldRefs[i];
435 usymtab_addEntryQuiet (/*@notnull@*/ usymtab s, /*@keep@*/ uentry e)
437 if (!(s->lexlevel > fileScope || !sRef_modInFunction ()))
439 if (uentry_isDatatype (e)
440 || uentry_isFunction (e)
441 || uentry_isAnyTag (e)
442 || uentry_isEnumConstant (e)
443 || context_inMacro ())
446 ** Not a bug. Code like,
447 ** int f (void) { typedef int tint; ... }
451 /* was nothing here! */
452 /*@i@*/ e->sref = sRef_saveCopy (e->sref);
457 (cstring_makeLiteral ("Declaration outside function scope"));
459 llcontbug (message ("usymtab_addEntryQuiet: inconsistent state "
460 "(lexlevel = %d, modFunction = %s) adding: %q",
461 s->lexlevel, bool_unparse (sRef_modInFunction ()),
462 uentry_unparse (e)));
463 sRef_setGlobalScope ();
464 /* make sure the sRef is not bashed! */
465 /*@i@*/ e->sref = sRef_saveCopy (e->sref);
475 s->entries[s->nentries] = e;
478 if (s == globtab || s == filetab)
480 if (!fileloc_isLib (g_currentloc))
482 uentry_tallyAnnots (e, AN_UNKNOWN);
487 if (cstringTable_isDefined (s->htable))
489 cstringTable_insert (s->htable, cstring_copy (uentry_rawName (e)), s->nentries);
495 static /*@observer@*/ uentry /*@alt void@*/
496 usymtab_addEntryBase (/*@notnull@*/ usymtab s, /*@only@*/ uentry e)
499 ** In theory, we shouldn't need this test because it this is
500 ** only called when a library is being read, and it shouldn't
501 ** ever have a duplicate entry. In practice, its safer to
502 ** leave it in, though.
507 if (uentry_isValid (old = usymtab_lookupQuiet (s, uentry_rawName (e))))
509 DPRINTF (("Duplicate entry: %s", uentry_unparse (e)));
510 uentry_free (e); /* duplicate */
515 int thisentry = s->nentries;
517 if (uentry_isVar (e))
519 uentry_setSref (e, sRef_makeCvar (globScope, thisentry,
521 stateInfo_makeLoc (uentry_whereLast (e))));
524 usymtab_addEntryQuiet (s, e);
530 static /*@observer@*/ uentry /*@alt void@*/
531 usymtab_addEntryAlways (/*@notnull@*/ usymtab s, /*@only@*/ uentry e)
534 ** In theory, we shouldn't need this test because it this is
535 ** only called when a library is being read, and it shouldn't
536 ** ever have a duplicate entry. In practice, its safer to
537 ** leave it in, though.
541 int thisentry = s->nentries;
543 if (uentry_isValid (old = usymtab_lookupQuiet (s, uentry_rawName (e))))
546 (message ("Duplicate entry in load library: %s. "
547 "Old entry: %q. New entry: %q",
549 uentry_unparseFull (old),
550 uentry_unparseFull (e)));
552 uentry_setName (e, message ("__x_%s", uentry_rawName (e)));
553 /* This shouldn't happen...unless the library is bad! */
557 if (uentry_isVar (e) && !uentry_isGlobalMarker (e))
559 uentry_setSref (e, sRef_makeCvar (globScope, thisentry,
561 stateInfo_makeLoc (uentry_whereLast (e))));
564 usymtab_addEntryQuiet (s, e);
569 usymtab_addEntryAux (/*@notnull@*/ usymtab st, /*@keep@*/ uentry e, bool isSref)
570 /*@globals globtab@*/
573 usymId thisentry = st->nentries;
575 llassert (!uentry_isElipsisMarker (e));
579 ** llassert (usymtab_lookupSafe (uentry_rawName (e)) == uentry_undefined);
582 llassertprint (uentry_isAnyTag (e) ? st == globtab : TRUE,
583 ("non-global tag: %s / %d", uentry_unparseFull (e), st->lexlevel));
585 if (st->lexlevel == fileScope
586 && (!(uentry_isStatic (e)) || uentry_isAnyTag (e)))
593 ctype ct = uentry_getType (e);
595 if (uentry_isFunction (e) && ctype_isFunction (ct))
597 ct = ctype_getReturnType (ct);
600 if (uentry_isStatic (e))
602 sRef sr = sRef_makeCvar (st->lexlevel, thisentry, ct, stateInfo_makeLoc (uentry_whereLast (e)));
604 if (sRef_isStack (sr) || sRef_isLocalState (sr))
606 sRef_setAliasKind (sr, AK_STATIC, uentry_whereLast (e));
607 sRef_setDefined (sr, uentry_whereLast (e));
610 uentry_setSref (e, sr);
614 uentry_setSref (e, sRef_makeCvar (st->lexlevel, thisentry, ct, stateInfo_makeLoc (uentry_whereLast (e))));
618 if (uentry_isDatatype (e))
621 uentry_setDatatype (e, thisentry);
624 if (uentry_isFunction (e))
626 ctype ct = uentry_getType (e);
628 if (ctype_isFunction (ct)
629 && uentryList_isMissingParams (ctype_argsFunction (ct)))
631 if (uentry_isDeclared (e))
635 message ("Function %q declared without parameter list",
637 uentry_whereDeclared (e));
643 message ("Function %q specified without parameter list",
645 uentry_whereSpecified (e));
650 if (st == globtab && !uentry_isSpecified (e))
652 exprChecks_checkExport (e);
655 uentry_checkName (e);
657 usymtab_addEntryQuiet (st, e);
658 DPRINTF (("Adding entry: [%p] %s", e, uentry_unparseFull (e)));
663 usymtab_addEntry (uentry e)
664 /*@globals utab, globtab@*/
665 /*@modifies utab, e@*/
667 llassertprint (!usymtab_exists (uentry_rawName (e)),
668 ("Entry already exists: %s", uentry_unparse (e)));
670 return usymtab_addEntryAux (utab, e, FALSE);
674 usymtab_addGlobalEntry (uentry e)
675 /*@modifies globtab, e@*/
677 DPRINTF (("Add global: %s / %s", uentry_unparse (e), bool_unparse (uentry_isForward (e))));
679 (void) usymtab_addEntryAux (globtab, e, FALSE);
683 ** supercede and replace entry use elements of entries array, and
684 ** assign an element to a new value, and then free the old value.
685 ** Need -deparrays to avoid errors for this.
691 usymtab_supEntryAux (/*@notnull@*/ usymtab st,
692 /*@only@*/ uentry e, bool isSref)
693 /*@globals globtab, filetab@*/
694 /*@modifies st, globtab, e@*/
696 cstring ename = uentry_rawName (e);
697 bool staticEntry = FALSE;
700 DPRINTF (("Sup entry aux: %s", uentry_unparseFull (e)));
702 /* static tags in global scope */
703 if (st->lexlevel == fileScope
704 && (!(uentry_isStatic (e)) || uentry_isAnyTag (e)))
706 eindex = usymtab_getIndex (st, ename);
708 if (eindex != NOT_FOUND)
710 uentry ce = st->entries[eindex];
714 message ("%s %q shadows static declaration",
715 ekind_capName (uentry_getKind (e)),
717 uentry_whereDeclared (e)))
719 uentry_showWhereLast (ce);
722 if (eindex == st->nentries - 1)
728 st->entries[eindex] = st->entries[st->nentries - 1];
731 if (cstringTable_isDefined (st->htable))
733 cstringTable_replaceKey (st->htable, uentry_rawName (ce),
734 cstring_copy (uentry_rawName (e)));
744 if (uentry_isStatic (e)) {
745 if (uentry_isFunction (e)) {
747 ** Static function declarations are at the file level,
748 ** even if they are in a deeper scope.
751 st = usymtab_getFileTab ();
754 if (!uentry_isAnyTag (e) && st == globtab)
756 st = usymtab_getFileTab ();
758 DPRINTF (("Static entry!"));
763 eindex = usymtab_getIndex (st, ename);
765 if (eindex != NOT_FOUND)
767 uentry ce = st->entries[eindex];
769 DPRINTF (("Found entry: %s", uentry_unparse (ce)));
771 if (uentry_isPriv (ce)
772 /* || (fileloc_isImport (uentry_whereSpecified (ce))) */
773 || (uentry_isStatic (ce)
774 && uentry_isAnyTag (ce)
775 && (uentry_isDeclared (ce)
776 && !fileloc_sameFile (uentry_whereDefined (ce),
777 uentry_whereDefined (e)))))
781 ** if overloading import should do some checks!
784 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
786 DPRINTF (("Overloading!"));
788 st->entries[eindex] = e;
790 if (uentry_isDatatype (e))
792 uentry_setDatatype (e, eindex);
795 if (st == globtab && !uentry_isSpecified (e))
797 exprChecks_checkExport (e);
800 if (cstringTable_isDefined (st->htable))
802 cstringTable_replaceKey (st->htable, uentry_rawName (ce),
803 cstring_copy (uentry_rawName (e)));
809 else if (uentry_isSpecified (ce))
811 if (uentry_isSpecified (e))
813 DPRINTF (("Here we are: %s", uentry_unparseFull (e)));
815 if (fileloc_isImport (uentry_whereSpecified (ce)))
817 if (cstringTable_isDefined (st->htable))
819 cstringTable_replaceKey (st->htable,
821 cstring_copy (uentry_rawName (e)));
825 st->entries[eindex] = e;
828 if (uentry_isDatatype (e)) uentry_setDatatype (e, eindex);
832 if (fileloc_isImport (uentry_whereSpecified (e)))
838 /* Respecification errors already reported */
839 DPRINTF (("Respecification: %s / %s",
840 fileloc_unparse (uentry_whereSpecified (e)),
841 bool_unparse (fileloc_isSpec (uentry_whereSpecified (e)))));
843 if (uentry_isDatatype (e))
845 uentry_setDatatype (e, eindex);
848 if (cstringTable_isDefined (st->htable))
850 cstringTable_replaceKey (st->htable,
852 cstring_copy (uentry_rawName (e)));
855 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
857 st->entries[eindex] = e;
862 else /* e not specified */
864 DPRINTF (("Merging..."));
866 if (uentry_isDeclared (ce))
869 No - this can happen for internal typedefs
870 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
873 DPRINTF (("Merge defn"));
874 uentry_mergeDefinition (ce, e);
879 No - this can happen for internal typedefs
880 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
883 DPRINTF (("Merge entries..."));
884 uentry_mergeEntries (ce, e);
885 DPRINTF (("After: %s", uentry_unparseFull (ce)));
889 else /* ce not specified */
891 if (!(st->lexlevel > fileScope || !sRef_modInFunction ()))
893 if (uentry_isDatatype (e) || uentry_isAnyTag (e)
894 || uentry_isEnumConstant (e))
897 ** Not a bug. Code like,
898 ** int f (void) { typedef int tint; ... }
904 llcontbug (message ("usymtab_supEntryAux: inconsistent state "
905 "(lexlevel = %d, modFunction = %s) adding: %q",
906 st->lexlevel, bool_unparse (sRef_modInFunction ()),
907 uentry_unparse (e)));
909 if (sRef_modInFunction ())
911 /* make sure the sRef is not bashed! */
912 /*@i@*/ e->sref = sRef_saveCopy (e->sref);
917 DPRINTF (("Merge.."));
918 uentry_mergeDefinition (ce, e);
923 ctype ct = uentry_getType (ce);
925 if (uentry_isFunction (ce) && ctype_isFunction (ct))
927 ct = ctype_getReturnType (ct);
930 uentry_setSref (ce, sRef_makeCvar (st->lexlevel, eindex, ct, stateInfo_makeLoc (uentry_whereLast (ce))));
933 else /* no previous entry */
937 if (st->lexlevel == globScope
938 && !uentry_isStatic (e)
939 && !uentry_isExtern (e)
940 && usymtab_isDefined (filetab))
943 ** check if there is a static entry: (i.e.,
950 eindex = usymtab_getIndex (filetab, ename);
952 if (eindex != NOT_FOUND)
954 uentry ce = filetab->entries[eindex];
956 uentry_setStatic (e);
957 uentry_mergeDefinition (ce, e);
963 outer = usymtab_lookupQuiet (st->env, ename);
965 DPRINTF (("New : [%p] %s", e, uentry_unparseFull (e)));
966 DPRINTF (("Outer: [%p] %s", outer, uentry_unparseFull (outer)));
969 ** no previous definition, add the new one
973 && uentry_isValid (outer)
974 && !(uentry_isYield (e) || uentry_isYield (outer))
975 && fileloc_isDefined (uentry_whereLast (e))
976 && !fileloc_isXHFile (uentry_whereLast (e))
977 && fileloc_isDefined (uentry_whereLast (outer))
978 && !fileloc_isXHFile (uentry_whereLast (outer)))
980 if (!uentry_sameKind (outer, e))
986 if (ctype_isUnknown (uentry_getType (outer))
987 || uentry_isForward (outer))
995 message ("%s %q shadows outer declaration",
996 ekind_capName (uentry_getKind (e)),
998 uentry_whereLast (e)))
1000 uentry_showWhereLast (outer);
1006 if (st == globtab && context_getFlag (FLG_NEWDECL))
1010 message ("New declaration: %q", uentry_getName (e)),
1011 uentry_whereLast (e));
1014 eindex = usymtab_addEntryAux (st, e, isSref);
1018 return (staticEntry ? USYMIDINVALID : eindex);
1023 usymtab_replaceEntryAux (/*@notnull@*/ usymtab st, /*@only@*/ uentry e)
1024 /*@globals globtab@*/ /*@modifies st, e@*/
1026 cstring ename = uentry_rawName (e);
1029 /* static tags in global scope */
1030 eindex = usymtab_getIndex (st, ename);
1032 if (eindex != NOT_FOUND)
1034 uentry ce = st->entries[eindex];
1036 if (cstringTable_isDefined (st->htable))
1038 cstringTable_replaceKey (st->htable, uentry_rawName (ce),
1039 cstring_copy (uentry_rawName (e)));
1043 st->entries[eindex] = e;
1047 eindex = usymtab_addEntryAux (st, e, FALSE);
1054 void usymtab_supEntry (uentry e)
1055 /*@globals utab, filetab, globtab@*/
1056 /*@modifies utab, globtab, e@*/
1058 (void) usymtab_supEntryAux (utab, e, FALSE);
1062 ** this should be lots more efficient!
1065 static /*@exposed@*/ uentry
1066 usymtab_supEntryReturnAux (/*@notnull@*/ usymtab tab,
1067 /*@only@*/ uentry e, bool isref)
1068 /*@globals globtab, filetab@*/
1069 /*@modifies tab, globtab, e@*/
1071 cstring rawName = cstring_copy (uentry_rawName (e));
1072 bool stat = (tab == globtab) && uentry_isStatic (e);
1075 (void) usymtab_supEntryAux (tab, e, isref);
1079 ret = usymtab_lookupAux (filetab, rawName);
1083 ret = usymtab_lookupAux (tab, rawName);
1085 if (uentry_isInvalid (ret) && usymtab_isDefined (filetab))
1087 ret = usymtab_lookupAux (filetab, rawName);
1091 cstring_free (rawName);
1095 /*@dependent@*/ /*@exposed@*/ uentry
1096 usymtab_supEntryReturn (/*@only@*/ uentry e)
1097 /*@globals utab, filetab, globtab@*/
1098 /*@modifies utab, globtab, e@*/
1100 return (usymtab_supEntryReturnAux (utab, e, FALSE));
1103 /*@dependent@*/ /*@exposed@*/ uentry
1104 usymtab_supEntrySrefReturn (/*@only@*/ uentry e)
1105 /*@globals utab, globtab, filetab@*/
1106 /*@modifies utab, globtab, e@*/
1108 return (usymtab_supEntryReturnAux (utab, e, TRUE));
1111 /*@dependent@*/ /*@exposed@*/ uentry
1112 usymtab_supGlobalEntryReturn (uentry e)
1113 /*@globals globtab, filetab@*/
1114 /*@modifies globtab, e@*/
1118 ret = usymtab_supEntryReturnAux (globtab, e, FALSE);
1121 ** We need to keep track of internal function declarations, so
1122 ** we can remove them from the symbol table after exiting this
1123 ** function. This is a bit bogus, of course.
1126 if (sRef_modInFunction ())
1128 recordFunctionType (ret);
1135 usymtab_supTypeEntry (/*@only@*/ uentry e)
1136 /*@globals globtab, filetab@*/
1137 /*@modifies globtab, e@*/
1142 if (uentry_isAbstractDatatype (e))
1144 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1145 ret = ctype_createAbstract (uid);
1149 uid = usymtab_supEntryAux (globtab, e, FALSE);
1150 ret = ctype_createUser (uid);
1153 if (sRef_modInFunction ())
1155 recordFunctionType (globtab->entries[uid]);
1162 usymtab_supReturnTypeEntry (/*@only@*/ uentry e)
1163 /*@globals globtab, filetab@*/
1164 /*@modifies globtab@*/
1168 DPRINTF (("Abstract? %s", uentry_unparseFull (e)));
1170 if (uentry_isAbstractDatatype (e))
1172 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1174 else if (uentry_isMaybeAbstract (e) && context_getFlag (FLG_IMPABSTRACT))
1176 bool maybeabs = TRUE;
1177 cstring sname = uentry_getName (e);
1178 uentry ue = usymtab_lookupGlobSafe (sname);
1179 cstring_free (sname);
1181 if (uentry_isValid (ue))
1183 DPRINTF (("Lookup: %s", uentry_unparseFull (ue)));
1185 if (uentry_isDatatype (ue))
1187 if (uentry_isMaybeAbstract (ue))
1198 DPRINTF (("Not datatype!"));
1205 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1206 ux = usymtab_getTypeEntry (uid);
1207 uentry_setAbstract (ux);
1211 uid = usymtab_supEntryAux (globtab, e, FALSE);
1212 e = usymtab_getTypeEntry (uid);
1214 if (uentry_isMaybeAbstract (e))
1216 uentry_setConcrete (e);
1222 uid = usymtab_supEntryAux (globtab, e, FALSE);
1223 e = usymtab_getTypeEntry (uid);
1225 if (uentry_isMaybeAbstract (e))
1227 uentry_setConcrete (e);
1231 if (sRef_modInFunction ())
1233 recordFunctionType (globtab->entries[uid]);
1236 return (globtab->entries[uid]);
1240 usymtab_supAbstractTypeEntry (/*@only@*/ uentry e, bool dodef)
1241 /*@globals globtab, filetab@*/
1242 /*@modifies globtab, e@*/
1247 uid = usymtab_supEntryAux (globtab, e, FALSE);
1248 ue = usymtab_getTypeEntry (uid);
1252 uentry_setDatatype (ue, uid);
1255 if (context_getFlag (FLG_ACCESSMODULE)) /* was accessfile */
1257 context_addFileAccessType (uid);
1260 if (sRef_modInFunction ())
1262 recordFunctionType (globtab->entries[uid]);
1270 usymtab_supExposedTypeEntry (/*@only@*/ uentry e, bool dodef)
1271 /*@globals globtab, filetab@*/
1272 /*@modifies globtab, e@*/
1276 uid = usymtab_supEntryAux (globtab, e, FALSE);
1280 uentry ue = usymtab_getTypeEntry (uid);
1282 uentry_setDatatype (ue, uid);
1285 if (sRef_modInFunction ())
1287 recordFunctionType (globtab->entries[uid]);
1295 usymtab_supForwardTypeEntry (/*@only@*/ uentry e)
1296 /*@globals globtab, filetab@*/
1297 /*@modifies globtab, e@*/
1299 usymId uid = usymtab_supEntryAux (globtab, e, FALSE);
1300 uentry ue = usymtab_getTypeEntry (uid);
1302 uentry_setDatatype (ue, uid);
1304 if (sRef_modInFunction ())
1306 recordFunctionType (globtab->entries[uid]);
1309 return (uentry_getAbstractType (ue));
1313 usymtab_supEntrySref (uentry e)
1314 /*@globals utab, globtab, filetab@*/
1315 /*@modifies utab, globtab, e@*/
1317 sRef old = uentry_getSref (e);
1319 if (sRef_isType (old))
1321 uentry ue = usymtab_supEntryReturnAux (utab, e, TRUE);
1324 if (uentry_isValid (ue))
1326 sRef uref = uentry_getSref (ue);
1328 sRef_mergeStateQuiet (uref, old);
1329 sRef_clearDerived (uref);
1331 /*@noaccess uentry@*/
1333 else if (sRef_isKnown (old))
1335 usymtab_supEntry (e);
1339 (void) usymtab_supEntryAux (utab, e, TRUE);
1343 void usymtab_supGlobalEntry (/*@only@*/ uentry e)
1344 /*@globals globtab, filetab@*/
1345 /*@modifies globtab, filetab, e@*/
1349 DPRINTF (("Sup global entry: %s", uentry_unparse (e)));
1351 uid = usymtab_supEntryAux (globtab, e, FALSE);
1353 if (sRef_modInFunction ())
1355 recordFunctionType (globtab->entries[uid]);
1360 usymtab_supReturnFileEntry (/*@only@*/ uentry e)
1361 /*@globals filetab, globtab@*/
1362 /*@modifies filetab, globtab, e@*/
1364 llassert (filetab != usymtab_undefined);
1365 DPRINTF (("File entry: %s", uentry_unparse (e)));
1366 return (usymtab_supEntryReturnAux (filetab, e, FALSE));
1374 usymtab_inDeepScope () /*@globals utab@*/
1376 return (utab->lexlevel > paramsScope);
1380 usymtab_getIndex (/*@notnull@*/ usymtab s, cstring k)
1384 DPRINTF (("Lookup %s", k));
1386 if (cstringTable_isDefined (s->htable))
1388 i = cstringTable_lookup (s->htable, k);
1393 for (i = 0; i < s->nentries; i++)
1395 uentry current = s->entries[i];
1397 DPRINTF (("Check %d: %s", i, uentry_rawName (current)));
1399 if (!uentry_isUndefined (current)
1400 && cstring_equal (uentry_rawName (current), k))
1411 usymtab_fetchIndex (/*@notnull@*/ usymtab s, int i)
1413 llassert (i >= 0 && i < s->nentries);
1414 return (s->entries[i]);
1418 usymtab_getTypeId (cstring k) /*@globals globtab@*/
1420 usymId uid = usymtab_getIndex (globtab, k);
1422 if (uid == NOT_FOUND) return USYMIDINVALID;
1424 if (!(uentry_isDatatype (usymtab_getTypeEntry (uid)))) {
1425 return USYMIDINVALID;
1431 /*@dependent@*/ uentry
1432 usymtab_lookupStructTag (cstring k)
1434 cstring sname = makeStruct (k);
1435 uentry ue = usymtab_lookupGlob (sname);
1437 cstring_free (sname);
1441 /*@dependent@*/ uentry
1442 usymtab_lookupUnionTag (cstring k)
1444 cstring uname = makeUnion (k);
1445 uentry res = usymtab_lookupGlob (uname);
1447 cstring_free (uname);
1451 /*@dependent@*/ uentry
1452 usymtab_lookupEnumTag (cstring k)
1454 cstring ename = makeEnum (k);
1455 uentry res = usymtab_lookupGlob (ename);
1457 cstring_free (ename);
1462 usymtab_getId (cstring k) /*@globals globtab@*/
1464 usymId uid = usymtab_getIndex (globtab, k);
1467 if (uid == NOT_FOUND)
1469 return USYMIDINVALID;
1472 ue = usymtab_getGlobalEntry (uid);
1474 if (uentry_isPriv (ue))
1476 return USYMIDINVALID;
1482 static /*@exposed@*/ uentry
1483 usymtab_getEntryAux (/*@notnull@*/ usymtab s, usymId uid)
1485 llassert (uid != USYMIDINVALID);
1487 if (uid < 0 || uid >= s->nentries)
1489 llcontbug (message ("usymtab_getEntry: out of range: level = %d [%d]",
1491 return uentry_undefined;
1494 llassertprint (uentry_isValid (s->entries[uid]),
1495 ("entry undefined: %d", uid));
1497 return s->entries[uid];
1500 /*@dependent@*/ /*@observer@*/ uentry
1501 usymtab_getGlobalEntry (usymId uid)
1502 /*@globals utab, globtab@*/
1504 if (dbgfree) return (uentry_undefined);
1506 if (utab->lexlevel > paramsScope)
1508 /* need to do this the awkward way, since it could be in conditional scope */
1509 return (usymtab_lookupSafe (uentry_rawName (globtab->entries[uid])));
1513 return (globtab->entries[uid]);
1517 /*@dependent@*/ /*@exposed@*/ uentry
1518 usymtab_getTypeEntry (usymId uid)
1519 /*@globals globtab@*/
1523 if (uid >= 0 && uid < globtab->nentries)
1525 return (globtab->entries[uid]);
1529 return (uentry_undefined);
1534 llassert (uid >= 0 && uid < globtab->nentries);
1535 return (globtab->entries[uid]);
1543 /*@dependent@*/ /*@exposed@*/ uentry
1544 usymtab_getTypeEntrySafe (usymId uid)
1545 /*@globals globtab@*/
1547 if (uid < 0 || uid >= globtab->nentries)
1549 return uentry_undefined;
1552 return (globtab->entries[uid]);
1556 usymtab_isBoolType (usymId uid)
1557 /*@globals globtab@*/
1559 llassert (uid >= 0 && uid < globtab->nentries);
1561 return (cstring_equal (uentry_rawName (globtab->entries[uid]),
1562 context_getBoolName ()));
1566 usymtab_getTypeEntryName (usymId uid)
1567 /*@globals globtab@*/
1573 return (cstring_makeLiteral ("<freetype>"));
1576 ue = usymtab_getTypeEntry (uid);
1578 if (dbgload && !uentry_isValid (ue))
1580 return (message ("<missing type: %d>", uid));
1583 llassertprint (uentry_isValid (ue), ("type undefined: %d", uid));
1585 return (uentry_getName (ue));
1589 /*@unused@*/ static void
1590 usymtab_rehash (/*@notnull@*/ usymtab s)
1594 if (cstringTable_isDefined (s->htable))
1596 cstringTable_free (s->htable);
1599 s->htable = cstringTable_create (LLAHSHSIZE);
1601 for (i = 0; i < s->nentries; i++)
1603 cstringTable_insert (s->htable, cstring_copy (uentry_rawName (s->entries[i])), i);
1609 ** superficial copy of usymtab
1611 ** DO copy spec entries
1614 static /*@only@*/ /*@notnull@*/ usymtab
1615 usymtab_shallowCopy (/*@notnull@*/ usymtab s) /*@*/
1617 usymtab copytab = usymtab_createRoot ();
1620 for (i = 0; i < s->nentries; i++)
1622 usymtab_addEntryBase (copytab, s->entries[i]);
1629 usymtab_shallowFree (/*@only@*/ /*@notnull@*/ usymtab s)
1631 aliasTable_free (s->aliases);
1632 refTable_free (s->reftable, s->nentries);
1634 /*@-compdestroy@*/ sfree (s); /*@=compdestroy@*/
1638 ** converts usymId from old table to sorted one
1642 usymtab_convertId (usymId uid)
1643 /*@globals oldtab, utab@*/
1649 llassert (usymtab_isDefined (oldtab));
1651 ue = usymtab_getEntryAux (oldtab, uid);
1653 llassertprint (uentry_isValid (ue), ("convertId: undefined: %d", uid));
1655 name = uentry_rawName (ue);
1657 ret = usymtab_getIndex (utab, name);
1658 llassert (ret == uid); /*! for now, no rehash! */
1659 DPRINTF (("Convert: %s [%d] -> %s [%d]",
1660 uentry_unparse (ue), uid,
1661 uentry_unparse (utab->entries[ret]), ret));
1663 llassertprint (ret != USYMIDINVALID, ("convertId: return is invalid"));
1669 usymtab_prepareDump (void)
1670 /*@globals oldtab, utab@*/
1671 /*@modifies oldtab, utab@*/
1673 llassert (usymtab_inGlobalScope ());
1674 llassert (oldtab == usymtab_undefined);
1677 DPRINTF (("Preparing to dump:"));
1678 usymtab_printAll ();
1681 oldtab = usymtab_shallowCopy (utab);
1684 ** alpha compare - make sure order is same on different platforms
1685 ** error messages appear in same order
1689 qsort (utab->entries, (size_t)utab->nentries,
1690 sizeof (*utab->entries),
1691 (int (*)(const void *, const void *)) uentry_xcomparealpha);
1693 usymtab_rehash (utab);
1697 DPRINTF (("After rehash:"));
1698 usymtab_printAll ();
1702 void usymtab_dump (FILE *fout)
1703 /*@globals utab, oldtab@*/
1706 bool neednl = FALSE;
1707 uentry lastentry = uentry_undefined;
1708 ekind lastekind = KINVALID;
1712 ** must call prepareDump first
1715 llassert (oldtab != usymtab_undefined);
1717 for (i = 0; i < utab->nentries; i++)
1719 uentry thisentry = utab->entries[i];
1720 ekind thisekind = uentry_getKind (thisentry);
1722 if (!uentry_hasRealName (thisentry))
1724 llassert (uentry_isGlobalMarker (thisentry));
1728 check (fputc ('\n', fout) == (int) '\n');
1731 fprintf (fout, "*%d (GlobalMarker)\n", KGLOBALMARKER);
1732 lastekind = KINVALID;
1738 if (thisekind != lastekind)
1742 check (fputc ('\n', fout) == (int) '\n');
1746 lastentry = uentry_undefined;
1747 fprintf (fout, "*%d (%s)\n", ekind_toInt (thisekind),
1748 cstring_toCharsSafe (ekind_capName (thisekind)));
1749 lastekind = thisekind;
1754 ** evans - 2001-02-18 - added the - 48 fudge factor...
1755 ** extra characters dumped, but I haven't counded them carefully...
1758 if (uentry_isInvalid (lastentry) || !uentry_equiv (lastentry, thisentry)
1759 || (linelen > (MAX_DUMP_LINE_LENGTH - (2 * MAX_NAME_LENGTH) - 48)))
1763 DPRINTF (("Dumping entry: %d", i));
1764 cdump = message ("^%d %q", i, uentry_dump (thisentry));
1765 /* was: cdump = uentry_dump (thisentry)); */
1767 lastentry = thisentry;
1770 check (fputc ('\n', fout) == (int) '\n');
1774 linelen += cstring_length (cdump);
1776 /* no new line here! */
1777 if (cstring_length (cdump) > 0)
1779 check (fputs (cstring_toCharsSafe (cdump), fout) != EOF);
1782 cstring_free (cdump);
1787 cstring cdump = uentry_rawName (thisentry);
1788 DPRINTF (("Raw name: %s", cdump));
1789 linelen += (cstring_length (cdump) + 1);
1790 fprintf (fout, "#%s", cstring_toCharsSafe (cdump));
1797 check (fputc ('\n', fout) == (int) '\n');
1800 lastekind = KINVALID;
1802 fprintf(fout, ";; Library constraints\n");
1803 for (i = 0; i < utab->nentries; i++)
1805 uentry thisentry = utab->entries[i];
1807 if (uentry_isFunction (thisentry) )
1809 constraintList preconditions;
1810 constraintList postconditions;
1812 preconditions = uentry_getFcnPreconditions (thisentry);
1813 postconditions = uentry_getFcnPostconditions (thisentry);
1815 if ( constraintList_isDefined(preconditions) ||
1816 constraintList_isDefined(postconditions) )
1818 fprintf(fout,"%s\n", cstring_toCharsSafe (uentry_rawName(thisentry) ) );
1819 if (constraintList_isDefined(preconditions) )
1821 fprintf(fout,"pre:\n");
1822 constraintList_dump(preconditions, fout);
1823 fprintf (fout, ";; end precondition constraints\n" );
1824 constraintList_free(preconditions);
1828 fprintf(fout,"pre:EMPTY\n");
1830 if (constraintList_isDefined(postconditions) )
1832 fprintf(fout,"post:\n");
1833 constraintList_dump(postconditions, fout);
1834 fprintf (fout, ";; end precondition constraints\n" );
1835 constraintList_free(postconditions);
1839 fprintf(fout,"post:EMPTY\n");
1848 void usymtab_load (FILE *f)
1849 /*@globals utab, globtab@*/
1850 /*@modifies utab, *f@*/
1852 char *s = mstring_create (MAX_DUMP_LINE_LENGTH);
1854 ekind kind = KINVALID;
1855 fileloc loc = g_currentloc;
1861 llassert (utab == globtab);
1862 llassert (utab->nentries == 0);
1864 while (((s = reader_readLine (f, s, MAX_DUMP_LINE_LENGTH)) != NULL)
1867 /* ignore ; comments */ ;
1870 while (s != NULL && *s != ';')
1878 ek = reader_getInt (&s);
1880 if (ek == KGLOBALMARKER)
1882 uentry lue = uentry_makeGlobalMarker ();
1883 DPRINTF (("Adding global marker: %s", uentry_unparseFull (lue)));
1884 usymtab_addEntryAlways (utab, lue);
1890 kind = ekind_fromInt (ek);
1898 (cstring_makeLiteral
1899 ("Library is in obsolete format. Use splint +whichlib "
1900 "to see which library is being loaded."));
1903 if (reader_optCheckChar (&s, '^'))
1905 index = reader_getInt (&s);
1912 llassert (kind != KINVALID);
1913 ue = uentry_undump (kind, loc, &s);
1915 llassert (utab->nentries == index || index == -1);
1917 if (uentry_isValid (ue))
1919 int lastindex = utab->nentries;
1920 ue = usymtab_addEntryAlways (utab, ue);
1924 if (uentry_isConstant (ue)) /*@i23! isPreProcessorMacro */
1926 cstring uname = uentry_getName (ue);
1928 /* Also check its a macro... */
1929 DPRINTF (("Installing: %s", uname));
1931 cpphash_installMacro
1932 (mstring_copy (cstring_toCharsSafe (uname)),
1933 cstring_length (uname),
1934 cpplib_createDefinition (message ("%s 255", uname),
1937 cpphash_hashCode (cstring_toCharsSafe (uname),
1938 cstring_length (uname),
1941 DPRINTF (("After install: %s", uname));
1945 if (utab->nentries != lastindex + 1)
1947 DPRINTF (("No add: %s", uentry_unparseFull (ue)));
1955 ** now, any other names are the same uentry
1958 while (*(s++) == '#')
1960 cstring name = cstring_fromCharsO (reader_getWord (&s));
1961 uentry nue = uentry_nameCopy (name, ue);
1963 DPRINTF (("Name copy: %s", uentry_unparseFull (nue)));
1967 usymtab_addEntryAlways (utab, nue);
1970 while ((c = *s) != '\0' && (c !='\n'))
1972 if (c != ' ' || c != '\t')
1974 llbuglit ("Junk in load file");
1982 s = reader_readLine (f, os, MAX_DUMP_LINE_LENGTH);
1987 to handle reading of buffer overflow related constraints
1989 while (fgets (s, MAX_DUMP_LINE_LENGTH, f) != NULL
1992 ; /* ignore ;-comments */
1995 while (s != NULL && *s != ';')
1997 constraintList preconditions;
1998 constraintList postconditions;
2000 cstring name = cstring_fromChars(reader_getWord(&s) );
2002 ue = usymtab_lookup ( name );
2006 preconditions = constraintList_undefined;
2007 postconditions = constraintList_undefined;
2009 if (!uentry_isValid(ue) )
2011 llfatalbug ((message("Invalid uentry for %s library file may be corrupted", cstring_fromChars(s) ) ));
2013 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
2015 temp = cstring_fromChars (reader_getWord(&s) );
2017 if (cstring_compareLit (temp,"pre:") == 0 )
2019 preconditions = constraintList_undump (f);
2023 if (cstring_compareLit (temp,"pre:EMPTY") != 0 )
2024 llfatalbug ((message("Error reading library file pre:EMPTY expected but got %s", temp ) ));
2029 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
2031 temp = cstring_fromChars(reader_getWord(&s) );
2032 if (cstring_compareLit (temp, "post:") == 0 )
2034 postconditions = constraintList_undump (f);
2038 if (cstring_compareLit (temp, "post:EMPTY") != 0 )
2039 llfatalbug ((message("Error reading library file post:EMPTY expected but got %s", temp ) ));
2042 cstring_free (temp);
2044 uentry_setPreconditions (ue, functionConstraint_createBufferConstraint (preconditions));
2045 uentry_setPostconditions (ue, functionConstraint_createBufferConstraint (postconditions));
2047 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
2055 ** file scope for static variables
2059 usymtab_enterFile ()
2060 /*@globals utab, globtab, filetab@*/
2061 /*@modifies filetab@*/
2063 llassert (utab == globtab);
2068 usymtab_entries (globtab, ue)
2070 if (sRef_hasDerived (uentry_getSref (ue)))
2072 fprintf (g_warningstream, "Derived Global: %s\n", uentry_unparse (ue));
2073 fprintf (g_warningstream, "sRef: %s\n", sRef_unparseFull (ue->sref));
2075 } end_usymtab_entries ;
2079 usymtab_enterScope ();
2085 /*@globals utab, filetab@*/
2086 /*@modifies filetab, utab@*/
2089 llassert (utab->lexlevel == 1);
2091 usymtab_exitScope (exprNode_undefined);
2096 usymtab_enterScope ()
2097 /*@globals utab, globtab, filetab@*/
2100 usymtab t = usymtab_create (US_NORMAL, utab, TRUE);
2102 /* unconditional scope: optimize to avoid copy */
2103 t->aliases = aliasTable_copy (utab->aliases);
2106 llassert (usymtab_isDefined (t->env));
2108 if (t->env->lexlevel == paramsScope && context_inFunctionLike ())
2110 noshadowerror = TRUE;
2111 usymtab_handleParams ();
2112 noshadowerror = FALSE;
2117 ** setup external references:
2118 ** o only, unique params alias external args
2119 ** o other params may alias anything of their type
2123 usymtab_handleParams (void)
2124 /*@globals utab, globtab, filetab@*/
2125 /*@modifies utab, globtab@*/
2127 usymtab ptab = utab->env;
2128 uentry fcn = context_getHeader ();
2130 usymtab_entries (ptab, param)
2134 if (!uentry_isYield (param))
2137 sRef pref = uentry_getSref (param);
2139 /* Could be a global. */
2141 if (uentry_isAnyParam (param))
2143 ue = uentry_makeVariable (fixParamName (uentry_rawName (param)),
2144 uentry_getType (param),
2145 fileloc_copy (uentry_whereDeclared (param)),
2148 uentry_copyState (ue, param);
2149 uentry_setRefParam (ue);
2151 ue = usymtab_supEntrySrefReturn (ue);
2153 /* must be after supercede! */
2155 if (!sRef_stateKnown (pref))
2157 uentry_setDefState (ue, SS_DEFINED);
2158 uentry_setDefState (param, SS_DEFINED);
2162 if (sRef_isStateSpecial (pref))
2164 uentry_setDefState (ue, SS_SPECIAL); /* ALLOCATED); */
2165 /* evans 2002-01-01: should be unnecessary, the pre clauses
2166 ** set the state if necessary.
2171 uentry_setDefState (ue, sRef_getDefState (pref));
2175 uref = uentry_getSref (ue);
2177 if (sRef_isStack (uref))
2179 alkind pkind = sRef_getAliasKind (pref);
2181 if (alkind_isKnown (pkind) && !alkind_isLocal (pkind)
2182 && !alkind_isStack (pkind))
2184 sRef_setAliasKind (uref, pkind, fileloc_undefined);
2185 sRef_setOrigAliasKind (uref, pkind);
2189 sRef_setAliasKind (uref, AK_IMPTEMP, fileloc_undefined);
2190 sRef_setOrigAliasKind (uref, AK_IMPTEMP);
2192 if (uentry_isOut (param))
2198 sRef_setDefined (uref, fileloc_undefined);
2204 usymtab_addMustAlias (uref, pref);
2206 if (!(uentry_isOnly (param) || uentry_isUnique (param)))
2209 ** This is needed for detecting possibly aliased parameters.
2212 sRef s = sRef_makeExternal (uref);
2213 usymtab_addMustAlias (uref, s);
2216 if (sRef_isKillRef (pref))
2218 sRef_setAliasKind (uref, AK_NEWREF, fileloc_undefined);
2219 sRef_setOrigAliasKind (uref, AK_KILLREF);
2221 else if (sRef_isRefCounted (uref))
2223 sRef_setOrigAliasKind (uref, AK_REFCOUNTED);
2228 sRef_setOrigAliasKind (uref, AK_LOCAL);
2235 } end_usymtab_entries;
2238 if (uentry_hasStateClauseList (fcn))
2240 stateClauseList clauses = uentry_getStateClauseList (fcn);
2242 stateClauseList_preElements (clauses, cl)
2244 fileloc loc = stateClause_loc (cl);
2245 sRefSet osrs = sRefSet_undefined;
2248 if (stateClause_isGlobal (cl))
2250 DPRINTF (("Global Marker: %s",
2251 sRef_unparseFull (usymtab_lookupGlobalMarker ())));
2252 llassert (sRef_isGlobalMarker (usymtab_lookupGlobalMarker ()));
2253 srs = sRefSet_single (usymtab_lookupGlobalMarker ());
2258 srs = stateClause_getRefs (cl);
2261 sRefSet_elements (srs, el)
2263 sRef base = sRef_getRootBase (el);
2264 sRef sb = sRef_updateSref (el);
2266 if (sRef_isResult (base))
2268 ; /* nothing to do before */
2270 else if (sRef_isParam (base) || sRef_isGlobalMarker (base))
2272 if (stateClause_setsMetaState (cl))
2274 /* copied from exprNode.c:3040 */
2275 qual ql = stateClause_getMetaQual (cl);
2276 annotationInfo ainfo = qual_getAnnotationInfo (ql);
2277 metaStateInfo minfo = annotationInfo_getState (ainfo);
2278 cstring key = metaStateInfo_getName (minfo);
2279 int mvalue = annotationInfo_getValue (ainfo);
2281 DPRINTF (("Sets meta state! %s", stateClause_unparse (cl)));
2283 if (sRef_isResult (base))
2289 sRef_setMetaStateValueComplete (sb, key, mvalue, loc);
2294 sRefMod modf = stateClause_getEntryFunction (cl);
2298 sRefSet aliases = usymtab_allAliases (sb);
2302 sRefSet_elements (aliases, sr)
2305 } end_sRefSet_elements ;
2307 sRefSet_free (aliases);
2313 if (sRef_isValid (base))
2315 DPRINTF (("Base: %s", sRef_unparseFull (base)));
2319 } end_sRefSet_elements ;
2320 } end_stateClauseList_preElements ;
2325 usymtab_enterFunctionScope (uentry fcn)
2326 /*@globals utab, filetab, globtab@*/
2329 usymtab t = usymtab_create (US_NORMAL, utab, TRUE);
2331 DPRINTF (("Enter function: %s", uentry_unparse (fcn)));
2333 if (utab->lexlevel != fileScope)
2335 if (utab->lexlevel > fileScope)
2337 llparseerror (cstring_makeLiteral ("New function scope inside function"));
2339 while (utab->lexlevel > fileScope)
2341 /*@i@*/ utab = usymtab_dropEnv (utab);
2348 llfatalbug (cstring_makeLiteral ("New function not inside file."));
2350 /*@-branchstate@*/ } /*@=branchstate@*/
2354 DPRINTF (("Globs: %s", globSet_unparse (uentry_getGlobs (fcn))));
2356 globSet_allElements (uentry_getGlobs (fcn), el)
2358 DPRINTF (("Here we go: %s", sRef_unparseFull (el)));
2360 if (sRef_isUndefGlob (el))
2362 int index = sRef_getScopeIndex (el);
2363 sRef sr = sRef_updateSref (el);
2364 fileloc loc = uentry_whereEarliest (fcn);
2366 DPRINTF (("update: %s", sRef_unparseFull (sr)));
2367 DPRINTF (("Undef!"));
2368 if (sRef_isFileStatic (el))
2370 ctype ct = sRef_getType (el);
2373 llassert (usymtab_isDefined (filetab));
2375 ue = usymtab_fetchIndex (filetab, index);
2377 if (ctype_isRealArray (ct) || ctype_isRealSU (ct))
2379 sRef_setAllocated (sr, loc);
2383 sRef_setUndefined (sr, loc);
2388 uentry ue = globtab->entries[index];
2389 ctype ct = uentry_getType (ue);
2391 if (ctype_isArray (ct) || ctype_isSU (ct))
2393 sRef_setAllocated (sr, loc);
2397 sRef_setUndefined (sr, loc);
2401 else if (sRef_isAllocated (el))
2403 sRef sr = sRef_updateSref (el);
2404 fileloc loc = uentry_whereEarliest (fcn);
2406 sRef_setAllocated (sr, loc);
2408 else if (sRef_isPartial (el))
2410 sRef sr = sRef_updateSref (el);
2411 fileloc loc = uentry_whereEarliest (fcn);
2413 sRef_setPartial (sr, loc);
2418 sRef sr = sRef_updateSref (el);
2419 fileloc loc = uentry_whereEarliest (fcn);
2421 sRef_setDefined (sr, loc);
2425 /* shouldn't need to do anything! */
2427 } end_globSet_allElements;
2429 DPRINTF (("Globs after: %s", globSet_unparse (uentry_getGlobs (fcn))));
2431 usymtab_checkAllValid ();
2436 usymtab_caseBranch (void)
2439 usymtab t = usymtab_create (US_CBRANCH, utab, FALSE);
2444 usymtab_switchBranch (/*@unused@*/ exprNode s)
2447 usymtab t = usymtab_create (US_SWITCH, utab, FALSE);
2449 t->aliases = aliasTable_copy (utab->aliases);
2454 usymtab_trueBranch (/*@only@*/ guardSet guards)
2457 usymtab t = usymtab_create (US_TBRANCH, utab, FALSE);
2460 ** not true! (could be in a macro)
2462 ** llassertprint (utab->lexlevel > paramsScope,
2463 ** ("not in scope: %s", usymtab_unparseLocal ()));
2467 guardSet_free (t->guards);
2470 aliasTable_free (t->aliases);
2471 t->aliases = aliasTable_copy (utab->aliases);
2479 ** { int a; if (...) a = 3; < a may be undefined here!
2484 usymtab_popTrueBranch (exprNode pred, exprNode expr, clause cl) /*@modifies utab@*/
2487 ** add a false branch
2488 ** (could be done more efficiently as a special case, but
2489 ** it is better to only maintain one version of the code)
2492 if (utab->kind != US_TBRANCH
2493 && context_inIterDef ())
2495 usymtab_exitScope (expr);
2499 DPRINTF (("pop true branch.."));
2500 usymtab_altBranch (guardSet_invert (exprNode_getGuards (pred)));
2501 usymtab_popBranches (pred, expr, exprNode_undefined, TRUE, cl);
2506 usymtab_popCaseBranch () /*@modifies utab@*/
2508 llassert (utab->kind == US_CBRANCH);
2509 usymtab_quietPlainExitScope ();
2513 usymtab_popTrueExecBranch (exprNode pred, exprNode expr, clause cl)
2516 ** add a false branch that must return --- that is,
2517 ** the true branch is always executed!
2520 usymtab_altBranch (guardSet_invert (exprNode_getGuards (pred)));
2521 usymtab_popBranches (pred, expr, exprNode_makeMustExit (), FALSE, cl);
2525 usymtab_popOrBranch (exprNode pred, exprNode expr)
2529 usymtab env = utab->env;
2530 usymtab otab = utab;
2533 llassert (env != NULL);
2535 if (exprNode_isError (expr))
2541 mustReturn = exprNode_mustEscape (expr);
2545 llassert (utab->kind == US_TBRANCH);
2548 ** merge each entry in table with its original
2549 ** unless execution cannot continue after this branch
2552 for (i = 0; i < utab->nentries; i++)
2554 uentry current = utab->entries[i];
2555 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
2557 uentry_mergeState (old, current, exprNode_loc (expr),
2558 mustReturn, FALSE, TRUE, ORCLAUSE);
2564 env->guards = guardSet_levelUnionFree (env->guards,
2565 guardSet_invert (exprNode_getGuards (pred)),
2570 env->aliases = aliasTable_levelUnion (env->aliases, otab->aliases, env->lexlevel);
2573 /* env is now utab */
2574 usymtab_quietPlainExitScope ();
2578 ** case syntax in C is very unrestricted. This is unfortunate.
2580 ** A switch case is ended either by a new case or default, or
2581 ** a close } that may close the switch or some other control
2586 usymtab_newCase (/*@unused@*/ exprNode pred, exprNode last)
2589 bool mustBreak = usymtab_mustBreak (utab);
2590 bool mustReturn = usymtab_mustEscape (utab);
2591 usymtab stab = utab;
2593 DPRINTF (("New case!"));
2596 ** Find last case (or outer switch)
2599 while (stab->kind != US_CBRANCH && stab->kind != US_SWITCH)
2602 llassert (stab != GLOBAL_ENV);
2605 while (stab->kind == US_CBRANCH)
2608 llassert (stab != GLOBAL_ENV);
2612 ** if its a fall through case, merge in outside entries and last case.
2617 ** case 1: x = 3; <fall through>
2618 ** case 2: << x may not be defined
2622 if (!mustBreak && !mustReturn && utab->kind == US_CBRANCH)
2624 llassert (stab->kind == US_SWITCH || stab->kind == US_NORMAL);
2626 usymtab_entries (utab, ue) /* but, keep track of used variables */
2628 uentry old = usymtab_lookupAux (stab, uentry_rawName (ue));
2630 llassert (uentry_isValid (old));
2632 /* modifies ue, not old */
2634 uentry_mergeState (ue, old, exprNode_loc (last),
2635 FALSE, FALSE, TRUE, CASECLAUSE);
2636 } end_usymtab_entries;
2638 utab->aliases = aliasTable_levelUnion (utab->aliases,
2639 stab->aliases, utab->lexlevel);
2642 ** No need for a new branch.
2649 usymtab_caseBranch ();
2650 /*@-mustfree@*/ /*< utab->aliases >*/
2651 utab->aliases = aliasTable_copy (stab->aliases);
2659 ** for && (both pred and expr are executed)
2663 usymtab_popAndBranch (exprNode pred, /*@unused@*/ exprNode expr)
2666 usymtab env = utab->env;
2670 llassert (utab->kind == US_TBRANCH);
2673 ** merge each entry in table with its original
2674 ** unless execution cannot continue after this branch
2677 for (i = 0; i < utab->nentries; i++)
2679 uentry current = utab->entries[i];
2680 sRef tref = uentry_getSref (current);
2681 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
2682 sRef oref = uentry_getSref (old);
2684 /* note that is current is in a nested branch,
2685 it may create a "new" old entry. */
2687 llassert (uentry_isValid (old));
2688 uentry_mergeState (old, current, exprNode_loc (expr),
2689 FALSE, FALSE, TRUE, ANDCLAUSE);
2692 ** if is it defined by the second clause, then it should be defined.
2695 if (sRef_isAnyDefined (tref)
2696 && (sRef_isAllocated (oref) || sRef_isStateUndefined (oref)))
2698 sRef_setDefined (oref, g_currentloc);
2702 utab->guards = guardSet_levelUnionFree (utab->guards,
2703 guardSet_invert (exprNode_getGuards (pred)),
2705 utab->aliases = aliasTable_levelUnion (utab->aliases, otab->aliases, utab->lexlevel);
2707 usymtab_quietPlainExitScope ();
2712 ** Stack should be [ US_CBRANCH+ US_SWITCH ]
2713 ** Only branches which do not return (except possibly the last branch) are included.
2715 ** Conditionally merge state from all CBRANCHes.
2717 ** If allpaths is TRUE, then all possible executions go through some switch
2718 ** case, and the original scope is not merged.
2722 usymtab_exitSwitch (/*@unused@*/ exprNode sw, bool allpaths)
2725 usymtab ttab = utab;
2726 usymtab stab = ttab;
2727 usymtab ltab = ttab;
2728 bool lastMustReturn = usymtab_mustEscape (utab);
2732 while (stab->kind == US_CBRANCH)
2735 llassert (stab != GLOBAL_ENV);
2738 while (stab->kind == US_NORMAL)
2741 llassert (stab != GLOBAL_ENV);
2744 llassert (stab->kind == US_SWITCH);
2746 /* go to the scope outside the switch (US_SWITCH is just a marker! */
2748 llassert (stab != GLOBAL_ENV);
2752 llassert (usymtab_isDefined (ttab));
2754 if (ttab->kind == US_CBRANCH)
2756 /* was quietPlainExitScope --- but, can't free it yet! */
2758 llassert (utab != GLOBAL_ENV);
2760 while (ttab->kind == US_CBRANCH)
2763 ** (from popTrueBranch)
2766 bool mustReturn = usymtab_mustEscape (ttab);
2767 bool mustBreak = usymtab_mustBreak (ttab);
2769 usymtab_entries (ttab, current)
2771 uentry old = /*@-compmempass@*/ usymtab_lookupAux (ltab, uentry_rawName (current));
2775 ** note that is this is in a nested branch,
2776 ** it may create a "new" old entry.
2779 if (uentry_isValid (old))
2783 uentry_mergeUses (current, old);
2784 uentry_setState (old, current);
2788 uentry_mergeState (old, current, exprNode_loc (sw),
2789 mustReturn, FALSE, TRUE, SWITCHCLAUSE);
2796 } end_usymtab_entries;
2799 ** if entry is not in symbol table for this case, merge with pre-switch
2803 if (!mustReturn && !mustBreak)
2805 usymtab_entries (stab, current)
2807 if (usymtab_getIndex (ttab, uentry_rawName (current)) == NOT_FOUND)
2809 uentry old = /*@-compmempass@*/
2810 usymtab_lookupAux (ltab, uentry_rawName (current));
2813 llassert (uentry_isValid (old));
2814 uentry_mergeState (old, current, exprNode_loc (sw),
2815 FALSE, FALSE, TRUE, SWITCHCLAUSE);
2817 } end_usymtab_entries;
2820 ltab->env = ttab->env;
2824 ** Suprious error, becuase of environments.
2827 /*@i1@*/ utab = ltab;
2829 lastMustReturn = FALSE;
2836 ** now, there is one US_CBRANCH. Merge this with the stab.
2840 for (i = 0; i < ltab->nentries; i++)
2842 uentry current = ltab->entries[i];
2843 uentry old = usymtab_lookupAux (stab, uentry_rawName (current));
2845 /* note that is this is in a nested branch,
2846 it may create a "new" old entry. */
2849 if (uentry_isValid (old))
2853 uentry_mergeUses (current, old);
2854 uentry_setState (old, current);
2858 uentry_mergeState (old, current, exprNode_loc (sw),
2859 FALSE, FALSE, TRUE, SWITCHCLAUSE);
2873 ** switch may or may not be followed by a new scope
2876 if (utab->kind == US_SWITCH)
2878 usymtab_quietPlainExitScope ();
2882 usymtab_quietPlainExitScope ();
2883 llassert (utab->kind == US_SWITCH);
2884 usymtab_quietPlainExitScope ();
2890 updateNullState (sRef el, /*@notnull@*/ usymtab ttab,
2891 /*@notnull@*/ usymtab ftab, bool trueGuard)
2893 sRef base = sRef_getRootBase (el);
2894 int level = sRef_lexLevel (base);
2896 if (sRef_isCvar (base))
2898 usymId index = sRef_getScopeIndex (base);
2899 uentry ue = usymtab_getRefTab (ttab, level, index);
2901 if (!uentry_isLset (ue))
2903 sRef sr = uentry_getSref (ue);
2907 sRef_setDerivNullState (sr, el, NS_NOTNULL);
2911 if (!guardSet_isGuarded (ttab->guards, el)
2912 && !sRef_isNotNull (sr))
2914 DPRINTF (("Here! %s / %s",
2915 sRef_unparseFull (sr),
2916 sRef_unparseFull (el)));
2917 sRef_setDerivNullState (sr, el, NS_DEFNULL);
2926 ue = usymtab_getRefTab (ftab, level, index);
2928 if (!uentry_isLset (ue))
2930 sRef sr = uentry_getSref (ue);
2932 if (!trueGuard) /* yikes! forgot the ! */
2934 sRef_setDerivNullState (sr, el, NS_NOTNULL);
2939 if (!guardSet_isGuarded (ftab->guards, el)
2940 && !sRef_isNotNull (sr))
2942 sRef_setDerivNullState (sr, el, NS_DEFNULL);
2954 usymtab_popBranches (exprNode pred, exprNode tbranch, exprNode fbranch,
2955 bool isOpt, clause cl)
2959 usymtab ftab = utab;
2960 usymtab ttab = utab->env;
2964 guardSet guards = exprNode_getGuards (pred);
2965 sRefSet tguards = guardSet_getTrueGuards (guards);
2966 sRefSet fguards = guardSet_getFalseGuards (guards);
2967 bool mustReturnT = exprNode_mustEscape (tbranch);
2968 bool mustReturnF = exprNode_mustEscape (fbranch);
2970 DPRINTF (("Pop branches: %s [mustreturn: %s/%s]", exprNode_unparse (pred),
2971 bool_unparse (mustReturnT),
2972 bool_unparse (mustReturnF)));
2974 if (exprNode_isDefined (fbranch))
2976 loc = exprNode_loc (fbranch);
2980 loc = exprNode_loc (tbranch);
2983 llassert (usymtab_isDefined (ttab));
2987 llassert (usymtab_isDefined (env));
2988 llassert (ftab->kind == US_FBRANCH);
2989 llassert (ttab->kind == US_TBRANCH);
2992 ** For each element that is true guarded (i.e., if (x != NULL))
2993 ** make x = null in false branch,
2994 ** and x = notnull in true branch.
2995 ** unless x was set locally in that branch.
2996 ** For each element that is false guarded (x == NULL)
2997 ** make x = null in true, notnull in false.
2999 ** For each element that is either guarded (pred(x))
3003 sRefSet_allElements (tguards, el)
3005 updateNullState (el, ttab, ftab, TRUE);
3006 } end_sRefSet_allElements;
3008 sRefSet_allElements (fguards, el)
3010 updateNullState (el, ttab, ftab, FALSE);
3011 } end_sRefSet_allElements;
3015 ** if an entry is in both true and false, merge the entries,
3016 ** then replace original with new state.
3018 ** if an entry is in one table, merge it with the original.
3021 DPRINTF (("ftab: %d", ftab->nentries));
3023 for (i = 0; i < ftab->nentries; i++)
3025 uentry fthis = ftab->entries[i];
3026 uentry old = usymtab_lookupAux (env, uentry_rawName (fthis));
3027 int tindex = usymtab_getIndex (ttab, uentry_rawName (fthis));
3029 DPRINTF (("Entry: %s / %s", uentry_unparseFull (fthis), uentry_unparseFull (old)));
3031 if (uentry_isUndefined (old))
3033 /* possible entry was added as an undefined id */
3034 DPRINTF (("Undefined! %s", uentry_rawName (fthis)));
3038 if (tindex != NOT_FOUND)
3040 uentry tthis = ttab->entries[tindex];
3042 /* note that is this is in a nested branch,
3043 it may create a "new" old entry. */
3049 uentry_mergeState (fthis, tthis, loc,
3050 mustReturnT, FALSE, FALSE, cl);
3054 uentry_mergeUses (fthis, tthis);
3057 uentry_setState (old, fthis);
3064 uentry_setState (old, tthis);
3065 uentry_mergeState (old, fthis, loc, mustReturnF,
3069 ttab->entries[tindex] = uentry_undefined;
3070 uentry_free (tthis);
3074 uentry_mergeState (old, fthis, loc, mustReturnF, TRUE, FALSE, cl);
3078 for (i = 0; i < ttab->nentries; i++)
3080 uentry current = ttab->entries[i];
3082 DPRINTF (("ttab: %s", uentry_unparseFull (current)));
3084 if (!uentry_isUndefined (current))
3086 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
3088 DPRINTF (("Old: %s", uentry_unparseFull (old)));
3090 if (uentry_isUndefined (old))
3092 llcontbug (message ("Undefined entry: %s", uentry_rawName (current)));
3098 uentry_mergeUses (current, old);
3099 uentry_setState (old, current);
3104 ** Assumes false branch is a fall-through if
3105 ** fbranch is not defined. This is true, unless
3106 ** where was some greivous error in processing
3107 ** the else branch of an if-then, in which case
3108 ** this is probably the right thing to do anyway.
3111 uentry_mergeState (old, current, loc, mustReturnT,
3115 DPRINTF (("==> %s", uentry_unparseFull (old)));
3120 ** Plain levelUnion doesn't work, since we need to use the sRef's in env->aliases
3121 ** if they are present.
3124 llassert (NOALIAS (env->aliases, ttab->aliases));
3125 llassert (NOALIAS (env->aliases, ftab->aliases));
3127 aliasTable_free (env->aliases);
3129 env->aliases = aliasTable_levelUnionNew (ttab->aliases,
3130 ftab->aliases, env->lexlevel);
3132 aliasTable_fixSrefs (env->aliases);
3134 DPRINTF (("Aliases: %s", aliasTable_unparse (env->aliases)));
3136 /* exit true and false scopes */
3137 usymtab_quietPlainExitScope ();
3138 usymtab_quietPlainExitScope ();
3142 utab->guards = guardSet_levelUnionFree
3144 guardSet_invert (exprNode_getGuards (pred)),
3150 utab->guards = guardSet_levelUnion (utab->guards,
3151 exprNode_getGuards (pred),
3155 DPRINTF (("Here."));
3158 static void usymtab_fixCases (void) /*@modifies utab@*/ {
3159 while (utab->kind == US_CBRANCH)
3161 usymtab_quietPlainExitScope ();
3164 llassert (utab->kind != US_CBRANCH);
3168 usymtab_altBranch (/*@only@*/ guardSet guards)
3172 usymtab parent = utab->env;
3174 t = usymtab_create (US_FBRANCH, utab, FALSE);
3177 ** If we are in a case, need to close it. The C syntax
3178 ** is very liberal, so this kludge is necessary.
3181 usymtab_fixCases ();
3183 DPRINTF (("Current kind: %s", usymtab_unparseStack ()));
3185 llassert (utab->kind == US_TBRANCH);
3186 llassert (parent != GLOBAL_ENV);
3188 guardSet_free (t->guards);
3191 aliasTable_free (t->aliases);
3192 t->aliases = aliasTable_copy (parent->aliases);
3198 usymtab_allDefined (void)
3199 /*@globals utab, globtab@*/
3203 llassert (utab == globtab);
3205 for (i = 0; i < utab->nentries; i++)
3207 uentry e = utab->entries[i];
3209 if (uentry_isPriv (e))
3211 ; /* no need to define it */
3215 if (context_getFlag (FLG_SPECUNDECL))
3217 fileloc sloc = uentry_whereSpecified (e);
3218 fileloc dloc = uentry_whereDeclared (e);
3220 if (fileloc_isDefined (sloc)
3221 && !uentry_isFakeTag (e)
3222 && !fileloc_isDefined (dloc))
3226 message ("%s %q specified but not declared",
3227 ekind_capName (uentry_getKind (e)),
3228 uentry_getName (e)),
3233 if (!uentry_isCodeDefined (e))
3235 fileloc dloc = uentry_whereDeclared (e);
3237 if (fileloc_isLib (dloc) || fileloc_isXHFile (dloc))
3241 else if (fileloc_isDefined (dloc))
3243 if (!uentry_isAnyTag (e))
3245 if (fileloc_isUser (dloc))
3249 message ("%s %q declared but not defined",
3250 ekind_capName (uentry_getKind (e)),
3251 uentry_getName (e)),
3253 DPRINTF (("decl: %s", uentry_unparseFull (e)));
3259 fileloc sloc = uentry_whereSpecified (e);
3261 if (fileloc_isDefined (sloc)
3262 && !fileloc_isImport (sloc)
3263 && !fileloc_isLib (sloc)
3264 && !fileloc_isPreproc (sloc)
3265 && !uentry_isFakeTag (e))
3267 if (uentry_isVariable (e) || uentry_isFunction (e))
3271 message ("%s %q specified but not declared or defined",
3272 ekind_capName (uentry_getKind (e)),
3273 uentry_getName (e)),
3280 message ("%s %q specified but not defined",
3281 ekind_capName (uentry_getKind (e)),
3282 uentry_getName (e)),
3292 void usymtab_exportHeader (void)
3297 for (i = 0; i < utab->nentries; i++)
3299 uentry ce = utab->entries[i];
3301 if (!uentry_isDatatype (ce)
3302 && !uentry_isAnyTag (ce)
3303 && !uentry_isEitherConstant (ce) && !uentry_isStatic (ce)
3304 && !uentry_isExternal (ce)
3305 && !uentry_isForward (ce))
3307 fileloc fwhere = uentry_whereDeclared (ce);
3309 if (fileloc_isUndefined (fwhere)
3310 && uentry_isFunction (ce))
3312 fwhere = uentry_whereDefined (ce);
3315 if (fileloc_isDefined (fwhere)
3316 && !fileloc_isHeader (fwhere)
3317 && !fileloc_isXHFile (fwhere)
3318 && !(fileloc_isSpecialFile (fwhere)
3319 && !context_getFlag (FLG_UNUSEDSPECIAL)))
3321 if (uentry_isVariable (ce))
3324 (FLG_EXPORTHEADERVAR,
3325 message ("%s %q exported but not declared in header file",
3326 ekind_capName (uentry_getKind (ce)),
3327 uentry_getName (ce)),
3330 uentry_showDefSpecInfo (ce, fwhere);
3335 if (!uentry_isIter (ce)
3336 && !uentry_isEndIter (ce)
3337 && !uentry_isExpandedMacro (ce))
3339 if (uentry_isFunction (ce)
3340 && cstring_equalLit (uentry_rawName (ce), "main"))
3342 ; /* no error for main */
3348 message ("%s %q exported but not declared "
3350 ekind_capName (uentry_getKind (ce)),
3351 uentry_getName (ce)),
3354 uentry_showDefSpecInfo (ce, fwhere);
3364 void usymtab_exportLocal (void)
3369 for (i = 0; i < utab->nentries; i++)
3371 uentry ce = utab->entries[i];
3373 if (!uentry_isDatatype (ce) && !uentry_isAnyTag (ce)
3374 && !uentry_isEitherConstant (ce)
3375 && !uentry_isIter (ce)
3376 && !uentry_isEndIter (ce)
3377 && !uentry_isExpandedMacro (ce)
3378 && uentry_isUsed (ce))
3380 /* check static uses */
3381 filelocList fuses = uentry_getUses (ce);
3382 fileloc mod = uentry_whereDefined (ce);
3383 bool ok = filelocList_isEmpty (fuses);
3384 fileloc fwhere = uentry_whereDeclared (ce);
3386 if (fileloc_isSpecialFile (fwhere)
3387 && !context_getFlag (FLG_UNUSEDSPECIAL))
3389 ok = TRUE; /* no errors for special files */
3393 filelocList_elements (fuses, uloc)
3395 if (fileloc_isUndefined (uloc) || !fileloc_sameModule (uloc, mod))
3398 /*@innerbreak@*/ break;
3400 } end_filelocList_elements;
3407 message ("%s exported but not used outside %s: %q",
3408 ekind_capName (uentry_getKind (ce)),
3409 fileloc_getBase (mod),
3410 uentry_getName (ce)),
3413 uentry_showDefSpecInfo (ce, fwhere);
3421 usymtab_allUsed (void)
3425 bool isFileStatic = usymtab_inFileScope ();
3426 cstring last_file = cstring_undefined;
3428 for (i = 0; i < utab->nentries; i++)
3430 bool hasError = FALSE;
3431 uentry ce = utab->entries[i];
3432 fileloc fwhere = uentry_whereDeclared (ce);
3434 if (fileloc_isUndefined (fwhere))
3436 fwhere = uentry_whereDefined (ce);
3439 if (fileloc_isInvalid (fwhere)
3440 || fileloc_isLib (fwhere)
3441 || fileloc_isBuiltin (fwhere)
3442 || ((fileloc_isSpecialFile (fwhere)
3443 || fileloc_isSpecialFile (uentry_whereDefined (ce)))
3444 && !context_getFlag (FLG_UNUSEDSPECIAL)))
3448 else if (!uentry_wasUsed (ce) && !uentry_isAnyTag (ce))
3450 cstring fname = fileloc_filename (fwhere);
3452 if (cstring_isUndefined (last_file))
3456 else if (cstring_equal (fname, last_file))
3465 if (uentry_isParam (ce))
3467 if (context_inMacro ())
3469 sRef cref = uentry_getSref (ce);
3471 if (uentry_isYield (ce))
3473 ; /* no checks (for now) */
3475 else if (sRef_isSafe (cref))
3481 if (uentry_hasRealName (ce))
3484 optgenerror (FLG_MACROPARAMS,
3485 message ("Macro parameter %q not used",
3486 uentry_getName (ce)),
3493 if (cstring_equalFree (uentry_getName (ce),
3494 cstring_makeLiteral ("...")))
3500 hasError = optgenerror (FLG_PARAMUNUSED,
3501 message ("Parameter %q not used",
3502 uentry_getName (ce)),
3507 else if (uentry_isFunction (ce) || uentry_isIter (ce))
3509 if (fileloc_isUser (fwhere))
3511 hasError = optgenerror
3513 message ("%q %q declared but not used",
3515 (uentry_isIter (ce) ? "Iterator"
3516 : (isFileStatic ? "File static function" : "Function")),
3517 uentry_getName (ce)),
3521 else if (uentry_isEndIter (ce))
3523 ; /* no error (already reported for iter */
3525 else if (uentry_isEnumConstant (ce))
3527 if (fileloc_isUser (fwhere))
3529 hasError = optgenerror
3531 message ("Enum member %q not used",
3532 uentry_getName (ce)),
3536 else if (uentry_isConstant (ce))
3538 if (fileloc_isUser (fwhere))
3540 hasError = optgenerror
3542 message ("Constant %q declared but not used",
3543 uentry_getName (ce)),
3547 else if (uentry_isDatatype (ce))
3549 if (fileloc_isUser (fwhere))
3551 hasError = optgenerror
3553 message ("Type %q declared but not used",
3554 uentry_getName (ce)),
3558 else if (!uentry_isRefParam (ce) && !uentry_isExpandedMacro (ce))
3559 { /* errors for ref params will be reported in the next scope */
3560 llassertprint (uentry_isVar (ce),
3561 ("ce: %s", uentry_unparseFull (ce)));
3563 if (ctype_isFunction (uentry_getType (ce)))
3565 if (fileloc_isUser (fwhere))
3567 hasError = optgenerror
3569 message ("%q %q declared but not used",
3571 (isFileStatic ? "File static function"
3573 uentry_getName (ce)),
3579 if (fileloc_isUser (fwhere))
3583 hasError = optgenerror
3585 message ("%q %q declared but not used",
3587 (isFileStatic ? "File static variable"
3589 uentry_getName (ce)),
3599 else if ((uentry_isDatatype (ce) || uentry_isAnyTag (ce)))
3600 { /* check all fields */
3601 ctype ct = uentry_getRealType (ce);
3604 while (ctype_isAP (ct))
3606 ct = ctype_getBaseType (ct);
3609 if (ctype_isSU (ct))
3611 uentryList fields = ctype_getFields (ct);
3613 uentryList_elements (fields, field)
3615 if (!uentry_isUsed (field))
3617 if (uentry_hasName (ce))
3619 hasError |= optgenerror
3621 message ("Field %q of %s %q declared but not used",
3622 uentry_getName (field),
3623 cstring_makeLiteralTemp
3624 (ctype_isStruct (ct) ? "structure" : "union"),
3625 uentry_getName (ce)),
3626 uentry_whereEarliest (field));
3632 ** Can't report these errors for unnamed structs.
3633 ** No way to tell when there are multiple consistent
3634 ** unnamed structure types. (Could go through table
3635 ** and mark them all unused...)
3637 hasError |= optgenerror
3639 message ("Field %q of unnamed %s declared but not used",
3640 uentry_getName (field),
3641 cstring_makeLiteralTemp
3642 (ctype_isStruct (ct) ? "structure" : "union")),
3643 uentry_whereEarliest (field));
3648 uentry_setUsed (field, fileloc_undefined);
3650 } end_uentryList_elements;
3660 if (uentry_isParam (ce) && context_inMacro ())
3662 if (fileloc_isDefined (uentry_whereSpecified (ce)))
3664 uentry_showWhereSpecified (ce);
3669 uentry_showDefSpecInfo (ce, fwhere);
3672 uentry_setUsed (ce, fileloc_undefined);
3678 checkGlobalReturn (uentry glob, sRef orig)
3680 sRef sr = uentry_getSref (glob);
3682 DPRINTF (("Check global return: %s / orig: %s / sr: %s",
3683 uentry_unparseFull (glob),
3684 sRef_unparseFull (orig),
3685 sRef_unparseFull (sr)));
3687 DPRINTF (("Is killed: %s", bool_unparse (sRef_isKilledGlob (orig))));
3689 if (context_getFlag (FLG_GLOBSTATE))
3691 DPRINTF (("Is killed: %s", sRef_unparseFull (orig)));
3693 if (sRef_isKilledGlob (orig))
3695 if (sRef_isStateUndefined (sr)
3696 || sRef_isUnuseable (sr)
3697 || sRef_isStateUnknown (sr)
3698 || sRef_isDead (sr))
3704 ctype ct = ctype_realType (uentry_getType (glob));
3706 DPRINTF (("Check global destroyed: %s", uentry_unparseFull (glob)));
3708 if (ctype_isVisiblySharable (ct))
3713 ("Killed global %q (type %s) not released before return",
3714 uentry_getName (glob),
3715 ctype_unparse (ct)),
3718 sRef_showStateInfo (sr);
3723 sRef_protectDerivs ();
3724 (void) transferChecks_globalDestroyed (sr, g_currentloc);
3725 sRef_clearProtectDerivs ();
3731 if (sRef_isStateUndefined (sr))
3733 if (optgenerror (FLG_GLOBSTATE,
3735 ("Function returns with global %q undefined",
3736 uentry_getName (glob)),
3739 sRef_showStateInfo (sr);
3744 if (sRef_isDead (sr) || sRef_isKept (sr))
3748 message ("Function returns with global %q "
3749 "referencing %s storage",
3750 uentry_getName (glob),
3751 cstring_makeLiteralTemp (sRef_isDead (sr) ? "released" : "kept")),
3754 if (sRef_isKept (sr))
3756 sRef_showAliasInfo (sr);
3760 sRef_showStateInfo (sr);
3763 sRef_setDefState (sr, SS_UNKNOWN, fileloc_undefined);
3767 if (ctype_isRealPointer (uentry_getType (glob))
3768 && sRef_possiblyNull (sr)
3769 && !uentry_possiblyNull (glob))
3773 message ("Function returns with non-null global %q "
3774 "referencing null storage",
3775 uentry_getName (glob)),
3778 sRef_showNullInfo (sr);
3783 DPRINTF (("Check transfer: %s", uentry_unparseFull (glob)));
3784 transferChecks_globalReturn (glob);
3792 ** remember: check alias globals
3795 void usymtab_checkFinalScope (bool isReturn)
3798 bool mustFree = context_getFlag (FLG_MUSTFREEONLY) || context_getFlag (FLG_MUSTFREEFRESH); /*@i423 remove this mustFree */
3799 bool mustDefine = context_getFlag (FLG_MUSTDEFINE);
3800 /* bool mustNotAlias = context_getFlag (FLG_MUSTNOTALIAS); */
3801 sRefSet checked = sRefSet_new ();
3802 usymtab stab = utab;
3806 ** need to check all scopes out to function parameters.
3811 for (i = 0; i < stab->nentries; i++)
3813 uentry ce = stab->entries[i];
3814 sRef sr = uentry_getSref (ce);
3815 sRef rb = sRef_getRootBase (sr);
3819 ** Shouldn't check if shadow checked in deeper scope:
3824 uentry oue = usymtab_lookupQuietNoAlt (utab, uentry_observeRealName (ce));
3826 if (!uentry_sameObject (ce, oue))
3828 DPRINTF (("Skipping outer entry: %s / %s", uentry_unparseFull (ce),
3829 uentry_unparseFull (oue)));
3830 /*@i32 what if it is one an alternate branch? */
3831 /*@innercontinue@*/ continue;
3835 DPRINTF (("Here check final scope: %s", uentry_unparseFull (ce)));
3837 if (ctype_isFunction (uentry_getType (ce)))
3839 /*@innercontinue@*/ continue;
3842 if (uentry_isAnyParam (ce)
3843 || uentry_isRefParam (ce)
3844 || sRef_isFileOrGlobalScope (rb))
3846 /* Don't do the loseref check...but should check state! */
3847 DPRINTF (("Skipping check 1"));
3849 else if (sRef_isDefinitelyNull (sr)
3850 || usymtab_isDefinitelyNull (sr))
3853 ** No state reference errors for definitely null references.
3856 DPRINTF (("Skipping check 2"));
3860 DPRINTF (("Lose ref: %s / %s", uentry_unparse (ce),
3861 sRef_unparseFull (sr)));
3863 tvalues = sRef_getValueTable (sr);
3865 valueTable_elements (tvalues, fkey, fval) {
3866 metaStateInfo minfo;
3867 cstring msg = cstring_undefined;
3870 minfo = context_lookupMetaStateInfo (fkey);
3871 llassert (metaStateInfo_isDefined (minfo));
3873 if (stateValue_isError (fval)
3874 || sRef_isStateUndefined (sr)) /* No errors for undefined state */
3876 DPRINTF (("Skipping check 3"));
3880 DPRINTF (("Check: %s / %s / %s", fkey,
3881 metaStateInfo_unparse (minfo),
3882 stateValue_unparse (fval)));
3884 minfo = context_lookupMetaStateInfo (fkey);
3886 nval = stateCombinationTable_lookupLoseReference
3887 (metaStateInfo_getTransferTable (minfo),
3888 stateValue_getValue (fval), &msg);
3890 if (cstring_isDefined (msg))
3892 /*@i32 print extra info for assignments@*/
3893 DPRINTF (("From: %s", sRef_unparseFull (sr)));
3894 DPRINTF (("Null? %s / %s",
3895 bool_unparse (sRef_isDefinitelyNull (sr)),
3896 bool_unparse (usymtab_isGuarded (sr))));
3901 ("%s loses reference %q in invalid state %q (%s)",
3902 cstring_makeLiteralTemp (isReturn ? "Return" : "Scope exit"),
3903 uentry_getName (ce),
3904 stateValue_unparseValue (fval, minfo),
3908 stateValue_show (fval, minfo);
3912 DPRINTF (("Suppressed transfer error: %s", msg));
3916 } end_valueTable_elements;
3919 DPRINTF (("Here 1"));
3923 DPRINTF (("Check mustfree entry: %s", uentry_unparseFull (ce)));
3925 if (!sRefSet_member (checked, sr) && !sRef_isFileOrGlobalScope (rb))
3927 if (ctype_isRealSU (uentry_getType (ce))
3928 && !uentry_isAnyParam (ce)
3929 && !uentry_isRefParam (ce)
3930 && !uentry_isStatic (ce)
3931 && !sRef_isDependent (sr)
3932 && !sRef_isOwned (sr))
3934 sRefSet als = usymtab_allAliases (sr);
3936 if (sRefSet_isEmpty (als))
3938 transferChecks_localDestroyed (sr, g_currentloc);
3942 /* aliased, no problem */ ;
3948 (!uentry_isStatic (ce)
3949 && ((sRef_isNewRef (sr))
3950 || (((sRef_isOnly (sr) || sRef_isFresh (sr)
3951 || sRef_isKeep (sr) || sRef_isOwned (sr))
3952 && !sRef_isDead (sr))
3953 && (!sRef_definitelyNull (sr))
3954 && (!usymtab_isDefinitelyNull (sr)))))
3956 bool hasError = TRUE;
3958 DPRINTF (("Checking: %s", sRef_unparseFull (sr)));
3961 ** If its a scope exit, check if there is an alias.
3962 ** If so, make it only. If not, there is an error.
3967 if (transferChecks_canLoseReference (sr, g_currentloc))
3969 DPRINTF (("Can lose!"));
3976 if (sRef_hasLastReference (sr))
3978 sRef ar = sRef_getAliasInfoRef (sr);
3981 (sRef_isFresh (ar) ? FLG_MUSTFREEFRESH : FLG_MUSTFREEONLY,
3983 ("Last reference %q to %s storage %qnot %q before %q",
3985 alkind_unparse (sRef_getAliasKind (sr)),
3986 sRef_unparseOpt (ar),
3987 cstring_makeLiteral (sRef_isKeep (sr)
3988 ? "transferred" : "released"),
3989 cstring_makeLiteral (isReturn
3990 ? "return" : "scope exit")),
3993 sRef_showRefLost (sr);
3996 else if (sRef_isNewRef (sr))
3999 (sRef_isFresh (sr) ? FLG_MUSTFREEFRESH : FLG_MUSTFREEONLY,
4001 ("%q %q not released before %q",
4003 (alkind_isKillRef (sRef_getOrigAliasKind (sr))
4004 ? "Kill reference parameter" : "New reference"),
4005 uentry_getName (ce),
4006 cstring_makeLiteral (isReturn
4007 ? "return" : "scope exit")),
4010 sRef_showAliasInfo (sr);
4015 if (ctype_isRealSU (sRef_getType (sr)))
4017 transferChecks_structDestroyed (sr, g_currentloc);
4021 DPRINTF (("Here we are: %s", sRef_unparseFull (sr)));
4024 (sRef_isFresh (sr) ? FLG_MUSTFREEFRESH : FLG_MUSTFREEONLY,
4026 ("%s storage %q not %q before %q",
4027 alkind_capName (sRef_getAliasKind (sr)),
4028 uentry_getName (ce),
4029 cstring_makeLiteral (sRef_isKeep (sr)
4030 ? "transferred" : "released"),
4031 cstring_makeLiteral (isReturn
4032 ? "return" : "scope exit")),
4035 sRef_showAliasInfo (sr);
4036 DPRINTF (("Storage: %s", sRef_unparseFull (sr)));
4049 if (mustDefine && uentry_isOut (ce))
4051 /* No error if its dead (either only or error already reported */
4052 if (!sRef_isReallyDefined (sr) && !sRef_isDead (sr))
4056 message ("Out storage %q not defined before %q",
4057 uentry_getName (ce),
4059 (isReturn ? "return" : "scope exit")),
4062 DPRINTF (("sr: %s", sRef_unparseFull (sr)));
4067 ** also check state is okay
4070 if (usymtab_lexicalLevel () > functionScope
4071 && uentry_isVariable (ce)
4072 && (sRef_isLocalVar (sr)
4073 && (sRef_isDependent (sr) || sRef_isLocalState (sr))))
4075 sRefSet ab = usymtab_aliasedBy (sr);
4077 /* should do something more efficient here */
4079 if (sRefSet_isEmpty (ab))
4081 /* and no local ref */
4082 DPRINTF (("Check lose ref: %s", uentry_unparseFull (ce)));
4083 transferChecks_loseReference (ce);
4097 checked = sRefSet_insert (checked, sr);
4100 llassert (usymtab_isDefined (stab->env));
4102 if (usymtab_isBranch (stab))
4104 stab = usymtab_dropEnv (stab);
4111 llassert (stab != usymtab_undefined);
4112 } while (isReturn && (stab->lexlevel >= paramsScope));
4114 sRefSet_free (checked);
4118 ** all globals are appropriately defined
4119 ** all parameters are appropriately defined
4120 ** special clauses are followed
4123 if (isReturn || (utab->lexlevel == paramsScope))
4125 uentry fcn = context_getHeader ();
4126 uentryList params = context_getParams ();
4127 globSet uglobs = context_getUsedGlobs ();
4128 globSet sglobs = context_getGlobs ();
4130 if (isReturn && context_maybeSet (FLG_GLOBALIAS))
4132 aliasTable_checkGlobs (utab->aliases);
4136 ** state clauses (ensures, defines, sets, allocates, releases)
4139 if (uentry_hasStateClauseList (fcn))
4141 stateClauseList clauses = uentry_getStateClauseList (fcn);
4143 stateClauseList_elements (clauses, cl)
4145 if (stateClause_isAfter (cl) && !stateClause_isGlobal (cl))
4147 if (stateClause_setsMetaState (cl))
4149 sRefSet rfs = stateClause_getRefs (cl);
4150 qual q = stateClause_getMetaQual (cl);
4151 annotationInfo ainfo = qual_getAnnotationInfo (q);
4152 metaStateInfo minfo = annotationInfo_getState (ainfo);
4153 cstring key = metaStateInfo_getName (minfo);
4154 int mvalue = annotationInfo_getValue (ainfo);
4156 DPRINTF (("Post meta state clause: %s", stateClause_unparse (cl)));
4158 sRefSet_elements (rfs, el)
4160 sRef base = sRef_getRootBase (el);
4162 if (sRef_isResult (base))
4165 ** This is checked for return transfers.
4169 else if (sRef_isParam (base) || sRef_isGlobalMarker (base))
4171 sRef sr = sRef_updateSref (base);
4172 sr = sRef_fixBase (el, sr);
4174 if (!sRef_checkMetaStateValue (sr, key, mvalue))
4179 ("Ensures clause not satisfied%q (state is %q): %q",
4180 sRef_isGlobalMarker (sr)
4182 : message (" by %q", sRef_unparse (sr)),
4183 stateValue_unparseValue (sRef_getMetaStateValue (sr, key),
4185 stateClause_unparse (cl)),
4188 sRef_showMetaStateInfo (sr, key);
4194 if (sRef_isMeaningful (el))
4199 } end_sRefSet_elements ;
4203 /* evs - 2000 07 10 - added this */
4204 sRefTest tst = stateClause_getPostTestFunction (cl);
4205 sRefSet rfs = stateClause_getRefs (cl);
4207 sRefSet_elements (rfs, el)
4209 sRef base = sRef_getRootBase (el);
4211 if (sRef_isResult (base))
4214 ** This is checked for return transfers.
4219 else if (sRef_isParam (base))
4221 sRef sr = sRef_updateSref (base);
4222 sr = sRef_fixBase (el, sr);
4224 if (tst != NULL && !tst (sr))
4227 (stateClause_postErrorCode (cl),
4228 message ("%s storage %qcorresponds to "
4229 "storage listed in %q clause",
4230 stateClause_postErrorString (cl, sr),
4231 sRef_unparseOpt (sr),
4232 stateClause_unparseKind (cl)),
4235 sRefShower ss = stateClause_getPostTestShower (cl);
4242 DPRINTF (("Storage: %s", sRef_unparseFull (sr)));
4248 if (sRef_isMeaningful (el))
4253 } end_sRefSet_elements ;
4256 } end_stateClauseList_elements ;
4260 ** check parameters on return
4263 uentryList_elements (params, arg)
4265 if (!uentry_isElipsisMarker (arg))
4267 ctype rt = ctype_realType (uentry_getType (arg));
4269 if (ctype_isMutable (rt) || ctype_isSU (rt))
4271 uentry param = usymtab_lookupQuiet (utab, uentry_rawName (arg));
4272 DPRINTF (("Check param return: %s", uentry_unparseFull (param)));
4273 transferChecks_paramReturn (param);
4276 } end_uentryList_elements;
4278 DPRINTF (("Check global return: %s",
4279 globSet_unparse (sglobs)));
4281 globSet_allElements (sglobs, el)
4283 sRef orig = el; /*! sRef_updateSref (el); */ /*!!!*/
4284 uentry current = sRef_getUentry (el);
4286 DPRINTF (("Check global return: %s / %s", sRef_unparseFull (el),
4287 uentry_unparseFull (current)));
4289 if (uentry_isVariable (current) && !uentry_isRealFunction (current))
4291 checkGlobalReturn (current, orig);
4293 } end_globSet_allElements;
4295 globSet_allElements (uglobs, el)
4297 if (!globSet_member (sglobs, el))
4299 uentry current = sRef_getUentry (el);
4301 if (uentry_isVariable (current)
4302 && !uentry_isRealFunction (current))
4304 checkGlobalReturn (current, sRef_undefined);
4307 } end_globSet_allElements;
4312 usymtab_quietExitScope (fileloc loc)
4313 /*@globals utab, globtab, filetab; @*/
4316 usymtab t = utab->env;
4318 if (utab->reftable != NULL)
4322 for (i = 0; i < utab->nentries; i++)
4324 uentry current = utab->entries[i];
4325 uentry old = usymtab_lookupAux (t, uentry_rawName (current));
4327 uentry_mergeState (old, current, loc, FALSE, FALSE, FALSE, NOCLAUSE);
4331 llassert (t != NULL);
4333 if (t->lexlevel > paramsScope)
4335 t->guards = guardSet_levelUnion (t->guards, utab->guards, t->lexlevel);
4336 t->aliases = aliasTable_levelUnionSeq (t->aliases, utab->aliases,
4338 utab->aliases = aliasTable_undefined;
4341 t->mustBreak = utab->mustBreak;
4342 t->exitCode = utab->exitCode;
4344 usymtab_freeLevel (utab);
4349 usymtab_checkAllValid ();
4354 ** Exit a scope with no checking, lose alias states.
4355 ** (When should this be used?)
4358 void usymtab_quietPlainExitScope (void)
4359 /*@globals utab, globtab, filetab@*/
4362 usymtab t = utab->env;
4364 llassert (t != NULL);
4365 llassert (NOALIAS (utab->aliases, t->aliases));
4366 usymtab_freeLevel (utab);
4370 void usymtab_exitScope (exprNode expr)
4371 /*@globals utab, filetab, globtab@*/
4372 /*@modifies utab, globtab@*/
4374 usymtab ctab = usymtab_undefined;
4375 usymtab lctab = usymtab_undefined;
4376 bool mustReturn = exprNode_mustEscape (expr);
4378 DPRINTF (("Exit scope"));
4380 if (utab->kind == US_CBRANCH)
4383 ** save the case branches, remove the first non-cbranch
4388 while (utab->kind == US_CBRANCH)
4392 llassert (utab != GLOBAL_ENV);
4396 if (utab->kind == US_TBRANCH || utab->kind == US_FBRANCH
4397 || utab->kind == US_CBRANCH || utab->kind == US_SWITCH)
4399 if (context_inMacro ())
4401 /* evs 2000-07-25 */
4402 /* Unparseable macro may end inside nested scope. Deal with it. */
4404 llerror (FLG_SYNTAX,
4405 message ("Problem parsing macro body of %s (unbalanced scopes). "
4406 "Attempting to recover, recommend /*@notfunction@*/ before "
4407 "macro definition.",
4408 context_inFunctionName ()));
4410 while (utab->kind == US_TBRANCH
4411 || utab->kind == US_FBRANCH
4412 || utab->kind == US_CBRANCH
4413 || utab->kind == US_SWITCH)
4416 llassert (utab != GLOBAL_ENV);
4420 llcontbug (message ("exitScope: in branch: %q", usymtab_unparseStack ()));
4422 } /*@=branchstate@*/
4426 ** check all variables in scope were used
4430 ** bogus errors if this is the normal inside a switch,
4431 ** since cases have not been merged yet. Should probably
4432 ** still check this, but I'm too lazy at the moment...
4435 llassertfatal (utab->env != GLOBAL_ENV);
4437 if (utab->env->kind != US_SWITCH)
4443 ** check aliasing: all only params are released (dead)
4444 ** definition: all out params are defined, all modified params
4445 ** are completely defined
4447 ** NOTE: note for exiting paramsScope, since checkReturn should be
4451 if (!mustReturn && (usymtab_lexicalLevel () > functionScope))
4454 ** should only call this is end of scope is reachable...
4457 usymtab_checkFinalScope (FALSE);
4460 if (usymtab_lexicalLevel () == paramsScope && context_inFunctionLike ())
4463 ** leaving a function, need to fix up globals
4466 uentryList params = context_getParams ();
4467 globSet globs = context_getUsedGlobs ();
4469 uentryList_elements (params, ue)
4471 uentry_fixupSref (ue);
4472 } end_uentryList_elements;
4474 clearFunctionTypes ();
4476 DPRINTF (("Fixing up globals: %s", globSet_unparse (globs)));
4478 globSet_allElements (globs, el)
4480 DPRINTF (("Fix: %s", sRef_unparseDebug (el)));
4482 if (sRef_isCvar (el))
4485 int index = sRef_getScopeIndex (el);
4487 if (sRef_isFileStatic (el))
4489 llassert (usymtab_isDefined (filetab));
4490 current = usymtab_fetchIndex (filetab, index);
4494 current = usymtab_fetchIndex (globtab, index);
4497 if (uentry_isVariable (current))
4499 DPRINTF (("Fixup: %s", uentry_unparse (current)));
4500 uentry_fixupSref (current);
4504 DPRINTF (("Clear: %s", uentry_getSref (current)));
4505 sRef_clearDerived (uentry_getSref (current));
4509 sRef_clearDerived (el); /* evans 2002-03-14 - this is the likely source of many crashes! */
4510 } end_globSet_allElements;
4513 usymtab_quietExitScope (exprNode_loc (expr));
4515 if (lctab != usymtab_undefined)
4517 /*@i@*/ lctab->env = utab;
4518 /*@i@*/ utab = ctab;
4519 /*@-branchstate@*/ } /*@=branchstate@*/
4524 usymtab_checkAllValid ();
4530 ** yikes! don't let the '170 kids see this one...
4534 uentry_directParamNo (uentry ue)
4536 if (uentry_isVar (ue))
4538 sRef sr = uentry_getSref (ue);
4540 if (sRef_lexLevel (sr) == functionScope)
4542 int index = sRef_getScopeIndex (sr);
4544 if (index < uentryList_size (context_getParams ()))
4553 /*@dependent@*/ /*@exposed@*/ uentry
4554 usymtab_getParam (int paramno)
4558 ** requires in a function context (checked)
4560 ** depends on no nested functions --- the function
4561 ** parameters are ALWAYS one scope inside the global scope
4562 ** and entered in order!
4566 if (!context_inFunctionLike ())
4567 llfatalbug (message ("usymtab_getParam: not in function context: %q",
4568 context_unparse ()));
4570 while (s->lexlevel > paramsScope)
4575 llassert (usymtab_isDefined (s));
4577 if (paramno >= s->nentries)
4580 ** Parse errors lead to this.
4583 uentry err = uentry_makeVariableLoc (cstring_makeLiteralTemp ("<error>"),
4586 uentry_markOwned (err);
4590 return (s->entries[paramno]);
4593 static /*@dependent@*/ /*@exposed@*/ uentry
4594 usymtab_getRefTab (/*@notnull@*/ usymtab u, int level, usymId index)
4598 ue = usymtab_getRefNoisy (u, level, index);
4600 if (uentry_isUndefined (ue))
4602 llbug (message ("usymtab_getRef: out of range: %d. level = %d",
4609 static /*@dependent@*/ /*@exposed@*/ usymtab
4610 usymtab_dropEnv (/*@notnull@*/ usymtab s)
4612 if (s->kind == US_CBRANCH)
4620 llassert (s != GLOBAL_ENV);
4621 } while (s->kind == US_CBRANCH);
4622 /* drop all cases (except in nested scopes */
4625 llassert (s != GLOBAL_ENV);
4628 if (s->kind == US_FBRANCH)
4630 s = s->env; /* skip the true branch */
4631 llassert (usymtab_isDefined (s));
4632 llassert (s->kind == US_TBRANCH);
4635 llassert (s != GLOBAL_ENV);
4641 /*@dependent@*/ /*@exposed@*/ uentry
4642 usymtab_getRefQuiet (int level, usymId index)
4648 llassert (s != NULL);
4649 llassert (index >= 0);
4651 if (level > s->lexlevel)
4653 return uentry_undefined;
4656 llassertprint (level <= s->lexlevel, ("level: %d / lexlevel: %d",
4657 level, s->lexlevel));
4659 while (s->lexlevel > level)
4661 if (usymtab_isBranch (s))
4663 int eindex = refTable_lookup (s, level, index);
4665 if (eindex != NOT_FOUND)
4667 return (s->entries[eindex]);
4671 s = usymtab_dropEnv (s);
4674 while (usymtab_isBranch (s) && s->lexlevel == level)
4676 int eindex = refTable_lookup (s, level, index);
4678 if (eindex != NOT_FOUND)
4680 return (s->entries[eindex]);
4683 s = usymtab_dropEnv (s);
4686 if (index >= s->nentries)
4688 return uentry_undefined;
4691 llassert (!uentry_isUndefined (s->entries[index]));
4693 return s->entries[index];
4696 static /*@dependent@*/ /*@exposed@*/ uentry
4697 usymtab_getRefNoisy (/*@notnull@*/ usymtab s, int level, usymId index)
4700 uentry ue = uentry_undefined;
4702 llassert (index >= 0);
4704 while (s->lexlevel > level)
4706 if (usymtab_isBranch (s))
4708 int eindex = refTable_lookup (s, level, index);
4710 if (eindex != NOT_FOUND)
4712 ue = s->entries[eindex];
4716 while (!usymtab_isBranch (otab))
4718 otab = usymtab_dropEnv (otab);
4719 llassert (otab != GLOBAL_ENV);
4722 if (refTable_lookup (otab, level, index) == NOT_FOUND)
4724 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4736 s = usymtab_dropEnv (s);
4739 llassert (usymtab_isDefined (s));
4741 while (usymtab_isBranch (s) && s->lexlevel == level)
4743 int eindex = refTable_lookup (s, level, index);
4746 if (eindex != NOT_FOUND)
4748 ue = s->entries[eindex];
4752 while (!usymtab_isBranch (otab))
4754 otab = usymtab_dropEnv (otab);
4755 llassert (otab != GLOBAL_ENV);
4758 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4768 s = usymtab_dropEnv (s);
4771 if (s->lexlevel == level && (index < s->nentries))
4773 ue = s->entries[index];
4775 if (uentry_isValid (ue))
4779 while (!usymtab_isBranch (otab))
4781 otab = usymtab_dropEnv (otab);
4783 if (otab == GLOBAL_ENV)
4789 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4800 if (index >= s->nentries)
4802 return uentry_undefined;
4805 llassert (!uentry_isUndefined (s->entries[index]));
4807 return s->entries[index];
4811 ** looking up entries
4813 ** If entry is inside a branch, then copy it, and put it into
4814 ** the branch table.
4818 int refTable_lookup (/*@notnull@*/ usymtab ut, int level, int index)
4820 refTable rt = ut->reftable;
4823 llassert (rt != NULL);
4825 for (i = 0; i < ut->nentries; i++)
4827 if (rt[i]->level == level && rt[i]->index == index)
4837 /*@only@*/ refentry refentry_create (int level, int index)
4839 refentry r = (refentry) dmalloc (sizeof (*r));
4847 static /*@dependent@*/ /*@exposed@*/ uentry
4848 usymtab_addRefEntry (/*@notnull@*/ usymtab s, cstring k)
4853 if (ut->reftable == NULL)
4855 DPRINTF (("Adding ref entry without reftable: %s", k));
4856 return uentry_undefined;
4859 llassert (ut->reftable != NULL);
4861 while (s != GLOBAL_ENV)
4863 eindex = usymtab_getIndex (s, k);
4865 if (eindex != NOT_FOUND)
4867 uentry current = s->entries[eindex];
4869 if (uentry_isVar (current) && !ctype_isFunction (uentry_getType (current)))
4873 DPRINTF (("Here: copying %s", uentry_unparse (current)));
4874 ue = uentry_copy (current);
4875 DPRINTF (("Here: copying %s", uentry_unparse (ue)));
4876 usymtab_addEntryQuiet (ut, ue);
4877 DPRINTF (("Okay..."));
4879 if (s->reftable != NULL)
4881 refentry ref = s->reftable[eindex];
4883 ut->reftable[ut->nentries - 1]
4884 = refentry_create (ref->level, ref->index);
4888 ut->reftable[ut->nentries - 1]
4889 = refentry_create (s->lexlevel, eindex);
4900 s = usymtab_dropEnv (s);
4903 return uentry_undefined;
4906 static uentry usymtab_lookupAux (usymtab s, cstring k)
4908 DPRINTF (("Lookup: %s", k));
4910 while (s != GLOBAL_ENV)
4912 int eindex = usymtab_getIndex (s, k);
4914 if (eindex != NOT_FOUND)
4916 uentry ret = s->entries[eindex];
4920 if (s->kind == US_TBRANCH
4921 || s->kind == US_FBRANCH
4922 || s->kind == US_CBRANCH)
4923 /* uentry_isGlobalVariable (ret) && os->lexlevel > fileScope) */
4926 DPRINTF (("Adding global ref entry: %s", k));
4927 ret = usymtab_addRefEntry (os, k);
4928 DPRINTF (("Adding ref entry: %s", uentry_unparseFull (ret)));
4933 DPRINTF (("Found: %s", uentry_unparseFull (ret)));
4937 if (s->kind == US_TBRANCH || s->kind == US_FBRANCH
4938 || s->kind == US_CBRANCH)
4940 /* why isn't this os??? */
4941 uentry ret = usymtab_addRefEntry (s, k);
4942 DPRINTF (("Adding ref entry: %s", uentry_unparseFull (ret)));
4949 return uentry_undefined;
4952 static /*@dependent@*/ /*@exposed@*/ uentry
4953 usymtab_lookupQuietAux (usymtab s, cstring k, bool noalt)
4957 while (s != GLOBAL_ENV)
4959 eindex = usymtab_getIndex (s, k);
4961 if (eindex != NOT_FOUND)
4963 uentry ret = s->entries[eindex];
4967 if (noalt && usymtab_isBranch (s))
4969 s = usymtab_dropEnv (s);
4973 llassert (s != NULL); /*@i523 should not need this? */
4978 return uentry_undefined;
4981 static /*@exposed@*/ /*@dependent@*/ uentry
4982 usymtab_lookupQuiet (usymtab s, cstring k)
4984 return usymtab_lookupQuietAux (s, k, FALSE);
4987 static /*@exposed@*/ /*@dependent@*/ uentry
4988 usymtab_lookupQuietNoAlt (usymtab s, cstring k)
4990 return usymtab_lookupQuietAux (s, k, TRUE);
4993 /*@dependent@*/ /*@observer@*/ uentry
4994 usymtab_lookupSafe (cstring k)
4997 DPRINTF (("Lookup safe: %s", k));
4998 return (usymtab_lookupAux (utab, k));
5002 usymtab_lookupExpose (cstring k)
5005 uentry ce = usymtab_lookupAux (utab, k);
5007 if (uentry_isUndefined (ce))
5009 llfatalbug (message ("usymtab_lookup: not found: *%s*", k));
5012 if (uentry_isPriv (ce))
5014 llfatalbug (message ("usymtab_lookup: private: *%s*", k));
5020 uentry usymtab_lookupExposeGlob (cstring k)
5022 return (usymtab_lookupGlobSafe (k));
5025 uentry usymtab_lookupGlob (cstring k)
5026 /*@globals globtab@*/
5028 uentry ce = usymtab_lookupAux (globtab, k);
5030 if (uentry_isUndefined (ce))
5031 llfatalbug (message ("usymtab_lookup: not found: %s", k));
5033 if (uentry_isPriv (ce))
5034 llfatalbug (message ("usymtab_lookup: private: %s", k));
5036 DPRINTF (("Lookup global: %s", uentry_unparseFull (ce)));
5040 /*@observer@*/ uentry
5041 usymtab_lookupGlobSafe (cstring k)
5042 /*@globals globtab@*/
5044 uentry ce = usymtab_lookupAux (globtab, k);
5045 DPRINTF (("Lookup global: %s", uentry_unparseFull (ce)));
5049 uentry usymtab_lookupEither (cstring k)
5052 uentry ce = usymtab_lookupSafe (k);
5054 if (uentry_isUndefined (ce))
5055 llfatalerror (message ("usymtab_lookup: not found: %s", k));
5057 DPRINTF (("Lookup either: %s", uentry_unparseFull (ce)));
5063 usymtab_lookupType (cstring k)
5064 /*@globals globtab@*/
5066 usymId uid = usymtab_getTypeId (k);
5068 if (uid == USYMIDINVALID)
5070 llcontbug (message ("usymtab_lookupType: not found: %s", k));
5071 return ctype_unknown;
5074 return (uentry_getRealType (usymtab_getTypeEntry (uid)));
5079 usymtab_lookupAbstractType (cstring k) /*@globals globtab@*/
5081 usymId uid = usymtab_getTypeId (k);
5083 if (uid == USYMIDINVALID)
5085 llcontbug (message ("usymtab_lookupType: not found: %s", k));
5086 return ctype_unknown;
5089 return (uentry_getAbstractType (usymtab_getTypeEntry (uid)));
5093 ** if there is an unnamed lcl-specified struct tag matching
5094 ** the uentryList, return its datatype. Otherwise, returns
5099 usymtab_structFieldsType (uentryList f)
5100 /*@globals globtab@*/
5102 return (usymtab_suFieldsType (f, TRUE));
5106 usymtab_unionFieldsType (uentryList f)
5107 /*@globals globtab@*/
5109 return (usymtab_suFieldsType (f, FALSE));
5113 usymtab_suFieldsType (uentryList f, bool isStruct)
5114 /*@globals globtab@*/
5118 DPRINTF (("Fields: %s", uentryList_unparse (f)));
5120 if (fileloc_isSpec (g_currentloc))
5122 return (ctype_undefined);
5125 for (i = 0; i < globtab->nentries; i++)
5127 uentry current = globtab->entries[i];
5130 ? uentry_isStructTag (current) : uentry_isUnionTag (current)))
5132 if (isFakeTag (uentry_rawName (current)))
5134 ctype ct = uentry_getType (current);
5136 DPRINTF (("Check: %s", ctype_unparse (ct)));
5138 if ((isStruct ? ctype_isStruct (ct) : ctype_isUnion (ct))
5140 (uentry_isSpecified (current)
5141 && uentryList_equivFields (f, ctype_getFields (ct))))
5143 return uentry_getAbstractType (current);
5153 return ctype_undefined;
5157 usymtab_enumEnumNameListType (enumNameList f)
5158 /*@globals globtab@*/
5162 for (i = 0; i < globtab->nentries; i++)
5164 uentry current = globtab->entries[i];
5166 if (uentry_isEnumTag (current))
5168 if (isFakeTag (uentry_rawName (current)))
5170 ctype ct = uentry_getType (current);
5172 if (ctype_isEnum (ct) && (enumNameList_match (f, ctype_elist (ct))))
5174 return uentry_getType (current);
5180 return ctype_undefined;
5184 usymtab_exists (cstring k)
5187 uentry ce = usymtab_lookupSafe (k);
5188 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5192 usymtab_existsReal (cstring k)
5195 uentry ce = usymtab_lookupSafe (k);
5197 return (!(uentry_isUndefined (ce))
5198 && !(uentry_isPriv (ce))
5199 && !(uentry_isExpandedMacro (ce)));
5203 usymtab_existsGlob (cstring k)
5204 /*@globals globtab@*/
5206 uentry ce = usymtab_lookupAux (globtab, k);
5208 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5213 usymtab_existsEither (cstring k)
5216 uentry ce = usymtab_lookupAux (utab, k);
5218 return (uentry_isValid (ce));
5222 usymtab_existsGlobEither (cstring k)
5223 /*@globals globtab@*/
5225 uentry ce = usymtab_lookupAux (globtab, k);
5227 return (uentry_isValid (ce));
5232 usymtab_existsType (cstring k)
5233 /*@globals globtab@*/
5235 uentry ce = usymtab_lookupAux (globtab, k);
5237 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)) && uentry_isDatatype (ce));
5241 usymtab_existsTypeEither (cstring k)
5242 /*@globals globtab@*/
5245 ce = usymtab_lookupAux (globtab, k);
5246 return (uentry_isValid (ce) && uentry_isDatatype (ce));
5250 usymtab_existsStructTag (cstring k) /*@globals globtab@*/
5252 cstring sname = makeStruct (k);
5253 uentry ce = usymtab_lookupAux (globtab, sname);
5254 cstring_free (sname);
5255 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5259 usymtab_existsUnionTag (cstring k) /*@globals globtab@*/
5261 cstring uname = makeUnion (k);
5262 uentry ce = usymtab_lookupAux (globtab, uname);
5264 cstring_free (uname);
5266 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5270 usymtab_existsEnumTag (cstring k) /*@globals globtab@*/
5272 cstring ename = makeEnum (k);
5273 uentry ce = usymtab_lookupAux (globtab, ename);
5275 cstring_free (ename);
5276 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5280 bool usymtab_existsVar (cstring k)
5283 uentry ce = usymtab_lookupSafe (k);
5285 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)) && (uentry_isVar (ce)));
5294 refTable_free (/*@only@*/ /*@null@*/ refTable x, int nentries)
5300 for (i = 0; i < nentries; i++)
5310 usymtab_freeLevel (/*@notnull@*/ /*@only@*/ usymtab u)
5311 /*@globals globtab, utab, filetab@*/
5315 aliasTable_free (u->aliases);
5317 refTable_free (u->reftable, u->nentries);
5319 if (u == filetab || u == globtab)
5321 for (i = 0; i < u->nentries; i++)
5323 uentry_freeComplete (u->entries[i]);
5328 for (i = 0; i < u->nentries; i++)
5330 uentry_free (u->entries[i]);
5334 guardSet_free (u->guards);
5341 llassert (!cstringTable_isDefined (u->htable));
5349 usymtab_freeAux (/*@only@*/ usymtab u)
5350 /*@globals globtab, utab, filetab@*/
5353 while (u != GLOBAL_ENV)
5356 usymtab_freeLevel (u);
5363 void usymtab_free ()
5364 /*@globals killed utab, globtab, filetab@*/
5368 usymtab_freeAux (utab);
5371 static int usymtab_lexicalLevel (void) /*@globals utab@*/
5373 return (utab->lexlevel);
5376 bool usymtab_inGlobalScope () /*@globals utab, globtab@*/
5378 return (utab == globtab);
5381 bool usymtab_inFileScope () /*@globals utab@*/
5383 return (utab->lexlevel == fileScope);
5386 bool usymtab_inFunctionScope () /*@globals utab@*/
5388 return (utab->lexlevel == functionScope);
5393 usymtab_replaceEntry (uentry s)
5394 /*@globals utab, globtab@*/
5395 /*@modifies utab, s@*/
5397 usymtab_replaceEntryAux (utab, s);
5402 usymtab_matchForwardStruct (usymId u1, usymId u2)
5403 /*@globals globtab@*/
5405 uentry ue1 = usymtab_getTypeEntry (u1);
5406 uentry ue2 = usymtab_getTypeEntry (u2);
5408 if (uentry_isAnyTag (ue2))
5410 ctype reptype = uentry_getType (ue1);
5412 if (ctype_isPointer (reptype))
5414 ctype repbase = ctype_getBaseType (reptype);
5416 if (ctype_isUA (repbase))
5418 typeId rtuid = ctype_typeId (repbase);
5420 if (u2 == rtuid) return TRUE;
5422 if (usymId_isValid (rtuid))
5424 reptype = uentry_getType (usymtab_getTypeEntry (rtuid));
5426 return (ctype_isUA (reptype) && (u2 == (ctype_typeId (reptype))));
5435 void usymtab_addGuards (guardSet guards)
5438 utab->guards = guardSet_union (utab->guards, guards);
5441 static bool usymtab_isGuardedAux (sRef s)
5445 sRef base = sRef_getRootBase (s);
5446 int lowlevel = paramsScope;
5447 int baselevel = sRef_lexLevel (base);
5449 if (sRef_isCvar (base))
5451 lowlevel = baselevel;
5452 if (lowlevel < paramsScope) lowlevel = paramsScope;
5455 while (tab->lexlevel >= lowlevel)
5457 DPRINTF (("Is guarded? [%s] %s",
5458 guardSet_unparse (tab->guards),
5459 sRef_unparseFull (s)));
5461 if (guardSet_isGuarded (tab->guards, s))
5464 if (!sRef_definitelyNull (s))
5466 sRef_setNotNull (s, fileloc_undefined);
5472 tab = usymtab_dropEnv (tab);
5478 void usymtab_unguard (sRef s) /*@modifies utab@*/
5481 sRef base = sRef_getRootBase (s);
5482 int lowlevel = paramsScope;
5483 int baselevel = sRef_lexLevel (base);
5485 if (sRef_isCvar (base))
5487 lowlevel = baselevel;
5488 if (lowlevel < paramsScope) lowlevel = paramsScope;
5491 while (tab->lexlevel >= lowlevel)
5493 if (guardSet_isGuarded (tab->guards, s))
5495 guardSet_delete (tab->guards, s);
5498 tab = usymtab_dropEnv (tab);
5502 bool usymtab_isGuarded (sRef s)
5504 DPRINTF (("Is guarded? %s", sRef_unparseFull (s)));
5505 return (sRef_aliasCompleteSimplePred (usymtab_isGuardedAux, s));
5508 bool usymtab_isDefinitelyNull (sRef s)
5510 return (sRef_aliasCheckSimplePred (usymtab_isDefinitelyNullAux, s));
5513 bool usymtab_isDefinitelyNullDeep (sRef s)
5515 return (sRef_deepPred (usymtab_isDefinitelyNull, s));
5518 static bool usymtab_isDefinitelyNullAux (sRef s)
5522 sRef base = sRef_getRootBase (s);
5523 int lowlevel = paramsScope;
5525 if (sRef_isCvar (base))
5527 lowlevel = sRef_lexLevel (base);
5528 if (lowlevel < paramsScope) lowlevel = paramsScope;
5531 while (tab->lexlevel >= lowlevel)
5533 if (guardSet_mustBeNull (tab->guards, s))
5538 while (tab->kind == US_CBRANCH)
5543 llassert (usymtab_isDefined (tab));
5545 if (tab->kind == US_FBRANCH)
5548 llassert (tab->kind == US_TBRANCH);
5558 usymtab_printGuards ()
5559 /*@globals utab, globtab@*/
5561 usymtab ttab = utab;
5563 while (ttab != globtab)
5565 llmsg (message ("Guards [%d]: %q", ttab->lexlevel,
5566 guardSet_unparse (ttab->guards)));
5572 usymtab_displayAllUses ()
5573 /*@globals utab, globtab@*/
5577 /* only in top scope */
5578 llassert (utab == globtab);
5580 /* need a copy, so order is not messed up by sort! */
5581 copy = usymtab_shallowCopy (globtab);
5583 qsort (copy->entries, (size_t)copy->nentries,
5584 sizeof (*copy->entries), (int (*)(const void *, const void *)) uentry_xcompareuses);
5586 usymtab_entries (copy, ue)
5588 if (uentry_isValid (ue) && !uentry_isGlobalMarker (ue))
5590 filelocList uses = uentry_getUses (ue);
5591 int size = filelocList_realSize (uses);
5593 if (fileloc_isDefined (uentry_whereDefined (ue))
5594 && !fileloc_isLib (uentry_whereDefined (ue))
5597 llmsg (message ("%q (%q), %d use%&:\n %q",
5598 uentry_getName (ue),
5599 fileloc_unparse (uentry_whereDefined (ue)),
5600 size, filelocList_unparseUses (uses)));
5603 } end_usymtab_entries;
5605 usymtab_shallowFree (copy);
5608 static /*@dependent@*/ /*@exposed@*/ usymtab
5609 usymtab_getFileTab ()
5610 /*@globals filetab@*/
5612 llassert (filetab != NULL);
5618 usymtab_unparseStack ()
5621 return (usymtab_unparseStackTab (utab));
5624 static /*@only@*/ cstring
5625 usymtab_unparseStackTab (usymtab t)
5627 bool firstOne = TRUE;
5628 cstring ret = cstring_makeLiteral ("[");
5630 while (t != GLOBAL_ENV)
5634 ret = message ("%q %q", ret, usymtab_typeName (t));
5639 ret = message ("%q, %q", ret, usymtab_typeName (t));
5644 ret = message ("%q ]", ret);
5648 static /*@only@*/ cstring
5649 usymtab_typeName (/*@notnull@*/ usymtab t)
5653 case US_GLOBAL: return cstring_makeLiteral ("global");
5654 case US_NORMAL: return cstring_makeLiteral ("normal");
5655 case US_TBRANCH: return cstring_makeLiteral ("true");
5656 case US_FBRANCH: return cstring_makeLiteral ("false");
5657 case US_CBRANCH: return cstring_makeLiteral ("case");
5658 case US_SWITCH: return cstring_makeLiteral ("switch");
5664 void usymtab_addMustAlias (/*@exposed@*/ sRef s, /*@exposed@*/ sRef al)
5667 if (!sRef_similar (s, al))
5669 usymtab_addForceMustAlias (s, al);
5674 ** Same as usymtab_addMustAlias, except does not check sRef_isSimilar.
5677 void usymtab_addForceMustAlias (/*@exposed@*/ sRef s, /*@exposed@*/ sRef al)
5680 /* evans 2002-03-3: was sRef_isMeaningful -- but we need to keep aliases for new storage also! */
5681 if (sRef_isMeaningful (s)
5682 && sRef_isMeaningful (al)
5683 && !(sRef_isConst (s) || sRef_isConst (al))
5684 && !(sRef_isAddress (al) && sRef_isDirectParam (sRef_getBase (al))))
5686 utab->aliases = aliasTable_addMustAlias (utab->aliases, s, al);
5687 DPRINTF (("Must alias: %s", aliasTable_unparse (utab->aliases)));
5690 ** for local variable, aliasing is symmetric
5693 if (sRef_isLocalVar (s) && sRef_isLocalVar (al))
5695 utab->aliases = aliasTable_addMustAlias (utab->aliases, al, s);
5700 DPRINTF (("Not aliasing! %s / %s", sRef_unparseFull (s), sRef_unparseFull (al)));
5701 DPRINTF (("meaningful: %d %d", sRef_isMeaningful (s), sRef_isMeaningful (al)));
5705 void usymtab_addReallyForceMustAlias (/*@exposed@*/ sRef s, /*@exposed@*/ sRef al)
5708 utab->aliases = aliasTable_addMustAlias (utab->aliases, s, al);
5711 void usymtab_clearAlias (sRef s)
5712 /*@modifies utab, s@*/
5715 aliasTable_clearAliases (utab->aliases, s);
5718 sRefSet usymtab_allAliases (sRef s)
5721 if (sRef_isSomewhatMeaningful (s))
5725 ret = sRefSet_unionFree (aliasTable_aliasedBy (utab->aliases, s),
5726 aliasTable_canAlias (utab->aliases, s));
5731 DPRINTF (("NOT A MEANINGFUL SREF!"));
5732 return sRefSet_undefined;
5736 /*@only@*/ sRefSet usymtab_canAlias (sRef s)
5739 if (sRef_isSomewhatMeaningful (s))
5741 sRefSet res = aliasTable_canAlias (utab->aliases, s);
5745 return sRefSet_undefined;
5748 /*@only@*/ sRefSet usymtab_aliasedBy (sRef s)
5751 return (aliasTable_aliasedBy (utab->aliases, s));
5754 /*@only@*/ cstring usymtab_unparseAliases ()
5757 return (aliasTable_unparse (utab->aliases));
5761 ** Debugging routines:
5762 ** okay to leak storage here, only for debugging
5768 usymtab_printOut (void)
5774 char *ind = mstring_copy (" ");
5776 fprintf (g_warningstream, "<<< [symbol table] >>>\n");
5778 while (s != GLOBAL_ENV && s->env != GLOBAL_ENV)
5780 cstring tname = usymtab_typeName (s);
5784 ind[depth * 3 + 1] = '\0';
5787 fprintf (g_warningstream, "level: %d (%s)\n", s->lexlevel,
5788 cstring_toCharsSafe (tname));
5790 cstring_free (tname);
5792 for (i = 0; i < s->nentries; i++)
5794 cstring us = uentry_unparseFull (s->entries[i]);
5795 fprintf (g_warningstream, "%s\n", cstring_toCharsSafe (us));
5799 if (s->reftable != NULL && s->nentries > 0)
5801 fprintf (g_warningstream, "\t<< Ref table >>\n");
5803 for (i = 0; i < s->nentries; i++)
5805 fprintf (g_warningstream, "\t%s %3d: %d, %d\n", ind, i,
5806 s->reftable[i]->level,
5807 s->reftable[i]->index);
5811 ind[depth * 3 + 1] = ' ';
5815 fprintf (g_warningstream, "<<< end usymtab >>>\n");
5821 usymtab_printTypes ()
5822 /*@globals globtab@*/
5824 usymtab_printAllAux (globtab);
5828 usymtab_printAll (void)
5831 usymtab_printAllAux (utab);
5835 usymtab_printAllAux (usymtab s)
5836 /*@modifies g_warningstream@*/
5840 char *ind = mstring_copy (" ");
5842 printf ("[[[ usymtab ]]]");
5844 while (s != GLOBAL_ENV)
5847 ind[depth * 3 + 1] = '\0';
5849 if (s->env == GLOBAL_ENV)
5853 printf ("level: %d / break: %s / exit: %s\n", s->lexlevel,
5854 cstring_toCharsSafe (bool_unparse (s->mustBreak)),
5855 cstring_toCharsSafe (exitkind_unparse (s->exitCode)));
5859 for (i = looplow; i < s->nentries; i++)
5861 printf ("%s%3d. %s\n", ind, i,
5862 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5867 printf ("level: %d / break: %s / exit: %s\n", s->lexlevel,
5868 cstring_toCharsSafe (bool_unparse (s->mustBreak)),
5869 cstring_toCharsSafe (exitkind_unparse (s->exitCode)));
5871 for (i = 0; i < s->nentries; i++)
5873 printf ("%s%3d %s\n", ind, i,
5874 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5878 ind[depth * 3 + 1] = ' ';
5882 printf ("----------\n");
5886 usymtab_printComplete ()
5891 char *ind = mstring_copy (" ");
5894 while (s != GLOBAL_ENV)
5898 ind[depth * 3 + 1] = '\0';
5901 if (s->env == GLOBAL_ENV)
5905 printf ("level: %d\n", s->lexlevel);
5909 for (i = looplow; i < s->nentries; i++)
5911 printf ("%s%3d %s\n", ind, i,
5912 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5917 printf ("level: %d\n", s->lexlevel);
5918 for (i = 0; i < s->nentries; i++)
5920 printf ("%s%3d %s\n", ind, i,
5921 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5925 ind[depth * 3 + 1] = ' ';
5930 printf ("----------\n");
5935 static /*@only@*/ cstring /*@unused@*/
5936 usymtab_unparseLocalAux (/*@notnull@*/ usymtab s)
5938 cstring c = message ("lexlevel: %d\n", s->lexlevel);
5941 for (i = 0; i < s->nentries; i++)
5943 c = message ("%q\n%q", c, uentry_unparseFull (s->entries[i]));
5946 c = message ("%q\n=========", c);
5950 static cstring /*@unused@*/ /*@only@*/
5951 usymtab_unparseLocalList (/*@notnull@*/ usymtab s)
5953 cstring c = message ("[%d/%s/%s] ", s->lexlevel,
5954 bool_unparse (s->mustBreak),
5955 exitkind_unparse (s->exitCode));
5958 for (i = 0; i < s->nentries; i++)
5960 sRef sr = uentry_getSref (s->entries[i]);
5964 c = message ("%q: %q [%b]", c, uentry_getName (s->entries[i]),
5965 sRef_isStateDefined (sr));
5969 c = message ("%q, %q [%b]", c, uentry_getName (s->entries[i]),
5970 sRef_isStateDefined (sr));
5980 usymtab_printLocal (void)
5986 printf ("lexlevel: %d\n", s->lexlevel);
5988 for (i = 0; i < s->nentries; i++)
5990 printf ("%s\n", cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5993 while (s->lexlevel > 1)
5998 llassert (usymtab_isDefined (s));
6000 printf ("Params:\n");
6002 for (i = 0; i < s->nentries; i++)
6004 printf ("%d: %s\n", i,
6005 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
6010 static bool checkDistinctExternalName (uentry e)
6011 /*@globals globtab@*/
6012 /*@modifies *g_warningstream@*/
6014 size_t checklen = size_fromInt (context_getValue (FLG_EXTERNALNAMELEN));
6015 bool ignorecase = context_getFlag (FLG_EXTERNALNAMECASEINSENSITIVE);
6016 bool gotone = FALSE;
6017 bool extras = FALSE;
6018 bool hasError = FALSE;
6019 cstring name = uentry_rawName (e);
6020 usymtab st = globtab;
6028 if (uentry_isAnyTag (e))
6030 checklen++; /* the tag marker doesn't count */
6034 usymtab_entries (st, oe)
6036 if (uentry_sameObject (oe, e))
6043 if (cstring_equalCaseInsensitive (uentry_rawName (oe), name))
6052 (FLG_DISTINCTEXTERNALNAMES,
6054 ("External identifier %q is not distinguishable from %q "
6055 "because alphabetical case is ignored",
6057 uentry_getName (oe)),
6058 uentry_whereLast (e)))
6060 uentry_showWhereAny (oe);
6061 uentry_setHasNameError (oe);
6070 if (cstring_equalLenCaseInsensitive (uentry_rawName (oe),
6079 if (cstring_equalLen (uentry_rawName (oe), name, checklen))
6082 (FLG_DISTINCTEXTERNALNAMES,
6085 ("External identifier %q is not distinguishable from %q "
6086 "in the first %d characters (%q)",
6088 uentry_getName (oe),
6089 size_toInt (checklen),
6090 cstring_clip (uentry_getName (e), checklen)),
6092 uentry_whereLast (e)))
6094 uentry_showWhereAny (oe);
6095 uentry_setHasNameError (oe);
6108 (FLG_DISTINCTEXTERNALNAMES,
6110 ("External identifier %q is not distinguishable from %q "
6111 "in the first %d characters because alphabetical case "
6114 uentry_getName (oe),
6115 size_toInt (checklen)),
6116 uentry_whereLast (e)))
6118 uentry_showWhereAny (oe);
6119 uentry_setHasNameError (oe);
6125 else if (cstring_equalLen (uentry_rawName (oe), name, checklen))
6134 (FLG_DISTINCTEXTERNALNAMES,
6137 ("External identifier %q is not distinguishable from %q "
6138 "in the first %d characters (%q)",
6140 uentry_getName (oe),
6141 size_toInt (checklen),
6142 cstring_clip (uentry_getName (e), checklen)),
6144 uentry_whereLast (e)))
6146 uentry_showWhereAny (oe);
6147 uentry_setHasNameError (oe);
6156 } end_usymtab_entries ;
6163 (cstring_makeLiteral ("One or more additional "
6164 "indistinguishable external "
6165 "names not reported"));
6171 static bool checkDistinctInternalName (uentry e)
6173 /*@modifies *g_warningstream@*/
6175 usymtab ttab = utab;
6176 cstring name = uentry_rawName (e);
6177 size_t numchars = size_fromInt (context_getValue (FLG_INTERNALNAMELEN));
6178 bool caseinsensitive = context_getFlag (FLG_INTERNALNAMECASEINSENSITIVE);
6179 bool lookalike = context_getFlag (FLG_INTERNALNAMELOOKALIKE);
6181 if (uentry_isAnyTag (e) && (numchars != 0))
6183 numchars++; /* the tag marker doesn't count */
6186 while (usymtab_isDefined (ttab))
6188 usymtab_entries (ttab, oe)
6190 if (uentry_sameObject (oe, e))
6192 /*@innercontinue@*/ continue;
6195 switch (cstring_genericEqual
6196 (name, uentry_rawName (oe),
6197 numchars, caseinsensitive, lookalike))
6199 case CGE_DISTINCT: /* okay */
6203 if (cstring_equal (name, uentry_rawName (oe)))
6205 ; /* got a shadow error */
6210 (FLG_DISTINCTINTERNALNAMES,
6213 ("Internal identifier %q is not distinguishable from %q "
6214 "in the first %d characters (%q)",
6216 uentry_getName (oe),
6217 size_toInt (numchars),
6218 cstring_clip (uentry_getName (e), numchars)),
6220 uentry_whereLast (e)))
6222 uentry_showWhereAny (oe);
6223 uentry_setHasNameError (oe);
6230 if (numchars == 0 || (cstring_length (name) <= numchars))
6233 (FLG_DISTINCTINTERNALNAMES,
6235 ("Internal identifier %q is not distinguishable from %q "
6236 "without case sensitivity",
6238 uentry_getName (oe)),
6239 uentry_whereLast (e)))
6241 uentry_showWhereAny (oe);
6242 uentry_setHasNameError (oe);
6249 (FLG_DISTINCTINTERNALNAMES,
6251 ("Internal identifier %q is not distinguishable from %q "
6252 "in the first %d characters without case sensitivity",
6254 uentry_getName (oe),
6255 size_toInt (numchars)),
6256 uentry_whereLast (e)))
6258 uentry_showWhereAny (oe);
6259 uentry_setHasNameError (oe);
6267 || (cstring_length (name) <= numchars))
6270 (FLG_DISTINCTINTERNALNAMES,
6272 ("Internal identifier %q is not distinguishable from %q "
6273 "except by lookalike characters",
6275 uentry_getName (oe)),
6276 uentry_whereLast (e)))
6278 uentry_showWhereAny (oe);
6279 uentry_setHasNameError (oe);
6286 (FLG_DISTINCTINTERNALNAMES,
6288 ("Internal identifier %q is not distinguishable from %q "
6289 "in the first %d characters except by lookalike characters",
6291 uentry_getName (oe),
6292 size_toInt (numchars)),
6293 uentry_whereLast (e)))
6295 uentry_showWhereAny (oe);
6296 uentry_setHasNameError (oe);
6301 } end_usymtab_entries ;
6309 void usymtab_checkDistinctName (uentry e, int scope)
6310 /*@globals utab, globtab@*/
6312 bool hasError = FALSE;
6313 fileloc where = uentry_whereLast (e);
6315 if (!fileloc_isPreproc (where) && !fileloc_isBuiltin (where))
6317 if (scope == globScope)
6319 if (context_getFlag (FLG_DISTINCTEXTERNALNAMES))
6321 hasError = checkDistinctExternalName (e);
6325 if (!hasError && context_getFlag (FLG_DISTINCTINTERNALNAMES))
6327 hasError = checkDistinctInternalName (e);
6333 uentry_setHasNameError (e);
6337 /*@exposed@*/ sRef usymtab_lookupGlobalMarker (void) /*@globals utab@*/
6341 ue = usymtab_lookupAux (utab, GLOBAL_MARKER_NAME);
6342 llassert (uentry_isValid (ue));
6344 return uentry_getSref (ue);
6350 ** For debugging only
6354 usymtab_checkAllValid () /*@globals utab@*/
6358 while (tab != GLOBAL_ENV)
6362 for (i = 0; i < utab->nentries; i++)
6364 uentry e = utab->entries[i];
6366 uentry_checkValid (e);
6369 aliasTable_checkValid (tab->aliases);