glsl: move to compiler/
[mesa.git] / src / compiler / glsl / hir_field_selection.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "ir.h"
25 #include "glsl_parser_extras.h"
26 #include "ast.h"
27 #include "compiler/glsl_types.h"
28
29 ir_rvalue *
30 _mesa_ast_field_selection_to_hir(const ast_expression *expr,
31 exec_list *instructions,
32 struct _mesa_glsl_parse_state *state)
33 {
34 void *ctx = state;
35 ir_rvalue *result = NULL;
36 ir_rvalue *op;
37
38 op = expr->subexpressions[0]->hir(instructions, state);
39
40 /* There are two kinds of field selection. There is the selection of a
41 * specific field from a structure, and there is the selection of a
42 * swizzle / mask from a vector. Which is which is determined entirely
43 * by the base type of the thing to which the field selection operator is
44 * being applied.
45 */
46 YYLTYPE loc = expr->get_location();
47 if (op->type->is_error()) {
48 /* silently propagate the error */
49 } else if (op->type->base_type == GLSL_TYPE_STRUCT
50 || op->type->base_type == GLSL_TYPE_INTERFACE) {
51 result = new(ctx) ir_dereference_record(op,
52 expr->primary_expression.identifier);
53
54 if (result->type->is_error()) {
55 _mesa_glsl_error(& loc, state, "cannot access field `%s' of "
56 "structure",
57 expr->primary_expression.identifier);
58 }
59 } else if (op->type->is_vector() ||
60 (state->has_420pack() && op->type->is_scalar())) {
61 ir_swizzle *swiz = ir_swizzle::create(op,
62 expr->primary_expression.identifier,
63 op->type->vector_elements);
64 if (swiz != NULL) {
65 result = swiz;
66 } else {
67 /* FINISHME: Logging of error messages should be moved into
68 * FINISHME: ir_swizzle::create. This allows the generation of more
69 * FINISHME: specific error messages.
70 */
71 _mesa_glsl_error(& loc, state, "invalid swizzle / mask `%s'",
72 expr->primary_expression.identifier);
73 }
74 } else {
75 _mesa_glsl_error(& loc, state, "cannot access field `%s' of "
76 "non-structure / non-vector",
77 expr->primary_expression.identifier);
78 }
79
80 return result ? result : ir_rvalue::error_value(ctx);
81 }