Remove ast_node::type
[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
437
438 class ast_function : public ast_node {
439 public:
440 ast_function(void);
441
442 virtual void print(void) const;
443
444 virtual ir_rvalue *hir(exec_list *instructions,
445 struct _mesa_glsl_parse_state *state);
446
447 ast_fully_specified_type *return_type;
448 char *identifier;
449
450 struct simple_node parameters;
451
452 private:
453 /**
454 * Is this prototype part of the function definition?
455 *
456 * Used by ast_function_definition::hir to process the parameters, etc.
457 * of the function.
458 *
459 * \sa ::hir
460 */
461 bool is_definition;
462
463 /**
464 * Function signature corresponding to this function prototype instance
465 *
466 * Used by ast_function_definition::hir to process the parameters, etc.
467 * of the function.
468 *
469 * \sa ::hir
470 */
471 class ir_function_signature *signature;
472
473 friend class ast_function_definition;
474 };
475
476
477 class ast_declaration_statement : public ast_node {
478 public:
479 ast_declaration_statement(void);
480
481 enum {
482 ast_function,
483 ast_declaration,
484 ast_precision
485 } mode;
486
487 union {
488 class ast_function *function;
489 ast_declarator_list *declarator;
490 ast_type_specifier *type;
491 ast_node *node;
492 } declaration;
493 };
494
495
496 class ast_expression_statement : public ast_node {
497 public:
498 ast_expression_statement(ast_expression *);
499 virtual void print(void) const;
500
501 virtual ir_rvalue *hir(exec_list *instructions,
502 struct _mesa_glsl_parse_state *state);
503
504 ast_expression *expression;
505 };
506
507
508 class ast_case_label : public ast_node {
509 public:
510
511 /**
512 * An expression of NULL means 'default'.
513 */
514 ast_expression *expression;
515 };
516
517 class ast_selection_statement : public ast_node {
518 public:
519 ast_selection_statement(ast_expression *condition,
520 ast_node *then_statement,
521 ast_node *else_statement);
522 virtual void print(void) const;
523
524 virtual ir_rvalue *hir(exec_list *instructions,
525 struct _mesa_glsl_parse_state *state);
526
527 ast_expression *condition;
528 ast_node *then_statement;
529 ast_node *else_statement;
530 };
531
532
533 class ast_switch_statement : public ast_node {
534 public:
535 ast_expression *expression;
536 struct simple_node statements;
537 };
538
539 class ast_iteration_statement : public ast_node {
540 public:
541 ast_iteration_statement(int mode, ast_node *init, ast_node *condition,
542 ast_expression *rest_expression, ast_node *body);
543
544 virtual void print(void) const;
545
546 enum ast_iteration_modes {
547 ast_for,
548 ast_while,
549 ast_do_while
550 } mode;
551
552
553 ast_node *init_statement;
554 ast_node *condition;
555 ast_expression *rest_expression;
556
557 ast_node *body;
558 };
559
560
561 class ast_jump_statement : public ast_node {
562 public:
563 ast_jump_statement(int mode, ast_expression *return_value);
564 virtual void print(void) const;
565
566 virtual ir_rvalue *hir(exec_list *instructions,
567 struct _mesa_glsl_parse_state *state);
568
569 enum ast_jump_modes {
570 ast_continue,
571 ast_break,
572 ast_return,
573 ast_discard
574 } mode;
575
576 ast_expression *opt_return_value;
577 };
578
579
580 class ast_function_definition : public ast_node {
581 public:
582 virtual void print(void) const;
583
584 virtual ir_rvalue *hir(exec_list *instructions,
585 struct _mesa_glsl_parse_state *state);
586
587 ast_function *prototype;
588 ast_compound_statement *body;
589 };
590
591
592 extern void
593 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state);
594
595 extern struct ir_rvalue *
596 _mesa_ast_field_selection_to_hir(const struct ast_expression *expr,
597 exec_list *instructions,
598 struct _mesa_glsl_parse_state *state);
599
600 #endif /* AST_H */