]> andersk Git - splint.git/blob - src/ctbase.i
Fixed handling of array dimensions (bug reported by Randi J. Relander)
[splint.git] / src / ctbase.i
1 /* ;-*-C-*-; 
2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 University of Virginia,
4 **         Massachusetts Institute of Technology
5 **
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.
10 ** 
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.
15 ** 
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.
19 **
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
23 */
24 /*
25 ** ctbase.i
26 **
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.)
30 */
31
32 /*@access cprim*/
33
34 abst_typedef /*@null@*/ struct s_ctbase *ctbase;
35
36 /*@private@*/ typedef struct {
37   ctkind kind;
38   ctbase ctbase; 
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 */
43 } *ctentry ;
44
45 typedef /*@only@*/ ctentry o_ctentry;
46           
47 typedef struct {
48   int size;
49   int nspace;
50   /*@relnull@*/ /*@only@*/ o_ctentry *entries;
51   /* memoize matches...maybe in context? */
52 } cttable ;
53
54 extern bool ctentry_isBogus (/*@sef@*/ ctentry p_c) /*@*/;
55 # define ctentry_isBogus(c) \
56    ((c)->kind == CTK_INVALID || (c)->kind == CTK_DNE)
57
58 static cttable cttab = { 0, 0, NULL };
59
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) /*@*/ ;
67
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) /*@*/ ;
73
74 /* 
75 ** These are file-static macros (used in ctype.c).  No way to
76 ** declare them as static in C.
77 */
78
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))
90
91 # define ctbase_fixUser(c)        (c = ctbase_realType(c))
92 /*@=allmacros@*/ /*@=macrospec@*/ /*@=namechecks@*/
93
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) /*@*/ ;
112
113 static /*@notnull@*/ /*@only@*/ ctbase
114   ctbase_createStruct (/*@only@*/ cstring p_n, /*@only@*/ uentryList p_f);
115
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) /*@*/ ;
137
138 /*@-macroundef@*/
139 extern int cttable_lastIndex();
140 # define cttable_lastIndex()  (cttab.size - 1)
141 /*@=macroundef@*/
142
143 typedef struct
144 {
145   ctype rval;
146   /*@only@*/ uentryList params; 
147 } *cfcn; 
148
149 typedef struct
150 {
151   cstring   name;
152   uentryList fields;
153 } *tsu;
154
155 typedef struct
156 {
157   ctype a;
158   ctype b;
159   bool  isExplicit;
160 } *tconj;
161
162 typedef struct
163 {
164   cstring      tag;
165   enumNameList members;
166 } *tenum;
167
168 typedef struct
169 {
170   ctype base;
171   long size;
172 } *tfixed;
173  
174 typedef union 
175 {
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 */
184 } uconts;
185
186 struct s_ctbase
187 {
188   ctuid    type;
189   uconts   contents;
190 } ;
191
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) /*@*/ ;
199
200 static /*@falsenull@*/ bool ctbase_isFunction(ctbase p_c) /*@*/ ;
201
202 /*@constant null ctbase ctbase_undefined; @*/
203 # define ctbase_undefined        ((ctbase)0)
204
205 static /*@owned@*/ ctbase ctbase_bool = ctbase_undefined;
206 static /*@owned@*/ ctbase ctbase_unknown = ctbase_undefined;
207
208 static /*@falsenull@*/ bool ctbase_isDefined (ctbase c) /*@*/
209 {
210   return ((c) != ctbase_undefined);
211 }
212
213 static /*@truenull@*/ bool ctbase_isUndefined (ctbase c)
214 {
215   return ((c) == ctbase_undefined);
216 }
217
218 static ctkind ctype_getCtKind (ctype c)
219 {
220   ctentry ce = ctype_getCtentry (c);
221
222   return ctentry_getKind (ce);
223 }
224
225 static bool ctbase_isUser (ctbase c)
226 {
227   if (ctbase_isDefined (c))
228     {
229       return (ctbase_isKind (c, CT_USER));
230     }
231   else
232     {
233       return FALSE;
234     }
235 }
236
237 static bool ctbase_isEnum (ctbase c)
238 {
239   if (ctbase_isDefined (c))
240     {
241       return (ctbase_isKind (c, CT_ENUM));
242     }
243   else
244     {
245       return FALSE;
246     }
247 }
248
249 static bool ctbase_isExpFcn (ctbase c)
250 {
251   if (ctbase_isDefined (c))
252     {
253       return (c->type == CT_EXPFCN);
254     }
255   else
256     {
257       return FALSE;
258     }
259 }
260
261 static /*@falsenull@*/ bool ctbase_isConj (ctbase c)
262 {
263   if (ctbase_isDefined (c))
264     {
265       return (c->type == CT_CONJ);
266     }
267   else
268     {
269       return FALSE;
270     }
271 }
272
273 static bool ctuid_isAP (ctuid c) /*@*/ 
274 {
275   return (c == CT_ARRAY || c == CT_PTR);
276 }
277
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) /*@*/ ;
295
296 static /*@out@*/ /*@notnull@*/ /*@only@*/ ctbase ctbase_new (void) /*@*/ ;
297 static int nctbases = 0;
298
299 static /*@notnull@*/ /*@only@*/ 
300   ctbase ctbase_makeLiveFunction (ctype p_b, /*@only@*/ uentryList p_p);
301
302 static bool ctbase_isUnnamedSU (ctbase c)
303 {
304   return (ctbase_isDefined (c)
305           && (ctbase_isStruct (c) || ctbase_isUnion (c))
306           && isFakeTag (c->contents.su->name));
307 }
308
309 static /*@observer@*/ ctbase ctbase_realType (ctbase c)
310 {
311   if (ctbase_isUA (c))
312     {
313       typeId uid = ctbase_typeId (c);
314
315       if (usymtab_isBoolType (uid))
316         {
317           return ctbase_getBool ();
318         }
319       else
320         {
321           ctbase ret = ctype_getCtbase 
322             (uentry_getRealType (usymtab_getTypeEntry (ctbase_typeId (c))));
323           
324           llassert (ret != ctbase_undefined);
325           return ret;
326         }
327     }
328   else
329     {
330       return c;
331     }
332 }
333
334 static bool
335 ctbase_isVoidPointer (/*@dependent@*/ /*@notnull@*/ ctbase c)
336 {
337   ctbase r = ctbase_realType (c);
338
339   return (ctbase_isKind (r, CT_PTR) &&
340           ctype_isVoid (r->contents.base));
341 }
342
343 static bool
344 ctbase_isPointer (/*@notnull@*/ /*@dependent@*/ ctbase c)
345 {
346   ctbase r = ctbase_realType (c);
347
348   return (ctbase_isKind (r, CT_PTR));
349 }
350
351 static bool
352 ctbase_isEitherArray (/*@notnull@*/ ctbase c)
353 {
354   ctbase r = ctbase_realType (c);
355
356   return (ctbase_isKind (r, CT_ARRAY)
357           || ctbase_isKind (r, CT_FIXEDARRAY));
358 }
359
360 static bool
361 ctbase_isFixedArray (/*@notnull@*/ ctbase c)
362 {
363   ctbase r = ctbase_realType (c);
364
365   return (ctbase_isKind (r, CT_FIXEDARRAY));
366 }
367
368 static bool
369 ctbase_isStruct (/*@notnull@*/ ctbase c)
370 {
371   ctbase r = ctbase_realType (c);
372
373   return (ctbase_isKind (r, CT_STRUCT));
374 }
375
376 static bool
377 ctbase_isUnion (/*@notnull@*/ ctbase c)
378 {
379   ctbase r = ctbase_realType (c);
380
381   return (ctbase_isKind (r, CT_UNION));
382 }
383
384 /*
385 ** clean this up -> typeTable should store ctype
386 */
387
388 static typeId
389 ctbase_typeBaseUid (ctbase c)
390 {
391   ctuid ct;
392
393   if (ctbase_isDefined (c))
394     {
395       ct = c->type;
396
397       if (ctuid_isAP (ct))
398         {
399           return ctbase_typeBaseUid (ctype_getCtbase (c->contents.base));
400         }
401       else if (ct == CT_USER || ct == CT_ABST)
402         {
403           return c->contents.tid;
404         }
405       else if (ct == CT_FIXEDARRAY)
406         {
407           return ctbase_typeBaseUid (ctype_getCtbase (c->contents.farray->base));
408         }
409       else
410         {
411           llcontbuglit ("ctbase_typeBaseUid: bad call");
412           return typeId_invalid;
413         }
414     }
415   return typeId_invalid;
416 }
417
418 static bool
419 ctbase_isBaseUA (ctbase c)
420 {
421   ctuid ct;
422
423   if (ctbase_isDefined (c))
424     {
425       ct = c->type;
426
427       if (ctuid_isAP (ct))
428         {
429           return ctbase_isBaseUA (ctype_getCtbase (c->contents.base));
430         }
431       else if (ct == CT_FIXEDARRAY)
432         {
433           return ctbase_isBaseUA (ctype_getCtbase (c->contents.farray->base));
434         }
435       else
436         return (ct == CT_USER || ct == CT_ABST);
437     }
438   return FALSE;
439 }
440
441 static typeId
442 ctbase_typeId (ctbase c)
443 {
444   if (ctbase_isUA (c))
445     {
446       return c->contents.tid;
447     }
448   else
449     {
450       if (ctbase_isConj (c)) 
451         {
452           return ctbase_typeId (ctype_getCtbase (ctbase_getConjA (c)));
453         }
454       else
455         {
456           llcontbug (message ("ctbase_typeId: bad call: %q", ctbase_unparse (c)));
457           return typeId_invalid;
458         }
459     }
460 }
461
462 static /*@only@*/ cstring
463 ctbase_unparse (ctbase c)
464 {
465   if (ctbase_isUndefined (c)) {
466     return cstring_makeLiteral ("<<undef>>");
467   }
468   
469   switch (c->type)
470     {
471     case CT_UNKNOWN:
472       return cstring_makeLiteral ("?");
473     case CT_BOOL:
474       return cstring_copy (context_printBoolName ());
475     case CT_PRIM:
476       return (cprim_unparse (c->contents.prim));
477     case CT_USER:
478       return (usymtab_getTypeEntryName (c->contents.tid));
479     case CT_ABST:
480       return (usymtab_getTypeEntryName (c->contents.tid));
481     case CT_EXPFCN:
482       return (message ("<expf: %t>", c->contents.base));
483     case CT_PTR:
484       /* no spaces for multiple pointers */
485
486       if (ctype_isPointer (c->contents.base))
487         {
488           return (cstring_appendChar (cstring_copy (ctype_unparse (c->contents.base)), '*'));
489         }
490       else
491         {
492           return (message ("%t *", c->contents.base));
493         }
494     case CT_FIXEDARRAY:
495       return (message ("%t [%d]", 
496                        c->contents.farray->base, 
497                        (int) c->contents.farray->size));
498     case CT_ARRAY:
499       return (message ("%t []", c->contents.base));
500     case CT_FCN:
501       return (message ("[function (%q) returns %t]",
502                        uentryList_unparseParams (c->contents.fcn->params),
503                        c->contents.fcn->rval));
504     case CT_STRUCT:
505       if (cstring_isDefined (c->contents.su->name) &&
506           !cstring_isEmpty (c->contents.su->name) &&
507           !isFakeTag (c->contents.su->name))
508         {
509           return (message ("struct %s", c->contents.su->name));
510         }
511       else
512         {
513           return (message ("struct { %q }", 
514                            uentryList_unparseAbbrev (c->contents.su->fields))); 
515         }
516     case CT_UNION:
517       if (cstring_isDefined (c->contents.su->name) &&
518           !cstring_isEmpty (c->contents.su->name) &&
519           !isFakeTag (c->contents.su->name))
520         {
521           return (message ("union %s", c->contents.su->name));
522         }
523       else
524         {
525           return (message ("union { %q }", 
526                            uentryList_unparseAbbrev (c->contents.su->fields)));
527         }
528     case CT_ENUM:
529       if (isFakeTag (c->contents.cenum->tag))
530         {
531           return (message ("enum { %q }", 
532                            enumNameList_unparseBrief (c->contents.cenum->members)));
533         }
534       else
535         {
536           return (message ("enum %s { %q }", 
537                            c->contents.cenum->tag,
538                            enumNameList_unparseBrief (c->contents.cenum->members)));
539         }
540     case CT_CONJ:
541       if (c->contents.conj->isExplicit || context_getFlag (FLG_SHOWALLCONJS))
542         {
543           if (!ctype_isSimple (c->contents.conj->a) ||
544               !ctype_isSimple (c->contents.conj->b))
545             {
546               return (message ("<%t> | <%t>", c->contents.conj->a, c->contents.conj->b));
547             }
548           else
549             {
550               return (message ("%t | %t", c->contents.conj->a, c->contents.conj->b));
551             }
552         }
553       else
554         {
555           return (cstring_copy (ctype_unparse (c->contents.conj->a)));
556         }
557     BADDEFAULT;
558     }
559   BADEXIT;
560 }
561
562 static /*@only@*/ cstring
563   ctbase_unparseDeep (ctbase c)
564 {
565   if (ctbase_isUndefined (c))
566     {
567       return cstring_makeLiteral ("<<undef>>");
568     }
569
570   switch (c->type)
571     {
572     case CT_UNKNOWN:
573       return cstring_makeLiteral ("?");
574     case CT_BOOL:
575       return cstring_copy (context_printBoolName ());
576     case CT_PRIM:
577       return (cprim_unparse (c->contents.prim));
578     case CT_ENUM:
579       if (cstring_isNonEmpty (c->contents.cenum->tag))
580         {
581           return (message ("enum %s { %q }",
582                            c->contents.cenum->tag,
583                            enumNameList_unparse (c->contents.cenum->members)));
584         }
585       else
586         {
587           return (message ("enum { %q }",
588                            enumNameList_unparse (c->contents.cenum->members)));
589         }
590     case CT_USER:
591       return (usymtab_getTypeEntryName (c->contents.tid));
592     case CT_ABST:
593       return (usymtab_getTypeEntryName (c->contents.tid));
594     case CT_EXPFCN:
595       return (message ("<expf: %t>", c->contents.base));
596     case CT_PTR:
597       return (message ("%t *", c->contents.base));
598     case CT_FIXEDARRAY:
599       return (message ("%t [%d]", c->contents.farray->base, 
600                        (int) c->contents.farray->size));
601     case CT_ARRAY:
602       return (message ("%t []", c->contents.base));
603     case CT_FCN:
604       return (message ("[function (%q) returns %t]",
605                        uentryList_unparse (c->contents.fcn->params),
606                        c->contents.fcn->rval));
607     case CT_STRUCT:
608       return (message ("struct %s { ... } ", c->contents.su->name));
609     case CT_UNION:
610       return (message ("union %s { ... }", c->contents.su->name));
611     case CT_CONJ:
612       return (message ("%t", c->contents.conj->a));
613     BADDEFAULT;
614     }
615   BADEXIT;
616 }
617
618 static /*@only@*/ cstring
619 ctbase_unparseNotypes (ctbase c)
620 {
621   llassert (ctbase_isDefined (c));
622
623   switch (c->type)
624     {
625     case CT_UNKNOWN:
626       return cstring_makeLiteral ("?");
627     case CT_BOOL:
628       return cstring_copy (context_printBoolName ());
629     case CT_PRIM:
630       return (cprim_unparse (c->contents.prim));
631     case CT_ENUM:
632       if (typeId_isInvalid (c->contents.tid))
633         {
634           return cstring_makeLiteral ("enum");
635         }
636       else
637         {
638           return (message ("T#%d", c->contents.tid));
639         }
640     case CT_USER:
641       return (message ("uT#%d", c->contents.tid));
642     case CT_ABST:
643       return (message ("aT#%d", c->contents.tid));
644     case CT_EXPFCN:
645       return (message ("<expf: %q >", ctbase_unparseNotypes (ctype_getCtbase (c->contents.base))));
646     case CT_PTR:
647       return (message ("%q  *", ctbase_unparseNotypes (ctype_getCtbase (c->contents.base))));
648     case CT_ARRAY:
649       return (message ("%q []", ctbase_unparseNotypes (ctype_getCtbase (c->contents.base))));
650     case CT_FCN:
651       return (message ("[function (%d) returns %q]", uentryList_size (c->contents.fcn->params),
652                        ctbase_unparseNotypes (ctype_getCtbase (c->contents.fcn->rval))));
653     case CT_STRUCT:
654       return (message ("struct %s", c->contents.su->name));
655     case CT_UNION:
656       return (message ("union %s", c->contents.su->name));
657     case CT_ENUMLIST:
658       return (message ("[enumlist]"));
659     case CT_CONJ:
660       return (message ("%q/%q", 
661                        ctbase_unparseNotypes (ctype_getCtbase (c->contents.conj->a)),
662                        ctbase_unparseNotypes (ctype_getCtbase (c->contents.conj->b))));
663     BADDEFAULT;
664     }
665   BADEXIT;
666 }
667
668 static /*@only@*/ cstring
669 ctbase_unparseDeclaration (ctbase c, /*@only@*/ cstring name) /*@*/ 
670 {
671   if (ctbase_isUndefined (c))
672     {
673       return name;
674     }
675   
676   switch (c->type)
677     {
678     case CT_UNKNOWN:
679       return (message ("? %q", name));
680     case CT_BOOL:
681       return (message ("%s %q", context_printBoolName (), name));
682     case CT_PRIM:
683       return (message ("%q %q", cprim_unparse (c->contents.prim), name));
684     case CT_USER:
685     case CT_ABST:
686       return (message ("%q %q", usymtab_getTypeEntryName (c->contents.tid), name));
687     case CT_EXPFCN:
688       llcontbuglit ("ctbase_unparseDeclaration: expfcn");
689       return name;
690     case CT_PTR:
691       if (ctype_isFunction (c->contents.base))
692         {
693           return ctbase_unparseDeclaration (ctype_getCtbase (c->contents.base), name);
694         }
695       else
696         {
697           cstring s = cstring_prependChar ('*', name);
698           cstring ret = ctbase_unparseDeclaration (ctype_getCtbase (c->contents.base), s);
699           cstring_free (name);
700           return (ret);
701         }
702     case CT_FIXEDARRAY:
703       return (message ("%q[%d]", 
704                        ctbase_unparseDeclaration (ctype_getCtbase (c->contents.farray->base), name),
705                        (int) c->contents.farray->size));
706     case CT_ARRAY:
707       return (message ("%q[]", 
708                        ctbase_unparseDeclaration (ctype_getCtbase (c->contents.base), name)));
709     case CT_FCN:
710         {
711           cstring s = message ("%q(%q)", name, 
712                                uentryList_unparseParams (c->contents.fcn->params));
713
714           return (ctbase_unparseDeclaration 
715                   (ctype_getCtbase (c->contents.fcn->rval), s));
716         }
717     case CT_STRUCT:
718       if (cstring_isDefined (c->contents.su->name) &&
719           !cstring_isEmpty (c->contents.su->name) &&
720           !isFakeTag (c->contents.su->name))
721         {
722           return (message ("struct %s %q", c->contents.su->name, name));
723         }
724       else
725         {
726           return (message ("struct { %q } %q", 
727                            uentryList_unparseAbbrev (c->contents.su->fields),
728                            name));
729         }
730     case CT_UNION:
731       if (cstring_isDefined (c->contents.su->name) &&
732           !cstring_isEmpty (c->contents.su->name) &&
733           !isFakeTag (c->contents.su->name))
734         {
735           return (message ("union %s %q", c->contents.su->name, name));
736         }
737       else
738         {
739           return (message ("union { %q } %q", 
740                            uentryList_unparseAbbrev (c->contents.su->fields),
741                            name));
742         }
743     case CT_ENUM:
744       if (isFakeTag (c->contents.cenum->tag))
745         {
746           return (message ("enum { %q } %q", 
747                            enumNameList_unparseBrief (c->contents.cenum->members),
748                            name));
749         }
750       else
751         {
752           return (message ("enum %s { %q } %q", 
753                            c->contents.cenum->tag,
754                            enumNameList_unparseBrief (c->contents.cenum->members),
755                            name));
756         }
757     case CT_CONJ:      
758       if (c->contents.conj->isExplicit || context_getFlag (FLG_SHOWALLCONJS))
759         {
760           if (!ctype_isSimple (c->contents.conj->a) ||
761               !ctype_isSimple (c->contents.conj->b))
762             {
763               cstring name1 = cstring_copy (name);
764
765               return 
766                 (message 
767                  ("<%q> | <%q>", 
768                   ctbase_unparseDeclaration 
769                   (ctype_getCtbase (c->contents.conj->a), name1),
770                   ctbase_unparseDeclaration 
771                   (ctype_getCtbase (c->contents.conj->b), name)));
772             }
773           else
774             {
775               cstring s1 = ctbase_unparseDeclaration (ctype_getCtbase (c->contents.conj->a), 
776                                                       cstring_copy (name));
777               return
778                 (message ("%q | %q", s1, 
779                           ctbase_unparseDeclaration (ctype_getCtbase (c->contents.conj->b),
780                                                      name)));
781             }
782         }
783       else
784         {
785           cstring_free (name);
786           return (cstring_copy (ctype_unparse (c->contents.conj->a)));
787         }
788       BADDEFAULT;
789     }
790   BADEXIT;
791 }
792
793 static ctbase ctbase_undump (d_char *c) /*@requires maxRead(*c) >= 2 @*/
794 {
795   ctbase res;
796   char p = **c;
797
798   (*c)++;
799
800   switch (p)
801     {
802     case '?':
803       return (ctbase_undefined);
804     case 'u':
805       return (ctbase_createUnknown ());
806     case 'b':
807       return (ctbase_createBool ());
808     case 'p':
809       res = ctbase_createPrim (cprim_fromInt (reader_getInt (c)));
810       reader_checkChar (c, '|');
811       return res;
812     case 's':
813       res = ctbase_createUser (typeId_fromInt (reader_getInt (c)));
814       reader_checkChar (c, '|');
815       return res;
816     case 'a':
817       res = ctbase_createAbstract (typeId_fromInt (reader_getInt (c)));
818       reader_checkChar (c, '|');
819       return res;
820     case 't':
821       res = ctbase_makePointer (ctype_undump (c));
822       reader_checkChar (c, '|');
823       return res;
824     case 'y':
825       res = ctbase_makeArray (ctype_undump (c));
826       reader_checkChar (c, '|');
827       return res;
828     case 'F':
829       {
830         ctype ct = ctype_undump (c);
831         int size;
832
833         reader_checkChar (c, '/');
834         size = reader_getInt (c);
835         reader_checkChar (c, '|');
836         return (ctbase_makeFixedArray (ct, size));
837       }
838     case 'f':
839       {
840         ctype ct;
841         char *lp = strchr (*c, '(');
842
843         llassertfatal (lp != NULL);
844
845         *lp = '\0';
846         ct = ctype_undump (c);
847         *c = lp + 1;
848
849         return (ctbase_makeLiveFunction (ct, uentryList_undump (c)));
850       }
851     case 'S':
852       {
853         uentryList fields;
854         ctbase ctb;
855         char *sname;
856         char *lc = strchr (*c, '{');
857
858         llassert (lc != NULL);
859         *lc = '\0';
860
861         sname = mstring_copy (*c);
862
863         *c = lc + 1;
864
865         if (*sname == '!')
866           {
867             unsigned int i;
868
869             i = (unsigned) atoi (sname + 1);
870
871             setTagNo (i);
872           }
873
874         fields = uentryList_undumpFields (c, g_currentloc);
875
876         ctb = ctbase_createStruct (cstring_fromCharsO (sname), fields);
877         return ctb;
878       }
879     case 'U':
880       {
881         char *sname;
882         char *lc = strchr (*c, '{');
883
884         llassert (lc != NULL);
885
886         *lc = '\0';
887         sname = mstring_copy (*c);
888         llassert (sname != NULL);
889
890         *c = lc + 1;
891
892         if (*sname == '!')
893           {
894             unsigned int i;
895
896             i = (unsigned) atoi (sname + 1);
897             setTagNo (i);
898           }
899
900         return (ctbase_createUnion (cstring_fromCharsO (sname),
901                                     uentryList_undumpFields (c, g_currentloc)));
902       }
903     case 'e':
904       {
905         ctbase ret;
906         char *sname;
907         char *lc = strchr (*c, '{');
908
909         llassert (lc != NULL);
910
911         *lc = '\0';
912         sname = mstring_copy (*c);
913         *c = lc + 1;
914
915         if (*sname == '!')
916           {
917             unsigned int i;
918
919             i = (unsigned) atoi (sname + 1);
920             setTagNo (i);
921           }
922
923         ret = ctbase_createEnum (cstring_fromCharsO (sname),
924                                  enumNameList_undump (c));
925         return ret;
926       }
927     case 'C':
928       {
929         bool isExplicit;
930         ctype c1, c2;
931
932         isExplicit = bool_fromInt (reader_getInt (c));
933         reader_checkChar (c, '.');
934         c1 = ctype_undump (c);
935         reader_checkChar (c, '/');
936         c2 = ctype_undump (c);
937         reader_checkChar (c, '|');
938
939         return (ctbase_makeConj (c1, c2, isExplicit));
940       }
941
942     default:
943       (*c)--;
944       llerror (FLG_SYNTAX, 
945                message ("Bad Library line (type): %s", cstring_fromChars (*c)));
946
947   /*drl bee: pbr*/      while (**c != '\0')
948         {
949           (*c)++;
950         }
951
952       return ctbase_createUnknown ();
953     }
954 }
955
956 /* first letter of c encodes type:     */
957 /*    u  unknown                       */
958 /*    b  bool                          */
959 /*    p  prim                          */
960 /*    e  enum                          */
961 /*    l  enumList                      */
962 /*    s  uSer                          */
963 /*    a  abstract                      */
964 /*    t  poinTer                       */
965 /*    y  arraY                         */
966 /*    F  Fixed array                   */
967 /*    f  function                      */
968 /*    S  structure                     */
969 /*    U  union                         */
970 /*    C  conj                          */
971
972 static /*@only@*/ cstring
973 ctbase_dump (ctbase c)
974 {
975   if (!ctbase_isDefined (c))
976     {
977       return cstring_makeLiteral ("?");
978     }
979
980   switch (c->type)
981     {
982     case CT_UNKNOWN:
983       return cstring_makeLiteral ("u");
984     case CT_BOOL:
985       return cstring_makeLiteral ("b");
986     case CT_PRIM:
987       return (message ("p%d|", c->contents.prim));
988     case CT_USER:
989       return (message ("s%d|", 
990                        usymtab_convertId (c->contents.tid)));
991     case CT_ABST:
992       return (message ("a%d|", usymtab_convertId (c->contents.tid)));
993     case CT_PTR:
994       return (message ("t%q|", ctype_dump (c->contents.base)));
995     case CT_ARRAY:
996       return (message ("y%q|", ctype_dump (c->contents.base)));
997     case CT_FIXEDARRAY:
998       return (message ("F%q/%d|", 
999                        ctype_dump (c->contents.farray->base),
1000                        (int) c->contents.farray->size));
1001     case CT_FCN:
1002       DPRINTF (("Dump function: %s", ctbase_unparse (c)));
1003       return (message ("f%q (%q)", ctype_dump (c->contents.fcn->rval),
1004                        uentryList_dumpParams (c->contents.fcn->params)));
1005     case CT_STRUCT:
1006       return (message ("S%s{%q}", c->contents.su->name,
1007                        uentryList_dumpFields (c->contents.su->fields)));
1008     case CT_UNION:
1009       return (message ("U%s{%q}", c->contents.su->name,
1010                        uentryList_dumpFields (c->contents.su->fields)));
1011     case CT_ENUM:
1012       {
1013         cstring s;
1014         
1015         if (cstring_isNonEmpty (c->contents.cenum->tag))
1016           {
1017             s = message ("e%s{%q}", 
1018                          c->contents.cenum->tag,
1019                          enumNameList_dump (c->contents.cenum->members));
1020           }
1021         else
1022           {
1023             s = message ("e{%q}", 
1024                          enumNameList_dump (c->contents.cenum->members));
1025           }
1026         return (s);
1027       }
1028     case CT_CONJ:
1029       return (message ("C%d.%q/%q|", 
1030                        bool_toInt (c->contents.conj->isExplicit),
1031                        ctype_dump (c->contents.conj->a),
1032                        ctype_dump (c->contents.conj->b)));
1033     case CT_EXPFCN:
1034       /* should clean them up! */
1035       return (cstring_makeLiteral ("?"));
1036     case CT_ENUMLIST:
1037       llcontbug (message ("Cannot dump: %q", ctbase_unparse (c))); 
1038       return (message ("u"));
1039     BADDEFAULT;
1040     }
1041
1042   BADEXIT;
1043 }
1044
1045 static /*@only@*/ ctbase
1046 ctbase_copy (/*@notnull@*/ ctbase c)
1047 {
1048   switch (c->type)
1049     {
1050     case CT_UNKNOWN:
1051       return (ctbase_createUnknown ());
1052     case CT_BOOL:
1053       return (ctbase_createBool ());
1054     case CT_ENUM:
1055       return (ctbase_createEnum (cstring_copy (c->contents.cenum->tag), 
1056                                  enumNameList_copy (c->contents.cenum->members)));
1057     case CT_PRIM:
1058       return (ctbase_createPrim (c->contents.prim));
1059     case CT_USER:
1060       return (ctbase_createUser (c->contents.tid));
1061     case CT_ABST:
1062       return (ctbase_createAbstract (c->contents.tid));
1063     case CT_EXPFCN:
1064       return (ctbase_expectFunction (c->contents.base));
1065     case CT_PTR:
1066       return (ctbase_makePointer (c->contents.base));
1067     case CT_ARRAY:
1068       return (ctbase_makeArray (c->contents.base));
1069     case CT_FCN:
1070       return (ctbase_makeLiveFunction (c->contents.fcn->rval,
1071                                        uentryList_copy (c->contents.fcn->params)));     
1072     case CT_STRUCT:
1073       return (ctbase_createStruct (cstring_copy (c->contents.su->name),
1074                                    uentryList_copy (c->contents.su->fields)));
1075     case CT_UNION:
1076       return (ctbase_createUnion (cstring_copy (c->contents.su->name),
1077                                   uentryList_copy (c->contents.su->fields)));
1078     case CT_CONJ:
1079       /*@i@*/ return (c); /* not a real copy for conj's */
1080     default:
1081       llbug (message ("ctbase_copy: %q", ctbase_unparse (c)));
1082     }
1083
1084   BADEXIT;
1085 }
1086                                 
1087 static enumNameList
1088 ctbase_elist (ctbase c)
1089 {
1090   llassert (ctbase_isDefined (c));
1091   llassert (c->type == CT_ENUM);
1092
1093   return (c->contents.cenum->members);
1094 }
1095
1096 static void
1097 ctbase_free (/*@only@*/ ctbase c)
1098 {
1099   if (c == ctbase_bool || c == ctbase_unknown)
1100     {
1101       /*@-mustfree@*/ return; /*@=mustfree@*/
1102     }
1103
1104   --nctbases;
1105   
1106     
1107   if (ctbase_isDefined (c))
1108     {
1109       switch (c->type)
1110         {
1111         case CT_UNKNOWN:
1112           sfree (c);
1113           break;
1114         case CT_PRIM:
1115           sfree (c);
1116           break;
1117         case CT_ENUM:
1118           sfree (c);
1119           break;
1120         case CT_ENUMLIST:
1121           /* sfree list? */
1122           sfree (c);
1123           break;
1124         case CT_USER:
1125           sfree (c);
1126           break;
1127         case CT_ABST:
1128           sfree (c);
1129           break;
1130         case CT_PTR:
1131         case CT_ARRAY:
1132           sfree (c);
1133           break;
1134         case CT_FCN:
1135           /*@i32@*/ /* uentryList_free (c->contents.fcn->params); */
1136           sfree (c);
1137           break;
1138         case CT_STRUCT:
1139         case CT_UNION:
1140           cstring_free (c->contents.su->name);
1141           uentryList_free (c->contents.su->fields); 
1142           sfree (c);
1143           break;
1144         case CT_CONJ:
1145           /* Don't bree conj's, */
1146           break;
1147         default:
1148           sfree (c);
1149           break;
1150         }
1151     }
1152 }
1153
1154 /*
1155 ** c should be * <unknown>
1156 */
1157
1158 static /*@only@*/ ctbase
1159 ctbase_expectFunction (ctype c)
1160 {
1161   ctbase f = ctbase_new ();
1162
1163   f->type = CT_EXPFCN;
1164   f->contents.base = c;
1165
1166   return (f);
1167 }
1168
1169 static bool
1170 ctbase_genMatch (ctbase c1, ctbase c2, bool force, bool arg, bool def, bool deep)
1171 {
1172   ctuid c1tid, c2tid;
1173   
1174   /* undefined types never match */
1175   
1176   if (ctbase_isUndefined (c1) || ctbase_isUndefined (c2))
1177     return FALSE;
1178   
1179   /* abstract types match user types of same name */
1180
1181   c1 = ctbase_realType (c1);
1182   c2 = ctbase_realType (c2);
1183   
1184   DPRINTF (("Matching: %s / %s", ctbase_unparse (c1),
1185             ctbase_unparse (c2)));
1186   
1187   c1tid = c1->type;
1188   c2tid = c2->type;
1189   
1190   if (c1tid == CT_CONJ)
1191     {
1192       return (ctbase_genMatch (ctype_getCtbase (c1->contents.conj->a), c2,
1193                                force, arg, def, deep)
1194               || ctbase_genMatch (ctype_getCtbase (c1->contents.conj->b), c2,
1195                                   force, arg, def, deep));
1196     }
1197   
1198   if (c2tid == CT_CONJ)
1199     {
1200       return (ctbase_genMatch (c1, ctype_getCtbase (c2->contents.conj->a),
1201                                force, arg, def, deep)
1202               || ctbase_genMatch (c1, ctype_getCtbase (c2->contents.conj->b),
1203                                   force, arg, def, deep));
1204     }
1205
1206   /*
1207   ** if the types don't match, there are some special cases...
1208   */
1209
1210   if (c1tid != c2tid)
1211     {
1212       /* unknowns match anything */
1213       
1214       if (c1tid == CT_UNKNOWN || c2tid == CT_UNKNOWN)
1215         {
1216           return TRUE;
1217         }
1218
1219       if (c1tid == CT_FIXEDARRAY 
1220           && (c2tid == CT_ARRAY || (!def && c2tid == CT_PTR)))
1221         {
1222           if (ctype_isVoid (c2->contents.base))
1223             {
1224               return (context_getFlag (FLG_ABSTVOIDP) ||
1225                       (!(ctype_isRealAbstract (c1->contents.farray->base)) &&
1226                        !(ctype_isRealAbstract (c2->contents.base))));
1227             }
1228
1229           return (ctbase_genMatch (ctype_getCtbase (c1->contents.farray->base),
1230                                    ctype_getCtbase (c2->contents.base),
1231                                    force, arg, def, deep));
1232         }
1233
1234
1235       if (c2tid == CT_FIXEDARRAY 
1236           && (c1tid == CT_ARRAY || (!def && c1tid == CT_PTR)))
1237         {
1238           if (ctype_isVoid (c1->contents.base))
1239             {
1240               return (context_getFlag (FLG_ABSTVOIDP) ||
1241                       (!(ctype_isRealAbstract (c2->contents.farray->base)) &&
1242                        !(ctype_isRealAbstract (c1->contents.base))));
1243             }
1244
1245           return (ctbase_genMatch (ctype_getCtbase (c1->contents.base),
1246                                    ctype_getCtbase (c2->contents.farray->base),
1247                                    force, arg, def, deep));
1248         }
1249       
1250       /* evs 2000-07-25: Bool's may match user/abstract types */
1251
1252       if ((c1tid == CT_BOOL 
1253            && (c2tid == CT_PRIM && cprim_isInt (c2->contents.prim))) ||
1254           (c2tid == CT_BOOL 
1255            && (c1tid == CT_PRIM && cprim_isInt (c1->contents.prim))))
1256         {
1257           return (context_msgBoolInt ());
1258         }
1259
1260       if ((c1tid == CT_BOOL && (c2tid == CT_ABST || c2tid == CT_USER))) {
1261         ctype t2c = c2->contents.base;
1262
1263         return (ctype_isBool (t2c));
1264       }
1265
1266       if ((c2tid == CT_BOOL && (c1tid == CT_ABST || c1tid == CT_USER))) {
1267         ctype t1c = c1->contents.base;
1268
1269         return (ctype_isBool (t1c));
1270       }
1271
1272       if ((c1tid == CT_ENUM
1273            && (c2tid == CT_PRIM && cprim_isInt (c2->contents.prim))) ||
1274           (c2tid == CT_ENUM
1275            && (c1tid == CT_PRIM && cprim_isInt (c1->contents.prim))))
1276         {
1277           return (context_msgEnumInt ());
1278         }
1279
1280       /*
1281       ** arrays and pointers...yuk!
1282       **
1283       ** Considered equivalent except in definitions.  
1284       ** (e.g., function parameters are equivalent)
1285       **
1286       */
1287       
1288       if (!def)
1289         {
1290           if (ctuid_isAP (c1tid) && ctuid_isAP (c2tid))
1291             {
1292               c2tid = c1tid;
1293             }
1294         }
1295
1296       /*
1297       ** Function pointers can be removed.
1298       **
1299       ** [function ..] is equivalent to [function ..] *
1300       */
1301
1302       if (c1tid == CT_PTR && c2tid == CT_FCN)
1303         {
1304           if (ctype_isFunction (ctype_realType (c1->contents.base)))
1305             {
1306               c1 = ctbase_realType (ctype_getCtbaseSafe (c1->contents.base));
1307               c1tid = c1->type;
1308             }
1309         }
1310
1311       if (c2tid == CT_PTR && c1tid == CT_FCN)
1312         {
1313           if (ctype_isFunction (ctype_realType (c2->contents.base)))
1314             {
1315               c2 = ctbase_realType (ctype_getCtbaseSafe (c2->contents.base));
1316               c2tid = c2->type;
1317             }
1318         }
1319
1320       /*
1321       ** we allow forward declarations to structures like,
1322       **
1323       **          typedef struct _t *t;
1324       ** 
1325       ** to allow,
1326       **           struct _t * to match t
1327       */
1328   
1329       if (context_getFlag (FLG_FORWARDDECL))
1330         {
1331           if (c1tid == CT_ABST || c1tid == CT_USER)
1332             {
1333               if (ctuid_isAP (c2tid))
1334                 {
1335                   ctype ts = c2->contents.base;
1336                   
1337                   if (ctype_isUA (ts))
1338                     {
1339                       typeId ttid = ctype_typeId (ts);
1340                       typeId ctid = c1->contents.tid ;
1341                       
1342                       if (usymtab_matchForwardStruct (ctid, ttid))
1343                         {
1344                           return TRUE;
1345                         }
1346                     }
1347                 }
1348             }
1349           
1350           if (c2tid == CT_ABST || c2tid == CT_USER)
1351             {
1352               if (ctuid_isAP (c1tid))
1353                 {
1354                   ctype ts = c1->contents.base;
1355                   
1356                   if (ctype_isUA (ts))
1357                     {
1358                       typeId ttid = ctype_typeId (ts);
1359                       typeId ctid = c2->contents.tid ;
1360                       
1361                       if (usymtab_matchForwardStruct (ctid, ttid))
1362                         {
1363                           return TRUE;
1364                         }
1365                     }
1366                 }
1367             }
1368         }
1369     }
1370
1371   if (c1tid != c2tid)
1372     return FALSE;
1373
1374   switch (c1tid)
1375     {
1376     case CT_UNKNOWN:
1377       return (TRUE);
1378     case CT_PRIM:
1379       if (deep) {
1380         return (cprim_closeEnoughDeep (c1->contents.prim, c2->contents.prim));
1381       } else {
1382         return (cprim_closeEnough (c1->contents.prim, c2->contents.prim));
1383       }
1384     case CT_BOOL:
1385       return (TRUE);
1386     case CT_ABST:
1387       return (typeId_equal (c1->contents.tid, c2->contents.tid));
1388     case CT_USER:
1389       return (typeId_equal (c1->contents.tid, c2->contents.tid));
1390     case CT_ENUM:       
1391       return (cstring_equal (c1->contents.cenum->tag, c2->contents.cenum->tag));
1392     case CT_PTR:
1393       if (ctype_isVoid (c1->contents.base)
1394           || (ctype_isVoid (c2->contents.base)))
1395         {
1396           if (ctype_isFunction (ctype_realType (c1->contents.base))
1397               || ctype_isFunction (ctype_realType (c2->contents.base)))
1398             {
1399               return (!context_getFlag (FLG_CASTFCNPTR));
1400             }
1401           else
1402             {
1403               return (context_getFlag (FLG_ABSTVOIDP) ||
1404                       (!(ctype_isRealAbstract (c1->contents.base)) &&
1405                        !(ctype_isRealAbstract (c2->contents.base))));
1406             }
1407         }
1408       else
1409         {
1410           /* Only allow one implicit function pointer. */
1411                           
1412           if (!bool_equal (ctype_isRealPointer (c1->contents.base),
1413                            ctype_isRealPointer (c2->contents.base))
1414               && (ctype_isRealFunction (c1->contents.base)
1415                   || ctype_isRealFunction (c2->contents.base)))
1416             {
1417               return FALSE;
1418             }
1419
1420           return (ctype_genMatch (c1->contents.base,
1421                                   c2->contents.base, force, arg, def, TRUE));
1422         }
1423     case CT_FIXEDARRAY:
1424       if (ctype_isVoid (c1->contents.farray->base) 
1425           || ctype_isVoid (c2->contents.farray->base))
1426         return TRUE;
1427       return (ctype_genMatch (c1->contents.farray->base, 
1428                               c2->contents.farray->base, 
1429                               force, arg, def, deep));
1430     case CT_ARRAY:
1431       if (ctype_isVoid (c1->contents.base) || ctype_isVoid (c2->contents.base))
1432         return TRUE;
1433       return (ctype_genMatch (c1->contents.base, c2->contents.base, force, arg, def, TRUE));
1434     case CT_FCN:
1435       return (ctype_genMatch (c1->contents.fcn->rval, 
1436                               c2->contents.fcn->rval, 
1437                               force, arg, def, TRUE) 
1438               && uentryList_matchParams (c1->contents.fcn->params, 
1439                                          c2->contents.fcn->params, 
1440                                          force, TRUE));
1441     case CT_STRUCT:
1442     case CT_UNION:
1443       DPRINTF (("Struct: %s / %s",
1444                 c1->contents.su->name,
1445                 c2->contents.su->name));
1446
1447       if (isFakeTag (c1->contents.su->name)
1448           && isFakeTag (c2->contents.su->name))
1449         {
1450           /* Both fake tags, check structure */
1451           if (cstring_equal (c1->contents.su->name, c2->contents.su->name))
1452             {
1453               return TRUE;
1454             }
1455           else
1456             {
1457               return uentryList_matchFields (c1->contents.su->fields, 
1458                                              c2->contents.su->fields);
1459             }
1460         }
1461       else
1462         {
1463           if (!cstring_isEmpty (c1->contents.su->name))
1464             {
1465               return (cstring_equal (c1->contents.su->name, c2->contents.su->name));
1466             }
1467           else
1468             {
1469               if (!cstring_isEmpty (c2->contents.su->name))
1470                 {
1471                   return FALSE;
1472                 }
1473               
1474               llcontbuglit ("ctbase_genMatch: match fields");
1475               return (FALSE);
1476             }
1477         }
1478     default:
1479       llcontbug (message ("ctbase_genMatch: unknown type: %d\n", (int)c1tid));
1480       return (FALSE);
1481     }
1482 }
1483
1484 /*
1485 ** like ctbase_match, except for conjuncts:
1486 **   modifies conjuncts to match only
1487 */
1488
1489 static bool
1490 ctbase_forceMatch (ctbase c1, ctbase c2) /*@modifies c1, c2@*/
1491 {
1492   return (ctbase_genMatch (c1, c2, TRUE, FALSE, FALSE, FALSE));
1493 }
1494
1495 static bool
1496 ctbase_match (ctbase c1, ctbase c2) /*@modifies nothing@*/
1497 {
1498   return (ctbase_genMatch (c1, c2, FALSE, FALSE, FALSE, FALSE));
1499 }
1500
1501 static bool
1502 ctbase_matchDef (ctbase c1, ctbase c2) /*@modifies nothing@*/
1503 {
1504   return (ctbase_genMatch (c1, c2, FALSE, FALSE, TRUE, FALSE));
1505 }
1506
1507 static bool
1508 ctbase_matchArg (ctbase c1, ctbase c2)
1509 {
1510   return (ctbase_genMatch (c1, c2, FALSE, TRUE, FALSE, FALSE));
1511 }
1512
1513 static /*@out@*/ /*@only@*/ /*@notnull@*/ ctbase 
1514 ctbase_new ()
1515 {
1516   ctbase c = (ctbase) dmalloc (sizeof (*c));
1517
1518   nctbases++;
1519  /*
1520    if (nctbases % 100 == 0 && nctbases > lastnc)
1521      {
1522        llmsg (message ("ctbases: %d", nctbases));
1523        lastnc = nctbases;
1524      }
1525  */
1526   return (c);
1527 }
1528
1529 static /*@only@*/ ctbase
1530 ctbase_createPrim (cprim p)
1531 {
1532   ctbase c = ctbase_new ();
1533
1534   c->type = CT_PRIM;
1535   c->contents.prim = p;
1536
1537   return (c);
1538 }
1539
1540 static /*@observer@*/ ctbase
1541 ctbase_getBool (void)
1542 {
1543   /*@i@*/ return ctbase_createBool ();
1544 }
1545
1546 static ctbase
1547 ctbase_createBool ()
1548 {
1549   if (!ctbase_isDefined (ctbase_bool))
1550     {
1551       ctbase_bool = ctbase_new ();
1552       ctbase_bool->type = CT_BOOL;
1553       ctbase_bool->contents.prim = CTX_BOOL;
1554     }
1555
1556   /*@-retalias@*/ /*@-globstate@*/
1557   return ctbase_bool;
1558   /*@=retalias@*/ /*@=globstate@*/
1559 }
1560
1561 static /*@only@*/ ctbase
1562 ctbase_createUser (typeId u)
1563 {
1564   ctbase c = ctbase_new ();
1565
1566   c->type = CT_USER;
1567   c->contents.tid = u;
1568
1569   llassert (typeId_isValid (u));
1570
1571   return (c);
1572 }
1573
1574 static /*@only@*/ ctbase
1575 ctbase_createEnum (/*@keep@*/ cstring etag, /*@keep@*/ enumNameList emembers)
1576 {
1577   ctbase c = ctbase_new ();
1578
1579   c->type = CT_ENUM;
1580
1581   if (cstring_isUndefined (etag))
1582     {
1583       llcontbuglit ("Undefined enum tag!");
1584       etag = fakeTag ();
1585     }
1586
1587   c->contents.cenum = (tenum) dmalloc (sizeof (*c->contents.cenum));
1588   c->contents.cenum->tag = etag;
1589   c->contents.cenum->members = emembers;
1590
1591   return (c);
1592 }
1593
1594 static /*@observer@*/ cstring
1595 ctbase_enumTag (/*@notnull@*/ ctbase ct)
1596 {
1597   return (ct->contents.cenum->tag);
1598 }
1599
1600 static /*@only@*/ ctbase
1601 ctbase_createAbstract (typeId u)
1602 {
1603   ctbase c = ctbase_new ();
1604
1605   c->type = CT_ABST;
1606   c->contents.tid = u;
1607
1608  /* also check its abstract? */
1609
1610   llassert (typeId_isValid (c->contents.tid));
1611
1612   return (c);
1613 }
1614
1615 static /*@only@*/ ctbase
1616 ctbase_createUnknown (void)
1617 {
1618   if (!ctbase_isDefined (ctbase_unknown))
1619     {
1620       ctbase_unknown = ctbase_new ();
1621       ctbase_unknown->type = CT_UNKNOWN;
1622       ctbase_unknown->contents.prim = CTX_UNKNOWN;
1623     }
1624
1625   /*@-retalias@*/ /*@-globstate@*/
1626   return ctbase_unknown;
1627   /*@=retalias@*/ /*@=globstate@*/
1628 }
1629
1630 /*
1631 ** requires: result is not assigned to b
1632 **           (should copy, but no way to reclaim storage)
1633 */
1634
1635 static /*@only@*/ ctbase
1636 ctbase_makePointer (ctype b)
1637 {
1638   ctbase c = ctbase_new ();
1639
1640   c->type = CT_PTR;
1641   c->contents.base = b;
1642
1643   return (c);
1644 }
1645
1646 static /*@only@*/ ctbase
1647 ctbase_makeArray (ctype b)
1648 {
1649   ctbase c = ctbase_new ();
1650
1651   c->type = CT_ARRAY;
1652   c->contents.base = b;
1653
1654   return (c);
1655 }
1656
1657 static /*@notnull@*/ /*@only@*/ ctbase
1658 ctbase_makeFixedArray (ctype b, long size)
1659 {
1660   ctbase c = ctbase_new ();
1661
1662   c->type = CT_FIXEDARRAY;
1663
1664   c->contents.farray = (tfixed) dmalloc (sizeof (*c->contents.farray));
1665   c->contents.farray->base = b;
1666   c->contents.farray->size = size;
1667
1668   return (c);
1669 }
1670
1671 static ctype
1672 ctbase_makeFunction (ctype b, /*@only@*/ uentryList p)
1673 {
1674   ctbase c = ctbase_new ();
1675   ctype ct;
1676   
1677   c->type = CT_FCN;
1678   c->contents.fcn = (cfcn) dmalloc (sizeof (*c->contents.fcn));
1679
1680   if (ctype_isFunction (b)) /* was: && ctype_isPointer (b)) */
1681     {
1682       ctbase ctb;
1683       ctype rval;
1684       
1685       if (ctype_isPointer (b))
1686         {
1687           ctb  = ctype_getCtbase (ctype_baseArrayPtr (b));
1688         }
1689       else
1690         {
1691           ctb = ctype_getCtbase (b);
1692         }
1693       
1694       llassert (ctbase_isDefined (ctb));
1695       llassert (ctb->type == CT_FCN);
1696       
1697       rval = ctype_makeFunction (ctb->contents.fcn->rval, p);
1698
1699       c->contents.fcn->rval = rval;
1700       c->contents.fcn->params = uentryList_copy (ctb->contents.fcn->params); /* no copy before */
1701     }
1702   else
1703     {
1704       c->contents.fcn->rval = b;
1705       c->contents.fcn->params = uentryList_copy (p); /* no copy before */
1706       /*@-branchstate@*/ /* p is really released on this branch */
1707     } 
1708   /*@=branchstate@*/
1709
1710   ct = cttable_addComplex (c);  
1711   return (ct); /* was: ctype_makePointer (ct)); */
1712 }
1713
1714 static ctype
1715 ctbase_makeNFFunction (ctype b, /*@only@*/ uentryList p)
1716 {
1717   ctbase c = ctbase_new ();
1718   ctype ct;
1719
1720   c->type = CT_FCN;
1721   c->contents.fcn = (cfcn) dmalloc (sizeof (*c->contents.fcn));
1722
1723   if (ctype_isFunction (b)) /* was && ctype_isPointer (b)) */
1724     {
1725       ctbase ctb;
1726       ctype rval;
1727       
1728       if (ctype_isPointer (b))
1729         {
1730           ctb  = ctype_getCtbase (ctype_baseArrayPtr (b));
1731         }
1732       else
1733         {
1734           ctb = ctype_getCtbase (b);
1735         }
1736
1737       llassert (ctbase_isDefined (ctb));
1738       llassert (ctb->type == CT_FCN);
1739       
1740       rval = ctype_makeNFParamsFunction (ctb->contents.fcn->rval, p);
1741       
1742       c->contents.fcn->rval = rval;
1743       c->contents.fcn->params = uentryList_copy (ctb->contents.fcn->params);
1744     }
1745   else
1746     {
1747       c->contents.fcn->rval = b;
1748       c->contents.fcn->params = uentryList_copy (p);
1749       /*@-branchstate@*/ 
1750     }
1751   /*@=branchstate@*/
1752
1753   ct = cttable_addComplex (c);
1754   return (ct); /* was: ctype_makePointer (ct)); */
1755 }
1756
1757 static /*@only@*/ ctbase
1758   ctbase_makeLiveFunction (ctype b, /*@only@*/ uentryList p)
1759 {
1760   ctbase c = ctbase_new ();
1761
1762   c->type = CT_FCN;
1763
1764   c->contents.fcn = (cfcn) dmalloc (sizeof (*c->contents.fcn));
1765   c->contents.fcn->rval = b;
1766   c->contents.fcn->params = p;
1767
1768   /*@-mustfree@*/ return (c); /*@=mustfree@*/
1769 }
1770
1771 static /*@observer@*/ /*@notnull@*/ ctbase
1772 ctbase_realFunction (/*@dependent@*/ /*@notnull@*/ ctbase c)
1773 {
1774   ctbase res;
1775
1776   if (c->type == CT_FCN)
1777     {
1778       return c;
1779     }
1780
1781   llassert (ctbase_isFunction (c));
1782
1783   res = ctype_getCtbase (c->contents.base);
1784   
1785   llassert (ctbase_isDefined (res));
1786
1787   return (res);
1788 }
1789
1790 static bool
1791 ctbase_isFunction (ctbase c)
1792 {
1793   llassert (c != ctbase_undefined);
1794
1795   if (c->type == CT_FCN)
1796     {
1797       return TRUE;
1798     }
1799   else
1800     {
1801       if (c->type == CT_PTR)
1802         {
1803           ctbase fcn = ctype_getCtbase (ctbase_baseArrayPtr (c));
1804
1805           return (ctbase_isDefined (fcn) && fcn->type == CT_FCN);
1806         }
1807
1808       return FALSE;
1809     }
1810 }
1811
1812 /* doesn't copy c1 and c2 */
1813
1814 static /*@only@*/ ctbase
1815   ctbase_makeConj (ctype c1, ctype c2, bool isExplicit)
1816 {
1817   ctbase c = ctbase_new ();
1818
1819   c->type = CT_CONJ;
1820
1821   c->contents.conj = (tconj) dmalloc (sizeof (*c->contents.conj));
1822   c->contents.conj->a = c1;
1823   c->contents.conj->b = c2;
1824   c->contents.conj->isExplicit = isExplicit;
1825
1826   return (c);
1827 }
1828
1829 static ctype
1830 ctbase_getConjA (/*@notnull@*/ ctbase c)
1831 {
1832   llassert (c->type == CT_CONJ);
1833   return (c->contents.conj->a);
1834 }
1835
1836 static ctype
1837 ctbase_getConjB (/*@notnull@*/ ctbase c)
1838 {
1839   llassert (c->type == CT_CONJ);
1840   return (c->contents.conj->b);
1841 }
1842
1843 static bool
1844 ctbase_isExplicitConj (/*@notnull@*/ ctbase c)
1845 {
1846   llassert (c->type == CT_CONJ);
1847   return (c->contents.conj->isExplicit);
1848 }
1849
1850 static /*@only@*/ ctbase
1851 ctbase_createStruct (/*@only@*/ cstring n, /*@only@*/ uentryList f)
1852 {
1853   ctbase c = ctbase_new ();
1854
1855   c->type = CT_STRUCT;
1856
1857   c->contents.su = (tsu) dmalloc (sizeof (*c->contents.su));
1858   c->contents.su->name = n;
1859   c->contents.su->fields = f;
1860
1861   return (c);
1862 }
1863
1864 static /*@observer@*/ uentryList
1865 ctbase_getuentryList (/*@notnull@*/ ctbase c)
1866 {
1867   c = ctbase_realType (c);
1868
1869   if (!(c->type == CT_STRUCT || c->type == CT_UNION))
1870     llfatalbug (message ("ctbase_getuentryList: bad invocation: %q", ctbase_unparse (c)));
1871
1872   return (c->contents.su->fields);
1873 }
1874
1875 static ctbase
1876 ctbase_createUnion (/*@keep@*/ cstring n, /*@only@*/ uentryList f)
1877 {
1878   ctbase c = ctbase_new ();
1879
1880   c->type = CT_UNION;
1881
1882   c->contents.su = (tsu) dmalloc (sizeof (*c->contents.su));
1883   c->contents.su->name = n;
1884   c->contents.su->fields = f;
1885
1886   return (c);
1887 }
1888
1889 static ctype
1890 ctbase_baseArrayPtr (/*@notnull@*/ ctbase c)
1891 {
1892   ctuid ct;
1893   c = ctbase_realType (c);
1894   ct = c->type;
1895
1896   if (ct == CT_FIXEDARRAY)
1897     {
1898       return c->contents.farray->base;
1899     }
1900   else
1901     {
1902       llassert (ctuid_isAP (ct));
1903
1904       return c->contents.base;
1905     }
1906 }
1907
1908 static ctype
1909 ctbase_baseFunction (/*@notnull@*/ ctbase c)
1910 {
1911   ctbase_fixUser (c);
1912   c = ctbase_realFunction (c);
1913
1914   if (c->type != CT_FCN)
1915     {
1916       llfatalbug (message ("ctbase_baseFunction: bad call: %q", ctbase_unparse (c)));
1917     }
1918
1919   return (c->contents.fcn->rval);
1920 }
1921
1922 static uentryList
1923 ctbase_argsFunction (/*@notnull@*/ ctbase c)
1924 {
1925   ctbase_fixUser (c);
1926   c = ctbase_realFunction (c);
1927
1928   if (c->type != CT_FCN)
1929     {
1930       llfatalbug (message ("ctbase_argsFunction: bad call: %q", 
1931                            ctbase_unparse (c)));
1932     }
1933   return (c->contents.fcn->params);
1934 }
1935
1936 static bool
1937 ctbase_baseisExpFcn (ctype c)
1938 {
1939   ctbase cb;
1940   c = ctype_removePointers (c);
1941
1942   cb = ctype_getCtbase (c);
1943   llassert (ctbase_isDefined (cb));
1944
1945   if (cb->type == CT_FCN)
1946     {
1947       c = ctype_removePointers (ctype_getReturnType (c));
1948
1949       cb = ctype_getCtbase (c);
1950       llassert (ctbase_isDefined (cb));
1951
1952       return (cb->type == CT_EXPFCN);
1953     }
1954   return FALSE;
1955 }
1956
1957 /*
1958 ** ctbase_newBase behaves specially when p is a CONJ:
1959 **
1960 **    c -> conj (newBase (c, p.a), p.b)
1961 */
1962
1963 static ctype
1964 ctbase_newBase (ctype c, ctype p)
1965 {
1966   ctbase cb;
1967
1968   DPRINTF (("New base: %s / %s", ctype_unparse (c), ctype_unparse (p)));
1969
1970   if (ctype_isUndefined (c) || ctype_isUnknown (c))
1971     {
1972       return p;
1973     }
1974
1975   cb = ctype_getCtbase (c);
1976
1977   if (ctype_isConj (p))
1978     {
1979       ctbase pb = ctype_getCtbase (p);
1980
1981       llassert (ctbase_isDefined (pb));
1982
1983       if (pb->contents.conj->isExplicit)
1984         {
1985           return (ctype_makeExplicitConj (ctype_newBase (c, pb->contents.conj->a),
1986                                           pb->contents.conj->b)); 
1987                                           
1988         }
1989       else
1990         {
1991           return (ctype_makeConj (ctype_newBase (c, pb->contents.conj->a),
1992                                   pb->contents.conj->b));
1993                                   
1994         }
1995     }
1996
1997   if (ctbase_baseisExpFcn (c))
1998     {
1999       return (ctbase_newBaseExpFcn (c, p));
2000     }
2001
2002   llassert (ctbase_isDefined (cb));
2003
2004   switch (cb->type)
2005     {
2006     case CT_UNKNOWN:
2007     case CT_PRIM:
2008     case CT_USER:
2009     case CT_ENUM:
2010     case CT_ABST:
2011     case CT_STRUCT:
2012     case CT_UNION:
2013     case CT_EXPFCN:
2014       return (p);
2015
2016     case CT_PTR:
2017       {
2018         ctype ret;
2019         ctype cbn;
2020
2021         cbn = ctbase_newBase (cb->contents.base, p);
2022         ret = ctype_makePointer (cbn);
2023
2024         return ret;
2025       }
2026     case CT_FIXEDARRAY:
2027       return (ctype_makeFixedArray (ctbase_newBase (cb->contents.farray->base, p),
2028                                     cb->contents.farray->size));
2029     case CT_ARRAY:
2030       return (ctype_makeArray (ctbase_newBase (cb->contents.base, p)));
2031     case CT_FCN:
2032       return (ctype_makeRawFunction (ctbase_newBase (cb->contents.fcn->rval, p),
2033                                       cb->contents.fcn->params));
2034     case CT_CONJ:
2035       return (ctype_makeConjAux (ctbase_newBase (cb->contents.conj->a, p),
2036                                  ctbase_newBase (cb->contents.conj->b, p),
2037                                  cb->contents.conj->isExplicit));
2038     default:
2039       llcontbug (message ("ctbase_newBase: bad ctbase: %q", ctbase_unparse (cb))); 
2040       return (p); 
2041     }
2042   BADEXIT;
2043 }
2044
2045 static ctype
2046 ctbase_newBaseExpFcn (ctype c, ctype p)
2047 {
2048   ctbase cb = ctype_getCtbase (c);
2049   ctbase tcb;
2050   ctype ret, tmpct;
2051   ctype fp = ctype_unknown;
2052   uentryList ctargs = ctype_argsFunction (c);
2053
2054   /*
2055   ** okay, this is really ugly...
2056   **
2057   ** pointers inside <expf> mean pointers to the function;
2058   ** pointers outside <expf> are pointers to the return value;
2059   ** because its a function there is one superfluous pointer.
2060   */
2061   
2062   /*
2063   ** bf is a ctype, used to derived structure of cb
2064   */
2065   
2066   if (!ctbase_isFunction (cb))
2067     llbuglit ("ctbase_newBaseExpFcn: expFcn -> not a function");
2068   
2069   tmpct = ctype_getBaseType (ctype_getReturnType (c));
2070   
2071   /*
2072   ** pointers before expfcn -> p are pointers to function, not result
2073   **
2074   */
2075   
2076   tcb = ctype_getCtbase (tmpct);
2077
2078   llassert (ctbase_isDefined (tcb));
2079   tmpct = tcb->contents.base;
2080   
2081   /*
2082   ** record pointers to base in fp
2083   */
2084   
2085   while (!ctype_isUnknown (tmpct))
2086     {
2087       if (ctype_isExpFcn (tmpct)) {
2088         ctbase ttcb = ctype_getCtbase (tmpct);
2089
2090         /*
2091         ** evs 2000-05-16: This is necessary to deal with function pointers in parens.  
2092         ** The whole function pointer parsing is a major kludge, but it seems to work,
2093         ** and I'm only embarrassed by it when I haven't look at the C spec recently...
2094         */
2095            
2096         llassert (ctbase_isDefined (ttcb));
2097         tmpct = ttcb->contents.base;
2098         llassert (!ctype_isUnknown (tmpct));
2099       }
2100
2101       switch (ctype_getCtKind (tmpct))
2102         {
2103         case CTK_PTR:
2104           fp = ctype_makePointer (fp);
2105           /*@switchbreak@*/ break;
2106         case CTK_ARRAY:
2107           fp = ctype_makeArray (fp);
2108           /*@switchbreak@*/ break;
2109         case CTK_COMPLEX:
2110           {
2111             ctbase fbase = ctype_getCtbase (tmpct);
2112
2113             if (ctbase_isFunction (fbase))
2114               {
2115                 fp = ctype_makeFunction (fp, uentryList_copy (ctargs));
2116                 ctargs = ctbase_argsFunction (fbase);
2117               }
2118             else
2119               {
2120                 llbug 
2121                   (message
2122                    ("ctbase_newBaseExpFcn: fixing expfunction: bad complex type: %s [base: %q]",
2123                     ctype_unparse (tmpct), ctbase_unparse (fbase)));
2124               }
2125             goto exitLoop;
2126           }
2127         default:
2128           {
2129             llcontbug 
2130               (message ("ctbase_newBaseExpFcn: fixing expfunction: bad type: %s",
2131                         ctype_unparse (tmpct)));
2132             goto exitLoop; 
2133           }
2134         }
2135       tmpct = ctype_baseArrayPtr (tmpct);
2136     }
2137
2138  exitLoop:
2139   tmpct = ctype_getReturnType (c);
2140
2141   /*
2142   ** pointers to expf are pointers to return value
2143   */
2144   
2145   while (!ctype_isExpFcn (tmpct))
2146     {
2147       switch (ctype_getCtKind (tmpct))
2148         {
2149         case CTK_PTR:
2150           p = ctype_makePointer (p);
2151           /*@switchbreak@*/ break;
2152         case CTK_ARRAY:
2153           p = ctype_makeArray (p);
2154           /*@switchbreak@*/ break;
2155         case CTK_COMPLEX:
2156           {
2157             ctbase fbase = ctype_getCtbase (tmpct);
2158           
2159             if (ctbase_isFunction (fbase))
2160               {
2161                 p = ctype_makeFunction (p, uentryList_copy (ctbase_argsFunction (fbase)));
2162               }
2163             else
2164               {
2165                 llbug 
2166                   (message
2167                    ("ctbase_newBaseExpFcn: fixing expfunction: bad complex type: %s",
2168                     ctype_unparse (tmpct)));
2169               }
2170             goto exitLoop2;
2171           }
2172
2173         default:
2174           {
2175             llcontbug 
2176               (message ("ctbase_newBaseExpFcn: fixing expfunction2: bad type: %t", 
2177                         tmpct));
2178             goto exitLoop2; 
2179           }
2180         }
2181       tmpct = ctype_baseArrayPtr (tmpct);
2182     }
2183   
2184  exitLoop2:
2185   
2186   /*
2187   ** pointers to fp are pointers to function type
2188   */
2189
2190   ret = ctype_makeRawFunction (p, uentryList_copy (ctargs));
2191   
2192   while (ctype_getCtKind (fp) > CTK_PLAIN)
2193     {
2194       switch (ctype_getCtKind (fp))
2195         {
2196         case CTK_PTR:
2197           ret = ctype_makePointer (ret);
2198           /*@switchbreak@*/ break;
2199         case CTK_ARRAY:
2200           ret = ctype_makeArray (ret);
2201           /*@switchbreak@*/ break;
2202         case CTK_COMPLEX:
2203           {
2204             ctbase fbase = ctype_getCtbase (fp);
2205             
2206             if (ctbase_isFunction (fbase))
2207               {
2208                 ret = 
2209                   ctype_makeFunction (ret,
2210                                       uentryList_copy (ctbase_argsFunction (fbase)));
2211               }
2212             else
2213               {
2214                 BADBRANCH;
2215               }
2216             goto exitLoop3;
2217           }
2218
2219         default:
2220           {
2221             llcontbug (message ("post-fixing expfunction: bad type: %t", fp));
2222             goto exitLoop3;
2223           }
2224         }
2225       fp = ctype_baseArrayPtr (fp);
2226     }
2227   
2228  exitLoop3:
2229   return (ret);
2230 }
2231
2232 /*
2233 ** returns lowest level base of c: plain type
2234 */
2235
2236 static /*@notnull@*/ /*@only@*/ ctbase
2237 ctbase_getBaseType (/*@notnull@*/ ctbase c)
2238 {
2239   switch (c->type)
2240     {
2241     case CT_UNKNOWN:
2242     case CT_PRIM:
2243     case CT_USER:
2244     case CT_ENUM:
2245     case CT_ENUMLIST:
2246     case CT_BOOL:
2247     case CT_ABST:
2248     case CT_FCN:
2249     case CT_STRUCT:
2250     case CT_UNION:
2251       return (ctbase_copy (c));
2252
2253     case CT_PTR:
2254     case CT_ARRAY:
2255       return (ctbase_getBaseType (ctype_getCtbaseSafe (c->contents.base)));
2256
2257     case CT_FIXEDARRAY:
2258       return (ctbase_getBaseType (ctype_getCtbaseSafe (c->contents.farray->base)));
2259     case CT_CONJ:               /* base type of A conj branch? */
2260       return (ctbase_getBaseType (ctype_getCtbaseSafe (c->contents.conj->a)));
2261     case CT_EXPFCN:
2262       return (ctbase_copy (c));
2263
2264     default:
2265       llfatalbug (message ("ctbase_getBaseType: bad ctbase: %q", ctbase_unparse (c)));
2266     }
2267
2268   BADEXIT;
2269 }
2270
2271 static int
2272 ctbase_compare (ctbase c1, ctbase c2, bool strict)
2273 {
2274   ctuid c1tid, c2tid;
2275
2276   if (ctbase_isUndefined (c1) || ctbase_isUndefined (c2))
2277     {
2278       llcontbuglit ("ctbase_compare: undefined ctbase");
2279       return -1;
2280     }
2281
2282   c1tid = c1->type;
2283   c2tid = c2->type;
2284
2285   if (c1tid < c2tid)
2286     return -1;
2287   if (c1tid > c2tid)
2288     return 1;
2289
2290   switch (c1tid)
2291     {
2292     case CT_UNKNOWN:
2293       return 0;
2294     case CT_PRIM:
2295       return (int_compare (c1->contents.prim, c2->contents.prim));
2296     case CT_BOOL:
2297       return 0;
2298     case CT_USER:
2299       return (int_compare (c1->contents.tid, c2->contents.tid));
2300     case CT_ENUMLIST:        
2301       return 1;
2302     case CT_ENUM:               /* for now, keep like abstract */
2303     case CT_ABST:
2304       return (int_compare (c1->contents.tid, c2->contents.tid));
2305     case CT_PTR:
2306       return (ctype_compare (c1->contents.base, c2->contents.base));
2307     case CT_FIXEDARRAY:
2308       INTCOMPARERETURN (c1->contents.farray->size, c2->contents.farray->size);
2309
2310       return (ctype_compare (c1->contents.farray->base,
2311                              c2->contents.farray->base));
2312     case CT_ARRAY:
2313       return (ctype_compare (c1->contents.base, c2->contents.base));
2314     case CT_FCN:
2315       {
2316         COMPARERETURN (ctype_compare (c1->contents.fcn->rval, c2->contents.fcn->rval));
2317
2318         if (strict)
2319           {
2320             return (uentryList_compareStrict (c1->contents.fcn->params, 
2321                                               c2->contents.fcn->params));
2322           }
2323         else
2324           {
2325             return (uentryList_compareParams (c1->contents.fcn->params, 
2326                                               c2->contents.fcn->params));
2327           }
2328       }
2329     case CT_EXPFCN:
2330       return (ctype_compare (c1->contents.base, c2->contents.base));
2331     case CT_STRUCT:
2332     case CT_UNION:
2333       /* evs 2000-07-28: this block was missing! */
2334       if (strict) {
2335         int ncmp = cstring_compare (c1->contents.su->name,
2336                                     c2->contents.su->name);
2337
2338         if (ncmp != 0) {
2339           if (isFakeTag (c1->contents.su->name) 
2340               && isFakeTag (c2->contents.su->name)) {
2341             ; /* If they are both fake struct tags, don't require match. */
2342           } else {
2343             return ncmp;
2344           }
2345         }
2346       }
2347
2348       DPRINTF (("Comparing fields: %s / %s",
2349                 ctbase_unparse (c1),
2350                 ctbase_unparse (c2)));
2351
2352       return (uentryList_compareFields (c1->contents.su->fields,
2353                                         c2->contents.su->fields));
2354     case CT_CONJ:
2355       {
2356         COMPARERETURN (ctype_compare (c1->contents.conj->a,
2357                                       c2->contents.conj->a));
2358         COMPARERETURN (ctype_compare (c1->contents.conj->b,
2359                                       c2->contents.conj->b));
2360         return (bool_compare (c1->contents.conj->isExplicit,
2361                               c2->contents.conj->isExplicit));
2362       }
2363     }
2364   BADEXIT;
2365 }
2366
2367 static int
2368 ctbase_compareStrict (/*@notnull@*/ ctbase c1, /*@notnull@*/ ctbase c2)
2369 {
2370  return (ctbase_compare (c1, c2, TRUE));
2371 }
2372
2373 static bool ctbase_equivStrict (/*@notnull@*/ ctbase c1, /*@notnull@*/ ctbase c2)
2374 {
2375   return (ctbase_compareStrict (c1,c2) == 0);
2376 }
2377
2378 static bool ctbase_equiv (/*@notnull@*/ ctbase c1, /*@notnull@*/ ctbase c2)
2379 {
2380   return (ctbase_compare (c1, c2, FALSE) == 0);
2381 }
2382
2383 static bool
2384 ctbase_isKind (/*@notnull@*/ ctbase c, ctuid kind)
2385 {
2386   ctuid ck = c->type;
2387
2388   if (ck == kind)
2389     return TRUE;
2390
2391   if (ck == CT_CONJ)
2392     return (ctbase_isKind (ctype_getCtbaseSafe (c->contents.conj->a), kind) ||
2393             ctbase_isKind (ctype_getCtbaseSafe (c->contents.conj->b), kind));
2394
2395   return FALSE;
2396 }
2397
2398 static bool
2399 ctbase_isKind2 (/*@notnull@*/ ctbase c, ctuid kind1, ctuid kind2)
2400 {
2401   ctuid ck = c->type;
2402
2403   if (ck == kind1 || ck == kind2)
2404     return TRUE;
2405
2406   if (ck == CT_CONJ)
2407     return (ctbase_isKind2 (ctype_getCtbaseSafe (c->contents.conj->a), kind1, kind2) ||
2408        ctbase_isKind2 (ctype_getCtbaseSafe (c->contents.conj->b), kind1, kind2));
2409
2410   return FALSE;
2411 }
2412
2413 static bool
2414 ctbase_isAbstract (/*@notnull@*/ ctbase c)
2415 {
2416   return (c->type == CT_ABST);
2417 }
2418
2419 static bool ctbase_isUA (ctbase c) 
2420 {
2421   return (ctbase_isDefined (c) && ((c)->type == CT_USER || (c)->type == CT_ABST));
2422 }
2423
2424 static bool
2425 ctbase_almostEqual (ctbase c1, ctbase c2)
2426 {
2427   ctuid c1tid, c2tid;
2428   
2429   /* undefined types never match */
2430   
2431   if (ctbase_isUndefined (c1) || ctbase_isUndefined (c2))
2432     return FALSE;
2433   
2434   c1tid = c1->type;
2435   c2tid = c2->type;
2436   
2437   if (c1tid == CT_FIXEDARRAY && c2tid == CT_ARRAY)
2438     {
2439       return (ctbase_almostEqual (ctype_getCtbase (c1->contents.farray->base),
2440                                   ctype_getCtbase (c2->contents.base)));
2441     }
2442   
2443   if (c2tid == CT_FIXEDARRAY && c1tid == CT_ARRAY)
2444     {
2445       return (ctbase_almostEqual (ctype_getCtbase (c1->contents.base),
2446                                   ctype_getCtbase (c2->contents.farray->base)));
2447     }
2448   
2449   if (c1tid != c2tid)
2450     return FALSE;
2451
2452   switch (c1tid)
2453     {
2454     case CT_UNKNOWN:
2455       return TRUE;
2456     case CT_PRIM:
2457       return (cprim_equal (c1->contents.prim, c2->contents.prim));
2458     case CT_BOOL:
2459       return TRUE;
2460     case CT_ABST:
2461       return (typeId_equal (c1->contents.tid, c2->contents.tid));
2462     case CT_USER:
2463       return (typeId_equal (c1->contents.tid, c2->contents.tid));
2464     case CT_ENUM:       
2465       return (cstring_equal (c1->contents.cenum->tag, c2->contents.cenum->tag));
2466     case CT_PTR:
2467       return (ctype_almostEqual (c1->contents.base, c2->contents.base));
2468     case CT_FIXEDARRAY:
2469       return (ctype_almostEqual (c1->contents.farray->base, 
2470                                  c2->contents.farray->base));
2471     case CT_ARRAY:
2472       return (ctype_almostEqual (c1->contents.base, c2->contents.base));
2473     case CT_FCN:
2474       return (ctype_almostEqual (c1->contents.fcn->rval, c2->contents.fcn->rval)
2475               && uentryList_matchParams (c1->contents.fcn->params, 
2476                                          c2->contents.fcn->params, FALSE, TRUE));
2477     case CT_STRUCT:
2478     case CT_UNION:
2479       if (!cstring_isEmpty (c1->contents.su->name))
2480         {
2481           return (cstring_equal (c1->contents.su->name, c2->contents.su->name));
2482         }
2483       else
2484         {
2485           if (!cstring_isEmpty (c2->contents.su->name))
2486             {
2487               return FALSE;
2488             }
2489
2490           llcontbuglit ("ctbase_almostEqual: match fields");
2491           return (FALSE);
2492         }
2493     default:
2494       llcontbug (message ("ctbase_almostEqual: unknown type: %d\n", (int)c1tid));
2495       return (FALSE);
2496     }
2497 }
2498
2499 /*drl added July 02, 001
2500   called by ctype_getArraySize
2501 */
2502
2503 long int ctbase_getArraySize (ctbase ctb)
2504 {
2505   llassert (ctbase_isDefined (ctb) );
2506   
2507   llassert (ctbase_isFixedArray(ctb) );
2508   
2509   return (ctb->contents.farray->size);
2510
2511 }
This page took 0.953258 seconds and 5 git commands to generate.