glsl2: Use Elements from main/compiler.h instead of open-coding
[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_zero_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->identifier = NULL;
461 this->is_array = false;
462 this->array_size = 0;
463 }
464
465 virtual void print(void) const;
466
467 virtual ir_rvalue *hir(exec_list *instructions,
468 struct _mesa_glsl_parse_state *state);
469
470 ast_fully_specified_type *type;
471 char *identifier;
472 int is_array;
473 ast_expression *array_size;
474
475 static void parameters_to_hir(exec_list *ast_parameters,
476 bool formal, exec_list *ir_parameters,
477 struct _mesa_glsl_parse_state *state);
478
479 private:
480 /** Is this parameter declaration part of a formal parameter list? */
481 bool formal_parameter;
482
483 /**
484 * Is this parameter 'void' type?
485 *
486 * This field is set by \c ::hir.
487 */
488 bool is_void;
489 };
490
491
492 class ast_function : public ast_node {
493 public:
494 ast_function(void);
495
496 virtual void print(void) const;
497
498 virtual ir_rvalue *hir(exec_list *instructions,
499 struct _mesa_glsl_parse_state *state);
500
501 ast_fully_specified_type *return_type;
502 char *identifier;
503
504 exec_list parameters;
505
506 private:
507 /**
508 * Is this prototype part of the function definition?
509 *
510 * Used by ast_function_definition::hir to process the parameters, etc.
511 * of the function.
512 *
513 * \sa ::hir
514 */
515 bool is_definition;
516
517 /**
518 * Function signature corresponding to this function prototype instance
519 *
520 * Used by ast_function_definition::hir to process the parameters, etc.
521 * of the function.
522 *
523 * \sa ::hir
524 */
525 class ir_function_signature *signature;
526
527 friend class ast_function_definition;
528 };
529
530
531 class ast_declaration_statement : public ast_node {
532 public:
533 ast_declaration_statement(void);
534
535 enum {
536 ast_function,
537 ast_declaration,
538 ast_precision
539 } mode;
540
541 union {
542 class ast_function *function;
543 ast_declarator_list *declarator;
544 ast_type_specifier *type;
545 ast_node *node;
546 } declaration;
547 };
548
549
550 class ast_expression_statement : public ast_node {
551 public:
552 ast_expression_statement(ast_expression *);
553 virtual void print(void) const;
554
555 virtual ir_rvalue *hir(exec_list *instructions,
556 struct _mesa_glsl_parse_state *state);
557
558 ast_expression *expression;
559 };
560
561
562 class ast_case_label : public ast_node {
563 public:
564
565 /**
566 * An expression of NULL means 'default'.
567 */
568 ast_expression *expression;
569 };
570
571 class ast_selection_statement : public ast_node {
572 public:
573 ast_selection_statement(ast_expression *condition,
574 ast_node *then_statement,
575 ast_node *else_statement);
576 virtual void print(void) const;
577
578 virtual ir_rvalue *hir(exec_list *instructions,
579 struct _mesa_glsl_parse_state *state);
580
581 ast_expression *condition;
582 ast_node *then_statement;
583 ast_node *else_statement;
584 };
585
586
587 class ast_switch_statement : public ast_node {
588 public:
589 ast_expression *expression;
590 exec_list statements;
591 };
592
593 class ast_iteration_statement : public ast_node {
594 public:
595 ast_iteration_statement(int mode, ast_node *init, ast_node *condition,
596 ast_expression *rest_expression, ast_node *body);
597
598 virtual void print(void) const;
599
600 virtual ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
601
602 enum ast_iteration_modes {
603 ast_for,
604 ast_while,
605 ast_do_while
606 } mode;
607
608
609 ast_node *init_statement;
610 ast_node *condition;
611 ast_expression *rest_expression;
612
613 ast_node *body;
614
615 private:
616 /**
617 * Generate IR from the condition of a loop
618 *
619 * This is factored out of ::hir because some loops have the condition
620 * test at the top (for and while), and others have it at the end (do-while).
621 */
622 void condition_to_hir(class ir_loop *, struct _mesa_glsl_parse_state *);
623 };
624
625
626 class ast_jump_statement : public ast_node {
627 public:
628 ast_jump_statement(int mode, ast_expression *return_value);
629 virtual void print(void) const;
630
631 virtual ir_rvalue *hir(exec_list *instructions,
632 struct _mesa_glsl_parse_state *state);
633
634 enum ast_jump_modes {
635 ast_continue,
636 ast_break,
637 ast_return,
638 ast_discard
639 } mode;
640
641 ast_expression *opt_return_value;
642 };
643
644
645 class ast_function_definition : public ast_node {
646 public:
647 virtual void print(void) const;
648
649 virtual ir_rvalue *hir(exec_list *instructions,
650 struct _mesa_glsl_parse_state *state);
651
652 ast_function *prototype;
653 ast_compound_statement *body;
654 };
655
656
657 extern void
658 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state);
659
660 extern struct ir_rvalue *
661 _mesa_ast_field_selection_to_hir(const struct ast_expression *expr,
662 exec_list *instructions,
663 struct _mesa_glsl_parse_state *state);
664
665 #endif /* AST_H */