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