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