re PR fortran/29976 (ICE on optional arg)
[gcc.git] / gcc / fortran / trans-expr.c
1 /* Expression translation
2 Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
4 and Steven Bosscher <s.bosscher@student.tudelft.nl>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
22
23 /* trans-expr.c-- generate GENERIC trees for gfc_expr. */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tree.h"
29 #include "convert.h"
30 #include "ggc.h"
31 #include "toplev.h"
32 #include "real.h"
33 #include "tree-gimple.h"
34 #include "langhooks.h"
35 #include "flags.h"
36 #include "gfortran.h"
37 #include "trans.h"
38 #include "trans-const.h"
39 #include "trans-types.h"
40 #include "trans-array.h"
41 /* Only for gfc_trans_assign and gfc_trans_pointer_assign. */
42 #include "trans-stmt.h"
43 #include "dependency.h"
44
45 static tree gfc_trans_structure_assign (tree dest, gfc_expr * expr);
46 static void gfc_apply_interface_mapping_to_expr (gfc_interface_mapping *,
47 gfc_expr *);
48
49 /* Copy the scalarization loop variables. */
50
51 static void
52 gfc_copy_se_loopvars (gfc_se * dest, gfc_se * src)
53 {
54 dest->ss = src->ss;
55 dest->loop = src->loop;
56 }
57
58
59 /* Initialize a simple expression holder.
60
61 Care must be taken when multiple se are created with the same parent.
62 The child se must be kept in sync. The easiest way is to delay creation
63 of a child se until after after the previous se has been translated. */
64
65 void
66 gfc_init_se (gfc_se * se, gfc_se * parent)
67 {
68 memset (se, 0, sizeof (gfc_se));
69 gfc_init_block (&se->pre);
70 gfc_init_block (&se->post);
71
72 se->parent = parent;
73
74 if (parent)
75 gfc_copy_se_loopvars (se, parent);
76 }
77
78
79 /* Advances to the next SS in the chain. Use this rather than setting
80 se->ss = se->ss->next because all the parents needs to be kept in sync.
81 See gfc_init_se. */
82
83 void
84 gfc_advance_se_ss_chain (gfc_se * se)
85 {
86 gfc_se *p;
87
88 gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
89
90 p = se;
91 /* Walk down the parent chain. */
92 while (p != NULL)
93 {
94 /* Simple consistency check. */
95 gcc_assert (p->parent == NULL || p->parent->ss == p->ss);
96
97 p->ss = p->ss->next;
98
99 p = p->parent;
100 }
101 }
102
103
104 /* Ensures the result of the expression as either a temporary variable
105 or a constant so that it can be used repeatedly. */
106
107 void
108 gfc_make_safe_expr (gfc_se * se)
109 {
110 tree var;
111
112 if (CONSTANT_CLASS_P (se->expr))
113 return;
114
115 /* We need a temporary for this result. */
116 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
117 gfc_add_modify_expr (&se->pre, var, se->expr);
118 se->expr = var;
119 }
120
121
122 /* Return an expression which determines if a dummy parameter is present.
123 Also used for arguments to procedures with multiple entry points. */
124
125 tree
126 gfc_conv_expr_present (gfc_symbol * sym)
127 {
128 tree decl;
129
130 gcc_assert (sym->attr.dummy);
131
132 decl = gfc_get_symbol_decl (sym);
133 if (TREE_CODE (decl) != PARM_DECL)
134 {
135 /* Array parameters use a temporary descriptor, we want the real
136 parameter. */
137 gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
138 || GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
139 decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
140 }
141 return build2 (NE_EXPR, boolean_type_node, decl,
142 fold_convert (TREE_TYPE (decl), null_pointer_node));
143 }
144
145
146 /* Converts a missing, dummy argument into a null or zero. */
147
148 void
149 gfc_conv_missing_dummy (gfc_se * se, gfc_expr * arg, gfc_typespec ts)
150 {
151 tree present;
152 tree tmp;
153
154 present = gfc_conv_expr_present (arg->symtree->n.sym);
155 tmp = build3 (COND_EXPR, TREE_TYPE (se->expr), present, se->expr,
156 fold_convert (TREE_TYPE (se->expr), integer_zero_node));
157
158 tmp = gfc_evaluate_now (tmp, &se->pre);
159 se->expr = tmp;
160 if (ts.type == BT_CHARACTER)
161 {
162 tmp = build_int_cst (gfc_charlen_type_node, 0);
163 tmp = build3 (COND_EXPR, gfc_charlen_type_node, present,
164 se->string_length, tmp);
165 tmp = gfc_evaluate_now (tmp, &se->pre);
166 se->string_length = tmp;
167 }
168 return;
169 }
170
171
172 /* Get the character length of an expression, looking through gfc_refs
173 if necessary. */
174
175 tree
176 gfc_get_expr_charlen (gfc_expr *e)
177 {
178 gfc_ref *r;
179 tree length;
180
181 gcc_assert (e->expr_type == EXPR_VARIABLE
182 && e->ts.type == BT_CHARACTER);
183
184 length = NULL; /* To silence compiler warning. */
185
186 /* First candidate: if the variable is of type CHARACTER, the
187 expression's length could be the length of the character
188 variable. */
189 if (e->symtree->n.sym->ts.type == BT_CHARACTER)
190 length = e->symtree->n.sym->ts.cl->backend_decl;
191
192 /* Look through the reference chain for component references. */
193 for (r = e->ref; r; r = r->next)
194 {
195 switch (r->type)
196 {
197 case REF_COMPONENT:
198 if (r->u.c.component->ts.type == BT_CHARACTER)
199 length = r->u.c.component->ts.cl->backend_decl;
200 break;
201
202 case REF_ARRAY:
203 /* Do nothing. */
204 break;
205
206 default:
207 /* We should never got substring references here. These will be
208 broken down by the scalarizer. */
209 gcc_unreachable ();
210 }
211 }
212
213 gcc_assert (length != NULL);
214 return length;
215 }
216
217
218
219 /* Generate code to initialize a string length variable. Returns the
220 value. */
221
222 void
223 gfc_trans_init_string_length (gfc_charlen * cl, stmtblock_t * pblock)
224 {
225 gfc_se se;
226 tree tmp;
227
228 gfc_init_se (&se, NULL);
229 gfc_conv_expr_type (&se, cl->length, gfc_charlen_type_node);
230 gfc_add_block_to_block (pblock, &se.pre);
231
232 tmp = cl->backend_decl;
233 gfc_add_modify_expr (pblock, tmp, se.expr);
234 }
235
236
237 static void
238 gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
239 const char *name, locus *where)
240 {
241 tree tmp;
242 tree type;
243 tree var;
244 tree fault;
245 gfc_se start;
246 gfc_se end;
247 char *msg;
248
249 type = gfc_get_character_type (kind, ref->u.ss.length);
250 type = build_pointer_type (type);
251
252 var = NULL_TREE;
253 gfc_init_se (&start, se);
254 gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
255 gfc_add_block_to_block (&se->pre, &start.pre);
256
257 if (integer_onep (start.expr))
258 gfc_conv_string_parameter (se);
259 else
260 {
261 /* Change the start of the string. */
262 if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
263 tmp = se->expr;
264 else
265 tmp = build_fold_indirect_ref (se->expr);
266 tmp = gfc_build_array_ref (tmp, start.expr);
267 se->expr = gfc_build_addr_expr (type, tmp);
268 }
269
270 /* Length = end + 1 - start. */
271 gfc_init_se (&end, se);
272 if (ref->u.ss.end == NULL)
273 end.expr = se->string_length;
274 else
275 {
276 gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
277 gfc_add_block_to_block (&se->pre, &end.pre);
278 }
279 if (flag_bounds_check)
280 {
281 /* Check lower bound. */
282 fault = fold_build2 (LT_EXPR, boolean_type_node, start.expr,
283 build_int_cst (gfc_charlen_type_node, 1));
284 if (name)
285 asprintf (&msg, "Substring out of bounds: lower bound of '%s' "
286 "is less than one", name);
287 else
288 asprintf (&msg, "Substring out of bounds: lower bound "
289 "is less than one");
290 gfc_trans_runtime_check (fault, msg, &se->pre, where);
291 gfc_free (msg);
292
293 /* Check upper bound. */
294 fault = fold_build2 (GT_EXPR, boolean_type_node, end.expr,
295 se->string_length);
296 if (name)
297 asprintf (&msg, "Substring out of bounds: upper bound of '%s' "
298 "exceeds string length", name);
299 else
300 asprintf (&msg, "Substring out of bounds: upper bound "
301 "exceeds string length");
302 gfc_trans_runtime_check (fault, msg, &se->pre, where);
303 gfc_free (msg);
304 }
305
306 tmp = fold_build2 (MINUS_EXPR, gfc_charlen_type_node,
307 build_int_cst (gfc_charlen_type_node, 1),
308 start.expr);
309 tmp = fold_build2 (PLUS_EXPR, gfc_charlen_type_node, end.expr, tmp);
310 tmp = fold_build2 (MAX_EXPR, gfc_charlen_type_node, tmp,
311 build_int_cst (gfc_charlen_type_node, 0));
312 se->string_length = tmp;
313 }
314
315
316 /* Convert a derived type component reference. */
317
318 static void
319 gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
320 {
321 gfc_component *c;
322 tree tmp;
323 tree decl;
324 tree field;
325
326 c = ref->u.c.component;
327
328 gcc_assert (c->backend_decl);
329
330 field = c->backend_decl;
331 gcc_assert (TREE_CODE (field) == FIELD_DECL);
332 decl = se->expr;
333 tmp = build3 (COMPONENT_REF, TREE_TYPE (field), decl, field, NULL_TREE);
334
335 se->expr = tmp;
336
337 if (c->ts.type == BT_CHARACTER)
338 {
339 tmp = c->ts.cl->backend_decl;
340 /* Components must always be constant length. */
341 gcc_assert (tmp && INTEGER_CST_P (tmp));
342 se->string_length = tmp;
343 }
344
345 if (c->pointer && c->dimension == 0 && c->ts.type != BT_CHARACTER)
346 se->expr = build_fold_indirect_ref (se->expr);
347 }
348
349
350 /* Return the contents of a variable. Also handles reference/pointer
351 variables (all Fortran pointer references are implicit). */
352
353 static void
354 gfc_conv_variable (gfc_se * se, gfc_expr * expr)
355 {
356 gfc_ref *ref;
357 gfc_symbol *sym;
358 tree parent_decl;
359 int parent_flag;
360 bool return_value;
361 bool alternate_entry;
362 bool entry_master;
363
364 sym = expr->symtree->n.sym;
365 if (se->ss != NULL)
366 {
367 /* Check that something hasn't gone horribly wrong. */
368 gcc_assert (se->ss != gfc_ss_terminator);
369 gcc_assert (se->ss->expr == expr);
370
371 /* A scalarized term. We already know the descriptor. */
372 se->expr = se->ss->data.info.descriptor;
373 se->string_length = se->ss->string_length;
374 for (ref = se->ss->data.info.ref; ref; ref = ref->next)
375 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
376 break;
377 }
378 else
379 {
380 tree se_expr = NULL_TREE;
381
382 se->expr = gfc_get_symbol_decl (sym);
383
384 /* Deal with references to a parent results or entries by storing
385 the current_function_decl and moving to the parent_decl. */
386 return_value = sym->attr.function && sym->result == sym;
387 alternate_entry = sym->attr.function && sym->attr.entry
388 && sym->result == sym;
389 entry_master = sym->attr.result
390 && sym->ns->proc_name->attr.entry_master
391 && !gfc_return_by_reference (sym->ns->proc_name);
392 parent_decl = DECL_CONTEXT (current_function_decl);
393
394 if ((se->expr == parent_decl && return_value)
395 || (sym->ns && sym->ns->proc_name
396 && parent_decl
397 && sym->ns->proc_name->backend_decl == parent_decl
398 && (alternate_entry || entry_master)))
399 parent_flag = 1;
400 else
401 parent_flag = 0;
402
403 /* Special case for assigning the return value of a function.
404 Self recursive functions must have an explicit return value. */
405 if (return_value && (se->expr == current_function_decl || parent_flag))
406 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
407
408 /* Similarly for alternate entry points. */
409 else if (alternate_entry
410 && (sym->ns->proc_name->backend_decl == current_function_decl
411 || parent_flag))
412 {
413 gfc_entry_list *el = NULL;
414
415 for (el = sym->ns->entries; el; el = el->next)
416 if (sym == el->sym)
417 {
418 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
419 break;
420 }
421 }
422
423 else if (entry_master
424 && (sym->ns->proc_name->backend_decl == current_function_decl
425 || parent_flag))
426 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
427
428 if (se_expr)
429 se->expr = se_expr;
430
431 /* Procedure actual arguments. */
432 else if (sym->attr.flavor == FL_PROCEDURE
433 && se->expr != current_function_decl)
434 {
435 gcc_assert (se->want_pointer);
436 if (!sym->attr.dummy)
437 {
438 gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
439 se->expr = build_fold_addr_expr (se->expr);
440 }
441 return;
442 }
443
444
445 /* Dereference the expression, where needed. Since characters
446 are entirely different from other types, they are treated
447 separately. */
448 if (sym->ts.type == BT_CHARACTER)
449 {
450 /* Dereference character pointer dummy arguments
451 or results. */
452 if ((sym->attr.pointer || sym->attr.allocatable)
453 && (sym->attr.dummy
454 || sym->attr.function
455 || sym->attr.result))
456 se->expr = build_fold_indirect_ref (se->expr);
457 }
458 else
459 {
460 /* Dereference non-character scalar dummy arguments. */
461 if (sym->attr.dummy && !sym->attr.dimension)
462 se->expr = build_fold_indirect_ref (se->expr);
463
464 /* Dereference scalar hidden result. */
465 if (gfc_option.flag_f2c && sym->ts.type == BT_COMPLEX
466 && (sym->attr.function || sym->attr.result)
467 && !sym->attr.dimension && !sym->attr.pointer)
468 se->expr = build_fold_indirect_ref (se->expr);
469
470 /* Dereference non-character pointer variables.
471 These must be dummies, results, or scalars. */
472 if ((sym->attr.pointer || sym->attr.allocatable)
473 && (sym->attr.dummy
474 || sym->attr.function
475 || sym->attr.result
476 || !sym->attr.dimension))
477 se->expr = build_fold_indirect_ref (se->expr);
478 }
479
480 ref = expr->ref;
481 }
482
483 /* For character variables, also get the length. */
484 if (sym->ts.type == BT_CHARACTER)
485 {
486 /* If the character length of an entry isn't set, get the length from
487 the master function instead. */
488 if (sym->attr.entry && !sym->ts.cl->backend_decl)
489 se->string_length = sym->ns->proc_name->ts.cl->backend_decl;
490 else
491 se->string_length = sym->ts.cl->backend_decl;
492 gcc_assert (se->string_length);
493 }
494
495 while (ref)
496 {
497 switch (ref->type)
498 {
499 case REF_ARRAY:
500 /* Return the descriptor if that's what we want and this is an array
501 section reference. */
502 if (se->descriptor_only && ref->u.ar.type != AR_ELEMENT)
503 return;
504 /* TODO: Pointers to single elements of array sections, eg elemental subs. */
505 /* Return the descriptor for array pointers and allocations. */
506 if (se->want_pointer
507 && ref->next == NULL && (se->descriptor_only))
508 return;
509
510 gfc_conv_array_ref (se, &ref->u.ar, sym, &expr->where);
511 /* Return a pointer to an element. */
512 break;
513
514 case REF_COMPONENT:
515 gfc_conv_component_ref (se, ref);
516 break;
517
518 case REF_SUBSTRING:
519 gfc_conv_substring (se, ref, expr->ts.kind,
520 expr->symtree->name, &expr->where);
521 break;
522
523 default:
524 gcc_unreachable ();
525 break;
526 }
527 ref = ref->next;
528 }
529 /* Pointer assignment, allocation or pass by reference. Arrays are handled
530 separately. */
531 if (se->want_pointer)
532 {
533 if (expr->ts.type == BT_CHARACTER)
534 gfc_conv_string_parameter (se);
535 else
536 se->expr = build_fold_addr_expr (se->expr);
537 }
538 }
539
540
541 /* Unary ops are easy... Or they would be if ! was a valid op. */
542
543 static void
544 gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
545 {
546 gfc_se operand;
547 tree type;
548
549 gcc_assert (expr->ts.type != BT_CHARACTER);
550 /* Initialize the operand. */
551 gfc_init_se (&operand, se);
552 gfc_conv_expr_val (&operand, expr->value.op.op1);
553 gfc_add_block_to_block (&se->pre, &operand.pre);
554
555 type = gfc_typenode_for_spec (&expr->ts);
556
557 /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
558 We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
559 All other unary operators have an equivalent GIMPLE unary operator. */
560 if (code == TRUTH_NOT_EXPR)
561 se->expr = build2 (EQ_EXPR, type, operand.expr,
562 build_int_cst (type, 0));
563 else
564 se->expr = build1 (code, type, operand.expr);
565
566 }
567
568 /* Expand power operator to optimal multiplications when a value is raised
569 to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
570 Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
571 Programming", 3rd Edition, 1998. */
572
573 /* This code is mostly duplicated from expand_powi in the backend.
574 We establish the "optimal power tree" lookup table with the defined size.
575 The items in the table are the exponents used to calculate the index
576 exponents. Any integer n less than the value can get an "addition chain",
577 with the first node being one. */
578 #define POWI_TABLE_SIZE 256
579
580 /* The table is from builtins.c. */
581 static const unsigned char powi_table[POWI_TABLE_SIZE] =
582 {
583 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */
584 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */
585 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */
586 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */
587 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */
588 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */
589 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */
590 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */
591 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */
592 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */
593 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */
594 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */
595 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */
596 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */
597 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */
598 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */
599 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */
600 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */
601 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */
602 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */
603 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */
604 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */
605 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */
606 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */
607 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */
608 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */
609 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */
610 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */
611 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */
612 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */
613 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */
614 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */
615 };
616
617 /* If n is larger than lookup table's max index, we use the "window
618 method". */
619 #define POWI_WINDOW_SIZE 3
620
621 /* Recursive function to expand the power operator. The temporary
622 values are put in tmpvar. The function returns tmpvar[1] ** n. */
623 static tree
624 gfc_conv_powi (gfc_se * se, int n, tree * tmpvar)
625 {
626 tree op0;
627 tree op1;
628 tree tmp;
629 int digit;
630
631 if (n < POWI_TABLE_SIZE)
632 {
633 if (tmpvar[n])
634 return tmpvar[n];
635
636 op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
637 op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
638 }
639 else if (n & 1)
640 {
641 digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
642 op0 = gfc_conv_powi (se, n - digit, tmpvar);
643 op1 = gfc_conv_powi (se, digit, tmpvar);
644 }
645 else
646 {
647 op0 = gfc_conv_powi (se, n >> 1, tmpvar);
648 op1 = op0;
649 }
650
651 tmp = fold_build2 (MULT_EXPR, TREE_TYPE (op0), op0, op1);
652 tmp = gfc_evaluate_now (tmp, &se->pre);
653
654 if (n < POWI_TABLE_SIZE)
655 tmpvar[n] = tmp;
656
657 return tmp;
658 }
659
660
661 /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
662 return 1. Else return 0 and a call to runtime library functions
663 will have to be built. */
664 static int
665 gfc_conv_cst_int_power (gfc_se * se, tree lhs, tree rhs)
666 {
667 tree cond;
668 tree tmp;
669 tree type;
670 tree vartmp[POWI_TABLE_SIZE];
671 int n;
672 int sgn;
673
674 type = TREE_TYPE (lhs);
675 n = abs (TREE_INT_CST_LOW (rhs));
676 sgn = tree_int_cst_sgn (rhs);
677
678 if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations) || optimize_size)
679 && (n > 2 || n < -1))
680 return 0;
681
682 /* rhs == 0 */
683 if (sgn == 0)
684 {
685 se->expr = gfc_build_const (type, integer_one_node);
686 return 1;
687 }
688 /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1. */
689 if ((sgn == -1) && (TREE_CODE (type) == INTEGER_TYPE))
690 {
691 tmp = build2 (EQ_EXPR, boolean_type_node, lhs,
692 build_int_cst (TREE_TYPE (lhs), -1));
693 cond = build2 (EQ_EXPR, boolean_type_node, lhs,
694 build_int_cst (TREE_TYPE (lhs), 1));
695
696 /* If rhs is even,
697 result = (lhs == 1 || lhs == -1) ? 1 : 0. */
698 if ((n & 1) == 0)
699 {
700 tmp = build2 (TRUTH_OR_EXPR, boolean_type_node, tmp, cond);
701 se->expr = build3 (COND_EXPR, type, tmp, build_int_cst (type, 1),
702 build_int_cst (type, 0));
703 return 1;
704 }
705 /* If rhs is odd,
706 result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0. */
707 tmp = build3 (COND_EXPR, type, tmp, build_int_cst (type, -1),
708 build_int_cst (type, 0));
709 se->expr = build3 (COND_EXPR, type, cond, build_int_cst (type, 1), tmp);
710 return 1;
711 }
712
713 memset (vartmp, 0, sizeof (vartmp));
714 vartmp[1] = lhs;
715 if (sgn == -1)
716 {
717 tmp = gfc_build_const (type, integer_one_node);
718 vartmp[1] = build2 (RDIV_EXPR, type, tmp, vartmp[1]);
719 }
720
721 se->expr = gfc_conv_powi (se, n, vartmp);
722
723 return 1;
724 }
725
726
727 /* Power op (**). Constant integer exponent has special handling. */
728
729 static void
730 gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
731 {
732 tree gfc_int4_type_node;
733 int kind;
734 int ikind;
735 gfc_se lse;
736 gfc_se rse;
737 tree fndecl;
738 tree tmp;
739
740 gfc_init_se (&lse, se);
741 gfc_conv_expr_val (&lse, expr->value.op.op1);
742 lse.expr = gfc_evaluate_now (lse.expr, &lse.pre);
743 gfc_add_block_to_block (&se->pre, &lse.pre);
744
745 gfc_init_se (&rse, se);
746 gfc_conv_expr_val (&rse, expr->value.op.op2);
747 gfc_add_block_to_block (&se->pre, &rse.pre);
748
749 if (expr->value.op.op2->ts.type == BT_INTEGER
750 && expr->value.op.op2->expr_type == EXPR_CONSTANT)
751 if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
752 return;
753
754 gfc_int4_type_node = gfc_get_int_type (4);
755
756 kind = expr->value.op.op1->ts.kind;
757 switch (expr->value.op.op2->ts.type)
758 {
759 case BT_INTEGER:
760 ikind = expr->value.op.op2->ts.kind;
761 switch (ikind)
762 {
763 case 1:
764 case 2:
765 rse.expr = convert (gfc_int4_type_node, rse.expr);
766 /* Fall through. */
767
768 case 4:
769 ikind = 0;
770 break;
771
772 case 8:
773 ikind = 1;
774 break;
775
776 case 16:
777 ikind = 2;
778 break;
779
780 default:
781 gcc_unreachable ();
782 }
783 switch (kind)
784 {
785 case 1:
786 case 2:
787 if (expr->value.op.op1->ts.type == BT_INTEGER)
788 lse.expr = convert (gfc_int4_type_node, lse.expr);
789 else
790 gcc_unreachable ();
791 /* Fall through. */
792
793 case 4:
794 kind = 0;
795 break;
796
797 case 8:
798 kind = 1;
799 break;
800
801 case 10:
802 kind = 2;
803 break;
804
805 case 16:
806 kind = 3;
807 break;
808
809 default:
810 gcc_unreachable ();
811 }
812
813 switch (expr->value.op.op1->ts.type)
814 {
815 case BT_INTEGER:
816 if (kind == 3) /* Case 16 was not handled properly above. */
817 kind = 2;
818 fndecl = gfor_fndecl_math_powi[kind][ikind].integer;
819 break;
820
821 case BT_REAL:
822 fndecl = gfor_fndecl_math_powi[kind][ikind].real;
823 break;
824
825 case BT_COMPLEX:
826 fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
827 break;
828
829 default:
830 gcc_unreachable ();
831 }
832 break;
833
834 case BT_REAL:
835 switch (kind)
836 {
837 case 4:
838 fndecl = built_in_decls[BUILT_IN_POWF];
839 break;
840 case 8:
841 fndecl = built_in_decls[BUILT_IN_POW];
842 break;
843 case 10:
844 case 16:
845 fndecl = built_in_decls[BUILT_IN_POWL];
846 break;
847 default:
848 gcc_unreachable ();
849 }
850 break;
851
852 case BT_COMPLEX:
853 switch (kind)
854 {
855 case 4:
856 fndecl = gfor_fndecl_math_cpowf;
857 break;
858 case 8:
859 fndecl = gfor_fndecl_math_cpow;
860 break;
861 case 10:
862 fndecl = gfor_fndecl_math_cpowl10;
863 break;
864 case 16:
865 fndecl = gfor_fndecl_math_cpowl16;
866 break;
867 default:
868 gcc_unreachable ();
869 }
870 break;
871
872 default:
873 gcc_unreachable ();
874 break;
875 }
876
877 tmp = gfc_chainon_list (NULL_TREE, lse.expr);
878 tmp = gfc_chainon_list (tmp, rse.expr);
879 se->expr = build_function_call_expr (fndecl, tmp);
880 }
881
882
883 /* Generate code to allocate a string temporary. */
884
885 tree
886 gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
887 {
888 tree var;
889 tree tmp;
890 tree args;
891
892 gcc_assert (TREE_TYPE (len) == gfc_charlen_type_node);
893
894 if (gfc_can_put_var_on_stack (len))
895 {
896 /* Create a temporary variable to hold the result. */
897 tmp = fold_build2 (MINUS_EXPR, gfc_charlen_type_node, len,
898 build_int_cst (gfc_charlen_type_node, 1));
899 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node, tmp);
900 tmp = build_array_type (gfc_character1_type_node, tmp);
901 var = gfc_create_var (tmp, "str");
902 var = gfc_build_addr_expr (type, var);
903 }
904 else
905 {
906 /* Allocate a temporary to hold the result. */
907 var = gfc_create_var (type, "pstr");
908 args = gfc_chainon_list (NULL_TREE, len);
909 tmp = build_function_call_expr (gfor_fndecl_internal_malloc, args);
910 tmp = convert (type, tmp);
911 gfc_add_modify_expr (&se->pre, var, tmp);
912
913 /* Free the temporary afterwards. */
914 tmp = convert (pvoid_type_node, var);
915 args = gfc_chainon_list (NULL_TREE, tmp);
916 tmp = build_function_call_expr (gfor_fndecl_internal_free, args);
917 gfc_add_expr_to_block (&se->post, tmp);
918 }
919
920 return var;
921 }
922
923
924 /* Handle a string concatenation operation. A temporary will be allocated to
925 hold the result. */
926
927 static void
928 gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
929 {
930 gfc_se lse;
931 gfc_se rse;
932 tree len;
933 tree type;
934 tree var;
935 tree args;
936 tree tmp;
937
938 gcc_assert (expr->value.op.op1->ts.type == BT_CHARACTER
939 && expr->value.op.op2->ts.type == BT_CHARACTER);
940
941 gfc_init_se (&lse, se);
942 gfc_conv_expr (&lse, expr->value.op.op1);
943 gfc_conv_string_parameter (&lse);
944 gfc_init_se (&rse, se);
945 gfc_conv_expr (&rse, expr->value.op.op2);
946 gfc_conv_string_parameter (&rse);
947
948 gfc_add_block_to_block (&se->pre, &lse.pre);
949 gfc_add_block_to_block (&se->pre, &rse.pre);
950
951 type = gfc_get_character_type (expr->ts.kind, expr->ts.cl);
952 len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
953 if (len == NULL_TREE)
954 {
955 len = fold_build2 (PLUS_EXPR, TREE_TYPE (lse.string_length),
956 lse.string_length, rse.string_length);
957 }
958
959 type = build_pointer_type (type);
960
961 var = gfc_conv_string_tmp (se, type, len);
962
963 /* Do the actual concatenation. */
964 args = NULL_TREE;
965 args = gfc_chainon_list (args, len);
966 args = gfc_chainon_list (args, var);
967 args = gfc_chainon_list (args, lse.string_length);
968 args = gfc_chainon_list (args, lse.expr);
969 args = gfc_chainon_list (args, rse.string_length);
970 args = gfc_chainon_list (args, rse.expr);
971 tmp = build_function_call_expr (gfor_fndecl_concat_string, args);
972 gfc_add_expr_to_block (&se->pre, tmp);
973
974 /* Add the cleanup for the operands. */
975 gfc_add_block_to_block (&se->pre, &rse.post);
976 gfc_add_block_to_block (&se->pre, &lse.post);
977
978 se->expr = var;
979 se->string_length = len;
980 }
981
982 /* Translates an op expression. Common (binary) cases are handled by this
983 function, others are passed on. Recursion is used in either case.
984 We use the fact that (op1.ts == op2.ts) (except for the power
985 operator **).
986 Operators need no special handling for scalarized expressions as long as
987 they call gfc_conv_simple_val to get their operands.
988 Character strings get special handling. */
989
990 static void
991 gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
992 {
993 enum tree_code code;
994 gfc_se lse;
995 gfc_se rse;
996 tree type;
997 tree tmp;
998 int lop;
999 int checkstring;
1000
1001 checkstring = 0;
1002 lop = 0;
1003 switch (expr->value.op.operator)
1004 {
1005 case INTRINSIC_UPLUS:
1006 case INTRINSIC_PARENTHESES:
1007 gfc_conv_expr (se, expr->value.op.op1);
1008 return;
1009
1010 case INTRINSIC_UMINUS:
1011 gfc_conv_unary_op (NEGATE_EXPR, se, expr);
1012 return;
1013
1014 case INTRINSIC_NOT:
1015 gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
1016 return;
1017
1018 case INTRINSIC_PLUS:
1019 code = PLUS_EXPR;
1020 break;
1021
1022 case INTRINSIC_MINUS:
1023 code = MINUS_EXPR;
1024 break;
1025
1026 case INTRINSIC_TIMES:
1027 code = MULT_EXPR;
1028 break;
1029
1030 case INTRINSIC_DIVIDE:
1031 /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
1032 an integer, we must round towards zero, so we use a
1033 TRUNC_DIV_EXPR. */
1034 if (expr->ts.type == BT_INTEGER)
1035 code = TRUNC_DIV_EXPR;
1036 else
1037 code = RDIV_EXPR;
1038 break;
1039
1040 case INTRINSIC_POWER:
1041 gfc_conv_power_op (se, expr);
1042 return;
1043
1044 case INTRINSIC_CONCAT:
1045 gfc_conv_concat_op (se, expr);
1046 return;
1047
1048 case INTRINSIC_AND:
1049 code = TRUTH_ANDIF_EXPR;
1050 lop = 1;
1051 break;
1052
1053 case INTRINSIC_OR:
1054 code = TRUTH_ORIF_EXPR;
1055 lop = 1;
1056 break;
1057
1058 /* EQV and NEQV only work on logicals, but since we represent them
1059 as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE. */
1060 case INTRINSIC_EQ:
1061 case INTRINSIC_EQV:
1062 code = EQ_EXPR;
1063 checkstring = 1;
1064 lop = 1;
1065 break;
1066
1067 case INTRINSIC_NE:
1068 case INTRINSIC_NEQV:
1069 code = NE_EXPR;
1070 checkstring = 1;
1071 lop = 1;
1072 break;
1073
1074 case INTRINSIC_GT:
1075 code = GT_EXPR;
1076 checkstring = 1;
1077 lop = 1;
1078 break;
1079
1080 case INTRINSIC_GE:
1081 code = GE_EXPR;
1082 checkstring = 1;
1083 lop = 1;
1084 break;
1085
1086 case INTRINSIC_LT:
1087 code = LT_EXPR;
1088 checkstring = 1;
1089 lop = 1;
1090 break;
1091
1092 case INTRINSIC_LE:
1093 code = LE_EXPR;
1094 checkstring = 1;
1095 lop = 1;
1096 break;
1097
1098 case INTRINSIC_USER:
1099 case INTRINSIC_ASSIGN:
1100 /* These should be converted into function calls by the frontend. */
1101 gcc_unreachable ();
1102
1103 default:
1104 fatal_error ("Unknown intrinsic op");
1105 return;
1106 }
1107
1108 /* The only exception to this is **, which is handled separately anyway. */
1109 gcc_assert (expr->value.op.op1->ts.type == expr->value.op.op2->ts.type);
1110
1111 if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
1112 checkstring = 0;
1113
1114 /* lhs */
1115 gfc_init_se (&lse, se);
1116 gfc_conv_expr (&lse, expr->value.op.op1);
1117 gfc_add_block_to_block (&se->pre, &lse.pre);
1118
1119 /* rhs */
1120 gfc_init_se (&rse, se);
1121 gfc_conv_expr (&rse, expr->value.op.op2);
1122 gfc_add_block_to_block (&se->pre, &rse.pre);
1123
1124 if (checkstring)
1125 {
1126 gfc_conv_string_parameter (&lse);
1127 gfc_conv_string_parameter (&rse);
1128
1129 lse.expr = gfc_build_compare_string (lse.string_length, lse.expr,
1130 rse.string_length, rse.expr);
1131 rse.expr = integer_zero_node;
1132 gfc_add_block_to_block (&lse.post, &rse.post);
1133 }
1134
1135 type = gfc_typenode_for_spec (&expr->ts);
1136
1137 if (lop)
1138 {
1139 /* The result of logical ops is always boolean_type_node. */
1140 tmp = fold_build2 (code, type, lse.expr, rse.expr);
1141 se->expr = convert (type, tmp);
1142 }
1143 else
1144 se->expr = fold_build2 (code, type, lse.expr, rse.expr);
1145
1146 /* Add the post blocks. */
1147 gfc_add_block_to_block (&se->post, &rse.post);
1148 gfc_add_block_to_block (&se->post, &lse.post);
1149 }
1150
1151 /* If a string's length is one, we convert it to a single character. */
1152
1153 static tree
1154 gfc_to_single_character (tree len, tree str)
1155 {
1156 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str)));
1157
1158 if (INTEGER_CST_P (len) && TREE_INT_CST_LOW (len) == 1
1159 && TREE_INT_CST_HIGH (len) == 0)
1160 {
1161 str = fold_convert (pchar_type_node, str);
1162 return build_fold_indirect_ref (str);
1163 }
1164
1165 return NULL_TREE;
1166 }
1167
1168 /* Compare two strings. If they are all single characters, the result is the
1169 subtraction of them. Otherwise, we build a library call. */
1170
1171 tree
1172 gfc_build_compare_string (tree len1, tree str1, tree len2, tree str2)
1173 {
1174 tree sc1;
1175 tree sc2;
1176 tree type;
1177 tree tmp;
1178
1179 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1)));
1180 gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2)));
1181
1182 type = gfc_get_int_type (gfc_default_integer_kind);
1183
1184 sc1 = gfc_to_single_character (len1, str1);
1185 sc2 = gfc_to_single_character (len2, str2);
1186
1187 /* Deal with single character specially. */
1188 if (sc1 != NULL_TREE && sc2 != NULL_TREE)
1189 {
1190 sc1 = fold_convert (type, sc1);
1191 sc2 = fold_convert (type, sc2);
1192 tmp = fold_build2 (MINUS_EXPR, type, sc1, sc2);
1193 }
1194 else
1195 {
1196 tmp = NULL_TREE;
1197 tmp = gfc_chainon_list (tmp, len1);
1198 tmp = gfc_chainon_list (tmp, str1);
1199 tmp = gfc_chainon_list (tmp, len2);
1200 tmp = gfc_chainon_list (tmp, str2);
1201
1202 /* Build a call for the comparison. */
1203 tmp = build_function_call_expr (gfor_fndecl_compare_string, tmp);
1204 }
1205
1206 return tmp;
1207 }
1208
1209 static void
1210 gfc_conv_function_val (gfc_se * se, gfc_symbol * sym)
1211 {
1212 tree tmp;
1213
1214 if (sym->attr.dummy)
1215 {
1216 tmp = gfc_get_symbol_decl (sym);
1217 gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE
1218 && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) == FUNCTION_TYPE);
1219 }
1220 else
1221 {
1222 if (!sym->backend_decl)
1223 sym->backend_decl = gfc_get_extern_function_decl (sym);
1224
1225 tmp = sym->backend_decl;
1226 if (sym->attr.cray_pointee)
1227 tmp = convert (build_pointer_type (TREE_TYPE (tmp)),
1228 gfc_get_symbol_decl (sym->cp_pointer));
1229 if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
1230 {
1231 gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
1232 tmp = build_fold_addr_expr (tmp);
1233 }
1234 }
1235 se->expr = tmp;
1236 }
1237
1238
1239 /* Initialize MAPPING. */
1240
1241 void
1242 gfc_init_interface_mapping (gfc_interface_mapping * mapping)
1243 {
1244 mapping->syms = NULL;
1245 mapping->charlens = NULL;
1246 }
1247
1248
1249 /* Free all memory held by MAPPING (but not MAPPING itself). */
1250
1251 void
1252 gfc_free_interface_mapping (gfc_interface_mapping * mapping)
1253 {
1254 gfc_interface_sym_mapping *sym;
1255 gfc_interface_sym_mapping *nextsym;
1256 gfc_charlen *cl;
1257 gfc_charlen *nextcl;
1258
1259 for (sym = mapping->syms; sym; sym = nextsym)
1260 {
1261 nextsym = sym->next;
1262 gfc_free_symbol (sym->new->n.sym);
1263 gfc_free (sym->new);
1264 gfc_free (sym);
1265 }
1266 for (cl = mapping->charlens; cl; cl = nextcl)
1267 {
1268 nextcl = cl->next;
1269 gfc_free_expr (cl->length);
1270 gfc_free (cl);
1271 }
1272 }
1273
1274
1275 /* Return a copy of gfc_charlen CL. Add the returned structure to
1276 MAPPING so that it will be freed by gfc_free_interface_mapping. */
1277
1278 static gfc_charlen *
1279 gfc_get_interface_mapping_charlen (gfc_interface_mapping * mapping,
1280 gfc_charlen * cl)
1281 {
1282 gfc_charlen *new;
1283
1284 new = gfc_get_charlen ();
1285 new->next = mapping->charlens;
1286 new->length = gfc_copy_expr (cl->length);
1287
1288 mapping->charlens = new;
1289 return new;
1290 }
1291
1292
1293 /* A subroutine of gfc_add_interface_mapping. Return a descriptorless
1294 array variable that can be used as the actual argument for dummy
1295 argument SYM. Add any initialization code to BLOCK. PACKED is as
1296 for gfc_get_nodesc_array_type and DATA points to the first element
1297 in the passed array. */
1298
1299 static tree
1300 gfc_get_interface_mapping_array (stmtblock_t * block, gfc_symbol * sym,
1301 int packed, tree data)
1302 {
1303 tree type;
1304 tree var;
1305
1306 type = gfc_typenode_for_spec (&sym->ts);
1307 type = gfc_get_nodesc_array_type (type, sym->as, packed);
1308
1309 var = gfc_create_var (type, "ifm");
1310 gfc_add_modify_expr (block, var, fold_convert (type, data));
1311
1312 return var;
1313 }
1314
1315
1316 /* A subroutine of gfc_add_interface_mapping. Set the stride, upper bounds
1317 and offset of descriptorless array type TYPE given that it has the same
1318 size as DESC. Add any set-up code to BLOCK. */
1319
1320 static void
1321 gfc_set_interface_mapping_bounds (stmtblock_t * block, tree type, tree desc)
1322 {
1323 int n;
1324 tree dim;
1325 tree offset;
1326 tree tmp;
1327
1328 offset = gfc_index_zero_node;
1329 for (n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
1330 {
1331 dim = gfc_rank_cst[n];
1332 GFC_TYPE_ARRAY_STRIDE (type, n) = gfc_conv_array_stride (desc, n);
1333 if (GFC_TYPE_ARRAY_LBOUND (type, n) == NULL_TREE)
1334 {
1335 GFC_TYPE_ARRAY_LBOUND (type, n)
1336 = gfc_conv_descriptor_lbound (desc, dim);
1337 GFC_TYPE_ARRAY_UBOUND (type, n)
1338 = gfc_conv_descriptor_ubound (desc, dim);
1339 }
1340 else if (GFC_TYPE_ARRAY_UBOUND (type, n) == NULL_TREE)
1341 {
1342 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
1343 gfc_conv_descriptor_ubound (desc, dim),
1344 gfc_conv_descriptor_lbound (desc, dim));
1345 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1346 GFC_TYPE_ARRAY_LBOUND (type, n),
1347 tmp);
1348 tmp = gfc_evaluate_now (tmp, block);
1349 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1350 }
1351 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type,
1352 GFC_TYPE_ARRAY_LBOUND (type, n),
1353 GFC_TYPE_ARRAY_STRIDE (type, n));
1354 offset = fold_build2 (MINUS_EXPR, gfc_array_index_type, offset, tmp);
1355 }
1356 offset = gfc_evaluate_now (offset, block);
1357 GFC_TYPE_ARRAY_OFFSET (type) = offset;
1358 }
1359
1360
1361 /* Extend MAPPING so that it maps dummy argument SYM to the value stored
1362 in SE. The caller may still use se->expr and se->string_length after
1363 calling this function. */
1364
1365 void
1366 gfc_add_interface_mapping (gfc_interface_mapping * mapping,
1367 gfc_symbol * sym, gfc_se * se)
1368 {
1369 gfc_interface_sym_mapping *sm;
1370 tree desc;
1371 tree tmp;
1372 tree value;
1373 gfc_symbol *new_sym;
1374 gfc_symtree *root;
1375 gfc_symtree *new_symtree;
1376
1377 /* Create a new symbol to represent the actual argument. */
1378 new_sym = gfc_new_symbol (sym->name, NULL);
1379 new_sym->ts = sym->ts;
1380 new_sym->attr.referenced = 1;
1381 new_sym->attr.dimension = sym->attr.dimension;
1382 new_sym->attr.pointer = sym->attr.pointer;
1383 new_sym->attr.allocatable = sym->attr.allocatable;
1384 new_sym->attr.flavor = sym->attr.flavor;
1385
1386 /* Create a fake symtree for it. */
1387 root = NULL;
1388 new_symtree = gfc_new_symtree (&root, sym->name);
1389 new_symtree->n.sym = new_sym;
1390 gcc_assert (new_symtree == root);
1391
1392 /* Create a dummy->actual mapping. */
1393 sm = gfc_getmem (sizeof (*sm));
1394 sm->next = mapping->syms;
1395 sm->old = sym;
1396 sm->new = new_symtree;
1397 mapping->syms = sm;
1398
1399 /* Stabilize the argument's value. */
1400 se->expr = gfc_evaluate_now (se->expr, &se->pre);
1401
1402 if (sym->ts.type == BT_CHARACTER)
1403 {
1404 /* Create a copy of the dummy argument's length. */
1405 new_sym->ts.cl = gfc_get_interface_mapping_charlen (mapping, sym->ts.cl);
1406
1407 /* If the length is specified as "*", record the length that
1408 the caller is passing. We should use the callee's length
1409 in all other cases. */
1410 if (!new_sym->ts.cl->length)
1411 {
1412 se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
1413 new_sym->ts.cl->backend_decl = se->string_length;
1414 }
1415 }
1416
1417 /* Use the passed value as-is if the argument is a function. */
1418 if (sym->attr.flavor == FL_PROCEDURE)
1419 value = se->expr;
1420
1421 /* If the argument is either a string or a pointer to a string,
1422 convert it to a boundless character type. */
1423 else if (!sym->attr.dimension && sym->ts.type == BT_CHARACTER)
1424 {
1425 tmp = gfc_get_character_type_len (sym->ts.kind, NULL);
1426 tmp = build_pointer_type (tmp);
1427 if (sym->attr.pointer)
1428 value = build_fold_indirect_ref (se->expr);
1429 else
1430 value = se->expr;
1431 value = fold_convert (tmp, value);
1432 }
1433
1434 /* If the argument is a scalar, a pointer to an array or an allocatable,
1435 dereference it. */
1436 else if (!sym->attr.dimension || sym->attr.pointer || sym->attr.allocatable)
1437 value = build_fold_indirect_ref (se->expr);
1438
1439 /* For character(*), use the actual argument's descriptor. */
1440 else if (sym->ts.type == BT_CHARACTER && !new_sym->ts.cl->length)
1441 value = build_fold_indirect_ref (se->expr);
1442
1443 /* If the argument is an array descriptor, use it to determine
1444 information about the actual argument's shape. */
1445 else if (POINTER_TYPE_P (TREE_TYPE (se->expr))
1446 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
1447 {
1448 /* Get the actual argument's descriptor. */
1449 desc = build_fold_indirect_ref (se->expr);
1450
1451 /* Create the replacement variable. */
1452 tmp = gfc_conv_descriptor_data_get (desc);
1453 value = gfc_get_interface_mapping_array (&se->pre, sym, 0, tmp);
1454
1455 /* Use DESC to work out the upper bounds, strides and offset. */
1456 gfc_set_interface_mapping_bounds (&se->pre, TREE_TYPE (value), desc);
1457 }
1458 else
1459 /* Otherwise we have a packed array. */
1460 value = gfc_get_interface_mapping_array (&se->pre, sym, 2, se->expr);
1461
1462 new_sym->backend_decl = value;
1463 }
1464
1465
1466 /* Called once all dummy argument mappings have been added to MAPPING,
1467 but before the mapping is used to evaluate expressions. Pre-evaluate
1468 the length of each argument, adding any initialization code to PRE and
1469 any finalization code to POST. */
1470
1471 void
1472 gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
1473 stmtblock_t * pre, stmtblock_t * post)
1474 {
1475 gfc_interface_sym_mapping *sym;
1476 gfc_expr *expr;
1477 gfc_se se;
1478
1479 for (sym = mapping->syms; sym; sym = sym->next)
1480 if (sym->new->n.sym->ts.type == BT_CHARACTER
1481 && !sym->new->n.sym->ts.cl->backend_decl)
1482 {
1483 expr = sym->new->n.sym->ts.cl->length;
1484 gfc_apply_interface_mapping_to_expr (mapping, expr);
1485 gfc_init_se (&se, NULL);
1486 gfc_conv_expr (&se, expr);
1487
1488 se.expr = gfc_evaluate_now (se.expr, &se.pre);
1489 gfc_add_block_to_block (pre, &se.pre);
1490 gfc_add_block_to_block (post, &se.post);
1491
1492 sym->new->n.sym->ts.cl->backend_decl = se.expr;
1493 }
1494 }
1495
1496
1497 /* Like gfc_apply_interface_mapping_to_expr, but applied to
1498 constructor C. */
1499
1500 static void
1501 gfc_apply_interface_mapping_to_cons (gfc_interface_mapping * mapping,
1502 gfc_constructor * c)
1503 {
1504 for (; c; c = c->next)
1505 {
1506 gfc_apply_interface_mapping_to_expr (mapping, c->expr);
1507 if (c->iterator)
1508 {
1509 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->start);
1510 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->end);
1511 gfc_apply_interface_mapping_to_expr (mapping, c->iterator->step);
1512 }
1513 }
1514 }
1515
1516
1517 /* Like gfc_apply_interface_mapping_to_expr, but applied to
1518 reference REF. */
1519
1520 static void
1521 gfc_apply_interface_mapping_to_ref (gfc_interface_mapping * mapping,
1522 gfc_ref * ref)
1523 {
1524 int n;
1525
1526 for (; ref; ref = ref->next)
1527 switch (ref->type)
1528 {
1529 case REF_ARRAY:
1530 for (n = 0; n < ref->u.ar.dimen; n++)
1531 {
1532 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.start[n]);
1533 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.end[n]);
1534 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.stride[n]);
1535 }
1536 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.offset);
1537 break;
1538
1539 case REF_COMPONENT:
1540 break;
1541
1542 case REF_SUBSTRING:
1543 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.start);
1544 gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.end);
1545 break;
1546 }
1547 }
1548
1549
1550 /* EXPR is a copy of an expression that appeared in the interface
1551 associated with MAPPING. Walk it recursively looking for references to
1552 dummy arguments that MAPPING maps to actual arguments. Replace each such
1553 reference with a reference to the associated actual argument. */
1554
1555 static void
1556 gfc_apply_interface_mapping_to_expr (gfc_interface_mapping * mapping,
1557 gfc_expr * expr)
1558 {
1559 gfc_interface_sym_mapping *sym;
1560 gfc_actual_arglist *actual;
1561
1562 if (!expr)
1563 return;
1564
1565 /* Copying an expression does not copy its length, so do that here. */
1566 if (expr->ts.type == BT_CHARACTER && expr->ts.cl)
1567 {
1568 expr->ts.cl = gfc_get_interface_mapping_charlen (mapping, expr->ts.cl);
1569 gfc_apply_interface_mapping_to_expr (mapping, expr->ts.cl->length);
1570 }
1571
1572 /* Apply the mapping to any references. */
1573 gfc_apply_interface_mapping_to_ref (mapping, expr->ref);
1574
1575 /* ...and to the expression's symbol, if it has one. */
1576 if (expr->symtree)
1577 for (sym = mapping->syms; sym; sym = sym->next)
1578 if (sym->old == expr->symtree->n.sym)
1579 expr->symtree = sym->new;
1580
1581 /* ...and to subexpressions in expr->value. */
1582 switch (expr->expr_type)
1583 {
1584 case EXPR_VARIABLE:
1585 case EXPR_CONSTANT:
1586 case EXPR_NULL:
1587 case EXPR_SUBSTRING:
1588 break;
1589
1590 case EXPR_OP:
1591 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op1);
1592 gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op2);
1593 break;
1594
1595 case EXPR_FUNCTION:
1596 for (sym = mapping->syms; sym; sym = sym->next)
1597 if (sym->old == expr->value.function.esym)
1598 expr->value.function.esym = sym->new->n.sym;
1599
1600 for (actual = expr->value.function.actual; actual; actual = actual->next)
1601 gfc_apply_interface_mapping_to_expr (mapping, actual->expr);
1602 break;
1603
1604 case EXPR_ARRAY:
1605 case EXPR_STRUCTURE:
1606 gfc_apply_interface_mapping_to_cons (mapping, expr->value.constructor);
1607 break;
1608 }
1609 }
1610
1611
1612 /* Evaluate interface expression EXPR using MAPPING. Store the result
1613 in SE. */
1614
1615 void
1616 gfc_apply_interface_mapping (gfc_interface_mapping * mapping,
1617 gfc_se * se, gfc_expr * expr)
1618 {
1619 expr = gfc_copy_expr (expr);
1620 gfc_apply_interface_mapping_to_expr (mapping, expr);
1621 gfc_conv_expr (se, expr);
1622 se->expr = gfc_evaluate_now (se->expr, &se->pre);
1623 gfc_free_expr (expr);
1624 }
1625
1626 /* Returns a reference to a temporary array into which a component of
1627 an actual argument derived type array is copied and then returned
1628 after the function call.
1629 TODO Get rid of this kludge, when array descriptors are capable of
1630 handling aliased arrays. */
1631
1632 static void
1633 gfc_conv_aliased_arg (gfc_se * parmse, gfc_expr * expr,
1634 int g77, sym_intent intent)
1635 {
1636 gfc_se lse;
1637 gfc_se rse;
1638 gfc_ss *lss;
1639 gfc_ss *rss;
1640 gfc_loopinfo loop;
1641 gfc_loopinfo loop2;
1642 gfc_ss_info *info;
1643 tree offset;
1644 tree tmp_index;
1645 tree tmp;
1646 tree base_type;
1647 stmtblock_t body;
1648 int n;
1649
1650 gcc_assert (expr->expr_type == EXPR_VARIABLE);
1651
1652 gfc_init_se (&lse, NULL);
1653 gfc_init_se (&rse, NULL);
1654
1655 /* Walk the argument expression. */
1656 rss = gfc_walk_expr (expr);
1657
1658 gcc_assert (rss != gfc_ss_terminator);
1659
1660 /* Initialize the scalarizer. */
1661 gfc_init_loopinfo (&loop);
1662 gfc_add_ss_to_loop (&loop, rss);
1663
1664 /* Calculate the bounds of the scalarization. */
1665 gfc_conv_ss_startstride (&loop);
1666
1667 /* Build an ss for the temporary. */
1668 base_type = gfc_typenode_for_spec (&expr->ts);
1669 if (GFC_ARRAY_TYPE_P (base_type)
1670 || GFC_DESCRIPTOR_TYPE_P (base_type))
1671 base_type = gfc_get_element_type (base_type);
1672
1673 loop.temp_ss = gfc_get_ss ();;
1674 loop.temp_ss->type = GFC_SS_TEMP;
1675 loop.temp_ss->data.temp.type = base_type;
1676
1677 if (expr->ts.type == BT_CHARACTER)
1678 {
1679 gfc_ref *char_ref = expr->ref;
1680
1681 for (; expr->ts.cl == NULL && char_ref; char_ref = char_ref->next)
1682 if (char_ref->type == REF_SUBSTRING)
1683 {
1684 gfc_se tmp_se;
1685
1686 expr->ts.cl = gfc_get_charlen ();
1687 expr->ts.cl->next = char_ref->u.ss.length->next;
1688 char_ref->u.ss.length->next = expr->ts.cl;
1689
1690 gfc_init_se (&tmp_se, NULL);
1691 gfc_conv_expr_type (&tmp_se, char_ref->u.ss.end,
1692 gfc_array_index_type);
1693 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1694 tmp_se.expr, gfc_index_one_node);
1695 tmp = gfc_evaluate_now (tmp, &parmse->pre);
1696 gfc_init_se (&tmp_se, NULL);
1697 gfc_conv_expr_type (&tmp_se, char_ref->u.ss.start,
1698 gfc_array_index_type);
1699 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
1700 tmp, tmp_se.expr);
1701 expr->ts.cl->backend_decl = tmp;
1702
1703 break;
1704 }
1705 loop.temp_ss->data.temp.type
1706 = gfc_typenode_for_spec (&expr->ts);
1707 loop.temp_ss->string_length = expr->ts.cl->backend_decl;
1708 }
1709
1710 loop.temp_ss->data.temp.dimen = loop.dimen;
1711 loop.temp_ss->next = gfc_ss_terminator;
1712
1713 /* Associate the SS with the loop. */
1714 gfc_add_ss_to_loop (&loop, loop.temp_ss);
1715
1716 /* Setup the scalarizing loops. */
1717 gfc_conv_loop_setup (&loop);
1718
1719 /* Pass the temporary descriptor back to the caller. */
1720 info = &loop.temp_ss->data.info;
1721 parmse->expr = info->descriptor;
1722
1723 /* Setup the gfc_se structures. */
1724 gfc_copy_loopinfo_to_se (&lse, &loop);
1725 gfc_copy_loopinfo_to_se (&rse, &loop);
1726
1727 rse.ss = rss;
1728 lse.ss = loop.temp_ss;
1729 gfc_mark_ss_chain_used (rss, 1);
1730 gfc_mark_ss_chain_used (loop.temp_ss, 1);
1731
1732 /* Start the scalarized loop body. */
1733 gfc_start_scalarized_body (&loop, &body);
1734
1735 /* Translate the expression. */
1736 gfc_conv_expr (&rse, expr);
1737
1738 gfc_conv_tmp_array_ref (&lse);
1739 gfc_advance_se_ss_chain (&lse);
1740
1741 if (intent != INTENT_OUT)
1742 {
1743 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, true, false);
1744 gfc_add_expr_to_block (&body, tmp);
1745 gcc_assert (rse.ss == gfc_ss_terminator);
1746 gfc_trans_scalarizing_loops (&loop, &body);
1747 }
1748 else
1749 {
1750 /* Make sure that the temporary declaration survives by merging
1751 all the loop declarations into the current context. */
1752 for (n = 0; n < loop.dimen; n++)
1753 {
1754 gfc_merge_block_scope (&body);
1755 body = loop.code[loop.order[n]];
1756 }
1757 gfc_merge_block_scope (&body);
1758 }
1759
1760 /* Add the post block after the second loop, so that any
1761 freeing of allocated memory is done at the right time. */
1762 gfc_add_block_to_block (&parmse->pre, &loop.pre);
1763
1764 /**********Copy the temporary back again.*********/
1765
1766 gfc_init_se (&lse, NULL);
1767 gfc_init_se (&rse, NULL);
1768
1769 /* Walk the argument expression. */
1770 lss = gfc_walk_expr (expr);
1771 rse.ss = loop.temp_ss;
1772 lse.ss = lss;
1773
1774 /* Initialize the scalarizer. */
1775 gfc_init_loopinfo (&loop2);
1776 gfc_add_ss_to_loop (&loop2, lss);
1777
1778 /* Calculate the bounds of the scalarization. */
1779 gfc_conv_ss_startstride (&loop2);
1780
1781 /* Setup the scalarizing loops. */
1782 gfc_conv_loop_setup (&loop2);
1783
1784 gfc_copy_loopinfo_to_se (&lse, &loop2);
1785 gfc_copy_loopinfo_to_se (&rse, &loop2);
1786
1787 gfc_mark_ss_chain_used (lss, 1);
1788 gfc_mark_ss_chain_used (loop.temp_ss, 1);
1789
1790 /* Declare the variable to hold the temporary offset and start the
1791 scalarized loop body. */
1792 offset = gfc_create_var (gfc_array_index_type, NULL);
1793 gfc_start_scalarized_body (&loop2, &body);
1794
1795 /* Build the offsets for the temporary from the loop variables. The
1796 temporary array has lbounds of zero and strides of one in all
1797 dimensions, so this is very simple. The offset is only computed
1798 outside the innermost loop, so the overall transfer could be
1799 optimized further. */
1800 info = &rse.ss->data.info;
1801
1802 tmp_index = gfc_index_zero_node;
1803 for (n = info->dimen - 1; n > 0; n--)
1804 {
1805 tree tmp_str;
1806 tmp = rse.loop->loopvar[n];
1807 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type,
1808 tmp, rse.loop->from[n]);
1809 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1810 tmp, tmp_index);
1811
1812 tmp_str = fold_build2 (MINUS_EXPR, gfc_array_index_type,
1813 rse.loop->to[n-1], rse.loop->from[n-1]);
1814 tmp_str = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1815 tmp_str, gfc_index_one_node);
1816
1817 tmp_index = fold_build2 (MULT_EXPR, gfc_array_index_type,
1818 tmp, tmp_str);
1819 }
1820
1821 tmp_index = fold_build2 (MINUS_EXPR, gfc_array_index_type,
1822 tmp_index, rse.loop->from[0]);
1823 gfc_add_modify_expr (&rse.loop->code[0], offset, tmp_index);
1824
1825 tmp_index = fold_build2 (PLUS_EXPR, gfc_array_index_type,
1826 rse.loop->loopvar[0], offset);
1827
1828 /* Now use the offset for the reference. */
1829 tmp = build_fold_indirect_ref (info->data);
1830 rse.expr = gfc_build_array_ref (tmp, tmp_index);
1831
1832 if (expr->ts.type == BT_CHARACTER)
1833 rse.string_length = expr->ts.cl->backend_decl;
1834
1835 gfc_conv_expr (&lse, expr);
1836
1837 gcc_assert (lse.ss == gfc_ss_terminator);
1838
1839 tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, false);
1840 gfc_add_expr_to_block (&body, tmp);
1841
1842 /* Generate the copying loops. */
1843 gfc_trans_scalarizing_loops (&loop2, &body);
1844
1845 /* Wrap the whole thing up by adding the second loop to the post-block
1846 and following it by the post-block of the first loop. In this way,
1847 if the temporary needs freeing, it is done after use! */
1848 if (intent != INTENT_IN)
1849 {
1850 gfc_add_block_to_block (&parmse->post, &loop2.pre);
1851 gfc_add_block_to_block (&parmse->post, &loop2.post);
1852 }
1853
1854 gfc_add_block_to_block (&parmse->post, &loop.post);
1855
1856 gfc_cleanup_loop (&loop);
1857 gfc_cleanup_loop (&loop2);
1858
1859 /* Pass the string length to the argument expression. */
1860 if (expr->ts.type == BT_CHARACTER)
1861 parmse->string_length = expr->ts.cl->backend_decl;
1862
1863 /* We want either the address for the data or the address of the descriptor,
1864 depending on the mode of passing array arguments. */
1865 if (g77)
1866 parmse->expr = gfc_conv_descriptor_data_get (parmse->expr);
1867 else
1868 parmse->expr = build_fold_addr_expr (parmse->expr);
1869
1870 return;
1871 }
1872
1873 /* Is true if an array reference is followed by a component or substring
1874 reference. */
1875
1876 static bool
1877 is_aliased_array (gfc_expr * e)
1878 {
1879 gfc_ref * ref;
1880 bool seen_array;
1881
1882 seen_array = false;
1883 for (ref = e->ref; ref; ref = ref->next)
1884 {
1885 if (ref->type == REF_ARRAY
1886 && ref->u.ar.type != AR_ELEMENT)
1887 seen_array = true;
1888
1889 if (seen_array
1890 && ref->type != REF_ARRAY)
1891 return seen_array;
1892 }
1893 return false;
1894 }
1895
1896 /* Generate code for a procedure call. Note can return se->post != NULL.
1897 If se->direct_byref is set then se->expr contains the return parameter.
1898 Return nonzero, if the call has alternate specifiers. */
1899
1900 int
1901 gfc_conv_function_call (gfc_se * se, gfc_symbol * sym,
1902 gfc_actual_arglist * arg, tree append_args)
1903 {
1904 gfc_interface_mapping mapping;
1905 tree arglist;
1906 tree retargs;
1907 tree tmp;
1908 tree fntype;
1909 gfc_se parmse;
1910 gfc_ss *argss;
1911 gfc_ss_info *info;
1912 int byref;
1913 int parm_kind;
1914 tree type;
1915 tree var;
1916 tree len;
1917 tree stringargs;
1918 gfc_formal_arglist *formal;
1919 int has_alternate_specifier = 0;
1920 bool need_interface_mapping;
1921 bool callee_alloc;
1922 gfc_typespec ts;
1923 gfc_charlen cl;
1924 gfc_expr *e;
1925 gfc_symbol *fsym;
1926 stmtblock_t post;
1927 enum {MISSING = 0, ELEMENTAL, SCALAR, SCALAR_POINTER, ARRAY};
1928
1929 arglist = NULL_TREE;
1930 retargs = NULL_TREE;
1931 stringargs = NULL_TREE;
1932 var = NULL_TREE;
1933 len = NULL_TREE;
1934
1935 if (se->ss != NULL)
1936 {
1937 if (!sym->attr.elemental)
1938 {
1939 gcc_assert (se->ss->type == GFC_SS_FUNCTION);
1940 if (se->ss->useflags)
1941 {
1942 gcc_assert (gfc_return_by_reference (sym)
1943 && sym->result->attr.dimension);
1944 gcc_assert (se->loop != NULL);
1945
1946 /* Access the previously obtained result. */
1947 gfc_conv_tmp_array_ref (se);
1948 gfc_advance_se_ss_chain (se);
1949 return 0;
1950 }
1951 }
1952 info = &se->ss->data.info;
1953 }
1954 else
1955 info = NULL;
1956
1957 gfc_init_block (&post);
1958 gfc_init_interface_mapping (&mapping);
1959 need_interface_mapping = ((sym->ts.type == BT_CHARACTER
1960 && sym->ts.cl->length
1961 && sym->ts.cl->length->expr_type
1962 != EXPR_CONSTANT)
1963 || sym->attr.dimension);
1964 formal = sym->formal;
1965 /* Evaluate the arguments. */
1966 for (; arg != NULL; arg = arg->next, formal = formal ? formal->next : NULL)
1967 {
1968 e = arg->expr;
1969 fsym = formal ? formal->sym : NULL;
1970 parm_kind = MISSING;
1971 if (e == NULL)
1972 {
1973
1974 if (se->ignore_optional)
1975 {
1976 /* Some intrinsics have already been resolved to the correct
1977 parameters. */
1978 continue;
1979 }
1980 else if (arg->label)
1981 {
1982 has_alternate_specifier = 1;
1983 continue;
1984 }
1985 else
1986 {
1987 /* Pass a NULL pointer for an absent arg. */
1988 gfc_init_se (&parmse, NULL);
1989 parmse.expr = null_pointer_node;
1990 if (arg->missing_arg_type == BT_CHARACTER)
1991 parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
1992 }
1993 }
1994 else if (se->ss && se->ss->useflags)
1995 {
1996 /* An elemental function inside a scalarized loop. */
1997 gfc_init_se (&parmse, se);
1998 gfc_conv_expr_reference (&parmse, e);
1999 parm_kind = ELEMENTAL;
2000 }
2001 else
2002 {
2003 /* A scalar or transformational function. */
2004 gfc_init_se (&parmse, NULL);
2005 argss = gfc_walk_expr (e);
2006
2007 if (argss == gfc_ss_terminator)
2008 {
2009 gfc_conv_expr_reference (&parmse, e);
2010 parm_kind = SCALAR;
2011 if (fsym && fsym->attr.pointer
2012 && e->expr_type != EXPR_NULL)
2013 {
2014 /* Scalar pointer dummy args require an extra level of
2015 indirection. The null pointer already contains
2016 this level of indirection. */
2017 parm_kind = SCALAR_POINTER;
2018 parmse.expr = build_fold_addr_expr (parmse.expr);
2019 }
2020 }
2021 else
2022 {
2023 /* If the procedure requires an explicit interface, the actual
2024 argument is passed according to the corresponding formal
2025 argument. If the corresponding formal argument is a POINTER,
2026 ALLOCATABLE or assumed shape, we do not use g77's calling
2027 convention, and pass the address of the array descriptor
2028 instead. Otherwise we use g77's calling convention. */
2029 int f;
2030 f = (fsym != NULL)
2031 && !(fsym->attr.pointer || fsym->attr.allocatable)
2032 && fsym->as->type != AS_ASSUMED_SHAPE;
2033 f = f || !sym->attr.always_explicit;
2034
2035 if (e->expr_type == EXPR_VARIABLE
2036 && is_aliased_array (e))
2037 /* The actual argument is a component reference to an
2038 array of derived types. In this case, the argument
2039 is converted to a temporary, which is passed and then
2040 written back after the procedure call. */
2041 gfc_conv_aliased_arg (&parmse, e, f,
2042 fsym ? fsym->attr.intent : INTENT_INOUT);
2043 else
2044 gfc_conv_array_parameter (&parmse, e, argss, f);
2045
2046 /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
2047 allocated on entry, it must be deallocated. */
2048 if (fsym && fsym->attr.allocatable
2049 && fsym->attr.intent == INTENT_OUT)
2050 {
2051 tmp = e->symtree->n.sym->backend_decl;
2052 if (e->symtree->n.sym->attr.dummy)
2053 tmp = build_fold_indirect_ref (tmp);
2054 tmp = gfc_trans_dealloc_allocated (tmp);
2055 gfc_add_expr_to_block (&se->pre, tmp);
2056 }
2057
2058 }
2059 }
2060
2061 if (fsym)
2062 {
2063 if (e)
2064 {
2065 /* If an optional argument is itself an optional dummy
2066 argument, check its presence and substitute a null
2067 if absent. */
2068 if (e->expr_type == EXPR_VARIABLE
2069 && e->symtree->n.sym->attr.optional
2070 && fsym->attr.optional)
2071 gfc_conv_missing_dummy (&parmse, e, fsym->ts);
2072
2073 /* If an INTENT(OUT) dummy of derived type has a default
2074 initializer, it must be (re)initialized here. */
2075 if (fsym->attr.intent == INTENT_OUT
2076 && fsym->ts.type == BT_DERIVED
2077 && fsym->value)
2078 {
2079 gcc_assert (!fsym->attr.allocatable);
2080 tmp = gfc_trans_assignment (e, fsym->value, false);
2081 gfc_add_expr_to_block (&se->pre, tmp);
2082 }
2083
2084 /* Obtain the character length of an assumed character
2085 length procedure from the typespec. */
2086 if (fsym->ts.type == BT_CHARACTER
2087 && parmse.string_length == NULL_TREE
2088 && e->ts.type == BT_PROCEDURE
2089 && e->symtree->n.sym->ts.type == BT_CHARACTER
2090 && e->symtree->n.sym->ts.cl->length != NULL)
2091 {
2092 gfc_conv_const_charlen (e->symtree->n.sym->ts.cl);
2093 parmse.string_length
2094 = e->symtree->n.sym->ts.cl->backend_decl;
2095 }
2096 }
2097
2098 if (need_interface_mapping)
2099 gfc_add_interface_mapping (&mapping, fsym, &parmse);
2100 }
2101
2102 gfc_add_block_to_block (&se->pre, &parmse.pre);
2103 gfc_add_block_to_block (&post, &parmse.post);
2104
2105 /* Allocated allocatable components of derived types must be
2106 deallocated for INTENT(OUT) dummy arguments and non-variable
2107 scalars. Non-variable arrays are dealt with in trans-array.c
2108 (gfc_conv_array_parameter). */
2109 if (e && e->ts.type == BT_DERIVED
2110 && e->ts.derived->attr.alloc_comp
2111 && ((formal && formal->sym->attr.intent == INTENT_OUT)
2112 ||
2113 (e->expr_type != EXPR_VARIABLE && !e->rank)))
2114 {
2115 int parm_rank;
2116 tmp = build_fold_indirect_ref (parmse.expr);
2117 parm_rank = e->rank;
2118 switch (parm_kind)
2119 {
2120 case (ELEMENTAL):
2121 case (SCALAR):
2122 parm_rank = 0;
2123 break;
2124
2125 case (SCALAR_POINTER):
2126 tmp = build_fold_indirect_ref (tmp);
2127 break;
2128 case (ARRAY):
2129 tmp = parmse.expr;
2130 break;
2131 }
2132
2133 tmp = gfc_deallocate_alloc_comp (e->ts.derived, tmp, parm_rank);
2134 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.optional)
2135 tmp = build3_v (COND_EXPR, gfc_conv_expr_present (e->symtree->n.sym),
2136 tmp, build_empty_stmt ());
2137
2138 if (e->expr_type != EXPR_VARIABLE)
2139 /* Don't deallocate non-variables until they have been used. */
2140 gfc_add_expr_to_block (&se->post, tmp);
2141 else
2142 {
2143 gcc_assert (formal && formal->sym->attr.intent == INTENT_OUT);
2144 gfc_add_expr_to_block (&se->pre, tmp);
2145 }
2146 }
2147
2148 /* Character strings are passed as two parameters, a length and a
2149 pointer. */
2150 if (parmse.string_length != NULL_TREE)
2151 stringargs = gfc_chainon_list (stringargs, parmse.string_length);
2152
2153 arglist = gfc_chainon_list (arglist, parmse.expr);
2154 }
2155 gfc_finish_interface_mapping (&mapping, &se->pre, &se->post);
2156
2157 ts = sym->ts;
2158 if (ts.type == BT_CHARACTER)
2159 {
2160 if (sym->ts.cl->length == NULL)
2161 {
2162 /* Assumed character length results are not allowed by 5.1.1.5 of the
2163 standard and are trapped in resolve.c; except in the case of SPREAD
2164 (and other intrinsics?) and dummy functions. In the case of SPREAD,
2165 we take the character length of the first argument for the result.
2166 For dummies, we have to look through the formal argument list for
2167 this function and use the character length found there.*/
2168 if (!sym->attr.dummy)
2169 cl.backend_decl = TREE_VALUE (stringargs);
2170 else
2171 {
2172 formal = sym->ns->proc_name->formal;
2173 for (; formal; formal = formal->next)
2174 if (strcmp (formal->sym->name, sym->name) == 0)
2175 cl.backend_decl = formal->sym->ts.cl->backend_decl;
2176 }
2177 }
2178 else
2179 {
2180 /* Calculate the length of the returned string. */
2181 gfc_init_se (&parmse, NULL);
2182 if (need_interface_mapping)
2183 gfc_apply_interface_mapping (&mapping, &parmse, sym->ts.cl->length);
2184 else
2185 gfc_conv_expr (&parmse, sym->ts.cl->length);
2186 gfc_add_block_to_block (&se->pre, &parmse.pre);
2187 gfc_add_block_to_block (&se->post, &parmse.post);
2188 cl.backend_decl = fold_convert (gfc_charlen_type_node, parmse.expr);
2189 }
2190
2191 /* Set up a charlen structure for it. */
2192 cl.next = NULL;
2193 cl.length = NULL;
2194 ts.cl = &cl;
2195
2196 len = cl.backend_decl;
2197 }
2198
2199 byref = gfc_return_by_reference (sym);
2200 if (byref)
2201 {
2202 if (se->direct_byref)
2203 retargs = gfc_chainon_list (retargs, se->expr);
2204 else if (sym->result->attr.dimension)
2205 {
2206 gcc_assert (se->loop && info);
2207
2208 /* Set the type of the array. */
2209 tmp = gfc_typenode_for_spec (&ts);
2210 info->dimen = se->loop->dimen;
2211
2212 /* Evaluate the bounds of the result, if known. */
2213 gfc_set_loop_bounds_from_array_spec (&mapping, se, sym->result->as);
2214
2215 /* Create a temporary to store the result. In case the function
2216 returns a pointer, the temporary will be a shallow copy and
2217 mustn't be deallocated. */
2218 callee_alloc = sym->attr.allocatable || sym->attr.pointer;
2219 gfc_trans_create_temp_array (&se->pre, &se->post, se->loop, info, tmp,
2220 false, !sym->attr.pointer, callee_alloc,
2221 true);
2222
2223 /* Pass the temporary as the first argument. */
2224 tmp = info->descriptor;
2225 tmp = build_fold_addr_expr (tmp);
2226 retargs = gfc_chainon_list (retargs, tmp);
2227 }
2228 else if (ts.type == BT_CHARACTER)
2229 {
2230 /* Pass the string length. */
2231 type = gfc_get_character_type (ts.kind, ts.cl);
2232 type = build_pointer_type (type);
2233
2234 /* Return an address to a char[0:len-1]* temporary for
2235 character pointers. */
2236 if (sym->attr.pointer || sym->attr.allocatable)
2237 {
2238 /* Build char[0:len-1] * pstr. */
2239 tmp = fold_build2 (MINUS_EXPR, gfc_charlen_type_node, len,
2240 build_int_cst (gfc_charlen_type_node, 1));
2241 tmp = build_range_type (gfc_array_index_type,
2242 gfc_index_zero_node, tmp);
2243 tmp = build_array_type (gfc_character1_type_node, tmp);
2244 var = gfc_create_var (build_pointer_type (tmp), "pstr");
2245
2246 /* Provide an address expression for the function arguments. */
2247 var = build_fold_addr_expr (var);
2248 }
2249 else
2250 var = gfc_conv_string_tmp (se, type, len);
2251
2252 retargs = gfc_chainon_list (retargs, var);
2253 }
2254 else
2255 {
2256 gcc_assert (gfc_option.flag_f2c && ts.type == BT_COMPLEX);
2257
2258 type = gfc_get_complex_type (ts.kind);
2259 var = build_fold_addr_expr (gfc_create_var (type, "cmplx"));
2260 retargs = gfc_chainon_list (retargs, var);
2261 }
2262
2263 /* Add the string length to the argument list. */
2264 if (ts.type == BT_CHARACTER)
2265 retargs = gfc_chainon_list (retargs, len);
2266 }
2267 gfc_free_interface_mapping (&mapping);
2268
2269 /* Add the return arguments. */
2270 arglist = chainon (retargs, arglist);
2271
2272 /* Add the hidden string length parameters to the arguments. */
2273 arglist = chainon (arglist, stringargs);
2274
2275 /* We may want to append extra arguments here. This is used e.g. for
2276 calls to libgfortran_matmul_??, which need extra information. */
2277 if (append_args != NULL_TREE)
2278 arglist = chainon (arglist, append_args);
2279
2280 /* Generate the actual call. */
2281 gfc_conv_function_val (se, sym);
2282 /* If there are alternate return labels, function type should be
2283 integer. Can't modify the type in place though, since it can be shared
2284 with other functions. */
2285 if (has_alternate_specifier
2286 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) != integer_type_node)
2287 {
2288 gcc_assert (! sym->attr.dummy);
2289 TREE_TYPE (sym->backend_decl)
2290 = build_function_type (integer_type_node,
2291 TYPE_ARG_TYPES (TREE_TYPE (sym->backend_decl)));
2292 se->expr = build_fold_addr_expr (sym->backend_decl);
2293 }
2294
2295 fntype = TREE_TYPE (TREE_TYPE (se->expr));
2296 se->expr = build3 (CALL_EXPR, TREE_TYPE (fntype), se->expr,
2297 arglist, NULL_TREE);
2298
2299 /* If we have a pointer function, but we don't want a pointer, e.g.
2300 something like
2301 x = f()
2302 where f is pointer valued, we have to dereference the result. */
2303 if (!se->want_pointer && !byref && sym->attr.pointer)
2304 se->expr = build_fold_indirect_ref (se->expr);
2305
2306 /* f2c calling conventions require a scalar default real function to
2307 return a double precision result. Convert this back to default
2308 real. We only care about the cases that can happen in Fortran 77.
2309 */
2310 if (gfc_option.flag_f2c && sym->ts.type == BT_REAL
2311 && sym->ts.kind == gfc_default_real_kind
2312 && !sym->attr.always_explicit)
2313 se->expr = fold_convert (gfc_get_real_type (sym->ts.kind), se->expr);
2314
2315 /* A pure function may still have side-effects - it may modify its
2316 parameters. */
2317 TREE_SIDE_EFFECTS (se->expr) = 1;
2318 #if 0
2319 if (!sym->attr.pure)
2320 TREE_SIDE_EFFECTS (se->expr) = 1;
2321 #endif
2322
2323 if (byref)
2324 {
2325 /* Add the function call to the pre chain. There is no expression. */
2326 gfc_add_expr_to_block (&se->pre, se->expr);
2327 se->expr = NULL_TREE;
2328
2329 if (!se->direct_byref)
2330 {
2331 if (sym->attr.dimension)
2332 {
2333 if (flag_bounds_check)
2334 {
2335 /* Check the data pointer hasn't been modified. This would
2336 happen in a function returning a pointer. */
2337 tmp = gfc_conv_descriptor_data_get (info->descriptor);
2338 tmp = fold_build2 (NE_EXPR, boolean_type_node,
2339 tmp, info->data);
2340 gfc_trans_runtime_check (tmp, gfc_msg_fault, &se->pre, NULL);
2341 }
2342 se->expr = info->descriptor;
2343 /* Bundle in the string length. */
2344 se->string_length = len;
2345 }
2346 else if (sym->ts.type == BT_CHARACTER)
2347 {
2348 /* Dereference for character pointer results. */
2349 if (sym->attr.pointer || sym->attr.allocatable)
2350 se->expr = build_fold_indirect_ref (var);
2351 else
2352 se->expr = var;
2353
2354 se->string_length = len;
2355 }
2356 else
2357 {
2358 gcc_assert (sym->ts.type == BT_COMPLEX && gfc_option.flag_f2c);
2359 se->expr = build_fold_indirect_ref (var);
2360 }
2361 }
2362 }
2363
2364 /* Follow the function call with the argument post block. */
2365 if (byref)
2366 gfc_add_block_to_block (&se->pre, &post);
2367 else
2368 gfc_add_block_to_block (&se->post, &post);
2369
2370 return has_alternate_specifier;
2371 }
2372
2373
2374 /* Generate code to copy a string. */
2375
2376 static void
2377 gfc_trans_string_copy (stmtblock_t * block, tree dlength, tree dest,
2378 tree slength, tree src)
2379 {
2380 tree tmp, dlen, slen;
2381 tree dsc;
2382 tree ssc;
2383 tree cond;
2384 tree cond2;
2385 tree tmp2;
2386 tree tmp3;
2387 tree tmp4;
2388 stmtblock_t tempblock;
2389
2390 dlen = fold_convert (size_type_node, gfc_evaluate_now (dlength, block));
2391 slen = fold_convert (size_type_node, gfc_evaluate_now (slength, block));
2392
2393 /* Deal with single character specially. */
2394 dsc = gfc_to_single_character (dlen, dest);
2395 ssc = gfc_to_single_character (slen, src);
2396 if (dsc != NULL_TREE && ssc != NULL_TREE)
2397 {
2398 gfc_add_modify_expr (block, dsc, ssc);
2399 return;
2400 }
2401
2402 /* Do nothing if the destination length is zero. */
2403 cond = fold_build2 (GT_EXPR, boolean_type_node, dlen,
2404 build_int_cst (gfc_charlen_type_node, 0));
2405
2406 /* The following code was previously in _gfortran_copy_string:
2407
2408 // The two strings may overlap so we use memmove.
2409 void
2410 copy_string (GFC_INTEGER_4 destlen, char * dest,
2411 GFC_INTEGER_4 srclen, const char * src)
2412 {
2413 if (srclen >= destlen)
2414 {
2415 // This will truncate if too long.
2416 memmove (dest, src, destlen);
2417 }
2418 else
2419 {
2420 memmove (dest, src, srclen);
2421 // Pad with spaces.
2422 memset (&dest[srclen], ' ', destlen - srclen);
2423 }
2424 }
2425
2426 We're now doing it here for better optimization, but the logic
2427 is the same. */
2428
2429 /* Truncate string if source is too long. */
2430 cond2 = fold_build2 (GE_EXPR, boolean_type_node, slen, dlen);
2431 tmp2 = gfc_chainon_list (NULL_TREE, dest);
2432 tmp2 = gfc_chainon_list (tmp2, src);
2433 tmp2 = gfc_chainon_list (tmp2, dlen);
2434 tmp2 = build_function_call_expr (built_in_decls[BUILT_IN_MEMMOVE], tmp2);
2435
2436 /* Else copy and pad with spaces. */
2437 tmp3 = gfc_chainon_list (NULL_TREE, dest);
2438 tmp3 = gfc_chainon_list (tmp3, src);
2439 tmp3 = gfc_chainon_list (tmp3, slen);
2440 tmp3 = build_function_call_expr (built_in_decls[BUILT_IN_MEMMOVE], tmp3);
2441
2442 tmp4 = fold_build2 (PLUS_EXPR, pchar_type_node, dest,
2443 fold_convert (pchar_type_node, slen));
2444 tmp4 = gfc_chainon_list (NULL_TREE, tmp4);
2445 tmp4 = gfc_chainon_list (tmp4, build_int_cst
2446 (gfc_get_int_type (gfc_c_int_kind),
2447 lang_hooks.to_target_charset (' ')));
2448 tmp4 = gfc_chainon_list (tmp4, fold_build2 (MINUS_EXPR, TREE_TYPE(dlen),
2449 dlen, slen));
2450 tmp4 = build_function_call_expr (built_in_decls[BUILT_IN_MEMSET], tmp4);
2451
2452 gfc_init_block (&tempblock);
2453 gfc_add_expr_to_block (&tempblock, tmp3);
2454 gfc_add_expr_to_block (&tempblock, tmp4);
2455 tmp3 = gfc_finish_block (&tempblock);
2456
2457 /* The whole copy_string function is there. */
2458 tmp = fold_build3 (COND_EXPR, void_type_node, cond2, tmp2, tmp3);
2459 tmp = fold_build3 (COND_EXPR, void_type_node, cond, tmp, build_empty_stmt ());
2460 gfc_add_expr_to_block (block, tmp);
2461 }
2462
2463
2464 /* Translate a statement function.
2465 The value of a statement function reference is obtained by evaluating the
2466 expression using the values of the actual arguments for the values of the
2467 corresponding dummy arguments. */
2468
2469 static void
2470 gfc_conv_statement_function (gfc_se * se, gfc_expr * expr)
2471 {
2472 gfc_symbol *sym;
2473 gfc_symbol *fsym;
2474 gfc_formal_arglist *fargs;
2475 gfc_actual_arglist *args;
2476 gfc_se lse;
2477 gfc_se rse;
2478 gfc_saved_var *saved_vars;
2479 tree *temp_vars;
2480 tree type;
2481 tree tmp;
2482 int n;
2483
2484 sym = expr->symtree->n.sym;
2485 args = expr->value.function.actual;
2486 gfc_init_se (&lse, NULL);
2487 gfc_init_se (&rse, NULL);
2488
2489 n = 0;
2490 for (fargs = sym->formal; fargs; fargs = fargs->next)
2491 n++;
2492 saved_vars = (gfc_saved_var *)gfc_getmem (n * sizeof (gfc_saved_var));
2493 temp_vars = (tree *)gfc_getmem (n * sizeof (tree));
2494
2495 for (fargs = sym->formal, n = 0; fargs; fargs = fargs->next, n++)
2496 {
2497 /* Each dummy shall be specified, explicitly or implicitly, to be
2498 scalar. */
2499 gcc_assert (fargs->sym->attr.dimension == 0);
2500 fsym = fargs->sym;
2501
2502 /* Create a temporary to hold the value. */
2503 type = gfc_typenode_for_spec (&fsym->ts);
2504 temp_vars[n] = gfc_create_var (type, fsym->name);
2505
2506 if (fsym->ts.type == BT_CHARACTER)
2507 {
2508 /* Copy string arguments. */
2509 tree arglen;
2510
2511 gcc_assert (fsym->ts.cl && fsym->ts.cl->length
2512 && fsym->ts.cl->length->expr_type == EXPR_CONSTANT);
2513
2514 arglen = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
2515 tmp = gfc_build_addr_expr (build_pointer_type (type),
2516 temp_vars[n]);
2517
2518 gfc_conv_expr (&rse, args->expr);
2519 gfc_conv_string_parameter (&rse);
2520 gfc_add_block_to_block (&se->pre, &lse.pre);
2521 gfc_add_block_to_block (&se->pre, &rse.pre);
2522
2523 gfc_trans_string_copy (&se->pre, arglen, tmp, rse.string_length,
2524 rse.expr);
2525 gfc_add_block_to_block (&se->pre, &lse.post);
2526 gfc_add_block_to_block (&se->pre, &rse.post);
2527 }
2528 else
2529 {
2530 /* For everything else, just evaluate the expression. */
2531 gfc_conv_expr (&lse, args->expr);
2532
2533 gfc_add_block_to_block (&se->pre, &lse.pre);
2534 gfc_add_modify_expr (&se->pre, temp_vars[n], lse.expr);
2535 gfc_add_block_to_block (&se->pre, &lse.post);
2536 }
2537
2538 args = args->next;
2539 }
2540
2541 /* Use the temporary variables in place of the real ones. */
2542 for (fargs = sym->formal, n = 0; fargs; fargs = fargs->next, n++)
2543 gfc_shadow_sym (fargs->sym, temp_vars[n], &saved_vars[n]);
2544
2545 gfc_conv_expr (se, sym->value);
2546
2547 if (sym->ts.type == BT_CHARACTER)
2548 {
2549 gfc_conv_const_charlen (sym->ts.cl);
2550
2551 /* Force the expression to the correct length. */
2552 if (!INTEGER_CST_P (se->string_length)
2553 || tree_int_cst_lt (se->string_length,
2554 sym->ts.cl->backend_decl))
2555 {
2556 type = gfc_get_character_type (sym->ts.kind, sym->ts.cl);
2557 tmp = gfc_create_var (type, sym->name);
2558 tmp = gfc_build_addr_expr (build_pointer_type (type), tmp);
2559 gfc_trans_string_copy (&se->pre, sym->ts.cl->backend_decl, tmp,
2560 se->string_length, se->expr);
2561 se->expr = tmp;
2562 }
2563 se->string_length = sym->ts.cl->backend_decl;
2564 }
2565
2566 /* Restore the original variables. */
2567 for (fargs = sym->formal, n = 0; fargs; fargs = fargs->next, n++)
2568 gfc_restore_sym (fargs->sym, &saved_vars[n]);
2569 gfc_free (saved_vars);
2570 }
2571
2572
2573 /* Translate a function expression. */
2574
2575 static void
2576 gfc_conv_function_expr (gfc_se * se, gfc_expr * expr)
2577 {
2578 gfc_symbol *sym;
2579
2580 if (expr->value.function.isym)
2581 {
2582 gfc_conv_intrinsic_function (se, expr);
2583 return;
2584 }
2585
2586 /* We distinguish statement functions from general functions to improve
2587 runtime performance. */
2588 if (expr->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2589 {
2590 gfc_conv_statement_function (se, expr);
2591 return;
2592 }
2593
2594 /* expr.value.function.esym is the resolved (specific) function symbol for
2595 most functions. However this isn't set for dummy procedures. */
2596 sym = expr->value.function.esym;
2597 if (!sym)
2598 sym = expr->symtree->n.sym;
2599 gfc_conv_function_call (se, sym, expr->value.function.actual, NULL_TREE);
2600 }
2601
2602
2603 static void
2604 gfc_conv_array_constructor_expr (gfc_se * se, gfc_expr * expr)
2605 {
2606 gcc_assert (se->ss != NULL && se->ss != gfc_ss_terminator);
2607 gcc_assert (se->ss->expr == expr && se->ss->type == GFC_SS_CONSTRUCTOR);
2608
2609 gfc_conv_tmp_array_ref (se);
2610 gfc_advance_se_ss_chain (se);
2611 }
2612
2613
2614 /* Build a static initializer. EXPR is the expression for the initial value.
2615 The other parameters describe the variable of the component being
2616 initialized. EXPR may be null. */
2617
2618 tree
2619 gfc_conv_initializer (gfc_expr * expr, gfc_typespec * ts, tree type,
2620 bool array, bool pointer)
2621 {
2622 gfc_se se;
2623
2624 if (!(expr || pointer))
2625 return NULL_TREE;
2626
2627 if (array)
2628 {
2629 /* Arrays need special handling. */
2630 if (pointer)
2631 return gfc_build_null_descriptor (type);
2632 else
2633 return gfc_conv_array_initializer (type, expr);
2634 }
2635 else if (pointer)
2636 return fold_convert (type, null_pointer_node);
2637 else
2638 {
2639 switch (ts->type)
2640 {
2641 case BT_DERIVED:
2642 gfc_init_se (&se, NULL);
2643 gfc_conv_structure (&se, expr, 1);
2644 return se.expr;
2645
2646 case BT_CHARACTER:
2647 return gfc_conv_string_init (ts->cl->backend_decl,expr);
2648
2649 default:
2650 gfc_init_se (&se, NULL);
2651 gfc_conv_constant (&se, expr);
2652 return se.expr;
2653 }
2654 }
2655 }
2656
2657 static tree
2658 gfc_trans_subarray_assign (tree dest, gfc_component * cm, gfc_expr * expr)
2659 {
2660 gfc_se rse;
2661 gfc_se lse;
2662 gfc_ss *rss;
2663 gfc_ss *lss;
2664 stmtblock_t body;
2665 stmtblock_t block;
2666 gfc_loopinfo loop;
2667 int n;
2668 tree tmp;
2669
2670 gfc_start_block (&block);
2671
2672 /* Initialize the scalarizer. */
2673 gfc_init_loopinfo (&loop);
2674
2675 gfc_init_se (&lse, NULL);
2676 gfc_init_se (&rse, NULL);
2677
2678 /* Walk the rhs. */
2679 rss = gfc_walk_expr (expr);
2680 if (rss == gfc_ss_terminator)
2681 {
2682 /* The rhs is scalar. Add a ss for the expression. */
2683 rss = gfc_get_ss ();
2684 rss->next = gfc_ss_terminator;
2685 rss->type = GFC_SS_SCALAR;
2686 rss->expr = expr;
2687 }
2688
2689 /* Create a SS for the destination. */
2690 lss = gfc_get_ss ();
2691 lss->type = GFC_SS_COMPONENT;
2692 lss->expr = NULL;
2693 lss->shape = gfc_get_shape (cm->as->rank);
2694 lss->next = gfc_ss_terminator;
2695 lss->data.info.dimen = cm->as->rank;
2696 lss->data.info.descriptor = dest;
2697 lss->data.info.data = gfc_conv_array_data (dest);
2698 lss->data.info.offset = gfc_conv_array_offset (dest);
2699 for (n = 0; n < cm->as->rank; n++)
2700 {
2701 lss->data.info.dim[n] = n;
2702 lss->data.info.start[n] = gfc_conv_array_lbound (dest, n);
2703 lss->data.info.stride[n] = gfc_index_one_node;
2704
2705 mpz_init (lss->shape[n]);
2706 mpz_sub (lss->shape[n], cm->as->upper[n]->value.integer,
2707 cm->as->lower[n]->value.integer);
2708 mpz_add_ui (lss->shape[n], lss->shape[n], 1);
2709 }
2710
2711 /* Associate the SS with the loop. */
2712 gfc_add_ss_to_loop (&loop, lss);
2713 gfc_add_ss_to_loop (&loop, rss);
2714
2715 /* Calculate the bounds of the scalarization. */
2716 gfc_conv_ss_startstride (&loop);
2717
2718 /* Setup the scalarizing loops. */
2719 gfc_conv_loop_setup (&loop);
2720
2721 /* Setup the gfc_se structures. */
2722 gfc_copy_loopinfo_to_se (&lse, &loop);
2723 gfc_copy_loopinfo_to_se (&rse, &loop);
2724
2725 rse.ss = rss;
2726 gfc_mark_ss_chain_used (rss, 1);
2727 lse.ss = lss;
2728 gfc_mark_ss_chain_used (lss, 1);
2729
2730 /* Start the scalarized loop body. */
2731 gfc_start_scalarized_body (&loop, &body);
2732
2733 gfc_conv_tmp_array_ref (&lse);
2734 if (cm->ts.type == BT_CHARACTER)
2735 lse.string_length = cm->ts.cl->backend_decl;
2736
2737 gfc_conv_expr (&rse, expr);
2738
2739 tmp = gfc_trans_scalar_assign (&lse, &rse, cm->ts, true, false);
2740 gfc_add_expr_to_block (&body, tmp);
2741
2742 gcc_assert (rse.ss == gfc_ss_terminator);
2743
2744 /* Generate the copying loops. */
2745 gfc_trans_scalarizing_loops (&loop, &body);
2746
2747 /* Wrap the whole thing up. */
2748 gfc_add_block_to_block (&block, &loop.pre);
2749 gfc_add_block_to_block (&block, &loop.post);
2750
2751 for (n = 0; n < cm->as->rank; n++)
2752 mpz_clear (lss->shape[n]);
2753 gfc_free (lss->shape);
2754
2755 gfc_cleanup_loop (&loop);
2756
2757 return gfc_finish_block (&block);
2758 }
2759
2760
2761 /* Assign a single component of a derived type constructor. */
2762
2763 static tree
2764 gfc_trans_subcomponent_assign (tree dest, gfc_component * cm, gfc_expr * expr)
2765 {
2766 gfc_se se;
2767 gfc_se lse;
2768 gfc_ss *rss;
2769 stmtblock_t block;
2770 tree tmp;
2771 tree offset;
2772 int n;
2773
2774 gfc_start_block (&block);
2775
2776 if (cm->pointer)
2777 {
2778 gfc_init_se (&se, NULL);
2779 /* Pointer component. */
2780 if (cm->dimension)
2781 {
2782 /* Array pointer. */
2783 if (expr->expr_type == EXPR_NULL)
2784 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
2785 else
2786 {
2787 rss = gfc_walk_expr (expr);
2788 se.direct_byref = 1;
2789 se.expr = dest;
2790 gfc_conv_expr_descriptor (&se, expr, rss);
2791 gfc_add_block_to_block (&block, &se.pre);
2792 gfc_add_block_to_block (&block, &se.post);
2793 }
2794 }
2795 else
2796 {
2797 /* Scalar pointers. */
2798 se.want_pointer = 1;
2799 gfc_conv_expr (&se, expr);
2800 gfc_add_block_to_block (&block, &se.pre);
2801 gfc_add_modify_expr (&block, dest,
2802 fold_convert (TREE_TYPE (dest), se.expr));
2803 gfc_add_block_to_block (&block, &se.post);
2804 }
2805 }
2806 else if (cm->dimension)
2807 {
2808 if (cm->allocatable && expr->expr_type == EXPR_NULL)
2809 gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
2810 else if (cm->allocatable)
2811 {
2812 tree tmp2;
2813
2814 gfc_init_se (&se, NULL);
2815
2816 rss = gfc_walk_expr (expr);
2817 se.want_pointer = 0;
2818 gfc_conv_expr_descriptor (&se, expr, rss);
2819 gfc_add_block_to_block (&block, &se.pre);
2820
2821 tmp = fold_convert (TREE_TYPE (dest), se.expr);
2822 gfc_add_modify_expr (&block, dest, tmp);
2823
2824 if (cm->ts.type == BT_DERIVED && cm->ts.derived->attr.alloc_comp)
2825 tmp = gfc_copy_alloc_comp (cm->ts.derived, se.expr, dest,
2826 cm->as->rank);
2827 else
2828 tmp = gfc_duplicate_allocatable (dest, se.expr,
2829 TREE_TYPE(cm->backend_decl),
2830 cm->as->rank);
2831
2832 gfc_add_expr_to_block (&block, tmp);
2833
2834 gfc_add_block_to_block (&block, &se.post);
2835 gfc_conv_descriptor_data_set (&block, se.expr, null_pointer_node);
2836
2837 /* Shift the lbound and ubound of temporaries to being unity, rather
2838 than zero, based. Calculate the offset for all cases. */
2839 offset = gfc_conv_descriptor_offset (dest);
2840 gfc_add_modify_expr (&block, offset, gfc_index_zero_node);
2841 tmp2 =gfc_create_var (gfc_array_index_type, NULL);
2842 for (n = 0; n < expr->rank; n++)
2843 {
2844 if (expr->expr_type != EXPR_VARIABLE
2845 && expr->expr_type != EXPR_CONSTANT)
2846 {
2847 tmp = gfc_conv_descriptor_ubound (dest, gfc_rank_cst[n]);
2848 gfc_add_modify_expr (&block, tmp,
2849 fold_build2 (PLUS_EXPR,
2850 gfc_array_index_type,
2851 tmp, gfc_index_one_node));
2852 tmp = gfc_conv_descriptor_lbound (dest, gfc_rank_cst[n]);
2853 gfc_add_modify_expr (&block, tmp, gfc_index_one_node);
2854 }
2855 tmp = fold_build2 (MULT_EXPR, gfc_array_index_type,
2856 gfc_conv_descriptor_lbound (dest,
2857 gfc_rank_cst[n]),
2858 gfc_conv_descriptor_stride (dest,
2859 gfc_rank_cst[n]));
2860 gfc_add_modify_expr (&block, tmp2, tmp);
2861 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, offset, tmp2);
2862 gfc_add_modify_expr (&block, offset, tmp);
2863 }
2864 }
2865 else
2866 {
2867 tmp = gfc_trans_subarray_assign (dest, cm, expr);
2868 gfc_add_expr_to_block (&block, tmp);
2869 }
2870 }
2871 else if (expr->ts.type == BT_DERIVED)
2872 {
2873 if (expr->expr_type != EXPR_STRUCTURE)
2874 {
2875 gfc_init_se (&se, NULL);
2876 gfc_conv_expr (&se, expr);
2877 gfc_add_modify_expr (&block, dest,
2878 fold_convert (TREE_TYPE (dest), se.expr));
2879 }
2880 else
2881 {
2882 /* Nested constructors. */
2883 tmp = gfc_trans_structure_assign (dest, expr);
2884 gfc_add_expr_to_block (&block, tmp);
2885 }
2886 }
2887 else
2888 {
2889 /* Scalar component. */
2890 gfc_init_se (&se, NULL);
2891 gfc_init_se (&lse, NULL);
2892
2893 gfc_conv_expr (&se, expr);
2894 if (cm->ts.type == BT_CHARACTER)
2895 lse.string_length = cm->ts.cl->backend_decl;
2896 lse.expr = dest;
2897 tmp = gfc_trans_scalar_assign (&lse, &se, cm->ts, true, false);
2898 gfc_add_expr_to_block (&block, tmp);
2899 }
2900 return gfc_finish_block (&block);
2901 }
2902
2903 /* Assign a derived type constructor to a variable. */
2904
2905 static tree
2906 gfc_trans_structure_assign (tree dest, gfc_expr * expr)
2907 {
2908 gfc_constructor *c;
2909 gfc_component *cm;
2910 stmtblock_t block;
2911 tree field;
2912 tree tmp;
2913
2914 gfc_start_block (&block);
2915 cm = expr->ts.derived->components;
2916 for (c = expr->value.constructor; c; c = c->next, cm = cm->next)
2917 {
2918 /* Skip absent members in default initializers. */
2919 if (!c->expr)
2920 continue;
2921
2922 field = cm->backend_decl;
2923 tmp = build3 (COMPONENT_REF, TREE_TYPE (field), dest, field, NULL_TREE);
2924 tmp = gfc_trans_subcomponent_assign (tmp, cm, c->expr);
2925 gfc_add_expr_to_block (&block, tmp);
2926 }
2927 return gfc_finish_block (&block);
2928 }
2929
2930 /* Build an expression for a constructor. If init is nonzero then
2931 this is part of a static variable initializer. */
2932
2933 void
2934 gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
2935 {
2936 gfc_constructor *c;
2937 gfc_component *cm;
2938 tree val;
2939 tree type;
2940 tree tmp;
2941 VEC(constructor_elt,gc) *v = NULL;
2942
2943 gcc_assert (se->ss == NULL);
2944 gcc_assert (expr->expr_type == EXPR_STRUCTURE);
2945 type = gfc_typenode_for_spec (&expr->ts);
2946
2947 if (!init)
2948 {
2949 /* Create a temporary variable and fill it in. */
2950 se->expr = gfc_create_var (type, expr->ts.derived->name);
2951 tmp = gfc_trans_structure_assign (se->expr, expr);
2952 gfc_add_expr_to_block (&se->pre, tmp);
2953 return;
2954 }
2955
2956 cm = expr->ts.derived->components;
2957
2958 for (c = expr->value.constructor; c; c = c->next, cm = cm->next)
2959 {
2960 /* Skip absent members in default initializers and allocatable
2961 components. Although the latter have a default initializer
2962 of EXPR_NULL,... by default, the static nullify is not needed
2963 since this is done every time we come into scope. */
2964 if (!c->expr || cm->allocatable)
2965 continue;
2966
2967 val = gfc_conv_initializer (c->expr, &cm->ts,
2968 TREE_TYPE (cm->backend_decl), cm->dimension, cm->pointer);
2969
2970 /* Append it to the constructor list. */
2971 CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
2972 }
2973 se->expr = build_constructor (type, v);
2974 }
2975
2976
2977 /* Translate a substring expression. */
2978
2979 static void
2980 gfc_conv_substring_expr (gfc_se * se, gfc_expr * expr)
2981 {
2982 gfc_ref *ref;
2983
2984 ref = expr->ref;
2985
2986 gcc_assert (ref->type == REF_SUBSTRING);
2987
2988 se->expr = gfc_build_string_const(expr->value.character.length,
2989 expr->value.character.string);
2990 se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
2991 TYPE_STRING_FLAG (TREE_TYPE (se->expr))=1;
2992
2993 gfc_conv_substring(se,ref,expr->ts.kind,NULL,&expr->where);
2994 }
2995
2996
2997 /* Entry point for expression translation. Evaluates a scalar quantity.
2998 EXPR is the expression to be translated, and SE is the state structure if
2999 called from within the scalarized. */
3000
3001 void
3002 gfc_conv_expr (gfc_se * se, gfc_expr * expr)
3003 {
3004 if (se->ss && se->ss->expr == expr
3005 && (se->ss->type == GFC_SS_SCALAR || se->ss->type == GFC_SS_REFERENCE))
3006 {
3007 /* Substitute a scalar expression evaluated outside the scalarization
3008 loop. */
3009 se->expr = se->ss->data.scalar.expr;
3010 se->string_length = se->ss->string_length;
3011 gfc_advance_se_ss_chain (se);
3012 return;
3013 }
3014
3015 switch (expr->expr_type)
3016 {
3017 case EXPR_OP:
3018 gfc_conv_expr_op (se, expr);
3019 break;
3020
3021 case EXPR_FUNCTION:
3022 gfc_conv_function_expr (se, expr);
3023 break;
3024
3025 case EXPR_CONSTANT:
3026 gfc_conv_constant (se, expr);
3027 break;
3028
3029 case EXPR_VARIABLE:
3030 gfc_conv_variable (se, expr);
3031 break;
3032
3033 case EXPR_NULL:
3034 se->expr = null_pointer_node;
3035 break;
3036
3037 case EXPR_SUBSTRING:
3038 gfc_conv_substring_expr (se, expr);
3039 break;
3040
3041 case EXPR_STRUCTURE:
3042 gfc_conv_structure (se, expr, 0);
3043 break;
3044
3045 case EXPR_ARRAY:
3046 gfc_conv_array_constructor_expr (se, expr);
3047 break;
3048
3049 default:
3050 gcc_unreachable ();
3051 break;
3052 }
3053 }
3054
3055 /* Like gfc_conv_expr_val, but the value is also suitable for use in the lhs
3056 of an assignment. */
3057 void
3058 gfc_conv_expr_lhs (gfc_se * se, gfc_expr * expr)
3059 {
3060 gfc_conv_expr (se, expr);
3061 /* All numeric lvalues should have empty post chains. If not we need to
3062 figure out a way of rewriting an lvalue so that it has no post chain. */
3063 gcc_assert (expr->ts.type == BT_CHARACTER || !se->post.head);
3064 }
3065
3066 /* Like gfc_conv_expr, but the POST block is guaranteed to be empty for
3067 numeric expressions. Used for scalar values where inserting cleanup code
3068 is inconvenient. */
3069 void
3070 gfc_conv_expr_val (gfc_se * se, gfc_expr * expr)
3071 {
3072 tree val;
3073
3074 gcc_assert (expr->ts.type != BT_CHARACTER);
3075 gfc_conv_expr (se, expr);
3076 if (se->post.head)
3077 {
3078 val = gfc_create_var (TREE_TYPE (se->expr), NULL);
3079 gfc_add_modify_expr (&se->pre, val, se->expr);
3080 se->expr = val;
3081 gfc_add_block_to_block (&se->pre, &se->post);
3082 }
3083 }
3084
3085 /* Helper to translate and expression and convert it to a particular type. */
3086 void
3087 gfc_conv_expr_type (gfc_se * se, gfc_expr * expr, tree type)
3088 {
3089 gfc_conv_expr_val (se, expr);
3090 se->expr = convert (type, se->expr);
3091 }
3092
3093
3094 /* Converts an expression so that it can be passed by reference. Scalar
3095 values only. */
3096
3097 void
3098 gfc_conv_expr_reference (gfc_se * se, gfc_expr * expr)
3099 {
3100 tree var;
3101
3102 if (se->ss && se->ss->expr == expr
3103 && se->ss->type == GFC_SS_REFERENCE)
3104 {
3105 se->expr = se->ss->data.scalar.expr;
3106 se->string_length = se->ss->string_length;
3107 gfc_advance_se_ss_chain (se);
3108 return;
3109 }
3110
3111 if (expr->ts.type == BT_CHARACTER)
3112 {
3113 gfc_conv_expr (se, expr);
3114 gfc_conv_string_parameter (se);
3115 return;
3116 }
3117
3118 if (expr->expr_type == EXPR_VARIABLE)
3119 {
3120 se->want_pointer = 1;
3121 gfc_conv_expr (se, expr);
3122 if (se->post.head)
3123 {
3124 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
3125 gfc_add_modify_expr (&se->pre, var, se->expr);
3126 gfc_add_block_to_block (&se->pre, &se->post);
3127 se->expr = var;
3128 }
3129 return;
3130 }
3131
3132 gfc_conv_expr (se, expr);
3133
3134 /* Create a temporary var to hold the value. */
3135 if (TREE_CONSTANT (se->expr))
3136 {
3137 tree tmp = se->expr;
3138 STRIP_TYPE_NOPS (tmp);
3139 var = build_decl (CONST_DECL, NULL, TREE_TYPE (tmp));
3140 DECL_INITIAL (var) = tmp;
3141 TREE_STATIC (var) = 1;
3142 pushdecl (var);
3143 }
3144 else
3145 {
3146 var = gfc_create_var (TREE_TYPE (se->expr), NULL);
3147 gfc_add_modify_expr (&se->pre, var, se->expr);
3148 }
3149 gfc_add_block_to_block (&se->pre, &se->post);
3150
3151 /* Take the address of that value. */
3152 se->expr = build_fold_addr_expr (var);
3153 }
3154
3155
3156 tree
3157 gfc_trans_pointer_assign (gfc_code * code)
3158 {
3159 return gfc_trans_pointer_assignment (code->expr, code->expr2);
3160 }
3161
3162
3163 /* Generate code for a pointer assignment. */
3164
3165 tree
3166 gfc_trans_pointer_assignment (gfc_expr * expr1, gfc_expr * expr2)
3167 {
3168 gfc_se lse;
3169 gfc_se rse;
3170 gfc_ss *lss;
3171 gfc_ss *rss;
3172 stmtblock_t block;
3173 tree desc;
3174 tree tmp;
3175
3176 gfc_start_block (&block);
3177
3178 gfc_init_se (&lse, NULL);
3179
3180 lss = gfc_walk_expr (expr1);
3181 rss = gfc_walk_expr (expr2);
3182 if (lss == gfc_ss_terminator)
3183 {
3184 /* Scalar pointers. */
3185 lse.want_pointer = 1;
3186 gfc_conv_expr (&lse, expr1);
3187 gcc_assert (rss == gfc_ss_terminator);
3188 gfc_init_se (&rse, NULL);
3189 rse.want_pointer = 1;
3190 gfc_conv_expr (&rse, expr2);
3191 gfc_add_block_to_block (&block, &lse.pre);
3192 gfc_add_block_to_block (&block, &rse.pre);
3193 gfc_add_modify_expr (&block, lse.expr,
3194 fold_convert (TREE_TYPE (lse.expr), rse.expr));
3195 gfc_add_block_to_block (&block, &rse.post);
3196 gfc_add_block_to_block (&block, &lse.post);
3197 }
3198 else
3199 {
3200 /* Array pointer. */
3201 gfc_conv_expr_descriptor (&lse, expr1, lss);
3202 switch (expr2->expr_type)
3203 {
3204 case EXPR_NULL:
3205 /* Just set the data pointer to null. */
3206 gfc_conv_descriptor_data_set (&lse.pre, lse.expr, null_pointer_node);
3207 break;
3208
3209 case EXPR_VARIABLE:
3210 /* Assign directly to the pointer's descriptor. */
3211 lse.direct_byref = 1;
3212 gfc_conv_expr_descriptor (&lse, expr2, rss);
3213 break;
3214
3215 default:
3216 /* Assign to a temporary descriptor and then copy that
3217 temporary to the pointer. */
3218 desc = lse.expr;
3219 tmp = gfc_create_var (TREE_TYPE (desc), "ptrtemp");
3220
3221 lse.expr = tmp;
3222 lse.direct_byref = 1;
3223 gfc_conv_expr_descriptor (&lse, expr2, rss);
3224 gfc_add_modify_expr (&lse.pre, desc, tmp);
3225 break;
3226 }
3227 gfc_add_block_to_block (&block, &lse.pre);
3228 gfc_add_block_to_block (&block, &lse.post);
3229 }
3230 return gfc_finish_block (&block);
3231 }
3232
3233
3234 /* Makes sure se is suitable for passing as a function string parameter. */
3235 /* TODO: Need to check all callers fo this function. It may be abused. */
3236
3237 void
3238 gfc_conv_string_parameter (gfc_se * se)
3239 {
3240 tree type;
3241
3242 if (TREE_CODE (se->expr) == STRING_CST)
3243 {
3244 se->expr = gfc_build_addr_expr (pchar_type_node, se->expr);
3245 return;
3246 }
3247
3248 type = TREE_TYPE (se->expr);
3249 if (TYPE_STRING_FLAG (type))
3250 {
3251 gcc_assert (TREE_CODE (se->expr) != INDIRECT_REF);
3252 se->expr = gfc_build_addr_expr (pchar_type_node, se->expr);
3253 }
3254
3255 gcc_assert (POINTER_TYPE_P (TREE_TYPE (se->expr)));
3256 gcc_assert (se->string_length
3257 && TREE_CODE (TREE_TYPE (se->string_length)) == INTEGER_TYPE);
3258 }
3259
3260
3261 /* Generate code for assignment of scalar variables. Includes character
3262 strings and derived types with allocatable components. */
3263
3264 tree
3265 gfc_trans_scalar_assign (gfc_se * lse, gfc_se * rse, gfc_typespec ts,
3266 bool l_is_temp, bool r_is_var)
3267 {
3268 stmtblock_t block;
3269 tree tmp;
3270 tree cond;
3271
3272 gfc_init_block (&block);
3273
3274 if (ts.type == BT_CHARACTER)
3275 {
3276 gcc_assert (lse->string_length != NULL_TREE
3277 && rse->string_length != NULL_TREE);
3278
3279 gfc_conv_string_parameter (lse);
3280 gfc_conv_string_parameter (rse);
3281
3282 gfc_add_block_to_block (&block, &lse->pre);
3283 gfc_add_block_to_block (&block, &rse->pre);
3284
3285 gfc_trans_string_copy (&block, lse->string_length, lse->expr,
3286 rse->string_length, rse->expr);
3287 }
3288 else if (ts.type == BT_DERIVED && ts.derived->attr.alloc_comp)
3289 {
3290 cond = NULL_TREE;
3291
3292 /* Are the rhs and the lhs the same? */
3293 if (r_is_var)
3294 {
3295 cond = fold_build2 (EQ_EXPR, boolean_type_node,
3296 build_fold_addr_expr (lse->expr),
3297 build_fold_addr_expr (rse->expr));
3298 cond = gfc_evaluate_now (cond, &lse->pre);
3299 }
3300
3301 /* Deallocate the lhs allocated components as long as it is not
3302 the same as the rhs. */
3303 if (!l_is_temp)
3304 {
3305 tmp = gfc_deallocate_alloc_comp (ts.derived, lse->expr, 0);
3306 if (r_is_var)
3307 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (), tmp);
3308 gfc_add_expr_to_block (&lse->pre, tmp);
3309 }
3310
3311 gfc_add_block_to_block (&block, &lse->pre);
3312 gfc_add_block_to_block (&block, &rse->pre);
3313
3314 gfc_add_modify_expr (&block, lse->expr,
3315 fold_convert (TREE_TYPE (lse->expr), rse->expr));
3316
3317 /* Do a deep copy if the rhs is a variable, if it is not the
3318 same as the lhs. */
3319 if (r_is_var)
3320 {
3321 tmp = gfc_copy_alloc_comp (ts.derived, rse->expr, lse->expr, 0);
3322 tmp = build3_v (COND_EXPR, cond, build_empty_stmt (), tmp);
3323 gfc_add_expr_to_block (&block, tmp);
3324 }
3325 }
3326 else
3327 {
3328 gfc_add_block_to_block (&block, &lse->pre);
3329 gfc_add_block_to_block (&block, &rse->pre);
3330
3331 gfc_add_modify_expr (&block, lse->expr,
3332 fold_convert (TREE_TYPE (lse->expr), rse->expr));
3333 }
3334
3335 gfc_add_block_to_block (&block, &lse->post);
3336 gfc_add_block_to_block (&block, &rse->post);
3337
3338 return gfc_finish_block (&block);
3339 }
3340
3341
3342 /* Try to translate array(:) = func (...), where func is a transformational
3343 array function, without using a temporary. Returns NULL is this isn't the
3344 case. */
3345
3346 static tree
3347 gfc_trans_arrayfunc_assign (gfc_expr * expr1, gfc_expr * expr2)
3348 {
3349 gfc_se se;
3350 gfc_ss *ss;
3351 gfc_ref * ref;
3352 bool seen_array_ref;
3353
3354 /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION. */
3355 if (expr2->value.function.isym && !gfc_is_intrinsic_libcall (expr2))
3356 return NULL;
3357
3358 /* Elemental functions don't need a temporary anyway. */
3359 if (expr2->value.function.esym != NULL
3360 && expr2->value.function.esym->attr.elemental)
3361 return NULL;
3362
3363 /* Fail if EXPR1 can't be expressed as a descriptor. */
3364 if (gfc_ref_needs_temporary_p (expr1->ref))
3365 return NULL;
3366
3367 /* Functions returning pointers need temporaries. */
3368 if (expr2->symtree->n.sym->attr.pointer
3369 || expr2->symtree->n.sym->attr.allocatable)
3370 return NULL;
3371
3372 /* Check that no LHS component references appear during an array
3373 reference. This is needed because we do not have the means to
3374 span any arbitrary stride with an array descriptor. This check
3375 is not needed for the rhs because the function result has to be
3376 a complete type. */
3377 seen_array_ref = false;
3378 for (ref = expr1->ref; ref; ref = ref->next)
3379 {
3380 if (ref->type == REF_ARRAY)
3381 seen_array_ref= true;
3382 else if (ref->type == REF_COMPONENT && seen_array_ref)
3383 return NULL;
3384 }
3385
3386 /* Check for a dependency. */
3387 if (gfc_check_fncall_dependency (expr1, INTENT_OUT,
3388 expr2->value.function.esym,
3389 expr2->value.function.actual))
3390 return NULL;
3391
3392 /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
3393 functions. */
3394 gcc_assert (expr2->value.function.isym
3395 || (gfc_return_by_reference (expr2->value.function.esym)
3396 && expr2->value.function.esym->result->attr.dimension));
3397
3398 ss = gfc_walk_expr (expr1);
3399 gcc_assert (ss != gfc_ss_terminator);
3400 gfc_init_se (&se, NULL);
3401 gfc_start_block (&se.pre);
3402 se.want_pointer = 1;
3403
3404 gfc_conv_array_parameter (&se, expr1, ss, 0);
3405
3406 se.direct_byref = 1;
3407 se.ss = gfc_walk_expr (expr2);
3408 gcc_assert (se.ss != gfc_ss_terminator);
3409 gfc_conv_function_expr (&se, expr2);
3410 gfc_add_block_to_block (&se.pre, &se.post);
3411
3412 return gfc_finish_block (&se.pre);
3413 }
3414
3415
3416 /* Translate an assignment. Most of the code is concerned with
3417 setting up the scalarizer. */
3418
3419 tree
3420 gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag)
3421 {
3422 gfc_se lse;
3423 gfc_se rse;
3424 gfc_ss *lss;
3425 gfc_ss *lss_section;
3426 gfc_ss *rss;
3427 gfc_loopinfo loop;
3428 tree tmp;
3429 stmtblock_t block;
3430 stmtblock_t body;
3431 bool l_is_temp;
3432
3433 /* Special case a single function returning an array. */
3434 if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
3435 {
3436 tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
3437 if (tmp)
3438 return tmp;
3439 }
3440
3441 /* Assignment of the form lhs = rhs. */
3442 gfc_start_block (&block);
3443
3444 gfc_init_se (&lse, NULL);
3445 gfc_init_se (&rse, NULL);
3446
3447 /* Walk the lhs. */
3448 lss = gfc_walk_expr (expr1);
3449 rss = NULL;
3450 if (lss != gfc_ss_terminator)
3451 {
3452 /* The assignment needs scalarization. */
3453 lss_section = lss;
3454
3455 /* Find a non-scalar SS from the lhs. */
3456 while (lss_section != gfc_ss_terminator
3457 && lss_section->type != GFC_SS_SECTION)
3458 lss_section = lss_section->next;
3459
3460 gcc_assert (lss_section != gfc_ss_terminator);
3461
3462 /* Initialize the scalarizer. */
3463 gfc_init_loopinfo (&loop);
3464
3465 /* Walk the rhs. */
3466 rss = gfc_walk_expr (expr2);
3467 if (rss == gfc_ss_terminator)
3468 {
3469 /* The rhs is scalar. Add a ss for the expression. */
3470 rss = gfc_get_ss ();
3471 rss->next = gfc_ss_terminator;
3472 rss->type = GFC_SS_SCALAR;
3473 rss->expr = expr2;
3474 }
3475 /* Associate the SS with the loop. */
3476 gfc_add_ss_to_loop (&loop, lss);
3477 gfc_add_ss_to_loop (&loop, rss);
3478
3479 /* Calculate the bounds of the scalarization. */
3480 gfc_conv_ss_startstride (&loop);
3481 /* Resolve any data dependencies in the statement. */
3482 gfc_conv_resolve_dependencies (&loop, lss, rss);
3483 /* Setup the scalarizing loops. */
3484 gfc_conv_loop_setup (&loop);
3485
3486 /* Setup the gfc_se structures. */
3487 gfc_copy_loopinfo_to_se (&lse, &loop);
3488 gfc_copy_loopinfo_to_se (&rse, &loop);
3489
3490 rse.ss = rss;
3491 gfc_mark_ss_chain_used (rss, 1);
3492 if (loop.temp_ss == NULL)
3493 {
3494 lse.ss = lss;
3495 gfc_mark_ss_chain_used (lss, 1);
3496 }
3497 else
3498 {
3499 lse.ss = loop.temp_ss;
3500 gfc_mark_ss_chain_used (lss, 3);
3501 gfc_mark_ss_chain_used (loop.temp_ss, 3);
3502 }
3503
3504 /* Start the scalarized loop body. */
3505 gfc_start_scalarized_body (&loop, &body);
3506 }
3507 else
3508 gfc_init_block (&body);
3509
3510 l_is_temp = (lss != gfc_ss_terminator && loop.temp_ss != NULL);
3511
3512 /* Translate the expression. */
3513 gfc_conv_expr (&rse, expr2);
3514
3515 if (l_is_temp)
3516 {
3517 gfc_conv_tmp_array_ref (&lse);
3518 gfc_advance_se_ss_chain (&lse);
3519 }
3520 else
3521 gfc_conv_expr (&lse, expr1);
3522
3523 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
3524 l_is_temp || init_flag,
3525 expr2->expr_type == EXPR_VARIABLE);
3526 gfc_add_expr_to_block (&body, tmp);
3527
3528 if (lss == gfc_ss_terminator)
3529 {
3530 /* Use the scalar assignment as is. */
3531 gfc_add_block_to_block (&block, &body);
3532 }
3533 else
3534 {
3535 gcc_assert (lse.ss == gfc_ss_terminator
3536 && rse.ss == gfc_ss_terminator);
3537
3538 if (l_is_temp)
3539 {
3540 gfc_trans_scalarized_loop_boundary (&loop, &body);
3541
3542 /* We need to copy the temporary to the actual lhs. */
3543 gfc_init_se (&lse, NULL);
3544 gfc_init_se (&rse, NULL);
3545 gfc_copy_loopinfo_to_se (&lse, &loop);
3546 gfc_copy_loopinfo_to_se (&rse, &loop);
3547
3548 rse.ss = loop.temp_ss;
3549 lse.ss = lss;
3550
3551 gfc_conv_tmp_array_ref (&rse);
3552 gfc_advance_se_ss_chain (&rse);
3553 gfc_conv_expr (&lse, expr1);
3554
3555 gcc_assert (lse.ss == gfc_ss_terminator
3556 && rse.ss == gfc_ss_terminator);
3557
3558 tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
3559 false, false);
3560 gfc_add_expr_to_block (&body, tmp);
3561 }
3562
3563 /* Generate the copying loops. */
3564 gfc_trans_scalarizing_loops (&loop, &body);
3565
3566 /* Wrap the whole thing up. */
3567 gfc_add_block_to_block (&block, &loop.pre);
3568 gfc_add_block_to_block (&block, &loop.post);
3569
3570 gfc_cleanup_loop (&loop);
3571 }
3572
3573 return gfc_finish_block (&block);
3574 }
3575
3576 tree
3577 gfc_trans_init_assign (gfc_code * code)
3578 {
3579 return gfc_trans_assignment (code->expr, code->expr2, true);
3580 }
3581
3582 tree
3583 gfc_trans_assign (gfc_code * code)
3584 {
3585 return gfc_trans_assignment (code->expr, code->expr2, false);
3586 }