Set the type of a function call to be the return type of the callee
[mesa.git] / ast_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 <cstdio>
25 #include "glsl_symbol_table.h"
26 #include "ast.h"
27 #include "glsl_types.h"
28 #include "ir.h"
29
30 static ir_instruction *
31 match_function_by_name(exec_list *instructions, const char *name,
32 YYLTYPE *loc, simple_node *parameters,
33 struct _mesa_glsl_parse_state *state)
34 {
35 ir_function *f = state->symbols->get_function(name);
36
37 if (f == NULL) {
38 _mesa_glsl_error(loc, state, "function `%s' undeclared", name);
39 return ir_call::get_error_instruction();
40 }
41
42 /* Once we've determined that the function being called might exist,
43 * process the parameters.
44 */
45 exec_list actual_parameters;
46 simple_node *const first = parameters;
47 if (first != NULL) {
48 simple_node *ptr = first;
49 do {
50 ir_instruction *const result =
51 ((ast_node *) ptr)->hir(instructions, state);
52 ptr = ptr->next;
53
54 actual_parameters.push_tail(result);
55 } while (ptr != first);
56 }
57
58 /* After processing the function's actual parameters, try to find an
59 * overload of the function that matches.
60 */
61 const ir_function_signature *sig =
62 f->matching_signature(& actual_parameters);
63 if (sig != NULL) {
64 /* FINISHME: The list of actual parameters needs to be modified to
65 * FINISHME: include any necessary conversions.
66 */
67 return new ir_call(sig, & actual_parameters);
68 } else {
69 /* FINISHME: Log a better error message here. G++ will show the types
70 * FINISHME: of the actual parameters and the set of candidate
71 * FINISHME: functions. A different error should also be logged when
72 * FINISHME: multiple functions match.
73 */
74 _mesa_glsl_error(loc, state, "no matching function for call to `%s'",
75 name);
76 return ir_call::get_error_instruction();
77 }
78 }
79
80
81 ir_instruction *
82 ast_function_expression::hir(exec_list *instructions,
83 struct _mesa_glsl_parse_state *state)
84 {
85 /* There are three sorts of function calls.
86 *
87 * 1. contstructors - The first subexpression is an ast_type_specifier.
88 * 2. methods - Only the .length() method of array types.
89 * 3. functions - Calls to regular old functions.
90 *
91 * There are two kinds of constructor call. Constructors for built-in
92 * language types, such as mat4 and vec2, are free form. The only
93 * requirement is that the parameters must provide enough values of the
94 * correct scalar type. Constructors for arrays and structures must have
95 * the exact number of parameters with matching types in the correct order.
96 * These constructors follow essentially the same type matching rules as
97 * functions.
98 *
99 * Method calls are actually detected when the ast_field_selection
100 * expression is handled.
101 */
102 if (is_constructor()) {
103 return ir_call::get_error_instruction();
104 } else {
105 const ast_expression *id = subexpressions[0];
106 YYLTYPE loc = id->get_location();
107
108 return match_function_by_name(instructions,
109 id->primary_expression.identifier, & loc,
110 subexpressions[1], state);
111 }
112
113 return ir_call::get_error_instruction();
114 }