1 /* Expression translation
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Free Software
4 Contributed by Paul Brook <paul@nowt.org>
5 and Steven Bosscher <s.bosscher@student.tudelft.nl>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* trans-expr.c-- generate GENERIC trees for gfc_expr. */
27 #include "coretypes.h"
33 #include "tree-gimple.h"
34 #include "langhooks.h"
38 #include "trans-const.h"
39 #include "trans-types.h"
40 #include "trans-array.h"
41 /* Only for gfc_trans_assign and gfc_trans_pointer_assign. */
42 #include "trans-stmt.h"
43 #include "dependency.h"
45 static tree gfc_trans_structure_assign (tree dest, gfc_expr * expr);
46 static int gfc_apply_interface_mapping_to_expr (gfc_interface_mapping *,
49 /* Copy the scalarization loop variables. */
52 gfc_copy_se_loopvars (gfc_se * dest, gfc_se * src)
55 dest->loop = src->loop;
59 /* Initialize a simple expression holder.
61 Care must be taken when multiple se are created with the same parent.
62 The child se must be kept in sync. The easiest way is to delay creation
63 of a child se until after after the previous se has been translated. */
66 gfc_init_se (gfc_se * se, gfc_se * parent)
68 memset (se, 0, sizeof (gfc_se));
69 gfc_init_block (&se->pre);
70 gfc_init_block (&se->post);
75 gfc_copy_se_loopvars (se, parent);
79 /* Advances to the next SS in the chain. Use this rather than setting
80 se->ss = se->ss->next because all the parents needs to be kept in sync.
84 gfc_advance_se_ss_chain (gfc_se * se)
88 gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
91 /* Walk down the parent chain. */
94 /* Simple consistency check. */
95 gcc_assert (p->parent == NULL || p->parent->ss == p->ss);
104 /* Ensures the result of the expression as either a temporary variable
105 or a constant so that it can be used repeatedly. */
108 gfc_make_safe_expr (gfc_se * se)
112 if (CONSTANT_CLASS_P (se->expr))
115 /* We need a temporary for this result. */
116 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
117 gfc_add_modify_expr (&se->pre, var, se->expr);
122 /* Return an expression which determines if a dummy parameter is present.
123 Also used for arguments to procedures with multiple entry points. */
126 gfc_conv_expr_present (gfc_symbol * sym)
130 gcc_assert (sym->attr.dummy);
132 decl = gfc_get_symbol_decl (sym);
133 if (TREE_CODE (decl) != PARM_DECL)
135 /* Array parameters use a temporary descriptor, we want the real
137 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
138 || GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
139 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
141 return build2 (NE_EXPR, boolean_type_node, decl,
142 fold_convert (TREE_TYPE (decl), null_pointer_node));
146 /* Converts a missing, dummy argument into a null or zero. */
149 gfc_conv_missing_dummy (gfc_se * se, gfc_expr * arg, gfc_typespec ts)
154 present = gfc_conv_expr_present (arg->symtree->n.sym);
155 tmp = build3 (COND_EXPR, TREE_TYPE (se->expr), present, se->expr,
156 fold_convert (TREE_TYPE (se->expr), integer_zero_node));
158 tmp = gfc_evaluate_now (tmp, &se->pre);
160 if (ts.type == BT_CHARACTER)
162 tmp = build_int_cst (gfc_charlen_type_node, 0);
163 tmp = build3 (COND_EXPR, gfc_charlen_type_node, present,
164 se->string_length, tmp);
165 tmp = gfc_evaluate_now (tmp, &se->pre);
166 se->string_length = tmp;
172 /* Get the character length of an expression, looking through gfc_refs
176 gfc_get_expr_charlen (gfc_expr *e)
181 gcc_assert (e->expr_type == EXPR_VARIABLE
182 && e->ts.type == BT_CHARACTER);
184 length = NULL; /* To silence compiler warning. */
186 if (is_subref_array (e) && e->ts.cl->length)
189 gfc_init_se (&tmpse, NULL);
190 gfc_conv_expr_type (&tmpse, e->ts.cl->length, gfc_charlen_type_node);
191 e->ts.cl->backend_decl = tmpse.expr;
195 /* First candidate: if the variable is of type CHARACTER, the
196 expression's length could be the length of the character
198 if (e->symtree->n.sym->ts.type == BT_CHARACTER)
199 length = e->symtree->n.sym->ts.cl->backend_decl;
201 /* Look through the reference chain for component references. */
202 for (r = e->ref; r; r = r->next)
207 if (r->u.c.component->ts.type == BT_CHARACTER)
208 length = r->u.c.component->ts.cl->backend_decl;
216 /* We should never got substring references here. These will be
217 broken down by the scalarizer. */
223 gcc_assert (length != NULL);
229 /* Generate code to initialize a string length variable. Returns the
233 gfc_conv_string_length (gfc_charlen * cl, stmtblock_t * pblock)
237 gfc_init_se (&se, NULL);
238 gfc_conv_expr_type (&se, cl->length, gfc_charlen_type_node);
239 se.expr = fold_build2 (MAX_EXPR, gfc_charlen_type_node, se.expr,
240 build_int_cst (gfc_charlen_type_node, 0));
241 gfc_add_block_to_block (pblock, &se.pre);
243 if (cl->backend_decl)
244 gfc_add_modify_expr (pblock, cl->backend_decl, se.expr);
246 cl->backend_decl = gfc_evaluate_now (se.expr, pblock);
251 gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
252 const char *name, locus *where)
262 type = gfc_get_character_type (kind, ref->u.ss.length);
263 type = build_pointer_type (type);
266 gfc_init_se (&start, se);
267 gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
268 gfc_add_block_to_block (&se->pre, &start.pre);
270 if (integer_onep (start.expr))
271 gfc_conv_string_parameter (se);
274 /* Avoid multiple evaluation of substring start. */
275 if (!CONSTANT_CLASS_P (start.expr) && !DECL_P (start.expr))
276 start.expr = gfc_evaluate_now (start.expr, &se->pre);
278 /* Change the start of the string. */
279 if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
282 tmp = build_fold_indirect_ref (se->expr);
283 tmp = gfc_build_array_ref (tmp, start.expr, NULL);
284 se->expr = gfc_build_addr_expr (type, tmp);
287 /* Length = end + 1 - start. */
288 gfc_init_se (&end, se);
289 if (ref->u.ss.end == NULL)
290 end.expr = se->string_length;
293 gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
294 gfc_add_block_to_block (&se->pre, &end.pre);
296 if (!CONSTANT_CLASS_P (end.expr) && !DECL_P (end.expr))
297 end.expr = gfc_evaluate_now (end.expr, &se->pre);
299 if (flag_bounds_check)
301 tree nonempty = fold_build2 (LE_EXPR, boolean_type_node,
302 start.expr, end.expr);
304 /* Check lower bound. */
305 fault = fold_build2 (LT_EXPR, boolean_type_node, start.expr,
306 build_int_cst (gfc_charlen_type_node, 1));
307 fault = fold_build2 (TRUTH_ANDIF_EXPR, boolean_type_node,
310 asprintf (&msg, "Substring out of bounds: lower bound (%%ld) of '%s' "
311 "is less than one", name);
313 asprintf (&msg, "Substring out of bounds: lower bound (%%ld)"
315 gfc_trans_runtime_check (fault, &se->pre, where, msg,
316 fold_convert (long_integer_type_node,
320 /* Check upper bound. */
321 fault = fold_build2 (GT_EXPR, boolean_type_node, end.expr,
323 fault = fold_build2 (TRUTH_ANDIF_EXPR, boolean_type_node,
326 asprintf (&msg, "Substring out of bounds: upper bound (%%ld) of '%s' "
327 "exceeds string length (%%ld)", name);
329 asprintf (&msg, "Substring out of bounds: upper bound (%%ld) "
330 "exceeds string length (%%ld)");
331 gfc_trans_runtime_check (fault, &se->pre, where, msg,
332 fold_convert (long_integer_type_node, end.expr),
333 fold_convert (long_integer_type_node,
338 tmp = fold_build2 (MINUS_EXPR, gfc_charlen_type_node,
339 build_int_cst (gfc_charlen_type_node, 1),
341 tmp = fold_build2 (PLUS_EXPR, gfc_charlen_type_node, end.expr, tmp);
342 tmp = fold_build2 (MAX_EXPR, gfc_charlen_type_node, tmp,
343 build_int_cst (gfc_charlen_type_node, 0));
344 se->string_length = tmp;
348 /* Convert a derived type component reference. */
351 gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
358 c = ref->u.c.component;
360 gcc_assert (c->backend_decl);
362 field = c->backend_decl;
363 gcc_assert (TREE_CODE (field) == FIELD_DECL);
365 tmp = build3 (COMPONENT_REF, TREE_TYPE (field), decl, field, NULL_TREE);
369 if (c->ts.type == BT_CHARACTER)
371 tmp = c->ts.cl->backend_decl;
372 /* Components must always be constant length. */
373 gcc_assert (tmp && INTEGER_CST_P (tmp));
374 se->string_length = tmp;
377 if (c->pointer && c->dimension == 0 && c->ts.type != BT_CHARACTER)
378 se->expr = build_fold_indirect_ref (se->expr);
382 /* Return the contents of a variable. Also handles reference/pointer
383 variables (all Fortran pointer references are implicit). */
386 gfc_conv_variable (gfc_se * se, gfc_expr * expr)
393 bool alternate_entry;
396 sym = expr->symtree->n.sym;
399 /* Check that something hasn't gone horribly wrong. */
400 gcc_assert (se->ss != gfc_ss_terminator);
401 gcc_assert (se->ss->expr == expr);
403 /* A scalarized term. We already know the descriptor. */
404 se->expr = se->ss->data.info.descriptor;
405 se->string_length = se->ss->string_length;
406 for (ref = se->ss->data.info.ref; ref; ref = ref->next)
407 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
412 tree se_expr = NULL_TREE;
414 se->expr = gfc_get_symbol_decl (sym);
416 /* Deal with references to a parent results or entries by storing
417 the current_function_decl and moving to the parent_decl. */
418 return_value = sym->attr.function && sym->result == sym;
419 alternate_entry = sym->attr.function && sym->attr.entry
420 && sym->result == sym;
421 entry_master = sym->attr.result
422 && sym->ns->proc_name->attr.entry_master
423 && !gfc_return_by_reference (sym->ns->proc_name);
424 parent_decl = DECL_CONTEXT (current_function_decl);
426 if ((se->expr == parent_decl && return_value)
427 || (sym->ns && sym->ns->proc_name
429 && sym->ns->proc_name->backend_decl == parent_decl
430 && (alternate_entry || entry_master)))
435 /* Special case for assigning the return value of a function.
436 Self recursive functions must have an explicit return value. */
437 if (return_value && (se->expr == current_function_decl || parent_flag))
438 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
440 /* Similarly for alternate entry points. */
441 else if (alternate_entry
442 && (sym->ns->proc_name->backend_decl == current_function_decl
445 gfc_entry_list *el = NULL;
447 for (el = sym->ns->entries; el; el = el->next)
450 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
455 else if (entry_master
456 && (sym->ns->proc_name->backend_decl == current_function_decl
458 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
463 /* Procedure actual arguments. */
464 else if (sym->attr.flavor == FL_PROCEDURE
465 && se->expr != current_function_decl)
467 gcc_assert (se->want_pointer);
468 if (!sym->attr.dummy)
470 gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
471 se->expr = build_fold_addr_expr (se->expr);
477 /* Dereference the expression, where needed. Since characters
478 are entirely different from other types, they are treated
480 if (sym->ts.type == BT_CHARACTER)
482 /* Dereference character pointer dummy arguments
484 if ((sym->attr.pointer || sym->attr.allocatable)
486 || sym->attr.function
487 || sym->attr.result))
488 se->expr = build_fold_indirect_ref (se->expr);
491 else if (!sym->attr.value)
493 /* Dereference non-character scalar dummy arguments. */
494 if (sym->attr.dummy && !sym->attr.dimension)
495 se->expr = build_fold_indirect_ref (se->expr);
497 /* Dereference scalar hidden result. */
498 if (gfc_option.flag_f2c && sym->ts.type == BT_COMPLEX
499 && (sym->attr.function || sym->attr.result)
500 && !sym->attr.dimension && !sym->attr.pointer)
501 se->expr = build_fold_indirect_ref (se->expr);
503 /* Dereference non-character pointer variables.
504 These must be dummies, results, or scalars. */
505 if ((sym->attr.pointer || sym->attr.allocatable)
507 || sym->attr.function
509 || !sym->attr.dimension))
510 se->expr = build_fold_indirect_ref (se->expr);
516 /* For character variables, also get the length. */
517 if (sym->ts.type == BT_CHARACTER)
519 /* If the character length of an entry isn't set, get the length from
520 the master function instead. */
521 if (sym->attr.entry && !sym->ts.cl->backend_decl)
522 se->string_length = sym->ns->proc_name->ts.cl->backend_decl;
524 se->string_length = sym->ts.cl->backend_decl;
525 gcc_assert (se->string_length);
533 /* Return the descriptor if that's what we want and this is an array
534 section reference. */
535 if (se->descriptor_only && ref->u.ar.type != AR_ELEMENT)
537 /* TODO: Pointers to single elements of array sections, eg elemental subs. */
538 /* Return the descriptor for array pointers and allocations. */
540 && ref->next == NULL && (se->descriptor_only))
543 gfc_conv_array_ref (se, &ref->u.ar, sym, &expr->where);
544 /* Return a pointer to an element. */
548 gfc_conv_component_ref (se, ref);
552 gfc_conv_substring (se, ref, expr->ts.kind,
553 expr->symtree->name, &expr->where);
562 /* Pointer assignment, allocation or pass by reference. Arrays are handled
564 if (se->want_pointer)
566 if (expr->ts.type == BT_CHARACTER)
567 gfc_conv_string_parameter (se);
569 se->expr = build_fold_addr_expr (se->expr);
574 /* Unary ops are easy... Or they would be if ! was a valid op. */
577 gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
582 gcc_assert (expr->ts.type != BT_CHARACTER);
583 /* Initialize the operand. */
584 gfc_init_se (&operand, se);
585 gfc_conv_expr_val (&operand, expr->value.op.op1);
586 gfc_add_block_to_block (&se->pre, &operand.pre);
588 type = gfc_typenode_for_spec (&expr->ts);
590 /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
591 We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
592 All other unary operators have an equivalent GIMPLE unary operator. */
593 if (code == TRUTH_NOT_EXPR)
594 se->expr = build2 (EQ_EXPR, type, operand.expr,
595 build_int_cst (type, 0));
597 se->expr = build1 (code, type, operand.expr);
601 /* Expand power operator to optimal multiplications when a value is raised
602 to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
603 Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
604 Programming", 3rd Edition, 1998. */
606 /* This code is mostly duplicated from expand_powi in the backend.
607 We establish the "optimal power tree" lookup table with the defined size.
608 The items in the table are the exponents used to calculate the index
609 exponents. Any integer n less than the value can get an "addition chain",
610 with the first node being one. */
611 #define POWI_TABLE_SIZE 256
613 /* The table is from builtins.c. */
614 static const unsigned char powi_table[POWI_TABLE_SIZE] =
616 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
617 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
618 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
619 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
620 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
621 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
622 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
623 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
624 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
625 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
626 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
627 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
628 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
629 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
630 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
631 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
632 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
633 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
634 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
635 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
636 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
637 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
638 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
639 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
640 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
641 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
642 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
643 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
644 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
645 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
646 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
647 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
650 /* If n is larger than lookup table's max index, we use the "window
652 #define POWI_WINDOW_SIZE 3
654 /* Recursive function to expand the power operator. The temporary
655 values are put in tmpvar. The function returns tmpvar[1] ** n. */
657 gfc_conv_powi (gfc_se * se, unsigned HOST_WIDE_INT n, tree * tmpvar)
664 if (n < POWI_TABLE_SIZE)
669 op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
670 op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
674 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
675 op0 = gfc_conv_powi (se, n - digit, tmpvar);
676 op1 = gfc_conv_powi (se, digit, tmpvar);
680 op0 = gfc_conv_powi (se, n >> 1, tmpvar);
684 tmp = fold_build2 (MULT_EXPR, TREE_TYPE (op0), op0, op1);
685 tmp = gfc_evaluate_now (tmp, &se->pre);
687 if (n < POWI_TABLE_SIZE)
694 /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
695 return 1. Else return 0 and a call to runtime library functions
696 will have to be built. */
698 gfc_conv_cst_int_power (gfc_se * se, tree lhs, tree rhs)
703 tree vartmp[POWI_TABLE_SIZE];
705 unsigned HOST_WIDE_INT n;
708 /* If exponent is too large, we won't expand it anyway, so don't bother
709 with large integer values. */
710 if (!double_int_fits_in_shwi_p (TREE_INT_CST (rhs)))
713 m = double_int_to_shwi (TREE_INT_CST (rhs));
714 /* There's no ABS for HOST_WIDE_INT, so here we go. It also takes care
715 of the asymmetric range of the integer type. */
716 n = (unsigned HOST_WIDE_INT) (m < 0 ? -m : m);
718 type = TREE_TYPE (lhs);
719 sgn = tree_int_cst_sgn (rhs);
721 if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
722 || optimize_size) && (m > 2 || m < -1))
728 se->expr = gfc_build_const (type, integer_one_node);
732 /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1. */
733 if ((sgn == -1) && (TREE_CODE (type) == INTEGER_TYPE))
735 tmp = build2 (EQ_EXPR, boolean_type_node, lhs,
736 build_int_cst (TREE_TYPE (lhs), -1));
737 cond = build2 (EQ_EXPR, boolean_type_node, lhs,
738 build_int_cst (TREE_TYPE (lhs), 1));
741 result = (lhs == 1 || lhs == -1) ? 1 : 0. */
744 tmp = build2 (TRUTH_OR_EXPR, boolean_type_node, tmp, cond);
745 se->expr = build3 (COND_EXPR, type, tmp, build_int_cst (type, 1),
746 build_int_cst (type, 0));
750 result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0. */
751 tmp = build3 (COND_EXPR, type, tmp, build_int_cst (type, -1),
752 build_int_cst (type, 0));
753 se->expr = build3 (COND_EXPR, type, cond, build_int_cst (type, 1), tmp);
757 memset (vartmp, 0, sizeof (vartmp));
761 tmp = gfc_build_const (type, integer_one_node);
762 vartmp[1] = build2 (RDIV_EXPR, type, tmp, vartmp[1]);
765 se->expr = gfc_conv_powi (se, n, vartmp);
771 /* Power op (**). Constant integer exponent has special handling. */
774 gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
776 tree gfc_int4_type_node;
783 gfc_init_se (&lse, se);
784 gfc_conv_expr_val (&lse, expr->value.op.op1);
785 lse.expr = gfc_evaluate_now (lse.expr, &lse.pre);
786 gfc_add_block_to_block (&se->pre, &lse.pre);
788 gfc_init_se (&rse, se);
789 gfc_conv_expr_val (&rse, expr->value.op.op2);
790 gfc_add_block_to_block (&se->pre, &rse.pre);
792 if (expr->value.op.op2->ts.type == BT_INTEGER
793 && expr->value.op.op2->expr_type == EXPR_CONSTANT)
794 if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
797 gfc_int4_type_node = gfc_get_int_type (4);
799 kind = expr->value.op.op1->ts.kind;
800 switch (expr->value.op.op2->ts.type)
803 ikind = expr->value.op.op2->ts.kind;
808 rse.expr = convert (gfc_int4_type_node, rse.expr);
830 if (expr->value.op.op1->ts.type == BT_INTEGER)
831 lse.expr = convert (gfc_int4_type_node, lse.expr);
856 switch (expr->value.op.op1->ts.type)
859 if (kind == 3) /* Case 16 was not handled properly above. */
861 fndecl = gfor_fndecl_math_powi[kind][ikind].integer;
865 /* Use builtins for real ** int4. */
871 fndecl = built_in_decls[BUILT_IN_POWIF];
875 fndecl = built_in_decls[BUILT_IN_POWI];
880 fndecl = built_in_decls[BUILT_IN_POWIL];
888 fndecl = gfor_fndecl_math_powi[kind][ikind].real;
892 fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
904 fndecl = built_in_decls[BUILT_IN_POWF];
907 fndecl = built_in_decls[BUILT_IN_POW];
911 fndecl = built_in_decls[BUILT_IN_POWL];
922 fndecl = gfor_fndecl_math_cpowf;
925 fndecl = gfor_fndecl_math_cpow;
928 fndecl = gfor_fndecl_math_cpowl10;
931 fndecl = gfor_fndecl_math_cpowl16;
943 se->expr = build_call_expr (fndecl, 2, lse.expr, rse.expr);
947 /* Generate code to allocate a string temporary. */
950 gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
955 gcc_assert (TREE_TYPE (len) == gfc_charlen_type_node);
957 if (gfc_can_put_var_on_stack (len))
959 /* Create a temporary variable to hold the result. */
960 tmp = fold_build2 (MINUS_EXPR, gfc_charlen_type_node, len,
961 build_int_cst (gfc_charlen_type_node, 1));
962 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node, tmp);
963 tmp = build_array_type (gfc_character1_type_node, tmp);
964 var = gfc_create_var (tmp, "str");
965 var = gfc_build_addr_expr (type, var);
969 /* Allocate a temporary to hold the result. */
970 var = gfc_create_var (type, "pstr");
971 tmp = gfc_call_malloc (&se->pre, type, len);
972 gfc_add_modify_expr (&se->pre, var, tmp);
974 /* Free the temporary afterwards. */
975 tmp = gfc_call_free (convert (pvoid_type_node, var));
976 gfc_add_expr_to_block (&se->post, tmp);
983 /* Handle a string concatenation operation. A temporary will be allocated to
987 gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
996 gcc_assert (expr->value.op.op1->ts.type == BT_CHARACTER
997 && expr->value.op.op2->ts.type == BT_CHARACTER);
999 gfc_init_se (&lse, se);
1000 gfc_conv_expr (&lse, expr->value.op.op1);
1001 gfc_conv_string_parameter (&lse);
1002 gfc_init_se (&rse, se);
1003 gfc_conv_expr (&rse, expr->value.op.op2);
1004 gfc_conv_string_parameter (&rse);
1006 gfc_add_block_to_block (&se->pre, &lse.pre);
1007 gfc_add_block_to_block (&se->pre, &rse.pre);
1009 type = gfc_get_character_type (expr->ts.kind, expr->ts.cl);
1010 len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
1011 if (len == NULL_TREE)
1013 len = fold_build2 (PLUS_EXPR, TREE_TYPE (lse.string_length),
1014 lse.string_length, rse.string_length);
1017 type = build_pointer_type (type);
1019 var = gfc_conv_string_tmp (se, type, len);
1021 /* Do the actual concatenation. */
1022 tmp = build_call_expr (gfor_fndecl_concat_string, 6,
1024 lse.string_length, lse.expr,
1025 rse.string_length, rse.expr);
1026 gfc_add_expr_to_block (&se->pre, tmp);
1028 /* Add the cleanup for the operands. */
1029 gfc_add_block_to_block (&se->pre, &rse.post);
1030 gfc_add_block_to_block (&se->pre, &lse.post);
1033 se->string_length = len;
1036 /* Translates an op expression. Common (binary) cases are handled by this
1037 function, others are passed on. Recursion is used in either case.
1038 We use the fact that (op1.ts == op2.ts) (except for the power
1040 Operators need no special handling for scalarized expressions as long as
1041 they call gfc_conv_simple_val to get their operands.
1042 Character strings get special handling. */
1045 gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
1047 enum tree_code code;
1056 switch (expr->value.op.operator)
1058 case INTRINSIC_UPLUS:
1059 case INTRINSIC_PARENTHESES:
1060 gfc_conv_expr (se, expr->value.op.op1);
1063 case INTRINSIC_UMINUS:
1064 gfc_conv_unary_op (NEGATE_EXPR, se, expr);
1068 gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
1071 case INTRINSIC_PLUS:
1075 case INTRINSIC_MINUS:
1079 case INTRINSIC_TIMES:
1083 case INTRINSIC_DIVIDE:
1084 /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
1085 an integer, we must round towards zero, so we use a
1087 if (expr->ts.type == BT_INTEGER)
1088 code = TRUNC_DIV_EXPR;
1093 case INTRINSIC_POWER:
1094 gfc_conv_power_op (se, expr);
1097 case INTRINSIC_CONCAT:
1098 gfc_conv_concat_op (se, expr);
1102 code = TRUTH_ANDIF_EXPR;
1107 code = TRUTH_ORIF_EXPR;
1111 /* EQV and NEQV only work on logicals, but since we represent them
1112 as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE. */
1114 case INTRINSIC_EQ_OS:
1122 case INTRINSIC_NE_OS:
1123 case INTRINSIC_NEQV:
1130 case INTRINSIC_GT_OS:
1137 case INTRINSIC_GE_OS:
1144 case INTRINSIC_LT_OS:
1151 case INTRINSIC_LE_OS:
1157 case INTRINSIC_USER:
1158 case INTRINSIC_ASSIGN:
1159 /* These should be converted into function calls by the frontend. */
1163 fatal_error ("Unknown intrinsic op");
1167 /* The only exception to this is **, which is handled separately anyway. */
1168 gcc_assert (expr->value.op.op1->ts.type == expr->value.op.op2->ts.type);
1170 if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
1174 gfc_init_se (&lse, se);
1175 gfc_conv_expr (&lse, expr->value.op.op1);
1176 gfc_add_block_to_block (&se->pre, &lse.pre);
1179 gfc_init_se (&rse, se);
1180 gfc_conv_expr (&rse, expr->value.op.op2);
1181 gfc_add_block_to_block (&se->pre, &rse.pre);
1185 gfc_conv_string_parameter (&lse);
1186 gfc_conv_string_parameter (&rse);
1188 lse.expr = gfc_build_compare_string (lse.string_length, lse.expr,
1189 rse.string_length, rse.expr);
1190 rse.expr = build_int_cst (TREE_TYPE (lse.expr), 0);
1191 gfc_add_block_to_block (&lse.post, &rse.post);
1194 type = gfc_typenode_for_spec (&expr->ts);
1198 /* The result of logical ops is always boolean_type_node. */
1199 tmp = fold_build2 (code, boolean_type_node, lse.expr, rse.expr);
1200 se->expr = convert (type, tmp);
1203 se->expr = fold_build2 (code, type, lse.expr, rse.expr);
1205 /* Add the post blocks. */
1206 gfc_add_block_to_block (&se->post, &rse.post);
1207 gfc_add_block_to_block (&se->post, &lse.post);
1210 /* If a string's length is one, we convert it to a single character. */
1213 gfc_to_single_character (tree len, tree str)
1215 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str)));
1217 if (INTEGER_CST_P (len) && TREE_INT_CST_LOW (len) == 1
1218 && TREE_INT_CST_HIGH (len) == 0)
1220 str = fold_convert (pchar_type_node, str);
1221 return build_fold_indirect_ref (str);
1229 gfc_conv_scalar_char_value (gfc_symbol *sym, gfc_se *se, gfc_expr **expr)
1232 if (sym->backend_decl)
1234 /* This becomes the nominal_type in
1235 function.c:assign_parm_find_data_types. */
1236 TREE_TYPE (sym->backend_decl) = unsigned_char_type_node;
1237 /* This becomes the passed_type in
1238 function.c:assign_parm_find_data_types. C promotes char to
1239 integer for argument passing. */
1240 DECL_ARG_TYPE (sym->backend_decl) = unsigned_type_node;
1242 DECL_BY_REFERENCE (sym->backend_decl) = 0;
1247 /* If we have a constant character expression, make it into an
1249 if ((*expr)->expr_type == EXPR_CONSTANT)
1253 *expr = gfc_int_expr ((int)(*expr)->value.character.string[0]);
1254 if ((*expr)->ts.kind != gfc_c_int_kind)
1256 /* The expr needs to be compatible with a C int. If the
1257 conversion fails, then the 2 causes an ICE. */
1258 ts.type = BT_INTEGER;
1259 ts.kind = gfc_c_int_kind;
1260 gfc_convert_type (*expr, &ts, 2);
1263 else if (se != NULL && (*expr)->expr_type == EXPR_VARIABLE)
1265 if ((*expr)->ref == NULL)
1267 se->expr = gfc_to_single_character
1268 (build_int_cst (integer_type_node, 1),
1269 gfc_build_addr_expr (pchar_type_node,
1271 ((*expr)->symtree->n.sym)));
1275 gfc_conv_variable (se, *expr);
1276 se->expr = gfc_to_single_character
1277 (build_int_cst (integer_type_node, 1),
1278 gfc_build_addr_expr (pchar_type_node, se->expr));
1285 /* Compare two strings. If they are all single characters, the result is the
1286 subtraction of them. Otherwise, we build a library call. */
1289 gfc_build_compare_string (tree len1, tree str1, tree len2, tree str2)
1295 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1)));
1296 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2)));
1298 sc1 = gfc_to_single_character (len1, str1);
1299 sc2 = gfc_to_single_character (len2, str2);
1301 /* Deal with single character specially. */
1302 if (sc1 != NULL_TREE && sc2 != NULL_TREE)
1304 sc1 = fold_convert (integer_type_node, sc1);
1305 sc2 = fold_convert (integer_type_node, sc2);
1306 tmp = fold_build2 (MINUS_EXPR, integer_type_node, sc1, sc2);
1309 /* Build a call for the comparison. */
1310 tmp = build_call_expr (gfor_fndecl_compare_string, 4,
1311 len1, str1, len2, str2);
1316 gfc_conv_function_val (gfc_se * se, gfc_symbol * sym)
1320 if (sym->attr.dummy)
1322 tmp = gfc_get_symbol_decl (sym);
1323 gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE
1324 && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) == FUNCTION_TYPE);
1328 if (!sym->backend_decl)
1329 sym->backend_decl = gfc_get_extern_function_decl (sym);
1331 tmp = sym->backend_decl;
1332 if (sym->attr.cray_pointee)
1333 tmp = convert (build_pointer_type (TREE_TYPE (tmp)),
1334 gfc_get_symbol_decl (sym->cp_pointer));
1335 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
1337 gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
1338 tmp = build_fold_addr_expr (tmp);
1345 /* Translate the call for an elemental subroutine call used in an operator
1346 assignment. This is a simplified version of gfc_conv_function_call. */
1349 gfc_conv_operator_assign (gfc_se *lse, gfc_se *rse, gfc_symbol *sym)
1356 /* Only elemental subroutines with two arguments. */
1357 gcc_assert (sym->attr.elemental && sym->attr.subroutine);
1358 gcc_assert (sym->formal->next->next == NULL);
1360 gfc_init_block (&block);
1362 gfc_add_block_to_block (&block, &lse->pre);
1363 gfc_add_block_to_block (&block, &rse->pre);
1365 /* Build the argument list for the call, including hidden string lengths. */
1366 args = gfc_chainon_list (NULL_TREE, build_fold_addr_expr (lse->expr));
1367 args = gfc_chainon_list (args, build_fold_addr_expr (rse->expr));
1368 if (lse->string_length != NULL_TREE)
1369 args = gfc_chainon_list (args, lse->string_length);
1370 if (rse->string_length != NULL_TREE)
1371 args = gfc_chainon_list (args, rse->string_length);
1373 /* Build the function call. */
1374 gfc_init_se (&se, NULL);
1375 gfc_conv_function_val (&se, sym);
1376 tmp = TREE_TYPE (TREE_TYPE (TREE_TYPE (se.expr)));
1377 tmp = build_call_list (tmp, se.expr, args);
1378 gfc_add_expr_to_block (&block, tmp);
1380 gfc_add_block_to_block (&block, &lse->post);
1381 gfc_add_block_to_block (&block, &rse->post);
1383 return gfc_finish_block (&block);
1387 /* Initialize MAPPING. */
1390 gfc_init_interface_mapping (gfc_interface_mapping * mapping)
1392 mapping->syms = NULL;
1393 mapping->charlens = NULL;
1397 /* Free all memory held by MAPPING (but not MAPPING itself). */
1400 gfc_free_interface_mapping (gfc_interface_mapping * mapping)
1402 gfc_interface_sym_mapping *sym;
1403 gfc_interface_sym_mapping *nextsym;
1405 gfc_charlen *nextcl;
1407 for (sym = mapping->syms; sym; sym = nextsym)
1409 nextsym = sym->next;
1410 gfc_free_symbol (sym->new->n.sym);
1411 gfc_free (sym->new);
1414 for (cl = mapping->charlens; cl; cl = nextcl)
1417 gfc_free_expr (cl->length);
1423 /* Return a copy of gfc_charlen CL. Add the returned structure to
1424 MAPPING so that it will be freed by gfc_free_interface_mapping. */
1426 static gfc_charlen *
1427 gfc_get_interface_mapping_charlen (gfc_interface_mapping * mapping,
1432 new = gfc_get_charlen ();
1433 new->next = mapping->charlens;
1434 new->length = gfc_copy_expr (cl->length);
1436 mapping->charlens = new;
1441 /* A subroutine of gfc_add_interface_mapping. Return a descriptorless
1442 array variable that can be used as the actual argument for dummy
1443 argument SYM. Add any initialization code to BLOCK. PACKED is as
1444 for gfc_get_nodesc_array_type and DATA points to the first element
1445 in the passed array. */
1448 gfc_get_interface_mapping_array (stmtblock_t * block, gfc_symbol * sym,
1449 gfc_packed packed, tree data)
1454 type = gfc_typenode_for_spec (&sym->ts);
1455 type = gfc_get_nodesc_array_type (type, sym->as, packed);
1457 var = gfc_create_var (type, "ifm");
1458 gfc_add_modify_expr (block, var, fold_convert (type, data));
1464 /* A subroutine of gfc_add_interface_mapping. Set the stride, upper bounds
1465 and offset of descriptorless array type TYPE given that it has the same
1466 size as DESC. Add any set-up code to BLOCK. */
1469 gfc_set_interface_mapping_bounds (stmtblock_t * block, tree type, tree desc)
1476 offset = gfc_index_zero_node;
1477 for (n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
1479 dim = gfc_rank_cst[n];
1480 GFC_TYPE_ARRAY_STRIDE (type, n) = gfc_conv_array_stride (desc, n);
1481 if (GFC_TYPE_ARRAY_LBOUND (type, n) == NULL_TREE)
1483 GFC_TYPE_ARRAY_LBOUND (type, n)
1484 = gfc_conv_descriptor_lbound (desc, dim);
1485 GFC_TYPE_ARRAY_UBOUND (type, n)
1486 = gfc_conv_descriptor_ubound (desc, dim);
1488 else if (GFC_TYPE_ARRAY_UBOUND (type, n) == NULL_TREE)
1490 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
1491 gfc_conv_descriptor_ubound (desc, dim),
1492 gfc_conv_descriptor_lbound (desc, dim));
1493 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1494 GFC_TYPE_ARRAY_LBOUND (type, n),
1496 tmp = gfc_evaluate_now (tmp, block);
1497 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1499 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type,
1500 GFC_TYPE_ARRAY_LBOUND (type, n),
1501 GFC_TYPE_ARRAY_STRIDE (type, n));
1502 offset = fold_build2 (MINUS_EXPR, gfc_array_index_type, offset, tmp);
1504 offset = gfc_evaluate_now (offset, block);
1505 GFC_TYPE_ARRAY_OFFSET (type) = offset;
1509 /* Extend MAPPING so that it maps dummy argument SYM to the value stored
1510 in SE. The caller may still use se->expr and se->string_length after
1511 calling this function. */
1514 gfc_add_interface_mapping (gfc_interface_mapping * mapping,
1515 gfc_symbol * sym, gfc_se * se)
1517 gfc_interface_sym_mapping *sm;
1521 gfc_symbol *new_sym;
1523 gfc_symtree *new_symtree;
1525 /* Create a new symbol to represent the actual argument. */
1526 new_sym = gfc_new_symbol (sym->name, NULL);
1527 new_sym->ts = sym->ts;
1528 new_sym->attr.referenced = 1;
1529 new_sym->attr.dimension = sym->attr.dimension;
1530 new_sym->attr.pointer = sym->attr.pointer;
1531 new_sym->attr.allocatable = sym->attr.allocatable;
1532 new_sym->attr.flavor = sym->attr.flavor;
1534 /* Create a fake symtree for it. */
1536 new_symtree = gfc_new_symtree (&root, sym->name);
1537 new_symtree->n.sym = new_sym;
1538 gcc_assert (new_symtree == root);
1540 /* Create a dummy->actual mapping. */
1541 sm = gfc_getmem (sizeof (*sm));
1542 sm->next = mapping->syms;
1544 sm->new = new_symtree;
1547 /* Stabilize the argument's value. */
1548 se->expr = gfc_evaluate_now (se->expr, &se->pre);
1550 if (sym->ts.type == BT_CHARACTER)
1552 /* Create a copy of the dummy argument's length. */
1553 new_sym->ts.cl = gfc_get_interface_mapping_charlen (mapping, sym->ts.cl);
1555 /* If the length is specified as "*", record the length that
1556 the caller is passing. We should use the callee's length
1557 in all other cases. */
1558 if (!new_sym->ts.cl->length)
1560 se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
1561 new_sym->ts.cl->backend_decl = se->string_length;
1565 /* Use the passed value as-is if the argument is a function. */
1566 if (sym->attr.flavor == FL_PROCEDURE)
1569 /* If the argument is either a string or a pointer to a string,
1570 convert it to a boundless character type. */
1571 else if (!sym->attr.dimension && sym->ts.type == BT_CHARACTER)
1573 tmp = gfc_get_character_type_len (sym->ts.kind, NULL);
1574 tmp = build_pointer_type (tmp);
1575 if (sym->attr.pointer)
1576 value = build_fold_indirect_ref (se->expr);
1579 value = fold_convert (tmp, value);
1582 /* If the argument is a scalar, a pointer to an array or an allocatable,
1584 else if (!sym->attr.dimension || sym->attr.pointer || sym->attr.allocatable)
1585 value = build_fold_indirect_ref (se->expr);
1587 /* For character(*), use the actual argument's descriptor. */
1588 else if (sym->ts.type == BT_CHARACTER && !new_sym->ts.cl->length)
1589 value = build_fold_indirect_ref (se->expr);
1591 /* If the argument is an array descriptor, use it to determine
1592 information about the actual argument's shape. */
1593 else if (POINTER_TYPE_P (TREE_TYPE (se->expr))
1594 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
1596 /* Get the actual argument's descriptor. */
1597 desc = build_fold_indirect_ref (se->expr);
1599 /* Create the replacement variable. */
1600 tmp = gfc_conv_descriptor_data_get (desc);
1601 value = gfc_get_interface_mapping_array (&se->pre, sym,
1604 /* Use DESC to work out the upper bounds, strides and offset. */
1605 gfc_set_interface_mapping_bounds (&se->pre, TREE_TYPE (value), desc);
1608 /* Otherwise we have a packed array. */
1609 value = gfc_get_interface_mapping_array (&se->pre, sym,
1610 PACKED_FULL, se->expr);
1612 new_sym->backend_decl = value;
1616 /* Called once all dummy argument mappings have been added to MAPPING,
1617 but before the mapping is used to evaluate expressions. Pre-evaluate
1618 the length of each argument, adding any initialization code to PRE and
1619 any finalization code to POST. */
1622 gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
1623 stmtblock_t * pre, stmtblock_t * post)
1625 gfc_interface_sym_mapping *sym;
1629 for (sym = mapping->syms; sym; sym = sym->next)
1630 if (sym->new->n.sym->ts.type == BT_CHARACTER
1631 && !sym->new->n.sym->ts.cl->backend_decl)
1633 expr = sym->new->n.sym->ts.cl->length;
1634 gfc_apply_interface_mapping_to_expr (mapping, expr);
1635 gfc_init_se (&se, NULL);
1636 gfc_conv_expr (&se, expr);
1638 se.expr = gfc_evaluate_now (se.expr, &se.pre);
1639 gfc_add_block_to_block (pre, &se.pre);
1640 gfc_add_block_to_block (post, &se.post);
1642 sym->new->n.sym->ts.cl->backend_decl = se.expr;
1647 /* Like gfc_apply_interface_mapping_to_expr, but applied to
1651 gfc_apply_interface_mapping_to_cons (gfc_interface_mapping * mapping,
1652 gfc_constructor * c)
1654 for (; c; c = c->next)
1656 gfc_apply_interface_mapping_to_expr (mapping, c->expr);
1659 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->start);
1660 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->end);
1661 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->step);
1667 /* Like gfc_apply_interface_mapping_to_expr, but applied to
1671 gfc_apply_interface_mapping_to_ref (gfc_interface_mapping * mapping,
1676 for (; ref; ref = ref->next)
1680 for (n = 0; n < ref->u.ar.dimen; n++)
1682 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.start[n]);
1683 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.end[n]);
1684 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.stride[n]);
1686 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.offset);
1693 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.start);
1694 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.end);
1700 /* EXPR is a copy of an expression that appeared in the interface
1701 associated with MAPPING. Walk it recursively looking for references to
1702 dummy arguments that MAPPING maps to actual arguments. Replace each such
1703 reference with a reference to the associated actual argument. */
1706 gfc_apply_interface_mapping_to_expr (gfc_interface_mapping * mapping,
1709 gfc_interface_sym_mapping *sym;
1710 gfc_actual_arglist *actual;
1711 int seen_result = 0;
1716 /* Copying an expression does not copy its length, so do that here. */
1717 if (expr->ts.type == BT_CHARACTER && expr->ts.cl)
1719 expr->ts.cl = gfc_get_interface_mapping_charlen (mapping, expr->ts.cl);
1720 gfc_apply_interface_mapping_to_expr (mapping, expr->ts.cl->length);
1723 /* Apply the mapping to any references. */
1724 gfc_apply_interface_mapping_to_ref (mapping, expr->ref);
1726 /* ...and to the expression's symbol, if it has one. */
1728 for (sym = mapping->syms; sym; sym = sym->next)
1729 if (sym->old == expr->symtree->n.sym)
1730 expr->symtree = sym->new;
1732 /* ...and to subexpressions in expr->value. */
1733 switch (expr->expr_type)
1736 if (expr->symtree->n.sym->attr.result)
1740 case EXPR_SUBSTRING:
1744 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op1);
1745 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op2);
1749 if (expr->value.function.esym == NULL
1750 && expr->value.function.isym != NULL
1751 && expr->value.function.isym->id == GFC_ISYM_LEN
1752 && expr->value.function.actual->expr->expr_type == EXPR_VARIABLE
1753 && gfc_apply_interface_mapping_to_expr (mapping,
1754 expr->value.function.actual->expr))
1757 new_expr = gfc_copy_expr (expr->value.function.actual->expr->ts.cl->length);
1759 gfc_free (new_expr);
1760 gfc_apply_interface_mapping_to_expr (mapping, expr);
1764 for (sym = mapping->syms; sym; sym = sym->next)
1765 if (sym->old == expr->value.function.esym)
1766 expr->value.function.esym = sym->new->n.sym;
1768 for (actual = expr->value.function.actual; actual; actual = actual->next)
1769 gfc_apply_interface_mapping_to_expr (mapping, actual->expr);
1773 case EXPR_STRUCTURE:
1774 gfc_apply_interface_mapping_to_cons (mapping, expr->value.constructor);
1781 /* Evaluate interface expression EXPR using MAPPING. Store the result
1785 gfc_apply_interface_mapping (gfc_interface_mapping * mapping,
1786 gfc_se * se, gfc_expr * expr)
1788 expr = gfc_copy_expr (expr);
1789 gfc_apply_interface_mapping_to_expr (mapping, expr);
1790 gfc_conv_expr (se, expr);
1791 se->expr = gfc_evaluate_now (se->expr, &se->pre);
1792 gfc_free_expr (expr);
1796 /* Returns a reference to a temporary array into which a component of
1797 an actual argument derived type array is copied and then returned
1798 after the function call. */
1800 gfc_conv_subref_array_arg (gfc_se * parmse, gfc_expr * expr,
1801 int g77, sym_intent intent)
1817 gcc_assert (expr->expr_type == EXPR_VARIABLE);
1819 gfc_init_se (&lse, NULL);
1820 gfc_init_se (&rse, NULL);
1822 /* Walk the argument expression. */
1823 rss = gfc_walk_expr (expr);
1825 gcc_assert (rss != gfc_ss_terminator);
1827 /* Initialize the scalarizer. */
1828 gfc_init_loopinfo (&loop);
1829 gfc_add_ss_to_loop (&loop, rss);
1831 /* Calculate the bounds of the scalarization. */
1832 gfc_conv_ss_startstride (&loop);
1834 /* Build an ss for the temporary. */
1835 if (expr->ts.type == BT_CHARACTER && !expr->ts.cl->backend_decl)
1836 gfc_conv_string_length (expr->ts.cl, &parmse->pre);
1838 base_type = gfc_typenode_for_spec (&expr->ts);
1839 if (GFC_ARRAY_TYPE_P (base_type)
1840 || GFC_DESCRIPTOR_TYPE_P (base_type))
1841 base_type = gfc_get_element_type (base_type);
1843 loop.temp_ss = gfc_get_ss ();;
1844 loop.temp_ss->type = GFC_SS_TEMP;
1845 loop.temp_ss->data.temp.type = base_type;
1847 if (expr->ts.type == BT_CHARACTER)
1848 loop.temp_ss->string_length = expr->ts.cl->backend_decl;
1850 loop.temp_ss->string_length = NULL;
1852 parmse->string_length = loop.temp_ss->string_length;
1853 loop.temp_ss->data.temp.dimen = loop.dimen;
1854 loop.temp_ss->next = gfc_ss_terminator;
1856 /* Associate the SS with the loop. */
1857 gfc_add_ss_to_loop (&loop, loop.temp_ss);
1859 /* Setup the scalarizing loops. */
1860 gfc_conv_loop_setup (&loop);
1862 /* Pass the temporary descriptor back to the caller. */
1863 info = &loop.temp_ss->data.info;
1864 parmse->expr = info->descriptor;
1866 /* Setup the gfc_se structures. */
1867 gfc_copy_loopinfo_to_se (&lse, &loop);
1868 gfc_copy_loopinfo_to_se (&rse, &loop);
1871 lse.ss = loop.temp_ss;
1872 gfc_mark_ss_chain_used (rss, 1);
1873 gfc_mark_ss_chain_used (loop.temp_ss, 1);
1875 /* Start the scalarized loop body. */
1876 gfc_start_scalarized_body (&loop, &body);
1878 /* Translate the expression. */
1879 gfc_conv_expr (&rse, expr);
1881 gfc_conv_tmp_array_ref (&lse);
1882 gfc_advance_se_ss_chain (&lse);
1884 if (intent != INTENT_OUT)
1886 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, true, false);
1887 gfc_add_expr_to_block (&body, tmp);
1888 gcc_assert (rse.ss == gfc_ss_terminator);
1889 gfc_trans_scalarizing_loops (&loop, &body);
1893 /* Make sure that the temporary declaration survives by merging
1894 all the loop declarations into the current context. */
1895 for (n = 0; n < loop.dimen; n++)
1897 gfc_merge_block_scope (&body);
1898 body = loop.code[loop.order[n]];
1900 gfc_merge_block_scope (&body);
1903 /* Add the post block after the second loop, so that any
1904 freeing of allocated memory is done at the right time. */
1905 gfc_add_block_to_block (&parmse->pre, &loop.pre);
1907 /**********Copy the temporary back again.*********/
1909 gfc_init_se (&lse, NULL);
1910 gfc_init_se (&rse, NULL);
1912 /* Walk the argument expression. */
1913 lss = gfc_walk_expr (expr);
1914 rse.ss = loop.temp_ss;
1917 /* Initialize the scalarizer. */
1918 gfc_init_loopinfo (&loop2);
1919 gfc_add_ss_to_loop (&loop2, lss);
1921 /* Calculate the bounds of the scalarization. */
1922 gfc_conv_ss_startstride (&loop2);
1924 /* Setup the scalarizing loops. */
1925 gfc_conv_loop_setup (&loop2);
1927 gfc_copy_loopinfo_to_se (&lse, &loop2);
1928 gfc_copy_loopinfo_to_se (&rse, &loop2);
1930 gfc_mark_ss_chain_used (lss, 1);
1931 gfc_mark_ss_chain_used (loop.temp_ss, 1);
1933 /* Declare the variable to hold the temporary offset and start the
1934 scalarized loop body. */
1935 offset = gfc_create_var (gfc_array_index_type, NULL);
1936 gfc_start_scalarized_body (&loop2, &body);
1938 /* Build the offsets for the temporary from the loop variables. The
1939 temporary array has lbounds of zero and strides of one in all
1940 dimensions, so this is very simple. The offset is only computed
1941 outside the innermost loop, so the overall transfer could be
1942 optimized further. */
1943 info = &rse.ss->data.info;
1945 tmp_index = gfc_index_zero_node;
1946 for (n = info->dimen - 1; n > 0; n--)
1949 tmp = rse.loop->loopvar[n];
1950 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
1951 tmp, rse.loop->from[n]);
1952 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1955 tmp_str = fold_build2 (MINUS_EXPR, gfc_array_index_type,
1956 rse.loop->to[n-1], rse.loop->from[n-1]);
1957 tmp_str = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1958 tmp_str, gfc_index_one_node);
1960 tmp_index = fold_build2 (MULT_EXPR, gfc_array_index_type,
1964 tmp_index = fold_build2 (MINUS_EXPR, gfc_array_index_type,
1965 tmp_index, rse.loop->from[0]);
1966 gfc_add_modify_expr (&rse.loop->code[0], offset, tmp_index);
1968 tmp_index = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1969 rse.loop->loopvar[0], offset);
1971 /* Now use the offset for the reference. */
1972 tmp = build_fold_indirect_ref (info->data);
1973 rse.expr = gfc_build_array_ref (tmp, tmp_index, NULL);
1975 if (expr->ts.type == BT_CHARACTER)
1976 rse.string_length = expr->ts.cl->backend_decl;
1978 gfc_conv_expr (&lse, expr);
1980 gcc_assert (lse.ss == gfc_ss_terminator);
1982 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, false);
1983 gfc_add_expr_to_block (&body, tmp);
1985 /* Generate the copying loops. */
1986 gfc_trans_scalarizing_loops (&loop2, &body);
1988 /* Wrap the whole thing up by adding the second loop to the post-block
1989 and following it by the post-block of the first loop. In this way,
1990 if the temporary needs freeing, it is done after use! */
1991 if (intent != INTENT_IN)
1993 gfc_add_block_to_block (&parmse->post, &loop2.pre);
1994 gfc_add_block_to_block (&parmse->post, &loop2.post);
1997 gfc_add_block_to_block (&parmse->post, &loop.post);
1999 gfc_cleanup_loop (&loop);
2000 gfc_cleanup_loop (&loop2);
2002 /* Pass the string length to the argument expression. */
2003 if (expr->ts.type == BT_CHARACTER)
2004 parmse->string_length = expr->ts.cl->backend_decl;
2006 /* We want either the address for the data or the address of the descriptor,
2007 depending on the mode of passing array arguments. */
2009 parmse->expr = gfc_conv_descriptor_data_get (parmse->expr);
2011 parmse->expr = build_fold_addr_expr (parmse->expr);
2017 /* Generate the code for argument list functions. */
2020 conv_arglist_function (gfc_se *se, gfc_expr *expr, const char *name)
2022 /* Pass by value for g77 %VAL(arg), pass the address
2023 indirectly for %LOC, else by reference. Thus %REF
2024 is a "do-nothing" and %LOC is the same as an F95
2026 if (strncmp (name, "%VAL", 4) == 0)
2027 gfc_conv_expr (se, expr);
2028 else if (strncmp (name, "%LOC", 4) == 0)
2030 gfc_conv_expr_reference (se, expr);
2031 se->expr = gfc_build_addr_expr (NULL, se->expr);
2033 else if (strncmp (name, "%REF", 4) == 0)
2034 gfc_conv_expr_reference (se, expr);
2036 gfc_error ("Unknown argument list function at %L", &expr->where);
2040 /* Generate code for a procedure call. Note can return se->post != NULL.
2041 If se->direct_byref is set then se->expr contains the return parameter.
2042 Return nonzero, if the call has alternate specifiers. */
2045 gfc_conv_function_call (gfc_se * se, gfc_symbol * sym,
2046 gfc_actual_arglist * arg, tree append_args)
2048 gfc_interface_mapping mapping;
2062 gfc_formal_arglist *formal;
2063 int has_alternate_specifier = 0;
2064 bool need_interface_mapping;
2071 enum {MISSING = 0, ELEMENTAL, SCALAR, SCALAR_POINTER, ARRAY};
2073 arglist = NULL_TREE;
2074 retargs = NULL_TREE;
2075 stringargs = NULL_TREE;
2079 if (sym->from_intmod == INTMOD_ISO_C_BINDING)
2081 if (sym->intmod_sym_id == ISOCBINDING_LOC)
2083 if (arg->expr->rank == 0)
2084 gfc_conv_expr_reference (se, arg->expr);
2088 /* This is really the actual arg because no formal arglist is
2089 created for C_LOC. */
2090 fsym = arg->expr->symtree->n.sym;
2092 /* We should want it to do g77 calling convention. */
2094 && !(fsym->attr.pointer || fsym->attr.allocatable)
2095 && fsym->as->type != AS_ASSUMED_SHAPE;
2096 f = f || !sym->attr.always_explicit;
2098 argss = gfc_walk_expr (arg->expr);
2099 gfc_conv_array_parameter (se, arg->expr, argss, f);
2104 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2106 arg->expr->ts.type = sym->ts.derived->ts.type;
2107 arg->expr->ts.f90_type = sym->ts.derived->ts.f90_type;
2108 arg->expr->ts.kind = sym->ts.derived->ts.kind;
2109 gfc_conv_expr_reference (se, arg->expr);
2117 if (!sym->attr.elemental)
2119 gcc_assert (se->ss->type == GFC_SS_FUNCTION);
2120 if (se->ss->useflags)
2122 gcc_assert (gfc_return_by_reference (sym)
2123 && sym->result->attr.dimension);
2124 gcc_assert (se->loop != NULL);
2126 /* Access the previously obtained result. */
2127 gfc_conv_tmp_array_ref (se);
2128 gfc_advance_se_ss_chain (se);
2132 info = &se->ss->data.info;
2137 gfc_init_block (&post);
2138 gfc_init_interface_mapping (&mapping);
2139 need_interface_mapping = ((sym->ts.type == BT_CHARACTER
2140 && sym->ts.cl->length
2141 && sym->ts.cl->length->expr_type
2143 || sym->attr.dimension);
2144 formal = sym->formal;
2145 /* Evaluate the arguments. */
2146 for (; arg != NULL; arg = arg->next, formal = formal ? formal->next : NULL)
2149 fsym = formal ? formal->sym : NULL;
2150 parm_kind = MISSING;
2154 if (se->ignore_optional)
2156 /* Some intrinsics have already been resolved to the correct
2160 else if (arg->label)
2162 has_alternate_specifier = 1;
2167 /* Pass a NULL pointer for an absent arg. */
2168 gfc_init_se (&parmse, NULL);
2169 parmse.expr = null_pointer_node;
2170 if (arg->missing_arg_type == BT_CHARACTER)
2171 parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
2174 else if (se->ss && se->ss->useflags)
2176 /* An elemental function inside a scalarized loop. */
2177 gfc_init_se (&parmse, se);
2178 gfc_conv_expr_reference (&parmse, e);
2179 parm_kind = ELEMENTAL;
2183 /* A scalar or transformational function. */
2184 gfc_init_se (&parmse, NULL);
2185 argss = gfc_walk_expr (e);
2187 if (argss == gfc_ss_terminator)
2189 if (fsym && fsym->attr.value)
2191 if (fsym->ts.type == BT_CHARACTER
2192 && fsym->ts.is_c_interop
2193 && fsym->ns->proc_name != NULL
2194 && fsym->ns->proc_name->attr.is_bind_c)
2197 gfc_conv_scalar_char_value (fsym, &parmse, &e);
2198 if (parmse.expr == NULL)
2199 gfc_conv_expr (&parmse, e);
2202 gfc_conv_expr (&parmse, e);
2204 else if (arg->name && arg->name[0] == '%')
2205 /* Argument list functions %VAL, %LOC and %REF are signalled
2206 through arg->name. */
2207 conv_arglist_function (&parmse, arg->expr, arg->name);
2208 else if ((e->expr_type == EXPR_FUNCTION)
2209 && e->symtree->n.sym->attr.pointer
2210 && fsym && fsym->attr.target)
2212 gfc_conv_expr (&parmse, e);
2213 parmse.expr = build_fold_addr_expr (parmse.expr);
2217 gfc_conv_expr_reference (&parmse, e);
2218 if (fsym && fsym->attr.pointer
2219 && fsym->attr.flavor != FL_PROCEDURE
2220 && e->expr_type != EXPR_NULL)
2222 /* Scalar pointer dummy args require an extra level of
2223 indirection. The null pointer already contains
2224 this level of indirection. */
2225 parm_kind = SCALAR_POINTER;
2226 parmse.expr = build_fold_addr_expr (parmse.expr);
2232 /* If the procedure requires an explicit interface, the actual
2233 argument is passed according to the corresponding formal
2234 argument. If the corresponding formal argument is a POINTER,
2235 ALLOCATABLE or assumed shape, we do not use g77's calling
2236 convention, and pass the address of the array descriptor
2237 instead. Otherwise we use g77's calling convention. */
2240 && !(fsym->attr.pointer || fsym->attr.allocatable)
2241 && fsym->as->type != AS_ASSUMED_SHAPE;
2242 f = f || !sym->attr.always_explicit;
2244 if (e->expr_type == EXPR_VARIABLE
2245 && is_subref_array (e))
2246 /* The actual argument is a component reference to an
2247 array of derived types. In this case, the argument
2248 is converted to a temporary, which is passed and then
2249 written back after the procedure call. */
2250 gfc_conv_subref_array_arg (&parmse, e, f,
2251 fsym ? fsym->attr.intent : INTENT_INOUT);
2253 gfc_conv_array_parameter (&parmse, e, argss, f);
2255 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
2256 allocated on entry, it must be deallocated. */
2257 if (fsym && fsym->attr.allocatable
2258 && fsym->attr.intent == INTENT_OUT)
2260 tmp = build_fold_indirect_ref (parmse.expr);
2261 tmp = gfc_trans_dealloc_allocated (tmp);
2262 gfc_add_expr_to_block (&se->pre, tmp);
2268 /* The case with fsym->attr.optional is that of a user subroutine
2269 with an interface indicating an optional argument. When we call
2270 an intrinsic subroutine, however, fsym is NULL, but we might still
2271 have an optional argument, so we proceed to the substitution
2273 if (e && (fsym == NULL || fsym->attr.optional))
2275 /* If an optional argument is itself an optional dummy argument,
2276 check its presence and substitute a null if absent. */
2277 if (e->expr_type == EXPR_VARIABLE
2278 && e->symtree->n.sym->attr.optional)
2279 gfc_conv_missing_dummy (&parmse, e, fsym ? fsym->ts : e->ts);
2284 /* Obtain the character length of an assumed character length
2285 length procedure from the typespec. */
2286 if (fsym->ts.type == BT_CHARACTER
2287 && parmse.string_length == NULL_TREE
2288 && e->ts.type == BT_PROCEDURE
2289 && e->symtree->n.sym->ts.type == BT_CHARACTER
2290 && e->symtree->n.sym->ts.cl->length != NULL)
2292 gfc_conv_const_charlen (e->symtree->n.sym->ts.cl);
2293 parmse.string_length = e->symtree->n.sym->ts.cl->backend_decl;
2297 if (fsym && need_interface_mapping)
2298 gfc_add_interface_mapping (&mapping, fsym, &parmse);
2300 gfc_add_block_to_block (&se->pre, &parmse.pre);
2301 gfc_add_block_to_block (&post, &parmse.post);
2303 /* Allocated allocatable components of derived types must be
2304 deallocated for INTENT(OUT) dummy arguments and non-variable
2305 scalars. Non-variable arrays are dealt with in trans-array.c
2306 (gfc_conv_array_parameter). */
2307 if (e && e->ts.type == BT_DERIVED
2308 && e->ts.derived->attr.alloc_comp
2309 && ((formal && formal->sym->attr.intent == INTENT_OUT)
2311 (e->expr_type != EXPR_VARIABLE && !e->rank)))
2314 tmp = build_fold_indirect_ref (parmse.expr);
2315 parm_rank = e->rank;
2323 case (SCALAR_POINTER):
2324 tmp = build_fold_indirect_ref (tmp);
2331 tmp = gfc_deallocate_alloc_comp (e->ts.derived, tmp, parm_rank);
2332 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.optional)
2333 tmp = build3_v (COND_EXPR, gfc_conv_expr_present (e->symtree->n.sym),
2334 tmp, build_empty_stmt ());
2336 if (e->expr_type != EXPR_VARIABLE)
2337 /* Don't deallocate non-variables until they have been used. */
2338 gfc_add_expr_to_block (&se->post, tmp);
2341 gcc_assert (formal && formal->sym->attr.intent == INTENT_OUT);
2342 gfc_add_expr_to_block (&se->pre, tmp);
2346 /* Character strings are passed as two parameters, a length and a
2348 if (parmse.string_length != NULL_TREE)
2349 stringargs = gfc_chainon_list (stringargs, parmse.string_length);
2351 arglist = gfc_chainon_list (arglist, parmse.expr);
2353 gfc_finish_interface_mapping (&mapping, &se->pre, &se->post);
2356 if (ts.type == BT_CHARACTER)
2358 if (sym->ts.cl->length == NULL)
2360 /* Assumed character length results are not allowed by 5.1.1.5 of the
2361 standard and are trapped in resolve.c; except in the case of SPREAD
2362 (and other intrinsics?) and dummy functions. In the case of SPREAD,
2363 we take the character length of the first argument for the result.
2364 For dummies, we have to look through the formal argument list for
2365 this function and use the character length found there.*/
2366 if (!sym->attr.dummy)
2367 cl.backend_decl = TREE_VALUE (stringargs);
2370 formal = sym->ns->proc_name->formal;
2371 for (; formal; formal = formal->next)
2372 if (strcmp (formal->sym->name, sym->name) == 0)
2373 cl.backend_decl = formal->sym->ts.cl->backend_decl;
2380 /* Calculate the length of the returned string. */
2381 gfc_init_se (&parmse, NULL);
2382 if (need_interface_mapping)
2383 gfc_apply_interface_mapping (&mapping, &parmse, sym->ts.cl->length);
2385 gfc_conv_expr (&parmse, sym->ts.cl->length);
2386 gfc_add_block_to_block (&se->pre, &parmse.pre);
2387 gfc_add_block_to_block (&se->post, &parmse.post);
2389 tmp = fold_convert (gfc_charlen_type_node, parmse.expr);
2390 tmp = fold_build2 (MAX_EXPR, gfc_charlen_type_node, tmp,
2391 build_int_cst (gfc_charlen_type_node, 0));
2392 cl.backend_decl = tmp;
2395 /* Set up a charlen structure for it. */
2400 len = cl.backend_decl;
2403 byref = gfc_return_by_reference (sym);
2406 if (se->direct_byref)
2408 /* Sometimes, too much indirection can be applied; eg. for
2409 function_result = array_valued_recursive_function. */
2410 if (TREE_TYPE (TREE_TYPE (se->expr))
2411 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))
2412 && GFC_DESCRIPTOR_TYPE_P
2413 (TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))))
2414 se->expr = build_fold_indirect_ref (se->expr);
2416 retargs = gfc_chainon_list (retargs, se->expr);
2418 else if (sym->result->attr.dimension)
2420 gcc_assert (se->loop && info);
2422 /* Set the type of the array. */
2423 tmp = gfc_typenode_for_spec (&ts);
2424 info->dimen = se->loop->dimen;
2426 /* Evaluate the bounds of the result, if known. */
2427 gfc_set_loop_bounds_from_array_spec (&mapping, se, sym->result->as);
2429 /* Create a temporary to store the result. In case the function
2430 returns a pointer, the temporary will be a shallow copy and
2431 mustn't be deallocated. */
2432 callee_alloc = sym->attr.allocatable || sym->attr.pointer;
2433 gfc_trans_create_temp_array (&se->pre, &se->post, se->loop, info, tmp,
2434 false, !sym->attr.pointer, callee_alloc);
2436 /* Pass the temporary as the first argument. */
2437 tmp = info->descriptor;
2438 tmp = build_fold_addr_expr (tmp);
2439 retargs = gfc_chainon_list (retargs, tmp);
2441 else if (ts.type == BT_CHARACTER)
2443 /* Pass the string length. */
2444 type = gfc_get_character_type (ts.kind, ts.cl);
2445 type = build_pointer_type (type);
2447 /* Return an address to a char[0:len-1]* temporary for
2448 character pointers. */
2449 if (sym->attr.pointer || sym->attr.allocatable)
2451 /* Build char[0:len-1] * pstr. */
2452 tmp = fold_build2 (MINUS_EXPR, gfc_charlen_type_node, len,
2453 build_int_cst (gfc_charlen_type_node, 1));
2454 tmp = build_range_type (gfc_array_index_type,
2455 gfc_index_zero_node, tmp);
2456 tmp = build_array_type (gfc_character1_type_node, tmp);
2457 var = gfc_create_var (build_pointer_type (tmp), "pstr");
2459 /* Provide an address expression for the function arguments. */
2460 var = build_fold_addr_expr (var);
2463 var = gfc_conv_string_tmp (se, type, len);
2465 retargs = gfc_chainon_list (retargs, var);
2469 gcc_assert (gfc_option.flag_f2c && ts.type == BT_COMPLEX);
2471 type = gfc_get_complex_type (ts.kind);
2472 var = build_fold_addr_expr (gfc_create_var (type, "cmplx"));
2473 retargs = gfc_chainon_list (retargs, var);
2476 /* Add the string length to the argument list. */
2477 if (ts.type == BT_CHARACTER)
2478 retargs = gfc_chainon_list (retargs, len);
2480 gfc_free_interface_mapping (&mapping);
2482 /* Add the return arguments. */
2483 arglist = chainon (retargs, arglist);
2485 /* Add the hidden string length parameters to the arguments. */
2486 arglist = chainon (arglist, stringargs);
2488 /* We may want to append extra arguments here. This is used e.g. for
2489 calls to libgfortran_matmul_??, which need extra information. */
2490 if (append_args != NULL_TREE)
2491 arglist = chainon (arglist, append_args);
2493 /* Generate the actual call. */
2494 gfc_conv_function_val (se, sym);
2496 /* If there are alternate return labels, function type should be
2497 integer. Can't modify the type in place though, since it can be shared
2498 with other functions. For dummy arguments, the typing is done to
2499 to this result, even if it has to be repeated for each call. */
2500 if (has_alternate_specifier
2501 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) != integer_type_node)
2503 if (!sym->attr.dummy)
2505 TREE_TYPE (sym->backend_decl)
2506 = build_function_type (integer_type_node,
2507 TYPE_ARG_TYPES (TREE_TYPE (sym->backend_decl)));
2508 se->expr = build_fold_addr_expr (sym->backend_decl);
2511 TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) = integer_type_node;
2514 fntype = TREE_TYPE (TREE_TYPE (se->expr));
2515 se->expr = build_call_list (TREE_TYPE (fntype), se->expr, arglist);
2517 /* If we have a pointer function, but we don't want a pointer, e.g.
2520 where f is pointer valued, we have to dereference the result. */
2521 if (!se->want_pointer && !byref && sym->attr.pointer)
2522 se->expr = build_fold_indirect_ref (se->expr);
2524 /* f2c calling conventions require a scalar default real function to
2525 return a double precision result. Convert this back to default
2526 real. We only care about the cases that can happen in Fortran 77.
2528 if (gfc_option.flag_f2c && sym->ts.type == BT_REAL
2529 && sym->ts.kind == gfc_default_real_kind
2530 && !sym->attr.always_explicit)
2531 se->expr = fold_convert (gfc_get_real_type (sym->ts.kind), se->expr);
2533 /* A pure function may still have side-effects - it may modify its
2535 TREE_SIDE_EFFECTS (se->expr) = 1;
2537 if (!sym->attr.pure)
2538 TREE_SIDE_EFFECTS (se->expr) = 1;
2543 /* Add the function call to the pre chain. There is no expression. */
2544 gfc_add_expr_to_block (&se->pre, se->expr);
2545 se->expr = NULL_TREE;
2547 if (!se->direct_byref)
2549 if (sym->attr.dimension)
2551 if (flag_bounds_check)
2553 /* Check the data pointer hasn't been modified. This would
2554 happen in a function returning a pointer. */
2555 tmp = gfc_conv_descriptor_data_get (info->descriptor);
2556 tmp = fold_build2 (NE_EXPR, boolean_type_node,
2558 gfc_trans_runtime_check (tmp, &se->pre, NULL, gfc_msg_fault);
2560 se->expr = info->descriptor;
2561 /* Bundle in the string length. */
2562 se->string_length = len;
2564 else if (sym->ts.type == BT_CHARACTER)
2566 /* Dereference for character pointer results. */
2567 if (sym->attr.pointer || sym->attr.allocatable)
2568 se->expr = build_fold_indirect_ref (var);
2572 se->string_length = len;
2576 gcc_assert (sym->ts.type == BT_COMPLEX && gfc_option.flag_f2c);
2577 se->expr = build_fold_indirect_ref (var);
2582 /* Follow the function call with the argument post block. */
2584 gfc_add_block_to_block (&se->pre, &post);
2586 gfc_add_block_to_block (&se->post, &post);
2588 return has_alternate_specifier;
2592 /* Generate code to copy a string. */
2595 gfc_trans_string_copy (stmtblock_t * block, tree dlength, tree dest,
2596 tree slength, tree src)
2598 tree tmp, dlen, slen;
2606 stmtblock_t tempblock;
2608 dlen = fold_convert (size_type_node, gfc_evaluate_now (dlength, block));
2609 slen = fold_convert (size_type_node, gfc_evaluate_now (slength, block));
2611 /* Deal with single character specially. */
2612 dsc = gfc_to_single_character (dlen, dest);
2613 ssc = gfc_to_single_character (slen, src);
2614 if (dsc != NULL_TREE && ssc != NULL_TREE)
2616 gfc_add_modify_expr (block, dsc, ssc);
2620 /* Do nothing if the destination length is zero. */
2621 cond = fold_build2 (GT_EXPR, boolean_type_node, dlen,
2622 build_int_cst (size_type_node, 0));
2624 /* The following code was previously in _gfortran_copy_string:
2626 // The two strings may overlap so we use memmove.
2628 copy_string (GFC_INTEGER_4 destlen, char * dest,
2629 GFC_INTEGER_4 srclen, const char * src)
2631 if (srclen >= destlen)
2633 // This will truncate if too long.
2634 memmove (dest, src, destlen);
2638 memmove (dest, src, srclen);
2640 memset (&dest[srclen], ' ', destlen - srclen);
2644 We're now doing it here for better optimization, but the logic
2647 /* Truncate string if source is too long. */
2648 cond2 = fold_build2 (GE_EXPR, boolean_type_node, slen, dlen);
2649 tmp2 = build_call_expr (built_in_decls[BUILT_IN_MEMMOVE],
2650 3, dest, src, dlen);
2652 /* Else copy and pad with spaces. */
2653 tmp3 = build_call_expr (built_in_decls[BUILT_IN_MEMMOVE],
2654 3, dest, src, slen);
2656 tmp4 = fold_build2 (POINTER_PLUS_EXPR, pchar_type_node, dest,
2657 fold_convert (sizetype, slen));
2658 tmp4 = build_call_expr (built_in_decls[BUILT_IN_MEMSET], 3,
2660 build_int_cst (gfc_get_int_type (gfc_c_int_kind),
2661 lang_hooks.to_target_charset (' ')),
2662 fold_build2 (MINUS_EXPR, TREE_TYPE(dlen),
2665 gfc_init_block (&tempblock);
2666 gfc_add_expr_to_block (&tempblock, tmp3);
2667 gfc_add_expr_to_block (&tempblock, tmp4);
2668 tmp3 = gfc_finish_block (&tempblock);
2670 /* The whole copy_string function is there. */
2671 tmp = fold_build3 (COND_EXPR, void_type_node, cond2, tmp2, tmp3);
2672 tmp = fold_build3 (COND_EXPR, void_type_node, cond, tmp, build_empty_stmt ());
2673 gfc_add_expr_to_block (block, tmp);
2677 /* Translate a statement function.
2678 The value of a statement function reference is obtained by evaluating the
2679 expression using the values of the actual arguments for the values of the
2680 corresponding dummy arguments. */
2683 gfc_conv_statement_function (gfc_se * se, gfc_expr * expr)
2687 gfc_formal_arglist *fargs;
2688 gfc_actual_arglist *args;
2691 gfc_saved_var *saved_vars;
2697 sym = expr->symtree->n.sym;
2698 args = expr->value.function.actual;
2699 gfc_init_se (&lse, NULL);
2700 gfc_init_se (&rse, NULL);
2703 for (fargs = sym->formal; fargs; fargs = fargs->next)
2705 saved_vars = (gfc_saved_var *)gfc_getmem (n * sizeof (gfc_saved_var));
2706 temp_vars = (tree *)gfc_getmem (n * sizeof (tree));
2708 for (fargs = sym->formal, n = 0; fargs; fargs = fargs->next, n++)
2710 /* Each dummy shall be specified, explicitly or implicitly, to be
2712 gcc_assert (fargs->sym->attr.dimension == 0);
2715 /* Create a temporary to hold the value. */
2716 type = gfc_typenode_for_spec (&fsym->ts);
2717 temp_vars[n] = gfc_create_var (type, fsym->name);
2719 if (fsym->ts.type == BT_CHARACTER)
2721 /* Copy string arguments. */
2724 gcc_assert (fsym->ts.cl && fsym->ts.cl->length
2725 && fsym->ts.cl->length->expr_type == EXPR_CONSTANT);
2727 arglen = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
2728 tmp = gfc_build_addr_expr (build_pointer_type (type),
2731 gfc_conv_expr (&rse, args->expr);
2732 gfc_conv_string_parameter (&rse);
2733 gfc_add_block_to_block (&se->pre, &lse.pre);
2734 gfc_add_block_to_block (&se->pre, &rse.pre);
2736 gfc_trans_string_copy (&se->pre, arglen, tmp, rse.string_length,
2738 gfc_add_block_to_block (&se->pre, &lse.post);
2739 gfc_add_block_to_block (&se->pre, &rse.post);
2743 /* For everything else, just evaluate the expression. */
2744 gfc_conv_expr (&lse, args->expr);
2746 gfc_add_block_to_block (&se->pre, &lse.pre);
2747 gfc_add_modify_expr (&se->pre, temp_vars[n], lse.expr);
2748 gfc_add_block_to_block (&se->pre, &lse.post);
2754 /* Use the temporary variables in place of the real ones. */
2755 for (fargs = sym->formal, n = 0; fargs; fargs = fargs->next, n++)
2756 gfc_shadow_sym (fargs->sym, temp_vars[n], &saved_vars[n]);
2758 gfc_conv_expr (se, sym->value);
2760 if (sym->ts.type == BT_CHARACTER)
2762 gfc_conv_const_charlen (sym->ts.cl);
2764 /* Force the expression to the correct length. */
2765 if (!INTEGER_CST_P (se->string_length)
2766 || tree_int_cst_lt (se->string_length,
2767 sym->ts.cl->backend_decl))
2769 type = gfc_get_character_type (sym->ts.kind, sym->ts.cl);
2770 tmp = gfc_create_var (type, sym->name);
2771 tmp = gfc_build_addr_expr (build_pointer_type (type), tmp);
2772 gfc_trans_string_copy (&se->pre, sym->ts.cl->backend_decl, tmp,
2773 se->string_length, se->expr);
2776 se->string_length = sym->ts.cl->backend_decl;
2779 /* Restore the original variables. */
2780 for (fargs = sym->formal, n = 0; fargs; fargs = fargs->next, n++)
2781 gfc_restore_sym (fargs->sym, &saved_vars[n]);
2782 gfc_free (saved_vars);
2786 /* Translate a function expression. */
2789 gfc_conv_function_expr (gfc_se * se, gfc_expr * expr)
2793 if (expr->value.function.isym)
2795 gfc_conv_intrinsic_function (se, expr);
2799 /* We distinguish statement functions from general functions to improve
2800 runtime performance. */
2801 if (expr->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2803 gfc_conv_statement_function (se, expr);
2807 /* expr.value.function.esym is the resolved (specific) function symbol for
2808 most functions. However this isn't set for dummy procedures. */
2809 sym = expr->value.function.esym;
2811 sym = expr->symtree->n.sym;
2812 gfc_conv_function_call (se, sym, expr->value.function.actual, NULL_TREE);
2817 gfc_conv_array_constructor_expr (gfc_se * se, gfc_expr * expr)
2819 gcc_assert (se->ss != NULL && se->ss != gfc_ss_terminator);
2820 gcc_assert (se->ss->expr == expr && se->ss->type == GFC_SS_CONSTRUCTOR);
2822 gfc_conv_tmp_array_ref (se);
2823 gfc_advance_se_ss_chain (se);
2827 /* Build a static initializer. EXPR is the expression for the initial value.
2828 The other parameters describe the variable of the component being
2829 initialized. EXPR may be null. */
2832 gfc_conv_initializer (gfc_expr * expr, gfc_typespec * ts, tree type,
2833 bool array, bool pointer)
2837 if (!(expr || pointer))
2840 /* Check if we have ISOCBINDING_NULL_PTR or ISOCBINDING_NULL_FUNPTR
2841 (these are the only two iso_c_binding derived types that can be
2842 used as initialization expressions). If so, we need to modify
2843 the 'expr' to be that for a (void *). */
2844 if (expr != NULL && expr->ts.type == BT_DERIVED
2845 && expr->ts.is_iso_c && expr->ts.derived)
2847 gfc_symbol *derived = expr->ts.derived;
2849 expr = gfc_int_expr (0);
2851 /* The derived symbol has already been converted to a (void *). Use
2853 expr->ts.f90_type = derived->ts.f90_type;
2854 expr->ts.kind = derived->ts.kind;
2859 /* Arrays need special handling. */
2861 return gfc_build_null_descriptor (type);
2863 return gfc_conv_array_initializer (type, expr);
2866 return fold_convert (type, null_pointer_node);
2872 gfc_init_se (&se, NULL);
2873 gfc_conv_structure (&se, expr, 1);
2877 return gfc_conv_string_init (ts->cl->backend_decl,expr);
2880 gfc_init_se (&se, NULL);
2881 gfc_conv_constant (&se, expr);
2888 gfc_trans_subarray_assign (tree dest, gfc_component * cm, gfc_expr * expr)
2900 gfc_start_block (&block);
2902 /* Initialize the scalarizer. */
2903 gfc_init_loopinfo (&loop);
2905 gfc_init_se (&lse, NULL);
2906 gfc_init_se (&rse, NULL);
2909 rss = gfc_walk_expr (expr);
2910 if (rss == gfc_ss_terminator)
2912 /* The rhs is scalar. Add a ss for the expression. */
2913 rss = gfc_get_ss ();
2914 rss->next = gfc_ss_terminator;
2915 rss->type = GFC_SS_SCALAR;
2919 /* Create a SS for the destination. */
2920 lss = gfc_get_ss ();
2921 lss->type = GFC_SS_COMPONENT;
2923 lss->shape = gfc_get_shape (cm->as->rank);
2924 lss->next = gfc_ss_terminator;
2925 lss->data.info.dimen = cm->as->rank;
2926 lss->data.info.descriptor = dest;
2927 lss->data.info.data = gfc_conv_array_data (dest);
2928 lss->data.info.offset = gfc_conv_array_offset (dest);
2929 for (n = 0; n < cm->as->rank; n++)
2931 lss->data.info.dim[n] = n;
2932 lss->data.info.start[n] = gfc_conv_array_lbound (dest, n);
2933 lss->data.info.stride[n] = gfc_index_one_node;
2935 mpz_init (lss->shape[n]);
2936 mpz_sub (lss->shape[n], cm->as->upper[n]->value.integer,
2937 cm->as->lower[n]->value.integer);
2938 mpz_add_ui (lss->shape[n], lss->shape[n], 1);
2941 /* Associate the SS with the loop. */
2942 gfc_add_ss_to_loop (&loop, lss);
2943 gfc_add_ss_to_loop (&loop, rss);
2945 /* Calculate the bounds of the scalarization. */
2946 gfc_conv_ss_startstride (&loop);
2948 /* Setup the scalarizing loops. */
2949 gfc_conv_loop_setup (&loop);
2951 /* Setup the gfc_se structures. */
2952 gfc_copy_loopinfo_to_se (&lse, &loop);
2953 gfc_copy_loopinfo_to_se (&rse, &loop);
2956 gfc_mark_ss_chain_used (rss, 1);
2958 gfc_mark_ss_chain_used (lss, 1);
2960 /* Start the scalarized loop body. */
2961 gfc_start_scalarized_body (&loop, &body);
2963 gfc_conv_tmp_array_ref (&lse);
2964 if (cm->ts.type == BT_CHARACTER)
2965 lse.string_length = cm->ts.cl->backend_decl;
2967 gfc_conv_expr (&rse, expr);
2969 tmp = gfc_trans_scalar_assign (&lse, &rse, cm->ts, true, false);
2970 gfc_add_expr_to_block (&body, tmp);
2972 gcc_assert (rse.ss == gfc_ss_terminator);
2974 /* Generate the copying loops. */
2975 gfc_trans_scalarizing_loops (&loop, &body);
2977 /* Wrap the whole thing up. */
2978 gfc_add_block_to_block (&block, &loop.pre);
2979 gfc_add_block_to_block (&block, &loop.post);
2981 for (n = 0; n < cm->as->rank; n++)
2982 mpz_clear (lss->shape[n]);
2983 gfc_free (lss->shape);
2985 gfc_cleanup_loop (&loop);
2987 return gfc_finish_block (&block);
2991 /* Assign a single component of a derived type constructor. */
2994 gfc_trans_subcomponent_assign (tree dest, gfc_component * cm, gfc_expr * expr)
3004 gfc_start_block (&block);
3008 gfc_init_se (&se, NULL);
3009 /* Pointer component. */
3012 /* Array pointer. */
3013 if (expr->expr_type == EXPR_NULL)
3014 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
3017 rss = gfc_walk_expr (expr);
3018 se.direct_byref = 1;
3020 gfc_conv_expr_descriptor (&se, expr, rss);
3021 gfc_add_block_to_block (&block, &se.pre);
3022 gfc_add_block_to_block (&block, &se.post);
3027 /* Scalar pointers. */
3028 se.want_pointer = 1;
3029 gfc_conv_expr (&se, expr);
3030 gfc_add_block_to_block (&block, &se.pre);
3031 gfc_add_modify_expr (&block, dest,
3032 fold_convert (TREE_TYPE (dest), se.expr));
3033 gfc_add_block_to_block (&block, &se.post);
3036 else if (cm->dimension)
3038 if (cm->allocatable && expr->expr_type == EXPR_NULL)
3039 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
3040 else if (cm->allocatable)
3044 gfc_init_se (&se, NULL);
3046 rss = gfc_walk_expr (expr);
3047 se.want_pointer = 0;
3048 gfc_conv_expr_descriptor (&se, expr, rss);
3049 gfc_add_block_to_block (&block, &se.pre);
3051 tmp = fold_convert (TREE_TYPE (dest), se.expr);
3052 gfc_add_modify_expr (&block, dest, tmp);
3054 if (cm->ts.type == BT_DERIVED && cm->ts.derived->attr.alloc_comp)
3055 tmp = gfc_copy_alloc_comp (cm->ts.derived, se.expr, dest,
3058 tmp = gfc_duplicate_allocatable (dest, se.expr,
3059 TREE_TYPE(cm->backend_decl),
3062 gfc_add_expr_to_block (&block, tmp);
3064 gfc_add_block_to_block (&block, &se.post);
3065 gfc_conv_descriptor_data_set (&block, se.expr, null_pointer_node);
3067 /* Shift the lbound and ubound of temporaries to being unity, rather
3068 than zero, based. Calculate the offset for all cases. */
3069 offset = gfc_conv_descriptor_offset (dest);
3070 gfc_add_modify_expr (&block, offset, gfc_index_zero_node);
3071 tmp2 =gfc_create_var (gfc_array_index_type, NULL);
3072 for (n = 0; n < expr->rank; n++)
3074 if (expr->expr_type != EXPR_VARIABLE
3075 && expr->expr_type != EXPR_CONSTANT)
3078 tmp = gfc_conv_descriptor_ubound (dest, gfc_rank_cst[n]);
3079 span = fold_build2 (MINUS_EXPR, gfc_array_index_type, tmp,
3080 gfc_conv_descriptor_lbound (dest, gfc_rank_cst[n]));
3081 gfc_add_modify_expr (&block, tmp,
3082 fold_build2 (PLUS_EXPR,
3083 gfc_array_index_type,
3084 span, gfc_index_one_node));
3085 tmp = gfc_conv_descriptor_lbound (dest, gfc_rank_cst[n]);
3086 gfc_add_modify_expr (&block, tmp, gfc_index_one_node);
3088 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type,
3089 gfc_conv_descriptor_lbound (dest,
3091 gfc_conv_descriptor_stride (dest,
3093 gfc_add_modify_expr (&block, tmp2, tmp);
3094 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, offset, tmp2);
3095 gfc_add_modify_expr (&block, offset, tmp);
3100 tmp = gfc_trans_subarray_assign (dest, cm, expr);
3101 gfc_add_expr_to_block (&block, tmp);
3104 else if (expr->ts.type == BT_DERIVED)
3106 if (expr->expr_type != EXPR_STRUCTURE)
3108 gfc_init_se (&se, NULL);
3109 gfc_conv_expr (&se, expr);
3110 gfc_add_modify_expr (&block, dest,
3111 fold_convert (TREE_TYPE (dest), se.expr));
3115 /* Nested constructors. */
3116 tmp = gfc_trans_structure_assign (dest, expr);
3117 gfc_add_expr_to_block (&block, tmp);
3122 /* Scalar component. */
3123 gfc_init_se (&se, NULL);
3124 gfc_init_se (&lse, NULL);
3126 gfc_conv_expr (&se, expr);
3127 if (cm->ts.type == BT_CHARACTER)
3128 lse.string_length = cm->ts.cl->backend_decl;
3130 tmp = gfc_trans_scalar_assign (&lse, &se, cm->ts, true, false);
3131 gfc_add_expr_to_block (&block, tmp);
3133 return gfc_finish_block (&block);
3136 /* Assign a derived type constructor to a variable. */
3139 gfc_trans_structure_assign (tree dest, gfc_expr * expr)
3147 gfc_start_block (&block);
3148 cm = expr->ts.derived->components;
3149 for (c = expr->value.constructor; c; c = c->next, cm = cm->next)
3151 /* Skip absent members in default initializers. */
3155 /* Update the type/kind of the expression if it represents either
3156 C_NULL_PTR or C_NULL_FUNPTR. This is done here because this may
3157 be the first place reached for initializing output variables that
3158 have components of type C_PTR/C_FUNPTR that are initialized. */
3159 if (c->expr->ts.type == BT_DERIVED && c->expr->ts.derived
3160 && c->expr->ts.derived->attr.is_iso_c)
3162 c->expr->expr_type = EXPR_NULL;
3163 c->expr->ts.type = c->expr->ts.derived->ts.type;
3164 c->expr->ts.f90_type = c->expr->ts.derived->ts.f90_type;
3165 c->expr->ts.kind = c->expr->ts.derived->ts.kind;
3168 field = cm->backend_decl;
3169 tmp = build3 (COMPONENT_REF, TREE_TYPE (field), dest, field, NULL_TREE);
3170 tmp = gfc_trans_subcomponent_assign (tmp, cm, c->expr);
3171 gfc_add_expr_to_block (&block, tmp);
3173 return gfc_finish_block (&block);
3176 /* Build an expression for a constructor. If init is nonzero then
3177 this is part of a static variable initializer. */
3180 gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
3187 VEC(constructor_elt,gc) *v = NULL;
3189 gcc_assert (se->ss == NULL);
3190 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
3191 type = gfc_typenode_for_spec (&expr->ts);
3195 /* Create a temporary variable and fill it in. */
3196 se->expr = gfc_create_var (type, expr->ts.derived->name);
3197 tmp = gfc_trans_structure_assign (se->expr, expr);
3198 gfc_add_expr_to_block (&se->pre, tmp);
3202 cm = expr->ts.derived->components;
3204 for (c = expr->value.constructor; c; c = c->next, cm = cm->next)
3206 /* Skip absent members in default initializers and allocatable
3207 components. Although the latter have a default initializer
3208 of EXPR_NULL,... by default, the static nullify is not needed
3209 since this is done every time we come into scope. */
3210 if (!c->expr || cm->allocatable)
3213 val = gfc_conv_initializer (c->expr, &cm->ts,
3214 TREE_TYPE (cm->backend_decl), cm->dimension, cm->pointer);
3216 /* Append it to the constructor list. */
3217 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
3219 se->expr = build_constructor (type, v);
3223 /* Translate a substring expression. */
3226 gfc_conv_substring_expr (gfc_se * se, gfc_expr * expr)
3232 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
3234 se->expr = gfc_build_string_const (expr->value.character.length,
3235 expr->value.character.string);
3236 se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
3237 TYPE_STRING_FLAG (TREE_TYPE (se->expr)) = 1;
3240 gfc_conv_substring (se, ref, expr->ts.kind, NULL, &expr->where);
3244 /* Entry point for expression translation. Evaluates a scalar quantity.
3245 EXPR is the expression to be translated, and SE is the state structure if
3246 called from within the scalarized. */
3249 gfc_conv_expr (gfc_se * se, gfc_expr * expr)
3251 if (se->ss && se->ss->expr == expr
3252 && (se->ss->type == GFC_SS_SCALAR || se->ss->type == GFC_SS_REFERENCE))
3254 /* Substitute a scalar expression evaluated outside the scalarization
3256 se->expr = se->ss->data.scalar.expr;
3257 se->string_length = se->ss->string_length;
3258 gfc_advance_se_ss_chain (se);
3262 /* We need to convert the expressions for the iso_c_binding derived types.
3263 C_NULL_PTR and C_NULL_FUNPTR will be made EXPR_NULL, which evaluates to
3264 null_pointer_node. C_PTR and C_FUNPTR are converted to match the
3265 typespec for the C_PTR and C_FUNPTR symbols, which has already been
3266 updated to be an integer with a kind equal to the size of a (void *). */
3267 if (expr->ts.type == BT_DERIVED && expr->ts.derived
3268 && expr->ts.derived->attr.is_iso_c)
3270 if (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
3271 || expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_FUNPTR)
3273 /* Set expr_type to EXPR_NULL, which will result in
3274 null_pointer_node being used below. */
3275 expr->expr_type = EXPR_NULL;
3279 /* Update the type/kind of the expression to be what the new
3280 type/kind are for the updated symbols of C_PTR/C_FUNPTR. */
3281 expr->ts.type = expr->ts.derived->ts.type;
3282 expr->ts.f90_type = expr->ts.derived->ts.f90_type;
3283 expr->ts.kind = expr->ts.derived->ts.kind;
3287 switch (expr->expr_type)
3290 gfc_conv_expr_op (se, expr);
3294 gfc_conv_function_expr (se, expr);
3298 gfc_conv_constant (se, expr);
3302 gfc_conv_variable (se, expr);
3306 se->expr = null_pointer_node;
3309 case EXPR_SUBSTRING:
3310 gfc_conv_substring_expr (se, expr);
3313 case EXPR_STRUCTURE:
3314 gfc_conv_structure (se, expr, 0);
3318 gfc_conv_array_constructor_expr (se, expr);
3327 /* Like gfc_conv_expr_val, but the value is also suitable for use in the lhs
3328 of an assignment. */
3330 gfc_conv_expr_lhs (gfc_se * se, gfc_expr * expr)
3332 gfc_conv_expr (se, expr);
3333 /* All numeric lvalues should have empty post chains. If not we need to
3334 figure out a way of rewriting an lvalue so that it has no post chain. */
3335 gcc_assert (expr->ts.type == BT_CHARACTER || !se->post.head);
3338 /* Like gfc_conv_expr, but the POST block is guaranteed to be empty for
3339 numeric expressions. Used for scalar values where inserting cleanup code
3342 gfc_conv_expr_val (gfc_se * se, gfc_expr * expr)
3346 gcc_assert (expr->ts.type != BT_CHARACTER);
3347 gfc_conv_expr (se, expr);
3350 val = gfc_create_var (TREE_TYPE (se->expr), NULL);
3351 gfc_add_modify_expr (&se->pre, val, se->expr);
3353 gfc_add_block_to_block (&se->pre, &se->post);
3357 /* Helper to translate and expression and convert it to a particular type. */
3359 gfc_conv_expr_type (gfc_se * se, gfc_expr * expr, tree type)
3361 gfc_conv_expr_val (se, expr);
3362 se->expr = convert (type, se->expr);
3366 /* Converts an expression so that it can be passed by reference. Scalar
3370 gfc_conv_expr_reference (gfc_se * se, gfc_expr * expr)
3374 if (se->ss && se->ss->expr == expr
3375 && se->ss->type == GFC_SS_REFERENCE)
3377 se->expr = se->ss->data.scalar.expr;
3378 se->string_length = se->ss->string_length;
3379 gfc_advance_se_ss_chain (se);
3383 if (expr->ts.type == BT_CHARACTER)
3385 gfc_conv_expr (se, expr);
3386 gfc_conv_string_parameter (se);
3390 if (expr->expr_type == EXPR_VARIABLE)
3392 se->want_pointer = 1;
3393 gfc_conv_expr (se, expr);
3396 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
3397 gfc_add_modify_expr (&se->pre, var, se->expr);
3398 gfc_add_block_to_block (&se->pre, &se->post);
3404 if (expr->expr_type == EXPR_FUNCTION
3405 && expr->symtree->n.sym->attr.pointer
3406 && !expr->symtree->n.sym->attr.dimension)
3408 se->want_pointer = 1;
3409 gfc_conv_expr (se, expr);
3410 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
3411 gfc_add_modify_expr (&se->pre, var, se->expr);
3417 gfc_conv_expr (se, expr);
3419 /* Create a temporary var to hold the value. */
3420 if (TREE_CONSTANT (se->expr))
3422 tree tmp = se->expr;
3423 STRIP_TYPE_NOPS (tmp);
3424 var = build_decl (CONST_DECL, NULL, TREE_TYPE (tmp));
3425 DECL_INITIAL (var) = tmp;
3426 TREE_STATIC (var) = 1;
3431 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
3432 gfc_add_modify_expr (&se->pre, var, se->expr);
3434 gfc_add_block_to_block (&se->pre, &se->post);
3436 /* Take the address of that value. */
3437 se->expr = build_fold_addr_expr (var);
3442 gfc_trans_pointer_assign (gfc_code * code)
3444 return gfc_trans_pointer_assignment (code->expr, code->expr2);
3448 /* Generate code for a pointer assignment. */
3451 gfc_trans_pointer_assignment (gfc_expr * expr1, gfc_expr * expr2)
3463 gfc_start_block (&block);
3465 gfc_init_se (&lse, NULL);
3467 lss = gfc_walk_expr (expr1);
3468 rss = gfc_walk_expr (expr2);
3469 if (lss == gfc_ss_terminator)
3471 /* Scalar pointers. */
3472 lse.want_pointer = 1;
3473 gfc_conv_expr (&lse, expr1);
3474 gcc_assert (rss == gfc_ss_terminator);
3475 gfc_init_se (&rse, NULL);
3476 rse.want_pointer = 1;
3477 gfc_conv_expr (&rse, expr2);
3478 gfc_add_block_to_block (&block, &lse.pre);
3479 gfc_add_block_to_block (&block, &rse.pre);
3480 gfc_add_modify_expr (&block, lse.expr,
3481 fold_convert (TREE_TYPE (lse.expr), rse.expr));
3482 gfc_add_block_to_block (&block, &rse.post);
3483 gfc_add_block_to_block (&block, &lse.post);
3487 /* Array pointer. */
3488 gfc_conv_expr_descriptor (&lse, expr1, lss);
3489 switch (expr2->expr_type)
3492 /* Just set the data pointer to null. */
3493 gfc_conv_descriptor_data_set (&lse.pre, lse.expr, null_pointer_node);
3497 /* Assign directly to the pointer's descriptor. */
3498 lse.direct_byref = 1;
3499 gfc_conv_expr_descriptor (&lse, expr2, rss);
3501 /* If this is a subreference array pointer assignment, use the rhs
3502 element size for the lhs span. */
3503 if (expr1->symtree->n.sym->attr.subref_array_pointer)
3505 decl = expr1->symtree->n.sym->backend_decl;
3506 tmp = rss->data.info.descriptor;
3507 tmp = gfc_get_element_type (TREE_TYPE (tmp));
3508 tmp = size_in_bytes (tmp);
3509 tmp = fold_convert (gfc_array_index_type, tmp);
3510 gfc_add_modify_expr (&lse.post, GFC_DECL_SPAN(decl), tmp);
3516 /* Assign to a temporary descriptor and then copy that
3517 temporary to the pointer. */
3519 tmp = gfc_create_var (TREE_TYPE (desc), "ptrtemp");
3522 lse.direct_byref = 1;
3523 gfc_conv_expr_descriptor (&lse, expr2, rss);
3524 gfc_add_modify_expr (&lse.pre, desc, tmp);
3527 gfc_add_block_to_block (&block, &lse.pre);
3528 gfc_add_block_to_block (&block, &lse.post);
3530 return gfc_finish_block (&block);
3534 /* Makes sure se is suitable for passing as a function string parameter. */
3535 /* TODO: Need to check all callers fo this function. It may be abused. */
3538 gfc_conv_string_parameter (gfc_se * se)
3542 if (TREE_CODE (se->expr) == STRING_CST)
3544 se->expr = gfc_build_addr_expr (pchar_type_node, se->expr);
3548 type = TREE_TYPE (se->expr);
3549 if (TYPE_STRING_FLAG (type))
3551 gcc_assert (TREE_CODE (se->expr) != INDIRECT_REF);
3552 se->expr = gfc_build_addr_expr (pchar_type_node, se->expr);
3555 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se->expr)));
3556 gcc_assert (se->string_length
3557 && TREE_CODE (TREE_TYPE (se->string_length)) == INTEGER_TYPE);
3561 /* Generate code for assignment of scalar variables. Includes character
3562 strings and derived types with allocatable components. */
3565 gfc_trans_scalar_assign (gfc_se * lse, gfc_se * rse, gfc_typespec ts,
3566 bool l_is_temp, bool r_is_var)
3572 gfc_init_block (&block);
3574 if (ts.type == BT_CHARACTER)
3576 gcc_assert (lse->string_length != NULL_TREE
3577 && rse->string_length != NULL_TREE);
3579 gfc_conv_string_parameter (lse);
3580 gfc_conv_string_parameter (rse);
3582 gfc_add_block_to_block (&block, &lse->pre);
3583 gfc_add_block_to_block (&block, &rse->pre);
3585 gfc_trans_string_copy (&block, lse->string_length, lse->expr,
3586 rse->string_length, rse->expr);
3588 else if (ts.type == BT_DERIVED && ts.derived->attr.alloc_comp)
3592 /* Are the rhs and the lhs the same? */
3595 cond = fold_build2 (EQ_EXPR, boolean_type_node,
3596 build_fold_addr_expr (lse->expr),
3597 build_fold_addr_expr (rse->expr));
3598 cond = gfc_evaluate_now (cond, &lse->pre);
3601 /* Deallocate the lhs allocated components as long as it is not
3602 the same as the rhs. This must be done following the assignment
3603 to prevent deallocating data that could be used in the rhs
3607 tmp = gfc_evaluate_now (lse->expr, &lse->pre);
3608 tmp = gfc_deallocate_alloc_comp (ts.derived, tmp, 0);
3610 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (), tmp);
3611 gfc_add_expr_to_block (&lse->post, tmp);
3614 gfc_add_block_to_block (&block, &rse->pre);
3615 gfc_add_block_to_block (&block, &lse->pre);
3617 gfc_add_modify_expr (&block, lse->expr,
3618 fold_convert (TREE_TYPE (lse->expr), rse->expr));
3620 /* Do a deep copy if the rhs is a variable, if it is not the
3624 tmp = gfc_copy_alloc_comp (ts.derived, rse->expr, lse->expr, 0);
3625 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (), tmp);
3626 gfc_add_expr_to_block (&block, tmp);
3631 gfc_add_block_to_block (&block, &lse->pre);
3632 gfc_add_block_to_block (&block, &rse->pre);
3634 gfc_add_modify_expr (&block, lse->expr,
3635 fold_convert (TREE_TYPE (lse->expr), rse->expr));
3638 gfc_add_block_to_block (&block, &lse->post);
3639 gfc_add_block_to_block (&block, &rse->post);
3641 return gfc_finish_block (&block);
3645 /* Try to translate array(:) = func (...), where func is a transformational
3646 array function, without using a temporary. Returns NULL is this isn't the
3650 gfc_trans_arrayfunc_assign (gfc_expr * expr1, gfc_expr * expr2)
3655 bool seen_array_ref;
3657 /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION. */
3658 if (expr2->value.function.isym && !gfc_is_intrinsic_libcall (expr2))
3661 /* Elemental functions don't need a temporary anyway. */
3662 if (expr2->value.function.esym != NULL
3663 && expr2->value.function.esym->attr.elemental)
3666 /* Fail if EXPR1 can't be expressed as a descriptor. */
3667 if (gfc_ref_needs_temporary_p (expr1->ref))
3670 /* Functions returning pointers need temporaries. */
3671 if (expr2->symtree->n.sym->attr.pointer
3672 || expr2->symtree->n.sym->attr.allocatable)
3675 /* Character array functions need temporaries unless the
3676 character lengths are the same. */
3677 if (expr2->ts.type == BT_CHARACTER && expr2->rank > 0)
3679 if (expr1->ts.cl->length == NULL
3680 || expr1->ts.cl->length->expr_type != EXPR_CONSTANT)
3683 if (expr2->ts.cl->length == NULL
3684 || expr2->ts.cl->length->expr_type != EXPR_CONSTANT)
3687 if (mpz_cmp (expr1->ts.cl->length->value.integer,
3688 expr2->ts.cl->length->value.integer) != 0)
3692 /* Check that no LHS component references appear during an array
3693 reference. This is needed because we do not have the means to
3694 span any arbitrary stride with an array descriptor. This check
3695 is not needed for the rhs because the function result has to be
3697 seen_array_ref = false;
3698 for (ref = expr1->ref; ref; ref = ref->next)
3700 if (ref->type == REF_ARRAY)
3701 seen_array_ref= true;
3702 else if (ref->type == REF_COMPONENT && seen_array_ref)
3706 /* Check for a dependency. */
3707 if (gfc_check_fncall_dependency (expr1, INTENT_OUT,
3708 expr2->value.function.esym,
3709 expr2->value.function.actual))
3712 /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
3714 gcc_assert (expr2->value.function.isym
3715 || (gfc_return_by_reference (expr2->value.function.esym)
3716 && expr2->value.function.esym->result->attr.dimension));
3718 ss = gfc_walk_expr (expr1);
3719 gcc_assert (ss != gfc_ss_terminator);
3720 gfc_init_se (&se, NULL);
3721 gfc_start_block (&se.pre);
3722 se.want_pointer = 1;
3724 gfc_conv_array_parameter (&se, expr1, ss, 0);
3726 se.direct_byref = 1;
3727 se.ss = gfc_walk_expr (expr2);
3728 gcc_assert (se.ss != gfc_ss_terminator);
3729 gfc_conv_function_expr (&se, expr2);
3730 gfc_add_block_to_block (&se.pre, &se.post);
3732 return gfc_finish_block (&se.pre);
3735 /* Determine whether the given EXPR_CONSTANT is a zero initializer. */
3738 is_zero_initializer_p (gfc_expr * expr)
3740 if (expr->expr_type != EXPR_CONSTANT)
3743 /* We ignore constants with prescribed memory representations for now. */
3744 if (expr->representation.string)
3747 switch (expr->ts.type)
3750 return mpz_cmp_si (expr->value.integer, 0) == 0;
3753 return mpfr_zero_p (expr->value.real)
3754 && MPFR_SIGN (expr->value.real) >= 0;
3757 return expr->value.logical == 0;
3760 return mpfr_zero_p (expr->value.complex.r)
3761 && MPFR_SIGN (expr->value.complex.r) >= 0
3762 && mpfr_zero_p (expr->value.complex.i)
3763 && MPFR_SIGN (expr->value.complex.i) >= 0;
3771 /* Try to efficiently translate array(:) = 0. Return NULL if this
3775 gfc_trans_zero_assign (gfc_expr * expr)
3777 tree dest, len, type;
3781 sym = expr->symtree->n.sym;
3782 dest = gfc_get_symbol_decl (sym);
3784 type = TREE_TYPE (dest);
3785 if (POINTER_TYPE_P (type))
3786 type = TREE_TYPE (type);
3787 if (!GFC_ARRAY_TYPE_P (type))
3790 /* Determine the length of the array. */
3791 len = GFC_TYPE_ARRAY_SIZE (type);
3792 if (!len || TREE_CODE (len) != INTEGER_CST)
3795 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
3796 len = fold_build2 (MULT_EXPR, gfc_array_index_type, len,
3797 fold_convert (gfc_array_index_type, tmp));
3799 /* Convert arguments to the correct types. */
3800 if (!POINTER_TYPE_P (TREE_TYPE (dest)))
3801 dest = gfc_build_addr_expr (pvoid_type_node, dest);
3803 dest = fold_convert (pvoid_type_node, dest);
3804 len = fold_convert (size_type_node, len);
3806 /* Construct call to __builtin_memset. */
3807 tmp = build_call_expr (built_in_decls[BUILT_IN_MEMSET],
3808 3, dest, integer_zero_node, len);
3809 return fold_convert (void_type_node, tmp);
3813 /* Helper for gfc_trans_array_copy and gfc_trans_array_constructor_copy
3814 that constructs the call to __builtin_memcpy. */
3817 gfc_build_memcpy_call (tree dst, tree src, tree len)
3821 /* Convert arguments to the correct types. */
3822 if (!POINTER_TYPE_P (TREE_TYPE (dst)))
3823 dst = gfc_build_addr_expr (pvoid_type_node, dst);
3825 dst = fold_convert (pvoid_type_node, dst);
3827 if (!POINTER_TYPE_P (TREE_TYPE (src)))
3828 src = gfc_build_addr_expr (pvoid_type_node, src);
3830 src = fold_convert (pvoid_type_node, src);
3832 len = fold_convert (size_type_node, len);
3834 /* Construct call to __builtin_memcpy. */
3835 tmp = build_call_expr (built_in_decls[BUILT_IN_MEMCPY], 3, dst, src, len);
3836 return fold_convert (void_type_node, tmp);
3840 /* Try to efficiently translate dst(:) = src(:). Return NULL if this
3841 can't be done. EXPR1 is the destination/lhs and EXPR2 is the
3842 source/rhs, both are gfc_full_array_ref_p which have been checked for
3846 gfc_trans_array_copy (gfc_expr * expr1, gfc_expr * expr2)
3848 tree dst, dlen, dtype;
3849 tree src, slen, stype;
3852 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
3853 src = gfc_get_symbol_decl (expr2->symtree->n.sym);
3855 dtype = TREE_TYPE (dst);
3856 if (POINTER_TYPE_P (dtype))
3857 dtype = TREE_TYPE (dtype);
3858 stype = TREE_TYPE (src);
3859 if (POINTER_TYPE_P (stype))
3860 stype = TREE_TYPE (stype);
3862 if (!GFC_ARRAY_TYPE_P (dtype) || !GFC_ARRAY_TYPE_P (stype))
3865 /* Determine the lengths of the arrays. */
3866 dlen = GFC_TYPE_ARRAY_SIZE (dtype);
3867 if (!dlen || TREE_CODE (dlen) != INTEGER_CST)
3869 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
3870 dlen = fold_build2 (MULT_EXPR, gfc_array_index_type, dlen,
3871 fold_convert (gfc_array_index_type, tmp));
3873 slen = GFC_TYPE_ARRAY_SIZE (stype);
3874 if (!slen || TREE_CODE (slen) != INTEGER_CST)
3876 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (stype));
3877 slen = fold_build2 (MULT_EXPR, gfc_array_index_type, slen,
3878 fold_convert (gfc_array_index_type, tmp));
3880 /* Sanity check that they are the same. This should always be
3881 the case, as we should already have checked for conformance. */
3882 if (!tree_int_cst_equal (slen, dlen))
3885 return gfc_build_memcpy_call (dst, src, dlen);
3889 /* Try to efficiently translate array(:) = (/ ... /). Return NULL if
3890 this can't be done. EXPR1 is the destination/lhs for which
3891 gfc_full_array_ref_p is true, and EXPR2 is the source/rhs. */
3894 gfc_trans_array_constructor_copy (gfc_expr * expr1, gfc_expr * expr2)
3896 unsigned HOST_WIDE_INT nelem;
3902 nelem = gfc_constant_array_constructor_p (expr2->value.constructor);
3906 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
3907 dtype = TREE_TYPE (dst);
3908 if (POINTER_TYPE_P (dtype))
3909 dtype = TREE_TYPE (dtype);
3910 if (!GFC_ARRAY_TYPE_P (dtype))
3913 /* Determine the lengths of the array. */
3914 len = GFC_TYPE_ARRAY_SIZE (dtype);
3915 if (!len || TREE_CODE (len) != INTEGER_CST)
3918 /* Confirm that the constructor is the same size. */
3919 if (compare_tree_int (len, nelem) != 0)
3922 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
3923 len = fold_build2 (MULT_EXPR, gfc_array_index_type, len,
3924 fold_convert (gfc_array_index_type, tmp));
3926 stype = gfc_typenode_for_spec (&expr2->ts);
3927 src = gfc_build_constant_array_constructor (expr2, stype);
3929 stype = TREE_TYPE (src);
3930 if (POINTER_TYPE_P (stype))
3931 stype = TREE_TYPE (stype);
3933 return gfc_build_memcpy_call (dst, src, len);
3937 /* Subroutine of gfc_trans_assignment that actually scalarizes the
3938 assignment. EXPR1 is the destination/RHS and EXPR2 is the source/LHS. */
3941 gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag)
3946 gfc_ss *lss_section;
3954 /* Assignment of the form lhs = rhs. */
3955 gfc_start_block (&block);
3957 gfc_init_se (&lse, NULL);
3958 gfc_init_se (&rse, NULL);
3961 lss = gfc_walk_expr (expr1);
3963 if (lss != gfc_ss_terminator)
3965 /* The assignment needs scalarization. */
3968 /* Find a non-scalar SS from the lhs. */
3969 while (lss_section != gfc_ss_terminator
3970 && lss_section->type != GFC_SS_SECTION)
3971 lss_section = lss_section->next;
3973 gcc_assert (lss_section != gfc_ss_terminator);
3975 /* Initialize the scalarizer. */
3976 gfc_init_loopinfo (&loop);
3979 rss = gfc_walk_expr (expr2);
3980 if (rss == gfc_ss_terminator)
3982 /* The rhs is scalar. Add a ss for the expression. */
3983 rss = gfc_get_ss ();
3984 rss->next = gfc_ss_terminator;
3985 rss->type = GFC_SS_SCALAR;
3988 /* Associate the SS with the loop. */
3989 gfc_add_ss_to_loop (&loop, lss);
3990 gfc_add_ss_to_loop (&loop, rss);
3992 /* Calculate the bounds of the scalarization. */
3993 gfc_conv_ss_startstride (&loop);
3994 /* Resolve any data dependencies in the statement. */
3995 gfc_conv_resolve_dependencies (&loop, lss, rss);
3996 /* Setup the scalarizing loops. */
3997 gfc_conv_loop_setup (&loop);
3999 /* Setup the gfc_se structures. */
4000 gfc_copy_loopinfo_to_se (&lse, &loop);
4001 gfc_copy_loopinfo_to_se (&rse, &loop);
4004 gfc_mark_ss_chain_used (rss, 1);
4005 if (loop.temp_ss == NULL)
4008 gfc_mark_ss_chain_used (lss, 1);
4012 lse.ss = loop.temp_ss;
4013 gfc_mark_ss_chain_used (lss, 3);
4014 gfc_mark_ss_chain_used (loop.temp_ss, 3);
4017 /* Start the scalarized loop body. */
4018 gfc_start_scalarized_body (&loop, &body);
4021 gfc_init_block (&body);
4023 l_is_temp = (lss != gfc_ss_terminator && loop.temp_ss != NULL);
4025 /* Translate the expression. */
4026 gfc_conv_expr (&rse, expr2);
4030 gfc_conv_tmp_array_ref (&lse);
4031 gfc_advance_se_ss_chain (&lse);
4034 gfc_conv_expr (&lse, expr1);
4036 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
4037 l_is_temp || init_flag,
4038 expr2->expr_type == EXPR_VARIABLE);
4039 gfc_add_expr_to_block (&body, tmp);
4041 if (lss == gfc_ss_terminator)
4043 /* Use the scalar assignment as is. */
4044 gfc_add_block_to_block (&block, &body);
4048 gcc_assert (lse.ss == gfc_ss_terminator
4049 && rse.ss == gfc_ss_terminator);
4053 gfc_trans_scalarized_loop_boundary (&loop, &body);
4055 /* We need to copy the temporary to the actual lhs. */
4056 gfc_init_se (&lse, NULL);
4057 gfc_init_se (&rse, NULL);
4058 gfc_copy_loopinfo_to_se (&lse, &loop);
4059 gfc_copy_loopinfo_to_se (&rse, &loop);
4061 rse.ss = loop.temp_ss;
4064 gfc_conv_tmp_array_ref (&rse);
4065 gfc_advance_se_ss_chain (&rse);
4066 gfc_conv_expr (&lse, expr1);
4068 gcc_assert (lse.ss == gfc_ss_terminator
4069 && rse.ss == gfc_ss_terminator);
4071 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
4073 gfc_add_expr_to_block (&body, tmp);
4076 /* Generate the copying loops. */
4077 gfc_trans_scalarizing_loops (&loop, &body);
4079 /* Wrap the whole thing up. */
4080 gfc_add_block_to_block (&block, &loop.pre);
4081 gfc_add_block_to_block (&block, &loop.post);
4083 gfc_cleanup_loop (&loop);
4086 return gfc_finish_block (&block);
4090 /* Check whether EXPR is a copyable array. */
4093 copyable_array_p (gfc_expr * expr)
4095 if (expr->expr_type != EXPR_VARIABLE)
4098 /* First check it's an array. */
4099 if (expr->rank < 1 || !expr->ref || expr->ref->next)
4102 if (!gfc_full_array_ref_p (expr->ref))
4105 /* Next check that it's of a simple enough type. */
4106 switch (expr->ts.type)
4118 return !expr->ts.derived->attr.alloc_comp;
4127 /* Translate an assignment. */
4130 gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag)
4134 /* Special case a single function returning an array. */
4135 if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
4137 tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
4142 /* Special case assigning an array to zero. */
4143 if (copyable_array_p (expr1)
4144 && is_zero_initializer_p (expr2))
4146 tmp = gfc_trans_zero_assign (expr1);
4151 /* Special case copying one array to another. */
4152 if (copyable_array_p (expr1)
4153 && copyable_array_p (expr2)
4154 && gfc_compare_types (&expr1->ts, &expr2->ts)
4155 && !gfc_check_dependency (expr1, expr2, 0))
4157 tmp = gfc_trans_array_copy (expr1, expr2);
4162 /* Special case initializing an array from a constant array constructor. */
4163 if (copyable_array_p (expr1)
4164 && expr2->expr_type == EXPR_ARRAY
4165 && gfc_compare_types (&expr1->ts, &expr2->ts))
4167 tmp = gfc_trans_array_constructor_copy (expr1, expr2);
4172 /* Fallback to the scalarizer to generate explicit loops. */
4173 return gfc_trans_assignment_1 (expr1, expr2, init_flag);
4177 gfc_trans_init_assign (gfc_code * code)
4179 return gfc_trans_assignment (code->expr, code->expr2, true);
4183 gfc_trans_assign (gfc_code * code)
4185 return gfc_trans_assignment (code->expr, code->expr2, false);