Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ast_function.cpp 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. #include <cstdio>
  24. #include "glsl_symbol_table.h"
  25. #include "ast.h"
  26. #include "glsl_types.h"
  27. #include "ir.h"
  28. static unsigned
  29. process_parameters(exec_list *instructions, exec_list *actual_parameters,
  30. exec_list *parameters,
  31. struct _mesa_glsl_parse_state *state)
  32. {
  33. unsigned count = 0;
  34. foreach_list (n, parameters) {
  35. ast_node *const ast = exec_node_data(ast_node, n, link);
  36. ir_rvalue *result = ast->hir(instructions, state);
  37. ir_constant *const constant = result->constant_expression_value();
  38. if (constant != NULL)
  39. result = constant;
  40. actual_parameters->push_tail(result);
  41. count++;
  42. }
  43. return count;
  44. }
  45. static ir_rvalue *
  46. process_call(exec_list *instructions, ir_function *f,
  47. YYLTYPE *loc, exec_list *actual_parameters,
  48. struct _mesa_glsl_parse_state *state)
  49. {
  50. const ir_function_signature *sig =
  51. f->matching_signature(actual_parameters);
  52. /* The instructions param will be used when the FINISHMEs below are done */
  53. (void) instructions;
  54. if (sig != NULL) {
  55. /* Verify that 'out' and 'inout' actual parameters are lvalues. This
  56. * isn't done in ir_function::matching_signature because that function
  57. * cannot generate the necessary diagnostics.
  58. */
  59. exec_list_iterator actual_iter = actual_parameters->iterator();
  60. exec_list_iterator formal_iter = sig->parameters.iterator();
  61. while (actual_iter.has_next()) {
  62. ir_rvalue *actual = (ir_rvalue *) actual_iter.get();
  63. ir_variable *formal = (ir_variable *) formal_iter.get();
  64. assert(actual != NULL);
  65. assert(formal != NULL);
  66. if ((formal->mode == ir_var_out)
  67. || (formal->mode == ir_var_inout)) {
  68. if (! actual->is_lvalue()) {
  69. /* FINISHME: Log a better diagnostic here. There is no way
  70. * FINISHME: to tell the user which parameter is invalid.
  71. */
  72. _mesa_glsl_error(loc, state, "`%s' parameter is not lvalue",
  73. (formal->mode == ir_var_out) ? "out" : "inout");
  74. }
  75. }
  76. actual_iter.next();
  77. formal_iter.next();
  78. }
  79. /* FINISHME: The list of actual parameters needs to be modified to
  80. * FINISHME: include any necessary conversions.
  81. */
  82. return new ir_call(sig, actual_parameters);
  83. } else {
  84. /* FINISHME: Log a better error message here. G++ will show the types
  85. * FINISHME: of the actual parameters and the set of candidate
  86. * FINISHME: functions. A different error should also be logged when
  87. * FINISHME: multiple functions match.
  88. */
  89. _mesa_glsl_error(loc, state, "no matching function for call to `%s'",
  90. f->name);
  91. return ir_call::get_error_instruction();
  92. }
  93. }
  94. static ir_rvalue *
  95. match_function_by_name(exec_list *instructions, const char *name,
  96. YYLTYPE *loc, exec_list *actual_parameters,
  97. struct _mesa_glsl_parse_state *state)
  98. {
  99. ir_function *f = state->symbols->get_function(name);
  100. if (f == NULL) {
  101. _mesa_glsl_error(loc, state, "function `%s' undeclared", name);
  102. return ir_call::get_error_instruction();
  103. }
  104. /* Once we've determined that the function being called might exist, try
  105. * to find an overload of the function that matches the parameters.
  106. */
  107. return process_call(instructions, f, loc, actual_parameters, state);
  108. }
  109. /**
  110. * Perform automatic type conversion of constructor parameters
  111. */
  112. static ir_rvalue *
  113. convert_component(ir_rvalue *src, const glsl_type *desired_type)
  114. {
  115. const unsigned a = desired_type->base_type;
  116. const unsigned b = src->type->base_type;
  117. ir_expression *result = NULL;
  118. if (src->type->is_error())
  119. return src;
  120. assert(a <= GLSL_TYPE_BOOL);
  121. assert(b <= GLSL_TYPE_BOOL);
  122. if ((a == b) || (src->type->is_integer() && desired_type->is_integer()))
  123. return src;
  124. switch (a) {
  125. case GLSL_TYPE_UINT:
  126. case GLSL_TYPE_INT:
  127. if (b == GLSL_TYPE_FLOAT)
  128. result = new ir_expression(ir_unop_f2i, desired_type, src, NULL);
  129. else {
  130. assert(b == GLSL_TYPE_BOOL);
  131. result = new ir_expression(ir_unop_b2i, desired_type, src, NULL);
  132. }
  133. break;
  134. case GLSL_TYPE_FLOAT:
  135. switch (b) {
  136. case GLSL_TYPE_UINT:
  137. result = new ir_expression(ir_unop_u2f, desired_type, src, NULL);
  138. break;
  139. case GLSL_TYPE_INT:
  140. result = new ir_expression(ir_unop_i2f, desired_type, src, NULL);
  141. break;
  142. case GLSL_TYPE_BOOL:
  143. result = new ir_expression(ir_unop_b2f, desired_type, src, NULL);
  144. break;
  145. }
  146. break;
  147. case GLSL_TYPE_BOOL: {
  148. ir_constant *zero = NULL;
  149. switch (b) {
  150. case GLSL_TYPE_UINT: zero = new ir_constant(unsigned(0)); break;
  151. case GLSL_TYPE_INT: zero = new ir_constant(int(0)); break;
  152. case GLSL_TYPE_FLOAT: zero = new ir_constant(0.0f); break;
  153. }
  154. result = new ir_expression(ir_binop_nequal, desired_type, src, zero);
  155. }
  156. }
  157. assert(result != NULL);
  158. ir_constant *const constant = result->constant_expression_value();
  159. return (constant != NULL) ? (ir_rvalue *) constant : (ir_rvalue *) result;
  160. }
  161. /**
  162. * Dereference a specific component from a scalar, vector, or matrix
  163. */
  164. static ir_rvalue *
  165. dereference_component(ir_rvalue *src, unsigned component)
  166. {
  167. assert(component < src->type->components());
  168. /* If the source is a constant, just create a new constant instead of a
  169. * dereference of the existing constant.
  170. */
  171. ir_constant *constant = src->as_constant();
  172. if (constant)
  173. return new ir_constant(constant, component);
  174. if (src->type->is_scalar()) {
  175. return src;
  176. } else if (src->type->is_vector()) {
  177. return new ir_swizzle(src, component, 0, 0, 0, 1);
  178. } else {
  179. assert(src->type->is_matrix());
  180. /* Dereference a row of the matrix, then call this function again to get
  181. * a specific element from that row.
  182. */
  183. const int c = component / src->type->column_type()->vector_elements;
  184. const int r = component % src->type->column_type()->vector_elements;
  185. ir_constant *const col_index = new ir_constant(c);
  186. ir_dereference *const col = new ir_dereference_array(src, col_index);
  187. col->type = src->type->column_type();
  188. return dereference_component(col, r);
  189. }
  190. assert(!"Should not get here.");
  191. return NULL;
  192. }
  193. static ir_rvalue *
  194. process_array_constructor(exec_list *instructions,
  195. const glsl_type *constructor_type,
  196. YYLTYPE *loc, exec_list *parameters,
  197. struct _mesa_glsl_parse_state *state)
  198. {
  199. /* Array constructors come in two forms: sized and unsized. Sized array
  200. * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
  201. * variables. In this case the number of parameters must exactly match the
  202. * specified size of the array.
  203. *
  204. * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
  205. * are vec4 variables. In this case the size of the array being constructed
  206. * is determined by the number of parameters.
  207. *
  208. * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
  209. *
  210. * "There must be exactly the same number of arguments as the size of
  211. * the array being constructed. If no size is present in the
  212. * constructor, then the array is explicitly sized to the number of
  213. * arguments provided. The arguments are assigned in order, starting at
  214. * element 0, to the elements of the constructed array. Each argument
  215. * must be the same type as the element type of the array, or be a type
  216. * that can be converted to the element type of the array according to
  217. * Section 4.1.10 "Implicit Conversions.""
  218. */
  219. exec_list actual_parameters;
  220. const unsigned parameter_count =
  221. process_parameters(instructions, &actual_parameters, parameters, state);
  222. if ((parameter_count == 0)
  223. || ((constructor_type->length != 0)
  224. && (constructor_type->length != parameter_count))) {
  225. const unsigned min_param = (constructor_type->length == 0)
  226. ? 1 : constructor_type->length;
  227. _mesa_glsl_error(loc, state, "array constructor must have %s %u "
  228. "parameter%s",
  229. (constructor_type->length != 0) ? "at least" : "exactly",
  230. min_param, (min_param <= 1) ? "" : "s");
  231. return ir_call::get_error_instruction();
  232. }
  233. if (constructor_type->length == 0) {
  234. constructor_type =
  235. glsl_type::get_array_instance(constructor_type->element_type(),
  236. parameter_count);
  237. assert(constructor_type != NULL);
  238. assert(constructor_type->length == parameter_count);
  239. }
  240. ir_function *f = state->symbols->get_function(constructor_type->name);
  241. /* If the constructor for this type of array does not exist, generate the
  242. * prototype and add it to the symbol table.
  243. */
  244. if (f == NULL) {
  245. f = constructor_type->generate_constructor(state->symbols);
  246. }
  247. ir_rvalue *const r =
  248. process_call(instructions, f, loc, &actual_parameters, state);
  249. assert(r != NULL);
  250. assert(r->type->is_error() || (r->type == constructor_type));
  251. return r;
  252. }
  253. /**
  254. * Try to convert a record constructor to a constant expression
  255. */
  256. static ir_constant *
  257. constant_record_constructor(const glsl_type *constructor_type,
  258. YYLTYPE *loc, exec_list *parameters,
  259. struct _mesa_glsl_parse_state *state)
  260. {
  261. bool all_parameters_are_constant = true;
  262. exec_node *node = parameters->head;
  263. for (unsigned i = 0; i < constructor_type->length; i++) {
  264. ir_instruction *ir = (ir_instruction *) node;
  265. if (node->is_tail_sentinal()) {
  266. _mesa_glsl_error(loc, state,
  267. "insufficient parameters to constructor for `%s'",
  268. constructor_type->name);
  269. return NULL;
  270. }
  271. if (ir->type != constructor_type->fields.structure[i].type) {
  272. _mesa_glsl_error(loc, state,
  273. "parameter type mismatch in constructor for `%s' "
  274. " (%s vs %s)",
  275. constructor_type->name,
  276. ir->type->name,
  277. constructor_type->fields.structure[i].type->name);
  278. return NULL;
  279. }
  280. if (ir->as_constant() == NULL)
  281. all_parameters_are_constant = false;
  282. node = node->next;
  283. }
  284. if (!all_parameters_are_constant)
  285. return NULL;
  286. return new ir_constant(constructor_type, parameters);
  287. }
  288. /**
  289. * Generate data for a constant matrix constructor w/a single scalar parameter
  290. *
  291. * Matrix constructors in GLSL can be passed a single scalar of the
  292. * approriate type. In these cases, the resulting matrix is the identity
  293. * matrix multipled by the specified scalar. This function generates data for
  294. * that matrix.
  295. *
  296. * \param type Type of the desired matrix.
  297. * \param initializer Scalar value used to initialize the matrix diagonal.
  298. * \param data Location to store the resulting matrix.
  299. */
  300. void
  301. generate_constructor_matrix(const glsl_type *type, ir_constant *initializer,
  302. ir_constant_data *data)
  303. {
  304. switch (type->base_type) {
  305. case GLSL_TYPE_UINT:
  306. case GLSL_TYPE_INT:
  307. for (unsigned i = 0; i < type->components(); i++)
  308. data->u[i] = 0;
  309. for (unsigned i = 0; i < type->matrix_columns; i++) {
  310. /* The array offset of the ith row and column of the matrix.
  311. */
  312. const unsigned idx = (i * type->vector_elements) + i;
  313. data->u[idx] = initializer->value.u[0];
  314. }
  315. break;
  316. case GLSL_TYPE_FLOAT:
  317. for (unsigned i = 0; i < type->components(); i++)
  318. data->f[i] = 0;
  319. for (unsigned i = 0; i < type->matrix_columns; i++) {
  320. /* The array offset of the ith row and column of the matrix.
  321. */
  322. const unsigned idx = (i * type->vector_elements) + i;
  323. data->f[idx] = initializer->value.f[0];
  324. }
  325. break;
  326. default:
  327. assert(!"Should not get here.");
  328. break;
  329. }
  330. }
  331. /**
  332. * Generate data for a constant vector constructor w/a single scalar parameter
  333. *
  334. * Vector constructors in GLSL can be passed a single scalar of the
  335. * approriate type. In these cases, the resulting vector contains the specified
  336. * value in all components. This function generates data for that vector.
  337. *
  338. * \param type Type of the desired vector.
  339. * \param initializer Scalar value used to initialize the vector.
  340. * \param data Location to store the resulting vector data.
  341. */
  342. void
  343. generate_constructor_vector(const glsl_type *type, ir_constant *initializer,
  344. ir_constant_data *data)
  345. {
  346. switch (type->base_type) {
  347. case GLSL_TYPE_UINT:
  348. case GLSL_TYPE_INT:
  349. for (unsigned i = 0; i < type->components(); i++)
  350. data->u[i] = initializer->value.u[0];
  351. break;
  352. case GLSL_TYPE_FLOAT:
  353. for (unsigned i = 0; i < type->components(); i++)
  354. data->f[i] = initializer->value.f[0];
  355. break;
  356. case GLSL_TYPE_BOOL:
  357. for (unsigned i = 0; i < type->components(); i++)
  358. data->b[i] = initializer->value.b[0];
  359. break;
  360. default:
  361. assert(!"Should not get here.");
  362. break;
  363. }
  364. }
  365. ir_rvalue *
  366. ast_function_expression::hir(exec_list *instructions,
  367. struct _mesa_glsl_parse_state *state)
  368. {
  369. /* There are three sorts of function calls.
  370. *
  371. * 1. contstructors - The first subexpression is an ast_type_specifier.
  372. * 2. methods - Only the .length() method of array types.
  373. * 3. functions - Calls to regular old functions.
  374. *
  375. * Method calls are actually detected when the ast_field_selection
  376. * expression is handled.
  377. */
  378. if (is_constructor()) {
  379. const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
  380. YYLTYPE loc = type->get_location();
  381. const char *name;
  382. const glsl_type *const constructor_type = type->glsl_type(& name, state);
  383. /* Constructors for samplers are illegal.
  384. */
  385. if (constructor_type->is_sampler()) {
  386. _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'",
  387. constructor_type->name);
  388. return ir_call::get_error_instruction();
  389. }
  390. if (constructor_type->is_array()) {
  391. if (state->language_version <= 110) {
  392. _mesa_glsl_error(& loc, state,
  393. "array constructors forbidden in GLSL 1.10");
  394. return ir_call::get_error_instruction();
  395. }
  396. return process_array_constructor(instructions, constructor_type,
  397. & loc, &this->expressions, state);
  398. }
  399. /* There are two kinds of constructor call. Constructors for built-in
  400. * language types, such as mat4 and vec2, are free form. The only
  401. * requirement is that the parameters must provide enough values of the
  402. * correct scalar type. Constructors for arrays and structures must
  403. * have the exact number of parameters with matching types in the
  404. * correct order. These constructors follow essentially the same type
  405. * matching rules as functions.
  406. */
  407. if (constructor_type->is_numeric() || constructor_type->is_boolean()) {
  408. /* Constructing a numeric type has a couple steps. First all values
  409. * passed to the constructor are broken into individual parameters
  410. * and type converted to the base type of the thing being constructed.
  411. *
  412. * At that point we have some number of values that match the base
  413. * type of the thing being constructed. Now the constructor can be
  414. * treated like a function call. Each numeric type has a small set
  415. * of constructor functions. The set of new parameters will either
  416. * match one of those functions or the original constructor is
  417. * invalid.
  418. */
  419. const glsl_type *const base_type = constructor_type->get_base_type();
  420. /* Total number of components of the type being constructed.
  421. */
  422. const unsigned type_components = constructor_type->components();
  423. /* Number of components from parameters that have actually been
  424. * consumed. This is used to perform several kinds of error checking.
  425. */
  426. unsigned components_used = 0;
  427. unsigned matrix_parameters = 0;
  428. unsigned nonmatrix_parameters = 0;
  429. exec_list actual_parameters;
  430. bool all_parameters_are_constant = true;
  431. assert(!this->expressions.is_empty());
  432. foreach_list (n, &this->expressions) {
  433. ast_node *ast = exec_node_data(ast_node, n, link);
  434. ir_rvalue *result =
  435. ast->hir(instructions, state)->as_rvalue();
  436. /* Attempt to convert the parameter to a constant valued expression.
  437. * After doing so, track whether or not all the parameters to the
  438. * constructor are trivially constant valued expressions.
  439. */
  440. ir_rvalue *const constant =
  441. result->constant_expression_value();
  442. if (constant != NULL)
  443. result = constant;
  444. else
  445. all_parameters_are_constant = false;
  446. /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
  447. *
  448. * "It is an error to provide extra arguments beyond this
  449. * last used argument."
  450. */
  451. if (components_used >= type_components) {
  452. _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
  453. "constructor",
  454. constructor_type->name);
  455. return ir_call::get_error_instruction();
  456. }
  457. if (!result->type->is_numeric() && !result->type->is_boolean()) {
  458. _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
  459. "non-numeric data type",
  460. constructor_type->name);
  461. return ir_call::get_error_instruction();
  462. }
  463. /* Count the number of matrix and nonmatrix parameters. This
  464. * is used below to enforce some of the constructor rules.
  465. */
  466. if (result->type->is_matrix())
  467. matrix_parameters++;
  468. else
  469. nonmatrix_parameters++;
  470. /* Process each of the components of the parameter. Dereference
  471. * each component individually, perform any type conversions, and
  472. * add it to the parameter list for the constructor.
  473. */
  474. for (unsigned i = 0; i < result->type->components(); i++) {
  475. if (components_used >= type_components)
  476. break;
  477. ir_rvalue *const component =
  478. convert_component(dereference_component(result, i),
  479. base_type);
  480. /* All cases that could result in component->type being the
  481. * error type should have already been caught above.
  482. */
  483. assert(component->type == base_type);
  484. if (component->as_constant() == NULL)
  485. all_parameters_are_constant = false;
  486. /* Don't actually generate constructor calls for scalars.
  487. * Instead, do the usual component selection and conversion,
  488. * and return the single component.
  489. */
  490. if (constructor_type->is_scalar())
  491. return component;
  492. actual_parameters.push_tail(component);
  493. components_used++;
  494. }
  495. }
  496. /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
  497. *
  498. * "It is an error to construct matrices from other matrices. This
  499. * is reserved for future use."
  500. */
  501. if ((state->language_version <= 110) && (matrix_parameters > 0)
  502. && constructor_type->is_matrix()) {
  503. _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
  504. "matrix in GLSL 1.10",
  505. constructor_type->name);
  506. return ir_call::get_error_instruction();
  507. }
  508. /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
  509. *
  510. * "If a matrix argument is given to a matrix constructor, it is
  511. * an error to have any other arguments."
  512. */
  513. if ((matrix_parameters > 0)
  514. && ((matrix_parameters + nonmatrix_parameters) > 1)
  515. && constructor_type->is_matrix()) {
  516. _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
  517. "matrix must be only parameter",
  518. constructor_type->name);
  519. return ir_call::get_error_instruction();
  520. }
  521. /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
  522. *
  523. * "In these cases, there must be enough components provided in the
  524. * arguments to provide an initializer for every component in the
  525. * constructed value."
  526. */
  527. if ((components_used < type_components) && (components_used != 1)) {
  528. _mesa_glsl_error(& loc, state, "too few components to construct "
  529. "`%s'",
  530. constructor_type->name);
  531. return ir_call::get_error_instruction();
  532. }
  533. ir_function *f = state->symbols->get_function(constructor_type->name);
  534. if (f == NULL) {
  535. _mesa_glsl_error(& loc, state, "no constructor for type `%s'",
  536. constructor_type->name);
  537. return ir_call::get_error_instruction();
  538. }
  539. const ir_function_signature *sig =
  540. f->matching_signature(& actual_parameters);
  541. if (sig != NULL) {
  542. /* If all of the parameters are trivially constant, create a
  543. * constant representing the complete collection of parameters.
  544. */
  545. if (all_parameters_are_constant) {
  546. if (components_used >= type_components)
  547. return new ir_constant(sig->return_type, & actual_parameters);
  548. assert(sig->return_type->is_vector()
  549. || sig->return_type->is_matrix());
  550. /* Constructors with exactly one component are special for
  551. * vectors and matrices. For vectors it causes all elements of
  552. * the vector to be filled with the value. For matrices it
  553. * causes the matrix to be filled with 0 and the diagonal to be
  554. * filled with the value.
  555. */
  556. ir_constant_data data;
  557. ir_constant *const initializer =
  558. (ir_constant *) actual_parameters.head;
  559. if (sig->return_type->is_matrix())
  560. generate_constructor_matrix(sig->return_type, initializer,
  561. &data);
  562. else
  563. generate_constructor_vector(sig->return_type, initializer,
  564. &data);
  565. return new ir_constant(sig->return_type, &data);
  566. } else
  567. return new ir_call(sig, & actual_parameters);
  568. } else {
  569. /* FINISHME: Log a better error message here. G++ will show the
  570. * FINSIHME: types of the actual parameters and the set of
  571. * FINSIHME: candidate functions. A different error should also be
  572. * FINSIHME: logged when multiple functions match.
  573. */
  574. _mesa_glsl_error(& loc, state, "no matching constructor for `%s'",
  575. constructor_type->name);
  576. return ir_call::get_error_instruction();
  577. }
  578. }
  579. return ir_call::get_error_instruction();
  580. } else {
  581. const ast_expression *id = subexpressions[0];
  582. YYLTYPE loc = id->get_location();
  583. exec_list actual_parameters;
  584. process_parameters(instructions, &actual_parameters, &this->expressions,
  585. state);
  586. const glsl_type *const type =
  587. state->symbols->get_type(id->primary_expression.identifier);
  588. if ((type != NULL) && type->is_record()) {
  589. ir_constant *constant =
  590. constant_record_constructor(type, &loc, &actual_parameters, state);
  591. if (constant != NULL)
  592. return constant;
  593. }
  594. return match_function_by_name(instructions,
  595. id->primary_expression.identifier, & loc,
  596. &actual_parameters, state);
  597. }
  598. return ir_call::get_error_instruction();
  599. }