Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1 | /* |
| 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 Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 25 | #include <string.h> |
| 26 | #include <assert.h> |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 27 | |
| 28 | #include "ast.h" |
| 29 | #include "glsl_parser_extras.h" |
Ian Romanick | d59673c | 2010-02-25 17:17:23 -0800 | [diff] [blame] | 30 | #include "glsl_parser.h" |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 31 | |
Ian Romanick | 5bfe30a | 2010-04-07 16:44:30 -0700 | [diff] [blame] | 32 | const char * |
| 33 | _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target) |
| 34 | { |
| 35 | switch (target) { |
| 36 | case vertex_shader: return "vertex"; |
| 37 | case fragment_shader: return "fragment"; |
| 38 | case geometry_shader: return "geometry"; |
Eric Anholt | 81f49a7 | 2010-04-29 17:57:28 -0700 | [diff] [blame] | 39 | case ir_shader: break; |
Ian Romanick | 5bfe30a | 2010-04-07 16:44:30 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | assert(!"Should not get here."); |
| 43 | } |
| 44 | |
| 45 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 46 | void |
Ian Romanick | 1f58518 | 2010-03-11 14:08:33 -0800 | [diff] [blame] | 47 | _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state, |
| 48 | const char *fmt, ...) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 49 | { |
| 50 | char buf[1024]; |
| 51 | int len; |
| 52 | va_list ap; |
| 53 | |
Ian Romanick | 71d0bbf | 2010-03-23 13:21:19 -0700 | [diff] [blame] | 54 | state->error = true; |
Ian Romanick | 1f58518 | 2010-03-11 14:08:33 -0800 | [diff] [blame] | 55 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 56 | len = snprintf(buf, sizeof(buf), "%u:%u(%u): error: ", |
| 57 | locp->source, locp->first_line, locp->first_column); |
| 58 | |
| 59 | va_start(ap, fmt); |
| 60 | vsnprintf(buf + len, sizeof(buf) - len, fmt, ap); |
| 61 | va_end(ap); |
| 62 | |
| 63 | printf("%s\n", buf); |
Eric Anholt | 3623df6 | 2010-04-29 18:00:33 -0700 | [diff] [blame] | 64 | |
| 65 | if (state->info_log) |
| 66 | free(state->info_log); |
| 67 | state->info_log = strdup(buf); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | |
Ian Romanick | 56b8b21 | 2010-04-07 14:47:46 -0700 | [diff] [blame] | 71 | void |
Eric Anholt | 3623df6 | 2010-04-29 18:00:33 -0700 | [diff] [blame] | 72 | _mesa_glsl_warning(const YYLTYPE *locp, _mesa_glsl_parse_state *state, |
Ian Romanick | 56b8b21 | 2010-04-07 14:47:46 -0700 | [diff] [blame] | 73 | const char *fmt, ...) |
| 74 | { |
| 75 | char buf[1024]; |
| 76 | int len; |
| 77 | va_list ap; |
| 78 | |
| 79 | len = snprintf(buf, sizeof(buf), "%u:%u(%u): warning: ", |
| 80 | locp->source, locp->first_line, locp->first_column); |
| 81 | |
| 82 | va_start(ap, fmt); |
| 83 | vsnprintf(buf + len, sizeof(buf) - len, fmt, ap); |
| 84 | va_end(ap); |
| 85 | |
| 86 | printf("%s\n", buf); |
Eric Anholt | 3623df6 | 2010-04-29 18:00:33 -0700 | [diff] [blame] | 87 | |
| 88 | if (!state->info_log) { |
| 89 | state->info_log = strdup(buf); |
| 90 | } |
Ian Romanick | 56b8b21 | 2010-04-07 14:47:46 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 94 | bool |
| 95 | _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp, |
| 96 | const char *behavior, YYLTYPE *behavior_locp, |
| 97 | _mesa_glsl_parse_state *state) |
| 98 | { |
| 99 | enum { |
| 100 | extension_disable, |
| 101 | extension_enable, |
| 102 | extension_require, |
| 103 | extension_warn |
| 104 | } ext_mode; |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 105 | |
| 106 | if (strcmp(behavior, "warn") == 0) { |
| 107 | ext_mode = extension_warn; |
| 108 | } else if (strcmp(behavior, "require") == 0) { |
| 109 | ext_mode = extension_require; |
| 110 | } else if (strcmp(behavior, "enable") == 0) { |
| 111 | ext_mode = extension_enable; |
| 112 | } else if (strcmp(behavior, "disable") == 0) { |
| 113 | ext_mode = extension_disable; |
| 114 | } else { |
| 115 | _mesa_glsl_error(behavior_locp, state, |
| 116 | "Unknown extension behavior `%s'", |
| 117 | behavior); |
| 118 | return false; |
| 119 | } |
| 120 | |
Ian Romanick | 887a8b0 | 2010-04-07 16:57:56 -0700 | [diff] [blame] | 121 | bool unsupported = false; |
| 122 | |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 123 | if (strcmp(name, "all") == 0) { |
| 124 | if ((ext_mode == extension_enable) || (ext_mode == extension_require)) { |
| 125 | _mesa_glsl_error(name_locp, state, "Cannot %s all extensions", |
| 126 | (ext_mode == extension_enable) |
| 127 | ? "enable" : "require"); |
| 128 | return false; |
| 129 | } |
Ian Romanick | eb56cea | 2010-04-23 13:32:23 -0700 | [diff] [blame] | 130 | } else if (strcmp(name, "GL_ARB_draw_buffers") == 0) { |
Ian Romanick | c77b257 | 2010-04-07 16:59:46 -0700 | [diff] [blame] | 131 | /* This extension is only supported in fragment shaders. |
| 132 | */ |
| 133 | if (state->target != fragment_shader) { |
| 134 | unsupported = true; |
| 135 | } else { |
| 136 | state->ARB_draw_buffers_enable = (ext_mode != extension_disable); |
| 137 | state->ARB_draw_buffers_warn = (ext_mode == extension_warn); |
| 138 | } |
Ian Romanick | eb56cea | 2010-04-23 13:32:23 -0700 | [diff] [blame] | 139 | } else if (strcmp(name, "GL_ARB_texture_rectangle") == 0) { |
Ian Romanick | 0c82465 | 2010-04-07 17:13:44 -0700 | [diff] [blame] | 140 | state->ARB_texture_rectangle_enable = (ext_mode != extension_disable); |
| 141 | state->ARB_texture_rectangle_warn = (ext_mode == extension_warn); |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 142 | } else { |
Ian Romanick | 887a8b0 | 2010-04-07 16:57:56 -0700 | [diff] [blame] | 143 | unsupported = true; |
| 144 | } |
| 145 | |
| 146 | if (unsupported) { |
| 147 | static const char *const fmt = "extension `%s' unsupported in %s shader"; |
| 148 | |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 149 | if (ext_mode == extension_require) { |
Ian Romanick | 887a8b0 | 2010-04-07 16:57:56 -0700 | [diff] [blame] | 150 | _mesa_glsl_error(name_locp, state, fmt, |
| 151 | name, _mesa_glsl_shader_target_name(state->target)); |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 152 | return false; |
Ian Romanick | 1799a0c | 2010-04-07 14:50:36 -0700 | [diff] [blame] | 153 | } else { |
Ian Romanick | 887a8b0 | 2010-04-07 16:57:56 -0700 | [diff] [blame] | 154 | _mesa_glsl_warning(name_locp, state, fmt, |
| 155 | name, _mesa_glsl_shader_target_name(state->target)); |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 163 | ast_node::~ast_node() |
| 164 | { |
| 165 | /* empty */ |
| 166 | } |
| 167 | |
| 168 | |
| 169 | void |
| 170 | _mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q) |
| 171 | { |
| 172 | if (q->constant) |
| 173 | printf("const "); |
| 174 | |
| 175 | if (q->invariant) |
| 176 | printf("invariant "); |
| 177 | |
| 178 | if (q->attribute) |
| 179 | printf("attribute "); |
| 180 | |
| 181 | if (q->varying) |
| 182 | printf("varying "); |
| 183 | |
| 184 | if (q->in && q->out) |
| 185 | printf("inout "); |
| 186 | else { |
| 187 | if (q->in) |
| 188 | printf("in "); |
| 189 | |
| 190 | if (q->out) |
| 191 | printf("out "); |
| 192 | } |
| 193 | |
| 194 | if (q->centroid) |
| 195 | printf("centroid "); |
| 196 | if (q->uniform) |
| 197 | printf("uniform "); |
| 198 | if (q->smooth) |
| 199 | printf("smooth "); |
| 200 | if (q->flat) |
| 201 | printf("flat "); |
| 202 | if (q->noperspective) |
| 203 | printf("noperspective "); |
| 204 | } |
| 205 | |
| 206 | |
| 207 | void |
| 208 | ast_node::print(void) const |
| 209 | { |
Ian Romanick | 03d3f3a | 2010-04-02 11:03:47 -0700 | [diff] [blame] | 210 | printf("unhandled node "); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | |
| 214 | ast_node::ast_node(void) |
| 215 | { |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 216 | /* empty */ |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 219 | |
| 220 | static void |
| 221 | ast_opt_array_size_print(bool is_array, const ast_expression *array_size) |
| 222 | { |
| 223 | if (is_array) { |
| 224 | printf("[ "); |
| 225 | |
| 226 | if (array_size) |
| 227 | array_size->print(); |
| 228 | |
| 229 | printf("] "); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 234 | void |
| 235 | ast_compound_statement::print(void) const |
| 236 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 237 | printf("{\n"); |
| 238 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 239 | foreach_list_const(n, &this->statements) { |
| 240 | ast_node *ast = exec_node_data(ast_node, n, link); |
| 241 | ast->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | printf("}\n"); |
| 245 | } |
| 246 | |
| 247 | |
| 248 | ast_compound_statement::ast_compound_statement(int new_scope, |
| 249 | ast_node *statements) |
| 250 | { |
| 251 | this->new_scope = new_scope; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 252 | |
| 253 | if (statements != NULL) { |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 254 | this->statements.push_degenerate_list_at_head(&statements->link); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | |
| 259 | void |
| 260 | ast_expression::print(void) const |
| 261 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 262 | switch (oper) { |
| 263 | case ast_assign: |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 264 | case ast_mul_assign: |
| 265 | case ast_div_assign: |
| 266 | case ast_mod_assign: |
| 267 | case ast_add_assign: |
| 268 | case ast_sub_assign: |
| 269 | case ast_ls_assign: |
| 270 | case ast_rs_assign: |
| 271 | case ast_and_assign: |
| 272 | case ast_xor_assign: |
| 273 | case ast_or_assign: |
| 274 | subexpressions[0]->print(); |
Ian Romanick | 88349b2 | 2010-02-22 19:10:25 -0800 | [diff] [blame] | 275 | printf("%s ", operator_string(oper)); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 276 | subexpressions[1]->print(); |
| 277 | break; |
| 278 | |
| 279 | case ast_field_selection: |
| 280 | subexpressions[0]->print(); |
| 281 | printf(". %s ", primary_expression.identifier); |
| 282 | break; |
| 283 | |
| 284 | case ast_plus: |
| 285 | case ast_neg: |
| 286 | case ast_bit_not: |
| 287 | case ast_logic_not: |
| 288 | case ast_pre_inc: |
| 289 | case ast_pre_dec: |
Ian Romanick | 88349b2 | 2010-02-22 19:10:25 -0800 | [diff] [blame] | 290 | printf("%s ", operator_string(oper)); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 291 | subexpressions[0]->print(); |
| 292 | break; |
| 293 | |
| 294 | case ast_post_inc: |
| 295 | case ast_post_dec: |
| 296 | subexpressions[0]->print(); |
Ian Romanick | 88349b2 | 2010-02-22 19:10:25 -0800 | [diff] [blame] | 297 | printf("%s ", operator_string(oper)); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 298 | break; |
| 299 | |
| 300 | case ast_conditional: |
| 301 | subexpressions[0]->print(); |
| 302 | printf("? "); |
| 303 | subexpressions[1]->print(); |
| 304 | printf(": "); |
| 305 | subexpressions[1]->print(); |
| 306 | break; |
| 307 | |
| 308 | case ast_array_index: |
| 309 | subexpressions[0]->print(); |
| 310 | printf("[ "); |
| 311 | subexpressions[1]->print(); |
| 312 | printf("] "); |
| 313 | break; |
| 314 | |
| 315 | case ast_function_call: { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 316 | subexpressions[0]->print(); |
| 317 | printf("( "); |
| 318 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 319 | foreach_list_const (n, &this->expressions) { |
Ian Romanick | 2384937 | 2010-05-14 16:06:41 -0700 | [diff] [blame] | 320 | if (n != this->expressions.get_head()) |
| 321 | printf(", "); |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 322 | |
| 323 | ast_node *ast = exec_node_data(ast_node, n, link); |
| 324 | ast->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | printf(") "); |
| 328 | break; |
| 329 | } |
| 330 | |
| 331 | case ast_identifier: |
| 332 | printf("%s ", primary_expression.identifier); |
| 333 | break; |
| 334 | |
| 335 | case ast_int_constant: |
| 336 | printf("%d ", primary_expression.int_constant); |
| 337 | break; |
| 338 | |
| 339 | case ast_uint_constant: |
| 340 | printf("%u ", primary_expression.uint_constant); |
| 341 | break; |
| 342 | |
| 343 | case ast_float_constant: |
| 344 | printf("%f ", primary_expression.float_constant); |
| 345 | break; |
| 346 | |
| 347 | case ast_bool_constant: |
| 348 | printf("%s ", |
| 349 | primary_expression.bool_constant |
| 350 | ? "true" : "false"); |
| 351 | break; |
| 352 | |
| 353 | case ast_sequence: { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 354 | printf("( "); |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 355 | foreach_list_const(n, & this->expressions) { |
| 356 | if (n != this->expressions.get_head()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 357 | printf(", "); |
| 358 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 359 | ast_node *ast = exec_node_data(ast_node, n, link); |
| 360 | ast->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 361 | } |
| 362 | printf(") "); |
| 363 | break; |
| 364 | } |
Ian Romanick | 88349b2 | 2010-02-22 19:10:25 -0800 | [diff] [blame] | 365 | |
| 366 | default: |
| 367 | assert(0); |
| 368 | break; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
| 372 | ast_expression::ast_expression(int oper, |
| 373 | ast_expression *ex0, |
| 374 | ast_expression *ex1, |
| 375 | ast_expression *ex2) |
| 376 | { |
| 377 | this->oper = ast_operators(oper); |
| 378 | this->subexpressions[0] = ex0; |
| 379 | this->subexpressions[1] = ex1; |
| 380 | this->subexpressions[2] = ex2; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | |
| 384 | void |
| 385 | ast_expression_statement::print(void) const |
| 386 | { |
| 387 | if (expression) |
| 388 | expression->print(); |
| 389 | |
| 390 | printf("; "); |
| 391 | } |
| 392 | |
| 393 | |
| 394 | ast_expression_statement::ast_expression_statement(ast_expression *ex) : |
| 395 | expression(ex) |
| 396 | { |
| 397 | /* empty */ |
| 398 | } |
| 399 | |
| 400 | |
| 401 | void |
| 402 | ast_function::print(void) const |
| 403 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 404 | return_type->print(); |
| 405 | printf(" %s (", identifier); |
| 406 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 407 | foreach_list_const(n, & this->parameters) { |
| 408 | ast_node *ast = exec_node_data(ast_node, n, link); |
| 409 | ast->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | printf(")"); |
| 413 | } |
| 414 | |
| 415 | |
| 416 | ast_function::ast_function(void) |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 417 | : is_definition(false), signature(NULL) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 418 | { |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 419 | /* empty */ |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | |
| 423 | void |
| 424 | ast_fully_specified_type::print(void) const |
| 425 | { |
| 426 | _mesa_ast_type_qualifier_print(& qualifier); |
| 427 | specifier->print(); |
| 428 | } |
| 429 | |
| 430 | |
| 431 | void |
| 432 | ast_parameter_declarator::print(void) const |
| 433 | { |
| 434 | type->print(); |
| 435 | if (identifier) |
| 436 | printf("%s ", identifier); |
| 437 | ast_opt_array_size_print(is_array, array_size); |
| 438 | } |
| 439 | |
| 440 | |
| 441 | void |
| 442 | ast_function_definition::print(void) const |
| 443 | { |
| 444 | prototype->print(); |
| 445 | body->print(); |
| 446 | } |
| 447 | |
| 448 | |
| 449 | void |
| 450 | ast_declaration::print(void) const |
| 451 | { |
| 452 | printf("%s ", identifier); |
| 453 | ast_opt_array_size_print(is_array, array_size); |
| 454 | |
| 455 | if (initializer) { |
| 456 | printf("= "); |
| 457 | initializer->print(); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | |
| 462 | ast_declaration::ast_declaration(char *identifier, int is_array, |
| 463 | ast_expression *array_size, |
| 464 | ast_expression *initializer) |
| 465 | { |
| 466 | this->identifier = identifier; |
| 467 | this->is_array = is_array; |
| 468 | this->array_size = array_size; |
| 469 | this->initializer = initializer; |
| 470 | } |
| 471 | |
| 472 | |
| 473 | void |
| 474 | ast_declarator_list::print(void) const |
| 475 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 476 | assert(type || invariant); |
| 477 | |
| 478 | if (type) |
| 479 | type->print(); |
| 480 | else |
| 481 | printf("invariant "); |
| 482 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 483 | foreach_list_const (ptr, & this->declarations) { |
| 484 | if (ptr != this->declarations.get_head()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 485 | printf(", "); |
| 486 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 487 | ast_node *ast = exec_node_data(ast_node, ptr, link); |
| 488 | ast->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | printf("; "); |
| 492 | } |
| 493 | |
| 494 | |
| 495 | ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type) |
| 496 | { |
| 497 | this->type = type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | void |
| 501 | ast_jump_statement::print(void) const |
| 502 | { |
| 503 | switch (mode) { |
| 504 | case ast_continue: |
| 505 | printf("continue; "); |
| 506 | break; |
| 507 | case ast_break: |
| 508 | printf("break; "); |
| 509 | break; |
| 510 | case ast_return: |
| 511 | printf("return "); |
| 512 | if (opt_return_value) |
| 513 | opt_return_value->print(); |
| 514 | |
| 515 | printf("; "); |
| 516 | break; |
| 517 | case ast_discard: |
| 518 | printf("discard; "); |
| 519 | break; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | |
| 524 | ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value) |
| 525 | { |
| 526 | this->mode = ast_jump_modes(mode); |
| 527 | |
| 528 | if (mode == ast_return) |
| 529 | opt_return_value = return_value; |
| 530 | } |
| 531 | |
| 532 | |
| 533 | void |
| 534 | ast_selection_statement::print(void) const |
| 535 | { |
| 536 | printf("if ( "); |
| 537 | condition->print(); |
| 538 | printf(") "); |
| 539 | |
| 540 | then_statement->print(); |
| 541 | |
| 542 | if (else_statement) { |
| 543 | printf("else "); |
| 544 | else_statement->print(); |
| 545 | } |
| 546 | |
| 547 | } |
| 548 | |
| 549 | |
| 550 | ast_selection_statement::ast_selection_statement(ast_expression *condition, |
| 551 | ast_node *then_statement, |
| 552 | ast_node *else_statement) |
| 553 | { |
| 554 | this->condition = condition; |
| 555 | this->then_statement = then_statement; |
| 556 | this->else_statement = else_statement; |
| 557 | } |
| 558 | |
| 559 | |
| 560 | void |
| 561 | ast_iteration_statement::print(void) const |
| 562 | { |
| 563 | switch (mode) { |
| 564 | case ast_for: |
| 565 | printf("for( "); |
| 566 | if (init_statement) |
| 567 | init_statement->print(); |
| 568 | printf("; "); |
| 569 | |
| 570 | if (condition) |
| 571 | condition->print(); |
| 572 | printf("; "); |
| 573 | |
| 574 | if (rest_expression) |
| 575 | rest_expression->print(); |
| 576 | printf(") "); |
| 577 | |
| 578 | body->print(); |
| 579 | break; |
| 580 | |
| 581 | case ast_while: |
| 582 | printf("while ( "); |
| 583 | if (condition) |
| 584 | condition->print(); |
| 585 | printf(") "); |
| 586 | body->print(); |
| 587 | break; |
| 588 | |
| 589 | case ast_do_while: |
| 590 | printf("do "); |
| 591 | body->print(); |
| 592 | printf("while ( "); |
| 593 | if (condition) |
| 594 | condition->print(); |
| 595 | printf("); "); |
| 596 | break; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | |
| 601 | ast_iteration_statement::ast_iteration_statement(int mode, |
| 602 | ast_node *init, |
| 603 | ast_node *condition, |
| 604 | ast_expression *rest_expression, |
| 605 | ast_node *body) |
| 606 | { |
| 607 | this->mode = ast_iteration_modes(mode); |
| 608 | this->init_statement = init; |
| 609 | this->condition = condition; |
| 610 | this->rest_expression = rest_expression; |
| 611 | this->body = body; |
| 612 | } |
| 613 | |
| 614 | |
| 615 | void |
| 616 | ast_struct_specifier::print(void) const |
| 617 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 618 | printf("struct %s { ", name); |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 619 | foreach_list_const(n, &this->declarations) { |
| 620 | ast_node *ast = exec_node_data(ast_node, n, link); |
| 621 | ast->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 622 | } |
| 623 | printf("} "); |
| 624 | } |
| 625 | |
| 626 | |
| 627 | ast_struct_specifier::ast_struct_specifier(char *identifier, |
| 628 | ast_node *declarator_list) |
| 629 | { |
| 630 | name = identifier; |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 631 | this->declarations.push_degenerate_list_at_head(&declarator_list->link); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 632 | } |