re PR fortran/32360 (GFORTRAN WON'T COMPILE 'DATA PTR1 /NULL ()/' WHEN PTR1 HAS POINT...
[gcc.git] / gcc / fortran / expr.c
1 /* Routines for manipulation of expression nodes.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "gfortran.h"
26 #include "arith.h"
27 #include "match.h"
28
29 /* Get a new expr node. */
30
31 gfc_expr *
32 gfc_get_expr (void)
33 {
34 gfc_expr *e;
35
36 e = gfc_getmem (sizeof (gfc_expr));
37 gfc_clear_ts (&e->ts);
38 e->shape = NULL;
39 e->ref = NULL;
40 e->symtree = NULL;
41 e->con_by_offset = NULL;
42 return e;
43 }
44
45
46 /* Free an argument list and everything below it. */
47
48 void
49 gfc_free_actual_arglist (gfc_actual_arglist *a1)
50 {
51 gfc_actual_arglist *a2;
52
53 while (a1)
54 {
55 a2 = a1->next;
56 gfc_free_expr (a1->expr);
57 gfc_free (a1);
58 a1 = a2;
59 }
60 }
61
62
63 /* Copy an arglist structure and all of the arguments. */
64
65 gfc_actual_arglist *
66 gfc_copy_actual_arglist (gfc_actual_arglist *p)
67 {
68 gfc_actual_arglist *head, *tail, *new;
69
70 head = tail = NULL;
71
72 for (; p; p = p->next)
73 {
74 new = gfc_get_actual_arglist ();
75 *new = *p;
76
77 new->expr = gfc_copy_expr (p->expr);
78 new->next = NULL;
79
80 if (head == NULL)
81 head = new;
82 else
83 tail->next = new;
84
85 tail = new;
86 }
87
88 return head;
89 }
90
91
92 /* Free a list of reference structures. */
93
94 void
95 gfc_free_ref_list (gfc_ref *p)
96 {
97 gfc_ref *q;
98 int i;
99
100 for (; p; p = q)
101 {
102 q = p->next;
103
104 switch (p->type)
105 {
106 case REF_ARRAY:
107 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
108 {
109 gfc_free_expr (p->u.ar.start[i]);
110 gfc_free_expr (p->u.ar.end[i]);
111 gfc_free_expr (p->u.ar.stride[i]);
112 }
113
114 break;
115
116 case REF_SUBSTRING:
117 gfc_free_expr (p->u.ss.start);
118 gfc_free_expr (p->u.ss.end);
119 break;
120
121 case REF_COMPONENT:
122 break;
123 }
124
125 gfc_free (p);
126 }
127 }
128
129
130 /* Workhorse function for gfc_free_expr() that frees everything
131 beneath an expression node, but not the node itself. This is
132 useful when we want to simplify a node and replace it with
133 something else or the expression node belongs to another structure. */
134
135 static void
136 free_expr0 (gfc_expr *e)
137 {
138 int n;
139
140 switch (e->expr_type)
141 {
142 case EXPR_CONSTANT:
143 /* Free any parts of the value that need freeing. */
144 switch (e->ts.type)
145 {
146 case BT_INTEGER:
147 mpz_clear (e->value.integer);
148 break;
149
150 case BT_REAL:
151 mpfr_clear (e->value.real);
152 break;
153
154 case BT_CHARACTER:
155 gfc_free (e->value.character.string);
156 break;
157
158 case BT_COMPLEX:
159 mpfr_clear (e->value.complex.r);
160 mpfr_clear (e->value.complex.i);
161 break;
162
163 default:
164 break;
165 }
166
167 /* Free the representation, except in character constants where it
168 is the same as value.character.string and thus already freed. */
169 if (e->representation.string && e->ts.type != BT_CHARACTER)
170 gfc_free (e->representation.string);
171
172 break;
173
174 case EXPR_OP:
175 if (e->value.op.op1 != NULL)
176 gfc_free_expr (e->value.op.op1);
177 if (e->value.op.op2 != NULL)
178 gfc_free_expr (e->value.op.op2);
179 break;
180
181 case EXPR_FUNCTION:
182 gfc_free_actual_arglist (e->value.function.actual);
183 break;
184
185 case EXPR_VARIABLE:
186 break;
187
188 case EXPR_ARRAY:
189 case EXPR_STRUCTURE:
190 gfc_free_constructor (e->value.constructor);
191 break;
192
193 case EXPR_SUBSTRING:
194 gfc_free (e->value.character.string);
195 break;
196
197 case EXPR_NULL:
198 break;
199
200 default:
201 gfc_internal_error ("free_expr0(): Bad expr type");
202 }
203
204 /* Free a shape array. */
205 if (e->shape != NULL)
206 {
207 for (n = 0; n < e->rank; n++)
208 mpz_clear (e->shape[n]);
209
210 gfc_free (e->shape);
211 }
212
213 gfc_free_ref_list (e->ref);
214
215 memset (e, '\0', sizeof (gfc_expr));
216 }
217
218
219 /* Free an expression node and everything beneath it. */
220
221 void
222 gfc_free_expr (gfc_expr *e)
223 {
224 if (e == NULL)
225 return;
226 if (e->con_by_offset)
227 splay_tree_delete (e->con_by_offset);
228 free_expr0 (e);
229 gfc_free (e);
230 }
231
232
233 /* Graft the *src expression onto the *dest subexpression. */
234
235 void
236 gfc_replace_expr (gfc_expr *dest, gfc_expr *src)
237 {
238 free_expr0 (dest);
239 *dest = *src;
240 gfc_free (src);
241 }
242
243
244 /* Try to extract an integer constant from the passed expression node.
245 Returns an error message or NULL if the result is set. It is
246 tempting to generate an error and return SUCCESS or FAILURE, but
247 failure is OK for some callers. */
248
249 const char *
250 gfc_extract_int (gfc_expr *expr, int *result)
251 {
252 if (expr->expr_type != EXPR_CONSTANT)
253 return _("Constant expression required at %C");
254
255 if (expr->ts.type != BT_INTEGER)
256 return _("Integer expression required at %C");
257
258 if ((mpz_cmp_si (expr->value.integer, INT_MAX) > 0)
259 || (mpz_cmp_si (expr->value.integer, INT_MIN) < 0))
260 {
261 return _("Integer value too large in expression at %C");
262 }
263
264 *result = (int) mpz_get_si (expr->value.integer);
265
266 return NULL;
267 }
268
269
270 /* Recursively copy a list of reference structures. */
271
272 static gfc_ref *
273 copy_ref (gfc_ref *src)
274 {
275 gfc_array_ref *ar;
276 gfc_ref *dest;
277
278 if (src == NULL)
279 return NULL;
280
281 dest = gfc_get_ref ();
282 dest->type = src->type;
283
284 switch (src->type)
285 {
286 case REF_ARRAY:
287 ar = gfc_copy_array_ref (&src->u.ar);
288 dest->u.ar = *ar;
289 gfc_free (ar);
290 break;
291
292 case REF_COMPONENT:
293 dest->u.c = src->u.c;
294 break;
295
296 case REF_SUBSTRING:
297 dest->u.ss = src->u.ss;
298 dest->u.ss.start = gfc_copy_expr (src->u.ss.start);
299 dest->u.ss.end = gfc_copy_expr (src->u.ss.end);
300 break;
301 }
302
303 dest->next = copy_ref (src->next);
304
305 return dest;
306 }
307
308
309 /* Detect whether an expression has any vector index array references. */
310
311 int
312 gfc_has_vector_index (gfc_expr *e)
313 {
314 gfc_ref *ref;
315 int i;
316 for (ref = e->ref; ref; ref = ref->next)
317 if (ref->type == REF_ARRAY)
318 for (i = 0; i < ref->u.ar.dimen; i++)
319 if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
320 return 1;
321 return 0;
322 }
323
324
325 /* Copy a shape array. */
326
327 mpz_t *
328 gfc_copy_shape (mpz_t *shape, int rank)
329 {
330 mpz_t *new_shape;
331 int n;
332
333 if (shape == NULL)
334 return NULL;
335
336 new_shape = gfc_get_shape (rank);
337
338 for (n = 0; n < rank; n++)
339 mpz_init_set (new_shape[n], shape[n]);
340
341 return new_shape;
342 }
343
344
345 /* Copy a shape array excluding dimension N, where N is an integer
346 constant expression. Dimensions are numbered in fortran style --
347 starting with ONE.
348
349 So, if the original shape array contains R elements
350 { s1 ... sN-1 sN sN+1 ... sR-1 sR}
351 the result contains R-1 elements:
352 { s1 ... sN-1 sN+1 ... sR-1}
353
354 If anything goes wrong -- N is not a constant, its value is out
355 of range -- or anything else, just returns NULL. */
356
357 mpz_t *
358 gfc_copy_shape_excluding (mpz_t *shape, int rank, gfc_expr *dim)
359 {
360 mpz_t *new_shape, *s;
361 int i, n;
362
363 if (shape == NULL
364 || rank <= 1
365 || dim == NULL
366 || dim->expr_type != EXPR_CONSTANT
367 || dim->ts.type != BT_INTEGER)
368 return NULL;
369
370 n = mpz_get_si (dim->value.integer);
371 n--; /* Convert to zero based index. */
372 if (n < 0 || n >= rank)
373 return NULL;
374
375 s = new_shape = gfc_get_shape (rank - 1);
376
377 for (i = 0; i < rank; i++)
378 {
379 if (i == n)
380 continue;
381 mpz_init_set (*s, shape[i]);
382 s++;
383 }
384
385 return new_shape;
386 }
387
388
389 /* Given an expression pointer, return a copy of the expression. This
390 subroutine is recursive. */
391
392 gfc_expr *
393 gfc_copy_expr (gfc_expr *p)
394 {
395 gfc_expr *q;
396 char *s;
397
398 if (p == NULL)
399 return NULL;
400
401 q = gfc_get_expr ();
402 *q = *p;
403
404 switch (q->expr_type)
405 {
406 case EXPR_SUBSTRING:
407 s = gfc_getmem (p->value.character.length + 1);
408 q->value.character.string = s;
409
410 memcpy (s, p->value.character.string, p->value.character.length + 1);
411 break;
412
413 case EXPR_CONSTANT:
414 /* Copy target representation, if it exists. */
415 if (p->representation.string)
416 {
417 s = gfc_getmem (p->representation.length + 1);
418 q->representation.string = s;
419
420 memcpy (s, p->representation.string, p->representation.length + 1);
421 }
422
423 /* Copy the values of any pointer components of p->value. */
424 switch (q->ts.type)
425 {
426 case BT_INTEGER:
427 mpz_init_set (q->value.integer, p->value.integer);
428 break;
429
430 case BT_REAL:
431 gfc_set_model_kind (q->ts.kind);
432 mpfr_init (q->value.real);
433 mpfr_set (q->value.real, p->value.real, GFC_RND_MODE);
434 break;
435
436 case BT_COMPLEX:
437 gfc_set_model_kind (q->ts.kind);
438 mpfr_init (q->value.complex.r);
439 mpfr_init (q->value.complex.i);
440 mpfr_set (q->value.complex.r, p->value.complex.r, GFC_RND_MODE);
441 mpfr_set (q->value.complex.i, p->value.complex.i, GFC_RND_MODE);
442 break;
443
444 case BT_CHARACTER:
445 if (p->representation.string)
446 q->value.character.string = q->representation.string;
447 else
448 {
449 s = gfc_getmem (p->value.character.length + 1);
450 q->value.character.string = s;
451
452 memcpy (s, p->value.character.string, p->value.character.length + 1);
453 }
454 break;
455
456 case BT_HOLLERITH:
457 case BT_LOGICAL:
458 case BT_DERIVED:
459 break; /* Already done */
460
461 case BT_PROCEDURE:
462 case BT_UNKNOWN:
463 gfc_internal_error ("gfc_copy_expr(): Bad expr node");
464 /* Not reached */
465 }
466
467 break;
468
469 case EXPR_OP:
470 switch (q->value.op.operator)
471 {
472 case INTRINSIC_NOT:
473 case INTRINSIC_PARENTHESES:
474 case INTRINSIC_UPLUS:
475 case INTRINSIC_UMINUS:
476 q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
477 break;
478
479 default: /* Binary operators. */
480 q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
481 q->value.op.op2 = gfc_copy_expr (p->value.op.op2);
482 break;
483 }
484
485 break;
486
487 case EXPR_FUNCTION:
488 q->value.function.actual =
489 gfc_copy_actual_arglist (p->value.function.actual);
490 break;
491
492 case EXPR_STRUCTURE:
493 case EXPR_ARRAY:
494 q->value.constructor = gfc_copy_constructor (p->value.constructor);
495 break;
496
497 case EXPR_VARIABLE:
498 case EXPR_NULL:
499 break;
500 }
501
502 q->shape = gfc_copy_shape (p->shape, p->rank);
503
504 q->ref = copy_ref (p->ref);
505
506 return q;
507 }
508
509
510 /* Return the maximum kind of two expressions. In general, higher
511 kind numbers mean more precision for numeric types. */
512
513 int
514 gfc_kind_max (gfc_expr *e1, gfc_expr *e2)
515 {
516 return (e1->ts.kind > e2->ts.kind) ? e1->ts.kind : e2->ts.kind;
517 }
518
519
520 /* Returns nonzero if the type is numeric, zero otherwise. */
521
522 static int
523 numeric_type (bt type)
524 {
525 return type == BT_COMPLEX || type == BT_REAL || type == BT_INTEGER;
526 }
527
528
529 /* Returns nonzero if the typespec is a numeric type, zero otherwise. */
530
531 int
532 gfc_numeric_ts (gfc_typespec *ts)
533 {
534 return numeric_type (ts->type);
535 }
536
537
538 /* Returns an expression node that is an integer constant. */
539
540 gfc_expr *
541 gfc_int_expr (int i)
542 {
543 gfc_expr *p;
544
545 p = gfc_get_expr ();
546
547 p->expr_type = EXPR_CONSTANT;
548 p->ts.type = BT_INTEGER;
549 p->ts.kind = gfc_default_integer_kind;
550
551 p->where = gfc_current_locus;
552 mpz_init_set_si (p->value.integer, i);
553
554 return p;
555 }
556
557
558 /* Returns an expression node that is a logical constant. */
559
560 gfc_expr *
561 gfc_logical_expr (int i, locus *where)
562 {
563 gfc_expr *p;
564
565 p = gfc_get_expr ();
566
567 p->expr_type = EXPR_CONSTANT;
568 p->ts.type = BT_LOGICAL;
569 p->ts.kind = gfc_default_logical_kind;
570
571 if (where == NULL)
572 where = &gfc_current_locus;
573 p->where = *where;
574 p->value.logical = i;
575
576 return p;
577 }
578
579
580 /* Return an expression node with an optional argument list attached.
581 A variable number of gfc_expr pointers are strung together in an
582 argument list with a NULL pointer terminating the list. */
583
584 gfc_expr *
585 gfc_build_conversion (gfc_expr *e)
586 {
587 gfc_expr *p;
588
589 p = gfc_get_expr ();
590 p->expr_type = EXPR_FUNCTION;
591 p->symtree = NULL;
592 p->value.function.actual = NULL;
593
594 p->value.function.actual = gfc_get_actual_arglist ();
595 p->value.function.actual->expr = e;
596
597 return p;
598 }
599
600
601 /* Given an expression node with some sort of numeric binary
602 expression, insert type conversions required to make the operands
603 have the same type.
604
605 The exception is that the operands of an exponential don't have to
606 have the same type. If possible, the base is promoted to the type
607 of the exponent. For example, 1**2.3 becomes 1.0**2.3, but
608 1.0**2 stays as it is. */
609
610 void
611 gfc_type_convert_binary (gfc_expr *e)
612 {
613 gfc_expr *op1, *op2;
614
615 op1 = e->value.op.op1;
616 op2 = e->value.op.op2;
617
618 if (op1->ts.type == BT_UNKNOWN || op2->ts.type == BT_UNKNOWN)
619 {
620 gfc_clear_ts (&e->ts);
621 return;
622 }
623
624 /* Kind conversions of same type. */
625 if (op1->ts.type == op2->ts.type)
626 {
627 if (op1->ts.kind == op2->ts.kind)
628 {
629 /* No type conversions. */
630 e->ts = op1->ts;
631 goto done;
632 }
633
634 if (op1->ts.kind > op2->ts.kind)
635 gfc_convert_type (op2, &op1->ts, 2);
636 else
637 gfc_convert_type (op1, &op2->ts, 2);
638
639 e->ts = op1->ts;
640 goto done;
641 }
642
643 /* Integer combined with real or complex. */
644 if (op2->ts.type == BT_INTEGER)
645 {
646 e->ts = op1->ts;
647
648 /* Special case for ** operator. */
649 if (e->value.op.operator == INTRINSIC_POWER)
650 goto done;
651
652 gfc_convert_type (e->value.op.op2, &e->ts, 2);
653 goto done;
654 }
655
656 if (op1->ts.type == BT_INTEGER)
657 {
658 e->ts = op2->ts;
659 gfc_convert_type (e->value.op.op1, &e->ts, 2);
660 goto done;
661 }
662
663 /* Real combined with complex. */
664 e->ts.type = BT_COMPLEX;
665 if (op1->ts.kind > op2->ts.kind)
666 e->ts.kind = op1->ts.kind;
667 else
668 e->ts.kind = op2->ts.kind;
669 if (op1->ts.type != BT_COMPLEX || op1->ts.kind != e->ts.kind)
670 gfc_convert_type (e->value.op.op1, &e->ts, 2);
671 if (op2->ts.type != BT_COMPLEX || op2->ts.kind != e->ts.kind)
672 gfc_convert_type (e->value.op.op2, &e->ts, 2);
673
674 done:
675 return;
676 }
677
678
679 /* Function to determine if an expression is constant or not. This
680 function expects that the expression has already been simplified. */
681
682 int
683 gfc_is_constant_expr (gfc_expr *e)
684 {
685 gfc_constructor *c;
686 gfc_actual_arglist *arg;
687 int rv;
688
689 if (e == NULL)
690 return 1;
691
692 switch (e->expr_type)
693 {
694 case EXPR_OP:
695 rv = (gfc_is_constant_expr (e->value.op.op1)
696 && (e->value.op.op2 == NULL
697 || gfc_is_constant_expr (e->value.op.op2)));
698 break;
699
700 case EXPR_VARIABLE:
701 rv = 0;
702 break;
703
704 case EXPR_FUNCTION:
705 /* Call to intrinsic with at least one argument. */
706 rv = 0;
707 if (e->value.function.isym && e->value.function.actual)
708 {
709 for (arg = e->value.function.actual; arg; arg = arg->next)
710 {
711 if (!gfc_is_constant_expr (arg->expr))
712 break;
713 }
714 if (arg == NULL)
715 rv = 1;
716 }
717 break;
718
719 case EXPR_CONSTANT:
720 case EXPR_NULL:
721 rv = 1;
722 break;
723
724 case EXPR_SUBSTRING:
725 rv = (gfc_is_constant_expr (e->ref->u.ss.start)
726 && gfc_is_constant_expr (e->ref->u.ss.end));
727 break;
728
729 case EXPR_STRUCTURE:
730 rv = 0;
731 for (c = e->value.constructor; c; c = c->next)
732 if (!gfc_is_constant_expr (c->expr))
733 break;
734
735 if (c == NULL)
736 rv = 1;
737 break;
738
739 case EXPR_ARRAY:
740 rv = gfc_constant_ac (e);
741 break;
742
743 default:
744 gfc_internal_error ("gfc_is_constant_expr(): Unknown expression type");
745 }
746
747 return rv;
748 }
749
750
751 /* Try to collapse intrinsic expressions. */
752
753 static try
754 simplify_intrinsic_op (gfc_expr *p, int type)
755 {
756 gfc_expr *op1, *op2, *result;
757
758 if (p->value.op.operator == INTRINSIC_USER)
759 return SUCCESS;
760
761 op1 = p->value.op.op1;
762 op2 = p->value.op.op2;
763
764 if (gfc_simplify_expr (op1, type) == FAILURE)
765 return FAILURE;
766 if (gfc_simplify_expr (op2, type) == FAILURE)
767 return FAILURE;
768
769 if (!gfc_is_constant_expr (op1)
770 || (op2 != NULL && !gfc_is_constant_expr (op2)))
771 return SUCCESS;
772
773 /* Rip p apart. */
774 p->value.op.op1 = NULL;
775 p->value.op.op2 = NULL;
776
777 switch (p->value.op.operator)
778 {
779 case INTRINSIC_PARENTHESES:
780 result = gfc_parentheses (op1);
781 break;
782
783 case INTRINSIC_UPLUS:
784 result = gfc_uplus (op1);
785 break;
786
787 case INTRINSIC_UMINUS:
788 result = gfc_uminus (op1);
789 break;
790
791 case INTRINSIC_PLUS:
792 result = gfc_add (op1, op2);
793 break;
794
795 case INTRINSIC_MINUS:
796 result = gfc_subtract (op1, op2);
797 break;
798
799 case INTRINSIC_TIMES:
800 result = gfc_multiply (op1, op2);
801 break;
802
803 case INTRINSIC_DIVIDE:
804 result = gfc_divide (op1, op2);
805 break;
806
807 case INTRINSIC_POWER:
808 result = gfc_power (op1, op2);
809 break;
810
811 case INTRINSIC_CONCAT:
812 result = gfc_concat (op1, op2);
813 break;
814
815 case INTRINSIC_EQ:
816 result = gfc_eq (op1, op2);
817 break;
818
819 case INTRINSIC_NE:
820 result = gfc_ne (op1, op2);
821 break;
822
823 case INTRINSIC_GT:
824 result = gfc_gt (op1, op2);
825 break;
826
827 case INTRINSIC_GE:
828 result = gfc_ge (op1, op2);
829 break;
830
831 case INTRINSIC_LT:
832 result = gfc_lt (op1, op2);
833 break;
834
835 case INTRINSIC_LE:
836 result = gfc_le (op1, op2);
837 break;
838
839 case INTRINSIC_NOT:
840 result = gfc_not (op1);
841 break;
842
843 case INTRINSIC_AND:
844 result = gfc_and (op1, op2);
845 break;
846
847 case INTRINSIC_OR:
848 result = gfc_or (op1, op2);
849 break;
850
851 case INTRINSIC_EQV:
852 result = gfc_eqv (op1, op2);
853 break;
854
855 case INTRINSIC_NEQV:
856 result = gfc_neqv (op1, op2);
857 break;
858
859 default:
860 gfc_internal_error ("simplify_intrinsic_op(): Bad operator");
861 }
862
863 if (result == NULL)
864 {
865 gfc_free_expr (op1);
866 gfc_free_expr (op2);
867 return FAILURE;
868 }
869
870 result->rank = p->rank;
871 result->where = p->where;
872 gfc_replace_expr (p, result);
873
874 return SUCCESS;
875 }
876
877
878 /* Subroutine to simplify constructor expressions. Mutually recursive
879 with gfc_simplify_expr(). */
880
881 static try
882 simplify_constructor (gfc_constructor *c, int type)
883 {
884 for (; c; c = c->next)
885 {
886 if (c->iterator
887 && (gfc_simplify_expr (c->iterator->start, type) == FAILURE
888 || gfc_simplify_expr (c->iterator->end, type) == FAILURE
889 || gfc_simplify_expr (c->iterator->step, type) == FAILURE))
890 return FAILURE;
891
892 if (c->expr && gfc_simplify_expr (c->expr, type) == FAILURE)
893 return FAILURE;
894 }
895
896 return SUCCESS;
897 }
898
899
900 /* Pull a single array element out of an array constructor. */
901
902 static try
903 find_array_element (gfc_constructor *cons, gfc_array_ref *ar,
904 gfc_constructor **rval)
905 {
906 unsigned long nelemen;
907 int i;
908 mpz_t delta;
909 mpz_t offset;
910 mpz_t span;
911 mpz_t tmp;
912 gfc_expr *e;
913 try t;
914
915 t = SUCCESS;
916 e = NULL;
917
918 mpz_init_set_ui (offset, 0);
919 mpz_init (delta);
920 mpz_init (tmp);
921 mpz_init_set_ui (span, 1);
922 for (i = 0; i < ar->dimen; i++)
923 {
924 e = gfc_copy_expr (ar->start[i]);
925 if (e->expr_type != EXPR_CONSTANT)
926 {
927 cons = NULL;
928 goto depart;
929 }
930
931 /* Check the bounds. */
932 if (ar->as->upper[i]
933 && (mpz_cmp (e->value.integer, ar->as->upper[i]->value.integer) > 0
934 || mpz_cmp (e->value.integer,
935 ar->as->lower[i]->value.integer) < 0))
936 {
937 gfc_error ("index in dimension %d is out of bounds "
938 "at %L", i + 1, &ar->c_where[i]);
939 cons = NULL;
940 t = FAILURE;
941 goto depart;
942 }
943
944 mpz_sub (delta, e->value.integer, ar->as->lower[i]->value.integer);
945 mpz_mul (delta, delta, span);
946 mpz_add (offset, offset, delta);
947
948 mpz_set_ui (tmp, 1);
949 mpz_add (tmp, tmp, ar->as->upper[i]->value.integer);
950 mpz_sub (tmp, tmp, ar->as->lower[i]->value.integer);
951 mpz_mul (span, span, tmp);
952 }
953
954 if (cons)
955 {
956 for (nelemen = mpz_get_ui (offset); nelemen > 0; nelemen--)
957 {
958 if (cons->iterator)
959 {
960 cons = NULL;
961 goto depart;
962 }
963 cons = cons->next;
964 }
965 }
966
967 depart:
968 mpz_clear (delta);
969 mpz_clear (offset);
970 mpz_clear (span);
971 mpz_clear (tmp);
972 if (e)
973 gfc_free_expr (e);
974 *rval = cons;
975 return t;
976 }
977
978
979 /* Find a component of a structure constructor. */
980
981 static gfc_constructor *
982 find_component_ref (gfc_constructor *cons, gfc_ref *ref)
983 {
984 gfc_component *comp;
985 gfc_component *pick;
986
987 comp = ref->u.c.sym->components;
988 pick = ref->u.c.component;
989 while (comp != pick)
990 {
991 comp = comp->next;
992 cons = cons->next;
993 }
994
995 return cons;
996 }
997
998
999 /* Replace an expression with the contents of a constructor, removing
1000 the subobject reference in the process. */
1001
1002 static void
1003 remove_subobject_ref (gfc_expr *p, gfc_constructor *cons)
1004 {
1005 gfc_expr *e;
1006
1007 e = cons->expr;
1008 cons->expr = NULL;
1009 e->ref = p->ref->next;
1010 p->ref->next = NULL;
1011 gfc_replace_expr (p, e);
1012 }
1013
1014
1015 /* Pull an array section out of an array constructor. */
1016
1017 static try
1018 find_array_section (gfc_expr *expr, gfc_ref *ref)
1019 {
1020 int idx;
1021 int rank;
1022 int d;
1023 int shape_i;
1024 long unsigned one = 1;
1025 bool incr_ctr;
1026 mpz_t start[GFC_MAX_DIMENSIONS];
1027 mpz_t end[GFC_MAX_DIMENSIONS];
1028 mpz_t stride[GFC_MAX_DIMENSIONS];
1029 mpz_t delta[GFC_MAX_DIMENSIONS];
1030 mpz_t ctr[GFC_MAX_DIMENSIONS];
1031 mpz_t delta_mpz;
1032 mpz_t tmp_mpz;
1033 mpz_t nelts;
1034 mpz_t ptr;
1035 mpz_t index;
1036 gfc_constructor *cons;
1037 gfc_constructor *base;
1038 gfc_expr *begin;
1039 gfc_expr *finish;
1040 gfc_expr *step;
1041 gfc_expr *upper;
1042 gfc_expr *lower;
1043 gfc_constructor *vecsub[GFC_MAX_DIMENSIONS], *c;
1044 try t;
1045
1046 t = SUCCESS;
1047
1048 base = expr->value.constructor;
1049 expr->value.constructor = NULL;
1050
1051 rank = ref->u.ar.as->rank;
1052
1053 if (expr->shape == NULL)
1054 expr->shape = gfc_get_shape (rank);
1055
1056 mpz_init_set_ui (delta_mpz, one);
1057 mpz_init_set_ui (nelts, one);
1058 mpz_init (tmp_mpz);
1059
1060 /* Do the initialization now, so that we can cleanup without
1061 keeping track of where we were. */
1062 for (d = 0; d < rank; d++)
1063 {
1064 mpz_init (delta[d]);
1065 mpz_init (start[d]);
1066 mpz_init (end[d]);
1067 mpz_init (ctr[d]);
1068 mpz_init (stride[d]);
1069 vecsub[d] = NULL;
1070 }
1071
1072 /* Build the counters to clock through the array reference. */
1073 shape_i = 0;
1074 for (d = 0; d < rank; d++)
1075 {
1076 /* Make this stretch of code easier on the eye! */
1077 begin = ref->u.ar.start[d];
1078 finish = ref->u.ar.end[d];
1079 step = ref->u.ar.stride[d];
1080 lower = ref->u.ar.as->lower[d];
1081 upper = ref->u.ar.as->upper[d];
1082
1083 if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR) /* Vector subscript. */
1084 {
1085 gcc_assert (begin);
1086
1087 if (begin->expr_type != EXPR_ARRAY)
1088 {
1089 t = FAILURE;
1090 goto cleanup;
1091 }
1092
1093 gcc_assert (begin->rank == 1);
1094 gcc_assert (begin->shape);
1095
1096 vecsub[d] = begin->value.constructor;
1097 mpz_set (ctr[d], vecsub[d]->expr->value.integer);
1098 mpz_mul (nelts, nelts, begin->shape[0]);
1099 mpz_set (expr->shape[shape_i++], begin->shape[0]);
1100
1101 /* Check bounds. */
1102 for (c = vecsub[d]; c; c = c->next)
1103 {
1104 if (mpz_cmp (c->expr->value.integer, upper->value.integer) > 0
1105 || mpz_cmp (c->expr->value.integer,
1106 lower->value.integer) < 0)
1107 {
1108 gfc_error ("index in dimension %d is out of bounds "
1109 "at %L", d + 1, &ref->u.ar.c_where[d]);
1110 t = FAILURE;
1111 goto cleanup;
1112 }
1113 }
1114 }
1115 else
1116 {
1117 if ((begin && begin->expr_type != EXPR_CONSTANT)
1118 || (finish && finish->expr_type != EXPR_CONSTANT)
1119 || (step && step->expr_type != EXPR_CONSTANT))
1120 {
1121 t = FAILURE;
1122 goto cleanup;
1123 }
1124
1125 /* Obtain the stride. */
1126 if (step)
1127 mpz_set (stride[d], step->value.integer);
1128 else
1129 mpz_set_ui (stride[d], one);
1130
1131 if (mpz_cmp_ui (stride[d], 0) == 0)
1132 mpz_set_ui (stride[d], one);
1133
1134 /* Obtain the start value for the index. */
1135 if (begin)
1136 mpz_set (start[d], begin->value.integer);
1137 else
1138 mpz_set (start[d], lower->value.integer);
1139
1140 mpz_set (ctr[d], start[d]);
1141
1142 /* Obtain the end value for the index. */
1143 if (finish)
1144 mpz_set (end[d], finish->value.integer);
1145 else
1146 mpz_set (end[d], upper->value.integer);
1147
1148 /* Separate 'if' because elements sometimes arrive with
1149 non-null end. */
1150 if (ref->u.ar.dimen_type[d] == DIMEN_ELEMENT)
1151 mpz_set (end [d], begin->value.integer);
1152
1153 /* Check the bounds. */
1154 if (mpz_cmp (ctr[d], upper->value.integer) > 0
1155 || mpz_cmp (end[d], upper->value.integer) > 0
1156 || mpz_cmp (ctr[d], lower->value.integer) < 0
1157 || mpz_cmp (end[d], lower->value.integer) < 0)
1158 {
1159 gfc_error ("index in dimension %d is out of bounds "
1160 "at %L", d + 1, &ref->u.ar.c_where[d]);
1161 t = FAILURE;
1162 goto cleanup;
1163 }
1164
1165 /* Calculate the number of elements and the shape. */
1166 mpz_set (tmp_mpz, stride[d]);
1167 mpz_add (tmp_mpz, end[d], tmp_mpz);
1168 mpz_sub (tmp_mpz, tmp_mpz, ctr[d]);
1169 mpz_div (tmp_mpz, tmp_mpz, stride[d]);
1170 mpz_mul (nelts, nelts, tmp_mpz);
1171
1172 /* An element reference reduces the rank of the expression; don't
1173 add anything to the shape array. */
1174 if (ref->u.ar.dimen_type[d] != DIMEN_ELEMENT)
1175 mpz_set (expr->shape[shape_i++], tmp_mpz);
1176 }
1177
1178 /* Calculate the 'stride' (=delta) for conversion of the
1179 counter values into the index along the constructor. */
1180 mpz_set (delta[d], delta_mpz);
1181 mpz_sub (tmp_mpz, upper->value.integer, lower->value.integer);
1182 mpz_add_ui (tmp_mpz, tmp_mpz, one);
1183 mpz_mul (delta_mpz, delta_mpz, tmp_mpz);
1184 }
1185
1186 mpz_init (index);
1187 mpz_init (ptr);
1188 cons = base;
1189
1190 /* Now clock through the array reference, calculating the index in
1191 the source constructor and transferring the elements to the new
1192 constructor. */
1193 for (idx = 0; idx < (int) mpz_get_si (nelts); idx++)
1194 {
1195 if (ref->u.ar.offset)
1196 mpz_set (ptr, ref->u.ar.offset->value.integer);
1197 else
1198 mpz_init_set_ui (ptr, 0);
1199
1200 incr_ctr = true;
1201 for (d = 0; d < rank; d++)
1202 {
1203 mpz_set (tmp_mpz, ctr[d]);
1204 mpz_sub (tmp_mpz, tmp_mpz, ref->u.ar.as->lower[d]->value.integer);
1205 mpz_mul (tmp_mpz, tmp_mpz, delta[d]);
1206 mpz_add (ptr, ptr, tmp_mpz);
1207
1208 if (!incr_ctr) continue;
1209
1210 if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR) /* Vector subscript. */
1211 {
1212 gcc_assert(vecsub[d]);
1213
1214 if (!vecsub[d]->next)
1215 vecsub[d] = ref->u.ar.start[d]->value.constructor;
1216 else
1217 {
1218 vecsub[d] = vecsub[d]->next;
1219 incr_ctr = false;
1220 }
1221 mpz_set (ctr[d], vecsub[d]->expr->value.integer);
1222 }
1223 else
1224 {
1225 mpz_add (ctr[d], ctr[d], stride[d]);
1226
1227 if (mpz_cmp_ui (stride[d], 0) > 0
1228 ? mpz_cmp (ctr[d], end[d]) > 0
1229 : mpz_cmp (ctr[d], end[d]) < 0)
1230 mpz_set (ctr[d], start[d]);
1231 else
1232 incr_ctr = false;
1233 }
1234 }
1235
1236 /* There must be a better way of dealing with negative strides
1237 than resetting the index and the constructor pointer! */
1238 if (mpz_cmp (ptr, index) < 0)
1239 {
1240 mpz_set_ui (index, 0);
1241 cons = base;
1242 }
1243
1244 while (mpz_cmp (ptr, index) > 0)
1245 {
1246 mpz_add_ui (index, index, one);
1247 cons = cons->next;
1248 }
1249
1250 gfc_append_constructor (expr, gfc_copy_expr (cons->expr));
1251 }
1252
1253 mpz_clear (ptr);
1254 mpz_clear (index);
1255
1256 cleanup:
1257
1258 mpz_clear (delta_mpz);
1259 mpz_clear (tmp_mpz);
1260 mpz_clear (nelts);
1261 for (d = 0; d < rank; d++)
1262 {
1263 mpz_clear (delta[d]);
1264 mpz_clear (start[d]);
1265 mpz_clear (end[d]);
1266 mpz_clear (ctr[d]);
1267 mpz_clear (stride[d]);
1268 }
1269 gfc_free_constructor (base);
1270 return t;
1271 }
1272
1273 /* Pull a substring out of an expression. */
1274
1275 static try
1276 find_substring_ref (gfc_expr *p, gfc_expr **newp)
1277 {
1278 int end;
1279 int start;
1280 char *chr;
1281
1282 if (p->ref->u.ss.start->expr_type != EXPR_CONSTANT
1283 || p->ref->u.ss.end->expr_type != EXPR_CONSTANT)
1284 return FAILURE;
1285
1286 *newp = gfc_copy_expr (p);
1287 chr = p->value.character.string;
1288 end = (int) mpz_get_ui (p->ref->u.ss.end->value.integer);
1289 start = (int) mpz_get_ui (p->ref->u.ss.start->value.integer);
1290
1291 (*newp)->value.character.length = end - start + 1;
1292 strncpy ((*newp)->value.character.string, &chr[start - 1],
1293 (*newp)->value.character.length);
1294 return SUCCESS;
1295 }
1296
1297
1298
1299 /* Simplify a subobject reference of a constructor. This occurs when
1300 parameter variable values are substituted. */
1301
1302 static try
1303 simplify_const_ref (gfc_expr *p)
1304 {
1305 gfc_constructor *cons;
1306 gfc_expr *newp;
1307
1308 while (p->ref)
1309 {
1310 switch (p->ref->type)
1311 {
1312 case REF_ARRAY:
1313 switch (p->ref->u.ar.type)
1314 {
1315 case AR_ELEMENT:
1316 if (find_array_element (p->value.constructor, &p->ref->u.ar,
1317 &cons) == FAILURE)
1318 return FAILURE;
1319
1320 if (!cons)
1321 return SUCCESS;
1322
1323 remove_subobject_ref (p, cons);
1324 break;
1325
1326 case AR_SECTION:
1327 if (find_array_section (p, p->ref) == FAILURE)
1328 return FAILURE;
1329 p->ref->u.ar.type = AR_FULL;
1330
1331 /* Fall through. */
1332
1333 case AR_FULL:
1334 if (p->ref->next != NULL
1335 && (p->ts.type == BT_CHARACTER || p->ts.type == BT_DERIVED))
1336 {
1337 cons = p->value.constructor;
1338 for (; cons; cons = cons->next)
1339 {
1340 cons->expr->ref = copy_ref (p->ref->next);
1341 simplify_const_ref (cons->expr);
1342 }
1343 }
1344 gfc_free_ref_list (p->ref);
1345 p->ref = NULL;
1346 break;
1347
1348 default:
1349 return SUCCESS;
1350 }
1351
1352 break;
1353
1354 case REF_COMPONENT:
1355 cons = find_component_ref (p->value.constructor, p->ref);
1356 remove_subobject_ref (p, cons);
1357 break;
1358
1359 case REF_SUBSTRING:
1360 if (find_substring_ref (p, &newp) == FAILURE)
1361 return FAILURE;
1362
1363 gfc_replace_expr (p, newp);
1364 gfc_free_ref_list (p->ref);
1365 p->ref = NULL;
1366 break;
1367 }
1368 }
1369
1370 return SUCCESS;
1371 }
1372
1373
1374 /* Simplify a chain of references. */
1375
1376 static try
1377 simplify_ref_chain (gfc_ref *ref, int type)
1378 {
1379 int n;
1380
1381 for (; ref; ref = ref->next)
1382 {
1383 switch (ref->type)
1384 {
1385 case REF_ARRAY:
1386 for (n = 0; n < ref->u.ar.dimen; n++)
1387 {
1388 if (gfc_simplify_expr (ref->u.ar.start[n], type) == FAILURE)
1389 return FAILURE;
1390 if (gfc_simplify_expr (ref->u.ar.end[n], type) == FAILURE)
1391 return FAILURE;
1392 if (gfc_simplify_expr (ref->u.ar.stride[n], type) == FAILURE)
1393 return FAILURE;
1394 }
1395 break;
1396
1397 case REF_SUBSTRING:
1398 if (gfc_simplify_expr (ref->u.ss.start, type) == FAILURE)
1399 return FAILURE;
1400 if (gfc_simplify_expr (ref->u.ss.end, type) == FAILURE)
1401 return FAILURE;
1402 break;
1403
1404 default:
1405 break;
1406 }
1407 }
1408 return SUCCESS;
1409 }
1410
1411
1412 /* Try to substitute the value of a parameter variable. */
1413
1414 static try
1415 simplify_parameter_variable (gfc_expr *p, int type)
1416 {
1417 gfc_expr *e;
1418 try t;
1419
1420 e = gfc_copy_expr (p->symtree->n.sym->value);
1421 if (e == NULL)
1422 return FAILURE;
1423
1424 e->rank = p->rank;
1425
1426 /* Do not copy subobject refs for constant. */
1427 if (e->expr_type != EXPR_CONSTANT && p->ref != NULL)
1428 e->ref = copy_ref (p->ref);
1429 t = gfc_simplify_expr (e, type);
1430
1431 /* Only use the simplification if it eliminated all subobject references. */
1432 if (t == SUCCESS && !e->ref)
1433 gfc_replace_expr (p, e);
1434 else
1435 gfc_free_expr (e);
1436
1437 return t;
1438 }
1439
1440 /* Given an expression, simplify it by collapsing constant
1441 expressions. Most simplification takes place when the expression
1442 tree is being constructed. If an intrinsic function is simplified
1443 at some point, we get called again to collapse the result against
1444 other constants.
1445
1446 We work by recursively simplifying expression nodes, simplifying
1447 intrinsic functions where possible, which can lead to further
1448 constant collapsing. If an operator has constant operand(s), we
1449 rip the expression apart, and rebuild it, hoping that it becomes
1450 something simpler.
1451
1452 The expression type is defined for:
1453 0 Basic expression parsing
1454 1 Simplifying array constructors -- will substitute
1455 iterator values.
1456 Returns FAILURE on error, SUCCESS otherwise.
1457 NOTE: Will return SUCCESS even if the expression can not be simplified. */
1458
1459 try
1460 gfc_simplify_expr (gfc_expr *p, int type)
1461 {
1462 gfc_actual_arglist *ap;
1463
1464 if (p == NULL)
1465 return SUCCESS;
1466
1467 switch (p->expr_type)
1468 {
1469 case EXPR_CONSTANT:
1470 case EXPR_NULL:
1471 break;
1472
1473 case EXPR_FUNCTION:
1474 for (ap = p->value.function.actual; ap; ap = ap->next)
1475 if (gfc_simplify_expr (ap->expr, type) == FAILURE)
1476 return FAILURE;
1477
1478 if (p->value.function.isym != NULL
1479 && gfc_intrinsic_func_interface (p, 1) == MATCH_ERROR)
1480 return FAILURE;
1481
1482 break;
1483
1484 case EXPR_SUBSTRING:
1485 if (simplify_ref_chain (p->ref, type) == FAILURE)
1486 return FAILURE;
1487
1488 if (gfc_is_constant_expr (p))
1489 {
1490 char *s;
1491 int start, end;
1492
1493 gfc_extract_int (p->ref->u.ss.start, &start);
1494 start--; /* Convert from one-based to zero-based. */
1495 gfc_extract_int (p->ref->u.ss.end, &end);
1496 s = gfc_getmem (end - start + 2);
1497 memcpy (s, p->value.character.string + start, end - start);
1498 s[end - start + 1] = '\0'; /* TODO: C-style string. */
1499 gfc_free (p->value.character.string);
1500 p->value.character.string = s;
1501 p->value.character.length = end - start;
1502 p->ts.cl = gfc_get_charlen ();
1503 p->ts.cl->next = gfc_current_ns->cl_list;
1504 gfc_current_ns->cl_list = p->ts.cl;
1505 p->ts.cl->length = gfc_int_expr (p->value.character.length);
1506 gfc_free_ref_list (p->ref);
1507 p->ref = NULL;
1508 p->expr_type = EXPR_CONSTANT;
1509 }
1510 break;
1511
1512 case EXPR_OP:
1513 if (simplify_intrinsic_op (p, type) == FAILURE)
1514 return FAILURE;
1515 break;
1516
1517 case EXPR_VARIABLE:
1518 /* Only substitute array parameter variables if we are in an
1519 initialization expression, or we want a subsection. */
1520 if (p->symtree->n.sym->attr.flavor == FL_PARAMETER
1521 && (gfc_init_expr || p->ref
1522 || p->symtree->n.sym->value->expr_type != EXPR_ARRAY))
1523 {
1524 if (simplify_parameter_variable (p, type) == FAILURE)
1525 return FAILURE;
1526 break;
1527 }
1528
1529 if (type == 1)
1530 {
1531 gfc_simplify_iterator_var (p);
1532 }
1533
1534 /* Simplify subcomponent references. */
1535 if (simplify_ref_chain (p->ref, type) == FAILURE)
1536 return FAILURE;
1537
1538 break;
1539
1540 case EXPR_STRUCTURE:
1541 case EXPR_ARRAY:
1542 if (simplify_ref_chain (p->ref, type) == FAILURE)
1543 return FAILURE;
1544
1545 if (simplify_constructor (p->value.constructor, type) == FAILURE)
1546 return FAILURE;
1547
1548 if (p->expr_type == EXPR_ARRAY && p->ref && p->ref->type == REF_ARRAY
1549 && p->ref->u.ar.type == AR_FULL)
1550 gfc_expand_constructor (p);
1551
1552 if (simplify_const_ref (p) == FAILURE)
1553 return FAILURE;
1554
1555 break;
1556 }
1557
1558 return SUCCESS;
1559 }
1560
1561
1562 /* Returns the type of an expression with the exception that iterator
1563 variables are automatically integers no matter what else they may
1564 be declared as. */
1565
1566 static bt
1567 et0 (gfc_expr *e)
1568 {
1569 if (e->expr_type == EXPR_VARIABLE && gfc_check_iter_variable (e) == SUCCESS)
1570 return BT_INTEGER;
1571
1572 return e->ts.type;
1573 }
1574
1575
1576 /* Check an intrinsic arithmetic operation to see if it is consistent
1577 with some type of expression. */
1578
1579 static try check_init_expr (gfc_expr *);
1580
1581
1582 /* Scalarize an expression for an elemental intrinsic call. */
1583
1584 static try
1585 scalarize_intrinsic_call (gfc_expr *e)
1586 {
1587 gfc_actual_arglist *a, *b;
1588 gfc_constructor *args[5], *ctor, *new_ctor;
1589 gfc_expr *expr, *old;
1590 int n, i, rank[5];
1591
1592 old = gfc_copy_expr (e);
1593
1594 /* Assume that the old expression carries the type information and
1595 that the first arg carries all the shape information. */
1596 expr = gfc_copy_expr (old->value.function.actual->expr);
1597 gfc_free_constructor (expr->value.constructor);
1598 expr->value.constructor = NULL;
1599
1600 expr->ts = old->ts;
1601 expr->expr_type = EXPR_ARRAY;
1602
1603 /* Copy the array argument constructors into an array, with nulls
1604 for the scalars. */
1605 n = 0;
1606 a = old->value.function.actual;
1607 for (; a; a = a->next)
1608 {
1609 /* Check that this is OK for an initialization expression. */
1610 if (a->expr && check_init_expr (a->expr) == FAILURE)
1611 goto cleanup;
1612
1613 rank[n] = 0;
1614 if (a->expr && a->expr->rank && a->expr->expr_type == EXPR_VARIABLE)
1615 {
1616 rank[n] = a->expr->rank;
1617 ctor = a->expr->symtree->n.sym->value->value.constructor;
1618 args[n] = gfc_copy_constructor (ctor);
1619 }
1620 else if (a->expr && a->expr->expr_type == EXPR_ARRAY)
1621 {
1622 if (a->expr->rank)
1623 rank[n] = a->expr->rank;
1624 else
1625 rank[n] = 1;
1626 args[n] = gfc_copy_constructor (a->expr->value.constructor);
1627 }
1628 else
1629 args[n] = NULL;
1630 n++;
1631 }
1632
1633 for (i = 1; i < n; i++)
1634 if (rank[i] && rank[i] != rank[0])
1635 goto compliance;
1636
1637 /* Using the first argument as the master, step through the array
1638 calling the function for each element and advancing the array
1639 constructors together. */
1640 ctor = args[0];
1641 new_ctor = NULL;
1642 for (; ctor; ctor = ctor->next)
1643 {
1644 if (expr->value.constructor == NULL)
1645 expr->value.constructor
1646 = new_ctor = gfc_get_constructor ();
1647 else
1648 {
1649 new_ctor->next = gfc_get_constructor ();
1650 new_ctor = new_ctor->next;
1651 }
1652 new_ctor->expr = gfc_copy_expr (old);
1653 gfc_free_actual_arglist (new_ctor->expr->value.function.actual);
1654 a = NULL;
1655 b = old->value.function.actual;
1656 for (i = 0; i < n; i++)
1657 {
1658 if (a == NULL)
1659 new_ctor->expr->value.function.actual
1660 = a = gfc_get_actual_arglist ();
1661 else
1662 {
1663 a->next = gfc_get_actual_arglist ();
1664 a = a->next;
1665 }
1666 if (args[i])
1667 a->expr = gfc_copy_expr (args[i]->expr);
1668 else
1669 a->expr = gfc_copy_expr (b->expr);
1670
1671 b = b->next;
1672 }
1673
1674 /* Simplify the function calls. */
1675 if (gfc_simplify_expr (new_ctor->expr, 0) == FAILURE)
1676 goto cleanup;
1677
1678 for (i = 0; i < n; i++)
1679 if (args[i])
1680 args[i] = args[i]->next;
1681
1682 for (i = 1; i < n; i++)
1683 if (rank[i] && ((args[i] != NULL && args[0] == NULL)
1684 || (args[i] == NULL && args[0] != NULL)))
1685 goto compliance;
1686 }
1687
1688 free_expr0 (e);
1689 *e = *expr;
1690 gfc_free_expr (old);
1691 return SUCCESS;
1692
1693 compliance:
1694 gfc_error_now ("elemental function arguments at %C are not compliant");
1695
1696 cleanup:
1697 gfc_free_expr (expr);
1698 gfc_free_expr (old);
1699 return FAILURE;
1700 }
1701
1702
1703 static try
1704 check_intrinsic_op (gfc_expr *e, try (*check_function) (gfc_expr *))
1705 {
1706 gfc_expr *op1 = e->value.op.op1;
1707 gfc_expr *op2 = e->value.op.op2;
1708
1709 if ((*check_function) (op1) == FAILURE)
1710 return FAILURE;
1711
1712 switch (e->value.op.operator)
1713 {
1714 case INTRINSIC_UPLUS:
1715 case INTRINSIC_UMINUS:
1716 if (!numeric_type (et0 (op1)))
1717 goto not_numeric;
1718 break;
1719
1720 case INTRINSIC_EQ:
1721 case INTRINSIC_NE:
1722 case INTRINSIC_GT:
1723 case INTRINSIC_GE:
1724 case INTRINSIC_LT:
1725 case INTRINSIC_LE:
1726 if ((*check_function) (op2) == FAILURE)
1727 return FAILURE;
1728
1729 if (!(et0 (op1) == BT_CHARACTER && et0 (op2) == BT_CHARACTER)
1730 && !(numeric_type (et0 (op1)) && numeric_type (et0 (op2))))
1731 {
1732 gfc_error ("Numeric or CHARACTER operands are required in "
1733 "expression at %L", &e->where);
1734 return FAILURE;
1735 }
1736 break;
1737
1738 case INTRINSIC_PLUS:
1739 case INTRINSIC_MINUS:
1740 case INTRINSIC_TIMES:
1741 case INTRINSIC_DIVIDE:
1742 case INTRINSIC_POWER:
1743 if ((*check_function) (op2) == FAILURE)
1744 return FAILURE;
1745
1746 if (!numeric_type (et0 (op1)) || !numeric_type (et0 (op2)))
1747 goto not_numeric;
1748
1749 if (e->value.op.operator == INTRINSIC_POWER
1750 && check_function == check_init_expr && et0 (op2) != BT_INTEGER)
1751 {
1752 if (gfc_notify_std (GFC_STD_F2003,"Fortran 2003: Noninteger "
1753 "exponent in an initialization "
1754 "expression at %L", &op2->where)
1755 == FAILURE)
1756 return FAILURE;
1757 }
1758
1759 break;
1760
1761 case INTRINSIC_CONCAT:
1762 if ((*check_function) (op2) == FAILURE)
1763 return FAILURE;
1764
1765 if (et0 (op1) != BT_CHARACTER || et0 (op2) != BT_CHARACTER)
1766 {
1767 gfc_error ("Concatenation operator in expression at %L "
1768 "must have two CHARACTER operands", &op1->where);
1769 return FAILURE;
1770 }
1771
1772 if (op1->ts.kind != op2->ts.kind)
1773 {
1774 gfc_error ("Concat operator at %L must concatenate strings of the "
1775 "same kind", &e->where);
1776 return FAILURE;
1777 }
1778
1779 break;
1780
1781 case INTRINSIC_NOT:
1782 if (et0 (op1) != BT_LOGICAL)
1783 {
1784 gfc_error (".NOT. operator in expression at %L must have a LOGICAL "
1785 "operand", &op1->where);
1786 return FAILURE;
1787 }
1788
1789 break;
1790
1791 case INTRINSIC_AND:
1792 case INTRINSIC_OR:
1793 case INTRINSIC_EQV:
1794 case INTRINSIC_NEQV:
1795 if ((*check_function) (op2) == FAILURE)
1796 return FAILURE;
1797
1798 if (et0 (op1) != BT_LOGICAL || et0 (op2) != BT_LOGICAL)
1799 {
1800 gfc_error ("LOGICAL operands are required in expression at %L",
1801 &e->where);
1802 return FAILURE;
1803 }
1804
1805 break;
1806
1807 case INTRINSIC_PARENTHESES:
1808 break;
1809
1810 default:
1811 gfc_error ("Only intrinsic operators can be used in expression at %L",
1812 &e->where);
1813 return FAILURE;
1814 }
1815
1816 return SUCCESS;
1817
1818 not_numeric:
1819 gfc_error ("Numeric operands are required in expression at %L", &e->where);
1820
1821 return FAILURE;
1822 }
1823
1824
1825
1826 /* Certain inquiry functions are specifically allowed to have variable
1827 arguments, which is an exception to the normal requirement that an
1828 initialization function have initialization arguments. We head off
1829 this problem here. */
1830
1831 static try
1832 check_inquiry (gfc_expr *e, int not_restricted)
1833 {
1834 const char *name;
1835
1836 /* FIXME: This should be moved into the intrinsic definitions,
1837 to eliminate this ugly hack. */
1838 static const char * const inquiry_function[] = {
1839 "digits", "epsilon", "huge", "kind", "len", "maxexponent", "minexponent",
1840 "precision", "radix", "range", "tiny", "bit_size", "size", "shape",
1841 "lbound", "ubound", NULL
1842 };
1843
1844 int i;
1845
1846 /* An undeclared parameter will get us here (PR25018). */
1847 if (e->symtree == NULL)
1848 return FAILURE;
1849
1850 name = e->symtree->n.sym->name;
1851
1852 for (i = 0; inquiry_function[i]; i++)
1853 if (strcmp (inquiry_function[i], name) == 0)
1854 break;
1855
1856 if (inquiry_function[i] == NULL)
1857 return FAILURE;
1858
1859 e = e->value.function.actual->expr;
1860
1861 if (e == NULL || e->expr_type != EXPR_VARIABLE)
1862 return FAILURE;
1863
1864 /* At this point we have an inquiry function with a variable argument. The
1865 type of the variable might be undefined, but we need it now, because the
1866 arguments of these functions are allowed to be undefined. */
1867
1868 if (e->ts.type == BT_UNKNOWN)
1869 {
1870 if (e->symtree->n.sym->ts.type == BT_UNKNOWN
1871 && gfc_set_default_type (e->symtree->n.sym, 0, gfc_current_ns)
1872 == FAILURE)
1873 return FAILURE;
1874
1875 e->ts = e->symtree->n.sym->ts;
1876 }
1877
1878 /* Assumed character length will not reduce to a constant expression
1879 with LEN, as required by the standard. */
1880 if (i == 4 && not_restricted
1881 && e->symtree->n.sym->ts.type == BT_CHARACTER
1882 && e->symtree->n.sym->ts.cl->length == NULL)
1883 gfc_notify_std (GFC_STD_GNU, "assumed character length "
1884 "variable '%s' in constant expression at %L",
1885 e->symtree->n.sym->name, &e->where);
1886
1887 return SUCCESS;
1888 }
1889
1890
1891 /* Verify that an expression is an initialization expression. A side
1892 effect is that the expression tree is reduced to a single constant
1893 node if all goes well. This would normally happen when the
1894 expression is constructed but function references are assumed to be
1895 intrinsics in the context of initialization expressions. If
1896 FAILURE is returned an error message has been generated. */
1897
1898 static try
1899 check_init_expr (gfc_expr *e)
1900 {
1901 gfc_actual_arglist *ap;
1902 match m;
1903 try t;
1904 gfc_intrinsic_sym *isym;
1905
1906 if (e == NULL)
1907 return SUCCESS;
1908
1909 switch (e->expr_type)
1910 {
1911 case EXPR_OP:
1912 t = check_intrinsic_op (e, check_init_expr);
1913 if (t == SUCCESS)
1914 t = gfc_simplify_expr (e, 0);
1915
1916 break;
1917
1918 case EXPR_FUNCTION:
1919 t = SUCCESS;
1920
1921 if (check_inquiry (e, 1) != SUCCESS)
1922 {
1923 t = SUCCESS;
1924 for (ap = e->value.function.actual; ap; ap = ap->next)
1925 if (check_init_expr (ap->expr) == FAILURE)
1926 {
1927 t = FAILURE;
1928 break;
1929 }
1930 }
1931
1932 /* Try to scalarize an elemental intrinsic function that has an
1933 array argument. */
1934 isym = gfc_find_function (e->symtree->n.sym->name);
1935 if (isym && isym->elemental
1936 && e->value.function.actual->expr->expr_type == EXPR_ARRAY)
1937 {
1938 if (scalarize_intrinsic_call (e) == SUCCESS)
1939 break;
1940 }
1941
1942 if (t == SUCCESS)
1943 {
1944 m = gfc_intrinsic_func_interface (e, 0);
1945
1946 if (m == MATCH_NO)
1947 gfc_error ("Function '%s' in initialization expression at %L "
1948 "must be an intrinsic function",
1949 e->symtree->n.sym->name, &e->where);
1950
1951 if (m != MATCH_YES)
1952 t = FAILURE;
1953 }
1954
1955 break;
1956
1957 case EXPR_VARIABLE:
1958 t = SUCCESS;
1959
1960 if (gfc_check_iter_variable (e) == SUCCESS)
1961 break;
1962
1963 if (e->symtree->n.sym->attr.flavor == FL_PARAMETER)
1964 {
1965 t = simplify_parameter_variable (e, 0);
1966 break;
1967 }
1968
1969 if (gfc_in_match_data ())
1970 break;
1971
1972 gfc_error ("Parameter '%s' at %L has not been declared or is "
1973 "a variable, which does not reduce to a constant "
1974 "expression", e->symtree->n.sym->name, &e->where);
1975 t = FAILURE;
1976 break;
1977
1978 case EXPR_CONSTANT:
1979 case EXPR_NULL:
1980 t = SUCCESS;
1981 break;
1982
1983 case EXPR_SUBSTRING:
1984 t = check_init_expr (e->ref->u.ss.start);
1985 if (t == FAILURE)
1986 break;
1987
1988 t = check_init_expr (e->ref->u.ss.end);
1989 if (t == SUCCESS)
1990 t = gfc_simplify_expr (e, 0);
1991
1992 break;
1993
1994 case EXPR_STRUCTURE:
1995 t = gfc_check_constructor (e, check_init_expr);
1996 break;
1997
1998 case EXPR_ARRAY:
1999 t = gfc_check_constructor (e, check_init_expr);
2000 if (t == FAILURE)
2001 break;
2002
2003 t = gfc_expand_constructor (e);
2004 if (t == FAILURE)
2005 break;
2006
2007 t = gfc_check_constructor_type (e);
2008 break;
2009
2010 default:
2011 gfc_internal_error ("check_init_expr(): Unknown expression type");
2012 }
2013
2014 return t;
2015 }
2016
2017
2018 /* Match an initialization expression. We work by first matching an
2019 expression, then reducing it to a constant. */
2020
2021 match
2022 gfc_match_init_expr (gfc_expr **result)
2023 {
2024 gfc_expr *expr;
2025 match m;
2026 try t;
2027
2028 m = gfc_match_expr (&expr);
2029 if (m != MATCH_YES)
2030 return m;
2031
2032 gfc_init_expr = 1;
2033 t = gfc_resolve_expr (expr);
2034 if (t == SUCCESS)
2035 t = check_init_expr (expr);
2036 gfc_init_expr = 0;
2037
2038 if (t == FAILURE)
2039 {
2040 gfc_free_expr (expr);
2041 return MATCH_ERROR;
2042 }
2043
2044 if (expr->expr_type == EXPR_ARRAY
2045 && (gfc_check_constructor_type (expr) == FAILURE
2046 || gfc_expand_constructor (expr) == FAILURE))
2047 {
2048 gfc_free_expr (expr);
2049 return MATCH_ERROR;
2050 }
2051
2052 /* Not all inquiry functions are simplified to constant expressions
2053 so it is necessary to call check_inquiry again. */
2054 if (!gfc_is_constant_expr (expr) && check_inquiry (expr, 1) == FAILURE
2055 && !gfc_in_match_data ())
2056 {
2057 gfc_error ("Initialization expression didn't reduce %C");
2058 return MATCH_ERROR;
2059 }
2060
2061 *result = expr;
2062
2063 return MATCH_YES;
2064 }
2065
2066
2067 static try check_restricted (gfc_expr *);
2068
2069 /* Given an actual argument list, test to see that each argument is a
2070 restricted expression and optionally if the expression type is
2071 integer or character. */
2072
2073 static try
2074 restricted_args (gfc_actual_arglist *a)
2075 {
2076 for (; a; a = a->next)
2077 {
2078 if (check_restricted (a->expr) == FAILURE)
2079 return FAILURE;
2080 }
2081
2082 return SUCCESS;
2083 }
2084
2085
2086 /************* Restricted/specification expressions *************/
2087
2088
2089 /* Make sure a non-intrinsic function is a specification function. */
2090
2091 static try
2092 external_spec_function (gfc_expr *e)
2093 {
2094 gfc_symbol *f;
2095
2096 f = e->value.function.esym;
2097
2098 if (f->attr.proc == PROC_ST_FUNCTION)
2099 {
2100 gfc_error ("Specification function '%s' at %L cannot be a statement "
2101 "function", f->name, &e->where);
2102 return FAILURE;
2103 }
2104
2105 if (f->attr.proc == PROC_INTERNAL)
2106 {
2107 gfc_error ("Specification function '%s' at %L cannot be an internal "
2108 "function", f->name, &e->where);
2109 return FAILURE;
2110 }
2111
2112 if (!f->attr.pure && !f->attr.elemental)
2113 {
2114 gfc_error ("Specification function '%s' at %L must be PURE", f->name,
2115 &e->where);
2116 return FAILURE;
2117 }
2118
2119 if (f->attr.recursive)
2120 {
2121 gfc_error ("Specification function '%s' at %L cannot be RECURSIVE",
2122 f->name, &e->where);
2123 return FAILURE;
2124 }
2125
2126 return restricted_args (e->value.function.actual);
2127 }
2128
2129
2130 /* Check to see that a function reference to an intrinsic is a
2131 restricted expression. */
2132
2133 static try
2134 restricted_intrinsic (gfc_expr *e)
2135 {
2136 /* TODO: Check constraints on inquiry functions. 7.1.6.2 (7). */
2137 if (check_inquiry (e, 0) == SUCCESS)
2138 return SUCCESS;
2139
2140 return restricted_args (e->value.function.actual);
2141 }
2142
2143
2144 /* Verify that an expression is a restricted expression. Like its
2145 cousin check_init_expr(), an error message is generated if we
2146 return FAILURE. */
2147
2148 static try
2149 check_restricted (gfc_expr *e)
2150 {
2151 gfc_symbol *sym;
2152 try t;
2153
2154 if (e == NULL)
2155 return SUCCESS;
2156
2157 switch (e->expr_type)
2158 {
2159 case EXPR_OP:
2160 t = check_intrinsic_op (e, check_restricted);
2161 if (t == SUCCESS)
2162 t = gfc_simplify_expr (e, 0);
2163
2164 break;
2165
2166 case EXPR_FUNCTION:
2167 t = e->value.function.esym ? external_spec_function (e)
2168 : restricted_intrinsic (e);
2169 break;
2170
2171 case EXPR_VARIABLE:
2172 sym = e->symtree->n.sym;
2173 t = FAILURE;
2174
2175 if (sym->attr.optional)
2176 {
2177 gfc_error ("Dummy argument '%s' at %L cannot be OPTIONAL",
2178 sym->name, &e->where);
2179 break;
2180 }
2181
2182 if (sym->attr.intent == INTENT_OUT)
2183 {
2184 gfc_error ("Dummy argument '%s' at %L cannot be INTENT(OUT)",
2185 sym->name, &e->where);
2186 break;
2187 }
2188
2189 /* gfc_is_formal_arg broadcasts that a formal argument list is being
2190 processed in resolve.c(resolve_formal_arglist). This is done so
2191 that host associated dummy array indices are accepted (PR23446).
2192 This mechanism also does the same for the specification expressions
2193 of array-valued functions. */
2194 if (sym->attr.in_common
2195 || sym->attr.use_assoc
2196 || sym->attr.dummy
2197 || sym->ns != gfc_current_ns
2198 || (sym->ns->proc_name != NULL
2199 && sym->ns->proc_name->attr.flavor == FL_MODULE)
2200 || (gfc_is_formal_arg () && (sym->ns == gfc_current_ns)))
2201 {
2202 t = SUCCESS;
2203 break;
2204 }
2205
2206 gfc_error ("Variable '%s' cannot appear in the expression at %L",
2207 sym->name, &e->where);
2208
2209 break;
2210
2211 case EXPR_NULL:
2212 case EXPR_CONSTANT:
2213 t = SUCCESS;
2214 break;
2215
2216 case EXPR_SUBSTRING:
2217 t = gfc_specification_expr (e->ref->u.ss.start);
2218 if (t == FAILURE)
2219 break;
2220
2221 t = gfc_specification_expr (e->ref->u.ss.end);
2222 if (t == SUCCESS)
2223 t = gfc_simplify_expr (e, 0);
2224
2225 break;
2226
2227 case EXPR_STRUCTURE:
2228 t = gfc_check_constructor (e, check_restricted);
2229 break;
2230
2231 case EXPR_ARRAY:
2232 t = gfc_check_constructor (e, check_restricted);
2233 break;
2234
2235 default:
2236 gfc_internal_error ("check_restricted(): Unknown expression type");
2237 }
2238
2239 return t;
2240 }
2241
2242
2243 /* Check to see that an expression is a specification expression. If
2244 we return FAILURE, an error has been generated. */
2245
2246 try
2247 gfc_specification_expr (gfc_expr *e)
2248 {
2249
2250 if (e == NULL)
2251 return SUCCESS;
2252
2253 if (e->ts.type != BT_INTEGER)
2254 {
2255 gfc_error ("Expression at %L must be of INTEGER type", &e->where);
2256 return FAILURE;
2257 }
2258
2259 if (e->rank != 0)
2260 {
2261 gfc_error ("Expression at %L must be scalar", &e->where);
2262 return FAILURE;
2263 }
2264
2265 if (gfc_simplify_expr (e, 0) == FAILURE)
2266 return FAILURE;
2267
2268 return check_restricted (e);
2269 }
2270
2271
2272 /************** Expression conformance checks. *************/
2273
2274 /* Given two expressions, make sure that the arrays are conformable. */
2275
2276 try
2277 gfc_check_conformance (const char *optype_msgid, gfc_expr *op1, gfc_expr *op2)
2278 {
2279 int op1_flag, op2_flag, d;
2280 mpz_t op1_size, op2_size;
2281 try t;
2282
2283 if (op1->rank == 0 || op2->rank == 0)
2284 return SUCCESS;
2285
2286 if (op1->rank != op2->rank)
2287 {
2288 gfc_error ("Incompatible ranks in %s at %L", _(optype_msgid),
2289 &op1->where);
2290 return FAILURE;
2291 }
2292
2293 t = SUCCESS;
2294
2295 for (d = 0; d < op1->rank; d++)
2296 {
2297 op1_flag = gfc_array_dimen_size (op1, d, &op1_size) == SUCCESS;
2298 op2_flag = gfc_array_dimen_size (op2, d, &op2_size) == SUCCESS;
2299
2300 if (op1_flag && op2_flag && mpz_cmp (op1_size, op2_size) != 0)
2301 {
2302 gfc_error ("different shape for %s at %L on dimension %d (%d/%d)",
2303 _(optype_msgid), &op1->where, d + 1,
2304 (int) mpz_get_si (op1_size),
2305 (int) mpz_get_si (op2_size));
2306
2307 t = FAILURE;
2308 }
2309
2310 if (op1_flag)
2311 mpz_clear (op1_size);
2312 if (op2_flag)
2313 mpz_clear (op2_size);
2314
2315 if (t == FAILURE)
2316 return FAILURE;
2317 }
2318
2319 return SUCCESS;
2320 }
2321
2322
2323 /* Given an assignable expression and an arbitrary expression, make
2324 sure that the assignment can take place. */
2325
2326 try
2327 gfc_check_assign (gfc_expr *lvalue, gfc_expr *rvalue, int conform)
2328 {
2329 gfc_symbol *sym;
2330 gfc_ref *ref;
2331 int has_pointer;
2332
2333 sym = lvalue->symtree->n.sym;
2334
2335 /* Check INTENT(IN), unless the object itself is the component or
2336 sub-component of a pointer. */
2337 has_pointer = sym->attr.pointer;
2338
2339 for (ref = lvalue->ref; ref; ref = ref->next)
2340 if (ref->type == REF_COMPONENT && ref->u.c.component->pointer)
2341 {
2342 has_pointer = 1;
2343 break;
2344 }
2345
2346 if (!has_pointer && sym->attr.intent == INTENT_IN)
2347 {
2348 gfc_error ("Cannot assign to INTENT(IN) variable '%s' at %L",
2349 sym->name, &lvalue->where);
2350 return FAILURE;
2351 }
2352
2353 /* 12.5.2.2, Note 12.26: The result variable is very similar to any other
2354 variable local to a function subprogram. Its existence begins when
2355 execution of the function is initiated and ends when execution of the
2356 function is terminated...
2357 Therefore, the left hand side is no longer a variable, when it is: */
2358 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_ST_FUNCTION
2359 && !sym->attr.external)
2360 {
2361 bool bad_proc;
2362 bad_proc = false;
2363
2364 /* (i) Use associated; */
2365 if (sym->attr.use_assoc)
2366 bad_proc = true;
2367
2368 /* (ii) The assignment is in the main program; or */
2369 if (gfc_current_ns->proc_name->attr.is_main_program)
2370 bad_proc = true;
2371
2372 /* (iii) A module or internal procedure... */
2373 if ((gfc_current_ns->proc_name->attr.proc == PROC_INTERNAL
2374 || gfc_current_ns->proc_name->attr.proc == PROC_MODULE)
2375 && gfc_current_ns->parent
2376 && (!(gfc_current_ns->parent->proc_name->attr.function
2377 || gfc_current_ns->parent->proc_name->attr.subroutine)
2378 || gfc_current_ns->parent->proc_name->attr.is_main_program))
2379 {
2380 /* ... that is not a function... */
2381 if (!gfc_current_ns->proc_name->attr.function)
2382 bad_proc = true;
2383
2384 /* ... or is not an entry and has a different name. */
2385 if (!sym->attr.entry && sym->name != gfc_current_ns->proc_name->name)
2386 bad_proc = true;
2387 }
2388
2389 if (bad_proc)
2390 {
2391 gfc_error ("'%s' at %L is not a VALUE", sym->name, &lvalue->where);
2392 return FAILURE;
2393 }
2394 }
2395
2396 if (rvalue->rank != 0 && lvalue->rank != rvalue->rank)
2397 {
2398 gfc_error ("Incompatible ranks %d and %d in assignment at %L",
2399 lvalue->rank, rvalue->rank, &lvalue->where);
2400 return FAILURE;
2401 }
2402
2403 if (lvalue->ts.type == BT_UNKNOWN)
2404 {
2405 gfc_error ("Variable type is UNKNOWN in assignment at %L",
2406 &lvalue->where);
2407 return FAILURE;
2408 }
2409
2410 if (rvalue->expr_type == EXPR_NULL)
2411 {
2412 if (lvalue->symtree->n.sym->attr.pointer
2413 && lvalue->symtree->n.sym->attr.data)
2414 return SUCCESS;
2415 else
2416 {
2417 gfc_error ("NULL appears on right-hand side in assignment at %L",
2418 &rvalue->where);
2419 return FAILURE;
2420 }
2421 }
2422
2423 if (sym->attr.cray_pointee
2424 && lvalue->ref != NULL
2425 && lvalue->ref->u.ar.type == AR_FULL
2426 && lvalue->ref->u.ar.as->cp_was_assumed)
2427 {
2428 gfc_error ("Vector assignment to assumed-size Cray Pointee at %L "
2429 "is illegal", &lvalue->where);
2430 return FAILURE;
2431 }
2432
2433 /* This is possibly a typo: x = f() instead of x => f(). */
2434 if (gfc_option.warn_surprising
2435 && rvalue->expr_type == EXPR_FUNCTION
2436 && rvalue->symtree->n.sym->attr.pointer)
2437 gfc_warning ("POINTER valued function appears on right-hand side of "
2438 "assignment at %L", &rvalue->where);
2439
2440 /* Check size of array assignments. */
2441 if (lvalue->rank != 0 && rvalue->rank != 0
2442 && gfc_check_conformance ("Array assignment", lvalue, rvalue) != SUCCESS)
2443 return FAILURE;
2444
2445 if (gfc_compare_types (&lvalue->ts, &rvalue->ts))
2446 return SUCCESS;
2447
2448 if (!conform)
2449 {
2450 /* Numeric can be converted to any other numeric. And Hollerith can be
2451 converted to any other type. */
2452 if ((gfc_numeric_ts (&lvalue->ts) && gfc_numeric_ts (&rvalue->ts))
2453 || rvalue->ts.type == BT_HOLLERITH)
2454 return SUCCESS;
2455
2456 if (lvalue->ts.type == BT_LOGICAL && rvalue->ts.type == BT_LOGICAL)
2457 return SUCCESS;
2458
2459 gfc_error ("Incompatible types in assignment at %L, %s to %s",
2460 &rvalue->where, gfc_typename (&rvalue->ts),
2461 gfc_typename (&lvalue->ts));
2462
2463 return FAILURE;
2464 }
2465
2466 return gfc_convert_type (rvalue, &lvalue->ts, 1);
2467 }
2468
2469
2470 /* Check that a pointer assignment is OK. We first check lvalue, and
2471 we only check rvalue if it's not an assignment to NULL() or a
2472 NULLIFY statement. */
2473
2474 try
2475 gfc_check_pointer_assign (gfc_expr *lvalue, gfc_expr *rvalue)
2476 {
2477 symbol_attribute attr;
2478 gfc_ref *ref;
2479 int is_pure;
2480 int pointer, check_intent_in;
2481
2482 if (lvalue->symtree->n.sym->ts.type == BT_UNKNOWN)
2483 {
2484 gfc_error ("Pointer assignment target is not a POINTER at %L",
2485 &lvalue->where);
2486 return FAILURE;
2487 }
2488
2489 if (lvalue->symtree->n.sym->attr.flavor == FL_PROCEDURE
2490 && lvalue->symtree->n.sym->attr.use_assoc)
2491 {
2492 gfc_error ("'%s' in the pointer assignment at %L cannot be an "
2493 "l-value since it is a procedure",
2494 lvalue->symtree->n.sym->name, &lvalue->where);
2495 return FAILURE;
2496 }
2497
2498
2499 /* Check INTENT(IN), unless the object itself is the component or
2500 sub-component of a pointer. */
2501 check_intent_in = 1;
2502 pointer = lvalue->symtree->n.sym->attr.pointer;
2503
2504 for (ref = lvalue->ref; ref; ref = ref->next)
2505 {
2506 if (pointer)
2507 check_intent_in = 0;
2508
2509 if (ref->type == REF_COMPONENT && ref->u.c.component->pointer)
2510 pointer = 1;
2511 }
2512
2513 if (check_intent_in && lvalue->symtree->n.sym->attr.intent == INTENT_IN)
2514 {
2515 gfc_error ("Cannot assign to INTENT(IN) variable '%s' at %L",
2516 lvalue->symtree->n.sym->name, &lvalue->where);
2517 return FAILURE;
2518 }
2519
2520 if (!pointer)
2521 {
2522 gfc_error ("Pointer assignment to non-POINTER at %L", &lvalue->where);
2523 return FAILURE;
2524 }
2525
2526 is_pure = gfc_pure (NULL);
2527
2528 if (is_pure && gfc_impure_variable (lvalue->symtree->n.sym))
2529 {
2530 gfc_error ("Bad pointer object in PURE procedure at %L", &lvalue->where);
2531 return FAILURE;
2532 }
2533
2534 /* If rvalue is a NULL() or NULLIFY, we're done. Otherwise the type,
2535 kind, etc for lvalue and rvalue must match, and rvalue must be a
2536 pure variable if we're in a pure function. */
2537 if (rvalue->expr_type == EXPR_NULL && rvalue->ts.type == BT_UNKNOWN)
2538 return SUCCESS;
2539
2540 if (!gfc_compare_types (&lvalue->ts, &rvalue->ts))
2541 {
2542 gfc_error ("Different types in pointer assignment at %L",
2543 &lvalue->where);
2544 return FAILURE;
2545 }
2546
2547 if (lvalue->ts.kind != rvalue->ts.kind)
2548 {
2549 gfc_error ("Different kind type parameters in pointer "
2550 "assignment at %L", &lvalue->where);
2551 return FAILURE;
2552 }
2553
2554 if (lvalue->rank != rvalue->rank)
2555 {
2556 gfc_error ("Different ranks in pointer assignment at %L",
2557 &lvalue->where);
2558 return FAILURE;
2559 }
2560
2561 /* Now punt if we are dealing with a NULLIFY(X) or X = NULL(X). */
2562 if (rvalue->expr_type == EXPR_NULL)
2563 return SUCCESS;
2564
2565 if (lvalue->ts.type == BT_CHARACTER
2566 && lvalue->ts.cl && rvalue->ts.cl
2567 && lvalue->ts.cl->length && rvalue->ts.cl->length
2568 && abs (gfc_dep_compare_expr (lvalue->ts.cl->length,
2569 rvalue->ts.cl->length)) == 1)
2570 {
2571 gfc_error ("Different character lengths in pointer "
2572 "assignment at %L", &lvalue->where);
2573 return FAILURE;
2574 }
2575
2576 attr = gfc_expr_attr (rvalue);
2577 if (!attr.target && !attr.pointer)
2578 {
2579 gfc_error ("Pointer assignment target is neither TARGET "
2580 "nor POINTER at %L", &rvalue->where);
2581 return FAILURE;
2582 }
2583
2584 if (is_pure && gfc_impure_variable (rvalue->symtree->n.sym))
2585 {
2586 gfc_error ("Bad target in pointer assignment in PURE "
2587 "procedure at %L", &rvalue->where);
2588 }
2589
2590 if (gfc_has_vector_index (rvalue))
2591 {
2592 gfc_error ("Pointer assignment with vector subscript "
2593 "on rhs at %L", &rvalue->where);
2594 return FAILURE;
2595 }
2596
2597 if (attr.protected && attr.use_assoc)
2598 {
2599 gfc_error ("Pointer assigment target has PROTECTED "
2600 "attribute at %L", &rvalue->where);
2601 return FAILURE;
2602 }
2603
2604 return SUCCESS;
2605 }
2606
2607
2608 /* Relative of gfc_check_assign() except that the lvalue is a single
2609 symbol. Used for initialization assignments. */
2610
2611 try
2612 gfc_check_assign_symbol (gfc_symbol *sym, gfc_expr *rvalue)
2613 {
2614 gfc_expr lvalue;
2615 try r;
2616
2617 memset (&lvalue, '\0', sizeof (gfc_expr));
2618
2619 lvalue.expr_type = EXPR_VARIABLE;
2620 lvalue.ts = sym->ts;
2621 if (sym->as)
2622 lvalue.rank = sym->as->rank;
2623 lvalue.symtree = (gfc_symtree *) gfc_getmem (sizeof (gfc_symtree));
2624 lvalue.symtree->n.sym = sym;
2625 lvalue.where = sym->declared_at;
2626
2627 if (sym->attr.pointer)
2628 r = gfc_check_pointer_assign (&lvalue, rvalue);
2629 else
2630 r = gfc_check_assign (&lvalue, rvalue, 1);
2631
2632 gfc_free (lvalue.symtree);
2633
2634 return r;
2635 }
2636
2637
2638 /* Get an expression for a default initializer. */
2639
2640 gfc_expr *
2641 gfc_default_initializer (gfc_typespec *ts)
2642 {
2643 gfc_constructor *tail;
2644 gfc_expr *init;
2645 gfc_component *c;
2646
2647 init = NULL;
2648
2649 /* See if we have a default initializer. */
2650 for (c = ts->derived->components; c; c = c->next)
2651 {
2652 if ((c->initializer || c->allocatable) && init == NULL)
2653 init = gfc_get_expr ();
2654 }
2655
2656 if (init == NULL)
2657 return NULL;
2658
2659 /* Build the constructor. */
2660 init->expr_type = EXPR_STRUCTURE;
2661 init->ts = *ts;
2662 init->where = ts->derived->declared_at;
2663 tail = NULL;
2664 for (c = ts->derived->components; c; c = c->next)
2665 {
2666 if (tail == NULL)
2667 init->value.constructor = tail = gfc_get_constructor ();
2668 else
2669 {
2670 tail->next = gfc_get_constructor ();
2671 tail = tail->next;
2672 }
2673
2674 if (c->initializer)
2675 tail->expr = gfc_copy_expr (c->initializer);
2676
2677 if (c->allocatable)
2678 {
2679 tail->expr = gfc_get_expr ();
2680 tail->expr->expr_type = EXPR_NULL;
2681 tail->expr->ts = c->ts;
2682 }
2683 }
2684 return init;
2685 }
2686
2687
2688 /* Given a symbol, create an expression node with that symbol as a
2689 variable. If the symbol is array valued, setup a reference of the
2690 whole array. */
2691
2692 gfc_expr *
2693 gfc_get_variable_expr (gfc_symtree *var)
2694 {
2695 gfc_expr *e;
2696
2697 e = gfc_get_expr ();
2698 e->expr_type = EXPR_VARIABLE;
2699 e->symtree = var;
2700 e->ts = var->n.sym->ts;
2701
2702 if (var->n.sym->as != NULL)
2703 {
2704 e->rank = var->n.sym->as->rank;
2705 e->ref = gfc_get_ref ();
2706 e->ref->type = REF_ARRAY;
2707 e->ref->u.ar.type = AR_FULL;
2708 }
2709
2710 return e;
2711 }
2712
2713
2714 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
2715
2716 void
2717 gfc_expr_set_symbols_referenced (gfc_expr *expr)
2718 {
2719 gfc_actual_arglist *arg;
2720 gfc_constructor *c;
2721 gfc_ref *ref;
2722 int i;
2723
2724 if (!expr) return;
2725
2726 switch (expr->expr_type)
2727 {
2728 case EXPR_OP:
2729 gfc_expr_set_symbols_referenced (expr->value.op.op1);
2730 gfc_expr_set_symbols_referenced (expr->value.op.op2);
2731 break;
2732
2733 case EXPR_FUNCTION:
2734 for (arg = expr->value.function.actual; arg; arg = arg->next)
2735 gfc_expr_set_symbols_referenced (arg->expr);
2736 break;
2737
2738 case EXPR_VARIABLE:
2739 gfc_set_sym_referenced (expr->symtree->n.sym);
2740 break;
2741
2742 case EXPR_CONSTANT:
2743 case EXPR_NULL:
2744 case EXPR_SUBSTRING:
2745 break;
2746
2747 case EXPR_STRUCTURE:
2748 case EXPR_ARRAY:
2749 for (c = expr->value.constructor; c; c = c->next)
2750 gfc_expr_set_symbols_referenced (c->expr);
2751 break;
2752
2753 default:
2754 gcc_unreachable ();
2755 break;
2756 }
2757
2758 for (ref = expr->ref; ref; ref = ref->next)
2759 switch (ref->type)
2760 {
2761 case REF_ARRAY:
2762 for (i = 0; i < ref->u.ar.dimen; i++)
2763 {
2764 gfc_expr_set_symbols_referenced (ref->u.ar.start[i]);
2765 gfc_expr_set_symbols_referenced (ref->u.ar.end[i]);
2766 gfc_expr_set_symbols_referenced (ref->u.ar.stride[i]);
2767 }
2768 break;
2769
2770 case REF_COMPONENT:
2771 break;
2772
2773 case REF_SUBSTRING:
2774 gfc_expr_set_symbols_referenced (ref->u.ss.start);
2775 gfc_expr_set_symbols_referenced (ref->u.ss.end);
2776 break;
2777
2778 default:
2779 gcc_unreachable ();
2780 break;
2781 }
2782 }