revert between 56095 -> 55830 in arch
[AROS.git] / workbench / libs / mesa / src / glsl / ir_function.cpp
blob06eca020664c0361252518b90e45504950e24522
1 /*
2 * Copyright © 2010 Intel Corporation
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:
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.
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.
24 #include "glsl_types.h"
25 #include "ir.h"
27 int
28 type_compare(const glsl_type *a, const glsl_type *b)
30 /* If the types are the same, they trivially match.
32 if (a == b)
33 return 0;
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.
41 if ((a->is_integer() != b->is_integer())
42 || (a->is_boolean() != b->is_boolean()))
43 return -1;
45 /* FALLTHROUGH */
47 case GLSL_TYPE_FLOAT:
48 if ((a->vector_elements != b->vector_elements)
49 || (a->matrix_columns != b->matrix_columns))
50 return -1;
52 return 1;
54 case GLSL_TYPE_SAMPLER:
55 case GLSL_TYPE_STRUCT:
56 /* Samplers and structures must match exactly.
58 return -1;
60 case GLSL_TYPE_ARRAY:
61 if ((b->base_type != GLSL_TYPE_ARRAY)
62 || (a->length != b->length))
63 return -1;
65 /* From GLSL 1.50 spec, page 27 (page 33 of the PDF):
66 * "There are no implicit array or structure conversions."
68 * If the comparison of the array element types detects that a conversion
69 * would be required, the array types do not match.
71 return (type_compare(a->fields.array, b->fields.array) == 0) ? 0 : -1;
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.
79 return -1;
82 /* This point should be unreachable.
84 assert(0);
88 static int
89 parameter_lists_match(const exec_list *list_a, const exec_list *list_b)
91 const exec_node *node_a = list_a->head;
92 const exec_node *node_b = list_b->head;
93 int total_score = 0;
95 for (/* empty */
96 ; !node_a->is_tail_sentinel()
97 ; node_a = node_a->next, node_b = node_b->next) {
98 /* If all of the parameters from the other parameter list have been
99 * exhausted, the lists have different length and, by definition,
100 * do not match.
102 if (node_b->is_tail_sentinel())
103 return -1;
106 const ir_variable *const param = (ir_variable *) node_a;
107 const ir_instruction *const actual = (ir_instruction *) node_b;
109 /* Determine whether or not the types match. If the types are an
110 * exact match, the match score is zero. If the types don't match
111 * but the actual parameter can be coerced to the type of the declared
112 * parameter, the match score is one.
114 int score = -1;
115 switch ((enum ir_variable_mode)(param->mode)) {
116 case ir_var_auto:
117 case ir_var_uniform:
118 case ir_var_temporary:
119 /* These are all error conditions. It is invalid for a parameter to
120 * a function to be declared as auto (not in, out, or inout) or
121 * as uniform.
123 assert(0);
124 return -1;
126 case ir_var_const_in:
127 case ir_var_in:
128 score = type_compare(param->type, actual->type);
129 break;
131 case ir_var_out:
132 score = type_compare(actual->type, param->type);
133 break;
135 case ir_var_inout:
136 /* Since there are no bi-directional automatic conversions (e.g.,
137 * there is int -> float but no float -> int), inout parameters must
138 * be exact matches.
140 score = (type_compare(actual->type, param->type) == 0) ? 0 : -1;
141 break;
143 default:
144 assert(false);
147 if (score < 0)
148 return -1;
150 total_score += score;
153 /* If all of the parameters from the other parameter list have been
154 * exhausted, the lists have different length and, by definition, do not
155 * match.
157 if (!node_b->is_tail_sentinel())
158 return -1;
160 return total_score;
164 ir_function_signature *
165 ir_function::matching_signature(const exec_list *actual_parameters)
167 ir_function_signature *match = NULL;
168 bool multiple_inexact_matches = false;
170 /* From page 42 (page 49 of the PDF) of the GLSL 1.20 spec:
172 * "If an exact match is found, the other signatures are ignored, and
173 * the exact match is used. Otherwise, if no exact match is found, then
174 * the implicit conversions in Section 4.1.10 "Implicit Conversions" will
175 * be applied to the calling arguments if this can make their types match
176 * a signature. In this case, it is a semantic error if there are
177 * multiple ways to apply these conversions to the actual arguments of a
178 * call such that the call can be made to match multiple signatures."
180 foreach_iter(exec_list_iterator, iter, signatures) {
181 ir_function_signature *const sig =
182 (ir_function_signature *) iter.get();
184 const int score = parameter_lists_match(& sig->parameters,
185 actual_parameters);
187 /* If we found an exact match, simply return it */
188 if (score == 0)
189 return sig;
191 if (score > 0) {
192 if (match == NULL)
193 match = sig;
194 else
195 multiple_inexact_matches = true;
199 /* There is no exact match (we would have returned it by now). If there
200 * are multiple inexact matches, the call is ambiguous, which is an error.
202 * FINISHME: Report a decent error. Returning NULL will likely result in
203 * FINISHME: a "no matching signature" error; it should report that the
204 * FINISHME: call is ambiguous. But reporting errors from here is hard.
206 if (multiple_inexact_matches)
207 return NULL;
209 return match;
213 static bool
214 parameter_lists_match_exact(const exec_list *list_a, const exec_list *list_b)
216 const exec_node *node_a = list_a->head;
217 const exec_node *node_b = list_b->head;
219 for (/* empty */
220 ; !node_a->is_tail_sentinel() && !node_b->is_tail_sentinel()
221 ; node_a = node_a->next, node_b = node_b->next) {
222 ir_variable *a = (ir_variable *) node_a;
223 ir_variable *b = (ir_variable *) node_b;
225 /* If the types of the parameters do not match, the parameters lists
226 * are different.
228 if (a->type != b->type)
229 return false;
232 /* Unless both lists are exhausted, they differ in length and, by
233 * definition, do not match.
235 return (node_a->is_tail_sentinel() == node_b->is_tail_sentinel());
238 ir_function_signature *
239 ir_function::exact_matching_signature(const exec_list *actual_parameters)
241 foreach_iter(exec_list_iterator, iter, signatures) {
242 ir_function_signature *const sig =
243 (ir_function_signature *) iter.get();
245 if (parameter_lists_match_exact(&sig->parameters, actual_parameters))
246 return sig;
248 return NULL;