blob: 15fa61d950586a19e2396991647487921612a4bb [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 Romanickf50f0652010-06-30 17:30:03 -0700201 } else if (strcmp(name, "GL_ARB_fragment_coord_conventions") == 0) {
202 state->ARB_fragment_coord_conventions_enable =
203 (ext_mode != extension_disable);
204 state->ARB_fragment_coord_conventions_warn =
205 (ext_mode == extension_warn);
206
207 unsupported = !state->extensions->ARB_fragment_coord_conventions;
Ian Romanickeb56cea2010-04-23 13:32:23 -0700208 } else if (strcmp(name, "GL_ARB_texture_rectangle") == 0) {
Ian Romanick0c824652010-04-07 17:13:44 -0700209 state->ARB_texture_rectangle_enable = (ext_mode != extension_disable);
210 state->ARB_texture_rectangle_warn = (ext_mode == extension_warn);
Ian Romanick667f4e12010-06-30 16:42:07 -0700211 } else if (strcmp(name, "GL_EXT_texture_array") == 0) {
212 state->EXT_texture_array_enable = (ext_mode != extension_disable);
213 state->EXT_texture_array_warn = (ext_mode == extension_warn);
214
215 unsupported = !state->extensions->EXT_texture_array;
Ian Romanicke7017612010-04-07 16:46:25 -0700216 } else {
Ian Romanick887a8b02010-04-07 16:57:56 -0700217 unsupported = true;
218 }
219
220 if (unsupported) {
221 static const char *const fmt = "extension `%s' unsupported in %s shader";
222
Ian Romanicke7017612010-04-07 16:46:25 -0700223 if (ext_mode == extension_require) {
Ian Romanick887a8b02010-04-07 16:57:56 -0700224 _mesa_glsl_error(name_locp, state, fmt,
225 name, _mesa_glsl_shader_target_name(state->target));
Ian Romanicke7017612010-04-07 16:46:25 -0700226 return false;
Ian Romanick1799a0c2010-04-07 14:50:36 -0700227 } else {
Ian Romanick887a8b02010-04-07 16:57:56 -0700228 _mesa_glsl_warning(name_locp, state, fmt,
229 name, _mesa_glsl_shader_target_name(state->target));
Ian Romanicke7017612010-04-07 16:46:25 -0700230 }
231 }
232
233 return true;
234}
235
Ian Romanicka87ac252010-02-22 13:19:34 -0800236void
237_mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)
238{
239 if (q->constant)
240 printf("const ");
241
242 if (q->invariant)
243 printf("invariant ");
244
245 if (q->attribute)
246 printf("attribute ");
247
248 if (q->varying)
249 printf("varying ");
250
251 if (q->in && q->out)
252 printf("inout ");
253 else {
254 if (q->in)
255 printf("in ");
256
257 if (q->out)
258 printf("out ");
259 }
260
261 if (q->centroid)
262 printf("centroid ");
263 if (q->uniform)
264 printf("uniform ");
265 if (q->smooth)
266 printf("smooth ");
267 if (q->flat)
268 printf("flat ");
269 if (q->noperspective)
270 printf("noperspective ");
271}
272
273
274void
275ast_node::print(void) const
276{
Ian Romanick03d3f3a2010-04-02 11:03:47 -0700277 printf("unhandled node ");
Ian Romanicka87ac252010-02-22 13:19:34 -0800278}
279
280
281ast_node::ast_node(void)
282{
Carl Worthec9675e2010-07-29 16:39:36 -0700283 this->location.source = 0;
284 this->location.line = 0;
285 this->location.column = 0;
Ian Romanicka87ac252010-02-22 13:19:34 -0800286}
287
Ian Romanicka87ac252010-02-22 13:19:34 -0800288
289static void
290ast_opt_array_size_print(bool is_array, const ast_expression *array_size)
291{
292 if (is_array) {
293 printf("[ ");
294
295 if (array_size)
296 array_size->print();
297
298 printf("] ");
299 }
300}
301
302
Ian Romanicka87ac252010-02-22 13:19:34 -0800303void
304ast_compound_statement::print(void) const
305{
Ian Romanicka87ac252010-02-22 13:19:34 -0800306 printf("{\n");
307
Ian Romanick304ea902010-05-10 11:17:53 -0700308 foreach_list_const(n, &this->statements) {
309 ast_node *ast = exec_node_data(ast_node, n, link);
310 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800311 }
312
313 printf("}\n");
314}
315
316
317ast_compound_statement::ast_compound_statement(int new_scope,
318 ast_node *statements)
319{
320 this->new_scope = new_scope;
Ian Romanicka87ac252010-02-22 13:19:34 -0800321
322 if (statements != NULL) {
Ian Romanick304ea902010-05-10 11:17:53 -0700323 this->statements.push_degenerate_list_at_head(&statements->link);
Ian Romanicka87ac252010-02-22 13:19:34 -0800324 }
325}
326
327
328void
329ast_expression::print(void) const
330{
Ian Romanicka87ac252010-02-22 13:19:34 -0800331 switch (oper) {
332 case ast_assign:
Ian Romanicka87ac252010-02-22 13:19:34 -0800333 case ast_mul_assign:
334 case ast_div_assign:
335 case ast_mod_assign:
336 case ast_add_assign:
337 case ast_sub_assign:
338 case ast_ls_assign:
339 case ast_rs_assign:
340 case ast_and_assign:
341 case ast_xor_assign:
342 case ast_or_assign:
343 subexpressions[0]->print();
Ian Romanick88349b22010-02-22 19:10:25 -0800344 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800345 subexpressions[1]->print();
346 break;
347
348 case ast_field_selection:
349 subexpressions[0]->print();
350 printf(". %s ", primary_expression.identifier);
351 break;
352
353 case ast_plus:
354 case ast_neg:
355 case ast_bit_not:
356 case ast_logic_not:
357 case ast_pre_inc:
358 case ast_pre_dec:
Ian Romanick88349b22010-02-22 19:10:25 -0800359 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800360 subexpressions[0]->print();
361 break;
362
363 case ast_post_inc:
364 case ast_post_dec:
365 subexpressions[0]->print();
Ian Romanick88349b22010-02-22 19:10:25 -0800366 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800367 break;
368
369 case ast_conditional:
370 subexpressions[0]->print();
371 printf("? ");
372 subexpressions[1]->print();
373 printf(": ");
374 subexpressions[1]->print();
375 break;
376
377 case ast_array_index:
378 subexpressions[0]->print();
379 printf("[ ");
380 subexpressions[1]->print();
381 printf("] ");
382 break;
383
384 case ast_function_call: {
Ian Romanicka87ac252010-02-22 13:19:34 -0800385 subexpressions[0]->print();
386 printf("( ");
387
Ian Romanick304ea902010-05-10 11:17:53 -0700388 foreach_list_const (n, &this->expressions) {
Ian Romanick23849372010-05-14 16:06:41 -0700389 if (n != this->expressions.get_head())
390 printf(", ");
Ian Romanick304ea902010-05-10 11:17:53 -0700391
392 ast_node *ast = exec_node_data(ast_node, n, link);
393 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800394 }
395
396 printf(") ");
397 break;
398 }
399
400 case ast_identifier:
401 printf("%s ", primary_expression.identifier);
402 break;
403
404 case ast_int_constant:
405 printf("%d ", primary_expression.int_constant);
406 break;
407
408 case ast_uint_constant:
409 printf("%u ", primary_expression.uint_constant);
410 break;
411
412 case ast_float_constant:
413 printf("%f ", primary_expression.float_constant);
414 break;
415
416 case ast_bool_constant:
417 printf("%s ",
418 primary_expression.bool_constant
419 ? "true" : "false");
420 break;
421
422 case ast_sequence: {
Ian Romanicka87ac252010-02-22 13:19:34 -0800423 printf("( ");
Ian Romanick304ea902010-05-10 11:17:53 -0700424 foreach_list_const(n, & this->expressions) {
425 if (n != this->expressions.get_head())
Ian Romanicka87ac252010-02-22 13:19:34 -0800426 printf(", ");
427
Ian Romanick304ea902010-05-10 11:17:53 -0700428 ast_node *ast = exec_node_data(ast_node, n, link);
429 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800430 }
431 printf(") ");
432 break;
433 }
Ian Romanick88349b22010-02-22 19:10:25 -0800434
435 default:
436 assert(0);
437 break;
Ian Romanicka87ac252010-02-22 13:19:34 -0800438 }
439}
440
441ast_expression::ast_expression(int oper,
442 ast_expression *ex0,
443 ast_expression *ex1,
444 ast_expression *ex2)
445{
446 this->oper = ast_operators(oper);
447 this->subexpressions[0] = ex0;
448 this->subexpressions[1] = ex1;
449 this->subexpressions[2] = ex2;
Ian Romanicka87ac252010-02-22 13:19:34 -0800450}
451
452
453void
454ast_expression_statement::print(void) const
455{
456 if (expression)
457 expression->print();
458
459 printf("; ");
460}
461
462
463ast_expression_statement::ast_expression_statement(ast_expression *ex) :
464 expression(ex)
465{
466 /* empty */
467}
468
469
470void
471ast_function::print(void) const
472{
Ian Romanicka87ac252010-02-22 13:19:34 -0800473 return_type->print();
474 printf(" %s (", identifier);
475
Ian Romanick304ea902010-05-10 11:17:53 -0700476 foreach_list_const(n, & this->parameters) {
477 ast_node *ast = exec_node_data(ast_node, n, link);
478 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800479 }
480
481 printf(")");
482}
483
484
485ast_function::ast_function(void)
Ian Romanick92318a92010-03-31 18:23:21 -0700486 : is_definition(false), signature(NULL)
Ian Romanicka87ac252010-02-22 13:19:34 -0800487{
Ian Romanick304ea902010-05-10 11:17:53 -0700488 /* empty */
Ian Romanicka87ac252010-02-22 13:19:34 -0800489}
490
491
492void
493ast_fully_specified_type::print(void) const
494{
495 _mesa_ast_type_qualifier_print(& qualifier);
496 specifier->print();
497}
498
499
500void
501ast_parameter_declarator::print(void) const
502{
503 type->print();
504 if (identifier)
505 printf("%s ", identifier);
506 ast_opt_array_size_print(is_array, array_size);
507}
508
509
510void
511ast_function_definition::print(void) const
512{
513 prototype->print();
514 body->print();
515}
516
517
518void
519ast_declaration::print(void) const
520{
521 printf("%s ", identifier);
522 ast_opt_array_size_print(is_array, array_size);
523
524 if (initializer) {
525 printf("= ");
526 initializer->print();
527 }
528}
529
530
531ast_declaration::ast_declaration(char *identifier, int is_array,
532 ast_expression *array_size,
533 ast_expression *initializer)
534{
535 this->identifier = identifier;
536 this->is_array = is_array;
537 this->array_size = array_size;
538 this->initializer = initializer;
539}
540
541
542void
543ast_declarator_list::print(void) const
544{
Ian Romanicka87ac252010-02-22 13:19:34 -0800545 assert(type || invariant);
546
547 if (type)
548 type->print();
549 else
550 printf("invariant ");
551
Ian Romanick304ea902010-05-10 11:17:53 -0700552 foreach_list_const (ptr, & this->declarations) {
553 if (ptr != this->declarations.get_head())
Ian Romanicka87ac252010-02-22 13:19:34 -0800554 printf(", ");
555
Ian Romanick304ea902010-05-10 11:17:53 -0700556 ast_node *ast = exec_node_data(ast_node, ptr, link);
557 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800558 }
559
560 printf("; ");
561}
562
563
564ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type)
565{
566 this->type = type;
Ian Romanick38327062010-07-01 17:10:11 -0700567 this->invariant = false;
Ian Romanicka87ac252010-02-22 13:19:34 -0800568}
569
570void
571ast_jump_statement::print(void) const
572{
573 switch (mode) {
574 case ast_continue:
575 printf("continue; ");
576 break;
577 case ast_break:
578 printf("break; ");
579 break;
580 case ast_return:
581 printf("return ");
582 if (opt_return_value)
583 opt_return_value->print();
584
585 printf("; ");
586 break;
587 case ast_discard:
588 printf("discard; ");
589 break;
590 }
591}
592
593
594ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)
595{
596 this->mode = ast_jump_modes(mode);
597
598 if (mode == ast_return)
599 opt_return_value = return_value;
600}
601
602
603void
604ast_selection_statement::print(void) const
605{
606 printf("if ( ");
607 condition->print();
608 printf(") ");
609
610 then_statement->print();
611
612 if (else_statement) {
613 printf("else ");
614 else_statement->print();
615 }
616
617}
618
619
620ast_selection_statement::ast_selection_statement(ast_expression *condition,
621 ast_node *then_statement,
622 ast_node *else_statement)
623{
624 this->condition = condition;
625 this->then_statement = then_statement;
626 this->else_statement = else_statement;
627}
628
629
630void
631ast_iteration_statement::print(void) const
632{
633 switch (mode) {
634 case ast_for:
635 printf("for( ");
636 if (init_statement)
637 init_statement->print();
638 printf("; ");
639
640 if (condition)
641 condition->print();
642 printf("; ");
643
644 if (rest_expression)
645 rest_expression->print();
646 printf(") ");
647
648 body->print();
649 break;
650
651 case ast_while:
652 printf("while ( ");
653 if (condition)
654 condition->print();
655 printf(") ");
656 body->print();
657 break;
658
659 case ast_do_while:
660 printf("do ");
661 body->print();
662 printf("while ( ");
663 if (condition)
664 condition->print();
665 printf("); ");
666 break;
667 }
668}
669
670
671ast_iteration_statement::ast_iteration_statement(int mode,
672 ast_node *init,
673 ast_node *condition,
674 ast_expression *rest_expression,
675 ast_node *body)
676{
677 this->mode = ast_iteration_modes(mode);
678 this->init_statement = init;
679 this->condition = condition;
680 this->rest_expression = rest_expression;
681 this->body = body;
682}
683
684
685void
686ast_struct_specifier::print(void) const
687{
Ian Romanicka87ac252010-02-22 13:19:34 -0800688 printf("struct %s { ", name);
Ian Romanick304ea902010-05-10 11:17:53 -0700689 foreach_list_const(n, &this->declarations) {
690 ast_node *ast = exec_node_data(ast_node, n, link);
691 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800692 }
693 printf("} ");
694}
695
696
697ast_struct_specifier::ast_struct_specifier(char *identifier,
698 ast_node *declarator_list)
699{
700 name = identifier;
Ian Romanick304ea902010-05-10 11:17:53 -0700701 this->declarations.push_degenerate_list_at_head(&declarator_list->link);
Ian Romanicka87ac252010-02-22 13:19:34 -0800702}