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