Rename .cc files to .cpp
[mesa.git] / ast.h
1 /*
2 * Copyright © 2009 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #pragma once
25 #ifndef AST_H
26 #define AST_H
27
28 #include "main/simple_list.h"
29 #include "glsl_parser_extras.h"
30
31 struct ir_instruction;
32 struct _mesa_glsl_parse_state;
33
34 struct YYLTYPE;
35
36 #define _mesa_ast_print(n) \
37 ((ast_node *) n)->print()
38
39 #define _mesa_ast_to_hir(n, instr, s) \
40 ((struct ast_node *) n)->vtbl->to_hir((struct ast_node *) n, instr, s)
41
42 #define _mesa_ast_function_call_to_hir(n, p, s) \
43 ((struct ast_node *) n)->vtbl->function_call_to_hir( \
44 (struct ast_node *) n, \
45 (struct ast_node *) p, \
46 s)
47
48 class ast_node : public simple_node {
49 public:
50 virtual ~ast_node();
51 virtual void print(void) const;
52
53 /**
54 * Retrieve the source location of an AST node
55 *
56 * This function is primarily used to get the source position of an AST node
57 * into a form that can be passed to \c _mesa_glsl_error.
58 *
59 * \sa _mesa_glsl_error, ast_node::set_location
60 */
61 struct YYLTYPE get_location(void) const
62 {
63 struct YYLTYPE locp;
64
65 locp.source = this->location.source;
66 locp.first_line = this->location.line;
67 locp.first_column = this->location.column;
68 locp.last_line = locp.first_line;
69 locp.last_column = locp.first_column;
70
71 return locp;
72 }
73
74 /**
75 * Set the source location of an AST node from a parser location
76 *
77 * \sa ast_node::get_location
78 */
79 void set_location(const struct YYLTYPE *locp)
80 {
81 this->location.source = locp->source;
82 this->location.line = locp->first_line;
83 this->location.column = locp->first_column;
84 }
85
86
87 int type;
88
89 struct {
90 unsigned source;
91 unsigned line;
92 unsigned column;
93 } location;
94
95 protected:
96 ast_node(void);
97 };
98
99
100 enum ast_operators {
101 ast_assign,
102 ast_plus, /**< Unary + operator. */
103 ast_neg,
104 ast_add,
105 ast_sub,
106 ast_mul,
107 ast_div,
108 ast_mod,
109 ast_lshift,
110 ast_rshift,
111 ast_less,
112 ast_greater,
113 ast_lequal,
114 ast_gequal,
115 ast_equal,
116 ast_nequal,
117 ast_bit_and,
118 ast_bit_xor,
119 ast_bit_or,
120 ast_bit_not,
121 ast_logic_and,
122 ast_logic_xor,
123 ast_logic_or,
124 ast_logic_not,
125
126 ast_mul_assign,
127 ast_div_assign,
128 ast_mod_assign,
129 ast_add_assign,
130 ast_sub_assign,
131 ast_ls_assign,
132 ast_rs_assign,
133 ast_and_assign,
134 ast_xor_assign,
135 ast_or_assign,
136
137 ast_conditional,
138
139 ast_pre_inc,
140 ast_pre_dec,
141 ast_post_inc,
142 ast_post_dec,
143 ast_field_selection,
144 ast_array_index,
145
146 ast_function_call,
147
148 ast_identifier,
149 ast_int_constant,
150 ast_uint_constant,
151 ast_float_constant,
152 ast_bool_constant,
153
154 ast_sequence
155 };
156
157 class ast_expression : public ast_node {
158 public:
159 ast_expression(int oper, ast_expression *,
160 ast_expression *, ast_expression *);
161
162 virtual void print(void) const;
163
164 enum ast_operators oper;
165
166 ast_expression *subexpressions[3];
167
168 union {
169 char *identifier;
170 int int_constant;
171 float float_constant;
172 unsigned uint_constant;
173 int bool_constant;
174 } primary_expression;
175
176
177 /**
178 * List of expressions for an \c ast_sequence.
179 */
180 struct simple_node expressions;
181 };
182
183 /**
184 * Number of possible operators for an ast_expression
185 *
186 * This is done as a define instead of as an additional value in the enum so
187 * that the compiler won't generate spurious messages like "warning:
188 * enumeration value ‘ast_num_operators’ not handled in switch"
189 */
190 #define AST_NUM_OPERATORS (ast_sequence + 1)
191
192
193 class ast_compound_statement : public ast_node {
194 public:
195 ast_compound_statement(int new_scope, ast_node *statements);
196 virtual void print(void) const;
197
198 int new_scope;
199 struct simple_node statements;
200 };
201
202 class ast_declaration : public ast_node {
203 public:
204 ast_declaration(char *identifier, int is_array, ast_expression *array_size,
205 ast_expression *initializer);
206 virtual void print(void) const;
207
208 char *identifier;
209
210 int is_array;
211 ast_expression *array_size;
212
213 ast_expression *initializer;
214 };
215
216
217 enum {
218 ast_precision_high = 0, /**< Default precision. */
219 ast_precision_medium,
220 ast_precision_low
221 };
222
223 struct ast_type_qualifier {
224 unsigned invariant:1;
225 unsigned constant:1;
226 unsigned attribute:1;
227 unsigned varying:1;
228 unsigned in:1;
229 unsigned out:1;
230 unsigned centroid:1;
231 unsigned uniform:1;
232 unsigned smooth:1;
233 unsigned flat:1;
234 unsigned noperspective:1;
235 };
236
237 class ast_struct_specifier : public ast_node {
238 public:
239 ast_struct_specifier(char *identifier, ast_node *declarator_list);
240 virtual void print(void) const;
241
242 char *name;
243 struct simple_node declarations;
244 };
245
246
247 enum ast_types {
248 ast_void,
249 ast_float,
250 ast_int,
251 ast_uint,
252 ast_bool,
253 ast_vec2,
254 ast_vec3,
255 ast_vec4,
256 ast_bvec2,
257 ast_bvec3,
258 ast_bvec4,
259 ast_ivec2,
260 ast_ivec3,
261 ast_ivec4,
262 ast_uvec2,
263 ast_uvec3,
264 ast_uvec4,
265 ast_mat2,
266 ast_mat2x3,
267 ast_mat2x4,
268 ast_mat3x2,
269 ast_mat3,
270 ast_mat3x4,
271 ast_mat4x2,
272 ast_mat4x3,
273 ast_mat4,
274 ast_sampler1d,
275 ast_sampler2d,
276 ast_sampler3d,
277 ast_samplercube,
278 ast_sampler1dshadow,
279 ast_sampler2dshadow,
280 ast_samplercubeshadow,
281 ast_sampler1darray,
282 ast_sampler2darray,
283 ast_sampler1darrayshadow,
284 ast_sampler2darrayshadow,
285 ast_isampler1d,
286 ast_isampler2d,
287 ast_isampler3d,
288 ast_isamplercube,
289 ast_isampler1darray,
290 ast_isampler2darray,
291 ast_usampler1d,
292 ast_usampler2d,
293 ast_usampler3d,
294 ast_usamplercube,
295 ast_usampler1darray,
296 ast_usampler2darray,
297
298 ast_struct,
299 ast_type_name
300 };
301
302
303 class ast_type_specifier : public ast_node {
304 public:
305 ast_type_specifier(int specifier);
306
307 virtual void print(void) const;
308
309 enum ast_types type_specifier;
310
311 char *type_name;
312 ast_struct_specifier *structure;
313
314 int is_array;
315 ast_expression *array_size;
316
317 unsigned precision:2;
318 };
319
320
321 class ast_fully_specified_type : public ast_node {
322 public:
323 virtual void print(void) const;
324
325 ast_type_qualifier qualifier;
326 ast_type_specifier *specifier;
327 };
328
329
330 class ast_declarator_list : public ast_node {
331 public:
332 ast_declarator_list(ast_fully_specified_type *);
333 virtual void print(void) const;
334
335 ast_fully_specified_type *type;
336 struct simple_node declarations;
337
338 /**
339 * Special flag for vertex shader "invariant" declarations.
340 *
341 * Vertex shaders can contain "invariant" variable redeclarations that do
342 * not include a type. For example, "invariant gl_Position;". This flag
343 * is used to note these cases when no type is specified.
344 */
345 int invariant;
346 };
347
348
349 class ast_parameter_declarator : public ast_node {
350 public:
351 virtual void print(void) const;
352
353 ast_fully_specified_type *type;
354 char *identifier;
355 int is_array;
356 ast_expression *array_size;
357 };
358
359
360 class ast_function : public ast_node {
361 public:
362 ast_function(void);
363
364 virtual void print(void) const;
365
366 ast_fully_specified_type *return_type;
367 char *identifier;
368
369 struct simple_node parameters;
370 };
371
372
373 class ast_declaration_statement : public ast_node {
374 public:
375 ast_declaration_statement(void);
376
377 enum {
378 ast_function,
379 ast_declaration,
380 ast_precision
381 } mode;
382
383 union {
384 class ast_function *function;
385 ast_declarator_list *declarator;
386 ast_type_specifier *type;
387 ast_node *node;
388 } declaration;
389 };
390
391
392 class ast_expression_statement : public ast_node {
393 public:
394 ast_expression_statement(ast_expression *);
395 virtual void print(void) const;
396
397 ast_expression *expression;
398 };
399
400
401 class ast_case_label : public ast_node {
402 public:
403
404 /**
405 * An expression of NULL means 'default'.
406 */
407 ast_expression *expression;
408 };
409
410 class ast_selection_statement : public ast_node {
411 public:
412 ast_selection_statement(ast_expression *condition,
413 ast_node *then_statement,
414 ast_node *else_statement);
415 virtual void print(void) const;
416
417 ast_expression *condition;
418 ast_node *then_statement;
419 ast_node *else_statement;
420 };
421
422
423 class ast_switch_statement : public ast_node {
424 public:
425 ast_expression *expression;
426 struct simple_node statements;
427 };
428
429 class ast_iteration_statement : public ast_node {
430 public:
431 ast_iteration_statement(int mode, ast_node *init, ast_node *condition,
432 ast_expression *rest_expression, ast_node *body);
433
434 virtual void print(void) const;
435
436 enum ast_iteration_modes {
437 ast_for,
438 ast_while,
439 ast_do_while
440 } mode;
441
442
443 ast_node *init_statement;
444 ast_node *condition;
445 ast_expression *rest_expression;
446
447 ast_node *body;
448 };
449
450
451 class ast_jump_statement : public ast_node {
452 public:
453 ast_jump_statement(int mode, ast_expression *return_value);
454 virtual void print(void) const;
455
456 enum ast_jump_modes {
457 ast_continue,
458 ast_break,
459 ast_return,
460 ast_discard
461 } mode;
462
463 ast_expression *opt_return_value;
464 };
465
466
467 class ast_function_definition : public ast_node {
468 public:
469 virtual void print(void) const;
470
471 ast_function *prototype;
472 ast_compound_statement *body;
473 };
474
475
476 extern struct ir_instruction *
477 ast_expression_to_hir(const ast_node *ast,
478 struct simple_node *instructions,
479 struct _mesa_glsl_parse_state *state);
480
481 extern struct ir_instruction *
482 ast_expression_statement_to_hir(const struct ast_node *ast,
483 struct simple_node *instructions,
484 struct _mesa_glsl_parse_state *state);
485
486 extern struct ir_instruction *
487 ast_compound_statement_to_hir(const struct ast_node *ast,
488 struct simple_node *instructions,
489 struct _mesa_glsl_parse_state *state);
490
491 extern struct ir_instruction *
492 ast_function_definition_to_hir(const struct ast_node *ast,
493 struct simple_node *instructions,
494 struct _mesa_glsl_parse_state *state);
495
496 extern struct ir_instruction *
497 ast_declarator_list_to_hir(const struct ast_node *ast,
498 struct simple_node *instructions,
499 struct _mesa_glsl_parse_state *state);
500
501 extern struct ir_instruction *
502 ast_parameter_declarator_to_hir(const struct ast_node *ast,
503 struct simple_node *instructions,
504 struct _mesa_glsl_parse_state *state);
505
506 extern struct ir_instruction *
507 _mesa_ast_field_selection_to_hir(const struct ast_expression *expr,
508 struct simple_node *instructions,
509 struct _mesa_glsl_parse_state *state);
510
511 #endif /* AST_H */