]> andersk Git - splint.git/blob - src/qtype.c
10dd0231b22059179c2430d078a39ba9d986313c
[splint.git] / src / qtype.c
1 /*
2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 University of Virginia,
4 **         Massachusetts Institute of Technology
5 **
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
10 ** 
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ** General Public License for more details.
15 ** 
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
19 **
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
23 */
24 /*
25 ** qtype.c
26 **
27 ** Qualified types: a type qualifier list, and a ctype.
28 ** qtypes are mutable
29 */
30
31 # include "splintMacros.nf"
32 # include "basic.h"
33
34 /*@notnull@*/ qtype qtype_create (ctype c)
35 {
36   qtype q = (qtype) dmalloc (sizeof (*q));
37
38   q->type = c;
39   q->quals = qualList_new ();
40   return q;
41 }
42
43 void qtype_free (/*@only@*/ qtype q)
44 {
45   if (qtype_isDefined (q))
46     {
47       qualList_free (q->quals);
48       sfree (q);
49     }
50 }
51
52 qtype qtype_unknown ()
53 {
54   return (qtype_create (ctype_unknown));
55 }
56
57 qtype qtype_addQual (qtype qt, qual q)
58 {
59   DPRINTF (("Add qual: %s / %s", qtype_unparse (qt), qual_unparse (q)));
60
61   if (qtype_isDefined (qt))
62     {
63       qt->quals = qualList_add (qt->quals, q);
64     }
65
66   DPRINTF (("==> %s", qtype_unparse (qt)));
67   return qt;
68 }
69
70 # ifndef NOLCL
71 qtype qtype_addQualList (/*@returned@*/ qtype qt, qualList ql)
72 {
73   if (qtype_isDefined (qt))
74     {
75       qt->quals = qualList_appendList (qt->quals, ql);
76     }
77
78   return qt;
79 }
80 # endif
81
82 static void checkAltQuals (qtype q)
83 {
84   if (qtype_isDefined (q))
85     {
86       qualList badQuals = qualList_undefined;
87       
88       qualList_elements (q->quals, qu)
89         {
90           if (!qual_isCQual (qu) && !qual_isImplied (qu))
91             {
92               badQuals = qualList_add (badQuals, qu);
93             }
94         } end_qualList_elements ;
95       
96       if (!qualList_isEmpty (badQuals))
97         {
98           voptgenerror (FLG_SYNTAX,
99                         message 
100                         ("Alternate type cannot use annotations %q: %q",
101                          qualList_unparse (badQuals),
102                          qtype_unparse (q)),
103                         g_currentloc);
104         }
105     }
106 }
107
108 # ifndef NOLCL
109 qtype qtype_mergeImplicitAlt (/*@returned@*/ qtype q1, /*@only@*/ qtype q2)
110 {
111   if (qtype_isDefined (q1) && qtype_isDefined (q2))
112     {
113       q1->type = ctype_makeConj (q1->type, q2->type);
114       
115       if (!qualList_isEmpty (q2->quals))
116         {
117           checkAltQuals (q2);
118         }
119     }
120
121   qtype_free (q2);
122   return q1;
123 }
124 # endif
125
126 qtype qtype_mergeAlt (/*@returned@*/ qtype q1, /*@only@*/ qtype q2)
127 {
128   DPRINTF (("Merge alt: %s + %s", qtype_unparse (q1), qtype_unparse (q2)));
129
130   if (qtype_isDefined (q1) && qtype_isDefined (q2))
131     {
132       if (context_getFlag (FLG_IMPCONJ))
133         {
134           q1->type = ctype_makeConj (q1->type, q2->type);
135         }
136       else
137         {
138           q1->type = ctype_makeExplicitConj (q1->type, q2->type);
139         }
140       
141       if (!qualList_isEmpty (q2->quals))
142         {
143           checkAltQuals (q2);
144         }
145     }
146   
147   qtype_free (q2);
148   return q1;
149 }
150
151 qtype qtype_combine (/*@returned@*/ qtype q1, ctype ct)
152 {
153   if (qtype_isDefined (q1))
154     {
155       /* ct is modifier (or q1->type is unknown) */
156       q1->type = ctype_combine (q1->type, ct); 
157     }
158
159   return q1;
160 }
161
162 qtype qtype_resolve (/*@returned@*/ qtype q)
163 {
164   if (qtype_isDefined (q))
165     {
166       DPRINTF (("Resolving: %s", qtype_unparse (q)));
167       q->type = ctype_resolve (q->type);
168       DPRINTF (("Resolving: %s", qtype_unparse (q)));
169     }
170
171   return q;
172 }
173
174 cstring qtype_unparse (qtype q)
175 {
176   if (qtype_isDefined (q))
177     {
178       return (message ("%q%s", qualList_unparse (q->quals), 
179                        ctype_unparse (q->type)));
180     }
181   else
182     {
183       return (cstring_makeLiteral ("<undefined>"));
184     }
185 }
186
187 qtype qtype_newBase (/*@returned@*/ qtype q, ctype ct)
188 {
189   if (qtype_isDefined (q))
190     {
191       q->type = ctype_newBase (ct, q->type);
192     }
193
194   return q;
195 }
196
197 qtype qtype_newQbase (qtype q1, qtype q2)
198 {
199   if (qtype_isDefined (q1) && qtype_isDefined (q2))
200     {
201       q1->type = ctype_newBase (q1->type, q2->type);
202       q1->quals = qualList_appendList (q1->quals, q2->quals);
203     }
204
205   return q1;
206 }
207
208 void qtype_adjustPointers (int n, qtype q)
209 {
210   if (qtype_isDefined (q))
211     {
212       q->type = ctype_adjustPointers (n, q->type);
213     }
214 }
215
216 # ifndef NOLCL
217 qtype qtype_copy (qtype q)
218 {
219   if (qtype_isDefined (q))
220     {
221       qtype r = qtype_create (q->type);
222       
223       qualList_free (r->quals);
224       r->quals = qualList_copy (q->quals);
225       return r;
226     }
227   else
228     {
229       return qtype_undefined;
230     }
231 }
232 # endif
This page took 0.040518 seconds and 3 git commands to generate.