Add support for =, != to ir_constant_expresion.cpp
[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.
182 */
183 struct simple_node expressions;
184 };
185
186 class ast_expression_bin : public ast_expression {
187 public:
188 ast_expression_bin(int oper, ast_expression *, ast_expression *);
189
190 virtual void print(void) const;
191 };
192
193 /**
194 * Subclass of expressions for function calls
195 */
196 class ast_function_expression : public ast_expression {
197 public:
198 ast_function_expression(ast_expression *callee)
199 : ast_expression(ast_function_call, callee,
200 NULL, NULL),
201 cons(false)
202 {
203 /* empty */
204 }
205
206 ast_function_expression(class ast_type_specifier *type)
207 : ast_expression(ast_function_call, (ast_expression *) type,
208 NULL, NULL),
209 cons(true)
210 {
211 /* empty */
212 }
213
214 bool is_constructor() const
215 {
216 return cons;
217 }
218
219 virtual ir_rvalue *hir(exec_list *instructions,
220 struct _mesa_glsl_parse_state *state);
221
222 private:
223 /**
224 * Is this function call actually a constructor?
225 */
226 bool cons;
227 };
228
229
230 /**
231 * Number of possible operators for an ast_expression
232 *
233 * This is done as a define instead of as an additional value in the enum so
234 * that the compiler won't generate spurious messages like "warning:
235 * enumeration value ‘ast_num_operators’ not handled in switch"
236 */
237 #define AST_NUM_OPERATORS (ast_sequence + 1)
238
239
240 class ast_compound_statement : public ast_node {
241 public:
242 ast_compound_statement(int new_scope, ast_node *statements);
243 virtual void print(void) const;
244
245 virtual ir_rvalue *hir(exec_list *instructions,
246 struct _mesa_glsl_parse_state *state);
247
248 int new_scope;
249 struct simple_node statements;
250 };
251
252 class ast_declaration : public ast_node {
253 public:
254 ast_declaration(char *identifier, int is_array, ast_expression *array_size,
255 ast_expression *initializer);
256 virtual void print(void) const;
257
258 char *identifier;
259
260 int is_array;
261 ast_expression *array_size;
262
263 ast_expression *initializer;
264 };
265
266
267 enum {
268 ast_precision_high = 0, /**< Default precision. */
269 ast_precision_medium,
270 ast_precision_low
271 };
272
273 struct ast_type_qualifier {
274 unsigned invariant:1;
275 unsigned constant:1;
276 unsigned attribute:1;
277 unsigned varying:1;
278 unsigned in:1;
279 unsigned out:1;
280 unsigned centroid:1;
281 unsigned uniform:1;
282 unsigned smooth:1;
283 unsigned flat:1;
284 unsigned noperspective:1;
285 };
286
287 class ast_struct_specifier : public ast_node {
288 public:
289 ast_struct_specifier(char *identifier, ast_node *declarator_list);
290 virtual void print(void) const;
291
292 char *name;
293 struct simple_node declarations;
294 };
295
296
297 enum ast_types {
298 ast_void,
299 ast_float,
300 ast_int,
301 ast_uint,
302 ast_bool,
303 ast_vec2,
304 ast_vec3,
305 ast_vec4,
306 ast_bvec2,
307 ast_bvec3,
308 ast_bvec4,
309 ast_ivec2,
310 ast_ivec3,
311 ast_ivec4,
312 ast_uvec2,
313 ast_uvec3,
314 ast_uvec4,
315 ast_mat2,
316 ast_mat2x3,
317 ast_mat2x4,
318 ast_mat3x2,
319 ast_mat3,
320 ast_mat3x4,
321 ast_mat4x2,
322 ast_mat4x3,
323 ast_mat4,
324 ast_sampler1d,
325 ast_sampler2d,
326 ast_sampler2drect,
327 ast_sampler3d,
328 ast_samplercube,
329 ast_sampler1dshadow,
330 ast_sampler2dshadow,
331 ast_sampler2drectshadow,
332 ast_samplercubeshadow,
333 ast_sampler1darray,
334 ast_sampler2darray,
335 ast_sampler1darrayshadow,
336 ast_sampler2darrayshadow,
337 ast_isampler1d,
338 ast_isampler2d,
339 ast_isampler3d,
340 ast_isamplercube,
341 ast_isampler1darray,
342 ast_isampler2darray,
343 ast_usampler1d,
344 ast_usampler2d,
345 ast_usampler3d,
346 ast_usamplercube,
347 ast_usampler1darray,
348 ast_usampler2darray,
349
350 ast_struct,
351 ast_type_name
352 };
353
354
355 class ast_type_specifier : public ast_node {
356 public:
357 ast_type_specifier(int specifier);
358
359 /** Construct a type specifier from a type name */
360 ast_type_specifier(const char *name)
361 : type_specifier(ast_type_name), type_name(name), structure(NULL),
362 is_array(false), array_size(NULL), precision(ast_precision_high)
363 {
364 /* empty */
365 }
366
367 /** Construct a type specifier from a structure definition */
368 ast_type_specifier(ast_struct_specifier *s)
369 : type_specifier(ast_struct), type_name(s->name), structure(s),
370 is_array(false), array_size(NULL), precision(ast_precision_high)
371 {
372 /* empty */
373 }
374
375 const struct glsl_type *glsl_type(const char **name,
376 struct _mesa_glsl_parse_state *state)
377 const;
378
379 virtual void print(void) const;
380
381 enum ast_types type_specifier;
382
383 const char *type_name;
384 ast_struct_specifier *structure;
385
386 int is_array;
387 ast_expression *array_size;
388
389 unsigned precision:2;
390 };
391
392
393 class ast_fully_specified_type : public ast_node {
394 public:
395 virtual void print(void) const;
396
397 ast_type_qualifier qualifier;
398 ast_type_specifier *specifier;
399 };
400
401
402 class ast_declarator_list : public ast_node {
403 public:
404 ast_declarator_list(ast_fully_specified_type *);
405 virtual void print(void) const;
406
407 virtual ir_rvalue *hir(exec_list *instructions,
408 struct _mesa_glsl_parse_state *state);
409
410 ast_fully_specified_type *type;
411 struct simple_node declarations;
412
413 /**
414 * Special flag for vertex shader "invariant" declarations.
415 *
416 * Vertex shaders can contain "invariant" variable redeclarations that do
417 * not include a type. For example, "invariant gl_Position;". This flag
418 * is used to note these cases when no type is specified.
419 */
420 int invariant;
421 };
422
423
424 class ast_parameter_declarator : public ast_node {
425 public:
426 virtual void print(void) const;
427
428 virtual ir_rvalue *hir(exec_list *instructions,
429 struct _mesa_glsl_parse_state *state);
430
431 ast_fully_specified_type *type;
432 char *identifier;
433 int is_array;
434 ast_expression *array_size;
435
436 static void parameters_to_hir(simple_node *ast_parameters,
437 bool formal, exec_list *ir_parameters,
438 struct _mesa_glsl_parse_state *state);
439
440 private:
441 /** Is this parameter declaration part of a formal parameter list? */
442 bool formal_parameter;
443
444 /**
445 * Is this parameter 'void' type?
446 *
447 * This field is set by \c ::hir.
448 */
449 bool is_void;
450 };
451
452
453 class ast_function : public ast_node {
454 public:
455 ast_function(void);
456
457 virtual void print(void) const;
458
459 virtual ir_rvalue *hir(exec_list *instructions,
460 struct _mesa_glsl_parse_state *state);
461
462 ast_fully_specified_type *return_type;
463 char *identifier;
464
465 struct simple_node parameters;
466
467 private:
468 /**
469 * Is this prototype part of the function definition?
470 *
471 * Used by ast_function_definition::hir to process the parameters, etc.
472 * of the function.
473 *
474 * \sa ::hir
475 */
476 bool is_definition;
477
478 /**
479 * Function signature corresponding to this function prototype instance
480 *
481 * Used by ast_function_definition::hir to process the parameters, etc.
482 * of the function.
483 *
484 * \sa ::hir
485 */
486 class ir_function_signature *signature;
487
488 friend class ast_function_definition;
489 };
490
491
492 class ast_declaration_statement : public ast_node {
493 public:
494 ast_declaration_statement(void);
495
496 enum {
497 ast_function,
498 ast_declaration,
499 ast_precision
500 } mode;
501
502 union {
503 class ast_function *function;
504 ast_declarator_list *declarator;
505 ast_type_specifier *type;
506 ast_node *node;
507 } declaration;
508 };
509
510
511 class ast_expression_statement : public ast_node {
512 public:
513 ast_expression_statement(ast_expression *);
514 virtual void print(void) const;
515
516 virtual ir_rvalue *hir(exec_list *instructions,
517 struct _mesa_glsl_parse_state *state);
518
519 ast_expression *expression;
520 };
521
522
523 class ast_case_label : public ast_node {
524 public:
525
526 /**
527 * An expression of NULL means 'default'.
528 */
529 ast_expression *expression;
530 };
531
532 class ast_selection_statement : public ast_node {
533 public:
534 ast_selection_statement(ast_expression *condition,
535 ast_node *then_statement,
536 ast_node *else_statement);
537 virtual void print(void) const;
538
539 virtual ir_rvalue *hir(exec_list *instructions,
540 struct _mesa_glsl_parse_state *state);
541
542 ast_expression *condition;
543 ast_node *then_statement;
544 ast_node *else_statement;
545 };
546
547
548 class ast_switch_statement : public ast_node {
549 public:
550 ast_expression *expression;
551 struct simple_node statements;
552 };
553
554 class ast_iteration_statement : public ast_node {
555 public:
556 ast_iteration_statement(int mode, ast_node *init, ast_node *condition,
557 ast_expression *rest_expression, ast_node *body);
558
559 virtual void print(void) const;
560
561 enum ast_iteration_modes {
562 ast_for,
563 ast_while,
564 ast_do_while
565 } mode;
566
567
568 ast_node *init_statement;
569 ast_node *condition;
570 ast_expression *rest_expression;
571
572 ast_node *body;
573 };
574
575
576 class ast_jump_statement : public ast_node {
577 public:
578 ast_jump_statement(int mode, ast_expression *return_value);
579 virtual void print(void) const;
580
581 virtual ir_rvalue *hir(exec_list *instructions,
582 struct _mesa_glsl_parse_state *state);
583
584 enum ast_jump_modes {
585 ast_continue,
586 ast_break,
587 ast_return,
588 ast_discard
589 } mode;
590
591 ast_expression *opt_return_value;
592 };
593
594
595 class ast_function_definition : public ast_node {
596 public:
597 virtual void print(void) const;
598
599 virtual ir_rvalue *hir(exec_list *instructions,
600 struct _mesa_glsl_parse_state *state);
601
602 ast_function *prototype;
603 ast_compound_statement *body;
604 };
605
606
607 extern void
608 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state);
609
610 extern struct ir_rvalue *
611 _mesa_ast_field_selection_to_hir(const struct ast_expression *expr,
612 exec_list *instructions,
613 struct _mesa_glsl_parse_state *state);
614
615 #endif /* AST_H */