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);
1052 (void) usymtab_supEntryAux (tab, e, isref);
1056 ret = usymtab_lookupAux (filetab, rawName);
1060 ret = usymtab_lookupAux (tab, rawName);
1062 if (uentry_isInvalid (ret) && usymtab_isDefined (filetab))
1064 ret = usymtab_lookupAux (filetab, rawName);
1068 cstring_free (rawName);
1072 /*@dependent@*/ /*@exposed@*/ uentry
1073 usymtab_supEntryReturn (/*@only@*/ uentry e)
1074 /*@globals utab, filetab, globtab@*/
1075 /*@modifies utab, globtab, e@*/
1077 return (usymtab_supEntryReturnAux (utab, e, FALSE));
1080 /*@dependent@*/ /*@exposed@*/ uentry
1081 usymtab_supEntrySrefReturn (/*@only@*/ uentry e)
1082 /*@globals utab, globtab, filetab@*/
1083 /*@modifies utab, globtab, e@*/
1085 return (usymtab_supEntryReturnAux (utab, e, TRUE));
1088 /*@dependent@*/ /*@exposed@*/ uentry
1089 usymtab_supGlobalEntryReturn (uentry e)
1090 /*@globals globtab, filetab@*/
1091 /*@modifies globtab, e@*/
1095 ret = usymtab_supEntryReturnAux (globtab, e, FALSE);
1097 if (sRef_modInFunction ())
1099 recordFunctionType (ret);
1106 usymtab_supTypeEntry (/*@only@*/ uentry e)
1107 /*@globals globtab, filetab@*/
1108 /*@modifies globtab, e@*/
1113 if (uentry_isAbstractDatatype (e))
1115 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1116 ret = ctype_createAbstract (uid);
1120 uid = usymtab_supEntryAux (globtab, e, FALSE);
1121 ret = ctype_createUser (uid);
1124 if (sRef_modInFunction ())
1126 recordFunctionType (globtab->entries[uid]);
1133 usymtab_supReturnTypeEntry (/*@only@*/ uentry e)
1134 /*@globals globtab, filetab@*/
1135 /*@modifies globtab@*/
1139 DPRINTF (("Abstract? %s", uentry_unparseFull (e)));
1141 if (uentry_isAbstractDatatype (e))
1143 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1145 else if (uentry_isMaybeAbstract (e) && context_getFlag (FLG_IMPABSTRACT))
1147 bool maybeabs = TRUE;
1148 cstring sname = uentry_getName (e);
1149 uentry ue = usymtab_lookupGlobSafe (sname);
1150 cstring_free (sname);
1152 if (uentry_isValid (ue))
1154 DPRINTF (("Lookup: %s", uentry_unparseFull (ue)));
1156 if (uentry_isDatatype (ue))
1158 if (uentry_isMaybeAbstract (ue))
1169 DPRINTF (("Not datatype!"));
1176 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1177 ux = usymtab_getTypeEntry (uid);
1178 uentry_setAbstract (ux);
1182 uid = usymtab_supEntryAux (globtab, e, FALSE);
1183 e = usymtab_getTypeEntry (uid);
1185 if (uentry_isMaybeAbstract (e))
1187 uentry_setConcrete (e);
1193 uid = usymtab_supEntryAux (globtab, e, FALSE);
1194 e = usymtab_getTypeEntry (uid);
1196 if (uentry_isMaybeAbstract (e))
1198 uentry_setConcrete (e);
1202 if (sRef_modInFunction ())
1204 recordFunctionType (globtab->entries[uid]);
1207 return (globtab->entries[uid]);
1211 usymtab_supAbstractTypeEntry (/*@only@*/ uentry e, bool dodef)
1212 /*@globals globtab, filetab@*/
1213 /*@modifies globtab, e@*/
1218 uid = usymtab_supEntryAux (globtab, e, FALSE);
1219 ue = usymtab_getTypeEntry (uid);
1223 uentry_setDatatype (ue, uid);
1226 if (context_getFlag (FLG_ACCESSMODULE)) /* was accessfile */
1228 context_addFileAccessType (uid);
1231 if (sRef_modInFunction ())
1233 recordFunctionType (globtab->entries[uid]);
1241 usymtab_supExposedTypeEntry (/*@only@*/ uentry e, bool dodef)
1242 /*@globals globtab, filetab@*/
1243 /*@modifies globtab, e@*/
1247 uid = usymtab_supEntryAux (globtab, e, FALSE);
1251 uentry ue = usymtab_getTypeEntry (uid);
1253 uentry_setDatatype (ue, uid);
1256 if (sRef_modInFunction ())
1258 recordFunctionType (globtab->entries[uid]);
1266 usymtab_supForwardTypeEntry (/*@only@*/ uentry e)
1267 /*@globals globtab, filetab@*/
1268 /*@modifies globtab, e@*/
1270 usymId uid = usymtab_supEntryAux (globtab, e, FALSE);
1271 uentry ue = usymtab_getTypeEntry (uid);
1273 uentry_setDatatype (ue, uid);
1275 if (sRef_modInFunction ())
1277 recordFunctionType (globtab->entries[uid]);
1280 return (uentry_getAbstractType (ue));
1284 usymtab_supEntrySref (uentry e)
1285 /*@globals utab, globtab, filetab@*/
1286 /*@modifies utab, globtab, e@*/
1288 sRef old = uentry_getSref (e);
1291 if (sRef_isType (old))
1293 uentry ue = usymtab_supEntryReturnAux (utab, e, TRUE);
1296 if (uentry_isValid (ue))
1298 sRef uref = uentry_getSref (ue);
1300 sRef_mergeStateQuiet (uref, old);
1301 sRef_clearDerived (uref);
1303 /*@noaccess uentry@*/
1305 else if (sRef_isKnown (old))
1307 usymtab_supEntry (e);
1311 (void) usymtab_supEntryAux (utab, e, TRUE);
1315 void usymtab_supGlobalEntry (/*@only@*/ uentry e)
1316 /*@globals globtab, filetab@*/
1317 /*@modifies globtab, filetab, e@*/
1321 DPRINTF (("Sup global entry: %s", uentry_unparse (e)));
1323 uid = usymtab_supEntryAux (globtab, e, FALSE);
1325 if (sRef_modInFunction ())
1327 recordFunctionType (globtab->entries[uid]);
1332 usymtab_supReturnFileEntry (/*@only@*/ uentry e)
1333 /*@globals filetab, globtab@*/
1334 /*@modifies filetab, globtab, e@*/
1336 llassert (filetab != usymtab_undefined);
1337 DPRINTF (("File entry: %s", uentry_unparse (e)));
1338 return (usymtab_supEntryReturnAux (filetab, e, FALSE));
1346 usymtab_inDeepScope () /*@globals utab@*/
1348 return (utab->lexlevel > paramsScope);
1352 usymtab_getIndex (/*@notnull@*/ usymtab s, cstring k)
1356 DPRINTF (("Lookup %s", k));
1358 if (cstringTable_isDefined (s->htable))
1360 i = cstringTable_lookup (s->htable, k);
1365 for (i = 0; i < s->nentries; i++)
1367 uentry current = s->entries[i];
1369 DPRINTF (("Check %d: %s", i, uentry_rawName (current)));
1371 if (!uentry_isUndefined (current)
1372 && cstring_equal (uentry_rawName (current), k))
1383 usymtab_fetchIndex (/*@notnull@*/ usymtab s, int i)
1385 llassert (i >= 0 && i < s->nentries);
1386 return (s->entries[i]);
1390 usymtab_getTypeId (cstring k) /*@globals globtab@*/
1392 usymId uid = usymtab_getIndex (globtab, k);
1394 if (uid == NOT_FOUND) return USYMIDINVALID;
1396 if (!(uentry_isDatatype (usymtab_getTypeEntry (uid)))) {
1397 return USYMIDINVALID;
1403 /*@dependent@*/ uentry
1404 usymtab_lookupStructTag (cstring k)
1406 cstring sname = makeStruct (k);
1407 uentry ue = usymtab_lookupGlob (sname);
1409 cstring_free (sname);
1413 /*@dependent@*/ uentry
1414 usymtab_lookupUnionTag (cstring k)
1416 cstring uname = makeUnion (k);
1417 uentry res = usymtab_lookupGlob (uname);
1419 cstring_free (uname);
1423 /*@dependent@*/ uentry
1424 usymtab_lookupEnumTag (cstring k)
1426 cstring ename = makeEnum (k);
1427 uentry res = usymtab_lookupGlob (ename);
1429 cstring_free (ename);
1434 usymtab_getId (cstring k) /*@globals globtab@*/
1436 usymId uid = usymtab_getIndex (globtab, k);
1439 if (uid == NOT_FOUND)
1441 return USYMIDINVALID;
1444 ue = usymtab_getGlobalEntry (uid);
1446 if (uentry_isPriv (ue))
1448 return USYMIDINVALID;
1454 static /*@exposed@*/ uentry
1455 usymtab_getEntryAux (/*@notnull@*/ usymtab s, usymId uid)
1457 llassert (uid != USYMIDINVALID);
1459 if (uid < 0 || uid >= s->nentries)
1461 llcontbug (message ("usymtab_getEntry: out of range: level = %d [%d]",
1463 return uentry_undefined;
1466 llassertprint (uentry_isValid (s->entries[uid]),
1467 ("entry undefined: %d", uid));
1469 return s->entries[uid];
1472 /*@dependent@*/ /*@observer@*/ uentry
1473 usymtab_getGlobalEntry (usymId uid)
1474 /*@globals utab, globtab@*/
1476 if (dbgfree) return (uentry_undefined);
1478 if (utab->lexlevel > paramsScope)
1480 /* need to do this the awkward way, since it could be in conditional scope */
1481 return (usymtab_lookupSafe (uentry_rawName (globtab->entries[uid])));
1485 return (globtab->entries[uid]);
1489 /*@dependent@*/ /*@exposed@*/ uentry
1490 usymtab_getTypeEntry (usymId uid)
1491 /*@globals globtab@*/
1495 if (uid >= 0 && uid < globtab->nentries)
1497 return (globtab->entries[uid]);
1501 return (uentry_undefined);
1506 llassert (uid >= 0 && uid < globtab->nentries);
1507 return (globtab->entries[uid]);
1515 /*@dependent@*/ /*@exposed@*/ uentry
1516 usymtab_getTypeEntrySafe (usymId uid)
1517 /*@globals globtab@*/
1519 if (uid < 0 || uid >= globtab->nentries)
1521 return uentry_undefined;
1524 return (globtab->entries[uid]);
1528 usymtab_isBoolType (usymId uid)
1529 /*@globals globtab@*/
1531 llassert (uid >= 0 && uid < globtab->nentries);
1533 return (cstring_equal (uentry_rawName (globtab->entries[uid]),
1534 context_getBoolName ()));
1538 usymtab_getTypeEntryName (usymId uid)
1539 /*@globals globtab@*/
1545 return (cstring_makeLiteral ("<freetype>"));
1548 ue = usymtab_getTypeEntry (uid);
1550 if (dbgload && !uentry_isValid (ue))
1552 return (message ("<missing type: %d>", uid));
1555 llassertprint (uentry_isValid (ue), ("type undefined: %d", uid));
1557 return (uentry_getName (ue));
1560 /*@unused@*/ static void
1561 usymtab_rehash (/*@notnull@*/ usymtab s)
1565 if (cstringTable_isDefined (s->htable))
1567 cstringTable_free (s->htable);
1570 s->htable = cstringTable_create (LLHASHSIZE);
1572 for (i = 0; i < s->nentries; i++)
1574 cstringTable_insert (s->htable, cstring_copy (uentry_rawName (s->entries[i])), i);
1579 ** superficial copy of usymtab
1581 ** DO copy spec entries
1584 static /*@only@*/ /*@notnull@*/ usymtab
1585 usymtab_shallowCopy (/*@notnull@*/ usymtab s) /*@*/
1587 usymtab copytab = usymtab_createRoot ();
1590 for (i = 0; i < s->nentries; i++)
1592 usymtab_addEntryBase (copytab, s->entries[i]);
1599 usymtab_shallowFree (/*@only@*/ /*@notnull@*/ usymtab s)
1601 aliasTable_free (s->aliases);
1602 refTable_free (s->reftable, s->nentries);
1604 /*@-compdestroy@*/ sfree (s); /*@=compdestroy@*/
1608 ** converts usymId from old table to sorted one
1612 usymtab_convertId (usymId uid)
1613 /*@globals oldtab, utab@*/
1619 llassert (usymtab_isDefined (oldtab));
1621 ue = usymtab_getEntryAux (oldtab, uid);
1623 llassertprint (uentry_isValid (ue), ("convertId: undefined: %d", uid));
1625 name = uentry_rawName (ue);
1627 ret = usymtab_getIndex (utab, name);
1628 llassert (ret == uid); /*! for now, no rehash! */
1629 DPRINTF (("Convert: %s [%d] -> %s [%d]",
1630 uentry_unparse (ue), uid,
1631 uentry_unparse (utab->entries[ret]), ret));
1633 llassertprint (ret != USYMIDINVALID, ("convertId: return is invalid"));
1639 usymtab_prepareDump (void)
1640 /*@globals oldtab, utab@*/
1641 /*@modifies oldtab, utab@*/
1643 llassert (usymtab_inGlobalScope ());
1644 llassert (oldtab == usymtab_undefined);
1647 DPRINTF (("Preparing to dump:"));
1648 usymtab_printAll ();
1651 oldtab = usymtab_shallowCopy (utab);
1654 ** alpha compare - make sure order is same on different platforms
1655 ** error messages appear in same order
1659 qsort (utab->entries, (size_t)utab->nentries,
1660 sizeof (*utab->entries),
1661 (int (*)(const void *, const void *)) uentry_xcomparealpha);
1663 usymtab_rehash (utab);
1667 DPRINTF (("After rehash:"));
1668 usymtab_printAll ();
1672 void usymtab_dump (FILE *fout)
1673 /*@globals utab, oldtab@*/
1676 bool neednl = FALSE;
1677 uentry lastentry = uentry_undefined;
1678 ekind lastekind = KINVALID;
1682 ** must call prepareDump first
1685 llassert (oldtab != usymtab_undefined);
1687 for (i = 0; i < utab->nentries; i++)
1689 uentry thisentry = utab->entries[i];
1690 ekind thisekind = uentry_getKind (thisentry);
1692 if (!uentry_hasRealName (thisentry))
1694 llassert (uentry_isGlobalMarker (thisentry));
1698 check (fputc ('\n', fout) == (int) '\n');
1701 fprintf (fout, "*%d (GlobalMarker)\n", KGLOBALMARKER);
1702 lastekind = KINVALID;
1708 if (thisekind != lastekind)
1712 check (fputc ('\n', fout) == (int) '\n');
1716 lastentry = uentry_undefined;
1717 fprintf (fout, "*%d (%s)\n", ekind_toInt (thisekind),
1718 cstring_toCharsSafe (ekind_capName (thisekind)));
1719 lastekind = thisekind;
1724 ** evans - 2001-02-18 - added the - 48 fudge factor...
1725 ** extra characters dumped, but I haven't counded them carefully...
1728 if (uentry_isInvalid (lastentry) || !uentry_equiv (lastentry, thisentry)
1729 || (linelen > (MAX_DUMP_LINE_LENGTH - (2 * MAX_NAME_LENGTH) - 48)))
1733 DPRINTF (("Dumping entry: %d", i));
1734 cdump = message ("^%d %q", i, uentry_dump (thisentry));
1735 /* was: cdump = uentry_dump (thisentry)); */
1737 lastentry = thisentry;
1740 check (fputc ('\n', fout) == (int) '\n');
1744 linelen += cstring_length (cdump);
1746 /* no new line here! */
1747 if (cstring_length (cdump) > 0)
1749 check (fputs (cstring_toCharsSafe (cdump), fout) != EOF);
1752 cstring_free (cdump);
1757 cstring cdump = uentry_rawName (thisentry);
1758 DPRINTF (("Raw name: %s", cdump));
1759 linelen += (cstring_length (cdump) + 1);
1760 fprintf (fout, "#%s", cstring_toCharsSafe (cdump));
1767 check (fputc ('\n', fout) == (int) '\n');
1770 lastekind = KINVALID;
1772 fprintf(fout, ";; Library constraints\n");
1773 for (i = 0; i < utab->nentries; i++)
1775 uentry thisentry = utab->entries[i];
1777 if (uentry_isFunction (thisentry) )
1779 constraintList preconditions;
1780 constraintList postconditions;
1782 preconditions = uentry_getFcnPreconditions (thisentry);
1783 postconditions = uentry_getFcnPostconditions (thisentry);
1785 if ( constraintList_isDefined(preconditions) ||
1786 constraintList_isDefined(postconditions) )
1788 fprintf(fout,"%s\n", cstring_toCharsSafe (uentry_rawName(thisentry) ) );
1789 if (constraintList_isDefined(preconditions) )
1791 fprintf(fout,"pre:\n");
1792 constraintList_dump(preconditions, fout);
1793 fprintf (fout, ";; end precondition constraints\n" );
1794 constraintList_free(preconditions);
1798 fprintf(fout,"pre:EMPTY\n");
1800 if (constraintList_isDefined(postconditions) )
1802 fprintf(fout,"post:\n");
1803 constraintList_dump(postconditions, fout);
1804 fprintf (fout, ";; end precondition constraints\n" );
1805 constraintList_free(postconditions);
1809 fprintf(fout,"post:EMPTY\n");
1820 void usymtab_load (FILE *f)
1821 /*@globals utab, globtab@*/
1822 /*@modifies utab, *f@*/
1824 char *s = mstring_create (MAX_DUMP_LINE_LENGTH);
1826 ekind kind = KINVALID;
1827 fileloc loc = g_currentloc;
1833 llassert (utab == globtab);
1834 llassert (utab->nentries == 0);
1836 while (((s = reader_readLine (f, s, MAX_DUMP_LINE_LENGTH)) != NULL)
1839 /* ignore ; comments */ ;
1842 while (s != NULL && *s != ';')
1850 ek = reader_getInt (&s);
1852 if (ek == KGLOBALMARKER)
1854 uentry lue = uentry_makeGlobalMarker ();
1855 DPRINTF (("Adding global marker: %s", uentry_unparseFull (lue)));
1856 usymtab_addEntryAlways (utab, lue);
1862 kind = ekind_fromInt (ek);
1870 (cstring_makeLiteral
1871 ("Library is in obsolete format. Use lclint +whichlib "
1872 "to see which library is being loaded."));
1875 if (reader_optCheckChar (&s, '^'))
1877 index = reader_getInt (&s);
1884 llassert (kind != KINVALID);
1885 ue = uentry_undump (kind, loc, &s);
1887 llassert (utab->nentries == index || index == -1);
1889 if (uentry_isValid (ue))
1891 int lastindex = utab->nentries;
1892 ue = usymtab_addEntryAlways (utab, ue);
1893 if (utab->nentries != lastindex + 1)
1895 DPRINTF (("No add: %s", uentry_unparseFull (ue)));
1903 ** now, any other names are the same uentry
1906 while (*(s++) == '#')
1908 cstring name = cstring_fromCharsO (reader_getWord (&s));
1909 uentry nue = uentry_nameCopy (name, ue);
1911 DPRINTF (("Name copy: %s", uentry_unparseFull (nue)));
1915 usymtab_addEntryAlways (utab, nue);
1918 while ((c = *s) != '\0' && (c !='\n'))
1920 if (c != ' ' || c != '\t')
1922 llbuglit ("Junk in load file");
1930 s = reader_readLine (f, os, MAX_DUMP_LINE_LENGTH);
1935 to handle reading of buffer overflow related constraints
1937 while (fgets (s, MAX_DUMP_LINE_LENGTH, f) != NULL
1940 ; /* ignore ;-comments */
1943 while (s != NULL && *s != ';')
1945 constraintList preconditions;
1946 constraintList postconditions;
1948 cstring name = cstring_fromChars(reader_getWord(&s) );
1950 ue = usymtab_lookup ( name );
1954 preconditions = constraintList_undefined;
1955 postconditions = constraintList_undefined;
1957 if (!uentry_isValid(ue) )
1959 llfatalbug ((message("Invalid uentry for %s library file may be corrupted", cstring_fromChars(s) ) ));
1961 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
1963 temp = cstring_fromChars (reader_getWord(&s) );
1965 if (cstring_compareLit (temp,"pre:") == 0 )
1967 preconditions = constraintList_undump (f);
1971 if (cstring_compareLit (temp,"pre:EMPTY") != 0 )
1972 llfatalbug ((message("Error reading library file pre:EMPTY expected but got %s", temp ) ));
1977 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
1979 temp = cstring_fromChars(reader_getWord(&s) );
1980 if (cstring_compareLit (temp, "post:") == 0 )
1982 postconditions = constraintList_undump (f);
1986 if (cstring_compareLit (temp, "post:EMPTY") != 0 )
1987 llfatalbug ((message("Error reading library file post:EMPTY expected but got %s", temp ) ));
1990 cstring_free (temp);
1992 uentry_setPreconditions (ue, functionConstraint_createBufferConstraint (preconditions));
1993 uentry_setPostconditions (ue, functionConstraint_createBufferConstraint (postconditions));
1995 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
2003 ** file scope for static variables
2007 usymtab_enterFile ()
2008 /*@globals utab, globtab, filetab@*/
2009 /*@modifies filetab@*/
2011 llassert (utab == globtab);
2016 usymtab_entries (globtab, ue)
2018 if (sRef_hasDerived (uentry_getSref (ue)))
2020 fprintf (g_msgstream, "Derived Global: %s\n", uentry_unparse (ue));
2021 fprintf (g_msgstream, "sRef: %s\n", sRef_unparseFull (ue->sref));
2023 } end_usymtab_entries ;
2027 usymtab_enterScope ();
2033 /*@globals utab, filetab@*/
2034 /*@modifies filetab, utab@*/
2037 llassert (utab->lexlevel == 1);
2039 usymtab_exitScope (exprNode_undefined);
2044 usymtab_enterScope ()
2045 /*@globals utab, globtab, filetab@*/
2048 usymtab t = usymtab_create (US_NORMAL, utab, TRUE);
2050 /* unconditional scope: optimize to avoid copy */
2051 t->aliases = aliasTable_copy (utab->aliases);
2054 llassert (usymtab_isDefined (t->env));
2056 if (t->env->lexlevel == paramsScope && context_inFunctionLike ())
2058 noshadowerror = TRUE;
2059 usymtab_handleParams ();
2060 noshadowerror = FALSE;
2065 ** setup external references:
2066 ** o only, unique params alias external args
2067 ** o other params may alias anything of their type
2071 usymtab_handleParams (void)
2072 /*@globals utab, globtab, filetab@*/
2073 /*@modifies utab, globtab@*/
2075 usymtab ptab = utab->env;
2076 uentry fcn = context_getHeader ();
2078 usymtab_entries (ptab, param)
2082 if (!uentry_isYield (param))
2085 sRef pref = uentry_getSref (param);
2087 /* Could be a global. */
2089 if (uentry_isAnyParam (param))
2091 ue = uentry_makeVariable (fixParamName (uentry_rawName (param)),
2092 uentry_getType (param),
2093 fileloc_copy (uentry_whereDeclared (param)),
2096 uentry_copyState (ue, param);
2097 uentry_setRefParam (ue);
2099 ue = usymtab_supEntrySrefReturn (ue);
2101 /* must be after supercede! */
2103 if (!sRef_stateKnown (pref))
2105 uentry_setDefState (ue, SS_DEFINED);
2106 uentry_setDefState (param, SS_DEFINED);
2110 if (sRef_isStateSpecial (pref))
2112 uentry_setDefState (ue, SS_ALLOCATED);
2116 uentry_setDefState (ue, sRef_getDefState (pref));
2120 uref = uentry_getSref (ue);
2122 if (sRef_isStack (uref))
2124 alkind pkind = sRef_getAliasKind (pref);
2126 if (alkind_isKnown (pkind) && !alkind_isLocal (pkind)
2127 && !alkind_isStack (pkind))
2129 sRef_setAliasKind (uref, pkind, fileloc_undefined);
2130 sRef_setOrigAliasKind (uref, pkind);
2134 sRef_setAliasKind (uref, AK_IMPTEMP, fileloc_undefined);
2135 sRef_setOrigAliasKind (uref, AK_IMPTEMP);
2137 if (uentry_isOut (param))
2143 sRef_setDefined (uref, fileloc_undefined);
2149 usymtab_addMustAlias (uref, pref);
2151 if (!(uentry_isOnly (param) || uentry_isUnique (param)))
2154 ** This is needed for detecting possibly aliased parameters.
2157 sRef s = sRef_makeExternal (uref);
2158 usymtab_addMustAlias (uref, s);
2161 if (sRef_isKillRef (pref))
2163 sRef_setAliasKind (uref, AK_NEWREF, fileloc_undefined);
2164 sRef_setOrigAliasKind (uref, AK_KILLREF);
2166 else if (sRef_isRefCounted (uref))
2168 sRef_setOrigAliasKind (uref, AK_REFCOUNTED);
2173 sRef_setOrigAliasKind (uref, AK_LOCAL);
2180 } end_usymtab_entries;
2183 if (uentry_hasStateClauseList (fcn))
2185 stateClauseList clauses = uentry_getStateClauseList (fcn);
2187 stateClauseList_preElements (clauses, cl)
2189 fileloc loc = stateClause_loc (cl);
2190 sRefSet osrs = sRefSet_undefined;
2193 if (stateClause_isGlobal (cl))
2195 DPRINTF (("Global Marker: %s",
2196 sRef_unparseFull (usymtab_lookupGlobalMarker ())));
2197 llassert (sRef_isGlobalMarker (usymtab_lookupGlobalMarker ()));
2198 srs = sRefSet_single (usymtab_lookupGlobalMarker ());
2203 srs = stateClause_getRefs (cl);
2206 sRefSet_elements (srs, el)
2208 sRef base = sRef_getRootBase (el);
2209 sRef sb = sRef_updateSref (el);
2211 if (sRef_isResult (base))
2213 ; /* nothing to do before */
2215 else if (sRef_isParam (base) || sRef_isGlobalMarker (base))
2217 if (stateClause_setsMetaState (cl))
2219 /* copied from exprNode.c:3040 */
2220 qual ql = stateClause_getMetaQual (cl);
2221 annotationInfo ainfo = qual_getAnnotationInfo (ql);
2222 metaStateInfo minfo = annotationInfo_getState (ainfo);
2223 cstring key = metaStateInfo_getName (minfo);
2224 int mvalue = annotationInfo_getValue (ainfo);
2226 DPRINTF (("Sets meta state! %s", stateClause_unparse (cl)));
2228 if (sRef_isResult (base))
2234 sRef_setMetaStateValueComplete (sb, key, mvalue, loc);
2239 sRefMod modf = stateClause_getEntryFunction (cl);
2243 sRefSet aliases = usymtab_allAliases (sb);
2247 sRefSet_elements (aliases, sr)
2250 } end_sRefSet_elements ;
2252 sRefSet_free (aliases);
2258 if (sRef_isValid (base))
2260 DPRINTF (("Base: %s", sRef_unparseFull (base)));
2264 } end_sRefSet_elements ;
2265 } end_stateClauseList_preElements ;
2270 usymtab_enterFunctionScope (uentry fcn)
2271 /*@globals utab, filetab, globtab@*/
2274 usymtab t = usymtab_create (US_NORMAL, utab, TRUE);
2276 if (utab->lexlevel != fileScope)
2278 if (utab->lexlevel > fileScope)
2280 llparseerror (cstring_makeLiteral ("New function scope inside function."));
2282 while (utab->lexlevel > fileScope)
2284 /*@i@*/ utab = usymtab_dropEnv (utab);
2291 llfatalbug (cstring_makeLiteral ("New function not inside file."));
2293 /*@-branchstate@*/ } /*@=branchstate@*/
2297 DPRINTF (("Globs: %s", globSet_unparse (uentry_getGlobs (fcn))));
2299 globSet_allElements (uentry_getGlobs (fcn), el)
2301 DPRINTF (("Here we go: %s", sRef_unparseFull (el)));
2303 if (sRef_isUndefGlob (el))
2305 int index = sRef_getScopeIndex (el);
2306 sRef sr = sRef_updateSref (el);
2307 fileloc loc = uentry_whereEarliest (fcn);
2309 DPRINTF (("update: %s", sRef_unparseFull (sr)));
2310 DPRINTF (("Undef!"));
2311 if (sRef_isFileStatic (el))
2313 ctype ct = sRef_getType (el);
2316 llassert (usymtab_isDefined (filetab));
2318 ue = usymtab_fetchIndex (filetab, index);
2320 if (ctype_isRealArray (ct) || ctype_isRealSU (ct))
2322 sRef_setAllocated (sr, loc);
2326 sRef_setUndefined (sr, loc);
2331 uentry ue = globtab->entries[index];
2332 ctype ct = uentry_getType (ue);
2334 if (ctype_isArray (ct) || ctype_isSU (ct))
2336 sRef_setAllocated (sr, loc);
2340 sRef_setUndefined (sr, loc);
2344 else if (sRef_isAllocated (el))
2346 sRef sr = sRef_updateSref (el);
2347 fileloc loc = uentry_whereEarliest (fcn);
2349 sRef_setAllocated (sr, loc);
2351 else if (sRef_isPartial (el))
2353 sRef sr = sRef_updateSref (el);
2354 fileloc loc = uentry_whereEarliest (fcn);
2356 sRef_setPartial (sr, loc);
2361 sRef sr = sRef_updateSref (el);
2362 fileloc loc = uentry_whereEarliest (fcn);
2364 sRef_setDefined (sr, loc);
2368 /* shouldn't need to do anything! */
2370 } end_globSet_allElements;
2372 DPRINTF (("Globs after: %s", globSet_unparse (uentry_getGlobs (fcn))));
2376 usymtab_caseBranch (void)
2379 usymtab t = usymtab_create (US_CBRANCH, utab, FALSE);
2384 usymtab_switchBranch (/*@unused@*/ exprNode s)
2387 usymtab t = usymtab_create (US_SWITCH, utab, FALSE);
2389 t->aliases = aliasTable_copy (utab->aliases);
2394 usymtab_trueBranch (/*@only@*/ guardSet guards)
2397 usymtab t = usymtab_create (US_TBRANCH, utab, FALSE);
2400 ** not true! (could be in a macro)
2402 ** llassertprint (utab->lexlevel > paramsScope,
2403 ** ("not in scope: %s", usymtab_unparseLocal ()));
2407 guardSet_free (t->guards);
2410 aliasTable_free (t->aliases);
2411 t->aliases = aliasTable_copy (utab->aliases);
2419 ** { int a; if (...) a = 3; < a may be undefined here!
2424 usymtab_popTrueBranch (exprNode pred, exprNode expr, clause cl) /*@modifies utab@*/
2427 ** add a false branch
2428 ** (could be done more efficiently as a special case, but
2429 ** it is better to only maintain one version of the code)
2432 if (utab->kind != US_TBRANCH
2433 && context_inIterDef ())
2435 usymtab_exitScope (expr);
2439 DPRINTF (("pop true branch.."));
2440 usymtab_altBranch (guardSet_invert (exprNode_getGuards (pred)));
2441 usymtab_popBranches (pred, expr, exprNode_undefined, TRUE, cl);
2446 usymtab_popCaseBranch () /*@modifies utab@*/
2448 llassert (utab->kind == US_CBRANCH);
2449 usymtab_quietPlainExitScope ();
2453 usymtab_popTrueExecBranch (exprNode pred, exprNode expr, clause cl)
2456 ** add a false branch that must return --- that is,
2457 ** the true branch is always executed!
2460 usymtab_altBranch (guardSet_invert (exprNode_getGuards (pred)));
2461 usymtab_popBranches (pred, expr, exprNode_makeMustExit (), FALSE, cl);
2465 usymtab_popOrBranch (exprNode pred, exprNode expr)
2469 usymtab env = utab->env;
2470 usymtab otab = utab;
2473 llassert (env != NULL);
2475 if (exprNode_isError (expr))
2481 mustReturn = exprNode_mustEscape (expr);
2485 llassert (utab->kind == US_TBRANCH);
2488 ** merge each entry in table with its original
2489 ** unless execution cannot continue after this branch
2492 for (i = 0; i < utab->nentries; i++)
2494 uentry current = utab->entries[i];
2495 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
2497 uentry_mergeState (old, current, exprNode_loc (expr),
2498 mustReturn, FALSE, TRUE, ORCLAUSE);
2504 env->guards = guardSet_levelUnionFree (env->guards,
2505 guardSet_invert (exprNode_getGuards (pred)),
2510 env->aliases = aliasTable_levelUnion (env->aliases, otab->aliases, env->lexlevel);
2513 /* env is now utab */
2514 usymtab_quietPlainExitScope ();
2518 ** case syntax in C is very unrestricted. This is unfortunate.
2520 ** A switch case is ended either by a new case or default, or
2521 ** a close } that may close the switch or some other control
2526 usymtab_newCase (/*@unused@*/ exprNode pred, exprNode last)
2529 bool mustBreak = usymtab_mustBreak (utab);
2530 bool mustReturn = usymtab_mustEscape (utab);
2531 usymtab stab = utab;
2534 ** Find last case (or outer switch)
2537 while (stab->kind != US_CBRANCH && stab->kind != US_SWITCH)
2540 llassert (stab != GLOBAL_ENV);
2545 while (stab->kind == US_CBRANCH)
2548 llassert (stab != GLOBAL_ENV);
2552 ** if its a fall through case, merge in outside entries and last case.
2557 ** case 1: x = 3; <fall through>
2558 ** case 2: << x may not be defined
2562 if (!mustBreak && !mustReturn && utab->kind == US_CBRANCH)
2564 llassert (stab->kind == US_SWITCH || stab->kind == US_NORMAL);
2566 usymtab_entries (utab, ue) /* but, keep track of used variables */
2568 uentry old = usymtab_lookupAux (stab, uentry_rawName (ue));
2570 llassert (uentry_isValid (old));
2572 /* modifies ue, not old */
2574 uentry_mergeState (ue, old, exprNode_loc (last),
2575 FALSE, FALSE, TRUE, CASECLAUSE);
2576 } end_usymtab_entries;
2578 utab->aliases = aliasTable_levelUnion (utab->aliases,
2579 stab->aliases, utab->lexlevel);
2582 ** No need for a new branch.
2589 usymtab_caseBranch ();
2590 /*@-mustfree@*/ /*< utab->aliases >*/
2591 utab->aliases = aliasTable_copy (stab->aliases);
2599 ** for && (both pred and expr are executed)
2603 usymtab_popAndBranch (exprNode pred, /*@unused@*/ exprNode expr)
2606 usymtab env = utab->env;
2610 llassert (utab->kind == US_TBRANCH);
2613 ** merge each entry in table with its original
2614 ** unless execution cannot continue after this branch
2617 for (i = 0; i < utab->nentries; i++)
2619 uentry current = utab->entries[i];
2620 sRef tref = uentry_getSref (current);
2621 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
2622 sRef oref = uentry_getSref (old);
2624 /* note that is current is in a nested branch,
2625 it may create a "new" old entry. */
2627 llassert (uentry_isValid (old));
2628 uentry_mergeState (old, current, exprNode_loc (expr),
2629 FALSE, FALSE, TRUE, ANDCLAUSE);
2632 ** if is it defined by the second clause, then it should be defined.
2635 if (sRef_isAnyDefined (tref)
2636 && (sRef_isAllocated (oref) || sRef_isStateUndefined (oref)))
2638 sRef_setDefined (oref, g_currentloc);
2642 utab->guards = guardSet_levelUnionFree (utab->guards,
2643 guardSet_invert (exprNode_getGuards (pred)),
2645 utab->aliases = aliasTable_levelUnion (utab->aliases, otab->aliases, utab->lexlevel);
2647 usymtab_quietPlainExitScope ();
2652 ** Stack should be [ US_CBRANCH+ US_SWITCH ]
2653 ** Only branches which do not return (except possibly the last branch) are included.
2655 ** Conditionally merge state from all CBRANCHes.
2657 ** If allpaths is TRUE, then all possible executions go through some switch
2658 ** case, and the original scope is not merged.
2662 usymtab_exitSwitch (/*@unused@*/ exprNode sw, bool allpaths)
2665 usymtab ttab = utab;
2666 usymtab stab = ttab;
2667 usymtab ltab = ttab;
2668 bool lastMustReturn = usymtab_mustEscape (utab);
2672 while (stab->kind == US_CBRANCH)
2675 llassert (stab != GLOBAL_ENV);
2678 while (stab->kind == US_NORMAL)
2681 llassert (stab != GLOBAL_ENV);
2684 llassert (stab->kind == US_SWITCH);
2686 /* go to the scope outside the switch (US_SWITCH is just a marker! */
2688 llassert (stab != GLOBAL_ENV);
2692 llassert (usymtab_isDefined (ttab));
2694 if (ttab->kind == US_CBRANCH)
2696 /* was quietPlainExitScope --- but, can't free it yet! */
2698 llassert (utab != GLOBAL_ENV);
2700 while (ttab->kind == US_CBRANCH)
2703 ** (from popTrueBranch)
2706 bool mustReturn = usymtab_mustEscape (ttab);
2707 bool mustBreak = usymtab_mustBreak (ttab);
2709 usymtab_entries (ttab, current)
2711 uentry old = /*@-compmempass@*/ usymtab_lookupAux (ltab, uentry_rawName (current));
2715 ** note that is this is in a nested branch,
2716 ** it may create a "new" old entry.
2719 if (uentry_isValid (old))
2723 uentry_mergeUses (current, old);
2724 uentry_setState (old, current);
2728 uentry_mergeState (old, current, exprNode_loc (sw),
2729 mustReturn, FALSE, TRUE, SWITCHCLAUSE);
2736 } end_usymtab_entries;
2739 ** if entry is not in symbol table for this case, merge with pre-switch
2743 if (!mustReturn && !mustBreak)
2745 usymtab_entries (stab, current)
2747 if (usymtab_getIndex (ttab, uentry_rawName (current)) == NOT_FOUND)
2749 uentry old = /*@-compmempass@*/
2750 usymtab_lookupAux (ltab, uentry_rawName (current));
2753 llassert (uentry_isValid (old));
2754 uentry_mergeState (old, current, exprNode_loc (sw),
2755 FALSE, FALSE, TRUE, SWITCHCLAUSE);
2757 } end_usymtab_entries;
2760 ltab->env = ttab->env;
2764 ** Suprious error, becuase of environments.
2767 /*@i1@*/ utab = ltab;
2769 lastMustReturn = FALSE;
2776 ** now, there is one US_CBRANCH. Merge this with the stab.
2780 for (i = 0; i < ltab->nentries; i++)
2782 uentry current = ltab->entries[i];
2783 uentry old = usymtab_lookupAux (stab, uentry_rawName (current));
2785 /* note that is this is in a nested branch,
2786 it may create a "new" old entry. */
2789 if (uentry_isValid (old))
2793 uentry_mergeUses (current, old);
2794 uentry_setState (old, current);
2798 uentry_mergeState (old, current, exprNode_loc (sw),
2799 FALSE, FALSE, TRUE, SWITCHCLAUSE);
2813 ** switch may or may not be followed by a new scope
2816 if (utab->kind == US_SWITCH)
2818 usymtab_quietPlainExitScope ();
2822 usymtab_quietPlainExitScope ();
2823 llassert (utab->kind == US_SWITCH);
2824 usymtab_quietPlainExitScope ();
2830 updateNullState (sRef el, /*@notnull@*/ usymtab ttab,
2831 /*@notnull@*/ usymtab ftab, bool trueGuard)
2833 sRef base = sRef_getRootBase (el);
2834 int level = sRef_lexLevel (base);
2836 if (sRef_isCvar (base))
2838 usymId index = sRef_getScopeIndex (base);
2839 uentry ue = usymtab_getRefTab (ttab, level, index);
2841 if (!uentry_isLset (ue))
2843 sRef sr = uentry_getSref (ue);
2847 sRef_setDerivNullState (sr, el, NS_NOTNULL);
2851 if (!guardSet_isGuarded (ttab->guards, el)
2852 && !sRef_isNotNull (sr))
2854 sRef_setDerivNullState (sr, el, NS_DEFNULL);
2862 ue = usymtab_getRefTab (ftab, level, index);
2864 if (!uentry_isLset (ue))
2866 sRef sr = uentry_getSref (ue);
2869 if (!trueGuard) /* yikes! forgot the ! */
2871 sRef_setDerivNullState (sr, el, NS_NOTNULL);
2876 if (!guardSet_isGuarded (ftab->guards, el)
2877 && !sRef_isNotNull (sr))
2879 sRef_setDerivNullState (sr, el, NS_DEFNULL);
2891 usymtab_popBranches (exprNode pred, exprNode tbranch, exprNode fbranch,
2892 bool isOpt, clause cl)
2896 usymtab ftab = utab;
2897 usymtab ttab = utab->env;
2901 guardSet guards = exprNode_getGuards (pred);
2902 sRefSet tguards = guardSet_getTrueGuards (guards);
2903 sRefSet fguards = guardSet_getFalseGuards (guards);
2904 bool mustReturnT = exprNode_mustEscape (tbranch);
2905 bool mustReturnF = exprNode_mustEscape (fbranch);
2907 DPRINTF (("Pop branches: %s [mustreturn: %s/%s]", exprNode_unparse (pred),
2908 bool_unparse (mustReturnT),
2909 bool_unparse (mustReturnF)));
2911 if (exprNode_isDefined (fbranch))
2913 loc = exprNode_loc (fbranch);
2917 loc = exprNode_loc (tbranch);
2920 llassert (usymtab_isDefined (ttab));
2924 llassert (usymtab_isDefined (env));
2925 llassert (ftab->kind == US_FBRANCH);
2926 llassert (ttab->kind == US_TBRANCH);
2929 ** For each element that is true guarded (i.e., if (x != NULL))
2930 ** make x = null in false branch,
2931 ** and x = notnull in true branch.
2932 ** unless x was set locally in that branch.
2933 ** For each element that is false guarded (x == NULL)
2934 ** make x = null in true, notnull in false.
2936 ** For each element that is either guarded (pred(x))
2940 sRefSet_allElements (tguards, el)
2942 updateNullState (el, ttab, ftab, TRUE);
2943 } end_sRefSet_allElements;
2945 sRefSet_allElements (fguards, el)
2947 updateNullState (el, ttab, ftab, FALSE);
2948 } end_sRefSet_allElements;
2952 ** if an entry is in both true and false, merge the entries,
2953 ** then replace original with new state.
2955 ** if an entry is in one table, merge it with the original.
2958 DPRINTF (("ftab: %d", ftab->nentries));
2960 for (i = 0; i < ftab->nentries; i++)
2962 uentry fthis = ftab->entries[i];
2963 uentry old = usymtab_lookupAux (env, uentry_rawName (fthis));
2964 int tindex = usymtab_getIndex (ttab, uentry_rawName (fthis));
2966 DPRINTF (("Entry: %s / %s", uentry_unparseFull (fthis), uentry_unparseFull (old)));
2968 if (uentry_isUndefined (old))
2970 /* possible entry was added as an undefined id */
2971 DPRINTF (("Undefined! %s", uentry_rawName (fthis)));
2975 if (tindex != NOT_FOUND)
2977 uentry tthis = ttab->entries[tindex];
2979 /* note that is this is in a nested branch,
2980 it may create a "new" old entry. */
2986 uentry_mergeState (fthis, tthis, loc,
2987 mustReturnT, FALSE, FALSE, cl);
2991 uentry_mergeUses (fthis, tthis);
2994 uentry_setState (old, fthis);
3001 uentry_setState (old, tthis);
3002 uentry_mergeState (old, fthis, loc, mustReturnF,
3006 ttab->entries[tindex] = uentry_undefined;
3007 uentry_free (tthis);
3011 uentry_mergeState (old, fthis, loc, mustReturnF, TRUE, FALSE, cl);
3015 for (i = 0; i < ttab->nentries; i++)
3017 uentry current = ttab->entries[i];
3019 DPRINTF (("ttab: %s", uentry_unparseFull (current)));
3021 if (!uentry_isUndefined (current))
3023 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
3025 DPRINTF (("Old: %s", uentry_unparseFull (old)));
3027 if (uentry_isUndefined (old))
3029 llcontbug (message ("Undefined entry: %s", uentry_rawName (current)));
3035 uentry_mergeUses (current, old);
3036 uentry_setState (old, current);
3041 ** Assumes false branch is a fall-through if
3042 ** fbranch is not defined. This is true, unless
3043 ** where was some greivous error in processing
3044 ** the else branch of an if-then, in which case
3045 ** this is probably the right thing to do anyway.
3048 uentry_mergeState (old, current, loc, mustReturnT,
3052 DPRINTF (("==> %s", uentry_unparseFull (old)));
3057 ** Plain levelUnion doesn't work, since we need to use the sRef's in env->aliases
3058 ** if they are present.
3061 llassert (NOALIAS (env->aliases, ttab->aliases));
3062 llassert (NOALIAS (env->aliases, ftab->aliases));
3064 aliasTable_free (env->aliases);
3066 env->aliases = aliasTable_levelUnionNew (ttab->aliases,
3067 ftab->aliases, env->lexlevel);
3069 aliasTable_fixSrefs (env->aliases);
3071 DPRINTF (("Aliases: %s", aliasTable_unparse (env->aliases)));
3073 /* exit true and false scopes */
3074 usymtab_quietPlainExitScope ();
3075 usymtab_quietPlainExitScope ();
3079 utab->guards = guardSet_levelUnionFree
3081 guardSet_invert (exprNode_getGuards (pred)),
3087 utab->guards = guardSet_levelUnion (utab->guards,
3088 exprNode_getGuards (pred),
3092 DPRINTF (("Here."));
3095 static void usymtab_fixCases (void) /*@modifies utab@*/ {
3096 while (utab->kind == US_CBRANCH)
3098 usymtab_quietPlainExitScope ();
3101 llassert (utab->kind != US_CBRANCH);
3105 usymtab_altBranch (/*@only@*/ guardSet guards)
3109 usymtab parent = utab->env;
3111 t = usymtab_create (US_FBRANCH, utab, FALSE);
3114 ** If we are in a case, need to close it. The C syntax
3115 ** is very liberal, so this kludge is necessary.
3118 usymtab_fixCases ();
3120 DPRINTF (("Current kind: %s", usymtab_unparseStack ()));
3122 llassert (utab->kind == US_TBRANCH);
3123 llassert (parent != GLOBAL_ENV);
3125 guardSet_free (t->guards);
3128 aliasTable_free (t->aliases);
3129 t->aliases = aliasTable_copy (parent->aliases);
3135 usymtab_allDefined (void)
3136 /*@globals utab, globtab@*/
3140 llassert (utab == globtab);
3142 for (i = 0; i < utab->nentries; i++)
3144 uentry e = utab->entries[i];
3146 if (uentry_isPriv (e))
3148 ; /* no need to define it */
3152 if (context_getFlag (FLG_SPECUNDECL))
3154 fileloc sloc = uentry_whereSpecified (e);
3155 fileloc dloc = uentry_whereDeclared (e);
3157 if (fileloc_isDefined (sloc)
3158 && !uentry_isFakeTag (e)
3159 && !fileloc_isDefined (dloc))
3163 message ("%s %q specified but not declared",
3164 ekind_capName (uentry_getKind (e)),
3165 uentry_getName (e)),
3170 if (!uentry_isCodeDefined (e))
3172 fileloc dloc = uentry_whereDeclared (e);
3174 if (fileloc_isLib (dloc) || fileloc_isXHFile (dloc))
3178 else if (fileloc_isDefined (dloc))
3180 if (!uentry_isAnyTag (e))
3182 if (fileloc_isUser (dloc))
3186 message ("%s %q declared but not defined",
3187 ekind_capName (uentry_getKind (e)),
3188 uentry_getName (e)),
3190 DPRINTF (("decl: %s", uentry_unparseFull (e)));
3196 fileloc sloc = uentry_whereSpecified (e);
3198 if (fileloc_isDefined (sloc)
3199 && !fileloc_isImport (sloc)
3200 && !fileloc_isLib (sloc)
3201 && !fileloc_isPreproc (sloc)
3202 && !uentry_isFakeTag (e))
3204 if (uentry_isVariable (e) || uentry_isFunction (e))
3208 message ("%s %q specified but not declared or defined",
3209 ekind_capName (uentry_getKind (e)),
3210 uentry_getName (e)),
3217 message ("%s %q specified but not defined",
3218 ekind_capName (uentry_getKind (e)),
3219 uentry_getName (e)),
3229 void usymtab_exportHeader (void)
3234 for (i = 0; i < utab->nentries; i++)
3236 uentry ce = utab->entries[i];
3238 if (!uentry_isDatatype (ce)
3239 && !uentry_isAnyTag (ce)
3240 && !uentry_isEitherConstant (ce) && !uentry_isStatic (ce)
3241 && !uentry_isExternal (ce)
3242 && !uentry_isForward (ce))
3244 fileloc fwhere = uentry_whereDeclared (ce);
3246 if (fileloc_isUndefined (fwhere)
3247 && uentry_isFunction (ce))
3249 fwhere = uentry_whereDefined (ce);
3252 if (fileloc_isDefined (fwhere)
3253 && !fileloc_isHeader (fwhere)
3254 && !fileloc_isXHFile (fwhere)
3255 && !(fileloc_isSpecialFile (fwhere)
3256 && !context_getFlag (FLG_UNUSEDSPECIAL)))
3258 if (uentry_isVariable (ce))
3261 (FLG_EXPORTHEADERVAR,
3262 message ("%s %q exported but not declared in header file",
3263 ekind_capName (uentry_getKind (ce)),
3264 uentry_getName (ce)),
3267 uentry_showDefSpecInfo (ce, fwhere);
3272 if (!uentry_isIter (ce)
3273 && !uentry_isEndIter (ce)
3274 && !uentry_isExpandedMacro (ce))
3276 if (uentry_isFunction (ce)
3277 && cstring_equalLit (uentry_rawName (ce), "main"))
3279 ; /* no error for main */
3285 message ("%s %q exported but not declared "
3287 ekind_capName (uentry_getKind (ce)),
3288 uentry_getName (ce)),
3291 uentry_showDefSpecInfo (ce, fwhere);
3301 void usymtab_exportLocal (void)
3306 for (i = 0; i < utab->nentries; i++)
3308 uentry ce = utab->entries[i];
3310 if (!uentry_isDatatype (ce) && !uentry_isAnyTag (ce)
3311 && !uentry_isEitherConstant (ce)
3312 && !uentry_isIter (ce)
3313 && !uentry_isEndIter (ce)
3314 && !uentry_isExpandedMacro (ce)
3315 && uentry_isUsed (ce))
3317 /* check static uses */
3318 filelocList fuses = uentry_getUses (ce);
3319 fileloc mod = uentry_whereDefined (ce);
3320 bool ok = filelocList_isEmpty (fuses);
3321 fileloc fwhere = uentry_whereDeclared (ce);
3323 if (fileloc_isSpecialFile (fwhere)
3324 && !context_getFlag (FLG_UNUSEDSPECIAL))
3326 ok = TRUE; /* no errors for special files */
3330 filelocList_elements (fuses, uloc)
3332 if (fileloc_isUndefined (uloc) || !fileloc_sameModule (uloc, mod))
3335 /*@innerbreak@*/ break;
3337 } end_filelocList_elements;
3344 message ("%s exported but not used outside %s: %q",
3345 ekind_capName (uentry_getKind (ce)),
3346 fileloc_getBase (mod),
3347 uentry_getName (ce)),
3350 uentry_showDefSpecInfo (ce, fwhere);
3358 usymtab_allUsed (void)
3362 bool isFileStatic = usymtab_inFileScope ();
3363 cstring last_file = cstring_undefined;
3365 for (i = 0; i < utab->nentries; i++)
3367 bool hasError = FALSE;
3368 uentry ce = utab->entries[i];
3369 fileloc fwhere = uentry_whereDeclared (ce);
3371 if (fileloc_isUndefined (fwhere))
3373 fwhere = uentry_whereDefined (ce);
3376 if (fileloc_isInvalid (fwhere)
3377 || fileloc_isLib (fwhere)
3378 || fileloc_isBuiltin (fwhere)
3379 || ((fileloc_isSpecialFile (fwhere)
3380 || fileloc_isSpecialFile (uentry_whereDefined (ce)))
3381 && !context_getFlag (FLG_UNUSEDSPECIAL)))
3385 else if (!uentry_wasUsed (ce) && !uentry_isAnyTag (ce))
3387 cstring fname = fileloc_filename (fwhere);
3389 if (cstring_isUndefined (last_file))
3393 else if (cstring_equal (fname, last_file))
3402 if (uentry_isParam (ce))
3404 if (context_inMacro ())
3406 sRef cref = uentry_getSref (ce);
3408 if (uentry_isYield (ce))
3410 ; /* no checks (for now) */
3412 else if (sRef_isSafe (cref))
3418 if (uentry_hasRealName (ce))
3421 optgenerror (FLG_MACROPARAMS,
3422 message ("Macro parameter %q not used",
3423 uentry_getName (ce)),
3430 if (cstring_equalFree (uentry_getName (ce),
3431 cstring_makeLiteral ("...")))
3437 hasError = optgenerror (FLG_PARAMUNUSED,
3438 message ("Parameter %q not used",
3439 uentry_getName (ce)),
3444 else if (uentry_isFunction (ce) || uentry_isIter (ce))
3446 if (fileloc_isUser (fwhere))
3448 hasError = optgenerror
3450 message ("%q %q declared but not used",
3452 (uentry_isIter (ce) ? "Iterator"
3453 : (isFileStatic ? "File static function" : "Function")),
3454 uentry_getName (ce)),
3458 else if (uentry_isEndIter (ce))
3460 ; /* no error (already reported for iter */
3462 else if (uentry_isEnumConstant (ce))
3464 if (fileloc_isUser (fwhere))
3466 hasError = optgenerror
3468 message ("Enum member %q not used",
3469 uentry_getName (ce)),
3473 else if (uentry_isConstant (ce))
3475 if (fileloc_isUser (fwhere))
3477 hasError = optgenerror
3479 message ("Constant %q declared but not used",
3480 uentry_getName (ce)),
3484 else if (uentry_isDatatype (ce))
3486 if (fileloc_isUser (fwhere))
3488 hasError = optgenerror
3490 message ("Type %q declared but not used",
3491 uentry_getName (ce)),
3495 else if (!uentry_isRefParam (ce) && !uentry_isExpandedMacro (ce))
3496 { /* errors for ref params will be reported in the next scope */
3497 llassertprint (uentry_isVar (ce),
3498 ("ce: %s", uentry_unparseFull (ce)));
3500 if (ctype_isFunction (uentry_getType (ce)))
3502 if (fileloc_isUser (fwhere))
3504 hasError = optgenerror
3506 message ("%q %q declared but not used",
3508 (isFileStatic ? "File static function"
3510 uentry_getName (ce)),
3516 if (fileloc_isUser (fwhere))
3520 hasError = optgenerror
3522 message ("%q %q declared but not used",
3524 (isFileStatic ? "File static variable"
3526 uentry_getName (ce)),
3536 else if ((uentry_isDatatype (ce) || uentry_isAnyTag (ce)))
3537 { /* check all fields */
3538 ctype ct = uentry_getRealType (ce);
3541 while (ctype_isAP (ct))
3543 ct = ctype_getBaseType (ct);
3546 if (ctype_isSU (ct))
3548 uentryList fields = ctype_getFields (ct);
3550 uentryList_elements (fields, field)
3552 if (!uentry_isUsed (field))
3554 if (uentry_hasName (ce))
3556 hasError |= optgenerror
3558 message ("Field %q of %s %q declared but not used",
3559 uentry_getName (field),
3560 cstring_makeLiteralTemp
3561 (ctype_isStruct (ct) ? "structure" : "union"),
3562 uentry_getName (ce)),
3563 uentry_whereEarliest (field));
3569 ** Can't report these errors for unnamed structs.
3570 ** No way to tell when there are multiple consistent
3571 ** unnamed structure types. (Could go through table
3572 ** and mark them all unused...)
3574 hasError |= optgenerror
3576 message ("Field %q of unnamed %s declared but not used",
3577 uentry_getName (field),
3578 cstring_makeLiteralTemp
3579 (ctype_isStruct (ct) ? "structure" : "union")),
3580 uentry_whereEarliest (field));
3585 uentry_setUsed (field, fileloc_undefined);
3587 } end_uentryList_elements;
3597 if (uentry_isParam (ce) && context_inMacro ())
3599 if (fileloc_isDefined (uentry_whereSpecified (ce)))
3601 uentry_showWhereSpecified (ce);
3606 uentry_showDefSpecInfo (ce, fwhere);
3609 uentry_setUsed (ce, fileloc_undefined);
3615 checkGlobalReturn (uentry glob, sRef orig)
3617 sRef sr = uentry_getSref (glob);
3619 DPRINTF (("Check global return: %s // orig: %s // sr: %s",
3620 uentry_unparseFull (glob),
3621 sRef_unparseFull (orig),
3622 sRef_unparseFull (sr)));
3624 DPRINTF (("Is killed: %s", bool_unparse (sRef_isKilledGlob (orig))));
3626 if (context_getFlag (FLG_GLOBSTATE))
3628 DPRINTF (("Is killed: %s", sRef_unparseFull (orig)));
3630 if (sRef_isKilledGlob (orig))
3632 if (sRef_isStateUndefined (sr)
3633 || sRef_isUnuseable (sr)
3634 || sRef_isStateUnknown (sr)
3635 || sRef_isDead (sr))
3641 ctype ct = ctype_realType (uentry_getType (glob));
3643 if (ctype_isVisiblySharable (ct))
3648 ("Killed global %q (type %s) not released before return",
3649 uentry_getName (glob),
3650 ctype_unparse (ct)),
3653 sRef_showStateInfo (sr);
3658 sRef_protectDerivs ();
3659 (void) checkGlobalDestroyed (sr, g_currentloc);
3660 sRef_clearProtectDerivs ();
3666 if (sRef_isStateUndefined (sr))
3668 if (optgenerror (FLG_GLOBSTATE,
3670 ("Function returns with global %q undefined",
3671 uentry_getName (glob)),
3674 sRef_showStateInfo (sr);
3679 if (sRef_isDead (sr))
3683 message ("Function returns with global %q "
3684 "referencing released storage",
3685 uentry_getName (glob)),
3688 sRef_showStateInfo (sr);
3689 sRef_setDefState (sr, SS_UNKNOWN, fileloc_undefined);
3693 if (ctype_isRealPointer (uentry_getType (glob))
3694 && sRef_possiblyNull (sr)
3695 && !uentry_possiblyNull (glob))
3699 message ("Function returns with non-null global %q "
3700 "referencing null storage",
3701 uentry_getName (glob)),
3704 sRef_showNullInfo (sr);
3709 checkGlobReturn (glob);
3717 ** remember: check alias globals
3720 void usymtab_checkFinalScope (bool isReturn)
3723 bool mustFree = context_getFlag (FLG_MUSTFREE);
3724 bool mustDefine = context_getFlag (FLG_MUSTDEFINE);
3725 /* bool mustNotAlias = context_getFlag (FLG_MUSTNOTALIAS); */
3726 sRefSet checked = sRefSet_new ();
3727 usymtab stab = utab;
3731 ** need to check all scopes out to function parameters.
3736 for (i = 0; i < stab->nentries; i++)
3738 uentry ce = stab->entries[i];
3739 sRef sr = uentry_getSref (ce);
3740 sRef rb = sRef_getRootBase (sr);
3744 ** Shouldn't check if shadow checked in deeper scope:
3749 uentry oue = usymtab_lookupQuietNoAlt (utab, uentry_observeRealName (ce));
3751 if (!uentry_sameObject (ce, oue))
3753 DPRINTF (("Skipping outer entry: %s / %s", uentry_unparseFull (ce),
3754 uentry_unparseFull (oue)));
3755 /*@i32 what if it is one an alternate branch? */
3756 /*@innercontinue@*/ continue;
3760 DPRINTF (("Here check final scope: %s", uentry_unparseFull (ce)));
3762 if (ctype_isFunction (uentry_getType (ce)))
3764 /*@innercontinue@*/ continue;
3767 if (uentry_isAnyParam (ce)
3768 || uentry_isRefParam (ce)
3769 || sRef_isFileOrGlobalScope (rb))
3771 /* Don't do the loseref check...but should check state! */
3773 else if (sRef_isDefinitelyNull (sr)
3774 || usymtab_isDefinitelyNull (sr))
3777 ** No state reference errors for definitely null references.
3782 DPRINTF (("Lose ref: %s / %s", uentry_unparse (ce),
3783 sRef_unparseFull (sr)));
3785 tvalues = sRef_getValueTable (sr);
3787 valueTable_elements (tvalues, fkey, fval) {
3788 metaStateInfo minfo;
3789 cstring msg = cstring_undefined;
3792 minfo = context_lookupMetaStateInfo (fkey);
3793 llassert (metaStateInfo_isDefined (minfo));
3795 if (stateValue_isError (fval))
3801 DPRINTF (("Check: %s / %s / %s", fkey,
3802 metaStateInfo_unparse (minfo),
3803 stateValue_unparse (fval)));
3805 minfo = context_lookupMetaStateInfo (fkey);
3807 nval = stateCombinationTable_lookupLoseReference
3808 (metaStateInfo_getTransferTable (minfo),
3809 stateValue_getValue (fval), &msg);
3811 if (cstring_isDefined (msg))
3813 /*@i32 print extra info for assignments@*/
3814 DPRINTF (("From: %s", sRef_unparseFull (sr)));
3815 DPRINTF (("Null? %s / %s",
3816 bool_unparse (sRef_isDefinitelyNull (sr)),
3817 bool_unparse (usymtab_isGuarded (sr))));
3822 ("%s loses reference %q in invalid state %q (%s)",
3823 cstring_makeLiteralTemp (isReturn ? "Return" : "Scope exit"),
3824 uentry_getName (ce),
3825 stateValue_unparseValue (fval, minfo),
3829 stateValue_show (fval, minfo);
3833 DPRINTF (("Suppressed transfer error: %s", msg));
3837 } end_valueTable_elements;
3842 DPRINTF (("Check entry: %s", uentry_unparseFull (ce)));
3844 if (!sRefSet_member (checked, sr) && !sRef_isFileOrGlobalScope (rb))
3846 if (ctype_isRealSU (uentry_getType (ce))
3847 && !uentry_isAnyParam (ce)
3848 && !uentry_isRefParam (ce)
3849 && !uentry_isStatic (ce)
3850 && !sRef_isDependent (sr)
3851 && !sRef_isOwned (sr))
3853 sRefSet als = usymtab_allAliases (sr);
3855 if (sRefSet_isEmpty (als))
3857 checkLocalDestroyed (sr, g_currentloc);
3861 /* aliased, no problem */ ;
3867 (!uentry_isStatic (ce)
3868 && ((sRef_isNewRef (sr))
3869 || (((sRef_isOnly (sr) || sRef_isFresh (sr)
3870 || sRef_isKeep (sr) || sRef_isOwned (sr))
3871 && !sRef_isDead (sr))
3872 && (!sRef_definitelyNull (sr))
3873 && (!usymtab_isDefinitelyNull (sr)))))
3875 bool hasError = TRUE;
3877 DPRINTF (("Checking: %s", sRef_unparseFull (sr)));
3880 ** If its a scope exit, check if there is an alias.
3881 ** If so, make it only. If not, there is an error.
3886 if (canLoseReference (sr, g_currentloc))
3888 DPRINTF (("Can lose!"));
3895 if (sRef_hasLastReference (sr))
3897 sRef ar = sRef_getAliasInfoRef (sr);
3902 ("Last reference %q to %s storage %qnot %q before %q",
3904 alkind_unparse (sRef_getAliasKind (sr)),
3905 sRef_unparseOpt (ar),
3906 cstring_makeLiteral (sRef_isKeep (sr)
3907 ? "transferred" : "released"),
3908 cstring_makeLiteral (isReturn
3909 ? "return" : "scope exit")),
3912 sRef_showRefLost (sr);
3915 else if (sRef_isNewRef (sr))
3920 ("%q %q not released before %q",
3922 (alkind_isKillRef (sRef_getOrigAliasKind (sr))
3923 ? "Kill reference parameter" : "New reference"),
3924 uentry_getName (ce),
3925 cstring_makeLiteral (isReturn
3926 ? "return" : "scope exit")),
3929 sRef_showAliasInfo (sr);
3934 if (ctype_isRealSU (sRef_getType (sr)))
3936 checkStructDestroyed (sr, g_currentloc);
3940 DPRINTF (("Here we are: %s", sRef_unparseFull (sr)));
3945 ("%s storage %q not %q before %q",
3946 alkind_capName (sRef_getAliasKind (sr)),
3947 uentry_getName (ce),
3948 cstring_makeLiteral (sRef_isKeep (sr)
3949 ? "transferred" : "released"),
3950 cstring_makeLiteral (isReturn
3951 ? "return" : "scope exit")),
3954 sRef_showAliasInfo (sr);
3955 DPRINTF (("Storage: %s", sRef_unparseFull (sr)));
3966 else if (mustDefine && uentry_isOut (ce))
3968 if (!ynm_toBoolStrict (sRef_isReadable (sr)))
3972 message ("Out storage %q not defined before %q",
3973 uentry_getName (ce),
3975 (isReturn ? "return" : "scope exit")),
3978 /* uentry_showWhereDeclared (ce); */
3987 ** also check state is okay
3990 if (usymtab_lexicalLevel () > functionScope
3991 && uentry_isVariable (ce)
3992 && (sRef_isLocalVar (sr)
3993 && (sRef_isDependent (sr) || sRef_isLocalState (sr))))
3995 sRefSet ab = usymtab_aliasedBy (sr);
3997 /* should do something more efficient here */
3999 if (sRefSet_isEmpty (ab))
4001 /* and no local ref */
4002 DPRINTF (("Check lose ref: %s", uentry_unparseFull (ce)));
4017 checked = sRefSet_insert (checked, sr);
4021 llassert (usymtab_isDefined (stab->env));
4023 if (usymtab_isBranch (stab))
4025 stab = usymtab_dropEnv (stab);
4032 llassert (stab != usymtab_undefined);
4033 } while (isReturn && (stab->lexlevel >= paramsScope));
4035 sRefSet_free (checked);
4039 ** all globals are appropriately defined
4040 ** all parameters are appropriately defined
4041 ** special clauses are followed
4044 if (isReturn || (utab->lexlevel == paramsScope))
4046 uentry fcn = context_getHeader ();
4047 uentryList params = context_getParams ();
4048 globSet uglobs = context_getUsedGlobs ();
4049 globSet sglobs = context_getGlobs ();
4051 if (isReturn && context_maybeSet (FLG_GLOBALIAS))
4053 aliasTable_checkGlobs (utab->aliases);
4057 ** state clauses (ensures, defines, sets, allocates, releases)
4060 if (uentry_hasStateClauseList (fcn))
4062 stateClauseList clauses = uentry_getStateClauseList (fcn);
4064 stateClauseList_elements (clauses, cl)
4066 if (stateClause_isAfter (cl) && !stateClause_isGlobal (cl))
4068 if (stateClause_setsMetaState (cl))
4070 sRefSet rfs = stateClause_getRefs (cl);
4071 qual q = stateClause_getMetaQual (cl);
4072 annotationInfo ainfo = qual_getAnnotationInfo (q);
4073 metaStateInfo minfo = annotationInfo_getState (ainfo);
4074 cstring key = metaStateInfo_getName (minfo);
4075 int mvalue = annotationInfo_getValue (ainfo);
4077 DPRINTF (("Post meta state clause: %s", stateClause_unparse (cl)));
4079 sRefSet_elements (rfs, el)
4081 sRef base = sRef_getRootBase (el);
4083 if (sRef_isResult (base))
4086 ** This is checked for return transfers.
4090 else if (sRef_isParam (base) || sRef_isGlobalMarker (base))
4092 sRef sr = sRef_updateSref (base);
4093 sr = sRef_fixBase (el, sr);
4095 if (!sRef_checkMetaStateValue (sr, key, mvalue))
4100 ("Ensures clause not satisfied%q (state is %q): %q",
4101 sRef_isGlobalMarker (sr)
4103 : message (" by %q", sRef_unparse (sr)),
4104 stateValue_unparseValue (sRef_getMetaStateValue (sr, key),
4106 stateClause_unparse (cl)),
4109 sRef_showMetaStateInfo (sr, key);
4115 if (sRef_isMeaningful (el))
4120 } end_sRefSet_elements ;
4124 /* evs - 2000 07 10 - added this */
4125 sRefTest tst = stateClause_getPostTestFunction (cl);
4126 sRefSet rfs = stateClause_getRefs (cl);
4128 sRefSet_elements (rfs, el)
4130 sRef base = sRef_getRootBase (el);
4132 if (sRef_isResult (base))
4135 ** This is checked for return transfers.
4140 else if (sRef_isParam (base))
4142 sRef sr = sRef_updateSref (base);
4143 sr = sRef_fixBase (el, sr);
4145 if (tst != NULL && !tst (sr))
4148 (stateClause_postErrorCode (cl),
4149 message ("%s storage %qcorresponds to "
4150 "storage listed in %q clause",
4151 stateClause_postErrorString (cl, sr),
4152 sRef_unparseOpt (sr),
4153 stateClause_unparseKind (cl)),
4156 sRefShower ss = stateClause_getPostTestShower (cl);
4163 DPRINTF (("Storage: %s", sRef_unparseFull (sr)));
4169 if (sRef_isMeaningful (el))
4174 } end_sRefSet_elements ;
4177 } end_stateClauseList_elements ;
4181 ** check parameters on return
4184 uentryList_elements (params, arg)
4186 if (!uentry_isElipsisMarker (arg))
4188 ctype rt = ctype_realType (uentry_getType (arg));
4190 if (ctype_isMutable (rt) || ctype_isSU (rt))
4192 uentry param = usymtab_lookupQuiet (utab, uentry_rawName (arg));
4193 DPRINTF (("Check param return: %s", uentry_unparseFull (param)));
4194 checkParamReturn (param);
4197 } end_uentryList_elements;
4199 DPRINTF (("Check global return: %s",
4200 globSet_unparse (sglobs)));
4202 globSet_allElements (sglobs, el)
4204 sRef orig = el; /*! sRef_updateSref (el); */ /*!!!*/
4205 uentry current = sRef_getUentry (el);
4207 DPRINTF (("Check global return: %s / %s", sRef_unparseFull (el),
4208 uentry_unparseFull (current)));
4210 if (uentry_isVariable (current) && !uentry_isRealFunction (current))
4212 checkGlobalReturn (current, orig);
4214 } end_globSet_allElements;
4216 globSet_allElements (uglobs, el)
4218 if (!globSet_member (sglobs, el))
4220 uentry current = sRef_getUentry (el);
4222 if (uentry_isVariable (current)
4223 && !uentry_isRealFunction (current))
4225 checkGlobalReturn (current, sRef_undefined);
4228 } end_globSet_allElements;
4233 usymtab_quietExitScope (fileloc loc)
4234 /*@globals utab, globtab, filetab; @*/
4237 usymtab t = utab->env;
4239 if (utab->reftable != NULL)
4243 for (i = 0; i < utab->nentries; i++)
4245 uentry current = utab->entries[i];
4246 uentry old = usymtab_lookupAux (t, uentry_rawName (current));
4248 uentry_mergeState (old, current, loc, FALSE, FALSE, FALSE, NOCLAUSE);
4252 llassert (t != NULL);
4254 if (t->lexlevel > paramsScope)
4256 t->guards = guardSet_levelUnion (t->guards, utab->guards, t->lexlevel);
4257 t->aliases = aliasTable_levelUnionSeq (t->aliases, utab->aliases,
4259 utab->aliases = aliasTable_undefined;
4262 t->mustBreak = utab->mustBreak;
4263 t->exitCode = utab->exitCode;
4265 usymtab_freeLevel (utab);
4271 ** Exit a scope with no checking, lose alias states.
4272 ** (When should this be used?)
4275 void usymtab_quietPlainExitScope (void)
4276 /*@globals utab, globtab, filetab@*/
4279 usymtab t = utab->env;
4281 llassert (t != NULL);
4282 llassert (NOALIAS (utab->aliases, t->aliases));
4283 usymtab_freeLevel (utab);
4287 void usymtab_exitScope (exprNode expr)
4288 /*@globals utab, filetab, globtab@*/
4289 /*@modifies utab, globtab@*/
4291 usymtab ctab = usymtab_undefined;
4292 usymtab lctab = usymtab_undefined;
4293 bool mustReturn = exprNode_mustEscape (expr);
4295 DPRINTF (("Exit scope"));
4297 if (utab->kind == US_CBRANCH)
4300 ** save the case branches, remove the first non-cbranch
4305 while (utab->kind == US_CBRANCH)
4309 llassert (utab != GLOBAL_ENV);
4313 if (utab->kind == US_TBRANCH || utab->kind == US_FBRANCH
4314 || utab->kind == US_CBRANCH || utab->kind == US_SWITCH) {
4316 if (context_inMacro ()) {
4317 /* evs 2000-07-25 */
4318 /* Unparseable macro may end inside nested scope. Deal with it. */
4320 llerror (FLG_SYNTAX,
4321 message ("Problem parsing macro body of %s (unbalanced scopes). "
4322 "Attempting to recover, recommend /*@notfunction@*/ before "
4323 "macro definition.",
4324 context_inFunctionName ()));
4326 while (utab->kind == US_TBRANCH
4327 || utab->kind == US_FBRANCH
4328 || utab->kind == US_CBRANCH
4329 || utab->kind == US_SWITCH)
4332 llassert (utab != GLOBAL_ENV);
4335 llcontbug (message ("exitScope: in branch: %q", usymtab_unparseStack ()));
4337 } /*@=branchstate@*/
4341 ** check all variables in scope were used
4345 ** bogus errors if this is the normal inside a switch,
4346 ** since cases have not been merged yet. Should probably
4347 ** still check this, but I'm too lazy at the moment...
4350 llassertfatal (utab->env != GLOBAL_ENV);
4352 if (utab->env->kind != US_SWITCH)
4358 ** check aliasing: all only params are released (dead)
4359 ** definition: all out params are defined, all modified params
4360 ** are completely defined
4362 ** NOTE: note for exiting paramsScope, since checkReturn should be
4366 if (!mustReturn && (usymtab_lexicalLevel () > functionScope))
4369 ** should only call this is end of scope is reachable...
4372 usymtab_checkFinalScope (FALSE);
4375 if (usymtab_lexicalLevel () == paramsScope && context_inFunctionLike ())
4378 ** leaving a function, need to fix up globals
4381 uentryList params = context_getParams ();
4382 globSet globs = context_getUsedGlobs ();
4385 uentryList_elements (params, ue)
4387 uentry_fixupSref (ue);
4388 } end_uentryList_elements;
4390 clearFunctionTypes ();
4393 globSet_allElements (globs, el)
4395 if (sRef_isCvar (el))
4398 int index = sRef_getScopeIndex (el);
4400 if (sRef_isFileStatic (el))
4402 llassert (usymtab_isDefined (filetab));
4403 current = usymtab_fetchIndex (filetab, index);
4407 current = usymtab_fetchIndex (globtab, index);
4410 if (uentry_isVariable (current))
4412 uentry_fixupSref (current);
4416 sRef_clearDerived (uentry_getSref (current));
4419 } end_globSet_allElements;
4422 usymtab_quietExitScope (exprNode_loc (expr));
4424 if (lctab != usymtab_undefined)
4426 /*@i@*/ lctab->env = utab;
4427 /*@i@*/ utab = ctab;
4428 /*@-branchstate@*/ } /*@=branchstate@*/
4434 ** yikes! don't let the '170 kids see this one...
4438 uentry_directParamNo (uentry ue)
4440 if (uentry_isVar (ue))
4442 sRef sr = uentry_getSref (ue);
4444 if (sRef_lexLevel (sr) == functionScope)
4449 llassert (sr->info != NULL);
4450 llassert (sr->info->cvar != NULL);
4451 index = sr->info->cvar->index;
4454 if (index < uentryList_size (context_getParams ()))
4463 /*@dependent@*/ /*@exposed@*/ uentry
4464 usymtab_getParam (int paramno)
4468 ** requires in a function context (checked)
4470 ** depends on no nested functions --- the function
4471 ** parameters are ALWAYS one scope inside the global scope
4472 ** and entered in order!
4476 if (!context_inFunctionLike ())
4477 llfatalbug (message ("usymtab_getParam: not in function context: %q",
4478 context_unparse ()));
4480 while (s->lexlevel > paramsScope)
4485 llassert (usymtab_isDefined (s));
4487 if (paramno >= s->nentries)
4490 ** Parse errors lead to this.
4493 uentry err = uentry_makeVariableLoc (cstring_makeLiteralTemp ("<error>"),
4496 uentry_markOwned (err);
4500 return (s->entries[paramno]);
4503 static /*@dependent@*/ /*@exposed@*/ uentry
4504 usymtab_getRefTab (/*@notnull@*/ usymtab u, int level, usymId index)
4508 ue = usymtab_getRefNoisy (u, level, index);
4510 if (uentry_isUndefined (ue))
4512 llbug (message ("usymtab_getRef: out of range: %d. level = %d",
4519 static /*@dependent@*/ /*@exposed@*/ usymtab
4520 usymtab_dropEnv (/*@notnull@*/ usymtab s)
4522 if (s->kind == US_CBRANCH)
4530 llassert (s != GLOBAL_ENV);
4531 } while (s->kind == US_CBRANCH);
4532 /* drop all cases (except in nested scopes */
4535 llassert (s != GLOBAL_ENV);
4538 if (s->kind == US_FBRANCH)
4540 s = s->env; /* skip the true branch */
4541 llassert (usymtab_isDefined (s));
4542 llassert (s->kind == US_TBRANCH);
4545 llassert (s != GLOBAL_ENV);
4551 /*@dependent@*/ /*@exposed@*/ uentry
4552 usymtab_getRefQuiet (int level, usymId index)
4558 llassert (s != NULL);
4559 llassert (index >= 0);
4561 if (level > s->lexlevel)
4563 return uentry_undefined;
4566 llassertprint (level <= s->lexlevel, ("level: %d / lexlevel: %d",
4567 level, s->lexlevel));
4569 while (s->lexlevel > level)
4571 if (usymtab_isBranch (s))
4573 int eindex = refTable_lookup (s, level, index);
4575 if (eindex != NOT_FOUND)
4577 return (s->entries[eindex]);
4581 s = usymtab_dropEnv (s);
4584 while (usymtab_isBranch (s) && s->lexlevel == level)
4586 int eindex = refTable_lookup (s, level, index);
4588 if (eindex != NOT_FOUND)
4590 return (s->entries[eindex]);
4593 s = usymtab_dropEnv (s);
4596 if (index >= s->nentries)
4598 return uentry_undefined;
4601 llassert (!uentry_isUndefined (s->entries[index]));
4603 return s->entries[index];
4606 static /*@dependent@*/ /*@exposed@*/ uentry
4607 usymtab_getRefNoisy (/*@notnull@*/ usymtab s, int level, usymId index)
4610 uentry ue = uentry_undefined;
4612 llassert (index >= 0);
4614 while (s->lexlevel > level)
4616 if (usymtab_isBranch (s))
4618 int eindex = refTable_lookup (s, level, index);
4620 if (eindex != NOT_FOUND)
4622 ue = s->entries[eindex];
4626 while (!usymtab_isBranch (otab))
4628 otab = usymtab_dropEnv (otab);
4629 llassert (otab != GLOBAL_ENV);
4632 if (refTable_lookup (otab, level, index) == NOT_FOUND)
4634 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4646 s = usymtab_dropEnv (s);
4649 llassert (usymtab_isDefined (s));
4651 while (usymtab_isBranch (s) && s->lexlevel == level)
4653 int eindex = refTable_lookup (s, level, index);
4656 if (eindex != NOT_FOUND)
4658 ue = s->entries[eindex];
4662 while (!usymtab_isBranch (otab))
4664 otab = usymtab_dropEnv (otab);
4665 llassert (otab != GLOBAL_ENV);
4668 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4678 s = usymtab_dropEnv (s);
4681 if (s->lexlevel == level && (index < s->nentries))
4683 ue = s->entries[index];
4685 if (uentry_isValid (ue))
4689 while (!usymtab_isBranch (otab))
4691 otab = usymtab_dropEnv (otab);
4693 if (otab == GLOBAL_ENV)
4699 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4710 if (index >= s->nentries)
4712 return uentry_undefined;
4715 llassert (!uentry_isUndefined (s->entries[index]));
4717 return s->entries[index];
4721 ** looking up entries
4723 ** If entry is inside a branch, then copy it, and put it into
4724 ** the branch table.
4728 int refTable_lookup (/*@notnull@*/ usymtab ut, int level, int index)
4730 refTable rt = ut->reftable;
4733 llassert (rt != NULL);
4735 for (i = 0; i < ut->nentries; i++)
4737 if (rt[i]->level == level && rt[i]->index == index)
4747 /*@only@*/ refentry refentry_create (int level, int index)
4749 refentry r = (refentry) dmalloc (sizeof (*r));
4757 static /*@dependent@*/ /*@exposed@*/ uentry
4758 usymtab_addRefEntry (/*@notnull@*/ usymtab s, cstring k)
4763 if (ut->reftable == NULL)
4765 DPRINTF (("Adding ref entry without reftable: %s", k));
4766 return uentry_undefined;
4769 llassert (ut->reftable != NULL);
4771 while (s != GLOBAL_ENV)
4773 eindex = usymtab_getIndex (s, k);
4775 if (eindex != NOT_FOUND)
4777 uentry current = s->entries[eindex];
4779 if (uentry_isVar (current) && !ctype_isFunction (uentry_getType (current)))
4783 DPRINTF (("Here: copying %s", uentry_unparse (current)));
4784 ue = uentry_copy (current);
4785 DPRINTF (("Here: copying %s", uentry_unparse (ue)));
4786 usymtab_addEntryQuiet (ut, ue);
4787 DPRINTF (("Okay..."));
4789 if (s->reftable != NULL)
4791 refentry ref = s->reftable[eindex];
4793 ut->reftable[ut->nentries - 1]
4794 = refentry_create (ref->level, ref->index);
4798 ut->reftable[ut->nentries - 1]
4799 = refentry_create (s->lexlevel, eindex);
4810 s = usymtab_dropEnv (s);
4813 return uentry_undefined;
4816 static uentry usymtab_lookupAux (usymtab s, cstring k)
4818 DPRINTF (("Lookup: %s", k));
4820 while (s != GLOBAL_ENV)
4822 int eindex = usymtab_getIndex (s, k);
4824 if (eindex != NOT_FOUND)
4826 uentry ret = s->entries[eindex];
4830 if (s->kind == US_TBRANCH
4831 || s->kind == US_FBRANCH
4832 || s->kind == US_CBRANCH)
4833 /* uentry_isGlobalVariable (ret) && os->lexlevel > fileScope) */
4836 DPRINTF (("Adding global ref entry: %s", k));
4837 ret = usymtab_addRefEntry (os, k);
4838 DPRINTF (("Adding ref entry: %s", uentry_unparseFull (ret)));
4843 DPRINTF (("Found: %s", uentry_unparseFull (ret)));
4847 if (s->kind == US_TBRANCH || s->kind == US_FBRANCH
4848 || s->kind == US_CBRANCH)
4850 /* why isn't this os??? */
4851 uentry ret = usymtab_addRefEntry (s, k);
4852 DPRINTF (("Adding ref entry: %s", uentry_unparseFull (ret)));
4859 return uentry_undefined;
4862 static /*@dependent@*/ /*@exposed@*/ uentry
4863 usymtab_lookupQuietAux (usymtab s, cstring k, bool noalt)
4867 while (s != GLOBAL_ENV)
4869 eindex = usymtab_getIndex (s, k);
4871 if (eindex != NOT_FOUND)
4873 uentry ret = s->entries[eindex];
4877 if (noalt && usymtab_isBranch (s))
4879 s = usymtab_dropEnv (s);
4887 return uentry_undefined;
4890 static /*@exposed@*/ /*@dependent@*/ uentry
4891 usymtab_lookupQuiet (usymtab s, cstring k)
4893 return usymtab_lookupQuietAux (s, k, FALSE);
4896 static /*@exposed@*/ /*@dependent@*/ uentry
4897 usymtab_lookupQuietNoAlt (usymtab s, cstring k)
4899 return usymtab_lookupQuietAux (s, k, TRUE);
4902 /*@dependent@*/ /*@observer@*/ uentry
4903 usymtab_lookupSafe (cstring k)
4906 DPRINTF (("Lookup safe: %s", k));
4907 return (usymtab_lookupAux (utab, k));
4911 usymtab_lookupExpose (cstring k)
4914 uentry ce = usymtab_lookupAux (utab, k);
4916 if (uentry_isUndefined (ce))
4918 llfatalbug (message ("usymtab_lookup: not found: *%s*", k));
4921 if (uentry_isPriv (ce))
4923 llfatalbug (message ("usymtab_lookup: private: *%s*", k));
4929 uentry usymtab_lookupExposeGlob (cstring k)
4931 return (usymtab_lookupGlobSafe (k));
4934 uentry usymtab_lookupGlob (cstring k)
4935 /*@globals globtab@*/
4937 uentry ce = usymtab_lookupAux (globtab, k);
4939 if (uentry_isUndefined (ce))
4940 llfatalbug (message ("usymtab_lookup: not found: %s", k));
4942 if (uentry_isPriv (ce))
4943 llfatalbug (message ("usymtab_lookup: private: %s", k));
4948 /*@observer@*/ uentry
4949 usymtab_lookupGlobSafe (cstring k)
4950 /*@globals globtab@*/
4952 uentry ce = usymtab_lookupAux (globtab, k);
4957 uentry usymtab_lookupEither (cstring k)
4960 uentry ce = usymtab_lookupSafe (k);
4962 if (uentry_isUndefined (ce))
4963 llfatalerror (message ("usymtab_lookup: not found: %s", k));
4970 usymtab_lookupType (cstring k)
4971 /*@globals globtab@*/
4973 usymId uid = usymtab_getTypeId (k);
4975 if (uid == USYMIDINVALID)
4977 llcontbug (message ("usymtab_lookupType: not found: %s", k));
4978 return ctype_unknown;
4981 return (uentry_getRealType (usymtab_getTypeEntry (uid)));
4986 usymtab_lookupAbstractType (cstring k) /*@globals globtab@*/
4988 usymId uid = usymtab_getTypeId (k);
4990 if (uid == USYMIDINVALID)
4992 llcontbug (message ("usymtab_lookupType: not found: %s", k));
4993 return ctype_unknown;
4996 return (uentry_getAbstractType (usymtab_getTypeEntry (uid)));
5000 ** if there is an unnamed lcl-specified struct tag matching
5001 ** the uentryList, return its datatype. Otherwise, returns
5006 usymtab_structFieldsType (uentryList f)
5007 /*@globals globtab@*/
5009 return (usymtab_suFieldsType (f, TRUE));
5013 usymtab_unionFieldsType (uentryList f)
5014 /*@globals globtab@*/
5016 return (usymtab_suFieldsType (f, FALSE));
5020 usymtab_suFieldsType (uentryList f, bool isStruct)
5021 /*@globals globtab@*/
5025 DPRINTF (("Fields: %s", uentryList_unparse (f)));
5027 if (fileloc_isSpec (g_currentloc))
5029 return (ctype_undefined);
5032 for (i = 0; i < globtab->nentries; i++)
5034 uentry current = globtab->entries[i];
5037 ? uentry_isStructTag (current) : uentry_isUnionTag (current)))
5039 if (isFakeTag (uentry_rawName (current)))
5041 ctype ct = uentry_getType (current);
5043 DPRINTF (("Check: %s", ctype_unparse (ct)));
5045 if ((isStruct ? ctype_isStruct (ct) : ctype_isUnion (ct))
5047 (uentry_isSpecified (current)
5048 && uentryList_equivFields (f, ctype_getFields (ct))))
5050 return uentry_getAbstractType (current);
5060 return ctype_undefined;
5064 usymtab_enumEnumNameListType (enumNameList f)
5065 /*@globals globtab@*/
5069 for (i = 0; i < globtab->nentries; i++)
5071 uentry current = globtab->entries[i];
5073 if (uentry_isEnumTag (current))
5075 if (isFakeTag (uentry_rawName (current)))
5077 ctype ct = uentry_getType (current);
5079 if (ctype_isEnum (ct) && (enumNameList_match (f, ctype_elist (ct))))
5081 return uentry_getType (current);
5087 return ctype_undefined;
5091 usymtab_exists (cstring k)
5094 uentry ce = usymtab_lookupSafe (k);
5095 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5099 usymtab_existsReal (cstring k)
5102 uentry ce = usymtab_lookupSafe (k);
5104 return (!(uentry_isUndefined (ce))
5105 && !(uentry_isPriv (ce))
5106 && !(uentry_isExpandedMacro (ce)));
5110 usymtab_existsGlob (cstring k)
5111 /*@globals globtab@*/
5113 uentry ce = usymtab_lookupAux (globtab, k);
5115 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5120 usymtab_existsEither (cstring k)
5123 uentry ce = usymtab_lookupAux (utab, k);
5125 return (uentry_isValid (ce));
5129 usymtab_existsGlobEither (cstring k)
5130 /*@globals globtab@*/
5132 uentry ce = usymtab_lookupAux (globtab, k);
5134 return (uentry_isValid (ce));
5139 usymtab_existsType (cstring k)
5140 /*@globals globtab@*/
5142 uentry ce = usymtab_lookupAux (globtab, k);
5144 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)) && uentry_isDatatype (ce));
5148 usymtab_existsTypeEither (cstring k)
5149 /*@globals globtab@*/
5152 ce = usymtab_lookupAux (globtab, k);
5153 return (uentry_isValid (ce) && uentry_isDatatype (ce));
5157 usymtab_existsStructTag (cstring k) /*@globals globtab@*/
5159 cstring sname = makeStruct (k);
5160 uentry ce = usymtab_lookupAux (globtab, sname);
5161 cstring_free (sname);
5162 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5166 usymtab_existsUnionTag (cstring k) /*@globals globtab@*/
5168 cstring uname = makeUnion (k);
5169 uentry ce = usymtab_lookupAux (globtab, uname);
5171 cstring_free (uname);
5173 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5177 usymtab_existsEnumTag (cstring k) /*@globals globtab@*/
5179 cstring ename = makeEnum (k);
5180 uentry ce = usymtab_lookupAux (globtab, ename);
5182 cstring_free (ename);
5183 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5187 bool usymtab_existsVar (cstring k)
5190 uentry ce = usymtab_lookupSafe (k);
5192 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)) && (uentry_isVar (ce)));
5201 refTable_free (/*@only@*/ /*@null@*/ refTable x, int nentries)
5207 for (i = 0; i < nentries; i++)
5217 usymtab_freeLevel (/*@notnull@*/ /*@only@*/ usymtab u)
5218 /*@globals globtab, utab, filetab@*/
5222 aliasTable_free (u->aliases);
5224 refTable_free (u->reftable, u->nentries);
5226 if (u == filetab || u == globtab)
5228 for (i = 0; i < u->nentries; i++)
5230 uentry_freeComplete (u->entries[i]);
5235 for (i = 0; i < u->nentries; i++)
5237 uentry_free (u->entries[i]);
5241 guardSet_free (u->guards);
5248 llassert (!cstringTable_isDefined (u->htable));
5256 usymtab_freeAux (/*@only@*/ usymtab u)
5257 /*@globals globtab, utab, filetab@*/
5260 while (u != GLOBAL_ENV)
5263 usymtab_freeLevel (u);
5270 void usymtab_free ()
5271 /*@globals killed utab, globtab, filetab@*/
5275 usymtab_freeAux (utab);
5278 static int usymtab_lexicalLevel (void) /*@globals utab@*/
5280 return (utab->lexlevel);
5283 bool usymtab_inGlobalScope () /*@globals utab, globtab@*/
5285 return (utab == globtab);
5288 bool usymtab_inFileScope () /*@globals utab@*/
5290 return (utab->lexlevel == fileScope);
5293 bool usymtab_inFunctionScope () /*@globals utab@*/
5295 return (utab->lexlevel == functionScope);
5300 usymtab_replaceEntry (uentry s)
5301 /*@globals utab, globtab@*/
5302 /*@modifies utab, s@*/
5304 usymtab_replaceEntryAux (utab, s);
5309 usymtab_matchForwardStruct (usymId u1, usymId u2)
5310 /*@globals globtab@*/
5312 uentry ue1 = usymtab_getTypeEntry (u1);
5313 uentry ue2 = usymtab_getTypeEntry (u2);
5315 if (uentry_isAnyTag (ue2))
5317 ctype reptype = uentry_getType (ue1);
5319 if (ctype_isPointer (reptype))
5321 ctype repbase = ctype_getBaseType (reptype);
5323 if (ctype_isUA (repbase))
5325 typeId rtuid = ctype_typeId (repbase);
5327 if (u2 == rtuid) return TRUE;
5329 if (usymId_isValid (rtuid))
5331 reptype = uentry_getType (usymtab_getTypeEntry (rtuid));
5333 return (ctype_isUA (reptype) && (u2 == (ctype_typeId (reptype))));
5342 void usymtab_addGuards (guardSet guards)
5345 utab->guards = guardSet_union (utab->guards, guards);
5348 static bool usymtab_isGuardedAux (sRef s)
5352 sRef base = sRef_getRootBase (s);
5353 int lowlevel = paramsScope;
5354 int baselevel = sRef_lexLevel (base);
5356 if (sRef_isCvar (base))
5358 lowlevel = baselevel;
5359 if (lowlevel < paramsScope) lowlevel = paramsScope;
5362 while (tab->lexlevel >= lowlevel)
5364 if (guardSet_isGuarded (tab->guards, s))
5367 if (!sRef_definitelyNull (s))
5369 sRef_setNotNull (s, fileloc_undefined);
5375 tab = usymtab_dropEnv (tab);
5381 void usymtab_unguard (sRef s) /*@modifies utab@*/
5384 sRef base = sRef_getRootBase (s);
5385 int lowlevel = paramsScope;
5386 int baselevel = sRef_lexLevel (base);
5388 if (sRef_isCvar (base))
5390 lowlevel = baselevel;
5391 if (lowlevel < paramsScope) lowlevel = paramsScope;
5394 while (tab->lexlevel >= lowlevel)
5396 if (guardSet_isGuarded (tab->guards, s))
5398 guardSet_delete (tab->guards, s);
5401 tab = usymtab_dropEnv (tab);
5405 bool usymtab_isGuarded (sRef s)
5408 return (sRef_aliasCompleteSimplePred (usymtab_isGuardedAux, s));
5411 bool usymtab_isDefinitelyNull (sRef s)
5413 return (sRef_aliasCheckSimplePred (usymtab_isDefinitelyNullAux, s));
5416 bool usymtab_isDefinitelyNullDeep (sRef s)
5418 return (sRef_deepPred (usymtab_isDefinitelyNull, s));
5421 static bool usymtab_isDefinitelyNullAux (sRef s)
5425 sRef base = sRef_getRootBase (s);
5426 int lowlevel = paramsScope;
5428 if (sRef_isCvar (base))
5430 lowlevel = sRef_lexLevel (base);
5431 if (lowlevel < paramsScope) lowlevel = paramsScope;
5434 while (tab->lexlevel >= lowlevel)
5436 if (guardSet_mustBeNull (tab->guards, s))
5441 while (tab->kind == US_CBRANCH)
5446 llassert (usymtab_isDefined (tab));
5448 if (tab->kind == US_FBRANCH)
5451 llassert (tab->kind == US_TBRANCH);
5461 usymtab_printGuards ()
5462 /*@globals utab, globtab@*/
5464 usymtab ttab = utab;
5466 while (ttab != globtab)
5468 llmsg (message ("Guards [%d]: %q", ttab->lexlevel,
5469 guardSet_unparse (ttab->guards)));
5475 usymtab_displayAllUses ()
5476 /*@globals utab, globtab@*/
5480 /* only in top scope */
5481 llassert (utab == globtab);
5483 /* need a copy, so order is not messed up by sort! */
5484 copy = usymtab_shallowCopy (globtab);
5486 qsort (copy->entries, (size_t)copy->nentries,
5487 sizeof (*copy->entries), (int (*)(const void *, const void *)) uentry_xcompareuses);
5489 usymtab_entries (copy, ue)
5491 if (uentry_isValid (ue))
5493 filelocList uses = uentry_getUses (ue);
5494 int size = filelocList_realSize (uses);
5496 if (fileloc_isDefined (uentry_whereDefined (ue))
5497 && !fileloc_isLib (uentry_whereDefined (ue))
5500 llmsg (message ("%q (%q), %d use%&:\n %q",
5501 uentry_getName (ue),
5502 fileloc_unparse (uentry_whereDefined (ue)),
5503 size, filelocList_unparseUses (uses)));
5506 } end_usymtab_entries;
5508 usymtab_shallowFree (copy);
5511 static /*@dependent@*/ /*@exposed@*/ usymtab
5512 usymtab_getFileTab ()
5513 /*@globals filetab@*/
5515 llassert (filetab != NULL);
5521 usymtab_unparseStack ()
5524 return (usymtab_unparseStackTab (utab));
5527 static /*@only@*/ cstring
5528 usymtab_unparseStackTab (usymtab t)
5530 bool firstOne = TRUE;
5531 cstring ret = cstring_makeLiteral ("[");
5533 while (t != GLOBAL_ENV)
5537 ret = message ("%q %q", ret, usymtab_typeName (t));
5542 ret = message ("%q, %q", ret, usymtab_typeName (t));
5547 ret = message ("%q ]", ret);
5551 static /*@only@*/ cstring
5552 usymtab_typeName (/*@notnull@*/ usymtab t)
5556 case US_GLOBAL: return cstring_makeLiteral ("global");
5557 case US_NORMAL: return cstring_makeLiteral ("normal");
5558 case US_TBRANCH: return cstring_makeLiteral ("true");
5559 case US_FBRANCH: return cstring_makeLiteral ("false");
5560 case US_CBRANCH: return cstring_makeLiteral ("case");
5561 case US_SWITCH: return cstring_makeLiteral ("switch");
5567 void usymtab_addMustAlias (/*@exposed@*/ sRef s, /*@exposed@*/ sRef al)
5570 if (!sRef_similar (s, al))
5572 usymtab_addForceMustAlias (s, al);
5577 ** Same as usymtab_addMustAlias, except does not check sRef_isSimilar.
5580 void usymtab_addForceMustAlias (/*@exposed@*/ sRef s, /*@exposed@*/ sRef al)
5583 if (sRef_isMeaningful (s)
5584 && sRef_isMeaningful (al)
5585 && !(sRef_isConst (s) || sRef_isConst (al))
5586 && !(sRef_isAddress (al) && sRef_isDirectParam (sRef_getBase (al))))
5588 utab->aliases = aliasTable_addMustAlias (utab->aliases, s, al);
5589 DPRINTF (("Must alias: %s", aliasTable_unparse (utab->aliases)));
5592 ** for local variable, aliasing is symmetric
5595 if (sRef_isLocalVar (s) && sRef_isLocalVar (al))
5597 utab->aliases = aliasTable_addMustAlias (utab->aliases, al, s);
5606 void usymtab_clearAlias (sRef s)
5607 /*@modifies utab, s@*/
5610 aliasTable_clearAliases (utab->aliases, s);
5613 sRefSet usymtab_allAliases (sRef s)
5616 if (sRef_isMeaningful (s))
5620 ret = sRefSet_unionFree (aliasTable_aliasedBy (utab->aliases, s),
5621 aliasTable_canAlias (utab->aliases, s));
5626 DPRINTF (("NOT A MEANINGFUL SREF!"));
5627 return sRefSet_undefined;
5631 /*@only@*/ sRefSet usymtab_canAlias (sRef s)
5634 if (sRef_isMeaningful (s))
5636 sRefSet res = aliasTable_canAlias (utab->aliases, s);
5640 return sRefSet_undefined;
5643 /*@only@*/ sRefSet usymtab_aliasedBy (sRef s)
5646 return (aliasTable_aliasedBy (utab->aliases, s));
5649 /*@only@*/ cstring usymtab_unparseAliases ()
5652 return (aliasTable_unparse (utab->aliases));
5656 ** Debugging routines:
5657 ** okay to leak storage here, only for debugging
5663 usymtab_printOut (void)
5669 char *ind = mstring_copy (" ");
5671 fprintf (g_msgstream, "<<< [symbol table] >>>\n");
5673 while (s != GLOBAL_ENV && s->env != GLOBAL_ENV)
5675 cstring tname = usymtab_typeName (s);
5679 ind[depth * 3 + 1] = '\0';
5682 fprintf (g_msgstream, "level: %d (%s)\n", s->lexlevel,
5683 cstring_toCharsSafe (tname));
5685 cstring_free (tname);
5687 for (i = 0; i < s->nentries; i++)
5689 cstring us = uentry_unparseFull (s->entries[i]);
5690 fprintf (g_msgstream, "%s\n", cstring_toCharsSafe (us));
5694 if (s->reftable != NULL && s->nentries > 0)
5696 fprintf (g_msgstream, "\t<< Ref table >>\n");
5698 for (i = 0; i < s->nentries; i++)
5700 fprintf (g_msgstream, "\t%s %3d: %d, %d\n", ind, i,
5701 s->reftable[i]->level,
5702 s->reftable[i]->index);
5706 ind[depth * 3 + 1] = ' ';
5710 fprintf (g_msgstream, "<<< end usymtab >>>\n");
5716 usymtab_printTypes ()
5717 /*@globals globtab@*/
5719 usymtab_printAllAux (globtab);
5723 usymtab_printAll (void)
5726 usymtab_printAllAux (utab);
5730 usymtab_printAllAux (usymtab s)
5731 /*@modifies g_msgstream@*/
5735 char *ind = mstring_copy (" ");
5737 printf ("[[[ usymtab ]]]");
5739 while (s != GLOBAL_ENV)
5742 ind[depth * 3 + 1] = '\0';
5744 if (s->env == GLOBAL_ENV)
5748 printf ("level: %d / break: %s / exit: %s\n", s->lexlevel,
5749 cstring_toCharsSafe (bool_unparse (s->mustBreak)),
5750 cstring_toCharsSafe (exitkind_unparse (s->exitCode)));
5754 for (i = looplow; i < s->nentries; i++)
5756 printf ("%s%3d. %s\n", ind, i,
5757 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5762 printf ("level: %d / break: %s / exit: %s\n", s->lexlevel,
5763 cstring_toCharsSafe (bool_unparse (s->mustBreak)),
5764 cstring_toCharsSafe (exitkind_unparse (s->exitCode)));
5766 for (i = 0; i < s->nentries; i++)
5768 printf ("%s%3d %s\n", ind, i,
5769 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5773 ind[depth * 3 + 1] = ' ';
5777 printf ("----------\n");
5781 usymtab_printComplete ()
5786 char *ind = mstring_copy (" ");
5789 while (s != GLOBAL_ENV)
5793 ind[depth * 3 + 1] = '\0';
5796 if (s->env == GLOBAL_ENV)
5800 printf ("level: %d\n", s->lexlevel);
5804 for (i = looplow; i < s->nentries; i++)
5806 printf ("%s%3d %s\n", ind, i,
5807 cstring_toCharsSafe (uentry_unparse (s->entries[i])));
5812 printf ("level: %d\n", s->lexlevel);
5813 for (i = 0; i < s->nentries; i++)
5815 printf ("%s%3d %s\n", ind, i,
5816 cstring_toCharsSafe (uentry_unparse (s->entries[i])));
5820 ind[depth * 3 + 1] = ' ';
5825 printf ("----------\n");
5829 static /*@only@*/ cstring /*@unused@*/
5830 usymtab_unparseLocalAux (/*@notnull@*/ usymtab s)
5832 cstring c = message ("lexlevel: %d\n", s->lexlevel);
5835 for (i = 0; i < s->nentries; i++)
5837 c = message ("%q\n%q", c, uentry_unparseFull (s->entries[i]));
5840 c = message ("%q\n=========", c);
5844 static cstring /*@unused@*/ /*@only@*/
5845 usymtab_unparseLocalList (/*@notnull@*/ usymtab s)
5847 cstring c = message ("[%d/%s/%s] ", s->lexlevel,
5848 bool_unparse (s->mustBreak),
5849 exitkind_unparse (s->exitCode));
5852 for (i = 0; i < s->nentries; i++)
5854 sRef sr = uentry_getSref (s->entries[i]);
5858 c = message ("%q: %q [%b]", c, uentry_getName (s->entries[i]),
5859 sRef_isStateDefined (sr));
5863 c = message ("%q, %q [%b]", c, uentry_getName (s->entries[i]),
5864 sRef_isStateDefined (sr));
5873 usymtab_printLocal (void)
5879 printf ("lexlevel: %d\n", s->lexlevel);
5881 for (i = 0; i < s->nentries; i++)
5883 printf ("%s\n", cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5886 while (s->lexlevel > 1)
5891 llassert (usymtab_isDefined (s));
5893 printf ("Params:\n");
5895 for (i = 0; i < s->nentries; i++)
5897 printf ("%d: %s\n", i,
5898 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5903 static bool checkDistinctExternalName (uentry e)
5904 /*@globals globtab@*/
5905 /*@modifies *g_msgstream@*/
5907 int checklen = context_getValue (FLG_EXTERNALNAMELEN);
5908 bool ignorecase = context_getFlag (FLG_EXTERNALNAMECASEINSENSITIVE);
5909 bool gotone = FALSE;
5910 bool extras = FALSE;
5911 bool hasError = FALSE;
5912 cstring name = uentry_rawName (e);
5913 usymtab st = globtab;
5921 if (uentry_isAnyTag (e))
5923 checklen++; /* the tag marker doesn't count */
5927 usymtab_entries (st, oe)
5929 if (uentry_sameObject (oe, e))
5936 if (cstring_equalCaseInsensitive (uentry_rawName (oe), name))
5945 (FLG_DISTINCTEXTERNALNAMES,
5947 ("External identifier %q is not distinguishable from %q "
5948 "because alphabetical case is ignored",
5950 uentry_getName (oe)),
5951 uentry_whereLast (e)))
5953 uentry_showWhereAny (oe);
5954 uentry_setHasNameError (oe);
5963 if (cstring_equalLenCaseInsensitive (uentry_rawName (oe),
5972 if (cstring_equalLen (uentry_rawName (oe), name, checklen))
5975 (FLG_DISTINCTEXTERNALNAMES,
5978 ("External identifier %q is not distinguishable from %q "
5979 "in the first %d characters (%q)",
5981 uentry_getName (oe),
5983 cstring_clip (uentry_getName (e), checklen)),
5985 uentry_whereLast (e)))
5987 uentry_showWhereAny (oe);
5988 uentry_setHasNameError (oe);
6001 (FLG_DISTINCTEXTERNALNAMES,
6003 ("External identifier %q is not distinguishable from %q "
6004 "in the first %d characters because alphabetical case "
6007 uentry_getName (oe),
6009 uentry_whereLast (e)))
6011 uentry_showWhereAny (oe);
6012 uentry_setHasNameError (oe);
6018 else if (cstring_equalLen (uentry_rawName (oe), name, checklen))
6027 (FLG_DISTINCTEXTERNALNAMES,
6030 ("External identifier %q is not distinguishable from %q "
6031 "in the first %d characters (%q)",
6033 uentry_getName (oe),
6035 cstring_clip (uentry_getName (e), checklen)),
6037 uentry_whereLast (e)))
6039 uentry_showWhereAny (oe);
6040 uentry_setHasNameError (oe);
6049 } end_usymtab_entries ;
6056 (cstring_makeLiteral ("One or more additional "
6057 "indistinguishable external "
6058 "names not reported"));
6064 static bool checkDistinctInternalName (uentry e)
6066 /*@modifies *g_msgstream@*/
6068 usymtab ttab = utab;
6069 cstring name = uentry_rawName (e);
6070 int numchars = context_getValue (FLG_INTERNALNAMELEN);
6071 bool caseinsensitive = context_getFlag (FLG_INTERNALNAMECASEINSENSITIVE);
6072 bool lookalike = context_getFlag (FLG_INTERNALNAMELOOKALIKE);
6074 if (uentry_isAnyTag (e) && (numchars != 0))
6076 numchars++; /* the tag marker doesn't count */
6079 while (usymtab_isDefined (ttab))
6081 usymtab_entries (ttab, oe)
6083 if (uentry_sameObject (oe, e))
6085 /*@innercontinue@*/ continue;
6088 switch (cstring_genericEqual
6089 (name, uentry_rawName (oe),
6090 numchars, caseinsensitive, lookalike))
6092 case CGE_DISTINCT: /* okay */
6096 if (cstring_equal (name, uentry_rawName (oe)))
6098 ; /* got a shadow error */
6103 (FLG_DISTINCTINTERNALNAMES,
6106 ("Internal identifier %q is not distinguishable from %q "
6107 "in the first %d characters (%q)",
6109 uentry_getName (oe),
6111 cstring_clip (uentry_getName (e), numchars)),
6113 uentry_whereLast (e)))
6115 uentry_showWhereAny (oe);
6116 uentry_setHasNameError (oe);
6124 || (cstring_length (name) <= numchars))
6127 (FLG_DISTINCTINTERNALNAMES,
6129 ("Internal identifier %q is not distinguishable from %q "
6130 "without case sensitivity",
6132 uentry_getName (oe)),
6133 uentry_whereLast (e)))
6135 uentry_showWhereAny (oe);
6136 uentry_setHasNameError (oe);
6143 (FLG_DISTINCTINTERNALNAMES,
6145 ("Internal identifier %q is not distinguishable from %q "
6146 "in the first %d characters without case sensitivity",
6148 uentry_getName (oe),
6150 uentry_whereLast (e)))
6152 uentry_showWhereAny (oe);
6153 uentry_setHasNameError (oe);
6161 || (cstring_length (name) <= numchars))
6164 (FLG_DISTINCTINTERNALNAMES,
6166 ("Internal identifier %q is not distinguishable from %q "
6167 "except by lookalike characters",
6169 uentry_getName (oe)),
6170 uentry_whereLast (e)))
6172 uentry_showWhereAny (oe);
6173 uentry_setHasNameError (oe);
6180 (FLG_DISTINCTINTERNALNAMES,
6182 ("Internal identifier %q is not distinguishable from %q "
6183 "in the first %d characters except by lookalike characters",
6185 uentry_getName (oe),
6187 uentry_whereLast (e)))
6189 uentry_showWhereAny (oe);
6190 uentry_setHasNameError (oe);
6195 } end_usymtab_entries ;
6203 void usymtab_checkDistinctName (uentry e, int scope)
6204 /*@globals utab, globtab@*/
6206 bool hasError = FALSE;
6207 fileloc where = uentry_whereLast (e);
6209 if (!fileloc_isPreproc (where) && !fileloc_isBuiltin (where))
6211 if (scope == globScope)
6213 if (context_getFlag (FLG_DISTINCTEXTERNALNAMES))
6215 hasError = checkDistinctExternalName (e);
6219 if (!hasError && context_getFlag (FLG_DISTINCTINTERNALNAMES))
6221 hasError = checkDistinctInternalName (e);
6227 uentry_setHasNameError (e);
6231 /*@exposed@*/ sRef usymtab_lookupGlobalMarker (void) /*@globals utab@*/
6235 ue = usymtab_lookupAux (utab, GLOBAL_MARKER_NAME);
6236 llassert (uentry_isValid (ue));
6238 return uentry_getSref (ue);