glsl: Fix implicit conversions in non-constructor function calls
[mesa.git] / src / glsl / ir_function.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 "glsl_types.h"
25 #include "ir.h"
26
27 int
28 type_compare(const glsl_type *a, const glsl_type *b)
29 {
30 /* If the types are the same, they trivially match.
31 */
32 if (a == b)
33 return 0;
34
35 switch (a->base_type) {
36 case GLSL_TYPE_UINT:
37 case GLSL_TYPE_INT:
38 case GLSL_TYPE_BOOL:
39 /* There is no implicit conversion to or from integer types or bool.
40 */
41 if ((a->is_integer() != b->is_integer())
42 || (a->is_boolean() != b->is_boolean()))
43 return -1;
44
45 /* FALLTHROUGH */
46
47 case GLSL_TYPE_FLOAT:
48 if ((a->vector_elements != b->vector_elements)
49 || (a->matrix_columns != b->matrix_columns))
50 return -1;
51
52 return 1;
53
54 case GLSL_TYPE_SAMPLER:
55 case GLSL_TYPE_STRUCT:
56 /* Samplers and structures must match exactly.
57 */
58 return -1;
59
60 case GLSL_TYPE_ARRAY:
61 if ((b->base_type != GLSL_TYPE_ARRAY)
62 || (a->length != b->length))
63 return -1;
64
65 /* From GLSL 1.50 spec, page 27 (page 33 of the PDF):
66 * "There are no implicit array or structure conversions."
67 *
68 * If the comparison of the array element types detects that a conversion
69 * would be required, the array types do not match.
70 */
71 return (type_compare(a->fields.array, b->fields.array) == 0) ? 0 : -1;
72
73 case GLSL_TYPE_VOID:
74 case GLSL_TYPE_ERROR:
75 default:
76 /* These are all error conditions. It is invalid for a parameter to
77 * a function to be declared as error, void, or a function.
78 */
79 return -1;
80 }
81
82 /* This point should be unreachable.
83 */
84 assert(0);
85 }
86
87
88 /**
89 * \brief Check if two parameter lists match.
90 *
91 * \param list_a Parameters of the function definition.
92 * \param list_b Actual parameters passed to the function.
93 * \return If an exact match, return 0.
94 * If an inexact match requiring implicit conversion, return 1.
95 * If not a match, return -1.
96 * \see matching_signature()
97 */
98 static int
99 parameter_lists_match(const exec_list *list_a, const exec_list *list_b)
100 {
101 const exec_node *node_a = list_a->head;
102 const exec_node *node_b = list_b->head;
103
104 /* This is set to true if there is an inexact match requiring an implicit
105 * conversion. */
106 bool inexact_match = false;
107
108 for (/* empty */
109 ; !node_a->is_tail_sentinel()
110 ; node_a = node_a->next, node_b = node_b->next) {
111 /* If all of the parameters from the other parameter list have been
112 * exhausted, the lists have different length and, by definition,
113 * do not match.
114 */
115 if (node_b->is_tail_sentinel())
116 return -1;
117
118
119 const ir_variable *const param = (ir_variable *) node_a;
120 const ir_instruction *const actual = (ir_instruction *) node_b;
121
122 if (param->type == actual->type)
123 continue;
124
125 /* Try to find an implicit conversion from actual to param. */
126 inexact_match = true;
127 switch ((enum ir_variable_mode)(param->mode)) {
128 case ir_var_auto:
129 case ir_var_uniform:
130 case ir_var_temporary:
131 /* These are all error conditions. It is invalid for a parameter to
132 * a function to be declared as auto (not in, out, or inout) or
133 * as uniform.
134 */
135 assert(0);
136 return -1;
137
138 case ir_var_const_in:
139 case ir_var_in:
140 if (!actual->type->can_implicitly_convert_to(param->type))
141 return -1;
142 break;
143
144 case ir_var_out:
145 if (!param->type->can_implicitly_convert_to(actual->type))
146 return -1;
147 break;
148
149 case ir_var_inout:
150 /* Since there are no bi-directional automatic conversions (e.g.,
151 * there is int -> float but no float -> int), inout parameters must
152 * be exact matches.
153 */
154 return -1;
155
156 default:
157 assert(false);
158 return -1;
159 }
160 }
161
162 /* If all of the parameters from the other parameter list have been
163 * exhausted, the lists have different length and, by definition, do not
164 * match.
165 */
166 if (!node_b->is_tail_sentinel())
167 return -1;
168
169 if (inexact_match)
170 return 1;
171 else
172 return 0;
173 }
174
175
176 ir_function_signature *
177 ir_function::matching_signature(const exec_list *actual_parameters)
178 {
179 ir_function_signature *match = NULL;
180 bool multiple_inexact_matches = false;
181
182 /* From page 42 (page 49 of the PDF) of the GLSL 1.20 spec:
183 *
184 * "If an exact match is found, the other signatures are ignored, and
185 * the exact match is used. Otherwise, if no exact match is found, then
186 * the implicit conversions in Section 4.1.10 "Implicit Conversions" will
187 * be applied to the calling arguments if this can make their types match
188 * a signature. In this case, it is a semantic error if there are
189 * multiple ways to apply these conversions to the actual arguments of a
190 * call such that the call can be made to match multiple signatures."
191 */
192 foreach_iter(exec_list_iterator, iter, signatures) {
193 ir_function_signature *const sig =
194 (ir_function_signature *) iter.get();
195
196 const int score = parameter_lists_match(& sig->parameters,
197 actual_parameters);
198
199 /* If we found an exact match, simply return it */
200 if (score == 0)
201 return sig;
202
203 if (score > 0) {
204 if (match == NULL)
205 match = sig;
206 else
207 multiple_inexact_matches = true;
208 }
209 }
210
211 /* There is no exact match (we would have returned it by now). If there
212 * are multiple inexact matches, the call is ambiguous, which is an error.
213 *
214 * FINISHME: Report a decent error. Returning NULL will likely result in
215 * FINISHME: a "no matching signature" error; it should report that the
216 * FINISHME: call is ambiguous. But reporting errors from here is hard.
217 */
218 if (multiple_inexact_matches)
219 return NULL;
220
221 return match;
222 }
223
224
225 static bool
226 parameter_lists_match_exact(const exec_list *list_a, const exec_list *list_b)
227 {
228 const exec_node *node_a = list_a->head;
229 const exec_node *node_b = list_b->head;
230
231 for (/* empty */
232 ; !node_a->is_tail_sentinel() && !node_b->is_tail_sentinel()
233 ; node_a = node_a->next, node_b = node_b->next) {
234 ir_variable *a = (ir_variable *) node_a;
235 ir_variable *b = (ir_variable *) node_b;
236
237 /* If the types of the parameters do not match, the parameters lists
238 * are different.
239 */
240 if (a->type != b->type)
241 return false;
242 }
243
244 /* Unless both lists are exhausted, they differ in length and, by
245 * definition, do not match.
246 */
247 return (node_a->is_tail_sentinel() == node_b->is_tail_sentinel());
248 }
249
250 ir_function_signature *
251 ir_function::exact_matching_signature(const exec_list *actual_parameters)
252 {
253 foreach_iter(exec_list_iterator, iter, signatures) {
254 ir_function_signature *const sig =
255 (ir_function_signature *) iter.get();
256
257 if (parameter_lists_match_exact(&sig->parameters, actual_parameters))
258 return sig;
259 }
260 return NULL;
261 }