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