Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -0700 | [diff] [blame] | 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> |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 25 | #include "glsl_symbol_table.h" |
Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -0700 | [diff] [blame] | 26 | #include "ast.h" |
| 27 | #include "glsl_types.h" |
| 28 | #include "ir.h" |
| 29 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame^] | 30 | static ir_rvalue * |
Ian Romanick | f474961 | 2010-03-15 13:26:02 -0700 | [diff] [blame] | 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 | { |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 35 | ir_function *f = state->symbols->get_function(name); |
Ian Romanick | f474961 | 2010-03-15 13:26:02 -0700 | [diff] [blame] | 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 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame^] | 81 | ir_rvalue * |
Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -0700 | [diff] [blame] | 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 | * |
Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -0700 | [diff] [blame] | 91 | * Method calls are actually detected when the ast_field_selection |
| 92 | * expression is handled. |
| 93 | */ |
| 94 | if (is_constructor()) { |
Ian Romanick | abef955 | 2010-03-23 15:08:30 -0700 | [diff] [blame] | 95 | const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0]; |
| 96 | YYLTYPE loc = type->get_location(); |
| 97 | |
| 98 | const glsl_type *const constructor_type = |
| 99 | state->symbols->get_type(type->type_name); |
| 100 | |
| 101 | |
| 102 | /* Constructors for samplers are illegal. |
| 103 | */ |
| 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(); |
| 108 | } |
| 109 | |
| 110 | |
| 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. |
| 118 | */ |
| 119 | |
Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -0700 | [diff] [blame] | 120 | return ir_call::get_error_instruction(); |
| 121 | } else { |
| 122 | const ast_expression *id = subexpressions[0]; |
Ian Romanick | f474961 | 2010-03-15 13:26:02 -0700 | [diff] [blame] | 123 | YYLTYPE loc = id->get_location(); |
Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -0700 | [diff] [blame] | 124 | |
Ian Romanick | f474961 | 2010-03-15 13:26:02 -0700 | [diff] [blame] | 125 | return match_function_by_name(instructions, |
| 126 | id->primary_expression.identifier, & loc, |
| 127 | subexpressions[1], state); |
Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | return ir_call::get_error_instruction(); |
| 131 | } |