Merge branch 'mesa_7_7_branch'
[mesa.git] / src / glsl / pp / sl_pp_if.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include "sl_pp_expression.h"
31 #include "sl_pp_process.h"
32
33
34 static int
35 _parse_defined(struct sl_pp_context *context,
36 struct sl_pp_token_buffer *buffer,
37 struct sl_pp_process_state *state)
38 {
39 struct sl_pp_token_info input;
40 int parens = 0;
41 int macro_name;
42 struct sl_pp_macro *macro;
43 int defined = 0;
44 struct sl_pp_token_info result;
45
46 if (sl_pp_token_buffer_skip_white(buffer, &input)) {
47 return -1;
48 }
49
50 if (input.token == SL_PP_LPAREN) {
51 if (sl_pp_token_buffer_skip_white(buffer, &input)) {
52 return -1;
53 }
54 parens = 1;
55 }
56
57 if (input.token != SL_PP_IDENTIFIER) {
58 strcpy(context->error_msg, "expected an identifier");
59 return -1;
60 }
61
62 macro_name = input.data.identifier;
63 for (macro = context->macro; macro; macro = macro->next) {
64 if (macro->name == macro_name) {
65 defined = 1;
66 break;
67 }
68 }
69
70 if (parens) {
71 if (sl_pp_token_buffer_skip_white(buffer, &input)) {
72 return -1;
73 }
74 if (input.token != SL_PP_RPAREN) {
75 strcpy(context->error_msg, "expected `)'");
76 return -1;
77 }
78 }
79
80 result.token = SL_PP_UINT;
81 result.data._uint = (defined ? context->dict._1 : context->dict._0);
82
83 if (sl_pp_process_out(state, &result)) {
84 strcpy(context->error_msg, "out of memory");
85 return -1;
86 }
87
88 return 0;
89 }
90
91 static unsigned int
92 _evaluate_if_stack(struct sl_pp_context *context)
93 {
94 unsigned int i;
95
96 for (i = context->if_ptr; i < SL_PP_MAX_IF_NESTING; i++) {
97 if (!(context->if_stack[i] & 1)) {
98 return 0;
99 }
100 }
101 return 1;
102 }
103
104 static int
105 _parse_if(struct sl_pp_context *context,
106 struct sl_pp_token_buffer *buffer)
107 {
108 struct sl_pp_process_state state;
109 int found_end = 0;
110 struct sl_pp_token_info eof;
111 int result;
112
113 if (!context->if_ptr) {
114 strcpy(context->error_msg, "`#if' nesting too deep");
115 return -1;
116 }
117
118 memset(&state, 0, sizeof(state));
119 while (!found_end) {
120 struct sl_pp_token_info input;
121
122 sl_pp_token_buffer_get(buffer, &input);
123 switch (input.token) {
124 case SL_PP_WHITESPACE:
125 break;
126
127 case SL_PP_IDENTIFIER:
128 if (input.data.identifier == context->dict.defined) {
129 if (_parse_defined(context, buffer, &state)) {
130 free(state.out);
131 return -1;
132 }
133 } else {
134 sl_pp_token_buffer_unget(buffer, &input);
135 if (sl_pp_macro_expand(context, buffer, NULL, &state, sl_pp_macro_expand_unknown_to_0)) {
136 free(state.out);
137 return -1;
138 }
139 }
140 break;
141
142 case SL_PP_NEWLINE:
143 case SL_PP_EOF:
144 found_end = 1;
145 break;
146
147 default:
148 if (sl_pp_process_out(&state, &input)) {
149 strcpy(context->error_msg, "out of memory");
150 free(state.out);
151 return -1;
152 }
153 }
154 }
155
156 eof.token = SL_PP_EOF;
157 if (sl_pp_process_out(&state, &eof)) {
158 strcpy(context->error_msg, "out of memory");
159 free(state.out);
160 return -1;
161 }
162
163 if (sl_pp_execute_expression(context, state.out, &result)) {
164 free(state.out);
165 return -1;
166 }
167
168 free(state.out);
169
170 context->if_ptr--;
171 context->if_stack[context->if_ptr] = result ? 1 : 0;
172 context->if_value = _evaluate_if_stack(context);
173
174 return 0;
175 }
176
177 static int
178 _parse_else(struct sl_pp_context *context)
179 {
180 if (context->if_ptr == SL_PP_MAX_IF_NESTING) {
181 strcpy(context->error_msg, "no matching `#if'");
182 return -1;
183 }
184
185 /* Bit b1 indicates we already went through #else. */
186 if (context->if_stack[context->if_ptr] & 2) {
187 strcpy(context->error_msg, "no matching `#if'");
188 return -1;
189 }
190
191 /* Invert current condition value and mark that we are in the #else block. */
192 context->if_stack[context->if_ptr] = (1 - (context->if_stack[context->if_ptr] & 1)) | 2;
193 context->if_value = _evaluate_if_stack(context);
194
195 return 0;
196 }
197
198 int
199 sl_pp_process_if(struct sl_pp_context *context,
200 struct sl_pp_token_buffer *buffer)
201 {
202 return _parse_if(context, buffer);
203 }
204
205 int
206 sl_pp_process_ifdef(struct sl_pp_context *context,
207 const struct sl_pp_token_info *input,
208 unsigned int first,
209 unsigned int last)
210 {
211 unsigned int i;
212
213 if (!context->if_ptr) {
214 strcpy(context->error_msg, "`#if' nesting too deep");
215 return -1;
216 }
217
218 for (i = first; i < last; i++) {
219 switch (input[i].token) {
220 case SL_PP_IDENTIFIER:
221 {
222 struct sl_pp_macro *macro;
223 int macro_name = input[i].data.identifier;
224 int defined = 0;
225
226 for (macro = context->macro; macro; macro = macro->next) {
227 if (macro->name == macro_name) {
228 defined = 1;
229 break;
230 }
231 }
232
233 context->if_ptr--;
234 context->if_stack[context->if_ptr] = defined ? 1 : 0;
235 context->if_value = _evaluate_if_stack(context);
236 }
237 return 0;
238
239 case SL_PP_WHITESPACE:
240 break;
241
242 default:
243 strcpy(context->error_msg, "expected an identifier");
244 return -1;
245 }
246 }
247
248 strcpy(context->error_msg, "expected an identifier");
249 return -1;
250 }
251
252 int
253 sl_pp_process_ifndef(struct sl_pp_context *context,
254 const struct sl_pp_token_info *input,
255 unsigned int first,
256 unsigned int last)
257 {
258 unsigned int i;
259
260 if (!context->if_ptr) {
261 strcpy(context->error_msg, "`#if' nesting too deep");
262 return -1;
263 }
264
265 for (i = first; i < last; i++) {
266 switch (input[i].token) {
267 case SL_PP_IDENTIFIER:
268 {
269 struct sl_pp_macro *macro;
270 int macro_name = input[i].data.identifier;
271 int defined = 0;
272
273 for (macro = context->macro; macro; macro = macro->next) {
274 if (macro->name == macro_name) {
275 defined = 1;
276 break;
277 }
278 }
279
280 context->if_ptr--;
281 context->if_stack[context->if_ptr] = defined ? 0 : 1;
282 context->if_value = _evaluate_if_stack(context);
283 }
284 return 0;
285
286 case SL_PP_WHITESPACE:
287 break;
288
289 default:
290 strcpy(context->error_msg, "expected an identifier");
291 return -1;
292 }
293 }
294
295 strcpy(context->error_msg, "expected an identifier");
296 return -1;
297 }
298
299 int
300 sl_pp_process_elif(struct sl_pp_context *context,
301 struct sl_pp_token_buffer *buffer)
302 {
303 if (_parse_else(context)) {
304 return -1;
305 }
306
307 if (context->if_stack[context->if_ptr] & 1) {
308 context->if_ptr++;
309 if (_parse_if(context, buffer)) {
310 return -1;
311 }
312 }
313
314 /* We are still in the #if block. */
315 context->if_stack[context->if_ptr] = context->if_stack[context->if_ptr] & ~2;
316
317 return 0;
318 }
319
320 int
321 sl_pp_process_else(struct sl_pp_context *context,
322 const struct sl_pp_token_info *input,
323 unsigned int first,
324 unsigned int last)
325 {
326 return _parse_else(context);
327 }
328
329 int
330 sl_pp_process_endif(struct sl_pp_context *context,
331 const struct sl_pp_token_info *input,
332 unsigned int first,
333 unsigned int last)
334 {
335 if (context->if_ptr == SL_PP_MAX_IF_NESTING) {
336 strcpy(context->error_msg, "no matching `#if'");
337 return -1;
338 }
339
340 context->if_ptr++;
341 context->if_value = _evaluate_if_stack(context);
342
343 return 0;
344 }