2 ** Copyright (c) Massachusetts Institute of Technology 1994-1998.
3 ** All Rights Reserved.
4 ** Unpublished rights reserved under the copyright laws of
7 ** THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8 ** OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
10 ** This code is distributed freely and may be used freely under the
11 ** following conditions:
13 ** 1. This notice may not be removed or altered.
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).
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.)
30 ** type definitions and forward declarations in ctbase.i
34 ctentry_free (/*@only@*/ ctentry c)
36 ctbase_free (c->ctbase);
37 cstring_free (c->unparse);
41 static void cttable_reset (void)
45 if (cttab.entries != NULL)
49 for (i = 0; i < cttab.size; i++)
51 ctentry_free (cttab.entries[i]);
55 sfree (cttab.entries);
65 static /*@observer@*/ ctbase ctype_getCtbase (ctype c)
68 if (c >= 0 && c < cttab.size)
70 return (cttab.entries[c]->ctbase);
75 llbuglit ("ctype_getCtbase: ctype unknown");
77 llbuglit ("ctype_getCtbase: ctype invalid");
79 llbuglit ("ctype_getCtbase: ctype dne");
81 llbuglit ("ctype_getCtbase: elips marker");
83 llfatalbug (message ("ctype_getCtbase: ctype out of range: %d", c));
90 static /*@notnull@*/ /*@observer@*/ ctbase
91 ctype_getCtbaseSafe (ctype c)
93 ctbase res = ctype_getCtbase (c);
95 llassert (ctbase_isDefined (res));
104 ctype_getCtentry (ctype c)
106 static /*@only@*/ ctentry errorEntry = NULL;
110 if (errorEntry == NULL)
112 errorEntry = ctentry_makeNew (CTK_UNKNOWN, ctbase_undefined);
119 if (c >= CTK_PLAIN && c < cttab.size)
121 return (cttab.entries[c]);
123 else if (c == CTK_UNKNOWN)
124 llcontbuglit ("ctype_getCtentry: ctype unknown");
125 else if (c == CTK_INVALID)
126 llcontbuglit ("ctype_getCtentry: ctype invalid (ctype_undefined)");
127 else if (c == CTK_DNE)
128 llcontbuglit ("ctype_getCtentry: ctype dne");
130 llbug (message ("ctype_getCtentry: ctype out of range: %d", c));
132 return (cttab.entries[ctype_unknown]);
137 ctentry_makeNew (ctkind ctk, /*@only@*/ ctbase c)
140 return (ctentry_make (ctk, c, ctype_dne, ctype_dne, ctype_dne, cstring_undefined));
143 static /*@only@*/ ctentry
144 ctentry_make (ctkind ctk,
145 /*@keep@*/ ctbase c, ctype base,
146 ctype ptr, ctype array,
147 /*@keep@*/ cstring unparse) /*@*/
149 ctentry cte = (ctentry) dmalloc (sizeof (*cte));
155 cte->unparse = unparse;
160 ctentry_unparse (ctentry c)
163 ("%20s [%d] [%d] [%d]",
164 (cstring_isDefined (c->unparse) ? c->unparse : cstring_makeLiteral ("<no name>")),
171 ctentry_isInteresting (ctentry c)
173 return (cstring_isNonEmpty (c->unparse));
176 static /*@only@*/ cstring
177 ctentry_dump (ctentry c)
179 if (c->ptr == ctype_dne
180 && c->array == ctype_dne
181 && c->base == ctype_dne)
183 return (message ("%d %q&",
184 ctkind_toInt (c->kind),
185 ctbase_dump (c->ctbase)));
187 else if (c->base == ctype_undefined
188 && c->array == ctype_dne)
190 if (c->ptr == ctype_dne)
192 return (message ("%d %q!",
193 ctkind_toInt (c->kind),
194 ctbase_dump (c->ctbase)));
198 return (message ("%d %q^%d",
199 ctkind_toInt (c->kind),
200 ctbase_dump (c->ctbase),
204 else if (c->ptr == ctype_dne
205 && c->array == ctype_dne)
207 return (message ("%d %q%d&",
208 ctkind_toInt (c->kind),
209 ctbase_dump (c->ctbase),
214 return (message ("%d %q%d %d %d",
215 ctkind_toInt (c->kind),
216 ctbase_dump (c->ctbase),
217 c->base, c->ptr, c->array));
222 static /*@only@*/ ctentry
223 ctentry_undump (/*@dependent@*/ char *s)
225 int base, ptr, array;
229 kind = ctkind_fromInt (getInt (&s));
230 ct = ctbase_undump (&s);
232 if (optCheckChar (&s, '&'))
238 else if (optCheckChar (&s, '!'))
240 base = ctype_undefined;
244 else if (optCheckChar (&s, '^'))
246 base = ctype_undefined;
254 if (optCheckChar (&s, '&'))
266 /* can't unparse w/o typeTable */
267 return (ctentry_make (kind, ct, base, ptr, array, cstring_undefined));
270 static /*@observer@*/ cstring
271 ctentry_doUnparse (ctentry c) /*@modifies c@*/
273 if (cstring_isDefined (c->unparse))
279 cstring s = ctbase_unparse (c->ctbase);
281 if (!cstring_isEmpty (s) && !cstring_containsChar (s, '<'))
287 cstring_markOwned (s);
294 static /*@observer@*/ cstring
295 ctentry_doUnparseDeep (ctentry c)
297 if (cstring_isDefined (c->unparse))
303 c->unparse = ctbase_unparseDeep (c->ctbase);
309 ** cttable operations
312 static /*@only@*/ cstring
313 cttable_unparse (void)
316 cstring s = cstring_undefined;
319 for (i = 0; i < cttab.size; i++)
321 ctentry cte = cttab.entries[i];
322 if (ctentry_isInteresting (cte))
324 if (ctbase_isUA (cte->ctbase))
326 s = message ("%s%d\t%q [%d]\n", s, i, ctentry_unparse (cttab.entries[i]),
327 cte->ctbase->contents.tid);
331 s = message ("%s%d\t%q\n", s, i, ctentry_unparse (cttab.entries[i]));
335 /*@noaccess ctbase@*/
345 for (i = 0; i < cttab.size; i++)
347 ctentry cte = cttab.entries[i];
349 if (ctentry_isInteresting (cte))
351 if (ctbase_isUA (cte->ctbase))
353 fprintf (g_msgstream, "%3d: %s [%d]\n", i,
354 cstring_toCharsSafe (ctentry_doUnparse (cttab.entries[i])),
355 cte->ctbase->contents.tid);
359 fprintf (g_msgstream, "%3d: %s\n", i,
360 cstring_toCharsSafe (ctentry_doUnparse (cttab.entries[i])));
365 /* fprintf (g_msgstream, "%3d: <no name>\n", i); */
368 /*@noaccess ctbase@*/
374 ** Output cttable for dump file
378 cttable_dump (FILE *fout)
380 bool showdots = FALSE;
381 int showdotstride = 0;
384 if (context_getFlag (FLG_SHOWSCAN) && cttab.size > 5000)
386 fprintf (g_msgstream, " >\n"); /* end dumping to */
387 fprintf (g_msgstream, "< Dumping type table (%d types) ", cttab.size);
388 showdotstride = cttab.size / 5;
392 for (i = 0; i < cttab.size; i++)
396 s = ctentry_dump (cttab.entries[i]);
397 llassert (cstring_length (s) < MAX_DUMP_LINE_LENGTH);
398 fputline (fout, cstring_toCharsSafe (s));
401 if (showdots && (i != 0 && ((i - 1) % showdotstride == 0)))
403 (void) fflush (g_msgstream);
404 fprintf (stderr, ".");
405 (void) fflush (stderr);
411 fprintf (stderr, " >\n< Continuing dump ");
417 ** load cttable from init file
420 static void cttable_load (FILE *f)
422 /*@modifies cttab, f @*/
424 char *s = mstring_create (MAX_DUMP_LINE_LENGTH);
429 while (fgets (s, MAX_DUMP_LINE_LENGTH, f) != NULL && *s == ';')
434 if (mstring_length (s) == (MAX_DUMP_LINE_LENGTH - 1))
436 llbug (message ("Library line too long: %s\n", cstring_fromChars (s)));
439 while (s != NULL && *s != ';' && *s != '\0')
443 cte = ctentry_undump (s);
444 ct = cttable_addFull (cte);
446 if (ctbase_isConj (cte->ctbase)
447 && !(cte->ctbase->contents.conj->isExplicit))
449 ctype_recordConj (ct);
452 (void) fgets (s, MAX_DUMP_LINE_LENGTH, f);
461 ** fill up the cttable with basic types, and first order derivations.
462 ** this is done without using our constructors for efficiency reasons
468 static void cttable_init (void)
469 /*@globals cttab@*/ /*@modifies cttab@*/
473 ctbase ct = ctbase_undefined;
475 llassert (cttab.size == 0);
477 /* do for plain, pointer, arrayof */
478 for (i = CTK_PLAIN; i <= CTK_ARRAY; i++)
480 for (j = CTX_UNKNOWN; j <= CTX_LAST; j++)
486 ct = ctbase_createBool (); /* why different? */
488 else if (j == CTX_UNKNOWN)
490 ct = ctbase_createUnknown ();
494 ct = ctbase_createPrim ((cprim)j);
497 (void) cttable_addFull
498 (ctentry_make (CTK_PLAIN,
500 j + CTK_PREDEFINED, j + CTK_PREDEFINED2,
501 ctbase_unparse (ct)));
508 ct = ctbase_makePointer (j);
509 /*@switchbreak@*/ break;
511 ct = ctbase_makeArray (j);
512 /*@switchbreak@*/ break;
514 llbugexitlit ("cttable_init: base case");
517 (void) cttable_addDerived (i, ct, j);
522 /**** reserve a slot for userBool ****/
523 (void) cttable_addFull (ctentry_make (CTK_INVALID, ctbase_undefined,
524 ctype_undefined, ctype_dne, ctype_dne,
534 o_ctentry *newentries ;
536 cttab.nspace = CTK_BASESIZE;
537 newentries = (ctentry *) dmalloc (sizeof (*newentries) * (cttab.size + cttab.nspace));
539 if (newentries == NULL)
541 llfatalerror (message ("cttable_grow: out of memory. Size: %d",
545 for (i = 0; i < cttab.size; i++)
547 newentries[i] = cttab.entries[i];
551 sfree (cttab.entries);
554 cttab.entries = newentries;
559 cttable_addDerived (ctkind ctk, /*@keep@*/ ctbase cnew, ctype base)
561 if (cttab.nspace == 0)
564 cttab.entries[cttab.size] =
565 ctentry_make (ctk, cnew, base, ctype_dne, ctype_dne, cstring_undefined);
569 return (cttab.size++);
573 cttable_addComplex (/*@only@*/ /*@notnull@*/ ctbase cnew)
574 /*@modifies cttab; @*/
576 if (cnew->type != CT_FCN && cnew->type != CT_EXPFCN)
579 int ctstop = cttable_lastIndex () - DEFAULT_OPTLEVEL;
581 if (ctstop < LAST_PREDEFINED)
583 ctstop = LAST_PREDEFINED;
586 for (i = cttable_lastIndex (); i >= ctstop; i--) /* better to go from back... */
591 ctb = ctype_getCtbase (i);
593 if (ctbase_isDefined (ctb) && ctbase_equivStrict (cnew, ctb))
602 if (cttab.nspace == 0)
605 cttab.entries[cttab.size] = ctentry_make (CTK_COMPLEX, cnew, ctype_undefined,
606 ctype_dne, ctype_dne,
610 return (cttab.size++);
614 cttable_addFull (ctentry cnew)
616 if (cttab.nspace == 0)
621 cttab.entries[cttab.size] = cnew;
624 return (cttab.size++);
628 cttable_addFullSafe (/*@only@*/ ctentry cnew)
631 ctbase cnewbase = cnew->ctbase;
633 llassert (ctbase_isDefined (cnewbase));
635 for (i = cttable_lastIndex (); i >= CT_FIRST; i--)
637 ctbase ctb = ctype_getCtbase (i);
639 if (ctbase_isDefined (ctb)
640 && ctbase_equiv (cnewbase, ctype_getCtbaseSafe (i)))
647 if (cttab.nspace == 0)
650 cttab.entries[cttab.size] = cnew;
654 return (cttab.size++);