glsl: Optimize -(-expr) into expr.
[mesa.git] / src / glsl / ast.h
index aa769da3f6579fb9bd35e6a13e44c0026d96282a..97905c6a6d1dde2a463d898283e858b4efb29603 100644 (file)
 #include "list.h"
 #include "glsl_parser_extras.h"
 
-struct ir_instruction;
 struct _mesa_glsl_parse_state;
 
 struct YYLTYPE;
 
+/**
+ * \defgroup AST Abstract syntax tree node definitions
+ *
+ * An abstract syntax tree is generated by the parser.  This is a fairly
+ * direct representation of the gramma derivation for the source program.
+ * No symantic checking is done during the generation of the AST.  Only
+ * syntactic checking is done.  Symantic checking is performed by a later
+ * stage that converts the AST to a more generic intermediate representation.
+ *
+ *@{
+ */
+/**
+ * Base class of all abstract syntax tree nodes
+ */
 class ast_node {
 public:
-   /* Callers of this talloc-based new need not call delete. It's
-    * easier to just talloc_free 'ctx' (or any of its ancestors). */
-   static void* operator new(size_t size, void *ctx)
-   {
-      void *node;
-
-      node = talloc_size(ctx, size);
-      assert(node != NULL);
-
-      return node;
-   }
-
-   /* If the user *does* call delete, that's OK, we will just
-    * talloc_free in that case. */
-   static void operator delete(void *table)
-   {
-      talloc_free(table);
-   }
+   DECLARE_RALLOC_CXX_OPERATORS(ast_node);
 
+   /**
+    * Print an AST node in something approximating the original GLSL code
+    */
    virtual void print(void) const;
+
+   /**
+    * Convert the AST node to the high-level intermediate representation
+    */
    virtual ir_rvalue *hir(exec_list *instructions,
                          struct _mesa_glsl_parse_state *state);
 
@@ -92,19 +95,29 @@ public:
       this->location.column = locp.first_column;
    }
 
+   /**
+    * Source location of the AST node.
+    */
    struct {
-      unsigned source;
-      unsigned line;
-      unsigned column;
+      unsigned source;    /**< GLSL source number. */
+      unsigned line;      /**< Line number within the source string. */
+      unsigned column;    /**< Column in the line. */
    } location;
 
    exec_node link;
 
 protected:
+   /**
+    * The only constructor is protected so that only derived class objects can
+    * be created.
+    */
    ast_node(void);
 };
 
 
