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