ast.h revision b2c0df2b60a77b043d461f265c85d8b5b066a008
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 "list.h"
30#include "glsl_parser_extras.h"
31
32struct _mesa_glsl_parse_state;
33
34struct YYLTYPE;
35
36/**
37 * \defgroup AST Abstract syntax tree node definitions
38 *
39 * An abstract syntax tree is generated by the parser.  This is a fairly
40 * direct representation of the gramma derivation for the source program.
41 * No symantic checking is done during the generation of the AST.  Only
42 * syntactic checking is done.  Symantic checking is performed by a later
43 * stage that converts the AST to a more generic intermediate representation.
44 *
45 *@{
46 */
47/**
48 * Base class of all abstract syntax tree nodes
49 */
50class ast_node {
51public:
52   /* Callers of this ralloc-based new need not call delete. It's
53    * easier to just ralloc_free 'ctx' (or any of its ancestors). */
54   static void* operator new(size_t size, void *ctx)
55   {
56      void *node;
57
58      node = rzalloc_size(ctx, size);
59      assert(node != NULL);
60
61      return node;
62   }
63
64   /* If the user *does* call delete, that's OK, we will just
65    * ralloc_free in that case. */
66   static void operator delete(void *table)
67   {
68      ralloc_free(table);
69   }
70
71   /**
72    * Print an AST node in something approximating the original GLSL code
73    */
74   virtual void print(void) const;
75
76   /**
77    * Convert the AST node to the high-level intermediate representation
78    */
79   virtual ir_rvalue *hir(exec_list *instructions,
80			  struct _mesa_glsl_parse_state *state);
81
82   /**
83    * Retrieve the source location of an AST node
84    *
85    * This function is primarily used to get the source position of an AST node
86    * into a form that can be passed to \c _mesa_glsl_error.
87    *
88    * \sa _mesa_glsl_error, ast_node::set_location
89    */
90   struct YYLTYPE get_location(void) const
91   {
92      struct YYLTYPE locp;
93
94      locp.source = this->location.source;
95      locp.first_line = this->location.line;
96      locp.first_column = this->location.column;
97      locp.last_line = locp.first_line;
98      locp.last_column = locp.first_column;
99
100      return locp;
101   }
102
103   /**
104    * Set the source location of an AST node from a parser location
105    *
106    * \sa ast_node::get_location
107    */
108   void set_location(const struct YYLTYPE &locp)
109   {
110      this->location.source = locp.source;
111      this->location.line = locp.first_line;
112      this->location.column = locp.first_column;
113   }
114
115   /**
116    * Source location of the AST node.
117    */
118   struct {
119      unsigned source;    /**< GLSL source number. */
120      unsigned line;      /**< Line number within the source string. */
121      unsigned column;    /**< Column in the line. */
122   } location;
123
124   exec_node link;
125
126protected:
127   /**
128    * The only constructor is protected so that only derived class objects can
129    * be created.
130    */
131   ast_node(void);
132};
133
134
135/**
136 * Operators for AST expression nodes.
137 */
138enum ast_operators {
139   ast_assign,
140   ast_plus,        /**< Unary + operator. */
141   ast_neg,
142   ast_add,
143   ast_sub,
144   ast_mul,
145   ast_div,
146   ast_mod,
147   ast_lshift,
148   ast_rshift,
149   ast_less,
150   ast_greater,
151   ast_lequal,
152   ast_gequal,
153   ast_equal,
154   ast_nequal,
155   ast_bit_and,
156   ast_bit_xor,
157   ast_bit_or,
158   ast_bit_not,
159   ast_logic_and,
160   ast_logic_xor,
161   ast_logic_or,
162   ast_logic_not,
163
164   ast_mul_assign,
165   ast_div_assign,
166   ast_mod_assign,
167   ast_add_assign,
168   ast_sub_assign,
169   ast_ls_assign,
170   ast_rs_assign,
171   ast_and_assign,
172   ast_xor_assign,
173   ast_or_assign,
174
175   ast_conditional,
176
177   ast_pre_inc,
178   ast_pre_dec,
179   ast_post_inc,
180   ast_post_dec,
181   ast_field_selection,
182   ast_array_index,
183
184   ast_function_call,
185
186   ast_identifier,
187   ast_int_constant,
188   ast_uint_constant,
189   ast_float_constant,
190   ast_bool_constant,
191
192   ast_sequence
193};
194
195/**
196 * Representation of any sort of expression.
197 */
198class ast_expression : public ast_node {
199public:
200   ast_expression(int oper, ast_expression *,
201		  ast_expression *, ast_expression *);
202
203   ast_expression(const char *identifier) :
204      oper(ast_identifier)
205   {
206      subexpressions[0] = NULL;
207      subexpressions[1] = NULL;
208      subexpressions[2] = NULL;
209      primary_expression.identifier = identifier;
210      this->non_lvalue_description = NULL;
211   }
212
213   static const char *operator_string(enum ast_operators op);
214
215   virtual ir_rvalue *hir(exec_list *instructions,
216			  struct _mesa_glsl_parse_state *state);
217
218   virtual void print(void) const;
219
220   enum ast_operators oper;
221
222   ast_expression *subexpressions[3];
223
224   union {
225      const char *identifier;
226      int int_constant;
227      float float_constant;
228      unsigned uint_constant;
229      int bool_constant;
230   } primary_expression;
231
232
233   /**
234    * List of expressions for an \c ast_sequence or parameters for an
235    * \c ast_function_call
236    */
237   exec_list expressions;
238
239   /**
240    * For things that can't be l-values, this describes what it is.
241    *
242    * This text is used by the code that generates IR for assignments to
243    * detect and emit useful messages for assignments to some things that
244    * can't be l-values.  For example, pre- or post-incerement expressions.
245    *
246    * \note
247    * This pointer may be \c NULL.
248    */
249   const char *non_lvalue_description;
250};
251
252class ast_expression_bin : public ast_expression {
253public:
254   ast_expression_bin(int oper, ast_expression *, ast_expression *);
255
256   virtual void print(void) const;
257};
258
259/**
260 * Subclass of expressions for function calls
261 */
262class ast_function_expression : public ast_expression {
263public:
264   ast_function_expression(ast_expression *callee)
265      : ast_expression(ast_function_call, callee,
266		       NULL, NULL),
267	cons(false)
268   {
269      /* empty */
270   }
271
272   ast_function_expression(class ast_type_specifier *type)
273      : ast_expression(ast_function_call, (ast_expression *) type,
274		       NULL, NULL),
275	cons(true)
276   {
277      /* empty */
278   }
279
280   bool is_constructor() const
281   {
282      return cons;
283   }
284
285   virtual ir_rvalue *hir(exec_list *instructions,
286			  struct _mesa_glsl_parse_state *state);
287
288private:
289   /**
290    * Is this function call actually a constructor?
291    */
292   bool cons;
293};
294
295
296/**
297 * Number of possible operators for an ast_expression
298 *
299 * This is done as a define instead of as an additional value in the enum so
300 * that the compiler won't generate spurious messages like "warning:
301 * enumeration value ‘ast_num_operators’ not handled in switch"
302 */
303#define AST_NUM_OPERATORS (ast_sequence + 1)
304
305
306class ast_compound_statement : public ast_node {
307public:
308   ast_compound_statement(int new_scope, ast_node *statements);
309   virtual void print(void) const;
310
311   virtual ir_rvalue *hir(exec_list *instructions,
312			  struct _mesa_glsl_parse_state *state);
313
314   int new_scope;
315   exec_list statements;
316};
317
318class ast_declaration : public ast_node {
319public:
320   ast_declaration(const char *identifier, int is_array, ast_expression *array_size,
321		   ast_expression *initializer);
322   virtual void print(void) const;
323
324   const char *identifier;
325
326   int is_array;
327   ast_expression *array_size;
328
329   ast_expression *initializer;
330};
331
332
333enum {
334   ast_precision_none = 0, /**< Absence of precision qualifier. */
335   ast_precision_high,
336   ast_precision_medium,
337   ast_precision_low
338};
339
340struct ast_type_qualifier {
341   union {
342      struct {
343	 unsigned invariant:1;
344	 unsigned constant:1;
345	 unsigned attribute:1;
346	 unsigned varying:1;
347	 unsigned in:1;
348	 unsigned out:1;
349	 unsigned centroid:1;
350	 unsigned uniform:1;
351	 unsigned smooth:1;
352	 unsigned flat:1;
353	 unsigned noperspective:1;
354
355	 /** \name Layout qualifiers for GL_ARB_fragment_coord_conventions */
356	 /*@{*/
357	 unsigned origin_upper_left:1;
358	 unsigned pixel_center_integer:1;
359	 /*@}*/
360
361	 /**
362	  * Flag set if GL_ARB_explicit_attrib_location "location" layout
363	  * qualifier is used.
364	  */
365	 unsigned explicit_location:1;
366
367         /** \name Layout qualifiers for GL_AMD_conservative_depth */
368         /** \{ */
369         unsigned depth_any:1;
370         unsigned depth_greater:1;
371         unsigned depth_less:1;
372         unsigned depth_unchanged:1;
373         /** \} */
374      }
375      /** \brief Set of flags, accessed by name. */
376      q;
377
378      /** \brief Set of flags, accessed as a bitmask. */
379      unsigned i;
380   } flags;
381
382   /**
383    * Location specified via GL_ARB_explicit_attrib_location layout
384    *
385    * \note
386    * This field is only valid if \c explicit_location is set.
387    */
388   int location;
389
390   /**
391    * Return true if and only if an interpolation qualifier is present.
392    */
393   bool has_interpolation() const;
394
395   /**
396    * \brief Return string representation of interpolation qualifier.
397    *
398    * If an interpolation qualifier is present, then return that qualifier's
399    * string representation. Otherwise, return null. For example, if the
400    * noperspective bit is set, then this returns "noperspective".
401    *
402    * If multiple interpolation qualifiers are somehow present, then the
403    * returned string is undefined but not null.
404    */
405   const char *interpolation_string() const;
406};
407
408class ast_struct_specifier : public ast_node {
409public:
410   ast_struct_specifier(const char *identifier, ast_node *declarator_list);
411   virtual void print(void) const;
412
413   virtual ir_rvalue *hir(exec_list *instructions,
414			  struct _mesa_glsl_parse_state *state);
415
416   const char *name;
417   exec_list declarations;
418};
419
420
421enum ast_types {
422   ast_void,
423   ast_float,
424   ast_int,
425   ast_uint,
426   ast_bool,
427   ast_vec2,
428   ast_vec3,
429   ast_vec4,
430   ast_bvec2,
431   ast_bvec3,
432   ast_bvec4,
433   ast_ivec2,
434   ast_ivec3,
435   ast_ivec4,
436   ast_uvec2,
437   ast_uvec3,
438   ast_uvec4,
439   ast_mat2,
440   ast_mat2x3,
441   ast_mat2x4,
442   ast_mat3x2,
443   ast_mat3,
444   ast_mat3x4,
445   ast_mat4x2,
446   ast_mat4x3,
447   ast_mat4,
448   ast_sampler1d,
449   ast_sampler2d,
450   ast_sampler2drect,
451   ast_sampler3d,
452   ast_samplercube,
453   ast_samplerexternaloes,
454   ast_sampler1dshadow,
455   ast_sampler2dshadow,
456   ast_sampler2drectshadow,
457   ast_samplercubeshadow,
458   ast_sampler1darray,
459   ast_sampler2darray,
460   ast_sampler1darrayshadow,
461   ast_sampler2darrayshadow,
462   ast_isampler1d,
463   ast_isampler2d,
464   ast_isampler3d,
465   ast_isamplercube,
466   ast_isampler1darray,
467   ast_isampler2darray,
468   ast_usampler1d,
469   ast_usampler2d,
470   ast_usampler3d,
471   ast_usamplercube,
472   ast_usampler1darray,
473   ast_usampler2darray,
474
475   ast_struct,
476   ast_type_name
477};
478
479
480class ast_type_specifier : public ast_node {
481public:
482   ast_type_specifier(int specifier);
483
484   /** Construct a type specifier from a type name */
485   ast_type_specifier(const char *name)
486      : type_specifier(ast_type_name), type_name(name), structure(NULL),
487	is_array(false), array_size(NULL), precision(ast_precision_none),
488	is_precision_statement(false)
489   {
490      /* empty */
491   }
492
493   /** Construct a type specifier from a structure definition */
494   ast_type_specifier(ast_struct_specifier *s)
495      : type_specifier(ast_struct), type_name(s->name), structure(s),
496	is_array(false), array_size(NULL), precision(ast_precision_none),
497	is_precision_statement(false)
498   {
499      /* empty */
500   }
501
502   const struct glsl_type *glsl_type(const char **name,
503				     struct _mesa_glsl_parse_state *state)
504      const;
505
506   virtual void print(void) const;
507
508   ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
509
510   enum ast_types type_specifier;
511
512   const char *type_name;
513   ast_struct_specifier *structure;
514
515   int is_array;
516   ast_expression *array_size;
517
518   unsigned precision:2;
519
520   bool is_precision_statement;
521};
522
523
524class ast_fully_specified_type : public ast_node {
525public:
526   virtual void print(void) const;
527   bool has_qualifiers() const;
528
529   ast_type_qualifier qualifier;
530   ast_type_specifier *specifier;
531};
532
533
534class ast_declarator_list : public ast_node {
535public:
536   ast_declarator_list(ast_fully_specified_type *);
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_fully_specified_type *type;
543   exec_list declarations;
544
545   /**
546    * Special flag for vertex shader "invariant" declarations.
547    *
548    * Vertex shaders can contain "invariant" variable redeclarations that do
549    * not include a type.  For example, "invariant gl_Position;".  This flag
550    * is used to note these cases when no type is specified.
551    */
552   int invariant;
553};
554
555
556class ast_parameter_declarator : public ast_node {
557public:
558   ast_parameter_declarator()
559   {
560      this->identifier = NULL;
561      this->is_array = false;
562      this->array_size = 0;
563   }
564
565   virtual void print(void) const;
566
567   virtual ir_rvalue *hir(exec_list *instructions,
568			  struct _mesa_glsl_parse_state *state);
569
570   ast_fully_specified_type *type;
571   const char *identifier;
572   int is_array;
573   ast_expression *array_size;
574
575   static void parameters_to_hir(exec_list *ast_parameters,
576				 bool formal, exec_list *ir_parameters,
577				 struct _mesa_glsl_parse_state *state);
578
579private:
580   /** Is this parameter declaration part of a formal parameter list? */
581   bool formal_parameter;
582
583   /**
584    * Is this parameter 'void' type?
585    *
586    * This field is set by \c ::hir.
587    */
588   bool is_void;
589};
590
591
592class ast_function : public ast_node {
593public:
594   ast_function(void);
595
596   virtual void print(void) const;
597
598   virtual ir_rvalue *hir(exec_list *instructions,
599			  struct _mesa_glsl_parse_state *state);
600
601   ast_fully_specified_type *return_type;
602   const char *identifier;
603
604   exec_list parameters;
605
606private:
607   /**
608    * Is this prototype part of the function definition?
609    *
610    * Used by ast_function_definition::hir to process the parameters, etc.
611    * of the function.
612    *
613    * \sa ::hir
614    */
615   bool is_definition;
616
617   /**
618    * Function signature corresponding to this function prototype instance
619    *
620    * Used by ast_function_definition::hir to process the parameters, etc.
621    * of the function.
622    *
623    * \sa ::hir
624    */
625   class ir_function_signature *signature;
626
627   friend class ast_function_definition;
628};
629
630
631class ast_expression_statement : public ast_node {
632public:
633   ast_expression_statement(ast_expression *);
634   virtual void print(void) const;
635
636   virtual ir_rvalue *hir(exec_list *instructions,
637			  struct _mesa_glsl_parse_state *state);
638
639   ast_expression *expression;
640};
641
642
643class ast_case_label : public ast_node {
644public:
645   ast_case_label(ast_expression *test_value);
646   virtual void print(void) const;
647
648   virtual ir_rvalue *hir(exec_list *instructions,
649			  struct _mesa_glsl_parse_state *state);
650
651   /**
652    * An test value of NULL means 'default'.
653    */
654   ast_expression *test_value;
655};
656
657
658class ast_case_label_list : public ast_node {
659public:
660   ast_case_label_list(void);
661   virtual void print(void) const;
662
663   virtual ir_rvalue *hir(exec_list *instructions,
664			  struct _mesa_glsl_parse_state *state);
665
666   /**
667    * A list of case labels.
668    */
669   exec_list labels;
670};
671
672
673class ast_case_statement : public ast_node {
674public:
675   ast_case_statement(ast_case_label_list *labels);
676   virtual void print(void) const;
677
678   virtual ir_rvalue *hir(exec_list *instructions,
679			  struct _mesa_glsl_parse_state *state);
680
681   ast_case_label_list *labels;
682
683   /**
684    * A list of statements.
685    */
686   exec_list stmts;
687};
688
689
690class ast_case_statement_list : public ast_node {
691public:
692   ast_case_statement_list(void);
693   virtual void print(void) const;
694
695   virtual ir_rvalue *hir(exec_list *instructions,
696			  struct _mesa_glsl_parse_state *state);
697
698   /**
699    * A list of cases.
700    */
701   exec_list cases;
702};
703
704
705class ast_switch_body : public ast_node {
706public:
707   ast_switch_body(ast_case_statement_list *stmts);
708   virtual void print(void) const;
709
710   virtual ir_rvalue *hir(exec_list *instructions,
711			  struct _mesa_glsl_parse_state *state);
712
713   ast_case_statement_list *stmts;
714};
715
716
717class ast_selection_statement : public ast_node {
718public:
719   ast_selection_statement(ast_expression *condition,
720			   ast_node *then_statement,
721			   ast_node *else_statement);
722   virtual void print(void) const;
723
724   virtual ir_rvalue *hir(exec_list *instructions,
725			  struct _mesa_glsl_parse_state *state);
726
727   ast_expression *condition;
728   ast_node *then_statement;
729   ast_node *else_statement;
730};
731
732
733class ast_switch_statement : public ast_node {
734public:
735   ast_switch_statement(ast_expression *test_expression,
736			ast_node *body);
737   virtual void print(void) const;
738
739   virtual ir_rvalue *hir(exec_list *instructions,
740			  struct _mesa_glsl_parse_state *state);
741
742   ast_expression *test_expression;
743   ast_node *body;
744
745protected:
746   void test_to_hir(exec_list *, struct _mesa_glsl_parse_state *);
747};
748
749class ast_iteration_statement : public ast_node {
750public:
751   ast_iteration_statement(int mode, ast_node *init, ast_node *condition,
752			   ast_expression *rest_expression, ast_node *body);
753
754   virtual void print(void) const;
755
756   virtual ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
757
758   enum ast_iteration_modes {
759      ast_for,
760      ast_while,
761      ast_do_while
762   } mode;
763
764
765   ast_node *init_statement;
766   ast_node *condition;
767   ast_expression *rest_expression;
768
769   ast_node *body;
770
771private:
772   /**
773    * Generate IR from the condition of a loop
774    *
775    * This is factored out of ::hir because some loops have the condition
776    * test at the top (for and while), and others have it at the end (do-while).
777    */
778   void condition_to_hir(class ir_loop *, struct _mesa_glsl_parse_state *);
779};
780
781
782class ast_jump_statement : public ast_node {
783public:
784   ast_jump_statement(int mode, ast_expression *return_value);
785   virtual void print(void) const;
786
787   virtual ir_rvalue *hir(exec_list *instructions,
788			  struct _mesa_glsl_parse_state *state);
789
790   enum ast_jump_modes {
791      ast_continue,
792      ast_break,
793      ast_return,
794      ast_discard
795   } mode;
796
797   ast_expression *opt_return_value;
798};
799
800
801class ast_function_definition : public ast_node {
802public:
803   virtual void print(void) const;
804
805   virtual ir_rvalue *hir(exec_list *instructions,
806			  struct _mesa_glsl_parse_state *state);
807
808   ast_function *prototype;
809   ast_compound_statement *body;
810};
811/*@}*/
812
813extern void
814_mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state);
815
816extern ir_rvalue *
817_mesa_ast_field_selection_to_hir(const ast_expression *expr,
818				 exec_list *instructions,
819				 struct _mesa_glsl_parse_state *state);
820
821void
822emit_function(_mesa_glsl_parse_state *state, ir_function *f);
823
824#endif /* AST_H */
825