glsl: add missing null check in tfeedback_decl::init()
[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 #include "glsl_parser_extras.h"
27
28 typedef enum {
29 PARAMETER_LIST_NO_MATCH,
30 PARAMETER_LIST_EXACT_MATCH,
31 PARAMETER_LIST_INEXACT_MATCH /*< Match requires implicit conversion. */
32 } parameter_list_match_t;
33
34 /**
35 * \brief Check if two parameter lists match.
36 *
37 * \param list_a Parameters of the function definition.
38 * \param list_b Actual parameters passed to the function.
39 * \see matching_signature()
40 */
41 static parameter_list_match_t
42 parameter_lists_match(_mesa_glsl_parse_state *state,
43 const exec_list *list_a, const exec_list *list_b)
44 {
45 const exec_node *node_a = list_a->head;
46 const exec_node *node_b = list_b->head;
47
48 /* This is set to true if there is an inexact match requiring an implicit
49 * conversion. */
50 bool inexact_match = false;
51
52 for (/* empty */
53 ; !node_a->is_tail_sentinel()
54 ; node_a = node_a->next, node_b = node_b->next) {
55 /* If all of the parameters from the other parameter list have been
56 * exhausted, the lists have different length and, by definition,
57 * do not match.
58 */
59 if (node_b->is_tail_sentinel())
60 return PARAMETER_LIST_NO_MATCH;
61
62
63 const ir_variable *const param = (ir_variable *) node_a;
64 const ir_rvalue *const actual = (ir_rvalue *) node_b;
65
66 if (param->type == actual->type)
67 continue;
68
69 /* Try to find an implicit conversion from actual to param. */
70 inexact_match = true;
71 switch ((enum ir_variable_mode)(param->data.mode)) {
72 case ir_var_auto:
73 case ir_var_uniform:
74 case ir_var_temporary:
75 /* These are all error conditions. It is invalid for a parameter to
76 * a function to be declared as auto (not in, out, or inout) or
77 * as uniform.
78 */
79 assert(0);
80 return PARAMETER_LIST_NO_MATCH;
81
82 case ir_var_const_in:
83 case ir_var_function_in:
84 if (!actual->type->can_implicitly_convert_to(param->type, state))
85 return PARAMETER_LIST_NO_MATCH;
86 break;
87
88 case ir_var_function_out:
89 if (!param->type->can_implicitly_convert_to(actual->type, state))
90 return PARAMETER_LIST_NO_MATCH;
91 break;
92
93 case ir_var_function_inout:
94 /* Since there are no bi-directional automatic conversions (e.g.,
95 * there is int -> float but no float -> int), inout parameters must
96 * be exact matches.
97 */
98 return PARAMETER_LIST_NO_MATCH;
99
100 default:
101 assert(false);
102 return PARAMETER_LIST_NO_MATCH;
103 }
104 }
105
106 /* If all of the parameters from the other parameter list have been
107 * exhausted, the lists have different length and, by definition, do not
108 * match.
109 */
110 if (!node_b->is_tail_sentinel())
111 return PARAMETER_LIST_NO_MATCH;
112
113 if (inexact_match)
114 return PARAMETER_LIST_INEXACT_MATCH;
115 else
116 return PARAMETER_LIST_EXACT_MATCH;
117 }
118
119
120 /* Classes of parameter match, sorted (mostly) best matches first.
121 * See is_better_parameter_match() below for the exceptions.
122 * */
123 typedef enum {
124 PARAMETER_EXACT_MATCH,
125 PARAMETER_FLOAT_TO_DOUBLE,
126 PARAMETER_INT_TO_FLOAT,
127 PARAMETER_INT_TO_DOUBLE,
128 PARAMETER_OTHER_CONVERSION,
129 } parameter_match_t;
130
131
132 static parameter_match_t
133 get_parameter_match_type(const ir_variable *param,
134 const ir_rvalue *actual)
135 {
136 const glsl_type *from_type;
137 const glsl_type *to_type;
138
139 if (param->data.mode == ir_var_function_out) {
140 from_type = param->type;
141 to_type = actual->type;
142 } else {
143 from_type = actual->type;
144 to_type = param->type;
145 }
146
147 if (from_type == to_type)
148 return PARAMETER_EXACT_MATCH;
149
150 /* XXX: When ARB_gpu_shader_fp64 support is added, check for float->double,
151 * and int/uint->double conversions
152 */
153
154 if (to_type->base_type == GLSL_TYPE_FLOAT)
155 return PARAMETER_INT_TO_FLOAT;
156
157 /* int -> uint and any other oddball conversions */
158 return PARAMETER_OTHER_CONVERSION;
159 }
160
161
162 static bool
163 is_better_parameter_match(parameter_match_t a_match,
164 parameter_match_t b_match)
165 {
166 /* From section 6.1 of the GLSL 4.00 spec (and the ARB_gpu_shader5 spec):
167 *
168 * 1. An exact match is better than a match involving any implicit
169 * conversion.
170 *
171 * 2. A match involving an implicit conversion from float to double
172 * is better than match involving any other implicit conversion.
173 *
174 * [XXX: Not in GLSL 4.0: Only in ARB_gpu_shader5:
175 * 3. A match involving an implicit conversion from either int or uint
176 * to float is better than a match involving an implicit conversion
177 * from either int or uint to double.]
178 *
179 * If none of the rules above apply to a particular pair of conversions,
180 * neither conversion is considered better than the other.
181 *
182 * --
183 *
184 * Notably, the int->uint conversion is *not* considered to be better
185 * or worse than int/uint->float or int/uint->double.
186 */
187
188 if (a_match >= PARAMETER_INT_TO_FLOAT && b_match == PARAMETER_OTHER_CONVERSION)
189 return false;
190
191 return a_match < b_match;
192 }
193
194
195 static bool
196 is_best_inexact_overload(const exec_list *actual_parameters,
197 ir_function_signature **matches,
198 int num_matches,
199 ir_function_signature *sig)
200 {
201 /* From section 6.1 of the GLSL 4.00 spec (and the ARB_gpu_shader5 spec):
202 *
203 * "A function definition A is considered a better
204 * match than function definition B if:
205 *
206 * * for at least one function argument, the conversion for that argument
207 * in A is better than the corresponding conversion in B; and
208 *
209 * * there is no function argument for which the conversion in B is better
210 * than the corresponding conversion in A.
211 *
212 * If a single function definition is considered a better match than every
213 * other matching function definition, it will be used. Otherwise, a
214 * semantic error occurs and the shader will fail to compile."
215 */
216 for (ir_function_signature **other = matches;
217 other < matches + num_matches; other++) {
218 if (*other == sig)
219 continue;
220
221 const exec_node *node_a = sig->parameters.head;
222 const exec_node *node_b = (*other)->parameters.head;
223 const exec_node *node_p = actual_parameters->head;
224
225 bool better_for_some_parameter = false;
226
227 for (/* empty */
228 ; !node_a->is_tail_sentinel()
229 ; node_a = node_a->next,
230 node_b = node_b->next,
231 node_p = node_p->next) {
232 parameter_match_t a_match = get_parameter_match_type(
233 (const ir_variable *)node_a,
234 (const ir_rvalue *)node_p);
235 parameter_match_t b_match = get_parameter_match_type(
236 (const ir_variable *)node_b,
237 (const ir_rvalue *)node_p);
238
239 if (is_better_parameter_match(a_match, b_match))
240 better_for_some_parameter = true;
241
242 if (is_better_parameter_match(b_match, a_match))
243 return false; /* B is better for this parameter */
244 }
245
246 if (!better_for_some_parameter)
247 return false; /* A must be better than B for some parameter */
248
249 }
250
251 return true;
252 }
253
254
255 static ir_function_signature *
256 choose_best_inexact_overload(_mesa_glsl_parse_state *state,
257 const exec_list *actual_parameters,
258 ir_function_signature **matches,
259 int num_matches)
260 {
261 if (num_matches == 0)
262 return NULL;
263
264 if (num_matches == 1)
265 return *matches;
266
267 /* Without GLSL 4.0 / ARB_gpu_shader5, there is no overload resolution
268 * among multiple inexact matches. Note that state may be NULL here if
269 * called from the linker; in that case we assume everything supported in
270 * any GLSL version is available. */
271 if (!state || state->is_version(400, 0) || state->ARB_gpu_shader5_enable) {
272 for (ir_function_signature **sig = matches; sig < matches + num_matches; sig++) {
273 if (is_best_inexact_overload(actual_parameters, matches, num_matches, *sig))
274 return *sig;
275 }
276 }
277
278 return NULL; /* no best candidate */
279 }
280
281
282 ir_function_signature *
283 ir_function::matching_signature(_mesa_glsl_parse_state *state,
284 const exec_list *actual_parameters,
285 bool allow_builtins)
286 {
287 bool is_exact;
288 return matching_signature(state, actual_parameters, allow_builtins,
289 &is_exact);
290 }
291
292 ir_function_signature *
293 ir_function::matching_signature(_mesa_glsl_parse_state *state,
294 const exec_list *actual_parameters,
295 bool allow_builtins,
296 bool *is_exact)
297 {
298 ir_function_signature **inexact_matches = NULL;
299 ir_function_signature *match = NULL;
300 int num_inexact_matches = 0;
301
302 /* From page 42 (page 49 of the PDF) of the GLSL 1.20 spec:
303 *
304 * "If an exact match is found, the other signatures are ignored, and
305 * the exact match is used. Otherwise, if no exact match is found, then
306 * the implicit conversions in Section 4.1.10 "Implicit Conversions" will
307 * be applied to the calling arguments if this can make their types match
308 * a signature. In this case, it is a semantic error if there are
309 * multiple ways to apply these conversions to the actual arguments of a
310 * call such that the call can be made to match multiple signatures."
311 */
312 foreach_in_list(ir_function_signature, sig, &this->signatures) {
313 /* Skip over any built-ins that aren't available in this shader. */
314 if (sig->is_builtin() && (!allow_builtins ||
315 !sig->is_builtin_available(state)))
316 continue;
317
318 switch (parameter_lists_match(state, & sig->parameters, actual_parameters)) {
319 case PARAMETER_LIST_EXACT_MATCH:
320 *is_exact = true;
321 free(inexact_matches);
322 return sig;
323 case PARAMETER_LIST_INEXACT_MATCH:
324 inexact_matches = (ir_function_signature **)
325 realloc(inexact_matches,
326 sizeof(*inexact_matches) *
327 (num_inexact_matches + 1));
328 assert(inexact_matches);
329 inexact_matches[num_inexact_matches++] = sig;
330 continue;
331 case PARAMETER_LIST_NO_MATCH:
332 continue;
333 default:
334 assert(false);
335 return NULL;
336 }
337 }
338
339 /* There is no exact match (we would have returned it by now). If there
340 * are multiple inexact matches, the call is ambiguous, which is an error.
341 *
342 * FINISHME: Report a decent error. Returning NULL will likely result in
343 * FINISHME: a "no matching signature" error; it should report that the
344 * FINISHME: call is ambiguous. But reporting errors from here is hard.
345 */
346 *is_exact = false;
347
348 match = choose_best_inexact_overload(state, actual_parameters,
349 inexact_matches, num_inexact_matches);
350
351 free(inexact_matches);
352 return match;
353 }
354
355
356 static bool
357 parameter_lists_match_exact(const exec_list *list_a, const exec_list *list_b)
358 {
359 const exec_node *node_a = list_a->head;
360 const exec_node *node_b = list_b->head;
361
362 for (/* empty */
363 ; !node_a->is_tail_sentinel() && !node_b->is_tail_sentinel()
364 ; node_a = node_a->next, node_b = node_b->next) {
365 ir_variable *a = (ir_variable *) node_a;
366 ir_variable *b = (ir_variable *) node_b;
367
368 /* If the types of the parameters do not match, the parameters lists
369 * are different.
370 */
371 if (a->type != b->type)
372 return false;
373 }
374
375 /* Unless both lists are exhausted, they differ in length and, by
376 * definition, do not match.
377 */
378 return (node_a->is_tail_sentinel() == node_b->is_tail_sentinel());
379 }
380
381 ir_function_signature *
382 ir_function::exact_matching_signature(_mesa_glsl_parse_state *state,
383 const exec_list *actual_parameters)
384 {
385 foreach_in_list(ir_function_signature, sig, &this->signatures) {
386 /* Skip over any built-ins that aren't available in this shader. */
387 if (sig->is_builtin() && !sig->is_builtin_available(state))
388 continue;
389
390 if (parameter_lists_match_exact(&sig->parameters, actual_parameters))
391 return sig;
392 }
393 return NULL;
394 }