]> andersk Git - splint.git/blob - src/tokentable.c
ecc2e1971124e819f07bc6c662288e43c5645128
[splint.git] / src / tokentable.c
1 /*
2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 University of Virginia,
4 **         Massachusetts Institute of Technology
5 **
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
10 ** 
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ** General Public License for more details.
15 ** 
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
19 **
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
23 */
24 /*
25 ** tokentable.c
26 */
27
28 # include "splintMacros.nf"
29 # include "llbasic.h"
30 # include "osd.h"
31 # include "tokentable.h"
32
33 static long unsigned MaxToken;  
34 static /*@null@*/ /*@only@*/ o_ltoken *TokenTable;       
35
36 static void AllocTokenTable (void) /*@modifies TokenTable, MaxToken@*/ ;
37
38 ltoken
39   LSLInsertToken (ltokenCode cod, lsymbol sym, lsymbol rTxt, bool def)
40 {
41   while (sym >= MaxToken)
42     {
43       AllocTokenTable ();
44     }
45
46   llassert (TokenTable != NULL);
47
48   if (ltoken_isUndefined (TokenTable[sym]))
49     {
50       TokenTable[sym] = ltoken_create (cod, sym);        
51       ltoken_setRawText (TokenTable[sym], rTxt);
52       ltoken_setDefined (TokenTable[sym], def);
53     }
54   
55   return TokenTable[sym];
56 }
57
58 void
59 LSLUpdateToken (ltokenCode cod, lsymbol sym, bool def)
60 {
61   llassert (TokenTable != NULL);
62
63   if (!ltoken_isUndefined (TokenTable[sym]))
64     {
65       ltoken_setCode (TokenTable[sym], cod);
66       ltoken_setDefined (TokenTable[sym], def);
67     }
68   else
69     {
70       llfatalbug (message ("LSLUpdateToken: token not in table: %d, text: %s", 
71                            (int) cod, cstring_fromChars (lsymbol_toChars (sym))));
72     }
73 }
74
75 void
76 LSLSetTokenHasSyn (lsymbol sym, bool syn)
77 {
78   llassert (TokenTable != NULL);
79     
80   if (!ltoken_isUndefined (TokenTable[sym]))
81     {
82       ltoken_setHasSyn (TokenTable[sym], syn);
83     }
84   else
85     {
86       llbuglit ("LSLSetTokenHasSyn: null token");
87     }
88 }
89
90 ltoken LSLGetToken (lsymbol sym)
91 {
92   llassert (TokenTable != NULL);
93
94   if (!((sym < MaxToken) || (!ltoken_isUndefined (TokenTable[sym]))))
95     {
96       llcontbuglit ("LSLGetToken: bad argument");
97       return TokenTable[0];
98     }
99
100   return TokenTable[sym];
101 }
102
103 /*@exposed@*/ ltoken
104 LSLReserveToken (ltokenCode cod, char *txt)
105 {
106   lsymbol sym;
107   
108   sym = lsymbol_fromChars (txt);
109
110   /* 
111   ** Reserved tokens never have raw text like synonyms.
112   */
113
114   return LSLInsertToken (cod, sym, lsymbol_undefined, TRUE);
115 }
116
117 static void
118 AllocTokenTable (void)
119 {
120   long unsigned oldSize, newSize;
121   long unsigned int i;
122
123   oldSize = MaxToken;
124
125   if (oldSize == 0)
126     {
127       newSize = INITTOKENTABLE;
128       llassert (TokenTable == NULL);
129       TokenTable = (ltoken *) 
130         dmalloc (size_fromLongUnsigned (newSize * sizeof (*TokenTable))); 
131     }
132   else
133     {
134       o_ltoken *oldTokenTable = TokenTable;
135
136       newSize = (long unsigned) (DELTATOKENTABLE * oldSize);
137       TokenTable = (ltoken *) 
138         dmalloc (size_fromLongUnsigned (newSize * sizeof (*TokenTable)));
139
140       llassert (oldSize > 0);
141       llassert (oldTokenTable != NULL);
142       
143       for (i = 0; i < oldSize; i++)
144         {
145           TokenTable[i] = oldTokenTable[i];
146         }
147
148       sfree (oldTokenTable);
149     }
150
151   /*@+loopexec@*/
152   for (i = oldSize; i < newSize; i++)
153     {
154       TokenTable[i] = ltoken_undefined;
155     }
156   /*@=loopexec@*/
157
158   MaxToken = newSize;
159 /*@-compdef@*/ } /*=compdef@*/
160
161 void
162 ltokenTableInit (void)
163 {
164   MaxToken = 0;
165 }
166
167 void
168 ltokenTableCleanup (void)
169 {
170   if (TokenTable != NULL)
171     {
172       long unsigned i;
173       
174       for (i = 0; i < MaxToken; i++)
175         {
176           ltoken_free (TokenTable[i]);
177         }
178
179       sfree (TokenTable); 
180       TokenTable = NULL;
181     }
182 }
This page took 0.037847 seconds and 3 git commands to generate.