+/**
+ * Operators for AST expression nodes.
+ */
 enum ast_operators {
    ast_assign,
    ast_plus,        /**< Unary + operator. */
@@ -159,9 +172,13 @@ enum ast_operators {
    ast_float_constant,
    ast_bool_constant,
 
-   ast_sequence
+   ast_sequence,
+   ast_aggregate
 };
 
+/**
+ * Representation of any sort of expression.
+ */
 class ast_expression : public ast_node {
 public:
    ast_expression(int oper, ast_expression *,
@@ -173,7 +190,8 @@ public:
       subexpressions[0] = NULL;
       subexpressions[1] = NULL;
       subexpressions[2] = NULL;
-      primary_expression.identifier = (char *) identifier;
+      primary_expression.identifier = identifier;
+      this->non_lvalue_description = NULL;
    }
 
    static const char *operator_string(enum ast_operators op);
@@ -188,7 +206,7 @@ public:
    ast_expression *subexpressions[3];
 
    union {
-      char *identifier;
+      const char *identifier;
       int int_constant;
       float float_constant;
       unsigned uint_constant;
@@ -201,6 +219,18 @@ public:
     * \c ast_function_call
     */
    exec_list expressions;
+
+   /**
+    * For things that can't be l-values, this describes what it is.
+    *
+    * This text is used by the code that generates IR for assignments to
+    * detect and emit useful messages for assignments to some things that
+    * can't be l-values.  For example, pre- or post-incerement expressions.
+    *
+    * \note
+    * This pointer may be \c NULL.
+    */
+   const char *non_lvalue_description;
 };
 
 class ast_expression_bin : public ast_expression {
@@ -246,6 +276,30 @@ private:
    bool cons;
 };
 
+/**
+ * C-style aggregate initialization class
+ *
+ * Represents C-style initializers of vectors, matrices, arrays, and
+ * structures. E.g., vec3 pos = {1.0, 0.0, -1.0} is equivalent to
+ * vec3 pos = vec3(1.0, 0.0, -1.0).
+ *
+ * Specified in GLSL 4.20 and GL_ARB_shading_language_420pack.
+ *
+ * \sa _mesa_ast_set_aggregate_type
+ */
+class ast_aggregate_initializer : public ast_expression {
+public:
+   ast_aggregate_initializer()
+      : ast_expression(ast_aggregate, NULL, NULL, NULL),
+        constructor_type(NULL)
+   {
+      /* empty */
+   }
+
+   ast_type_specifier *constructor_type;
+   virtual ir_rvalue *hir(exec_list *instructions,
+                          struct _mesa_glsl_parse_state *state);
+};
 
 /**
  * Number of possible operators for an ast_expression
@@ -271,13 +325,13 @@ public:
 
 class ast_declaration : public ast_node {
 public:
-   ast_declaration(char *identifier, int is_array, ast_expression *array_size,
+   ast_declaration(const char *identifier, bool is_array, ast_expression *array_size,
                   ast_expression *initializer);
    virtual void print(void) const;
 
-   char *identifier;
+   const char *identifier;
    
-   int is_array;
+   bool is_array;
    ast_expression *array_size;
 
    ast_expression *initializer;
@@ -285,118 +339,215 @@ public:
 
 
 enum {
-   ast_precision_high = 0, /**< Default precision. */
+   ast_precision_none = 0, /**< Absence of precision qualifier. */
+   ast_precision_high,
    ast_precision_medium,
    ast_precision_low
 };
 
 struct ast_type_qualifier {
-   unsigned invariant:1;
-   unsigned constant:1;
-   unsigned attribute:1;
-   unsigned varying:1;
-   unsigned in:1;
-   unsigned out:1;
-   unsigned centroid:1;
-   unsigned uniform:1;
-   unsigned smooth:1;
-   unsigned flat:1;
-   unsigned noperspective:1;
-
-   /** \name Layout qualifiers for GL_ARB_fragment_coord_conventions */
-   /*@{*/
-   unsigned origin_upper_left:1;
-   unsigned pixel_center_integer:1;
-   /*@}*/
+   DECLARE_RALLOC_CXX_OPERATORS(ast_type_qualifier);
+
+   union {
+      struct {
+        unsigned invariant:1;
+        unsigned constant:1;
+        unsigned attribute:1;
+        unsigned varying:1;
+        unsigned in:1;
+        unsigned out:1;
+        unsigned centroid:1;
+        unsigned uniform:1;
+        unsigned smooth:1;
+        unsigned flat:1;
+        unsigned noperspective:1;
+
+        /** \name Layout qualifiers for GL_ARB_fragment_coord_conventions */
+        /*@{*/
+        unsigned origin_upper_left:1;
+        unsigned pixel_center_integer:1;
+        /*@}*/
+
+        /**
+         * Flag set if GL_ARB_explicit_attrib_location "location" layout
+         * qualifier is used.
+         */
+        unsigned explicit_location:1;
+        /**
+         * Flag set if GL_ARB_explicit_attrib_location "index" layout
+         * qualifier is used.
+         */
+        unsigned explicit_index:1;
+
+         /**
+          * Flag set if GL_ARB_shading_language_420pack "binding" layout
+          * qualifier is used.
+          */
+         unsigned explicit_binding:1;
+
+         /** \name Layout qualifiers for GL_AMD_conservative_depth */
+         /** \{ */
+         unsigned depth_any:1;
+         unsigned depth_greater:1;
+         unsigned depth_less:1;
+         unsigned depth_unchanged:1;
+         /** \} */
+
+        /** \name Layout qualifiers for GL_ARB_uniform_buffer_object */
+        /** \{ */
+         unsigned std140:1;
+         unsigned shared:1;
+         unsigned packed:1;
+         unsigned column_major:1;
+         unsigned row_major:1;
+        /** \} */
+
+        /** \name Layout qualifiers for GLSL 1.50 geometry shaders */
+        /** \{ */
+        unsigned prim_type:1;
+        unsigned max_vertices:1;
+        /** \} */
+      }
+      /** \brief Set of flags, accessed by name. */
+      q;
+
+      /** \brief Set of flags, accessed as a bitmask. */
+      unsigned i;
+   } flags;
+
+   /** Precision of the type (highp/medium/lowp). */
+   unsigned precision:2;
+
+   /**
+    * Location specified via GL_ARB_explicit_attrib_location layout
+    *
+    * \note
+    * This field is only valid if \c explicit_location is set.
+    */
+   int location;
+   /**
+    * Index specified via GL_ARB_explicit_attrib_location layout
+    *
+    * \note
+    * This field is only valid if \c explicit_index is set.
+    */
+   int index;
+
+   /** Maximum output vertices in GLSL 1.50 geometry shaders. */
+   int max_vertices;
+
+   /** Input or output primitive type in GLSL 1.50 geometry shaders */
+   GLenum prim_type;
+
+   /**
+    * Binding specified via GL_ARB_shading_language_420pack's "binding" keyword.
+    *
+    * \note
+    * This field is only valid if \c explicit_binding is set.
+    */
+   int binding;
+
+   /**
+    * Return true if and only if an interpolation qualifier is present.
+    */
+   bool has_interpolation() const;
+
+   /**
+    * Return whether a layout qualifier is present.
+    */
+   bool has_layout() const;
+
+   /**
+    * Return whether a storage qualifier is present.
+    */
+   bool has_storage() const;
+
+   /**
+    * Return whether an auxiliary storage qualifier is present.
+    */
+   bool has_auxiliary_storage() const;
+
+   /**
+    * \brief Return string representation of interpolation qualifier.
+    *
+    * If an interpolation qualifier is present, then return that qualifier's
+    * string representation. Otherwise, return null. For example, if the
+    * noperspective bit is set, then this returns "noperspective".
+    *
+    * If multiple interpolation qualifiers are somehow present, then the
+    * returned string is undefined but not null.
+    */
+   const char *interpolation_string() const;
+
+   bool merge_qualifier(YYLTYPE *loc,
+                       _mesa_glsl_parse_state *state,
+                       ast_type_qualifier q);
 };
 
+class ast_declarator_list;
+
 class ast_struct_specifier : public ast_node {
 public:
-   ast_struct_specifier(char *identifier, ast_node *declarator_list);
+   /**
+    * \brief Make a shallow copy of an ast_struct_specifier.
+    *
+    * Use only if the objects are allocated from the same context and will not
+    * be modified. Zeros the inherited ast_node's fields.
+    */
+   ast_struct_specifier(const ast_struct_specifier& that):
+      ast_node(), name(that.name), declarations(that.declarations),
+      is_declaration(that.is_declaration)
+   {
+      /* empty */
+   }
+
+   ast_struct_specifier(const char *identifier,
+                       ast_declarator_list *declarator_list);
    virtual void print(void) const;
 
    virtual ir_rvalue *hir(exec_list *instructions,
                          struct _mesa_glsl_parse_state *state);
 
-   char *name;
+   const char *name;
+   /* List of ast_declarator_list * */
    exec_list declarations;
+   bool is_declaration;
 };
 
 
-enum ast_types {
-   ast_void,
-   ast_float,
-   ast_int,
-   ast_uint,
-   ast_bool,
-   ast_vec2,
-   ast_vec3,
-   ast_vec4,
-   ast_bvec2,
-   ast_bvec3,
-   ast_bvec4,
-   ast_ivec2,
-   ast_ivec3,
-   ast_ivec4,
-   ast_uvec2,
-   ast_uvec3,
-   ast_uvec4,
-   ast_mat2,
-   ast_mat2x3,
-   ast_mat2x4,
-   ast_mat3x2,
-   ast_mat3,
-   ast_mat3x4,
-   ast_mat4x2,
-   ast_mat4x3,
-   ast_mat4,
-   ast_sampler1d,
-   ast_sampler2d,
-   ast_sampler2drect,
-   ast_sampler3d,
-   ast_samplercube,
-   ast_sampler1dshadow,
-   ast_sampler2dshadow,
-   ast_sampler2drectshadow,
-   ast_samplercubeshadow,
-   ast_sampler1darray,
-   ast_sampler2darray,
-   ast_sampler1darrayshadow,
-   ast_sampler2darrayshadow,
-   ast_isampler1d,
-   ast_isampler2d,
-   ast_isampler3d,
-   ast_isamplercube,
-   ast_isampler1darray,
-   ast_isampler2darray,
-   ast_usampler1d,
-   ast_usampler2d,
-   ast_usampler3d,
-   ast_usamplercube,
-   ast_usampler1darray,
-   ast_usampler2darray,
-
-   ast_struct,
-   ast_type_name
-};
-
 
 class ast_type_specifier : public ast_node {
 public:
-   ast_type_specifier(int specifier);
+   /**
+    * \brief Make a shallow copy of an ast_type_specifier, specifying array
+    *        fields.
+    *
+    * Use only if the objects are allocated from the same context and will not
+    * be modified. Zeros the inherited ast_node's fields.
+    */
+   ast_type_specifier(const ast_type_specifier *that, bool is_array,
+                      ast_expression *array_size)
+      : ast_node(), type_name(that->type_name), structure(that->structure),
+        is_array(is_array), array_size(array_size),
+        default_precision(that->default_precision)
+   {
+      /* empty */
+   }
 
    /** Construct a type specifier from a type name */
    ast_type_specifier(const char *name) 
-      : type_specifier(ast_type_name), type_name(name), structure(NULL),
-       is_array(false), array_size(NULL), precision(ast_precision_high)
+      : type_name(name), structure(NULL),
+       is_array(false), array_size(NULL),
+       default_precision(ast_precision_none)
    {
       /* empty */
    }
 
    /** Construct a type specifier from a structure definition */
    ast_type_specifier(ast_struct_specifier *s)
-      : type_specifier(ast_struct), type_name(s->name), structure(s),
-       is_array(false), array_size(NULL), precision(ast_precision_high)
+      : type_name(s->name), structure(s),
+       is_array(false), array_size(NULL),
+       default_precision(ast_precision_none)
    {
       /* empty */
    }
@@ -409,15 +560,14 @@ public:
 
    ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
 
-   enum ast_types type_specifier;
-
    const char *type_name;
    ast_struct_specifier *structure;
 
-   int is_array;
+   bool is_array;
    ast_expression *array_size;
 
-   unsigned precision:2;
+   /** For precision statements, this is the given precision; otherwise none. */
+   unsigned default_precision:2;
 };
 
 
@@ -426,6 +576,14 @@ public:
    virtual void print(void) const;
    bool has_qualifiers() const;
 
+   ast_fully_specified_type() : qualifier(), specifier(NULL)
+   {
+   }
+
+   const struct glsl_type *glsl_type(const char **name,
+                                    struct _mesa_glsl_parse_state *state)
+      const;
+
    ast_type_qualifier qualifier;
    ast_type_specifier *specifier;
 };
