hir_field_selection.cpp revision 61c59234f916406512b3591f46599cc29a5d8e23
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 "program/symbol_table.h" 26#include "glsl_parser_extras.h" 27#include "ast.h" 28#include "glsl_types.h" 29 30using std::strcmp; 31 32ir_rvalue * 33_mesa_ast_field_selection_to_hir(const ast_expression *expr, 34 exec_list *instructions, 35 struct _mesa_glsl_parse_state *state) 36{ 37 void *ctx = state; 38 ir_rvalue *result = NULL; 39 ir_rvalue *op; 40 41 op = expr->subexpressions[0]->hir(instructions, state); 42 43 /* There are two kinds of field selection. There is the selection of a 44 * specific field from a structure, and there is the selection of a 45 * swizzle / mask from a vector. Which is which is determined entirely 46 * by the base type of the thing to which the field selection operator is 47 * being applied. 48 */ 49 YYLTYPE loc = expr->get_location(); 50 if (op->type->is_error()) { 51 /* silently propagate the error */ 52 } else if (op->type->is_vector()) { 53 ir_swizzle *swiz = ir_swizzle::create(op, 54 expr->primary_expression.identifier, 55 op->type->vector_elements); 56 if (swiz != NULL) { 57 result = swiz; 58 } else { 59 /* FINISHME: Logging of error messages should be moved into 60 * FINISHME: ir_swizzle::create. This allows the generation of more 61 * FINISHME: specific error messages. 62 */ 63 _mesa_glsl_error(& loc, state, "Invalid swizzle / mask `%s'", 64 expr->primary_expression.identifier); 65 } 66 } else if (op->type->base_type == GLSL_TYPE_STRUCT) { 67 result = new(ctx) ir_dereference_record(op, 68 expr->primary_expression.identifier); 69 70 if (result->type->is_error()) { 71 _mesa_glsl_error(& loc, state, "Cannot access field `%s' of " 72 "structure", 73 expr->primary_expression.identifier); 74 } 75 } else if (expr->subexpressions[1] != NULL) { 76 /* Handle "method calls" in GLSL 1.20 - namely, array.length() */ 77 if (state->language_version < 120) 78 _mesa_glsl_error(&loc, state, "Methods not supported in GLSL 1.10."); 79 80 ast_expression *call = expr->subexpressions[1]; 81 assert(call->oper == ast_function_call); 82 83 const char *method; 84 method = call->subexpressions[0]->primary_expression.identifier; 85 86 if (op->type->is_array() && strcmp(method, "length") == 0) { 87 if (!call->expressions.is_empty()) 88 _mesa_glsl_error(&loc, state, "length method takes no arguments."); 89 90 if (op->type->array_size() == 0) 91 _mesa_glsl_error(&loc, state, "length called on unsized array."); 92 93 result = new(ctx) ir_constant(op->type->array_size()); 94 } else { 95 _mesa_glsl_error(&loc, state, "Unknown method: `%s'.", method); 96 } 97 } else { 98 _mesa_glsl_error(& loc, state, "Cannot access field `%s' of " 99 "non-structure / non-vector.", 100 expr->primary_expression.identifier); 101 } 102 103 return result ? result : ir_call::get_error_instruction(ctx); 104} 105