make __stl_prime_list in comdat
[official-gcc.git] / gcc / fortran / trans-expr.c
blobcf9f0f7cdb96ce80f59deda27434d5c6857bde52
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;
86 gfc_ss *ss;
88 gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
90 p = se;
91 /* Walk down the parent chain. */
92 while (p != NULL)
94 /* Simple consistency check. */
95 gcc_assert (p->parent == NULL || p->parent->ss == p->ss
96 || p->parent->ss->nested_ss == p->ss);
98 /* If we were in a nested loop, the next scalarized expression can be
99 on the parent ss' next pointer. Thus we should not take the next
100 pointer blindly, but rather go up one nest level as long as next
101 is the end of chain. */
102 ss = p->ss;
103 while (ss->next == gfc_ss_terminator && ss->parent != NULL)
104 ss = ss->parent;
106 p->ss = ss->next;
108 p = p->parent;
113 /* Ensures the result of the expression as either a temporary variable
114 or a constant so that it can be used repeatedly. */
116 void
117 gfc_make_safe_expr (gfc_se * se)
119 tree var;
121 if (CONSTANT_CLASS_P (se->expr))
122 return;
124 /* We need a temporary for this result. */
125 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
126 gfc_add_modify (&se->pre, var, se->expr);
127 se->expr = var;
131 /* Return an expression which determines if a dummy parameter is present.
132 Also used for arguments to procedures with multiple entry points. */
134 tree
135 gfc_conv_expr_present (gfc_symbol * sym)
137 tree decl, cond;
139 gcc_assert (sym->attr.dummy);
141 decl = gfc_get_symbol_decl (sym);
142 if (TREE_CODE (decl) != PARM_DECL)
144 /* Array parameters use a temporary descriptor, we want the real
145 parameter. */
146 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
147 || GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
148 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
151 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, decl,
152 fold_convert (TREE_TYPE (decl), null_pointer_node));
154 /* Fortran 2008 allows to pass null pointers and non-associated pointers
155 as actual argument to denote absent dummies. For array descriptors,
156 we thus also need to check the array descriptor. */
157 if (!sym->attr.pointer && !sym->attr.allocatable
158 && sym->as && sym->as->type == AS_ASSUMED_SHAPE
159 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
161 tree tmp;
162 tmp = build_fold_indirect_ref_loc (input_location, decl);
163 tmp = gfc_conv_array_data (tmp);
164 tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, tmp,
165 fold_convert (TREE_TYPE (tmp), null_pointer_node));
166 cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
167 boolean_type_node, cond, tmp);
170 return cond;
174 /* Converts a missing, dummy argument into a null or zero. */
176 void
177 gfc_conv_missing_dummy (gfc_se * se, gfc_expr * arg, gfc_typespec ts, int kind)
179 tree present;
180 tree tmp;
182 present = gfc_conv_expr_present (arg->symtree->n.sym);
184 if (kind > 0)
186 /* Create a temporary and convert it to the correct type. */
187 tmp = gfc_get_int_type (kind);
188 tmp = fold_convert (tmp, build_fold_indirect_ref_loc (input_location,
189 se->expr));
191 /* Test for a NULL value. */
192 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), present,
193 tmp, fold_convert (TREE_TYPE (tmp), integer_one_node));
194 tmp = gfc_evaluate_now (tmp, &se->pre);
195 se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
197 else
199 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se->expr),
200 present, se->expr,
201 build_zero_cst (TREE_TYPE (se->expr)));
202 tmp = gfc_evaluate_now (tmp, &se->pre);
203 se->expr = tmp;
206 if (ts.type == BT_CHARACTER)
208 tmp = build_int_cst (gfc_charlen_type_node, 0);
209 tmp = fold_build3_loc (input_location, COND_EXPR, gfc_charlen_type_node,
210 present, se->string_length, tmp);
211 tmp = gfc_evaluate_now (tmp, &se->pre);
212 se->string_length = tmp;
214 return;
218 /* Get the character length of an expression, looking through gfc_refs
219 if necessary. */
221 tree
222 gfc_get_expr_charlen (gfc_expr *e)
224 gfc_ref *r;
225 tree length;
227 gcc_assert (e->expr_type == EXPR_VARIABLE
228 && e->ts.type == BT_CHARACTER);
230 length = NULL; /* To silence compiler warning. */
232 if (is_subref_array (e) && e->ts.u.cl->length)
234 gfc_se tmpse;
235 gfc_init_se (&tmpse, NULL);
236 gfc_conv_expr_type (&tmpse, e->ts.u.cl->length, gfc_charlen_type_node);
237 e->ts.u.cl->backend_decl = tmpse.expr;
238 return tmpse.expr;
241 /* First candidate: if the variable is of type CHARACTER, the
242 expression's length could be the length of the character
243 variable. */
244 if (e->symtree->n.sym->ts.type == BT_CHARACTER)
245 length = e->symtree->n.sym->ts.u.cl->backend_decl;
247 /* Look through the reference chain for component references. */
248 for (r = e->ref; r; r = r->next)
250 switch (r->type)
252 case REF_COMPONENT:
253 if (r->u.c.component->ts.type == BT_CHARACTER)
254 length = r->u.c.component->ts.u.cl->backend_decl;
255 break;
257 case REF_ARRAY:
258 /* Do nothing. */
259 break;
261 default:
262 /* We should never got substring references here. These will be
263 broken down by the scalarizer. */
264 gcc_unreachable ();
265 break;
269 gcc_assert (length != NULL);
270 return length;
274 /* Return for an expression the backend decl of the coarray. */
276 static tree
277 get_tree_for_caf_expr (gfc_expr *expr)
279 tree caf_decl = NULL_TREE;
280 gfc_ref *ref;
282 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE);
283 if (expr->symtree->n.sym->attr.codimension)
284 caf_decl = expr->symtree->n.sym->backend_decl;
286 for (ref = expr->ref; ref; ref = ref->next)
287 if (ref->type == REF_COMPONENT)
289 gfc_component *comp = ref->u.c.component;
290 if (comp->attr.pointer || comp->attr.allocatable)
291 caf_decl = NULL_TREE;
292 if (comp->attr.codimension)
293 caf_decl = comp->backend_decl;
296 gcc_assert (caf_decl != NULL_TREE);
297 return caf_decl;
301 /* For each character array constructor subexpression without a ts.u.cl->length,
302 replace it by its first element (if there aren't any elements, the length
303 should already be set to zero). */
305 static void
306 flatten_array_ctors_without_strlen (gfc_expr* e)
308 gfc_actual_arglist* arg;
309 gfc_constructor* c;
311 if (!e)
312 return;
314 switch (e->expr_type)
317 case EXPR_OP:
318 flatten_array_ctors_without_strlen (e->value.op.op1);
319 flatten_array_ctors_without_strlen (e->value.op.op2);
320 break;
322 case EXPR_COMPCALL:
323 /* TODO: Implement as with EXPR_FUNCTION when needed. */
324 gcc_unreachable ();
326 case EXPR_FUNCTION:
327 for (arg = e->value.function.actual; arg; arg = arg->next)
328 flatten_array_ctors_without_strlen (arg->expr);
329 break;
331 case EXPR_ARRAY:
333 /* We've found what we're looking for. */
334 if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
336 gfc_constructor *c;
337 gfc_expr* new_expr;
339 gcc_assert (e->value.constructor);
341 c = gfc_constructor_first (e->value.constructor);
342 new_expr = c->expr;
343 c->expr = NULL;
345 flatten_array_ctors_without_strlen (new_expr);
346 gfc_replace_expr (e, new_expr);
347 break;
350 /* Otherwise, fall through to handle constructor elements. */
351 case EXPR_STRUCTURE:
352 for (c = gfc_constructor_first (e->value.constructor);
353 c; c = gfc_constructor_next (c))
354 flatten_array_ctors_without_strlen (c->expr);
355 break;
357 default:
358 break;
364 /* Generate code to initialize a string length variable. Returns the
365 value. For array constructors, cl->length might be NULL and in this case,
366 the first element of the constructor is needed. expr is the original
367 expression so we can access it but can be NULL if this is not needed. */
369 void
370 gfc_conv_string_length (gfc_charlen * cl, gfc_expr * expr, stmtblock_t * pblock)
372 gfc_se se;
374 gfc_init_se (&se, NULL);
376 if (!cl->length
377 && cl->backend_decl
378 && TREE_CODE (cl->backend_decl) == VAR_DECL)
379 return;
381 /* If cl->length is NULL, use gfc_conv_expr to obtain the string length but
382 "flatten" array constructors by taking their first element; all elements
383 should be the same length or a cl->length should be present. */
384 if (!cl->length)
386 gfc_expr* expr_flat;
387 gcc_assert (expr);
388 expr_flat = gfc_copy_expr (expr);
389 flatten_array_ctors_without_strlen (expr_flat);
390 gfc_resolve_expr (expr_flat);
392 gfc_conv_expr (&se, expr_flat);
393 gfc_add_block_to_block (pblock, &se.pre);
394 cl->backend_decl = convert (gfc_charlen_type_node, se.string_length);
396 gfc_free_expr (expr_flat);
397 return;
400 /* Convert cl->length. */
402 gcc_assert (cl->length);
404 gfc_conv_expr_type (&se, cl->length, gfc_charlen_type_node);
405 se.expr = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
406 se.expr, build_int_cst (gfc_charlen_type_node, 0));
407 gfc_add_block_to_block (pblock, &se.pre);
409 if (cl->backend_decl)
410 gfc_add_modify (pblock, cl->backend_decl, se.expr);
411 else
412 cl->backend_decl = gfc_evaluate_now (se.expr, pblock);
416 static void
417 gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
418 const char *name, locus *where)
420 tree tmp;
421 tree type;
422 tree fault;
423 gfc_se start;
424 gfc_se end;
425 char *msg;
427 type = gfc_get_character_type (kind, ref->u.ss.length);
428 type = build_pointer_type (type);
430 gfc_init_se (&start, se);
431 gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
432 gfc_add_block_to_block (&se->pre, &start.pre);
434 if (integer_onep (start.expr))
435 gfc_conv_string_parameter (se);
436 else
438 tmp = start.expr;
439 STRIP_NOPS (tmp);
440 /* Avoid multiple evaluation of substring start. */
441 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
442 start.expr = gfc_evaluate_now (start.expr, &se->pre);
444 /* Change the start of the string. */
445 if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
446 tmp = se->expr;
447 else
448 tmp = build_fold_indirect_ref_loc (input_location,
449 se->expr);
450 tmp = gfc_build_array_ref (tmp, start.expr, NULL);
451 se->expr = gfc_build_addr_expr (type, tmp);
454 /* Length = end + 1 - start. */
455 gfc_init_se (&end, se);
456 if (ref->u.ss.end == NULL)
457 end.expr = se->string_length;
458 else
460 gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
461 gfc_add_block_to_block (&se->pre, &end.pre);
463 tmp = end.expr;
464 STRIP_NOPS (tmp);
465 if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
466 end.expr = gfc_evaluate_now (end.expr, &se->pre);
468 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
470 tree nonempty = fold_build2_loc (input_location, LE_EXPR,
471 boolean_type_node, start.expr,
472 end.expr);
474 /* Check lower bound. */
475 fault = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
476 start.expr,
477 build_int_cst (gfc_charlen_type_node, 1));
478 fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
479 boolean_type_node, nonempty, fault);
480 if (name)
481 asprintf (&msg, "Substring out of bounds: lower bound (%%ld) of '%s' "
482 "is less than one", name);
483 else
484 asprintf (&msg, "Substring out of bounds: lower bound (%%ld)"
485 "is less than one");
486 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
487 fold_convert (long_integer_type_node,
488 start.expr));
489 free (msg);
491 /* Check upper bound. */
492 fault = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
493 end.expr, se->string_length);
494 fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
495 boolean_type_node, nonempty, fault);
496 if (name)
497 asprintf (&msg, "Substring out of bounds: upper bound (%%ld) of '%s' "
498 "exceeds string length (%%ld)", name);
499 else
500 asprintf (&msg, "Substring out of bounds: upper bound (%%ld) "
501 "exceeds string length (%%ld)");
502 gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
503 fold_convert (long_integer_type_node, end.expr),
504 fold_convert (long_integer_type_node,
505 se->string_length));
506 free (msg);
509 /* If the start and end expressions are equal, the length is one. */
510 if (ref->u.ss.end
511 && gfc_dep_compare_expr (ref->u.ss.start, ref->u.ss.end) == 0)
512 tmp = build_int_cst (gfc_charlen_type_node, 1);
513 else
515 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_charlen_type_node,
516 end.expr, start.expr);
517 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_charlen_type_node,
518 build_int_cst (gfc_charlen_type_node, 1), tmp);
519 tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
520 tmp, build_int_cst (gfc_charlen_type_node, 0));
523 se->string_length = tmp;
527 /* Convert a derived type component reference. */
529 static void
530 gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
532 gfc_component *c;
533 tree tmp;
534 tree decl;
535 tree field;
537 c = ref->u.c.component;
539 gcc_assert (c->backend_decl);
541 field = c->backend_decl;
542 gcc_assert (TREE_CODE (field) == FIELD_DECL);
543 decl = se->expr;
545 /* Components can correspond to fields of different containing
546 types, as components are created without context, whereas
547 a concrete use of a component has the type of decl as context.
548 So, if the type doesn't match, we search the corresponding
549 FIELD_DECL in the parent type. To not waste too much time
550 we cache this result in norestrict_decl. */
552 if (DECL_FIELD_CONTEXT (field) != TREE_TYPE (decl))
554 tree f2 = c->norestrict_decl;
555 if (!f2 || DECL_FIELD_CONTEXT (f2) != TREE_TYPE (decl))
556 for (f2 = TYPE_FIELDS (TREE_TYPE (decl)); f2; f2 = DECL_CHAIN (f2))
557 if (TREE_CODE (f2) == FIELD_DECL
558 && DECL_NAME (f2) == DECL_NAME (field))
559 break;
560 gcc_assert (f2);
561 c->norestrict_decl = f2;
562 field = f2;
564 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
565 decl, field, NULL_TREE);
567 se->expr = tmp;
569 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer)
571 tmp = c->ts.u.cl->backend_decl;
572 /* Components must always be constant length. */
573 gcc_assert (tmp && INTEGER_CST_P (tmp));
574 se->string_length = tmp;
577 if (((c->attr.pointer || c->attr.allocatable)
578 && (!c->attr.dimension && !c->attr.codimension)
579 && c->ts.type != BT_CHARACTER)
580 || c->attr.proc_pointer)
581 se->expr = build_fold_indirect_ref_loc (input_location,
582 se->expr);
586 /* This function deals with component references to components of the
587 parent type for derived type extensons. */
588 static void
589 conv_parent_component_references (gfc_se * se, gfc_ref * ref)
591 gfc_component *c;
592 gfc_component *cmp;
593 gfc_symbol *dt;
594 gfc_ref parent;
596 dt = ref->u.c.sym;
597 c = ref->u.c.component;
599 /* Return if the component is not in the parent type. */
600 for (cmp = dt->components; cmp; cmp = cmp->next)
601 if (strcmp (c->name, cmp->name) == 0)
602 return;
604 /* Build a gfc_ref to recursively call gfc_conv_component_ref. */
605 parent.type = REF_COMPONENT;
606 parent.next = NULL;
607 parent.u.c.sym = dt;
608 parent.u.c.component = dt->components;
610 if (dt->backend_decl == NULL)
611 gfc_get_derived_type (dt);
613 /* Build the reference and call self. */
614 gfc_conv_component_ref (se, &parent);
615 parent.u.c.sym = dt->components->ts.u.derived;
616 parent.u.c.component = c;
617 conv_parent_component_references (se, &parent);
620 /* Return the contents of a variable. Also handles reference/pointer
621 variables (all Fortran pointer references are implicit). */
623 static void
624 gfc_conv_variable (gfc_se * se, gfc_expr * expr)
626 gfc_ss *ss;
627 gfc_ref *ref;
628 gfc_symbol *sym;
629 tree parent_decl = NULL_TREE;
630 int parent_flag;
631 bool return_value;
632 bool alternate_entry;
633 bool entry_master;
635 sym = expr->symtree->n.sym;
636 ss = se->ss;
637 if (ss != NULL)
639 gfc_ss_info *ss_info = ss->info;
641 /* Check that something hasn't gone horribly wrong. */
642 gcc_assert (ss != gfc_ss_terminator);
643 gcc_assert (ss_info->expr == expr);
645 /* A scalarized term. We already know the descriptor. */
646 se->expr = ss_info->data.array.descriptor;
647 se->string_length = ss_info->string_length;
648 for (ref = ss_info->data.array.ref; ref; ref = ref->next)
649 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
650 break;
652 else
654 tree se_expr = NULL_TREE;
656 se->expr = gfc_get_symbol_decl (sym);
658 /* Deal with references to a parent results or entries by storing
659 the current_function_decl and moving to the parent_decl. */
660 return_value = sym->attr.function && sym->result == sym;
661 alternate_entry = sym->attr.function && sym->attr.entry
662 && sym->result == sym;
663 entry_master = sym->attr.result
664 && sym->ns->proc_name->attr.entry_master
665 && !gfc_return_by_reference (sym->ns->proc_name);
666 if (current_function_decl)
667 parent_decl = DECL_CONTEXT (current_function_decl);
669 if ((se->expr == parent_decl && return_value)
670 || (sym->ns && sym->ns->proc_name
671 && parent_decl
672 && sym->ns->proc_name->backend_decl == parent_decl
673 && (alternate_entry || entry_master)))
674 parent_flag = 1;
675 else
676 parent_flag = 0;
678 /* Special case for assigning the return value of a function.
679 Self recursive functions must have an explicit return value. */
680 if (return_value && (se->expr == current_function_decl || parent_flag))
681 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
683 /* Similarly for alternate entry points. */
684 else if (alternate_entry
685 && (sym->ns->proc_name->backend_decl == current_function_decl
686 || parent_flag))
688 gfc_entry_list *el = NULL;
690 for (el = sym->ns->entries; el; el = el->next)
691 if (sym == el->sym)
693 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
694 break;
698 else if (entry_master
699 && (sym->ns->proc_name->backend_decl == current_function_decl
700 || parent_flag))
701 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
703 if (se_expr)
704 se->expr = se_expr;
706 /* Procedure actual arguments. */
707 else if (sym->attr.flavor == FL_PROCEDURE
708 && se->expr != current_function_decl)
710 if (!sym->attr.dummy && !sym->attr.proc_pointer)
712 gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
713 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
715 return;
719 /* Dereference the expression, where needed. Since characters
720 are entirely different from other types, they are treated
721 separately. */
722 if (sym->ts.type == BT_CHARACTER)
724 /* Dereference character pointer dummy arguments
725 or results. */
726 if ((sym->attr.pointer || sym->attr.allocatable)
727 && (sym->attr.dummy
728 || sym->attr.function
729 || sym->attr.result))
730 se->expr = build_fold_indirect_ref_loc (input_location,
731 se->expr);
734 else if (!sym->attr.value)
736 /* Dereference non-character scalar dummy arguments. */
737 if (sym->attr.dummy && !sym->attr.dimension
738 && !(sym->attr.codimension && sym->attr.allocatable))
739 se->expr = build_fold_indirect_ref_loc (input_location,
740 se->expr);
742 /* Dereference scalar hidden result. */
743 if (gfc_option.flag_f2c && sym->ts.type == BT_COMPLEX
744 && (sym->attr.function || sym->attr.result)
745 && !sym->attr.dimension && !sym->attr.pointer
746 && !sym->attr.always_explicit)
747 se->expr = build_fold_indirect_ref_loc (input_location,
748 se->expr);
750 /* Dereference non-character pointer variables.
751 These must be dummies, results, or scalars. */
752 if ((sym->attr.pointer || sym->attr.allocatable
753 || gfc_is_associate_pointer (sym))
754 && (sym->attr.dummy
755 || sym->attr.function
756 || sym->attr.result
757 || (!sym->attr.dimension
758 && (!sym->attr.codimension || !sym->attr.allocatable))))
759 se->expr = build_fold_indirect_ref_loc (input_location,
760 se->expr);
763 ref = expr->ref;
766 /* For character variables, also get the length. */
767 if (sym->ts.type == BT_CHARACTER)
769 /* If the character length of an entry isn't set, get the length from
770 the master function instead. */
771 if (sym->attr.entry && !sym->ts.u.cl->backend_decl)
772 se->string_length = sym->ns->proc_name->ts.u.cl->backend_decl;
773 else
774 se->string_length = sym->ts.u.cl->backend_decl;
775 gcc_assert (se->string_length);
778 while (ref)
780 switch (ref->type)
782 case REF_ARRAY:
783 /* Return the descriptor if that's what we want and this is an array
784 section reference. */
785 if (se->descriptor_only && ref->u.ar.type != AR_ELEMENT)
786 return;
787 /* TODO: Pointers to single elements of array sections, eg elemental subs. */
788 /* Return the descriptor for array pointers and allocations. */
789 if (se->want_pointer
790 && ref->next == NULL && (se->descriptor_only))
791 return;
793 gfc_conv_array_ref (se, &ref->u.ar, sym, &expr->where);
794 /* Return a pointer to an element. */
795 break;
797 case REF_COMPONENT:
798 if (ref->u.c.sym->attr.extension)
799 conv_parent_component_references (se, ref);
801 gfc_conv_component_ref (se, ref);
802 break;
804 case REF_SUBSTRING:
805 gfc_conv_substring (se, ref, expr->ts.kind,
806 expr->symtree->name, &expr->where);
807 break;
809 default:
810 gcc_unreachable ();
811 break;
813 ref = ref->next;
815 /* Pointer assignment, allocation or pass by reference. Arrays are handled
816 separately. */
817 if (se->want_pointer)
819 if (expr->ts.type == BT_CHARACTER && !gfc_is_proc_ptr_comp (expr, NULL))
820 gfc_conv_string_parameter (se);
821 else
822 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
827 /* Unary ops are easy... Or they would be if ! was a valid op. */
829 static void
830 gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
832 gfc_se operand;
833 tree type;
835 gcc_assert (expr->ts.type != BT_CHARACTER);
836 /* Initialize the operand. */
837 gfc_init_se (&operand, se);
838 gfc_conv_expr_val (&operand, expr->value.op.op1);
839 gfc_add_block_to_block (&se->pre, &operand.pre);
841 type = gfc_typenode_for_spec (&expr->ts);
843 /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
844 We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
845 All other unary operators have an equivalent GIMPLE unary operator. */
846 if (code == TRUTH_NOT_EXPR)
847 se->expr = fold_build2_loc (input_location, EQ_EXPR, type, operand.expr,
848 build_int_cst (type, 0));
849 else
850 se->expr = fold_build1_loc (input_location, code, type, operand.expr);
854 /* Expand power operator to optimal multiplications when a value is raised
855 to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
856 Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
857 Programming", 3rd Edition, 1998. */
859 /* This code is mostly duplicated from expand_powi in the backend.
860 We establish the "optimal power tree" lookup table with the defined size.
861 The items in the table are the exponents used to calculate the index
862 exponents. Any integer n less than the value can get an "addition chain",
863 with the first node being one. */
864 #define POWI_TABLE_SIZE 256
866 /* The table is from builtins.c. */
867 static const unsigned char powi_table[POWI_TABLE_SIZE] =
869 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
870 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
871 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
872 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
873 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
874 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
875 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
876 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
877 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
878 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
879 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
880 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
881 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
882 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
883 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
884 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
885 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
886 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
887 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
888 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
889 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
890 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
891 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
892 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
893 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
894 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
895 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
896 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
897 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
898 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
899 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
900 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
903 /* If n is larger than lookup table's max index, we use the "window
904 method". */
905 #define POWI_WINDOW_SIZE 3
907 /* Recursive function to expand the power operator. The temporary
908 values are put in tmpvar. The function returns tmpvar[1] ** n. */
909 static tree
910 gfc_conv_powi (gfc_se * se, unsigned HOST_WIDE_INT n, tree * tmpvar)
912 tree op0;
913 tree op1;
914 tree tmp;
915 int digit;
917 if (n < POWI_TABLE_SIZE)
919 if (tmpvar[n])
920 return tmpvar[n];
922 op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
923 op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
925 else if (n & 1)
927 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
928 op0 = gfc_conv_powi (se, n - digit, tmpvar);
929 op1 = gfc_conv_powi (se, digit, tmpvar);
931 else
933 op0 = gfc_conv_powi (se, n >> 1, tmpvar);
934 op1 = op0;
937 tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (op0), op0, op1);
938 tmp = gfc_evaluate_now (tmp, &se->pre);
940 if (n < POWI_TABLE_SIZE)
941 tmpvar[n] = tmp;
943 return tmp;
947 /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
948 return 1. Else return 0 and a call to runtime library functions
949 will have to be built. */
950 static int
951 gfc_conv_cst_int_power (gfc_se * se, tree lhs, tree rhs)
953 tree cond;
954 tree tmp;
955 tree type;
956 tree vartmp[POWI_TABLE_SIZE];
957 HOST_WIDE_INT m;
958 unsigned HOST_WIDE_INT n;
959 int sgn;
961 /* If exponent is too large, we won't expand it anyway, so don't bother
962 with large integer values. */
963 if (!double_int_fits_in_shwi_p (TREE_INT_CST (rhs)))
964 return 0;
966 m = double_int_to_shwi (TREE_INT_CST (rhs));
967 /* There's no ABS for HOST_WIDE_INT, so here we go. It also takes care
968 of the asymmetric range of the integer type. */
969 n = (unsigned HOST_WIDE_INT) (m < 0 ? -m : m);
971 type = TREE_TYPE (lhs);
972 sgn = tree_int_cst_sgn (rhs);
974 if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
975 || optimize_size) && (m > 2 || m < -1))
976 return 0;
978 /* rhs == 0 */
979 if (sgn == 0)
981 se->expr = gfc_build_const (type, integer_one_node);
982 return 1;
985 /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1. */
986 if ((sgn == -1) && (TREE_CODE (type) == INTEGER_TYPE))
988 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
989 lhs, build_int_cst (TREE_TYPE (lhs), -1));
990 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
991 lhs, build_int_cst (TREE_TYPE (lhs), 1));
993 /* If rhs is even,
994 result = (lhs == 1 || lhs == -1) ? 1 : 0. */
995 if ((n & 1) == 0)
997 tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR,
998 boolean_type_node, tmp, cond);
999 se->expr = fold_build3_loc (input_location, COND_EXPR, type,
1000 tmp, build_int_cst (type, 1),
1001 build_int_cst (type, 0));
1002 return 1;
1004 /* If rhs is odd,
1005 result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0. */
1006 tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp,
1007 build_int_cst (type, -1),
1008 build_int_cst (type, 0));
1009 se->expr = fold_build3_loc (input_location, COND_EXPR, type,
1010 cond, build_int_cst (type, 1), tmp);
1011 return 1;
1014 memset (vartmp, 0, sizeof (vartmp));
1015 vartmp[1] = lhs;
1016 if (sgn == -1)
1018 tmp = gfc_build_const (type, integer_one_node);
1019 vartmp[1] = fold_build2_loc (input_location, RDIV_EXPR, type, tmp,
1020 vartmp[1]);
1023 se->expr = gfc_conv_powi (se, n, vartmp);
1025 return 1;
1029 /* Power op (**). Constant integer exponent has special handling. */
1031 static void
1032 gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
1034 tree gfc_int4_type_node;
1035 int kind;
1036 int ikind;
1037 int res_ikind_1, res_ikind_2;
1038 gfc_se lse;
1039 gfc_se rse;
1040 tree fndecl = NULL;
1042 gfc_init_se (&lse, se);
1043 gfc_conv_expr_val (&lse, expr->value.op.op1);
1044 lse.expr = gfc_evaluate_now (lse.expr, &lse.pre);
1045 gfc_add_block_to_block (&se->pre, &lse.pre);
1047 gfc_init_se (&rse, se);
1048 gfc_conv_expr_val (&rse, expr->value.op.op2);
1049 gfc_add_block_to_block (&se->pre, &rse.pre);
1051 if (expr->value.op.op2->ts.type == BT_INTEGER
1052 && expr->value.op.op2->expr_type == EXPR_CONSTANT)
1053 if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
1054 return;
1056 gfc_int4_type_node = gfc_get_int_type (4);
1058 /* In case of integer operands with kinds 1 or 2, we call the integer kind 4
1059 library routine. But in the end, we have to convert the result back
1060 if this case applies -- with res_ikind_K, we keep track whether operand K
1061 falls into this case. */
1062 res_ikind_1 = -1;
1063 res_ikind_2 = -1;
1065 kind = expr->value.op.op1->ts.kind;
1066 switch (expr->value.op.op2->ts.type)
1068 case BT_INTEGER:
1069 ikind = expr->value.op.op2->ts.kind;
1070 switch (ikind)
1072 case 1:
1073 case 2:
1074 rse.expr = convert (gfc_int4_type_node, rse.expr);
1075 res_ikind_2 = ikind;
1076 /* Fall through. */
1078 case 4:
1079 ikind = 0;
1080 break;
1082 case 8:
1083 ikind = 1;
1084 break;
1086 case 16:
1087 ikind = 2;
1088 break;
1090 default:
1091 gcc_unreachable ();
1093 switch (kind)
1095 case 1:
1096 case 2:
1097 if (expr->value.op.op1->ts.type == BT_INTEGER)
1099 lse.expr = convert (gfc_int4_type_node, lse.expr);
1100 res_ikind_1 = kind;
1102 else
1103 gcc_unreachable ();
1104 /* Fall through. */
1106 case 4:
1107 kind = 0;
1108 break;
1110 case 8:
1111 kind = 1;
1112 break;
1114 case 10:
1115 kind = 2;
1116 break;
1118 case 16:
1119 kind = 3;
1120 break;
1122 default:
1123 gcc_unreachable ();
1126 switch (expr->value.op.op1->ts.type)
1128 case BT_INTEGER:
1129 if (kind == 3) /* Case 16 was not handled properly above. */
1130 kind = 2;
1131 fndecl = gfor_fndecl_math_powi[kind][ikind].integer;
1132 break;
1134 case BT_REAL:
1135 /* Use builtins for real ** int4. */
1136 if (ikind == 0)
1138 switch (kind)
1140 case 0:
1141 fndecl = builtin_decl_explicit (BUILT_IN_POWIF);
1142 break;
1144 case 1:
1145 fndecl = builtin_decl_explicit (BUILT_IN_POWI);
1146 break;
1148 case 2:
1149 fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
1150 break;
1152 case 3:
1153 /* Use the __builtin_powil() only if real(kind=16) is
1154 actually the C long double type. */
1155 if (!gfc_real16_is_float128)
1156 fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
1157 break;
1159 default:
1160 gcc_unreachable ();
1164 /* If we don't have a good builtin for this, go for the
1165 library function. */
1166 if (!fndecl)
1167 fndecl = gfor_fndecl_math_powi[kind][ikind].real;
1168 break;
1170 case BT_COMPLEX:
1171 fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
1172 break;
1174 default:
1175 gcc_unreachable ();
1177 break;
1179 case BT_REAL:
1180 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_POW, kind);
1181 break;
1183 case BT_COMPLEX:
1184 fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_CPOW, kind);
1185 break;
1187 default:
1188 gcc_unreachable ();
1189 break;
1192 se->expr = build_call_expr_loc (input_location,
1193 fndecl, 2, lse.expr, rse.expr);
1195 /* Convert the result back if it is of wrong integer kind. */
1196 if (res_ikind_1 != -1 && res_ikind_2 != -1)
1198 /* We want the maximum of both operand kinds as result. */
1199 if (res_ikind_1 < res_ikind_2)
1200 res_ikind_1 = res_ikind_2;
1201 se->expr = convert (gfc_get_int_type (res_ikind_1), se->expr);
1206 /* Generate code to allocate a string temporary. */
1208 tree
1209 gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
1211 tree var;
1212 tree tmp;
1214 if (gfc_can_put_var_on_stack (len))
1216 /* Create a temporary variable to hold the result. */
1217 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1218 gfc_charlen_type_node, len,
1219 build_int_cst (gfc_charlen_type_node, 1));
1220 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node, tmp);
1222 if (TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
1223 tmp = build_array_type (TREE_TYPE (TREE_TYPE (type)), tmp);
1224 else
1225 tmp = build_array_type (TREE_TYPE (type), tmp);
1227 var = gfc_create_var (tmp, "str");
1228 var = gfc_build_addr_expr (type, var);
1230 else
1232 /* Allocate a temporary to hold the result. */
1233 var = gfc_create_var (type, "pstr");
1234 tmp = gfc_call_malloc (&se->pre, type,
1235 fold_build2_loc (input_location, MULT_EXPR,
1236 TREE_TYPE (len), len,
1237 fold_convert (TREE_TYPE (len),
1238 TYPE_SIZE (type))));
1239 gfc_add_modify (&se->pre, var, tmp);
1241 /* Free the temporary afterwards. */
1242 tmp = gfc_call_free (convert (pvoid_type_node, var));
1243 gfc_add_expr_to_block (&se->post, tmp);
1246 return var;
1250 /* Handle a string concatenation operation. A temporary will be allocated to
1251 hold the result. */
1253 static void
1254 gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
1256 gfc_se lse, rse;
1257 tree len, type, var, tmp, fndecl;
1259 gcc_assert (expr->value.op.op1->ts.type == BT_CHARACTER
1260 && expr->value.op.op2->ts.type == BT_CHARACTER);
1261 gcc_assert (expr->value.op.op1->ts.kind == expr->value.op.op2->ts.kind);
1263 gfc_init_se (&lse, se);
1264 gfc_conv_expr (&lse, expr->value.op.op1);
1265 gfc_conv_string_parameter (&lse);
1266 gfc_init_se (&rse, se);
1267 gfc_conv_expr (&rse, expr->value.op.op2);
1268 gfc_conv_string_parameter (&rse);
1270 gfc_add_block_to_block (&se->pre, &lse.pre);
1271 gfc_add_block_to_block (&se->pre, &rse.pre);
1273 type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
1274 len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
1275 if (len == NULL_TREE)
1277 len = fold_build2_loc (input_location, PLUS_EXPR,
1278 TREE_TYPE (lse.string_length),
1279 lse.string_length, rse.string_length);
1282 type = build_pointer_type (type);
1284 var = gfc_conv_string_tmp (se, type, len);
1286 /* Do the actual concatenation. */
1287 if (expr->ts.kind == 1)
1288 fndecl = gfor_fndecl_concat_string;
1289 else if (expr->ts.kind == 4)
1290 fndecl = gfor_fndecl_concat_string_char4;
1291 else
1292 gcc_unreachable ();
1294 tmp = build_call_expr_loc (input_location,
1295 fndecl, 6, len, var, lse.string_length, lse.expr,
1296 rse.string_length, rse.expr);
1297 gfc_add_expr_to_block (&se->pre, tmp);
1299 /* Add the cleanup for the operands. */
1300 gfc_add_block_to_block (&se->pre, &rse.post);
1301 gfc_add_block_to_block (&se->pre, &lse.post);
1303 se->expr = var;
1304 se->string_length = len;
1307 /* Translates an op expression. Common (binary) cases are handled by this
1308 function, others are passed on. Recursion is used in either case.
1309 We use the fact that (op1.ts == op2.ts) (except for the power
1310 operator **).
1311 Operators need no special handling for scalarized expressions as long as
1312 they call gfc_conv_simple_val to get their operands.
1313 Character strings get special handling. */
1315 static void
1316 gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
1318 enum tree_code code;
1319 gfc_se lse;
1320 gfc_se rse;
1321 tree tmp, type;
1322 int lop;
1323 int checkstring;
1325 checkstring = 0;
1326 lop = 0;
1327 switch (expr->value.op.op)
1329 case INTRINSIC_PARENTHESES:
1330 if ((expr->ts.type == BT_REAL
1331 || expr->ts.type == BT_COMPLEX)
1332 && gfc_option.flag_protect_parens)
1334 gfc_conv_unary_op (PAREN_EXPR, se, expr);
1335 gcc_assert (FLOAT_TYPE_P (TREE_TYPE (se->expr)));
1336 return;
1339 /* Fallthrough. */
1340 case INTRINSIC_UPLUS:
1341 gfc_conv_expr (se, expr->value.op.op1);
1342 return;
1344 case INTRINSIC_UMINUS:
1345 gfc_conv_unary_op (NEGATE_EXPR, se, expr);
1346 return;
1348 case INTRINSIC_NOT:
1349 gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
1350 return;
1352 case INTRINSIC_PLUS:
1353 code = PLUS_EXPR;
1354 break;
1356 case INTRINSIC_MINUS:
1357 code = MINUS_EXPR;
1358 break;
1360 case INTRINSIC_TIMES:
1361 code = MULT_EXPR;
1362 break;
1364 case INTRINSIC_DIVIDE:
1365 /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
1366 an integer, we must round towards zero, so we use a
1367 TRUNC_DIV_EXPR. */
1368 if (expr->ts.type == BT_INTEGER)
1369 code = TRUNC_DIV_EXPR;
1370 else
1371 code = RDIV_EXPR;
1372 break;
1374 case INTRINSIC_POWER:
1375 gfc_conv_power_op (se, expr);
1376 return;
1378 case INTRINSIC_CONCAT:
1379 gfc_conv_concat_op (se, expr);
1380 return;
1382 case INTRINSIC_AND:
1383 code = TRUTH_ANDIF_EXPR;
1384 lop = 1;
1385 break;
1387 case INTRINSIC_OR:
1388 code = TRUTH_ORIF_EXPR;
1389 lop = 1;
1390 break;
1392 /* EQV and NEQV only work on logicals, but since we represent them
1393 as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE. */
1394 case INTRINSIC_EQ:
1395 case INTRINSIC_EQ_OS:
1396 case INTRINSIC_EQV:
1397 code = EQ_EXPR;
1398 checkstring = 1;
1399 lop = 1;
1400 break;
1402 case INTRINSIC_NE:
1403 case INTRINSIC_NE_OS:
1404 case INTRINSIC_NEQV:
1405 code = NE_EXPR;
1406 checkstring = 1;
1407 lop = 1;
1408 break;
1410 case INTRINSIC_GT:
1411 case INTRINSIC_GT_OS:
1412 code = GT_EXPR;
1413 checkstring = 1;
1414 lop = 1;
1415 break;
1417 case INTRINSIC_GE:
1418 case INTRINSIC_GE_OS:
1419 code = GE_EXPR;
1420 checkstring = 1;
1421 lop = 1;
1422 break;
1424 case INTRINSIC_LT:
1425 case INTRINSIC_LT_OS:
1426 code = LT_EXPR;
1427 checkstring = 1;
1428 lop = 1;
1429 break;
1431 case INTRINSIC_LE:
1432 case INTRINSIC_LE_OS:
1433 code = LE_EXPR;
1434 checkstring = 1;
1435 lop = 1;
1436 break;
1438 case INTRINSIC_USER:
1439 case INTRINSIC_ASSIGN:
1440 /* These should be converted into function calls by the frontend. */
1441 gcc_unreachable ();
1443 default:
1444 fatal_error ("Unknown intrinsic op");
1445 return;
1448 /* The only exception to this is **, which is handled separately anyway. */
1449 gcc_assert (expr->value.op.op1->ts.type == expr->value.op.op2->ts.type);
1451 if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
1452 checkstring = 0;
1454 /* lhs */
1455 gfc_init_se (&lse, se);
1456 gfc_conv_expr (&lse, expr->value.op.op1);
1457 gfc_add_block_to_block (&se->pre, &lse.pre);
1459 /* rhs */
1460 gfc_init_se (&rse, se);
1461 gfc_conv_expr (&rse, expr->value.op.op2);
1462 gfc_add_block_to_block (&se->pre, &rse.pre);
1464 if (checkstring)
1466 gfc_conv_string_parameter (&lse);
1467 gfc_conv_string_parameter (&rse);
1469 lse.expr = gfc_build_compare_string (lse.string_length, lse.expr,
1470 rse.string_length, rse.expr,
1471 expr->value.op.op1->ts.kind,
1472 code);
1473 rse.expr = build_int_cst (TREE_TYPE (lse.expr), 0);
1474 gfc_add_block_to_block (&lse.post, &rse.post);
1477 type = gfc_typenode_for_spec (&expr->ts);
1479 if (lop)
1481 /* The result of logical ops is always boolean_type_node. */
1482 tmp = fold_build2_loc (input_location, code, boolean_type_node,
1483 lse.expr, rse.expr);
1484 se->expr = convert (type, tmp);
1486 else
1487 se->expr = fold_build2_loc (input_location, code, type, lse.expr, rse.expr);
1489 /* Add the post blocks. */
1490 gfc_add_block_to_block (&se->post, &rse.post);
1491 gfc_add_block_to_block (&se->post, &lse.post);
1494 /* If a string's length is one, we convert it to a single character. */
1496 tree
1497 gfc_string_to_single_character (tree len, tree str, int kind)
1500 if (!INTEGER_CST_P (len) || TREE_INT_CST_HIGH (len) != 0
1501 || !POINTER_TYPE_P (TREE_TYPE (str)))
1502 return NULL_TREE;
1504 if (TREE_INT_CST_LOW (len) == 1)
1506 str = fold_convert (gfc_get_pchar_type (kind), str);
1507 return build_fold_indirect_ref_loc (input_location, str);
1510 if (kind == 1
1511 && TREE_CODE (str) == ADDR_EXPR
1512 && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
1513 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
1514 && array_ref_low_bound (TREE_OPERAND (str, 0))
1515 == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
1516 && TREE_INT_CST_LOW (len) > 1
1517 && TREE_INT_CST_LOW (len)
1518 == (unsigned HOST_WIDE_INT)
1519 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
1521 tree ret = fold_convert (gfc_get_pchar_type (kind), str);
1522 ret = build_fold_indirect_ref_loc (input_location, ret);
1523 if (TREE_CODE (ret) == INTEGER_CST)
1525 tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
1526 int i, length = TREE_STRING_LENGTH (string_cst);
1527 const char *ptr = TREE_STRING_POINTER (string_cst);
1529 for (i = 1; i < length; i++)
1530 if (ptr[i] != ' ')
1531 return NULL_TREE;
1533 return ret;
1537 return NULL_TREE;
1541 void
1542 gfc_conv_scalar_char_value (gfc_symbol *sym, gfc_se *se, gfc_expr **expr)
1545 if (sym->backend_decl)
1547 /* This becomes the nominal_type in
1548 function.c:assign_parm_find_data_types. */
1549 TREE_TYPE (sym->backend_decl) = unsigned_char_type_node;
1550 /* This becomes the passed_type in
1551 function.c:assign_parm_find_data_types. C promotes char to
1552 integer for argument passing. */
1553 DECL_ARG_TYPE (sym->backend_decl) = unsigned_type_node;
1555 DECL_BY_REFERENCE (sym->backend_decl) = 0;
1558 if (expr != NULL)
1560 /* If we have a constant character expression, make it into an
1561 integer. */
1562 if ((*expr)->expr_type == EXPR_CONSTANT)
1564 gfc_typespec ts;
1565 gfc_clear_ts (&ts);
1567 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL,
1568 (int)(*expr)->value.character.string[0]);
1569 if ((*expr)->ts.kind != gfc_c_int_kind)
1571 /* The expr needs to be compatible with a C int. If the
1572 conversion fails, then the 2 causes an ICE. */
1573 ts.type = BT_INTEGER;
1574 ts.kind = gfc_c_int_kind;
1575 gfc_convert_type (*expr, &ts, 2);
1578 else if (se != NULL && (*expr)->expr_type == EXPR_VARIABLE)
1580 if ((*expr)->ref == NULL)
1582 se->expr = gfc_string_to_single_character
1583 (build_int_cst (integer_type_node, 1),
1584 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
1585 gfc_get_symbol_decl
1586 ((*expr)->symtree->n.sym)),
1587 (*expr)->ts.kind);
1589 else
1591 gfc_conv_variable (se, *expr);
1592 se->expr = gfc_string_to_single_character
1593 (build_int_cst (integer_type_node, 1),
1594 gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
1595 se->expr),
1596 (*expr)->ts.kind);
1602 /* Helper function for gfc_build_compare_string. Return LEN_TRIM value
1603 if STR is a string literal, otherwise return -1. */
1605 static int
1606 gfc_optimize_len_trim (tree len, tree str, int kind)
1608 if (kind == 1
1609 && TREE_CODE (str) == ADDR_EXPR
1610 && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
1611 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
1612 && array_ref_low_bound (TREE_OPERAND (str, 0))
1613 == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
1614 && TREE_INT_CST_LOW (len) >= 1
1615 && TREE_INT_CST_LOW (len)
1616 == (unsigned HOST_WIDE_INT)
1617 TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
1619 tree folded = fold_convert (gfc_get_pchar_type (kind), str);
1620 folded = build_fold_indirect_ref_loc (input_location, folded);
1621 if (TREE_CODE (folded) == INTEGER_CST)
1623 tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
1624 int length = TREE_STRING_LENGTH (string_cst);
1625 const char *ptr = TREE_STRING_POINTER (string_cst);
1627 for (; length > 0; length--)
1628 if (ptr[length - 1] != ' ')
1629 break;
1631 return length;
1634 return -1;
1637 /* Compare two strings. If they are all single characters, the result is the
1638 subtraction of them. Otherwise, we build a library call. */
1640 tree
1641 gfc_build_compare_string (tree len1, tree str1, tree len2, tree str2, int kind,
1642 enum tree_code code)
1644 tree sc1;
1645 tree sc2;
1646 tree fndecl;
1648 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1)));
1649 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2)));
1651 sc1 = gfc_string_to_single_character (len1, str1, kind);
1652 sc2 = gfc_string_to_single_character (len2, str2, kind);
1654 if (sc1 != NULL_TREE && sc2 != NULL_TREE)
1656 /* Deal with single character specially. */
1657 sc1 = fold_convert (integer_type_node, sc1);
1658 sc2 = fold_convert (integer_type_node, sc2);
1659 return fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
1660 sc1, sc2);
1663 if ((code == EQ_EXPR || code == NE_EXPR)
1664 && optimize
1665 && INTEGER_CST_P (len1) && INTEGER_CST_P (len2))
1667 /* If one string is a string literal with LEN_TRIM longer
1668 than the length of the second string, the strings
1669 compare unequal. */
1670 int len = gfc_optimize_len_trim (len1, str1, kind);
1671 if (len > 0 && compare_tree_int (len2, len) < 0)
1672 return integer_one_node;
1673 len = gfc_optimize_len_trim (len2, str2, kind);
1674 if (len > 0 && compare_tree_int (len1, len) < 0)
1675 return integer_one_node;
1678 /* Build a call for the comparison. */
1679 if (kind == 1)
1680 fndecl = gfor_fndecl_compare_string;
1681 else if (kind == 4)
1682 fndecl = gfor_fndecl_compare_string_char4;
1683 else
1684 gcc_unreachable ();
1686 return build_call_expr_loc (input_location, fndecl, 4,
1687 len1, str1, len2, str2);
1691 /* Return the backend_decl for a procedure pointer component. */
1693 static tree
1694 get_proc_ptr_comp (gfc_expr *e)
1696 gfc_se comp_se;
1697 gfc_expr *e2;
1698 expr_t old_type;
1700 gfc_init_se (&comp_se, NULL);
1701 e2 = gfc_copy_expr (e);
1702 /* We have to restore the expr type later so that gfc_free_expr frees
1703 the exact same thing that was allocated.
1704 TODO: This is ugly. */
1705 old_type = e2->expr_type;
1706 e2->expr_type = EXPR_VARIABLE;
1707 gfc_conv_expr (&comp_se, e2);
1708 e2->expr_type = old_type;
1709 gfc_free_expr (e2);
1710 return build_fold_addr_expr_loc (input_location, comp_se.expr);
1714 static void
1715 conv_function_val (gfc_se * se, gfc_symbol * sym, gfc_expr * expr)
1717 tree tmp;
1719 if (gfc_is_proc_ptr_comp (expr, NULL))
1720 tmp = get_proc_ptr_comp (expr);
1721 else if (sym->attr.dummy)
1723 tmp = gfc_get_symbol_decl (sym);
1724 if (sym->attr.proc_pointer)
1725 tmp = build_fold_indirect_ref_loc (input_location,
1726 tmp);
1727 gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE
1728 && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) == FUNCTION_TYPE);
1730 else
1732 if (!sym->backend_decl)
1733 sym->backend_decl = gfc_get_extern_function_decl (sym);
1735 tmp = sym->backend_decl;
1737 if (sym->attr.cray_pointee)
1739 /* TODO - make the cray pointee a pointer to a procedure,
1740 assign the pointer to it and use it for the call. This
1741 will do for now! */
1742 tmp = convert (build_pointer_type (TREE_TYPE (tmp)),
1743 gfc_get_symbol_decl (sym->cp_pointer));
1744 tmp = gfc_evaluate_now (tmp, &se->pre);
1747 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
1749 gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
1750 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
1753 se->expr = tmp;
1757 /* Initialize MAPPING. */
1759 void
1760 gfc_init_interface_mapping (gfc_interface_mapping * mapping)
1762 mapping->syms = NULL;
1763 mapping->charlens = NULL;
1767 /* Free all memory held by MAPPING (but not MAPPING itself). */
1769 void
1770 gfc_free_interface_mapping (gfc_interface_mapping * mapping)
1772 gfc_interface_sym_mapping *sym;
1773 gfc_interface_sym_mapping *nextsym;
1774 gfc_charlen *cl;
1775 gfc_charlen *nextcl;
1777 for (sym = mapping->syms; sym; sym = nextsym)
1779 nextsym = sym->next;
1780 sym->new_sym->n.sym->formal = NULL;
1781 gfc_free_symbol (sym->new_sym->n.sym);
1782 gfc_free_expr (sym->expr);
1783 free (sym->new_sym);
1784 free (sym);
1786 for (cl = mapping->charlens; cl; cl = nextcl)
1788 nextcl = cl->next;
1789 gfc_free_expr (cl->length);
1790 free (cl);
1795 /* Return a copy of gfc_charlen CL. Add the returned structure to
1796 MAPPING so that it will be freed by gfc_free_interface_mapping. */
1798 static gfc_charlen *
1799 gfc_get_interface_mapping_charlen (gfc_interface_mapping * mapping,
1800 gfc_charlen * cl)
1802 gfc_charlen *new_charlen;
1804 new_charlen = gfc_get_charlen ();
1805 new_charlen->next = mapping->charlens;
1806 new_charlen->length = gfc_copy_expr (cl->length);
1808 mapping->charlens = new_charlen;
1809 return new_charlen;
1813 /* A subroutine of gfc_add_interface_mapping. Return a descriptorless
1814 array variable that can be used as the actual argument for dummy
1815 argument SYM. Add any initialization code to BLOCK. PACKED is as
1816 for gfc_get_nodesc_array_type and DATA points to the first element
1817 in the passed array. */
1819 static tree
1820 gfc_get_interface_mapping_array (stmtblock_t * block, gfc_symbol * sym,
1821 gfc_packed packed, tree data)
1823 tree type;
1824 tree var;
1826 type = gfc_typenode_for_spec (&sym->ts);
1827 type = gfc_get_nodesc_array_type (type, sym->as, packed,
1828 !sym->attr.target && !sym->attr.pointer
1829 && !sym->attr.proc_pointer);
1831 var = gfc_create_var (type, "ifm");
1832 gfc_add_modify (block, var, fold_convert (type, data));
1834 return var;
1838 /* A subroutine of gfc_add_interface_mapping. Set the stride, upper bounds
1839 and offset of descriptorless array type TYPE given that it has the same
1840 size as DESC. Add any set-up code to BLOCK. */
1842 static void
1843 gfc_set_interface_mapping_bounds (stmtblock_t * block, tree type, tree desc)
1845 int n;
1846 tree dim;
1847 tree offset;
1848 tree tmp;
1850 offset = gfc_index_zero_node;
1851 for (n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
1853 dim = gfc_rank_cst[n];
1854 GFC_TYPE_ARRAY_STRIDE (type, n) = gfc_conv_array_stride (desc, n);
1855 if (GFC_TYPE_ARRAY_LBOUND (type, n) == NULL_TREE)
1857 GFC_TYPE_ARRAY_LBOUND (type, n)
1858 = gfc_conv_descriptor_lbound_get (desc, dim);
1859 GFC_TYPE_ARRAY_UBOUND (type, n)
1860 = gfc_conv_descriptor_ubound_get (desc, dim);
1862 else if (GFC_TYPE_ARRAY_UBOUND (type, n) == NULL_TREE)
1864 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1865 gfc_array_index_type,
1866 gfc_conv_descriptor_ubound_get (desc, dim),
1867 gfc_conv_descriptor_lbound_get (desc, dim));
1868 tmp = fold_build2_loc (input_location, PLUS_EXPR,
1869 gfc_array_index_type,
1870 GFC_TYPE_ARRAY_LBOUND (type, n), tmp);
1871 tmp = gfc_evaluate_now (tmp, block);
1872 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1874 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
1875 GFC_TYPE_ARRAY_LBOUND (type, n),
1876 GFC_TYPE_ARRAY_STRIDE (type, n));
1877 offset = fold_build2_loc (input_location, MINUS_EXPR,
1878 gfc_array_index_type, offset, tmp);
1880 offset = gfc_evaluate_now (offset, block);
1881 GFC_TYPE_ARRAY_OFFSET (type) = offset;
1885 /* Extend MAPPING so that it maps dummy argument SYM to the value stored
1886 in SE. The caller may still use se->expr and se->string_length after
1887 calling this function. */
1889 void
1890 gfc_add_interface_mapping (gfc_interface_mapping * mapping,
1891 gfc_symbol * sym, gfc_se * se,
1892 gfc_expr *expr)
1894 gfc_interface_sym_mapping *sm;
1895 tree desc;
1896 tree tmp;
1897 tree value;
1898 gfc_symbol *new_sym;
1899 gfc_symtree *root;
1900 gfc_symtree *new_symtree;
1902 /* Create a new symbol to represent the actual argument. */
1903 new_sym = gfc_new_symbol (sym->name, NULL);
1904 new_sym->ts = sym->ts;
1905 new_sym->as = gfc_copy_array_spec (sym->as);
1906 new_sym->attr.referenced = 1;
1907 new_sym->attr.dimension = sym->attr.dimension;
1908 new_sym->attr.contiguous = sym->attr.contiguous;
1909 new_sym->attr.codimension = sym->attr.codimension;
1910 new_sym->attr.pointer = sym->attr.pointer;
1911 new_sym->attr.allocatable = sym->attr.allocatable;
1912 new_sym->attr.flavor = sym->attr.flavor;
1913 new_sym->attr.function = sym->attr.function;
1915 /* Ensure that the interface is available and that
1916 descriptors are passed for array actual arguments. */
1917 if (sym->attr.flavor == FL_PROCEDURE)
1919 new_sym->formal = expr->symtree->n.sym->formal;
1920 new_sym->attr.always_explicit
1921 = expr->symtree->n.sym->attr.always_explicit;
1924 /* Create a fake symtree for it. */
1925 root = NULL;
1926 new_symtree = gfc_new_symtree (&root, sym->name);
1927 new_symtree->n.sym = new_sym;
1928 gcc_assert (new_symtree == root);
1930 /* Create a dummy->actual mapping. */
1931 sm = XCNEW (gfc_interface_sym_mapping);
1932 sm->next = mapping->syms;
1933 sm->old = sym;
1934 sm->new_sym = new_symtree;
1935 sm->expr = gfc_copy_expr (expr);
1936 mapping->syms = sm;
1938 /* Stabilize the argument's value. */
1939 if (!sym->attr.function && se)
1940 se->expr = gfc_evaluate_now (se->expr, &se->pre);
1942 if (sym->ts.type == BT_CHARACTER)
1944 /* Create a copy of the dummy argument's length. */
1945 new_sym->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, sym->ts.u.cl);
1946 sm->expr->ts.u.cl = new_sym->ts.u.cl;
1948 /* If the length is specified as "*", record the length that
1949 the caller is passing. We should use the callee's length
1950 in all other cases. */
1951 if (!new_sym->ts.u.cl->length && se)
1953 se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
1954 new_sym->ts.u.cl->backend_decl = se->string_length;
1958 if (!se)
1959 return;
1961 /* Use the passed value as-is if the argument is a function. */
1962 if (sym->attr.flavor == FL_PROCEDURE)
1963 value = se->expr;
1965 /* If the argument is either a string or a pointer to a string,
1966 convert it to a boundless character type. */
1967 else if (!sym->attr.dimension && sym->ts.type == BT_CHARACTER)
1969 tmp = gfc_get_character_type_len (sym->ts.kind, NULL);
1970 tmp = build_pointer_type (tmp);
1971 if (sym->attr.pointer)
1972 value = build_fold_indirect_ref_loc (input_location,
1973 se->expr);
1974 else
1975 value = se->expr;
1976 value = fold_convert (tmp, value);
1979 /* If the argument is a scalar, a pointer to an array or an allocatable,
1980 dereference it. */
1981 else if (!sym->attr.dimension || sym->attr.pointer || sym->attr.allocatable)
1982 value = build_fold_indirect_ref_loc (input_location,
1983 se->expr);
1985 /* For character(*), use the actual argument's descriptor. */
1986 else if (sym->ts.type == BT_CHARACTER && !new_sym->ts.u.cl->length)
1987 value = build_fold_indirect_ref_loc (input_location,
1988 se->expr);
1990 /* If the argument is an array descriptor, use it to determine
1991 information about the actual argument's shape. */
1992 else if (POINTER_TYPE_P (TREE_TYPE (se->expr))
1993 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
1995 /* Get the actual argument's descriptor. */
1996 desc = build_fold_indirect_ref_loc (input_location,
1997 se->expr);
1999 /* Create the replacement variable. */
2000 tmp = gfc_conv_descriptor_data_get (desc);
2001 value = gfc_get_interface_mapping_array (&se->pre, sym,
2002 PACKED_NO, tmp);
2004 /* Use DESC to work out the upper bounds, strides and offset. */
2005 gfc_set_interface_mapping_bounds (&se->pre, TREE_TYPE (value), desc);
2007 else
2008 /* Otherwise we have a packed array. */
2009 value = gfc_get_interface_mapping_array (&se->pre, sym,
2010 PACKED_FULL, se->expr);
2012 new_sym->backend_decl = value;
2016 /* Called once all dummy argument mappings have been added to MAPPING,
2017 but before the mapping is used to evaluate expressions. Pre-evaluate
2018 the length of each argument, adding any initialization code to PRE and
2019 any finalization code to POST. */
2021 void
2022 gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
2023 stmtblock_t * pre, stmtblock_t * post)
2025 gfc_interface_sym_mapping *sym;
2026 gfc_expr *expr;
2027 gfc_se se;
2029 for (sym = mapping->syms; sym; sym = sym->next)
2030 if (sym->new_sym->n.sym->ts.type == BT_CHARACTER
2031 && !sym->new_sym->n.sym->ts.u.cl->backend_decl)
2033 expr = sym->new_sym->n.sym->ts.u.cl->length;
2034 gfc_apply_interface_mapping_to_expr (mapping, expr);
2035 gfc_init_se (&se, NULL);
2036 gfc_conv_expr (&se, expr);
2037 se.expr = fold_convert (gfc_charlen_type_node, se.expr);
2038 se.expr = gfc_evaluate_now (se.expr, &se.pre);
2039 gfc_add_block_to_block (pre, &se.pre);
2040 gfc_add_block_to_block (post, &se.post);
2042 sym->new_sym->n.sym->ts.u.cl->backend_decl = se.expr;
2047 /* Like gfc_apply_interface_mapping_to_expr, but applied to
2048 constructor C. */
2050 static void
2051 gfc_apply_interface_mapping_to_cons (gfc_interface_mapping * mapping,
2052 gfc_constructor_base base)
2054 gfc_constructor *c;
2055 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
2057 gfc_apply_interface_mapping_to_expr (mapping, c->expr);
2058 if (c->iterator)
2060 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->start);
2061 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->end);
2062 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->step);
2068 /* Like gfc_apply_interface_mapping_to_expr, but applied to
2069 reference REF. */
2071 static void
2072 gfc_apply_interface_mapping_to_ref (gfc_interface_mapping * mapping,
2073 gfc_ref * ref)
2075 int n;
2077 for (; ref; ref = ref->next)
2078 switch (ref->type)
2080 case REF_ARRAY:
2081 for (n = 0; n < ref->u.ar.dimen; n++)
2083 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.start[n]);
2084 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.end[n]);
2085 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.stride[n]);
2087 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.offset);
2088 break;
2090 case REF_COMPONENT:
2091 break;
2093 case REF_SUBSTRING:
2094 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.start);
2095 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.end);
2096 break;
2101 /* Convert intrinsic function calls into result expressions. */
2103 static bool
2104 gfc_map_intrinsic_function (gfc_expr *expr, gfc_interface_mapping *mapping)
2106 gfc_symbol *sym;
2107 gfc_expr *new_expr;
2108 gfc_expr *arg1;
2109 gfc_expr *arg2;
2110 int d, dup;
2112 arg1 = expr->value.function.actual->expr;
2113 if (expr->value.function.actual->next)
2114 arg2 = expr->value.function.actual->next->expr;
2115 else
2116 arg2 = NULL;
2118 sym = arg1->symtree->n.sym;
2120 if (sym->attr.dummy)
2121 return false;
2123 new_expr = NULL;
2125 switch (expr->value.function.isym->id)
2127 case GFC_ISYM_LEN:
2128 /* TODO figure out why this condition is necessary. */
2129 if (sym->attr.function
2130 && (arg1->ts.u.cl->length == NULL
2131 || (arg1->ts.u.cl->length->expr_type != EXPR_CONSTANT
2132 && arg1->ts.u.cl->length->expr_type != EXPR_VARIABLE)))
2133 return false;
2135 new_expr = gfc_copy_expr (arg1->ts.u.cl->length);
2136 break;
2138 case GFC_ISYM_SIZE:
2139 if (!sym->as || sym->as->rank == 0)
2140 return false;
2142 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
2144 dup = mpz_get_si (arg2->value.integer);
2145 d = dup - 1;
2147 else
2149 dup = sym->as->rank;
2150 d = 0;
2153 for (; d < dup; d++)
2155 gfc_expr *tmp;
2157 if (!sym->as->upper[d] || !sym->as->lower[d])
2159 gfc_free_expr (new_expr);
2160 return false;
2163 tmp = gfc_add (gfc_copy_expr (sym->as->upper[d]),
2164 gfc_get_int_expr (gfc_default_integer_kind,
2165 NULL, 1));
2166 tmp = gfc_subtract (tmp, gfc_copy_expr (sym->as->lower[d]));
2167 if (new_expr)
2168 new_expr = gfc_multiply (new_expr, tmp);
2169 else
2170 new_expr = tmp;
2172 break;
2174 case GFC_ISYM_LBOUND:
2175 case GFC_ISYM_UBOUND:
2176 /* TODO These implementations of lbound and ubound do not limit if
2177 the size < 0, according to F95's 13.14.53 and 13.14.113. */
2179 if (!sym->as || sym->as->rank == 0)
2180 return false;
2182 if (arg2 && arg2->expr_type == EXPR_CONSTANT)
2183 d = mpz_get_si (arg2->value.integer) - 1;
2184 else
2185 /* TODO: If the need arises, this could produce an array of
2186 ubound/lbounds. */
2187 gcc_unreachable ();
2189 if (expr->value.function.isym->id == GFC_ISYM_LBOUND)
2191 if (sym->as->lower[d])
2192 new_expr = gfc_copy_expr (sym->as->lower[d]);
2194 else
2196 if (sym->as->upper[d])
2197 new_expr = gfc_copy_expr (sym->as->upper[d]);
2199 break;
2201 default:
2202 break;
2205 gfc_apply_interface_mapping_to_expr (mapping, new_expr);
2206 if (!new_expr)
2207 return false;
2209 gfc_replace_expr (expr, new_expr);
2210 return true;
2214 static void
2215 gfc_map_fcn_formal_to_actual (gfc_expr *expr, gfc_expr *map_expr,
2216 gfc_interface_mapping * mapping)
2218 gfc_formal_arglist *f;
2219 gfc_actual_arglist *actual;
2221 actual = expr->value.function.actual;
2222 f = map_expr->symtree->n.sym->formal;
2224 for (; f && actual; f = f->next, actual = actual->next)
2226 if (!actual->expr)
2227 continue;
2229 gfc_add_interface_mapping (mapping, f->sym, NULL, actual->expr);
2232 if (map_expr->symtree->n.sym->attr.dimension)
2234 int d;
2235 gfc_array_spec *as;
2237 as = gfc_copy_array_spec (map_expr->symtree->n.sym->as);
2239 for (d = 0; d < as->rank; d++)
2241 gfc_apply_interface_mapping_to_expr (mapping, as->lower[d]);
2242 gfc_apply_interface_mapping_to_expr (mapping, as->upper[d]);
2245 expr->value.function.esym->as = as;
2248 if (map_expr->symtree->n.sym->ts.type == BT_CHARACTER)
2250 expr->value.function.esym->ts.u.cl->length
2251 = gfc_copy_expr (map_expr->symtree->n.sym->ts.u.cl->length);
2253 gfc_apply_interface_mapping_to_expr (mapping,
2254 expr->value.function.esym->ts.u.cl->length);
2259 /* EXPR is a copy of an expression that appeared in the interface
2260 associated with MAPPING. Walk it recursively looking for references to
2261 dummy arguments that MAPPING maps to actual arguments. Replace each such
2262 reference with a reference to the associated actual argument. */
2264 static void
2265 gfc_apply_interface_mapping_to_expr (gfc_interface_mapping * mapping,
2266 gfc_expr * expr)
2268 gfc_interface_sym_mapping *sym;
2269 gfc_actual_arglist *actual;
2271 if (!expr)
2272 return;
2274 /* Copying an expression does not copy its length, so do that here. */
2275 if (expr->ts.type == BT_CHARACTER && expr->ts.u.cl)
2277 expr->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, expr->ts.u.cl);
2278 gfc_apply_interface_mapping_to_expr (mapping, expr->ts.u.cl->length);
2281 /* Apply the mapping to any references. */
2282 gfc_apply_interface_mapping_to_ref (mapping, expr->ref);
2284 /* ...and to the expression's symbol, if it has one. */
2285 /* TODO Find out why the condition on expr->symtree had to be moved into
2286 the loop rather than being outside it, as originally. */
2287 for (sym = mapping->syms; sym; sym = sym->next)
2288 if (expr->symtree && sym->old == expr->symtree->n.sym)
2290 if (sym->new_sym->n.sym->backend_decl)
2291 expr->symtree = sym->new_sym;
2292 else if (sym->expr)
2293 gfc_replace_expr (expr, gfc_copy_expr (sym->expr));
2294 /* Replace base type for polymorphic arguments. */
2295 if (expr->ref && expr->ref->type == REF_COMPONENT
2296 && sym->expr && sym->expr->ts.type == BT_CLASS)
2297 expr->ref->u.c.sym = sym->expr->ts.u.derived;
2300 /* ...and to subexpressions in expr->value. */
2301 switch (expr->expr_type)
2303 case EXPR_VARIABLE:
2304 case EXPR_CONSTANT:
2305 case EXPR_NULL:
2306 case EXPR_SUBSTRING:
2307 break;
2309 case EXPR_OP:
2310 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op1);
2311 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op2);
2312 break;
2314 case EXPR_FUNCTION:
2315 for (actual = expr->value.function.actual; actual; actual = actual->next)
2316 gfc_apply_interface_mapping_to_expr (mapping, actual->expr);
2318 if (expr->value.function.esym == NULL
2319 && expr->value.function.isym != NULL
2320 && expr->value.function.actual->expr->symtree
2321 && gfc_map_intrinsic_function (expr, mapping))
2322 break;
2324 for (sym = mapping->syms; sym; sym = sym->next)
2325 if (sym->old == expr->value.function.esym)
2327 expr->value.function.esym = sym->new_sym->n.sym;
2328 gfc_map_fcn_formal_to_actual (expr, sym->expr, mapping);
2329 expr->value.function.esym->result = sym->new_sym->n.sym;
2331 break;
2333 case EXPR_ARRAY:
2334 case EXPR_STRUCTURE:
2335 gfc_apply_interface_mapping_to_cons (mapping, expr->value.constructor);
2336 break;
2338 case EXPR_COMPCALL:
2339 case EXPR_PPC:
2340 gcc_unreachable ();
2341 break;
2344 return;
2348 /* Evaluate interface expression EXPR using MAPPING. Store the result
2349 in SE. */
2351 void
2352 gfc_apply_interface_mapping (gfc_interface_mapping * mapping,
2353 gfc_se * se, gfc_expr * expr)
2355 expr = gfc_copy_expr (expr);
2356 gfc_apply_interface_mapping_to_expr (mapping, expr);
2357 gfc_conv_expr (se, expr);
2358 se->expr = gfc_evaluate_now (se->expr, &se->pre);
2359 gfc_free_expr (expr);
2363 /* Returns a reference to a temporary array into which a component of
2364 an actual argument derived type array is copied and then returned
2365 after the function call. */
2366 void
2367 gfc_conv_subref_array_arg (gfc_se * parmse, gfc_expr * expr, int g77,
2368 sym_intent intent, bool formal_ptr)
2370 gfc_se lse;
2371 gfc_se rse;
2372 gfc_ss *lss;
2373 gfc_ss *rss;
2374 gfc_loopinfo loop;
2375 gfc_loopinfo loop2;
2376 gfc_array_info *info;
2377 tree offset;
2378 tree tmp_index;
2379 tree tmp;
2380 tree base_type;
2381 tree size;
2382 stmtblock_t body;
2383 int n;
2384 int dimen;
2386 gcc_assert (expr->expr_type == EXPR_VARIABLE);
2388 gfc_init_se (&lse, NULL);
2389 gfc_init_se (&rse, NULL);
2391 /* Walk the argument expression. */
2392 rss = gfc_walk_expr (expr);
2394 gcc_assert (rss != gfc_ss_terminator);
2396 /* Initialize the scalarizer. */
2397 gfc_init_loopinfo (&loop);
2398 gfc_add_ss_to_loop (&loop, rss);
2400 /* Calculate the bounds of the scalarization. */
2401 gfc_conv_ss_startstride (&loop);
2403 /* Build an ss for the temporary. */
2404 if (expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->backend_decl)
2405 gfc_conv_string_length (expr->ts.u.cl, expr, &parmse->pre);
2407 base_type = gfc_typenode_for_spec (&expr->ts);
2408 if (GFC_ARRAY_TYPE_P (base_type)
2409 || GFC_DESCRIPTOR_TYPE_P (base_type))
2410 base_type = gfc_get_element_type (base_type);
2412 loop.temp_ss = gfc_get_temp_ss (base_type, ((expr->ts.type == BT_CHARACTER)
2413 ? expr->ts.u.cl->backend_decl
2414 : NULL),
2415 loop.dimen);
2417 parmse->string_length = loop.temp_ss->info->string_length;
2419 /* Associate the SS with the loop. */
2420 gfc_add_ss_to_loop (&loop, loop.temp_ss);
2422 /* Setup the scalarizing loops. */
2423 gfc_conv_loop_setup (&loop, &expr->where);
2425 /* Pass the temporary descriptor back to the caller. */
2426 info = &loop.temp_ss->info->data.array;
2427 parmse->expr = info->descriptor;
2429 /* Setup the gfc_se structures. */
2430 gfc_copy_loopinfo_to_se (&lse, &loop);
2431 gfc_copy_loopinfo_to_se (&rse, &loop);
2433 rse.ss = rss;
2434 lse.ss = loop.temp_ss;
2435 gfc_mark_ss_chain_used (rss, 1);
2436 gfc_mark_ss_chain_used (loop.temp_ss, 1);
2438 /* Start the scalarized loop body. */
2439 gfc_start_scalarized_body (&loop, &body);
2441 /* Translate the expression. */
2442 gfc_conv_expr (&rse, expr);
2444 gfc_conv_tmp_array_ref (&lse);
2446 if (intent != INTENT_OUT)
2448 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, true, false, true);
2449 gfc_add_expr_to_block (&body, tmp);
2450 gcc_assert (rse.ss == gfc_ss_terminator);
2451 gfc_trans_scalarizing_loops (&loop, &body);
2453 else
2455 /* Make sure that the temporary declaration survives by merging
2456 all the loop declarations into the current context. */
2457 for (n = 0; n < loop.dimen; n++)
2459 gfc_merge_block_scope (&body);
2460 body = loop.code[loop.order[n]];
2462 gfc_merge_block_scope (&body);
2465 /* Add the post block after the second loop, so that any
2466 freeing of allocated memory is done at the right time. */
2467 gfc_add_block_to_block (&parmse->pre, &loop.pre);
2469 /**********Copy the temporary back again.*********/
2471 gfc_init_se (&lse, NULL);
2472 gfc_init_se (&rse, NULL);
2474 /* Walk the argument expression. */
2475 lss = gfc_walk_expr (expr);
2476 rse.ss = loop.temp_ss;
2477 lse.ss = lss;
2479 /* Initialize the scalarizer. */
2480 gfc_init_loopinfo (&loop2);
2481 gfc_add_ss_to_loop (&loop2, lss);
2483 /* Calculate the bounds of the scalarization. */
2484 gfc_conv_ss_startstride (&loop2);
2486 /* Setup the scalarizing loops. */
2487 gfc_conv_loop_setup (&loop2, &expr->where);
2489 gfc_copy_loopinfo_to_se (&lse, &loop2);
2490 gfc_copy_loopinfo_to_se (&rse, &loop2);
2492 gfc_mark_ss_chain_used (lss, 1);
2493 gfc_mark_ss_chain_used (loop.temp_ss, 1);
2495 /* Declare the variable to hold the temporary offset and start the
2496 scalarized loop body. */
2497 offset = gfc_create_var (gfc_array_index_type, NULL);
2498 gfc_start_scalarized_body (&loop2, &body);
2500 /* Build the offsets for the temporary from the loop variables. The
2501 temporary array has lbounds of zero and strides of one in all
2502 dimensions, so this is very simple. The offset is only computed
2503 outside the innermost loop, so the overall transfer could be
2504 optimized further. */
2505 info = &rse.ss->info->data.array;
2506 dimen = rse.ss->dimen;
2508 tmp_index = gfc_index_zero_node;
2509 for (n = dimen - 1; n > 0; n--)
2511 tree tmp_str;
2512 tmp = rse.loop->loopvar[n];
2513 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
2514 tmp, rse.loop->from[n]);
2515 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
2516 tmp, tmp_index);
2518 tmp_str = fold_build2_loc (input_location, MINUS_EXPR,
2519 gfc_array_index_type,
2520 rse.loop->to[n-1], rse.loop->from[n-1]);
2521 tmp_str = fold_build2_loc (input_location, PLUS_EXPR,
2522 gfc_array_index_type,
2523 tmp_str, gfc_index_one_node);
2525 tmp_index = fold_build2_loc (input_location, MULT_EXPR,
2526 gfc_array_index_type, tmp, tmp_str);
2529 tmp_index = fold_build2_loc (input_location, MINUS_EXPR,
2530 gfc_array_index_type,
2531 tmp_index, rse.loop->from[0]);
2532 gfc_add_modify (&rse.loop->code[0], offset, tmp_index);
2534 tmp_index = fold_build2_loc (input_location, PLUS_EXPR,
2535 gfc_array_index_type,
2536 rse.loop->loopvar[0], offset);
2538 /* Now use the offset for the reference. */
2539 tmp = build_fold_indirect_ref_loc (input_location,
2540 info->data);
2541 rse.expr = gfc_build_array_ref (tmp, tmp_index, NULL);
2543 if (expr->ts.type == BT_CHARACTER)
2544 rse.string_length = expr->ts.u.cl->backend_decl;
2546 gfc_conv_expr (&lse, expr);
2548 gcc_assert (lse.ss == gfc_ss_terminator);
2550 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, false, true);
2551 gfc_add_expr_to_block (&body, tmp);
2553 /* Generate the copying loops. */
2554 gfc_trans_scalarizing_loops (&loop2, &body);
2556 /* Wrap the whole thing up by adding the second loop to the post-block
2557 and following it by the post-block of the first loop. In this way,
2558 if the temporary needs freeing, it is done after use! */
2559 if (intent != INTENT_IN)
2561 gfc_add_block_to_block (&parmse->post, &loop2.pre);
2562 gfc_add_block_to_block (&parmse->post, &loop2.post);
2565 gfc_add_block_to_block (&parmse->post, &loop.post);
2567 gfc_cleanup_loop (&loop);
2568 gfc_cleanup_loop (&loop2);
2570 /* Pass the string length to the argument expression. */
2571 if (expr->ts.type == BT_CHARACTER)
2572 parmse->string_length = expr->ts.u.cl->backend_decl;
2574 /* Determine the offset for pointer formal arguments and set the
2575 lbounds to one. */
2576 if (formal_ptr)
2578 size = gfc_index_one_node;
2579 offset = gfc_index_zero_node;
2580 for (n = 0; n < dimen; n++)
2582 tmp = gfc_conv_descriptor_ubound_get (parmse->expr,
2583 gfc_rank_cst[n]);
2584 tmp = fold_build2_loc (input_location, PLUS_EXPR,
2585 gfc_array_index_type, tmp,
2586 gfc_index_one_node);
2587 gfc_conv_descriptor_ubound_set (&parmse->pre,
2588 parmse->expr,
2589 gfc_rank_cst[n],
2590 tmp);
2591 gfc_conv_descriptor_lbound_set (&parmse->pre,
2592 parmse->expr,
2593 gfc_rank_cst[n],
2594 gfc_index_one_node);
2595 size = gfc_evaluate_now (size, &parmse->pre);
2596 offset = fold_build2_loc (input_location, MINUS_EXPR,
2597 gfc_array_index_type,
2598 offset, size);
2599 offset = gfc_evaluate_now (offset, &parmse->pre);
2600 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2601 gfc_array_index_type,
2602 rse.loop->to[n], rse.loop->from[n]);
2603 tmp = fold_build2_loc (input_location, PLUS_EXPR,
2604 gfc_array_index_type,
2605 tmp, gfc_index_one_node);
2606 size = fold_build2_loc (input_location, MULT_EXPR,
2607 gfc_array_index_type, size, tmp);
2610 gfc_conv_descriptor_offset_set (&parmse->pre, parmse->expr,
2611 offset);
2614 /* We want either the address for the data or the address of the descriptor,
2615 depending on the mode of passing array arguments. */
2616 if (g77)
2617 parmse->expr = gfc_conv_descriptor_data_get (parmse->expr);
2618 else
2619 parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
2621 return;
2625 /* Generate the code for argument list functions. */
2627 static void
2628 conv_arglist_function (gfc_se *se, gfc_expr *expr, const char *name)
2630 /* Pass by value for g77 %VAL(arg), pass the address
2631 indirectly for %LOC, else by reference. Thus %REF
2632 is a "do-nothing" and %LOC is the same as an F95
2633 pointer. */
2634 if (strncmp (name, "%VAL", 4) == 0)
2635 gfc_conv_expr (se, expr);
2636 else if (strncmp (name, "%LOC", 4) == 0)
2638 gfc_conv_expr_reference (se, expr);
2639 se->expr = gfc_build_addr_expr (NULL, se->expr);
2641 else if (strncmp (name, "%REF", 4) == 0)
2642 gfc_conv_expr_reference (se, expr);
2643 else
2644 gfc_error ("Unknown argument list function at %L", &expr->where);
2648 /* Takes a derived type expression and returns the address of a temporary
2649 class object of the 'declared' type. */
2650 static void
2651 gfc_conv_derived_to_class (gfc_se *parmse, gfc_expr *e,
2652 gfc_typespec class_ts)
2654 gfc_component *cmp;
2655 gfc_symbol *vtab;
2656 gfc_symbol *declared = class_ts.u.derived;
2657 gfc_ss *ss;
2658 tree ctree;
2659 tree var;
2660 tree tmp;
2662 /* The derived type needs to be converted to a temporary
2663 CLASS object. */
2664 tmp = gfc_typenode_for_spec (&class_ts);
2665 var = gfc_create_var (tmp, "class");
2667 /* Set the vptr. */
2668 cmp = gfc_find_component (declared, "_vptr", true, true);
2669 ctree = fold_build3_loc (input_location, COMPONENT_REF,
2670 TREE_TYPE (cmp->backend_decl),
2671 var, cmp->backend_decl, NULL_TREE);
2673 /* Remember the vtab corresponds to the derived type
2674 not to the class declared type. */
2675 vtab = gfc_find_derived_vtab (e->ts.u.derived);
2676 gcc_assert (vtab);
2677 tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
2678 gfc_add_modify (&parmse->pre, ctree,
2679 fold_convert (TREE_TYPE (ctree), tmp));
2681 /* Now set the data field. */
2682 cmp = gfc_find_component (declared, "_data", true, true);
2683 ctree = fold_build3_loc (input_location, COMPONENT_REF,
2684 TREE_TYPE (cmp->backend_decl),
2685 var, cmp->backend_decl, NULL_TREE);
2686 ss = gfc_walk_expr (e);
2687 if (ss == gfc_ss_terminator)
2689 parmse->ss = NULL;
2690 gfc_conv_expr_reference (parmse, e);
2691 tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
2692 gfc_add_modify (&parmse->pre, ctree, tmp);
2694 else
2696 parmse->ss = ss;
2697 gfc_conv_expr (parmse, e);
2698 gfc_add_modify (&parmse->pre, ctree, parmse->expr);
2701 /* Pass the address of the class object. */
2702 parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
2706 /* The following routine generates code for the intrinsic
2707 procedures from the ISO_C_BINDING module:
2708 * C_LOC (function)
2709 * C_FUNLOC (function)
2710 * C_F_POINTER (subroutine)
2711 * C_F_PROCPOINTER (subroutine)
2712 * C_ASSOCIATED (function)
2713 One exception which is not handled here is C_F_POINTER with non-scalar
2714 arguments. Returns 1 if the call was replaced by inline code (else: 0). */
2716 static int
2717 conv_isocbinding_procedure (gfc_se * se, gfc_symbol * sym,
2718 gfc_actual_arglist * arg)
2720 gfc_symbol *fsym;
2721 gfc_ss *argss;
2723 if (sym->intmod_sym_id == ISOCBINDING_LOC)
2725 if (arg->expr->rank == 0)
2726 gfc_conv_expr_reference (se, arg->expr);
2727 else
2729 int f;
2730 /* This is really the actual arg because no formal arglist is
2731 created for C_LOC. */
2732 fsym = arg->expr->symtree->n.sym;
2734 /* We should want it to do g77 calling convention. */
2735 f = (fsym != NULL)
2736 && !(fsym->attr.pointer || fsym->attr.allocatable)
2737 && fsym->as->type != AS_ASSUMED_SHAPE;
2738 f = f || !sym->attr.always_explicit;
2740 argss = gfc_walk_expr (arg->expr);
2741 gfc_conv_array_parameter (se, arg->expr, argss, f,
2742 NULL, NULL, NULL);
2745 /* TODO -- the following two lines shouldn't be necessary, but if
2746 they're removed, a bug is exposed later in the code path.
2747 This workaround was thus introduced, but will have to be
2748 removed; please see PR 35150 for details about the issue. */
2749 se->expr = convert (pvoid_type_node, se->expr);
2750 se->expr = gfc_evaluate_now (se->expr, &se->pre);
2752 return 1;
2754 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2756 arg->expr->ts.type = sym->ts.u.derived->ts.type;
2757 arg->expr->ts.f90_type = sym->ts.u.derived->ts.f90_type;
2758 arg->expr->ts.kind = sym->ts.u.derived->ts.kind;
2759 gfc_conv_expr_reference (se, arg->expr);
2761 return 1;
2763 else if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER
2764 && arg->next->expr->rank == 0)
2765 || sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER)
2767 /* Convert c_f_pointer if fptr is a scalar
2768 and convert c_f_procpointer. */
2769 gfc_se cptrse;
2770 gfc_se fptrse;
2772 gfc_init_se (&cptrse, NULL);
2773 gfc_conv_expr (&cptrse, arg->expr);
2774 gfc_add_block_to_block (&se->pre, &cptrse.pre);
2775 gfc_add_block_to_block (&se->post, &cptrse.post);
2777 gfc_init_se (&fptrse, NULL);
2778 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER
2779 || gfc_is_proc_ptr_comp (arg->next->expr, NULL))
2780 fptrse.want_pointer = 1;
2782 gfc_conv_expr (&fptrse, arg->next->expr);
2783 gfc_add_block_to_block (&se->pre, &fptrse.pre);
2784 gfc_add_block_to_block (&se->post, &fptrse.post);
2786 if (arg->next->expr->symtree->n.sym->attr.proc_pointer
2787 && arg->next->expr->symtree->n.sym->attr.dummy)
2788 fptrse.expr = build_fold_indirect_ref_loc (input_location,
2789 fptrse.expr);
2791 se->expr = fold_build2_loc (input_location, MODIFY_EXPR,
2792 TREE_TYPE (fptrse.expr),
2793 fptrse.expr,
2794 fold_convert (TREE_TYPE (fptrse.expr),
2795 cptrse.expr));
2797 return 1;
2799 else if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2801 gfc_se arg1se;
2802 gfc_se arg2se;
2804 /* Build the addr_expr for the first argument. The argument is
2805 already an *address* so we don't need to set want_pointer in
2806 the gfc_se. */
2807 gfc_init_se (&arg1se, NULL);
2808 gfc_conv_expr (&arg1se, arg->expr);
2809 gfc_add_block_to_block (&se->pre, &arg1se.pre);
2810 gfc_add_block_to_block (&se->post, &arg1se.post);
2812 /* See if we were given two arguments. */
2813 if (arg->next == NULL)
2814 /* Only given one arg so generate a null and do a
2815 not-equal comparison against the first arg. */
2816 se->expr = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
2817 arg1se.expr,
2818 fold_convert (TREE_TYPE (arg1se.expr),
2819 null_pointer_node));
2820 else
2822 tree eq_expr;
2823 tree not_null_expr;
2825 /* Given two arguments so build the arg2se from second arg. */
2826 gfc_init_se (&arg2se, NULL);
2827 gfc_conv_expr (&arg2se, arg->next->expr);
2828 gfc_add_block_to_block (&se->pre, &arg2se.pre);
2829 gfc_add_block_to_block (&se->post, &arg2se.post);
2831 /* Generate test to compare that the two args are equal. */
2832 eq_expr = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2833 arg1se.expr, arg2se.expr);
2834 /* Generate test to ensure that the first arg is not null. */
2835 not_null_expr = fold_build2_loc (input_location, NE_EXPR,
2836 boolean_type_node,
2837 arg1se.expr, null_pointer_node);
2839 /* Finally, the generated test must check that both arg1 is not
2840 NULL and that it is equal to the second arg. */
2841 se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
2842 boolean_type_node,
2843 not_null_expr, eq_expr);
2846 return 1;
2849 /* Nothing was done. */
2850 return 0;
2854 /* Generate code for a procedure call. Note can return se->post != NULL.
2855 If se->direct_byref is set then se->expr contains the return parameter.
2856 Return nonzero, if the call has alternate specifiers.
2857 'expr' is only needed for procedure pointer components. */
2860 gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
2861 gfc_actual_arglist * args, gfc_expr * expr,
2862 VEC(tree,gc) *append_args)
2864 gfc_interface_mapping mapping;
2865 VEC(tree,gc) *arglist;
2866 VEC(tree,gc) *retargs;
2867 tree tmp;
2868 tree fntype;
2869 gfc_se parmse;
2870 gfc_ss *argss;
2871 gfc_array_info *info;
2872 int byref;
2873 int parm_kind;
2874 tree type;
2875 tree var;
2876 tree len;
2877 VEC(tree,gc) *stringargs;
2878 tree result = NULL;
2879 gfc_formal_arglist *formal;
2880 gfc_actual_arglist *arg;
2881 int has_alternate_specifier = 0;
2882 bool need_interface_mapping;
2883 bool callee_alloc;
2884 gfc_typespec ts;
2885 gfc_charlen cl;
2886 gfc_expr *e;
2887 gfc_symbol *fsym;
2888 stmtblock_t post;
2889 enum {MISSING = 0, ELEMENTAL, SCALAR, SCALAR_POINTER, ARRAY};
2890 gfc_component *comp = NULL;
2891 int arglen;
2893 arglist = NULL;
2894 retargs = NULL;
2895 stringargs = NULL;
2896 var = NULL_TREE;
2897 len = NULL_TREE;
2898 gfc_clear_ts (&ts);
2900 if (sym->from_intmod == INTMOD_ISO_C_BINDING
2901 && conv_isocbinding_procedure (se, sym, args))
2902 return 0;
2904 gfc_is_proc_ptr_comp (expr, &comp);
2906 if (se->ss != NULL)
2908 if (!sym->attr.elemental)
2910 gcc_assert (se->ss->info->type == GFC_SS_FUNCTION);
2911 if (se->ss->info->useflags)
2913 gcc_assert ((!comp && gfc_return_by_reference (sym)
2914 && sym->result->attr.dimension)
2915 || (comp && comp->attr.dimension));
2916 gcc_assert (se->loop != NULL);
2918 /* Access the previously obtained result. */
2919 gfc_conv_tmp_array_ref (se);
2920 return 0;
2923 info = &se->ss->info->data.array;
2925 else
2926 info = NULL;
2928 gfc_init_block (&post);
2929 gfc_init_interface_mapping (&mapping);
2930 if (!comp)
2932 formal = sym->formal;
2933 need_interface_mapping = sym->attr.dimension ||
2934 (sym->ts.type == BT_CHARACTER
2935 && sym->ts.u.cl->length
2936 && sym->ts.u.cl->length->expr_type
2937 != EXPR_CONSTANT);
2939 else
2941 formal = comp->formal;
2942 need_interface_mapping = comp->attr.dimension ||
2943 (comp->ts.type == BT_CHARACTER
2944 && comp->ts.u.cl->length
2945 && comp->ts.u.cl->length->expr_type
2946 != EXPR_CONSTANT);
2949 /* Evaluate the arguments. */
2950 for (arg = args; arg != NULL;
2951 arg = arg->next, formal = formal ? formal->next : NULL)
2953 e = arg->expr;
2954 fsym = formal ? formal->sym : NULL;
2955 parm_kind = MISSING;
2957 if (e == NULL)
2959 if (se->ignore_optional)
2961 /* Some intrinsics have already been resolved to the correct
2962 parameters. */
2963 continue;
2965 else if (arg->label)
2967 has_alternate_specifier = 1;
2968 continue;
2970 else
2972 /* Pass a NULL pointer for an absent arg. */
2973 gfc_init_se (&parmse, NULL);
2974 parmse.expr = null_pointer_node;
2975 if (arg->missing_arg_type == BT_CHARACTER)
2976 parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
2979 else if (arg->expr->expr_type == EXPR_NULL && fsym && !fsym->attr.pointer)
2981 /* Pass a NULL pointer to denote an absent arg. */
2982 gcc_assert (fsym->attr.optional && !fsym->attr.allocatable);
2983 gfc_init_se (&parmse, NULL);
2984 parmse.expr = null_pointer_node;
2985 if (arg->missing_arg_type == BT_CHARACTER)
2986 parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
2988 else if (fsym && fsym->ts.type == BT_CLASS
2989 && e->ts.type == BT_DERIVED)
2991 /* The derived type needs to be converted to a temporary
2992 CLASS object. */
2993 gfc_init_se (&parmse, se);
2994 gfc_conv_derived_to_class (&parmse, e, fsym->ts);
2996 else if (se->ss && se->ss->info->useflags)
2998 /* An elemental function inside a scalarized loop. */
2999 gfc_init_se (&parmse, se);
3000 parm_kind = ELEMENTAL;
3002 if (se->ss->dimen > 0
3003 && se->ss->info->data.array.ref == NULL)
3005 gfc_conv_tmp_array_ref (&parmse);
3006 if (e->ts.type == BT_CHARACTER)
3007 gfc_conv_string_parameter (&parmse);
3008 else
3009 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
3011 else
3012 gfc_conv_expr_reference (&parmse, e);
3014 else
3016 /* A scalar or transformational function. */
3017 gfc_init_se (&parmse, NULL);
3018 argss = gfc_walk_expr (e);
3020 if (argss == gfc_ss_terminator)
3022 if (e->expr_type == EXPR_VARIABLE
3023 && e->symtree->n.sym->attr.cray_pointee
3024 && fsym && fsym->attr.flavor == FL_PROCEDURE)
3026 /* The Cray pointer needs to be converted to a pointer to
3027 a type given by the expression. */
3028 gfc_conv_expr (&parmse, e);
3029 type = build_pointer_type (TREE_TYPE (parmse.expr));
3030 tmp = gfc_get_symbol_decl (e->symtree->n.sym->cp_pointer);
3031 parmse.expr = convert (type, tmp);
3033 else if (fsym && fsym->attr.value)
3035 if (fsym->ts.type == BT_CHARACTER
3036 && fsym->ts.is_c_interop
3037 && fsym->ns->proc_name != NULL
3038 && fsym->ns->proc_name->attr.is_bind_c)
3040 parmse.expr = NULL;
3041 gfc_conv_scalar_char_value (fsym, &parmse, &e);
3042 if (parmse.expr == NULL)
3043 gfc_conv_expr (&parmse, e);
3045 else
3046 gfc_conv_expr (&parmse, e);
3048 else if (arg->name && arg->name[0] == '%')
3049 /* Argument list functions %VAL, %LOC and %REF are signalled
3050 through arg->name. */
3051 conv_arglist_function (&parmse, arg->expr, arg->name);
3052 else if ((e->expr_type == EXPR_FUNCTION)
3053 && ((e->value.function.esym
3054 && e->value.function.esym->result->attr.pointer)
3055 || (!e->value.function.esym
3056 && e->symtree->n.sym->attr.pointer))
3057 && fsym && fsym->attr.target)
3059 gfc_conv_expr (&parmse, e);
3060 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
3062 else if (e->expr_type == EXPR_FUNCTION
3063 && e->symtree->n.sym->result
3064 && e->symtree->n.sym->result != e->symtree->n.sym
3065 && e->symtree->n.sym->result->attr.proc_pointer)
3067 /* Functions returning procedure pointers. */
3068 gfc_conv_expr (&parmse, e);
3069 if (fsym && fsym->attr.proc_pointer)
3070 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
3072 else
3074 gfc_conv_expr_reference (&parmse, e);
3076 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
3077 allocated on entry, it must be deallocated. */
3078 if (fsym && fsym->attr.allocatable
3079 && fsym->attr.intent == INTENT_OUT)
3081 stmtblock_t block;
3083 gfc_init_block (&block);
3084 tmp = gfc_deallocate_with_status (parmse.expr, NULL_TREE,
3085 true, NULL);
3086 gfc_add_expr_to_block (&block, tmp);
3087 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
3088 void_type_node, parmse.expr,
3089 null_pointer_node);
3090 gfc_add_expr_to_block (&block, tmp);
3092 if (fsym->attr.optional
3093 && e->expr_type == EXPR_VARIABLE
3094 && e->symtree->n.sym->attr.optional)
3096 tmp = fold_build3_loc (input_location, COND_EXPR,
3097 void_type_node,
3098 gfc_conv_expr_present (e->symtree->n.sym),
3099 gfc_finish_block (&block),
3100 build_empty_stmt (input_location));
3102 else
3103 tmp = gfc_finish_block (&block);
3105 gfc_add_expr_to_block (&se->pre, tmp);
3108 if (fsym && e->expr_type != EXPR_NULL
3109 && ((fsym->attr.pointer
3110 && fsym->attr.flavor != FL_PROCEDURE)
3111 || (fsym->attr.proc_pointer
3112 && !(e->expr_type == EXPR_VARIABLE
3113 && e->symtree->n.sym->attr.dummy))
3114 || (fsym->attr.proc_pointer
3115 && e->expr_type == EXPR_VARIABLE
3116 && gfc_is_proc_ptr_comp (e, NULL))
3117 || fsym->attr.allocatable))
3119 /* Scalar pointer dummy args require an extra level of
3120 indirection. The null pointer already contains
3121 this level of indirection. */
3122 parm_kind = SCALAR_POINTER;
3123 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
3127 else
3129 /* If the procedure requires an explicit interface, the actual
3130 argument is passed according to the corresponding formal
3131 argument. If the corresponding formal argument is a POINTER,
3132 ALLOCATABLE or assumed shape, we do not use g77's calling
3133 convention, and pass the address of the array descriptor
3134 instead. Otherwise we use g77's calling convention. */
3135 bool f;
3136 f = (fsym != NULL)
3137 && !(fsym->attr.pointer || fsym->attr.allocatable)
3138 && fsym->as && fsym->as->type != AS_ASSUMED_SHAPE;
3139 if (comp)
3140 f = f || !comp->attr.always_explicit;
3141 else
3142 f = f || !sym->attr.always_explicit;
3144 /* If the argument is a function call that may not create
3145 a temporary for the result, we have to check that we
3146 can do it, i.e. that there is no alias between this
3147 argument and another one. */
3148 if (gfc_get_noncopying_intrinsic_argument (e) != NULL)
3150 gfc_expr *iarg;
3151 sym_intent intent;
3153 if (fsym != NULL)
3154 intent = fsym->attr.intent;
3155 else
3156 intent = INTENT_UNKNOWN;
3158 if (gfc_check_fncall_dependency (e, intent, sym, args,
3159 NOT_ELEMENTAL))
3160 parmse.force_tmp = 1;
3162 iarg = e->value.function.actual->expr;
3164 /* Temporary needed if aliasing due to host association. */
3165 if (sym->attr.contained
3166 && !sym->attr.pure
3167 && !sym->attr.implicit_pure
3168 && !sym->attr.use_assoc
3169 && iarg->expr_type == EXPR_VARIABLE
3170 && sym->ns == iarg->symtree->n.sym->ns)
3171 parmse.force_tmp = 1;
3173 /* Ditto within module. */
3174 if (sym->attr.use_assoc
3175 && !sym->attr.pure
3176 && !sym->attr.implicit_pure
3177 && iarg->expr_type == EXPR_VARIABLE
3178 && sym->module == iarg->symtree->n.sym->module)
3179 parmse.force_tmp = 1;
3182 if (e->expr_type == EXPR_VARIABLE
3183 && is_subref_array (e))
3184 /* The actual argument is a component reference to an
3185 array of derived types. In this case, the argument
3186 is converted to a temporary, which is passed and then
3187 written back after the procedure call. */
3188 gfc_conv_subref_array_arg (&parmse, e, f,
3189 fsym ? fsym->attr.intent : INTENT_INOUT,
3190 fsym && fsym->attr.pointer);
3191 else
3192 gfc_conv_array_parameter (&parmse, e, argss, f, fsym,
3193 sym->name, NULL);
3195 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
3196 allocated on entry, it must be deallocated. */
3197 if (fsym && fsym->attr.allocatable
3198 && fsym->attr.intent == INTENT_OUT)
3200 tmp = build_fold_indirect_ref_loc (input_location,
3201 parmse.expr);
3202 tmp = gfc_trans_dealloc_allocated (tmp);
3203 if (fsym->attr.optional
3204 && e->expr_type == EXPR_VARIABLE
3205 && e->symtree->n.sym->attr.optional)
3206 tmp = fold_build3_loc (input_location, COND_EXPR,
3207 void_type_node,
3208 gfc_conv_expr_present (e->symtree->n.sym),
3209 tmp, build_empty_stmt (input_location));
3210 gfc_add_expr_to_block (&se->pre, tmp);
3215 /* The case with fsym->attr.optional is that of a user subroutine
3216 with an interface indicating an optional argument. When we call
3217 an intrinsic subroutine, however, fsym is NULL, but we might still
3218 have an optional argument, so we proceed to the substitution
3219 just in case. */
3220 if (e && (fsym == NULL || fsym->attr.optional))
3222 /* If an optional argument is itself an optional dummy argument,
3223 check its presence and substitute a null if absent. This is
3224 only needed when passing an array to an elemental procedure
3225 as then array elements are accessed - or no NULL pointer is
3226 allowed and a "1" or "0" should be passed if not present.
3227 When passing a non-array-descriptor full array to a
3228 non-array-descriptor dummy, no check is needed. For
3229 array-descriptor actual to array-descriptor dummy, see
3230 PR 41911 for why a check has to be inserted.
3231 fsym == NULL is checked as intrinsics required the descriptor
3232 but do not always set fsym. */
3233 if (e->expr_type == EXPR_VARIABLE
3234 && e->symtree->n.sym->attr.optional
3235 && ((e->rank > 0 && sym->attr.elemental)
3236 || e->representation.length || e->ts.type == BT_CHARACTER
3237 || (e->rank > 0
3238 && (fsym == NULL
3239 || (fsym-> as
3240 && (fsym->as->type == AS_ASSUMED_SHAPE
3241 || fsym->as->type == AS_DEFERRED))))))
3242 gfc_conv_missing_dummy (&parmse, e, fsym ? fsym->ts : e->ts,
3243 e->representation.length);
3246 if (fsym && e)
3248 /* Obtain the character length of an assumed character length
3249 length procedure from the typespec. */
3250 if (fsym->ts.type == BT_CHARACTER
3251 && parmse.string_length == NULL_TREE
3252 && e->ts.type == BT_PROCEDURE
3253 && e->symtree->n.sym->ts.type == BT_CHARACTER
3254 && e->symtree->n.sym->ts.u.cl->length != NULL
3255 && e->symtree->n.sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
3257 gfc_conv_const_charlen (e->symtree->n.sym->ts.u.cl);
3258 parmse.string_length = e->symtree->n.sym->ts.u.cl->backend_decl;
3262 if (fsym && need_interface_mapping && e)
3263 gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
3265 gfc_add_block_to_block (&se->pre, &parmse.pre);
3266 gfc_add_block_to_block (&post, &parmse.post);
3268 /* Allocated allocatable components of derived types must be
3269 deallocated for non-variable scalars. Non-variable arrays are
3270 dealt with in trans-array.c(gfc_conv_array_parameter). */
3271 if (e && e->ts.type == BT_DERIVED
3272 && e->ts.u.derived->attr.alloc_comp
3273 && !(e->symtree && e->symtree->n.sym->attr.pointer)
3274 && (e->expr_type != EXPR_VARIABLE && !e->rank))
3276 int parm_rank;
3277 tmp = build_fold_indirect_ref_loc (input_location,
3278 parmse.expr);
3279 parm_rank = e->rank;
3280 switch (parm_kind)
3282 case (ELEMENTAL):
3283 case (SCALAR):
3284 parm_rank = 0;
3285 break;
3287 case (SCALAR_POINTER):
3288 tmp = build_fold_indirect_ref_loc (input_location,
3289 tmp);
3290 break;
3293 if (e->expr_type == EXPR_OP
3294 && e->value.op.op == INTRINSIC_PARENTHESES
3295 && e->value.op.op1->expr_type == EXPR_VARIABLE)
3297 tree local_tmp;
3298 local_tmp = gfc_evaluate_now (tmp, &se->pre);
3299 local_tmp = gfc_copy_alloc_comp (e->ts.u.derived, local_tmp, tmp, parm_rank);
3300 gfc_add_expr_to_block (&se->post, local_tmp);
3303 tmp = gfc_deallocate_alloc_comp (e->ts.u.derived, tmp, parm_rank);
3305 gfc_add_expr_to_block (&se->post, tmp);
3308 /* Add argument checking of passing an unallocated/NULL actual to
3309 a nonallocatable/nonpointer dummy. */
3311 if (gfc_option.rtcheck & GFC_RTCHECK_POINTER && e != NULL)
3313 symbol_attribute attr;
3314 char *msg;
3315 tree cond;
3317 if (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
3318 attr = gfc_expr_attr (e);
3319 else
3320 goto end_pointer_check;
3322 /* In Fortran 2008 it's allowed to pass a NULL pointer/nonallocated
3323 allocatable to an optional dummy, cf. 12.5.2.12. */
3324 if (fsym != NULL && fsym->attr.optional && !attr.proc_pointer
3325 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
3326 goto end_pointer_check;
3328 if (attr.optional)
3330 /* If the actual argument is an optional pointer/allocatable and
3331 the formal argument takes an nonpointer optional value,
3332 it is invalid to pass a non-present argument on, even
3333 though there is no technical reason for this in gfortran.
3334 See Fortran 2003, Section 12.4.1.6 item (7)+(8). */
3335 tree present, null_ptr, type;
3337 if (attr.allocatable
3338 && (fsym == NULL || !fsym->attr.allocatable))
3339 asprintf (&msg, "Allocatable actual argument '%s' is not "
3340 "allocated or not present", e->symtree->n.sym->name);
3341 else if (attr.pointer
3342 && (fsym == NULL || !fsym->attr.pointer))
3343 asprintf (&msg, "Pointer actual argument '%s' is not "
3344 "associated or not present",
3345 e->symtree->n.sym->name);
3346 else if (attr.proc_pointer
3347 && (fsym == NULL || !fsym->attr.proc_pointer))
3348 asprintf (&msg, "Proc-pointer actual argument '%s' is not "
3349 "associated or not present",
3350 e->symtree->n.sym->name);
3351 else
3352 goto end_pointer_check;
3354 present = gfc_conv_expr_present (e->symtree->n.sym);
3355 type = TREE_TYPE (present);
3356 present = fold_build2_loc (input_location, EQ_EXPR,
3357 boolean_type_node, present,
3358 fold_convert (type,
3359 null_pointer_node));
3360 type = TREE_TYPE (parmse.expr);
3361 null_ptr = fold_build2_loc (input_location, EQ_EXPR,
3362 boolean_type_node, parmse.expr,
3363 fold_convert (type,
3364 null_pointer_node));
3365 cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
3366 boolean_type_node, present, null_ptr);
3368 else
3370 if (attr.allocatable
3371 && (fsym == NULL || !fsym->attr.allocatable))
3372 asprintf (&msg, "Allocatable actual argument '%s' is not "
3373 "allocated", e->symtree->n.sym->name);
3374 else if (attr.pointer
3375 && (fsym == NULL || !fsym->attr.pointer))
3376 asprintf (&msg, "Pointer actual argument '%s' is not "
3377 "associated", e->symtree->n.sym->name);
3378 else if (attr.proc_pointer
3379 && (fsym == NULL || !fsym->attr.proc_pointer))
3380 asprintf (&msg, "Proc-pointer actual argument '%s' is not "
3381 "associated", e->symtree->n.sym->name);
3382 else
3383 goto end_pointer_check;
3385 tmp = parmse.expr;
3387 /* If the argument is passed by value, we need to strip the
3388 INDIRECT_REF. */
3389 if (!POINTER_TYPE_P (TREE_TYPE (parmse.expr)))
3390 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
3392 cond = fold_build2_loc (input_location, EQ_EXPR,
3393 boolean_type_node, tmp,
3394 fold_convert (TREE_TYPE (tmp),
3395 null_pointer_node));
3398 gfc_trans_runtime_check (true, false, cond, &se->pre, &e->where,
3399 msg);
3400 free (msg);
3402 end_pointer_check:
3404 /* Deferred length dummies pass the character length by reference
3405 so that the value can be returned. */
3406 if (parmse.string_length && fsym && fsym->ts.deferred)
3408 tmp = parmse.string_length;
3409 if (TREE_CODE (tmp) != VAR_DECL)
3410 tmp = gfc_evaluate_now (parmse.string_length, &se->pre);
3411 parmse.string_length = gfc_build_addr_expr (NULL_TREE, tmp);
3414 /* Character strings are passed as two parameters, a length and a
3415 pointer - except for Bind(c) which only passes the pointer. */
3416 if (parmse.string_length != NULL_TREE && !sym->attr.is_bind_c)
3417 VEC_safe_push (tree, gc, stringargs, parmse.string_length);
3419 /* For descriptorless coarrays and assumed-shape coarray dummies, we
3420 pass the token and the offset as additional arguments. */
3421 if (fsym && fsym->attr.codimension
3422 && gfc_option.coarray == GFC_FCOARRAY_LIB
3423 && !fsym->attr.allocatable
3424 && e == NULL)
3426 /* Token and offset. */
3427 VEC_safe_push (tree, gc, stringargs, null_pointer_node);
3428 VEC_safe_push (tree, gc, stringargs,
3429 build_int_cst (gfc_array_index_type, 0));
3430 gcc_assert (fsym->attr.optional);
3432 else if (fsym && fsym->attr.codimension
3433 && !fsym->attr.allocatable
3434 && gfc_option.coarray == GFC_FCOARRAY_LIB)
3436 tree caf_decl, caf_type;
3437 tree offset, tmp2;
3439 caf_decl = get_tree_for_caf_expr (e);
3440 caf_type = TREE_TYPE (caf_decl);
3442 if (GFC_DESCRIPTOR_TYPE_P (caf_type)
3443 && GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_ALLOCATABLE)
3444 tmp = gfc_conv_descriptor_token (caf_decl);
3445 else if (DECL_LANG_SPECIFIC (caf_decl)
3446 && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
3447 tmp = GFC_DECL_TOKEN (caf_decl);
3448 else
3450 gcc_assert (GFC_ARRAY_TYPE_P (caf_type)
3451 && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) != NULL_TREE);
3452 tmp = GFC_TYPE_ARRAY_CAF_TOKEN (caf_type);
3455 VEC_safe_push (tree, gc, stringargs, tmp);
3457 if (GFC_DESCRIPTOR_TYPE_P (caf_type)
3458 && GFC_TYPE_ARRAY_AKIND (caf_type) == GFC_ARRAY_ALLOCATABLE)
3459 offset = build_int_cst (gfc_array_index_type, 0);
3460 else if (DECL_LANG_SPECIFIC (caf_decl)
3461 && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
3462 offset = GFC_DECL_CAF_OFFSET (caf_decl);
3463 else if (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) != NULL_TREE)
3464 offset = GFC_TYPE_ARRAY_CAF_OFFSET (caf_type);
3465 else
3466 offset = build_int_cst (gfc_array_index_type, 0);
3468 if (GFC_DESCRIPTOR_TYPE_P (caf_type))
3469 tmp = gfc_conv_descriptor_data_get (caf_decl);
3470 else
3472 gcc_assert (POINTER_TYPE_P (caf_type));
3473 tmp = caf_decl;
3476 if (fsym->as->type == AS_ASSUMED_SHAPE)
3478 gcc_assert (POINTER_TYPE_P (TREE_TYPE (parmse.expr)));
3479 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE
3480 (TREE_TYPE (parmse.expr))));
3481 tmp2 = build_fold_indirect_ref_loc (input_location, parmse.expr);
3482 tmp2 = gfc_conv_descriptor_data_get (tmp2);
3484 else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (parmse.expr)))
3485 tmp2 = gfc_conv_descriptor_data_get (parmse.expr);
3486 else
3488 gcc_assert (POINTER_TYPE_P (TREE_TYPE (parmse.expr)));
3489 tmp2 = parmse.expr;
3492 tmp = fold_build2_loc (input_location, MINUS_EXPR,
3493 gfc_array_index_type,
3494 fold_convert (gfc_array_index_type, tmp2),
3495 fold_convert (gfc_array_index_type, tmp));
3496 offset = fold_build2_loc (input_location, PLUS_EXPR,
3497 gfc_array_index_type, offset, tmp);
3499 VEC_safe_push (tree, gc, stringargs, offset);
3502 VEC_safe_push (tree, gc, arglist, parmse.expr);
3504 gfc_finish_interface_mapping (&mapping, &se->pre, &se->post);
3506 if (comp)
3507 ts = comp->ts;
3508 else
3509 ts = sym->ts;
3511 if (ts.type == BT_CHARACTER && sym->attr.is_bind_c)
3512 se->string_length = build_int_cst (gfc_charlen_type_node, 1);
3513 else if (ts.type == BT_CHARACTER)
3515 if (ts.u.cl->length == NULL)
3517 /* Assumed character length results are not allowed by 5.1.1.5 of the
3518 standard and are trapped in resolve.c; except in the case of SPREAD
3519 (and other intrinsics?) and dummy functions. In the case of SPREAD,
3520 we take the character length of the first argument for the result.
3521 For dummies, we have to look through the formal argument list for
3522 this function and use the character length found there.*/
3523 if (ts.deferred && (sym->attr.allocatable || sym->attr.pointer))
3524 cl.backend_decl = gfc_create_var (gfc_charlen_type_node, "slen");
3525 else if (!sym->attr.dummy)
3526 cl.backend_decl = VEC_index (tree, stringargs, 0);
3527 else
3529 formal = sym->ns->proc_name->formal;
3530 for (; formal; formal = formal->next)
3531 if (strcmp (formal->sym->name, sym->name) == 0)
3532 cl.backend_decl = formal->sym->ts.u.cl->backend_decl;
3535 else
3537 tree tmp;
3539 /* Calculate the length of the returned string. */
3540 gfc_init_se (&parmse, NULL);
3541 if (need_interface_mapping)
3542 gfc_apply_interface_mapping (&mapping, &parmse, ts.u.cl->length);
3543 else
3544 gfc_conv_expr (&parmse, ts.u.cl->length);
3545 gfc_add_block_to_block (&se->pre, &parmse.pre);
3546 gfc_add_block_to_block (&se->post, &parmse.post);
3548 tmp = fold_convert (gfc_charlen_type_node, parmse.expr);
3549 tmp = fold_build2_loc (input_location, MAX_EXPR,
3550 gfc_charlen_type_node, tmp,
3551 build_int_cst (gfc_charlen_type_node, 0));
3552 cl.backend_decl = tmp;
3555 /* Set up a charlen structure for it. */
3556 cl.next = NULL;
3557 cl.length = NULL;
3558 ts.u.cl = &cl;
3560 len = cl.backend_decl;
3563 byref = (comp && (comp->attr.dimension || comp->ts.type == BT_CHARACTER))
3564 || (!comp && gfc_return_by_reference (sym));
3565 if (byref)
3567 if (se->direct_byref)
3569 /* Sometimes, too much indirection can be applied; e.g. for
3570 function_result = array_valued_recursive_function. */
3571 if (TREE_TYPE (TREE_TYPE (se->expr))
3572 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))
3573 && GFC_DESCRIPTOR_TYPE_P
3574 (TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))))
3575 se->expr = build_fold_indirect_ref_loc (input_location,
3576 se->expr);
3578 /* If the lhs of an assignment x = f(..) is allocatable and
3579 f2003 is allowed, we must do the automatic reallocation.
3580 TODO - deal with intrinsics, without using a temporary. */
3581 if (gfc_option.flag_realloc_lhs
3582 && se->ss && se->ss->loop_chain
3583 && se->ss->loop_chain->is_alloc_lhs
3584 && !expr->value.function.isym
3585 && sym->result->as != NULL)
3587 /* Evaluate the bounds of the result, if known. */
3588 gfc_set_loop_bounds_from_array_spec (&mapping, se,
3589 sym->result->as);
3591 /* Perform the automatic reallocation. */
3592 tmp = gfc_alloc_allocatable_for_assignment (se->loop,
3593 expr, NULL);
3594 gfc_add_expr_to_block (&se->pre, tmp);
3596 /* Pass the temporary as the first argument. */
3597 result = info->descriptor;
3599 else
3600 result = build_fold_indirect_ref_loc (input_location,
3601 se->expr);
3602 VEC_safe_push (tree, gc, retargs, se->expr);
3604 else if (comp && comp->attr.dimension)
3606 gcc_assert (se->loop && info);
3608 /* Set the type of the array. */
3609 tmp = gfc_typenode_for_spec (&comp->ts);
3610 gcc_assert (se->ss->dimen == se->loop->dimen);
3612 /* Evaluate the bounds of the result, if known. */
3613 gfc_set_loop_bounds_from_array_spec (&mapping, se, comp->as);
3615 /* If the lhs of an assignment x = f(..) is allocatable and
3616 f2003 is allowed, we must not generate the function call
3617 here but should just send back the results of the mapping.
3618 This is signalled by the function ss being flagged. */
3619 if (gfc_option.flag_realloc_lhs
3620 && se->ss && se->ss->is_alloc_lhs)
3622 gfc_free_interface_mapping (&mapping);
3623 return has_alternate_specifier;
3626 /* Create a temporary to store the result. In case the function
3627 returns a pointer, the temporary will be a shallow copy and
3628 mustn't be deallocated. */
3629 callee_alloc = comp->attr.allocatable || comp->attr.pointer;
3630 gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
3631 tmp, NULL_TREE, false,
3632 !comp->attr.pointer, callee_alloc,
3633 &se->ss->info->expr->where);
3635 /* Pass the temporary as the first argument. */
3636 result = info->descriptor;
3637 tmp = gfc_build_addr_expr (NULL_TREE, result);
3638 VEC_safe_push (tree, gc, retargs, tmp);
3640 else if (!comp && sym->result->attr.dimension)
3642 gcc_assert (se->loop && info);
3644 /* Set the type of the array. */
3645 tmp = gfc_typenode_for_spec (&ts);
3646 gcc_assert (se->ss->dimen == se->loop->dimen);
3648 /* Evaluate the bounds of the result, if known. */
3649 gfc_set_loop_bounds_from_array_spec (&mapping, se, sym->result->as);
3651 /* If the lhs of an assignment x = f(..) is allocatable and
3652 f2003 is allowed, we must not generate the function call
3653 here but should just send back the results of the mapping.
3654 This is signalled by the function ss being flagged. */
3655 if (gfc_option.flag_realloc_lhs
3656 && se->ss && se->ss->is_alloc_lhs)
3658 gfc_free_interface_mapping (&mapping);
3659 return has_alternate_specifier;
3662 /* Create a temporary to store the result. In case the function
3663 returns a pointer, the temporary will be a shallow copy and
3664 mustn't be deallocated. */
3665 callee_alloc = sym->attr.allocatable || sym->attr.pointer;
3666 gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
3667 tmp, NULL_TREE, false,
3668 !sym->attr.pointer, callee_alloc,
3669 &se->ss->info->expr->where);
3671 /* Pass the temporary as the first argument. */
3672 result = info->descriptor;
3673 tmp = gfc_build_addr_expr (NULL_TREE, result);
3674 VEC_safe_push (tree, gc, retargs, tmp);
3676 else if (ts.type == BT_CHARACTER)
3678 /* Pass the string length. */
3679 type = gfc_get_character_type (ts.kind, ts.u.cl);
3680 type = build_pointer_type (type);
3682 /* Return an address to a char[0:len-1]* temporary for
3683 character pointers. */
3684 if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
3685 || (comp && (comp->attr.pointer || comp->attr.allocatable)))
3687 var = gfc_create_var (type, "pstr");
3689 if ((!comp && sym->attr.allocatable)
3690 || (comp && comp->attr.allocatable))
3691 gfc_add_modify (&se->pre, var,
3692 fold_convert (TREE_TYPE (var),
3693 null_pointer_node));
3695 /* Provide an address expression for the function arguments. */
3696 var = gfc_build_addr_expr (NULL_TREE, var);
3698 else
3699 var = gfc_conv_string_tmp (se, type, len);
3701 VEC_safe_push (tree, gc, retargs, var);
3703 else
3705 gcc_assert (gfc_option.flag_f2c && ts.type == BT_COMPLEX);
3707 type = gfc_get_complex_type (ts.kind);
3708 var = gfc_build_addr_expr (NULL_TREE, gfc_create_var (type, "cmplx"));
3709 VEC_safe_push (tree, gc, retargs, var);
3712 if (ts.type == BT_CHARACTER && ts.deferred
3713 && (sym->attr.allocatable || sym->attr.pointer))
3715 tmp = len;
3716 if (TREE_CODE (tmp) != VAR_DECL)
3717 tmp = gfc_evaluate_now (len, &se->pre);
3718 len = gfc_build_addr_expr (NULL_TREE, tmp);
3721 /* Add the string length to the argument list. */
3722 if (ts.type == BT_CHARACTER)
3723 VEC_safe_push (tree, gc, retargs, len);
3725 gfc_free_interface_mapping (&mapping);
3727 /* We need to glom RETARGS + ARGLIST + STRINGARGS + APPEND_ARGS. */
3728 arglen = (VEC_length (tree, arglist)
3729 + VEC_length (tree, stringargs) + VEC_length (tree, append_args));
3730 VEC_reserve_exact (tree, gc, retargs, arglen);
3732 /* Add the return arguments. */
3733 VEC_splice (tree, retargs, arglist);
3735 /* Add the hidden string length parameters to the arguments. */
3736 VEC_splice (tree, retargs, stringargs);
3738 /* We may want to append extra arguments here. This is used e.g. for
3739 calls to libgfortran_matmul_??, which need extra information. */
3740 if (!VEC_empty (tree, append_args))
3741 VEC_splice (tree, retargs, append_args);
3742 arglist = retargs;
3744 /* Generate the actual call. */
3745 conv_function_val (se, sym, expr);
3747 /* If there are alternate return labels, function type should be
3748 integer. Can't modify the type in place though, since it can be shared
3749 with other functions. For dummy arguments, the typing is done to
3750 this result, even if it has to be repeated for each call. */
3751 if (has_alternate_specifier
3752 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) != integer_type_node)
3754 if (!sym->attr.dummy)
3756 TREE_TYPE (sym->backend_decl)
3757 = build_function_type (integer_type_node,
3758 TYPE_ARG_TYPES (TREE_TYPE (sym->backend_decl)));
3759 se->expr = gfc_build_addr_expr (NULL_TREE, sym->backend_decl);
3761 else
3762 TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) = integer_type_node;
3765 fntype = TREE_TYPE (TREE_TYPE (se->expr));
3766 se->expr = build_call_vec (TREE_TYPE (fntype), se->expr, arglist);
3768 /* If we have a pointer function, but we don't want a pointer, e.g.
3769 something like
3770 x = f()
3771 where f is pointer valued, we have to dereference the result. */
3772 if (!se->want_pointer && !byref
3773 && ((!comp && (sym->attr.pointer || sym->attr.allocatable))
3774 || (comp && (comp->attr.pointer || comp->attr.allocatable))))
3775 se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
3777 /* f2c calling conventions require a scalar default real function to
3778 return a double precision result. Convert this back to default
3779 real. We only care about the cases that can happen in Fortran 77.
3781 if (gfc_option.flag_f2c && sym->ts.type == BT_REAL
3782 && sym->ts.kind == gfc_default_real_kind
3783 && !sym->attr.always_explicit)
3784 se->expr = fold_convert (gfc_get_real_type (sym->ts.kind), se->expr);
3786 /* A pure function may still have side-effects - it may modify its
3787 parameters. */
3788 TREE_SIDE_EFFECTS (se->expr) = 1;
3789 #if 0
3790 if (!sym->attr.pure)
3791 TREE_SIDE_EFFECTS (se->expr) = 1;
3792 #endif
3794 if (byref)
3796 /* Add the function call to the pre chain. There is no expression. */
3797 gfc_add_expr_to_block (&se->pre, se->expr);
3798 se->expr = NULL_TREE;
3800 if (!se->direct_byref)
3802 if ((sym->attr.dimension && !comp) || (comp && comp->attr.dimension))
3804 if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
3806 /* Check the data pointer hasn't been modified. This would
3807 happen in a function returning a pointer. */
3808 tmp = gfc_conv_descriptor_data_get (info->descriptor);
3809 tmp = fold_build2_loc (input_location, NE_EXPR,
3810 boolean_type_node,
3811 tmp, info->data);
3812 gfc_trans_runtime_check (true, false, tmp, &se->pre, NULL,
3813 gfc_msg_fault);
3815 se->expr = info->descriptor;
3816 /* Bundle in the string length. */
3817 se->string_length = len;
3819 else if (ts.type == BT_CHARACTER)
3821 /* Dereference for character pointer results. */
3822 if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
3823 || (comp && (comp->attr.pointer || comp->attr.allocatable)))
3824 se->expr = build_fold_indirect_ref_loc (input_location, var);
3825 else
3826 se->expr = var;
3828 if (!ts.deferred)
3829 se->string_length = len;
3830 else if (sym->attr.allocatable || sym->attr.pointer)
3831 se->string_length = cl.backend_decl;
3833 else
3835 gcc_assert (ts.type == BT_COMPLEX && gfc_option.flag_f2c);
3836 se->expr = build_fold_indirect_ref_loc (input_location, var);
3841 /* Follow the function call with the argument post block. */
3842 if (byref)
3844 gfc_add_block_to_block (&se->pre, &post);
3846 /* Transformational functions of derived types with allocatable
3847 components must have the result allocatable components copied. */
3848 arg = expr->value.function.actual;
3849 if (result && arg && expr->rank
3850 && expr->value.function.isym
3851 && expr->value.function.isym->transformational
3852 && arg->expr->ts.type == BT_DERIVED
3853 && arg->expr->ts.u.derived->attr.alloc_comp)
3855 tree tmp2;
3856 /* Copy the allocatable components. We have to use a
3857 temporary here to prevent source allocatable components
3858 from being corrupted. */
3859 tmp2 = gfc_evaluate_now (result, &se->pre);
3860 tmp = gfc_copy_alloc_comp (arg->expr->ts.u.derived,
3861 result, tmp2, expr->rank);
3862 gfc_add_expr_to_block (&se->pre, tmp);
3863 tmp = gfc_copy_allocatable_data (result, tmp2, TREE_TYPE(tmp2),
3864 expr->rank);
3865 gfc_add_expr_to_block (&se->pre, tmp);
3867 /* Finally free the temporary's data field. */
3868 tmp = gfc_conv_descriptor_data_get (tmp2);
3869 tmp = gfc_deallocate_with_status (tmp, NULL_TREE, true, NULL);
3870 gfc_add_expr_to_block (&se->pre, tmp);
3873 else
3874 gfc_add_block_to_block (&se->post, &post);
3876 return has_alternate_specifier;
3880 /* Fill a character string with spaces. */
3882 static tree
3883 fill_with_spaces (tree start, tree type, tree size)
3885 stmtblock_t block, loop;
3886 tree i, el, exit_label, cond, tmp;
3888 /* For a simple char type, we can call memset(). */
3889 if (compare_tree_int (TYPE_SIZE_UNIT (type), 1) == 0)
3890 return build_call_expr_loc (input_location,
3891 builtin_decl_explicit (BUILT_IN_MEMSET),
3892 3, start,
3893 build_int_cst (gfc_get_int_type (gfc_c_int_kind),
3894 lang_hooks.to_target_charset (' ')),
3895 size);
3897 /* Otherwise, we use a loop:
3898 for (el = start, i = size; i > 0; el--, i+= TYPE_SIZE_UNIT (type))
3899 *el = (type) ' ';
3902 /* Initialize variables. */
3903 gfc_init_block (&block);
3904 i = gfc_create_var (sizetype, "i");
3905 gfc_add_modify (&block, i, fold_convert (sizetype, size));
3906 el = gfc_create_var (build_pointer_type (type), "el");
3907 gfc_add_modify (&block, el, fold_convert (TREE_TYPE (el), start));
3908 exit_label = gfc_build_label_decl (NULL_TREE);
3909 TREE_USED (exit_label) = 1;
3912 /* Loop body. */
3913 gfc_init_block (&loop);
3915 /* Exit condition. */
3916 cond = fold_build2_loc (input_location, LE_EXPR, boolean_type_node, i,
3917 build_zero_cst (sizetype));
3918 tmp = build1_v (GOTO_EXPR, exit_label);
3919 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
3920 build_empty_stmt (input_location));
3921 gfc_add_expr_to_block (&loop, tmp);
3923 /* Assignment. */
3924 gfc_add_modify (&loop,
3925 fold_build1_loc (input_location, INDIRECT_REF, type, el),
3926 build_int_cst (type, lang_hooks.to_target_charset (' ')));
3928 /* Increment loop variables. */
3929 gfc_add_modify (&loop, i,
3930 fold_build2_loc (input_location, MINUS_EXPR, sizetype, i,
3931 TYPE_SIZE_UNIT (type)));
3932 gfc_add_modify (&loop, el,
3933 fold_build_pointer_plus_loc (input_location,
3934 el, TYPE_SIZE_UNIT (type)));
3936 /* Making the loop... actually loop! */
3937 tmp = gfc_finish_block (&loop);
3938 tmp = build1_v (LOOP_EXPR, tmp);
3939 gfc_add_expr_to_block (&block, tmp);
3941 /* The exit label. */
3942 tmp = build1_v (LABEL_EXPR, exit_label);
3943 gfc_add_expr_to_block (&block, tmp);
3946 return gfc_finish_block (&block);
3950 /* Generate code to copy a string. */
3952 void
3953 gfc_trans_string_copy (stmtblock_t * block, tree dlength, tree dest,
3954 int dkind, tree slength, tree src, int skind)
3956 tree tmp, dlen, slen;
3957 tree dsc;
3958 tree ssc;
3959 tree cond;
3960 tree cond2;
3961 tree tmp2;
3962 tree tmp3;
3963 tree tmp4;
3964 tree chartype;
3965 stmtblock_t tempblock;
3967 gcc_assert (dkind == skind);
3969 if (slength != NULL_TREE)
3971 slen = fold_convert (size_type_node, gfc_evaluate_now (slength, block));
3972 ssc = gfc_string_to_single_character (slen, src, skind);
3974 else
3976 slen = build_int_cst (size_type_node, 1);
3977 ssc = src;
3980 if (dlength != NULL_TREE)
3982 dlen = fold_convert (size_type_node, gfc_evaluate_now (dlength, block));
3983 dsc = gfc_string_to_single_character (dlen, dest, dkind);
3985 else
3987 dlen = build_int_cst (size_type_node, 1);
3988 dsc = dest;
3991 /* Assign directly if the types are compatible. */
3992 if (dsc != NULL_TREE && ssc != NULL_TREE
3993 && TREE_TYPE (dsc) == TREE_TYPE (ssc))
3995 gfc_add_modify (block, dsc, ssc);
3996 return;
3999 /* Do nothing if the destination length is zero. */
4000 cond = fold_build2_loc (input_location, GT_EXPR, boolean_type_node, dlen,
4001 build_int_cst (size_type_node, 0));
4003 /* The following code was previously in _gfortran_copy_string:
4005 // The two strings may overlap so we use memmove.
4006 void
4007 copy_string (GFC_INTEGER_4 destlen, char * dest,
4008 GFC_INTEGER_4 srclen, const char * src)
4010 if (srclen >= destlen)
4012 // This will truncate if too long.
4013 memmove (dest, src, destlen);
4015 else
4017 memmove (dest, src, srclen);
4018 // Pad with spaces.
4019 memset (&dest[srclen], ' ', destlen - srclen);
4023 We're now doing it here for better optimization, but the logic
4024 is the same. */
4026 /* For non-default character kinds, we have to multiply the string
4027 length by the base type size. */
4028 chartype = gfc_get_char_type (dkind);
4029 slen = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
4030 fold_convert (size_type_node, slen),
4031 fold_convert (size_type_node,
4032 TYPE_SIZE_UNIT (chartype)));
4033 dlen = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
4034 fold_convert (size_type_node, dlen),
4035 fold_convert (size_type_node,
4036 TYPE_SIZE_UNIT (chartype)));
4038 if (dlength && POINTER_TYPE_P (TREE_TYPE (dest)))
4039 dest = fold_convert (pvoid_type_node, dest);
4040 else
4041 dest = gfc_build_addr_expr (pvoid_type_node, dest);
4043 if (slength && POINTER_TYPE_P (TREE_TYPE (src)))
4044 src = fold_convert (pvoid_type_node, src);
4045 else
4046 src = gfc_build_addr_expr (pvoid_type_node, src);
4048 /* Truncate string if source is too long. */
4049 cond2 = fold_build2_loc (input_location, GE_EXPR, boolean_type_node, slen,
4050 dlen);
4051 tmp2 = build_call_expr_loc (input_location,
4052 builtin_decl_explicit (BUILT_IN_MEMMOVE),
4053 3, dest, src, dlen);
4055 /* Else copy and pad with spaces. */
4056 tmp3 = build_call_expr_loc (input_location,
4057 builtin_decl_explicit (BUILT_IN_MEMMOVE),
4058 3, dest, src, slen);
4060 tmp4 = fold_build_pointer_plus_loc (input_location, dest, slen);
4061 tmp4 = fill_with_spaces (tmp4, chartype,
4062 fold_build2_loc (input_location, MINUS_EXPR,
4063 TREE_TYPE(dlen), dlen, slen));
4065 gfc_init_block (&tempblock);
4066 gfc_add_expr_to_block (&tempblock, tmp3);
4067 gfc_add_expr_to_block (&tempblock, tmp4);
4068 tmp3 = gfc_finish_block (&tempblock);
4070 /* The whole copy_string function is there. */
4071 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
4072 tmp2, tmp3);
4073 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
4074 build_empty_stmt (input_location));
4075 gfc_add_expr_to_block (block, tmp);
4079 /* Translate a statement function.
4080 The value of a statement function reference is obtained by evaluating the
4081 expression using the values of the actual arguments for the values of the
4082 corresponding dummy arguments. */
4084 static void
4085 gfc_conv_statement_function (gfc_se * se, gfc_expr * expr)
4087 gfc_symbol *sym;
4088 gfc_symbol *fsym;
4089 gfc_formal_arglist *fargs;
4090 gfc_actual_arglist *args;
4091 gfc_se lse;
4092 gfc_se rse;
4093 gfc_saved_var *saved_vars;
4094 tree *temp_vars;
4095 tree type;
4096 tree tmp;
4097 int n;
4099 sym = expr->symtree->n.sym;
4100 args = expr->value.function.actual;
4101 gfc_init_se (&lse, NULL);
4102 gfc_init_se (&rse, NULL);
4104 n = 0;
4105 for (fargs = sym->formal; fargs; fargs = fargs->next)
4106 n++;
4107 saved_vars = XCNEWVEC (gfc_saved_var, n);
4108 temp_vars = XCNEWVEC (tree, n);
4110 for (fargs = sym->formal, n = 0; fargs; fargs = fargs->next, n++)
4112 /* Each dummy shall be specified, explicitly or implicitly, to be
4113 scalar. */
4114 gcc_assert (fargs->sym->attr.dimension == 0);
4115 fsym = fargs->sym;
4117 if (fsym->ts.type == BT_CHARACTER)
4119 /* Copy string arguments. */
4120 tree arglen;
4122 gcc_assert (fsym->ts.u.cl && fsym->ts.u.cl->length
4123 && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT);
4125 /* Create a temporary to hold the value. */
4126 if (fsym->ts.u.cl->backend_decl == NULL_TREE)
4127 fsym->ts.u.cl->backend_decl
4128 = gfc_conv_constant_to_tree (fsym->ts.u.cl->length);
4130 type = gfc_get_character_type (fsym->ts.kind, fsym->ts.u.cl);
4131 temp_vars[n] = gfc_create_var (type, fsym->name);
4133 arglen = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
4135 gfc_conv_expr (&rse, args->expr);
4136 gfc_conv_string_parameter (&rse);
4137 gfc_add_block_to_block (&se->pre, &lse.pre);
4138 gfc_add_block_to_block (&se->pre, &rse.pre);
4140 gfc_trans_string_copy (&se->pre, arglen, temp_vars[n], fsym->ts.kind,
4141 rse.string_length, rse.expr, fsym->ts.kind);
4142 gfc_add_block_to_block (&se->pre, &lse.post);
4143 gfc_add_block_to_block (&se->pre, &rse.post);
4145 else
4147 /* For everything else, just evaluate the expression. */
4149 /* Create a temporary to hold the value. */
4150 type = gfc_typenode_for_spec (&fsym->ts);
4151 temp_vars[n] = gfc_create_var (type, fsym->name);
4153 gfc_conv_expr (&lse, args->expr);
4155 gfc_add_block_to_block (&se->pre, &lse.pre);
4156 gfc_add_modify (&se->pre, temp_vars[n], lse.expr);
4157 gfc_add_block_to_block (&se->pre, &lse.post);
4160 args = args->next;
4163 /* Use the temporary variables in place of the real ones. */
4164 for (fargs = sym->formal, n = 0; fargs; fargs = fargs->next, n++)
4165 gfc_shadow_sym (fargs->sym, temp_vars[n], &saved_vars[n]);
4167 gfc_conv_expr (se, sym->value);
4169 if (sym->ts.type == BT_CHARACTER)
4171 gfc_conv_const_charlen (sym->ts.u.cl);
4173 /* Force the expression to the correct length. */
4174 if (!INTEGER_CST_P (se->string_length)
4175 || tree_int_cst_lt (se->string_length,
4176 sym->ts.u.cl->backend_decl))
4178 type = gfc_get_character_type (sym->ts.kind, sym->ts.u.cl);
4179 tmp = gfc_create_var (type, sym->name);
4180 tmp = gfc_build_addr_expr (build_pointer_type (type), tmp);
4181 gfc_trans_string_copy (&se->pre, sym->ts.u.cl->backend_decl, tmp,
4182 sym->ts.kind, se->string_length, se->expr,
4183 sym->ts.kind);
4184 se->expr = tmp;
4186 se->string_length = sym->ts.u.cl->backend_decl;
4189 /* Restore the original variables. */
4190 for (fargs = sym->formal, n = 0; fargs; fargs = fargs->next, n++)
4191 gfc_restore_sym (fargs->sym, &saved_vars[n]);
4192 free (saved_vars);
4196 /* Translate a function expression. */
4198 static void
4199 gfc_conv_function_expr (gfc_se * se, gfc_expr * expr)
4201 gfc_symbol *sym;
4203 if (expr->value.function.isym)
4205 gfc_conv_intrinsic_function (se, expr);
4206 return;
4209 /* We distinguish statement functions from general functions to improve
4210 runtime performance. */
4211 if (expr->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
4213 gfc_conv_statement_function (se, expr);
4214 return;
4217 /* expr.value.function.esym is the resolved (specific) function symbol for
4218 most functions. However this isn't set for dummy procedures. */
4219 sym = expr->value.function.esym;
4220 if (!sym)
4221 sym = expr->symtree->n.sym;
4223 gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr, NULL);
4227 /* Determine whether the given EXPR_CONSTANT is a zero initializer. */
4229 static bool
4230 is_zero_initializer_p (gfc_expr * expr)
4232 if (expr->expr_type != EXPR_CONSTANT)
4233 return false;
4235 /* We ignore constants with prescribed memory representations for now. */
4236 if (expr->representation.string)
4237 return false;
4239 switch (expr->ts.type)
4241 case BT_INTEGER:
4242 return mpz_cmp_si (expr->value.integer, 0) == 0;
4244 case BT_REAL:
4245 return mpfr_zero_p (expr->value.real)
4246 && MPFR_SIGN (expr->value.real) >= 0;
4248 case BT_LOGICAL:
4249 return expr->value.logical == 0;
4251 case BT_COMPLEX:
4252 return mpfr_zero_p (mpc_realref (expr->value.complex))
4253 && MPFR_SIGN (mpc_realref (expr->value.complex)) >= 0
4254 && mpfr_zero_p (mpc_imagref (expr->value.complex))
4255 && MPFR_SIGN (mpc_imagref (expr->value.complex)) >= 0;
4257 default:
4258 break;
4260 return false;
4264 static void
4265 gfc_conv_array_constructor_expr (gfc_se * se, gfc_expr * expr)
4267 gfc_ss *ss;
4269 ss = se->ss;
4270 gcc_assert (ss != NULL && ss != gfc_ss_terminator);
4271 gcc_assert (ss->info->expr == expr && ss->info->type == GFC_SS_CONSTRUCTOR);
4273 gfc_conv_tmp_array_ref (se);
4277 /* Build a static initializer. EXPR is the expression for the initial value.
4278 The other parameters describe the variable of the component being
4279 initialized. EXPR may be null. */
4281 tree
4282 gfc_conv_initializer (gfc_expr * expr, gfc_typespec * ts, tree type,
4283 bool array, bool pointer, bool procptr)
4285 gfc_se se;
4287 if (!(expr || pointer || procptr))
4288 return NULL_TREE;
4290 /* Check if we have ISOCBINDING_NULL_PTR or ISOCBINDING_NULL_FUNPTR
4291 (these are the only two iso_c_binding derived types that can be
4292 used as initialization expressions). If so, we need to modify
4293 the 'expr' to be that for a (void *). */
4294 if (expr != NULL && expr->ts.type == BT_DERIVED
4295 && expr->ts.is_iso_c && expr->ts.u.derived)
4297 gfc_symbol *derived = expr->ts.u.derived;
4299 /* The derived symbol has already been converted to a (void *). Use
4300 its kind. */
4301 expr = gfc_get_int_expr (derived->ts.kind, NULL, 0);
4302 expr->ts.f90_type = derived->ts.f90_type;
4304 gfc_init_se (&se, NULL);
4305 gfc_conv_constant (&se, expr);
4306 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
4307 return se.expr;
4310 if (array && !procptr)
4312 tree ctor;
4313 /* Arrays need special handling. */
4314 if (pointer)
4315 ctor = gfc_build_null_descriptor (type);
4316 /* Special case assigning an array to zero. */
4317 else if (is_zero_initializer_p (expr))
4318 ctor = build_constructor (type, NULL);
4319 else
4320 ctor = gfc_conv_array_initializer (type, expr);
4321 TREE_STATIC (ctor) = 1;
4322 return ctor;
4324 else if (pointer || procptr)
4326 if (!expr || expr->expr_type == EXPR_NULL)
4327 return fold_convert (type, null_pointer_node);
4328 else
4330 gfc_init_se (&se, NULL);
4331 se.want_pointer = 1;
4332 gfc_conv_expr (&se, expr);
4333 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
4334 return se.expr;
4337 else
4339 switch (ts->type)
4341 case BT_DERIVED:
4342 case BT_CLASS:
4343 gfc_init_se (&se, NULL);
4344 if (ts->type == BT_CLASS && expr->expr_type == EXPR_NULL)
4345 gfc_conv_structure (&se, gfc_class_null_initializer(ts), 1);
4346 else
4347 gfc_conv_structure (&se, expr, 1);
4348 gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
4349 TREE_STATIC (se.expr) = 1;
4350 return se.expr;
4352 case BT_CHARACTER:
4354 tree ctor = gfc_conv_string_init (ts->u.cl->backend_decl,expr);
4355 TREE_STATIC (ctor) = 1;
4356 return ctor;
4359 default:
4360 gfc_init_se (&se, NULL);
4361 gfc_conv_constant (&se, expr);
4362 gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
4363 return se.expr;
4368 static tree
4369 gfc_trans_subarray_assign (tree dest, gfc_component * cm, gfc_expr * expr)
4371 gfc_se rse;
4372 gfc_se lse;
4373 gfc_ss *rss;
4374 gfc_ss *lss;
4375 gfc_array_info *lss_array;
4376 stmtblock_t body;
4377 stmtblock_t block;
4378 gfc_loopinfo loop;
4379 int n;
4380 tree tmp;
4382 gfc_start_block (&block);
4384 /* Initialize the scalarizer. */
4385 gfc_init_loopinfo (&loop);
4387 gfc_init_se (&lse, NULL);
4388 gfc_init_se (&rse, NULL);
4390 /* Walk the rhs. */
4391 rss = gfc_walk_expr (expr);
4392 if (rss == gfc_ss_terminator)
4393 /* The rhs is scalar. Add a ss for the expression. */
4394 rss = gfc_get_scalar_ss (gfc_ss_terminator, expr);
4396 /* Create a SS for the destination. */
4397 lss = gfc_get_array_ss (gfc_ss_terminator, NULL, cm->as->rank,
4398 GFC_SS_COMPONENT);
4399 lss_array = &lss->info->data.array;
4400 lss_array->shape = gfc_get_shape (cm->as->rank);
4401 lss_array->descriptor = dest;
4402 lss_array->data = gfc_conv_array_data (dest);
4403 lss_array->offset = gfc_conv_array_offset (dest);
4404 for (n = 0; n < cm->as->rank; n++)
4406 lss_array->start[n] = gfc_conv_array_lbound (dest, n);
4407 lss_array->stride[n] = gfc_index_one_node;
4409 mpz_init (lss_array->shape[n]);
4410 mpz_sub (lss_array->shape[n], cm->as->upper[n]->value.integer,
4411 cm->as->lower[n]->value.integer);
4412 mpz_add_ui (lss_array->shape[n], lss_array->shape[n], 1);
4415 /* Associate the SS with the loop. */
4416 gfc_add_ss_to_loop (&loop, lss);
4417 gfc_add_ss_to_loop (&loop, rss);
4419 /* Calculate the bounds of the scalarization. */
4420 gfc_conv_ss_startstride (&loop);
4422 /* Setup the scalarizing loops. */
4423 gfc_conv_loop_setup (&loop, &expr->where);
4425 /* Setup the gfc_se structures. */
4426 gfc_copy_loopinfo_to_se (&lse, &loop);
4427 gfc_copy_loopinfo_to_se (&rse, &loop);
4429 rse.ss = rss;
4430 gfc_mark_ss_chain_used (rss, 1);
4431 lse.ss = lss;
4432 gfc_mark_ss_chain_used (lss, 1);
4434 /* Start the scalarized loop body. */
4435 gfc_start_scalarized_body (&loop, &body);
4437 gfc_conv_tmp_array_ref (&lse);
4438 if (cm->ts.type == BT_CHARACTER)
4439 lse.string_length = cm->ts.u.cl->backend_decl;
4441 gfc_conv_expr (&rse, expr);
4443 tmp = gfc_trans_scalar_assign (&lse, &rse, cm->ts, true, false, true);
4444 gfc_add_expr_to_block (&body, tmp);
4446 gcc_assert (rse.ss == gfc_ss_terminator);
4448 /* Generate the copying loops. */
4449 gfc_trans_scalarizing_loops (&loop, &body);
4451 /* Wrap the whole thing up. */
4452 gfc_add_block_to_block (&block, &loop.pre);
4453 gfc_add_block_to_block (&block, &loop.post);
4455 gcc_assert (lss_array->shape != NULL);
4456 gfc_free_shape (&lss_array->shape, cm->as->rank);
4457 gfc_cleanup_loop (&loop);
4459 return gfc_finish_block (&block);
4463 static tree
4464 gfc_trans_alloc_subarray_assign (tree dest, gfc_component * cm,
4465 gfc_expr * expr)
4467 gfc_se se;
4468 gfc_ss *rss;
4469 stmtblock_t block;
4470 tree offset;
4471 int n;
4472 tree tmp;
4473 tree tmp2;
4474 gfc_array_spec *as;
4475 gfc_expr *arg = NULL;
4477 gfc_start_block (&block);
4478 gfc_init_se (&se, NULL);
4480 /* Get the descriptor for the expressions. */
4481 rss = gfc_walk_expr (expr);
4482 se.want_pointer = 0;
4483 gfc_conv_expr_descriptor (&se, expr, rss);
4484 gfc_add_block_to_block (&block, &se.pre);
4485 gfc_add_modify (&block, dest, se.expr);
4487 /* Deal with arrays of derived types with allocatable components. */
4488 if (cm->ts.type == BT_DERIVED
4489 && cm->ts.u.derived->attr.alloc_comp)
4490 tmp = gfc_copy_alloc_comp (cm->ts.u.derived,
4491 se.expr, dest,
4492 cm->as->rank);
4493 else
4494 tmp = gfc_duplicate_allocatable (dest, se.expr,
4495 TREE_TYPE(cm->backend_decl),
4496 cm->as->rank);
4498 gfc_add_expr_to_block (&block, tmp);
4499 gfc_add_block_to_block (&block, &se.post);
4501 if (expr->expr_type != EXPR_VARIABLE)
4502 gfc_conv_descriptor_data_set (&block, se.expr,
4503 null_pointer_node);
4505 /* We need to know if the argument of a conversion function is a
4506 variable, so that the correct lower bound can be used. */
4507 if (expr->expr_type == EXPR_FUNCTION
4508 && expr->value.function.isym
4509 && expr->value.function.isym->conversion
4510 && expr->value.function.actual->expr
4511 && expr->value.function.actual->expr->expr_type == EXPR_VARIABLE)
4512 arg = expr->value.function.actual->expr;
4514 /* Obtain the array spec of full array references. */
4515 if (arg)
4516 as = gfc_get_full_arrayspec_from_expr (arg);
4517 else
4518 as = gfc_get_full_arrayspec_from_expr (expr);
4520 /* Shift the lbound and ubound of temporaries to being unity,
4521 rather than zero, based. Always calculate the offset. */
4522 offset = gfc_conv_descriptor_offset_get (dest);
4523 gfc_add_modify (&block, offset, gfc_index_zero_node);
4524 tmp2 =gfc_create_var (gfc_array_index_type, NULL);
4526 for (n = 0; n < expr->rank; n++)
4528 tree span;
4529 tree lbound;
4531 /* Obtain the correct lbound - ISO/IEC TR 15581:2001 page 9.
4532 TODO It looks as if gfc_conv_expr_descriptor should return
4533 the correct bounds and that the following should not be
4534 necessary. This would simplify gfc_conv_intrinsic_bound
4535 as well. */
4536 if (as && as->lower[n])
4538 gfc_se lbse;
4539 gfc_init_se (&lbse, NULL);
4540 gfc_conv_expr (&lbse, as->lower[n]);
4541 gfc_add_block_to_block (&block, &lbse.pre);
4542 lbound = gfc_evaluate_now (lbse.expr, &block);
4544 else if (as && arg)
4546 tmp = gfc_get_symbol_decl (arg->symtree->n.sym);
4547 lbound = gfc_conv_descriptor_lbound_get (tmp,
4548 gfc_rank_cst[n]);
4550 else if (as)
4551 lbound = gfc_conv_descriptor_lbound_get (dest,
4552 gfc_rank_cst[n]);
4553 else
4554 lbound = gfc_index_one_node;
4556 lbound = fold_convert (gfc_array_index_type, lbound);
4558 /* Shift the bounds and set the offset accordingly. */
4559 tmp = gfc_conv_descriptor_ubound_get (dest, gfc_rank_cst[n]);
4560 span = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
4561 tmp, gfc_conv_descriptor_lbound_get (dest, gfc_rank_cst[n]));
4562 tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
4563 span, lbound);
4564 gfc_conv_descriptor_ubound_set (&block, dest,
4565 gfc_rank_cst[n], tmp);
4566 gfc_conv_descriptor_lbound_set (&block, dest,
4567 gfc_rank_cst[n], lbound);
4569 tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
4570 gfc_conv_descriptor_lbound_get (dest,
4571 gfc_rank_cst[n]),
4572 gfc_conv_descriptor_stride_get (dest,
4573 gfc_rank_cst[n]));
4574 gfc_add_modify (&block, tmp2, tmp);
4575 tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
4576 offset, tmp2);
4577 gfc_conv_descriptor_offset_set (&block, dest, tmp);
4580 if (arg)
4582 /* If a conversion expression has a null data pointer
4583 argument, nullify the allocatable component. */
4584 tree non_null_expr;
4585 tree null_expr;
4587 if (arg->symtree->n.sym->attr.allocatable
4588 || arg->symtree->n.sym->attr.pointer)
4590 non_null_expr = gfc_finish_block (&block);
4591 gfc_start_block (&block);
4592 gfc_conv_descriptor_data_set (&block, dest,
4593 null_pointer_node);
4594 null_expr = gfc_finish_block (&block);
4595 tmp = gfc_conv_descriptor_data_get (arg->symtree->n.sym->backend_decl);
4596 tmp = build2_loc (input_location, EQ_EXPR, boolean_type_node, tmp,
4597 fold_convert (TREE_TYPE (tmp), null_pointer_node));
4598 return build3_v (COND_EXPR, tmp,
4599 null_expr, non_null_expr);
4603 return gfc_finish_block (&block);
4607 /* Assign a single component of a derived type constructor. */
4609 static tree
4610 gfc_trans_subcomponent_assign (tree dest, gfc_component * cm, gfc_expr * expr)
4612 gfc_se se;
4613 gfc_se lse;
4614 gfc_ss *rss;
4615 stmtblock_t block;
4616 tree tmp;
4618 gfc_start_block (&block);
4620 if (cm->attr.pointer)
4622 gfc_init_se (&se, NULL);
4623 /* Pointer component. */
4624 if (cm->attr.dimension)
4626 /* Array pointer. */
4627 if (expr->expr_type == EXPR_NULL)
4628 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
4629 else
4631 rss = gfc_walk_expr (expr);
4632 se.direct_byref = 1;
4633 se.expr = dest;
4634 gfc_conv_expr_descriptor (&se, expr, rss);
4635 gfc_add_block_to_block (&block, &se.pre);
4636 gfc_add_block_to_block (&block, &se.post);
4639 else
4641 /* Scalar pointers. */
4642 se.want_pointer = 1;
4643 gfc_conv_expr (&se, expr);
4644 gfc_add_block_to_block (&block, &se.pre);
4645 gfc_add_modify (&block, dest,
4646 fold_convert (TREE_TYPE (dest), se.expr));
4647 gfc_add_block_to_block (&block, &se.post);
4650 else if (cm->ts.type == BT_CLASS && expr->expr_type == EXPR_NULL)
4652 /* NULL initialization for CLASS components. */
4653 tmp = gfc_trans_structure_assign (dest,
4654 gfc_class_null_initializer (&cm->ts));
4655 gfc_add_expr_to_block (&block, tmp);
4657 else if (cm->attr.dimension && !cm->attr.proc_pointer)
4659 if (cm->attr.allocatable && expr->expr_type == EXPR_NULL)
4660 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
4661 else if (cm->attr.allocatable)
4663 tmp = gfc_trans_alloc_subarray_assign (dest, cm, expr);
4664 gfc_add_expr_to_block (&block, tmp);
4666 else
4668 tmp = gfc_trans_subarray_assign (dest, cm, expr);
4669 gfc_add_expr_to_block (&block, tmp);
4672 else if (expr->ts.type == BT_DERIVED)
4674 if (expr->expr_type != EXPR_STRUCTURE)
4676 gfc_init_se (&se, NULL);
4677 gfc_conv_expr (&se, expr);
4678 gfc_add_block_to_block (&block, &se.pre);
4679 gfc_add_modify (&block, dest,
4680 fold_convert (TREE_TYPE (dest), se.expr));
4681 gfc_add_block_to_block (&block, &se.post);
4683 else
4685 /* Nested constructors. */
4686 tmp = gfc_trans_structure_assign (dest, expr);
4687 gfc_add_expr_to_block (&block, tmp);
4690 else
4692 /* Scalar component. */
4693 gfc_init_se (&se, NULL);
4694 gfc_init_se (&lse, NULL);
4696 gfc_conv_expr (&se, expr);
4697 if (cm->ts.type == BT_CHARACTER)
4698 lse.string_length = cm->ts.u.cl->backend_decl;
4699 lse.expr = dest;
4700 tmp = gfc_trans_scalar_assign (&lse, &se, cm->ts, true, false, true);
4701 gfc_add_expr_to_block (&block, tmp);
4703 return gfc_finish_block (&block);
4706 /* Assign a derived type constructor to a variable. */
4708 static tree
4709 gfc_trans_structure_assign (tree dest, gfc_expr * expr)
4711 gfc_constructor *c;
4712 gfc_component *cm;
4713 stmtblock_t block;
4714 tree field;
4715 tree tmp;
4717 gfc_start_block (&block);
4718 cm = expr->ts.u.derived->components;
4720 if (expr->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING
4721 && (expr->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
4722 || expr->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR))
4724 gfc_se se, lse;
4726 gcc_assert (cm->backend_decl == NULL);
4727 gfc_init_se (&se, NULL);
4728 gfc_init_se (&lse, NULL);
4729 gfc_conv_expr (&se, gfc_constructor_first (expr->value.constructor)->expr);
4730 lse.expr = dest;
4731 gfc_add_modify (&block, lse.expr,
4732 fold_convert (TREE_TYPE (lse.expr), se.expr));
4734 return gfc_finish_block (&block);
4737 for (c = gfc_constructor_first (expr->value.constructor);
4738 c; c = gfc_constructor_next (c), cm = cm->next)
4740 /* Skip absent members in default initializers. */
4741 if (!c->expr)
4742 continue;
4744 field = cm->backend_decl;
4745 tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
4746 dest, field, NULL_TREE);
4747 tmp = gfc_trans_subcomponent_assign (tmp, cm, c->expr);
4748 gfc_add_expr_to_block (&block, tmp);
4750 return gfc_finish_block (&block);
4753 /* Build an expression for a constructor. If init is nonzero then
4754 this is part of a static variable initializer. */
4756 void
4757 gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
4759 gfc_constructor *c;
4760 gfc_component *cm;
4761 tree val;
4762 tree type;
4763 tree tmp;
4764 VEC(constructor_elt,gc) *v = NULL;
4766 gcc_assert (se->ss == NULL);
4767 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
4768 type = gfc_typenode_for_spec (&expr->ts);
4770 if (!init)
4772 /* Create a temporary variable and fill it in. */
4773 se->expr = gfc_create_var (type, expr->ts.u.derived->name);
4774 tmp = gfc_trans_structure_assign (se->expr, expr);
4775 gfc_add_expr_to_block (&se->pre, tmp);
4776 return;
4779 cm = expr->ts.u.derived->components;
4781 for (c = gfc_constructor_first (expr->value.constructor);
4782 c; c = gfc_constructor_next (c), cm = cm->next)
4784 /* Skip absent members in default initializers and allocatable
4785 components. Although the latter have a default initializer
4786 of EXPR_NULL,... by default, the static nullify is not needed
4787 since this is done every time we come into scope. */
4788 if (!c->expr || (cm->attr.allocatable && cm->attr.flavor != FL_PROCEDURE))
4789 continue;
4791 if (strcmp (cm->name, "_size") == 0)
4793 val = TYPE_SIZE_UNIT (gfc_get_derived_type (cm->ts.u.derived));
4794 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
4796 else if (cm->initializer && cm->initializer->expr_type != EXPR_NULL
4797 && strcmp (cm->name, "_extends") == 0)
4799 tree vtab;
4800 gfc_symbol *vtabs;
4801 vtabs = cm->initializer->symtree->n.sym;
4802 vtab = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtabs));
4803 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, vtab);
4805 else
4807 val = gfc_conv_initializer (c->expr, &cm->ts,
4808 TREE_TYPE (cm->backend_decl),
4809 cm->attr.dimension, cm->attr.pointer,
4810 cm->attr.proc_pointer);
4812 /* Append it to the constructor list. */
4813 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
4816 se->expr = build_constructor (type, v);
4817 if (init)
4818 TREE_CONSTANT (se->expr) = 1;
4822 /* Translate a substring expression. */
4824 static void
4825 gfc_conv_substring_expr (gfc_se * se, gfc_expr * expr)
4827 gfc_ref *ref;
4829 ref = expr->ref;
4831 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
4833 se->expr = gfc_build_wide_string_const (expr->ts.kind,
4834 expr->value.character.length,
4835 expr->value.character.string);
4837 se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
4838 TYPE_STRING_FLAG (TREE_TYPE (se->expr)) = 1;
4840 if (ref)
4841 gfc_conv_substring (se, ref, expr->ts.kind, NULL, &expr->where);
4845 /* Entry point for expression translation. Evaluates a scalar quantity.
4846 EXPR is the expression to be translated, and SE is the state structure if
4847 called from within the scalarized. */
4849 void
4850 gfc_conv_expr (gfc_se * se, gfc_expr * expr)
4852 gfc_ss *ss;
4854 ss = se->ss;
4855 if (ss && ss->info->expr == expr
4856 && (ss->info->type == GFC_SS_SCALAR
4857 || ss->info->type == GFC_SS_REFERENCE))
4859 gfc_ss_info *ss_info;
4861 ss_info = ss->info;
4862 /* Substitute a scalar expression evaluated outside the scalarization
4863 loop. */
4864 se->expr = ss_info->data.scalar.value;
4865 if (ss_info->type == GFC_SS_REFERENCE)
4866 se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
4867 se->string_length = ss_info->string_length;
4868 gfc_advance_se_ss_chain (se);
4869 return;
4872 /* We need to convert the expressions for the iso_c_binding derived types.
4873 C_NULL_PTR and C_NULL_FUNPTR will be made EXPR_NULL, which evaluates to
4874 null_pointer_node. C_PTR and C_FUNPTR are converted to match the
4875 typespec for the C_PTR and C_FUNPTR symbols, which has already been
4876 updated to be an integer with a kind equal to the size of a (void *). */
4877 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
4878 && expr->ts.u.derived->attr.is_iso_c)
4880 if (expr->expr_type == EXPR_VARIABLE
4881 && (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
4882 || expr->symtree->n.sym->intmod_sym_id
4883 == ISOCBINDING_NULL_FUNPTR))
4885 /* Set expr_type to EXPR_NULL, which will result in
4886 null_pointer_node being used below. */
4887 expr->expr_type = EXPR_NULL;
4889 else
4891 /* Update the type/kind of the expression to be what the new
4892 type/kind are for the updated symbols of C_PTR/C_FUNPTR. */
4893 expr->ts.type = expr->ts.u.derived->ts.type;
4894 expr->ts.f90_type = expr->ts.u.derived->ts.f90_type;
4895 expr->ts.kind = expr->ts.u.derived->ts.kind;
4899 switch (expr->expr_type)
4901 case EXPR_OP:
4902 gfc_conv_expr_op (se, expr);
4903 break;
4905 case EXPR_FUNCTION:
4906 gfc_conv_function_expr (se, expr);
4907 break;
4909 case EXPR_CONSTANT:
4910 gfc_conv_constant (se, expr);
4911 break;
4913 case EXPR_VARIABLE:
4914 gfc_conv_variable (se, expr);
4915 break;
4917 case EXPR_NULL:
4918 se->expr = null_pointer_node;
4919 break;
4921 case EXPR_SUBSTRING:
4922 gfc_conv_substring_expr (se, expr);
4923 break;
4925 case EXPR_STRUCTURE:
4926 gfc_conv_structure (se, expr, 0);
4927 break;
4929 case EXPR_ARRAY:
4930 gfc_conv_array_constructor_expr (se, expr);
4931 break;
4933 default:
4934 gcc_unreachable ();
4935 break;
4939 /* Like gfc_conv_expr_val, but the value is also suitable for use in the lhs
4940 of an assignment. */
4941 void
4942 gfc_conv_expr_lhs (gfc_se * se, gfc_expr * expr)
4944 gfc_conv_expr (se, expr);
4945 /* All numeric lvalues should have empty post chains. If not we need to
4946 figure out a way of rewriting an lvalue so that it has no post chain. */
4947 gcc_assert (expr->ts.type == BT_CHARACTER || !se->post.head);
4950 /* Like gfc_conv_expr, but the POST block is guaranteed to be empty for
4951 numeric expressions. Used for scalar values where inserting cleanup code
4952 is inconvenient. */
4953 void
4954 gfc_conv_expr_val (gfc_se * se, gfc_expr * expr)
4956 tree val;
4958 gcc_assert (expr->ts.type != BT_CHARACTER);
4959 gfc_conv_expr (se, expr);
4960 if (se->post.head)
4962 val = gfc_create_var (TREE_TYPE (se->expr), NULL);
4963 gfc_add_modify (&se->pre, val, se->expr);
4964 se->expr = val;
4965 gfc_add_block_to_block (&se->pre, &se->post);
4969 /* Helper to translate an expression and convert it to a particular type. */
4970 void
4971 gfc_conv_expr_type (gfc_se * se, gfc_expr * expr, tree type)
4973 gfc_conv_expr_val (se, expr);
4974 se->expr = convert (type, se->expr);
4978 /* Converts an expression so that it can be passed by reference. Scalar
4979 values only. */
4981 void
4982 gfc_conv_expr_reference (gfc_se * se, gfc_expr * expr)
4984 gfc_ss *ss;
4985 tree var;
4987 ss = se->ss;
4988 if (ss && ss->info->expr == expr
4989 && ss->info->type == GFC_SS_REFERENCE)
4991 /* Returns a reference to the scalar evaluated outside the loop
4992 for this case. */
4993 gfc_conv_expr (se, expr);
4994 return;
4997 if (expr->ts.type == BT_CHARACTER)
4999 gfc_conv_expr (se, expr);
5000 gfc_conv_string_parameter (se);
5001 return;
5004 if (expr->expr_type == EXPR_VARIABLE)
5006 se->want_pointer = 1;
5007 gfc_conv_expr (se, expr);
5008 if (se->post.head)
5010 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
5011 gfc_add_modify (&se->pre, var, se->expr);
5012 gfc_add_block_to_block (&se->pre, &se->post);
5013 se->expr = var;
5015 return;
5018 if (expr->expr_type == EXPR_FUNCTION
5019 && ((expr->value.function.esym
5020 && expr->value.function.esym->result->attr.pointer
5021 && !expr->value.function.esym->result->attr.dimension)
5022 || (!expr->value.function.esym
5023 && expr->symtree->n.sym->attr.pointer
5024 && !expr->symtree->n.sym->attr.dimension)))
5026 se->want_pointer = 1;
5027 gfc_conv_expr (se, expr);
5028 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
5029 gfc_add_modify (&se->pre, var, se->expr);
5030 se->expr = var;
5031 return;
5035 gfc_conv_expr (se, expr);
5037 /* Create a temporary var to hold the value. */
5038 if (TREE_CONSTANT (se->expr))
5040 tree tmp = se->expr;
5041 STRIP_TYPE_NOPS (tmp);
5042 var = build_decl (input_location,
5043 CONST_DECL, NULL, TREE_TYPE (tmp));
5044 DECL_INITIAL (var) = tmp;
5045 TREE_STATIC (var) = 1;
5046 pushdecl (var);
5048 else
5050 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
5051 gfc_add_modify (&se->pre, var, se->expr);
5053 gfc_add_block_to_block (&se->pre, &se->post);
5055 /* Take the address of that value. */
5056 se->expr = gfc_build_addr_expr (NULL_TREE, var);
5060 tree
5061 gfc_trans_pointer_assign (gfc_code * code)
5063 return gfc_trans_pointer_assignment (code->expr1, code->expr2);
5067 /* Generate code for a pointer assignment. */
5069 tree
5070 gfc_trans_pointer_assignment (gfc_expr * expr1, gfc_expr * expr2)
5072 gfc_se lse;
5073 gfc_se rse;
5074 gfc_ss *lss;
5075 gfc_ss *rss;
5076 stmtblock_t block;
5077 tree desc;
5078 tree tmp;
5079 tree decl;
5081 gfc_start_block (&block);
5083 gfc_init_se (&lse, NULL);
5085 lss = gfc_walk_expr (expr1);
5086 rss = gfc_walk_expr (expr2);
5087 if (lss == gfc_ss_terminator)
5089 /* Scalar pointers. */
5090 lse.want_pointer = 1;
5091 gfc_conv_expr (&lse, expr1);
5092 gcc_assert (rss == gfc_ss_terminator);
5093 gfc_init_se (&rse, NULL);
5094 rse.want_pointer = 1;
5095 gfc_conv_expr (&rse, expr2);
5097 if (expr1->symtree->n.sym->attr.proc_pointer
5098 && expr1->symtree->n.sym->attr.dummy)
5099 lse.expr = build_fold_indirect_ref_loc (input_location,
5100 lse.expr);
5102 if (expr2->symtree && expr2->symtree->n.sym->attr.proc_pointer
5103 && expr2->symtree->n.sym->attr.dummy)
5104 rse.expr = build_fold_indirect_ref_loc (input_location,
5105 rse.expr);
5107 gfc_add_block_to_block (&block, &lse.pre);
5108 gfc_add_block_to_block (&block, &rse.pre);
5110 /* Check character lengths if character expression. The test is only
5111 really added if -fbounds-check is enabled. Exclude deferred
5112 character length lefthand sides. */
5113 if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL
5114 && !(expr1->ts.deferred
5115 && (TREE_CODE (lse.string_length) == VAR_DECL))
5116 && !expr1->symtree->n.sym->attr.proc_pointer
5117 && !gfc_is_proc_ptr_comp (expr1, NULL))
5119 gcc_assert (expr2->ts.type == BT_CHARACTER);
5120 gcc_assert (lse.string_length && rse.string_length);
5121 gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
5122 lse.string_length, rse.string_length,
5123 &block);
5126 /* The assignment to an deferred character length sets the string
5127 length to that of the rhs. */
5128 if (expr1->ts.deferred && (TREE_CODE (lse.string_length) == VAR_DECL))
5130 if (expr2->expr_type != EXPR_NULL)
5131 gfc_add_modify (&block, lse.string_length, rse.string_length);
5132 else
5133 gfc_add_modify (&block, lse.string_length,
5134 build_int_cst (gfc_charlen_type_node, 0));
5137 gfc_add_modify (&block, lse.expr,
5138 fold_convert (TREE_TYPE (lse.expr), rse.expr));
5140 gfc_add_block_to_block (&block, &rse.post);
5141 gfc_add_block_to_block (&block, &lse.post);
5143 else
5145 gfc_ref* remap;
5146 bool rank_remap;
5147 tree strlen_lhs;
5148 tree strlen_rhs = NULL_TREE;
5150 /* Array pointer. Find the last reference on the LHS and if it is an
5151 array section ref, we're dealing with bounds remapping. In this case,
5152 set it to AR_FULL so that gfc_conv_expr_descriptor does
5153 not see it and process the bounds remapping afterwards explicitely. */
5154 for (remap = expr1->ref; remap; remap = remap->next)
5155 if (!remap->next && remap->type == REF_ARRAY
5156 && remap->u.ar.type == AR_SECTION)
5158 remap->u.ar.type = AR_FULL;
5159 break;
5161 rank_remap = (remap && remap->u.ar.end[0]);
5163 gfc_conv_expr_descriptor (&lse, expr1, lss);
5164 strlen_lhs = lse.string_length;
5165 desc = lse.expr;
5167 if (expr2->expr_type == EXPR_NULL)
5169 /* Just set the data pointer to null. */
5170 gfc_conv_descriptor_data_set (&lse.pre, lse.expr, null_pointer_node);
5172 else if (rank_remap)
5174 /* If we are rank-remapping, just get the RHS's descriptor and
5175 process this later on. */
5176 gfc_init_se (&rse, NULL);
5177 rse.direct_byref = 1;
5178 rse.byref_noassign = 1;
5179 gfc_conv_expr_descriptor (&rse, expr2, rss);
5180 strlen_rhs = rse.string_length;
5182 else if (expr2->expr_type == EXPR_VARIABLE)
5184 /* Assign directly to the LHS's descriptor. */
5185 lse.direct_byref = 1;
5186 gfc_conv_expr_descriptor (&lse, expr2, rss);
5187 strlen_rhs = lse.string_length;
5189 /* If this is a subreference array pointer assignment, use the rhs
5190 descriptor element size for the lhs span. */
5191 if (expr1->symtree->n.sym->attr.subref_array_pointer)
5193 decl = expr1->symtree->n.sym->backend_decl;
5194 gfc_init_se (&rse, NULL);
5195 rse.descriptor_only = 1;
5196 gfc_conv_expr (&rse, expr2);
5197 tmp = gfc_get_element_type (TREE_TYPE (rse.expr));
5198 tmp = fold_convert (gfc_array_index_type, size_in_bytes (tmp));
5199 if (!INTEGER_CST_P (tmp))
5200 gfc_add_block_to_block (&lse.post, &rse.pre);
5201 gfc_add_modify (&lse.post, GFC_DECL_SPAN(decl), tmp);
5204 else
5206 /* Assign to a temporary descriptor and then copy that
5207 temporary to the pointer. */
5208 tmp = gfc_create_var (TREE_TYPE (desc), "ptrtemp");
5210 lse.expr = tmp;
5211 lse.direct_byref = 1;
5212 gfc_conv_expr_descriptor (&lse, expr2, rss);
5213 strlen_rhs = lse.string_length;
5214 gfc_add_modify (&lse.pre, desc, tmp);
5217 gfc_add_block_to_block (&block, &lse.pre);
5218 if (rank_remap)
5219 gfc_add_block_to_block (&block, &rse.pre);
5221 /* If we do bounds remapping, update LHS descriptor accordingly. */
5222 if (remap)
5224 int dim;
5225 gcc_assert (remap->u.ar.dimen == expr1->rank);
5227 if (rank_remap)
5229 /* Do rank remapping. We already have the RHS's descriptor
5230 converted in rse and now have to build the correct LHS
5231 descriptor for it. */
5233 tree dtype, data;
5234 tree offs, stride;
5235 tree lbound, ubound;
5237 /* Set dtype. */
5238 dtype = gfc_conv_descriptor_dtype (desc);
5239 tmp = gfc_get_dtype (TREE_TYPE (desc));
5240 gfc_add_modify (&block, dtype, tmp);
5242 /* Copy data pointer. */
5243 data = gfc_conv_descriptor_data_get (rse.expr);
5244 gfc_conv_descriptor_data_set (&block, desc, data);
5246 /* Copy offset but adjust it such that it would correspond
5247 to a lbound of zero. */
5248 offs = gfc_conv_descriptor_offset_get (rse.expr);
5249 for (dim = 0; dim < expr2->rank; ++dim)
5251 stride = gfc_conv_descriptor_stride_get (rse.expr,
5252 gfc_rank_cst[dim]);
5253 lbound = gfc_conv_descriptor_lbound_get (rse.expr,
5254 gfc_rank_cst[dim]);
5255 tmp = fold_build2_loc (input_location, MULT_EXPR,
5256 gfc_array_index_type, stride, lbound);
5257 offs = fold_build2_loc (input_location, PLUS_EXPR,
5258 gfc_array_index_type, offs, tmp);
5260 gfc_conv_descriptor_offset_set (&block, desc, offs);
5262 /* Set the bounds as declared for the LHS and calculate strides as
5263 well as another offset update accordingly. */
5264 stride = gfc_conv_descriptor_stride_get (rse.expr,
5265 gfc_rank_cst[0]);
5266 for (dim = 0; dim < expr1->rank; ++dim)
5268 gfc_se lower_se;
5269 gfc_se upper_se;
5271 gcc_assert (remap->u.ar.start[dim] && remap->u.ar.end[dim]);
5273 /* Convert declared bounds. */
5274 gfc_init_se (&lower_se, NULL);
5275 gfc_init_se (&upper_se, NULL);
5276 gfc_conv_expr (&lower_se, remap->u.ar.start[dim]);
5277 gfc_conv_expr (&upper_se, remap->u.ar.end[dim]);
5279 gfc_add_block_to_block (&block, &lower_se.pre);
5280 gfc_add_block_to_block (&block, &upper_se.pre);
5282 lbound = fold_convert (gfc_array_index_type, lower_se.expr);
5283 ubound = fold_convert (gfc_array_index_type, upper_se.expr);
5285 lbound = gfc_evaluate_now (lbound, &block);
5286 ubound = gfc_evaluate_now (ubound, &block);
5288 gfc_add_block_to_block (&block, &lower_se.post);
5289 gfc_add_block_to_block (&block, &upper_se.post);
5291 /* Set bounds in descriptor. */
5292 gfc_conv_descriptor_lbound_set (&block, desc,
5293 gfc_rank_cst[dim], lbound);
5294 gfc_conv_descriptor_ubound_set (&block, desc,
5295 gfc_rank_cst[dim], ubound);
5297 /* Set stride. */
5298 stride = gfc_evaluate_now (stride, &block);
5299 gfc_conv_descriptor_stride_set (&block, desc,
5300 gfc_rank_cst[dim], stride);
5302 /* Update offset. */
5303 offs = gfc_conv_descriptor_offset_get (desc);
5304 tmp = fold_build2_loc (input_location, MULT_EXPR,
5305 gfc_array_index_type, lbound, stride);
5306 offs = fold_build2_loc (input_location, MINUS_EXPR,
5307 gfc_array_index_type, offs, tmp);
5308 offs = gfc_evaluate_now (offs, &block);
5309 gfc_conv_descriptor_offset_set (&block, desc, offs);
5311 /* Update stride. */
5312 tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
5313 stride = fold_build2_loc (input_location, MULT_EXPR,
5314 gfc_array_index_type, stride, tmp);
5317 else
5319 /* Bounds remapping. Just shift the lower bounds. */
5321 gcc_assert (expr1->rank == expr2->rank);
5323 for (dim = 0; dim < remap->u.ar.dimen; ++dim)
5325 gfc_se lbound_se;
5327 gcc_assert (remap->u.ar.start[dim]);
5328 gcc_assert (!remap->u.ar.end[dim]);
5329 gfc_init_se (&lbound_se, NULL);
5330 gfc_conv_expr (&lbound_se, remap->u.ar.start[dim]);
5332 gfc_add_block_to_block (&block, &lbound_se.pre);
5333 gfc_conv_shift_descriptor_lbound (&block, desc,
5334 dim, lbound_se.expr);
5335 gfc_add_block_to_block (&block, &lbound_se.post);
5340 /* Check string lengths if applicable. The check is only really added
5341 to the output code if -fbounds-check is enabled. */
5342 if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL)
5344 gcc_assert (expr2->ts.type == BT_CHARACTER);
5345 gcc_assert (strlen_lhs && strlen_rhs);
5346 gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
5347 strlen_lhs, strlen_rhs, &block);
5350 /* If rank remapping was done, check with -fcheck=bounds that
5351 the target is at least as large as the pointer. */
5352 if (rank_remap && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
5354 tree lsize, rsize;
5355 tree fault;
5356 const char* msg;
5358 lsize = gfc_conv_descriptor_size (lse.expr, expr1->rank);
5359 rsize = gfc_conv_descriptor_size (rse.expr, expr2->rank);
5361 lsize = gfc_evaluate_now (lsize, &block);
5362 rsize = gfc_evaluate_now (rsize, &block);
5363 fault = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
5364 rsize, lsize);
5366 msg = _("Target of rank remapping is too small (%ld < %ld)");
5367 gfc_trans_runtime_check (true, false, fault, &block, &expr2->where,
5368 msg, rsize, lsize);
5371 gfc_add_block_to_block (&block, &lse.post);
5372 if (rank_remap)
5373 gfc_add_block_to_block (&block, &rse.post);
5376 return gfc_finish_block (&block);
5380 /* Makes sure se is suitable for passing as a function string parameter. */
5381 /* TODO: Need to check all callers of this function. It may be abused. */
5383 void
5384 gfc_conv_string_parameter (gfc_se * se)
5386 tree type;
5388 if (TREE_CODE (se->expr) == STRING_CST)
5390 type = TREE_TYPE (TREE_TYPE (se->expr));
5391 se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
5392 return;
5395 if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
5397 if (TREE_CODE (se->expr) != INDIRECT_REF)
5399 type = TREE_TYPE (se->expr);
5400 se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
5402 else
5404 type = gfc_get_character_type_len (gfc_default_character_kind,
5405 se->string_length);
5406 type = build_pointer_type (type);
5407 se->expr = gfc_build_addr_expr (type, se->expr);
5411 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se->expr)));
5415 /* Generate code for assignment of scalar variables. Includes character
5416 strings and derived types with allocatable components.
5417 If you know that the LHS has no allocations, set dealloc to false. */
5419 tree
5420 gfc_trans_scalar_assign (gfc_se * lse, gfc_se * rse, gfc_typespec ts,
5421 bool l_is_temp, bool r_is_var, bool dealloc)
5423 stmtblock_t block;
5424 tree tmp;
5425 tree cond;
5427 gfc_init_block (&block);
5429 if (ts.type == BT_CHARACTER)
5431 tree rlen = NULL;
5432 tree llen = NULL;
5434 if (lse->string_length != NULL_TREE)
5436 gfc_conv_string_parameter (lse);
5437 gfc_add_block_to_block (&block, &lse->pre);
5438 llen = lse->string_length;
5441 if (rse->string_length != NULL_TREE)
5443 gcc_assert (rse->string_length != NULL_TREE);
5444 gfc_conv_string_parameter (rse);
5445 gfc_add_block_to_block (&block, &rse->pre);
5446 rlen = rse->string_length;
5449 gfc_trans_string_copy (&block, llen, lse->expr, ts.kind, rlen,
5450 rse->expr, ts.kind);
5452 else if (ts.type == BT_DERIVED && ts.u.derived->attr.alloc_comp)
5454 cond = NULL_TREE;
5456 /* Are the rhs and the lhs the same? */
5457 if (r_is_var)
5459 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
5460 gfc_build_addr_expr (NULL_TREE, lse->expr),
5461 gfc_build_addr_expr (NULL_TREE, rse->expr));
5462 cond = gfc_evaluate_now (cond, &lse->pre);
5465 /* Deallocate the lhs allocated components as long as it is not
5466 the same as the rhs. This must be done following the assignment
5467 to prevent deallocating data that could be used in the rhs
5468 expression. */
5469 if (!l_is_temp && dealloc)
5471 tmp = gfc_evaluate_now (lse->expr, &lse->pre);
5472 tmp = gfc_deallocate_alloc_comp (ts.u.derived, tmp, 0);
5473 if (r_is_var)
5474 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
5475 tmp);
5476 gfc_add_expr_to_block (&lse->post, tmp);
5479 gfc_add_block_to_block (&block, &rse->pre);
5480 gfc_add_block_to_block (&block, &lse->pre);
5482 gfc_add_modify (&block, lse->expr,
5483 fold_convert (TREE_TYPE (lse->expr), rse->expr));
5485 /* Do a deep copy if the rhs is a variable, if it is not the
5486 same as the lhs. */
5487 if (r_is_var)
5489 tmp = gfc_copy_alloc_comp (ts.u.derived, rse->expr, lse->expr, 0);
5490 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
5491 tmp);
5492 gfc_add_expr_to_block (&block, tmp);
5495 else if (ts.type == BT_DERIVED || ts.type == BT_CLASS)
5497 gfc_add_block_to_block (&block, &lse->pre);
5498 gfc_add_block_to_block (&block, &rse->pre);
5499 tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
5500 TREE_TYPE (lse->expr), rse->expr);
5501 gfc_add_modify (&block, lse->expr, tmp);
5503 else
5505 gfc_add_block_to_block (&block, &lse->pre);
5506 gfc_add_block_to_block (&block, &rse->pre);
5508 gfc_add_modify (&block, lse->expr,
5509 fold_convert (TREE_TYPE (lse->expr), rse->expr));
5512 gfc_add_block_to_block (&block, &lse->post);
5513 gfc_add_block_to_block (&block, &rse->post);
5515 return gfc_finish_block (&block);
5519 /* There are quite a lot of restrictions on the optimisation in using an
5520 array function assign without a temporary. */
5522 static bool
5523 arrayfunc_assign_needs_temporary (gfc_expr * expr1, gfc_expr * expr2)
5525 gfc_ref * ref;
5526 bool seen_array_ref;
5527 bool c = false;
5528 gfc_symbol *sym = expr1->symtree->n.sym;
5530 /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION. */
5531 if (expr2->value.function.isym && !gfc_is_intrinsic_libcall (expr2))
5532 return true;
5534 /* Elemental functions are scalarized so that they don't need a
5535 temporary in gfc_trans_assignment_1, so return a true. Otherwise,
5536 they would need special treatment in gfc_trans_arrayfunc_assign. */
5537 if (expr2->value.function.esym != NULL
5538 && expr2->value.function.esym->attr.elemental)
5539 return true;
5541 /* Need a temporary if rhs is not FULL or a contiguous section. */
5542 if (expr1->ref && !(gfc_full_array_ref_p (expr1->ref, &c) || c))
5543 return true;
5545 /* Need a temporary if EXPR1 can't be expressed as a descriptor. */
5546 if (gfc_ref_needs_temporary_p (expr1->ref))
5547 return true;
5549 /* Functions returning pointers or allocatables need temporaries. */
5550 c = expr2->value.function.esym
5551 ? (expr2->value.function.esym->attr.pointer
5552 || expr2->value.function.esym->attr.allocatable)
5553 : (expr2->symtree->n.sym->attr.pointer
5554 || expr2->symtree->n.sym->attr.allocatable);
5555 if (c)
5556 return true;
5558 /* Character array functions need temporaries unless the
5559 character lengths are the same. */
5560 if (expr2->ts.type == BT_CHARACTER && expr2->rank > 0)
5562 if (expr1->ts.u.cl->length == NULL
5563 || expr1->ts.u.cl->length->expr_type != EXPR_CONSTANT)
5564 return true;
5566 if (expr2->ts.u.cl->length == NULL
5567 || expr2->ts.u.cl->length->expr_type != EXPR_CONSTANT)
5568 return true;
5570 if (mpz_cmp (expr1->ts.u.cl->length->value.integer,
5571 expr2->ts.u.cl->length->value.integer) != 0)
5572 return true;
5575 /* Check that no LHS component references appear during an array
5576 reference. This is needed because we do not have the means to
5577 span any arbitrary stride with an array descriptor. This check
5578 is not needed for the rhs because the function result has to be
5579 a complete type. */
5580 seen_array_ref = false;
5581 for (ref = expr1->ref; ref; ref = ref->next)
5583 if (ref->type == REF_ARRAY)
5584 seen_array_ref= true;
5585 else if (ref->type == REF_COMPONENT && seen_array_ref)
5586 return true;
5589 /* Check for a dependency. */
5590 if (gfc_check_fncall_dependency (expr1, INTENT_OUT,
5591 expr2->value.function.esym,
5592 expr2->value.function.actual,
5593 NOT_ELEMENTAL))
5594 return true;
5596 /* If we have reached here with an intrinsic function, we do not
5597 need a temporary except in the particular case that reallocation
5598 on assignment is active and the lhs is allocatable and a target. */
5599 if (expr2->value.function.isym)
5600 return (gfc_option.flag_realloc_lhs
5601 && sym->attr.allocatable
5602 && sym->attr.target);
5604 /* If the LHS is a dummy, we need a temporary if it is not
5605 INTENT(OUT). */
5606 if (sym->attr.dummy && sym->attr.intent != INTENT_OUT)
5607 return true;
5609 /* If the lhs has been host_associated, is in common, a pointer or is
5610 a target and the function is not using a RESULT variable, aliasing
5611 can occur and a temporary is needed. */
5612 if ((sym->attr.host_assoc
5613 || sym->attr.in_common
5614 || sym->attr.pointer
5615 || sym->attr.cray_pointee
5616 || sym->attr.target)
5617 && expr2->symtree != NULL
5618 && expr2->symtree->n.sym == expr2->symtree->n.sym->result)
5619 return true;
5621 /* A PURE function can unconditionally be called without a temporary. */
5622 if (expr2->value.function.esym != NULL
5623 && expr2->value.function.esym->attr.pure)
5624 return false;
5626 /* Implicit_pure functions are those which could legally be declared
5627 to be PURE. */
5628 if (expr2->value.function.esym != NULL
5629 && expr2->value.function.esym->attr.implicit_pure)
5630 return false;
5632 if (!sym->attr.use_assoc
5633 && !sym->attr.in_common
5634 && !sym->attr.pointer
5635 && !sym->attr.target
5636 && !sym->attr.cray_pointee
5637 && expr2->value.function.esym)
5639 /* A temporary is not needed if the function is not contained and
5640 the variable is local or host associated and not a pointer or
5641 a target. */
5642 if (!expr2->value.function.esym->attr.contained)
5643 return false;
5645 /* A temporary is not needed if the lhs has never been host
5646 associated and the procedure is contained. */
5647 else if (!sym->attr.host_assoc)
5648 return false;
5650 /* A temporary is not needed if the variable is local and not
5651 a pointer, a target or a result. */
5652 if (sym->ns->parent
5653 && expr2->value.function.esym->ns == sym->ns->parent)
5654 return false;
5657 /* Default to temporary use. */
5658 return true;
5662 /* Provide the loop info so that the lhs descriptor can be built for
5663 reallocatable assignments from extrinsic function calls. */
5665 static void
5666 realloc_lhs_loop_for_fcn_call (gfc_se *se, locus *where, gfc_ss **ss,
5667 gfc_loopinfo *loop)
5669 /* Signal that the function call should not be made by
5670 gfc_conv_loop_setup. */
5671 se->ss->is_alloc_lhs = 1;
5672 gfc_init_loopinfo (loop);
5673 gfc_add_ss_to_loop (loop, *ss);
5674 gfc_add_ss_to_loop (loop, se->ss);
5675 gfc_conv_ss_startstride (loop);
5676 gfc_conv_loop_setup (loop, where);
5677 gfc_copy_loopinfo_to_se (se, loop);
5678 gfc_add_block_to_block (&se->pre, &loop->pre);
5679 gfc_add_block_to_block (&se->pre, &loop->post);
5680 se->ss->is_alloc_lhs = 0;
5684 /* For Assignment to a reallocatable lhs from intrinsic functions,
5685 replace the se.expr (ie. the result) with a temporary descriptor.
5686 Null the data field so that the library allocates space for the
5687 result. Free the data of the original descriptor after the function,
5688 in case it appears in an argument expression and transfer the
5689 result to the original descriptor. */
5691 static void
5692 fcncall_realloc_result (gfc_se *se, int rank)
5694 tree desc;
5695 tree res_desc;
5696 tree tmp;
5697 tree offset;
5698 int n;
5700 /* Use the allocation done by the library. Substitute the lhs
5701 descriptor with a copy, whose data field is nulled.*/
5702 desc = build_fold_indirect_ref_loc (input_location, se->expr);
5703 /* Unallocated, the descriptor does not have a dtype. */
5704 tmp = gfc_conv_descriptor_dtype (desc);
5705 gfc_add_modify (&se->pre, tmp, gfc_get_dtype (TREE_TYPE (desc)));
5706 res_desc = gfc_evaluate_now (desc, &se->pre);
5707 gfc_conv_descriptor_data_set (&se->pre, res_desc, null_pointer_node);
5708 se->expr = gfc_build_addr_expr (TREE_TYPE (se->expr), res_desc);
5710 /* Free the lhs after the function call and copy the result to
5711 the lhs descriptor. */
5712 tmp = gfc_conv_descriptor_data_get (desc);
5713 tmp = gfc_call_free (fold_convert (pvoid_type_node, tmp));
5714 gfc_add_expr_to_block (&se->post, tmp);
5715 gfc_add_modify (&se->post, desc, res_desc);
5717 offset = gfc_index_zero_node;
5718 tmp = gfc_index_one_node;
5719 /* Now reset the bounds from zero based to unity based. */
5720 for (n = 0 ; n < rank; n++)
5722 /* Accumulate the offset. */
5723 offset = fold_build2_loc (input_location, MINUS_EXPR,
5724 gfc_array_index_type,
5725 offset, tmp);
5726 /* Now do the bounds. */
5727 gfc_conv_descriptor_offset_set (&se->post, desc, tmp);
5728 tmp = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
5729 tmp = fold_build2_loc (input_location, PLUS_EXPR,
5730 gfc_array_index_type,
5731 tmp, gfc_index_one_node);
5732 gfc_conv_descriptor_lbound_set (&se->post, desc,
5733 gfc_rank_cst[n],
5734 gfc_index_one_node);
5735 gfc_conv_descriptor_ubound_set (&se->post, desc,
5736 gfc_rank_cst[n], tmp);
5738 /* The extent for the next contribution to offset. */
5739 tmp = fold_build2_loc (input_location, MINUS_EXPR,
5740 gfc_array_index_type,
5741 gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]),
5742 gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]));
5743 tmp = fold_build2_loc (input_location, PLUS_EXPR,
5744 gfc_array_index_type,
5745 tmp, gfc_index_one_node);
5747 gfc_conv_descriptor_offset_set (&se->post, desc, offset);
5752 /* Try to translate array(:) = func (...), where func is a transformational
5753 array function, without using a temporary. Returns NULL if this isn't the
5754 case. */
5756 static tree
5757 gfc_trans_arrayfunc_assign (gfc_expr * expr1, gfc_expr * expr2)
5759 gfc_se se;
5760 gfc_ss *ss;
5761 gfc_component *comp = NULL;
5762 gfc_loopinfo loop;
5764 if (arrayfunc_assign_needs_temporary (expr1, expr2))
5765 return NULL;
5767 /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
5768 functions. */
5769 gcc_assert (expr2->value.function.isym
5770 || (gfc_is_proc_ptr_comp (expr2, &comp)
5771 && comp && comp->attr.dimension)
5772 || (!comp && gfc_return_by_reference (expr2->value.function.esym)
5773 && expr2->value.function.esym->result->attr.dimension));
5775 ss = gfc_walk_expr (expr1);
5776 gcc_assert (ss != gfc_ss_terminator);
5777 gfc_init_se (&se, NULL);
5778 gfc_start_block (&se.pre);
5779 se.want_pointer = 1;
5781 gfc_conv_array_parameter (&se, expr1, ss, false, NULL, NULL, NULL);
5783 if (expr1->ts.type == BT_DERIVED
5784 && expr1->ts.u.derived->attr.alloc_comp)
5786 tree tmp;
5787 tmp = gfc_deallocate_alloc_comp (expr1->ts.u.derived, se.expr,
5788 expr1->rank);
5789 gfc_add_expr_to_block (&se.pre, tmp);
5792 se.direct_byref = 1;
5793 se.ss = gfc_walk_expr (expr2);
5794 gcc_assert (se.ss != gfc_ss_terminator);
5796 /* Reallocate on assignment needs the loopinfo for extrinsic functions.
5797 This is signalled to gfc_conv_procedure_call by setting is_alloc_lhs.
5798 Clearly, this cannot be done for an allocatable function result, since
5799 the shape of the result is unknown and, in any case, the function must
5800 correctly take care of the reallocation internally. For intrinsic
5801 calls, the array data is freed and the library takes care of allocation.
5802 TODO: Add logic of trans-array.c: gfc_alloc_allocatable_for_assignment
5803 to the library. */
5804 if (gfc_option.flag_realloc_lhs
5805 && gfc_is_reallocatable_lhs (expr1)
5806 && !gfc_expr_attr (expr1).codimension
5807 && !gfc_is_coindexed (expr1)
5808 && !(expr2->value.function.esym
5809 && expr2->value.function.esym->result->attr.allocatable))
5811 if (!expr2->value.function.isym)
5813 realloc_lhs_loop_for_fcn_call (&se, &expr1->where, &ss, &loop);
5814 ss->is_alloc_lhs = 1;
5816 else
5817 fcncall_realloc_result (&se, expr1->rank);
5820 gfc_conv_function_expr (&se, expr2);
5821 gfc_add_block_to_block (&se.pre, &se.post);
5823 return gfc_finish_block (&se.pre);
5827 /* Try to efficiently translate array(:) = 0. Return NULL if this
5828 can't be done. */
5830 static tree
5831 gfc_trans_zero_assign (gfc_expr * expr)
5833 tree dest, len, type;
5834 tree tmp;
5835 gfc_symbol *sym;
5837 sym = expr->symtree->n.sym;
5838 dest = gfc_get_symbol_decl (sym);
5840 type = TREE_TYPE (dest);
5841 if (POINTER_TYPE_P (type))
5842 type = TREE_TYPE (type);
5843 if (!GFC_ARRAY_TYPE_P (type))
5844 return NULL_TREE;
5846 /* Determine the length of the array. */
5847 len = GFC_TYPE_ARRAY_SIZE (type);
5848 if (!len || TREE_CODE (len) != INTEGER_CST)
5849 return NULL_TREE;
5851 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
5852 len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
5853 fold_convert (gfc_array_index_type, tmp));
5855 /* If we are zeroing a local array avoid taking its address by emitting
5856 a = {} instead. */
5857 if (!POINTER_TYPE_P (TREE_TYPE (dest)))
5858 return build2_loc (input_location, MODIFY_EXPR, void_type_node,
5859 dest, build_constructor (TREE_TYPE (dest), NULL));
5861 /* Convert arguments to the correct types. */
5862 dest = fold_convert (pvoid_type_node, dest);
5863 len = fold_convert (size_type_node, len);
5865 /* Construct call to __builtin_memset. */
5866 tmp = build_call_expr_loc (input_location,
5867 builtin_decl_explicit (BUILT_IN_MEMSET),
5868 3, dest, integer_zero_node, len);
5869 return fold_convert (void_type_node, tmp);
5873 /* Helper for gfc_trans_array_copy and gfc_trans_array_constructor_copy
5874 that constructs the call to __builtin_memcpy. */
5876 tree
5877 gfc_build_memcpy_call (tree dst, tree src, tree len)
5879 tree tmp;
5881 /* Convert arguments to the correct types. */
5882 if (!POINTER_TYPE_P (TREE_TYPE (dst)))
5883 dst = gfc_build_addr_expr (pvoid_type_node, dst);
5884 else
5885 dst = fold_convert (pvoid_type_node, dst);
5887 if (!POINTER_TYPE_P (TREE_TYPE (src)))
5888 src = gfc_build_addr_expr (pvoid_type_node, src);
5889 else
5890 src = fold_convert (pvoid_type_node, src);
5892 len = fold_convert (size_type_node, len);
5894 /* Construct call to __builtin_memcpy. */
5895 tmp = build_call_expr_loc (input_location,
5896 builtin_decl_explicit (BUILT_IN_MEMCPY),
5897 3, dst, src, len);
5898 return fold_convert (void_type_node, tmp);
5902 /* Try to efficiently translate dst(:) = src(:). Return NULL if this
5903 can't be done. EXPR1 is the destination/lhs and EXPR2 is the
5904 source/rhs, both are gfc_full_array_ref_p which have been checked for
5905 dependencies. */
5907 static tree
5908 gfc_trans_array_copy (gfc_expr * expr1, gfc_expr * expr2)
5910 tree dst, dlen, dtype;
5911 tree src, slen, stype;
5912 tree tmp;
5914 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
5915 src = gfc_get_symbol_decl (expr2->symtree->n.sym);
5917 dtype = TREE_TYPE (dst);
5918 if (POINTER_TYPE_P (dtype))
5919 dtype = TREE_TYPE (dtype);
5920 stype = TREE_TYPE (src);
5921 if (POINTER_TYPE_P (stype))
5922 stype = TREE_TYPE (stype);
5924 if (!GFC_ARRAY_TYPE_P (dtype) || !GFC_ARRAY_TYPE_P (stype))
5925 return NULL_TREE;
5927 /* Determine the lengths of the arrays. */
5928 dlen = GFC_TYPE_ARRAY_SIZE (dtype);
5929 if (!dlen || TREE_CODE (dlen) != INTEGER_CST)
5930 return NULL_TREE;
5931 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
5932 dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
5933 dlen, fold_convert (gfc_array_index_type, tmp));
5935 slen = GFC_TYPE_ARRAY_SIZE (stype);
5936 if (!slen || TREE_CODE (slen) != INTEGER_CST)
5937 return NULL_TREE;
5938 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (stype));
5939 slen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
5940 slen, fold_convert (gfc_array_index_type, tmp));
5942 /* Sanity check that they are the same. This should always be
5943 the case, as we should already have checked for conformance. */
5944 if (!tree_int_cst_equal (slen, dlen))
5945 return NULL_TREE;
5947 return gfc_build_memcpy_call (dst, src, dlen);
5951 /* Try to efficiently translate array(:) = (/ ... /). Return NULL if
5952 this can't be done. EXPR1 is the destination/lhs for which
5953 gfc_full_array_ref_p is true, and EXPR2 is the source/rhs. */
5955 static tree
5956 gfc_trans_array_constructor_copy (gfc_expr * expr1, gfc_expr * expr2)
5958 unsigned HOST_WIDE_INT nelem;
5959 tree dst, dtype;
5960 tree src, stype;
5961 tree len;
5962 tree tmp;
5964 nelem = gfc_constant_array_constructor_p (expr2->value.constructor);
5965 if (nelem == 0)
5966 return NULL_TREE;
5968 dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
5969 dtype = TREE_TYPE (dst);
5970 if (POINTER_TYPE_P (dtype))
5971 dtype = TREE_TYPE (dtype);
5972 if (!GFC_ARRAY_TYPE_P (dtype))
5973 return NULL_TREE;
5975 /* Determine the lengths of the array. */
5976 len = GFC_TYPE_ARRAY_SIZE (dtype);
5977 if (!len || TREE_CODE (len) != INTEGER_CST)
5978 return NULL_TREE;
5980 /* Confirm that the constructor is the same size. */
5981 if (compare_tree_int (len, nelem) != 0)
5982 return NULL_TREE;
5984 tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
5985 len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
5986 fold_convert (gfc_array_index_type, tmp));
5988 stype = gfc_typenode_for_spec (&expr2->ts);
5989 src = gfc_build_constant_array_constructor (expr2, stype);
5991 stype = TREE_TYPE (src);
5992 if (POINTER_TYPE_P (stype))
5993 stype = TREE_TYPE (stype);
5995 return gfc_build_memcpy_call (dst, src, len);
5999 /* Tells whether the expression is to be treated as a variable reference. */
6001 static bool
6002 expr_is_variable (gfc_expr *expr)
6004 gfc_expr *arg;
6006 if (expr->expr_type == EXPR_VARIABLE)
6007 return true;
6009 arg = gfc_get_noncopying_intrinsic_argument (expr);
6010 if (arg)
6012 gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
6013 return expr_is_variable (arg);
6016 return false;
6020 /* Is the lhs OK for automatic reallocation? */
6022 static bool
6023 is_scalar_reallocatable_lhs (gfc_expr *expr)
6025 gfc_ref * ref;
6027 /* An allocatable variable with no reference. */
6028 if (expr->symtree->n.sym->attr.allocatable
6029 && !expr->ref)
6030 return true;
6032 /* All that can be left are allocatable components. */
6033 if ((expr->symtree->n.sym->ts.type != BT_DERIVED
6034 && expr->symtree->n.sym->ts.type != BT_CLASS)
6035 || !expr->symtree->n.sym->ts.u.derived->attr.alloc_comp)
6036 return false;
6038 /* Find an allocatable component ref last. */
6039 for (ref = expr->ref; ref; ref = ref->next)
6040 if (ref->type == REF_COMPONENT
6041 && !ref->next
6042 && ref->u.c.component->attr.allocatable)
6043 return true;
6045 return false;
6049 /* Allocate or reallocate scalar lhs, as necessary. */
6051 static void
6052 alloc_scalar_allocatable_for_assignment (stmtblock_t *block,
6053 tree string_length,
6054 gfc_expr *expr1,
6055 gfc_expr *expr2)
6058 tree cond;
6059 tree tmp;
6060 tree size;
6061 tree size_in_bytes;
6062 tree jump_label1;
6063 tree jump_label2;
6064 gfc_se lse;
6066 if (!expr1 || expr1->rank)
6067 return;
6069 if (!expr2 || expr2->rank)
6070 return;
6072 /* Since this is a scalar lhs, we can afford to do this. That is,
6073 there is no risk of side effects being repeated. */
6074 gfc_init_se (&lse, NULL);
6075 lse.want_pointer = 1;
6076 gfc_conv_expr (&lse, expr1);
6078 jump_label1 = gfc_build_label_decl (NULL_TREE);
6079 jump_label2 = gfc_build_label_decl (NULL_TREE);
6081 /* Do the allocation if the lhs is NULL. Otherwise go to label 1. */
6082 tmp = build_int_cst (TREE_TYPE (lse.expr), 0);
6083 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
6084 lse.expr, tmp);
6085 tmp = build3_v (COND_EXPR, cond,
6086 build1_v (GOTO_EXPR, jump_label1),
6087 build_empty_stmt (input_location));
6088 gfc_add_expr_to_block (block, tmp);
6090 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
6092 /* Use the rhs string length and the lhs element size. */
6093 size = string_length;
6094 tmp = TREE_TYPE (gfc_typenode_for_spec (&expr1->ts));
6095 tmp = TYPE_SIZE_UNIT (tmp);
6096 size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
6097 TREE_TYPE (tmp), tmp,
6098 fold_convert (TREE_TYPE (tmp), size));
6100 else
6102 /* Otherwise use the length in bytes of the rhs. */
6103 size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr1->ts));
6104 size_in_bytes = size;
6107 tmp = build_call_expr_loc (input_location,
6108 builtin_decl_explicit (BUILT_IN_MALLOC),
6109 1, size_in_bytes);
6110 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
6111 gfc_add_modify (block, lse.expr, tmp);
6112 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
6114 /* Deferred characters need checking for lhs and rhs string
6115 length. Other deferred parameter variables will have to
6116 come here too. */
6117 tmp = build1_v (GOTO_EXPR, jump_label2);
6118 gfc_add_expr_to_block (block, tmp);
6120 tmp = build1_v (LABEL_EXPR, jump_label1);
6121 gfc_add_expr_to_block (block, tmp);
6123 /* For a deferred length character, reallocate if lengths of lhs and
6124 rhs are different. */
6125 if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
6127 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
6128 expr1->ts.u.cl->backend_decl, size);
6129 /* Jump past the realloc if the lengths are the same. */
6130 tmp = build3_v (COND_EXPR, cond,
6131 build1_v (GOTO_EXPR, jump_label2),
6132 build_empty_stmt (input_location));
6133 gfc_add_expr_to_block (block, tmp);
6134 tmp = build_call_expr_loc (input_location,
6135 builtin_decl_explicit (BUILT_IN_REALLOC),
6136 2, fold_convert (pvoid_type_node, lse.expr),
6137 size_in_bytes);
6138 tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
6139 gfc_add_modify (block, lse.expr, tmp);
6140 tmp = build1_v (LABEL_EXPR, jump_label2);
6141 gfc_add_expr_to_block (block, tmp);
6143 /* Update the lhs character length. */
6144 size = string_length;
6145 gfc_add_modify (block, expr1->ts.u.cl->backend_decl, size);
6150 /* Subroutine of gfc_trans_assignment that actually scalarizes the
6151 assignment. EXPR1 is the destination/LHS and EXPR2 is the source/RHS.
6152 init_flag indicates initialization expressions and dealloc that no
6153 deallocate prior assignment is needed (if in doubt, set true). */
6155 static tree
6156 gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
6157 bool dealloc)
6159 gfc_se lse;
6160 gfc_se rse;
6161 gfc_ss *lss;
6162 gfc_ss *lss_section;
6163 gfc_ss *rss;
6164 gfc_loopinfo loop;
6165 tree tmp;
6166 stmtblock_t block;
6167 stmtblock_t body;
6168 bool l_is_temp;
6169 bool scalar_to_array;
6170 bool def_clen_func;
6171 tree string_length;
6172 int n;
6174 /* Assignment of the form lhs = rhs. */
6175 gfc_start_block (&block);
6177 gfc_init_se (&lse, NULL);
6178 gfc_init_se (&rse, NULL);
6180 /* Walk the lhs. */
6181 lss = gfc_walk_expr (expr1);
6182 if (gfc_is_reallocatable_lhs (expr1)
6183 && !(expr2->expr_type == EXPR_FUNCTION
6184 && expr2->value.function.isym != NULL))
6185 lss->is_alloc_lhs = 1;
6186 rss = NULL;
6187 if (lss != gfc_ss_terminator)
6189 /* The assignment needs scalarization. */
6190 lss_section = lss;
6192 /* Find a non-scalar SS from the lhs. */
6193 while (lss_section != gfc_ss_terminator
6194 && lss_section->info->type != GFC_SS_SECTION)
6195 lss_section = lss_section->next;
6197 gcc_assert (lss_section != gfc_ss_terminator);
6199 /* Initialize the scalarizer. */
6200 gfc_init_loopinfo (&loop);
6202 /* Walk the rhs. */
6203 rss = gfc_walk_expr (expr2);
6204 if (rss == gfc_ss_terminator)
6205 /* The rhs is scalar. Add a ss for the expression. */
6206 rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2);
6208 /* Associate the SS with the loop. */
6209 gfc_add_ss_to_loop (&loop, lss);
6210 gfc_add_ss_to_loop (&loop, rss);
6212 /* Calculate the bounds of the scalarization. */
6213 gfc_conv_ss_startstride (&loop);
6214 /* Enable loop reversal. */
6215 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
6216 loop.reverse[n] = GFC_ENABLE_REVERSE;
6217 /* Resolve any data dependencies in the statement. */
6218 gfc_conv_resolve_dependencies (&loop, lss, rss);
6219 /* Setup the scalarizing loops. */
6220 gfc_conv_loop_setup (&loop, &expr2->where);
6222 /* Setup the gfc_se structures. */
6223 gfc_copy_loopinfo_to_se (&lse, &loop);
6224 gfc_copy_loopinfo_to_se (&rse, &loop);
6226 rse.ss = rss;
6227 gfc_mark_ss_chain_used (rss, 1);
6228 if (loop.temp_ss == NULL)
6230 lse.ss = lss;
6231 gfc_mark_ss_chain_used (lss, 1);
6233 else
6235 lse.ss = loop.temp_ss;
6236 gfc_mark_ss_chain_used (lss, 3);
6237 gfc_mark_ss_chain_used (loop.temp_ss, 3);
6240 /* Allow the scalarizer to workshare array assignments. */
6241 if ((ompws_flags & OMPWS_WORKSHARE_FLAG) && loop.temp_ss == NULL)
6242 ompws_flags |= OMPWS_SCALARIZER_WS;
6244 /* Start the scalarized loop body. */
6245 gfc_start_scalarized_body (&loop, &body);
6247 else
6248 gfc_init_block (&body);
6250 l_is_temp = (lss != gfc_ss_terminator && loop.temp_ss != NULL);
6252 /* Translate the expression. */
6253 gfc_conv_expr (&rse, expr2);
6255 /* Stabilize a string length for temporaries. */
6256 if (expr2->ts.type == BT_CHARACTER)
6257 string_length = gfc_evaluate_now (rse.string_length, &rse.pre);
6258 else
6259 string_length = NULL_TREE;
6261 if (l_is_temp)
6263 gfc_conv_tmp_array_ref (&lse);
6264 if (expr2->ts.type == BT_CHARACTER)
6265 lse.string_length = string_length;
6267 else
6268 gfc_conv_expr (&lse, expr1);
6270 /* Assignments of scalar derived types with allocatable components
6271 to arrays must be done with a deep copy and the rhs temporary
6272 must have its components deallocated afterwards. */
6273 scalar_to_array = (expr2->ts.type == BT_DERIVED
6274 && expr2->ts.u.derived->attr.alloc_comp
6275 && !expr_is_variable (expr2)
6276 && !gfc_is_constant_expr (expr2)
6277 && expr1->rank && !expr2->rank);
6278 if (scalar_to_array && dealloc)
6280 tmp = gfc_deallocate_alloc_comp (expr2->ts.u.derived, rse.expr, 0);
6281 gfc_add_expr_to_block (&loop.post, tmp);
6284 /* For a deferred character length function, the function call must
6285 happen before the (re)allocation of the lhs, otherwise the character
6286 length of the result is not known. */
6287 def_clen_func = (((expr2->expr_type == EXPR_FUNCTION)
6288 || (expr2->expr_type == EXPR_COMPCALL)
6289 || (expr2->expr_type == EXPR_PPC))
6290 && expr2->ts.deferred);
6291 if (gfc_option.flag_realloc_lhs
6292 && expr2->ts.type == BT_CHARACTER
6293 && (def_clen_func || expr2->expr_type == EXPR_OP)
6294 && expr1->ts.deferred)
6295 gfc_add_block_to_block (&block, &rse.pre);
6297 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
6298 l_is_temp || init_flag,
6299 expr_is_variable (expr2) || scalar_to_array
6300 || expr2->expr_type == EXPR_ARRAY, dealloc);
6301 gfc_add_expr_to_block (&body, tmp);
6303 if (lss == gfc_ss_terminator)
6305 /* F2003: Add the code for reallocation on assignment. */
6306 if (gfc_option.flag_realloc_lhs
6307 && is_scalar_reallocatable_lhs (expr1))
6308 alloc_scalar_allocatable_for_assignment (&block, rse.string_length,
6309 expr1, expr2);
6311 /* Use the scalar assignment as is. */
6312 gfc_add_block_to_block (&block, &body);
6314 else
6316 gcc_assert (lse.ss == gfc_ss_terminator
6317 && rse.ss == gfc_ss_terminator);
6319 if (l_is_temp)
6321 gfc_trans_scalarized_loop_boundary (&loop, &body);
6323 /* We need to copy the temporary to the actual lhs. */
6324 gfc_init_se (&lse, NULL);
6325 gfc_init_se (&rse, NULL);
6326 gfc_copy_loopinfo_to_se (&lse, &loop);
6327 gfc_copy_loopinfo_to_se (&rse, &loop);
6329 rse.ss = loop.temp_ss;
6330 lse.ss = lss;
6332 gfc_conv_tmp_array_ref (&rse);
6333 gfc_conv_expr (&lse, expr1);
6335 gcc_assert (lse.ss == gfc_ss_terminator
6336 && rse.ss == gfc_ss_terminator);
6338 if (expr2->ts.type == BT_CHARACTER)
6339 rse.string_length = string_length;
6341 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
6342 false, false, dealloc);
6343 gfc_add_expr_to_block (&body, tmp);
6346 /* F2003: Allocate or reallocate lhs of allocatable array. */
6347 if (gfc_option.flag_realloc_lhs
6348 && gfc_is_reallocatable_lhs (expr1)
6349 && !gfc_expr_attr (expr1).codimension
6350 && !gfc_is_coindexed (expr1))
6352 ompws_flags &= ~OMPWS_SCALARIZER_WS;
6353 tmp = gfc_alloc_allocatable_for_assignment (&loop, expr1, expr2);
6354 if (tmp != NULL_TREE)
6355 gfc_add_expr_to_block (&loop.code[expr1->rank - 1], tmp);
6358 /* Generate the copying loops. */
6359 gfc_trans_scalarizing_loops (&loop, &body);
6361 /* Wrap the whole thing up. */
6362 gfc_add_block_to_block (&block, &loop.pre);
6363 gfc_add_block_to_block (&block, &loop.post);
6365 gfc_cleanup_loop (&loop);
6368 return gfc_finish_block (&block);
6372 /* Check whether EXPR is a copyable array. */
6374 static bool
6375 copyable_array_p (gfc_expr * expr)
6377 if (expr->expr_type != EXPR_VARIABLE)
6378 return false;
6380 /* First check it's an array. */
6381 if (expr->rank < 1 || !expr->ref || expr->ref->next)
6382 return false;
6384 if (!gfc_full_array_ref_p (expr->ref, NULL))
6385 return false;
6387 /* Next check that it's of a simple enough type. */
6388 switch (expr->ts.type)
6390 case BT_INTEGER:
6391 case BT_REAL:
6392 case BT_COMPLEX:
6393 case BT_LOGICAL:
6394 return true;
6396 case BT_CHARACTER:
6397 return false;
6399 case BT_DERIVED:
6400 return !expr->ts.u.derived->attr.alloc_comp;
6402 default:
6403 break;
6406 return false;
6409 /* Translate an assignment. */
6411 tree
6412 gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
6413 bool dealloc)
6415 tree tmp;
6417 /* Special case a single function returning an array. */
6418 if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
6420 tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
6421 if (tmp)
6422 return tmp;
6425 /* Special case assigning an array to zero. */
6426 if (copyable_array_p (expr1)
6427 && is_zero_initializer_p (expr2))
6429 tmp = gfc_trans_zero_assign (expr1);
6430 if (tmp)
6431 return tmp;
6434 /* Special case copying one array to another. */
6435 if (copyable_array_p (expr1)
6436 && copyable_array_p (expr2)
6437 && gfc_compare_types (&expr1->ts, &expr2->ts)
6438 && !gfc_check_dependency (expr1, expr2, 0))
6440 tmp = gfc_trans_array_copy (expr1, expr2);
6441 if (tmp)
6442 return tmp;
6445 /* Special case initializing an array from a constant array constructor. */
6446 if (copyable_array_p (expr1)
6447 && expr2->expr_type == EXPR_ARRAY
6448 && gfc_compare_types (&expr1->ts, &expr2->ts))
6450 tmp = gfc_trans_array_constructor_copy (expr1, expr2);
6451 if (tmp)
6452 return tmp;
6455 /* Fallback to the scalarizer to generate explicit loops. */
6456 return gfc_trans_assignment_1 (expr1, expr2, init_flag, dealloc);
6459 tree
6460 gfc_trans_init_assign (gfc_code * code)
6462 return gfc_trans_assignment (code->expr1, code->expr2, true, false);
6465 tree
6466 gfc_trans_assign (gfc_code * code)
6468 return gfc_trans_assignment (code->expr1, code->expr2, false, true);
6472 /* Special case for initializing a polymorphic dummy with INTENT(OUT).
6473 A MEMCPY is needed to copy the full data from the default initializer
6474 of the dynamic type. */
6476 tree
6477 gfc_trans_class_init_assign (gfc_code *code)
6479 stmtblock_t block;
6480 tree tmp;
6481 gfc_se dst,src,memsz;
6482 gfc_expr *lhs,*rhs,*sz;
6484 gfc_start_block (&block);
6486 lhs = gfc_copy_expr (code->expr1);
6487 gfc_add_data_component (lhs);
6489 rhs = gfc_copy_expr (code->expr1);
6490 gfc_add_vptr_component (rhs);
6492 /* Make sure that the component backend_decls have been built, which
6493 will not have happened if the derived types concerned have not
6494 been referenced. */
6495 gfc_get_derived_type (rhs->ts.u.derived);
6496 gfc_add_def_init_component (rhs);
6498 sz = gfc_copy_expr (code->expr1);
6499 gfc_add_vptr_component (sz);
6500 gfc_add_size_component (sz);
6502 gfc_init_se (&dst, NULL);
6503 gfc_init_se (&src, NULL);
6504 gfc_init_se (&memsz, NULL);
6505 gfc_conv_expr (&dst, lhs);
6506 gfc_conv_expr (&src, rhs);
6507 gfc_conv_expr (&memsz, sz);
6508 gfc_add_block_to_block (&block, &src.pre);
6509 tmp = gfc_build_memcpy_call (dst.expr, src.expr, memsz.expr);
6510 gfc_add_expr_to_block (&block, tmp);
6512 return gfc_finish_block (&block);
6516 /* Translate an assignment to a CLASS object
6517 (pointer or ordinary assignment). */
6519 tree
6520 gfc_trans_class_assign (gfc_expr *expr1, gfc_expr *expr2, gfc_exec_op op)
6522 stmtblock_t block;
6523 tree tmp;
6524 gfc_expr *lhs;
6525 gfc_expr *rhs;
6527 gfc_start_block (&block);
6529 if (expr2->ts.type != BT_CLASS)
6531 /* Insert an additional assignment which sets the '_vptr' field. */
6532 gfc_symbol *vtab = NULL;
6533 gfc_symtree *st;
6535 lhs = gfc_copy_expr (expr1);
6536 gfc_add_vptr_component (lhs);
6538 if (expr2->ts.type == BT_DERIVED)
6539 vtab = gfc_find_derived_vtab (expr2->ts.u.derived);
6540 else if (expr2->expr_type == EXPR_NULL)
6541 vtab = gfc_find_derived_vtab (expr1->ts.u.derived);
6542 gcc_assert (vtab);
6544 rhs = gfc_get_expr ();
6545 rhs->expr_type = EXPR_VARIABLE;
6546 gfc_find_sym_tree (vtab->name, vtab->ns, 1, &st);
6547 rhs->symtree = st;
6548 rhs->ts = vtab->ts;
6550 tmp = gfc_trans_pointer_assignment (lhs, rhs);
6551 gfc_add_expr_to_block (&block, tmp);
6553 gfc_free_expr (lhs);
6554 gfc_free_expr (rhs);
6557 /* Do the actual CLASS assignment. */
6558 if (expr2->ts.type == BT_CLASS)
6559 op = EXEC_ASSIGN;
6560 else
6561 gfc_add_data_component (expr1);
6563 if (op == EXEC_ASSIGN)
6564 tmp = gfc_trans_assignment (expr1, expr2, false, true);
6565 else if (op == EXEC_POINTER_ASSIGN)
6566 tmp = gfc_trans_pointer_assignment (expr1, expr2);
6567 else
6568 gcc_unreachable();
6570 gfc_add_expr_to_block (&block, tmp);
6572 return gfc_finish_block (&block);