2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2000 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
25 ** usymtab_interface.c
27 ** Grammar interface to symtab.
29 ** The LCLint parser will build symbol tables for abstract types and
30 ** function declarations.
34 # include "lclintMacros.nf"
38 # include "lclsyntable.h"
39 # include "lslparse.h"
40 # include "usymtab_interface.h"
41 # include "structNames.h"
44 declareFcnAux (fcnNode p_f, /*@only@*/ qtype p_qt, ctype p_ct, typeId p_tn,
45 bool p_priv, bool p_spec);
47 static uentryList paramNodeList_toUentryList (paramNodeList p_p);
48 static /*@observer@*/ cstring getVarName (/*@null@*/ typeExpr p_x);
49 static qtype convertLclTypeSpecNode (/*@null@*/ lclTypeSpecNode p_n);
50 static ctype convertTypeExpr (ctype p_c, /*@null@*/ typeExpr p_x);
51 static ctype convertCTypeExpr (ctype p_c, /*@null@*/ typeExpr p_x);
52 static /*@exposed@*/ sRef fixTermNode (termNode p_n, fcnNode p_f, uentryList p_cl);
53 static sRefSet fixModifies (fcnNode p_f, uentryList p_cl);
56 convertuentryList (stDeclNodeList x)
58 uentryList fl = uentryList_new ();
61 stDeclNodeList_elements (x, i)
63 declaratorNodeList d = i->declarators;
64 qtype q = convertLclTypeSpecNode (i->lcltypespec);
66 declaratorNodeList_elements (d, j)
70 qtype_setType (q, convertTypeExpr (qtype_getType (q), j->type));
71 id = idDecl_create (cstring_copy (getVarName (j->type)), qtype_copy (q));
72 fl = uentryList_add (fl, uentry_makeIdVariable (id));
74 } end_declaratorNodeList_elements;
77 } end_stDeclNodeList_elements;
84 convert_uentryList (paramNodeList x)
86 uentryList p = uentryList_undefined;
87 bool first_one = TRUE;
90 paramNodeList_elements (x, i)
92 if (i != (paramNode) 0)
94 if (paramNode_isElipsis (i))
97 p = uentryList_add (p, uentry_makeElipsisMarker ());
101 qtype q = convertLclTypeSpecNode (i->type);
102 typeExpr t = i->paramdecl;
104 qtype_setType (q, convertTypeExpr (qtype_getType (q), t));
106 /* note: has to be like this to hack around void ???? still */
110 if (ctype_isVoid (qtype_getType (q)))
112 llassert (uentryList_isUndefined (p));
121 ** don't do qualifiers here, will get errors later
124 p = uentryList_add (p, uentry_makeUnnamedVariable (qtype_getType (q)));
130 llbug (cstring_makeLiteral ("convertuentryList: null paramNode"));
132 } end_paramNodeList_elements;
136 llassert (uentryList_isUndefined (p));
138 p = uentryList_makeMissingParams ();
147 ** modify c with pointer, array, function
149 ** (based on printTypeExpr2 from abstract.c)
154 convertTypeExpr (ctype c, typeExpr x)
156 if (x == (typeExpr) 0)
166 return (convertTypeExpr (ctype_makePointer (c), x->content.pointer));
168 return (convertTypeExpr (ctype_makeArray (c), x->content.array.elementtype));
171 ctype rv = convertTypeExpr (c, x->content.function.returntype);
172 uentryList p = paramNodeList_toUentryList (x->content.function.args);
174 if (x->content.function.returntype != NULL
175 && x->content.function.returntype->wrapped == 1
176 && ctype_isPointer (rv))
178 rv = ctype_baseArrayPtr (rv);
181 return (ctype_makeParamsFunction (rv, p));
185 llfatalbug (message ("convertTypeExpr: unknown typeExprKind: %d",
194 ctype convertCTypeExpr (ctype c, typeExpr x)
196 if (x == (typeExpr) 0)
203 case TEXPR_BASE: return (c);
204 case TEXPR_PTR: return (convertCTypeExpr (ctype_makePointer (c),
205 x->content.pointer));
206 case TEXPR_ARRAY: return (convertCTypeExpr (ctype_makeArray (c),
207 x->content.array.elementtype));
210 ctype rv = convertCTypeExpr (c, x->content.function.returntype);
211 uentryList p = convert_uentryList (x->content.function.args);
213 return (ctype_makeParamsFunction (rv, p));
217 llfatalbug (message ("convertCTypeExpr: unknown typeExprKind: %d", (int) x->kind));
224 ** convertLclTypeSpecNode
226 ** LclTypeSpecNode --> ctype
227 ** this is the base type only!
233 ** for now, assume only last leaf is relevant.
234 ** this should be a safe assumption in general???
238 convertLeaves (ltokenList f)
240 ctype c = ctype_unknown;
242 ltokenList_reset (f);
244 ltokenList_elements (f, current)
246 switch (ltoken_getCode (current))
248 case LLT_TYPEDEF_NAME:
250 cstring tn = ltoken_getRawString (current);
252 if (usymtab_existsTypeEither (tn))
254 c = ctype_combine (uentry_getAbstractType
255 (usymtab_lookupEither (tn)), c);
257 else if (cstring_equalLit (tn, "bool"))
260 ** Bogus...keep consistent with old lcl builtin.
266 fileloc loc = fileloc_fromTok (current);
268 voptgenerror (FLG_UNRECOG,
269 message ("Unrecognized type: %s", tn), loc);
274 (tn, ctype_unknown, MAYBE, NO, fileloc_getBuiltin ()));
277 /*@switchbreak@*/ break;
280 c = ctype_combine (ctype_char, c);
281 /*@switchbreak@*/ break;
284 c = ctype_combine (ctype_double, c);
285 /*@switchbreak@*/ break;
287 c = ctype_combine (ctype_float, c);
288 /*@switchbreak@*/ break;
291 /*@switchbreak@*/ break;
293 c = ctype_combine (ctype_int, c);
294 /*@switchbreak@*/ break;
296 c = ctype_combine (c, ctype_lint);
297 /*@switchbreak@*/ break;
299 c = ctype_combine (c, ctype_sint);
300 /*@switchbreak@*/ break;
302 c = ctype_combine (c, ctype_int);
303 /*@switchbreak@*/ break;
305 c = ctype_combine (c, ctype_uint);
306 /*@switchbreak@*/ break;
308 c = ctype_combine (ctype_unknown, c);
309 /*@switchbreak@*/ break;
311 c = ctype_combine (ctype_void, c);
312 /*@switchbreak@*/ break;
314 llcontbug (cstring_makeLiteral ("convertLeaves: enum"));
316 /*@switchbreak@*/ break;
318 llfatalbug (message ("convertLeaves: bad token: %q",
319 ltoken_unparseCodeName (current)));
321 } end_ltokenList_elements;
327 convertEnumList (ltokenList enums)
329 enumNameList el = enumNameList_new ();
331 if (ltokenList_isDefined (enums))
333 ltokenList_elements (enums, i)
336 (el, enumName_create (cstring_copy (ltoken_unparse (i))));
337 } end_ltokenList_elements;
343 static /*@only@*/ qtype
344 convertLclTypeSpecNode (/*@null@*/ lclTypeSpecNode n)
347 if (n != (lclTypeSpecNode) 0)
355 qtype c1 = convertLclTypeSpecNode (n->content.conj->a);
356 qtype c2 = convertLclTypeSpecNode (n->content.conj->b);
362 if (fileloc_isLib (g_currentloc)
363 || fileloc_isStandardLibrary (g_currentloc))
365 result = qtype_mergeImplicitAlt (c1, c2);
369 result = qtype_mergeAlt (c1, c2);
375 llassert (n->content.type != NULL);
376 result = qtype_create (convertLeaves (n->content.type->ctypes));
378 case LTS_STRUCTUNION:
381 cstring cn = cstring_undefined;
383 sn = n->content.structorunion;
385 llassert (sn != (strOrUnionNode) 0);
387 if (!ltoken_isUndefined (sn->opttagid))
389 cn = cstring_copy (ltoken_getRawString (sn->opttagid));
399 if (usymtab_existsStructTag (cn))
402 result = qtype_create (uentry_getAbstractType
403 (usymtab_lookupStructTag (cn)));
408 uentryList fl = convertuentryList (sn->structdecls);
411 ct = ctype_createStruct (cstring_copy (cn), fl);
414 ** If it was a forward declaration, this could add it to
415 ** the table. Need to check if it exists again...
418 if (usymtab_existsStructTag (cn))
420 result = qtype_create (uentry_getAbstractType
421 (usymtab_lookupStructTag (cn)));
425 fileloc loc = fileloc_fromTok (n->content.structorunion->tok);
426 uentry ue = uentry_makeStructTag (cn, ct, loc);
428 result = qtype_create (usymtab_supTypeEntry (ue));
433 /*@switchbreak@*/ break;
435 if (usymtab_existsUnionTag (cn))
438 result = qtype_create (uentry_getAbstractType
439 (usymtab_lookupUnionTag (cn)));
447 fl = convertuentryList (sn->structdecls);
448 ct = ctype_createUnion (cstring_copy (cn), fl);
451 ** If it was a forward declaration, this could add it to
452 ** the table. Need to check if it exists again...
457 if (usymtab_existsUnionTag (cn))
460 result = qtype_create (uentry_getAbstractType
461 (usymtab_lookupUnionTag (cn)));
465 fileloc loc = fileloc_fromTok (n->content.structorunion->tok);
466 uentry ue = uentry_makeUnionTag (cn, ct, loc);
468 result = qtype_create (usymtab_supTypeEntry (ue));
473 /*@switchbreak@*/ break;
480 enumSpecNode e = n->content.enumspec;
487 llassert (e != NULL);
488 el = convertEnumList (e->enums);
490 if (!ltoken_isUndefined (e->opttagid)) /* named enumerator */
492 ename = cstring_copy (ltoken_getRawString (e->opttagid));
499 cet = ctype_createEnum (ename, el);
501 if (usymtab_existsEnumTag (ename))
503 ta = uentry_getAbstractType (usymtab_lookupEnumTag (ename));
507 fileloc loc = fileloc_fromTok (e->tok);
508 uentry ue = uentry_makeEnumTag (ename, cet, loc);
510 ta = usymtab_supTypeEntry (ue);
513 enumNameList_elements (el, en)
520 ltokenList_reset (e->enums);
525 ltokenList_advance (e->enums);
528 loc = fileloc_fromTok (ltokenList_current (e->enums));
529 ue = uentry_makeSpecEnumConstant (en, cet, loc);
532 ** Can't check name here, might not have
533 ** type yet. Will check in .lh file?
536 ue = usymtab_supGlobalEntryReturn (ue);
538 if (context_inLCLLib ())
540 uentry_setDefined (ue, loc);
542 } end_enumNameList_elements;
544 result = qtype_create (ta);
549 llfatalbug (message ("convertLclTypeSpecNode: unknown lclTypeSpec kind: %d",
554 result = qtype_addQualList (result, n->quals);
558 qtype_adjustPointers (n->pointers, result);
565 llcontbug (cstring_makeLiteral ("convertLclTypeSpecNode: null"));
566 return qtype_unknown ();
571 static /*@only@*/ multiVal
572 literalValue (ctype ct, ltoken lit)
574 cstring text = cstring_fromChars (lsymbol_toChars (ltoken_getText (lit)));
577 if (cstring_length (text) > 0)
579 first = cstring_firstChar (text);
583 return multiVal_unknown ();
587 if /*@-usedef@*/ (first == '\"') /*@=usedef@*/
589 int len = cstring_length (text) - 2;
590 char *val = mstring_create (len);
592 llassert (cstring_lastChar (text) == '\"');
593 strncpy (val, cstring_toCharsSafe (text) + 1, size_fromInt (len));
594 return (multiVal_makeString (cstring_fromCharsO (val)));
597 if (ctype_isDirectInt (ct) || ctype_isPointer (ct))
601 if (sscanf (cstring_toCharsSafe (text), "%ld", &val) == 1)
603 return multiVal_makeInt (val);
607 return multiVal_unknown ();
614 ** unfortunately, because the abstract types are different, this
615 ** cannot be easily subsumed into declareVar.
619 doDeclareConstant (constDeclarationNode c, bool priv)
625 if (c == (constDeclarationNode) 0)
631 qt = convertLclTypeSpecNode (t);
633 ctx = qtype_getType (qt);
635 initDeclNodeList_elements (c->decls, i)
637 ctype ct = convertTypeExpr (ctx, i->declarator->type);
638 cstring s = getVarName (i->declarator->type);
640 if (ctype_isFunction (ct))
642 fcnNode fcn = fcnNode_fromDeclarator (lclTypeSpecNode_copy (t),
643 declaratorNode_copy (i->declarator));
645 /* FALSE == unspecified function, only a declaration */
647 doDeclareFcn (fcn, typeId_invalid, priv, FALSE);
653 fileloc loc = fileloc_fromTok (i->declarator->id);
655 if (i->value != (termNode)0 &&
656 i->value->kind == TRM_LITERAL)
658 ue = uentry_makeConstantAux (s, ct, loc, priv,
659 literalValue (ct, i->value->literal));
663 ue = uentry_makeConstantAux (s, ct, loc, priv, multiVal_unknown ());
666 uentry_reflectQualifiers (ue, qtype_getQuals (qt));
669 if (context_inLCLLib () && !priv)
671 uentry_setDefined (ue, loc);
674 usymtab_supGlobalEntry (ue);
676 } end_initDeclNodeList_elements;
682 getVarName (/*@null@*/ typeExpr x)
684 cstring s = cstring_undefined;
686 if (x != (typeExpr) 0)
691 s = ltoken_getRawString (x->content.base);
694 s = getVarName (x->content.pointer);
697 s = getVarName (x->content.array.elementtype);
700 s = getVarName (x->content.function.returntype);
703 llfatalbug (message ("getVarName: unknown typeExprKind: %d", (int) x->kind));
711 doDeclareVar (varDeclarationNode v, bool priv)
716 if (v == (varDeclarationNode) 0)
722 c = convertLclTypeSpecNode (t);
724 initDeclNodeList_elements (v->decls, i)
726 ctype ct = convertTypeExpr (qtype_getType (c), i->declarator->type);
727 cstring s = getVarName (i->declarator->type);
729 qtype_setType (c, ct);
731 if (ctype_isFunction (ct))
736 fcn = fcnNode_fromDeclarator (lclTypeSpecNode_copy (t),
737 declaratorNode_copy (i->declarator));
739 /* FALSE == unspecified function, only a declaration */
740 declareFcnAux (fcn, qtype_unknown (), ct,
741 typeId_invalid, priv, FALSE);
746 fileloc loc = fileloc_fromTok (i->declarator->id);
747 uentry le = uentry_makeVariable (s, ct, loc, priv);
749 uentry_reflectQualifiers (le, qtype_getQuals (c));
751 if (uentry_isCheckedUnknown (le))
753 if (context_getFlag (FLG_IMPCHECKEDSTRICTSPECGLOBALS))
755 uentry_setCheckedStrict (le);
757 else if (context_getFlag (FLG_IMPCHECKEDSPECGLOBALS))
759 uentry_setChecked (le);
761 else if (context_getFlag (FLG_IMPCHECKMODSPECGLOBALS))
763 uentry_setCheckMod (le);
771 if (context_inLCLLib () && !priv)
773 uentry_setDefined (le, loc);
776 if (initDeclNode_isRedeclaration (i))
778 usymtab_replaceEntry (le);
782 le = usymtab_supEntrySrefReturn (le);
785 } end_initDeclNodeList_elements;
791 processGlob (/*@returned@*/ globSet globs, varDeclarationNode v)
793 if (v == (varDeclarationNode) 0)
800 globs = globSet_insert (globs, v->sref);
804 lclTypeSpecNode t = v->type;
805 qtype qt = convertLclTypeSpecNode (t);
806 ctype c = qtype_getType (qt);
809 initDeclNodeList_elements (v->decls, i)
813 qualList quals = qtype_getQuals (qt);
815 s = getVarName (i->declarator->type);
816 ue = usymtab_lookupGlobSafe (s);
818 if (uentry_isInvalid (ue))
820 ; /* error already reported */
824 if (uentry_isPriv (ue))
826 globs = globSet_insert (globs, sRef_makeSpecState ());
830 uentry ce = uentry_copy (ue);
831 ctype lt = uentry_getType (ce);
832 fileloc loc = fileloc_fromTok (i->declarator->id);
834 ct = convertTypeExpr (c, i->declarator->type);
836 if (!ctype_match (lt, ct))
839 (lt, exprNode_undefined,
840 ct, exprNode_undefined,
841 message ("Global type mismatch %s (%t, %t)",
846 uentry_reflectQualifiers (ce, quals);
847 globs = globSet_insert (globs,
848 sRef_copy (uentry_getSref (ce)));
853 } end_initDeclNodeList_elements;
862 declareAbstractType (abstractNode n, bool priv)
870 if (n == (abstractNode) 0)
876 tn = ltoken_getRawString (n->name);
878 loc = fileloc_fromTok (n->tok);
880 ue = uentry_makeDatatypeAux (tn, ctype_unknown,
881 ynm_fromBool (n->isMutable), YES, loc, priv);
885 uentry_setRefCounted (ue);
888 if (context_inLCLLib () && !priv)
890 uentry_setDefined (ue, loc);
893 uid = usymtab_supAbstractTypeEntry (ue, context_inLCLLib() && !priv);
896 if (!priv && (ab = n->body) != (abstBodyNode) 0)
898 fcnNodeList ops = ab->fcns;
900 if (!fcnNodeList_isEmpty (ops))
902 fcnNodeList_elements (ops, i)
904 if (i->typespec == (lclTypeSpecNode) 0)
906 cstring fname = ltoken_getRawString (i->name);
908 if (usymtab_exists (fname))
910 uentry e = usymtab_lookup (fname);
911 fileloc floc = fileloc_fromTok (i->declarator->id);
913 if (uentry_isForward (e))
916 (uentry_makeTypeListFunction
917 (fname, typeIdSet_insert (uentry_accessType (e), uid),
923 (uentry_makeSpecFunction
924 (fname, uentry_getType (e),
925 typeIdSet_insert (uentry_accessType (e), uid),
930 if (context_inLCLLib ())
932 llbuglit ("Jolly jeepers Wilma, it ain't dead after all!");
939 (uentry_makeForwardFunction (fname, uid, loc));
946 } end_fcnNodeList_elements;
952 declareExposedType (exposedNode n, bool priv)
959 if (n == (exposedNode) 0)
964 c = convertLclTypeSpecNode (n->type);
966 declaratorInvNodeList_elements (n->decls, i)
968 ctype realType = convertTypeExpr (qtype_getType (c), i->declarator->type);
969 fileloc loc = fileloc_fromTok (i->declarator->id);
972 s = getVarName (i->declarator->type);
974 ue = uentry_makeDatatypeAux (s, realType, MAYBE, NO, loc, priv);
976 uentry_reflectQualifiers (ue, qtype_getQuals (c));
978 if (context_inLCLLib () && !priv)
980 uentry_setDefined (ue, loc);
983 uid = usymtab_supExposedTypeEntry (ue, context_inLCLLib () && !priv);
984 } end_declaratorInvNodeList_elements;
990 ** ah...remember ye old days...
992 ** wow...same thing in THREE symbol tables! talk about space efficiency
993 ** (or as Joe Theory once said, its only a constant factor)
997 doDeclareType (typeNode t, bool priv)
1000 if (t != (typeNode) 0)
1005 declareAbstractType (t->content.abstract, priv);
1009 declareExposedType (t->content.exposed, priv);
1015 llfatalbug (message ("declareType ERROR: unknown kind: %q",
1016 cstring_fromCharsO (FormatInt ((int)t->kind))));
1024 declareIter (iterNode iter)
1026 fileloc loc = fileloc_fromTok (iter->name);
1028 uentry_makeIter (ltoken_unparse (iter->name),
1031 paramNodeList_toUentryList (iter->params)),
1032 fileloc_copy (loc));
1034 usymtab_supEntry (ue);
1036 (uentry_makeEndIter (ltoken_unparse (iter->name), loc));
1044 declareFcnAux (fcnNode f, /*@only@*/ qtype qt, ctype ct,
1045 typeId tn, bool priv, bool spec)
1049 sRefSet sl = sRefSet_undefined;
1050 globSet globlist = globSet_undefined;
1051 cstring s = getVarName (f->declarator->type);
1052 fileloc loc = fileloc_fromTok (f->declarator->id);
1056 ** type conversion generates args
1059 if (ctype_isFunction (ct))
1061 args = ctype_argsFunction (ct);
1065 llcontbug (message ("Not function: %s", ctype_unparse (ct)));
1066 args = uentryList_undefined;
1070 fileloc_setColumnUndefined (loc);
1074 globals = f->globals;
1076 sl = fixModifies (f, args);
1079 ** Bind let declarations in modifies list
1082 varDeclarationNodeList_elements (globals, glob)
1084 globlist = processGlob (globlist, glob);
1085 } end_varDeclarationNodeList_elements;
1088 if (f->checks != (lclPredicateNode) 0)
1089 /* push stderr on globalList */
1090 /* modifies *stderr^ */
1094 if (!(usymtab_existsVar (cstring_makeLiteralTemp ("stderr"))))
1098 llmsglit ("Global stderr implied by checks clause, "
1099 "not declared in initializations.");
1101 tfile = usymtab_lookupType (cstring_makeLiteralTemp ("FILE"));
1103 if (ctype_isUndefined (tfile))
1105 llmsglit ("FILE datatype implied by checks clause not defined.");
1106 tfile = ctype_unknown;
1109 usymtab_supGlobalEntry
1110 (uentry_makeVariable (cstring_makeLiteralTemp ("stderr"),
1111 tfile, fileloc_getBuiltin (), FALSE));
1114 ue = usymtab_lookupGlob (cstring_makeLiteralTemp ("stderr"));
1116 globlist = globSet_insert (globlist, sRef_copy (uentry_getSref (ue)));
1117 sl = sRefSet_insert (sl, sRef_buildPointer (uentry_getSref (ue)));
1122 if (usymId_isInvalid (tn))
1124 acct = context_fileAccessTypes ();
1128 acct = typeIdSet_single (tn);
1131 if (usymtab_exists (s))
1133 uentry l = usymtab_lookup (s);
1136 if (uentry_isForward (l) || (fileloc_isLib (uentry_whereSpecified (l))))
1138 typeIdSet accessType;
1140 if (uentry_isFunction (l))
1142 accessType = typeIdSet_union (uentry_accessType (l),
1143 context_fileAccessTypes ());
1147 accessType = context_fileAccessTypes ();
1152 ue = uentry_makeSpecFunction (s, ct, accessType, globlist, sl, loc);
1157 globSet_free (globlist);
1159 ue = uentry_makeUnspecFunction (s, ct, accessType, loc);
1162 uentry_reflectQualifiers (ue, qtype_getQuals (qt));
1164 usymtab_supEntry (ue);
1169 ** error reported by symtable already
1171 ** llgenerror (message ("Function redeclared: %s (previous declaration: %s)", s,
1172 ** fileloc_unparse (uentry_whereSpecified (l))),
1179 globSet_free (globlist);
1190 le = uentry_makePrivFunction2 (s, ct, acct, globlist, sl, loc);
1194 le = uentry_makeSpecFunction (s, ct, acct, globlist, sl, loc);
1199 le = uentry_makeUnspecFunction (s, ct, acct, loc);
1202 globSet_free (globlist);
1205 if (context_inLCLLib () && !priv)
1207 uentry_setDefined (le, loc);
1210 uentry_reflectQualifiers (le, qtype_getQuals (qt));
1214 case QU_UNKNOWN: break;
1215 case QU_PRINTFLIKE: uentry_setPrintfLike (le); break;
1216 case QU_SCANFLIKE: uentry_setScanfLike (le); break;
1217 case QU_MESSAGELIKE: uentry_setMessageLike (le); break;
1221 usymtab_supEntry (le);
1228 doDeclareFcn (fcnNode f, typeId tn, bool priv, bool spec)
1230 qtype qt = convertLclTypeSpecNode (f->typespec);
1231 ctype ct = convertTypeExpr (qtype_getType (qt), f->declarator->type);
1233 declareFcnAux (f, qt, ct, tn, priv, spec);
1237 ** is s is an argument to f, return its arg no.
1238 ** otherwise, return 0
1242 getParamNo (cstring s, fcnNode f)
1244 /* gasp, maybe should do run-time checks here */
1245 paramNodeList params;
1246 typeExpr fd = f->declarator->type;
1248 /* is this a bug in the LCL grammar? */
1250 while (fd != NULL && (fd->kind == TEXPR_PTR || fd->kind == TEXPR_ARRAY))
1252 if (fd->kind == TEXPR_PTR)
1254 fd = fd->content.pointer;
1258 /*@-null@*/ fd = fd->content.array.elementtype; /*@=null@*/
1261 ** This is a bug in checking, that I should eventually fix.
1262 ** Need some way of deleting the guard from the true branch,
1263 ** but adding it back in the false branch...
1268 llassert (fd != NULL);
1270 if (fd->kind != TEXPR_FCN)
1272 llfatalbug (message ("getParamNo: not a function: %q (%d)",
1273 typeExpr_unparse (fd), (int) fd->kind));
1276 params = fd->content.function.args;
1278 if (paramNodeList_empty (params))
1286 paramNodeList_elements (params, i)
1288 if (i->paramdecl != (typeExpr) 0) /* handle (void) */
1290 if (cstring_equal (s, getVarName (i->paramdecl)))
1296 } end_paramNodeList_elements;
1301 static /*@null@*/ /*@observer@*/ termNode
1302 getLetDecl (cstring s, fcnNode f)
1304 letDeclNodeList x = f->lets;
1306 letDeclNodeList_elements (x, i)
1308 if (cstring_equal (s, ltoken_getRawString (i->varid)))
1310 if (i->sortspec != NULL)
1312 llbuglit ("getLetDecl: cannot return sort!");
1315 { /* is a termNode */
1319 } end_letDeclNodeList_elements;
1321 return (termNode) 0;
1325 ** processTermNode --- based on printTermNode2
1328 static /*@exposed@*/ sRef
1329 processTermNode (/*@null@*/ opFormNode op, termNodeList args,
1330 fcnNode f, uentryList cl)
1332 if (op != (opFormNode) 0)
1337 llcontbuglit ("processTermNode: OPF_IF: not handled");
1340 llcontbuglit ("processTermNode: OPF_ANYOP: not handled");
1344 int size = termNodeList_size (args);
1347 && (cstring_equalLit (ltoken_getRawString (op->content.anyop), "'") ||
1348 cstring_equalLit (ltoken_getRawString (op->content.anyop), "^")))
1350 return (fixTermNode (termNodeList_head (args), f, cl));
1360 int size = termNodeList_size (args);
1363 && (cstring_equalLit (ltoken_getRawString (op->content.anyop), "*")))
1368 ft = fixTermNode (termNodeList_head (args), f, cl);
1369 res = sRef_buildPointer (ft);
1379 llcontbuglit ("OPF_MANYOPM: not handled\n");
1382 llcontbuglit ("OPF_MIDDLE: not handled\n");
1385 llcontbuglit ("OPF_MMIDDLE: not handled\n");
1388 llcontbuglit ("OPF_MIDDLEM: not handled\n");
1391 llcontbuglit ("OPF_MMIDDLEM: not handled\n");
1394 if (op->content.middle == 1)
1395 llbug (message ("array fetch: [%q]",
1396 termNodeList_unparse (args)));
1398 llcontbuglit ("OPF_BMIDDLE: bad\n");
1402 if (op->content.middle <= 1)
1404 sRef arr = fixTermNode (termNodeList_head (args), f, cl);
1407 if (op->content.middle == 1)
1409 termNode t = (termNodeList_reset (args),
1410 termNodeList_advance (args),
1411 termNodeList_current (args));
1413 if (t->kind == TRM_LITERAL)
1418 (cstring_toCharsSafe
1419 (ltoken_getRawString (t->literal)),
1422 ret = sRef_buildArrayFetchKnown (arr, i);
1426 ret = sRef_buildArrayFetch (arr);
1435 ret = sRef_buildArrayFetch (arr);
1441 llcontbug (message ("op->content.middle = %d",
1442 op->content.middle));
1447 llcontbuglit ("OPF_BMIDDLEM not handled");
1451 llcontbuglit ("OPF_BMMIDDLEM not handled");
1455 llcontbug (message ("select: .%s",
1456 ltoken_getRawString (op->content.id)));
1460 llcontbug (message ("map: .%s",
1461 ltoken_getRawString (op->content.id)));
1466 sRef rec = fixTermNode (termNodeList_head (args), f, cl);
1468 ctype ct = ctype_realType (sRef_deriveType (rec, cl));
1469 cstring fieldname = ltoken_getRawString (op->content.id);
1471 ct = ctype_realType (ct);
1474 ** does it correspond to a typedef struct field
1476 ** (kind of kludgey, but there is no direct way to
1477 ** tell if it is an lsl operator instead)
1480 if (ctype_isStructorUnion (ct) &&
1482 (uentryList_lookupField (ctype_getFields (ct), fieldname)))
1484 cstring fname = cstring_copy (fieldname);
1486 ret = sRef_buildField (rec, fname);
1487 cstring_markOwned (fname);
1491 ret = sRef_undefined;
1498 sRef rec = fixTermNode (termNodeList_head (args), f, cl);
1499 sRef ret = sRef_undefined;
1500 ctype ct = ctype_realType (sRef_deriveType (rec, cl));
1501 cstring fieldname = ltoken_getRawString (op->content.id);
1504 ** does it correspond to a typedef struct field
1507 if (ctype_isPointer (ct))
1509 ctype ctb = ctype_realType (ctype_baseArrayPtr (ct));
1511 if (ctype_isStructorUnion (ctb) &&
1512 uentry_isValid (uentryList_lookupField
1513 (ctype_getFields (ctb), fieldname)))
1515 cstring fname = cstring_copy (fieldname);
1517 ret = sRef_buildArrow (rec, fname);
1518 cstring_markOwned (fname);
1527 return sRef_undefined;
1533 ** o replace anything in modifies that is bound with let with value
1534 ** o replace spec variables with internal state
1535 ** o replace paramaters with paramno identifiers
1536 ** o replace globals with their usymid's
1537 ** o make everything sRefs
1540 static /*@exposed@*/ sRef fixTermNode (termNode n, fcnNode f, uentryList cl)
1542 if (n != (termNode) 0)
1552 cstring s = ltoken_getRawString (n->literal);
1553 termNode tl = getLetDecl (s, f);
1555 if (tl != (termNode) 0)
1557 return (fixTermNode (tl, f, cl));
1561 int i = getParamNo (s, f);
1565 usymId usym = usymtab_getId (s);
1567 if (usymId_isInvalid (usym))
1569 if (usymtab_existsEither (s))
1571 return sRef_makeSpecState ();
1575 llcontbuglit ("Invalid symbol in modifies list");
1576 return sRef_undefined;
1580 return (sRef_makeGlobal (usym, ctype_unknown));
1585 sRef p = sRef_makeParam (i, ctype_unknown);
1591 case TRM_APPLICATION:
1593 nameNode nn = n->name;
1595 if (nn != (nameNode) 0)
1599 /* must we handle n->given ? skip for now */
1602 (message ("fixTermNode: expect non-empty nameNode: "
1603 "TRM_APPLICATION: %q",
1604 nameNode_unparse (nn)));
1610 sr = processTermNode (nn->content.opform, n->args, f, cl);
1615 return sRef_undefined;
1617 case TRM_UNCHANGEDALL:
1618 case TRM_UNCHANGEDOTHERS:
1620 case TRM_QUANTIFIER:
1621 return sRef_undefined;
1625 return sRef_undefined;
1629 /*@only@*/ sRefSet fixModifies (fcnNode f, uentryList cl)
1631 static bool shownWarning = FALSE;
1632 modifyNode m = f->modify;
1633 sRefSet sl = sRefSet_new ();
1635 if (m != (modifyNode) 0)
1637 if (m->hasStoreRefList)
1639 storeRefNodeList srefs = m->list;
1641 storeRefNodeList_elements (srefs, i)
1643 if (storeRefNode_isObj (i) || storeRefNode_isType (i))
1647 fileloc loc = fileloc_fromTok (f->name);
1650 ("%q: Warning: object and type modifications "
1651 "not understood by LCLint",
1652 fileloc_unparse (loc)));
1654 shownWarning = TRUE;
1657 else if (storeRefNode_isSpecial (i))
1659 sl = sRefSet_insert (sl, i->content.ref);
1661 else if (storeRefNode_isTerm (i))
1663 sRef s = fixTermNode (i->content.term, f, cl);
1665 if (sRef_isKnown (s))
1667 sl = sRefSet_insert (sl, s);
1674 } end_storeRefNodeList_elements;
1682 static /*@only@*/ cstring
1683 paramNode_name (paramNode x)
1685 return (typeExpr_name (x->paramdecl));
1688 static /*@only@*/ uentry
1689 paramNode_toUentry (paramNode p)
1691 if (p != (paramNode) 0)
1693 if (p->kind == PELIPSIS)
1695 return uentry_makeElipsisMarker ();
1699 qtype ct = convertLclTypeSpecNode (p->type);
1700 ctype cr = convertTypeExpr (qtype_getType (ct), p->paramdecl);
1701 cstring pname = (p->paramdecl == (typeExpr)0) ? cstring_undefined
1702 : paramNode_name (p);
1703 uentry ue = uentry_makeVariableParam (pname, cr);
1705 uentry_reflectQualifiers (ue, qtype_getQuals (ct));
1712 llcontbuglit ("paramNode_toUentry: NULL");
1713 return uentry_undefined;
1719 paramNodeList_toUentryList (paramNodeList p)
1721 uentryList cl = uentryList_new ();
1723 if (paramNodeList_isNull (p)) return (cl);
1725 paramNodeList_elements (p, current)
1727 cl = uentryList_add (cl, paramNode_toUentry (current));
1728 } end_paramNodeList_elements;