1 /* Expression translation
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
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"
34 #include "langhooks.h"
38 #include "constructor.h"
40 #include "trans-const.h"
41 #include "trans-types.h"
42 #include "trans-array.h"
43 /* Only for gfc_trans_assign and gfc_trans_pointer_assign. */
44 #include "trans-stmt.h"
45 #include "dependency.h"
47 static tree gfc_trans_structure_assign (tree dest, gfc_expr * expr);
48 static void gfc_apply_interface_mapping_to_expr (gfc_interface_mapping *,
51 /* Copy the scalarization loop variables. */
54 gfc_copy_se_loopvars (gfc_se * dest, gfc_se * src)
57 dest->loop = src->loop;
61 /* Initialize a simple expression holder.
63 Care must be taken when multiple se are created with the same parent.
64 The child se must be kept in sync. The easiest way is to delay creation
65 of a child se until after after the previous se has been translated. */
68 gfc_init_se (gfc_se * se, gfc_se * parent)
70 memset (se, 0, sizeof (gfc_se));
71 gfc_init_block (&se->pre);
72 gfc_init_block (&se->post);
77 gfc_copy_se_loopvars (se, parent);
81 /* Advances to the next SS in the chain. Use this rather than setting
82 se->ss = se->ss->next because all the parents needs to be kept in sync.
86 gfc_advance_se_ss_chain (gfc_se * se)
90 gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
93 /* Walk down the parent chain. */
96 /* Simple consistency check. */
97 gcc_assert (p->parent == NULL || p->parent->ss == p->ss);
106 /* Ensures the result of the expression as either a temporary variable
107 or a constant so that it can be used repeatedly. */
110 gfc_make_safe_expr (gfc_se * se)
114 if (CONSTANT_CLASS_P (se->expr))
117 /* We need a temporary for this result. */
118 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
119 gfc_add_modify (&se->pre, var, se->expr);
124 /* Return an expression which determines if a dummy parameter is present.
125 Also used for arguments to procedures with multiple entry points. */
128 gfc_conv_expr_present (gfc_symbol * sym)
132 gcc_assert (sym->attr.dummy);
134 decl = gfc_get_symbol_decl (sym);
135 if (TREE_CODE (decl) != PARM_DECL)
137 /* Array parameters use a temporary descriptor, we want the real
139 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
140 || GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
141 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
143 return fold_build2 (NE_EXPR, boolean_type_node, decl,
144 fold_convert (TREE_TYPE (decl), null_pointer_node));
148 /* Converts a missing, dummy argument into a null or zero. */
151 gfc_conv_missing_dummy (gfc_se * se, gfc_expr * arg, gfc_typespec ts, int kind)
156 present = gfc_conv_expr_present (arg->symtree->n.sym);
160 /* Create a temporary and convert it to the correct type. */
161 tmp = gfc_get_int_type (kind);
162 tmp = fold_convert (tmp, build_fold_indirect_ref_loc (input_location,
165 /* Test for a NULL value. */
166 tmp = build3 (COND_EXPR, TREE_TYPE (tmp), present, tmp,
167 fold_convert (TREE_TYPE (tmp), integer_one_node));
168 tmp = gfc_evaluate_now (tmp, &se->pre);
169 se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
173 tmp = build3 (COND_EXPR, TREE_TYPE (se->expr), present, se->expr,
174 fold_convert (TREE_TYPE (se->expr), integer_zero_node));
175 tmp = gfc_evaluate_now (tmp, &se->pre);
179 if (ts.type == BT_CHARACTER)
181 tmp = build_int_cst (gfc_charlen_type_node, 0);
182 tmp = fold_build3 (COND_EXPR, gfc_charlen_type_node,
183 present, se->string_length, tmp);
184 tmp = gfc_evaluate_now (tmp, &se->pre);
185 se->string_length = tmp;
191 /* Get the character length of an expression, looking through gfc_refs
195 gfc_get_expr_charlen (gfc_expr *e)
200 gcc_assert (e->expr_type == EXPR_VARIABLE
201 && e->ts.type == BT_CHARACTER);
203 length = NULL; /* To silence compiler warning. */
205 if (is_subref_array (e) && e->ts.u.cl->length)
208 gfc_init_se (&tmpse, NULL);
209 gfc_conv_expr_type (&tmpse, e->ts.u.cl->length, gfc_charlen_type_node);
210 e->ts.u.cl->backend_decl = tmpse.expr;
214 /* First candidate: if the variable is of type CHARACTER, the
215 expression's length could be the length of the character
217 if (e->symtree->n.sym->ts.type == BT_CHARACTER)
218 length = e->symtree->n.sym->ts.u.cl->backend_decl;
220 /* Look through the reference chain for component references. */
221 for (r = e->ref; r; r = r->next)
226 if (r->u.c.component->ts.type == BT_CHARACTER)
227 length = r->u.c.component->ts.u.cl->backend_decl;
235 /* We should never got substring references here. These will be
236 broken down by the scalarizer. */
242 gcc_assert (length != NULL);
247 /* For each character array constructor subexpression without a ts.u.cl->length,
248 replace it by its first element (if there aren't any elements, the length
249 should already be set to zero). */
252 flatten_array_ctors_without_strlen (gfc_expr* e)
254 gfc_actual_arglist* arg;
260 switch (e->expr_type)
264 flatten_array_ctors_without_strlen (e->value.op.op1);
265 flatten_array_ctors_without_strlen (e->value.op.op2);
269 /* TODO: Implement as with EXPR_FUNCTION when needed. */
273 for (arg = e->value.function.actual; arg; arg = arg->next)
274 flatten_array_ctors_without_strlen (arg->expr);
279 /* We've found what we're looking for. */
280 if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
285 gcc_assert (e->value.constructor);
287 c = gfc_constructor_first (e->value.constructor);
291 flatten_array_ctors_without_strlen (new_expr);
292 gfc_replace_expr (e, new_expr);
296 /* Otherwise, fall through to handle constructor elements. */
298 for (c = gfc_constructor_first (e->value.constructor);
299 c; c = gfc_constructor_next (c))
300 flatten_array_ctors_without_strlen (c->expr);
310 /* Generate code to initialize a string length variable. Returns the
311 value. For array constructors, cl->length might be NULL and in this case,
312 the first element of the constructor is needed. expr is the original
313 expression so we can access it but can be NULL if this is not needed. */
316 gfc_conv_string_length (gfc_charlen * cl, gfc_expr * expr, stmtblock_t * pblock)
320 gfc_init_se (&se, NULL);
322 /* If cl->length is NULL, use gfc_conv_expr to obtain the string length but
323 "flatten" array constructors by taking their first element; all elements
324 should be the same length or a cl->length should be present. */
330 expr_flat = gfc_copy_expr (expr);
331 flatten_array_ctors_without_strlen (expr_flat);
332 gfc_resolve_expr (expr_flat);
334 gfc_conv_expr (&se, expr_flat);
335 gfc_add_block_to_block (pblock, &se.pre);
336 cl->backend_decl = convert (gfc_charlen_type_node, se.string_length);
338 gfc_free_expr (expr_flat);
342 /* Convert cl->length. */
344 gcc_assert (cl->length);
346 gfc_conv_expr_type (&se, cl->length, gfc_charlen_type_node);
347 se.expr = fold_build2 (MAX_EXPR, gfc_charlen_type_node, se.expr,
348 build_int_cst (gfc_charlen_type_node, 0));
349 gfc_add_block_to_block (pblock, &se.pre);
351 if (cl->backend_decl)
352 gfc_add_modify (pblock, cl->backend_decl, se.expr);
354 cl->backend_decl = gfc_evaluate_now (se.expr, pblock);
359 gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
360 const char *name, locus *where)
369 type = gfc_get_character_type (kind, ref->u.ss.length);
370 type = build_pointer_type (type);
372 gfc_init_se (&start, se);
373 gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
374 gfc_add_block_to_block (&se->pre, &start.pre);
376 if (integer_onep (start.expr))
377 gfc_conv_string_parameter (se);
382 /* Avoid multiple evaluation of substring start. */
383 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
384 start.expr = gfc_evaluate_now (start.expr, &se->pre);
386 /* Change the start of the string. */
387 if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
390 tmp = build_fold_indirect_ref_loc (input_location,
392 tmp = gfc_build_array_ref (tmp, start.expr, NULL);
393 se->expr = gfc_build_addr_expr (type, tmp);
396 /* Length = end + 1 - start. */
397 gfc_init_se (&end, se);
398 if (ref->u.ss.end == NULL)
399 end.expr = se->string_length;
402 gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
403 gfc_add_block_to_block (&se->pre, &end.pre);
407 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
408 end.expr = gfc_evaluate_now (end.expr, &se->pre);
410 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
412 tree nonempty = fold_build2 (LE_EXPR, boolean_type_node,
413 start.expr, end.expr);
415 /* Check lower bound. */
416 fault = fold_build2 (LT_EXPR, boolean_type_node, start.expr,
417 build_int_cst (gfc_charlen_type_node, 1));
418 fault = fold_build2 (TRUTH_ANDIF_EXPR, boolean_type_node,
421 asprintf (&msg, "Substring out of bounds: lower bound (%%ld) of '%s' "
422 "is less than one", name);
424 asprintf (&msg, "Substring out of bounds: lower bound (%%ld)"
426 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
427 fold_convert (long_integer_type_node,
431 /* Check upper bound. */
432 fault = fold_build2 (GT_EXPR, boolean_type_node, end.expr,
434 fault = fold_build2 (TRUTH_ANDIF_EXPR, boolean_type_node,
437 asprintf (&msg, "Substring out of bounds: upper bound (%%ld) of '%s' "
438 "exceeds string length (%%ld)", name);
440 asprintf (&msg, "Substring out of bounds: upper bound (%%ld) "
441 "exceeds string length (%%ld)");
442 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
443 fold_convert (long_integer_type_node, end.expr),
444 fold_convert (long_integer_type_node,
449 tmp = fold_build2 (MINUS_EXPR, gfc_charlen_type_node,
450 end.expr, start.expr);
451 tmp = fold_build2 (PLUS_EXPR, gfc_charlen_type_node,
452 build_int_cst (gfc_charlen_type_node, 1), tmp);
453 tmp = fold_build2 (MAX_EXPR, gfc_charlen_type_node, tmp,
454 build_int_cst (gfc_charlen_type_node, 0));
455 se->string_length = tmp;
459 /* Convert a derived type component reference. */
462 gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
469 c = ref->u.c.component;
471 gcc_assert (c->backend_decl);
473 field = c->backend_decl;
474 gcc_assert (TREE_CODE (field) == FIELD_DECL);
476 tmp = fold_build3 (COMPONENT_REF, TREE_TYPE (field), decl, field, NULL_TREE);
480 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer)
482 tmp = c->ts.u.cl->backend_decl;
483 /* Components must always be constant length. */
484 gcc_assert (tmp && INTEGER_CST_P (tmp));
485 se->string_length = tmp;
488 if (((c->attr.pointer || c->attr.allocatable) && c->attr.dimension == 0
489 && c->ts.type != BT_CHARACTER)
490 || c->attr.proc_pointer)
491 se->expr = build_fold_indirect_ref_loc (input_location,
496 /* This function deals with component references to components of the
497 parent type for derived type extensons. */
499 conv_parent_component_references (gfc_se * se, gfc_ref * ref)
507 c = ref->u.c.component;
509 /* Build a gfc_ref to recursively call gfc_conv_component_ref. */
510 parent.type = REF_COMPONENT;
513 parent.u.c.component = dt->components;
515 if (dt->backend_decl == NULL)
516 gfc_get_derived_type (dt);
518 if (dt->attr.extension && dt->components)
520 if (dt->attr.is_class)
521 cmp = dt->components;
523 cmp = dt->components->next;
524 /* Return if the component is not in the parent type. */
525 for (; cmp; cmp = cmp->next)
526 if (strcmp (c->name, cmp->name) == 0)
529 /* Otherwise build the reference and call self. */
530 gfc_conv_component_ref (se, &parent);
531 parent.u.c.sym = dt->components->ts.u.derived;
532 parent.u.c.component = c;
533 conv_parent_component_references (se, &parent);
537 /* Return the contents of a variable. Also handles reference/pointer
538 variables (all Fortran pointer references are implicit). */
541 gfc_conv_variable (gfc_se * se, gfc_expr * expr)
548 bool alternate_entry;
551 sym = expr->symtree->n.sym;
554 /* Check that something hasn't gone horribly wrong. */
555 gcc_assert (se->ss != gfc_ss_terminator);
556 gcc_assert (se->ss->expr == expr);
558 /* A scalarized term. We already know the descriptor. */
559 se->expr = se->ss->data.info.descriptor;
560 se->string_length = se->ss->string_length;
561 for (ref = se->ss->data.info.ref; ref; ref = ref->next)
562 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
567 tree se_expr = NULL_TREE;
569 se->expr = gfc_get_symbol_decl (sym);
571 /* Deal with references to a parent results or entries by storing
572 the current_function_decl and moving to the parent_decl. */
573 return_value = sym->attr.function && sym->result == sym;
574 alternate_entry = sym->attr.function && sym->attr.entry
575 && sym->result == sym;
576 entry_master = sym->attr.result
577 && sym->ns->proc_name->attr.entry_master
578 && !gfc_return_by_reference (sym->ns->proc_name);
579 parent_decl = DECL_CONTEXT (current_function_decl);
581 if ((se->expr == parent_decl && return_value)
582 || (sym->ns && sym->ns->proc_name
584 && sym->ns->proc_name->backend_decl == parent_decl
585 && (alternate_entry || entry_master)))
590 /* Special case for assigning the return value of a function.
591 Self recursive functions must have an explicit return value. */
592 if (return_value && (se->expr == current_function_decl || parent_flag))
593 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
595 /* Similarly for alternate entry points. */
596 else if (alternate_entry
597 && (sym->ns->proc_name->backend_decl == current_function_decl
600 gfc_entry_list *el = NULL;
602 for (el = sym->ns->entries; el; el = el->next)
605 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
610 else if (entry_master
611 && (sym->ns->proc_name->backend_decl == current_function_decl
613 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
618 /* Procedure actual arguments. */
619 else if (sym->attr.flavor == FL_PROCEDURE
620 && se->expr != current_function_decl)
622 if (!sym->attr.dummy && !sym->attr.proc_pointer)
624 gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
625 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
631 /* Dereference the expression, where needed. Since characters
632 are entirely different from other types, they are treated
634 if (sym->ts.type == BT_CHARACTER)
636 /* Dereference character pointer dummy arguments
638 if ((sym->attr.pointer || sym->attr.allocatable)
640 || sym->attr.function
641 || sym->attr.result))
642 se->expr = build_fold_indirect_ref_loc (input_location,
646 else if (!sym->attr.value)
648 /* Dereference non-character scalar dummy arguments. */
649 if (sym->attr.dummy && !sym->attr.dimension)
650 se->expr = build_fold_indirect_ref_loc (input_location,
653 /* Dereference scalar hidden result. */
654 if (gfc_option.flag_f2c && sym->ts.type == BT_COMPLEX
655 && (sym->attr.function || sym->attr.result)
656 && !sym->attr.dimension && !sym->attr.pointer
657 && !sym->attr.always_explicit)
658 se->expr = build_fold_indirect_ref_loc (input_location,
661 /* Dereference non-character pointer variables.
662 These must be dummies, results, or scalars. */
663 if ((sym->attr.pointer || sym->attr.allocatable)
665 || sym->attr.function
667 || !sym->attr.dimension))
668 se->expr = build_fold_indirect_ref_loc (input_location,
675 /* For character variables, also get the length. */
676 if (sym->ts.type == BT_CHARACTER)
678 /* If the character length of an entry isn't set, get the length from
679 the master function instead. */
680 if (sym->attr.entry && !sym->ts.u.cl->backend_decl)
681 se->string_length = sym->ns->proc_name->ts.u.cl->backend_decl;
683 se->string_length = sym->ts.u.cl->backend_decl;
684 gcc_assert (se->string_length);
692 /* Return the descriptor if that's what we want and this is an array
693 section reference. */
694 if (se->descriptor_only && ref->u.ar.type != AR_ELEMENT)
696 /* TODO: Pointers to single elements of array sections, eg elemental subs. */
697 /* Return the descriptor for array pointers and allocations. */
699 && ref->next == NULL && (se->descriptor_only))
702 gfc_conv_array_ref (se, &ref->u.ar, sym, &expr->where);
703 /* Return a pointer to an element. */
707 if (ref->u.c.sym->attr.extension)
708 conv_parent_component_references (se, ref);
710 gfc_conv_component_ref (se, ref);
714 gfc_conv_substring (se, ref, expr->ts.kind,
715 expr->symtree->name, &expr->where);
724 /* Pointer assignment, allocation or pass by reference. Arrays are handled
726 if (se->want_pointer)
728 if (expr->ts.type == BT_CHARACTER && !gfc_is_proc_ptr_comp (expr, NULL))
729 gfc_conv_string_parameter (se);
731 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
736 /* Unary ops are easy... Or they would be if ! was a valid op. */
739 gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
744 gcc_assert (expr->ts.type != BT_CHARACTER);
745 /* Initialize the operand. */
746 gfc_init_se (&operand, se);
747 gfc_conv_expr_val (&operand, expr->value.op.op1);
748 gfc_add_block_to_block (&se->pre, &operand.pre);
750 type = gfc_typenode_for_spec (&expr->ts);
752 /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
753 We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
754 All other unary operators have an equivalent GIMPLE unary operator. */
755 if (code == TRUTH_NOT_EXPR)
756 se->expr = fold_build2 (EQ_EXPR, type, operand.expr,
757 build_int_cst (type, 0));
759 se->expr = fold_build1 (code, type, operand.expr);
763 /* Expand power operator to optimal multiplications when a value is raised
764 to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
765 Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
766 Programming", 3rd Edition, 1998. */
768 /* This code is mostly duplicated from expand_powi in the backend.
769 We establish the "optimal power tree" lookup table with the defined size.
770 The items in the table are the exponents used to calculate the index
771 exponents. Any integer n less than the value can get an "addition chain",
772 with the first node being one. */
773 #define POWI_TABLE_SIZE 256
775 /* The table is from builtins.c. */
776 static const unsigned char powi_table[POWI_TABLE_SIZE] =
778 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
779 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
780 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
781 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
782 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
783 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
784 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
785 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
786 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
787 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
788 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
789 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
790 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
791 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
792 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
793 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
794 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
795 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
796 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
797 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
798 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
799 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
800 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
801 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
802 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
803 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
804 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
805 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
806 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
807 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
808 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
809 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
812 /* If n is larger than lookup table's max index, we use the "window
814 #define POWI_WINDOW_SIZE 3
816 /* Recursive function to expand the power operator. The temporary
817 values are put in tmpvar. The function returns tmpvar[1] ** n. */
819 gfc_conv_powi (gfc_se * se, unsigned HOST_WIDE_INT n, tree * tmpvar)
826 if (n < POWI_TABLE_SIZE)
831 op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
832 op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
836 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
837 op0 = gfc_conv_powi (se, n - digit, tmpvar);
838 op1 = gfc_conv_powi (se, digit, tmpvar);
842 op0 = gfc_conv_powi (se, n >> 1, tmpvar);
846 tmp = fold_build2 (MULT_EXPR, TREE_TYPE (op0), op0, op1);
847 tmp = gfc_evaluate_now (tmp, &se->pre);
849 if (n < POWI_TABLE_SIZE)
856 /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
857 return 1. Else return 0 and a call to runtime library functions
858 will have to be built. */
860 gfc_conv_cst_int_power (gfc_se * se, tree lhs, tree rhs)
865 tree vartmp[POWI_TABLE_SIZE];
867 unsigned HOST_WIDE_INT n;
870 /* If exponent is too large, we won't expand it anyway, so don't bother
871 with large integer values. */
872 if (!double_int_fits_in_shwi_p (TREE_INT_CST (rhs)))
875 m = double_int_to_shwi (TREE_INT_CST (rhs));
876 /* There's no ABS for HOST_WIDE_INT, so here we go. It also takes care
877 of the asymmetric range of the integer type. */
878 n = (unsigned HOST_WIDE_INT) (m < 0 ? -m : m);
880 type = TREE_TYPE (lhs);
881 sgn = tree_int_cst_sgn (rhs);
883 if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
884 || optimize_size) && (m > 2 || m < -1))
890 se->expr = gfc_build_const (type, integer_one_node);
894 /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1. */
895 if ((sgn == -1) && (TREE_CODE (type) == INTEGER_TYPE))
897 tmp = fold_build2 (EQ_EXPR, boolean_type_node,
898 lhs, build_int_cst (TREE_TYPE (lhs), -1));
899 cond = fold_build2 (EQ_EXPR, boolean_type_node,
900 lhs, build_int_cst (TREE_TYPE (lhs), 1));
903 result = (lhs == 1 || lhs == -1) ? 1 : 0. */
906 tmp = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, tmp, cond);
907 se->expr = fold_build3 (COND_EXPR, type,
908 tmp, build_int_cst (type, 1),
909 build_int_cst (type, 0));
913 result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0. */
914 tmp = fold_build3 (COND_EXPR, type, tmp, build_int_cst (type, -1),
915 build_int_cst (type, 0));
916 se->expr = fold_build3 (COND_EXPR, type,
917 cond, build_int_cst (type, 1), tmp);
921 memset (vartmp, 0, sizeof (vartmp));
925 tmp = gfc_build_const (type, integer_one_node);
926 vartmp[1] = fold_build2 (RDIV_EXPR, type, tmp, vartmp[1]);
929 se->expr = gfc_conv_powi (se, n, vartmp);
935 /* Power op (**). Constant integer exponent has special handling. */
938 gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
940 tree gfc_int4_type_node;
947 gfc_init_se (&lse, se);
948 gfc_conv_expr_val (&lse, expr->value.op.op1);
949 lse.expr = gfc_evaluate_now (lse.expr, &lse.pre);
950 gfc_add_block_to_block (&se->pre, &lse.pre);
952 gfc_init_se (&rse, se);
953 gfc_conv_expr_val (&rse, expr->value.op.op2);
954 gfc_add_block_to_block (&se->pre, &rse.pre);
956 if (expr->value.op.op2->ts.type == BT_INTEGER
957 && expr->value.op.op2->expr_type == EXPR_CONSTANT)
958 if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
961 gfc_int4_type_node = gfc_get_int_type (4);
963 kind = expr->value.op.op1->ts.kind;
964 switch (expr->value.op.op2->ts.type)
967 ikind = expr->value.op.op2->ts.kind;
972 rse.expr = convert (gfc_int4_type_node, rse.expr);
994 if (expr->value.op.op1->ts.type == BT_INTEGER)
995 lse.expr = convert (gfc_int4_type_node, lse.expr);
1020 switch (expr->value.op.op1->ts.type)
1023 if (kind == 3) /* Case 16 was not handled properly above. */
1025 fndecl = gfor_fndecl_math_powi[kind][ikind].integer;
1029 /* Use builtins for real ** int4. */
1035 fndecl = built_in_decls[BUILT_IN_POWIF];
1039 fndecl = built_in_decls[BUILT_IN_POWI];
1044 fndecl = built_in_decls[BUILT_IN_POWIL];
1052 fndecl = gfor_fndecl_math_powi[kind][ikind].real;
1056 fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
1068 fndecl = built_in_decls[BUILT_IN_POWF];
1071 fndecl = built_in_decls[BUILT_IN_POW];
1075 fndecl = built_in_decls[BUILT_IN_POWL];
1086 fndecl = built_in_decls[BUILT_IN_CPOWF];
1089 fndecl = built_in_decls[BUILT_IN_CPOW];
1093 fndecl = built_in_decls[BUILT_IN_CPOWL];
1105 se->expr = build_call_expr_loc (input_location,
1106 fndecl, 2, lse.expr, rse.expr);
1110 /* Generate code to allocate a string temporary. */
1113 gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
1118 gcc_assert (types_compatible_p (TREE_TYPE (len), gfc_charlen_type_node));
1120 if (gfc_can_put_var_on_stack (len))
1122 /* Create a temporary variable to hold the result. */
1123 tmp = fold_build2 (MINUS_EXPR, gfc_charlen_type_node, len,
1124 build_int_cst (gfc_charlen_type_node, 1));
1125 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node, tmp);
1127 if (TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
1128 tmp = build_array_type (TREE_TYPE (TREE_TYPE (type)), tmp);
1130 tmp = build_array_type (TREE_TYPE (type), tmp);
1132 var = gfc_create_var (tmp, "str");
1133 var = gfc_build_addr_expr (type, var);
1137 /* Allocate a temporary to hold the result. */
1138 var = gfc_create_var (type, "pstr");
1139 tmp = gfc_call_malloc (&se->pre, type,
1140 fold_build2 (MULT_EXPR, TREE_TYPE (len), len,
1141 fold_convert (TREE_TYPE (len),
1142 TYPE_SIZE (type))));
1143 gfc_add_modify (&se->pre, var, tmp);
1145 /* Free the temporary afterwards. */
1146 tmp = gfc_call_free (convert (pvoid_type_node, var));
1147 gfc_add_expr_to_block (&se->post, tmp);
1154 /* Handle a string concatenation operation. A temporary will be allocated to
1158 gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
1161 tree len, type, var, tmp, fndecl;
1163 gcc_assert (expr->value.op.op1->ts.type == BT_CHARACTER
1164 && expr->value.op.op2->ts.type == BT_CHARACTER);
1165 gcc_assert (expr->value.op.op1->ts.kind == expr->value.op.op2->ts.kind);
1167 gfc_init_se (&lse, se);
1168 gfc_conv_expr (&lse, expr->value.op.op1);
1169 gfc_conv_string_parameter (&lse);
1170 gfc_init_se (&rse, se);
1171 gfc_conv_expr (&rse, expr->value.op.op2);
1172 gfc_conv_string_parameter (&rse);
1174 gfc_add_block_to_block (&se->pre, &lse.pre);
1175 gfc_add_block_to_block (&se->pre, &rse.pre);
1177 type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
1178 len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
1179 if (len == NULL_TREE)
1181 len = fold_build2 (PLUS_EXPR, TREE_TYPE (lse.string_length),
1182 lse.string_length, rse.string_length);
1185 type = build_pointer_type (type);
1187 var = gfc_conv_string_tmp (se, type, len);
1189 /* Do the actual concatenation. */
1190 if (expr->ts.kind == 1)
1191 fndecl = gfor_fndecl_concat_string;
1192 else if (expr->ts.kind == 4)
1193 fndecl = gfor_fndecl_concat_string_char4;
1197 tmp = build_call_expr_loc (input_location,
1198 fndecl, 6, len, var, lse.string_length, lse.expr,
1199 rse.string_length, rse.expr);
1200 gfc_add_expr_to_block (&se->pre, tmp);
1202 /* Add the cleanup for the operands. */
1203 gfc_add_block_to_block (&se->pre, &rse.post);
1204 gfc_add_block_to_block (&se->pre, &lse.post);
1207 se->string_length = len;
1210 /* Translates an op expression. Common (binary) cases are handled by this
1211 function, others are passed on. Recursion is used in either case.
1212 We use the fact that (op1.ts == op2.ts) (except for the power
1214 Operators need no special handling for scalarized expressions as long as
1215 they call gfc_conv_simple_val to get their operands.
1216 Character strings get special handling. */
1219 gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
1221 enum tree_code code;
1230 switch (expr->value.op.op)
1232 case INTRINSIC_PARENTHESES:
1233 if ((expr->ts.type == BT_REAL
1234 || expr->ts.type == BT_COMPLEX)
1235 && gfc_option.flag_protect_parens)
1237 gfc_conv_unary_op (PAREN_EXPR, se, expr);
1238 gcc_assert (FLOAT_TYPE_P (TREE_TYPE (se->expr)));
1243 case INTRINSIC_UPLUS:
1244 gfc_conv_expr (se, expr->value.op.op1);
1247 case INTRINSIC_UMINUS:
1248 gfc_conv_unary_op (NEGATE_EXPR, se, expr);
1252 gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
1255 case INTRINSIC_PLUS:
1259 case INTRINSIC_MINUS:
1263 case INTRINSIC_TIMES:
1267 case INTRINSIC_DIVIDE:
1268 /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
1269 an integer, we must round towards zero, so we use a
1271 if (expr->ts.type == BT_INTEGER)
1272 code = TRUNC_DIV_EXPR;
1277 case INTRINSIC_POWER:
1278 gfc_conv_power_op (se, expr);
1281 case INTRINSIC_CONCAT:
1282 gfc_conv_concat_op (se, expr);
1286 code = TRUTH_ANDIF_EXPR;
1291 code = TRUTH_ORIF_EXPR;
1295 /* EQV and NEQV only work on logicals, but since we represent them
1296 as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE. */
1298 case INTRINSIC_EQ_OS:
1306 case INTRINSIC_NE_OS:
1307 case INTRINSIC_NEQV:
1314 case INTRINSIC_GT_OS:
1321 case INTRINSIC_GE_OS:
1328 case INTRINSIC_LT_OS:
1335 case INTRINSIC_LE_OS:
1341 case INTRINSIC_USER:
1342 case INTRINSIC_ASSIGN:
1343 /* These should be converted into function calls by the frontend. */
1347 fatal_error ("Unknown intrinsic op");
1351 /* The only exception to this is **, which is handled separately anyway. */
1352 gcc_assert (expr->value.op.op1->ts.type == expr->value.op.op2->ts.type);
1354 if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
1358 gfc_init_se (&lse, se);
1359 gfc_conv_expr (&lse, expr->value.op.op1);
1360 gfc_add_block_to_block (&se->pre, &lse.pre);
1363 gfc_init_se (&rse, se);
1364 gfc_conv_expr (&rse, expr->value.op.op2);
1365 gfc_add_block_to_block (&se->pre, &rse.pre);
1369 gfc_conv_string_parameter (&lse);
1370 gfc_conv_string_parameter (&rse);
1372 lse.expr = gfc_build_compare_string (lse.string_length, lse.expr,
1373 rse.string_length, rse.expr,
1374 expr->value.op.op1->ts.kind);
1375 rse.expr = build_int_cst (TREE_TYPE (lse.expr), 0);
1376 gfc_add_block_to_block (&lse.post, &rse.post);
1379 type = gfc_typenode_for_spec (&expr->ts);
1383 /* The result of logical ops is always boolean_type_node. */
1384 tmp = fold_build2 (code, boolean_type_node, lse.expr, rse.expr);
1385 se->expr = convert (type, tmp);
1388 se->expr = fold_build2 (code, type, lse.expr, rse.expr);
1390 /* Add the post blocks. */
1391 gfc_add_block_to_block (&se->post, &rse.post);
1392 gfc_add_block_to_block (&se->post, &lse.post);
1395 /* If a string's length is one, we convert it to a single character. */
1398 string_to_single_character (tree len, tree str, int kind)
1400 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str)));
1402 if (INTEGER_CST_P (len) && TREE_INT_CST_LOW (len) == 1
1403 && TREE_INT_CST_HIGH (len) == 0)
1405 str = fold_convert (gfc_get_pchar_type (kind), str);
1406 return build_fold_indirect_ref_loc (input_location,
1415 gfc_conv_scalar_char_value (gfc_symbol *sym, gfc_se *se, gfc_expr **expr)
1418 if (sym->backend_decl)
1420 /* This becomes the nominal_type in
1421 function.c:assign_parm_find_data_types. */
1422 TREE_TYPE (sym->backend_decl) = unsigned_char_type_node;
1423 /* This becomes the passed_type in
1424 function.c:assign_parm_find_data_types. C promotes char to
1425 integer for argument passing. */
1426 DECL_ARG_TYPE (sym->backend_decl) = unsigned_type_node;
1428 DECL_BY_REFERENCE (sym->backend_decl) = 0;
1433 /* If we have a constant character expression, make it into an
1435 if ((*expr)->expr_type == EXPR_CONSTANT)
1440 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL,
1441 (int)(*expr)->value.character.string[0]);
1442 if ((*expr)->ts.kind != gfc_c_int_kind)
1444 /* The expr needs to be compatible with a C int. If the
1445 conversion fails, then the 2 causes an ICE. */
1446 ts.type = BT_INTEGER;
1447 ts.kind = gfc_c_int_kind;
1448 gfc_convert_type (*expr, &ts, 2);
1451 else if (se != NULL && (*expr)->expr_type == EXPR_VARIABLE)
1453 if ((*expr)->ref == NULL)
1455 se->expr = string_to_single_character
1456 (build_int_cst (integer_type_node, 1),
1457 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
1459 ((*expr)->symtree->n.sym)),
1464 gfc_conv_variable (se, *expr);
1465 se->expr = string_to_single_character
1466 (build_int_cst (integer_type_node, 1),
1467 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
1476 /* Compare two strings. If they are all single characters, the result is the
1477 subtraction of them. Otherwise, we build a library call. */
1480 gfc_build_compare_string (tree len1, tree str1, tree len2, tree str2, int kind)
1486 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1)));
1487 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2)));
1489 sc1 = string_to_single_character (len1, str1, kind);
1490 sc2 = string_to_single_character (len2, str2, kind);
1492 if (sc1 != NULL_TREE && sc2 != NULL_TREE)
1494 /* Deal with single character specially. */
1495 sc1 = fold_convert (integer_type_node, sc1);
1496 sc2 = fold_convert (integer_type_node, sc2);
1497 tmp = fold_build2 (MINUS_EXPR, integer_type_node, sc1, sc2);
1501 /* Build a call for the comparison. */
1505 fndecl = gfor_fndecl_compare_string;
1507 fndecl = gfor_fndecl_compare_string_char4;
1511 tmp = build_call_expr_loc (input_location,
1512 fndecl, 4, len1, str1, len2, str2);
1519 /* Return the backend_decl for a procedure pointer component. */
1522 get_proc_ptr_comp (gfc_expr *e)
1526 gfc_init_se (&comp_se, NULL);
1527 e2 = gfc_copy_expr (e);
1528 e2->expr_type = EXPR_VARIABLE;
1529 gfc_conv_expr (&comp_se, e2);
1531 return build_fold_addr_expr_loc (input_location, comp_se.expr);
1536 conv_function_val (gfc_se * se, gfc_symbol * sym, gfc_expr * expr)
1540 if (gfc_is_proc_ptr_comp (expr, NULL))
1541 tmp = get_proc_ptr_comp (expr);
1542 else if (sym->attr.dummy)
1544 tmp = gfc_get_symbol_decl (sym);
1545 if (sym->attr.proc_pointer)
1546 tmp = build_fold_indirect_ref_loc (input_location,
1548 gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE
1549 && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) == FUNCTION_TYPE);
1553 if (!sym->backend_decl)
1554 sym->backend_decl = gfc_get_extern_function_decl (sym);
1556 tmp = sym->backend_decl;
1558 if (sym->attr.cray_pointee)
1560 /* TODO - make the cray pointee a pointer to a procedure,
1561 assign the pointer to it and use it for the call. This
1563 tmp = convert (build_pointer_type (TREE_TYPE (tmp)),
1564 gfc_get_symbol_decl (sym->cp_pointer));
1565 tmp = gfc_evaluate_now (tmp, &se->pre);
1568 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
1570 gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
1571 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
1578 /* Initialize MAPPING. */
1581 gfc_init_interface_mapping (gfc_interface_mapping * mapping)
1583 mapping->syms = NULL;
1584 mapping->charlens = NULL;
1588 /* Free all memory held by MAPPING (but not MAPPING itself). */
1591 gfc_free_interface_mapping (gfc_interface_mapping * mapping)
1593 gfc_interface_sym_mapping *sym;
1594 gfc_interface_sym_mapping *nextsym;
1596 gfc_charlen *nextcl;
1598 for (sym = mapping->syms; sym; sym = nextsym)
1600 nextsym = sym->next;
1601 sym->new_sym->n.sym->formal = NULL;
1602 gfc_free_symbol (sym->new_sym->n.sym);
1603 gfc_free_expr (sym->expr);
1604 gfc_free (sym->new_sym);
1607 for (cl = mapping->charlens; cl; cl = nextcl)
1610 gfc_free_expr (cl->length);
1616 /* Return a copy of gfc_charlen CL. Add the returned structure to
1617 MAPPING so that it will be freed by gfc_free_interface_mapping. */
1619 static gfc_charlen *
1620 gfc_get_interface_mapping_charlen (gfc_interface_mapping * mapping,
1623 gfc_charlen *new_charlen;
1625 new_charlen = gfc_get_charlen ();
1626 new_charlen->next = mapping->charlens;
1627 new_charlen->length = gfc_copy_expr (cl->length);
1629 mapping->charlens = new_charlen;
1634 /* A subroutine of gfc_add_interface_mapping. Return a descriptorless
1635 array variable that can be used as the actual argument for dummy
1636 argument SYM. Add any initialization code to BLOCK. PACKED is as
1637 for gfc_get_nodesc_array_type and DATA points to the first element
1638 in the passed array. */
1641 gfc_get_interface_mapping_array (stmtblock_t * block, gfc_symbol * sym,
1642 gfc_packed packed, tree data)
1647 type = gfc_typenode_for_spec (&sym->ts);
1648 type = gfc_get_nodesc_array_type (type, sym->as, packed,
1649 !sym->attr.target && !sym->attr.pointer
1650 && !sym->attr.proc_pointer);
1652 var = gfc_create_var (type, "ifm");
1653 gfc_add_modify (block, var, fold_convert (type, data));
1659 /* A subroutine of gfc_add_interface_mapping. Set the stride, upper bounds
1660 and offset of descriptorless array type TYPE given that it has the same
1661 size as DESC. Add any set-up code to BLOCK. */
1664 gfc_set_interface_mapping_bounds (stmtblock_t * block, tree type, tree desc)
1671 offset = gfc_index_zero_node;
1672 for (n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
1674 dim = gfc_rank_cst[n];
1675 GFC_TYPE_ARRAY_STRIDE (type, n) = gfc_conv_array_stride (desc, n);
1676 if (GFC_TYPE_ARRAY_LBOUND (type, n) == NULL_TREE)
1678 GFC_TYPE_ARRAY_LBOUND (type, n)
1679 = gfc_conv_descriptor_lbound_get (desc, dim);
1680 GFC_TYPE_ARRAY_UBOUND (type, n)
1681 = gfc_conv_descriptor_ubound_get (desc, dim);
1683 else if (GFC_TYPE_ARRAY_UBOUND (type, n) == NULL_TREE)
1685 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
1686 gfc_conv_descriptor_ubound_get (desc, dim),
1687 gfc_conv_descriptor_lbound_get (desc, dim));
1688 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1689 GFC_TYPE_ARRAY_LBOUND (type, n),
1691 tmp = gfc_evaluate_now (tmp, block);
1692 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1694 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type,
1695 GFC_TYPE_ARRAY_LBOUND (type, n),
1696 GFC_TYPE_ARRAY_STRIDE (type, n));
1697 offset = fold_build2 (MINUS_EXPR, gfc_array_index_type, offset, tmp);
1699 offset = gfc_evaluate_now (offset, block);
1700 GFC_TYPE_ARRAY_OFFSET (type) = offset;
1704 /* Extend MAPPING so that it maps dummy argument SYM to the value stored
1705 in SE. The caller may still use se->expr and se->string_length after
1706 calling this function. */
1709 gfc_add_interface_mapping (gfc_interface_mapping * mapping,
1710 gfc_symbol * sym, gfc_se * se,
1713 gfc_interface_sym_mapping *sm;
1717 gfc_symbol *new_sym;
1719 gfc_symtree *new_symtree;
1721 /* Create a new symbol to represent the actual argument. */
1722 new_sym = gfc_new_symbol (sym->name, NULL);
1723 new_sym->ts = sym->ts;
1724 new_sym->as = gfc_copy_array_spec (sym->as);
1725 new_sym->attr.referenced = 1;
1726 new_sym->attr.dimension = sym->attr.dimension;
1727 new_sym->attr.codimension = sym->attr.codimension;
1728 new_sym->attr.pointer = sym->attr.pointer;
1729 new_sym->attr.allocatable = sym->attr.allocatable;
1730 new_sym->attr.flavor = sym->attr.flavor;
1731 new_sym->attr.function = sym->attr.function;
1733 /* Ensure that the interface is available and that
1734 descriptors are passed for array actual arguments. */
1735 if (sym->attr.flavor == FL_PROCEDURE)
1737 new_sym->formal = expr->symtree->n.sym->formal;
1738 new_sym->attr.always_explicit
1739 = expr->symtree->n.sym->attr.always_explicit;
1742 /* Create a fake symtree for it. */
1744 new_symtree = gfc_new_symtree (&root, sym->name);
1745 new_symtree->n.sym = new_sym;
1746 gcc_assert (new_symtree == root);
1748 /* Create a dummy->actual mapping. */
1749 sm = XCNEW (gfc_interface_sym_mapping);
1750 sm->next = mapping->syms;
1752 sm->new_sym = new_symtree;
1753 sm->expr = gfc_copy_expr (expr);
1756 /* Stabilize the argument's value. */
1757 if (!sym->attr.function && se)
1758 se->expr = gfc_evaluate_now (se->expr, &se->pre);
1760 if (sym->ts.type == BT_CHARACTER)
1762 /* Create a copy of the dummy argument's length. */
1763 new_sym->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, sym->ts.u.cl);
1764 sm->expr->ts.u.cl = new_sym->ts.u.cl;
1766 /* If the length is specified as "*", record the length that
1767 the caller is passing. We should use the callee's length
1768 in all other cases. */
1769 if (!new_sym->ts.u.cl->length && se)
1771 se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
1772 new_sym->ts.u.cl->backend_decl = se->string_length;
1779 /* Use the passed value as-is if the argument is a function. */
1780 if (sym->attr.flavor == FL_PROCEDURE)
1783 /* If the argument is either a string or a pointer to a string,
1784 convert it to a boundless character type. */
1785 else if (!sym->attr.dimension && sym->ts.type == BT_CHARACTER)
1787 tmp = gfc_get_character_type_len (sym->ts.kind, NULL);
1788 tmp = build_pointer_type (tmp);
1789 if (sym->attr.pointer)
1790 value = build_fold_indirect_ref_loc (input_location,
1794 value = fold_convert (tmp, value);
1797 /* If the argument is a scalar, a pointer to an array or an allocatable,
1799 else if (!sym->attr.dimension || sym->attr.pointer || sym->attr.allocatable)
1800 value = build_fold_indirect_ref_loc (input_location,
1803 /* For character(*), use the actual argument's descriptor. */
1804 else if (sym->ts.type == BT_CHARACTER && !new_sym->ts.u.cl->length)
1805 value = build_fold_indirect_ref_loc (input_location,
1808 /* If the argument is an array descriptor, use it to determine
1809 information about the actual argument's shape. */
1810 else if (POINTER_TYPE_P (TREE_TYPE (se->expr))
1811 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
1813 /* Get the actual argument's descriptor. */
1814 desc = build_fold_indirect_ref_loc (input_location,
1817 /* Create the replacement variable. */
1818 tmp = gfc_conv_descriptor_data_get (desc);
1819 value = gfc_get_interface_mapping_array (&se->pre, sym,
1822 /* Use DESC to work out the upper bounds, strides and offset. */
1823 gfc_set_interface_mapping_bounds (&se->pre, TREE_TYPE (value), desc);
1826 /* Otherwise we have a packed array. */
1827 value = gfc_get_interface_mapping_array (&se->pre, sym,
1828 PACKED_FULL, se->expr);
1830 new_sym->backend_decl = value;
1834 /* Called once all dummy argument mappings have been added to MAPPING,
1835 but before the mapping is used to evaluate expressions. Pre-evaluate
1836 the length of each argument, adding any initialization code to PRE and
1837 any finalization code to POST. */
1840 gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
1841 stmtblock_t * pre, stmtblock_t * post)
1843 gfc_interface_sym_mapping *sym;
1847 for (sym = mapping->syms; sym; sym = sym->next)
1848 if (sym->new_sym->n.sym->ts.type == BT_CHARACTER
1849 && !sym->new_sym->n.sym->ts.u.cl->backend_decl)
1851 expr = sym->new_sym->n.sym->ts.u.cl->length;
1852 gfc_apply_interface_mapping_to_expr (mapping, expr);
1853 gfc_init_se (&se, NULL);
1854 gfc_conv_expr (&se, expr);
1855 se.expr = fold_convert (gfc_charlen_type_node, se.expr);
1856 se.expr = gfc_evaluate_now (se.expr, &se.pre);
1857 gfc_add_block_to_block (pre, &se.pre);
1858 gfc_add_block_to_block (post, &se.post);
1860 sym->new_sym->n.sym->ts.u.cl->backend_decl = se.expr;
1865 /* Like gfc_apply_interface_mapping_to_expr, but applied to
1869 gfc_apply_interface_mapping_to_cons (gfc_interface_mapping * mapping,
1870 gfc_constructor_base base)
1873 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1875 gfc_apply_interface_mapping_to_expr (mapping, c->expr);
1878 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->start);
1879 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->end);
1880 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->step);
1886 /* Like gfc_apply_interface_mapping_to_expr, but applied to
1890 gfc_apply_interface_mapping_to_ref (gfc_interface_mapping * mapping,
1895 for (; ref; ref = ref->next)
1899 for (n = 0; n < ref->u.ar.dimen; n++)
1901 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.start[n]);
1902 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.end[n]);
1903 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.stride[n]);
1905 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.offset);
1912 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.start);
1913 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.end);
1919 /* Convert intrinsic function calls into result expressions. */
1922 gfc_map_intrinsic_function (gfc_expr *expr, gfc_interface_mapping *mapping)
1930 arg1 = expr->value.function.actual->expr;
1931 if (expr->value.function.actual->next)
1932 arg2 = expr->value.function.actual->next->expr;
1936 sym = arg1->symtree->n.sym;
1938 if (sym->attr.dummy)
1943 switch (expr->value.function.isym->id)
1946 /* TODO figure out why this condition is necessary. */
1947 if (sym->attr.function
1948 && (arg1->ts.u.cl->length == NULL
1949 || (arg1->ts.u.cl->length->expr_type != EXPR_CONSTANT
1950 && arg1->ts.u.cl->length->expr_type != EXPR_VARIABLE)))
1953 new_expr = gfc_copy_expr (arg1->ts.u.cl->length);
1957 if (!sym->as || sym->as->rank == 0)
1960 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
1962 dup = mpz_get_si (arg2->value.integer);
1967 dup = sym->as->rank;
1971 for (; d < dup; d++)
1975 if (!sym->as->upper[d] || !sym->as->lower[d])
1977 gfc_free_expr (new_expr);
1981 tmp = gfc_add (gfc_copy_expr (sym->as->upper[d]),
1982 gfc_get_int_expr (gfc_default_integer_kind,
1984 tmp = gfc_subtract (tmp, gfc_copy_expr (sym->as->lower[d]));
1986 new_expr = gfc_multiply (new_expr, tmp);
1992 case GFC_ISYM_LBOUND:
1993 case GFC_ISYM_UBOUND:
1994 /* TODO These implementations of lbound and ubound do not limit if
1995 the size < 0, according to F95's 13.14.53 and 13.14.113. */
1997 if (!sym->as || sym->as->rank == 0)
2000 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
2001 d = mpz_get_si (arg2->value.integer) - 1;
2003 /* TODO: If the need arises, this could produce an array of
2007 if (expr->value.function.isym->id == GFC_ISYM_LBOUND)
2009 if (sym->as->lower[d])
2010 new_expr = gfc_copy_expr (sym->as->lower[d]);
2014 if (sym->as->upper[d])
2015 new_expr = gfc_copy_expr (sym->as->upper[d]);
2023 gfc_apply_interface_mapping_to_expr (mapping, new_expr);
2027 gfc_replace_expr (expr, new_expr);
2033 gfc_map_fcn_formal_to_actual (gfc_expr *expr, gfc_expr *map_expr,
2034 gfc_interface_mapping * mapping)
2036 gfc_formal_arglist *f;
2037 gfc_actual_arglist *actual;
2039 actual = expr->value.function.actual;
2040 f = map_expr->symtree->n.sym->formal;
2042 for (; f && actual; f = f->next, actual = actual->next)
2047 gfc_add_interface_mapping (mapping, f->sym, NULL, actual->expr);
2050 if (map_expr->symtree->n.sym->attr.dimension)
2055 as = gfc_copy_array_spec (map_expr->symtree->n.sym->as);
2057 for (d = 0; d < as->rank; d++)
2059 gfc_apply_interface_mapping_to_expr (mapping, as->lower[d]);
2060 gfc_apply_interface_mapping_to_expr (mapping, as->upper[d]);
2063 expr->value.function.esym->as = as;
2066 if (map_expr->symtree->n.sym->ts.type == BT_CHARACTER)
2068 expr->value.function.esym->ts.u.cl->length
2069 = gfc_copy_expr (map_expr->symtree->n.sym->ts.u.cl->length);
2071 gfc_apply_interface_mapping_to_expr (mapping,
2072 expr->value.function.esym->ts.u.cl->length);
2077 /* EXPR is a copy of an expression that appeared in the interface
2078 associated with MAPPING. Walk it recursively looking for references to
2079 dummy arguments that MAPPING maps to actual arguments. Replace each such
2080 reference with a reference to the associated actual argument. */
2083 gfc_apply_interface_mapping_to_expr (gfc_interface_mapping * mapping,
2086 gfc_interface_sym_mapping *sym;
2087 gfc_actual_arglist *actual;
2092 /* Copying an expression does not copy its length, so do that here. */
2093 if (expr->ts.type == BT_CHARACTER && expr->ts.u.cl)
2095 expr->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, expr->ts.u.cl);
2096 gfc_apply_interface_mapping_to_expr (mapping, expr->ts.u.cl->length);
2099 /* Apply the mapping to any references. */
2100 gfc_apply_interface_mapping_to_ref (mapping, expr->ref);
2102 /* ...and to the expression's symbol, if it has one. */
2103 /* TODO Find out why the condition on expr->symtree had to be moved into
2104 the loop rather than being outside it, as originally. */
2105 for (sym = mapping->syms; sym; sym = sym->next)
2106 if (expr->symtree && sym->old == expr->symtree->n.sym)
2108 if (sym->new_sym->n.sym->backend_decl)
2109 expr->symtree = sym->new_sym;
2111 gfc_replace_expr (expr, gfc_copy_expr (sym->expr));
2114 /* ...and to subexpressions in expr->value. */
2115 switch (expr->expr_type)
2120 case EXPR_SUBSTRING:
2124 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op1);
2125 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op2);
2129 for (actual = expr->value.function.actual; actual; actual = actual->next)
2130 gfc_apply_interface_mapping_to_expr (mapping, actual->expr);
2132 if (expr->value.function.esym == NULL
2133 && expr->value.function.isym != NULL
2134 && expr->value.function.actual->expr->symtree
2135 && gfc_map_intrinsic_function (expr, mapping))
2138 for (sym = mapping->syms; sym; sym = sym->next)
2139 if (sym->old == expr->value.function.esym)
2141 expr->value.function.esym = sym->new_sym->n.sym;
2142 gfc_map_fcn_formal_to_actual (expr, sym->expr, mapping);
2143 expr->value.function.esym->result = sym->new_sym->n.sym;
2148 case EXPR_STRUCTURE:
2149 gfc_apply_interface_mapping_to_cons (mapping, expr->value.constructor);
2162 /* Evaluate interface expression EXPR using MAPPING. Store the result
2166 gfc_apply_interface_mapping (gfc_interface_mapping * mapping,
2167 gfc_se * se, gfc_expr * expr)
2169 expr = gfc_copy_expr (expr);
2170 gfc_apply_interface_mapping_to_expr (mapping, expr);
2171 gfc_conv_expr (se, expr);
2172 se->expr = gfc_evaluate_now (se->expr, &se->pre);
2173 gfc_free_expr (expr);
2177 /* Returns a reference to a temporary array into which a component of
2178 an actual argument derived type array is copied and then returned
2179 after the function call. */
2181 gfc_conv_subref_array_arg (gfc_se * parmse, gfc_expr * expr, int g77,
2182 sym_intent intent, bool formal_ptr)
2200 gcc_assert (expr->expr_type == EXPR_VARIABLE);
2202 gfc_init_se (&lse, NULL);
2203 gfc_init_se (&rse, NULL);
2205 /* Walk the argument expression. */
2206 rss = gfc_walk_expr (expr);
2208 gcc_assert (rss != gfc_ss_terminator);
2210 /* Initialize the scalarizer. */
2211 gfc_init_loopinfo (&loop);
2212 gfc_add_ss_to_loop (&loop, rss);
2214 /* Calculate the bounds of the scalarization. */
2215 gfc_conv_ss_startstride (&loop);
2217 /* Build an ss for the temporary. */
2218 if (expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->backend_decl)
2219 gfc_conv_string_length (expr->ts.u.cl, expr, &parmse->pre);
2221 base_type = gfc_typenode_for_spec (&expr->ts);
2222 if (GFC_ARRAY_TYPE_P (base_type)
2223 || GFC_DESCRIPTOR_TYPE_P (base_type))
2224 base_type = gfc_get_element_type (base_type);
2226 loop.temp_ss = gfc_get_ss ();;
2227 loop.temp_ss->type = GFC_SS_TEMP;
2228 loop.temp_ss->data.temp.type = base_type;
2230 if (expr->ts.type == BT_CHARACTER)
2231 loop.temp_ss->string_length = expr->ts.u.cl->backend_decl;
2233 loop.temp_ss->string_length = NULL;
2235 parmse->string_length = loop.temp_ss->string_length;
2236 loop.temp_ss->data.temp.dimen = loop.dimen;
2237 loop.temp_ss->next = gfc_ss_terminator;
2239 /* Associate the SS with the loop. */
2240 gfc_add_ss_to_loop (&loop, loop.temp_ss);
2242 /* Setup the scalarizing loops. */
2243 gfc_conv_loop_setup (&loop, &expr->where);
2245 /* Pass the temporary descriptor back to the caller. */
2246 info = &loop.temp_ss->data.info;
2247 parmse->expr = info->descriptor;
2249 /* Setup the gfc_se structures. */
2250 gfc_copy_loopinfo_to_se (&lse, &loop);
2251 gfc_copy_loopinfo_to_se (&rse, &loop);
2254 lse.ss = loop.temp_ss;
2255 gfc_mark_ss_chain_used (rss, 1);
2256 gfc_mark_ss_chain_used (loop.temp_ss, 1);
2258 /* Start the scalarized loop body. */
2259 gfc_start_scalarized_body (&loop, &body);
2261 /* Translate the expression. */
2262 gfc_conv_expr (&rse, expr);
2264 gfc_conv_tmp_array_ref (&lse);
2265 gfc_advance_se_ss_chain (&lse);
2267 if (intent != INTENT_OUT)
2269 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, true, false, true);
2270 gfc_add_expr_to_block (&body, tmp);
2271 gcc_assert (rse.ss == gfc_ss_terminator);
2272 gfc_trans_scalarizing_loops (&loop, &body);
2276 /* Make sure that the temporary declaration survives by merging
2277 all the loop declarations into the current context. */
2278 for (n = 0; n < loop.dimen; n++)
2280 gfc_merge_block_scope (&body);
2281 body = loop.code[loop.order[n]];
2283 gfc_merge_block_scope (&body);
2286 /* Add the post block after the second loop, so that any
2287 freeing of allocated memory is done at the right time. */
2288 gfc_add_block_to_block (&parmse->pre, &loop.pre);
2290 /**********Copy the temporary back again.*********/
2292 gfc_init_se (&lse, NULL);
2293 gfc_init_se (&rse, NULL);
2295 /* Walk the argument expression. */
2296 lss = gfc_walk_expr (expr);
2297 rse.ss = loop.temp_ss;
2300 /* Initialize the scalarizer. */
2301 gfc_init_loopinfo (&loop2);
2302 gfc_add_ss_to_loop (&loop2, lss);
2304 /* Calculate the bounds of the scalarization. */
2305 gfc_conv_ss_startstride (&loop2);
2307 /* Setup the scalarizing loops. */
2308 gfc_conv_loop_setup (&loop2, &expr->where);
2310 gfc_copy_loopinfo_to_se (&lse, &loop2);
2311 gfc_copy_loopinfo_to_se (&rse, &loop2);
2313 gfc_mark_ss_chain_used (lss, 1);
2314 gfc_mark_ss_chain_used (loop.temp_ss, 1);
2316 /* Declare the variable to hold the temporary offset and start the
2317 scalarized loop body. */
2318 offset = gfc_create_var (gfc_array_index_type, NULL);
2319 gfc_start_scalarized_body (&loop2, &body);
2321 /* Build the offsets for the temporary from the loop variables. The
2322 temporary array has lbounds of zero and strides of one in all
2323 dimensions, so this is very simple. The offset is only computed
2324 outside the innermost loop, so the overall transfer could be
2325 optimized further. */
2326 info = &rse.ss->data.info;
2327 dimen = info->dimen;
2329 tmp_index = gfc_index_zero_node;
2330 for (n = dimen - 1; n > 0; n--)
2333 tmp = rse.loop->loopvar[n];
2334 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
2335 tmp, rse.loop->from[n]);
2336 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
2339 tmp_str = fold_build2 (MINUS_EXPR, gfc_array_index_type,
2340 rse.loop->to[n-1], rse.loop->from[n-1]);
2341 tmp_str = fold_build2 (PLUS_EXPR, gfc_array_index_type,
2342 tmp_str, gfc_index_one_node);
2344 tmp_index = fold_build2 (MULT_EXPR, gfc_array_index_type,
2348 tmp_index = fold_build2 (MINUS_EXPR, gfc_array_index_type,
2349 tmp_index, rse.loop->from[0]);
2350 gfc_add_modify (&rse.loop->code[0], offset, tmp_index);
2352 tmp_index = fold_build2 (PLUS_EXPR, gfc_array_index_type,
2353 rse.loop->loopvar[0], offset);
2355 /* Now use the offset for the reference. */
2356 tmp = build_fold_indirect_ref_loc (input_location,
2358 rse.expr = gfc_build_array_ref (tmp, tmp_index, NULL);
2360 if (expr->ts.type == BT_CHARACTER)
2361 rse.string_length = expr->ts.u.cl->backend_decl;
2363 gfc_conv_expr (&lse, expr);
2365 gcc_assert (lse.ss == gfc_ss_terminator);
2367 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, false, true);
2368 gfc_add_expr_to_block (&body, tmp);
2370 /* Generate the copying loops. */
2371 gfc_trans_scalarizing_loops (&loop2, &body);
2373 /* Wrap the whole thing up by adding the second loop to the post-block
2374 and following it by the post-block of the first loop. In this way,
2375 if the temporary needs freeing, it is done after use! */
2376 if (intent != INTENT_IN)
2378 gfc_add_block_to_block (&parmse->post, &loop2.pre);
2379 gfc_add_block_to_block (&parmse->post, &loop2.post);
2382 gfc_add_block_to_block (&parmse->post, &loop.post);
2384 gfc_cleanup_loop (&loop);
2385 gfc_cleanup_loop (&loop2);
2387 /* Pass the string length to the argument expression. */
2388 if (expr->ts.type == BT_CHARACTER)
2389 parmse->string_length = expr->ts.u.cl->backend_decl;
2391 /* Determine the offset for pointer formal arguments and set the
2395 size = gfc_index_one_node;
2396 offset = gfc_index_zero_node;
2397 for (n = 0; n < dimen; n++)
2399 tmp = gfc_conv_descriptor_ubound_get (parmse->expr,
2401 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
2402 tmp, gfc_index_one_node);
2403 gfc_conv_descriptor_ubound_set (&parmse->pre,
2407 gfc_conv_descriptor_lbound_set (&parmse->pre,
2410 gfc_index_one_node);
2411 size = gfc_evaluate_now (size, &parmse->pre);
2412 offset = fold_build2 (MINUS_EXPR, gfc_array_index_type,
2414 offset = gfc_evaluate_now (offset, &parmse->pre);
2415 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
2416 rse.loop->to[n], rse.loop->from[n]);
2417 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
2418 tmp, gfc_index_one_node);
2419 size = fold_build2 (MULT_EXPR, gfc_array_index_type,
2423 gfc_conv_descriptor_offset_set (&parmse->pre, parmse->expr,
2427 /* We want either the address for the data or the address of the descriptor,
2428 depending on the mode of passing array arguments. */
2430 parmse->expr = gfc_conv_descriptor_data_get (parmse->expr);
2432 parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
2438 /* Generate the code for argument list functions. */
2441 conv_arglist_function (gfc_se *se, gfc_expr *expr, const char *name)
2443 /* Pass by value for g77 %VAL(arg), pass the address
2444 indirectly for %LOC, else by reference. Thus %REF
2445 is a "do-nothing" and %LOC is the same as an F95
2447 if (strncmp (name, "%VAL", 4) == 0)
2448 gfc_conv_expr (se, expr);
2449 else if (strncmp (name, "%LOC", 4) == 0)
2451 gfc_conv_expr_reference (se, expr);
2452 se->expr = gfc_build_addr_expr (NULL, se->expr);
2454 else if (strncmp (name, "%REF", 4) == 0)
2455 gfc_conv_expr_reference (se, expr);
2457 gfc_error ("Unknown argument list function at %L", &expr->where);
2461 /* Takes a derived type expression and returns the address of a temporary
2462 class object of the 'declared' type. */
2464 gfc_conv_derived_to_class (gfc_se *parmse, gfc_expr *e,
2465 gfc_typespec class_ts)
2469 gfc_symbol *declared = class_ts.u.derived;
2475 /* The derived type needs to be converted to a temporary
2477 tmp = gfc_typenode_for_spec (&class_ts);
2478 var = gfc_create_var (tmp, "class");
2481 cmp = gfc_find_component (declared, "$vptr", true, true);
2482 ctree = fold_build3 (COMPONENT_REF, TREE_TYPE (cmp->backend_decl),
2483 var, cmp->backend_decl, NULL_TREE);
2485 /* Remember the vtab corresponds to the derived type
2486 not to the class declared type. */
2487 vtab = gfc_find_derived_vtab (e->ts.u.derived, true);
2489 gfc_trans_assign_vtab_procs (&parmse->pre, e->ts.u.derived, vtab);
2490 tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
2491 gfc_add_modify (&parmse->pre, ctree,
2492 fold_convert (TREE_TYPE (ctree), tmp));
2494 /* Now set the data field. */
2495 cmp = gfc_find_component (declared, "$data", true, true);
2496 ctree = fold_build3 (COMPONENT_REF, TREE_TYPE (cmp->backend_decl),
2497 var, cmp->backend_decl, NULL_TREE);
2498 ss = gfc_walk_expr (e);
2499 if (ss == gfc_ss_terminator)
2501 gfc_conv_expr_reference (parmse, e);
2502 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
2503 gfc_add_modify (&parmse->pre, ctree, tmp);
2507 gfc_conv_expr (parmse, e);
2508 gfc_add_modify (&parmse->pre, ctree, parmse->expr);
2511 /* Pass the address of the class object. */
2512 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
2516 /* The following routine generates code for the intrinsic
2517 procedures from the ISO_C_BINDING module:
2519 * C_FUNLOC (function)
2520 * C_F_POINTER (subroutine)
2521 * C_F_PROCPOINTER (subroutine)
2522 * C_ASSOCIATED (function)
2523 One exception which is not handled here is C_F_POINTER with non-scalar
2524 arguments. Returns 1 if the call was replaced by inline code (else: 0). */
2527 conv_isocbinding_procedure (gfc_se * se, gfc_symbol * sym,
2528 gfc_actual_arglist * arg)
2533 if (sym->intmod_sym_id == ISOCBINDING_LOC)
2535 if (arg->expr->rank == 0)
2536 gfc_conv_expr_reference (se, arg->expr);
2540 /* This is really the actual arg because no formal arglist is
2541 created for C_LOC. */
2542 fsym = arg->expr->symtree->n.sym;
2544 /* We should want it to do g77 calling convention. */
2546 && !(fsym->attr.pointer || fsym->attr.allocatable)
2547 && fsym->as->type != AS_ASSUMED_SHAPE;
2548 f = f || !sym->attr.always_explicit;
2550 argss = gfc_walk_expr (arg->expr);
2551 gfc_conv_array_parameter (se, arg->expr, argss, f,
2555 /* TODO -- the following two lines shouldn't be necessary, but if
2556 they're removed, a bug is exposed later in the code path.
2557 This workaround was thus introduced, but will have to be
2558 removed; please see PR 35150 for details about the issue. */
2559 se->expr = convert (pvoid_type_node, se->expr);
2560 se->expr = gfc_evaluate_now (se->expr, &se->pre);
2564 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2566 arg->expr->ts.type = sym->ts.u.derived->ts.type;
2567 arg->expr->ts.f90_type = sym->ts.u.derived->ts.f90_type;
2568 arg->expr->ts.kind = sym->ts.u.derived->ts.kind;
2569 gfc_conv_expr_reference (se, arg->expr);
2573 else if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER
2574 && arg->next->expr->rank == 0)
2575 || sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER)
2577 /* Convert c_f_pointer if fptr is a scalar
2578 and convert c_f_procpointer. */
2582 gfc_init_se (&cptrse, NULL);
2583 gfc_conv_expr (&cptrse, arg->expr);
2584 gfc_add_block_to_block (&se->pre, &cptrse.pre);
2585 gfc_add_block_to_block (&se->post, &cptrse.post);
2587 gfc_init_se (&fptrse, NULL);
2588 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER
2589 || gfc_is_proc_ptr_comp (arg->next->expr, NULL))
2590 fptrse.want_pointer = 1;
2592 gfc_conv_expr (&fptrse, arg->next->expr);
2593 gfc_add_block_to_block (&se->pre, &fptrse.pre);
2594 gfc_add_block_to_block (&se->post, &fptrse.post);
2596 if (arg->next->expr->symtree->n.sym->attr.proc_pointer
2597 && arg->next->expr->symtree->n.sym->attr.dummy)
2598 fptrse.expr = build_fold_indirect_ref_loc (input_location,
2601 se->expr = fold_build2 (MODIFY_EXPR, TREE_TYPE (fptrse.expr),
2603 fold_convert (TREE_TYPE (fptrse.expr),
2608 else if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2613 /* Build the addr_expr for the first argument. The argument is
2614 already an *address* so we don't need to set want_pointer in
2616 gfc_init_se (&arg1se, NULL);
2617 gfc_conv_expr (&arg1se, arg->expr);
2618 gfc_add_block_to_block (&se->pre, &arg1se.pre);
2619 gfc_add_block_to_block (&se->post, &arg1se.post);
2621 /* See if we were given two arguments. */
2622 if (arg->next == NULL)
2623 /* Only given one arg so generate a null and do a
2624 not-equal comparison against the first arg. */
2625 se->expr = fold_build2 (NE_EXPR, boolean_type_node, arg1se.expr,
2626 fold_convert (TREE_TYPE (arg1se.expr),
2627 null_pointer_node));
2633 /* Given two arguments so build the arg2se from second arg. */
2634 gfc_init_se (&arg2se, NULL);
2635 gfc_conv_expr (&arg2se, arg->next->expr);
2636 gfc_add_block_to_block (&se->pre, &arg2se.pre);
2637 gfc_add_block_to_block (&se->post, &arg2se.post);
2639 /* Generate test to compare that the two args are equal. */
2640 eq_expr = fold_build2 (EQ_EXPR, boolean_type_node,
2641 arg1se.expr, arg2se.expr);
2642 /* Generate test to ensure that the first arg is not null. */
2643 not_null_expr = fold_build2 (NE_EXPR, boolean_type_node,
2644 arg1se.expr, null_pointer_node);
2646 /* Finally, the generated test must check that both arg1 is not
2647 NULL and that it is equal to the second arg. */
2648 se->expr = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
2649 not_null_expr, eq_expr);
2655 /* Nothing was done. */
2660 /* Generate code for a procedure call. Note can return se->post != NULL.
2661 If se->direct_byref is set then se->expr contains the return parameter.
2662 Return nonzero, if the call has alternate specifiers.
2663 'expr' is only needed for procedure pointer components. */
2666 gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
2667 gfc_actual_arglist * arg, gfc_expr * expr,
2670 gfc_interface_mapping mapping;
2685 gfc_formal_arglist *formal;
2686 int has_alternate_specifier = 0;
2687 bool need_interface_mapping;
2694 enum {MISSING = 0, ELEMENTAL, SCALAR, SCALAR_POINTER, ARRAY};
2695 gfc_component *comp = NULL;
2697 arglist = NULL_TREE;
2698 retargs = NULL_TREE;
2699 stringargs = NULL_TREE;
2704 if (sym->from_intmod == INTMOD_ISO_C_BINDING
2705 && conv_isocbinding_procedure (se, sym, arg))
2708 gfc_is_proc_ptr_comp (expr, &comp);
2712 if (!sym->attr.elemental)
2714 gcc_assert (se->ss->type == GFC_SS_FUNCTION);
2715 if (se->ss->useflags)
2717 gcc_assert ((!comp && gfc_return_by_reference (sym)
2718 && sym->result->attr.dimension)
2719 || (comp && comp->attr.dimension));
2720 gcc_assert (se->loop != NULL);
2722 /* Access the previously obtained result. */
2723 gfc_conv_tmp_array_ref (se);
2724 gfc_advance_se_ss_chain (se);
2728 info = &se->ss->data.info;
2733 gfc_init_block (&post);
2734 gfc_init_interface_mapping (&mapping);
2737 formal = sym->formal;
2738 need_interface_mapping = sym->attr.dimension ||
2739 (sym->ts.type == BT_CHARACTER
2740 && sym->ts.u.cl->length
2741 && sym->ts.u.cl->length->expr_type
2746 formal = comp->formal;
2747 need_interface_mapping = comp->attr.dimension ||
2748 (comp->ts.type == BT_CHARACTER
2749 && comp->ts.u.cl->length
2750 && comp->ts.u.cl->length->expr_type
2754 /* Evaluate the arguments. */
2755 for (; arg != NULL; arg = arg->next, formal = formal ? formal->next : NULL)
2758 fsym = formal ? formal->sym : NULL;
2759 parm_kind = MISSING;
2763 if (se->ignore_optional)
2765 /* Some intrinsics have already been resolved to the correct
2769 else if (arg->label)
2771 has_alternate_specifier = 1;
2776 /* Pass a NULL pointer for an absent arg. */
2777 gfc_init_se (&parmse, NULL);
2778 parmse.expr = null_pointer_node;
2779 if (arg->missing_arg_type == BT_CHARACTER)
2780 parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
2783 else if (fsym && fsym->ts.type == BT_CLASS
2784 && e->ts.type == BT_DERIVED)
2786 /* The derived type needs to be converted to a temporary
2788 gfc_init_se (&parmse, se);
2789 gfc_conv_derived_to_class (&parmse, e, fsym->ts);
2791 else if (se->ss && se->ss->useflags)
2793 /* An elemental function inside a scalarized loop. */
2794 gfc_init_se (&parmse, se);
2795 gfc_conv_expr_reference (&parmse, e);
2796 parm_kind = ELEMENTAL;
2800 /* A scalar or transformational function. */
2801 gfc_init_se (&parmse, NULL);
2802 argss = gfc_walk_expr (e);
2804 if (argss == gfc_ss_terminator)
2806 if (e->expr_type == EXPR_VARIABLE
2807 && e->symtree->n.sym->attr.cray_pointee
2808 && fsym && fsym->attr.flavor == FL_PROCEDURE)
2810 /* The Cray pointer needs to be converted to a pointer to
2811 a type given by the expression. */
2812 gfc_conv_expr (&parmse, e);
2813 type = build_pointer_type (TREE_TYPE (parmse.expr));
2814 tmp = gfc_get_symbol_decl (e->symtree->n.sym->cp_pointer);
2815 parmse.expr = convert (type, tmp);
2817 else if (fsym && fsym->attr.value)
2819 if (fsym->ts.type == BT_CHARACTER
2820 && fsym->ts.is_c_interop
2821 && fsym->ns->proc_name != NULL
2822 && fsym->ns->proc_name->attr.is_bind_c)
2825 gfc_conv_scalar_char_value (fsym, &parmse, &e);
2826 if (parmse.expr == NULL)
2827 gfc_conv_expr (&parmse, e);
2830 gfc_conv_expr (&parmse, e);
2832 else if (arg->name && arg->name[0] == '%')
2833 /* Argument list functions %VAL, %LOC and %REF are signalled
2834 through arg->name. */
2835 conv_arglist_function (&parmse, arg->expr, arg->name);
2836 else if ((e->expr_type == EXPR_FUNCTION)
2837 && ((e->value.function.esym
2838 && e->value.function.esym->result->attr.pointer)
2839 || (!e->value.function.esym
2840 && e->symtree->n.sym->attr.pointer))
2841 && fsym && fsym->attr.target)
2843 gfc_conv_expr (&parmse, e);
2844 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
2846 else if (e->expr_type == EXPR_FUNCTION
2847 && e->symtree->n.sym->result
2848 && e->symtree->n.sym->result != e->symtree->n.sym
2849 && e->symtree->n.sym->result->attr.proc_pointer)
2851 /* Functions returning procedure pointers. */
2852 gfc_conv_expr (&parmse, e);
2853 if (fsym && fsym->attr.proc_pointer)
2854 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
2858 gfc_conv_expr_reference (&parmse, e);
2860 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
2861 allocated on entry, it must be deallocated. */
2862 if (fsym && fsym->attr.allocatable
2863 && fsym->attr.intent == INTENT_OUT)
2867 gfc_init_block (&block);
2868 tmp = gfc_deallocate_with_status (parmse.expr, NULL_TREE,
2870 gfc_add_expr_to_block (&block, tmp);
2871 tmp = fold_build2 (MODIFY_EXPR, void_type_node,
2872 parmse.expr, null_pointer_node);
2873 gfc_add_expr_to_block (&block, tmp);
2875 if (fsym->attr.optional
2876 && e->expr_type == EXPR_VARIABLE
2877 && e->symtree->n.sym->attr.optional)
2879 tmp = fold_build3 (COND_EXPR, void_type_node,
2880 gfc_conv_expr_present (e->symtree->n.sym),
2881 gfc_finish_block (&block),
2882 build_empty_stmt (input_location));
2885 tmp = gfc_finish_block (&block);
2887 gfc_add_expr_to_block (&se->pre, tmp);
2890 if (fsym && e->expr_type != EXPR_NULL
2891 && ((fsym->attr.pointer
2892 && fsym->attr.flavor != FL_PROCEDURE)
2893 || (fsym->attr.proc_pointer
2894 && !(e->expr_type == EXPR_VARIABLE
2895 && e->symtree->n.sym->attr.dummy))
2896 || (e->expr_type == EXPR_VARIABLE
2897 && gfc_is_proc_ptr_comp (e, NULL))
2898 || fsym->attr.allocatable))
2900 /* Scalar pointer dummy args require an extra level of
2901 indirection. The null pointer already contains
2902 this level of indirection. */
2903 parm_kind = SCALAR_POINTER;
2904 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
2910 /* If the procedure requires an explicit interface, the actual
2911 argument is passed according to the corresponding formal
2912 argument. If the corresponding formal argument is a POINTER,
2913 ALLOCATABLE or assumed shape, we do not use g77's calling
2914 convention, and pass the address of the array descriptor
2915 instead. Otherwise we use g77's calling convention. */
2918 && !(fsym->attr.pointer || fsym->attr.allocatable)
2919 && fsym->as->type != AS_ASSUMED_SHAPE;
2921 f = f || !comp->attr.always_explicit;
2923 f = f || !sym->attr.always_explicit;
2925 if (e->expr_type == EXPR_VARIABLE
2926 && is_subref_array (e))
2927 /* The actual argument is a component reference to an
2928 array of derived types. In this case, the argument
2929 is converted to a temporary, which is passed and then
2930 written back after the procedure call. */
2931 gfc_conv_subref_array_arg (&parmse, e, f,
2932 fsym ? fsym->attr.intent : INTENT_INOUT,
2933 fsym && fsym->attr.pointer);
2935 gfc_conv_array_parameter (&parmse, e, argss, f, fsym,
2938 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
2939 allocated on entry, it must be deallocated. */
2940 if (fsym && fsym->attr.allocatable
2941 && fsym->attr.intent == INTENT_OUT)
2943 tmp = build_fold_indirect_ref_loc (input_location,
2945 tmp = gfc_trans_dealloc_allocated (tmp);
2946 if (fsym->attr.optional
2947 && e->expr_type == EXPR_VARIABLE
2948 && e->symtree->n.sym->attr.optional)
2949 tmp = fold_build3 (COND_EXPR, void_type_node,
2950 gfc_conv_expr_present (e->symtree->n.sym),
2951 tmp, build_empty_stmt (input_location));
2952 gfc_add_expr_to_block (&se->pre, tmp);
2957 /* The case with fsym->attr.optional is that of a user subroutine
2958 with an interface indicating an optional argument. When we call
2959 an intrinsic subroutine, however, fsym is NULL, but we might still
2960 have an optional argument, so we proceed to the substitution
2962 if (e && (fsym == NULL || fsym->attr.optional))
2964 /* If an optional argument is itself an optional dummy argument,
2965 check its presence and substitute a null if absent. This is
2966 only needed when passing an array to an elemental procedure
2967 as then array elements are accessed - or no NULL pointer is
2968 allowed and a "1" or "0" should be passed if not present.
2969 When passing a non-array-descriptor full array to a
2970 non-array-descriptor dummy, no check is needed. For
2971 array-descriptor actual to array-descriptor dummy, see
2972 PR 41911 for why a check has to be inserted.
2973 fsym == NULL is checked as intrinsics required the descriptor
2974 but do not always set fsym. */
2975 if (e->expr_type == EXPR_VARIABLE
2976 && e->symtree->n.sym->attr.optional
2977 && ((e->rank > 0 && sym->attr.elemental)
2978 || e->representation.length || e->ts.type == BT_CHARACTER
2980 && (fsym == NULL || fsym->as->type == AS_ASSUMED_SHAPE
2981 || fsym->as->type == AS_DEFERRED))))
2982 gfc_conv_missing_dummy (&parmse, e, fsym ? fsym->ts : e->ts,
2983 e->representation.length);
2988 /* Obtain the character length of an assumed character length
2989 length procedure from the typespec. */
2990 if (fsym->ts.type == BT_CHARACTER
2991 && parmse.string_length == NULL_TREE
2992 && e->ts.type == BT_PROCEDURE
2993 && e->symtree->n.sym->ts.type == BT_CHARACTER
2994 && e->symtree->n.sym->ts.u.cl->length != NULL
2995 && e->symtree->n.sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
2997 gfc_conv_const_charlen (e->symtree->n.sym->ts.u.cl);
2998 parmse.string_length = e->symtree->n.sym->ts.u.cl->backend_decl;
3002 if (fsym && need_interface_mapping && e)
3003 gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
3005 gfc_add_block_to_block (&se->pre, &parmse.pre);
3006 gfc_add_block_to_block (&post, &parmse.post);
3008 /* Allocated allocatable components of derived types must be
3009 deallocated for non-variable scalars. Non-variable arrays are
3010 dealt with in trans-array.c(gfc_conv_array_parameter). */
3011 if (e && e->ts.type == BT_DERIVED
3012 && e->ts.u.derived->attr.alloc_comp
3013 && !(e->symtree && e->symtree->n.sym->attr.pointer)
3014 && (e->expr_type != EXPR_VARIABLE && !e->rank))
3017 tmp = build_fold_indirect_ref_loc (input_location,
3019 parm_rank = e->rank;
3027 case (SCALAR_POINTER):
3028 tmp = build_fold_indirect_ref_loc (input_location,
3033 if (e->expr_type == EXPR_OP
3034 && e->value.op.op == INTRINSIC_PARENTHESES
3035 && e->value.op.op1->expr_type == EXPR_VARIABLE)
3038 local_tmp = gfc_evaluate_now (tmp, &se->pre);
3039 local_tmp = gfc_copy_alloc_comp (e->ts.u.derived, local_tmp, tmp, parm_rank);
3040 gfc_add_expr_to_block (&se->post, local_tmp);
3043 tmp = gfc_deallocate_alloc_comp (e->ts.u.derived, tmp, parm_rank);
3045 gfc_add_expr_to_block (&se->post, tmp);
3048 /* Add argument checking of passing an unallocated/NULL actual to
3049 a nonallocatable/nonpointer dummy. */
3051 if (gfc_option.rtcheck & GFC_RTCHECK_POINTER && e != NULL)
3053 symbol_attribute *attr;
3057 if (e->expr_type == EXPR_VARIABLE)
3058 attr = &e->symtree->n.sym->attr;
3059 else if (e->expr_type == EXPR_FUNCTION)
3061 /* For intrinsic functions, the gfc_attr are not available. */
3062 if (e->symtree->n.sym->attr.generic && e->value.function.isym)
3063 goto end_pointer_check;
3065 if (e->symtree->n.sym->attr.generic)
3066 attr = &e->value.function.esym->attr;
3068 attr = &e->symtree->n.sym->result->attr;
3071 goto end_pointer_check;
3075 /* If the actual argument is an optional pointer/allocatable and
3076 the formal argument takes an nonpointer optional value,
3077 it is invalid to pass a non-present argument on, even
3078 though there is no technical reason for this in gfortran.
3079 See Fortran 2003, Section 12.4.1.6 item (7)+(8). */
3080 tree present, null_ptr, type;
3082 if (attr->allocatable
3083 && (fsym == NULL || !fsym->attr.allocatable))
3084 asprintf (&msg, "Allocatable actual argument '%s' is not "
3085 "allocated or not present", e->symtree->n.sym->name);
3086 else if (attr->pointer
3087 && (fsym == NULL || !fsym->attr.pointer))
3088 asprintf (&msg, "Pointer actual argument '%s' is not "
3089 "associated or not present",
3090 e->symtree->n.sym->name);
3091 else if (attr->proc_pointer
3092 && (fsym == NULL || !fsym->attr.proc_pointer))
3093 asprintf (&msg, "Proc-pointer actual argument '%s' is not "
3094 "associated or not present",
3095 e->symtree->n.sym->name);
3097 goto end_pointer_check;
3099 present = gfc_conv_expr_present (e->symtree->n.sym);
3100 type = TREE_TYPE (present);
3101 present = fold_build2 (EQ_EXPR, boolean_type_node, present,
3102 fold_convert (type, null_pointer_node));
3103 type = TREE_TYPE (parmse.expr);
3104 null_ptr = fold_build2 (EQ_EXPR, boolean_type_node, parmse.expr,
3105 fold_convert (type, null_pointer_node));
3106 cond = fold_build2 (TRUTH_ORIF_EXPR, boolean_type_node,
3111 if (attr->allocatable
3112 && (fsym == NULL || !fsym->attr.allocatable))
3113 asprintf (&msg, "Allocatable actual argument '%s' is not "
3114 "allocated", e->symtree->n.sym->name);
3115 else if (attr->pointer
3116 && (fsym == NULL || !fsym->attr.pointer))
3117 asprintf (&msg, "Pointer actual argument '%s' is not "
3118 "associated", e->symtree->n.sym->name);
3119 else if (attr->proc_pointer
3120 && (fsym == NULL || !fsym->attr.proc_pointer))
3121 asprintf (&msg, "Proc-pointer actual argument '%s' is not "
3122 "associated", e->symtree->n.sym->name);
3124 goto end_pointer_check;
3127 cond = fold_build2 (EQ_EXPR, boolean_type_node, parmse.expr,
3128 fold_convert (TREE_TYPE (parmse.expr),
3129 null_pointer_node));
3132 gfc_trans_runtime_check (true, false, cond, &se->pre, &e->where,
3139 /* Character strings are passed as two parameters, a length and a
3140 pointer - except for Bind(c) which only passes the pointer. */
3141 if (parmse.string_length != NULL_TREE && !sym->attr.is_bind_c)
3142 stringargs = gfc_chainon_list (stringargs, parmse.string_length);
3144 arglist = gfc_chainon_list (arglist, parmse.expr);
3146 gfc_finish_interface_mapping (&mapping, &se->pre, &se->post);
3153 if (ts.type == BT_CHARACTER && sym->attr.is_bind_c)
3154 se->string_length = build_int_cst (gfc_charlen_type_node, 1);
3155 else if (ts.type == BT_CHARACTER)
3157 if (ts.u.cl->length == NULL)
3159 /* Assumed character length results are not allowed by 5.1.1.5 of the
3160 standard and are trapped in resolve.c; except in the case of SPREAD
3161 (and other intrinsics?) and dummy functions. In the case of SPREAD,
3162 we take the character length of the first argument for the result.
3163 For dummies, we have to look through the formal argument list for
3164 this function and use the character length found there.*/
3165 if (!sym->attr.dummy)
3166 cl.backend_decl = TREE_VALUE (stringargs);
3169 formal = sym->ns->proc_name->formal;
3170 for (; formal; formal = formal->next)
3171 if (strcmp (formal->sym->name, sym->name) == 0)
3172 cl.backend_decl = formal->sym->ts.u.cl->backend_decl;
3179 /* Calculate the length of the returned string. */
3180 gfc_init_se (&parmse, NULL);
3181 if (need_interface_mapping)
3182 gfc_apply_interface_mapping (&mapping, &parmse, ts.u.cl->length);
3184 gfc_conv_expr (&parmse, ts.u.cl->length);
3185 gfc_add_block_to_block (&se->pre, &parmse.pre);
3186 gfc_add_block_to_block (&se->post, &parmse.post);
3188 tmp = fold_convert (gfc_charlen_type_node, parmse.expr);
3189 tmp = fold_build2 (MAX_EXPR, gfc_charlen_type_node, tmp,
3190 build_int_cst (gfc_charlen_type_node, 0));
3191 cl.backend_decl = tmp;
3194 /* Set up a charlen structure for it. */
3199 len = cl.backend_decl;
3202 byref = (comp && (comp->attr.dimension || comp->ts.type == BT_CHARACTER))
3203 || (!comp && gfc_return_by_reference (sym));
3206 if (se->direct_byref)
3208 /* Sometimes, too much indirection can be applied; e.g. for
3209 function_result = array_valued_recursive_function. */
3210 if (TREE_TYPE (TREE_TYPE (se->expr))
3211 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))
3212 && GFC_DESCRIPTOR_TYPE_P
3213 (TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))))
3214 se->expr = build_fold_indirect_ref_loc (input_location,
3217 result = build_fold_indirect_ref_loc (input_location,
3219 retargs = gfc_chainon_list (retargs, se->expr);
3221 else if (comp && comp->attr.dimension)
3223 gcc_assert (se->loop && info);
3225 /* Set the type of the array. */
3226 tmp = gfc_typenode_for_spec (&comp->ts);
3227 info->dimen = se->loop->dimen;
3229 /* Evaluate the bounds of the result, if known. */
3230 gfc_set_loop_bounds_from_array_spec (&mapping, se, comp->as);
3232 /* Create a temporary to store the result. In case the function
3233 returns a pointer, the temporary will be a shallow copy and
3234 mustn't be deallocated. */
3235 callee_alloc = comp->attr.allocatable || comp->attr.pointer;
3236 gfc_trans_create_temp_array (&se->pre, &se->post, se->loop, info, tmp,
3237 NULL_TREE, false, !comp->attr.pointer,
3238 callee_alloc, &se->ss->expr->where);
3240 /* Pass the temporary as the first argument. */
3241 result = info->descriptor;
3242 tmp = gfc_build_addr_expr (NULL_TREE, result);
3243 retargs = gfc_chainon_list (retargs, tmp);
3245 else if (!comp && sym->result->attr.dimension)
3247 gcc_assert (se->loop && info);
3249 /* Set the type of the array. */
3250 tmp = gfc_typenode_for_spec (&ts);
3251 info->dimen = se->loop->dimen;
3253 /* Evaluate the bounds of the result, if known. */
3254 gfc_set_loop_bounds_from_array_spec (&mapping, se, sym->result->as);
3256 /* Create a temporary to store the result. In case the function
3257 returns a pointer, the temporary will be a shallow copy and
3258 mustn't be deallocated. */
3259 callee_alloc = sym->attr.allocatable || sym->attr.pointer;
3260 gfc_trans_create_temp_array (&se->pre, &se->post, se->loop, info, tmp,
3261 NULL_TREE, false, !sym->attr.pointer,
3262 callee_alloc, &se->ss->expr->where);
3264 /* Pass the temporary as the first argument. */
3265 result = info->descriptor;
3266 tmp = gfc_build_addr_expr (NULL_TREE, result);
3267 retargs = gfc_chainon_list (retargs, tmp);
3269 else if (ts.type == BT_CHARACTER)
3271 /* Pass the string length. */
3272 type = gfc_get_character_type (ts.kind, ts.u.cl);
3273 type = build_pointer_type (type);
3275 /* Return an address to a char[0:len-1]* temporary for
3276 character pointers. */
3277 if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
3278 || (comp && (comp->attr.pointer || comp->attr.allocatable)))
3280 var = gfc_create_var (type, "pstr");
3282 if ((!comp && sym->attr.allocatable)
3283 || (comp && comp->attr.allocatable))
3284 gfc_add_modify (&se->pre, var,
3285 fold_convert (TREE_TYPE (var),
3286 null_pointer_node));
3288 /* Provide an address expression for the function arguments. */
3289 var = gfc_build_addr_expr (NULL_TREE, var);
3292 var = gfc_conv_string_tmp (se, type, len);
3294 retargs = gfc_chainon_list (retargs, var);
3298 gcc_assert (gfc_option.flag_f2c && ts.type == BT_COMPLEX);
3300 type = gfc_get_complex_type (ts.kind);
3301 var = gfc_build_addr_expr (NULL_TREE, gfc_create_var (type, "cmplx"));
3302 retargs = gfc_chainon_list (retargs, var);
3305 /* Add the string length to the argument list. */
3306 if (ts.type == BT_CHARACTER)
3307 retargs = gfc_chainon_list (retargs, len);
3309 gfc_free_interface_mapping (&mapping);
3311 /* Add the return arguments. */
3312 arglist = chainon (retargs, arglist);
3314 /* Add the hidden string length parameters to the arguments. */
3315 arglist = chainon (arglist, stringargs);
3317 /* We may want to append extra arguments here. This is used e.g. for
3318 calls to libgfortran_matmul_??, which need extra information. */
3319 if (append_args != NULL_TREE)
3320 arglist = chainon (arglist, append_args);
3322 /* Generate the actual call. */
3323 conv_function_val (se, sym, expr);
3325 /* If there are alternate return labels, function type should be
3326 integer. Can't modify the type in place though, since it can be shared
3327 with other functions. For dummy arguments, the typing is done to
3328 to this result, even if it has to be repeated for each call. */
3329 if (has_alternate_specifier
3330 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) != integer_type_node)
3332 if (!sym->attr.dummy)
3334 TREE_TYPE (sym->backend_decl)
3335 = build_function_type (integer_type_node,
3336 TYPE_ARG_TYPES (TREE_TYPE (sym->backend_decl)));
3337 se->expr = gfc_build_addr_expr (NULL_TREE, sym->backend_decl);
3340 TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) = integer_type_node;
3343 fntype = TREE_TYPE (TREE_TYPE (se->expr));
3344 se->expr = build_call_list (TREE_TYPE (fntype), se->expr, arglist);
3346 /* If we have a pointer function, but we don't want a pointer, e.g.
3349 where f is pointer valued, we have to dereference the result. */
3350 if (!se->want_pointer && !byref
3351 && (sym->attr.pointer || sym->attr.allocatable)
3352 && !gfc_is_proc_ptr_comp (expr, NULL))
3353 se->expr = build_fold_indirect_ref_loc (input_location,
3356 /* f2c calling conventions require a scalar default real function to
3357 return a double precision result. Convert this back to default
3358 real. We only care about the cases that can happen in Fortran 77.
3360 if (gfc_option.flag_f2c && sym->ts.type == BT_REAL
3361 && sym->ts.kind == gfc_default_real_kind
3362 && !sym->attr.always_explicit)