glsl2: initialize is_array and array_size of ast_parameter_declarator
[mesa.git] / src / glsl / ast.h
1 /* -*- c++ -*- */
2 /*
3 * Copyright © 2009 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #pragma once
26 #ifndef AST_H
27 #define AST_H
28
29 #include "list.h"
30 #include "glsl_parser_extras.h"
31
32 struct ir_instruction;
33 struct _mesa_glsl_parse_state;
34
35 struct YYLTYPE;
36
37 class ast_node {
38 public:
39 /* Callers of this talloc-based new need not call delete. It's
40 * easier to just talloc_free 'ctx' (or any of its ancestors). */
41 static void* operator new(size_t size, void *ctx)
42 {
43 void *node;
44
45 node = talloc_size(ctx, size);
46 assert(node != NULL);
47
48 return node;
49 }
50
51 /* If the user *does* call delete, that's OK, we will just
52 * talloc_free in that case. */
53 static void operator delete(void *table)
54 {
55 talloc_free(table);
56 }
57
58 virtual void print(void) const;
59 virtual ir_rvalue *hir(exec_list *instructions,
60 struct _mesa_glsl_parse_state *state);
61
62 /**
63 * Retrieve the source location of an AST node
64 *
65 * This function is primarily used to get the source position of an AST node
66 * into a form that can be passed to \c _mesa_glsl_error.
67 *
68 * \sa _mesa_glsl_error, ast_node::set_location
69 */
70 struct YYLTYPE get_location(void) const
71 {
72 struct YYLTYPE locp;
73
74 locp.source = this->location.source;
75 locp.first_line = this->location.line;
76 locp.first_column = this->location.column;
77 locp.last_line = locp.first_line;
78 locp.last_column = locp.first_column;
79
80 return locp;
81 }
82
83 /**
84 * Set the source location of an AST node from a parser location
85 *
86 * \sa ast_node::get_location
87 */
88 void set_location(const struct YYLTYPE &locp)
89 {
90 this->location.source = locp.source;
91 this->location.line = locp.first_line;
92 this->location.column = locp.first_column;
93 }
94
95 struct {
96 unsigned source;
97 unsigned line;
98 unsigned column;
99 } location;
100
101 exec_node link;
102
103 protected:
104 ast_node(void);
105 };
106
107
108 enum ast_operators {
109 ast_assign,
110 ast_plus, /**< Unary + operator. */
111 ast_neg,
112 ast_add,
113 ast_sub,
114 ast_mul,
115 ast_div,
116 ast_mod,
117 ast_lshift,
118 ast_rshift,
119 ast_less,
120 ast_greater,
121 ast_lequal,
122 ast_gequal,
123 ast_equal,
124 ast_nequal,
125 ast_bit_and,
126 ast_bit_xor,
127 ast_bit_or,
128 ast_bit_not,
129 ast_logic_and,
130 ast_logic_xor,
131 ast_logic_or,
132 ast_logic_not,
133
134 ast_mul_assign,
135 ast_div_assign,
136 ast_mod_assign,
137 ast_add_assign,
138 ast_sub_assign,
139 ast_ls_assign,
140 ast_rs_assign,
141 ast_and_assign,
142 ast_xor_assign,
143 ast_or_assign,
144
145 ast_conditional,
146
147 ast_pre_inc,
148 ast_pre_dec,
149 ast_post_inc,
150 ast_post_dec,
151 ast_field_selection,
152 ast_array_index,
153
154 ast_function_call,
155
156 ast_identifier,
157 ast_int_constant,
158 ast_uint_constant,
159 ast_float_constant,
160 ast_bool_constant,
161
162 ast_sequence
163 };
164
165 class ast_expression : public ast_node {
166 public:
167 ast_expression(int oper, ast_expression *,
168 ast_expression *, ast_expression *);
169
170 ast_expression(const char *identifier) :
171 oper(ast_identifier)
172 {
173 subexpressions[0] = NULL;
174 subexpressions[1] = NULL;
175 subexpressions[2] = NULL;
176 primary_expression.identifier = (char *) identifier;
177 }
178
179 static const char *operator_string(enum ast_operators op);
180
181 virtual ir_rvalue *hir(exec_list *instructions,
182 struct _mesa_glsl_parse_state *state);
183
184 virtual void print(void) const;
185
186 enum ast_operators oper;
187
188 ast_expression *subexpressions[3];
189
190 union {
191 char *identifier;
192 int int_constant;
193 float float_constant;
194 unsigned uint_constant;
195 int bool_constant;
196 } primary_expression;
197
198
199 /**
200 * List of expressions for an \c ast_sequence or parameters for an
201 * \c ast_function_call
202 */
203 exec_list expressions;
204 };
205
206 class ast_expression_bin : public ast_expression {
207 public:
208 ast_expression_bin(int oper, ast_expression *, ast_expression *);
209
210 virtual void print(void) const;
211 };
212
213 /**
214 * Subclass of expressions for function calls
215 */
216 class ast_function_expression : public ast_expression {
217 public:
218 ast_function_expression(ast_expression *callee)
219 : ast_expression(ast_function_call, callee,
220 NULL, NULL),
221 cons(false)
222 {
223 /* empty */
224 }
225
226 ast_function_expression(class ast_type_specifier *type)
227 : ast_expression(ast_function_call, (ast_expression *) type,
228 NULL, NULL),
229 cons(true)
230 {
231 /* empty */
232 }
233
234 bool is_constructor() const
235 {
236 return cons;
237 }
238
239 virtual ir_rvalue *hir(exec_list *instructions,
240 struct _mesa_glsl_parse_state *state);
241
242 private:
243 /**
244 * Is this function call actually a constructor?
245 */
246 bool cons;
247 };
248
249
250 /**
251 * Number of possible operators for an ast_expression
252 *
253 * This is done as a define instead of as an additional value in the enum so
254 * that the compiler won't generate spurious messages like "warning:
255 * enumeration value ‘ast_num_operators’ not handled in switch"
256 */
257 #define AST_NUM_OPERATORS (ast_sequence + 1)
258
259
260 class ast_compound_statement : public ast_node {
261 public:
262 ast_compound_statement(int new_scope, ast_node *statements);
263 virtual void print(void) const;
264
265 virtual ir_rvalue *hir(exec_list *instructions,
266 struct _mesa_glsl_parse_state *state);
267
268 int new_scope;
269 exec_list statements;
270 };
271
272 class ast_declaration : public ast_node {
273 public:
274 ast_declaration(char *identifier, int is_array, ast_expression *array_size,
275 ast_expression *initializer);
276 virtual void print(void) const;
277
278 char *identifier;
279
280 int is_array;
281 ast_expression *array_size;
282
283 ast_expression *initializer;
284 };
285
286
287 enum {
288 ast_precision_high = 0, /**< Default precision. */
289 ast_precision_medium,
290 ast_precision_low
291 };
292
293 struct ast_type_qualifier {
294 unsigned invariant:1;
295 unsigned constant:1;
296 unsigned attribute:1;
297 unsigned varying:1;
298 unsigned in:1;
299 unsigned out:1;
300 unsigned centroid:1;
301 unsigned uniform:1;
302 unsigned smooth:1;
303 unsigned flat:1;
304 unsigned noperspective:1;
305
306 /** \name Layout qualifiers for GL_ARB_fragment_coord_conventions */
307 /*@{*/
308 unsigned origin_upper_left:1;
309 unsigned pixel_center_integer:1;
310 /*@}*/
311 };
312
313 class ast_struct_specifier : public ast_node {
314 public:
315 ast_struct_specifier(char *identifier, ast_node *declarator_list);
316 virtual void print(void) const;
317
318 virtual ir_rvalue *hir(exec_list *instructions,
319 struct _mesa_glsl_parse_state *state);
320
321 char *name;
322 exec_list declarations;
323 };
324
325
326 enum ast_types {
327 ast_void,
328 ast_float,
329 ast_int,
330 ast_uint,
331 ast_bool,
332 ast_vec2,
333 ast_vec3,
334 ast_vec4,
335 ast_bvec2,
336 ast_bvec3,
337 ast_bvec4,
338 ast_ivec2,
339 ast_ivec3,
340 ast_ivec4,
341 ast_uvec2,
342 ast_uvec3,
343 ast_uvec4,
344 ast_mat2,
345 ast_mat2x3,
346 ast_mat2x4,
347 ast_mat3x2,
348 ast_mat3,
349 ast_mat3x4,
350 ast_mat4x2,
351 ast_mat4x3,
352 ast_mat4,
353 ast_sampler1d,
354 ast_sampler2d,
355 ast_sampler2drect,
356 ast_sampler3d,
357 ast_samplercube,
358 ast_sampler1dshadow,
359 ast_sampler2dshadow,
360 ast_sampler2drectshadow,
361 ast_samplercubeshadow,
362 ast_sampler1darray,
363 ast_sampler2darray,
364 ast_sampler1darrayshadow,
365 ast_sampler2darrayshadow,
366 ast_isampler1d,
367 ast_isampler2d,
368 ast_isampler3d,
369 ast_isamplercube,
370 ast_isampler1darray,
371 ast_isampler2darray,
372 ast_usampler1d,
373 ast_usampler2d,
374 ast_usampler3d,
375 ast_usamplercube,
376 ast_usampler1darray,
377 ast_usampler2darray,
378
379 ast_struct,
380 ast_type_name
381 };
382
383
384 class ast_type_specifier : public ast_node {
385 public:
386 ast_type_specifier(int specifier);
387
388 /** Construct a type specifier from a type name */
389 ast_type_specifier(const char *name)
390 : type_specifier(ast_type_name), type_name(name), structure(NULL),
391 is_array(false), array_size(NULL), precision(ast_precision_high)
392 {
393 /* empty */
394 }
395
396 /** Construct a type specifier from a structure definition */
397 ast_type_specifier(ast_struct_specifier *s)
398 : type_specifier(ast_struct), type_name(s->name), structure(s),
399 is_array(false), array_size(NULL), precision(ast_precision_high)
400 {
401 /* empty */
402 }
403
404 const struct glsl_type *glsl_type(const char **name,
405 struct _mesa_glsl_parse_state *state)
406 const;
407
408 virtual void print(void) const;
409
410 ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
411
412 enum ast_types type_specifier;
413
414 const char *type_name;
415 ast_struct_specifier *structure;
416
417 int is_array;
418 ast_expression *array_size;
419
420 unsigned precision:2;
421 };
422
423
424 class ast_fully_specified_type : public ast_node {
425 public:
426 virtual void print(void) const;
427 bool has_qualifiers() const;
428
429 ast_type_qualifier qualifier;
430 ast_type_specifier *specifier;
431 };
432
433
434 class ast_declarator_list : public ast_node {
435 public:
436 ast_declarator_list(ast_fully_specified_type *);
437 virtual void print(void) const;
438
439 virtual ir_rvalue *hir(exec_list *instructions,
440 struct _mesa_glsl_parse_state *state);
441
442 ast_fully_specified_type *type;
443 exec_list declarations;
444
445 /**
446 * Special flag for vertex shader "invariant" declarations.
447 *
448 * Vertex shaders can contain "invariant" variable redeclarations that do
449 * not include a type. For example, "invariant gl_Position;". This flag
450 * is used to note these cases when no type is specified.
451 */
452 int invariant;
453 };
454
455
456 class ast_parameter_declarator : public ast_node {
457 public:
458 ast_parameter_declarator()
459 {
460 this->is_array = false;
461 this->array_size = 0;
462 }
463
464 virtual void print(void) const;
465
466 virtual ir_rvalue *hir(exec_list *instructions,
467 struct _mesa_glsl_parse_state *state);
468
469 ast_fully_specified_type *type;
470 char *identifier;
471 int is_array;
472 ast_expression *array_size;
473
474 static void parameters_to_hir(exec_list *ast_parameters,
475 bool formal, exec_list *ir_parameters,
476 struct _mesa_glsl_parse_state *state);
477
478 private:
479 /** Is this parameter declaration part of a formal parameter list? */
480 bool formal_parameter;
481
482 /**
483 * Is this parameter 'void' type?
484 *
485 * This field is set by \c ::hir.
486 */
487 bool is_void;
488 };
489
490
491 class ast_function : public ast_node {
492 public:
493 ast_function(void);
494
495 virtual void print(void) const;
496
497 virtual ir_rvalue *hir(exec_list *instructions,
498 struct _mesa_glsl_parse_state *state);
499
500 ast_fully_specified_type *return_type;
501 char *identifier;
502
503 exec_list parameters;
504
505 private:
506 /**
507 * Is this prototype part of the function definition?
508 *
509 * Used by ast_function_definition::hir to process the parameters, etc.
510 * of the function.
511 *
512 * \sa ::hir
513 */
514 bool is_definition;
515
516 /**
517 * Function signature corresponding to this function prototype instance
518 *
519 * Used by ast_function_definition::hir to process the parameters, etc.
520 * of the function.
521 *
522 * \sa ::hir
523 */
524 class ir_function_signature *signature;
525
526 friend class ast_function_definition;
527 };
528
529
530 class ast_declaration_statement : public ast_node {
531 public:
532 ast_declaration_statement(void);
533
534 enum {
535 ast_function,
536 ast_declaration,
537 ast_precision
538 } mode;
539
540 union {
541 class ast_function *function;
542 ast_declarator_list *declarator;
543 ast_type_specifier *type;
544 ast_node *node;
545 } declaration;
546 };
547
548
549 class ast_expression_statement : public ast_node {
550 public:
551 ast_expression_statement(ast_expression *);
552 virtual void print(void) const;
553
554 virtual ir_rvalue *hir(exec_list *instructions,
555 struct _mesa_glsl_parse_state *state);
556
557 ast_expression *expression;
558 };
559
560
561 class ast_case_label : public ast_node {
562 public:
563
564 /**
565 * An expression of NULL means 'default'.
566 */
567 ast_expression *expression;
568 };
569
570 class ast_selection_statement : public ast_node {
571 public:
572 ast_selection_statement(ast_expression *condition,
573 ast_node *then_statement,
574 ast_node *else_statement);
575 virtual void print(void) const;
576
577 virtual ir_rvalue *hir(exec_list *instructions,
578 struct _mesa_glsl_parse_state *state);
579
580 ast_expression *condition;
581 ast_node *then_statement;
582 ast_node *else_statement;
583 };
584
585
586 class ast_switch_statement : public ast_node {
587 public:
588 ast_expression *expression;
589 exec_list statements;
590 };
591
592 class ast_iteration_statement : public ast_node {
593 public:
594 ast_iteration_statement(int mode, ast_node *init, ast_node *condition,
595 ast_expression *rest_expression, ast_node *body);
596
597 virtual void print(void) const;
598
599 virtual ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
600
601 enum ast_iteration_modes {
602 ast_for,
603 ast_while,
604 ast_do_while
605 } mode;
606
607
608 ast_node *init_statement;
609 ast_node *condition;
610 ast_expression *rest_expression;
611
612 ast_node *body;
613
614 private:
615 /**
616 * Generate IR from the condition of a loop
617 *
618 * This is factored out of ::hir because some loops have the condition
619 * test at the top (for and while), and others have it at the end (do-while).
620 */
621 void condition_to_hir(class ir_loop *, struct _mesa_glsl_parse_state *);
622 };
623
624
625 class ast_jump_statement : public ast_node {
626 public:
627 ast_jump_statement(int mode, ast_expression *return_value);
628 virtual void print(void) const;
629
630 virtual ir_rvalue *hir(exec_list *instructions,
631 struct _mesa_glsl_parse_state *state);
632
633 enum ast_jump_modes {
634 ast_continue,
635 ast_break,
636 ast_return,
637 ast_discard
638 } mode;
639
640 ast_expression *opt_return_value;
641 };
642
643
644 class ast_function_definition : public ast_node {
645 public:
646 virtual void print(void) const;
647
648 virtual ir_rvalue *hir(exec_list *instructions,
649 struct _mesa_glsl_parse_state *state);
650
651 ast_function *prototype;
652 ast_compound_statement *body;
653 };
654
655
656 extern void
657 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state);
658
659 extern struct ir_rvalue *
660 _mesa_ast_field_selection_to_hir(const struct ast_expression *expr,
661 exec_list *instructions,
662 struct _mesa_glsl_parse_state *state);
663
664 #endif /* AST_H */