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));
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);
946 ** no previous definition, add the new one
950 && uentry_isValid (outer)
951 && !(uentry_isYield (e) || uentry_isYield (outer))
952 && fileloc_isDefined (uentry_whereLast (e))
953 && !fileloc_isXHFile (uentry_whereLast (e))
954 && fileloc_isDefined (uentry_whereLast (outer))
955 && !fileloc_isXHFile (uentry_whereLast (outer)))
957 if (!uentry_sameKind (outer, e))
963 if (ctype_isUnknown (uentry_getType (outer))
964 || uentry_isForward (outer))
972 message ("%s %q shadows outer declaration",
973 ekind_capName (uentry_getKind (e)),
975 uentry_whereLast (e)))
977 uentry_showWhereLast (outer);
983 if (st == globtab && context_getFlag (FLG_NEWDECL))
987 message ("New declaration: %q", uentry_getName (e)),
988 uentry_whereLast (e));
991 eindex = usymtab_addEntryAux (st, e, isSref);
995 return (staticEntry ? USYMIDINVALID : eindex);
1000 usymtab_replaceEntryAux (/*@notnull@*/ usymtab st, /*@only@*/ uentry e)
1001 /*@globals globtab@*/ /*@modifies st, e@*/
1003 cstring ename = uentry_rawName (e);
1006 /* static tags in global scope */
1007 eindex = usymtab_getIndex (st, ename);
1009 if (eindex != NOT_FOUND)
1011 uentry ce = st->entries[eindex];
1013 if (cstringTable_isDefined (st->htable))
1015 cstringTable_replaceKey (st->htable, uentry_rawName (ce),
1016 cstring_copy (uentry_rawName (e)));
1020 st->entries[eindex] = e;
1024 eindex = usymtab_addEntryAux (st, e, FALSE);
1031 void usymtab_supEntry (uentry e)
1032 /*@globals utab, filetab, globtab@*/
1033 /*@modifies utab, globtab, e@*/
1035 (void) usymtab_supEntryAux (utab, e, FALSE);
1039 ** this should be lots more efficient!
1042 static /*@exposed@*/ uentry
1043 usymtab_supEntryReturnAux (/*@notnull@*/ usymtab tab,
1044 /*@only@*/ uentry e, bool isref)
1045 /*@globals globtab, filetab@*/
1046 /*@modifies tab, globtab, e@*/
1048 cstring rawName = cstring_copy (uentry_rawName (e));
1049 bool stat = (tab == globtab) && uentry_isStatic (e);
1053 (void) usymtab_supEntryAux (tab, e, isref);
1057 ret = usymtab_lookupAux (filetab, rawName);
1061 ret = usymtab_lookupAux (tab, rawName);
1063 if (uentry_isInvalid (ret) && usymtab_isDefined (filetab))
1065 ret = usymtab_lookupAux (filetab, rawName);
1069 cstring_free (rawName);
1073 /*@dependent@*/ /*@exposed@*/ uentry
1074 usymtab_supEntryReturn (/*@only@*/ uentry e)
1075 /*@globals utab, filetab, globtab@*/
1076 /*@modifies utab, globtab, e@*/
1078 return (usymtab_supEntryReturnAux (utab, e, FALSE));
1081 /*@dependent@*/ /*@exposed@*/ uentry
1082 usymtab_supEntrySrefReturn (/*@only@*/ uentry e)
1083 /*@globals utab, globtab, filetab@*/
1084 /*@modifies utab, globtab, e@*/
1086 return (usymtab_supEntryReturnAux (utab, e, TRUE));
1089 /*@dependent@*/ /*@exposed@*/ uentry
1090 usymtab_supGlobalEntryReturn (uentry e)
1091 /*@globals globtab, filetab@*/
1092 /*@modifies globtab, e@*/
1096 ret = usymtab_supEntryReturnAux (globtab, e, FALSE);
1098 if (sRef_modInFunction ())
1100 recordFunctionType (ret);
1107 usymtab_supTypeEntry (/*@only@*/ uentry e)
1108 /*@globals globtab, filetab@*/
1109 /*@modifies globtab, e@*/
1114 if (uentry_isAbstractDatatype (e))
1116 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1117 ret = ctype_createAbstract (uid);
1121 uid = usymtab_supEntryAux (globtab, e, FALSE);
1122 ret = ctype_createUser (uid);
1125 if (sRef_modInFunction ())
1127 recordFunctionType (globtab->entries[uid]);
1134 usymtab_supReturnTypeEntry (/*@only@*/ uentry e)
1135 /*@globals globtab, filetab@*/
1136 /*@modifies globtab@*/
1140 DPRINTF (("Abstract? %s", uentry_unparseFull (e)));
1142 if (uentry_isAbstractDatatype (e))
1144 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1146 else if (uentry_isMaybeAbstract (e) && context_getFlag (FLG_IMPABSTRACT))
1148 bool maybeabs = TRUE;
1149 cstring sname = uentry_getName (e);
1150 uentry ue = usymtab_lookupGlobSafe (sname);
1151 cstring_free (sname);
1153 if (uentry_isValid (ue))
1155 DPRINTF (("Lookup: %s", uentry_unparseFull (ue)));
1157 if (uentry_isDatatype (ue))
1159 if (uentry_isMaybeAbstract (ue))
1170 DPRINTF (("Not datatype!"));
1177 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1178 ux = usymtab_getTypeEntry (uid);
1179 uentry_setAbstract (ux);
1183 uid = usymtab_supEntryAux (globtab, e, FALSE);
1184 e = usymtab_getTypeEntry (uid);
1186 if (uentry_isMaybeAbstract (e))
1188 uentry_setConcrete (e);
1194 uid = usymtab_supEntryAux (globtab, e, FALSE);
1195 e = usymtab_getTypeEntry (uid);
1197 if (uentry_isMaybeAbstract (e))
1199 uentry_setConcrete (e);
1203 if (sRef_modInFunction ())
1205 recordFunctionType (globtab->entries[uid]);
1208 return (globtab->entries[uid]);
1212 usymtab_supAbstractTypeEntry (/*@only@*/ uentry e, bool dodef)
1213 /*@globals globtab, filetab@*/
1214 /*@modifies globtab, e@*/
1219 uid = usymtab_supEntryAux (globtab, e, FALSE);
1220 ue = usymtab_getTypeEntry (uid);
1224 uentry_setDatatype (ue, uid);
1227 if (context_getFlag (FLG_ACCESSMODULE)) /* was accessfile */
1229 context_addFileAccessType (uid);
1232 if (sRef_modInFunction ())
1234 recordFunctionType (globtab->entries[uid]);
1242 usymtab_supExposedTypeEntry (/*@only@*/ uentry e, bool dodef)
1243 /*@globals globtab, filetab@*/
1244 /*@modifies globtab, e@*/
1248 uid = usymtab_supEntryAux (globtab, e, FALSE);
1252 uentry ue = usymtab_getTypeEntry (uid);
1254 uentry_setDatatype (ue, uid);
1257 if (sRef_modInFunction ())
1259 recordFunctionType (globtab->entries[uid]);
1267 usymtab_supForwardTypeEntry (/*@only@*/ uentry e)
1268 /*@globals globtab, filetab@*/
1269 /*@modifies globtab, e@*/
1271 usymId uid = usymtab_supEntryAux (globtab, e, FALSE);
1272 uentry ue = usymtab_getTypeEntry (uid);
1274 uentry_setDatatype (ue, uid);
1276 if (sRef_modInFunction ())
1278 recordFunctionType (globtab->entries[uid]);
1281 return (uentry_getAbstractType (ue));
1285 usymtab_supEntrySref (uentry e)
1286 /*@globals utab, globtab, filetab@*/
1287 /*@modifies utab, globtab, e@*/
1289 sRef old = uentry_getSref (e);
1292 if (sRef_isType (old))
1294 uentry ue = usymtab_supEntryReturnAux (utab, e, TRUE);
1297 if (uentry_isValid (ue))
1299 sRef uref = uentry_getSref (ue);
1301 sRef_mergeStateQuiet (uref, old);
1302 sRef_clearDerived (uref);
1304 /*@noaccess uentry@*/
1306 else if (sRef_isKnown (old))
1308 usymtab_supEntry (e);
1312 (void) usymtab_supEntryAux (utab, e, TRUE);
1316 void usymtab_supGlobalEntry (/*@only@*/ uentry e)
1317 /*@globals globtab, filetab@*/
1318 /*@modifies globtab, filetab, e@*/
1322 DPRINTF (("Sup global entry: %s", uentry_unparse (e)));
1324 uid = usymtab_supEntryAux (globtab, e, FALSE);
1326 if (sRef_modInFunction ())
1328 recordFunctionType (globtab->entries[uid]);
1333 usymtab_supReturnFileEntry (/*@only@*/ uentry e)
1334 /*@globals filetab, globtab@*/
1335 /*@modifies filetab, globtab, e@*/
1337 llassert (filetab != usymtab_undefined);
1338 DPRINTF (("File entry: %s", uentry_unparse (e)));
1339 return (usymtab_supEntryReturnAux (filetab, e, FALSE));
1347 usymtab_inDeepScope () /*@globals utab@*/
1349 return (utab->lexlevel > paramsScope);
1353 usymtab_getIndex (/*@notnull@*/ usymtab s, cstring k)
1357 DPRINTF (("Lookup %s", k));
1359 if (cstringTable_isDefined (s->htable))
1361 i = cstringTable_lookup (s->htable, k);
1366 for (i = 0; i < s->nentries; i++)
1368 uentry current = s->entries[i];
1370 DPRINTF (("Check %d: %s", i, uentry_rawName (current)));
1372 if (!uentry_isUndefined (current)
1373 && cstring_equal (uentry_rawName (current), k))
1384 usymtab_fetchIndex (/*@notnull@*/ usymtab s, int i)
1386 llassert (i >= 0 && i < s->nentries);
1387 return (s->entries[i]);
1391 usymtab_getTypeId (cstring k) /*@globals globtab@*/
1393 usymId uid = usymtab_getIndex (globtab, k);
1395 if (uid == NOT_FOUND) return USYMIDINVALID;
1397 if (!(uentry_isDatatype (usymtab_getTypeEntry (uid)))) {
1398 return USYMIDINVALID;
1404 /*@dependent@*/ uentry
1405 usymtab_lookupStructTag (cstring k)
1407 cstring sname = makeStruct (k);
1408 uentry ue = usymtab_lookupGlob (sname);
1410 cstring_free (sname);
1414 /*@dependent@*/ uentry
1415 usymtab_lookupUnionTag (cstring k)
1417 cstring uname = makeUnion (k);
1418 uentry res = usymtab_lookupGlob (uname);
1420 cstring_free (uname);
1424 /*@dependent@*/ uentry
1425 usymtab_lookupEnumTag (cstring k)
1427 cstring ename = makeEnum (k);
1428 uentry res = usymtab_lookupGlob (ename);
1430 cstring_free (ename);
1435 usymtab_getId (cstring k) /*@globals globtab@*/
1437 usymId uid = usymtab_getIndex (globtab, k);
1440 if (uid == NOT_FOUND)
1442 return USYMIDINVALID;
1445 ue = usymtab_getGlobalEntry (uid);
1447 if (uentry_isPriv (ue))
1449 return USYMIDINVALID;
1455 static /*@exposed@*/ uentry
1456 usymtab_getEntryAux (/*@notnull@*/ usymtab s, usymId uid)
1458 llassert (uid != USYMIDINVALID);
1460 if (uid < 0 || uid >= s->nentries)
1462 llcontbug (message ("usymtab_getEntry: out of range: level = %d [%d]",
1464 return uentry_undefined;
1467 llassertprint (uentry_isValid (s->entries[uid]),
1468 ("entry undefined: %d", uid));
1470 return s->entries[uid];
1473 /*@dependent@*/ /*@observer@*/ uentry
1474 usymtab_getGlobalEntry (usymId uid)
1475 /*@globals utab, globtab@*/
1477 if (dbgfree) return (uentry_undefined);
1479 if (utab->lexlevel > paramsScope)
1481 /* need to do this the awkward way, since it could be in conditional scope */
1482 return (usymtab_lookupSafe (uentry_rawName (globtab->entries[uid])));
1486 return (globtab->entries[uid]);
1490 /*@dependent@*/ /*@exposed@*/ uentry
1491 usymtab_getTypeEntry (usymId uid)
1492 /*@globals globtab@*/
1496 if (uid >= 0 && uid < globtab->nentries)
1498 return (globtab->entries[uid]);
1502 return (uentry_undefined);
1507 llassert (uid >= 0 && uid < globtab->nentries);
1508 return (globtab->entries[uid]);
1516 /*@dependent@*/ /*@exposed@*/ uentry
1517 usymtab_getTypeEntrySafe (usymId uid)
1518 /*@globals globtab@*/
1520 if (uid < 0 || uid >= globtab->nentries)
1522 return uentry_undefined;
1525 return (globtab->entries[uid]);
1529 usymtab_isBoolType (usymId uid)
1530 /*@globals globtab@*/
1532 llassert (uid >= 0 && uid < globtab->nentries);
1534 return (cstring_equal (uentry_rawName (globtab->entries[uid]),
1535 context_getBoolName ()));
1539 usymtab_getTypeEntryName (usymId uid)
1540 /*@globals globtab@*/
1546 return (cstring_makeLiteral ("<freetype>"));
1549 ue = usymtab_getTypeEntry (uid);
1551 if (dbgload && !uentry_isValid (ue))
1553 return (message ("<missing type: %d>", uid));
1556 llassertprint (uentry_isValid (ue), ("type undefined: %d", uid));
1558 return (uentry_getName (ue));
1561 /*@unused@*/ static void
1562 usymtab_rehash (/*@notnull@*/ usymtab s)
1566 if (cstringTable_isDefined (s->htable))
1568 cstringTable_free (s->htable);
1571 s->htable = cstringTable_create (LLHASHSIZE);
1573 for (i = 0; i < s->nentries; i++)
1575 cstringTable_insert (s->htable, cstring_copy (uentry_rawName (s->entries[i])), i);
1580 ** superficial copy of usymtab
1582 ** DO copy spec entries
1585 static /*@only@*/ /*@notnull@*/ usymtab
1586 usymtab_shallowCopy (/*@notnull@*/ usymtab s) /*@*/
1588 usymtab copytab = usymtab_createRoot ();
1591 for (i = 0; i < s->nentries; i++)
1593 usymtab_addEntryBase (copytab, s->entries[i]);
1600 usymtab_shallowFree (/*@only@*/ /*@notnull@*/ usymtab s)
1602 aliasTable_free (s->aliases);
1603 refTable_free (s->reftable, s->nentries);
1605 /*@-compdestroy@*/ sfree (s); /*@=compdestroy@*/
1609 ** converts usymId from old table to sorted one
1613 usymtab_convertId (usymId uid)
1614 /*@globals oldtab, utab@*/
1620 llassert (usymtab_isDefined (oldtab));
1622 ue = usymtab_getEntryAux (oldtab, uid);
1624 llassertprint (uentry_isValid (ue), ("convertId: undefined: %d", uid));
1626 name = uentry_rawName (ue);
1628 ret = usymtab_getIndex (utab, name);
1629 llassert (ret == uid); /*! for now, no rehash! */
1630 DPRINTF (("Convert: %s [%d] -> %s [%d]",
1631 uentry_unparse (ue), uid,
1632 uentry_unparse (utab->entries[ret]), ret));
1634 llassertprint (ret != USYMIDINVALID, ("convertId: return is invalid"));
1640 usymtab_prepareDump (void)
1641 /*@globals oldtab, utab@*/
1642 /*@modifies oldtab, utab@*/
1644 llassert (usymtab_inGlobalScope ());
1645 llassert (oldtab == usymtab_undefined);
1648 DPRINTF (("Preparing to dump:"));
1649 usymtab_printAll ();
1652 oldtab = usymtab_shallowCopy (utab);
1655 ** alpha compare - make sure order is same on different platforms
1656 ** error messages appear in same order
1660 qsort (utab->entries, (size_t)utab->nentries,
1661 sizeof (*utab->entries),
1662 (int (*)(const void *, const void *)) uentry_xcomparealpha);
1664 usymtab_rehash (utab);
1668 DPRINTF (("After rehash:"));
1669 usymtab_printAll ();
1673 void usymtab_dump (FILE *fout)
1674 /*@globals utab, oldtab@*/
1677 bool neednl = FALSE;
1678 uentry lastentry = uentry_undefined;
1679 ekind lastekind = KINVALID;
1683 ** must call prepareDump first
1686 llassert (oldtab != usymtab_undefined);
1688 for (i = 0; i < utab->nentries; i++)
1690 uentry thisentry = utab->entries[i];
1691 ekind thisekind = uentry_getKind (thisentry);
1693 if (!uentry_hasRealName (thisentry))
1695 llassert (uentry_isGlobalMarker (thisentry));
1699 check (fputc ('\n', fout) == (int) '\n');
1702 fprintf (fout, "*%d (GlobalMarker)\n", KGLOBALMARKER);
1703 lastekind = KINVALID;
1709 if (thisekind != lastekind)
1713 check (fputc ('\n', fout) == (int) '\n');
1717 lastentry = uentry_undefined;
1718 fprintf (fout, "*%d (%s)\n", ekind_toInt (thisekind),
1719 cstring_toCharsSafe (ekind_capName (thisekind)));
1720 lastekind = thisekind;
1725 ** evans - 2001-02-18 - added the - 48 fudge factor...
1726 ** extra characters dumped, but I haven't counded them carefully...
1729 if (uentry_isInvalid (lastentry) || !uentry_equiv (lastentry, thisentry)
1730 || (linelen > (MAX_DUMP_LINE_LENGTH - (2 * MAX_NAME_LENGTH) - 48)))
1734 DPRINTF (("Dumping entry: %d", i));
1735 cdump = message ("^%d %q", i, uentry_dump (thisentry));
1736 /* was: cdump = uentry_dump (thisentry)); */
1738 lastentry = thisentry;
1741 check (fputc ('\n', fout) == (int) '\n');
1745 linelen += cstring_length (cdump);
1747 /* no new line here! */
1748 if (cstring_length (cdump) > 0)
1750 check (fputs (cstring_toCharsSafe (cdump), fout) != EOF);
1753 cstring_free (cdump);
1758 cstring cdump = uentry_rawName (thisentry);
1759 DPRINTF (("Raw name: %s", cdump));
1760 linelen += (cstring_length (cdump) + 1);
1761 fprintf (fout, "#%s", cstring_toCharsSafe (cdump));
1768 check (fputc ('\n', fout) == (int) '\n');
1771 lastekind = KINVALID;
1773 fprintf(fout, ";; Library constraints\n");
1774 for (i = 0; i < utab->nentries; i++)
1776 uentry thisentry = utab->entries[i];
1778 if (uentry_isFunction (thisentry) )
1780 constraintList preconditions;
1781 constraintList postconditions;
1783 preconditions = uentry_getFcnPreconditions (thisentry);
1784 postconditions = uentry_getFcnPostconditions (thisentry);
1786 if ( constraintList_isDefined(preconditions) ||
1787 constraintList_isDefined(postconditions) )
1789 fprintf(fout,"%s\n", cstring_toCharsSafe (uentry_rawName(thisentry) ) );
1790 if (constraintList_isDefined(preconditions) )
1792 fprintf(fout,"pre:\n");
1793 constraintList_dump(preconditions, fout);
1794 fprintf (fout, ";; end precondition constraints\n" );
1795 constraintList_free(preconditions);
1799 fprintf(fout,"pre:EMPTY\n");
1801 if (constraintList_isDefined(postconditions) )
1803 fprintf(fout,"post:\n");
1804 constraintList_dump(postconditions, fout);
1805 fprintf (fout, ";; end precondition constraints\n" );
1806 constraintList_free(postconditions);
1810 fprintf(fout,"post:EMPTY\n");
1821 void usymtab_load (FILE *f)
1822 /*@globals utab, globtab@*/
1823 /*@modifies utab, *f@*/
1825 char *s = mstring_create (MAX_DUMP_LINE_LENGTH);
1827 ekind kind = KINVALID;
1828 fileloc loc = g_currentloc;
1834 llassert (utab == globtab);
1835 llassert (utab->nentries == 0);
1837 while (((s = reader_readLine (f, s, MAX_DUMP_LINE_LENGTH)) != NULL)
1840 /* ignore ; comments */ ;
1843 while (s != NULL && *s != ';')
1851 ek = reader_getInt (&s);
1853 if (ek == KGLOBALMARKER)
1855 uentry lue = uentry_makeGlobalMarker ();
1856 DPRINTF (("Adding global marker: %s", uentry_unparseFull (lue)));
1857 usymtab_addEntryAlways (utab, lue);
1863 kind = ekind_fromInt (ek);
1871 (cstring_makeLiteral
1872 ("Library is in obsolete format. Use lclint +whichlib "
1873 "to see which library is being loaded."));
1876 if (reader_optCheckChar (&s, '^'))
1878 index = reader_getInt (&s);
1885 llassert (kind != KINVALID);
1886 ue = uentry_undump (kind, loc, &s);
1888 llassert (utab->nentries == index || index == -1);
1890 if (uentry_isValid (ue))
1892 int lastindex = utab->nentries;
1893 ue = usymtab_addEntryAlways (utab, ue);
1894 if (utab->nentries != lastindex + 1)
1896 DPRINTF (("No add: %s", uentry_unparseFull (ue)));
1904 ** now, any other names are the same uentry
1907 while (*(s++) == '#')
1909 cstring name = cstring_fromCharsO (reader_getWord (&s));
1910 uentry nue = uentry_nameCopy (name, ue);
1912 DPRINTF (("Name copy: %s", uentry_unparseFull (nue)));
1916 usymtab_addEntryAlways (utab, nue);
1919 while ((c = *s) != '\0' && (c !='\n'))
1921 if (c != ' ' || c != '\t')
1923 llbuglit ("Junk in load file");
1931 s = reader_readLine (f, os, MAX_DUMP_LINE_LENGTH);
1936 to handle reading of buffer overflow related constraints
1938 while (fgets (s, MAX_DUMP_LINE_LENGTH, f) != NULL
1941 ; /* ignore ;-comments */
1944 while (s != NULL && *s != ';')
1946 constraintList preconditions;
1947 constraintList postconditions;
1949 cstring name = cstring_fromChars(reader_getWord(&s) );
1951 ue = usymtab_lookup ( name );
1955 preconditions = constraintList_undefined;
1956 postconditions = constraintList_undefined;
1958 if (!uentry_isValid(ue) )
1960 llfatalbug ((message("Invalid uentry for %s library file may be corrupted", cstring_fromChars(s) ) ));
1962 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
1964 temp = cstring_fromChars (reader_getWord(&s) );
1966 if (cstring_compareLit (temp,"pre:") == 0 )
1968 preconditions = constraintList_undump (f);
1972 if (cstring_compareLit (temp,"pre:EMPTY") != 0 )
1973 llfatalbug ((message("Error reading library file pre:EMPTY expected but got %s", temp ) ));
1978 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
1980 temp = cstring_fromChars(reader_getWord(&s) );
1981 if (cstring_compareLit (temp, "post:") == 0 )
1983 postconditions = constraintList_undump (f);
1987 if (cstring_compareLit (temp, "post:EMPTY") != 0 )
1988 llfatalbug ((message("Error reading library file post:EMPTY expected but got %s", temp ) ));
1991 cstring_free (temp);
1993 uentry_setPreconditions (ue, functionConstraint_createBufferConstraint (preconditions));
1994 uentry_setPostconditions (ue, functionConstraint_createBufferConstraint (postconditions));
1996 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
2004 ** file scope for static variables
2008 usymtab_enterFile ()
2009 /*@globals utab, globtab, filetab@*/
2010 /*@modifies filetab@*/
2012 llassert (utab == globtab);
2017 usymtab_entries (globtab, ue)
2019 if (sRef_hasDerived (uentry_getSref (ue)))
2021 fprintf (g_msgstream, "Derived Global: %s\n", uentry_unparse (ue));
2022 fprintf (g_msgstream, "sRef: %s\n", sRef_unparseFull (ue->sref));
2024 } end_usymtab_entries ;
2028 usymtab_enterScope ();
2034 /*@globals utab, filetab@*/
2035 /*@modifies filetab, utab@*/
2038 llassert (utab->lexlevel == 1);
2040 usymtab_exitScope (exprNode_undefined);
2045 usymtab_enterScope ()
2046 /*@globals utab, globtab, filetab@*/
2049 usymtab t = usymtab_create (US_NORMAL, utab, TRUE);
2051 /* unconditional scope: optimize to avoid copy */
2052 t->aliases = aliasTable_copy (utab->aliases);
2055 llassert (usymtab_isDefined (t->env));
2057 if (t->env->lexlevel == paramsScope && context_inFunctionLike ())
2059 noshadowerror = TRUE;
2060 usymtab_handleParams ();
2061 noshadowerror = FALSE;
2066 ** setup external references:
2067 ** o only, unique params alias external args
2068 ** o other params may alias anything of their type
2072 usymtab_handleParams (void)
2073 /*@globals utab, globtab, filetab@*/
2074 /*@modifies utab, globtab@*/
2076 usymtab ptab = utab->env;
2077 uentry fcn = context_getHeader ();
2079 usymtab_entries (ptab, param)
2083 if (!uentry_isYield (param))
2086 sRef pref = uentry_getSref (param);
2088 /* Could be a global. */
2090 if (uentry_isAnyParam (param))
2092 ue = uentry_makeVariable (fixParamName (uentry_rawName (param)),
2093 uentry_getType (param),
2094 fileloc_copy (uentry_whereDeclared (param)),
2097 uentry_copyState (ue, param);
2098 uentry_setRefParam (ue);
2100 ue = usymtab_supEntrySrefReturn (ue);
2102 /* must be after supercede! */
2104 if (!sRef_stateKnown (pref))
2106 uentry_setDefState (ue, SS_DEFINED);
2107 uentry_setDefState (param, SS_DEFINED);
2111 if (sRef_isStateSpecial (pref))
2113 uentry_setDefState (ue, SS_ALLOCATED);
2117 uentry_setDefState (ue, sRef_getDefState (pref));
2121 uref = uentry_getSref (ue);
2123 if (sRef_isStack (uref))
2125 alkind pkind = sRef_getAliasKind (pref);
2127 if (alkind_isKnown (pkind) && !alkind_isLocal (pkind)
2128 && !alkind_isStack (pkind))
2130 sRef_setAliasKind (uref, pkind, fileloc_undefined);
2131 sRef_setOrigAliasKind (uref, pkind);
2135 sRef_setAliasKind (uref, AK_IMPTEMP, fileloc_undefined);
2136 sRef_setOrigAliasKind (uref, AK_IMPTEMP);
2138 if (uentry_isOut (param))
2144 sRef_setDefined (uref, fileloc_undefined);
2150 usymtab_addMustAlias (uref, pref);
2152 if (!(uentry_isOnly (param) || uentry_isUnique (param)))
2155 ** This is needed for detecting possibly aliased parameters.
2158 sRef s = sRef_makeExternal (uref);
2159 usymtab_addMustAlias (uref, s);
2162 if (sRef_isKillRef (pref))
2164 sRef_setAliasKind (uref, AK_NEWREF, fileloc_undefined);
2165 sRef_setOrigAliasKind (uref, AK_KILLREF);
2167 else if (sRef_isRefCounted (uref))
2169 sRef_setOrigAliasKind (uref, AK_REFCOUNTED);
2174 sRef_setOrigAliasKind (uref, AK_LOCAL);
2181 } end_usymtab_entries;
2184 if (uentry_hasStateClauseList (fcn))
2186 stateClauseList clauses = uentry_getStateClauseList (fcn);
2188 stateClauseList_preElements (clauses, cl)
2190 fileloc loc = stateClause_loc (cl);
2191 sRefSet osrs = sRefSet_undefined;
2194 if (stateClause_isGlobal (cl))
2196 DPRINTF (("Global Marker: %s",
2197 sRef_unparseFull (usymtab_lookupGlobalMarker ())));
2198 llassert (sRef_isGlobalMarker (usymtab_lookupGlobalMarker ()));
2199 srs = sRefSet_single (usymtab_lookupGlobalMarker ());
2204 srs = stateClause_getRefs (cl);
2207 sRefSet_elements (srs, el)
2209 sRef base = sRef_getRootBase (el);
2210 sRef sb = sRef_updateSref (el);
2212 if (sRef_isResult (base))
2214 ; /* nothing to do before */
2216 else if (sRef_isParam (base) || sRef_isGlobalMarker (base))
2218 if (stateClause_setsMetaState (cl))
2220 /* copied from exprNode.c:3040 */
2221 qual ql = stateClause_getMetaQual (cl);
2222 annotationInfo ainfo = qual_getAnnotationInfo (ql);
2223 metaStateInfo minfo = annotationInfo_getState (ainfo);
2224 cstring key = metaStateInfo_getName (minfo);
2225 int mvalue = annotationInfo_getValue (ainfo);
2227 DPRINTF (("Sets meta state! %s", stateClause_unparse (cl)));
2229 if (sRef_isResult (base))
2235 sRef_setMetaStateValueComplete (sb, key, mvalue, loc);
2240 sRefMod modf = stateClause_getEntryFunction (cl);
2244 sRefSet aliases = usymtab_allAliases (sb);
2248 sRefSet_elements (aliases, sr)
2251 } end_sRefSet_elements ;
2253 sRefSet_free (aliases);
2259 if (sRef_isValid (base))
2261 DPRINTF (("Base: %s", sRef_unparseFull (base)));
2265 } end_sRefSet_elements ;
2266 } end_stateClauseList_preElements ;
2271 usymtab_enterFunctionScope (uentry fcn)
2272 /*@globals utab, filetab, globtab@*/
2275 usymtab t = usymtab_create (US_NORMAL, utab, TRUE);
2277 if (utab->lexlevel != fileScope)
2279 if (utab->lexlevel > fileScope)
2281 llparseerror (cstring_makeLiteral ("New function scope inside function."));
2283 while (utab->lexlevel > fileScope)
2285 /*@i@*/ utab = usymtab_dropEnv (utab);
2292 llfatalbug (cstring_makeLiteral ("New function not inside file."));
2294 /*@-branchstate@*/ } /*@=branchstate@*/
2298 DPRINTF (("Globs: %s", globSet_unparse (uentry_getGlobs (fcn))));
2300 globSet_allElements (uentry_getGlobs (fcn), el)
2302 DPRINTF (("Here we go: %s", sRef_unparseFull (el)));
2304 if (sRef_isUndefGlob (el))
2306 int index = sRef_getScopeIndex (el);
2307 sRef sr = sRef_updateSref (el);
2308 fileloc loc = uentry_whereEarliest (fcn);
2310 DPRINTF (("update: %s", sRef_unparseFull (sr)));
2311 DPRINTF (("Undef!"));
2312 if (sRef_isFileStatic (el))
2314 ctype ct = sRef_getType (el);
2317 llassert (usymtab_isDefined (filetab));
2319 ue = usymtab_fetchIndex (filetab, index);
2321 if (ctype_isRealArray (ct) || ctype_isRealSU (ct))
2323 sRef_setAllocated (sr, loc);
2327 sRef_setUndefined (sr, loc);
2332 uentry ue = globtab->entries[index];
2333 ctype ct = uentry_getType (ue);
2335 if (ctype_isArray (ct) || ctype_isSU (ct))
2337 sRef_setAllocated (sr, loc);
2341 sRef_setUndefined (sr, loc);
2345 else if (sRef_isAllocated (el))
2347 sRef sr = sRef_updateSref (el);
2348 fileloc loc = uentry_whereEarliest (fcn);
2350 sRef_setAllocated (sr, loc);
2352 else if (sRef_isPartial (el))
2354 sRef sr = sRef_updateSref (el);
2355 fileloc loc = uentry_whereEarliest (fcn);
2357 sRef_setPartial (sr, loc);
2362 sRef sr = sRef_updateSref (el);
2363 fileloc loc = uentry_whereEarliest (fcn);
2365 sRef_setDefined (sr, loc);
2369 /* shouldn't need to do anything! */
2371 } end_globSet_allElements;
2373 DPRINTF (("Globs after: %s", globSet_unparse (uentry_getGlobs (fcn))));
2377 usymtab_caseBranch (void)
2380 usymtab t = usymtab_create (US_CBRANCH, utab, FALSE);
2385 usymtab_switchBranch (/*@unused@*/ exprNode s)
2388 usymtab t = usymtab_create (US_SWITCH, utab, FALSE);
2390 t->aliases = aliasTable_copy (utab->aliases);
2395 usymtab_trueBranch (/*@only@*/ guardSet guards)
2398 usymtab t = usymtab_create (US_TBRANCH, utab, FALSE);
2401 ** not true! (could be in a macro)
2403 ** llassertprint (utab->lexlevel > paramsScope,
2404 ** ("not in scope: %s", usymtab_unparseLocal ()));
2408 guardSet_free (t->guards);
2411 aliasTable_free (t->aliases);
2412 t->aliases = aliasTable_copy (utab->aliases);
2420 ** { int a; if (...) a = 3; < a may be undefined here!
2425 usymtab_popTrueBranch (exprNode pred, exprNode expr, clause cl) /*@modifies utab@*/
2428 ** add a false branch
2429 ** (could be done more efficiently as a special case, but
2430 ** it is better to only maintain one version of the code)
2433 if (utab->kind != US_TBRANCH
2434 && context_inIterDef ())
2436 usymtab_exitScope (expr);
2440 DPRINTF (("pop true branch.."));
2441 usymtab_altBranch (guardSet_invert (exprNode_getGuards (pred)));
2442 usymtab_popBranches (pred, expr, exprNode_undefined, TRUE, cl);
2447 usymtab_popCaseBranch () /*@modifies utab@*/
2449 llassert (utab->kind == US_CBRANCH);
2450 usymtab_quietPlainExitScope ();
2454 usymtab_popTrueExecBranch (exprNode pred, exprNode expr, clause cl)
2457 ** add a false branch that must return --- that is,
2458 ** the true branch is always executed!
2461 usymtab_altBranch (guardSet_invert (exprNode_getGuards (pred)));
2462 usymtab_popBranches (pred, expr, exprNode_makeMustExit (), FALSE, cl);
2466 usymtab_popOrBranch (exprNode pred, exprNode expr)
2470 usymtab env = utab->env;
2471 usymtab otab = utab;
2474 llassert (env != NULL);
2476 if (exprNode_isError (expr))
2482 mustReturn = exprNode_mustEscape (expr);
2486 llassert (utab->kind == US_TBRANCH);
2489 ** merge each entry in table with its original
2490 ** unless execution cannot continue after this branch
2493 for (i = 0; i < utab->nentries; i++)
2495 uentry current = utab->entries[i];
2496 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
2498 uentry_mergeState (old, current, exprNode_loc (expr),
2499 mustReturn, FALSE, TRUE, ORCLAUSE);
2505 env->guards = guardSet_levelUnionFree (env->guards,
2506 guardSet_invert (exprNode_getGuards (pred)),
2511 env->aliases = aliasTable_levelUnion (env->aliases, otab->aliases, env->lexlevel);
2514 /* env is now utab */
2515 usymtab_quietPlainExitScope ();
2519 ** case syntax in C is very unrestricted. This is unfortunate.
2521 ** A switch case is ended either by a new case or default, or
2522 ** a close } that may close the switch or some other control
2527 usymtab_newCase (/*@unused@*/ exprNode pred, exprNode last)
2530 bool mustBreak = usymtab_mustBreak (utab);
2531 bool mustReturn = usymtab_mustEscape (utab);
2532 usymtab stab = utab;
2535 ** Find last case (or outer switch)
2538 while (stab->kind != US_CBRANCH && stab->kind != US_SWITCH)
2541 llassert (stab != GLOBAL_ENV);
2546 while (stab->kind == US_CBRANCH)
2549 llassert (stab != GLOBAL_ENV);
2553 ** if its a fall through case, merge in outside entries and last case.
2558 ** case 1: x = 3; <fall through>
2559 ** case 2: << x may not be defined
2563 if (!mustBreak && !mustReturn && utab->kind == US_CBRANCH)
2565 llassert (stab->kind == US_SWITCH || stab->kind == US_NORMAL);
2567 usymtab_entries (utab, ue) /* but, keep track of used variables */
2569 uentry old = usymtab_lookupAux (stab, uentry_rawName (ue));
2571 llassert (uentry_isValid (old));
2573 /* modifies ue, not old */
2575 uentry_mergeState (ue, old, exprNode_loc (last),
2576 FALSE, FALSE, TRUE, CASECLAUSE);
2577 } end_usymtab_entries;
2579 utab->aliases = aliasTable_levelUnion (utab->aliases,
2580 stab->aliases, utab->lexlevel);
2583 ** No need for a new branch.
2590 usymtab_caseBranch ();
2591 /*@-mustfree@*/ /*< utab->aliases >*/
2592 utab->aliases = aliasTable_copy (stab->aliases);
2600 ** for && (both pred and expr are executed)
2604 usymtab_popAndBranch (exprNode pred, /*@unused@*/ exprNode expr)
2607 usymtab env = utab->env;
2611 llassert (utab->kind == US_TBRANCH);
2614 ** merge each entry in table with its original
2615 ** unless execution cannot continue after this branch
2618 for (i = 0; i < utab->nentries; i++)
2620 uentry current = utab->entries[i];
2621 sRef tref = uentry_getSref (current);
2622 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
2623 sRef oref = uentry_getSref (old);
2625 /* note that is current is in a nested branch,
2626 it may create a "new" old entry. */
2628 llassert (uentry_isValid (old));
2629 uentry_mergeState (old, current, exprNode_loc (expr),
2630 FALSE, FALSE, TRUE, ANDCLAUSE);
2633 ** if is it defined by the second clause, then it should be defined.
2636 if (sRef_isAnyDefined (tref)
2637 && (sRef_isAllocated (oref) || sRef_isStateUndefined (oref)))
2639 sRef_setDefined (oref, g_currentloc);
2643 utab->guards = guardSet_levelUnionFree (utab->guards,
2644 guardSet_invert (exprNode_getGuards (pred)),
2646 utab->aliases = aliasTable_levelUnion (utab->aliases, otab->aliases, utab->lexlevel);
2648 usymtab_quietPlainExitScope ();
2653 ** Stack should be [ US_CBRANCH+ US_SWITCH ]
2654 ** Only branches which do not return (except possibly the last branch) are included.
2656 ** Conditionally merge state from all CBRANCHes.
2658 ** If allpaths is TRUE, then all possible executions go through some switch
2659 ** case, and the original scope is not merged.
2663 usymtab_exitSwitch (/*@unused@*/ exprNode sw, bool allpaths)
2666 usymtab ttab = utab;
2667 usymtab stab = ttab;
2668 usymtab ltab = ttab;
2669 bool lastMustReturn = usymtab_mustEscape (utab);
2673 while (stab->kind == US_CBRANCH)
2676 llassert (stab != GLOBAL_ENV);
2679 while (stab->kind == US_NORMAL)
2682 llassert (stab != GLOBAL_ENV);
2685 llassert (stab->kind == US_SWITCH);
2687 /* go to the scope outside the switch (US_SWITCH is just a marker! */
2689 llassert (stab != GLOBAL_ENV);
2693 llassert (usymtab_isDefined (ttab));
2695 if (ttab->kind == US_CBRANCH)
2697 /* was quietPlainExitScope --- but, can't free it yet! */
2699 llassert (utab != GLOBAL_ENV);
2701 while (ttab->kind == US_CBRANCH)
2704 ** (from popTrueBranch)
2707 bool mustReturn = usymtab_mustEscape (ttab);
2708 bool mustBreak = usymtab_mustBreak (ttab);
2710 usymtab_entries (ttab, current)
2712 uentry old = /*@-compmempass@*/ usymtab_lookupAux (ltab, uentry_rawName (current));
2716 ** note that is this is in a nested branch,
2717 ** it may create a "new" old entry.
2720 if (uentry_isValid (old))
2724 uentry_mergeUses (current, old);
2725 uentry_setState (old, current);
2729 uentry_mergeState (old, current, exprNode_loc (sw),
2730 mustReturn, FALSE, TRUE, SWITCHCLAUSE);
2737 } end_usymtab_entries;
2740 ** if entry is not in symbol table for this case, merge with pre-switch
2744 if (!mustReturn && !mustBreak)
2746 usymtab_entries (stab, current)
2748 if (usymtab_getIndex (ttab, uentry_rawName (current)) == NOT_FOUND)
2750 uentry old = /*@-compmempass@*/
2751 usymtab_lookupAux (ltab, uentry_rawName (current));
2754 llassert (uentry_isValid (old));
2755 uentry_mergeState (old, current, exprNode_loc (sw),
2756 FALSE, FALSE, TRUE, SWITCHCLAUSE);
2758 } end_usymtab_entries;
2761 ltab->env = ttab->env;
2765 ** Suprious error, becuase of environments.
2768 /*@i1@*/ utab = ltab;
2770 lastMustReturn = FALSE;
2777 ** now, there is one US_CBRANCH. Merge this with the stab.
2781 for (i = 0; i < ltab->nentries; i++)
2783 uentry current = ltab->entries[i];
2784 uentry old = usymtab_lookupAux (stab, uentry_rawName (current));
2786 /* note that is this is in a nested branch,
2787 it may create a "new" old entry. */
2790 if (uentry_isValid (old))
2794 uentry_mergeUses (current, old);
2795 uentry_setState (old, current);
2799 uentry_mergeState (old, current, exprNode_loc (sw),
2800 FALSE, FALSE, TRUE, SWITCHCLAUSE);
2814 ** switch may or may not be followed by a new scope
2817 if (utab->kind == US_SWITCH)
2819 usymtab_quietPlainExitScope ();
2823 usymtab_quietPlainExitScope ();
2824 llassert (utab->kind == US_SWITCH);
2825 usymtab_quietPlainExitScope ();
2831 updateNullState (sRef el, /*@notnull@*/ usymtab ttab,
2832 /*@notnull@*/ usymtab ftab, bool trueGuard)
2834 sRef base = sRef_getRootBase (el);
2835 int level = sRef_lexLevel (base);
2837 if (sRef_isCvar (base))
2839 usymId index = sRef_getScopeIndex (base);
2840 uentry ue = usymtab_getRefTab (ttab, level, index);
2842 if (!uentry_isLset (ue))
2844 sRef sr = uentry_getSref (ue);
2848 sRef_setDerivNullState (sr, el, NS_NOTNULL);
2852 if (!guardSet_isGuarded (ttab->guards, el)
2853 && !sRef_isNotNull (sr))
2855 sRef_setDerivNullState (sr, el, NS_DEFNULL);
2863 ue = usymtab_getRefTab (ftab, level, index);
2865 if (!uentry_isLset (ue))
2867 sRef sr = uentry_getSref (ue);
2870 if (!trueGuard) /* yikes! forgot the ! */
2872 sRef_setDerivNullState (sr, el, NS_NOTNULL);
2877 if (!guardSet_isGuarded (ftab->guards, el)
2878 && !sRef_isNotNull (sr))
2880 sRef_setDerivNullState (sr, el, NS_DEFNULL);
2892 usymtab_popBranches (exprNode pred, exprNode tbranch, exprNode fbranch,
2893 bool isOpt, clause cl)
2897 usymtab ftab = utab;
2898 usymtab ttab = utab->env;
2902 guardSet guards = exprNode_getGuards (pred);
2903 sRefSet tguards = guardSet_getTrueGuards (guards);
2904 sRefSet fguards = guardSet_getFalseGuards (guards);
2905 bool mustReturnT = exprNode_mustEscape (tbranch);
2906 bool mustReturnF = exprNode_mustEscape (fbranch);
2908 DPRINTF (("Pop branches: %s [mustreturn: %s/%s]", exprNode_unparse (pred),
2909 bool_unparse (mustReturnT),
2910 bool_unparse (mustReturnF)));
2912 if (exprNode_isDefined (fbranch))
2914 loc = exprNode_loc (fbranch);
2918 loc = exprNode_loc (tbranch);
2921 llassert (usymtab_isDefined (ttab));
2925 llassert (usymtab_isDefined (env));
2926 llassert (ftab->kind == US_FBRANCH);
2927 llassert (ttab->kind == US_TBRANCH);
2930 ** For each element that is true guarded (i.e., if (x != NULL))
2931 ** make x = null in false branch,
2932 ** and x = notnull in true branch.
2933 ** unless x was set locally in that branch.
2934 ** For each element that is false guarded (x == NULL)
2935 ** make x = null in true, notnull in false.
2937 ** For each element that is either guarded (pred(x))
2941 sRefSet_allElements (tguards, el)
2943 updateNullState (el, ttab, ftab, TRUE);
2944 } end_sRefSet_allElements;
2946 sRefSet_allElements (fguards, el)
2948 updateNullState (el, ttab, ftab, FALSE);
2949 } end_sRefSet_allElements;
2953 ** if an entry is in both true and false, merge the entries,
2954 ** then replace original with new state.
2956 ** if an entry is in one table, merge it with the original.
2959 DPRINTF (("ftab: %d", ftab->nentries));
2961 for (i = 0; i < ftab->nentries; i++)
2963 uentry fthis = ftab->entries[i];
2964 uentry old = usymtab_lookupAux (env, uentry_rawName (fthis));
2965 int tindex = usymtab_getIndex (ttab, uentry_rawName (fthis));
2967 DPRINTF (("Entry: %s / %s", uentry_unparseFull (fthis), uentry_unparseFull (old)));
2969 if (uentry_isUndefined (old))
2971 /* possible entry was added as an undefined id */
2972 DPRINTF (("Undefined! %s", uentry_rawName (fthis)));
2976 if (tindex != NOT_FOUND)
2978 uentry tthis = ttab->entries[tindex];
2980 /* note that is this is in a nested branch,
2981 it may create a "new" old entry. */
2987 uentry_mergeState (fthis, tthis, loc,
2988 mustReturnT, FALSE, FALSE, cl);
2992 uentry_mergeUses (fthis, tthis);
2995 uentry_setState (old, fthis);
3002 uentry_setState (old, tthis);
3003 uentry_mergeState (old, fthis, loc, mustReturnF,
3007 ttab->entries[tindex] = uentry_undefined;
3008 uentry_free (tthis);
3012 uentry_mergeState (old, fthis, loc, mustReturnF, TRUE, FALSE, cl);
3016 for (i = 0; i < ttab->nentries; i++)
3018 uentry current = ttab->entries[i];
3020 DPRINTF (("ttab: %s", uentry_unparseFull (current)));
3022 if (!uentry_isUndefined (current))
3024 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
3026 DPRINTF (("Old: %s", uentry_unparseFull (old)));
3028 if (uentry_isUndefined (old))
3030 llcontbug (message ("Undefined entry: %s", uentry_rawName (current)));
3036 uentry_mergeUses (current, old);
3037 uentry_setState (old, current);
3042 ** Assumes false branch is a fall-through if
3043 ** fbranch is not defined. This is true, unless
3044 ** where was some greivous error in processing
3045 ** the else branch of an if-then, in which case
3046 ** this is probably the right thing to do anyway.
3049 uentry_mergeState (old, current, loc, mustReturnT,
3053 DPRINTF (("==> %s", uentry_unparseFull (old)));
3058 ** Plain levelUnion doesn't work, since we need to use the sRef's in env->aliases
3059 ** if they are present.
3062 llassert (NOALIAS (env->aliases, ttab->aliases));
3063 llassert (NOALIAS (env->aliases, ftab->aliases));
3065 aliasTable_free (env->aliases);
3067 env->aliases = aliasTable_levelUnionNew (ttab->aliases,
3068 ftab->aliases, env->lexlevel);
3070 aliasTable_fixSrefs (env->aliases);
3072 DPRINTF (("Aliases: %s", aliasTable_unparse (env->aliases)));
3074 /* exit true and false scopes */
3075 usymtab_quietPlainExitScope ();
3076 usymtab_quietPlainExitScope ();
3080 utab->guards = guardSet_levelUnionFree
3082 guardSet_invert (exprNode_getGuards (pred)),
3088 utab->guards = guardSet_levelUnion (utab->guards,
3089 exprNode_getGuards (pred),
3093 DPRINTF (("Here."));
3096 static void usymtab_fixCases (void) /*@modifies utab@*/ {
3097 while (utab->kind == US_CBRANCH)
3099 usymtab_quietPlainExitScope ();
3102 llassert (utab->kind != US_CBRANCH);
3106 usymtab_altBranch (/*@only@*/ guardSet guards)
3110 usymtab parent = utab->env;
3112 t = usymtab_create (US_FBRANCH, utab, FALSE);
3115 ** If we are in a case, need to close it. The C syntax
3116 ** is very liberal, so this kludge is necessary.
3119 usymtab_fixCases ();
3121 DPRINTF (("Current kind: %s", usymtab_unparseStack ()));
3123 llassert (utab->kind == US_TBRANCH);
3124 llassert (parent != GLOBAL_ENV);
3126 guardSet_free (t->guards);
3129 aliasTable_free (t->aliases);
3130 t->aliases = aliasTable_copy (parent->aliases);
3136 usymtab_allDefined (void)
3137 /*@globals utab, globtab@*/
3141 llassert (utab == globtab);
3143 for (i = 0; i < utab->nentries; i++)
3145 uentry e = utab->entries[i];
3147 if (uentry_isPriv (e))
3149 ; /* no need to define it */
3153 if (context_getFlag (FLG_SPECUNDECL))
3155 fileloc sloc = uentry_whereSpecified (e);
3156 fileloc dloc = uentry_whereDeclared (e);
3158 if (fileloc_isDefined (sloc)
3159 && !uentry_isFakeTag (e)
3160 && !fileloc_isDefined (dloc))
3164 message ("%s %q specified but not declared",
3165 ekind_capName (uentry_getKind (e)),
3166 uentry_getName (e)),
3171 if (!uentry_isCodeDefined (e))
3173 fileloc dloc = uentry_whereDeclared (e);
3175 if (fileloc_isLib (dloc) || fileloc_isXHFile (dloc))
3179 else if (fileloc_isDefined (dloc))
3181 if (!uentry_isAnyTag (e))
3183 if (fileloc_isUser (dloc))
3187 message ("%s %q declared but not defined",
3188 ekind_capName (uentry_getKind (e)),
3189 uentry_getName (e)),
3191 DPRINTF (("decl: %s", uentry_unparseFull (e)));
3197 fileloc sloc = uentry_whereSpecified (e);
3199 if (fileloc_isDefined (sloc)
3200 && !fileloc_isImport (sloc)
3201 && !fileloc_isLib (sloc)
3202 && !fileloc_isPreproc (sloc)
3203 && !uentry_isFakeTag (e))
3205 if (uentry_isVariable (e) || uentry_isFunction (e))
3209 message ("%s %q specified but not declared or defined",
3210 ekind_capName (uentry_getKind (e)),
3211 uentry_getName (e)),
3218 message ("%s %q specified but not defined",
3219 ekind_capName (uentry_getKind (e)),
3220 uentry_getName (e)),
3230 void usymtab_exportHeader (void)
3235 for (i = 0; i < utab->nentries; i++)
3237 uentry ce = utab->entries[i];
3239 if (!uentry_isDatatype (ce)
3240 && !uentry_isAnyTag (ce)
3241 && !uentry_isEitherConstant (ce) && !uentry_isStatic (ce)
3242 && !uentry_isExternal (ce)
3243 && !uentry_isForward (ce))
3245 fileloc fwhere = uentry_whereDeclared (ce);
3247 if (fileloc_isUndefined (fwhere)
3248 && uentry_isFunction (ce))
3250 fwhere = uentry_whereDefined (ce);
3253 if (fileloc_isDefined (fwhere)
3254 && !fileloc_isHeader (fwhere)
3255 && !fileloc_isXHFile (fwhere)
3256 && !(fileloc_isSpecialFile (fwhere)
3257 && !context_getFlag (FLG_UNUSEDSPECIAL)))
3259 if (uentry_isVariable (ce))
3262 (FLG_EXPORTHEADERVAR,
3263 message ("%s %q exported but not declared in header file",
3264 ekind_capName (uentry_getKind (ce)),
3265 uentry_getName (ce)),
3268 uentry_showDefSpecInfo (ce, fwhere);
3273 if (!uentry_isIter (ce)
3274 && !uentry_isEndIter (ce)
3275 && !uentry_isExpandedMacro (ce))
3277 if (uentry_isFunction (ce)
3278 && cstring_equalLit (uentry_rawName (ce), "main"))
3280 ; /* no error for main */
3286 message ("%s %q exported but not declared "
3288 ekind_capName (uentry_getKind (ce)),
3289 uentry_getName (ce)),
3292 uentry_showDefSpecInfo (ce, fwhere);
3302 void usymtab_exportLocal (void)
3307 for (i = 0; i < utab->nentries; i++)
3309 uentry ce = utab->entries[i];
3311 if (!uentry_isDatatype (ce) && !uentry_isAnyTag (ce)
3312 && !uentry_isEitherConstant (ce)
3313 && !uentry_isIter (ce)
3314 && !uentry_isEndIter (ce)
3315 && !uentry_isExpandedMacro (ce)
3316 && uentry_isUsed (ce))
3318 /* check static uses */
3319 filelocList fuses = uentry_getUses (ce);
3320 fileloc mod = uentry_whereDefined (ce);
3321 bool ok = filelocList_isEmpty (fuses);
3322 fileloc fwhere = uentry_whereDeclared (ce);
3324 if (fileloc_isSpecialFile (fwhere)
3325 && !context_getFlag (FLG_UNUSEDSPECIAL))
3327 ok = TRUE; /* no errors for special files */
3331 filelocList_elements (fuses, uloc)
3333 if (fileloc_isUndefined (uloc) || !fileloc_sameModule (uloc, mod))
3336 /*@innerbreak@*/ break;
3338 } end_filelocList_elements;
3345 message ("%s exported but not used outside %s: %q",
3346 ekind_capName (uentry_getKind (ce)),
3347 fileloc_getBase (mod),
3348 uentry_getName (ce)),
3351 uentry_showDefSpecInfo (ce, fwhere);
3359 usymtab_allUsed (void)
3363 bool isFileStatic = usymtab_inFileScope ();
3364 cstring last_file = cstring_undefined;
3366 for (i = 0; i < utab->nentries; i++)
3368 bool hasError = FALSE;
3369 uentry ce = utab->entries[i];
3370 fileloc fwhere = uentry_whereDeclared (ce);
3372 if (fileloc_isUndefined (fwhere))
3374 fwhere = uentry_whereDefined (ce);
3377 if (fileloc_isInvalid (fwhere)
3378 || fileloc_isLib (fwhere)
3379 || fileloc_isBuiltin (fwhere)
3380 || ((fileloc_isSpecialFile (fwhere)
3381 || fileloc_isSpecialFile (uentry_whereDefined (ce)))
3382 && !context_getFlag (FLG_UNUSEDSPECIAL)))
3386 else if (!uentry_wasUsed (ce) && !uentry_isAnyTag (ce))
3388 cstring fname = fileloc_filename (fwhere);
3390 if (cstring_isUndefined (last_file))
3394 else if (cstring_equal (fname, last_file))
3403 if (uentry_isParam (ce))
3405 if (context_inMacro ())
3407 sRef cref = uentry_getSref (ce);
3409 if (uentry_isYield (ce))
3411 ; /* no checks (for now) */
3413 else if (sRef_isSafe (cref))
3419 if (uentry_hasRealName (ce))
3422 optgenerror (FLG_MACROPARAMS,
3423 message ("Macro parameter %q not used",
3424 uentry_getName (ce)),
3431 if (cstring_equalFree (uentry_getName (ce),
3432 cstring_makeLiteral ("...")))
3438 hasError = optgenerror (FLG_PARAMUNUSED,
3439 message ("Parameter %q not used",
3440 uentry_getName (ce)),
3445 else if (uentry_isFunction (ce) || uentry_isIter (ce))
3447 if (fileloc_isUser (fwhere))
3449 hasError = optgenerror
3451 message ("%q %q declared but not used",
3453 (uentry_isIter (ce) ? "Iterator"
3454 : (isFileStatic ? "File static function" : "Function")),
3455 uentry_getName (ce)),
3459 else if (uentry_isEndIter (ce))
3461 ; /* no error (already reported for iter */
3463 else if (uentry_isEnumConstant (ce))
3465 if (fileloc_isUser (fwhere))
3467 hasError = optgenerror
3469 message ("Enum member %q not used",
3470 uentry_getName (ce)),
3474 else if (uentry_isConstant (ce))
3476 if (fileloc_isUser (fwhere))
3478 hasError = optgenerror
3480 message ("Constant %q declared but not used",
3481 uentry_getName (ce)),
3485 else if (uentry_isDatatype (ce))
3487 if (fileloc_isUser (fwhere))
3489 hasError = optgenerror
3491 message ("Type %q declared but not used",
3492 uentry_getName (ce)),
3496 else if (!uentry_isRefParam (ce) && !uentry_isExpandedMacro (ce))
3497 { /* errors for ref params will be reported in the next scope */
3498 llassertprint (uentry_isVar (ce),
3499 ("ce: %s", uentry_unparseFull (ce)));
3501 if (ctype_isFunction (uentry_getType (ce)))
3503 if (fileloc_isUser (fwhere))
3505 hasError = optgenerror
3507 message ("%q %q declared but not used",
3509 (isFileStatic ? "File static function"
3511 uentry_getName (ce)),
3517 if (fileloc_isUser (fwhere))
3521 hasError = optgenerror
3523 message ("%q %q declared but not used",
3525 (isFileStatic ? "File static variable"
3527 uentry_getName (ce)),
3537 else if ((uentry_isDatatype (ce) || uentry_isAnyTag (ce)))
3538 { /* check all fields */
3539 ctype ct = uentry_getRealType (ce);
3542 while (ctype_isAP (ct))
3544 ct = ctype_getBaseType (ct);
3547 if (ctype_isSU (ct))
3549 uentryList fields = ctype_getFields (ct);
3551 uentryList_elements (fields, field)
3553 if (!uentry_isUsed (field))
3555 if (uentry_hasName (ce))
3557 hasError |= optgenerror
3559 message ("Field %q of %s %q declared but not used",
3560 uentry_getName (field),
3561 cstring_makeLiteralTemp
3562 (ctype_isStruct (ct) ? "structure" : "union"),
3563 uentry_getName (ce)),
3564 uentry_whereEarliest (field));
3570 ** Can't report these errors for unnamed structs.
3571 ** No way to tell when there are multiple consistent
3572 ** unnamed structure types. (Could go through table
3573 ** and mark them all unused...)
3575 hasError |= optgenerror
3577 message ("Field %q of unnamed %s declared but not used",
3578 uentry_getName (field),
3579 cstring_makeLiteralTemp
3580 (ctype_isStruct (ct) ? "structure" : "union")),
3581 uentry_whereEarliest (field));
3586 uentry_setUsed (field, fileloc_undefined);
3588 } end_uentryList_elements;
3598 if (uentry_isParam (ce) && context_inMacro ())
3600 if (fileloc_isDefined (uentry_whereSpecified (ce)))
3602 uentry_showWhereSpecified (ce);
3607 uentry_showDefSpecInfo (ce, fwhere);
3610 uentry_setUsed (ce, fileloc_undefined);
3616 checkGlobalReturn (uentry glob, sRef orig)
3618 sRef sr = uentry_getSref (glob);
3620 DPRINTF (("Check global return: %s // orig: %s // sr: %s",
3621 uentry_unparseFull (glob),
3622 sRef_unparseFull (orig),
3623 sRef_unparseFull (sr)));
3625 DPRINTF (("Is killed: %s", bool_unparse (sRef_isKilledGlob (orig))));
3627 if (context_getFlag (FLG_GLOBSTATE))
3629 DPRINTF (("Is killed: %s", sRef_unparseFull (orig)));
3631 if (sRef_isKilledGlob (orig))
3633 if (sRef_isStateUndefined (sr)
3634 || sRef_isUnuseable (sr)
3635 || sRef_isStateUnknown (sr)
3636 || sRef_isDead (sr))
3642 ctype ct = ctype_realType (uentry_getType (glob));
3644 if (ctype_isVisiblySharable (ct))
3649 ("Killed global %q (type %s) not released before return",
3650 uentry_getName (glob),
3651 ctype_unparse (ct)),
3654 sRef_showStateInfo (sr);
3659 sRef_protectDerivs ();
3660 (void) checkGlobalDestroyed (sr, g_currentloc);
3661 sRef_clearProtectDerivs ();
3667 if (sRef_isStateUndefined (sr))
3669 if (optgenerror (FLG_GLOBSTATE,
3671 ("Function returns with global %q undefined",
3672 uentry_getName (glob)),
3675 sRef_showStateInfo (sr);
3680 if (sRef_isDead (sr))
3684 message ("Function returns with global %q "
3685 "referencing released storage",
3686 uentry_getName (glob)),
3689 sRef_showStateInfo (sr);
3690 sRef_setDefState (sr, SS_UNKNOWN, fileloc_undefined);
3694 if (ctype_isRealPointer (uentry_getType (glob))
3695 && sRef_possiblyNull (sr)
3696 && !uentry_possiblyNull (glob))
3700 message ("Function returns with non-null global %q "
3701 "referencing null storage",
3702 uentry_getName (glob)),
3705 sRef_showNullInfo (sr);
3710 checkGlobReturn (glob);
3718 ** remember: check alias globals
3721 void usymtab_checkFinalScope (bool isReturn)
3724 bool mustFree = context_getFlag (FLG_MUSTFREE);
3725 bool mustDefine = context_getFlag (FLG_MUSTDEFINE);
3726 /* bool mustNotAlias = context_getFlag (FLG_MUSTNOTALIAS); */
3727 sRefSet checked = sRefSet_new ();
3728 usymtab stab = utab;
3732 ** need to check all scopes out to function parameters.
3737 for (i = 0; i < stab->nentries; i++)
3739 uentry ce = stab->entries[i];
3740 sRef sr = uentry_getSref (ce);
3741 sRef rb = sRef_getRootBase (sr);
3745 ** Shouldn't check if shadow checked in deeper scope:
3750 uentry oue = usymtab_lookupQuietNoAlt (utab, uentry_observeRealName (ce));
3752 if (!uentry_sameObject (ce, oue))
3754 DPRINTF (("Skipping outer entry: %s / %s", uentry_unparseFull (ce),
3755 uentry_unparseFull (oue)));
3756 /*@i32 what if it is one an alternate branch? */
3757 /*@innercontinue@*/ continue;
3761 DPRINTF (("Here check final scope: %s", uentry_unparseFull (ce)));
3763 if (ctype_isFunction (uentry_getType (ce)))
3765 /*@innercontinue@*/ continue;
3768 if (uentry_isAnyParam (ce)
3769 || uentry_isRefParam (ce)
3770 || sRef_isFileOrGlobalScope (rb))
3772 /* Don't do the loseref check...but should check state! */
3774 else if (sRef_isDefinitelyNull (sr)
3775 || usymtab_isDefinitelyNull (sr))
3778 ** No state reference errors for definitely null references.
3783 DPRINTF (("Lose ref: %s / %s", uentry_unparse (ce),
3784 sRef_unparseFull (sr)));
3786 tvalues = sRef_getValueTable (sr);
3788 valueTable_elements (tvalues, fkey, fval) {
3789 metaStateInfo minfo;
3790 cstring msg = cstring_undefined;
3793 minfo = context_lookupMetaStateInfo (fkey);
3794 llassert (metaStateInfo_isDefined (minfo));
3796 if (stateValue_isError (fval))
3802 DPRINTF (("Check: %s / %s / %s", fkey,
3803 metaStateInfo_unparse (minfo),
3804 stateValue_unparse (fval)));
3806 minfo = context_lookupMetaStateInfo (fkey);
3808 nval = stateCombinationTable_lookupLoseReference
3809 (metaStateInfo_getTransferTable (minfo),
3810 stateValue_getValue (fval), &msg);
3812 if (cstring_isDefined (msg))
3814 /*@i32 print extra info for assignments@*/
3815 DPRINTF (("From: %s", sRef_unparseFull (sr)));
3816 DPRINTF (("Null? %s / %s",
3817 bool_unparse (sRef_isDefinitelyNull (sr)),
3818 bool_unparse (usymtab_isGuarded (sr))));
3823 ("%s loses reference %q in invalid state %q (%s)",
3824 cstring_makeLiteralTemp (isReturn ? "Return" : "Scope exit"),
3825 uentry_getName (ce),
3826 stateValue_unparseValue (fval, minfo),
3830 stateValue_show (fval, minfo);
3834 DPRINTF (("Suppressed transfer error: %s", msg));
3838 } end_valueTable_elements;
3843 DPRINTF (("Check entry: %s", uentry_unparseFull (ce)));
3845 if (!sRefSet_member (checked, sr) && !sRef_isFileOrGlobalScope (rb))
3847 if (ctype_isRealSU (uentry_getType (ce))
3848 && !uentry_isAnyParam (ce)
3849 && !uentry_isRefParam (ce)
3850 && !uentry_isStatic (ce)
3851 && !sRef_isDependent (sr)
3852 && !sRef_isOwned (sr))
3854 sRefSet als = usymtab_allAliases (sr);
3856 if (sRefSet_isEmpty (als))
3858 checkLocalDestroyed (sr, g_currentloc);
3862 /* aliased, no problem */ ;
3868 (!uentry_isStatic (ce)
3869 && ((sRef_isNewRef (sr))
3870 || (((sRef_isOnly (sr) || sRef_isFresh (sr)
3871 || sRef_isKeep (sr) || sRef_isOwned (sr))
3872 && !sRef_isDead (sr))
3873 && (!sRef_definitelyNull (sr))
3874 && (!usymtab_isDefinitelyNull (sr)))))
3876 bool hasError = TRUE;
3878 DPRINTF (("Checking: %s", sRef_unparseFull (sr)));
3881 ** If its a scope exit, check if there is an alias.
3882 ** If so, make it only. If not, there is an error.
3887 if (canLoseReference (sr, g_currentloc))
3889 DPRINTF (("Can lose!"));
3896 if (sRef_hasLastReference (sr))
3898 sRef ar = sRef_getAliasInfoRef (sr);
3903 ("Last reference %q to %s storage %qnot %q before %q",
3905 alkind_unparse (sRef_getAliasKind (sr)),
3906 sRef_unparseOpt (ar),
3907 cstring_makeLiteral (sRef_isKeep (sr)
3908 ? "transferred" : "released"),
3909 cstring_makeLiteral (isReturn
3910 ? "return" : "scope exit")),
3913 sRef_showRefLost (sr);
3916 else if (sRef_isNewRef (sr))
3921 ("%q %q not released before %q",
3923 (alkind_isKillRef (sRef_getOrigAliasKind (sr))
3924 ? "Kill reference parameter" : "New reference"),
3925 uentry_getName (ce),
3926 cstring_makeLiteral (isReturn
3927 ? "return" : "scope exit")),
3930 sRef_showAliasInfo (sr);
3935 if (ctype_isRealSU (sRef_getType (sr)))
3937 checkStructDestroyed (sr, g_currentloc);
3941 DPRINTF (("Here we are: %s", sRef_unparseFull (sr)));
3946 ("%s storage %q not %q before %q",
3947 alkind_capName (sRef_getAliasKind (sr)),
3948 uentry_getName (ce),
3949 cstring_makeLiteral (sRef_isKeep (sr)
3950 ? "transferred" : "released"),
3951 cstring_makeLiteral (isReturn
3952 ? "return" : "scope exit")),
3955 sRef_showAliasInfo (sr);
3956 DPRINTF (("Storage: %s", sRef_unparseFull (sr)));
3967 else if (mustDefine && uentry_isOut (ce))
3969 if (!ynm_toBoolStrict (sRef_isReadable (sr)))
3973 message ("Out storage %q not defined before %q",
3974 uentry_getName (ce),
3976 (isReturn ? "return" : "scope exit")),
3979 /* uentry_showWhereDeclared (ce); */
3988 ** also check state is okay
3991 if (usymtab_lexicalLevel () > functionScope
3992 && uentry_isVariable (ce)
3993 && (sRef_isLocalVar (sr)
3994 && (sRef_isDependent (sr) || sRef_isLocalState (sr))))
3996 sRefSet ab = usymtab_aliasedBy (sr);
3998 /* should do something more efficient here */
4000 if (sRefSet_isEmpty (ab))
4002 /* and no local ref */
4003 DPRINTF (("Check lose ref: %s", uentry_unparseFull (ce)));
4018 checked = sRefSet_insert (checked, sr);
4022 llassert (usymtab_isDefined (stab->env));
4024 if (usymtab_isBranch (stab))
4026 stab = usymtab_dropEnv (stab);
4033 llassert (stab != usymtab_undefined);
4034 } while (isReturn && (stab->lexlevel >= paramsScope));
4036 sRefSet_free (checked);
4040 ** all globals are appropriately defined
4041 ** all parameters are appropriately defined
4042 ** special clauses are followed
4045 if (isReturn || (utab->lexlevel == paramsScope))
4047 uentry fcn = context_getHeader ();
4048 uentryList params = context_getParams ();
4049 globSet uglobs = context_getUsedGlobs ();
4050 globSet sglobs = context_getGlobs ();
4052 if (isReturn && context_maybeSet (FLG_GLOBALIAS))
4054 aliasTable_checkGlobs (utab->aliases);
4058 ** state clauses (ensures, defines, sets, allocates, releases)
4061 if (uentry_hasStateClauseList (fcn))
4063 stateClauseList clauses = uentry_getStateClauseList (fcn);
4065 stateClauseList_elements (clauses, cl)
4067 if (stateClause_isAfter (cl) && !stateClause_isGlobal (cl))
4069 if (stateClause_setsMetaState (cl))
4071 sRefSet rfs = stateClause_getRefs (cl);
4072 qual q = stateClause_getMetaQual (cl);
4073 annotationInfo ainfo = qual_getAnnotationInfo (q);
4074 metaStateInfo minfo = annotationInfo_getState (ainfo);
4075 cstring key = metaStateInfo_getName (minfo);
4076 int mvalue = annotationInfo_getValue (ainfo);
4078 DPRINTF (("Post meta state clause: %s", stateClause_unparse (cl)));
4080 sRefSet_elements (rfs, el)
4082 sRef base = sRef_getRootBase (el);
4084 if (sRef_isResult (base))
4087 ** This is checked for return transfers.
4091 else if (sRef_isParam (base) || sRef_isGlobalMarker (base))
4093 sRef sr = sRef_updateSref (base);
4094 sr = sRef_fixBase (el, sr);
4096 if (!sRef_checkMetaStateValue (sr, key, mvalue))
4101 ("Ensures clause not satisfied%q (state is %q): %q",
4102 sRef_isGlobalMarker (sr)
4104 : message (" by %q", sRef_unparse (sr)),
4105 stateValue_unparseValue (sRef_getMetaStateValue (sr, key),
4107 stateClause_unparse (cl)),
4110 sRef_showMetaStateInfo (sr, key);
4116 if (sRef_isMeaningful (el))
4121 } end_sRefSet_elements ;
4125 /* evs - 2000 07 10 - added this */
4126 sRefTest tst = stateClause_getPostTestFunction (cl);
4127 sRefSet rfs = stateClause_getRefs (cl);
4129 sRefSet_elements (rfs, el)
4131 sRef base = sRef_getRootBase (el);
4133 if (sRef_isResult (base))
4136 ** This is checked for return transfers.
4141 else if (sRef_isParam (base))
4143 sRef sr = sRef_updateSref (base);
4144 sr = sRef_fixBase (el, sr);
4146 if (tst != NULL && !tst (sr))
4149 (stateClause_postErrorCode (cl),
4150 message ("%s storage %qcorresponds to "
4151 "storage listed in %q clause",
4152 stateClause_postErrorString (cl, sr),
4153 sRef_unparseOpt (sr),
4154 stateClause_unparseKind (cl)),
4157 sRefShower ss = stateClause_getPostTestShower (cl);
4164 DPRINTF (("Storage: %s", sRef_unparseFull (sr)));
4170 if (sRef_isMeaningful (el))
4175 } end_sRefSet_elements ;
4178 } end_stateClauseList_elements ;
4182 ** check parameters on return
4185 uentryList_elements (params, arg)
4187 if (!uentry_isElipsisMarker (arg))
4189 ctype rt = ctype_realType (uentry_getType (arg));
4191 if (ctype_isMutable (rt) || ctype_isSU (rt))
4193 uentry param = usymtab_lookupQuiet (utab, uentry_rawName (arg));
4194 DPRINTF (("Check param return: %s", uentry_unparseFull (param)));
4195 checkParamReturn (param);
4198 } end_uentryList_elements;
4200 DPRINTF (("Check global return: %s",
4201 globSet_unparse (sglobs)));
4203 globSet_allElements (sglobs, el)
4205 sRef orig = el; /*! sRef_updateSref (el); */ /*!!!*/
4206 uentry current = sRef_getUentry (el);
4208 DPRINTF (("Check global return: %s / %s", sRef_unparseFull (el),
4209 uentry_unparseFull (current)));
4211 if (uentry_isVariable (current) && !uentry_isRealFunction (current))
4213 checkGlobalReturn (current, orig);
4215 } end_globSet_allElements;
4217 globSet_allElements (uglobs, el)
4219 if (!globSet_member (sglobs, el))
4221 uentry current = sRef_getUentry (el);
4223 if (uentry_isVariable (current)
4224 && !uentry_isRealFunction (current))
4226 checkGlobalReturn (current, sRef_undefined);
4229 } end_globSet_allElements;
4234 usymtab_quietExitScope (fileloc loc)
4235 /*@globals utab, globtab, filetab; @*/
4238 usymtab t = utab->env;
4240 if (utab->reftable != NULL)
4244 for (i = 0; i < utab->nentries; i++)
4246 uentry current = utab->entries[i];
4247 uentry old = usymtab_lookupAux (t, uentry_rawName (current));
4249 uentry_mergeState (old, current, loc, FALSE, FALSE, FALSE, NOCLAUSE);
4253 llassert (t != NULL);
4255 if (t->lexlevel > paramsScope)
4257 t->guards = guardSet_levelUnion (t->guards, utab->guards, t->lexlevel);
4258 t->aliases = aliasTable_levelUnionSeq (t->aliases, utab->aliases,
4260 utab->aliases = aliasTable_undefined;
4263 t->mustBreak = utab->mustBreak;
4264 t->exitCode = utab->exitCode;
4266 usymtab_freeLevel (utab);
4272 ** Exit a scope with no checking, lose alias states.
4273 ** (When should this be used?)
4276 void usymtab_quietPlainExitScope (void)
4277 /*@globals utab, globtab, filetab@*/
4280 usymtab t = utab->env;
4282 llassert (t != NULL);
4283 llassert (NOALIAS (utab->aliases, t->aliases));
4284 usymtab_freeLevel (utab);
4288 void usymtab_exitScope (exprNode expr)
4289 /*@globals utab, filetab, globtab@*/
4290 /*@modifies utab, globtab@*/
4292 usymtab ctab = usymtab_undefined;
4293 usymtab lctab = usymtab_undefined;
4294 bool mustReturn = exprNode_mustEscape (expr);
4296 DPRINTF (("Exit scope"));
4298 if (utab->kind == US_CBRANCH)
4301 ** save the case branches, remove the first non-cbranch
4306 while (utab->kind == US_CBRANCH)
4310 llassert (utab != GLOBAL_ENV);
4314 if (utab->kind == US_TBRANCH || utab->kind == US_FBRANCH
4315 || utab->kind == US_CBRANCH || utab->kind == US_SWITCH) {
4317 if (context_inMacro ()) {
4318 /* evs 2000-07-25 */
4319 /* Unparseable macro may end inside nested scope. Deal with it. */
4321 llerror (FLG_SYNTAX,
4322 message ("Problem parsing macro body of %s (unbalanced scopes). "
4323 "Attempting to recover, recommend /*@notfunction@*/ before "
4324 "macro definition.",
4325 context_inFunctionName ()));
4327 while (utab->kind == US_TBRANCH
4328 || utab->kind == US_FBRANCH
4329 || utab->kind == US_CBRANCH
4330 || utab->kind == US_SWITCH)
4333 llassert (utab != GLOBAL_ENV);
4336 llcontbug (message ("exitScope: in branch: %q", usymtab_unparseStack ()));
4338 } /*@=branchstate@*/
4342 ** check all variables in scope were used
4346 ** bogus errors if this is the normal inside a switch,
4347 ** since cases have not been merged yet. Should probably
4348 ** still check this, but I'm too lazy at the moment...
4351 llassertfatal (utab->env != GLOBAL_ENV);
4353 if (utab->env->kind != US_SWITCH)
4359 ** check aliasing: all only params are released (dead)
4360 ** definition: all out params are defined, all modified params
4361 ** are completely defined
4363 ** NOTE: note for exiting paramsScope, since checkReturn should be
4367 if (!mustReturn && (usymtab_lexicalLevel () > functionScope))
4370 ** should only call this is end of scope is reachable...
4373 usymtab_checkFinalScope (FALSE);
4376 if (usymtab_lexicalLevel () == paramsScope && context_inFunctionLike ())
4379 ** leaving a function, need to fix up globals
4382 uentryList params = context_getParams ();
4383 globSet globs = context_getUsedGlobs ();
4386 uentryList_elements (params, ue)
4388 uentry_fixupSref (ue);
4389 } end_uentryList_elements;
4391 clearFunctionTypes ();
4394 globSet_allElements (globs, el)
4396 if (sRef_isCvar (el))
4399 int index = sRef_getScopeIndex (el);
4401 if (sRef_isFileStatic (el))
4403 llassert (usymtab_isDefined (filetab));
4404 current = usymtab_fetchIndex (filetab, index);
4408 current = usymtab_fetchIndex (globtab, index);
4411 if (uentry_isVariable (current))
4413 uentry_fixupSref (current);
4417 sRef_clearDerived (uentry_getSref (current));
4420 } end_globSet_allElements;
4423 usymtab_quietExitScope (exprNode_loc (expr));
4425 if (lctab != usymtab_undefined)
4427 /*@i@*/ lctab->env = utab;
4428 /*@i@*/ utab = ctab;
4429 /*@-branchstate@*/ } /*@=branchstate@*/
4435 ** yikes! don't let the '170 kids see this one...
4439 uentry_directParamNo (uentry ue)
4441 if (uentry_isVar (ue))
4443 sRef sr = uentry_getSref (ue);
4445 if (sRef_lexLevel (sr) == functionScope)
4450 llassert (sr->info != NULL);
4451 llassert (sr->info->cvar != NULL);
4452 index = sr->info->cvar->index;
4455 if (index < uentryList_size (context_getParams ()))
4464 /*@dependent@*/ /*@exposed@*/ uentry
4465 usymtab_getParam (int paramno)
4469 ** requires in a function context (checked)
4471 ** depends on no nested functions --- the function
4472 ** parameters are ALWAYS one scope inside the global scope
4473 ** and entered in order!
4477 if (!context_inFunctionLike ())
4478 llfatalbug (message ("usymtab_getParam: not in function context: %q",
4479 context_unparse ()));
4481 while (s->lexlevel > paramsScope)
4486 llassert (usymtab_isDefined (s));
4488 if (paramno >= s->nentries)
4491 ** Parse errors lead to this.
4494 uentry err = uentry_makeVariableLoc (cstring_makeLiteralTemp ("<error>"),
4497 uentry_markOwned (err);
4501 return (s->entries[paramno]);
4504 static /*@dependent@*/ /*@exposed@*/ uentry
4505 usymtab_getRefTab (/*@notnull@*/ usymtab u, int level, usymId index)
4509 ue = usymtab_getRefNoisy (u, level, index);
4511 if (uentry_isUndefined (ue))
4513 llbug (message ("usymtab_getRef: out of range: %d. level = %d",
4520 static /*@dependent@*/ /*@exposed@*/ usymtab
4521 usymtab_dropEnv (/*@notnull@*/ usymtab s)
4523 if (s->kind == US_CBRANCH)
4531 llassert (s != GLOBAL_ENV);
4532 } while (s->kind == US_CBRANCH);
4533 /* drop all cases (except in nested scopes */
4536 llassert (s != GLOBAL_ENV);
4539 if (s->kind == US_FBRANCH)
4541 s = s->env; /* skip the true branch */
4542 llassert (usymtab_isDefined (s));
4543 llassert (s->kind == US_TBRANCH);
4546 llassert (s != GLOBAL_ENV);
4552 /*@dependent@*/ /*@exposed@*/ uentry
4553 usymtab_getRefQuiet (int level, usymId index)
4559 llassert (s != NULL);
4560 llassert (index >= 0);
4562 if (level > s->lexlevel)
4564 return uentry_undefined;
4567 llassertprint (level <= s->lexlevel, ("level: %d / lexlevel: %d",
4568 level, s->lexlevel));
4570 while (s->lexlevel > level)
4572 if (usymtab_isBranch (s))
4574 int eindex = refTable_lookup (s, level, index);
4576 if (eindex != NOT_FOUND)
4578 return (s->entries[eindex]);
4582 s = usymtab_dropEnv (s);
4585 while (usymtab_isBranch (s) && s->lexlevel == level)
4587 int eindex = refTable_lookup (s, level, index);
4589 if (eindex != NOT_FOUND)
4591 return (s->entries[eindex]);
4594 s = usymtab_dropEnv (s);
4597 if (index >= s->nentries)
4599 return uentry_undefined;
4602 llassert (!uentry_isUndefined (s->entries[index]));
4604 return s->entries[index];
4607 static /*@dependent@*/ /*@exposed@*/ uentry
4608 usymtab_getRefNoisy (/*@notnull@*/ usymtab s, int level, usymId index)
4611 uentry ue = uentry_undefined;
4613 llassert (index >= 0);
4615 while (s->lexlevel > level)
4617 if (usymtab_isBranch (s))
4619 int eindex = refTable_lookup (s, level, index);
4621 if (eindex != NOT_FOUND)
4623 ue = s->entries[eindex];
4627 while (!usymtab_isBranch (otab))
4629 otab = usymtab_dropEnv (otab);
4630 llassert (otab != GLOBAL_ENV);
4633 if (refTable_lookup (otab, level, index) == NOT_FOUND)
4635 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4647 s = usymtab_dropEnv (s);
4650 llassert (usymtab_isDefined (s));
4652 while (usymtab_isBranch (s) && s->lexlevel == level)
4654 int eindex = refTable_lookup (s, level, index);
4657 if (eindex != NOT_FOUND)
4659 ue = s->entries[eindex];
4663 while (!usymtab_isBranch (otab))
4665 otab = usymtab_dropEnv (otab);
4666 llassert (otab != GLOBAL_ENV);
4669 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4679 s = usymtab_dropEnv (s);
4682 if (s->lexlevel == level && (index < s->nentries))
4684 ue = s->entries[index];
4686 if (uentry_isValid (ue))
4690 while (!usymtab_isBranch (otab))
4692 otab = usymtab_dropEnv (otab);
4694 if (otab == GLOBAL_ENV)
4700 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4711 if (index >= s->nentries)
4713 return uentry_undefined;
4716 llassert (!uentry_isUndefined (s->entries[index]));
4718 return s->entries[index];
4722 ** looking up entries
4724 ** If entry is inside a branch, then copy it, and put it into
4725 ** the branch table.
4729 int refTable_lookup (/*@notnull@*/ usymtab ut, int level, int index)
4731 refTable rt = ut->reftable;
4734 llassert (rt != NULL);
4736 for (i = 0; i < ut->nentries; i++)
4738 if (rt[i]->level == level && rt[i]->index == index)
4748 /*@only@*/ refentry refentry_create (int level, int index)
4750 refentry r = (refentry) dmalloc (sizeof (*r));
4758 static /*@dependent@*/ /*@exposed@*/ uentry
4759 usymtab_addRefEntry (/*@notnull@*/ usymtab s, cstring k)
4764 if (ut->reftable == NULL)
4766 DPRINTF (("Adding ref entry without reftable: %s", k));
4767 return uentry_undefined;
4770 llassert (ut->reftable != NULL);
4772 while (s != GLOBAL_ENV)
4774 eindex = usymtab_getIndex (s, k);
4776 if (eindex != NOT_FOUND)
4778 uentry current = s->entries[eindex];
4780 if (uentry_isVar (current) && !ctype_isFunction (uentry_getType (current)))
4784 DPRINTF (("Here: copying %s", uentry_unparse (current)));
4785 ue = uentry_copy (current);
4786 DPRINTF (("Here: copying %s", uentry_unparse (ue)));
4787 usymtab_addEntryQuiet (ut, ue);
4788 DPRINTF (("Okay..."));
4790 if (s->reftable != NULL)
4792 refentry ref = s->reftable[eindex];
4794 ut->reftable[ut->nentries - 1]
4795 = refentry_create (ref->level, ref->index);
4799 ut->reftable[ut->nentries - 1]
4800 = refentry_create (s->lexlevel, eindex);
4811 s = usymtab_dropEnv (s);
4814 return uentry_undefined;
4817 static uentry usymtab_lookupAux (usymtab s, cstring k)
4819 DPRINTF (("Lookup: %s", k));
4821 while (s != GLOBAL_ENV)
4823 int eindex = usymtab_getIndex (s, k);
4825 if (eindex != NOT_FOUND)
4827 uentry ret = s->entries[eindex];
4831 if (s->kind == US_TBRANCH
4832 || s->kind == US_FBRANCH
4833 || s->kind == US_CBRANCH)
4834 /* uentry_isGlobalVariable (ret) && os->lexlevel > fileScope) */
4837 DPRINTF (("Adding global ref entry: %s", k));
4838 ret = usymtab_addRefEntry (os, k);
4839 DPRINTF (("Adding ref entry: %s", uentry_unparseFull (ret)));
4844 DPRINTF (("Found: %s", uentry_unparseFull (ret)));
4848 if (s->kind == US_TBRANCH || s->kind == US_FBRANCH
4849 || s->kind == US_CBRANCH)
4851 /* why isn't this os??? */
4852 uentry ret = usymtab_addRefEntry (s, k);
4853 DPRINTF (("Adding ref entry: %s", uentry_unparseFull (ret)));
4860 return uentry_undefined;
4863 static /*@dependent@*/ /*@exposed@*/ uentry
4864 usymtab_lookupQuietAux (usymtab s, cstring k, bool noalt)
4868 while (s != GLOBAL_ENV)
4870 eindex = usymtab_getIndex (s, k);
4872 if (eindex != NOT_FOUND)
4874 uentry ret = s->entries[eindex];
4878 if (noalt && usymtab_isBranch (s))
4880 s = usymtab_dropEnv (s);
4888 return uentry_undefined;
4891 static /*@exposed@*/ /*@dependent@*/ uentry
4892 usymtab_lookupQuiet (usymtab s, cstring k)
4894 return usymtab_lookupQuietAux (s, k, FALSE);
4897 static /*@exposed@*/ /*@dependent@*/ uentry
4898 usymtab_lookupQuietNoAlt (usymtab s, cstring k)
4900 return usymtab_lookupQuietAux (s, k, TRUE);
4903 /*@dependent@*/ /*@observer@*/ uentry
4904 usymtab_lookupSafe (cstring k)
4907 DPRINTF (("Lookup safe: %s", k));
4908 return (usymtab_lookupAux (utab, k));
4912 usymtab_lookupExpose (cstring k)
4915 uentry ce = usymtab_lookupAux (utab, k);
4917 if (uentry_isUndefined (ce))
4919 llfatalbug (message ("usymtab_lookup: not found: *%s*", k));
4922 if (uentry_isPriv (ce))
4924 llfatalbug (message ("usymtab_lookup: private: *%s*", k));
4930 uentry usymtab_lookupExposeGlob (cstring k)
4932 return (usymtab_lookupGlobSafe (k));
4935 uentry usymtab_lookupGlob (cstring k)
4936 /*@globals globtab@*/
4938 uentry ce = usymtab_lookupAux (globtab, k);
4940 if (uentry_isUndefined (ce))
4941 llfatalbug (message ("usymtab_lookup: not found: %s", k));
4943 if (uentry_isPriv (ce))
4944 llfatalbug (message ("usymtab_lookup: private: %s", k));
4949 /*@observer@*/ uentry
4950 usymtab_lookupGlobSafe (cstring k)
4951 /*@globals globtab@*/
4953 uentry ce = usymtab_lookupAux (globtab, k);
4958 uentry usymtab_lookupEither (cstring k)
4961 uentry ce = usymtab_lookupSafe (k);
4963 if (uentry_isUndefined (ce))
4964 llfatalerror (message ("usymtab_lookup: not found: %s", k));
4971 usymtab_lookupType (cstring k)
4972 /*@globals globtab@*/
4974 usymId uid = usymtab_getTypeId (k);
4976 if (uid == USYMIDINVALID)
4978 llcontbug (message ("usymtab_lookupType: not found: %s", k));
4979 return ctype_unknown;
4982 return (uentry_getRealType (usymtab_getTypeEntry (uid)));
4987 usymtab_lookupAbstractType (cstring k) /*@globals globtab@*/
4989 usymId uid = usymtab_getTypeId (k);
4991 if (uid == USYMIDINVALID)
4993 llcontbug (message ("usymtab_lookupType: not found: %s", k));
4994 return ctype_unknown;
4997 return (uentry_getAbstractType (usymtab_getTypeEntry (uid)));
5001 ** if there is an unnamed lcl-specified struct tag matching
5002 ** the uentryList, return its datatype. Otherwise, returns
5007 usymtab_structFieldsType (uentryList f)
5008 /*@globals globtab@*/
5010 return (usymtab_suFieldsType (f, TRUE));
5014 usymtab_unionFieldsType (uentryList f)
5015 /*@globals globtab@*/
5017 return (usymtab_suFieldsType (f, FALSE));
5021 usymtab_suFieldsType (uentryList f, bool isStruct)
5022 /*@globals globtab@*/
5026 DPRINTF (("Fields: %s", uentryList_unparse (f)));
5028 if (fileloc_isSpec (g_currentloc))
5030 return (ctype_undefined);
5033 for (i = 0; i < globtab->nentries; i++)
5035 uentry current = globtab->entries[i];
5038 ? uentry_isStructTag (current) : uentry_isUnionTag (current)))
5040 if (isFakeTag (uentry_rawName (current)))
5042 ctype ct = uentry_getType (current);
5044 DPRINTF (("Check: %s", ctype_unparse (ct)));
5046 if ((isStruct ? ctype_isStruct (ct) : ctype_isUnion (ct))
5048 (uentry_isSpecified (current)
5049 && uentryList_equivFields (f, ctype_getFields (ct))))
5051 return uentry_getAbstractType (current);
5061 return ctype_undefined;
5065 usymtab_enumEnumNameListType (enumNameList f)
5066 /*@globals globtab@*/
5070 for (i = 0; i < globtab->nentries; i++)
5072 uentry current = globtab->entries[i];
5074 if (uentry_isEnumTag (current))
5076 if (isFakeTag (uentry_rawName (current)))
5078 ctype ct = uentry_getType (current);
5080 if (ctype_isEnum (ct) && (enumNameList_match (f, ctype_elist (ct))))
5082 return uentry_getType (current);
5088 return ctype_undefined;
5092 usymtab_exists (cstring k)
5095 uentry ce = usymtab_lookupSafe (k);
5096 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5100 usymtab_existsReal (cstring k)
5103 uentry ce = usymtab_lookupSafe (k);
5105 return (!(uentry_isUndefined (ce))
5106 && !(uentry_isPriv (ce))
5107 && !(uentry_isExpandedMacro (ce)));
5111 usymtab_existsGlob (cstring k)
5112 /*@globals globtab@*/
5114 uentry ce = usymtab_lookupAux (globtab, k);
5116 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5121 usymtab_existsEither (cstring k)
5124 uentry ce = usymtab_lookupAux (utab, k);
5126 return (uentry_isValid (ce));
5130 usymtab_existsGlobEither (cstring k)
5131 /*@globals globtab@*/
5133 uentry ce = usymtab_lookupAux (globtab, k);
5135 return (uentry_isValid (ce));
5140 usymtab_existsType (cstring k)
5141 /*@globals globtab@*/
5143 uentry ce = usymtab_lookupAux (globtab, k);
5145 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)) && uentry_isDatatype (ce));
5149 usymtab_existsTypeEither (cstring k)
5150 /*@globals globtab@*/
5153 ce = usymtab_lookupAux (globtab, k);
5154 return (uentry_isValid (ce) && uentry_isDatatype (ce));
5158 usymtab_existsStructTag (cstring k) /*@globals globtab@*/
5160 cstring sname = makeStruct (k);
5161 uentry ce = usymtab_lookupAux (globtab, sname);
5162 cstring_free (sname);
5163 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5167 usymtab_existsUnionTag (cstring k) /*@globals globtab@*/
5169 cstring uname = makeUnion (k);
5170 uentry ce = usymtab_lookupAux (globtab, uname);
5172 cstring_free (uname);
5174 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5178 usymtab_existsEnumTag (cstring k) /*@globals globtab@*/
5180 cstring ename = makeEnum (k);
5181 uentry ce = usymtab_lookupAux (globtab, ename);
5183 cstring_free (ename);
5184 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5188 bool usymtab_existsVar (cstring k)
5191 uentry ce = usymtab_lookupSafe (k);
5193 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)) && (uentry_isVar (ce)));
5202 refTable_free (/*@only@*/ /*@null@*/ refTable x, int nentries)
5208 for (i = 0; i < nentries; i++)
5218 usymtab_freeLevel (/*@notnull@*/ /*@only@*/ usymtab u)
5219 /*@globals globtab, utab, filetab@*/
5223 aliasTable_free (u->aliases);
5225 refTable_free (u->reftable, u->nentries);
5227 if (u == filetab || u == globtab)
5229 for (i = 0; i < u->nentries; i++)
5231 uentry_freeComplete (u->entries[i]);
5236 for (i = 0; i < u->nentries; i++)
5238 uentry_free (u->entries[i]);
5242 guardSet_free (u->guards);
5249 llassert (!cstringTable_isDefined (u->htable));
5257 usymtab_freeAux (/*@only@*/ usymtab u)
5258 /*@globals globtab, utab, filetab@*/
5261 while (u != GLOBAL_ENV)
5264 usymtab_freeLevel (u);
5271 void usymtab_free ()
5272 /*@globals killed utab, globtab, filetab@*/
5276 usymtab_freeAux (utab);
5279 static int usymtab_lexicalLevel (void) /*@globals utab@*/
5281 return (utab->lexlevel);
5284 bool usymtab_inGlobalScope () /*@globals utab, globtab@*/
5286 return (utab == globtab);
5289 bool usymtab_inFileScope () /*@globals utab@*/
5291 return (utab->lexlevel == fileScope);
5294 bool usymtab_inFunctionScope () /*@globals utab@*/
5296 return (utab->lexlevel == functionScope);
5301 usymtab_replaceEntry (uentry s)
5302 /*@globals utab, globtab@*/
5303 /*@modifies utab, s@*/
5305 usymtab_replaceEntryAux (utab, s);
5310 usymtab_matchForwardStruct (usymId u1, usymId u2)
5311 /*@globals globtab@*/
5313 uentry ue1 = usymtab_getTypeEntry (u1);
5314 uentry ue2 = usymtab_getTypeEntry (u2);
5316 if (uentry_isAnyTag (ue2))
5318 ctype reptype = uentry_getType (ue1);
5320 if (ctype_isPointer (reptype))
5322 ctype repbase = ctype_getBaseType (reptype);
5324 if (ctype_isUA (repbase))
5326 typeId rtuid = ctype_typeId (repbase);
5328 if (u2 == rtuid) return TRUE;
5330 if (usymId_isValid (rtuid))
5332 reptype = uentry_getType (usymtab_getTypeEntry (rtuid));
5334 return (ctype_isUA (reptype) && (u2 == (ctype_typeId (reptype))));
5343 void usymtab_addGuards (guardSet guards)
5346 utab->guards = guardSet_union (utab->guards, guards);
5349 static bool usymtab_isGuardedAux (sRef s)
5353 sRef base = sRef_getRootBase (s);
5354 int lowlevel = paramsScope;
5355 int baselevel = sRef_lexLevel (base);
5357 if (sRef_isCvar (base))
5359 lowlevel = baselevel;
5360 if (lowlevel < paramsScope) lowlevel = paramsScope;
5363 while (tab->lexlevel >= lowlevel)
5365 if (guardSet_isGuarded (tab->guards, s))
5368 if (!sRef_definitelyNull (s))
5370 sRef_setNotNull (s, fileloc_undefined);
5376 tab = usymtab_dropEnv (tab);
5382 void usymtab_unguard (sRef s) /*@modifies utab@*/
5385 sRef base = sRef_getRootBase (s);
5386 int lowlevel = paramsScope;
5387 int baselevel = sRef_lexLevel (base);
5389 if (sRef_isCvar (base))
5391 lowlevel = baselevel;
5392 if (lowlevel < paramsScope) lowlevel = paramsScope;
5395 while (tab->lexlevel >= lowlevel)
5397 if (guardSet_isGuarded (tab->guards, s))
5399 guardSet_delete (tab->guards, s);
5402 tab = usymtab_dropEnv (tab);
5406 bool usymtab_isGuarded (sRef s)
5409 return (sRef_aliasCompleteSimplePred (usymtab_isGuardedAux, s));
5412 bool usymtab_isDefinitelyNull (sRef s)
5414 return (sRef_aliasCheckSimplePred (usymtab_isDefinitelyNullAux, s));
5417 bool usymtab_isDefinitelyNullDeep (sRef s)
5419 return (sRef_deepPred (usymtab_isDefinitelyNull, s));
5422 static bool usymtab_isDefinitelyNullAux (sRef s)
5426 sRef base = sRef_getRootBase (s);
5427 int lowlevel = paramsScope;
5429 if (sRef_isCvar (base))
5431 lowlevel = sRef_lexLevel (base);
5432 if (lowlevel < paramsScope) lowlevel = paramsScope;
5435 while (tab->lexlevel >= lowlevel)
5437 if (guardSet_mustBeNull (tab->guards, s))
5442 while (tab->kind == US_CBRANCH)
5447 llassert (usymtab_isDefined (tab));
5449 if (tab->kind == US_FBRANCH)
5452 llassert (tab->kind == US_TBRANCH);
5462 usymtab_printGuards ()
5463 /*@globals utab, globtab@*/
5465 usymtab ttab = utab;
5467 while (ttab != globtab)
5469 llmsg (message ("Guards [%d]: %q", ttab->lexlevel,
5470 guardSet_unparse (ttab->guards)));
5476 usymtab_displayAllUses ()
5477 /*@globals utab, globtab@*/
5481 /* only in top scope */
5482 llassert (utab == globtab);
5484 /* need a copy, so order is not messed up by sort! */
5485 copy = usymtab_shallowCopy (globtab);
5487 qsort (copy->entries, (size_t)copy->nentries,
5488 sizeof (*copy->entries), (int (*)(const void *, const void *)) uentry_xcompareuses);
5490 usymtab_entries (copy, ue)
5492 if (uentry_isValid (ue))
5494 filelocList uses = uentry_getUses (ue);
5495 int size = filelocList_realSize (uses);
5497 if (fileloc_isDefined (uentry_whereDefined (ue))
5498 && !fileloc_isLib (uentry_whereDefined (ue))
5501 llmsg (message ("%q (%q), %d use%&:\n %q",
5502 uentry_getName (ue),
5503 fileloc_unparse (uentry_whereDefined (ue)),
5504 size, filelocList_unparseUses (uses)));
5507 } end_usymtab_entries;
5509 usymtab_shallowFree (copy);
5512 static /*@dependent@*/ /*@exposed@*/ usymtab
5513 usymtab_getFileTab ()
5514 /*@globals filetab@*/
5516 llassert (filetab != NULL);
5522 usymtab_unparseStack ()
5525 return (usymtab_unparseStackTab (utab));
5528 static /*@only@*/ cstring
5529 usymtab_unparseStackTab (usymtab t)
5531 bool firstOne = TRUE;
5532 cstring ret = cstring_makeLiteral ("[");
5534 while (t != GLOBAL_ENV)
5538 ret = message ("%q %q", ret, usymtab_typeName (t));
5543 ret = message ("%q, %q", ret, usymtab_typeName (t));
5548 ret = message ("%q ]", ret);
5552 static /*@only@*/ cstring
5553 usymtab_typeName (/*@notnull@*/ usymtab t)
5557 case US_GLOBAL: return cstring_makeLiteral ("global");
5558 case US_NORMAL: return cstring_makeLiteral ("normal");
5559 case US_TBRANCH: return cstring_makeLiteral ("true");
5560 case US_FBRANCH: return cstring_makeLiteral ("false");
5561 case US_CBRANCH: return cstring_makeLiteral ("case");
5562 case US_SWITCH: return cstring_makeLiteral ("switch");
5568 void usymtab_addMustAlias (/*@exposed@*/ sRef s, /*@exposed@*/ sRef al)
5571 if (!sRef_similar (s, al))
5573 usymtab_addForceMustAlias (s, al);
5578 ** Same as usymtab_addMustAlias, except does not check sRef_isSimilar.
5581 void usymtab_addForceMustAlias (/*@exposed@*/ sRef s, /*@exposed@*/ sRef al)
5584 if (sRef_isMeaningful (s)
5585 && sRef_isMeaningful (al)
5586 && !(sRef_isConst (s) || sRef_isConst (al))
5587 && !(sRef_isAddress (al) && sRef_isDirectParam (sRef_getBase (al))))
5589 utab->aliases = aliasTable_addMustAlias (utab->aliases, s, al);
5590 DPRINTF (("Must alias: %s", aliasTable_unparse (utab->aliases)));
5593 ** for local variable, aliasing is symmetric
5596 if (sRef_isLocalVar (s) && sRef_isLocalVar (al))
5598 utab->aliases = aliasTable_addMustAlias (utab->aliases, al, s);
5607 void usymtab_clearAlias (sRef s)
5608 /*@modifies utab, s@*/
5611 aliasTable_clearAliases (utab->aliases, s);
5614 sRefSet usymtab_allAliases (sRef s)
5617 if (sRef_isMeaningful (s))
5621 ret = sRefSet_unionFree (aliasTable_aliasedBy (utab->aliases, s),
5622 aliasTable_canAlias (utab->aliases, s));
5627 DPRINTF (("NOT A MEANINGFUL SREF!"));
5628 return sRefSet_undefined;
5632 /*@only@*/ sRefSet usymtab_canAlias (sRef s)
5635 if (sRef_isMeaningful (s))
5637 sRefSet res = aliasTable_canAlias (utab->aliases, s);
5641 return sRefSet_undefined;
5644 /*@only@*/ sRefSet usymtab_aliasedBy (sRef s)
5647 return (aliasTable_aliasedBy (utab->aliases, s));
5650 /*@only@*/ cstring usymtab_unparseAliases ()
5653 return (aliasTable_unparse (utab->aliases));
5657 ** Debugging routines:
5658 ** okay to leak storage here, only for debugging
5664 usymtab_printOut (void)
5670 char *ind = mstring_copy (" ");
5672 fprintf (g_msgstream, "<<< [symbol table] >>>\n");
5674 while (s != GLOBAL_ENV && s->env != GLOBAL_ENV)
5676 cstring tname = usymtab_typeName (s);
5680 ind[depth * 3 + 1] = '\0';
5683 fprintf (g_msgstream, "level: %d (%s)\n", s->lexlevel,
5684 cstring_toCharsSafe (tname));
5686 cstring_free (tname);
5688 for (i = 0; i < s->nentries; i++)
5690 cstring us = uentry_unparseFull (s->entries[i]);
5691 fprintf (g_msgstream, "%s\n", cstring_toCharsSafe (us));
5695 if (s->reftable != NULL && s->nentries > 0)
5697 fprintf (g_msgstream, "\t<< Ref table >>\n");
5699 for (i = 0; i < s->nentries; i++)
5701 fprintf (g_msgstream, "\t%s %3d: %d, %d\n", ind, i,
5702 s->reftable[i]->level,
5703 s->reftable[i]->index);
5707 ind[depth * 3 + 1] = ' ';
5711 fprintf (g_msgstream, "<<< end usymtab >>>\n");
5717 usymtab_printTypes ()
5718 /*@globals globtab@*/
5720 usymtab_printAllAux (globtab);
5724 usymtab_printAll (void)
5727 usymtab_printAllAux (utab);
5731 usymtab_printAllAux (usymtab s)
5732 /*@modifies g_msgstream@*/
5736 char *ind = mstring_copy (" ");
5738 printf ("[[[ usymtab ]]]");
5740 while (s != GLOBAL_ENV)
5743 ind[depth * 3 + 1] = '\0';
5745 if (s->env == GLOBAL_ENV)
5749 printf ("level: %d / break: %s / exit: %s\n", s->lexlevel,
5750 cstring_toCharsSafe (bool_unparse (s->mustBreak)),
5751 cstring_toCharsSafe (exitkind_unparse (s->exitCode)));
5755 for (i = looplow; i < s->nentries; i++)
5757 printf ("%s%3d. %s\n", ind, i,
5758 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5763 printf ("level: %d / break: %s / exit: %s\n", s->lexlevel,
5764 cstring_toCharsSafe (bool_unparse (s->mustBreak)),
5765 cstring_toCharsSafe (exitkind_unparse (s->exitCode)));
5767 for (i = 0; i < s->nentries; i++)
5769 printf ("%s%3d %s\n", ind, i,
5770 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5774 ind[depth * 3 + 1] = ' ';
5778 printf ("----------\n");
5782 usymtab_printComplete ()
5787 char *ind = mstring_copy (" ");
5790 while (s != GLOBAL_ENV)
5794 ind[depth * 3 + 1] = '\0';
5797 if (s->env == GLOBAL_ENV)
5801 printf ("level: %d\n", s->lexlevel);
5805 for (i = looplow; i < s->nentries; i++)
5807 printf ("%s%3d %s\n", ind, i,
5808 cstring_toCharsSafe (uentry_unparse (s->entries[i])));
5813 printf ("level: %d\n", s->lexlevel);
5814 for (i = 0; i < s->nentries; i++)
5816 printf ("%s%3d %s\n", ind, i,
5817 cstring_toCharsSafe (uentry_unparse (s->entries[i])));
5821 ind[depth * 3 + 1] = ' ';
5826 printf ("----------\n");
5830 static /*@only@*/ cstring /*@unused@*/
5831 usymtab_unparseLocalAux (/*@notnull@*/ usymtab s)
5833 cstring c = message ("lexlevel: %d\n", s->lexlevel);
5836 for (i = 0; i < s->nentries; i++)
5838 c = message ("%q\n%q", c, uentry_unparseFull (s->entries[i]));
5841 c = message ("%q\n=========", c);
5845 static cstring /*@unused@*/ /*@only@*/
5846 usymtab_unparseLocalList (/*@notnull@*/ usymtab s)
5848 cstring c = message ("[%d/%s/%s] ", s->lexlevel,
5849 bool_unparse (s->mustBreak),
5850 exitkind_unparse (s->exitCode));
5853 for (i = 0; i < s->nentries; i++)
5855 sRef sr = uentry_getSref (s->entries[i]);
5859 c = message ("%q: %q [%b]", c, uentry_getName (s->entries[i]),
5860 sRef_isStateDefined (sr));
5864 c = message ("%q, %q [%b]", c, uentry_getName (s->entries[i]),
5865 sRef_isStateDefined (sr));
5874 usymtab_printLocal (void)
5880 printf ("lexlevel: %d\n", s->lexlevel);
5882 for (i = 0; i < s->nentries; i++)
5884 printf ("%s\n", cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5887 while (s->lexlevel > 1)
5892 llassert (usymtab_isDefined (s));
5894 printf ("Params:\n");
5896 for (i = 0; i < s->nentries; i++)
5898 printf ("%d: %s\n", i,
5899 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5904 static bool checkDistinctExternalName (uentry e)
5905 /*@globals globtab@*/
5906 /*@modifies *g_msgstream@*/
5908 int checklen = context_getValue (FLG_EXTERNALNAMELEN);
5909 bool ignorecase = context_getFlag (FLG_EXTERNALNAMECASEINSENSITIVE);
5910 bool gotone = FALSE;
5911 bool extras = FALSE;
5912 bool hasError = FALSE;
5913 cstring name = uentry_rawName (e);
5914 usymtab st = globtab;
5922 if (uentry_isAnyTag (e))
5924 checklen++; /* the tag marker doesn't count */
5928 usymtab_entries (st, oe)
5930 if (uentry_sameObject (oe, e))
5937 if (cstring_equalCaseInsensitive (uentry_rawName (oe), name))
5946 (FLG_DISTINCTEXTERNALNAMES,
5948 ("External identifier %q is not distinguishable from %q "
5949 "because alphabetical case is ignored",
5951 uentry_getName (oe)),
5952 uentry_whereLast (e)))
5954 uentry_showWhereAny (oe);
5955 uentry_setHasNameError (oe);
5964 if (cstring_equalLenCaseInsensitive (uentry_rawName (oe),
5973 if (cstring_equalLen (uentry_rawName (oe), name, checklen))
5976 (FLG_DISTINCTEXTERNALNAMES,
5979 ("External identifier %q is not distinguishable from %q "
5980 "in the first %d characters (%q)",
5982 uentry_getName (oe),
5984 cstring_clip (uentry_getName (e), checklen)),
5986 uentry_whereLast (e)))
5988 uentry_showWhereAny (oe);
5989 uentry_setHasNameError (oe);
6002 (FLG_DISTINCTEXTERNALNAMES,
6004 ("External identifier %q is not distinguishable from %q "
6005 "in the first %d characters because alphabetical case "
6008 uentry_getName (oe),
6010 uentry_whereLast (e)))
6012 uentry_showWhereAny (oe);
6013 uentry_setHasNameError (oe);
6019 else if (cstring_equalLen (uentry_rawName (oe), name, checklen))
6028 (FLG_DISTINCTEXTERNALNAMES,
6031 ("External identifier %q is not distinguishable from %q "
6032 "in the first %d characters (%q)",
6034 uentry_getName (oe),
6036 cstring_clip (uentry_getName (e), checklen)),
6038 uentry_whereLast (e)))
6040 uentry_showWhereAny (oe);
6041 uentry_setHasNameError (oe);
6050 } end_usymtab_entries ;
6057 (cstring_makeLiteral ("One or more additional "
6058 "indistinguishable external "
6059 "names not reported"));
6065 static bool checkDistinctInternalName (uentry e)
6067 /*@modifies *g_msgstream@*/
6069 usymtab ttab = utab;
6070 cstring name = uentry_rawName (e);
6071 int numchars = context_getValue (FLG_INTERNALNAMELEN);
6072 bool caseinsensitive = context_getFlag (FLG_INTERNALNAMECASEINSENSITIVE);
6073 bool lookalike = context_getFlag (FLG_INTERNALNAMELOOKALIKE);
6075 if (uentry_isAnyTag (e) && (numchars != 0))
6077 numchars++; /* the tag marker doesn't count */
6080 while (usymtab_isDefined (ttab))
6082 usymtab_entries (ttab, oe)
6084 if (uentry_sameObject (oe, e))
6086 /*@innercontinue@*/ continue;
6089 switch (cstring_genericEqual
6090 (name, uentry_rawName (oe),
6091 numchars, caseinsensitive, lookalike))
6093 case CGE_DISTINCT: /* okay */
6097 if (cstring_equal (name, uentry_rawName (oe)))
6099 ; /* got a shadow error */
6104 (FLG_DISTINCTINTERNALNAMES,
6107 ("Internal identifier %q is not distinguishable from %q "
6108 "in the first %d characters (%q)",
6110 uentry_getName (oe),
6112 cstring_clip (uentry_getName (e), numchars)),
6114 uentry_whereLast (e)))
6116 uentry_showWhereAny (oe);
6117 uentry_setHasNameError (oe);
6125 || (cstring_length (name) <= numchars))
6128 (FLG_DISTINCTINTERNALNAMES,
6130 ("Internal identifier %q is not distinguishable from %q "
6131 "without case sensitivity",
6133 uentry_getName (oe)),
6134 uentry_whereLast (e)))
6136 uentry_showWhereAny (oe);
6137 uentry_setHasNameError (oe);
6144 (FLG_DISTINCTINTERNALNAMES,
6146 ("Internal identifier %q is not distinguishable from %q "
6147 "in the first %d characters without case sensitivity",
6149 uentry_getName (oe),
6151 uentry_whereLast (e)))
6153 uentry_showWhereAny (oe);
6154 uentry_setHasNameError (oe);
6162 || (cstring_length (name) <= numchars))
6165 (FLG_DISTINCTINTERNALNAMES,
6167 ("Internal identifier %q is not distinguishable from %q "
6168 "except by lookalike characters",
6170 uentry_getName (oe)),
6171 uentry_whereLast (e)))
6173 uentry_showWhereAny (oe);
6174 uentry_setHasNameError (oe);
6181 (FLG_DISTINCTINTERNALNAMES,
6183 ("Internal identifier %q is not distinguishable from %q "
6184 "in the first %d characters except by lookalike characters",
6186 uentry_getName (oe),
6188 uentry_whereLast (e)))
6190 uentry_showWhereAny (oe);
6191 uentry_setHasNameError (oe);
6196 } end_usymtab_entries ;
6204 void usymtab_checkDistinctName (uentry e, int scope)
6205 /*@globals utab, globtab@*/
6207 bool hasError = FALSE;
6208 fileloc where = uentry_whereLast (e);
6210 if (!fileloc_isPreproc (where) && !fileloc_isBuiltin (where))
6212 if (scope == globScope)
6214 if (context_getFlag (FLG_DISTINCTEXTERNALNAMES))
6216 hasError = checkDistinctExternalName (e);
6220 if (!hasError && context_getFlag (FLG_DISTINCTINTERNALNAMES))
6222 hasError = checkDistinctInternalName (e);
6228 uentry_setHasNameError (e);
6232 /*@exposed@*/ sRef usymtab_lookupGlobalMarker (void) /*@globals utab@*/
6236 ue = usymtab_lookupAux (utab, GLOBAL_MARKER_NAME);
6237 llassert (uentry_isValid (ue));
6239 return uentry_getSref (ue);