re PR fortran/32945 (ICE with initialization expressions)
[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 /* This is the case for the C_NULL_CHAR named constant. */
453 if (p->value.character.length == 0
454 && (p->ts.is_c_interop || p->ts.is_iso_c))
455 {
456 *s = '\0';
457 /* Need to set the length to 1 to make sure the NUL
458 terminator is copied. */
459 q->value.character.length = 1;
460 }
461 else
462 memcpy (s, p->value.character.string,
463 p->value.character.length + 1);
464 }
465 break;
466
467 case BT_HOLLERITH:
468 case BT_LOGICAL:
469 case BT_DERIVED:
470 break; /* Already done. */
471
472 case BT_PROCEDURE:
473 case BT_VOID:
474 /* Should never be reached. */
475 case BT_UNKNOWN:
476 gfc_internal_error ("gfc_copy_expr(): Bad expr node");
477 /* Not reached. */
478 }
479
480 break;
481
482 case EXPR_OP:
483 switch (q->value.op.operator)
484 {
485 case INTRINSIC_NOT:
486 case INTRINSIC_PARENTHESES:
487 case INTRINSIC_UPLUS:
488 case INTRINSIC_UMINUS:
489 q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
490 break;
491
492 default: /* Binary operators. */
493 q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
494 q->value.op.op2 = gfc_copy_expr (p->value.op.op2);
495 break;
496 }
497
498 break;
499
500 case EXPR_FUNCTION:
501 q->value.function.actual =
502 gfc_copy_actual_arglist (p->value.function.actual);
503 break;
504
505 case EXPR_STRUCTURE:
506 case EXPR_ARRAY:
507 q->value.constructor = gfc_copy_constructor (p->value.constructor);
508 break;
509
510 case EXPR_VARIABLE:
511 case EXPR_NULL:
512 break;
513 }
514
515 q->shape = gfc_copy_shape (p->shape, p->rank);
516
517 q->ref = copy_ref (p->ref);
518
519 return q;
520 }
521
522
523 /* Return the maximum kind of two expressions. In general, higher
524 kind numbers mean more precision for numeric types. */
525
526 int
527 gfc_kind_max (gfc_expr *e1, gfc_expr *e2)
528 {
529 return (e1->ts.kind > e2->ts.kind) ? e1->ts.kind : e2->ts.kind;
530 }
531
532
533 /* Returns nonzero if the type is numeric, zero otherwise. */
534
535 static int
536 numeric_type (bt type)
537 {
538 return type == BT_COMPLEX || type == BT_REAL || type == BT_INTEGER;
539 }
540
541
542 /* Returns nonzero if the typespec is a numeric type, zero otherwise. */
543
544 int
545 gfc_numeric_ts (gfc_typespec *ts)
546 {
547 return numeric_type (ts->type);
548 }
549
550
551 /* Returns an expression node that is an integer constant. */
552
553 gfc_expr *
554 gfc_int_expr (int i)
555 {
556 gfc_expr *p;
557
558 p = gfc_get_expr ();
559
560 p->expr_type = EXPR_CONSTANT;
561 p->ts.type = BT_INTEGER;
562 p->ts.kind = gfc_default_integer_kind;
563
564 p->where = gfc_current_locus;
565 mpz_init_set_si (p->value.integer, i);
566
567 return p;
568 }
569
570
571 /* Returns an expression node that is a logical constant. */
572
573 gfc_expr *
574 gfc_logical_expr (int i, locus *where)
575 {
576 gfc_expr *p;
577
578 p = gfc_get_expr ();
579
580 p->expr_type = EXPR_CONSTANT;
581 p->ts.type = BT_LOGICAL;
582 p->ts.kind = gfc_default_logical_kind;
583
584 if (where == NULL)
585 where = &gfc_current_locus;
586 p->where = *where;
587 p->value.logical = i;
588
589 return p;
590 }
591
592
593 /* Return an expression node with an optional argument list attached.
594 A variable number of gfc_expr pointers are strung together in an
595 argument list with a NULL pointer terminating the list. */
596
597 gfc_expr *
598 gfc_build_conversion (gfc_expr *e)
599 {
600 gfc_expr *p;
601
602 p = gfc_get_expr ();
603 p->expr_type = EXPR_FUNCTION;
604 p->symtree = NULL;
605 p->value.function.actual = NULL;
606
607 p->value.function.actual = gfc_get_actual_arglist ();
608 p->value.function.actual->expr = e;
609
610 return p;
611 }
612
613
614 /* Given an expression node with some sort of numeric binary
615 expression, insert type conversions required to make the operands
616 have the same type.
617
618 The exception is that the operands of an exponential don't have to
619 have the same type. If possible, the base is promoted to the type
620 of the exponent. For example, 1**2.3 becomes 1.0**2.3, but
621 1.0**2 stays as it is. */
622
623 void
624 gfc_type_convert_binary (gfc_expr *e)
625 {
626 gfc_expr *op1, *op2;
627
628 op1 = e->value.op.op1;
629 op2 = e->value.op.op2;
630
631 if (op1->ts.type == BT_UNKNOWN || op2->ts.type == BT_UNKNOWN)
632 {
633 gfc_clear_ts (&e->ts);
634 return;
635 }
636
637 /* Kind conversions of same type. */
638 if (op1->ts.type == op2->ts.type)
639 {
640 if (op1->ts.kind == op2->ts.kind)
641 {
642 /* No type conversions. */
643 e->ts = op1->ts;
644 goto done;
645 }
646
647 if (op1->ts.kind > op2->ts.kind)
648 gfc_convert_type (op2, &op1->ts, 2);
649 else
650 gfc_convert_type (op1, &op2->ts, 2);
651
652 e->ts = op1->ts;
653 goto done;
654 }
655
656 /* Integer combined with real or complex. */
657 if (op2->ts.type == BT_INTEGER)
658 {
659 e->ts = op1->ts;
660
661 /* Special case for ** operator. */
662 if (e->value.op.operator == INTRINSIC_POWER)
663 goto done;
664
665 gfc_convert_type (e->value.op.op2, &e->ts, 2);
666 goto done;
667 }
668
669 if (op1->ts.type == BT_INTEGER)
670 {
671 e->ts = op2->ts;
672 gfc_convert_type (e->value.op.op1, &e->ts, 2);
673 goto done;
674 }
675
676 /* Real combined with complex. */
677 e->ts.type = BT_COMPLEX;
678 if (op1->ts.kind > op2->ts.kind)
679 e->ts.kind = op1->ts.kind;
680 else
681 e->ts.kind = op2->ts.kind;
682 if (op1->ts.type != BT_COMPLEX || op1->ts.kind != e->ts.kind)
683 gfc_convert_type (e->value.op.op1, &e->ts, 2);
684 if (op2->ts.type != BT_COMPLEX || op2->ts.kind != e->ts.kind)
685 gfc_convert_type (e->value.op.op2, &e->ts, 2);
686
687 done:
688 return;
689 }
690
691
692 static match
693 check_specification_function (gfc_expr *e)
694 {
695 gfc_symbol *sym;
696
697 if (!e->symtree)
698 return MATCH_NO;
699
700 sym = e->symtree->n.sym;
701
702 /* F95, 7.1.6.2; F2003, 7.1.7 */
703 if (sym
704 && sym->attr.function
705 && sym->attr.pure
706 && !sym->attr.intrinsic
707 && !sym->attr.recursive
708 && sym->attr.proc != PROC_INTERNAL
709 && sym->attr.proc != PROC_ST_FUNCTION
710 && sym->attr.proc != PROC_UNKNOWN
711 && sym->formal == NULL)
712 return MATCH_YES;
713
714 return MATCH_NO;
715 }
716
717 /* Function to determine if an expression is constant or not. This
718 function expects that the expression has already been simplified. */
719
720 int
721 gfc_is_constant_expr (gfc_expr *e)
722 {
723 gfc_constructor *c;
724 gfc_actual_arglist *arg;
725 int rv;
726
727 if (e == NULL)
728 return 1;
729
730 switch (e->expr_type)
731 {
732 case EXPR_OP:
733 rv = (gfc_is_constant_expr (e->value.op.op1)
734 && (e->value.op.op2 == NULL
735 || gfc_is_constant_expr (e->value.op.op2)));
736 break;
737
738 case EXPR_VARIABLE:
739 rv = 0;
740 break;
741
742 case EXPR_FUNCTION:
743 /* Specification functions are constant. */
744 if (check_specification_function (e) == MATCH_YES)
745 {
746 rv = 1;
747 break;
748 }
749
750 /* Call to intrinsic with at least one argument. */
751 rv = 0;
752 if (e->value.function.isym && e->value.function.actual)
753 {
754 for (arg = e->value.function.actual; arg; arg = arg->next)
755 {
756 if (!gfc_is_constant_expr (arg->expr))
757 break;
758 }
759 if (arg == NULL)
760 rv = 1;
761 }
762 break;
763
764 case EXPR_CONSTANT:
765 case EXPR_NULL:
766 rv = 1;
767 break;
768
769 case EXPR_SUBSTRING:
770 rv = (gfc_is_constant_expr (e->ref->u.ss.start)
771 && gfc_is_constant_expr (e->ref->u.ss.end));
772 break;
773
774 case EXPR_STRUCTURE:
775 rv = 0;
776 for (c = e->value.constructor; c; c = c->next)
777 if (!gfc_is_constant_expr (c->expr))
778 break;
779
780 if (c == NULL)
781 rv = 1;
782 break;
783
784 case EXPR_ARRAY:
785 rv = gfc_constant_ac (e);
786 break;
787
788 default:
789 gfc_internal_error ("gfc_is_constant_expr(): Unknown expression type");
790 }
791
792 return rv;
793 }
794
795
796 /* Try to collapse intrinsic expressions. */
797
798 static try
799 simplify_intrinsic_op (gfc_expr *p, int type)
800 {
801 gfc_intrinsic_op op;
802 gfc_expr *op1, *op2, *result;
803
804 if (p->value.op.operator == INTRINSIC_USER)
805 return SUCCESS;
806
807 op1 = p->value.op.op1;
808 op2 = p->value.op.op2;
809 op = p->value.op.operator;
810
811 if (gfc_simplify_expr (op1, type) == FAILURE)
812 return FAILURE;
813 if (gfc_simplify_expr (op2, type) == FAILURE)
814 return FAILURE;
815
816 if (!gfc_is_constant_expr (op1)
817 || (op2 != NULL && !gfc_is_constant_expr (op2)))
818 return SUCCESS;
819
820 /* Rip p apart. */
821 p->value.op.op1 = NULL;
822 p->value.op.op2 = NULL;
823
824 switch (op)
825 {
826 case INTRINSIC_PARENTHESES:
827 result = gfc_parentheses (op1);
828 break;
829
830 case INTRINSIC_UPLUS:
831 result = gfc_uplus (op1);
832 break;
833
834 case INTRINSIC_UMINUS:
835 result = gfc_uminus (op1);
836 break;
837
838 case INTRINSIC_PLUS:
839 result = gfc_add (op1, op2);
840 break;
841
842 case INTRINSIC_MINUS:
843 result = gfc_subtract (op1, op2);
844 break;
845
846 case INTRINSIC_TIMES:
847 result = gfc_multiply (op1, op2);
848 break;
849
850 case INTRINSIC_DIVIDE:
851 result = gfc_divide (op1, op2);
852 break;
853
854 case INTRINSIC_POWER:
855 result = gfc_power (op1, op2);
856 break;
857
858 case INTRINSIC_CONCAT:
859 result = gfc_concat (op1, op2);
860 break;
861
862 case INTRINSIC_EQ:
863 case INTRINSIC_EQ_OS:
864 result = gfc_eq (op1, op2, op);
865 break;
866
867 case INTRINSIC_NE:
868 case INTRINSIC_NE_OS:
869 result = gfc_ne (op1, op2, op);
870 break;
871
872 case INTRINSIC_GT:
873 case INTRINSIC_GT_OS:
874 result = gfc_gt (op1, op2, op);
875 break;
876
877 case INTRINSIC_GE:
878 case INTRINSIC_GE_OS:
879 result = gfc_ge (op1, op2, op);
880 break;
881
882 case INTRINSIC_LT:
883 case INTRINSIC_LT_OS:
884 result = gfc_lt (op1, op2, op);
885 break;
886
887 case INTRINSIC_LE:
888 case INTRINSIC_LE_OS:
889 result = gfc_le (op1, op2, op);
890 break;
891
892 case INTRINSIC_NOT:
893 result = gfc_not (op1);
894 break;
895
896 case INTRINSIC_AND:
897 result = gfc_and (op1, op2);
898 break;
899
900 case INTRINSIC_OR:
901 result = gfc_or (op1, op2);
902 break;
903
904 case INTRINSIC_EQV:
905 result = gfc_eqv (op1, op2);
906 break;
907
908 case INTRINSIC_NEQV:
909 result = gfc_neqv (op1, op2);
910 break;
911
912 default:
913 gfc_internal_error ("simplify_intrinsic_op(): Bad operator");
914 }
915
916 if (result == NULL)
917 {
918 gfc_free_expr (op1);
919 gfc_free_expr (op2);
920 return FAILURE;
921 }
922
923 result->rank = p->rank;
924 result->where = p->where;
925 gfc_replace_expr (p, result);
926
927 return SUCCESS;
928 }
929
930
931 /* Subroutine to simplify constructor expressions. Mutually recursive
932 with gfc_simplify_expr(). */
933
934 static try
935 simplify_constructor (gfc_constructor *c, int type)
936 {
937 for (; c; c = c->next)
938 {
939 if (c->iterator
940 && (gfc_simplify_expr (c->iterator->start, type) == FAILURE
941 || gfc_simplify_expr (c->iterator->end, type) == FAILURE
942 || gfc_simplify_expr (c->iterator->step, type) == FAILURE))
943 return FAILURE;
944
945 if (c->expr && gfc_simplify_expr (c->expr, type) == FAILURE)
946 return FAILURE;
947 }
948
949 return SUCCESS;
950 }
951
952
953 /* Pull a single array element out of an array constructor. */
954
955 static try
956 find_array_element (gfc_constructor *cons, gfc_array_ref *ar,
957 gfc_constructor **rval)
958 {
959 unsigned long nelemen;
960 int i;
961 mpz_t delta;
962 mpz_t offset;
963 mpz_t span;
964 mpz_t tmp;
965 gfc_expr *e;
966 try t;
967
968 t = SUCCESS;
969 e = NULL;
970
971 mpz_init_set_ui (offset, 0);
972 mpz_init (delta);
973 mpz_init (tmp);
974 mpz_init_set_ui (span, 1);
975 for (i = 0; i < ar->dimen; i++)
976 {
977 e = gfc_copy_expr (ar->start[i]);
978 if (e->expr_type != EXPR_CONSTANT)
979 {
980 cons = NULL;
981 goto depart;
982 }
983
984 /* Check the bounds. */
985 if (ar->as->upper[i]
986 && (mpz_cmp (e->value.integer, ar->as->upper[i]->value.integer) > 0
987 || mpz_cmp (e->value.integer,
988 ar->as->lower[i]->value.integer) < 0))
989 {
990 gfc_error ("index in dimension %d is out of bounds "
991 "at %L", i + 1, &ar->c_where[i]);
992 cons = NULL;
993 t = FAILURE;
994 goto depart;
995 }
996
997 mpz_sub (delta, e->value.integer, ar->as->lower[i]->value.integer);
998 mpz_mul (delta, delta, span);
999 mpz_add (offset, offset, delta);
1000
1001 mpz_set_ui (tmp, 1);
1002 mpz_add (tmp, tmp, ar->as->upper[i]->value.integer);
1003 mpz_sub (tmp, tmp, ar->as->lower[i]->value.integer);
1004 mpz_mul (span, span, tmp);
1005 }
1006
1007 if (cons)
1008 {
1009 for (nelemen = mpz_get_ui (offset); nelemen > 0; nelemen--)
1010 {
1011 if (cons->iterator)
1012 {
1013 cons = NULL;
1014 goto depart;
1015 }
1016 cons = cons->next;
1017 }
1018 }
1019
1020 depart:
1021 mpz_clear (delta);
1022 mpz_clear (offset);
1023 mpz_clear (span);
1024 mpz_clear (tmp);
1025 if (e)
1026 gfc_free_expr (e);
1027 *rval = cons;
1028 return t;
1029 }
1030
1031
1032 /* Find a component of a structure constructor. */
1033
1034 static gfc_constructor *
1035 find_component_ref (gfc_constructor *cons, gfc_ref *ref)
1036 {
1037 gfc_component *comp;
1038 gfc_component *pick;
1039
1040 comp = ref->u.c.sym->components;
1041 pick = ref->u.c.component;
1042 while (comp != pick)
1043 {
1044 comp = comp->next;
1045 cons = cons->next;
1046 }
1047
1048 return cons;
1049 }
1050
1051
1052 /* Replace an expression with the contents of a constructor, removing
1053 the subobject reference in the process. */
1054
1055 static void
1056 remove_subobject_ref (gfc_expr *p, gfc_constructor *cons)
1057 {
1058 gfc_expr *e;
1059
1060 e = cons->expr;
1061 cons->expr = NULL;
1062 e->ref = p->ref->next;
1063 p->ref->next = NULL;
1064 gfc_replace_expr (p, e);
1065 }
1066
1067
1068 /* Pull an array section out of an array constructor. */
1069
1070 static try
1071 find_array_section (gfc_expr *expr, gfc_ref *ref)
1072 {
1073 int idx;
1074 int rank;
1075 int d;
1076 int shape_i;
1077 long unsigned one = 1;
1078 bool incr_ctr;
1079 mpz_t start[GFC_MAX_DIMENSIONS];
1080 mpz_t end[GFC_MAX_DIMENSIONS];
1081 mpz_t stride[GFC_MAX_DIMENSIONS];
1082 mpz_t delta[GFC_MAX_DIMENSIONS];
1083 mpz_t ctr[GFC_MAX_DIMENSIONS];
1084 mpz_t delta_mpz;
1085 mpz_t tmp_mpz;
1086 mpz_t nelts;
1087 mpz_t ptr;
1088 mpz_t index;
1089 gfc_constructor *cons;
1090 gfc_constructor *base;
1091 gfc_expr *begin;
1092 gfc_expr *finish;
1093 gfc_expr *step;
1094 gfc_expr *upper;
1095 gfc_expr *lower;
1096 gfc_constructor *vecsub[GFC_MAX_DIMENSIONS], *c;
1097 try t;
1098
1099 t = SUCCESS;
1100
1101 base = expr->value.constructor;
1102 expr->value.constructor = NULL;
1103
1104 rank = ref->u.ar.as->rank;
1105
1106 if (expr->shape == NULL)
1107 expr->shape = gfc_get_shape (rank);
1108
1109 mpz_init_set_ui (delta_mpz, one);
1110 mpz_init_set_ui (nelts, one);
1111 mpz_init (tmp_mpz);
1112
1113 /* Do the initialization now, so that we can cleanup without
1114 keeping track of where we were. */
1115 for (d = 0; d < rank; d++)
1116 {
1117 mpz_init (delta[d]);
1118 mpz_init (start[d]);
1119 mpz_init (end[d]);
1120 mpz_init (ctr[d]);
1121 mpz_init (stride[d]);
1122 vecsub[d] = NULL;
1123 }
1124
1125 /* Build the counters to clock through the array reference. */
1126 shape_i = 0;
1127 for (d = 0; d < rank; d++)
1128 {
1129 /* Make this stretch of code easier on the eye! */
1130 begin = ref->u.ar.start[d];
1131 finish = ref->u.ar.end[d];
1132 step = ref->u.ar.stride[d];
1133 lower = ref->u.ar.as->lower[d];
1134 upper = ref->u.ar.as->upper[d];
1135
1136 if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR) /* Vector subscript. */
1137 {
1138 gcc_assert (begin);
1139
1140 if (begin->expr_type != EXPR_ARRAY)
1141 {
1142 t = FAILURE;
1143 goto cleanup;
1144 }
1145
1146 gcc_assert (begin->rank == 1);
1147 gcc_assert (begin->shape);
1148
1149 vecsub[d] = begin->value.constructor;
1150 mpz_set (ctr[d], vecsub[d]->expr->value.integer);
1151 mpz_mul (nelts, nelts, begin->shape[0]);
1152 mpz_set (expr->shape[shape_i++], begin->shape[0]);
1153
1154 /* Check bounds. */
1155 for (c = vecsub[d]; c; c = c->next)
1156 {
1157 if (mpz_cmp (c->expr->value.integer, upper->value.integer) > 0
1158 || mpz_cmp (c->expr->value.integer,
1159 lower->value.integer) < 0)
1160 {
1161 gfc_error ("index in dimension %d is out of bounds "
1162 "at %L", d + 1, &ref->u.ar.c_where[d]);
1163 t = FAILURE;
1164 goto cleanup;
1165 }
1166 }
1167 }
1168 else
1169 {
1170 if ((begin && begin->expr_type != EXPR_CONSTANT)
1171 || (finish && finish->expr_type != EXPR_CONSTANT)
1172 || (step && step->expr_type != EXPR_CONSTANT))
1173 {
1174 t = FAILURE;
1175 goto cleanup;
1176 }
1177
1178 /* Obtain the stride. */
1179 if (step)
1180 mpz_set (stride[d], step->value.integer);
1181 else
1182 mpz_set_ui (stride[d], one);
1183
1184 if (mpz_cmp_ui (stride[d], 0) == 0)
1185 mpz_set_ui (stride[d], one);
1186
1187 /* Obtain the start value for the index. */
1188 if (begin)
1189 mpz_set (start[d], begin->value.integer);
1190 else
1191 mpz_set (start[d], lower->value.integer);
1192
1193 mpz_set (ctr[d], start[d]);
1194
1195 /* Obtain the end value for the index. */
1196 if (finish)
1197 mpz_set (end[d], finish->value.integer);
1198 else
1199 mpz_set (end[d], upper->value.integer);
1200
1201 /* Separate 'if' because elements sometimes arrive with
1202 non-null end. */
1203 if (ref->u.ar.dimen_type[d] == DIMEN_ELEMENT)
1204 mpz_set (end [d], begin->value.integer);
1205
1206 /* Check the bounds. */
1207 if (mpz_cmp (ctr[d], upper->value.integer) > 0
1208 || mpz_cmp (end[d], upper->value.integer) > 0
1209 || mpz_cmp (ctr[d], lower->value.integer) < 0
1210 || mpz_cmp (end[d], lower->value.integer) < 0)
1211 {
1212 gfc_error ("index in dimension %d is out of bounds "
1213 "at %L", d + 1, &ref->u.ar.c_where[d]);
1214 t = FAILURE;
1215 goto cleanup;
1216 }
1217
1218 /* Calculate the number of elements and the shape. */
1219 mpz_set (tmp_mpz, stride[d]);
1220 mpz_add (tmp_mpz, end[d], tmp_mpz);
1221 mpz_sub (tmp_mpz, tmp_mpz, ctr[d]);
1222 mpz_div (tmp_mpz, tmp_mpz, stride[d]);
1223 mpz_mul (nelts, nelts, tmp_mpz);
1224
1225 /* An element reference reduces the rank of the expression; don't
1226 add anything to the shape array. */
1227 if (ref->u.ar.dimen_type[d] != DIMEN_ELEMENT)
1228 mpz_set (expr->shape[shape_i++], tmp_mpz);
1229 }
1230
1231 /* Calculate the 'stride' (=delta) for conversion of the
1232 counter values into the index along the constructor. */
1233 mpz_set (delta[d], delta_mpz);
1234 mpz_sub (tmp_mpz, upper->value.integer, lower->value.integer);
1235 mpz_add_ui (tmp_mpz, tmp_mpz, one);
1236 mpz_mul (delta_mpz, delta_mpz, tmp_mpz);
1237 }
1238
1239 mpz_init (index);
1240 mpz_init (ptr);
1241 cons = base;
1242
1243 /* Now clock through the array reference, calculating the index in
1244 the source constructor and transferring the elements to the new
1245 constructor. */
1246 for (idx = 0; idx < (int) mpz_get_si (nelts); idx++)
1247 {
1248 if (ref->u.ar.offset)
1249 mpz_set (ptr, ref->u.ar.offset->value.integer);
1250 else
1251 mpz_init_set_ui (ptr, 0);
1252
1253 incr_ctr = true;
1254 for (d = 0; d < rank; d++)
1255 {
1256 mpz_set (tmp_mpz, ctr[d]);
1257 mpz_sub (tmp_mpz, tmp_mpz, ref->u.ar.as->lower[d]->value.integer);
1258 mpz_mul (tmp_mpz, tmp_mpz, delta[d]);
1259 mpz_add (ptr, ptr, tmp_mpz);
1260
1261 if (!incr_ctr) continue;
1262
1263 if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR) /* Vector subscript. */
1264 {
1265 gcc_assert(vecsub[d]);
1266
1267 if (!vecsub[d]->next)
1268 vecsub[d] = ref->u.ar.start[d]->value.constructor;
1269 else
1270 {
1271 vecsub[d] = vecsub[d]->next;
1272 incr_ctr = false;
1273 }
1274 mpz_set (ctr[d], vecsub[d]->expr->value.integer);
1275 }
1276 else
1277 {
1278 mpz_add (ctr[d], ctr[d], stride[d]);
1279
1280 if (mpz_cmp_ui (stride[d], 0) > 0
1281 ? mpz_cmp (ctr[d], end[d]) > 0
1282 : mpz_cmp (ctr[d], end[d]) < 0)
1283 mpz_set (ctr[d], start[d]);
1284 else
1285 incr_ctr = false;
1286 }
1287 }
1288
1289 /* There must be a better way of dealing with negative strides
1290 than resetting the index and the constructor pointer! */
1291 if (mpz_cmp (ptr, index) < 0)
1292 {
1293 mpz_set_ui (index, 0);
1294 cons = base;
1295 }
1296
1297 while (mpz_cmp (ptr, index) > 0)
1298 {
1299 mpz_add_ui (index, index, one);
1300 cons = cons->next;
1301 }
1302
1303 gfc_append_constructor (expr, gfc_copy_expr (cons->expr));
1304 }
1305
1306 mpz_clear (ptr);
1307 mpz_clear (index);
1308
1309 cleanup:
1310
1311 mpz_clear (delta_mpz);
1312 mpz_clear (tmp_mpz);
1313 mpz_clear (nelts);
1314 for (d = 0; d < rank; d++)
1315 {
1316 mpz_clear (delta[d]);
1317 mpz_clear (start[d]);
1318 mpz_clear (end[d]);
1319 mpz_clear (ctr[d]);
1320 mpz_clear (stride[d]);
1321 }
1322 gfc_free_constructor (base);
1323 return t;
1324 }
1325
1326 /* Pull a substring out of an expression. */
1327
1328 static try
1329 find_substring_ref (gfc_expr *p, gfc_expr **newp)
1330 {
1331 int end;
1332 int start;
1333 char *chr;
1334
1335 if (p->ref->u.ss.start->expr_type != EXPR_CONSTANT
1336 || p->ref->u.ss.end->expr_type != EXPR_CONSTANT)
1337 return FAILURE;
1338
1339 *newp = gfc_copy_expr (p);
1340 chr = p->value.character.string;
1341 end = (int) mpz_get_ui (p->ref->u.ss.end->value.integer);
1342 start = (int) mpz_get_ui (p->ref->u.ss.start->value.integer);
1343
1344 (*newp)->value.character.length = end - start + 1;
1345 strncpy ((*newp)->value.character.string, &chr[start - 1],
1346 (*newp)->value.character.length);
1347 return SUCCESS;
1348 }
1349
1350
1351
1352 /* Simplify a subobject reference of a constructor. This occurs when
1353 parameter variable values are substituted. */
1354
1355 static try
1356 simplify_const_ref (gfc_expr *p)
1357 {
1358 gfc_constructor *cons;
1359 gfc_expr *newp;
1360
1361 while (p->ref)
1362 {
1363 switch (p->ref->type)
1364 {
1365 case REF_ARRAY:
1366 switch (p->ref->u.ar.type)
1367 {
1368 case AR_ELEMENT:
1369 if (find_array_element (p->value.constructor, &p->ref->u.ar,
1370 &cons) == FAILURE)
1371 return FAILURE;
1372
1373 if (!cons)
1374 return SUCCESS;
1375
1376 remove_subobject_ref (p, cons);
1377 break;
1378
1379 case AR_SECTION:
1380 if (find_array_section (p, p->ref) == FAILURE)
1381 return FAILURE;
1382 p->ref->u.ar.type = AR_FULL;
1383
1384 /* Fall through. */
1385
1386 case AR_FULL:
1387 if (p->ref->next != NULL
1388 && (p->ts.type == BT_CHARACTER || p->ts.type == BT_DERIVED))
1389 {
1390 cons = p->value.constructor;
1391 for (; cons; cons = cons->next)
1392 {
1393 cons->expr->ref = copy_ref (p->ref->next);
1394 simplify_const_ref (cons->expr);
1395 }
1396 }
1397 gfc_free_ref_list (p->ref);
1398 p->ref = NULL;
1399 break;
1400
1401 default:
1402 return SUCCESS;
1403 }
1404
1405 break;
1406
1407 case REF_COMPONENT:
1408 cons = find_component_ref (p->value.constructor, p->ref);
1409 remove_subobject_ref (p, cons);
1410 break;
1411
1412 case REF_SUBSTRING:
1413 if (find_substring_ref (p, &newp) == FAILURE)
1414 return FAILURE;
1415
1416 gfc_replace_expr (p, newp);
1417 gfc_free_ref_list (p->ref);
1418 p->ref = NULL;
1419 break;
1420 }
1421 }
1422
1423 return SUCCESS;
1424 }
1425
1426
1427 /* Simplify a chain of references. */
1428
1429 static try
1430 simplify_ref_chain (gfc_ref *ref, int type)
1431 {
1432 int n;
1433
1434 for (; ref; ref = ref->next)
1435 {
1436 switch (ref->type)
1437 {
1438 case REF_ARRAY:
1439 for (n = 0; n < ref->u.ar.dimen; n++)
1440 {
1441 if (gfc_simplify_expr (ref->u.ar.start[n], type) == FAILURE)
1442 return FAILURE;
1443 if (gfc_simplify_expr (ref->u.ar.end[n], type) == FAILURE)
1444 return FAILURE;
1445 if (gfc_simplify_expr (ref->u.ar.stride[n], type) == FAILURE)
1446 return FAILURE;
1447 }
1448 break;
1449
1450 case REF_SUBSTRING:
1451 if (gfc_simplify_expr (ref->u.ss.start, type) == FAILURE)
1452 return FAILURE;
1453 if (gfc_simplify_expr (ref->u.ss.end, type) == FAILURE)
1454 return FAILURE;
1455 break;
1456
1457 default:
1458 break;
1459 }
1460 }
1461 return SUCCESS;
1462 }
1463
1464
1465 /* Try to substitute the value of a parameter variable. */
1466
1467 static try
1468 simplify_parameter_variable (gfc_expr *p, int type)
1469 {
1470 gfc_expr *e;
1471 try t;
1472
1473 e = gfc_copy_expr (p->symtree->n.sym->value);
1474 if (e == NULL)
1475 return FAILURE;
1476
1477 e->rank = p->rank;
1478
1479 /* Do not copy subobject refs for constant. */
1480 if (e->expr_type != EXPR_CONSTANT && p->ref != NULL)
1481 e->ref = copy_ref (p->ref);
1482 t = gfc_simplify_expr (e, type);
1483
1484 /* Only use the simplification if it eliminated all subobject references. */
1485 if (t == SUCCESS && !e->ref)
1486 gfc_replace_expr (p, e);
1487 else
1488 gfc_free_expr (e);
1489
1490 return t;
1491 }
1492
1493 /* Given an expression, simplify it by collapsing constant
1494 expressions. Most simplification takes place when the expression
1495 tree is being constructed. If an intrinsic function is simplified
1496 at some point, we get called again to collapse the result against
1497 other constants.
1498
1499 We work by recursively simplifying expression nodes, simplifying
1500 intrinsic functions where possible, which can lead to further
1501 constant collapsing. If an operator has constant operand(s), we
1502 rip the expression apart, and rebuild it, hoping that it becomes
1503 something simpler.
1504
1505 The expression type is defined for:
1506 0 Basic expression parsing
1507 1 Simplifying array constructors -- will substitute
1508 iterator values.
1509 Returns FAILURE on error, SUCCESS otherwise.
1510 NOTE: Will return SUCCESS even if the expression can not be simplified. */
1511
1512 try
1513 gfc_simplify_expr (gfc_expr *p, int type)
1514 {
1515 gfc_actual_arglist *ap;
1516
1517 if (p == NULL)
1518 return SUCCESS;
1519
1520 switch (p->expr_type)
1521 {
1522 case EXPR_CONSTANT:
1523 case EXPR_NULL:
1524 break;
1525
1526 case EXPR_FUNCTION:
1527 for (ap = p->value.function.actual; ap; ap = ap->next)
1528 if (gfc_simplify_expr (ap->expr, type) == FAILURE)
1529 return FAILURE;
1530
1531 if (p->value.function.isym != NULL
1532 && gfc_intrinsic_func_interface (p, 1) == MATCH_ERROR)
1533 return FAILURE;
1534
1535 break;
1536
1537 case EXPR_SUBSTRING:
1538 if (simplify_ref_chain (p->ref, type) == FAILURE)
1539 return FAILURE;
1540
1541 if (gfc_is_constant_expr (p))
1542 {
1543 char *s;
1544 int start, end;
1545
1546 gfc_extract_int (p->ref->u.ss.start, &start);
1547 start--; /* Convert from one-based to zero-based. */
1548 gfc_extract_int (p->ref->u.ss.end, &end);
1549 s = gfc_getmem (end - start + 2);
1550 memcpy (s, p->value.character.string + start, end - start);
1551 s[end - start + 1] = '\0'; /* TODO: C-style string. */
1552 gfc_free (p->value.character.string);
1553 p->value.character.string = s;
1554 p->value.character.length = end - start;
1555 p->ts.cl = gfc_get_charlen ();
1556 p->ts.cl->next = gfc_current_ns->cl_list;
1557 gfc_current_ns->cl_list = p->ts.cl;
1558 p->ts.cl->length = gfc_int_expr (p->value.character.length);
1559 gfc_free_ref_list (p->ref);
1560 p->ref = NULL;
1561 p->expr_type = EXPR_CONSTANT;
1562 }
1563 break;
1564
1565 case EXPR_OP:
1566 if (simplify_intrinsic_op (p, type) == FAILURE)
1567 return FAILURE;
1568 break;
1569
1570 case EXPR_VARIABLE:
1571 /* Only substitute array parameter variables if we are in an
1572 initialization expression, or we want a subsection. */
1573 if (p->symtree->n.sym->attr.flavor == FL_PARAMETER
1574 && (gfc_init_expr || p->ref
1575 || p->symtree->n.sym->value->expr_type != EXPR_ARRAY))
1576 {
1577 if (simplify_parameter_variable (p, type) == FAILURE)
1578 return FAILURE;
1579 break;
1580 }
1581
1582 if (type == 1)
1583 {
1584 gfc_simplify_iterator_var (p);
1585 }
1586
1587 /* Simplify subcomponent references. */
1588 if (simplify_ref_chain (p->ref, type) == FAILURE)
1589 return FAILURE;
1590
1591 break;
1592
1593 case EXPR_STRUCTURE:
1594 case EXPR_ARRAY:
1595 if (simplify_ref_chain (p->ref, type) == FAILURE)
1596 return FAILURE;
1597
1598 if (simplify_constructor (p->value.constructor, type) == FAILURE)
1599 return FAILURE;
1600
1601 if (p->expr_type == EXPR_ARRAY && p->ref && p->ref->type == REF_ARRAY
1602 && p->ref->u.ar.type == AR_FULL)
1603 gfc_expand_constructor (p);
1604
1605 if (simplify_const_ref (p) == FAILURE)
1606 return FAILURE;
1607
1608 break;
1609 }
1610
1611 return SUCCESS;
1612 }
1613
1614
1615 /* Returns the type of an expression with the exception that iterator
1616 variables are automatically integers no matter what else they may
1617 be declared as. */
1618
1619 static bt
1620 et0 (gfc_expr *e)
1621 {
1622 if (e->expr_type == EXPR_VARIABLE && gfc_check_iter_variable (e) == SUCCESS)
1623 return BT_INTEGER;
1624
1625 return e->ts.type;
1626 }
1627
1628
1629 /* Check an intrinsic arithmetic operation to see if it is consistent
1630 with some type of expression. */
1631
1632 static try check_init_expr (gfc_expr *);
1633
1634
1635 /* Scalarize an expression for an elemental intrinsic call. */
1636
1637 static try
1638 scalarize_intrinsic_call (gfc_expr *e)
1639 {
1640 gfc_actual_arglist *a, *b;
1641 gfc_constructor *args[5], *ctor, *new_ctor;
1642 gfc_expr *expr, *old;
1643 int n, i, rank[5];
1644
1645 old = gfc_copy_expr (e);
1646
1647 /* Assume that the old expression carries the type information and
1648 that the first arg carries all the shape information. */
1649 expr = gfc_copy_expr (old->value.function.actual->expr);
1650 gfc_free_constructor (expr->value.constructor);
1651 expr->value.constructor = NULL;
1652
1653 expr->ts = old->ts;
1654 expr->expr_type = EXPR_ARRAY;
1655
1656 /* Copy the array argument constructors into an array, with nulls
1657 for the scalars. */
1658 n = 0;
1659 a = old->value.function.actual;
1660 for (; a; a = a->next)
1661 {
1662 /* Check that this is OK for an initialization expression. */
1663 if (a->expr && check_init_expr (a->expr) == FAILURE)
1664 goto cleanup;
1665
1666 rank[n] = 0;
1667 if (a->expr && a->expr->rank && a->expr->expr_type == EXPR_VARIABLE)
1668 {
1669 rank[n] = a->expr->rank;
1670 ctor = a->expr->symtree->n.sym->value->value.constructor;
1671 args[n] = gfc_copy_constructor (ctor);
1672 }
1673 else if (a->expr && a->expr->expr_type == EXPR_ARRAY)
1674 {
1675 if (a->expr->rank)
1676 rank[n] = a->expr->rank;
1677 else
1678 rank[n] = 1;
1679 args[n] = gfc_copy_constructor (a->expr->value.constructor);
1680 }
1681 else
1682 args[n] = NULL;
1683 n++;
1684 }
1685
1686 for (i = 1; i < n; i++)
1687 if (rank[i] && rank[i] != rank[0])
1688 goto compliance;
1689
1690 /* Using the first argument as the master, step through the array
1691 calling the function for each element and advancing the array
1692 constructors together. */
1693 ctor = args[0];
1694 new_ctor = NULL;
1695 for (; ctor; ctor = ctor->next)
1696 {
1697 if (expr->value.constructor == NULL)
1698 expr->value.constructor
1699 = new_ctor = gfc_get_constructor ();
1700 else
1701 {
1702 new_ctor->next = gfc_get_constructor ();
1703 new_ctor = new_ctor->next;
1704 }
1705 new_ctor->expr = gfc_copy_expr (old);
1706 gfc_free_actual_arglist (new_ctor->expr->value.function.actual);
1707 a = NULL;
1708 b = old->value.function.actual;
1709 for (i = 0; i < n; i++)
1710 {
1711 if (a == NULL)
1712 new_ctor->expr->value.function.actual
1713 = a = gfc_get_actual_arglist ();
1714 else
1715 {
1716 a->next = gfc_get_actual_arglist ();
1717 a = a->next;
1718 }
1719 if (args[i])
1720 a->expr = gfc_copy_expr (args[i]->expr);
1721 else
1722 a->expr = gfc_copy_expr (b->expr);
1723
1724 b = b->next;
1725 }
1726
1727 /* Simplify the function calls. */
1728 if (gfc_simplify_expr (new_ctor->expr, 0) == FAILURE)
1729 goto cleanup;
1730
1731 for (i = 0; i < n; i++)
1732 if (args[i])
1733 args[i] = args[i]->next;
1734
1735 for (i = 1; i < n; i++)
1736 if (rank[i] && ((args[i] != NULL && args[0] == NULL)
1737 || (args[i] == NULL && args[0] != NULL)))
1738 goto compliance;
1739 }
1740
1741 free_expr0 (e);
1742 *e = *expr;
1743 gfc_free_expr (old);
1744 return SUCCESS;
1745
1746 compliance:
1747 gfc_error_now ("elemental function arguments at %C are not compliant");
1748
1749 cleanup:
1750 gfc_free_expr (expr);
1751 gfc_free_expr (old);
1752 return FAILURE;
1753 }
1754
1755
1756 static try
1757 check_intrinsic_op (gfc_expr *e, try (*check_function) (gfc_expr *))
1758 {
1759 gfc_expr *op1 = e->value.op.op1;
1760 gfc_expr *op2 = e->value.op.op2;
1761
1762 if ((*check_function) (op1) == FAILURE)
1763 return FAILURE;
1764
1765 switch (e->value.op.operator)
1766 {
1767 case INTRINSIC_UPLUS:
1768 case INTRINSIC_UMINUS:
1769 if (!numeric_type (et0 (op1)))
1770 goto not_numeric;
1771 break;
1772
1773 case INTRINSIC_EQ:
1774 case INTRINSIC_EQ_OS:
1775 case INTRINSIC_NE:
1776 case INTRINSIC_NE_OS:
1777 case INTRINSIC_GT:
1778 case INTRINSIC_GT_OS:
1779 case INTRINSIC_GE:
1780 case INTRINSIC_GE_OS:
1781 case INTRINSIC_LT:
1782 case INTRINSIC_LT_OS:
1783 case INTRINSIC_LE:
1784 case INTRINSIC_LE_OS:
1785 if ((*check_function) (op2) == FAILURE)
1786 return FAILURE;
1787
1788 if (!(et0 (op1) == BT_CHARACTER && et0 (op2) == BT_CHARACTER)
1789 && !(numeric_type (et0 (op1)) && numeric_type (et0 (op2))))
1790 {
1791 gfc_error ("Numeric or CHARACTER operands are required in "
1792 "expression at %L", &e->where);
1793 return FAILURE;
1794 }
1795 break;
1796
1797 case INTRINSIC_PLUS:
1798 case INTRINSIC_MINUS:
1799 case INTRINSIC_TIMES:
1800 case INTRINSIC_DIVIDE:
1801 case INTRINSIC_POWER:
1802 if ((*check_function) (op2) == FAILURE)
1803 return FAILURE;
1804
1805 if (!numeric_type (et0 (op1)) || !numeric_type (et0 (op2)))
1806 goto not_numeric;
1807
1808 if (e->value.op.operator == INTRINSIC_POWER
1809 && check_function == check_init_expr && et0 (op2) != BT_INTEGER)
1810 {
1811 if (gfc_notify_std (GFC_STD_F2003,"Fortran 2003: Noninteger "
1812 "exponent in an initialization "
1813 "expression at %L", &op2->where)
1814 == FAILURE)
1815 return FAILURE;
1816 }
1817
1818 break;
1819
1820 case INTRINSIC_CONCAT:
1821 if ((*check_function) (op2) == FAILURE)
1822 return FAILURE;
1823
1824 if (et0 (op1) != BT_CHARACTER || et0 (op2) != BT_CHARACTER)
1825 {
1826 gfc_error ("Concatenation operator in expression at %L "
1827 "must have two CHARACTER operands", &op1->where);
1828 return FAILURE;
1829 }
1830
1831 if (op1->ts.kind != op2->ts.kind)
1832 {
1833 gfc_error ("Concat operator at %L must concatenate strings of the "
1834 "same kind", &e->where);
1835 return FAILURE;
1836 }
1837
1838 break;
1839
1840 case INTRINSIC_NOT:
1841 if (et0 (op1) != BT_LOGICAL)
1842 {
1843 gfc_error (".NOT. operator in expression at %L must have a LOGICAL "
1844 "operand", &op1->where);
1845 return FAILURE;
1846 }
1847
1848 break;
1849
1850 case INTRINSIC_AND:
1851 case INTRINSIC_OR:
1852 case INTRINSIC_EQV:
1853 case INTRINSIC_NEQV:
1854 if ((*check_function) (op2) == FAILURE)
1855 return FAILURE;
1856
1857 if (et0 (op1) != BT_LOGICAL || et0 (op2) != BT_LOGICAL)
1858 {
1859 gfc_error ("LOGICAL operands are required in expression at %L",
1860 &e->where);
1861 return FAILURE;
1862 }
1863
1864 break;
1865
1866 case INTRINSIC_PARENTHESES:
1867 break;
1868
1869 default:
1870 gfc_error ("Only intrinsic operators can be used in expression at %L",
1871 &e->where);
1872 return FAILURE;
1873 }
1874
1875 return SUCCESS;
1876
1877 not_numeric:
1878 gfc_error ("Numeric operands are required in expression at %L", &e->where);
1879
1880 return FAILURE;
1881 }
1882
1883
1884 static match
1885 check_init_expr_arguments (gfc_expr *e)
1886 {
1887 gfc_actual_arglist *ap;
1888
1889 for (ap = e->value.function.actual; ap; ap = ap->next)
1890 if (check_init_expr (ap->expr) == FAILURE)
1891 return MATCH_ERROR;
1892
1893 return MATCH_YES;
1894 }
1895
1896 /* F95, 7.1.6.1, Initialization expressions, (7)
1897 F2003, 7.1.7 Initialization expression, (8) */
1898
1899 static match
1900 check_inquiry (gfc_expr *e, int not_restricted)
1901 {
1902 const char *name;
1903 const char *const *functions;
1904
1905 static const char *const inquiry_func_f95[] = {
1906 "lbound", "shape", "size", "ubound",
1907 "bit_size", "len", "kind",
1908 "digits", "epsilon", "huge", "maxexponent", "minexponent",
1909 "precision", "radix", "range", "tiny",
1910 NULL
1911 };
1912
1913 static const char *const inquiry_func_f2003[] = {
1914 "lbound", "shape", "size", "ubound",
1915 "bit_size", "len", "kind",
1916 "digits", "epsilon", "huge", "maxexponent", "minexponent",
1917 "precision", "radix", "range", "tiny",
1918 "new_line", NULL
1919 };
1920
1921 int i;
1922 gfc_actual_arglist *ap;
1923
1924 if (!e->value.function.isym
1925 || !e->value.function.isym->inquiry)
1926 return MATCH_NO;
1927
1928 /* An undeclared parameter will get us here (PR25018). */
1929 if (e->symtree == NULL)
1930 return MATCH_NO;
1931
1932 name = e->symtree->n.sym->name;
1933
1934 functions = (gfc_option.warn_std & GFC_STD_F2003)
1935 ? inquiry_func_f2003 : inquiry_func_f95;
1936
1937 for (i = 0; functions[i]; i++)
1938 if (strcmp (functions[i], name) == 0)
1939 break;
1940
1941 if (functions[i] == NULL)
1942 {
1943 gfc_error ("Inquiry function '%s' at %L is not permitted "
1944 "in an initialization expression", name, &e->where);
1945 return MATCH_ERROR;
1946 }
1947
1948 /* At this point we have an inquiry function with a variable argument. The
1949 type of the variable might be undefined, but we need it now, because the
1950 arguments of these functions are not allowed to be undefined. */
1951
1952 for (ap = e->value.function.actual; ap; ap = ap->next)
1953 {
1954 if (!ap->expr)
1955 continue;
1956
1957 if (ap->expr->ts.type == BT_UNKNOWN)
1958 {
1959 if (ap->expr->symtree->n.sym->ts.type == BT_UNKNOWN
1960 && gfc_set_default_type (ap->expr->symtree->n.sym, 0, gfc_current_ns)
1961 == FAILURE)
1962 return MATCH_NO;
1963
1964 ap->expr->ts = ap->expr->symtree->n.sym->ts;
1965 }
1966
1967 /* Assumed character length will not reduce to a constant expression
1968 with LEN, as required by the standard. */
1969 if (i == 5 && not_restricted
1970 && ap->expr->symtree->n.sym->ts.type == BT_CHARACTER
1971 && ap->expr->symtree->n.sym->ts.cl->length == NULL)
1972 {
1973 gfc_error ("assumed character length variable '%s' in constant "
1974 "expression at %L", e->symtree->n.sym->name, &e->where);
1975 return MATCH_ERROR;
1976 }
1977 else if (not_restricted && check_init_expr (ap->expr) == FAILURE)
1978 return MATCH_ERROR;
1979 }
1980
1981 return MATCH_YES;
1982 }
1983
1984
1985 /* F95, 7.1.6.1, Initialization expressions, (5)
1986 F2003, 7.1.7 Initialization expression, (5) */
1987
1988 static match
1989 check_transformational (gfc_expr *e)
1990 {
1991 static const char * const trans_func_f95[] = {
1992 "repeat", "reshape", "selected_int_kind",
1993 "selected_real_kind", "transfer", "trim", NULL
1994 };
1995
1996 int i;
1997 const char *name;
1998
1999 if (!e->value.function.isym
2000 || !e->value.function.isym->transformational)
2001 return MATCH_NO;
2002
2003 name = e->symtree->n.sym->name;
2004
2005 /* NULL() is dealt with below. */
2006 if (strcmp ("null", name) == 0)
2007 return MATCH_NO;
2008
2009 for (i = 0; trans_func_f95[i]; i++)
2010 if (strcmp (trans_func_f95[i], name) == 0)
2011 break;
2012
2013 /* FIXME, F2003: implement translation of initialization
2014 expressions before enabling this check. For F95, error
2015 out if the transformational function is not in the list. */
2016 #if 0
2017 if (trans_func_f95[i] == NULL
2018 && gfc_notify_std (GFC_STD_F2003,
2019 "transformational intrinsic '%s' at %L is not permitted "
2020 "in an initialization expression", name, &e->where) == FAILURE)
2021 return MATCH_ERROR;
2022 #else
2023 if (trans_func_f95[i] == NULL)
2024 {
2025 gfc_error("transformational intrinsic '%s' at %L is not permitted "
2026 "in an initialization expression", name, &e->where);
2027 return MATCH_ERROR;
2028 }
2029 #endif
2030
2031 return check_init_expr_arguments (e);
2032 }
2033
2034
2035 /* F95, 7.1.6.1, Initialization expressions, (6)
2036 F2003, 7.1.7 Initialization expression, (6) */
2037
2038 static match
2039 check_null (gfc_expr *e)
2040 {
2041 if (strcmp ("null", e->symtree->n.sym->name) != 0)
2042 return MATCH_NO;
2043
2044 return check_init_expr_arguments (e);
2045 }
2046
2047
2048 static match
2049 check_elemental (gfc_expr *e)
2050 {
2051 if (!e->value.function.isym
2052 || !e->value.function.isym->elemental)
2053 return MATCH_NO;
2054
2055 if ((e->ts.type != BT_INTEGER || e->ts.type != BT_CHARACTER)
2056 && gfc_notify_std (GFC_STD_F2003, "Extension: Evaluation of "
2057 "nonstandard initialization expression at %L",
2058 &e->where) == FAILURE)
2059 return MATCH_ERROR;
2060
2061 return check_init_expr_arguments (e);
2062 }
2063
2064
2065 static match
2066 check_conversion (gfc_expr *e)
2067 {
2068 if (!e->value.function.isym
2069 || !e->value.function.isym->conversion)
2070 return MATCH_NO;
2071
2072 return check_init_expr_arguments (e);
2073 }
2074
2075
2076 /* Verify that an expression is an initialization expression. A side
2077 effect is that the expression tree is reduced to a single constant
2078 node if all goes well. This would normally happen when the
2079 expression is constructed but function references are assumed to be
2080 intrinsics in the context of initialization expressions. If
2081 FAILURE is returned an error message has been generated. */
2082
2083 static try
2084 check_init_expr (gfc_expr *e)
2085 {
2086 match m;
2087 try t;
2088 gfc_intrinsic_sym *isym;
2089
2090 if (e == NULL)
2091 return SUCCESS;
2092
2093 switch (e->expr_type)
2094 {
2095 case EXPR_OP:
2096 t = check_intrinsic_op (e, check_init_expr);
2097 if (t == SUCCESS)
2098 t = gfc_simplify_expr (e, 0);
2099
2100 break;
2101
2102 case EXPR_FUNCTION:
2103 t = FAILURE;
2104
2105 if ((m = check_specification_function (e)) != MATCH_YES)
2106 {
2107 if ((m = gfc_intrinsic_func_interface (e, 0)) != MATCH_YES)
2108 {
2109 gfc_error ("Function '%s' in initialization expression at %L "
2110 "must be an intrinsic or a specification function",
2111 e->symtree->n.sym->name, &e->where);
2112 break;
2113 }
2114
2115 if ((m = check_conversion (e)) == MATCH_NO
2116 && (m = check_inquiry (e, 1)) == MATCH_NO
2117 && (m = check_null (e)) == MATCH_NO
2118 && (m = check_transformational (e)) == MATCH_NO
2119 && (m = check_elemental (e)) == MATCH_NO)
2120 {
2121 gfc_error ("Intrinsic function '%s' at %L is not permitted "
2122 "in an initialization expression",
2123 e->symtree->n.sym->name, &e->where);
2124 m = MATCH_ERROR;
2125 }
2126
2127 /* Try to scalarize an elemental intrinsic function that has an
2128 array argument. */
2129 isym = gfc_find_function (e->symtree->n.sym->name);
2130 if (isym && isym->elemental
2131 && e->value.function.actual->expr->expr_type == EXPR_ARRAY)
2132 {
2133 if ((t = scalarize_intrinsic_call (e)) == SUCCESS)
2134 break;
2135 }
2136 }
2137
2138 if (m == MATCH_YES)
2139 t = gfc_simplify_expr (e, 0);
2140
2141 break;
2142
2143 case EXPR_VARIABLE:
2144 t = SUCCESS;
2145
2146 if (gfc_check_iter_variable (e) == SUCCESS)
2147 break;
2148
2149 if (e->symtree->n.sym->attr.flavor == FL_PARAMETER)
2150 {
2151 t = simplify_parameter_variable (e, 0);
2152 break;
2153 }
2154
2155 if (gfc_in_match_data ())
2156 break;
2157
2158 t = FAILURE;
2159
2160 if (e->symtree->n.sym->as)
2161 {
2162 switch (e->symtree->n.sym->as->type)
2163 {
2164 case AS_ASSUMED_SIZE:
2165 gfc_error ("assumed size array '%s' at %L is not permitted "
2166 "in an initialization expression",
2167 e->symtree->n.sym->name, &e->where);
2168 break;
2169
2170 case AS_ASSUMED_SHAPE:
2171 gfc_error ("assumed shape array '%s' at %L is not permitted "
2172 "in an initialization expression",
2173 e->symtree->n.sym->name, &e->where);
2174 break;
2175
2176 case AS_DEFERRED:
2177 gfc_error ("deferred array '%s' at %L is not permitted "
2178 "in an initialization expression",
2179 e->symtree->n.sym->name, &e->where);
2180 break;
2181
2182 default:
2183 gcc_unreachable();
2184 }
2185 }
2186 else
2187 gfc_error ("Parameter '%s' at %L has not been declared or is "
2188 "a variable, which does not reduce to a constant "
2189 "expression", e->symtree->n.sym->name, &e->where);
2190
2191 break;
2192
2193 case EXPR_CONSTANT:
2194 case EXPR_NULL:
2195 t = SUCCESS;
2196 break;
2197
2198 case EXPR_SUBSTRING:
2199 t = check_init_expr (e->ref->u.ss.start);
2200 if (t == FAILURE)
2201 break;
2202
2203 t = check_init_expr (e->ref->u.ss.end);
2204 if (t == SUCCESS)
2205 t = gfc_simplify_expr (e, 0);
2206
2207 break;
2208
2209 case EXPR_STRUCTURE:
2210 t = gfc_check_constructor (e, check_init_expr);
2211 break;
2212
2213 case EXPR_ARRAY:
2214 t = gfc_check_constructor (e, check_init_expr);
2215 if (t == FAILURE)
2216 break;
2217
2218 t = gfc_expand_constructor (e);
2219 if (t == FAILURE)
2220 break;
2221
2222 t = gfc_check_constructor_type (e);
2223 break;
2224
2225 default:
2226 gfc_internal_error ("check_init_expr(): Unknown expression type");
2227 }
2228
2229 return t;
2230 }
2231
2232
2233 /* Match an initialization expression. We work by first matching an
2234 expression, then reducing it to a constant. */
2235
2236 match
2237 gfc_match_init_expr (gfc_expr **result)
2238 {
2239 gfc_expr *expr;
2240 match m;
2241 try t;
2242
2243 m = gfc_match_expr (&expr);
2244 if (m != MATCH_YES)
2245 return m;
2246
2247 gfc_init_expr = 1;
2248 t = gfc_resolve_expr (expr);
2249 if (t == SUCCESS)
2250 t = check_init_expr (expr);
2251 gfc_init_expr = 0;
2252
2253 if (t == FAILURE)
2254 {
2255 gfc_free_expr (expr);
2256 return MATCH_ERROR;
2257 }
2258
2259 if (expr->expr_type == EXPR_ARRAY
2260 && (gfc_check_constructor_type (expr) == FAILURE
2261 || gfc_expand_constructor (expr) == FAILURE))
2262 {
2263 gfc_free_expr (expr);
2264 return MATCH_ERROR;
2265 }
2266
2267 /* Not all inquiry functions are simplified to constant expressions
2268 so it is necessary to call check_inquiry again. */
2269 if (!gfc_is_constant_expr (expr) && check_inquiry (expr, 1) != MATCH_YES
2270 && !gfc_in_match_data ())
2271 {
2272 gfc_error ("Initialization expression didn't reduce %C");
2273 return MATCH_ERROR;
2274 }
2275
2276 *result = expr;
2277
2278 return MATCH_YES;
2279 }
2280
2281
2282 static try check_restricted (gfc_expr *);
2283
2284 /* Given an actual argument list, test to see that each argument is a
2285 restricted expression and optionally if the expression type is
2286 integer or character. */
2287
2288 static try
2289 restricted_args (gfc_actual_arglist *a)
2290 {
2291 for (; a; a = a->next)
2292 {
2293 if (check_restricted (a->expr) == FAILURE)
2294 return FAILURE;
2295 }
2296
2297 return SUCCESS;
2298 }
2299
2300
2301 /************* Restricted/specification expressions *************/
2302
2303
2304 /* Make sure a non-intrinsic function is a specification function. */
2305
2306 static try
2307 external_spec_function (gfc_expr *e)
2308 {
2309 gfc_symbol *f;
2310
2311 f = e->value.function.esym;
2312
2313 if (f->attr.proc == PROC_ST_FUNCTION)
2314 {
2315 gfc_error ("Specification function '%s' at %L cannot be a statement "
2316 "function", f->name, &e->where);
2317 return FAILURE;
2318 }
2319
2320 if (f->attr.proc == PROC_INTERNAL)
2321 {
2322 gfc_error ("Specification function '%s' at %L cannot be an internal "
2323 "function", f->name, &e->where);
2324 return FAILURE;
2325 }
2326
2327 if (!f->attr.pure && !f->attr.elemental)
2328 {
2329 gfc_error ("Specification function '%s' at %L must be PURE", f->name,
2330 &e->where);
2331 return FAILURE;
2332 }
2333
2334 if (f->attr.recursive)
2335 {
2336 gfc_error ("Specification function '%s' at %L cannot be RECURSIVE",
2337 f->name, &e->where);
2338 return FAILURE;
2339 }
2340
2341 return restricted_args (e->value.function.actual);
2342 }
2343
2344
2345 /* Check to see that a function reference to an intrinsic is a
2346 restricted expression. */
2347
2348 static try
2349 restricted_intrinsic (gfc_expr *e)
2350 {
2351 /* TODO: Check constraints on inquiry functions. 7.1.6.2 (7). */
2352 if (check_inquiry (e, 0) == MATCH_YES)
2353 return SUCCESS;
2354
2355 return restricted_args (e->value.function.actual);
2356 }
2357
2358
2359 /* Verify that an expression is a restricted expression. Like its
2360 cousin check_init_expr(), an error message is generated if we
2361 return FAILURE. */
2362
2363 static try
2364 check_restricted (gfc_expr *e)
2365 {
2366 gfc_symbol *sym;
2367 try t;
2368
2369 if (e == NULL)
2370 return SUCCESS;
2371
2372 switch (e->expr_type)
2373 {
2374 case EXPR_OP:
2375 t = check_intrinsic_op (e, check_restricted);
2376 if (t == SUCCESS)
2377 t = gfc_simplify_expr (e, 0);
2378
2379 break;
2380
2381 case EXPR_FUNCTION:
2382 t = e->value.function.esym ? external_spec_function (e)
2383 : restricted_intrinsic (e);
2384 break;
2385
2386 case EXPR_VARIABLE:
2387 sym = e->symtree->n.sym;
2388 t = FAILURE;
2389
2390 if (sym->attr.optional)
2391 {
2392 gfc_error ("Dummy argument '%s' at %L cannot be OPTIONAL",
2393 sym->name, &e->where);
2394 break;
2395 }
2396
2397 if (sym->attr.intent == INTENT_OUT)
2398 {
2399 gfc_error ("Dummy argument '%s' at %L cannot be INTENT(OUT)",
2400 sym->name, &e->where);
2401 break;
2402 }
2403
2404 /* gfc_is_formal_arg broadcasts that a formal argument list is being
2405 processed in resolve.c(resolve_formal_arglist). This is done so
2406 that host associated dummy array indices are accepted (PR23446).
2407 This mechanism also does the same for the specification expressions
2408 of array-valued functions. */
2409 if (sym->attr.in_common
2410 || sym->attr.use_assoc
2411 || sym->attr.dummy
2412 || sym->ns != gfc_current_ns
2413 || (sym->ns->proc_name != NULL
2414 && sym->ns->proc_name->attr.flavor == FL_MODULE)
2415 || (gfc_is_formal_arg () && (sym->ns == gfc_current_ns)))
2416 {
2417 t = SUCCESS;
2418 break;
2419 }
2420
2421 gfc_error ("Variable '%s' cannot appear in the expression at %L",
2422 sym->name, &e->where);
2423
2424 break;
2425
2426 case EXPR_NULL:
2427 case EXPR_CONSTANT:
2428 t = SUCCESS;
2429 break;
2430
2431 case EXPR_SUBSTRING:
2432 t = gfc_specification_expr (e->ref->u.ss.start);
2433 if (t == FAILURE)
2434 break;
2435
2436 t = gfc_specification_expr (e->ref->u.ss.end);
2437 if (t == SUCCESS)
2438 t = gfc_simplify_expr (e, 0);
2439
2440 break;
2441
2442 case EXPR_STRUCTURE:
2443 t = gfc_check_constructor (e, check_restricted);
2444 break;
2445
2446 case EXPR_ARRAY:
2447 t = gfc_check_constructor (e, check_restricted);
2448 break;
2449
2450 default:
2451 gfc_internal_error ("check_restricted(): Unknown expression type");
2452 }
2453
2454 return t;
2455 }
2456
2457
2458 /* Check to see that an expression is a specification expression. If
2459 we return FAILURE, an error has been generated. */
2460
2461 try
2462 gfc_specification_expr (gfc_expr *e)
2463 {
2464
2465 if (e == NULL)
2466 return SUCCESS;
2467
2468 if (e->ts.type != BT_INTEGER)
2469 {
2470 gfc_error ("Expression at %L must be of INTEGER type", &e->where);
2471 return FAILURE;
2472 }
2473
2474 if (e->rank != 0)
2475 {
2476 gfc_error ("Expression at %L must be scalar", &e->where);
2477 return FAILURE;
2478 }
2479
2480 if (gfc_simplify_expr (e, 0) == FAILURE)
2481 return FAILURE;
2482
2483 return check_restricted (e);
2484 }
2485
2486
2487 /************** Expression conformance checks. *************/
2488
2489 /* Given two expressions, make sure that the arrays are conformable. */
2490
2491 try
2492 gfc_check_conformance (const char *optype_msgid, gfc_expr *op1, gfc_expr *op2)
2493 {
2494 int op1_flag, op2_flag, d;
2495 mpz_t op1_size, op2_size;
2496 try t;
2497
2498 if (op1->rank == 0 || op2->rank == 0)
2499 return SUCCESS;
2500
2501 if (op1->rank != op2->rank)
2502 {
2503 gfc_error ("Incompatible ranks in %s at %L", _(optype_msgid),
2504 &op1->where);
2505 return FAILURE;
2506 }
2507
2508 t = SUCCESS;
2509
2510 for (d = 0; d < op1->rank; d++)
2511 {
2512 op1_flag = gfc_array_dimen_size (op1, d, &op1_size) == SUCCESS;
2513 op2_flag = gfc_array_dimen_size (op2, d, &op2_size) == SUCCESS;
2514
2515 if (op1_flag && op2_flag && mpz_cmp (op1_size, op2_size) != 0)
2516 {
2517 gfc_error ("different shape for %s at %L on dimension %d (%d/%d)",
2518 _(optype_msgid), &op1->where, d + 1,
2519 (int) mpz_get_si (op1_size),
2520 (int) mpz_get_si (op2_size));
2521
2522 t = FAILURE;
2523 }
2524
2525 if (op1_flag)
2526 mpz_clear (op1_size);
2527 if (op2_flag)
2528 mpz_clear (op2_size);
2529
2530 if (t == FAILURE)
2531 return FAILURE;
2532 }
2533
2534 return SUCCESS;
2535 }
2536
2537
2538 /* Given an assignable expression and an arbitrary expression, make
2539 sure that the assignment can take place. */
2540
2541 try
2542 gfc_check_assign (gfc_expr *lvalue, gfc_expr *rvalue, int conform)
2543 {
2544 gfc_symbol *sym;
2545 gfc_ref *ref;
2546 int has_pointer;
2547
2548 sym = lvalue->symtree->n.sym;
2549
2550 /* Check INTENT(IN), unless the object itself is the component or
2551 sub-component of a pointer. */
2552 has_pointer = sym->attr.pointer;
2553
2554 for (ref = lvalue->ref; ref; ref = ref->next)
2555 if (ref->type == REF_COMPONENT && ref->u.c.component->pointer)
2556 {
2557 has_pointer = 1;
2558 break;
2559 }
2560
2561 if (!has_pointer && sym->attr.intent == INTENT_IN)
2562 {
2563 gfc_error ("Cannot assign to INTENT(IN) variable '%s' at %L",
2564 sym->name, &lvalue->where);
2565 return FAILURE;
2566 }
2567
2568 /* 12.5.2.2, Note 12.26: The result variable is very similar to any other
2569 variable local to a function subprogram. Its existence begins when
2570 execution of the function is initiated and ends when execution of the
2571 function is terminated...
2572 Therefore, the left hand side is no longer a variable, when it is: */
2573 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_ST_FUNCTION
2574 && !sym->attr.external)
2575 {
2576 bool bad_proc;
2577 bad_proc = false;
2578
2579 /* (i) Use associated; */
2580 if (sym->attr.use_assoc)
2581 bad_proc = true;
2582
2583 /* (ii) The assignment is in the main program; or */
2584 if (gfc_current_ns->proc_name->attr.is_main_program)
2585 bad_proc = true;
2586
2587 /* (iii) A module or internal procedure... */
2588 if ((gfc_current_ns->proc_name->attr.proc == PROC_INTERNAL
2589 || gfc_current_ns->proc_name->attr.proc == PROC_MODULE)
2590 && gfc_current_ns->parent
2591 && (!(gfc_current_ns->parent->proc_name->attr.function
2592 || gfc_current_ns->parent->proc_name->attr.subroutine)
2593 || gfc_current_ns->parent->proc_name->attr.is_main_program))
2594 {
2595 /* ... that is not a function... */
2596 if (!gfc_current_ns->proc_name->attr.function)
2597 bad_proc = true;
2598
2599 /* ... or is not an entry and has a different name. */
2600 if (!sym->attr.entry && sym->name != gfc_current_ns->proc_name->name)
2601 bad_proc = true;
2602 }
2603
2604 if (bad_proc)
2605 {
2606 gfc_error ("'%s' at %L is not a VALUE", sym->name, &lvalue->where);
2607 return FAILURE;
2608 }
2609 }
2610
2611 if (rvalue->rank != 0 && lvalue->rank != rvalue->rank)
2612 {
2613 gfc_error ("Incompatible ranks %d and %d in assignment at %L",
2614 lvalue->rank, rvalue->rank, &lvalue->where);
2615 return FAILURE;
2616 }
2617
2618 if (lvalue->ts.type == BT_UNKNOWN)
2619 {
2620 gfc_error ("Variable type is UNKNOWN in assignment at %L",
2621 &lvalue->where);
2622 return FAILURE;
2623 }
2624
2625 if (rvalue->expr_type == EXPR_NULL)
2626 {
2627 if (lvalue->symtree->n.sym->attr.pointer
2628 && lvalue->symtree->n.sym->attr.data)
2629 return SUCCESS;
2630 else
2631 {
2632 gfc_error ("NULL appears on right-hand side in assignment at %L",
2633 &rvalue->where);
2634 return FAILURE;
2635 }
2636 }
2637
2638 if (sym->attr.cray_pointee
2639 && lvalue->ref != NULL
2640 && lvalue->ref->u.ar.type == AR_FULL
2641 && lvalue->ref->u.ar.as->cp_was_assumed)
2642 {
2643 gfc_error ("Vector assignment to assumed-size Cray Pointee at %L "
2644 "is illegal", &lvalue->where);
2645 return FAILURE;
2646 }
2647
2648 /* This is possibly a typo: x = f() instead of x => f(). */
2649 if (gfc_option.warn_surprising
2650 && rvalue->expr_type == EXPR_FUNCTION
2651 && rvalue->symtree->n.sym->attr.pointer)
2652 gfc_warning ("POINTER valued function appears on right-hand side of "
2653 "assignment at %L", &rvalue->where);
2654
2655 /* Check size of array assignments. */
2656 if (lvalue->rank != 0 && rvalue->rank != 0
2657 && gfc_check_conformance ("Array assignment", lvalue, rvalue) != SUCCESS)
2658 return FAILURE;
2659
2660 if (gfc_compare_types (&lvalue->ts, &rvalue->ts))
2661 return SUCCESS;
2662
2663 if (!conform)
2664 {
2665 /* Numeric can be converted to any other numeric. And Hollerith can be
2666 converted to any other type. */
2667 if ((gfc_numeric_ts (&lvalue->ts) && gfc_numeric_ts (&rvalue->ts))
2668 || rvalue->ts.type == BT_HOLLERITH)
2669 return SUCCESS;
2670
2671 if (lvalue->ts.type == BT_LOGICAL && rvalue->ts.type == BT_LOGICAL)
2672 return SUCCESS;
2673
2674 gfc_error ("Incompatible types in assignment at %L, %s to %s",
2675 &rvalue->where, gfc_typename (&rvalue->ts),
2676 gfc_typename (&lvalue->ts));
2677
2678 return FAILURE;
2679 }
2680
2681 return gfc_convert_type (rvalue, &lvalue->ts, 1);
2682 }
2683
2684
2685 /* Check that a pointer assignment is OK. We first check lvalue, and
2686 we only check rvalue if it's not an assignment to NULL() or a
2687 NULLIFY statement. */
2688
2689 try
2690 gfc_check_pointer_assign (gfc_expr *lvalue, gfc_expr *rvalue)
2691 {
2692 symbol_attribute attr;
2693 gfc_ref *ref;
2694 int is_pure;
2695 int pointer, check_intent_in;
2696
2697 if (lvalue->symtree->n.sym->ts.type == BT_UNKNOWN)
2698 {
2699 gfc_error ("Pointer assignment target is not a POINTER at %L",
2700 &lvalue->where);
2701 return FAILURE;
2702 }
2703
2704 if (lvalue->symtree->n.sym->attr.flavor == FL_PROCEDURE
2705 && lvalue->symtree->n.sym->attr.use_assoc)
2706 {
2707 gfc_error ("'%s' in the pointer assignment at %L cannot be an "
2708 "l-value since it is a procedure",
2709 lvalue->symtree->n.sym->name, &lvalue->where);
2710 return FAILURE;
2711 }
2712
2713
2714 /* Check INTENT(IN), unless the object itself is the component or
2715 sub-component of a pointer. */
2716 check_intent_in = 1;
2717 pointer = lvalue->symtree->n.sym->attr.pointer;
2718
2719 for (ref = lvalue->ref; ref; ref = ref->next)
2720 {
2721 if (pointer)
2722 check_intent_in = 0;
2723
2724 if (ref->type == REF_COMPONENT && ref->u.c.component->pointer)
2725 pointer = 1;
2726 }
2727
2728 if (check_intent_in && lvalue->symtree->n.sym->attr.intent == INTENT_IN)
2729 {
2730 gfc_error ("Cannot assign to INTENT(IN) variable '%s' at %L",
2731 lvalue->symtree->n.sym->name, &lvalue->where);
2732 return FAILURE;
2733 }
2734
2735 if (!pointer)
2736 {
2737 gfc_error ("Pointer assignment to non-POINTER at %L", &lvalue->where);
2738 return FAILURE;
2739 }
2740
2741 is_pure = gfc_pure (NULL);
2742
2743 if (is_pure && gfc_impure_variable (lvalue->symtree->n.sym))
2744 {
2745 gfc_error ("Bad pointer object in PURE procedure at %L", &lvalue->where);
2746 return FAILURE;
2747 }
2748
2749 /* If rvalue is a NULL() or NULLIFY, we're done. Otherwise the type,
2750 kind, etc for lvalue and rvalue must match, and rvalue must be a
2751 pure variable if we're in a pure function. */
2752 if (rvalue->expr_type == EXPR_NULL && rvalue->ts.type == BT_UNKNOWN)
2753 return SUCCESS;
2754
2755 if (!gfc_compare_types (&lvalue->ts, &rvalue->ts))
2756 {
2757 gfc_error ("Different types in pointer assignment at %L",
2758 &lvalue->where);
2759 return FAILURE;
2760 }
2761
2762 if (lvalue->ts.kind != rvalue->ts.kind)
2763 {
2764 gfc_error ("Different kind type parameters in pointer "
2765 "assignment at %L", &lvalue->where);
2766 return FAILURE;
2767 }
2768
2769 if (lvalue->rank != rvalue->rank)
2770 {
2771 gfc_error ("Different ranks in pointer assignment at %L",
2772 &lvalue->where);
2773 return FAILURE;
2774 }
2775
2776 /* Now punt if we are dealing with a NULLIFY(X) or X = NULL(X). */
2777 if (rvalue->expr_type == EXPR_NULL)
2778 return SUCCESS;
2779
2780 if (lvalue->ts.type == BT_CHARACTER
2781 && lvalue->ts.cl && rvalue->ts.cl
2782 && lvalue->ts.cl->length && rvalue->ts.cl->length
2783 && abs (gfc_dep_compare_expr (lvalue->ts.cl->length,
2784 rvalue->ts.cl->length)) == 1)
2785 {
2786 gfc_error ("Different character lengths in pointer "
2787 "assignment at %L", &lvalue->where);
2788 return FAILURE;
2789 }
2790
2791 attr = gfc_expr_attr (rvalue);
2792 if (!attr.target && !attr.pointer)
2793 {
2794 gfc_error ("Pointer assignment target is neither TARGET "
2795 "nor POINTER at %L", &rvalue->where);
2796 return FAILURE;
2797 }
2798
2799 if (is_pure && gfc_impure_variable (rvalue->symtree->n.sym))
2800 {
2801 gfc_error ("Bad target in pointer assignment in PURE "
2802 "procedure at %L", &rvalue->where);
2803 }
2804
2805 if (gfc_has_vector_index (rvalue))
2806 {
2807 gfc_error ("Pointer assignment with vector subscript "
2808 "on rhs at %L", &rvalue->where);
2809 return FAILURE;
2810 }
2811
2812 if (attr.protected && attr.use_assoc)
2813 {
2814 gfc_error ("Pointer assigment target has PROTECTED "
2815 "attribute at %L", &rvalue->where);
2816 return FAILURE;
2817 }
2818
2819 return SUCCESS;
2820 }
2821
2822
2823 /* Relative of gfc_check_assign() except that the lvalue is a single
2824 symbol. Used for initialization assignments. */
2825
2826 try
2827 gfc_check_assign_symbol (gfc_symbol *sym, gfc_expr *rvalue)
2828 {
2829 gfc_expr lvalue;
2830 try r;
2831
2832 memset (&lvalue, '\0', sizeof (gfc_expr));
2833
2834 lvalue.expr_type = EXPR_VARIABLE;
2835 lvalue.ts = sym->ts;
2836 if (sym->as)
2837 lvalue.rank = sym->as->rank;
2838 lvalue.symtree = (gfc_symtree *) gfc_getmem (sizeof (gfc_symtree));
2839 lvalue.symtree->n.sym = sym;
2840 lvalue.where = sym->declared_at;
2841
2842 if (sym->attr.pointer)
2843 r = gfc_check_pointer_assign (&lvalue, rvalue);
2844 else
2845 r = gfc_check_assign (&lvalue, rvalue, 1);
2846
2847 gfc_free (lvalue.symtree);
2848
2849 return r;
2850 }
2851
2852
2853 /* Get an expression for a default initializer. */
2854
2855 gfc_expr *
2856 gfc_default_initializer (gfc_typespec *ts)
2857 {
2858 gfc_constructor *tail;
2859 gfc_expr *init;
2860 gfc_component *c;
2861
2862 init = NULL;
2863
2864 /* See if we have a default initializer. */
2865 for (c = ts->derived->components; c; c = c->next)
2866 {
2867 if ((c->initializer || c->allocatable) && init == NULL)
2868 init = gfc_get_expr ();
2869 }
2870
2871 if (init == NULL)
2872 return NULL;
2873
2874 /* Build the constructor. */
2875 init->expr_type = EXPR_STRUCTURE;
2876 init->ts = *ts;
2877 init->where = ts->derived->declared_at;
2878 tail = NULL;
2879 for (c = ts->derived->components; c; c = c->next)
2880 {
2881 if (tail == NULL)
2882 init->value.constructor = tail = gfc_get_constructor ();
2883 else
2884 {
2885 tail->next = gfc_get_constructor ();
2886 tail = tail->next;
2887 }
2888
2889 if (c->initializer)
2890 tail->expr = gfc_copy_expr (c->initializer);
2891
2892 if (c->allocatable)
2893 {
2894 tail->expr = gfc_get_expr ();
2895 tail->expr->expr_type = EXPR_NULL;
2896 tail->expr->ts = c->ts;
2897 }
2898 }
2899 return init;
2900 }
2901
2902
2903 /* Given a symbol, create an expression node with that symbol as a
2904 variable. If the symbol is array valued, setup a reference of the
2905 whole array. */
2906
2907 gfc_expr *
2908 gfc_get_variable_expr (gfc_symtree *var)
2909 {
2910 gfc_expr *e;
2911
2912 e = gfc_get_expr ();
2913 e->expr_type = EXPR_VARIABLE;
2914 e->symtree = var;
2915 e->ts = var->n.sym->ts;
2916
2917 if (var->n.sym->as != NULL)
2918 {
2919 e->rank = var->n.sym->as->rank;
2920 e->ref = gfc_get_ref ();
2921 e->ref->type = REF_ARRAY;
2922 e->ref->u.ar.type = AR_FULL;
2923 }
2924
2925 return e;
2926 }
2927
2928
2929 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
2930
2931 void
2932 gfc_expr_set_symbols_referenced (gfc_expr *expr)
2933 {
2934 gfc_actual_arglist *arg;
2935 gfc_constructor *c;
2936 gfc_ref *ref;
2937 int i;
2938
2939 if (!expr) return;
2940
2941 switch (expr->expr_type)
2942 {
2943 case EXPR_OP:
2944 gfc_expr_set_symbols_referenced (expr->value.op.op1);
2945 gfc_expr_set_symbols_referenced (expr->value.op.op2);
2946 break;
2947
2948 case EXPR_FUNCTION:
2949 for (arg = expr->value.function.actual; arg; arg = arg->next)
2950 gfc_expr_set_symbols_referenced (arg->expr);
2951 break;
2952
2953 case EXPR_VARIABLE:
2954 gfc_set_sym_referenced (expr->symtree->n.sym);
2955 break;
2956
2957 case EXPR_CONSTANT:
2958 case EXPR_NULL:
2959 case EXPR_SUBSTRING:
2960 break;
2961
2962 case EXPR_STRUCTURE:
2963 case EXPR_ARRAY:
2964 for (c = expr->value.constructor; c; c = c->next)
2965 gfc_expr_set_symbols_referenced (c->expr);
2966 break;
2967
2968 default:
2969 gcc_unreachable ();
2970 break;
2971 }
2972
2973 for (ref = expr->ref; ref; ref = ref->next)
2974 switch (ref->type)
2975 {
2976 case REF_ARRAY:
2977 for (i = 0; i < ref->u.ar.dimen; i++)
2978 {
2979 gfc_expr_set_symbols_referenced (ref->u.ar.start[i]);
2980 gfc_expr_set_symbols_referenced (ref->u.ar.end[i]);
2981 gfc_expr_set_symbols_referenced (ref->u.ar.stride[i]);
2982 }
2983 break;
2984
2985 case REF_COMPONENT:
2986 break;
2987
2988 case REF_SUBSTRING:
2989 gfc_expr_set_symbols_referenced (ref->u.ss.start);
2990 gfc_expr_set_symbols_referenced (ref->u.ss.end);
2991 break;
2992
2993 default:
2994 gcc_unreachable ();
2995 break;
2996 }
2997 }