]> andersk Git - splint.git/blob - src/typeIdSet.c
Initial revision
[splint.git] / src / typeIdSet.c
1 /*
2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2000 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 lclint: lclint-request@cs.virginia.edu
21 ** To report a bug: lclint-bug@cs.virginia.edu
22 ** For more information: http://lclint.cs.virginia.edu
23 */
24 /*
25 ** typeIdSet.c
26 */
27
28 # include "lclintMacros.nf"
29 # include "basic.h"
30
31 /*@constant int TISTABLEBASESIZE;@*/
32 # define TISTABLEBASESIZE LARGEBASESIZE
33
34 static int tistableentries = 0;
35 static int tistablefree = 0;
36 typedef /*@only@*/ usymIdSet o_usymIdSet;
37 static /*@only@*/ o_usymIdSet *tistable;
38 static void tistable_addDirectEntry (/*@only@*/ usymIdSet p_s) 
39    /*@modifies tistable, tistableentries, tistablefree@*/;
40
41 void typeIdSet_initMod (void)
42    /*@globals undef tistable;@*/
43    /*@modifies tistable, tistablefree;@*/
44 {
45   llassert (tistableentries == 0 && tistablefree == 0);
46
47   tistablefree = TISTABLEBASESIZE;
48   tistable = (usymIdSet *) dmalloc (sizeof (tistable) * tistablefree);
49   tistable[0] = usymIdSet_undefined;
50   tistableentries = 1;
51   tistablefree--;
52 }
53
54 void typeIdSet_destroyMod (void)
55    /*@globals killed tistable, tistableentries@*/
56 {
57   int i;
58
59   for (i = 0; i < tistableentries; i++)
60     {
61       usymIdSet_free (tistable[i]);
62     }
63
64   sfree (tistable);
65   tistableentries = 0;
66 }
67
68 void typeIdSet_dumpTable (FILE *fout)
69 {
70   int i;
71
72   /*
73   ** Don't dump 0th entry
74   */
75
76   for (i = 1; i < tistableentries; i++)
77     {
78       cstring s = usymIdSet_dump (tistable[i]);
79
80       fprintf (fout, "%s\n", cstring_toCharsSafe (s));
81       cstring_free (s);
82     }
83 }
84
85 static /*@unused@*/ void tistable_printOut (void)
86 {
87   int i;
88
89   /*
90   ** Don't dump 0th entry
91   */
92
93   for (i = 1; i < tistableentries; i++)
94     {
95       cstring s = usymIdSet_unparse (tistable[i]);
96
97       fprintf (g_msgstream, "%d: %s\n", i, cstring_toCharsSafe (s));
98       cstring_free (s);
99     }
100 }
101
102 void typeIdSet_loadTable (FILE *fin)
103 {
104   char *s = mstring_create (MAX_DUMP_LINE_LENGTH);  
105   char *os = s;
106
107   llassert (tistableentries == 1);
108
109   s = fgets (s, MAX_DUMP_LINE_LENGTH, fin);
110
111   while (s != NULL && *s != ';')
112     {
113       usymIdSet u = usymIdSet_undump (&s);
114       
115       llassert (*s == '\0' || *s == '\n');
116       
117       tistable_addDirectEntry (u);
118       s = fgets (os, MAX_DUMP_LINE_LENGTH, fin);
119     }
120 }
121   
122 static void tistable_grow (void)
123 {
124   o_usymIdSet *oldtable = tistable;
125   int newsize = tistableentries + TISTABLEBASESIZE;
126   int i;
127
128   llassert (tistablefree == 0);
129
130   tistable = (usymIdSet *) dmalloc (sizeof (tistable) * newsize);
131
132   for (i = 0; i < tistableentries; i++)
133     {
134       tistable[i] = oldtable[i];
135     }
136
137   tistablefree = TISTABLEBASESIZE;
138   sfree (oldtable);
139
140
141 static void tistable_addDirectEntry (/*@only@*/ usymIdSet s)
142 {
143   if (tistablefree == 0)
144     {
145       tistable_grow ();
146     }
147
148   tistable[tistableentries] = s;
149   tistableentries++;
150   tistablefree--;
151 }
152
153 static int tistable_addEntry (/*@only@*/ usymIdSet s)
154 {
155   int i;
156
157   
158   for (i = 0; i < tistableentries; i++)
159     {
160       if (usymIdSet_compare (tistable[i], s) == 0)
161         {
162           /*@access usymIdSet@*/
163           llassert (i == 0 || s != tistable[i]);
164           /*@noaccess usymIdSet@*/
165
166           usymIdSet_free (s);
167                   return i;
168         }
169     }
170
171   tistable_addDirectEntry (s);
172   return (tistableentries - 1);
173 }
174   
175 static /*@observer@*/ usymIdSet tistable_fetch (typeIdSet t)
176    /*@globals tistableentries, tistable@*/
177    /*@modifies nothing;@*/
178 {
179   llassert (t >= 0 && t < tistableentries);
180
181   return tistable[t];
182 }
183
184 typeIdSet typeIdSet_emptySet (void)
185 {
186   if (tistableentries == 0)
187     {
188       int val = tistable_addEntry (usymIdSet_new ());
189
190       llassert (val == 0);
191     }
192
193   llassert (usymIdSet_isUndefined (tistable[0]));
194   return 0;
195 }
196     
197 bool typeIdSet_member (typeIdSet t, typeId el)
198 {
199   usymIdSet u = tistable_fetch (t);
200
201   return usymIdSet_member (u, el);
202 }
203
204 bool typeIdSet_isEmpty (typeIdSet t)
205 {
206   return (t == 0);
207 }
208
209 typeIdSet typeIdSet_single (typeId t)
210 {
211   return (tistable_addEntry (usymIdSet_single (t)));
212 }
213
214 typeIdSet typeIdSet_singleOpt (typeId t)
215 {
216   if (typeId_isValid (t))
217     {
218       return (tistable_addEntry (usymIdSet_single (t)));
219     }
220   else
221     {
222       return typeIdSet_empty;
223     }
224 }
225
226 typeIdSet typeIdSet_insert (typeIdSet t, typeId el)
227 {
228   usymIdSet u = tistable_fetch (t);
229
230   if (usymIdSet_member (u, el))
231     {
232       return t;
233     }
234   else
235     {
236       return (tistable_addEntry (usymIdSet_add (u, el)));
237     }
238 }
239
240 typeIdSet typeIdSet_removeFresh (typeIdSet t, typeId el)
241 {
242   return (tistable_addEntry (usymIdSet_removeFresh (tistable_fetch (t), el)));
243 }
244
245 cstring typeIdSet_unparse (typeIdSet t)
246 {
247   return (usymIdSet_unparse (tistable_fetch (t)));
248 }
249
250 int typeIdSet_compare (typeIdSet t1, typeIdSet t2)
251 {
252   return (int_compare (t1, t2));
253 }
254
255 typeIdSet typeIdSet_subtract (typeIdSet s, typeIdSet t)
256 {
257   if (typeIdSet_isEmpty (t))
258     {
259       return s;
260     }
261   else
262     {
263       return (tistable_addEntry (usymIdSet_subtract (tistable_fetch (s),
264                                                      tistable_fetch (t))));
265     }
266 }
267
268 cstring typeIdSet_dump (typeIdSet t)
269 {
270   return (message ("%d", t));
271 }
272
273 typeIdSet typeIdSet_undump (char **s)
274 {
275   int i;
276
277   
278   i = getInt (s);
279
280   llassert (i >= 0 && i < tistableentries);
281   return (typeIdSet) i;
282 }
283
284 typeIdSet typeIdSet_union (typeIdSet t1, typeIdSet t2)
285 {
286   if (t1 == typeIdSet_undefined)
287     {
288       return t2;
289     }
290   else if (t2 == typeIdSet_undefined)
291     {
292       return t1;
293     }
294   else
295     {
296       return (tistable_addEntry (usymIdSet_newUnion (tistable_fetch (t1),
297                                                      tistable_fetch (t2))));
298     }
299 }
This page took 0.060553 seconds and 5 git commands to generate.