Add functions to generate constructors for built-in types.
[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_instruction *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_instruction *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_instruction *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_instruction *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_sampler3d,
330 ast_samplercube,
331 ast_sampler1dshadow,
332 ast_sampler2dshadow,
333 ast_samplercubeshadow,
334 ast_sampler1darray,
335 ast_sampler2darray,
336 ast_sampler1darrayshadow,
337 ast_sampler2darrayshadow,
338 ast_isampler1d,
339 ast_isampler2d,
340 ast_isampler3d,
341 ast_isamplercube,
342 ast_isampler1darray,
343 ast_isampler2darray,
344 ast_usampler1d,
345 ast_usampler2d,
346 ast_usampler3d,
347 ast_usamplercube,
348 ast_usampler1darray,
349 ast_usampler2darray,
350
351 ast_struct,
352 ast_type_name
353 };
354
355
356 class ast_type_specifier : public ast_node {
357 public:
358 ast_type_specifier(int specifier);
359
360 /** Construct a type specifier from a type name */
361 ast_type_specifier(const char *name)
362 : type_specifier(ast_type_name), type_name(name), structure(NULL),
363 is_array(false), array_size(NULL), precision(ast_precision_high)
364 {
365 /* empty */
366 }
367
368 /** Construct a type specifier from a structure definition */
369 ast_type_specifier(ast_struct_specifier *s)
370 : type_specifier(ast_struct), type_name(s->name), structure(s),
371 is_array(false), array_size(NULL), precision(ast_precision_high)
372 {
373 /* empty */
374 }
375
376 virtual void print(void) const;
377
378 enum ast_types type_specifier;
379
380 const char *type_name;
381 ast_struct_specifier *structure;
382
383 int is_array;
384 ast_expression *array_size;
385
386 unsigned precision:2;
387 };
388
389
390 class ast_fully_specified_type : public ast_node {
391 public:
392 virtual void print(void) const;
393
394 ast_type_qualifier qualifier;
395 ast_type_specifier *specifier;
396 };
397
398
399 class ast_declarator_list : public ast_node {
400 public:
401 ast_declarator_list(ast_fully_specified_type *);
402 virtual void print(void) const;
403
404 virtual ir_instruction *hir(exec_list *instructions,
405 struct _mesa_glsl_parse_state *state);
406
407 ast_fully_specified_type *type;
408 struct simple_node declarations;
409
410 /**
411 * Special flag for vertex shader "invariant" declarations.
412 *
413 * Vertex shaders can contain "invariant" variable redeclarations that do
414 * not include a type. For example, "invariant gl_Position;". This flag
415 * is used to note these cases when no type is specified.
416 */
417 int invariant;
418 };
419
420
421 class ast_parameter_declarator : public ast_node {
422 public:
423 virtual void print(void) const;
424
425 virtual ir_instruction *hir(exec_list *instructions,
426 struct _mesa_glsl_parse_state *state);
427
428 ast_fully_specified_type *type;
429 char *identifier;
430 int is_array;
431 ast_expression *array_size;
432 };
433
434
435 class ast_function : public ast_node {
436 public:
437 ast_function(void);
438
439 virtual void print(void) const;
440
441 ast_fully_specified_type *return_type;
442 char *identifier;
443
444 struct simple_node parameters;
445 };
446
447
448 class ast_declaration_statement : public ast_node {
449 public:
450 ast_declaration_statement(void);
451
452 enum {
453 ast_function,
454 ast_declaration,
455 ast_precision
456 } mode;
457
458 union {
459 class ast_function *function;
460 ast_declarator_list *declarator;
461 ast_type_specifier *type;
462 ast_node *node;
463 } declaration;
464 };
465
466
467 class ast_expression_statement : public ast_node {
468 public:
469 ast_expression_statement(ast_expression *);
470 virtual void print(void) const;
471
472 virtual ir_instruction *hir(exec_list *instructions,
473 struct _mesa_glsl_parse_state *state);
474
475 ast_expression *expression;
476 };
477
478
479 class ast_case_label : public ast_node {
480 public:
481
482 /**
483 * An expression of NULL means 'default'.
484 */
485 ast_expression *expression;
486 };
487
488 class ast_selection_statement : public ast_node {
489 public:
490 ast_selection_statement(ast_expression *condition,
491 ast_node *then_statement,
492 ast_node *else_statement);
493 virtual void print(void) const;
494
495 ast_expression *condition;
496 ast_node *then_statement;
497 ast_node *else_statement;
498 };
499
500
501 class ast_switch_statement : public ast_node {
502 public:
503 ast_expression *expression;
504 struct simple_node statements;
505 };
506
507 class ast_iteration_statement : public ast_node {
508 public:
509 ast_iteration_statement(int mode, ast_node *init, ast_node *condition,
510 ast_expression *rest_expression, ast_node *body);
511
512 virtual void print(void) const;
513
514 enum ast_iteration_modes {
515 ast_for,
516 ast_while,
517 ast_do_while
518 } mode;
519
520
521 ast_node *init_statement;
522 ast_node *condition;
523 ast_expression *rest_expression;
524
525 ast_node *body;
526 };
527
528
529 class ast_jump_statement : public ast_node {
530 public:
531 ast_jump_statement(int mode, ast_expression *return_value);
532 virtual void print(void) const;
533
534 virtual ir_instruction *hir(exec_list *instructions,
535 struct _mesa_glsl_parse_state *state);
536
537 enum ast_jump_modes {
538 ast_continue,
539 ast_break,
540 ast_return,
541 ast_discard
542 } mode;
543
544 ast_expression *opt_return_value;
545 };
546
547
548 class ast_function_definition : public ast_node {
549 public:
550 virtual void print(void) const;
551
552 virtual ir_instruction *hir(exec_list *instructions,
553 struct _mesa_glsl_parse_state *state);
554
555 ast_function *prototype;
556 ast_compound_statement *body;
557 };
558
559
560 extern void
561 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state);
562
563 extern struct ir_instruction *
564 _mesa_ast_field_selection_to_hir(const struct ast_expression *expr,
565 exec_list *instructions,
566 struct _mesa_glsl_parse_state *state);
567
568 #endif /* AST_H */