]>
Commit | Line | Data |
---|---|---|
616915dd | 1 | /* |
11db3170 | 2 | ** Splint - annotation-assisted static program checker |
77d37419 | 3 | ** Copyright (C) 1994-2002 University of Virginia, |
616915dd | 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 | ** | |
155af98d | 20 | ** For information on splint: info@splint.org |
21 | ** To report a bug: splint-bug@splint.org | |
11db3170 | 22 | ** For more information: http://www.splint.org |
616915dd | 23 | */ |
24 | /* | |
25 | ** qtype.c | |
26 | ** | |
27 | ** Qualified types: a type qualifier list, and a ctype. | |
28 | ** qtypes are mutable | |
29 | */ | |
30 | ||
1b8ae690 | 31 | # include "splintMacros.nf" |
616915dd | 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 | { | |
37ae0b5e | 59 | DPRINTF (("Add qual: %s / %s", qtype_unparse (qt), qual_unparse (q))); |
60 | ||
616915dd | 61 | if (qtype_isDefined (qt)) |
62 | { | |
63 | qt->quals = qualList_add (qt->quals, q); | |
64 | } | |
65 | ||
37ae0b5e | 66 | DPRINTF (("==> %s", qtype_unparse (qt))); |
616915dd | 67 | return qt; |
68 | } | |
69 | ||
616915dd | 70 | qtype qtype_addQualList (/*@returned@*/ qtype qt, qualList ql) |
71 | { | |
72 | if (qtype_isDefined (qt)) | |
73 | { | |
74 | qt->quals = qualList_appendList (qt->quals, ql); | |
75 | } | |
76 | ||
77 | return qt; | |
78 | } | |
616915dd | 79 | |
80 | static void checkAltQuals (qtype q) | |
81 | { | |
82 | if (qtype_isDefined (q)) | |
83 | { | |
84 | qualList badQuals = qualList_undefined; | |
85 | ||
86 | qualList_elements (q->quals, qu) | |
87 | { | |
88 | if (!qual_isCQual (qu) && !qual_isImplied (qu)) | |
89 | { | |
90 | badQuals = qualList_add (badQuals, qu); | |
91 | } | |
92 | } end_qualList_elements ; | |
93 | ||
94 | if (!qualList_isEmpty (badQuals)) | |
95 | { | |
96 | voptgenerror (FLG_SYNTAX, | |
97 | message | |
98 | ("Alternate type cannot use annotations %q: %q", | |
99 | qualList_unparse (badQuals), | |
100 | qtype_unparse (q)), | |
101 | g_currentloc); | |
102 | } | |
103 | } | |
104 | } | |
105 | ||
616915dd | 106 | qtype qtype_mergeImplicitAlt (/*@returned@*/ qtype q1, /*@only@*/ qtype q2) |
107 | { | |
108 | if (qtype_isDefined (q1) && qtype_isDefined (q2)) | |
109 | { | |
110 | q1->type = ctype_makeConj (q1->type, q2->type); | |
111 | ||
112 | if (!qualList_isEmpty (q2->quals)) | |
113 | { | |
114 | checkAltQuals (q2); | |
115 | } | |
116 | } | |
117 | ||
118 | qtype_free (q2); | |
119 | return q1; | |
120 | } | |
616915dd | 121 | |
122 | qtype qtype_mergeAlt (/*@returned@*/ qtype q1, /*@only@*/ qtype q2) | |
123 | { | |
02b84d4b | 124 | DPRINTF (("Merge alt: %s + %s", qtype_unparse (q1), qtype_unparse (q2))); |
125 | ||
616915dd | 126 | if (qtype_isDefined (q1) && qtype_isDefined (q2)) |
127 | { | |
128 | if (context_getFlag (FLG_IMPCONJ)) | |
129 | { | |
130 | q1->type = ctype_makeConj (q1->type, q2->type); | |
131 | } | |
132 | else | |
133 | { | |
134 | q1->type = ctype_makeExplicitConj (q1->type, q2->type); | |
135 | } | |
616915dd | 136 | |
137 | if (!qualList_isEmpty (q2->quals)) | |
138 | { | |
139 | checkAltQuals (q2); | |
140 | } | |
141 | } | |
142 | ||
143 | qtype_free (q2); | |
144 | return q1; | |
145 | } | |
146 | ||
147 | qtype qtype_combine (/*@returned@*/ qtype q1, ctype ct) | |
148 | { | |
b87215ab | 149 | DPRINTF (("Combine: %s %s", qtype_unparse (q1), ctype_unparse (ct))); |
616915dd | 150 | if (qtype_isDefined (q1)) |
151 | { | |
152 | /* ct is modifier (or q1->type is unknown) */ | |
153 | q1->type = ctype_combine (q1->type, ct); | |
154 | } | |
155 | ||
b87215ab | 156 | DPRINTF (("Combine: %s %s", qtype_unparse (q1), ctype_unparse (ct))); |
616915dd | 157 | return q1; |
158 | } | |
159 | ||
160 | qtype qtype_resolve (/*@returned@*/ qtype q) | |
161 | { | |
162 | if (qtype_isDefined (q)) | |
163 | { | |
01a8227e | 164 | DPRINTF (("Resolving: %s", qtype_unparse (q))); |
616915dd | 165 | q->type = ctype_resolve (q->type); |
01a8227e | 166 | DPRINTF (("Resolving: %s", qtype_unparse (q))); |
616915dd | 167 | } |
168 | ||
169 | return q; | |
170 | } | |
171 | ||
172 | cstring qtype_unparse (qtype q) | |
173 | { | |
174 | if (qtype_isDefined (q)) | |
175 | { | |
176 | return (message ("%q%s", qualList_unparse (q->quals), | |
177 | ctype_unparse (q->type))); | |
178 | } | |
179 | else | |
180 | { | |
181 | return (cstring_makeLiteral ("<undefined>")); | |
182 | } | |
183 | } | |
184 | ||
185 | qtype qtype_newBase (/*@returned@*/ qtype q, ctype ct) | |
186 | { | |
187 | if (qtype_isDefined (q)) | |
188 | { | |
b87215ab | 189 | DPRINTF (("new base: %s -> %s", qtype_unparse (q), ctype_unparse (ct))); |
616915dd | 190 | q->type = ctype_newBase (ct, q->type); |
b87215ab | 191 | DPRINTF (("new base: %s -> %s", qtype_unparse (q), ctype_unparse (ct))); |
616915dd | 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 | ||
b87215ab | 205 | DPRINTF (("new base: %s -> %s", qtype_unparse (q1), qtype_unparse (q1))); |
616915dd | 206 | return q1; |
207 | } | |
208 | ||
f9264521 | 209 | void qtype_adjustPointers (pointers n, qtype q) |
616915dd | 210 | { |
211 | if (qtype_isDefined (q)) | |
212 | { | |
f9264521 | 213 | DPRINTF (("Pointers: %s %s", pointers_unparse (n), qtype_unparse (q))); |
616915dd | 214 | q->type = ctype_adjustPointers (n, q->type); |
215 | } | |
f9264521 | 216 | |
217 | pointers_free (n); | |
616915dd | 218 | } |
219 | ||
616915dd | 220 | qtype qtype_copy (qtype q) |
221 | { | |
222 | if (qtype_isDefined (q)) | |
223 | { | |
224 | qtype r = qtype_create (q->type); | |
225 | ||
226 | qualList_free (r->quals); | |
227 | r->quals = qualList_copy (q->quals); | |
228 | return r; | |
229 | } | |
230 | else | |
231 | { | |
232 | return qtype_undefined; | |
233 | } | |
234 | } |