glsl2: Add a method for querying if an AST type has any qualifiers.
[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
307 class ast_struct_specifier : public ast_node {
308 public:
309 ast_struct_specifier(char *identifier, ast_node *declarator_list);
310 virtual void print(void) const;
311
312 virtual ir_rvalue *hir(exec_list *instructions,
313 struct _mesa_glsl_parse_state *state);
314
315 char *name;
316 exec_list declarations;
317 };
318
319
320 enum ast_types {
321 ast_void,
322 ast_float,
323 ast_int,
324 ast_uint,
325 ast_bool,
326 ast_vec2,
327 ast_vec3,
328 ast_vec4,
329 ast_bvec2,
330 ast_bvec3,
331 ast_bvec4,
332 ast_ivec2,
333 ast_ivec3,
334 ast_ivec4,
335 ast_uvec2,
336 ast_uvec3,
337 ast_uvec4,
338 ast_mat2,
339 ast_mat2x3,
340 ast_mat2x4,
341 ast_mat3x2,
342 ast_mat3,
343 ast_mat3x4,
344 ast_mat4x2,
345 ast_mat4x3,
346 ast_mat4,
347 ast_sampler1d,
348 ast_sampler2d,
349 ast_sampler2drect,
350 ast_sampler3d,
351 ast_samplercube,
352 ast_sampler1dshadow,
353 ast_sampler2dshadow,
354 ast_sampler2drectshadow,
355 ast_samplercubeshadow,
356 ast_sampler1darray,
357 ast_sampler2darray,
358 ast_sampler1darrayshadow,
359 ast_sampler2darrayshadow,
360 ast_isampler1d,
361 ast_isampler2d,
362 ast_isampler3d,
363 ast_isamplercube,
364 ast_isampler1darray,
365 ast_isampler2darray,
366 ast_usampler1d,
367 ast_usampler2d,
368 ast_usampler3d,
369 ast_usamplercube,
370 ast_usampler1darray,
371 ast_usampler2darray,
372
373 ast_struct,
374 ast_type_name
375 };
376
377
378 class ast_type_specifier : public ast_node {
379 public:
380 ast_type_specifier(int specifier);
381
382 /** Construct a type specifier from a type name */
383 ast_type_specifier(const char *name)
384 : type_specifier(ast_type_name), type_name(name), structure(NULL),
385 is_array(false), array_size(NULL), precision(ast_precision_high)
386 {
387 /* empty */
388 }
389
390 /** Construct a type specifier from a structure definition */
391 ast_type_specifier(ast_struct_specifier *s)
392 : type_specifier(ast_struct), type_name(s->name), structure(s),
393 is_array(false), array_size(NULL), precision(ast_precision_high)
394 {
395 /* empty */
396 }
397
398 const struct glsl_type *glsl_type(const char **name,
399 struct _mesa_glsl_parse_state *state)
400 const;
401
402 virtual void print(void) const;
403
404 ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
405
406 enum ast_types type_specifier;
407
408 const char *type_name;
409 ast_struct_specifier *structure;
410
411 int is_array;
412 ast_expression *array_size;
413
414 unsigned precision:2;
415 };
416
417
418 class ast_fully_specified_type : public ast_node {
419 public:
420 virtual void print(void) const;
421 bool has_qualifiers() const;
422
423 ast_type_qualifier qualifier;
424 ast_type_specifier *specifier;
425 };
426
427
428 class ast_declarator_list : public ast_node {
429 public:
430 ast_declarator_list(ast_fully_specified_type *);
431 virtual void print(void) const;
432
433 virtual ir_rvalue *hir(exec_list *instructions,
434 struct _mesa_glsl_parse_state *state);
435
436 ast_fully_specified_type *type;
437 exec_list declarations;
438
439 /**
440 * Special flag for vertex shader "invariant" declarations.
441 *
442 * Vertex shaders can contain "invariant" variable redeclarations that do
443 * not include a type. For example, "invariant gl_Position;". This flag
444 * is used to note these cases when no type is specified.
445 */
446 int invariant;
447 };
448
449
450 class ast_parameter_declarator : public ast_node {
451 public:
452 virtual void print(void) const;
453
454 virtual ir_rvalue *hir(exec_list *instructions,
455 struct _mesa_glsl_parse_state *state);
456
457 ast_fully_specified_type *type;
458 char *identifier;
459 int is_array;
460 ast_expression *array_size;
461
462 static void parameters_to_hir(exec_list *ast_parameters,
463 bool formal, exec_list *ir_parameters,
464 struct _mesa_glsl_parse_state *state);
465
466 private:
467 /** Is this parameter declaration part of a formal parameter list? */
468 bool formal_parameter;
469
470 /**
471 * Is this parameter 'void' type?
472 *
473 * This field is set by \c ::hir.
474 */
475 bool is_void;
476 };
477
478
479 class ast_function : public ast_node {
480 public:
481 ast_function(void);
482
483 virtual void print(void) const;
484
485 virtual ir_rvalue *hir(exec_list *instructions,
486 struct _mesa_glsl_parse_state *state);
487
488 ast_fully_specified_type *return_type;
489 char *identifier;
490
491 exec_list parameters;
492
493 private:
494 /**
495 * Is this prototype part of the function definition?
496 *
497 * Used by ast_function_definition::hir to process the parameters, etc.
498 * of the function.
499 *
500 * \sa ::hir
501 */
502 bool is_definition;
503
504 /**
505 * Function signature corresponding to this function prototype instance
506 *
507 * Used by ast_function_definition::hir to process the parameters, etc.
508 * of the function.
509 *
510 * \sa ::hir
511 */
512 class ir_function_signature *signature;
513
514 friend class ast_function_definition;
515 };
516
517
518 class ast_declaration_statement : public ast_node {
519 public:
520 ast_declaration_statement(void);
521
522 enum {
523 ast_function,
524 ast_declaration,
525 ast_precision
526 } mode;
527
528 union {
529 class ast_function *function;
530 ast_declarator_list *declarator;
531 ast_type_specifier *type;
532 ast_node *node;
533 } declaration;
534 };
535
536
537 class ast_expression_statement : public ast_node {
538 public:
539 ast_expression_statement(ast_expression *);
540 virtual void print(void) const;
541
542 virtual ir_rvalue *hir(exec_list *instructions,
543 struct _mesa_glsl_parse_state *state);
544
545 ast_expression *expression;
546 };
547
548
549 class ast_case_label : public ast_node {
550 public:
551
552 /**
553 * An expression of NULL means 'default'.
554 */
555 ast_expression *expression;
556 };
557
558 class ast_selection_statement : public ast_node {
559 public:
560 ast_selection_statement(ast_expression *condition,
561 ast_node *then_statement,
562 ast_node *else_statement);
563 virtual void print(void) const;
564
565 virtual ir_rvalue *hir(exec_list *instructions,
566 struct _mesa_glsl_parse_state *state);
567
568 ast_expression *condition;
569 ast_node *then_statement;
570 ast_node *else_statement;
571 };
572
573
574 class ast_switch_statement : public ast_node {
575 public:
576 ast_expression *expression;
577 exec_list statements;
578 };
579
580 class ast_iteration_statement : public ast_node {
581 public:
582 ast_iteration_statement(int mode, ast_node *init, ast_node *condition,
583 ast_expression *rest_expression, ast_node *body);
584
585 virtual void print(void) const;
586
587 virtual ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
588
589 enum ast_iteration_modes {
590 ast_for,
591 ast_while,
592 ast_do_while
593 } mode;
594
595
596 ast_node *init_statement;
597 ast_node *condition;
598 ast_expression *rest_expression;
599
600 ast_node *body;
601
602 private:
603 /**
604 * Generate IR from the condition of a loop
605 *
606 * This is factored out of ::hir because some loops have the condition
607 * test at the top (for and while), and others have it at the end (do-while).
608 */
609 void condition_to_hir(class ir_loop *, struct _mesa_glsl_parse_state *);
610 };
611
612
613 class ast_jump_statement : public ast_node {
614 public:
615 ast_jump_statement(int mode, ast_expression *return_value);
616 virtual void print(void) const;
617
618 virtual ir_rvalue *hir(exec_list *instructions,
619 struct _mesa_glsl_parse_state *state);
620
621 enum ast_jump_modes {
622 ast_continue,
623 ast_break,
624 ast_return,
625 ast_discard
626 } mode;
627
628 ast_expression *opt_return_value;
629 };
630
631
632 class ast_function_definition : public ast_node {
633 public:
634 virtual void print(void) const;
635
636 virtual ir_rvalue *hir(exec_list *instructions,
637 struct _mesa_glsl_parse_state *state);
638
639 ast_function *prototype;
640 ast_compound_statement *body;
641 };
642
643
644 extern void
645 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state);
646
647 extern struct ir_rvalue *
648 _mesa_ast_field_selection_to_hir(const struct ast_expression *expr,
649 exec_list *instructions,
650 struct _mesa_glsl_parse_state *state);
651
652 #endif /* AST_H */