@@ -440,6 +598,7 @@ public:
                          struct _mesa_glsl_parse_state *state);
 
    ast_fully_specified_type *type;
+   /** List of 'ast_declaration *' */
    exec_list declarations;
 
    /**
@@ -455,10 +614,15 @@ public:
 
 class ast_parameter_declarator : public ast_node {
 public:
-   ast_parameter_declarator()
+   ast_parameter_declarator() :
+      type(NULL),
+      identifier(NULL),
+      is_array(false),
+      array_size(NULL),
+      formal_parameter(false),
+      is_void(false)
    {
-      this->is_array = false;
-      this->array_size = 0;
+      /* empty */
    }
 
    virtual void print(void) const;
@@ -467,8 +631,8 @@ public:
                          struct _mesa_glsl_parse_state *state);
 
    ast_fully_specified_type *type;
-   char *identifier;
-   int is_array;
+   const char *identifier;
+   bool is_array;
    ast_expression *array_size;
 
    static void parameters_to_hir(exec_list *ast_parameters,
@@ -498,7 +662,7 @@ public:
                          struct _mesa_glsl_parse_state *state);
 
    ast_fully_specified_type *return_type;
-   char *identifier;
+   const char *identifier;
 
    exec_list parameters;
 
@@ -527,46 +691,92 @@ private:
 };
 
 
