+
+static struct gl_uniform_block *
+get_next_uniform_block(struct _mesa_glsl_parse_state *state)
+{
+ if (state->num_uniform_blocks >= state->uniform_block_array_size) {
+ state->uniform_block_array_size *= 2;
+ if (state->uniform_block_array_size <= 4)
+ state->uniform_block_array_size = 4;
+
+ state->uniform_blocks = reralloc(state,
+ state->uniform_blocks,
+ struct gl_uniform_block,
+ state->uniform_block_array_size);
+ }
+
+ memset(&state->uniform_blocks[state->num_uniform_blocks],
+ 0, sizeof(*state->uniform_blocks));
+ return &state->uniform_blocks[state->num_uniform_blocks++];
+}
+
+ir_rvalue *
+ast_uniform_block::hir(exec_list *instructions,
+ struct _mesa_glsl_parse_state *state)
+{
+ /* The ast_uniform_block has a list of ast_declarator_lists. We
+ * need to turn those into ir_variables with an association
+ * with this uniform block.
+ */
+ struct gl_uniform_block *ubo = get_next_uniform_block(state);
+ ubo->Name = ralloc_strdup(state->uniform_blocks, this->block_name);
+
+ unsigned int num_variables = 0;
+ foreach_list_typed(ast_declarator_list, decl_list, link, &declarations) {
+ foreach_list_const(node, &decl_list->declarations) {
+ num_variables++;
+ }
+ }
+
+ bool block_row_major = this->layout.flags.q.row_major;
+
+ ubo->Uniforms = rzalloc_array(state->uniform_blocks,
+ struct gl_uniform_buffer_variable,
+ num_variables);
+
+ foreach_list_typed(ast_declarator_list, decl_list, link, &declarations) {
+ exec_list declared_variables;
+
+ decl_list->hir(&declared_variables, state);
+
+ foreach_list_const(node, &declared_variables) {
+ struct ir_variable *var = (ir_variable *)node;
+
+ struct gl_uniform_buffer_variable *ubo_var =
+ &ubo->Uniforms[ubo->NumUniforms++];
+
+ var->uniform_block = ubo - state->uniform_blocks;
+
+ ubo_var->Name = ralloc_strdup(state->uniform_blocks, var->name);
+ ubo_var->Type = var->type;
+ ubo_var->Buffer = ubo - state->uniform_blocks;
+ ubo_var->Offset = 0; /* Assigned at link time. */
+
+ if (var->type->is_matrix() ||
+ (var->type->is_array() && var->type->fields.array->is_matrix())) {
+ ubo_var->RowMajor = block_row_major;
+ if (decl_list->type->qualifier.flags.q.row_major)
+ ubo_var->RowMajor = true;
+ else if (decl_list->type->qualifier.flags.q.column_major)
+ ubo_var->RowMajor = false;
+ }
+
+ /* From the GL_ARB_uniform_buffer_object spec:
+ *
+ * "Sampler types are not allowed inside of uniform
+ * blocks. All other types, arrays, and structures
+ * allowed for uniforms are allowed within a uniform
+ * block."
+ */
+ if (var->type->contains_sampler()) {
+ YYLTYPE loc = decl_list->get_location();
+ _mesa_glsl_error(&loc, state,
+ "Uniform in non-default uniform block contains sampler\n");
+ }
+ }
+
+ instructions->append_list(&declared_variables);
+ }
+
+ return NULL;
+}
+
+static void
+detect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
+ exec_list *instructions)
+{
+ bool gl_FragColor_assigned = false;
+ bool gl_FragData_assigned = false;
+ bool user_defined_fs_output_assigned = false;
+ ir_variable *user_defined_fs_output = NULL;
+
+ /* It would be nice to have proper location information. */
+ YYLTYPE loc;
+ memset(&loc, 0, sizeof(loc));
+
+ foreach_list(node, instructions) {
+ ir_variable *var = ((ir_instruction *)node)->as_variable();
+
+ if (!var || !var->assigned)
+ continue;
+
+ if (strcmp(var->name, "gl_FragColor") == 0)
+ gl_FragColor_assigned = true;
+ else if (strcmp(var->name, "gl_FragData") == 0)
+ gl_FragData_assigned = true;
+ else if (strncmp(var->name, "gl_", 3) != 0) {
+ if (state->target == fragment_shader &&
+ (var->mode == ir_var_out || var->mode == ir_var_inout)) {
+ user_defined_fs_output_assigned = true;
+ user_defined_fs_output = var;
+ }
+ }
+ }
+
+ /* From the GLSL 1.30 spec:
+ *
+ * "If a shader statically assigns a value to gl_FragColor, it
+ * may not assign a value to any element of gl_FragData. If a
+ * shader statically writes a value to any element of
+ * gl_FragData, it may not assign a value to
+ * gl_FragColor. That is, a shader may assign values to either
+ * gl_FragColor or gl_FragData, but not both. Multiple shaders
+ * linked together must also consistently write just one of
+ * these variables. Similarly, if user declared output
+ * variables are in use (statically assigned to), then the
+ * built-in variables gl_FragColor and gl_FragData may not be
+ * assigned to. These incorrect usages all generate compile
+ * time errors."
+ */
+ if (gl_FragColor_assigned && gl_FragData_assigned) {
+ _mesa_glsl_error(&loc, state, "fragment shader writes to both "
+ "`gl_FragColor' and `gl_FragData'\n");
+ } else if (gl_FragColor_assigned && user_defined_fs_output_assigned) {
+ _mesa_glsl_error(&loc, state, "fragment shader writes to both "
+ "`gl_FragColor' and `%s'\n",
+ user_defined_fs_output->name);
+ } else if (gl_FragData_assigned && user_defined_fs_output_assigned) {
+ _mesa_glsl_error(&loc, state, "fragment shader writes to both "
+ "`gl_FragData' and `%s'\n",
+ user_defined_fs_output->name);
+ }
+}