1b8e80941Smrg/*
2b8e80941Smrg * Copyright © 2010 Intel Corporation
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the next
12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg * Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21b8e80941Smrg * DEALINGS IN THE SOFTWARE.
22b8e80941Smrg */
23b8e80941Smrg
24b8e80941Smrg#include "compiler/glsl_types.h"
25b8e80941Smrg#include "ir.h"
26b8e80941Smrg#include "glsl_parser_extras.h"
27b8e80941Smrg#include "main/errors.h"
28b8e80941Smrg
29b8e80941Smrgtypedef enum {
30b8e80941Smrg   PARAMETER_LIST_NO_MATCH,
31b8e80941Smrg   PARAMETER_LIST_EXACT_MATCH,
32b8e80941Smrg   PARAMETER_LIST_INEXACT_MATCH /*< Match requires implicit conversion. */
33b8e80941Smrg} parameter_list_match_t;
34b8e80941Smrg
35b8e80941Smrg/**
36b8e80941Smrg * \brief Check if two parameter lists match.
37b8e80941Smrg *
38b8e80941Smrg * \param list_a Parameters of the function definition.
39b8e80941Smrg * \param list_b Actual parameters passed to the function.
40b8e80941Smrg * \see matching_signature()
41b8e80941Smrg */
42b8e80941Smrgstatic parameter_list_match_t
43b8e80941Smrgparameter_lists_match(_mesa_glsl_parse_state *state,
44b8e80941Smrg                      const exec_list *list_a, const exec_list *list_b)
45b8e80941Smrg{
46b8e80941Smrg   const exec_node *node_a = list_a->get_head_raw();
47b8e80941Smrg   const exec_node *node_b = list_b->get_head_raw();
48b8e80941Smrg
49b8e80941Smrg   /* This is set to true if there is an inexact match requiring an implicit
50b8e80941Smrg    * conversion. */
51b8e80941Smrg   bool inexact_match = false;
52b8e80941Smrg
53b8e80941Smrg   for (/* empty */
54b8e80941Smrg	; !node_a->is_tail_sentinel()
55b8e80941Smrg	; node_a = node_a->next, node_b = node_b->next) {
56b8e80941Smrg      /* If all of the parameters from the other parameter list have been
57b8e80941Smrg       * exhausted, the lists have different length and, by definition,
58b8e80941Smrg       * do not match.
59b8e80941Smrg       */
60b8e80941Smrg      if (node_b->is_tail_sentinel())
61b8e80941Smrg	 return PARAMETER_LIST_NO_MATCH;
62b8e80941Smrg
63b8e80941Smrg
64b8e80941Smrg      const ir_variable *const param = (ir_variable *) node_a;
65b8e80941Smrg      const ir_rvalue *const actual = (ir_rvalue *) node_b;
66b8e80941Smrg
67b8e80941Smrg      if (param->type == actual->type)
68b8e80941Smrg	 continue;
69b8e80941Smrg
70b8e80941Smrg      /* Try to find an implicit conversion from actual to param. */
71b8e80941Smrg      inexact_match = true;
72b8e80941Smrg      switch ((enum ir_variable_mode)(param->data.mode)) {
73b8e80941Smrg      case ir_var_auto:
74b8e80941Smrg      case ir_var_uniform:
75b8e80941Smrg      case ir_var_shader_storage:
76b8e80941Smrg      case ir_var_temporary:
77b8e80941Smrg	 /* These are all error conditions.  It is invalid for a parameter to
78b8e80941Smrg	  * a function to be declared as auto (not in, out, or inout) or
79b8e80941Smrg	  * as uniform.
80b8e80941Smrg	  */
81b8e80941Smrg	 assert(0);
82b8e80941Smrg	 return PARAMETER_LIST_NO_MATCH;
83b8e80941Smrg
84b8e80941Smrg      case ir_var_const_in:
85b8e80941Smrg      case ir_var_function_in:
86b8e80941Smrg	 if (!actual->type->can_implicitly_convert_to(param->type, state))
87b8e80941Smrg	    return PARAMETER_LIST_NO_MATCH;
88b8e80941Smrg	 break;
89b8e80941Smrg
90b8e80941Smrg      case ir_var_function_out:
91b8e80941Smrg	 if (!param->type->can_implicitly_convert_to(actual->type, state))
92b8e80941Smrg	    return PARAMETER_LIST_NO_MATCH;
93b8e80941Smrg	 break;
94b8e80941Smrg
95b8e80941Smrg      case ir_var_function_inout:
96b8e80941Smrg	 /* Since there are no bi-directional automatic conversions (e.g.,
97b8e80941Smrg	  * there is int -> float but no float -> int), inout parameters must
98b8e80941Smrg	  * be exact matches.
99b8e80941Smrg	  */
100b8e80941Smrg	 return PARAMETER_LIST_NO_MATCH;
101b8e80941Smrg
102b8e80941Smrg      default:
103b8e80941Smrg	 assert(false);
104b8e80941Smrg	 return PARAMETER_LIST_NO_MATCH;
105b8e80941Smrg      }
106b8e80941Smrg   }
107b8e80941Smrg
108b8e80941Smrg   /* If all of the parameters from the other parameter list have been
109b8e80941Smrg    * exhausted, the lists have different length and, by definition, do not
110b8e80941Smrg    * match.
111b8e80941Smrg    */
112b8e80941Smrg   if (!node_b->is_tail_sentinel())
113b8e80941Smrg      return PARAMETER_LIST_NO_MATCH;
114b8e80941Smrg
115b8e80941Smrg   if (inexact_match)
116b8e80941Smrg      return PARAMETER_LIST_INEXACT_MATCH;
117b8e80941Smrg   else
118b8e80941Smrg      return PARAMETER_LIST_EXACT_MATCH;
119b8e80941Smrg}
120b8e80941Smrg
121b8e80941Smrg
122b8e80941Smrg/* Classes of parameter match, sorted (mostly) best matches first.
123b8e80941Smrg * See is_better_parameter_match() below for the exceptions.
124b8e80941Smrg * */
125b8e80941Smrgtypedef enum {
126b8e80941Smrg   PARAMETER_EXACT_MATCH,
127b8e80941Smrg   PARAMETER_FLOAT_TO_DOUBLE,
128b8e80941Smrg   PARAMETER_INT_TO_FLOAT,
129b8e80941Smrg   PARAMETER_INT_TO_DOUBLE,
130b8e80941Smrg   PARAMETER_OTHER_CONVERSION,
131b8e80941Smrg} parameter_match_t;
132b8e80941Smrg
133b8e80941Smrg
134b8e80941Smrgstatic parameter_match_t
135b8e80941Smrgget_parameter_match_type(const ir_variable *param,
136b8e80941Smrg                         const ir_rvalue *actual)
137b8e80941Smrg{
138b8e80941Smrg   const glsl_type *from_type;
139b8e80941Smrg   const glsl_type *to_type;
140b8e80941Smrg
141b8e80941Smrg   if (param->data.mode == ir_var_function_out) {
142b8e80941Smrg      from_type = param->type;
143b8e80941Smrg      to_type = actual->type;
144b8e80941Smrg   } else {
145b8e80941Smrg      from_type = actual->type;
146b8e80941Smrg      to_type = param->type;
147b8e80941Smrg   }
148b8e80941Smrg
149b8e80941Smrg   if (from_type == to_type)
150b8e80941Smrg      return PARAMETER_EXACT_MATCH;
151b8e80941Smrg
152b8e80941Smrg   if (to_type->is_double()) {
153b8e80941Smrg      if (from_type->is_float())
154b8e80941Smrg         return PARAMETER_FLOAT_TO_DOUBLE;
155b8e80941Smrg      return PARAMETER_INT_TO_DOUBLE;
156b8e80941Smrg   }
157b8e80941Smrg
158b8e80941Smrg   if (to_type->is_float())
159b8e80941Smrg      return PARAMETER_INT_TO_FLOAT;
160b8e80941Smrg
161b8e80941Smrg   /* int -> uint and any other oddball conversions */
162b8e80941Smrg   return PARAMETER_OTHER_CONVERSION;
163b8e80941Smrg}
164b8e80941Smrg
165b8e80941Smrg
166b8e80941Smrgstatic bool
167b8e80941Smrgis_better_parameter_match(parameter_match_t a_match,
168b8e80941Smrg                          parameter_match_t b_match)
169b8e80941Smrg{
170b8e80941Smrg   /* From section 6.1 of the GLSL 4.00 spec (and the ARB_gpu_shader5 spec):
171b8e80941Smrg    *
172b8e80941Smrg    * 1. An exact match is better than a match involving any implicit
173b8e80941Smrg    * conversion.
174b8e80941Smrg    *
175b8e80941Smrg    * 2. A match involving an implicit conversion from float to double
176b8e80941Smrg    * is better than match involving any other implicit conversion.
177b8e80941Smrg    *
178b8e80941Smrg    * [XXX: Not in GLSL 4.0: Only in ARB_gpu_shader5:
179b8e80941Smrg    * 3. A match involving an implicit conversion from either int or uint
180b8e80941Smrg    * to float is better than a match involving an implicit conversion
181b8e80941Smrg    * from either int or uint to double.]
182b8e80941Smrg    *
183b8e80941Smrg    * If none of the rules above apply to a particular pair of conversions,
184b8e80941Smrg    * neither conversion is considered better than the other.
185b8e80941Smrg    *
186b8e80941Smrg    * --
187b8e80941Smrg    *
188b8e80941Smrg    * Notably, the int->uint conversion is *not* considered to be better
189b8e80941Smrg    * or worse than int/uint->float or int/uint->double.
190b8e80941Smrg    */
191b8e80941Smrg
192b8e80941Smrg   if (a_match >= PARAMETER_INT_TO_FLOAT && b_match == PARAMETER_OTHER_CONVERSION)
193b8e80941Smrg      return false;
194b8e80941Smrg
195b8e80941Smrg   return a_match < b_match;
196b8e80941Smrg}
197b8e80941Smrg
198b8e80941Smrg
199b8e80941Smrgstatic bool
200b8e80941Smrgis_best_inexact_overload(const exec_list *actual_parameters,
201b8e80941Smrg                         ir_function_signature **matches,
202b8e80941Smrg                         int num_matches,
203b8e80941Smrg                         ir_function_signature *sig)
204b8e80941Smrg{
205b8e80941Smrg   /* From section 6.1 of the GLSL 4.00 spec (and the ARB_gpu_shader5 spec):
206b8e80941Smrg    *
207b8e80941Smrg    * "A function definition A is considered a better
208b8e80941Smrg    * match than function definition B if:
209b8e80941Smrg    *
210b8e80941Smrg    *   * for at least one function argument, the conversion for that argument
211b8e80941Smrg    *     in A is better than the corresponding conversion in B; and
212b8e80941Smrg    *
213b8e80941Smrg    *   * there is no function argument for which the conversion in B is better
214b8e80941Smrg    *     than the corresponding conversion in A.
215b8e80941Smrg    *
216b8e80941Smrg    * If a single function definition is considered a better match than every
217b8e80941Smrg    * other matching function definition, it will be used.  Otherwise, a
218b8e80941Smrg    * semantic error occurs and the shader will fail to compile."
219b8e80941Smrg    */
220b8e80941Smrg   for (ir_function_signature **other = matches;
221b8e80941Smrg        other < matches + num_matches; other++) {
222b8e80941Smrg      if (*other == sig)
223b8e80941Smrg         continue;
224b8e80941Smrg
225b8e80941Smrg      const exec_node *node_a = sig->parameters.get_head_raw();
226b8e80941Smrg      const exec_node *node_b = (*other)->parameters.get_head_raw();
227b8e80941Smrg      const exec_node *node_p = actual_parameters->get_head_raw();
228b8e80941Smrg
229b8e80941Smrg      bool better_for_some_parameter = false;
230b8e80941Smrg
231b8e80941Smrg      for (/* empty */
232b8e80941Smrg           ; !node_a->is_tail_sentinel()
233b8e80941Smrg           ; node_a = node_a->next,
234b8e80941Smrg             node_b = node_b->next,
235b8e80941Smrg             node_p = node_p->next) {
236b8e80941Smrg         parameter_match_t a_match = get_parameter_match_type(
237b8e80941Smrg               (const ir_variable *)node_a,
238b8e80941Smrg               (const ir_rvalue *)node_p);
239b8e80941Smrg         parameter_match_t b_match = get_parameter_match_type(
240b8e80941Smrg               (const ir_variable *)node_b,
241b8e80941Smrg               (const ir_rvalue *)node_p);
242b8e80941Smrg
243b8e80941Smrg         if (is_better_parameter_match(a_match, b_match))
244b8e80941Smrg               better_for_some_parameter = true;
245b8e80941Smrg
246b8e80941Smrg         if (is_better_parameter_match(b_match, a_match))
247b8e80941Smrg               return false;     /* B is better for this parameter */
248b8e80941Smrg      }
249b8e80941Smrg
250b8e80941Smrg      if (!better_for_some_parameter)
251b8e80941Smrg         return false;     /* A must be better than B for some parameter */
252b8e80941Smrg
253b8e80941Smrg   }
254b8e80941Smrg
255b8e80941Smrg   return true;
256b8e80941Smrg}
257b8e80941Smrg
258b8e80941Smrg
259b8e80941Smrgstatic ir_function_signature *
260b8e80941Smrgchoose_best_inexact_overload(_mesa_glsl_parse_state *state,
261b8e80941Smrg                             const exec_list *actual_parameters,
262b8e80941Smrg                             ir_function_signature **matches,
263b8e80941Smrg                             int num_matches)
264b8e80941Smrg{
265b8e80941Smrg   if (num_matches == 0)
266b8e80941Smrg      return NULL;
267b8e80941Smrg
268b8e80941Smrg   if (num_matches == 1)
269b8e80941Smrg      return *matches;
270b8e80941Smrg
271b8e80941Smrg   /* Without GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions,
272b8e80941Smrg    * there is no overload resolution among multiple inexact matches. Note
273b8e80941Smrg    * that state may be NULL here if called from the linker; in that case we
274b8e80941Smrg    * assume everything supported in any GLSL version is available.
275b8e80941Smrg    */
276b8e80941Smrg   if (!state || state->is_version(400, 0) || state->ARB_gpu_shader5_enable ||
277b8e80941Smrg       state->MESA_shader_integer_functions_enable ||
278b8e80941Smrg       state->EXT_shader_implicit_conversions_enable) {
279b8e80941Smrg      for (ir_function_signature **sig = matches; sig < matches + num_matches; sig++) {
280b8e80941Smrg         if (is_best_inexact_overload(actual_parameters, matches, num_matches, *sig))
281b8e80941Smrg            return *sig;
282b8e80941Smrg      }
283b8e80941Smrg   }
284b8e80941Smrg
285b8e80941Smrg   return NULL;   /* no best candidate */
286b8e80941Smrg}
287b8e80941Smrg
288b8e80941Smrg
289b8e80941Smrgir_function_signature *
290b8e80941Smrgir_function::matching_signature(_mesa_glsl_parse_state *state,
291b8e80941Smrg                                const exec_list *actual_parameters,
292b8e80941Smrg                                bool allow_builtins)
293b8e80941Smrg{
294b8e80941Smrg   bool is_exact;
295b8e80941Smrg   return matching_signature(state, actual_parameters, allow_builtins,
296b8e80941Smrg                             &is_exact);
297b8e80941Smrg}
298b8e80941Smrg
299b8e80941Smrgir_function_signature *
300b8e80941Smrgir_function::matching_signature(_mesa_glsl_parse_state *state,
301b8e80941Smrg                                const exec_list *actual_parameters,
302b8e80941Smrg                                bool allow_builtins,
303b8e80941Smrg                                bool *is_exact)
304b8e80941Smrg{
305b8e80941Smrg   ir_function_signature **inexact_matches = NULL;
306b8e80941Smrg   ir_function_signature **inexact_matches_temp;
307b8e80941Smrg   ir_function_signature *match = NULL;
308b8e80941Smrg   int num_inexact_matches = 0;
309b8e80941Smrg
310b8e80941Smrg   /* From page 42 (page 49 of the PDF) of the GLSL 1.20 spec:
311b8e80941Smrg    *
312b8e80941Smrg    * "If an exact match is found, the other signatures are ignored, and
313b8e80941Smrg    *  the exact match is used.  Otherwise, if no exact match is found, then
314b8e80941Smrg    *  the implicit conversions in Section 4.1.10 "Implicit Conversions" will
315b8e80941Smrg    *  be applied to the calling arguments if this can make their types match
316b8e80941Smrg    *  a signature.  In this case, it is a semantic error if there are
317b8e80941Smrg    *  multiple ways to apply these conversions to the actual arguments of a
318b8e80941Smrg    *  call such that the call can be made to match multiple signatures."
319b8e80941Smrg    */
320b8e80941Smrg   foreach_in_list(ir_function_signature, sig, &this->signatures) {
321b8e80941Smrg      /* Skip over any built-ins that aren't available in this shader. */
322b8e80941Smrg      if (sig->is_builtin() && (!allow_builtins ||
323b8e80941Smrg                                !sig->is_builtin_available(state)))
324b8e80941Smrg         continue;
325b8e80941Smrg
326b8e80941Smrg      switch (parameter_lists_match(state, & sig->parameters, actual_parameters)) {
327b8e80941Smrg      case PARAMETER_LIST_EXACT_MATCH:
328b8e80941Smrg         *is_exact = true;
329b8e80941Smrg         free(inexact_matches);
330b8e80941Smrg         return sig;
331b8e80941Smrg      case PARAMETER_LIST_INEXACT_MATCH:
332b8e80941Smrg         inexact_matches_temp = (ir_function_signature **)
333b8e80941Smrg               realloc(inexact_matches,
334b8e80941Smrg                       sizeof(*inexact_matches) *
335b8e80941Smrg                       (num_inexact_matches + 1));
336b8e80941Smrg         if (inexact_matches_temp == NULL) {
337b8e80941Smrg            _mesa_error_no_memory(__func__);
338b8e80941Smrg            free(inexact_matches);
339b8e80941Smrg            return NULL;
340b8e80941Smrg         }
341b8e80941Smrg         inexact_matches = inexact_matches_temp;
342b8e80941Smrg         inexact_matches[num_inexact_matches++] = sig;
343b8e80941Smrg         continue;
344b8e80941Smrg      case PARAMETER_LIST_NO_MATCH:
345b8e80941Smrg	 continue;
346b8e80941Smrg      default:
347b8e80941Smrg	 assert(false);
348b8e80941Smrg	 return NULL;
349b8e80941Smrg      }
350b8e80941Smrg   }
351b8e80941Smrg
352b8e80941Smrg   /* There is no exact match (we would have returned it by now).  If there
353b8e80941Smrg    * are multiple inexact matches, the call is ambiguous, which is an error.
354b8e80941Smrg    *
355b8e80941Smrg    * FINISHME: Report a decent error.  Returning NULL will likely result in
356b8e80941Smrg    * FINISHME: a "no matching signature" error; it should report that the
357b8e80941Smrg    * FINISHME: call is ambiguous.  But reporting errors from here is hard.
358b8e80941Smrg    */
359b8e80941Smrg   *is_exact = false;
360b8e80941Smrg
361b8e80941Smrg   match = choose_best_inexact_overload(state, actual_parameters,
362b8e80941Smrg                                        inexact_matches, num_inexact_matches);
363b8e80941Smrg
364b8e80941Smrg   free(inexact_matches);
365b8e80941Smrg   return match;
366b8e80941Smrg}
367b8e80941Smrg
368b8e80941Smrg
369b8e80941Smrgstatic bool
370b8e80941Smrgparameter_lists_match_exact(const exec_list *list_a, const exec_list *list_b)
371b8e80941Smrg{
372b8e80941Smrg   const exec_node *node_a = list_a->get_head_raw();
373b8e80941Smrg   const exec_node *node_b = list_b->get_head_raw();
374b8e80941Smrg
375b8e80941Smrg   for (/* empty */
376b8e80941Smrg	; !node_a->is_tail_sentinel() && !node_b->is_tail_sentinel()
377b8e80941Smrg	; node_a = node_a->next, node_b = node_b->next) {
378b8e80941Smrg      ir_variable *a = (ir_variable *) node_a;
379b8e80941Smrg      ir_variable *b = (ir_variable *) node_b;
380b8e80941Smrg
381b8e80941Smrg      /* If the types of the parameters do not match, the parameters lists
382b8e80941Smrg       * are different.
383b8e80941Smrg       */
384b8e80941Smrg      if (a->type != b->type)
385b8e80941Smrg         return false;
386b8e80941Smrg   }
387b8e80941Smrg
388b8e80941Smrg   /* Unless both lists are exhausted, they differ in length and, by
389b8e80941Smrg    * definition, do not match.
390b8e80941Smrg    */
391b8e80941Smrg   return (node_a->is_tail_sentinel() == node_b->is_tail_sentinel());
392b8e80941Smrg}
393b8e80941Smrg
394b8e80941Smrgir_function_signature *
395b8e80941Smrgir_function::exact_matching_signature(_mesa_glsl_parse_state *state,
396b8e80941Smrg                                      const exec_list *actual_parameters)
397b8e80941Smrg{
398b8e80941Smrg   foreach_in_list(ir_function_signature, sig, &this->signatures) {
399b8e80941Smrg      /* Skip over any built-ins that aren't available in this shader. */
400b8e80941Smrg      if (sig->is_builtin() && !sig->is_builtin_available(state))
401b8e80941Smrg         continue;
402b8e80941Smrg
403b8e80941Smrg      if (parameter_lists_match_exact(&sig->parameters, actual_parameters))
404b8e80941Smrg	 return sig;
405b8e80941Smrg   }
406b8e80941Smrg   return NULL;
407b8e80941Smrg}
408