glsl: Disallow embedded structure definitions
authorIan Romanick <ian.d.romanick@intel.com>
Tue, 13 Aug 2013 16:15:01 +0000 (09:15 -0700)
committerIan Romanick <ian.d.romanick@intel.com>
Mon, 19 Aug 2013 23:39:04 +0000 (16:39 -0700)
Continue to allow them in GLSL 1.10 because the spec allows it.
Generate an error in all other versions because the specs specifically
disallow it.

Signed-off-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Cc: "9.2" <mesa-stable@lists.freedesktop.org>
src/glsl/ast_to_hir.cpp
src/glsl/glsl_parser_extras.cpp
src/glsl/glsl_parser_extras.h

index 3807fdec9b3eacc62e94cfad108f755d7cb1fa5b..c439391ab27dd31d9d4b766388a88a55e3b19765 100644 (file)
@@ -4455,6 +4455,34 @@ ast_struct_specifier::hir(exec_list *instructions,
                          struct _mesa_glsl_parse_state *state)
 {
    YYLTYPE loc = this->get_location();
+
+   /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
+    *
+    *     "Anonymous structures are not supported; so embedded structures must
+    *     have a declarator. A name given to an embedded struct is scoped at
+    *     the same level as the struct it is embedded in."
+    *
+    * The same section of the  GLSL 1.20 spec says:
+    *
+    *     "Anonymous structures are not supported. Embedded structures are not
+    *     supported.
+    *
+    *         struct S { float f; };
+    *         struct T {
+    *             S;              // Error: anonymous structures disallowed
+    *             struct { ... }; // Error: embedded structures disallowed
+    *             S s;            // Okay: nested structures with name are allowed
+    *         };"
+    *
+    * The GLSL ES 1.00 and 3.00 specs have similar langauge and examples.  So,
+    * we allow embedded structures in 1.10 only.
+    */
+   if (state->language_version != 110 && state->struct_specifier_depth != 0)
+      _mesa_glsl_error(&loc, state,
+                      "embedded structure declartions are not allowed");
+
+   state->struct_specifier_depth++;
+
    glsl_struct_field *fields;
    unsigned decl_count =
       ast_process_structure_or_interface_block(instructions,
@@ -4481,6 +4509,8 @@ ast_struct_specifier::hir(exec_list *instructions,
       }
    }
 
+   state->struct_specifier_depth--;
+
    /* Structure type definitions do not have r-values.
     */
    return NULL;
index 88f0483659514ab1a985f8f8b2524bfe3a33590c..a97b3d934c5bed179d1c3428eadb95b00604af9a 100644 (file)
@@ -71,6 +71,7 @@ _mesa_glsl_parse_state::_mesa_glsl_parse_state(struct gl_context *_ctx,
    this->loop_nesting_ast = NULL;
    this->switch_state.switch_nesting_ast = NULL;
 
+   this->struct_specifier_depth = 0;
    this->num_builtins_to_link = 0;
 
    /* Set default language version and extensions */
index b9ca4e3a4d66d2381165a91c4eedab2a0aa958c8..440c15bb801f66806e8949b42c28ece8180f8c20 100644 (file)
@@ -158,6 +158,13 @@ struct _mesa_glsl_parse_state {
    unsigned language_version;
    enum _mesa_glsl_parser_targets target;
 
+   /**
+    * Number of nested struct_specifier levels
+    *
+    * Outside a struct_specifer, this is zero.
+    */
+   unsigned struct_specifier_depth;
+
    /**
     * Default uniform layout qualifiers tracked during parsing.
     * Currently affects uniform blocks and uniform buffer variables in