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 splint: splint@cs.virginia.edu
21 ** To report a bug: splint-bug@cs.virginia.edu
22 ** For more information: http://www.splint.org
27 ** NOTE: This is not a stand-alone source file, but is included in ctype.c.
28 ** (This is necessary because there is no other way in C to have a
29 ** hidden scope, besides at the file level.)
34 abst_typedef /*@null@*/ struct s_ctbase *ctbase;
36 /*@private@*/ typedef struct {
39 ctype base; /* type I point to (or element of array) */
40 ctype ptr; /* type of pointer to me */
41 ctype array; /* type of array of me */
42 cstring unparse; /* unparse me, if memoized */
45 typedef /*@only@*/ ctentry o_ctentry;
50 /*@relnull@*/ /*@only@*/ o_ctentry *entries;
51 /* memoize matches...maybe in context? */
54 extern bool ctentry_isBogus (/*@sef@*/ ctentry p_c) /*@*/;
55 # define ctentry_isBogus(c) \
56 ((c)->kind == CTK_INVALID || (c)->kind == CTK_DNE)
58 static cttable cttab = { 0, 0, NULL };
60 static /*@notnull@*/ /*@only@*/ ctbase ctbase_createAbstract (typeId p_u);
61 static /*@observer@*/ cstring ctentry_doUnparse (ctentry p_c) /*@modifies p_c@*/;
62 static /*@only@*/ ctentry
63 ctentry_make (ctkind p_ctk, /*@keep@*/ ctbase p_c, ctype p_base,
64 ctype p_ptr, ctype p_array, /*@keep@*/ cstring p_unparse);
65 static /*@only@*/ ctentry ctentry_makeNew (ctkind p_ctk, /*@only@*/ ctbase p_c);
66 static /*@only@*/ cstring ctentry_unparse (ctentry p_c) /*@*/ ;
68 static void cttable_grow (void);
69 static ctype cttable_addDerived (ctkind p_ctk, /*@keep@*/ ctbase p_cnew, ctype p_base);
70 static ctype cttable_addFull (/*@keep@*/ ctentry p_cnew);
71 static bool ctentry_isInteresting (ctentry p_c) /*@*/;
72 static /*@notnull@*/ /*@only@*/ ctbase ctbase_makeFixedArray (ctype p_b, long p_size) /*@*/ ;
75 ** These are file-static macros (used in ctype.c). No way to
76 ** declare them as static in C.
79 /*@-allmacros@*/ /*@-macrospec@*/ /*@-namechecks@*/
80 # define ctentry_getBase(c) ((c)->base)
81 # define ctentry_getKind(c) ((c)->kind)
82 # define ctentry_getArray(c) ((c)->array)
83 # define ctentry_getPtr(c) ((c)->ptr)
84 # define ctentry_isArray(c) ((c)->kind == CTK_ARRAY)
85 # define ctentry_isComplex(c) ((c)->kind == CTK_COMPLEX)
86 # define ctentry_isPlain(c) ((c)->kind == CTK_PLAIN)
87 # define ctentry_isPointer(c) ((c)->kind == CTK_PTR)
88 # define ctentry_setArray(c,b) ((c)->array = (b))
89 # define ctentry_setPtr(c,b) ((c)->ptr = (b))
91 # define ctbase_fixUser(c) (c = ctbase_realType(c))
92 /*@=allmacros@*/ /*@=macrospec@*/ /*@=namechecks@*/
94 static ctype cttable_addComplex (/*@notnull@*/ /*@only@*/ ctbase p_cnew);
95 static /*@observer@*/ ctbase ctype_getCtbase (ctype p_c) /*@*/ ;
96 static ctype ctype_makeConjAux (ctype p_c1, ctype p_c2, bool p_isExplicit) /*@*/ ;
97 static /*@notnull@*/ /*@observer@*/ ctbase ctype_getCtbaseSafe (ctype p_c) /*@*/ ;
98 static /*@observer@*/ ctentry ctype_getCtentry (ctype p_c) /*@*/ ;
99 static /*@observer@*/ /*@notnull@*/ ctbase
100 ctbase_realType (/*@notnull@*/ ctbase p_c) /*@*/ ;
101 static bool ctbase_isPointer (/*@notnull@*/ /*@dependent@*/ ctbase p_c) /*@*/ ;
102 static bool ctbase_isEitherArray (/*@notnull@*/ /*@dependent@*/ ctbase p_c) /*@*/ ;
103 static /*@observer@*/ enumNameList ctbase_elist (ctbase p_c) /*@*/ ;
104 static /*@only@*/ cstring ctbase_unparse (ctbase p_c) /*@*/ ;
105 static /*@only@*/ cstring ctbase_unparseDeep (ctbase p_c) /*@*/ ;
106 static /*@only@*/ /*@notnull@*/ ctbase ctbase_copy (/*@notnull@*/ ctbase p_c) /*@*/ ;
107 static void ctbase_free (/*@only@*/ ctbase p_c);
108 static /*@notnull@*/ /*@only@*/ ctbase ctbase_createPrim (cprim p_p) /*@*/ ;
109 static /*@notnull@*/ /*@only@*/ ctbase ctbase_createBool (void) /*@*/ ;
110 static /*@notnull@*/ /*@observer@*/ ctbase ctbase_getBool (void) /*@*/ ;
111 static /*@notnull@*/ /*@only@*/ ctbase ctbase_createUser (typeId p_u) /*@*/ ;
113 static /*@notnull@*/ /*@only@*/ ctbase
114 ctbase_createStruct (/*@only@*/ cstring p_n, /*@only@*/ uentryList p_f);
116 static /*@notnull@*/ /*@only@*/ ctbase
117 ctbase_createUnion (/*@keep@*/ cstring p_n, /*@only@*/ uentryList p_f);
118 static /*@notnull@*/ /*@only@*/ ctbase ctbase_createEnum (/*@keep@*/ cstring p_etag, /*@keep@*/ enumNameList p_emembers);
119 static /*@notnull@*/ /*@only@*/ ctbase ctbase_createUnknown (void);
120 static bool ctbase_match (ctbase p_c1, ctbase p_c2) /*@modifies nothing@*/;
121 static bool ctbase_matchDef (ctbase p_c1, ctbase p_c2) /*@modifies nothing@*/;
122 static bool ctbase_genMatch (ctbase p_c1, ctbase p_c2, bool p_force, bool p_arg, bool p_def, bool p_deep);
123 static bool ctbase_isAbstract (/*@notnull@*/ ctbase p_c) /*@*/ ;
124 static /*@notnull@*/ /*@only@*/ ctbase ctbase_makePointer (ctype p_b) /*@*/ ;
125 static /*@notnull@*/ /*@only@*/ ctbase ctbase_makeArray (ctype p_b) /*@*/ ;
126 static /*@notnull@*/ ctype
127 ctbase_makeFunction (ctype p_b, /*@only@*/ uentryList p_p) /*@*/ ;
128 static /*@notnull@*/ /*@observer@*/ ctbase
129 ctbase_realFunction (/*@notnull@*/ /*@dependent@*/ ctbase p_c) /*@*/ ;
130 static ctype ctbase_baseArrayPtr (/*@notnull@*/ ctbase p_c) /*@*/ ;
131 static ctype ctbase_baseFunction (/*@notnull@*/ ctbase p_c) /*@*/ ;
132 static /*@observer@*/ uentryList ctbase_argsFunction (/*@notnull@*/ ctbase p_c) /*@*/ ;
133 static /*@observer@*/ uentryList ctbase_getuentryList (/*@notnull@*/ ctbase p_c) /*@*/ ;
134 static ctype ctbase_newBase (ctype p_c, ctype p_p) /*@*/ ;
135 static ctype ctbase_newBaseExpFcn (ctype p_c, ctype p_p) /*@*/ ;
136 static bool ctbase_isFixedArray (/*@notnull@*/ ctbase p_c) /*@*/ ;
139 extern int cttable_lastIndex();
140 # define cttable_lastIndex() (cttab.size - 1)
146 /*@only@*/ uentryList params;
165 enumNameList members;
176 cprim prim; /* primitive */
177 typeId tid; /* abstract, user */
178 ctype base; /* ptr, array */
179 cfcn fcn; /* function */
180 tsu su; /* struct union */
181 tenum cenum; /* enum */
182 tconj conj; /* conj */
183 tfixed farray; /* fixed array */
192 static /*@falsenull@*/ bool ctbase_isUA (ctbase p_c) /*@*/ ;
193 static bool ctbase_isBaseUA(ctbase p_c) /*@*/ ;
194 static typeId ctbase_typeBaseUid(ctbase p_c) /*@*/ ;
195 static bool ctbase_isKind (/*@notnull@*/ ctbase p_c, ctuid p_kind) /*@*/ ;
196 static bool ctbase_isKind2 (/*@notnull@*/ ctbase p_c, ctuid p_kind1, ctuid p_kind2) /*@*/ ;
197 static /*@only@*/ /*@notnull@*/ ctbase
198 ctbase_getBaseType (/*@notnull@*/ ctbase p_c) /*@*/ ;
200 static /*@falsenull@*/ bool ctbase_isFunction(ctbase p_c) /*@*/ ;
202 /*@constant null ctbase ctbase_undefined; @*/
203 # define ctbase_undefined ((ctbase)0)
205 static /*@owned@*/ ctbase ctbase_bool = ctbase_undefined;
206 static /*@owned@*/ ctbase ctbase_unknown = ctbase_undefined;
208 static /*@falsenull@*/ bool ctbase_isDefined (ctbase c) /*@*/
210 return ((c) != ctbase_undefined);
213 static /*@truenull@*/ bool ctbase_isUndefined (ctbase c)
215 return ((c) == ctbase_undefined);
218 static ctkind ctype_getCtKind (ctype c)
220 ctentry ce = ctype_getCtentry (c);
222 return ctentry_getKind (ce);
225 static bool ctbase_isUser (ctbase c)
227 if (ctbase_isDefined (c))
229 return (ctbase_isKind (c, CT_USER));
237 static bool ctbase_isEnum (ctbase c)
239 if (ctbase_isDefined (c))
241 return (ctbase_isKind (c, CT_ENUM));
249 static bool ctbase_isExpFcn (ctbase c)
251 if (ctbase_isDefined (c))
253 return (c->type == CT_EXPFCN);
261 static /*@falsenull@*/ bool ctbase_isConj (ctbase c)
263 if (ctbase_isDefined (c))
265 return (c->type == CT_CONJ);
273 static bool ctuid_isAP (ctuid c) /*@*/
275 return (c == CT_ARRAY || c == CT_PTR);
278 static typeId ctbase_typeId (ctbase p_c);
279 static /*@only@*/ cstring ctbase_dump (ctbase p_c);
280 static /*@only@*/ ctbase ctbase_undump (char **p_c) /*@requires maxRead(*p_c) >= 2 @*/;
281 static int ctbase_compare (ctbase p_c1, ctbase p_c2, bool p_strict);
282 static bool ctbase_matchArg (ctbase p_c1, ctbase p_c2);
283 static /*@notnull@*/ /*@only@*/ ctbase
284 ctbase_makeConj (ctype p_c1, ctype p_c2, bool p_isExplicit) /*@*/ ;
285 static ctype ctbase_getConjA (/*@notnull@*/ ctbase p_c) /*@*/ ;
286 static ctype ctbase_getConjB (/*@notnull@*/ ctbase p_c) /*@*/ ;
287 static bool ctbase_isExplicitConj (/*@notnull@*/ ctbase p_c) /*@*/ ;
288 static bool ctbase_forceMatch (ctbase p_c1, ctbase p_c2) /*@modifies p_c1, p_c2@*/ ;
289 static /*@notnull@*/ /*@only@*/ ctbase ctbase_expectFunction (ctype p_c);
290 static bool ctbase_isVoidPointer(/*@notnull@*/ /*@dependent@*/ ctbase p_c) /*@*/ ;
291 static bool ctbase_isUnion (/*@notnull@*/ /*@temp@*/ ctbase p_c) /*@*/ ;
292 static bool ctbase_isStruct (/*@notnull@*/ /*@temp@*/ ctbase p_c) /*@*/ ;
293 static /*@observer@*/ cstring ctbase_enumTag (/*@notnull@*/ ctbase p_ct) /*@*/ ;
294 static /*@only@*/ cstring ctbase_unparseNotypes (ctbase p_c) /*@*/ ;
296 static /*@out@*/ /*@notnull@*/ /*@only@*/ ctbase ctbase_new (void) /*@*/ ;
297 static int nctbases = 0;
299 static /*@notnull@*/ /*@only@*/
300 ctbase ctbase_makeLiveFunction (ctype p_b, /*@only@*/ uentryList p_p);
302 static bool ctbase_isUnnamedSU (ctbase c)
304 return (ctbase_isDefined (c)
305 && (ctbase_isStruct (c) || ctbase_isUnion (c))
306 && isFakeTag (c->contents.su->name));
309 static /*@observer@*/ ctbase ctbase_realType (ctbase c)
313 typeId uid = ctbase_typeId (c);
315 if (usymtab_isBoolType (uid))
317 return ctbase_getBool ();
321 ctbase ret = ctype_getCtbase
322 (uentry_getRealType (usymtab_getTypeEntry (ctbase_typeId (c))));
324 llassert (ret != ctbase_undefined);
335 ctbase_isVoidPointer (/*@dependent@*/ /*@notnull@*/ ctbase c)
337 ctbase r = ctbase_realType (c);
339 return (ctbase_isKind (r, CT_PTR) &&
340 ctype_isVoid (r->contents.base));
344 ctbase_isPointer (/*@notnull@*/ /*@dependent@*/ ctbase c)
346 ctbase r = ctbase_realType (c);
348 return (ctbase_isKind (r, CT_PTR));
352 ctbase_isEitherArray (/*@notnull@*/ ctbase c)
354 ctbase r = ctbase_realType (c);
356 return (ctbase_isKind (r, CT_ARRAY)
357 || ctbase_isKind (r, CT_FIXEDARRAY));
361 ctbase_isFixedArray (/*@notnull@*/ ctbase c)
363 ctbase r = ctbase_realType (c);
365 return (ctbase_isKind (r, CT_FIXEDARRAY));
369 ctbase_isStruct (/*@notnull@*/ ctbase c)
371 ctbase r = ctbase_realType (c);
373 return (ctbase_isKind (r, CT_STRUCT));
377 ctbase_isUnion (/*@notnull@*/ ctbase c)
379 ctbase r = ctbase_realType (c);
381 return (ctbase_isKind (r, CT_UNION));
385 ** clean this up -> typeTable should store ctype
389 ctbase_typeBaseUid (ctbase c)
393 if (ctbase_isDefined (c))
399 return ctbase_typeBaseUid (ctype_getCtbase (c->contents.base));
401 else if (ct == CT_USER || ct == CT_ABST)
403 return c->contents.tid;
405 else if (ct == CT_FIXEDARRAY)
407 return ctbase_typeBaseUid (ctype_getCtbase (c->contents.farray->base));
411 llcontbuglit ("ctbase_typeBaseUid: bad call");
412 return typeId_invalid;
415 return typeId_invalid;
419 ctbase_isBaseUA (ctbase c)
423 if (ctbase_isDefined (c))
429 return ctbase_isBaseUA (ctype_getCtbase (c->contents.base));
431 else if (ct == CT_FIXEDARRAY)
433 return ctbase_isBaseUA (ctype_getCtbase (c->contents.farray->base));
436 return (ct == CT_USER || ct == CT_ABST);
442 ctbase_typeId (ctbase c)
446 return c->contents.tid;
450 if (ctbase_isConj (c))
452 return ctbase_typeId (ctype_getCtbase (ctbase_getConjA (c)));
456 llcontbug (message ("ctbase_typeId: bad call: %q", ctbase_unparse (c)));
457 return typeId_invalid;
462 static /*@only@*/ cstring
463 ctbase_unparse (ctbase c)
465 if (ctbase_isUndefined (c)) {
466 return cstring_makeLiteral ("<<undef>>");
472 return cstring_makeLiteral ("?");
474 return cstring_copy (context_printBoolName ());
476 return (cprim_unparse (c->contents.prim));
478 return (usymtab_getTypeEntryName (c->contents.tid));
480 return (usymtab_getTypeEntryName (c->contents.tid));
482 return (message ("<expf: %t>", c->contents.base));
484 /* no spaces for multiple pointers */
486 if (ctype_isPointer (c->contents.base))
488 return (cstring_appendChar (cstring_copy (ctype_unparse (c->contents.base)), '*'));
492 return (message ("%t *", c->contents.base));
495 return (message ("%t [%d]", c->contents.farray->base,
496 (int) c->contents.farray->size));
498 return (message ("%t []", c->contents.base));
500 return (message ("[function (%q) returns %t]",
501 uentryList_unparseParams (c->contents.fcn->params),
502 c->contents.fcn->rval));
504 if (cstring_isDefined (c->contents.su->name) &&
505 !cstring_isEmpty (c->contents.su->name) &&
506 !isFakeTag (c->contents.su->name))
508 return (message ("struct %s", c->contents.su->name));
512 return (message ("struct { %q }",
513 uentryList_unparseAbbrev (c->contents.su->fields)));
516 if (cstring_isDefined (c->contents.su->name) &&
517 !cstring_isEmpty (c->contents.su->name) &&
518 !isFakeTag (c->contents.su->name))
520 return (message ("union %s", c->contents.su->name));
524 return (message ("union { %q }",
525 uentryList_unparseAbbrev (c->contents.su->fields)));
528 if (isFakeTag (c->contents.cenum->tag))
530 return (message ("enum { %q }",
531 enumNameList_unparseBrief (c->contents.cenum->members)));
535 return (message ("enum %s { %q }",
536 c->contents.cenum->tag,
537 enumNameList_unparseBrief (c->contents.cenum->members)));
540 if (c->contents.conj->isExplicit || context_getFlag (FLG_SHOWALLCONJS))
542 if (!ctype_isSimple (c->contents.conj->a) ||
543 !ctype_isSimple (c->contents.conj->b))
545 return (message ("<%t> | <%t>", c->contents.conj->a, c->contents.conj->b));
549 return (message ("%t | %t", c->contents.conj->a, c->contents.conj->b));
554 return (cstring_copy (ctype_unparse (c->contents.conj->a)));
561 static /*@only@*/ cstring
562 ctbase_unparseDeep (ctbase c)
564 if (ctbase_isUndefined (c))
566 return cstring_makeLiteral ("<<undef>>");
572 return cstring_makeLiteral ("?");
574 return cstring_copy (context_printBoolName ());
576 return (cprim_unparse (c->contents.prim));
578 if (cstring_isNonEmpty (c->contents.cenum->tag))
580 return (message ("enum %s { %q }",
581 c->contents.cenum->tag,
582 enumNameList_unparse (c->contents.cenum->members)));
586 return (message ("enum { %q }",
587 enumNameList_unparse (c->contents.cenum->members)));
590 return (usymtab_getTypeEntryName (c->contents.tid));
592 return (usymtab_getTypeEntryName (c->contents.tid));
594 return (message ("<expf: %t>", c->contents.base));
596 return (message ("%t *", c->contents.base));
598 return (message ("%t [%d]", c->contents.farray->base,
599 (int) c->contents.farray->size));
601 return (message ("%t []", c->contents.base));
603 return (message ("[function (%q) returns %t]",
604 uentryList_unparse (c->contents.fcn->params),
605 c->contents.fcn->rval));
607 return (message ("struct %s { ... } ", c->contents.su->name));
609 return (message ("union %s { ... }", c->contents.su->name));
611 return (message ("%t", c->contents.conj->a));
617 static /*@only@*/ cstring
618 ctbase_unparseNotypes (ctbase c)
620 llassert (ctbase_isDefined (c));
625 return cstring_makeLiteral ("?");
627 return cstring_copy (context_printBoolName ());
629 return (cprim_unparse (c->contents.prim));
631 if (typeId_isInvalid (c->contents.tid))
633 return cstring_makeLiteral ("enum");
637 return (message ("T#%d", c->contents.tid));
640 return (message ("uT#%d", c->contents.tid));
642 return (message ("aT#%d", c->contents.tid));
644 return (message ("<expf: %q >", ctbase_unparseNotypes (ctype_getCtbase (c->contents.base))));
646 return (message ("%q *", ctbase_unparseNotypes (ctype_getCtbase (c->contents.base))));
648 return (message ("%q []", ctbase_unparseNotypes (ctype_getCtbase (c->contents.base))));
650 return (message ("[function (%d) returns %q]", uentryList_size (c->contents.fcn->params),
651 ctbase_unparseNotypes (ctype_getCtbase (c->contents.fcn->rval))));
653 return (message ("struct %s", c->contents.su->name));
655 return (message ("union %s", c->contents.su->name));
657 return (message ("[enumlist]"));
659 return (message ("%q/%q",
660 ctbase_unparseNotypes (ctype_getCtbase (c->contents.conj->a)),
661 ctbase_unparseNotypes (ctype_getCtbase (c->contents.conj->b))));
667 static /*@only@*/ cstring
668 ctbase_unparseDeclaration (ctbase c, /*@only@*/ cstring name) /*@*/
670 if (ctbase_isUndefined (c))
678 return (message ("? %q", name));
680 return (message ("%s %q", context_printBoolName (), name));
682 return (message ("%q %q", cprim_unparse (c->contents.prim), name));
685 return (message ("%q %q", usymtab_getTypeEntryName (c->contents.tid), name));
687 llcontbuglit ("ctbase_unparseDeclaration: expfcn");
690 if (ctype_isFunction (c->contents.base))
692 return ctbase_unparseDeclaration (ctype_getCtbase (c->contents.base), name);
696 cstring s = cstring_prependChar ('*', name);
697 cstring ret = ctbase_unparseDeclaration (ctype_getCtbase (c->contents.base), s);
702 return (message ("%q[%d]",
703 ctbase_unparseDeclaration (ctype_getCtbase (c->contents.farray->base), name),
704 (int) c->contents.farray->size));
706 return (message ("%q[]",
707 ctbase_unparseDeclaration (ctype_getCtbase (c->contents.base), name)));
710 cstring s = message ("%q(%q)", name,
711 uentryList_unparseParams (c->contents.fcn->params));
713 return (ctbase_unparseDeclaration
714 (ctype_getCtbase (c->contents.fcn->rval), s));
717 if (cstring_isDefined (c->contents.su->name) &&
718 !cstring_isEmpty (c->contents.su->name) &&
719 !isFakeTag (c->contents.su->name))
721 return (message ("struct %s %q", c->contents.su->name, name));
725 return (message ("struct { %q } %q",
726 uentryList_unparseAbbrev (c->contents.su->fields),
730 if (cstring_isDefined (c->contents.su->name) &&
731 !cstring_isEmpty (c->contents.su->name) &&
732 !isFakeTag (c->contents.su->name))
734 return (message ("union %s %q", c->contents.su->name, name));
738 return (message ("union { %q } %q",
739 uentryList_unparseAbbrev (c->contents.su->fields),
743 if (isFakeTag (c->contents.cenum->tag))
745 return (message ("enum { %q } %q",
746 enumNameList_unparseBrief (c->contents.cenum->members),
751 return (message ("enum %s { %q } %q",
752 c->contents.cenum->tag,
753 enumNameList_unparseBrief (c->contents.cenum->members),
757 if (c->contents.conj->isExplicit || context_getFlag (FLG_SHOWALLCONJS))
759 if (!ctype_isSimple (c->contents.conj->a) ||
760 !ctype_isSimple (c->contents.conj->b))
762 cstring name1 = cstring_copy (name);
767 ctbase_unparseDeclaration
768 (ctype_getCtbase (c->contents.conj->a), name1),
769 ctbase_unparseDeclaration
770 (ctype_getCtbase (c->contents.conj->b), name)));
774 cstring s1 = ctbase_unparseDeclaration (ctype_getCtbase (c->contents.conj->a),
775 cstring_copy (name));
777 (message ("%q | %q", s1,
778 ctbase_unparseDeclaration (ctype_getCtbase (c->contents.conj->b),
785 return (cstring_copy (ctype_unparse (c->contents.conj->a)));
792 static ctbase ctbase_undump (d_char *c) /*@requires maxRead(*c) >= 2 @*/
802 return (ctbase_undefined);
804 return (ctbase_createUnknown ());
806 return (ctbase_createBool ());
808 res = ctbase_createPrim (cprim_fromInt (reader_getInt (c)));
809 reader_checkChar (c, '|');
812 res = ctbase_createUser (typeId_fromInt (reader_getInt (c)));
813 reader_checkChar (c, '|');
816 res = ctbase_createAbstract (typeId_fromInt (reader_getInt (c)));
817 reader_checkChar (c, '|');
820 res = ctbase_makePointer (ctype_undump (c));
821 reader_checkChar (c, '|');
824 res = ctbase_makeArray (ctype_undump (c));
825 reader_checkChar (c, '|');
829 ctype ct = ctype_undump (c);
832 reader_checkChar (c, '/');
833 size = reader_getInt (c);
834 reader_checkChar (c, '|');
835 return (ctbase_makeFixedArray (ct, size));
840 char *lp = strchr (*c, '(');
842 llassertfatal (lp != NULL);
845 ct = ctype_undump (c);
848 return (ctbase_makeLiveFunction (ct, uentryList_undump (c)));
855 char *lc = strchr (*c, '{');
857 llassert (lc != NULL);
860 sname = mstring_copy (*c);
868 i = (unsigned) atoi (sname + 1);
873 fields = uentryList_undumpFields (c, g_currentloc);
875 ctb = ctbase_createStruct (cstring_fromCharsO (sname), fields);
881 char *lc = strchr (*c, '{');
883 llassert (lc != NULL);
886 sname = mstring_copy (*c);
887 llassert (sname != NULL);
895 i = (unsigned) atoi (sname + 1);
899 return (ctbase_createUnion (cstring_fromCharsO (sname),
900 uentryList_undumpFields (c, g_currentloc)));
906 char *lc = strchr (*c, '{');
908 llassert (lc != NULL);
911 sname = mstring_copy (*c);
918 i = (unsigned) atoi (sname + 1);
922 ret = ctbase_createEnum (cstring_fromCharsO (sname),
923 enumNameList_undump (c));
931 isExplicit = bool_fromInt (reader_getInt (c));
932 reader_checkChar (c, '.');
933 c1 = ctype_undump (c);
934 reader_checkChar (c, '/');
935 c2 = ctype_undump (c);
936 reader_checkChar (c, '|');
938 return (ctbase_makeConj (c1, c2, isExplicit));
944 message ("Bad Library line (type): %s", cstring_fromChars (*c)));
946 /*drl bee: pbr*/ while (**c != '\0')
951 return ctbase_createUnknown ();
955 /* first letter of c encodes type: */
971 static /*@only@*/ cstring
972 ctbase_dump (ctbase c)
974 if (!ctbase_isDefined (c))
976 return cstring_makeLiteral ("?");
982 return cstring_makeLiteral ("u");
984 return cstring_makeLiteral ("b");
986 return (message ("p%d|", c->contents.prim));
988 return (message ("s%d|",
989 usymtab_convertId (c->contents.tid)));
991 return (message ("a%d|", usymtab_convertId (c->contents.tid)));
993 return (message ("t%q|", ctype_dump (c->contents.base)));
995 return (message ("y%q|", ctype_dump (c->contents.base)));
997 return (message ("F%q/%d|",
998 ctype_dump (c->contents.farray->base),
999 (int) c->contents.farray->size));
1001 DPRINTF (("Dump function: %s", ctbase_unparse (c)));
1002 return (message ("f%q (%q)", ctype_dump (c->contents.fcn->rval),
1003 uentryList_dumpParams (c->contents.fcn->params)));
1005 return (message ("S%s{%q}", c->contents.su->name,
1006 uentryList_dumpFields (c->contents.su->fields)));
1008 return (message ("U%s{%q}", c->contents.su->name,
1009 uentryList_dumpFields (c->contents.su->fields)));
1014 if (cstring_isNonEmpty (c->contents.cenum->tag))
1016 s = message ("e%s{%q}",
1017 c->contents.cenum->tag,
1018 enumNameList_dump (c->contents.cenum->members));
1022 s = message ("e{%q}",
1023 enumNameList_dump (c->contents.cenum->members));
1028 return (message ("C%d.%q/%q|",
1029 bool_toInt (c->contents.conj->isExplicit),
1030 ctype_dump (c->contents.conj->a),
1031 ctype_dump (c->contents.conj->b)));
1033 /* should clean them up! */
1034 return (cstring_makeLiteral ("?"));
1036 llcontbug (message ("Cannot dump: %q", ctbase_unparse (c)));
1037 return (message ("u"));
1044 static /*@only@*/ ctbase
1045 ctbase_copy (/*@notnull@*/ ctbase c)
1050 return (ctbase_createUnknown ());
1052 return (ctbase_createBool ());
1054 return (ctbase_createEnum (cstring_copy (c->contents.cenum->tag),
1055 enumNameList_copy (c->contents.cenum->members)));
1057 return (ctbase_createPrim (c->contents.prim));
1059 return (ctbase_createUser (c->contents.tid));
1061 return (ctbase_createAbstract (c->contents.tid));
1063 return (ctbase_expectFunction (c->contents.base));
1065 return (ctbase_makePointer (c->contents.base));
1067 return (ctbase_makeArray (c->contents.base));
1069 return (ctbase_makeLiveFunction (c->contents.fcn->rval,
1070 uentryList_copy (c->contents.fcn->params)));
1072 return (ctbase_createStruct (cstring_copy (c->contents.su->name),
1073 uentryList_copy (c->contents.su->fields)));
1075 return (ctbase_createUnion (cstring_copy (c->contents.su->name),
1076 uentryList_copy (c->contents.su->fields)));
1078 /*@i@*/ return (c); /* not a real copy for conj's */
1080 llbug (message ("ctbase_copy: %q", ctbase_unparse (c)));
1087 ctbase_elist (ctbase c)
1089 llassert (ctbase_isDefined (c));
1090 llassert (c->type == CT_ENUM);
1092 return (c->contents.cenum->members);
1096 ctbase_free (/*@only@*/ ctbase c)
1098 if (c == ctbase_bool || c == ctbase_unknown)
1100 /*@-mustfree@*/ return; /*@=mustfree@*/
1106 if (ctbase_isDefined (c))
1134 /*@i32@*/ /* uentryList_free (c->contents.fcn->params); */
1139 cstring_free (c->contents.su->name);
1140 uentryList_free (c->contents.su->fields);
1144 /* Don't bree conj's, */
1154 ** c should be * <unknown>
1157 static /*@only@*/ ctbase
1158 ctbase_expectFunction (ctype c)
1160 ctbase f = ctbase_new ();
1162 f->type = CT_EXPFCN;
1163 f->contents.base = c;
1169 ctbase_genMatch (ctbase c1, ctbase c2, bool force, bool arg, bool def, bool deep)
1173 /* undefined types never match */
1175 if (ctbase_isUndefined (c1) || ctbase_isUndefined (c2))
1178 /* abstract types match user types of same name */
1180 c1 = ctbase_realType (c1);
1181 c2 = ctbase_realType (c2);
1183 DPRINTF (("Matching: %s / %s", ctbase_unparse (c1),
1184 ctbase_unparse (c2)));
1189 if (c1tid == CT_CONJ)
1191 return (ctbase_genMatch (ctype_getCtbase (c1->contents.conj->a), c2,
1192 force, arg, def, deep)
1193 || ctbase_genMatch (ctype_getCtbase (c1->contents.conj->b), c2,
1194 force, arg, def, deep));
1197 if (c2tid == CT_CONJ)
1199 return (ctbase_genMatch (c1, ctype_getCtbase (c2->contents.conj->a),
1200 force, arg, def, deep)
1201 || ctbase_genMatch (c1, ctype_getCtbase (c2->contents.conj->b),
1202 force, arg, def, deep));
1206 ** if the types don't match, there are some special cases...
1211 /* unknowns match anything */
1213 if (c1tid == CT_UNKNOWN || c2tid == CT_UNKNOWN)
1218 if (c1tid == CT_FIXEDARRAY
1219 && (c2tid == CT_ARRAY || (!def && c2tid == CT_PTR)))
1221 if (ctype_isVoid (c2->contents.base))
1223 return (context_getFlag (FLG_ABSTVOIDP) ||
1224 (!(ctype_isRealAbstract (c1->contents.farray->base)) &&
1225 !(ctype_isRealAbstract (c2->contents.base))));
1228 return (ctbase_genMatch (ctype_getCtbase (c1->contents.farray->base),
1229 ctype_getCtbase (c2->contents.base),
1230 force, arg, def, deep));
1234 if (c2tid == CT_FIXEDARRAY
1235 && (c1tid == CT_ARRAY || (!def && c1tid == CT_PTR)))
1237 if (ctype_isVoid (c1->contents.base))
1239 return (context_getFlag (FLG_ABSTVOIDP) ||
1240 (!(ctype_isRealAbstract (c2->contents.farray->base)) &&
1241 !(ctype_isRealAbstract (c1->contents.base))));
1244 return (ctbase_genMatch (ctype_getCtbase (c1->contents.base),
1245 ctype_getCtbase (c2->contents.farray->base),
1246 force, arg, def, deep));
1249 /* evs 2000-07-25: Bool's may match user/abstract types */
1251 if ((c1tid == CT_BOOL
1252 && (c2tid == CT_PRIM && cprim_isInt (c2->contents.prim))) ||
1254 && (c1tid == CT_PRIM && cprim_isInt (c1->contents.prim))))
1256 return (context_msgBoolInt ());
1259 if ((c1tid == CT_BOOL && (c2tid == CT_ABST || c2tid == CT_USER))) {
1260 ctype t2c = c2->contents.base;
1262 return (ctype_isBool (t2c));
1265 if ((c2tid == CT_BOOL && (c1tid == CT_ABST || c1tid == CT_USER))) {
1266 ctype t1c = c1->contents.base;
1268 return (ctype_isBool (t1c));
1271 if ((c1tid == CT_ENUM
1272 && (c2tid == CT_PRIM && cprim_isInt (c2->contents.prim))) ||
1274 && (c1tid == CT_PRIM && cprim_isInt (c1->contents.prim))))
1276 return (context_msgEnumInt ());
1280 ** arrays and pointers...yuk!
1282 ** Considered equivalent except in definitions.
1283 ** (e.g., function parameters are equivalent)
1289 if (ctuid_isAP (c1tid) && ctuid_isAP (c2tid))
1296 ** Function pointers can be removed.
1298 ** [function ..] is equivalent to [function ..] *
1301 if (c1tid == CT_PTR && c2tid == CT_FCN)
1303 if (ctype_isFunction (ctype_realType (c1->contents.base)))
1305 c1 = ctbase_realType (ctype_getCtbaseSafe (c1->contents.base));
1310 if (c2tid == CT_PTR && c1tid == CT_FCN)
1312 if (ctype_isFunction (ctype_realType (c2->contents.base)))
1314 c2 = ctbase_realType (ctype_getCtbaseSafe (c2->contents.base));
1320 ** we allow forward declarations to structures like,
1322 ** typedef struct _t *t;
1325 ** struct _t * to match t
1328 if (context_getFlag (FLG_FORWARDDECL))
1330 if (c1tid == CT_ABST || c1tid == CT_USER)
1332 if (ctuid_isAP (c2tid))
1334 ctype ts = c2->contents.base;
1336 if (ctype_isUA (ts))
1338 typeId ttid = ctype_typeId (ts);
1339 typeId ctid = c1->contents.tid ;
1341 if (usymtab_matchForwardStruct (ctid, ttid))
1349 if (c2tid == CT_ABST || c2tid == CT_USER)
1351 if (ctuid_isAP (c1tid))
1353 ctype ts = c1->contents.base;
1355 if (ctype_isUA (ts))
1357 typeId ttid = ctype_typeId (ts);
1358 typeId ctid = c2->contents.tid ;
1360 if (usymtab_matchForwardStruct (ctid, ttid))
1379 return (cprim_closeEnoughDeep (c1->contents.prim, c2->contents.prim));
1381 return (cprim_closeEnough (c1->contents.prim, c2->contents.prim));
1386 return (typeId_equal (c1->contents.tid, c2->contents.tid));
1388 return (typeId_equal (c1->contents.tid, c2->contents.tid));
1390 return (cstring_equal (c1->contents.cenum->tag, c2->contents.cenum->tag));
1392 if (ctype_isVoid (c1->contents.base)
1393 || (ctype_isVoid (c2->contents.base)))
1395 if (ctype_isFunction (ctype_realType (c1->contents.base))
1396 || ctype_isFunction (ctype_realType (c2->contents.base)))
1398 return (!context_getFlag (FLG_CASTFCNPTR));
1402 return (context_getFlag (FLG_ABSTVOIDP) ||
1403 (!(ctype_isRealAbstract (c1->contents.base)) &&
1404 !(ctype_isRealAbstract (c2->contents.base))));
1409 /* Only allow one implicit function pointer. */
1411 if (!bool_equal (ctype_isRealPointer (c1->contents.base),
1412 ctype_isRealPointer (c2->contents.base))
1413 && (ctype_isRealFunction (c1->contents.base)
1414 || ctype_isRealFunction (c2->contents.base)))
1419 return (ctype_genMatch (c1->contents.base,
1420 c2->contents.base, force, arg, def, TRUE));
1423 if (ctype_isVoid (c1->contents.farray->base)
1424 || ctype_isVoid (c2->contents.farray->base))
1426 return (ctype_genMatch (c1->contents.farray->base,
1427 c2->contents.farray->base,
1428 force, arg, def, deep));
1430 if (ctype_isVoid (c1->contents.base) || ctype_isVoid (c2->contents.base))
1432 return (ctype_genMatch (c1->contents.base, c2->contents.base, force, arg, def, TRUE));
1434 return (ctype_genMatch (c1->contents.fcn->rval,
1435 c2->contents.fcn->rval,
1436 force, arg, def, TRUE)
1437 && uentryList_matchParams (c1->contents.fcn->params,
1438 c2->contents.fcn->params,
1442 DPRINTF (("Struct: %s / %s",
1443 c1->contents.su->name,
1444 c2->contents.su->name));
1446 if (isFakeTag (c1->contents.su->name)
1447 && isFakeTag (c2->contents.su->name))
1449 /* Both fake tags, check structure */
1450 if (cstring_equal (c1->contents.su->name, c2->contents.su->name))
1456 return uentryList_matchFields (c1->contents.su->fields,
1457 c2->contents.su->fields);
1462 if (!cstring_isEmpty (c1->contents.su->name))
1464 return (cstring_equal (c1->contents.su->name, c2->contents.su->name));
1468 if (!cstring_isEmpty (c2->contents.su->name))
1473 llcontbuglit ("ctbase_genMatch: match fields");
1478 llcontbug (message ("ctbase_genMatch: unknown type: %d\n", (int)c1tid));
1484 ** like ctbase_match, except for conjuncts:
1485 ** modifies conjuncts to match only
1489 ctbase_forceMatch (ctbase c1, ctbase c2) /*@modifies c1, c2@*/
1491 return (ctbase_genMatch (c1, c2, TRUE, FALSE, FALSE, FALSE));
1495 ctbase_match (ctbase c1, ctbase c2) /*@modifies nothing@*/
1497 return (ctbase_genMatch (c1, c2, FALSE, FALSE, FALSE, FALSE));
1501 ctbase_matchDef (ctbase c1, ctbase c2) /*@modifies nothing@*/
1503 return (ctbase_genMatch (c1, c2, FALSE, FALSE, TRUE, FALSE));
1507 ctbase_matchArg (ctbase c1, ctbase c2)
1509 return (ctbase_genMatch (c1, c2, FALSE, TRUE, FALSE, FALSE));
1512 static /*@out@*/ /*@only@*/ /*@notnull@*/ ctbase
1515 ctbase c = (ctbase) dmalloc (sizeof (*c));
1519 if (nctbases % 100 == 0 && nctbases > lastnc)
1521 llmsg (message ("ctbases: %d", nctbases));
1528 static /*@only@*/ ctbase
1529 ctbase_createPrim (cprim p)
1531 ctbase c = ctbase_new ();
1534 c->contents.prim = p;
1539 static /*@observer@*/ ctbase
1540 ctbase_getBool (void)
1542 /*@i@*/ return ctbase_createBool ();
1546 ctbase_createBool ()
1548 if (!ctbase_isDefined (ctbase_bool))
1550 ctbase_bool = ctbase_new ();
1551 ctbase_bool->type = CT_BOOL;
1552 ctbase_bool->contents.prim = CTX_BOOL;
1555 /*@-retalias@*/ /*@-globstate@*/
1557 /*@=retalias@*/ /*@=globstate@*/
1560 static /*@only@*/ ctbase
1561 ctbase_createUser (typeId u)
1563 ctbase c = ctbase_new ();
1566 c->contents.tid = u;
1568 llassert (typeId_isValid (u));
1573 static /*@only@*/ ctbase
1574 ctbase_createEnum (/*@keep@*/ cstring etag, /*@keep@*/ enumNameList emembers)
1576 ctbase c = ctbase_new ();
1580 if (cstring_isUndefined (etag))
1582 llcontbuglit ("Undefined enum tag!");
1586 c->contents.cenum = (tenum) dmalloc (sizeof (*c->contents.cenum));
1587 c->contents.cenum->tag = etag;
1588 c->contents.cenum->members = emembers;
1593 static /*@observer@*/ cstring
1594 ctbase_enumTag (/*@notnull@*/ ctbase ct)
1596 return (ct->contents.cenum->tag);
1599 static /*@only@*/ ctbase
1600 ctbase_createAbstract (typeId u)
1602 ctbase c = ctbase_new ();
1605 c->contents.tid = u;
1607 /* also check its abstract? */
1609 llassert (typeId_isValid (c->contents.tid));
1614 static /*@only@*/ ctbase
1615 ctbase_createUnknown (void)
1617 if (!ctbase_isDefined (ctbase_unknown))
1619 ctbase_unknown = ctbase_new ();
1620 ctbase_unknown->type = CT_UNKNOWN;
1621 ctbase_unknown->contents.prim = CTX_UNKNOWN;
1624 /*@-retalias@*/ /*@-globstate@*/
1625 return ctbase_unknown;
1626 /*@=retalias@*/ /*@=globstate@*/
1630 ** requires: result is not assigned to b
1631 ** (should copy, but no way to reclaim storage)
1634 static /*@only@*/ ctbase
1635 ctbase_makePointer (ctype b)
1637 ctbase c = ctbase_new ();
1640 c->contents.base = b;
1645 static /*@only@*/ ctbase
1646 ctbase_makeArray (ctype b)
1648 ctbase c = ctbase_new ();
1651 c->contents.base = b;
1656 static /*@notnull@*/ /*@only@*/ ctbase
1657 ctbase_makeFixedArray (ctype b, long size)
1659 ctbase c = ctbase_new ();
1661 c->type = CT_FIXEDARRAY;
1663 c->contents.farray = (tfixed) dmalloc (sizeof (*c->contents.farray));
1664 c->contents.farray->base = b;
1665 c->contents.farray->size = size;
1671 ctbase_makeFunction (ctype b, /*@only@*/ uentryList p)
1673 ctbase c = ctbase_new ();
1677 c->contents.fcn = (cfcn) dmalloc (sizeof (*c->contents.fcn));
1679 if (ctype_isFunction (b)) /* was: && ctype_isPointer (b)) */
1684 if (ctype_isPointer (b))
1686 ctb = ctype_getCtbase (ctype_baseArrayPtr (b));
1690 ctb = ctype_getCtbase (b);
1693 llassert (ctbase_isDefined (ctb));
1694 llassert (ctb->type == CT_FCN);
1696 rval = ctype_makeFunction (ctb->contents.fcn->rval, p);
1698 c->contents.fcn->rval = rval;
1699 c->contents.fcn->params = uentryList_copy (ctb->contents.fcn->params); /* no copy before */
1703 c->contents.fcn->rval = b;
1704 c->contents.fcn->params = uentryList_copy (p); /* no copy before */
1705 /*@-branchstate@*/ /* p is really released on this branch */
1709 ct = cttable_addComplex (c);
1710 return (ct); /* was: ctype_makePointer (ct)); */
1714 ctbase_makeNFFunction (ctype b, /*@only@*/ uentryList p)
1716 ctbase c = ctbase_new ();
1720 c->contents.fcn = (cfcn) dmalloc (sizeof (*c->contents.fcn));
1722 if (ctype_isFunction (b)) /* was && ctype_isPointer (b)) */
1727 if (ctype_isPointer (b))
1729 ctb = ctype_getCtbase (ctype_baseArrayPtr (b));
1733 ctb = ctype_getCtbase (b);
1736 llassert (ctbase_isDefined (ctb));
1737 llassert (ctb->type == CT_FCN);
1739 rval = ctype_makeNFParamsFunction (ctb->contents.fcn->rval, p);
1741 c->contents.fcn->rval = rval;
1742 c->contents.fcn->params = uentryList_copy (ctb->contents.fcn->params);
1746 c->contents.fcn->rval = b;
1747 c->contents.fcn->params = uentryList_copy (p);
1752 ct = cttable_addComplex (c);
1753 return (ct); /* was: ctype_makePointer (ct)); */
1756 static /*@only@*/ ctbase
1757 ctbase_makeLiveFunction (ctype b, /*@only@*/ uentryList p)
1759 ctbase c = ctbase_new ();
1763 c->contents.fcn = (cfcn) dmalloc (sizeof (*c->contents.fcn));
1764 c->contents.fcn->rval = b;
1765 c->contents.fcn->params = p;
1767 /*@-mustfree@*/ return (c); /*@=mustfree@*/
1770 static /*@observer@*/ /*@notnull@*/ ctbase
1771 ctbase_realFunction (/*@dependent@*/ /*@notnull@*/ ctbase c)
1775 if (c->type == CT_FCN)
1780 llassert (ctbase_isFunction (c));
1782 res = ctype_getCtbase (c->contents.base);
1784 llassert (ctbase_isDefined (res));
1790 ctbase_isFunction (ctbase c)
1792 llassert (c != ctbase_undefined);
1794 if (c->type == CT_FCN)
1800 if (c->type == CT_PTR)
1802 ctbase fcn = ctype_getCtbase (ctbase_baseArrayPtr (c));
1804 return (ctbase_isDefined (fcn) && fcn->type == CT_FCN);
1811 /* doesn't copy c1 and c2 */
1813 static /*@only@*/ ctbase
1814 ctbase_makeConj (ctype c1, ctype c2, bool isExplicit)
1816 ctbase c = ctbase_new ();
1820 c->contents.conj = (tconj) dmalloc (sizeof (*c->contents.conj));
1821 c->contents.conj->a = c1;
1822 c->contents.conj->b = c2;
1823 c->contents.conj->isExplicit = isExplicit;
1829 ctbase_getConjA (/*@notnull@*/ ctbase c)
1831 llassert (c->type == CT_CONJ);
1832 return (c->contents.conj->a);
1836 ctbase_getConjB (/*@notnull@*/ ctbase c)
1838 llassert (c->type == CT_CONJ);
1839 return (c->contents.conj->b);
1843 ctbase_isExplicitConj (/*@notnull@*/ ctbase c)
1845 llassert (c->type == CT_CONJ);
1846 return (c->contents.conj->isExplicit);
1849 static /*@only@*/ ctbase
1850 ctbase_createStruct (/*@only@*/ cstring n, /*@only@*/ uentryList f)
1852 ctbase c = ctbase_new ();
1854 c->type = CT_STRUCT;
1856 c->contents.su = (tsu) dmalloc (sizeof (*c->contents.su));
1857 c->contents.su->name = n;
1858 c->contents.su->fields = f;
1863 static /*@observer@*/ uentryList
1864 ctbase_getuentryList (/*@notnull@*/ ctbase c)
1866 c = ctbase_realType (c);
1868 if (!(c->type == CT_STRUCT || c->type == CT_UNION))
1869 llfatalbug (message ("ctbase_getuentryList: bad invocation: %q", ctbase_unparse (c)));
1871 return (c->contents.su->fields);
1875 ctbase_createUnion (/*@keep@*/ cstring n, /*@only@*/ uentryList f)
1877 ctbase c = ctbase_new ();
1881 c->contents.su = (tsu) dmalloc (sizeof (*c->contents.su));
1882 c->contents.su->name = n;
1883 c->contents.su->fields = f;
1889 ctbase_baseArrayPtr (/*@notnull@*/ ctbase c)
1892 c = ctbase_realType (c);
1895 if (ct == CT_FIXEDARRAY)
1897 return c->contents.farray->base;
1901 llassert (ctuid_isAP (ct));
1903 return c->contents.base;
1908 ctbase_baseFunction (/*@notnull@*/ ctbase c)
1911 c = ctbase_realFunction (c);
1913 if (c->type != CT_FCN)
1915 llfatalbug (message ("ctbase_baseFunction: bad call: %q", ctbase_unparse (c)));
1918 return (c->contents.fcn->rval);
1922 ctbase_argsFunction (/*@notnull@*/ ctbase c)
1925 c = ctbase_realFunction (c);
1927 if (c->type != CT_FCN)
1929 llfatalbug (message ("ctbase_argsFunction: bad call: %q",
1930 ctbase_unparse (c)));
1932 return (c->contents.fcn->params);
1936 ctbase_baseisExpFcn (ctype c)
1939 c = ctype_removePointers (c);
1941 cb = ctype_getCtbase (c);
1942 llassert (ctbase_isDefined (cb));
1944 if (cb->type == CT_FCN)
1946 c = ctype_removePointers (ctype_getReturnType (c));
1948 cb = ctype_getCtbase (c);
1949 llassert (ctbase_isDefined (cb));
1951 return (cb->type == CT_EXPFCN);
1957 ** ctbase_newBase behaves specially when p is a CONJ:
1959 ** c -> conj (newBase (c, p.a), p.b)
1963 ctbase_newBase (ctype c, ctype p)
1967 DPRINTF (("New base: %s / %s", ctype_unparse (c), ctype_unparse (p)));
1969 if (ctype_isUndefined (c) || ctype_isUnknown (c))
1974 cb = ctype_getCtbase (c);
1976 if (ctype_isConj (p))
1978 ctbase pb = ctype_getCtbase (p);
1980 llassert (ctbase_isDefined (pb));
1982 if (pb->contents.conj->isExplicit)
1984 return (ctype_makeExplicitConj (ctype_newBase (c, pb->contents.conj->a),
1985 pb->contents.conj->b));
1990 return (ctype_makeConj (ctype_newBase (c, pb->contents.conj->a),
1991 pb->contents.conj->b));
1996 if (ctbase_baseisExpFcn (c))
1998 return (ctbase_newBaseExpFcn (c, p));
2001 llassert (ctbase_isDefined (cb));
2020 cbn = ctbase_newBase (cb->contents.base, p);
2021 ret = ctype_makePointer (cbn);
2026 return (ctype_makeFixedArray (ctbase_newBase (cb->contents.farray->base, p),
2027 cb->contents.farray->size));
2029 return (ctype_makeArray (ctbase_newBase (cb->contents.base, p)));
2031 return (ctype_makeRawFunction (ctbase_newBase (cb->contents.fcn->rval, p),
2032 cb->contents.fcn->params));
2034 return (ctype_makeConjAux (ctbase_newBase (cb->contents.conj->a, p),
2035 ctbase_newBase (cb->contents.conj->b, p),
2036 cb->contents.conj->isExplicit));
2038 llcontbug (message ("ctbase_newBase: bad ctbase: %q", ctbase_unparse (cb)));
2045 ctbase_newBaseExpFcn (ctype c, ctype p)
2047 ctbase cb = ctype_getCtbase (c);
2050 ctype fp = ctype_unknown;
2051 uentryList ctargs = ctype_argsFunction (c);
2054 ** okay, this is really ugly...
2056 ** pointers inside <expf> mean pointers to the function;
2057 ** pointers outside <expf> are pointers to the return value;
2058 ** because its a function there is one superfluous pointer.
2062 ** bf is a ctype, used to derived structure of cb
2065 if (!ctbase_isFunction (cb))
2066 llbuglit ("ctbase_newBaseExpFcn: expFcn -> not a function");
2068 tmpct = ctype_getBaseType (ctype_getReturnType (c));
2071 ** pointers before expfcn -> p are pointers to function, not result
2075 tcb = ctype_getCtbase (tmpct);
2077 llassert (ctbase_isDefined (tcb));
2078 tmpct = tcb->contents.base;
2081 ** record pointers to base in fp
2084 while (!ctype_isUnknown (tmpct))
2086 if (ctype_isExpFcn (tmpct)) {
2087 ctbase ttcb = ctype_getCtbase (tmpct);
2090 ** evs 2000-05-16: This is necessary to deal with function pointers in parens.
2091 ** The whole function pointer parsing is a major kludge, but it seems to work,
2092 ** and I'm only embarrassed by it when I haven't look at the C spec recently...
2095 llassert (ctbase_isDefined (ttcb));
2096 tmpct = ttcb->contents.base;
2097 llassert (!ctype_isUnknown (tmpct));
2100 switch (ctype_getCtKind (tmpct))
2103 fp = ctype_makePointer (fp);
2104 /*@switchbreak@*/ break;
2106 fp = ctype_makeArray (fp);
2107 /*@switchbreak@*/ break;
2110 ctbase fbase = ctype_getCtbase (tmpct);
2112 if (ctbase_isFunction (fbase))
2114 fp = ctype_makeFunction (fp, uentryList_copy (ctargs));
2115 ctargs = ctbase_argsFunction (fbase);
2121 ("ctbase_newBaseExpFcn: fixing expfunction: bad complex type: %s [base: %q]",
2122 ctype_unparse (tmpct), ctbase_unparse (fbase)));
2129 (message ("ctbase_newBaseExpFcn: fixing expfunction: bad type: %s",
2130 ctype_unparse (tmpct)));
2134 tmpct = ctype_baseArrayPtr (tmpct);
2138 tmpct = ctype_getReturnType (c);
2141 ** pointers to expf are pointers to return value
2144 while (!ctype_isExpFcn (tmpct))
2146 switch (ctype_getCtKind (tmpct))
2149 p = ctype_makePointer (p);
2150 /*@switchbreak@*/ break;
2152 p = ctype_makeArray (p);
2153 /*@switchbreak@*/ break;
2156 ctbase fbase = ctype_getCtbase (tmpct);
2158 if (ctbase_isFunction (fbase))
2160 p = ctype_makeFunction (p, uentryList_copy (ctbase_argsFunction (fbase)));
2166 ("ctbase_newBaseExpFcn: fixing expfunction: bad complex type: %s",
2167 ctype_unparse (tmpct)));
2175 (message ("ctbase_newBaseExpFcn: fixing expfunction2: bad type: %t",
2180 tmpct = ctype_baseArrayPtr (tmpct);
2186 ** pointers to fp are pointers to function type
2189 ret = ctype_makeRawFunction (p, uentryList_copy (ctargs));
2191 while (ctype_getCtKind (fp) > CTK_PLAIN)
2193 switch (ctype_getCtKind (fp))
2196 ret = ctype_makePointer (ret);
2197 /*@switchbreak@*/ break;
2199 ret = ctype_makeArray (ret);
2200 /*@switchbreak@*/ break;
2203 ctbase fbase = ctype_getCtbase (fp);
2205 if (ctbase_isFunction (fbase))
2208 ctype_makeFunction (ret,
2209 uentryList_copy (ctbase_argsFunction (fbase)));
2220 llcontbug (message ("post-fixing expfunction: bad type: %t", fp));
2224 fp = ctype_baseArrayPtr (fp);
2232 ** returns lowest level base of c: plain type
2235 static /*@notnull@*/ /*@only@*/ ctbase
2236 ctbase_getBaseType (/*@notnull@*/ ctbase c)
2250 return (ctbase_copy (c));
2254 return (ctbase_getBaseType (ctype_getCtbaseSafe (c->contents.base)));
2257 return (ctbase_getBaseType (ctype_getCtbaseSafe (c->contents.farray->base)));
2258 case CT_CONJ: /* base type of A conj branch? */
2259 return (ctbase_getBaseType (ctype_getCtbaseSafe (c->contents.conj->a)));
2261 return (ctbase_copy (c));
2264 llfatalbug (message ("ctbase_getBaseType: bad ctbase: %q", ctbase_unparse (c)));
2271 ctbase_compare (ctbase c1, ctbase c2, bool strict)
2275 if (ctbase_isUndefined (c1) || ctbase_isUndefined (c2))
2277 llcontbuglit ("ctbase_compare: undefined ctbase");
2294 return (int_compare (c1->contents.prim, c2->contents.prim));
2298 return (int_compare (c1->contents.tid, c2->contents.tid));
2301 case CT_ENUM: /* for now, keep like abstract */
2303 return (int_compare (c1->contents.tid, c2->contents.tid));
2305 return (ctype_compare (c1->contents.base, c2->contents.base));
2307 INTCOMPARERETURN (c1->contents.farray->size, c2->contents.farray->size);
2309 return (ctype_compare (c1->contents.farray->base,
2310 c2->contents.farray->base));
2312 return (ctype_compare (c1->contents.base, c2->contents.base));
2315 COMPARERETURN (ctype_compare (c1->contents.fcn->rval, c2->contents.fcn->rval));
2319 return (uentryList_compareStrict (c1->contents.fcn->params,
2320 c2->contents.fcn->params));
2324 return (uentryList_compareParams (c1->contents.fcn->params,
2325 c2->contents.fcn->params));
2329 return (ctype_compare (c1->contents.base, c2->contents.base));
2332 /* evs 2000-07-28: this block was missing! */
2334 int ncmp = cstring_compare (c1->contents.su->name,
2335 c2->contents.su->name);
2338 if (isFakeTag (c1->contents.su->name)
2339 && isFakeTag (c2->contents.su->name)) {
2340 ; /* If they are both fake struct tags, don't require match. */
2347 DPRINTF (("Comparing fields: %s / %s",
2348 ctbase_unparse (c1),
2349 ctbase_unparse (c2)));
2351 return (uentryList_compareFields (c1->contents.su->fields,
2352 c2->contents.su->fields));
2355 COMPARERETURN (ctype_compare (c1->contents.conj->a,
2356 c2->contents.conj->a));
2357 COMPARERETURN (ctype_compare (c1->contents.conj->b,
2358 c2->contents.conj->b));
2359 return (bool_compare (c1->contents.conj->isExplicit,
2360 c2->contents.conj->isExplicit));
2367 ctbase_compareStrict (/*@notnull@*/ ctbase c1, /*@notnull@*/ ctbase c2)
2369 return (ctbase_compare (c1, c2, TRUE));
2372 static bool ctbase_equivStrict (/*@notnull@*/ ctbase c1, /*@notnull@*/ ctbase c2)
2374 return (ctbase_compareStrict (c1,c2) == 0);
2377 static bool ctbase_equiv (/*@notnull@*/ ctbase c1, /*@notnull@*/ ctbase c2)
2379 return (ctbase_compare (c1, c2, FALSE) == 0);
2383 ctbase_isKind (/*@notnull@*/ ctbase c, ctuid kind)
2391 return (ctbase_isKind (ctype_getCtbaseSafe (c->contents.conj->a), kind) ||
2392 ctbase_isKind (ctype_getCtbaseSafe (c->contents.conj->b), kind));
2398 ctbase_isKind2 (/*@notnull@*/ ctbase c, ctuid kind1, ctuid kind2)
2402 if (ck == kind1 || ck == kind2)
2406 return (ctbase_isKind2 (ctype_getCtbaseSafe (c->contents.conj->a), kind1, kind2) ||
2407 ctbase_isKind2 (ctype_getCtbaseSafe (c->contents.conj->b), kind1, kind2));
2413 ctbase_isAbstract (/*@notnull@*/ ctbase c)
2415 return (c->type == CT_ABST);
2418 static bool ctbase_isUA (ctbase c)
2420 return (ctbase_isDefined (c) && ((c)->type == CT_USER || (c)->type == CT_ABST));
2424 ctbase_almostEqual (ctbase c1, ctbase c2)
2428 /* undefined types never match */
2430 if (ctbase_isUndefined (c1) || ctbase_isUndefined (c2))
2436 if (c1tid == CT_FIXEDARRAY && c2tid == CT_ARRAY)
2438 return (ctbase_almostEqual (ctype_getCtbase (c1->contents.farray->base),
2439 ctype_getCtbase (c2->contents.base)));
2442 if (c2tid == CT_FIXEDARRAY && c1tid == CT_ARRAY)
2444 return (ctbase_almostEqual (ctype_getCtbase (c1->contents.base),
2445 ctype_getCtbase (c2->contents.farray->base)));
2456 return (cprim_equal (c1->contents.prim, c2->contents.prim));
2460 return (typeId_equal (c1->contents.tid, c2->contents.tid));
2462 return (typeId_equal (c1->contents.tid, c2->contents.tid));
2464 return (cstring_equal (c1->contents.cenum->tag, c2->contents.cenum->tag));
2466 return (ctype_almostEqual (c1->contents.base, c2->contents.base));
2468 return (ctype_almostEqual (c1->contents.farray->base,
2469 c2->contents.farray->base));
2471 return (ctype_almostEqual (c1->contents.base, c2->contents.base));
2473 return (ctype_almostEqual (c1->contents.fcn->rval, c2->contents.fcn->rval)
2474 && uentryList_matchParams (c1->contents.fcn->params,
2475 c2->contents.fcn->params, FALSE, TRUE));
2478 if (!cstring_isEmpty (c1->contents.su->name))
2480 return (cstring_equal (c1->contents.su->name, c2->contents.su->name));
2484 if (!cstring_isEmpty (c2->contents.su->name))
2489 llcontbuglit ("ctbase_almostEqual: match fields");
2493 llcontbug (message ("ctbase_almostEqual: unknown type: %d\n", (int)c1tid));
2498 /*drl added July 02, 001
2499 called by ctype_getArraySize
2502 long int ctbase_getArraySize (ctbase ctb)
2504 llassert (ctbase_isDefined (ctb) );
2506 llassert (ctbase_isFixedArray(ctb) );
2508 return (ctb->contents.farray->size);