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