Ian Romanick | 471471f | 2010-03-11 14:50:30 -0800 | [diff] [blame] | 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 "glsl_types.h" |
| 25 | #include "ir.h" |
| 26 | |
| 27 | int |
| 28 | type_compare(const glsl_type *a, const glsl_type *b) |
| 29 | { |
Ian Romanick | 80b5ed6 | 2010-03-25 13:05:43 -0700 | [diff] [blame] | 30 | /* If the types are the same, they trivially match. |
| 31 | */ |
| 32 | if (a == b) |
| 33 | return 0; |
| 34 | |
Ian Romanick | 471471f | 2010-03-11 14:50:30 -0800 | [diff] [blame] | 35 | switch (a->base_type) { |
| 36 | case GLSL_TYPE_UINT: |
| 37 | case GLSL_TYPE_INT: |
| 38 | case GLSL_TYPE_FLOAT: |
| 39 | case GLSL_TYPE_BOOL: |
| 40 | if ((a->vector_elements != b->vector_elements) |
Ian Romanick | 1b4f041 | 2010-03-25 13:19:13 -0700 | [diff] [blame] | 41 | || (a->matrix_columns != b->matrix_columns)) |
Ian Romanick | 471471f | 2010-03-11 14:50:30 -0800 | [diff] [blame] | 42 | return -1; |
| 43 | |
Ian Romanick | 471471f | 2010-03-11 14:50:30 -0800 | [diff] [blame] | 44 | /* There is no implicit conversion to or from bool. |
| 45 | */ |
| 46 | if ((a->base_type == GLSL_TYPE_BOOL) |
| 47 | || (b->base_type == GLSL_TYPE_BOOL)) |
| 48 | return -1; |
| 49 | |
| 50 | return 1; |
| 51 | |
| 52 | case GLSL_TYPE_SAMPLER: |
Ian Romanick | 471471f | 2010-03-11 14:50:30 -0800 | [diff] [blame] | 53 | case GLSL_TYPE_STRUCT: |
Ian Romanick | 80b5ed6 | 2010-03-25 13:05:43 -0700 | [diff] [blame] | 54 | /* Samplers and structures must match exactly. |
| 55 | */ |
| 56 | return -1; |
Ian Romanick | 471471f | 2010-03-11 14:50:30 -0800 | [diff] [blame] | 57 | |
| 58 | case GLSL_TYPE_ARRAY: |
| 59 | if ((b->base_type != GLSL_TYPE_ARRAY) |
| 60 | || (a->length != b->length)) |
| 61 | return -1; |
| 62 | |
| 63 | /* From GLSL 1.50 spec, page 27 (page 33 of the PDF): |
| 64 | * "There are no implicit array or structure conversions." |
| 65 | * |
| 66 | * If the comparison of the array element types detects that a conversion |
| 67 | * would be required, the array types do not match. |
| 68 | */ |
| 69 | return (type_compare(a->fields.array, b->fields.array) == 0) ? 0 : -1; |
| 70 | |
| 71 | case GLSL_TYPE_FUNCTION: |
| 72 | case GLSL_TYPE_VOID: |
| 73 | case GLSL_TYPE_ERROR: |
| 74 | default: |
| 75 | /* These are all error conditions. It is invalid for a parameter to |
| 76 | * a function to be declared as error, void, or a function. |
| 77 | */ |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | /* This point should be unreachable. |
| 82 | */ |
| 83 | assert(0); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | static int |
| 88 | parameter_lists_match(exec_list *list_a, exec_list *list_b) |
| 89 | { |
| 90 | exec_list_iterator iter_a = list_a->iterator(); |
| 91 | exec_list_iterator iter_b = list_b->iterator(); |
| 92 | int total_score = 0; |
| 93 | |
| 94 | for (/* empty */ ; iter_a.has_next(); iter_a.next(), iter_b.next()) { |
| 95 | /* If all of the parameters from the other parameter list have been |
| 96 | * exhausted, the lists have different length and, by definition, |
| 97 | * do not match. |
| 98 | */ |
| 99 | if (!iter_b.has_next()) |
| 100 | return -1; |
| 101 | |
| 102 | |
| 103 | const ir_variable *const param = (ir_variable *) iter_a.get(); |
| 104 | const ir_instruction *const actual = (ir_instruction *) iter_b.get(); |
| 105 | |
| 106 | /* Determine whether or not the types match. If the types are an |
| 107 | * exact match, the match score is zero. If the types don't match |
| 108 | * but the actual parameter can be coerced to the type of the declared |
| 109 | * parameter, the match score is one. |
| 110 | */ |
| 111 | int score; |
| 112 | switch ((enum ir_variable_mode)(param->mode)) { |
| 113 | case ir_var_auto: |
| 114 | case ir_var_uniform: |
| 115 | /* These are all error conditions. It is invalid for a parameter to |
| 116 | * a function to be declared as auto (not in, out, or inout) or |
| 117 | * as uniform. |
| 118 | */ |
| 119 | assert(0); |
| 120 | return -1; |
| 121 | |
| 122 | case ir_var_in: |
| 123 | score = type_compare(param->type, actual->type); |
| 124 | break; |
| 125 | |
| 126 | case ir_var_out: |
| 127 | /* FINISHME: Make sure that actual is a valid lvalue. */ |
| 128 | score = type_compare(actual->type, param->type); |
| 129 | break; |
| 130 | |
| 131 | case ir_var_inout: |
| 132 | /* FINISHME: Make sure that actual is a valid lvalue. */ |
| 133 | |
| 134 | /* Since there are no bi-directional automatic conversions (e.g., |
| 135 | * there is int -> float but no float -> int), inout parameters must |
| 136 | * be exact matches. |
| 137 | */ |
| 138 | score = (type_compare(actual->type, param->type) == 0) ? 0 : -1; |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | if (score < 0) |
| 143 | return -1; |
| 144 | |
| 145 | total_score += score; |
| 146 | } |
| 147 | |
| 148 | /* If all of the parameters from the other parameter list have been |
| 149 | * exhausted, the lists have different length and, by definition, do not |
| 150 | * match. |
| 151 | */ |
| 152 | if (iter_b.has_next()) |
| 153 | return -1; |
| 154 | |
| 155 | return total_score; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | const ir_function_signature * |
| 160 | ir_function::matching_signature(exec_list *actual_parameters) |
| 161 | { |
| 162 | ir_function_signature *match = NULL; |
| 163 | |
| 164 | foreach_iter(exec_list_iterator, iter, signatures) { |
| 165 | ir_function_signature *const sig = |
| 166 | (ir_function_signature *) iter.get(); |
| 167 | |
| 168 | const int score = parameter_lists_match(& sig->parameters, |
| 169 | actual_parameters); |
| 170 | |
| 171 | if (score == 0) |
| 172 | return sig; |
| 173 | |
| 174 | if (score > 0) { |
| 175 | if (match != NULL) |
| 176 | return NULL; |
| 177 | |
| 178 | match = sig; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return match; |
| 183 | } |