2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 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://www.splint.org
28 # include <ctype.h> /* for isdigit */
29 # include "lclintMacros.nf"
31 # include "cgrammar.h"
32 # include "cgrammar_tokens.h"
34 # include "exprChecks.h"
35 # include "transferChecks.h"
36 # include "exprNodeSList.h"
38 static bool exprNode_isEmptyStatement (exprNode p_e);
39 static /*@exposed@*/ exprNode exprNode_firstStatement (/*@returned@*/ exprNode p_e);
40 static bool exprNode_isFalseConstant (exprNode p_e) /*@*/ ;
41 static bool exprNode_isBlock (exprNode p_e);
42 static void checkGlobUse (uentry p_glob, bool p_isCall, /*@notnull@*/ exprNode p_e);
43 static void exprNode_addUse (exprNode p_e, /*@exposed@*/ sRef p_s);
44 static bool exprNode_matchArgType (ctype p_ct, exprNode p_e);
45 static exprNode exprNode_fakeCopy (exprNode p_e) /*@*/ ;
46 static exprNode exprNode_statementError (/*@only@*/ exprNode p_e, /*@only@*/ lltok p_t);
47 static bool exprNode_matchTypes (exprNode p_e1, exprNode p_e2);
48 static void checkUniqueParams (exprNode p_fcn,
49 /*@notnull@*/ exprNode p_current, exprNodeList p_args,
50 int p_paramno, uentry p_ucurrent);
51 static void updateAliases (/*@notnull@*/ exprNode p_e1, /*@notnull@*/ exprNode p_e2);
52 static void abstractOpError (ctype p_tr1, ctype p_tr2, lltok p_op,
53 /*@notnull@*/ exprNode p_e1, /*@notnull@*/ exprNode p_e2,
54 fileloc p_loc1, fileloc p_loc2);
55 static ctype checkNumerics (ctype p_tr1, ctype p_tr2, ctype p_te1, ctype p_te2,
56 /*@notnull@*/ exprNode p_e1, /*@notnull@*/ exprNode p_e2, lltok p_op);
57 static void doAssign (/*@notnull@*/ exprNode p_e1, /*@notnull@*/ exprNode p_e2, bool p_isInit);
58 static void checkSafeUse (exprNode p_e, /*@exposed@*/ sRef p_s);
59 static void reflectNullTest (/*@notnull@*/ exprNode p_e, bool p_isnull);
60 static void checkMacroParen (exprNode p_e);
61 static exprNodeSList exprNode_flatten (/*@dependent@*/ exprNode p_e);
62 static void exprNode_checkSetAny (exprNode p_e, /*@dependent@*/ cstring p_name);
63 static void exprNode_checkUse (exprNode p_e, /*@exposed@*/ sRef p_s, fileloc p_loc);
64 static void exprNode_mergeUSs (exprNode p_res, exprNode p_other);
65 static void exprNode_mergeCondUSs (exprNode p_res, exprNode p_other1, exprNode p_other2);
66 static /*@only@*/ /*@notnull@*/ exprNode exprNode_fromIdentifierAux (/*@observer@*/ uentry p_c);
67 static void checkAnyCall (/*@notnull@*/ /*@dependent@*/ exprNode p_fcn,
68 /*@dependent@*/ cstring p_fname,
69 uentryList p_pn, exprNodeList p_args,
70 bool p_hasMods, sRefSet p_mods, bool p_isSpec,
72 static void checkOneArg (uentry p_ucurrent, /*@notnull@*/ exprNode p_current,
73 /*@dependent@*/ exprNode p_fcn, bool p_isSpec, int p_argno, int p_totargs);
75 checkUnspecCall (/*@notnull@*/ /*@dependent@*/ exprNode p_fcn, uentryList p_params, exprNodeList p_args);
77 static /*@only@*/ exprNode exprNode_effect (exprNode p_e)
78 /*@globals internalState@*/ ;
79 static /*@only@*/ cstring exprNode_doUnparse (exprNode p_e);
80 static /*@observer@*/ cstring exprNode_rootVarName (exprNode p_e);
81 static /*@exposed@*/ exprNode
82 exprNode_lastStatement (/*@returned@*/ exprNode p_e);
84 static /*@null@*/ sRef defref = sRef_undefined;
85 static /*@only@*/ exprNode mustExitNode = exprNode_undefined;
87 static int checkArgsReal (uentry p_fcn, /*@dependent@*/ exprNode p_f,
89 exprNodeList p_args, bool p_isIter, exprNode p_ret);
91 static bool inEffect = FALSE;
92 static int nowalloc = 0;
93 static int totalloc = 0;
94 static int maxalloc = 0;
96 static /*@only@*/ uentry regArg;
97 static /*@only@*/ uentry outArg;
98 static /*@only@*/ uentry outStringArg;
99 static /*@exposed@*/ sRef stdinRef;
100 static /*@exposed@*/ sRef stdoutRef;
101 static /*@only@*/ uentry csArg;
102 static /*@only@*/ uentry csOnlyArg;
103 static ctype cstringType;
104 static ctype ctypeType;
105 static ctype filelocType;
106 static bool initMod = FALSE;
108 /*@function void exprNode_swap (sef exprNode, sef exprNode)@*/
110 # define exprNode_swap(e1,e2) do { exprNode m_tmp = (e1); (e1) = (e2); (e2) = m_tmp; } while (FALSE)
113 static void exprNode_defineConstraints(/*@sef@*/ /*@special@*/ /*@notnull@*/ exprNode e)
114 /*@defines e->requiresConstraints, e->ensuresConstraints,
115 e->trueEnsuresConstraints, e->falseEnsuresConstraints @*/
117 e->requiresConstraints = constraintList_makeNew ();
118 e->ensuresConstraints = constraintList_makeNew ();
119 e->trueEnsuresConstraints = constraintList_makeNew ();
120 e->falseEnsuresConstraints = constraintList_makeNew ();
124 ** must occur after library has been read
127 void exprNode_initMod (void)
128 /*@globals undef regArg, undef outArg, undef outStringArg,
129 undef csOnlyArg, undef csArg;
136 cstringType = ctype_unknown;
137 ctypeType = ctype_unknown;
138 filelocType = ctype_unknown;
140 defref = sRef_undefined;
142 if (usymtab_existsType (cstring_makeLiteralTemp ("cstring")))
144 cstringType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("cstring"));
147 if (usymtab_existsType (cstring_makeLiteralTemp ("ctype")))
149 ctypeType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("ctype"));
152 if (usymtab_existsType (cstring_makeLiteralTemp ("fileloc")))
154 filelocType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("fileloc"));
157 if (usymtab_existsGlob (cstring_makeLiteralTemp ("stdin")))
159 ue = usymtab_lookupGlob (cstring_makeLiteralTemp ("stdin"));
161 else /* define stdin */
163 ue = uentry_makeVariable (cstring_makeLiteralTemp ("stdin"),
165 fileloc_getBuiltin (),
167 uentry_setHasNameError (ue);
168 ue = usymtab_supGlobalEntryReturn (ue);
171 stdinRef = sRef_makePointer (uentry_getSref (ue));
173 if (usymtab_existsGlob (cstring_makeLiteralTemp ("stdout")))
175 ue = usymtab_lookupGlob (cstring_makeLiteralTemp ("stdout"));
179 ue = uentry_makeVariable (cstring_makeLiteralTemp ("stdout"),
181 fileloc_getBuiltin (),
183 uentry_setHasNameError (ue);
184 ue = usymtab_supGlobalEntryReturn (ue);
187 stdoutRef = sRef_makePointer (uentry_getSref (ue));
189 tmp = idDecl_create (cstring_undefined, qtype_create (ctype_unknown));
191 regArg = uentry_makeParam (tmp, PARAMUNKNOWN);
194 qtype_addQual (qtype_create (ctype_makePointer (ctype_unknown)),
197 outArg = uentry_makeParam (tmp, PARAMUNKNOWN);
199 idDecl_setTyp (tmp, qtype_addQual (qtype_create (ctype_string),
202 outStringArg = uentry_makeParam (tmp, PARAMUNKNOWN);
204 idDecl_setTyp (tmp, qtype_addQual (qtype_addQual (qtype_create (cstringType),
206 qual_createNull ()));
208 csOnlyArg = uentry_makeParam (tmp, PARAMUNKNOWN);
210 idDecl_setTyp (tmp, qtype_addQual (qtype_create (cstringType), qual_createNull ()));
211 csArg = uentry_makeParam (tmp, PARAMUNKNOWN);
217 exprNode_destroyMod (void)
218 /*@globals killed regArg, killed outArg, killed outStringArg,
219 killed mustExitNode, initMod @*/
223 uentry_free (regArg);
224 uentry_free (outArg);
225 uentry_free (outStringArg);
227 exprNode_free (mustExitNode);
234 static void exprNode_resetSref (/*@notnull@*/ exprNode e)
239 exprNode exprNode_fakeCopy (exprNode e)
241 /*@-temptrans@*/ /*@-retalias@*/
243 /*@=temptrans@*/ /*@=retalias@*/
246 static bool isFlagKey (char key)
248 return (key == '-' || key == '+' || key == ' ' || key == '#');
251 static void exprNode_combineControl (/*@notnull@*/ exprNode ret,
252 /*@notnull@*/ exprNode ifclause,
253 /*@notnull@*/ exprNode elseclause)
255 ret->canBreak = ifclause->canBreak || elseclause->canBreak;
258 (ifclause->mustBreak || exprNode_mustEscape (ifclause))
259 && (elseclause->mustBreak || exprNode_mustEscape (elseclause));
261 ret->exitCode = exitkind_combine (ifclause->exitCode,
262 elseclause->exitCode);
267 ** For exprNode's returned by exprNode_effect.
270 static bool shallowKind (exprKind kind)
272 return (kind == XPR_STRINGLITERAL
273 || kind == XPR_NUMLIT
276 || kind == XPR_NODE);
280 exprNode_freeIniter (/*@only@*/ exprNode e)
282 if (!exprNode_isError (e))
288 ** Its a fake copy, don't free the field->rec and field->field
293 sfree (e->edata->field);
299 exprNode_free (e->edata->op->b);
300 /*@-compdestroy@*/ sfree (e->edata->op); /*@=compdestroy@*/
304 llbug (message ("other: %s", exprNode_unparse (e)));
307 multiVal_free (e->val);
308 cstring_free (e->etext);
309 fileloc_free (e->loc);
310 sRefSet_free (e->uses);
311 sRefSet_free (e->sets);
312 sRefSet_free (e->msets);
313 guardSet_free (e->guards);
315 constraintList_free(e->requiresConstraints);
316 constraintList_free(e->ensuresConstraints);
317 constraintList_free(e->trueEnsuresConstraints);
318 constraintList_free(e->falseEnsuresConstraints);
320 e->requiresConstraints = NULL;
321 e->ensuresConstraints = NULL;
322 e->trueEnsuresConstraints = NULL;
323 e->falseEnsuresConstraints = NULL;
330 exprNode_freeShallow (/*@only@*/ exprNode e)
332 if (!exprNode_isError (e))
334 if (shallowKind (e->kind))
341 if (e->kind == XPR_EMPTY
342 || e->kind == XPR_BODY
343 || e->kind == XPR_STRINGLITERAL
344 || e->kind == XPR_NUMLIT
345 || e->kind == XPR_NODE
346 || e->kind == XPR_OFFSETOF
347 || e->kind == XPR_ALIGNOFT
348 || e->kind == XPR_ALIGNOF
349 || e->kind == XPR_SIZEOFT
350 || e->kind == XPR_SIZEOF)
352 /* don't free anything */
356 /* multiVal_free (e->val); */
357 cstring_free (e->etext);
358 fileloc_free (e->loc);
359 sRefSet_free (e->uses);
360 sRefSet_free (e->sets);
361 sRefSet_free (e->msets);
362 guardSet_free (e->guards);
363 exprData_freeShallow (e->edata, e->kind);
365 /*@-compdestroy@*/ sfree (e); /*@=compdestroy@*/
374 exprNode_free (exprNode e)
376 if (!exprNode_isError (e))
380 multiVal_free (e->val);
381 cstring_free (e->etext);
382 fileloc_free (e->loc);
383 sRefSet_free (e->uses);
384 sRefSet_free (e->sets);
385 sRefSet_free (e->msets);
386 guardSet_free (e->guards);
387 exprData_free (e->edata, e->kind);
389 constraintList_free(e->requiresConstraints);
390 constraintList_free(e->ensuresConstraints);
391 constraintList_free(e->trueEnsuresConstraints);
392 constraintList_free(e->falseEnsuresConstraints);
394 e->requiresConstraints = NULL;
395 e->ensuresConstraints = NULL;
396 e->trueEnsuresConstraints = NULL;
397 e->falseEnsuresConstraints = NULL;
407 exprNode_makeError ()
409 return exprNode_undefined;
412 static /*@out@*/ /*@only@*/ /*@notnull@*/ exprNode
415 exprNode ret = (exprNode) dmalloc (sizeof (*ret));
416 /* static int lastexpnodes = 0; */
421 if (nowalloc > maxalloc)
429 static /*@notnull@*/ /*@special@*/ exprNode
430 exprNode_createPlain (ctype c)
432 /*@ensures isnull result->edata, result->loc, result->val, result->guards,
433 result->uses, result->sets, result->msets, result->etext @*/
436 exprNode e = exprNode_new ();
440 e->val = multiVal_undefined;
442 e->etext = cstring_undefined;
443 e->loc = fileloc_undefined;
444 e->guards = guardSet_undefined;
445 e->uses = sRefSet_undefined;
446 e->sets = sRefSet_undefined;
447 e->msets = sRefSet_undefined;
448 e->edata = exprData_undefined;
449 e->exitCode = XK_NEVERESCAPE;
451 e->mustBreak = FALSE;
452 e->isJumpPoint = FALSE;
454 exprNode_defineConstraints(e);
459 /*@observer@*/ exprNode exprNode_makeMustExit (void)
461 if (exprNode_isUndefined (mustExitNode))
463 mustExitNode = exprNode_createPlain (ctype_unknown);
464 mustExitNode->exitCode = XK_MUSTEXIT;
471 static /*@notnull@*/ /*@special@*/ exprNode exprNode_create (ctype c)
473 /*@post:isnull result->edata, result->guards, result->val,
474 result->uses, result->sets, result->msets@*/
477 exprNode e = exprNode_createPlain (c);
478 e->loc = fileloc_copy (g_currentloc);
482 static /*@notnull@*/ /*@special@*/ exprNode exprNode_createUnknown (void)
484 /*@post:isnull result->edata, result->guards,
485 result->uses, result->sets, result->msets@*/
488 return (exprNode_create (ctype_unknown));
491 static /*@notnull@*/ /*@special@*/ exprNode
492 exprNode_createLoc (ctype c, /*@keep@*/ fileloc loc)
494 /*@post:isnull result->edata, result->guards, result->val,
495 result->uses, result->sets, result->msets@*/
498 exprNode e = exprNode_createPlain (c);
504 exprNode_copySets (/*@special@*/ /*@notnull@*/ exprNode ret, exprNode e)
505 /*@defines ret->guards, ret->uses, ret->sets, ret->msets@*/
507 if (exprNode_isDefined (e))
509 ret->guards = guardSet_copy (e->guards);
510 ret->uses = sRefSet_newCopy (e->uses);
511 ret->sets = sRefSet_newCopy (e->sets);
512 ret->msets = sRefSet_newCopy (e->msets);
516 ret->guards = guardSet_undefined;
517 ret->uses = sRefSet_undefined;
518 ret->sets = sRefSet_undefined;
519 ret->msets = sRefSet_undefined;
523 static /*@notnull@*/ /*@special@*/ exprNode
524 exprNode_createPartialLocCopy (exprNode e, /*@only@*/ fileloc loc)
526 /*@post:isnull result->edata, result->etext@*/
529 exprNode ret = exprNode_new ();
531 if (exprNode_isError (e))
533 ret->typ = ctype_unknown;
534 ret->val = multiVal_undefined;
536 ret->guards = guardSet_undefined;
537 ret->uses = sRefSet_undefined;
538 ret->sets = sRefSet_undefined;
539 ret->msets = sRefSet_undefined;
544 ret->val = multiVal_copy (e->val);
546 ret->guards = guardSet_copy (e->guards);
547 ret->uses = sRefSet_newCopy (e->uses);
548 ret->sets = sRefSet_newCopy (e->sets);
549 ret->msets = sRefSet_newCopy (e->msets);
552 ret->kind = XPR_EMPTY;
554 ret->etext = cstring_undefined;
555 ret->exitCode = XK_NEVERESCAPE;
556 ret->canBreak = FALSE;
557 ret->mustBreak = FALSE;
558 ret->isJumpPoint = FALSE;
559 ret->edata = exprData_undefined;
561 exprNode_defineConstraints(ret);
567 static /*@notnull@*/ /*@special@*/ exprNode
568 exprNode_createPartialCopy (exprNode e)
570 /*@post:isnull result->edata, result->etext@*/
573 return (exprNode_createPartialLocCopy (e, fileloc_copy (exprNode_loc (e))));
576 static /*@notnull@*/ /*@special@*/ exprNode
577 exprNode_createPartialNVCopy (exprNode e)
579 /*@post:isnull result->edata, result->etext, result->val @*/
582 exprNode ret = exprNode_new ();
584 if (exprNode_isError (e))
586 ret->typ = ctype_unknown;
587 ret->loc = fileloc_undefined;
588 ret->guards = guardSet_undefined;
589 ret->uses = sRefSet_undefined;
590 ret->sets = sRefSet_undefined;
591 ret->msets = sRefSet_undefined;
596 ret->loc = fileloc_copy (e->loc);
597 ret->guards = guardSet_copy (e->guards);
598 ret->uses = sRefSet_newCopy (e->uses);
599 ret->sets = sRefSet_newCopy (e->sets);
600 ret->msets = sRefSet_newCopy (e->msets);
603 ret->val = multiVal_undefined;
604 ret->kind = XPR_EMPTY;
606 ret->etext = cstring_undefined;
607 ret->exitCode = XK_NEVERESCAPE;
608 ret->canBreak = FALSE;
609 ret->mustBreak = FALSE;
610 ret->isJumpPoint = FALSE;
611 ret->edata = exprData_undefined;
613 exprNode_defineConstraints(ret);
618 static /*@notnull@*/ /*@special@*/ exprNode
619 exprNode_createSemiCopy (exprNode e)
621 /*@post:isnull result->edata, result->etext, result->sets,
622 result->msets, result->uses, result->guards@*/
625 if (exprNode_isError (e))
627 return exprNode_createPlain (ctype_unknown);
631 exprNode ret = exprNode_new ();
634 ret->val = multiVal_copy (e->val);
635 ret->loc = fileloc_copy (e->loc);
636 ret->guards = guardSet_undefined;
637 ret->uses = sRefSet_undefined;
638 ret->sets = sRefSet_undefined;
639 ret->msets = sRefSet_undefined;
641 ret->kind = XPR_EMPTY;
643 ret->etext = cstring_undefined;
644 ret->exitCode = XK_NEVERESCAPE;
645 ret->canBreak = FALSE;
646 ret->mustBreak = FALSE;
647 ret->isJumpPoint = FALSE;
648 ret->edata = exprData_undefined;
650 exprNode_defineConstraints(ret);
657 exprNode_isNullValue (exprNode e)
659 if (exprNode_isDefined (e))
661 multiVal m = exprNode_getValue (e);
663 if (multiVal_isInt (m))
665 return (multiVal_forceInt (m) == 0);
673 exprNode_isUnknownConstant (/*@notnull@*/ exprNode e)
675 while (e->kind == XPR_PARENS)
677 e = exprData_getUopNode (e->edata);
678 llassert (exprNode_isDefined (e));
681 if (e->kind == XPR_CONST)
683 multiVal m = exprNode_getValue (e);
685 if (multiVal_isUnknown (m))
695 exprNode_numLiteral (ctype c, /*@temp@*/ cstring t,
696 /*@only@*/ fileloc loc, long val)
698 exprNode e = exprNode_createLoc (c, loc);
700 e->kind = XPR_NUMLIT;
702 llassert (multiVal_isUndefined (e->val));
703 e->val = multiVal_makeInt (val);
704 e->edata = exprData_makeLiteral (cstring_copy (t));
708 e->sref = sRef_makeUnknown ();
709 sRef_setDefNull (e->sref, e->loc);
712 DPRINTF (("Num lit: %s / %s", exprNode_unparse (e), ctype_unparse (exprNode_getType (e))));
717 exprNode_charLiteral (char c, cstring text, /*@only@*/ fileloc loc)
719 exprNode e = exprNode_createLoc (ctype_char, loc);
721 if (context_getFlag (FLG_CHARINTLITERAL))
723 e->typ = ctype_makeConj (ctype_char, ctype_int);
726 e->kind = XPR_NUMLIT;
727 e->val = multiVal_makeChar (c);
729 e->edata = exprData_makeLiteral (cstring_copy (text));
734 exprNode_floatLiteral (double d, ctype ct, cstring text, /*@only@*/ fileloc loc)
736 exprNode e = exprNode_createLoc (ct, loc);
738 e->kind = XPR_NUMLIT;
739 e->val = multiVal_makeDouble (d);
740 e->edata = exprData_makeLiteral (cstring_copy (text));
744 multiVal exprNode_getValue (exprNode e)
746 while (exprNode_isInParens (e)) {
747 if (e->edata != NULL) {
748 e = exprData_getUopNode (e->edata);
754 if (exprNode_isDefined (e)) {
757 return multiVal_undefined;
762 exprNode_combineLiterals (exprNode e, exprNode rest)
766 /* Both must be string literals. */
768 if (exprNode_isUndefined (rest) || exprNode_isUndefined (e))
770 exprNode_free (rest);
774 if (!exprNode_isStringLiteral (e))
778 message ("Constant concatentation is ungrammatical: %s %s", exprNode_unparse (e),
779 exprNode_unparse (rest)),
781 exprNode_free (rest);
785 if (!exprNode_isStringLiteral (rest))
789 message ("Constant concatentation is ungrammatical: %s %s", exprNode_unparse (e), exprNode_unparse (rest)),
792 exprNode_free (rest);
796 ns = cstring_concat (multiVal_forceString (exprNode_getValue (e)),
797 multiVal_forceString (exprNode_getValue (rest)));
799 multiVal_free (e->val);
800 exprData_free (e->edata, e->kind);
801 e->edata = exprData_makeLiteral (cstring_copy (ns));
802 e->val = multiVal_makeString (ns);
803 exprNode_free (rest);
808 exprNode_rawStringLiteral (/*@only@*/ cstring t, /*@only@*/ fileloc loc)
810 exprNode e = exprNode_createLoc (ctype_string, loc);
811 int len = cstring_length (t);
813 if (context_getFlag (FLG_STRINGLITERALLEN))
815 if (len > context_getValue (FLG_STRINGLITERALLEN))
817 voptgenerror (FLG_STRINGLITERALLEN,
819 ("String literal length (%d) exceeds maximum "
820 "length (%d): \"%s\"",
822 context_getValue (FLG_STRINGLITERALLEN),
828 e->kind = XPR_STRINGLITERAL;
829 e->val = multiVal_makeString (cstring_copy (t));
830 e->edata = exprData_makeLiteral (t);
831 e->sref = sRef_makeConst (ctype_string);
833 if (context_getFlag (FLG_READONLYSTRINGS))
835 sRef_setAliasKind (e->sref, AK_STATIC, fileloc_undefined);
836 sRef_setExKind (e->sref, XO_OBSERVER, loc);
840 sRef_setAliasKind (e->sref, AK_ERROR, fileloc_undefined);
843 return (e); /* s released */
847 exprNode_wideStringLiteral (/*@only@*/ cstring t, /*@only@*/ fileloc loc)
849 exprNode res = exprNode_stringLiteral (t, loc);
850 res->typ = ctype_makeWideString ();
856 exprNode_stringLiteral (/*@only@*/ cstring t, /*@only@*/ fileloc loc)
858 int len = cstring_length (t) - 2;
859 char *ts = cstring_toCharsSafe (t);
860 char *s = cstring_toCharsSafe (cstring_create (len + 1));
862 llassert (*ts == '\"' && *(ts + len + 1) == '\"');
863 strncpy (s, ts+1, size_fromInt (len));
866 return exprNode_rawStringLiteral (cstring_fromCharsO (s), loc);
869 exprNode exprNode_fromUIO (cstring c)
871 fileloc loc = context_getSaveLocation ();
872 exprNode e = exprNode_createPlain (ctype_unknown);
876 if (fileloc_isUndefined (loc))
878 loc = fileloc_copy (g_currentloc);
881 e->loc = loc; /* save loc was mangled */
884 if (usymtab_exists (c))
886 uentry ue = usymtab_lookupEither (c);
888 if (uentry_isDatatype (ue)
889 && uentry_isSpecified (ue))
892 (message ("%q: Specified datatype %s used in code, but not defined. "
893 "(Cannot continue reasonably from this error.)",
894 fileloc_unparse (e->loc), c));
902 llassertprint (!usymtab_exists (c), ("Entry exists: %s", c));
905 ** was supercedeGlobalEntry...is this better?
908 if (!context_inIterEnd ())
910 if (context_inMacro ())
912 if (context_getFlag (FLG_UNRECOG))
916 message ("Unrecognized identifier in macro definition: %s", c), e->loc);
920 flagcode_recordSuppressed (FLG_UNRECOG);
926 (FLG_UNRECOG, message ("Unrecognized identifier: %s", c), e->loc);
930 e->edata = exprData_makeId (uentry_makeUnrecognized (c, fileloc_copy (loc)));
932 /* No alias errors for unrecognized identifiers */
933 sRef_setAliasKind (e->sref, AK_ERROR, loc);
938 exprNode exprNode_makeConstantString (cstring c, /*@only@*/ fileloc loc)
940 exprNode e = exprNode_createPlain (ctype_unknown);
943 e->sref = sRef_makeConst (ctype_string);
944 e->edata = exprData_makeId (uentry_makeUnrecognized (c, fileloc_copy (loc)));
945 e->typ = ctype_string;
947 /* No alias errors for unrecognized identifiers */
948 sRef_setAliasKind (e->sref, AK_STATIC, loc);
949 sRef_setExKind (e->sref, XO_OBSERVER, loc);
954 exprNode exprNode_createId (/*@observer@*/ uentry c)
956 if (uentry_isValid (c))
958 exprNode e = exprNode_new ();
960 e->typ = uentry_getType (c);
962 if (uentry_isFunction (c)
963 && !sRef_isLocalVar (uentry_getSref (c)))
965 e->sref = sRef_undefined;
969 e->sref = uentry_getSref (c);
972 if (sRef_isStateUnknown (e->sref) && uentry_isNonLocal (c))
974 sRef_setDefined (e->sref, fileloc_undefined);
978 ** yoikes! leaving this out was a heinous bug...that would have been
979 ** caught if i had lclint working first. gag!
982 e->etext = cstring_undefined;
984 if (uentry_isEitherConstant (c))
987 e->val = multiVal_copy (uentry_getConstantValue (c));
992 e->val = multiVal_unknown ();
995 e->edata = exprData_makeId (c);
996 e->loc = context_getSaveLocation ();
998 if (fileloc_isUndefined (e->loc))
1000 fileloc_free (e->loc);
1001 e->loc = fileloc_copy (g_currentloc);
1004 e->guards = guardSet_new ();
1005 e->sets = sRefSet_new ();
1006 e->msets = sRefSet_new ();
1007 e->uses = sRefSet_new ();
1009 /*> missing fields, detected by lclint <*/
1010 e->exitCode = XK_NEVERESCAPE;
1011 e->isJumpPoint = FALSE;
1012 e->canBreak = FALSE;
1013 e->mustBreak = FALSE;
1015 exprNode_defineConstraints(e);
1021 return exprNode_createUnknown ();
1025 /*@notnull@*/ exprNode
1026 exprNode_fromIdentifier (/*@observer@*/ uentry c)
1030 if (context_justPopped ()) /* watch out! c could be dead */
1032 uentry ce = usymtab_lookupSafe (LastIdentifier ());
1034 if (uentry_isValid (ce))
1040 llbuglit ("Looks like Aunt Millie forgot to walk to dog again.");
1044 ret = exprNode_fromIdentifierAux (c);
1049 static void exprNode_checkStringLiteralLength (ctype t1, exprNode e2)
1051 multiVal mval = exprNode_getValue (e2);
1055 if (ctype_isFixedArray (t1))
1057 int nelements = long_toInt (ctype_getArraySize (t1));
1059 llassert (multiVal_isString (mval));
1060 slit = multiVal_forceString (mval);
1061 len = cstring_length (slit);
1063 if (len == nelements)
1066 (FLG_STRINGLITNOROOM,
1067 message ("String literal with %d character%& "
1068 "is assigned to %s (no room for null terminator): %s",
1069 cstring_length (slit),
1071 exprNode_unparse (e2)),
1074 else if (len > nelements)
1077 (FLG_STRINGLITTOOLONG,
1078 message ("Stirng literal with %d character%& (counting null terminator) "
1079 "is assigned to %s (insufficient storage available): %s",
1080 cstring_length (slit),
1082 exprNode_unparse (e2)),
1085 else if (len < nelements - 1)
1088 (FLG_STRINGLITSMALLER,
1089 message ("String literal with %d character%& is assigned to %s (possible waste of storage): %s",
1090 cstring_length (slit),
1092 exprNode_unparse (e2)),
1102 static /*@only@*/ /*@notnull@*/ exprNode
1103 exprNode_fromIdentifierAux (/*@observer@*/ uentry c)
1105 exprNode e = exprNode_createId (c);
1108 uentry_setUsed (c, e->loc);
1110 if (uentry_isVar (c) && sRef_isFileOrGlobalScope (sr))
1112 checkGlobUse (c, FALSE, e);
1119 exprNode_isZero (exprNode e)
1121 if (exprNode_isDefined (e))
1123 multiVal m = exprNode_getValue (e);
1125 if (multiVal_isInt (m))
1127 return (multiVal_forceInt (m) == 0);
1135 exprNode_isNonNegative (exprNode e)
1137 if (exprNode_isDefined (e))
1139 multiVal m = exprNode_getValue (e);
1141 if (multiVal_isInt (m))
1143 return (multiVal_forceInt (m) >= 0);
1151 ** a[x] - uses a but NOT a[]
1152 ** result sref = a[] (set/use in assignment)
1154 ** The syntax x[a] is also legal in C, and has the same
1155 ** semantics. If ind is an array, and arr is an int, flip
1160 exprNode_arrayFetch (/*@only@*/ exprNode e1, /*@only@*/ exprNode e2)
1163 ** error in arr, error propagates (no new messages)
1164 ** error in ind, assume valid and continue
1167 DPRINTF (("Array fetch: %s / %s",
1168 exprNode_unparse (e1), exprNode_unparse (e2)));
1170 if (exprNode_isError (e1))
1173 return (exprNode_makeError ());
1179 ctype carr = exprNode_getType (e1);
1180 ctype crarr = ctype_realType (carr);
1183 ** this sets up funny aliasing, that leads to spurious
1184 ** lclint errors. Hence, the i2 comments.
1187 /* evans 2001-09-09 added ctype_isKnown so there is no swap when e1 type is unknown */
1188 if (ctype_isKnown (crarr)
1189 && !ctype_isRealArray (crarr)
1190 && ctype_isRealNumeric (crarr)
1191 && !exprNode_isError (e2)
1192 && ctype_isRealAP (exprNode_getType (e2))) /* fetch like 3[a] */
1197 carr = exprNode_getType (arr);
1198 crarr = ctype_realType (carr);
1206 DPRINTF (("arr: %s", exprNode_unparse (arr)));
1208 if (sRef_possiblyNull (arr->sref))
1210 if (!usymtab_isGuarded (arr->sref))
1212 if (optgenerror (FLG_NULLDEREF,
1213 message ("Index of %s pointer %q: %s",
1214 sRef_nullMessage (arr->sref),
1215 sRef_unparse (arr->sref),
1216 exprNode_unparse (arr)),
1219 DPRINTF (("ref: %s", sRef_unparseFull (arr->sref)));
1220 sRef_showNullInfo (arr->sref);
1222 /* suppress future messages */
1223 sRef_setNullError (arr->sref);
1228 if (exprNode_isError (ind))
1230 if ((ctype_isArrayPtr (crarr)
1231 && !ctype_isFunction (crarr))
1232 || ctype_isUnknown (carr))
1234 exprNode ret = exprNode_createPartialCopy (arr);
1236 if (ctype_isKnown (carr))
1238 ret->typ = ctype_baseArrayPtr (crarr);
1242 ret->typ = ctype_unknown;
1245 ret->sref = sRef_makeArrayFetch (arr->sref);
1247 ret->kind = XPR_FETCH;
1250 ** Because of funny aliasing (when arr and ind are
1251 ** flipped) spurious errors would be reported here.
1254 /*@i2@*/ ret->edata = exprData_makePair (arr, ind);
1255 checkSafeUse (ret, arr->sref);
1260 voptgenerror (FLG_TYPE,
1261 message ("Array fetch from non-array (%t): %s[%s]", carr,
1262 exprNode_unparse (e1), exprNode_unparse (e2)),
1264 exprNode_free (arr);
1265 return (exprNode_makeError ());
1270 if (!ctype_isForceRealInt (&(ind->typ)))
1272 ctype rt = ctype_realType (ind->typ);
1274 if (ctype_isChar (rt))
1278 message ("Array fetch using non-integer, %t: %s[%s]",
1280 exprNode_unparse (e1), exprNode_unparse (e2)),
1283 else if (ctype_isEnum (rt))
1287 message ("Array fetch using non-integer, %t: %s[%s]",
1289 exprNode_unparse (e1), exprNode_unparse (e2)),
1296 message ("Array fetch using non-integer, %t: %s[%s]",
1298 exprNode_unparse (e1), exprNode_unparse (e2)),
1302 multiVal_free (ind->val);
1303 ind->val = multiVal_unknown ();
1306 if (ctype_isArrayPtr (crarr) && !ctype_isFunction (crarr))
1308 exprNode ret = exprNode_createSemiCopy (arr);
1309 multiVal m = exprNode_getValue (ind);
1311 ret->typ = ctype_baseArrayPtr (crarr);
1312 ret->kind = XPR_FETCH;
1314 if (multiVal_isInt (m))
1316 int i = (int) multiVal_forceInt (m);
1318 if (sRef_isValid (arr->sref)) {
1319 ret->sref = sRef_makeArrayFetchKnown (arr->sref, i);
1321 ret->sref = sRef_undefined;
1326 ret->sref = sRef_makeArrayFetch (arr->sref);
1329 ret->sets = sRefSet_realNewUnion (arr->sets, ind->sets);
1330 ret->msets = sRefSet_realNewUnion (arr->msets, ind->msets);
1331 ret->uses = sRefSet_realNewUnion (arr->uses, ind->uses);
1333 /* (see comment on spurious errors above) */
1334 /*@i2@*/ ret->edata = exprData_makePair (arr, ind);
1336 exprNode_checkUse (ret, ind->sref, ind->loc);
1337 exprNode_checkUse (ret, arr->sref, arr->loc);
1343 if (ctype_isUnknown (carr))
1345 exprNode ret = exprNode_createPartialCopy (arr);
1347 ret->kind = XPR_FETCH;
1348 ret->typ = ctype_unknown;
1349 ret->sets = sRefSet_union (ret->sets, ind->sets);
1350 ret->msets = sRefSet_union (ret->msets, ind->msets);
1351 ret->uses = sRefSet_union (ret->uses, ind->uses);
1353 /* (see comment on spurious errors above) */
1354 /*@i2@*/ ret->edata = exprData_makePair (arr, ind);
1356 exprNode_checkUse (ret, ind->sref, ind->loc);
1357 exprNode_checkUse (ret, arr->sref, arr->loc);
1364 message ("Array fetch from non-array (%t): %s[%s]", carr,
1365 exprNode_unparse (e1), exprNode_unparse (e2)),
1368 exprNode_free (arr);
1369 exprNode_free (ind);
1371 return (exprNode_makeError ());
1381 checkArgs (uentry fcn, /*@dependent@*/ exprNode f, ctype t,
1382 exprNodeList args, exprNode ret)
1384 return (checkArgsReal (fcn, f, ctype_argsFunction (t), args, FALSE, ret));
1388 ** checkPrintfArgs --- checks arguments for printf-like functions
1389 ** Arguments before ... have already been checked.
1390 ** The argument before the ... is a char *.
1391 ** argno is the format string argument.
1395 checkPrintfArgs (/*@notnull@*/ /*@dependent@*/ exprNode f, uentry fcn,
1396 exprNodeList args, exprNode ret, int argno)
1399 ** the last argument before the elips is the format string
1404 int nargs = exprNodeList_size (args);
1405 uentryList params = uentry_getParams (fcn);
1409 ** These should be ensured by checkSpecialFunction
1412 llassert (uentryList_size (params) == argno + 1);
1413 llassert (uentry_isElipsisMarker (uentryList_getN (params, argno)));
1415 a = exprNodeList_getN (args, argno - 1);
1416 formatloc = fileloc_copy (exprNode_loc (a));
1418 if (exprNode_isDefined (a) && exprNode_isStringLiteral (a)
1419 && exprNode_knownStringValue (a))
1421 char *format = cstring_toCharsSafe (multiVal_forceString (exprNode_getValue (a)));
1422 char *code = format;
1425 nargs = exprNodeList_size (args);
1427 while ((code = strchr (code, '%')) != NULL)
1429 char *origcode = code;
1430 cstring codetext = cstring_newEmpty ();
1431 char key = *(++code);
1432 ctype modtype = ctype_int;
1433 bool modified = FALSE;
1435 fileloc_addColumn (formatloc, code - ocode);
1437 codetext = cstring_appendChar (codetext, key);
1440 while (isFlagKey (key))
1443 codetext = cstring_appendChar (codetext, key);
1444 fileloc_incColumn (formatloc);
1447 if (key == 'm') /* skipped in syslog */
1452 /* ignore field width */
1453 while (isdigit ((int) key) != 0)
1456 codetext = cstring_appendChar (codetext, key);
1457 fileloc_incColumn (formatloc);
1460 /* ignore precision */
1464 codetext = cstring_appendChar (codetext, key);
1465 fileloc_incColumn (formatloc);
1468 ** In printf, '*' means: read the next arg as an int for the
1469 ** field width. This seems to be missing from my copy of the
1470 ** standard x3.159-1989. Setion 4.9.6.1 refers to * (described
1471 ** later) but never does.
1476 ; /* don't do anything --- handle later */
1480 while (isdigit ((int) key) != 0)
1483 codetext = cstring_appendChar (codetext, key);
1484 fileloc_incColumn (formatloc);
1491 modtype = ctype_sint; /* short */
1493 codetext = cstring_appendChar (codetext, key);
1494 fileloc_incColumn (formatloc);
1496 else if (key == 'l' || key == 'L')
1498 modtype = ctype_lint; /* long */
1500 codetext = cstring_appendChar (codetext, key);
1501 fileloc_incColumn (formatloc);
1503 if (key == 'l' || key == 'L') {
1504 modtype = ctype_llint; /* long long */
1506 codetext = cstring_appendChar (codetext, key);
1507 fileloc_incColumn (formatloc);
1515 /* now, key = type of conversion to apply */
1517 fileloc_incColumn (formatloc);
1525 message ("No argument corresponding to %q format "
1526 "code %d (%%%s): \"%s\"",
1527 uentry_getName (fcn),
1529 cstring_fromChars (format)),
1532 if (fileloc_isDefined (formatloc)
1533 && context_getFlag (FLG_SHOWCOL))
1535 llgenindentmsg (cstring_makeLiteral ("Corresponding format code"),
1543 a = exprNodeList_getN (args, i);
1546 if (!exprNode_isError (a))
1552 case '*': /* int argument for fieldwidth */
1553 expecttype = ctype_int;
1554 *(--code) = '%'; /* convert it for next code */
1555 fileloc_subColumn (formatloc, 1);
1556 /*@switchbreak@*/ break;
1559 expecttype = ctype_combine (ctype_uint, modtype);
1560 /*@switchbreak@*/ break;
1562 case 'i': /* int argument */
1564 expecttype = ctype_combine (ctype_int, modtype);
1565 /*@switchbreak@*/ break;
1567 case 'x': /* unsigned int */
1569 expecttype = ctype_combine (ctype_uint, modtype);
1571 /*@switchbreak@*/ break;
1577 case 'f': /* double */
1578 expecttype = ctype_combine (ctype_double, modtype);
1579 /*@switchbreak@*/ break;
1581 case 'c': /* int converted to char (check its a char?) */
1582 expecttype = ctype_makeConj (ctype_int,
1583 ctype_makeConj (ctype_char,
1586 /* evans 2001-10-05 - changed to reflect correct ISO spec:
1587 int converted to char */
1589 /* expecttype = ctype_makeConj (ctype_char, ctype_uchar); */
1590 /*@switchbreak@*/ break;
1592 case 's': /* string */
1593 expecttype = ctype_string;
1594 /*@switchbreak@*/ break;
1597 while (((key = *(++code)) != ']')
1600 codetext = cstring_appendChar (codetext, key);
1601 fileloc_incColumn (formatloc);
1607 (message ("Bad character set format: %s",
1608 cstring_fromChars (origcode)));
1611 expecttype = ctype_string;
1612 /*@switchbreak@*/ break;
1614 case 'p': /* pointer */
1615 expecttype = ctype_makePointer (ctype_void);
1616 uentry_setDefState (regArg, SS_RELDEF); /* need not be defined */
1617 sRef_setPosNull (uentry_getSref (regArg), fileloc_undefined); /* could be null */
1618 /*@switchbreak@*/ break;
1620 case 'n': /* pointer to int, modified by call! */
1621 expecttype = ctype_combine (ctype_makePointer (ctype_int), modtype);
1623 uentry_setDefState (regArg, SS_ALLOCATED); /* corresponds to out */
1624 /*@switchbreak@*/ break;
1626 case 'm': /* in a syslog, it doesn't consume an argument */
1627 /* should check we're really doing syslog */
1629 /*@switchbreak@*/ break;
1633 expecttype = ctype_unknown;
1637 message ("Unrecognized format code: %s",
1638 cstring_fromChars (origcode)),
1639 fileloc_isDefined (formatloc)
1640 ? formatloc : g_currentloc);
1642 /*@switchbreak@*/ break;
1645 if (!(exprNode_matchArgType (expecttype, a)))
1647 if (ctype_isVoidPointer (expecttype)
1648 && ctype_isRealAbstract (a->typ)
1649 && (context_getFlag (FLG_ABSTVOIDP)))
1655 if (llgenformattypeerror
1656 (expecttype, exprNode_undefined,
1658 message ("Format argument %d to %q (%%%s) expects "
1661 uentry_getName (fcn),
1664 a->typ, exprNode_unparse (a)),
1667 if (fileloc_isDefined (formatloc)
1668 && context_getFlag (FLG_SHOWCOL))
1671 (cstring_makeLiteral
1672 ("Corresponding format code"),
1679 uentry_setType (regArg, expecttype);
1680 checkOneArg (regArg, a, f, FALSE, i+1, nargs);
1682 if (ctype_equal (expecttype, ctype_string))
1684 exprNode_checkUse (a, sRef_makePointer (a->sref), a->loc);
1687 uentry_setType (regArg, ctype_unknown);
1688 uentry_fixupSref (regArg);
1692 exprNode_checkCallModifyVal (a->sref, args, f, ret);
1703 cstring_free (codetext);
1708 voptgenerror (FLG_TYPE,
1709 message ("Format string for %q has %d arg%&, given %d",
1710 uentry_getName (fcn), i - argno, nargs - argno),
1716 /* no checking possible for compile-time unknown format strings */
1717 if (exprNode_isDefined (a))
1721 message ("Format string parameter to %s is not a compile-time constant: %s",
1722 exprNode_unparse (f),
1723 exprNode_unparse (a)),
1728 fileloc_free (formatloc);
1732 checkScanfArgs (/*@notnull@*/ /*@dependent@*/ exprNode f, uentry fcn,
1733 exprNodeList args, exprNode ret, int argno)
1737 int nargs = exprNodeList_size (args);
1738 uentryList params = uentry_getParams (fcn);
1742 ** These should be ensured by checkSpecialFunction
1745 llassert (uentryList_size (params) == argno + 1);
1746 llassert (uentry_isElipsisMarker (uentryList_getN (params, argno)));
1748 a = exprNodeList_getN (args, argno - 1);
1749 formatloc = fileloc_copy (exprNode_loc (a));
1751 if (exprNode_isDefined (a) && exprNode_isStringLiteral (a)
1752 && exprNode_knownStringValue (a))
1754 char *format = cstring_toCharsSafe (multiVal_forceString (exprNode_getValue (a)));
1755 char *code = format;
1758 nargs = exprNodeList_size (args);
1760 while ((code = strchr (code, '%')) != NULL)
1762 char *origcode = code;
1763 char key = *(++code);
1764 cstring codetext = cstring_newEmpty ();
1765 ctype modtype = ctype_int;
1766 char modifier = '\0';
1767 bool modified = TRUE;
1768 bool ignore = FALSE;
1770 codetext = cstring_appendChar (codetext, key);
1771 fileloc_addColumn (formatloc, code - ocode);
1774 ** this is based on ANSI standard library description of fscanf
1775 ** (from ANSI standard X3.159-1989, 4.9.6.1)
1778 /* '*' suppresses assignment (does not need match argument) */
1783 codetext = cstring_appendChar (codetext, key);
1786 fileloc_incColumn (formatloc);
1789 /* ignore field width */
1790 while (isdigit ((int) key) != 0)
1793 codetext = cstring_appendChar (codetext, key);
1794 fileloc_incColumn (formatloc);
1799 modtype = ctype_sint; /* short */
1801 codetext = cstring_appendChar (codetext, key);
1802 fileloc_incColumn (formatloc);
1804 else if (key == 'l' || key == 'L')
1806 modtype = ctype_lint; /* long */
1810 codetext = cstring_appendChar (codetext, key);
1812 fileloc_incColumn (formatloc);
1814 if (key == 'l' || key == 'L') {
1815 modtype = ctype_llint; /* long long */
1817 codetext = cstring_appendChar (codetext, key);
1818 fileloc_incColumn (formatloc);
1826 /* now, key = type of conversion to apply */
1828 fileloc_incColumn (formatloc);
1842 message ("No argument corresponding to %q format "
1843 "code %d (%%%s): \"%s\"",
1844 uentry_getName (fcn),
1846 cstring_fromChars (format)),
1849 if (fileloc_isDefined (formatloc)
1850 && context_getFlag (FLG_SHOWCOL))
1853 (cstring_makeLiteral ("Corresponding format code"),
1861 a = exprNodeList_getN (args, i);
1864 if (!exprNode_isError (a))
1870 case '*': /* int argument for fieldwidth */
1871 expecttype = ctype_makePointer (ctype_int);
1872 *(--code) = '%'; /* convert it for next code */
1873 fileloc_subColumn (formatloc, 1);
1874 /*@switchbreak@*/ break;
1877 expecttype = ctype_makePointer (ctype_combine (ctype_uint, modtype));
1878 /*@switchbreak@*/ break;
1883 case 'X': /* unsigned int */
1884 expecttype = ctype_makePointer (ctype_combine (ctype_int, modtype));
1885 /*@switchbreak@*/ break;
1892 /* printf is double, scanf is float! */
1894 if (modifier == 'l')
1896 expecttype = ctype_makePointer (ctype_double);
1898 else if (modifier == 'L')
1900 expecttype = ctype_makePointer (ctype_ldouble);
1904 llassert (modifier == '\0');
1905 expecttype = ctype_makePointer (ctype_float);
1907 /*@switchbreak@*/ break;
1909 case 'c': /* int converted to char (check its a char?) */
1910 expecttype = ctype_makePointer (ctype_makeConj (ctype_char, ctype_uchar));
1911 /*@switchbreak@*/ break;
1913 case 's': /* string */
1914 expecttype = ctype_string;
1915 /*@switchbreak@*/ break;
1919 while (((key = *(++code)) != ']')
1922 codetext = cstring_appendChar (codetext, key);
1923 fileloc_incColumn (formatloc);
1929 (message ("Bad character set format: %s",
1930 cstring_fromChars (origcode)));
1933 expecttype = ctype_string;
1934 /*@switchbreak@*/ break;
1937 case 'p': /* pointer */
1940 message ("Format code should not be used in scanf: %s",
1941 cstring_fromChars (origcode)),
1942 fileloc_isDefined (formatloc)
1943 ? formatloc : g_currentloc);
1945 expecttype = ctype_unknown;
1946 /*@switchbreak@*/ break;
1948 case 'n': /* pointer to int, modified by call! */
1949 expecttype = ctype_makePointer (ctype_int);
1950 /*@switchbreak@*/ break;
1953 expecttype = ctype_unknown;
1957 message ("Unrecognized format code: %s",
1958 cstring_fromChars (origcode)),
1959 fileloc_isDefined (formatloc)
1960 ? formatloc : g_currentloc);
1962 /*@switchbreak@*/ break;
1965 if (!(exprNode_matchArgType (expecttype, a)))
1967 if (ctype_isVoidPointer (expecttype)
1968 && ctype_isRealAbstract (a->typ)
1969 && (context_getFlag (FLG_ABSTVOIDP)))
1975 if (llgenformattypeerror
1976 (expecttype, exprNode_undefined,
1978 message ("Format argument %d to %q (%%%s) expects "
1981 uentry_getName (fcn),
1982 codetext, expecttype,
1983 a->typ, exprNode_unparse (a)),
1986 if (fileloc_isDefined (formatloc)
1987 && context_getFlag (FLG_SHOWCOL))
1990 (cstring_makeLiteral
1991 ("Corresponding format code"),
1998 uentry_setType (outArg, expecttype);
1999 checkOneArg (outArg, a, f, FALSE, i+1, nargs);
2000 uentry_setType (outArg, ctype_unknown);
2001 uentry_fixupSref (outArg);
2005 exprNode_checkCallModifyVal (a->sref, args, f, ret);
2010 /* a->sref = defref; */
2017 cstring_free (codetext);
2022 voptgenerror (FLG_TYPE,
2023 message ("Format string for %q has %d arg%&, given %d",
2024 uentry_getName (fcn), i - argno, nargs - argno),
2030 /* no checking possible for compile-time unknown format strings */
2033 fileloc_free (formatloc);
2037 checkMessageArgs (/*@notnull@*/ /*@dependent@*/ exprNode f,
2040 /*@unused@*/ int argno)
2043 ** the last argument before the elips is the format string
2046 int nargs = exprNodeList_size (args);
2051 a = exprNodeList_getN (args, argno - 1);
2052 formatloc = fileloc_copy (exprNode_loc (a));
2054 if (ctype_isUnknown (cstringType)) {
2055 if (usymtab_existsType (cstring_makeLiteralTemp ("cstring")))
2057 cstringType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("cstring"));
2061 if (ctype_isUnknown (ctypeType)) {
2062 if (usymtab_existsType (cstring_makeLiteralTemp ("ctype")))
2064 ctypeType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("ctype"));
2068 if (ctype_isUnknown (filelocType)) {
2069 if (usymtab_existsType (cstring_makeLiteralTemp ("fileloc")))
2071 filelocType = usymtab_lookupAbstractType (cstring_makeLiteralTemp ("fileloc"));
2075 if (exprNode_isDefined (a) && exprNode_isStringLiteral (a)
2076 && exprNode_knownStringValue (a))
2078 cstring format = multiVal_forceString (exprNode_getValue (a));
2079 char *code = cstring_toCharsSafe (format);
2082 nargs = exprNodeList_size (args);
2084 while ((code = strchr (code, '%')) != NULL)
2086 char *origcode = code;
2087 char key = *(++code);
2088 cstring codetext = cstring_newEmpty ();
2089 bool isOnly = FALSE;
2091 codetext = cstring_appendChar (codetext, key);
2093 fileloc_addColumn (formatloc, code - ocode);
2095 while (key >= '0' && key <= '9')
2098 codetext = cstring_appendChar (codetext, key);
2099 fileloc_incColumn (formatloc);
2103 fileloc_incColumn (formatloc);
2107 if (key == '&') /* plural marker */
2116 message ("Message missing format arg %d (%%%s): \"%s\"",
2117 i + 1, codetext, format),
2123 a = exprNodeList_getN (args, i);
2127 if (!exprNode_isError (a))
2131 /*@-loopswitchbreak@*/
2137 expecttype = ctype_char; break;
2139 expecttype = cstringType; break;
2141 expecttype = cstringType; isOnly = TRUE; break;
2143 expecttype = cstringType; isOnly = TRUE; break;
2144 case 'd': expecttype = ctype_int; break;
2145 case 'u': expecttype = ctype_uint; break;
2146 case 'w': expecttype = ctype_ulint; break;
2147 case 'f': expecttype = ctype_float; break;
2148 case 'b': expecttype = ctype_bool; break;
2149 case 't': expecttype = ctypeType; break;
2150 case 'l': expecttype = filelocType; break;
2151 case '&': /* a wee bit of a hack methinks */
2152 expecttype = ctype_int;
2154 case 'r': expecttype = ctype_bool; break;
2156 expecttype = ctype_unknown;
2159 message ("Unrecognized format code: %s",
2160 cstring_fromChars (origcode)),
2161 fileloc_isDefined (formatloc)
2162 ? formatloc : g_currentloc);
2165 /*@=loopswitchbreak@*/
2167 if (!(exprNode_matchArgType (expecttype, a)))
2169 if (ctype_isVoidPointer (expecttype)
2170 && ctype_isRealAbstract (a->typ)
2171 && (context_getFlag (FLG_ABSTVOIDP)))
2177 if (llgenformattypeerror
2178 (expecttype, exprNode_undefined,
2180 message ("Format argument %d to %q (%%%s) expects "
2183 uentry_getName (fcn),
2184 codetext, expecttype,
2185 a->typ, exprNode_unparse (a)),
2188 if (fileloc_isDefined (formatloc)
2189 && context_getFlag (FLG_SHOWCOL))
2192 (cstring_makeLiteral
2193 ("Corresponding format code"),
2200 if (ctype_equal (expecttype, cstringType))
2204 checkOneArg (csOnlyArg, a, f, FALSE, i+1, nargs);
2205 uentry_fixupSref (csOnlyArg);
2209 checkOneArg (csArg, a, f, FALSE, i+1, nargs);
2210 uentry_fixupSref (csArg);
2215 checkOneArg (regArg, a, f, FALSE, i+1, nargs);
2216 uentry_fixupSref (regArg);
2222 cstring_free (codetext);
2227 voptgenerror (FLG_TYPE,
2228 message ("Format string for %q has %d arg%&, given %d",
2229 uentry_getName (fcn), i - argno, nargs -argno),
2235 /* no checking possible for compile-time unknown format strings */
2238 fileloc_free (formatloc);
2242 checkExpressionDefinedAux (/*@notnull@*/ exprNode e1,
2243 /*@notnull@*/ exprNode e2,
2249 bool hadUncon = FALSE;
2251 if (sRef_isFileOrGlobalScope (sRef_getRootBase (e1->sref)) &&
2252 sRefSet_hasUnconstrained (sets2))
2255 (FLG_EVALORDERUNCON,
2257 ("Expression may have undefined behavior (%q used in right operand "
2258 "may set global variable %q used in left operand): %s %s %s",
2259 sRefSet_unparseUnconstrained (sets2),
2260 sRef_unparse (sRef_getRootBase (e1->sref)),
2261 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2265 if (sRef_isFileOrGlobalScope (sRef_getRootBase (e2->sref)) &&
2266 sRefSet_hasUnconstrained (sets1))
2269 (FLG_EVALORDERUNCON,
2271 ("Expression has undefined behavior (%q used in left operand "
2272 "may set global variable %q used in right operand): %s %s %s",
2273 sRefSet_unparseUnconstrained (sets1),
2274 sRef_unparse (e2->sref),
2275 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2279 sRefSet_realElements (e1->uses, sr)
2281 if (sRef_isMeaningful (sr) && sRefSet_member (sets2, sr))
2286 ("Expression has undefined behavior (left operand uses %q, "
2287 "modified by right operand): %s %s %s",
2289 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2292 } end_sRefSet_realElements;
2294 sRefSet_realElements (sets1, sr)
2296 if (sRef_isMeaningful (sr))
2298 if (sRef_same (sr, e2->sref))
2303 ("Expression has undefined behavior (value of right operand "
2304 "modified by left operand): %s %s %s",
2305 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2308 else if (sRefSet_member (e2->uses, sr))
2313 ("Expression has undefined behavior (left operand modifies %q, "
2314 "used by right operand): %s %s %s",
2316 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
2321 if (sRefSet_member (sets2, sr))
2323 if (sRef_isUnconstrained (sr))
2331 hadUncon = optgenerror
2332 (FLG_EVALORDERUNCON,
2334 ("Expression may have undefined behavior. Left operand "
2335 "calls %q; right operand calls %q. The unconstrained "
2336 "functions may modify global state used by "
2337 "the other operand): %s %s %s",
2338 sRefSet_unparseUnconstrained (sets1),
2339 sRefSet_unparseUnconstrained (sets2),
2340 exprNode_unparse (e1), lltok_unparse (op),
2341 exprNode_unparse (e2)),
2350 ("Expression has undefined behavior (both "
2351 "operands modify %q): %s %s %s",
2353 exprNode_unparse (e1),
2354 lltok_unparse (op), exprNode_unparse (e2)),
2360 } end_sRefSet_realElements;
2363 static void checkExpressionDefined (exprNode e1, exprNode e2, lltok op)
2365 bool hasError = FALSE;
2367 if (exprNode_isError (e1) || exprNode_isError (e2))
2372 if (sRefSet_member (e2->sets, e1->sref))
2374 if (e2->kind == XPR_CALL)
2380 hasError = optgenerror
2382 message ("Expression has undefined behavior "
2383 "(value of left operand %s is modified "
2384 "by right operand %s): %s %s %s",
2385 exprNode_unparse (e1),
2386 exprNode_unparse (e2),
2387 exprNode_unparse (e1), lltok_unparse (op),
2388 exprNode_unparse (e2)),
2393 if (context_getFlag (FLG_EVALORDERUNCON))
2395 if (sRefSet_member (e2->msets, e1->sref))
2397 if (e2->kind == XPR_CALL)
2403 hasError = optgenerror
2406 ("Expression has undefined behavior (value of left "
2407 "operand may be modified by right operand): %s %s %s",
2408 exprNode_unparse (e1), lltok_unparse (op),
2409 exprNode_unparse (e2)),
2417 checkExpressionDefinedAux (e1, e2, e1->sets, e2->sets, op, FLG_EVALORDER);
2419 if (context_maybeSet (FLG_EVALORDERUNCON))
2421 checkExpressionDefinedAux (e1, e2, e1->msets,
2422 e2->msets, op, FLG_EVALORDERUNCON);
2427 static void checkSequencing (exprNode p_f, exprNodeList p_args);
2430 checkArgsReal (uentry fcn, /*@dependent@*/ exprNode f, uentryList cl,
2431 exprNodeList args, bool isIter, exprNode ret)
2435 if (!exprNode_isError (f))
2437 if (!uentryList_isMissingParams (cl))
2439 int nargs = exprNodeList_size (args);
2440 int expectargs = uentryList_size (cl);
2444 if (expectargs == 0)
2452 message ("Iter %q invoked with %d args, "
2454 uentry_getName (fcn),
2462 message ("Function %s called with %d args, "
2464 exprNode_unparse (f), nargs),
2471 last = uentry_getType (uentryList_getN (cl, expectargs - 1));
2473 exprNodeList_reset (args);
2475 uentryList_elements (cl, current)
2477 ctype ct = uentry_getType (current);
2480 if (ctype_isElips (ct))
2483 ** do special checking for printf/scanf library functions
2485 ** this is kludgey code, just for handling the special case
2489 if (uentry_isPrintfLike (fcn))
2491 checkPrintfArgs (f, fcn, args, ret, i);
2494 else if (uentry_isScanfLike (fcn))
2496 checkScanfArgs (f, fcn, args, ret, i);
2499 else if (uentry_isMessageLike (fcn))
2501 checkMessageArgs (f, fcn, args, i);
2506 llassert (!uentry_isSpecialFunction (fcn));
2509 nargs = expectargs; /* avoid errors */
2514 if (i >= nargs) break;
2516 a = exprNodeList_current (args);
2517 exprNodeList_advance (args);
2521 if (exprNode_isError (a))
2528 probably necessary? I'm not sure about this one
2529 checkMacroParen (a);
2532 f->guards = guardSet_union (f->guards, a->guards);
2534 DPRINTF (("match arg: %s / %s", ctype_unparse (ct), ctype_unparse (a->typ)));
2536 if (!(exprNode_matchArgType (ct, a)))
2538 DPRINTF (("Args mismatch!"));
2540 if (ctype_isVoidPointer (ct)
2541 && (ctype_isPointer (a->typ)
2542 && (ctype_isRealAbstract (ctype_baseArrayPtr (a->typ)))))
2547 ("Pointer to abstract type (%t) used "
2549 "(arg %d to %q): %s",
2551 uentry_getName (fcn),
2552 exprNode_unparse (a)),
2560 (ct, exprNode_undefined,
2563 ("Iter %q expects arg %d to "
2564 "be %t gets %t: %s",
2565 uentry_getName (fcn),
2566 i, ct, a->typ, exprNode_unparse (a)),
2577 ("Function %q expects arg %d to be %t gets %t: %s",
2578 uentry_getName (fcn),
2579 i, ct, a->typ, exprNode_unparse (a)),
2582 DPRINTF (("Types: %s / %s",
2584 ctype_unparse (a->typ)));
2588 ** Clear null marker for abstract types.
2589 ** (It is not revealed, so suppress future messages.)
2592 if (ctype_isAbstract (a->typ))
2594 sRef_setNullUnknown (exprNode_getSref (a), a->loc);
2601 } end_uentryList_elements ;
2604 if (expectargs != nargs) /* note: not != since we may have ... */
2606 if (ctype_isElips (last))
2610 message ("Function %s called with %d args, expects at least %d",
2611 exprNode_unparse (f),
2612 nargs, expectargs - 1),
2621 message ("Iter %q invoked with %d args, expects %d",
2622 uentry_getName (fcn), nargs, expectargs),
2629 message ("Function %s called with %d args, expects %d",
2630 exprNode_unparse (f),
2643 ** Check for undefined code sequences in function arguments:
2645 ** one parameter sets something used by another parameter
2646 ** one parameter sets something set by another parameter
2650 checkSequencingOne (exprNode f, exprNodeList args,
2651 /*@notnull@*/ exprNode el, int argno)
2654 ** Do second loop, iff +undefunspec
2658 int numloops = context_maybeSet (FLG_EVALORDERUNCON) ? 2 : 1;
2660 for (checkloop = 0; checkloop < numloops; checkloop++)
2666 thissets = el->sets;
2670 llassert (checkloop == 1);
2671 thissets = el->msets;
2674 sRefSet_realElements (thissets, thisset)
2678 /*@access exprNodeList@*/
2679 for (j = 0; j < args->nelements; j++)
2681 exprNode jl = args->elements[j];
2682 int thisargno = j + 1;
2684 if (thisargno != argno && exprNode_isDefined (jl))
2686 sRefSet otheruses = jl->uses;
2688 if (sRef_isFileOrGlobalScope (sRef_getRootBase (jl->sref)) &&
2689 sRefSet_hasUnconstrained (thissets))
2692 (FLG_EVALORDERUNCON,
2695 ("%q used in argument %d may set "
2696 "global variable %q used by argument %d: %s(%q)",
2697 cstring_capitalizeFree (sRefSet_unparseUnconstrained (thissets)),
2700 sRef_unparse (sRef_getRootBase (jl->sref)),
2702 exprNode_unparse (f), exprNodeList_unparse (args)),
2706 if (sRefSet_member (otheruses, thisset))
2708 if (sRef_isUnconstrained (thisset))
2711 (FLG_EVALORDERUNCON,
2713 ("Unconstrained functions used in arguments %d (%q) "
2714 "and %d (%s) may modify "
2715 "or use global state in undefined way: %s(%q)",
2717 sRefSet_unparseUnconstrainedPlain (otheruses),
2719 sRef_unconstrainedName (thisset),
2720 exprNode_unparse (f),
2721 exprNodeList_unparse (args)),
2729 ("Argument %d modifies %q, used by argument %d "
2730 "(order of evaluation of actual parameters is "
2731 "undefined): %s(%q)",
2732 argno, sRef_unparse (thisset), thisargno,
2733 exprNode_unparse (f), exprNodeList_unparse (args)),
2739 sRefSet othersets = jl->sets;
2741 if (sRefSet_member (othersets, thisset))
2743 if (sRef_isUnconstrained (thisset))
2746 (FLG_EVALORDERUNCON,
2748 ("Unconstrained functions used in "
2749 "arguments %d (%q) and %d (%s) may modify "
2750 "or use global state in undefined way: %s(%q)",
2752 sRefSet_unparseUnconstrainedPlain (othersets),
2754 sRef_unconstrainedName (thisset),
2755 exprNode_unparse (f), exprNodeList_unparse (args)),
2763 ("Argument %d modifies %q, set by argument %d (order of"
2764 " evaluation of actual parameters is undefined): %s(%q)",
2765 argno, sRef_unparse (thisset), thisargno,
2766 exprNode_unparse (f), exprNodeList_unparse (args)),
2773 /*@noaccess exprNodeList@*/
2774 } end_sRefSet_realElements;
2779 checkSequencing (exprNode f, exprNodeList args)
2781 if (exprNodeList_size (args) > 1)
2786 /*@access exprNodeList*/
2788 for (i = 0; i < args->nelements; i++)
2790 el = args->elements[i];
2792 if (!exprNode_isError (el))
2794 checkSequencingOne (f, args, el, i + 1);
2797 /*@noaccess exprNodeList*/
2802 ** requires le = exprNode_getUentry (f)
2806 checkGlobMods (/*@notnull@*/ /*@dependent@*/ exprNode f,
2807 uentry le, exprNodeList args,
2808 /*@notnull@*/ exprNode ret, int specialArgs)
2810 bool isSpec = FALSE;
2811 bool hasMods = FALSE;
2813 globSet usesGlobs = globSet_undefined;
2814 sRefSet mods = sRefSet_undefined;
2815 bool freshMods = FALSE;
2816 uentryList params = uentryList_undefined;
2818 DPRINTF (("Check glob mods: %s", exprNode_unparse (ret)));
2821 ** check globals and modifies
2826 if (!uentry_isValid (le))
2828 ctype fr = ctype_realType (f->typ);
2830 if (ctype_isFunction (fr))
2832 params = ctype_argsFunction (fr);
2836 params = uentryList_missingParams;
2839 if (!context_getFlag (FLG_MODNOMODS)
2840 && !context_getFlag (FLG_GLOBUNSPEC))
2842 checkUnspecCall (f, params, args);
2848 fname = uentry_rawName (le);
2852 if (uentry_isFunction (le))
2854 params = uentry_getParams (le);
2855 mods = uentry_getMods (le);
2856 hasMods = uentry_hasMods (le);
2857 usesGlobs = uentry_getGlobs (le);
2858 isSpec = uentry_isSpecified (le);
2860 else /* not a function */
2862 ctype ct = ctype_realType (uentry_getType (le));
2864 llassertprint (uentry_isVar (le) && ctype_isFunction (ct),
2865 ("checkModGlobs: uentry not a function: %s",
2866 uentry_unparse (le)));
2868 params = ctype_argsFunction (ct);
2869 return; /*@32 ! remove this? */
2878 globSet_allElements (usesGlobs, el)
2880 if (sRef_isValid (el))
2882 if (sRef_isInternalState (el) || sRef_isSystemState (el))
2884 context_usedGlobal (el);
2885 exprNode_checkUse (f, el, f->loc);
2887 if (context_checkInternalUse ())
2889 if (!context_globAccess (el))
2891 if (sRef_isSystemState (el)
2892 && !context_getFlag (FLG_MODFILESYSTEM))
2901 ("Called procedure %s may access %q, but "
2902 "globals list does not include globals %s",
2903 exprNode_unparse (f),
2905 cstring_makeLiteralTemp (sRef_isInternalState (el)
2913 else if (sRef_isNothing (el) || sRef_isSpecState (el))
2919 uentry gle = sRef_getUentry (el);
2920 sRef sr = sRef_updateSref (el);
2922 if (sRef_isUndefGlob (el))
2924 sRef_setDefined (sr, f->loc);
2925 exprNode_checkSet (f, sr);
2933 if (sRef_isAllocated (el))
2935 exprNode_checkSet (f, sr);
2939 if (sRef_isStateUndefined (sr))
2944 ("%s %q used by function undefined before call: %s",
2945 sRef_getScopeName (sr),
2947 exprNode_unparse (f)),
2949 sRef_setDefined (sr, f->loc);
2951 exprNode_checkUse (f, sr, f->loc);
2954 checkGlobUse (gle, TRUE, f);
2957 if (sRef_isKilledGlob (el))
2959 sRef_kill (sr, f->loc);
2960 context_usedGlobal (sr);
2964 } end_globSet_allElements;
2970 if (context_hasMods () || context_getFlag (FLG_MODNOMODS))
2972 sRefSet smods = sRefSet_undefined;
2975 ** NEED to check for modifies anything
2979 ** check each sRef that called function modifies (ml), is
2985 sRefSet_allElements (mods, s) /* s is something which may be modified */
2987 DPRINTF (("Check modify: %s", sRef_unparse (s)));
2989 if (sRef_isKindSpecial (s))
2991 if (sRef_isSpecInternalState (s))
2993 if (context_getFlag (FLG_MODINTERNALSTRICT))
2995 exprNode_checkCallModifyVal (s, args, f, ret);
2999 sRefSet mmods = context_modList ();
3001 sRefSet_allElements (mmods, el)
3003 if (sRef_isInternalState (el))
3005 sRef_setModified (el);
3007 } end_sRefSet_allElements ;
3012 exprNode_checkCallModifyVal (s, args, f, ret);
3017 sRef rb = sRef_getRootBase (s);
3019 if (sRef_isFileOrGlobalScope (rb))
3021 context_usedGlobal (rb);
3024 if (sRef_isFileStatic (s)
3025 && !fileId_equal (fileloc_fileId (f->loc),
3026 fileloc_fileId (uentry_whereDefined (le))))
3028 smods = sRefSet_insert (smods, s);
3032 exprNode_checkCallModifyVal (s, args, f, ret);
3035 } end_sRefSet_allElements;
3040 ** Static elements in modifies set can have nasty consequences.
3041 ** (I think...have not been able to reproduce a possible bug.)
3044 if (!sRefSet_isDefined (smods))
3046 mods = sRefSet_newCopy (mods);
3049 sRefSet_allElements (smods, el)
3051 bool res = sRefSet_delete (mods, el);
3054 } end_sRefSet_allElements;
3056 sRefSet_free (smods);
3061 else if (sRefSet_isDefined (mods))
3062 { /* just check observers */
3065 sRefSet_allElements (mods, s) /* s is something which may be modified */
3067 sRef rb = sRef_getRootBase (s);
3071 if (sRef_isParam (rb))
3073 sRef b = sRef_fixBaseParam (s, args);
3075 if (sRef_isObserver (b))
3077 exprNode e = exprNodeList_nth (args, sRef_getParam (rb));
3081 message ("Function call may modify observer%q: %s",
3082 sRef_unparsePreOpt (b), exprNode_unparse (e)),
3085 sRef_showExpInfo (b);
3089 } end_sRefSet_allElements;
3093 if (!hasMods) /* no specified modifications */
3095 if (context_getFlag (FLG_MODOBSERVERUNCON))
3097 exprNodeList_elements (args, e)
3099 if (exprNode_isDefined (e))
3101 sRef s = exprNode_getSref (e);
3103 if (sRef_isObserver (s)
3104 && ctype_isMutable (sRef_getType (s)))
3107 (FLG_MODOBSERVERUNCON,
3109 ("Call to unconstrained function %s may modify observer%q: %s",
3110 exprNode_unparse (f),
3111 sRef_unparsePreOpt (s), exprNode_unparse (e)),
3114 sRef_showExpInfo (s);
3118 } end_exprNodeList_elements;
3123 checkAnyCall (f, fname, params, args, hasMods, mods, isSpec, specialArgs);
3125 ret->uses = sRefSet_union (ret->uses, f->uses);
3126 ret->sets = sRefSet_union (ret->sets, f->sets);
3127 ret->msets = sRefSet_union (ret->msets, f->msets);
3132 ** Spurious errors reported, because lclint can't tell
3133 ** mods must be fresh if freshMods is true.
3136 /*@i@*/ sRefSet_free (mods);
3142 void checkGlobUse (uentry glob, bool isCall, /*@notnull@*/ exprNode e)
3144 if (uentry_isVar (glob))
3146 if (context_inFunctionLike ())
3148 sRef sr = uentry_getSref (glob);
3150 context_usedGlobal (sr);
3152 if (context_checkGlobUse (glob))
3154 if (!context_globAccess (sr))
3160 message ("Called procedure %s may access %s %q",
3161 exprNode_unparse (e),
3162 sRef_unparseScope (sr),
3163 uentry_getName (glob)),
3170 message ("Undocumented use of %s %s",
3171 sRef_unparseScope (sr),
3172 exprNode_unparse (e)),
3181 llbug (message ("Global not variable: %q", uentry_unparse (glob)));
3186 reflectEnsuresClause (exprNode ret, uentry le, exprNode f, exprNodeList args)
3188 DPRINTF (("Reflect ensures clause: %s(%s) / %s / %s",
3189 exprNode_unparse (f), exprNodeList_unparse (args),
3190 uentry_unparseFull (le),
3191 stateClauseList_unparse (uentry_getStateClauseList (le))));
3193 if (uentry_isValid (le) && uentry_isFunction (le))
3195 stateClauseList sclauses = uentry_getStateClauseList (le);
3197 if (stateClauseList_isDefined (sclauses))
3199 DPRINTF (("Reflect ensures: %s / %s / %s",
3200 uentry_unparse (le),
3201 exprNode_unparse (f), exprNodeList_unparse (args)));
3203 stateClauseList_elements (sclauses, cl)
3205 if (stateClause_hasEnsures (cl))
3207 /* Same in usymtab.c:1904 */
3208 if (stateClause_setsMetaState (cl))
3210 qual q = stateClause_getMetaQual (cl);
3211 annotationInfo ainfo = qual_getAnnotationInfo (q);
3212 metaStateInfo minfo = annotationInfo_getState (ainfo);
3213 cstring key = metaStateInfo_getName (minfo);
3214 int mvalue = annotationInfo_getValue (ainfo);
3216 sRefSet osrs = sRefSet_undefined;
3219 if (stateClause_isGlobal (cl))
3221 srs = sRefSet_single (usymtab_lookupGlobalMarker ());
3226 srs = stateClause_getRefs (cl);
3229 DPRINTF (("Reflect ensures clause: %s", stateClause_unparse (cl)));
3232 DPRINTF (("Sets meta state! %s", stateClause_unparse (cl)));
3234 sRefSet_elements (srs, sel)
3238 if (sRef_isResult (sRef_getRootBase (sel)))
3240 s = exprNode_getSref (ret);
3244 s = sRef_fixBaseParam (sel, args);
3247 DPRINTF (("Reflecting state clause on: %s / %s",
3248 sRef_unparse (sel), sRef_unparse (s)));
3250 sRef_setMetaStateValueComplete (s, key, mvalue, exprNode_loc (f));
3251 } end_sRefSet_elements;
3253 sRefSet_free (osrs);
3257 sRefSet srs = stateClause_getRefs (cl);
3258 sRefModVal modf = stateClause_getEnsuresFunction (cl);
3259 int eparam = stateClause_getStateParameter (cl);
3261 DPRINTF (("Reflect after clause: %s / %s",
3262 stateClause_unparse (cl),
3263 sRefSet_unparse (srs)));
3265 sRefSet_elements (srs, sel)
3269 DPRINTF (("elements: %s", sRef_unparse (sel)));
3270 DPRINTF (("elements: %s", sRef_unparseFull (sel)));
3272 if (sRef_isResult (sRef_getRootBase (sel)))
3274 DPRINTF (("Fix base: %s / %s",
3275 sRef_unparse (sel), sRef_unparse (exprNode_getSref (ret))));
3276 s = sRef_fixBase (sel, exprNode_getSref (ret));
3277 DPRINTF (("==> %s", sRef_unparseFull (s)));
3281 s = sRef_fixBaseParam (sel, args);
3284 DPRINTF (("elements: %s", sRef_unparse (s)));
3285 DPRINTF (("elements: %s", sRef_unparseFull (s)));
3287 DPRINTF (("Reflecting state clause on: %s / %s",
3288 sRef_unparseFull (sel), sRef_unparseFull (s)));
3290 /* evans 2001-08-24 - added aliasSetCompleteParam */
3291 sRef_aliasSetCompleteParam (modf, s, eparam, exprNode_loc (f));
3293 DPRINTF (("After reflecting state clause on: %s / %s",
3294 sRef_unparseFull (sel), sRef_unparseFull (s)));
3295 } end_sRefSet_elements;
3298 } end_stateClauseList_elements ;
3301 DPRINTF (("Here: %s / %s",
3302 uentry_unparseFull (le),
3303 bool_unparse (uentry_hasMetaStateEnsures (le))));
3305 if (uentry_hasMetaStateEnsures (le))
3307 fileloc loc = exprNode_loc (f);
3309 metaStateConstraintList mscl = uentry_getMetaStateEnsures (le);
3311 metaStateConstraintList_elements (mscl, msc)
3313 metaStateSpecifier msspec = metaStateConstraint_getSpecifier (msc);
3314 metaStateInfo msinfo = metaStateSpecifier_getMetaStateInfo (msspec);
3315 metaStateExpression msexpr = metaStateConstraint_getExpression (msc);
3316 cstring key = metaStateInfo_getName (msinfo);
3317 sRef mlsr = metaStateSpecifier_getSref (msspec);
3319 sRef lastref = sRef_undefined;
3320 stateValue sval = stateValue_undefined;
3322 DPRINTF (("Meta state constraint for %s: %s", uentry_unparse (le),
3323 metaStateConstraint_unparse (msc)));
3324 DPRINTF (("Matches left: %s", sRef_unparseDebug (mlsr)));
3326 if (sRef_isResult (sRef_getRootBase (mlsr)))
3328 s = exprNode_getSref (ret);
3332 s = sRef_fixBaseParam (mlsr, args);
3335 DPRINTF (("Setting state: %s", sRef_unparseFull (s)));
3337 while (metaStateExpression_isDefined (msexpr))
3339 metaStateSpecifier ms = metaStateExpression_getSpecifier (msexpr);
3340 metaStateInfo msi = metaStateSpecifier_getMetaStateInfo (ms);
3343 DPRINTF (("Check expression: %s", metaStateExpression_unparse (msexpr)));
3345 if (metaStateExpression_isMerge (msexpr))
3347 msexpr = metaStateExpression_getRest (msexpr);
3351 msexpr = metaStateExpression_undefined;
3354 if (metaStateInfo_isDefined (msi))
3356 /* Must match lhs state */
3357 llassert (metaStateInfo_equal (msinfo, msi));
3360 if (metaStateSpecifier_isElipsis (ms))
3363 ** For elipsis, we need to merge all the relevant elipsis parameters
3367 uentryList params = uentry_getParams (le);
3368 int paramno = uentryList_size (params) - 1;
3370 if (!uentry_isElipsisMarker (uentryList_getN (params, paramno)))
3374 message ("Ensures clauses uses ... for function without ... in parameter list: %q",
3375 uentry_getName (le)),
3376 uentry_whereLast (le));
3377 /*@innerbreak@*/ break;
3380 while (paramno < exprNodeList_size (args))
3382 exprNode arg = exprNodeList_getN (args, paramno);
3383 fs = exprNode_getSref (arg);
3384 DPRINTF (("Merge arg: %s", exprNode_unparse (arg)));
3386 /* cut and pasted... gack*/
3387 if (stateValue_isDefined (sval))
3389 /* Use combination table to merge old state value with new one: */
3390 stateValue tval = sRef_getMetaStateValue (fs, key);
3392 if (stateValue_isDefined (tval))
3394 stateCombinationTable sctable = metaStateInfo_getMergeTable (msinfo);
3395 cstring msg = cstring_undefined;
3396 int nval = stateCombinationTable_lookup (sctable,
3397 stateValue_getValue (sval),
3398 stateValue_getValue (tval),
3400 DPRINTF (("Combining: %s + %s -> %d",
3401 stateValue_unparseValue (sval, msinfo),
3402 stateValue_unparseValue (tval, msinfo),
3405 if (nval == stateValue_error)
3410 ("Attributes merged in ensures clause in states that "
3411 "cannot be combined (%q is %q, %q is %q)%q",
3412 sRef_unparse (lastref),
3413 stateValue_unparseValue (sval, msinfo),
3415 stateValue_unparseValue (tval, msinfo),
3416 cstring_isDefined (msg) ?
3417 message (": %s", msg) : cstring_undefined),
3420 sRef_showMetaStateInfo (fs, key);
3424 stateValue_updateValueLoc (sval, nval, fileloc_undefined);
3425 loc = exprNode_loc (arg);
3429 DPRINTF (("No value for: %s:%s", sRef_unparse (fs), key));
3434 sval = sRef_getMetaStateValue (fs, key);
3439 if (stateValue_isError (sval))
3441 /*@innerbreak@*/ break; /* Don't merge any more values if here was an error */
3450 msr = metaStateSpecifier_getSref (ms);
3453 llassert (sRef_isParam (sRef_getRootBase (msr)));
3454 fs = sRef_fixBaseParam (msr, args);
3456 if (stateValue_isDefined (sval))
3458 /* Use combination table to merge old state value with new one: */
3459 stateValue tval = sRef_getMetaStateValue (fs, key);
3461 if (stateValue_isDefined (tval))
3463 stateCombinationTable sctable = metaStateInfo_getMergeTable (msinfo);
3464 cstring msg = cstring_undefined;
3465 int nval = stateCombinationTable_lookup (sctable,
3466 stateValue_getValue (sval),
3467 stateValue_getValue (tval),
3469 DPRINTF (("Combining: %s + %s -> %d",
3470 stateValue_unparseValue (sval, msinfo),
3471 stateValue_unparseValue (tval, msinfo),
3474 if (nval == stateValue_error)
3479 ("Attributes merged in ensures clause in states that "
3480 "cannot be combined (%q is %q, %q is %q)%q",
3481 sRef_unparse (lastref),
3482 stateValue_unparseValue (sval, msinfo),
3484 stateValue_unparseValue (tval, msinfo),
3485 cstring_isDefined (msg)
3486 ? message (": %s", msg) : cstring_undefined),
3489 sRef_showMetaStateInfo (fs, key);
3493 stateValue_updateValueLoc (sval, nval, fileloc_undefined);
3497 DPRINTF (("No value for: %s:%s", sRef_unparse (fs), key));
3502 sval = sRef_getMetaStateValue (fs, key);
3507 if (stateValue_isError (sval))
3509 /*@innerbreak@*/ break; /* Don't merge any more values if here was an error */
3514 DPRINTF (("Setting: %s:%s <- %s", sRef_unparse (s), key, stateValue_unparse (sval)));
3516 if (stateValue_isDefined (sval))
3518 sRef_setMetaStateValueComplete (s, key, stateValue_getValue (sval), loc);
3522 DPRINTF (("Undefined state: %s", cstring_toCharsSafe (sRef_unparse (s))));
3524 } end_metaStateConstraintList_elements ;
3526 metaStateConstraintList_free (mscl);
3532 checkRequiresClause (uentry le, exprNode f, exprNodeList args)
3534 DPRINTF (("Check requires clause: %s(%s) / %s / %s",
3535 exprNode_unparse (f), exprNodeList_unparse (args),
3536 uentry_unparseFull (le),
3537 stateClauseList_unparse (uentry_getStateClauseList (le))));
3539 if (uentry_isValid (le) && uentry_isFunction (le))
3541 stateClauseList sclauses = uentry_getStateClauseList (le);
3543 if (stateClauseList_isDefined (sclauses))
3545 DPRINTF (("Check requires: %s / %s / %s",
3546 uentry_unparse (le),
3547 exprNode_unparse (f), exprNodeList_unparse (args)));
3549 stateClauseList_elements (sclauses, cl)
3551 DPRINTF (("Check clause: %s / %s",
3552 stateClause_unparse (cl),
3553 bool_unparse (stateClause_hasRequires (cl))));
3555 if (stateClause_hasRequires (cl))
3557 sRefSet osrs = sRefSet_undefined;
3560 if (stateClause_isGlobal (cl))
3562 srs = sRefSet_single (usymtab_lookupGlobalMarker ());
3567 srs = stateClause_getRefs (cl);
3570 DPRINTF (("Refs: %s", sRefSet_unparse (srs)));
3572 if (stateClause_setsMetaState (cl))
3574 qual q = stateClause_getMetaQual (cl);
3575 annotationInfo ainfo = qual_getAnnotationInfo (q);
3576 metaStateInfo minfo = annotationInfo_getState (ainfo);
3577 cstring key = metaStateInfo_getName (minfo);
3578 int mvalue = annotationInfo_getValue (ainfo);
3580 DPRINTF (("Requires meta state! %s = %d", key, mvalue));
3582 sRefSet_elements (srs, sel)
3584 sRef s = sRef_fixBaseParam (sel, args);
3586 if (sRef_isResult (sRef_getRootBase (sel)))
3592 DPRINTF (("Checking state clause on: %s / %s / %s = %d",
3593 sRef_unparseFull (sel), sRef_unparseFull (s),
3596 if (!sRef_checkMetaStateValue (s, key, mvalue))
3598 DPRINTF (("HERE: %s", sRef_unparse (s)));
3602 ("Requires clause of called function %q not satisfied%q (state is %q): %q",
3603 uentry_getName (le),
3604 sRef_isGlobalMarker (s)
3606 : message (" by %q", sRef_unparse (s)),
3607 stateValue_unparseValue (sRef_getMetaStateValue (s, key),
3609 stateClause_unparse (cl)),
3612 sRef_showAliasInfo (s);
3616 DPRINTF (("Error supressed!"));
3617 DPRINTF (("Loc: %s", fileloc_unparse (exprNode_loc (f))));
3618 DPRINTF (("Context supress: %s",
3619 bool_unparse (context_suppressFlagMsg (FLG_STATETRANSFER, exprNode_loc (f)))));
3623 } end_sRefSet_elements;
3627 sRefModVal modf = stateClause_getRequiresBodyFunction (cl);
3628 int eparam = stateClause_getStateParameter (cl);
3630 DPRINTF (("Reflect after clause: %s / %s",
3631 stateClause_unparse (cl),
3632 sRefSet_unparse (srs)));
3634 sRefSet_elements (srs, sel)
3638 DPRINTF (("elements: %s", sRef_unparse (sel)));
3639 DPRINTF (("elements: %s", sRef_unparseFull (sel)));
3641 s = sRef_fixBaseParam (sel, args);
3643 DPRINTF (("elements: %s", sRef_unparse (s)));
3644 DPRINTF (("elements: %s", sRef_unparseFull (s)));
3646 if (sRef_isResult (sRef_getRootBase (sel)))
3648 ; /*@i423 what do we do about results */
3652 DPRINTF (("Reflecting state clause on: %s / %s",
3653 sRef_unparse (sel), sRef_unparse (s)));
3655 modf (s, eparam, exprNode_loc (f));
3657 } end_sRefSet_elements;
3660 sRefSet_free (osrs);
3662 } end_stateClauseList_elements ;
3667 static /*@only@*/ exprNode
3668 functionCallSafe (/*@only@*/ /*@notnull@*/ exprNode f,
3669 ctype t, /*@keep@*/ exprNodeList args)
3671 /* requires f is a non-error exprNode, with type function */
3672 cstring fname = exprNode_unparse (f);
3673 uentry le = exprNode_getUentry (f);
3674 exprNode ret = exprNode_createPartialCopy (f);
3679 DPRINTF (("Call: %s %s",exprNode_unparse (f), exprNodeList_unparse (args)));
3681 ret->typ = ctype_getReturnType (t);
3682 ret->kind = XPR_CALL;
3684 ret->edata = exprData_makeCall (f, args);
3687 ** Order of these steps is very important!
3689 ** Must check for argument dependencies before messing up uses and sets.
3692 if (context_getFlag (FLG_EVALORDER))
3694 exprNodeList_elements (args, current)
3696 if (exprNode_isDefined (current))
3698 exprNode_addUse (current, current->sref);
3700 } end_exprNodeList_elements;
3702 if (context_maybeSet (FLG_EVALORDER) || context_maybeSet (FLG_EVALORDERUNCON))
3704 checkSequencing (f, args);
3707 exprNodeList_elements (args, current)
3709 if (exprNode_isDefined (current) && sRef_isMeaningful (current->sref))
3711 exprNode_addUse (ret, sRef_makeDerived (current->sref));
3713 } end_exprNodeList_elements ;
3716 special = checkArgs (le, f, t, args, ret);
3717 checkGlobMods (f, le, args, ret, special);
3718 checkRequiresClause (le, f, args);
3721 if (uentry_isValid (le)
3722 && (uentry_isFunction (le)
3723 || (uentry_isVariable (le)
3724 && ctype_isFunction (uentry_getType (le)))))
3726 exitkind exk = uentry_getExitCode (le);
3728 /* f->typ is already set to the return type */
3730 DPRINTF (("Function: %s", uentry_unparseFull (le)));
3731 ret->sref = uentry_returnedRef (le, args);
3732 DPRINTF (("Returned: %s / %s",
3733 uentry_unparseFull (le),
3734 sRef_unparseFull (ret->sref)));
3736 if (uentry_isFunction (le) && exprNodeList_size (args) >= 1)
3738 qual nullPred = uentry_nullPred (le);
3740 if (qual_isTrueNull (nullPred))
3742 exprNode arg = exprNodeList_head (args);
3744 if (exprNode_isDefined (arg))
3746 ret->guards = guardSet_addFalseGuard (ret->guards, arg->sref);
3749 else if (qual_isFalseNull (nullPred))
3751 exprNode arg = exprNodeList_head (args);
3753 if (exprNode_isDefined (arg))
3755 ret->guards = guardSet_addTrueGuard (ret->guards, arg->sref);
3760 llassert (qual_isUnknown (nullPred));
3764 if (exitkind_isConditionalExit (exk))
3768 ** if (arg0) then { exit! } else { ; }
3770 ** if (arg0) then { ; } else { exit! }
3775 llassert (!exprNodeList_isEmpty (args));
3776 firstArg = exprNodeList_head (args);
3778 if (exprNode_isDefined (firstArg)
3779 && !guardSet_isEmpty (firstArg->guards))
3781 usymtab_trueBranch (guardSet_undefined);
3782 usymtab_altBranch (guardSet_undefined);
3784 if (exitkind_isTrueExit (exk))
3786 usymtab_popBranches (firstArg,
3787 exprNode_makeMustExit (),
3789 TRUE, TRUEEXITCLAUSE);
3793 usymtab_popBranches (firstArg,
3795 exprNode_makeMustExit (),
3796 TRUE, FALSEEXITCLAUSE);
3800 ret->exitCode = XK_MAYEXIT;
3802 else if (exitkind_mustExit (exk))
3804 ret->exitCode = XK_MUSTEXIT;
3806 else if (exitkind_couldExit (exk))
3808 ret->exitCode = XK_MAYEXIT;
3815 if (cstring_equalLit (fname, "exit"))
3817 if (exprNodeList_size (args) == 1)
3819 exprNode arg = exprNodeList_head (args);
3821 if (exprNode_isDefined (arg) && exprNode_knownIntValue (arg))
3823 long int val = multiVal_forceInt (exprNode_getValue (arg));
3830 ("Argument to exit has implementation defined behavior: %s",
3831 exprNode_unparse (arg)),
3832 exprNode_loc (arg));
3841 exprNode_checkSetAny (ret, uentry_rawName (le));
3844 DPRINTF (("Before reflect: %s", sRef_unparseFull (ret->sref)));
3845 DPRINTF (("Reflect: %s", uentry_unparseFull (le)));
3846 reflectEnsuresClause (ret, le, f, args);
3853 ** this is yucky! should keep the uentry as part of exprNode!
3856 uentry exprNode_getUentry (exprNode e)
3858 if (exprNode_isError (e))
3860 return uentry_undefined;
3864 cstring s = exprNode_rootVarName (e);
3865 uentry ue = usymtab_lookupSafe (s);
3872 exprNode_makeInitBlock (lltok brace, /*@only@*/ exprNodeList inits)
3874 exprNode ret = exprNode_createPlain (ctype_unknown);
3876 ret->kind = XPR_INITBLOCK;
3877 ret->edata = exprData_makeCall (exprNode_undefined, inits);
3878 ret->loc = fileloc_update (ret->loc, lltok_getLoc (brace));
3884 exprNode_functionCall (/*@only@*/ exprNode f, /*@only@*/ exprNodeList args)
3890 if (exprNode_isUndefined (f))
3893 exprNodeList_free (args);
3894 return exprNode_undefined;
3897 t = exprNode_getType (f);
3899 if (sRef_isLocalVar (f->sref))
3901 exprNode_checkUse (f, f->sref, f->loc);
3903 if (sRef_possiblyNull (f->sref))
3905 if (!usymtab_isGuarded (f->sref))
3907 if (optgenerror (FLG_NULLDEREF,
3908 message ("Function call using %s pointer %q",
3909 sRef_nullMessage (f->sref),
3910 sRef_unparse (f->sref)),
3913 sRef_showNullInfo (f->sref);
3914 sRef_setNullError (f->sref);
3922 if (ctype_isRealFunction (t))
3924 exprNode ret = functionCallSafe (f, t, args);
3928 else if (ctype_isUnknown (t))
3930 exprNode ret = exprNode_createPartialCopy (f);
3936 exprNodeList_elements (args, current)
3938 if (exprNode_isDefined (current))
3940 exprNode_checkUse (ret, current->sref, ret->loc);
3943 ** also, anything derivable from current->sref may be used
3946 exprNode_addUse (ret, sRef_makeDerived (current->sref));
3947 exprNode_mergeUSs (ret, current);
3949 } end_exprNodeList_elements;
3951 ret->edata = exprData_makeCall (f, args);
3952 ret->kind = XPR_CALL;
3954 tstring = cstring_copy (exprNode_unparse (f));
3956 cstring_markOwned (tstring);
3957 exprNode_checkSetAny (ret, tstring);
3963 voptgenerror (FLG_TYPE,
3964 message ("Call to non-function (type %t): %s", t,
3965 exprNode_unparse (f)),
3968 exprNodeList_free (args);
3970 return (exprNode_makeError ());
3975 exprNode_fieldAccessAux (/*@only@*/ exprNode s, /*@observer@*/ fileloc loc,
3976 /*@only@*/ cstring f)
3978 exprNode ret = exprNode_createPartialCopy (s);
3980 ret->kind = XPR_FACCESS;
3982 if (exprNode_isError (s))
3984 ret->edata = exprData_makeField (s, f);
3989 ctype t = exprNode_getType (s);
3990 ctype tr = ctype_realType (t);
3992 checkMacroParen (s);
3994 ret->edata = exprData_makeField (s, f);
3996 if (ctype_isStructorUnion (tr))
3998 uentry tf = uentryList_lookupField (ctype_getFields (tr), f);
4000 if (uentry_isUndefined (tf))
4002 voptgenerror (FLG_TYPE,
4003 message ("Access non-existent field %s of %t: %s", f, t,
4004 exprNode_unparse (ret)),
4006 /*! cstring_free (f); */ /* evans 2001-03-25 self-detect */
4011 uentry_setUsed (tf, exprNode_loc (ret));
4013 ret->typ = uentry_getType (tf);
4014 checkSafeUse (ret, s->sref);
4016 ret->sref = sRef_makeField (s->sref, uentry_rawName (tf));
4017 /*!? exprNode_free (s); */ /* evans 2001-03-25 self-detect */
4021 else /* isStructorUnion */
4023 if (ctype_isRealAbstract (tr))
4027 message ("Access field of abstract type (%t): %s.%s",
4028 t, exprNode_unparse (s), f),
4030 ret->typ = ctype_unknown;
4034 if (ctype_isKnown (tr))
4039 ("Access field of non-struct or union (%t): %s.%s",
4040 t, exprNode_unparse (s), f),
4043 ret->typ = ctype_unknown;
4047 cstring sn = cstring_copy (f);
4049 checkSafeUse (ret, s->sref);
4050 cstring_markOwned (sn);
4051 ret->sref = sRef_makeField (s->sref, sn);
4063 exprNode_fieldAccess (/*@only@*/ exprNode s, /*@only@*/ lltok dot,
4064 /*@only@*/ cstring f)
4066 exprNode res = exprNode_fieldAccessAux (s, lltok_getLoc (dot), f);
4067 lltok_release (dot);
4072 exprNode_addParens (/*@only@*/ lltok lpar, /*@only@*/ exprNode e)
4074 exprNode ret = exprNode_createPartialCopy (e);
4076 ret->loc = fileloc_update (ret->loc, lltok_getLoc (lpar));
4077 ret->kind = XPR_PARENS;
4078 ret->edata = exprData_makeUop (e, lpar);
4080 if (!exprNode_isError (e))
4082 ret->exitCode = e->exitCode;
4083 ret->canBreak = e->canBreak;
4084 ret->mustBreak = e->mustBreak;
4085 ret->isJumpPoint = e->isJumpPoint;
4086 ret->sref = e->sref;
4093 exprNode_arrowAccessAux (/*@only@*/ exprNode s, /*@observer@*/ fileloc loc,
4094 /*@only@*/ cstring f)
4096 exprNode ret = exprNode_createPartialCopy (s);
4098 ret->edata = exprData_makeField (s, f);
4099 ret->kind = XPR_ARROW;
4101 if (exprNode_isError (s))
4107 ctype t = exprNode_getType (s);
4108 ctype tr = ctype_realType (t);
4110 checkMacroParen (s);
4112 (void) ctype_fixArrayPtr (tr); /* REWRITE THIS */
4114 if (ctype_isRealPointer (tr))
4116 ctype b = ctype_realType (ctype_baseArrayPtr (tr));
4118 if (ctype_isStructorUnion (b))
4120 uentry fentry = uentryList_lookupField (ctype_getFields (b), f);
4122 if (sRef_isKnown (s->sref) && sRef_possiblyNull (s->sref))
4124 if (!usymtab_isGuarded (s->sref) && !context_inProtectVars ())
4128 message ("Arrow access from %s pointer%q: %s",
4129 sRef_nullMessage (s->sref),
4130 sRef_unparsePreOpt (s->sref),
4131 exprNode_unparse (ret)),
4134 sRef_showNullInfo (s->sref);
4135 sRef_setNullError (s->sref);
4140 if (uentry_isUndefined (fentry))
4144 message ("Access non-existent field %s of %t: %s",
4145 f, t, exprNode_unparse (ret)),
4147 ret->typ = ctype_unknown;
4153 ** was safeUse: shouldn't be safe!
4156 ** rec must be defined,
4157 ** *rec must be allocated
4158 ** rec->field need only be defined it if is an rvalue
4161 uentry_setUsed (fentry, exprNode_loc (ret));
4162 ret->typ = uentry_getType (fentry);
4164 exprNode_checkUse (ret, s->sref, s->loc);
4166 /* exprNode_checkUse (ret, sRef_makePointer (s->sref), s->loc); */
4167 ret->sref = sRef_makeArrow (s->sref, uentry_rawName (fentry));
4171 else /* Pointer to something that is not a struct or union*/
4173 if (ctype_isRealAbstract (tr))
4175 ctype xrt = ctype_forceRealType (tr);
4179 message ("Arrow access field of abstract type (%t): %s->%s",
4180 t, exprNode_unparse (s), f),
4184 ** Set the state correctly, as if the abstraction is broken.
4187 if (ctype_isRealPointer (xrt) &&
4188 (b = ctype_realType (ctype_baseArrayPtr (xrt)),
4189 ctype_isStructorUnion (b)))
4191 uentry fentry = uentryList_lookupField (ctype_getFields (b), f);
4192 ret->typ = uentry_getType (fentry);
4193 ret->sref = sRef_makeArrow (s->sref, uentry_rawName (fentry));
4197 ret->typ = ctype_unknown;
4198 ret->sref = sRef_undefined;
4201 else /* not a struct, union or abstract */
4203 if (ctype_isUnknown (tr)) {
4204 cstring sn = cstring_copy (f);
4206 DPRINTF (("Here: %s", exprNode_unparse (s)));
4208 exprNode_checkUse (ret, s->sref, s->loc);
4209 exprNode_checkUse (ret, sRef_makePointer (s->sref), s->loc);
4211 cstring_markOwned (sn);
4212 ret->sref = sRef_makeArrow (s->sref, sn);
4214 ret->kind = XPR_ARROW;
4219 message ("Arrow access field of non-struct or union "
4220 "pointer (%t): %s->%s",
4221 t, exprNode_unparse (s), f),
4224 ret->typ = ctype_unknown;
4225 ret->sref = sRef_undefined;
4230 else /* its not a pointer */
4232 if (!ctype_isUnknown (tr))
4236 message ("Arrow access of non-pointer (%t): %s->%s",
4237 t, exprNode_unparse (s), f),
4240 ret->typ = ctype_unknown;
4241 ret->sref = sRef_undefined;
4245 cstring sn = cstring_copy (f);
4247 DPRINTF (("Here: %s", exprNode_unparse (s)));
4249 exprNode_checkUse (ret, s->sref, s->loc);
4250 exprNode_checkUse (ret, sRef_makePointer (s->sref), s->loc);
4252 cstring_markOwned (sn);
4253 ret->sref = sRef_makeArrow (s->sref, sn);
4255 ret->kind = XPR_ARROW;
4266 exprNode_arrowAccess (/*@only@*/ exprNode s,
4267 /*@only@*/ lltok arrow,
4268 /*@only@*/ cstring f)
4270 exprNode res = exprNode_arrowAccessAux (s, lltok_getLoc (arrow), f);
4271 lltok_release (arrow);
4276 ** only postOp's in C: i++ and i--
4280 exprNode_postOp (/*@only@*/ exprNode e, /*@only@*/ lltok op)
4282 /* check modification also */
4283 /* cstring opname = lltok_unparse (op);*/
4285 exprNode ret = exprNode_createPartialCopy (e);
4287 ret->loc = fileloc_update (ret->loc, lltok_getLoc (op));
4288 ret->kind = XPR_POSTOP;
4289 ret->edata = exprData_makeUop (e, op);
4291 if (!exprNode_isDefined (e))
4296 checkMacroParen (e);
4298 exprNode_checkUse (ret, e->sref, e->loc);
4299 exprNode_checkSet (ret, e->sref);
4301 t = exprNode_getType (e);
4303 if (sRef_isUnsafe (e->sref))
4305 voptgenerror (FLG_MACROPARAMS,
4306 message ("Operand of %s is macro parameter (non-functional): %s%s",
4307 lltok_unparse (op), exprNode_unparse (e), lltok_unparse (op)),
4309 sRef_makeSafe (e->sref);
4310 sRef_makeSafe (ret->sref);
4313 if (ctype_isForceRealNumeric (&t) || ctype_isRealAP (t))
4319 if (ctype_isRealAbstract (t))
4323 message ("Operand of %s is abstract type (%t): %s",
4324 lltok_unparse (op), t, exprNode_unparse (e)),
4331 message ("Operand of %s is non-numeric (%t): %s",
4332 lltok_unparse (op), t, exprNode_unparse (e)),
4335 ret->typ = ctype_unknown;
4338 /* if (ctype_isZero (t)) e->typ = ctype_int; */
4340 exprNode_checkModify (e, ret);
4342 /* added 7/11/2000 D.L */
4344 /*DRL 6/8/01 I decided to disable all Splint warnings here since the code
4345 probably needs a rewrite any way */
4350 /* updateEnvironmentForPostOp (e); */
4352 /* start modifications */
4353 /* added by Seejo on 4/16/2000 */
4355 /* Arithmetic operations on pointers wil modify the size/len/null terminated
4357 if ((sRef_isPossiblyNullTerminated (e->sref)) || (sRef_isNullTerminated(e->sref))) {
4359 ret->sref = sRef_copy (e->sref);
4362 if (lltok_getTok (op) == INC_OP) {
4363 if (sRef_getSize(e->sref) > 0) {
4365 sRef_setSize (ret->sref, sRef_getSize(e->sref) - 1);
4367 if (sRef_getLen(e->sref) == 1) { /* i.e. the first character is \0 */
4368 /* Assumption: there is only 1 \0 in the buffer */
4369 /* This will not be correct if there are 2 \0's in the buffer */
4370 sRef_setNotNullTerminatedState(ret->sref);
4371 sRef_resetLen(ret->sref);
4373 sRef_setNullTerminatedState(ret->sref);
4374 sRef_setLen (ret->sref, sRef_getLen(e->sref) - 1);
4376 if (sRef_isNullTerminated (ret->sref))
4377 printf ("ret->sref is Null Terminated\n");
4378 else if (sRef_isPossiblyNullTerminated (ret->sref))
4379 printf ("ret->sref is Possibly Null Terminated\n");
4380 else if (sRef_isNotNullTerminated (ret->sref))
4381 printf ("ret->sref is Not Null Terminated\n");
4386 if (lltok_getTok (op) == DEC_OP) {
4387 if (sRef_getSize(e->sref) >= 0) {
4388 sRef_setSize (ret->sref, sRef_getSize(e->sref) + 1);
4389 sRef_setLen (ret->sref, sRef_getLen(e->sref) + 1);
4394 /* end modifications */
4400 exprNode_preOp (/*@only@*/ exprNode e, /*@only@*/ lltok op)
4402 bool checkMod = FALSE;
4404 int opid = lltok_getTok (op);
4405 exprNode ret = exprNode_createSemiCopy (e);
4407 exprNode_copySets (ret, e);
4409 multiVal_free (ret->val);
4410 ret->val = multiVal_undefined;
4411 ret->loc = fileloc_update (ret->loc, lltok_getLoc (op));
4412 ret->kind = XPR_PREOP;
4413 ret->edata = exprData_makeUop (e, op);
4415 if (exprNode_isError (e))
4420 checkMacroParen (e);
4422 te = exprNode_getType (e);
4423 tr = ctype_realType (te);
4425 if (opid != TAMPERSAND)
4427 exprNode_checkUse (ret, e->sref, e->loc);
4429 if (ctype_isRealAbstract (tr)
4430 && (!(ctype_isRealBool (te) && (opid == TEXCL))))
4432 if (optgenerror (FLG_ABSTRACT,
4433 message ("Operand of %s is abstract type (%t): %s",
4434 lltok_unparse (op), tr,
4435 exprNode_unparse (ret)),
4438 tr = te = ctype_unknown;
4439 ret->typ = ctype_unknown;
4440 sRef_setNullError (e->sref);
4448 case DEC_OP: /* should also check modification! */
4449 if (sRef_isMacroParamRef (e->sref))
4453 message ("Operand of %s is macro parameter (non-functional): %s",
4454 lltok_unparse (op), exprNode_unparse (ret)),
4459 exprNode_checkSet (ret, e->sref);
4462 if (ctype_isForceRealNumeric (&tr) || ctype_isRealAP (tr))
4467 if (context_msgStrictOps ())
4471 message ("Operand of %s is non-numeric (%t): %s",
4472 lltok_unparse (op), te, exprNode_unparse (ret)),
4475 ret->typ = ctype_int;
4478 /* start modifications */
4479 /* added by Seejo on 4/16/2000 */
4481 /* Arithmetic operations on pointers wil modify the size/len/null terminated
4483 if ((sRef_isPossiblyNullTerminated (e->sref)) || (sRef_isNullTerminated(e->sref))) {
4485 ret->sref = sRef_copy (e->sref);
4488 if (lltok_getTok (op) == INC_OP) {
4489 if (sRef_getSize(e->sref) > 0) {
4491 sRef_setSize (ret->sref, sRef_getSize(e->sref) - 1);
4493 if (sRef_getLen(e->sref) == 1) { /* i.e. the first character is \0 */
4494 /* Assumption: there is only 1 \0 in the buffer */
4495 /* This will not be correct if there are 2 \0's in the buffer */
4496 sRef_setNotNullTerminatedState(ret->sref);
4497 sRef_resetLen (ret->sref);
4499 sRef_setNullTerminatedState(ret->sref);
4500 sRef_setLen (ret->sref, sRef_getLen(e->sref) - 1);
4506 if (lltok_getTok (op) == DEC_OP) {
4507 if (sRef_getSize(e->sref) >= 0) {
4508 sRef_setSize (ret->sref, sRef_getSize(e->sref) + 1);
4509 sRef_setLen (ret->sref, sRef_getLen(e->sref) + 1);
4514 /* end modifications */
4521 if (ctype_isForceRealNumeric (&tr))
4525 ret->val = multiVal_invert (exprNode_getValue (e));
4529 ret->val = multiVal_copy (exprNode_getValue (e));
4534 if (context_msgStrictOps ())
4538 message ("Operand of %s is non-numeric (%t): %s",
4539 lltok_unparse (op), te, exprNode_unparse (ret)),
4543 ret->typ = ctype_int;
4547 case TEXCL: /* maybe this should be restricted */
4548 guardSet_flip (ret->guards);
4550 if (ctype_isRealBool (te) || ctype_isUnknown (te))
4556 if (ctype_isRealPointer (tr))
4558 if (sRef_isKnown (e->sref))
4560 ret->guards = guardSet_addFalseGuard (ret->guards, e->sref);
4564 (FLG_BOOLOPS, FLG_PTRNEGATE,
4565 message ("Operand of %s is non-boolean (%t): %s",
4566 lltok_unparse (op), te, exprNode_unparse (ret)),
4573 message ("Operand of %s is non-boolean (%t): %s",
4574 lltok_unparse (op), te, exprNode_unparse (ret)),
4578 ret->typ = ctype_bool;
4583 if (ctype_isForceRealInt (&tr))
4588 if (context_msgStrictOps ())
4592 message ("Operand of %s is non-integer (%t): %s",
4593 lltok_unparse (op), te, exprNode_unparse (ret)),
4597 if (ctype_isInt (e->typ))
4603 ret->typ = ctype_int;
4609 ret->typ = ctype_makePointer (e->typ);
4611 if (sRef_isKnown (e->sref))
4613 ret->sref = sRef_makeAddress (e->sref);
4620 if (ctype_isAP (tr))
4622 ret->typ = ctype_baseArrayPtr (e->typ);
4626 if (ctype_isKnown (te))
4628 if (ctype_isFunction (te))
4634 message ("Dereference of function type (%t): %s",
4635 te, exprNode_unparse (ret)),
4640 voptgenerror (FLG_TYPE,
4641 message ("Dereference of non-pointer (%t): %s",
4642 te, exprNode_unparse (ret)),
4644 ret->typ = ctype_unknown;
4649 ret->typ = ctype_unknown;
4654 if (sRef_isKnown (e->sref))
4656 DPRINTF (("Checking possibly null: %s", sRef_unparseFull (e->sref)));
4658 if (sRef_possiblyNull (e->sref))
4660 DPRINTF (("Checking possibly null: %s", sRef_unparse (e->sref)));
4661 if (!usymtab_isGuarded (e->sref) && !context_inProtectVars ())
4665 message ("Dereference of %s pointer %q: %s",
4666 sRef_nullMessage (e->sref),
4667 sRef_unparse (e->sref),
4668 exprNode_unparse (ret)),
4671 sRef_showNullInfo (e->sref);
4672 sRef_setNotNull (e->sref, e->loc); /* suppress future messages */
4677 ret->sref = sRef_makePointer (e->sref);
4682 llbug (message ("exprNode_preOp: unhandled op: %s", lltok_unparse (op)));
4687 exprNode_checkModify (e, ret);
4694 ** any reason to disallow sizeof (abstract type) ?
4698 ** used by both sizeof
4702 ctype sizeof_resultType (void)
4704 static ctype sizet = ctype_unknown;
4706 if (ctype_isUnknown (sizet))
4708 if (usymtab_existsType (cstring_makeLiteralTemp ("size_t")))
4710 sizet = uentry_getAbstractType (usymtab_lookup (cstring_makeLiteralTemp ("size_t")));
4714 sizet = ctype_ulint;
4721 exprNode_sizeofType (/*@only@*/ qtype qt)
4723 exprNode ret = exprNode_create (sizeof_resultType ());
4724 ctype ct = qtype_getType (qt);
4726 ret->kind = XPR_SIZEOFT;
4727 ret->edata = exprData_makeSizeofType (qt);
4729 voptgenerror (FLG_SIZEOFTYPE,
4730 message ("Parameter to sizeof is type %s: %s",
4732 exprNode_unparse (ret)),
4739 exprNode_alignofType (/*@only@*/ qtype qt)
4741 exprNode ret = exprNode_create (sizeof_resultType ());
4742 ctype ct = qtype_getType (qt);
4744 ret->kind = XPR_ALIGNOFT;
4745 ret->edata = exprData_makeSizeofType (qt);
4747 voptgenerror (FLG_SIZEOFTYPE,
4748 message ("Parameter to alignof is type %s: %s",
4750 exprNode_unparse (ret)),
4756 exprNode exprNode_offsetof (qtype qt, cstringList s)
4758 exprNode ret = exprNode_create (sizeof_resultType ());
4759 ctype ct = qtype_getType (qt);
4761 ret->kind = XPR_OFFSETOF;
4762 ret->edata = exprData_makeOffsetof (qt, s);
4764 if (!ctype_isRealSU (ct))
4766 voptgenerror (FLG_TYPE,
4767 message ("First parameter to offsetof is not a "
4768 "struct or union type (type %s): %s",
4770 exprNode_unparse (ret)),
4777 cstringList_elements (s, el) {
4781 if (ctype_isUndefined (lt))
4785 else if (!ctype_isRealSU (lt))
4787 voptgenerror (FLG_TYPE,
4788 message ("Inner offsetof type is not a "
4789 "struct or union type (type %s before field %s): %s",
4790 ctype_unparse (lt), el,
4791 exprNode_unparse (ret)),
4797 fields = ctype_getFields (ctype_realType (lt));
4798 fld = uentryList_lookupField (fields, el);
4799 DPRINTF (("Try: %s / %s", ctype_unparse (lt), el));
4801 if (uentry_isUndefined (fld))
4803 if (ctype_equal (lt, ct)) {
4804 voptgenerror (FLG_TYPE,
4805 message ("Field %s in offsetof is not the "
4806 "name of a field of %s: %s",
4809 exprNode_unparse (ret)),
4812 voptgenerror (FLG_TYPE,
4813 message ("Deep field %s in offsetof is not the "
4814 "name of a field of %s: %s",
4817 exprNode_unparse (ret)),
4823 lt = uentry_getType (fld);
4826 } end_cstringList_elements;
4828 /* Should report error if its a bit field - behavior is undefined! */
4835 exprNode_sizeofExpr (/*@only@*/ exprNode e)
4839 if (exprNode_isUndefined (e))
4841 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
4842 ret->edata = exprData_makeSingle (e);
4843 ret->typ = sizeof_resultType ();
4844 ret->kind = XPR_SIZEOF;
4848 uentry u = exprNode_getUentry (e);
4850 ret = exprNode_createPartialCopy (e);
4851 ret->edata = exprData_makeSingle (e);
4853 ret->typ = sizeof_resultType ();
4854 ret->kind = XPR_SIZEOF;
4856 if (uentry_isValid (u)
4857 && uentry_isRefParam (u)
4858 && ctype_isRealArray (uentry_getType (u)))
4861 (FLG_SIZEOFFORMALARRAY,
4862 message ("Parameter to sizeof is an array-type function parameter: %s",
4863 exprNode_unparse (ret)),
4869 ** sizeof (x) doesn't "really" use x
4876 exprNode_alignofExpr (/*@only@*/ exprNode e)
4880 if (exprNode_isUndefined (e))
4882 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
4886 ret = exprNode_createPartialCopy (e);
4889 ret->edata = exprData_makeSingle (e);
4890 ret->typ = sizeof_resultType ();
4891 ret->kind = XPR_ALIGNOF;
4894 ** sizeof (x) doesn't "really" use x
4901 exprNode_cast (/*@only@*/ lltok tok, /*@only@*/ exprNode e, /*@only@*/ qtype q)
4907 if (exprNode_isError (e))
4910 lltok_release (tok);
4911 return exprNode_undefined;
4914 checkMacroParen (e);
4916 c = qtype_getType (q);
4917 t = exprNode_getType (e);
4919 ret = exprNode_createPartialCopy (e);
4921 ret->loc = fileloc_update (ret->loc, lltok_getLoc (tok));
4923 ret->kind = XPR_CAST;
4924 ret->edata = exprData_makeCast (tok, e, q);
4926 if (ctype_isRealSU (ctype_getBaseType (sRef_getType (e->sref))))
4929 ** This is a bit of a hack to avoid a problem
4930 ** when the code does,
4931 ** (some other struct) x
4936 ret->sref = sRef_copy (e->sref);
4937 usymtab_addForceMustAlias (ret->sref, e->sref);
4938 sRef_setTypeFull (ret->sref, c);
4939 DPRINTF (("Cast: %s -> %s", sRef_unparseFull (e->sref),
4940 sRef_unparseFull (ret->sref)));
4944 ret->sref = e->sref;
4945 sRef_setTypeFull (ret->sref, c);
4946 DPRINTF (("Cast 2: -> %s", sRef_unparseFull (ret->sref)));
4953 ** void * <-> abstract * (if FLG_ABSTVOIDP)
4954 ** abstract * <-> void * (if FLG_ABSTVOIDP)
4957 if (ctype_isVoid (c)) /* cast to void is always okay --- discard value */
4961 else if (ctype_isRealAP (c)) /* casting to array or pointer */
4963 ctype bc = ctype_getBaseType (c);
4964 ctype bt = ctype_getBaseType (t);
4965 ctype rt = ctype_realType (t);
4967 if (ctype_isFunction (ctype_baseArrayPtr (ctype_realType (c)))
4968 && (ctype_isArrayPtr (rt)
4969 && !ctype_isFunction (ctype_realType (ctype_baseArrayPtr (rt)))))
4973 message ("Cast from function pointer type (%t) to "
4974 "non-function pointer (%t): %s",
4975 c, t, exprNode_unparse (ret)),
4979 if (!ctype_isFunction (ctype_baseArrayPtr (c))
4980 && (ctype_isArrayPtr (rt)
4981 && ctype_isFunction (ctype_realType (ctype_baseArrayPtr (rt)))))
4985 message ("Cast from non-function pointer type (%t) to "
4986 "function pointer (%t): %s",
4987 c, t, exprNode_unparse (ret)),
4991 if (exprNode_isZero (e) && context_getFlag (FLG_ZEROPTR) &&
4992 !(ctype_isRealAbstract (bc)
4993 && context_hasAccess (ctype_typeId (bc))))
4995 ; /* okay to cast zero */
4999 if (ctype_isRealAbstract (bc)
5000 && !context_hasAccess (ctype_typeId (bc)))
5002 if (ctype_isVoidPointer (t) || ctype_isUnknown (t))
5006 message ("Cast to underlying abstract type %t: %s",
5007 c, exprNode_unparse (ret)),
5014 message ("Cast to underlying abstract type %t: %s",
5015 c, exprNode_unparse (ret)),
5020 if (ctype_isRealAbstract (bt)
5021 && !context_hasAccess (ctype_typeId (bt)))
5023 if (ctype_isUnknown (c) || ctype_isVoidPointer (c))
5027 message ("Cast from underlying abstract type %t: %s",
5028 t, exprNode_unparse (ret)),
5035 message ("Cast from underlying abstract type %t: %s",
5036 t, exprNode_unparse (ret)),
5044 ctype bt = ctype_realType (ctype_getBaseType (t));
5045 ctype bc = ctype_realType (ctype_getBaseType (c));
5047 if (ctype_isAbstract (bt) && !context_hasAccess (ctype_typeId (bt)))
5049 if (ctype_match (c, t))
5051 if (ctype_equal (c, t))
5055 message ("Redundant cast involving abstract type %t: %s",
5056 bt, exprNode_unparse (ret)),
5064 message ("Cast from abstract type %t: %s",
5065 bt, exprNode_unparse (ret)),
5070 if (ctype_isAbstract (bc)
5071 && !context_hasAccess (ctype_typeId (bc)))
5073 if (ctype_match (c, t))
5079 DPRINTF (("No access to: %s / %d",
5080 ctype_unparse (bc), ctype_typeId (bc)));
5081 DPRINTF (("Context %s %s",
5082 bool_unparse (context_inFunctionLike ()),
5083 context_unparse ()));
5086 message ("Cast to abstract type %t: %s", bc,
5087 exprNode_unparse (ret)),
5093 if (ctype_isAbstract (c))
5095 if (sRef_isExposed (e->sref) || sRef_isOnly (e->sref))
5097 /* okay, cast exposed to abstract */
5098 sRef_clearExKindComplete (ret->sref, fileloc_undefined);
5102 if (ctype_isVisiblySharable (t)
5103 && sRef_isExternallyVisible (e->sref)
5104 && !(ctype_isAbstract (t)
5105 && context_hasAccess (ctype_typeId (t))))
5109 message ("Cast to abstract type from externally visible "
5110 "mutable storage exposes rep of %s: %s",
5112 exprNode_unparse (e)),
5122 evaluationOrderUndefined (lltok op)
5124 int opid = lltok_getTok (op);
5126 return (opid != AND_OP && opid != OR_OP);
5129 static bool checkIntegral (/*@notnull@*/ exprNode e1,
5130 /*@notnull@*/ exprNode e2,
5131 /*@notnull@*/ exprNode ret,
5136 ctype te1 = exprNode_getType (e1);
5137 ctype te2 = exprNode_getType (e2);
5139 ctype tr1 = ctype_realishType (te1);
5140 ctype tr2 = ctype_realishType (te2);
5142 if (ctype_isForceRealInt (&tr1) && ctype_isForceRealInt (&tr2))
5148 if (context_msgStrictOps ())
5150 if (!ctype_isInt (tr1) && !ctype_isInt (tr2))
5152 if (ctype_sameName (te1, te2))
5156 message ("Operands of %s are non-integer (%t): %s",
5157 lltok_unparse (op), te1,
5158 exprNode_unparse (ret)),
5165 message ("Operands of %s are non-integers (%t, %t): %s",
5166 lltok_unparse (op), te1, te2,
5167 exprNode_unparse (ret)),
5171 else if (!ctype_isInt (tr1))
5175 message ("Left operand of %s is non-integer (%t): %s",
5176 lltok_unparse (op), te1, exprNode_unparse (ret)),
5180 /* !ctype_isInt (te2) */
5184 message ("Right operand of %s is non-integer (%t): %s",
5185 lltok_unparse (op), te2, exprNode_unparse (ret)),
5195 ** returns exprNode representing e1 op e2
5197 ** uses msg if there are errors
5198 ** can be used for both assignment ops and regular ops
5203 static /*@only@*/ /*@notnull@*/ exprNode
5204 exprNode_makeOp (/*@keep@*/ exprNode e1, /*@keep@*/ exprNode e2,
5205 /*@keep@*/ lltok op)
5207 ctype te1, te2, tr1, tr2, tret;
5208 int opid = lltok_getTok (op);
5209 bool hasError = FALSE;
5212 if (exprNode_isError (e1))
5214 ret = exprNode_createPartialNVCopy (e2);
5218 ret = exprNode_createPartialNVCopy (e1);
5221 ret->val = multiVal_undefined;
5223 ret->edata = exprData_makeOp (e1, e2, op);
5225 if (exprNode_isError (e1) || exprNode_isError (e2))
5227 if (opid == TLT || opid == TGT || opid == LE_OP || opid == GE_OP
5228 || opid == EQ_OP || opid == NE_OP
5229 || opid == AND_OP || opid == OR_OP)
5231 ret->typ = ctype_bool;
5234 if (exprNode_isDefined (e1))
5236 exprNode_checkUse (ret, e1->sref, e1->loc);
5239 if (exprNode_isDefined (e2))
5241 exprNode_mergeUSs (ret, e2);
5242 exprNode_checkUse (ret, e2->sref, e2->loc);
5248 tret = ctype_unknown;
5249 te1 = exprNode_getType (e1);
5251 DPRINTF (("te1 = %s / %s", exprNode_unparse (e1), ctype_unparse (te1)));
5253 te2 = exprNode_getType (e2);
5255 tr1 = ctype_realishType (te1);
5256 tr2 = ctype_realishType (te2);
5260 ret->guards = guardSet_or (ret->guards, e2->guards);
5262 else if (opid == AND_OP)
5264 ret->guards = guardSet_and (ret->guards, e2->guards);
5271 if (opid == EQ_OP || opid == NE_OP)
5273 exprNode temp1 = e1, temp2 = e2;
5275 /* could do NULL == x */
5277 if (exprNode_isNullValue (e1) || exprNode_isUnknownConstant (e1))
5279 temp1 = e2; temp2 = e1;
5282 if (exprNode_isNullValue (temp2) || exprNode_isUnknownConstant (temp2))
5284 reflectNullTest (temp1, (opid == NE_OP));
5285 guardSet_free (ret->guards);
5286 ret->guards = guardSet_copy (temp1->guards);
5290 if (opid == TLT || opid == TGT || opid == LE_OP || opid == GE_OP
5291 || opid == EQ_OP || opid == NE_OP || opid == AND_OP || opid == OR_OP)
5296 if (anyAbstract (tr1, tr2) &&
5297 (!((ctype_isRealBool (te1) || ctype_isRealBool (te2)) &&
5298 (opid == AND_OP || opid == OR_OP
5299 || opid == EQ_OP || opid == NE_OP))))
5301 abstractOpError (tr1, tr2, op, e1, e2, e1->loc, e2->loc);
5303 else if (ctype_isUnknown (te1) || ctype_isUnknown (te2))
5305 /* unknown types, no comparisons possible */
5311 case TMULT: /* multiplication and division: */
5313 case MUL_ASSIGN: /* numeric, numeric -> numeric */
5314 case DIV_ASSIGN: /* */
5316 tret = checkNumerics (tr1, tr2, te1, te2, e1, e2, op);
5319 case TPLUS: /* addition and subtraction: */
5320 case TMINUS: /* pointer, int -> pointer */
5321 case SUB_ASSIGN: /* int, pointer -> pointer */
5322 case ADD_ASSIGN: /* numeric, numeric -> numeric */
5324 tr1 = ctype_fixArrayPtr (tr1);
5326 if ((ctype_isRealPointer (tr1) && !exprNode_isNullValue (e1))
5327 && (!ctype_isRealPointer (tr2) && ctype_isRealInt (tr2)))
5331 if (context_msgPointerArith ())
5335 message ("Pointer arithmetic (%t, %t): %s",
5336 te1, te2, exprNode_unparse (ret)),
5341 ** Swap terms so e1 is always the pointer
5344 if (ctype_isRealPointer (tr1))
5350 exprNode_swap (e1, e2);
5354 if (sRef_possiblyNull (e1->sref)
5355 && !usymtab_isGuarded (e1->sref))
5358 (FLG_NULLPOINTERARITH,
5359 message ("Pointer arithmetic involving possibly "
5360 "null pointer %s: %s",
5361 exprNode_unparse (e1),
5362 exprNode_unparse (ret)),
5366 ret->sref = sRef_copy (e1->sref);
5368 /* start modifications */
5369 /* added by Seejo on 4/16/2000 */
5371 /* Arithmetic operations on pointers wil modify the size/len/null terminated
5373 if ((sRef_isPossiblyNullTerminated (e1->sref)) || (sRef_isNullTerminated(e1->sref))) {
5376 added ugly fixed to stop
5377 program from crashing on point + int +int
5378 one day I'll fix this or ask Seejo wtf the codes supposed to do. */
5380 if (!multiVal_isInt (e2->val) )
5384 val = (int) multiVal_forceInt (e2->val);
5386 /* Operator : + or += */
5387 if ((lltok_getTok (op) == TPLUS) || (lltok_getTok(op) == ADD_ASSIGN)) {
5388 if (sRef_getSize(e1->sref) >= val) {/* Incrementing the pointer by
5389 val should not result in a
5390 size < 0 (size = 0 is ok !) */
5392 sRef_setSize (ret->sref, sRef_getSize(e1->sref) - val);
5394 if (sRef_getLen(e1->sref) == val) { /* i.e. the character at posn val is \0 */
5395 sRef_setNotNullTerminatedState(ret->sref);
5396 sRef_resetLen (ret->sref);
5398 sRef_setNullTerminatedState(ret->sref);
5399 sRef_setLen (ret->sref, sRef_getLen(e1->sref) - val);
5404 /* Operator : - or -= */
5405 if ((lltok_getTok (op) == TMINUS) || (lltok_getTok (op) == SUB_ASSIGN)) {
5406 if (sRef_getSize(e1->sref) >= 0) {
5407 sRef_setSize (ret->sref, sRef_getSize(e1->sref) + val);
5408 sRef_setLen (ret->sref, sRef_getLen(e1->sref) + val);
5413 /* end modifications */
5415 sRef_setNullError (ret->sref);
5418 ** Fixed for 2.2c: the alias state of ptr + int is dependent,
5419 ** since is points to storage that should not be deallocated
5420 ** through this pointer.
5423 if (sRef_isOnly (ret->sref)
5424 || sRef_isFresh (ret->sref))
5426 sRef_setAliasKind (ret->sref, AK_DEPENDENT, exprNode_loc (ret));
5431 else if ((!ctype_isRealPointer(tr1) && ctype_isRealInt (tr1))
5432 && (ctype_isRealPointer (tr2) && !exprNode_isNullValue (e2)))
5434 if (context_msgPointerArith ())
5438 message ("Pointer arithmetic (%t, %t): %s",
5439 te1, te2, exprNode_unparse (ret)),
5443 if (sRef_possiblyNull (e1->sref)
5444 && !usymtab_isGuarded (e1->sref))
5447 (FLG_NULLPOINTERARITH,
5448 message ("Pointer arithmetic involving possibly "
5449 "null pointer %s: %s",
5450 exprNode_unparse (e2),
5451 exprNode_unparse (ret)),
5455 ret->sref = sRef_copy (e2->sref);
5457 /* start modifications */
5458 /* added by Seejo on 4/16/2000 */
5460 /* Arithmetic operations on pointers wil modify the size/len/null terminated
5463 if ((sRef_isPossiblyNullTerminated (e2->sref)) || (sRef_isNullTerminated(e2->sref))) {
5464 int val = (int) multiVal_forceInt (e1->val);
5466 /* Operator : + or += */
5467 if ((lltok_getTok (op) == TPLUS) || (lltok_getTok(op) == ADD_ASSIGN)) {
5468 if (sRef_getSize(e2->sref) >= val) {/* Incrementing the pointer by
5469 val should not result in a
5470 size < 0 (size = 0 is ok !) */
5472 sRef_setSize (ret->sref, sRef_getSize(e2->sref) - val);
5474 if (sRef_getLen(e2->sref) == val) { /* i.e. the character at posn val is \0 */
5475 sRef_setNotNullTerminatedState(ret->sref);
5476 sRef_resetLen (ret->sref);
5478 sRef_setNullTerminatedState(ret->sref);
5479 sRef_setLen (ret->sref, sRef_getLen(e2->sref) - val);
5484 /* Operator : - or -= */
5485 if ((lltok_getTok (op) == TMINUS) || (lltok_getTok (op) == SUB_ASSIGN)) {
5486 if (sRef_getSize(e2->sref) >= 0) {
5487 sRef_setSize (ret->sref, sRef_getSize(e2->sref) + val);
5488 sRef_setLen (ret->sref, sRef_getLen(e2->sref) + val);
5492 /* end modifications */
5494 sRef_setNullError (ret->sref);
5497 ** Fixed for 2.2c: the alias state of ptr + int is dependent,
5498 ** since is points to storage that should not be deallocated
5499 ** through this pointer.
5502 if (sRef_isOnly (ret->sref)
5503 || sRef_isFresh (ret->sref)) {
5504 sRef_setAliasKind (ret->sref, AK_DEPENDENT, exprNode_loc (ret));
5508 ret->sref = e2->sref;
5512 tret = checkNumerics (tr1, tr2, te1, te2, e1, e2, op);
5517 case LEFT_ASSIGN: /* Shifts: should be unsigned values */
5521 case TAMPERSAND: /* bitwise & */
5523 case TCIRC: /* ^ (XOR) */
5528 bool reported = FALSE;
5529 flagcode code = FLG_BITWISEOPS;
5531 if (opid == LEFT_OP || opid == LEFT_ASSIGN
5532 || opid == RIGHT_OP || opid == RIGHT_ASSIGN) {
5533 code = FLG_SHIFTSIGNED;
5536 if (!ctype_isUnsigned (tr1))
5538 if (exprNode_isNonNegative (e1)) {
5541 reported = optgenerror
5543 message ("Left operand of %s is not unsigned value (%t): %s",
5544 lltok_unparse (op), te1,
5545 exprNode_unparse (ret)),
5555 /* right need not be signed for shifts */
5556 if (code != FLG_SHIFTSIGNED
5557 && !ctype_isUnsigned (tr2))
5559 if (!exprNode_isNonNegative (e2)) {
5560 reported = optgenerror
5562 message ("Right operand of %s is not unsigned value (%t): %s",
5563 lltok_unparse (op), te2,
5564 exprNode_unparse (ret)),
5572 if (!checkIntegral (e1, e2, ret, op)) {
5573 te1 = ctype_unknown;
5577 DPRINTF (("Set: %s", ctype_unparse (te1)));
5580 ** tret is the widest type of te1 and te2
5583 tret = ctype_widest (te1, te2);
5588 if (checkIntegral (e1, e2, ret, op)) {
5591 tret = ctype_unknown;
5596 case TLT: /* comparisons */
5597 case TGT: /* numeric, numeric -> bool */
5599 DPRINTF (("Here we go: %s / %s",
5600 ctype_unparse (tr1), ctype_unparse (tr2)));
5602 if ((ctype_isReal (tr1) && !ctype_isInt (tr1))
5603 || (ctype_isReal (tr2) && !ctype_isInt (tr2)))
5606 bool fepsilon = FALSE;
5608 if (!ctype_isReal (rtype) || ctype_isInt (rtype))
5613 if (opid == TLT || opid == TGT)
5615 uentry ue1 = exprNode_getUentry (e1);
5616 uentry ue2 = exprNode_getUentry (e2);
5619 ** FLT_EPSILON, etc. really is a variable, not
5623 if (uentry_isVariable (ue1))
5625 cstring uname = uentry_rawName (ue1);
5627 if (cstring_equalLit (uname, "FLT_EPSILON")
5628 || cstring_equalLit (uname, "DBL_EPSILON")
5629 || cstring_equalLit (uname, "LDBL_EPSILON"))
5635 if (uentry_isVariable (ue2))
5637 cstring uname = uentry_rawName (ue2);
5639 if (cstring_equalLit (uname, "FLT_EPSILON")
5640 || cstring_equalLit (uname, "DBL_EPSILON")
5641 || cstring_equalLit (uname, "LDBL_EPSILON"))
5650 ; /* Don't complain. */
5656 message ("Dangerous comparison involving %s types: %s",
5657 ctype_unparse (rtype),
5658 exprNode_unparse (ret)),
5667 ** Types should match.
5670 DPRINTF (("Match types: %s / %s", exprNode_unparse (e1),
5671 exprNode_unparse (e2)));
5673 if (!exprNode_matchTypes (e1, e2))
5675 hasError = gentypeerror
5677 message ("Operands of %s have incompatible types (%t, %t): %s",
5678 lltok_unparse (op), te1, te2, exprNode_unparse (ret)),
5684 || (ctype_isForceRealNumeric (&tr1)
5685 && ctype_isForceRealNumeric (&tr2)) ||
5686 (ctype_isRealPointer (tr1) && ctype_isRealPointer (tr2)))
5692 if ((ctype_isRealNumeric (tr1) && ctype_isRealPointer (tr2)) ||
5693 (ctype_isRealPointer (tr1) && ctype_isRealNumeric (tr2)))
5697 message ("Comparison of pointer and numeric (%t, %t): %s",
5698 te1, te2, exprNode_unparse (ret)),
5703 (void) checkNumerics (tr1, tr2, te1, te2, e1, e2, op);
5708 /* certain comparisons on unsigned's and zero look suspicious */
5710 if (opid == TLT || opid == LE_OP || opid == GE_OP)
5712 if ((ctype_isUnsigned (tr1) && exprNode_isZero (e2))
5713 || (ctype_isUnsigned (tr2) && exprNode_isZero (e1)))
5716 (FLG_UNSIGNEDCOMPARE,
5717 message ("Comparison of unsigned value involving zero: %s",
5718 exprNode_unparse (ret)),
5723 /* EQ_OP should NOT be used with booleans (unless one is FALSE) */
5725 if ((opid == EQ_OP || opid == NE_OP) &&
5726 ctype_isDirectBool (tr1) && ctype_isDirectBool (tr2))
5729 ** is one a variable?
5732 if (uentry_isVariable (exprNode_getUentry (e1))
5733 || uentry_isVariable (exprNode_getUentry (e2)))
5736 ** comparisons with FALSE are okay
5739 if (exprNode_isFalseConstant (e1)
5740 || exprNode_isFalseConstant (e2))
5749 ("Use of %q with %s variables (risks inconsistency because "
5750 "of multiple true values): %s",
5751 cstring_makeLiteral ((opid == EQ_OP) ? "==" : "!="),
5752 context_printBoolName (), exprNode_unparse (ret)),
5759 case AND_OP: /* bool, bool -> bool */
5762 if (ctype_isForceRealBool (&tr1) && ctype_isForceRealBool (&tr2))
5768 if (context_maybeSet (FLG_BOOLOPS))
5770 if (!ctype_isRealBool (te1) && !ctype_isRealBool (te2))
5772 if (ctype_sameName (te1, te2))
5776 message ("Operands of %s are non-boolean (%t): %s",
5777 lltok_unparse (op), te1,
5778 exprNode_unparse (ret)),
5786 ("Operands of %s are non-booleans (%t, %t): %s",
5787 lltok_unparse (op), te1, te2, exprNode_unparse (ret)),
5791 else if (!ctype_isRealBool (te1))
5795 message ("Left operand of %s is non-boolean (%t): %s",
5796 lltok_unparse (op), te1, exprNode_unparse (ret)),
5799 else if (!ctype_isRealBool (te2))
5803 message ("Right operand of %s is non-boolean (%t): %s",
5804 lltok_unparse (op), te2, exprNode_unparse (ret)),
5817 (cstring_makeLiteral
5818 ("There has been a problem in the parser. This is believed to result "
5819 "from a problem with bison v. 1.25. Please try rebuidling Splint "
5820 "using the pre-compiled grammar files by commenting out the "
5821 "BISON= line in the top-level Makefile."));
5826 DPRINTF (("Return type: %s", ctype_unparse (tret)));
5829 exprNode_checkUse (ret, e1->sref, e1->loc);
5830 exprNode_mergeUSs (ret, e2);
5831 exprNode_checkUse (ret, e2->sref, e2->loc);
5837 exprNode_op (/*@only@*/ exprNode e1, /*@keep@*/ exprNode e2,
5838 /*@only@*/ lltok op)
5842 checkMacroParen (e1);
5843 checkMacroParen (e2);
5845 if (evaluationOrderUndefined (op) && context_maybeSet (FLG_EVALORDER))
5847 checkExpressionDefined (e1, e2, op);
5850 ret = exprNode_makeOp (e1, e2, op);
5855 void exprNode_checkAssignMod (exprNode e1, exprNode ret)
5858 ** This is somewhat bogus!
5860 ** Assigning to a nested observer in a non-observer datatype
5861 ** should not produce an error.
5864 sRef ref = exprNode_getSref (e1);
5866 DPRINTF (("Check assign mod: %s",
5867 sRef_unparseFull (ref)));
5869 if (sRef_isObserver (ref)
5870 || ((sRef_isFileStatic (ref) || sRef_isFileOrGlobalScope (ref))
5871 && ctype_isArray (ctype_realType (sRef_getType (ref)))))
5873 sRef base = sRef_getBase (ref);
5875 if (sRef_isValid (base) && sRef_isObserver (base))
5877 exprNode_checkModify (e1, ret);
5881 exprNode_checkModifyVal (e1, ret);
5886 exprNode_checkModify (e1, ret);
5891 exprNode_assign (/*@only@*/ exprNode e1,
5892 /*@only@*/ exprNode e2, /*@only@*/ lltok op)
5894 bool isalloc = FALSE;
5895 bool isjustalloc = FALSE;
5896 bool noalias = FALSE;
5899 DPRINTF (("%s [%s] <- %s [%s]",
5900 exprNode_unparse (e1),
5901 ctype_unparse (e1->typ),
5902 exprNode_unparse (e2),
5903 ctype_unparse (e2->typ)));
5905 if (lltok_getTok (op) != TASSIGN)
5907 ret = exprNode_makeOp (e1, e2, op);
5909 DPRINTF (("Here goes: %s %s",
5910 ctype_unparse (e1->typ),
5911 ctype_unparse (e2->typ)));
5913 if (ctype_isNumeric (e2->typ)
5914 || ctype_isNumeric (e1->typ))
5916 /* Its a pointer arithmetic expression like ptr += i */
5922 ret = exprNode_createPartialCopy (e1);
5923 ret->kind = XPR_ASSIGN;
5924 ret->edata = exprData_makeOp (e1, e2, op);
5926 if (!exprNode_isError (e2))
5928 ret->sets = sRefSet_union (ret->sets, e2->sets);
5929 ret->msets = sRefSet_union (ret->msets, e2->msets);
5930 ret->uses = sRefSet_union (ret->uses, e2->uses);
5934 checkExpressionDefined (e1, e2, op);
5936 if (exprNode_isError (e1))
5938 if (!exprNode_isError (e2))
5940 ret->loc = fileloc_update (ret->loc, e2->loc);
5944 ret->loc = fileloc_update (ret->loc, g_currentloc);
5948 if (!exprNode_isError (e2))
5950 checkMacroParen (e2);
5953 if (exprNode_isDefined (e1))
5955 if (sRef_isMacroParamRef (e1->sref))
5957 if (context_inIterDef ())
5959 uentry ue = sRef_getUentry (e1->sref);
5961 if (uentry_isYield (ue))
5967 if (fileloc_isDefined (e1->loc))
5971 message ("Assignment to non-yield iter parameter: %q",
5972 sRef_unparse (e1->sref)),
5979 message ("Assignment to non-yield iter parameter: %q",
5980 sRef_unparse (e1->sref)),
5987 if (fileloc_isDefined (e1->loc))
5991 message ("Assignment to macro parameter: %q",
5992 sRef_unparse (e1->sref)),
5999 message ("Assignment to macro parameter: %q",
6000 sRef_unparse (e1->sref)),
6004 exprNode_checkAssignMod (e1, ret); /* evans 2001-07-22 */
6009 exprNode_checkAssignMod (e1, ret);
6012 if (exprNode_isDefined (e2))
6014 if (lltok_getTok (op) == TASSIGN)
6016 ctype te1 = exprNode_getType (e1);
6017 ctype te2 = exprNode_getType (e2);
6019 if (!ctype_forceMatch (te1, te2))
6021 if (exprNode_matchLiteral (te1, e2))
6029 message ("Assignment of %t to %t: %s %s %s",
6030 te2, te1, exprNode_unparse (e1),
6032 exprNode_unparse (e2)),
6038 exprNode_mergeUSs (ret, e2);
6039 exprNode_checkUse (ret, e2->sref, e2->loc);
6041 DPRINTF (("Do assign! %s %s", exprNode_unparse (e1), exprNode_unparse (e2)));
6048 doAssign (e1, e2, FALSE);
6051 ret->sref = e1->sref;
6055 if (exprNode_isDefined (e2))
6057 exprNode_mergeUSs (ret, e2);
6058 exprNode_checkUse (ret, e2->sref, e2->loc);
6062 if (sRef_isPointer (e1->sref) && !sRef_isMacroParamRef (e1->sref))
6064 exprNode_checkUse (ret, sRef_getBase (e1->sref), e1->loc);
6067 isjustalloc = sRef_isJustAllocated (e1->sref);
6068 isalloc = sRef_isAllocated (e1->sref);
6070 if (sRef_isField (e1->sref))
6072 sRef root = sRef_getRootBase (sRef_getBase (e1->sref));
6074 if (!sRef_isAllocated (root) && !sRef_isMacroParamRef (root))
6076 exprNode_checkUse (ret, root, e1->loc);
6082 ** be careful! this defines e1->sref.
6085 /* evans 2001-07-22: removed if (!sRef_isMacroParamRef (e1->sref)) */
6087 DPRINTF (("Setting: %s -> %s", exprNode_unparse (ret), sRef_unparse (e1->sref)));
6088 exprNode_checkSet (ret, e1->sref);
6092 sRef_setAllocatedComplete (e1->sref, exprNode_isDefined (e2)
6093 ? e2->loc : e1->loc);
6099 sRef_setAllocatedShallowComplete (e1->sref, exprNode_loc (e2));
6108 exprNode_cond (/*@keep@*/ exprNode pred, /*@keep@*/ exprNode ifclause,
6109 /*@keep@*/ exprNode elseclause)
6113 if (!exprNode_isError (pred))
6115 ret = exprNode_createPartialCopy (pred);
6116 checkMacroParen (pred);
6117 exprNode_checkPred (cstring_makeLiteralTemp ("conditional"), pred);
6119 if (!exprNode_isError (ifclause))
6121 checkMacroParen (ifclause); /* update macro counts! */
6123 if (!exprNode_isError (elseclause))
6125 checkMacroParen (elseclause);
6127 if (!exprNode_matchTypes (ifclause, elseclause))
6130 (exprNode_getType (ifclause),
6132 exprNode_getType (elseclause),
6134 message ("Conditional clauses are not of same type: "
6136 exprNode_unparse (ifclause),
6137 exprNode_getType (ifclause),
6138 exprNode_unparse (elseclause),
6139 exprNode_getType (elseclause)),
6142 ret->sref = sRef_undefined;
6143 ret->typ = ctype_unknown;
6148 /* for now...should merge the states */
6149 ret->sref = ifclause->sref;
6150 ret->typ = ifclause->typ;
6152 if (exprNode_isNullValue (ifclause))
6154 ret->typ = elseclause->typ;
6158 exprNode_checkUse (ret, pred->sref, pred->loc);
6159 exprNode_checkUse (ifclause, ifclause->sref, ifclause->loc);
6160 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
6162 exprNode_mergeCondUSs (ret, ifclause, elseclause);
6167 ret->typ = ifclause->typ;
6169 exprNode_checkUse (pred, pred->sref, pred->loc);
6170 exprNode_checkUse (ifclause, ifclause->sref, ifclause->loc);
6172 exprNode_mergeCondUSs (ret, ifclause, exprNode_undefined);
6177 if (!exprNode_isError (elseclause))
6179 ret->typ = elseclause->typ;
6181 exprNode_checkUse (pred, pred->sref, pred->loc);
6182 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
6184 exprNode_mergeCondUSs (ret, exprNode_undefined, elseclause);
6188 else /* pred is error */
6190 if (!exprNode_isError (ifclause))
6192 ret = exprNode_createSemiCopy (ifclause);
6194 checkMacroParen (ifclause); /* update macro counts! */
6196 if (!exprNode_isError (elseclause))
6198 checkMacroParen (elseclause);
6200 ret->typ = ifclause->typ;
6202 if (!ctype_forceMatch (ifclause->typ, elseclause->typ))
6205 (exprNode_getType (ifclause),
6207 exprNode_getType (elseclause),
6209 message ("Conditional clauses are not of same type: "
6211 exprNode_unparse (ifclause),
6212 exprNode_getType (ifclause),
6213 exprNode_unparse (elseclause),
6214 exprNode_getType (elseclause)),
6217 ret->typ = ctype_unknown;
6221 exprNode_checkUse (ifclause, ifclause->sref, ifclause->loc);
6222 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
6224 exprNode_mergeCondUSs (ret, ifclause, elseclause);
6227 else if (!exprNode_isError (elseclause)) /* pred, if errors */
6229 ret = exprNode_createSemiCopy (ifclause);
6231 ret->typ = elseclause->typ;
6232 checkMacroParen (elseclause);
6234 exprNode_checkUse (elseclause, elseclause->sref, elseclause->loc);
6235 exprNode_mergeCondUSs (ret, exprNode_undefined, elseclause);
6237 else /* all errors! */
6239 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
6243 ret->kind = XPR_COND;
6244 ret->edata = exprData_makeCond (pred, ifclause, elseclause);
6246 if (exprNode_isDefined (ifclause) && exprNode_isDefined (elseclause))
6248 exprNode_combineControl (ret, ifclause, elseclause);
6255 exprNode_vaArg (/*@only@*/ lltok tok, /*@only@*/ exprNode arg, /*@only@*/ qtype qt)
6257 ctype totype = qtype_getType (qt);
6259 exprNode_createPartialLocCopy (arg, fileloc_copy (lltok_getLoc (tok)));
6263 ** check use of va_arg : <valist>, type -> type
6266 if (exprNode_isError (arg))
6271 targ = exprNode_getType (arg);
6274 ** arg should have be a pointer
6277 if (!ctype_isUA (targ) ||
6278 (!usymId_equal (ctype_typeId (targ),
6279 usymtab_getTypeId (cstring_makeLiteralTemp ("va_list")))))
6283 message ("First argument to va_arg is not a va_list (type %t): %s",
6284 targ, exprNode_unparse (arg)),
6288 exprNode_checkSet (ret, arg->sref);
6292 ** return type is totype
6296 ret->kind = XPR_VAARG;
6297 ret->edata = exprData_makeCast (tok, arg, qt);
6302 exprNode exprNode_labelMarker (/*@only@*/ cstring label)
6304 exprNode ret = exprNode_createPlain (ctype_undefined);
6305 ret->kind = XPR_LABEL;
6306 ret->edata = exprData_makeLiteral (label);
6307 ret->isJumpPoint = TRUE;
6309 return (ret); /* for now, ignore label */
6312 exprNode exprNode_notReached (/*@returned@*/ exprNode stmt)
6314 if (exprNode_isDefined (stmt))
6316 stmt->isJumpPoint = TRUE;
6318 /* This prevent stray no return path errors, etc. */
6319 stmt->exitCode = XK_MUSTEXIT;
6325 bool exprNode_isDefaultMarker (exprNode e)
6327 if (exprNode_isDefined (e))
6329 return (e->kind == XPR_DEFAULT || e->kind == XPR_FTDEFAULT);
6335 bool exprNode_isCaseMarker (exprNode e)
6337 if (exprNode_isDefined (e))
6339 return (e->kind == XPR_FTCASE || e->kind == XPR_CASE);
6345 bool exprNode_isLabelMarker (exprNode e)
6347 if (exprNode_isDefined (e))
6349 return (e->kind == XPR_LABEL);
6355 exprNode exprNode_caseMarker (/*@only@*/ exprNode test, bool fallThrough)
6357 exprNode ret = exprNode_createPartialCopy (test);
6359 ret->kind = fallThrough ? XPR_FTCASE : XPR_CASE;
6361 if (exprNode_isError (test)) {
6365 exprNode_checkUse (ret, test->sref, test->loc);
6367 usymtab_setExitCode (ret->exitCode);
6371 usymtab_setMustBreak ();
6374 ret->edata = exprData_makeSingle (test);
6375 ret->isJumpPoint = TRUE;
6381 exprNode exprNode_caseStatement (/*@only@*/ exprNode test, /*@only@*/ exprNode stmt, bool fallThrough)
6383 exprNode ret = exprNode_createPartialCopy (test);
6385 ret->kind = fallThrough ? XPR_FTCASE : XPR_CASE;
6386 ret->edata = exprData_makePair (test, stmt);
6387 ret->isJumpPoint = TRUE;
6389 if (exprNode_isError (test))
6394 exprNode_checkUse (ret, test->sref, test->loc);
6396 if (exprNode_isError (stmt))
6401 exprNode_mergeUSs (ret, stmt);
6403 ret->exitCode = stmt->exitCode;
6404 ret->mustBreak = stmt->mustBreak;
6405 ret->canBreak = stmt->canBreak;
6407 usymtab_setExitCode (ret->exitCode);
6411 usymtab_setMustBreak ();
6418 /*@notnull@*/ /*@only@*/ exprNode
6419 exprNode_defaultMarker (/*@only@*/ lltok def, bool fallThrough)
6421 exprNode ret = exprNode_createTok (def);
6423 ret->isJumpPoint = TRUE;
6424 ret->kind = fallThrough ? XPR_FTDEFAULT : XPR_DEFAULT;
6429 exprNode_mayEscape (exprNode e)
6431 if (exprNode_isDefined (e))
6433 return exitkind_couldEscape (e->exitCode);
6439 exprNode_mustBreak (exprNode e)
6441 if (exprNode_isDefined (e))
6443 return e->mustBreak;
6450 exprNode_mustEscape (exprNode e)
6452 if (exprNode_isDefined (e))
6454 return exitkind_mustEscape (e->exitCode) || exprNode_mustBreak (e);
6461 exprNode_errorEscape (exprNode e)
6463 if (exprNode_isDefined (e))
6465 return exitkind_isError (e->exitCode);
6471 exprNode exprNode_concat (/*@only@*/ exprNode e1, /*@only@*/ exprNode e2)
6473 exprNode ret = exprNode_createPartialCopy (e1);
6475 DPRINTF (("Concat: %s / %s", exprNode_unparse (e1), exprNode_unparse (e2)));
6477 ret->edata = exprData_makePair (e1, e2);
6478 ret->kind = XPR_STMTLIST;
6480 if (exprNode_isDefined (e1))
6482 ret->isJumpPoint = e1->isJumpPoint;
6483 ret->canBreak = e1->canBreak;
6487 if (exprNode_isDefined (e2))
6489 ret->loc = fileloc_update (ret->loc, e2->loc);
6493 if (exprNode_isDefined (e2))
6495 ret->exitCode = e2->exitCode;
6496 ret->mustBreak = e2->mustBreak;
6497 if (e2->canBreak) ret->canBreak = TRUE;
6501 ** if e1 must return, then e2 is unreachable!
6504 if (exprNode_isDefined (e1) && exprNode_isDefined (e2))
6506 if ((exprNode_mustEscape (e1) || exprNode_mustBreak (e1))
6507 && !(e2->isJumpPoint))
6509 if (context_getFlag (FLG_UNREACHABLE))
6513 if (e2->kind == XPR_STMT)
6515 nr = exprData_getUopNode (e2->edata);
6518 if ((nr->kind == XPR_TOK
6519 && lltok_isSemi (exprData_getTok (nr->edata))))
6521 /* okay to have unreachable ";" */
6522 ret->exitCode = XK_MUSTEXIT;
6523 ret->canBreak = TRUE;
6527 if (optgenerror (FLG_UNREACHABLE,
6528 message ("Unreachable code: %s",
6529 exprNode_unparseFirst (nr)),
6532 ret->isJumpPoint = TRUE;
6533 ret->mustBreak = FALSE;
6534 ret->exitCode = XK_ERROR;
6535 DPRINTF (("Jump point: %s", exprNode_unparse (ret)));
6539 ret->exitCode = XK_MUSTEXIT;
6540 ret->canBreak = TRUE;
6548 if ((e2->kind == XPR_CASE || e2->kind == XPR_DEFAULT))
6551 ** We want a warning anytime we have:
6553 ** yyy; <<<- no break or return
6557 exprNode lastStmt = exprNode_lastStatement (e1);
6559 if (exprNode_isDefined (lastStmt)
6560 && !exprNode_mustEscape (lastStmt)
6561 && !exprNode_mustBreak (lastStmt)
6562 && !exprNode_isCaseMarker (lastStmt)
6563 && !exprNode_isDefaultMarker (lastStmt)
6564 && !exprNode_isLabelMarker (lastStmt))
6566 voptgenerror (FLG_CASEBREAK,
6568 ("Fall through case (no preceding break)"),
6575 exprNode_mergeUSs (ret, e2);
6577 usymtab_setExitCode (ret->exitCode);
6581 usymtab_setMustBreak ();
6587 exprNode exprNode_createTok (/*@only@*/ lltok t)
6589 exprNode ret; /*@i23 if on same line, bad things happen...!@*/
6590 ret = exprNode_create (ctype_unknown);
6591 ret->kind = XPR_TOK;
6592 ret->edata = exprData_makeTok (t);
6596 exprNode exprNode_statement (/*@only@*/ exprNode e, /*@only@*/ lltok t)
6598 if (!exprNode_isError (e))
6600 exprNode_checkStatement(e);
6603 return (exprNode_statementError (e, t));
6606 static exprNode exprNode_statementError (/*@only@*/ exprNode e, /*@only@*/ lltok t)
6608 exprNode ret = exprNode_createPartialCopy (e);
6610 if (!exprNode_isError (e))
6612 if (e->kind != XPR_ASSIGN)
6614 exprNode_checkUse (ret, e->sref, e->loc);
6617 ret->exitCode = e->exitCode;
6618 ret->canBreak = e->canBreak;
6619 ret->mustBreak = e->mustBreak;
6622 ret->edata = exprData_makeUop (e, t);
6623 ret->kind = XPR_STMT;
6628 exprNode exprNode_checkExpr (/*@returned@*/ exprNode e)
6630 if (!exprNode_isError (e))
6632 if (e->kind != XPR_ASSIGN)
6634 exprNode_checkUse (e, e->sref, e->loc);
6641 void exprNode_produceGuards (exprNode pred)
6643 if (!exprNode_isError (pred))
6645 if (ctype_isRealPointer (pred->typ))
6647 pred->guards = guardSet_addTrueGuard (pred->guards, pred->sref);
6650 exprNode_checkUse (pred, pred->sref, pred->loc);
6651 exprNode_resetSref (pred);
6655 exprNode exprNode_makeBlock (/*@only@*/ exprNode e)
6657 exprNode ret = exprNode_createPartialCopy (e);
6659 if (!exprNode_isError (e))
6661 ret->exitCode = e->exitCode;
6662 ret->canBreak = e->canBreak;
6663 ret->mustBreak = e->mustBreak;
6666 ret->edata = exprData_makeSingle (e);
6667 ret->kind = XPR_BLOCK;
6671 bool exprNode_isBlock (exprNode e)
6673 return (exprNode_isDefined (e)
6674 && ((e)->kind == XPR_BLOCK));
6677 bool exprNode_isAssign (exprNode e)
6679 if (exprNode_isDefined (e))
6681 return (e->kind == XPR_ASSIGN);
6687 bool exprNode_isEmptyStatement (exprNode e)
6689 return (exprNode_isDefined (e)
6690 && (e->kind == XPR_TOK)
6691 && (lltok_isSemi (exprData_getTok (e->edata))));
6694 exprNode exprNode_if (/*@only@*/ exprNode pred, /*@only@*/ exprNode tclause)
6697 bool emptyErr = FALSE;
6699 if (context_maybeSet (FLG_IFEMPTY))
6701 if (exprNode_isEmptyStatement (tclause))
6703 emptyErr = optgenerror (FLG_IFEMPTY,
6705 ("Body of if statement is empty"),
6706 exprNode_loc (tclause));
6710 if (!emptyErr && context_maybeSet (FLG_IFBLOCK))
6712 if (exprNode_isDefined (tclause)
6713 && !exprNode_isBlock (tclause))
6715 voptgenerror (FLG_IFBLOCK,
6717 ("Body of if statement is not a block: %s",
6718 exprNode_unparse (tclause)),
6719 exprNode_loc (tclause));
6723 if (exprNode_isError (pred))
6725 if (exprNode_isError (tclause))
6727 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
6731 ret = exprNode_createPartialCopy (tclause);
6736 if (exprNode_mustEscape (pred))
6740 message ("Predicate always exits: %s", exprNode_unparse (pred)),
6741 exprNode_loc (pred));
6744 exprNode_checkPred (cstring_makeLiteralTemp ("if"), pred);
6745 exprNode_checkUse (pred, pred->sref, pred->loc);
6747 if (!exprNode_isError (tclause))
6749 exprNode_mergeCondUSs (pred, tclause, exprNode_undefined);
6752 ret = exprNode_createPartialCopy (pred);
6756 ret->edata = exprData_makePair (pred, tclause);
6758 ret->exitCode = XK_UNKNOWN;
6760 if (exprNode_isDefined (tclause))
6762 ret->exitCode = exitkind_makeConditional (tclause->exitCode);
6763 ret->canBreak = tclause->canBreak;
6764 ret->sets = sRefSet_union (ret->sets, tclause->sets);
6765 ret->msets = sRefSet_union (ret->msets, tclause->msets);
6766 ret->uses = sRefSet_union (ret->uses, tclause->uses);
6769 ret->mustBreak = FALSE;
6774 exprNode exprNode_ifelse (/*@only@*/ exprNode pred,
6775 /*@only@*/ exprNode tclause,
6776 /*@only@*/ exprNode eclause)
6779 bool tEmptyErr = FALSE;
6780 bool eEmptyErr = FALSE;
6782 if (context_maybeSet (FLG_IFEMPTY))
6784 if (exprNode_isEmptyStatement (tclause))
6786 tEmptyErr = optgenerror
6789 ("Body of if clause of if statement is empty"),
6790 exprNode_loc (tclause));
6793 if (exprNode_isEmptyStatement (eclause))
6795 eEmptyErr = optgenerror
6798 ("Body of else clause of if statement is empty"),
6799 exprNode_loc (eclause));
6803 if (context_maybeSet (FLG_IFBLOCK))
6806 && exprNode_isDefined (tclause)
6807 && !exprNode_isBlock (tclause))
6809 voptgenerror (FLG_IFBLOCK,
6811 ("Body of if clause of if statement is not a block: %s",
6812 exprNode_unparse (tclause)),
6813 exprNode_loc (tclause));
6817 && exprNode_isDefined (eclause)
6818 && !exprNode_isBlock (eclause)
6819 && !(eclause->kind == XPR_IF)
6820 && !(eclause->kind == XPR_IFELSE))
6825 ("Body of else clause of if statement is not a block: %s",
6826 exprNode_unparse (eclause)),
6827 exprNode_loc (eclause));
6831 if (context_maybeSet (FLG_ELSEIFCOMPLETE))
6833 if (exprNode_isDefined (eclause)
6834 && (eclause->kind == XPR_IF))
6836 voptgenerror (FLG_ELSEIFCOMPLETE,
6837 message ("Incomplete else if logic (no final else): %s",
6838 exprNode_unparse (eclause)),
6839 exprNode_loc (eclause));
6843 if (exprNode_isError (pred))
6845 if (exprNode_isError (tclause))
6847 if (exprNode_isError (eclause))
6849 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
6853 ret = exprNode_createPartialCopy (eclause);
6858 ret = exprNode_createPartialCopy (tclause);
6861 else /* pred is okay */
6863 ret = exprNode_createPartialCopy (pred);
6865 if (exprNode_mustEscape (pred))
6869 message ("Predicate always exits: %s", exprNode_unparse (pred)),
6870 exprNode_loc (pred));
6873 exprNode_checkPred (cstring_makeLiteralTemp ("if"), pred);
6874 exprNode_checkUse (ret, pred->sref, pred->loc);
6876 exprNode_mergeCondUSs (ret, tclause, eclause);
6879 ret->kind = XPR_IFELSE;
6880 ret->edata = exprData_makeCond (pred, tclause, eclause);
6882 if (exprNode_isDefined (tclause) && exprNode_isDefined (eclause))
6884 exprNode_combineControl (ret, tclause, eclause);
6885 ret->loc = fileloc_update (ret->loc, eclause->loc);
6892 ** *allpaths <- TRUE iff all executions paths must go through the switch
6896 checkSwitchExpr (exprNode test, /*@dependent@*/ exprNode e, /*@out@*/ bool *allpaths)
6898 exprNodeSList el = exprNode_flatten (e);
6899 bool mustReturn = TRUE; /* find a branch that doesn't */
6900 bool thisReturn = FALSE;
6901 bool hasDefault = FALSE;
6902 bool hasAllMembers = FALSE;
6903 bool inSwitch = FALSE;
6904 bool isEnumSwitch = FALSE;
6905 bool canBreak = FALSE;
6906 bool fallThrough = FALSE;
6907 ctype ct = ctype_unknown;
6908 enumNameSList usedEnums;
6911 if (exprNode_isDefined (test))
6916 ttype = ctype_realType (ct);
6918 if (ctype_isEnum (ttype))
6920 isEnumSwitch = TRUE;
6921 enums = ctype_elist (ttype);
6922 usedEnums = enumNameSList_new ();
6926 exprNodeSList_elements (el, current)
6928 if (exprNode_isDefined (current))
6930 switch (current->kind)
6938 message ("Duplicate default cases in switch"),
6939 exprNode_loc (current));
6944 if (current->kind == XPR_DEFAULT || current->kind == XPR_FTDEFAULT)
6952 exprNode st = exprData_getSingle (current->edata);
6953 uentry ue = exprNode_getUentry (st);
6955 if (uentry_isValid (ue))
6957 cstring cname = uentry_rawName (ue);
6959 if (enumNameList_member (/*@-usedef@*/enums/*@=usedef@*/, cname))
6961 if (enumNameSList_member
6962 (/*@-usedef@*/usedEnums/*@=usedef@*/, cname))
6966 message ("Duplicate case in switch: %s",
6972 enumNameSList_addh (usedEnums, cname);
6979 message ("Case in switch not %s member: %s",
6980 ctype_unparse (ct), cname),
6987 if (inSwitch && !fallThrough)
6989 if (!thisReturn || canBreak)
6999 /*@switchbreak@*/ break;
7001 thisReturn = thisReturn || exprNode_mustEscape (current);
7002 canBreak = canBreak || current->canBreak;
7003 if (canBreak) fallThrough = FALSE;
7006 } end_exprNodeSList_elements;
7008 if (inSwitch) /* check the last one! */
7010 if (!thisReturn || canBreak)
7019 && (enumNameSList_size (/*@-usedef@*/usedEnums/*@=usedef@*/) !=
7020 enumNameList_size (/*@-usedef@*/enums/*@=usedef@*/)))
7022 enumNameSList unused = enumNameSList_subtract (enums, usedEnums);
7024 voptgenerror (FLG_MISSCASE,
7025 message ("Missing case%s in switch: %q",
7026 cstring_makeLiteralTemp
7027 ((enumNameSList_size (unused) > 1) ? "s" : ""),
7028 enumNameSList_unparse (unused)),
7031 enumNameSList_free (unused);
7035 hasAllMembers = TRUE;
7039 enumNameSList_free (usedEnums);
7043 *allpaths = hasDefault;
7046 exprNodeSList_free (el);
7047 return ((hasDefault || hasAllMembers) && mustReturn);
7050 exprNode exprNode_switch (/*@only@*/ exprNode e, /*@only@*/ exprNode s)
7052 exprNode ret = exprNode_createPartialCopy (e);
7055 DPRINTF (("Switch: %s", exprNode_unparse (s)));
7057 ret->kind = XPR_SWITCH;
7058 ret->edata = exprData_makePair (e, s);
7060 if (!exprNode_isError (s))
7062 exprNode fs = exprNode_firstStatement (s);
7063 ret->loc = fileloc_update (ret->loc, s->loc);
7065 if (exprNode_isUndefined (fs)
7066 || exprNode_isCaseMarker (fs) || exprNode_isLabelMarker (fs)
7067 || exprNode_isDefaultMarker (fs)) {
7070 voptgenerror (FLG_FIRSTCASE,
7072 ("Statement after switch is not a case: %s", exprNode_unparse (fs)),
7077 if (!exprNode_isError (e))
7079 if (checkSwitchExpr (e, s, &allpaths))
7081 ret->exitCode = XK_MUSTRETURN;
7085 ret->exitCode = e->exitCode;
7088 ret->canBreak = e->canBreak;
7089 ret->mustBreak = e->mustBreak;
7093 ** exprNode.c:3883,32: Variable allpaths used before definition
7100 DPRINTF (("Context exit switch!"));
7101 context_exitSwitch (ret, allpaths);
7102 DPRINTF (("Context exit switch done!"));
7107 static void checkInfiniteLoop (/*@notnull@*/ exprNode test,
7108 /*@notnull@*/ exprNode body)
7110 sRefSet tuses = test->uses;
7112 if (!sRefSet_isEmpty (test->uses))
7114 sRefSet sets = sRefSet_newCopy (body->sets);
7115 bool hasError = TRUE;
7116 bool innerState = FALSE;
7117 sRefSet tuncon = sRefSet_undefined;
7119 sets = sRefSet_union (sets, test->sets);
7120 sets = sRefSet_union (sets, body->msets);
7121 sets = sRefSet_union (sets, test->msets);
7123 sRefSet_allElements (tuses, el)
7125 if (sRef_isUnconstrained (el))
7127 tuncon = sRefSet_insert (tuncon, el);
7131 if (sRefSet_member (sets, el))
7138 if (sRef_isInternalState (el)
7139 || sRef_isFileStatic (sRef_getRootBase (el)))
7143 } end_sRefSet_allElements ;
7147 sRefSet suncon = sRefSet_undefined;
7148 bool sinner = FALSE;
7150 sRefSet_allElements (sets, el)
7152 if (sRef_isUnconstrained (el))
7154 suncon = sRefSet_insert (suncon, el);
7156 else if (sRef_isInternalState (el))
7164 } end_sRefSet_allElements ;
7166 if (sinner && innerState)
7170 else if (sRefSet_isEmpty (tuncon)
7171 && sRefSet_isEmpty (suncon))
7176 ("Suspected infinite loop. No value used in loop test (%q) "
7177 "is modified by test or loop body.",
7178 sRefSet_unparsePlain (tuses)),
7183 if (sRefSet_isEmpty (tuncon))
7187 message ("Suspected infinite loop. No condition values "
7188 "modified. Modification possible through "
7189 "unconstrained calls: %q",
7190 sRefSet_unparsePlain (suncon)),
7197 message ("Suspected infinite loop. No condition values "
7198 "modified. Possible undetected dependency through "
7199 "unconstrained calls in loop test: %q",
7200 sRefSet_unparsePlain (tuncon)),
7206 sRefSet_free (sets);
7210 exprNode exprNode_while (/*@keep@*/ exprNode t, /*@keep@*/ exprNode b)
7213 bool emptyErr = FALSE;
7215 if (context_maybeSet (FLG_WHILEEMPTY))
7217 if (exprNode_isEmptyStatement (b))
7219 emptyErr = optgenerror
7222 ("Body of while statement is empty"),
7227 if (!emptyErr && context_maybeSet (FLG_WHILEBLOCK))
7229 if (exprNode_isDefined (b)
7230 && !exprNode_isBlock (b))
7232 if (context_inIterDef ()
7233 && (b->kind == XPR_STMTLIST
7234 || b->kind == XPR_TOK))
7240 voptgenerror (FLG_WHILEBLOCK,
7242 ("Body of while statement is not a block: %s",
7243 exprNode_unparse (b)),
7249 if (exprNode_isError (t))
7251 if (exprNode_isError (b))
7253 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
7257 ret = exprNode_createPartialCopy (b);
7264 ret = exprNode_createPartialCopy (t);
7266 llassert (t->kind == XPR_WHILEPRED);
7268 test = exprData_getSingle (t->edata);
7270 if (!exprNode_isError (b) && exprNode_isDefined (test))
7272 if (context_maybeSet (FLG_INFLOOPS)
7273 || context_maybeSet (FLG_INFLOOPSUNCON))
7276 ** check that some variable in the predicate is set by the body
7277 ** if the predicate uses any variables
7280 checkInfiniteLoop (test, b);
7283 exprNode_mergeUSs (ret, b);
7285 if (exprNode_isDefined (b))
7287 ret->exitCode = exitkind_makeConditional (b->exitCode);
7292 ret->edata = exprData_makePair (t, b);
7293 ret->kind = XPR_WHILE;
7295 if (exprNode_isDefined (t) && exprNode_mustEscape (t))
7299 message ("Predicate always exits: %s", exprNode_unparse (t)),
7303 ret->exitCode = XK_NEVERESCAPE;
7306 ** If loop is infinite, and there is no break inside,
7307 ** exit code is never reach.
7310 if (exprNode_knownIntValue (t))
7312 if (!exprNode_isZero (t))
7314 if (exprNode_isDefined (b))
7318 /* Really, it means never reached. */
7319 ret->exitCode = XK_MUSTEXIT;
7329 ret->canBreak = FALSE;
7330 ret->mustBreak = FALSE;
7336 ** do { b } while (t);
7338 ** note: body passed as first argument
7341 exprNode exprNode_doWhile (/*@only@*/ exprNode b, /*@only@*/ exprNode t)
7345 DPRINTF (("Do while: %s / %s",
7346 exprNode_unparse (b), exprNode_unparse (t)));
7348 if (exprNode_isError (t))
7350 if (exprNode_isError (b))
7352 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
7356 ret = exprNode_createPartialCopy (b);
7358 ret->exitCode = exitkind_makeConditional (b->exitCode);
7359 exprNode_checkUse (ret, b->sref, b->loc);
7360 ret->exitCode = b->exitCode;
7361 ret->canBreak = b->canBreak;
7362 ret->mustBreak = FALSE;
7367 DPRINTF (("Do while: %s / %s",
7368 exitkind_unparse (t->exitCode),
7369 exitkind_unparse (b->exitCode)));
7371 ret = exprNode_createPartialCopy (t);
7372 exprNode_checkPred (cstring_makeLiteralTemp ("while"), t);
7374 if (!exprNode_isError (b))
7377 ** forgot the copy's --- why wasn't this detected??
7380 ret->sets = sRefSet_copyInto (ret->sets, b->sets);
7381 ret->msets = sRefSet_copyInto (ret->msets, b->msets);
7382 ret->uses = sRefSet_copyInto (ret->uses, b->uses);
7384 /* left this out --- causes and aliasing bug (infinite loop)
7385 should be detected?? */
7387 exprNode_checkUse (ret, b->sref, b->loc);
7388 exprNode_mergeUSs (ret, t);
7389 exprNode_checkUse (ret, t->sref, t->loc);
7391 /* evans 2001-10-05: while loop can break */
7392 ret->exitCode = exitkind_makeConditional (b->exitCode);
7394 DPRINTF (("Do while: %s",
7395 exitkind_unparse (ret->exitCode)));
7397 ret->canBreak = b->canBreak;
7399 /* Always FALSE for doWhile loops - break's when test is false */
7400 ret->mustBreak = FALSE; /* b->mustBreak; */
7404 context_exitDoWhileClause (t);
7406 ret->kind = XPR_DOWHILE;
7407 ret->edata = exprData_makePair (t, b);
7411 exprNode exprNode_for (/*@keep@*/ exprNode inc, /*@keep@*/ exprNode body)
7414 bool emptyErr = FALSE;
7416 if (context_maybeSet (FLG_FOREMPTY))
7418 if (exprNode_isEmptyStatement (body))
7420 emptyErr = optgenerror
7423 ("Body of for statement is empty"),
7424 exprNode_loc (body));
7428 if (!emptyErr && context_maybeSet (FLG_FORBLOCK))
7430 if (exprNode_isDefined (body)
7431 && !exprNode_isBlock (body))
7433 if (context_inIterDef ()
7434 && (body->kind == XPR_STMTLIST
7435 || body->kind == XPR_TOK))
7441 voptgenerror (FLG_FORBLOCK,
7443 ("Body of for statement is not a block: %s",
7444 exprNode_unparse (body)),
7445 exprNode_loc (body));
7451 ** for ud purposes: (alreadly) init -> test -> (now) LOOP: body + inc + test
7454 if (exprNode_isError (body))
7456 ret = exprNode_createPartialCopy (inc);
7460 ret = exprNode_createPartialCopy (body);
7462 ret->exitCode = exitkind_makeConditional (body->exitCode);
7464 exprNode_mergeUSs (inc, body);
7466 if (exprNode_isDefined (inc))
7470 context_setMessageAnnote (cstring_makeLiteral ("in post loop increment"));
7473 tmp = exprNode_effect (exprData_getTripleInc (inc->edata));
7474 exprNode_freeShallow (tmp);
7476 context_clearMessageAnnote ();
7477 context_setMessageAnnote (cstring_makeLiteral ("in post loop test"));
7479 tmp = exprNode_effect (exprData_getTripleTest (inc->edata));
7480 exprNode_freeShallow (tmp);
7482 context_clearMessageAnnote ();
7484 ret->uses = sRefSet_copyInto (ret->uses, inc->uses);
7485 ret->sets = sRefSet_copyInto (ret->sets, inc->sets);
7486 ret->msets = sRefSet_copyInto (ret->msets, inc->msets);
7490 ret->kind = XPR_FOR;
7491 ret->edata = exprData_makePair (inc, body);
7493 if (exprNode_isDefined (inc)) {
7494 exprNode test = exprData_getTripleTest (inc->edata);
7496 if (exprNode_isUndefined (test)) {
7497 if (exprNode_isDefined (body)) {
7498 if (!body->canBreak) {
7499 /* Really, it means never reached. */
7500 ret->exitCode = XK_MUSTEXIT;
7510 ** for (init; test; inc)
7513 ** while (test) { body; inc; }
7515 ** Now: check use of init (may set vars for test)
7516 ** check use of test
7520 /*@observer@*/ guardSet exprNode_getForGuards (exprNode pred)
7524 if (exprNode_isError (pred)) return guardSet_undefined;
7526 llassert (pred->kind == XPR_FORPRED);
7528 test = exprData_getTripleTest (pred->edata);
7530 if (!exprNode_isError (test))
7532 return (test->guards);
7535 return guardSet_undefined;
7538 exprNode exprNode_whilePred (/*@only@*/ exprNode test)
7540 exprNode ret = exprNode_createSemiCopy (test);
7542 if (exprNode_isDefined (test))
7544 exprNode_copySets (ret, test);
7545 exprNode_checkPred (cstring_makeLiteralTemp ("while"), test);
7546 exprNode_checkUse (ret, test->sref, test->loc);
7548 exprNode_produceGuards (test);
7550 ret->guards = guardSet_copy (test->guards);
7553 ret->edata = exprData_makeSingle (test);
7554 ret->kind = XPR_WHILEPRED;
7558 exprNode exprNode_forPred (/*@only@*/ exprNode init, /*@only@*/ exprNode test,
7559 /*@only@*/ exprNode inc)
7564 ** for ud purposes: init -> test -> LOOP: [ body, inc ]
7567 exprNode_checkPred (cstring_makeLiteralTemp ("for"), test);
7569 if (!exprNode_isError (inc))
7571 ret = exprNode_createPartialCopy (inc);
7575 if (!exprNode_isError (init))
7577 ret = exprNode_createPartialCopy (init);
7579 else if (!exprNode_isError (test))
7581 ret = exprNode_createPartialCopy (test);
7585 ret = exprNode_createUnknown ();
7589 exprNode_mergeUSs (ret, init);
7591 if (exprNode_isDefined (init))
7593 exprNode_checkUse (ret, init->sref, init->loc);
7596 exprNode_mergeUSs (ret, test);
7598 if (exprNode_isDefined (test))
7600 exprNode_checkUse (ret, test->sref, test->loc);
7603 ret->kind = XPR_FORPRED;
7604 ret->edata = exprData_makeFor (init, test, inc);
7608 /*@notnull@*/ /*@only@*/ exprNode exprNode_goto (/*@only@*/ cstring label)
7610 exprNode ret = exprNode_createUnknown ();
7612 if (context_inMacro ())
7614 voptgenerror (FLG_MACROSTMT,
7615 message ("Macro %s uses goto (not functional)",
7616 context_inFunctionName ()),
7620 ret->kind = XPR_GOTO;
7621 ret->edata = exprData_makeLiteral (label);
7622 ret->mustBreak = TRUE;
7623 ret->exitCode = XK_GOTO;
7624 ret->canBreak = TRUE;
7628 exprNode exprNode_continue (/*@only@*/ lltok l, int qcontinue)
7630 exprNode ret = exprNode_createLoc (ctype_unknown, fileloc_copy (lltok_getLoc (l)));
7632 ret->kind = XPR_CONTINUE;
7633 ret->edata = exprData_makeTok (l);
7634 ret->canBreak = TRUE;
7635 ret->mustBreak = TRUE;
7637 if (qcontinue == QSAFEBREAK)
7641 else if (qcontinue == QINNERCONTINUE)
7643 if (!context_inDeepLoop ())
7646 (FLG_LOOPLOOPCONTINUE,
7647 cstring_makeLiteral ("Continue statement marked with innercontinue "
7648 "is not inside a nested loop"),
7649 exprNode_loc (ret));
7652 else if (qcontinue == BADTOK)
7654 if (context_inDeepLoop ())
7657 (FLG_LOOPLOOPCONTINUE,
7658 cstring_makeLiteral ("Continue statement in nested loop"),
7659 exprNode_loc (ret));
7664 llbuglit ("exprNode_continue: bad qcontinue");
7670 exprNode exprNode_break (/*@only@*/ lltok l, int bqual)
7672 exprNode ret = exprNode_createLoc (ctype_unknown, fileloc_copy (lltok_getLoc (l)));
7673 clause breakClause = context_breakClause ();
7675 ret->kind = XPR_BREAK;
7676 ret->edata = exprData_makeTok (l);
7677 ret->canBreak = TRUE;
7678 ret->mustBreak = TRUE;
7680 if (breakClause == NOCLAUSE)
7684 cstring_makeLiteral ("Break not inside while, for or switch statement"),
7685 exprNode_loc (ret));
7689 if (bqual != BADTOK)
7696 if (breakClause == SWITCHCLAUSE)
7698 if (!context_inDeepSwitch ())
7700 voptgenerror (FLG_SYNTAX,
7702 ("Break preceded by innerbreak is not in a deep switch"),
7703 exprNode_loc (ret));
7708 if (!context_inDeepLoop ())
7710 voptgenerror (FLG_SYNTAX,
7712 ("Break preceded by innerbreak is not in a deep loop"),
7713 exprNode_loc (ret));
7718 if (breakClause == SWITCHCLAUSE)
7720 voptgenerror (FLG_SYNTAX,
7722 ("Break preceded by loopbreak is breaking a switch"),
7723 exprNode_loc (ret));
7727 if (breakClause != SWITCHCLAUSE)
7731 message ("Break preceded by switchbreak is breaking %s",
7732 cstring_makeLiteralTemp
7733 ((breakClause == WHILECLAUSE
7734 || breakClause == DOWHILECLAUSE) ? "a while loop"
7735 : (breakClause == FORCLAUSE) ? "a for loop"
7736 : (breakClause == ITERCLAUSE) ? "an iterator"
7738 exprNode_loc (ret));
7746 if (breakClause == SWITCHCLAUSE)
7748 clause nextBreakClause = context_nextBreakClause ();
7750 switch (nextBreakClause)
7752 case NOCLAUSE: break;
7758 (FLG_LOOPSWITCHBREAK,
7759 cstring_makeLiteral ("Break statement in switch inside loop"),
7760 exprNode_loc (ret));
7764 (FLG_SWITCHSWITCHBREAK,
7765 cstring_makeLiteral ("Break statement in switch inside switch"),
7766 exprNode_loc (ret));
7773 if (context_inDeepLoop ())
7777 cstring_makeLiteral ("Break statement in nested loop"),
7778 exprNode_loc (ret));
7782 if (context_inDeepLoopSwitch ())
7785 (FLG_SWITCHLOOPBREAK,
7786 cstring_makeLiteral ("Break statement in loop inside switch"),
7787 exprNode_loc (ret));
7797 exprNode exprNode_nullReturn (/*@only@*/ lltok t)
7799 fileloc loc = lltok_getLoc (t);
7800 exprNode ret = exprNode_createLoc (ctype_unknown, fileloc_copy (loc));
7802 context_returnFunction ();
7803 exprChecks_checkNullReturn (loc);
7805 ret->kind = XPR_NULLRETURN;
7806 ret->edata = exprData_makeTok (t);
7807 ret->exitCode = XK_MUSTRETURN;
7811 exprNode exprNode_return (/*@only@*/ exprNode e)
7815 if (exprNode_isError (e))
7817 ret = exprNode_createUnknown ();
7821 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (e->loc));
7823 exprNode_checkUse (ret, e->sref, e->loc);
7824 exprNode_checkReturn (e);
7827 context_returnFunction ();
7828 ret->kind = XPR_RETURN;
7829 ret->edata = exprData_makeSingle (e);
7830 ret->exitCode = XK_MUSTRETURN;
7835 exprNode exprNode_comma (/*@only@*/ exprNode e1, /*@only@*/ exprNode e2)
7839 if (exprNode_isError (e1))
7841 if (exprNode_isError (e2))
7843 ret = exprNode_createLoc (ctype_unknown, fileloc_copy (g_currentloc));
7847 ret = exprNode_createPartialCopy (e2);
7848 exprNode_checkUse (ret, e2->sref, e2->loc);
7849 ret->sref = e2->sref;
7854 ret = exprNode_createPartialCopy (e1);
7856 exprNode_checkUse (ret, e1->sref, e1->loc);
7858 if (!exprNode_isError (e2))
7860 exprNode_mergeUSs (ret, e2);
7861 exprNode_checkUse (ret, e2->sref, e2->loc);
7862 ret->sref = e2->sref;
7866 ret->kind = XPR_COMMA;
7867 ret->edata = exprData_makePair (e1, e2);
7869 if (exprNode_isDefined (e1))
7871 if (exprNode_isDefined (e2))
7875 if (exprNode_mustEscape (e1) || e1->mustBreak)
7879 message ("Second clause of comma expression is unreachable: %s",
7880 exprNode_unparse (e2)),
7884 ret->exitCode = exitkind_combine (e1->exitCode, e2->exitCode);
7885 ret->mustBreak = e1->mustBreak || e2->mustBreak;
7886 ret->canBreak = e1->canBreak || e2->canBreak;
7890 if (exprNode_mustEscape (e1) || e1->mustBreak)
7894 message ("Second clause of comma expression is unreachable: %s",
7895 exprNode_unparse (e2)),
7899 ret->exitCode = e1->exitCode;
7900 ret->canBreak = e1->canBreak;
7905 if (exprNode_isDefined (e2))
7907 ret->exitCode = e2->exitCode;
7908 ret->mustBreak = e2->mustBreak;
7909 ret->canBreak = e2->canBreak;
7916 static bool exprNode_checkOneInit (/*@notnull@*/ exprNode el, exprNode val)
7918 ctype t1 = exprNode_getType (el);
7919 ctype t2 = exprNode_getType (val);
7920 bool hasError = FALSE;
7922 DPRINTF (("Check one init: %s / %s",
7923 exprNode_unparse (el),
7924 exprNode_unparse (val)));
7926 if (ctype_isUnknown (t1))
7928 voptgenerror (FLG_IMPTYPE,
7929 message ("Variable has unknown (implicitly int) type: %s",
7930 exprNode_unparse (el)),
7934 el->typ = ctype_int;
7937 if (exprNode_isDefined (val) && val->kind == XPR_INITBLOCK)
7939 exprNodeList vals = exprData_getArgs (val->edata);
7941 DPRINTF (("Check one init: %s", exprNodeList_unparse (vals)));
7942 DPRINTF (("Type: %s", ctype_unparse (t1)));
7944 if (ctype_isRealAP (t1))
7949 if (ctype_isFixedArray (t1))
7951 int nelements = long_toInt (ctype_getArraySize (t1));
7953 if (exprNode_isStringLiteral (val))
7955 exprNode_checkStringLiteralLength (t1, val);
7959 if (exprNodeList_size (vals) != nelements)
7961 hasError = optgenerror
7962 (exprNodeList_size (vals) > nelements ? FLG_INITSIZE : FLG_INITALLELEMENTS,
7963 message ("Initializer block for "
7964 "%s has %d element%&, but declared as %s: %q",
7965 exprNode_unparse (el),
7966 exprNodeList_size (vals),
7968 exprNodeList_unparse (vals)),
7974 exprNodeList_elements (vals, oneval)
7976 cstring istring = message ("%d", i);
7979 (exprNode_fakeCopy (el),
7980 exprNode_numLiteral (ctype_int, istring,
7981 fileloc_copy (el->loc), i));
7983 if (exprNode_isDefined (newel))
7985 if (exprNodeList_size (vals) == 1
7986 && ctype_isString (exprNode_getType (oneval))
7987 && ctype_isChar (exprNode_getType (newel)))
7989 exprNode_freeIniter (newel);
7993 if (exprNode_checkOneInit (newel, oneval))
7998 if (nerrors > 3 && exprNodeList_size (vals) > 6)
8001 (message ("Additional initialization errors "
8002 "for %s not reported",
8003 exprNode_unparse (el)),
8005 exprNode_freeIniter (newel);
8010 exprNode_freeIniter (newel);
8015 exprNode_freeIniter (newel);
8020 cstring_free (istring);
8023 } end_exprNodeList_elements;
8026 else if (ctype_isStruct (ctype_realType (t1)))
8028 uentryList fields = ctype_getFields (t1);
8031 if (uentryList_size (fields) != exprNodeList_size (vals))
8033 if (uentryList_size (fields) > exprNodeList_size (vals))
8035 hasError = optgenerror
8037 message ("Initializer block for "
8038 "%s has %d field%&, but %s has %d field%&: %q",
8039 exprNode_unparse (el),
8040 exprNodeList_size (vals),
8042 uentryList_size (fields),
8043 exprNodeList_unparse (vals)),
8048 hasError = optgenerror
8050 message ("Initializer block for "
8051 "%s has %d field%&, but %s has %d field%&: %q",
8052 exprNode_unparse (el),
8053 exprNodeList_size (vals),
8055 uentryList_size (fields),
8056 exprNodeList_unparse (vals)),
8062 exprNodeList_elements (vals, oneval)
8064 uentry thisfield = uentryList_getN (fields, i);
8066 exprNode_fieldAccessAux (exprNode_fakeCopy (el),
8068 uentry_getName (thisfield));
8070 if (exprNode_isDefined (newel))
8072 if (exprNode_checkOneInit (newel, oneval))
8077 exprNode_freeIniter (newel);
8081 } end_exprNodeList_elements;
8084 /* evans 2001-12-30: added to fix bug reported by Jim Zelenka */
8085 else if (ctype_isUnion (ctype_realType (t1)))
8087 uentryList fields = ctype_getFields (t1);
8091 ** Union initializers set the first member always.
8094 DPRINTF (("Union initializer: %s / %s",
8095 exprNode_unparse (el), ctype_unparse (ctype_realType (t1))));
8097 if (exprNodeList_size (vals) != 1)
8099 hasError = optgenerror
8101 message ("Initializer block for union "
8102 "%s has %d elements, union initializers should have one element: %q",
8103 exprNode_unparse (el),
8104 exprNodeList_size (vals),
8105 exprNodeList_unparse (vals)),
8110 exprNode oneval = exprNodeList_head (vals);
8111 uentry thisfield = uentryList_getN (fields, i);
8113 exprNode_fieldAccessAux (exprNode_fakeCopy (el),
8115 uentry_getName (thisfield));
8117 if (exprNode_isDefined (newel))
8119 if (exprNode_checkOneInit (newel, oneval))
8124 exprNode_freeIniter (newel);
8130 hasError = optgenerror
8132 message ("Initializer block used for "
8133 "%s where %t is expected: %s",
8134 exprNode_unparse (el), t1, exprNode_unparse (val)),
8140 if (exprNode_isDefined (val))
8142 doAssign (el, val, TRUE);
8144 if (!exprNode_matchType (t1, val))
8146 hasError = gentypeerror
8148 message ("Initial value of %s is type %t, "
8150 exprNode_unparse (el),
8151 t2, t1, exprNode_unparse (val)),
8161 exprNode_makeInitializationAux (/*@temp@*/ idDecl t)
8165 DPRINTF (("Initialization: %s", idDecl_unparse (t)));
8167 if (usymtab_exists (idDecl_observeId (t)))
8169 uentry ue = usymtab_lookup (idDecl_observeId (t));
8170 ret = exprNode_createId (ue);
8172 /*@i723 don't do this...but why? */
8174 ct = ctype_realishType (ret->typ);
8176 DPRINTF (("Type: %s", ctype_unparse (ret->typ)));
8178 if (ctype_isUnknown (ct))
8180 if (uentry_isAnyTag (ue))
8184 message ("%s used but not previously declared: %s",
8185 uentry_ekindName (ue),
8186 idDecl_getName (t)),
8194 message ("Variable has unknown (implicitly int) type: %s",
8195 idDecl_getName (t)),
8207 DPRINTF (("Unrecognized: %s", idDecl_unparse (t)));
8209 ue = uentry_makeUnrecognized (idDecl_observeId (t), fileloc_copy (g_currentloc));
8210 /*!! fileloc_copy (g_currentloc)); */
8211 /*@i32!!! should get error without this */
8213 ret = exprNode_fromIdentifierAux (ue);
8216 ** No error - this happens in old style declarations:
8220 message ("Unrecognized identifier in intializer: %s", idDecl_observeId (t)),
8227 exprData_free (ret->edata, ret->kind);
8228 ret->edata = exprData_undefined;
8230 ret->exitCode = XK_NEVERESCAPE;
8231 ret->mustBreak = FALSE;
8232 ret->kind = XPR_INIT;
8236 exprNode exprNode_makeEmptyInitialization (/*@only@*/ idDecl t)
8238 exprNode ret = exprNode_makeInitializationAux (t);
8239 llassert (ret->edata == exprData_undefined);
8240 ret->edata = exprData_makeInit (t, exprNode_undefined);
8244 exprNode exprNode_makeInitialization (/*@only@*/ idDecl t,
8245 /*@only@*/ exprNode e)
8247 uentry ue = usymtab_lookup (idDecl_observeId (t));
8248 exprNode ret = exprNode_makeInitializationAux (t);
8249 fileloc loc = exprNode_loc (e);
8251 if (exprNode_isError (e))
8253 e = exprNode_createUnknown ();
8256 /* error: assume initializer is defined */
8257 sRef_setDefined (ret->sref, g_currentloc);
8261 ctype ct = ctype_realishType (ret->typ);
8266 ** was addSafeUse --- what's the problem?
8268 ** int x = 3, y = x ?
8271 exprData_free (ret->edata, ret->kind);
8272 ret->edata = exprData_makeInit (t, e);
8274 exprNode_checkUse (ret, e->sref, e->loc);
8276 if (ctype_isUnknown (e->typ) && uentry_isValid (ue))
8278 exprNode lhs = exprNode_createId (ue);
8281 ** static storage should be undefined before initializing
8284 if (uentry_isStatic (ue))
8286 sRef_setDefState (lhs->sref, SS_PARTIAL, fileloc_undefined);
8289 (void) exprNode_checkOneInit (lhs, e);
8291 if (uentry_isStatic (ue))
8293 sRef_setDefState (lhs->sref, SS_DEFINED, fileloc_undefined);
8296 exprNode_free (lhs);
8300 if (!exprNode_matchType (ct, e))
8302 if (exprNode_isZero (e) && ctype_isArrayPtr (ct))
8309 (exprNode_getType (e), e, exprNode_getType (ret), ret,
8311 ("Variable %q initialized to type %t, expects %t: %s",
8312 uentry_getName (ue), exprNode_getType (e),
8313 exprNode_getType (ret),
8314 exprNode_unparse (e)),
8320 if (uentry_isStatic (ue))
8322 sRef_setDefState (ret->sref, SS_PARTIAL, fileloc_undefined);
8325 doAssign (ret, e, TRUE);
8327 if (uentry_isStatic (ue))
8329 sRef_setDefState (ret->sref, SS_DEFINED, fileloc_undefined);
8333 if (context_inIterDef ())
8335 /* should check if it is yield */
8336 uentry_setUsed (ue, loc);
8343 exprNode_mergeUSs (ret, e);
8347 exprNode exprNode_iter (/*@observer@*/ uentry name,
8348 /*@only@*/ exprNodeList alist,
8349 /*@only@*/ exprNode body,
8350 /*@observer@*/ uentry end)
8355 llassert (uentry_isValid (name));
8357 uentry_setUsed (name, exprNode_loc (body));
8359 ret = exprNode_createPartialCopy (body);
8360 iname = uentry_getName (name);
8362 if (uentry_isInvalid (end))
8365 message ("Iter %s not balanced with end_%s", iname, iname));
8369 cstring ename = uentry_getName (end);
8371 if (!cstring_equalPrefixLit (ename, "end_"))
8373 llerror (FLG_ITER, message ("Iter %s not balanced with end_%s: %s",
8374 iname, iname, ename));
8378 if (!cstring_equal (iname, cstring_suffix (ename, 4)))
8381 message ("Iter %s not balanced with end_%s: %s",
8382 iname, iname, ename));
8386 cstring_free (ename);
8389 context_exitIterClause (body);
8391 ret->kind = XPR_ITER;
8392 ret->edata = exprData_makeIter (name, alist, body, end);
8394 if (uentry_isIter (name))
8396 (void) checkArgsReal (name, body,
8397 uentry_getParams (name), alist, TRUE, ret);
8400 cstring_free (iname);
8406 exprNode_iterNewId (/*@only@*/ cstring s)
8408 exprNode e = exprNode_new ();
8409 uentry ue = uentryList_getN (uentry_getParams (getCurrentIter ()), iterParamNo ());
8411 llassert (processingIterVars ());
8413 e->loc = context_getSaveLocation ();
8415 if (fileloc_isUndefined (e->loc))
8417 fileloc_free (e->loc);
8418 e->loc = fileloc_copy (g_currentloc);
8421 e->uses = sRefSet_new ();
8422 e->sets = sRefSet_new ();
8423 e->msets = sRefSet_new ();
8425 e->val = multiVal_unknown ();
8426 e->guards = guardSet_new ();
8428 e->isJumpPoint = FALSE;
8429 e->exitCode = XK_NEVERESCAPE;
8431 /*> missing fields, detected by lclint <*/
8432 e->canBreak = FALSE;
8433 e->mustBreak = FALSE;
8434 e->etext = cstring_undefined;
8436 if (uentry_isYield (ue))
8438 uentry uue = uentry_makeVariable (s, uentry_getType (ue),
8439 fileloc_copy (e->loc),
8443 uue = usymtab_supEntrySrefReturn (uue);
8445 sr = uentry_getSref (uue);
8446 sRef_mergeStateQuiet (sr, uentry_getSref (ue));
8447 sr = uentry_getSref (uue);
8448 sRef_setDefined (sr, e->loc);
8450 e->typ = uentry_getType (uue);
8452 e->edata = exprData_makeId (uue);
8453 uentry_setUsed (uue, g_currentloc);
8459 sRef_setGlobalScope ();
8460 uue = uentry_makeVariableLoc (s, ctype_unknown);
8462 e->typ = ctype_unknown;
8463 e->edata = exprData_makeId (uue);
8465 uentry_setUsed (uue, e->loc);
8466 uentry_setHasNameError (uue);
8468 if (context_getFlag (FLG_REPEATUNRECOG))
8470 uentry_markOwned (uue);
8474 usymtab_supGlobalEntry (uue);
8477 sRef_clearGlobalScope ();
8479 voptgenerror (FLG_UNRECOG, message ("Unrecognized identifier: %s", s),
8486 exprNode_defineConstraints(e);
8491 exprNode_iterExpr (/*@returned@*/ exprNode e)
8493 if (!processingIterVars ())
8495 llcontbuglit ("checkIterParam: not in iter");
8499 if (uentry_isYield (uentryList_getN (uentry_getParams (getCurrentIter ()),
8502 if (exprNode_isDefined (e))
8504 if (fileloc_isDefined (e->loc))
8508 message ("Yield parameter is not simple identifier: %s",
8509 exprNode_unparse (e)),
8516 message ("Yield parameter is not simple identifier: %s",
8517 exprNode_unparse (e)),
8527 exprNode_iterId (/*@observer@*/ uentry c)
8531 llassert (processingIterVars ());
8533 ue = uentryList_getN (uentry_getParams (getCurrentIter ()),
8536 if (uentry_isYield (ue))
8538 ctype ct = uentry_getType (ue);
8539 exprNode e = exprNode_createPlain (ct);
8540 cstring name = uentry_getName (c);
8541 uentry le = uentry_makeVariable (name, ct, fileloc_undefined, FALSE);
8543 uentry_setUsed (ue, g_currentloc);
8544 uentry_setHasNameError (ue);
8546 cstring_free (name);
8549 e->edata = exprData_makeId (le);
8550 e->loc = context_getSaveLocation ();
8551 e->sref = uentry_getSref (le);
8553 usymtab_supEntrySref (le);
8555 if (!context_inHeader ())
8559 message ("Yield parameter shadows local declaration: %q",
8560 uentry_getName (c)),
8561 fileloc_isDefined (e->loc) ? e->loc : g_currentloc))
8563 uentry_showWhereDeclared (c);
8570 return (exprNode_fromIdentifierAux (c));
8573 exprNode exprNode_iterStart (/*@observer@*/ uentry name, /*@only@*/ exprNodeList alist)
8575 exprNode ret = exprNode_create (ctype_unknown);
8577 ret->kind = XPR_ITERCALL;
8578 ret->edata = exprData_makeIterCall (name, alist);
8580 if (uentry_isIter (name))
8582 uentryList params = uentry_getParams (name);
8584 if (context_inIterDef ()
8585 && uentryList_size (params) == exprNodeList_size (alist))
8589 exprNodeList_elements (alist, arg)
8591 uentry parg = uentryList_getN (params, i);
8593 if (uentry_isYield (parg))
8595 uentry ue = exprNode_getUentry (arg);
8597 if (uentry_isValid (ue))
8604 } end_exprNodeList_elements;
8607 (void) checkArgsReal (name, ret, params, alist, TRUE, ret);
8608 checkUnspecCall (ret, params, alist);
8614 /*@exposed@*/ sRef exprNode_getSref (exprNode e)
8616 if (exprNode_isDefined (e))
8619 if (e->sref == defref) /*@noaccess sRef@*/
8622 e->sref = sRef_makeUnknown ();
8623 sRef_setAliasKind (e->sref, AK_ERROR, fileloc_undefined);
8634 return sRef_undefined;
8638 /*@observer@*/ cstring
8639 exprNode_unparseFirst (exprNode e)
8641 if (exprNode_isDefined (e))
8645 if (e->kind == XPR_STMTLIST
8646 || e->kind == XPR_COMMA || e->kind == XPR_COND)
8648 exprNode first = exprData_getPairA (e->edata);
8650 if (exprNode_isDefined (first))
8652 return (exprNode_unparseFirst (exprData_getPairA (e->edata)));
8656 return (cstring_makeLiteralTemp ("..."));
8660 ret = cstring_elide (exprNode_unparse (e), 20);
8661 cstring_markOwned (ret);
8667 return cstring_makeLiteralTemp ("<error>");
8671 /*@observer@*/ cstring
8672 exprNode_unparse (/*@temp@*/ exprNode e)
8674 if (exprNode_isError (e))
8676 return cstring_makeLiteralTemp ("<error>");
8679 if (cstring_isDefined (e->etext))
8685 cstring ret = exprNode_doUnparse (e);
8687 /*@-modifies@*/ /* benevolent */
8694 /*@observer@*/ fileloc
8695 exprNode_loc (exprNode e)
8697 if (exprNode_isError (e))
8699 return (g_currentloc);
8708 ** executes exprNode e
8709 ** recursively rexecutes as though in original parse using
8710 ** information in e->edata
8713 static /*@only@*/ exprNodeList exprNodeList_effect (exprNodeList e)
8715 exprNodeList ret = exprNodeList_new ();
8717 exprNodeList_elements (e, current)
8719 exprNodeList_addh (ret, exprNode_effect (current));
8720 } end_exprNodeList_elements;
8725 static /*@only@*/ exprNode exprNode_effect (exprNode e)
8726 /*@globals internalState@*/
8728 bool innerEffect = inEffect;
8734 context_clearJustPopped ();
8736 if (exprNode_isError (e))
8738 ret = exprNode_undefined;
8743 ** Turn off expose and dependent transfer checking.
8744 ** Need to pass exposed internal nodes,
8745 ** [ copying would be a waste! ]
8746 ** [ Actually, I think I wasted a lot more time than its worth ]
8747 ** [ trying to do this. ]
8751 /*@-observertrans@*/
8752 /*@-dependenttrans@*/
8759 ret = exprNode_addParens (exprData_getUopTok (data),
8760 exprNode_effect (exprData_getUopNode (data)));
8763 ret = exprNode_assign (exprNode_effect (exprData_getOpA (data)),
8764 exprNode_effect (exprData_getOpB (data)),
8765 exprData_getOpTok (data));
8768 ret = exprNode_undefined;
8771 ret = exprNode_functionCall (exprNode_effect (exprData_getFcn (data)),
8772 exprNodeList_effect (exprData_getArgs (data)));
8785 cstring id = exprData_getId (data);
8786 uentry ue = usymtab_lookupSafe (id);
8788 ret = exprNode_fromIdentifierAux (ue);
8789 ret->loc = fileloc_update (ret->loc, e->loc);
8796 ret = exprNode_arrayFetch (exprNode_effect (exprData_getPairA (data)),
8797 exprNode_effect (exprData_getPairB (data)));
8800 ret = exprNode_op (exprNode_effect (exprData_getOpA (data)),
8801 exprNode_effect (exprData_getOpB (data)),
8802 exprData_getOpTok (data));
8806 ret = exprNode_postOp (exprNode_effect (exprData_getUopNode (data)),
8807 exprData_getUopTok (data));
8810 ret = exprNode_preOp (exprNode_effect (exprData_getUopNode (data)),
8811 exprData_getUopTok (data));
8823 ret = exprNode_vaArg (exprData_getCastTok (data),
8824 exprNode_effect (exprData_getCastNode (data)),
8825 exprData_getCastType (data));
8829 ret = exprNode_cast (exprData_getCastTok (data),
8830 exprNode_effect (exprData_getCastNode (data)),
8831 exprData_getCastType (data));
8834 ret = exprNode_iterStart (exprData_getIterCallIter (data),
8836 (exprData_getIterCallArgs (data)));
8840 ret = exprNode_iter (exprData_getIterSname (data),
8841 exprNodeList_effect (exprData_getIterAlist (data)),
8842 exprNode_effect (exprData_getIterBody (data)),
8843 exprData_getIterEname (data));
8847 ret = exprNode_for (exprNode_effect (exprData_getPairA (data)),
8848 exprNode_effect (exprData_getPairB (data)));
8852 ret = exprNode_forPred (exprNode_effect (exprData_getTripleInit (data)),
8853 exprNode_effect (exprData_getTripleTest (data)),
8854 exprNode_effect (exprData_getTripleInc (data)));
8858 ret = exprNode_createTok (exprData_getTok (data));
8862 ret = exprNode_goto (exprData_getLiteral (data));
8863 ret->loc = fileloc_update (ret->loc, e->loc);
8867 ret = exprNode_continue (exprData_getTok (data), QSAFEBREAK);
8871 ret = exprNode_break (exprData_getTok (data), QSAFEBREAK);
8875 ret = exprNode_return (exprNode_effect (exprData_getSingle (data)));
8878 case XPR_NULLRETURN:
8879 ret = exprNode_nullReturn (exprData_getTok (data));
8883 ret = exprNode_comma (exprNode_effect (exprData_getPairA (data)),
8884 exprNode_effect (exprData_getPairB (data)));
8888 ret = exprNode_cond (exprNode_effect (exprData_getTriplePred (data)),
8889 exprNode_effect (exprData_getTripleTrue (data)),
8890 exprNode_effect (exprData_getTripleFalse (data)));
8893 ret = exprNode_if (exprNode_effect (exprData_getPairA (data)),
8894 exprNode_effect (exprData_getPairB (data)));
8898 ret = exprNode_ifelse (exprNode_effect (exprData_getTriplePred (data)),
8899 exprNode_effect (exprData_getTripleTrue (data)),
8900 exprNode_effect (exprData_getTripleFalse (data)));
8903 ret = exprNode_whilePred (exprData_getSingle (data));
8907 ret = exprNode_while (exprNode_effect (exprData_getPairA (data)),
8908 exprNode_effect (exprData_getPairB (data)));
8912 ret = exprNode_doWhile (exprNode_effect (exprData_getPairA (data)),
8913 exprNode_effect (exprData_getPairB (data)));
8917 ret = exprNode_makeBlock (exprNode_effect (exprData_getSingle (data)));
8921 ret = exprNode_statement (exprNode_effect (exprData_getUopNode (data)),
8922 exprData_getUopTok (data));
8926 ret = exprNode_concat (exprNode_effect (exprData_getPairA (data)),
8927 exprNode_effect (exprData_getPairB (data)));
8932 ret = exprNode_caseMarker
8933 (exprNode_effect (exprData_getSingle (data)),
8939 ret = exprNode_createTok (exprData_getTok (data));
8943 ret = exprNode_switch (exprNode_effect (exprData_getPairA (data)),
8944 exprNode_effect (exprData_getPairB (data)));
8948 ret = exprNode_makeInitialization
8949 (exprData_getInitId (data),
8950 exprNode_effect (exprData_getInitNode (data)));
8954 ret = exprNode_fieldAccessAux
8955 (exprNode_effect (exprData_getFieldNode (data)),
8956 exprNode_loc (exprData_getFieldNode (data)),
8957 cstring_copy (exprData_getFieldName (data)));
8961 ret = exprNode_arrowAccessAux
8962 (exprNode_effect (exprData_getFieldNode (data)),
8963 exprNode_loc (exprData_getFieldNode (data)),
8964 cstring_copy (exprData_getFieldName (data)));
8967 case XPR_STRINGLITERAL:
8981 /*@=observertrans@*/
8983 /*@=dependenttrans@*/
8994 static /*@observer@*/ cstring exprNode_rootVarName (exprNode e)
8999 if (exprNode_isError (e))
9001 return cstring_undefined;
9009 ret = exprNode_rootVarName (exprData_getUopNode (data));
9012 ret = exprNode_rootVarName (exprData_getOpA (data));
9016 ret = exprData_getId (data);
9019 ret = idDecl_getName (exprData_getInitId (data));
9044 case XPR_NULLRETURN:
9066 case XPR_STRINGLITERAL:
9067 ret = cstring_undefined;
9074 static /*@only@*/ cstring exprNode_doUnparse (exprNode e)
9079 if (exprNode_isError (e))
9081 static /*@only@*/ cstring error = cstring_undefined;
9083 if (!cstring_isDefined (error))
9085 error = cstring_makeLiteral ("<error>");
9096 ret = message ("(%s)", exprNode_unparse (exprData_getUopNode (e->edata)));
9099 ret = message ("%s %s %s",
9100 exprNode_unparse (exprData_getOpA (data)),
9101 lltok_unparse (exprData_getOpTok (data)),
9102 exprNode_unparse (exprData_getOpB (data)));
9105 ret = message ("%s(%q)",
9106 exprNode_unparse (exprData_getFcn (data)),
9107 exprNodeList_unparse (exprData_getArgs (data)));
9110 ret = message ("{ %q }", exprNodeList_unparse (exprData_getArgs (data)));
9113 ret = cstring_undefined;
9116 ret = message ("%s:", exprData_getId (data));
9120 ret = cstring_copy (exprData_getId (data));
9123 ret = message ("%s[%s]", exprNode_unparse (exprData_getPairA (data)),
9124 exprNode_unparse (exprData_getPairB (data)));
9127 ret = message ("<body>");
9130 ret = message ("%s %s %s",
9131 exprNode_unparse (exprData_getOpA (data)),
9132 lltok_unparse (exprData_getOpTok (data)),
9133 exprNode_unparse (exprData_getOpB (data)));
9137 ret = message ("%s%s",
9138 lltok_unparse (exprData_getUopTok (data)),
9139 exprNode_unparse (exprData_getUopNode (data)));
9143 ret = message ("%s%s",
9144 exprNode_unparse (exprData_getUopNode (data)),
9145 lltok_unparse (exprData_getUopTok (data)));
9149 ret = message ("offsetof(%s,%q)",
9150 ctype_unparse (qtype_getType (exprData_getOffsetType (data))),
9151 cstringList_unparseSep (exprData_getOffsetName (data), cstring_makeLiteralTemp (".")));
9155 ret = message ("sizeof(%s)", ctype_unparse (qtype_getType (exprData_getType (data))));
9159 ret = message ("sizeof(%s)", exprNode_unparse (exprData_getSingle (data)));
9163 ret = message ("alignof(%s)", ctype_unparse (qtype_getType (exprData_getType (data))));
9167 ret = message ("alignof(%s)", exprNode_unparse (exprData_getSingle (data)));
9171 ret = message ("va_arg(%s, %q)",
9172 exprNode_unparse (exprData_getCastNode (data)),
9173 qtype_unparse (exprData_getCastType (data)));
9177 ret = message ("%q(%q)",
9178 uentry_getName (exprData_getIterCallIter (data)),
9179 exprNodeList_unparse (exprData_getIterCallArgs (data)));
9182 ret = message ("%q(%q) %s %q",
9183 uentry_getName (exprData_getIterSname (data)),
9184 exprNodeList_unparse (exprData_getIterAlist (data)),
9185 exprNode_unparse (exprData_getIterBody (data)),
9186 uentry_getName (exprData_getIterEname (data)));
9189 ret = message ("(%q)%s",
9190 qtype_unparse (exprData_getCastType (data)),
9191 exprNode_unparse (exprData_getCastNode (data)));
9195 ret = message ("%s %s",
9196 exprNode_unparse (exprData_getPairA (data)),
9197 exprNode_unparse (exprData_getPairB (data)));
9201 ret = message ("for (%s; %s; %s)",
9202 exprNode_unparse (exprData_getTripleInit (data)),
9203 exprNode_unparse (exprData_getTripleTest (data)),
9204 exprNode_unparse (exprData_getTripleInc (data)));
9208 ret = message ("goto %s", exprData_getLiteral (data));
9212 ret = cstring_makeLiteral ("continue");
9216 ret = cstring_makeLiteral ("break");
9220 ret = message ("return %s", exprNode_unparse (exprData_getSingle (data)));
9223 case XPR_NULLRETURN:
9224 ret = cstring_makeLiteral ("return");
9228 ret = message ("%s, %s",
9229 exprNode_unparse (exprData_getPairA (data)),
9230 exprNode_unparse (exprData_getPairB (data)));
9234 ret = message ("%s ? %s : %s",
9235 exprNode_unparse (exprData_getTriplePred (data)),
9236 exprNode_unparse (exprData_getTripleTrue (data)),
9237 exprNode_unparse (exprData_getTripleFalse (data)));
9240 ret = message ("if (%s) %s",
9241 exprNode_unparse (exprData_getPairA (data)),
9242 exprNode_unparse (exprData_getPairB (data)));
9246 ret = message ("if (%s) %s else %s",
9247 exprNode_unparse (exprData_getTriplePred (data)),
9248 exprNode_unparse (exprData_getTripleTrue (data)),
9249 exprNode_unparse (exprData_getTripleFalse (data)));
9252 ret = message ("while (%s) %s",
9253 exprNode_unparse (exprData_getPairA (data)),
9254 exprNode_unparse (exprData_getPairB (data)));
9258 ret = cstring_copy (exprNode_unparse (exprData_getSingle (data)));
9262 ret = cstring_copy (lltok_unparse (exprData_getTok (data)));
9266 ret = message ("do { %s } while (%s)",
9267 exprNode_unparse (exprData_getPairB (data)),
9268 exprNode_unparse (exprData_getPairA (data)));
9272 ret = message ("{ %s }", exprNode_unparseFirst (exprData_getSingle (data)));
9276 ret = cstring_copy (exprNode_unparse (exprData_getUopNode (data)));
9280 ret = message ("%s; %s",
9281 exprNode_unparse (exprData_getPairA (data)),
9282 exprNode_unparse (exprData_getPairB (data)));
9287 ret = cstring_makeLiteral ("default:");
9291 ret = message ("switch (%s) %s",
9292 exprNode_unparse (exprData_getPairA (data)),
9293 exprNode_unparse (exprData_getPairB (data)));
9298 ret = message ("case %s:",
9299 exprNode_unparse (exprData_getSingle (data)));
9303 if (exprNode_isError (exprData_getInitNode (data)))
9305 ret = message ("%q", idDecl_unparseC (exprData_getInitId (data)));
9309 ret = message ("%q = %s",
9310 idDecl_unparseC (exprData_getInitId (data)),
9311 exprNode_unparse (exprData_getInitNode (data)));
9316 ret = message ("%s.%s",
9317 exprNode_unparse (exprData_getFieldNode (data)),
9318 exprData_getFieldName (data));
9322 ret = message ("%s->%s",
9323 exprNode_unparse (exprData_getFieldNode (data)),
9324 exprData_getFieldName (data));
9327 case XPR_STRINGLITERAL:
9328 if (ctype_isWideString (e->typ))
9330 ret = message ("L\"%s\"", exprData_getLiteral (data));
9334 ret = message ("\"%s\"", exprData_getLiteral (data));
9339 ret = cstring_copy (exprData_getLiteral (data));
9343 ret = cstring_makeLiteral ("<node>");
9351 exprNode_isInitializer (exprNode e)
9353 return (exprNode_isDefined (e)
9354 && e->kind == XPR_INIT);
9358 exprNode_isCharLit (exprNode e)
9360 if (exprNode_isDefined (e))
9362 return (multiVal_isChar (exprNode_getValue (e)));
9371 exprNode_isNumLit (exprNode e)
9373 if (exprNode_isDefined (e))
9375 return (multiVal_isInt (exprNode_getValue (e)));
9384 exprNode_isFalseConstant (exprNode e)
9386 if (exprNode_isDefined (e))
9388 cstring s = exprNode_rootVarName (e);
9390 if (cstring_equal (s, context_getFalseName ()))
9400 exprNode_matchLiteral (ctype expected, exprNode e)
9402 if (exprNode_isDefined (e))
9404 multiVal m = exprNode_getValue (e);
9406 if (multiVal_isDefined (m))
9408 if (multiVal_isInt (m))
9410 long int val = multiVal_forceInt (m);
9412 if (ctype_isDirectBool (ctype_realishType (expected)))
9416 return FALSE; /* really?! return TRUE; allow use of 0 for FALSE */
9424 if (ctype_isRealInt (expected))
9427 ** unsigned <- [ constant >= 0 is okay ]
9430 if (ctype_isUnsigned (expected))
9439 ** No checks on sizes of integers...maybe add
9443 DPRINTF (("Here: %s => %s", exprNode_unparse (e), ctype_unparse (expected)));
9444 DPRINTF (("Type: %s / %s", ctype_unparse (exprNode_getType (e)),
9445 bool_unparse (ctype_isInt (exprNode_getType (e)))));
9447 if (context_getFlag (FLG_NUMLITERAL)
9448 && (ctype_isRegularInt (exprNode_getType (e)) || val == 0)) {
9454 return FALSE; /* evs 2000-05-17: previously, always returned TRUE */
9458 else if (ctype_isChar (expected))
9462 else if (ctype_isArrayPtr (expected))
9465 ** evans 2001-10-14: We allow 0 to match any pointer, but only if the type matches or is void *.
9470 if (ctype_match (exprNode_getType (e), expected)
9471 || ctype_isVoidPointer (exprNode_getType (e)))
9481 else if (ctype_isAnyFloat (expected))
9483 return (context_getFlag (FLG_NUMLITERAL));
9490 else if (multiVal_isDouble (m))
9492 if (ctype_isAnyFloat (expected))
9497 else if (multiVal_isChar (m))
9499 char val = multiVal_forceChar (m);
9501 if (ctype_isChar (expected))
9503 if (ctype_isUnsigned (expected) && ((int)val) < 0)
9524 exprNode_matchType (ctype expected, exprNode e)
9528 if (!exprNode_isDefined (e)) return TRUE;
9530 actual = ctype_realishType (exprNode_getType (e));
9532 if (ctype_match (ctype_realishType (expected), actual))
9537 llassert (!exprNode_isError (e));
9538 return (exprNode_matchLiteral (expected, e));
9542 exprNode_matchTypes (exprNode e1, exprNode e2)
9547 if (!exprNode_isDefined (e1)) return TRUE;
9548 if (!exprNode_isDefined (e2)) return TRUE;
9551 ** realish type --- keep bools, bools
9554 t1 = ctype_realishType (exprNode_getType (e1));
9555 t2 = ctype_realishType (exprNode_getType (e2));
9557 if (ctype_match (t1, t2))
9562 DPRINTF (("Matching literal! %s %s %s %s",
9563 ctype_unparse (t1), exprNode_unparse (e2),
9564 ctype_unparse (t2), exprNode_unparse (e1)));
9566 return (exprNode_matchLiteral (t1, e2) || exprNode_matchLiteral (t2, e1));
9574 exprNode_matchArgType (ctype ct, exprNode e)
9578 if (!exprNode_isDefined (e))
9583 et = ctype_realType (exprNode_getType (e));
9585 if (ctype_matchArg (ct, et)) return TRUE;
9587 llassert (!exprNode_isError (e));
9588 return (exprNode_matchLiteral (ct, e));
9591 static /*@only@*/ exprNodeSList
9592 exprNode_flatten (/*@dependent@*/ exprNode e) /*@*/
9594 if (exprNode_isDefined (e))
9596 if (e->kind == XPR_STMTLIST)
9598 return (exprNodeSList_append
9599 (exprNode_flatten (exprData_getPairA (e->edata)),
9600 exprNode_flatten (exprData_getPairB (e->edata))));
9602 else if (e->kind == XPR_BLOCK)
9604 return (exprNode_flatten (exprData_getSingle (e->edata)));
9608 return (exprNodeSList_singleton (e));
9612 return exprNodeSList_new ();
9615 static /*@exposed@*/ exprNode
9616 exprNode_lastStatement (/*@returned@*/ exprNode e)
9618 if (exprNode_isDefined (e))
9620 if (e->kind == XPR_STMTLIST)
9622 exprNode b = exprData_getPairB (e->edata);
9624 if (exprNode_isDefined (b))
9626 return exprNode_lastStatement (b);
9630 return exprNode_lastStatement (exprData_getPairA (e->edata));
9633 else if (e->kind == XPR_BLOCK)
9635 return (exprNode_lastStatement (exprData_getSingle (e->edata)));
9643 return exprNode_undefined;
9646 static /*@exposed@*/ exprNode
9647 exprNode_firstStatement (/*@returned@*/ exprNode e)
9649 if (exprNode_isDefined (e))
9651 if (e->kind == XPR_STMTLIST)
9653 exprNode b = exprData_getPairA (e->edata);
9655 if (exprNode_isDefined (b))
9657 return exprNode_firstStatement (b);
9661 return exprNode_firstStatement (exprData_getPairB (e->edata));
9664 else if (e->kind == XPR_BLOCK)
9666 return (exprNode_firstStatement (exprData_getSingle (e->edata)));
9674 return exprNode_undefined;
9678 exprNode_mergeUSs (exprNode res, exprNode other)
9680 if (exprNode_isDefined (res) && exprNode_isDefined (other))
9682 res->msets = sRefSet_union (res->msets, other->msets);
9683 res->sets = sRefSet_union (res->sets, other->sets);
9684 res->uses = sRefSet_union (res->uses, other->uses);
9689 exprNode_mergeCondUSs (exprNode res, exprNode other1, exprNode other2)
9691 if (exprNode_isDefined (res))
9693 if (exprNode_isDefined (other1))
9695 res->sets = sRefSet_union (res->sets, other1->sets);
9696 res->msets = sRefSet_union (res->msets, other1->msets);
9697 res->uses = sRefSet_union (res->uses, other1->uses);
9699 if (exprNode_isDefined (other2))
9701 res->sets = sRefSet_union (res->sets, other2->sets);
9702 res->msets = sRefSet_union (res->msets, other2->msets);
9703 res->uses = sRefSet_union (res->uses, other2->uses);
9711 ** Reports errors is s is not defined.
9715 exprNode_addUse (exprNode e, /*@exposed@*/ sRef s)
9717 if (exprNode_isDefined (e))
9719 e->uses = sRefSet_insert (e->uses, s);
9724 exprNode_checkUse (exprNode e, /*@exposed@*/ sRef s, fileloc loc)
9726 if (sRef_isKnown (s) && !sRef_isConst (s))
9729 ** need to check all outer types are useable
9732 DPRINTF (("Check use: %s / %s",
9733 exprNode_unparse (e), sRef_unparse (s)));
9735 exprNode_addUse (e, s);
9737 if (!context_inProtectVars ())
9740 ** only report the deepest error
9743 sRef errorRef = sRef_undefined;
9744 sRef lastRef = sRef_undefined;
9745 bool deadRef = FALSE;
9746 bool unuseable = FALSE;
9747 bool errorMaybe = FALSE;
9749 while (sRef_isValid (s) && sRef_isKnown (s))
9751 ynm readable = sRef_isReadable (s);
9753 DPRINTF (("Readable: %s / %s",
9754 sRef_unparseFull (s), ynm_unparse (readable)));
9756 if (!(ynm_toBoolStrict (readable)))
9758 if (ynm_isMaybe (readable))
9762 DPRINTF (("Setting ERROR: %s", sRef_unparseFull (s)));
9763 deadRef = sRef_isPossiblyDead (errorRef);
9764 unuseable = sRef_isUnuseable (errorRef);
9771 deadRef = sRef_isDead (errorRef);
9772 unuseable = sRef_isUnuseable (errorRef);
9776 if (!sRef_isPartial (s))
9778 DPRINTF (("Defining! %s", sRef_unparseFull (s)));
9779 sRef_setDefined (s, fileloc_undefined);
9783 s = sRef_getBaseSafe (s);
9786 if (sRef_isValid (errorRef))
9788 if (sRef_isValid (lastRef) && sRef_isField (lastRef)
9789 && sRef_isPointer (errorRef))
9796 if (sRef_isThroughArrayFetch (errorRef))
9799 (FLG_STRICTUSERELEASED,
9800 message ("%q %q may be used after being released",
9801 sRef_unparseKindNamePlain (errorRef),
9802 sRef_unparse (errorRef)),
9805 sRef_showRefKilled (errorRef);
9807 if (sRef_isKept (errorRef))
9809 sRef_clearAliasState (errorRef, loc);
9815 DPRINTF (("HERE: %s", sRef_unparse (errorRef)));
9819 message ("%q %q %qused after being released",
9820 sRef_unparseKindNamePlain (errorRef),
9821 sRef_unparse (errorRef),
9822 cstring_makeLiteral (errorMaybe
9826 sRef_showRefKilled (errorRef);
9828 if (sRef_isKept (errorRef))
9830 sRef_clearAliasState (errorRef, loc);
9839 message ("%q %q%qused in inconsistent state",
9840 sRef_unparseKindName (errorRef),
9841 sRef_unparseOpt (errorRef),
9842 cstring_makeLiteral (errorMaybe ? "may be " : "")),
9845 sRef_showStateInconsistent (errorRef);
9850 DPRINTF (("HERE: %s", sRef_unparseFull (errorRef)));
9854 message ("%q %q%qused before definition",
9855 sRef_unparseKindName (errorRef),
9856 sRef_unparseOpt (errorRef),
9857 cstring_makeLiteral (errorMaybe ? "may be " : "")),
9860 DPRINTF (("Error: %s", sRef_unparseFull (errorRef)));
9863 sRef_setDefined (errorRef, loc);
9865 if (sRef_isAddress (errorRef))
9867 sRef_setDefined (sRef_getRootBase (errorRef), loc);
9869 } /* end is error */
9877 checkSafeUse (exprNode e, /*@exposed@*/ sRef s)
9879 if (exprNode_isDefined (e) && sRef_isKnown (s))
9881 e->uses = sRefSet_insert (e->uses, s);
9886 exprNode_checkSetAny (exprNode e, /*@dependent@*/ cstring name)
9888 if (exprNode_isDefined (e))
9890 e->sets = sRefSet_insert (e->sets, sRef_makeUnconstrained (name));
9895 exprNode_checkSet (exprNode e, /*@exposed@*/ sRef s)
9897 sRef defines = sRef_undefined;
9899 if (sRef_isValid (s) && !sRef_isNothing (s))
9901 uentry ue = sRef_getBaseUentry (s);
9903 if (uentry_isValid (ue))
9905 uentry_setLset (ue);
9908 if (!ynm_toBoolStrict (sRef_isWriteable (s)))
9910 voptgenerror (FLG_USEDEF,
9911 message ("Attempt to set unuseable storage: %q",
9916 if (sRef_isMeaningful (s))
9918 if (sRef_isDead (s))
9920 sRef base = sRef_getBaseSafe (s);
9922 if (sRef_isValid (base)
9923 && sRef_isDead (base))
9925 sRef_setPartial (s, exprNode_loc (e));
9928 defines = s; /* okay - modifies for only param */
9930 else if (sRef_isPartial (s))
9932 sRef eref = exprNode_getSref (e);
9934 if (!sRef_isPartial (eref))
9937 ** should do something different here???
9940 sRef_setDefinedComplete (eref, exprNode_loc (e));
9944 sRef_setPartialDefinedComplete (eref, exprNode_loc (e));
9947 if (sRef_isMeaningful (eref))
9956 else if (sRef_isAllocated (s))
9958 sRef eref = exprNode_getSref (e);
9961 if (!sRef_isAllocated (eref))
9963 sRef_setDefinedComplete (eref, exprNode_loc (e));
9967 sRef base = sRef_getBaseSafe (eref);
9969 if (sRef_isValid (base))
9971 sRef_setPdefined (base, exprNode_loc (e));
9979 sRef_setDefinedNCComplete (s, exprNode_loc (e));
9984 else /* not meaningful...but still need to insert it */
9990 if (exprNode_isDefined (e) && sRef_isValid (defines))
9992 e->sets = sRefSet_insert (e->sets, defines);
9997 exprNode_checkMSet (exprNode e, /*@exposed@*/ sRef s)
9999 if (sRef_isValid (s) && !sRef_isNothing (s))
10001 uentry ue = sRef_getBaseUentry (s);
10003 if (uentry_isValid (ue))
10005 uentry_setLset (ue);
10008 if (!ynm_toBoolStrict (sRef_isWriteable (s)))
10010 voptgenerror (FLG_USEDEF,
10011 message ("Attempt to set unuseable storage: %q", sRef_unparse (s)),
10015 if (sRef_isMeaningful (s))
10017 sRef_setDefinedComplete (s, exprNode_loc (e));
10020 if (exprNode_isDefined (e))
10022 e->msets = sRefSet_insert (e->msets, s);
10028 checkUnspecCall (/*@notnull@*/ /*@dependent@*/ exprNode fcn, uentryList params, exprNodeList args)
10030 checkAnyCall (fcn, cstring_undefined, params, args,
10031 FALSE, sRefSet_undefined, FALSE, 0);
10035 checkOneArg (uentry ucurrent, /*@notnull@*/ exprNode current,
10036 /*@dependent@*/ exprNode fcn, bool isSpec, int argno, int totargs)
10040 if (uentry_isYield (ucurrent))
10042 sRef_setDefined (exprNode_getSref (current), exprNode_loc (current));
10043 exprNode_checkSet (current, current->sref);
10047 if (uentry_isSefParam (ucurrent))
10049 sRefSet sets = current->sets;
10050 sRef ref = exprNode_getSref (current);
10052 if (sRef_isMacroParamRef (ref))
10054 uentry ue = sRef_getUentry (ref);
10056 if (!uentry_isSefParam (ue))
10061 ("Parameter %d to %s is declared sef, but "
10062 "the argument is a macro parameter declared "
10064 argno, exprNode_unparse (fcn),
10065 exprNode_unparse (current)),
10066 exprNode_loc (current));
10070 if (!sRefSet_isEmpty (sets))
10072 sRefSet reported = sRefSet_undefined;
10074 sRefSet_realElements (current->sets, el)
10076 if (sRefSet_isSameNameMember (reported, el))
10078 ; /* don't report again */
10082 if (sRef_isUnconstrained (el))
10087 ("Parameter %d to %s is declared sef, but "
10088 "the argument calls unconstrained function %s "
10089 "(no guarantee it will not modify something): %s",
10090 argno, exprNode_unparse (fcn),
10091 sRef_unconstrainedName (el),
10092 exprNode_unparse (current)),
10093 exprNode_loc (current));
10100 ("Parameter %d to %s is declared sef, but "
10101 "the argument may modify %q: %s",
10102 argno, exprNode_unparse (fcn),
10104 exprNode_unparse (current)),
10105 exprNode_loc (current));
10108 } end_sRefSet_realElements;
10112 checkPassTransfer (current, ucurrent, isSpec, fcn, argno, totargs);
10113 exprNode_mergeUSs (fcn, current);
10118 checkAnyCall (/*@dependent@*/ exprNode fcn,
10119 /*@dependent@*/ cstring fname,
10122 bool hasMods, sRefSet mods,
10127 int nargs = exprNodeList_size (args);
10132 ** concat all args ud's to f, add each arg sref as a use unless
10133 ** it was specified as "out", in which case it is a def.
10136 uentryList_reset (pn);
10139 ** aliasing checks:
10141 ** if paramn is only or unique, no other arg may alias argn
10144 exprNodeList_elements (args, current)
10148 if (exprNode_isDefined (current))
10150 if ((!uentryList_isUndefined (pn) && !uentryList_isFinished (pn)))
10152 uentry ucurrent = uentryList_current (pn);
10154 if (specialArgs == 0
10155 || (paramno < specialArgs))
10157 checkOneArg (ucurrent, current, fcn, isSpec, paramno, nargs);
10159 if (context_maybeSet (FLG_ALIASUNIQUE))
10161 if (uentry_isOnly (ucurrent)
10162 || uentry_isUnique (ucurrent))
10164 checkUniqueParams (fcn, current, args,
10165 paramno, ucurrent);
10170 else /* uentry is undefined */
10172 if (specialArgs == 0)
10174 exprNode_checkUseParam (current);
10177 exprNode_mergeUSs (fcn, current);
10180 uentryList_advanceSafe (pn);
10181 } end_exprNodeList_elements;
10187 sRefSet_allElements (mods, s)
10190 sRef rb = sRef_getRootBase (s);
10192 if (sRef_isFileOrGlobalScope (rb))
10194 context_usedGlobal (rb);
10197 fb = sRef_fixBaseParam (s, args);
10199 if (!sRef_isMacroParamRef (fb))
10201 if (sRef_isNothing (fb))
10207 if (sRef_isValid (fb))
10209 uentry ue = sRef_getBaseUentry (s);
10211 if (uentry_isValid (ue))
10213 uentry_setLset (ue);
10217 fcn->sets = sRefSet_insert (fcn->sets, fb);
10220 sRef_clearDerivedComplete (s);
10221 } end_sRefSet_allElements;
10227 if (context_hasMods ())
10229 if (context_maybeSet (FLG_MODUNCON))
10233 message ("Undetected modification possible "
10234 "from call to unconstrained function %s: %s",
10236 exprNode_unparse (fcn)),
10237 exprNode_loc (fcn));
10242 if (context_maybeSet (FLG_MODUNCONNOMODS)
10243 && !(context_inIterDef () || context_inIterEnd ()))
10246 (FLG_MODUNCONNOMODS,
10247 message ("Undetected modification possible "
10248 "from call to unconstrained function %s: %s",
10250 exprNode_unparse (fcn)),
10251 exprNode_loc (fcn));
10255 exprNode_checkSetAny (fcn, fname);
10259 void exprNode_checkUseParam (exprNode current)
10261 if (exprNode_isDefined (current))
10263 exprNode_checkUse (current, current->sref, current->loc);
10268 checkNumerics (ctype tr1, ctype tr2, ctype te1, ctype te2,
10269 /*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2,
10274 if (!ctype_match (tr1, tr2))
10276 if ((ctype_isRealInt (tr1) || ctype_isReal (tr1)) &&
10277 (ctype_isRealInt (tr2) || ctype_isReal (tr2)))
10279 DPRINTF (("No error: [%s] %s / [%s] %s",
10280 exprNode_unparse (e1), ctype_unparse (tr1),
10281 exprNode_unparse (e2), ctype_unparse (tr2)));
10285 (void) gentypeerror
10287 message ("Incompatible types for %s (%s, %s): %s %s %s",
10288 lltok_unparse (op),
10289 ctype_unparse (te1),
10290 ctype_unparse (te2),
10291 exprNode_unparse (e1), lltok_unparse (op),
10292 exprNode_unparse (e2)),
10295 ret = ctype_unknown;
10299 if (ctype_isForceRealNumeric (&tr1) && ctype_isForceRealNumeric (&tr2))
10301 ret = ctype_resolveNumerics (tr1, tr2);
10303 else if (!context_msgStrictOps ())
10305 if (ctype_isPointer (tr1))
10307 if (ctype_isPointer (tr2) && !exprNode_isNullValue (e2))
10311 else if (ctype_isInt (tr2))
10317 ret = ctype_unknown;
10320 else if (ctype_isPointer (tr2))
10322 if (ctype_isPointer (tr1))
10326 else if (ctype_isInt (tr1))
10332 ret = ctype_unknown;
10337 ret = ctype_resolveNumerics (tr1, tr2);
10342 int opid = lltok_getTok (op);
10343 bool comparop = (opid == EQ_OP || opid == NE_OP
10344 || opid == TLT || opid == TGT
10345 || opid == LE_OP || opid == GE_OP);
10347 if (!ctype_isNumeric (tr1) && !ctype_isNumeric (tr2))
10350 && ((ctype_isEnum (tr1) && ctype_isEnum (tr2))
10351 || (ctype_isBool (tr1) && ctype_isBool (tr2))
10352 || (ctype_isChar (tr1) && ctype_isChar (tr2))))
10358 if (ctype_sameName (te1, te2))
10362 message ("Operands of %s are non-numeric (%t): %s %s %s",
10363 lltok_unparse (op), te1,
10364 exprNode_unparse (e1), lltok_unparse (op),
10365 exprNode_unparse (e2)),
10372 message ("Operands of %s are non-numerics (%t, %t): %s %s %s",
10373 lltok_unparse (op), te1, te2,
10374 exprNode_unparse (e1), lltok_unparse (op),
10375 exprNode_unparse (e2)),
10380 else if (!ctype_isNumeric (tr1))
10384 message ("Right operand of %s is non-numeric (%t): %s %s %s",
10385 lltok_unparse (op), te1,
10386 exprNode_unparse (e1), lltok_unparse (op),
10387 exprNode_unparse (e2)),
10392 if (!ctype_isNumeric (tr2))
10396 message ("Left operand of %s is non-numeric (%t): %s %s %s",
10397 lltok_unparse (op), te2,
10398 exprNode_unparse (e1), lltok_unparse (op),
10399 exprNode_unparse (e2)),
10404 ret = ctype_unknown;
10412 abstractOpError (ctype tr1, ctype tr2, lltok op,
10413 /*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2,
10414 fileloc loc1, fileloc loc2)
10416 if (ctype_isRealAbstract (tr1) && ctype_isRealAbstract (tr2))
10418 if (ctype_match (tr1, tr2))
10422 message ("Operands of %s are abstract type (%t): %s %s %s",
10423 lltok_unparse (op), tr1,
10424 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
10431 message ("Operands of %s are abstract types (%t, %t): %s %s %s",
10432 lltok_unparse (op), tr1, tr2,
10433 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
10437 else if (ctype_isRealAbstract (tr1))
10441 message ("Left operand of %s is abstract type (%t): %s %s %s",
10442 lltok_unparse (op), tr1,
10443 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
10448 if (ctype_isRealAbstract (tr2))
10452 message ("Right operand of %s is abstract type (%t): %s %s %s",
10453 lltok_unparse (op), tr2,
10454 exprNode_unparse (e1), lltok_unparse (op), exprNode_unparse (e2)),
10463 ** requies e1 and e2 and not error exprNode's.
10467 ** If e1 is a component of an abstract type, and e2 is mutable and client-visible,
10468 ** the rep of the abstract type is exposed.
10470 ** The order is very important:
10472 ** check rep expose (move into check transfer)
10478 ** This isn't really a sensible procedure, but the indententation
10479 ** was getting too deep.
10483 checkOneRepExpose (sRef ysr, sRef base,
10484 /*@notnull@*/ exprNode e1,
10485 /*@notnull@*/ exprNode e2, ctype ct,
10488 if (!(sRef_isOnly (ysr) || sRef_isKeep (ysr)
10489 || sRef_isOwned (ysr)
10490 || sRef_isExposed (ysr)))
10492 if (sRef_isAnyParam (base) && !sRef_isExposed (base)
10493 && !sRef_isObserver (base)) /* evans 2001-07-11: added isObserver */
10496 if (sRef_isIReference (ysr))
10498 if (sRef_sameName (base, sRef_getRootBase (e2->sref)))
10503 ("Assignment of mutable component of parameter %q "
10504 "to component of abstract "
10505 "type %s exposes rep: %s = %s",
10506 sRef_unparse (base),
10507 ctype_unparse (ct),
10508 exprNode_unparse (e1), exprNode_unparse (e2)),
10516 ("Assignment of mutable component of parameter %q "
10517 "(through alias %q) to component of abstract "
10518 "type %s exposes rep: %s = %s",
10519 sRef_unparse (base),
10520 sRef_unparse (e2->sref),
10521 ctype_unparse (ct),
10522 exprNode_unparse (e1), exprNode_unparse (e2)),
10528 if (sRef_sameName (base, sRef_getRootBase (e2->sref)))
10532 message ("Assignment of mutable parameter %q "
10533 "to component of abstract type %s "
10534 "exposes rep: %s = %s",
10535 sRef_unparse (base),
10536 ctype_unparse (ct),
10537 exprNode_unparse (e1),
10538 exprNode_unparse (e2)),
10545 message ("Assignment of mutable parameter %q "
10546 "(through alias %q) to "
10547 "component of abstract type %s exposes "
10549 sRef_unparse (base),
10550 sRef_unparse (e2->sref),
10551 ctype_unparse (ct),
10552 exprNode_unparse (e1),
10553 exprNode_unparse (e2)),
10559 if (sRef_isFileOrGlobalScope (s2b))
10561 if (sRef_sameName (base, sRef_getRootBase (e2->sref)))
10565 message ("Assignment of global %q "
10567 "abstract type %s exposes rep: %s = %s",
10568 sRef_unparse (base),
10569 ctype_unparse (ct),
10570 exprNode_unparse (e1), exprNode_unparse (e2)),
10577 message ("Assignment of global %q (through alias %q) "
10579 "abstract type %s exposes rep: %s = %s",
10580 sRef_unparse (base),
10581 sRef_unparse (e2->sref),
10582 ctype_unparse (ct),
10583 exprNode_unparse (e1), exprNode_unparse (e2)),
10591 doAssign (/*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2, bool isInit)
10593 DPRINTF (("Do assign: %s <- %s",
10594 exprNode_unparse (e1), exprNode_unparse (e2)));
10595 DPRINTF (("Ctype: %s", ctype_unparse (exprNode_getType (e1))));
10597 if (ctype_isRealFunction (exprNode_getType (e1))
10598 && !ctype_isRealPointer (exprNode_getType (e1)))
10602 message ("Invalid left-hand side of assignment (function type %s): %s",
10603 ctype_unparse (exprNode_getType (e1)),
10604 exprNode_unparse (e1)),
10608 if (context_getFlag (FLG_ASSIGNEXPOSE) && ctype_isMutable (e2->typ))
10610 ctype t2 = exprNode_getType (e2);
10611 sRef sr = sRef_getRootBase (e1->sref);
10612 ctype ct = sRef_getType (sr);
10614 if (ctype_isAbstract (t2)
10615 && !(uentry_isMutableDatatype (usymtab_getTypeEntry (ctype_typeId (t2)))))
10617 /* it is immutable, okay to reference */
10618 goto donerepexpose;
10621 if (ctype_isAbstract (ct) && sRef_isIReference (e1->sref))
10623 sRef s2b = sRef_getRootBase (e2->sref);
10624 sRef s1 = e1->sref;
10625 sRef s1b = sRef_getRootBase (s1);
10628 aliases = usymtab_canAlias (e2->sref);
10630 if (!sRef_similar (s2b, s1b)
10631 && !sRef_isExposed (s1)
10632 && !(sRef_isOnly (s2b) || sRef_isKeep (s2b) || sRef_isExposed (s2b)))
10634 if (sRef_isAnyParam (s2b) && !sRef_isOnly (s2b)
10635 && !sRef_isOwned (s2b) && !sRef_isKeep (s2b)
10636 && !sRef_isExposed (s2b))
10638 if (sRef_isIReference (e2->sref))
10643 ("Assignment of mutable component of parameter %q "
10644 "to component of abstract type %s exposes rep: %s = %s",
10645 sRef_unparse (s2b),
10646 ctype_unparse (ct),
10647 exprNode_unparse (e1), exprNode_unparse (e2)),
10654 message ("Assignment of mutable parameter %q to "
10655 "component of abstract type %s exposes rep: %s = %s",
10656 sRef_unparse (s2b),
10657 ctype_unparse (ct),
10658 exprNode_unparse (e1), exprNode_unparse (e2)),
10663 if (sRef_isFileOrGlobalScope (s2b))
10667 message ("Assignment of global %q to component of "
10668 "abstract type %s exposes rep: %s = %s",
10669 sRef_unparse (s2b),
10670 ctype_unparse (ct),
10671 exprNode_unparse (e1), exprNode_unparse (e2)),
10675 sRefSet_realElements (aliases, ysr)
10677 sRef base = sRef_getRootBase (ysr);
10679 if (sRef_similar (ysr, s2b) || sRef_similar (s1b, base)
10680 || sRef_sameName (base, s1b))
10682 ; /* error already reported or same sref */
10686 checkOneRepExpose (ysr, base, e1, e2, ct, s2b);
10688 } end_sRefSet_realElements;
10690 sRefSet_free (aliases);
10697 ** function variables don't really work...
10700 if (!ctype_isFunction (ctype_realType (e2->typ)))
10704 DPRINTF (("Check init: %s / %s",
10705 exprNode_unparse (e1), exprNode_unparse (e2)));
10706 checkInitTransfer (e1, e2);
10710 checkAssignTransfer (e1, e2);
10715 sRef fref = e2->sref;
10717 sRef_setDefState (e1->sref, sRef_getDefState (fref), e1->loc);
10718 sRef_setNullState (e1->sref, sRef_getNullState (fref), e1->loc);
10720 /* Need to typecheck the annotation on the parameters */
10722 if (ctype_isRealFunction (e1->typ)) {
10723 uentryList e1p = ctype_argsFunction (ctype_realType (e1->typ));
10724 uentryList e2p = ctype_argsFunction (ctype_realType (e2->typ));
10726 if (!uentryList_isMissingParams (e1p)
10727 && !uentryList_isMissingParams (e2p)
10728 && uentryList_size (e1p) > 0) {
10729 if (uentryList_size (e1p) == uentryList_size (e2p)) {
10732 uentryList_elements (e1p, el1) {
10735 el2 = uentryList_getN (e2p, n);
10737 uentry_checkMatchParam (el1, el2, n, e2);
10738 } end_uentryList_elements;
10744 if (exprNode_isStringLiteral (e2))
10746 exprNode_checkStringLiteralLength (exprNode_getType (e1), e2);
10749 if (isInit && sRef_isFileOrGlobalScope (e1->sref))
10755 DPRINTF (("Update aliases: %s / %s", exprNode_unparse (e1), exprNode_unparse (e2)));
10756 updateAliases (e1, e2);
10761 checkMacroParen (exprNode e)
10763 if (exprNode_isError (e) || e->kind == XPR_CAST)
10769 if (sRef_isUnsafe (e->sref) && !exprNode_isInParens (e))
10773 message ("Macro parameter used without parentheses: %s",
10774 exprNode_unparse (e)),
10781 reflectNullTest (/*@notnull@*/ exprNode e, bool isnull)
10785 e->guards = guardSet_addTrueGuard (e->guards, e->sref);
10789 e->guards = guardSet_addFalseGuard (e->guards, e->sref);
10796 ** if e2 is a parameter or global derived location which
10797 ** can be modified (that is, e2 is a mutable abstract type,
10798 ** or a derived pointer), then e1 can alias e2.
10800 ** e1 can alias everything which e2 can alias.
10802 ** Also, if e1 is guarded, remove from guard sets!
10805 static void updateAliases (/*@notnull@*/ exprNode e1, /*@notnull@*/ exprNode e2)
10807 if (!context_inProtectVars ())
10810 ** depends on types of e1 and e2
10813 sRef s1 = e1->sref;
10814 sRef s2 = e2->sref;
10815 ctype t1 = exprNode_getType (e1);
10817 /* handle pointer sRefs, record fields, arrays, etc... */
10819 if (!ctype_isRealSU (t1))
10821 DPRINTF (("Copying real! %s", ctype_unparse (t1)));
10822 sRef_copyRealDerivedComplete (s1, s2);
10827 ** Fields should alias
10830 DPRINTF (("Not COPYING!: %s", ctype_unparse (t1)));
10833 if (ctype_isMutable (t1) && sRef_isKnown (s1))
10835 usymtab_clearAlias (s1);
10836 usymtab_addMustAlias (s1, s2);
10837 DPRINTF (("Add must alias: %s / %s", sRef_unparse (s1), sRef_unparse (s2)));
10841 DPRINTF (("Not mutable: %s", ctype_unparse (t1)));
10844 if (sRef_possiblyNull (s1) && usymtab_isGuarded (s1))
10846 usymtab_unguard (s1);
10851 exprNode exprNode_updateLocation (/*@returned@*/ exprNode e, /*@temp@*/ fileloc loc)
10853 if (exprNode_isDefined (e))
10855 e->loc = fileloc_update (e->loc, loc);
10859 e = exprNode_createLoc (ctype_unknown, fileloc_copy (loc));
10865 static void checkUniqueParams (exprNode fcn,
10866 /*@notnull@*/ exprNode current,
10868 int paramno, uentry ucurrent)
10871 sRef thisref = exprNode_getSref (current);
10874 ** Check if any argument could match this argument.
10877 exprNodeList_elements (args, icurrent)
10881 if (iparamno != paramno)
10883 sRef sr = exprNode_getSref (icurrent);
10885 if (sRef_similarRelaxed (thisref, sr))
10887 if (!sRef_isConst (thisref) && !sRef_isConst (sr))
10892 ("Parameter %d (%s) to function %s is declared %s but "
10893 "is aliased by parameter %d (%s)",
10895 exprNode_unparse (current),
10896 exprNode_unparse (fcn),
10897 alkind_unparse (uentry_getAliasKind (ucurrent)),
10898 iparamno, exprNode_unparse (icurrent)),
10904 sRefSet aliases = usymtab_canAlias (sr);
10906 sRefSet_allElements (aliases, asr)
10908 if (ctype_isUnknown (sRef_getType (thisref)))
10910 sRef_setType (thisref, uentry_getType (ucurrent));
10913 if (sRef_similarRelaxed (thisref, asr))
10915 if (sRef_isExternal (asr))
10917 if (sRef_isLocalState (thisref))
10923 sRef base = sRef_getRootBase (asr);
10925 if (!sRef_similar (sRef_getBase (asr), thisref))
10927 if (sRef_isUnique (base) || sRef_isOnly (base)
10928 || sRef_isKept (base)
10929 || (sRef_isAddress (asr) && sRef_isLocalVar (base))
10930 || (sRef_isAddress (thisref)
10931 && sRef_isLocalVar (sRef_getRootBase (thisref))))
10933 ; /* okay, no error */
10938 (FLG_MAYALIASUNIQUE,
10940 ("Parameter %d (%s) to function %s is declared %s but "
10941 "may be aliased externally by parameter %d (%s)",
10943 exprNode_unparse (current),
10944 exprNode_unparse (fcn),
10945 alkind_unparse (uentry_getAliasKind (ucurrent)),
10946 iparamno, exprNode_unparse (icurrent)),
10957 ("Parameter %d (%s) to function %s is declared %s but "
10958 "is aliased externally by parameter %d (%s) through "
10961 exprNode_unparse (current),
10962 exprNode_unparse (fcn),
10963 alkind_unparse (uentry_getAliasKind (ucurrent)),
10964 iparamno, exprNode_unparse (icurrent),
10965 sRef_unparse (asr)),
10969 } end_sRefSet_allElements;
10970 sRefSet_free (aliases);
10973 } end_exprNodeList_elements;
10976 long exprNode_getLongValue (exprNode e) {
10979 if (exprNode_hasValue (e)
10980 && multiVal_isInt (exprNode_getValue (e)))
10982 value = multiVal_forceInt (exprNode_getValue (e));
10993 /*@observer@*/ fileloc exprNode_getfileloc (exprNode p_e)
10995 if (exprNode_isDefined (p_e) )
10996 return ( p_e->loc );
10998 return fileloc_undefined;
11001 /*@only@*/ fileloc exprNode_getNextSequencePoint (exprNode e)
11004 ** Returns the location of the sequence point following e.
11006 ** Only works for statements (for now).
11009 if (exprNode_isDefined (e) && e->kind == XPR_STMT) {
11010 lltok t = exprData_getUopTok (e->edata);
11011 return fileloc_copy(lltok_getLoc (t));
11013 /* drl possible problem : warning fix
11014 llcontbug (message ("Cannot get next sequence point: %s", exprNode_unparse (e)));
11016 return fileloc_undefined;
11020 exprNode exprNode_createNew(ctype c)
11024 ret = exprNode_createPlain (c);
11029 bool exprNode_isInitBlock (exprNode e)
11031 return (exprNode_isDefined(e) && e->kind == XPR_INITBLOCK);