OSDN Git Service

ec1e848509c8ce1b1ff4b3b082f675286d9d271f
[pf3gnuchains/gcc-fork.git] / gcc / fortran / trans-expr.c
1 /* Expression translation
2    Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
3    2011
4    Free Software Foundation, Inc.
5    Contributed by Paul Brook <paul@nowt.org>
6    and Steven Bosscher <s.bosscher@student.tudelft.nl>
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23
24 /* trans-expr.c-- generate GENERIC trees for gfc_expr.  */
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tree.h"
30 #include "diagnostic-core.h"    /* For fatal_error.  */
31 #include "langhooks.h"
32 #include "flags.h"
33 #include "gfortran.h"
34 #include "arith.h"
35 #include "constructor.h"
36 #include "trans.h"
37 #include "trans-const.h"
38 #include "trans-types.h"
39 #include "trans-array.h"
40 /* Only for gfc_trans_assign and gfc_trans_pointer_assign.  */
41 #include "trans-stmt.h"
42 #include "dependency.h"
43
44 static tree gfc_trans_structure_assign (tree dest, gfc_expr * expr);
45 static void gfc_apply_interface_mapping_to_expr (gfc_interface_mapping *,
46                                                  gfc_expr *);
47
48 /* Copy the scalarization loop variables.  */
49
50 static void
51 gfc_copy_se_loopvars (gfc_se * dest, gfc_se * src)
52 {
53   dest->ss = src->ss;
54   dest->loop = src->loop;
55 }
56
57
58 /* Initialize a simple expression holder.
59
60    Care must be taken when multiple se are created with the same parent.
61    The child se must be kept in sync.  The easiest way is to delay creation
62    of a child se until after after the previous se has been translated.  */
63
64 void
65 gfc_init_se (gfc_se * se, gfc_se * parent)
66 {
67   memset (se, 0, sizeof (gfc_se));
68   gfc_init_block (&se->pre);
69   gfc_init_block (&se->post);
70
71   se->parent = parent;
72
73   if (parent)
74     gfc_copy_se_loopvars (se, parent);
75 }
76
77
78 /* Advances to the next SS in the chain.  Use this rather than setting
79    se->ss = se->ss->next because all the parents needs to be kept in sync.
80    See gfc_init_se.  */
81
82 void
83 gfc_advance_se_ss_chain (gfc_se * se)
84 {
85   gfc_se *p;
86
87   gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
88
89   p = se;
90   /* Walk down the parent chain.  */
91   while (p != NULL)
92     {
93       /* Simple consistency check.  */
94       gcc_assert (p->parent == NULL || p->parent->ss == p->ss);
95
96       p->ss = p->ss->next;
97
98       p = p->parent;
99     }
100 }
101
102
103 /* Ensures the result of the expression as either a temporary variable
104    or a constant so that it can be used repeatedly.  */
105
106 void
107 gfc_make_safe_expr (gfc_se * se)
108 {
109   tree var;
110
111   if (CONSTANT_CLASS_P (se->expr))
112     return;
113
114   /* We need a temporary for this result.  */
115   var = gfc_create_var (TREE_TYPE (se->expr), NULL);
116   gfc_add_modify (&se->pre, var, se->expr);
117   se->expr = var;
118 }
119
120
121 /* Return an expression which determines if a dummy parameter is present.
122    Also used for arguments to procedures with multiple entry points.  */
123
124 tree
125 gfc_conv_expr_present (gfc_symbol * sym)
126 {
127   tree decl, cond;
128
129   gcc_assert (sym->attr.dummy);
130
131   decl = gfc_get_symbol_decl (sym);
132   if (TREE_CODE (decl) != PARM_DECL)
133     {
134       /* Array parameters use a temporary descriptor, we want the real
135          parameter.  */
136       gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
137              || GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
138       decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
139     }
140
141   cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, decl,
142                           fold_convert (TREE_TYPE (decl), null_pointer_node));
143
144   /* Fortran 2008 allows to pass null pointers and non-associated pointers
145      as actual argument to denote absent dummies. For array descriptors,
146      we thus also need to check the array descriptor.  */
147   if (!sym->attr.pointer && !sym->attr.allocatable
148       && sym->as && sym->as->type == AS_ASSUMED_SHAPE
149       && (gfc_option.allow_std & GFC_STD_F2008) != 0)
150     {
151       tree tmp;
152       tmp = build_fold_indirect_ref_loc (input_location, decl);
153       tmp = gfc_conv_array_data (tmp);
154       tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, tmp,
155                              fold_convert (TREE_TYPE (tmp), null_pointer_node));
156       cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
157                               boolean_type_node, cond, tmp);
158     }
159
160   return cond;
161 }
162
163
164 /* Converts a missing, dummy argument into a null or zero.  */
165
166 void
167 gfc_conv_missing_dummy (gfc_se * se, gfc_expr * arg, gfc_typespec ts, int kind)
168 {
169   tree present;
170   tree tmp;
171
172   present = gfc_conv_expr_present (arg->symtree->n.sym);
173
174   if (kind > 0)
175     {
176       /* Create a temporary and convert it to the correct type.  */
177       tmp = gfc_get_int_type (kind);
178       tmp = fold_convert (tmp, build_fold_indirect_ref_loc (input_location,
179                                                         se->expr));
180     
181       /* Test for a NULL value.  */
182       tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), present,
183                         tmp, fold_convert (TREE_TYPE (tmp), integer_one_node));
184       tmp = gfc_evaluate_now (tmp, &se->pre);
185       se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
186     }
187   else
188     {
189       tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se->expr),
190                         present, se->expr,
191                         build_zero_cst (TREE_TYPE (se->expr)));
192       tmp = gfc_evaluate_now (tmp, &se->pre);
193       se->expr = tmp;
194     }
195
196   if (ts.type == BT_CHARACTER)
197     {
198       tmp = build_int_cst (gfc_charlen_type_node, 0);
199       tmp = fold_build3_loc (input_location, COND_EXPR, gfc_charlen_type_node,
200                              present, se->string_length, tmp);
201       tmp = gfc_evaluate_now (tmp, &se->pre);
202       se->string_length = tmp;
203     }
204   return;
205 }
206
207
208 /* Get the character length of an expression, looking through gfc_refs
209    if necessary.  */
210
211 tree
212 gfc_get_expr_charlen (gfc_expr *e)
213 {
214   gfc_ref *r;
215   tree length;
216
217   gcc_assert (e->expr_type == EXPR_VARIABLE 
218               && e->ts.type == BT_CHARACTER);
219   
220   length = NULL; /* To silence compiler warning.  */
221
222   if (is_subref_array (e) && e->ts.u.cl->length)
223     {
224       gfc_se tmpse;
225       gfc_init_se (&tmpse, NULL);
226       gfc_conv_expr_type (&tmpse, e->ts.u.cl->length, gfc_charlen_type_node);
227       e->ts.u.cl->backend_decl = tmpse.expr;
228       return tmpse.expr;
229     }
230
231   /* First candidate: if the variable is of type CHARACTER, the
232      expression's length could be the length of the character
233      variable.  */
234   if (e->symtree->n.sym->ts.type == BT_CHARACTER)
235     length = e->symtree->n.sym->ts.u.cl->backend_decl;
236
237   /* Look through the reference chain for component references.  */
238   for (r = e->ref; r; r = r->next)
239     {
240       switch (r->type)
241         {
242         case REF_COMPONENT:
243           if (r->u.c.component->ts.type == BT_CHARACTER)
244             length = r->u.c.component->ts.u.cl->backend_decl;
245           break;
246
247         case REF_ARRAY:
248           /* Do nothing.  */
249           break;
250
251         default:
252           /* We should never got substring references here.  These will be
253              broken down by the scalarizer.  */
254           gcc_unreachable ();
255           break;
256         }
257     }
258
259   gcc_assert (length != NULL);
260   return length;
261 }
262
263
264 /* For each character array constructor subexpression without a ts.u.cl->length,
265    replace it by its first element (if there aren't any elements, the length
266    should already be set to zero).  */
267
268 static void
269 flatten_array_ctors_without_strlen (gfc_expr* e)
270 {
271   gfc_actual_arglist* arg;
272   gfc_constructor* c;
273
274   if (!e)
275     return;
276
277   switch (e->expr_type)
278     {
279
280     case EXPR_OP:
281       flatten_array_ctors_without_strlen (e->value.op.op1); 
282       flatten_array_ctors_without_strlen (e->value.op.op2); 
283       break;
284
285     case EXPR_COMPCALL:
286       /* TODO: Implement as with EXPR_FUNCTION when needed.  */
287       gcc_unreachable ();
288
289     case EXPR_FUNCTION:
290       for (arg = e->value.function.actual; arg; arg = arg->next)
291         flatten_array_ctors_without_strlen (arg->expr);
292       break;
293
294     case EXPR_ARRAY:
295
296       /* We've found what we're looking for.  */
297       if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
298         {
299           gfc_constructor *c;
300           gfc_expr* new_expr;
301
302           gcc_assert (e->value.constructor);
303
304           c = gfc_constructor_first (e->value.constructor);
305           new_expr = c->expr;
306           c->expr = NULL;
307
308           flatten_array_ctors_without_strlen (new_expr);
309           gfc_replace_expr (e, new_expr);
310           break;
311         }
312
313       /* Otherwise, fall through to handle constructor elements.  */
314     case EXPR_STRUCTURE:
315       for (c = gfc_constructor_first (e->value.constructor);
316            c; c = gfc_constructor_next (c))
317         flatten_array_ctors_without_strlen (c->expr);
318       break;
319
320     default:
321       break;
322
323     }
324 }
325
326
327 /* Generate code to initialize a string length variable. Returns the
328    value.  For array constructors, cl->length might be NULL and in this case,
329    the first element of the constructor is needed.  expr is the original
330    expression so we can access it but can be NULL if this is not needed.  */
331
332 void
333 gfc_conv_string_length (gfc_charlen * cl, gfc_expr * expr, stmtblock_t * pblock)
334 {
335   gfc_se se;
336
337   gfc_init_se (&se, NULL);
338
339   if (!cl->length
340         && cl->backend_decl
341         && TREE_CODE (cl->backend_decl) == VAR_DECL)
342     return;
343
344   /* If cl->length is NULL, use gfc_conv_expr to obtain the string length but
345      "flatten" array constructors by taking their first element; all elements
346      should be the same length or a cl->length should be present.  */
347   if (!cl->length)
348     {
349       gfc_expr* expr_flat;
350       gcc_assert (expr);
351       expr_flat = gfc_copy_expr (expr);
352       flatten_array_ctors_without_strlen (expr_flat);
353       gfc_resolve_expr (expr_flat);
354
355       gfc_conv_expr (&se, expr_flat);
356       gfc_add_block_to_block (pblock, &se.pre);
357       cl->backend_decl = convert (gfc_charlen_type_node, se.string_length);
358
359       gfc_free_expr (expr_flat);
360       return;
361     }
362
363   /* Convert cl->length.  */
364
365   gcc_assert (cl->length);
366
367   gfc_conv_expr_type (&se, cl->length, gfc_charlen_type_node);
368   se.expr = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
369                              se.expr, build_int_cst (gfc_charlen_type_node, 0));
370   gfc_add_block_to_block (pblock, &se.pre);
371
372   if (cl->backend_decl)
373     gfc_add_modify (pblock, cl->backend_decl, se.expr);
374   else
375     cl->backend_decl = gfc_evaluate_now (se.expr, pblock);
376 }
377
378
379 static void
380 gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
381                     const char *name, locus *where)
382 {
383   tree tmp;
384   tree type;
385   tree fault;
386   gfc_se start;
387   gfc_se end;
388   char *msg;
389
390   type = gfc_get_character_type (kind, ref->u.ss.length);
391   type = build_pointer_type (type);
392
393   gfc_init_se (&start, se);
394   gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
395   gfc_add_block_to_block (&se->pre, &start.pre);
396
397   if (integer_onep (start.expr))
398     gfc_conv_string_parameter (se);
399   else
400     {
401       tmp = start.expr;
402       STRIP_NOPS (tmp);
403       /* Avoid multiple evaluation of substring start.  */
404       if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
405         start.expr = gfc_evaluate_now (start.expr, &se->pre);
406
407       /* Change the start of the string.  */
408       if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
409         tmp = se->expr;
410       else
411         tmp = build_fold_indirect_ref_loc (input_location,
412                                        se->expr);
413       tmp = gfc_build_array_ref (tmp, start.expr, NULL);
414       se->expr = gfc_build_addr_expr (type, tmp);
415     }
416
417   /* Length = end + 1 - start.  */
418   gfc_init_se (&end, se);
419   if (ref->u.ss.end == NULL)
420     end.expr = se->string_length;
421   else
422     {
423       gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
424       gfc_add_block_to_block (&se->pre, &end.pre);
425     }
426   tmp = end.expr;
427   STRIP_NOPS (tmp);
428   if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
429     end.expr = gfc_evaluate_now (end.expr, &se->pre);
430
431   if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
432     {
433       tree nonempty = fold_build2_loc (input_location, LE_EXPR,
434                                        boolean_type_node, start.expr,
435                                        end.expr);
436
437       /* Check lower bound.  */
438       fault = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
439                                start.expr,
440                                build_int_cst (gfc_charlen_type_node, 1));
441       fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
442                                boolean_type_node, nonempty, fault);
443       if (name)
444         asprintf (&msg, "Substring out of bounds: lower bound (%%ld) of '%s' "
445                   "is less than one", name);
446       else
447         asprintf (&msg, "Substring out of bounds: lower bound (%%ld)"
448                   "is less than one");
449       gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
450                                fold_convert (long_integer_type_node,
451                                              start.expr));
452       gfc_free (msg);
453
454       /* Check upper bound.  */
455       fault = fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
456                                end.expr, se->string_length);
457       fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
458                                boolean_type_node, nonempty, fault);
459       if (name)
460         asprintf (&msg, "Substring out of bounds: upper bound (%%ld) of '%s' "
461                   "exceeds string length (%%ld)", name);
462       else
463         asprintf (&msg, "Substring out of bounds: upper bound (%%ld) "
464                   "exceeds string length (%%ld)");
465       gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
466                                fold_convert (long_integer_type_node, end.expr),
467                                fold_convert (long_integer_type_node,
468                                              se->string_length));
469       gfc_free (msg);
470     }
471
472   /* If the start and end expressions are equal, the length is one.  */
473   if (ref->u.ss.end
474       && gfc_dep_compare_expr (ref->u.ss.start, ref->u.ss.end) == 0)
475     tmp = build_int_cst (gfc_charlen_type_node, 1);
476   else
477     {
478       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_charlen_type_node,
479                              end.expr, start.expr);
480       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_charlen_type_node,
481                              build_int_cst (gfc_charlen_type_node, 1), tmp);
482       tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
483                              tmp, build_int_cst (gfc_charlen_type_node, 0));
484     }
485
486   se->string_length = tmp;
487 }
488
489
490 /* Convert a derived type component reference.  */
491
492 static void
493 gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
494 {
495   gfc_component *c;
496   tree tmp;
497   tree decl;
498   tree field;
499
500   c = ref->u.c.component;
501
502   gcc_assert (c->backend_decl);
503
504   field = c->backend_decl;
505   gcc_assert (TREE_CODE (field) == FIELD_DECL);
506   decl = se->expr;
507   tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
508                          decl, field, NULL_TREE);
509
510   se->expr = tmp;
511
512   if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer)
513     {
514       tmp = c->ts.u.cl->backend_decl;
515       /* Components must always be constant length.  */
516       gcc_assert (tmp && INTEGER_CST_P (tmp));
517       se->string_length = tmp;
518     }
519
520   if (((c->attr.pointer || c->attr.allocatable) && c->attr.dimension == 0
521        && c->ts.type != BT_CHARACTER)
522       || c->attr.proc_pointer)
523     se->expr = build_fold_indirect_ref_loc (input_location,
524                                         se->expr);
525 }
526
527
528 /* This function deals with component references to components of the
529    parent type for derived type extensons.  */
530 static void
531 conv_parent_component_references (gfc_se * se, gfc_ref * ref)
532 {
533   gfc_component *c;
534   gfc_component *cmp;
535   gfc_symbol *dt;
536   gfc_ref parent;
537
538   dt = ref->u.c.sym;
539   c = ref->u.c.component;
540
541   /* Build a gfc_ref to recursively call gfc_conv_component_ref.  */
542   parent.type = REF_COMPONENT;
543   parent.next = NULL;
544   parent.u.c.sym = dt;
545   parent.u.c.component = dt->components;
546
547   if (dt->backend_decl == NULL)
548     gfc_get_derived_type (dt);
549
550   if (dt->attr.extension && dt->components)
551     {
552       if (dt->attr.is_class)
553         cmp = dt->components;
554       else
555         cmp = dt->components->next;
556       /* Return if the component is not in the parent type.  */
557       for (; cmp; cmp = cmp->next)
558         if (strcmp (c->name, cmp->name) == 0)
559           return;
560         
561       /* Otherwise build the reference and call self.  */
562       gfc_conv_component_ref (se, &parent);
563       parent.u.c.sym = dt->components->ts.u.derived;
564       parent.u.c.component = c;
565       conv_parent_component_references (se, &parent);
566     }
567 }
568
569 /* Return the contents of a variable. Also handles reference/pointer
570    variables (all Fortran pointer references are implicit).  */
571
572 static void
573 gfc_conv_variable (gfc_se * se, gfc_expr * expr)
574 {
575   gfc_ref *ref;
576   gfc_symbol *sym;
577   tree parent_decl = NULL_TREE;
578   int parent_flag;
579   bool return_value;
580   bool alternate_entry;
581   bool entry_master;
582
583   sym = expr->symtree->n.sym;
584   if (se->ss != NULL)
585     {
586       /* Check that something hasn't gone horribly wrong.  */
587       gcc_assert (se->ss != gfc_ss_terminator);
588       gcc_assert (se->ss->expr == expr);
589
590       /* A scalarized term.  We already know the descriptor.  */
591       se->expr = se->ss->data.info.descriptor;
592       se->string_length = se->ss->string_length;
593       for (ref = se->ss->data.info.ref; ref; ref = ref->next)
594         if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
595           break;
596     }
597   else
598     {
599       tree se_expr = NULL_TREE;
600
601       se->expr = gfc_get_symbol_decl (sym);
602
603       /* Deal with references to a parent results or entries by storing
604          the current_function_decl and moving to the parent_decl.  */
605       return_value = sym->attr.function && sym->result == sym;
606       alternate_entry = sym->attr.function && sym->attr.entry
607                         && sym->result == sym;
608       entry_master = sym->attr.result
609                      && sym->ns->proc_name->attr.entry_master
610                      && !gfc_return_by_reference (sym->ns->proc_name);
611       if (current_function_decl)
612         parent_decl = DECL_CONTEXT (current_function_decl);
613
614       if ((se->expr == parent_decl && return_value)
615            || (sym->ns && sym->ns->proc_name
616                && parent_decl
617                && sym->ns->proc_name->backend_decl == parent_decl
618                && (alternate_entry || entry_master)))
619         parent_flag = 1;
620       else
621         parent_flag = 0;
622
623       /* Special case for assigning the return value of a function.
624          Self recursive functions must have an explicit return value.  */
625       if (return_value && (se->expr == current_function_decl || parent_flag))
626         se_expr = gfc_get_fake_result_decl (sym, parent_flag);
627
628       /* Similarly for alternate entry points.  */
629       else if (alternate_entry 
630                && (sym->ns->proc_name->backend_decl == current_function_decl
631                    || parent_flag))
632         {
633           gfc_entry_list *el = NULL;
634
635           for (el = sym->ns->entries; el; el = el->next)
636             if (sym == el->sym)
637               {
638                 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
639                 break;
640               }
641         }
642
643       else if (entry_master
644                && (sym->ns->proc_name->backend_decl == current_function_decl
645                    || parent_flag))
646         se_expr = gfc_get_fake_result_decl (sym, parent_flag);
647
648       if (se_expr)
649         se->expr = se_expr;
650
651       /* Procedure actual arguments.  */
652       else if (sym->attr.flavor == FL_PROCEDURE
653                && se->expr != current_function_decl)
654         {
655           if (!sym->attr.dummy && !sym->attr.proc_pointer)
656             {
657               gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
658               se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
659             }
660           return;
661         }
662
663
664       /* Dereference the expression, where needed. Since characters
665          are entirely different from other types, they are treated 
666          separately.  */
667       if (sym->ts.type == BT_CHARACTER)
668         {
669           /* Dereference character pointer dummy arguments
670              or results.  */
671           if ((sym->attr.pointer || sym->attr.allocatable)
672               && (sym->attr.dummy
673                   || sym->attr.function
674                   || sym->attr.result))
675             se->expr = build_fold_indirect_ref_loc (input_location,
676                                                 se->expr);
677
678         }
679       else if (!sym->attr.value)
680         {
681           /* Dereference non-character scalar dummy arguments.  */
682           if (sym->attr.dummy && !sym->attr.dimension)
683             se->expr = build_fold_indirect_ref_loc (input_location,
684                                                 se->expr);
685
686           /* Dereference scalar hidden result.  */
687           if (gfc_option.flag_f2c && sym->ts.type == BT_COMPLEX
688               && (sym->attr.function || sym->attr.result)
689               && !sym->attr.dimension && !sym->attr.pointer
690               && !sym->attr.always_explicit)
691             se->expr = build_fold_indirect_ref_loc (input_location,
692                                                 se->expr);
693
694           /* Dereference non-character pointer variables. 
695              These must be dummies, results, or scalars.  */
696           if ((sym->attr.pointer || sym->attr.allocatable
697                || gfc_is_associate_pointer (sym))
698               && (sym->attr.dummy
699                   || sym->attr.function
700                   || sym->attr.result
701                   || !sym->attr.dimension))
702             se->expr = build_fold_indirect_ref_loc (input_location,
703                                                 se->expr);
704         }
705
706       ref = expr->ref;
707     }
708
709   /* For character variables, also get the length.  */
710   if (sym->ts.type == BT_CHARACTER)
711     {
712       /* If the character length of an entry isn't set, get the length from
713          the master function instead.  */
714       if (sym->attr.entry && !sym->ts.u.cl->backend_decl)
715         se->string_length = sym->ns->proc_name->ts.u.cl->backend_decl;
716       else
717         se->string_length = sym->ts.u.cl->backend_decl;
718       gcc_assert (se->string_length);
719     }
720
721   while (ref)
722     {
723       switch (ref->type)
724         {
725         case REF_ARRAY:
726           /* Return the descriptor if that's what we want and this is an array
727              section reference.  */
728           if (se->descriptor_only && ref->u.ar.type != AR_ELEMENT)
729             return;
730 /* TODO: Pointers to single elements of array sections, eg elemental subs.  */
731           /* Return the descriptor for array pointers and allocations.  */
732           if (se->want_pointer
733               && ref->next == NULL && (se->descriptor_only))
734             return;
735
736           gfc_conv_array_ref (se, &ref->u.ar, sym, &expr->where);
737           /* Return a pointer to an element.  */
738           break;
739
740         case REF_COMPONENT:
741           if (ref->u.c.sym->attr.extension)
742             conv_parent_component_references (se, ref);
743
744           gfc_conv_component_ref (se, ref);
745           break;
746
747         case REF_SUBSTRING:
748           gfc_conv_substring (se, ref, expr->ts.kind,
749                               expr->symtree->name, &expr->where);
750           break;
751
752         default:
753           gcc_unreachable ();
754           break;
755         }
756       ref = ref->next;
757     }
758   /* Pointer assignment, allocation or pass by reference.  Arrays are handled
759      separately.  */
760   if (se->want_pointer)
761     {
762       if (expr->ts.type == BT_CHARACTER && !gfc_is_proc_ptr_comp (expr, NULL))
763         gfc_conv_string_parameter (se);
764       else 
765         se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
766     }
767 }
768
769
770 /* Unary ops are easy... Or they would be if ! was a valid op.  */
771
772 static void
773 gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
774 {
775   gfc_se operand;
776   tree type;
777
778   gcc_assert (expr->ts.type != BT_CHARACTER);
779   /* Initialize the operand.  */
780   gfc_init_se (&operand, se);
781   gfc_conv_expr_val (&operand, expr->value.op.op1);
782   gfc_add_block_to_block (&se->pre, &operand.pre);
783
784   type = gfc_typenode_for_spec (&expr->ts);
785
786   /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
787      We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
788      All other unary operators have an equivalent GIMPLE unary operator.  */
789   if (code == TRUTH_NOT_EXPR)
790     se->expr = fold_build2_loc (input_location, EQ_EXPR, type, operand.expr,
791                                 build_int_cst (type, 0));
792   else
793     se->expr = fold_build1_loc (input_location, code, type, operand.expr);
794
795 }
796
797 /* Expand power operator to optimal multiplications when a value is raised
798    to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
799    Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
800    Programming", 3rd Edition, 1998.  */
801
802 /* This code is mostly duplicated from expand_powi in the backend.
803    We establish the "optimal power tree" lookup table with the defined size.
804    The items in the table are the exponents used to calculate the index
805    exponents. Any integer n less than the value can get an "addition chain",
806    with the first node being one.  */
807 #define POWI_TABLE_SIZE 256
808
809 /* The table is from builtins.c.  */
810 static const unsigned char powi_table[POWI_TABLE_SIZE] =
811   {
812       0,   1,   1,   2,   2,   3,   3,   4,  /*   0 -   7 */
813       4,   6,   5,   6,   6,  10,   7,   9,  /*   8 -  15 */
814       8,  16,   9,  16,  10,  12,  11,  13,  /*  16 -  23 */
815      12,  17,  13,  18,  14,  24,  15,  26,  /*  24 -  31 */
816      16,  17,  17,  19,  18,  33,  19,  26,  /*  32 -  39 */
817      20,  25,  21,  40,  22,  27,  23,  44,  /*  40 -  47 */
818      24,  32,  25,  34,  26,  29,  27,  44,  /*  48 -  55 */
819      28,  31,  29,  34,  30,  60,  31,  36,  /*  56 -  63 */
820      32,  64,  33,  34,  34,  46,  35,  37,  /*  64 -  71 */
821      36,  65,  37,  50,  38,  48,  39,  69,  /*  72 -  79 */
822      40,  49,  41,  43,  42,  51,  43,  58,  /*  80 -  87 */
823      44,  64,  45,  47,  46,  59,  47,  76,  /*  88 -  95 */
824      48,  65,  49,  66,  50,  67,  51,  66,  /*  96 - 103 */
825      52,  70,  53,  74,  54, 104,  55,  74,  /* 104 - 111 */
826      56,  64,  57,  69,  58,  78,  59,  68,  /* 112 - 119 */
827      60,  61,  61,  80,  62,  75,  63,  68,  /* 120 - 127 */
828      64,  65,  65, 128,  66, 129,  67,  90,  /* 128 - 135 */
829      68,  73,  69, 131,  70,  94,  71,  88,  /* 136 - 143 */
830      72, 128,  73,  98,  74, 132,  75, 121,  /* 144 - 151 */
831      76, 102,  77, 124,  78, 132,  79, 106,  /* 152 - 159 */
832      80,  97,  81, 160,  82,  99,  83, 134,  /* 160 - 167 */
833      84,  86,  85,  95,  86, 160,  87, 100,  /* 168 - 175 */
834      88, 113,  89,  98,  90, 107,  91, 122,  /* 176 - 183 */
835      92, 111,  93, 102,  94, 126,  95, 150,  /* 184 - 191 */
836      96, 128,  97, 130,  98, 133,  99, 195,  /* 192 - 199 */
837     100, 128, 101, 123, 102, 164, 103, 138,  /* 200 - 207 */
838     104, 145, 105, 146, 106, 109, 107, 149,  /* 208 - 215 */
839     108, 200, 109, 146, 110, 170, 111, 157,  /* 216 - 223 */
840     112, 128, 113, 130, 114, 182, 115, 132,  /* 224 - 231 */
841     116, 200, 117, 132, 118, 158, 119, 206,  /* 232 - 239 */
842     120, 240, 121, 162, 122, 147, 123, 152,  /* 240 - 247 */
843     124, 166, 125, 214, 126, 138, 127, 153,  /* 248 - 255 */
844   };
845
846 /* If n is larger than lookup table's max index, we use the "window 
847    method".  */
848 #define POWI_WINDOW_SIZE 3
849
850 /* Recursive function to expand the power operator. The temporary 
851    values are put in tmpvar. The function returns tmpvar[1] ** n.  */
852 static tree
853 gfc_conv_powi (gfc_se * se, unsigned HOST_WIDE_INT n, tree * tmpvar)
854 {
855   tree op0;
856   tree op1;
857   tree tmp;
858   int digit;
859
860   if (n < POWI_TABLE_SIZE)
861     {
862       if (tmpvar[n])
863         return tmpvar[n];
864
865       op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
866       op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
867     }
868   else if (n & 1)
869     {
870       digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
871       op0 = gfc_conv_powi (se, n - digit, tmpvar);
872       op1 = gfc_conv_powi (se, digit, tmpvar);
873     }
874   else
875     {
876       op0 = gfc_conv_powi (se, n >> 1, tmpvar);
877       op1 = op0;
878     }
879
880   tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (op0), op0, op1);
881   tmp = gfc_evaluate_now (tmp, &se->pre);
882
883   if (n < POWI_TABLE_SIZE)
884     tmpvar[n] = tmp;
885
886   return tmp;
887 }
888
889
890 /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
891    return 1. Else return 0 and a call to runtime library functions
892    will have to be built.  */
893 static int
894 gfc_conv_cst_int_power (gfc_se * se, tree lhs, tree rhs)
895 {
896   tree cond;
897   tree tmp;
898   tree type;
899   tree vartmp[POWI_TABLE_SIZE];
900   HOST_WIDE_INT m;
901   unsigned HOST_WIDE_INT n;
902   int sgn;
903
904   /* If exponent is too large, we won't expand it anyway, so don't bother
905      with large integer values.  */
906   if (!double_int_fits_in_shwi_p (TREE_INT_CST (rhs)))
907     return 0;
908
909   m = double_int_to_shwi (TREE_INT_CST (rhs));
910   /* There's no ABS for HOST_WIDE_INT, so here we go. It also takes care
911      of the asymmetric range of the integer type.  */
912   n = (unsigned HOST_WIDE_INT) (m < 0 ? -m : m);
913   
914   type = TREE_TYPE (lhs);
915   sgn = tree_int_cst_sgn (rhs);
916
917   if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
918        || optimize_size) && (m > 2 || m < -1))
919     return 0;
920
921   /* rhs == 0  */
922   if (sgn == 0)
923     {
924       se->expr = gfc_build_const (type, integer_one_node);
925       return 1;
926     }
927
928   /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1.  */
929   if ((sgn == -1) && (TREE_CODE (type) == INTEGER_TYPE))
930     {
931       tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
932                              lhs, build_int_cst (TREE_TYPE (lhs), -1));
933       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
934                               lhs, build_int_cst (TREE_TYPE (lhs), 1));
935
936       /* If rhs is even,
937          result = (lhs == 1 || lhs == -1) ? 1 : 0.  */
938       if ((n & 1) == 0)
939         {
940           tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR,
941                                  boolean_type_node, tmp, cond);
942           se->expr = fold_build3_loc (input_location, COND_EXPR, type,
943                                       tmp, build_int_cst (type, 1),
944                                       build_int_cst (type, 0));
945           return 1;
946         }
947       /* If rhs is odd,
948          result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0.  */
949       tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp,
950                              build_int_cst (type, -1),
951                              build_int_cst (type, 0));
952       se->expr = fold_build3_loc (input_location, COND_EXPR, type,
953                                   cond, build_int_cst (type, 1), tmp);
954       return 1;
955     }
956
957   memset (vartmp, 0, sizeof (vartmp));
958   vartmp[1] = lhs;
959   if (sgn == -1)
960     {
961       tmp = gfc_build_const (type, integer_one_node);
962       vartmp[1] = fold_build2_loc (input_location, RDIV_EXPR, type, tmp,
963                                    vartmp[1]);
964     }
965
966   se->expr = gfc_conv_powi (se, n, vartmp);
967
968   return 1;
969 }
970
971
972 /* Power op (**).  Constant integer exponent has special handling.  */
973
974 static void
975 gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
976 {
977   tree gfc_int4_type_node;
978   int kind;
979   int ikind;
980   int res_ikind_1, res_ikind_2;
981   gfc_se lse;
982   gfc_se rse;
983   tree fndecl = NULL;
984
985   gfc_init_se (&lse, se);
986   gfc_conv_expr_val (&lse, expr->value.op.op1);
987   lse.expr = gfc_evaluate_now (lse.expr, &lse.pre);
988   gfc_add_block_to_block (&se->pre, &lse.pre);
989
990   gfc_init_se (&rse, se);
991   gfc_conv_expr_val (&rse, expr->value.op.op2);
992   gfc_add_block_to_block (&se->pre, &rse.pre);
993
994   if (expr->value.op.op2->ts.type == BT_INTEGER
995       && expr->value.op.op2->expr_type == EXPR_CONSTANT)
996     if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
997       return;
998
999   gfc_int4_type_node = gfc_get_int_type (4);
1000
1001   /* In case of integer operands with kinds 1 or 2, we call the integer kind 4
1002      library routine.  But in the end, we have to convert the result back
1003      if this case applies -- with res_ikind_K, we keep track whether operand K
1004      falls into this case.  */
1005   res_ikind_1 = -1;
1006   res_ikind_2 = -1;
1007
1008   kind = expr->value.op.op1->ts.kind;
1009   switch (expr->value.op.op2->ts.type)
1010     {
1011     case BT_INTEGER:
1012       ikind = expr->value.op.op2->ts.kind;
1013       switch (ikind)
1014         {
1015         case 1:
1016         case 2:
1017           rse.expr = convert (gfc_int4_type_node, rse.expr);
1018           res_ikind_2 = ikind;
1019           /* Fall through.  */
1020
1021         case 4:
1022           ikind = 0;
1023           break;
1024           
1025         case 8:
1026           ikind = 1;
1027           break;
1028
1029         case 16:
1030           ikind = 2;
1031           break;
1032
1033         default:
1034           gcc_unreachable ();
1035         }
1036       switch (kind)
1037         {
1038         case 1:
1039         case 2:
1040           if (expr->value.op.op1->ts.type == BT_INTEGER)
1041             {
1042               lse.expr = convert (gfc_int4_type_node, lse.expr);
1043               res_ikind_1 = kind;
1044             }
1045           else
1046             gcc_unreachable ();
1047           /* Fall through.  */
1048
1049         case 4:
1050           kind = 0;
1051           break;
1052           
1053         case 8:
1054           kind = 1;
1055           break;
1056
1057         case 10:
1058           kind = 2;
1059           break;
1060
1061         case 16:
1062           kind = 3;
1063           break;
1064
1065         default:
1066           gcc_unreachable ();
1067         }
1068       
1069       switch (expr->value.op.op1->ts.type)
1070         {
1071         case BT_INTEGER:
1072           if (kind == 3) /* Case 16 was not handled properly above.  */
1073             kind = 2;
1074           fndecl = gfor_fndecl_math_powi[kind][ikind].integer;
1075           break;
1076
1077         case BT_REAL:
1078           /* Use builtins for real ** int4.  */
1079           if (ikind == 0)
1080             {
1081               switch (kind)
1082                 {
1083                 case 0:
1084                   fndecl = built_in_decls[BUILT_IN_POWIF];
1085                   break;
1086                 
1087                 case 1:
1088                   fndecl = built_in_decls[BUILT_IN_POWI];
1089                   break;
1090
1091                 case 2:
1092                   fndecl = built_in_decls[BUILT_IN_POWIL];
1093                   break;
1094
1095                 case 3:
1096                   /* Use the __builtin_powil() only if real(kind=16) is 
1097                      actually the C long double type.  */
1098                   if (!gfc_real16_is_float128)
1099                     fndecl = built_in_decls[BUILT_IN_POWIL];
1100                   break;
1101
1102                 default:
1103                   gcc_unreachable ();
1104                 }
1105             }
1106
1107           /* If we don't have a good builtin for this, go for the 
1108              library function.  */
1109           if (!fndecl)
1110             fndecl = gfor_fndecl_math_powi[kind][ikind].real;
1111           break;
1112
1113         case BT_COMPLEX:
1114           fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
1115           break;
1116
1117         default:
1118           gcc_unreachable ();
1119         }
1120       break;
1121
1122     case BT_REAL:
1123       fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_POW, kind);
1124       break;
1125
1126     case BT_COMPLEX:
1127       fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_CPOW, kind);
1128       break;
1129
1130     default:
1131       gcc_unreachable ();
1132       break;
1133     }
1134
1135   se->expr = build_call_expr_loc (input_location,
1136                               fndecl, 2, lse.expr, rse.expr);
1137
1138   /* Convert the result back if it is of wrong integer kind.  */
1139   if (res_ikind_1 != -1 && res_ikind_2 != -1)
1140     {
1141       /* We want the maximum of both operand kinds as result.  */
1142       if (res_ikind_1 < res_ikind_2)
1143         res_ikind_1 = res_ikind_2;
1144       se->expr = convert (gfc_get_int_type (res_ikind_1), se->expr);
1145     }
1146 }
1147
1148
1149 /* Generate code to allocate a string temporary.  */
1150
1151 tree
1152 gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
1153 {
1154   tree var;
1155   tree tmp;
1156
1157   if (gfc_can_put_var_on_stack (len))
1158     {
1159       /* Create a temporary variable to hold the result.  */
1160       tmp = fold_build2_loc (input_location, MINUS_EXPR,
1161                              gfc_charlen_type_node, len,
1162                              build_int_cst (gfc_charlen_type_node, 1));
1163       tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node, tmp);
1164
1165       if (TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
1166         tmp = build_array_type (TREE_TYPE (TREE_TYPE (type)), tmp);
1167       else
1168         tmp = build_array_type (TREE_TYPE (type), tmp);
1169
1170       var = gfc_create_var (tmp, "str");
1171       var = gfc_build_addr_expr (type, var);
1172     }
1173   else
1174     {
1175       /* Allocate a temporary to hold the result.  */
1176       var = gfc_create_var (type, "pstr");
1177       tmp = gfc_call_malloc (&se->pre, type,
1178                              fold_build2_loc (input_location, MULT_EXPR,
1179                                               TREE_TYPE (len), len,
1180                                               fold_convert (TREE_TYPE (len),
1181                                                             TYPE_SIZE (type))));
1182       gfc_add_modify (&se->pre, var, tmp);
1183
1184       /* Free the temporary afterwards.  */
1185       tmp = gfc_call_free (convert (pvoid_type_node, var));
1186       gfc_add_expr_to_block (&se->post, tmp);
1187     }
1188
1189   return var;
1190 }
1191
1192
1193 /* Handle a string concatenation operation.  A temporary will be allocated to
1194    hold the result.  */
1195
1196 static void
1197 gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
1198 {
1199   gfc_se lse, rse;
1200   tree len, type, var, tmp, fndecl;
1201
1202   gcc_assert (expr->value.op.op1->ts.type == BT_CHARACTER
1203               && expr->value.op.op2->ts.type == BT_CHARACTER);
1204   gcc_assert (expr->value.op.op1->ts.kind == expr->value.op.op2->ts.kind);
1205
1206   gfc_init_se (&lse, se);
1207   gfc_conv_expr (&lse, expr->value.op.op1);
1208   gfc_conv_string_parameter (&lse);
1209   gfc_init_se (&rse, se);
1210   gfc_conv_expr (&rse, expr->value.op.op2);
1211   gfc_conv_string_parameter (&rse);
1212
1213   gfc_add_block_to_block (&se->pre, &lse.pre);
1214   gfc_add_block_to_block (&se->pre, &rse.pre);
1215
1216   type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
1217   len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
1218   if (len == NULL_TREE)
1219     {
1220       len = fold_build2_loc (input_location, PLUS_EXPR,
1221                              TREE_TYPE (lse.string_length),
1222                              lse.string_length, rse.string_length);
1223     }
1224
1225   type = build_pointer_type (type);
1226
1227   var = gfc_conv_string_tmp (se, type, len);
1228
1229   /* Do the actual concatenation.  */
1230   if (expr->ts.kind == 1)
1231     fndecl = gfor_fndecl_concat_string;
1232   else if (expr->ts.kind == 4)
1233     fndecl = gfor_fndecl_concat_string_char4;
1234   else
1235     gcc_unreachable ();
1236
1237   tmp = build_call_expr_loc (input_location,
1238                          fndecl, 6, len, var, lse.string_length, lse.expr,
1239                          rse.string_length, rse.expr);
1240   gfc_add_expr_to_block (&se->pre, tmp);
1241
1242   /* Add the cleanup for the operands.  */
1243   gfc_add_block_to_block (&se->pre, &rse.post);
1244   gfc_add_block_to_block (&se->pre, &lse.post);
1245
1246   se->expr = var;
1247   se->string_length = len;
1248 }
1249
1250 /* Translates an op expression. Common (binary) cases are handled by this
1251    function, others are passed on. Recursion is used in either case.
1252    We use the fact that (op1.ts == op2.ts) (except for the power
1253    operator **).
1254    Operators need no special handling for scalarized expressions as long as
1255    they call gfc_conv_simple_val to get their operands.
1256    Character strings get special handling.  */
1257
1258 static void
1259 gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
1260 {
1261   enum tree_code code;
1262   gfc_se lse;
1263   gfc_se rse;
1264   tree tmp, type;
1265   int lop;
1266   int checkstring;
1267
1268   checkstring = 0;
1269   lop = 0;
1270   switch (expr->value.op.op)
1271     {
1272     case INTRINSIC_PARENTHESES:
1273       if ((expr->ts.type == BT_REAL
1274            || expr->ts.type == BT_COMPLEX)
1275           && gfc_option.flag_protect_parens)
1276         {
1277           gfc_conv_unary_op (PAREN_EXPR, se, expr);
1278           gcc_assert (FLOAT_TYPE_P (TREE_TYPE (se->expr)));
1279           return;
1280         }
1281
1282       /* Fallthrough.  */
1283     case INTRINSIC_UPLUS:
1284       gfc_conv_expr (se, expr->value.op.op1);
1285       return;
1286
1287     case INTRINSIC_UMINUS:
1288       gfc_conv_unary_op (NEGATE_EXPR, se, expr);
1289       return;
1290
1291     case INTRINSIC_NOT:
1292       gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
1293       return;
1294
1295     case INTRINSIC_PLUS:
1296       code = PLUS_EXPR;
1297       break;
1298
1299     case INTRINSIC_MINUS:
1300       code = MINUS_EXPR;
1301       break;
1302
1303     case INTRINSIC_TIMES:
1304       code = MULT_EXPR;
1305       break;
1306
1307     case INTRINSIC_DIVIDE:
1308       /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
1309          an integer, we must round towards zero, so we use a
1310          TRUNC_DIV_EXPR.  */
1311       if (expr->ts.type == BT_INTEGER)
1312         code = TRUNC_DIV_EXPR;
1313       else
1314         code = RDIV_EXPR;
1315       break;
1316
1317     case INTRINSIC_POWER:
1318       gfc_conv_power_op (se, expr);
1319       return;
1320
1321     case INTRINSIC_CONCAT:
1322       gfc_conv_concat_op (se, expr);
1323       return;
1324
1325     case INTRINSIC_AND:
1326       code = TRUTH_ANDIF_EXPR;
1327       lop = 1;
1328       break;
1329
1330     case INTRINSIC_OR:
1331       code = TRUTH_ORIF_EXPR;
1332       lop = 1;
1333       break;
1334
1335       /* EQV and NEQV only work on logicals, but since we represent them
1336          as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE.  */
1337     case INTRINSIC_EQ:
1338     case INTRINSIC_EQ_OS:
1339     case INTRINSIC_EQV:
1340       code = EQ_EXPR;
1341       checkstring = 1;
1342       lop = 1;
1343       break;
1344
1345     case INTRINSIC_NE:
1346     case INTRINSIC_NE_OS:
1347     case INTRINSIC_NEQV:
1348       code = NE_EXPR;
1349       checkstring = 1;
1350       lop = 1;
1351       break;
1352
1353     case INTRINSIC_GT:
1354     case INTRINSIC_GT_OS:
1355       code = GT_EXPR;
1356       checkstring = 1;
1357       lop = 1;
1358       break;
1359
1360     case INTRINSIC_GE:
1361     case INTRINSIC_GE_OS:
1362       code = GE_EXPR;
1363       checkstring = 1;
1364       lop = 1;
1365       break;
1366
1367     case INTRINSIC_LT:
1368     case INTRINSIC_LT_OS:
1369       code = LT_EXPR;
1370       checkstring = 1;
1371       lop = 1;
1372       break;
1373
1374     case INTRINSIC_LE:
1375     case INTRINSIC_LE_OS:
1376       code = LE_EXPR;
1377       checkstring = 1;
1378       lop = 1;
1379       break;
1380
1381     case INTRINSIC_USER:
1382     case INTRINSIC_ASSIGN:
1383       /* These should be converted into function calls by the frontend.  */
1384       gcc_unreachable ();
1385
1386     default:
1387       fatal_error ("Unknown intrinsic op");
1388       return;
1389     }
1390
1391   /* The only exception to this is **, which is handled separately anyway.  */
1392   gcc_assert (expr->value.op.op1->ts.type == expr->value.op.op2->ts.type);
1393
1394   if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
1395     checkstring = 0;
1396
1397   /* lhs */
1398   gfc_init_se (&lse, se);
1399   gfc_conv_expr (&lse, expr->value.op.op1);
1400   gfc_add_block_to_block (&se->pre, &lse.pre);
1401
1402   /* rhs */
1403   gfc_init_se (&rse, se);
1404   gfc_conv_expr (&rse, expr->value.op.op2);
1405   gfc_add_block_to_block (&se->pre, &rse.pre);
1406
1407   if (checkstring)
1408     {
1409       gfc_conv_string_parameter (&lse);
1410       gfc_conv_string_parameter (&rse);
1411
1412       lse.expr = gfc_build_compare_string (lse.string_length, lse.expr,
1413                                            rse.string_length, rse.expr,
1414                                            expr->value.op.op1->ts.kind,
1415                                            code);
1416       rse.expr = build_int_cst (TREE_TYPE (lse.expr), 0);
1417       gfc_add_block_to_block (&lse.post, &rse.post);
1418     }
1419
1420   type = gfc_typenode_for_spec (&expr->ts);
1421
1422   if (lop)
1423     {
1424       /* The result of logical ops is always boolean_type_node.  */
1425       tmp = fold_build2_loc (input_location, code, boolean_type_node,
1426                              lse.expr, rse.expr);
1427       se->expr = convert (type, tmp);
1428     }
1429   else
1430     se->expr = fold_build2_loc (input_location, code, type, lse.expr, rse.expr);
1431
1432   /* Add the post blocks.  */
1433   gfc_add_block_to_block (&se->post, &rse.post);
1434   gfc_add_block_to_block (&se->post, &lse.post);
1435 }
1436
1437 /* If a string's length is one, we convert it to a single character.  */
1438
1439 tree
1440 gfc_string_to_single_character (tree len, tree str, int kind)
1441 {
1442
1443   if (!INTEGER_CST_P (len) || TREE_INT_CST_HIGH (len) != 0
1444       || !POINTER_TYPE_P (TREE_TYPE (str)))
1445     return NULL_TREE;
1446
1447   if (TREE_INT_CST_LOW (len) == 1)
1448     {
1449       str = fold_convert (gfc_get_pchar_type (kind), str);
1450       return build_fold_indirect_ref_loc (input_location, str);
1451     }
1452
1453   if (kind == 1
1454       && TREE_CODE (str) == ADDR_EXPR
1455       && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
1456       && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
1457       && array_ref_low_bound (TREE_OPERAND (str, 0))
1458          == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
1459       && TREE_INT_CST_LOW (len) > 1
1460       && TREE_INT_CST_LOW (len)
1461          == (unsigned HOST_WIDE_INT)
1462             TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
1463     {
1464       tree ret = fold_convert (gfc_get_pchar_type (kind), str);
1465       ret = build_fold_indirect_ref_loc (input_location, ret);
1466       if (TREE_CODE (ret) == INTEGER_CST)
1467         {
1468           tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
1469           int i, length = TREE_STRING_LENGTH (string_cst);
1470           const char *ptr = TREE_STRING_POINTER (string_cst);
1471
1472           for (i = 1; i < length; i++)
1473             if (ptr[i] != ' ')
1474               return NULL_TREE;
1475
1476           return ret;
1477         }
1478     }
1479
1480   return NULL_TREE;
1481 }
1482
1483
1484 void
1485 gfc_conv_scalar_char_value (gfc_symbol *sym, gfc_se *se, gfc_expr **expr)
1486 {
1487
1488   if (sym->backend_decl)
1489     {
1490       /* This becomes the nominal_type in
1491          function.c:assign_parm_find_data_types.  */
1492       TREE_TYPE (sym->backend_decl) = unsigned_char_type_node;
1493       /* This becomes the passed_type in
1494          function.c:assign_parm_find_data_types.  C promotes char to
1495          integer for argument passing.  */
1496       DECL_ARG_TYPE (sym->backend_decl) = unsigned_type_node;
1497
1498       DECL_BY_REFERENCE (sym->backend_decl) = 0;
1499     }
1500
1501   if (expr != NULL)
1502     {
1503       /* If we have a constant character expression, make it into an
1504          integer.  */
1505       if ((*expr)->expr_type == EXPR_CONSTANT)
1506         {
1507           gfc_typespec ts;
1508           gfc_clear_ts (&ts);
1509
1510           *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL,
1511                                     (int)(*expr)->value.character.string[0]);
1512           if ((*expr)->ts.kind != gfc_c_int_kind)
1513             {
1514               /* The expr needs to be compatible with a C int.  If the 
1515                  conversion fails, then the 2 causes an ICE.  */
1516               ts.type = BT_INTEGER;
1517               ts.kind = gfc_c_int_kind;
1518               gfc_convert_type (*expr, &ts, 2);
1519             }
1520         }
1521       else if (se != NULL && (*expr)->expr_type == EXPR_VARIABLE)
1522         {
1523           if ((*expr)->ref == NULL)
1524             {
1525               se->expr = gfc_string_to_single_character
1526                 (build_int_cst (integer_type_node, 1),
1527                  gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
1528                                       gfc_get_symbol_decl
1529                                       ((*expr)->symtree->n.sym)),
1530                  (*expr)->ts.kind);
1531             }
1532           else
1533             {
1534               gfc_conv_variable (se, *expr);
1535               se->expr = gfc_string_to_single_character
1536                 (build_int_cst (integer_type_node, 1),
1537                  gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
1538                                       se->expr),
1539                  (*expr)->ts.kind);
1540             }
1541         }
1542     }
1543 }
1544
1545 /* Helper function for gfc_build_compare_string.  Return LEN_TRIM value
1546    if STR is a string literal, otherwise return -1.  */
1547
1548 static int
1549 gfc_optimize_len_trim (tree len, tree str, int kind)
1550 {
1551   if (kind == 1
1552       && TREE_CODE (str) == ADDR_EXPR
1553       && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
1554       && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
1555       && array_ref_low_bound (TREE_OPERAND (str, 0))
1556          == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
1557       && TREE_INT_CST_LOW (len) >= 1
1558       && TREE_INT_CST_LOW (len)
1559          == (unsigned HOST_WIDE_INT)
1560             TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
1561     {
1562       tree folded = fold_convert (gfc_get_pchar_type (kind), str);
1563       folded = build_fold_indirect_ref_loc (input_location, folded);
1564       if (TREE_CODE (folded) == INTEGER_CST)
1565         {
1566           tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
1567           int length = TREE_STRING_LENGTH (string_cst);
1568           const char *ptr = TREE_STRING_POINTER (string_cst);
1569
1570           for (; length > 0; length--)
1571             if (ptr[length - 1] != ' ')
1572               break;
1573
1574           return length;
1575         }
1576     }
1577   return -1;
1578 }
1579
1580 /* Compare two strings. If they are all single characters, the result is the
1581    subtraction of them. Otherwise, we build a library call.  */
1582
1583 tree
1584 gfc_build_compare_string (tree len1, tree str1, tree len2, tree str2, int kind,
1585                           enum tree_code code)
1586 {
1587   tree sc1;
1588   tree sc2;
1589   tree fndecl;
1590
1591   gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1)));
1592   gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2)));
1593
1594   sc1 = gfc_string_to_single_character (len1, str1, kind);
1595   sc2 = gfc_string_to_single_character (len2, str2, kind);
1596
1597   if (sc1 != NULL_TREE && sc2 != NULL_TREE)
1598     {
1599       /* Deal with single character specially.  */
1600       sc1 = fold_convert (integer_type_node, sc1);
1601       sc2 = fold_convert (integer_type_node, sc2);
1602       return fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
1603                               sc1, sc2);
1604     }
1605
1606   if ((code == EQ_EXPR || code == NE_EXPR)
1607       && optimize
1608       && INTEGER_CST_P (len1) && INTEGER_CST_P (len2))
1609     {
1610       /* If one string is a string literal with LEN_TRIM longer
1611          than the length of the second string, the strings
1612          compare unequal.  */
1613       int len = gfc_optimize_len_trim (len1, str1, kind);
1614       if (len > 0 && compare_tree_int (len2, len) < 0)
1615         return integer_one_node;
1616       len = gfc_optimize_len_trim (len2, str2, kind);
1617       if (len > 0 && compare_tree_int (len1, len) < 0)
1618         return integer_one_node;
1619     }
1620
1621   /* Build a call for the comparison.  */
1622   if (kind == 1)
1623     fndecl = gfor_fndecl_compare_string;
1624   else if (kind == 4)
1625     fndecl = gfor_fndecl_compare_string_char4;
1626   else
1627     gcc_unreachable ();
1628
1629   return build_call_expr_loc (input_location, fndecl, 4,
1630                               len1, str1, len2, str2);
1631 }
1632
1633
1634 /* Return the backend_decl for a procedure pointer component.  */
1635
1636 static tree
1637 get_proc_ptr_comp (gfc_expr *e)
1638 {
1639   gfc_se comp_se;
1640   gfc_expr *e2;
1641   expr_t old_type;
1642
1643   gfc_init_se (&comp_se, NULL);
1644   e2 = gfc_copy_expr (e);
1645   /* We have to restore the expr type later so that gfc_free_expr frees
1646      the exact same thing that was allocated.
1647      TODO: This is ugly.  */
1648   old_type = e2->expr_type;
1649   e2->expr_type = EXPR_VARIABLE;
1650   gfc_conv_expr (&comp_se, e2);
1651   e2->expr_type = old_type;
1652   gfc_free_expr (e2);
1653   return build_fold_addr_expr_loc (input_location, comp_se.expr);
1654 }
1655
1656
1657 static void
1658 conv_function_val (gfc_se * se, gfc_symbol * sym, gfc_expr * expr)
1659 {
1660   tree tmp;
1661
1662   if (gfc_is_proc_ptr_comp (expr, NULL))
1663     tmp = get_proc_ptr_comp (expr);
1664   else if (sym->attr.dummy)
1665     {
1666       tmp = gfc_get_symbol_decl (sym);
1667       if (sym->attr.proc_pointer)
1668         tmp = build_fold_indirect_ref_loc (input_location,
1669                                        tmp);
1670       gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE
1671               && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) == FUNCTION_TYPE);
1672     }
1673   else
1674     {
1675       if (!sym->backend_decl)
1676         sym->backend_decl = gfc_get_extern_function_decl (sym);
1677
1678       tmp = sym->backend_decl;
1679
1680       if (sym->attr.cray_pointee)
1681         {
1682           /* TODO - make the cray pointee a pointer to a procedure,
1683              assign the pointer to it and use it for the call.  This
1684              will do for now!  */
1685           tmp = convert (build_pointer_type (TREE_TYPE (tmp)),
1686                          gfc_get_symbol_decl (sym->cp_pointer));
1687           tmp = gfc_evaluate_now (tmp, &se->pre);
1688         }
1689
1690       if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
1691         {
1692           gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
1693           tmp = gfc_build_addr_expr (NULL_TREE, tmp);
1694         }
1695     }
1696   se->expr = tmp;
1697 }
1698
1699
1700 /* Initialize MAPPING.  */
1701
1702 void
1703 gfc_init_interface_mapping (gfc_interface_mapping * mapping)
1704 {
1705   mapping->syms = NULL;
1706   mapping->charlens = NULL;
1707 }
1708
1709
1710 /* Free all memory held by MAPPING (but not MAPPING itself).  */
1711
1712 void
1713 gfc_free_interface_mapping (gfc_interface_mapping * mapping)
1714 {
1715   gfc_interface_sym_mapping *sym;
1716   gfc_interface_sym_mapping *nextsym;
1717   gfc_charlen *cl;
1718   gfc_charlen *nextcl;
1719
1720   for (sym = mapping->syms; sym; sym = nextsym)
1721     {
1722       nextsym = sym->next;
1723       sym->new_sym->n.sym->formal = NULL;
1724       gfc_free_symbol (sym->new_sym->n.sym);
1725       gfc_free_expr (sym->expr);
1726       gfc_free (sym->new_sym);
1727       gfc_free (sym);
1728     }
1729   for (cl = mapping->charlens; cl; cl = nextcl)
1730     {
1731       nextcl = cl->next;
1732       gfc_free_expr (cl->length);
1733       gfc_free (cl);
1734     }
1735 }
1736
1737
1738 /* Return a copy of gfc_charlen CL.  Add the returned structure to
1739    MAPPING so that it will be freed by gfc_free_interface_mapping.  */
1740
1741 static gfc_charlen *
1742 gfc_get_interface_mapping_charlen (gfc_interface_mapping * mapping,
1743                                    gfc_charlen * cl)
1744 {
1745   gfc_charlen *new_charlen;
1746
1747   new_charlen = gfc_get_charlen ();
1748   new_charlen->next = mapping->charlens;
1749   new_charlen->length = gfc_copy_expr (cl->length);
1750
1751   mapping->charlens = new_charlen;
1752   return new_charlen;
1753 }
1754
1755
1756 /* A subroutine of gfc_add_interface_mapping.  Return a descriptorless
1757    array variable that can be used as the actual argument for dummy
1758    argument SYM.  Add any initialization code to BLOCK.  PACKED is as
1759    for gfc_get_nodesc_array_type and DATA points to the first element
1760    in the passed array.  */
1761
1762 static tree
1763 gfc_get_interface_mapping_array (stmtblock_t * block, gfc_symbol * sym,
1764                                  gfc_packed packed, tree data)
1765 {
1766   tree type;
1767   tree var;
1768
1769   type = gfc_typenode_for_spec (&sym->ts);
1770   type = gfc_get_nodesc_array_type (type, sym->as, packed,
1771                                     !sym->attr.target && !sym->attr.pointer
1772                                     && !sym->attr.proc_pointer);
1773
1774   var = gfc_create_var (type, "ifm");
1775   gfc_add_modify (block, var, fold_convert (type, data));
1776
1777   return var;
1778 }
1779
1780
1781 /* A subroutine of gfc_add_interface_mapping.  Set the stride, upper bounds
1782    and offset of descriptorless array type TYPE given that it has the same
1783    size as DESC.  Add any set-up code to BLOCK.  */
1784
1785 static void
1786 gfc_set_interface_mapping_bounds (stmtblock_t * block, tree type, tree desc)
1787 {
1788   int n;
1789   tree dim;
1790   tree offset;
1791   tree tmp;
1792
1793   offset = gfc_index_zero_node;
1794   for (n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
1795     {
1796       dim = gfc_rank_cst[n];
1797       GFC_TYPE_ARRAY_STRIDE (type, n) = gfc_conv_array_stride (desc, n);
1798       if (GFC_TYPE_ARRAY_LBOUND (type, n) == NULL_TREE)
1799         {
1800           GFC_TYPE_ARRAY_LBOUND (type, n)
1801                 = gfc_conv_descriptor_lbound_get (desc, dim);
1802           GFC_TYPE_ARRAY_UBOUND (type, n)
1803                 = gfc_conv_descriptor_ubound_get (desc, dim);
1804         }
1805       else if (GFC_TYPE_ARRAY_UBOUND (type, n) == NULL_TREE)
1806         {
1807           tmp = fold_build2_loc (input_location, MINUS_EXPR,
1808                                  gfc_array_index_type,
1809                                  gfc_conv_descriptor_ubound_get (desc, dim),
1810                                  gfc_conv_descriptor_lbound_get (desc, dim));
1811           tmp = fold_build2_loc (input_location, PLUS_EXPR,
1812                                  gfc_array_index_type,
1813                                  GFC_TYPE_ARRAY_LBOUND (type, n), tmp);
1814           tmp = gfc_evaluate_now (tmp, block);
1815           GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1816         }
1817       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
1818                              GFC_TYPE_ARRAY_LBOUND (type, n),
1819                              GFC_TYPE_ARRAY_STRIDE (type, n));
1820       offset = fold_build2_loc (input_location, MINUS_EXPR,
1821                                 gfc_array_index_type, offset, tmp);
1822     }
1823   offset = gfc_evaluate_now (offset, block);
1824   GFC_TYPE_ARRAY_OFFSET (type) = offset;
1825 }
1826
1827
1828 /* Extend MAPPING so that it maps dummy argument SYM to the value stored
1829    in SE.  The caller may still use se->expr and se->string_length after
1830    calling this function.  */
1831
1832 void
1833 gfc_add_interface_mapping (gfc_interface_mapping * mapping,
1834                            gfc_symbol * sym, gfc_se * se,
1835                            gfc_expr *expr)
1836 {
1837   gfc_interface_sym_mapping *sm;
1838   tree desc;
1839   tree tmp;
1840   tree value;
1841   gfc_symbol *new_sym;
1842   gfc_symtree *root;
1843   gfc_symtree *new_symtree;
1844
1845   /* Create a new symbol to represent the actual argument.  */
1846   new_sym = gfc_new_symbol (sym->name, NULL);
1847   new_sym->ts = sym->ts;
1848   new_sym->as = gfc_copy_array_spec (sym->as);
1849   new_sym->attr.referenced = 1;
1850   new_sym->attr.dimension = sym->attr.dimension;
1851   new_sym->attr.contiguous = sym->attr.contiguous;
1852   new_sym->attr.codimension = sym->attr.codimension;
1853   new_sym->attr.pointer = sym->attr.pointer;
1854   new_sym->attr.allocatable = sym->attr.allocatable;
1855   new_sym->attr.flavor = sym->attr.flavor;
1856   new_sym->attr.function = sym->attr.function;
1857
1858   /* Ensure that the interface is available and that
1859      descriptors are passed for array actual arguments.  */
1860   if (sym->attr.flavor == FL_PROCEDURE)
1861     {
1862       new_sym->formal = expr->symtree->n.sym->formal;
1863       new_sym->attr.always_explicit
1864             = expr->symtree->n.sym->attr.always_explicit;
1865     }
1866
1867   /* Create a fake symtree for it.  */
1868   root = NULL;
1869   new_symtree = gfc_new_symtree (&root, sym->name);
1870   new_symtree->n.sym = new_sym;
1871   gcc_assert (new_symtree == root);
1872
1873   /* Create a dummy->actual mapping.  */
1874   sm = XCNEW (gfc_interface_sym_mapping);
1875   sm->next = mapping->syms;
1876   sm->old = sym;
1877   sm->new_sym = new_symtree;
1878   sm->expr = gfc_copy_expr (expr);
1879   mapping->syms = sm;
1880
1881   /* Stabilize the argument's value.  */
1882   if (!sym->attr.function && se)
1883     se->expr = gfc_evaluate_now (se->expr, &se->pre);
1884
1885   if (sym->ts.type == BT_CHARACTER)
1886     {
1887       /* Create a copy of the dummy argument's length.  */
1888       new_sym->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, sym->ts.u.cl);
1889       sm->expr->ts.u.cl = new_sym->ts.u.cl;
1890
1891       /* If the length is specified as "*", record the length that
1892          the caller is passing.  We should use the callee's length
1893          in all other cases.  */
1894       if (!new_sym->ts.u.cl->length && se)
1895         {
1896           se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
1897           new_sym->ts.u.cl->backend_decl = se->string_length;
1898         }
1899     }
1900
1901   if (!se)
1902     return;
1903
1904   /* Use the passed value as-is if the argument is a function.  */
1905   if (sym->attr.flavor == FL_PROCEDURE)
1906     value = se->expr;
1907
1908   /* If the argument is either a string or a pointer to a string,
1909      convert it to a boundless character type.  */
1910   else if (!sym->attr.dimension && sym->ts.type == BT_CHARACTER)
1911     {
1912       tmp = gfc_get_character_type_len (sym->ts.kind, NULL);
1913       tmp = build_pointer_type (tmp);
1914       if (sym->attr.pointer)
1915         value = build_fold_indirect_ref_loc (input_location,
1916                                          se->expr);
1917       else
1918         value = se->expr;
1919       value = fold_convert (tmp, value);
1920     }
1921
1922   /* If the argument is a scalar, a pointer to an array or an allocatable,
1923      dereference it.  */
1924   else if (!sym->attr.dimension || sym->attr.pointer || sym->attr.allocatable)
1925     value = build_fold_indirect_ref_loc (input_location,
1926                                      se->expr);
1927   
1928   /* For character(*), use the actual argument's descriptor.  */  
1929   else if (sym->ts.type == BT_CHARACTER && !new_sym->ts.u.cl->length)
1930     value = build_fold_indirect_ref_loc (input_location,
1931                                      se->expr);
1932
1933   /* If the argument is an array descriptor, use it to determine
1934      information about the actual argument's shape.  */
1935   else if (POINTER_TYPE_P (TREE_TYPE (se->expr))
1936            && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
1937     {
1938       /* Get the actual argument's descriptor.  */
1939       desc = build_fold_indirect_ref_loc (input_location,
1940                                       se->expr);
1941
1942       /* Create the replacement variable.  */
1943       tmp = gfc_conv_descriptor_data_get (desc);
1944       value = gfc_get_interface_mapping_array (&se->pre, sym,
1945                                                PACKED_NO, tmp);
1946
1947       /* Use DESC to work out the upper bounds, strides and offset.  */
1948       gfc_set_interface_mapping_bounds (&se->pre, TREE_TYPE (value), desc);
1949     }
1950   else
1951     /* Otherwise we have a packed array.  */
1952     value = gfc_get_interface_mapping_array (&se->pre, sym,
1953                                              PACKED_FULL, se->expr);
1954
1955   new_sym->backend_decl = value;
1956 }
1957
1958
1959 /* Called once all dummy argument mappings have been added to MAPPING,
1960    but before the mapping is used to evaluate expressions.  Pre-evaluate
1961    the length of each argument, adding any initialization code to PRE and
1962    any finalization code to POST.  */
1963
1964 void
1965 gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
1966                               stmtblock_t * pre, stmtblock_t * post)
1967 {
1968   gfc_interface_sym_mapping *sym;
1969   gfc_expr *expr;
1970   gfc_se se;
1971
1972   for (sym = mapping->syms; sym; sym = sym->next)
1973     if (sym->new_sym->n.sym->ts.type == BT_CHARACTER
1974         && !sym->new_sym->n.sym->ts.u.cl->backend_decl)
1975       {
1976         expr = sym->new_sym->n.sym->ts.u.cl->length;
1977         gfc_apply_interface_mapping_to_expr (mapping, expr);
1978         gfc_init_se (&se, NULL);
1979         gfc_conv_expr (&se, expr);
1980         se.expr = fold_convert (gfc_charlen_type_node, se.expr);
1981         se.expr = gfc_evaluate_now (se.expr, &se.pre);
1982         gfc_add_block_to_block (pre, &se.pre);
1983         gfc_add_block_to_block (post, &se.post);
1984
1985         sym->new_sym->n.sym->ts.u.cl->backend_decl = se.expr;
1986       }
1987 }
1988
1989
1990 /* Like gfc_apply_interface_mapping_to_expr, but applied to
1991    constructor C.  */
1992
1993 static void
1994 gfc_apply_interface_mapping_to_cons (gfc_interface_mapping * mapping,
1995                                      gfc_constructor_base base)
1996 {
1997   gfc_constructor *c;
1998   for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1999     {
2000       gfc_apply_interface_mapping_to_expr (mapping, c->expr);
2001       if (c->iterator)
2002         {
2003           gfc_apply_interface_mapping_to_expr (mapping, c->iterator->start);
2004           gfc_apply_interface_mapping_to_expr (mapping, c->iterator->end);
2005           gfc_apply_interface_mapping_to_expr (mapping, c->iterator->step);
2006         }
2007     }
2008 }
2009
2010
2011 /* Like gfc_apply_interface_mapping_to_expr, but applied to
2012    reference REF.  */
2013
2014 static void
2015 gfc_apply_interface_mapping_to_ref (gfc_interface_mapping * mapping,
2016                                     gfc_ref * ref)
2017 {
2018   int n;
2019
2020   for (; ref; ref = ref->next)
2021     switch (ref->type)
2022       {
2023       case REF_ARRAY:
2024         for (n = 0; n < ref->u.ar.dimen; n++)
2025           {
2026             gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.start[n]);
2027             gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.end[n]);
2028             gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.stride[n]);
2029           }
2030         gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.offset);
2031         break;
2032
2033       case REF_COMPONENT:
2034         break;
2035
2036       case REF_SUBSTRING:
2037         gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.start);
2038         gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.end);
2039         break;
2040       }
2041 }
2042
2043
2044 /* Convert intrinsic function calls into result expressions.  */
2045
2046 static bool
2047 gfc_map_intrinsic_function (gfc_expr *expr, gfc_interface_mapping *mapping)
2048 {
2049   gfc_symbol *sym;
2050   gfc_expr *new_expr;
2051   gfc_expr *arg1;
2052   gfc_expr *arg2;
2053   int d, dup;
2054
2055   arg1 = expr->value.function.actual->expr;
2056   if (expr->value.function.actual->next)
2057     arg2 = expr->value.function.actual->next->expr;
2058   else
2059     arg2 = NULL;
2060
2061   sym = arg1->symtree->n.sym;
2062
2063   if (sym->attr.dummy)
2064     return false;
2065
2066   new_expr = NULL;
2067
2068   switch (expr->value.function.isym->id)
2069     {
2070     case GFC_ISYM_LEN:
2071       /* TODO figure out why this condition is necessary.  */
2072       if (sym->attr.function
2073           && (arg1->ts.u.cl->length == NULL
2074               || (arg1->ts.u.cl->length->expr_type != EXPR_CONSTANT
2075                   && arg1->ts.u.cl->length->expr_type != EXPR_VARIABLE)))
2076         return false;
2077
2078       new_expr = gfc_copy_expr (arg1->ts.u.cl->length);
2079       break;
2080
2081     case GFC_ISYM_SIZE:
2082       if (!sym->as || sym->as->rank == 0)
2083         return false;
2084
2085       if (arg2 && arg2->expr_type == EXPR_CONSTANT)
2086         {
2087           dup = mpz_get_si (arg2->value.integer);
2088           d = dup - 1;
2089         }
2090       else
2091         {
2092           dup = sym->as->rank;
2093           d = 0;
2094         }
2095
2096       for (; d < dup; d++)
2097         {
2098           gfc_expr *tmp;
2099
2100           if (!sym->as->upper[d] || !sym->as->lower[d])
2101             {
2102               gfc_free_expr (new_expr);
2103               return false;
2104             }
2105
2106           tmp = gfc_add (gfc_copy_expr (sym->as->upper[d]),
2107                                         gfc_get_int_expr (gfc_default_integer_kind,
2108                                                           NULL, 1));
2109           tmp = gfc_subtract (tmp, gfc_copy_expr (sym->as->lower[d]));
2110           if (new_expr)
2111             new_expr = gfc_multiply (new_expr, tmp);
2112           else
2113             new_expr = tmp;
2114         }
2115       break;
2116
2117     case GFC_ISYM_LBOUND:
2118     case GFC_ISYM_UBOUND:
2119         /* TODO These implementations of lbound and ubound do not limit if
2120            the size < 0, according to F95's 13.14.53 and 13.14.113.  */
2121
2122       if (!sym->as || sym->as->rank == 0)
2123         return false;
2124
2125       if (arg2 && arg2->expr_type == EXPR_CONSTANT)
2126         d = mpz_get_si (arg2->value.integer) - 1;
2127       else
2128         /* TODO: If the need arises, this could produce an array of
2129            ubound/lbounds.  */
2130         gcc_unreachable ();
2131
2132       if (expr->value.function.isym->id == GFC_ISYM_LBOUND)
2133         {
2134           if (sym->as->lower[d])
2135             new_expr = gfc_copy_expr (sym->as->lower[d]);
2136         }
2137       else
2138         {
2139           if (sym->as->upper[d])
2140             new_expr = gfc_copy_expr (sym->as->upper[d]);
2141         }
2142       break;
2143
2144     default:
2145       break;
2146     }
2147
2148   gfc_apply_interface_mapping_to_expr (mapping, new_expr);
2149   if (!new_expr)
2150     return false;
2151
2152   gfc_replace_expr (expr, new_expr);
2153   return true;
2154 }
2155
2156
2157 static void
2158 gfc_map_fcn_formal_to_actual (gfc_expr *expr, gfc_expr *map_expr,
2159                               gfc_interface_mapping * mapping)
2160 {
2161   gfc_formal_arglist *f;
2162   gfc_actual_arglist *actual;
2163
2164   actual = expr->value.function.actual;
2165   f = map_expr->symtree->n.sym->formal;
2166
2167   for (; f && actual; f = f->next, actual = actual->next)
2168     {
2169       if (!actual->expr)
2170         continue;
2171
2172       gfc_add_interface_mapping (mapping, f->sym, NULL, actual->expr);
2173     }
2174
2175   if (map_expr->symtree->n.sym->attr.dimension)
2176     {
2177       int d;
2178       gfc_array_spec *as;
2179
2180       as = gfc_copy_array_spec (map_expr->symtree->n.sym->as);
2181
2182       for (d = 0; d < as->rank; d++)
2183         {
2184           gfc_apply_interface_mapping_to_expr (mapping, as->lower[d]);
2185           gfc_apply_interface_mapping_to_expr (mapping, as->upper[d]);
2186         }
2187
2188       expr->value.function.esym->as = as;
2189     }
2190
2191   if (map_expr->symtree->n.sym->ts.type == BT_CHARACTER)
2192     {
2193       expr->value.function.esym->ts.u.cl->length
2194         = gfc_copy_expr (map_expr->symtree->n.sym->ts.u.cl->length);
2195
2196       gfc_apply_interface_mapping_to_expr (mapping,
2197                         expr->value.function.esym->ts.u.cl->length);
2198     }
2199 }
2200
2201
2202 /* EXPR is a copy of an expression that appeared in the interface
2203    associated with MAPPING.  Walk it recursively looking for references to
2204    dummy arguments that MAPPING maps to actual arguments.  Replace each such
2205    reference with a reference to the associated actual argument.  */
2206
2207 static void
2208 gfc_apply_interface_mapping_to_expr (gfc_interface_mapping * mapping,
2209                                      gfc_expr * expr)
2210 {
2211   gfc_interface_sym_mapping *sym;
2212   gfc_actual_arglist *actual;
2213
2214   if (!expr)
2215     return;
2216
2217   /* Copying an expression does not copy its length, so do that here.  */
2218   if (expr->ts.type == BT_CHARACTER && expr->ts.u.cl)
2219     {
2220       expr->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, expr->ts.u.cl);
2221       gfc_apply_interface_mapping_to_expr (mapping, expr->ts.u.cl->length);
2222     }
2223
2224   /* Apply the mapping to any references.  */
2225   gfc_apply_interface_mapping_to_ref (mapping, expr->ref);
2226
2227   /* ...and to the expression's symbol, if it has one.  */
2228   /* TODO Find out why the condition on expr->symtree had to be moved into
2229      the loop rather than being outside it, as originally.  */
2230   for (sym = mapping->syms; sym; sym = sym->next)
2231     if (expr->symtree && sym->old == expr->symtree->n.sym)
2232       {
2233         if (sym->new_sym->n.sym->backend_decl)
2234           expr->symtree = sym->new_sym;
2235         else if (sym->expr)
2236           gfc_replace_expr (expr, gfc_copy_expr (sym->expr));
2237       }
2238
2239       /* ...and to subexpressions in expr->value.  */
2240   switch (expr->expr_type)
2241     {
2242     case EXPR_VARIABLE:
2243     case EXPR_CONSTANT:
2244     case EXPR_NULL:
2245     case EXPR_SUBSTRING:
2246       break;
2247
2248     case EXPR_OP:
2249       gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op1);
2250       gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op2);
2251       break;
2252
2253     case EXPR_FUNCTION:
2254       for (actual = expr->value.function.actual; actual; actual = actual->next)
2255         gfc_apply_interface_mapping_to_expr (mapping, actual->expr);
2256
2257       if (expr->value.function.esym == NULL
2258             && expr->value.function.isym != NULL
2259             && expr->value.function.actual->expr->symtree
2260             && gfc_map_intrinsic_function (expr, mapping))
2261         break;
2262
2263       for (sym = mapping->syms; sym; sym = sym->next)
2264         if (sym->old == expr->value.function.esym)
2265           {
2266             expr->value.function.esym = sym->new_sym->n.sym;
2267             gfc_map_fcn_formal_to_actual (expr, sym->expr, mapping);
2268             expr->value.function.esym->result = sym->new_sym->n.sym;
2269           }
2270       break;
2271
2272     case EXPR_ARRAY:
2273     case EXPR_STRUCTURE:
2274       gfc_apply_interface_mapping_to_cons (mapping, expr->value.constructor);
2275       break;
2276
2277     case EXPR_COMPCALL:
2278     case EXPR_PPC:
2279       gcc_unreachable ();
2280       break;
2281     }
2282
2283   return;
2284 }
2285
2286
2287 /* Evaluate interface expression EXPR using MAPPING.  Store the result
2288    in SE.  */
2289
2290 void
2291 gfc_apply_interface_mapping (gfc_interface_mapping * mapping,
2292                              gfc_se * se, gfc_expr * expr)
2293 {
2294   expr = gfc_copy_expr (expr);
2295   gfc_apply_interface_mapping_to_expr (mapping, expr);
2296   gfc_conv_expr (se, expr);
2297   se->expr = gfc_evaluate_now (se->expr, &se->pre);
2298   gfc_free_expr (expr);
2299 }
2300
2301
2302 /* Returns a reference to a temporary array into which a component of
2303    an actual argument derived type array is copied and then returned
2304    after the function call.  */
2305 void
2306 gfc_conv_subref_array_arg (gfc_se * parmse, gfc_expr * expr, int g77,
2307                            sym_intent intent, bool formal_ptr)
2308 {
2309   gfc_se lse;
2310   gfc_se rse;
2311   gfc_ss *lss;
2312   gfc_ss *rss;
2313   gfc_loopinfo loop;
2314   gfc_loopinfo loop2;
2315   gfc_ss_info *info;
2316   tree offset;
2317   tree tmp_index;
2318   tree tmp;
2319   tree base_type;
2320   tree size;
2321   stmtblock_t body;
2322   int n;
2323   int dimen;
2324
2325   gcc_assert (expr->expr_type == EXPR_VARIABLE);
2326
2327   gfc_init_se (&lse, NULL);
2328   gfc_init_se (&rse, NULL);
2329
2330   /* Walk the argument expression.  */
2331   rss = gfc_walk_expr (expr);
2332
2333   gcc_assert (rss != gfc_ss_terminator);
2334  
2335   /* Initialize the scalarizer.  */
2336   gfc_init_loopinfo (&loop);
2337   gfc_add_ss_to_loop (&loop, rss);
2338
2339   /* Calculate the bounds of the scalarization.  */
2340   gfc_conv_ss_startstride (&loop);
2341
2342   /* Build an ss for the temporary.  */
2343   if (expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->backend_decl)
2344     gfc_conv_string_length (expr->ts.u.cl, expr, &parmse->pre);
2345
2346   base_type = gfc_typenode_for_spec (&expr->ts);
2347   if (GFC_ARRAY_TYPE_P (base_type)
2348                 || GFC_DESCRIPTOR_TYPE_P (base_type))
2349     base_type = gfc_get_element_type (base_type);
2350
2351   loop.temp_ss = gfc_get_ss ();;
2352   loop.temp_ss->type = GFC_SS_TEMP;
2353   loop.temp_ss->data.temp.type = base_type;
2354
2355   if (expr->ts.type == BT_CHARACTER)
2356     loop.temp_ss->string_length = expr->ts.u.cl->backend_decl;
2357   else
2358     loop.temp_ss->string_length = NULL;
2359
2360   parmse->string_length = loop.temp_ss->string_length;
2361   loop.temp_ss->data.temp.dimen = loop.dimen;
2362   loop.temp_ss->next = gfc_ss_terminator;
2363
2364   /* Associate the SS with the loop.  */
2365   gfc_add_ss_to_loop (&loop, loop.temp_ss);
2366
2367   /* Setup the scalarizing loops.  */
2368   gfc_conv_loop_setup (&loop, &expr->where);
2369
2370   /* Pass the temporary descriptor back to the caller.  */
2371   info = &loop.temp_ss->data.info;
2372   parmse->expr = info->descriptor;
2373
2374   /* Setup the gfc_se structures.  */
2375   gfc_copy_loopinfo_to_se (&lse, &loop);
2376   gfc_copy_loopinfo_to_se (&rse, &loop);
2377
2378   rse.ss = rss;
2379   lse.ss = loop.temp_ss;
2380   gfc_mark_ss_chain_used (rss, 1);
2381   gfc_mark_ss_chain_used (loop.temp_ss, 1);
2382
2383   /* Start the scalarized loop body.  */
2384   gfc_start_scalarized_body (&loop, &body);
2385
2386   /* Translate the expression.  */
2387   gfc_conv_expr (&rse, expr);
2388
2389   gfc_conv_tmp_array_ref (&lse);
2390
2391   if (intent != INTENT_OUT)
2392     {
2393       tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, true, false, true);
2394       gfc_add_expr_to_block (&body, tmp);
2395       gcc_assert (rse.ss == gfc_ss_terminator);
2396       gfc_trans_scalarizing_loops (&loop, &body);
2397     }
2398   else
2399     {
2400       /* Make sure that the temporary declaration survives by merging
2401        all the loop declarations into the current context.  */
2402       for (n = 0; n < loop.dimen; n++)
2403         {
2404           gfc_merge_block_scope (&body);
2405           body = loop.code[loop.order[n]];
2406         }
2407       gfc_merge_block_scope (&body);
2408     }
2409
2410   /* Add the post block after the second loop, so that any
2411      freeing of allocated memory is done at the right time.  */
2412   gfc_add_block_to_block (&parmse->pre, &loop.pre);
2413
2414   /**********Copy the temporary back again.*********/
2415
2416   gfc_init_se (&lse, NULL);
2417   gfc_init_se (&rse, NULL);
2418
2419   /* Walk the argument expression.  */
2420   lss = gfc_walk_expr (expr);
2421   rse.ss = loop.temp_ss;
2422   lse.ss = lss;
2423
2424   /* Initialize the scalarizer.  */
2425   gfc_init_loopinfo (&loop2);
2426   gfc_add_ss_to_loop (&loop2, lss);
2427
2428   /* Calculate the bounds of the scalarization.  */
2429   gfc_conv_ss_startstride (&loop2);
2430
2431   /* Setup the scalarizing loops.  */
2432   gfc_conv_loop_setup (&loop2, &expr->where);
2433
2434   gfc_copy_loopinfo_to_se (&lse, &loop2);
2435   gfc_copy_loopinfo_to_se (&rse, &loop2);
2436
2437   gfc_mark_ss_chain_used (lss, 1);
2438   gfc_mark_ss_chain_used (loop.temp_ss, 1);
2439
2440   /* Declare the variable to hold the temporary offset and start the
2441      scalarized loop body.  */
2442   offset = gfc_create_var (gfc_array_index_type, NULL);
2443   gfc_start_scalarized_body (&loop2, &body);
2444
2445   /* Build the offsets for the temporary from the loop variables.  The
2446      temporary array has lbounds of zero and strides of one in all
2447      dimensions, so this is very simple.  The offset is only computed
2448      outside the innermost loop, so the overall transfer could be
2449      optimized further.  */
2450   info = &rse.ss->data.info;
2451   dimen = info->dimen;
2452
2453   tmp_index = gfc_index_zero_node;
2454   for (n = dimen - 1; n > 0; n--)
2455     {
2456       tree tmp_str;
2457       tmp = rse.loop->loopvar[n];
2458       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
2459                              tmp, rse.loop->from[n]);
2460       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
2461                              tmp, tmp_index);
2462
2463       tmp_str = fold_build2_loc (input_location, MINUS_EXPR,
2464                                  gfc_array_index_type,
2465                                  rse.loop->to[n-1], rse.loop->from[n-1]);
2466       tmp_str = fold_build2_loc (input_location, PLUS_EXPR,
2467                                  gfc_array_index_type,
2468                                  tmp_str, gfc_index_one_node);
2469
2470       tmp_index = fold_build2_loc (input_location, MULT_EXPR,
2471                                    gfc_array_index_type, tmp, tmp_str);
2472     }
2473
2474   tmp_index = fold_build2_loc (input_location, MINUS_EXPR,
2475                                gfc_array_index_type,
2476                                tmp_index, rse.loop->from[0]);
2477   gfc_add_modify (&rse.loop->code[0], offset, tmp_index);
2478
2479   tmp_index = fold_build2_loc (input_location, PLUS_EXPR,
2480                                gfc_array_index_type,
2481                                rse.loop->loopvar[0], offset);
2482
2483   /* Now use the offset for the reference.  */
2484   tmp = build_fold_indirect_ref_loc (input_location,
2485                                  info->data);
2486   rse.expr = gfc_build_array_ref (tmp, tmp_index, NULL);
2487
2488   if (expr->ts.type == BT_CHARACTER)
2489     rse.string_length = expr->ts.u.cl->backend_decl;
2490
2491   gfc_conv_expr (&lse, expr);
2492
2493   gcc_assert (lse.ss == gfc_ss_terminator);
2494
2495   tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, false, true);
2496   gfc_add_expr_to_block (&body, tmp);
2497   
2498   /* Generate the copying loops.  */
2499   gfc_trans_scalarizing_loops (&loop2, &body);
2500
2501   /* Wrap the whole thing up by adding the second loop to the post-block
2502      and following it by the post-block of the first loop.  In this way,
2503      if the temporary needs freeing, it is done after use!  */
2504   if (intent != INTENT_IN)
2505     {
2506       gfc_add_block_to_block (&parmse->post, &loop2.pre);
2507       gfc_add_block_to_block (&parmse->post, &loop2.post);
2508     }
2509
2510   gfc_add_block_to_block (&parmse->post, &loop.post);
2511
2512   gfc_cleanup_loop (&loop);
2513   gfc_cleanup_loop (&loop2);
2514
2515   /* Pass the string length to the argument expression.  */
2516   if (expr->ts.type == BT_CHARACTER)
2517     parmse->string_length = expr->ts.u.cl->backend_decl;
2518
2519   /* Determine the offset for pointer formal arguments and set the
2520      lbounds to one.  */
2521   if (formal_ptr)
2522     {
2523       size = gfc_index_one_node;
2524       offset = gfc_index_zero_node;  
2525       for (n = 0; n < dimen; n++)
2526         {
2527           tmp = gfc_conv_descriptor_ubound_get (parmse->expr,
2528                                                 gfc_rank_cst[n]);
2529           tmp = fold_build2_loc (input_location, PLUS_EXPR,
2530                                  gfc_array_index_type, tmp,
2531                                  gfc_index_one_node);
2532           gfc_conv_descriptor_ubound_set (&parmse->pre,
2533                                           parmse->expr,
2534                                           gfc_rank_cst[n],
2535                                           tmp);
2536           gfc_conv_descriptor_lbound_set (&parmse->pre,
2537                                           parmse->expr,
2538                                           gfc_rank_cst[n],
2539                                           gfc_index_one_node);
2540           size = gfc_evaluate_now (size, &parmse->pre);
2541           offset = fold_build2_loc (input_location, MINUS_EXPR,
2542                                     gfc_array_index_type,
2543                                     offset, size);
2544           offset = gfc_evaluate_now (offset, &parmse->pre);
2545           tmp = fold_build2_loc (input_location, MINUS_EXPR,
2546                                  gfc_array_index_type,
2547                                  rse.loop->to[n], rse.loop->from[n]);
2548           tmp = fold_build2_loc (input_location, PLUS_EXPR,
2549                                  gfc_array_index_type,
2550                                  tmp, gfc_index_one_node);
2551           size = fold_build2_loc (input_location, MULT_EXPR,
2552                                   gfc_array_index_type, size, tmp);
2553         }
2554
2555       gfc_conv_descriptor_offset_set (&parmse->pre, parmse->expr,
2556                                       offset);
2557     }
2558
2559   /* We want either the address for the data or the address of the descriptor,
2560      depending on the mode of passing array arguments.  */
2561   if (g77)
2562     parmse->expr = gfc_conv_descriptor_data_get (parmse->expr);
2563   else
2564     parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
2565
2566   return;
2567 }
2568
2569
2570 /* Generate the code for argument list functions.  */
2571
2572 static void
2573 conv_arglist_function (gfc_se *se, gfc_expr *expr, const char *name)
2574 {
2575   /* Pass by value for g77 %VAL(arg), pass the address
2576      indirectly for %LOC, else by reference.  Thus %REF
2577      is a "do-nothing" and %LOC is the same as an F95
2578      pointer.  */
2579   if (strncmp (name, "%VAL", 4) == 0)
2580     gfc_conv_expr (se, expr);
2581   else if (strncmp (name, "%LOC", 4) == 0)
2582     {
2583       gfc_conv_expr_reference (se, expr);
2584       se->expr = gfc_build_addr_expr (NULL, se->expr);
2585     }
2586   else if (strncmp (name, "%REF", 4) == 0)
2587     gfc_conv_expr_reference (se, expr);
2588   else
2589     gfc_error ("Unknown argument list function at %L", &expr->where);
2590 }
2591
2592
2593 /* Takes a derived type expression and returns the address of a temporary
2594    class object of the 'declared' type.  */ 
2595 static void
2596 gfc_conv_derived_to_class (gfc_se *parmse, gfc_expr *e,
2597                            gfc_typespec class_ts)
2598 {
2599   gfc_component *cmp;
2600   gfc_symbol *vtab;
2601   gfc_symbol *declared = class_ts.u.derived;
2602   gfc_ss *ss;
2603   tree ctree;
2604   tree var;
2605   tree tmp;
2606
2607   /* The derived type needs to be converted to a temporary
2608      CLASS object.  */
2609   tmp = gfc_typenode_for_spec (&class_ts);
2610   var = gfc_create_var (tmp, "class");
2611
2612   /* Set the vptr.  */
2613   cmp = gfc_find_component (declared, "_vptr", true, true);
2614   ctree = fold_build3_loc (input_location, COMPONENT_REF,
2615                            TREE_TYPE (cmp->backend_decl),
2616                            var, cmp->backend_decl, NULL_TREE);
2617
2618   /* Remember the vtab corresponds to the derived type
2619      not to the class declared type.  */
2620   vtab = gfc_find_derived_vtab (e->ts.u.derived);
2621   gcc_assert (vtab);
2622   tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
2623   gfc_add_modify (&parmse->pre, ctree,
2624                   fold_convert (TREE_TYPE (ctree), tmp));
2625
2626   /* Now set the data field.  */
2627   cmp = gfc_find_component (declared, "_data", true, true);
2628   ctree = fold_build3_loc (input_location, COMPONENT_REF,
2629                            TREE_TYPE (cmp->backend_decl),
2630                            var, cmp->backend_decl, NULL_TREE);
2631   ss = gfc_walk_expr (e);
2632   if (ss == gfc_ss_terminator)
2633     {
2634       parmse->ss = NULL;
2635       gfc_conv_expr_reference (parmse, e);
2636       tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
2637       gfc_add_modify (&parmse->pre, ctree, tmp);
2638     }
2639   else
2640     {
2641       parmse->ss = ss;
2642       gfc_conv_expr (parmse, e);
2643       gfc_add_modify (&parmse->pre, ctree, parmse->expr);
2644     }
2645
2646   /* Pass the address of the class object.  */
2647   parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
2648 }
2649
2650
2651 /* The following routine generates code for the intrinsic
2652    procedures from the ISO_C_BINDING module:
2653     * C_LOC           (function)
2654     * C_FUNLOC        (function)
2655     * C_F_POINTER     (subroutine)
2656     * C_F_PROCPOINTER (subroutine)
2657     * C_ASSOCIATED    (function)
2658    One exception which is not handled here is C_F_POINTER with non-scalar
2659    arguments. Returns 1 if the call was replaced by inline code (else: 0).  */
2660
2661 static int
2662 conv_isocbinding_procedure (gfc_se * se, gfc_symbol * sym,
2663                             gfc_actual_arglist * arg)
2664 {
2665   gfc_symbol *fsym;
2666   gfc_ss *argss;
2667     
2668   if (sym->intmod_sym_id == ISOCBINDING_LOC)
2669     {
2670       if (arg->expr->rank == 0)
2671         gfc_conv_expr_reference (se, arg->expr);
2672       else
2673         {
2674           int f;
2675           /* This is really the actual arg because no formal arglist is
2676              created for C_LOC.  */
2677           fsym = arg->expr->symtree->n.sym;
2678
2679           /* We should want it to do g77 calling convention.  */
2680           f = (fsym != NULL)
2681             && !(fsym->attr.pointer || fsym->attr.allocatable)
2682             && fsym->as->type != AS_ASSUMED_SHAPE;
2683           f = f || !sym->attr.always_explicit;
2684       
2685           argss = gfc_walk_expr (arg->expr);
2686           gfc_conv_array_parameter (se, arg->expr, argss, f,
2687                                     NULL, NULL, NULL);
2688         }
2689
2690       /* TODO -- the following two lines shouldn't be necessary, but if
2691          they're removed, a bug is exposed later in the code path.
2692          This workaround was thus introduced, but will have to be
2693          removed; please see PR 35150 for details about the issue.  */
2694       se->expr = convert (pvoid_type_node, se->expr);
2695       se->expr = gfc_evaluate_now (se->expr, &se->pre);
2696
2697       return 1;
2698     }
2699   else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2700     {
2701       arg->expr->ts.type = sym->ts.u.derived->ts.type;
2702       arg->expr->ts.f90_type = sym->ts.u.derived->ts.f90_type;
2703       arg->expr->ts.kind = sym->ts.u.derived->ts.kind;
2704       gfc_conv_expr_reference (se, arg->expr);
2705   
2706       return 1;
2707     }
2708   else if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER
2709             && arg->next->expr->rank == 0)
2710            || sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER)
2711     {
2712       /* Convert c_f_pointer if fptr is a scalar
2713          and convert c_f_procpointer.  */
2714       gfc_se cptrse;
2715       gfc_se fptrse;
2716
2717       gfc_init_se (&cptrse, NULL);
2718       gfc_conv_expr (&cptrse, arg->expr);
2719       gfc_add_block_to_block (&se->pre, &cptrse.pre);
2720       gfc_add_block_to_block (&se->post, &cptrse.post);
2721
2722       gfc_init_se (&fptrse, NULL);
2723       if (sym->intmod_sym_id == ISOCBINDING_F_POINTER
2724           || gfc_is_proc_ptr_comp (arg->next->expr, NULL))
2725         fptrse.want_pointer = 1;
2726
2727       gfc_conv_expr (&fptrse, arg->next->expr);
2728       gfc_add_block_to_block (&se->pre, &fptrse.pre);
2729       gfc_add_block_to_block (&se->post, &fptrse.post);
2730       
2731       if (arg->next->expr->symtree->n.sym->attr.proc_pointer
2732           && arg->next->expr->symtree->n.sym->attr.dummy)
2733         fptrse.expr = build_fold_indirect_ref_loc (input_location,
2734                                                    fptrse.expr);
2735       
2736       se->expr = fold_build2_loc (input_location, MODIFY_EXPR,
2737                                   TREE_TYPE (fptrse.expr),
2738                                   fptrse.expr,
2739                                   fold_convert (TREE_TYPE (fptrse.expr),
2740                                                 cptrse.expr));
2741
2742       return 1;
2743     }
2744   else if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2745     {
2746       gfc_se arg1se;
2747       gfc_se arg2se;
2748
2749       /* Build the addr_expr for the first argument.  The argument is
2750          already an *address* so we don't need to set want_pointer in
2751          the gfc_se.  */
2752       gfc_init_se (&arg1se, NULL);
2753       gfc_conv_expr (&arg1se, arg->expr);
2754       gfc_add_block_to_block (&se->pre, &arg1se.pre);
2755       gfc_add_block_to_block (&se->post, &arg1se.post);
2756
2757       /* See if we were given two arguments.  */
2758       if (arg->next == NULL)
2759         /* Only given one arg so generate a null and do a
2760            not-equal comparison against the first arg.  */
2761         se->expr = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
2762                                     arg1se.expr,
2763                                     fold_convert (TREE_TYPE (arg1se.expr),
2764                                                   null_pointer_node));
2765       else
2766         {
2767           tree eq_expr;
2768           tree not_null_expr;
2769           
2770           /* Given two arguments so build the arg2se from second arg.  */
2771           gfc_init_se (&arg2se, NULL);
2772           gfc_conv_expr (&arg2se, arg->next->expr);
2773           gfc_add_block_to_block (&se->pre, &arg2se.pre);
2774           gfc_add_block_to_block (&se->post, &arg2se.post);
2775
2776           /* Generate test to compare that the two args are equal.  */
2777           eq_expr = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
2778                                      arg1se.expr, arg2se.expr);
2779           /* Generate test to ensure that the first arg is not null.  */
2780           not_null_expr = fold_build2_loc (input_location, NE_EXPR,
2781                                            boolean_type_node,
2782                                            arg1se.expr, null_pointer_node);
2783
2784           /* Finally, the generated test must check that both arg1 is not
2785              NULL and that it is equal to the second arg.  */
2786           se->expr = fold_build2_loc (input_location, TRUTH_AND_EXPR,
2787                                       boolean_type_node,
2788                                       not_null_expr, eq_expr);
2789         }
2790
2791       return 1;
2792     }
2793     
2794   /* Nothing was done.  */
2795   return 0;
2796 }
2797
2798 /* Generate code for a procedure call.  Note can return se->post != NULL.
2799    If se->direct_byref is set then se->expr contains the return parameter.
2800    Return nonzero, if the call has alternate specifiers.
2801    'expr' is only needed for procedure pointer components.  */
2802
2803 int
2804 gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
2805                          gfc_actual_arglist * args, gfc_expr * expr,
2806                          VEC(tree,gc) *append_args)
2807 {
2808   gfc_interface_mapping mapping;
2809   VEC(tree,gc) *arglist;
2810   VEC(tree,gc) *retargs;
2811   tree tmp;
2812   tree fntype;
2813   gfc_se parmse;
2814   gfc_ss *argss;
2815   gfc_ss_info *info;
2816   int byref;
2817   int parm_kind;
2818   tree type;
2819   tree var;
2820   tree len;
2821   VEC(tree,gc) *stringargs;
2822   tree result = NULL;
2823   gfc_formal_arglist *formal;
2824   gfc_actual_arglist *arg;
2825   int has_alternate_specifier = 0;
2826   bool need_interface_mapping;
2827   bool callee_alloc;
2828   gfc_typespec ts;
2829   gfc_charlen cl;
2830   gfc_expr *e;
2831   gfc_symbol *fsym;
2832   stmtblock_t post;
2833   enum {MISSING = 0, ELEMENTAL, SCALAR, SCALAR_POINTER, ARRAY};
2834   gfc_component *comp = NULL;
2835   int arglen;
2836
2837   arglist = NULL;
2838   retargs = NULL;
2839   stringargs = NULL;
2840   var = NULL_TREE;
2841   len = NULL_TREE;
2842   gfc_clear_ts (&ts);
2843
2844   if (sym->from_intmod == INTMOD_ISO_C_BINDING
2845       && conv_isocbinding_procedure (se, sym, args))
2846     return 0;
2847
2848   gfc_is_proc_ptr_comp (expr, &comp);
2849
2850   if (se->ss != NULL)
2851     {
2852       if (!sym->attr.elemental)
2853         {
2854           gcc_assert (se->ss->type == GFC_SS_FUNCTION);
2855           if (se->ss->useflags)
2856             {
2857               gcc_assert ((!comp && gfc_return_by_reference (sym)
2858                            && sym->result->attr.dimension)
2859                           || (comp && comp->attr.dimension));
2860               gcc_assert (se->loop != NULL);
2861
2862               /* Access the previously obtained result.  */
2863               gfc_conv_tmp_array_ref (se);
2864               return 0;
2865             }
2866         }
2867       info = &se->ss->data.info;
2868     }
2869   else
2870     info = NULL;
2871
2872   gfc_init_block (&post);
2873   gfc_init_interface_mapping (&mapping);
2874   if (!comp)
2875     {
2876       formal = sym->formal;
2877       need_interface_mapping = sym->attr.dimension ||
2878                                (sym->ts.type == BT_CHARACTER
2879                                 && sym->ts.u.cl->length
2880                                 && sym->ts.u.cl->length->expr_type
2881                                    != EXPR_CONSTANT);
2882     }
2883   else
2884     {
2885       formal = comp->formal;
2886       need_interface_mapping = comp->attr.dimension ||
2887                                (comp->ts.type == BT_CHARACTER
2888                                 && comp->ts.u.cl->length
2889                                 && comp->ts.u.cl->length->expr_type
2890                                    != EXPR_CONSTANT);
2891     }
2892
2893   /* Evaluate the arguments.  */
2894   for (arg = args; arg != NULL;
2895        arg = arg->next, formal = formal ? formal->next : NULL)
2896     {
2897       e = arg->expr;
2898       fsym = formal ? formal->sym : NULL;
2899       parm_kind = MISSING;
2900
2901       if (e == NULL)
2902         {
2903           if (se->ignore_optional)
2904             {
2905               /* Some intrinsics have already been resolved to the correct
2906                  parameters.  */
2907               continue;
2908             }
2909           else if (arg->label)
2910             {
2911               has_alternate_specifier = 1;
2912               continue;
2913             }
2914           else
2915             {
2916               /* Pass a NULL pointer for an absent arg.  */
2917               gfc_init_se (&parmse, NULL);
2918               parmse.expr = null_pointer_node;
2919               if (arg->missing_arg_type == BT_CHARACTER)
2920                 parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
2921             }
2922         }
2923       else if (arg->expr->expr_type == EXPR_NULL && fsym && !fsym->attr.pointer)
2924         {
2925           /* Pass a NULL pointer to denote an absent arg.  */
2926           gcc_assert (fsym->attr.optional && !fsym->attr.allocatable);
2927           gfc_init_se (&parmse, NULL);
2928           parmse.expr = null_pointer_node;
2929           if (arg->missing_arg_type == BT_CHARACTER)
2930             parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
2931         }
2932       else if (fsym && fsym->ts.type == BT_CLASS
2933                  && e->ts.type == BT_DERIVED)
2934         {
2935           /* The derived type needs to be converted to a temporary
2936              CLASS object.  */
2937           gfc_init_se (&parmse, se);
2938           gfc_conv_derived_to_class (&parmse, e, fsym->ts);
2939         }
2940       else if (se->ss && se->ss->useflags)
2941         {
2942           /* An elemental function inside a scalarized loop.  */
2943           gfc_init_se (&parmse, se);
2944           gfc_conv_expr_reference (&parmse, e);
2945           parm_kind = ELEMENTAL;
2946         }
2947       else
2948         {
2949           /* A scalar or transformational function.  */
2950           gfc_init_se (&parmse, NULL);
2951           argss = gfc_walk_expr (e);
2952
2953           if (argss == gfc_ss_terminator)
2954             {
2955               if (e->expr_type == EXPR_VARIABLE
2956                     && e->symtree->n.sym->attr.cray_pointee
2957                     && fsym && fsym->attr.flavor == FL_PROCEDURE)
2958                 {
2959                     /* The Cray pointer needs to be converted to a pointer to
2960                        a type given by the expression.  */
2961                     gfc_conv_expr (&parmse, e);
2962                     type = build_pointer_type (TREE_TYPE (parmse.expr));
2963                     tmp = gfc_get_symbol_decl (e->symtree->n.sym->cp_pointer);
2964                     parmse.expr = convert (type, tmp);
2965                 }
2966               else if (fsym && fsym->attr.value)
2967                 {
2968                   if (fsym->ts.type == BT_CHARACTER
2969                       && fsym->ts.is_c_interop
2970                       && fsym->ns->proc_name != NULL
2971                       && fsym->ns->proc_name->attr.is_bind_c)
2972                     {
2973                       parmse.expr = NULL;
2974                       gfc_conv_scalar_char_value (fsym, &parmse, &e);
2975                       if (parmse.expr == NULL)
2976                         gfc_conv_expr (&parmse, e);
2977                     }
2978                   else
2979                     gfc_conv_expr (&parmse, e);
2980                 }
2981               else if (arg->name && arg->name[0] == '%')
2982                 /* Argument list functions %VAL, %LOC and %REF are signalled
2983                    through arg->name.  */
2984                 conv_arglist_function (&parmse, arg->expr, arg->name);
2985               else if ((e->expr_type == EXPR_FUNCTION)
2986                         && ((e->value.function.esym
2987                              && e->value.function.esym->result->attr.pointer)
2988                             || (!e->value.function.esym
2989                                 && e->symtree->n.sym->attr.pointer))
2990                         && fsym && fsym->attr.target)
2991                 {
2992                   gfc_conv_expr (&parmse, e);
2993                   parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
2994                 }
2995               else if (e->expr_type == EXPR_FUNCTION
2996                        && e->symtree->n.sym->result
2997                        && e->symtree->n.sym->result != e->symtree->n.sym
2998                        && e->symtree->n.sym->result->attr.proc_pointer)
2999                 {
3000                   /* Functions returning procedure pointers.  */
3001                   gfc_conv_expr (&parmse, e);
3002                   if (fsym && fsym->attr.proc_pointer)
3003                     parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
3004                 }
3005               else
3006                 {
3007                   gfc_conv_expr_reference (&parmse, e);
3008
3009                   /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is 
3010                      allocated on entry, it must be deallocated.  */
3011                   if (fsym && fsym->attr.allocatable
3012                       && fsym->attr.intent == INTENT_OUT)
3013                     {
3014                       stmtblock_t block;
3015
3016                       gfc_init_block  (&block);
3017                       tmp = gfc_deallocate_with_status (parmse.expr, NULL_TREE,
3018                                                         true, NULL);
3019                       gfc_add_expr_to_block (&block, tmp);
3020                       tmp = fold_build2_loc (input_location, MODIFY_EXPR,
3021                                              void_type_node, parmse.expr,
3022                                              null_pointer_node);
3023                       gfc_add_expr_to_block (&block, tmp);
3024
3025                       if (fsym->attr.optional
3026                           && e->expr_type == EXPR_VARIABLE
3027                           && e->symtree->n.sym->attr.optional)
3028                         {
3029                           tmp = fold_build3_loc (input_location, COND_EXPR,
3030                                      void_type_node,
3031                                      gfc_conv_expr_present (e->symtree->n.sym),
3032                                             gfc_finish_block (&block),
3033                                             build_empty_stmt (input_location));
3034                         }
3035                       else
3036                         tmp = gfc_finish_block (&block);
3037
3038                       gfc_add_expr_to_block (&se->pre, tmp);
3039                     }
3040
3041                   if (fsym && e->expr_type != EXPR_NULL
3042                       && ((fsym->attr.pointer
3043                            && fsym->attr.flavor != FL_PROCEDURE)
3044                           || (fsym->attr.proc_pointer
3045                               && !(e->expr_type == EXPR_VARIABLE
3046                                    && e->symtree->n.sym->attr.dummy))
3047                           || (fsym->attr.proc_pointer
3048                               && e->expr_type == EXPR_VARIABLE
3049                               && gfc_is_proc_ptr_comp (e, NULL))
3050                           || fsym->attr.allocatable))
3051                     {
3052                       /* Scalar pointer dummy args require an extra level of
3053                          indirection. The null pointer already contains
3054                          this level of indirection.  */
3055                       parm_kind = SCALAR_POINTER;
3056                       parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
3057                     }
3058                 }
3059             }
3060           else
3061             {
3062               /* If the procedure requires an explicit interface, the actual
3063                  argument is passed according to the corresponding formal
3064                  argument.  If the corresponding formal argument is a POINTER,
3065                  ALLOCATABLE or assumed shape, we do not use g77's calling
3066                  convention, and pass the address of the array descriptor
3067                  instead. Otherwise we use g77's calling convention.  */
3068               bool f;
3069               f = (fsym != NULL)
3070                   && !(fsym->attr.pointer || fsym->attr.allocatable)
3071                   && fsym->as && fsym->as->type != AS_ASSUMED_SHAPE;
3072               if (comp)
3073                 f = f || !comp->attr.always_explicit;
3074               else
3075                 f = f || !sym->attr.always_explicit;
3076
3077               /* If the argument is a function call that may not create
3078                  a temporary for the result, we have to check that we
3079                  can do it, i.e. that there is no alias between this 
3080                  argument and another one.  */
3081               if (gfc_get_noncopying_intrinsic_argument (e) != NULL)
3082                 {
3083                   gfc_expr *iarg;
3084                   sym_intent intent;
3085
3086                   if (fsym != NULL)
3087                     intent = fsym->attr.intent;
3088                   else
3089                     intent = INTENT_UNKNOWN;
3090
3091                   if (gfc_check_fncall_dependency (e, intent, sym, args,
3092                                                    NOT_ELEMENTAL))
3093                     parmse.force_tmp = 1;
3094
3095                   iarg = e->value.function.actual->expr;
3096
3097                   /* Temporary needed if aliasing due to host association.  */
3098                   if (sym->attr.contained
3099                         && !sym->attr.pure
3100                         && !sym->attr.implicit_pure
3101                         && !sym->attr.use_assoc
3102                         && iarg->expr_type == EXPR_VARIABLE
3103                         && sym->ns == iarg->symtree->n.sym->ns)
3104                     parmse.force_tmp = 1;
3105
3106                   /* Ditto within module.  */
3107                   if (sym->attr.use_assoc
3108                         && !sym->attr.pure
3109                         && !sym->attr.implicit_pure
3110                         && iarg->expr_type == EXPR_VARIABLE
3111                         && sym->module == iarg->symtree->n.sym->module)
3112                     parmse.force_tmp = 1;
3113                 }
3114
3115               if (e->expr_type == EXPR_VARIABLE
3116                     && is_subref_array (e))
3117                 /* The actual argument is a component reference to an
3118                    array of derived types.  In this case, the argument
3119                    is converted to a temporary, which is passed and then
3120                    written back after the procedure call.  */
3121                 gfc_conv_subref_array_arg (&parmse, e, f,
3122                                 fsym ? fsym->attr.intent : INTENT_INOUT,
3123                                 fsym && fsym->attr.pointer);
3124               else
3125                 gfc_conv_array_parameter (&parmse, e, argss, f, fsym,
3126                                           sym->name, NULL);
3127
3128               /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is 
3129                  allocated on entry, it must be deallocated.  */
3130               if (fsym && fsym->attr.allocatable
3131                   && fsym->attr.intent == INTENT_OUT)
3132                 {
3133                   tmp = build_fold_indirect_ref_loc (input_location,
3134                                                      parmse.expr);
3135                   tmp = gfc_trans_dealloc_allocated (tmp);
3136                   if (fsym->attr.optional
3137                       && e->expr_type == EXPR_VARIABLE
3138                       && e->symtree->n.sym->attr.optional)
3139                     tmp = fold_build3_loc (input_location, COND_EXPR,
3140                                      void_type_node,
3141                                      gfc_conv_expr_present (e->symtree->n.sym),
3142                                        tmp, build_empty_stmt (input_location));
3143                   gfc_add_expr_to_block (&se->pre, tmp);
3144                 }
3145             } 
3146         }
3147
3148       /* The case with fsym->attr.optional is that of a user subroutine
3149          with an interface indicating an optional argument.  When we call
3150          an intrinsic subroutine, however, fsym is NULL, but we might still
3151          have an optional argument, so we proceed to the substitution
3152          just in case.  */
3153       if (e && (fsym == NULL || fsym->attr.optional))
3154         {
3155           /* If an optional argument is itself an optional dummy argument,
3156              check its presence and substitute a null if absent.  This is
3157              only needed when passing an array to an elemental procedure
3158              as then array elements are accessed - or no NULL pointer is
3159              allowed and a "1" or "0" should be passed if not present.
3160              When passing a non-array-descriptor full array to a
3161              non-array-descriptor dummy, no check is needed. For
3162              array-descriptor actual to array-descriptor dummy, see
3163              PR 41911 for why a check has to be inserted.
3164              fsym == NULL is checked as intrinsics required the descriptor
3165              but do not always set fsym.  */
3166           if (e->expr_type == EXPR_VARIABLE
3167               && e->symtree->n.sym->attr.optional
3168               && ((e->rank > 0 && sym->attr.elemental)
3169                   || e->representation.length || e->ts.type == BT_CHARACTER
3170                   || (e->rank > 0
3171                       && (fsym == NULL 
3172                           || (fsym-> as
3173                               && (fsym->as->type == AS_ASSUMED_SHAPE
3174                                   || fsym->as->type == AS_DEFERRED))))))
3175             gfc_conv_missing_dummy (&parmse, e, fsym ? fsym->ts : e->ts,
3176                                     e->representation.length);
3177         }
3178
3179       if (fsym && e)
3180         {
3181           /* Obtain the character length of an assumed character length
3182              length procedure from the typespec.  */
3183           if (fsym->ts.type == BT_CHARACTER
3184               && parmse.string_length == NULL_TREE
3185               && e->ts.type == BT_PROCEDURE
3186               && e->symtree->n.sym->ts.type == BT_CHARACTER
3187               && e->symtree->n.sym->ts.u.cl->length != NULL
3188               && e->symtree->n.sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
3189             {
3190               gfc_conv_const_charlen (e->symtree->n.sym->ts.u.cl);
3191               parmse.string_length = e->symtree->n.sym->ts.u.cl->backend_decl;
3192             }
3193         }
3194
3195       if (fsym && need_interface_mapping && e)
3196         gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
3197
3198       gfc_add_block_to_block (&se->pre, &parmse.pre);
3199       gfc_add_block_to_block (&post, &parmse.post);
3200
3201       /* Allocated allocatable components of derived types must be
3202          deallocated for non-variable scalars.  Non-variable arrays are
3203          dealt with in trans-array.c(gfc_conv_array_parameter).  */
3204       if (e && e->ts.type == BT_DERIVED
3205             && e->ts.u.derived->attr.alloc_comp
3206             && !(e->symtree && e->symtree->n.sym->attr.pointer)
3207             && (e->expr_type != EXPR_VARIABLE && !e->rank))
3208         {
3209           int parm_rank;
3210           tmp = build_fold_indirect_ref_loc (input_location,
3211                                          parmse.expr);
3212           parm_rank = e->rank;
3213           switch (parm_kind)
3214             {
3215             case (ELEMENTAL):
3216             case (SCALAR):
3217               parm_rank = 0;
3218               break;
3219
3220             case (SCALAR_POINTER):
3221               tmp = build_fold_indirect_ref_loc (input_location,
3222                                              tmp);
3223               break;
3224             }
3225
3226           if (e->expr_type == EXPR_OP
3227                 && e->value.op.op == INTRINSIC_PARENTHESES
3228                 && e->value.op.op1->expr_type == EXPR_VARIABLE)
3229             {
3230               tree local_tmp;
3231               local_tmp = gfc_evaluate_now (tmp, &se->pre);
3232               local_tmp = gfc_copy_alloc_comp (e->ts.u.derived, local_tmp, tmp, parm_rank);
3233               gfc_add_expr_to_block (&se->post, local_tmp);
3234             }
3235
3236           tmp = gfc_deallocate_alloc_comp (e->ts.u.derived, tmp, parm_rank);
3237
3238           gfc_add_expr_to_block (&se->post, tmp);
3239         }
3240
3241       /* Add argument checking of passing an unallocated/NULL actual to
3242          a nonallocatable/nonpointer dummy.  */
3243
3244       if (gfc_option.rtcheck & GFC_RTCHECK_POINTER && e != NULL)
3245         {
3246           symbol_attribute attr;
3247           char *msg;
3248           tree cond;
3249
3250           if (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
3251             attr = gfc_expr_attr (e);
3252           else
3253             goto end_pointer_check;
3254
3255           if (attr.optional)
3256             {
3257               /* If the actual argument is an optional pointer/allocatable and
3258                  the formal argument takes an nonpointer optional value,
3259                  it is invalid to pass a non-present argument on, even
3260                  though there is no technical reason for this in gfortran.
3261                  See Fortran 2003, Section 12.4.1.6 item (7)+(8).  */
3262               tree present, null_ptr, type;
3263
3264               if (attr.allocatable
3265                   && (fsym == NULL || !fsym->attr.allocatable))
3266                 asprintf (&msg, "Allocatable actual argument '%s' is not "
3267                           "allocated or not present", e->symtree->n.sym->name);
3268               else if (attr.pointer
3269                        && (fsym == NULL || !fsym->attr.pointer))
3270                 asprintf (&msg, "Pointer actual argument '%s' is not "
3271                           "associated or not present",
3272                           e->symtree->n.sym->name);
3273               else if (attr.proc_pointer
3274                        && (fsym == NULL || !fsym->attr.proc_pointer))
3275                 asprintf (&msg, "Proc-pointer actual argument '%s' is not "
3276                           "associated or not present",
3277                           e->symtree->n.sym->name);
3278               else
3279                 goto end_pointer_check;
3280
3281               present = gfc_conv_expr_present (e->symtree->n.sym);
3282               type = TREE_TYPE (present);
3283               present = fold_build2_loc (input_location, EQ_EXPR,
3284                                          boolean_type_node, present,
3285                                          fold_convert (type,
3286                                                        null_pointer_node));
3287               type = TREE_TYPE (parmse.expr);
3288               null_ptr = fold_build2_loc (input_location, EQ_EXPR,
3289                                           boolean_type_node, parmse.expr,
3290                                           fold_convert (type,
3291                                                         null_pointer_node));
3292               cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
3293                                       boolean_type_node, present, null_ptr);
3294             }
3295           else
3296             {
3297               if (attr.allocatable
3298                   && (fsym == NULL || !fsym->attr.allocatable))
3299                 asprintf (&msg, "Allocatable actual argument '%s' is not "
3300                       "allocated", e->symtree->n.sym->name);
3301               else if (attr.pointer
3302                        && (fsym == NULL || !fsym->attr.pointer))
3303                 asprintf (&msg, "Pointer actual argument '%s' is not "
3304                       "associated", e->symtree->n.sym->name);
3305               else if (attr.proc_pointer
3306                        && (fsym == NULL || !fsym->attr.proc_pointer))
3307                 asprintf (&msg, "Proc-pointer actual argument '%s' is not "
3308                       "associated", e->symtree->n.sym->name);
3309               else
3310                 goto end_pointer_check;
3311
3312
3313               cond = fold_build2_loc (input_location, EQ_EXPR,
3314                                       boolean_type_node, parmse.expr,
3315                                       fold_convert (TREE_TYPE (parmse.expr),
3316                                                     null_pointer_node));
3317             }
3318  
3319           gfc_trans_runtime_check (true, false, cond, &se->pre, &e->where,
3320                                    msg);
3321           gfc_free (msg);
3322         }
3323       end_pointer_check:
3324
3325
3326       /* Character strings are passed as two parameters, a length and a
3327          pointer - except for Bind(c) which only passes the pointer.  */
3328       if (parmse.string_length != NULL_TREE && !sym->attr.is_bind_c)
3329         VEC_safe_push (tree, gc, stringargs, parmse.string_length);
3330
3331       VEC_safe_push (tree, gc, arglist, parmse.expr);
3332     }
3333   gfc_finish_interface_mapping (&mapping, &se->pre, &se->post);
3334
3335   if (comp)
3336     ts = comp->ts;
3337   else
3338    ts = sym->ts;
3339
3340   if (ts.type == BT_CHARACTER && sym->attr.is_bind_c)
3341     se->string_length = build_int_cst (gfc_charlen_type_node, 1);
3342   else if (ts.type == BT_CHARACTER)
3343     {
3344       if (ts.u.cl->length == NULL)
3345         {
3346           /* Assumed character length results are not allowed by 5.1.1.5 of the
3347              standard and are trapped in resolve.c; except in the case of SPREAD
3348              (and other intrinsics?) and dummy functions.  In the case of SPREAD,
3349              we take the character length of the first argument for the result.
3350              For dummies, we have to look through the formal argument list for
3351              this function and use the character length found there.*/
3352           if (!sym->attr.dummy)
3353             cl.backend_decl = VEC_index (tree, stringargs, 0);
3354           else
3355             {
3356               formal = sym->ns->proc_name->formal;
3357               for (; formal; formal = formal->next)
3358                 if (strcmp (formal->sym->name, sym->name) == 0)
3359                   cl.backend_decl = formal->sym->ts.u.cl->backend_decl;
3360             }
3361         }
3362       else
3363         {
3364           tree tmp;
3365
3366           /* Calculate the length of the returned string.  */
3367           gfc_init_se (&parmse, NULL);
3368           if (need_interface_mapping)
3369             gfc_apply_interface_mapping (&mapping, &parmse, ts.u.cl->length);
3370           else
3371             gfc_conv_expr (&parmse, ts.u.cl->length);
3372           gfc_add_block_to_block (&se->pre, &parmse.pre);
3373           gfc_add_block_to_block (&se->post, &parmse.post);
3374           
3375           tmp = fold_convert (gfc_charlen_type_node, parmse.expr);
3376           tmp = fold_build2_loc (input_location, MAX_EXPR,
3377                                  gfc_charlen_type_node, tmp,
3378                                  build_int_cst (gfc_charlen_type_node, 0));
3379           cl.backend_decl = tmp;
3380         }
3381
3382       /* Set up a charlen structure for it.  */
3383       cl.next = NULL;
3384       cl.length = NULL;
3385       ts.u.cl = &cl;
3386
3387       len = cl.backend_decl;
3388     }
3389
3390   byref = (comp && (comp->attr.dimension || comp->ts.type == BT_CHARACTER))
3391           || (!comp && gfc_return_by_reference (sym));
3392   if (byref)
3393     {
3394       if (se->direct_byref)
3395         {
3396           /* Sometimes, too much indirection can be applied; e.g. for
3397              function_result = array_valued_recursive_function.  */
3398           if (TREE_TYPE (TREE_TYPE (se->expr))
3399                 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))
3400                 && GFC_DESCRIPTOR_TYPE_P
3401                         (TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))))
3402             se->expr = build_fold_indirect_ref_loc (input_location,
3403                                                 se->expr);
3404
3405           /* If the lhs of an assignment x = f(..) is allocatable and
3406              f2003 is allowed, we must do the automatic reallocation.
3407              TODO - deal with intrinsics, without using a temporary.  */
3408           if (gfc_option.flag_realloc_lhs
3409                 && se->ss && se->ss->loop_chain
3410                 && se->ss->loop_chain->is_alloc_lhs
3411                 && !expr->value.function.isym
3412                 && sym->result->as != NULL)
3413             {
3414               /* Evaluate the bounds of the result, if known.  */
3415               gfc_set_loop_bounds_from_array_spec (&mapping, se,
3416                                                    sym->result->as);
3417
3418               /* Perform the automatic reallocation.  */
3419               tmp = gfc_alloc_allocatable_for_assignment (se->loop,
3420                                                           expr, NULL);
3421               gfc_add_expr_to_block (&se->pre, tmp);
3422
3423               /* Pass the temporary as the first argument.  */
3424               result = info->descriptor;
3425             }
3426           else
3427             result = build_fold_indirect_ref_loc (input_location,
3428                                                   se->expr);
3429           VEC_safe_push (tree, gc, retargs, se->expr);
3430         }
3431       else if (comp && comp->attr.dimension)
3432         {
3433           gcc_assert (se->loop && info);
3434
3435           /* Set the type of the array.  */
3436           tmp = gfc_typenode_for_spec (&comp->ts);
3437           info->dimen = se->loop->dimen;
3438
3439           /* Evaluate the bounds of the result, if known.  */
3440           gfc_set_loop_bounds_from_array_spec (&mapping, se, comp->as);
3441
3442           /* If the lhs of an assignment x = f(..) is allocatable and
3443              f2003 is allowed, we must not generate the function call
3444              here but should just send back the results of the mapping.
3445              This is signalled by the function ss being flagged.  */
3446           if (gfc_option.flag_realloc_lhs
3447                 && se->ss && se->ss->is_alloc_lhs)
3448             {
3449               gfc_free_interface_mapping (&mapping);
3450               return has_alternate_specifier;
3451             }
3452
3453           /* Create a temporary to store the result.  In case the function
3454              returns a pointer, the temporary will be a shallow copy and
3455              mustn't be deallocated.  */
3456           callee_alloc = comp->attr.allocatable || comp->attr.pointer;
3457           gfc_trans_create_temp_array (&se->pre, &se->post, se->loop, info, tmp,
3458                                        NULL_TREE, false, !comp->attr.pointer,
3459                                        callee_alloc, &se->ss->expr->where);
3460
3461           /* Pass the temporary as the first argument.  */
3462           result = info->descriptor;
3463           tmp = gfc_build_addr_expr (NULL_TREE, result);
3464           VEC_safe_push (tree, gc, retargs, tmp);
3465         }
3466       else if (!comp && sym->result->attr.dimension)
3467         {
3468           gcc_assert (se->loop && info);
3469
3470           /* Set the type of the array.  */
3471           tmp = gfc_typenode_for_spec (&ts);
3472           info->dimen = se->loop->dimen;
3473
3474           /* Evaluate the bounds of the result, if known.  */
3475           gfc_set_loop_bounds_from_array_spec (&mapping, se, sym->result->as);
3476
3477           /* If the lhs of an assignment x = f(..) is allocatable and
3478              f2003 is allowed, we must not generate the function call
3479              here but should just send back the results of the mapping.
3480              This is signalled by the function ss being flagged.  */
3481           if (gfc_option.flag_realloc_lhs
3482                 && se->ss && se->ss->is_alloc_lhs)
3483             {
3484               gfc_free_interface_mapping (&mapping);
3485               return has_alternate_specifier;
3486             }
3487
3488           /* Create a temporary to store the result.  In case the function
3489              returns a pointer, the temporary will be a shallow copy and
3490              mustn't be deallocated.  */
3491           callee_alloc = sym->attr.allocatable || sym->attr.pointer;
3492           gfc_trans_create_temp_array (&se->pre, &se->post, se->loop, info, tmp,
3493                                        NULL_TREE, false, !sym->attr.pointer,
3494                                        callee_alloc, &se->ss->expr->where);
3495
3496           /* Pass the temporary as the first argument.  */
3497           result = info->descriptor;
3498           tmp = gfc_build_addr_expr (NULL_TREE, result);
3499           VEC_safe_push (tree, gc, retargs, tmp);
3500         }
3501       else if (ts.type == BT_CHARACTER)
3502         {
3503           /* Pass the string length.  */
3504           type = gfc_get_character_type (ts.kind, ts.u.cl);
3505           type = build_pointer_type (type);
3506
3507           /* Return an address to a char[0:len-1]* temporary for
3508              character pointers.  */
3509           if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
3510                || (comp && (comp->attr.pointer || comp->attr.allocatable)))
3511             {
3512               var = gfc_create_var (type, "pstr");
3513
3514               if ((!comp && sym->attr.allocatable)
3515                   || (comp && comp->attr.allocatable))
3516                 gfc_add_modify (&se->pre, var,
3517                                 fold_convert (TREE_TYPE (var),
3518                                               null_pointer_node));
3519
3520               /* Provide an address expression for the function arguments.  */
3521               var = gfc_build_addr_expr (NULL_TREE, var);
3522             }
3523           else
3524             var = gfc_conv_string_tmp (se, type, len);
3525
3526           VEC_safe_push (tree, gc, retargs, var);
3527         }
3528       else
3529         {
3530           gcc_assert (gfc_option.flag_f2c && ts.type == BT_COMPLEX);
3531
3532           type = gfc_get_complex_type (ts.kind);
3533           var = gfc_build_addr_expr (NULL_TREE, gfc_create_var (type, "cmplx"));
3534           VEC_safe_push (tree, gc, retargs, var);
3535         }
3536
3537       /* Add the string length to the argument list.  */
3538       if (ts.type == BT_CHARACTER)
3539         VEC_safe_push (tree, gc, retargs, len);
3540     }
3541   gfc_free_interface_mapping (&mapping);
3542
3543   /* We need to glom RETARGS + ARGLIST + STRINGARGS + APPEND_ARGS.  */
3544   arglen = (VEC_length (tree, arglist)
3545             + VEC_length (tree, stringargs) + VEC_length (tree, append_args));
3546   VEC_reserve_exact (tree, gc, retargs, arglen);
3547
3548   /* Add the return arguments.  */
3549   VEC_splice (tree, retargs, arglist);
3550
3551   /* Add the hidden string length parameters to the arguments.  */
3552   VEC_splice (tree, retargs, stringargs);
3553
3554   /* We may want to append extra arguments here.  This is used e.g. for
3555      calls to libgfortran_matmul_??, which need extra information.  */
3556   if (!VEC_empty (tree, append_args))
3557     VEC_splice (tree, retargs, append_args);
3558   arglist = retargs;
3559
3560   /* Generate the actual call.  */
3561   conv_function_val (se, sym, expr);
3562
3563   /* If there are alternate return labels, function type should be
3564      integer.  Can't modify the type in place though, since it can be shared
3565      with other functions.  For dummy arguments, the typing is done to
3566      to this result, even if it has to be repeated for each call.  */
3567   if (has_alternate_specifier
3568       && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) != integer_type_node)
3569     {
3570       if (!sym->attr.dummy)
3571         {
3572           TREE_TYPE (sym->backend_decl)
3573                 = build_function_type (integer_type_node,
3574                       TYPE_ARG_TYPES (TREE_TYPE (sym->backend_decl)));
3575           se->expr = gfc_build_addr_expr (NULL_TREE, sym->backend_decl);
3576         }
3577       else
3578         TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) = integer_type_node;
3579     }
3580
3581   fntype = TREE_TYPE (TREE_TYPE (se->expr));
3582   se->expr = build_call_vec (TREE_TYPE (fntype), se->expr, arglist);
3583
3584   /* If we have a pointer function, but we don't want a pointer, e.g.
3585      something like
3586         x = f()
3587      where f is pointer valued, we have to dereference the result.  */
3588   if (!se->want_pointer && !byref
3589       && (sym->attr.pointer || sym->attr.allocatable)
3590       && !gfc_is_proc_ptr_comp (expr, NULL))
3591     se->expr = build_fold_indirect_ref_loc (input_location,
3592                                         se->expr);
3593
3594   /* f2c calling conventions require a scalar default real function to
3595      return a double precision result.  Convert this back to default
3596      real.  We only care about the cases that can happen in Fortran 77.
3597   */
3598   if (gfc_option.flag_f2c && sym->ts.type == BT_REAL
3599       && sym->ts.kind == gfc_default_real_kind
3600       && !sym->attr.always_explicit)
3601     se->expr = fold_convert (gfc_get_real_type (sym->ts.kind), se->expr);
3602
3603   /* A pure function may still have side-effects - it may modify its
3604      parameters.  */
3605   TREE_SIDE_EFFECTS (se->expr) = 1;
3606 #if 0
3607   if (!sym->attr.pure)
3608     TREE_SIDE_EFFECTS (se->expr) = 1;
3609 #endif
3610
3611   if (byref)
3612     {
3613       /* Add the function call to the pre chain.  There is no expression.  */
3614       gfc_add_expr_to_block (&se->pre, se->expr);
3615       se->expr = NULL_TREE;
3616
3617       if (!se->direct_byref)
3618         {
3619           if ((sym->attr.dimension && !comp) || (comp && comp->attr.dimension))
3620             {
3621               if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
3622                 {
3623                   /* Check the data pointer hasn't been modified.  This would
3624                      happen in a function returning a pointer.  */
3625                   tmp = gfc_conv_descriptor_data_get (info->descriptor);
3626                   tmp = fold_build2_loc (input_location, NE_EXPR,
3627                                          boolean_type_node,
3628                                          tmp, info->data);
3629                   gfc_trans_runtime_check (true, false, tmp, &se->pre, NULL,
3630                                            gfc_msg_fault);
3631                 }
3632               se->expr = info->descriptor;
3633               /* Bundle in the string length.  */
3634               se->string_length = len;
3635             }
3636           else if (ts.type == BT_CHARACTER)
3637             {
3638               /* Dereference for character pointer results.  */
3639               if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
3640                   || (comp && (comp->attr.pointer || comp->attr.allocatable)))
3641                 se->expr = build_fold_indirect_ref_loc (input_location, var);
3642               else
3643                 se->expr = var;
3644
3645               se->string_length = len;
3646             }
3647           else
3648             {
3649               gcc_assert (ts.type == BT_COMPLEX && gfc_option.flag_f2c);
3650               se->expr = build_fold_indirect_ref_loc (input_location, var);
3651             }
3652         }
3653     }
3654
3655   /* Follow the function call with the argument post block.  */
3656   if (byref)
3657     {
3658       gfc_add_block_to_block (&se->pre, &post);
3659
3660       /* Transformational functions of derived types with allocatable
3661          components must have the result allocatable components copied.  */
3662       arg = expr->value.function.actual;
3663       if (result && arg && expr->rank
3664             && expr->value.function.isym
3665             && expr->value.function.isym->transformational
3666             && arg->expr->ts.type == BT_DERIVED
3667             && arg->expr->ts.u.derived->attr.alloc_comp)
3668         {
3669           tree tmp2;
3670           /* Copy the allocatable components.  We have to use a
3671              temporary here to prevent source allocatable components
3672              from being corrupted.  */
3673           tmp2 = gfc_evaluate_now (result, &se->pre);
3674           tmp = gfc_copy_alloc_comp (arg->expr->ts.u.derived,
3675                                      result, tmp2, expr->rank);
3676           gfc_add_expr_to_block (&se->pre, tmp);
3677           tmp = gfc_copy_allocatable_data (result, tmp2, TREE_TYPE(tmp2),
3678                                            expr->rank);
3679           gfc_add_expr_to_block (&se->pre, tmp);
3680
3681           /* Finally free the temporary's data field.  */
3682           tmp = gfc_conv_descriptor_data_get (tmp2);
3683           tmp = gfc_deallocate_with_status (tmp, NULL_TREE, true, NULL);
3684           gfc_add_expr_to_block (&se->pre, tmp);
3685         }
3686     }
3687   else
3688     gfc_add_block_to_block (&se->post, &post);
3689
3690   return has_alternate_specifier;
3691 }
3692
3693
3694 /* Fill a character string with spaces.  */
3695
3696 static tree
3697 fill_with_spaces (tree start, tree type, tree size)
3698 {
3699   stmtblock_t block, loop;
3700   tree i, el, exit_label, cond, tmp;
3701
3702   /* For a simple char type, we can call memset().  */
3703   if (compare_tree_int (TYPE_SIZE_UNIT (type), 1) == 0)
3704     return build_call_expr_loc (input_location,
3705                             built_in_decls[BUILT_IN_MEMSET], 3, start,
3706                             build_int_cst (gfc_get_int_type (gfc_c_int_kind),
3707                                            lang_hooks.to_target_charset (' ')),
3708                             size);
3709
3710   /* Otherwise, we use a loop:
3711         for (el = start, i = size; i > 0; el--, i+= TYPE_SIZE_UNIT (type))
3712           *el = (type) ' ';
3713    */
3714
3715   /* Initialize variables.  */
3716   gfc_init_block (&block);
3717   i = gfc_create_var (sizetype, "i");
3718   gfc_add_modify (&block, i, fold_convert (sizetype, size));
3719   el = gfc_create_var (build_pointer_type (type), "el");
3720   gfc_add_modify (&block, el, fold_convert (TREE_TYPE (el), start));
3721   exit_label = gfc_build_label_decl (NULL_TREE);
3722   TREE_USED (exit_label) = 1;
3723
3724
3725   /* Loop body.  */
3726   gfc_init_block (&loop);
3727
3728   /* Exit condition.  */
3729   cond = fold_build2_loc (input_location, LE_EXPR, boolean_type_node, i,
3730                           build_zero_cst (sizetype));
3731   tmp = build1_v (GOTO_EXPR, exit_label);
3732   tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
3733                          build_empty_stmt (input_location));
3734   gfc_add_expr_to_block (&loop, tmp);
3735
3736   /* Assignment.  */
3737   gfc_add_modify (&loop,
3738                   fold_build1_loc (input_location, INDIRECT_REF, type, el),
3739                   build_int_cst (type, lang_hooks.to_target_charset (' ')));
3740
3741   /* Increment loop variables.  */
3742   gfc_add_modify (&loop, i,
3743                   fold_build2_loc (input_location, MINUS_EXPR, sizetype, i,
3744                                    TYPE_SIZE_UNIT (type)));
3745   gfc_add_modify (&loop, el,
3746                   fold_build2_loc (input_location, POINTER_PLUS_EXPR,
3747                                    TREE_TYPE (el), el, TYPE_SIZE_UNIT (type)));
3748
3749   /* Making the loop... actually loop!  */
3750   tmp = gfc_finish_block (&loop);
3751   tmp = build1_v (LOOP_EXPR, tmp);
3752   gfc_add_expr_to_block (&block, tmp);
3753
3754   /* The exit label.  */
3755   tmp = build1_v (LABEL_EXPR, exit_label);
3756   gfc_add_expr_to_block (&block, tmp);
3757
3758
3759   return gfc_finish_block (&block);
3760 }
3761
3762
3763 /* Generate code to copy a string.  */
3764
3765 void
3766 gfc_trans_string_copy (stmtblock_t * block, tree dlength, tree dest,
3767                        int dkind, tree slength, tree src, int skind)
3768 {
3769   tree tmp, dlen, slen;
3770   tree dsc;
3771   tree ssc;
3772   tree cond;
3773   tree cond2;
3774   tree tmp2;
3775   tree tmp3;
3776   tree tmp4;
3777   tree chartype;
3778   stmtblock_t tempblock;
3779
3780   gcc_assert (dkind == skind);
3781
3782   if (slength != NULL_TREE)
3783     {
3784       slen = fold_convert (size_type_node, gfc_evaluate_now (slength, block));
3785       ssc = gfc_string_to_single_character (slen, src, skind);
3786     }
3787   else
3788     {
3789       slen = build_int_cst (size_type_node, 1);
3790       ssc =  src;
3791     }
3792
3793   if (dlength != NULL_TREE)
3794     {
3795       dlen = fold_convert (size_type_node, gfc_evaluate_now (dlength, block));
3796       dsc = gfc_string_to_single_character (dlen, dest, dkind);
3797     }
3798   else
3799     {
3800       dlen = build_int_cst (size_type_node, 1);
3801       dsc =  dest;
3802     }
3803
3804   /* Assign directly if the types are compatible.  */
3805   if (dsc != NULL_TREE && ssc != NULL_TREE
3806       && TREE_TYPE (dsc) == TREE_TYPE (ssc))
3807     {
3808       gfc_add_modify (block, dsc, ssc);
3809       return;
3810     }
3811
3812   /* Do nothing if the destination length is zero.  */
3813   cond = fold_build2_loc (input_location, GT_EXPR, boolean_type_node, dlen,
3814                           build_int_cst (size_type_node, 0));
3815
3816   /* The following code was previously in _gfortran_copy_string:
3817
3818        // The two strings may overlap so we use memmove.
3819        void
3820        copy_string (GFC_INTEGER_4 destlen, char * dest,
3821                     GFC_INTEGER_4 srclen, const char * src)
3822        {
3823          if (srclen >= destlen)
3824            {
3825              // This will truncate if too long.
3826              memmove (dest, src, destlen);
3827            }
3828          else
3829            {
3830              memmove (dest, src, srclen);
3831              // Pad with spaces.
3832              memset (&dest[srclen], ' ', destlen - srclen);
3833            }
3834        }
3835
3836      We're now doing it here for better optimization, but the logic
3837      is the same.  */
3838
3839   /* For non-default character kinds, we have to multiply the string
3840      length by the base type size.  */
3841   chartype = gfc_get_char_type (dkind);
3842   slen = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
3843                           fold_convert (size_type_node, slen),
3844                           fold_convert (size_type_node,
3845                                         TYPE_SIZE_UNIT (chartype)));
3846   dlen = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
3847                           fold_convert (size_type_node, dlen),
3848                           fold_convert (size_type_node,
3849                                         TYPE_SIZE_UNIT (chartype)));
3850
3851   if (dlength && POINTER_TYPE_P (TREE_TYPE (dest)))
3852     dest = fold_convert (pvoid_type_node, dest);
3853   else
3854     dest = gfc_build_addr_expr (pvoid_type_node, dest);
3855
3856   if (slength && POINTER_TYPE_P (TREE_TYPE (src)))
3857     src = fold_convert (pvoid_type_node, src);
3858   else
3859     src = gfc_build_addr_expr (pvoid_type_node, src);
3860
3861   /* Truncate string if source is too long.  */
3862   cond2 = fold_build2_loc (input_location, GE_EXPR, boolean_type_node, slen,
3863                            dlen);
3864   tmp2 = build_call_expr_loc (input_location,
3865                           built_in_decls[BUILT_IN_MEMMOVE],
3866                           3, dest, src, dlen);
3867
3868   /* Else copy and pad with spaces.  */
3869   tmp3 = build_call_expr_loc (input_location,
3870                           built_in_decls[BUILT_IN_MEMMOVE],
3871                           3, dest, src, slen);
3872
3873   tmp4 = fold_build2_loc (input_location, POINTER_PLUS_EXPR, TREE_TYPE (dest),
3874                           dest, fold_convert (sizetype, slen));
3875   tmp4 = fill_with_spaces (tmp4, chartype,
3876                            fold_build2_loc (input_location, MINUS_EXPR,
3877                                             TREE_TYPE(dlen), dlen, slen));
3878
3879   gfc_init_block (&tempblock);
3880   gfc_add_expr_to_block (&tempblock, tmp3);
3881   gfc_add_expr_to_block (&tempblock, tmp4);
3882   tmp3 = gfc_finish_block (&tempblock);
3883
3884   /* The whole copy_string function is there.  */
3885   tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
3886                          tmp2, tmp3);
3887   tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
3888                          build_empty_stmt (input_location));
3889   gfc_add_expr_to_block (block, tmp);
3890 }
3891
3892
3893 /* Translate a statement function.
3894    The value of a statement function reference is obtained by evaluating the
3895    expression using the values of the actual arguments for the values of the
3896    corresponding dummy arguments.  */
3897
3898 static void
3899 gfc_conv_statement_function (gfc_se * se, gfc_expr * expr)
3900 {
3901   gfc_symbol *sym;
3902   gfc_symbol *fsym;
3903   gfc_formal_arglist *fargs;
3904   gfc_actual_arglist *args;
3905   gfc_se lse;
3906   gfc_se rse;
3907   gfc_saved_var *saved_vars;
3908   tree *temp_vars;
3909   tree type;
3910   tree tmp;
3911   int n;
3912
3913   sym = expr->symtree->n.sym;
3914   args = expr->value.function.actual;
3915   gfc_init_se (&lse, NULL);
3916   gfc_init_se (&rse, NULL);
3917
3918   n = 0;
3919   for (fargs = sym->formal; fargs; fargs = fargs->next)
3920     n++;
3921   saved_vars = (gfc_saved_var *)gfc_getmem (n * sizeof (gfc_saved_var));
3922   temp_vars = (tree *)gfc_getmem (n * sizeof (tree));
3923
3924   for (fargs = sym->formal, n = 0; fargs; fargs = fargs->next, n++)
3925     {
3926       /* Each dummy shall be specified, explicitly or implicitly, to be
3927          scalar.  */
3928       gcc_assert (fargs->sym->attr.dimension == 0);
3929       fsym = fargs->sym;
3930
3931       if (fsym->ts.type == BT_CHARACTER)
3932         {
3933           /* Copy string arguments.  */
3934           tree arglen;
3935
3936           gcc_assert (fsym->ts.u.cl && fsym->ts.u.cl->length
3937                       && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT);
3938
3939           /* Create a temporary to hold the value.  */
3940           if (fsym->ts.u.cl->backend_decl == NULL_TREE)
3941              fsym->ts.u.cl->backend_decl
3942                 = gfc_conv_constant_to_tree (fsym->ts.u.cl->length);
3943
3944           type = gfc_get_character_type (fsym->ts.kind, fsym->ts.u.cl);
3945           temp_vars[n] = gfc_create_var (type, fsym->name);
3946
3947           arglen = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
3948
3949           gfc_conv_expr (&rse, args->expr);
3950           gfc_conv_string_parameter (&rse);
3951           gfc_add_block_to_block (&se->pre, &lse.pre);
3952           gfc_add_block_to_block (&se->pre, &rse.pre);
3953
3954           gfc_trans_string_copy (&se->pre, arglen, temp_vars[n], fsym->ts.kind,
3955                                  rse.string_length, rse.expr, fsym->ts.kind);
3956           gfc_add_block_to_block (&se->pre, &lse.post);
3957           gfc_add_block_to_block (&se->pre, &rse.post);
3958         }
3959       else
3960         {
3961           /* For everything else, just evaluate the expression.  */
3962
3963           /* Create a temporary to hold the value.  */
3964           type = gfc_typenode_for_spec (&fsym->ts);
3965           temp_vars[n] = gfc_create_var (type, fsym->name);
3966
3967           gfc_conv_expr (&lse, args->expr);
3968
3969           gfc_add_block_to_block (&se->pre, &lse.pre);
3970           gfc_add_modify (&se->pre, temp_vars[n], lse.expr);
3971           gfc_add_block_to_block (&se->pre, &lse.post);
3972         }
3973
3974       args = args->next;
3975     }
3976
3977   /* Use the temporary variables in place of the real ones.  */
3978   for (fargs = sym->formal, n = 0; fargs; fargs = fargs->next, n++)
3979     gfc_shadow_sym (fargs->sym, temp_vars[n], &saved_vars[n]);
3980
3981   gfc_conv_expr (se, sym->value);
3982
3983   if (sym->ts.type == BT_CHARACTER)
3984     {
3985       gfc_conv_const_charlen (sym->ts.u.cl);
3986
3987       /* Force the expression to the correct length.  */
3988       if (!INTEGER_CST_P (se->string_length)
3989           || tree_int_cst_lt (se->string_length,
3990                               sym->ts.u.cl->backend_decl))
3991         {
3992           type = gfc_get_character_type (sym->ts.kind, sym->ts.u.cl);
3993           tmp = gfc_create_var (type, sym->name);
3994           tmp = gfc_build_addr_expr (build_pointer_type (type), tmp);
3995           gfc_trans_string_copy (&se->pre, sym->ts.u.cl->backend_decl, tmp,
3996                                  sym->ts.kind, se->string_length, se->expr,
3997                                  sym->ts.kind);
3998           se->expr = tmp;
3999         }
4000       se->string_length = sym->ts.u.cl->backend_decl;
4001     }
4002
4003   /* Restore the original variables.  */
4004   for (fargs = sym->formal, n = 0; fargs; fargs = fargs->next, n++)
4005     gfc_restore_sym (fargs->sym, &saved_vars[n]);
4006   gfc_free (saved_vars);
4007 }
4008
4009
4010 /* Translate a function expression.  */
4011
4012 static void
4013 gfc_conv_function_expr (gfc_se * se, gfc_expr * expr)
4014 {
4015   gfc_symbol *sym;
4016
4017   if (expr->value.function.isym)
4018     {
4019       gfc_conv_intrinsic_function (se, expr);
4020       return;
4021     }
4022
4023   /* We distinguish statement functions from general functions to improve
4024      runtime performance.  */
4025   if (expr->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
4026     {
4027       gfc_conv_statement_function (se, expr);
4028       return;
4029     }
4030
4031   /* expr.value.function.esym is the resolved (specific) function symbol for
4032      most functions.  However this isn't set for dummy procedures.  */
4033   sym = expr->value.function.esym;
4034   if (!sym)
4035     sym = expr->symtree->n.sym;
4036
4037   gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr, NULL);
4038 }
4039
4040
4041 /* Determine whether the given EXPR_CONSTANT is a zero initializer.  */
4042
4043 static bool
4044 is_zero_initializer_p (gfc_expr * expr)
4045 {
4046   if (expr->expr_type != EXPR_CONSTANT)
4047     return false;
4048
4049   /* We ignore constants with prescribed memory representations for now.  */
4050   if (expr->representation.string)
4051     return false;
4052
4053   switch (expr->ts.type)
4054     {
4055     case BT_INTEGER:
4056       return mpz_cmp_si (expr->value.integer, 0) == 0;
4057
4058     case BT_REAL:
4059       return mpfr_zero_p (expr->value.real)
4060              && MPFR_SIGN (expr->value.real) >= 0;
4061
4062     case BT_LOGICAL:
4063       return expr->value.logical == 0;
4064
4065     case BT_COMPLEX:
4066       return mpfr_zero_p (mpc_realref (expr->value.complex))
4067              && MPFR_SIGN (mpc_realref (expr->value.complex)) >= 0
4068              && mpfr_zero_p (mpc_imagref (expr->value.complex))
4069              && MPFR_SIGN (mpc_imagref (expr->value.complex)) >= 0;
4070
4071     default:
4072       break;
4073     }
4074   return false;
4075 }
4076
4077
4078 static void
4079 gfc_conv_array_constructor_expr (gfc_se * se, gfc_expr * expr)
4080 {
4081   gcc_assert (se->ss != NULL && se->ss != gfc_ss_terminator);
4082   gcc_assert (se->ss->expr == expr && se->ss->type == GFC_SS_CONSTRUCTOR);
4083
4084   gfc_conv_tmp_array_ref (se);
4085 }
4086
4087
4088 /* Build a static initializer.  EXPR is the expression for the initial value.
4089    The other parameters describe the variable of the component being 
4090    initialized. EXPR may be null.  */
4091
4092 tree
4093 gfc_conv_initializer (gfc_expr * expr, gfc_typespec * ts, tree type,
4094                       bool array, bool pointer, bool procptr)
4095 {
4096   gfc_se se;
4097
4098   if (!(expr || pointer || procptr))
4099     return NULL_TREE;
4100
4101   /* Check if we have ISOCBINDING_NULL_PTR or ISOCBINDING_NULL_FUNPTR
4102      (these are the only two iso_c_binding derived types that can be
4103      used as initialization expressions).  If so, we need to modify
4104      the 'expr' to be that for a (void *).  */
4105   if (expr != NULL && expr->ts.type == BT_DERIVED
4106       && expr->ts.is_iso_c && expr->ts.u.derived)
4107     {
4108       gfc_symbol *derived = expr->ts.u.derived;
4109
4110       /* The derived symbol has already been converted to a (void *).  Use
4111          its kind.  */
4112       expr = gfc_get_int_expr (derived->ts.kind, NULL, 0);
4113       expr->ts.f90_type = derived->ts.f90_type;
4114
4115       gfc_init_se (&se, NULL);
4116       gfc_conv_constant (&se, expr);
4117       gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
4118       return se.expr;
4119     }
4120   
4121   if (array && !procptr)
4122     {
4123       tree ctor;
4124       /* Arrays need special handling.  */
4125       if (pointer)
4126         ctor = gfc_build_null_descriptor (type);
4127       /* Special case assigning an array to zero.  */
4128       else if (is_zero_initializer_p (expr))
4129         ctor = build_constructor (type, NULL);
4130       else
4131         ctor = gfc_conv_array_initializer (type, expr);
4132       TREE_STATIC (ctor) = 1;
4133       return ctor;
4134     }
4135   else if (pointer || procptr)
4136     {
4137       if (!expr || expr->expr_type == EXPR_NULL)
4138         return fold_convert (type, null_pointer_node);
4139       else
4140         {
4141           gfc_init_se (&se, NULL);
4142           se.want_pointer = 1;
4143           gfc_conv_expr (&se, expr);
4144           gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
4145           return se.expr;
4146         }
4147     }
4148   else
4149     {
4150       switch (ts->type)
4151         {
4152         case BT_DERIVED:
4153         case BT_CLASS:
4154           gfc_init_se (&se, NULL);
4155           if (ts->type == BT_CLASS && expr->expr_type == EXPR_NULL)
4156             gfc_conv_structure (&se, gfc_class_null_initializer(ts), 1);
4157           else
4158             gfc_conv_structure (&se, expr, 1);
4159           gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
4160           TREE_STATIC (se.expr) = 1;
4161           return se.expr;
4162
4163         case BT_CHARACTER:
4164           {
4165             tree ctor = gfc_conv_string_init (ts->u.cl->backend_decl,expr);
4166             TREE_STATIC (ctor) = 1;
4167             return ctor;
4168           }
4169
4170         default:
4171           gfc_init_se (&se, NULL);
4172           gfc_conv_constant (&se, expr);
4173           gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
4174           return se.expr;
4175         }
4176     }
4177 }
4178   
4179 static tree
4180 gfc_trans_subarray_assign (tree dest, gfc_component * cm, gfc_expr * expr)
4181 {
4182   gfc_se rse;
4183   gfc_se lse;
4184   gfc_ss *rss;
4185   gfc_ss *lss;
4186   stmtblock_t body;
4187   stmtblock_t block;
4188   gfc_loopinfo loop;
4189   int n;
4190   tree tmp;
4191
4192   gfc_start_block (&block);
4193
4194   /* Initialize the scalarizer.  */
4195   gfc_init_loopinfo (&loop);
4196
4197   gfc_init_se (&lse, NULL);
4198   gfc_init_se (&rse, NULL);
4199
4200   /* Walk the rhs.  */
4201   rss = gfc_walk_expr (expr);
4202   if (rss == gfc_ss_terminator)
4203     {
4204       /* The rhs is scalar.  Add a ss for the expression.  */
4205       rss = gfc_get_ss ();
4206       rss->next = gfc_ss_terminator;
4207       rss->type = GFC_SS_SCALAR;
4208       rss->expr = expr;
4209     }
4210
4211   /* Create a SS for the destination.  */
4212   lss = gfc_get_ss ();
4213   lss->type = GFC_SS_COMPONENT;
4214   lss->expr = NULL;
4215   lss->shape = gfc_get_shape (cm->as->rank);
4216   lss->next = gfc_ss_terminator;
4217   lss->data.info.dimen = cm->as->rank;
4218   lss->data.info.descriptor = dest;
4219   lss->data.info.data = gfc_conv_array_data (dest);
4220   lss->data.info.offset = gfc_conv_array_offset (dest);
4221   for (n = 0; n < cm->as->rank; n++)
4222     {
4223       lss->data.info.dim[n] = n;
4224       lss->data.info.start[n] = gfc_conv_array_lbound (dest, n);
4225       lss->data.info.stride[n] = gfc_index_one_node;
4226
4227       mpz_init (lss->shape[n]);
4228       mpz_sub (lss->shape[n], cm->as->upper[n]->value.integer,
4229                cm->as->lower[n]->value.integer);
4230       mpz_add_ui (lss->shape[n], lss->shape[n], 1);
4231     }
4232   
4233   /* Associate the SS with the loop.  */
4234   gfc_add_ss_to_loop (&loop, lss);
4235   gfc_add_ss_to_loop (&loop, rss);
4236
4237   /* Calculate the bounds of the scalarization.  */
4238   gfc_conv_ss_startstride (&loop);
4239
4240   /* Setup the scalarizing loops.  */
4241   gfc_conv_loop_setup (&loop, &expr->where);
4242
4243   /* Setup the gfc_se structures.  */
4244   gfc_copy_loopinfo_to_se (&lse, &loop);
4245   gfc_copy_loopinfo_to_se (&rse, &loop);
4246
4247   rse.ss = rss;
4248   gfc_mark_ss_chain_used (rss, 1);
4249   lse.ss = lss;
4250   gfc_mark_ss_chain_used (lss, 1);
4251
4252   /* Start the scalarized loop body.  */
4253   gfc_start_scalarized_body (&loop, &body);
4254
4255   gfc_conv_tmp_array_ref (&lse);
4256   if (cm->ts.type == BT_CHARACTER)
4257     lse.string_length = cm->ts.u.cl->backend_decl;
4258
4259   gfc_conv_expr (&rse, expr);
4260
4261   tmp = gfc_trans_scalar_assign (&lse, &rse, cm->ts, true, false, true);
4262   gfc_add_expr_to_block (&body, tmp);
4263
4264   gcc_assert (rse.ss == gfc_ss_terminator);
4265
4266   /* Generate the copying loops.  */
4267   gfc_trans_scalarizing_loops (&loop, &body);
4268
4269   /* Wrap the whole thing up.  */
4270   gfc_add_block_to_block (&block, &loop.pre);
4271   gfc_add_block_to_block (&block, &loop.post);
4272
4273   for (n = 0; n < cm->as->rank; n++)
4274     mpz_clear (lss->shape[n]);
4275   gfc_free (lss->shape);
4276
4277   gfc_cleanup_loop (&loop);
4278
4279   return gfc_finish_block (&block);
4280 }
4281
4282
4283 static tree
4284 gfc_trans_alloc_subarray_assign (tree dest, gfc_component * cm,
4285                                  gfc_expr * expr)
4286 {
4287   gfc_se se;
4288   gfc_ss *rss;
4289   stmtblock_t block;
4290   tree offset;
4291   int n;
4292   tree tmp;
4293   tree tmp2;
4294   gfc_array_spec *as;
4295   gfc_expr *arg = NULL;
4296
4297   gfc_start_block (&block);
4298   gfc_init_se (&se, NULL);
4299
4300   /* Get the descriptor for the expressions.  */ 
4301   rss = gfc_walk_expr (expr);
4302   se.want_pointer = 0;
4303   gfc_conv_expr_descriptor (&se, expr, rss);
4304   gfc_add_block_to_block (&block, &se.pre);
4305   gfc_add_modify (&block, dest, se.expr);
4306
4307   /* Deal with arrays of derived types with allocatable components.  */
4308   if (cm->ts.type == BT_DERIVED
4309         && cm->ts.u.derived->attr.alloc_comp)
4310     tmp = gfc_copy_alloc_comp (cm->ts.u.derived,
4311                                se.expr, dest,
4312                                cm->as->rank);
4313   else
4314     tmp = gfc_duplicate_allocatable (dest, se.expr,
4315                                      TREE_TYPE(cm->backend_decl),
4316                                      cm->as->rank);
4317
4318   gfc_add_expr_to_block (&block, tmp);
4319   gfc_add_block_to_block (&block, &se.post);
4320
4321   if (expr->expr_type != EXPR_VARIABLE)
4322     gfc_conv_descriptor_data_set (&block, se.expr,
4323                                   null_pointer_node);
4324
4325   /* We need to know if the argument of a conversion function is a
4326      variable, so that the correct lower bound can be used.  */
4327   if (expr->expr_type == EXPR_FUNCTION
4328         && expr->value.function.isym
4329         && expr->value.function.isym->conversion
4330         && expr->value.function.actual->expr
4331         && expr->value.function.actual->expr->expr_type == EXPR_VARIABLE)
4332     arg = expr->value.function.actual->expr;
4333
4334   /* Obtain the array spec of full array references.  */
4335   if (arg)
4336     as = gfc_get_full_arrayspec_from_expr (arg);
4337   else
4338     as = gfc_get_full_arrayspec_from_expr (expr);
4339
4340   /* Shift the lbound and ubound of temporaries to being unity,
4341      rather than zero, based. Always calculate the offset.  */
4342   offset = gfc_conv_descriptor_offset_get (dest);
4343   gfc_add_modify (&block, offset, gfc_index_zero_node);
4344   tmp2 =gfc_create_var (gfc_array_index_type, NULL);
4345
4346   for (n = 0; n < expr->rank; n++)
4347     {
4348       tree span;
4349       tree lbound;
4350
4351       /* Obtain the correct lbound - ISO/IEC TR 15581:2001 page 9.
4352          TODO It looks as if gfc_conv_expr_descriptor should return
4353          the correct bounds and that the following should not be
4354          necessary.  This would simplify gfc_conv_intrinsic_bound
4355          as well.  */
4356       if (as && as->lower[n])
4357         {
4358           gfc_se lbse;
4359           gfc_init_se (&lbse, NULL);
4360           gfc_conv_expr (&lbse, as->lower[n]);
4361           gfc_add_block_to_block (&block, &lbse.pre);
4362           lbound = gfc_evaluate_now (lbse.expr, &block);
4363         }
4364       else if (as && arg)
4365         {
4366           tmp = gfc_get_symbol_decl (arg->symtree->n.sym);
4367           lbound = gfc_conv_descriptor_lbound_get (tmp,
4368                                         gfc_rank_cst[n]);
4369         }
4370       else if (as)
4371         lbound = gfc_conv_descriptor_lbound_get (dest,
4372                                                 gfc_rank_cst[n]);
4373       else
4374         lbound = gfc_index_one_node;
4375
4376       lbound = fold_convert (gfc_array_index_type, lbound);
4377
4378       /* Shift the bounds and set the offset accordingly.  */
4379       tmp = gfc_conv_descriptor_ubound_get (dest, gfc_rank_cst[n]);
4380       span = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
4381                 tmp, gfc_conv_descriptor_lbound_get (dest, gfc_rank_cst[n]));
4382       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
4383                              span, lbound);
4384       gfc_conv_descriptor_ubound_set (&block, dest,
4385                                       gfc_rank_cst[n], tmp);
4386       gfc_conv_descriptor_lbound_set (&block, dest,
4387                                       gfc_rank_cst[n], lbound);
4388
4389       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
4390                          gfc_conv_descriptor_lbound_get (dest,
4391                                                          gfc_rank_cst[n]),
4392                          gfc_conv_descriptor_stride_get (dest,
4393                                                          gfc_rank_cst[n]));
4394       gfc_add_modify (&block, tmp2, tmp);
4395       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
4396                              offset, tmp2);
4397       gfc_conv_descriptor_offset_set (&block, dest, tmp);
4398     }
4399
4400   if (arg)
4401     {
4402       /* If a conversion expression has a null data pointer
4403          argument, nullify the allocatable component.  */
4404       tree non_null_expr;
4405       tree null_expr;
4406
4407       if (arg->symtree->n.sym->attr.allocatable
4408             || arg->symtree->n.sym->attr.pointer)
4409         {
4410           non_null_expr = gfc_finish_block (&block);
4411           gfc_start_block (&block);
4412           gfc_conv_descriptor_data_set (&block, dest,
4413                                         null_pointer_node);
4414           null_expr = gfc_finish_block (&block);
4415           tmp = gfc_conv_descriptor_data_get (arg->symtree->n.sym->backend_decl);
4416           tmp = build2_loc (input_location, EQ_EXPR, boolean_type_node, tmp,
4417                             fold_convert (TREE_TYPE (tmp), null_pointer_node));
4418           return build3_v (COND_EXPR, tmp,
4419                            null_expr, non_null_expr);
4420         }
4421     }
4422
4423   return gfc_finish_block (&block);
4424 }
4425
4426
4427 /* Assign a single component of a derived type constructor.  */
4428
4429 static tree
4430 gfc_trans_subcomponent_assign (tree dest, gfc_component * cm, gfc_expr * expr)
4431 {
4432   gfc_se se;
4433   gfc_se lse;
4434   gfc_ss *rss;
4435   stmtblock_t block;
4436   tree tmp;
4437
4438   gfc_start_block (&block);
4439
4440   if (cm->attr.pointer)
4441     {
4442       gfc_init_se (&se, NULL);
4443       /* Pointer component.  */
4444       if (cm->attr.dimension)
4445         {
4446           /* Array pointer.  */
4447           if (expr->expr_type == EXPR_NULL)
4448             gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
4449           else
4450             {
4451               rss = gfc_walk_expr (expr);
4452               se.direct_byref = 1;
4453               se.expr = dest;
4454               gfc_conv_expr_descriptor (&se, expr, rss);
4455               gfc_add_block_to_block (&block, &se.pre);
4456               gfc_add_block_to_block (&block, &se.post);
4457             }
4458         }
4459       else
4460         {
4461           /* Scalar pointers.  */
4462           se.want_pointer = 1;
4463           gfc_conv_expr (&se, expr);
4464           gfc_add_block_to_block (&block, &se.pre);
4465           gfc_add_modify (&block, dest,
4466                                fold_convert (TREE_TYPE (dest), se.expr));
4467           gfc_add_block_to_block (&block, &se.post);
4468         }
4469     }
4470   else if (cm->ts.type == BT_CLASS && expr->expr_type == EXPR_NULL)
4471     {
4472       /* NULL initialization for CLASS components.  */
4473       tmp = gfc_trans_structure_assign (dest,
4474                                         gfc_class_null_initializer (&cm->ts));
4475       gfc_add_expr_to_block (&block, tmp);
4476     }
4477   else if (cm->attr.dimension && !cm->attr.proc_pointer)
4478     {
4479       if (cm->attr.allocatable && expr->expr_type == EXPR_NULL)
4480         gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
4481       else if (cm->attr.allocatable)
4482         {
4483           tmp = gfc_trans_alloc_subarray_assign (dest, cm, expr);
4484           gfc_add_expr_to_block (&block, tmp);
4485         }
4486       else
4487         {
4488           tmp = gfc_trans_subarray_assign (dest, cm, expr);
4489           gfc_add_expr_to_block (&block, tmp);
4490         }
4491     }
4492   else if (expr->ts.type == BT_DERIVED)
4493     {
4494       if (expr->expr_type != EXPR_STRUCTURE)
4495         {
4496           gfc_init_se (&se, NULL);
4497           gfc_conv_expr (&se, expr);
4498           gfc_add_block_to_block (&block, &se.pre);
4499           gfc_add_modify (&block, dest,
4500                                fold_convert (TREE_TYPE (dest), se.expr));
4501           gfc_add_block_to_block (&block, &se.post);
4502         }
4503       else
4504         {
4505           /* Nested constructors.  */
4506           tmp = gfc_trans_structure_assign (dest, expr);
4507           gfc_add_expr_to_block (&block, tmp);
4508         }
4509     }
4510   else
4511     {
4512       /* Scalar component.  */
4513       gfc_init_se (&se, NULL);
4514       gfc_init_se (&lse, NULL);
4515
4516       gfc_conv_expr (&se, expr);
4517       if (cm->ts.type == BT_CHARACTER)
4518         lse.string_length = cm->ts.u.cl->backend_decl;
4519       lse.expr = dest;
4520       tmp = gfc_trans_scalar_assign (&lse, &se, cm->ts, true, false, true);
4521       gfc_add_expr_to_block (&block, tmp);
4522     }
4523   return gfc_finish_block (&block);
4524 }
4525
4526 /* Assign a derived type constructor to a variable.  */
4527
4528 static tree
4529 gfc_trans_structure_assign (tree dest, gfc_expr * expr)
4530 {
4531   gfc_constructor *c;
4532   gfc_component *cm;
4533   stmtblock_t block;
4534   tree field;
4535   tree tmp;
4536
4537   gfc_start_block (&block);
4538   cm = expr->ts.u.derived->components;
4539
4540   if (expr->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING
4541       && (expr->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
4542           || expr->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR))
4543     {
4544       gfc_se se, lse;
4545
4546       gcc_assert (cm->backend_decl == NULL);
4547       gfc_init_se (&se, NULL);
4548       gfc_init_se (&lse, NULL);
4549       gfc_conv_expr (&se, gfc_constructor_first (expr->value.constructor)->expr);
4550       lse.expr = dest;
4551       gfc_add_modify (&block, lse.expr,
4552                       fold_convert (TREE_TYPE (lse.expr), se.expr));
4553
4554       return gfc_finish_block (&block);
4555     } 
4556
4557   for (c = gfc_constructor_first (expr->value.constructor);
4558        c; c = gfc_constructor_next (c), cm = cm->next)
4559     {
4560       /* Skip absent members in default initializers.  */
4561       if (!c->expr)
4562         continue;
4563
4564       field = cm->backend_decl;
4565       tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
4566                              dest, field, NULL_TREE);
4567       tmp = gfc_trans_subcomponent_assign (tmp, cm, c->expr);
4568       gfc_add_expr_to_block (&block, tmp);
4569     }
4570   return gfc_finish_block (&block);
4571 }
4572
4573 /* Build an expression for a constructor. If init is nonzero then
4574    this is part of a static variable initializer.  */
4575
4576 void
4577 gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
4578 {
4579   gfc_constructor *c;
4580   gfc_component *cm;
4581   tree val;
4582   tree type;
4583   tree tmp;
4584   VEC(constructor_elt,gc) *v = NULL;
4585
4586   gcc_assert (se->ss == NULL);
4587   gcc_assert (expr->expr_type == EXPR_STRUCTURE);
4588   type = gfc_typenode_for_spec (&expr->ts);
4589
4590   if (!init)
4591     {
4592       /* Create a temporary variable and fill it in.  */
4593       se->expr = gfc_create_var (type, expr->ts.u.derived->name);
4594       tmp = gfc_trans_structure_assign (se->expr, expr);
4595       gfc_add_expr_to_block (&se->pre, tmp);
4596       return;
4597     }
4598
4599   cm = expr->ts.u.derived->components;
4600
4601   for (c = gfc_constructor_first (expr->value.constructor);
4602        c; c = gfc_constructor_next (c), cm = cm->next)
4603     {
4604       /* Skip absent members in default initializers and allocatable
4605          components.  Although the latter have a default initializer
4606          of EXPR_NULL,... by default, the static nullify is not needed
4607          since this is done every time we come into scope.  */
4608       if (!c->expr || cm->attr.allocatable)
4609         continue;
4610
4611       if (strcmp (cm->name, "_size") == 0)
4612         {
4613           val = TYPE_SIZE_UNIT (gfc_get_derived_type (cm->ts.u.derived));
4614           CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
4615         }
4616       else if (cm->initializer && cm->initializer->expr_type != EXPR_NULL
4617                && strcmp (cm->name, "_extends") == 0)
4618         {
4619           tree vtab;
4620           gfc_symbol *vtabs;
4621           vtabs = cm->initializer->symtree->n.sym;
4622           vtab = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtabs));
4623           CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, vtab);
4624         }
4625       else
4626         {
4627           val = gfc_conv_initializer (c->expr, &cm->ts,
4628                                       TREE_TYPE (cm->backend_decl),
4629                                       cm->attr.dimension, cm->attr.pointer,
4630                                       cm->attr.proc_pointer);
4631
4632           /* Append it to the constructor list.  */
4633           CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
4634         }
4635     }
4636   se->expr = build_constructor (type, v);
4637   if (init) 
4638     TREE_CONSTANT (se->expr) = 1;
4639 }
4640
4641
4642 /* Translate a substring expression.  */
4643
4644 static void
4645 gfc_conv_substring_expr (gfc_se * se, gfc_expr * expr)
4646 {
4647   gfc_ref *ref;
4648
4649   ref = expr->ref;
4650
4651   gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
4652
4653   se->expr = gfc_build_wide_string_const (expr->ts.kind,
4654                                           expr->value.character.length,
4655                                           expr->value.character.string);
4656
4657   se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
4658   TYPE_STRING_FLAG (TREE_TYPE (se->expr)) = 1;
4659
4660   if (ref)
4661     gfc_conv_substring (se, ref, expr->ts.kind, NULL, &expr->where);
4662 }
4663
4664
4665 /* Entry point for expression translation.  Evaluates a scalar quantity.
4666    EXPR is the expression to be translated, and SE is the state structure if
4667    called from within the scalarized.  */
4668
4669 void
4670 gfc_conv_expr (gfc_se * se, gfc_expr * expr)
4671 {
4672   if (se->ss && se->ss->expr == expr
4673       && (se->ss->type == GFC_SS_SCALAR || se->ss->type == GFC_SS_REFERENCE))
4674     {
4675       /* Substitute a scalar expression evaluated outside the scalarization
4676          loop.  */
4677       se->expr = se->ss->data.scalar.expr;
4678       if (se->ss->type == GFC_SS_REFERENCE)
4679         se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
4680       se->string_length = se->ss->string_length;
4681       gfc_advance_se_ss_chain (se);
4682       return;
4683     }
4684
4685   /* We need to convert the expressions for the iso_c_binding derived types.
4686      C_NULL_PTR and C_NULL_FUNPTR will be made EXPR_NULL, which evaluates to
4687      null_pointer_node.  C_PTR and C_FUNPTR are converted to match the
4688      typespec for the C_PTR and C_FUNPTR symbols, which has already been
4689      updated to be an integer with a kind equal to the size of a (void *).  */
4690   if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
4691       && expr->ts.u.derived->attr.is_iso_c)
4692     {
4693       if (expr->expr_type == EXPR_VARIABLE
4694           && (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
4695               || expr->symtree->n.sym->intmod_sym_id
4696                  == ISOCBINDING_NULL_FUNPTR))
4697         {
4698           /* Set expr_type to EXPR_NULL, which will result in
4699              null_pointer_node being used below.  */
4700           expr->expr_type = EXPR_NULL;
4701         }
4702       else
4703         {
4704           /* Update the type/kind of the expression to be what the new
4705              type/kind are for the updated symbols of C_PTR/C_FUNPTR.  */
4706           expr->ts.type = expr->ts.u.derived->ts.type;
4707           expr->ts.f90_type = expr->ts.u.derived->ts.f90_type;
4708           expr->ts.kind = expr->ts.u.derived->ts.kind;
4709         }
4710     }
4711   
4712   switch (expr->expr_type)
4713     {
4714     case EXPR_OP:
4715       gfc_conv_expr_op (se, expr);
4716       break;
4717
4718     case EXPR_FUNCTION:
4719       gfc_conv_function_expr (se, expr);
4720       break;
4721
4722     case EXPR_CONSTANT:
4723       gfc_conv_constant (se, expr);
4724       break;
4725
4726     case EXPR_VARIABLE:
4727       gfc_conv_variable (se, expr);
4728       break;
4729
4730     case EXPR_NULL:
4731       se->expr = null_pointer_node;
4732       break;
4733
4734     case EXPR_SUBSTRING:
4735       gfc_conv_substring_expr (se, expr);
4736       break;
4737
4738     case EXPR_STRUCTURE:
4739       gfc_conv_structure (se, expr, 0);
4740       break;
4741
4742     case EXPR_ARRAY:
4743       gfc_conv_array_constructor_expr (se, expr);
4744       break;
4745
4746     default:
4747       gcc_unreachable ();
4748       break;
4749     }
4750 }
4751
4752 /* Like gfc_conv_expr_val, but the value is also suitable for use in the lhs
4753    of an assignment.  */
4754 void
4755 gfc_conv_expr_lhs (gfc_se * se, gfc_expr * expr)
4756 {
4757   gfc_conv_expr (se, expr);
4758   /* All numeric lvalues should have empty post chains.  If not we need to
4759      figure out a way of rewriting an lvalue so that it has no post chain.  */
4760   gcc_assert (expr->ts.type == BT_CHARACTER || !se->post.head);
4761 }
4762
4763 /* Like gfc_conv_expr, but the POST block is guaranteed to be empty for
4764    numeric expressions.  Used for scalar values where inserting cleanup code
4765    is inconvenient.  */
4766 void
4767 gfc_conv_expr_val (gfc_se * se, gfc_expr * expr)
4768 {
4769   tree val;
4770
4771   gcc_assert (expr->ts.type != BT_CHARACTER);
4772   gfc_conv_expr (se, expr);
4773   if (se->post.head)
4774     {
4775       val = gfc_create_var (TREE_TYPE (se->expr), NULL);
4776       gfc_add_modify (&se->pre, val, se->expr);
4777       se->expr = val;
4778       gfc_add_block_to_block (&se->pre, &se->post);
4779     }
4780 }
4781
4782 /* Helper to translate an expression and convert it to a particular type.  */
4783 void
4784 gfc_conv_expr_type (gfc_se * se, gfc_expr * expr, tree type)
4785 {
4786   gfc_conv_expr_val (se, expr);
4787   se->expr = convert (type, se->expr);
4788 }
4789
4790
4791 /* Converts an expression so that it can be passed by reference.  Scalar
4792    values only.  */
4793
4794 void
4795 gfc_conv_expr_reference (gfc_se * se, gfc_expr * expr)
4796 {
4797   tree var;
4798
4799   if (se->ss && se->ss->expr == expr
4800       && se->ss->type == GFC_SS_REFERENCE)
4801     {
4802       /* Returns a reference to the scalar evaluated outside the loop
4803          for this case.  */
4804       gfc_conv_expr (se, expr);
4805       return;
4806     }
4807
4808   if (expr->ts.type == BT_CHARACTER)
4809     {
4810       gfc_conv_expr (se, expr);
4811       gfc_conv_string_parameter (se);
4812       return;
4813     }
4814
4815   if (expr->expr_type == EXPR_VARIABLE)
4816     {
4817       se->want_pointer = 1;
4818       gfc_conv_expr (se, expr);
4819       if (se->post.head)
4820         {
4821           var = gfc_create_var (TREE_TYPE (se->expr), NULL);
4822           gfc_add_modify (&se->pre, var, se->expr);
4823           gfc_add_block_to_block (&se->pre, &se->post);
4824           se->expr = var;
4825         }
4826       return;
4827     }
4828
4829   if (expr->expr_type == EXPR_FUNCTION
4830       && ((expr->value.function.esym
4831            && expr->value.function.esym->result->attr.pointer
4832            && !expr->value.function.esym->result->attr.dimension)
4833           || (!expr->value.function.esym
4834               && expr->symtree->n.sym->attr.pointer
4835               && !expr->symtree->n.sym->attr.dimension)))
4836     {
4837       se->want_pointer = 1;
4838       gfc_conv_expr (se, expr);
4839       var = gfc_create_var (TREE_TYPE (se->expr), NULL);
4840       gfc_add_modify (&se->pre, var, se->expr);
4841       se->expr = var;
4842       return;
4843     }
4844
4845
4846   gfc_conv_expr (se, expr);
4847
4848   /* Create a temporary var to hold the value.  */
4849   if (TREE_CONSTANT (se->expr))
4850     {
4851       tree tmp = se->expr;
4852       STRIP_TYPE_NOPS (tmp);
4853       var = build_decl (input_location,
4854                         CONST_DECL, NULL, TREE_TYPE (tmp));
4855       DECL_INITIAL (var) = tmp;
4856       TREE_STATIC (var) = 1;
4857       pushdecl (var);
4858     }
4859   else
4860     {
4861       var = gfc_create_var (TREE_TYPE (se->expr), NULL);
4862       gfc_add_modify (&se->pre, var, se->expr);
4863     }
4864   gfc_add_block_to_block (&se->pre, &se->post);
4865
4866   /* Take the address of that value.  */
4867   se->expr = gfc_build_addr_expr (NULL_TREE, var);
4868 }
4869
4870
4871 tree
4872 gfc_trans_pointer_assign (gfc_code * code)
4873 {
4874   return gfc_trans_pointer_assignment (code->expr1, code->expr2);
4875 }
4876
4877
4878 /* Generate code for a pointer assignment.  */
4879
4880 tree
4881 gfc_trans_pointer_assignment (gfc_expr * expr1, gfc_expr * expr2)
4882 {
4883   gfc_se lse;
4884   gfc_se rse;
4885   gfc_ss *lss;
4886   gfc_ss *rss;
4887   stmtblock_t block;
4888   tree desc;
4889   tree tmp;
4890   tree decl;
4891
4892   gfc_start_block (&block);
4893
4894   gfc_init_se (&lse, NULL);
4895
4896   lss = gfc_walk_expr (expr1);
4897   rss = gfc_walk_expr (expr2);
4898   if (lss == gfc_ss_terminator)
4899     {
4900       /* Scalar pointers.  */
4901       lse.want_pointer = 1;
4902       gfc_conv_expr (&lse, expr1);
4903       gcc_assert (rss == gfc_ss_terminator);
4904       gfc_init_se (&rse, NULL);
4905       rse.want_pointer = 1;
4906       gfc_conv_expr (&rse, expr2);
4907
4908       if (expr1->symtree->n.sym->attr.proc_pointer
4909           && expr1->symtree->n.sym->attr.dummy)
4910         lse.expr = build_fold_indirect_ref_loc (input_location,
4911                                             lse.expr);
4912
4913       if (expr2->symtree && expr2->symtree->n.sym->attr.proc_pointer
4914           && expr2->symtree->n.sym->attr.dummy)
4915         rse.expr = build_fold_indirect_ref_loc (input_location,
4916                                             rse.expr);
4917
4918       gfc_add_block_to_block (&block, &lse.pre);
4919       gfc_add_block_to_block (&block, &rse.pre);
4920
4921       /* Check character lengths if character expression.  The test is only
4922          really added if -fbounds-check is enabled.  */
4923       if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL
4924           && !expr1->symtree->n.sym->attr.proc_pointer
4925           && !gfc_is_proc_ptr_comp (expr1, NULL))
4926         {
4927           gcc_assert (expr2->ts.type == BT_CHARACTER);
4928           gcc_assert (lse.string_length && rse.string_length);
4929           gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
4930                                        lse.string_length, rse.string_length,
4931                                        &block);
4932         }
4933
4934       gfc_add_modify (&block, lse.expr,
4935                            fold_convert (TREE_TYPE (lse.expr), rse.expr));
4936
4937       gfc_add_block_to_block (&block, &rse.post);
4938       gfc_add_block_to_block (&block, &lse.post);
4939     }
4940   else
4941     {
4942       gfc_ref* remap;
4943       bool rank_remap;
4944       tree strlen_lhs;
4945       tree strlen_rhs = NULL_TREE;
4946
4947       /* Array pointer.  Find the last reference on the LHS and if it is an
4948          array section ref, we're dealing with bounds remapping.  In this case,
4949          set it to AR_FULL so that gfc_conv_expr_descriptor does
4950          not see it and process the bounds remapping afterwards explicitely.  */
4951       for (remap = expr1->ref; remap; remap = remap->next)
4952         if (!remap->next && remap->type == REF_ARRAY
4953             && remap->u.ar.type == AR_SECTION)
4954           {  
4955             remap->u.ar.type = AR_FULL;
4956             break;
4957           }
4958       rank_remap = (remap && remap->u.ar.end[0]);
4959
4960       gfc_conv_expr_descriptor (&lse, expr1, lss);
4961       strlen_lhs = lse.string_length;
4962       desc = lse.expr;
4963
4964       if (expr2->expr_type == EXPR_NULL)
4965         {
4966           /* Just set the data pointer to null.  */
4967           gfc_conv_descriptor_data_set (&lse.pre, lse.expr, null_pointer_node);
4968         }
4969       else if (rank_remap)
4970         {
4971           /* If we are rank-remapping, just get the RHS's descriptor and
4972              process this later on.  */
4973           gfc_init_se (&rse, NULL);
4974           rse.direct_byref = 1;
4975           rse.byref_noassign = 1;
4976           gfc_conv_expr_descriptor (&rse, expr2, rss);
4977           strlen_rhs = rse.string_length;
4978         }
4979       else if (expr2->expr_type == EXPR_VARIABLE)
4980         {
4981           /* Assign directly to the LHS's descriptor.  */
4982           lse.direct_byref = 1;
4983           gfc_conv_expr_descriptor (&lse, expr2, rss);
4984           strlen_rhs = lse.string_length;
4985
4986           /* If this is a subreference array pointer assignment, use the rhs
4987              descriptor element size for the lhs span.  */
4988           if (expr1->symtree->n.sym->attr.subref_array_pointer)
4989             {
4990               decl = expr1->symtree->n.sym->backend_decl;
4991               gfc_init_se (&rse, NULL);
4992               rse.descriptor_only = 1;
4993               gfc_conv_expr (&rse, expr2);
4994               tmp = gfc_get_element_type (TREE_TYPE (rse.expr));
4995               tmp = fold_convert (gfc_array_index_type, size_in_bytes (tmp));
4996               if (!INTEGER_CST_P (tmp))
4997                 gfc_add_block_to_block (&lse.post, &rse.pre);
4998               gfc_add_modify (&lse.post, GFC_DECL_SPAN(decl), tmp);
4999             }
5000         }
5001       else
5002         {
5003           /* Assign to a temporary descriptor and then copy that
5004              temporary to the pointer.  */
5005           tmp = gfc_create_var (TREE_TYPE (desc), "ptrtemp");
5006
5007           lse.expr = tmp;
5008           lse.direct_byref = 1;
5009           gfc_conv_expr_descriptor (&lse, expr2, rss);
5010           strlen_rhs = lse.string_length;
5011           gfc_add_modify (&lse.pre, desc, tmp);
5012         }
5013
5014       gfc_add_block_to_block (&block, &lse.pre);
5015       if (rank_remap)
5016         gfc_add_block_to_block (&block, &rse.pre);
5017
5018       /* If we do bounds remapping, update LHS descriptor accordingly.  */
5019       if (remap)
5020         {
5021           int dim;
5022           gcc_assert (remap->u.ar.dimen == expr1->rank);
5023
5024           if (rank_remap)
5025             {
5026               /* Do rank remapping.  We already have the RHS's descriptor
5027                  converted in rse and now have to build the correct LHS
5028                  descriptor for it.  */
5029
5030               tree dtype, data;
5031               tree offs, stride;
5032               tree lbound, ubound;
5033
5034               /* Set dtype.  */
5035               dtype = gfc_conv_descriptor_dtype (desc);
5036               tmp = gfc_get_dtype (TREE_TYPE (desc));
5037               gfc_add_modify (&block, dtype, tmp);
5038
5039               /* Copy data pointer.  */
5040               data = gfc_conv_descriptor_data_get (rse.expr);
5041               gfc_conv_descriptor_data_set (&block, desc, data);
5042
5043               /* Copy offset but adjust it such that it would correspond
5044                  to a lbound of zero.  */
5045               offs = gfc_conv_descriptor_offset_get (rse.expr);
5046               for (dim = 0; dim < expr2->rank; ++dim)
5047                 {
5048                   stride = gfc_conv_descriptor_stride_get (rse.expr,
5049                                                            gfc_rank_cst[dim]);
5050                   lbound = gfc_conv_descriptor_lbound_get (rse.expr,
5051                                                            gfc_rank_cst[dim]);
5052                   tmp = fold_build2_loc (input_location, MULT_EXPR,
5053                                          gfc_array_index_type, stride, lbound);
5054                   offs = fold_build2_loc (input_location, PLUS_EXPR,
5055                                           gfc_array_index_type, offs, tmp);
5056                 }
5057               gfc_conv_descriptor_offset_set (&block, desc, offs);
5058
5059               /* Set the bounds as declared for the LHS and calculate strides as
5060                  well as another offset update accordingly.  */
5061               stride = gfc_conv_descriptor_stride_get (rse.expr,
5062                                                        gfc_rank_cst[0]);
5063               for (dim = 0; dim < expr1->rank; ++dim)
5064                 {
5065                   gfc_se lower_se;
5066                   gfc_se upper_se;
5067
5068                   gcc_assert (remap->u.ar.start[dim] && remap->u.ar.end[dim]);
5069
5070                   /* Convert declared bounds.  */
5071                   gfc_init_se (&lower_se, NULL);
5072                   gfc_init_se (&upper_se, NULL);
5073                   gfc_conv_expr (&lower_se, remap->u.ar.start[dim]);
5074                   gfc_conv_expr (&upper_se, remap->u.ar.end[dim]);
5075
5076                   gfc_add_block_to_block (&block, &lower_se.pre);
5077                   gfc_add_block_to_block (&block, &upper_se.pre);
5078
5079                   lbound = fold_convert (gfc_array_index_type, lower_se.expr);
5080                   ubound = fold_convert (gfc_array_index_type, upper_se.expr);
5081
5082                   lbound = gfc_evaluate_now (lbound, &block);
5083                   ubound = gfc_evaluate_now (ubound, &block);
5084
5085                   gfc_add_block_to_block (&block, &lower_se.post);
5086                   gfc_add_block_to_block (&block, &upper_se.post);
5087
5088                   /* Set bounds in descriptor.  */
5089                   gfc_conv_descriptor_lbound_set (&block, desc,
5090                                                   gfc_rank_cst[dim], lbound);
5091                   gfc_conv_descriptor_ubound_set (&block, desc,
5092                                                   gfc_rank_cst[dim], ubound);
5093
5094                   /* Set stride.  */
5095                   stride = gfc_evaluate_now (stride, &block);
5096                   gfc_conv_descriptor_stride_set (&block, desc,
5097                                                   gfc_rank_cst[dim], stride);
5098
5099                   /* Update offset.  */
5100                   offs = gfc_conv_descriptor_offset_get (desc);
5101                   tmp = fold_build2_loc (input_location, MULT_EXPR,
5102                                          gfc_array_index_type, lbound, stride);
5103                   offs = fold_build2_loc (input_location, MINUS_EXPR,
5104                                           gfc_array_index_type, offs, tmp);
5105                   offs = gfc_evaluate_now (offs, &block);
5106                   gfc_conv_descriptor_offset_set (&block, desc, offs);
5107
5108                   /* Update stride.  */
5109                   tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
5110                   stride = fold_build2_loc (input_location, MULT_EXPR,
5111                                             gfc_array_index_type, stride, tmp);
5112                 }
5113             }
5114           else
5115             {
5116               /* Bounds remapping.  Just shift the lower bounds.  */
5117
5118               gcc_assert (expr1->rank == expr2->rank);
5119
5120               for (dim = 0; dim < remap->u.ar.dimen; ++dim)
5121                 {
5122                   gfc_se lbound_se;
5123
5124                   gcc_assert (remap->u.ar.start[dim]);
5125                   gcc_assert (!remap->u.ar.end[dim]);
5126                   gfc_init_se (&lbound_se, NULL);
5127                   gfc_conv_expr (&lbound_se, remap->u.ar.start[dim]);
5128
5129                   gfc_add_block_to_block (&block, &lbound_se.pre);
5130                   gfc_conv_shift_descriptor_lbound (&block, desc,
5131                                                     dim, lbound_se.expr);
5132                   gfc_add_block_to_block (&block, &lbound_se.post);
5133                 }
5134             }
5135         }
5136
5137       /* Check string lengths if applicable.  The check is only really added
5138          to the output code if -fbounds-check is enabled.  */
5139       if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL)
5140         {
5141           gcc_assert (expr2->ts.type == BT_CHARACTER);
5142           gcc_assert (strlen_lhs && strlen_rhs);
5143           gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
5144                                        strlen_lhs, strlen_rhs, &block);
5145         }
5146
5147       /* If rank remapping was done, check with -fcheck=bounds that
5148          the target is at least as large as the pointer.  */
5149       if (rank_remap && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS))
5150         {
5151           tree lsize, rsize;
5152           tree fault;
5153           const char* msg;
5154
5155           lsize = gfc_conv_descriptor_size (lse.expr, expr1->rank);
5156           rsize = gfc_conv_descriptor_size (rse.expr, expr2->rank);
5157
5158           lsize = gfc_evaluate_now (lsize, &block);
5159           rsize = gfc_evaluate_now (rsize, &block);
5160           fault = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
5161                                    rsize, lsize);
5162
5163           msg = _("Target of rank remapping is too small (%ld < %ld)");
5164           gfc_trans_runtime_check (true, false, fault, &block, &expr2->where,
5165                                    msg, rsize, lsize);
5166         }
5167
5168       gfc_add_block_to_block (&block, &lse.post);
5169       if (rank_remap)
5170         gfc_add_block_to_block (&block, &rse.post);
5171     }
5172
5173   return gfc_finish_block (&block);
5174 }
5175
5176
5177 /* Makes sure se is suitable for passing as a function string parameter.  */
5178 /* TODO: Need to check all callers of this function.  It may be abused.  */
5179
5180 void
5181 gfc_conv_string_parameter (gfc_se * se)
5182 {
5183   tree type;
5184
5185   if (TREE_CODE (se->expr) == STRING_CST)
5186     {
5187       type = TREE_TYPE (TREE_TYPE (se->expr));
5188       se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
5189       return;
5190     }
5191
5192   if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
5193     {
5194       if (TREE_CODE (se->expr) != INDIRECT_REF)
5195         {
5196           type = TREE_TYPE (se->expr);
5197           se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
5198         }
5199       else
5200         {
5201           type = gfc_get_character_type_len (gfc_default_character_kind,
5202                                              se->string_length);
5203           type = build_pointer_type (type);
5204           se->expr = gfc_build_addr_expr (type, se->expr);
5205         }
5206     }
5207
5208   gcc_assert (POINTER_TYPE_P (TREE_TYPE (se->expr)));
5209   gcc_assert (se->string_length
5210           && TREE_CODE (TREE_TYPE (se->string_length)) == INTEGER_TYPE);
5211 }
5212
5213
5214 /* Generate code for assignment of scalar variables.  Includes character
5215    strings and derived types with allocatable components.
5216    If you know that the LHS has no allocations, set dealloc to false.  */
5217
5218 tree
5219 gfc_trans_scalar_assign (gfc_se * lse, gfc_se * rse, gfc_typespec ts,
5220                          bool l_is_temp, bool r_is_var, bool dealloc)
5221 {
5222   stmtblock_t block;
5223   tree tmp;
5224   tree cond;
5225
5226   gfc_init_block (&block);
5227
5228   if (ts.type == BT_CHARACTER)
5229     {
5230       tree rlen = NULL;
5231       tree llen = NULL;
5232
5233       if (lse->string_length != NULL_TREE)
5234         {
5235           gfc_conv_string_parameter (lse);
5236           gfc_add_block_to_block (&block, &lse->pre);
5237           llen = lse->string_length;
5238         }
5239
5240       if (rse->string_length != NULL_TREE)
5241         {
5242           gcc_assert (rse->string_length != NULL_TREE);
5243           gfc_conv_string_parameter (rse);
5244           gfc_add_block_to_block (&block, &rse->pre);
5245           rlen = rse->string_length;
5246         }
5247
5248       gfc_trans_string_copy (&block, llen, lse->expr, ts.kind, rlen,
5249                              rse->expr, ts.kind);
5250     }
5251   else if (ts.type == BT_DERIVED && ts.u.derived->attr.alloc_comp)
5252     {
5253       cond = NULL_TREE;
5254         
5255       /* Are the rhs and the lhs the same?  */
5256       if (r_is_var)
5257         {
5258           cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
5259                                   gfc_build_addr_expr (NULL_TREE, lse->expr),
5260                                   gfc_build_addr_expr (NULL_TREE, rse->expr));
5261           cond = gfc_evaluate_now (cond, &lse->pre);
5262         }
5263
5264       /* Deallocate the lhs allocated components as long as it is not
5265          the same as the rhs.  This must be done following the assignment
5266          to prevent deallocating data that could be used in the rhs
5267          expression.  */
5268       if (!l_is_temp && dealloc)
5269         {
5270           tmp = gfc_evaluate_now (lse->expr, &lse->pre);
5271           tmp = gfc_deallocate_alloc_comp (ts.u.derived, tmp, 0);
5272           if (r_is_var)
5273             tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
5274                             tmp);
5275           gfc_add_expr_to_block (&lse->post, tmp);
5276         }
5277
5278       gfc_add_block_to_block (&block, &rse->pre);
5279       gfc_add_block_to_block (&block, &lse->pre);
5280
5281       gfc_add_modify (&block, lse->expr,
5282                            fold_convert (TREE_TYPE (lse->expr), rse->expr));
5283
5284       /* Do a deep copy if the rhs is a variable, if it is not the
5285          same as the lhs.  */
5286       if (r_is_var)
5287         {
5288           tmp = gfc_copy_alloc_comp (ts.u.derived, rse->expr, lse->expr, 0);
5289           tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
5290                           tmp);
5291           gfc_add_expr_to_block (&block, tmp);
5292         }
5293     }
5294   else if (ts.type == BT_DERIVED || ts.type == BT_CLASS)
5295     {
5296       gfc_add_block_to_block (&block, &lse->pre);
5297       gfc_add_block_to_block (&block, &rse->pre);
5298       tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
5299                              TREE_TYPE (lse->expr), rse->expr);
5300       gfc_add_modify (&block, lse->expr, tmp);
5301     }
5302   else
5303     {
5304       gfc_add_block_to_block (&block, &lse->pre);
5305       gfc_add_block_to_block (&block, &rse->pre);
5306
5307       gfc_add_modify (&block, lse->expr,
5308                       fold_convert (TREE_TYPE (lse->expr), rse->expr));
5309     }
5310
5311   gfc_add_block_to_block (&block, &lse->post);
5312   gfc_add_block_to_block (&block, &rse->post);
5313
5314   return gfc_finish_block (&block);
5315 }
5316
5317
5318 /* There are quite a lot of restrictions on the optimisation in using an
5319    array function assign without a temporary.  */
5320
5321 static bool
5322 arrayfunc_assign_needs_temporary (gfc_expr * expr1, gfc_expr * expr2)
5323 {
5324   gfc_ref * ref;
5325   bool seen_array_ref;
5326   bool c = false;
5327   gfc_symbol *sym = expr1->symtree->n.sym;
5328
5329   /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION.  */
5330   if (expr2->value.function.isym && !gfc_is_intrinsic_libcall (expr2))
5331     return true;
5332
5333   /* Elemental functions are scalarized so that they don't need a
5334      temporary in gfc_trans_assignment_1, so return a true.  Otherwise,
5335      they would need special treatment in gfc_trans_arrayfunc_assign.  */
5336   if (expr2->value.function.esym != NULL
5337       && expr2->value.function.esym->attr.elemental)
5338     return true;
5339
5340   /* Need a temporary if rhs is not FULL or a contiguous section.  */
5341   if (expr1->ref && !(gfc_full_array_ref_p (expr1->ref, &c) || c))
5342     return true;
5343
5344   /* Need a temporary if EXPR1 can't be expressed as a descriptor.  */
5345   if (gfc_ref_needs_temporary_p (expr1->ref))
5346     return true;
5347
5348   /* Functions returning pointers need temporaries.  */
5349   if (expr2->symtree->n.sym->attr.pointer 
5350       || expr2->symtree->n.sym->attr.allocatable)
5351     return true;
5352
5353   /* Character array functions need temporaries unless the
5354      character lengths are the same.  */
5355   if (expr2->ts.type == BT_CHARACTER && expr2->rank > 0)
5356     {
5357       if (expr1->ts.u.cl->length == NULL
5358             || expr1->ts.u.cl->length->expr_type != EXPR_CONSTANT)
5359         return true;
5360
5361       if (expr2->ts.u.cl->length == NULL
5362             || expr2->ts.u.cl->length->expr_type != EXPR_CONSTANT)
5363         return true;
5364
5365       if (mpz_cmp (expr1->ts.u.cl->length->value.integer,
5366                      expr2->ts.u.cl->length->value.integer) != 0)
5367         return true;
5368     }
5369
5370   /* Check that no LHS component references appear during an array
5371      reference. This is needed because we do not have the means to
5372      span any arbitrary stride with an array descriptor. This check
5373      is not needed for the rhs because the function result has to be
5374      a complete type.  */
5375   seen_array_ref = false;
5376   for (ref = expr1->ref; ref; ref = ref->next)
5377     {
5378       if (ref->type == REF_ARRAY)
5379         seen_array_ref= true;
5380       else if (ref->type == REF_COMPONENT && seen_array_ref)
5381         return true;
5382     }
5383
5384   /* Check for a dependency.  */
5385   if (gfc_check_fncall_dependency (expr1, INTENT_OUT,
5386                                    expr2->value.function.esym,
5387                                    expr2->value.function.actual,
5388                                    NOT_ELEMENTAL))
5389     return true;
5390
5391   /* If we have reached here with an intrinsic function, we do not
5392      need a temporary.  */
5393   if (expr2->value.function.isym)
5394     return false;
5395
5396   /* If the LHS is a dummy, we need a temporary if it is not
5397      INTENT(OUT).  */
5398   if (sym->attr.dummy && sym->attr.intent != INTENT_OUT)
5399     return true;
5400
5401   /* If the lhs has been host_associated, is in common, a pointer or is
5402      a target and the function is not using a RESULT variable, aliasing
5403      can occur and a temporary is needed.  */
5404   if ((sym->attr.host_assoc
5405            || sym->attr.in_common
5406            || sym->attr.pointer
5407            || sym->attr.cray_pointee
5408            || sym->attr.target)
5409         && expr2->symtree != NULL
5410         && expr2->symtree->n.sym == expr2->symtree->n.sym->result)
5411     return true;
5412
5413   /* A PURE function can unconditionally be called without a temporary.  */
5414   if (expr2->value.function.esym != NULL
5415       && expr2->value.function.esym->attr.pure)
5416     return false;
5417
5418   /* Implicit_pure functions are those which could legally be declared
5419      to be PURE.  */
5420   if (expr2->value.function.esym != NULL
5421       && expr2->value.function.esym->attr.implicit_pure)
5422     return false;
5423
5424   if (!sym->attr.use_assoc
5425         && !sym->attr.in_common
5426         && !sym->attr.pointer
5427         && !sym->attr.target
5428         && !sym->attr.cray_pointee
5429         && expr2->value.function.esym)
5430     {
5431       /* A temporary is not needed if the function is not contained and
5432          the variable is local or host associated and not a pointer or
5433          a target. */
5434       if (!expr2->value.function.esym->attr.contained)
5435         return false;
5436
5437       /* A temporary is not needed if the lhs has never been host
5438          associated and the procedure is contained.  */
5439       else if (!sym->attr.host_assoc)
5440         return false;
5441
5442       /* A temporary is not needed if the variable is local and not
5443          a pointer, a target or a result.  */
5444       if (sym->ns->parent
5445             && expr2->value.function.esym->ns == sym->ns->parent)
5446         return false;
5447     }
5448
5449   /* Default to temporary use.  */
5450   return true;
5451 }
5452
5453
5454 /* Provide the loop info so that the lhs descriptor can be built for
5455    reallocatable assignments from extrinsic function calls.  */
5456
5457 static void
5458 realloc_lhs_loop_for_fcn_call (gfc_se *se, locus *where, gfc_ss **ss)
5459 {
5460   gfc_loopinfo loop;
5461   /* Signal that the function call should not be made by
5462      gfc_conv_loop_setup. */
5463   se->ss->is_alloc_lhs = 1;
5464   gfc_init_loopinfo (&loop);
5465   gfc_add_ss_to_loop (&loop, *ss);
5466   gfc_add_ss_to_loop (&loop, se->ss);
5467   gfc_conv_ss_startstride (&loop);
5468   gfc_conv_loop_setup (&loop, where);
5469   gfc_copy_loopinfo_to_se (se, &loop);
5470   gfc_add_block_to_block (&se->pre, &loop.pre);
5471   gfc_add_block_to_block (&se->pre, &loop.post);
5472   se->ss->is_alloc_lhs = 0;
5473 }
5474
5475
5476 static void
5477 realloc_lhs_bounds_for_intrinsic_call (gfc_se *se, int rank)
5478 {
5479   tree desc;
5480   tree tmp;
5481   tree offset;
5482   int n;
5483
5484   /* Use the allocation done by the library.  */
5485   desc = build_fold_indirect_ref_loc (input_location, se->expr);
5486   tmp = gfc_conv_descriptor_data_get (desc);
5487   tmp = gfc_call_free (fold_convert (pvoid_type_node, tmp));
5488   gfc_add_expr_to_block (&se->pre, tmp);
5489   gfc_conv_descriptor_data_set (&se->pre, desc, null_pointer_node);
5490   /* Unallocated, the descriptor does not have a dtype.  */
5491   tmp = gfc_conv_descriptor_dtype (desc);
5492   gfc_add_modify (&se->pre, tmp, gfc_get_dtype (TREE_TYPE (desc)));
5493
5494   offset = gfc_index_zero_node;
5495   tmp = gfc_index_one_node;
5496   /* Now reset the bounds from zero based to unity based.  */
5497   for (n = 0 ; n < rank; n++)
5498     {
5499       /* Accumulate the offset.  */
5500       offset = fold_build2_loc (input_location, MINUS_EXPR,
5501                                 gfc_array_index_type,
5502                                 offset, tmp);
5503       /* Now do the bounds.  */
5504       gfc_conv_descriptor_offset_set (&se->post, desc, tmp);
5505       tmp = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
5506       tmp = fold_build2_loc (input_location, PLUS_EXPR,
5507                              gfc_array_index_type,
5508                              tmp, gfc_index_one_node);
5509       gfc_conv_descriptor_lbound_set (&se->post, desc,
5510                                       gfc_rank_cst[n],
5511                                       gfc_index_one_node);
5512       gfc_conv_descriptor_ubound_set (&se->post, desc,
5513                                       gfc_rank_cst[n], tmp);
5514
5515       /* The extent for the next contribution to offset.  */
5516       tmp = fold_build2_loc (input_location, MINUS_EXPR,
5517                              gfc_array_index_type,
5518                              gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]),
5519                              gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]));
5520       tmp = fold_build2_loc (input_location, PLUS_EXPR,
5521                              gfc_array_index_type,
5522                              tmp, gfc_index_one_node);
5523     }
5524   gfc_conv_descriptor_offset_set (&se->post, desc, offset);
5525 }
5526
5527
5528
5529 /* Try to translate array(:) = func (...), where func is a transformational
5530    array function, without using a temporary.  Returns NULL if this isn't the
5531    case.  */
5532
5533 static tree
5534 gfc_trans_arrayfunc_assign (gfc_expr * expr1, gfc_expr * expr2)
5535 {
5536   gfc_se se;
5537   gfc_ss *ss;
5538   gfc_component *comp = NULL;
5539
5540   if (arrayfunc_assign_needs_temporary (expr1, expr2))
5541     return NULL;
5542
5543   /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
5544      functions.  */
5545   gcc_assert (expr2->value.function.isym
5546               || (gfc_is_proc_ptr_comp (expr2, &comp)
5547                   && comp && comp->attr.dimension)
5548               || (!comp && gfc_return_by_reference (expr2->value.function.esym)
5549                   && expr2->value.function.esym->result->attr.dimension));
5550
5551   ss = gfc_walk_expr (expr1);
5552   gcc_assert (ss != gfc_ss_terminator);
5553   gfc_init_se (&se, NULL);
5554   gfc_start_block (&se.pre);
5555   se.want_pointer = 1;
5556
5557   gfc_conv_array_parameter (&se, expr1, ss, false, NULL, NULL, NULL);
5558
5559   if (expr1->ts.type == BT_DERIVED
5560         && expr1->ts.u.derived->attr.alloc_comp)
5561     {
5562       tree tmp;
5563       tmp = gfc_deallocate_alloc_comp (expr1->ts.u.derived, se.expr,
5564                                        expr1->rank);
5565       gfc_add_expr_to_block (&se.pre, tmp);
5566     }
5567
5568   se.direct_byref = 1;
5569   se.ss = gfc_walk_expr (expr2);
5570   gcc_assert (se.ss != gfc_ss_terminator);
5571
5572   /* Reallocate on assignment needs the loopinfo for extrinsic functions.
5573      This is signalled to gfc_conv_procedure_call by setting is_alloc_lhs.
5574      Clearly, this cannot be done for an allocatable function result, since
5575      the shape of the result is unknown and, in any case, the function must
5576      correctly take care of the reallocation internally. For intrinsic
5577      calls, the array data is freed and the library takes care of allocation.
5578      TODO: Add logic of trans-array.c: gfc_alloc_allocatable_for_assignment
5579      to the library.  */    
5580   if (gfc_option.flag_realloc_lhs
5581         && gfc_is_reallocatable_lhs (expr1)
5582         && !gfc_expr_attr (expr1).codimension
5583         && !gfc_is_coindexed (expr1)
5584         && !(expr2->value.function.esym
5585             && expr2->value.function.esym->result->attr.allocatable))
5586     {
5587       if (!expr2->value.function.isym)
5588         {
5589           realloc_lhs_loop_for_fcn_call (&se, &expr1->where, &ss);
5590           ss->is_alloc_lhs = 1;
5591         }
5592       else
5593         realloc_lhs_bounds_for_intrinsic_call (&se, expr1->rank);
5594     }
5595
5596   gfc_conv_function_expr (&se, expr2);
5597   gfc_add_block_to_block (&se.pre, &se.post);
5598
5599   return gfc_finish_block (&se.pre);
5600 }
5601
5602
5603 /* Try to efficiently translate array(:) = 0.  Return NULL if this
5604    can't be done.  */
5605
5606 static tree
5607 gfc_trans_zero_assign (gfc_expr * expr)
5608 {
5609   tree dest, len, type;
5610   tree tmp;
5611   gfc_symbol *sym;
5612
5613   sym = expr->symtree->n.sym;
5614   dest = gfc_get_symbol_decl (sym);
5615
5616   type = TREE_TYPE (dest);
5617   if (POINTER_TYPE_P (type))
5618     type = TREE_TYPE (type);
5619   if (!GFC_ARRAY_TYPE_P (type))
5620     return NULL_TREE;
5621
5622   /* Determine the length of the array.  */
5623   len = GFC_TYPE_ARRAY_SIZE (type);
5624   if (!len || TREE_CODE (len) != INTEGER_CST)
5625     return NULL_TREE;
5626
5627   tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
5628   len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
5629                          fold_convert (gfc_array_index_type, tmp));
5630
5631   /* If we are zeroing a local array avoid taking its address by emitting
5632      a = {} instead.  */
5633   if (!POINTER_TYPE_P (TREE_TYPE (dest)))
5634     return build2_loc (input_location, MODIFY_EXPR, void_type_node,
5635                        dest, build_constructor (TREE_TYPE (dest), NULL));
5636
5637   /* Convert arguments to the correct types.  */
5638   dest = fold_convert (pvoid_type_node, dest);
5639   len = fold_convert (size_type_node, len);
5640
5641   /* Construct call to __builtin_memset.  */
5642   tmp = build_call_expr_loc (input_location,
5643                          built_in_decls[BUILT_IN_MEMSET],
5644                          3, dest, integer_zero_node, len);
5645   return fold_convert (void_type_node, tmp);
5646 }
5647
5648
5649 /* Helper for gfc_trans_array_copy and gfc_trans_array_constructor_copy
5650    that constructs the call to __builtin_memcpy.  */
5651
5652 tree
5653 gfc_build_memcpy_call (tree dst, tree src, tree len)
5654 {
5655   tree tmp;
5656
5657   /* Convert arguments to the correct types.  */
5658   if (!POINTER_TYPE_P (TREE_TYPE (dst)))
5659     dst = gfc_build_addr_expr (pvoid_type_node, dst);
5660   else
5661     dst = fold_convert (pvoid_type_node, dst);
5662
5663   if (!POINTER_TYPE_P (TREE_TYPE (src)))
5664     src = gfc_build_addr_expr (pvoid_type_node, src);
5665   else
5666     src = fold_convert (pvoid_type_node, src);
5667
5668   len = fold_convert (size_type_node, len);
5669
5670   /* Construct call to __builtin_memcpy.  */
5671   tmp = build_call_expr_loc (input_location,
5672                          built_in_decls[BUILT_IN_MEMCPY], 3, dst, src, len);
5673   return fold_convert (void_type_node, tmp);
5674 }
5675
5676
5677 /* Try to efficiently translate dst(:) = src(:).  Return NULL if this
5678    can't be done.  EXPR1 is the destination/lhs and EXPR2 is the
5679    source/rhs, both are gfc_full_array_ref_p which have been checked for
5680    dependencies.  */
5681
5682 static tree
5683 gfc_trans_array_copy (gfc_expr * expr1, gfc_expr * expr2)
5684 {
5685   tree dst, dlen, dtype;
5686   tree src, slen, stype;
5687   tree tmp;
5688
5689   dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
5690   src = gfc_get_symbol_decl (expr2->symtree->n.sym);
5691
5692   dtype = TREE_TYPE (dst);
5693   if (POINTER_TYPE_P (dtype))
5694     dtype = TREE_TYPE (dtype);
5695   stype = TREE_TYPE (src);
5696   if (POINTER_TYPE_P (stype))
5697     stype = TREE_TYPE (stype);
5698
5699   if (!GFC_ARRAY_TYPE_P (dtype) || !GFC_ARRAY_TYPE_P (stype))
5700     return NULL_TREE;
5701
5702   /* Determine the lengths of the arrays.  */
5703   dlen = GFC_TYPE_ARRAY_SIZE (dtype);
5704   if (!dlen || TREE_CODE (dlen) != INTEGER_CST)
5705     return NULL_TREE;
5706   tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
5707   dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
5708                           dlen, fold_convert (gfc_array_index_type, tmp));
5709
5710   slen = GFC_TYPE_ARRAY_SIZE (stype);
5711   if (!slen || TREE_CODE (slen) != INTEGER_CST)
5712     return NULL_TREE;
5713   tmp = TYPE_SIZE_UNIT (gfc_get_element_type (stype));
5714   slen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
5715                           slen, fold_convert (gfc_array_index_type, tmp));
5716
5717   /* Sanity check that they are the same.  This should always be
5718      the case, as we should already have checked for conformance.  */
5719   if (!tree_int_cst_equal (slen, dlen))
5720     return NULL_TREE;
5721
5722   return gfc_build_memcpy_call (dst, src, dlen);
5723 }
5724
5725
5726 /* Try to efficiently translate array(:) = (/ ... /).  Return NULL if
5727    this can't be done.  EXPR1 is the destination/lhs for which
5728    gfc_full_array_ref_p is true, and EXPR2 is the source/rhs.  */
5729
5730 static tree
5731 gfc_trans_array_constructor_copy (gfc_expr * expr1, gfc_expr * expr2)
5732 {
5733   unsigned HOST_WIDE_INT nelem;
5734   tree dst, dtype;
5735   tree src, stype;
5736   tree len;
5737   tree tmp;
5738
5739   nelem = gfc_constant_array_constructor_p (expr2->value.constructor);
5740   if (nelem == 0)
5741     return NULL_TREE;
5742
5743   dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
5744   dtype = TREE_TYPE (dst);
5745   if (POINTER_TYPE_P (dtype))
5746     dtype = TREE_TYPE (dtype);
5747   if (!GFC_ARRAY_TYPE_P (dtype))
5748     return NULL_TREE;
5749
5750   /* Determine the lengths of the array.  */
5751   len = GFC_TYPE_ARRAY_SIZE (dtype);
5752   if (!len || TREE_CODE (len) != INTEGER_CST)
5753     return NULL_TREE;
5754
5755   /* Confirm that the constructor is the same size.  */
5756   if (compare_tree_int (len, nelem) != 0)
5757     return NULL_TREE;
5758
5759   tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
5760   len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
5761                          fold_convert (gfc_array_index_type, tmp));
5762
5763   stype = gfc_typenode_for_spec (&expr2->ts);
5764   src = gfc_build_constant_array_constructor (expr2, stype);
5765
5766   stype = TREE_TYPE (src);
5767   if (POINTER_TYPE_P (stype))
5768     stype = TREE_TYPE (stype);
5769
5770   return gfc_build_memcpy_call (dst, src, len);
5771 }
5772
5773
5774 /* Tells whether the expression is to be treated as a variable reference.  */
5775
5776 static bool
5777 expr_is_variable (gfc_expr *expr)
5778 {
5779   gfc_expr *arg;
5780
5781   if (expr->expr_type == EXPR_VARIABLE)
5782     return true;
5783
5784   arg = gfc_get_noncopying_intrinsic_argument (expr);
5785   if (arg)
5786     {
5787       gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
5788       return expr_is_variable (arg);
5789     }
5790
5791   return false;
5792 }
5793
5794
5795 /* Subroutine of gfc_trans_assignment that actually scalarizes the
5796    assignment.  EXPR1 is the destination/LHS and EXPR2 is the source/RHS.
5797    init_flag indicates initialization expressions and dealloc that no
5798    deallocate prior assignment is needed (if in doubt, set true).  */
5799
5800 static tree
5801 gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
5802                         bool dealloc)
5803 {
5804   gfc_se lse;
5805   gfc_se rse;
5806   gfc_ss *lss;
5807   gfc_ss *lss_section;
5808   gfc_ss *rss;
5809   gfc_loopinfo loop;
5810   tree tmp;
5811   stmtblock_t block;
5812   stmtblock_t body;
5813   bool l_is_temp;
5814   bool scalar_to_array;
5815   tree string_length;
5816   int n;
5817
5818   /* Assignment of the form lhs = rhs.  */
5819   gfc_start_block (&block);
5820
5821   gfc_init_se (&lse, NULL);
5822   gfc_init_se (&rse, NULL);
5823
5824   /* Walk the lhs.  */
5825   lss = gfc_walk_expr (expr1);
5826   if (gfc_is_reallocatable_lhs (expr1)
5827         && !(expr2->expr_type == EXPR_FUNCTION
5828              && expr2->value.function.isym != NULL))
5829     lss->is_alloc_lhs = 1;
5830   rss = NULL;
5831   if (lss != gfc_ss_terminator)
5832     {
5833       /* Allow the scalarizer to workshare array assignments.  */
5834       if (ompws_flags & OMPWS_WORKSHARE_FLAG)
5835         ompws_flags |= OMPWS_SCALARIZER_WS;
5836
5837       /* The assignment needs scalarization.  */
5838       lss_section = lss;
5839
5840       /* Find a non-scalar SS from the lhs.  */
5841       while (lss_section != gfc_ss_terminator
5842              && lss_section->type != GFC_SS_SECTION)
5843         lss_section = lss_section->next;
5844
5845       gcc_assert (lss_section != gfc_ss_terminator);
5846
5847       /* Initialize the scalarizer.  */
5848       gfc_init_loopinfo (&loop);
5849
5850       /* Walk the rhs.  */
5851       rss = gfc_walk_expr (expr2);
5852       if (rss == gfc_ss_terminator)
5853         {
5854           /* The rhs is scalar.  Add a ss for the expression.  */
5855           rss = gfc_get_ss ();
5856           rss->next = gfc_ss_terminator;
5857           rss->type = GFC_SS_SCALAR;
5858           rss->expr = expr2;
5859         }
5860       /* Associate the SS with the loop.  */
5861       gfc_add_ss_to_loop (&loop, lss);
5862       gfc_add_ss_to_loop (&loop, rss);
5863
5864       /* Calculate the bounds of the scalarization.  */
5865       gfc_conv_ss_startstride (&loop);
5866       /* Enable loop reversal.  */
5867       for (n = 0; n < loop.dimen; n++)
5868         loop.reverse[n] = GFC_REVERSE_NOT_SET;
5869       /* Resolve any data dependencies in the statement.  */
5870       gfc_conv_resolve_dependencies (&loop, lss, rss);
5871       /* Setup the scalarizing loops.  */
5872       gfc_conv_loop_setup (&loop, &expr2->where);
5873
5874       /* Setup the gfc_se structures.  */
5875       gfc_copy_loopinfo_to_se (&lse, &loop);
5876       gfc_copy_loopinfo_to_se (&rse, &loop);
5877
5878       rse.ss = rss;
5879       gfc_mark_ss_chain_used (rss, 1);
5880       if (loop.temp_ss == NULL)
5881         {
5882           lse.ss = lss;
5883           gfc_mark_ss_chain_used (lss, 1);
5884         }
5885       else
5886         {
5887           lse.ss = loop.temp_ss;
5888           gfc_mark_ss_chain_used (lss, 3);
5889           gfc_mark_ss_chain_used (loop.temp_ss, 3);
5890         }
5891
5892       /* Start the scalarized loop body.  */
5893       gfc_start_scalarized_body (&loop, &body);
5894     }
5895   else
5896     gfc_init_block (&body);
5897
5898   l_is_temp = (lss != gfc_ss_terminator && loop.temp_ss != NULL);
5899
5900   /* Translate the expression.  */
5901   gfc_conv_expr (&rse, expr2);
5902
5903   /* Stabilize a string length for temporaries.  */
5904   if (expr2->ts.type == BT_CHARACTER)
5905     string_length = gfc_evaluate_now (rse.string_length, &rse.pre);
5906   else
5907     string_length = NULL_TREE;
5908
5909   if (l_is_temp)
5910     {
5911       gfc_conv_tmp_array_ref (&lse);
5912       if (expr2->ts.type == BT_CHARACTER)
5913         lse.string_length = string_length;
5914     }
5915   else
5916     gfc_conv_expr (&lse, expr1);
5917
5918   /* Assignments of scalar derived types with allocatable components
5919      to arrays must be done with a deep copy and the rhs temporary
5920      must have its components deallocated afterwards.  */
5921   scalar_to_array = (expr2->ts.type == BT_DERIVED
5922                        && expr2->ts.u.derived->attr.alloc_comp
5923                        && !expr_is_variable (expr2)
5924                        && !gfc_is_constant_expr (expr2)
5925                        && expr1->rank && !expr2->rank);
5926   if (scalar_to_array && dealloc)
5927     {
5928       tmp = gfc_deallocate_alloc_comp (expr2->ts.u.derived, rse.expr, 0);
5929       gfc_add_expr_to_block (&loop.post, tmp);
5930     }
5931
5932   tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
5933                                  l_is_temp || init_flag,
5934                                  expr_is_variable (expr2) || scalar_to_array,
5935                                  dealloc);
5936   gfc_add_expr_to_block (&body, tmp);
5937
5938   if (lss == gfc_ss_terminator)
5939     {
5940       /* Use the scalar assignment as is.  */
5941       gfc_add_block_to_block (&block, &body);
5942     }
5943   else
5944     {
5945       gcc_assert (lse.ss == gfc_ss_terminator
5946                   && rse.ss == gfc_ss_terminator);
5947
5948       if (l_is_temp)
5949         {
5950           gfc_trans_scalarized_loop_boundary (&loop, &body);
5951
5952           /* We need to copy the temporary to the actual lhs.  */
5953           gfc_init_se (&lse, NULL);
5954           gfc_init_se (&rse, NULL);
5955           gfc_copy_loopinfo_to_se (&lse, &loop);
5956           gfc_copy_loopinfo_to_se (&rse, &loop);
5957
5958           rse.ss = loop.temp_ss;
5959           lse.ss = lss;
5960
5961           gfc_conv_tmp_array_ref (&rse);
5962           gfc_conv_expr (&lse, expr1);
5963
5964           gcc_assert (lse.ss == gfc_ss_terminator
5965                       && rse.ss == gfc_ss_terminator);
5966
5967           if (expr2->ts.type == BT_CHARACTER)
5968             rse.string_length = string_length;
5969
5970           tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
5971                                          false, false, dealloc);
5972           gfc_add_expr_to_block (&body, tmp);
5973         }
5974
5975       /* Allocate or reallocate lhs of allocatable array.  */
5976       if (gfc_option.flag_realloc_lhs
5977             && gfc_is_reallocatable_lhs (expr1)
5978             && !gfc_expr_attr (expr1).codimension
5979             && !gfc_is_coindexed (expr1))
5980         {
5981           tmp = gfc_alloc_allocatable_for_assignment (&loop, expr1, expr2);
5982           if (tmp != NULL_TREE)
5983             gfc_add_expr_to_block (&loop.code[expr1->rank - 1], tmp);
5984         }
5985
5986       /* Generate the copying loops.  */
5987       gfc_trans_scalarizing_loops (&loop, &body);
5988
5989       /* Wrap the whole thing up.  */
5990       gfc_add_block_to_block (&block, &loop.pre);
5991       gfc_add_block_to_block (&block, &loop.post);
5992
5993       gfc_cleanup_loop (&loop);
5994     }
5995
5996   return gfc_finish_block (&block);
5997 }
5998
5999
6000 /* Check whether EXPR is a copyable array.  */
6001
6002 static bool
6003 copyable_array_p (gfc_expr * expr)
6004 {
6005   if (expr->expr_type != EXPR_VARIABLE)
6006     return false;
6007
6008   /* First check it's an array.  */
6009   if (expr->rank < 1 || !expr->ref || expr->ref->next)
6010     return false;
6011
6012   if (!gfc_full_array_ref_p (expr->ref, NULL))
6013     return false;
6014
6015   /* Next check that it's of a simple enough type.  */
6016   switch (expr->ts.type)
6017     {
6018     case BT_INTEGER:
6019     case BT_REAL:
6020     case BT_COMPLEX:
6021     case BT_LOGICAL:
6022       return true;
6023
6024     case BT_CHARACTER:
6025       return false;
6026
6027     case BT_DERIVED:
6028       return !expr->ts.u.derived->attr.alloc_comp;
6029
6030     default:
6031       break;
6032     }
6033
6034   return false;
6035 }
6036
6037 /* Translate an assignment.  */
6038
6039 tree
6040 gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
6041                       bool dealloc)
6042 {
6043   tree tmp;
6044
6045   if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
6046     {
6047       gfc_error ("Assignment to deferred-length character variable at %L "
6048                  "not implemented", &expr1->where);
6049       return NULL_TREE;
6050     }
6051
6052   /* Special case a single function returning an array.  */
6053   if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
6054     {
6055       tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
6056       if (tmp)
6057         return tmp;
6058     }
6059
6060   /* Special case assigning an array to zero.  */
6061   if (copyable_array_p (expr1)
6062       && is_zero_initializer_p (expr2))
6063     {
6064       tmp = gfc_trans_zero_assign (expr1);
6065       if (tmp)
6066         return tmp;
6067     }
6068
6069   /* Special case copying one array to another.  */
6070   if (copyable_array_p (expr1)
6071       && copyable_array_p (expr2)
6072       && gfc_compare_types (&expr1->ts, &expr2->ts)
6073       && !gfc_check_dependency (expr1, expr2, 0))
6074     {
6075       tmp = gfc_trans_array_copy (expr1, expr2);
6076       if (tmp)
6077         return tmp;
6078     }
6079
6080   /* Special case initializing an array from a constant array constructor.  */
6081   if (copyable_array_p (expr1)
6082       && expr2->expr_type == EXPR_ARRAY
6083       && gfc_compare_types (&expr1->ts, &expr2->ts))
6084     {
6085       tmp = gfc_trans_array_constructor_copy (expr1, expr2);
6086       if (tmp)
6087         return tmp;
6088     }
6089
6090   /* Fallback to the scalarizer to generate explicit loops.  */
6091   return gfc_trans_assignment_1 (expr1, expr2, init_flag, dealloc);
6092 }
6093
6094 tree
6095 gfc_trans_init_assign (gfc_code * code)
6096 {
6097   return gfc_trans_assignment (code->expr1, code->expr2, true, false);
6098 }
6099
6100 tree
6101 gfc_trans_assign (gfc_code * code)
6102 {
6103   return gfc_trans_assignment (code->expr1, code->expr2, false, true);
6104 }
6105
6106
6107 /* Special case for initializing a polymorphic dummy with INTENT(OUT).
6108    A MEMCPY is needed to copy the full data from the default initializer
6109    of the dynamic type.  */
6110
6111 tree
6112 gfc_trans_class_init_assign (gfc_code *code)
6113 {
6114   stmtblock_t block;
6115   tree tmp;
6116   gfc_se dst,src,memsz;
6117   gfc_expr *lhs,*rhs,*sz;
6118
6119   gfc_start_block (&block);
6120
6121   lhs = gfc_copy_expr (code->expr1);
6122   gfc_add_data_component (lhs);
6123
6124   rhs = gfc_copy_expr (code->expr1);
6125   gfc_add_vptr_component (rhs);
6126   gfc_add_def_init_component (rhs);
6127
6128   sz = gfc_copy_expr (code->expr1);
6129   gfc_add_vptr_component (sz);
6130   gfc_add_size_component (sz);
6131
6132   gfc_init_se (&dst, NULL);
6133   gfc_init_se (&src, NULL);
6134   gfc_init_se (&memsz, NULL);
6135   gfc_conv_expr (&dst, lhs);
6136   gfc_conv_expr (&src, rhs);
6137   gfc_conv_expr (&memsz, sz);
6138   gfc_add_block_to_block (&block, &src.pre);
6139   tmp = gfc_build_memcpy_call (dst.expr, src.expr, memsz.expr);
6140   gfc_add_expr_to_block (&block, tmp);
6141   
6142   return gfc_finish_block (&block);
6143 }
6144
6145
6146 /* Translate an assignment to a CLASS object
6147    (pointer or ordinary assignment).  */
6148
6149 tree
6150 gfc_trans_class_assign (gfc_expr *expr1, gfc_expr *expr2, gfc_exec_op op)
6151 {
6152   stmtblock_t block;
6153   tree tmp;
6154   gfc_expr *lhs;
6155   gfc_expr *rhs;
6156
6157   gfc_start_block (&block);
6158
6159   if (expr2->ts.type != BT_CLASS)
6160     {
6161       /* Insert an additional assignment which sets the '_vptr' field.  */
6162       gfc_symbol *vtab = NULL;
6163       gfc_symtree *st;
6164
6165       lhs = gfc_copy_expr (expr1);
6166       gfc_add_vptr_component (lhs);
6167
6168       if (expr2->ts.type == BT_DERIVED)
6169         vtab = gfc_find_derived_vtab (expr2->ts.u.derived);
6170       else if (expr2->expr_type == EXPR_NULL)
6171         vtab = gfc_find_derived_vtab (expr1->ts.u.derived);
6172       gcc_assert (vtab);
6173
6174       rhs = gfc_get_expr ();
6175       rhs->expr_type = EXPR_VARIABLE;
6176       gfc_find_sym_tree (vtab->name, vtab->ns, 1, &st);
6177       rhs->symtree = st;
6178       rhs->ts = vtab->ts;
6179
6180       tmp = gfc_trans_pointer_assignment (lhs, rhs);
6181       gfc_add_expr_to_block (&block, tmp);
6182
6183       gfc_free_expr (lhs);
6184       gfc_free_expr (rhs);
6185     }
6186
6187   /* Do the actual CLASS assignment.  */
6188   if (expr2->ts.type == BT_CLASS)
6189     op = EXEC_ASSIGN;
6190   else
6191     gfc_add_data_component (expr1);
6192
6193   if (op == EXEC_ASSIGN)
6194     tmp = gfc_trans_assignment (expr1, expr2, false, true);
6195   else if (op == EXEC_POINTER_ASSIGN)
6196     tmp = gfc_trans_pointer_assignment (expr1, expr2);
6197   else
6198     gcc_unreachable();
6199
6200   gfc_add_expr_to_block (&block, tmp);
6201
6202   return gfc_finish_block (&block);
6203 }