-class ast_declaration_statement : public ast_node {
+class ast_expression_statement : public ast_node {
 public:
-   ast_declaration_statement(void);
+   ast_expression_statement(ast_expression *);
+   virtual void print(void) const;
 
-   enum {
-      ast_function,
-      ast_declaration,
-      ast_precision
-   } mode;
+   virtual ir_rvalue *hir(exec_list *instructions,
+                         struct _mesa_glsl_parse_state *state);
 
-   union {
-      class ast_function *function;
-      ast_declarator_list *declarator;
-      ast_type_specifier *type;
-      ast_node *node;
-   } declaration;
+   ast_expression *expression;
 };
 
 
-class ast_expression_statement : public ast_node {
+class ast_case_label : public ast_node {
 public:
-   ast_expression_statement(ast_expression *);
+   ast_case_label(ast_expression *test_value);
    virtual void print(void) const;
 
    virtual ir_rvalue *hir(exec_list *instructions,
                          struct _mesa_glsl_parse_state *state);
 
-   ast_expression *expression;
+   /**
+    * An test value of NULL means 'default'.
+    */
+   ast_expression *test_value;
 };
 
 
-class ast_case_label : public ast_node {
+class ast_case_label_list : public ast_node {
 public:
+   ast_case_label_list(void);
+   virtual void print(void) const;
+
+   virtual ir_rvalue *hir(exec_list *instructions,
+                         struct _mesa_glsl_parse_state *state);
 
    /**
-    * An expression of NULL means 'default'.
+    * A list of case labels.
     */
-   ast_expression *expression;
+   exec_list labels;
 };
 
+
+class ast_case_statement : public ast_node {
+public:
+   ast_case_statement(ast_case_label_list *labels);
+   virtual void print(void) const;
+
+   virtual ir_rvalue *hir(exec_list *instructions,
+                         struct _mesa_glsl_parse_state *state);
+
+   ast_case_label_list *labels;
+
+   /**
+    * A list of statements.
+    */
+   exec_list stmts;
+};
+
+
+class ast_case_statement_list : public ast_node {
+public:
+   ast_case_statement_list(void);
+   virtual void print(void) const;
+
+   virtual ir_rvalue *hir(exec_list *instructions,
+                         struct _mesa_glsl_parse_state *state);
+
+   /**
+    * A list of cases.
+    */
+   exec_list cases;
+};
+
+
+class ast_switch_body : public ast_node {
+public:
+   ast_switch_body(ast_case_statement_list *stmts);
+   virtual void print(void) const;
+
+   virtual ir_rvalue *hir(exec_list *instructions,
+                         struct _mesa_glsl_parse_state *state);
+
+   ast_case_statement_list *stmts;
+};
+
+
 class ast_selection_statement : public ast_node {
 public:
    ast_selection_statement(ast_expression *condition,
@@ -585,8 +795,18 @@ public:
 
 class ast_switch_statement : public ast_node {
 public:
-   ast_expression *expression;
-   exec_list statements;
+   ast_switch_statement(ast_expression *test_expression,
+                       ast_node *body);
+   virtual void print(void) const;
+
+   virtual ir_rvalue *hir(exec_list *instructions,
+                         struct _mesa_glsl_parse_state *state);
+
+   ast_expression *test_expression;
+   ast_node *body;
+
+protected:
+   void test_to_hir(exec_list *, struct _mesa_glsl_parse_state *);
 };
 
 class ast_iteration_statement : public ast_node {
@@ -643,6 +863,10 @@ public:
 
 class ast_function_definition : public ast_node {
 public:
+   ast_function_definition() : prototype(NULL), body(NULL)
+   {
+   }
+
    virtual void print(void) const;
 
    virtual ir_rvalue *hir(exec_list *instructions,
@@ -652,13 +876,101 @@ public:
    ast_compound_statement *body;
 };
 
+class ast_interface_block : public ast_node {
+public:
+   ast_interface_block(ast_type_qualifier layout,
+                       const char *instance_name,
+                       bool is_array,
+                       ast_expression *array_size)
+   : layout(layout), block_name(NULL), instance_name(instance_name),
+     is_array(is_array), array_size(array_size)
+   {
+      if (!is_array)
+         assert(array_size == NULL);
+   }
+
+   virtual ir_rvalue *hir(exec_list *instructions,
+                         struct _mesa_glsl_parse_state *state);
+
+   ast_type_qualifier layout;
+   const char *block_name;
+
+   /**
+    * Declared name of the block instance, if specified.
+    *
+    * If the block does not have an instance name, this field will be
+    * \c NULL.
+    */
+   const char *instance_name;
+
+   /** List of ast_declarator_list * */
+   exec_list declarations;
+
+   /**
+    * True if the block is declared as an array
+    *
+    * \note
+    * A block can only be an array if it also has an instance name.  If this
+    * field is true, ::instance_name must also not be \c NULL.
+    */
+   bool is_array;
+
+   /**
+    * Declared array size of the block instance
+    *
+    * If the block is not declared as an array or if the block instance array
+    * is unsized, this field will be \c NULL.
+    */
+   ast_expression *array_size;
+};
+
+
+/**
+ * AST node representing a declaration of the input layout for geometry
+ * shaders.
+ */
+class ast_gs_input_layout : public ast_node
+{
+public:
+   ast_gs_input_layout(const struct YYLTYPE &locp, GLenum prim_type)
+      : prim_type(prim_type)
+   {
+      set_location(locp);
+   }
+
+   virtual ir_rvalue *hir(exec_list *instructions,
+                          struct _mesa_glsl_parse_state *state);
+
+private:
+   const GLenum prim_type;
+};
+
+/*@}*/
 
 extern void
 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state);
 
-extern struct ir_rvalue *
-_mesa_ast_field_selection_to_hir(const struct ast_expression *expr,
+extern ir_rvalue *
+_mesa_ast_field_selection_to_hir(const ast_expression *expr,
                                 exec_list *instructions,
                                 struct _mesa_glsl_parse_state *state);
 
+extern ir_rvalue *
+_mesa_ast_array_index_to_hir(void *mem_ctx,
+                            struct _mesa_glsl_parse_state *state,
+                            ir_rvalue *array, ir_rvalue *idx,
+                            YYLTYPE &loc, YYLTYPE &idx_loc);
+
+extern void
+_mesa_ast_set_aggregate_type(const ast_type_specifier *type,
+                             ast_expression *expr,
+                             _mesa_glsl_parse_state *state);
+
+void
+emit_function(_mesa_glsl_parse_state *state, ir_function *f);
+
+extern void
+check_builtin_array_max_size(const char *name, unsigned size,
+                             YYLTYPE loc, struct _mesa_glsl_parse_state *state);
+
 #endif /* AST_H */