grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / libs / mesa / src / glsl / ast.h
blob878f48b20706613373c7959c998c499559ecf616
1 /* -*- c++ -*- */
2 /*
3 * Copyright © 2009 Intel Corporation
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:
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.
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.
25 #pragma once
26 #ifndef AST_H
27 #define AST_H
29 #include "list.h"
30 #include "glsl_parser_extras.h"
32 struct _mesa_glsl_parse_state;
34 struct YYLTYPE;
36 /**
37 * \defgroup AST Abstract syntax tree node definitions
39 * An abstract syntax tree is generated by the parser. This is a fairly
40 * direct representation of the gramma derivation for the source program.
41 * No symantic checking is done during the generation of the AST. Only
42 * syntactic checking is done. Symantic checking is performed by a later
43 * stage that converts the AST to a more generic intermediate representation.
45 *@{
47 /**
48 * Base class of all abstract syntax tree nodes
50 class ast_node {
51 public:
52 /* Callers of this ralloc-based new need not call delete. It's
53 * easier to just ralloc_free 'ctx' (or any of its ancestors). */
54 static void* operator new(size_t size, void *ctx)
56 void *node;
58 node = rzalloc_size(ctx, size);
59 assert(node != NULL);
61 return node;
64 /* If the user *does* call delete, that's OK, we will just
65 * ralloc_free in that case. */
66 static void operator delete(void *table)
68 ralloc_free(table);
71 /**
72 * Print an AST node in something approximating the original GLSL code
74 virtual void print(void) const;
76 /**
77 * Convert the AST node to the high-level intermediate representation
79 virtual ir_rvalue *hir(exec_list *instructions,
80 struct _mesa_glsl_parse_state *state);
82 /**
83 * Retrieve the source location of an AST node
85 * This function is primarily used to get the source position of an AST node
86 * into a form that can be passed to \c _mesa_glsl_error.
88 * \sa _mesa_glsl_error, ast_node::set_location
90 struct YYLTYPE get_location(void) const
92 struct YYLTYPE locp;
94 locp.source = this->location.source;
95 locp.first_line = this->location.line;
96 locp.first_column = this->location.column;
97 locp.last_line = locp.first_line;
98 locp.last_column = locp.first_column;
100 return locp;
104 * Set the source location of an AST node from a parser location
106 * \sa ast_node::get_location
108 void set_location(const struct YYLTYPE &locp)
110 this->location.source = locp.source;
111 this->location.line = locp.first_line;
112 this->location.column = locp.first_column;
116 * Source location of the AST node.
118 struct {
119 unsigned source; /**< GLSL source number. */
120 unsigned line; /**< Line number within the source string. */
121 unsigned column; /**< Column in the line. */
122 } location;
124 exec_node link;
126 protected:
128 * The only constructor is protected so that only derived class objects can
129 * be created.
131 ast_node(void);
136 * Operators for AST expression nodes.
138 enum ast_operators {
139 ast_assign,
140 ast_plus, /**< Unary + operator. */
141 ast_neg,
142 ast_add,
143 ast_sub,
144 ast_mul,
145 ast_div,
146 ast_mod,
147 ast_lshift,
148 ast_rshift,
149 ast_less,
150 ast_greater,
151 ast_lequal,
152 ast_gequal,
153 ast_equal,
154 ast_nequal,
155 ast_bit_and,
156 ast_bit_xor,
157 ast_bit_or,
158 ast_bit_not,
159 ast_logic_and,
160 ast_logic_xor,
161 ast_logic_or,
162 ast_logic_not,
164 ast_mul_assign,
165 ast_div_assign,
166 ast_mod_assign,
167 ast_add_assign,
168 ast_sub_assign,
169 ast_ls_assign,
170 ast_rs_assign,
171 ast_and_assign,
172 ast_xor_assign,
173 ast_or_assign,
175 ast_conditional,
177 ast_pre_inc,
178 ast_pre_dec,
179 ast_post_inc,
180 ast_post_dec,
181 ast_field_selection,
182 ast_array_index,
184 ast_function_call,
186 ast_identifier,
187 ast_int_constant,
188 ast_uint_constant,
189 ast_float_constant,
190 ast_bool_constant,
192 ast_sequence
196 * Representation of any sort of expression.
198 class ast_expression : public ast_node {
199 public:
200 ast_expression(int oper, ast_expression *,
201 ast_expression *, ast_expression *);
203 ast_expression(const char *identifier) :
204 oper(ast_identifier)
206 subexpressions[0] = NULL;
207 subexpressions[1] = NULL;
208 subexpressions[2] = NULL;
209 primary_expression.identifier = (char *) identifier;
212 static const char *operator_string(enum ast_operators op);
214 virtual ir_rvalue *hir(exec_list *instructions,
215 struct _mesa_glsl_parse_state *state);
217 virtual void print(void) const;
219 enum ast_operators oper;
221 ast_expression *subexpressions[3];
223 union {
224 char *identifier;
225 int int_constant;
226 float float_constant;
227 unsigned uint_constant;
228 int bool_constant;
229 } primary_expression;
233 * List of expressions for an \c ast_sequence or parameters for an
234 * \c ast_function_call
236 exec_list expressions;
239 class ast_expression_bin : public ast_expression {
240 public:
241 ast_expression_bin(int oper, ast_expression *, ast_expression *);
243 virtual void print(void) const;
247 * Subclass of expressions for function calls
249 class ast_function_expression : public ast_expression {
250 public:
251 ast_function_expression(ast_expression *callee)
252 : ast_expression(ast_function_call, callee,
253 NULL, NULL),
254 cons(false)
256 /* empty */
259 ast_function_expression(class ast_type_specifier *type)
260 : ast_expression(ast_function_call, (ast_expression *) type,
261 NULL, NULL),
262 cons(true)
264 /* empty */
267 bool is_constructor() const
269 return cons;
272 virtual ir_rvalue *hir(exec_list *instructions,
273 struct _mesa_glsl_parse_state *state);
275 private:
277 * Is this function call actually a constructor?
279 bool cons;
284 * Number of possible operators for an ast_expression
286 * This is done as a define instead of as an additional value in the enum so
287 * that the compiler won't generate spurious messages like "warning:
288 * enumeration value ‘ast_num_operators’ not handled in switch"
290 #define AST_NUM_OPERATORS (ast_sequence + 1)
293 class ast_compound_statement : public ast_node {
294 public:
295 ast_compound_statement(int new_scope, ast_node *statements);
296 virtual void print(void) const;
298 virtual ir_rvalue *hir(exec_list *instructions,
299 struct _mesa_glsl_parse_state *state);
301 int new_scope;
302 exec_list statements;
305 class ast_declaration : public ast_node {
306 public:
307 ast_declaration(char *identifier, int is_array, ast_expression *array_size,
308 ast_expression *initializer);
309 virtual void print(void) const;
311 char *identifier;
313 int is_array;
314 ast_expression *array_size;
316 ast_expression *initializer;
320 enum {
321 ast_precision_none = 0, /**< Absence of precision qualifier. */
322 ast_precision_high,
323 ast_precision_medium,
324 ast_precision_low
327 struct ast_type_qualifier {
328 union {
329 struct {
330 unsigned invariant:1;
331 unsigned constant:1;
332 unsigned attribute:1;
333 unsigned varying:1;
334 unsigned in:1;
335 unsigned out:1;
336 unsigned centroid:1;
337 unsigned uniform:1;
338 unsigned smooth:1;
339 unsigned flat:1;
340 unsigned noperspective:1;
342 /** \name Layout qualifiers for GL_ARB_fragment_coord_conventions */
343 /*@{*/
344 unsigned origin_upper_left:1;
345 unsigned pixel_center_integer:1;
346 /*@}*/
349 * Flag set if GL_ARB_explicit_attrib_location "location" layout
350 * qualifier is used.
352 unsigned explicit_location:1;
354 /** \name Layout qualifiers for GL_AMD_conservative_depth */
355 /** \{ */
356 unsigned depth_any:1;
357 unsigned depth_greater:1;
358 unsigned depth_less:1;
359 unsigned depth_unchanged:1;
360 /** \} */
362 /** \brief Set of flags, accessed by name. */
365 /** \brief Set of flags, accessed as a bitmask. */
366 unsigned i;
367 } flags;
370 * Location specified via GL_ARB_explicit_attrib_location layout
372 * \note
373 * This field is only valid if \c explicit_location is set.
375 unsigned location;
378 * Return true if and only if an interpolation qualifier is present.
380 bool has_interpolation() const;
383 * \brief Return string representation of interpolation qualifier.
385 * If an interpolation qualifier is present, then return that qualifier's
386 * string representation. Otherwise, return null. For example, if the
387 * noperspective bit is set, then this returns "noperspective".
389 * If multiple interpolation qualifiers are somehow present, then the
390 * returned string is undefined but not null.
392 const char *interpolation_string() const;
395 class ast_struct_specifier : public ast_node {
396 public:
397 ast_struct_specifier(char *identifier, ast_node *declarator_list);
398 virtual void print(void) const;
400 virtual ir_rvalue *hir(exec_list *instructions,
401 struct _mesa_glsl_parse_state *state);
403 char *name;
404 exec_list declarations;
408 enum ast_types {
409 ast_void,
410 ast_float,
411 ast_int,
412 ast_uint,
413 ast_bool,
414 ast_vec2,
415 ast_vec3,
416 ast_vec4,
417 ast_bvec2,
418 ast_bvec3,
419 ast_bvec4,
420 ast_ivec2,
421 ast_ivec3,
422 ast_ivec4,
423 ast_uvec2,
424 ast_uvec3,
425 ast_uvec4,
426 ast_mat2,
427 ast_mat2x3,
428 ast_mat2x4,
429 ast_mat3x2,
430 ast_mat3,
431 ast_mat3x4,
432 ast_mat4x2,
433 ast_mat4x3,
434 ast_mat4,
435 ast_sampler1d,
436 ast_sampler2d,
437 ast_sampler2drect,
438 ast_sampler3d,
439 ast_samplercube,
440 ast_sampler1dshadow,
441 ast_sampler2dshadow,
442 ast_sampler2drectshadow,
443 ast_samplercubeshadow,
444 ast_sampler1darray,
445 ast_sampler2darray,
446 ast_sampler1darrayshadow,
447 ast_sampler2darrayshadow,
448 ast_isampler1d,
449 ast_isampler2d,
450 ast_isampler3d,
451 ast_isamplercube,
452 ast_isampler1darray,
453 ast_isampler2darray,
454 ast_usampler1d,
455 ast_usampler2d,
456 ast_usampler3d,
457 ast_usamplercube,
458 ast_usampler1darray,
459 ast_usampler2darray,
461 ast_struct,
462 ast_type_name
466 class ast_type_specifier : public ast_node {
467 public:
468 ast_type_specifier(int specifier);
470 /** Construct a type specifier from a type name */
471 ast_type_specifier(const char *name)
472 : type_specifier(ast_type_name), type_name(name), structure(NULL),
473 is_array(false), array_size(NULL), precision(ast_precision_none),
474 is_precision_statement(false)
476 /* empty */
479 /** Construct a type specifier from a structure definition */
480 ast_type_specifier(ast_struct_specifier *s)
481 : type_specifier(ast_struct), type_name(s->name), structure(s),
482 is_array(false), array_size(NULL), precision(ast_precision_none),
483 is_precision_statement(false)
485 /* empty */
488 const struct glsl_type *glsl_type(const char **name,
489 struct _mesa_glsl_parse_state *state)
490 const;
492 virtual void print(void) const;
494 ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
496 enum ast_types type_specifier;
498 const char *type_name;
499 ast_struct_specifier *structure;
501 int is_array;
502 ast_expression *array_size;
504 unsigned precision:2;
506 bool is_precision_statement;
510 class ast_fully_specified_type : public ast_node {
511 public:
512 virtual void print(void) const;
513 bool has_qualifiers() const;
515 ast_type_qualifier qualifier;
516 ast_type_specifier *specifier;
520 class ast_declarator_list : public ast_node {
521 public:
522 ast_declarator_list(ast_fully_specified_type *);
523 virtual void print(void) const;
525 virtual ir_rvalue *hir(exec_list *instructions,
526 struct _mesa_glsl_parse_state *state);
528 ast_fully_specified_type *type;
529 exec_list declarations;
532 * Special flag for vertex shader "invariant" declarations.
534 * Vertex shaders can contain "invariant" variable redeclarations that do
535 * not include a type. For example, "invariant gl_Position;". This flag
536 * is used to note these cases when no type is specified.
538 int invariant;
542 class ast_parameter_declarator : public ast_node {
543 public:
544 ast_parameter_declarator()
546 this->identifier = NULL;
547 this->is_array = false;
548 this->array_size = 0;
551 virtual void print(void) const;
553 virtual ir_rvalue *hir(exec_list *instructions,
554 struct _mesa_glsl_parse_state *state);
556 ast_fully_specified_type *type;
557 char *identifier;
558 int is_array;
559 ast_expression *array_size;
561 static void parameters_to_hir(exec_list *ast_parameters,
562 bool formal, exec_list *ir_parameters,
563 struct _mesa_glsl_parse_state *state);
565 private:
566 /** Is this parameter declaration part of a formal parameter list? */
567 bool formal_parameter;
570 * Is this parameter 'void' type?
572 * This field is set by \c ::hir.
574 bool is_void;
578 class ast_function : public ast_node {
579 public:
580 ast_function(void);
582 virtual void print(void) const;
584 virtual ir_rvalue *hir(exec_list *instructions,
585 struct _mesa_glsl_parse_state *state);
587 ast_fully_specified_type *return_type;
588 char *identifier;
590 exec_list parameters;
592 private:
594 * Is this prototype part of the function definition?
596 * Used by ast_function_definition::hir to process the parameters, etc.
597 * of the function.
599 * \sa ::hir
601 bool is_definition;
604 * Function signature corresponding to this function prototype instance
606 * Used by ast_function_definition::hir to process the parameters, etc.
607 * of the function.
609 * \sa ::hir
611 class ir_function_signature *signature;
613 friend class ast_function_definition;
617 class ast_expression_statement : public ast_node {
618 public:
619 ast_expression_statement(ast_expression *);
620 virtual void print(void) const;
622 virtual ir_rvalue *hir(exec_list *instructions,
623 struct _mesa_glsl_parse_state *state);
625 ast_expression *expression;
629 class ast_case_label : public ast_node {
630 public:
633 * An expression of NULL means 'default'.
635 ast_expression *expression;
638 class ast_selection_statement : public ast_node {
639 public:
640 ast_selection_statement(ast_expression *condition,
641 ast_node *then_statement,
642 ast_node *else_statement);
643 virtual void print(void) const;
645 virtual ir_rvalue *hir(exec_list *instructions,
646 struct _mesa_glsl_parse_state *state);
648 ast_expression *condition;
649 ast_node *then_statement;
650 ast_node *else_statement;
654 class ast_switch_statement : public ast_node {
655 public:
656 ast_expression *expression;
657 exec_list statements;
660 class ast_iteration_statement : public ast_node {
661 public:
662 ast_iteration_statement(int mode, ast_node *init, ast_node *condition,
663 ast_expression *rest_expression, ast_node *body);
665 virtual void print(void) const;
667 virtual ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
669 enum ast_iteration_modes {
670 ast_for,
671 ast_while,
672 ast_do_while
673 } mode;
676 ast_node *init_statement;
677 ast_node *condition;
678 ast_expression *rest_expression;
680 ast_node *body;
682 private:
684 * Generate IR from the condition of a loop
686 * This is factored out of ::hir because some loops have the condition
687 * test at the top (for and while), and others have it at the end (do-while).
689 void condition_to_hir(class ir_loop *, struct _mesa_glsl_parse_state *);
693 class ast_jump_statement : public ast_node {
694 public:
695 ast_jump_statement(int mode, ast_expression *return_value);
696 virtual void print(void) const;
698 virtual ir_rvalue *hir(exec_list *instructions,
699 struct _mesa_glsl_parse_state *state);
701 enum ast_jump_modes {
702 ast_continue,
703 ast_break,
704 ast_return,
705 ast_discard
706 } mode;
708 ast_expression *opt_return_value;
712 class ast_function_definition : public ast_node {
713 public:
714 virtual void print(void) const;
716 virtual ir_rvalue *hir(exec_list *instructions,
717 struct _mesa_glsl_parse_state *state);
719 ast_function *prototype;
720 ast_compound_statement *body;
722 /*@}*/
724 extern void
725 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state);
727 extern ir_rvalue *
728 _mesa_ast_field_selection_to_hir(const ast_expression *expr,
729 exec_list *instructions,
730 struct _mesa_glsl_parse_state *state);
732 void
733 emit_function(_mesa_glsl_parse_state *state, exec_list *instructions,
734 ir_function *f);
736 #endif /* AST_H */