blob: 009aabcd354677d64f8bcf062d0e4212fffb33e2 [file] [log] [blame]
Ian Romanicka87ac252010-02-22 13:19:34 -08001/*
2 * Copyright © 2008, 2009 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23#include <stdio.h>
24#include <stdarg.h>
Ian Romanicka87ac252010-02-22 13:19:34 -080025#include <string.h>
26#include <assert.h>
Ian Romanicka87ac252010-02-22 13:19:34 -080027
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -070028extern "C" {
29#include <talloc.h>
Ian Romanick667f4e12010-06-30 16:42:07 -070030#include "main/mtypes.h"
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -070031}
32
Ian Romanicka87ac252010-02-22 13:19:34 -080033#include "ast.h"
34#include "glsl_parser_extras.h"
Ian Romanickd59673c2010-02-25 17:17:23 -080035#include "glsl_parser.h"
Ian Romanicka87ac252010-02-22 13:19:34 -080036
Ian Romanick2462a532010-07-18 15:59:43 -070037_mesa_glsl_parse_state::_mesa_glsl_parse_state(struct __GLcontextRec *ctx,
38 GLenum target, void *mem_ctx)
39{
40 switch (target) {
41 case GL_VERTEX_SHADER: this->target = vertex_shader; break;
42 case GL_FRAGMENT_SHADER: this->target = fragment_shader; break;
43 case GL_GEOMETRY_SHADER: this->target = geometry_shader; break;
44 }
45
46 this->scanner = NULL;
47 this->translation_unit.make_empty();
48 this->symbols = new(mem_ctx) glsl_symbol_table;
49 this->info_log = talloc_strdup(mem_ctx, "");
50 this->error = false;
51 this->loop_or_switch_nesting = NULL;
52 this->ARB_texture_rectangle_enable = true;
53
54 if (ctx != NULL) {
55 this->extensions = &ctx->Extensions;
56
57 this->Const.MaxLights = ctx->Const.MaxLights;
58 this->Const.MaxClipPlanes = ctx->Const.MaxClipPlanes;
59 this->Const.MaxTextureUnits = ctx->Const.MaxTextureUnits;
60 this->Const.MaxTextureCoords = ctx->Const.MaxTextureCoordUnits;
61 this->Const.MaxVertexAttribs = ctx->Const.VertexProgram.MaxAttribs;
62 this->Const.MaxVertexUniformComponents = ctx->Const.VertexProgram.MaxUniformComponents;
63 this->Const.MaxVaryingFloats = ctx->Const.MaxVarying * 4;
64 this->Const.MaxVertexTextureImageUnits = ctx->Const.MaxVertexTextureImageUnits;
65 this->Const.MaxCombinedTextureImageUnits = ctx->Const.MaxCombinedTextureImageUnits;
66 this->Const.MaxTextureImageUnits = ctx->Const.MaxTextureImageUnits;
67 this->Const.MaxFragmentUniformComponents = ctx->Const.FragmentProgram.MaxUniformComponents;
68
69 this->Const.MaxDrawBuffers = ctx->Const.MaxDrawBuffers;
70 } else {
Kenneth Graunkeaa9f86a2010-07-22 16:20:36 -070071 /* If there is no GL context (standalone compiler), fill in constants
72 * with the minimum required values.
73 */
Ian Romanick2462a532010-07-18 15:59:43 -070074 static struct gl_extensions null_extensions;
75
76 memset(&null_extensions, 0, sizeof(null_extensions));
77 this->extensions = &null_extensions;
Kenneth Graunkeaa9f86a2010-07-22 16:20:36 -070078
79 /* 1.10 minimums. */
80 this->Const.MaxLights = 8;
81 this->Const.MaxClipPlanes = 8;
82 this->Const.MaxTextureUnits = 2;
83
84 /* More than the 1.10 minimum to appease parser tests taken from
85 * apps that (hopefully) already checked the number of coords.
86 */
87 this->Const.MaxTextureCoords = 4;
88
89 this->Const.MaxVertexAttribs = 16;
90 this->Const.MaxVertexUniformComponents = 512;
91 this->Const.MaxVaryingFloats = 32;
92 this->Const.MaxVertexTextureImageUnits = 0;
93 this->Const.MaxCombinedTextureImageUnits = 2;
94 this->Const.MaxTextureImageUnits = 2;
95 this->Const.MaxFragmentUniformComponents = 64;
96
97 this->Const.MaxDrawBuffers = 2;
Ian Romanick2462a532010-07-18 15:59:43 -070098 }
99}
100
Ian Romanick5bfe30a2010-04-07 16:44:30 -0700101const char *
102_mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target)
103{
104 switch (target) {
105 case vertex_shader: return "vertex";
106 case fragment_shader: return "fragment";
107 case geometry_shader: return "geometry";
Eric Anholt81f49a72010-04-29 17:57:28 -0700108 case ir_shader: break;
Ian Romanick5bfe30a2010-04-07 16:44:30 -0700109 }
110
111 assert(!"Should not get here.");
Eric Anholt87a2ee82010-07-18 17:47:15 -0700112 return "unknown";
Ian Romanick5bfe30a2010-04-07 16:44:30 -0700113}
114
115
Ian Romanicka87ac252010-02-22 13:19:34 -0800116void
Ian Romanick1f585182010-03-11 14:08:33 -0800117_mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
118 const char *fmt, ...)
Ian Romanicka87ac252010-02-22 13:19:34 -0800119{
Ian Romanicka87ac252010-02-22 13:19:34 -0800120 va_list ap;
121
Ian Romanick71d0bbf2010-03-23 13:21:19 -0700122 state->error = true;
Ian Romanick1f585182010-03-11 14:08:33 -0800123
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -0700124 assert(state->info_log != NULL);
125 state->info_log = talloc_asprintf_append(state->info_log,
126 "%u:%u(%u): error: ",
127 locp->source,
128 locp->first_line,
129 locp->first_column);
Ian Romanicka87ac252010-02-22 13:19:34 -0800130 va_start(ap, fmt);
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -0700131 state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap);
Ian Romanicka87ac252010-02-22 13:19:34 -0800132 va_end(ap);
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -0700133 state->info_log = talloc_strdup_append(state->info_log, "\n");
Ian Romanicka87ac252010-02-22 13:19:34 -0800134}
135
136
Ian Romanick56b8b212010-04-07 14:47:46 -0700137void
Eric Anholt3623df62010-04-29 18:00:33 -0700138_mesa_glsl_warning(const YYLTYPE *locp, _mesa_glsl_parse_state *state,
Ian Romanick56b8b212010-04-07 14:47:46 -0700139 const char *fmt, ...)
140{
Ian Romanick56b8b212010-04-07 14:47:46 -0700141 va_list ap;
142
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -0700143 assert(state->info_log != NULL);
144 state->info_log = talloc_asprintf_append(state->info_log,
145 "%u:%u(%u): warning: ",
146 locp->source,
147 locp->first_line,
148 locp->first_column);
Ian Romanick56b8b212010-04-07 14:47:46 -0700149 va_start(ap, fmt);
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -0700150 state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap);
Ian Romanick56b8b212010-04-07 14:47:46 -0700151 va_end(ap);
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -0700152 state->info_log = talloc_strdup_append(state->info_log, "\n");
Ian Romanick56b8b212010-04-07 14:47:46 -0700153}
154
155
Ian Romanicke7017612010-04-07 16:46:25 -0700156bool
157_mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
158 const char *behavior, YYLTYPE *behavior_locp,
159 _mesa_glsl_parse_state *state)
160{
161 enum {
162 extension_disable,
163 extension_enable,
164 extension_require,
165 extension_warn
166 } ext_mode;
Ian Romanicke7017612010-04-07 16:46:25 -0700167
168 if (strcmp(behavior, "warn") == 0) {
169 ext_mode = extension_warn;
170 } else if (strcmp(behavior, "require") == 0) {
171 ext_mode = extension_require;
172 } else if (strcmp(behavior, "enable") == 0) {
173 ext_mode = extension_enable;
174 } else if (strcmp(behavior, "disable") == 0) {
175 ext_mode = extension_disable;
176 } else {
177 _mesa_glsl_error(behavior_locp, state,
178 "Unknown extension behavior `%s'",
179 behavior);
180 return false;
181 }
182
Ian Romanick887a8b02010-04-07 16:57:56 -0700183 bool unsupported = false;
184
Ian Romanicke7017612010-04-07 16:46:25 -0700185 if (strcmp(name, "all") == 0) {
186 if ((ext_mode == extension_enable) || (ext_mode == extension_require)) {
187 _mesa_glsl_error(name_locp, state, "Cannot %s all extensions",
188 (ext_mode == extension_enable)
189 ? "enable" : "require");
190 return false;
191 }
Ian Romanickeb56cea2010-04-23 13:32:23 -0700192 } else if (strcmp(name, "GL_ARB_draw_buffers") == 0) {
Ian Romanickc77b2572010-04-07 16:59:46 -0700193 /* This extension is only supported in fragment shaders.
194 */
195 if (state->target != fragment_shader) {
196 unsupported = true;
197 } else {
198 state->ARB_draw_buffers_enable = (ext_mode != extension_disable);
199 state->ARB_draw_buffers_warn = (ext_mode == extension_warn);
200 }
Ian Romanickeb56cea2010-04-23 13:32:23 -0700201 } else if (strcmp(name, "GL_ARB_texture_rectangle") == 0) {
Ian Romanick0c824652010-04-07 17:13:44 -0700202 state->ARB_texture_rectangle_enable = (ext_mode != extension_disable);
203 state->ARB_texture_rectangle_warn = (ext_mode == extension_warn);
Ian Romanick667f4e12010-06-30 16:42:07 -0700204 } else if (strcmp(name, "GL_EXT_texture_array") == 0) {
205 state->EXT_texture_array_enable = (ext_mode != extension_disable);
206 state->EXT_texture_array_warn = (ext_mode == extension_warn);
207
208 unsupported = !state->extensions->EXT_texture_array;
Ian Romanicke7017612010-04-07 16:46:25 -0700209 } else {
Ian Romanick887a8b02010-04-07 16:57:56 -0700210 unsupported = true;
211 }
212
213 if (unsupported) {
214 static const char *const fmt = "extension `%s' unsupported in %s shader";
215
Ian Romanicke7017612010-04-07 16:46:25 -0700216 if (ext_mode == extension_require) {
Ian Romanick887a8b02010-04-07 16:57:56 -0700217 _mesa_glsl_error(name_locp, state, fmt,
218 name, _mesa_glsl_shader_target_name(state->target));
Ian Romanicke7017612010-04-07 16:46:25 -0700219 return false;
Ian Romanick1799a0c2010-04-07 14:50:36 -0700220 } else {
Ian Romanick887a8b02010-04-07 16:57:56 -0700221 _mesa_glsl_warning(name_locp, state, fmt,
222 name, _mesa_glsl_shader_target_name(state->target));
Ian Romanicke7017612010-04-07 16:46:25 -0700223 }
224 }
225
226 return true;
227}
228
Ian Romanicka87ac252010-02-22 13:19:34 -0800229void
230_mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)
231{
232 if (q->constant)
233 printf("const ");
234
235 if (q->invariant)
236 printf("invariant ");
237
238 if (q->attribute)
239 printf("attribute ");
240
241 if (q->varying)
242 printf("varying ");
243
244 if (q->in && q->out)
245 printf("inout ");
246 else {
247 if (q->in)
248 printf("in ");
249
250 if (q->out)
251 printf("out ");
252 }
253
254 if (q->centroid)
255 printf("centroid ");
256 if (q->uniform)
257 printf("uniform ");
258 if (q->smooth)
259 printf("smooth ");
260 if (q->flat)
261 printf("flat ");
262 if (q->noperspective)
263 printf("noperspective ");
264}
265
266
267void
268ast_node::print(void) const
269{
Ian Romanick03d3f3a2010-04-02 11:03:47 -0700270 printf("unhandled node ");
Ian Romanicka87ac252010-02-22 13:19:34 -0800271}
272
273
274ast_node::ast_node(void)
275{
Ian Romanick304ea902010-05-10 11:17:53 -0700276 /* empty */
Ian Romanicka87ac252010-02-22 13:19:34 -0800277}
278
Ian Romanicka87ac252010-02-22 13:19:34 -0800279
280static void
281ast_opt_array_size_print(bool is_array, const ast_expression *array_size)
282{
283 if (is_array) {
284 printf("[ ");
285
286 if (array_size)
287 array_size->print();
288
289 printf("] ");
290 }
291}
292
293
Ian Romanicka87ac252010-02-22 13:19:34 -0800294void
295ast_compound_statement::print(void) const
296{
Ian Romanicka87ac252010-02-22 13:19:34 -0800297 printf("{\n");
298
Ian Romanick304ea902010-05-10 11:17:53 -0700299 foreach_list_const(n, &this->statements) {
300 ast_node *ast = exec_node_data(ast_node, n, link);
301 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800302 }
303
304 printf("}\n");
305}
306
307
308ast_compound_statement::ast_compound_statement(int new_scope,
309 ast_node *statements)
310{
311 this->new_scope = new_scope;
Ian Romanicka87ac252010-02-22 13:19:34 -0800312
313 if (statements != NULL) {
Ian Romanick304ea902010-05-10 11:17:53 -0700314 this->statements.push_degenerate_list_at_head(&statements->link);
Ian Romanicka87ac252010-02-22 13:19:34 -0800315 }
316}
317
318
319void
320ast_expression::print(void) const
321{
Ian Romanicka87ac252010-02-22 13:19:34 -0800322 switch (oper) {
323 case ast_assign:
Ian Romanicka87ac252010-02-22 13:19:34 -0800324 case ast_mul_assign:
325 case ast_div_assign:
326 case ast_mod_assign:
327 case ast_add_assign:
328 case ast_sub_assign:
329 case ast_ls_assign:
330 case ast_rs_assign:
331 case ast_and_assign:
332 case ast_xor_assign:
333 case ast_or_assign:
334 subexpressions[0]->print();
Ian Romanick88349b22010-02-22 19:10:25 -0800335 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800336 subexpressions[1]->print();
337 break;
338
339 case ast_field_selection:
340 subexpressions[0]->print();
341 printf(". %s ", primary_expression.identifier);
342 break;
343
344 case ast_plus:
345 case ast_neg:
346 case ast_bit_not:
347 case ast_logic_not:
348 case ast_pre_inc:
349 case ast_pre_dec:
Ian Romanick88349b22010-02-22 19:10:25 -0800350 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800351 subexpressions[0]->print();
352 break;
353
354 case ast_post_inc:
355 case ast_post_dec:
356 subexpressions[0]->print();
Ian Romanick88349b22010-02-22 19:10:25 -0800357 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800358 break;
359
360 case ast_conditional:
361 subexpressions[0]->print();
362 printf("? ");
363 subexpressions[1]->print();
364 printf(": ");
365 subexpressions[1]->print();
366 break;
367
368 case ast_array_index:
369 subexpressions[0]->print();
370 printf("[ ");
371 subexpressions[1]->print();
372 printf("] ");
373 break;
374
375 case ast_function_call: {
Ian Romanicka87ac252010-02-22 13:19:34 -0800376 subexpressions[0]->print();
377 printf("( ");
378
Ian Romanick304ea902010-05-10 11:17:53 -0700379 foreach_list_const (n, &this->expressions) {
Ian Romanick23849372010-05-14 16:06:41 -0700380 if (n != this->expressions.get_head())
381 printf(", ");
Ian Romanick304ea902010-05-10 11:17:53 -0700382
383 ast_node *ast = exec_node_data(ast_node, n, link);
384 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800385 }
386
387 printf(") ");
388 break;
389 }
390
391 case ast_identifier:
392 printf("%s ", primary_expression.identifier);
393 break;
394
395 case ast_int_constant:
396 printf("%d ", primary_expression.int_constant);
397 break;
398
399 case ast_uint_constant:
400 printf("%u ", primary_expression.uint_constant);
401 break;
402
403 case ast_float_constant:
404 printf("%f ", primary_expression.float_constant);
405 break;
406
407 case ast_bool_constant:
408 printf("%s ",
409 primary_expression.bool_constant
410 ? "true" : "false");
411 break;
412
413 case ast_sequence: {
Ian Romanicka87ac252010-02-22 13:19:34 -0800414 printf("( ");
Ian Romanick304ea902010-05-10 11:17:53 -0700415 foreach_list_const(n, & this->expressions) {
416 if (n != this->expressions.get_head())
Ian Romanicka87ac252010-02-22 13:19:34 -0800417 printf(", ");
418
Ian Romanick304ea902010-05-10 11:17:53 -0700419 ast_node *ast = exec_node_data(ast_node, n, link);
420 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800421 }
422 printf(") ");
423 break;
424 }
Ian Romanick88349b22010-02-22 19:10:25 -0800425
426 default:
427 assert(0);
428 break;
Ian Romanicka87ac252010-02-22 13:19:34 -0800429 }
430}
431
432ast_expression::ast_expression(int oper,
433 ast_expression *ex0,
434 ast_expression *ex1,
435 ast_expression *ex2)
436{
437 this->oper = ast_operators(oper);
438 this->subexpressions[0] = ex0;
439 this->subexpressions[1] = ex1;
440 this->subexpressions[2] = ex2;
Ian Romanicka87ac252010-02-22 13:19:34 -0800441}
442
443
444void
445ast_expression_statement::print(void) const
446{
447 if (expression)
448 expression->print();
449
450 printf("; ");
451}
452
453
454ast_expression_statement::ast_expression_statement(ast_expression *ex) :
455 expression(ex)
456{
457 /* empty */
458}
459
460
461void
462ast_function::print(void) const
463{
Ian Romanicka87ac252010-02-22 13:19:34 -0800464 return_type->print();
465 printf(" %s (", identifier);
466
Ian Romanick304ea902010-05-10 11:17:53 -0700467 foreach_list_const(n, & this->parameters) {
468 ast_node *ast = exec_node_data(ast_node, n, link);
469 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800470 }
471
472 printf(")");
473}
474
475
476ast_function::ast_function(void)
Ian Romanick92318a92010-03-31 18:23:21 -0700477 : is_definition(false), signature(NULL)
Ian Romanicka87ac252010-02-22 13:19:34 -0800478{
Ian Romanick304ea902010-05-10 11:17:53 -0700479 /* empty */
Ian Romanicka87ac252010-02-22 13:19:34 -0800480}
481
482
483void
484ast_fully_specified_type::print(void) const
485{
486 _mesa_ast_type_qualifier_print(& qualifier);
487 specifier->print();
488}
489
490
491void
492ast_parameter_declarator::print(void) const
493{
494 type->print();
495 if (identifier)
496 printf("%s ", identifier);
497 ast_opt_array_size_print(is_array, array_size);
498}
499
500
501void
502ast_function_definition::print(void) const
503{
504 prototype->print();
505 body->print();
506}
507
508
509void
510ast_declaration::print(void) const
511{
512 printf("%s ", identifier);
513 ast_opt_array_size_print(is_array, array_size);
514
515 if (initializer) {
516 printf("= ");
517 initializer->print();
518 }
519}
520
521
522ast_declaration::ast_declaration(char *identifier, int is_array,
523 ast_expression *array_size,
524 ast_expression *initializer)
525{
526 this->identifier = identifier;
527 this->is_array = is_array;
528 this->array_size = array_size;
529 this->initializer = initializer;
530}
531
532
533void
534ast_declarator_list::print(void) const
535{
Ian Romanicka87ac252010-02-22 13:19:34 -0800536 assert(type || invariant);
537
538 if (type)
539 type->print();
540 else
541 printf("invariant ");
542
Ian Romanick304ea902010-05-10 11:17:53 -0700543 foreach_list_const (ptr, & this->declarations) {
544 if (ptr != this->declarations.get_head())
Ian Romanicka87ac252010-02-22 13:19:34 -0800545 printf(", ");
546
Ian Romanick304ea902010-05-10 11:17:53 -0700547 ast_node *ast = exec_node_data(ast_node, ptr, link);
548 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800549 }
550
551 printf("; ");
552}
553
554
555ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type)
556{
557 this->type = type;
Ian Romanick38327062010-07-01 17:10:11 -0700558 this->invariant = false;
Ian Romanicka87ac252010-02-22 13:19:34 -0800559}
560
561void
562ast_jump_statement::print(void) const
563{
564 switch (mode) {
565 case ast_continue:
566 printf("continue; ");
567 break;
568 case ast_break:
569 printf("break; ");
570 break;
571 case ast_return:
572 printf("return ");
573 if (opt_return_value)
574 opt_return_value->print();
575
576 printf("; ");
577 break;
578 case ast_discard:
579 printf("discard; ");
580 break;
581 }
582}
583
584
585ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)
586{
587 this->mode = ast_jump_modes(mode);
588
589 if (mode == ast_return)
590 opt_return_value = return_value;
591}
592
593
594void
595ast_selection_statement::print(void) const
596{
597 printf("if ( ");
598 condition->print();
599 printf(") ");
600
601 then_statement->print();
602
603 if (else_statement) {
604 printf("else ");
605 else_statement->print();
606 }
607
608}
609
610
611ast_selection_statement::ast_selection_statement(ast_expression *condition,
612 ast_node *then_statement,
613 ast_node *else_statement)
614{
615 this->condition = condition;
616 this->then_statement = then_statement;
617 this->else_statement = else_statement;
618}
619
620
621void
622ast_iteration_statement::print(void) const
623{
624 switch (mode) {
625 case ast_for:
626 printf("for( ");
627 if (init_statement)
628 init_statement->print();
629 printf("; ");
630
631 if (condition)
632 condition->print();
633 printf("; ");
634
635 if (rest_expression)
636 rest_expression->print();
637 printf(") ");
638
639 body->print();
640 break;
641
642 case ast_while:
643 printf("while ( ");
644 if (condition)
645 condition->print();
646 printf(") ");
647 body->print();
648 break;
649
650 case ast_do_while:
651 printf("do ");
652 body->print();
653 printf("while ( ");
654 if (condition)
655 condition->print();
656 printf("); ");
657 break;
658 }
659}
660
661
662ast_iteration_statement::ast_iteration_statement(int mode,
663 ast_node *init,
664 ast_node *condition,
665 ast_expression *rest_expression,
666 ast_node *body)
667{
668 this->mode = ast_iteration_modes(mode);
669 this->init_statement = init;
670 this->condition = condition;
671 this->rest_expression = rest_expression;
672 this->body = body;
673}
674
675
676void
677ast_struct_specifier::print(void) const
678{
Ian Romanicka87ac252010-02-22 13:19:34 -0800679 printf("struct %s { ", name);
Ian Romanick304ea902010-05-10 11:17:53 -0700680 foreach_list_const(n, &this->declarations) {
681 ast_node *ast = exec_node_data(ast_node, n, link);
682 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800683 }
684 printf("} ");
685}
686
687
688ast_struct_specifier::ast_struct_specifier(char *identifier,
689 ast_node *declarator_list)
690{
691 name = identifier;
Ian Romanick304ea902010-05-10 11:17:53 -0700692 this->declarations.push_degenerate_list_at_head(&declarator_list->link);
Ian Romanicka87ac252010-02-22 13:19:34 -0800693}