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,
506 uentry_getType (e)));
509 usymtab_addEntryQuiet (s, e);
515 static /*@observer@*/ uentry /*@alt void@*/
516 usymtab_addEntryAlways (/*@notnull@*/ usymtab s, /*@only@*/ uentry e)
519 ** In theory, we shouldn't need this test because it this is
520 ** only called when a library is being read, and it shouldn't
521 ** ever have a duplicate entry. In practice, its safer to
522 ** leave it in, though.
526 int thisentry = s->nentries;
528 if (uentry_isValid (old = usymtab_lookupQuiet (s, uentry_rawName (e))))
531 (message ("Duplicate entry in load library: %s. "
532 "Old entry: %q. New entry: %q",
534 uentry_unparseFull (old),
535 uentry_unparseFull (e)));
537 uentry_setName (e, message ("__x_%s", uentry_rawName (e)));
538 /* This shouldn't happen...unless the library is bad! */
542 if (uentry_isVar (e) && !uentry_isGlobalMarker (e))
544 uentry_setSref (e, sRef_makeCvar (globScope, thisentry,
545 uentry_getType (e)));
548 usymtab_addEntryQuiet (s, e);
553 usymtab_addEntryAux (/*@notnull@*/ usymtab st, /*@keep@*/ uentry e, bool isSref)
554 /*@globals globtab@*/
557 usymId thisentry = st->nentries;
559 llassert (!uentry_isElipsisMarker (e));
563 ** llassert (usymtab_lookupSafe (uentry_rawName (e)) == uentry_undefined);
566 llassertprint (uentry_isAnyTag (e) ? st == globtab : TRUE,
567 ("non-global tag: %s / %d", uentry_unparseFull (e), st->lexlevel));
569 if (st->lexlevel == fileScope
570 && (!(uentry_isStatic (e)) || uentry_isAnyTag (e)))
577 ctype ct = uentry_getType (e);
579 if (uentry_isFunction (e) && ctype_isFunction (ct))
581 ct = ctype_getReturnType (ct);
584 if (uentry_isStatic (e))
586 sRef sr = sRef_makeCvar (st->lexlevel, thisentry, ct);
588 if (sRef_isStack (sr) || sRef_isLocalState (sr))
590 sRef_setAliasKind (sr, AK_STATIC, uentry_whereLast (e));
591 sRef_setDefined (sr, uentry_whereLast (e));
594 uentry_setSref (e, sr);
598 uentry_setSref (e, sRef_makeCvar (st->lexlevel, thisentry, ct));
603 if (uentry_isDatatype (e))
606 uentry_setDatatype (e, thisentry);
609 if (uentry_isFunction (e))
611 ctype ct = uentry_getType (e);
613 if (ctype_isFunction (ct)
614 && uentryList_isMissingParams (ctype_argsFunction (ct)))
616 if (uentry_isDeclared (e))
620 message ("Function %q declared without parameter list",
622 uentry_whereDeclared (e));
628 message ("Function %q specified without parameter list",
630 uentry_whereSpecified (e));
635 if (st == globtab && !uentry_isSpecified (e))
637 exprChecks_checkExport (e);
640 uentry_checkName (e);
642 usymtab_addEntryQuiet (st, e);
643 DPRINTF (("Adding entry: [%p] %s", e, uentry_unparseFull (e)));
648 usymtab_addEntry (uentry e)
649 /*@globals utab, globtab@*/
650 /*@modifies utab, e@*/
652 llassertprint (!usymtab_exists (uentry_rawName (e)),
653 ("Entry already exists: %s", uentry_unparse (e)));
655 return usymtab_addEntryAux (utab, e, FALSE);
659 usymtab_addGlobalEntry (uentry e)
660 /*@modifies globtab, e@*/
662 DPRINTF (("Add global: %s / %s", uentry_unparse (e), bool_unparse (uentry_isForward (e))));
664 (void) usymtab_addEntryAux (globtab, e, FALSE);
668 ** supercede and replace entry use elements of entries array, and
669 ** assign an element to a new value, and then free the old value.
670 ** Need -deparrays to avoid errors for this.
676 usymtab_supEntryAux (/*@notnull@*/ usymtab st,
677 /*@only@*/ uentry e, bool isSref)
678 /*@globals globtab, filetab@*/
679 /*@modifies st, globtab, e@*/
681 cstring ename = uentry_rawName (e);
682 bool staticEntry = FALSE;
685 DPRINTF (("Sup entry aux: %s", uentry_unparseFull (e)));
687 /* static tags in global scope */
688 if (st->lexlevel == fileScope
689 && (!(uentry_isStatic (e)) || uentry_isAnyTag (e)))
691 eindex = usymtab_getIndex (st, ename);
693 if (eindex != NOT_FOUND)
695 uentry ce = st->entries[eindex];
699 message ("%s %q shadows static declaration",
700 ekind_capName (uentry_getKind (e)),
702 uentry_whereDeclared (e)))
704 uentry_showWhereLast (ce);
707 if (eindex == st->nentries - 1)
713 st->entries[eindex] = st->entries[st->nentries - 1];
716 if (cstringTable_isDefined (st->htable))
718 cstringTable_replaceKey (st->htable, uentry_rawName (ce),
719 cstring_copy (uentry_rawName (e)));
729 if (uentry_isStatic (e)) {
730 if (uentry_isFunction (e)) {
732 ** Static function declarations are at the file level,
733 ** even if they are in a deeper scope.
736 st = usymtab_getFileTab ();
739 if (!uentry_isAnyTag (e) && st == globtab)
741 st = usymtab_getFileTab ();
743 DPRINTF (("Static entry!"));
748 DPRINTF (("Using symtab: %s", usymtab_unparseLocalAux (st)));
750 eindex = usymtab_getIndex (st, ename);
752 if (eindex != NOT_FOUND)
754 uentry ce = st->entries[eindex];
756 DPRINTF (("Found entry: %s", uentry_unparse (ce)));
758 if (uentry_isPriv (ce)
759 /* || (fileloc_isImport (uentry_whereSpecified (ce))) */
760 || (uentry_isStatic (ce)
761 && uentry_isAnyTag (ce)
762 && (uentry_isDeclared (ce)
763 && !fileloc_sameFile (uentry_whereDefined (ce),
764 uentry_whereDefined (e)))))
768 ** if overloading import should do some checks!
771 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
773 DPRINTF (("Overloading!"));
775 st->entries[eindex] = e;
777 if (uentry_isDatatype (e))
779 uentry_setDatatype (e, eindex);
782 if (st == globtab && !uentry_isSpecified (e))
784 exprChecks_checkExport (e);
787 if (cstringTable_isDefined (st->htable))
789 cstringTable_replaceKey (st->htable, uentry_rawName (ce),
790 cstring_copy (uentry_rawName (e)));
796 else if (uentry_isSpecified (ce))
798 if (uentry_isSpecified (e))
800 DPRINTF (("Here we are: %s", uentry_unparseFull (e)));
802 if (fileloc_isImport (uentry_whereSpecified (ce)))
804 if (cstringTable_isDefined (st->htable))
806 cstringTable_replaceKey (st->htable,
808 cstring_copy (uentry_rawName (e)));
812 st->entries[eindex] = e;
815 if (uentry_isDatatype (e)) uentry_setDatatype (e, eindex);
819 if (fileloc_isImport (uentry_whereSpecified (e)))
825 /* Respecification errors already reported */
826 DPRINTF (("Respecification: %s / %s",
827 fileloc_unparse (uentry_whereSpecified (e)),
828 bool_unparse (fileloc_isSpec (uentry_whereSpecified (e)))));
830 if (uentry_isDatatype (e))
832 uentry_setDatatype (e, eindex);
835 if (cstringTable_isDefined (st->htable))
837 cstringTable_replaceKey (st->htable,
839 cstring_copy (uentry_rawName (e)));
842 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
844 st->entries[eindex] = e;
849 else /* e not specified */
851 DPRINTF (("Merging..."));
853 if (uentry_isDeclared (ce))
855 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
856 DPRINTF (("Merge defn"));
857 uentry_mergeDefinition (ce, e);
861 llassert ((st->lexlevel > fileScope || !sRef_modInFunction ()));
862 DPRINTF (("Merge entries..."));
863 uentry_mergeEntries (ce, e);
864 DPRINTF (("After: %s", uentry_unparseFull (ce)));
868 else /* ce not specified */
870 if (!(st->lexlevel > fileScope || !sRef_modInFunction ()))
872 if (uentry_isDatatype (e) || uentry_isAnyTag (e)
873 || uentry_isEnumConstant (e))
876 ** Not a bug. Code like,
877 ** int f (void) { typedef int tint; ... }
883 llcontbug (message ("usymtab_supEntryAux: inconsistent state "
884 "(lexlevel = %d, modFunction = %s) adding: %q",
885 st->lexlevel, bool_unparse (sRef_modInFunction ()),
886 uentry_unparse (e)));
888 if (sRef_modInFunction ())
890 /* make sure the sRef is not bashed! */
891 /*@i@*/ e->sref = sRef_saveCopy (e->sref);
896 DPRINTF (("Merge.."));
897 uentry_mergeDefinition (ce, e);
902 ctype ct = uentry_getType (ce);
904 if (uentry_isFunction (ce) && ctype_isFunction (ct))
906 ct = ctype_getReturnType (ct);
909 uentry_setSref (ce, sRef_makeCvar (st->lexlevel, eindex, ct));
912 else /* no previous entry */
916 if (st->lexlevel == globScope
917 && !uentry_isStatic (e)
918 && !uentry_isExtern (e)
919 && usymtab_isDefined (filetab))
922 ** check if there is a static entry: (i.e.,
929 eindex = usymtab_getIndex (filetab, ename);
931 if (eindex != NOT_FOUND)
933 uentry ce = filetab->entries[eindex];
935 uentry_setStatic (e);
936 uentry_mergeDefinition (ce, e);
942 outer = usymtab_lookupQuiet (st->env, ename);
945 ** no previous definition, add the new one
949 && uentry_isValid (outer)
950 && !(uentry_isYield (e) || uentry_isYield (outer))
951 && fileloc_isDefined (uentry_whereLast (e))
952 && fileloc_isDefined (uentry_whereLast (outer)))
954 if (!uentry_sameKind (outer, e))
960 if (ctype_isUnknown (uentry_getType (outer))
961 || uentry_isForward (outer))
969 message ("%s %q shadows outer declaration",
970 ekind_capName (uentry_getKind (e)),
972 uentry_whereLast (e)))
974 uentry_showWhereLast (outer);
980 if (st == globtab && context_getFlag (FLG_NEWDECL))
984 message ("New declaration: %q", uentry_getName (e)),
985 uentry_whereLast (e));
988 eindex = usymtab_addEntryAux (st, e, isSref);
992 return (staticEntry ? USYMIDINVALID : eindex);
997 usymtab_replaceEntryAux (/*@notnull@*/ usymtab st, /*@only@*/ uentry e)
998 /*@globals globtab@*/ /*@modifies st, e@*/
1000 cstring ename = uentry_rawName (e);
1003 /* static tags in global scope */
1004 eindex = usymtab_getIndex (st, ename);
1006 if (eindex != NOT_FOUND)
1008 uentry ce = st->entries[eindex];
1010 if (cstringTable_isDefined (st->htable))
1012 cstringTable_replaceKey (st->htable, uentry_rawName (ce),
1013 cstring_copy (uentry_rawName (e)));
1017 st->entries[eindex] = e;
1021 eindex = usymtab_addEntryAux (st, e, FALSE);
1028 void usymtab_supEntry (uentry e)
1029 /*@globals utab, filetab, globtab@*/
1030 /*@modifies utab, globtab, e@*/
1032 (void) usymtab_supEntryAux (utab, e, FALSE);
1036 ** this should be lots more efficient!
1039 static /*@exposed@*/ uentry
1040 usymtab_supEntryReturnAux (/*@notnull@*/ usymtab tab,
1041 /*@only@*/ uentry e, bool isref)
1042 /*@globals globtab, filetab@*/
1043 /*@modifies tab, globtab, e@*/
1045 cstring rawName = cstring_copy (uentry_rawName (e));
1046 bool stat = (tab == globtab) && uentry_isStatic (e);
1050 (void) usymtab_supEntryAux (tab, e, isref);
1054 ret = usymtab_lookupAux (filetab, rawName);
1058 ret = usymtab_lookupAux (tab, rawName);
1060 if (uentry_isInvalid (ret) && usymtab_isDefined (filetab))
1062 ret = usymtab_lookupAux (filetab, rawName);
1066 cstring_free (rawName);
1070 /*@dependent@*/ /*@exposed@*/ uentry
1071 usymtab_supEntryReturn (/*@only@*/ uentry e)
1072 /*@globals utab, filetab, globtab@*/
1073 /*@modifies utab, globtab, e@*/
1075 return (usymtab_supEntryReturnAux (utab, e, FALSE));
1078 /*@dependent@*/ /*@exposed@*/ uentry
1079 usymtab_supEntrySrefReturn (/*@only@*/ uentry e)
1080 /*@globals utab, globtab, filetab@*/
1081 /*@modifies utab, globtab, e@*/
1083 return (usymtab_supEntryReturnAux (utab, e, TRUE));
1086 /*@dependent@*/ /*@exposed@*/ uentry
1087 usymtab_supGlobalEntryReturn (uentry e)
1088 /*@globals globtab, filetab@*/
1089 /*@modifies globtab, e@*/
1093 ret = usymtab_supEntryReturnAux (globtab, e, FALSE);
1095 if (sRef_modInFunction ())
1097 recordFunctionType (ret);
1104 usymtab_supTypeEntry (/*@only@*/ uentry e)
1105 /*@globals globtab, filetab@*/
1106 /*@modifies globtab, e@*/
1111 if (uentry_isAbstractDatatype (e))
1113 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1114 ret = ctype_createAbstract (uid);
1118 uid = usymtab_supEntryAux (globtab, e, FALSE);
1119 ret = ctype_createUser (uid);
1122 if (sRef_modInFunction ())
1124 recordFunctionType (globtab->entries[uid]);
1131 usymtab_supReturnTypeEntry (/*@only@*/ uentry e)
1132 /*@globals globtab, filetab@*/
1133 /*@modifies globtab@*/
1137 DPRINTF (("Abstract? %s", uentry_unparseFull (e)));
1139 if (uentry_isAbstractDatatype (e))
1141 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1143 else if (uentry_isMaybeAbstract (e) && context_getFlag (FLG_IMPABSTRACT))
1145 bool maybeabs = TRUE;
1146 cstring sname = uentry_getName (e);
1147 uentry ue = usymtab_lookupGlobSafe (sname);
1148 cstring_free (sname);
1150 if (uentry_isValid (ue))
1152 DPRINTF (("Lookup: %s", uentry_unparseFull (ue)));
1154 if (uentry_isDatatype (ue))
1156 if (uentry_isMaybeAbstract (ue))
1167 DPRINTF (("Not datatype!"));
1174 uid = usymtab_supAbstractTypeEntry (e, FALSE);
1175 ux = usymtab_getTypeEntry (uid);
1176 uentry_setAbstract (ux);
1180 uid = usymtab_supEntryAux (globtab, e, FALSE);
1181 e = usymtab_getTypeEntry (uid);
1183 if (uentry_isMaybeAbstract (e))
1185 uentry_setConcrete (e);
1191 uid = usymtab_supEntryAux (globtab, e, FALSE);
1192 e = usymtab_getTypeEntry (uid);
1194 if (uentry_isMaybeAbstract (e))
1196 uentry_setConcrete (e);
1200 if (sRef_modInFunction ())
1202 recordFunctionType (globtab->entries[uid]);
1205 return (globtab->entries[uid]);
1209 usymtab_supAbstractTypeEntry (/*@only@*/ uentry e, bool dodef)
1210 /*@globals globtab, filetab@*/
1211 /*@modifies globtab, e@*/
1216 uid = usymtab_supEntryAux (globtab, e, FALSE);
1217 ue = usymtab_getTypeEntry (uid);
1221 uentry_setDatatype (ue, uid);
1224 if (context_getFlag (FLG_ACCESSMODULE)) /* was accessfile */
1226 context_addFileAccessType (uid);
1229 if (sRef_modInFunction ())
1231 recordFunctionType (globtab->entries[uid]);
1239 usymtab_supExposedTypeEntry (/*@only@*/ uentry e, bool dodef)
1240 /*@globals globtab, filetab@*/
1241 /*@modifies globtab, e@*/
1245 uid = usymtab_supEntryAux (globtab, e, FALSE);
1249 uentry ue = usymtab_getTypeEntry (uid);
1251 uentry_setDatatype (ue, uid);
1254 if (sRef_modInFunction ())
1256 recordFunctionType (globtab->entries[uid]);
1264 usymtab_supForwardTypeEntry (/*@only@*/ uentry e)
1265 /*@globals globtab, filetab@*/
1266 /*@modifies globtab, e@*/
1268 usymId uid = usymtab_supEntryAux (globtab, e, FALSE);
1269 uentry ue = usymtab_getTypeEntry (uid);
1271 uentry_setDatatype (ue, uid);
1273 if (sRef_modInFunction ())
1275 recordFunctionType (globtab->entries[uid]);
1278 return (uentry_getAbstractType (ue));
1282 usymtab_supEntrySref (uentry e)
1283 /*@globals utab, globtab, filetab@*/
1284 /*@modifies utab, globtab, e@*/
1286 sRef old = uentry_getSref (e);
1289 if (sRef_isType (old))
1291 uentry ue = usymtab_supEntryReturnAux (utab, e, TRUE);
1294 if (uentry_isValid (ue))
1296 sRef uref = uentry_getSref (ue);
1298 sRef_mergeStateQuiet (uref, old);
1299 sRef_clearDerived (uref);
1301 /*@noaccess uentry@*/
1303 else if (sRef_isKnown (old))
1305 usymtab_supEntry (e);
1309 (void) usymtab_supEntryAux (utab, e, TRUE);
1313 void usymtab_supGlobalEntry (/*@only@*/ uentry e)
1314 /*@globals globtab, filetab@*/
1315 /*@modifies globtab, filetab, e@*/
1319 DPRINTF (("Sup global entry: %s", uentry_unparse (e)));
1321 uid = usymtab_supEntryAux (globtab, e, FALSE);
1323 if (sRef_modInFunction ())
1325 recordFunctionType (globtab->entries[uid]);
1330 usymtab_supReturnFileEntry (/*@only@*/ uentry e)
1331 /*@globals filetab, globtab@*/
1332 /*@modifies filetab, globtab, e@*/
1334 llassert (filetab != usymtab_undefined);
1335 DPRINTF (("File entry: %s", uentry_unparse (e)));
1336 return (usymtab_supEntryReturnAux (filetab, e, FALSE));
1344 usymtab_inDeepScope () /*@globals utab@*/
1346 return (utab->lexlevel > paramsScope);
1350 usymtab_getIndex (/*@notnull@*/ usymtab s, cstring k)
1354 DPRINTF (("Lookup %s", k));
1356 if (cstringTable_isDefined (s->htable))
1358 i = cstringTable_lookup (s->htable, k);
1363 for (i = 0; i < s->nentries; i++)
1365 uentry current = s->entries[i];
1367 DPRINTF (("Check %d: %s", i, uentry_rawName (current)));
1369 if (!uentry_isUndefined (current)
1370 && cstring_equal (uentry_rawName (current), k))
1381 usymtab_fetchIndex (/*@notnull@*/ usymtab s, int i)
1383 llassert (i >= 0 && i < s->nentries);
1384 return (s->entries[i]);
1388 usymtab_getTypeId (cstring k) /*@globals globtab@*/
1390 usymId uid = usymtab_getIndex (globtab, k);
1392 if (uid == NOT_FOUND) return USYMIDINVALID;
1394 if (!(uentry_isDatatype (usymtab_getTypeEntry (uid)))) {
1395 return USYMIDINVALID;
1401 /*@dependent@*/ uentry
1402 usymtab_lookupStructTag (cstring k)
1404 cstring sname = makeStruct (k);
1405 uentry ue = usymtab_lookupGlob (sname);
1407 cstring_free (sname);
1411 /*@dependent@*/ uentry
1412 usymtab_lookupUnionTag (cstring k)
1414 cstring uname = makeUnion (k);
1415 uentry res = usymtab_lookupGlob (uname);
1417 cstring_free (uname);
1421 /*@dependent@*/ uentry
1422 usymtab_lookupEnumTag (cstring k)
1424 cstring ename = makeEnum (k);
1425 uentry res = usymtab_lookupGlob (ename);
1427 cstring_free (ename);
1432 usymtab_getId (cstring k) /*@globals globtab@*/
1434 usymId uid = usymtab_getIndex (globtab, k);
1437 if (uid == NOT_FOUND)
1439 return USYMIDINVALID;
1442 ue = usymtab_getGlobalEntry (uid);
1444 if (uentry_isPriv (ue))
1446 return USYMIDINVALID;
1452 static /*@exposed@*/ uentry
1453 usymtab_getEntryAux (/*@notnull@*/ usymtab s, usymId uid)
1455 llassert (uid != USYMIDINVALID);
1457 if (uid < 0 || uid >= s->nentries)
1459 llcontbug (message ("usymtab_getEntry: out of range: level = %d [%d]",
1461 return uentry_undefined;
1464 llassertprint (uentry_isValid (s->entries[uid]),
1465 ("entry undefined: %d", uid));
1467 return s->entries[uid];
1470 /*@dependent@*/ /*@observer@*/ uentry
1471 usymtab_getGlobalEntry (usymId uid)
1472 /*@globals utab, globtab@*/
1474 if (dbgfree) return (uentry_undefined);
1476 if (utab->lexlevel > paramsScope)
1478 /* need to do this the awkward way, since it could be in conditional scope */
1479 return (usymtab_lookupSafe (uentry_rawName (globtab->entries[uid])));
1483 return (globtab->entries[uid]);
1487 /*@dependent@*/ /*@exposed@*/ uentry
1488 usymtab_getTypeEntry (usymId uid)
1489 /*@globals globtab@*/
1493 if (uid >= 0 && uid < globtab->nentries)
1495 return (globtab->entries[uid]);
1499 return (uentry_undefined);
1504 llassert (uid >= 0 && uid < globtab->nentries);
1505 return (globtab->entries[uid]);
1513 /*@dependent@*/ /*@exposed@*/ uentry
1514 usymtab_getTypeEntrySafe (usymId uid)
1515 /*@globals globtab@*/
1517 if (uid < 0 || uid >= globtab->nentries)
1519 return uentry_undefined;
1522 return (globtab->entries[uid]);
1526 usymtab_isBoolType (usymId uid)
1527 /*@globals globtab@*/
1529 llassert (uid >= 0 && uid < globtab->nentries);
1531 return (cstring_equal (uentry_rawName (globtab->entries[uid]),
1532 context_getBoolName ()));
1536 usymtab_getTypeEntryName (usymId uid)
1537 /*@globals globtab@*/
1543 return (cstring_makeLiteral ("<freetype>"));
1546 ue = usymtab_getTypeEntry (uid);
1548 if (dbgload && !uentry_isValid (ue))
1550 return (message ("<missing type: %d>", uid));
1553 llassertprint (uentry_isValid (ue), ("type undefined: %d", uid));
1555 return (uentry_getName (ue));
1558 /*@unused@*/ static void
1559 usymtab_rehash (/*@notnull@*/ usymtab s)
1563 if (cstringTable_isDefined (s->htable))
1565 cstringTable_free (s->htable);
1568 s->htable = cstringTable_create (LLHASHSIZE);
1570 for (i = 0; i < s->nentries; i++)
1572 cstringTable_insert (s->htable, cstring_copy (uentry_rawName (s->entries[i])), i);
1577 ** superficial copy of usymtab
1579 ** DO copy spec entries
1582 static /*@only@*/ /*@notnull@*/ usymtab
1583 usymtab_shallowCopy (/*@notnull@*/ usymtab s) /*@*/
1585 usymtab copytab = usymtab_createRoot ();
1588 for (i = 0; i < s->nentries; i++)
1590 usymtab_addEntryBase (copytab, s->entries[i]);
1597 usymtab_shallowFree (/*@only@*/ /*@notnull@*/ usymtab s)
1599 aliasTable_free (s->aliases);
1600 refTable_free (s->reftable, s->nentries);
1602 /*@-compdestroy@*/ sfree (s); /*@=compdestroy@*/
1606 ** converts usymId from old table to sorted one
1610 usymtab_convertId (usymId uid)
1611 /*@globals oldtab, utab@*/
1617 llassert (usymtab_isDefined (oldtab));
1619 ue = usymtab_getEntryAux (oldtab, uid);
1621 llassertprint (uentry_isValid (ue), ("convertId: undefined: %d", uid));
1623 name = uentry_rawName (ue);
1625 ret = usymtab_getIndex (utab, name);
1626 llassert (ret == uid); /*! for now, no rehash! */
1627 DPRINTF (("Convert: %s [%d] -> %s [%d]",
1628 uentry_unparse (ue), uid,
1629 uentry_unparse (utab->entries[ret]), ret));
1631 llassertprint (ret != USYMIDINVALID, ("convertId: return is invalid"));
1637 usymtab_prepareDump (void)
1638 /*@globals oldtab, utab@*/
1639 /*@modifies oldtab, utab@*/
1641 llassert (usymtab_inGlobalScope ());
1642 llassert (oldtab == usymtab_undefined);
1645 DPRINTF (("Preparing to dump:"));
1646 usymtab_printAll ();
1649 oldtab = usymtab_shallowCopy (utab);
1652 ** alpha compare - make sure order is same on different platforms
1653 ** error messages appear in same order
1657 qsort (utab->entries, (size_t)utab->nentries,
1658 sizeof (*utab->entries),
1659 (int (*)(const void *, const void *)) uentry_xcomparealpha);
1661 usymtab_rehash (utab);
1665 DPRINTF (("After rehash:"));
1666 usymtab_printAll ();
1670 void usymtab_dump (FILE *fout)
1671 /*@globals utab, oldtab@*/
1674 bool neednl = FALSE;
1675 uentry lastentry = uentry_undefined;
1676 ekind lastekind = KINVALID;
1680 ** must call prepareDump first
1683 llassert (oldtab != usymtab_undefined);
1685 for (i = 0; i < utab->nentries; i++)
1687 uentry thisentry = utab->entries[i];
1688 ekind thisekind = uentry_getKind (thisentry);
1690 if (!uentry_hasRealName (thisentry))
1692 llassert (uentry_isGlobalMarker (thisentry));
1696 check (fputc ('\n', fout) == (int) '\n');
1699 fprintf (fout, "*%d (GlobalMarker)\n", KGLOBALMARKER);
1700 lastekind = KINVALID;
1706 if (thisekind != lastekind)
1710 check (fputc ('\n', fout) == (int) '\n');
1714 lastentry = uentry_undefined;
1715 fprintf (fout, "*%d (%s)\n", ekind_toInt (thisekind),
1716 cstring_toCharsSafe (ekind_capName (thisekind)));
1717 lastekind = thisekind;
1722 ** evans - 2001-02-18 - added the - 48 fudge factor...
1723 ** extra characters dumped, but I haven't counded them carefully...
1726 if (uentry_isInvalid (lastentry) || !uentry_equiv (lastentry, thisentry)
1727 || (linelen > (MAX_DUMP_LINE_LENGTH - (2 * MAX_NAME_LENGTH) - 48)))
1731 DPRINTF (("Dumping entry: %d", i));
1732 cdump = message ("^%d %q", i, uentry_dump (thisentry));
1733 /* was: cdump = uentry_dump (thisentry)); */
1735 lastentry = thisentry;
1738 check (fputc ('\n', fout) == (int) '\n');
1742 linelen += cstring_length (cdump);
1744 /* no new line here! */
1745 if (cstring_length (cdump) > 0)
1747 check (fputs (cstring_toCharsSafe (cdump), fout) != EOF);
1750 cstring_free (cdump);
1755 cstring cdump = uentry_rawName (thisentry);
1756 DPRINTF (("Raw name: %s", cdump));
1757 linelen += (cstring_length (cdump) + 1);
1758 fprintf (fout, "#%s", cstring_toCharsSafe (cdump));
1765 check (fputc ('\n', fout) == (int) '\n');
1768 lastekind = KINVALID;
1770 fprintf(fout, ";; Library constraints\n");
1771 for (i = 0; i < utab->nentries; i++)
1773 uentry thisentry = utab->entries[i];
1775 if (uentry_isFunction (thisentry) )
1777 constraintList preconditions;
1778 constraintList postconditions;
1780 preconditions = uentry_getFcnPreconditions (thisentry);
1781 postconditions = uentry_getFcnPostconditions (thisentry);
1783 if ( constraintList_isDefined(preconditions) ||
1784 constraintList_isDefined(postconditions) )
1786 fprintf(fout,"%s\n", cstring_toCharsSafe (uentry_rawName(thisentry) ) );
1787 if (constraintList_isDefined(preconditions) )
1789 fprintf(fout,"pre:\n");
1790 constraintList_dump(preconditions, fout);
1791 fprintf (fout, ";; end precondition constraints\n" );
1792 constraintList_free(preconditions);
1796 fprintf(fout,"pre:EMPTY\n");
1798 if (constraintList_isDefined(postconditions) )
1800 fprintf(fout,"post:\n");
1801 constraintList_dump(postconditions, fout);
1802 fprintf (fout, ";; end precondition constraints\n" );
1803 constraintList_free(postconditions);
1807 fprintf(fout,"post:EMPTY\n");
1818 void usymtab_load (FILE *f)
1819 /*@globals utab, globtab@*/
1820 /*@modifies utab, *f@*/
1822 char *s = mstring_create (MAX_DUMP_LINE_LENGTH);
1824 ekind kind = KINVALID;
1825 fileloc loc = g_currentloc;
1831 llassert (utab == globtab);
1832 llassert (utab->nentries == 0);
1834 while (((s = reader_readLine (f, s, MAX_DUMP_LINE_LENGTH)) != NULL)
1837 /* ignore ; comments */ ;
1840 while (s != NULL && *s != ';')
1848 ek = reader_getInt (&s);
1850 if (ek == KGLOBALMARKER)
1852 uentry lue = uentry_makeGlobalMarker ();
1853 DPRINTF (("Adding global marker: %s", uentry_unparseFull (lue)));
1854 usymtab_addEntryAlways (utab, lue);
1860 kind = ekind_fromInt (ek);
1868 (cstring_makeLiteral
1869 ("Library is in obsolete format. Use lclint +whichlib "
1870 "to see which library is being loaded."));
1873 if (reader_optCheckChar (&s, '^'))
1875 index = reader_getInt (&s);
1882 llassert (kind != KINVALID);
1883 ue = uentry_undump (kind, loc, &s);
1885 llassert (utab->nentries == index || index == -1);
1887 if (uentry_isValid (ue))
1889 int lastindex = utab->nentries;
1890 ue = usymtab_addEntryAlways (utab, ue);
1891 if (utab->nentries != lastindex + 1)
1893 DPRINTF (("No add: %s", uentry_unparseFull (ue)));
1901 ** now, any other names are the same uentry
1904 while (*(s++) == '#')
1906 cstring name = cstring_fromCharsO (reader_getWord (&s));
1907 uentry nue = uentry_nameCopy (name, ue);
1909 DPRINTF (("Name copy: %s", uentry_unparseFull (nue)));
1913 usymtab_addEntryAlways (utab, nue);
1916 while ((c = *s) != '\0' && (c !='\n'))
1918 if (c != ' ' || c != '\t')
1920 llbuglit ("Junk in load file");
1928 s = reader_readLine (f, os, MAX_DUMP_LINE_LENGTH);
1933 to handle reading of buffer overflow related constraints
1935 while (fgets (s, MAX_DUMP_LINE_LENGTH, f) != NULL
1938 ; /* ignore ;-comments */
1941 while (s != NULL && *s != ';')
1943 constraintList preconditions;
1944 constraintList postconditions;
1946 cstring name = cstring_fromChars(reader_getWord(&s) );
1948 ue = usymtab_lookup ( name );
1952 preconditions = constraintList_undefined;
1953 postconditions = constraintList_undefined;
1955 if (!uentry_isValid(ue) )
1957 llfatalbug ((message("Invalid uentry for %s library file may be corrupted", cstring_fromChars(s) ) ));
1959 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
1961 temp = cstring_fromChars(reader_getWord(&s) );
1963 if (cstring_compareLit (temp,"pre:") == 0 )
1965 preconditions = constraintList_undump (f);
1969 if (cstring_compareLit (temp,"pre:EMPTY") != 0 )
1970 llfatalbug ((message("Error reading library file pre:EMPTY expected but got %s", temp ) ));
1975 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
1977 temp = cstring_fromChars(reader_getWord(&s) );
1978 if (cstring_compareLit (temp,"post:") == 0 )
1980 postconditions = constraintList_undump (f);
1984 if (cstring_compareLit (temp,"post:EMPTY") != 0 )
1985 llfatalbug ((message("Error reading library file post:EMPTY expected but got %s", temp ) ));
1990 uentry_setPreconditions (ue, functionConstraint_createBufferConstraint (preconditions));
1991 uentry_setPostconditions (ue, functionConstraint_createBufferConstraint (postconditions));
1993 s = fgets (os, MAX_DUMP_LINE_LENGTH, f);
2001 ** file scope for static variables
2005 usymtab_enterFile ()
2006 /*@globals utab, globtab, filetab@*/
2007 /*@modifies filetab@*/
2009 llassert (utab == globtab);
2014 usymtab_entries (globtab, ue)
2016 if (sRef_hasDerived (uentry_getSref (ue)))
2018 fprintf (g_msgstream, "Derived Global: %s\n", uentry_unparse (ue));
2019 fprintf (g_msgstream, "sRef: %s\n", sRef_unparseFull (ue->sref));
2021 } end_usymtab_entries ;
2025 usymtab_enterScope ();
2031 /*@globals utab, filetab@*/
2032 /*@modifies filetab, utab@*/
2035 llassert (utab->lexlevel == 1);
2037 usymtab_exitScope (exprNode_undefined);
2042 usymtab_enterScope ()
2043 /*@globals utab, globtab, filetab@*/
2046 usymtab t = usymtab_create (US_NORMAL, utab, TRUE);
2048 /* unconditional scope: optimize to avoid copy */
2049 t->aliases = aliasTable_copy (utab->aliases);
2052 llassert (usymtab_isDefined (t->env));
2054 if (t->env->lexlevel == paramsScope && context_inFunctionLike ())
2056 noshadowerror = TRUE;
2057 usymtab_handleParams ();
2058 noshadowerror = FALSE;
2063 ** setup external references:
2064 ** o only, unique params alias external args
2065 ** o other params may alias anything of their type
2069 usymtab_handleParams (void)
2070 /*@globals utab, globtab, filetab@*/
2071 /*@modifies utab, globtab@*/
2073 usymtab ptab = utab->env;
2074 uentry fcn = context_getHeader ();
2076 usymtab_entries (ptab, param)
2080 if (!uentry_isYield (param))
2083 sRef pref = uentry_getSref (param);
2085 /* Could be a global. */
2087 if (uentry_isAnyParam (param))
2089 ue = uentry_makeVariable (fixParamName (uentry_rawName (param)),
2090 uentry_getType (param),
2091 fileloc_copy (uentry_whereDeclared (param)),
2094 uentry_copyState (ue, param);
2095 uentry_setRefParam (ue);
2097 ue = usymtab_supEntrySrefReturn (ue);
2099 /* must be after supercede! */
2101 if (!sRef_stateKnown (pref))
2103 uentry_setDefState (ue, SS_DEFINED);
2104 uentry_setDefState (param, SS_DEFINED);
2108 if (sRef_isStateSpecial (pref))
2110 uentry_setDefState (ue, SS_ALLOCATED);
2114 uentry_setDefState (ue, sRef_getDefState (pref));
2118 uref = uentry_getSref (ue);
2120 if (sRef_isStack (uref))
2122 alkind pkind = sRef_getAliasKind (pref);
2124 if (alkind_isKnown (pkind) && !alkind_isLocal (pkind)
2125 && !alkind_isStack (pkind))
2127 sRef_setAliasKind (uref, pkind, fileloc_undefined);
2128 sRef_setOrigAliasKind (uref, pkind);
2132 sRef_setAliasKind (uref, AK_IMPTEMP, fileloc_undefined);
2133 sRef_setOrigAliasKind (uref, AK_IMPTEMP);
2135 if (uentry_isOut (param))
2141 sRef_setDefined (uref, fileloc_undefined);
2147 usymtab_addMustAlias (uref, pref);
2149 if (!(uentry_isOnly (param) || uentry_isUnique (param)))
2152 ** This is needed for detecting possibly aliased parameters.
2155 sRef s = sRef_makeExternal (uref);
2156 usymtab_addMustAlias (uref, s);
2159 if (sRef_isKillRef (pref))
2161 sRef_setAliasKind (uref, AK_NEWREF, fileloc_undefined);
2162 sRef_setOrigAliasKind (uref, AK_KILLREF);
2164 else if (sRef_isRefCounted (uref))
2166 sRef_setOrigAliasKind (uref, AK_REFCOUNTED);
2171 sRef_setOrigAliasKind (uref, AK_LOCAL);
2178 } end_usymtab_entries;
2181 if (uentry_hasStateClauseList (fcn))
2183 stateClauseList clauses = uentry_getStateClauseList (fcn);
2185 stateClauseList_preElements (clauses, cl)
2187 fileloc loc = stateClause_loc (cl);
2188 sRefSet osrs = sRefSet_undefined;
2191 if (stateClause_isGlobal (cl))
2193 DPRINTF (("Global Marker: %s",
2194 sRef_unparseFull (usymtab_lookupGlobalMarker ())));
2195 llassert (sRef_isGlobalMarker (usymtab_lookupGlobalMarker ()));
2196 srs = sRefSet_single (usymtab_lookupGlobalMarker ());
2201 srs = stateClause_getRefs (cl);
2204 sRefSet_elements (srs, el)
2206 sRef base = sRef_getRootBase (el);
2207 sRef sb = sRef_updateSref (el);
2209 if (sRef_isResult (base))
2211 ; /* nothing to do before */
2213 else if (sRef_isParam (base) || sRef_isGlobalMarker (base))
2215 if (stateClause_setsMetaState (cl))
2217 /* copied from exprNode.c:3040 */
2218 qual ql = stateClause_getMetaQual (cl);
2219 annotationInfo ainfo = qual_getAnnotationInfo (ql);
2220 metaStateInfo minfo = annotationInfo_getState (ainfo);
2221 cstring key = metaStateInfo_getName (minfo);
2222 int mvalue = annotationInfo_getValue (ainfo);
2224 DPRINTF (("Sets meta state! %s", stateClause_unparse (cl)));
2226 if (sRef_isResult (base))
2232 sRef_setMetaStateValueComplete (sb, key, mvalue, loc);
2237 sRefMod modf = stateClause_getEntryFunction (cl);
2241 sRefSet aliases = usymtab_allAliases (sb);
2245 sRefSet_elements (aliases, sr)
2248 } end_sRefSet_elements ;
2250 sRefSet_free (aliases);
2256 if (sRef_isValid (base))
2258 DPRINTF (("Base: %s", sRef_unparseFull (base)));
2262 } end_sRefSet_elements ;
2263 } end_stateClauseList_preElements ;
2268 usymtab_enterFunctionScope (uentry fcn)
2269 /*@globals utab, filetab, globtab@*/
2272 usymtab t = usymtab_create (US_NORMAL, utab, TRUE);
2274 if (utab->lexlevel != fileScope)
2276 if (utab->lexlevel > fileScope)
2278 llparseerror (cstring_makeLiteral ("New function scope inside function."));
2280 while (utab->lexlevel > fileScope)
2282 /*@i@*/ utab = usymtab_dropEnv (utab);
2289 llfatalbug (cstring_makeLiteral ("New function not inside file."));
2291 /*@-branchstate@*/ } /*@=branchstate@*/
2295 DPRINTF (("Globs: %s", globSet_unparse (uentry_getGlobs (fcn))));
2297 globSet_allElements (uentry_getGlobs (fcn), el)
2299 DPRINTF (("Here we go: %s", sRef_unparseFull (el)));
2301 if (sRef_isUndefGlob (el))
2303 int index = sRef_getScopeIndex (el);
2304 sRef sr = sRef_updateSref (el);
2305 fileloc loc = uentry_whereEarliest (fcn);
2307 DPRINTF (("update: %s", sRef_unparseFull (sr)));
2308 DPRINTF (("Undef!"));
2309 if (sRef_isFileStatic (el))
2311 ctype ct = sRef_getType (el);
2314 llassert (usymtab_isDefined (filetab));
2316 ue = usymtab_fetchIndex (filetab, index);
2318 if (ctype_isRealArray (ct) || ctype_isRealSU (ct))
2320 sRef_setAllocated (sr, loc);
2324 sRef_setUndefined (sr, loc);
2329 uentry ue = globtab->entries[index];
2330 ctype ct = uentry_getType (ue);
2332 if (ctype_isArray (ct) || ctype_isSU (ct))
2334 sRef_setAllocated (sr, loc);
2338 sRef_setUndefined (sr, loc);
2342 else if (sRef_isAllocated (el))
2344 sRef sr = sRef_updateSref (el);
2345 fileloc loc = uentry_whereEarliest (fcn);
2347 sRef_setAllocated (sr, loc);
2349 else if (sRef_isPartial (el))
2351 sRef sr = sRef_updateSref (el);
2352 fileloc loc = uentry_whereEarliest (fcn);
2354 sRef_setPartial (sr, loc);
2359 sRef sr = sRef_updateSref (el);
2360 fileloc loc = uentry_whereEarliest (fcn);
2362 sRef_setDefined (sr, loc);
2366 /* shouldn't need to do anything! */
2368 } end_globSet_allElements;
2370 DPRINTF (("Globs after: %s", globSet_unparse (uentry_getGlobs (fcn))));
2374 usymtab_caseBranch (void)
2377 usymtab t = usymtab_create (US_CBRANCH, utab, FALSE);
2382 usymtab_switchBranch (/*@unused@*/ exprNode s)
2385 usymtab t = usymtab_create (US_SWITCH, utab, FALSE);
2387 t->aliases = aliasTable_copy (utab->aliases);
2392 usymtab_trueBranch (/*@only@*/ guardSet guards)
2395 usymtab t = usymtab_create (US_TBRANCH, utab, FALSE);
2398 ** not true! (could be in a macro)
2400 ** llassertprint (utab->lexlevel > paramsScope,
2401 ** ("not in scope: %s", usymtab_unparseLocal ()));
2405 guardSet_free (t->guards);
2408 aliasTable_free (t->aliases);
2409 t->aliases = aliasTable_copy (utab->aliases);
2417 ** { int a; if (...) a = 3; < a may be undefined here!
2422 usymtab_popTrueBranch (exprNode pred, exprNode expr, clause cl) /*@modifies utab@*/
2425 ** add a false branch
2426 ** (could be done more efficiently as a special case, but
2427 ** it is better to only maintain one version of the code)
2430 if (utab->kind != US_TBRANCH
2431 && context_inIterDef ())
2433 usymtab_exitScope (expr);
2437 DPRINTF (("pop true branch.."));
2438 usymtab_altBranch (guardSet_invert (exprNode_getGuards (pred)));
2439 usymtab_popBranches (pred, expr, exprNode_undefined, TRUE, cl);
2444 usymtab_popCaseBranch () /*@modifies utab@*/
2446 llassert (utab->kind == US_CBRANCH);
2447 usymtab_quietPlainExitScope ();
2451 usymtab_popTrueExecBranch (exprNode pred, exprNode expr, clause cl)
2454 ** add a false branch that must return --- that is,
2455 ** the true branch is always executed!
2458 usymtab_altBranch (guardSet_invert (exprNode_getGuards (pred)));
2459 usymtab_popBranches (pred, expr, exprNode_makeMustExit (), FALSE, cl);
2463 usymtab_popOrBranch (exprNode pred, exprNode expr)
2467 usymtab env = utab->env;
2468 usymtab otab = utab;
2471 llassert (env != NULL);
2473 if (exprNode_isError (expr))
2479 mustReturn = exprNode_mustEscape (expr);
2483 llassert (utab->kind == US_TBRANCH);
2486 ** merge each entry in table with its original
2487 ** unless execution cannot continue after this branch
2490 for (i = 0; i < utab->nentries; i++)
2492 uentry current = utab->entries[i];
2493 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
2495 uentry_mergeState (old, current, exprNode_loc (expr),
2496 mustReturn, FALSE, TRUE, ORCLAUSE);
2502 env->guards = guardSet_levelUnionFree (env->guards,
2503 guardSet_invert (exprNode_getGuards (pred)),
2508 env->aliases = aliasTable_levelUnion (env->aliases, otab->aliases, env->lexlevel);
2511 /* env is now utab */
2512 usymtab_quietPlainExitScope ();
2516 ** case syntax in C is very unrestricted. This is unfortunate.
2518 ** A switch case is ended either by a new case or default, or
2519 ** a close } that may close the switch or some other control
2524 usymtab_newCase (/*@unused@*/ exprNode pred, exprNode last)
2527 bool mustBreak = usymtab_mustBreak (utab);
2528 bool mustReturn = usymtab_mustEscape (utab);
2529 usymtab stab = utab;
2532 ** Find last case (or outer switch)
2535 while (stab->kind != US_CBRANCH && stab->kind != US_SWITCH)
2538 llassert (stab != GLOBAL_ENV);
2543 while (stab->kind == US_CBRANCH)
2546 llassert (stab != GLOBAL_ENV);
2550 ** if its a fall through case, merge in outside entries and last case.
2555 ** case 1: x = 3; <fall through>
2556 ** case 2: << x may not be defined
2560 if (!mustBreak && !mustReturn && utab->kind == US_CBRANCH)
2562 llassert (stab->kind == US_SWITCH || stab->kind == US_NORMAL);
2564 usymtab_entries (utab, ue) /* but, keep track of used variables */
2566 uentry old = usymtab_lookupAux (stab, uentry_rawName (ue));
2568 llassert (uentry_isValid (old));
2570 /* modifies ue, not old */
2572 uentry_mergeState (ue, old, exprNode_loc (last),
2573 FALSE, FALSE, TRUE, CASECLAUSE);
2574 } end_usymtab_entries;
2576 utab->aliases = aliasTable_levelUnion (utab->aliases,
2577 stab->aliases, utab->lexlevel);
2580 ** No need for a new branch.
2587 usymtab_caseBranch ();
2588 /*@-mustfree@*/ /*< utab->aliases >*/
2589 utab->aliases = aliasTable_copy (stab->aliases);
2597 ** for && (both pred and expr are executed)
2601 usymtab_popAndBranch (exprNode pred, /*@unused@*/ exprNode expr)
2604 usymtab env = utab->env;
2608 llassert (utab->kind == US_TBRANCH);
2611 ** merge each entry in table with its original
2612 ** unless execution cannot continue after this branch
2615 for (i = 0; i < utab->nentries; i++)
2617 uentry current = utab->entries[i];
2618 sRef tref = uentry_getSref (current);
2619 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
2620 sRef oref = uentry_getSref (old);
2622 /* note that is current is in a nested branch,
2623 it may create a "new" old entry. */
2625 llassert (uentry_isValid (old));
2626 uentry_mergeState (old, current, exprNode_loc (expr),
2627 FALSE, FALSE, TRUE, ANDCLAUSE);
2630 ** if is it defined by the second clause, then it should be defined.
2633 if (sRef_isAnyDefined (tref)
2634 && (sRef_isAllocated (oref) || sRef_isStateUndefined (oref)))
2636 sRef_setDefined (oref, g_currentloc);
2640 utab->guards = guardSet_levelUnionFree (utab->guards,
2641 guardSet_invert (exprNode_getGuards (pred)),
2643 utab->aliases = aliasTable_levelUnion (utab->aliases, otab->aliases, utab->lexlevel);
2645 usymtab_quietPlainExitScope ();
2650 ** Stack should be [ US_CBRANCH+ US_SWITCH ]
2651 ** Only branches which do not return (except possibly the last branch) are included.
2653 ** Conditionally merge state from all CBRANCHes.
2655 ** If allpaths is TRUE, then all possible executions go through some switch
2656 ** case, and the original scope is not merged.
2660 usymtab_exitSwitch (/*@unused@*/ exprNode sw, bool allpaths)
2663 usymtab ttab = utab;
2664 usymtab stab = ttab;
2665 usymtab ltab = ttab;
2666 bool lastMustReturn = usymtab_mustEscape (utab);
2670 while (stab->kind == US_CBRANCH)
2673 llassert (stab != GLOBAL_ENV);
2676 while (stab->kind == US_NORMAL)
2679 llassert (stab != GLOBAL_ENV);
2682 llassert (stab->kind == US_SWITCH);
2684 /* go to the scope outside the switch (US_SWITCH is just a marker! */
2686 llassert (stab != GLOBAL_ENV);
2690 llassert (usymtab_isDefined (ttab));
2692 if (ttab->kind == US_CBRANCH)
2694 /* was quietPlainExitScope --- but, can't free it yet! */
2696 llassert (utab != GLOBAL_ENV);
2698 while (ttab->kind == US_CBRANCH)
2701 ** (from popTrueBranch)
2704 bool mustReturn = usymtab_mustEscape (ttab);
2705 bool mustBreak = usymtab_mustBreak (ttab);
2707 usymtab_entries (ttab, current)
2709 uentry old = /*@-compmempass@*/ usymtab_lookupAux (ltab, uentry_rawName (current));
2713 ** note that is this is in a nested branch,
2714 ** it may create a "new" old entry.
2717 if (uentry_isValid (old))
2721 uentry_mergeUses (current, old);
2722 uentry_setState (old, current);
2726 uentry_mergeState (old, current, exprNode_loc (sw),
2727 mustReturn, FALSE, TRUE, SWITCHCLAUSE);
2734 } end_usymtab_entries;
2737 ** if entry is not in symbol table for this case, merge with pre-switch
2741 if (!mustReturn && !mustBreak)
2743 usymtab_entries (stab, current)
2745 if (usymtab_getIndex (ttab, uentry_rawName (current)) == NOT_FOUND)
2747 uentry old = /*@-compmempass@*/
2748 usymtab_lookupAux (ltab, uentry_rawName (current));
2751 llassert (uentry_isValid (old));
2752 uentry_mergeState (old, current, exprNode_loc (sw),
2753 FALSE, FALSE, TRUE, SWITCHCLAUSE);
2755 } end_usymtab_entries;
2758 ltab->env = ttab->env;
2762 ** Suprious error, becuase of environments.
2765 /*@i1@*/ utab = ltab;
2767 lastMustReturn = FALSE;
2774 ** now, there is one US_CBRANCH. Merge this with the stab.
2778 for (i = 0; i < ltab->nentries; i++)
2780 uentry current = ltab->entries[i];
2781 uentry old = usymtab_lookupAux (stab, uentry_rawName (current));
2783 /* note that is this is in a nested branch,
2784 it may create a "new" old entry. */
2787 if (uentry_isValid (old))
2791 uentry_mergeUses (current, old);
2792 uentry_setState (old, current);
2796 uentry_mergeState (old, current, exprNode_loc (sw),
2797 FALSE, FALSE, TRUE, SWITCHCLAUSE);
2811 ** switch may or may not be followed by a new scope
2814 if (utab->kind == US_SWITCH)
2816 usymtab_quietPlainExitScope ();
2820 usymtab_quietPlainExitScope ();
2821 llassert (utab->kind == US_SWITCH);
2822 usymtab_quietPlainExitScope ();
2828 updateNullState (sRef el, /*@notnull@*/ usymtab ttab,
2829 /*@notnull@*/ usymtab ftab, bool trueGuard)
2831 sRef base = sRef_getRootBase (el);
2832 int level = sRef_lexLevel (base);
2834 if (sRef_isCvar (base))
2836 usymId index = sRef_getScopeIndex (base);
2837 uentry ue = usymtab_getRefTab (ttab, level, index);
2839 if (!uentry_isLset (ue))
2841 sRef sr = uentry_getSref (ue);
2845 sRef_setDerivNullState (sr, el, NS_NOTNULL);
2849 if (!guardSet_isGuarded (ttab->guards, el)
2850 && !sRef_isNotNull (sr))
2852 sRef_setDerivNullState (sr, el, NS_DEFNULL);
2860 ue = usymtab_getRefTab (ftab, level, index);
2862 if (!uentry_isLset (ue))
2864 sRef sr = uentry_getSref (ue);
2867 if (!trueGuard) /* yikes! forgot the ! */
2869 sRef_setDerivNullState (sr, el, NS_NOTNULL);
2874 if (!guardSet_isGuarded (ftab->guards, el)
2875 && !sRef_isNotNull (sr))
2877 sRef_setDerivNullState (sr, el, NS_DEFNULL);
2889 usymtab_popBranches (exprNode pred, exprNode tbranch, exprNode fbranch,
2890 bool isOpt, clause cl)
2894 usymtab ftab = utab;
2895 usymtab ttab = utab->env;
2899 guardSet guards = exprNode_getGuards (pred);
2900 sRefSet tguards = guardSet_getTrueGuards (guards);
2901 sRefSet fguards = guardSet_getFalseGuards (guards);
2902 bool mustReturnT = exprNode_mustEscape (tbranch);
2903 bool mustReturnF = exprNode_mustEscape (fbranch);
2905 DPRINTF (("Pop branches: %s [mustreturn: %s/%s]", exprNode_unparse (pred),
2906 bool_unparse (mustReturnT),
2907 bool_unparse (mustReturnF)));
2909 if (exprNode_isDefined (fbranch))
2911 loc = exprNode_loc (fbranch);
2915 loc = exprNode_loc (tbranch);
2918 llassert (usymtab_isDefined (ttab));
2922 llassert (usymtab_isDefined (env));
2923 llassert (ftab->kind == US_FBRANCH);
2924 llassert (ttab->kind == US_TBRANCH);
2927 ** For each element that is true guarded (i.e., if (x != NULL))
2928 ** make x = null in false branch,
2929 ** and x = notnull in true branch.
2930 ** unless x was set locally in that branch.
2931 ** For each element that is false guarded (x == NULL)
2932 ** make x = null in true, notnull in false.
2934 ** For each element that is either guarded (pred(x))
2938 sRefSet_allElements (tguards, el)
2940 updateNullState (el, ttab, ftab, TRUE);
2941 } end_sRefSet_allElements;
2943 sRefSet_allElements (fguards, el)
2945 updateNullState (el, ttab, ftab, FALSE);
2946 } end_sRefSet_allElements;
2950 ** if an entry is in both true and false, merge the entries,
2951 ** then replace original with new state.
2953 ** if an entry is in one table, merge it with the original.
2956 DPRINTF (("ftab: %d", ftab->nentries));
2958 for (i = 0; i < ftab->nentries; i++)
2960 uentry fthis = ftab->entries[i];
2961 uentry old = usymtab_lookupAux (env, uentry_rawName (fthis));
2962 int tindex = usymtab_getIndex (ttab, uentry_rawName (fthis));
2964 DPRINTF (("Entry: %s / %s", uentry_unparseFull (fthis), uentry_unparseFull (old)));
2966 if (uentry_isUndefined (old))
2968 /* possible entry was added as an undefined id */
2969 DPRINTF (("Undefined! %s", uentry_rawName (fthis)));
2973 if (tindex != NOT_FOUND)
2975 uentry tthis = ttab->entries[tindex];
2977 /* note that is this is in a nested branch,
2978 it may create a "new" old entry. */
2984 uentry_mergeState (fthis, tthis, loc,
2985 mustReturnT, FALSE, FALSE, cl);
2989 uentry_mergeUses (fthis, tthis);
2992 uentry_setState (old, fthis);
2999 uentry_setState (old, tthis);
3000 uentry_mergeState (old, fthis, loc, mustReturnF,
3004 ttab->entries[tindex] = uentry_undefined;
3005 uentry_free (tthis);
3009 uentry_mergeState (old, fthis, loc, mustReturnF, TRUE, FALSE, cl);
3013 for (i = 0; i < ttab->nentries; i++)
3015 uentry current = ttab->entries[i];
3017 DPRINTF (("ttab: %s", uentry_unparseFull (current)));
3019 if (!uentry_isUndefined (current))
3021 uentry old = usymtab_lookupAux (env, uentry_rawName (current));
3023 DPRINTF (("Old: %s", uentry_unparseFull (old)));
3025 if (uentry_isUndefined (old))
3027 llcontbug (message ("Undefined entry: %s", uentry_rawName (current)));
3033 uentry_mergeUses (current, old);
3034 uentry_setState (old, current);
3039 ** Assumes false branch is a fall-through if
3040 ** fbranch is not defined. This is true, unless
3041 ** where was some greivous error in processing
3042 ** the else branch of an if-then, in which case
3043 ** this is probably the right thing to do anyway.
3046 uentry_mergeState (old, current, loc, mustReturnT,
3050 DPRINTF (("==> %s", uentry_unparseFull (old)));
3055 ** Plain levelUnion doesn't work, since we need to use the sRef's in env->aliases
3056 ** if they are present.
3059 llassert (NOALIAS (env->aliases, ttab->aliases));
3060 llassert (NOALIAS (env->aliases, ftab->aliases));
3062 aliasTable_free (env->aliases);
3064 env->aliases = aliasTable_levelUnionNew (ttab->aliases,
3065 ftab->aliases, env->lexlevel);
3067 aliasTable_fixSrefs (env->aliases);
3069 DPRINTF (("Aliases: %s", aliasTable_unparse (env->aliases)));
3071 /* exit true and false scopes */
3072 usymtab_quietPlainExitScope ();
3073 usymtab_quietPlainExitScope ();
3077 utab->guards = guardSet_levelUnionFree
3079 guardSet_invert (exprNode_getGuards (pred)),
3085 utab->guards = guardSet_levelUnion (utab->guards,
3086 exprNode_getGuards (pred),
3090 DPRINTF (("Here."));
3093 static void usymtab_fixCases (void) /*@modifies utab@*/ {
3094 while (utab->kind == US_CBRANCH)
3096 usymtab_quietPlainExitScope ();
3099 llassert (utab->kind != US_CBRANCH);
3103 usymtab_altBranch (/*@only@*/ guardSet guards)
3107 usymtab parent = utab->env;
3109 t = usymtab_create (US_FBRANCH, utab, FALSE);
3112 ** If we are in a case, need to close it. The C syntax
3113 ** is very liberal, so this kludge is necessary.
3116 usymtab_fixCases ();
3118 DPRINTF (("Current kind: %s", usymtab_unparseStack ()));
3120 llassert (utab->kind == US_TBRANCH);
3121 llassert (parent != GLOBAL_ENV);
3123 guardSet_free (t->guards);
3126 aliasTable_free (t->aliases);
3127 t->aliases = aliasTable_copy (parent->aliases);
3133 usymtab_allDefined (void)
3134 /*@globals utab, globtab@*/
3138 llassert (utab == globtab);
3140 for (i = 0; i < utab->nentries; i++)
3142 uentry e = utab->entries[i];
3144 if (uentry_isPriv (e))
3146 ; /* no need to define it */
3150 if (context_getFlag (FLG_SPECUNDECL))
3152 fileloc sloc = uentry_whereSpecified (e);
3153 fileloc dloc = uentry_whereDeclared (e);
3155 if (fileloc_isDefined (sloc)
3156 && !uentry_isFakeTag (e)
3157 && !fileloc_isDefined (dloc))
3161 message ("%s %q specified but not declared",
3162 ekind_capName (uentry_getKind (e)),
3163 uentry_getName (e)),
3168 if (!uentry_isCodeDefined (e))
3170 fileloc dloc = uentry_whereDeclared (e);
3172 if (fileloc_isLib (dloc) || fileloc_isXHFile (dloc))
3176 else if (fileloc_isDefined (dloc))
3178 if (!uentry_isAnyTag (e))
3180 if (fileloc_isUser (dloc))
3184 message ("%s %q declared but not defined",
3185 ekind_capName (uentry_getKind (e)),
3186 uentry_getName (e)),
3188 DPRINTF (("decl: %s", uentry_unparseFull (e)));
3194 fileloc sloc = uentry_whereSpecified (e);
3196 if (fileloc_isDefined (sloc)
3197 && !fileloc_isImport (sloc)
3198 && !fileloc_isLib (sloc)
3199 && !fileloc_isPreproc (sloc)
3200 && !uentry_isFakeTag (e))
3202 if (uentry_isVariable (e) || uentry_isFunction (e))
3206 message ("%s %q specified but not declared or defined",
3207 ekind_capName (uentry_getKind (e)),
3208 uentry_getName (e)),
3215 message ("%s %q specified but not defined",
3216 ekind_capName (uentry_getKind (e)),
3217 uentry_getName (e)),
3227 void usymtab_exportHeader (void)
3232 for (i = 0; i < utab->nentries; i++)
3234 uentry ce = utab->entries[i];
3236 if (!uentry_isDatatype (ce)
3237 && !uentry_isAnyTag (ce)
3238 && !uentry_isEitherConstant (ce) && !uentry_isStatic (ce)
3239 && !uentry_isExternal (ce)
3240 && !uentry_isForward (ce))
3242 fileloc fwhere = uentry_whereDeclared (ce);
3244 if (fileloc_isUndefined (fwhere)
3245 && uentry_isFunction (ce))
3247 fwhere = uentry_whereDefined (ce);
3250 if (fileloc_isDefined (fwhere)
3251 && !fileloc_isHeader (fwhere)
3252 && !fileloc_isXHFile (fwhere)
3253 && !(fileloc_isSpecialFile (fwhere)
3254 && !context_getFlag (FLG_UNUSEDSPECIAL)))
3256 if (uentry_isVariable (ce))
3259 (FLG_EXPORTHEADERVAR,
3260 message ("%s %q exported but not declared in header file",
3261 ekind_capName (uentry_getKind (ce)),
3262 uentry_getName (ce)),
3265 uentry_showDefSpecInfo (ce, fwhere);
3270 if (!uentry_isIter (ce)
3271 && !uentry_isEndIter (ce)
3272 && !uentry_isExpandedMacro (ce))
3274 if (uentry_isFunction (ce)
3275 && cstring_equalLit (uentry_rawName (ce), "main"))
3277 ; /* no error for main */
3283 message ("%s %q exported but not declared "
3285 ekind_capName (uentry_getKind (ce)),
3286 uentry_getName (ce)),
3289 uentry_showDefSpecInfo (ce, fwhere);
3299 void usymtab_exportLocal (void)
3304 for (i = 0; i < utab->nentries; i++)
3306 uentry ce = utab->entries[i];
3308 if (!uentry_isDatatype (ce) && !uentry_isAnyTag (ce)
3309 && !uentry_isEitherConstant (ce)
3310 && !uentry_isIter (ce)
3311 && !uentry_isEndIter (ce)
3312 && !uentry_isExpandedMacro (ce)
3313 && uentry_isUsed (ce))
3315 /* check static uses */
3316 filelocList fuses = uentry_getUses (ce);
3317 fileloc mod = uentry_whereDefined (ce);
3318 bool ok = filelocList_isEmpty (fuses);
3319 fileloc fwhere = uentry_whereDeclared (ce);
3321 if (fileloc_isSpecialFile (fwhere)
3322 && !context_getFlag (FLG_UNUSEDSPECIAL))
3324 ok = TRUE; /* no errors for special files */
3328 filelocList_elements (fuses, uloc)
3330 if (fileloc_isUndefined (uloc) || !fileloc_sameModule (uloc, mod))
3333 /*@innerbreak@*/ break;
3335 } end_filelocList_elements;
3342 message ("%s exported but not used outside %s: %q",
3343 ekind_capName (uentry_getKind (ce)),
3344 fileloc_getBase (mod),
3345 uentry_getName (ce)),
3348 uentry_showDefSpecInfo (ce, fwhere);
3356 usymtab_allUsed (void)
3360 bool isFileStatic = usymtab_inFileScope ();
3361 cstring last_file = cstring_undefined;
3363 for (i = 0; i < utab->nentries; i++)
3365 bool hasError = FALSE;
3366 uentry ce = utab->entries[i];
3367 fileloc fwhere = uentry_whereDeclared (ce);
3369 if (fileloc_isUndefined (fwhere))
3371 fwhere = uentry_whereDefined (ce);
3374 if (fileloc_isInvalid (fwhere)
3375 || fileloc_isLib (fwhere)
3376 || fileloc_isBuiltin (fwhere)
3377 || ((fileloc_isSpecialFile (fwhere)
3378 || fileloc_isSpecialFile (uentry_whereDefined (ce)))
3379 && !context_getFlag (FLG_UNUSEDSPECIAL)))
3383 else if (!uentry_wasUsed (ce) && !uentry_isAnyTag (ce))
3385 cstring fname = fileloc_filename (fwhere);
3387 if (cstring_isUndefined (last_file))
3391 else if (cstring_equal (fname, last_file))
3400 if (uentry_isParam (ce))
3402 if (context_inMacro ())
3404 sRef cref = uentry_getSref (ce);
3406 if (uentry_isYield (ce))
3408 ; /* no checks (for now) */
3410 else if (sRef_isSafe (cref))
3416 if (uentry_hasRealName (ce))
3419 optgenerror (FLG_MACROPARAMS,
3420 message ("Macro parameter %q not used",
3421 uentry_getName (ce)),
3428 if (cstring_equalFree (uentry_getName (ce),
3429 cstring_makeLiteral ("...")))
3435 hasError = optgenerror (FLG_PARAMUNUSED,
3436 message ("Parameter %q not used",
3437 uentry_getName (ce)),
3442 else if (uentry_isFunction (ce) || uentry_isIter (ce))
3444 if (fileloc_isUser (fwhere))
3446 hasError = optgenerror
3448 message ("%q %q declared but not used",
3450 (uentry_isIter (ce) ? "Iterator"
3451 : (isFileStatic ? "File static function" : "Function")),
3452 uentry_getName (ce)),
3456 else if (uentry_isEndIter (ce))
3458 ; /* no error (already reported for iter */
3460 else if (uentry_isEnumConstant (ce))
3462 if (fileloc_isUser (fwhere))
3464 hasError = optgenerror
3466 message ("Enum member %q not used",
3467 uentry_getName (ce)),
3471 else if (uentry_isConstant (ce))
3473 if (fileloc_isUser (fwhere))
3475 hasError = optgenerror
3477 message ("Constant %q declared but not used",
3478 uentry_getName (ce)),
3482 else if (uentry_isDatatype (ce))
3484 if (fileloc_isUser (fwhere))
3486 hasError = optgenerror
3488 message ("Type %q declared but not used",
3489 uentry_getName (ce)),
3493 else if (!uentry_isRefParam (ce) && !uentry_isExpandedMacro (ce))
3494 { /* errors for ref params will be reported in the next scope */
3495 llassertprint (uentry_isVar (ce),
3496 ("ce: %s", uentry_unparseFull (ce)));
3498 if (ctype_isFunction (uentry_getType (ce)))
3500 if (fileloc_isUser (fwhere))
3502 hasError = optgenerror
3504 message ("%q %q declared but not used",
3506 (isFileStatic ? "File static function"
3508 uentry_getName (ce)),
3514 if (fileloc_isUser (fwhere))
3518 hasError = optgenerror
3520 message ("%q %q declared but not used",
3522 (isFileStatic ? "File static variable"
3524 uentry_getName (ce)),
3534 else if ((uentry_isDatatype (ce) || uentry_isAnyTag (ce)))
3535 { /* check all fields */
3536 ctype ct = uentry_getRealType (ce);
3539 while (ctype_isAP (ct))
3541 ct = ctype_getBaseType (ct);
3544 if (ctype_isSU (ct))
3546 uentryList fields = ctype_getFields (ct);
3548 uentryList_elements (fields, field)
3550 if (!uentry_isUsed (field))
3552 if (uentry_hasName (ce))
3554 hasError |= optgenerror
3556 message ("Field %q of %s %q declared but not used",
3557 uentry_getName (field),
3558 cstring_makeLiteralTemp
3559 (ctype_isStruct (ct) ? "structure" : "union"),
3560 uentry_getName (ce)),
3561 uentry_whereEarliest (field));
3567 ** Can't report these errors for unnamed structs.
3568 ** No way to tell when there are multiple consistent
3569 ** unnamed structure types. (Could go through table
3570 ** and mark them all unused...)
3572 hasError |= optgenerror
3574 message ("Field %q of unnamed %s declared but not used",
3575 uentry_getName (field),
3576 cstring_makeLiteralTemp
3577 (ctype_isStruct (ct) ? "structure" : "union")),
3578 uentry_whereEarliest (field));
3583 uentry_setUsed (field, fileloc_undefined);
3585 } end_uentryList_elements;
3595 if (uentry_isParam (ce) && context_inMacro ())
3597 if (fileloc_isDefined (uentry_whereSpecified (ce)))
3599 uentry_showWhereSpecified (ce);
3604 uentry_showDefSpecInfo (ce, fwhere);
3607 uentry_setUsed (ce, fileloc_undefined);
3613 checkGlobalReturn (uentry glob, sRef orig)
3615 sRef sr = uentry_getSref (glob);
3617 DPRINTF (("Check global return: %s // orig: %s // sr: %s",
3618 uentry_unparseFull (glob),
3619 sRef_unparseFull (orig),
3620 sRef_unparseFull (sr)));
3622 DPRINTF (("Is killed: %s", bool_unparse (sRef_isKilledGlob (orig))));
3624 if (context_getFlag (FLG_GLOBSTATE))
3626 DPRINTF (("Is killed: %s", sRef_unparseFull (orig)));
3628 if (sRef_isKilledGlob (orig))
3630 if (sRef_isStateUndefined (sr)
3631 || sRef_isUnuseable (sr)
3632 || sRef_isStateUnknown (sr)
3633 || sRef_isDead (sr))
3639 ctype ct = ctype_realType (uentry_getType (glob));
3641 if (ctype_isVisiblySharable (ct))
3646 ("Killed global %q (type %s) not released before return",
3647 uentry_getName (glob),
3648 ctype_unparse (ct)),
3651 sRef_showStateInfo (sr);
3656 sRef_protectDerivs ();
3657 (void) checkGlobalDestroyed (sr, g_currentloc);
3658 sRef_clearProtectDerivs ();
3664 if (sRef_isStateUndefined (sr))
3666 if (optgenerror (FLG_GLOBSTATE,
3668 ("Function returns with global %q undefined",
3669 uentry_getName (glob)),
3672 sRef_showStateInfo (sr);
3677 if (sRef_isDead (sr))
3681 message ("Function returns with global %q "
3682 "referencing released storage",
3683 uentry_getName (glob)),
3686 sRef_showStateInfo (sr);
3687 sRef_setDefState (sr, SS_UNKNOWN, fileloc_undefined);
3691 if (ctype_isRealPointer (uentry_getType (glob))
3692 && sRef_possiblyNull (sr)
3693 && !uentry_possiblyNull (glob))
3697 message ("Function returns with non-null global %q "
3698 "referencing null storage",
3699 uentry_getName (glob)),
3702 sRef_showNullInfo (sr);
3707 checkGlobReturn (glob);
3715 ** remember: check alias globals
3718 void usymtab_checkFinalScope (bool isReturn)
3721 bool mustFree = context_getFlag (FLG_MUSTFREE);
3722 bool mustDefine = context_getFlag (FLG_MUSTDEFINE);
3723 /* bool mustNotAlias = context_getFlag (FLG_MUSTNOTALIAS); */
3724 sRefSet checked = sRefSet_new ();
3725 usymtab stab = utab;
3729 ** need to check all scopes out to function parameters.
3734 for (i = 0; i < stab->nentries; i++)
3736 uentry ce = stab->entries[i];
3737 sRef sr = uentry_getSref (ce);
3738 sRef rb = sRef_getRootBase (sr);
3742 ** Shouldn't check if shadow checked in deeper scope:
3747 uentry oue = usymtab_lookupQuietNoAlt (utab, uentry_observeRealName (ce));
3749 if (!uentry_sameObject (ce, oue))
3751 DPRINTF (("Skipping outer entry: %s / %s", uentry_unparseFull (ce),
3752 uentry_unparseFull (oue)));
3753 /*@i32 what if it is one an alternate branch? */
3754 /*@innercontinue@*/ continue;
3758 DPRINTF (("Here check final scope: %s", uentry_unparseFull (ce)));
3760 if (ctype_isFunction (uentry_getType (ce)))
3762 /*@innercontinue@*/ continue;
3765 if (uentry_isAnyParam (ce)
3766 || uentry_isRefParam (ce)
3767 || sRef_isFileOrGlobalScope (rb))
3769 /* Don't do the loseref check...but should check state! */
3771 else if (sRef_isDefinitelyNull (sr)
3772 || usymtab_isDefinitelyNull (sr))
3775 ** No state reference errors for definitely null references.
3780 DPRINTF (("Lose ref: %s / %s", uentry_unparse (ce),
3781 sRef_unparseFull (sr)));
3783 tvalues = sRef_getValueTable (sr);
3785 valueTable_elements (tvalues, fkey, fval) {
3786 metaStateInfo minfo;
3787 cstring msg = cstring_undefined;
3790 minfo = context_lookupMetaStateInfo (fkey);
3791 llassert (metaStateInfo_isDefined (minfo));
3793 if (stateValue_isError (fval))
3799 DPRINTF (("Check: %s / %s / %s", fkey,
3800 metaStateInfo_unparse (minfo),
3801 stateValue_unparse (fval)));
3803 minfo = context_lookupMetaStateInfo (fkey);
3805 nval = stateCombinationTable_lookupLoseReference
3806 (metaStateInfo_getTransferTable (minfo),
3807 stateValue_getValue (fval), &msg);
3809 if (cstring_isDefined (msg))
3811 /*@i32 print extra info for assignments@*/
3812 DPRINTF (("From: %s", sRef_unparseFull (sr)));
3813 DPRINTF (("Null? %s / %s",
3814 bool_unparse (sRef_isDefinitelyNull (sr)),
3815 bool_unparse (usymtab_isGuarded (sr))));
3820 ("%s loses reference %q in invalid state %q (%s)",
3821 cstring_makeLiteralTemp (isReturn ? "Return" : "Scope exit"),
3822 uentry_getName (ce),
3823 stateValue_unparseValue (fval, minfo),
3827 stateValue_show (fval, minfo);
3831 DPRINTF (("Suppressed transfer error: %s", msg));
3835 } end_valueTable_elements;
3840 DPRINTF (("Check entry: %s", uentry_unparseFull (ce)));
3842 if (!sRefSet_member (checked, sr) && !sRef_isFileOrGlobalScope (rb))
3844 if (ctype_isRealSU (uentry_getType (ce))
3845 && !uentry_isAnyParam (ce)
3846 && !uentry_isRefParam (ce)
3847 && !uentry_isStatic (ce)
3848 && !sRef_isDependent (sr)
3849 && !sRef_isOwned (sr))
3851 sRefSet als = usymtab_allAliases (sr);
3853 if (sRefSet_isEmpty (als))
3855 checkLocalDestroyed (sr, g_currentloc);
3859 /* aliased, no problem */ ;
3865 (!uentry_isStatic (ce)
3866 && ((sRef_isNewRef (sr))
3867 || (((sRef_isOnly (sr) || sRef_isFresh (sr)
3868 || sRef_isKeep (sr) || sRef_isOwned (sr))
3869 && !sRef_isDead (sr))
3870 && (!sRef_definitelyNull (sr))
3871 && (!usymtab_isDefinitelyNull (sr)))))
3873 bool hasError = TRUE;
3875 DPRINTF (("Checking: %s", sRef_unparseFull (sr)));
3878 ** If its a scope exit, check if there is an alias.
3879 ** If so, make it only. If not, there is an error.
3884 if (canLoseReference (sr, g_currentloc))
3886 DPRINTF (("Can lose!"));
3893 if (sRef_hasLastReference (sr))
3895 sRef ar = sRef_getAliasInfoRef (sr);
3900 ("Last reference %q to %s storage %qnot %q before %q",
3902 alkind_unparse (sRef_getAliasKind (sr)),
3903 sRef_unparseOpt (ar),
3904 cstring_makeLiteral (sRef_isKeep (sr)
3905 ? "transferred" : "released"),
3906 cstring_makeLiteral (isReturn
3907 ? "return" : "scope exit")),
3910 sRef_showRefLost (sr);
3913 else if (sRef_isNewRef (sr))
3918 ("%q %q not released before %q",
3920 (alkind_isKillRef (sRef_getOrigAliasKind (sr))
3921 ? "Kill reference parameter" : "New reference"),
3922 uentry_getName (ce),
3923 cstring_makeLiteral (isReturn
3924 ? "return" : "scope exit")),
3927 sRef_showAliasInfo (sr);
3932 if (ctype_isRealSU (sRef_getType (sr)))
3934 checkStructDestroyed (sr, g_currentloc);
3938 DPRINTF (("Here we are: %s", sRef_unparseFull (sr)));
3943 ("%s storage %q not %q before %q",
3944 alkind_capName (sRef_getAliasKind (sr)),
3945 uentry_getName (ce),
3946 cstring_makeLiteral (sRef_isKeep (sr)
3947 ? "transferred" : "released"),
3948 cstring_makeLiteral (isReturn
3949 ? "return" : "scope exit")),
3952 sRef_showAliasInfo (sr);
3953 DPRINTF (("Storage: %s", sRef_unparseFull (sr)));
3964 else if (mustDefine && uentry_isOut (ce))
3966 if (!ynm_toBoolStrict (sRef_isReadable (sr)))
3970 message ("Out storage %q not defined before %q",
3971 uentry_getName (ce),
3973 (isReturn ? "return" : "scope exit")),
3976 /* uentry_showWhereDeclared (ce); */
3985 ** also check state is okay
3988 if (usymtab_lexicalLevel () > functionScope
3989 && uentry_isVariable (ce)
3990 && (sRef_isLocalVar (sr)
3991 && (sRef_isDependent (sr) || sRef_isLocalState (sr))))
3993 sRefSet ab = usymtab_aliasedBy (sr);
3995 /* should do something more efficient here */
3997 if (sRefSet_isEmpty (ab))
3999 /* and no local ref */
4000 DPRINTF (("Check lose ref: %s", uentry_unparseFull (ce)));
4015 checked = sRefSet_insert (checked, sr);
4019 llassert (usymtab_isDefined (stab->env));
4021 if (usymtab_isBranch (stab))
4023 stab = usymtab_dropEnv (stab);
4030 llassert (stab != usymtab_undefined);
4031 } while (isReturn && (stab->lexlevel >= paramsScope));
4033 sRefSet_free (checked);
4037 ** all globals are appropriately defined
4038 ** all parameters are appropriately defined
4039 ** special clauses are followed
4042 if (isReturn || (utab->lexlevel == paramsScope))
4044 uentry fcn = context_getHeader ();
4045 uentryList params = context_getParams ();
4046 globSet uglobs = context_getUsedGlobs ();
4047 globSet sglobs = context_getGlobs ();
4049 if (isReturn && context_maybeSet (FLG_GLOBALIAS))
4051 aliasTable_checkGlobs (utab->aliases);
4055 ** state clauses (ensures, defines, sets, allocates, releases)
4058 if (uentry_hasStateClauseList (fcn))
4060 stateClauseList clauses = uentry_getStateClauseList (fcn);
4062 stateClauseList_elements (clauses, cl)
4064 if (stateClause_isAfter (cl) && !stateClause_isGlobal (cl))
4066 if (stateClause_setsMetaState (cl))
4068 sRefSet rfs = stateClause_getRefs (cl);
4069 qual q = stateClause_getMetaQual (cl);
4070 annotationInfo ainfo = qual_getAnnotationInfo (q);
4071 metaStateInfo minfo = annotationInfo_getState (ainfo);
4072 cstring key = metaStateInfo_getName (minfo);
4073 int mvalue = annotationInfo_getValue (ainfo);
4075 DPRINTF (("Post meta state clause: %s", stateClause_unparse (cl)));
4077 sRefSet_elements (rfs, el)
4079 sRef base = sRef_getRootBase (el);
4081 if (sRef_isResult (base))
4084 ** This is checked for return transfers.
4088 else if (sRef_isParam (base) || sRef_isGlobalMarker (base))
4090 sRef sr = sRef_updateSref (base);
4091 sr = sRef_fixBase (el, sr);
4093 if (!sRef_checkMetaStateValue (sr, key, mvalue))
4098 ("Ensures clause not satisfied%q (state is %q): %q",
4099 sRef_isGlobalMarker (sr)
4101 : message (" by %q", sRef_unparse (sr)),
4102 stateValue_unparseValue (sRef_getMetaStateValue (sr, key),
4104 stateClause_unparse (cl)),
4107 sRef_showMetaStateInfo (sr, key);
4113 if (sRef_isMeaningful (el))
4118 } end_sRefSet_elements ;
4122 /* evs - 2000 07 10 - added this */
4123 sRefTest tst = stateClause_getPostTestFunction (cl);
4124 sRefSet rfs = stateClause_getRefs (cl);
4126 sRefSet_elements (rfs, el)
4128 sRef base = sRef_getRootBase (el);
4130 if (sRef_isResult (base))
4133 ** This is checked for return transfers.
4138 else if (sRef_isParam (base))
4140 sRef sr = sRef_updateSref (base);
4141 sr = sRef_fixBase (el, sr);
4143 if (tst != NULL && !tst (sr))
4146 (stateClause_postErrorCode (cl),
4147 message ("%s storage %qcorresponds to "
4148 "storage listed in %q clause",
4149 stateClause_postErrorString (cl, sr),
4150 sRef_unparseOpt (sr),
4151 stateClause_unparseKind (cl)),
4154 sRefShower ss = stateClause_getPostTestShower (cl);
4161 DPRINTF (("Storage: %s", sRef_unparseFull (sr)));
4167 if (sRef_isMeaningful (el))
4172 } end_sRefSet_elements ;
4175 } end_stateClauseList_elements ;
4179 ** check parameters on return
4182 uentryList_elements (params, arg)
4184 if (!uentry_isElipsisMarker (arg))
4186 ctype rt = ctype_realType (uentry_getType (arg));
4188 if (ctype_isMutable (rt) || ctype_isSU (rt))
4190 uentry param = usymtab_lookupQuiet (utab, uentry_rawName (arg));
4191 DPRINTF (("Check param return: %s", uentry_unparseFull (param)));
4192 checkParamReturn (param);
4195 } end_uentryList_elements;
4197 DPRINTF (("Check global return: %s",
4198 globSet_unparse (sglobs)));
4200 globSet_allElements (sglobs, el)
4202 sRef orig = el; /*! sRef_updateSref (el); */ /*!!!*/
4203 uentry current = sRef_getUentry (el);
4205 DPRINTF (("Check global return: %s / %s", sRef_unparseFull (el),
4206 uentry_unparseFull (current)));
4208 if (uentry_isVariable (current) && !uentry_isRealFunction (current))
4210 checkGlobalReturn (current, orig);
4212 } end_globSet_allElements;
4214 globSet_allElements (uglobs, el)
4216 if (!globSet_member (sglobs, el))
4218 uentry current = sRef_getUentry (el);
4220 if (uentry_isVariable (current)
4221 && !uentry_isRealFunction (current))
4223 checkGlobalReturn (current, sRef_undefined);
4226 } end_globSet_allElements;
4231 usymtab_quietExitScope (fileloc loc)
4232 /*@globals utab, globtab, filetab; @*/
4235 usymtab t = utab->env;
4237 if (utab->reftable != NULL)
4241 for (i = 0; i < utab->nentries; i++)
4243 uentry current = utab->entries[i];
4244 uentry old = usymtab_lookupAux (t, uentry_rawName (current));
4246 uentry_mergeState (old, current, loc, FALSE, FALSE, FALSE, NOCLAUSE);
4250 llassert (t != NULL);
4252 if (t->lexlevel > paramsScope)
4254 t->guards = guardSet_levelUnion (t->guards, utab->guards, t->lexlevel);
4255 t->aliases = aliasTable_levelUnionSeq (t->aliases, utab->aliases,
4257 utab->aliases = aliasTable_undefined;
4260 t->mustBreak = utab->mustBreak;
4261 t->exitCode = utab->exitCode;
4263 usymtab_freeLevel (utab);
4269 ** Exit a scope with no checking, lose alias states.
4270 ** (When should this be used?)
4273 void usymtab_quietPlainExitScope (void)
4274 /*@globals utab, globtab, filetab@*/
4277 usymtab t = utab->env;
4279 llassert (t != NULL);
4280 llassert (NOALIAS (utab->aliases, t->aliases));
4281 usymtab_freeLevel (utab);
4285 void usymtab_exitScope (exprNode expr)
4286 /*@globals utab, filetab, globtab@*/
4287 /*@modifies utab, globtab@*/
4289 usymtab ctab = usymtab_undefined;
4290 usymtab lctab = usymtab_undefined;
4291 bool mustReturn = exprNode_mustEscape (expr);
4293 DPRINTF (("Exit scope"));
4295 if (utab->kind == US_CBRANCH)
4298 ** save the case branches, remove the first non-cbranch
4303 while (utab->kind == US_CBRANCH)
4307 llassert (utab != GLOBAL_ENV);
4311 if (utab->kind == US_TBRANCH || utab->kind == US_FBRANCH
4312 || utab->kind == US_CBRANCH || utab->kind == US_SWITCH) {
4314 if (context_inMacro ()) {
4315 /* evs 2000-07-25 */
4316 /* Unparseable macro may end inside nested scope. Deal with it. */
4318 llerror (FLG_SYNTAX,
4319 message ("Problem parsing macro body of %s (unbalanced scopes). "
4320 "Attempting to recover, recommend /*@notfunction@*/ before "
4321 "macro definition.",
4322 context_inFunctionName ()));
4324 while (utab->kind == US_TBRANCH
4325 || utab->kind == US_FBRANCH
4326 || utab->kind == US_CBRANCH
4327 || utab->kind == US_SWITCH)
4330 llassert (utab != GLOBAL_ENV);
4333 llcontbug (message ("exitScope: in branch: %q", usymtab_unparseStack ()));
4335 } /*@=branchstate@*/
4339 ** check all variables in scope were used
4343 ** bogus errors if this is the normal inside a switch,
4344 ** since cases have not been merged yet. Should probably
4345 ** still check this, but I'm too lazy at the moment...
4348 llassertfatal (utab->env != GLOBAL_ENV);
4350 if (utab->env->kind != US_SWITCH)
4356 ** check aliasing: all only params are released (dead)
4357 ** definition: all out params are defined, all modified params
4358 ** are completely defined
4360 ** NOTE: note for exiting paramsScope, since checkReturn should be
4364 if (!mustReturn && (usymtab_lexicalLevel () > functionScope))
4367 ** should only call this is end of scope is reachable...
4370 usymtab_checkFinalScope (FALSE);
4373 if (usymtab_lexicalLevel () == paramsScope && context_inFunctionLike ())
4376 ** leaving a function, need to fix up globals
4379 uentryList params = context_getParams ();
4380 globSet globs = context_getUsedGlobs ();
4383 uentryList_elements (params, ue)
4385 uentry_fixupSref (ue);
4386 } end_uentryList_elements;
4388 clearFunctionTypes ();
4391 globSet_allElements (globs, el)
4393 if (sRef_isCvar (el))
4396 int index = sRef_getScopeIndex (el);
4398 if (sRef_isFileStatic (el))
4400 llassert (usymtab_isDefined (filetab));
4401 current = usymtab_fetchIndex (filetab, index);
4405 current = usymtab_fetchIndex (globtab, index);
4408 if (uentry_isVariable (current))
4410 uentry_fixupSref (current);
4414 sRef_clearDerived (uentry_getSref (current));
4417 } end_globSet_allElements;
4420 usymtab_quietExitScope (exprNode_loc (expr));
4422 if (lctab != usymtab_undefined)
4424 /*@i@*/ lctab->env = utab;
4425 /*@i@*/ utab = ctab;
4426 /*@-branchstate@*/ } /*@=branchstate@*/
4432 ** yikes! don't let the '170 kids see this one...
4436 uentry_directParamNo (uentry ue)
4438 if (uentry_isVar (ue))
4440 sRef sr = uentry_getSref (ue);
4442 if (sRef_lexLevel (sr) == functionScope)
4447 llassert (sr->info != NULL);
4448 llassert (sr->info->cvar != NULL);
4449 index = sr->info->cvar->index;
4452 if (index < uentryList_size (context_getParams ()))
4461 /*@dependent@*/ /*@exposed@*/ uentry
4462 usymtab_getParam (int paramno)
4466 ** requires in a function context (checked)
4468 ** depends on no nested functions --- the function
4469 ** parameters are ALWAYS one scope inside the global scope
4470 ** and entered in order!
4474 if (!context_inFunctionLike ())
4475 llfatalbug (message ("usymtab_getParam: not in function context: %q",
4476 context_unparse ()));
4478 while (s->lexlevel > paramsScope)
4483 llassert (usymtab_isDefined (s));
4485 if (paramno >= s->nentries)
4488 ** Parse errors lead to this.
4491 uentry err = uentry_makeVariableLoc (cstring_makeLiteralTemp ("<error>"),
4494 uentry_markOwned (err);
4498 return (s->entries[paramno]);
4501 static /*@dependent@*/ /*@exposed@*/ uentry
4502 usymtab_getRefTab (/*@notnull@*/ usymtab u, int level, usymId index)
4506 ue = usymtab_getRefNoisy (u, level, index);
4508 if (uentry_isUndefined (ue))
4510 llbug (message ("usymtab_getRef: out of range: %d. level = %d",
4517 static /*@dependent@*/ /*@exposed@*/ usymtab
4518 usymtab_dropEnv (/*@notnull@*/ usymtab s)
4520 if (s->kind == US_CBRANCH)
4528 llassert (s != GLOBAL_ENV);
4529 } while (s->kind == US_CBRANCH);
4530 /* drop all cases (except in nested scopes */
4533 llassert (s != GLOBAL_ENV);
4536 if (s->kind == US_FBRANCH)
4538 s = s->env; /* skip the true branch */
4539 llassert (usymtab_isDefined (s));
4540 llassert (s->kind == US_TBRANCH);
4543 llassert (s != GLOBAL_ENV);
4549 /*@dependent@*/ /*@exposed@*/ uentry
4550 usymtab_getRefQuiet (int level, usymId index)
4556 llassert (s != NULL);
4557 llassert (index >= 0);
4559 if (level > s->lexlevel)
4561 return uentry_undefined;
4564 llassertprint (level <= s->lexlevel, ("level: %d / lexlevel: %d",
4565 level, s->lexlevel));
4567 while (s->lexlevel > level)
4569 if (usymtab_isBranch (s))
4571 int eindex = refTable_lookup (s, level, index);
4573 if (eindex != NOT_FOUND)
4575 return (s->entries[eindex]);
4579 s = usymtab_dropEnv (s);
4582 while (usymtab_isBranch (s) && s->lexlevel == level)
4584 int eindex = refTable_lookup (s, level, index);
4586 if (eindex != NOT_FOUND)
4588 return (s->entries[eindex]);
4591 s = usymtab_dropEnv (s);
4594 if (index >= s->nentries)
4596 return uentry_undefined;
4599 llassert (!uentry_isUndefined (s->entries[index]));
4601 return s->entries[index];
4604 static /*@dependent@*/ /*@exposed@*/ uentry
4605 usymtab_getRefNoisy (/*@notnull@*/ usymtab s, int level, usymId index)
4608 uentry ue = uentry_undefined;
4610 llassert (index >= 0);
4612 while (s->lexlevel > level)
4614 if (usymtab_isBranch (s))
4616 int eindex = refTable_lookup (s, level, index);
4618 if (eindex != NOT_FOUND)
4620 ue = s->entries[eindex];
4624 while (!usymtab_isBranch (otab))
4626 otab = usymtab_dropEnv (otab);
4627 llassert (otab != GLOBAL_ENV);
4630 if (refTable_lookup (otab, level, index) == NOT_FOUND)
4632 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4644 s = usymtab_dropEnv (s);
4647 llassert (usymtab_isDefined (s));
4649 while (usymtab_isBranch (s) && s->lexlevel == level)
4651 int eindex = refTable_lookup (s, level, index);
4654 if (eindex != NOT_FOUND)
4656 ue = s->entries[eindex];
4660 while (!usymtab_isBranch (otab))
4662 otab = usymtab_dropEnv (otab);
4663 llassert (otab != GLOBAL_ENV);
4666 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4676 s = usymtab_dropEnv (s);
4679 if (s->lexlevel == level && (index < s->nentries))
4681 ue = s->entries[index];
4683 if (uentry_isValid (ue))
4687 while (!usymtab_isBranch (otab))
4689 otab = usymtab_dropEnv (otab);
4691 if (otab == GLOBAL_ENV)
4697 ue = usymtab_addRefEntry (otab, uentry_rawName (ue));
4708 if (index >= s->nentries)
4710 return uentry_undefined;
4713 llassert (!uentry_isUndefined (s->entries[index]));
4715 return s->entries[index];
4719 ** looking up entries
4721 ** If entry is inside a branch, then copy it, and put it into
4722 ** the branch table.
4726 int refTable_lookup (/*@notnull@*/ usymtab ut, int level, int index)
4728 refTable rt = ut->reftable;
4731 llassert (rt != NULL);
4733 for (i = 0; i < ut->nentries; i++)
4735 if (rt[i]->level == level && rt[i]->index == index)
4745 /*@only@*/ refentry refentry_create (int level, int index)
4747 refentry r = (refentry) dmalloc (sizeof (*r));
4755 static /*@dependent@*/ /*@exposed@*/ uentry
4756 usymtab_addRefEntry (/*@notnull@*/ usymtab s, cstring k)
4761 if (ut->reftable == NULL)
4763 DPRINTF (("Adding ref entry without reftable: %s", k));
4764 return uentry_undefined;
4767 llassert (ut->reftable != NULL);
4769 while (s != GLOBAL_ENV)
4771 eindex = usymtab_getIndex (s, k);
4773 if (eindex != NOT_FOUND)
4775 uentry current = s->entries[eindex];
4777 if (uentry_isVar (current) && !ctype_isFunction (uentry_getType (current)))
4781 DPRINTF (("Here: copying %s", uentry_unparse (current)));
4782 ue = uentry_copy (current);
4783 DPRINTF (("Here: copying %s", uentry_unparse (ue)));
4784 usymtab_addEntryQuiet (ut, ue);
4785 DPRINTF (("Okay..."));
4787 if (s->reftable != NULL)
4789 refentry ref = s->reftable[eindex];
4791 ut->reftable[ut->nentries - 1]
4792 = refentry_create (ref->level, ref->index);
4796 ut->reftable[ut->nentries - 1]
4797 = refentry_create (s->lexlevel, eindex);
4808 s = usymtab_dropEnv (s);
4811 return uentry_undefined;
4814 static uentry usymtab_lookupAux (usymtab s, cstring k)
4816 DPRINTF (("Lookup: %s", k));
4818 while (s != GLOBAL_ENV)
4820 int eindex = usymtab_getIndex (s, k);
4822 if (eindex != NOT_FOUND)
4824 uentry ret = s->entries[eindex];
4828 if (s->kind == US_TBRANCH
4829 || s->kind == US_FBRANCH
4830 || s->kind == US_CBRANCH)
4831 /* uentry_isGlobalVariable (ret) && os->lexlevel > fileScope) */
4834 DPRINTF (("Adding global ref entry: %s", k));
4835 ret = usymtab_addRefEntry (os, k);
4836 DPRINTF (("Adding ref entry: %s", uentry_unparseFull (ret)));
4841 DPRINTF (("Found: %s", uentry_unparseFull (ret)));
4845 if (s->kind == US_TBRANCH || s->kind == US_FBRANCH
4846 || s->kind == US_CBRANCH)
4848 /* why isn't this os??? */
4849 uentry ret = usymtab_addRefEntry (s, k);
4850 DPRINTF (("Adding ref entry: %s", uentry_unparseFull (ret)));
4857 return uentry_undefined;
4860 static /*@dependent@*/ /*@exposed@*/ uentry
4861 usymtab_lookupQuietAux (usymtab s, cstring k, bool noalt)
4865 while (s != GLOBAL_ENV)
4867 eindex = usymtab_getIndex (s, k);
4869 if (eindex != NOT_FOUND)
4871 uentry ret = s->entries[eindex];
4875 if (noalt && usymtab_isBranch (s))
4877 s = usymtab_dropEnv (s);
4885 return uentry_undefined;
4888 static /*@exposed@*/ /*@dependent@*/ uentry
4889 usymtab_lookupQuiet (usymtab s, cstring k)
4891 return usymtab_lookupQuietAux (s, k, FALSE);
4894 static /*@exposed@*/ /*@dependent@*/ uentry
4895 usymtab_lookupQuietNoAlt (usymtab s, cstring k)
4897 return usymtab_lookupQuietAux (s, k, TRUE);
4900 /*@dependent@*/ /*@observer@*/ uentry
4901 usymtab_lookupSafe (cstring k)
4904 DPRINTF (("Lookup safe: %s", k));
4905 return (usymtab_lookupAux (utab, k));
4909 usymtab_lookupExpose (cstring k)
4912 uentry ce = usymtab_lookupAux (utab, k);
4914 if (uentry_isUndefined (ce))
4916 llfatalbug (message ("usymtab_lookup: not found: *%s*", k));
4919 if (uentry_isPriv (ce))
4921 llfatalbug (message ("usymtab_lookup: private: *%s*", k));
4927 uentry usymtab_lookupExposeGlob (cstring k)
4929 return (usymtab_lookupGlobSafe (k));
4932 uentry usymtab_lookupGlob (cstring k)
4933 /*@globals globtab@*/
4935 uentry ce = usymtab_lookupAux (globtab, k);
4937 if (uentry_isUndefined (ce))
4938 llfatalbug (message ("usymtab_lookup: not found: %s", k));
4940 if (uentry_isPriv (ce))
4941 llfatalbug (message ("usymtab_lookup: private: %s", k));
4946 /*@observer@*/ uentry
4947 usymtab_lookupGlobSafe (cstring k)
4948 /*@globals globtab@*/
4950 uentry ce = usymtab_lookupAux (globtab, k);
4955 uentry usymtab_lookupEither (cstring k)
4958 uentry ce = usymtab_lookupSafe (k);
4960 if (uentry_isUndefined (ce))
4961 llfatalerror (message ("usymtab_lookup: not found: %s", k));
4968 usymtab_lookupType (cstring k)
4969 /*@globals globtab@*/
4971 usymId uid = usymtab_getTypeId (k);
4973 if (uid == USYMIDINVALID)
4975 llcontbug (message ("usymtab_lookupType: not found: %s", k));
4976 return ctype_unknown;
4979 return (uentry_getRealType (usymtab_getTypeEntry (uid)));
4984 usymtab_lookupAbstractType (cstring k) /*@globals globtab@*/
4986 usymId uid = usymtab_getTypeId (k);
4988 if (uid == USYMIDINVALID)
4990 llcontbug (message ("usymtab_lookupType: not found: %s", k));
4991 return ctype_unknown;
4994 return (uentry_getAbstractType (usymtab_getTypeEntry (uid)));
4998 ** if there is an unnamed lcl-specified struct tag matching
4999 ** the uentryList, return its datatype. Otherwise, returns
5004 usymtab_structFieldsType (uentryList f)
5005 /*@globals globtab@*/
5007 return (usymtab_suFieldsType (f, TRUE));
5011 usymtab_unionFieldsType (uentryList f)
5012 /*@globals globtab@*/
5014 return (usymtab_suFieldsType (f, FALSE));
5018 usymtab_suFieldsType (uentryList f, bool isStruct)
5019 /*@globals globtab@*/
5023 DPRINTF (("Fields: %s", uentryList_unparse (f)));
5025 if (fileloc_isSpec (g_currentloc))
5027 return (ctype_undefined);
5030 for (i = 0; i < globtab->nentries; i++)
5032 uentry current = globtab->entries[i];
5035 ? uentry_isStructTag (current) : uentry_isUnionTag (current)))
5037 if (isFakeTag (uentry_rawName (current)))
5039 ctype ct = uentry_getType (current);
5041 DPRINTF (("Check: %s", ctype_unparse (ct)));
5043 if ((isStruct ? ctype_isStruct (ct) : ctype_isUnion (ct))
5045 (uentry_isSpecified (current)
5046 && uentryList_equivFields (f, ctype_getFields (ct))))
5048 return uentry_getAbstractType (current);
5058 return ctype_undefined;
5062 usymtab_enumEnumNameListType (enumNameList f)
5063 /*@globals globtab@*/
5067 for (i = 0; i < globtab->nentries; i++)
5069 uentry current = globtab->entries[i];
5071 if (uentry_isEnumTag (current))
5073 if (isFakeTag (uentry_rawName (current)))
5075 ctype ct = uentry_getType (current);
5077 if (ctype_isEnum (ct) && (enumNameList_match (f, ctype_elist (ct))))
5079 return uentry_getType (current);
5085 return ctype_undefined;
5089 usymtab_exists (cstring k)
5092 uentry ce = usymtab_lookupSafe (k);
5093 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5097 usymtab_existsReal (cstring k)
5100 uentry ce = usymtab_lookupSafe (k);
5102 return (!(uentry_isUndefined (ce))
5103 && !(uentry_isPriv (ce))
5104 && !(uentry_isExpandedMacro (ce)));
5108 usymtab_existsGlob (cstring k)
5109 /*@globals globtab@*/
5111 uentry ce = usymtab_lookupAux (globtab, k);
5113 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5118 usymtab_existsEither (cstring k)
5121 uentry ce = usymtab_lookupAux (utab, k);
5123 return (uentry_isValid (ce));
5127 usymtab_existsGlobEither (cstring k)
5128 /*@globals globtab@*/
5130 uentry ce = usymtab_lookupAux (globtab, k);
5132 return (uentry_isValid (ce));
5137 usymtab_existsType (cstring k)
5138 /*@globals globtab@*/
5140 uentry ce = usymtab_lookupAux (globtab, k);
5142 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)) && uentry_isDatatype (ce));
5146 usymtab_existsTypeEither (cstring k)
5147 /*@globals globtab@*/
5150 ce = usymtab_lookupAux (globtab, k);
5151 return (uentry_isValid (ce) && uentry_isDatatype (ce));
5155 usymtab_existsStructTag (cstring k) /*@globals globtab@*/
5157 cstring sname = makeStruct (k);
5158 uentry ce = usymtab_lookupAux (globtab, sname);
5159 cstring_free (sname);
5160 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5164 usymtab_existsUnionTag (cstring k) /*@globals globtab@*/
5166 cstring uname = makeUnion (k);
5167 uentry ce = usymtab_lookupAux (globtab, uname);
5169 cstring_free (uname);
5171 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5175 usymtab_existsEnumTag (cstring k) /*@globals globtab@*/
5177 cstring ename = makeEnum (k);
5178 uentry ce = usymtab_lookupAux (globtab, ename);
5180 cstring_free (ename);
5181 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)));
5185 bool usymtab_existsVar (cstring k)
5188 uentry ce = usymtab_lookupSafe (k);
5190 return (!(uentry_isUndefined (ce)) && !(uentry_isPriv (ce)) && (uentry_isVar (ce)));
5199 refTable_free (/*@only@*/ /*@null@*/ refTable x, int nentries)
5205 for (i = 0; i < nentries; i++)
5215 usymtab_freeLevel (/*@notnull@*/ /*@only@*/ usymtab u)
5216 /*@globals globtab, utab, filetab@*/
5220 aliasTable_free (u->aliases);
5222 refTable_free (u->reftable, u->nentries);
5224 if (u == filetab || u == globtab)
5226 for (i = 0; i < u->nentries; i++)
5228 uentry_freeComplete (u->entries[i]);
5233 for (i = 0; i < u->nentries; i++)
5235 uentry_free (u->entries[i]);
5239 guardSet_free (u->guards);
5246 llassert (!cstringTable_isDefined (u->htable));
5254 usymtab_freeAux (/*@only@*/ usymtab u)
5255 /*@globals globtab, utab, filetab@*/
5258 while (u != GLOBAL_ENV)
5261 usymtab_freeLevel (u);
5268 void usymtab_free ()
5269 /*@globals killed utab, globtab, filetab@*/
5273 usymtab_freeAux (utab);
5276 static int usymtab_lexicalLevel (void) /*@globals utab@*/
5278 return (utab->lexlevel);
5281 bool usymtab_inGlobalScope () /*@globals utab, globtab@*/
5283 return (utab == globtab);
5286 bool usymtab_inFileScope () /*@globals utab@*/
5288 return (utab->lexlevel == fileScope);
5291 bool usymtab_inFunctionScope () /*@globals utab@*/
5293 return (utab->lexlevel == functionScope);
5298 usymtab_replaceEntry (uentry s)
5299 /*@globals utab, globtab@*/
5300 /*@modifies utab, s@*/
5302 usymtab_replaceEntryAux (utab, s);
5307 usymtab_matchForwardStruct (usymId u1, usymId u2)
5308 /*@globals globtab@*/
5310 uentry ue1 = usymtab_getTypeEntry (u1);
5311 uentry ue2 = usymtab_getTypeEntry (u2);
5313 if (uentry_isAnyTag (ue2))
5315 ctype reptype = uentry_getType (ue1);
5317 if (ctype_isPointer (reptype))
5319 ctype repbase = ctype_getBaseType (reptype);
5321 if (ctype_isUA (repbase))
5323 typeId rtuid = ctype_typeId (repbase);
5325 if (u2 == rtuid) return TRUE;
5327 if (usymId_isValid (rtuid))
5329 reptype = uentry_getType (usymtab_getTypeEntry (rtuid));
5331 return (ctype_isUA (reptype) && (u2 == (ctype_typeId (reptype))));
5340 void usymtab_addGuards (guardSet guards)
5343 utab->guards = guardSet_union (utab->guards, guards);
5346 static bool usymtab_isGuardedAux (sRef s)
5350 sRef base = sRef_getRootBase (s);
5351 int lowlevel = paramsScope;
5352 int baselevel = sRef_lexLevel (base);
5354 if (sRef_isCvar (base))
5356 lowlevel = baselevel;
5357 if (lowlevel < paramsScope) lowlevel = paramsScope;
5360 while (tab->lexlevel >= lowlevel)
5362 if (guardSet_isGuarded (tab->guards, s))
5365 if (!sRef_definitelyNull (s))
5367 sRef_setNotNull (s, fileloc_undefined);
5373 tab = usymtab_dropEnv (tab);
5379 void usymtab_unguard (sRef s) /*@modifies utab@*/
5382 sRef base = sRef_getRootBase (s);
5383 int lowlevel = paramsScope;
5384 int baselevel = sRef_lexLevel (base);
5386 if (sRef_isCvar (base))
5388 lowlevel = baselevel;
5389 if (lowlevel < paramsScope) lowlevel = paramsScope;
5392 while (tab->lexlevel >= lowlevel)
5394 if (guardSet_isGuarded (tab->guards, s))
5396 guardSet_delete (tab->guards, s);
5399 tab = usymtab_dropEnv (tab);
5403 bool usymtab_isGuarded (sRef s)
5406 return (sRef_aliasCompleteSimplePred (usymtab_isGuardedAux, s));
5409 bool usymtab_isDefinitelyNull (sRef s)
5411 return (sRef_aliasCheckSimplePred (usymtab_isDefinitelyNullAux, s));
5414 bool usymtab_isDefinitelyNullDeep (sRef s)
5416 return (sRef_deepPred (usymtab_isDefinitelyNull, s));
5419 static bool usymtab_isDefinitelyNullAux (sRef s)
5423 sRef base = sRef_getRootBase (s);
5424 int lowlevel = paramsScope;
5426 if (sRef_isCvar (base))
5428 lowlevel = sRef_lexLevel (base);
5429 if (lowlevel < paramsScope) lowlevel = paramsScope;
5432 while (tab->lexlevel >= lowlevel)
5434 if (guardSet_mustBeNull (tab->guards, s))
5439 while (tab->kind == US_CBRANCH)
5444 llassert (usymtab_isDefined (tab));
5446 if (tab->kind == US_FBRANCH)
5449 llassert (tab->kind == US_TBRANCH);
5459 usymtab_printGuards ()
5460 /*@globals utab, globtab@*/
5462 usymtab ttab = utab;
5464 while (ttab != globtab)
5466 llmsg (message ("Guards [%d]: %q", ttab->lexlevel,
5467 guardSet_unparse (ttab->guards)));
5473 usymtab_displayAllUses ()
5474 /*@globals utab, globtab@*/
5478 /* only in top scope */
5479 llassert (utab == globtab);
5481 /* need a copy, so order is not messed up by sort! */
5482 copy = usymtab_shallowCopy (globtab);
5484 qsort (copy->entries, (size_t)copy->nentries,
5485 sizeof (*copy->entries), (int (*)(const void *, const void *)) uentry_xcompareuses);
5487 usymtab_entries (copy, ue)
5489 if (uentry_isValid (ue))
5491 filelocList uses = uentry_getUses (ue);
5492 int size = filelocList_realSize (uses);
5494 if (fileloc_isDefined (uentry_whereDefined (ue))
5495 && !fileloc_isLib (uentry_whereDefined (ue))
5498 llmsg (message ("%q (%q), %d use%&:\n %q",
5499 uentry_getName (ue),
5500 fileloc_unparse (uentry_whereDefined (ue)),
5501 size, filelocList_unparseUses (uses)));
5504 } end_usymtab_entries;
5506 usymtab_shallowFree (copy);
5509 static /*@dependent@*/ /*@exposed@*/ usymtab
5510 usymtab_getFileTab ()
5511 /*@globals filetab@*/
5513 llassert (filetab != NULL);
5519 usymtab_unparseStack ()
5522 return (usymtab_unparseStackTab (utab));
5525 static /*@only@*/ cstring
5526 usymtab_unparseStackTab (usymtab t)
5528 bool firstOne = TRUE;
5529 cstring ret = cstring_makeLiteral ("[");
5531 while (t != GLOBAL_ENV)
5535 ret = message ("%q %q", ret, usymtab_typeName (t));
5540 ret = message ("%q, %q", ret, usymtab_typeName (t));
5545 ret = message ("%q ]", ret);
5549 static /*@only@*/ cstring
5550 usymtab_typeName (/*@notnull@*/ usymtab t)
5554 case US_GLOBAL: return cstring_makeLiteral ("global");
5555 case US_NORMAL: return cstring_makeLiteral ("normal");
5556 case US_TBRANCH: return cstring_makeLiteral ("true");
5557 case US_FBRANCH: return cstring_makeLiteral ("false");
5558 case US_CBRANCH: return cstring_makeLiteral ("case");
5559 case US_SWITCH: return cstring_makeLiteral ("switch");
5565 void usymtab_addMustAlias (/*@exposed@*/ sRef s, /*@exposed@*/ sRef al)
5568 if (!sRef_similar (s, al))
5570 usymtab_addForceMustAlias (s, al);
5575 ** Same as usymtab_addMustAlias, except does not check sRef_isSimilar.
5578 void usymtab_addForceMustAlias (/*@exposed@*/ sRef s, /*@exposed@*/ sRef al)
5581 if (sRef_isMeaningful (s)
5582 && sRef_isMeaningful (al)
5583 && !(sRef_isConst (s) || sRef_isConst (al))
5584 && !(sRef_isAddress (al) && sRef_isDirectParam (sRef_getBase (al))))
5586 utab->aliases = aliasTable_addMustAlias (utab->aliases, s, al);
5587 DPRINTF (("Must alias: %s", aliasTable_unparse (utab->aliases)));
5590 ** for local variable, aliasing is symmetric
5593 if (sRef_isLocalVar (s) && sRef_isLocalVar (al))
5595 utab->aliases = aliasTable_addMustAlias (utab->aliases, al, s);
5604 void usymtab_clearAlias (sRef s)
5605 /*@modifies utab, s@*/
5608 aliasTable_clearAliases (utab->aliases, s);
5611 sRefSet usymtab_allAliases (sRef s)
5614 if (sRef_isMeaningful (s))
5618 ret = sRefSet_unionFree (aliasTable_aliasedBy (utab->aliases, s),
5619 aliasTable_canAlias (utab->aliases, s));
5624 DPRINTF (("NOT A MEANINGFUL SREF!"));
5625 return sRefSet_undefined;
5629 /*@only@*/ sRefSet usymtab_canAlias (sRef s)
5632 if (sRef_isMeaningful (s))
5634 sRefSet res = aliasTable_canAlias (utab->aliases, s);
5638 return sRefSet_undefined;
5641 /*@only@*/ sRefSet usymtab_aliasedBy (sRef s)
5644 return (aliasTable_aliasedBy (utab->aliases, s));
5647 /*@only@*/ cstring usymtab_unparseAliases ()
5650 return (aliasTable_unparse (utab->aliases));
5654 ** Debugging routines:
5655 ** okay to leak storage here, only for debugging
5661 usymtab_printOut (void)
5667 char *ind = mstring_copy (" ");
5669 fprintf (g_msgstream, "<<< [symbol table] >>>\n");
5671 while (s != GLOBAL_ENV && s->env != GLOBAL_ENV)
5673 cstring tname = usymtab_typeName (s);
5677 ind[depth * 3 + 1] = '\0';
5680 fprintf (g_msgstream, "level: %d (%s)\n", s->lexlevel,
5681 cstring_toCharsSafe (tname));
5683 cstring_free (tname);
5685 for (i = 0; i < s->nentries; i++)
5687 cstring us = uentry_unparseFull (s->entries[i]);
5688 fprintf (g_msgstream, "%s\n", cstring_toCharsSafe (us));
5692 if (s->reftable != NULL && s->nentries > 0)
5694 fprintf (g_msgstream, "\t<< Ref table >>\n");
5696 for (i = 0; i < s->nentries; i++)
5698 fprintf (g_msgstream, "\t%s %3d: %d, %d\n", ind, i,
5699 s->reftable[i]->level,
5700 s->reftable[i]->index);
5704 ind[depth * 3 + 1] = ' ';
5708 fprintf (g_msgstream, "<<< end usymtab >>>\n");
5714 usymtab_printTypes ()
5715 /*@globals globtab@*/
5717 usymtab_printAllAux (globtab);
5721 usymtab_printAll (void)
5724 usymtab_printAllAux (utab);
5728 usymtab_printAllAux (usymtab s)
5729 /*@modifies g_msgstream@*/
5733 char *ind = mstring_copy (" ");
5735 printf ("[[[ usymtab ]]]");
5737 while (s != GLOBAL_ENV)
5740 ind[depth * 3 + 1] = '\0';
5742 if (s->env == GLOBAL_ENV)
5746 printf ("level: %d / break: %s / exit: %s\n", s->lexlevel,
5747 cstring_toCharsSafe (bool_unparse (s->mustBreak)),
5748 cstring_toCharsSafe (exitkind_unparse (s->exitCode)));
5752 for (i = looplow; i < s->nentries; i++)
5754 printf ("%s%3d. %s\n", ind, i,
5755 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5760 printf ("level: %d / break: %s / exit: %s\n", s->lexlevel,
5761 cstring_toCharsSafe (bool_unparse (s->mustBreak)),
5762 cstring_toCharsSafe (exitkind_unparse (s->exitCode)));
5764 for (i = 0; i < s->nentries; i++)
5766 printf ("%s%3d %s\n", ind, i,
5767 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5771 ind[depth * 3 + 1] = ' ';
5775 printf ("----------\n");
5779 usymtab_printComplete ()
5784 char *ind = mstring_copy (" ");
5787 while (s != GLOBAL_ENV)
5791 ind[depth * 3 + 1] = '\0';
5794 if (s->env == GLOBAL_ENV)
5798 printf ("level: %d\n", s->lexlevel);
5802 for (i = looplow; i < s->nentries; i++)
5804 printf ("%s%3d %s\n", ind, i,
5805 cstring_toCharsSafe (uentry_unparse (s->entries[i])));
5810 printf ("level: %d\n", s->lexlevel);
5811 for (i = 0; i < s->nentries; i++)
5813 printf ("%s%3d %s\n", ind, i,
5814 cstring_toCharsSafe (uentry_unparse (s->entries[i])));
5818 ind[depth * 3 + 1] = ' ';
5823 printf ("----------\n");
5827 static /*@only@*/ cstring /*@unused@*/
5828 usymtab_unparseLocalAux (/*@notnull@*/ usymtab s)
5830 cstring c = message ("lexlevel: %d\n", s->lexlevel);
5833 for (i = 0; i < s->nentries; i++)
5835 c = message ("%q\n%q", c, uentry_unparseFull (s->entries[i]));
5838 c = message ("%q\n=========", c);
5842 static cstring /*@unused@*/ /*@only@*/
5843 usymtab_unparseLocalList (/*@notnull@*/ usymtab s)
5845 cstring c = message ("[%d/%s/%s] ", s->lexlevel,
5846 bool_unparse (s->mustBreak),
5847 exitkind_unparse (s->exitCode));
5850 for (i = 0; i < s->nentries; i++)
5852 sRef sr = uentry_getSref (s->entries[i]);
5856 c = message ("%q: %q [%b]", c, uentry_getName (s->entries[i]),
5857 sRef_isStateDefined (sr));
5861 c = message ("%q, %q [%b]", c, uentry_getName (s->entries[i]),
5862 sRef_isStateDefined (sr));
5871 usymtab_printLocal (void)
5877 printf ("lexlevel: %d\n", s->lexlevel);
5879 for (i = 0; i < s->nentries; i++)
5881 printf ("%s\n", cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5884 while (s->lexlevel > 1)
5889 llassert (usymtab_isDefined (s));
5891 printf ("Params:\n");
5893 for (i = 0; i < s->nentries; i++)
5895 printf ("%d: %s\n", i,
5896 cstring_toCharsSafe (uentry_unparseFull (s->entries[i])));
5901 static bool checkDistinctExternalName (uentry e)
5902 /*@globals globtab@*/
5903 /*@modifies *g_msgstream@*/
5905 int checklen = context_getValue (FLG_EXTERNALNAMELEN);
5906 bool ignorecase = context_getFlag (FLG_EXTERNALNAMECASEINSENSITIVE);
5907 bool gotone = FALSE;
5908 bool extras = FALSE;
5909 bool hasError = FALSE;
5910 cstring name = uentry_rawName (e);
5911 usymtab st = globtab;
5919 if (uentry_isAnyTag (e))
5921 checklen++; /* the tag marker doesn't count */
5925 usymtab_entries (st, oe)
5927 if (uentry_sameObject (oe, e))
5934 if (cstring_equalCaseInsensitive (uentry_rawName (oe), name))
5943 (FLG_DISTINCTEXTERNALNAMES,
5945 ("External identifier %q is not distinguishable from %q "
5946 "because alphabetical case is ignored",
5948 uentry_getName (oe)),
5949 uentry_whereLast (e)))
5951 uentry_showWhereAny (oe);
5952 uentry_setHasNameError (oe);
5961 if (cstring_equalLenCaseInsensitive (uentry_rawName (oe),
5970 if (cstring_equalLen (uentry_rawName (oe), name, checklen))
5973 (FLG_DISTINCTEXTERNALNAMES,
5976 ("External identifier %q is not distinguishable from %q "
5977 "in the first %d characters (%q)",
5979 uentry_getName (oe),
5981 cstring_clip (uentry_getName (e), checklen)),
5983 uentry_whereLast (e)))
5985 uentry_showWhereAny (oe);
5986 uentry_setHasNameError (oe);
5999 (FLG_DISTINCTEXTERNALNAMES,
6001 ("External identifier %q is not distinguishable from %q "
6002 "in the first %d characters because alphabetical case "
6005 uentry_getName (oe),
6007 uentry_whereLast (e)))
6009 uentry_showWhereAny (oe);
6010 uentry_setHasNameError (oe);
6016 else if (cstring_equalLen (uentry_rawName (oe), name, checklen))
6025 (FLG_DISTINCTEXTERNALNAMES,
6028 ("External identifier %q is not distinguishable from %q "
6029 "in the first %d characters (%q)",
6031 uentry_getName (oe),
6033 cstring_clip (uentry_getName (e), checklen)),
6035 uentry_whereLast (e)))
6037 uentry_showWhereAny (oe);
6038 uentry_setHasNameError (oe);
6047 } end_usymtab_entries ;
6054 (cstring_makeLiteral ("One or more additional "
6055 "indistinguishable external "
6056 "names not reported"));
6062 static bool checkDistinctInternalName (uentry e)
6064 /*@modifies *g_msgstream@*/
6066 usymtab ttab = utab;
6067 cstring name = uentry_rawName (e);
6068 int numchars = context_getValue (FLG_INTERNALNAMELEN);
6069 bool caseinsensitive = context_getFlag (FLG_INTERNALNAMECASEINSENSITIVE);
6070 bool lookalike = context_getFlag (FLG_INTERNALNAMELOOKALIKE);
6072 if (uentry_isAnyTag (e) && (numchars != 0))
6074 numchars++; /* the tag marker doesn't count */
6077 while (usymtab_isDefined (ttab))
6079 usymtab_entries (ttab, oe)
6081 if (uentry_sameObject (oe, e))
6083 /*@innercontinue@*/ continue;
6086 switch (cstring_genericEqual
6087 (name, uentry_rawName (oe),
6088 numchars, caseinsensitive, lookalike))
6090 case CGE_DISTINCT: /* okay */
6094 if (cstring_equal (name, uentry_rawName (oe)))
6096 ; /* got a shadow error */
6101 (FLG_DISTINCTINTERNALNAMES,
6104 ("Internal identifier %q is not distinguishable from %q "
6105 "in the first %d characters (%q)",
6107 uentry_getName (oe),
6109 cstring_clip (uentry_getName (e), numchars)),
6111 uentry_whereLast (e)))
6113 uentry_showWhereAny (oe);
6114 uentry_setHasNameError (oe);
6122 || (cstring_length (name) <= numchars))
6125 (FLG_DISTINCTINTERNALNAMES,
6127 ("Internal identifier %q is not distinguishable from %q "
6128 "without case sensitivity",
6130 uentry_getName (oe)),
6131 uentry_whereLast (e)))
6133 uentry_showWhereAny (oe);
6134 uentry_setHasNameError (oe);
6141 (FLG_DISTINCTINTERNALNAMES,
6143 ("Internal identifier %q is not distinguishable from %q "
6144 "in the first %d characters without case sensitivity",
6146 uentry_getName (oe),
6148 uentry_whereLast (e)))
6150 uentry_showWhereAny (oe);
6151 uentry_setHasNameError (oe);
6159 || (cstring_length (name) <= numchars))
6162 (FLG_DISTINCTINTERNALNAMES,
6164 ("Internal identifier %q is not distinguishable from %q "
6165 "except by lookalike characters",
6167 uentry_getName (oe)),
6168 uentry_whereLast (e)))
6170 uentry_showWhereAny (oe);
6171 uentry_setHasNameError (oe);
6178 (FLG_DISTINCTINTERNALNAMES,
6180 ("Internal identifier %q is not distinguishable from %q "
6181 "in the first %d characters except by lookalike characters",
6183 uentry_getName (oe),
6185 uentry_whereLast (e)))
6187 uentry_showWhereAny (oe);
6188 uentry_setHasNameError (oe);
6193 } end_usymtab_entries ;
6201 void usymtab_checkDistinctName (uentry e, int scope)
6202 /*@globals utab, globtab@*/
6204 bool hasError = FALSE;
6205 fileloc where = uentry_whereLast (e);
6207 if (!fileloc_isPreproc (where) && !fileloc_isBuiltin (where))
6209 if (scope == globScope)
6211 if (context_getFlag (FLG_DISTINCTEXTERNALNAMES))
6213 hasError = checkDistinctExternalName (e);
6217 if (!hasError && context_getFlag (FLG_DISTINCTINTERNALNAMES))
6219 hasError = checkDistinctInternalName (e);
6225 uentry_setHasNameError (e);
6229 /*@exposed@*/ sRef usymtab_lookupGlobalMarker (void) /*@globals utab@*/
6233 ue = usymtab_lookupAux (utab, GLOBAL_MARKER_NAME);
6234 llassert (uentry_isValid (ue));
6236 return uentry_getSref (ue);