2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 University of Virginia,
4 ** Massachusetts Institute of Technology
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ** General Public License for more details.
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
27 ** Lexically scoped symbol table.
29 ** Unlike a normal symbol table, this table splits to reflect branches.
31 ** This is necessary since symbol table entries also reflect
32 ** some state information, like initialization, that may be
33 ** different on different branches.
35 ** For each control structure we split:
37 ** { } - a normal scope (no control) US_NORMAL
39 ** if <pred> { } - US_TBRANCH true branch
40 ** while <pred> { } US_FBRANCH else branch
42 ** switch <pred> { } - US_SWITCH
44 ** case x: - case scope US_CBRANCH
45 ** Should be kept in a separate table, because of
49 # include "splintMacros.nf"
51 # include "structNames.h"
52 # include "exprChecks.h"
53 # include "transferChecks.h"
55 /* Needed to install macros when loading libraries */
58 # include "cpperror.h"
62 ** Keep track of type definitions inside a function.
65 static uentryList functypes = uentryList_undefined;
67 static bool dbgfree = FALSE;
68 static bool dbgload = TRUE;
73 ** Hack to prevent shadow errors from appearing when function parameters
74 ** are entered into a new scope.
77 static bool noshadowerror = FALSE;
80 ** Constraint: static variables can live in 1 or > 2.
82 ** except for tags. All tags must be at the global level.
83 ** Static tags are handled in a kludgey way.
87 ** utab is the universal symbol table
88 ** globtab is the global environment of utab
89 ** oldtab is needed for conversions in dumping (after sorting the table)
91 ** there can be only one usymtab!
94 static /*@checkedstrict@*/ /*@owned@*/ /*@notnull@*/ usymtab utab;
96 /* Reachable environments from from utab */
97 static /*@checkedstrict@*/ /*@notnull@*/ /*@dependent@*/ usymtab globtab;
98 static /*@checkedstrict@*/ /*@dependent@*/ usymtab filetab;
100 /* Used for sorting for dumpage. */
101 static /*@checkedstrict@*/ /*@owned@*/ usymtab oldtab;
103 static int usymtab_lexicalLevel (void) /*@globals utab@*/ ;
104 static bool usymtab_isAltDefinitelyNull (sRef p_s) /*@globals utab@*/ ;
105 static void refTable_free (/*@only@*/ /*@null@*/ refTable p_x, int p_nentries);
106 static ctype usymtab_suFieldsType (uentryList p_f, bool p_isStruct) /*@globals globtab@*/ ;
108 extern int usymtab_getCurrentDepth (void) /*@globals utab@*/
110 return utab->lexlevel;
114 usymtab_freeLevel (/*@notnull@*/ /*@only@*/ usymtab p_u)
115 /*@globals globtab, utab, filetab@*/ /*@modifies p_u@*/ ;
117 static bool usymtab_isDefinitelyNullAux (sRef p_s) /*@globals utab@*/ ;
118 static /*@only@*/ cstring usymtab_unparseStackTab (usymtab p_t);
119 static /*@exposed@*/ /*@dependent@*/ uentry
120 usymtab_getRefTab (/*@notnull@*/ usymtab p_u, int p_level, usymId p_index);
123 /* These are not used anymore... */
124 static /*@unused@*/ /*@only@*/ cstring
125 usymtab_unparseLocalAux (/*@notnull@*/ usymtab p_s);
126 static /*@unused@*/ /*@only@*/ cstring
127 usymtab_unparseLocalList (/*@notnull@*/ usymtab p_s);
130 static /*@only@*/ cstring usymtab_typeName (/*@notnull@*/ usymtab p_t);
131 static void usymtab_handleParams (void)
132 /*@globals utab, globtab, filetab@*/
133 /*@modifies utab@*/ ;
135 static /*@exposed@*/ /*@dependent@*/ uentry
136 usymtab_addRefEntry (/*@notnull@*/ usymtab p_s, cstring p_k);
137 static /*@exposed@*/ /*@dependent@*/ usymtab
138 usymtab_dropEnv (/*@notnull@*/ usymtab p_s);
139 static /*@exposed@*/ /*@dependent@*/ uentry
140 usymtab_getRefNoisy (/*@notnull@*/ usymtab p_s, int p_level, usymId p_index);
142 static /*@exposed@*/ /*@dependent@*/ uentry
143 usymtab_lookupQuietAux (usymtab p_s, cstring p_k, bool p_noalt);
145 static /*@exposed@*/ /*@dependent@*/ uentry
146 usymtab_lookupQuiet (usymtab p_s, cstring p_k);
148 static /*@exposed@*/ /*@dependent@*/ uentry
149 usymtab_lookupQuietNoAlt (usymtab p_s, cstring p_k);
151 static void usymtab_printAllAux (usymtab p_s) /*@modifies g_msgstream@*/ ;
152 static int usymtab_getIndex (/*@notnull@*/ usymtab p_s, cstring p_k);
153 static /*@exposed@*/ uentry usymtab_fetchIndex (/*@notnull@*/ usymtab p_s, int p_i);
154 static /*@exposed@*/ uentry
155 usymtab_lookupAux (usymtab p_s, cstring p_k);
156 static /*@exposed@*/ /*@dependent@*/ /*@notnull@*/ usymtab
157 usymtab_getFileTab (void) /*@globals filetab@*/ ;
158 static int refTable_lookup (/*@notnull@*/ usymtab p_ut, int p_level, int p_index);
159 static bool usymtab_mustBreak (usymtab p_s);
160 static bool usymtab_mustEscape (usymtab p_s);
162 static void recordFunctionType (uentry ue)
164 llassert (uentry_isDatatype (ue) || uentry_isAnyTag (ue)
165 || uentry_isEnumConstant (ue));
166 DPRINTF (("Function type: %s", uentry_unparseFull (ue)));
168 functypes = uentryList_add (functypes, ue);
172 static void clearFunctionTypes (void)
173 /*@modifies globtab@*/
175 uentryList_elements (functypes, el)
177 if (cstring_isDefined (uentry_rawName (el)))
179 if (cstringTable_isDefined (globtab->htable))
181 cstringTable_remove (globtab->htable, uentry_rawName (el));
184 uentry_setName (el, cstring_undefined);
188 llassert (uentry_isValid (el));
189 el->sref = sRef_undefined;
190 /*@noaccess uentry@*/
191 } end_uentryList_elements ;
193 uentryList_clear (functypes);
196 static /*@falsewhennull@*/ bool usymtab_isBranch (usymtab u)
198 return (usymtab_isDefined (u) &&
199 (u->kind == US_TBRANCH || u->kind == US_FBRANCH
200 || u->kind == US_CBRANCH || u->kind == US_SWITCH));
203 static bool usymtab_mustBreak (usymtab s)
205 llassert (s != GLOBAL_ENV);
206 return (s->mustBreak);
209 static bool usymtab_mustEscape (usymtab s)
211 llassert (s != GLOBAL_ENV);
212 return (exitkind_mustEscape (s->exitCode));
215 void usymtab_setMustBreak ()
218 llassert (utab != GLOBAL_ENV);
219 utab->mustBreak = TRUE;
222 void usymtab_setExitCode (exitkind ex)
225 llassert (utab != GLOBAL_ENV);
229 if (exitkind_mustEscape (ex))
231 utab->mustBreak = TRUE;
235 bool usymtab_isAltDefinitelyNullDeep (sRef s)
237 return (sRef_deepPred (usymtab_isAltDefinitelyNull, s));
240 static bool usymtab_isAltDefinitelyNull (sRef s)
248 llassert (utab->env != NULL);
250 /*@-mods@*/ /* These modifications are cancelled. */
251 utab->guards = utab->env->guards;
252 utab->env->guards = t;
255 llassert (usymtab_isDefined (utab));
256 res = usymtab_isDefinitelyNull (s);
259 ** This reports a spurious error. It is okay, because of
260 ** the nesting relationship of usymtab environments.
263 /*@-mods@*/ /* Cancelling modifications. */
264 /*@i1@*/ utab->env->guards = utab->guards;
265 /*@i1@*/ utab->guards = t;
271 static /*@notnull@*/ /*@special@*/ usymtab
272 usymtab_create (uskind kind, /*@keep@*/ usymtab env, bool nextlevel)
274 /*@post:isnull result->htable, result->guards, result->aliases@*/
276 usymtab t = (usymtab) dmalloc (sizeof (*t));
279 t->nspace = CBASESIZE;
280 t->entries = (uentry *) dmalloc (sizeof (*t->entries) * CBASESIZE);
282 /* We only use a reftable for branch-level symbol tables.
285 t->reftable = (nextlevel
287 : (refentry *) dmalloc (sizeof (*t->reftable) * CBASESIZE));
290 t->lexlevel = (env == GLOBAL_ENV ? 0 : env->lexlevel) + (nextlevel ? 1 : 0);
295 t->guards = guardSet_undefined;
296 t->aliases = aliasTable_undefined;
298 t->mustBreak = FALSE;
299 t->exitCode = XK_NEVERESCAPE;
308 static /*@only@*/ /*@notnull@*/ usymtab
309 usymtab_createRoot (void) /*@modifies nothing@*/
311 usymtab u = (usymtab) dmalloc (sizeof (*u));
314 u->nspace = CGLOBBASESIZE;
315 u->entries = (uentry *) dmalloc (sizeof (*u->entries) * CGLOBBASESIZE);
318 u->htable = cstringTable_create (CGLOBHASHSIZE);
321 u->guards = guardSet_new ();
322 u->aliases = aliasTable_new ();
324 u->mustBreak = FALSE;
325 u->exitCode = XK_NEVERESCAPE;
332 usymtab_initMod (void)
333 /*@globals undef utab, undef oldtab, undef globtab, undef filetab @*/
334 /*@modifies utab, globtab, filetab, oldtab @*/
336 utab = usymtab_createRoot ();
338 filetab = usymtab_undefined;
339 oldtab = usymtab_undefined;
344 usymtab_initGlobalMarker () /*@globals globtab@*/
346 if (uentry_isValid (usymtab_lookupAux (globtab, GLOBAL_MARKER_NAME)))
348 ; /* Already entered from load table. */
352 usymtab_addGlobalEntry (uentry_makeGlobalMarker ());
357 ** utab should be empty? (requires?)
359 ** Adds bool types to symbol table (these are built in for now)
360 ** Only do this is there is no library!
366 if (context_getFlag (FLG_NOLIB))
368 ctype boolt = ctype_bool;
369 /* evs 2000-07-24: bool is now treated as abstract (always) */
371 uentry boolentry = uentry_makeBoolDatatype (YES);
372 usymtab_supGlobalEntry (boolentry);
373 context_addBoolAccess ();
376 ** We supply values 0 and 1 for the constants, so things like
377 ** while (TRUE) can be interpreted correctly.
380 usymtab_supGlobalEntry
381 (uentry_makeConstantValue (context_getFalseName (), boolt,
382 fileloc_getBuiltin (), FALSE,
383 multiVal_makeInt (0)));
385 usymtab_supGlobalEntry
386 (uentry_makeConstantValue (context_getTrueName (), boolt,
387 fileloc_getBuiltin (), FALSE,
388 multiVal_makeInt (1)));
397 usymtab_grow (/*@notnull@*/ usymtab s)
400 o_uentry *oldsyms = s->entries;
402 s->nspace = CBASESIZE;
403 s->entries = (uentry *) dmalloc (sizeof (*s->entries)
404 * (s->nentries + s->nspace));
406 for (i = 0; i < s->nentries; i++)
408 s->entries[i] = oldsyms[i];
413 if (s->reftable != NULL)
415 refTable oldRefs = s->reftable;
417 s->reftable = (refentry *) dmalloc (sizeof (*s->reftable)
418 * (s->nentries + CBASESIZE + 1));
420 for (i = 0; i < s->nentries; i++)
422 s->reftable[i] = oldRefs[i];
433 usymtab_addEntryQuiet (/*@notnull@*/ usymtab s, /*@keep@*/ uentry e)
435 if (!(s->lexlevel > fileScope || !sRef_modInFunction ()))
437 if (uentry_isDatatype (e)
438 || uentry_isFunction (e)
439 || uentry_isAnyTag (e)
440 || uentry_isEnumConstant (e)
441 || context_inMacro ())
444 ** Not a bug. Code like,
445 ** int f (void) { typedef int tint; ... }
449 /* was nothing here! */
450 /*@i@*/ e->sref = sRef_saveCopy (e->sref);
455 (cstring_makeLiteral ("Declaration outside function scope"));
457 llcontbug (message ("usymtab_addEntryQuiet: inconsistent state "
458 "(lexlevel = %d, modFunction = %s) adding: %q",
459 s->lexlevel, bool_unparse (sRef_modInFunction ()),
460 uentry_unparse (e)));
461 sRef_setGlobalScope ();
462 /* make sure the sRef is not bashed! */
463 /*@i@*/ e->sref = sRef_saveCopy (e->sref);
473 s->entries[s->nentries] = e;
476 if (s == globtab || s == filetab)
478 if (!fileloc_isLib (g_currentloc))
480 uentry_tallyAnnots (e, AN_UNKNOWN);
485 if (cstringTable_isDefined (s->htable))
487 cstringTable_insert (s->htable, cstring_copy (uentry_rawName (e)), s->nentries);
493 static /*@observer@*/ uentry /*@alt void@*/
494 usymtab_addEntryBase (/*@notnull@*/ usymtab s, /*@only@*/ uentry e)
497 ** In theory, we shouldn't need this test because it this is
498 ** only called when a library is being read, and it shouldn't
499 ** ever have a duplicate entry. In practice, its safer to
500 ** leave it in, though.
505 if (uentry_isValid (old = usymtab_lookupQuiet (s, uentry_rawName (e))))
507 DPRINTF (("Duplicate entry: %s", uentry_unparse (e)));
508 uentry_free (e); /* duplicate */
513 int thisentry = s->nentries;
515 if (uentry_isVar (e))
517 uentry_setSref (e, sRef_makeCvar (globScope, thisentry,
519 stateInfo_makeLoc (uentry_whereLast (e))));
522 usymtab_addEntryQuiet (s, e);
528 static /*@observer@*/ uentry /*@alt void@*/
529 usymtab_addEntryAlways (/*@notnull@*/ usymtab s, /*@only@*/ uentry e)
532 ** In theory, we shouldn't need this test because it this is
533 ** only called when a library is being read, and it shouldn't
534 ** ever have a duplicate entry. In practice, its safer to
535 ** leave it in, though.
539 int thisentry = s->nentries;
541 if (uentry_isValid (old = usymtab_lookupQuiet (s, uentry_rawName (e))))
544 (message ("Duplicate entry in load library: %s. "
545 "Old entry: %q. New entry: %q",
547 uentry_unparseFull (old),
548 uentry_unparseFull (e)));
550 uentry_setName (e, message ("__x_%s", uentry_rawName (e)));
551 /* This shouldn't happen...unless the library is bad! */
555 if (uentry_isVar (e) && !uentry_isGlobalMarker (e))
557 uentry_setSref (e, sRef_makeCvar (globScope, thisentry,
559 stateInfo_makeLoc (uentry_whereLast (e))));
562 usymtab_addEntryQuiet (s, e);
567 usymtab_addEntryAux (/*@notnull@*/ usymtab st, /*@keep@*/ uentry e, bool isSref)
568 /*@globals globtab@*/
571 usymId thisentry = st->nentries;
573 llassert (!uentry_isElipsisMarker (e));
577 ** llassert (usymtab_lookupSafe (uentry_rawName (e)) == uentry_undefined);
580 llassertprint (uentry_isAnyTag (e) ? st == globtab : TRUE,
581 ("non-global tag: %s / %d", uentry_unparseFull (e), st->lexlevel));
583 if (st->lexlevel == fileScope
584 && (!(uentry_isStatic (e)) || uentry_isAnyTag (e)))
591 ctype ct = uentry_getType (e);
593 if (uentry_isFunction (e) && ctype_isFunction (ct))
595 ct = ctype_getReturnType (ct);
598 if (uentry_isStatic (e))
600 sRef sr = sRef_makeCvar (st->lexlevel, thisentry, ct, stateInfo_makeLoc (uentry_whereLast (e)));
602 if (sRef_isStack (sr) || sRef_isLocalState (sr))
604 sRef_setAliasKind (sr, AK_STATIC, uentry_whereLast (e));
605 sRef_setDefined (sr, uentry_whereLast (e));
608 uentry_setSref (e, sr);
612 uentry_setSref (e, sRef_makeCvar (st->lexlevel, thisentry, ct, stateInfo_makeLoc (uentry_whereLast (e))));
616 if (uentry_isDatatype (e))
619 uentry_setDatatype (e, thisentry);
622 if (uentry_isFunction (e))
624 ctype ct = uentry_getType (e);
626 if (ctype_isFunction (ct)
627 && uentryList_isMissingParams (ctype_argsFunction (ct)))
629 if (uentry_isDeclared (e))
633 message ("Function %q declared without parameter list",
635 uentry_whereDeclared (e));
641 message ("Function %q specified without parameter list",
643 uentry_whereSpecified (e));
648 if (st == globtab && !uentry_isSpecified (e))
650 exprChecks_checkExport (e);
653 uentry_checkName (e);
655 usymtab_addEntryQuiet (st, e);
656 DPRINTF (("Adding entry: [%p] %s", e, uentry_unparseFull (e)));
661 usymtab_addEntry (uentry e)
662 /*@globals utab, globtab@*/
663 /*@modifies utab, e@*/
665 llassertprint (!usymtab_exists (uentry_rawName (e)),
666 ("Entry already exists: %s", uentry_unparse (e)));
668 return usymtab_addEntryAux (utab, e, FALSE);
672 usymtab_addGlobalEntry (uentry e)
673 /*@modifies globtab, e@*/
675 DPRINTF (("Add global: %s / %s", uentry_unparse (e), bool_unparse (uentry_isForward (e))));
677 (void) usymtab_addEntryAux (globtab, e, FALSE);
681 ** supercede and replace entry use elements of entries array, and
682 ** assign an element to a new value, and then free the old value.
683 ** Need -deparrays to avoid errors for this.
689 usymtab_supEntryAux (/*@notnull@*/ usymtab st,
690 /*@only@*/ uentry e, bool isSref)
691 /*@globals globtab, filetab@*/
692 /*@modifies st, globtab, e@*/
694 cstring ename = uentry_rawName (e);
695 bool staticEntry = FALSE;
698 DPRINTF (("Sup entry aux: %s", uentry_unparseFull (e)));
700 /* static tags in global scope */
701 if (st->lexlevel == fileScope
702 && (!(uentry_isStatic (e)) || uentry_isAnyTag (e)))
704 eindex = usymtab_getIndex (st, ename);
706 if (eindex != NOT_FOUND)
708 uentry ce = st->entries[eindex];
712 message ("%s %q shadows static declaration",
713 ekind_capName (uentry_getKind (e)),
715 uentry_whereDeclared (e)))
717 uentry_showWhereLast (ce);
720 if (eindex == st->nentries - 1)
726 st->entries[eindex] = st->entries[st->nentries - 1];
729 if (cstringTable_isDefined (st->htable))
731 cstringTable_replaceKey (st->htable, uentry_rawName (ce),
732 cstring_copy (uentry_rawName (e)));
742 if (uentry_isStatic (e)) {
743 if (uentry_isFunction (e)) {
745 ** Static function declarations are at the file level,
746 ** even if they are in a deeper scope.
749 st = usymtab_getFileTab ();
752 if (!uentry_isAnyTag (e) && st == globtab)
754 st = usymtab_getFileTab ();
756 DPRINTF (("Static entry!"));
761 eindex = usymtab_getIndex (st, ename);
763 if (eindex != NOT_FOUND)
765 uentry ce = st->entries[eindex];
767 DPRINTF (("Found entry: %s", uentry_unparse (ce)));
769 if (uentry_isPriv (ce)
770 /* || (fileloc_isImport (uentry_whereSpecified (ce))) */
771 || (uentry_isStatic (ce)
772 && uentry_isAnyTag (ce)
773 && (uentry_isDeclared (ce)
774 && !fileloc_sameFile (uentry_whereDefined (ce),
775 uentry_whereDefined (e)))))
779 ** if overloading import should do some checks!
782 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
784 DPRINTF (("Overloading!"));
786 st->entries[eindex] = e;
788 if (uentry_isDatatype (e))
790 uentry_setDatatype (e, eindex);
793 if (st == globtab && !uentry_isSpecified (e))
795 exprChecks_checkExport (e);
798 if (cstringTable_isDefined (st->htable))
800 cstringTable_replaceKey (st->htable, uentry_rawName (ce),
801 cstring_copy (uentry_rawName (e)));
807 else if (uentry_isSpecified (ce))
809 if (uentry_isSpecified (e))
811 DPRINTF (("Here we are: %s", uentry_unparseFull (e)));
813 if (fileloc_isImport (uentry_whereSpecified (ce)))
815 if (cstringTable_isDefined (st->htable))
817 cstringTable_replaceKey (st->htable,
819 cstring_copy (uentry_rawName (e)));
823 st->entries[eindex] = e;
826 if (uentry_isDatatype (e)) uentry_setDatatype (e, eindex);
830 if (fileloc_isImport (uentry_whereSpecified (e)))
836 /* Respecification errors already reported */
837 DPRINTF (("Respecification: %s / %s",
838 fileloc_unparse (uentry_whereSpecified (e)),
839 bool_unparse (fileloc_isSpec (uentry_whereSpecified (e)))));
841 if (uentry_isDatatype (e))
843 uentry_setDatatype (e, eindex);
846 if (cstringTable_isDefined (st->htable))
848 cstringTable_replaceKey (st->htable,
850 cstring_copy (uentry_rawName (e)));
853 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
855 st->entries[eindex] = e;
860 else /* e not specified */
862 DPRINTF (("Merging..."));
864 if (uentry_isDeclared (ce))
867 No - this can happen for internal typedefs
868 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
871 DPRINTF (("Merge defn"));
872 uentry_mergeDefinition (ce, e);
877 No - this can happen for internal typedefs
878 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
881 DPRINTF (("Merge entries..."));
882 uentry_mergeEntries (ce, e);
883 DPRINTF (("After: %s", uentry_unparseFull (ce)));
887 else /* ce not specified */
889 if (!(st->lexlevel > fileScope || !sRef_modInFunction ()))
891 if (uentry_isDatatype (e) || uentry_isAnyTag (e)
892 || uentry_isEnumConstant (e))
895 ** Not a bug. Code like,
896 ** int f (void) { typedef int tint; ... }
902 llcontbug (message ("usymtab_supEntryAux: inconsistent state "
903 "(lexlevel = %d, modFunction = %s) adding: %q",
904 st->lexlevel, bool_unparse (sRef_modInFunction ()),
905 uentry_unparse (e)));
907 if (sRef_modInFunction ())
909 /* make sure the sRef is not bashed! */
910 /*@i@*/ e->sref = sRef_saveCopy (e->sref);
915 DPRINTF (("Merge.."));
916 uentry_mergeDefinition (ce, e);
921 ctype ct = uentry_getType (ce);
923 if (uentry_isFunction (ce) && ctype_isFunction (ct))
925 ct = ctype_getReturnType (ct);
928 uentry_setSref (ce, sRef_makeCvar (st->lexlevel, eindex, ct, stateInfo_makeLoc (uentry_whereLast (ce))));
931 else /* no previous entry */
935 if (st->lexlevel == globScope
936 && !uentry_isStatic (e)
937 && !uentry_isExtern (e)
938 && usymtab_isDefined (filetab))
941 ** check if there is a static entry: (i.e.,
948 eindex = usymtab_getIndex (filetab, ename);
950 if (eindex != NOT_FOUND)
952 uentry ce = filetab->entries[eindex];
954 uentry_setStatic (e);
955 uentry_mergeDefinition (ce, e);
961 outer = usymtab_lookupQuiet (st->env, ename);
963 DPRINTF (("New : [%p] %s", e, uentry_unparseFull (e)));
964 DPRINTF (("Outer: [%p] %s", outer, uentry_unparseFull (outer)));
967 ** no previous definition, add the new one
971 && uentry_isValid (outer)
972 && !(uentry_isYield (e) || uentry_isYield (outer))
973 && fileloc_isDefined (uentry_whereLast (e))
974 && !fileloc_isXHFile (uentry_whereLast (e))
975 && fileloc_isDefined (uentry_whereLast (outer))
976 && !fileloc_isXHFile (uentry_whereLast (outer)))
978 if (!uentry_sameKind (outer, e))
984 if (ctype_isUnknown (uentry_getType (outer))
985 || uentry_isForward (outer))
993 message ("%s %q shadows outer declaration",
994 ekind_capName (uentry_getKind (e)),
996 uentry_whereLast (e)))
998 uentry_showWhereLast (outer);
1004 if (st == globtab && context_getFlag (FLG_NEWDECL))
1008 message ("New declaration: %q", uentry_getName (e)),
1009 uentry_whereLast (e));
1012 eindex = usymtab_addEntryAux (st, e, isSref);
1016 return (staticEntry ? USYMIDINVALID : eindex);
1021 usymtab_replaceEntryAux (/*@notnull@*/ usymtab st, /*@only@*/ uentry e)
1022 /*@globals globtab@*/ /*@modifies st, e@*/
1024 cstring ename = uentry_rawName (e);
1027 /* static tags in global scope */
1028 eindex = usymtab_getIndex (st, ename);
1030 if (eindex != NOT_FOUND)
1032 uentry ce = st->entries[eindex];
1034 if (cstringTable_isDefined (st->htable))
1036 cstringTable_replaceKey (st->htable, uentry_rawName (ce),
1037 cstring_copy (uentry_rawName (e)));
1041 st->entries[eindex] = e;
1045 eindex = usymtab_addEntryAux (st, e, FALSE);
1052 void usymtab_supEntry (uentry e)
1053 /*@globals utab, filetab, globtab@*/
1054 /*@modifies utab, globtab, e@*/
1056 (void) usymtab_supEntryAux (utab, e, FALSE);
1060 ** this should be lots more efficient!
1063 static /*@exposed@*/ uentry
1064 usymtab_supEntryReturnAux (/*@notnull@*/ usymtab tab,
1065 /*@only@*/ uentry e, bool isref)
1066 /*@globals globtab, filetab@*/
1067 /*@modifies tab, globtab, e@*/
1069 cstring rawName = cstring_copy (uentry_rawName (e));
1070 bool stat = (tab == globtab) && uentry_isStatic (e);
1073 (void) usymtab_supEntryAux (tab, e, isref);
1077 ret = usymtab_lookupAux (filetab, rawName);
1081 ret = usymtab_lookupAux (tab, rawName);
1083 if (uentry_isInvalid (ret) && usymtab_isDefined (filetab))
1085 ret = usymtab_lookupAux (filetab, rawName);
1089 cstring_free (rawName);
1093 /*@dependent@*/ /*@exposed@*/ uentry
1094 usymtab_supEntryReturn (/*@only@*/ uentry e)
1095 /*@globals utab, filetab, globtab@*/
1096 /*@modifies utab, globtab, e@*/
1098 return (usymtab_supEntryReturnAux (utab, e, FALSE));
1101 /*@dependent@*/ /*@exposed@*/ uentry
1102 usymtab_supEntrySrefReturn (/*@only@*/ uentry e)
1103 /*@globals utab, globtab, filetab@*/
1104 /*@modifies utab, globtab, e@*/
1106 return (usymtab_supEntryReturnAux (utab, e, TRUE));
1109 /*@dependent@*/ /*@exposed@*/ uentry
1110 usymtab_supGlobalEntryReturn (uentry e)
1111 /*@globals globtab, filetab@*/
1112 /*@modifies globtab, e@*/
1116 ret = usymtab_supEntryReturnAux (globtab, e, FALSE);
1119 ** We need to keep track of internal function declarations, so
1120 ** we can remove them from the symbol table after exiting this
1121 ** function. This is a bit bogus, of course.
1124 if (sRef_modInFunction ())
1126 recordFunctionType (ret);
1133 usymtab_supTypeEntry (/*@only@*/ uentry e)
1134 /*@globals globtab, filetab@*/
1135 /*@modifies globtab, e@*/
1140 if (uentry_isAbstractDatatype (e))
1142 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1143 ret = ctype_createAbstract (uid);
1147 uid = usymtab_supEntryAux (globtab, e, FALSE);
1148 ret = ctype_createUser (uid);
1151 if (sRef_modInFunction ())
1153 recordFunctionType (globtab->entries[uid]);
1160 usymtab_supReturnTypeEntry (/*@only@*/ uentry e)
1161 /*@globals globtab, filetab@*/
1162 /*@modifies globtab@*/
1166 DPRINTF (("Abstract? %s", uentry_unparseFull (e)));
1168 if (uentry_isAbstractDatatype (e))
1170 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1172 else if (uentry_isMaybeAbstract (e) && context_getFlag (FLG_IMPABSTRACT))
1174 bool maybeabs = TRUE;
1175 cstring sname = uentry_getName (e);
1176 uentry ue = usymtab_lookupGlobSafe (sname);
1177 cstring_free (sname);
1179 if (uentry_isValid (ue))
1181 DPRINTF (("Lookup: %s", uentry_unparseFull (ue)));
1183 if (uentry_isDatatype (ue))
1185 if (uentry_isMaybeAbstract (ue))
1196 DPRINTF (("Not datatype!"));
1203 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1204 ux = usymtab_getTypeEntry (uid);
1205 uentry_setAbstract (ux);
1209 uid = usymtab_supEntryAux (globtab, e, FALSE);
1210 e = usymtab_getTypeEntry (uid);
1212 if (uentry_isMaybeAbstract (e))
1214 uentry_setConcrete (e);
1220 uid = usymtab_supEntryAux (globtab, e, FALSE);
1221 e = usymtab_getTypeEntry (uid);
1223 if (uentry_isMaybeAbstract (e))
1225 uentry_setConcrete (e);
1229 if (sRef_modInFunction ())
1231 recordFunctionType (globtab->entries[uid]);
1234 return (globtab->entries[uid]);
1238 usymtab_supAbstractTypeEntry (/*@only@*/ uentry e, bool dodef)
1239 /*@globals globtab, filetab@*/
1240 /*@modifies globtab, e@*/
1245 uid = usymtab_supEntryAux (globtab, e, FALSE);
1246 ue = usymtab_getTypeEntry (uid);
1250 uentry_setDatatype (ue, uid);
1253 if (context_getFlag (FLG_ACCESSMODULE)) /* was accessfile */
1255 context_addFileAccessType (uid);
1258 if (sRef_modInFunction ())
1260 recordFunctionType (globtab->entries[uid]);
1268 usymtab_supExposedTypeEntry (/*@only@*/ uentry e, bool dodef)
1269 /*@globals globtab, filetab@*/
1270 /*@modifies globtab, e@*/
1274 uid = usymtab_supEntryAux (globtab, e, FALSE);
1278 uentry ue = usymtab_getTypeEntry (uid);
1280 uentry_setDatatype (ue, uid);
1283 if (sRef_modInFunction ())
1285 recordFunctionType (globtab->entries[uid]);
1293 usymtab_supForwardTypeEntry (/*@only@*/ uentry e)
1294 /*@globals globtab, filetab@*/
1295 /*@modifies globtab, e@*/
1297 usymId uid = usymtab_supEntryAux (globtab, e, FALSE);
1298 uentry ue = usymtab_getTypeEntry (uid);
1300 uentry_setDatatype (ue, uid);
1302 if (sRef_modInFunction ())
1304 recordFunctionType (globtab->entries[uid]);
1307 return (uentry_getAbstractType (ue));
1311 usymtab_supEntrySref (uentry e)
1312 /*@globals utab, globtab, filetab@*/
1313 /*@modifies utab, globtab, e@*/
1315 sRef old = uentry_getSref (e);
1318 if (sRef_isType (old))
1320 uentry ue = usymtab_supEntryReturnAux (utab, e, TRUE);
1323 if (uentry_isValid (ue))
1325 sRef uref = uentry_getSref (ue);
1327 sRef_mergeStateQuiet (uref, old);
1328 sRef_clearDerived (uref);
1330 /*@noaccess uentry@*/
1332 else if (sRef_isKnown (old))
1334 usymtab_supEntry (e);
1338 (void) usymtab_supEntryAux (utab, e, TRUE);
1342 void usymtab_supGlobalEntry (/*@only@*/ uentry e)
1343 /*@globals globtab, filetab@*/
1344 /*@modifies globtab, filetab, e@*/
1348 DPRINTF (("Sup global entry: %s", uentry_unparse (e)));
1350 uid = usymtab_supEntryAux (globtab, e, FALSE);
1352 if (sRef_modInFunction ())
1354 recordFunctionType (globtab->entries[uid]);
1359 usymtab_supReturnFileEntry (/*@only@*/ uentry e)
1360 /*@globals filetab, globtab@*/
1361 /*@modifies filetab, globtab, e@*/
1363 llassert (filetab != usymtab_undefined);
1364 DPRINTF (("File entry: %s", uentry_unparse (e)));
1365 return (usymtab_supEntryReturnAux (filetab, e, FALSE));
1373 usymtab_inDeepScope () /*@globals utab@*/
1375 return (utab->lexlevel > paramsScope);
1379 usymtab_getIndex (/*@notnull@*/ usymtab s, cstring k)
1383 DPRINTF (("Lookup %s", k));
1385 if (cstringTable_isDefined (s->htable))
1387 i = cstringTable_lookup (s->htable, k);
1392 for (i = 0; i < s->nentries; i++)
1394 uentry current = s->entries[i];
1396 DPRINTF (("Check %d: %s", i, uentry_rawName (current)));
1398 if (!uentry_isUndefined (current)
1399 && cstring_equal (uentry_rawName (current), k))
1410 usymtab_fetchIndex (/*@notnull@*/ usymtab s, int i)
1412 llassert (i >= 0 && i < s->nentries);
1413 return (s->entries[i]);
1417 usymtab_getTypeId (cstring k) /*@globals globtab@*/
1419 usymId uid = usymtab_getIndex (globtab, k);
1421 if (uid == NOT_FOUND) return USYMIDINVALID;
1423 if (!(uentry_isDatatype (usymtab_getTypeEntry (uid)))) {
1424 return USYMIDINVALID;
1430 /*@dependent@*/ uentry
1431 usymtab_lookupStructTag (cstring k)
1433 cstring sname = makeStruct (k);
1434 uentry ue = usymtab_lookupGlob (sname);
1436 cstring_free (sname);
1440 /*@dependent@*/ uentry
1441 usymtab_lookupUnionTag (cstring k)
1443 cstring uname = makeUnion (k);
1444 uentry res = usymtab_lookupGlob (uname);
1446 cstring_free (uname);
1450 /*@dependent@*/ uentry
1451 usymtab_lookupEnumTag (cstring k)
1453 cstring ename = makeEnum (k);
1454 uentry res = usymtab_lookupGlob (ename);
1456 cstring_free (ename);
1461 usymtab_getId (cstring k) /*@globals globtab@*/
1463 usymId uid = usymtab_getIndex (globtab, k);
1466 if (uid == NOT_FOUND)
1468 return USYMIDINVALID;
1471 ue = usymtab_getGlobalEntry (uid);
1473 if (uentry_isPriv (ue))
1475 return USYMIDINVALID;
1481 static /*@exposed@*/ uentry
1482 usymtab_getEntryAux (/*@notnull@*/ usymtab s, usymId uid)
1484 llassert (uid != USYMIDINVALID);
1486 if (uid < 0 || uid >= s->nentries)
1488 llcontbug (message ("usymtab_getEntry: out of range: level = %d [%d]",
1490 return uentry_undefined;
1493 llassertprint (uentry_isValid (s->entries[uid]),
1494 ("entry undefined: %d", uid));
1496 return s->entries[uid];
1499 /*@dependent@*/ /*@observer@*/ uentry
1500 usymtab_getGlobalEntry (usymId uid)
1501 /*@globals utab, globtab@*/
1503 if (dbgfree) return (uentry_undefined);
1505 if (utab->lexlevel > paramsScope)
1507 /* need to do this the awkward way, since it could be in conditional scope */
1508 return (usymtab_lookupSafe (uentry_rawName (globtab->entries[uid])));
1512 return (globtab->entries[uid]);
1516 /*@dependent@*/ /*@exposed@*/ uentry
1517 usymtab_getTypeEntry (usymId uid)
1518 /*@globals globtab@*/
1522 if (uid >= 0 && uid < globtab->nentries)
1524 return (globtab->entries[uid]);
1528 return (uentry_undefined);
1533 llassert (uid >= 0 && uid < globtab->nentries);
1534 return (globtab->entries[uid]);
1542 /*@dependent@*/ /*@exposed@*/ uentry
1543 usymtab_getTypeEntrySafe (usymId uid)
1544 /*@globals globtab@*/
1546 if (uid < 0 || uid >= globtab->nentries)
1548 return uentry_undefined;
1551 return (globtab->entries[uid]);
1555 usymtab_isBoolType (usymId uid)
1556 /*@globals globtab@*/
1558 llassert (uid >= 0 && uid < globtab->nentries);
1560 return (cstring_equal (uentry_rawName (globtab->entries[uid]),
1561 context_getBoolName ()));
1565 usymtab_getTypeEntryName (usymId uid)
1566 /*@globals globtab@*/
1572 return (cstring_makeLiteral ("<freetype>"));
1575 ue = usymtab_getTypeEntry (uid);
1577 if (dbgload && !uentry_isValid (ue))
1579 return (message ("<missing type: %d>", uid));
1582 llassertprint (uentry_isValid (ue), ("type undefined: %d", uid));
1584 return (uentry_getName (ue));
1588 /*@unused@*/ static void
1589 usymtab_rehash (/*@notnull@*/ usymtab s)
1593 if (cstringTable_isDefined (s->htable))
1595 cstringTable_free (s->htable);
1598 s->htable = cstringTable_create (LLHASHSIZE);
1600 for (i = 0; i < s->nentries; i++)
1602 cstringTable_insert (s->htable, cstring_copy (uentry_rawName (s->entries[i])), i);
1608 ** superficial copy of usymtab
1610 ** DO copy spec entries
1613 static /*@only@*/ /*@notnull@*/ usymtab
1614 usymtab_shallowCopy (/*@notnull@*/ usymtab s) /*@*/
1616 usymtab copytab = usymtab_createRoot ();
1619 for (i = 0; i < s->nentries; i++)
1621 usymtab_addEntryBase (copytab, s->entries[i]);
1628 usymtab_shallowFree (/*@only@*/ /*@notnull@*/ usymtab s)
1630 aliasTable_free (s->aliases);
1631 refTable_free (s->reftable, s->nentries);
1633 /*@-compdestroy@*/ sfree (s); /*@=compdestroy@*/
1637 ** converts usymId from old table to sorted one
1641 usymtab_convertId (usymId uid)
1642 /*@globals oldtab, utab@*/
1648 llassert (usymtab_isDefined (oldtab));
1650 ue = usymtab_getEntryAux (oldtab, uid);
1652 llassertprint (uentry_isValid (ue), ("convertId: undefined: %d", uid));
1654 name = uentry_rawName (ue);
1656 ret = usymtab_getIndex (utab, name);
1657 llassert (ret == uid); /*! for now, no rehash! */
1658 DPRINTF (("Convert: %s [%d] -> %s [%d]",
1659 uentry_unparse (ue), uid,
1660 uentry_unparse (utab->entries[ret]), ret));
1662 llassertprint (ret != USYMIDINVALID, ("convertId: return is invalid"));
1668 usymtab_prepareDump (void)
1669 /*@globals oldtab, utab@*/
1670 /*@modifies oldtab, utab@*/
1672 llassert (usymtab_inGlobalScope ());
1673 llassert (oldtab == usymtab_undefined);
1676 DPRINTF (("Preparing to dump:"));
1677 usymtab_printAll ();
1680 oldtab = usymtab_shallowCopy (utab);
1683 ** alpha compare - make sure order is same on different platforms
1684 ** error messages appear in same order
1688 qsort (utab->entries, (size_t)utab->nentries,
1689 sizeof (*utab->entries),
1690 (int (*)(const void *, const void *)) uentry_xcomparealpha);
1692 usymtab_rehash (utab);
1696 DPRINTF (("After rehash:"));
1697 usymtab_printAll ();
1701 void usymtab_dump (FILE *fout)
1702 /*@globals utab, oldtab@*/
1705 bool neednl = FALSE;
1706 uentry lastentry = uentry_undefined;
1707 ekind lastekind = KINVALID;
1711 ** must call prepareDump first
1714 llassert (oldtab != usymtab_undefined);
1716 for (i = 0; i < utab->nentries; i++)
1718 uentry thisentry = utab->entries[i];
1719 ekind thisekind = uentry_getKind (thisentry);
1721 if (!uentry_hasRealName (thisentry))
1723 llassert (uentry_isGlobalMarker (thisentry));
1727 check (fputc ('\n', fout) == (int) '\n');
1730 fprintf (fout, "*%d (GlobalMarker)\n", KGLOBALMARKER);
1731 lastekind = KINVALID;
1737 if (thisekind != lastekind)
1741 check (fputc ('\n', fout) == (int) '\n');
1745 lastentry = uentry_undefined;
1746 fprintf (fout, "*%d (%s)\n", ekind_toInt (thisekind),
1747 cstring_toCharsSafe (ekind_capName (thisekind)));
1748 lastekind = thisekind;
1753 ** evans - 2001-02-18 - added the - 48 fudge factor...
1754 ** extra characters dumped, but I haven't counded them carefully...
1757 if (uentry_isInvalid (lastentry) || !uentry_equiv (lastentry, thisentry)
1758 || (linelen > (MAX_DUMP_LINE_LENGTH - (2 * MAX_NAME_LENGTH) - 48)))
1762 DPRINTF (("Dumping entry: %d", i));
1763 cdump = message ("^%d %q", i, uentry_dump (thisentry));
1764 /* was: cdump = uentry_dump (thisentry)); */
1766 lastentry = thisentry;
1769 check (fputc ('\n', fout) == (int) '\n');
1773 linelen += cstring_length (cdump);
1775 /* no new line here! */
1776 if (cstring_length (cdump) > 0)
1778 check (fputs (cstring_toCharsSafe (cdump), fout) != EOF);
1781 cstring_free (cdump);
1786 cstring cdump = uentry_rawName (thisentry);
1787 DPRINTF (("Raw name: %s", cdump));
1788 linelen += (cstring_length (cdump) + 1);
1789 fprintf (fout, "#%s", cstring_toCharsSafe (cdump));
1796 check (fputc ('\n', fout) == (int) '\n');
1799 lastekind = KINVALID;
1801 fprintf(fout, ";; Library constraints\n");
1802 for (i = 0; i < utab->nentries; i++)
1804 uentry thisentry = utab->entries[i];
1806 if (uentry_isFunction (thisentry) )
1808 constraintList preconditions;
1809 constraintList postconditions;
1811 preconditions = uentry_getFcnPreconditions (thisentry);
1812 postconditions = uentry_getFcnPostconditions (thisentry);
1814 if ( constraintList_isDefined(preconditions) ||
1815 constraintList_isDefined(postconditions) )
1817 fprintf(fout,"%s\n", cstring_toCharsSafe (uentry_rawName(thisentry) ) );
1818 if (constraintList_isDefined(preconditions) )
1820 fprintf(fout,"pre:\n");
1821 constraintList_dump(preconditions, fout);
1822 fprintf (fout, ";; end precondition constraints\n" );
1823 constraintList_free(preconditions);
1827 fprintf(fout,"pre:EMPTY\n");
1829 if (constraintList_isDefined(postconditions) )
1831 fprintf(fout,"post:\n");
1832 constraintList_dump(postconditions, fout);
1833 fprintf (fout, ";; end precondition constraints\n" );
1834 constraintList_free(postconditions);
1838 fprintf(fout,"post:EMPTY\n");
1847 void usymtab_load (FILE *f)
1848 /*@globals utab, globtab@*/
1849 /*@modifies utab, *f@*/
1851 char *s = mstring_create (MAX_DUMP_LINE_LENGTH);
1853 ekind kind = KINVALID;
1854 fileloc loc = g_currentloc;
1860 llassert (utab == globtab);
1861 llassert (utab->nentries == 0);
1863 while (((s = reader_readLine (f, s, MAX_DUMP_LINE_LENGTH)) != NULL)
1866 /* ignore ; comments */ ;
1869 while (s != NULL && *s != ';')
1877 ek = reader_getInt (&s);
1879 if (ek == KGLOBALMARKER)
1881 uentry lue = uentry_makeGlobalMarker ();
1882 DPRINTF (("Adding global marker: %s", uentry_unparseFull (lue)));
1883 usymtab_addEntryAlways (utab, lue);
1889 kind = ekind_fromInt (ek);
1897 (cstring_makeLiteral
1898 ("Library is in obsolete format. Use splint +whichlib "
1899 "to see which library is being loaded."));
1902 if (reader_optCheckChar (&s, '^'))
1904 index = reader_getInt (&s);
1911 llassert (kind != KINVALID);
1912 ue = uentry_undump (kind, loc, &s);
1914 llassert (utab->nentries == index || index == -1);
1916 if (uentry_isValid (ue))
1918 int lastindex = utab->nentries;
1919 ue = usymtab_addEntryAlways (utab, ue);
1923 if (uentry_isConstant (ue)) /*@i23! isPreProcessorMacro */
1925 cstring uname = uentry_getName (ue);
1927 /* Also check its a macro... */
1928 DPRINTF (("Installing: %s", uname));
1930 cpphash_installMacro
1931 (mstring_copy (cstring_toCharsSafe (uname)),
1932 cstring_length (uname),
1933 cpplib_createDefinition (message ("%s 255", uname),
1936 cpphash_hashCode (cstring_toCharsSafe (uname),
1937 cstring_length (uname),
1940 DPRINTF (("After install: %s", uname));
1944 if (utab->nentries != lastindex + 1)
1946 DPRINTF (("No add: %s", uentry_unparseFull (ue)));
1954 ** now, any other names are the same uentry
1957 while (*(s++) == '#')
1959 cstring name = cstring_fromCharsO (reader_getWord (&s));
1960 uentry nue = uentry_nameCopy (name, ue);
1962 DPRINTF (("Name copy: %s", uentry_unparseFull (nue)));
1966 usymtab_addEntryAlways (utab, nue);
1969 while ((c = *s) != '\0' && (c !='\n'))
1971 if (c != ' ' || c != '\t')
1973 llbuglit ("Junk in load file");
1981 s = reader_readLine (f, os, MAX_DUMP_LINE_LENGTH);
1986 to handle reading of buffer overflow related constraints
1988 while (fgets (s, MAX_DUMP_LINE_LENGTH, f) != NULL
1991 ; /* ignore ;-comments */
1994 while (s != NULL && *s != ';')
1996 constraintList preconditions;
1997 constraintList postconditions;
1999 cstring name = cstring_fromChars(reader_getWord(&s) );
2001 ue = usymtab_lookup ( name );
2005 preconditions = constraintList_undefined;
2006 postconditions = constraintList_undefined;
2008 if (!uentry_isValid(ue) )
2010 llfatalbug ((message("Invalid uentry for %s library file may be corrupted", cstring_fromChars(s) ) ));
2012 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
2014 temp = cstring_fromChars (reader_getWord(&s) );
2016 if (cstring_compareLit (temp,"pre:") == 0 )
2018 preconditions = constraintList_undump (f);
2022 if (cstring_compareLit (temp,"pre:EMPTY") != 0 )
2023 llfatalbug ((message("Error reading library file pre:EMPTY expected but got %s", temp ) ));
2028 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
2030 temp = cstring_fromChars(reader_getWord(&s) );
2031 if (cstring_compareLit (temp, "post:") == 0 )
2033 postconditions = constraintList_undump (f);
2037 if (cstring_compareLit (temp, "post:EMPTY") != 0 )
2038 llfatalbug ((message("Error reading library file post:EMPTY expected but got %s", temp ) ));
2041 cstring_free (temp);
2043 uentry_setPreconditions (ue, functionConstraint_createBufferConstraint (preconditions));
2044 uentry_setPostconditions (ue, functionConstraint_createBufferConstraint (postconditions));
2046 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
2054 ** file scope for static variables
2058 usymtab_enterFile ()
2059 /*@globals utab, globtab, filetab@*/
2060 /*@modifies filetab@*/
2062 llassert (utab == globtab);
2067 usymtab_entries (globtab, ue)
2069 if (sRef_hasDerived (uentry_getSref (ue)))
2071 fprintf (g_msgstream, "Derived Global: %s\n", uentry_unparse (ue));
2072 fprintf (g_msgstream, "sRef: %s\n", sRef_unparseFull (ue->sref));
2074 } end_usymtab_entries ;
2078 usymtab_enterScope ();
2084 /*@globals utab, filetab@*/
2085 /*@modifies filetab, utab@*/
2088 llassert (utab->lexlevel == 1);
2090 usymtab_exitScope (exprNode_undefined);
2095 usymtab_enterScope ()
2096 /*@globals utab, globtab, filetab@*/
2099 usymtab t = usymtab_create (US_NORMAL, utab, TRUE);
2101 /* unconditional scope: optimize to avoid copy */
2102 t->aliases = aliasTable_copy (utab->aliases);
2105 llassert (usymtab_isDefined (t->env));
2107 if (t->env->lexlevel == paramsScope && context_inFunctionLike ())
2109 noshadowerror = TRUE;
2110 usymtab_handleParams ();
2111 noshadowerror = FALSE;
2116 ** setup external references:
2117 ** o only, unique params alias external args
2118 ** o other params may alias anything of their type
2122 usymtab_handleParams (void)
2123 /*@globals utab, globtab, filetab@*/
2124 /*@modifies utab, globtab@*/
2126 usymtab ptab = utab->env;
2127 uentry fcn = context_getHeader ();
2129 usymtab_entries (ptab, param)
2133 if (!uentry_isYield (param))
2136 sRef pref = uentry_getSref (param);
2138 /* Could be a global. */
2140 if (uentry_isAnyParam (param))
2142 ue = uentry_makeVariable (fixParamName (uentry_rawName (param)),
2143 uentry_getType (param),
2144 fileloc_copy (uentry_whereDeclared (param)),
2147 uentry_copyState (ue, param);
2148 uentry_setRefParam (ue);
2150 ue = usymtab_supEntrySrefReturn (ue);
2152 /* must be after supercede! */
2154 if (!sRef_stateKnown (pref))
2156 uentry_setDefState (ue, SS_DEFINED);
2157 uentry_setDefState (param, SS_DEFINED);
2161 if (sRef_isStateSpecial (pref))
2163 uentry_setDefState (ue, SS_SPECIAL); /* ALLOCATED); */
2164 /* evans 2002-01-01: should be unnecessary, the pre clauses
2165 ** set the state if necessary.
2170 uentry_setDefState (ue, sRef_getDefState (pref));
2174 uref = uentry_getSref (ue);
2176 if (sRef_isStack (uref))
2178 alkind pkind = sRef_getAliasKind (pref);
2180 if (alkind_isKnown (pkind) && !alkind_isLocal (pkind)
2181 && !alkind_isStack (pkind))
2183 sRef_setAliasKind (uref, pkind, fileloc_undefined);
2184 sRef_setOrigAliasKind (uref, pkind);
2188 sRef_setAliasKind (uref, AK_IMPTEMP, fileloc_undefined);
2189 sRef_setOrigAliasKind (uref, AK_IMPTEMP);
2191 if (uentry_isOut (param))
2197 sRef_setDefined (uref, fileloc_undefined);
2203 usymtab_addMustAlias (uref, pref);
2205 if (!(uentry_isOnly (param) || uentry_isUnique (param)))
2208 ** This is needed for detecting possibly aliased parameters.
2211 sRef s = sRef_makeExternal (uref);
2212 usymtab_addMustAlias (uref, s);
2215 if (sRef_isKillRef (pref))
2217 sRef_setAliasKind (uref, AK_NEWREF, fileloc_undefined);
2218 sRef_setOrigAliasKind (uref, AK_KILLREF);
2220 else if (sRef_isRefCounted (uref))
2222 sRef_setOrigAliasKind (uref, AK_REFCOUNTED);
2227 sRef_setOrigAliasKind (uref, AK_LOCAL);
2234 } end_usymtab_entries;
2237 if (uentry_hasStateClauseList (fcn))
2239 stateClauseList clauses = uentry_getStateClauseList (fcn);
2241 stateClauseList_preElements (clauses, cl)
2243 fileloc loc = stateClause_loc (cl);
2244 sRefSet osrs = sRefSet_undefined;
2247 if (stateClause_isGlobal (cl))
2249 DPRINTF (("Global Marker: %s",
2250 sRef_unparseFull (usymtab_lookupGlobalMarker ())));
2251 llassert (sRef_isGlobalMarker (usymtab_lookupGlobalMarker ()));
2252 srs = sRefSet_single (usymtab_lookupGlobalMarker ());
2257 srs = stateClause_getRefs (cl);
2260 sRefSet_elements (srs, el)
2262 sRef base = sRef_getRootBase (el);
2263 sRef sb = sRef_updateSref (el);
2265 if (sRef_isResult (base))
2267 ; /* nothing to do before */
2269 else if (sRef_isParam (base) || sRef_isGlobalMarker (base))
2271 if (stateClause_setsMetaState (cl))
2273 /* copied from exprNode.c:3040 */
2274 qual ql = stateClause_getMetaQual (cl);
2275 annotationInfo ainfo = qual_getAnnotationInfo (ql);
2276 metaStateInfo minfo = annotationInfo_getState (ainfo);
2277 cstring key = metaStateInfo_getName (minfo);
2278 int mvalue = annotationInfo_getValue (ainfo);
2280 DPRINTF (("Sets meta state! %s", stateClause_unparse (cl)));
2282 if (sRef_isResult (base))
2288 sRef_setMetaStateValueComplete (sb, key, mvalue, loc);
2293 sRefMod modf = stateClause_getEntryFunction (cl);
2297 sRefSet aliases = usymtab_allAliases (sb);
2301 sRefSet_elements (aliases, sr)
2304 } end_sRefSet_elements ;
2306 sRefSet_free (aliases);
2312 if (sRef_isValid (base))
2314 DPRINTF (("Base: %s", sRef_unparseFull (base)));
2318 } end_sRefSet_elements ;
2319 } end_stateClauseList_preElements ;
2324 usymtab_enterFunctionScope (uentry fcn)
2325 /*@globals utab, filetab, globtab@*/
2328 usymtab t = usymtab_create (US_NORMAL, utab, TRUE);
2330 if (utab->lexlevel != fileScope)
2332 if (utab->lexlevel > fileScope)
2334 llparseerror (cstring_makeLiteral ("New function scope inside function"));
2336 while (utab->lexlevel > fileScope)
2338 /*@i@*/ utab = usymtab_dropEnv (utab);
2345 llfatalbug (cstring_makeLiteral ("New function not inside file."));
2347 /*@-branchstate@*/ } /*@=branchstate@*/
2351 DPRINTF (("Globs: %s", globSet_unparse (uentry_getGlobs (fcn))));
2353 globSet_allElements (uentry_getGlobs (fcn), el)
2355 DPRINTF (("Here we go: %s", sRef_unparseFull (el)));
2357 if (sRef_isUndefGlob (el))
2359 int index = sRef_getScopeIndex (el);
2360 sRef sr = sRef_updateSref (el);
2361 fileloc loc = uentry_whereEarliest (fcn);
2363 DPRINTF (("update: %s", sRef_unparseFull (sr)));
2364 DPRINTF (("Undef!"));
2365 if (sRef_isFileStatic (el))
2367 ctype ct = sRef_getType (el);
2370 llassert (usymtab_isDefined (filetab));
2372 ue = usymtab_fetchIndex (filetab, index);
2374 if (ctype_isRealArray (ct) || ctype_isRealSU (ct))
2376 sRef_setAllocated (sr, loc);
2380 sRef_setUndefined (sr, loc);
2385 uentry ue = globtab->entries[index];
2386 ctype ct = uentry_getType (ue);
2388 if (ctype_isArray (ct) || ctype_isSU (ct))
2390 sRef_setAllocated (sr, loc);
2394 sRef_setUndefined (sr, loc);
2398 else if (sRef_isAllocated (el))
2400 sRef sr = sRef_updateSref (el);
2401 fileloc loc = uentry_whereEarliest (fcn);
2403 sRef_setAllocated (sr, loc);
2405 else if (sRef_isPartial (el))
2407 sRef sr = sRef_updateSref (el);
2408 fileloc loc = uentry_whereEarliest (fcn);
2410 sRef_setPartial (sr, loc);
2415 sRef sr = sRef_updateSref (el);
2416 fileloc loc = uentry_whereEarliest (fcn);
2418 sRef_setDefined (sr, loc);
2422 /* shouldn't need to do anything! */
2424 } end_globSet_allElements;
2426 DPRINTF (("Globs after: %s", globSet_unparse (uentry_getGlobs (fcn))));
2430 usymtab_caseBranch (void)
2433 usymtab t = usymtab_create (US_CBRANCH, utab, FALSE);
2438 usymtab_switchBranch (/*@unused@*/ exprNode s)
2441 usymtab t = usymtab_create (US_SWITCH, utab, FALSE);
2443 t->aliases = aliasTable_copy (utab->aliases);
2448 usymtab_trueBranch (/*@only@*/ guardSet guards)
2451 usymtab t = usymtab_create (US_TBRANCH, utab, FALSE);
2454 ** not true! (could be in a macro)
2456 ** llassertprint (utab->lexlevel > paramsScope,
2457 ** ("not in scope: %s", usymtab_unparseLocal ()));
2461 guardSet_free (t->guards);
2464 aliasTable_free (t->aliases);
2465 t->aliases = aliasTable_copy (utab->aliases);
2473 ** { int a; if (...) a = 3; < a may be undefined here!
2478 usymtab_popTrueBranch (exprNode pred, exprNode expr, clause cl) /*@modifies utab@*/
2481 ** add a false branch
2482 ** (could be done more efficiently as a special case, but
2483 ** it is better to only maintain one version of the code)
2486 if (utab->kind != US_TBRANCH
2487 && context_inIterDef ())
2489 usymtab_exitScope (expr);
2493 DPRINTF (("pop true branch.."));
2494 usymtab_altBranch (guardSet_invert (exprNode_getGuards (pred)));
2495 usymtab_popBranches (pred, expr, exprNode_undefined, TRUE, cl);
2500 usymtab_popCaseBranch () /*@modifies utab@*/
2502 llassert (utab->kind == US_CBRANCH);
2503 usymtab_quietPlainExitScope ();
2507 usymtab_popTrueExecBranch (exprNode pred, exprNode expr, clause cl)
2510 ** add a false branch that must return --- that is,
2511 ** the true branch is always executed!
2514 usymtab_altBranch (guardSet_invert (exprNode_getGuards (pred)));
2515 usymtab_popBranches (pred, expr, exprNode_makeMustExit (), FALSE, cl);
2519 usymtab_popOrBranch (exprNode pred, exprNode expr)
2523 usymtab env = utab->env;
2524 usymtab otab = utab;
2527 llassert (env != NULL);
2529 if (exprNode_isError (expr))
2535 mustReturn = exprNode_mustEscape (expr);
2539 llassert (utab->kind == US_TBRANCH);
2542 ** merge each entry in table with its original
2543 ** unless execution cannot continue after this branch
2546 for (i = 0; i < utab->nentries; i++)
2548 uentry current = utab->entries[i];
2549 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
2551 uentry_mergeState (old, current, exprNode_loc (expr),
2552 mustReturn, FALSE, TRUE, ORCLAUSE);
2558 env->guards = guardSet_levelUnionFree (env->guards,
2559 guardSet_invert (exprNode_getGuards (pred)),
2564 env->aliases = aliasTable_levelUnion (env->aliases, otab->aliases, env->lexlevel);
2567 /* env is now utab */
2568 usymtab_quietPlainExitScope ();
2572 ** case syntax in C is very unrestricted. This is unfortunate.
2574 ** A switch case is ended either by a new case or default, or
2575 ** a close } that may close the switch or some other control
2580 usymtab_newCase (/*@unused@*/ exprNode pred, exprNode last)
2583 bool mustBreak = usymtab_mustBreak (utab);
2584 bool mustReturn = usymtab_mustEscape (utab);
2585 usymtab stab = utab;
2587 DPRINTF (("New case!"));
2590 ** Find last case (or outer switch)
2593 while (stab->kind != US_CBRANCH && stab->kind != US_SWITCH)
2596 llassert (stab != GLOBAL_ENV);
2599 while (stab->kind == US_CBRANCH)
2602 llassert (stab != GLOBAL_ENV);
2606 ** if its a fall through case, merge in outside entries and last case.
2611 ** case 1: x = 3; <fall through>
2612 ** case 2: << x may not be defined
2616 if (!mustBreak && !mustReturn && utab->kind == US_CBRANCH)
2618 llassert (stab->kind == US_SWITCH || stab->kind == US_NORMAL);
2620 usymtab_entries (utab, ue) /* but, keep track of used variables */
2622 uentry old = usymtab_lookupAux (stab, uentry_rawName (ue));
2624 llassert (uentry_isValid (old));
2626 /* modifies ue, not old */
2628 uentry_mergeState (ue, old, exprNode_loc (last),
2629 FALSE, FALSE, TRUE, CASECLAUSE);
2630 } end_usymtab_entries;
2632 utab->aliases = aliasTable_levelUnion (utab->aliases,
2633 stab->aliases, utab->lexlevel);
2636 ** No need for a new branch.
2643 usymtab_caseBranch ();
2644 /*@-mustfree@*/ /*< utab->aliases >*/
2645 utab->aliases = aliasTable_copy (stab->aliases);
2653 ** for && (both pred and expr are executed)
2657 usymtab_popAndBranch (exprNode pred, /*@unused@*/ exprNode expr)
2660 usymtab env = utab->env;
2664 llassert (utab->kind == US_TBRANCH);
2667 ** merge each entry in table with its original
2668 ** unless execution cannot continue after this branch
2671 for (i = 0; i < utab->nentries; i++)
2673 uentry current = utab->entries[i];
2674 sRef tref = uentry_getSref (current);
2675 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
2676 sRef oref = uentry_getSref (old);
2678 /* note that is current is in a nested branch,
2679 it may create a "new" old entry. */
2681 llassert (uentry_isValid (old));
2682 uentry_mergeState (old, current, exprNode_loc (expr),
2683 FALSE, FALSE, TRUE, ANDCLAUSE);
2686 ** if is it defined by the second clause, then it should be defined.
2689 if (sRef_isAnyDefined (tref)
2690 && (sRef_isAllocated (oref) || sRef_isStateUndefined (oref)))
2692 sRef_setDefined (oref, g_currentloc);
2696 utab->guards = guardSet_levelUnionFree (utab->guards,
2697 guardSet_invert (exprNode_getGuards (pred)),
2699 utab->aliases = aliasTable_levelUnion (utab->aliases, otab->aliases, utab->lexlevel);
2701 usymtab_quietPlainExitScope ();
2706 ** Stack should be [ US_CBRANCH+ US_SWITCH ]
2707 ** Only branches which do not return (except possibly the last branch) are included.
2709 ** Conditionally merge state from all CBRANCHes.
2711 ** If allpaths is TRUE, then all possible executions go through some switch
2712 ** case, and the original scope is not merged.
2716 usymtab_exitSwitch (/*@unused@*/ exprNode sw, bool allpaths)
2719 usymtab ttab = utab;
2720 usymtab stab = ttab;
2721 usymtab ltab = ttab;
2722 bool lastMustReturn = usymtab_mustEscape (utab);
2726 while (stab->kind == US_CBRANCH)
2729 llassert (stab != GLOBAL_ENV);
2732 while (stab->kind == US_NORMAL)
2735 llassert (stab != GLOBAL_ENV);
2738 llassert (stab->kind == US_SWITCH);
2740 /* go to the scope outside the switch (US_SWITCH is just a marker! */
2742 llassert (stab != GLOBAL_ENV);
2746 llassert (usymtab_isDefined (ttab));
2748 if (ttab->kind == US_CBRANCH)
2750 /* was quietPlainExitScope --- but, can't free it yet! */
2752 llassert (utab != GLOBAL_ENV);
2754 while (ttab->kind == US_CBRANCH)
2757 ** (from popTrueBranch)
2760 bool mustReturn = usymtab_mustEscape (ttab);
2761 bool mustBreak = usymtab_mustBreak (ttab);
2763 usymtab_entries (ttab, current)
2765 uentry old = /*@-compmempass@*/ usymtab_lookupAux (ltab, uentry_rawName (current));
2769 ** note that is this is in a nested branch,
2770 ** it may create a "new" old entry.
2773 if (uentry_isValid (old))
2777 uentry_mergeUses (current, old);
2778 uentry_setState (old, current);
2782 uentry_mergeState (old, current, exprNode_loc (sw),
2783 mustReturn, FALSE, TRUE, SWITCHCLAUSE);
2790 } end_usymtab_entries;
2793 ** if entry is not in symbol table for this case, merge with pre-switch
2797 if (!mustReturn && !mustBreak)
2799 usymtab_entries (stab, current)
2801 if (usymtab_getIndex (ttab, uentry_rawName (current)) == NOT_FOUND)
2803 uentry old = /*@-compmempass@*/
2804 usymtab_lookupAux (ltab, uentry_rawName (current));
2807 llassert (uentry_isValid (old));
2808 uentry_mergeState (old, current, exprNode_loc (sw),
2809 FALSE, FALSE, TRUE, SWITCHCLAUSE);
2811 } end_usymtab_entries;
2814 ltab->env = ttab->env;
2818 ** Suprious error, becuase of environments.
2821 /*@i1@*/ utab = ltab;
2823 lastMustReturn = FALSE;
2830 ** now, there is one US_CBRANCH. Merge this with the stab.
2834 for (i = 0; i < ltab->nentries; i++)
2836 uentry current = ltab->entries[i];
2837 uentry old = usymtab_lookupAux (stab, uentry_rawName (current));
2839 /* note that is this is in a nested branch,
2840 it may create a "new" old entry. */
2843 if (uentry_isValid (old))
2847 uentry_mergeUses (current, old);
2848 uentry_setState (old, current);
2852 uentry_mergeState (old, current, exprNode_loc (sw),
2853 FALSE, FALSE, TRUE, SWITCHCLAUSE);
2867 ** switch may or may not be followed by a new scope
2870 if (utab->kind == US_SWITCH)
2872 usymtab_quietPlainExitScope ();
2876 usymtab_quietPlainExitScope ();
2877 llassert (utab->kind == US_SWITCH);
2878 usymtab_quietPlainExitScope ();
2884 updateNullState (sRef el, /*@notnull@*/ usymtab ttab,
2885 /*@notnull@*/ usymtab ftab, bool trueGuard)
2887 sRef base = sRef_getRootBase (el);
2888 int level = sRef_lexLevel (base);
2890 if (sRef_isCvar (base))
2892 usymId index = sRef_getScopeIndex (base);
2893 uentry ue = usymtab_getRefTab (ttab, level, index);
2895 if (!uentry_isLset (ue))
2897 sRef sr = uentry_getSref (ue);
2901 sRef_setDerivNullState (sr, el, NS_NOTNULL);
2905 if (!guardSet_isGuarded (ttab->guards, el)
2906 && !sRef_isNotNull (sr))
2908 sRef_setDerivNullState (sr, el, NS_DEFNULL);
2916 ue = usymtab_getRefTab (ftab, level, index);
2918 if (!uentry_isLset (ue))
2920 sRef sr = uentry_getSref (ue);
2923 if (!trueGuard) /* yikes! forgot the ! */
2925 sRef_setDerivNullState (sr, el, NS_NOTNULL);
2930 if (!guardSet_isGuarded (ftab->guards, el)
2931 && !sRef_isNotNull (sr))
2933 sRef_setDerivNullState (sr, el, NS_DEFNULL);
2945 usymtab_popBranches (exprNode pred, exprNode tbranch, exprNode fbranch,
2946 bool isOpt, clause cl)
2950 usymtab ftab = utab;
2951 usymtab ttab = utab->env;
2955 guardSet guards = exprNode_getGuards (pred);
2956 sRefSet tguards = guardSet_getTrueGuards (guards);
2957 sRefSet fguards = guardSet_getFalseGuards (guards);
2958 bool mustReturnT = exprNode_mustEscape (tbranch);
2959 bool mustReturnF = exprNode_mustEscape (fbranch);
2961 DPRINTF (("Pop branches: %s [mustreturn: %s/%s]", exprNode_unparse (pred),
2962 bool_unparse (mustReturnT),
2963 bool_unparse (mustReturnF)));
2965 if (exprNode_isDefined (fbranch))
2967 loc = exprNode_loc (fbranch);
2971 loc = exprNode_loc (tbranch);
2974 llassert (usymtab_isDefined (ttab));
2978 llassert (usymtab_isDefined (env));
2979 llassert (ftab->kind == US_FBRANCH);
2980 llassert (ttab->kind == US_TBRANCH);
2983 ** For each element that is true guarded (i.e., if (x != NULL))
2984 ** make x = null in false branch,
2985 ** and x = notnull in true branch.
2986 ** unless x was set locally in that branch.
2987 ** For each element that is false guarded (x == NULL)
2988 ** make x = null in true, notnull in false.
2990 ** For each element that is either guarded (pred(x))
2994 sRefSet_allElements (tguards, el)
2996 updateNullState (el, ttab, ftab, TRUE);
2997 } end_sRefSet_allElements;
2999 sRefSet_allElements (fguards, el)
3001 updateNullState (el, ttab, ftab, FALSE);
3002 } end_sRefSet_allElements;
3006 ** if an entry is in both true and false, merge the entries,
3007 ** then replace original with new state.
3009 ** if an entry is in one table, merge it with the original.
3012 DPRINTF (("ftab: %d", ftab->nentries));
3014 for (i = 0; i < ftab->nentries; i++)
3016 uentry fthis = ftab->entries[i];
3017 uentry old = usymtab_lookupAux (env, uentry_rawName (fthis));
3018 int tindex = usymtab_getIndex (ttab, uentry_rawName (fthis));
3020 DPRINTF (("Entry: %s / %s", uentry_unparseFull (fthis), uentry_unparseFull (old)));
3022 if (uentry_isUndefined (old))
3024 /* possible entry was added as an undefined id */
3025 DPRINTF (("Undefined! %s", uentry_rawName (fthis)));
3029 if (tindex != NOT_FOUND)
3031 uentry tthis = ttab->entries[tindex];
3033 /* note that is this is in a nested branch,
3034 it may create a "new" old entry. */
3040 uentry_mergeState (fthis, tthis, loc,
3041 mustReturnT, FALSE, FALSE, cl);
3045 uentry_mergeUses (fthis, tthis);
3048 uentry_setState (old, fthis);
3055 uentry_setState (old, tthis);
3056 uentry_mergeState (old, fthis, loc, mustReturnF,
3060 ttab->entries[tindex] = uentry_undefined;
3061 uentry_free (tthis);
3065 uentry_mergeState (old, fthis, loc, mustReturnF, TRUE, FALSE, cl);
3069 for (i = 0; i < ttab->nentries; i++)
3071 uentry current = ttab->entries[i];
3073 DPRINTF (("ttab: %s", uentry_unparseFull (current)));
3075 if (!uentry_isUndefined (current))
3077 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
3079 DPRINTF (("Old: %s", uentry_unparseFull (old)));
3081 if (uentry_isUndefined (old))
3083 llcontbug (message ("Undefined entry: %s", uentry_rawName (current)));
3089 uentry_mergeUses (current, old);
3090 uentry_setState (old, current);
3095 ** Assumes false branch is a fall-through if
3096 ** fbranch is not defined. This is true, unless
3097 ** where was some greivous error in processing
3098 ** the else branch of an if-then, in which case
3099 ** this is probably the right thing to do anyway.
3102 uentry_mergeState (old, current, loc, mustReturnT,
3106 DPRINTF (("==> %s", uentry_unparseFull (old)));
3111 ** Plain levelUnion doesn't work, since we need to use the sRef's in env->aliases
3112 ** if they are present.
3115 llassert (NOALIAS (env->aliases, ttab->aliases));
3116 llassert (NOALIAS (env->aliases, ftab->aliases));
3118 aliasTable_free (env->aliases);
3120 env->aliases = aliasTable_levelUnionNew (ttab->aliases,
3121 ftab->aliases, env->lexlevel);
3123 aliasTable_fixSrefs (env->aliases);
3125 DPRINTF (("Aliases: %s", aliasTable_unparse (env->aliases)));
3127 /* exit true and false scopes */
3128 usymtab_quietPlainExitScope ();
3129 usymtab_quietPlainExitScope ();
3133 utab->guards = guardSet_levelUnionFree
3135 guardSet_invert (exprNode_getGuards (pred)),
3141 utab->guards = guardSet_levelUnion (utab->guards,
3142 exprNode_getGuards (pred),
3146 DPRINTF (("Here."));
3149 static void usymtab_fixCases (void) /*@modifies utab@*/ {
3150 while (utab->kind == US_CBRANCH)
3152 usymtab_quietPlainExitScope ();
3155 llassert (utab->kind != US_CBRANCH);
3159 usymtab_altBranch (/*@only@*/ guardSet guards)
3163 usymtab parent = utab->env;
3165 t = usymtab_create (US_FBRANCH, utab, FALSE);
3168 ** If we are in a case, need to close it. The C syntax
3169 ** is very liberal, so this kludge is necessary.
3172 usymtab_fixCases ();
3174 DPRINTF (("Current kind: %s", usymtab_unparseStack ()));
3176 llassert (utab->kind == US_TBRANCH);
3177 llassert (parent != GLOBAL_ENV);
3179 guardSet_free (t->guards);
3182 aliasTable_free (t->aliases);
3183 t->aliases = aliasTable_copy (parent->aliases);
3189 usymtab_allDefined (void)
3190 /*@globals utab, globtab@*/
3194 llassert (utab == globtab);
3196 for (i = 0; i < utab->nentries; i++)
3198 uentry e = utab->entries[i];
3200 if (uentry_isPriv (e))
3202 ; /* no need to define it */
3206 if (context_getFlag (FLG_SPECUNDECL))
3208 fileloc sloc = uentry_whereSpecified (e);
3209 fileloc dloc = uentry_whereDeclared (e);
3211 if (fileloc_isDefined (sloc)
3212 && !uentry_isFakeTag (e)
3213 && !fileloc_isDefined (dloc))
3217 message ("%s %q specified but not declared",
3218 ekind_capName (uentry_getKind (e)),
3219 uentry_getName (e)),
3224 if (!uentry_isCodeDefined (e))
3226 fileloc dloc = uentry_whereDeclared (e);
3228 if (fileloc_isLib (dloc) || fileloc_isXHFile (dloc))
3232 else if (fileloc_isDefined (dloc))
3234 if (!uentry_isAnyTag (e))
3236 if (fileloc_isUser (dloc))
3240 message ("%s %q declared but not defined",
3241 ekind_capName (uentry_getKind (e)),
3242 uentry_getName (e)),
3244 DPRINTF (("decl: %s", uentry_unparseFull (e)));
3250 fileloc sloc = uentry_whereSpecified (e);
3252 if (fileloc_isDefined (sloc)
3253 && !fileloc_isImport (sloc)
3254 && !fileloc_isLib (sloc)
3255 && !fileloc_isPreproc (sloc)
3256 && !uentry_isFakeTag (e))
3258 if (uentry_isVariable (e) || uentry_isFunction (e))
3262 message ("%s %q specified but not declared or defined",
3263 ekind_capName (uentry_getKind (e)),
3264 uentry_getName (e)),
3271 message ("%s %q specified but not defined",
3272 ekind_capName (uentry_getKind (e)),
3273 uentry_getName (e)),
3283 void usymtab_exportHeader (void)
3288 for (i = 0; i < utab->nentries; i++)
3290 uentry ce = utab->entries[i];
3292 if (!uentry_isDatatype (ce)
3293 && !uentry_isAnyTag (ce)
3294 && !uentry_isEitherConstant (ce) && !uentry_isStatic (ce)
3295 && !uentry_isExternal (ce)
3296 && !uentry_isForward (ce))
3298 fileloc fwhere = uentry_whereDeclared (ce);
3300 if (fileloc_isUndefined (fwhere)
3301 && uentry_isFunction (ce))
3303 fwhere = uentry_whereDefined (ce);
3306 if (fileloc_isDefined (fwhere)
3307 && !fileloc_isHeader (fwhere)
3308 && !fileloc_isXHFile (fwhere)
3309 && !(fileloc_isSpecialFile (fwhere)
3310 && !context_getFlag (FLG_UNUSEDSPECIAL)))
3312 if (uentry_isVariable (ce))
3315 (FLG_EXPORTHEADERVAR,
3316 message ("%s %q exported but not declared in header file",
3317 ekind_capName (uentry_getKind (ce)),
3318 uentry_getName (ce)),
3321 uentry_showDefSpecInfo (ce, fwhere);
3326 if (!uentry_isIter (ce)
3327 && !uentry_isEndIter (ce)
3328 && !uentry_isExpandedMacro (ce))
3330 if (uentry_isFunction (ce)
3331 && cstring_equalLit (uentry_rawName (ce), "main"))
3333 ; /* no error for main */
3339 message ("%s %q exported but not declared "
3341 ekind_capName (uentry_getKind (ce)),
3342 uentry_getName (ce)),
3345 uentry_showDefSpecInfo (ce, fwhere);
3355 void usymtab_exportLocal (void)
3360 for (i = 0; i < utab->nentries; i++)
3362 uentry ce = utab->entries[i];
3364 if (!uentry_isDatatype (ce) && !uentry_isAnyTag (ce)
3365 && !uentry_isEitherConstant (ce)
3366 && !uentry_isIter (ce)
3367 && !uentry_isEndIter (ce)
3368 && !uentry_isExpandedMacro (ce)
3369 && uentry_isUsed (ce))
3371 /* check static uses */
3372 filelocList fuses = uentry_getUses (ce);
3373 fileloc mod = uentry_whereDefined (ce);
3374 bool ok = filelocList_isEmpty (fuses);
3375 fileloc fwhere = uentry_whereDeclared (ce);
3377 if (fileloc_isSpecialFile (fwhere)
3378 && !context_getFlag (FLG_UNUSEDSPECIAL))
3380 ok = TRUE; /* no errors for special files */
3384 filelocList_elements (fuses, uloc)
3386 if (fileloc_isUndefined (uloc) || !fileloc_sameModule (uloc, mod))
3389 /*@innerbreak@*/ break;
3391 } end_filelocList_elements;
3398 message ("%s exported but not used outside %s: %q",
3399 ekind_capName (uentry_getKind (ce)),
3400 fileloc_getBase (mod),
3401 uentry_getName (ce)),
3404 uentry_showDefSpecInfo (ce, fwhere);
3412 usymtab_allUsed (void)
3416 bool isFileStatic = usymtab_inFileScope ();
3417 cstring last_file = cstring_undefined;
3419 for (i = 0; i < utab->nentries; i++)
3421 bool hasError = FALSE;
3422 uentry ce = utab->entries[i];
3423 fileloc fwhere = uentry_whereDeclared (ce);
3425 if (fileloc_isUndefined (fwhere))
3427 fwhere = uentry_whereDefined (ce);
3430 if (fileloc_isInvalid (fwhere)
3431 || fileloc_isLib (fwhere)
3432 || fileloc_isBuiltin (fwhere)
3433 || ((fileloc_isSpecialFile (fwhere)
3434 || fileloc_isSpecialFile (uentry_whereDefined (ce)))
3435 && !context_getFlag (FLG_UNUSEDSPECIAL)))
3439 else if (!uentry_wasUsed (ce) && !uentry_isAnyTag (ce))
3441 cstring fname = fileloc_filename (fwhere);
3443 if (cstring_isUndefined (last_file))
3447 else if (cstring_equal (fname, last_file))
3456 if (uentry_isParam (ce))
3458 if (context_inMacro ())
3460 sRef cref = uentry_getSref (ce);
3462 if (uentry_isYield (ce))
3464 ; /* no checks (for now) */
3466 else if (sRef_isSafe (cref))
3472 if (uentry_hasRealName (ce))
3475 optgenerror (FLG_MACROPARAMS,
3476 message ("Macro parameter %q not used",
3477 uentry_getName (ce)),
3484 if (cstring_equalFree (uentry_getName (ce),
3485 cstring_makeLiteral ("...")))
3491 hasError = optgenerror (FLG_PARAMUNUSED,
3492 message ("Parameter %q not used",
3493 uentry_getName (ce)),
3498 else if (uentry_isFunction (ce) || uentry_isIter (ce))
3500 if (fileloc_isUser (fwhere))
3502 hasError = optgenerror
3504 message ("%q %q declared but not used",
3506 (uentry_isIter (ce) ? "Iterator"
3507 : (isFileStatic ? "File static function" : "Function")),
3508 uentry_getName (ce)),
3512 else if (uentry_isEndIter (ce))
3514 ; /* no error (already reported for iter */
3516 else if (uentry_isEnumConstant (ce))
3518 if (fileloc_isUser (fwhere))
3520 hasError = optgenerror
3522 message ("Enum member %q not used",
3523 uentry_getName (ce)),
3527 else if (uentry_isConstant (ce))
3529 if (fileloc_isUser (fwhere))
3531 hasError = optgenerror
3533 message ("Constant %q declared but not used",
3534 uentry_getName (ce)),
3538 else if (uentry_isDatatype (ce))
3540 if (fileloc_isUser (fwhere))
3542 hasError = optgenerror
3544 message ("Type %q declared but not used",
3545 uentry_getName (ce)),
3549 else if (!uentry_isRefParam (ce) && !uentry_isExpandedMacro (ce))
3550 { /* errors for ref params will be reported in the next scope */
3551 llassertprint (uentry_isVar (ce),
3552 ("ce: %s", uentry_unparseFull (ce)));
3554 if (ctype_isFunction (uentry_getType (ce)))
3556 if (fileloc_isUser (fwhere))
3558 hasError = optgenerror
3560 message ("%q %q declared but not used",
3562 (isFileStatic ? "File static function"
3564 uentry_getName (ce)),
3570 if (fileloc_isUser (fwhere))
3574 hasError = optgenerror
3576 message ("%q %q declared but not used",
3578 (isFileStatic ? "File static variable"
3580 uentry_getName (ce)),
3590 else if ((uentry_isDatatype (ce) || uentry_isAnyTag (ce)))
3591 { /* check all fields */
3592 ctype ct = uentry_getRealType (ce);
3595 while (ctype_isAP (ct))
3597 ct = ctype_getBaseType (ct);
3600 if (ctype_isSU (ct))
3602 uentryList fields = ctype_getFields (ct);
3604 uentryList_elements (fields, field)
3606 if (!uentry_isUsed (field))
3608 if (uentry_hasName (ce))
3610 hasError |= optgenerror
3612 message ("Field %q of %s %q declared but not used",
3613 uentry_getName (field),
3614 cstring_makeLiteralTemp
3615 (ctype_isStruct (ct) ? "structure" : "union"),
3616 uentry_getName (ce)),
3617 uentry_whereEarliest (field));
3623 ** Can't report these errors for unnamed structs.
3624 ** No way to tell when there are multiple consistent
3625 ** unnamed structure types. (Could go through table
3626 ** and mark them all unused...)
3628 hasError |= optgenerror
3630 message ("Field %q of unnamed %s declared but not used",
3631 uentry_getName (field),
3632 cstring_makeLiteralTemp
3633 (ctype_isStruct (ct) ? "structure" : "union")),
3634 uentry_whereEarliest (field));
3639 uentry_setUsed (field, fileloc_undefined);
3641 } end_uentryList_elements;
3651 if (uentry_isParam (ce) && context_inMacro ())
3653 if (fileloc_isDefined (uentry_whereSpecified (ce)))
3655 uentry_showWhereSpecified (ce);
3660 uentry_showDefSpecInfo (ce, fwhere);
3663 uentry_setUsed (ce, fileloc_undefined);
3669 checkGlobalReturn (uentry glob, sRef orig)
3671 sRef sr = uentry_getSref (glob);
3673 DPRINTF (("Check global return: %s / orig: %s / sr: %s",
3674 uentry_unparseFull (glob),
3675 sRef_unparseFull (orig),
3676 sRef_unparseFull (sr)));
3678 DPRINTF (("Is killed: %s", bool_unparse (sRef_isKilledGlob (orig))));
3680 if (context_getFlag (FLG_GLOBSTATE))
3682 DPRINTF (("Is killed: %s", sRef_unparseFull (orig)));
3684 if (sRef_isKilledGlob (orig))
3686 if (sRef_isStateUndefined (sr)
3687 || sRef_isUnuseable (sr)
3688 || sRef_isStateUnknown (sr)
3689 || sRef_isDead (sr))
3695 ctype ct = ctype_realType (uentry_getType (glob));
3697 DPRINTF (("Check global destroyed: %s", uentry_unparseFull (glob)));
3699 if (ctype_isVisiblySharable (ct))
3704 ("Killed global %q (type %s) not released before return",
3705 uentry_getName (glob),
3706 ctype_unparse (ct)),
3709 sRef_showStateInfo (sr);
3714 sRef_protectDerivs ();
3715 (void) transferChecks_globalDestroyed (sr, g_currentloc);
3716 sRef_clearProtectDerivs ();
3722 if (sRef_isStateUndefined (sr))
3724 if (optgenerror (FLG_GLOBSTATE,
3726 ("Function returns with global %q undefined",
3727 uentry_getName (glob)),
3730 sRef_showStateInfo (sr);
3735 if (sRef_isDead (sr) || sRef_isKept (sr))
3739 message ("Function returns with global %q "
3740 "referencing %s storage",
3741 uentry_getName (glob),
3742 cstring_makeLiteralTemp (sRef_isDead (sr) ? "released" : "kept")),
3745 if (sRef_isKept (sr))
3747 sRef_showAliasInfo (sr);
3751 sRef_showStateInfo (sr);
3754 sRef_setDefState (sr, SS_UNKNOWN, fileloc_undefined);
3758 if (ctype_isRealPointer (uentry_getType (glob))
3759 && sRef_possiblyNull (sr)
3760 && !uentry_possiblyNull (glob))
3764 message ("Function returns with non-null global %q "
3765 "referencing null storage",
3766 uentry_getName (glob)),
3769 sRef_showNullInfo (sr);
3774 transferChecks_globalReturn (glob);
3782 ** remember: check alias globals
3785 void usymtab_checkFinalScope (bool isReturn)
3788 bool mustFree = context_getFlag (FLG_MUSTFREEONLY) || context_getFlag (FLG_MUSTFREEFRESH); /*@i423 remove this mustFree */
3789 bool mustDefine = context_getFlag (FLG_MUSTDEFINE);
3790 /* bool mustNotAlias = context_getFlag (FLG_MUSTNOTALIAS); */
3791 sRefSet checked = sRefSet_new ();
3792 usymtab stab = utab;
3796 ** need to check all scopes out to function parameters.
3801 for (i = 0; i < stab->nentries; i++)
3803 uentry ce = stab->entries[i];
3804 sRef sr = uentry_getSref (ce);
3805 sRef rb = sRef_getRootBase (sr);
3809 ** Shouldn't check if shadow checked in deeper scope:
3814 uentry oue = usymtab_lookupQuietNoAlt (utab, uentry_observeRealName (ce));
3816 if (!uentry_sameObject (ce, oue))
3818 DPRINTF (("Skipping outer entry: %s / %s", uentry_unparseFull (ce),
3819 uentry_unparseFull (oue)));
3820 /*@i32 what if it is one an alternate branch? */
3821 /*@innercontinue@*/ continue;
3825 DPRINTF (("Here check final scope: %s", uentry_unparseFull (ce)));
3827 if (ctype_isFunction (uentry_getType (ce)))
3829 /*@innercontinue@*/ continue;
3832 if (uentry_isAnyParam (ce)
3833 || uentry_isRefParam (ce)
3834 || sRef_isFileOrGlobalScope (rb))
3836 /* Don't do the loseref check...but should check state! */
3837 DPRINTF (("Skipping check 1"));
3839 else if (sRef_isDefinitelyNull (sr)
3840 || usymtab_isDefinitelyNull (sr))
3843 ** No state reference errors for definitely null references.
3846 DPRINTF (("Skipping check 2"));
3850 DPRINTF (("Lose ref: %s / %s", uentry_unparse (ce),
3851 sRef_unparseFull (sr)));
3853 tvalues = sRef_getValueTable (sr);
3855 valueTable_elements (tvalues, fkey, fval) {
3856 metaStateInfo minfo;
3857 cstring msg = cstring_undefined;
3860 minfo = context_lookupMetaStateInfo (fkey);
3861 llassert (metaStateInfo_isDefined (minfo));
3863 if (stateValue_isError (fval)
3864 || sRef_isStateUndefined (sr)) /* No errors for undefined state */
3866 DPRINTF (("Skipping check 3"));
3870 DPRINTF (("Check: %s / %s / %s", fkey,
3871 metaStateInfo_unparse (minfo),
3872 stateValue_unparse (fval)));
3874 minfo = context_lookupMetaStateInfo (fkey);
3876 nval = stateCombinationTable_lookupLoseReference
3877 (metaStateInfo_getTransferTable (minfo),
3878 stateValue_getValue (fval), &msg);
3880 if (cstring_isDefined (msg))
3882 /*@i32 print extra info for assignments@*/
3883 DPRINTF (("From: %s", sRef_unparseFull (sr)));
3884 DPRINTF (("Null? %s / %s",
3885 bool_unparse (sRef_isDefinitelyNull (sr)),
3886 bool_unparse (usymtab_isGuarded (sr))));
3891 ("%s loses reference %q in invalid state %q (%s)",
3892 cstring_makeLiteralTemp (isReturn ? "Return" : "Scope exit"),
3893 uentry_getName (ce),
3894 stateValue_unparseValue (fval, minfo),
3898 stateValue_show (fval, minfo);
3902 DPRINTF (("Suppressed transfer error: %s", msg));
3906 } end_valueTable_elements;
3909 DPRINTF (("Here 1"));
3913 DPRINTF (("Check mustfree entry: %s", uentry_unparseFull (ce)));
3915 if (!sRefSet_member (checked, sr) && !sRef_isFileOrGlobalScope (rb))
3917 if (ctype_isRealSU (uentry_getType (ce))
3918 && !uentry_isAnyParam (ce)
3919 && !uentry_isRefParam (ce)
3920 && !uentry_isStatic (ce)
3921 && !sRef_isDependent (sr)
3922 && !sRef_isOwned (sr))
3924 sRefSet als = usymtab_allAliases (sr);
3926 if (sRefSet_isEmpty (als))
3928 transferChecks_localDestroyed (sr, g_currentloc);
3932 /* aliased, no problem */ ;
3938 (!uentry_isStatic (ce)
3939 && ((sRef_isNewRef (sr))
3940 || (((sRef_isOnly (sr) || sRef_isFresh (sr)
3941 || sRef_isKeep (sr) || sRef_isOwned (sr))
3942 && !sRef_isDead (sr))
3943 && (!sRef_definitelyNull (sr))
3944 && (!usymtab_isDefinitelyNull (sr)))))
3946 bool hasError = TRUE;
3948 DPRINTF (("Checking: %s", sRef_unparseFull (sr)));
3951 ** If its a scope exit, check if there is an alias.
3952 ** If so, make it only. If not, there is an error.
3957 if (transferChecks_canLoseReference (sr, g_currentloc))
3959 DPRINTF (("Can lose!"));
3966 if (sRef_hasLastReference (sr))
3968 sRef ar = sRef_getAliasInfoRef (sr);
3971 (sRef_isFresh (ar) ? FLG_MUSTFREEFRESH : FLG_MUSTFREEONLY,
3973 ("Last reference %q to %s storage %qnot %q before %q",
3975 alkind_unparse (sRef_getAliasKind (sr)),
3976 sRef_unparseOpt (ar),
3977 cstring_makeLiteral (sRef_isKeep (sr)
3978 ? "transferred" : "released"),
3979 cstring_makeLiteral (isReturn
3980 ? "return" : "scope exit")),
3983 sRef_showRefLost (sr);
3986 else if (sRef_isNewRef (sr))
3989 (sRef_isFresh (sr) ? FLG_MUSTFREEFRESH : FLG_MUSTFREEONLY,
3991 ("%q %q not released before %q",
3993 (alkind_isKillRef (sRef_getOrigAliasKind (sr))
3994 ? "Kill reference parameter" : "New reference"),
3995 uentry_getName (ce),
3996 cstring_makeLiteral (isReturn
3997 ? "return" : "scope exit")),
4000 sRef_showAliasInfo (sr);
4005 if (ctype_isRealSU (sRef_getType (sr)))
4007 transferChecks_structDestroyed (sr, g_currentloc);
4011 DPRINTF (("Here we are: %s", sRef_unparseFull (sr)));
4014 (sRef_isFresh (sr) ? FLG_MUSTFREEFRESH : FLG_MUSTFREEONLY,
4016 ("%s storage %q not %q before %q",
4017 alkind_capName (sRef_getAliasKind (sr)),
4018 uentry_getName (ce),
4019 cstring_makeLiteral (sRef_isKeep (sr)
4020 ? "transferred" : "released"),
4021 cstring_makeLiteral (isReturn
4022 ? "return" : "scope exit")),
4025 sRef_showAliasInfo (sr);
4026 DPRINTF (("Storage: %s", sRef_unparseFull (sr)));
4039 if (mustDefine && uentry_isOut (ce))
4041 /* No error if its dead (either only or error already reported */
4042 if (!sRef_isReallyDefined (sr) && !sRef_isDead (sr))
4046 message ("Out storage %q not defined before %q",
4047 uentry_getName (ce),
4049 (isReturn ? "return" : "scope exit")),
4052 DPRINTF (("sr: %s", sRef_unparseFull (sr)));
4057 ** also check state is okay
4060 if (usymtab_lexicalLevel () > functionScope
4061 && uentry_isVariable (ce)
4062 && (sRef_isLocalVar (sr)
4063 && (sRef_isDependent (sr) || sRef_isLocalState (sr))))
4065 sRefSet ab = usymtab_aliasedBy (sr);
4067 /* should do something more efficient here */
4069 if (sRefSet_isEmpty (ab))
4071 /* and no local ref */
4072 DPRINTF (("Check lose ref: %s", uentry_unparseFull (ce)));
4073 transferChecks_loseReference (ce);
4087 checked = sRefSet_insert (checked, sr);
4090 llassert (usymtab_isDefined (stab->env));
4092 if (usymtab_isBranch (stab))
4094 stab = usymtab_dropEnv (stab);
4101 llassert (stab != usymtab_undefined);
4102 } while (isReturn && (stab->lexlevel >= paramsScope));
4104 sRefSet_free (checked);
4108 ** all globals are appropriately defined
4109 ** all parameters are appropriately defined
4110 ** special clauses are followed
4113 if (isReturn || (utab->lexlevel == paramsScope))
4115 uentry fcn = context_getHeader ();
4116 uentryList params = context_getParams ();
4117 globSet uglobs = context_getUsedGlobs ();
4118 globSet sglobs = context_getGlobs ();
4120 if (isReturn && context_maybeSet (FLG_GLOBALIAS))
4122 aliasTable_checkGlobs (utab->aliases);
4126 ** state clauses (ensures, defines, sets, allocates, releases)
4129 if (uentry_hasStateClauseList (fcn))
4131 stateClauseList clauses = uentry_getStateClauseList (fcn);
4133 stateClauseList_elements (clauses, cl)
4135 if (stateClause_isAfter (cl) && !stateClause_isGlobal (cl))
4137 if (stateClause_setsMetaState (cl))
4139 sRefSet rfs = stateClause_getRefs (cl);
4140 qual q = stateClause_getMetaQual (cl);
4141 annotationInfo ainfo = qual_getAnnotationInfo (q);
4142 metaStateInfo minfo = annotationInfo_getState (ainfo);
4143 cstring key = metaStateInfo_getName (minfo);
4144 int mvalue = annotationInfo_getValue (ainfo);
4146 DPRINTF (("Post meta state clause: %s", stateClause_unparse (cl)));
4148 sRefSet_elements (rfs, el)
4150 sRef base = sRef_getRootBase (el);
4152 if (sRef_isResult (base))
4155 ** This is checked for return transfers.
4159 else if (sRef_isParam (base) || sRef_isGlobalMarker (base))
4161 sRef sr = sRef_updateSref (base);
4162 sr = sRef_fixBase (el, sr);
4164 if (!sRef_checkMetaStateValue (sr, key, mvalue))
4169 ("Ensures clause not satisfied%q (state is %q): %q",
4170 sRef_isGlobalMarker (sr)
4172 : message (" by %q", sRef_unparse (sr)),
4173 stateValue_unparseValue (sRef_getMetaStateValue (sr, key),
4175 stateClause_unparse (cl)),
4178 sRef_showMetaStateInfo (sr, key);
4184 if (sRef_isMeaningful (el))
4189 } end_sRefSet_elements ;
4193 /* evs - 2000 07 10 - added this */
4194 sRefTest tst = stateClause_getPostTestFunction (cl);
4195 sRefSet rfs = stateClause_getRefs (cl);
4197 sRefSet_elements (rfs, el)
4199 sRef base = sRef_getRootBase (el);
4201 if (sRef_isResult (base))
4204 ** This is checked for return transfers.
4209 else if (sRef_isParam (base))
4211 sRef sr = sRef_updateSref (base);
4212 sr = sRef_fixBase (el, sr);
4214 if (tst != NULL && !tst (sr))
4217 (stateClause_postErrorCode (cl),
4218 message ("%s storage %qcorresponds to "
4219 "storage listed in %q clause",
4220 stateClause_postErrorString (cl, sr),
4221 sRef_unparseOpt (sr),
4222 stateClause_unparseKind (cl)),
4225 sRefShower ss = stateClause_getPostTestShower (cl);
4232 DPRINTF (("Storage: %s", sRef_unparseFull (sr)));
4238 if (sRef_isMeaningful (el))
4243 } end_sRefSet_elements ;
4246 } end_stateClauseList_elements ;
4250 ** check parameters on return
4253 uentryList_elements (params, arg)
4255 if (!uentry_isElipsisMarker (arg))
4257 ctype rt = ctype_realType (uentry_getType (arg));
4259 if (ctype_isMutable (rt) || ctype_isSU (rt))
4261 uentry param = usymtab_lookupQuiet (utab, uentry_rawName (arg));
4262 DPRINTF (("Check param return: %s", uentry_unparseFull (param)));
4263 transferChecks_paramReturn (param);
4266 } end_uentryList_elements;
4268 DPRINTF (("Check global return: %s",
4269 globSet_unparse (sglobs)));
4271 globSet_allElements (sglobs, el)
4273 sRef orig = el; /*! sRef_updateSref (el); */ /*!!!*/
4274 uentry current = sRef_getUentry (el);
4276 DPRINTF (("Check global return: %s / %s", sRef_unparseFull (el),
4277 uentry_unparseFull (current)));
4279 if (uentry_isVariable (current) && !uentry_isRealFunction (current))
4281 checkGlobalReturn (current, orig);
4283 } end_globSet_allElements;
4285 globSet_allElements (uglobs, el)
4287 if (!globSet_member (sglobs, el))
4289 uentry current = sRef_getUentry (el);
4291 if (uentry_isVariable (current)
4292 && !uentry_isRealFunction (current))
4294 checkGlobalReturn (current, sRef_undefined);
4297 } end_globSet_allElements;
4302 usymtab_quietExitScope (fileloc loc)
4303 /*@globals utab, globtab, filetab; @*/
4306 usymtab t = utab->env;
4308 if (utab->reftable != NULL)
4312 for (i = 0; i < utab->nentries; i++)
4314 uentry current = utab->entries[i];
4315 uentry old = usymtab_lookupAux (t, uentry_rawName (current));
4317 uentry_mergeState (old, current, loc, FALSE, FALSE, FALSE, NOCLAUSE);
4321 llassert (t != NULL);
4323 if (t->lexlevel > paramsScope)
4325 t->guards = guardSet_levelUnion (t->guards, utab->guards, t->lexlevel);
4326 t->aliases = aliasTable_levelUnionSeq (t->aliases, utab->aliases,
4328 utab->aliases = aliasTable_undefined;
4331 t->mustBreak = utab->mustBreak;
4332 t->exitCode = utab->exitCode;
4334 usymtab_freeLevel (utab);
4340 ** Exit a scope with no checking, lose alias states.
4341 ** (When should this be used?)
4344 void usymtab_quietPlainExitScope (void)
4345 /*@globals utab, globtab, filetab@*/
4348 usymtab t = utab->env;
4350 llassert (t != NULL);
4351 llassert (NOALIAS (utab->aliases, t->aliases));
4352 usymtab_freeLevel (utab);
4356 void usymtab_exitScope (exprNode expr)
4357 /*@globals utab, filetab, globtab@*/
4358 /*@modifies utab, globtab@*/
4360 usymtab ctab = usymtab_undefined;
4361 usymtab lctab = usymtab_undefined;
4362 bool mustReturn = exprNode_mustEscape (expr);
4364 DPRINTF (("Exit scope"));
4366 if (utab->kind == US_CBRANCH)
4369 ** save the case branches, remove the first non-cbranch
4374 while (utab->kind == US_CBRANCH)
4378 llassert (utab != GLOBAL_ENV);
4382 if (utab->kind == US_TBRANCH || utab->kind == US_FBRANCH
4383 || utab->kind == US_CBRANCH || utab->kind == US_SWITCH) {
4385 if (context_inMacro ()) {
4386 /* evs 2000-07-25 */
4387 /* Unparseable macro may end inside nested scope. Deal with it. */
4389 llerror (FLG_SYNTAX,
4390 message ("Problem parsing macro body of %s (unbalanced scopes). "
4391 "Attempting to recover, recommend /*@notfunction@*/ before "
4392 "macro definition.",
4393 context_inFunctionName ()));
4395 while (utab->kind == US_TBRANCH
4396 || utab->kind == US_FBRANCH
4397 || utab->kind == US_CBRANCH
4398 || utab->kind == US_SWITCH)
4401 llassert (utab != GLOBAL_ENV);
4404 llcontbug (message ("exitScope: in branch: %q", usymtab_unparseStack ()));
4406 } /*@=branchstate@*/
4410 ** check all variables in scope were used
4414 ** bogus errors if this is the normal inside a switch,
4415 ** since cases have not been merged yet. Should probably
4416 ** still check this, but I'm too lazy at the moment...
4419 llassertfatal (utab->env != GLOBAL_ENV);
4421 if (utab->env->kind != US_SWITCH)
4427 ** check aliasing: all only params are released (dead)
4428 ** definition: all out params are defined, all modified params
4429 ** are completely defined
4431 ** NOTE: note for exiting paramsScope, since checkReturn should be
4435 if (!mustReturn && (usymtab_lexicalLevel () > functionScope))
4438 ** should only call this is end of scope is reachable...
4441 usymtab_checkFinalScope (FALSE);
4444 if (usymtab_lexicalLevel () == paramsScope && context_inFunctionLike ())
4447 ** leaving a function, need to fix up globals
4450 uentryList params = context_getParams ();
4451 globSet globs = context_getUsedGlobs ();
4454 uentryList_elements (params, ue)
4456 uentry_fixupSref (ue);
4457 } end_uentryList_elements;
4459 clearFunctionTypes ();
4462 globSet_allElements (globs, el)
4464 if (sRef_isCvar (el))
4467 int index = sRef_getScopeIndex (el);
4469 if (sRef_isFileStatic (el))
4471 llassert (usymtab_isDefined (filetab));
4472 current = usymtab_fetchIndex (filetab, index);
4476 current = usymtab_fetchIndex (globtab, index);
4479 if (uentry_isVariable (current))
4481 uentry_fixupSref (current);
4485 sRef_clearDerived (uentry_getSref (current));
4488 } end_globSet_allElements;
4491 usymtab_quietExitScope (exprNode_loc (expr));
4493 if (lctab != usymtab_undefined)
4495 /*@i@*/ lctab->env = utab;
4496 /*@i@*/ utab = ctab;
4497 /*@-branchstate@*/ } /*@=branchstate@*/
4503 ** yikes! don't let the '170 kids see this one...
4507 uentry_directParamNo (uentry ue)
4509 if (uentry_isVar (ue))
4511 sRef sr = uentry_getSref (ue);
4513 if (sRef_lexLevel (sr) == functionScope)
4518 llassert (sr->info != NULL);
4519 llassert (sr->info->cvar != NULL);
4520 index = sr->info->cvar->index;
4523 if (index < uentryList_size (context_getParams ()))
4532 /*@dependent@*/ /*@exposed@*/ uentry
4533 usymtab_getParam (int paramno)
4537 ** requires in a function context (checked)
4539 ** depends on no nested functions --- the function
4540 ** parameters are ALWAYS one scope inside the global scope
4541 ** and entered in order!
4545 if (!context_inFunctionLike ())
4546 llfatalbug (message ("usymtab_getParam: not in function context: %q",
4547 context_unparse ()));
4549 while (s->lexlevel > paramsScope)
4554 llassert (usymtab_isDefined (s));
4556 if (paramno >= s->nentries)
4559 ** Parse errors lead to this.
4562 uentry err = uentry_makeVariableLoc (cstring_makeLiteralTemp ("<error>"),
4565 uentry_markOwned (err);
4569 return (s->entries[paramno]);
4572 static /*@dependent@*/ /*@exposed@*/ uentry
4573 usymtab_getRefTab (/*@notnull@*/ usymtab u, int level, usymId index)
4577 ue = usymtab_getRefNoisy (u, level, index);
4579 if (uentry_isUndefined (ue))
4581 llbug (message ("usymtab_getRef: out of range: %d. level = %d",
4588 static /*@dependent@*/ /*@exposed@*/ usymtab
4589 usymtab_dropEnv (/*@notnull@*/ usymtab s)
4591 if (s->kind == US_CBRANCH)
4599 llassert (s != GLOBAL_ENV);
4600 } while (s->kind == US_CBRANCH);
4601 /* drop all cases (except in nested scopes */
4604 llassert (s != GLOBAL_ENV);
4607 if (s->kind == US_FBRANCH)
4609 s = s->env; /* skip the true branch */
4610 llassert (usymtab_isDefined (s));
4611 llassert (s->kind == US_TBRANCH);
4614 llassert (s != GLOBAL_ENV);
4620 /*@dependent@*/ /*@exposed@*/ uentry
4621 usymtab_getRefQuiet (int level, usymId index)
4627 llassert (s != NULL);
4628 llassert (index >= 0);
4630 if (level > s->lexlevel)
4632 return uentry_undefined;
4635 llassertprint (level <= s->lexlevel, ("level: %d / lexlevel: %d",
4636 level, s->lexlevel));
4638 while (s->lexlevel > level)
4640 if (usymtab_isBranch (s))
4642 int eindex = refTable_lookup (s, level, index);
4644 if (eindex != NOT_FOUND)
4646 return (s->entries[eindex]);
4650 s = usymtab_dropEnv (s);
4653 while (usymtab_isBranch (s) && s->lexlevel == level)
4655 int eindex = refTable_lookup (s, level, index);
4657 if (eindex != NOT_FOUND)
4659 return (s->entries[eindex]);
4662 s = usymtab_dropEnv (s);
4665 if (index >= s->nentries)
4667 return uentry_undefined;
4670 llassert (!uentry_isUndefined (s->entries[index]));
4672 return s->entries[index];
4675 static /*@dependent@*/ /*@exposed@*/ uentry
4676 usymtab_getRefNoisy (/*@notnull@*/ usymtab s, int level, usymId index)
4679 uentry ue = uentry_undefined;
4681 llassert (index >= 0);
4683 while (s->lexlevel > level)
4685 if (usymtab_isBranch (s))
4687 int eindex = refTable_lookup (s, level, index);
4689 if (eindex != NOT_FOUND)
4691 ue = s->entries[eindex];
4695 while (!usymtab_isBranch (otab))
4697 otab = usymtab_dropEnv (otab);
4698 llassert (otab != GLOBAL_ENV);
4701 if (refTable_lookup (otab, level, index) == NOT_FOUND)
4703 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4715 s = usymtab_dropEnv (s);
4718 llassert (usymtab_isDefined (s));
4720 while (usymtab_isBranch (s) && s->lexlevel == level)
4722 int eindex = refTable_lookup (s, level, index);
4725 if (eindex != NOT_FOUND)
4727 ue = s->entries[eindex];
4731 while (!usymtab_isBranch (otab))
4733 otab = usymtab_dropEnv (otab);
4734 llassert (otab != GLOBAL_ENV);
4737 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4747 s = usymtab_dropEnv (s);
4750 if (s->lexlevel == level && (index < s->nentries))
4752 ue = s->entries[index];
4754 if (uentry_isValid (ue))
4758 while (!usymtab_isBranch (otab))
4760 otab = usymtab_dropEnv (otab);
4762 if (otab == GLOBAL_ENV)
4768 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4779 if (index >= s->nentries)
4781 return uentry_undefined;
4784 llassert (!uentry_isUndefined (s->entries[index]));
4786 return s->entries[index];
4790 ** looking up entries
4792 ** If entry is inside a branch, then copy it, and put it into
4793 ** the branch table.
4797 int refTable_lookup (/*@notnull@*/ usymtab ut, int level, int index)
4799 refTable rt = ut->reftable;
4802 llassert (rt != NULL);
4804 for (i = 0; i < ut->nentries; i++)
4806 if (rt[i]->level == level && rt[i]->index == index)
4816 /*@only@*/ refentry refentry_create (int level, int index)
4818 refentry r = (refentry) dmalloc (sizeof (*r));
4826 static /*@dependent@*/ /*@exposed@*/ uentry
4827 usymtab_addRefEntry (/*@notnull@*/ usymtab s, cstring k)
4832 if (ut->reftable == NULL)
4834 DPRINTF (("Adding ref entry without reftable: %s", k));
4835 return uentry_undefined;
4838 llassert (ut->reftable != NULL);
4840 while (s != GLOBAL_ENV)
4842 eindex = usymtab_getIndex (s, k);
4844 if (eindex != NOT_FOUND)
4846 uentry current = s->entries[eindex];
4848 if (uentry_isVar (current) && !ctype_isFunction (uentry_getType (current)))
4852 DPRINTF (("Here: copying %s", uentry_unparse (current)));
4853 ue = uentry_copy (current);
4854 DPRINTF (("Here: copying %s", uentry_unparse (ue)));
4855 usymtab_addEntryQuiet (ut, ue);
4856 DPRINTF (("Okay..."));
4858 if (s->reftable != NULL)
4860 refentry ref = s->reftable[eindex];
4862 ut->reftable[ut->nentries - 1]
4863 = refentry_create (ref->level, ref->index);
4867 ut->reftable[ut->nentries - 1]
4868 = refentry_create (s->lexlevel, eindex);
4879 s = usymtab_dropEnv (s);
4882 return uentry_undefined;
4885 static uentry usymtab_lookupAux (usymtab s, cstring k)
4887 DPRINTF (("Lookup: %s", k));
4889 while (s != GLOBAL_ENV)
4891 int eindex = usymtab_getIndex (s, k);
4893 if (eindex != NOT_FOUND)
4895 uentry ret = s->entries[eindex];
4899 if (s->kind == US_TBRANCH
4900 || s->kind == US_FBRANCH
4901 || s->kind == US_CBRANCH)
4902 /* uentry_isGlobalVariable (ret) && os->lexlevel > fileScope) */
4905 DPRINTF (("Adding global ref entry: %s", k));
4906 ret = usymtab_addRefEntry (os, k);
4907 DPRINTF (("Adding ref entry: %s", uentry_unparseFull (ret)));
4912 DPRINTF (("Found: %s", uentry_unparseFull (ret)));
4916 if (s->kind == US_TBRANCH || s->kind == US_FBRANCH
4917 || s->kind == US_CBRANCH)
4919 /* why isn't this os??? */
4920 uentry ret = usymtab_addRefEntry (s, k);
4921 DPRINTF (("Adding ref entry: %s", uentry_unparseFull (ret)));
4928 return uentry_undefined;
4931 static /*@dependent@*/ /*@exposed@*/ uentry
4932 usymtab_lookupQuietAux (usymtab s, cstring k, bool noalt)
4936 while (s != GLOBAL_ENV)
4938 eindex = usymtab_getIndex (s, k);
4940 if (eindex != NOT_FOUND)
4942 uentry ret = s->entries[eindex];
4946 if (noalt && usymtab_isBranch (s))
4948 s = usymtab_dropEnv (s);
4956 return uentry_undefined;
4959 static /*@exposed@*/ /*@dependent@*/ uentry
4960 usymtab_lookupQuiet (usymtab s, cstring k)
4962 return usymtab_lookupQuietAux (s, k, FALSE);
4965 static /*@exposed@*/ /*@dependent@*/ uentry
4966 usymtab_lookupQuietNoAlt (usymtab s, cstring k)
4968 return usymtab_lookupQuietAux (s, k, TRUE);
4971 /*@dependent@*/ /*@observer@*/ uentry
4972 usymtab_lookupSafe (cstring k)
4975 DPRINTF (("Lookup safe: %s", k));
4976 return (usymtab_lookupAux (utab, k));
4980 usymtab_lookupExpose (cstring k)
4983 uentry ce = usymtab_lookupAux (utab, k);
4985 if (uentry_isUndefined (ce))
4987 llfatalbug (message ("usymtab_lookup: not found: *%s*", k));
4990 if (uentry_isPriv (ce))
4992 llfatalbug (message ("usymtab_lookup: private: *%s*", k));
4998 uentry usymtab_lookupExposeGlob (cstring k)
5000 return (usymtab_lookupGlobSafe (k));
5003 uentry usymtab_lookupGlob (cstring k)
5004 /*@globals globtab@*/
5006 uentry ce = usymtab_lookupAux (globtab, k);
5008 if (uentry_isUndefined (ce))
5009 llfatalbug (message ("usymtab_lookup: not found: %s", k));
5011 if (uentry_isPriv (ce))
5012 llfatalbug (message ("usymtab_lookup: private: %s", k));
5017 /*@observer@*/ uentry
5018 usymtab_lookupGlobSafe (cstring k)
5019 /*@globals globtab@*/
5021 uentry ce = usymtab_lookupAux (globtab, k);
5026 uentry usymtab_lookupEither (cstring k)
5029 uentry ce = usymtab_lookupSafe (k);
5031 if (uentry_isUndefined (ce))
5032 llfatalerror (message ("usymtab_lookup: not found: %s", k));
5039 usymtab_lookupType (cstring k)
5040 /*@globals globtab@*/
5042 usymId uid = usymtab_getTypeId (k);
5044 if (uid == USYMIDINVALID)
5046 llcontbug (message ("usymtab_lookupType: not found: %s", k));
5047 return ctype_unknown;
5050 return (uentry_getRealType (usymtab_getTypeEntry (uid)));
5055 usymtab_lookupAbstractType (cstring k) /*@globals globtab@*/
5057 usymId uid = usymtab_getTypeId (k);
5059 if (uid == USYMIDINVALID)
5061 llcontbug (message ("usymtab_lookupType: not found: %s", k));
5062 return ctype_unknown;
5065 return (uentry_getAbstractType (usymtab_getTypeEntry (uid)));
5069 ** if there is an unnamed lcl-specified struct tag matching
5070 ** the uentryList, return its datatype. Otherwise, returns
5075 usymtab_structFieldsType (uentryList f)
5076 /*@globals globtab@*/
5078 return (usymtab_suFieldsType (f, TRUE));
5082 usymtab_unionFieldsType (uentryList f)
5083 /*@globals globtab@*/
5085 return (usymtab_suFieldsType (f, FALSE));
5089 usymtab_suFieldsType (uentryList f, bool isStruct)
5090 /*@globals globtab@*/
5094 DPRINTF (("Fields: %s", uentryList_unparse (f)));
5096 if (fileloc_isSpec (g_currentloc))
5098 return (ctype_undefined);
5101 for (i = 0; i < globtab->nentries; i++)
5103 uentry current = globtab->entries[i];
5106 ? uentry_isStructTag (current) : uentry_isUnionTag (current)))
5108 if (isFakeTag (uentry_rawName (current)))
5110 ctype ct = uentry_getType (current);
5112 DPRINTF (("Check: %s", ctype_unparse (ct)));
5114 if ((isStruct ? ctype_isStruct (ct) : ctype_isUnion (ct))
5116 (uentry_isSpecified (current)
5117 && uentryList_equivFields (f, ctype_getFields (ct))))
5119 return uentry_getAbstractType (current);
5129 return ctype_undefined;
5133 usymtab_enumEnumNameListType (enumNameList f)
5134 /*@globals globtab@*/
5138 for (i = 0; i < globtab->nentries; i++)
5140 uentry current = globtab->entries[i];
5142 if (uentry_isEnumTag (current))
5144 if (isFakeTag (uentry_rawName (current)))
5146 ctype ct = uentry_getType (current);
5148 if (ctype_isEnum (ct) && (enumNameList_match (f, ctype_elist (ct))))
5150 return uentry_getType (current);
5156 return ctype_undefined;
5160 usymtab_exists (cstring k)
5163 uentry ce = usymtab_lookupSafe (k);
5164 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5168 usymtab_existsReal (cstring k)
5171 uentry ce = usymtab_lookupSafe (k);
5173 return (!(uentry_isUndefined (ce))
5174 && !(uentry_isPriv (ce))
5175 && !(uentry_isExpandedMacro (ce)));
5179 usymtab_existsGlob (cstring k)
5180 /*@globals globtab@*/
5182 uentry ce = usymtab_lookupAux (globtab, k);
5184 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5189 usymtab_existsEither (cstring k)
5192 uentry ce = usymtab_lookupAux (utab, k);
5194 return (uentry_isValid (ce));
5198 usymtab_existsGlobEither (cstring k)
5199 /*@globals globtab@*/
5201 uentry ce = usymtab_lookupAux (globtab, k);
5203 return (uentry_isValid (ce));
5208 usymtab_existsType (cstring k)
5209 /*@globals globtab@*/
5211 uentry ce = usymtab_lookupAux (globtab, k);
5213 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)) && uentry_isDatatype (ce));
5217 usymtab_existsTypeEither (cstring k)
5218 /*@globals globtab@*/
5221 ce = usymtab_lookupAux (globtab, k);
5222 return (uentry_isValid (ce) && uentry_isDatatype (ce));
5226 usymtab_existsStructTag (cstring k) /*@globals globtab@*/
5228 cstring sname = makeStruct (k);
5229 uentry ce = usymtab_lookupAux (globtab, sname);
5230 cstring_free (sname);
5231 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5235 usymtab_existsUnionTag (cstring k) /*@globals globtab@*/
5237 cstring uname = makeUnion (k);
5238 uentry ce = usymtab_lookupAux (globtab, uname);
5240 cstring_free (uname);
5242 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5246 usymtab_existsEnumTag (cstring k) /*@globals globtab@*/
5248 cstring ename = makeEnum (k);
5249 uentry ce = usymtab_lookupAux (globtab, ename);
5251 cstring_free (ename);
5252 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5256 bool usymtab_existsVar (cstring k)
5259 uentry ce = usymtab_lookupSafe (k);
5261 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)) && (uentry_isVar (ce)));
5270 refTable_free (/*@only@*/ /*@null@*/ refTable x, int nentries)
5276 for (i = 0; i < nentries; i++)
5286 usymtab_freeLevel (/*@notnull@*/ /*@only@*/ usymtab u)
5287 /*@globals globtab, utab, filetab@*/
5291 aliasTable_free (u->aliases);
5293 refTable_free (u->reftable, u->nentries);
5295 if (u == filetab || u == globtab)
5297 for (i = 0; i < u->nentries; i++)
5299 uentry_freeComplete (u->entries[i]);
5304 for (i = 0; i < u->nentries; i++)
5306 uentry_free (u->entries[i]);
5310 guardSet_free (u->guards);
5317 llassert (!cstringTable_isDefined (u->htable));
5325 usymtab_freeAux (/*@only@*/ usymtab u)
5326 /*@globals globtab, utab, filetab@*/
5329 while (u != GLOBAL_ENV)
5332 usymtab_freeLevel (u);
5339 void usymtab_free ()
5340 /*@globals killed utab, globtab, filetab@*/
5344 usymtab_freeAux (utab);
5347 static int usymtab_lexicalLevel (void) /*@globals utab@*/
5349 return (utab->lexlevel);
5352 bool usymtab_inGlobalScope () /*@globals utab, globtab@*/
5354 return (utab == globtab);
5357 bool usymtab_inFileScope () /*@globals utab@*/
5359 return (utab->lexlevel == fileScope);
5362 bool usymtab_inFunctionScope () /*@globals utab@*/
5364 return (utab->lexlevel == functionScope);
5369 usymtab_replaceEntry (uentry s)
5370 /*@globals utab, globtab@*/
5371 /*@modifies utab, s@*/
5373 usymtab_replaceEntryAux (utab, s);
5378 usymtab_matchForwardStruct (usymId u1, usymId u2)
5379 /*@globals globtab@*/
5381 uentry ue1 = usymtab_getTypeEntry (u1);
5382 uentry ue2 = usymtab_getTypeEntry (u2);
5384 if (uentry_isAnyTag (ue2))
5386 ctype reptype = uentry_getType (ue1);
5388 if (ctype_isPointer (reptype))
5390 ctype repbase = ctype_getBaseType (reptype);
5392 if (ctype_isUA (repbase))
5394 typeId rtuid = ctype_typeId (repbase);
5396 if (u2 == rtuid) return TRUE;
5398 if (usymId_isValid (rtuid))
5400 reptype = uentry_getType (usymtab_getTypeEntry (rtuid));
5402 return (ctype_isUA (reptype) && (u2 == (ctype_typeId (reptype))));
5411 void usymtab_addGuards (guardSet guards)
5414 utab->guards = guardSet_union (utab->guards, guards);
5417 static bool usymtab_isGuardedAux (sRef s)
5421 sRef base = sRef_getRootBase (s);
5422 int lowlevel = paramsScope;
5423 int baselevel = sRef_lexLevel (base);
5425 if (sRef_isCvar (base))
5427 lowlevel = baselevel;
5428 if (lowlevel < paramsScope) lowlevel = paramsScope;
5431 while (tab->lexlevel >= lowlevel)
5433 DPRINTF (("Is guarded? [%s] %s",
5434 guardSet_unparse (tab->guards),
5435 sRef_unparseFull (s)));
5437 if (guardSet_isGuarded (tab->guards, s))
5440 if (!sRef_definitelyNull (s))
5442 sRef_setNotNull (s, fileloc_undefined);
5448 tab = usymtab_dropEnv (tab);
5454 void usymtab_unguard (sRef s) /*@modifies utab@*/
5457 sRef base = sRef_getRootBase (s);
5458 int lowlevel = paramsScope;
5459 int baselevel = sRef_lexLevel (base);
5461 if (sRef_isCvar (base))
5463 lowlevel = baselevel;
5464 if (lowlevel < paramsScope) lowlevel = paramsScope;
5467 while (tab->lexlevel >= lowlevel)
5469 if (guardSet_isGuarded (tab->guards, s))
5471 guardSet_delete (tab->guards, s);
5474 tab = usymtab_dropEnv (tab);
5478 bool usymtab_isGuarded (sRef s)
5480 DPRINTF (("Is guarded? %s", sRef_unparseFull (s)));
5481 return (sRef_aliasCompleteSimplePred (usymtab_isGuardedAux, s));
5484 bool usymtab_isDefinitelyNull (sRef s)
5486 return (sRef_aliasCheckSimplePred (usymtab_isDefinitelyNullAux, s));
5489 bool usymtab_isDefinitelyNullDeep (sRef s)
5491 return (sRef_deepPred (usymtab_isDefinitelyNull, s));
5494 static bool usymtab_isDefinitelyNullAux (sRef s)
5498 sRef base = sRef_getRootBase (s);
5499 int lowlevel = paramsScope;
5501 if (sRef_isCvar (base))
5503 lowlevel = sRef_lexLevel (base);
5504 if (lowlevel < paramsScope) lowlevel = paramsScope;
5507 while (tab->lexlevel >= lowlevel)
5509 if (guardSet_mustBeNull (tab->guards, s))
5514 while (tab->kind == US_CBRANCH)
5519 llassert (usymtab_isDefined (tab));
5521 if (tab->kind == US_FBRANCH)
5524 llassert (tab->kind == US_TBRANCH);
5534 usymtab_printGuards ()
5535 /*@globals utab, globtab@*/
5537 usymtab ttab = utab;
5539 while (ttab != globtab)
5541 llmsg (message ("Guards [%d]: %q", ttab->lexlevel,
5542 guardSet_unparse (ttab->guards)));
5548 usymtab_displayAllUses ()
5549 /*@globals utab, globtab@*/
5553 /* only in top scope */
5554 llassert (utab == globtab);
5556 /* need a copy, so order is not messed up by sort! */
5557 copy = usymtab_shallowCopy (globtab);
5559 qsort (copy->entries, (size_t)copy->nentries,
5560 sizeof (*copy->entries), (int (*)(const void *, const void *)) uentry_xcompareuses);
5562 usymtab_entries (copy, ue)
5564 if (uentry_isValid (ue))
5566 filelocList uses = uentry_getUses (ue);
5567 int size = filelocList_realSize (uses);
5569 if (fileloc_isDefined (uentry_whereDefined (ue))
5570 && !fileloc_isLib (uentry_whereDefined (ue))
5573 llmsg (message ("%q (%q), %d use%&:\n %q",
5574 uentry_getName (ue),
5575 fileloc_unparse (uentry_whereDefined (ue)),
5576 size, filelocList_unparseUses (uses)));
5579 } end_usymtab_entries;
5581 usymtab_shallowFree (copy);
5584 static /*@dependent@*/ /*@exposed@*/ usymtab
5585 usymtab_getFileTab ()
5586 /*@globals filetab@*/
5588 llassert (filetab != NULL);
5594 usymtab_unparseStack ()
5597 return (usymtab_unparseStackTab (utab));
5600 static /*@only@*/ cstring
5601 usymtab_unparseStackTab (usymtab t)
5603 bool firstOne = TRUE;
5604 cstring ret = cstring_makeLiteral ("[");
5606 while (t != GLOBAL_ENV)
5610 ret = message ("%q %q", ret, usymtab_typeName (t));
5615 ret = message ("%q, %q", ret, usymtab_typeName (t));
5620 ret = message ("%q ]", ret);
5624 static /*@only@*/ cstring
5625 usymtab_typeName (/*@notnull@*/ usymtab t)
5629 case US_GLOBAL: return cstring_makeLiteral ("global");
5630 case US_NORMAL: return cstring_makeLiteral ("normal");
5631 case US_TBRANCH: return cstring_makeLiteral ("true");
5632 case US_FBRANCH: return cstring_makeLiteral ("false");
5633 case US_CBRANCH: return cstring_makeLiteral ("case");
5634 case US_SWITCH: return cstring_makeLiteral ("switch");
5640 void usymtab_addMustAlias (/*@exposed@*/ sRef s, /*@exposed@*/ sRef al)
5643 if (!sRef_similar (s, al))
5645 usymtab_addForceMustAlias (s, al);
5650 ** Same as usymtab_addMustAlias, except does not check sRef_isSimilar.
5653 void usymtab_addForceMustAlias (/*@exposed@*/ sRef s, /*@exposed@*/ sRef al)
5656 if (sRef_isMeaningful (s)
5657 && sRef_isMeaningful (al)
5658 && !(sRef_isConst (s) || sRef_isConst (al))
5659 && !(sRef_isAddress (al) && sRef_isDirectParam (sRef_getBase (al))))
5661 utab->aliases = aliasTable_addMustAlias (utab->aliases, s, al);
5662 DPRINTF (("Must alias: %s", aliasTable_unparse (utab->aliases)));
5665 ** for local variable, aliasing is symmetric
5668 if (sRef_isLocalVar (s) && sRef_isLocalVar (al))
5670 utab->aliases = aliasTable_addMustAlias (utab->aliases, al, s);
5679 void usymtab_clearAlias (sRef s)
5680 /*@modifies utab, s@*/
5683 aliasTable_clearAliases (utab->aliases, s);
5686 sRefSet usymtab_allAliases (sRef s)
5689 if (sRef_isMeaningful (s))
5693 ret = sRefSet_unionFree (aliasTable_aliasedBy (utab->aliases, s),
5694 aliasTable_canAlias (utab->aliases, s));
5699 DPRINTF (("NOT A MEANINGFUL SREF!"));
5700 return sRefSet_undefined;
5704 /*@only@*/ sRefSet usymtab_canAlias (sRef s)
5707 if (sRef_isMeaningful (s))
5709 sRefSet res = aliasTable_canAlias (utab->aliases, s);
5713 return sRefSet_undefined;
5716 /*@only@*/ sRefSet usymtab_aliasedBy (sRef s)
5719 return (aliasTable_aliasedBy (utab->aliases, s));
5722 /*@only@*/ cstring usymtab_unparseAliases ()
5725 return (aliasTable_unparse (utab->aliases));
5729 ** Debugging routines:
5730 ** okay to leak storage here, only for debugging
5736 usymtab_printOut (void)
5742 char *ind = mstring_copy (" ");
5744 fprintf (g_msgstream, "<<< [symbol table] >>>\n");
5746 while (s != GLOBAL_ENV && s->env != GLOBAL_ENV)
5748 cstring tname = usymtab_typeName (s);
5752 ind[depth * 3 + 1] = '\0';
5755 fprintf (g_msgstream, "level: %d (%s)\n", s->lexlevel,
5756 cstring_toCharsSafe (tname));
5758 cstring_free (tname);
5760 for (i = 0; i < s->nentries; i++)
5762 cstring us = uentry_unparseFull (s->entries[i]);
5763 fprintf (g_msgstream, "%s\n", cstring_toCharsSafe (us));
5767 if (s->reftable != NULL && s->nentries > 0)
5769 fprintf (g_msgstream, "\t<< Ref table >>\n");
5771 for (i = 0; i < s->nentries; i++)
5773 fprintf (g_msgstream, "\t%s %3d: %d, %d\n", ind, i,
5774 s->reftable[i]->level,
5775 s->reftable[i]->index);
5779 ind[depth * 3 + 1] = ' ';
5783 fprintf (g_msgstream, "<<< end usymtab >>>\n");
5789 usymtab_printTypes ()
5790 /*@globals globtab@*/
5792 usymtab_printAllAux (globtab);
5796 usymtab_printAll (void)
5799 usymtab_printAllAux (utab);
5803 usymtab_printAllAux (usymtab s)
5804 /*@modifies g_msgstream@*/
5808 char *ind = mstring_copy (" ");
5810 printf ("[[[ usymtab ]]]");
5812 while (s != GLOBAL_ENV)
5815 ind[depth * 3 + 1] = '\0';
5817 if (s->env == GLOBAL_ENV)
5821 printf ("level: %d / break: %s / exit: %s\n", s->lexlevel,
5822 cstring_toCharsSafe (bool_unparse (s->mustBreak)),
5823 cstring_toCharsSafe (exitkind_unparse (s->exitCode)));
5827 for (i = looplow; i < s->nentries; i++)
5829 printf ("%s%3d. %s\n", ind, i,
5830 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5835 printf ("level: %d / break: %s / exit: %s\n", s->lexlevel,
5836 cstring_toCharsSafe (bool_unparse (s->mustBreak)),
5837 cstring_toCharsSafe (exitkind_unparse (s->exitCode)));
5839 for (i = 0; i < s->nentries; i++)
5841 printf ("%s%3d %s\n", ind, i,
5842 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5846 ind[depth * 3 + 1] = ' ';
5850 printf ("----------\n");
5854 usymtab_printComplete ()
5859 char *ind = mstring_copy (" ");
5862 while (s != GLOBAL_ENV)
5866 ind[depth * 3 + 1] = '\0';
5869 if (s->env == GLOBAL_ENV)
5873 printf ("level: %d\n", s->lexlevel);
5877 for (i = looplow; i < s->nentries; i++)
5879 printf ("%s%3d %s\n", ind, i,
5880 cstring_toCharsSafe (uentry_unparse (s->entries[i])));
5885 printf ("level: %d\n", s->lexlevel);
5886 for (i = 0; i < s->nentries; i++)
5888 printf ("%s%3d %s\n", ind, i,
5889 cstring_toCharsSafe (uentry_unparse (s->entries[i])));
5893 ind[depth * 3 + 1] = ' ';
5898 printf ("----------\n");
5903 static /*@only@*/ cstring /*@unused@*/
5904 usymtab_unparseLocalAux (/*@notnull@*/ usymtab s)
5906 cstring c = message ("lexlevel: %d\n", s->lexlevel);
5909 for (i = 0; i < s->nentries; i++)
5911 c = message ("%q\n%q", c, uentry_unparseFull (s->entries[i]));
5914 c = message ("%q\n=========", c);
5918 static cstring /*@unused@*/ /*@only@*/
5919 usymtab_unparseLocalList (/*@notnull@*/ usymtab s)
5921 cstring c = message ("[%d/%s/%s] ", s->lexlevel,
5922 bool_unparse (s->mustBreak),
5923 exitkind_unparse (s->exitCode));
5926 for (i = 0; i < s->nentries; i++)
5928 sRef sr = uentry_getSref (s->entries[i]);
5932 c = message ("%q: %q [%b]", c, uentry_getName (s->entries[i]),
5933 sRef_isStateDefined (sr));
5937 c = message ("%q, %q [%b]", c, uentry_getName (s->entries[i]),
5938 sRef_isStateDefined (sr));
5948 usymtab_printLocal (void)
5954 printf ("lexlevel: %d\n", s->lexlevel);
5956 for (i = 0; i < s->nentries; i++)
5958 printf ("%s\n", cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5961 while (s->lexlevel > 1)
5966 llassert (usymtab_isDefined (s));
5968 printf ("Params:\n");
5970 for (i = 0; i < s->nentries; i++)
5972 printf ("%d: %s\n", i,
5973 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5978 static bool checkDistinctExternalName (uentry e)
5979 /*@globals globtab@*/
5980 /*@modifies *g_msgstream@*/
5982 int checklen = context_getValue (FLG_EXTERNALNAMELEN);
5983 bool ignorecase = context_getFlag (FLG_EXTERNALNAMECASEINSENSITIVE);
5984 bool gotone = FALSE;
5985 bool extras = FALSE;
5986 bool hasError = FALSE;
5987 cstring name = uentry_rawName (e);
5988 usymtab st = globtab;
5996 if (uentry_isAnyTag (e))
5998 checklen++; /* the tag marker doesn't count */
6002 usymtab_entries (st, oe)
6004 if (uentry_sameObject (oe, e))
6011 if (cstring_equalCaseInsensitive (uentry_rawName (oe), name))
6020 (FLG_DISTINCTEXTERNALNAMES,
6022 ("External identifier %q is not distinguishable from %q "
6023 "because alphabetical case is ignored",
6025 uentry_getName (oe)),
6026 uentry_whereLast (e)))
6028 uentry_showWhereAny (oe);
6029 uentry_setHasNameError (oe);
6038 if (cstring_equalLenCaseInsensitive (uentry_rawName (oe),
6047 if (cstring_equalLen (uentry_rawName (oe), name, checklen))
6050 (FLG_DISTINCTEXTERNALNAMES,
6053 ("External identifier %q is not distinguishable from %q "
6054 "in the first %d characters (%q)",
6056 uentry_getName (oe),
6058 cstring_clip (uentry_getName (e), checklen)),
6060 uentry_whereLast (e)))
6062 uentry_showWhereAny (oe);
6063 uentry_setHasNameError (oe);
6076 (FLG_DISTINCTEXTERNALNAMES,
6078 ("External identifier %q is not distinguishable from %q "
6079 "in the first %d characters because alphabetical case "
6082 uentry_getName (oe),
6084 uentry_whereLast (e)))
6086 uentry_showWhereAny (oe);
6087 uentry_setHasNameError (oe);
6093 else if (cstring_equalLen (uentry_rawName (oe), name, checklen))
6102 (FLG_DISTINCTEXTERNALNAMES,
6105 ("External identifier %q is not distinguishable from %q "
6106 "in the first %d characters (%q)",
6108 uentry_getName (oe),
6110 cstring_clip (uentry_getName (e), checklen)),
6112 uentry_whereLast (e)))
6114 uentry_showWhereAny (oe);
6115 uentry_setHasNameError (oe);
6124 } end_usymtab_entries ;
6131 (cstring_makeLiteral ("One or more additional "
6132 "indistinguishable external "
6133 "names not reported"));
6139 static bool checkDistinctInternalName (uentry e)
6141 /*@modifies *g_msgstream@*/
6143 usymtab ttab = utab;
6144 cstring name = uentry_rawName (e);
6145 int numchars = context_getValue (FLG_INTERNALNAMELEN);
6146 bool caseinsensitive = context_getFlag (FLG_INTERNALNAMECASEINSENSITIVE);
6147 bool lookalike = context_getFlag (FLG_INTERNALNAMELOOKALIKE);
6149 if (uentry_isAnyTag (e) && (numchars != 0))
6151 numchars++; /* the tag marker doesn't count */
6154 while (usymtab_isDefined (ttab))
6156 usymtab_entries (ttab, oe)
6158 if (uentry_sameObject (oe, e))
6160 /*@innercontinue@*/ continue;
6163 switch (cstring_genericEqual
6164 (name, uentry_rawName (oe),
6165 numchars, caseinsensitive, lookalike))
6167 case CGE_DISTINCT: /* okay */
6171 if (cstring_equal (name, uentry_rawName (oe)))
6173 ; /* got a shadow error */
6178 (FLG_DISTINCTINTERNALNAMES,
6181 ("Internal identifier %q is not distinguishable from %q "
6182 "in the first %d characters (%q)",
6184 uentry_getName (oe),
6186 cstring_clip (uentry_getName (e), numchars)),
6188 uentry_whereLast (e)))
6190 uentry_showWhereAny (oe);
6191 uentry_setHasNameError (oe);
6199 || (cstring_length (name) <= numchars))
6202 (FLG_DISTINCTINTERNALNAMES,
6204 ("Internal identifier %q is not distinguishable from %q "
6205 "without case sensitivity",
6207 uentry_getName (oe)),
6208 uentry_whereLast (e)))
6210 uentry_showWhereAny (oe);
6211 uentry_setHasNameError (oe);
6218 (FLG_DISTINCTINTERNALNAMES,
6220 ("Internal identifier %q is not distinguishable from %q "
6221 "in the first %d characters without case sensitivity",
6223 uentry_getName (oe),
6225 uentry_whereLast (e)))
6227 uentry_showWhereAny (oe);
6228 uentry_setHasNameError (oe);
6236 || (cstring_length (name) <= numchars))
6239 (FLG_DISTINCTINTERNALNAMES,
6241 ("Internal identifier %q is not distinguishable from %q "
6242 "except by lookalike characters",
6244 uentry_getName (oe)),
6245 uentry_whereLast (e)))
6247 uentry_showWhereAny (oe);
6248 uentry_setHasNameError (oe);
6255 (FLG_DISTINCTINTERNALNAMES,
6257 ("Internal identifier %q is not distinguishable from %q "
6258 "in the first %d characters except by lookalike characters",
6260 uentry_getName (oe),
6262 uentry_whereLast (e)))
6264 uentry_showWhereAny (oe);
6265 uentry_setHasNameError (oe);
6270 } end_usymtab_entries ;
6278 void usymtab_checkDistinctName (uentry e, int scope)
6279 /*@globals utab, globtab@*/
6281 bool hasError = FALSE;
6282 fileloc where = uentry_whereLast (e);
6284 if (!fileloc_isPreproc (where) && !fileloc_isBuiltin (where))
6286 if (scope == globScope)
6288 if (context_getFlag (FLG_DISTINCTEXTERNALNAMES))
6290 hasError = checkDistinctExternalName (e);
6294 if (!hasError && context_getFlag (FLG_DISTINCTINTERNALNAMES))
6296 hasError = checkDistinctInternalName (e);
6302 uentry_setHasNameError (e);
6306 /*@exposed@*/ sRef usymtab_lookupGlobalMarker (void) /*@globals utab@*/
6310 ue = usymtab_lookupAux (utab, GLOBAL_MARKER_NAME);
6311 llassert (uentry_isValid (ue));
6313 return uentry_getSref (ue);