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
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.
25 #include "glsl_symbol_table.h"
27 #include "glsl_types.h"
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
)
35 ir_function
*f
= state
->symbols
->get_function(name
);
38 _mesa_glsl_error(loc
, state
, "function `%s' undeclared", name
);
39 return ir_call::get_error_instruction();
42 /* Once we've determined that the function being called might exist,
43 * process the parameters.
45 exec_list actual_parameters
;
46 simple_node
*const first
= parameters
;
48 simple_node
*ptr
= first
;
50 ir_instruction
*const result
=
51 ((ast_node
*) ptr
)->hir(instructions
, state
);
54 actual_parameters
.push_tail(result
);
55 } while (ptr
!= first
);
58 /* After processing the function's actual parameters, try to find an
59 * overload of the function that matches.
61 const ir_function_signature
*sig
=
62 f
->matching_signature(& actual_parameters
);
64 /* FINISHME: The list of actual parameters needs to be modified to
65 * FINISHME: include any necessary conversions.
67 return new ir_call(sig
, & actual_parameters
);
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.
74 _mesa_glsl_error(loc
, state
, "no matching function for call to `%s'",
76 return ir_call::get_error_instruction();
82 ast_function_expression::hir(exec_list
*instructions
,
83 struct _mesa_glsl_parse_state
*state
)
85 /* There are three sorts of function calls.
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.
91 * Method calls are actually detected when the ast_field_selection
92 * expression is handled.
94 if (is_constructor()) {
95 const ast_type_specifier
*type
= (ast_type_specifier
*) subexpressions
[0];
96 YYLTYPE loc
= type
->get_location();
98 const glsl_type
*const constructor_type
=
99 state
->symbols
->get_type(type
->type_name
);
102 /* Constructors for samplers are illegal.
104 if (constructor_type
->is_sampler()) {
105 _mesa_glsl_error(& loc
, state
, "cannot construct sampler type `%s'",
106 constructor_type
->name
);
107 return ir_call::get_error_instruction();
111 /* There are two kinds of constructor call. Constructors for built-in
112 * language types, such as mat4 and vec2, are free form. The only
113 * requirement is that the parameters must provide enough values of the
114 * correct scalar type. Constructors for arrays and structures must
115 * have the exact number of parameters with matching types in the
116 * correct order. These constructors follow essentially the same type
117 * matching rules as functions.
120 return ir_call::get_error_instruction();
122 const ast_expression
*id
= subexpressions
[0];
123 YYLTYPE loc
= id
->get_location();
125 return match_function_by_name(instructions
,
126 id
->primary_expression
.identifier
, & loc
,
127 subexpressions
[1], state
);
130 return ir_call::get_error_instruction();