+static bool
+add_shader_variable(struct gl_shader_program *shProg, unsigned stage_mask,
+ GLenum programInterface, ir_variable *var,
+ const char *name, const glsl_type *type,
+ bool use_implicit_location, int location,
+ const glsl_type *outermost_struct_type = NULL)
+{
+ const bool is_vertex_input =
+ programInterface == GL_PROGRAM_INPUT &&
+ stage_mask == MESA_SHADER_VERTEX;
+
+ switch (type->base_type) {
+ case GLSL_TYPE_STRUCT: {
+ /* The ARB_program_interface_query spec says:
+ *
+ * "For an active variable declared as a structure, a separate entry
+ * will be generated for each active structure member. The name of
+ * each entry is formed by concatenating the name of the structure,
+ * the "." character, and the name of the structure member. If a
+ * structure member to enumerate is itself a structure or array,
+ * these enumeration rules are applied recursively."
+ */
+ if (outermost_struct_type == NULL)
+ outermost_struct_type = type;
+
+ unsigned field_location = location;
+ for (unsigned i = 0; i < type->length; i++) {
+ const struct glsl_struct_field *field = &type->fields.structure[i];
+ char *field_name = ralloc_asprintf(shProg, "%s.%s", name, field->name);
+ if (!add_shader_variable(shProg, stage_mask, programInterface,
+ var, field_name, field->type,
+ use_implicit_location, field_location,
+ outermost_struct_type))
+ return false;
+
+ field_location +=
+ field->type->count_attribute_slots(is_vertex_input);
+ }
+ return true;
+ }
+
+ default: {
+ /* Issue #16 of the ARB_program_interface_query spec says:
+ *
+ * "* If a variable is a member of an interface block without an
+ * instance name, it is enumerated using just the variable name.
+ *
+ * * If a variable is a member of an interface block with an instance
+ * name, it is enumerated as "BlockName.Member", where "BlockName" is
+ * the name of the interface block (not the instance name) and
+ * "Member" is the name of the variable."
+ */
+ const char *prefixed_name = (var->data.from_named_ifc_block &&
+ !is_gl_identifier(var->name))
+ ? ralloc_asprintf(shProg, "%s.%s", var->get_interface_type()->name,
+ name)
+ : name;
+
+ /* The ARB_program_interface_query spec says:
+ *
+ * "For an active variable declared as a single instance of a basic
+ * type, a single entry will be generated, using the variable name
+ * from the shader source."
+ */
+ gl_shader_variable *sha_v =
+ create_shader_variable(shProg, var, prefixed_name, type,
+ use_implicit_location, location,
+ outermost_struct_type);
+ if (!sha_v)
+ return false;
+
+ return add_program_resource(shProg, programInterface, sha_v, stage_mask);
+ }
+ }
+}
+