1 /* Expression translation
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 Free Software Foundation, Inc.
5 Contributed by Paul Brook <paul@nowt.org>
6 and Steven Bosscher <s.bosscher@student.tudelft.nl>
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* trans-expr.c-- generate GENERIC trees for gfc_expr. */
28 #include "coretypes.h"
30 #include "diagnostic-core.h" /* For fatal_error. */
31 #include "langhooks.h"
35 #include "constructor.h"
37 #include "trans-const.h"
38 #include "trans-types.h"
39 #include "trans-array.h"
40 /* Only for gfc_trans_assign and gfc_trans_pointer_assign. */
41 #include "trans-stmt.h"
42 #include "dependency.h"
44 static tree gfc_trans_structure_assign (tree dest, gfc_expr * expr);
45 static void gfc_apply_interface_mapping_to_expr (gfc_interface_mapping *,
48 /* Copy the scalarization loop variables. */
51 gfc_copy_se_loopvars (gfc_se * dest, gfc_se * src)
54 dest->loop = src->loop;
58 /* Initialize a simple expression holder.
60 Care must be taken when multiple se are created with the same parent.
61 The child se must be kept in sync. The easiest way is to delay creation
62 of a child se until after after the previous se has been translated. */
65 gfc_init_se (gfc_se * se, gfc_se * parent)
67 memset (se, 0, sizeof (gfc_se));
68 gfc_init_block (&se->pre);
69 gfc_init_block (&se->post);
74 gfc_copy_se_loopvars (se, parent);
78 /* Advances to the next SS in the chain. Use this rather than setting
79 se->ss = se->ss->next because all the parents needs to be kept in sync.
83 gfc_advance_se_ss_chain (gfc_se * se)
88 gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
91 /* Walk down the parent chain. */
94 /* Simple consistency check. */
95 gcc_assert (p->parent == NULL || p->parent->ss == p->ss);
97 /* If we were in a nested loop, the next scalarized expression can be
98 on the parent ss' next pointer. Thus we should not take the next
99 pointer blindly, but rather go up one nest level as long as next
100 is the end of chain. */
102 while (ss->next == gfc_ss_terminator && ss->parent != NULL)
112 /* Ensures the result of the expression as either a temporary variable
113 or a constant so that it can be used repeatedly. */
116 gfc_make_safe_expr (gfc_se * se)
120 if (CONSTANT_CLASS_P (se->expr))
123 /* We need a temporary for this result. */
124 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
125 gfc_add_modify (&se->pre, var, se->expr);
130 /* Return an expression which determines if a dummy parameter is present.
131 Also used for arguments to procedures with multiple entry points. */
134 gfc_conv_expr_present (gfc_symbol * sym)
138 gcc_assert (sym->attr.dummy);
140 decl = gfc_get_symbol_decl (sym);
141 if (TREE_CODE (decl) != PARM_DECL)
143 /* Array parameters use a temporary descriptor, we want the real
145 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
146 || GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
147 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
150 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, decl,
151 fold_convert (TREE_TYPE (decl), null_pointer_node));
153 /* Fortran 2008 allows to pass null pointers and non-associated pointers
154 as actual argument to denote absent dummies. For array descriptors,
155 we thus also need to check the array descriptor. */
156 if (!sym->attr.pointer && !sym->attr.allocatable
157 && sym->as && sym->as->type == AS_ASSUMED_SHAPE
158 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
161 tmp = build_fold_indirect_ref_loc (input_location, decl);
162 tmp = gfc_conv_array_data (tmp);
163 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, tmp,
164 fold_convert (TREE_TYPE (tmp), null_pointer_node));
165 cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
166 boolean_type_node, cond, tmp);
173 /* Converts a missing, dummy argument into a null or zero. */
176 gfc_conv_missing_dummy (gfc_se * se, gfc_expr * arg, gfc_typespec ts, int kind)
181 present = gfc_conv_expr_present (arg->symtree->n.sym);
185 /* Create a temporary and convert it to the correct type. */
186 tmp = gfc_get_int_type (kind);
187 tmp = fold_convert (tmp, build_fold_indirect_ref_loc (input_location,
190 /* Test for a NULL value. */
191 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), present,
192 tmp, fold_convert (TREE_TYPE (tmp), integer_one_node));
193 tmp = gfc_evaluate_now (tmp, &se->pre);
194 se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
198 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se->expr),
200 build_zero_cst (TREE_TYPE (se->expr)));
201 tmp = gfc_evaluate_now (tmp, &se->pre);
205 if (ts.type == BT_CHARACTER)
207 tmp = build_int_cst (gfc_charlen_type_node, 0);
208 tmp = fold_build3_loc (input_location, COND_EXPR, gfc_charlen_type_node,
209 present, se->string_length, tmp);
210 tmp = gfc_evaluate_now (tmp, &se->pre);
211 se->string_length = tmp;
217 /* Get the character length of an expression, looking through gfc_refs
221 gfc_get_expr_charlen (gfc_expr *e)
226 gcc_assert (e->expr_type == EXPR_VARIABLE
227 && e->ts.type == BT_CHARACTER);
229 length = NULL; /* To silence compiler warning. */
231 if (is_subref_array (e) && e->ts.u.cl->length)
234 gfc_init_se (&tmpse, NULL);
235 gfc_conv_expr_type (&tmpse, e->ts.u.cl->length, gfc_charlen_type_node);
236 e->ts.u.cl->backend_decl = tmpse.expr;
240 /* First candidate: if the variable is of type CHARACTER, the
241 expression's length could be the length of the character
243 if (e->symtree->n.sym->ts.type == BT_CHARACTER)
244 length = e->symtree->n.sym->ts.u.cl->backend_decl;
246 /* Look through the reference chain for component references. */
247 for (r = e->ref; r; r = r->next)
252 if (r->u.c.component->ts.type == BT_CHARACTER)
253 length = r->u.c.component->ts.u.cl->backend_decl;
261 /* We should never got substring references here. These will be
262 broken down by the scalarizer. */
268 gcc_assert (length != NULL);
273 /* Return for an expression the backend decl of the coarray. */
276 get_tree_for_caf_expr (gfc_expr *expr)
278 tree caf_decl = NULL_TREE;
281 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE);
282 if (expr->symtree->n.sym->attr.codimension)
283 caf_decl = expr->symtree->n.sym->backend_decl;
285 for (ref = expr->ref; ref; ref = ref->next)
286 if (ref->type == REF_COMPONENT)
288 gfc_component *comp = ref->u.c.component;
289 if (comp->attr.pointer || comp->attr.allocatable)
290 caf_decl = NULL_TREE;
291 if (comp->attr.codimension)
292 caf_decl = comp->backend_decl;
295 gcc_assert (caf_decl != NULL_TREE);
300 /* For each character array constructor subexpression without a ts.u.cl->length,
301 replace it by its first element (if there aren't any elements, the length
302 should already be set to zero). */
305 flatten_array_ctors_without_strlen (gfc_expr* e)
307 gfc_actual_arglist* arg;
313 switch (e->expr_type)
317 flatten_array_ctors_without_strlen (e->value.op.op1);
318 flatten_array_ctors_without_strlen (e->value.op.op2);
322 /* TODO: Implement as with EXPR_FUNCTION when needed. */
326 for (arg = e->value.function.actual; arg; arg = arg->next)
327 flatten_array_ctors_without_strlen (arg->expr);
332 /* We've found what we're looking for. */
333 if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
338 gcc_assert (e->value.constructor);
340 c = gfc_constructor_first (e->value.constructor);
344 flatten_array_ctors_without_strlen (new_expr);
345 gfc_replace_expr (e, new_expr);
349 /* Otherwise, fall through to handle constructor elements. */
351 for (c = gfc_constructor_first (e->value.constructor);
352 c; c = gfc_constructor_next (c))
353 flatten_array_ctors_without_strlen (c->expr);
363 /* Generate code to initialize a string length variable. Returns the
364 value. For array constructors, cl->length might be NULL and in this case,
365 the first element of the constructor is needed. expr is the original
366 expression so we can access it but can be NULL if this is not needed. */
369 gfc_conv_string_length (gfc_charlen * cl, gfc_expr * expr, stmtblock_t * pblock)
373 gfc_init_se (&se, NULL);
377 && TREE_CODE (cl->backend_decl) == VAR_DECL)
380 /* If cl->length is NULL, use gfc_conv_expr to obtain the string length but
381 "flatten" array constructors by taking their first element; all elements
382 should be the same length or a cl->length should be present. */
387 expr_flat = gfc_copy_expr (expr);
388 flatten_array_ctors_without_strlen (expr_flat);
389 gfc_resolve_expr (expr_flat);
391 gfc_conv_expr (&se, expr_flat);
392 gfc_add_block_to_block (pblock, &se.pre);
393 cl->backend_decl = convert (gfc_charlen_type_node, se.string_length);
395 gfc_free_expr (expr_flat);
399 /* Convert cl->length. */
401 gcc_assert (cl->length);
403 gfc_conv_expr_type (&se, cl->length, gfc_charlen_type_node);
404 se.expr = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
405 se.expr, build_int_cst (gfc_charlen_type_node, 0));
406 gfc_add_block_to_block (pblock, &se.pre);
408 if (cl->backend_decl)
409 gfc_add_modify (pblock, cl->backend_decl, se.expr);
411 cl->backend_decl = gfc_evaluate_now (se.expr, pblock);
416 gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
417 const char *name, locus *where)
426 type = gfc_get_character_type (kind, ref->u.ss.length);
427 type = build_pointer_type (type);
429 gfc_init_se (&start, se);
430 gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
431 gfc_add_block_to_block (&se->pre, &start.pre);
433 if (integer_onep (start.expr))
434 gfc_conv_string_parameter (se);
439 /* Avoid multiple evaluation of substring start. */
440 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
441 start.expr = gfc_evaluate_now (start.expr, &se->pre);
443 /* Change the start of the string. */
444 if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
447 tmp = build_fold_indirect_ref_loc (input_location,
449 tmp = gfc_build_array_ref (tmp, start.expr, NULL);
450 se->expr = gfc_build_addr_expr (type, tmp);
453 /* Length = end + 1 - start. */
454 gfc_init_se (&end, se);
455 if (ref->u.ss.end == NULL)
456 end.expr = se->string_length;
459 gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
460 gfc_add_block_to_block (&se->pre, &end.pre);
464 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
465 end.expr = gfc_evaluate_now (end.expr, &se->pre);
467 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
469 tree nonempty = fold_build2_loc (input_location, LE_EXPR,
470 boolean_type_node, start.expr,
473 /* Check lower bound. */
474 fault = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
476 build_int_cst (gfc_charlen_type_node, 1));
477 fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
478 boolean_type_node, nonempty, fault);
480 asprintf (&msg, "Substring out of bounds: lower bound (%%ld) of '%s' "
481 "is less than one", name);
483 asprintf (&msg, "Substring out of bounds: lower bound (%%ld)"
485 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
486 fold_convert (long_integer_type_node,
490 /* Check upper bound. */
491 fault = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
492 end.expr, se->string_length);
493 fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
494 boolean_type_node, nonempty, fault);
496 asprintf (&msg, "Substring out of bounds: upper bound (%%ld) of '%s' "
497 "exceeds string length (%%ld)", name);
499 asprintf (&msg, "Substring out of bounds: upper bound (%%ld) "
500 "exceeds string length (%%ld)");
501 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
502 fold_convert (long_integer_type_node, end.expr),
503 fold_convert (long_integer_type_node,
508 /* If the start and end expressions are equal, the length is one. */
510 && gfc_dep_compare_expr (ref->u.ss.start, ref->u.ss.end) == 0)
511 tmp = build_int_cst (gfc_charlen_type_node, 1);
514 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_charlen_type_node,
515 end.expr, start.expr);
516 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_charlen_type_node,
517 build_int_cst (gfc_charlen_type_node, 1), tmp);
518 tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
519 tmp, build_int_cst (gfc_charlen_type_node, 0));
522 se->string_length = tmp;
526 /* Convert a derived type component reference. */
529 gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
536 c = ref->u.c.component;
538 gcc_assert (c->backend_decl);
540 field = c->backend_decl;
541 gcc_assert (TREE_CODE (field) == FIELD_DECL);
544 /* Components can correspond to fields of different containing
545 types, as components are created without context, whereas
546 a concrete use of a component has the type of decl as context.
547 So, if the type doesn't match, we search the corresponding
548 FIELD_DECL in the parent type. To not waste too much time
549 we cache this result in norestrict_decl. */
551 if (DECL_FIELD_CONTEXT (field) != TREE_TYPE (decl))
553 tree f2 = c->norestrict_decl;
554 if (!f2 || DECL_FIELD_CONTEXT (f2) != TREE_TYPE (decl))
555 for (f2 = TYPE_FIELDS (TREE_TYPE (decl)); f2; f2 = DECL_CHAIN (f2))
556 if (TREE_CODE (f2) == FIELD_DECL
557 && DECL_NAME (f2) == DECL_NAME (field))
560 c->norestrict_decl = f2;
563 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
564 decl, field, NULL_TREE);
568 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer)
570 tmp = c->ts.u.cl->backend_decl;
571 /* Components must always be constant length. */
572 gcc_assert (tmp && INTEGER_CST_P (tmp));
573 se->string_length = tmp;
576 if (((c->attr.pointer || c->attr.allocatable)
577 && (!c->attr.dimension && !c->attr.codimension)
578 && c->ts.type != BT_CHARACTER)
579 || c->attr.proc_pointer)
580 se->expr = build_fold_indirect_ref_loc (input_location,
585 /* This function deals with component references to components of the
586 parent type for derived type extensons. */
588 conv_parent_component_references (gfc_se * se, gfc_ref * ref)
596 c = ref->u.c.component;
598 /* Return if the component is not in the parent type. */
599 for (cmp = dt->components; cmp; cmp = cmp->next)
600 if (strcmp (c->name, cmp->name) == 0)
603 /* Build a gfc_ref to recursively call gfc_conv_component_ref. */
604 parent.type = REF_COMPONENT;
607 parent.u.c.component = dt->components;
609 if (dt->backend_decl == NULL)
610 gfc_get_derived_type (dt);
612 /* Build the reference and call self. */
613 gfc_conv_component_ref (se, &parent);
614 parent.u.c.sym = dt->components->ts.u.derived;
615 parent.u.c.component = c;
616 conv_parent_component_references (se, &parent);
619 /* Return the contents of a variable. Also handles reference/pointer
620 variables (all Fortran pointer references are implicit). */
623 gfc_conv_variable (gfc_se * se, gfc_expr * expr)
628 tree parent_decl = NULL_TREE;
631 bool alternate_entry;
634 sym = expr->symtree->n.sym;
638 gfc_ss_info *ss_info = ss->info;
640 /* Check that something hasn't gone horribly wrong. */
641 gcc_assert (ss != gfc_ss_terminator);
642 gcc_assert (ss_info->expr == expr);
644 /* A scalarized term. We already know the descriptor. */
645 se->expr = ss_info->data.array.descriptor;
646 se->string_length = ss_info->string_length;
647 for (ref = ss_info->data.array.ref; ref; ref = ref->next)
648 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
653 tree se_expr = NULL_TREE;
655 se->expr = gfc_get_symbol_decl (sym);
657 /* Deal with references to a parent results or entries by storing
658 the current_function_decl and moving to the parent_decl. */
659 return_value = sym->attr.function && sym->result == sym;
660 alternate_entry = sym->attr.function && sym->attr.entry
661 && sym->result == sym;
662 entry_master = sym->attr.result
663 && sym->ns->proc_name->attr.entry_master
664 && !gfc_return_by_reference (sym->ns->proc_name);
665 if (current_function_decl)
666 parent_decl = DECL_CONTEXT (current_function_decl);
668 if ((se->expr == parent_decl && return_value)
669 || (sym->ns && sym->ns->proc_name
671 && sym->ns->proc_name->backend_decl == parent_decl
672 && (alternate_entry || entry_master)))
677 /* Special case for assigning the return value of a function.
678 Self recursive functions must have an explicit return value. */
679 if (return_value && (se->expr == current_function_decl || parent_flag))
680 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
682 /* Similarly for alternate entry points. */
683 else if (alternate_entry
684 && (sym->ns->proc_name->backend_decl == current_function_decl
687 gfc_entry_list *el = NULL;
689 for (el = sym->ns->entries; el; el = el->next)
692 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
697 else if (entry_master
698 && (sym->ns->proc_name->backend_decl == current_function_decl
700 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
705 /* Procedure actual arguments. */
706 else if (sym->attr.flavor == FL_PROCEDURE
707 && se->expr != current_function_decl)
709 if (!sym->attr.dummy && !sym->attr.proc_pointer)
711 gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
712 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
718 /* Dereference the expression, where needed. Since characters
719 are entirely different from other types, they are treated
721 if (sym->ts.type == BT_CHARACTER)
723 /* Dereference character pointer dummy arguments
725 if ((sym->attr.pointer || sym->attr.allocatable)
727 || sym->attr.function
728 || sym->attr.result))
729 se->expr = build_fold_indirect_ref_loc (input_location,
733 else if (!sym->attr.value)
735 /* Dereference non-character scalar dummy arguments. */
736 if (sym->attr.dummy && !sym->attr.dimension
737 && !(sym->attr.codimension && sym->attr.allocatable))
738 se->expr = build_fold_indirect_ref_loc (input_location,
741 /* Dereference scalar hidden result. */
742 if (gfc_option.flag_f2c && sym->ts.type == BT_COMPLEX
743 && (sym->attr.function || sym->attr.result)
744 && !sym->attr.dimension && !sym->attr.pointer
745 && !sym->attr.always_explicit)
746 se->expr = build_fold_indirect_ref_loc (input_location,
749 /* Dereference non-character pointer variables.
750 These must be dummies, results, or scalars. */
751 if ((sym->attr.pointer || sym->attr.allocatable
752 || gfc_is_associate_pointer (sym))
754 || sym->attr.function
756 || (!sym->attr.dimension
757 && (!sym->attr.codimension || !sym->attr.allocatable))))
758 se->expr = build_fold_indirect_ref_loc (input_location,
765 /* For character variables, also get the length. */
766 if (sym->ts.type == BT_CHARACTER)
768 /* If the character length of an entry isn't set, get the length from
769 the master function instead. */
770 if (sym->attr.entry && !sym->ts.u.cl->backend_decl)
771 se->string_length = sym->ns->proc_name->ts.u.cl->backend_decl;
773 se->string_length = sym->ts.u.cl->backend_decl;
774 gcc_assert (se->string_length);
782 /* Return the descriptor if that's what we want and this is an array
783 section reference. */
784 if (se->descriptor_only && ref->u.ar.type != AR_ELEMENT)
786 /* TODO: Pointers to single elements of array sections, eg elemental subs. */
787 /* Return the descriptor for array pointers and allocations. */
789 && ref->next == NULL && (se->descriptor_only))
792 gfc_conv_array_ref (se, &ref->u.ar, sym, &expr->where);
793 /* Return a pointer to an element. */
797 if (ref->u.c.sym->attr.extension)
798 conv_parent_component_references (se, ref);
800 gfc_conv_component_ref (se, ref);
804 gfc_conv_substring (se, ref, expr->ts.kind,
805 expr->symtree->name, &expr->where);
814 /* Pointer assignment, allocation or pass by reference. Arrays are handled
816 if (se->want_pointer)
818 if (expr->ts.type == BT_CHARACTER && !gfc_is_proc_ptr_comp (expr, NULL))
819 gfc_conv_string_parameter (se);
821 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
826 /* Unary ops are easy... Or they would be if ! was a valid op. */
829 gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
834 gcc_assert (expr->ts.type != BT_CHARACTER);
835 /* Initialize the operand. */
836 gfc_init_se (&operand, se);
837 gfc_conv_expr_val (&operand, expr->value.op.op1);
838 gfc_add_block_to_block (&se->pre, &operand.pre);
840 type = gfc_typenode_for_spec (&expr->ts);
842 /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
843 We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
844 All other unary operators have an equivalent GIMPLE unary operator. */
845 if (code == TRUTH_NOT_EXPR)
846 se->expr = fold_build2_loc (input_location, EQ_EXPR, type, operand.expr,
847 build_int_cst (type, 0));
849 se->expr = fold_build1_loc (input_location, code, type, operand.expr);
853 /* Expand power operator to optimal multiplications when a value is raised
854 to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
855 Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
856 Programming", 3rd Edition, 1998. */
858 /* This code is mostly duplicated from expand_powi in the backend.
859 We establish the "optimal power tree" lookup table with the defined size.
860 The items in the table are the exponents used to calculate the index
861 exponents. Any integer n less than the value can get an "addition chain",
862 with the first node being one. */
863 #define POWI_TABLE_SIZE 256
865 /* The table is from builtins.c. */
866 static const unsigned char powi_table[POWI_TABLE_SIZE] =
868 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
869 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
870 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
871 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
872 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
873 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
874 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
875 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
876 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
877 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
878 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
879 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
880 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
881 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
882 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
883 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
884 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
885 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
886 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
887 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
888 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
889 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
890 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
891 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
892 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
893 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
894 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
895 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
896 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
897 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
898 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
899 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
902 /* If n is larger than lookup table's max index, we use the "window
904 #define POWI_WINDOW_SIZE 3
906 /* Recursive function to expand the power operator. The temporary
907 values are put in tmpvar. The function returns tmpvar[1] ** n. */
909 gfc_conv_powi (gfc_se * se, unsigned HOST_WIDE_INT n, tree * tmpvar)
916 if (n < POWI_TABLE_SIZE)
921 op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
922 op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
926 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
927 op0 = gfc_conv_powi (se, n - digit, tmpvar);
928 op1 = gfc_conv_powi (se, digit, tmpvar);
932 op0 = gfc_conv_powi (se, n >> 1, tmpvar);
936 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (op0), op0, op1);
937 tmp = gfc_evaluate_now (tmp, &se->pre);
939 if (n < POWI_TABLE_SIZE)
946 /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
947 return 1. Else return 0 and a call to runtime library functions
948 will have to be built. */
950 gfc_conv_cst_int_power (gfc_se * se, tree lhs, tree rhs)
955 tree vartmp[POWI_TABLE_SIZE];
957 unsigned HOST_WIDE_INT n;
960 /* If exponent is too large, we won't expand it anyway, so don't bother
961 with large integer values. */
962 if (!double_int_fits_in_shwi_p (TREE_INT_CST (rhs)))
965 m = double_int_to_shwi (TREE_INT_CST (rhs));
966 /* There's no ABS for HOST_WIDE_INT, so here we go. It also takes care
967 of the asymmetric range of the integer type. */
968 n = (unsigned HOST_WIDE_INT) (m < 0 ? -m : m);
970 type = TREE_TYPE (lhs);
971 sgn = tree_int_cst_sgn (rhs);
973 if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
974 || optimize_size) && (m > 2 || m < -1))
980 se->expr = gfc_build_const (type, integer_one_node);
984 /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1. */
985 if ((sgn == -1) && (TREE_CODE (type) == INTEGER_TYPE))
987 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
988 lhs, build_int_cst (TREE_TYPE (lhs), -1));
989 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
990 lhs, build_int_cst (TREE_TYPE (lhs), 1));
993 result = (lhs == 1 || lhs == -1) ? 1 : 0. */
996 tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR,
997 boolean_type_node, tmp, cond);
998 se->expr = fold_build3_loc (input_location, COND_EXPR, type,
999 tmp, build_int_cst (type, 1),
1000 build_int_cst (type, 0));
1004 result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0. */
1005 tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp,
1006 build_int_cst (type, -1),
1007 build_int_cst (type, 0));
1008 se->expr = fold_build3_loc (input_location, COND_EXPR, type,
1009 cond, build_int_cst (type, 1), tmp);
1013 memset (vartmp, 0, sizeof (vartmp));
1017 tmp = gfc_build_const (type, integer_one_node);
1018 vartmp[1] = fold_build2_loc (input_location, RDIV_EXPR, type, tmp,
1022 se->expr = gfc_conv_powi (se, n, vartmp);
1028 /* Power op (**). Constant integer exponent has special handling. */
1031 gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
1033 tree gfc_int4_type_node;
1036 int res_ikind_1, res_ikind_2;
1041 gfc_init_se (&lse, se);
1042 gfc_conv_expr_val (&lse, expr->value.op.op1);
1043 lse.expr = gfc_evaluate_now (lse.expr, &lse.pre);
1044 gfc_add_block_to_block (&se->pre, &lse.pre);
1046 gfc_init_se (&rse, se);
1047 gfc_conv_expr_val (&rse, expr->value.op.op2);
1048 gfc_add_block_to_block (&se->pre, &rse.pre);
1050 if (expr->value.op.op2->ts.type == BT_INTEGER
1051 && expr->value.op.op2->expr_type == EXPR_CONSTANT)
1052 if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
1055 gfc_int4_type_node = gfc_get_int_type (4);
1057 /* In case of integer operands with kinds 1 or 2, we call the integer kind 4
1058 library routine. But in the end, we have to convert the result back
1059 if this case applies -- with res_ikind_K, we keep track whether operand K
1060 falls into this case. */
1064 kind = expr->value.op.op1->ts.kind;
1065 switch (expr->value.op.op2->ts.type)
1068 ikind = expr->value.op.op2->ts.kind;
1073 rse.expr = convert (gfc_int4_type_node, rse.expr);
1074 res_ikind_2 = ikind;
1096 if (expr->value.op.op1->ts.type == BT_INTEGER)
1098 lse.expr = convert (gfc_int4_type_node, lse.expr);
1125 switch (expr->value.op.op1->ts.type)
1128 if (kind == 3) /* Case 16 was not handled properly above. */
1130 fndecl = gfor_fndecl_math_powi[kind][ikind].integer;
1134 /* Use builtins for real ** int4. */
1140 fndecl = builtin_decl_explicit (BUILT_IN_POWIF);
1144 fndecl = builtin_decl_explicit (BUILT_IN_POWI);
1148 fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
1152 /* Use the __builtin_powil() only if real(kind=16) is
1153 actually the C long double type. */
1154 if (!gfc_real16_is_float128)
1155 fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
1163 /* If we don't have a good builtin for this, go for the
1164 library function. */
1166 fndecl = gfor_fndecl_math_powi[kind][ikind].real;
1170 fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
1179 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_POW, kind);
1183 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_CPOW, kind);
1191 se->expr = build_call_expr_loc (input_location,
1192 fndecl, 2, lse.expr, rse.expr);
1194 /* Convert the result back if it is of wrong integer kind. */
1195 if (res_ikind_1 != -1 && res_ikind_2 != -1)
1197 /* We want the maximum of both operand kinds as result. */
1198 if (res_ikind_1 < res_ikind_2)
1199 res_ikind_1 = res_ikind_2;
1200 se->expr = convert (gfc_get_int_type (res_ikind_1), se->expr);
1205 /* Generate code to allocate a string temporary. */
1208 gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
1213 if (gfc_can_put_var_on_stack (len))
1215 /* Create a temporary variable to hold the result. */
1216 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1217 gfc_charlen_type_node, len,
1218 build_int_cst (gfc_charlen_type_node, 1));
1219 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node, tmp);
1221 if (TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
1222 tmp = build_array_type (TREE_TYPE (TREE_TYPE (type)), tmp);
1224 tmp = build_array_type (TREE_TYPE (type), tmp);
1226 var = gfc_create_var (tmp, "str");
1227 var = gfc_build_addr_expr (type, var);
1231 /* Allocate a temporary to hold the result. */
1232 var = gfc_create_var (type, "pstr");
1233 tmp = gfc_call_malloc (&se->pre, type,
1234 fold_build2_loc (input_location, MULT_EXPR,
1235 TREE_TYPE (len), len,
1236 fold_convert (TREE_TYPE (len),
1237 TYPE_SIZE (type))));
1238 gfc_add_modify (&se->pre, var, tmp);
1240 /* Free the temporary afterwards. */
1241 tmp = gfc_call_free (convert (pvoid_type_node, var));
1242 gfc_add_expr_to_block (&se->post, tmp);
1249 /* Handle a string concatenation operation. A temporary will be allocated to
1253 gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
1256 tree len, type, var, tmp, fndecl;
1258 gcc_assert (expr->value.op.op1->ts.type == BT_CHARACTER
1259 && expr->value.op.op2->ts.type == BT_CHARACTER);
1260 gcc_assert (expr->value.op.op1->ts.kind == expr->value.op.op2->ts.kind);
1262 gfc_init_se (&lse, se);
1263 gfc_conv_expr (&lse, expr->value.op.op1);
1264 gfc_conv_string_parameter (&lse);
1265 gfc_init_se (&rse, se);
1266 gfc_conv_expr (&rse, expr->value.op.op2);
1267 gfc_conv_string_parameter (&rse);
1269 gfc_add_block_to_block (&se->pre, &lse.pre);
1270 gfc_add_block_to_block (&se->pre, &rse.pre);
1272 type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
1273 len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
1274 if (len == NULL_TREE)
1276 len = fold_build2_loc (input_location, PLUS_EXPR,
1277 TREE_TYPE (lse.string_length),
1278 lse.string_length, rse.string_length);
1281 type = build_pointer_type (type);
1283 var = gfc_conv_string_tmp (se, type, len);
1285 /* Do the actual concatenation. */
1286 if (expr->ts.kind == 1)
1287 fndecl = gfor_fndecl_concat_string;
1288 else if (expr->ts.kind == 4)
1289 fndecl = gfor_fndecl_concat_string_char4;
1293 tmp = build_call_expr_loc (input_location,
1294 fndecl, 6, len, var, lse.string_length, lse.expr,
1295 rse.string_length, rse.expr);
1296 gfc_add_expr_to_block (&se->pre, tmp);
1298 /* Add the cleanup for the operands. */
1299 gfc_add_block_to_block (&se->pre, &rse.post);
1300 gfc_add_block_to_block (&se->pre, &lse.post);
1303 se->string_length = len;
1306 /* Translates an op expression. Common (binary) cases are handled by this
1307 function, others are passed on. Recursion is used in either case.
1308 We use the fact that (op1.ts == op2.ts) (except for the power
1310 Operators need no special handling for scalarized expressions as long as
1311 they call gfc_conv_simple_val to get their operands.
1312 Character strings get special handling. */
1315 gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
1317 enum tree_code code;
1326 switch (expr->value.op.op)
1328 case INTRINSIC_PARENTHESES:
1329 if ((expr->ts.type == BT_REAL
1330 || expr->ts.type == BT_COMPLEX)
1331 && gfc_option.flag_protect_parens)
1333 gfc_conv_unary_op (PAREN_EXPR, se, expr);
1334 gcc_assert (FLOAT_TYPE_P (TREE_TYPE (se->expr)));
1339 case INTRINSIC_UPLUS:
1340 gfc_conv_expr (se, expr->value.op.op1);
1343 case INTRINSIC_UMINUS:
1344 gfc_conv_unary_op (NEGATE_EXPR, se, expr);
1348 gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
1351 case INTRINSIC_PLUS:
1355 case INTRINSIC_MINUS:
1359 case INTRINSIC_TIMES:
1363 case INTRINSIC_DIVIDE:
1364 /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
1365 an integer, we must round towards zero, so we use a
1367 if (expr->ts.type == BT_INTEGER)
1368 code = TRUNC_DIV_EXPR;
1373 case INTRINSIC_POWER:
1374 gfc_conv_power_op (se, expr);
1377 case INTRINSIC_CONCAT:
1378 gfc_conv_concat_op (se, expr);
1382 code = TRUTH_ANDIF_EXPR;
1387 code = TRUTH_ORIF_EXPR;
1391 /* EQV and NEQV only work on logicals, but since we represent them
1392 as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE. */
1394 case INTRINSIC_EQ_OS:
1402 case INTRINSIC_NE_OS:
1403 case INTRINSIC_NEQV:
1410 case INTRINSIC_GT_OS:
1417 case INTRINSIC_GE_OS:
1424 case INTRINSIC_LT_OS:
1431 case INTRINSIC_LE_OS:
1437 case INTRINSIC_USER:
1438 case INTRINSIC_ASSIGN:
1439 /* These should be converted into function calls by the frontend. */
1443 fatal_error ("Unknown intrinsic op");
1447 /* The only exception to this is **, which is handled separately anyway. */
1448 gcc_assert (expr->value.op.op1->ts.type == expr->value.op.op2->ts.type);
1450 if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
1454 gfc_init_se (&lse, se);
1455 gfc_conv_expr (&lse, expr->value.op.op1);
1456 gfc_add_block_to_block (&se->pre, &lse.pre);
1459 gfc_init_se (&rse, se);
1460 gfc_conv_expr (&rse, expr->value.op.op2);
1461 gfc_add_block_to_block (&se->pre, &rse.pre);
1465 gfc_conv_string_parameter (&lse);
1466 gfc_conv_string_parameter (&rse);
1468 lse.expr = gfc_build_compare_string (lse.string_length, lse.expr,
1469 rse.string_length, rse.expr,
1470 expr->value.op.op1->ts.kind,
1472 rse.expr = build_int_cst (TREE_TYPE (lse.expr), 0);
1473 gfc_add_block_to_block (&lse.post, &rse.post);
1476 type = gfc_typenode_for_spec (&expr->ts);
1480 /* The result of logical ops is always boolean_type_node. */
1481 tmp = fold_build2_loc (input_location, code, boolean_type_node,
1482 lse.expr, rse.expr);
1483 se->expr = convert (type, tmp);
1486 se->expr = fold_build2_loc (input_location, code, type, lse.expr, rse.expr);
1488 /* Add the post blocks. */
1489 gfc_add_block_to_block (&se->post, &rse.post);
1490 gfc_add_block_to_block (&se->post, &lse.post);
1493 /* If a string's length is one, we convert it to a single character. */
1496 gfc_string_to_single_character (tree len, tree str, int kind)
1499 if (!INTEGER_CST_P (len) || TREE_INT_CST_HIGH (len) != 0
1500 || !POINTER_TYPE_P (TREE_TYPE (str)))
1503 if (TREE_INT_CST_LOW (len) == 1)
1505 str = fold_convert (gfc_get_pchar_type (kind), str);
1506 return build_fold_indirect_ref_loc (input_location, str);
1510 && TREE_CODE (str) == ADDR_EXPR
1511 && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
1512 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
1513 && array_ref_low_bound (TREE_OPERAND (str, 0))
1514 == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
1515 && TREE_INT_CST_LOW (len) > 1
1516 && TREE_INT_CST_LOW (len)
1517 == (unsigned HOST_WIDE_INT)
1518 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
1520 tree ret = fold_convert (gfc_get_pchar_type (kind), str);
1521 ret = build_fold_indirect_ref_loc (input_location, ret);
1522 if (TREE_CODE (ret) == INTEGER_CST)
1524 tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
1525 int i, length = TREE_STRING_LENGTH (string_cst);
1526 const char *ptr = TREE_STRING_POINTER (string_cst);
1528 for (i = 1; i < length; i++)
1541 gfc_conv_scalar_char_value (gfc_symbol *sym, gfc_se *se, gfc_expr **expr)
1544 if (sym->backend_decl)
1546 /* This becomes the nominal_type in
1547 function.c:assign_parm_find_data_types. */
1548 TREE_TYPE (sym->backend_decl) = unsigned_char_type_node;
1549 /* This becomes the passed_type in
1550 function.c:assign_parm_find_data_types. C promotes char to
1551 integer for argument passing. */
1552 DECL_ARG_TYPE (sym->backend_decl) = unsigned_type_node;
1554 DECL_BY_REFERENCE (sym->backend_decl) = 0;
1559 /* If we have a constant character expression, make it into an
1561 if ((*expr)->expr_type == EXPR_CONSTANT)
1566 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL,
1567 (int)(*expr)->value.character.string[0]);
1568 if ((*expr)->ts.kind != gfc_c_int_kind)
1570 /* The expr needs to be compatible with a C int. If the
1571 conversion fails, then the 2 causes an ICE. */
1572 ts.type = BT_INTEGER;
1573 ts.kind = gfc_c_int_kind;
1574 gfc_convert_type (*expr, &ts, 2);
1577 else if (se != NULL && (*expr)->expr_type == EXPR_VARIABLE)
1579 if ((*expr)->ref == NULL)
1581 se->expr = gfc_string_to_single_character
1582 (build_int_cst (integer_type_node, 1),
1583 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
1585 ((*expr)->symtree->n.sym)),
1590 gfc_conv_variable (se, *expr);
1591 se->expr = gfc_string_to_single_character
1592 (build_int_cst (integer_type_node, 1),
1593 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
1601 /* Helper function for gfc_build_compare_string. Return LEN_TRIM value
1602 if STR is a string literal, otherwise return -1. */
1605 gfc_optimize_len_trim (tree len, tree str, int kind)
1608 && TREE_CODE (str) == ADDR_EXPR
1609 && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
1610 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
1611 && array_ref_low_bound (TREE_OPERAND (str, 0))
1612 == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
1613 && TREE_INT_CST_LOW (len) >= 1
1614 && TREE_INT_CST_LOW (len)
1615 == (unsigned HOST_WIDE_INT)
1616 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
1618 tree folded = fold_convert (gfc_get_pchar_type (kind), str);
1619 folded = build_fold_indirect_ref_loc (input_location, folded);
1620 if (TREE_CODE (folded) == INTEGER_CST)
1622 tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
1623 int length = TREE_STRING_LENGTH (string_cst);
1624 const char *ptr = TREE_STRING_POINTER (string_cst);
1626 for (; length > 0; length--)
1627 if (ptr[length - 1] != ' ')
1636 /* Compare two strings. If they are all single characters, the result is the
1637 subtraction of them. Otherwise, we build a library call. */
1640 gfc_build_compare_string (tree len1, tree str1, tree len2, tree str2, int kind,
1641 enum tree_code code)
1647 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1)));
1648 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2)));
1650 sc1 = gfc_string_to_single_character (len1, str1, kind);
1651 sc2 = gfc_string_to_single_character (len2, str2, kind);
1653 if (sc1 != NULL_TREE && sc2 != NULL_TREE)
1655 /* Deal with single character specially. */
1656 sc1 = fold_convert (integer_type_node, sc1);
1657 sc2 = fold_convert (integer_type_node, sc2);
1658 return fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
1662 if ((code == EQ_EXPR || code == NE_EXPR)
1664 && INTEGER_CST_P (len1) && INTEGER_CST_P (len2))
1666 /* If one string is a string literal with LEN_TRIM longer
1667 than the length of the second string, the strings
1669 int len = gfc_optimize_len_trim (len1, str1, kind);
1670 if (len > 0 && compare_tree_int (len2, len) < 0)
1671 return integer_one_node;
1672 len = gfc_optimize_len_trim (len2, str2, kind);
1673 if (len > 0 && compare_tree_int (len1, len) < 0)
1674 return integer_one_node;
1677 /* Build a call for the comparison. */
1679 fndecl = gfor_fndecl_compare_string;
1681 fndecl = gfor_fndecl_compare_string_char4;
1685 return build_call_expr_loc (input_location, fndecl, 4,
1686 len1, str1, len2, str2);
1690 /* Return the backend_decl for a procedure pointer component. */
1693 get_proc_ptr_comp (gfc_expr *e)
1699 gfc_init_se (&comp_se, NULL);
1700 e2 = gfc_copy_expr (e);
1701 /* We have to restore the expr type later so that gfc_free_expr frees
1702 the exact same thing that was allocated.
1703 TODO: This is ugly. */
1704 old_type = e2->expr_type;
1705 e2->expr_type = EXPR_VARIABLE;
1706 gfc_conv_expr (&comp_se, e2);
1707 e2->expr_type = old_type;
1709 return build_fold_addr_expr_loc (input_location, comp_se.expr);
1714 conv_function_val (gfc_se * se, gfc_symbol * sym, gfc_expr * expr)
1718 if (gfc_is_proc_ptr_comp (expr, NULL))
1719 tmp = get_proc_ptr_comp (expr);
1720 else if (sym->attr.dummy)
1722 tmp = gfc_get_symbol_decl (sym);
1723 if (sym->attr.proc_pointer)
1724 tmp = build_fold_indirect_ref_loc (input_location,
1726 gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE
1727 && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) == FUNCTION_TYPE);
1731 if (!sym->backend_decl)
1732 sym->backend_decl = gfc_get_extern_function_decl (sym);
1734 tmp = sym->backend_decl;
1736 if (sym->attr.cray_pointee)
1738 /* TODO - make the cray pointee a pointer to a procedure,
1739 assign the pointer to it and use it for the call. This
1741 tmp = convert (build_pointer_type (TREE_TYPE (tmp)),
1742 gfc_get_symbol_decl (sym->cp_pointer));
1743 tmp = gfc_evaluate_now (tmp, &se->pre);
1746 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
1748 gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
1749 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
1756 /* Initialize MAPPING. */
1759 gfc_init_interface_mapping (gfc_interface_mapping * mapping)
1761 mapping->syms = NULL;
1762 mapping->charlens = NULL;
1766 /* Free all memory held by MAPPING (but not MAPPING itself). */
1769 gfc_free_interface_mapping (gfc_interface_mapping * mapping)
1771 gfc_interface_sym_mapping *sym;
1772 gfc_interface_sym_mapping *nextsym;
1774 gfc_charlen *nextcl;
1776 for (sym = mapping->syms; sym; sym = nextsym)
1778 nextsym = sym->next;
1779 sym->new_sym->n.sym->formal = NULL;
1780 gfc_free_symbol (sym->new_sym->n.sym);
1781 gfc_free_expr (sym->expr);
1782 free (sym->new_sym);
1785 for (cl = mapping->charlens; cl; cl = nextcl)
1788 gfc_free_expr (cl->length);
1794 /* Return a copy of gfc_charlen CL. Add the returned structure to
1795 MAPPING so that it will be freed by gfc_free_interface_mapping. */
1797 static gfc_charlen *
1798 gfc_get_interface_mapping_charlen (gfc_interface_mapping * mapping,
1801 gfc_charlen *new_charlen;
1803 new_charlen = gfc_get_charlen ();
1804 new_charlen->next = mapping->charlens;
1805 new_charlen->length = gfc_copy_expr (cl->length);
1807 mapping->charlens = new_charlen;
1812 /* A subroutine of gfc_add_interface_mapping. Return a descriptorless
1813 array variable that can be used as the actual argument for dummy
1814 argument SYM. Add any initialization code to BLOCK. PACKED is as
1815 for gfc_get_nodesc_array_type and DATA points to the first element
1816 in the passed array. */
1819 gfc_get_interface_mapping_array (stmtblock_t * block, gfc_symbol * sym,
1820 gfc_packed packed, tree data)
1825 type = gfc_typenode_for_spec (&sym->ts);
1826 type = gfc_get_nodesc_array_type (type, sym->as, packed,
1827 !sym->attr.target && !sym->attr.pointer
1828 && !sym->attr.proc_pointer);
1830 var = gfc_create_var (type, "ifm");
1831 gfc_add_modify (block, var, fold_convert (type, data));
1837 /* A subroutine of gfc_add_interface_mapping. Set the stride, upper bounds
1838 and offset of descriptorless array type TYPE given that it has the same
1839 size as DESC. Add any set-up code to BLOCK. */
1842 gfc_set_interface_mapping_bounds (stmtblock_t * block, tree type, tree desc)
1849 offset = gfc_index_zero_node;
1850 for (n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
1852 dim = gfc_rank_cst[n];
1853 GFC_TYPE_ARRAY_STRIDE (type, n) = gfc_conv_array_stride (desc, n);
1854 if (GFC_TYPE_ARRAY_LBOUND (type, n) == NULL_TREE)
1856 GFC_TYPE_ARRAY_LBOUND (type, n)
1857 = gfc_conv_descriptor_lbound_get (desc, dim);
1858 GFC_TYPE_ARRAY_UBOUND (type, n)
1859 = gfc_conv_descriptor_ubound_get (desc, dim);
1861 else if (GFC_TYPE_ARRAY_UBOUND (type, n) == NULL_TREE)
1863 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1864 gfc_array_index_type,
1865 gfc_conv_descriptor_ubound_get (desc, dim),
1866 gfc_conv_descriptor_lbound_get (desc, dim));
1867 tmp = fold_build2_loc (input_location, PLUS_EXPR,
1868 gfc_array_index_type,
1869 GFC_TYPE_ARRAY_LBOUND (type, n), tmp);
1870 tmp = gfc_evaluate_now (tmp, block);
1871 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1873 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
1874 GFC_TYPE_ARRAY_LBOUND (type, n),
1875 GFC_TYPE_ARRAY_STRIDE (type, n));
1876 offset = fold_build2_loc (input_location, MINUS_EXPR,
1877 gfc_array_index_type, offset, tmp);
1879 offset = gfc_evaluate_now (offset, block);
1880 GFC_TYPE_ARRAY_OFFSET (type) = offset;
1884 /* Extend MAPPING so that it maps dummy argument SYM to the value stored
1885 in SE. The caller may still use se->expr and se->string_length after
1886 calling this function. */
1889 gfc_add_interface_mapping (gfc_interface_mapping * mapping,
1890 gfc_symbol * sym, gfc_se * se,
1893 gfc_interface_sym_mapping *sm;
1897 gfc_symbol *new_sym;
1899 gfc_symtree *new_symtree;
1901 /* Create a new symbol to represent the actual argument. */
1902 new_sym = gfc_new_symbol (sym->name, NULL);
1903 new_sym->ts = sym->ts;
1904 new_sym->as = gfc_copy_array_spec (sym->as);
1905 new_sym->attr.referenced = 1;
1906 new_sym->attr.dimension = sym->attr.dimension;
1907 new_sym->attr.contiguous = sym->attr.contiguous;
1908 new_sym->attr.codimension = sym->attr.codimension;
1909 new_sym->attr.pointer = sym->attr.pointer;
1910 new_sym->attr.allocatable = sym->attr.allocatable;
1911 new_sym->attr.flavor = sym->attr.flavor;
1912 new_sym->attr.function = sym->attr.function;
1914 /* Ensure that the interface is available and that
1915 descriptors are passed for array actual arguments. */
1916 if (sym->attr.flavor == FL_PROCEDURE)
1918 new_sym->formal = expr->symtree->n.sym->formal;
1919 new_sym->attr.always_explicit
1920 = expr->symtree->n.sym->attr.always_explicit;
1923 /* Create a fake symtree for it. */
1925 new_symtree = gfc_new_symtree (&root, sym->name);
1926 new_symtree->n.sym = new_sym;
1927 gcc_assert (new_symtree == root);
1929 /* Create a dummy->actual mapping. */
1930 sm = XCNEW (gfc_interface_sym_mapping);
1931 sm->next = mapping->syms;
1933 sm->new_sym = new_symtree;
1934 sm->expr = gfc_copy_expr (expr);
1937 /* Stabilize the argument's value. */
1938 if (!sym->attr.function && se)
1939 se->expr = gfc_evaluate_now (se->expr, &se->pre);
1941 if (sym->ts.type == BT_CHARACTER)
1943 /* Create a copy of the dummy argument's length. */
1944 new_sym->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, sym->ts.u.cl);
1945 sm->expr->ts.u.cl = new_sym->ts.u.cl;
1947 /* If the length is specified as "*", record the length that
1948 the caller is passing. We should use the callee's length
1949 in all other cases. */
1950 if (!new_sym->ts.u.cl->length && se)
1952 se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
1953 new_sym->ts.u.cl->backend_decl = se->string_length;
1960 /* Use the passed value as-is if the argument is a function. */
1961 if (sym->attr.flavor == FL_PROCEDURE)
1964 /* If the argument is either a string or a pointer to a string,
1965 convert it to a boundless character type. */
1966 else if (!sym->attr.dimension && sym->ts.type == BT_CHARACTER)
1968 tmp = gfc_get_character_type_len (sym->ts.kind, NULL);
1969 tmp = build_pointer_type (tmp);
1970 if (sym->attr.pointer)
1971 value = build_fold_indirect_ref_loc (input_location,
1975 value = fold_convert (tmp, value);
1978 /* If the argument is a scalar, a pointer to an array or an allocatable,
1980 else if (!sym->attr.dimension || sym->attr.pointer || sym->attr.allocatable)
1981 value = build_fold_indirect_ref_loc (input_location,
1984 /* For character(*), use the actual argument's descriptor. */
1985 else if (sym->ts.type == BT_CHARACTER && !new_sym->ts.u.cl->length)
1986 value = build_fold_indirect_ref_loc (input_location,
1989 /* If the argument is an array descriptor, use it to determine
1990 information about the actual argument's shape. */
1991 else if (POINTER_TYPE_P (TREE_TYPE (se->expr))
1992 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
1994 /* Get the actual argument's descriptor. */
1995 desc = build_fold_indirect_ref_loc (input_location,
1998 /* Create the replacement variable. */
1999 tmp = gfc_conv_descriptor_data_get (desc);
2000 value = gfc_get_interface_mapping_array (&se->pre, sym,
2003 /* Use DESC to work out the upper bounds, strides and offset. */
2004 gfc_set_interface_mapping_bounds (&se->pre, TREE_TYPE (value), desc);
2007 /* Otherwise we have a packed array. */
2008 value = gfc_get_interface_mapping_array (&se->pre, sym,
2009 PACKED_FULL, se->expr);
2011 new_sym->backend_decl = value;
2015 /* Called once all dummy argument mappings have been added to MAPPING,
2016 but before the mapping is used to evaluate expressions. Pre-evaluate
2017 the length of each argument, adding any initialization code to PRE and
2018 any finalization code to POST. */
2021 gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
2022 stmtblock_t * pre, stmtblock_t * post)
2024 gfc_interface_sym_mapping *sym;
2028 for (sym = mapping->syms; sym; sym = sym->next)
2029 if (sym->new_sym->n.sym->ts.type == BT_CHARACTER
2030 && !sym->new_sym->n.sym->ts.u.cl->backend_decl)
2032 expr = sym->new_sym->n.sym->ts.u.cl->length;
2033 gfc_apply_interface_mapping_to_expr (mapping, expr);
2034 gfc_init_se (&se, NULL);
2035 gfc_conv_expr (&se, expr);
2036 se.expr = fold_convert (gfc_charlen_type_node, se.expr);
2037 se.expr = gfc_evaluate_now (se.expr, &se.pre);
2038 gfc_add_block_to_block (pre, &se.pre);
2039 gfc_add_block_to_block (post, &se.post);
2041 sym->new_sym->n.sym->ts.u.cl->backend_decl = se.expr;
2046 /* Like gfc_apply_interface_mapping_to_expr, but applied to
2050 gfc_apply_interface_mapping_to_cons (gfc_interface_mapping * mapping,
2051 gfc_constructor_base base)
2054 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
2056 gfc_apply_interface_mapping_to_expr (mapping, c->expr);
2059 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->start);
2060 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->end);
2061 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->step);
2067 /* Like gfc_apply_interface_mapping_to_expr, but applied to
2071 gfc_apply_interface_mapping_to_ref (gfc_interface_mapping * mapping,
2076 for (; ref; ref = ref->next)
2080 for (n = 0; n < ref->u.ar.dimen; n++)
2082 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.start[n]);
2083 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.end[n]);
2084 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.stride[n]);
2086 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.offset);
2093 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.start);
2094 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.end);
2100 /* Convert intrinsic function calls into result expressions. */
2103 gfc_map_intrinsic_function (gfc_expr *expr, gfc_interface_mapping *mapping)
2111 arg1 = expr->value.function.actual->expr;
2112 if (expr->value.function.actual->next)
2113 arg2 = expr->value.function.actual->next->expr;
2117 sym = arg1->symtree->n.sym;
2119 if (sym->attr.dummy)
2124 switch (expr->value.function.isym->id)
2127 /* TODO figure out why this condition is necessary. */
2128 if (sym->attr.function
2129 && (arg1->ts.u.cl->length == NULL
2130 || (arg1->ts.u.cl->length->expr_type != EXPR_CONSTANT
2131 && arg1->ts.u.cl->length->expr_type != EXPR_VARIABLE)))
2134 new_expr = gfc_copy_expr (arg1->ts.u.cl->length);
2138 if (!sym->as || sym->as->rank == 0)
2141 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
2143 dup = mpz_get_si (arg2->value.integer);
2148 dup = sym->as->rank;
2152 for (; d < dup; d++)
2156 if (!sym->as->upper[d] || !sym->as->lower[d])
2158 gfc_free_expr (new_expr);
2162 tmp = gfc_add (gfc_copy_expr (sym->as->upper[d]),
2163 gfc_get_int_expr (gfc_default_integer_kind,
2165 tmp = gfc_subtract (tmp, gfc_copy_expr (sym->as->lower[d]));
2167 new_expr = gfc_multiply (new_expr, tmp);
2173 case GFC_ISYM_LBOUND:
2174 case GFC_ISYM_UBOUND:
2175 /* TODO These implementations of lbound and ubound do not limit if
2176 the size < 0, according to F95's 13.14.53 and 13.14.113. */
2178 if (!sym->as || sym->as->rank == 0)
2181 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
2182 d = mpz_get_si (arg2->value.integer) - 1;
2184 /* TODO: If the need arises, this could produce an array of
2188 if (expr->value.function.isym->id == GFC_ISYM_LBOUND)
2190 if (sym->as->lower[d])
2191 new_expr = gfc_copy_expr (sym->as->lower[d]);
2195 if (sym->as->upper[d])
2196 new_expr = gfc_copy_expr (sym->as->upper[d]);
2204 gfc_apply_interface_mapping_to_expr (mapping, new_expr);
2208 gfc_replace_expr (expr, new_expr);
2214 gfc_map_fcn_formal_to_actual (gfc_expr *expr, gfc_expr *map_expr,
2215 gfc_interface_mapping * mapping)
2217 gfc_formal_arglist *f;
2218 gfc_actual_arglist *actual;
2220 actual = expr->value.function.actual;
2221 f = map_expr->symtree->n.sym->formal;
2223 for (; f && actual; f = f->next, actual = actual->next)
2228 gfc_add_interface_mapping (mapping, f->sym, NULL, actual->expr);
2231 if (map_expr->symtree->n.sym->attr.dimension)
2236 as = gfc_copy_array_spec (map_expr->symtree->n.sym->as);
2238 for (d = 0; d < as->rank; d++)
2240 gfc_apply_interface_mapping_to_expr (mapping, as->lower[d]);
2241 gfc_apply_interface_mapping_to_expr (mapping, as->upper[d]);
2244 expr->value.function.esym->as = as;
2247 if (map_expr->symtree->n.sym->ts.type == BT_CHARACTER)
2249 expr->value.function.esym->ts.u.cl->length
2250 = gfc_copy_expr (map_expr->symtree->n.sym->ts.u.cl->length);
2252 gfc_apply_interface_mapping_to_expr (mapping,
2253 expr->value.function.esym->ts.u.cl->length);
2258 /* EXPR is a copy of an expression that appeared in the interface
2259 associated with MAPPING. Walk it recursively looking for references to
2260 dummy arguments that MAPPING maps to actual arguments. Replace each such
2261 reference with a reference to the associated actual argument. */
2264 gfc_apply_interface_mapping_to_expr (gfc_interface_mapping * mapping,
2267 gfc_interface_sym_mapping *sym;
2268 gfc_actual_arglist *actual;
2273 /* Copying an expression does not copy its length, so do that here. */
2274 if (expr->ts.type == BT_CHARACTER && expr->ts.u.cl)
2276 expr->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, expr->ts.u.cl);
2277 gfc_apply_interface_mapping_to_expr (mapping, expr->ts.u.cl->length);
2280 /* Apply the mapping to any references. */
2281 gfc_apply_interface_mapping_to_ref (mapping, expr->ref);
2283 /* ...and to the expression's symbol, if it has one. */
2284 /* TODO Find out why the condition on expr->symtree had to be moved into
2285 the loop rather than being outside it, as originally. */
2286 for (sym = mapping->syms; sym; sym = sym->next)
2287 if (expr->symtree && sym->old == expr->symtree->n.sym)
2289 if (sym->new_sym->n.sym->backend_decl)
2290 expr->symtree = sym->new_sym;
2292 gfc_replace_expr (expr, gfc_copy_expr (sym->expr));
2293 /* Replace base type for polymorphic arguments. */
2294 if (expr->ref && expr->ref->type == REF_COMPONENT
2295 && sym->expr && sym->expr->ts.type == BT_CLASS)
2296 expr->ref->u.c.sym = sym->expr->ts.u.derived;
2299 /* ...and to subexpressions in expr->value. */
2300 switch (expr->expr_type)
2305 case EXPR_SUBSTRING:
2309 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op1);
2310 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op2);
2314 for (actual = expr->value.function.actual; actual; actual = actual->next)
2315 gfc_apply_interface_mapping_to_expr (mapping, actual->expr);
2317 if (expr->value.function.esym == NULL
2318 && expr->value.function.isym != NULL
2319 && expr->value.function.actual->expr->symtree
2320 && gfc_map_intrinsic_function (expr, mapping))
2323 for (sym = mapping->syms; sym; sym = sym->next)
2324 if (sym->old == expr->value.function.esym)
2326 expr->value.function.esym = sym->new_sym->n.sym;
2327 gfc_map_fcn_formal_to_actual (expr, sym->expr, mapping);
2328 expr->value.function.esym->result = sym->new_sym->n.sym;
2333 case EXPR_STRUCTURE:
2334 gfc_apply_interface_mapping_to_cons (mapping, expr->value.constructor);
2347 /* Evaluate interface expression EXPR using MAPPING. Store the result
2351 gfc_apply_interface_mapping (gfc_interface_mapping * mapping,
2352 gfc_se * se, gfc_expr * expr)
2354 expr = gfc_copy_expr (expr);
2355 gfc_apply_interface_mapping_to_expr (mapping, expr);
2356 gfc_conv_expr (se, expr);
2357 se->expr = gfc_evaluate_now (se->expr, &se->pre);
2358 gfc_free_expr (expr);
2362 /* Returns a reference to a temporary array into which a component of
2363 an actual argument derived type array is copied and then returned
2364 after the function call. */
2366 gfc_conv_subref_array_arg (gfc_se * parmse, gfc_expr * expr, int g77,
2367 sym_intent intent, bool formal_ptr)
2375 gfc_array_info *info;
2385 gcc_assert (expr->expr_type == EXPR_VARIABLE);
2387 gfc_init_se (&lse, NULL);
2388 gfc_init_se (&rse, NULL);
2390 /* Walk the argument expression. */
2391 rss = gfc_walk_expr (expr);
2393 gcc_assert (rss != gfc_ss_terminator);
2395 /* Initialize the scalarizer. */
2396 gfc_init_loopinfo (&loop);
2397 gfc_add_ss_to_loop (&loop, rss);
2399 /* Calculate the bounds of the scalarization. */
2400 gfc_conv_ss_startstride (&loop);
2402 /* Build an ss for the temporary. */
2403 if (expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->backend_decl)
2404 gfc_conv_string_length (expr->ts.u.cl, expr, &parmse->pre);
2406 base_type = gfc_typenode_for_spec (&expr->ts);
2407 if (GFC_ARRAY_TYPE_P (base_type)
2408 || GFC_DESCRIPTOR_TYPE_P (base_type))
2409 base_type = gfc_get_element_type (base_type);
2411 loop.temp_ss = gfc_get_temp_ss (base_type, ((expr->ts.type == BT_CHARACTER)
2412 ? expr->ts.u.cl->backend_decl
2416 parmse->string_length = loop.temp_ss->info->string_length;
2418 /* Associate the SS with the loop. */
2419 gfc_add_ss_to_loop (&loop, loop.temp_ss);
2421 /* Setup the scalarizing loops. */
2422 gfc_conv_loop_setup (&loop, &expr->where);
2424 /* Pass the temporary descriptor back to the caller. */
2425 info = &loop.temp_ss->info->data.array;
2426 parmse->expr = info->descriptor;
2428 /* Setup the gfc_se structures. */
2429 gfc_copy_loopinfo_to_se (&lse, &loop);
2430 gfc_copy_loopinfo_to_se (&rse, &loop);
2433 lse.ss = loop.temp_ss;
2434 gfc_mark_ss_chain_used (rss, 1);
2435 gfc_mark_ss_chain_used (loop.temp_ss, 1);
2437 /* Start the scalarized loop body. */
2438 gfc_start_scalarized_body (&loop, &body);
2440 /* Translate the expression. */
2441 gfc_conv_expr (&rse, expr);
2443 gfc_conv_tmp_array_ref (&lse);
2445 if (intent != INTENT_OUT)
2447 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, true, false, true);
2448 gfc_add_expr_to_block (&body, tmp);
2449 gcc_assert (rse.ss == gfc_ss_terminator);
2450 gfc_trans_scalarizing_loops (&loop, &body);
2454 /* Make sure that the temporary declaration survives by merging
2455 all the loop declarations into the current context. */
2456 for (n = 0; n < loop.dimen; n++)
2458 gfc_merge_block_scope (&body);
2459 body = loop.code[loop.order[n]];
2461 gfc_merge_block_scope (&body);
2464 /* Add the post block after the second loop, so that any
2465 freeing of allocated memory is done at the right time. */
2466 gfc_add_block_to_block (&parmse->pre, &loop.pre);
2468 /**********Copy the temporary back again.*********/
2470 gfc_init_se (&lse, NULL);
2471 gfc_init_se (&rse, NULL);
2473 /* Walk the argument expression. */
2474 lss = gfc_walk_expr (expr);
2475 rse.ss = loop.temp_ss;
2478 /* Initialize the scalarizer. */
2479 gfc_init_loopinfo (&loop2);
2480 gfc_add_ss_to_loop (&loop2, lss);
2482 /* Calculate the bounds of the scalarization. */
2483 gfc_conv_ss_startstride (&loop2);
2485 /* Setup the scalarizing loops. */
2486 gfc_conv_loop_setup (&loop2, &expr->where);
2488 gfc_copy_loopinfo_to_se (&lse, &loop2);
2489 gfc_copy_loopinfo_to_se (&rse, &loop2);
2491 gfc_mark_ss_chain_used (lss, 1);
2492 gfc_mark_ss_chain_used (loop.temp_ss, 1);
2494 /* Declare the variable to hold the temporary offset and start the
2495 scalarized loop body. */
2496 offset = gfc_create_var (gfc_array_index_type, NULL);
2497 gfc_start_scalarized_body (&loop2, &body);
2499 /* Build the offsets for the temporary from the loop variables. The
2500 temporary array has lbounds of zero and strides of one in all
2501 dimensions, so this is very simple. The offset is only computed
2502 outside the innermost loop, so the overall transfer could be
2503 optimized further. */
2504 info = &rse.ss->info->data.array;
2505 dimen = rse.ss->dimen;
2507 tmp_index = gfc_index_zero_node;
2508 for (n = dimen - 1; n > 0; n--)
2511 tmp = rse.loop->loopvar[n];
2512 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
2513 tmp, rse.loop->from[n]);
2514 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
2517 tmp_str = fold_build2_loc (input_location, MINUS_EXPR,
2518 gfc_array_index_type,
2519 rse.loop->to[n-1], rse.loop->from[n-1]);
2520 tmp_str = fold_build2_loc (input_location, PLUS_EXPR,
2521 gfc_array_index_type,
2522 tmp_str, gfc_index_one_node);
2524 tmp_index = fold_build2_loc (input_location, MULT_EXPR,
2525 gfc_array_index_type, tmp, tmp_str);
2528 tmp_index = fold_build2_loc (input_location, MINUS_EXPR,
2529 gfc_array_index_type,
2530 tmp_index, rse.loop->from[0]);
2531 gfc_add_modify (&rse.loop->code[0], offset, tmp_index);
2533 tmp_index = fold_build2_loc (input_location, PLUS_EXPR,
2534 gfc_array_index_type,
2535 rse.loop->loopvar[0], offset);
2537 /* Now use the offset for the reference. */
2538 tmp = build_fold_indirect_ref_loc (input_location,
2540 rse.expr = gfc_build_array_ref (tmp, tmp_index, NULL);
2542 if (expr->ts.type == BT_CHARACTER)
2543 rse.string_length = expr->ts.u.cl->backend_decl;
2545 gfc_conv_expr (&lse, expr);
2547 gcc_assert (lse.ss == gfc_ss_terminator);
2549 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, false, true);
2550 gfc_add_expr_to_block (&body, tmp);
2552 /* Generate the copying loops. */
2553 gfc_trans_scalarizing_loops (&loop2, &body);
2555 /* Wrap the whole thing up by adding the second loop to the post-block
2556 and following it by the post-block of the first loop. In this way,
2557 if the temporary needs freeing, it is done after use! */
2558 if (intent != INTENT_IN)
2560 gfc_add_block_to_block (&parmse->post, &loop2.pre);
2561 gfc_add_block_to_block (&parmse->post, &loop2.post);
2564 gfc_add_block_to_block (&parmse->post, &loop.post);
2566 gfc_cleanup_loop (&loop);
2567 gfc_cleanup_loop (&loop2);
2569 /* Pass the string length to the argument expression. */
2570 if (expr->ts.type == BT_CHARACTER)
2571 parmse->string_length = expr->ts.u.cl->backend_decl;
2573 /* Determine the offset for pointer formal arguments and set the
2577 size = gfc_index_one_node;
2578 offset = gfc_index_zero_node;
2579 for (n = 0; n < dimen; n++)
2581 tmp = gfc_conv_descriptor_ubound_get (parmse->expr,
2583 tmp = fold_build2_loc (input_location, PLUS_EXPR,
2584 gfc_array_index_type, tmp,
2585 gfc_index_one_node);
2586 gfc_conv_descriptor_ubound_set (&parmse->pre,
2590 gfc_conv_descriptor_lbound_set (&parmse->pre,
2593 gfc_index_one_node);
2594 size = gfc_evaluate_now (size, &parmse->pre);
2595 offset = fold_build2_loc (input_location, MINUS_EXPR,
2596 gfc_array_index_type,
2598 offset = gfc_evaluate_now (offset, &parmse->pre);
2599 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2600 gfc_array_index_type,
2601 rse.loop->to[n], rse.loop->from[n]);
2602 tmp = fold_build2_loc (input_location, PLUS_EXPR,
2603 gfc_array_index_type,
2604 tmp, gfc_index_one_node);
2605 size = fold_build2_loc (input_location, MULT_EXPR,
2606 gfc_array_index_type, size, tmp);
2609 gfc_conv_descriptor_offset_set (&parmse->pre, parmse->expr,
2613 /* We want either the address for the data or the address of the descriptor,
2614 depending on the mode of passing array arguments. */
2616 parmse->expr = gfc_conv_descriptor_data_get (parmse->expr);
2618 parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
2624 /* Generate the code for argument list functions. */
2627 conv_arglist_function (gfc_se *se, gfc_expr *expr, const char *name)
2629 /* Pass by value for g77 %VAL(arg), pass the address
2630 indirectly for %LOC, else by reference. Thus %REF
2631 is a "do-nothing" and %LOC is the same as an F95
2633 if (strncmp (name, "%VAL", 4) == 0)
2634 gfc_conv_expr (se, expr);
2635 else if (strncmp (name, "%LOC", 4) == 0)
2637 gfc_conv_expr_reference (se, expr);
2638 se->expr = gfc_build_addr_expr (NULL, se->expr);
2640 else if (strncmp (name, "%REF", 4) == 0)
2641 gfc_conv_expr_reference (se, expr);
2643 gfc_error ("Unknown argument list function at %L", &expr->where);
2647 /* Takes a derived type expression and returns the address of a temporary
2648 class object of the 'declared' type. */
2650 gfc_conv_derived_to_class (gfc_se *parmse, gfc_expr *e,
2651 gfc_typespec class_ts)
2655 gfc_symbol *declared = class_ts.u.derived;
2661 /* The derived type needs to be converted to a temporary
2663 tmp = gfc_typenode_for_spec (&class_ts);
2664 var = gfc_create_var (tmp, "class");
2667 cmp = gfc_find_component (declared, "_vptr", true, true);
2668 ctree = fold_build3_loc (input_location, COMPONENT_REF,
2669 TREE_TYPE (cmp->backend_decl),
2670 var, cmp->backend_decl, NULL_TREE);
2672 /* Remember the vtab corresponds to the derived type
2673 not to the class declared type. */
2674 vtab = gfc_find_derived_vtab (e->ts.u.derived);
2676 tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
2677 gfc_add_modify (&parmse->pre, ctree,
2678 fold_convert (TREE_TYPE (ctree), tmp));
2680 /* Now set the data field. */
2681 cmp = gfc_find_component (declared, "_data", true, true);
2682 ctree = fold_build3_loc (input_location, COMPONENT_REF,
2683 TREE_TYPE (cmp->backend_decl),
2684 var, cmp->backend_decl, NULL_TREE);
2685 ss = gfc_walk_expr (e);
2686 if (ss == gfc_ss_terminator)
2689 gfc_conv_expr_reference (parmse, e);
2690 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
2691 gfc_add_modify (&parmse->pre, ctree, tmp);
2696 gfc_conv_expr (parmse, e);
2697 gfc_add_modify (&parmse->pre, ctree, parmse->expr);
2700 /* Pass the address of the class object. */
2701 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
2705 /* The following routine generates code for the intrinsic
2706 procedures from the ISO_C_BINDING module:
2708 * C_FUNLOC (function)
2709 * C_F_POINTER (subroutine)
2710 * C_F_PROCPOINTER (subroutine)
2711 * C_ASSOCIATED (function)
2712 One exception which is not handled here is C_F_POINTER with non-scalar
2713 arguments. Returns 1 if the call was replaced by inline code (else: 0). */
2716 conv_isocbinding_procedure (gfc_se * se, gfc_symbol * sym,
2717 gfc_actual_arglist * arg)
2722 if (sym->intmod_sym_id == ISOCBINDING_LOC)
2724 if (arg->expr->rank == 0)
2725 gfc_conv_expr_reference (se, arg->expr);
2729 /* This is really the actual arg because no formal arglist is
2730 created for C_LOC. */
2731 fsym = arg->expr->symtree->n.sym;
2733 /* We should want it to do g77 calling convention. */
2735 && !(fsym->attr.pointer || fsym->attr.allocatable)
2736 && fsym->as->type != AS_ASSUMED_SHAPE;
2737 f = f || !sym->attr.always_explicit;
2739 argss = gfc_walk_expr (arg->expr);
2740 gfc_conv_array_parameter (se, arg->expr, argss, f,
2744 /* TODO -- the following two lines shouldn't be necessary, but if
2745 they're removed, a bug is exposed later in the code path.
2746 This workaround was thus introduced, but will have to be
2747 removed; please see PR 35150 for details about the issue. */
2748 se->expr = convert (pvoid_type_node, se->expr);
2749 se->expr = gfc_evaluate_now (se->expr, &se->pre);
2753 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2755 arg->expr->ts.type = sym->ts.u.derived->ts.type;
2756 arg->expr->ts.f90_type = sym->ts.u.derived->ts.f90_type;
2757 arg->expr->ts.kind = sym->ts.u.derived->ts.kind;
2758 gfc_conv_expr_reference (se, arg->expr);
2762 else if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER
2763 && arg->next->expr->rank == 0)
2764 || sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER)
2766 /* Convert c_f_pointer if fptr is a scalar
2767 and convert c_f_procpointer. */
2771 gfc_init_se (&cptrse, NULL);
2772 gfc_conv_expr (&cptrse, arg->expr);
2773 gfc_add_block_to_block (&se->pre, &cptrse.pre);
2774 gfc_add_block_to_block (&se->post, &cptrse.post);
2776 gfc_init_se (&fptrse, NULL);
2777 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER
2778 || gfc_is_proc_ptr_comp (arg->next->expr, NULL))
2779 fptrse.want_pointer = 1;
2781 gfc_conv_expr (&fptrse, arg->next->expr);
2782 gfc_add_block_to_block (&se->pre, &fptrse.pre);
2783 gfc_add_block_to_block (&se->post, &fptrse.post);
2785 if (arg->next->expr->symtree->n.sym->attr.proc_pointer
2786 && arg->next->expr->symtree->n.sym->attr.dummy)
2787 fptrse.expr = build_fold_indirect_ref_loc (input_location,
2790 se->expr = fold_build2_loc (input_location, MODIFY_EXPR,
2791 TREE_TYPE (fptrse.expr),
2793 fold_convert (TREE_TYPE (fptrse.expr),
2798 else if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2803 /* Build the addr_expr for the first argument. The argument is
2804 already an *address* so we don't need to set want_pointer in
2806 gfc_init_se (&arg1se, NULL);
2807 gfc_conv_expr (&arg1se, arg->expr);
2808 gfc_add_block_to_block (&se->pre, &arg1se.pre);
2809 gfc_add_block_to_block (&se->post, &arg1se.post);
2811 /* See if we were given two arguments. */
2812 if (arg->next == NULL)
2813 /* Only given one arg so generate a null and do a
2814 not-equal comparison against the first arg. */
2815 se->expr = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
2817 fold_convert (TREE_TYPE (arg1se.expr),
2818 null_pointer_node));
2824 /* Given two arguments so build the arg2se from second arg. */
2825 gfc_init_se (&arg2se, NULL);
2826 gfc_conv_expr (&arg2se, arg->next->expr);
2827 gfc_add_block_to_block (&se->pre, &arg2se.pre);
2828 gfc_add_block_to_block (&se->post, &arg2se.post);
2830 /* Generate test to compare that the two args are equal. */
2831 eq_expr = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2832 arg1se.expr, arg2se.expr);
2833 /* Generate test to ensure that the first arg is not null. */
2834 not_null_expr = fold_build2_loc (input_location, NE_EXPR,
2836 arg1se.expr, null_pointer_node);
2838 /* Finally, the generated test must check that both arg1 is not
2839 NULL and that it is equal to the second arg. */
2840 se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
2842 not_null_expr, eq_expr);
2848 /* Nothing was done. */
2853 /* Generate code for a procedure call. Note can return se->post != NULL.
2854 If se->direct_byref is set then se->expr contains the return parameter.
2855 Return nonzero, if the call has alternate specifiers.
2856 'expr' is only needed for procedure pointer components. */
2859 gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
2860 gfc_actual_arglist * args, gfc_expr * expr,
2861 VEC(tree,gc) *append_args)
2863 gfc_interface_mapping mapping;
2864 VEC(tree,gc) *arglist;
2865 VEC(tree,gc) *retargs;
2870 gfc_array_info *info;
2876 VEC(tree,gc) *stringargs;
2878 gfc_formal_arglist *formal;
2879 gfc_actual_arglist *arg;
2880 int has_alternate_specifier = 0;
2881 bool need_interface_mapping;
2888 enum {MISSING = 0, ELEMENTAL, SCALAR, SCALAR_POINTER, ARRAY};
2889 gfc_component *comp = NULL;
2899 if (sym->from_intmod == INTMOD_ISO_C_BINDING
2900 && conv_isocbinding_procedure (se, sym, args))
2903 gfc_is_proc_ptr_comp (expr, &comp);
2907 if (!sym->attr.elemental)
2909 gcc_assert (se->ss->info->type == GFC_SS_FUNCTION);
2910 if (se->ss->info->useflags)
2912 gcc_assert ((!comp && gfc_return_by_reference (sym)
2913 && sym->result->attr.dimension)
2914 || (comp && comp->attr.dimension));
2915 gcc_assert (se->loop != NULL);
2917 /* Access the previously obtained result. */
2918 gfc_conv_tmp_array_ref (se);
2922 info = &se->ss->info->data.array;
2927 gfc_init_block (&post);
2928 gfc_init_interface_mapping (&mapping);
2931 formal = sym->formal;
2932 need_interface_mapping = sym->attr.dimension ||
2933 (sym->ts.type == BT_CHARACTER
2934 && sym->ts.u.cl->length
2935 && sym->ts.u.cl->length->expr_type
2940 formal = comp->formal;
2941 need_interface_mapping = comp->attr.dimension ||
2942 (comp->ts.type == BT_CHARACTER
2943 && comp->ts.u.cl->length
2944 && comp->ts.u.cl->length->expr_type
2948 /* Evaluate the arguments. */
2949 for (arg = args; arg != NULL;
2950 arg = arg->next, formal = formal ? formal->next : NULL)
2953 fsym = formal ? formal->sym : NULL;
2954 parm_kind = MISSING;
2958 if (se->ignore_optional)
2960 /* Some intrinsics have already been resolved to the correct
2964 else if (arg->label)
2966 has_alternate_specifier = 1;
2971 /* Pass a NULL pointer for an absent arg. */
2972 gfc_init_se (&parmse, NULL);
2973 parmse.expr = null_pointer_node;
2974 if (arg->missing_arg_type == BT_CHARACTER)
2975 parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
2978 else if (arg->expr->expr_type == EXPR_NULL && fsym && !fsym->attr.pointer)
2980 /* Pass a NULL pointer to denote an absent arg. */
2981 gcc_assert (fsym->attr.optional && !fsym->attr.allocatable);
2982 gfc_init_se (&parmse, NULL);
2983 parmse.expr = null_pointer_node;
2984 if (arg->missing_arg_type == BT_CHARACTER)
2985 parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
2987 else if (fsym && fsym->ts.type == BT_CLASS
2988 && e->ts.type == BT_DERIVED)
2990 /* The derived type needs to be converted to a temporary
2992 gfc_init_se (&parmse, se);
2993 gfc_conv_derived_to_class (&parmse, e, fsym->ts);
2995 else if (se->ss && se->ss->info->useflags)
2997 /* An elemental function inside a scalarized loop. */
2998 gfc_init_se (&parmse, se);
2999 gfc_conv_expr_reference (&parmse, e);
3000 parm_kind = ELEMENTAL;
3004 /* A scalar or transformational function. */
3005 gfc_init_se (&parmse, NULL);
3006 argss = gfc_walk_expr (e);
3008 if (argss == gfc_ss_terminator)
3010 if (e->expr_type == EXPR_VARIABLE
3011 && e->symtree->n.sym->attr.cray_pointee
3012 && fsym && fsym->attr.flavor == FL_PROCEDURE)
3014 /* The Cray pointer needs to be converted to a pointer to
3015 a type given by the expression. */
3016 gfc_conv_expr (&parmse, e);
3017 type = build_pointer_type (TREE_TYPE (parmse.expr));
3018 tmp = gfc_get_symbol_decl (e->symtree->n.sym->cp_pointer);
3019 parmse.expr = convert (type, tmp);
3021 else if (fsym && fsym->attr.value)
3023 if (fsym->ts.type == BT_CHARACTER
3024 && fsym->ts.is_c_interop
3025 && fsym->ns->proc_name != NULL
3026 && fsym->ns->proc_name->attr.is_bind_c)
3029 gfc_conv_scalar_char_value (fsym, &parmse, &e);
3030 if (parmse.expr == NULL)
3031 gfc_conv_expr (&parmse, e);
3034 gfc_conv_expr (&parmse, e);
3036 else if (arg->name && arg->name[0] == '%')
3037 /* Argument list functions %VAL, %LOC and %REF are signalled
3038 through arg->name. */
3039 conv_arglist_function (&parmse, arg->expr, arg->name);
3040 else if ((e->expr_type == EXPR_FUNCTION)
3041 && ((e->value.function.esym
3042 && e->value.function.esym->result->attr.pointer)
3043 || (!e->value.function.esym
3044 && e->symtree->n.sym->attr.pointer))
3045 && fsym && fsym->attr.target)
3047 gfc_conv_expr (&parmse, e);
3048 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
3050 else if (e->expr_type == EXPR_FUNCTION
3051 && e->symtree->n.sym->result
3052 && e->symtree->n.sym->result != e->symtree->n.sym
3053 && e->symtree->n.sym->result->attr.proc_pointer)
3055 /* Functions returning procedure pointers. */
3056 gfc_conv_expr (&parmse, e);
3057 if (fsym && fsym->attr.proc_pointer)
3058 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
3062 gfc_conv_expr_reference (&parmse, e);
3064 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
3065 allocated on entry, it must be deallocated. */
3066 if (fsym && fsym->attr.allocatable
3067 && fsym->attr.intent == INTENT_OUT)
3071 gfc_init_block (&block);
3072 tmp = gfc_deallocate_with_status (parmse.expr, NULL_TREE,
3074 gfc_add_expr_to_block (&block, tmp);
3075 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
3076 void_type_node, parmse.expr,
3078 gfc_add_expr_to_block (&block, tmp);
3080 if (fsym->attr.optional
3081 && e->expr_type == EXPR_VARIABLE
3082 && e->symtree->n.sym->attr.optional)
3084 tmp = fold_build3_loc (input_location, COND_EXPR,
3086 gfc_conv_expr_present (e->symtree->n.sym),
3087 gfc_finish_block (&block),
3088 build_empty_stmt (input_location));
3091 tmp = gfc_finish_block (&block);
3093 gfc_add_expr_to_block (&se->pre, tmp);
3096 if (fsym && e->expr_type != EXPR_NULL
3097 && ((fsym->attr.pointer
3098 && fsym->attr.flavor != FL_PROCEDURE)
3099 || (fsym->attr.proc_pointer
3100 && !(e->expr_type == EXPR_VARIABLE
3101 && e->symtree->n.sym->attr.dummy))
3102 || (fsym->attr.proc_pointer
3103 && e->expr_type == EXPR_VARIABLE
3104 && gfc_is_proc_ptr_comp (e, NULL))
3105 || fsym->attr.allocatable))
3107 /* Scalar pointer dummy args require an extra level of
3108 indirection. The null pointer already contains
3109 this level of indirection. */
3110 parm_kind = SCALAR_POINTER;
3111 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
3117 /* If the procedure requires an explicit interface, the actual
3118 argument is passed according to the corresponding formal
3119 argument. If the corresponding formal argument is a POINTER,
3120 ALLOCATABLE or assumed shape, we do not use g77's calling
3121 convention, and pass the address of the array descriptor
3122 instead. Otherwise we use g77's calling convention. */
3125 && !(fsym->attr.pointer || fsym->attr.allocatable)
3126 && fsym->as && fsym->as->type != AS_ASSUMED_SHAPE;
3128 f = f || !comp->attr.always_explicit;
3130 f = f || !sym->attr.always_explicit;
3132 /* If the argument is a function call that may not create
3133 a temporary for the result, we have to check that we
3134 can do it, i.e. that there is no alias between this
3135 argument and another one. */
3136 if (gfc_get_noncopying_intrinsic_argument (e) != NULL)
3142 intent = fsym->attr.intent;
3144 intent = INTENT_UNKNOWN;
3146 if (gfc_check_fncall_dependency (e, intent, sym, args,
3148 parmse.force_tmp = 1;
3150 iarg = e->value.function.actual->expr;
3152 /* Temporary needed if aliasing due to host association. */
3153 if (sym->attr.contained
3155 && !sym->attr.implicit_pure
3156 && !sym->attr.use_assoc
3157 && iarg->expr_type == EXPR_VARIABLE
3158 && sym->ns == iarg->symtree->n.sym->ns)
3159 parmse.force_tmp = 1;
3161 /* Ditto within module. */
3162 if (sym->attr.use_assoc
3164 && !sym->attr.implicit_pure
3165 && iarg->expr_type == EXPR_VARIABLE
3166 && sym->module == iarg->symtree->n.sym->module)
3167 parmse.force_tmp = 1;
3170 if (e->expr_type == EXPR_VARIABLE
3171 && is_subref_array (e))
3172 /* The actual argument is a component reference to an
3173 array of derived types. In this case, the argument
3174 is converted to a temporary, which is passed and then
3175 written back after the procedure call. */
3176 gfc_conv_subref_array_arg (&parmse, e, f,
3177 fsym ? fsym->attr.intent : INTENT_INOUT,
3178 fsym && fsym->attr.pointer);
3180 gfc_conv_array_parameter (&parmse, e, argss, f, fsym,
3183 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
3184 allocated on entry, it must be deallocated. */
3185 if (fsym && fsym->attr.allocatable
3186 && fsym->attr.intent == INTENT_OUT)
3188 tmp = build_fold_indirect_ref_loc (input_location,
3190 tmp = gfc_trans_dealloc_allocated (tmp);
3191 if (fsym->attr.optional
3192 && e->expr_type == EXPR_VARIABLE
3193 && e->symtree->n.sym->attr.optional)
3194 tmp = fold_build3_loc (input_location, COND_EXPR,
3196 gfc_conv_expr_present (e->symtree->n.sym),
3197 tmp, build_empty_stmt (input_location));
3198 gfc_add_expr_to_block (&se->pre, tmp);
3203 /* The case with fsym->attr.optional is that of a user subroutine
3204 with an interface indicating an optional argument. When we call
3205 an intrinsic subroutine, however, fsym is NULL, but we might still
3206 have an optional argument, so we proceed to the substitution
3208 if (e && (fsym == NULL || fsym->attr.optional))
3210 /* If an optional argument is itself an optional dummy argument,
3211 check its presence and substitute a null if absent. This is
3212 only needed when passing an array to an elemental procedure
3213 as then array elements are accessed - or no NULL pointer is
3214 allowed and a "1" or "0" should be passed if not present.
3215 When passing a non-array-descriptor full array to a
3216 non-array-descriptor dummy, no check is needed. For
3217 array-descriptor actual to array-descriptor dummy, see
3218 PR 41911 for why a check has to be inserted.
3219 fsym == NULL is checked as intrinsics required the descriptor
3220 but do not always set fsym. */
3221 if (e->expr_type == EXPR_VARIABLE
3222 && e->symtree->n.sym->attr.optional
3223 && ((e->rank > 0 && sym->attr.elemental)
3224 || e->representation.length || e->ts.type == BT_CHARACTER
3228 && (fsym->as->type == AS_ASSUMED_SHAPE
3229 || fsym->as->type == AS_DEFERRED))))))
3230 gfc_conv_missing_dummy (&parmse, e, fsym ? fsym->ts : e->ts,
3231 e->representation.length);
3236 /* Obtain the character length of an assumed character length
3237 length procedure from the typespec. */
3238 if (fsym->ts.type == BT_CHARACTER
3239 && parmse.string_length == NULL_TREE
3240 && e->ts.type == BT_PROCEDURE
3241 && e->symtree->n.sym->ts.type == BT_CHARACTER
3242 && e->symtree->n.sym->ts.u.cl->length != NULL
3243 && e->symtree->n.sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
3245 gfc_conv_const_charlen (e->symtree->n.sym->ts.u.cl);
3246 parmse.string_length = e->symtree->n.sym->ts.u.cl->backend_decl;
3250 if (fsym && need_interface_mapping && e)
3251 gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
3253 gfc_add_block_to_block (&se->pre, &parmse.pre);
3254 gfc_add_block_to_block (&post, &parmse.post);
3256 /* Allocated allocatable components of derived types must be
3257 deallocated for non-variable scalars. Non-variable arrays are
3258 dealt with in trans-array.c(gfc_conv_array_parameter). */
3259 if (e && e->ts.type == BT_DERIVED
3260 && e->ts.u.derived->attr.alloc_comp
3261 && !(e->symtree && e->symtree->n.sym->attr.pointer)
3262 && (e->expr_type != EXPR_VARIABLE && !e->rank))
3265 tmp = build_fold_indirect_ref_loc (input_location,
3267 parm_rank = e->rank;
3275 case (SCALAR_POINTER):
3276 tmp = build_fold_indirect_ref_loc (input_location,
3281 if (e->expr_type == EXPR_OP
3282 && e->value.op.op == INTRINSIC_PARENTHESES
3283 && e->value.op.op1->expr_type == EXPR_VARIABLE)
3286 local_tmp = gfc_evaluate_now (tmp, &se->pre);
3287 local_tmp = gfc_copy_alloc_comp (e->ts.u.derived, local_tmp, tmp, parm_rank);
3288 gfc_add_expr_to_block (&se->post, local_tmp);
3291 tmp = gfc_deallocate_alloc_comp (e->ts.u.derived, tmp, parm_rank);
3293 gfc_add_expr_to_block (&se->post, tmp);
3296 /* Add argument checking of passing an unallocated/NULL actual to
3297 a nonallocatable/nonpointer dummy. */
3299 if (gfc_option.rtcheck & GFC_RTCHECK_POINTER && e != NULL)
3301 symbol_attribute attr;
3305 if (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
3306 attr = gfc_expr_attr (e);
3308 goto end_pointer_check;
3310 /* In Fortran 2008 it's allowed to pass a NULL pointer/nonallocated
3311 allocatable to an optional dummy, cf. 12.5.2.12. */
3312 if (fsym != NULL && fsym->attr.optional && !attr.proc_pointer
3313 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
3314 goto end_pointer_check;
3318 /* If the actual argument is an optional pointer/allocatable and
3319 the formal argument takes an nonpointer optional value,
3320 it is invalid to pass a non-present argument on, even
3321 though there is no technical reason for this in gfortran.
3322 See Fortran 2003, Section 12.4.1.6 item (7)+(8). */
3323 tree present, null_ptr, type;
3325 if (attr.allocatable
3326 && (fsym == NULL || !fsym->attr.allocatable))
3327 asprintf (&msg, "Allocatable actual argument '%s' is not "
3328 "allocated or not present", e->symtree->n.sym->name);
3329 else if (attr.pointer
3330 && (fsym == NULL || !fsym->attr.pointer))
3331 asprintf (&msg, "Pointer actual argument '%s' is not "
3332 "associated or not present",
3333 e->symtree->n.sym->name);
3334 else if (attr.proc_pointer
3335 && (fsym == NULL || !fsym->attr.proc_pointer))
3336 asprintf (&msg, "Proc-pointer actual argument '%s' is not "
3337 "associated or not present",
3338 e->symtree->n.sym->name);
3340 goto end_pointer_check;
3342 present = gfc_conv_expr_present (e->symtree->n.sym);
3343 type = TREE_TYPE (present);
3344 present = fold_build2_loc (input_location, EQ_EXPR,
3345 boolean_type_node, present,