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"
29 #include "diagnostic-core.h" /* For fatal_error. */
30 #include "langhooks.h"
34 #include "constructor.h"
36 #include "trans-const.h"
37 #include "trans-types.h"
38 #include "trans-array.h"
39 /* Only for gfc_trans_assign and gfc_trans_pointer_assign. */
40 #include "trans-stmt.h"
41 #include "dependency.h"
43 static tree gfc_trans_structure_assign (tree dest, gfc_expr * expr);
44 static void gfc_apply_interface_mapping_to_expr (gfc_interface_mapping *,
47 /* Copy the scalarization loop variables. */
50 gfc_copy_se_loopvars (gfc_se * dest, gfc_se * src)
53 dest->loop = src->loop;
57 /* Initialize a simple expression holder.
59 Care must be taken when multiple se are created with the same parent.
60 The child se must be kept in sync. The easiest way is to delay creation
61 of a child se until after after the previous se has been translated. */
64 gfc_init_se (gfc_se * se, gfc_se * parent)
66 memset (se, 0, sizeof (gfc_se));
67 gfc_init_block (&se->pre);
68 gfc_init_block (&se->post);
73 gfc_copy_se_loopvars (se, parent);
77 /* Advances to the next SS in the chain. Use this rather than setting
78 se->ss = se->ss->next because all the parents needs to be kept in sync.
82 gfc_advance_se_ss_chain (gfc_se * se)
86 gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
89 /* Walk down the parent chain. */
92 /* Simple consistency check. */
93 gcc_assert (p->parent == NULL || p->parent->ss == p->ss);
102 /* Ensures the result of the expression as either a temporary variable
103 or a constant so that it can be used repeatedly. */
106 gfc_make_safe_expr (gfc_se * se)
110 if (CONSTANT_CLASS_P (se->expr))
113 /* We need a temporary for this result. */
114 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
115 gfc_add_modify (&se->pre, var, se->expr);
120 /* Return an expression which determines if a dummy parameter is present.
121 Also used for arguments to procedures with multiple entry points. */
124 gfc_conv_expr_present (gfc_symbol * sym)
128 gcc_assert (sym->attr.dummy);
130 decl = gfc_get_symbol_decl (sym);
131 if (TREE_CODE (decl) != PARM_DECL)
133 /* Array parameters use a temporary descriptor, we want the real
135 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
136 || GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
137 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
140 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, decl,
141 fold_convert (TREE_TYPE (decl), null_pointer_node));
143 /* Fortran 2008 allows to pass null pointers and non-associated pointers
144 as actual argument to denote absent dummies. For array descriptors,
145 we thus also need to check the array descriptor. */
146 if (!sym->attr.pointer && !sym->attr.allocatable
147 && sym->as && sym->as->type == AS_ASSUMED_SHAPE
148 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
151 tmp = build_fold_indirect_ref_loc (input_location, decl);
152 tmp = gfc_conv_array_data (tmp);
153 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, tmp,
154 fold_convert (TREE_TYPE (tmp), null_pointer_node));
155 cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
156 boolean_type_node, cond, tmp);
163 /* Converts a missing, dummy argument into a null or zero. */
166 gfc_conv_missing_dummy (gfc_se * se, gfc_expr * arg, gfc_typespec ts, int kind)
171 present = gfc_conv_expr_present (arg->symtree->n.sym);
175 /* Create a temporary and convert it to the correct type. */
176 tmp = gfc_get_int_type (kind);
177 tmp = fold_convert (tmp, build_fold_indirect_ref_loc (input_location,
180 /* Test for a NULL value. */
181 tmp = build3 (COND_EXPR, TREE_TYPE (tmp), present, tmp,
182 fold_convert (TREE_TYPE (tmp), integer_one_node));
183 tmp = gfc_evaluate_now (tmp, &se->pre);
184 se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
188 tmp = build3 (COND_EXPR, TREE_TYPE (se->expr), present, se->expr,
189 fold_convert (TREE_TYPE (se->expr), integer_zero_node));
190 tmp = gfc_evaluate_now (tmp, &se->pre);
194 if (ts.type == BT_CHARACTER)
196 tmp = build_int_cst (gfc_charlen_type_node, 0);
197 tmp = fold_build3_loc (input_location, COND_EXPR, gfc_charlen_type_node,
198 present, se->string_length, tmp);
199 tmp = gfc_evaluate_now (tmp, &se->pre);
200 se->string_length = tmp;
206 /* Get the character length of an expression, looking through gfc_refs
210 gfc_get_expr_charlen (gfc_expr *e)
215 gcc_assert (e->expr_type == EXPR_VARIABLE
216 && e->ts.type == BT_CHARACTER);
218 length = NULL; /* To silence compiler warning. */
220 if (is_subref_array (e) && e->ts.u.cl->length)
223 gfc_init_se (&tmpse, NULL);
224 gfc_conv_expr_type (&tmpse, e->ts.u.cl->length, gfc_charlen_type_node);
225 e->ts.u.cl->backend_decl = tmpse.expr;
229 /* First candidate: if the variable is of type CHARACTER, the
230 expression's length could be the length of the character
232 if (e->symtree->n.sym->ts.type == BT_CHARACTER)
233 length = e->symtree->n.sym->ts.u.cl->backend_decl;
235 /* Look through the reference chain for component references. */
236 for (r = e->ref; r; r = r->next)
241 if (r->u.c.component->ts.type == BT_CHARACTER)
242 length = r->u.c.component->ts.u.cl->backend_decl;
250 /* We should never got substring references here. These will be
251 broken down by the scalarizer. */
257 gcc_assert (length != NULL);
262 /* For each character array constructor subexpression without a ts.u.cl->length,
263 replace it by its first element (if there aren't any elements, the length
264 should already be set to zero). */
267 flatten_array_ctors_without_strlen (gfc_expr* e)
269 gfc_actual_arglist* arg;
275 switch (e->expr_type)
279 flatten_array_ctors_without_strlen (e->value.op.op1);
280 flatten_array_ctors_without_strlen (e->value.op.op2);
284 /* TODO: Implement as with EXPR_FUNCTION when needed. */
288 for (arg = e->value.function.actual; arg; arg = arg->next)
289 flatten_array_ctors_without_strlen (arg->expr);
294 /* We've found what we're looking for. */
295 if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
300 gcc_assert (e->value.constructor);
302 c = gfc_constructor_first (e->value.constructor);
306 flatten_array_ctors_without_strlen (new_expr);
307 gfc_replace_expr (e, new_expr);
311 /* Otherwise, fall through to handle constructor elements. */
313 for (c = gfc_constructor_first (e->value.constructor);
314 c; c = gfc_constructor_next (c))
315 flatten_array_ctors_without_strlen (c->expr);
325 /* Generate code to initialize a string length variable. Returns the
326 value. For array constructors, cl->length might be NULL and in this case,
327 the first element of the constructor is needed. expr is the original
328 expression so we can access it but can be NULL if this is not needed. */
331 gfc_conv_string_length (gfc_charlen * cl, gfc_expr * expr, stmtblock_t * pblock)
335 gfc_init_se (&se, NULL);
337 /* If cl->length is NULL, use gfc_conv_expr to obtain the string length but
338 "flatten" array constructors by taking their first element; all elements
339 should be the same length or a cl->length should be present. */
345 expr_flat = gfc_copy_expr (expr);
346 flatten_array_ctors_without_strlen (expr_flat);
347 gfc_resolve_expr (expr_flat);
349 gfc_conv_expr (&se, expr_flat);
350 gfc_add_block_to_block (pblock, &se.pre);
351 cl->backend_decl = convert (gfc_charlen_type_node, se.string_length);
353 gfc_free_expr (expr_flat);
357 /* Convert cl->length. */
359 gcc_assert (cl->length);
361 gfc_conv_expr_type (&se, cl->length, gfc_charlen_type_node);
362 se.expr = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
363 se.expr, build_int_cst (gfc_charlen_type_node, 0));
364 gfc_add_block_to_block (pblock, &se.pre);
366 if (cl->backend_decl)
367 gfc_add_modify (pblock, cl->backend_decl, se.expr);
369 cl->backend_decl = gfc_evaluate_now (se.expr, pblock);
374 gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
375 const char *name, locus *where)
384 type = gfc_get_character_type (kind, ref->u.ss.length);
385 type = build_pointer_type (type);
387 gfc_init_se (&start, se);
388 gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
389 gfc_add_block_to_block (&se->pre, &start.pre);
391 if (integer_onep (start.expr))
392 gfc_conv_string_parameter (se);
397 /* Avoid multiple evaluation of substring start. */
398 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
399 start.expr = gfc_evaluate_now (start.expr, &se->pre);
401 /* Change the start of the string. */
402 if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
405 tmp = build_fold_indirect_ref_loc (input_location,
407 tmp = gfc_build_array_ref (tmp, start.expr, NULL);
408 se->expr = gfc_build_addr_expr (type, tmp);
411 /* Length = end + 1 - start. */
412 gfc_init_se (&end, se);
413 if (ref->u.ss.end == NULL)
414 end.expr = se->string_length;
417 gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
418 gfc_add_block_to_block (&se->pre, &end.pre);
422 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
423 end.expr = gfc_evaluate_now (end.expr, &se->pre);
425 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
427 tree nonempty = fold_build2_loc (input_location, LE_EXPR,
428 boolean_type_node, start.expr,
431 /* Check lower bound. */
432 fault = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
434 build_int_cst (gfc_charlen_type_node, 1));
435 fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
436 boolean_type_node, nonempty, fault);
438 asprintf (&msg, "Substring out of bounds: lower bound (%%ld) of '%s' "
439 "is less than one", name);
441 asprintf (&msg, "Substring out of bounds: lower bound (%%ld)"
443 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
444 fold_convert (long_integer_type_node,
448 /* Check upper bound. */
449 fault = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
450 end.expr, se->string_length);
451 fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
452 boolean_type_node, nonempty, fault);
454 asprintf (&msg, "Substring out of bounds: upper bound (%%ld) of '%s' "
455 "exceeds string length (%%ld)", name);
457 asprintf (&msg, "Substring out of bounds: upper bound (%%ld) "
458 "exceeds string length (%%ld)");
459 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
460 fold_convert (long_integer_type_node, end.expr),
461 fold_convert (long_integer_type_node,
466 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_charlen_type_node,
467 end.expr, start.expr);
468 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_charlen_type_node,
469 build_int_cst (gfc_charlen_type_node, 1), tmp);
470 tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node, tmp,
471 build_int_cst (gfc_charlen_type_node, 0));
472 se->string_length = tmp;
476 /* Convert a derived type component reference. */
479 gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
486 c = ref->u.c.component;
488 gcc_assert (c->backend_decl);
490 field = c->backend_decl;
491 gcc_assert (TREE_CODE (field) == FIELD_DECL);
493 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
494 decl, field, NULL_TREE);
498 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer)
500 tmp = c->ts.u.cl->backend_decl;
501 /* Components must always be constant length. */
502 gcc_assert (tmp && INTEGER_CST_P (tmp));
503 se->string_length = tmp;
506 if (((c->attr.pointer || c->attr.allocatable) && c->attr.dimension == 0
507 && c->ts.type != BT_CHARACTER)
508 || c->attr.proc_pointer)
509 se->expr = build_fold_indirect_ref_loc (input_location,
514 /* This function deals with component references to components of the
515 parent type for derived type extensons. */
517 conv_parent_component_references (gfc_se * se, gfc_ref * ref)
525 c = ref->u.c.component;
527 /* Build a gfc_ref to recursively call gfc_conv_component_ref. */
528 parent.type = REF_COMPONENT;
531 parent.u.c.component = dt->components;
533 if (dt->backend_decl == NULL)
534 gfc_get_derived_type (dt);
536 if (dt->attr.extension && dt->components)
538 if (dt->attr.is_class)
539 cmp = dt->components;
541 cmp = dt->components->next;
542 /* Return if the component is not in the parent type. */
543 for (; cmp; cmp = cmp->next)
544 if (strcmp (c->name, cmp->name) == 0)
547 /* Otherwise build the reference and call self. */
548 gfc_conv_component_ref (se, &parent);
549 parent.u.c.sym = dt->components->ts.u.derived;
550 parent.u.c.component = c;
551 conv_parent_component_references (se, &parent);
555 /* Return the contents of a variable. Also handles reference/pointer
556 variables (all Fortran pointer references are implicit). */
559 gfc_conv_variable (gfc_se * se, gfc_expr * expr)
563 tree parent_decl = NULL_TREE;
566 bool alternate_entry;
569 sym = expr->symtree->n.sym;
572 /* Check that something hasn't gone horribly wrong. */
573 gcc_assert (se->ss != gfc_ss_terminator);
574 gcc_assert (se->ss->expr == expr);
576 /* A scalarized term. We already know the descriptor. */
577 se->expr = se->ss->data.info.descriptor;
578 se->string_length = se->ss->string_length;
579 for (ref = se->ss->data.info.ref; ref; ref = ref->next)
580 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
585 tree se_expr = NULL_TREE;
587 se->expr = gfc_get_symbol_decl (sym);
589 /* Deal with references to a parent results or entries by storing
590 the current_function_decl and moving to the parent_decl. */
591 return_value = sym->attr.function && sym->result == sym;
592 alternate_entry = sym->attr.function && sym->attr.entry
593 && sym->result == sym;
594 entry_master = sym->attr.result
595 && sym->ns->proc_name->attr.entry_master
596 && !gfc_return_by_reference (sym->ns->proc_name);
597 if (current_function_decl)
598 parent_decl = DECL_CONTEXT (current_function_decl);
600 if ((se->expr == parent_decl && return_value)
601 || (sym->ns && sym->ns->proc_name
603 && sym->ns->proc_name->backend_decl == parent_decl
604 && (alternate_entry || entry_master)))
609 /* Special case for assigning the return value of a function.
610 Self recursive functions must have an explicit return value. */
611 if (return_value && (se->expr == current_function_decl || parent_flag))
612 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
614 /* Similarly for alternate entry points. */
615 else if (alternate_entry
616 && (sym->ns->proc_name->backend_decl == current_function_decl
619 gfc_entry_list *el = NULL;
621 for (el = sym->ns->entries; el; el = el->next)
624 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
629 else if (entry_master
630 && (sym->ns->proc_name->backend_decl == current_function_decl
632 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
637 /* Procedure actual arguments. */
638 else if (sym->attr.flavor == FL_PROCEDURE
639 && se->expr != current_function_decl)
641 if (!sym->attr.dummy && !sym->attr.proc_pointer)
643 gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
644 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
650 /* Dereference the expression, where needed. Since characters
651 are entirely different from other types, they are treated
653 if (sym->ts.type == BT_CHARACTER)
655 /* Dereference character pointer dummy arguments
657 if ((sym->attr.pointer || sym->attr.allocatable)
659 || sym->attr.function
660 || sym->attr.result))
661 se->expr = build_fold_indirect_ref_loc (input_location,
665 else if (!sym->attr.value)
667 /* Dereference non-character scalar dummy arguments. */
668 if (sym->attr.dummy && !sym->attr.dimension)
669 se->expr = build_fold_indirect_ref_loc (input_location,
672 /* Dereference scalar hidden result. */
673 if (gfc_option.flag_f2c && sym->ts.type == BT_COMPLEX
674 && (sym->attr.function || sym->attr.result)
675 && !sym->attr.dimension && !sym->attr.pointer
676 && !sym->attr.always_explicit)
677 se->expr = build_fold_indirect_ref_loc (input_location,
680 /* Dereference non-character pointer variables.
681 These must be dummies, results, or scalars. */
682 if ((sym->attr.pointer || sym->attr.allocatable
683 || gfc_is_associate_pointer (sym))
685 || sym->attr.function
687 || !sym->attr.dimension))
688 se->expr = build_fold_indirect_ref_loc (input_location,
695 /* For character variables, also get the length. */
696 if (sym->ts.type == BT_CHARACTER)
698 /* If the character length of an entry isn't set, get the length from
699 the master function instead. */
700 if (sym->attr.entry && !sym->ts.u.cl->backend_decl)
701 se->string_length = sym->ns->proc_name->ts.u.cl->backend_decl;
703 se->string_length = sym->ts.u.cl->backend_decl;
704 gcc_assert (se->string_length);
712 /* Return the descriptor if that's what we want and this is an array
713 section reference. */
714 if (se->descriptor_only && ref->u.ar.type != AR_ELEMENT)
716 /* TODO: Pointers to single elements of array sections, eg elemental subs. */
717 /* Return the descriptor for array pointers and allocations. */
719 && ref->next == NULL && (se->descriptor_only))
722 gfc_conv_array_ref (se, &ref->u.ar, sym, &expr->where);
723 /* Return a pointer to an element. */
727 if (ref->u.c.sym->attr.extension)
728 conv_parent_component_references (se, ref);
730 gfc_conv_component_ref (se, ref);
734 gfc_conv_substring (se, ref, expr->ts.kind,
735 expr->symtree->name, &expr->where);
744 /* Pointer assignment, allocation or pass by reference. Arrays are handled
746 if (se->want_pointer)
748 if (expr->ts.type == BT_CHARACTER && !gfc_is_proc_ptr_comp (expr, NULL))
749 gfc_conv_string_parameter (se);
751 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
756 /* Unary ops are easy... Or they would be if ! was a valid op. */
759 gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
764 gcc_assert (expr->ts.type != BT_CHARACTER);
765 /* Initialize the operand. */
766 gfc_init_se (&operand, se);
767 gfc_conv_expr_val (&operand, expr->value.op.op1);
768 gfc_add_block_to_block (&se->pre, &operand.pre);
770 type = gfc_typenode_for_spec (&expr->ts);
772 /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
773 We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
774 All other unary operators have an equivalent GIMPLE unary operator. */
775 if (code == TRUTH_NOT_EXPR)
776 se->expr = fold_build2_loc (input_location, EQ_EXPR, type, operand.expr,
777 build_int_cst (type, 0));
779 se->expr = fold_build1_loc (input_location, code, type, operand.expr);
783 /* Expand power operator to optimal multiplications when a value is raised
784 to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
785 Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
786 Programming", 3rd Edition, 1998. */
788 /* This code is mostly duplicated from expand_powi in the backend.
789 We establish the "optimal power tree" lookup table with the defined size.
790 The items in the table are the exponents used to calculate the index
791 exponents. Any integer n less than the value can get an "addition chain",
792 with the first node being one. */
793 #define POWI_TABLE_SIZE 256
795 /* The table is from builtins.c. */
796 static const unsigned char powi_table[POWI_TABLE_SIZE] =
798 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
799 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
800 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
801 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
802 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
803 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
804 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
805 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
806 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
807 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
808 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
809 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
810 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
811 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
812 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
813 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
814 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
815 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
816 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
817 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
818 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
819 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
820 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
821 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
822 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
823 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
824 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
825 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
826 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
827 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
828 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
829 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
832 /* If n is larger than lookup table's max index, we use the "window
834 #define POWI_WINDOW_SIZE 3
836 /* Recursive function to expand the power operator. The temporary
837 values are put in tmpvar. The function returns tmpvar[1] ** n. */
839 gfc_conv_powi (gfc_se * se, unsigned HOST_WIDE_INT n, tree * tmpvar)
846 if (n < POWI_TABLE_SIZE)
851 op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
852 op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
856 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
857 op0 = gfc_conv_powi (se, n - digit, tmpvar);
858 op1 = gfc_conv_powi (se, digit, tmpvar);
862 op0 = gfc_conv_powi (se, n >> 1, tmpvar);
866 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (op0), op0, op1);
867 tmp = gfc_evaluate_now (tmp, &se->pre);
869 if (n < POWI_TABLE_SIZE)
876 /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
877 return 1. Else return 0 and a call to runtime library functions
878 will have to be built. */
880 gfc_conv_cst_int_power (gfc_se * se, tree lhs, tree rhs)
885 tree vartmp[POWI_TABLE_SIZE];
887 unsigned HOST_WIDE_INT n;
890 /* If exponent is too large, we won't expand it anyway, so don't bother
891 with large integer values. */
892 if (!double_int_fits_in_shwi_p (TREE_INT_CST (rhs)))
895 m = double_int_to_shwi (TREE_INT_CST (rhs));
896 /* There's no ABS for HOST_WIDE_INT, so here we go. It also takes care
897 of the asymmetric range of the integer type. */
898 n = (unsigned HOST_WIDE_INT) (m < 0 ? -m : m);
900 type = TREE_TYPE (lhs);
901 sgn = tree_int_cst_sgn (rhs);
903 if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
904 || optimize_size) && (m > 2 || m < -1))
910 se->expr = gfc_build_const (type, integer_one_node);
914 /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1. */
915 if ((sgn == -1) && (TREE_CODE (type) == INTEGER_TYPE))
917 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
918 lhs, build_int_cst (TREE_TYPE (lhs), -1));
919 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
920 lhs, build_int_cst (TREE_TYPE (lhs), 1));
923 result = (lhs == 1 || lhs == -1) ? 1 : 0. */
926 tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR,
927 boolean_type_node, tmp, cond);
928 se->expr = fold_build3_loc (input_location, COND_EXPR, type,
929 tmp, build_int_cst (type, 1),
930 build_int_cst (type, 0));
934 result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0. */
935 tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp,
936 build_int_cst (type, -1),
937 build_int_cst (type, 0));
938 se->expr = fold_build3_loc (input_location, COND_EXPR, type,
939 cond, build_int_cst (type, 1), tmp);
943 memset (vartmp, 0, sizeof (vartmp));
947 tmp = gfc_build_const (type, integer_one_node);
948 vartmp[1] = fold_build2_loc (input_location, RDIV_EXPR, type, tmp,
952 se->expr = gfc_conv_powi (se, n, vartmp);
958 /* Power op (**). Constant integer exponent has special handling. */
961 gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
963 tree gfc_int4_type_node;
970 gfc_init_se (&lse, se);
971 gfc_conv_expr_val (&lse, expr->value.op.op1);
972 lse.expr = gfc_evaluate_now (lse.expr, &lse.pre);
973 gfc_add_block_to_block (&se->pre, &lse.pre);
975 gfc_init_se (&rse, se);
976 gfc_conv_expr_val (&rse, expr->value.op.op2);
977 gfc_add_block_to_block (&se->pre, &rse.pre);
979 if (expr->value.op.op2->ts.type == BT_INTEGER
980 && expr->value.op.op2->expr_type == EXPR_CONSTANT)
981 if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
984 gfc_int4_type_node = gfc_get_int_type (4);
986 kind = expr->value.op.op1->ts.kind;
987 switch (expr->value.op.op2->ts.type)
990 ikind = expr->value.op.op2->ts.kind;
995 rse.expr = convert (gfc_int4_type_node, rse.expr);
1017 if (expr->value.op.op1->ts.type == BT_INTEGER)
1018 lse.expr = convert (gfc_int4_type_node, lse.expr);
1043 switch (expr->value.op.op1->ts.type)
1046 if (kind == 3) /* Case 16 was not handled properly above. */
1048 fndecl = gfor_fndecl_math_powi[kind][ikind].integer;
1052 /* Use builtins for real ** int4. */
1058 fndecl = built_in_decls[BUILT_IN_POWIF];
1062 fndecl = built_in_decls[BUILT_IN_POWI];
1066 fndecl = built_in_decls[BUILT_IN_POWIL];
1070 /* Use the __builtin_powil() only if real(kind=16) is
1071 actually the C long double type. */
1072 if (!gfc_real16_is_float128)
1073 fndecl = built_in_decls[BUILT_IN_POWIL];
1081 /* If we don't have a good builtin for this, go for the
1082 library function. */
1084 fndecl = gfor_fndecl_math_powi[kind][ikind].real;
1088 fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
1097 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_POW, kind);
1101 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_CPOW, kind);
1109 se->expr = build_call_expr_loc (input_location,
1110 fndecl, 2, lse.expr, rse.expr);
1114 /* Generate code to allocate a string temporary. */
1117 gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
1122 if (gfc_can_put_var_on_stack (len))
1124 /* Create a temporary variable to hold the result. */
1125 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1126 gfc_charlen_type_node, len,
1127 build_int_cst (gfc_charlen_type_node, 1));
1128 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node, tmp);
1130 if (TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
1131 tmp = build_array_type (TREE_TYPE (TREE_TYPE (type)), tmp);
1133 tmp = build_array_type (TREE_TYPE (type), tmp);
1135 var = gfc_create_var (tmp, "str");
1136 var = gfc_build_addr_expr (type, var);
1140 /* Allocate a temporary to hold the result. */
1141 var = gfc_create_var (type, "pstr");
1142 tmp = gfc_call_malloc (&se->pre, type,
1143 fold_build2_loc (input_location, MULT_EXPR,
1144 TREE_TYPE (len), len,
1145 fold_convert (TREE_TYPE (len),
1146 TYPE_SIZE (type))));
1147 gfc_add_modify (&se->pre, var, tmp);
1149 /* Free the temporary afterwards. */
1150 tmp = gfc_call_free (convert (pvoid_type_node, var));
1151 gfc_add_expr_to_block (&se->post, tmp);
1158 /* Handle a string concatenation operation. A temporary will be allocated to
1162 gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
1165 tree len, type, var, tmp, fndecl;
1167 gcc_assert (expr->value.op.op1->ts.type == BT_CHARACTER
1168 && expr->value.op.op2->ts.type == BT_CHARACTER);
1169 gcc_assert (expr->value.op.op1->ts.kind == expr->value.op.op2->ts.kind);
1171 gfc_init_se (&lse, se);
1172 gfc_conv_expr (&lse, expr->value.op.op1);
1173 gfc_conv_string_parameter (&lse);
1174 gfc_init_se (&rse, se);
1175 gfc_conv_expr (&rse, expr->value.op.op2);
1176 gfc_conv_string_parameter (&rse);
1178 gfc_add_block_to_block (&se->pre, &lse.pre);
1179 gfc_add_block_to_block (&se->pre, &rse.pre);
1181 type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
1182 len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
1183 if (len == NULL_TREE)
1185 len = fold_build2_loc (input_location, PLUS_EXPR,
1186 TREE_TYPE (lse.string_length),
1187 lse.string_length, rse.string_length);
1190 type = build_pointer_type (type);
1192 var = gfc_conv_string_tmp (se, type, len);
1194 /* Do the actual concatenation. */
1195 if (expr->ts.kind == 1)
1196 fndecl = gfor_fndecl_concat_string;
1197 else if (expr->ts.kind == 4)
1198 fndecl = gfor_fndecl_concat_string_char4;
1202 tmp = build_call_expr_loc (input_location,
1203 fndecl, 6, len, var, lse.string_length, lse.expr,
1204 rse.string_length, rse.expr);
1205 gfc_add_expr_to_block (&se->pre, tmp);
1207 /* Add the cleanup for the operands. */
1208 gfc_add_block_to_block (&se->pre, &rse.post);
1209 gfc_add_block_to_block (&se->pre, &lse.post);
1212 se->string_length = len;
1215 /* Translates an op expression. Common (binary) cases are handled by this
1216 function, others are passed on. Recursion is used in either case.
1217 We use the fact that (op1.ts == op2.ts) (except for the power
1219 Operators need no special handling for scalarized expressions as long as
1220 they call gfc_conv_simple_val to get their operands.
1221 Character strings get special handling. */
1224 gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
1226 enum tree_code code;
1235 switch (expr->value.op.op)
1237 case INTRINSIC_PARENTHESES:
1238 if ((expr->ts.type == BT_REAL
1239 || expr->ts.type == BT_COMPLEX)
1240 && gfc_option.flag_protect_parens)
1242 gfc_conv_unary_op (PAREN_EXPR, se, expr);
1243 gcc_assert (FLOAT_TYPE_P (TREE_TYPE (se->expr)));
1248 case INTRINSIC_UPLUS:
1249 gfc_conv_expr (se, expr->value.op.op1);
1252 case INTRINSIC_UMINUS:
1253 gfc_conv_unary_op (NEGATE_EXPR, se, expr);
1257 gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
1260 case INTRINSIC_PLUS:
1264 case INTRINSIC_MINUS:
1268 case INTRINSIC_TIMES:
1272 case INTRINSIC_DIVIDE:
1273 /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
1274 an integer, we must round towards zero, so we use a
1276 if (expr->ts.type == BT_INTEGER)
1277 code = TRUNC_DIV_EXPR;
1282 case INTRINSIC_POWER:
1283 gfc_conv_power_op (se, expr);
1286 case INTRINSIC_CONCAT:
1287 gfc_conv_concat_op (se, expr);
1291 code = TRUTH_ANDIF_EXPR;
1296 code = TRUTH_ORIF_EXPR;
1300 /* EQV and NEQV only work on logicals, but since we represent them
1301 as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE. */
1303 case INTRINSIC_EQ_OS:
1311 case INTRINSIC_NE_OS:
1312 case INTRINSIC_NEQV:
1319 case INTRINSIC_GT_OS:
1326 case INTRINSIC_GE_OS:
1333 case INTRINSIC_LT_OS:
1340 case INTRINSIC_LE_OS:
1346 case INTRINSIC_USER:
1347 case INTRINSIC_ASSIGN:
1348 /* These should be converted into function calls by the frontend. */
1352 fatal_error ("Unknown intrinsic op");
1356 /* The only exception to this is **, which is handled separately anyway. */
1357 gcc_assert (expr->value.op.op1->ts.type == expr->value.op.op2->ts.type);
1359 if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
1363 gfc_init_se (&lse, se);
1364 gfc_conv_expr (&lse, expr->value.op.op1);
1365 gfc_add_block_to_block (&se->pre, &lse.pre);
1368 gfc_init_se (&rse, se);
1369 gfc_conv_expr (&rse, expr->value.op.op2);
1370 gfc_add_block_to_block (&se->pre, &rse.pre);
1374 gfc_conv_string_parameter (&lse);
1375 gfc_conv_string_parameter (&rse);
1377 lse.expr = gfc_build_compare_string (lse.string_length, lse.expr,
1378 rse.string_length, rse.expr,
1379 expr->value.op.op1->ts.kind,
1381 rse.expr = build_int_cst (TREE_TYPE (lse.expr), 0);
1382 gfc_add_block_to_block (&lse.post, &rse.post);
1385 type = gfc_typenode_for_spec (&expr->ts);
1389 /* The result of logical ops is always boolean_type_node. */
1390 tmp = fold_build2_loc (input_location, code, boolean_type_node,
1391 lse.expr, rse.expr);
1392 se->expr = convert (type, tmp);
1395 se->expr = fold_build2_loc (input_location, code, type, lse.expr, rse.expr);
1397 /* Add the post blocks. */
1398 gfc_add_block_to_block (&se->post, &rse.post);
1399 gfc_add_block_to_block (&se->post, &lse.post);
1402 /* If a string's length is one, we convert it to a single character. */
1405 gfc_string_to_single_character (tree len, tree str, int kind)
1407 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str)));
1409 if (!INTEGER_CST_P (len) || TREE_INT_CST_HIGH (len) != 0)
1412 if (TREE_INT_CST_LOW (len) == 1)
1414 str = fold_convert (gfc_get_pchar_type (kind), str);
1415 return build_fold_indirect_ref_loc (input_location, str);
1419 && TREE_CODE (str) == ADDR_EXPR
1420 && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
1421 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
1422 && array_ref_low_bound (TREE_OPERAND (str, 0))
1423 == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
1424 && TREE_INT_CST_LOW (len) > 1
1425 && TREE_INT_CST_LOW (len)
1426 == (unsigned HOST_WIDE_INT)
1427 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
1429 tree ret = fold_convert (gfc_get_pchar_type (kind), str);
1430 ret = build_fold_indirect_ref_loc (input_location, ret);
1431 if (TREE_CODE (ret) == INTEGER_CST)
1433 tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
1434 int i, length = TREE_STRING_LENGTH (string_cst);
1435 const char *ptr = TREE_STRING_POINTER (string_cst);
1437 for (i = 1; i < length; i++)
1450 gfc_conv_scalar_char_value (gfc_symbol *sym, gfc_se *se, gfc_expr **expr)
1453 if (sym->backend_decl)
1455 /* This becomes the nominal_type in
1456 function.c:assign_parm_find_data_types. */
1457 TREE_TYPE (sym->backend_decl) = unsigned_char_type_node;
1458 /* This becomes the passed_type in
1459 function.c:assign_parm_find_data_types. C promotes char to
1460 integer for argument passing. */
1461 DECL_ARG_TYPE (sym->backend_decl) = unsigned_type_node;
1463 DECL_BY_REFERENCE (sym->backend_decl) = 0;
1468 /* If we have a constant character expression, make it into an
1470 if ((*expr)->expr_type == EXPR_CONSTANT)
1475 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL,
1476 (int)(*expr)->value.character.string[0]);
1477 if ((*expr)->ts.kind != gfc_c_int_kind)
1479 /* The expr needs to be compatible with a C int. If the
1480 conversion fails, then the 2 causes an ICE. */
1481 ts.type = BT_INTEGER;
1482 ts.kind = gfc_c_int_kind;
1483 gfc_convert_type (*expr, &ts, 2);
1486 else if (se != NULL && (*expr)->expr_type == EXPR_VARIABLE)
1488 if ((*expr)->ref == NULL)
1490 se->expr = gfc_string_to_single_character
1491 (build_int_cst (integer_type_node, 1),
1492 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
1494 ((*expr)->symtree->n.sym)),
1499 gfc_conv_variable (se, *expr);
1500 se->expr = gfc_string_to_single_character
1501 (build_int_cst (integer_type_node, 1),
1502 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
1510 /* Helper function for gfc_build_compare_string. Return LEN_TRIM value
1511 if STR is a string literal, otherwise return -1. */
1514 gfc_optimize_len_trim (tree len, tree str, int kind)
1517 && TREE_CODE (str) == ADDR_EXPR
1518 && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
1519 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
1520 && array_ref_low_bound (TREE_OPERAND (str, 0))
1521 == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
1522 && TREE_INT_CST_LOW (len) >= 1
1523 && TREE_INT_CST_LOW (len)
1524 == (unsigned HOST_WIDE_INT)
1525 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
1527 tree folded = fold_convert (gfc_get_pchar_type (kind), str);
1528 folded = build_fold_indirect_ref_loc (input_location, folded);
1529 if (TREE_CODE (folded) == INTEGER_CST)
1531 tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
1532 int length = TREE_STRING_LENGTH (string_cst);
1533 const char *ptr = TREE_STRING_POINTER (string_cst);
1535 for (; length > 0; length--)
1536 if (ptr[length - 1] != ' ')
1545 /* Compare two strings. If they are all single characters, the result is the
1546 subtraction of them. Otherwise, we build a library call. */
1549 gfc_build_compare_string (tree len1, tree str1, tree len2, tree str2, int kind,
1550 enum tree_code code)
1556 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1)));
1557 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2)));
1559 sc1 = gfc_string_to_single_character (len1, str1, kind);
1560 sc2 = gfc_string_to_single_character (len2, str2, kind);
1562 if (sc1 != NULL_TREE && sc2 != NULL_TREE)
1564 /* Deal with single character specially. */
1565 sc1 = fold_convert (integer_type_node, sc1);
1566 sc2 = fold_convert (integer_type_node, sc2);
1567 return fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
1571 if ((code == EQ_EXPR || code == NE_EXPR)
1573 && INTEGER_CST_P (len1) && INTEGER_CST_P (len2))
1575 /* If one string is a string literal with LEN_TRIM longer
1576 than the length of the second string, the strings
1578 int len = gfc_optimize_len_trim (len1, str1, kind);
1579 if (len > 0 && compare_tree_int (len2, len) < 0)
1580 return integer_one_node;
1581 len = gfc_optimize_len_trim (len2, str2, kind);
1582 if (len > 0 && compare_tree_int (len1, len) < 0)
1583 return integer_one_node;
1586 /* Build a call for the comparison. */
1588 fndecl = gfor_fndecl_compare_string;
1590 fndecl = gfor_fndecl_compare_string_char4;
1594 return build_call_expr_loc (input_location, fndecl, 4,
1595 len1, str1, len2, str2);
1599 /* Return the backend_decl for a procedure pointer component. */
1602 get_proc_ptr_comp (gfc_expr *e)
1606 gfc_init_se (&comp_se, NULL);
1607 e2 = gfc_copy_expr (e);
1608 e2->expr_type = EXPR_VARIABLE;
1609 gfc_conv_expr (&comp_se, e2);
1611 return build_fold_addr_expr_loc (input_location, comp_se.expr);
1616 conv_function_val (gfc_se * se, gfc_symbol * sym, gfc_expr * expr)
1620 if (gfc_is_proc_ptr_comp (expr, NULL))
1621 tmp = get_proc_ptr_comp (expr);
1622 else if (sym->attr.dummy)
1624 tmp = gfc_get_symbol_decl (sym);
1625 if (sym->attr.proc_pointer)
1626 tmp = build_fold_indirect_ref_loc (input_location,
1628 gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE
1629 && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) == FUNCTION_TYPE);
1633 if (!sym->backend_decl)
1634 sym->backend_decl = gfc_get_extern_function_decl (sym);
1636 tmp = sym->backend_decl;
1638 if (sym->attr.cray_pointee)
1640 /* TODO - make the cray pointee a pointer to a procedure,
1641 assign the pointer to it and use it for the call. This
1643 tmp = convert (build_pointer_type (TREE_TYPE (tmp)),
1644 gfc_get_symbol_decl (sym->cp_pointer));
1645 tmp = gfc_evaluate_now (tmp, &se->pre);
1648 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
1650 gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
1651 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
1658 /* Initialize MAPPING. */
1661 gfc_init_interface_mapping (gfc_interface_mapping * mapping)
1663 mapping->syms = NULL;
1664 mapping->charlens = NULL;
1668 /* Free all memory held by MAPPING (but not MAPPING itself). */
1671 gfc_free_interface_mapping (gfc_interface_mapping * mapping)
1673 gfc_interface_sym_mapping *sym;
1674 gfc_interface_sym_mapping *nextsym;
1676 gfc_charlen *nextcl;
1678 for (sym = mapping->syms; sym; sym = nextsym)
1680 nextsym = sym->next;
1681 sym->new_sym->n.sym->formal = NULL;
1682 gfc_free_symbol (sym->new_sym->n.sym);
1683 gfc_free_expr (sym->expr);
1684 gfc_free (sym->new_sym);
1687 for (cl = mapping->charlens; cl; cl = nextcl)
1690 gfc_free_expr (cl->length);
1696 /* Return a copy of gfc_charlen CL. Add the returned structure to
1697 MAPPING so that it will be freed by gfc_free_interface_mapping. */
1699 static gfc_charlen *
1700 gfc_get_interface_mapping_charlen (gfc_interface_mapping * mapping,
1703 gfc_charlen *new_charlen;
1705 new_charlen = gfc_get_charlen ();
1706 new_charlen->next = mapping->charlens;
1707 new_charlen->length = gfc_copy_expr (cl->length);
1709 mapping->charlens = new_charlen;
1714 /* A subroutine of gfc_add_interface_mapping. Return a descriptorless
1715 array variable that can be used as the actual argument for dummy
1716 argument SYM. Add any initialization code to BLOCK. PACKED is as
1717 for gfc_get_nodesc_array_type and DATA points to the first element
1718 in the passed array. */
1721 gfc_get_interface_mapping_array (stmtblock_t * block, gfc_symbol * sym,
1722 gfc_packed packed, tree data)
1727 type = gfc_typenode_for_spec (&sym->ts);
1728 type = gfc_get_nodesc_array_type (type, sym->as, packed,
1729 !sym->attr.target && !sym->attr.pointer
1730 && !sym->attr.proc_pointer);
1732 var = gfc_create_var (type, "ifm");
1733 gfc_add_modify (block, var, fold_convert (type, data));
1739 /* A subroutine of gfc_add_interface_mapping. Set the stride, upper bounds
1740 and offset of descriptorless array type TYPE given that it has the same
1741 size as DESC. Add any set-up code to BLOCK. */
1744 gfc_set_interface_mapping_bounds (stmtblock_t * block, tree type, tree desc)
1751 offset = gfc_index_zero_node;
1752 for (n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
1754 dim = gfc_rank_cst[n];
1755 GFC_TYPE_ARRAY_STRIDE (type, n) = gfc_conv_array_stride (desc, n);
1756 if (GFC_TYPE_ARRAY_LBOUND (type, n) == NULL_TREE)
1758 GFC_TYPE_ARRAY_LBOUND (type, n)
1759 = gfc_conv_descriptor_lbound_get (desc, dim);
1760 GFC_TYPE_ARRAY_UBOUND (type, n)
1761 = gfc_conv_descriptor_ubound_get (desc, dim);
1763 else if (GFC_TYPE_ARRAY_UBOUND (type, n) == NULL_TREE)
1765 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1766 gfc_array_index_type,
1767 gfc_conv_descriptor_ubound_get (desc, dim),
1768 gfc_conv_descriptor_lbound_get (desc, dim));
1769 tmp = fold_build2_loc (input_location, PLUS_EXPR,
1770 gfc_array_index_type,
1771 GFC_TYPE_ARRAY_LBOUND (type, n), tmp);
1772 tmp = gfc_evaluate_now (tmp, block);
1773 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1775 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
1776 GFC_TYPE_ARRAY_LBOUND (type, n),
1777 GFC_TYPE_ARRAY_STRIDE (type, n));
1778 offset = fold_build2_loc (input_location, MINUS_EXPR,
1779 gfc_array_index_type, offset, tmp);
1781 offset = gfc_evaluate_now (offset, block);
1782 GFC_TYPE_ARRAY_OFFSET (type) = offset;
1786 /* Extend MAPPING so that it maps dummy argument SYM to the value stored
1787 in SE. The caller may still use se->expr and se->string_length after
1788 calling this function. */
1791 gfc_add_interface_mapping (gfc_interface_mapping * mapping,
1792 gfc_symbol * sym, gfc_se * se,
1795 gfc_interface_sym_mapping *sm;
1799 gfc_symbol *new_sym;
1801 gfc_symtree *new_symtree;
1803 /* Create a new symbol to represent the actual argument. */
1804 new_sym = gfc_new_symbol (sym->name, NULL);
1805 new_sym->ts = sym->ts;
1806 new_sym->as = gfc_copy_array_spec (sym->as);
1807 new_sym->attr.referenced = 1;
1808 new_sym->attr.dimension = sym->attr.dimension;
1809 new_sym->attr.contiguous = sym->attr.contiguous;
1810 new_sym->attr.codimension = sym->attr.codimension;
1811 new_sym->attr.pointer = sym->attr.pointer;
1812 new_sym->attr.allocatable = sym->attr.allocatable;
1813 new_sym->attr.flavor = sym->attr.flavor;
1814 new_sym->attr.function = sym->attr.function;
1816 /* Ensure that the interface is available and that
1817 descriptors are passed for array actual arguments. */
1818 if (sym->attr.flavor == FL_PROCEDURE)
1820 new_sym->formal = expr->symtree->n.sym->formal;
1821 new_sym->attr.always_explicit
1822 = expr->symtree->n.sym->attr.always_explicit;
1825 /* Create a fake symtree for it. */
1827 new_symtree = gfc_new_symtree (&root, sym->name);
1828 new_symtree->n.sym = new_sym;
1829 gcc_assert (new_symtree == root);
1831 /* Create a dummy->actual mapping. */
1832 sm = XCNEW (gfc_interface_sym_mapping);
1833 sm->next = mapping->syms;
1835 sm->new_sym = new_symtree;
1836 sm->expr = gfc_copy_expr (expr);
1839 /* Stabilize the argument's value. */
1840 if (!sym->attr.function && se)
1841 se->expr = gfc_evaluate_now (se->expr, &se->pre);
1843 if (sym->ts.type == BT_CHARACTER)
1845 /* Create a copy of the dummy argument's length. */
1846 new_sym->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, sym->ts.u.cl);
1847 sm->expr->ts.u.cl = new_sym->ts.u.cl;
1849 /* If the length is specified as "*", record the length that
1850 the caller is passing. We should use the callee's length
1851 in all other cases. */
1852 if (!new_sym->ts.u.cl->length && se)
1854 se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
1855 new_sym->ts.u.cl->backend_decl = se->string_length;
1862 /* Use the passed value as-is if the argument is a function. */
1863 if (sym->attr.flavor == FL_PROCEDURE)
1866 /* If the argument is either a string or a pointer to a string,
1867 convert it to a boundless character type. */
1868 else if (!sym->attr.dimension && sym->ts.type == BT_CHARACTER)
1870 tmp = gfc_get_character_type_len (sym->ts.kind, NULL);
1871 tmp = build_pointer_type (tmp);
1872 if (sym->attr.pointer)
1873 value = build_fold_indirect_ref_loc (input_location,
1877 value = fold_convert (tmp, value);
1880 /* If the argument is a scalar, a pointer to an array or an allocatable,
1882 else if (!sym->attr.dimension || sym->attr.pointer || sym->attr.allocatable)
1883 value = build_fold_indirect_ref_loc (input_location,
1886 /* For character(*), use the actual argument's descriptor. */
1887 else if (sym->ts.type == BT_CHARACTER && !new_sym->ts.u.cl->length)
1888 value = build_fold_indirect_ref_loc (input_location,
1891 /* If the argument is an array descriptor, use it to determine
1892 information about the actual argument's shape. */
1893 else if (POINTER_TYPE_P (TREE_TYPE (se->expr))
1894 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
1896 /* Get the actual argument's descriptor. */
1897 desc = build_fold_indirect_ref_loc (input_location,
1900 /* Create the replacement variable. */
1901 tmp = gfc_conv_descriptor_data_get (desc);
1902 value = gfc_get_interface_mapping_array (&se->pre, sym,
1905 /* Use DESC to work out the upper bounds, strides and offset. */
1906 gfc_set_interface_mapping_bounds (&se->pre, TREE_TYPE (value), desc);
1909 /* Otherwise we have a packed array. */
1910 value = gfc_get_interface_mapping_array (&se->pre, sym,
1911 PACKED_FULL, se->expr);
1913 new_sym->backend_decl = value;
1917 /* Called once all dummy argument mappings have been added to MAPPING,
1918 but before the mapping is used to evaluate expressions. Pre-evaluate
1919 the length of each argument, adding any initialization code to PRE and
1920 any finalization code to POST. */
1923 gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
1924 stmtblock_t * pre, stmtblock_t * post)
1926 gfc_interface_sym_mapping *sym;
1930 for (sym = mapping->syms; sym; sym = sym->next)
1931 if (sym->new_sym->n.sym->ts.type == BT_CHARACTER
1932 && !sym->new_sym->n.sym->ts.u.cl->backend_decl)
1934 expr = sym->new_sym->n.sym->ts.u.cl->length;
1935 gfc_apply_interface_mapping_to_expr (mapping, expr);
1936 gfc_init_se (&se, NULL);
1937 gfc_conv_expr (&se, expr);
1938 se.expr = fold_convert (gfc_charlen_type_node, se.expr);
1939 se.expr = gfc_evaluate_now (se.expr, &se.pre);
1940 gfc_add_block_to_block (pre, &se.pre);
1941 gfc_add_block_to_block (post, &se.post);
1943 sym->new_sym->n.sym->ts.u.cl->backend_decl = se.expr;
1948 /* Like gfc_apply_interface_mapping_to_expr, but applied to
1952 gfc_apply_interface_mapping_to_cons (gfc_interface_mapping * mapping,
1953 gfc_constructor_base base)
1956 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1958 gfc_apply_interface_mapping_to_expr (mapping, c->expr);
1961 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->start);
1962 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->end);
1963 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->step);
1969 /* Like gfc_apply_interface_mapping_to_expr, but applied to
1973 gfc_apply_interface_mapping_to_ref (gfc_interface_mapping * mapping,
1978 for (; ref; ref = ref->next)
1982 for (n = 0; n < ref->u.ar.dimen; n++)
1984 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.start[n]);
1985 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.end[n]);
1986 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.stride[n]);
1988 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.offset);
1995 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.start);
1996 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.end);
2002 /* Convert intrinsic function calls into result expressions. */
2005 gfc_map_intrinsic_function (gfc_expr *expr, gfc_interface_mapping *mapping)
2013 arg1 = expr->value.function.actual->expr;
2014 if (expr->value.function.actual->next)
2015 arg2 = expr->value.function.actual->next->expr;
2019 sym = arg1->symtree->n.sym;
2021 if (sym->attr.dummy)
2026 switch (expr->value.function.isym->id)
2029 /* TODO figure out why this condition is necessary. */
2030 if (sym->attr.function
2031 && (arg1->ts.u.cl->length == NULL
2032 || (arg1->ts.u.cl->length->expr_type != EXPR_CONSTANT
2033 && arg1->ts.u.cl->length->expr_type != EXPR_VARIABLE)))
2036 new_expr = gfc_copy_expr (arg1->ts.u.cl->length);
2040 if (!sym->as || sym->as->rank == 0)
2043 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
2045 dup = mpz_get_si (arg2->value.integer);
2050 dup = sym->as->rank;
2054 for (; d < dup; d++)
2058 if (!sym->as->upper[d] || !sym->as->lower[d])
2060 gfc_free_expr (new_expr);
2064 tmp = gfc_add (gfc_copy_expr (sym->as->upper[d]),
2065 gfc_get_int_expr (gfc_default_integer_kind,
2067 tmp = gfc_subtract (tmp, gfc_copy_expr (sym->as->lower[d]));
2069 new_expr = gfc_multiply (new_expr, tmp);
2075 case GFC_ISYM_LBOUND:
2076 case GFC_ISYM_UBOUND:
2077 /* TODO These implementations of lbound and ubound do not limit if
2078 the size < 0, according to F95's 13.14.53 and 13.14.113. */
2080 if (!sym->as || sym->as->rank == 0)
2083 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
2084 d = mpz_get_si (arg2->value.integer) - 1;
2086 /* TODO: If the need arises, this could produce an array of
2090 if (expr->value.function.isym->id == GFC_ISYM_LBOUND)
2092 if (sym->as->lower[d])
2093 new_expr = gfc_copy_expr (sym->as->lower[d]);
2097 if (sym->as->upper[d])
2098 new_expr = gfc_copy_expr (sym->as->upper[d]);
2106 gfc_apply_interface_mapping_to_expr (mapping, new_expr);
2110 gfc_replace_expr (expr, new_expr);
2116 gfc_map_fcn_formal_to_actual (gfc_expr *expr, gfc_expr *map_expr,
2117 gfc_interface_mapping * mapping)
2119 gfc_formal_arglist *f;
2120 gfc_actual_arglist *actual;
2122 actual = expr->value.function.actual;
2123 f = map_expr->symtree->n.sym->formal;
2125 for (; f && actual; f = f->next, actual = actual->next)
2130 gfc_add_interface_mapping (mapping, f->sym, NULL, actual->expr);
2133 if (map_expr->symtree->n.sym->attr.dimension)
2138 as = gfc_copy_array_spec (map_expr->symtree->n.sym->as);
2140 for (d = 0; d < as->rank; d++)
2142 gfc_apply_interface_mapping_to_expr (mapping, as->lower[d]);
2143 gfc_apply_interface_mapping_to_expr (mapping, as->upper[d]);
2146 expr->value.function.esym->as = as;
2149 if (map_expr->symtree->n.sym->ts.type == BT_CHARACTER)
2151 expr->value.function.esym->ts.u.cl->length
2152 = gfc_copy_expr (map_expr->symtree->n.sym->ts.u.cl->length);
2154 gfc_apply_interface_mapping_to_expr (mapping,
2155 expr->value.function.esym->ts.u.cl->length);
2160 /* EXPR is a copy of an expression that appeared in the interface
2161 associated with MAPPING. Walk it recursively looking for references to
2162 dummy arguments that MAPPING maps to actual arguments. Replace each such
2163 reference with a reference to the associated actual argument. */
2166 gfc_apply_interface_mapping_to_expr (gfc_interface_mapping * mapping,
2169 gfc_interface_sym_mapping *sym;
2170 gfc_actual_arglist *actual;
2175 /* Copying an expression does not copy its length, so do that here. */
2176 if (expr->ts.type == BT_CHARACTER && expr->ts.u.cl)
2178 expr->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, expr->ts.u.cl);
2179 gfc_apply_interface_mapping_to_expr (mapping, expr->ts.u.cl->length);
2182 /* Apply the mapping to any references. */
2183 gfc_apply_interface_mapping_to_ref (mapping, expr->ref);
2185 /* ...and to the expression's symbol, if it has one. */
2186 /* TODO Find out why the condition on expr->symtree had to be moved into
2187 the loop rather than being outside it, as originally. */
2188 for (sym = mapping->syms; sym; sym = sym->next)
2189 if (expr->symtree && sym->old == expr->symtree->n.sym)
2191 if (sym->new_sym->n.sym->backend_decl)
2192 expr->symtree = sym->new_sym;
2194 gfc_replace_expr (expr, gfc_copy_expr (sym->expr));
2197 /* ...and to subexpressions in expr->value. */
2198 switch (expr->expr_type)
2203 case EXPR_SUBSTRING:
2207 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op1);
2208 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op2);
2212 for (actual = expr->value.function.actual; actual; actual = actual->next)
2213 gfc_apply_interface_mapping_to_expr (mapping, actual->expr);
2215 if (expr->value.function.esym == NULL
2216 && expr->value.function.isym != NULL
2217 && expr->value.function.actual->expr->symtree
2218 && gfc_map_intrinsic_function (expr, mapping))
2221 for (sym = mapping->syms; sym; sym = sym->next)
2222 if (sym->old == expr->value.function.esym)
2224 expr->value.function.esym = sym->new_sym->n.sym;
2225 gfc_map_fcn_formal_to_actual (expr, sym->expr, mapping);
2226 expr->value.function.esym->result = sym->new_sym->n.sym;
2231 case EXPR_STRUCTURE:
2232 gfc_apply_interface_mapping_to_cons (mapping, expr->value.constructor);
2245 /* Evaluate interface expression EXPR using MAPPING. Store the result
2249 gfc_apply_interface_mapping (gfc_interface_mapping * mapping,
2250 gfc_se * se, gfc_expr * expr)
2252 expr = gfc_copy_expr (expr);
2253 gfc_apply_interface_mapping_to_expr (mapping, expr);
2254 gfc_conv_expr (se, expr);
2255 se->expr = gfc_evaluate_now (se->expr, &se->pre);
2256 gfc_free_expr (expr);
2260 /* Returns a reference to a temporary array into which a component of
2261 an actual argument derived type array is copied and then returned
2262 after the function call. */
2264 gfc_conv_subref_array_arg (gfc_se * parmse, gfc_expr * expr, int g77,
2265 sym_intent intent, bool formal_ptr)
2283 gcc_assert (expr->expr_type == EXPR_VARIABLE);
2285 gfc_init_se (&lse, NULL);
2286 gfc_init_se (&rse, NULL);
2288 /* Walk the argument expression. */
2289 rss = gfc_walk_expr (expr);
2291 gcc_assert (rss != gfc_ss_terminator);
2293 /* Initialize the scalarizer. */
2294 gfc_init_loopinfo (&loop);
2295 gfc_add_ss_to_loop (&loop, rss);
2297 /* Calculate the bounds of the scalarization. */
2298 gfc_conv_ss_startstride (&loop);
2300 /* Build an ss for the temporary. */
2301 if (expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->backend_decl)
2302 gfc_conv_string_length (expr->ts.u.cl, expr, &parmse->pre);
2304 base_type = gfc_typenode_for_spec (&expr->ts);
2305 if (GFC_ARRAY_TYPE_P (base_type)
2306 || GFC_DESCRIPTOR_TYPE_P (base_type))
2307 base_type = gfc_get_element_type (base_type);
2309 loop.temp_ss = gfc_get_ss ();;
2310 loop.temp_ss->type = GFC_SS_TEMP;
2311 loop.temp_ss->data.temp.type = base_type;
2313 if (expr->ts.type == BT_CHARACTER)
2314 loop.temp_ss->string_length = expr->ts.u.cl->backend_decl;
2316 loop.temp_ss->string_length = NULL;
2318 parmse->string_length = loop.temp_ss->string_length;
2319 loop.temp_ss->data.temp.dimen = loop.dimen;
2320 loop.temp_ss->next = gfc_ss_terminator;
2322 /* Associate the SS with the loop. */
2323 gfc_add_ss_to_loop (&loop, loop.temp_ss);
2325 /* Setup the scalarizing loops. */
2326 gfc_conv_loop_setup (&loop, &expr->where);
2328 /* Pass the temporary descriptor back to the caller. */
2329 info = &loop.temp_ss->data.info;
2330 parmse->expr = info->descriptor;
2332 /* Setup the gfc_se structures. */
2333 gfc_copy_loopinfo_to_se (&lse, &loop);
2334 gfc_copy_loopinfo_to_se (&rse, &loop);
2337 lse.ss = loop.temp_ss;
2338 gfc_mark_ss_chain_used (rss, 1);
2339 gfc_mark_ss_chain_used (loop.temp_ss, 1);
2341 /* Start the scalarized loop body. */
2342 gfc_start_scalarized_body (&loop, &body);
2344 /* Translate the expression. */
2345 gfc_conv_expr (&rse, expr);
2347 gfc_conv_tmp_array_ref (&lse);
2348 gfc_advance_se_ss_chain (&lse);
2350 if (intent != INTENT_OUT)
2352 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, true, false, true);
2353 gfc_add_expr_to_block (&body, tmp);
2354 gcc_assert (rse.ss == gfc_ss_terminator);
2355 gfc_trans_scalarizing_loops (&loop, &body);
2359 /* Make sure that the temporary declaration survives by merging
2360 all the loop declarations into the current context. */
2361 for (n = 0; n < loop.dimen; n++)
2363 gfc_merge_block_scope (&body);
2364 body = loop.code[loop.order[n]];
2366 gfc_merge_block_scope (&body);
2369 /* Add the post block after the second loop, so that any
2370 freeing of allocated memory is done at the right time. */
2371 gfc_add_block_to_block (&parmse->pre, &loop.pre);
2373 /**********Copy the temporary back again.*********/
2375 gfc_init_se (&lse, NULL);
2376 gfc_init_se (&rse, NULL);
2378 /* Walk the argument expression. */
2379 lss = gfc_walk_expr (expr);
2380 rse.ss = loop.temp_ss;
2383 /* Initialize the scalarizer. */
2384 gfc_init_loopinfo (&loop2);
2385 gfc_add_ss_to_loop (&loop2, lss);
2387 /* Calculate the bounds of the scalarization. */
2388 gfc_conv_ss_startstride (&loop2);
2390 /* Setup the scalarizing loops. */
2391 gfc_conv_loop_setup (&loop2, &expr->where);
2393 gfc_copy_loopinfo_to_se (&lse, &loop2);
2394 gfc_copy_loopinfo_to_se (&rse, &loop2);
2396 gfc_mark_ss_chain_used (lss, 1);
2397 gfc_mark_ss_chain_used (loop.temp_ss, 1);
2399 /* Declare the variable to hold the temporary offset and start the
2400 scalarized loop body. */
2401 offset = gfc_create_var (gfc_array_index_type, NULL);
2402 gfc_start_scalarized_body (&loop2, &body);
2404 /* Build the offsets for the temporary from the loop variables. The
2405 temporary array has lbounds of zero and strides of one in all
2406 dimensions, so this is very simple. The offset is only computed
2407 outside the innermost loop, so the overall transfer could be
2408 optimized further. */
2409 info = &rse.ss->data.info;
2410 dimen = info->dimen;
2412 tmp_index = gfc_index_zero_node;
2413 for (n = dimen - 1; n > 0; n--)
2416 tmp = rse.loop->loopvar[n];
2417 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
2418 tmp, rse.loop->from[n]);
2419 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
2422 tmp_str = fold_build2_loc (input_location, MINUS_EXPR,
2423 gfc_array_index_type,
2424 rse.loop->to[n-1], rse.loop->from[n-1]);
2425 tmp_str = fold_build2_loc (input_location, PLUS_EXPR,
2426 gfc_array_index_type,
2427 tmp_str, gfc_index_one_node);
2429 tmp_index = fold_build2_loc (input_location, MULT_EXPR,
2430 gfc_array_index_type, tmp, tmp_str);
2433 tmp_index = fold_build2_loc (input_location, MINUS_EXPR,
2434 gfc_array_index_type,
2435 tmp_index, rse.loop->from[0]);
2436 gfc_add_modify (&rse.loop->code[0], offset, tmp_index);
2438 tmp_index = fold_build2_loc (input_location, PLUS_EXPR,
2439 gfc_array_index_type,
2440 rse.loop->loopvar[0], offset);
2442 /* Now use the offset for the reference. */
2443 tmp = build_fold_indirect_ref_loc (input_location,
2445 rse.expr = gfc_build_array_ref (tmp, tmp_index, NULL);
2447 if (expr->ts.type == BT_CHARACTER)
2448 rse.string_length = expr->ts.u.cl->backend_decl;
2450 gfc_conv_expr (&lse, expr);
2452 gcc_assert (lse.ss == gfc_ss_terminator);
2454 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, false, true);
2455 gfc_add_expr_to_block (&body, tmp);
2457 /* Generate the copying loops. */
2458 gfc_trans_scalarizing_loops (&loop2, &body);
2460 /* Wrap the whole thing up by adding the second loop to the post-block
2461 and following it by the post-block of the first loop. In this way,
2462 if the temporary needs freeing, it is done after use! */
2463 if (intent != INTENT_IN)
2465 gfc_add_block_to_block (&parmse->post, &loop2.pre);
2466 gfc_add_block_to_block (&parmse->post, &loop2.post);
2469 gfc_add_block_to_block (&parmse->post, &loop.post);
2471 gfc_cleanup_loop (&loop);
2472 gfc_cleanup_loop (&loop2);
2474 /* Pass the string length to the argument expression. */
2475 if (expr->ts.type == BT_CHARACTER)
2476 parmse->string_length = expr->ts.u.cl->backend_decl;
2478 /* Determine the offset for pointer formal arguments and set the
2482 size = gfc_index_one_node;
2483 offset = gfc_index_zero_node;
2484 for (n = 0; n < dimen; n++)
2486 tmp = gfc_conv_descriptor_ubound_get (parmse->expr,
2488 tmp = fold_build2_loc (input_location, PLUS_EXPR,
2489 gfc_array_index_type, tmp,
2490 gfc_index_one_node);
2491 gfc_conv_descriptor_ubound_set (&parmse->pre,
2495 gfc_conv_descriptor_lbound_set (&parmse->pre,
2498 gfc_index_one_node);
2499 size = gfc_evaluate_now (size, &parmse->pre);
2500 offset = fold_build2_loc (input_location, MINUS_EXPR,
2501 gfc_array_index_type,
2503 offset = gfc_evaluate_now (offset, &parmse->pre);
2504 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2505 gfc_array_index_type,
2506 rse.loop->to[n], rse.loop->from[n]);
2507 tmp = fold_build2_loc (input_location, PLUS_EXPR,
2508 gfc_array_index_type,
2509 tmp, gfc_index_one_node);
2510 size = fold_build2_loc (input_location, MULT_EXPR,
2511 gfc_array_index_type, size, tmp);
2514 gfc_conv_descriptor_offset_set (&parmse->pre, parmse->expr,
2518 /* We want either the address for the data or the address of the descriptor,
2519 depending on the mode of passing array arguments. */
2521 parmse->expr = gfc_conv_descriptor_data_get (parmse->expr);
2523 parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
2529 /* Generate the code for argument list functions. */
2532 conv_arglist_function (gfc_se *se, gfc_expr *expr, const char *name)
2534 /* Pass by value for g77 %VAL(arg), pass the address
2535 indirectly for %LOC, else by reference. Thus %REF
2536 is a "do-nothing" and %LOC is the same as an F95
2538 if (strncmp (name, "%VAL", 4) == 0)
2539 gfc_conv_expr (se, expr);
2540 else if (strncmp (name, "%LOC", 4) == 0)
2542 gfc_conv_expr_reference (se, expr);
2543 se->expr = gfc_build_addr_expr (NULL, se->expr);
2545 else if (strncmp (name, "%REF", 4) == 0)
2546 gfc_conv_expr_reference (se, expr);
2548 gfc_error ("Unknown argument list function at %L", &expr->where);
2552 /* Takes a derived type expression and returns the address of a temporary
2553 class object of the 'declared' type. */
2555 gfc_conv_derived_to_class (gfc_se *parmse, gfc_expr *e,
2556 gfc_typespec class_ts)
2560 gfc_symbol *declared = class_ts.u.derived;
2566 /* The derived type needs to be converted to a temporary
2568 tmp = gfc_typenode_for_spec (&class_ts);
2569 var = gfc_create_var (tmp, "class");
2572 cmp = gfc_find_component (declared, "$vptr", true, true);
2573 ctree = fold_build3_loc (input_location, COMPONENT_REF,
2574 TREE_TYPE (cmp->backend_decl),
2575 var, cmp->backend_decl, NULL_TREE);
2577 /* Remember the vtab corresponds to the derived type
2578 not to the class declared type. */
2579 vtab = gfc_find_derived_vtab (e->ts.u.derived);
2581 tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
2582 gfc_add_modify (&parmse->pre, ctree,
2583 fold_convert (TREE_TYPE (ctree), tmp));
2585 /* Now set the data field. */
2586 cmp = gfc_find_component (declared, "$data", true, true);
2587 ctree = fold_build3_loc (input_location, COMPONENT_REF,
2588 TREE_TYPE (cmp->backend_decl),
2589 var, cmp->backend_decl, NULL_TREE);
2590 ss = gfc_walk_expr (e);
2591 if (ss == gfc_ss_terminator)
2594 gfc_conv_expr_reference (parmse, e);
2595 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
2596 gfc_add_modify (&parmse->pre, ctree, tmp);
2601 gfc_conv_expr (parmse, e);
2602 gfc_add_modify (&parmse->pre, ctree, parmse->expr);
2605 /* Pass the address of the class object. */
2606 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
2610 /* The following routine generates code for the intrinsic
2611 procedures from the ISO_C_BINDING module:
2613 * C_FUNLOC (function)
2614 * C_F_POINTER (subroutine)
2615 * C_F_PROCPOINTER (subroutine)
2616 * C_ASSOCIATED (function)
2617 One exception which is not handled here is C_F_POINTER with non-scalar
2618 arguments. Returns 1 if the call was replaced by inline code (else: 0). */
2621 conv_isocbinding_procedure (gfc_se * se, gfc_symbol * sym,
2622 gfc_actual_arglist * arg)
2627 if (sym->intmod_sym_id == ISOCBINDING_LOC)
2629 if (arg->expr->rank == 0)
2630 gfc_conv_expr_reference (se, arg->expr);
2634 /* This is really the actual arg because no formal arglist is
2635 created for C_LOC. */
2636 fsym = arg->expr->symtree->n.sym;
2638 /* We should want it to do g77 calling convention. */
2640 && !(fsym->attr.pointer || fsym->attr.allocatable)
2641 && fsym->as->type != AS_ASSUMED_SHAPE;
2642 f = f || !sym->attr.always_explicit;
2644 argss = gfc_walk_expr (arg->expr);
2645 gfc_conv_array_parameter (se, arg->expr, argss, f,
2649 /* TODO -- the following two lines shouldn't be necessary, but if
2650 they're removed, a bug is exposed later in the code path.
2651 This workaround was thus introduced, but will have to be
2652 removed; please see PR 35150 for details about the issue. */
2653 se->expr = convert (pvoid_type_node, se->expr);
2654 se->expr = gfc_evaluate_now (se->expr, &se->pre);
2658 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2660 arg->expr->ts.type = sym->ts.u.derived->ts.type;
2661 arg->expr->ts.f90_type = sym->ts.u.derived->ts.f90_type;
2662 arg->expr->ts.kind = sym->ts.u.derived->ts.kind;
2663 gfc_conv_expr_reference (se, arg->expr);
2667 else if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER
2668 && arg->next->expr->rank == 0)
2669 || sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER)
2671 /* Convert c_f_pointer if fptr is a scalar
2672 and convert c_f_procpointer. */
2676 gfc_init_se (&cptrse, NULL);
2677 gfc_conv_expr (&cptrse, arg->expr);
2678 gfc_add_block_to_block (&se->pre, &cptrse.pre);
2679 gfc_add_block_to_block (&se->post, &cptrse.post);
2681 gfc_init_se (&fptrse, NULL);
2682 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER
2683 || gfc_is_proc_ptr_comp (arg->next->expr, NULL))
2684 fptrse.want_pointer = 1;
2686 gfc_conv_expr (&fptrse, arg->next->expr);
2687 gfc_add_block_to_block (&se->pre, &fptrse.pre);
2688 gfc_add_block_to_block (&se->post, &fptrse.post);
2690 if (arg->next->expr->symtree->n.sym->attr.proc_pointer
2691 && arg->next->expr->symtree->n.sym->attr.dummy)
2692 fptrse.expr = build_fold_indirect_ref_loc (input_location,
2695 se->expr = fold_build2_loc (input_location, MODIFY_EXPR,
2696 TREE_TYPE (fptrse.expr),
2698 fold_convert (TREE_TYPE (fptrse.expr),
2703 else if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2708 /* Build the addr_expr for the first argument. The argument is
2709 already an *address* so we don't need to set want_pointer in
2711 gfc_init_se (&arg1se, NULL);
2712 gfc_conv_expr (&arg1se, arg->expr);
2713 gfc_add_block_to_block (&se->pre, &arg1se.pre);
2714 gfc_add_block_to_block (&se->post, &arg1se.post);
2716 /* See if we were given two arguments. */
2717 if (arg->next == NULL)
2718 /* Only given one arg so generate a null and do a
2719 not-equal comparison against the first arg. */
2720 se->expr = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
2722 fold_convert (TREE_TYPE (arg1se.expr),
2723 null_pointer_node));
2729 /* Given two arguments so build the arg2se from second arg. */
2730 gfc_init_se (&arg2se, NULL);
2731 gfc_conv_expr (&arg2se, arg->next->expr);
2732 gfc_add_block_to_block (&se->pre, &arg2se.pre);
2733 gfc_add_block_to_block (&se->post, &arg2se.post);
2735 /* Generate test to compare that the two args are equal. */
2736 eq_expr = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2737 arg1se.expr, arg2se.expr);
2738 /* Generate test to ensure that the first arg is not null. */
2739 not_null_expr = fold_build2_loc (input_location, NE_EXPR,
2741 arg1se.expr, null_pointer_node);
2743 /* Finally, the generated test must check that both arg1 is not
2744 NULL and that it is equal to the second arg. */
2745 se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
2747 not_null_expr, eq_expr);
2753 /* Nothing was done. */
2757 /* Generate code for a procedure call. Note can return se->post != NULL.
2758 If se->direct_byref is set then se->expr contains the return parameter.
2759 Return nonzero, if the call has alternate specifiers.
2760 'expr' is only needed for procedure pointer components. */
2763 gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
2764 gfc_actual_arglist * arg, gfc_expr * expr,
2765 VEC(tree,gc) *append_args)
2767 gfc_interface_mapping mapping;
2768 VEC(tree,gc) *arglist;
2769 VEC(tree,gc) *retargs;
2780 VEC(tree,gc) *stringargs;
2782 gfc_formal_arglist *formal;
2783 int has_alternate_specifier = 0;
2784 bool need_interface_mapping;
2791 enum {MISSING = 0, ELEMENTAL, SCALAR, SCALAR_POINTER, ARRAY};
2792 gfc_component *comp = NULL;
2802 if (sym->from_intmod == INTMOD_ISO_C_BINDING
2803 && conv_isocbinding_procedure (se, sym, arg))
2806 gfc_is_proc_ptr_comp (expr, &comp);
2810 if (!sym->attr.elemental)
2812 gcc_assert (se->ss->type == GFC_SS_FUNCTION);
2813 if (se->ss->useflags)
2815 gcc_assert ((!comp && gfc_return_by_reference (sym)
2816 && sym->result->attr.dimension)
2817 || (comp && comp->attr.dimension));
2818 gcc_assert (se->loop != NULL);
2820 /* Access the previously obtained result. */
2821 gfc_conv_tmp_array_ref (se);
2822 gfc_advance_se_ss_chain (se);
2826 info = &se->ss->data.info;
2831 gfc_init_block (&post);
2832 gfc_init_interface_mapping (&mapping);
2835 formal = sym->formal;
2836 need_interface_mapping = sym->attr.dimension ||
2837 (sym->ts.type == BT_CHARACTER
2838 && sym->ts.u.cl->length
2839 && sym->ts.u.cl->length->expr_type
2844 formal = comp->formal;
2845 need_interface_mapping = comp->attr.dimension ||
2846 (comp->ts.type == BT_CHARACTER
2847 && comp->ts.u.cl->length
2848 && comp->ts.u.cl->length->expr_type
2852 /* Evaluate the arguments. */
2853 for (; arg != NULL; arg = arg->next, formal = formal ? formal->next : NULL)
2856 fsym = formal ? formal->sym : NULL;
2857 parm_kind = MISSING;
2861 if (se->ignore_optional)
2863 /* Some intrinsics have already been resolved to the correct
2867 else if (arg->label)
2869 has_alternate_specifier = 1;
2874 /* Pass a NULL pointer for an absent arg. */
2875 gfc_init_se (&parmse, NULL);
2876 parmse.expr = null_pointer_node;
2877 if (arg->missing_arg_type == BT_CHARACTER)
2878 parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
2881 else if (arg->expr->expr_type == EXPR_NULL && fsym && !fsym->attr.pointer)
2883 /* Pass a NULL pointer to denote an absent arg. */
2884 gcc_assert (fsym->attr.optional && !fsym->attr.allocatable);
2885 gfc_init_se (&parmse, NULL);
2886 parmse.expr = null_pointer_node;
2887 if (arg->missing_arg_type == BT_CHARACTER)
2888 parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
2890 else if (fsym && fsym->ts.type == BT_CLASS
2891 && e->ts.type == BT_DERIVED)
2893 /* The derived type needs to be converted to a temporary
2895 gfc_init_se (&parmse, se);
2896 gfc_conv_derived_to_class (&parmse, e, fsym->ts);
2898 else if (se->ss && se->ss->useflags)
2900 /* An elemental function inside a scalarized loop. */
2901 gfc_init_se (&parmse, se);
2902 gfc_conv_expr_reference (&parmse, e);
2903 parm_kind = ELEMENTAL;
2907 /* A scalar or transformational function. */
2908 gfc_init_se (&parmse, NULL);
2909 argss = gfc_walk_expr (e);
2911 if (argss == gfc_ss_terminator)
2913 if (e->expr_type == EXPR_VARIABLE
2914 && e->symtree->n.sym->attr.cray_pointee
2915 && fsym && fsym->attr.flavor == FL_PROCEDURE)
2917 /* The Cray pointer needs to be converted to a pointer to
2918 a type given by the expression. */
2919 gfc_conv_expr (&parmse, e);
2920 type = build_pointer_type (TREE_TYPE (parmse.expr));
2921 tmp = gfc_get_symbol_decl (e->symtree->n.sym->cp_pointer);
2922 parmse.expr = convert (type, tmp);
2924 else if (fsym && fsym->attr.value)
2926 if (fsym->ts.type == BT_CHARACTER
2927 && fsym->ts.is_c_interop
2928 && fsym->ns->proc_name != NULL
2929 && fsym->ns->proc_name->attr.is_bind_c)
2932 gfc_conv_scalar_char_value (fsym, &parmse, &e);
2933 if (parmse.expr == NULL)
2934 gfc_conv_expr (&parmse, e);
2937 gfc_conv_expr (&parmse, e);
2939 else if (arg->name && arg->name[0] == '%')
2940 /* Argument list functions %VAL, %LOC and %REF are signalled
2941 through arg->name. */
2942 conv_arglist_function (&parmse, arg->expr, arg->name);
2943 else if ((e->expr_type == EXPR_FUNCTION)
2944 && ((e->value.function.esym
2945 && e->value.function.esym->result->attr.pointer)
2946 || (!e->value.function.esym
2947 && e->symtree->n.sym->attr.pointer))
2948 && fsym && fsym->attr.target)
2950 gfc_conv_expr (&parmse, e);
2951 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
2953 else if (e->expr_type == EXPR_FUNCTION
2954 && e->symtree->n.sym->result
2955 && e->symtree->n.sym->result != e->symtree->n.sym
2956 && e->symtree->n.sym->result->attr.proc_pointer)
2958 /* Functions returning procedure pointers. */
2959 gfc_conv_expr (&parmse, e);
2960 if (fsym && fsym->attr.proc_pointer)
2961 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
2965 gfc_conv_expr_reference (&parmse, e);
2967 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
2968 allocated on entry, it must be deallocated. */
2969 if (fsym && fsym->attr.allocatable
2970 && fsym->attr.intent == INTENT_OUT)
2974 gfc_init_block (&block);
2975 tmp = gfc_deallocate_with_status (parmse.expr, NULL_TREE,
2977 gfc_add_expr_to_block (&block, tmp);
2978 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
2979 void_type_node, parmse.expr,
2981 gfc_add_expr_to_block (&block, tmp);
2983 if (fsym->attr.optional
2984 && e->expr_type == EXPR_VARIABLE
2985 && e->symtree->n.sym->attr.optional)
2987 tmp = fold_build3_loc (input_location, COND_EXPR,
2989 gfc_conv_expr_present (e->symtree->n.sym),
2990 gfc_finish_block (&block),
2991 build_empty_stmt (input_location));
2994 tmp = gfc_finish_block (&block);
2996 gfc_add_expr_to_block (&se->pre, tmp);
2999 if (fsym && e->expr_type != EXPR_NULL
3000 && ((fsym->attr.pointer
3001 && fsym->attr.flavor != FL_PROCEDURE)
3002 || (fsym->attr.proc_pointer
3003 && !(e->expr_type == EXPR_VARIABLE
3004 && e->symtree->n.sym->attr.dummy))
3005 || (e->expr_type == EXPR_VARIABLE
3006 && gfc_is_proc_ptr_comp (e, NULL))
3007 || fsym->attr.allocatable))
3009 /* Scalar pointer dummy args require an extra level of
3010 indirection. The null pointer already contains
3011 this level of indirection. */
3012 parm_kind = SCALAR_POINTER;
3013 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
3019 /* If the procedure requires an explicit interface, the actual
3020 argument is passed according to the corresponding formal
3021 argument. If the corresponding formal argument is a POINTER,
3022 ALLOCATABLE or assumed shape, we do not use g77's calling
3023 convention, and pass the address of the array descriptor
3024 instead. Otherwise we use g77's calling convention. */
3027 && !(fsym->attr.pointer || fsym->attr.allocatable)
3028 && fsym->as && fsym->as->type != AS_ASSUMED_SHAPE;
3030 f = f || !comp->attr.always_explicit;
3032 f = f || !sym->attr.always_explicit;
3034 if (e->expr_type == EXPR_VARIABLE
3035 && is_subref_array (e))
3036 /* The actual argument is a component reference to an
3037 array of derived types. In this case, the argument
3038 is converted to a temporary, which is passed and then
3039 written back after the procedure call. */
3040 gfc_conv_subref_array_arg (&parmse, e, f,
3041 fsym ? fsym->attr.intent : INTENT_INOUT,
3042 fsym && fsym->attr.pointer);
3044 gfc_conv_array_parameter (&parmse, e, argss, f, fsym,
3047 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
3048 allocated on entry, it must be deallocated. */
3049 if (fsym && fsym->attr.allocatable
3050 && fsym->attr.intent == INTENT_OUT)
3052 tmp = build_fold_indirect_ref_loc (input_location,
3054 tmp = gfc_trans_dealloc_allocated (tmp);
3055 if (fsym->attr.optional
3056 && e->expr_type == EXPR_VARIABLE
3057 && e->symtree->n.sym->attr.optional)
3058 tmp = fold_build3_loc (input_location, COND_EXPR,
3060 gfc_conv_expr_present (e->symtree->n.sym),
3061 tmp, build_empty_stmt (input_location));
3062 gfc_add_expr_to_block (&se->pre, tmp);
3067 /* The case with fsym->attr.optional is that of a user subroutine
3068 with an interface indicating an optional argument. When we call
3069 an intrinsic subroutine, however, fsym is NULL, but we might still
3070 have an optional argument, so we proceed to the substitution
3072 if (e && (fsym == NULL || fsym->attr.optional))
3074 /* If an optional argument is itself an optional dummy argument,
3075 check its presence and substitute a null if absent. This is
3076 only needed when passing an array to an elemental procedure
3077 as then array elements are accessed - or no NULL pointer is
3078 allowed and a "1" or "0" should be passed if not present.
3079 When passing a non-array-descriptor full array to a
3080 non-array-descriptor dummy, no check is needed. For
3081 array-descriptor actual to array-descriptor dummy, see
3082 PR 41911 for why a check has to be inserted.
3083 fsym == NULL is checked as intrinsics required the descriptor
3084 but do not always set fsym. */
3085 if (e->expr_type == EXPR_VARIABLE
3086 && e->symtree->n.sym->attr.optional
3087 && ((e->rank > 0 && sym->attr.elemental)
3088 || e->representation.length || e->ts.type == BT_CHARACTER
3090 && (fsym == NULL || fsym->as->type == AS_ASSUMED_SHAPE
3091 || fsym->as->type == AS_DEFERRED))))
3092 gfc_conv_missing_dummy (&parmse, e, fsym ? fsym->ts : e->ts,
3093 e->representation.length);
3098 /* Obtain the character length of an assumed character length
3099 length procedure from the typespec. */
3100 if (fsym->ts.type == BT_CHARACTER
3101 && parmse.string_length == NULL_TREE
3102 && e->ts.type == BT_PROCEDURE
3103 && e->symtree->n.sym->ts.type == BT_CHARACTER
3104 && e->symtree->n.sym->ts.u.cl->length != NULL
3105 && e->symtree->n.sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
3107 gfc_conv_const_charlen (e->symtree->n.sym->ts.u.cl);
3108 parmse.string_length = e->symtree->n.sym->ts.u.cl->backend_decl;
3112 if (fsym && need_interface_mapping && e)
3113 gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
3115 gfc_add_block_to_block (&se->pre, &parmse.pre);
3116 gfc_add_block_to_block (&post, &parmse.post);
3118 /* Allocated allocatable components of derived types must be
3119 deallocated for non-variable scalars. Non-variable arrays are
3120 dealt with in trans-array.c(gfc_conv_array_parameter). */
3121 if (e && e->ts.type == BT_DERIVED
3122 && e->ts.u.derived->attr.alloc_comp
3123 && !(e->symtree && e->symtree->n.sym->attr.pointer)
3124 && (e->expr_type != EXPR_VARIABLE && !e->rank))
3127 tmp = build_fold_indirect_ref_loc (input_location,
3129 parm_rank = e->rank;
3137 case (SCALAR_POINTER):
3138 tmp = build_fold_indirect_ref_loc (input_location,
3143 if (e->expr_type == EXPR_OP
3144 && e->value.op.op == INTRINSIC_PARENTHESES
3145 && e->value.op.op1->expr_type == EXPR_VARIABLE)
3148 local_tmp = gfc_evaluate_now (tmp, &se->pre);
3149 local_tmp = gfc_copy_alloc_comp (e->ts.u.derived, local_tmp, tmp, parm_rank);
3150 gfc_add_expr_to_block (&se->post, local_tmp);
3153 tmp = gfc_deallocate_alloc_comp (e->ts.u.derived, tmp, parm_rank);
3155 gfc_add_expr_to_block (&se->post, tmp);
3158 /* Add argument checking of passing an unallocated/NULL actual to
3159 a nonallocatable/nonpointer dummy. */
3161 if (gfc_option.rtcheck & GFC_RTCHECK_POINTER && e != NULL)
3163 symbol_attribute *attr;
3167 if (e->expr_type == EXPR_VARIABLE)
3168 attr = &e->symtree->n.sym->attr;
3169 else if (e->expr_type == EXPR_FUNCTION)
3171 /* For intrinsic functions, the gfc_attr are not available. */
3172 if (e->symtree->n.sym->attr.generic && e->value.function.isym)
3173 goto end_pointer_check;
3175 if (e->symtree->n.sym->attr.generic)
3176 attr = &e->value.function.esym->attr;
3178 attr = &e->symtree->n.sym->result->attr;
3181 goto end_pointer_check;
3185 /* If the actual argument is an optional pointer/allocatable and
3186 the formal argument takes an nonpointer optional value,
3187 it is invalid to pass a non-present argument on, even
3188 though there is no technical reason for this in gfortran.
3189 See Fortran 2003, Section 12.4.1.6 item (7)+(8). */
3190 tree present, null_ptr, type;
3192 if (attr->allocatable
3193 && (fsym == NULL || !fsym->attr.allocatable))
3194 asprintf (&msg, "Allocatable actual argument '%s' is not "
3195 "allocated or not present", e->symtree->n.sym->name);
3196 else if (attr->pointer
3197 && (fsym == NULL || !fsym->attr.pointer))
3198 asprintf (&msg, "Pointer actual argument '%s' is not "
3199 "associated or not present",
3200 e->symtree->n.sym->name);
3201 else if (attr->proc_pointer
3202 && (fsym == NULL || !fsym->attr.proc_pointer))
3203 asprintf (&msg, "Proc-pointer actual argument '%s' is not "
3204 "associated or not present",
3205 e->symtree->n.sym->name);
3207 goto end_pointer_check;
3209 present = gfc_conv_expr_present (e->symtree->n.sym);
3210 type = TREE_TYPE (present);
3211 present = fold_build2_loc (input_location, EQ_EXPR,
3212 boolean_type_node, present,
3214 null_pointer_node));
3215 type = TREE_TYPE (parmse.expr);
3216 null_ptr = fold_build2_loc (input_location, EQ_EXPR,
3217 boolean_type_node, parmse.expr,
3219 null_pointer_node));
3220 cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
3221 boolean_type_node, present, null_ptr);
3225 if (attr->allocatable
3226 && (fsym == NULL || !fsym->attr.allocatable))
3227 asprintf (&msg, "Allocatable actual argument '%s' is not "
3228 "allocated", e->symtree->n.sym->name);
3229 else if (attr->pointer
3230 && (fsym == NULL || !fsym->attr.pointer))
3231 asprintf (&msg, "Pointer actual argument '%s' is not "
3232 "associated", e->symtree->n.sym->name);
3233 else if (attr->proc_pointer
3234 && (fsym == NULL || !fsym->attr.proc_pointer))
3235 asprintf (&msg, "Proc-pointer actual argument '%s' is not "
3236 "associated", e->symtree->n.sym->name);
3238 goto end_pointer_check;
3241 cond = fold_build2_loc (input_location, EQ_EXPR,
3242 boolean_type_node, parmse.expr,
3243 fold_convert (TREE_TYPE (parmse.expr),
3244 null_pointer_node));
3247 gfc_trans_runtime_check (true, false, cond, &se->pre, &e->where,
3254 /* Character strings are passed as two parameters, a length and a
3255 pointer - except for Bind(c) which only passes the pointer. */
3256 if (parmse.string_length != NULL_TREE && !sym->attr.is_bind_c)
3257 VEC_safe_push (tree, gc, stringargs, parmse.string_length);
3259 VEC_safe_push (tree, gc, arglist, parmse.expr);
3261 gfc_finish_interface_mapping (&mapping, &se->pre, &se->post);
3268 if (ts.type == BT_CHARACTER && sym->attr.is_bind_c)
3269 se->string_length = build_int_cst (gfc_charlen_type_node, 1);
3270 else if (ts.type == BT_CHARACTER)
3272 if (ts.u.cl->length == NULL)
3274 /* Assumed character length results are not allowed by 5.1.1.5 of the
3275 standard and are trapped in resolve.c; except in the case of SPREAD
3276 (and other intrinsics?) and dummy functions. In the case of SPREAD,
3277 we take the character length of the first argument for the result.
3278 For dummies, we have to look through the formal argument list for
3279 this function and use the character length found there.*/
3280 if (!sym->attr.dummy)
3281 cl.backend_decl = VEC_index (tree, stringargs, 0);
3284 formal = sym->ns->proc_name->formal;
3285 for (; formal; formal = formal->next)
3286 if (strcmp (formal->sym->name, sym->name) == 0)
3287 cl.backend_decl = formal->sym->ts.u.cl->backend_decl;
3294 /* Calculate the length of the returned string. */
3295 gfc_init_se (&parmse, NULL);
3296 if (need_interface_mapping)
3297 gfc_apply_interface_mapping (&mapping, &parmse, ts.u.cl->length);
3299 gfc_conv_expr (&parmse, ts.u.cl->length);
3300 gfc_add_block_to_block (&se->pre, &parmse.pre);
3301 gfc_add_block_to_block (&se->post, &parmse.post);
3303 tmp = fold_convert (gfc_charlen_type_node, parmse.expr);
3304 tmp = fold_build2_loc (input_location, MAX_EXPR,
3305 gfc_charlen_type_node, tmp,
3306 build_int_cst (gfc_charlen_type_node, 0));
3307 cl.backend_decl = tmp;
3310 /* Set up a charlen structure for it. */
3315 len = cl.backend_decl;
3318 byref = (comp && (comp->attr.dimension || comp->ts.type == BT_CHARACTER))
3319 || (!comp && gfc_return_by_reference (sym));
3322 if (se->direct_byref)
3324 /* Sometimes, too much indirection can be applied; e.g. for
3325 function_result = array_valued_recursive_function. */
3326 if (TREE_TYPE (TREE_TYPE (se->expr))
3327 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))
3328 && GFC_DESCRIPTOR_TYPE_P
3329 (TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))))
3330 se->expr = build_fold_indirect_ref_loc (input_location,
3333 result = build_fold_indirect_ref_loc (input_location,
3335 VEC_safe_push (tree, gc, retargs, se->expr);
3337 else if (comp && comp->attr.dimension)
3339 gcc_assert (se->loop && info);
3341 /* Set the type of the array. */