Fix a bug that broke -freorder-functions
[official-gcc.git] / gcc / fortran / trans-expr.c
blob96510c2c294984d158bd4d4e82592e4de9151622
1 /* Expression translation
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
3 2011
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
13 version.
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
18 for more details.
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. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tree.h"
30 #include "diagnostic-core.h" /* For fatal_error. */
31 #include "langhooks.h"
32 #include "flags.h"
33 #include "gfortran.h"
34 #include "arith.h"
35 #include "constructor.h"
36 #include "trans.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 *,
46 gfc_expr *);
48 /* Copy the scalarization loop variables. */
50 static void
51 gfc_copy_se_loopvars (gfc_se * dest, gfc_se * src)
53 dest->ss = src->ss;
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. */
64 void
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);
71 se->parent = parent;
73 if (parent)
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.
80 See gfc_init_se. */
82 void
83 gfc_advance_se_ss_chain (gfc_se * se)
85 gfc_se *p;
87 gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
89 p = se;
90 /* Walk down the parent chain. */
91 while (p != NULL)
93 /* Simple consistency check. */
94 gcc_assert (p->parent == NULL || p->parent->ss == p->ss);
96 p->ss = p->ss->next;
98 p = p->parent;
103 /* Ensures the result of the expression as either a temporary variable
104 or a constant so that it can be used repeatedly. */
106 void
107 gfc_make_safe_expr (gfc_se * se)
109 tree var;
111 if (CONSTANT_CLASS_P (se->expr))
112 return;
114 /* We need a temporary for this result. */
115 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
116 gfc_add_modify (&se->pre, var, se->expr);
117 se->expr = var;
121 /* Return an expression which determines if a dummy parameter is present.
122 Also used for arguments to procedures with multiple entry points. */
124 tree
125 gfc_conv_expr_present (gfc_symbol * sym)
127 tree decl, cond;
129 gcc_assert (sym->attr.dummy);
131 decl = gfc_get_symbol_decl (sym);
132 if (TREE_CODE (decl) != PARM_DECL)
134 /* Array parameters use a temporary descriptor, we want the real
135 parameter. */
136 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
137 || GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
138 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
141 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, decl,
142 fold_convert (TREE_TYPE (decl), null_pointer_node));
144 /* Fortran 2008 allows to pass null pointers and non-associated pointers
145 as actual argument to denote absent dummies. For array descriptors,
146 we thus also need to check the array descriptor. */
147 if (!sym->attr.pointer && !sym->attr.allocatable
148 && sym->as && sym->as->type == AS_ASSUMED_SHAPE
149 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
151 tree tmp;
152 tmp = build_fold_indirect_ref_loc (input_location, decl);
153 tmp = gfc_conv_array_data (tmp);
154 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, tmp,
155 fold_convert (TREE_TYPE (tmp), null_pointer_node));
156 cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
157 boolean_type_node, cond, tmp);
160 return cond;
164 /* Converts a missing, dummy argument into a null or zero. */
166 void
167 gfc_conv_missing_dummy (gfc_se * se, gfc_expr * arg, gfc_typespec ts, int kind)
169 tree present;
170 tree tmp;
172 present = gfc_conv_expr_present (arg->symtree->n.sym);
174 if (kind > 0)
176 /* Create a temporary and convert it to the correct type. */
177 tmp = gfc_get_int_type (kind);
178 tmp = fold_convert (tmp, build_fold_indirect_ref_loc (input_location,
179 se->expr));
181 /* Test for a NULL value. */
182 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), present,
183 tmp, fold_convert (TREE_TYPE (tmp), integer_one_node));
184 tmp = gfc_evaluate_now (tmp, &se->pre);
185 se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
187 else
189 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se->expr),
190 present, se->expr,
191 build_zero_cst (TREE_TYPE (se->expr)));
192 tmp = gfc_evaluate_now (tmp, &se->pre);
193 se->expr = tmp;
196 if (ts.type == BT_CHARACTER)
198 tmp = build_int_cst (gfc_charlen_type_node, 0);
199 tmp = fold_build3_loc (input_location, COND_EXPR, gfc_charlen_type_node,
200 present, se->string_length, tmp);
201 tmp = gfc_evaluate_now (tmp, &se->pre);
202 se->string_length = tmp;
204 return;
208 /* Get the character length of an expression, looking through gfc_refs
209 if necessary. */
211 tree
212 gfc_get_expr_charlen (gfc_expr *e)
214 gfc_ref *r;
215 tree length;
217 gcc_assert (e->expr_type == EXPR_VARIABLE
218 && e->ts.type == BT_CHARACTER);
220 length = NULL; /* To silence compiler warning. */
222 if (is_subref_array (e) && e->ts.u.cl->length)
224 gfc_se tmpse;
225 gfc_init_se (&tmpse, NULL);
226 gfc_conv_expr_type (&tmpse, e->ts.u.cl->length, gfc_charlen_type_node);
227 e->ts.u.cl->backend_decl = tmpse.expr;
228 return tmpse.expr;
231 /* First candidate: if the variable is of type CHARACTER, the
232 expression's length could be the length of the character
233 variable. */
234 if (e->symtree->n.sym->ts.type == BT_CHARACTER)
235 length = e->symtree->n.sym->ts.u.cl->backend_decl;
237 /* Look through the reference chain for component references. */
238 for (r = e->ref; r; r = r->next)
240 switch (r->type)
242 case REF_COMPONENT:
243 if (r->u.c.component->ts.type == BT_CHARACTER)
244 length = r->u.c.component->ts.u.cl->backend_decl;
245 break;
247 case REF_ARRAY:
248 /* Do nothing. */
249 break;
251 default:
252 /* We should never got substring references here. These will be
253 broken down by the scalarizer. */
254 gcc_unreachable ();
255 break;
259 gcc_assert (length != NULL);
260 return length;
264 /* Return for an expression the backend decl of the coarray. */
266 static tree
267 get_tree_for_caf_expr (gfc_expr *expr)
269 tree caf_decl = NULL_TREE;
270 gfc_ref *ref;
272 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE);
273 if (expr->symtree->n.sym->attr.codimension)
274 caf_decl = expr->symtree->n.sym->backend_decl;
276 for (ref = expr->ref; ref; ref = ref->next)
277 if (ref->type == REF_COMPONENT)
279 gfc_component *comp = ref->u.c.component;
280 if (comp->attr.pointer || comp->attr.allocatable)
281 caf_decl = NULL_TREE;
282 if (comp->attr.codimension)
283 caf_decl = comp->backend_decl;
286 gcc_assert (caf_decl != NULL_TREE);
287 return caf_decl;
291 /* For each character array constructor subexpression without a ts.u.cl->length,
292 replace it by its first element (if there aren't any elements, the length
293 should already be set to zero). */
295 static void
296 flatten_array_ctors_without_strlen (gfc_expr* e)
298 gfc_actual_arglist* arg;
299 gfc_constructor* c;
301 if (!e)
302 return;
304 switch (e->expr_type)
307 case EXPR_OP:
308 flatten_array_ctors_without_strlen (e->value.op.op1);
309 flatten_array_ctors_without_strlen (e->value.op.op2);
310 break;
312 case EXPR_COMPCALL:
313 /* TODO: Implement as with EXPR_FUNCTION when needed. */
314 gcc_unreachable ();
316 case EXPR_FUNCTION:
317 for (arg = e->value.function.actual; arg; arg = arg->next)
318 flatten_array_ctors_without_strlen (arg->expr);
319 break;
321 case EXPR_ARRAY:
323 /* We've found what we're looking for. */
324 if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
326 gfc_constructor *c;
327 gfc_expr* new_expr;
329 gcc_assert (e->value.constructor);
331 c = gfc_constructor_first (e->value.constructor);
332 new_expr = c->expr;
333 c->expr = NULL;
335 flatten_array_ctors_without_strlen (new_expr);
336 gfc_replace_expr (e, new_expr);
337 break;
340 /* Otherwise, fall through to handle constructor elements. */
341 case EXPR_STRUCTURE:
342 for (c = gfc_constructor_first (e->value.constructor);
343 c; c = gfc_constructor_next (c))
344 flatten_array_ctors_without_strlen (c->expr);
345 break;
347 default:
348 break;
354 /* Generate code to initialize a string length variable. Returns the
355 value. For array constructors, cl->length might be NULL and in this case,
356 the first element of the constructor is needed. expr is the original
357 expression so we can access it but can be NULL if this is not needed. */
359 void
360 gfc_conv_string_length (gfc_charlen * cl, gfc_expr * expr, stmtblock_t * pblock)
362 gfc_se se;
364 gfc_init_se (&se, NULL);
366 if (!cl->length
367 && cl->backend_decl
368 && TREE_CODE (cl->backend_decl) == VAR_DECL)
369 return;
371 /* If cl->length is NULL, use gfc_conv_expr to obtain the string length but
372 "flatten" array constructors by taking their first element; all elements
373 should be the same length or a cl->length should be present. */
374 if (!cl->length)
376 gfc_expr* expr_flat;
377 gcc_assert (expr);
378 expr_flat = gfc_copy_expr (expr);
379 flatten_array_ctors_without_strlen (expr_flat);
380 gfc_resolve_expr (expr_flat);
382 gfc_conv_expr (&se, expr_flat);
383 gfc_add_block_to_block (pblock, &se.pre);
384 cl->backend_decl = convert (gfc_charlen_type_node, se.string_length);
386 gfc_free_expr (expr_flat);
387 return;
390 /* Convert cl->length. */
392 gcc_assert (cl->length);
394 gfc_conv_expr_type (&se, cl->length, gfc_charlen_type_node);
395 se.expr = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
396 se.expr, build_int_cst (gfc_charlen_type_node, 0));
397 gfc_add_block_to_block (pblock, &se.pre);
399 if (cl->backend_decl)
400 gfc_add_modify (pblock, cl->backend_decl, se.expr);
401 else
402 cl->backend_decl = gfc_evaluate_now (se.expr, pblock);
406 static void
407 gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
408 const char *name, locus *where)
410 tree tmp;
411 tree type;
412 tree fault;
413 gfc_se start;
414 gfc_se end;
415 char *msg;
417 type = gfc_get_character_type (kind, ref->u.ss.length);
418 type = build_pointer_type (type);
420 gfc_init_se (&start, se);
421 gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
422 gfc_add_block_to_block (&se->pre, &start.pre);
424 if (integer_onep (start.expr))
425 gfc_conv_string_parameter (se);
426 else
428 tmp = start.expr;
429 STRIP_NOPS (tmp);
430 /* Avoid multiple evaluation of substring start. */
431 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
432 start.expr = gfc_evaluate_now (start.expr, &se->pre);
434 /* Change the start of the string. */
435 if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
436 tmp = se->expr;
437 else
438 tmp = build_fold_indirect_ref_loc (input_location,
439 se->expr);
440 tmp = gfc_build_array_ref (tmp, start.expr, NULL);
441 se->expr = gfc_build_addr_expr (type, tmp);
444 /* Length = end + 1 - start. */
445 gfc_init_se (&end, se);
446 if (ref->u.ss.end == NULL)
447 end.expr = se->string_length;
448 else
450 gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
451 gfc_add_block_to_block (&se->pre, &end.pre);
453 tmp = end.expr;
454 STRIP_NOPS (tmp);
455 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
456 end.expr = gfc_evaluate_now (end.expr, &se->pre);
458 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
460 tree nonempty = fold_build2_loc (input_location, LE_EXPR,
461 boolean_type_node, start.expr,
462 end.expr);
464 /* Check lower bound. */
465 fault = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
466 start.expr,
467 build_int_cst (gfc_charlen_type_node, 1));
468 fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
469 boolean_type_node, nonempty, fault);
470 if (name)
471 asprintf (&msg, "Substring out of bounds: lower bound (%%ld) of '%s' "
472 "is less than one", name);
473 else
474 asprintf (&msg, "Substring out of bounds: lower bound (%%ld)"
475 "is less than one");
476 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
477 fold_convert (long_integer_type_node,
478 start.expr));
479 free (msg);
481 /* Check upper bound. */
482 fault = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
483 end.expr, se->string_length);
484 fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
485 boolean_type_node, nonempty, fault);
486 if (name)
487 asprintf (&msg, "Substring out of bounds: upper bound (%%ld) of '%s' "
488 "exceeds string length (%%ld)", name);
489 else
490 asprintf (&msg, "Substring out of bounds: upper bound (%%ld) "
491 "exceeds string length (%%ld)");
492 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
493 fold_convert (long_integer_type_node, end.expr),
494 fold_convert (long_integer_type_node,
495 se->string_length));
496 free (msg);
499 /* If the start and end expressions are equal, the length is one. */
500 if (ref->u.ss.end
501 && gfc_dep_compare_expr (ref->u.ss.start, ref->u.ss.end) == 0)
502 tmp = build_int_cst (gfc_charlen_type_node, 1);
503 else
505 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_charlen_type_node,
506 end.expr, start.expr);
507 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_charlen_type_node,
508 build_int_cst (gfc_charlen_type_node, 1), tmp);
509 tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
510 tmp, build_int_cst (gfc_charlen_type_node, 0));
513 se->string_length = tmp;
517 /* Convert a derived type component reference. */
519 static void
520 gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
522 gfc_component *c;
523 tree tmp;
524 tree decl;
525 tree field;
527 c = ref->u.c.component;
529 gcc_assert (c->backend_decl);
531 field = c->backend_decl;
532 gcc_assert (TREE_CODE (field) == FIELD_DECL);
533 decl = se->expr;
535 /* Components can correspond to fields of different containing
536 types, as components are created without context, whereas
537 a concrete use of a component has the type of decl as context.
538 So, if the type doesn't match, we search the corresponding
539 FIELD_DECL in the parent type. To not waste too much time
540 we cache this result in norestrict_decl. */
542 if (DECL_FIELD_CONTEXT (field) != TREE_TYPE (decl))
544 tree f2 = c->norestrict_decl;
545 if (!f2 || DECL_FIELD_CONTEXT (f2) != TREE_TYPE (decl))
546 for (f2 = TYPE_FIELDS (TREE_TYPE (decl)); f2; f2 = DECL_CHAIN (f2))
547 if (TREE_CODE (f2) == FIELD_DECL
548 && DECL_NAME (f2) == DECL_NAME (field))
549 break;
550 gcc_assert (f2);
551 c->norestrict_decl = f2;
552 field = f2;
554 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
555 decl, field, NULL_TREE);
557 se->expr = tmp;
559 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer)
561 tmp = c->ts.u.cl->backend_decl;
562 /* Components must always be constant length. */
563 gcc_assert (tmp && INTEGER_CST_P (tmp));
564 se->string_length = tmp;
567 if (((c->attr.pointer || c->attr.allocatable) && c->attr.dimension == 0
568 && c->ts.type != BT_CHARACTER)
569 || c->attr.proc_pointer)
570 se->expr = build_fold_indirect_ref_loc (input_location,
571 se->expr);
575 /* This function deals with component references to components of the
576 parent type for derived type extensons. */
577 static void
578 conv_parent_component_references (gfc_se * se, gfc_ref * ref)
580 gfc_component *c;
581 gfc_component *cmp;
582 gfc_symbol *dt;
583 gfc_ref parent;
585 dt = ref->u.c.sym;
586 c = ref->u.c.component;
588 /* Return if the component is not in the parent type. */
589 for (cmp = dt->components; cmp; cmp = cmp->next)
590 if (strcmp (c->name, cmp->name) == 0)
591 return;
593 /* Build a gfc_ref to recursively call gfc_conv_component_ref. */
594 parent.type = REF_COMPONENT;
595 parent.next = NULL;
596 parent.u.c.sym = dt;
597 parent.u.c.component = dt->components;
599 if (dt->backend_decl == NULL)
600 gfc_get_derived_type (dt);
602 /* Build the reference and call self. */
603 gfc_conv_component_ref (se, &parent);
604 parent.u.c.sym = dt->components->ts.u.derived;
605 parent.u.c.component = c;
606 conv_parent_component_references (se, &parent);
609 /* Return the contents of a variable. Also handles reference/pointer
610 variables (all Fortran pointer references are implicit). */
612 static void
613 gfc_conv_variable (gfc_se * se, gfc_expr * expr)
615 gfc_ref *ref;
616 gfc_symbol *sym;
617 tree parent_decl = NULL_TREE;
618 int parent_flag;
619 bool return_value;
620 bool alternate_entry;
621 bool entry_master;
623 sym = expr->symtree->n.sym;
624 if (se->ss != NULL)
626 /* Check that something hasn't gone horribly wrong. */
627 gcc_assert (se->ss != gfc_ss_terminator);
628 gcc_assert (se->ss->expr == expr);
630 /* A scalarized term. We already know the descriptor. */
631 se->expr = se->ss->data.info.descriptor;
632 se->string_length = se->ss->string_length;
633 for (ref = se->ss->data.info.ref; ref; ref = ref->next)
634 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
635 break;
637 else
639 tree se_expr = NULL_TREE;
641 se->expr = gfc_get_symbol_decl (sym);
643 /* Deal with references to a parent results or entries by storing
644 the current_function_decl and moving to the parent_decl. */
645 return_value = sym->attr.function && sym->result == sym;
646 alternate_entry = sym->attr.function && sym->attr.entry
647 && sym->result == sym;
648 entry_master = sym->attr.result
649 && sym->ns->proc_name->attr.entry_master
650 && !gfc_return_by_reference (sym->ns->proc_name);
651 if (current_function_decl)
652 parent_decl = DECL_CONTEXT (current_function_decl);
654 if ((se->expr == parent_decl && return_value)
655 || (sym->ns && sym->ns->proc_name
656 && parent_decl
657 && sym->ns->proc_name->backend_decl == parent_decl
658 && (alternate_entry || entry_master)))
659 parent_flag = 1;
660 else
661 parent_flag = 0;
663 /* Special case for assigning the return value of a function.
664 Self recursive functions must have an explicit return value. */
665 if (return_value && (se->expr == current_function_decl || parent_flag))
666 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
668 /* Similarly for alternate entry points. */
669 else if (alternate_entry
670 && (sym->ns->proc_name->backend_decl == current_function_decl
671 || parent_flag))
673 gfc_entry_list *el = NULL;
675 for (el = sym->ns->entries; el; el = el->next)
676 if (sym == el->sym)
678 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
679 break;
683 else if (entry_master
684 && (sym->ns->proc_name->backend_decl == current_function_decl
685 || parent_flag))
686 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
688 if (se_expr)
689 se->expr = se_expr;
691 /* Procedure actual arguments. */
692 else if (sym->attr.flavor == FL_PROCEDURE
693 && se->expr != current_function_decl)
695 if (!sym->attr.dummy && !sym->attr.proc_pointer)
697 gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
698 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
700 return;
704 /* Dereference the expression, where needed. Since characters
705 are entirely different from other types, they are treated
706 separately. */
707 if (sym->ts.type == BT_CHARACTER)
709 /* Dereference character pointer dummy arguments
710 or results. */
711 if ((sym->attr.pointer || sym->attr.allocatable)
712 && (sym->attr.dummy
713 || sym->attr.function
714 || sym->attr.result))
715 se->expr = build_fold_indirect_ref_loc (input_location,
716 se->expr);
719 else if (!sym->attr.value)
721 /* Dereference non-character scalar dummy arguments. */
722 if (sym->attr.dummy && !sym->attr.dimension
723 && !(sym->attr.codimension && sym->attr.allocatable))
724 se->expr = build_fold_indirect_ref_loc (input_location,
725 se->expr);
727 /* Dereference scalar hidden result. */
728 if (gfc_option.flag_f2c && sym->ts.type == BT_COMPLEX
729 && (sym->attr.function || sym->attr.result)
730 && !sym->attr.dimension && !sym->attr.pointer
731 && !sym->attr.always_explicit)
732 se->expr = build_fold_indirect_ref_loc (input_location,
733 se->expr);
735 /* Dereference non-character pointer variables.
736 These must be dummies, results, or scalars. */
737 if ((sym->attr.pointer || sym->attr.allocatable
738 || gfc_is_associate_pointer (sym))
739 && (sym->attr.dummy
740 || sym->attr.function
741 || sym->attr.result
742 || (!sym->attr.dimension
743 && (!sym->attr.codimension || !sym->attr.allocatable))))
744 se->expr = build_fold_indirect_ref_loc (input_location,
745 se->expr);
748 ref = expr->ref;
751 /* For character variables, also get the length. */
752 if (sym->ts.type == BT_CHARACTER)
754 /* If the character length of an entry isn't set, get the length from
755 the master function instead. */
756 if (sym->attr.entry && !sym->ts.u.cl->backend_decl)
757 se->string_length = sym->ns->proc_name->ts.u.cl->backend_decl;
758 else
759 se->string_length = sym->ts.u.cl->backend_decl;
760 gcc_assert (se->string_length);
763 while (ref)
765 switch (ref->type)
767 case REF_ARRAY:
768 /* Return the descriptor if that's what we want and this is an array
769 section reference. */
770 if (se->descriptor_only && ref->u.ar.type != AR_ELEMENT)
771 return;
772 /* TODO: Pointers to single elements of array sections, eg elemental subs. */
773 /* Return the descriptor for array pointers and allocations. */
774 if (se->want_pointer
775 && ref->next == NULL && (se->descriptor_only))
776 return;
778 gfc_conv_array_ref (se, &ref->u.ar, sym, &expr->where);
779 /* Return a pointer to an element. */
780 break;
782 case REF_COMPONENT:
783 if (ref->u.c.sym->attr.extension)
784 conv_parent_component_references (se, ref);
786 gfc_conv_component_ref (se, ref);
787 break;
789 case REF_SUBSTRING:
790 gfc_conv_substring (se, ref, expr->ts.kind,
791 expr->symtree->name, &expr->where);
792 break;
794 default:
795 gcc_unreachable ();
796 break;
798 ref = ref->next;
800 /* Pointer assignment, allocation or pass by reference. Arrays are handled
801 separately. */
802 if (se->want_pointer)
804 if (expr->ts.type == BT_CHARACTER && !gfc_is_proc_ptr_comp (expr, NULL))
805 gfc_conv_string_parameter (se);
806 else
807 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
812 /* Unary ops are easy... Or they would be if ! was a valid op. */
814 static void
815 gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
817 gfc_se operand;
818 tree type;
820 gcc_assert (expr->ts.type != BT_CHARACTER);
821 /* Initialize the operand. */
822 gfc_init_se (&operand, se);
823 gfc_conv_expr_val (&operand, expr->value.op.op1);
824 gfc_add_block_to_block (&se->pre, &operand.pre);
826 type = gfc_typenode_for_spec (&expr->ts);
828 /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
829 We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
830 All other unary operators have an equivalent GIMPLE unary operator. */
831 if (code == TRUTH_NOT_EXPR)
832 se->expr = fold_build2_loc (input_location, EQ_EXPR, type, operand.expr,
833 build_int_cst (type, 0));
834 else
835 se->expr = fold_build1_loc (input_location, code, type, operand.expr);
839 /* Expand power operator to optimal multiplications when a value is raised
840 to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
841 Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
842 Programming", 3rd Edition, 1998. */
844 /* This code is mostly duplicated from expand_powi in the backend.
845 We establish the "optimal power tree" lookup table with the defined size.
846 The items in the table are the exponents used to calculate the index
847 exponents. Any integer n less than the value can get an "addition chain",
848 with the first node being one. */
849 #define POWI_TABLE_SIZE 256
851 /* The table is from builtins.c. */
852 static const unsigned char powi_table[POWI_TABLE_SIZE] =
854 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
855 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
856 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
857 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
858 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
859 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
860 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
861 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
862 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
863 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
864 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
865 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
866 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
867 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
868 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
869 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
870 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
871 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
872 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
873 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
874 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
875 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
876 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
877 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
878 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
879 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
880 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
881 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
882 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
883 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
884 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
885 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
888 /* If n is larger than lookup table's max index, we use the "window
889 method". */
890 #define POWI_WINDOW_SIZE 3
892 /* Recursive function to expand the power operator. The temporary
893 values are put in tmpvar. The function returns tmpvar[1] ** n. */
894 static tree
895 gfc_conv_powi (gfc_se * se, unsigned HOST_WIDE_INT n, tree * tmpvar)
897 tree op0;
898 tree op1;
899 tree tmp;
900 int digit;
902 if (n < POWI_TABLE_SIZE)
904 if (tmpvar[n])
905 return tmpvar[n];
907 op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
908 op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
910 else if (n & 1)
912 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
913 op0 = gfc_conv_powi (se, n - digit, tmpvar);
914 op1 = gfc_conv_powi (se, digit, tmpvar);
916 else
918 op0 = gfc_conv_powi (se, n >> 1, tmpvar);
919 op1 = op0;
922 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (op0), op0, op1);
923 tmp = gfc_evaluate_now (tmp, &se->pre);
925 if (n < POWI_TABLE_SIZE)
926 tmpvar[n] = tmp;
928 return tmp;
932 /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
933 return 1. Else return 0 and a call to runtime library functions
934 will have to be built. */
935 static int
936 gfc_conv_cst_int_power (gfc_se * se, tree lhs, tree rhs)
938 tree cond;
939 tree tmp;
940 tree type;
941 tree vartmp[POWI_TABLE_SIZE];
942 HOST_WIDE_INT m;
943 unsigned HOST_WIDE_INT n;
944 int sgn;
946 /* If exponent is too large, we won't expand it anyway, so don't bother
947 with large integer values. */
948 if (!double_int_fits_in_shwi_p (TREE_INT_CST (rhs)))
949 return 0;
951 m = double_int_to_shwi (TREE_INT_CST (rhs));
952 /* There's no ABS for HOST_WIDE_INT, so here we go. It also takes care
953 of the asymmetric range of the integer type. */
954 n = (unsigned HOST_WIDE_INT) (m < 0 ? -m : m);
956 type = TREE_TYPE (lhs);
957 sgn = tree_int_cst_sgn (rhs);
959 if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
960 || optimize_size) && (m > 2 || m < -1))
961 return 0;
963 /* rhs == 0 */
964 if (sgn == 0)
966 se->expr = gfc_build_const (type, integer_one_node);
967 return 1;
970 /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1. */
971 if ((sgn == -1) && (TREE_CODE (type) == INTEGER_TYPE))
973 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
974 lhs, build_int_cst (TREE_TYPE (lhs), -1));
975 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
976 lhs, build_int_cst (TREE_TYPE (lhs), 1));
978 /* If rhs is even,
979 result = (lhs == 1 || lhs == -1) ? 1 : 0. */
980 if ((n & 1) == 0)
982 tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR,
983 boolean_type_node, tmp, cond);
984 se->expr = fold_build3_loc (input_location, COND_EXPR, type,
985 tmp, build_int_cst (type, 1),
986 build_int_cst (type, 0));
987 return 1;
989 /* If rhs is odd,
990 result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0. */
991 tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp,
992 build_int_cst (type, -1),
993 build_int_cst (type, 0));
994 se->expr = fold_build3_loc (input_location, COND_EXPR, type,
995 cond, build_int_cst (type, 1), tmp);
996 return 1;
999 memset (vartmp, 0, sizeof (vartmp));
1000 vartmp[1] = lhs;
1001 if (sgn == -1)
1003 tmp = gfc_build_const (type, integer_one_node);
1004 vartmp[1] = fold_build2_loc (input_location, RDIV_EXPR, type, tmp,
1005 vartmp[1]);
1008 se->expr = gfc_conv_powi (se, n, vartmp);
1010 return 1;
1014 /* Power op (**). Constant integer exponent has special handling. */
1016 static void
1017 gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
1019 tree gfc_int4_type_node;
1020 int kind;
1021 int ikind;
1022 int res_ikind_1, res_ikind_2;
1023 gfc_se lse;
1024 gfc_se rse;
1025 tree fndecl = NULL;
1027 gfc_init_se (&lse, se);
1028 gfc_conv_expr_val (&lse, expr->value.op.op1);
1029 lse.expr = gfc_evaluate_now (lse.expr, &lse.pre);
1030 gfc_add_block_to_block (&se->pre, &lse.pre);
1032 gfc_init_se (&rse, se);
1033 gfc_conv_expr_val (&rse, expr->value.op.op2);
1034 gfc_add_block_to_block (&se->pre, &rse.pre);
1036 if (expr->value.op.op2->ts.type == BT_INTEGER
1037 && expr->value.op.op2->expr_type == EXPR_CONSTANT)
1038 if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
1039 return;
1041 gfc_int4_type_node = gfc_get_int_type (4);
1043 /* In case of integer operands with kinds 1 or 2, we call the integer kind 4
1044 library routine. But in the end, we have to convert the result back
1045 if this case applies -- with res_ikind_K, we keep track whether operand K
1046 falls into this case. */
1047 res_ikind_1 = -1;
1048 res_ikind_2 = -1;
1050 kind = expr->value.op.op1->ts.kind;
1051 switch (expr->value.op.op2->ts.type)
1053 case BT_INTEGER:
1054 ikind = expr->value.op.op2->ts.kind;
1055 switch (ikind)
1057 case 1:
1058 case 2:
1059 rse.expr = convert (gfc_int4_type_node, rse.expr);
1060 res_ikind_2 = ikind;
1061 /* Fall through. */
1063 case 4:
1064 ikind = 0;
1065 break;
1067 case 8:
1068 ikind = 1;
1069 break;
1071 case 16:
1072 ikind = 2;
1073 break;
1075 default:
1076 gcc_unreachable ();
1078 switch (kind)
1080 case 1:
1081 case 2:
1082 if (expr->value.op.op1->ts.type == BT_INTEGER)
1084 lse.expr = convert (gfc_int4_type_node, lse.expr);
1085 res_ikind_1 = kind;
1087 else
1088 gcc_unreachable ();
1089 /* Fall through. */
1091 case 4:
1092 kind = 0;
1093 break;
1095 case 8:
1096 kind = 1;
1097 break;
1099 case 10:
1100 kind = 2;
1101 break;
1103 case 16:
1104 kind = 3;
1105 break;
1107 default:
1108 gcc_unreachable ();
1111 switch (expr->value.op.op1->ts.type)
1113 case BT_INTEGER:
1114 if (kind == 3) /* Case 16 was not handled properly above. */
1115 kind = 2;
1116 fndecl = gfor_fndecl_math_powi[kind][ikind].integer;
1117 break;
1119 case BT_REAL:
1120 /* Use builtins for real ** int4. */
1121 if (ikind == 0)
1123 switch (kind)
1125 case 0:
1126 fndecl = built_in_decls[BUILT_IN_POWIF];
1127 break;
1129 case 1:
1130 fndecl = built_in_decls[BUILT_IN_POWI];
1131 break;
1133 case 2:
1134 fndecl = built_in_decls[BUILT_IN_POWIL];
1135 break;
1137 case 3:
1138 /* Use the __builtin_powil() only if real(kind=16) is
1139 actually the C long double type. */
1140 if (!gfc_real16_is_float128)
1141 fndecl = built_in_decls[BUILT_IN_POWIL];
1142 break;
1144 default:
1145 gcc_unreachable ();
1149 /* If we don't have a good builtin for this, go for the
1150 library function. */
1151 if (!fndecl)
1152 fndecl = gfor_fndecl_math_powi[kind][ikind].real;
1153 break;
1155 case BT_COMPLEX:
1156 fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
1157 break;
1159 default:
1160 gcc_unreachable ();
1162 break;
1164 case BT_REAL:
1165 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_POW, kind);
1166 break;
1168 case BT_COMPLEX:
1169 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_CPOW, kind);
1170 break;
1172 default:
1173 gcc_unreachable ();
1174 break;
1177 se->expr = build_call_expr_loc (input_location,
1178 fndecl, 2, lse.expr, rse.expr);
1180 /* Convert the result back if it is of wrong integer kind. */
1181 if (res_ikind_1 != -1 && res_ikind_2 != -1)
1183 /* We want the maximum of both operand kinds as result. */
1184 if (res_ikind_1 < res_ikind_2)
1185 res_ikind_1 = res_ikind_2;
1186 se->expr = convert (gfc_get_int_type (res_ikind_1), se->expr);
1191 /* Generate code to allocate a string temporary. */
1193 tree
1194 gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
1196 tree var;
1197 tree tmp;
1199 if (gfc_can_put_var_on_stack (len))
1201 /* Create a temporary variable to hold the result. */
1202 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1203 gfc_charlen_type_node, len,
1204 build_int_cst (gfc_charlen_type_node, 1));
1205 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node, tmp);
1207 if (TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
1208 tmp = build_array_type (TREE_TYPE (TREE_TYPE (type)), tmp);
1209 else
1210 tmp = build_array_type (TREE_TYPE (type), tmp);
1212 var = gfc_create_var (tmp, "str");
1213 var = gfc_build_addr_expr (type, var);
1215 else
1217 /* Allocate a temporary to hold the result. */
1218 var = gfc_create_var (type, "pstr");
1219 tmp = gfc_call_malloc (&se->pre, type,
1220 fold_build2_loc (input_location, MULT_EXPR,
1221 TREE_TYPE (len), len,
1222 fold_convert (TREE_TYPE (len),
1223 TYPE_SIZE (type))));
1224 gfc_add_modify (&se->pre, var, tmp);
1226 /* Free the temporary afterwards. */
1227 tmp = gfc_call_free (convert (pvoid_type_node, var));
1228 gfc_add_expr_to_block (&se->post, tmp);
1231 return var;
1235 /* Handle a string concatenation operation. A temporary will be allocated to
1236 hold the result. */
1238 static void
1239 gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
1241 gfc_se lse, rse;
1242 tree len, type, var, tmp, fndecl;
1244 gcc_assert (expr->value.op.op1->ts.type == BT_CHARACTER
1245 && expr->value.op.op2->ts.type == BT_CHARACTER);
1246 gcc_assert (expr->value.op.op1->ts.kind == expr->value.op.op2->ts.kind);
1248 gfc_init_se (&lse, se);
1249 gfc_conv_expr (&lse, expr->value.op.op1);
1250 gfc_conv_string_parameter (&lse);
1251 gfc_init_se (&rse, se);
1252 gfc_conv_expr (&rse, expr->value.op.op2);
1253 gfc_conv_string_parameter (&rse);
1255 gfc_add_block_to_block (&se->pre, &lse.pre);
1256 gfc_add_block_to_block (&se->pre, &rse.pre);
1258 type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
1259 len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
1260 if (len == NULL_TREE)
1262 len = fold_build2_loc (input_location, PLUS_EXPR,
1263 TREE_TYPE (lse.string_length),
1264 lse.string_length, rse.string_length);
1267 type = build_pointer_type (type);
1269 var = gfc_conv_string_tmp (se, type, len);
1271 /* Do the actual concatenation. */
1272 if (expr->ts.kind == 1)
1273 fndecl = gfor_fndecl_concat_string;
1274 else if (expr->ts.kind == 4)
1275 fndecl = gfor_fndecl_concat_string_char4;
1276 else
1277 gcc_unreachable ();
1279 tmp = build_call_expr_loc (input_location,
1280 fndecl, 6, len, var, lse.string_length, lse.expr,
1281 rse.string_length, rse.expr);
1282 gfc_add_expr_to_block (&se->pre, tmp);
1284 /* Add the cleanup for the operands. */
1285 gfc_add_block_to_block (&se->pre, &rse.post);
1286 gfc_add_block_to_block (&se->pre, &lse.post);
1288 se->expr = var;
1289 se->string_length = len;
1292 /* Translates an op expression. Common (binary) cases are handled by this
1293 function, others are passed on. Recursion is used in either case.
1294 We use the fact that (op1.ts == op2.ts) (except for the power
1295 operator **).
1296 Operators need no special handling for scalarized expressions as long as
1297 they call gfc_conv_simple_val to get their operands.
1298 Character strings get special handling. */
1300 static void
1301 gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
1303 enum tree_code code;
1304 gfc_se lse;
1305 gfc_se rse;
1306 tree tmp, type;
1307 int lop;
1308 int checkstring;
1310 checkstring = 0;
1311 lop = 0;
1312 switch (expr->value.op.op)
1314 case INTRINSIC_PARENTHESES:
1315 if ((expr->ts.type == BT_REAL
1316 || expr->ts.type == BT_COMPLEX)
1317 && gfc_option.flag_protect_parens)
1319 gfc_conv_unary_op (PAREN_EXPR, se, expr);
1320 gcc_assert (FLOAT_TYPE_P (TREE_TYPE (se->expr)));
1321 return;
1324 /* Fallthrough. */
1325 case INTRINSIC_UPLUS:
1326 gfc_conv_expr (se, expr->value.op.op1);
1327 return;
1329 case INTRINSIC_UMINUS:
1330 gfc_conv_unary_op (NEGATE_EXPR, se, expr);
1331 return;
1333 case INTRINSIC_NOT:
1334 gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
1335 return;
1337 case INTRINSIC_PLUS:
1338 code = PLUS_EXPR;
1339 break;
1341 case INTRINSIC_MINUS:
1342 code = MINUS_EXPR;
1343 break;
1345 case INTRINSIC_TIMES:
1346 code = MULT_EXPR;
1347 break;
1349 case INTRINSIC_DIVIDE:
1350 /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
1351 an integer, we must round towards zero, so we use a
1352 TRUNC_DIV_EXPR. */
1353 if (expr->ts.type == BT_INTEGER)
1354 code = TRUNC_DIV_EXPR;
1355 else
1356 code = RDIV_EXPR;
1357 break;
1359 case INTRINSIC_POWER:
1360 gfc_conv_power_op (se, expr);
1361 return;
1363 case INTRINSIC_CONCAT:
1364 gfc_conv_concat_op (se, expr);
1365 return;
1367 case INTRINSIC_AND:
1368 code = TRUTH_ANDIF_EXPR;
1369 lop = 1;
1370 break;
1372 case INTRINSIC_OR:
1373 code = TRUTH_ORIF_EXPR;
1374 lop = 1;
1375 break;
1377 /* EQV and NEQV only work on logicals, but since we represent them
1378 as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE. */
1379 case INTRINSIC_EQ:
1380 case INTRINSIC_EQ_OS:
1381 case INTRINSIC_EQV:
1382 code = EQ_EXPR;
1383 checkstring = 1;
1384 lop = 1;
1385 break;
1387 case INTRINSIC_NE:
1388 case INTRINSIC_NE_OS:
1389 case INTRINSIC_NEQV:
1390 code = NE_EXPR;
1391 checkstring = 1;
1392 lop = 1;
1393 break;
1395 case INTRINSIC_GT:
1396 case INTRINSIC_GT_OS:
1397 code = GT_EXPR;
1398 checkstring = 1;
1399 lop = 1;
1400 break;
1402 case INTRINSIC_GE:
1403 case INTRINSIC_GE_OS:
1404 code = GE_EXPR;
1405 checkstring = 1;
1406 lop = 1;
1407 break;
1409 case INTRINSIC_LT:
1410 case INTRINSIC_LT_OS:
1411 code = LT_EXPR;
1412 checkstring = 1;
1413 lop = 1;
1414 break;
1416 case INTRINSIC_LE:
1417 case INTRINSIC_LE_OS:
1418 code = LE_EXPR;
1419 checkstring = 1;
1420 lop = 1;
1421 break;
1423 case INTRINSIC_USER:
1424 case INTRINSIC_ASSIGN:
1425 /* These should be converted into function calls by the frontend. */
1426 gcc_unreachable ();
1428 default:
1429 fatal_error ("Unknown intrinsic op");
1430 return;
1433 /* The only exception to this is **, which is handled separately anyway. */
1434 gcc_assert (expr->value.op.op1->ts.type == expr->value.op.op2->ts.type);
1436 if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
1437 checkstring = 0;
1439 /* lhs */
1440 gfc_init_se (&lse, se);
1441 gfc_conv_expr (&lse, expr->value.op.op1);
1442 gfc_add_block_to_block (&se->pre, &lse.pre);
1444 /* rhs */
1445 gfc_init_se (&rse, se);
1446 gfc_conv_expr (&rse, expr->value.op.op2);
1447 gfc_add_block_to_block (&se->pre, &rse.pre);
1449 if (checkstring)
1451 gfc_conv_string_parameter (&lse);
1452 gfc_conv_string_parameter (&rse);
1454 lse.expr = gfc_build_compare_string (lse.string_length, lse.expr,
1455 rse.string_length, rse.expr,
1456 expr->value.op.op1->ts.kind,
1457 code);
1458 rse.expr = build_int_cst (TREE_TYPE (lse.expr), 0);
1459 gfc_add_block_to_block (&lse.post, &rse.post);
1462 type = gfc_typenode_for_spec (&expr->ts);
1464 if (lop)
1466 /* The result of logical ops is always boolean_type_node. */
1467 tmp = fold_build2_loc (input_location, code, boolean_type_node,
1468 lse.expr, rse.expr);
1469 se->expr = convert (type, tmp);
1471 else
1472 se->expr = fold_build2_loc (input_location, code, type, lse.expr, rse.expr);
1474 /* Add the post blocks. */
1475 gfc_add_block_to_block (&se->post, &rse.post);
1476 gfc_add_block_to_block (&se->post, &lse.post);
1479 /* If a string's length is one, we convert it to a single character. */
1481 tree
1482 gfc_string_to_single_character (tree len, tree str, int kind)
1485 if (!INTEGER_CST_P (len) || TREE_INT_CST_HIGH (len) != 0
1486 || !POINTER_TYPE_P (TREE_TYPE (str)))
1487 return NULL_TREE;
1489 if (TREE_INT_CST_LOW (len) == 1)
1491 str = fold_convert (gfc_get_pchar_type (kind), str);
1492 return build_fold_indirect_ref_loc (input_location, str);
1495 if (kind == 1
1496 && TREE_CODE (str) == ADDR_EXPR
1497 && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
1498 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
1499 && array_ref_low_bound (TREE_OPERAND (str, 0))
1500 == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
1501 && TREE_INT_CST_LOW (len) > 1
1502 && TREE_INT_CST_LOW (len)
1503 == (unsigned HOST_WIDE_INT)
1504 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
1506 tree ret = fold_convert (gfc_get_pchar_type (kind), str);
1507 ret = build_fold_indirect_ref_loc (input_location, ret);
1508 if (TREE_CODE (ret) == INTEGER_CST)
1510 tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
1511 int i, length = TREE_STRING_LENGTH (string_cst);
1512 const char *ptr = TREE_STRING_POINTER (string_cst);
1514 for (i = 1; i < length; i++)
1515 if (ptr[i] != ' ')
1516 return NULL_TREE;
1518 return ret;
1522 return NULL_TREE;
1526 void
1527 gfc_conv_scalar_char_value (gfc_symbol *sym, gfc_se *se, gfc_expr **expr)
1530 if (sym->backend_decl)
1532 /* This becomes the nominal_type in
1533 function.c:assign_parm_find_data_types. */
1534 TREE_TYPE (sym->backend_decl) = unsigned_char_type_node;
1535 /* This becomes the passed_type in
1536 function.c:assign_parm_find_data_types. C promotes char to
1537 integer for argument passing. */
1538 DECL_ARG_TYPE (sym->backend_decl) = unsigned_type_node;
1540 DECL_BY_REFERENCE (sym->backend_decl) = 0;
1543 if (expr != NULL)
1545 /* If we have a constant character expression, make it into an
1546 integer. */
1547 if ((*expr)->expr_type == EXPR_CONSTANT)
1549 gfc_typespec ts;
1550 gfc_clear_ts (&ts);
1552 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL,
1553 (int)(*expr)->value.character.string[0]);
1554 if ((*expr)->ts.kind != gfc_c_int_kind)
1556 /* The expr needs to be compatible with a C int. If the
1557 conversion fails, then the 2 causes an ICE. */
1558 ts.type = BT_INTEGER;
1559 ts.kind = gfc_c_int_kind;
1560 gfc_convert_type (*expr, &ts, 2);
1563 else if (se != NULL && (*expr)->expr_type == EXPR_VARIABLE)
1565 if ((*expr)->ref == NULL)
1567 se->expr = gfc_string_to_single_character
1568 (build_int_cst (integer_type_node, 1),
1569 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
1570 gfc_get_symbol_decl
1571 ((*expr)->symtree->n.sym)),
1572 (*expr)->ts.kind);
1574 else
1576 gfc_conv_variable (se, *expr);
1577 se->expr = gfc_string_to_single_character
1578 (build_int_cst (integer_type_node, 1),
1579 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
1580 se->expr),
1581 (*expr)->ts.kind);
1587 /* Helper function for gfc_build_compare_string. Return LEN_TRIM value
1588 if STR is a string literal, otherwise return -1. */
1590 static int
1591 gfc_optimize_len_trim (tree len, tree str, int kind)
1593 if (kind == 1
1594 && TREE_CODE (str) == ADDR_EXPR
1595 && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
1596 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
1597 && array_ref_low_bound (TREE_OPERAND (str, 0))
1598 == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
1599 && TREE_INT_CST_LOW (len) >= 1
1600 && TREE_INT_CST_LOW (len)
1601 == (unsigned HOST_WIDE_INT)
1602 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
1604 tree folded = fold_convert (gfc_get_pchar_type (kind), str);
1605 folded = build_fold_indirect_ref_loc (input_location, folded);
1606 if (TREE_CODE (folded) == INTEGER_CST)
1608 tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
1609 int length = TREE_STRING_LENGTH (string_cst);
1610 const char *ptr = TREE_STRING_POINTER (string_cst);
1612 for (; length > 0; length--)
1613 if (ptr[length - 1] != ' ')
1614 break;
1616 return length;
1619 return -1;
1622 /* Compare two strings. If they are all single characters, the result is the
1623 subtraction of them. Otherwise, we build a library call. */
1625 tree
1626 gfc_build_compare_string (tree len1, tree str1, tree len2, tree str2, int kind,
1627 enum tree_code code)
1629 tree sc1;
1630 tree sc2;
1631 tree fndecl;
1633 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1)));
1634 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2)));
1636 sc1 = gfc_string_to_single_character (len1, str1, kind);
1637 sc2 = gfc_string_to_single_character (len2, str2, kind);
1639 if (sc1 != NULL_TREE && sc2 != NULL_TREE)
1641 /* Deal with single character specially. */
1642 sc1 = fold_convert (integer_type_node, sc1);
1643 sc2 = fold_convert (integer_type_node, sc2);
1644 return fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
1645 sc1, sc2);
1648 if ((code == EQ_EXPR || code == NE_EXPR)
1649 && optimize
1650 && INTEGER_CST_P (len1) && INTEGER_CST_P (len2))
1652 /* If one string is a string literal with LEN_TRIM longer
1653 than the length of the second string, the strings
1654 compare unequal. */
1655 int len = gfc_optimize_len_trim (len1, str1, kind);
1656 if (len > 0 && compare_tree_int (len2, len) < 0)
1657 return integer_one_node;
1658 len = gfc_optimize_len_trim (len2, str2, kind);
1659 if (len > 0 && compare_tree_int (len1, len) < 0)
1660 return integer_one_node;
1663 /* Build a call for the comparison. */
1664 if (kind == 1)
1665 fndecl = gfor_fndecl_compare_string;
1666 else if (kind == 4)
1667 fndecl = gfor_fndecl_compare_string_char4;
1668 else
1669 gcc_unreachable ();
1671 return build_call_expr_loc (input_location, fndecl, 4,
1672 len1, str1, len2, str2);
1676 /* Return the backend_decl for a procedure pointer component. */
1678 static tree
1679 get_proc_ptr_comp (gfc_expr *e)
1681 gfc_se comp_se;
1682 gfc_expr *e2;
1683 expr_t old_type;
1685 gfc_init_se (&comp_se, NULL);
1686 e2 = gfc_copy_expr (e);
1687 /* We have to restore the expr type later so that gfc_free_expr frees
1688 the exact same thing that was allocated.
1689 TODO: This is ugly. */
1690 old_type = e2->expr_type;
1691 e2->expr_type = EXPR_VARIABLE;
1692 gfc_conv_expr (&comp_se, e2);
1693 e2->expr_type = old_type;
1694 gfc_free_expr (e2);
1695 return build_fold_addr_expr_loc (input_location, comp_se.expr);
1699 static void
1700 conv_function_val (gfc_se * se, gfc_symbol * sym, gfc_expr * expr)
1702 tree tmp;
1704 if (gfc_is_proc_ptr_comp (expr, NULL))
1705 tmp = get_proc_ptr_comp (expr);
1706 else if (sym->attr.dummy)
1708 tmp = gfc_get_symbol_decl (sym);
1709 if (sym->attr.proc_pointer)
1710 tmp = build_fold_indirect_ref_loc (input_location,
1711 tmp);
1712 gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE
1713 && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) == FUNCTION_TYPE);
1715 else
1717 if (!sym->backend_decl)
1718 sym->backend_decl = gfc_get_extern_function_decl (sym);
1720 tmp = sym->backend_decl;
1722 if (sym->attr.cray_pointee)
1724 /* TODO - make the cray pointee a pointer to a procedure,
1725 assign the pointer to it and use it for the call. This
1726 will do for now! */
1727 tmp = convert (build_pointer_type (TREE_TYPE (tmp)),
1728 gfc_get_symbol_decl (sym->cp_pointer));
1729 tmp = gfc_evaluate_now (tmp, &se->pre);
1732 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
1734 gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
1735 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
1738 se->expr = tmp;
1742 /* Initialize MAPPING. */
1744 void
1745 gfc_init_interface_mapping (gfc_interface_mapping * mapping)
1747 mapping->syms = NULL;
1748 mapping->charlens = NULL;
1752 /* Free all memory held by MAPPING (but not MAPPING itself). */
1754 void
1755 gfc_free_interface_mapping (gfc_interface_mapping * mapping)
1757 gfc_interface_sym_mapping *sym;
1758 gfc_interface_sym_mapping *nextsym;
1759 gfc_charlen *cl;
1760 gfc_charlen *nextcl;
1762 for (sym = mapping->syms; sym; sym = nextsym)
1764 nextsym = sym->next;
1765 sym->new_sym->n.sym->formal = NULL;
1766 gfc_free_symbol (sym->new_sym->n.sym);
1767 gfc_free_expr (sym->expr);
1768 free (sym->new_sym);
1769 free (sym);
1771 for (cl = mapping->charlens; cl; cl = nextcl)
1773 nextcl = cl->next;
1774 gfc_free_expr (cl->length);
1775 free (cl);
1780 /* Return a copy of gfc_charlen CL. Add the returned structure to
1781 MAPPING so that it will be freed by gfc_free_interface_mapping. */
1783 static gfc_charlen *
1784 gfc_get_interface_mapping_charlen (gfc_interface_mapping * mapping,
1785 gfc_charlen * cl)
1787 gfc_charlen *new_charlen;
1789 new_charlen = gfc_get_charlen ();
1790 new_charlen->next = mapping->charlens;
1791 new_charlen->length = gfc_copy_expr (cl->length);
1793 mapping->charlens = new_charlen;
1794 return new_charlen;
1798 /* A subroutine of gfc_add_interface_mapping. Return a descriptorless
1799 array variable that can be used as the actual argument for dummy
1800 argument SYM. Add any initialization code to BLOCK. PACKED is as
1801 for gfc_get_nodesc_array_type and DATA points to the first element
1802 in the passed array. */
1804 static tree
1805 gfc_get_interface_mapping_array (stmtblock_t * block, gfc_symbol * sym,
1806 gfc_packed packed, tree data)
1808 tree type;
1809 tree var;
1811 type = gfc_typenode_for_spec (&sym->ts);
1812 type = gfc_get_nodesc_array_type (type, sym->as, packed,
1813 !sym->attr.target && !sym->attr.pointer
1814 && !sym->attr.proc_pointer);
1816 var = gfc_create_var (type, "ifm");
1817 gfc_add_modify (block, var, fold_convert (type, data));
1819 return var;
1823 /* A subroutine of gfc_add_interface_mapping. Set the stride, upper bounds
1824 and offset of descriptorless array type TYPE given that it has the same
1825 size as DESC. Add any set-up code to BLOCK. */
1827 static void
1828 gfc_set_interface_mapping_bounds (stmtblock_t * block, tree type, tree desc)
1830 int n;
1831 tree dim;
1832 tree offset;
1833 tree tmp;
1835 offset = gfc_index_zero_node;
1836 for (n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
1838 dim = gfc_rank_cst[n];
1839 GFC_TYPE_ARRAY_STRIDE (type, n) = gfc_conv_array_stride (desc, n);
1840 if (GFC_TYPE_ARRAY_LBOUND (type, n) == NULL_TREE)
1842 GFC_TYPE_ARRAY_LBOUND (type, n)
1843 = gfc_conv_descriptor_lbound_get (desc, dim);
1844 GFC_TYPE_ARRAY_UBOUND (type, n)
1845 = gfc_conv_descriptor_ubound_get (desc, dim);
1847 else if (GFC_TYPE_ARRAY_UBOUND (type, n) == NULL_TREE)
1849 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1850 gfc_array_index_type,
1851 gfc_conv_descriptor_ubound_get (desc, dim),
1852 gfc_conv_descriptor_lbound_get (desc, dim));
1853 tmp = fold_build2_loc (input_location, PLUS_EXPR,
1854 gfc_array_index_type,
1855 GFC_TYPE_ARRAY_LBOUND (type, n), tmp);
1856 tmp = gfc_evaluate_now (tmp, block);
1857 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1859 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
1860 GFC_TYPE_ARRAY_LBOUND (type, n),
1861 GFC_TYPE_ARRAY_STRIDE (type, n));
1862 offset = fold_build2_loc (input_location, MINUS_EXPR,
1863 gfc_array_index_type, offset, tmp);
1865 offset = gfc_evaluate_now (offset, block);
1866 GFC_TYPE_ARRAY_OFFSET (type) = offset;
1870 /* Extend MAPPING so that it maps dummy argument SYM to the value stored
1871 in SE. The caller may still use se->expr and se->string_length after
1872 calling this function. */
1874 void
1875 gfc_add_interface_mapping (gfc_interface_mapping * mapping,
1876 gfc_symbol * sym, gfc_se * se,
1877 gfc_expr *expr)
1879 gfc_interface_sym_mapping *sm;
1880 tree desc;
1881 tree tmp;
1882 tree value;
1883 gfc_symbol *new_sym;
1884 gfc_symtree *root;
1885 gfc_symtree *new_symtree;
1887 /* Create a new symbol to represent the actual argument. */
1888 new_sym = gfc_new_symbol (sym->name, NULL);
1889 new_sym->ts = sym->ts;
1890 new_sym->as = gfc_copy_array_spec (sym->as);
1891 new_sym->attr.referenced = 1;
1892 new_sym->attr.dimension = sym->attr.dimension;
1893 new_sym->attr.contiguous = sym->attr.contiguous;
1894 new_sym->attr.codimension = sym->attr.codimension;
1895 new_sym->attr.pointer = sym->attr.pointer;
1896 new_sym->attr.allocatable = sym->attr.allocatable;
1897 new_sym->attr.flavor = sym->attr.flavor;
1898 new_sym->attr.function = sym->attr.function;
1900 /* Ensure that the interface is available and that
1901 descriptors are passed for array actual arguments. */
1902 if (sym->attr.flavor == FL_PROCEDURE)
1904 new_sym->formal = expr->symtree->n.sym->formal;
1905 new_sym->attr.always_explicit
1906 = expr->symtree->n.sym->attr.always_explicit;
1909 /* Create a fake symtree for it. */
1910 root = NULL;
1911 new_symtree = gfc_new_symtree (&root, sym->name);
1912 new_symtree->n.sym = new_sym;
1913 gcc_assert (new_symtree == root);
1915 /* Create a dummy->actual mapping. */
1916 sm = XCNEW (gfc_interface_sym_mapping);
1917 sm->next = mapping->syms;
1918 sm->old = sym;
1919 sm->new_sym = new_symtree;
1920 sm->expr = gfc_copy_expr (expr);
1921 mapping->syms = sm;
1923 /* Stabilize the argument's value. */
1924 if (!sym->attr.function && se)
1925 se->expr = gfc_evaluate_now (se->expr, &se->pre);
1927 if (sym->ts.type == BT_CHARACTER)
1929 /* Create a copy of the dummy argument's length. */
1930 new_sym->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, sym->ts.u.cl);
1931 sm->expr->ts.u.cl = new_sym->ts.u.cl;
1933 /* If the length is specified as "*", record the length that
1934 the caller is passing. We should use the callee's length
1935 in all other cases. */
1936 if (!new_sym->ts.u.cl->length && se)
1938 se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
1939 new_sym->ts.u.cl->backend_decl = se->string_length;
1943 if (!se)
1944 return;
1946 /* Use the passed value as-is if the argument is a function. */
1947 if (sym->attr.flavor == FL_PROCEDURE)
1948 value = se->expr;
1950 /* If the argument is either a string or a pointer to a string,
1951 convert it to a boundless character type. */
1952 else if (!sym->attr.dimension && sym->ts.type == BT_CHARACTER)
1954 tmp = gfc_get_character_type_len (sym->ts.kind, NULL);
1955 tmp = build_pointer_type (tmp);
1956 if (sym->attr.pointer)
1957 value = build_fold_indirect_ref_loc (input_location,
1958 se->expr);
1959 else
1960 value = se->expr;
1961 value = fold_convert (tmp, value);
1964 /* If the argument is a scalar, a pointer to an array or an allocatable,
1965 dereference it. */
1966 else if (!sym->attr.dimension || sym->attr.pointer || sym->attr.allocatable)
1967 value = build_fold_indirect_ref_loc (input_location,
1968 se->expr);
1970 /* For character(*), use the actual argument's descriptor. */
1971 else if (sym->ts.type == BT_CHARACTER && !new_sym->ts.u.cl->length)
1972 value = build_fold_indirect_ref_loc (input_location,
1973 se->expr);
1975 /* If the argument is an array descriptor, use it to determine
1976 information about the actual argument's shape. */
1977 else if (POINTER_TYPE_P (TREE_TYPE (se->expr))
1978 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
1980 /* Get the actual argument's descriptor. */
1981 desc = build_fold_indirect_ref_loc (input_location,
1982 se->expr);
1984 /* Create the replacement variable. */
1985 tmp = gfc_conv_descriptor_data_get (desc);
1986 value = gfc_get_interface_mapping_array (&se->pre, sym,
1987 PACKED_NO, tmp);
1989 /* Use DESC to work out the upper bounds, strides and offset. */
1990 gfc_set_interface_mapping_bounds (&se->pre, TREE_TYPE (value), desc);
1992 else
1993 /* Otherwise we have a packed array. */
1994 value = gfc_get_interface_mapping_array (&se->pre, sym,
1995 PACKED_FULL, se->expr);
1997 new_sym->backend_decl = value;
2001 /* Called once all dummy argument mappings have been added to MAPPING,
2002 but before the mapping is used to evaluate expressions. Pre-evaluate
2003 the length of each argument, adding any initialization code to PRE and
2004 any finalization code to POST. */
2006 void
2007 gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
2008 stmtblock_t * pre, stmtblock_t * post)
2010 gfc_interface_sym_mapping *sym;
2011 gfc_expr *expr;
2012 gfc_se se;
2014 for (sym = mapping->syms; sym; sym = sym->next)
2015 if (sym->new_sym->n.sym->ts.type == BT_CHARACTER
2016 && !sym->new_sym->n.sym->ts.u.cl->backend_decl)
2018 expr = sym->new_sym->n.sym->ts.u.cl->length;
2019 gfc_apply_interface_mapping_to_expr (mapping, expr);
2020 gfc_init_se (&se, NULL);
2021 gfc_conv_expr (&se, expr);
2022 se.expr = fold_convert (gfc_charlen_type_node, se.expr);
2023 se.expr = gfc_evaluate_now (se.expr, &se.pre);
2024 gfc_add_block_to_block (pre, &se.pre);
2025 gfc_add_block_to_block (post, &se.post);
2027 sym->new_sym->n.sym->ts.u.cl->backend_decl = se.expr;
2032 /* Like gfc_apply_interface_mapping_to_expr, but applied to
2033 constructor C. */
2035 static void
2036 gfc_apply_interface_mapping_to_cons (gfc_interface_mapping * mapping,
2037 gfc_constructor_base base)
2039 gfc_constructor *c;
2040 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
2042 gfc_apply_interface_mapping_to_expr (mapping, c->expr);
2043 if (c->iterator)
2045 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->start);
2046 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->end);
2047 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->step);
2053 /* Like gfc_apply_interface_mapping_to_expr, but applied to
2054 reference REF. */
2056 static void
2057 gfc_apply_interface_mapping_to_ref (gfc_interface_mapping * mapping,
2058 gfc_ref * ref)
2060 int n;
2062 for (; ref; ref = ref->next)
2063 switch (ref->type)
2065 case REF_ARRAY:
2066 for (n = 0; n < ref->u.ar.dimen; n++)
2068 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.start[n]);
2069 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.end[n]);
2070 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.stride[n]);
2072 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.offset);
2073 break;
2075 case REF_COMPONENT:
2076 break;
2078 case REF_SUBSTRING:
2079 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.start);
2080 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.end);
2081 break;
2086 /* Convert intrinsic function calls into result expressions. */
2088 static bool
2089 gfc_map_intrinsic_function (gfc_expr *expr, gfc_interface_mapping *mapping)
2091 gfc_symbol *sym;
2092 gfc_expr *new_expr;
2093 gfc_expr *arg1;
2094 gfc_expr *arg2;
2095 int d, dup;
2097 arg1 = expr->value.function.actual->expr;
2098 if (expr->value.function.actual->next)
2099 arg2 = expr->value.function.actual->next->expr;
2100 else
2101 arg2 = NULL;
2103 sym = arg1->symtree->n.sym;
2105 if (sym->attr.dummy)
2106 return false;
2108 new_expr = NULL;
2110 switch (expr->value.function.isym->id)
2112 case GFC_ISYM_LEN:
2113 /* TODO figure out why this condition is necessary. */
2114 if (sym->attr.function
2115 && (arg1->ts.u.cl->length == NULL
2116 || (arg1->ts.u.cl->length->expr_type != EXPR_CONSTANT
2117 && arg1->ts.u.cl->length->expr_type != EXPR_VARIABLE)))
2118 return false;
2120 new_expr = gfc_copy_expr (arg1->ts.u.cl->length);
2121 break;
2123 case GFC_ISYM_SIZE:
2124 if (!sym->as || sym->as->rank == 0)
2125 return false;
2127 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
2129 dup = mpz_get_si (arg2->value.integer);
2130 d = dup - 1;
2132 else
2134 dup = sym->as->rank;
2135 d = 0;
2138 for (; d < dup; d++)
2140 gfc_expr *tmp;
2142 if (!sym->as->upper[d] || !sym->as->lower[d])
2144 gfc_free_expr (new_expr);
2145 return false;
2148 tmp = gfc_add (gfc_copy_expr (sym->as->upper[d]),
2149 gfc_get_int_expr (gfc_default_integer_kind,
2150 NULL, 1));
2151 tmp = gfc_subtract (tmp, gfc_copy_expr (sym->as->lower[d]));
2152 if (new_expr)
2153 new_expr = gfc_multiply (new_expr, tmp);
2154 else
2155 new_expr = tmp;
2157 break;
2159 case GFC_ISYM_LBOUND:
2160 case GFC_ISYM_UBOUND:
2161 /* TODO These implementations of lbound and ubound do not limit if
2162 the size < 0, according to F95's 13.14.53 and 13.14.113. */
2164 if (!sym->as || sym->as->rank == 0)
2165 return false;
2167 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
2168 d = mpz_get_si (arg2->value.integer) - 1;
2169 else
2170 /* TODO: If the need arises, this could produce an array of
2171 ubound/lbounds. */
2172 gcc_unreachable ();
2174 if (expr->value.function.isym->id == GFC_ISYM_LBOUND)
2176 if (sym->as->lower[d])
2177 new_expr = gfc_copy_expr (sym->as->lower[d]);
2179 else
2181 if (sym->as->upper[d])
2182 new_expr = gfc_copy_expr (sym->as->upper[d]);
2184 break;
2186 default:
2187 break;
2190 gfc_apply_interface_mapping_to_expr (mapping, new_expr);
2191 if (!new_expr)
2192 return false;
2194 gfc_replace_expr (expr, new_expr);
2195 return true;
2199 static void
2200 gfc_map_fcn_formal_to_actual (gfc_expr *expr, gfc_expr *map_expr,
2201 gfc_interface_mapping * mapping)
2203 gfc_formal_arglist *f;
2204 gfc_actual_arglist *actual;
2206 actual = expr->value.function.actual;
2207 f = map_expr->symtree->n.sym->formal;
2209 for (; f && actual; f = f->next, actual = actual->next)
2211 if (!actual->expr)
2212 continue;
2214 gfc_add_interface_mapping (mapping, f->sym, NULL, actual->expr);
2217 if (map_expr->symtree->n.sym->attr.dimension)
2219 int d;
2220 gfc_array_spec *as;
2222 as = gfc_copy_array_spec (map_expr->symtree->n.sym->as);
2224 for (d = 0; d < as->rank; d++)
2226 gfc_apply_interface_mapping_to_expr (mapping, as->lower[d]);
2227 gfc_apply_interface_mapping_to_expr (mapping, as->upper[d]);
2230 expr->value.function.esym->as = as;
2233 if (map_expr->symtree->n.sym->ts.type == BT_CHARACTER)
2235 expr->value.function.esym->ts.u.cl->length
2236 = gfc_copy_expr (map_expr->symtree->n.sym->ts.u.cl->length);
2238 gfc_apply_interface_mapping_to_expr (mapping,
2239 expr->value.function.esym->ts.u.cl->length);
2244 /* EXPR is a copy of an expression that appeared in the interface
2245 associated with MAPPING. Walk it recursively looking for references to
2246 dummy arguments that MAPPING maps to actual arguments. Replace each such
2247 reference with a reference to the associated actual argument. */
2249 static void
2250 gfc_apply_interface_mapping_to_expr (gfc_interface_mapping * mapping,
2251 gfc_expr * expr)
2253 gfc_interface_sym_mapping *sym;
2254 gfc_actual_arglist *actual;
2256 if (!expr)
2257 return;
2259 /* Copying an expression does not copy its length, so do that here. */
2260 if (expr->ts.type == BT_CHARACTER && expr->ts.u.cl)
2262 expr->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, expr->ts.u.cl);
2263 gfc_apply_interface_mapping_to_expr (mapping, expr->ts.u.cl->length);
2266 /* Apply the mapping to any references. */
2267 gfc_apply_interface_mapping_to_ref (mapping, expr->ref);
2269 /* ...and to the expression's symbol, if it has one. */
2270 /* TODO Find out why the condition on expr->symtree had to be moved into
2271 the loop rather than being outside it, as originally. */
2272 for (sym = mapping->syms; sym; sym = sym->next)
2273 if (expr->symtree && sym->old == expr->symtree->n.sym)
2275 if (sym->new_sym->n.sym->backend_decl)
2276 expr->symtree = sym->new_sym;
2277 else if (sym->expr)
2278 gfc_replace_expr (expr, gfc_copy_expr (sym->expr));
2279 /* Replace base type for polymorphic arguments. */
2280 if (expr->ref && expr->ref->type == REF_COMPONENT
2281 && sym->expr && sym->expr->ts.type == BT_CLASS)
2282 expr->ref->u.c.sym = sym->expr->ts.u.derived;
2285 /* ...and to subexpressions in expr->value. */
2286 switch (expr->expr_type)
2288 case EXPR_VARIABLE:
2289 case EXPR_CONSTANT:
2290 case EXPR_NULL:
2291 case EXPR_SUBSTRING:
2292 break;
2294 case EXPR_OP:
2295 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op1);
2296 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op2);
2297 break;
2299 case EXPR_FUNCTION:
2300 for (actual = expr->value.function.actual; actual; actual = actual->next)
2301 gfc_apply_interface_mapping_to_expr (mapping, actual->expr);
2303 if (expr->value.function.esym == NULL
2304 && expr->value.function.isym != NULL
2305 && expr->value.function.actual->expr->symtree
2306 && gfc_map_intrinsic_function (expr, mapping))
2307 break;
2309 for (sym = mapping->syms; sym; sym = sym->next)
2310 if (sym->old == expr->value.function.esym)
2312 expr->value.function.esym = sym->new_sym->n.sym;
2313 gfc_map_fcn_formal_to_actual (expr, sym->expr, mapping);
2314 expr->value.function.esym->result = sym->new_sym->n.sym;
2316 break;
2318 case EXPR_ARRAY:
2319 case EXPR_STRUCTURE:
2320 gfc_apply_interface_mapping_to_cons (mapping, expr->value.constructor);
2321 break;
2323 case EXPR_COMPCALL:
2324 case EXPR_PPC:
2325 gcc_unreachable ();
2326 break;
2329 return;
2333 /* Evaluate interface expression EXPR using MAPPING. Store the result
2334 in SE. */
2336 void
2337 gfc_apply_interface_mapping (gfc_interface_mapping * mapping,
2338 gfc_se * se, gfc_expr * expr)
2340 expr = gfc_copy_expr (expr);
2341 gfc_apply_interface_mapping_to_expr (mapping, expr);
2342 gfc_conv_expr (se, expr);
2343 se->expr = gfc_evaluate_now (se->expr, &se->pre);
2344 gfc_free_expr (expr);
2348 /* Returns a reference to a temporary array into which a component of
2349 an actual argument derived type array is copied and then returned
2350 after the function call. */
2351 void
2352 gfc_conv_subref_array_arg (gfc_se * parmse, gfc_expr * expr, int g77,
2353 sym_intent intent, bool formal_ptr)
2355 gfc_se lse;
2356 gfc_se rse;
2357 gfc_ss *lss;
2358 gfc_ss *rss;
2359 gfc_loopinfo loop;
2360 gfc_loopinfo loop2;
2361 gfc_ss_info *info;
2362 tree offset;
2363 tree tmp_index;
2364 tree tmp;
2365 tree base_type;
2366 tree size;
2367 stmtblock_t body;
2368 int n;
2369 int dimen;
2371 gcc_assert (expr->expr_type == EXPR_VARIABLE);
2373 gfc_init_se (&lse, NULL);
2374 gfc_init_se (&rse, NULL);
2376 /* Walk the argument expression. */
2377 rss = gfc_walk_expr (expr);
2379 gcc_assert (rss != gfc_ss_terminator);
2381 /* Initialize the scalarizer. */
2382 gfc_init_loopinfo (&loop);
2383 gfc_add_ss_to_loop (&loop, rss);
2385 /* Calculate the bounds of the scalarization. */
2386 gfc_conv_ss_startstride (&loop);
2388 /* Build an ss for the temporary. */
2389 if (expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->backend_decl)
2390 gfc_conv_string_length (expr->ts.u.cl, expr, &parmse->pre);
2392 base_type = gfc_typenode_for_spec (&expr->ts);
2393 if (GFC_ARRAY_TYPE_P (base_type)
2394 || GFC_DESCRIPTOR_TYPE_P (base_type))
2395 base_type = gfc_get_element_type (base_type);
2397 loop.temp_ss = gfc_get_ss ();;
2398 loop.temp_ss->type = GFC_SS_TEMP;
2399 loop.temp_ss->data.temp.type = base_type;
2401 if (expr->ts.type == BT_CHARACTER)
2402 loop.temp_ss->string_length = expr->ts.u.cl->backend_decl;
2403 else
2404 loop.temp_ss->string_length = NULL;
2406 parmse->string_length = loop.temp_ss->string_length;
2407 loop.temp_ss->data.temp.dimen = loop.dimen;
2408 loop.temp_ss->next = gfc_ss_terminator;
2410 /* Associate the SS with the loop. */
2411 gfc_add_ss_to_loop (&loop, loop.temp_ss);
2413 /* Setup the scalarizing loops. */
2414 gfc_conv_loop_setup (&loop, &expr->where);
2416 /* Pass the temporary descriptor back to the caller. */
2417 info = &loop.temp_ss->data.info;
2418 parmse->expr = info->descriptor;
2420 /* Setup the gfc_se structures. */
2421 gfc_copy_loopinfo_to_se (&lse, &loop);
2422 gfc_copy_loopinfo_to_se (&rse, &loop);
2424 rse.ss = rss;
2425 lse.ss = loop.temp_ss;
2426 gfc_mark_ss_chain_used (rss, 1);
2427 gfc_mark_ss_chain_used (loop.temp_ss, 1);
2429 /* Start the scalarized loop body. */
2430 gfc_start_scalarized_body (&loop, &body);
2432 /* Translate the expression. */
2433 gfc_conv_expr (&rse, expr);
2435 gfc_conv_tmp_array_ref (&lse);
2437 if (intent != INTENT_OUT)
2439 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, true, false, true);
2440 gfc_add_expr_to_block (&body, tmp);
2441 gcc_assert (rse.ss == gfc_ss_terminator);
2442 gfc_trans_scalarizing_loops (&loop, &body);
2444 else
2446 /* Make sure that the temporary declaration survives by merging
2447 all the loop declarations into the current context. */
2448 for (n = 0; n < loop.dimen; n++)
2450 gfc_merge_block_scope (&body);
2451 body = loop.code[loop.order[n]];
2453 gfc_merge_block_scope (&body);
2456 /* Add the post block after the second loop, so that any
2457 freeing of allocated memory is done at the right time. */
2458 gfc_add_block_to_block (&parmse->pre, &loop.pre);
2460 /**********Copy the temporary back again.*********/
2462 gfc_init_se (&lse, NULL);
2463 gfc_init_se (&rse, NULL);
2465 /* Walk the argument expression. */
2466 lss = gfc_walk_expr (expr);
2467 rse.ss = loop.temp_ss;
2468 lse.ss = lss;
2470 /* Initialize the scalarizer. */
2471 gfc_init_loopinfo (&loop2);
2472 gfc_add_ss_to_loop (&loop2, lss);
2474 /* Calculate the bounds of the scalarization. */
2475 gfc_conv_ss_startstride (&loop2);
2477 /* Setup the scalarizing loops. */
2478 gfc_conv_loop_setup (&loop2, &expr->where);
2480 gfc_copy_loopinfo_to_se (&lse, &loop2);
2481 gfc_copy_loopinfo_to_se (&rse, &loop2);
2483 gfc_mark_ss_chain_used (lss, 1);
2484 gfc_mark_ss_chain_used (loop.temp_ss, 1);
2486 /* Declare the variable to hold the temporary offset and start the
2487 scalarized loop body. */
2488 offset = gfc_create_var (gfc_array_index_type, NULL);
2489 gfc_start_scalarized_body (&loop2, &body);
2491 /* Build the offsets for the temporary from the loop variables. The
2492 temporary array has lbounds of zero and strides of one in all
2493 dimensions, so this is very simple. The offset is only computed
2494 outside the innermost loop, so the overall transfer could be
2495 optimized further. */
2496 info = &rse.ss->data.info;
2497 dimen = info->dimen;
2499 tmp_index = gfc_index_zero_node;
2500 for (n = dimen - 1; n > 0; n--)
2502 tree tmp_str;
2503 tmp = rse.loop->loopvar[n];
2504 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
2505 tmp, rse.loop->from[n]);
2506 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
2507 tmp, tmp_index);
2509 tmp_str = fold_build2_loc (input_location, MINUS_EXPR,
2510 gfc_array_index_type,
2511 rse.loop->to[n-1], rse.loop->from[n-1]);
2512 tmp_str = fold_build2_loc (input_location, PLUS_EXPR,
2513 gfc_array_index_type,
2514 tmp_str, gfc_index_one_node);
2516 tmp_index = fold_build2_loc (input_location, MULT_EXPR,
2517 gfc_array_index_type, tmp, tmp_str);
2520 tmp_index = fold_build2_loc (input_location, MINUS_EXPR,
2521 gfc_array_index_type,
2522 tmp_index, rse.loop->from[0]);
2523 gfc_add_modify (&rse.loop->code[0], offset, tmp_index);
2525 tmp_index = fold_build2_loc (input_location, PLUS_EXPR,
2526 gfc_array_index_type,
2527 rse.loop->loopvar[0], offset);
2529 /* Now use the offset for the reference. */
2530 tmp = build_fold_indirect_ref_loc (input_location,
2531 info->data);
2532 rse.expr = gfc_build_array_ref (tmp, tmp_index, NULL);
2534 if (expr->ts.type == BT_CHARACTER)
2535 rse.string_length = expr->ts.u.cl->backend_decl;
2537 gfc_conv_expr (&lse, expr);
2539 gcc_assert (lse.ss == gfc_ss_terminator);
2541 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, false, true);
2542 gfc_add_expr_to_block (&body, tmp);
2544 /* Generate the copying loops. */
2545 gfc_trans_scalarizing_loops (&loop2, &body);
2547 /* Wrap the whole thing up by adding the second loop to the post-block
2548 and following it by the post-block of the first loop. In this way,
2549 if the temporary needs freeing, it is done after use! */
2550 if (intent != INTENT_IN)
2552 gfc_add_block_to_block (&parmse->post, &loop2.pre);
2553 gfc_add_block_to_block (&parmse->post, &loop2.post);
2556 gfc_add_block_to_block (&parmse->post, &loop.post);
2558 gfc_cleanup_loop (&loop);
2559 gfc_cleanup_loop (&loop2);
2561 /* Pass the string length to the argument expression. */
2562 if (expr->ts.type == BT_CHARACTER)
2563 parmse->string_length = expr->ts.u.cl->backend_decl;
2565 /* Determine the offset for pointer formal arguments and set the
2566 lbounds to one. */
2567 if (formal_ptr)
2569 size = gfc_index_one_node;
2570 offset = gfc_index_zero_node;
2571 for (n = 0; n < dimen; n++)
2573 tmp = gfc_conv_descriptor_ubound_get (parmse->expr,
2574 gfc_rank_cst[n]);
2575 tmp = fold_build2_loc (input_location, PLUS_EXPR,
2576 gfc_array_index_type, tmp,
2577 gfc_index_one_node);
2578 gfc_conv_descriptor_ubound_set (&parmse->pre,
2579 parmse->expr,
2580 gfc_rank_cst[n],
2581 tmp);
2582 gfc_conv_descriptor_lbound_set (&parmse->pre,
2583 parmse->expr,
2584 gfc_rank_cst[n],
2585 gfc_index_one_node);
2586 size = gfc_evaluate_now (size, &parmse->pre);
2587 offset = fold_build2_loc (input_location, MINUS_EXPR,
2588 gfc_array_index_type,
2589 offset, size);
2590 offset = gfc_evaluate_now (offset, &parmse->pre);
2591 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2592 gfc_array_index_type,
2593 rse.loop->to[n], rse.loop->from[n]);
2594 tmp = fold_build2_loc (input_location, PLUS_EXPR,
2595 gfc_array_index_type,
2596 tmp, gfc_index_one_node);
2597 size = fold_build2_loc (input_location, MULT_EXPR,
2598 gfc_array_index_type, size, tmp);
2601 gfc_conv_descriptor_offset_set (&parmse->pre, parmse->expr,
2602 offset);
2605 /* We want either the address for the data or the address of the descriptor,
2606 depending on the mode of passing array arguments. */
2607 if (g77)
2608 parmse->expr = gfc_conv_descriptor_data_get (parmse->expr);
2609 else
2610 parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
2612 return;
2616 /* Generate the code for argument list functions. */
2618 static void
2619 conv_arglist_function (gfc_se *se, gfc_expr *expr, const char *name)
2621 /* Pass by value for g77 %VAL(arg), pass the address
2622 indirectly for %LOC, else by reference. Thus %REF
2623 is a "do-nothing" and %LOC is the same as an F95
2624 pointer. */
2625 if (strncmp (name, "%VAL", 4) == 0)
2626 gfc_conv_expr (se, expr);
2627 else if (strncmp (name, "%LOC", 4) == 0)
2629 gfc_conv_expr_reference (se, expr);
2630 se->expr = gfc_build_addr_expr (NULL, se->expr);
2632 else if (strncmp (name, "%REF", 4) == 0)
2633 gfc_conv_expr_reference (se, expr);
2634 else
2635 gfc_error ("Unknown argument list function at %L", &expr->where);
2639 /* Takes a derived type expression and returns the address of a temporary
2640 class object of the 'declared' type. */
2641 static void
2642 gfc_conv_derived_to_class (gfc_se *parmse, gfc_expr *e,
2643 gfc_typespec class_ts)
2645 gfc_component *cmp;
2646 gfc_symbol *vtab;
2647 gfc_symbol *declared = class_ts.u.derived;
2648 gfc_ss *ss;
2649 tree ctree;
2650 tree var;
2651 tree tmp;
2653 /* The derived type needs to be converted to a temporary
2654 CLASS object. */
2655 tmp = gfc_typenode_for_spec (&class_ts);
2656 var = gfc_create_var (tmp, "class");
2658 /* Set the vptr. */
2659 cmp = gfc_find_component (declared, "_vptr", true, true);
2660 ctree = fold_build3_loc (input_location, COMPONENT_REF,
2661 TREE_TYPE (cmp->backend_decl),
2662 var, cmp->backend_decl, NULL_TREE);
2664 /* Remember the vtab corresponds to the derived type
2665 not to the class declared type. */
2666 vtab = gfc_find_derived_vtab (e->ts.u.derived);
2667 gcc_assert (vtab);
2668 tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
2669 gfc_add_modify (&parmse->pre, ctree,
2670 fold_convert (TREE_TYPE (ctree), tmp));
2672 /* Now set the data field. */
2673 cmp = gfc_find_component (declared, "_data", true, true);
2674 ctree = fold_build3_loc (input_location, COMPONENT_REF,
2675 TREE_TYPE (cmp->backend_decl),
2676 var, cmp->backend_decl, NULL_TREE);
2677 ss = gfc_walk_expr (e);
2678 if (ss == gfc_ss_terminator)
2680 parmse->ss = NULL;
2681 gfc_conv_expr_reference (parmse, e);
2682 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
2683 gfc_add_modify (&parmse->pre, ctree, tmp);
2685 else
2687 parmse->ss = ss;
2688 gfc_conv_expr (parmse, e);
2689 gfc_add_modify (&parmse->pre, ctree, parmse->expr);
2692 /* Pass the address of the class object. */
2693 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
2697 /* The following routine generates code for the intrinsic
2698 procedures from the ISO_C_BINDING module:
2699 * C_LOC (function)
2700 * C_FUNLOC (function)
2701 * C_F_POINTER (subroutine)
2702 * C_F_PROCPOINTER (subroutine)
2703 * C_ASSOCIATED (function)
2704 One exception which is not handled here is C_F_POINTER with non-scalar
2705 arguments. Returns 1 if the call was replaced by inline code (else: 0). */
2707 static int
2708 conv_isocbinding_procedure (gfc_se * se, gfc_symbol * sym,
2709 gfc_actual_arglist * arg)
2711 gfc_symbol *fsym;
2712 gfc_ss *argss;
2714 if (sym->intmod_sym_id == ISOCBINDING_LOC)
2716 if (arg->expr->rank == 0)
2717 gfc_conv_expr_reference (se, arg->expr);
2718 else
2720 int f;
2721 /* This is really the actual arg because no formal arglist is
2722 created for C_LOC. */
2723 fsym = arg->expr->symtree->n.sym;
2725 /* We should want it to do g77 calling convention. */
2726 f = (fsym != NULL)
2727 && !(fsym->attr.pointer || fsym->attr.allocatable)
2728 && fsym->as->type != AS_ASSUMED_SHAPE;
2729 f = f || !sym->attr.always_explicit;
2731 argss = gfc_walk_expr (arg->expr);
2732 gfc_conv_array_parameter (se, arg->expr, argss, f,
2733 NULL, NULL, NULL);
2736 /* TODO -- the following two lines shouldn't be necessary, but if
2737 they're removed, a bug is exposed later in the code path.
2738 This workaround was thus introduced, but will have to be
2739 removed; please see PR 35150 for details about the issue. */
2740 se->expr = convert (pvoid_type_node, se->expr);
2741 se->expr = gfc_evaluate_now (se->expr, &se->pre);
2743 return 1;
2745 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2747 arg->expr->ts.type = sym->ts.u.derived->ts.type;
2748 arg->expr->ts.f90_type = sym->ts.u.derived->ts.f90_type;
2749 arg->expr->ts.kind = sym->ts.u.derived->ts.kind;
2750 gfc_conv_expr_reference (se, arg->expr);
2752 return 1;
2754 else if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER
2755 && arg->next->expr->rank == 0)
2756 || sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER)
2758 /* Convert c_f_pointer if fptr is a scalar
2759 and convert c_f_procpointer. */
2760 gfc_se cptrse;
2761 gfc_se fptrse;
2763 gfc_init_se (&cptrse, NULL);
2764 gfc_conv_expr (&cptrse, arg->expr);
2765 gfc_add_block_to_block (&se->pre, &cptrse.pre);
2766 gfc_add_block_to_block (&se->post, &cptrse.post);
2768 gfc_init_se (&fptrse, NULL);
2769 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER
2770 || gfc_is_proc_ptr_comp (arg->next->expr, NULL))
2771 fptrse.want_pointer = 1;
2773 gfc_conv_expr (&fptrse, arg->next->expr);
2774 gfc_add_block_to_block (&se->pre, &fptrse.pre);
2775 gfc_add_block_to_block (&se->post, &fptrse.post);
2777 if (arg->next->expr->symtree->n.sym->attr.proc_pointer
2778 && arg->next->expr->symtree->n.sym->attr.dummy)
2779 fptrse.expr = build_fold_indirect_ref_loc (input_location,
2780 fptrse.expr);
2782 se->expr = fold_build2_loc (input_location, MODIFY_EXPR,
2783 TREE_TYPE (fptrse.expr),
2784 fptrse.expr,
2785 fold_convert (TREE_TYPE (fptrse.expr),
2786 cptrse.expr));
2788 return 1;
2790 else if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2792 gfc_se arg1se;
2793 gfc_se arg2se;
2795 /* Build the addr_expr for the first argument. The argument is
2796 already an *address* so we don't need to set want_pointer in
2797 the gfc_se. */
2798 gfc_init_se (&arg1se, NULL);
2799 gfc_conv_expr (&arg1se, arg->expr);
2800 gfc_add_block_to_block (&se->pre, &arg1se.pre);
2801 gfc_add_block_to_block (&se->post, &arg1se.post);
2803 /* See if we were given two arguments. */
2804 if (arg->next == NULL)
2805 /* Only given one arg so generate a null and do a
2806 not-equal comparison against the first arg. */
2807 se->expr = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
2808 arg1se.expr,
2809 fold_convert (TREE_TYPE (arg1se.expr),
2810 null_pointer_node));
2811 else
2813 tree eq_expr;
2814 tree not_null_expr;
2816 /* Given two arguments so build the arg2se from second arg. */
2817 gfc_init_se (&arg2se, NULL);
2818 gfc_conv_expr (&arg2se, arg->next->expr);
2819 gfc_add_block_to_block (&se->pre, &arg2se.pre);
2820 gfc_add_block_to_block (&se->post, &arg2se.post);
2822 /* Generate test to compare that the two args are equal. */
2823 eq_expr = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2824 arg1se.expr, arg2se.expr);
2825 /* Generate test to ensure that the first arg is not null. */
2826 not_null_expr = fold_build2_loc (input_location, NE_EXPR,
2827 boolean_type_node,
2828 arg1se.expr, null_pointer_node);
2830 /* Finally, the generated test must check that both arg1 is not
2831 NULL and that it is equal to the second arg. */
2832 se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
2833 boolean_type_node,
2834 not_null_expr, eq_expr);
2837 return 1;
2840 /* Nothing was done. */
2841 return 0;
2845 /* Generate code for a procedure call. Note can return se->post != NULL.
2846 If se->direct_byref is set then se->expr contains the return parameter.
2847 Return nonzero, if the call has alternate specifiers.
2848 'expr' is only needed for procedure pointer components. */
2851 gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
2852 gfc_actual_arglist * args, gfc_expr * expr,
2853 VEC(tree,gc) *append_args)
2855 gfc_interface_mapping mapping;
2856 VEC(tree,gc) *arglist;
2857 VEC(tree,gc) *retargs;
2858 tree tmp;
2859 tree fntype;
2860 gfc_se parmse;
2861 gfc_ss *argss;
2862 gfc_ss_info *info;
2863 int byref;
2864 int parm_kind;
2865 tree type;
2866 tree var;
2867 tree len;
2868 VEC(tree,gc) *stringargs;
2869 tree result = NULL;
2870 gfc_formal_arglist *formal;
2871 gfc_actual_arglist *arg;
2872 int has_alternate_specifier = 0;
2873 bool need_interface_mapping;
2874 bool callee_alloc;
2875 gfc_typespec ts;
2876 gfc_charlen cl;
2877 gfc_expr *e;
2878 gfc_symbol *fsym;
2879 stmtblock_t post;
2880 enum {MISSING = 0, ELEMENTAL, SCALAR, SCALAR_POINTER, ARRAY};
2881 gfc_component *comp = NULL;
2882 int arglen;
2884 arglist = NULL;
2885 retargs = NULL;
2886 stringargs = NULL;
2887 var = NULL_TREE;
2888 len = NULL_TREE;
2889 gfc_clear_ts (&ts);
2891 if (sym->from_intmod == INTMOD_ISO_C_BINDING
2892 && conv_isocbinding_procedure (se, sym, args))
2893 return 0;
2895 gfc_is_proc_ptr_comp (expr, &comp);
2897 if (se->ss != NULL)
2899 if (!sym->attr.elemental)
2901 gcc_assert (se->ss->type == GFC_SS_FUNCTION);
2902 if (se->ss->useflags)
2904 gcc_assert ((!comp && gfc_return_by_reference (sym)
2905 && sym->result->attr.dimension)
2906 || (comp && comp->attr.dimension));
2907 gcc_assert (se->loop != NULL);
2909 /* Access the previously obtained result. */
2910 gfc_conv_tmp_array_ref (se);
2911 return 0;
2914 info = &se->ss->data.info;
2916 else
2917 info = NULL;
2919 gfc_init_block (&post);
2920 gfc_init_interface_mapping (&mapping);
2921 if (!comp)
2923 formal = sym->formal;
2924 need_interface_mapping = sym->attr.dimension ||
2925 (sym->ts.type == BT_CHARACTER
2926 && sym->ts.u.cl->length
2927 && sym->ts.u.cl->length->expr_type
2928 != EXPR_CONSTANT);
2930 else
2932 formal = comp->formal;
2933 need_interface_mapping = comp->attr.dimension ||
2934 (comp->ts.type == BT_CHARACTER
2935 && comp->ts.u.cl->length
2936 && comp->ts.u.cl->length->expr_type
2937 != EXPR_CONSTANT);
2940 /* Evaluate the arguments. */
2941 for (arg = args; arg != NULL;
2942 arg = arg->next, formal = formal ? formal->next : NULL)
2944 e = arg->expr;
2945 fsym = formal ? formal->sym : NULL;
2946 parm_kind = MISSING;
2948 if (e == NULL)
2950 if (se->ignore_optional)
2952 /* Some intrinsics have already been resolved to the correct
2953 parameters. */
2954 continue;
2956 else if (arg->label)
2958 has_alternate_specifier = 1;
2959 continue;
2961 else
2963 /* Pass a NULL pointer for an absent arg. */
2964 gfc_init_se (&parmse, NULL);
2965 parmse.expr = null_pointer_node;
2966 if (arg->missing_arg_type == BT_CHARACTER)
2967 parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
2970 else if (arg->expr->expr_type == EXPR_NULL && fsym && !fsym->attr.pointer)
2972 /* Pass a NULL pointer to denote an absent arg. */
2973 gcc_assert (fsym->attr.optional && !fsym->attr.allocatable);
2974 gfc_init_se (&parmse, NULL);
2975 parmse.expr = null_pointer_node;
2976 if (arg->missing_arg_type == BT_CHARACTER)
2977 parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
2979 else if (fsym && fsym->ts.type == BT_CLASS
2980 && e->ts.type == BT_DERIVED)
2982 /* The derived type needs to be converted to a temporary
2983 CLASS object. */
2984 gfc_init_se (&parmse, se);
2985 gfc_conv_derived_to_class (&parmse, e, fsym->ts);
2987 else if (se->ss && se->ss->useflags)
2989 /* An elemental function inside a scalarized loop. */
2990 gfc_init_se (&parmse, se);
2991 gfc_conv_expr_reference (&parmse, e);
2992 parm_kind = ELEMENTAL;
2994 else
2996 /* A scalar or transformational function. */
2997 gfc_init_se (&parmse, NULL);
2998 argss = gfc_walk_expr (e);
3000 if (argss == gfc_ss_terminator)
3002 if (e->expr_type == EXPR_VARIABLE
3003 && e->symtree->n.sym->attr.cray_pointee
3004 && fsym && fsym->attr.flavor == FL_PROCEDURE)
3006 /* The Cray pointer needs to be converted to a pointer to
3007 a type given by the expression. */
3008 gfc_conv_expr (&parmse, e);
3009 type = build_pointer_type (TREE_TYPE (parmse.expr));
3010 tmp = gfc_get_symbol_decl (e->symtree->n.sym->cp_pointer);
3011 parmse.expr = convert (type, tmp);
3013 else if (fsym && fsym->attr.value)
3015 if (fsym->ts.type == BT_CHARACTER
3016 && fsym->ts.is_c_interop
3017 && fsym->ns->proc_name != NULL
3018 && fsym->ns->proc_name->attr.is_bind_c)
3020 parmse.expr = NULL;
3021 gfc_conv_scalar_char_value (fsym, &parmse, &e);
3022 if (parmse.expr == NULL)
3023 gfc_conv_expr (&parmse, e);
3025 else
3026 gfc_conv_expr (&parmse, e);
3028 else if (arg->name && arg->name[0] == '%')
3029 /* Argument list functions %VAL, %LOC and %REF are signalled
3030 through arg->name. */
3031 conv_arglist_function (&parmse, arg->expr, arg->name);
3032 else if ((e->expr_type == EXPR_FUNCTION)
3033 && ((e->value.function.esym
3034 && e->value.function.esym->result->attr.pointer)
3035 || (!e->value.function.esym
3036 && e->symtree->n.sym->attr.pointer))
3037 && fsym && fsym->attr.target)
3039 gfc_conv_expr (&parmse, e);
3040 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
3042 else if (e->expr_type == EXPR_FUNCTION
3043 && e->symtree->n.sym->result
3044 && e->symtree->n.sym->result != e->symtree->n.sym
3045 && e->symtree->n.sym->result->attr.proc_pointer)
3047 /* Functions returning procedure pointers. */
3048 gfc_conv_expr (&parmse, e);
3049 if (fsym && fsym->attr.proc_pointer)
3050 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
3052 else
3054 gfc_conv_expr_reference (&parmse, e);
3056 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
3057 allocated on entry, it must be deallocated. */
3058 if (fsym && fsym->attr.allocatable
3059 && fsym->attr.intent == INTENT_OUT)
3061 stmtblock_t block;
3063 gfc_init_block (&block);
3064 tmp = gfc_deallocate_with_status (parmse.expr, NULL_TREE,
3065 true, NULL);
3066 gfc_add_expr_to_block (&block, tmp);
3067 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
3068 void_type_node, parmse.expr,
3069 null_pointer_node);
3070 gfc_add_expr_to_block (&block, tmp);
3072 if (fsym->attr.optional
3073 && e->expr_type == EXPR_VARIABLE
3074 && e->symtree->n.sym->attr.optional)
3076 tmp = fold_build3_loc (input_location, COND_EXPR,
3077 void_type_node,
3078 gfc_conv_expr_present (e->symtree->n.sym),
3079 gfc_finish_block (&block),
3080 build_empty_stmt (input_location));
3082 else
3083 tmp = gfc_finish_block (&block);
3085 gfc_add_expr_to_block (&se->pre, tmp);
3088 if (fsym && e->expr_type != EXPR_NULL
3089 && ((fsym->attr.pointer
3090 && fsym->attr.flavor != FL_PROCEDURE)
3091 || (fsym->attr.proc_pointer
3092 && !(e->expr_type == EXPR_VARIABLE
3093 && e->symtree->n.sym->attr.dummy))
3094 || (fsym->attr.proc_pointer
3095 && e->expr_type == EXPR_VARIABLE
3096 && gfc_is_proc_ptr_comp (e, NULL))
3097 || fsym->attr.allocatable))
3099 /* Scalar pointer dummy args require an extra level of
3100 indirection. The null pointer already contains
3101 this level of indirection. */
3102 parm_kind = SCALAR_POINTER;
3103 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
3107 else
3109 /* If the procedure requires an explicit interface, the actual
3110 argument is passed according to the corresponding formal
3111 argument. If the corresponding formal argument is a POINTER,
3112 ALLOCATABLE or assumed shape, we do not use g77's calling
3113 convention, and pass the address of the array descriptor
3114 instead. Otherwise we use g77's calling convention. */
3115 bool f;
3116 f = (fsym != NULL)
3117 && !(fsym->attr.pointer || fsym->attr.allocatable)
3118 && fsym->as && fsym->as->type != AS_ASSUMED_SHAPE;
3119 if (comp)
3120 f = f || !comp->attr.always_explicit;
3121 else
3122 f = f || !sym->attr.always_explicit;
3124 /* If the argument is a function call that may not create
3125 a temporary for the result, we have to check that we
3126 can do it, i.e. that there is no alias between this
3127 argument and another one. */
3128 if (gfc_get_noncopying_intrinsic_argument (e) != NULL)
3130 gfc_expr *iarg;
3131 sym_intent intent;
3133 if (fsym != NULL)
3134 intent = fsym->attr.intent;
3135 else
3136 intent = INTENT_UNKNOWN;
3138 if (gfc_check_fncall_dependency (e, intent, sym, args,
3139 NOT_ELEMENTAL))
3140 parmse.force_tmp = 1;
3142 iarg = e->value.function.actual->expr;
3144 /* Temporary needed if aliasing due to host association. */
3145 if (sym->attr.contained
3146 && !sym->attr.pure
3147 && !sym->attr.implicit_pure
3148 && !sym->attr.use_assoc
3149 && iarg->expr_type == EXPR_VARIABLE
3150 && sym->ns == iarg->symtree->n.sym->ns)
3151 parmse.force_tmp = 1;
3153 /* Ditto within module. */
3154 if (sym->attr.use_assoc
3155 && !sym->attr.pure
3156 && !sym->attr.implicit_pure
3157 && iarg->expr_type == EXPR_VARIABLE
3158 && sym->module == iarg->symtree->n.sym->module)
3159 parmse.force_tmp = 1;
3162 if (e->expr_type == EXPR_VARIABLE
3163 && is_subref_array (e))
3164 /* The actual argument is a component reference to an
3165 array of derived types. In this case, the argument
3166 is converted to a temporary, which is passed and then
3167 written back after the procedure call. */
3168 gfc_conv_subref_array_arg (&parmse, e, f,
3169 fsym ? fsym->attr.intent : INTENT_INOUT,
3170 fsym && fsym->attr.pointer);
3171 else
3172 gfc_conv_array_parameter (&parmse, e, argss, f, fsym,
3173 sym->name, NULL);
3175 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
3176 allocated on entry, it must be deallocated. */
3177 if (fsym && fsym->attr.allocatable
3178 && fsym->attr.intent == INTENT_OUT)
3180 tmp = build_fold_indirect_ref_loc (input_location,
3181 parmse.expr);
3182 tmp = gfc_trans_dealloc_allocated (tmp);
3183 if (fsym->attr.optional
3184 && e->expr_type == EXPR_VARIABLE
3185 && e->symtree->n.sym->attr.optional)
3186 tmp = fold_build3_loc (input_location, COND_EXPR,
3187 void_type_node,
3188 gfc_conv_expr_present (e->symtree->n.sym),
3189 tmp, build_empty_stmt (input_location));
3190 gfc_add_expr_to_block (&se->pre, tmp);
3195 /* The case with fsym->attr.optional is that of a user subroutine
3196 with an interface indicating an optional argument. When we call
3197 an intrinsic subroutine, however, fsym is NULL, but we might still
3198 have an optional argument, so we proceed to the substitution
3199 just in case. */
3200 if (e && (fsym == NULL || fsym->attr.optional))
3202 /* If an optional argument is itself an optional dummy argument,
3203 check its presence and substitute a null if absent. This is
3204 only needed when passing an array to an elemental procedure
3205 as then array elements are accessed - or no NULL pointer is
3206 allowed and a "1" or "0" should be passed if not present.
3207 When passing a non-array-descriptor full array to a
3208 non-array-descriptor dummy, no check is needed. For
3209 array-descriptor actual to array-descriptor dummy, see
3210 PR 41911 for why a check has to be inserted.
3211 fsym == NULL is checked as intrinsics required the descriptor
3212 but do not always set fsym. */
3213 if (e->expr_type == EXPR_VARIABLE
3214 && e->symtree->n.sym->attr.optional
3215 && ((e->rank > 0 && sym->attr.elemental)
3216 || e->representation.length || e->ts.type == BT_CHARACTER
3217 || (e->rank > 0
3218 && (fsym == NULL
3219 || (fsym-> as
3220 && (fsym->as->type == AS_ASSUMED_SHAPE
3221 || fsym->as->type == AS_DEFERRED))))))
3222 gfc_conv_missing_dummy (&parmse, e, fsym ? fsym->ts : e->ts,
3223 e->representation.length);
3226 if (fsym && e)
3228 /* Obtain the character length of an assumed character length
3229 length procedure from the typespec. */
3230 if (fsym->ts.type == BT_CHARACTER
3231 && parmse.string_length == NULL_TREE
3232 && e->ts.type == BT_PROCEDURE
3233 && e->symtree->n.sym->ts.type == BT_CHARACTER
3234 && e->symtree->n.sym->ts.u.cl->length != NULL
3235 && e->symtree->n.sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
3237 gfc_conv_const_charlen (e->symtree->n.sym->ts.u.cl);
3238 parmse.string_length = e->symtree->n.sym->ts.u.cl->backend_decl;
3242 if (fsym && need_interface_mapping && e)
3243 gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
3245 gfc_add_block_to_block (&se->pre, &parmse.pre);
3246 gfc_add_block_to_block (&post, &parmse.post);
3248 /* Allocated allocatable components of derived types must be
3249 deallocated for non-variable scalars. Non-variable arrays are
3250 dealt with in trans-array.c(gfc_conv_array_parameter). */
3251 if (e && e->ts.type == BT_DERIVED
3252 && e->ts.u.derived->attr.alloc_comp
3253 && !(e->symtree && e->symtree->n.sym->attr.pointer)
3254 && (e->expr_type != EXPR_VARIABLE && !e->rank))
3256 int parm_rank;
3257 tmp = build_fold_indirect_ref_loc (input_location,
3258 parmse.expr);
3259 parm_rank = e->rank;
3260 switch (parm_kind)
3262 case (ELEMENTAL):
3263 case (SCALAR):
3264 parm_rank = 0;
3265 break;
3267 case (SCALAR_POINTER):
3268 tmp = build_fold_indirect_ref_loc (input_location,
3269 tmp);
3270 break;
3273 if (e->expr_type == EXPR_OP
3274 && e->value.op.op == INTRINSIC_PARENTHESES
3275 && e->value.op.op1->expr_type == EXPR_VARIABLE)
3277 tree local_tmp;
3278 local_tmp = gfc_evaluate_now (tmp, &se->pre);
3279 local_tmp = gfc_copy_alloc_comp (e->ts.u.derived, local_tmp, tmp, parm_rank);
3280 gfc_add_expr_to_block (&se->post, local_tmp);
3283 tmp = gfc_deallocate_alloc_comp (e->ts.u.derived, tmp, parm_rank);
3285 gfc_add_expr_to_block (&se->post, tmp);
3288 /* Add argument checking of passing an unallocated/NULL actual to
3289 a nonallocatable/nonpointer dummy. */
3291 if (gfc_option.rtcheck & GFC_RTCHECK_POINTER && e != NULL)
3293 symbol_attribute attr;
3294 char *msg;
3295 tree cond;
3297 if (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
3298 attr = gfc_expr_attr (e);
3299 else
3300 goto end_pointer_check;
3302 /* In Fortran 2008 it's allowed to pass a NULL pointer/nonallocated
3303 allocatable to an optional dummy, cf. 12.5.2.12. */
3304 if (fsym != NULL && fsym->attr.optional && !attr.proc_pointer
3305 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
3306 goto end_pointer_check;
3308 if (attr.optional)
3310 /* If the actual argument is an optional pointer/allocatable and
3311 the formal argument takes an nonpointer optional value,
3312 it is invalid to pass a non-present argument on, even
3313 though there is no technical reason for this in gfortran.
3314 See Fortran 2003, Section 12.4.1.6 item (7)+(8). */
3315 tree present, null_ptr, type;
3317 if (attr.allocatable
3318 && (fsym == NULL || !fsym->attr.allocatable))
3319 asprintf (&msg, "Allocatable actual argument '%s' is not "
3320 "allocated or not present", e->symtree->n.sym->name);
3321 else if (attr.pointer
3322 && (fsym == NULL || !fsym->attr.pointer))
3323 asprintf (&msg, "Pointer actual argument '%s' is not "
3324 "associated or not present",
3325 e->symtree->n.sym->name);
3326 else if (attr.proc_pointer
3327 && (fsym == NULL || !fsym->attr.proc_pointer))
3328 asprintf (&msg, "Proc-pointer actual argument '%s' is not "
3329 "associated or not present",
3330 e->symtree->n.sym->name);
3331 else
3332 goto end_pointer_check;
3334 present = gfc_conv_expr_present (e->symtree->n.sym);
3335 type = TREE_TYPE (present);
3336 present = fold_build2_loc (input_location, EQ_EXPR,
3337 boolean_type_node, present,
3338 fold_convert (type,
3339 null_pointer_node));
3340 type = TREE_TYPE (parmse.expr);
3341 null_ptr = fold_build2_loc (input_location, EQ_EXPR,
3342 boolean_type_node, parmse.expr,
3343 fold_convert (type,
3344 null_pointer_node));
3345 cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
3346 boolean_type_node, present, null_ptr);
3348 else
3350 if (attr.allocatable
3351 && (fsym == NULL || !fsym->attr.allocatable))
3352 asprintf (&msg, "Allocatable actual argument '%s' is not "
3353 "allocated", e->symtree->n.sym->name);
3354 else if (attr.pointer
3355 && (fsym == NULL || !fsym->attr.pointer))
3356 asprintf (&msg, "Pointer actual argument '%s' is not "
3357 "associated", e->symtree->n.sym->name);
3358 else if (attr.proc_pointer
3359 && (fsym == NULL || !fsym->attr.proc_pointer))
3360 asprintf (&msg, "Proc-pointer actual argument '%s' is not "
3361 "associated", e->symtree->n.sym->name);
3362 else
3363 goto end_pointer_check;
3366 cond = fold_build2_loc (input_location, EQ_EXPR,
3367 boolean_type_node, parmse.expr,
3368 fold_convert (TREE_TYPE (parmse.expr),
3369 null_pointer_node));
3372 gfc_trans_runtime_check (true, false, cond, &se->pre, &e->where,
3373 msg);
3374 free (msg);
3376 end_pointer_check:
3378 /* Deferred length dummies pass the character length by reference
3379 so that the value can be returned. */
3380 if (parmse.string_length && fsym && fsym->ts.deferred)
3382 tmp = parmse.string_length;
3383 if (TREE_CODE (tmp) != VAR_DECL)
3384 tmp = gfc_evaluate_now (parmse.string_length, &se->pre);
3385 parmse.string_length = gfc_build_addr_expr (NULL_TREE, tmp);
3388 /* Character strings are passed as two parameters, a length and a
3389 pointer - except for Bind(c) which only passes the pointer. */
3390 if (parmse.string_length != NULL_TREE && !sym->attr.is_bind_c)
3391 VEC_safe_push (tree, gc, stringargs, parmse.string_length);
3393 /* For descriptorless coarrays, we pass the token and the offset
3394 as additional arguments. */
3395 if (fsym && fsym->attr.codimension
3396 && gfc_option.coarray == GFC_FCOARRAY_LIB
3397 && !fsym->attr.allocatable && fsym->as->type != AS_ASSUMED_SHAPE
3398 && e == NULL)
3400 /* Token and offset. */
3401 VEC_safe_push (tree, gc, stringargs, null_pointer_node);
3402 VEC_safe_push (tree, gc, stringargs,
3403 build_int_cst (gfc_array_index_type, 0));
3404 gcc_assert (fsym->attr.optional);
3406 else if (fsym && fsym->attr.codimension
3407 && !fsym->attr.allocatable && fsym->as->type != AS_ASSUMED_SHAPE
3408 && gfc_option.coarray == GFC_FCOARRAY_LIB)
3410 tree caf_decl, caf_type;
3411 tree offset, tmp2;
3413 caf_decl = get_tree_for_caf_expr (e);
3414 caf_type = TREE_TYPE (caf_decl);
3416 if (GFC_DESCRIPTOR_TYPE_P (caf_type))
3417 tmp = gfc_conv_descriptor_token (caf_decl);
3418 else
3420 gcc_assert (GFC_ARRAY_TYPE_P (caf_type)
3421 && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) != NULL_TREE);
3422 tmp = GFC_TYPE_ARRAY_CAF_TOKEN (caf_type);
3425 VEC_safe_push (tree, gc, stringargs, tmp);
3427 if (GFC_DESCRIPTOR_TYPE_P (caf_type))
3428 offset = build_int_cst (gfc_array_index_type, 0);
3429 else if (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) != NULL_TREE)
3430 offset = GFC_TYPE_ARRAY_CAF_OFFSET (caf_type);
3431 else
3432 offset = build_int_cst (gfc_array_index_type, 0);
3434 if (GFC_DESCRIPTOR_TYPE_P (caf_type))
3435 tmp = gfc_conv_descriptor_data_get (caf_decl);
3436 else
3438 gcc_assert (POINTER_TYPE_P (caf_type));
3439 tmp = caf_decl;
3442 if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (parmse.expr)))
3443 tmp2 = gfc_conv_descriptor_data_get (parmse.expr);
3444 else
3446 gcc_assert (POINTER_TYPE_P (TREE_TYPE (parmse.expr)));
3447 tmp2 = parmse.expr;
3450 tmp = fold_build2_loc (input_location, MINUS_EXPR,
3451 gfc_array_index_type,
3452 fold_convert (gfc_array_index_type, tmp2),
3453 fold_convert (gfc_array_index_type, tmp));
3454 offset = fold_build2_loc (input_location, PLUS_EXPR,
3455 gfc_array_index_type, offset, tmp);
3457 VEC_safe_push (tree, gc, stringargs, offset);
3460 VEC_safe_push (tree, gc, arglist, parmse.expr);
3462 gfc_finish_interface_mapping (&mapping, &se->pre, &se->post);
3464 if (comp)
3465 ts = comp->ts;
3466 else
3467 ts = sym->ts;
3469 if (ts.type == BT_CHARACTER && sym->attr.is_bind_c)
3470 se->string_length = build_int_cst (gfc_charlen_type_node, 1);
3471 else if (ts.type == BT_CHARACTER)
3473 if (ts.u.cl->length == NULL)
3475 /* Assumed character length results are not allowed by 5.1.1.5 of the
3476 standard and are trapped in resolve.c; except in the case of SPREAD
3477 (and other intrinsics?) and dummy functions. In the case of SPREAD,
3478 we take the character length of the first argument for the result.
3479 For dummies, we have to look through the formal argument list for
3480 this function and use the character length found there.*/
3481 if (ts.deferred && (sym->attr.allocatable || sym->attr.pointer))
3482 cl.backend_decl = gfc_create_var (gfc_charlen_type_node, "slen");
3483 else if (!sym->attr.dummy)
3484 cl.backend_decl = VEC_index (tree, stringargs, 0);
3485 else
3487 formal = sym->ns->proc_name->formal;
3488 for (; formal; formal = formal->next)
3489 if (strcmp (formal->sym->name, sym->name) == 0)
3490 cl.backend_decl = formal->sym->ts.u.cl->backend_decl;
3493 else
3495 tree tmp;
3497 /* Calculate the length of the returned string. */
3498 gfc_init_se (&parmse, NULL);
3499 if (need_interface_mapping)
3500 gfc_apply_interface_mapping (&mapping, &parmse, ts.u.cl->length);
3501 else
3502 gfc_conv_expr (&parmse, ts.u.cl->length);
3503 gfc_add_block_to_block (&se->pre, &parmse.pre);
3504 gfc_add_block_to_block (&se->post, &parmse.post);
3506 tmp = fold_convert (gfc_charlen_type_node, parmse.expr);
3507 tmp = fold_build2_loc (input_location, MAX_EXPR,
3508 gfc_charlen_type_node, tmp,
3509 build_int_cst (gfc_charlen_type_node, 0));
3510 cl.backend_decl = tmp;
3513 /* Set up a charlen structure for it. */
3514 cl.next = NULL;
3515 cl.length = NULL;
3516 ts.u.cl = &cl;
3518 len = cl.backend_decl;
3521 byref = (comp && (comp->attr.dimension || comp->ts.type == BT_CHARACTER))
3522 || (!comp && gfc_return_by_reference (sym));
3523 if (byref)
3525 if (se->direct_byref)
3527 /* Sometimes, too much indirection can be applied; e.g. for
3528 function_result = array_valued_recursive_function. */
3529 if (TREE_TYPE (TREE_TYPE (se->expr))
3530 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))
3531 && GFC_DESCRIPTOR_TYPE_P
3532 (TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))))
3533 se->expr = build_fold_indirect_ref_loc (input_location,
3534 se->expr);
3536 /* If the lhs of an assignment x = f(..) is allocatable and
3537 f2003 is allowed, we must do the automatic reallocation.
3538 TODO - deal with intrinsics, without using a temporary. */
3539 if (gfc_option.flag_realloc_lhs
3540 && se->ss && se->ss->loop_chain
3541 && se->ss->loop_chain->is_alloc_lhs
3542 && !expr->value.function.isym
3543 && sym->result->as != NULL)
3545 /* Evaluate the bounds of the result, if known. */
3546 gfc_set_loop_bounds_from_array_spec (&mapping, se,
3547 sym->result->as);
3549 /* Perform the automatic reallocation. */
3550 tmp = gfc_alloc_allocatable_for_assignment (se->loop,
3551 expr, NULL);
3552 gfc_add_expr_to_block (&se->pre, tmp);
3554 /* Pass the temporary as the first argument. */
3555 result = info->descriptor;
3557 else
3558 result = build_fold_indirect_ref_loc (input_location,
3559 se->expr);
3560 VEC_safe_push (tree, gc, retargs, se->expr);
3562 else if (comp && comp->attr.dimension)
3564 gcc_assert (se->loop && info);
3566 /* Set the type of the array. */
3567 tmp = gfc_typenode_for_spec (&comp->ts);
3568 info->dimen = se->loop->dimen;
3570 /* Evaluate the bounds of the result, if known. */
3571 gfc_set_loop_bounds_from_array_spec (&mapping, se, comp->as);
3573 /* If the lhs of an assignment x = f(..) is allocatable and
3574 f2003 is allowed, we must not generate the function call
3575 here but should just send back the results of the mapping.
3576 This is signalled by the function ss being flagged. */
3577 if (gfc_option.flag_realloc_lhs
3578 && se->ss && se->ss->is_alloc_lhs)
3580 gfc_free_interface_mapping (&mapping);
3581 return has_alternate_specifier;
3584 /* Create a temporary to store the result. In case the function
3585 returns a pointer, the temporary will be a shallow copy and
3586 mustn't be deallocated. */
3587 callee_alloc = comp->attr.allocatable || comp->attr.pointer;
3588 gfc_trans_create_temp_array (&se->pre, &se->post, se->loop, info, tmp,
3589 NULL_TREE, false, !comp->attr.pointer,
3590 callee_alloc, &se->ss->expr->where);
3592 /* Pass the temporary as the first argument. */
3593 result = info->descriptor;
3594 tmp = gfc_build_addr_expr (NULL_TREE, result);
3595 VEC_safe_push (tree, gc, retargs, tmp);
3597 else if (!comp && sym->result->attr.dimension)
3599 gcc_assert (se->loop && info);
3601 /* Set the type of the array. */
3602 tmp = gfc_typenode_for_spec (&ts);
3603 info->dimen = se->loop->dimen;
3605 /* Evaluate the bounds of the result, if known. */
3606 gfc_set_loop_bounds_from_array_spec (&mapping, se, sym->result->as);
3608 /* If the lhs of an assignment x = f(..) is allocatable and
3609 f2003 is allowed, we must not generate the function call
3610 here but should just send back the results of the mapping.
3611 This is signalled by the function ss being flagged. */
3612 if (gfc_option.flag_realloc_lhs
3613 && se->ss && se->ss->is_alloc_lhs)
3615 gfc_free_interface_mapping (&mapping);
3616 return has_alternate_specifier;
3619 /* Create a temporary to store the result. In case the function
3620 returns a pointer, the temporary will be a shallow copy and
3621 mustn't be deallocated. */
3622 callee_alloc = sym->attr.allocatable || sym->attr.pointer;
3623 gfc_trans_create_temp_array (&se->pre, &se->post, se->loop, info, tmp,
3624 NULL_TREE, false, !sym->attr.pointer,
3625 callee_alloc, &se->ss->expr->where);
3627 /* Pass the temporary as the first argument. */
3628 result = info->descriptor;
3629 tmp = gfc_build_addr_expr (NULL_TREE, result);
3630 VEC_safe_push (tree, gc, retargs, tmp);
3632 else if (ts.type == BT_CHARACTER)
3634 /* Pass the string length. */
3635 type = gfc_get_character_type (ts.kind, ts.u.cl);
3636 type = build_pointer_type (type);
3638 /* Return an address to a char[0:len-1]* temporary for
3639 character pointers. */
3640 if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
3641 || (comp && (comp->attr.pointer || comp->attr.allocatable)))
3643 var = gfc_create_var (type, "pstr");
3645 if ((!comp && sym->attr.allocatable)
3646 || (comp && comp->attr.allocatable))
3647 gfc_add_modify (&se->pre, var,
3648 fold_convert (TREE_TYPE (var),
3649 null_pointer_node));
3651 /* Provide an address expression for the function arguments. */
3652 var = gfc_build_addr_expr (NULL_TREE, var);
3654 else
3655 var = gfc_conv_string_tmp (se, type, len);
3657 VEC_safe_push (tree, gc, retargs, var);
3659 else
3661 gcc_assert (gfc_option.flag_f2c && ts.type == BT_COMPLEX);
3663 type = gfc_get_complex_type (ts.kind);
3664 var = gfc_build_addr_expr (NULL_TREE, gfc_create_var (type, "cmplx"));
3665 VEC_safe_push (tree, gc, retargs, var);
3668 if (ts.type == BT_CHARACTER && ts.deferred
3669 && (sym->attr.allocatable || sym->attr.pointer))
3671 tmp = len;
3672 if (TREE_CODE (tmp) != VAR_DECL)
3673 tmp = gfc_evaluate_now (len, &se->pre);
3674 len = gfc_build_addr_expr (NULL_TREE, tmp);
3677 /* Add the string length to the argument list. */
3678 if (ts.type == BT_CHARACTER)
3679 VEC_safe_push (tree, gc, retargs, len);
3681 gfc_free_interface_mapping (&mapping);
3683 /* We need to glom RETARGS + ARGLIST + STRINGARGS + APPEND_ARGS. */
3684 arglen = (VEC_length (tree, arglist)
3685 + VEC_length (tree, stringargs) + VEC_length (tree, append_args));
3686 VEC_reserve_exact (tree, gc, retargs, arglen);
3688 /* Add the return arguments. */
3689 VEC_splice (tree, retargs, arglist);
3691 /* Add the hidden string length parameters to the arguments. */
3692 VEC_splice (tree, retargs, stringargs);
3694 /* We may want to append extra arguments here. This is used e.g. for
3695 calls to libgfortran_matmul_??, which need extra information. */
3696 if (!VEC_empty (tree, append_args))
3697 VEC_splice (tree, retargs, append_args);
3698 arglist = retargs;
3700 /* Generate the actual call. */
3701 conv_function_val (se, sym, expr);
3703 /* If there are alternate return labels, function type should be
3704 integer. Can't modify the type in place though, since it can be shared
3705 with other functions. For dummy arguments, the typing is done to
3706 this result, even if it has to be repeated for each call. */
3707 if (has_alternate_specifier
3708 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) != integer_type_node)
3710 if (!sym->attr.dummy)
3712 TREE_TYPE (sym->backend_decl)
3713 = build_function_type (integer_type_node,
3714 TYPE_ARG_TYPES (TREE_TYPE (sym->backend_decl)));
3715 se->expr = gfc_build_addr_expr (NULL_TREE, sym->backend_decl);
3717 else
3718 TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) = integer_type_node;
3721 fntype = TREE_TYPE (TREE_TYPE (se->expr));
3722 se->expr = build_call_vec (TREE_TYPE (fntype), se->expr, arglist);
3724 /* If we have a pointer function, but we don't want a pointer, e.g.
3725 something like
3726 x = f()
3727 where f is pointer valued, we have to dereference the result. */
3728 if (!se->want_pointer && !byref
3729 && ((!comp && (sym->attr.pointer || sym->attr.allocatable))
3730 || (comp && (comp->attr.pointer || comp->attr.allocatable))))
3731 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
3733 /* f2c calling conventions require a scalar default real function to
3734 return a double precision result. Convert this back to default
3735 real. We only care about the cases that can happen in Fortran 77.
3737 if (gfc_option.flag_f2c && sym->ts.type == BT_REAL
3738 && sym->ts.kind == gfc_default_real_kind
3739 && !sym->attr.always_explicit)
3740 se->expr = fold_convert (gfc_get_real_type (sym->ts.kind), se->expr);
3742 /* A pure function may still have side-effects - it may modify its
3743 parameters. */
3744 TREE_SIDE_EFFECTS (se->expr) = 1;
3745 #if 0
3746 if (!sym->attr.pure)
3747 TREE_SIDE_EFFECTS (se->expr) = 1;
3748 #endif
3750 if (byref)
3752 /* Add the function call to the pre chain. There is no expression. */
3753 gfc_add_expr_to_block (&se->pre, se->expr);
3754 se->expr = NULL_TREE;
3756 if (!se->direct_byref)
3758 if ((sym->attr.dimension && !comp) || (comp && comp->attr.dimension))
3760 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
3762 /* Check the data pointer hasn't been modified. This would
3763 happen in a function returning a pointer. */
3764 tmp = gfc_conv_descriptor_data_get (info->descriptor);
3765 tmp = fold_build2_loc (input_location, NE_EXPR,
3766 boolean_type_node,
3767 tmp, info->data);
3768 gfc_trans_runtime_check (true, false, tmp, &se->pre, NULL,
3769 gfc_msg_fault);
3771 se->expr = info->descriptor;
3772 /* Bundle in the string length. */
3773 se->string_length = len;
3775 else if (ts.type == BT_CHARACTER)
3777 /* Dereference for character pointer results. */
3778 if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
3779 || (comp && (comp->attr.pointer || comp->attr.allocatable)))
3780 se->expr = build_fold_indirect_ref_loc (input_location, var);
3781 else
3782 se->expr = var;
3784 if (!ts.deferred)
3785 se->string_length = len;
3786 else if (sym->attr.allocatable || sym->attr.pointer)
3787 se->string_length = cl.backend_decl;
3789 else
3791 gcc_assert (ts.type == BT_COMPLEX && gfc_option.flag_f2c);
3792 se->expr = build_fold_indirect_ref_loc (input_location, var);
3797 /* Follow the function call with the argument post block. */
3798 if (byref)
3800 gfc_add_block_to_block (&se->pre, &post);
3802 /* Transformational functions of derived types with allocatable
3803 components must have the result allocatable components copied. */
3804 arg = expr->value.function.actual;
3805 if (result && arg && expr->rank
3806 && expr->value.function.isym
3807 && expr->value.function.isym->transformational
3808 && arg->expr->ts.type == BT_DERIVED
3809 && arg->expr->ts.u.derived->attr.alloc_comp)
3811 tree tmp2;
3812 /* Copy the allocatable components. We have to use a
3813 temporary here to prevent source allocatable components
3814 from being corrupted. */
3815 tmp2 = gfc_evaluate_now (result, &se->pre);
3816 tmp = gfc_copy_alloc_comp (arg->expr->ts.u.derived,
3817 result, tmp2, expr->rank);
3818 gfc_add_expr_to_block (&se->pre, tmp);
3819 tmp = gfc_copy_allocatable_data (result, tmp2, TREE_TYPE(tmp2),
3820 expr->rank);
3821 gfc_add_expr_to_block (&se->pre, tmp);
3823 /* Finally free the temporary's data field. */
3824 tmp = gfc_conv_descriptor_data_get (tmp2);
3825 tmp = gfc_deallocate_with_status (tmp, NULL_TREE, true, NULL);
3826 gfc_add_expr_to_block (&se->pre, tmp);
3829 else
3830 gfc_add_block_to_block (&se->post, &post);
3832 return has_alternate_specifier;
3836 /* Fill a character string with spaces. */
3838 static tree
3839 fill_with_spaces (tree start, tree type, tree size)
3841 stmtblock_t block, loop;
3842 tree i, el, exit_label, cond, tmp;
3844 /* For a simple char type, we can call memset(). */
3845 if (compare_tree_int (TYPE_SIZE_UNIT (type), 1) == 0)
3846 return build_call_expr_loc (input_location,
3847 built_in_decls[BUILT_IN_MEMSET], 3, start,
3848 build_int_cst (gfc_get_int_type (gfc_c_int_kind),
3849 lang_hooks.to_target_charset (' ')),
3850 size);
3852 /* Otherwise, we use a loop:
3853 for (el = start, i = size; i > 0; el--, i+= TYPE_SIZE_UNIT (type))
3854 *el = (type) ' ';
3857 /* Initialize variables. */
3858 gfc_init_block (&block);
3859 i = gfc_create_var (sizetype, "i");
3860 gfc_add_modify (&block, i, fold_convert (sizetype, size));
3861 el = gfc_create_var (build_pointer_type (type), "el");
3862 gfc_add_modify (&block, el, fold_convert (TREE_TYPE (el), start));
3863 exit_label = gfc_build_label_decl (NULL_TREE);
3864 TREE_USED (exit_label) = 1;
3867 /* Loop body. */
3868 gfc_init_block (&loop);
3870 /* Exit condition. */
3871 cond = fold_build2_loc (input_location, LE_EXPR, boolean_type_node, i,
3872 build_zero_cst (sizetype));
3873 tmp = build1_v (GOTO_EXPR, exit_label);
3874 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
3875 build_empty_stmt (input_location));
3876 gfc_add_expr_to_block (&loop, tmp);
3878 /* Assignment. */
3879 gfc_add_modify (&loop,
3880 fold_build1_loc (input_location, INDIRECT_REF, type, el),
3881 build_int_cst (type, lang_hooks.to_target_charset (' ')));
3883 /* Increment loop variables. */
3884 gfc_add_modify (&loop, i,
3885 fold_build2_loc (input_location, MINUS_EXPR, sizetype, i,
3886 TYPE_SIZE_UNIT (type)));
3887 gfc_add_modify (&loop, el,
3888 fold_build_pointer_plus_loc (input_location,
3889 el, TYPE_SIZE_UNIT (type)));
3891 /* Making the loop... actually loop! */
3892 tmp = gfc_finish_block (&loop);
3893 tmp = build1_v (LOOP_EXPR, tmp);
3894 gfc_add_expr_to_block (&block, tmp);
3896 /* The exit label. */
3897 tmp = build1_v (LABEL_EXPR, exit_label);
3898 gfc_add_expr_to_block (&block, tmp);
3901 return gfc_finish_block (&block);
3905 /* Generate code to copy a string. */
3907 void
3908 gfc_trans_string_copy (stmtblock_t * block, tree dlength, tree dest,
3909 int dkind, tree slength, tree src, int skind)
3911 tree tmp, dlen, slen;
3912 tree dsc;
3913 tree ssc;
3914 tree cond;
3915 tree cond2;
3916 tree tmp2;
3917 tree tmp3;
3918 tree tmp4;
3919 tree chartype;
3920 stmtblock_t tempblock;
3922 gcc_assert (dkind == skind);
3924 if (slength != NULL_TREE)
3926 slen = fold_convert (size_type_node, gfc_evaluate_now (slength, block));
3927 ssc = gfc_string_to_single_character (slen, src, skind);
3929 else
3931 slen = build_int_cst (size_type_node, 1);
3932 ssc = src;
3935 if (dlength != NULL_TREE)
3937 dlen = fold_convert (size_type_node, gfc_evaluate_now (dlength, block));
3938 dsc = gfc_string_to_single_character (dlen, dest, dkind);
3940 else
3942 dlen = build_int_cst (size_type_node, 1);
3943 dsc = dest;
3946 /* Assign directly if the types are compatible. */
3947 if (dsc != NULL_TREE && ssc != NULL_TREE
3948 && TREE_TYPE (dsc) == TREE_TYPE (ssc))
3950 gfc_add_modify (block, dsc, ssc);
3951 return;
3954 /* Do nothing if the destination length is zero. */
3955 cond = fold_build2_loc (input_location, GT_EXPR, boolean_type_node, dlen,
3956 build_int_cst (size_type_node, 0));
3958 /* The following code was previously in _gfortran_copy_string:
3960 // The two strings may overlap so we use memmove.
3961 void
3962 copy_string (GFC_INTEGER_4 destlen, char * dest,
3963 GFC_INTEGER_4 srclen, const char * src)
3965 if (srclen >= destlen)
3967 // This will truncate if too long.
3968 memmove (dest, src, destlen);
3970 else
3972 memmove (dest, src, srclen);
3973 // Pad with spaces.
3974 memset (&dest[srclen], ' ', destlen - srclen);
3978 We're now doing it here for better optimization, but the logic
3979 is the same. */
3981 /* For non-default character kinds, we have to multiply the string
3982 length by the base type size. */
3983 chartype = gfc_get_char_type (dkind);
3984 slen = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
3985 fold_convert (size_type_node, slen),
3986 fold_convert (size_type_node,
3987 TYPE_SIZE_UNIT (chartype)));
3988 dlen = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
3989 fold_convert (size_type_node, dlen),
3990 fold_convert (size_type_node,
3991 TYPE_SIZE_UNIT (chartype)));
3993 if (dlength && POINTER_TYPE_P (TREE_TYPE (dest)))
3994 dest = fold_convert (pvoid_type_node, dest);
3995 else
3996 dest = gfc_build_addr_expr (pvoid_type_node, dest);
3998 if (slength && POINTER_TYPE_P (TREE_TYPE (src)))
3999 src = fold_convert (pvoid_type_node, src);
4000 else
4001 src = gfc_build_addr_expr (pvoid_type_node, src);
4003 /* Truncate string if source is too long. */
4004 cond2 = fold_build2_loc (input_location, GE_EXPR, boolean_type_node, slen,
4005 dlen);
4006 tmp2 = build_call_expr_loc (input_location,
4007 built_in_decls[BUILT_IN_MEMMOVE],
4008 3, dest, src, dlen);
4010 /* Else copy and pad with spaces. */
4011 tmp3 = build_call_expr_loc (input_location,
4012 built_in_decls[BUILT_IN_MEMMOVE],
4013 3, dest, src, slen);
4015 tmp4 = fold_build_pointer_plus_loc (input_location, dest, slen);
4016 tmp4 = fill_with_spaces (tmp4, chartype,
4017 fold_build2_loc (input_location, MINUS_EXPR,
4018 TREE_TYPE(dlen), dlen, slen));
4020 gfc_init_block (&tempblock);
4021 gfc_add_expr_to_block (&tempblock, tmp3);
4022 gfc_add_expr_to_block (&tempblock, tmp4);
4023 tmp3 = gfc_finish_block (&tempblock);
4025 /* The whole copy_string function is there. */
4026 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
4027 tmp2, tmp3);
4028 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
4029 build_empty_stmt (input_location));
4030 gfc_add_expr_to_block (block, tmp);
4034 /* Translate a statement function.
4035 The value of a statement function reference is obtained by evaluating the
4036 expression using the values of the actual arguments for the values of the
4037 corresponding dummy arguments. */
4039 static void
4040 gfc_conv_statement_function (gfc_se * se, gfc_expr * expr)
4042 gfc_symbol *sym;
4043 gfc_symbol *fsym;
4044 gfc_formal_arglist *fargs;
4045 gfc_actual_arglist *args;
4046 gfc_se lse;
4047 gfc_se rse;
4048 gfc_saved_var *saved_vars;
4049 tree *temp_vars;
4050 tree type;
4051 tree tmp;
4052 int n;
4054 sym = expr->symtree->n.sym;
4055 args = expr->value.function.actual;
4056 gfc_init_se (&lse, NULL);
4057 gfc_init_se (&rse, NULL);
4059 n = 0;
4060 for (fargs = sym->formal; fargs; fargs = fargs->next)
4061 n++;
4062 saved_vars = XCNEWVEC (gfc_saved_var, n);
4063 temp_vars = XCNEWVEC (tree, n);
4065 for (fargs = sym->formal, n = 0; fargs; fargs = fargs->next, n++)
4067 /* Each dummy shall be specified, explicitly or implicitly, to be
4068 scalar. */
4069 gcc_assert (fargs->sym->attr.dimension == 0);
4070 fsym = fargs->sym;
4072 if (fsym->ts.type == BT_CHARACTER)
4074 /* Copy string arguments. */
4075 tree arglen;
4077 gcc_assert (fsym->ts.u.cl && fsym->ts.u.cl->length
4078 && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT);
4080 /* Create a temporary to hold the value. */
4081 if (fsym->ts.u.cl->backend_decl == NULL_TREE)
4082 fsym->ts.u.cl->backend_decl
4083 = gfc_conv_constant_to_tree (fsym->ts.u.cl->length);
4085 type = gfc_get_character_type (fsym->ts.kind, fsym->ts.u.cl);
4086 temp_vars[n] = gfc_create_var (type, fsym->name);
4088 arglen = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
4090 gfc_conv_expr (&rse, args->expr);
4091 gfc_conv_string_parameter (&rse);
4092 gfc_add_block_to_block (&se->pre, &lse.pre);
4093 gfc_add_block_to_block (&se->pre, &rse.pre);
4095 gfc_trans_string_copy (&se->pre, arglen, temp_vars[n], fsym->ts.kind,
4096 rse.string_length, rse.expr, fsym->ts.kind);
4097 gfc_add_block_to_block (&se->pre, &lse.post);
4098 gfc_add_block_to_block (&se->pre, &rse.post);
4100 else
4102 /* For everything else, just evaluate the expression. */
4104 /* Create a temporary to hold the value. */
4105 type = gfc_typenode_for_spec (&fsym->ts);
4106 temp_vars[n] = gfc_create_var (type, fsym->name);
4108 gfc_conv_expr (&lse, args->expr);
4110 gfc_add_block_to_block (&se->pre, &lse.pre);
4111 gfc_add_modify (&se->pre, temp_vars[n], lse.expr);
4112 gfc_add_block_to_block (&se->pre, &lse.post);
4115 args = args->next;
4118 /* Use the temporary variables in place of the real ones. */
4119 for (fargs = sym->formal, n = 0; fargs; fargs = fargs->next, n++)
4120 gfc_shadow_sym (fargs->sym, temp_vars[n], &saved_vars[n]);
4122 gfc_conv_expr (se, sym->value);
4124 if (sym->ts.type == BT_CHARACTER)
4126 gfc_conv_const_charlen (sym->ts.u.cl);
4128 /* Force the expression to the correct length. */
4129 if (!INTEGER_CST_P (se->string_length)
4130 || tree_int_cst_lt (se->string_length,
4131 sym->ts.u.cl->backend_decl))
4133 type = gfc_get_character_type (sym->ts.kind, sym->ts.u.cl);
4134 tmp = gfc_create_var (type, sym->name);
4135 tmp = gfc_build_addr_expr (build_pointer_type (type), tmp);
4136 gfc_trans_string_copy (&se->pre, sym->ts.u.cl->backend_decl, tmp,
4137 sym->ts.kind, se->string_length, se->expr,
4138 sym->ts.kind);
4139 se->expr = tmp;
4141 se->string_length = sym->ts.u.cl->backend_decl;
4144 /* Restore the original variables. */
4145 for (fargs = sym->formal, n = 0; fargs; fargs = fargs->next, n++)
4146 gfc_restore_sym (fargs->sym, &saved_vars[n]);
4147 free (saved_vars);
4151 /* Translate a function expression. */
4153 static void
4154 gfc_conv_function_expr (gfc_se * se, gfc_expr * expr)
4156 gfc_symbol *sym;
4158 if (expr->value.function.isym)
4160 gfc_conv_intrinsic_function (se, expr);
4161 return;
4164 /* We distinguish statement functions from general functions to improve
4165 runtime performance. */
4166 if (expr->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
4168 gfc_conv_statement_function (se, expr);
4169 return;
4172 /* expr.value.function.esym is the resolved (specific) function symbol for
4173 most functions. However this isn't set for dummy procedures. */
4174 sym = expr->value.function.esym;
4175 if (!sym)
4176 sym = expr->symtree->n.sym;
4178 gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr, NULL);
4182 /* Determine whether the given EXPR_CONSTANT is a zero initializer. */
4184 static bool
4185 is_zero_initializer_p (gfc_expr * expr)
4187 if (expr->expr_type != EXPR_CONSTANT)
4188 return false;
4190 /* We ignore constants with prescribed memory representations for now. */
4191 if (expr->representation.string)
4192 return false;
4194 switch (expr->ts.type)
4196 case BT_INTEGER:
4197 return mpz_cmp_si (expr->value.integer, 0) == 0;
4199 case BT_REAL:
4200 return mpfr_zero_p (expr->value.real)
4201 && MPFR_SIGN (expr->value.real) >= 0;
4203 case BT_LOGICAL:
4204 return expr->value.logical == 0;
4206 case BT_COMPLEX:
4207 return mpfr_zero_p (mpc_realref (expr->value.complex))
4208 && MPFR_SIGN (mpc_realref (expr->value.complex)) >= 0
4209 && mpfr_zero_p (mpc_imagref (expr->value.complex))
4210 && MPFR_SIGN (mpc_imagref (expr->value.complex)) >= 0;
4212 default:
4213 break;
4215 return false;
4219 static void
4220 gfc_conv_array_constructor_expr (gfc_se * se, gfc_expr * expr)
4222 gcc_assert (se->ss != NULL && se->ss != gfc_ss_terminator);
4223 gcc_assert (se->ss->expr == expr && se->ss->type == GFC_SS_CONSTRUCTOR);
4225 gfc_conv_tmp_array_ref (se);
4229 /* Build a static initializer. EXPR is the expression for the initial value.
4230 The other parameters describe the variable of the component being
4231 initialized. EXPR may be null. */
4233 tree
4234 gfc_conv_initializer (gfc_expr * expr, gfc_typespec * ts, tree type,
4235 bool array, bool pointer, bool procptr)
4237 gfc_se se;
4239 if (!(expr || pointer || procptr))
4240 return NULL_TREE;
4242 /* Check if we have ISOCBINDING_NULL_PTR or ISOCBINDING_NULL_FUNPTR
4243 (these are the only two iso_c_binding derived types that can be
4244 used as initialization expressions). If so, we need to modify
4245 the 'expr' to be that for a (void *). */
4246 if (expr != NULL && expr->ts.type == BT_DERIVED
4247 && expr->ts.is_iso_c && expr->ts.u.derived)
4249 gfc_symbol *derived = expr->ts.u.derived;
4251 /* The derived symbol has already been converted to a (void *). Use
4252 its kind. */
4253 expr = gfc_get_int_expr (derived->ts.kind, NULL, 0);
4254 expr->ts.f90_type = derived->ts.f90_type;
4256 gfc_init_se (&se, NULL);
4257 gfc_conv_constant (&se, expr);
4258 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
4259 return se.expr;
4262 if (array && !procptr)
4264 tree ctor;
4265 /* Arrays need special handling. */
4266 if (pointer)
4267 ctor = gfc_build_null_descriptor (type);
4268 /* Special case assigning an array to zero. */
4269 else if (is_zero_initializer_p (expr))
4270 ctor = build_constructor (type, NULL);
4271 else
4272 ctor = gfc_conv_array_initializer (type, expr);
4273 TREE_STATIC (ctor) = 1;
4274 return ctor;
4276 else if (pointer || procptr)
4278 if (!expr || expr->expr_type == EXPR_NULL)
4279 return fold_convert (type, null_pointer_node);
4280 else
4282 gfc_init_se (&se, NULL);
4283 se.want_pointer = 1;
4284 gfc_conv_expr (&se, expr);
4285 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
4286 return se.expr;
4289 else
4291 switch (ts->type)
4293 case BT_DERIVED:
4294 case BT_CLASS:
4295 gfc_init_se (&se, NULL);
4296 if (ts->type == BT_CLASS && expr->expr_type == EXPR_NULL)
4297 gfc_conv_structure (&se, gfc_class_null_initializer(ts), 1);
4298 else
4299 gfc_conv_structure (&se, expr, 1);
4300 gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
4301 TREE_STATIC (se.expr) = 1;
4302 return se.expr;
4304 case BT_CHARACTER:
4306 tree ctor = gfc_conv_string_init (ts->u.cl->backend_decl,expr);
4307 TREE_STATIC (ctor) = 1;
4308 return ctor;
4311 default:
4312 gfc_init_se (&se, NULL);
4313 gfc_conv_constant (&se, expr);
4314 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
4315 return se.expr;
4320 static tree
4321 gfc_trans_subarray_assign (tree dest, gfc_component * cm, gfc_expr * expr)
4323 gfc_se rse;
4324 gfc_se lse;
4325 gfc_ss *rss;
4326 gfc_ss *lss;
4327 stmtblock_t body;
4328 stmtblock_t block;
4329 gfc_loopinfo loop;
4330 int n;
4331 tree tmp;
4333 gfc_start_block (&block);
4335 /* Initialize the scalarizer. */
4336 gfc_init_loopinfo (&loop);
4338 gfc_init_se (&lse, NULL);
4339 gfc_init_se (&rse, NULL);
4341 /* Walk the rhs. */
4342 rss = gfc_walk_expr (expr);
4343 if (rss == gfc_ss_terminator)
4345 /* The rhs is scalar. Add a ss for the expression. */
4346 rss = gfc_get_ss ();
4347 rss->next = gfc_ss_terminator;
4348 rss->type = GFC_SS_SCALAR;
4349 rss->expr = expr;
4352 /* Create a SS for the destination. */
4353 lss = gfc_get_ss ();
4354 lss->type = GFC_SS_COMPONENT;
4355 lss->expr = NULL;
4356 lss->shape = gfc_get_shape (cm->as->rank);
4357 lss->next = gfc_ss_terminator;
4358 lss->data.info.dimen = cm->as->rank;
4359 lss->data.info.descriptor = dest;
4360 lss->data.info.data = gfc_conv_array_data (dest);
4361 lss->data.info.offset = gfc_conv_array_offset (dest);
4362 for (n = 0; n < cm->as->rank; n++)
4364 lss->data.info.dim[n] = n;
4365 lss->data.info.start[n] = gfc_conv_array_lbound (dest, n);
4366 lss->data.info.stride[n] = gfc_index_one_node;
4368 mpz_init (lss->shape[n]);
4369 mpz_sub (lss->shape[n], cm->as->upper[n]->value.integer,
4370 cm->as->lower[n]->value.integer);
4371 mpz_add_ui (lss->shape[n], lss->shape[n], 1);
4374 /* Associate the SS with the loop. */
4375 gfc_add_ss_to_loop (&loop, lss);
4376 gfc_add_ss_to_loop (&loop, rss);
4378 /* Calculate the bounds of the scalarization. */
4379 gfc_conv_ss_startstride (&loop);
4381 /* Setup the scalarizing loops. */
4382 gfc_conv_loop_setup (&loop, &expr->where);
4384 /* Setup the gfc_se structures. */
4385 gfc_copy_loopinfo_to_se (&lse, &loop);
4386 gfc_copy_loopinfo_to_se (&rse, &loop);
4388 rse.ss = rss;
4389 gfc_mark_ss_chain_used (rss, 1);
4390 lse.ss = lss;
4391 gfc_mark_ss_chain_used (lss, 1);
4393 /* Start the scalarized loop body. */
4394 gfc_start_scalarized_body (&loop, &body);
4396 gfc_conv_tmp_array_ref (&lse);
4397 if (cm->ts.type == BT_CHARACTER)
4398 lse.string_length = cm->ts.u.cl->backend_decl;
4400 gfc_conv_expr (&rse, expr);
4402 tmp = gfc_trans_scalar_assign (&lse, &rse, cm->ts, true, false, true);
4403 gfc_add_expr_to_block (&body, tmp);
4405 gcc_assert (rse.ss == gfc_ss_terminator);
4407 /* Generate the copying loops. */
4408 gfc_trans_scalarizing_loops (&loop, &body);
4410 /* Wrap the whole thing up. */
4411 gfc_add_block_to_block (&block, &loop.pre);
4412 gfc_add_block_to_block (&block, &loop.post);
4414 for (n = 0; n < cm->as->rank; n++)
4415 mpz_clear (lss->shape[n]);
4416 free (lss->shape);
4418 gfc_cleanup_loop (&loop);
4420 return gfc_finish_block (&block);
4424 static tree
4425 gfc_trans_alloc_subarray_assign (tree dest, gfc_component * cm,
4426 gfc_expr * expr)
4428 gfc_se se;
4429 gfc_ss *rss;
4430 stmtblock_t block;
4431 tree offset;
4432 int n;
4433 tree tmp;
4434 tree tmp2;
4435 gfc_array_spec *as;
4436 gfc_expr *arg = NULL;
4438 gfc_start_block (&block);
4439 gfc_init_se (&se, NULL);
4441 /* Get the descriptor for the expressions. */
4442 rss = gfc_walk_expr (expr);
4443 se.want_pointer = 0;
4444 gfc_conv_expr_descriptor (&se, expr, rss);
4445 gfc_add_block_to_block (&block, &se.pre);
4446 gfc_add_modify (&block, dest, se.expr);
4448 /* Deal with arrays of derived types with allocatable components. */
4449 if (cm->ts.type == BT_DERIVED
4450 && cm->ts.u.derived->attr.alloc_comp)
4451 tmp = gfc_copy_alloc_comp (cm->ts.u.derived,
4452 se.expr, dest,
4453 cm->as->rank);
4454 else
4455 tmp = gfc_duplicate_allocatable (dest, se.expr,
4456 TREE_TYPE(cm->backend_decl),
4457 cm->as->rank);
4459 gfc_add_expr_to_block (&block, tmp);
4460 gfc_add_block_to_block (&block, &se.post);
4462 if (expr->expr_type != EXPR_VARIABLE)
4463 gfc_conv_descriptor_data_set (&block, se.expr,
4464 null_pointer_node);
4466 /* We need to know if the argument of a conversion function is a
4467 variable, so that the correct lower bound can be used. */
4468 if (expr->expr_type == EXPR_FUNCTION
4469 && expr->value.function.isym
4470 && expr->value.function.isym->conversion
4471 && expr->value.function.actual->expr
4472 && expr->value.function.actual->expr->expr_type == EXPR_VARIABLE)
4473 arg = expr->value.function.actual->expr;
4475 /* Obtain the array spec of full array references. */
4476 if (arg)
4477 as = gfc_get_full_arrayspec_from_expr (arg);
4478 else
4479 as = gfc_get_full_arrayspec_from_expr (expr);
4481 /* Shift the lbound and ubound of temporaries to being unity,
4482 rather than zero, based. Always calculate the offset. */
4483 offset = gfc_conv_descriptor_offset_get (dest);
4484 gfc_add_modify (&block, offset, gfc_index_zero_node);
4485 tmp2 =gfc_create_var (gfc_array_index_type, NULL);
4487 for (n = 0; n < expr->rank; n++)
4489 tree span;
4490 tree lbound;
4492 /* Obtain the correct lbound - ISO/IEC TR 15581:2001 page 9.
4493 TODO It looks as if gfc_conv_expr_descriptor should return
4494 the correct bounds and that the following should not be
4495 necessary. This would simplify gfc_conv_intrinsic_bound
4496 as well. */
4497 if (as && as->lower[n])
4499 gfc_se lbse;
4500 gfc_init_se (&lbse, NULL);
4501 gfc_conv_expr (&lbse, as->lower[n]);
4502 gfc_add_block_to_block (&block, &lbse.pre);
4503 lbound = gfc_evaluate_now (lbse.expr, &block);
4505 else if (as && arg)
4507 tmp = gfc_get_symbol_decl (arg->symtree->n.sym);
4508 lbound = gfc_conv_descriptor_lbound_get (tmp,
4509 gfc_rank_cst[n]);
4511 else if (as)
4512 lbound = gfc_conv_descriptor_lbound_get (dest,
4513 gfc_rank_cst[n]);
4514 else
4515 lbound = gfc_index_one_node;
4517 lbound = fold_convert (gfc_array_index_type, lbound);
4519 /* Shift the bounds and set the offset accordingly. */
4520 tmp = gfc_conv_descriptor_ubound_get (dest, gfc_rank_cst[n]);
4521 span = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
4522 tmp, gfc_conv_descriptor_lbound_get (dest, gfc_rank_cst[n]));
4523 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
4524 span, lbound);
4525 gfc_conv_descriptor_ubound_set (&block, dest,
4526 gfc_rank_cst[n], tmp);
4527 gfc_conv_descriptor_lbound_set (&block, dest,
4528 gfc_rank_cst[n], lbound);
4530 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
4531 gfc_conv_descriptor_lbound_get (dest,
4532 gfc_rank_cst[n]),
4533 gfc_conv_descriptor_stride_get (dest,
4534 gfc_rank_cst[n]));
4535 gfc_add_modify (&block, tmp2, tmp);
4536 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
4537 offset, tmp2);
4538 gfc_conv_descriptor_offset_set (&block, dest, tmp);
4541 if (arg)
4543 /* If a conversion expression has a null data pointer
4544 argument, nullify the allocatable component. */
4545 tree non_null_expr;
4546 tree null_expr;
4548 if (arg->symtree->n.sym->attr.allocatable
4549 || arg->symtree->n.sym->attr.pointer)
4551 non_null_expr = gfc_finish_block (&block);
4552 gfc_start_block (&block);
4553 gfc_conv_descriptor_data_set (&block, dest,
4554 null_pointer_node);
4555 null_expr = gfc_finish_block (&block);
4556 tmp = gfc_conv_descriptor_data_get (arg->symtree->n.sym->backend_decl);
4557 tmp = build2_loc (input_location, EQ_EXPR, boolean_type_node, tmp,
4558 fold_convert (TREE_TYPE (tmp), null_pointer_node));
4559 return build3_v (COND_EXPR, tmp,
4560 null_expr, non_null_expr);
4564 return gfc_finish_block (&block);
4568 /* Assign a single component of a derived type constructor. */
4570 static tree
4571 gfc_trans_subcomponent_assign (tree dest, gfc_component * cm, gfc_expr * expr)
4573 gfc_se se;
4574 gfc_se lse;
4575 gfc_ss *rss;
4576 stmtblock_t block;
4577 tree tmp;
4579 gfc_start_block (&block);
4581 if (cm->attr.pointer)
4583 gfc_init_se (&se, NULL);
4584 /* Pointer component. */
4585 if (cm->attr.dimension)
4587 /* Array pointer. */
4588 if (expr->expr_type == EXPR_NULL)
4589 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
4590 else
4592 rss = gfc_walk_expr (expr);
4593 se.direct_byref = 1;
4594 se.expr = dest;
4595 gfc_conv_expr_descriptor (&se, expr, rss);
4596 gfc_add_block_to_block (&block, &se.pre);
4597 gfc_add_block_to_block (&block, &se.post);
4600 else
4602 /* Scalar pointers. */
4603 se.want_pointer = 1;
4604 gfc_conv_expr (&se, expr);
4605 gfc_add_block_to_block (&block, &se.pre);
4606 gfc_add_modify (&block, dest,
4607 fold_convert (TREE_TYPE (dest), se.expr));
4608 gfc_add_block_to_block (&block, &se.post);
4611 else if (cm->ts.type == BT_CLASS && expr->expr_type == EXPR_NULL)
4613 /* NULL initialization for CLASS components. */
4614 tmp = gfc_trans_structure_assign (dest,
4615 gfc_class_null_initializer (&cm->ts));
4616 gfc_add_expr_to_block (&block, tmp);
4618 else if (cm->attr.dimension && !cm->attr.proc_pointer)
4620 if (cm->attr.allocatable && expr->expr_type == EXPR_NULL)
4621 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
4622 else if (cm->attr.allocatable)
4624 tmp = gfc_trans_alloc_subarray_assign (dest, cm, expr);
4625 gfc_add_expr_to_block (&block, tmp);
4627 else
4629 tmp = gfc_trans_subarray_assign (dest, cm, expr);
4630 gfc_add_expr_to_block (&block, tmp);
4633 else if (expr->ts.type == BT_DERIVED)
4635 if (expr->expr_type != EXPR_STRUCTURE)
4637 gfc_init_se (&se, NULL);
4638 gfc_conv_expr (&se, expr);
4639 gfc_add_block_to_block (&block, &se.pre);
4640 gfc_add_modify (&block, dest,
4641 fold_convert (TREE_TYPE (dest), se.expr));
4642 gfc_add_block_to_block (&block, &se.post);
4644 else
4646 /* Nested constructors. */
4647 tmp = gfc_trans_structure_assign (dest, expr);
4648 gfc_add_expr_to_block (&block, tmp);
4651 else
4653 /* Scalar component. */
4654 gfc_init_se (&se, NULL);
4655 gfc_init_se (&lse, NULL);
4657 gfc_conv_expr (&se, expr);
4658 if (cm->ts.type == BT_CHARACTER)
4659 lse.string_length = cm->ts.u.cl->backend_decl;
4660 lse.expr = dest;
4661 tmp = gfc_trans_scalar_assign (&lse, &se, cm->ts, true, false, true);
4662 gfc_add_expr_to_block (&block, tmp);
4664 return gfc_finish_block (&block);
4667 /* Assign a derived type constructor to a variable. */
4669 static tree
4670 gfc_trans_structure_assign (tree dest, gfc_expr * expr)
4672 gfc_constructor *c;
4673 gfc_component *cm;
4674 stmtblock_t block;
4675 tree field;
4676 tree tmp;
4678 gfc_start_block (&block);
4679 cm = expr->ts.u.derived->components;
4681 if (expr->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING
4682 && (expr->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
4683 || expr->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR))
4685 gfc_se se, lse;
4687 gcc_assert (cm->backend_decl == NULL);
4688 gfc_init_se (&se, NULL);
4689 gfc_init_se (&lse, NULL);
4690 gfc_conv_expr (&se, gfc_constructor_first (expr->value.constructor)->expr);
4691 lse.expr = dest;
4692 gfc_add_modify (&block, lse.expr,
4693 fold_convert (TREE_TYPE (lse.expr), se.expr));
4695 return gfc_finish_block (&block);
4698 for (c = gfc_constructor_first (expr->value.constructor);
4699 c; c = gfc_constructor_next (c), cm = cm->next)
4701 /* Skip absent members in default initializers. */
4702 if (!c->expr)
4703 continue;
4705 field = cm->backend_decl;
4706 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
4707 dest, field, NULL_TREE);
4708 tmp = gfc_trans_subcomponent_assign (tmp, cm, c->expr);
4709 gfc_add_expr_to_block (&block, tmp);
4711 return gfc_finish_block (&block);
4714 /* Build an expression for a constructor. If init is nonzero then
4715 this is part of a static variable initializer. */
4717 void
4718 gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
4720 gfc_constructor *c;
4721 gfc_component *cm;
4722 tree val;
4723 tree type;
4724 tree tmp;
4725 VEC(constructor_elt,gc) *v = NULL;
4727 gcc_assert (se->ss == NULL);
4728 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
4729 type = gfc_typenode_for_spec (&expr->ts);
4731 if (!init)
4733 /* Create a temporary variable and fill it in. */
4734 se->expr = gfc_create_var (type, expr->ts.u.derived->name);
4735 tmp = gfc_trans_structure_assign (se->expr, expr);
4736 gfc_add_expr_to_block (&se->pre, tmp);
4737 return;
4740 cm = expr->ts.u.derived->components;
4742 for (c = gfc_constructor_first (expr->value.constructor);
4743 c; c = gfc_constructor_next (c), cm = cm->next)
4745 /* Skip absent members in default initializers and allocatable
4746 components. Although the latter have a default initializer
4747 of EXPR_NULL,... by default, the static nullify is not needed
4748 since this is done every time we come into scope. */
4749 if (!c->expr || (cm->attr.allocatable && cm->attr.flavor != FL_PROCEDURE))
4750 continue;
4752 if (strcmp (cm->name, "_size") == 0)
4754 val = TYPE_SIZE_UNIT (gfc_get_derived_type (cm->ts.u.derived));
4755 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
4757 else if (cm->initializer && cm->initializer->expr_type != EXPR_NULL
4758 && strcmp (cm->name, "_extends") == 0)
4760 tree vtab;
4761 gfc_symbol *vtabs;
4762 vtabs = cm->initializer->symtree->n.sym;
4763 vtab = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtabs));
4764 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, vtab);
4766 else
4768 val = gfc_conv_initializer (c->expr, &cm->ts,
4769 TREE_TYPE (cm->backend_decl),
4770 cm->attr.dimension, cm->attr.pointer,
4771 cm->attr.proc_pointer);
4773 /* Append it to the constructor list. */
4774 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
4777 se->expr = build_constructor (type, v);
4778 if (init)
4779 TREE_CONSTANT (se->expr) = 1;
4783 /* Translate a substring expression. */
4785 static void
4786 gfc_conv_substring_expr (gfc_se * se, gfc_expr * expr)
4788 gfc_ref *ref;
4790 ref = expr->ref;
4792 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
4794 se->expr = gfc_build_wide_string_const (expr->ts.kind,
4795 expr->value.character.length,
4796 expr->value.character.string);
4798 se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
4799 TYPE_STRING_FLAG (TREE_TYPE (se->expr)) = 1;
4801 if (ref)
4802 gfc_conv_substring (se, ref, expr->ts.kind, NULL, &expr->where);
4806 /* Entry point for expression translation. Evaluates a scalar quantity.
4807 EXPR is the expression to be translated, and SE is the state structure if
4808 called from within the scalarized. */
4810 void
4811 gfc_conv_expr (gfc_se * se, gfc_expr * expr)
4813 if (se->ss && se->ss->expr == expr
4814 && (se->ss->type == GFC_SS_SCALAR || se->ss->type == GFC_SS_REFERENCE))
4816 /* Substitute a scalar expression evaluated outside the scalarization
4817 loop. */
4818 se->expr = se->ss->data.scalar.expr;
4819 if (se->ss->type == GFC_SS_REFERENCE)
4820 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
4821 se->string_length = se->ss->string_length;
4822 gfc_advance_se_ss_chain (se);
4823 return;
4826 /* We need to convert the expressions for the iso_c_binding derived types.
4827 C_NULL_PTR and C_NULL_FUNPTR will be made EXPR_NULL, which evaluates to
4828 null_pointer_node. C_PTR and C_FUNPTR are converted to match the
4829 typespec for the C_PTR and C_FUNPTR symbols, which has already been
4830 updated to be an integer with a kind equal to the size of a (void *). */
4831 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
4832 && expr->ts.u.derived->attr.is_iso_c)
4834 if (expr->expr_type == EXPR_VARIABLE
4835 && (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
4836 || expr->symtree->n.sym->intmod_sym_id
4837 == ISOCBINDING_NULL_FUNPTR))
4839 /* Set expr_type to EXPR_NULL, which will result in
4840 null_pointer_node being used below. */
4841 expr->expr_type = EXPR_NULL;
4843 else
4845 /* Update the type/kind of the expression to be what the new
4846 type/kind are for the updated symbols of C_PTR/C_FUNPTR. */
4847 expr->ts.type = expr->ts.u.derived->ts.type;
4848 expr->ts.f90_type = expr->ts.u.derived->ts.f90_type;
4849 expr->ts.kind = expr->ts.u.derived->ts.kind;
4853 switch (expr->expr_type)
4855 case EXPR_OP:
4856 gfc_conv_expr_op (se, expr);
4857 break;
4859 case EXPR_FUNCTION:
4860 gfc_conv_function_expr (se, expr);
4861 break;
4863 case EXPR_CONSTANT:
4864 gfc_conv_constant (se, expr);
4865 break;
4867 case EXPR_VARIABLE:
4868 gfc_conv_variable (se, expr);
4869 break;
4871 case EXPR_NULL:
4872 se->expr = null_pointer_node;
4873 break;
4875 case EXPR_SUBSTRING:
4876 gfc_conv_substring_expr (se, expr);
4877 break;
4879 case EXPR_STRUCTURE:
4880 gfc_conv_structure (se, expr, 0);
4881 break;
4883 case EXPR_ARRAY:
4884 gfc_conv_array_constructor_expr (se, expr);
4885 break;
4887 default:
4888 gcc_unreachable ();
4889 break;
4893 /* Like gfc_conv_expr_val, but the value is also suitable for use in the lhs
4894 of an assignment. */
4895 void
4896 gfc_conv_expr_lhs (gfc_se * se, gfc_expr * expr)
4898 gfc_conv_expr (se, expr);
4899 /* All numeric lvalues should have empty post chains. If not we need to
4900 figure out a way of rewriting an lvalue so that it has no post chain. */
4901 gcc_assert (expr->ts.type == BT_CHARACTER || !se->post.head);
4904 /* Like gfc_conv_expr, but the POST block is guaranteed to be empty for
4905 numeric expressions. Used for scalar values where inserting cleanup code
4906 is inconvenient. */
4907 void
4908 gfc_conv_expr_val (gfc_se * se, gfc_expr * expr)
4910 tree val;
4912 gcc_assert (expr->ts.type != BT_CHARACTER);
4913 gfc_conv_expr (se, expr);
4914 if (se->post.head)
4916 val = gfc_create_var (TREE_TYPE (se->expr), NULL);
4917 gfc_add_modify (&se->pre, val, se->expr);
4918 se->expr = val;
4919 gfc_add_block_to_block (&se->pre, &se->post);
4923 /* Helper to translate an expression and convert it to a particular type. */
4924 void
4925 gfc_conv_expr_type (gfc_se * se, gfc_expr * expr, tree type)
4927 gfc_conv_expr_val (se, expr);
4928 se->expr = convert (type, se->expr);
4932 /* Converts an expression so that it can be passed by reference. Scalar
4933 values only. */
4935 void
4936 gfc_conv_expr_reference (gfc_se * se, gfc_expr * expr)
4938 tree var;
4940 if (se->ss && se->ss->expr == expr
4941 && se->ss->type == GFC_SS_REFERENCE)
4943 /* Returns a reference to the scalar evaluated outside the loop
4944 for this case. */
4945 gfc_conv_expr (se, expr);
4946 return;
4949 if (expr->ts.type == BT_CHARACTER)
4951 gfc_conv_expr (se, expr);
4952 gfc_conv_string_parameter (se);
4953 return;
4956 if (expr->expr_type == EXPR_VARIABLE)
4958 se->want_pointer = 1;
4959 gfc_conv_expr (se, expr);
4960 if (se->post.head)
4962 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
4963 gfc_add_modify (&se->pre, var, se->expr);
4964 gfc_add_block_to_block (&se->pre, &se->post);
4965 se->expr = var;
4967 return;
4970 if (expr->expr_type == EXPR_FUNCTION
4971 && ((expr->value.function.esym
4972 && expr->value.function.esym->result->attr.pointer
4973 && !expr->value.function.esym->result->attr.dimension)
4974 || (!expr->value.function.esym
4975 && expr->symtree->n.sym->attr.pointer
4976 && !expr->symtree->n.sym->attr.dimension)))
4978 se->want_pointer = 1;
4979 gfc_conv_expr (se, expr);
4980 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
4981 gfc_add_modify (&se->pre, var, se->expr);
4982 se->expr = var;
4983 return;
4987 gfc_conv_expr (se, expr);
4989 /* Create a temporary var to hold the value. */
4990 if (TREE_CONSTANT (se->expr))
4992 tree tmp = se->expr;
4993 STRIP_TYPE_NOPS (tmp);
4994 var = build_decl (input_location,
4995 CONST_DECL, NULL, TREE_TYPE (tmp));
4996 DECL_INITIAL (var) = tmp;
4997 TREE_STATIC (var) = 1;
4998 pushdecl (var);
5000 else
5002 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
5003 gfc_add_modify (&se->pre, var, se->expr);
5005 gfc_add_block_to_block (&se->pre, &se->post);
5007 /* Take the address of that value. */
5008 se->expr = gfc_build_addr_expr (NULL_TREE, var);
5012 tree
5013 gfc_trans_pointer_assign (gfc_code * code)
5015 return gfc_trans_pointer_assignment (code->expr1, code->expr2);
5019 /* Generate code for a pointer assignment. */
5021 tree
5022 gfc_trans_pointer_assignment (gfc_expr * expr1, gfc_expr * expr2)
5024 gfc_se lse;
5025 gfc_se rse;
5026 gfc_ss *lss;
5027 gfc_ss *rss;
5028 stmtblock_t block;
5029 tree desc;
5030 tree tmp;
5031 tree decl;
5033 gfc_start_block (&block);
5035 gfc_init_se (&lse, NULL);
5037 lss = gfc_walk_expr (expr1);
5038 rss = gfc_walk_expr (expr2);
5039 if (lss == gfc_ss_terminator)
5041 /* Scalar pointers. */
5042 lse.want_pointer = 1;
5043 gfc_conv_expr (&lse, expr1);
5044 gcc_assert (rss == gfc_ss_terminator);
5045 gfc_init_se (&rse, NULL);
5046 rse.want_pointer = 1;
5047 gfc_conv_expr (&rse, expr2);
5049 if (expr1->symtree->n.sym->attr.proc_pointer
5050 && expr1->symtree->n.sym->attr.dummy)
5051 lse.expr = build_fold_indirect_ref_loc (input_location,
5052 lse.expr);
5054 if (expr2->symtree && expr2->symtree->n.sym->attr.proc_pointer
5055 && expr2->symtree->n.sym->attr.dummy)
5056 rse.expr = build_fold_indirect_ref_loc (input_location,
5057 rse.expr);
5059 gfc_add_block_to_block (&block, &lse.pre);
5060 gfc_add_block_to_block (&block, &rse.pre);
5062 /* Check character lengths if character expression. The test is only
5063 really added if -fbounds-check is enabled. Exclude deferred
5064 character length lefthand sides. */
5065 if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL
5066 && !(expr1->ts.deferred
5067 && (TREE_CODE (lse.string_length) == VAR_DECL))
5068 && !expr1->symtree->n.sym->attr.proc_pointer
5069 && !gfc_is_proc_ptr_comp (expr1, NULL))
5071 gcc_assert (expr2->ts.type == BT_CHARACTER);
5072 gcc_assert (lse.string_length && rse.string_length);
5073 gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
5074 lse.string_length, rse.string_length,
5075 &block);
5078 /* The assignment to an deferred character length sets the string
5079 length to that of the rhs. */
5080 if (expr1->ts.deferred && (TREE_CODE (lse.string_length) == VAR_DECL))
5082 if (expr2->expr_type != EXPR_NULL)
5083 gfc_add_modify (&block, lse.string_length, rse.string_length);
5084 else
5085 gfc_add_modify (&block, lse.string_length,
5086 build_int_cst (gfc_charlen_type_node, 0));
5089 gfc_add_modify (&block, lse.expr,
5090 fold_convert (TREE_TYPE (lse.expr), rse.expr));
5092 gfc_add_block_to_block (&block, &rse.post);
5093 gfc_add_block_to_block (&block, &lse.post);
5095 else
5097 gfc_ref* remap;
5098 bool rank_remap;
5099 tree strlen_lhs;
5100 tree strlen_rhs = NULL_TREE;
5102 /* Array pointer. Find the last reference on the LHS and if it is an
5103 array section ref, we're dealing with bounds remapping. In this case,
5104 set it to AR_FULL so that gfc_conv_expr_descriptor does
5105 not see it and process the bounds remapping afterwards explicitely. */
5106 for (remap = expr1->ref; remap; remap = remap->next)
5107 if (!remap->next && remap->type == REF_ARRAY
5108 && remap->u.ar.type == AR_SECTION)
5110 remap->u.ar.type = AR_FULL;
5111 break;
5113 rank_remap = (remap && remap->u.ar.end[0]);
5115 gfc_conv_expr_descriptor (&lse, expr1, lss);
5116 strlen_lhs = lse.string_length;
5117 desc = lse.expr;
5119 if (expr2->expr_type == EXPR_NULL)
5121 /* Just set the data pointer to null. */
5122 gfc_conv_descriptor_data_set (&lse.pre, lse.expr, null_pointer_node);
5124 else if (rank_remap)
5126 /* If we are rank-remapping, just get the RHS's descriptor and
5127 process this later on. */
5128 gfc_init_se (&rse, NULL);
5129 rse.direct_byref = 1;
5130 rse.byref_noassign = 1;
5131 gfc_conv_expr_descriptor (&rse, expr2, rss);
5132 strlen_rhs = rse.string_length;
5134 else if (expr2->expr_type == EXPR_VARIABLE)
5136 /* Assign directly to the LHS's descriptor. */
5137 lse.direct_byref = 1;
5138 gfc_conv_expr_descriptor (&lse, expr2, rss);
5139 strlen_rhs = lse.string_length;
5141 /* If this is a subreference array pointer assignment, use the rhs
5142 descriptor element size for the lhs span. */
5143 if (expr1->symtree->n.sym->attr.subref_array_pointer)
5145 decl = expr1->symtree->n.sym->backend_decl;
5146 gfc_init_se (&rse, NULL);
5147 rse.descriptor_only = 1;
5148 gfc_conv_expr (&rse, expr2);
5149 tmp = gfc_get_element_type (TREE_TYPE (rse.expr));
5150 tmp = fold_convert (gfc_array_index_type, size_in_bytes (tmp));
5151 if (!INTEGER_CST_P (tmp))
5152 gfc_add_block_to_block (&lse.post, &rse.pre);
5153 gfc_add_modify (&lse.post, GFC_DECL_SPAN(decl), tmp);
5156 else
5158 /* Assign to a temporary descriptor and then copy that
5159 temporary to the pointer. */
5160 tmp = gfc_create_var (TREE_TYPE (desc), "ptrtemp");
5162 lse.expr = tmp;
5163 lse.direct_byref = 1;
5164 gfc_conv_expr_descriptor (&lse, expr2, rss);
5165 strlen_rhs = lse.string_length;
5166 gfc_add_modify (&lse.pre, desc, tmp);
5169 gfc_add_block_to_block (&block, &lse.pre);
5170 if (rank_remap)
5171 gfc_add_block_to_block (&block, &rse.pre);
5173 /* If we do bounds remapping, update LHS descriptor accordingly. */
5174 if (remap)
5176 int dim;
5177 gcc_assert (remap->u.ar.dimen == expr1->rank);
5179 if (rank_remap)
5181 /* Do rank remapping. We already have the RHS's descriptor
5182 converted in rse and now have to build the correct LHS
5183 descriptor for it. */
5185 tree dtype, data;
5186 tree offs, stride;
5187 tree lbound, ubound;
5189 /* Set dtype. */
5190 dtype = gfc_conv_descriptor_dtype (desc);
5191 tmp = gfc_get_dtype (TREE_TYPE (desc));
5192 gfc_add_modify (&block, dtype, tmp);
5194 /* Copy data pointer. */
5195 data = gfc_conv_descriptor_data_get (rse.expr);
5196 gfc_conv_descriptor_data_set (&block, desc, data);
5198 /* Copy offset but adjust it such that it would correspond
5199 to a lbound of zero. */
5200 offs = gfc_conv_descriptor_offset_get (rse.expr);
5201 for (dim = 0; dim < expr2->rank; ++dim)
5203 stride = gfc_conv_descriptor_stride_get (rse.expr,
5204 gfc_rank_cst[dim]);
5205 lbound = gfc_conv_descriptor_lbound_get (rse.expr,
5206 gfc_rank_cst[dim]);
5207 tmp = fold_build2_loc (input_location, MULT_EXPR,
5208 gfc_array_index_type, stride, lbound);
5209 offs = fold_build2_loc (input_location, PLUS_EXPR,
5210 gfc_array_index_type, offs, tmp);
5212 gfc_conv_descriptor_offset_set (&block, desc, offs);
5214 /* Set the bounds as declared for the LHS and calculate strides as
5215 well as another offset update accordingly. */
5216 stride = gfc_conv_descriptor_stride_get (rse.expr,
5217 gfc_rank_cst[0]);
5218 for (dim = 0; dim < expr1->rank; ++dim)
5220 gfc_se lower_se;
5221 gfc_se upper_se;
5223 gcc_assert (remap->u.ar.start[dim] && remap->u.ar.end[dim]);
5225 /* Convert declared bounds. */
5226 gfc_init_se (&lower_se, NULL);
5227 gfc_init_se (&upper_se, NULL);
5228 gfc_conv_expr (&lower_se, remap->u.ar.start[dim]);
5229 gfc_conv_expr (&upper_se, remap->u.ar.end[dim]);
5231 gfc_add_block_to_block (&block, &lower_se.pre);
5232 gfc_add_block_to_block (&block, &upper_se.pre);
5234 lbound = fold_convert (gfc_array_index_type, lower_se.expr);
5235 ubound = fold_convert (gfc_array_index_type, upper_se.expr);
5237 lbound = gfc_evaluate_now (lbound, &block);
5238 ubound = gfc_evaluate_now (ubound, &block);
5240 gfc_add_block_to_block (&block, &lower_se.post);
5241 gfc_add_block_to_block (&block, &upper_se.post);
5243 /* Set bounds in descriptor. */
5244 gfc_conv_descriptor_lbound_set (&block, desc,
5245 gfc_rank_cst[dim], lbound);
5246 gfc_conv_descriptor_ubound_set (&block, desc,
5247 gfc_rank_cst[dim], ubound);
5249 /* Set stride. */
5250 stride = gfc_evaluate_now (stride, &block);
5251 gfc_conv_descriptor_stride_set (&block, desc,
5252 gfc_rank_cst[dim], stride);
5254 /* Update offset. */
5255 offs = gfc_conv_descriptor_offset_get (desc);
5256 tmp = fold_build2_loc (input_location, MULT_EXPR,
5257 gfc_array_index_type, lbound, stride);
5258 offs = fold_build2_loc (input_location, MINUS_EXPR,
5259 gfc_array_index_type, offs, tmp);
5260 offs = gfc_evaluate_now (offs, &block);
5261 gfc_conv_descriptor_offset_set (&block, desc, offs);
5263 /* Update stride. */
5264 tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
5265 stride = fold_build2_loc (input_location, MULT_EXPR,
5266 gfc_array_index_type, stride, tmp);
5269 else
5271 /* Bounds remapping. Just shift the lower bounds. */
5273 gcc_assert (expr1->rank == expr2->rank);
5275 for (dim = 0; dim < remap->u.ar.dimen; ++dim)
5277 gfc_se lbound_se;
5279 gcc_assert (remap->u.ar.start[dim]);
5280 gcc_assert (!remap->u.ar.end[dim]);
5281 gfc_init_se (&lbound_se, NULL);
5282 gfc_conv_expr (&lbound_se, remap->u.ar.start[dim]);
5284 gfc_add_block_to_block (&block, &lbound_se.pre);
5285 gfc_conv_shift_descriptor_lbound (&block, desc,
5286 dim, lbound_se.expr);
5287 gfc_add_block_to_block (&block, &lbound_se.post);
5292 /* Check string lengths if applicable. The check is only really added
5293 to the output code if -fbounds-check is enabled. */
5294 if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL)
5296 gcc_assert (expr2->ts.type == BT_CHARACTER);
5297 gcc_assert (strlen_lhs && strlen_rhs);
5298 gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
5299 strlen_lhs, strlen_rhs, &block);
5302 /* If rank remapping was done, check with -fcheck=bounds that
5303 the target is at least as large as the pointer. */
5304 if (rank_remap && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
5306 tree lsize, rsize;
5307 tree fault;
5308 const char* msg;
5310 lsize = gfc_conv_descriptor_size (lse.expr, expr1->rank);
5311 rsize = gfc_conv_descriptor_size (rse.expr, expr2->rank);
5313 lsize = gfc_evaluate_now (lsize, &block);
5314 rsize = gfc_evaluate_now (rsize, &block);
5315 fault = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
5316 rsize, lsize);
5318 msg = _("Target of rank remapping is too small (%ld < %ld)");
5319 gfc_trans_runtime_check (true, false, fault, &block, &expr2->where,
5320 msg, rsize, lsize);
5323 gfc_add_block_to_block (&block, &lse.post);
5324 if (rank_remap)
5325 gfc_add_block_to_block (&block, &rse.post);
5328 return gfc_finish_block (&block);
5332 /* Makes sure se is suitable for passing as a function string parameter. */
5333 /* TODO: Need to check all callers of this function. It may be abused. */
5335 void
5336 gfc_conv_string_parameter (gfc_se * se)
5338 tree type;
5340 if (TREE_CODE (se->expr) == STRING_CST)
5342 type = TREE_TYPE (TREE_TYPE (se->expr));
5343 se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
5344 return;
5347 if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
5349 if (TREE_CODE (se->expr) != INDIRECT_REF)
5351 type = TREE_TYPE (se->expr);
5352 se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
5354 else
5356 type = gfc_get_character_type_len (gfc_default_character_kind,
5357 se->string_length);
5358 type = build_pointer_type (type);
5359 se->expr = gfc_build_addr_expr (type, se->expr);
5363 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se->expr)));
5367 /* Generate code for assignment of scalar variables. Includes character
5368 strings and derived types with allocatable components.
5369 If you know that the LHS has no allocations, set dealloc to false. */
5371 tree
5372 gfc_trans_scalar_assign (gfc_se * lse, gfc_se * rse, gfc_typespec ts,
5373 bool l_is_temp, bool r_is_var, bool dealloc)
5375 stmtblock_t block;
5376 tree tmp;
5377 tree cond;
5379 gfc_init_block (&block);
5381 if (ts.type == BT_CHARACTER)
5383 tree rlen = NULL;
5384 tree llen = NULL;
5386 if (lse->string_length != NULL_TREE)
5388 gfc_conv_string_parameter (lse);
5389 gfc_add_block_to_block (&block, &lse->pre);
5390 llen = lse->string_length;
5393 if (rse->string_length != NULL_TREE)
5395 gcc_assert (rse->string_length != NULL_TREE);
5396 gfc_conv_string_parameter (rse);
5397 gfc_add_block_to_block (&block, &rse->pre);
5398 rlen = rse->string_length;
5401 gfc_trans_string_copy (&block, llen, lse->expr, ts.kind, rlen,
5402 rse->expr, ts.kind);
5404 else if (ts.type == BT_DERIVED && ts.u.derived->attr.alloc_comp)
5406 cond = NULL_TREE;
5408 /* Are the rhs and the lhs the same? */
5409 if (r_is_var)
5411 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
5412 gfc_build_addr_expr (NULL_TREE, lse->expr),
5413 gfc_build_addr_expr (NULL_TREE, rse->expr));
5414 cond = gfc_evaluate_now (cond, &lse->pre);
5417 /* Deallocate the lhs allocated components as long as it is not
5418 the same as the rhs. This must be done following the assignment
5419 to prevent deallocating data that could be used in the rhs
5420 expression. */
5421 if (!l_is_temp && dealloc)
5423 tmp = gfc_evaluate_now (lse->expr, &lse->pre);
5424 tmp = gfc_deallocate_alloc_comp (ts.u.derived, tmp, 0);
5425 if (r_is_var)
5426 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
5427 tmp);
5428 gfc_add_expr_to_block (&lse->post, tmp);
5431 gfc_add_block_to_block (&block, &rse->pre);
5432 gfc_add_block_to_block (&block, &lse->pre);
5434 gfc_add_modify (&block, lse->expr,
5435 fold_convert (TREE_TYPE (lse->expr), rse->expr));
5437 /* Do a deep copy if the rhs is a variable, if it is not the
5438 same as the lhs. */
5439 if (r_is_var)
5441 tmp = gfc_copy_alloc_comp (ts.u.derived, rse->expr, lse->expr, 0);
5442 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
5443 tmp);
5444 gfc_add_expr_to_block (&block, tmp);
5447 else if (ts.type == BT_DERIVED || ts.type == BT_CLASS)
5449 gfc_add_block_to_block (&block, &lse->pre);
5450 gfc_add_block_to_block (&block, &rse->pre);
5451 tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
5452 TREE_TYPE (lse->expr), rse->expr);
5453 gfc_add_modify (&block, lse->expr, tmp);
5455 else
5457 gfc_add_block_to_block (&block, &lse->pre);
5458 gfc_add_block_to_block (&block, &rse->pre);
5460 gfc_add_modify (&block, lse->expr,
5461 fold_convert (TREE_TYPE (lse->expr), rse->expr));
5464 gfc_add_block_to_block (&block, &lse->post);
5465 gfc_add_block_to_block (&block, &rse->post);
5467 return gfc_finish_block (&block);
5471 /* There are quite a lot of restrictions on the optimisation in using an
5472 array function assign without a temporary. */
5474 static bool
5475 arrayfunc_assign_needs_temporary (gfc_expr * expr1, gfc_expr * expr2)
5477 gfc_ref * ref;
5478 bool seen_array_ref;
5479 bool c = false;
5480 gfc_symbol *sym = expr1->symtree->n.sym;
5482 /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION. */
5483 if (expr2->value.function.isym && !gfc_is_intrinsic_libcall (expr2))
5484 return true;
5486 /* Elemental functions are scalarized so that they don't need a
5487 temporary in gfc_trans_assignment_1, so return a true. Otherwise,
5488 they would need special treatment in gfc_trans_arrayfunc_assign. */
5489 if (expr2->value.function.esym != NULL
5490 && expr2->value.function.esym->attr.elemental)
5491 return true;
5493 /* Need a temporary if rhs is not FULL or a contiguous section. */
5494 if (expr1->ref && !(gfc_full_array_ref_p (expr1->ref, &c) || c))
5495 return true;
5497 /* Need a temporary if EXPR1 can't be expressed as a descriptor. */
5498 if (gfc_ref_needs_temporary_p (expr1->ref))
5499 return true;
5501 /* Functions returning pointers or allocatables need temporaries. */
5502 c = expr2->value.function.esym
5503 ? (expr2->value.function.esym->attr.pointer
5504 || expr2->value.function.esym->attr.allocatable)
5505 : (expr2->symtree->n.sym->attr.pointer
5506 || expr2->symtree->n.sym->attr.allocatable);
5507 if (c)
5508 return true;
5510 /* Character array functions need temporaries unless the
5511 character lengths are the same. */
5512 if (expr2->ts.type == BT_CHARACTER && expr2->rank > 0)
5514 if (expr1->ts.u.cl->length == NULL
5515 || expr1->ts.u.cl->length->expr_type != EXPR_CONSTANT)
5516 return true;
5518 if (expr2->ts.u.cl->length == NULL
5519 || expr2->ts.u.cl->length->expr_type != EXPR_CONSTANT)
5520 return true;
5522 if (mpz_cmp (expr1->ts.u.cl->length->value.integer,
5523 expr2->ts.u.cl->length->value.integer) != 0)
5524 return true;
5527 /* Check that no LHS component references appear during an array
5528 reference. This is needed because we do not have the means to
5529 span any arbitrary stride with an array descriptor. This check
5530 is not needed for the rhs because the function result has to be
5531 a complete type. */
5532 seen_array_ref = false;
5533 for (ref = expr1->ref; ref; ref = ref->next)
5535 if (ref->type == REF_ARRAY)
5536 seen_array_ref= true;
5537 else if (ref->type == REF_COMPONENT && seen_array_ref)
5538 return true;
5541 /* Check for a dependency. */
5542 if (gfc_check_fncall_dependency (expr1, INTENT_OUT,
5543 expr2->value.function.esym,
5544 expr2->value.function.actual,
5545 NOT_ELEMENTAL))
5546 return true;
5548 /* If we have reached here with an intrinsic function, we do not
5549 need a temporary except in the particular case that reallocation
5550 on assignment is active and the lhs is allocatable and a target. */
5551 if (expr2->value.function.isym)
5552 return (gfc_option.flag_realloc_lhs
5553 && sym->attr.allocatable
5554 && sym->attr.target);
5556 /* If the LHS is a dummy, we need a temporary if it is not
5557 INTENT(OUT). */
5558 if (sym->attr.dummy && sym->attr.intent != INTENT_OUT)
5559 return true;
5561 /* If the lhs has been host_associated, is in common, a pointer or is
5562 a target and the function is not using a RESULT variable, aliasing
5563 can occur and a temporary is needed. */
5564 if ((sym->attr.host_assoc
5565 || sym->attr.in_common
5566 || sym->attr.pointer
5567 || sym->attr.cray_pointee
5568 || sym->attr.target)
5569 && expr2->symtree != NULL
5570 && expr2->symtree->n.sym == expr2->symtree->n.sym->result)
5571 return true;
5573 /* A PURE function can unconditionally be called without a temporary. */
5574 if (expr2->value.function.esym != NULL
5575 && expr2->value.function.esym->attr.pure)
5576 return false;
5578 /* Implicit_pure functions are those which could legally be declared
5579 to be PURE. */
5580 if (expr2->value.function.esym != NULL
5581 && expr2->value.function.esym->attr.implicit_pure)
5582 return false;
5584 if (!sym->attr.use_assoc
5585 && !sym->attr.in_common
5586 && !sym->attr.pointer
5587 && !sym->attr.target
5588 && !sym->attr.cray_pointee
5589 && expr2->value.function.esym)
5591 /* A temporary is not needed if the function is not contained and
5592 the variable is local or host associated and not a pointer or
5593 a target. */
5594 if (!expr2->value.function.esym->attr.contained)
5595 return false;
5597 /* A temporary is not needed if the lhs has never been host
5598 associated and the procedure is contained. */
5599 else if (!sym->attr.host_assoc)
5600 return false;
5602 /* A temporary is not needed if the variable is local and not
5603 a pointer, a target or a result. */
5604 if (sym->ns->parent
5605 && expr2->value.function.esym->ns == sym->ns->parent)
5606 return false;
5609 /* Default to temporary use. */
5610 return true;
5614 /* Provide the loop info so that the lhs descriptor can be built for
5615 reallocatable assignments from extrinsic function calls. */
5617 static void
5618 realloc_lhs_loop_for_fcn_call (gfc_se *se, locus *where, gfc_ss **ss,
5619 gfc_loopinfo *loop)
5621 /* Signal that the function call should not be made by
5622 gfc_conv_loop_setup. */
5623 se->ss->is_alloc_lhs = 1;
5624 gfc_init_loopinfo (loop);
5625 gfc_add_ss_to_loop (loop, *ss);
5626 gfc_add_ss_to_loop (loop, se->ss);
5627 gfc_conv_ss_startstride (loop);
5628 gfc_conv_loop_setup (loop, where);
5629 gfc_copy_loopinfo_to_se (se, loop);
5630 gfc_add_block_to_block (&se->pre, &loop->pre);
5631 gfc_add_block_to_block (&se->pre, &loop->post);
5632 se->ss->is_alloc_lhs = 0;
5636 /* For Assignment to a reallocatable lhs from intrinsic functions,
5637 replace the se.expr (ie. the result) with a temporary descriptor.
5638 Null the data field so that the library allocates space for the
5639 result. Free the data of the original descriptor after the function,
5640 in case it appears in an argument expression and transfer the
5641 result to the original descriptor. */
5643 static void
5644 fcncall_realloc_result (gfc_se *se, int rank)
5646 tree desc;
5647 tree res_desc;
5648 tree tmp;
5649 tree offset;
5650 int n;
5652 /* Use the allocation done by the library. Substitute the lhs
5653 descriptor with a copy, whose data field is nulled.*/
5654 desc = build_fold_indirect_ref_loc (input_location, se->expr);
5655 /* Unallocated, the descriptor does not have a dtype. */
5656 tmp = gfc_conv_descriptor_dtype (desc);
5657 gfc_add_modify (&se->pre, tmp, gfc_get_dtype (TREE_TYPE (desc)));
5658 res_desc = gfc_evaluate_now (desc, &se->pre);
5659 gfc_conv_descriptor_data_set (&se->pre, res_desc, null_pointer_node);
5660 se->expr = gfc_build_addr_expr (TREE_TYPE (se->expr), res_desc);
5662 /* Free the lhs after the function call and copy the result to
5663 the lhs descriptor. */
5664 tmp = gfc_conv_descriptor_data_get (desc);
5665 tmp = gfc_call_free (fold_convert (pvoid_type_node, tmp));
5666 gfc_add_expr_to_block (&se->post, tmp);
5667 gfc_add_modify (&se->post, desc, res_desc);
5669 offset = gfc_index_zero_node;
5670 tmp = gfc_index_one_node;
5671 /* Now reset the bounds from zero based to unity based. */
5672 for (n = 0 ; n < rank; n++)
5674 /* Accumulate the offset. */
5675 offset = fold_build2_loc (input_location, MINUS_EXPR,
5676 gfc_array_index_type,
5677 offset, tmp);
5678 /* Now do the bounds. */
5679 gfc_conv_descriptor_offset_set (&se->post, desc, tmp);
5680 tmp = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
5681 tmp = fold_build2_loc (input_location, PLUS_EXPR,
5682 gfc_array_index_type,
5683 tmp, gfc_index_one_node);
5684 gfc_conv_descriptor_lbound_set (&se->post, desc,
5685 gfc_rank_cst[n],
5686 gfc_index_one_node);
5687 gfc_conv_descriptor_ubound_set (&se->post, desc,
5688 gfc_rank_cst[n], tmp);
5690 /* The extent for the next contribution to offset. */
5691 tmp = fold_build2_loc (input_location, MINUS_EXPR,
5692 gfc_array_index_type,
5693 gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]),
5694 gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]));
5695 tmp = fold_build2_loc (input_location, PLUS_EXPR,
5696 gfc_array_index_type,
5697 tmp, gfc_index_one_node);
5699 gfc_conv_descriptor_offset_set (&se->post, desc, offset);
5704 /* Try to translate array(:) = func (...), where func is a transformational
5705 array function, without using a temporary. Returns NULL if this isn't the
5706 case. */
5708 static tree
5709 gfc_trans_arrayfunc_assign (gfc_expr * expr1, gfc_expr * expr2)
5711 gfc_se se;
5712 gfc_ss *ss;
5713 gfc_component *comp = NULL;
5714 gfc_loopinfo loop;
5716 if (arrayfunc_assign_needs_temporary (expr1, expr2))
5717 return NULL;
5719 /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
5720 functions. */
5721 gcc_assert (expr2->value.function.isym
5722 || (gfc_is_proc_ptr_comp (expr2, &comp)
5723 && comp && comp->attr.dimension)
5724 || (!comp && gfc_return_by_reference (expr2->value.function.esym)
5725 && expr2->value.function.esym->result->attr.dimension));
5727 ss = gfc_walk_expr (expr1);
5728 gcc_assert (ss != gfc_ss_terminator);
5729 gfc_init_se (&se, NULL);
5730 gfc_start_block (&se.pre);
5731 se.want_pointer = 1;
5733 gfc_conv_array_parameter (&se, expr1, ss, false, NULL, NULL, NULL);
5735 if (expr1->ts.type == BT_DERIVED
5736 && expr1->ts.u.derived->attr.alloc_comp)
5738 tree tmp;
5739 tmp = gfc_deallocate_alloc_comp (expr1->ts.u.derived, se.expr,
5740 expr1->rank);
5741 gfc_add_expr_to_block (&se.pre, tmp);
5744 se.direct_byref = 1;
5745 se.ss = gfc_walk_expr (expr2);
5746 gcc_assert (se.ss != gfc_ss_terminator);
5748 /* Reallocate on assignment needs the loopinfo for extrinsic functions.
5749 This is signalled to gfc_conv_procedure_call by setting is_alloc_lhs.
5750 Clearly, this cannot be done for an allocatable function result, since
5751 the shape of the result is unknown and, in any case, the function must
5752 correctly take care of the reallocation internally. For intrinsic
5753 calls, the array data is freed and the library takes care of allocation.
5754 TODO: Add logic of trans-array.c: gfc_alloc_allocatable_for_assignment
5755 to the library. */
5756 if (gfc_option.flag_realloc_lhs
5757 && gfc_is_reallocatable_lhs (expr1)
5758 && !gfc_expr_attr (expr1).codimension
5759 && !gfc_is_coindexed (expr1)
5760 && !(expr2->value.function.esym
5761 && expr2->value.function.esym->result->attr.allocatable))
5763 if (!expr2->value.function.isym)
5765 realloc_lhs_loop_for_fcn_call (&se, &expr1->where, &ss, &loop);
5766 ss->is_alloc_lhs = 1;
5768 else
5769 fcncall_realloc_result (&se, expr1->rank);
5772 gfc_conv_function_expr (&se, expr2);
5773 gfc_add_block_to_block (&se.pre, &se.post);
5775 return gfc_finish_block (&se.pre);
5779 /* Try to efficiently translate array(:) = 0. Return NULL if this
5780 can't be done. */
5782 static tree
5783 gfc_trans_zero_assign (gfc_expr * expr)
5785 tree dest, len, type;
5786 tree tmp;
5787 gfc_symbol *sym;
5789 sym = expr->symtree->n.sym;
5790 dest = gfc_get_symbol_decl (sym);
5792 type = TREE_TYPE (dest);
5793 if (POINTER_TYPE_P (type))
5794 type = TREE_TYPE (type);
5795 if (!GFC_ARRAY_TYPE_P (type))
5796 return NULL_TREE;
5798 /* Determine the length of the array. */
5799 len = GFC_TYPE_ARRAY_SIZE (type);
5800 if (!len || TREE_CODE (len) != INTEGER_CST)
5801 return NULL_TREE;
5803 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
5804 len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
5805 fold_convert (gfc_array_index_type, tmp));
5807 /* If we are zeroing a local array avoid taking its address by emitting
5808 a = {} instead. */
5809 if (!POINTER_TYPE_P (TREE_TYPE (dest)))
5810 return build2_loc (input_location, MODIFY_EXPR, void_type_node,
5811 dest, build_constructor (TREE_TYPE (dest), NULL));
5813 /* Convert arguments to the correct types. */
5814 dest = fold_convert (pvoid_type_node, dest);
5815 len = fold_convert (size_type_node, len);
5817 /* Construct call to __builtin_memset. */
5818 tmp = build_call_expr_loc (input_location,
5819 built_in_decls[BUILT_IN_MEMSET],
5820 3, dest, integer_zero_node, len);
5821 return fold_convert (void_type_node, tmp);
5825 /* Helper for gfc_trans_array_copy and gfc_trans_array_constructor_copy
5826 that constructs the call to __builtin_memcpy. */
5828 tree
5829 gfc_build_memcpy_call (tree dst, tree src, tree len)
5831 tree tmp;
5833 /* Convert arguments to the correct types. */
5834 if (!POINTER_TYPE_P (TREE_TYPE (dst)))
5835 dst = gfc_build_addr_expr (pvoid_type_node, dst);
5836 else
5837 dst = fold_convert (pvoid_type_node, dst);
5839 if (!POINTER_TYPE_P (TREE_TYPE (src)))
5840 src = gfc_build_addr_expr (pvoid_type_node, src);
5841 else
5842 src = fold_convert (pvoid_type_node, src);
5844 len = fold_convert (size_type_node, len);
5846 /* Construct call to __builtin_memcpy. */
5847 tmp = build_call_expr_loc (input_location,
5848 built_in_decls[BUILT_IN_MEMCPY], 3, dst, src, len);
5849 return fold_convert (void_type_node, tmp);
5853 /* Try to efficiently translate dst(:) = src(:). Return NULL if this
5854 can't be done. EXPR1 is the destination/lhs and EXPR2 is the
5855 source/rhs, both are gfc_full_array_ref_p which have been checked for
5856 dependencies. */
5858 static tree
5859 gfc_trans_array_copy (gfc_expr * expr1, gfc_expr * expr2)
5861 tree dst, dlen, dtype;
5862 tree src, slen, stype;
5863 tree tmp;
5865 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
5866 src = gfc_get_symbol_decl (expr2->symtree->n.sym);
5868 dtype = TREE_TYPE (dst);
5869 if (POINTER_TYPE_P (dtype))
5870 dtype = TREE_TYPE (dtype);
5871 stype = TREE_TYPE (src);
5872 if (POINTER_TYPE_P (stype))
5873 stype = TREE_TYPE (stype);
5875 if (!GFC_ARRAY_TYPE_P (dtype) || !GFC_ARRAY_TYPE_P (stype))
5876 return NULL_TREE;
5878 /* Determine the lengths of the arrays. */
5879 dlen = GFC_TYPE_ARRAY_SIZE (dtype);
5880 if (!dlen || TREE_CODE (dlen) != INTEGER_CST)
5881 return NULL_TREE;
5882 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
5883 dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
5884 dlen, fold_convert (gfc_array_index_type, tmp));
5886 slen = GFC_TYPE_ARRAY_SIZE (stype);
5887 if (!slen || TREE_CODE (slen) != INTEGER_CST)
5888 return NULL_TREE;
5889 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (stype));
5890 slen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
5891 slen, fold_convert (gfc_array_index_type, tmp));
5893 /* Sanity check that they are the same. This should always be
5894 the case, as we should already have checked for conformance. */
5895 if (!tree_int_cst_equal (slen, dlen))
5896 return NULL_TREE;
5898 return gfc_build_memcpy_call (dst, src, dlen);
5902 /* Try to efficiently translate array(:) = (/ ... /). Return NULL if
5903 this can't be done. EXPR1 is the destination/lhs for which
5904 gfc_full_array_ref_p is true, and EXPR2 is the source/rhs. */
5906 static tree
5907 gfc_trans_array_constructor_copy (gfc_expr * expr1, gfc_expr * expr2)
5909 unsigned HOST_WIDE_INT nelem;
5910 tree dst, dtype;
5911 tree src, stype;
5912 tree len;
5913 tree tmp;
5915 nelem = gfc_constant_array_constructor_p (expr2->value.constructor);
5916 if (nelem == 0)
5917 return NULL_TREE;
5919 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
5920 dtype = TREE_TYPE (dst);
5921 if (POINTER_TYPE_P (dtype))
5922 dtype = TREE_TYPE (dtype);
5923 if (!GFC_ARRAY_TYPE_P (dtype))
5924 return NULL_TREE;
5926 /* Determine the lengths of the array. */
5927 len = GFC_TYPE_ARRAY_SIZE (dtype);
5928 if (!len || TREE_CODE (len) != INTEGER_CST)
5929 return NULL_TREE;
5931 /* Confirm that the constructor is the same size. */
5932 if (compare_tree_int (len, nelem) != 0)
5933 return NULL_TREE;
5935 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
5936 len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
5937 fold_convert (gfc_array_index_type, tmp));
5939 stype = gfc_typenode_for_spec (&expr2->ts);
5940 src = gfc_build_constant_array_constructor (expr2, stype);
5942 stype = TREE_TYPE (src);
5943 if (POINTER_TYPE_P (stype))
5944 stype = TREE_TYPE (stype);
5946 return gfc_build_memcpy_call (dst, src, len);
5950 /* Tells whether the expression is to be treated as a variable reference. */
5952 static bool
5953 expr_is_variable (gfc_expr *expr)
5955 gfc_expr *arg;
5957 if (expr->expr_type == EXPR_VARIABLE)
5958 return true;
5960 arg = gfc_get_noncopying_intrinsic_argument (expr);
5961 if (arg)
5963 gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
5964 return expr_is_variable (arg);
5967 return false;
5971 /* Is the lhs OK for automatic reallocation? */
5973 static bool
5974 is_scalar_reallocatable_lhs (gfc_expr *expr)
5976 gfc_ref * ref;
5978 /* An allocatable variable with no reference. */
5979 if (expr->symtree->n.sym->attr.allocatable
5980 && !expr->ref)
5981 return true;
5983 /* All that can be left are allocatable components. */
5984 if ((expr->symtree->n.sym->ts.type != BT_DERIVED
5985 && expr->symtree->n.sym->ts.type != BT_CLASS)
5986 || !expr->symtree->n.sym->ts.u.derived->attr.alloc_comp)
5987 return false;
5989 /* Find an allocatable component ref last. */
5990 for (ref = expr->ref; ref; ref = ref->next)
5991 if (ref->type == REF_COMPONENT
5992 && !ref->next
5993 && ref->u.c.component->attr.allocatable)
5994 return true;
5996 return false;
6000 /* Allocate or reallocate scalar lhs, as necessary. */
6002 static void
6003 alloc_scalar_allocatable_for_assignment (stmtblock_t *block,
6004 tree string_length,
6005 gfc_expr *expr1,
6006 gfc_expr *expr2)
6009 tree cond;
6010 tree tmp;
6011 tree size;
6012 tree size_in_bytes;
6013 tree jump_label1;
6014 tree jump_label2;
6015 gfc_se lse;
6017 if (!expr1 || expr1->rank)
6018 return;
6020 if (!expr2 || expr2->rank)
6021 return;
6023 /* Since this is a scalar lhs, we can afford to do this. That is,
6024 there is no risk of side effects being repeated. */
6025 gfc_init_se (&lse, NULL);
6026 lse.want_pointer = 1;
6027 gfc_conv_expr (&lse, expr1);
6029 jump_label1 = gfc_build_label_decl (NULL_TREE);
6030 jump_label2 = gfc_build_label_decl (NULL_TREE);
6032 /* Do the allocation if the lhs is NULL. Otherwise go to label 1. */
6033 tmp = build_int_cst (TREE_TYPE (lse.expr), 0);
6034 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
6035 lse.expr, tmp);
6036 tmp = build3_v (COND_EXPR, cond,
6037 build1_v (GOTO_EXPR, jump_label1),
6038 build_empty_stmt (input_location));
6039 gfc_add_expr_to_block (block, tmp);
6041 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
6043 /* Use the rhs string length and the lhs element size. */
6044 size = string_length;
6045 tmp = TREE_TYPE (gfc_typenode_for_spec (&expr1->ts));
6046 tmp = TYPE_SIZE_UNIT (tmp);
6047 size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
6048 TREE_TYPE (tmp), tmp,
6049 fold_convert (TREE_TYPE (tmp), size));
6051 else
6053 /* Otherwise use the length in bytes of the rhs. */
6054 size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr1->ts));
6055 size_in_bytes = size;
6058 tmp = build_call_expr_loc (input_location,
6059 built_in_decls[BUILT_IN_MALLOC], 1,
6060 size_in_bytes);
6061 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
6062 gfc_add_modify (block, lse.expr, tmp);
6063 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
6065 /* Deferred characters need checking for lhs and rhs string
6066 length. Other deferred parameter variables will have to
6067 come here too. */
6068 tmp = build1_v (GOTO_EXPR, jump_label2);
6069 gfc_add_expr_to_block (block, tmp);
6071 tmp = build1_v (LABEL_EXPR, jump_label1);
6072 gfc_add_expr_to_block (block, tmp);
6074 /* For a deferred length character, reallocate if lengths of lhs and
6075 rhs are different. */
6076 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
6078 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
6079 expr1->ts.u.cl->backend_decl, size);
6080 /* Jump past the realloc if the lengths are the same. */
6081 tmp = build3_v (COND_EXPR, cond,
6082 build1_v (GOTO_EXPR, jump_label2),
6083 build_empty_stmt (input_location));
6084 gfc_add_expr_to_block (block, tmp);
6085 tmp = build_call_expr_loc (input_location,
6086 built_in_decls[BUILT_IN_REALLOC], 2,
6087 fold_convert (pvoid_type_node, lse.expr),
6088 size_in_bytes);
6089 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
6090 gfc_add_modify (block, lse.expr, tmp);
6091 tmp = build1_v (LABEL_EXPR, jump_label2);
6092 gfc_add_expr_to_block (block, tmp);
6094 /* Update the lhs character length. */
6095 size = string_length;
6096 gfc_add_modify (block, expr1->ts.u.cl->backend_decl, size);
6101 /* Subroutine of gfc_trans_assignment that actually scalarizes the
6102 assignment. EXPR1 is the destination/LHS and EXPR2 is the source/RHS.
6103 init_flag indicates initialization expressions and dealloc that no
6104 deallocate prior assignment is needed (if in doubt, set true). */
6106 static tree
6107 gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
6108 bool dealloc)
6110 gfc_se lse;
6111 gfc_se rse;
6112 gfc_ss *lss;
6113 gfc_ss *lss_section;
6114 gfc_ss *rss;
6115 gfc_loopinfo loop;
6116 tree tmp;
6117 stmtblock_t block;
6118 stmtblock_t body;
6119 bool l_is_temp;
6120 bool scalar_to_array;
6121 bool def_clen_func;
6122 tree string_length;
6123 int n;
6125 /* Assignment of the form lhs = rhs. */
6126 gfc_start_block (&block);
6128 gfc_init_se (&lse, NULL);
6129 gfc_init_se (&rse, NULL);
6131 /* Walk the lhs. */
6132 lss = gfc_walk_expr (expr1);
6133 if (gfc_is_reallocatable_lhs (expr1)
6134 && !(expr2->expr_type == EXPR_FUNCTION
6135 && expr2->value.function.isym != NULL))
6136 lss->is_alloc_lhs = 1;
6137 rss = NULL;
6138 if (lss != gfc_ss_terminator)
6140 /* Allow the scalarizer to workshare array assignments. */
6141 if (ompws_flags & OMPWS_WORKSHARE_FLAG)
6142 ompws_flags |= OMPWS_SCALARIZER_WS;
6144 /* The assignment needs scalarization. */
6145 lss_section = lss;
6147 /* Find a non-scalar SS from the lhs. */
6148 while (lss_section != gfc_ss_terminator
6149 && lss_section->type != GFC_SS_SECTION)
6150 lss_section = lss_section->next;
6152 gcc_assert (lss_section != gfc_ss_terminator);
6154 /* Initialize the scalarizer. */
6155 gfc_init_loopinfo (&loop);
6157 /* Walk the rhs. */
6158 rss = gfc_walk_expr (expr2);
6159 if (rss == gfc_ss_terminator)
6161 /* The rhs is scalar. Add a ss for the expression. */
6162 rss = gfc_get_ss ();
6163 rss->next = gfc_ss_terminator;
6164 rss->type = GFC_SS_SCALAR;
6165 rss->expr = expr2;
6167 /* Associate the SS with the loop. */
6168 gfc_add_ss_to_loop (&loop, lss);
6169 gfc_add_ss_to_loop (&loop, rss);
6171 /* Calculate the bounds of the scalarization. */
6172 gfc_conv_ss_startstride (&loop);
6173 /* Enable loop reversal. */
6174 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
6175 loop.reverse[n] = GFC_ENABLE_REVERSE;
6176 /* Resolve any data dependencies in the statement. */
6177 gfc_conv_resolve_dependencies (&loop, lss, rss);
6178 /* Setup the scalarizing loops. */
6179 gfc_conv_loop_setup (&loop, &expr2->where);
6181 /* Setup the gfc_se structures. */
6182 gfc_copy_loopinfo_to_se (&lse, &loop);
6183 gfc_copy_loopinfo_to_se (&rse, &loop);
6185 rse.ss = rss;
6186 gfc_mark_ss_chain_used (rss, 1);
6187 if (loop.temp_ss == NULL)
6189 lse.ss = lss;
6190 gfc_mark_ss_chain_used (lss, 1);
6192 else
6194 lse.ss = loop.temp_ss;
6195 gfc_mark_ss_chain_used (lss, 3);
6196 gfc_mark_ss_chain_used (loop.temp_ss, 3);
6199 /* Start the scalarized loop body. */
6200 gfc_start_scalarized_body (&loop, &body);
6202 else
6203 gfc_init_block (&body);
6205 l_is_temp = (lss != gfc_ss_terminator && loop.temp_ss != NULL);
6207 /* Translate the expression. */
6208 gfc_conv_expr (&rse, expr2);
6210 /* Stabilize a string length for temporaries. */
6211 if (expr2->ts.type == BT_CHARACTER)
6212 string_length = gfc_evaluate_now (rse.string_length, &rse.pre);
6213 else
6214 string_length = NULL_TREE;
6216 if (l_is_temp)
6218 gfc_conv_tmp_array_ref (&lse);
6219 if (expr2->ts.type == BT_CHARACTER)
6220 lse.string_length = string_length;
6222 else
6223 gfc_conv_expr (&lse, expr1);
6225 /* Assignments of scalar derived types with allocatable components
6226 to arrays must be done with a deep copy and the rhs temporary
6227 must have its components deallocated afterwards. */
6228 scalar_to_array = (expr2->ts.type == BT_DERIVED
6229 && expr2->ts.u.derived->attr.alloc_comp
6230 && !expr_is_variable (expr2)
6231 && !gfc_is_constant_expr (expr2)
6232 && expr1->rank && !expr2->rank);
6233 if (scalar_to_array && dealloc)
6235 tmp = gfc_deallocate_alloc_comp (expr2->ts.u.derived, rse.expr, 0);
6236 gfc_add_expr_to_block (&loop.post, tmp);
6239 /* For a deferred character length function, the function call must
6240 happen before the (re)allocation of the lhs, otherwise the character
6241 length of the result is not known. */
6242 def_clen_func = (((expr2->expr_type == EXPR_FUNCTION)
6243 || (expr2->expr_type == EXPR_COMPCALL)
6244 || (expr2->expr_type == EXPR_PPC))
6245 && expr2->ts.deferred);
6246 if (gfc_option.flag_realloc_lhs
6247 && expr2->ts.type == BT_CHARACTER
6248 && (def_clen_func || expr2->expr_type == EXPR_OP)
6249 && expr1->ts.deferred)
6250 gfc_add_block_to_block (&block, &rse.pre);
6252 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
6253 l_is_temp || init_flag,
6254 expr_is_variable (expr2) || scalar_to_array
6255 || expr2->expr_type == EXPR_ARRAY, dealloc);
6256 gfc_add_expr_to_block (&body, tmp);
6258 if (lss == gfc_ss_terminator)
6260 /* F2003: Add the code for reallocation on assignment. */
6261 if (gfc_option.flag_realloc_lhs
6262 && is_scalar_reallocatable_lhs (expr1))
6263 alloc_scalar_allocatable_for_assignment (&block, rse.string_length,
6264 expr1, expr2);
6266 /* Use the scalar assignment as is. */
6267 gfc_add_block_to_block (&block, &body);
6269 else
6271 gcc_assert (lse.ss == gfc_ss_terminator
6272 && rse.ss == gfc_ss_terminator);
6274 if (l_is_temp)
6276 gfc_trans_scalarized_loop_boundary (&loop, &body);
6278 /* We need to copy the temporary to the actual lhs. */
6279 gfc_init_se (&lse, NULL);
6280 gfc_init_se (&rse, NULL);
6281 gfc_copy_loopinfo_to_se (&lse, &loop);
6282 gfc_copy_loopinfo_to_se (&rse, &loop);
6284 rse.ss = loop.temp_ss;
6285 lse.ss = lss;
6287 gfc_conv_tmp_array_ref (&rse);
6288 gfc_conv_expr (&lse, expr1);
6290 gcc_assert (lse.ss == gfc_ss_terminator
6291 && rse.ss == gfc_ss_terminator);
6293 if (expr2->ts.type == BT_CHARACTER)
6294 rse.string_length = string_length;
6296 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
6297 false, false, dealloc);
6298 gfc_add_expr_to_block (&body, tmp);
6301 /* F2003: Allocate or reallocate lhs of allocatable array. */
6302 if (gfc_option.flag_realloc_lhs
6303 && gfc_is_reallocatable_lhs (expr1)
6304 && !gfc_expr_attr (expr1).codimension
6305 && !gfc_is_coindexed (expr1))
6307 tmp = gfc_alloc_allocatable_for_assignment (&loop, expr1, expr2);
6308 if (tmp != NULL_TREE)
6309 gfc_add_expr_to_block (&loop.code[expr1->rank - 1], tmp);
6312 /* Generate the copying loops. */
6313 gfc_trans_scalarizing_loops (&loop, &body);
6315 /* Wrap the whole thing up. */
6316 gfc_add_block_to_block (&block, &loop.pre);
6317 gfc_add_block_to_block (&block, &loop.post);
6319 gfc_cleanup_loop (&loop);
6322 return gfc_finish_block (&block);
6326 /* Check whether EXPR is a copyable array. */
6328 static bool
6329 copyable_array_p (gfc_expr * expr)
6331 if (expr->expr_type != EXPR_VARIABLE)
6332 return false;
6334 /* First check it's an array. */
6335 if (expr->rank < 1 || !expr->ref || expr->ref->next)
6336 return false;
6338 if (!gfc_full_array_ref_p (expr->ref, NULL))
6339 return false;
6341 /* Next check that it's of a simple enough type. */
6342 switch (expr->ts.type)
6344 case BT_INTEGER:
6345 case BT_REAL:
6346 case BT_COMPLEX:
6347 case BT_LOGICAL:
6348 return true;
6350 case BT_CHARACTER:
6351 return false;
6353 case BT_DERIVED:
6354 return !expr->ts.u.derived->attr.alloc_comp;
6356 default:
6357 break;
6360 return false;
6363 /* Translate an assignment. */
6365 tree
6366 gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
6367 bool dealloc)
6369 tree tmp;
6371 /* Special case a single function returning an array. */
6372 if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
6374 tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
6375 if (tmp)
6376 return tmp;
6379 /* Special case assigning an array to zero. */
6380 if (copyable_array_p (expr1)
6381 && is_zero_initializer_p (expr2))
6383 tmp = gfc_trans_zero_assign (expr1);
6384 if (tmp)
6385 return tmp;
6388 /* Special case copying one array to another. */
6389 if (copyable_array_p (expr1)
6390 && copyable_array_p (expr2)
6391 && gfc_compare_types (&expr1->ts, &expr2->ts)
6392 && !gfc_check_dependency (expr1, expr2, 0))
6394 tmp = gfc_trans_array_copy (expr1, expr2);
6395 if (tmp)
6396 return tmp;
6399 /* Special case initializing an array from a constant array constructor. */
6400 if (copyable_array_p (expr1)
6401 && expr2->expr_type == EXPR_ARRAY
6402 && gfc_compare_types (&expr1->ts, &expr2->ts))
6404 tmp = gfc_trans_array_constructor_copy (expr1, expr2);
6405 if (tmp)
6406 return tmp;
6409 /* Fallback to the scalarizer to generate explicit loops. */
6410 return gfc_trans_assignment_1 (expr1, expr2, init_flag, dealloc);
6413 tree
6414 gfc_trans_init_assign (gfc_code * code)
6416 return gfc_trans_assignment (code->expr1, code->expr2, true, false);
6419 tree
6420 gfc_trans_assign (gfc_code * code)
6422 return gfc_trans_assignment (code->expr1, code->expr2, false, true);
6426 /* Special case for initializing a polymorphic dummy with INTENT(OUT).
6427 A MEMCPY is needed to copy the full data from the default initializer
6428 of the dynamic type. */
6430 tree
6431 gfc_trans_class_init_assign (gfc_code *code)
6433 stmtblock_t block;
6434 tree tmp;
6435 gfc_se dst,src,memsz;
6436 gfc_expr *lhs,*rhs,*sz;
6438 gfc_start_block (&block);
6440 lhs = gfc_copy_expr (code->expr1);
6441 gfc_add_data_component (lhs);
6443 rhs = gfc_copy_expr (code->expr1);
6444 gfc_add_vptr_component (rhs);
6446 /* Make sure that the component backend_decls have been built, which
6447 will not have happened if the derived types concerned have not
6448 been referenced. */
6449 gfc_get_derived_type (rhs->ts.u.derived);
6450 gfc_add_def_init_component (rhs);
6452 sz = gfc_copy_expr (code->expr1);
6453 gfc_add_vptr_component (sz);
6454 gfc_add_size_component (sz);
6456 gfc_init_se (&dst, NULL);
6457 gfc_init_se (&src, NULL);
6458 gfc_init_se (&memsz, NULL);
6459 gfc_conv_expr (&dst, lhs);
6460 gfc_conv_expr (&src, rhs);
6461 gfc_conv_expr (&memsz, sz);
6462 gfc_add_block_to_block (&block, &src.pre);
6463 tmp = gfc_build_memcpy_call (dst.expr, src.expr, memsz.expr);
6464 gfc_add_expr_to_block (&block, tmp);
6466 return gfc_finish_block (&block);
6470 /* Translate an assignment to a CLASS object
6471 (pointer or ordinary assignment). */
6473 tree
6474 gfc_trans_class_assign (gfc_expr *expr1, gfc_expr *expr2, gfc_exec_op op)
6476 stmtblock_t block;
6477 tree tmp;
6478 gfc_expr *lhs;
6479 gfc_expr *rhs;
6481 gfc_start_block (&block);
6483 if (expr2->ts.type != BT_CLASS)
6485 /* Insert an additional assignment which sets the '_vptr' field. */
6486 gfc_symbol *vtab = NULL;
6487 gfc_symtree *st;
6489 lhs = gfc_copy_expr (expr1);
6490 gfc_add_vptr_component (lhs);
6492 if (expr2->ts.type == BT_DERIVED)
6493 vtab = gfc_find_derived_vtab (expr2->ts.u.derived);
6494 else if (expr2->expr_type == EXPR_NULL)
6495 vtab = gfc_find_derived_vtab (expr1->ts.u.derived);
6496 gcc_assert (vtab);
6498 rhs = gfc_get_expr ();
6499 rhs->expr_type = EXPR_VARIABLE;
6500 gfc_find_sym_tree (vtab->name, vtab->ns, 1, &st);
6501 rhs->symtree = st;
6502 rhs->ts = vtab->ts;
6504 tmp = gfc_trans_pointer_assignment (lhs, rhs);
6505 gfc_add_expr_to_block (&block, tmp);
6507 gfc_free_expr (lhs);
6508 gfc_free_expr (rhs);
6511 /* Do the actual CLASS assignment. */
6512 if (expr2->ts.type == BT_CLASS)
6513 op = EXEC_ASSIGN;
6514 else
6515 gfc_add_data_component (expr1);
6517 if (op == EXEC_ASSIGN)
6518 tmp = gfc_trans_assignment (expr1, expr2, false, true);
6519 else if (op == EXEC_POINTER_ASSIGN)
6520 tmp = gfc_trans_pointer_assignment (expr1, expr2);
6521 else
6522 gcc_unreachable();
6524 gfc_add_expr_to_block (&block, tmp);
6526 return gfc_finish_block (&block);