2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2001 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 lclint: lclint-request@cs.virginia.edu
21 ** To report a bug: lclint-bug@cs.virginia.edu
22 ** For more information: http://lclint.cs.virginia.edu
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 "lclintMacros.nf"
51 # include "structNames.h"
52 # include "exprChecks.h"
53 # include "transferChecks.h"
56 ** Keep track of type definitions inside a function.
59 static uentryList functypes = uentryList_undefined;
61 static bool dbgfree = FALSE;
62 static bool dbgload = TRUE;
67 ** Hack to prevent shadow errors from appearing when function parameters
68 ** are entered into a new scope.
71 static bool noshadowerror = FALSE;
74 ** Constraint: static variables can live in 1 or > 2.
76 ** except for tags. All tags must be at the global level.
77 ** Static tags are handled in a kludgey way.
81 ** utab is the universal symbol table
82 ** globtab is the global environment of utab
83 ** oldtab is needed for conversions in dumping (after sorting the table)
85 ** there can be only one usymtab!
88 static /*@checkedstrict@*/ /*@owned@*/ /*@notnull@*/ usymtab utab;
90 /* Reachable environments from from utab */
91 static /*@checkedstrict@*/ /*@notnull@*/ /*@dependent@*/ usymtab globtab;
92 static /*@checkedstrict@*/ /*@dependent@*/ usymtab filetab;
94 /* Used for sorting for dumpage. */
95 static /*@checkedstrict@*/ /*@owned@*/ usymtab oldtab;
97 static int usymtab_lexicalLevel (void) /*@globals utab@*/ ;
98 static bool usymtab_isAltDefinitelyNull (sRef p_s) /*@globals utab@*/ ;
99 static void refTable_free (/*@only@*/ /*@null@*/ refTable p_x, int p_nentries);
100 static ctype usymtab_suFieldsType (uentryList p_f, bool p_isStruct) /*@globals globtab@*/ ;
102 extern int usymtab_getCurrentDepth (void) /*@globals utab@*/
104 return utab->lexlevel;
108 usymtab_freeLevel (/*@notnull@*/ /*@only@*/ usymtab p_u)
109 /*@globals globtab, utab, filetab@*/ /*@modifies p_u@*/ ;
111 static bool usymtab_isDefinitelyNullAux (sRef p_s) /*@globals utab@*/ ;
112 static /*@only@*/ cstring usymtab_unparseStackTab (usymtab p_t);
113 static /*@exposed@*/ /*@dependent@*/ uentry
114 usymtab_getRefTab (/*@notnull@*/ usymtab p_u, int p_level, usymId p_index);
115 static /*@unused@*/ /*@only@*/ cstring
116 usymtab_unparseLocalAux (/*@notnull@*/ usymtab p_s);
117 static /*@unused@*/ /*@only@*/ cstring
118 usymtab_unparseLocalList (/*@notnull@*/ usymtab p_s);
119 static /*@only@*/ cstring usymtab_typeName (/*@notnull@*/ usymtab p_t);
120 static void usymtab_handleParams (void)
121 /*@globals utab, globtab, filetab@*/
122 /*@modifies utab@*/ ;
124 static /*@exposed@*/ /*@dependent@*/ uentry
125 usymtab_addRefEntry (/*@notnull@*/ usymtab p_s, cstring p_k);
126 static /*@exposed@*/ /*@dependent@*/ usymtab
127 usymtab_dropEnv (/*@notnull@*/ usymtab p_s);
128 static /*@exposed@*/ /*@dependent@*/ uentry
129 usymtab_getRefNoisy (/*@notnull@*/ usymtab p_s, int p_level, usymId p_index);
131 static /*@exposed@*/ /*@dependent@*/ uentry
132 usymtab_lookupQuietAux (usymtab p_s, cstring p_k, bool p_noalt);
134 static /*@exposed@*/ /*@dependent@*/ uentry
135 usymtab_lookupQuiet (usymtab p_s, cstring p_k);
137 static /*@exposed@*/ /*@dependent@*/ uentry
138 usymtab_lookupQuietNoAlt (usymtab p_s, cstring p_k);
140 static void usymtab_printAllAux (usymtab p_s) /*@modifies g_msgstream@*/ ;
141 static int usymtab_getIndex (/*@notnull@*/ usymtab p_s, cstring p_k);
142 static /*@exposed@*/ uentry usymtab_fetchIndex (/*@notnull@*/ usymtab p_s, int p_i);
143 static /*@exposed@*/ uentry
144 usymtab_lookupAux (usymtab p_s, cstring p_k);
145 static /*@exposed@*/ /*@dependent@*/ /*@notnull@*/ usymtab
146 usymtab_getFileTab (void) /*@globals filetab@*/ ;
147 static int refTable_lookup (/*@notnull@*/ usymtab p_ut, int p_level, int p_index);
148 static bool usymtab_mustBreak (usymtab p_s);
149 static bool usymtab_mustEscape (usymtab p_s);
151 static void recordFunctionType (uentry ue)
153 llassert (uentry_isDatatype (ue) || uentry_isAnyTag (ue)
154 || uentry_isEnumConstant (ue));
155 DPRINTF (("Function type: %s", uentry_unparseFull (ue)));
157 functypes = uentryList_add (functypes, ue);
161 static void clearFunctionTypes (void)
162 /*@modifies globtab@*/
164 uentryList_elements (functypes, el)
166 if (cstring_isDefined (uentry_rawName (el)))
168 if (cstringTable_isDefined (globtab->htable))
170 cstringTable_remove (globtab->htable, uentry_rawName (el));
173 uentry_setName (el, cstring_undefined);
177 llassert (uentry_isValid (el));
178 el->sref = sRef_undefined;
179 /*@noaccess uentry@*/
180 } end_uentryList_elements ;
182 uentryList_clear (functypes);
185 static /*@falsenull@*/ bool usymtab_isBranch (usymtab u)
187 return (usymtab_isDefined (u) &&
188 (u->kind == US_TBRANCH || u->kind == US_FBRANCH
189 || u->kind == US_CBRANCH || u->kind == US_SWITCH));
192 static bool usymtab_mustBreak (usymtab s)
194 llassert (s != GLOBAL_ENV);
195 return (s->mustBreak);
198 static bool usymtab_mustEscape (usymtab s)
200 llassert (s != GLOBAL_ENV);
201 return (exitkind_mustEscape (s->exitCode));
204 void usymtab_setMustBreak ()
207 llassert (utab != GLOBAL_ENV);
208 utab->mustBreak = TRUE;
211 void usymtab_setExitCode (exitkind ex)
214 llassert (utab != GLOBAL_ENV);
218 if (exitkind_mustEscape (ex))
220 utab->mustBreak = TRUE;
224 bool usymtab_isAltDefinitelyNullDeep (sRef s)
226 return (sRef_deepPred (usymtab_isAltDefinitelyNull, s));
229 static bool usymtab_isAltDefinitelyNull (sRef s)
237 llassert (utab->env != NULL);
239 /*@-mods@*/ /* These modifications are cancelled. */
240 utab->guards = utab->env->guards;
241 utab->env->guards = t;
244 llassert (usymtab_isDefined (utab));
245 res = usymtab_isDefinitelyNull (s);
248 ** This reports a spurious error. It is okay, because of
249 ** the nesting relationship of usymtab environments.
252 /*@-mods@*/ /* Cancelling modifications. */
253 /*@i1@*/ utab->env->guards = utab->guards;
254 /*@i1@*/ utab->guards = t;
260 static /*@notnull@*/ /*@special@*/ usymtab
261 usymtab_create (uskind kind, /*@keep@*/ usymtab env, bool nextlevel)
263 /*@post:isnull result->htable, result->guards, result->aliases@*/
265 usymtab t = (usymtab) dmalloc (sizeof (*t));
268 t->nspace = CBASESIZE;
269 t->entries = (uentry *) dmalloc (sizeof (*t->entries) * CBASESIZE);
271 /* We only use a reftable for branch-level symbol tables.
274 t->reftable = (nextlevel
276 : (refentry *) dmalloc (sizeof (*t->reftable) * CBASESIZE));
279 t->lexlevel = (env == GLOBAL_ENV ? 0 : env->lexlevel) + (nextlevel ? 1 : 0);
284 t->guards = guardSet_undefined;
285 t->aliases = aliasTable_undefined;
287 t->mustBreak = FALSE;
288 t->exitCode = XK_NEVERESCAPE;
297 static /*@only@*/ /*@notnull@*/ usymtab
298 usymtab_createRoot (void) /*@modifies nothing@*/
300 usymtab u = (usymtab) dmalloc (sizeof (*u));
303 u->nspace = CGLOBBASESIZE;
304 u->entries = (uentry *) dmalloc (sizeof (*u->entries) * CGLOBBASESIZE);
307 u->htable = cstringTable_create (CGLOBHASHSIZE);
310 u->guards = guardSet_new ();
311 u->aliases = aliasTable_new ();
313 u->mustBreak = FALSE;
314 u->exitCode = XK_NEVERESCAPE;
321 usymtab_initMod (void)
322 /*@globals undef utab, undef oldtab, undef globtab, undef filetab @*/
323 /*@modifies utab, globtab, filetab, oldtab @*/
325 utab = usymtab_createRoot ();
327 filetab = usymtab_undefined;
328 oldtab = usymtab_undefined;
333 usymtab_initGlobalMarker () /*@globals globtab@*/
335 if (uentry_isValid (usymtab_lookupAux (globtab, GLOBAL_MARKER_NAME)))
337 ; /* Already entered from load table. */
341 usymtab_addGlobalEntry (uentry_makeGlobalMarker ());
346 ** utab should be empty? (requires?)
348 ** Adds bool types to symbol table (these are built in for now)
349 ** Only do this is there is no library!
355 if (context_getFlag (FLG_NOLIB))
357 ctype boolt = ctype_bool;
358 /* evs 2000-07-24: bool is now treated as abstract (always) */
360 uentry boolentry = uentry_makeBoolDatatype (YES);
361 usymtab_supGlobalEntry (boolentry);
362 context_addBoolAccess ();
365 ** We supply values 0 and 1 for the constants, so things like
366 ** while (TRUE) can be interpreted correctly.
369 usymtab_supGlobalEntry
370 (uentry_makeConstantAux (context_getFalseName (), boolt,
371 fileloc_getBuiltin (), FALSE,
372 multiVal_makeInt (0)));
373 usymtab_supGlobalEntry
374 (uentry_makeConstantAux (context_getTrueName (), boolt,
375 fileloc_getBuiltin (), FALSE,
376 multiVal_makeInt (1)));
385 usymtab_grow (/*@notnull@*/ usymtab s)
388 o_uentry *oldsyms = s->entries;
390 s->nspace = CBASESIZE;
391 s->entries = (uentry *) dmalloc (sizeof (*s->entries)
392 * (s->nentries + s->nspace));
394 for (i = 0; i < s->nentries; i++)
396 s->entries[i] = oldsyms[i];
401 if (s->reftable != NULL)
403 refTable oldRefs = s->reftable;
405 s->reftable = (refentry *) dmalloc (sizeof (*s->reftable)
406 * (s->nentries + CBASESIZE + 1));
408 for (i = 0; i < s->nentries; i++)
410 s->reftable[i] = oldRefs[i];
421 usymtab_addEntryQuiet (/*@notnull@*/ usymtab s, /*@keep@*/ uentry e)
423 if (!(s->lexlevel > fileScope || !sRef_modInFunction ()))
425 if (uentry_isDatatype (e)
426 || uentry_isFunction (e)
427 || uentry_isAnyTag (e)
428 || uentry_isEnumConstant (e)
429 || context_inMacro ())
432 ** Not a bug. Code like,
433 ** int f (void) { typedef int tint; ... }
437 /* was nothing here! */
438 /*@i@*/ e->sref = sRef_saveCopy (e->sref);
443 (cstring_makeLiteral ("Declaration outside function scope"));
445 llcontbug (message ("usymtab_addEntryQuiet: inconsistent state "
446 "(lexlevel = %d, modFunction = %s) adding: %q",
447 s->lexlevel, bool_unparse (sRef_modInFunction ()),
448 uentry_unparse (e)));
449 sRef_setGlobalScope ();
450 /* make sure the sRef is not bashed! */
451 /*@i@*/ e->sref = sRef_saveCopy (e->sref);
461 s->entries[s->nentries] = e;
464 if (s == globtab || s == filetab)
466 if (!fileloc_isLib (g_currentloc))
468 uentry_tallyAnnots (e, AN_UNKNOWN);
473 if (cstringTable_isDefined (s->htable))
475 cstringTable_insert (s->htable, cstring_copy (uentry_rawName (e)), s->nentries);
481 static /*@observer@*/ uentry /*@alt void@*/
482 usymtab_addEntryBase (/*@notnull@*/ usymtab s, /*@only@*/ uentry e)
485 ** In theory, we shouldn't need this test because it this is
486 ** only called when a library is being read, and it shouldn't
487 ** ever have a duplicate entry. In practice, its safer to
488 ** leave it in, though.
493 if (uentry_isValid (old = usymtab_lookupQuiet (s, uentry_rawName (e))))
495 DPRINTF (("Duplicate entry: %s", uentry_unparse (e)));
496 uentry_free (e); /* duplicate */
501 int thisentry = s->nentries;
503 if (uentry_isVar (e))
505 uentry_setSref (e, sRef_makeCvar (globScope, thisentry,
507 stateInfo_makeLoc (uentry_whereLast (e))));
510 usymtab_addEntryQuiet (s, e);
516 static /*@observer@*/ uentry /*@alt void@*/
517 usymtab_addEntryAlways (/*@notnull@*/ usymtab s, /*@only@*/ uentry e)
520 ** In theory, we shouldn't need this test because it this is
521 ** only called when a library is being read, and it shouldn't
522 ** ever have a duplicate entry. In practice, its safer to
523 ** leave it in, though.
527 int thisentry = s->nentries;
529 if (uentry_isValid (old = usymtab_lookupQuiet (s, uentry_rawName (e))))
532 (message ("Duplicate entry in load library: %s. "
533 "Old entry: %q. New entry: %q",
535 uentry_unparseFull (old),
536 uentry_unparseFull (e)));
538 uentry_setName (e, message ("__x_%s", uentry_rawName (e)));
539 /* This shouldn't happen...unless the library is bad! */
543 if (uentry_isVar (e) && !uentry_isGlobalMarker (e))
545 uentry_setSref (e, sRef_makeCvar (globScope, thisentry,
547 stateInfo_makeLoc (uentry_whereLast (e))));
550 usymtab_addEntryQuiet (s, e);
555 usymtab_addEntryAux (/*@notnull@*/ usymtab st, /*@keep@*/ uentry e, bool isSref)
556 /*@globals globtab@*/
559 usymId thisentry = st->nentries;
561 llassert (!uentry_isElipsisMarker (e));
565 ** llassert (usymtab_lookupSafe (uentry_rawName (e)) == uentry_undefined);
568 llassertprint (uentry_isAnyTag (e) ? st == globtab : TRUE,
569 ("non-global tag: %s / %d", uentry_unparseFull (e), st->lexlevel));
571 if (st->lexlevel == fileScope
572 && (!(uentry_isStatic (e)) || uentry_isAnyTag (e)))
579 ctype ct = uentry_getType (e);
581 if (uentry_isFunction (e) && ctype_isFunction (ct))
583 ct = ctype_getReturnType (ct);
586 if (uentry_isStatic (e))
588 sRef sr = sRef_makeCvar (st->lexlevel, thisentry, ct, stateInfo_makeLoc (uentry_whereLast (e)));
590 if (sRef_isStack (sr) || sRef_isLocalState (sr))
592 sRef_setAliasKind (sr, AK_STATIC, uentry_whereLast (e));
593 sRef_setDefined (sr, uentry_whereLast (e));
596 uentry_setSref (e, sr);
600 uentry_setSref (e, sRef_makeCvar (st->lexlevel, thisentry, ct, stateInfo_makeLoc (uentry_whereLast (e))));
604 if (uentry_isDatatype (e))
607 uentry_setDatatype (e, thisentry);
610 if (uentry_isFunction (e))
612 ctype ct = uentry_getType (e);
614 if (ctype_isFunction (ct)
615 && uentryList_isMissingParams (ctype_argsFunction (ct)))
617 if (uentry_isDeclared (e))
621 message ("Function %q declared without parameter list",
623 uentry_whereDeclared (e));
629 message ("Function %q specified without parameter list",
631 uentry_whereSpecified (e));
636 if (st == globtab && !uentry_isSpecified (e))
638 exprChecks_checkExport (e);
641 uentry_checkName (e);
643 usymtab_addEntryQuiet (st, e);
644 DPRINTF (("Adding entry: [%p] %s", e, uentry_unparseFull (e)));
649 usymtab_addEntry (uentry e)
650 /*@globals utab, globtab@*/
651 /*@modifies utab, e@*/
653 llassertprint (!usymtab_exists (uentry_rawName (e)),
654 ("Entry already exists: %s", uentry_unparse (e)));
656 return usymtab_addEntryAux (utab, e, FALSE);
660 usymtab_addGlobalEntry (uentry e)
661 /*@modifies globtab, e@*/
663 DPRINTF (("Add global: %s / %s", uentry_unparse (e), bool_unparse (uentry_isForward (e))));
665 (void) usymtab_addEntryAux (globtab, e, FALSE);
669 ** supercede and replace entry use elements of entries array, and
670 ** assign an element to a new value, and then free the old value.
671 ** Need -deparrays to avoid errors for this.
677 usymtab_supEntryAux (/*@notnull@*/ usymtab st,
678 /*@only@*/ uentry e, bool isSref)
679 /*@globals globtab, filetab@*/
680 /*@modifies st, globtab, e@*/
682 cstring ename = uentry_rawName (e);
683 bool staticEntry = FALSE;
686 DPRINTF (("Sup entry aux: %s", uentry_unparseFull (e)));
688 /* static tags in global scope */
689 if (st->lexlevel == fileScope
690 && (!(uentry_isStatic (e)) || uentry_isAnyTag (e)))
692 eindex = usymtab_getIndex (st, ename);
694 if (eindex != NOT_FOUND)
696 uentry ce = st->entries[eindex];
700 message ("%s %q shadows static declaration",
701 ekind_capName (uentry_getKind (e)),
703 uentry_whereDeclared (e)))
705 uentry_showWhereLast (ce);
708 if (eindex == st->nentries - 1)
714 st->entries[eindex] = st->entries[st->nentries - 1];
717 if (cstringTable_isDefined (st->htable))
719 cstringTable_replaceKey (st->htable, uentry_rawName (ce),
720 cstring_copy (uentry_rawName (e)));
730 if (uentry_isStatic (e)) {
731 if (uentry_isFunction (e)) {
733 ** Static function declarations are at the file level,
734 ** even if they are in a deeper scope.
737 st = usymtab_getFileTab ();
740 if (!uentry_isAnyTag (e) && st == globtab)
742 st = usymtab_getFileTab ();
744 DPRINTF (("Static entry!"));
749 DPRINTF (("Using symtab: %s", usymtab_unparseLocalAux (st)));
751 eindex = usymtab_getIndex (st, ename);
753 if (eindex != NOT_FOUND)
755 uentry ce = st->entries[eindex];
757 DPRINTF (("Found entry: %s", uentry_unparse (ce)));
759 if (uentry_isPriv (ce)
760 /* || (fileloc_isImport (uentry_whereSpecified (ce))) */
761 || (uentry_isStatic (ce)
762 && uentry_isAnyTag (ce)
763 && (uentry_isDeclared (ce)
764 && !fileloc_sameFile (uentry_whereDefined (ce),
765 uentry_whereDefined (e)))))
769 ** if overloading import should do some checks!
772 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
774 DPRINTF (("Overloading!"));
776 st->entries[eindex] = e;
778 if (uentry_isDatatype (e))
780 uentry_setDatatype (e, eindex);
783 if (st == globtab && !uentry_isSpecified (e))
785 exprChecks_checkExport (e);
788 if (cstringTable_isDefined (st->htable))
790 cstringTable_replaceKey (st->htable, uentry_rawName (ce),
791 cstring_copy (uentry_rawName (e)));
797 else if (uentry_isSpecified (ce))
799 if (uentry_isSpecified (e))
801 DPRINTF (("Here we are: %s", uentry_unparseFull (e)));
803 if (fileloc_isImport (uentry_whereSpecified (ce)))
805 if (cstringTable_isDefined (st->htable))
807 cstringTable_replaceKey (st->htable,
809 cstring_copy (uentry_rawName (e)));
813 st->entries[eindex] = e;
816 if (uentry_isDatatype (e)) uentry_setDatatype (e, eindex);
820 if (fileloc_isImport (uentry_whereSpecified (e)))
826 /* Respecification errors already reported */
827 DPRINTF (("Respecification: %s / %s",
828 fileloc_unparse (uentry_whereSpecified (e)),
829 bool_unparse (fileloc_isSpec (uentry_whereSpecified (e)))));
831 if (uentry_isDatatype (e))
833 uentry_setDatatype (e, eindex);
836 if (cstringTable_isDefined (st->htable))
838 cstringTable_replaceKey (st->htable,
840 cstring_copy (uentry_rawName (e)));
843 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
845 st->entries[eindex] = e;
850 else /* e not specified */
852 DPRINTF (("Merging..."));
854 if (uentry_isDeclared (ce))
856 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
857 DPRINTF (("Merge defn"));
858 uentry_mergeDefinition (ce, e);
862 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
863 DPRINTF (("Merge entries..."));
864 uentry_mergeEntries (ce, e);
865 DPRINTF (("After: %s", uentry_unparseFull (ce)));
869 else /* ce not specified */
871 if (!(st->lexlevel > fileScope || !sRef_modInFunction ()))
873 if (uentry_isDatatype (e) || uentry_isAnyTag (e)
874 || uentry_isEnumConstant (e))
877 ** Not a bug. Code like,
878 ** int f (void) { typedef int tint; ... }
884 llcontbug (message ("usymtab_supEntryAux: inconsistent state "
885 "(lexlevel = %d, modFunction = %s) adding: %q",
886 st->lexlevel, bool_unparse (sRef_modInFunction ()),
887 uentry_unparse (e)));
889 if (sRef_modInFunction ())
891 /* make sure the sRef is not bashed! */
892 /*@i@*/ e->sref = sRef_saveCopy (e->sref);
897 DPRINTF (("Merge.."));
898 uentry_mergeDefinition (ce, e);
903 ctype ct = uentry_getType (ce);
905 if (uentry_isFunction (ce) && ctype_isFunction (ct))
907 ct = ctype_getReturnType (ct);
910 uentry_setSref (ce, sRef_makeCvar (st->lexlevel, eindex, ct, stateInfo_makeLoc (uentry_whereLast (ce))));
913 else /* no previous entry */
917 if (st->lexlevel == globScope
918 && !uentry_isStatic (e)
919 && !uentry_isExtern (e)
920 && usymtab_isDefined (filetab))
923 ** check if there is a static entry: (i.e.,
930 eindex = usymtab_getIndex (filetab, ename);
932 if (eindex != NOT_FOUND)
934 uentry ce = filetab->entries[eindex];
936 uentry_setStatic (e);
937 uentry_mergeDefinition (ce, e);
943 outer = usymtab_lookupQuiet (st->env, ename);
945 DPRINTF (("New : [%p] %s", e, uentry_unparseFull (e)));
946 DPRINTF (("Outer: [%p] %s", outer, uentry_unparseFull (outer)));
949 ** no previous definition, add the new one
953 && uentry_isValid (outer)
954 && !(uentry_isYield (e) || uentry_isYield (outer))
955 && fileloc_isDefined (uentry_whereLast (e))
956 && !fileloc_isXHFile (uentry_whereLast (e))
957 && fileloc_isDefined (uentry_whereLast (outer))
958 && !fileloc_isXHFile (uentry_whereLast (outer)))
960 if (!uentry_sameKind (outer, e))
966 if (ctype_isUnknown (uentry_getType (outer))
967 || uentry_isForward (outer))
975 message ("%s %q shadows outer declaration",
976 ekind_capName (uentry_getKind (e)),
978 uentry_whereLast (e)))
980 uentry_showWhereLast (outer);
986 if (st == globtab && context_getFlag (FLG_NEWDECL))
990 message ("New declaration: %q", uentry_getName (e)),
991 uentry_whereLast (e));
994 eindex = usymtab_addEntryAux (st, e, isSref);
998 return (staticEntry ? USYMIDINVALID : eindex);
1003 usymtab_replaceEntryAux (/*@notnull@*/ usymtab st, /*@only@*/ uentry e)
1004 /*@globals globtab@*/ /*@modifies st, e@*/
1006 cstring ename = uentry_rawName (e);
1009 /* static tags in global scope */
1010 eindex = usymtab_getIndex (st, ename);
1012 if (eindex != NOT_FOUND)
1014 uentry ce = st->entries[eindex];
1016 if (cstringTable_isDefined (st->htable))
1018 cstringTable_replaceKey (st->htable, uentry_rawName (ce),
1019 cstring_copy (uentry_rawName (e)));
1023 st->entries[eindex] = e;
1027 eindex = usymtab_addEntryAux (st, e, FALSE);
1034 void usymtab_supEntry (uentry e)
1035 /*@globals utab, filetab, globtab@*/
1036 /*@modifies utab, globtab, e@*/
1038 (void) usymtab_supEntryAux (utab, e, FALSE);
1042 ** this should be lots more efficient!
1045 static /*@exposed@*/ uentry
1046 usymtab_supEntryReturnAux (/*@notnull@*/ usymtab tab,
1047 /*@only@*/ uentry e, bool isref)
1048 /*@globals globtab, filetab@*/
1049 /*@modifies tab, globtab, e@*/
1051 cstring rawName = cstring_copy (uentry_rawName (e));
1052 bool stat = (tab == globtab) && uentry_isStatic (e);
1055 (void) usymtab_supEntryAux (tab, e, isref);
1059 ret = usymtab_lookupAux (filetab, rawName);
1063 ret = usymtab_lookupAux (tab, rawName);
1065 if (uentry_isInvalid (ret) && usymtab_isDefined (filetab))
1067 ret = usymtab_lookupAux (filetab, rawName);
1071 cstring_free (rawName);
1075 /*@dependent@*/ /*@exposed@*/ uentry
1076 usymtab_supEntryReturn (/*@only@*/ uentry e)
1077 /*@globals utab, filetab, globtab@*/
1078 /*@modifies utab, globtab, e@*/
1080 return (usymtab_supEntryReturnAux (utab, e, FALSE));
1083 /*@dependent@*/ /*@exposed@*/ uentry
1084 usymtab_supEntrySrefReturn (/*@only@*/ uentry e)
1085 /*@globals utab, globtab, filetab@*/
1086 /*@modifies utab, globtab, e@*/
1088 return (usymtab_supEntryReturnAux (utab, e, TRUE));
1091 /*@dependent@*/ /*@exposed@*/ uentry
1092 usymtab_supGlobalEntryReturn (uentry e)
1093 /*@globals globtab, filetab@*/
1094 /*@modifies globtab, e@*/
1098 ret = usymtab_supEntryReturnAux (globtab, e, FALSE);
1101 ** We need to keep track of internal function declarations, so
1102 ** we can remove them from the symbol table after exiting this
1103 ** function. This is a bit bogus, of course.
1106 if (sRef_modInFunction ())
1108 recordFunctionType (ret);
1115 usymtab_supTypeEntry (/*@only@*/ uentry e)
1116 /*@globals globtab, filetab@*/
1117 /*@modifies globtab, e@*/
1122 if (uentry_isAbstractDatatype (e))
1124 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1125 ret = ctype_createAbstract (uid);
1129 uid = usymtab_supEntryAux (globtab, e, FALSE);
1130 ret = ctype_createUser (uid);
1133 if (sRef_modInFunction ())
1135 recordFunctionType (globtab->entries[uid]);
1142 usymtab_supReturnTypeEntry (/*@only@*/ uentry e)
1143 /*@globals globtab, filetab@*/
1144 /*@modifies globtab@*/
1148 DPRINTF (("Abstract? %s", uentry_unparseFull (e)));
1150 if (uentry_isAbstractDatatype (e))
1152 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1154 else if (uentry_isMaybeAbstract (e) && context_getFlag (FLG_IMPABSTRACT))
1156 bool maybeabs = TRUE;
1157 cstring sname = uentry_getName (e);
1158 uentry ue = usymtab_lookupGlobSafe (sname);
1159 cstring_free (sname);
1161 if (uentry_isValid (ue))
1163 DPRINTF (("Lookup: %s", uentry_unparseFull (ue)));
1165 if (uentry_isDatatype (ue))
1167 if (uentry_isMaybeAbstract (ue))
1178 DPRINTF (("Not datatype!"));
1185 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1186 ux = usymtab_getTypeEntry (uid);
1187 uentry_setAbstract (ux);
1191 uid = usymtab_supEntryAux (globtab, e, FALSE);
1192 e = usymtab_getTypeEntry (uid);
1194 if (uentry_isMaybeAbstract (e))
1196 uentry_setConcrete (e);
1202 uid = usymtab_supEntryAux (globtab, e, FALSE);
1203 e = usymtab_getTypeEntry (uid);
1205 if (uentry_isMaybeAbstract (e))
1207 uentry_setConcrete (e);
1211 if (sRef_modInFunction ())
1213 recordFunctionType (globtab->entries[uid]);
1216 return (globtab->entries[uid]);
1220 usymtab_supAbstractTypeEntry (/*@only@*/ uentry e, bool dodef)
1221 /*@globals globtab, filetab@*/
1222 /*@modifies globtab, e@*/
1227 uid = usymtab_supEntryAux (globtab, e, FALSE);
1228 ue = usymtab_getTypeEntry (uid);
1232 uentry_setDatatype (ue, uid);
1235 if (context_getFlag (FLG_ACCESSMODULE)) /* was accessfile */
1237 context_addFileAccessType (uid);
1240 if (sRef_modInFunction ())
1242 recordFunctionType (globtab->entries[uid]);
1250 usymtab_supExposedTypeEntry (/*@only@*/ uentry e, bool dodef)
1251 /*@globals globtab, filetab@*/
1252 /*@modifies globtab, e@*/
1256 uid = usymtab_supEntryAux (globtab, e, FALSE);
1260 uentry ue = usymtab_getTypeEntry (uid);
1262 uentry_setDatatype (ue, uid);
1265 if (sRef_modInFunction ())
1267 recordFunctionType (globtab->entries[uid]);
1275 usymtab_supForwardTypeEntry (/*@only@*/ uentry e)
1276 /*@globals globtab, filetab@*/
1277 /*@modifies globtab, e@*/
1279 usymId uid = usymtab_supEntryAux (globtab, e, FALSE);
1280 uentry ue = usymtab_getTypeEntry (uid);
1282 uentry_setDatatype (ue, uid);
1284 if (sRef_modInFunction ())
1286 recordFunctionType (globtab->entries[uid]);
1289 return (uentry_getAbstractType (ue));
1293 usymtab_supEntrySref (uentry e)
1294 /*@globals utab, globtab, filetab@*/
1295 /*@modifies utab, globtab, e@*/
1297 sRef old = uentry_getSref (e);
1300 if (sRef_isType (old))
1302 uentry ue = usymtab_supEntryReturnAux (utab, e, TRUE);
1305 if (uentry_isValid (ue))
1307 sRef uref = uentry_getSref (ue);
1309 sRef_mergeStateQuiet (uref, old);
1310 sRef_clearDerived (uref);
1312 /*@noaccess uentry@*/
1314 else if (sRef_isKnown (old))
1316 usymtab_supEntry (e);
1320 (void) usymtab_supEntryAux (utab, e, TRUE);
1324 void usymtab_supGlobalEntry (/*@only@*/ uentry e)
1325 /*@globals globtab, filetab@*/
1326 /*@modifies globtab, filetab, e@*/
1330 DPRINTF (("Sup global entry: %s", uentry_unparse (e)));
1332 uid = usymtab_supEntryAux (globtab, e, FALSE);
1334 if (sRef_modInFunction ())
1336 recordFunctionType (globtab->entries[uid]);
1341 usymtab_supReturnFileEntry (/*@only@*/ uentry e)
1342 /*@globals filetab, globtab@*/
1343 /*@modifies filetab, globtab, e@*/
1345 llassert (filetab != usymtab_undefined);
1346 DPRINTF (("File entry: %s", uentry_unparse (e)));
1347 return (usymtab_supEntryReturnAux (filetab, e, FALSE));
1355 usymtab_inDeepScope () /*@globals utab@*/
1357 return (utab->lexlevel > paramsScope);
1361 usymtab_getIndex (/*@notnull@*/ usymtab s, cstring k)
1365 DPRINTF (("Lookup %s", k));
1367 if (cstringTable_isDefined (s->htable))
1369 i = cstringTable_lookup (s->htable, k);
1374 for (i = 0; i < s->nentries; i++)
1376 uentry current = s->entries[i];
1378 DPRINTF (("Check %d: %s", i, uentry_rawName (current)));
1380 if (!uentry_isUndefined (current)
1381 && cstring_equal (uentry_rawName (current), k))
1392 usymtab_fetchIndex (/*@notnull@*/ usymtab s, int i)
1394 llassert (i >= 0 && i < s->nentries);
1395 return (s->entries[i]);
1399 usymtab_getTypeId (cstring k) /*@globals globtab@*/
1401 usymId uid = usymtab_getIndex (globtab, k);
1403 if (uid == NOT_FOUND) return USYMIDINVALID;
1405 if (!(uentry_isDatatype (usymtab_getTypeEntry (uid)))) {
1406 return USYMIDINVALID;
1412 /*@dependent@*/ uentry
1413 usymtab_lookupStructTag (cstring k)
1415 cstring sname = makeStruct (k);
1416 uentry ue = usymtab_lookupGlob (sname);
1418 cstring_free (sname);
1422 /*@dependent@*/ uentry
1423 usymtab_lookupUnionTag (cstring k)
1425 cstring uname = makeUnion (k);
1426 uentry res = usymtab_lookupGlob (uname);
1428 cstring_free (uname);
1432 /*@dependent@*/ uentry
1433 usymtab_lookupEnumTag (cstring k)
1435 cstring ename = makeEnum (k);
1436 uentry res = usymtab_lookupGlob (ename);
1438 cstring_free (ename);
1443 usymtab_getId (cstring k) /*@globals globtab@*/
1445 usymId uid = usymtab_getIndex (globtab, k);
1448 if (uid == NOT_FOUND)
1450 return USYMIDINVALID;
1453 ue = usymtab_getGlobalEntry (uid);
1455 if (uentry_isPriv (ue))
1457 return USYMIDINVALID;
1463 static /*@exposed@*/ uentry
1464 usymtab_getEntryAux (/*@notnull@*/ usymtab s, usymId uid)
1466 llassert (uid != USYMIDINVALID);
1468 if (uid < 0 || uid >= s->nentries)
1470 llcontbug (message ("usymtab_getEntry: out of range: level = %d [%d]",
1472 return uentry_undefined;
1475 llassertprint (uentry_isValid (s->entries[uid]),
1476 ("entry undefined: %d", uid));
1478 return s->entries[uid];
1481 /*@dependent@*/ /*@observer@*/ uentry
1482 usymtab_getGlobalEntry (usymId uid)
1483 /*@globals utab, globtab@*/
1485 if (dbgfree) return (uentry_undefined);
1487 if (utab->lexlevel > paramsScope)
1489 /* need to do this the awkward way, since it could be in conditional scope */
1490 return (usymtab_lookupSafe (uentry_rawName (globtab->entries[uid])));
1494 return (globtab->entries[uid]);
1498 /*@dependent@*/ /*@exposed@*/ uentry
1499 usymtab_getTypeEntry (usymId uid)
1500 /*@globals globtab@*/
1504 if (uid >= 0 && uid < globtab->nentries)
1506 return (globtab->entries[uid]);
1510 return (uentry_undefined);
1515 llassert (uid >= 0 && uid < globtab->nentries);
1516 return (globtab->entries[uid]);
1524 /*@dependent@*/ /*@exposed@*/ uentry
1525 usymtab_getTypeEntrySafe (usymId uid)
1526 /*@globals globtab@*/
1528 if (uid < 0 || uid >= globtab->nentries)
1530 return uentry_undefined;
1533 return (globtab->entries[uid]);
1537 usymtab_isBoolType (usymId uid)
1538 /*@globals globtab@*/
1540 llassert (uid >= 0 && uid < globtab->nentries);
1542 return (cstring_equal (uentry_rawName (globtab->entries[uid]),
1543 context_getBoolName ()));
1547 usymtab_getTypeEntryName (usymId uid)
1548 /*@globals globtab@*/
1554 return (cstring_makeLiteral ("<freetype>"));
1557 ue = usymtab_getTypeEntry (uid);
1559 if (dbgload && !uentry_isValid (ue))
1561 return (message ("<missing type: %d>", uid));
1564 llassertprint (uentry_isValid (ue), ("type undefined: %d", uid));
1566 return (uentry_getName (ue));
1569 /*@unused@*/ static void
1570 usymtab_rehash (/*@notnull@*/ usymtab s)
1574 if (cstringTable_isDefined (s->htable))
1576 cstringTable_free (s->htable);
1579 s->htable = cstringTable_create (LLHASHSIZE);
1581 for (i = 0; i < s->nentries; i++)
1583 cstringTable_insert (s->htable, cstring_copy (uentry_rawName (s->entries[i])), i);
1588 ** superficial copy of usymtab
1590 ** DO copy spec entries
1593 static /*@only@*/ /*@notnull@*/ usymtab
1594 usymtab_shallowCopy (/*@notnull@*/ usymtab s) /*@*/
1596 usymtab copytab = usymtab_createRoot ();
1599 for (i = 0; i < s->nentries; i++)
1601 usymtab_addEntryBase (copytab, s->entries[i]);
1608 usymtab_shallowFree (/*@only@*/ /*@notnull@*/ usymtab s)
1610 aliasTable_free (s->aliases);
1611 refTable_free (s->reftable, s->nentries);
1613 /*@-compdestroy@*/ sfree (s); /*@=compdestroy@*/
1617 ** converts usymId from old table to sorted one
1621 usymtab_convertId (usymId uid)
1622 /*@globals oldtab, utab@*/
1628 llassert (usymtab_isDefined (oldtab));
1630 ue = usymtab_getEntryAux (oldtab, uid);
1632 llassertprint (uentry_isValid (ue), ("convertId: undefined: %d", uid));
1634 name = uentry_rawName (ue);
1636 ret = usymtab_getIndex (utab, name);
1637 llassert (ret == uid); /*! for now, no rehash! */
1638 DPRINTF (("Convert: %s [%d] -> %s [%d]",
1639 uentry_unparse (ue), uid,
1640 uentry_unparse (utab->entries[ret]), ret));
1642 llassertprint (ret != USYMIDINVALID, ("convertId: return is invalid"));
1648 usymtab_prepareDump (void)
1649 /*@globals oldtab, utab@*/
1650 /*@modifies oldtab, utab@*/
1652 llassert (usymtab_inGlobalScope ());
1653 llassert (oldtab == usymtab_undefined);
1656 DPRINTF (("Preparing to dump:"));
1657 usymtab_printAll ();
1660 oldtab = usymtab_shallowCopy (utab);
1663 ** alpha compare - make sure order is same on different platforms
1664 ** error messages appear in same order
1668 qsort (utab->entries, (size_t)utab->nentries,
1669 sizeof (*utab->entries),
1670 (int (*)(const void *, const void *)) uentry_xcomparealpha);
1672 usymtab_rehash (utab);
1676 DPRINTF (("After rehash:"));
1677 usymtab_printAll ();
1681 void usymtab_dump (FILE *fout)
1682 /*@globals utab, oldtab@*/
1685 bool neednl = FALSE;
1686 uentry lastentry = uentry_undefined;
1687 ekind lastekind = KINVALID;
1691 ** must call prepareDump first
1694 llassert (oldtab != usymtab_undefined);
1696 for (i = 0; i < utab->nentries; i++)
1698 uentry thisentry = utab->entries[i];
1699 ekind thisekind = uentry_getKind (thisentry);
1701 if (!uentry_hasRealName (thisentry))
1703 llassert (uentry_isGlobalMarker (thisentry));
1707 check (fputc ('\n', fout) == (int) '\n');
1710 fprintf (fout, "*%d (GlobalMarker)\n", KGLOBALMARKER);
1711 lastekind = KINVALID;
1717 if (thisekind != lastekind)
1721 check (fputc ('\n', fout) == (int) '\n');
1725 lastentry = uentry_undefined;
1726 fprintf (fout, "*%d (%s)\n", ekind_toInt (thisekind),
1727 cstring_toCharsSafe (ekind_capName (thisekind)));
1728 lastekind = thisekind;
1733 ** evans - 2001-02-18 - added the - 48 fudge factor...
1734 ** extra characters dumped, but I haven't counded them carefully...
1737 if (uentry_isInvalid (lastentry) || !uentry_equiv (lastentry, thisentry)
1738 || (linelen > (MAX_DUMP_LINE_LENGTH - (2 * MAX_NAME_LENGTH) - 48)))
1742 DPRINTF (("Dumping entry: %d", i));
1743 cdump = message ("^%d %q", i, uentry_dump (thisentry));
1744 /* was: cdump = uentry_dump (thisentry)); */
1746 lastentry = thisentry;
1749 check (fputc ('\n', fout) == (int) '\n');
1753 linelen += cstring_length (cdump);
1755 /* no new line here! */
1756 if (cstring_length (cdump) > 0)
1758 check (fputs (cstring_toCharsSafe (cdump), fout) != EOF);
1761 cstring_free (cdump);
1766 cstring cdump = uentry_rawName (thisentry);
1767 DPRINTF (("Raw name: %s", cdump));
1768 linelen += (cstring_length (cdump) + 1);
1769 fprintf (fout, "#%s", cstring_toCharsSafe (cdump));
1776 check (fputc ('\n', fout) == (int) '\n');
1779 lastekind = KINVALID;
1781 fprintf(fout, ";; Library constraints\n");
1782 for (i = 0; i < utab->nentries; i++)
1784 uentry thisentry = utab->entries[i];
1786 if (uentry_isFunction (thisentry) )
1788 constraintList preconditions;
1789 constraintList postconditions;
1791 preconditions = uentry_getFcnPreconditions (thisentry);
1792 postconditions = uentry_getFcnPostconditions (thisentry);
1794 if ( constraintList_isDefined(preconditions) ||
1795 constraintList_isDefined(postconditions) )
1797 fprintf(fout,"%s\n", cstring_toCharsSafe (uentry_rawName(thisentry) ) );
1798 if (constraintList_isDefined(preconditions) )
1800 fprintf(fout,"pre:\n");
1801 constraintList_dump(preconditions, fout);
1802 fprintf (fout, ";; end precondition constraints\n" );
1803 constraintList_free(preconditions);
1807 fprintf(fout,"pre:EMPTY\n");
1809 if (constraintList_isDefined(postconditions) )
1811 fprintf(fout,"post:\n");
1812 constraintList_dump(postconditions, fout);
1813 fprintf (fout, ";; end precondition constraints\n" );
1814 constraintList_free(postconditions);
1818 fprintf(fout,"post:EMPTY\n");
1829 void usymtab_load (FILE *f)
1830 /*@globals utab, globtab@*/
1831 /*@modifies utab, *f@*/
1833 char *s = mstring_create (MAX_DUMP_LINE_LENGTH);
1835 ekind kind = KINVALID;
1836 fileloc loc = g_currentloc;
1842 llassert (utab == globtab);
1843 llassert (utab->nentries == 0);
1845 while (((s = reader_readLine (f, s, MAX_DUMP_LINE_LENGTH)) != NULL)
1848 /* ignore ; comments */ ;
1851 while (s != NULL && *s != ';')
1859 ek = reader_getInt (&s);
1861 if (ek == KGLOBALMARKER)
1863 uentry lue = uentry_makeGlobalMarker ();
1864 DPRINTF (("Adding global marker: %s", uentry_unparseFull (lue)));
1865 usymtab_addEntryAlways (utab, lue);
1871 kind = ekind_fromInt (ek);
1879 (cstring_makeLiteral
1880 ("Library is in obsolete format. Use lclint +whichlib "
1881 "to see which library is being loaded."));
1884 if (reader_optCheckChar (&s, '^'))
1886 index = reader_getInt (&s);
1893 llassert (kind != KINVALID);
1894 ue = uentry_undump (kind, loc, &s);
1896 llassert (utab->nentries == index || index == -1);
1898 if (uentry_isValid (ue))
1900 int lastindex = utab->nentries;
1901 ue = usymtab_addEntryAlways (utab, ue);
1902 if (utab->nentries != lastindex + 1)
1904 DPRINTF (("No add: %s", uentry_unparseFull (ue)));
1912 ** now, any other names are the same uentry
1915 while (*(s++) == '#')
1917 cstring name = cstring_fromCharsO (reader_getWord (&s));
1918 uentry nue = uentry_nameCopy (name, ue);
1920 DPRINTF (("Name copy: %s", uentry_unparseFull (nue)));
1924 usymtab_addEntryAlways (utab, nue);
1927 while ((c = *s) != '\0' && (c !='\n'))
1929 if (c != ' ' || c != '\t')
1931 llbuglit ("Junk in load file");
1939 s = reader_readLine (f, os, MAX_DUMP_LINE_LENGTH);
1944 to handle reading of buffer overflow related constraints
1946 while (fgets (s, MAX_DUMP_LINE_LENGTH, f) != NULL
1949 ; /* ignore ;-comments */
1952 while (s != NULL && *s != ';')
1954 constraintList preconditions;
1955 constraintList postconditions;
1957 cstring name = cstring_fromChars(reader_getWord(&s) );
1959 ue = usymtab_lookup ( name );
1963 preconditions = constraintList_undefined;
1964 postconditions = constraintList_undefined;
1966 if (!uentry_isValid(ue) )
1968 llfatalbug ((message("Invalid uentry for %s library file may be corrupted", cstring_fromChars(s) ) ));
1970 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
1972 temp = cstring_fromChars (reader_getWord(&s) );
1974 if (cstring_compareLit (temp,"pre:") == 0 )
1976 preconditions = constraintList_undump (f);
1980 if (cstring_compareLit (temp,"pre:EMPTY") != 0 )
1981 llfatalbug ((message("Error reading library file pre:EMPTY expected but got %s", temp ) ));
1986 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
1988 temp = cstring_fromChars(reader_getWord(&s) );
1989 if (cstring_compareLit (temp, "post:") == 0 )
1991 postconditions = constraintList_undump (f);
1995 if (cstring_compareLit (temp, "post:EMPTY") != 0 )
1996 llfatalbug ((message("Error reading library file post:EMPTY expected but got %s", temp ) ));
1999 cstring_free (temp);
2001 uentry_setPreconditions (ue, functionConstraint_createBufferConstraint (preconditions));
2002 uentry_setPostconditions (ue, functionConstraint_createBufferConstraint (postconditions));
2004 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
2012 ** file scope for static variables
2016 usymtab_enterFile ()
2017 /*@globals utab, globtab, filetab@*/
2018 /*@modifies filetab@*/
2020 llassert (utab == globtab);
2025 usymtab_entries (globtab, ue)
2027 if (sRef_hasDerived (uentry_getSref (ue)))
2029 fprintf (g_msgstream, "Derived Global: %s\n", uentry_unparse (ue));
2030 fprintf (g_msgstream, "sRef: %s\n", sRef_unparseFull (ue->sref));
2032 } end_usymtab_entries ;
2036 usymtab_enterScope ();
2042 /*@globals utab, filetab@*/
2043 /*@modifies filetab, utab@*/
2046 llassert (utab->lexlevel == 1);
2048 usymtab_exitScope (exprNode_undefined);
2053 usymtab_enterScope ()
2054 /*@globals utab, globtab, filetab@*/
2057 usymtab t = usymtab_create (US_NORMAL, utab, TRUE);
2059 /* unconditional scope: optimize to avoid copy */
2060 t->aliases = aliasTable_copy (utab->aliases);
2063 llassert (usymtab_isDefined (t->env));
2065 if (t->env->lexlevel == paramsScope && context_inFunctionLike ())
2067 noshadowerror = TRUE;
2068 usymtab_handleParams ();
2069 noshadowerror = FALSE;
2074 ** setup external references:
2075 ** o only, unique params alias external args
2076 ** o other params may alias anything of their type
2080 usymtab_handleParams (void)
2081 /*@globals utab, globtab, filetab@*/
2082 /*@modifies utab, globtab@*/
2084 usymtab ptab = utab->env;
2085 uentry fcn = context_getHeader ();
2087 usymtab_entries (ptab, param)
2091 if (!uentry_isYield (param))
2094 sRef pref = uentry_getSref (param);
2096 /* Could be a global. */
2098 if (uentry_isAnyParam (param))
2100 ue = uentry_makeVariable (fixParamName (uentry_rawName (param)),
2101 uentry_getType (param),
2102 fileloc_copy (uentry_whereDeclared (param)),
2105 uentry_copyState (ue, param);
2106 uentry_setRefParam (ue);
2108 ue = usymtab_supEntrySrefReturn (ue);
2110 /* must be after supercede! */
2112 if (!sRef_stateKnown (pref))
2114 uentry_setDefState (ue, SS_DEFINED);
2115 uentry_setDefState (param, SS_DEFINED);
2119 if (sRef_isStateSpecial (pref))
2121 uentry_setDefState (ue, SS_ALLOCATED);
2125 uentry_setDefState (ue, sRef_getDefState (pref));
2129 uref = uentry_getSref (ue);
2131 if (sRef_isStack (uref))
2133 alkind pkind = sRef_getAliasKind (pref);
2135 if (alkind_isKnown (pkind) && !alkind_isLocal (pkind)
2136 && !alkind_isStack (pkind))
2138 sRef_setAliasKind (uref, pkind, fileloc_undefined);
2139 sRef_setOrigAliasKind (uref, pkind);
2143 sRef_setAliasKind (uref, AK_IMPTEMP, fileloc_undefined);
2144 sRef_setOrigAliasKind (uref, AK_IMPTEMP);
2146 if (uentry_isOut (param))
2152 sRef_setDefined (uref, fileloc_undefined);
2158 usymtab_addMustAlias (uref, pref);
2160 if (!(uentry_isOnly (param) || uentry_isUnique (param)))
2163 ** This is needed for detecting possibly aliased parameters.
2166 sRef s = sRef_makeExternal (uref);
2167 usymtab_addMustAlias (uref, s);
2170 if (sRef_isKillRef (pref))
2172 sRef_setAliasKind (uref, AK_NEWREF, fileloc_undefined);
2173 sRef_setOrigAliasKind (uref, AK_KILLREF);
2175 else if (sRef_isRefCounted (uref))
2177 sRef_setOrigAliasKind (uref, AK_REFCOUNTED);
2182 sRef_setOrigAliasKind (uref, AK_LOCAL);
2189 } end_usymtab_entries;
2192 if (uentry_hasStateClauseList (fcn))
2194 stateClauseList clauses = uentry_getStateClauseList (fcn);
2196 stateClauseList_preElements (clauses, cl)
2198 fileloc loc = stateClause_loc (cl);
2199 sRefSet osrs = sRefSet_undefined;
2202 if (stateClause_isGlobal (cl))
2204 DPRINTF (("Global Marker: %s",
2205 sRef_unparseFull (usymtab_lookupGlobalMarker ())));
2206 llassert (sRef_isGlobalMarker (usymtab_lookupGlobalMarker ()));
2207 srs = sRefSet_single (usymtab_lookupGlobalMarker ());
2212 srs = stateClause_getRefs (cl);
2215 sRefSet_elements (srs, el)
2217 sRef base = sRef_getRootBase (el);
2218 sRef sb = sRef_updateSref (el);
2220 if (sRef_isResult (base))
2222 ; /* nothing to do before */
2224 else if (sRef_isParam (base) || sRef_isGlobalMarker (base))
2226 if (stateClause_setsMetaState (cl))
2228 /* copied from exprNode.c:3040 */
2229 qual ql = stateClause_getMetaQual (cl);
2230 annotationInfo ainfo = qual_getAnnotationInfo (ql);
2231 metaStateInfo minfo = annotationInfo_getState (ainfo);
2232 cstring key = metaStateInfo_getName (minfo);
2233 int mvalue = annotationInfo_getValue (ainfo);
2235 DPRINTF (("Sets meta state! %s", stateClause_unparse (cl)));
2237 if (sRef_isResult (base))
2243 sRef_setMetaStateValueComplete (sb, key, mvalue, loc);
2248 sRefMod modf = stateClause_getEntryFunction (cl);
2252 sRefSet aliases = usymtab_allAliases (sb);
2256 sRefSet_elements (aliases, sr)
2259 } end_sRefSet_elements ;
2261 sRefSet_free (aliases);
2267 if (sRef_isValid (base))
2269 DPRINTF (("Base: %s", sRef_unparseFull (base)));
2273 } end_sRefSet_elements ;
2274 } end_stateClauseList_preElements ;
2279 usymtab_enterFunctionScope (uentry fcn)
2280 /*@globals utab, filetab, globtab@*/
2283 usymtab t = usymtab_create (US_NORMAL, utab, TRUE);
2285 if (utab->lexlevel != fileScope)
2287 if (utab->lexlevel > fileScope)
2289 llparseerror (cstring_makeLiteral ("New function scope inside function"));
2291 while (utab->lexlevel > fileScope)
2293 /*@i@*/ utab = usymtab_dropEnv (utab);
2300 llfatalbug (cstring_makeLiteral ("New function not inside file."));
2302 /*@-branchstate@*/ } /*@=branchstate@*/
2306 DPRINTF (("Globs: %s", globSet_unparse (uentry_getGlobs (fcn))));
2308 globSet_allElements (uentry_getGlobs (fcn), el)
2310 DPRINTF (("Here we go: %s", sRef_unparseFull (el)));
2312 if (sRef_isUndefGlob (el))
2314 int index = sRef_getScopeIndex (el);
2315 sRef sr = sRef_updateSref (el);
2316 fileloc loc = uentry_whereEarliest (fcn);
2318 DPRINTF (("update: %s", sRef_unparseFull (sr)));
2319 DPRINTF (("Undef!"));
2320 if (sRef_isFileStatic (el))
2322 ctype ct = sRef_getType (el);
2325 llassert (usymtab_isDefined (filetab));
2327 ue = usymtab_fetchIndex (filetab, index);
2329 if (ctype_isRealArray (ct) || ctype_isRealSU (ct))
2331 sRef_setAllocated (sr, loc);
2335 sRef_setUndefined (sr, loc);
2340 uentry ue = globtab->entries[index];
2341 ctype ct = uentry_getType (ue);
2343 if (ctype_isArray (ct) || ctype_isSU (ct))
2345 sRef_setAllocated (sr, loc);
2349 sRef_setUndefined (sr, loc);
2353 else if (sRef_isAllocated (el))
2355 sRef sr = sRef_updateSref (el);
2356 fileloc loc = uentry_whereEarliest (fcn);
2358 sRef_setAllocated (sr, loc);
2360 else if (sRef_isPartial (el))
2362 sRef sr = sRef_updateSref (el);
2363 fileloc loc = uentry_whereEarliest (fcn);
2365 sRef_setPartial (sr, loc);
2370 sRef sr = sRef_updateSref (el);
2371 fileloc loc = uentry_whereEarliest (fcn);
2373 sRef_setDefined (sr, loc);
2377 /* shouldn't need to do anything! */
2379 } end_globSet_allElements;
2381 DPRINTF (("Globs after: %s", globSet_unparse (uentry_getGlobs (fcn))));
2385 usymtab_caseBranch (void)
2388 usymtab t = usymtab_create (US_CBRANCH, utab, FALSE);
2393 usymtab_switchBranch (/*@unused@*/ exprNode s)
2396 usymtab t = usymtab_create (US_SWITCH, utab, FALSE);
2398 t->aliases = aliasTable_copy (utab->aliases);
2403 usymtab_trueBranch (/*@only@*/ guardSet guards)
2406 usymtab t = usymtab_create (US_TBRANCH, utab, FALSE);
2409 ** not true! (could be in a macro)
2411 ** llassertprint (utab->lexlevel > paramsScope,
2412 ** ("not in scope: %s", usymtab_unparseLocal ()));
2416 guardSet_free (t->guards);
2419 aliasTable_free (t->aliases);
2420 t->aliases = aliasTable_copy (utab->aliases);
2428 ** { int a; if (...) a = 3; < a may be undefined here!
2433 usymtab_popTrueBranch (exprNode pred, exprNode expr, clause cl) /*@modifies utab@*/
2436 ** add a false branch
2437 ** (could be done more efficiently as a special case, but
2438 ** it is better to only maintain one version of the code)
2441 if (utab->kind != US_TBRANCH
2442 && context_inIterDef ())
2444 usymtab_exitScope (expr);
2448 DPRINTF (("pop true branch.."));
2449 usymtab_altBranch (guardSet_invert (exprNode_getGuards (pred)));
2450 usymtab_popBranches (pred, expr, exprNode_undefined, TRUE, cl);
2455 usymtab_popCaseBranch () /*@modifies utab@*/
2457 llassert (utab->kind == US_CBRANCH);
2458 usymtab_quietPlainExitScope ();
2462 usymtab_popTrueExecBranch (exprNode pred, exprNode expr, clause cl)
2465 ** add a false branch that must return --- that is,
2466 ** the true branch is always executed!
2469 usymtab_altBranch (guardSet_invert (exprNode_getGuards (pred)));
2470 usymtab_popBranches (pred, expr, exprNode_makeMustExit (), FALSE, cl);
2474 usymtab_popOrBranch (exprNode pred, exprNode expr)
2478 usymtab env = utab->env;
2479 usymtab otab = utab;
2482 llassert (env != NULL);
2484 if (exprNode_isError (expr))
2490 mustReturn = exprNode_mustEscape (expr);
2494 llassert (utab->kind == US_TBRANCH);
2497 ** merge each entry in table with its original
2498 ** unless execution cannot continue after this branch
2501 for (i = 0; i < utab->nentries; i++)
2503 uentry current = utab->entries[i];
2504 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
2506 uentry_mergeState (old, current, exprNode_loc (expr),
2507 mustReturn, FALSE, TRUE, ORCLAUSE);
2513 env->guards = guardSet_levelUnionFree (env->guards,
2514 guardSet_invert (exprNode_getGuards (pred)),
2519 env->aliases = aliasTable_levelUnion (env->aliases, otab->aliases, env->lexlevel);
2522 /* env is now utab */
2523 usymtab_quietPlainExitScope ();
2527 ** case syntax in C is very unrestricted. This is unfortunate.
2529 ** A switch case is ended either by a new case or default, or
2530 ** a close } that may close the switch or some other control
2535 usymtab_newCase (/*@unused@*/ exprNode pred, exprNode last)
2538 bool mustBreak = usymtab_mustBreak (utab);
2539 bool mustReturn = usymtab_mustEscape (utab);
2540 usymtab stab = utab;
2543 ** Find last case (or outer switch)
2546 while (stab->kind != US_CBRANCH && stab->kind != US_SWITCH)
2549 llassert (stab != GLOBAL_ENV);
2554 while (stab->kind == US_CBRANCH)
2557 llassert (stab != GLOBAL_ENV);
2561 ** if its a fall through case, merge in outside entries and last case.
2566 ** case 1: x = 3; <fall through>
2567 ** case 2: << x may not be defined
2571 if (!mustBreak && !mustReturn && utab->kind == US_CBRANCH)
2573 llassert (stab->kind == US_SWITCH || stab->kind == US_NORMAL);
2575 usymtab_entries (utab, ue) /* but, keep track of used variables */
2577 uentry old = usymtab_lookupAux (stab, uentry_rawName (ue));
2579 llassert (uentry_isValid (old));
2581 /* modifies ue, not old */
2583 uentry_mergeState (ue, old, exprNode_loc (last),
2584 FALSE, FALSE, TRUE, CASECLAUSE);
2585 } end_usymtab_entries;
2587 utab->aliases = aliasTable_levelUnion (utab->aliases,
2588 stab->aliases, utab->lexlevel);
2591 ** No need for a new branch.
2598 usymtab_caseBranch ();
2599 /*@-mustfree@*/ /*< utab->aliases >*/
2600 utab->aliases = aliasTable_copy (stab->aliases);
2608 ** for && (both pred and expr are executed)
2612 usymtab_popAndBranch (exprNode pred, /*@unused@*/ exprNode expr)
2615 usymtab env = utab->env;
2619 llassert (utab->kind == US_TBRANCH);
2622 ** merge each entry in table with its original
2623 ** unless execution cannot continue after this branch
2626 for (i = 0; i < utab->nentries; i++)
2628 uentry current = utab->entries[i];
2629 sRef tref = uentry_getSref (current);
2630 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
2631 sRef oref = uentry_getSref (old);
2633 /* note that is current is in a nested branch,
2634 it may create a "new" old entry. */
2636 llassert (uentry_isValid (old));
2637 uentry_mergeState (old, current, exprNode_loc (expr),
2638 FALSE, FALSE, TRUE, ANDCLAUSE);
2641 ** if is it defined by the second clause, then it should be defined.
2644 if (sRef_isAnyDefined (tref)
2645 && (sRef_isAllocated (oref) || sRef_isStateUndefined (oref)))
2647 sRef_setDefined (oref, g_currentloc);
2651 utab->guards = guardSet_levelUnionFree (utab->guards,
2652 guardSet_invert (exprNode_getGuards (pred)),
2654 utab->aliases = aliasTable_levelUnion (utab->aliases, otab->aliases, utab->lexlevel);
2656 usymtab_quietPlainExitScope ();
2661 ** Stack should be [ US_CBRANCH+ US_SWITCH ]
2662 ** Only branches which do not return (except possibly the last branch) are included.
2664 ** Conditionally merge state from all CBRANCHes.
2666 ** If allpaths is TRUE, then all possible executions go through some switch
2667 ** case, and the original scope is not merged.
2671 usymtab_exitSwitch (/*@unused@*/ exprNode sw, bool allpaths)
2674 usymtab ttab = utab;
2675 usymtab stab = ttab;
2676 usymtab ltab = ttab;
2677 bool lastMustReturn = usymtab_mustEscape (utab);
2681 while (stab->kind == US_CBRANCH)
2684 llassert (stab != GLOBAL_ENV);
2687 while (stab->kind == US_NORMAL)
2690 llassert (stab != GLOBAL_ENV);
2693 llassert (stab->kind == US_SWITCH);
2695 /* go to the scope outside the switch (US_SWITCH is just a marker! */
2697 llassert (stab != GLOBAL_ENV);
2701 llassert (usymtab_isDefined (ttab));
2703 if (ttab->kind == US_CBRANCH)
2705 /* was quietPlainExitScope --- but, can't free it yet! */
2707 llassert (utab != GLOBAL_ENV);
2709 while (ttab->kind == US_CBRANCH)
2712 ** (from popTrueBranch)
2715 bool mustReturn = usymtab_mustEscape (ttab);
2716 bool mustBreak = usymtab_mustBreak (ttab);
2718 usymtab_entries (ttab, current)
2720 uentry old = /*@-compmempass@*/ usymtab_lookupAux (ltab, uentry_rawName (current));
2724 ** note that is this is in a nested branch,
2725 ** it may create a "new" old entry.
2728 if (uentry_isValid (old))
2732 uentry_mergeUses (current, old);
2733 uentry_setState (old, current);
2737 uentry_mergeState (old, current, exprNode_loc (sw),
2738 mustReturn, FALSE, TRUE, SWITCHCLAUSE);
2745 } end_usymtab_entries;
2748 ** if entry is not in symbol table for this case, merge with pre-switch
2752 if (!mustReturn && !mustBreak)
2754 usymtab_entries (stab, current)
2756 if (usymtab_getIndex (ttab, uentry_rawName (current)) == NOT_FOUND)
2758 uentry old = /*@-compmempass@*/
2759 usymtab_lookupAux (ltab, uentry_rawName (current));
2762 llassert (uentry_isValid (old));
2763 uentry_mergeState (old, current, exprNode_loc (sw),
2764 FALSE, FALSE, TRUE, SWITCHCLAUSE);
2766 } end_usymtab_entries;
2769 ltab->env = ttab->env;
2773 ** Suprious error, becuase of environments.
2776 /*@i1@*/ utab = ltab;
2778 lastMustReturn = FALSE;
2785 ** now, there is one US_CBRANCH. Merge this with the stab.
2789 for (i = 0; i < ltab->nentries; i++)
2791 uentry current = ltab->entries[i];
2792 uentry old = usymtab_lookupAux (stab, uentry_rawName (current));
2794 /* note that is this is in a nested branch,
2795 it may create a "new" old entry. */
2798 if (uentry_isValid (old))
2802 uentry_mergeUses (current, old);
2803 uentry_setState (old, current);
2807 uentry_mergeState (old, current, exprNode_loc (sw),
2808 FALSE, FALSE, TRUE, SWITCHCLAUSE);
2822 ** switch may or may not be followed by a new scope
2825 if (utab->kind == US_SWITCH)
2827 usymtab_quietPlainExitScope ();
2831 usymtab_quietPlainExitScope ();
2832 llassert (utab->kind == US_SWITCH);
2833 usymtab_quietPlainExitScope ();
2839 updateNullState (sRef el, /*@notnull@*/ usymtab ttab,
2840 /*@notnull@*/ usymtab ftab, bool trueGuard)
2842 sRef base = sRef_getRootBase (el);
2843 int level = sRef_lexLevel (base);
2845 if (sRef_isCvar (base))
2847 usymId index = sRef_getScopeIndex (base);
2848 uentry ue = usymtab_getRefTab (ttab, level, index);
2850 if (!uentry_isLset (ue))
2852 sRef sr = uentry_getSref (ue);
2856 sRef_setDerivNullState (sr, el, NS_NOTNULL);
2860 if (!guardSet_isGuarded (ttab->guards, el)
2861 && !sRef_isNotNull (sr))
2863 sRef_setDerivNullState (sr, el, NS_DEFNULL);
2871 ue = usymtab_getRefTab (ftab, level, index);
2873 if (!uentry_isLset (ue))
2875 sRef sr = uentry_getSref (ue);
2878 if (!trueGuard) /* yikes! forgot the ! */
2880 sRef_setDerivNullState (sr, el, NS_NOTNULL);
2885 if (!guardSet_isGuarded (ftab->guards, el)
2886 && !sRef_isNotNull (sr))
2888 sRef_setDerivNullState (sr, el, NS_DEFNULL);
2900 usymtab_popBranches (exprNode pred, exprNode tbranch, exprNode fbranch,
2901 bool isOpt, clause cl)
2905 usymtab ftab = utab;
2906 usymtab ttab = utab->env;
2910 guardSet guards = exprNode_getGuards (pred);
2911 sRefSet tguards = guardSet_getTrueGuards (guards);
2912 sRefSet fguards = guardSet_getFalseGuards (guards);
2913 bool mustReturnT = exprNode_mustEscape (tbranch);
2914 bool mustReturnF = exprNode_mustEscape (fbranch);
2916 DPRINTF (("Pop branches: %s [mustreturn: %s/%s]", exprNode_unparse (pred),
2917 bool_unparse (mustReturnT),
2918 bool_unparse (mustReturnF)));
2920 if (exprNode_isDefined (fbranch))
2922 loc = exprNode_loc (fbranch);
2926 loc = exprNode_loc (tbranch);
2929 llassert (usymtab_isDefined (ttab));
2933 llassert (usymtab_isDefined (env));
2934 llassert (ftab->kind == US_FBRANCH);
2935 llassert (ttab->kind == US_TBRANCH);
2938 ** For each element that is true guarded (i.e., if (x != NULL))
2939 ** make x = null in false branch,
2940 ** and x = notnull in true branch.
2941 ** unless x was set locally in that branch.
2942 ** For each element that is false guarded (x == NULL)
2943 ** make x = null in true, notnull in false.
2945 ** For each element that is either guarded (pred(x))
2949 sRefSet_allElements (tguards, el)
2951 updateNullState (el, ttab, ftab, TRUE);
2952 } end_sRefSet_allElements;
2954 sRefSet_allElements (fguards, el)
2956 updateNullState (el, ttab, ftab, FALSE);
2957 } end_sRefSet_allElements;
2961 ** if an entry is in both true and false, merge the entries,
2962 ** then replace original with new state.
2964 ** if an entry is in one table, merge it with the original.
2967 DPRINTF (("ftab: %d", ftab->nentries));
2969 for (i = 0; i < ftab->nentries; i++)
2971 uentry fthis = ftab->entries[i];
2972 uentry old = usymtab_lookupAux (env, uentry_rawName (fthis));
2973 int tindex = usymtab_getIndex (ttab, uentry_rawName (fthis));
2975 DPRINTF (("Entry: %s / %s", uentry_unparseFull (fthis), uentry_unparseFull (old)));
2977 if (uentry_isUndefined (old))
2979 /* possible entry was added as an undefined id */
2980 DPRINTF (("Undefined! %s", uentry_rawName (fthis)));
2984 if (tindex != NOT_FOUND)
2986 uentry tthis = ttab->entries[tindex];
2988 /* note that is this is in a nested branch,
2989 it may create a "new" old entry. */
2995 uentry_mergeState (fthis, tthis, loc,
2996 mustReturnT, FALSE, FALSE, cl);
3000 uentry_mergeUses (fthis, tthis);
3003 uentry_setState (old, fthis);
3010 uentry_setState (old, tthis);
3011 uentry_mergeState (old, fthis, loc, mustReturnF,
3015 ttab->entries[tindex] = uentry_undefined;
3016 uentry_free (tthis);
3020 uentry_mergeState (old, fthis, loc, mustReturnF, TRUE, FALSE, cl);
3024 for (i = 0; i < ttab->nentries; i++)
3026 uentry current = ttab->entries[i];
3028 DPRINTF (("ttab: %s", uentry_unparseFull (current)));
3030 if (!uentry_isUndefined (current))
3032 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
3034 DPRINTF (("Old: %s", uentry_unparseFull (old)));
3036 if (uentry_isUndefined (old))
3038 llcontbug (message ("Undefined entry: %s", uentry_rawName (current)));
3044 uentry_mergeUses (current, old);
3045 uentry_setState (old, current);
3050 ** Assumes false branch is a fall-through if
3051 ** fbranch is not defined. This is true, unless
3052 ** where was some greivous error in processing
3053 ** the else branch of an if-then, in which case
3054 ** this is probably the right thing to do anyway.
3057 uentry_mergeState (old, current, loc, mustReturnT,
3061 DPRINTF (("==> %s", uentry_unparseFull (old)));
3066 ** Plain levelUnion doesn't work, since we need to use the sRef's in env->aliases
3067 ** if they are present.
3070 llassert (NOALIAS (env->aliases, ttab->aliases));
3071 llassert (NOALIAS (env->aliases, ftab->aliases));
3073 aliasTable_free (env->aliases);
3075 env->aliases = aliasTable_levelUnionNew (ttab->aliases,
3076 ftab->aliases, env->lexlevel);
3078 aliasTable_fixSrefs (env->aliases);
3080 DPRINTF (("Aliases: %s", aliasTable_unparse (env->aliases)));
3082 /* exit true and false scopes */
3083 usymtab_quietPlainExitScope ();
3084 usymtab_quietPlainExitScope ();
3088 utab->guards = guardSet_levelUnionFree
3090 guardSet_invert (exprNode_getGuards (pred)),
3096 utab->guards = guardSet_levelUnion (utab->guards,
3097 exprNode_getGuards (pred),
3101 DPRINTF (("Here."));
3104 static void usymtab_fixCases (void) /*@modifies utab@*/ {
3105 while (utab->kind == US_CBRANCH)
3107 usymtab_quietPlainExitScope ();
3110 llassert (utab->kind != US_CBRANCH);
3114 usymtab_altBranch (/*@only@*/ guardSet guards)
3118 usymtab parent = utab->env;
3120 t = usymtab_create (US_FBRANCH, utab, FALSE);
3123 ** If we are in a case, need to close it. The C syntax
3124 ** is very liberal, so this kludge is necessary.
3127 usymtab_fixCases ();
3129 DPRINTF (("Current kind: %s", usymtab_unparseStack ()));
3131 llassert (utab->kind == US_TBRANCH);
3132 llassert (parent != GLOBAL_ENV);
3134 guardSet_free (t->guards);
3137 aliasTable_free (t->aliases);
3138 t->aliases = aliasTable_copy (parent->aliases);
3144 usymtab_allDefined (void)
3145 /*@globals utab, globtab@*/
3149 llassert (utab == globtab);
3151 for (i = 0; i < utab->nentries; i++)
3153 uentry e = utab->entries[i];
3155 if (uentry_isPriv (e))
3157 ; /* no need to define it */
3161 if (context_getFlag (FLG_SPECUNDECL))
3163 fileloc sloc = uentry_whereSpecified (e);
3164 fileloc dloc = uentry_whereDeclared (e);
3166 if (fileloc_isDefined (sloc)
3167 && !uentry_isFakeTag (e)
3168 && !fileloc_isDefined (dloc))
3172 message ("%s %q specified but not declared",
3173 ekind_capName (uentry_getKind (e)),
3174 uentry_getName (e)),
3179 if (!uentry_isCodeDefined (e))
3181 fileloc dloc = uentry_whereDeclared (e);
3183 if (fileloc_isLib (dloc) || fileloc_isXHFile (dloc))
3187 else if (fileloc_isDefined (dloc))
3189 if (!uentry_isAnyTag (e))
3191 if (fileloc_isUser (dloc))
3195 message ("%s %q declared but not defined",
3196 ekind_capName (uentry_getKind (e)),
3197 uentry_getName (e)),
3199 DPRINTF (("decl: %s", uentry_unparseFull (e)));
3205 fileloc sloc = uentry_whereSpecified (e);
3207 if (fileloc_isDefined (sloc)
3208 && !fileloc_isImport (sloc)
3209 && !fileloc_isLib (sloc)
3210 && !fileloc_isPreproc (sloc)
3211 && !uentry_isFakeTag (e))
3213 if (uentry_isVariable (e) || uentry_isFunction (e))
3217 message ("%s %q specified but not declared or defined",
3218 ekind_capName (uentry_getKind (e)),
3219 uentry_getName (e)),
3226 message ("%s %q specified but not defined",
3227 ekind_capName (uentry_getKind (e)),
3228 uentry_getName (e)),
3238 void usymtab_exportHeader (void)
3243 for (i = 0; i < utab->nentries; i++)
3245 uentry ce = utab->entries[i];
3247 if (!uentry_isDatatype (ce)
3248 && !uentry_isAnyTag (ce)
3249 && !uentry_isEitherConstant (ce) && !uentry_isStatic (ce)
3250 && !uentry_isExternal (ce)
3251 && !uentry_isForward (ce))
3253 fileloc fwhere = uentry_whereDeclared (ce);
3255 if (fileloc_isUndefined (fwhere)
3256 && uentry_isFunction (ce))
3258 fwhere = uentry_whereDefined (ce);
3261 if (fileloc_isDefined (fwhere)
3262 && !fileloc_isHeader (fwhere)
3263 && !fileloc_isXHFile (fwhere)
3264 && !(fileloc_isSpecialFile (fwhere)
3265 && !context_getFlag (FLG_UNUSEDSPECIAL)))
3267 if (uentry_isVariable (ce))
3270 (FLG_EXPORTHEADERVAR,
3271 message ("%s %q exported but not declared in header file",
3272 ekind_capName (uentry_getKind (ce)),
3273 uentry_getName (ce)),
3276 uentry_showDefSpecInfo (ce, fwhere);
3281 if (!uentry_isIter (ce)
3282 && !uentry_isEndIter (ce)
3283 && !uentry_isExpandedMacro (ce))
3285 if (uentry_isFunction (ce)
3286 && cstring_equalLit (uentry_rawName (ce), "main"))
3288 ; /* no error for main */
3294 message ("%s %q exported but not declared "
3296 ekind_capName (uentry_getKind (ce)),
3297 uentry_getName (ce)),
3300 uentry_showDefSpecInfo (ce, fwhere);
3310 void usymtab_exportLocal (void)
3315 for (i = 0; i < utab->nentries; i++)
3317 uentry ce = utab->entries[i];
3319 if (!uentry_isDatatype (ce) && !uentry_isAnyTag (ce)
3320 && !uentry_isEitherConstant (ce)
3321 && !uentry_isIter (ce)
3322 && !uentry_isEndIter (ce)
3323 && !uentry_isExpandedMacro (ce)
3324 && uentry_isUsed (ce))
3326 /* check static uses */
3327 filelocList fuses = uentry_getUses (ce);
3328 fileloc mod = uentry_whereDefined (ce);
3329 bool ok = filelocList_isEmpty (fuses);
3330 fileloc fwhere = uentry_whereDeclared (ce);
3332 if (fileloc_isSpecialFile (fwhere)
3333 && !context_getFlag (FLG_UNUSEDSPECIAL))
3335 ok = TRUE; /* no errors for special files */
3339 filelocList_elements (fuses, uloc)
3341 if (fileloc_isUndefined (uloc) || !fileloc_sameModule (uloc, mod))
3344 /*@innerbreak@*/ break;
3346 } end_filelocList_elements;
3353 message ("%s exported but not used outside %s: %q",
3354 ekind_capName (uentry_getKind (ce)),
3355 fileloc_getBase (mod),
3356 uentry_getName (ce)),
3359 uentry_showDefSpecInfo (ce, fwhere);
3367 usymtab_allUsed (void)
3371 bool isFileStatic = usymtab_inFileScope ();
3372 cstring last_file = cstring_undefined;
3374 for (i = 0; i < utab->nentries; i++)
3376 bool hasError = FALSE;
3377 uentry ce = utab->entries[i];
3378 fileloc fwhere = uentry_whereDeclared (ce);
3380 if (fileloc_isUndefined (fwhere))
3382 fwhere = uentry_whereDefined (ce);
3385 if (fileloc_isInvalid (fwhere)
3386 || fileloc_isLib (fwhere)
3387 || fileloc_isBuiltin (fwhere)
3388 || ((fileloc_isSpecialFile (fwhere)
3389 || fileloc_isSpecialFile (uentry_whereDefined (ce)))
3390 && !context_getFlag (FLG_UNUSEDSPECIAL)))
3394 else if (!uentry_wasUsed (ce) && !uentry_isAnyTag (ce))
3396 cstring fname = fileloc_filename (fwhere);
3398 if (cstring_isUndefined (last_file))
3402 else if (cstring_equal (fname, last_file))
3411 if (uentry_isParam (ce))
3413 if (context_inMacro ())
3415 sRef cref = uentry_getSref (ce);
3417 if (uentry_isYield (ce))
3419 ; /* no checks (for now) */
3421 else if (sRef_isSafe (cref))
3427 if (uentry_hasRealName (ce))
3430 optgenerror (FLG_MACROPARAMS,
3431 message ("Macro parameter %q not used",
3432 uentry_getName (ce)),
3439 if (cstring_equalFree (uentry_getName (ce),
3440 cstring_makeLiteral ("...")))
3446 hasError = optgenerror (FLG_PARAMUNUSED,
3447 message ("Parameter %q not used",
3448 uentry_getName (ce)),
3453 else if (uentry_isFunction (ce) || uentry_isIter (ce))
3455 if (fileloc_isUser (fwhere))
3457 hasError = optgenerror
3459 message ("%q %q declared but not used",
3461 (uentry_isIter (ce) ? "Iterator"
3462 : (isFileStatic ? "File static function" : "Function")),
3463 uentry_getName (ce)),
3467 else if (uentry_isEndIter (ce))
3469 ; /* no error (already reported for iter */
3471 else if (uentry_isEnumConstant (ce))
3473 if (fileloc_isUser (fwhere))
3475 hasError = optgenerror
3477 message ("Enum member %q not used",
3478 uentry_getName (ce)),
3482 else if (uentry_isConstant (ce))
3484 if (fileloc_isUser (fwhere))
3486 hasError = optgenerror
3488 message ("Constant %q declared but not used",
3489 uentry_getName (ce)),
3493 else if (uentry_isDatatype (ce))
3495 if (fileloc_isUser (fwhere))
3497 hasError = optgenerror
3499 message ("Type %q declared but not used",
3500 uentry_getName (ce)),
3504 else if (!uentry_isRefParam (ce) && !uentry_isExpandedMacro (ce))
3505 { /* errors for ref params will be reported in the next scope */
3506 llassertprint (uentry_isVar (ce),
3507 ("ce: %s", uentry_unparseFull (ce)));
3509 if (ctype_isFunction (uentry_getType (ce)))
3511 if (fileloc_isUser (fwhere))
3513 hasError = optgenerror
3515 message ("%q %q declared but not used",
3517 (isFileStatic ? "File static function"
3519 uentry_getName (ce)),
3525 if (fileloc_isUser (fwhere))
3529 hasError = optgenerror
3531 message ("%q %q declared but not used",
3533 (isFileStatic ? "File static variable"
3535 uentry_getName (ce)),
3545 else if ((uentry_isDatatype (ce) || uentry_isAnyTag (ce)))
3546 { /* check all fields */
3547 ctype ct = uentry_getRealType (ce);
3550 while (ctype_isAP (ct))
3552 ct = ctype_getBaseType (ct);
3555 if (ctype_isSU (ct))
3557 uentryList fields = ctype_getFields (ct);
3559 uentryList_elements (fields, field)
3561 if (!uentry_isUsed (field))
3563 if (uentry_hasName (ce))
3565 hasError |= optgenerror
3567 message ("Field %q of %s %q declared but not used",
3568 uentry_getName (field),
3569 cstring_makeLiteralTemp
3570 (ctype_isStruct (ct) ? "structure" : "union"),
3571 uentry_getName (ce)),
3572 uentry_whereEarliest (field));
3578 ** Can't report these errors for unnamed structs.
3579 ** No way to tell when there are multiple consistent
3580 ** unnamed structure types. (Could go through table
3581 ** and mark them all unused...)
3583 hasError |= optgenerror
3585 message ("Field %q of unnamed %s declared but not used",
3586 uentry_getName (field),
3587 cstring_makeLiteralTemp
3588 (ctype_isStruct (ct) ? "structure" : "union")),
3589 uentry_whereEarliest (field));
3594 uentry_setUsed (field, fileloc_undefined);
3596 } end_uentryList_elements;
3606 if (uentry_isParam (ce) && context_inMacro ())
3608 if (fileloc_isDefined (uentry_whereSpecified (ce)))
3610 uentry_showWhereSpecified (ce);
3615 uentry_showDefSpecInfo (ce, fwhere);
3618 uentry_setUsed (ce, fileloc_undefined);
3624 checkGlobalReturn (uentry glob, sRef orig)
3626 sRef sr = uentry_getSref (glob);
3628 DPRINTF (("Check global return: %s // orig: %s // sr: %s",
3629 uentry_unparseFull (glob),
3630 sRef_unparseFull (orig),
3631 sRef_unparseFull (sr)));
3633 DPRINTF (("Is killed: %s", bool_unparse (sRef_isKilledGlob (orig))));
3635 if (context_getFlag (FLG_GLOBSTATE))
3637 DPRINTF (("Is killed: %s", sRef_unparseFull (orig)));
3639 if (sRef_isKilledGlob (orig))
3641 if (sRef_isStateUndefined (sr)
3642 || sRef_isUnuseable (sr)
3643 || sRef_isStateUnknown (sr)
3644 || sRef_isDead (sr))
3650 ctype ct = ctype_realType (uentry_getType (glob));
3652 if (ctype_isVisiblySharable (ct))
3657 ("Killed global %q (type %s) not released before return",
3658 uentry_getName (glob),
3659 ctype_unparse (ct)),
3662 sRef_showStateInfo (sr);
3667 sRef_protectDerivs ();
3668 (void) checkGlobalDestroyed (sr, g_currentloc);
3669 sRef_clearProtectDerivs ();
3675 if (sRef_isStateUndefined (sr))
3677 if (optgenerror (FLG_GLOBSTATE,
3679 ("Function returns with global %q undefined",
3680 uentry_getName (glob)),
3683 sRef_showStateInfo (sr);
3688 if (sRef_isDead (sr) || sRef_isKept (sr))
3692 message ("Function returns with global %q "
3693 "referencing %s storage",
3694 uentry_getName (glob),
3695 cstring_makeLiteralTemp (sRef_isDead (sr) ? "released" : "kept")),
3698 if (sRef_isKept (sr))
3700 sRef_showAliasInfo (sr);
3704 sRef_showStateInfo (sr);
3707 sRef_setDefState (sr, SS_UNKNOWN, fileloc_undefined);
3711 if (ctype_isRealPointer (uentry_getType (glob))
3712 && sRef_possiblyNull (sr)
3713 && !uentry_possiblyNull (glob))
3717 message ("Function returns with non-null global %q "
3718 "referencing null storage",
3719 uentry_getName (glob)),
3722 sRef_showNullInfo (sr);
3727 checkGlobReturn (glob);
3735 ** remember: check alias globals
3738 void usymtab_checkFinalScope (bool isReturn)
3741 bool mustFree = context_getFlag (FLG_MUSTFREE);
3742 bool mustDefine = context_getFlag (FLG_MUSTDEFINE);
3743 /* bool mustNotAlias = context_getFlag (FLG_MUSTNOTALIAS); */
3744 sRefSet checked = sRefSet_new ();
3745 usymtab stab = utab;
3749 ** need to check all scopes out to function parameters.
3754 for (i = 0; i < stab->nentries; i++)
3756 uentry ce = stab->entries[i];
3757 sRef sr = uentry_getSref (ce);
3758 sRef rb = sRef_getRootBase (sr);
3762 ** Shouldn't check if shadow checked in deeper scope:
3767 uentry oue = usymtab_lookupQuietNoAlt (utab, uentry_observeRealName (ce));
3769 if (!uentry_sameObject (ce, oue))
3771 DPRINTF (("Skipping outer entry: %s / %s", uentry_unparseFull (ce),
3772 uentry_unparseFull (oue)));
3773 /*@i32 what if it is one an alternate branch? */
3774 /*@innercontinue@*/ continue;
3778 DPRINTF (("Here check final scope: %s", uentry_unparseFull (ce)));
3780 if (ctype_isFunction (uentry_getType (ce)))
3782 /*@innercontinue@*/ continue;
3785 if (uentry_isAnyParam (ce)
3786 || uentry_isRefParam (ce)
3787 || sRef_isFileOrGlobalScope (rb))
3789 /* Don't do the loseref check...but should check state! */
3791 else if (sRef_isDefinitelyNull (sr)
3792 || usymtab_isDefinitelyNull (sr))
3795 ** No state reference errors for definitely null references.
3800 DPRINTF (("Lose ref: %s / %s", uentry_unparse (ce),
3801 sRef_unparseFull (sr)));
3803 tvalues = sRef_getValueTable (sr);
3805 valueTable_elements (tvalues, fkey, fval) {
3806 metaStateInfo minfo;
3807 cstring msg = cstring_undefined;
3810 minfo = context_lookupMetaStateInfo (fkey);
3811 llassert (metaStateInfo_isDefined (minfo));
3813 if (stateValue_isError (fval))
3819 DPRINTF (("Check: %s / %s / %s", fkey,
3820 metaStateInfo_unparse (minfo),
3821 stateValue_unparse (fval)));
3823 minfo = context_lookupMetaStateInfo (fkey);
3825 nval = stateCombinationTable_lookupLoseReference
3826 (metaStateInfo_getTransferTable (minfo),
3827 stateValue_getValue (fval), &msg);
3829 if (cstring_isDefined (msg))
3831 /*@i32 print extra info for assignments@*/
3832 DPRINTF (("From: %s", sRef_unparseFull (sr)));
3833 DPRINTF (("Null? %s / %s",
3834 bool_unparse (sRef_isDefinitelyNull (sr)),
3835 bool_unparse (usymtab_isGuarded (sr))));
3840 ("%s loses reference %q in invalid state %q (%s)",
3841 cstring_makeLiteralTemp (isReturn ? "Return" : "Scope exit"),
3842 uentry_getName (ce),
3843 stateValue_unparseValue (fval, minfo),
3847 stateValue_show (fval, minfo);
3851 DPRINTF (("Suppressed transfer error: %s", msg));
3855 } end_valueTable_elements;
3860 DPRINTF (("Check entry: %s", uentry_unparseFull (ce)));
3862 if (!sRefSet_member (checked, sr) && !sRef_isFileOrGlobalScope (rb))
3864 if (ctype_isRealSU (uentry_getType (ce))
3865 && !uentry_isAnyParam (ce)
3866 && !uentry_isRefParam (ce)
3867 && !uentry_isStatic (ce)
3868 && !sRef_isDependent (sr)
3869 && !sRef_isOwned (sr))
3871 sRefSet als = usymtab_allAliases (sr);
3873 if (sRefSet_isEmpty (als))
3875 checkLocalDestroyed (sr, g_currentloc);
3879 /* aliased, no problem */ ;
3885 (!uentry_isStatic (ce)
3886 && ((sRef_isNewRef (sr))
3887 || (((sRef_isOnly (sr) || sRef_isFresh (sr)
3888 || sRef_isKeep (sr) || sRef_isOwned (sr))
3889 && !sRef_isDead (sr))
3890 && (!sRef_definitelyNull (sr))
3891 && (!usymtab_isDefinitelyNull (sr)))))
3893 bool hasError = TRUE;
3895 DPRINTF (("Checking: %s", sRef_unparseFull (sr)));
3898 ** If its a scope exit, check if there is an alias.
3899 ** If so, make it only. If not, there is an error.
3904 if (canLoseReference (sr, g_currentloc))
3906 DPRINTF (("Can lose!"));
3913 if (sRef_hasLastReference (sr))
3915 sRef ar = sRef_getAliasInfoRef (sr);
3920 ("Last reference %q to %s storage %qnot %q before %q",
3922 alkind_unparse (sRef_getAliasKind (sr)),
3923 sRef_unparseOpt (ar),
3924 cstring_makeLiteral (sRef_isKeep (sr)
3925 ? "transferred" : "released"),
3926 cstring_makeLiteral (isReturn
3927 ? "return" : "scope exit")),
3930 sRef_showRefLost (sr);
3933 else if (sRef_isNewRef (sr))
3938 ("%q %q not released before %q",
3940 (alkind_isKillRef (sRef_getOrigAliasKind (sr))
3941 ? "Kill reference parameter" : "New reference"),
3942 uentry_getName (ce),
3943 cstring_makeLiteral (isReturn
3944 ? "return" : "scope exit")),
3947 sRef_showAliasInfo (sr);
3952 if (ctype_isRealSU (sRef_getType (sr)))
3954 checkStructDestroyed (sr, g_currentloc);
3958 DPRINTF (("Here we are: %s", sRef_unparseFull (sr)));
3963 ("%s storage %q not %q before %q",
3964 alkind_capName (sRef_getAliasKind (sr)),
3965 uentry_getName (ce),
3966 cstring_makeLiteral (sRef_isKeep (sr)
3967 ? "transferred" : "released"),
3968 cstring_makeLiteral (isReturn
3969 ? "return" : "scope exit")),
3972 sRef_showAliasInfo (sr);
3973 DPRINTF (("Storage: %s", sRef_unparseFull (sr)));
3984 else if (mustDefine && uentry_isOut (ce))
3986 if (!ynm_toBoolStrict (sRef_isReadable (sr)))
3990 message ("Out storage %q not defined before %q",
3991 uentry_getName (ce),
3993 (isReturn ? "return" : "scope exit")),
3996 /* uentry_showWhereDeclared (ce); */
4005 ** also check state is okay
4008 if (usymtab_lexicalLevel () > functionScope
4009 && uentry_isVariable (ce)
4010 && (sRef_isLocalVar (sr)
4011 && (sRef_isDependent (sr) || sRef_isLocalState (sr))))
4013 sRefSet ab = usymtab_aliasedBy (sr);
4015 /* should do something more efficient here */
4017 if (sRefSet_isEmpty (ab))
4019 /* and no local ref */
4020 DPRINTF (("Check lose ref: %s", uentry_unparseFull (ce)));
4035 checked = sRefSet_insert (checked, sr);
4039 llassert (usymtab_isDefined (stab->env));
4041 if (usymtab_isBranch (stab))
4043 stab = usymtab_dropEnv (stab);
4050 llassert (stab != usymtab_undefined);
4051 } while (isReturn && (stab->lexlevel >= paramsScope));
4053 sRefSet_free (checked);
4057 ** all globals are appropriately defined
4058 ** all parameters are appropriately defined
4059 ** special clauses are followed
4062 if (isReturn || (utab->lexlevel == paramsScope))
4064 uentry fcn = context_getHeader ();
4065 uentryList params = context_getParams ();
4066 globSet uglobs = context_getUsedGlobs ();
4067 globSet sglobs = context_getGlobs ();
4069 if (isReturn && context_maybeSet (FLG_GLOBALIAS))
4071 aliasTable_checkGlobs (utab->aliases);
4075 ** state clauses (ensures, defines, sets, allocates, releases)
4078 if (uentry_hasStateClauseList (fcn))
4080 stateClauseList clauses = uentry_getStateClauseList (fcn);
4082 stateClauseList_elements (clauses, cl)
4084 if (stateClause_isAfter (cl) && !stateClause_isGlobal (cl))
4086 if (stateClause_setsMetaState (cl))
4088 sRefSet rfs = stateClause_getRefs (cl);
4089 qual q = stateClause_getMetaQual (cl);
4090 annotationInfo ainfo = qual_getAnnotationInfo (q);
4091 metaStateInfo minfo = annotationInfo_getState (ainfo);
4092 cstring key = metaStateInfo_getName (minfo);
4093 int mvalue = annotationInfo_getValue (ainfo);
4095 DPRINTF (("Post meta state clause: %s", stateClause_unparse (cl)));
4097 sRefSet_elements (rfs, el)
4099 sRef base = sRef_getRootBase (el);
4101 if (sRef_isResult (base))
4104 ** This is checked for return transfers.
4108 else if (sRef_isParam (base) || sRef_isGlobalMarker (base))
4110 sRef sr = sRef_updateSref (base);
4111 sr = sRef_fixBase (el, sr);
4113 if (!sRef_checkMetaStateValue (sr, key, mvalue))
4118 ("Ensures clause not satisfied%q (state is %q): %q",
4119 sRef_isGlobalMarker (sr)
4121 : message (" by %q", sRef_unparse (sr)),
4122 stateValue_unparseValue (sRef_getMetaStateValue (sr, key),
4124 stateClause_unparse (cl)),
4127 sRef_showMetaStateInfo (sr, key);
4133 if (sRef_isMeaningful (el))
4138 } end_sRefSet_elements ;
4142 /* evs - 2000 07 10 - added this */
4143 sRefTest tst = stateClause_getPostTestFunction (cl);
4144 sRefSet rfs = stateClause_getRefs (cl);
4146 sRefSet_elements (rfs, el)
4148 sRef base = sRef_getRootBase (el);
4150 if (sRef_isResult (base))
4153 ** This is checked for return transfers.
4158 else if (sRef_isParam (base))
4160 sRef sr = sRef_updateSref (base);
4161 sr = sRef_fixBase (el, sr);
4163 if (tst != NULL && !tst (sr))
4166 (stateClause_postErrorCode (cl),
4167 message ("%s storage %qcorresponds to "
4168 "storage listed in %q clause",
4169 stateClause_postErrorString (cl, sr),
4170 sRef_unparseOpt (sr),
4171 stateClause_unparseKind (cl)),
4174 sRefShower ss = stateClause_getPostTestShower (cl);
4181 DPRINTF (("Storage: %s", sRef_unparseFull (sr)));
4187 if (sRef_isMeaningful (el))
4192 } end_sRefSet_elements ;
4195 } end_stateClauseList_elements ;
4199 ** check parameters on return
4202 uentryList_elements (params, arg)
4204 if (!uentry_isElipsisMarker (arg))
4206 ctype rt = ctype_realType (uentry_getType (arg));
4208 if (ctype_isMutable (rt) || ctype_isSU (rt))
4210 uentry param = usymtab_lookupQuiet (utab, uentry_rawName (arg));
4211 DPRINTF (("Check param return: %s", uentry_unparseFull (param)));
4212 checkParamReturn (param);
4215 } end_uentryList_elements;
4217 DPRINTF (("Check global return: %s",
4218 globSet_unparse (sglobs)));
4220 globSet_allElements (sglobs, el)
4222 sRef orig = el; /*! sRef_updateSref (el); */ /*!!!*/
4223 uentry current = sRef_getUentry (el);
4225 DPRINTF (("Check global return: %s / %s", sRef_unparseFull (el),
4226 uentry_unparseFull (current)));
4228 if (uentry_isVariable (current) && !uentry_isRealFunction (current))
4230 checkGlobalReturn (current, orig);
4232 } end_globSet_allElements;
4234 globSet_allElements (uglobs, el)
4236 if (!globSet_member (sglobs, el))
4238 uentry current = sRef_getUentry (el);
4240 if (uentry_isVariable (current)
4241 && !uentry_isRealFunction (current))
4243 checkGlobalReturn (current, sRef_undefined);
4246 } end_globSet_allElements;
4251 usymtab_quietExitScope (fileloc loc)
4252 /*@globals utab, globtab, filetab; @*/
4255 usymtab t = utab->env;
4257 if (utab->reftable != NULL)
4261 for (i = 0; i < utab->nentries; i++)
4263 uentry current = utab->entries[i];
4264 uentry old = usymtab_lookupAux (t, uentry_rawName (current));
4266 uentry_mergeState (old, current, loc, FALSE, FALSE, FALSE, NOCLAUSE);
4270 llassert (t != NULL);
4272 if (t->lexlevel > paramsScope)
4274 t->guards = guardSet_levelUnion (t->guards, utab->guards, t->lexlevel);
4275 t->aliases = aliasTable_levelUnionSeq (t->aliases, utab->aliases,
4277 utab->aliases = aliasTable_undefined;
4280 t->mustBreak = utab->mustBreak;
4281 t->exitCode = utab->exitCode;
4283 usymtab_freeLevel (utab);
4289 ** Exit a scope with no checking, lose alias states.
4290 ** (When should this be used?)
4293 void usymtab_quietPlainExitScope (void)
4294 /*@globals utab, globtab, filetab@*/
4297 usymtab t = utab->env;
4299 llassert (t != NULL);
4300 llassert (NOALIAS (utab->aliases, t->aliases));
4301 usymtab_freeLevel (utab);
4305 void usymtab_exitScope (exprNode expr)
4306 /*@globals utab, filetab, globtab@*/
4307 /*@modifies utab, globtab@*/
4309 usymtab ctab = usymtab_undefined;
4310 usymtab lctab = usymtab_undefined;
4311 bool mustReturn = exprNode_mustEscape (expr);
4313 DPRINTF (("Exit scope"));
4315 if (utab->kind == US_CBRANCH)
4318 ** save the case branches, remove the first non-cbranch
4323 while (utab->kind == US_CBRANCH)
4327 llassert (utab != GLOBAL_ENV);
4331 if (utab->kind == US_TBRANCH || utab->kind == US_FBRANCH
4332 || utab->kind == US_CBRANCH || utab->kind == US_SWITCH) {
4334 if (context_inMacro ()) {
4335 /* evs 2000-07-25 */
4336 /* Unparseable macro may end inside nested scope. Deal with it. */
4338 llerror (FLG_SYNTAX,
4339 message ("Problem parsing macro body of %s (unbalanced scopes). "
4340 "Attempting to recover, recommend /*@notfunction@*/ before "
4341 "macro definition.",
4342 context_inFunctionName ()));
4344 while (utab->kind == US_TBRANCH
4345 || utab->kind == US_FBRANCH
4346 || utab->kind == US_CBRANCH
4347 || utab->kind == US_SWITCH)
4350 llassert (utab != GLOBAL_ENV);
4353 llcontbug (message ("exitScope: in branch: %q", usymtab_unparseStack ()));
4355 } /*@=branchstate@*/
4359 ** check all variables in scope were used
4363 ** bogus errors if this is the normal inside a switch,
4364 ** since cases have not been merged yet. Should probably
4365 ** still check this, but I'm too lazy at the moment...
4368 llassertfatal (utab->env != GLOBAL_ENV);
4370 if (utab->env->kind != US_SWITCH)
4376 ** check aliasing: all only params are released (dead)
4377 ** definition: all out params are defined, all modified params
4378 ** are completely defined
4380 ** NOTE: note for exiting paramsScope, since checkReturn should be
4384 if (!mustReturn && (usymtab_lexicalLevel () > functionScope))
4387 ** should only call this is end of scope is reachable...
4390 usymtab_checkFinalScope (FALSE);
4393 if (usymtab_lexicalLevel () == paramsScope && context_inFunctionLike ())
4396 ** leaving a function, need to fix up globals
4399 uentryList params = context_getParams ();
4400 globSet globs = context_getUsedGlobs ();
4403 uentryList_elements (params, ue)
4405 uentry_fixupSref (ue);
4406 } end_uentryList_elements;
4408 clearFunctionTypes ();
4411 globSet_allElements (globs, el)
4413 if (sRef_isCvar (el))
4416 int index = sRef_getScopeIndex (el);
4418 if (sRef_isFileStatic (el))
4420 llassert (usymtab_isDefined (filetab));
4421 current = usymtab_fetchIndex (filetab, index);
4425 current = usymtab_fetchIndex (globtab, index);
4428 if (uentry_isVariable (current))
4430 uentry_fixupSref (current);
4434 sRef_clearDerived (uentry_getSref (current));
4437 } end_globSet_allElements;
4440 usymtab_quietExitScope (exprNode_loc (expr));
4442 if (lctab != usymtab_undefined)
4444 /*@i@*/ lctab->env = utab;
4445 /*@i@*/ utab = ctab;
4446 /*@-branchstate@*/ } /*@=branchstate@*/
4452 ** yikes! don't let the '170 kids see this one...
4456 uentry_directParamNo (uentry ue)
4458 if (uentry_isVar (ue))
4460 sRef sr = uentry_getSref (ue);
4462 if (sRef_lexLevel (sr) == functionScope)
4467 llassert (sr->info != NULL);
4468 llassert (sr->info->cvar != NULL);
4469 index = sr->info->cvar->index;
4472 if (index < uentryList_size (context_getParams ()))
4481 /*@dependent@*/ /*@exposed@*/ uentry
4482 usymtab_getParam (int paramno)
4486 ** requires in a function context (checked)
4488 ** depends on no nested functions --- the function
4489 ** parameters are ALWAYS one scope inside the global scope
4490 ** and entered in order!
4494 if (!context_inFunctionLike ())
4495 llfatalbug (message ("usymtab_getParam: not in function context: %q",
4496 context_unparse ()));
4498 while (s->lexlevel > paramsScope)
4503 llassert (usymtab_isDefined (s));
4505 if (paramno >= s->nentries)
4508 ** Parse errors lead to this.
4511 uentry err = uentry_makeVariableLoc (cstring_makeLiteralTemp ("<error>"),
4514 uentry_markOwned (err);
4518 return (s->entries[paramno]);
4521 static /*@dependent@*/ /*@exposed@*/ uentry
4522 usymtab_getRefTab (/*@notnull@*/ usymtab u, int level, usymId index)
4526 ue = usymtab_getRefNoisy (u, level, index);
4528 if (uentry_isUndefined (ue))
4530 llbug (message ("usymtab_getRef: out of range: %d. level = %d",
4537 static /*@dependent@*/ /*@exposed@*/ usymtab
4538 usymtab_dropEnv (/*@notnull@*/ usymtab s)
4540 if (s->kind == US_CBRANCH)
4548 llassert (s != GLOBAL_ENV);
4549 } while (s->kind == US_CBRANCH);
4550 /* drop all cases (except in nested scopes */
4553 llassert (s != GLOBAL_ENV);
4556 if (s->kind == US_FBRANCH)
4558 s = s->env; /* skip the true branch */
4559 llassert (usymtab_isDefined (s));
4560 llassert (s->kind == US_TBRANCH);
4563 llassert (s != GLOBAL_ENV);
4569 /*@dependent@*/ /*@exposed@*/ uentry
4570 usymtab_getRefQuiet (int level, usymId index)
4576 llassert (s != NULL);
4577 llassert (index >= 0);
4579 if (level > s->lexlevel)
4581 return uentry_undefined;
4584 llassertprint (level <= s->lexlevel, ("level: %d / lexlevel: %d",
4585 level, s->lexlevel));
4587 while (s->lexlevel > level)
4589 if (usymtab_isBranch (s))
4591 int eindex = refTable_lookup (s, level, index);
4593 if (eindex != NOT_FOUND)
4595 return (s->entries[eindex]);
4599 s = usymtab_dropEnv (s);
4602 while (usymtab_isBranch (s) && s->lexlevel == level)
4604 int eindex = refTable_lookup (s, level, index);
4606 if (eindex != NOT_FOUND)
4608 return (s->entries[eindex]);
4611 s = usymtab_dropEnv (s);
4614 if (index >= s->nentries)
4616 return uentry_undefined;
4619 llassert (!uentry_isUndefined (s->entries[index]));
4621 return s->entries[index];
4624 static /*@dependent@*/ /*@exposed@*/ uentry
4625 usymtab_getRefNoisy (/*@notnull@*/ usymtab s, int level, usymId index)
4628 uentry ue = uentry_undefined;
4630 llassert (index >= 0);
4632 while (s->lexlevel > level)
4634 if (usymtab_isBranch (s))
4636 int eindex = refTable_lookup (s, level, index);
4638 if (eindex != NOT_FOUND)
4640 ue = s->entries[eindex];
4644 while (!usymtab_isBranch (otab))
4646 otab = usymtab_dropEnv (otab);
4647 llassert (otab != GLOBAL_ENV);
4650 if (refTable_lookup (otab, level, index) == NOT_FOUND)
4652 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4664 s = usymtab_dropEnv (s);
4667 llassert (usymtab_isDefined (s));
4669 while (usymtab_isBranch (s) && s->lexlevel == level)
4671 int eindex = refTable_lookup (s, level, index);
4674 if (eindex != NOT_FOUND)
4676 ue = s->entries[eindex];
4680 while (!usymtab_isBranch (otab))
4682 otab = usymtab_dropEnv (otab);
4683 llassert (otab != GLOBAL_ENV);
4686 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4696 s = usymtab_dropEnv (s);
4699 if (s->lexlevel == level && (index < s->nentries))
4701 ue = s->entries[index];
4703 if (uentry_isValid (ue))
4707 while (!usymtab_isBranch (otab))
4709 otab = usymtab_dropEnv (otab);
4711 if (otab == GLOBAL_ENV)
4717 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4728 if (index >= s->nentries)
4730 return uentry_undefined;
4733 llassert (!uentry_isUndefined (s->entries[index]));
4735 return s->entries[index];
4739 ** looking up entries
4741 ** If entry is inside a branch, then copy it, and put it into
4742 ** the branch table.
4746 int refTable_lookup (/*@notnull@*/ usymtab ut, int level, int index)
4748 refTable rt = ut->reftable;
4751 llassert (rt != NULL);
4753 for (i = 0; i < ut->nentries; i++)
4755 if (rt[i]->level == level && rt[i]->index == index)
4765 /*@only@*/ refentry refentry_create (int level, int index)
4767 refentry r = (refentry) dmalloc (sizeof (*r));
4775 static /*@dependent@*/ /*@exposed@*/ uentry
4776 usymtab_addRefEntry (/*@notnull@*/ usymtab s, cstring k)
4781 if (ut->reftable == NULL)
4783 DPRINTF (("Adding ref entry without reftable: %s", k));
4784 return uentry_undefined;
4787 llassert (ut->reftable != NULL);
4789 while (s != GLOBAL_ENV)
4791 eindex = usymtab_getIndex (s, k);
4793 if (eindex != NOT_FOUND)
4795 uentry current = s->entries[eindex];
4797 if (uentry_isVar (current) && !ctype_isFunction (uentry_getType (current)))
4801 DPRINTF (("Here: copying %s", uentry_unparse (current)));
4802 ue = uentry_copy (current);
4803 DPRINTF (("Here: copying %s", uentry_unparse (ue)));
4804 usymtab_addEntryQuiet (ut, ue);
4805 DPRINTF (("Okay..."));
4807 if (s->reftable != NULL)
4809 refentry ref = s->reftable[eindex];
4811 ut->reftable[ut->nentries - 1]
4812 = refentry_create (ref->level, ref->index);
4816 ut->reftable[ut->nentries - 1]
4817 = refentry_create (s->lexlevel, eindex);
4828 s = usymtab_dropEnv (s);
4831 return uentry_undefined;
4834 static uentry usymtab_lookupAux (usymtab s, cstring k)
4836 DPRINTF (("Lookup: %s", k));
4838 while (s != GLOBAL_ENV)
4840 int eindex = usymtab_getIndex (s, k);
4842 if (eindex != NOT_FOUND)
4844 uentry ret = s->entries[eindex];
4848 if (s->kind == US_TBRANCH
4849 || s->kind == US_FBRANCH
4850 || s->kind == US_CBRANCH)
4851 /* uentry_isGlobalVariable (ret) && os->lexlevel > fileScope) */
4854 DPRINTF (("Adding global ref entry: %s", k));
4855 ret = usymtab_addRefEntry (os, k);
4856 DPRINTF (("Adding ref entry: %s", uentry_unparseFull (ret)));
4861 DPRINTF (("Found: %s", uentry_unparseFull (ret)));
4865 if (s->kind == US_TBRANCH || s->kind == US_FBRANCH
4866 || s->kind == US_CBRANCH)
4868 /* why isn't this os??? */
4869 uentry ret = usymtab_addRefEntry (s, k);
4870 DPRINTF (("Adding ref entry: %s", uentry_unparseFull (ret)));
4877 return uentry_undefined;
4880 static /*@dependent@*/ /*@exposed@*/ uentry
4881 usymtab_lookupQuietAux (usymtab s, cstring k, bool noalt)
4885 while (s != GLOBAL_ENV)
4887 eindex = usymtab_getIndex (s, k);
4889 if (eindex != NOT_FOUND)
4891 uentry ret = s->entries[eindex];
4895 if (noalt && usymtab_isBranch (s))
4897 s = usymtab_dropEnv (s);
4905 return uentry_undefined;
4908 static /*@exposed@*/ /*@dependent@*/ uentry
4909 usymtab_lookupQuiet (usymtab s, cstring k)
4911 return usymtab_lookupQuietAux (s, k, FALSE);
4914 static /*@exposed@*/ /*@dependent@*/ uentry
4915 usymtab_lookupQuietNoAlt (usymtab s, cstring k)
4917 return usymtab_lookupQuietAux (s, k, TRUE);
4920 /*@dependent@*/ /*@observer@*/ uentry
4921 usymtab_lookupSafe (cstring k)
4924 DPRINTF (("Lookup safe: %s", k));
4925 return (usymtab_lookupAux (utab, k));
4929 usymtab_lookupExpose (cstring k)
4932 uentry ce = usymtab_lookupAux (utab, k);
4934 if (uentry_isUndefined (ce))
4936 llfatalbug (message ("usymtab_lookup: not found: *%s*", k));
4939 if (uentry_isPriv (ce))
4941 llfatalbug (message ("usymtab_lookup: private: *%s*", k));
4947 uentry usymtab_lookupExposeGlob (cstring k)
4949 return (usymtab_lookupGlobSafe (k));
4952 uentry usymtab_lookupGlob (cstring k)
4953 /*@globals globtab@*/
4955 uentry ce = usymtab_lookupAux (globtab, k);
4957 if (uentry_isUndefined (ce))
4958 llfatalbug (message ("usymtab_lookup: not found: %s", k));
4960 if (uentry_isPriv (ce))
4961 llfatalbug (message ("usymtab_lookup: private: %s", k));
4966 /*@observer@*/ uentry
4967 usymtab_lookupGlobSafe (cstring k)
4968 /*@globals globtab@*/
4970 uentry ce = usymtab_lookupAux (globtab, k);
4975 uentry usymtab_lookupEither (cstring k)
4978 uentry ce = usymtab_lookupSafe (k);
4980 if (uentry_isUndefined (ce))
4981 llfatalerror (message ("usymtab_lookup: not found: %s", k));
4988 usymtab_lookupType (cstring k)
4989 /*@globals globtab@*/
4991 usymId uid = usymtab_getTypeId (k);
4993 if (uid == USYMIDINVALID)
4995 llcontbug (message ("usymtab_lookupType: not found: %s", k));
4996 return ctype_unknown;
4999 return (uentry_getRealType (usymtab_getTypeEntry (uid)));
5004 usymtab_lookupAbstractType (cstring k) /*@globals globtab@*/
5006 usymId uid = usymtab_getTypeId (k);
5008 if (uid == USYMIDINVALID)
5010 llcontbug (message ("usymtab_lookupType: not found: %s", k));
5011 return ctype_unknown;
5014 return (uentry_getAbstractType (usymtab_getTypeEntry (uid)));
5018 ** if there is an unnamed lcl-specified struct tag matching
5019 ** the uentryList, return its datatype. Otherwise, returns
5024 usymtab_structFieldsType (uentryList f)
5025 /*@globals globtab@*/
5027 return (usymtab_suFieldsType (f, TRUE));
5031 usymtab_unionFieldsType (uentryList f)
5032 /*@globals globtab@*/
5034 return (usymtab_suFieldsType (f, FALSE));
5038 usymtab_suFieldsType (uentryList f, bool isStruct)
5039 /*@globals globtab@*/
5043 DPRINTF (("Fields: %s", uentryList_unparse (f)));
5045 if (fileloc_isSpec (g_currentloc))
5047 return (ctype_undefined);
5050 for (i = 0; i < globtab->nentries; i++)
5052 uentry current = globtab->entries[i];
5055 ? uentry_isStructTag (current) : uentry_isUnionTag (current)))
5057 if (isFakeTag (uentry_rawName (current)))
5059 ctype ct = uentry_getType (current);
5061 DPRINTF (("Check: %s", ctype_unparse (ct)));
5063 if ((isStruct ? ctype_isStruct (ct) : ctype_isUnion (ct))
5065 (uentry_isSpecified (current)
5066 && uentryList_equivFields (f, ctype_getFields (ct))))
5068 return uentry_getAbstractType (current);
5078 return ctype_undefined;
5082 usymtab_enumEnumNameListType (enumNameList f)
5083 /*@globals globtab@*/
5087 for (i = 0; i < globtab->nentries; i++)
5089 uentry current = globtab->entries[i];
5091 if (uentry_isEnumTag (current))
5093 if (isFakeTag (uentry_rawName (current)))
5095 ctype ct = uentry_getType (current);
5097 if (ctype_isEnum (ct) && (enumNameList_match (f, ctype_elist (ct))))
5099 return uentry_getType (current);
5105 return ctype_undefined;
5109 usymtab_exists (cstring k)
5112 uentry ce = usymtab_lookupSafe (k);
5113 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5117 usymtab_existsReal (cstring k)
5120 uentry ce = usymtab_lookupSafe (k);
5122 return (!(uentry_isUndefined (ce))
5123 && !(uentry_isPriv (ce))
5124 && !(uentry_isExpandedMacro (ce)));
5128 usymtab_existsGlob (cstring k)
5129 /*@globals globtab@*/
5131 uentry ce = usymtab_lookupAux (globtab, k);
5133 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5138 usymtab_existsEither (cstring k)
5141 uentry ce = usymtab_lookupAux (utab, k);
5143 return (uentry_isValid (ce));
5147 usymtab_existsGlobEither (cstring k)
5148 /*@globals globtab@*/
5150 uentry ce = usymtab_lookupAux (globtab, k);
5152 return (uentry_isValid (ce));
5157 usymtab_existsType (cstring k)
5158 /*@globals globtab@*/
5160 uentry ce = usymtab_lookupAux (globtab, k);
5162 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)) && uentry_isDatatype (ce));
5166 usymtab_existsTypeEither (cstring k)
5167 /*@globals globtab@*/
5170 ce = usymtab_lookupAux (globtab, k);
5171 return (uentry_isValid (ce) && uentry_isDatatype (ce));
5175 usymtab_existsStructTag (cstring k) /*@globals globtab@*/
5177 cstring sname = makeStruct (k);
5178 uentry ce = usymtab_lookupAux (globtab, sname);
5179 cstring_free (sname);
5180 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5184 usymtab_existsUnionTag (cstring k) /*@globals globtab@*/
5186 cstring uname = makeUnion (k);
5187 uentry ce = usymtab_lookupAux (globtab, uname);
5189 cstring_free (uname);
5191 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5195 usymtab_existsEnumTag (cstring k) /*@globals globtab@*/
5197 cstring ename = makeEnum (k);
5198 uentry ce = usymtab_lookupAux (globtab, ename);
5200 cstring_free (ename);
5201 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5205 bool usymtab_existsVar (cstring k)
5208 uentry ce = usymtab_lookupSafe (k);
5210 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)) && (uentry_isVar (ce)));
5219 refTable_free (/*@only@*/ /*@null@*/ refTable x, int nentries)
5225 for (i = 0; i < nentries; i++)
5235 usymtab_freeLevel (/*@notnull@*/ /*@only@*/ usymtab u)
5236 /*@globals globtab, utab, filetab@*/
5240 aliasTable_free (u->aliases);
5242 refTable_free (u->reftable, u->nentries);
5244 if (u == filetab || u == globtab)
5246 for (i = 0; i < u->nentries; i++)
5248 uentry_freeComplete (u->entries[i]);
5253 for (i = 0; i < u->nentries; i++)
5255 uentry_free (u->entries[i]);
5259 guardSet_free (u->guards);
5266 llassert (!cstringTable_isDefined (u->htable));
5274 usymtab_freeAux (/*@only@*/ usymtab u)
5275 /*@globals globtab, utab, filetab@*/
5278 while (u != GLOBAL_ENV)
5281 usymtab_freeLevel (u);
5288 void usymtab_free ()
5289 /*@globals killed utab, globtab, filetab@*/
5293 usymtab_freeAux (utab);
5296 static int usymtab_lexicalLevel (void) /*@globals utab@*/
5298 return (utab->lexlevel);
5301 bool usymtab_inGlobalScope () /*@globals utab, globtab@*/
5303 return (utab == globtab);
5306 bool usymtab_inFileScope () /*@globals utab@*/
5308 return (utab->lexlevel == fileScope);
5311 bool usymtab_inFunctionScope () /*@globals utab@*/
5313 return (utab->lexlevel == functionScope);
5318 usymtab_replaceEntry (uentry s)
5319 /*@globals utab, globtab@*/
5320 /*@modifies utab, s@*/
5322 usymtab_replaceEntryAux (utab, s);
5327 usymtab_matchForwardStruct (usymId u1, usymId u2)
5328 /*@globals globtab@*/
5330 uentry ue1 = usymtab_getTypeEntry (u1);
5331 uentry ue2 = usymtab_getTypeEntry (u2);
5333 if (uentry_isAnyTag (ue2))
5335 ctype reptype = uentry_getType (ue1);
5337 if (ctype_isPointer (reptype))
5339 ctype repbase = ctype_getBaseType (reptype);
5341 if (ctype_isUA (repbase))
5343 typeId rtuid = ctype_typeId (repbase);
5345 if (u2 == rtuid) return TRUE;
5347 if (usymId_isValid (rtuid))
5349 reptype = uentry_getType (usymtab_getTypeEntry (rtuid));
5351 return (ctype_isUA (reptype) && (u2 == (ctype_typeId (reptype))));
5360 void usymtab_addGuards (guardSet guards)
5363 utab->guards = guardSet_union (utab->guards, guards);
5366 static bool usymtab_isGuardedAux (sRef s)
5370 sRef base = sRef_getRootBase (s);
5371 int lowlevel = paramsScope;
5372 int baselevel = sRef_lexLevel (base);
5374 if (sRef_isCvar (base))
5376 lowlevel = baselevel;
5377 if (lowlevel < paramsScope) lowlevel = paramsScope;
5380 while (tab->lexlevel >= lowlevel)
5382 if (guardSet_isGuarded (tab->guards, s))
5385 if (!sRef_definitelyNull (s))
5387 sRef_setNotNull (s, fileloc_undefined);
5393 tab = usymtab_dropEnv (tab);
5399 void usymtab_unguard (sRef s) /*@modifies utab@*/
5402 sRef base = sRef_getRootBase (s);
5403 int lowlevel = paramsScope;
5404 int baselevel = sRef_lexLevel (base);
5406 if (sRef_isCvar (base))
5408 lowlevel = baselevel;
5409 if (lowlevel < paramsScope) lowlevel = paramsScope;
5412 while (tab->lexlevel >= lowlevel)
5414 if (guardSet_isGuarded (tab->guards, s))
5416 guardSet_delete (tab->guards, s);
5419 tab = usymtab_dropEnv (tab);
5423 bool usymtab_isGuarded (sRef s)
5426 return (sRef_aliasCompleteSimplePred (usymtab_isGuardedAux, s));
5429 bool usymtab_isDefinitelyNull (sRef s)
5431 return (sRef_aliasCheckSimplePred (usymtab_isDefinitelyNullAux, s));
5434 bool usymtab_isDefinitelyNullDeep (sRef s)
5436 return (sRef_deepPred (usymtab_isDefinitelyNull, s));
5439 static bool usymtab_isDefinitelyNullAux (sRef s)
5443 sRef base = sRef_getRootBase (s);
5444 int lowlevel = paramsScope;
5446 if (sRef_isCvar (base))
5448 lowlevel = sRef_lexLevel (base);
5449 if (lowlevel < paramsScope) lowlevel = paramsScope;
5452 while (tab->lexlevel >= lowlevel)
5454 if (guardSet_mustBeNull (tab->guards, s))
5459 while (tab->kind == US_CBRANCH)
5464 llassert (usymtab_isDefined (tab));
5466 if (tab->kind == US_FBRANCH)
5469 llassert (tab->kind == US_TBRANCH);
5479 usymtab_printGuards ()
5480 /*@globals utab, globtab@*/
5482 usymtab ttab = utab;
5484 while (ttab != globtab)
5486 llmsg (message ("Guards [%d]: %q", ttab->lexlevel,
5487 guardSet_unparse (ttab->guards)));
5493 usymtab_displayAllUses ()
5494 /*@globals utab, globtab@*/
5498 /* only in top scope */
5499 llassert (utab == globtab);
5501 /* need a copy, so order is not messed up by sort! */
5502 copy = usymtab_shallowCopy (globtab);
5504 qsort (copy->entries, (size_t)copy->nentries,
5505 sizeof (*copy->entries), (int (*)(const void *, const void *)) uentry_xcompareuses);
5507 usymtab_entries (copy, ue)
5509 if (uentry_isValid (ue))
5511 filelocList uses = uentry_getUses (ue);
5512 int size = filelocList_realSize (uses);
5514 if (fileloc_isDefined (uentry_whereDefined (ue))
5515 && !fileloc_isLib (uentry_whereDefined (ue))
5518 llmsg (message ("%q (%q), %d use%&:\n %q",
5519 uentry_getName (ue),
5520 fileloc_unparse (uentry_whereDefined (ue)),
5521 size, filelocList_unparseUses (uses)));
5524 } end_usymtab_entries;
5526 usymtab_shallowFree (copy);
5529 static /*@dependent@*/ /*@exposed@*/ usymtab
5530 usymtab_getFileTab ()
5531 /*@globals filetab@*/
5533 llassert (filetab != NULL);
5539 usymtab_unparseStack ()
5542 return (usymtab_unparseStackTab (utab));
5545 static /*@only@*/ cstring
5546 usymtab_unparseStackTab (usymtab t)
5548 bool firstOne = TRUE;
5549 cstring ret = cstring_makeLiteral ("[");
5551 while (t != GLOBAL_ENV)
5555 ret = message ("%q %q", ret, usymtab_typeName (t));
5560 ret = message ("%q, %q", ret, usymtab_typeName (t));
5565 ret = message ("%q ]", ret);
5569 static /*@only@*/ cstring
5570 usymtab_typeName (/*@notnull@*/ usymtab t)
5574 case US_GLOBAL: return cstring_makeLiteral ("global");
5575 case US_NORMAL: return cstring_makeLiteral ("normal");
5576 case US_TBRANCH: return cstring_makeLiteral ("true");
5577 case US_FBRANCH: return cstring_makeLiteral ("false");
5578 case US_CBRANCH: return cstring_makeLiteral ("case");
5579 case US_SWITCH: return cstring_makeLiteral ("switch");
5585 void usymtab_addMustAlias (/*@exposed@*/ sRef s, /*@exposed@*/ sRef al)
5588 if (!sRef_similar (s, al))
5590 usymtab_addForceMustAlias (s, al);
5595 ** Same as usymtab_addMustAlias, except does not check sRef_isSimilar.
5598 void usymtab_addForceMustAlias (/*@exposed@*/ sRef s, /*@exposed@*/ sRef al)
5601 if (sRef_isMeaningful (s)
5602 && sRef_isMeaningful (al)
5603 && !(sRef_isConst (s) || sRef_isConst (al))
5604 && !(sRef_isAddress (al) && sRef_isDirectParam (sRef_getBase (al))))
5606 utab->aliases = aliasTable_addMustAlias (utab->aliases, s, al);
5607 DPRINTF (("Must alias: %s", aliasTable_unparse (utab->aliases)));
5610 ** for local variable, aliasing is symmetric
5613 if (sRef_isLocalVar (s) && sRef_isLocalVar (al))
5615 utab->aliases = aliasTable_addMustAlias (utab->aliases, al, s);
5624 void usymtab_clearAlias (sRef s)
5625 /*@modifies utab, s@*/
5628 aliasTable_clearAliases (utab->aliases, s);
5631 sRefSet usymtab_allAliases (sRef s)
5634 if (sRef_isMeaningful (s))
5638 ret = sRefSet_unionFree (aliasTable_aliasedBy (utab->aliases, s),
5639 aliasTable_canAlias (utab->aliases, s));
5644 DPRINTF (("NOT A MEANINGFUL SREF!"));
5645 return sRefSet_undefined;
5649 /*@only@*/ sRefSet usymtab_canAlias (sRef s)
5652 if (sRef_isMeaningful (s))
5654 sRefSet res = aliasTable_canAlias (utab->aliases, s);
5658 return sRefSet_undefined;
5661 /*@only@*/ sRefSet usymtab_aliasedBy (sRef s)
5664 return (aliasTable_aliasedBy (utab->aliases, s));
5667 /*@only@*/ cstring usymtab_unparseAliases ()
5670 return (aliasTable_unparse (utab->aliases));
5674 ** Debugging routines:
5675 ** okay to leak storage here, only for debugging
5681 usymtab_printOut (void)
5687 char *ind = mstring_copy (" ");
5689 fprintf (g_msgstream, "<<< [symbol table] >>>\n");
5691 while (s != GLOBAL_ENV && s->env != GLOBAL_ENV)
5693 cstring tname = usymtab_typeName (s);
5697 ind[depth * 3 + 1] = '\0';
5700 fprintf (g_msgstream, "level: %d (%s)\n", s->lexlevel,
5701 cstring_toCharsSafe (tname));
5703 cstring_free (tname);
5705 for (i = 0; i < s->nentries; i++)
5707 cstring us = uentry_unparseFull (s->entries[i]);
5708 fprintf (g_msgstream, "%s\n", cstring_toCharsSafe (us));
5712 if (s->reftable != NULL && s->nentries > 0)
5714 fprintf (g_msgstream, "\t<< Ref table >>\n");
5716 for (i = 0; i < s->nentries; i++)
5718 fprintf (g_msgstream, "\t%s %3d: %d, %d\n", ind, i,
5719 s->reftable[i]->level,
5720 s->reftable[i]->index);
5724 ind[depth * 3 + 1] = ' ';
5728 fprintf (g_msgstream, "<<< end usymtab >>>\n");
5734 usymtab_printTypes ()
5735 /*@globals globtab@*/
5737 usymtab_printAllAux (globtab);
5741 usymtab_printAll (void)
5744 usymtab_printAllAux (utab);
5748 usymtab_printAllAux (usymtab s)
5749 /*@modifies g_msgstream@*/
5753 char *ind = mstring_copy (" ");
5755 printf ("[[[ usymtab ]]]");
5757 while (s != GLOBAL_ENV)
5760 ind[depth * 3 + 1] = '\0';
5762 if (s->env == GLOBAL_ENV)
5766 printf ("level: %d / break: %s / exit: %s\n", s->lexlevel,
5767 cstring_toCharsSafe (bool_unparse (s->mustBreak)),
5768 cstring_toCharsSafe (exitkind_unparse (s->exitCode)));
5772 for (i = looplow; i < s->nentries; i++)
5774 printf ("%s%3d. %s\n", ind, i,
5775 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5780 printf ("level: %d / break: %s / exit: %s\n", s->lexlevel,
5781 cstring_toCharsSafe (bool_unparse (s->mustBreak)),
5782 cstring_toCharsSafe (exitkind_unparse (s->exitCode)));
5784 for (i = 0; i < s->nentries; i++)
5786 printf ("%s%3d %s\n", ind, i,
5787 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5791 ind[depth * 3 + 1] = ' ';
5795 printf ("----------\n");
5799 usymtab_printComplete ()
5804 char *ind = mstring_copy (" ");
5807 while (s != GLOBAL_ENV)
5811 ind[depth * 3 + 1] = '\0';
5814 if (s->env == GLOBAL_ENV)
5818 printf ("level: %d\n", s->lexlevel);
5822 for (i = looplow; i < s->nentries; i++)
5824 printf ("%s%3d %s\n", ind, i,
5825 cstring_toCharsSafe (uentry_unparse (s->entries[i])));
5830 printf ("level: %d\n", s->lexlevel);
5831 for (i = 0; i < s->nentries; i++)
5833 printf ("%s%3d %s\n", ind, i,
5834 cstring_toCharsSafe (uentry_unparse (s->entries[i])));
5838 ind[depth * 3 + 1] = ' ';
5843 printf ("----------\n");
5847 static /*@only@*/ cstring /*@unused@*/
5848 usymtab_unparseLocalAux (/*@notnull@*/ usymtab s)
5850 cstring c = message ("lexlevel: %d\n", s->lexlevel);
5853 for (i = 0; i < s->nentries; i++)
5855 c = message ("%q\n%q", c, uentry_unparseFull (s->entries[i]));
5858 c = message ("%q\n=========", c);
5862 static cstring /*@unused@*/ /*@only@*/
5863 usymtab_unparseLocalList (/*@notnull@*/ usymtab s)
5865 cstring c = message ("[%d/%s/%s] ", s->lexlevel,
5866 bool_unparse (s->mustBreak),
5867 exitkind_unparse (s->exitCode));
5870 for (i = 0; i < s->nentries; i++)
5872 sRef sr = uentry_getSref (s->entries[i]);
5876 c = message ("%q: %q [%b]", c, uentry_getName (s->entries[i]),
5877 sRef_isStateDefined (sr));
5881 c = message ("%q, %q [%b]", c, uentry_getName (s->entries[i]),
5882 sRef_isStateDefined (sr));
5891 usymtab_printLocal (void)
5897 printf ("lexlevel: %d\n", s->lexlevel);
5899 for (i = 0; i < s->nentries; i++)
5901 printf ("%s\n", cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5904 while (s->lexlevel > 1)
5909 llassert (usymtab_isDefined (s));
5911 printf ("Params:\n");
5913 for (i = 0; i < s->nentries; i++)
5915 printf ("%d: %s\n", i,
5916 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5921 static bool checkDistinctExternalName (uentry e)
5922 /*@globals globtab@*/
5923 /*@modifies *g_msgstream@*/
5925 int checklen = context_getValue (FLG_EXTERNALNAMELEN);
5926 bool ignorecase = context_getFlag (FLG_EXTERNALNAMECASEINSENSITIVE);
5927 bool gotone = FALSE;
5928 bool extras = FALSE;
5929 bool hasError = FALSE;
5930 cstring name = uentry_rawName (e);
5931 usymtab st = globtab;
5939 if (uentry_isAnyTag (e))
5941 checklen++; /* the tag marker doesn't count */
5945 usymtab_entries (st, oe)
5947 if (uentry_sameObject (oe, e))
5954 if (cstring_equalCaseInsensitive (uentry_rawName (oe), name))
5963 (FLG_DISTINCTEXTERNALNAMES,
5965 ("External identifier %q is not distinguishable from %q "
5966 "because alphabetical case is ignored",
5968 uentry_getName (oe)),
5969 uentry_whereLast (e)))
5971 uentry_showWhereAny (oe);
5972 uentry_setHasNameError (oe);
5981 if (cstring_equalLenCaseInsensitive (uentry_rawName (oe),
5990 if (cstring_equalLen (uentry_rawName (oe), name, checklen))
5993 (FLG_DISTINCTEXTERNALNAMES,
5996 ("External identifier %q is not distinguishable from %q "
5997 "in the first %d characters (%q)",
5999 uentry_getName (oe),
6001 cstring_clip (uentry_getName (e), checklen)),
6003 uentry_whereLast (e)))
6005 uentry_showWhereAny (oe);
6006 uentry_setHasNameError (oe);
6019 (FLG_DISTINCTEXTERNALNAMES,
6021 ("External identifier %q is not distinguishable from %q "
6022 "in the first %d characters because alphabetical case "
6025 uentry_getName (oe),
6027 uentry_whereLast (e)))
6029 uentry_showWhereAny (oe);
6030 uentry_setHasNameError (oe);
6036 else if (cstring_equalLen (uentry_rawName (oe), name, checklen))
6045 (FLG_DISTINCTEXTERNALNAMES,
6048 ("External identifier %q is not distinguishable from %q "
6049 "in the first %d characters (%q)",
6051 uentry_getName (oe),
6053 cstring_clip (uentry_getName (e), checklen)),
6055 uentry_whereLast (e)))
6057 uentry_showWhereAny (oe);
6058 uentry_setHasNameError (oe);
6067 } end_usymtab_entries ;
6074 (cstring_makeLiteral ("One or more additional "
6075 "indistinguishable external "
6076 "names not reported"));
6082 static bool checkDistinctInternalName (uentry e)
6084 /*@modifies *g_msgstream@*/
6086 usymtab ttab = utab;
6087 cstring name = uentry_rawName (e);
6088 int numchars = context_getValue (FLG_INTERNALNAMELEN);
6089 bool caseinsensitive = context_getFlag (FLG_INTERNALNAMECASEINSENSITIVE);
6090 bool lookalike = context_getFlag (FLG_INTERNALNAMELOOKALIKE);
6092 if (uentry_isAnyTag (e) && (numchars != 0))
6094 numchars++; /* the tag marker doesn't count */
6097 while (usymtab_isDefined (ttab))
6099 usymtab_entries (ttab, oe)
6101 if (uentry_sameObject (oe, e))
6103 /*@innercontinue@*/ continue;
6106 switch (cstring_genericEqual
6107 (name, uentry_rawName (oe),
6108 numchars, caseinsensitive, lookalike))
6110 case CGE_DISTINCT: /* okay */
6114 if (cstring_equal (name, uentry_rawName (oe)))
6116 ; /* got a shadow error */
6121 (FLG_DISTINCTINTERNALNAMES,
6124 ("Internal identifier %q is not distinguishable from %q "
6125 "in the first %d characters (%q)",
6127 uentry_getName (oe),
6129 cstring_clip (uentry_getName (e), numchars)),
6131 uentry_whereLast (e)))
6133 uentry_showWhereAny (oe);
6134 uentry_setHasNameError (oe);
6142 || (cstring_length (name) <= numchars))
6145 (FLG_DISTINCTINTERNALNAMES,
6147 ("Internal identifier %q is not distinguishable from %q "
6148 "without case sensitivity",
6150 uentry_getName (oe)),
6151 uentry_whereLast (e)))
6153 uentry_showWhereAny (oe);
6154 uentry_setHasNameError (oe);
6161 (FLG_DISTINCTINTERNALNAMES,
6163 ("Internal identifier %q is not distinguishable from %q "
6164 "in the first %d characters without case sensitivity",
6166 uentry_getName (oe),
6168 uentry_whereLast (e)))
6170 uentry_showWhereAny (oe);
6171 uentry_setHasNameError (oe);
6179 || (cstring_length (name) <= numchars))
6182 (FLG_DISTINCTINTERNALNAMES,
6184 ("Internal identifier %q is not distinguishable from %q "
6185 "except by lookalike characters",
6187 uentry_getName (oe)),
6188 uentry_whereLast (e)))
6190 uentry_showWhereAny (oe);
6191 uentry_setHasNameError (oe);
6198 (FLG_DISTINCTINTERNALNAMES,
6200 ("Internal identifier %q is not distinguishable from %q "
6201 "in the first %d characters except by lookalike characters",
6203 uentry_getName (oe),
6205 uentry_whereLast (e)))
6207 uentry_showWhereAny (oe);
6208 uentry_setHasNameError (oe);
6213 } end_usymtab_entries ;
6221 void usymtab_checkDistinctName (uentry e, int scope)
6222 /*@globals utab, globtab@*/
6224 bool hasError = FALSE;
6225 fileloc where = uentry_whereLast (e);
6227 if (!fileloc_isPreproc (where) && !fileloc_isBuiltin (where))
6229 if (scope == globScope)
6231 if (context_getFlag (FLG_DISTINCTEXTERNALNAMES))
6233 hasError = checkDistinctExternalName (e);
6237 if (!hasError && context_getFlag (FLG_DISTINCTINTERNALNAMES))
6239 hasError = checkDistinctInternalName (e);
6245 uentry_setHasNameError (e);
6249 /*@exposed@*/ sRef usymtab_lookupGlobalMarker (void) /*@globals utab@*/
6253 ue = usymtab_lookupAux (utab, GLOBAL_MARKER_NAME);
6254 llassert (uentry_isValid (ue));
6256 return uentry_getSref (ue);