re PR fortran/50163 (ICE: initialization expression)
[gcc.git] / gcc / fortran / expr.c
1 /* Routines for manipulation of expression nodes.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Contributed by Andy Vaught
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "gfortran.h"
26 #include "arith.h"
27 #include "match.h"
28 #include "target-memory.h" /* for gfc_convert_boz */
29 #include "constructor.h"
30
31
32 /* The following set of functions provide access to gfc_expr* of
33 various types - actual all but EXPR_FUNCTION and EXPR_VARIABLE.
34
35 There are two functions available elsewhere that provide
36 slightly different flavours of variables. Namely:
37 expr.c (gfc_get_variable_expr)
38 symbol.c (gfc_lval_expr_from_sym)
39 TODO: Merge these functions, if possible. */
40
41 /* Get a new expression node. */
42
43 gfc_expr *
44 gfc_get_expr (void)
45 {
46 gfc_expr *e;
47
48 e = XCNEW (gfc_expr);
49 gfc_clear_ts (&e->ts);
50 e->shape = NULL;
51 e->ref = NULL;
52 e->symtree = NULL;
53 return e;
54 }
55
56
57 /* Get a new expression node that is an array constructor
58 of given type and kind. */
59
60 gfc_expr *
61 gfc_get_array_expr (bt type, int kind, locus *where)
62 {
63 gfc_expr *e;
64
65 e = gfc_get_expr ();
66 e->expr_type = EXPR_ARRAY;
67 e->value.constructor = NULL;
68 e->rank = 1;
69 e->shape = NULL;
70
71 e->ts.type = type;
72 e->ts.kind = kind;
73 if (where)
74 e->where = *where;
75
76 return e;
77 }
78
79
80 /* Get a new expression node that is the NULL expression. */
81
82 gfc_expr *
83 gfc_get_null_expr (locus *where)
84 {
85 gfc_expr *e;
86
87 e = gfc_get_expr ();
88 e->expr_type = EXPR_NULL;
89 e->ts.type = BT_UNKNOWN;
90
91 if (where)
92 e->where = *where;
93
94 return e;
95 }
96
97
98 /* Get a new expression node that is an operator expression node. */
99
100 gfc_expr *
101 gfc_get_operator_expr (locus *where, gfc_intrinsic_op op,
102 gfc_expr *op1, gfc_expr *op2)
103 {
104 gfc_expr *e;
105
106 e = gfc_get_expr ();
107 e->expr_type = EXPR_OP;
108 e->value.op.op = op;
109 e->value.op.op1 = op1;
110 e->value.op.op2 = op2;
111
112 if (where)
113 e->where = *where;
114
115 return e;
116 }
117
118
119 /* Get a new expression node that is an structure constructor
120 of given type and kind. */
121
122 gfc_expr *
123 gfc_get_structure_constructor_expr (bt type, int kind, locus *where)
124 {
125 gfc_expr *e;
126
127 e = gfc_get_expr ();
128 e->expr_type = EXPR_STRUCTURE;
129 e->value.constructor = NULL;
130
131 e->ts.type = type;
132 e->ts.kind = kind;
133 if (where)
134 e->where = *where;
135
136 return e;
137 }
138
139
140 /* Get a new expression node that is an constant of given type and kind. */
141
142 gfc_expr *
143 gfc_get_constant_expr (bt type, int kind, locus *where)
144 {
145 gfc_expr *e;
146
147 if (!where)
148 gfc_internal_error ("gfc_get_constant_expr(): locus 'where' cannot be NULL");
149
150 e = gfc_get_expr ();
151
152 e->expr_type = EXPR_CONSTANT;
153 e->ts.type = type;
154 e->ts.kind = kind;
155 e->where = *where;
156
157 switch (type)
158 {
159 case BT_INTEGER:
160 mpz_init (e->value.integer);
161 break;
162
163 case BT_REAL:
164 gfc_set_model_kind (kind);
165 mpfr_init (e->value.real);
166 break;
167
168 case BT_COMPLEX:
169 gfc_set_model_kind (kind);
170 mpc_init2 (e->value.complex, mpfr_get_default_prec());
171 break;
172
173 default:
174 break;
175 }
176
177 return e;
178 }
179
180
181 /* Get a new expression node that is an string constant.
182 If no string is passed, a string of len is allocated,
183 blanked and null-terminated. */
184
185 gfc_expr *
186 gfc_get_character_expr (int kind, locus *where, const char *src, int len)
187 {
188 gfc_expr *e;
189 gfc_char_t *dest;
190
191 if (!src)
192 {
193 dest = gfc_get_wide_string (len + 1);
194 gfc_wide_memset (dest, ' ', len);
195 dest[len] = '\0';
196 }
197 else
198 dest = gfc_char_to_widechar (src);
199
200 e = gfc_get_constant_expr (BT_CHARACTER, kind,
201 where ? where : &gfc_current_locus);
202 e->value.character.string = dest;
203 e->value.character.length = len;
204
205 return e;
206 }
207
208
209 /* Get a new expression node that is an integer constant. */
210
211 gfc_expr *
212 gfc_get_int_expr (int kind, locus *where, int value)
213 {
214 gfc_expr *p;
215 p = gfc_get_constant_expr (BT_INTEGER, kind,
216 where ? where : &gfc_current_locus);
217
218 mpz_set_si (p->value.integer, value);
219
220 return p;
221 }
222
223
224 /* Get a new expression node that is a logical constant. */
225
226 gfc_expr *
227 gfc_get_logical_expr (int kind, locus *where, bool value)
228 {
229 gfc_expr *p;
230 p = gfc_get_constant_expr (BT_LOGICAL, kind,
231 where ? where : &gfc_current_locus);
232
233 p->value.logical = value;
234
235 return p;
236 }
237
238
239 gfc_expr *
240 gfc_get_iokind_expr (locus *where, io_kind k)
241 {
242 gfc_expr *e;
243
244 /* Set the types to something compatible with iokind. This is needed to
245 get through gfc_free_expr later since iokind really has no Basic Type,
246 BT, of its own. */
247
248 e = gfc_get_expr ();
249 e->expr_type = EXPR_CONSTANT;
250 e->ts.type = BT_LOGICAL;
251 e->value.iokind = k;
252 e->where = *where;
253
254 return e;
255 }
256
257
258 /* Given an expression pointer, return a copy of the expression. This
259 subroutine is recursive. */
260
261 gfc_expr *
262 gfc_copy_expr (gfc_expr *p)
263 {
264 gfc_expr *q;
265 gfc_char_t *s;
266 char *c;
267
268 if (p == NULL)
269 return NULL;
270
271 q = gfc_get_expr ();
272 *q = *p;
273
274 switch (q->expr_type)
275 {
276 case EXPR_SUBSTRING:
277 s = gfc_get_wide_string (p->value.character.length + 1);
278 q->value.character.string = s;
279 memcpy (s, p->value.character.string,
280 (p->value.character.length + 1) * sizeof (gfc_char_t));
281 break;
282
283 case EXPR_CONSTANT:
284 /* Copy target representation, if it exists. */
285 if (p->representation.string)
286 {
287 c = XCNEWVEC (char, p->representation.length + 1);
288 q->representation.string = c;
289 memcpy (c, p->representation.string, (p->representation.length + 1));
290 }
291
292 /* Copy the values of any pointer components of p->value. */
293 switch (q->ts.type)
294 {
295 case BT_INTEGER:
296 mpz_init_set (q->value.integer, p->value.integer);
297 break;
298
299 case BT_REAL:
300 gfc_set_model_kind (q->ts.kind);
301 mpfr_init (q->value.real);
302 mpfr_set (q->value.real, p->value.real, GFC_RND_MODE);
303 break;
304
305 case BT_COMPLEX:
306 gfc_set_model_kind (q->ts.kind);
307 mpc_init2 (q->value.complex, mpfr_get_default_prec());
308 mpc_set (q->value.complex, p->value.complex, GFC_MPC_RND_MODE);
309 break;
310
311 case BT_CHARACTER:
312 if (p->representation.string)
313 q->value.character.string
314 = gfc_char_to_widechar (q->representation.string);
315 else
316 {
317 s = gfc_get_wide_string (p->value.character.length + 1);
318 q->value.character.string = s;
319
320 /* This is the case for the C_NULL_CHAR named constant. */
321 if (p->value.character.length == 0
322 && (p->ts.is_c_interop || p->ts.is_iso_c))
323 {
324 *s = '\0';
325 /* Need to set the length to 1 to make sure the NUL
326 terminator is copied. */
327 q->value.character.length = 1;
328 }
329 else
330 memcpy (s, p->value.character.string,
331 (p->value.character.length + 1) * sizeof (gfc_char_t));
332 }
333 break;
334
335 case BT_HOLLERITH:
336 case BT_LOGICAL:
337 case BT_DERIVED:
338 case BT_CLASS:
339 break; /* Already done. */
340
341 case BT_PROCEDURE:
342 case BT_VOID:
343 /* Should never be reached. */
344 case BT_UNKNOWN:
345 gfc_internal_error ("gfc_copy_expr(): Bad expr node");
346 /* Not reached. */
347 }
348
349 break;
350
351 case EXPR_OP:
352 switch (q->value.op.op)
353 {
354 case INTRINSIC_NOT:
355 case INTRINSIC_PARENTHESES:
356 case INTRINSIC_UPLUS:
357 case INTRINSIC_UMINUS:
358 q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
359 break;
360
361 default: /* Binary operators. */
362 q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
363 q->value.op.op2 = gfc_copy_expr (p->value.op.op2);
364 break;
365 }
366
367 break;
368
369 case EXPR_FUNCTION:
370 q->value.function.actual =
371 gfc_copy_actual_arglist (p->value.function.actual);
372 break;
373
374 case EXPR_COMPCALL:
375 case EXPR_PPC:
376 q->value.compcall.actual =
377 gfc_copy_actual_arglist (p->value.compcall.actual);
378 q->value.compcall.tbp = p->value.compcall.tbp;
379 break;
380
381 case EXPR_STRUCTURE:
382 case EXPR_ARRAY:
383 q->value.constructor = gfc_constructor_copy (p->value.constructor);
384 break;
385
386 case EXPR_VARIABLE:
387 case EXPR_NULL:
388 break;
389 }
390
391 q->shape = gfc_copy_shape (p->shape, p->rank);
392
393 q->ref = gfc_copy_ref (p->ref);
394
395 return q;
396 }
397
398
399 void
400 gfc_clear_shape (mpz_t *shape, int rank)
401 {
402 int i;
403
404 for (i = 0; i < rank; i++)
405 mpz_clear (shape[i]);
406 }
407
408
409 void
410 gfc_free_shape (mpz_t **shape, int rank)
411 {
412 gfc_clear_shape (*shape, rank);
413 free (*shape);
414 *shape = NULL;
415 }
416
417
418 /* Workhorse function for gfc_free_expr() that frees everything
419 beneath an expression node, but not the node itself. This is
420 useful when we want to simplify a node and replace it with
421 something else or the expression node belongs to another structure. */
422
423 static void
424 free_expr0 (gfc_expr *e)
425 {
426 switch (e->expr_type)
427 {
428 case EXPR_CONSTANT:
429 /* Free any parts of the value that need freeing. */
430 switch (e->ts.type)
431 {
432 case BT_INTEGER:
433 mpz_clear (e->value.integer);
434 break;
435
436 case BT_REAL:
437 mpfr_clear (e->value.real);
438 break;
439
440 case BT_CHARACTER:
441 free (e->value.character.string);
442 break;
443
444 case BT_COMPLEX:
445 mpc_clear (e->value.complex);
446 break;
447
448 default:
449 break;
450 }
451
452 /* Free the representation. */
453 free (e->representation.string);
454
455 break;
456
457 case EXPR_OP:
458 if (e->value.op.op1 != NULL)
459 gfc_free_expr (e->value.op.op1);
460 if (e->value.op.op2 != NULL)
461 gfc_free_expr (e->value.op.op2);
462 break;
463
464 case EXPR_FUNCTION:
465 gfc_free_actual_arglist (e->value.function.actual);
466 break;
467
468 case EXPR_COMPCALL:
469 case EXPR_PPC:
470 gfc_free_actual_arglist (e->value.compcall.actual);
471 break;
472
473 case EXPR_VARIABLE:
474 break;
475
476 case EXPR_ARRAY:
477 case EXPR_STRUCTURE:
478 gfc_constructor_free (e->value.constructor);
479 break;
480
481 case EXPR_SUBSTRING:
482 free (e->value.character.string);
483 break;
484
485 case EXPR_NULL:
486 break;
487
488 default:
489 gfc_internal_error ("free_expr0(): Bad expr type");
490 }
491
492 /* Free a shape array. */
493 if (e->shape != NULL)
494 gfc_free_shape (&e->shape, e->rank);
495
496 gfc_free_ref_list (e->ref);
497
498 memset (e, '\0', sizeof (gfc_expr));
499 }
500
501
502 /* Free an expression node and everything beneath it. */
503
504 void
505 gfc_free_expr (gfc_expr *e)
506 {
507 if (e == NULL)
508 return;
509 free_expr0 (e);
510 free (e);
511 }
512
513
514 /* Free an argument list and everything below it. */
515
516 void
517 gfc_free_actual_arglist (gfc_actual_arglist *a1)
518 {
519 gfc_actual_arglist *a2;
520
521 while (a1)
522 {
523 a2 = a1->next;
524 gfc_free_expr (a1->expr);
525 free (a1);
526 a1 = a2;
527 }
528 }
529
530
531 /* Copy an arglist structure and all of the arguments. */
532
533 gfc_actual_arglist *
534 gfc_copy_actual_arglist (gfc_actual_arglist *p)
535 {
536 gfc_actual_arglist *head, *tail, *new_arg;
537
538 head = tail = NULL;
539
540 for (; p; p = p->next)
541 {
542 new_arg = gfc_get_actual_arglist ();
543 *new_arg = *p;
544
545 new_arg->expr = gfc_copy_expr (p->expr);
546 new_arg->next = NULL;
547
548 if (head == NULL)
549 head = new_arg;
550 else
551 tail->next = new_arg;
552
553 tail = new_arg;
554 }
555
556 return head;
557 }
558
559
560 /* Free a list of reference structures. */
561
562 void
563 gfc_free_ref_list (gfc_ref *p)
564 {
565 gfc_ref *q;
566 int i;
567
568 for (; p; p = q)
569 {
570 q = p->next;
571
572 switch (p->type)
573 {
574 case REF_ARRAY:
575 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
576 {
577 gfc_free_expr (p->u.ar.start[i]);
578 gfc_free_expr (p->u.ar.end[i]);
579 gfc_free_expr (p->u.ar.stride[i]);
580 }
581
582 break;
583
584 case REF_SUBSTRING:
585 gfc_free_expr (p->u.ss.start);
586 gfc_free_expr (p->u.ss.end);
587 break;
588
589 case REF_COMPONENT:
590 break;
591 }
592
593 free (p);
594 }
595 }
596
597
598 /* Graft the *src expression onto the *dest subexpression. */
599
600 void
601 gfc_replace_expr (gfc_expr *dest, gfc_expr *src)
602 {
603 free_expr0 (dest);
604 *dest = *src;
605 free (src);
606 }
607
608
609 /* Try to extract an integer constant from the passed expression node.
610 Returns an error message or NULL if the result is set. It is
611 tempting to generate an error and return SUCCESS or FAILURE, but
612 failure is OK for some callers. */
613
614 const char *
615 gfc_extract_int (gfc_expr *expr, int *result)
616 {
617 if (expr->expr_type != EXPR_CONSTANT)
618 return _("Constant expression required at %C");
619
620 if (expr->ts.type != BT_INTEGER)
621 return _("Integer expression required at %C");
622
623 if ((mpz_cmp_si (expr->value.integer, INT_MAX) > 0)
624 || (mpz_cmp_si (expr->value.integer, INT_MIN) < 0))
625 {
626 return _("Integer value too large in expression at %C");
627 }
628
629 *result = (int) mpz_get_si (expr->value.integer);
630
631 return NULL;
632 }
633
634
635 /* Recursively copy a list of reference structures. */
636
637 gfc_ref *
638 gfc_copy_ref (gfc_ref *src)
639 {
640 gfc_array_ref *ar;
641 gfc_ref *dest;
642
643 if (src == NULL)
644 return NULL;
645
646 dest = gfc_get_ref ();
647 dest->type = src->type;
648
649 switch (src->type)
650 {
651 case REF_ARRAY:
652 ar = gfc_copy_array_ref (&src->u.ar);
653 dest->u.ar = *ar;
654 free (ar);
655 break;
656
657 case REF_COMPONENT:
658 dest->u.c = src->u.c;
659 break;
660
661 case REF_SUBSTRING:
662 dest->u.ss = src->u.ss;
663 dest->u.ss.start = gfc_copy_expr (src->u.ss.start);
664 dest->u.ss.end = gfc_copy_expr (src->u.ss.end);
665 break;
666 }
667
668 dest->next = gfc_copy_ref (src->next);
669
670 return dest;
671 }
672
673
674 /* Detect whether an expression has any vector index array references. */
675
676 int
677 gfc_has_vector_index (gfc_expr *e)
678 {
679 gfc_ref *ref;
680 int i;
681 for (ref = e->ref; ref; ref = ref->next)
682 if (ref->type == REF_ARRAY)
683 for (i = 0; i < ref->u.ar.dimen; i++)
684 if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
685 return 1;
686 return 0;
687 }
688
689
690 /* Copy a shape array. */
691
692 mpz_t *
693 gfc_copy_shape (mpz_t *shape, int rank)
694 {
695 mpz_t *new_shape;
696 int n;
697
698 if (shape == NULL)
699 return NULL;
700
701 new_shape = gfc_get_shape (rank);
702
703 for (n = 0; n < rank; n++)
704 mpz_init_set (new_shape[n], shape[n]);
705
706 return new_shape;
707 }
708
709
710 /* Copy a shape array excluding dimension N, where N is an integer
711 constant expression. Dimensions are numbered in fortran style --
712 starting with ONE.
713
714 So, if the original shape array contains R elements
715 { s1 ... sN-1 sN sN+1 ... sR-1 sR}
716 the result contains R-1 elements:
717 { s1 ... sN-1 sN+1 ... sR-1}
718
719 If anything goes wrong -- N is not a constant, its value is out
720 of range -- or anything else, just returns NULL. */
721
722 mpz_t *
723 gfc_copy_shape_excluding (mpz_t *shape, int rank, gfc_expr *dim)
724 {
725 mpz_t *new_shape, *s;
726 int i, n;
727
728 if (shape == NULL
729 || rank <= 1
730 || dim == NULL
731 || dim->expr_type != EXPR_CONSTANT
732 || dim->ts.type != BT_INTEGER)
733 return NULL;
734
735 n = mpz_get_si (dim->value.integer);
736 n--; /* Convert to zero based index. */
737 if (n < 0 || n >= rank)
738 return NULL;
739
740 s = new_shape = gfc_get_shape (rank - 1);
741
742 for (i = 0; i < rank; i++)
743 {
744 if (i == n)
745 continue;
746 mpz_init_set (*s, shape[i]);
747 s++;
748 }
749
750 return new_shape;
751 }
752
753
754 /* Return the maximum kind of two expressions. In general, higher
755 kind numbers mean more precision for numeric types. */
756
757 int
758 gfc_kind_max (gfc_expr *e1, gfc_expr *e2)
759 {
760 return (e1->ts.kind > e2->ts.kind) ? e1->ts.kind : e2->ts.kind;
761 }
762
763
764 /* Returns nonzero if the type is numeric, zero otherwise. */
765
766 static int
767 numeric_type (bt type)
768 {
769 return type == BT_COMPLEX || type == BT_REAL || type == BT_INTEGER;
770 }
771
772
773 /* Returns nonzero if the typespec is a numeric type, zero otherwise. */
774
775 int
776 gfc_numeric_ts (gfc_typespec *ts)
777 {
778 return numeric_type (ts->type);
779 }
780
781
782 /* Return an expression node with an optional argument list attached.
783 A variable number of gfc_expr pointers are strung together in an
784 argument list with a NULL pointer terminating the list. */
785
786 gfc_expr *
787 gfc_build_conversion (gfc_expr *e)
788 {
789 gfc_expr *p;
790
791 p = gfc_get_expr ();
792 p->expr_type = EXPR_FUNCTION;
793 p->symtree = NULL;
794 p->value.function.actual = NULL;
795
796 p->value.function.actual = gfc_get_actual_arglist ();
797 p->value.function.actual->expr = e;
798
799 return p;
800 }
801
802
803 /* Given an expression node with some sort of numeric binary
804 expression, insert type conversions required to make the operands
805 have the same type. Conversion warnings are disabled if wconversion
806 is set to 0.
807
808 The exception is that the operands of an exponential don't have to
809 have the same type. If possible, the base is promoted to the type
810 of the exponent. For example, 1**2.3 becomes 1.0**2.3, but
811 1.0**2 stays as it is. */
812
813 void
814 gfc_type_convert_binary (gfc_expr *e, int wconversion)
815 {
816 gfc_expr *op1, *op2;
817
818 op1 = e->value.op.op1;
819 op2 = e->value.op.op2;
820
821 if (op1->ts.type == BT_UNKNOWN || op2->ts.type == BT_UNKNOWN)
822 {
823 gfc_clear_ts (&e->ts);
824 return;
825 }
826
827 /* Kind conversions of same type. */
828 if (op1->ts.type == op2->ts.type)
829 {
830 if (op1->ts.kind == op2->ts.kind)
831 {
832 /* No type conversions. */
833 e->ts = op1->ts;
834 goto done;
835 }
836
837 if (op1->ts.kind > op2->ts.kind)
838 gfc_convert_type_warn (op2, &op1->ts, 2, wconversion);
839 else
840 gfc_convert_type_warn (op1, &op2->ts, 2, wconversion);
841
842 e->ts = op1->ts;
843 goto done;
844 }
845
846 /* Integer combined with real or complex. */
847 if (op2->ts.type == BT_INTEGER)
848 {
849 e->ts = op1->ts;
850
851 /* Special case for ** operator. */
852 if (e->value.op.op == INTRINSIC_POWER)
853 goto done;
854
855 gfc_convert_type_warn (e->value.op.op2, &e->ts, 2, wconversion);
856 goto done;
857 }
858
859 if (op1->ts.type == BT_INTEGER)
860 {
861 e->ts = op2->ts;
862 gfc_convert_type_warn (e->value.op.op1, &e->ts, 2, wconversion);
863 goto done;
864 }
865
866 /* Real combined with complex. */
867 e->ts.type = BT_COMPLEX;
868 if (op1->ts.kind > op2->ts.kind)
869 e->ts.kind = op1->ts.kind;
870 else
871 e->ts.kind = op2->ts.kind;
872 if (op1->ts.type != BT_COMPLEX || op1->ts.kind != e->ts.kind)
873 gfc_convert_type_warn (e->value.op.op1, &e->ts, 2, wconversion);
874 if (op2->ts.type != BT_COMPLEX || op2->ts.kind != e->ts.kind)
875 gfc_convert_type_warn (e->value.op.op2, &e->ts, 2, wconversion);
876
877 done:
878 return;
879 }
880
881
882 /* Function to determine if an expression is constant or not. This
883 function expects that the expression has already been simplified. */
884
885 int
886 gfc_is_constant_expr (gfc_expr *e)
887 {
888 gfc_constructor *c;
889 gfc_actual_arglist *arg;
890 gfc_symbol *sym;
891
892 if (e == NULL)
893 return 1;
894
895 switch (e->expr_type)
896 {
897 case EXPR_OP:
898 return (gfc_is_constant_expr (e->value.op.op1)
899 && (e->value.op.op2 == NULL
900 || gfc_is_constant_expr (e->value.op.op2)));
901
902 case EXPR_VARIABLE:
903 return 0;
904
905 case EXPR_FUNCTION:
906 case EXPR_PPC:
907 case EXPR_COMPCALL:
908 gcc_assert (e->symtree || e->value.function.esym
909 || e->value.function.isym);
910
911 /* Call to intrinsic with at least one argument. */
912 if (e->value.function.isym && e->value.function.actual)
913 {
914 for (arg = e->value.function.actual; arg; arg = arg->next)
915 if (!gfc_is_constant_expr (arg->expr))
916 return 0;
917 }
918
919 /* Specification functions are constant. */
920 /* F95, 7.1.6.2; F2003, 7.1.7 */
921 sym = NULL;
922 if (e->symtree)
923 sym = e->symtree->n.sym;
924 if (e->value.function.esym)
925 sym = e->value.function.esym;
926
927 if (sym
928 && sym->attr.function
929 && sym->attr.pure
930 && !sym->attr.intrinsic
931 && !sym->attr.recursive
932 && sym->attr.proc != PROC_INTERNAL
933 && sym->attr.proc != PROC_ST_FUNCTION
934 && sym->attr.proc != PROC_UNKNOWN
935 && sym->formal == NULL)
936 return 1;
937
938 if (e->value.function.isym
939 && (e->value.function.isym->elemental
940 || e->value.function.isym->pure
941 || e->value.function.isym->inquiry
942 || e->value.function.isym->transformational))
943 return 1;
944
945 return 0;
946
947 case EXPR_CONSTANT:
948 case EXPR_NULL:
949 return 1;
950
951 case EXPR_SUBSTRING:
952 return e->ref == NULL || (gfc_is_constant_expr (e->ref->u.ss.start)
953 && gfc_is_constant_expr (e->ref->u.ss.end));
954
955 case EXPR_ARRAY:
956 case EXPR_STRUCTURE:
957 c = gfc_constructor_first (e->value.constructor);
958 if ((e->expr_type == EXPR_ARRAY) && c && c->iterator)
959 return gfc_constant_ac (e);
960
961 for (; c; c = gfc_constructor_next (c))
962 if (!gfc_is_constant_expr (c->expr))
963 return 0;
964
965 return 1;
966
967
968 default:
969 gfc_internal_error ("gfc_is_constant_expr(): Unknown expression type");
970 return 0;
971 }
972 }
973
974
975 /* Is true if an array reference is followed by a component or substring
976 reference. */
977 bool
978 is_subref_array (gfc_expr * e)
979 {
980 gfc_ref * ref;
981 bool seen_array;
982
983 if (e->expr_type != EXPR_VARIABLE)
984 return false;
985
986 if (e->symtree->n.sym->attr.subref_array_pointer)
987 return true;
988
989 seen_array = false;
990 for (ref = e->ref; ref; ref = ref->next)
991 {
992 if (ref->type == REF_ARRAY
993 && ref->u.ar.type != AR_ELEMENT)
994 seen_array = true;
995
996 if (seen_array
997 && ref->type != REF_ARRAY)
998 return seen_array;
999 }
1000 return false;
1001 }
1002
1003
1004 /* Try to collapse intrinsic expressions. */
1005
1006 static gfc_try
1007 simplify_intrinsic_op (gfc_expr *p, int type)
1008 {
1009 gfc_intrinsic_op op;
1010 gfc_expr *op1, *op2, *result;
1011
1012 if (p->value.op.op == INTRINSIC_USER)
1013 return SUCCESS;
1014
1015 op1 = p->value.op.op1;
1016 op2 = p->value.op.op2;
1017 op = p->value.op.op;
1018
1019 if (gfc_simplify_expr (op1, type) == FAILURE)
1020 return FAILURE;
1021 if (gfc_simplify_expr (op2, type) == FAILURE)
1022 return FAILURE;
1023
1024 if (!gfc_is_constant_expr (op1)
1025 || (op2 != NULL && !gfc_is_constant_expr (op2)))
1026 return SUCCESS;
1027
1028 /* Rip p apart. */
1029 p->value.op.op1 = NULL;
1030 p->value.op.op2 = NULL;
1031
1032 switch (op)
1033 {
1034 case INTRINSIC_PARENTHESES:
1035 result = gfc_parentheses (op1);
1036 break;
1037
1038 case INTRINSIC_UPLUS:
1039 result = gfc_uplus (op1);
1040 break;
1041
1042 case INTRINSIC_UMINUS:
1043 result = gfc_uminus (op1);
1044 break;
1045
1046 case INTRINSIC_PLUS:
1047 result = gfc_add (op1, op2);
1048 break;
1049
1050 case INTRINSIC_MINUS:
1051 result = gfc_subtract (op1, op2);
1052 break;
1053
1054 case INTRINSIC_TIMES:
1055 result = gfc_multiply (op1, op2);
1056 break;
1057
1058 case INTRINSIC_DIVIDE:
1059 result = gfc_divide (op1, op2);
1060 break;
1061
1062 case INTRINSIC_POWER:
1063 result = gfc_power (op1, op2);
1064 break;
1065
1066 case INTRINSIC_CONCAT:
1067 result = gfc_concat (op1, op2);
1068 break;
1069
1070 case INTRINSIC_EQ:
1071 case INTRINSIC_EQ_OS:
1072 result = gfc_eq (op1, op2, op);
1073 break;
1074
1075 case INTRINSIC_NE:
1076 case INTRINSIC_NE_OS:
1077 result = gfc_ne (op1, op2, op);
1078 break;
1079
1080 case INTRINSIC_GT:
1081 case INTRINSIC_GT_OS:
1082 result = gfc_gt (op1, op2, op);
1083 break;
1084
1085 case INTRINSIC_GE:
1086 case INTRINSIC_GE_OS:
1087 result = gfc_ge (op1, op2, op);
1088 break;
1089
1090 case INTRINSIC_LT:
1091 case INTRINSIC_LT_OS:
1092 result = gfc_lt (op1, op2, op);
1093 break;
1094
1095 case INTRINSIC_LE:
1096 case INTRINSIC_LE_OS:
1097 result = gfc_le (op1, op2, op);
1098 break;
1099
1100 case INTRINSIC_NOT:
1101 result = gfc_not (op1);
1102 break;
1103
1104 case INTRINSIC_AND:
1105 result = gfc_and (op1, op2);
1106 break;
1107
1108 case INTRINSIC_OR:
1109 result = gfc_or (op1, op2);
1110 break;
1111
1112 case INTRINSIC_EQV:
1113 result = gfc_eqv (op1, op2);
1114 break;
1115
1116 case INTRINSIC_NEQV:
1117 result = gfc_neqv (op1, op2);
1118 break;
1119
1120 default:
1121 gfc_internal_error ("simplify_intrinsic_op(): Bad operator");
1122 }
1123
1124 if (result == NULL)
1125 {
1126 gfc_free_expr (op1);
1127 gfc_free_expr (op2);
1128 return FAILURE;
1129 }
1130
1131 result->rank = p->rank;
1132 result->where = p->where;
1133 gfc_replace_expr (p, result);
1134
1135 return SUCCESS;
1136 }
1137
1138
1139 /* Subroutine to simplify constructor expressions. Mutually recursive
1140 with gfc_simplify_expr(). */
1141
1142 static gfc_try
1143 simplify_constructor (gfc_constructor_base base, int type)
1144 {
1145 gfc_constructor *c;
1146 gfc_expr *p;
1147
1148 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1149 {
1150 if (c->iterator
1151 && (gfc_simplify_expr (c->iterator->start, type) == FAILURE
1152 || gfc_simplify_expr (c->iterator->end, type) == FAILURE
1153 || gfc_simplify_expr (c->iterator->step, type) == FAILURE))
1154 return FAILURE;
1155
1156 if (c->expr)
1157 {
1158 /* Try and simplify a copy. Replace the original if successful
1159 but keep going through the constructor at all costs. Not
1160 doing so can make a dog's dinner of complicated things. */
1161 p = gfc_copy_expr (c->expr);
1162
1163 if (gfc_simplify_expr (p, type) == FAILURE)
1164 {
1165 gfc_free_expr (p);
1166 continue;
1167 }
1168
1169 gfc_replace_expr (c->expr, p);
1170 }
1171 }
1172
1173 return SUCCESS;
1174 }
1175
1176
1177 /* Pull a single array element out of an array constructor. */
1178
1179 static gfc_try
1180 find_array_element (gfc_constructor_base base, gfc_array_ref *ar,
1181 gfc_constructor **rval)
1182 {
1183 unsigned long nelemen;
1184 int i;
1185 mpz_t delta;
1186 mpz_t offset;
1187 mpz_t span;
1188 mpz_t tmp;
1189 gfc_constructor *cons;
1190 gfc_expr *e;
1191 gfc_try t;
1192
1193 t = SUCCESS;
1194 e = NULL;
1195
1196 mpz_init_set_ui (offset, 0);
1197 mpz_init (delta);
1198 mpz_init (tmp);
1199 mpz_init_set_ui (span, 1);
1200 for (i = 0; i < ar->dimen; i++)
1201 {
1202 if (gfc_reduce_init_expr (ar->as->lower[i]) == FAILURE
1203 || gfc_reduce_init_expr (ar->as->upper[i]) == FAILURE)
1204 {
1205 t = FAILURE;
1206 cons = NULL;
1207 goto depart;
1208 }
1209
1210 e = gfc_copy_expr (ar->start[i]);
1211 if (e->expr_type != EXPR_CONSTANT)
1212 {
1213 cons = NULL;
1214 goto depart;
1215 }
1216
1217 gcc_assert (ar->as->upper[i]->expr_type == EXPR_CONSTANT
1218 && ar->as->lower[i]->expr_type == EXPR_CONSTANT);
1219
1220 /* Check the bounds. */
1221 if ((ar->as->upper[i]
1222 && mpz_cmp (e->value.integer,
1223 ar->as->upper[i]->value.integer) > 0)
1224 || (mpz_cmp (e->value.integer,
1225 ar->as->lower[i]->value.integer) < 0))
1226 {
1227 gfc_error ("Index in dimension %d is out of bounds "
1228 "at %L", i + 1, &ar->c_where[i]);
1229 cons = NULL;
1230 t = FAILURE;
1231 goto depart;
1232 }
1233
1234 mpz_sub (delta, e->value.integer, ar->as->lower[i]->value.integer);
1235 mpz_mul (delta, delta, span);
1236 mpz_add (offset, offset, delta);
1237
1238 mpz_set_ui (tmp, 1);
1239 mpz_add (tmp, tmp, ar->as->upper[i]->value.integer);
1240 mpz_sub (tmp, tmp, ar->as->lower[i]->value.integer);
1241 mpz_mul (span, span, tmp);
1242 }
1243
1244 for (cons = gfc_constructor_first (base), nelemen = mpz_get_ui (offset);
1245 cons && nelemen > 0; cons = gfc_constructor_next (cons), nelemen--)
1246 {
1247 if (cons->iterator)
1248 {
1249 cons = NULL;
1250 goto depart;
1251 }
1252 }
1253
1254 depart:
1255 mpz_clear (delta);
1256 mpz_clear (offset);
1257 mpz_clear (span);
1258 mpz_clear (tmp);
1259 if (e)
1260 gfc_free_expr (e);
1261 *rval = cons;
1262 return t;
1263 }
1264
1265
1266 /* Find a component of a structure constructor. */
1267
1268 static gfc_constructor *
1269 find_component_ref (gfc_constructor_base base, gfc_ref *ref)
1270 {
1271 gfc_component *comp;
1272 gfc_component *pick;
1273 gfc_constructor *c = gfc_constructor_first (base);
1274
1275 comp = ref->u.c.sym->components;
1276 pick = ref->u.c.component;
1277 while (comp != pick)
1278 {
1279 comp = comp->next;
1280 c = gfc_constructor_next (c);
1281 }
1282
1283 return c;
1284 }
1285
1286
1287 /* Replace an expression with the contents of a constructor, removing
1288 the subobject reference in the process. */
1289
1290 static void
1291 remove_subobject_ref (gfc_expr *p, gfc_constructor *cons)
1292 {
1293 gfc_expr *e;
1294
1295 if (cons)
1296 {
1297 e = cons->expr;
1298 cons->expr = NULL;
1299 }
1300 else
1301 e = gfc_copy_expr (p);
1302 e->ref = p->ref->next;
1303 p->ref->next = NULL;
1304 gfc_replace_expr (p, e);
1305 }
1306
1307
1308 /* Pull an array section out of an array constructor. */
1309
1310 static gfc_try
1311 find_array_section (gfc_expr *expr, gfc_ref *ref)
1312 {
1313 int idx;
1314 int rank;
1315 int d;
1316 int shape_i;
1317 int limit;
1318 long unsigned one = 1;
1319 bool incr_ctr;
1320 mpz_t start[GFC_MAX_DIMENSIONS];
1321 mpz_t end[GFC_MAX_DIMENSIONS];
1322 mpz_t stride[GFC_MAX_DIMENSIONS];
1323 mpz_t delta[GFC_MAX_DIMENSIONS];
1324 mpz_t ctr[GFC_MAX_DIMENSIONS];
1325 mpz_t delta_mpz;
1326 mpz_t tmp_mpz;
1327 mpz_t nelts;
1328 mpz_t ptr;
1329 gfc_constructor_base base;
1330 gfc_constructor *cons, *vecsub[GFC_MAX_DIMENSIONS];
1331 gfc_expr *begin;
1332 gfc_expr *finish;
1333 gfc_expr *step;
1334 gfc_expr *upper;
1335 gfc_expr *lower;
1336 gfc_try t;
1337
1338 t = SUCCESS;
1339
1340 base = expr->value.constructor;
1341 expr->value.constructor = NULL;
1342
1343 rank = ref->u.ar.as->rank;
1344
1345 if (expr->shape == NULL)
1346 expr->shape = gfc_get_shape (rank);
1347
1348 mpz_init_set_ui (delta_mpz, one);
1349 mpz_init_set_ui (nelts, one);
1350 mpz_init (tmp_mpz);
1351
1352 /* Do the initialization now, so that we can cleanup without
1353 keeping track of where we were. */
1354 for (d = 0; d < rank; d++)
1355 {
1356 mpz_init (delta[d]);
1357 mpz_init (start[d]);
1358 mpz_init (end[d]);
1359 mpz_init (ctr[d]);
1360 mpz_init (stride[d]);
1361 vecsub[d] = NULL;
1362 }
1363
1364 /* Build the counters to clock through the array reference. */
1365 shape_i = 0;
1366 for (d = 0; d < rank; d++)
1367 {
1368 /* Make this stretch of code easier on the eye! */
1369 begin = ref->u.ar.start[d];
1370 finish = ref->u.ar.end[d];
1371 step = ref->u.ar.stride[d];
1372 lower = ref->u.ar.as->lower[d];
1373 upper = ref->u.ar.as->upper[d];
1374
1375 if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR) /* Vector subscript. */
1376 {
1377 gfc_constructor *ci;
1378 gcc_assert (begin);
1379
1380 if (begin->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (begin))
1381 {
1382 t = FAILURE;
1383 goto cleanup;
1384 }
1385
1386 gcc_assert (begin->rank == 1);
1387 /* Zero-sized arrays have no shape and no elements, stop early. */
1388 if (!begin->shape)
1389 {
1390 mpz_init_set_ui (nelts, 0);
1391 break;
1392 }
1393
1394 vecsub[d] = gfc_constructor_first (begin->value.constructor);
1395 mpz_set (ctr[d], vecsub[d]->expr->value.integer);
1396 mpz_mul (nelts, nelts, begin->shape[0]);
1397 mpz_set (expr->shape[shape_i++], begin->shape[0]);
1398
1399 /* Check bounds. */
1400 for (ci = vecsub[d]; ci; ci = gfc_constructor_next (ci))
1401 {
1402 if (mpz_cmp (ci->expr->value.integer, upper->value.integer) > 0
1403 || mpz_cmp (ci->expr->value.integer,
1404 lower->value.integer) < 0)
1405 {
1406 gfc_error ("index in dimension %d is out of bounds "
1407 "at %L", d + 1, &ref->u.ar.c_where[d]);
1408 t = FAILURE;
1409 goto cleanup;
1410 }
1411 }
1412 }
1413 else
1414 {
1415 if ((begin && begin->expr_type != EXPR_CONSTANT)
1416 || (finish && finish->expr_type != EXPR_CONSTANT)
1417 || (step && step->expr_type != EXPR_CONSTANT))
1418 {
1419 t = FAILURE;
1420 goto cleanup;
1421 }
1422
1423 /* Obtain the stride. */
1424 if (step)
1425 mpz_set (stride[d], step->value.integer);
1426 else
1427 mpz_set_ui (stride[d], one);
1428
1429 if (mpz_cmp_ui (stride[d], 0) == 0)
1430 mpz_set_ui (stride[d], one);
1431
1432 /* Obtain the start value for the index. */
1433 if (begin)
1434 mpz_set (start[d], begin->value.integer);
1435 else
1436 mpz_set (start[d], lower->value.integer);
1437
1438 mpz_set (ctr[d], start[d]);
1439
1440 /* Obtain the end value for the index. */
1441 if (finish)
1442 mpz_set (end[d], finish->value.integer);
1443 else
1444 mpz_set (end[d], upper->value.integer);
1445
1446 /* Separate 'if' because elements sometimes arrive with
1447 non-null end. */
1448 if (ref->u.ar.dimen_type[d] == DIMEN_ELEMENT)
1449 mpz_set (end [d], begin->value.integer);
1450
1451 /* Check the bounds. */
1452 if (mpz_cmp (ctr[d], upper->value.integer) > 0
1453 || mpz_cmp (end[d], upper->value.integer) > 0
1454 || mpz_cmp (ctr[d], lower->value.integer) < 0
1455 || mpz_cmp (end[d], lower->value.integer) < 0)
1456 {
1457 gfc_error ("index in dimension %d is out of bounds "
1458 "at %L", d + 1, &ref->u.ar.c_where[d]);
1459 t = FAILURE;
1460 goto cleanup;
1461 }
1462
1463 /* Calculate the number of elements and the shape. */
1464 mpz_set (tmp_mpz, stride[d]);
1465 mpz_add (tmp_mpz, end[d], tmp_mpz);
1466 mpz_sub (tmp_mpz, tmp_mpz, ctr[d]);
1467 mpz_div (tmp_mpz, tmp_mpz, stride[d]);
1468 mpz_mul (nelts, nelts, tmp_mpz);
1469
1470 /* An element reference reduces the rank of the expression; don't
1471 add anything to the shape array. */
1472 if (ref->u.ar.dimen_type[d] != DIMEN_ELEMENT)
1473 mpz_set (expr->shape[shape_i++], tmp_mpz);
1474 }
1475
1476 /* Calculate the 'stride' (=delta) for conversion of the
1477 counter values into the index along the constructor. */
1478 mpz_set (delta[d], delta_mpz);
1479 mpz_sub (tmp_mpz, upper->value.integer, lower->value.integer);
1480 mpz_add_ui (tmp_mpz, tmp_mpz, one);
1481 mpz_mul (delta_mpz, delta_mpz, tmp_mpz);
1482 }
1483
1484 mpz_init (ptr);
1485 cons = gfc_constructor_first (base);
1486
1487 /* Now clock through the array reference, calculating the index in
1488 the source constructor and transferring the elements to the new
1489 constructor. */
1490 for (idx = 0; idx < (int) mpz_get_si (nelts); idx++)
1491 {
1492 if (ref->u.ar.offset)
1493 mpz_set (ptr, ref->u.ar.offset->value.integer);
1494 else
1495 mpz_init_set_ui (ptr, 0);
1496
1497 incr_ctr = true;
1498 for (d = 0; d < rank; d++)
1499 {
1500 mpz_set (tmp_mpz, ctr[d]);
1501 mpz_sub (tmp_mpz, tmp_mpz, ref->u.ar.as->lower[d]->value.integer);
1502 mpz_mul (tmp_mpz, tmp_mpz, delta[d]);
1503 mpz_add (ptr, ptr, tmp_mpz);
1504
1505 if (!incr_ctr) continue;
1506
1507 if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR) /* Vector subscript. */
1508 {
1509 gcc_assert(vecsub[d]);
1510
1511 if (!gfc_constructor_next (vecsub[d]))
1512 vecsub[d] = gfc_constructor_first (ref->u.ar.start[d]->value.constructor);
1513 else
1514 {
1515 vecsub[d] = gfc_constructor_next (vecsub[d]);
1516 incr_ctr = false;
1517 }
1518 mpz_set (ctr[d], vecsub[d]->expr->value.integer);
1519 }
1520 else
1521 {
1522 mpz_add (ctr[d], ctr[d], stride[d]);
1523
1524 if (mpz_cmp_ui (stride[d], 0) > 0
1525 ? mpz_cmp (ctr[d], end[d]) > 0
1526 : mpz_cmp (ctr[d], end[d]) < 0)
1527 mpz_set (ctr[d], start[d]);
1528 else
1529 incr_ctr = false;
1530 }
1531 }
1532
1533 limit = mpz_get_ui (ptr);
1534 if (limit >= gfc_option.flag_max_array_constructor)
1535 {
1536 gfc_error ("The number of elements in the array constructor "
1537 "at %L requires an increase of the allowed %d "
1538 "upper limit. See -fmax-array-constructor "
1539 "option", &expr->where,
1540 gfc_option.flag_max_array_constructor);
1541 return FAILURE;
1542 }
1543
1544 cons = gfc_constructor_lookup (base, limit);
1545 gcc_assert (cons);
1546 gfc_constructor_append_expr (&expr->value.constructor,
1547 gfc_copy_expr (cons->expr), NULL);
1548 }
1549
1550 mpz_clear (ptr);
1551
1552 cleanup:
1553
1554 mpz_clear (delta_mpz);
1555 mpz_clear (tmp_mpz);
1556 mpz_clear (nelts);
1557 for (d = 0; d < rank; d++)
1558 {
1559 mpz_clear (delta[d]);
1560 mpz_clear (start[d]);
1561 mpz_clear (end[d]);
1562 mpz_clear (ctr[d]);
1563 mpz_clear (stride[d]);
1564 }
1565 gfc_constructor_free (base);
1566 return t;
1567 }
1568
1569 /* Pull a substring out of an expression. */
1570
1571 static gfc_try
1572 find_substring_ref (gfc_expr *p, gfc_expr **newp)
1573 {
1574 int end;
1575 int start;
1576 int length;
1577 gfc_char_t *chr;
1578
1579 if (p->ref->u.ss.start->expr_type != EXPR_CONSTANT
1580 || p->ref->u.ss.end->expr_type != EXPR_CONSTANT)
1581 return FAILURE;
1582
1583 *newp = gfc_copy_expr (p);
1584 free ((*newp)->value.character.string);
1585
1586 end = (int) mpz_get_ui (p->ref->u.ss.end->value.integer);
1587 start = (int) mpz_get_ui (p->ref->u.ss.start->value.integer);
1588 length = end - start + 1;
1589
1590 chr = (*newp)->value.character.string = gfc_get_wide_string (length + 1);
1591 (*newp)->value.character.length = length;
1592 memcpy (chr, &p->value.character.string[start - 1],
1593 length * sizeof (gfc_char_t));
1594 chr[length] = '\0';
1595 return SUCCESS;
1596 }
1597
1598
1599
1600 /* Simplify a subobject reference of a constructor. This occurs when
1601 parameter variable values are substituted. */
1602
1603 static gfc_try
1604 simplify_const_ref (gfc_expr *p)
1605 {
1606 gfc_constructor *cons, *c;
1607 gfc_expr *newp;
1608 gfc_ref *last_ref;
1609
1610 while (p->ref)
1611 {
1612 switch (p->ref->type)
1613 {
1614 case REF_ARRAY:
1615 switch (p->ref->u.ar.type)
1616 {
1617 case AR_ELEMENT:
1618 /* <type/kind spec>, parameter :: x(<int>) = scalar_expr
1619 will generate this. */
1620 if (p->expr_type != EXPR_ARRAY)
1621 {
1622 remove_subobject_ref (p, NULL);
1623 break;
1624 }
1625 if (find_array_element (p->value.constructor, &p->ref->u.ar,
1626 &cons) == FAILURE)
1627 return FAILURE;
1628
1629 if (!cons)
1630 return SUCCESS;
1631
1632 remove_subobject_ref (p, cons);
1633 break;
1634
1635 case AR_SECTION:
1636 if (find_array_section (p, p->ref) == FAILURE)
1637 return FAILURE;
1638 p->ref->u.ar.type = AR_FULL;
1639
1640 /* Fall through. */
1641
1642 case AR_FULL:
1643 if (p->ref->next != NULL
1644 && (p->ts.type == BT_CHARACTER || p->ts.type == BT_DERIVED))
1645 {
1646 for (c = gfc_constructor_first (p->value.constructor);
1647 c; c = gfc_constructor_next (c))
1648 {
1649 c->expr->ref = gfc_copy_ref (p->ref->next);
1650 if (simplify_const_ref (c->expr) == FAILURE)
1651 return FAILURE;
1652 }
1653
1654 if (p->ts.type == BT_DERIVED
1655 && p->ref->next
1656 && (c = gfc_constructor_first (p->value.constructor)))
1657 {
1658 /* There may have been component references. */
1659 p->ts = c->expr->ts;
1660 }
1661
1662 last_ref = p->ref;
1663 for (; last_ref->next; last_ref = last_ref->next) {};
1664
1665 if (p->ts.type == BT_CHARACTER
1666 && last_ref->type == REF_SUBSTRING)
1667 {
1668 /* If this is a CHARACTER array and we possibly took
1669 a substring out of it, update the type-spec's
1670 character length according to the first element
1671 (as all should have the same length). */
1672 int string_len;
1673 if ((c = gfc_constructor_first (p->value.constructor)))
1674 {
1675 const gfc_expr* first = c->expr;
1676 gcc_assert (first->expr_type == EXPR_CONSTANT);
1677 gcc_assert (first->ts.type == BT_CHARACTER);
1678 string_len = first->value.character.length;
1679 }
1680 else
1681 string_len = 0;
1682
1683 if (!p->ts.u.cl)
1684 p->ts.u.cl = gfc_new_charlen (p->symtree->n.sym->ns,
1685 NULL);
1686 else
1687 gfc_free_expr (p->ts.u.cl->length);
1688
1689 p->ts.u.cl->length
1690 = gfc_get_int_expr (gfc_default_integer_kind,
1691 NULL, string_len);
1692 }
1693 }
1694 gfc_free_ref_list (p->ref);
1695 p->ref = NULL;
1696 break;
1697
1698 default:
1699 return SUCCESS;
1700 }
1701
1702 break;
1703
1704 case REF_COMPONENT:
1705 cons = find_component_ref (p->value.constructor, p->ref);
1706 remove_subobject_ref (p, cons);
1707 break;
1708
1709 case REF_SUBSTRING:
1710 if (find_substring_ref (p, &newp) == FAILURE)
1711 return FAILURE;
1712
1713 gfc_replace_expr (p, newp);
1714 gfc_free_ref_list (p->ref);
1715 p->ref = NULL;
1716 break;
1717 }
1718 }
1719
1720 return SUCCESS;
1721 }
1722
1723
1724 /* Simplify a chain of references. */
1725
1726 static gfc_try
1727 simplify_ref_chain (gfc_ref *ref, int type)
1728 {
1729 int n;
1730
1731 for (; ref; ref = ref->next)
1732 {
1733 switch (ref->type)
1734 {
1735 case REF_ARRAY:
1736 for (n = 0; n < ref->u.ar.dimen; n++)
1737 {
1738 if (gfc_simplify_expr (ref->u.ar.start[n], type) == FAILURE)
1739 return FAILURE;
1740 if (gfc_simplify_expr (ref->u.ar.end[n], type) == FAILURE)
1741 return FAILURE;
1742 if (gfc_simplify_expr (ref->u.ar.stride[n], type) == FAILURE)
1743 return FAILURE;
1744 }
1745 break;
1746
1747 case REF_SUBSTRING:
1748 if (gfc_simplify_expr (ref->u.ss.start, type) == FAILURE)
1749 return FAILURE;
1750 if (gfc_simplify_expr (ref->u.ss.end, type) == FAILURE)
1751 return FAILURE;
1752 break;
1753
1754 default:
1755 break;
1756 }
1757 }
1758 return SUCCESS;
1759 }
1760
1761
1762 /* Try to substitute the value of a parameter variable. */
1763
1764 static gfc_try
1765 simplify_parameter_variable (gfc_expr *p, int type)
1766 {
1767 gfc_expr *e;
1768 gfc_try t;
1769
1770 e = gfc_copy_expr (p->symtree->n.sym->value);
1771 if (e == NULL)
1772 return FAILURE;
1773
1774 e->rank = p->rank;
1775
1776 /* Do not copy subobject refs for constant. */
1777 if (e->expr_type != EXPR_CONSTANT && p->ref != NULL)
1778 e->ref = gfc_copy_ref (p->ref);
1779 t = gfc_simplify_expr (e, type);
1780
1781 /* Only use the simplification if it eliminated all subobject references. */
1782 if (t == SUCCESS && !e->ref)
1783 gfc_replace_expr (p, e);
1784 else
1785 gfc_free_expr (e);
1786
1787 return t;
1788 }
1789
1790 /* Given an expression, simplify it by collapsing constant
1791 expressions. Most simplification takes place when the expression
1792 tree is being constructed. If an intrinsic function is simplified
1793 at some point, we get called again to collapse the result against
1794 other constants.
1795
1796 We work by recursively simplifying expression nodes, simplifying
1797 intrinsic functions where possible, which can lead to further
1798 constant collapsing. If an operator has constant operand(s), we
1799 rip the expression apart, and rebuild it, hoping that it becomes
1800 something simpler.
1801
1802 The expression type is defined for:
1803 0 Basic expression parsing
1804 1 Simplifying array constructors -- will substitute
1805 iterator values.
1806 Returns FAILURE on error, SUCCESS otherwise.
1807 NOTE: Will return SUCCESS even if the expression can not be simplified. */
1808
1809 gfc_try
1810 gfc_simplify_expr (gfc_expr *p, int type)
1811 {
1812 gfc_actual_arglist *ap;
1813
1814 if (p == NULL)
1815 return SUCCESS;
1816
1817 switch (p->expr_type)
1818 {
1819 case EXPR_CONSTANT:
1820 case EXPR_NULL:
1821 break;
1822
1823 case EXPR_FUNCTION:
1824 for (ap = p->value.function.actual; ap; ap = ap->next)
1825 if (gfc_simplify_expr (ap->expr, type) == FAILURE)
1826 return FAILURE;
1827
1828 if (p->value.function.isym != NULL
1829 && gfc_intrinsic_func_interface (p, 1) == MATCH_ERROR)
1830 return FAILURE;
1831
1832 break;
1833
1834 case EXPR_SUBSTRING:
1835 if (simplify_ref_chain (p->ref, type) == FAILURE)
1836 return FAILURE;
1837
1838 if (gfc_is_constant_expr (p))
1839 {
1840 gfc_char_t *s;
1841 int start, end;
1842
1843 start = 0;
1844 if (p->ref && p->ref->u.ss.start)
1845 {
1846 gfc_extract_int (p->ref->u.ss.start, &start);
1847 start--; /* Convert from one-based to zero-based. */
1848 }
1849
1850 end = p->value.character.length;
1851 if (p->ref && p->ref->u.ss.end)
1852 gfc_extract_int (p->ref->u.ss.end, &end);
1853
1854 if (end < 0)
1855 end = 0;
1856
1857 s = gfc_get_wide_string (end - start + 2);
1858 memcpy (s, p->value.character.string + start,
1859 (end - start) * sizeof (gfc_char_t));
1860 s[end - start + 1] = '\0'; /* TODO: C-style string. */
1861 free (p->value.character.string);
1862 p->value.character.string = s;
1863 p->value.character.length = end - start;
1864 p->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1865 p->ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
1866 NULL,
1867 p->value.character.length);
1868 gfc_free_ref_list (p->ref);
1869 p->ref = NULL;
1870 p->expr_type = EXPR_CONSTANT;
1871 }
1872 break;
1873
1874 case EXPR_OP:
1875 if (simplify_intrinsic_op (p, type) == FAILURE)
1876 return FAILURE;
1877 break;
1878
1879 case EXPR_VARIABLE:
1880 /* Only substitute array parameter variables if we are in an
1881 initialization expression, or we want a subsection. */
1882 if (p->symtree->n.sym->attr.flavor == FL_PARAMETER
1883 && (gfc_init_expr_flag || p->ref
1884 || p->symtree->n.sym->value->expr_type != EXPR_ARRAY))
1885 {
1886 if (simplify_parameter_variable (p, type) == FAILURE)
1887 return FAILURE;
1888 break;
1889 }
1890
1891 if (type == 1)
1892 {
1893 gfc_simplify_iterator_var (p);
1894 }
1895
1896 /* Simplify subcomponent references. */
1897 if (simplify_ref_chain (p->ref, type) == FAILURE)
1898 return FAILURE;
1899
1900 break;
1901
1902 case EXPR_STRUCTURE:
1903 case EXPR_ARRAY:
1904 if (simplify_ref_chain (p->ref, type) == FAILURE)
1905 return FAILURE;
1906
1907 if (simplify_constructor (p->value.constructor, type) == FAILURE)
1908 return FAILURE;
1909
1910 if (p->expr_type == EXPR_ARRAY && p->ref && p->ref->type == REF_ARRAY
1911 && p->ref->u.ar.type == AR_FULL)
1912 gfc_expand_constructor (p, false);
1913
1914 if (simplify_const_ref (p) == FAILURE)
1915 return FAILURE;
1916
1917 break;
1918
1919 case EXPR_COMPCALL:
1920 case EXPR_PPC:
1921 gcc_unreachable ();
1922 break;
1923 }
1924
1925 return SUCCESS;
1926 }
1927
1928
1929 /* Returns the type of an expression with the exception that iterator
1930 variables are automatically integers no matter what else they may
1931 be declared as. */
1932
1933 static bt
1934 et0 (gfc_expr *e)
1935 {
1936 if (e->expr_type == EXPR_VARIABLE && gfc_check_iter_variable (e) == SUCCESS)
1937 return BT_INTEGER;
1938
1939 return e->ts.type;
1940 }
1941
1942
1943 /* Check an intrinsic arithmetic operation to see if it is consistent
1944 with some type of expression. */
1945
1946 static gfc_try check_init_expr (gfc_expr *);
1947
1948
1949 /* Scalarize an expression for an elemental intrinsic call. */
1950
1951 static gfc_try
1952 scalarize_intrinsic_call (gfc_expr *e)
1953 {
1954 gfc_actual_arglist *a, *b;
1955 gfc_constructor_base ctor;
1956 gfc_constructor *args[5];
1957 gfc_constructor *ci, *new_ctor;
1958 gfc_expr *expr, *old;
1959 int n, i, rank[5], array_arg;
1960
1961 /* Find which, if any, arguments are arrays. Assume that the old
1962 expression carries the type information and that the first arg
1963 that is an array expression carries all the shape information.*/
1964 n = array_arg = 0;
1965 a = e->value.function.actual;
1966 for (; a; a = a->next)
1967 {
1968 n++;
1969 if (a->expr->expr_type != EXPR_ARRAY)
1970 continue;
1971 array_arg = n;
1972 expr = gfc_copy_expr (a->expr);
1973 break;
1974 }
1975
1976 if (!array_arg)
1977 return FAILURE;
1978
1979 old = gfc_copy_expr (e);
1980
1981 gfc_constructor_free (expr->value.constructor);
1982 expr->value.constructor = NULL;
1983 expr->ts = old->ts;
1984 expr->where = old->where;
1985 expr->expr_type = EXPR_ARRAY;
1986
1987 /* Copy the array argument constructors into an array, with nulls
1988 for the scalars. */
1989 n = 0;
1990 a = old->value.function.actual;
1991 for (; a; a = a->next)
1992 {
1993 /* Check that this is OK for an initialization expression. */
1994 if (a->expr && check_init_expr (a->expr) == FAILURE)
1995 goto cleanup;
1996
1997 rank[n] = 0;
1998 if (a->expr && a->expr->rank && a->expr->expr_type == EXPR_VARIABLE)
1999 {
2000 rank[n] = a->expr->rank;
2001 ctor = a->expr->symtree->n.sym->value->value.constructor;
2002 args[n] = gfc_constructor_first (ctor);
2003 }
2004 else if (a->expr && a->expr->expr_type == EXPR_ARRAY)
2005 {
2006 if (a->expr->rank)
2007 rank[n] = a->expr->rank;
2008 else
2009 rank[n] = 1;
2010 ctor = gfc_constructor_copy (a->expr->value.constructor);
2011 args[n] = gfc_constructor_first (ctor);
2012 }
2013 else
2014 args[n] = NULL;
2015
2016 n++;
2017 }
2018
2019
2020 /* Using the array argument as the master, step through the array
2021 calling the function for each element and advancing the array
2022 constructors together. */
2023 for (ci = args[array_arg - 1]; ci; ci = gfc_constructor_next (ci))
2024 {
2025 new_ctor = gfc_constructor_append_expr (&expr->value.constructor,
2026 gfc_copy_expr (old), NULL);
2027
2028 gfc_free_actual_arglist (new_ctor->expr->value.function.actual);
2029 a = NULL;
2030 b = old->value.function.actual;
2031 for (i = 0; i < n; i++)
2032 {
2033 if (a == NULL)
2034 new_ctor->expr->value.function.actual
2035 = a = gfc_get_actual_arglist ();
2036 else
2037 {
2038 a->next = gfc_get_actual_arglist ();
2039 a = a->next;
2040 }
2041
2042 if (args[i])
2043 a->expr = gfc_copy_expr (args[i]->expr);
2044 else
2045 a->expr = gfc_copy_expr (b->expr);
2046
2047 b = b->next;
2048 }
2049
2050 /* Simplify the function calls. If the simplification fails, the
2051 error will be flagged up down-stream or the library will deal
2052 with it. */
2053 gfc_simplify_expr (new_ctor->expr, 0);
2054
2055 for (i = 0; i < n; i++)
2056 if (args[i])
2057 args[i] = gfc_constructor_next (args[i]);
2058
2059 for (i = 1; i < n; i++)
2060 if (rank[i] && ((args[i] != NULL && args[array_arg - 1] == NULL)
2061 || (args[i] == NULL && args[array_arg - 1] != NULL)))
2062 goto compliance;
2063 }
2064
2065 free_expr0 (e);
2066 *e = *expr;
2067 gfc_free_expr (old);
2068 return SUCCESS;
2069
2070 compliance:
2071 gfc_error_now ("elemental function arguments at %C are not compliant");
2072
2073 cleanup:
2074 gfc_free_expr (expr);
2075 gfc_free_expr (old);
2076 return FAILURE;
2077 }
2078
2079
2080 static gfc_try
2081 check_intrinsic_op (gfc_expr *e, gfc_try (*check_function) (gfc_expr *))
2082 {
2083 gfc_expr *op1 = e->value.op.op1;
2084 gfc_expr *op2 = e->value.op.op2;
2085
2086 if ((*check_function) (op1) == FAILURE)
2087 return FAILURE;
2088
2089 switch (e->value.op.op)
2090 {
2091 case INTRINSIC_UPLUS:
2092 case INTRINSIC_UMINUS:
2093 if (!numeric_type (et0 (op1)))
2094 goto not_numeric;
2095 break;
2096
2097 case INTRINSIC_EQ:
2098 case INTRINSIC_EQ_OS:
2099 case INTRINSIC_NE:
2100 case INTRINSIC_NE_OS:
2101 case INTRINSIC_GT:
2102 case INTRINSIC_GT_OS:
2103 case INTRINSIC_GE:
2104 case INTRINSIC_GE_OS:
2105 case INTRINSIC_LT:
2106 case INTRINSIC_LT_OS:
2107 case INTRINSIC_LE:
2108 case INTRINSIC_LE_OS:
2109 if ((*check_function) (op2) == FAILURE)
2110 return FAILURE;
2111
2112 if (!(et0 (op1) == BT_CHARACTER && et0 (op2) == BT_CHARACTER)
2113 && !(numeric_type (et0 (op1)) && numeric_type (et0 (op2))))
2114 {
2115 gfc_error ("Numeric or CHARACTER operands are required in "
2116 "expression at %L", &e->where);
2117 return FAILURE;
2118 }
2119 break;
2120
2121 case INTRINSIC_PLUS:
2122 case INTRINSIC_MINUS:
2123 case INTRINSIC_TIMES:
2124 case INTRINSIC_DIVIDE:
2125 case INTRINSIC_POWER:
2126 if ((*check_function) (op2) == FAILURE)
2127 return FAILURE;
2128
2129 if (!numeric_type (et0 (op1)) || !numeric_type (et0 (op2)))
2130 goto not_numeric;
2131
2132 break;
2133
2134 case INTRINSIC_CONCAT:
2135 if ((*check_function) (op2) == FAILURE)
2136 return FAILURE;
2137
2138 if (et0 (op1) != BT_CHARACTER || et0 (op2) != BT_CHARACTER)
2139 {
2140 gfc_error ("Concatenation operator in expression at %L "
2141 "must have two CHARACTER operands", &op1->where);
2142 return FAILURE;
2143 }
2144
2145 if (op1->ts.kind != op2->ts.kind)
2146 {
2147 gfc_error ("Concat operator at %L must concatenate strings of the "
2148 "same kind", &e->where);
2149 return FAILURE;
2150 }
2151
2152 break;
2153
2154 case INTRINSIC_NOT:
2155 if (et0 (op1) != BT_LOGICAL)
2156 {
2157 gfc_error (".NOT. operator in expression at %L must have a LOGICAL "
2158 "operand", &op1->where);
2159 return FAILURE;
2160 }
2161
2162 break;
2163
2164 case INTRINSIC_AND:
2165 case INTRINSIC_OR:
2166 case INTRINSIC_EQV:
2167 case INTRINSIC_NEQV:
2168 if ((*check_function) (op2) == FAILURE)
2169 return FAILURE;
2170
2171 if (et0 (op1) != BT_LOGICAL || et0 (op2) != BT_LOGICAL)
2172 {
2173 gfc_error ("LOGICAL operands are required in expression at %L",
2174 &e->where);
2175 return FAILURE;
2176 }
2177
2178 break;
2179
2180 case INTRINSIC_PARENTHESES:
2181 break;
2182
2183 default:
2184 gfc_error ("Only intrinsic operators can be used in expression at %L",
2185 &e->where);
2186 return FAILURE;
2187 }
2188
2189 return SUCCESS;
2190
2191 not_numeric:
2192 gfc_error ("Numeric operands are required in expression at %L", &e->where);
2193
2194 return FAILURE;
2195 }
2196
2197 /* F2003, 7.1.7 (3): In init expression, allocatable components
2198 must not be data-initialized. */
2199 static gfc_try
2200 check_alloc_comp_init (gfc_expr *e)
2201 {
2202 gfc_component *comp;
2203 gfc_constructor *ctor;
2204
2205 gcc_assert (e->expr_type == EXPR_STRUCTURE);
2206 gcc_assert (e->ts.type == BT_DERIVED);
2207
2208 for (comp = e->ts.u.derived->components,
2209 ctor = gfc_constructor_first (e->value.constructor);
2210 comp; comp = comp->next, ctor = gfc_constructor_next (ctor))
2211 {
2212 if (comp->attr.allocatable
2213 && ctor->expr->expr_type != EXPR_NULL)
2214 {
2215 gfc_error("Invalid initialization expression for ALLOCATABLE "
2216 "component '%s' in structure constructor at %L",
2217 comp->name, &ctor->expr->where);
2218 return FAILURE;
2219 }
2220 }
2221
2222 return SUCCESS;
2223 }
2224
2225 static match
2226 check_init_expr_arguments (gfc_expr *e)
2227 {
2228 gfc_actual_arglist *ap;
2229
2230 for (ap = e->value.function.actual; ap; ap = ap->next)
2231 if (check_init_expr (ap->expr) == FAILURE)
2232 return MATCH_ERROR;
2233
2234 return MATCH_YES;
2235 }
2236
2237 static gfc_try check_restricted (gfc_expr *);
2238
2239 /* F95, 7.1.6.1, Initialization expressions, (7)
2240 F2003, 7.1.7 Initialization expression, (8) */
2241
2242 static match
2243 check_inquiry (gfc_expr *e, int not_restricted)
2244 {
2245 const char *name;
2246 const char *const *functions;
2247
2248 static const char *const inquiry_func_f95[] = {
2249 "lbound", "shape", "size", "ubound",
2250 "bit_size", "len", "kind",
2251 "digits", "epsilon", "huge", "maxexponent", "minexponent",
2252 "precision", "radix", "range", "tiny",
2253 NULL
2254 };
2255
2256 static const char *const inquiry_func_f2003[] = {
2257 "lbound", "shape", "size", "ubound",
2258 "bit_size", "len", "kind",
2259 "digits", "epsilon", "huge", "maxexponent", "minexponent",
2260 "precision", "radix", "range", "tiny",
2261 "new_line", NULL
2262 };
2263
2264 int i;
2265 gfc_actual_arglist *ap;
2266
2267 if (!e->value.function.isym
2268 || !e->value.function.isym->inquiry)
2269 return MATCH_NO;
2270
2271 /* An undeclared parameter will get us here (PR25018). */
2272 if (e->symtree == NULL)
2273 return MATCH_NO;
2274
2275 name = e->symtree->n.sym->name;
2276
2277 functions = (gfc_option.warn_std & GFC_STD_F2003)
2278 ? inquiry_func_f2003 : inquiry_func_f95;
2279
2280 for (i = 0; functions[i]; i++)
2281 if (strcmp (functions[i], name) == 0)
2282 break;
2283
2284 if (functions[i] == NULL)
2285 return MATCH_ERROR;
2286
2287 /* At this point we have an inquiry function with a variable argument. The
2288 type of the variable might be undefined, but we need it now, because the
2289 arguments of these functions are not allowed to be undefined. */
2290
2291 for (ap = e->value.function.actual; ap; ap = ap->next)
2292 {
2293 if (!ap->expr)
2294 continue;
2295
2296 if (ap->expr->ts.type == BT_UNKNOWN)
2297 {
2298 if (ap->expr->symtree->n.sym->ts.type == BT_UNKNOWN
2299 && gfc_set_default_type (ap->expr->symtree->n.sym, 0, gfc_current_ns)
2300 == FAILURE)
2301 return MATCH_NO;
2302
2303 ap->expr->ts = ap->expr->symtree->n.sym->ts;
2304 }
2305
2306 /* Assumed character length will not reduce to a constant expression
2307 with LEN, as required by the standard. */
2308 if (i == 5 && not_restricted
2309 && ap->expr->symtree->n.sym->ts.type == BT_CHARACTER
2310 && (ap->expr->symtree->n.sym->ts.u.cl->length == NULL
2311 || ap->expr->symtree->n.sym->ts.deferred))
2312 {
2313 gfc_error ("Assumed or deferred character length variable '%s' "
2314 " in constant expression at %L",
2315 ap->expr->symtree->n.sym->name,
2316 &ap->expr->where);
2317 return MATCH_ERROR;
2318 }
2319 else if (not_restricted && check_init_expr (ap->expr) == FAILURE)
2320 return MATCH_ERROR;
2321
2322 if (not_restricted == 0
2323 && ap->expr->expr_type != EXPR_VARIABLE
2324 && check_restricted (ap->expr) == FAILURE)
2325 return MATCH_ERROR;
2326
2327 if (not_restricted == 0
2328 && ap->expr->expr_type == EXPR_VARIABLE
2329 && ap->expr->symtree->n.sym->attr.dummy
2330 && ap->expr->symtree->n.sym->attr.optional)
2331 return MATCH_NO;
2332 }
2333
2334 return MATCH_YES;
2335 }
2336
2337
2338 /* F95, 7.1.6.1, Initialization expressions, (5)
2339 F2003, 7.1.7 Initialization expression, (5) */
2340
2341 static match
2342 check_transformational (gfc_expr *e)
2343 {
2344 static const char * const trans_func_f95[] = {
2345 "repeat", "reshape", "selected_int_kind",
2346 "selected_real_kind", "transfer", "trim", NULL
2347 };
2348
2349 static const char * const trans_func_f2003[] = {
2350 "all", "any", "count", "dot_product", "matmul", "null", "pack",
2351 "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
2352 "selected_real_kind", "spread", "sum", "transfer", "transpose",
2353 "trim", "unpack", NULL
2354 };
2355
2356 int i;
2357 const char *name;
2358 const char *const *functions;
2359
2360 if (!e->value.function.isym
2361 || !e->value.function.isym->transformational)
2362 return MATCH_NO;
2363
2364 name = e->symtree->n.sym->name;
2365
2366 functions = (gfc_option.allow_std & GFC_STD_F2003)
2367 ? trans_func_f2003 : trans_func_f95;
2368
2369 /* NULL() is dealt with below. */
2370 if (strcmp ("null", name) == 0)
2371 return MATCH_NO;
2372
2373 for (i = 0; functions[i]; i++)
2374 if (strcmp (functions[i], name) == 0)
2375 break;
2376
2377 if (functions[i] == NULL)
2378 {
2379 gfc_error("transformational intrinsic '%s' at %L is not permitted "
2380 "in an initialization expression", name, &e->where);
2381 return MATCH_ERROR;
2382 }
2383
2384 return check_init_expr_arguments (e);
2385 }
2386
2387
2388 /* F95, 7.1.6.1, Initialization expressions, (6)
2389 F2003, 7.1.7 Initialization expression, (6) */
2390
2391 static match
2392 check_null (gfc_expr *e)
2393 {
2394 if (strcmp ("null", e->symtree->n.sym->name) != 0)
2395 return MATCH_NO;
2396
2397 return check_init_expr_arguments (e);
2398 }
2399
2400
2401 static match
2402 check_elemental (gfc_expr *e)
2403 {
2404 if (!e->value.function.isym
2405 || !e->value.function.isym->elemental)
2406 return MATCH_NO;
2407
2408 if (e->ts.type != BT_INTEGER
2409 && e->ts.type != BT_CHARACTER
2410 && gfc_notify_std (GFC_STD_F2003, "Extension: Evaluation of "
2411 "nonstandard initialization expression at %L",
2412 &e->where) == FAILURE)
2413 return MATCH_ERROR;
2414
2415 return check_init_expr_arguments (e);
2416 }
2417
2418
2419 static match
2420 check_conversion (gfc_expr *e)
2421 {
2422 if (!e->value.function.isym
2423 || !e->value.function.isym->conversion)
2424 return MATCH_NO;
2425
2426 return check_init_expr_arguments (e);
2427 }
2428
2429
2430 /* Verify that an expression is an initialization expression. A side
2431 effect is that the expression tree is reduced to a single constant
2432 node if all goes well. This would normally happen when the
2433 expression is constructed but function references are assumed to be
2434 intrinsics in the context of initialization expressions. If
2435 FAILURE is returned an error message has been generated. */
2436
2437 static gfc_try
2438 check_init_expr (gfc_expr *e)
2439 {
2440 match m;
2441 gfc_try t;
2442
2443 if (e == NULL)
2444 return SUCCESS;
2445
2446 switch (e->expr_type)
2447 {
2448 case EXPR_OP:
2449 t = check_intrinsic_op (e, check_init_expr);
2450 if (t == SUCCESS)
2451 t = gfc_simplify_expr (e, 0);
2452
2453 break;
2454
2455 case EXPR_FUNCTION:
2456 t = FAILURE;
2457
2458 {
2459 gfc_intrinsic_sym* isym;
2460 gfc_symbol* sym;
2461
2462 sym = e->symtree->n.sym;
2463 if (!gfc_is_intrinsic (sym, 0, e->where)
2464 || (m = gfc_intrinsic_func_interface (e, 0)) != MATCH_YES)
2465 {
2466 gfc_error ("Function '%s' in initialization expression at %L "
2467 "must be an intrinsic function",
2468 e->symtree->n.sym->name, &e->where);
2469 break;
2470 }
2471
2472 if ((m = check_conversion (e)) == MATCH_NO
2473 && (m = check_inquiry (e, 1)) == MATCH_NO
2474 && (m = check_null (e)) == MATCH_NO
2475 && (m = check_transformational (e)) == MATCH_NO
2476 && (m = check_elemental (e)) == MATCH_NO)
2477 {
2478 gfc_error ("Intrinsic function '%s' at %L is not permitted "
2479 "in an initialization expression",
2480 e->symtree->n.sym->name, &e->where);
2481 m = MATCH_ERROR;
2482 }
2483
2484 if (m == MATCH_ERROR)
2485 return FAILURE;
2486
2487 /* Try to scalarize an elemental intrinsic function that has an
2488 array argument. */
2489 isym = gfc_find_function (e->symtree->n.sym->name);
2490 if (isym && isym->elemental
2491 && (t = scalarize_intrinsic_call (e)) == SUCCESS)
2492 break;
2493 }
2494
2495 if (m == MATCH_YES)
2496 t = gfc_simplify_expr (e, 0);
2497
2498 break;
2499
2500 case EXPR_VARIABLE:
2501 t = SUCCESS;
2502
2503 if (gfc_check_iter_variable (e) == SUCCESS)
2504 break;
2505
2506 if (e->symtree->n.sym->attr.flavor == FL_PARAMETER)
2507 {
2508 /* A PARAMETER shall not be used to define itself, i.e.
2509 REAL, PARAMETER :: x = transfer(0, x)
2510 is invalid. */
2511 if (!e->symtree->n.sym->value)
2512 {
2513 gfc_error("PARAMETER '%s' is used at %L before its definition "
2514 "is complete", e->symtree->n.sym->name, &e->where);
2515 t = FAILURE;
2516 }
2517 else
2518 t = simplify_parameter_variable (e, 0);
2519
2520 break;
2521 }
2522
2523 if (gfc_in_match_data ())
2524 break;
2525
2526 t = FAILURE;
2527
2528 if (e->symtree->n.sym->as)
2529 {
2530 switch (e->symtree->n.sym->as->type)
2531 {
2532 case AS_ASSUMED_SIZE:
2533 gfc_error ("Assumed size array '%s' at %L is not permitted "
2534 "in an initialization expression",
2535 e->symtree->n.sym->name, &e->where);
2536 break;
2537
2538 case AS_ASSUMED_SHAPE:
2539 gfc_error ("Assumed shape array '%s' at %L is not permitted "
2540 "in an initialization expression",
2541 e->symtree->n.sym->name, &e->where);
2542 break;
2543
2544 case AS_DEFERRED:
2545 gfc_error ("Deferred array '%s' at %L is not permitted "
2546 "in an initialization expression",
2547 e->symtree->n.sym->name, &e->where);
2548 break;
2549
2550 case AS_EXPLICIT:
2551 gfc_error ("Array '%s' at %L is a variable, which does "
2552 "not reduce to a constant expression",
2553 e->symtree->n.sym->name, &e->where);
2554 break;
2555
2556 default:
2557 gcc_unreachable();
2558 }
2559 }
2560 else
2561 gfc_error ("Parameter '%s' at %L has not been declared or is "
2562 "a variable, which does not reduce to a constant "
2563 "expression", e->symtree->n.sym->name, &e->where);
2564
2565 break;
2566
2567 case EXPR_CONSTANT:
2568 case EXPR_NULL:
2569 t = SUCCESS;
2570 break;
2571
2572 case EXPR_SUBSTRING:
2573 t = check_init_expr (e->ref->u.ss.start);
2574 if (t == FAILURE)
2575 break;
2576
2577 t = check_init_expr (e->ref->u.ss.end);
2578 if (t == SUCCESS)
2579 t = gfc_simplify_expr (e, 0);
2580
2581 break;
2582
2583 case EXPR_STRUCTURE:
2584 t = e->ts.is_iso_c ? SUCCESS : FAILURE;
2585 if (t == SUCCESS)
2586 break;
2587
2588 t = check_alloc_comp_init (e);
2589 if (t == FAILURE)
2590 break;
2591
2592 t = gfc_check_constructor (e, check_init_expr);
2593 if (t == FAILURE)
2594 break;
2595
2596 break;
2597
2598 case EXPR_ARRAY:
2599 t = gfc_check_constructor (e, check_init_expr);
2600 if (t == FAILURE)
2601 break;
2602
2603 t = gfc_expand_constructor (e, true);
2604 if (t == FAILURE)
2605 break;
2606
2607 t = gfc_check_constructor_type (e);
2608 break;
2609
2610 default:
2611 gfc_internal_error ("check_init_expr(): Unknown expression type");
2612 }
2613
2614 return t;
2615 }
2616
2617 /* Reduces a general expression to an initialization expression (a constant).
2618 This used to be part of gfc_match_init_expr.
2619 Note that this function doesn't free the given expression on FAILURE. */
2620
2621 gfc_try
2622 gfc_reduce_init_expr (gfc_expr *expr)
2623 {
2624 gfc_try t;
2625
2626 gfc_init_expr_flag = true;
2627 t = gfc_resolve_expr (expr);
2628 if (t == SUCCESS)
2629 t = check_init_expr (expr);
2630 gfc_init_expr_flag = false;
2631
2632 if (t == FAILURE)
2633 return FAILURE;
2634
2635 if (expr->expr_type == EXPR_ARRAY)
2636 {
2637 if (gfc_check_constructor_type (expr) == FAILURE)
2638 return FAILURE;
2639 if (gfc_expand_constructor (expr, true) == FAILURE)
2640 return FAILURE;
2641 }
2642
2643 return SUCCESS;
2644 }
2645
2646
2647 /* Match an initialization expression. We work by first matching an
2648 expression, then reducing it to a constant. */
2649
2650 match
2651 gfc_match_init_expr (gfc_expr **result)
2652 {
2653 gfc_expr *expr;
2654 match m;
2655 gfc_try t;
2656
2657 expr = NULL;
2658
2659 gfc_init_expr_flag = true;
2660
2661 m = gfc_match_expr (&expr);
2662 if (m != MATCH_YES)
2663 {
2664 gfc_init_expr_flag = false;
2665 return m;
2666 }
2667
2668 t = gfc_reduce_init_expr (expr);
2669 if (t != SUCCESS)
2670 {
2671 gfc_free_expr (expr);
2672 gfc_init_expr_flag = false;
2673 return MATCH_ERROR;
2674 }
2675
2676 *result = expr;
2677 gfc_init_expr_flag = false;
2678
2679 return MATCH_YES;
2680 }
2681
2682
2683 /* Given an actual argument list, test to see that each argument is a
2684 restricted expression and optionally if the expression type is
2685 integer or character. */
2686
2687 static gfc_try
2688 restricted_args (gfc_actual_arglist *a)
2689 {
2690 for (; a; a = a->next)
2691 {
2692 if (check_restricted (a->expr) == FAILURE)
2693 return FAILURE;
2694 }
2695
2696 return SUCCESS;
2697 }
2698
2699
2700 /************* Restricted/specification expressions *************/
2701
2702
2703 /* Make sure a non-intrinsic function is a specification function. */
2704
2705 static gfc_try
2706 external_spec_function (gfc_expr *e)
2707 {
2708 gfc_symbol *f;
2709
2710 f = e->value.function.esym;
2711
2712 if (f->attr.proc == PROC_ST_FUNCTION)
2713 {
2714 gfc_error ("Specification function '%s' at %L cannot be a statement "
2715 "function", f->name, &e->where);
2716 return FAILURE;
2717 }
2718
2719 if (f->attr.proc == PROC_INTERNAL)
2720 {
2721 gfc_error ("Specification function '%s' at %L cannot be an internal "
2722 "function", f->name, &e->where);
2723 return FAILURE;
2724 }
2725
2726 if (!f->attr.pure && !f->attr.elemental)
2727 {
2728 gfc_error ("Specification function '%s' at %L must be PURE", f->name,
2729 &e->where);
2730 return FAILURE;
2731 }
2732
2733 if (f->attr.recursive)
2734 {
2735 gfc_error ("Specification function '%s' at %L cannot be RECURSIVE",
2736 f->name, &e->where);
2737 return FAILURE;
2738 }
2739
2740 return restricted_args (e->value.function.actual);
2741 }
2742
2743
2744 /* Check to see that a function reference to an intrinsic is a
2745 restricted expression. */
2746
2747 static gfc_try
2748 restricted_intrinsic (gfc_expr *e)
2749 {
2750 /* TODO: Check constraints on inquiry functions. 7.1.6.2 (7). */
2751 if (check_inquiry (e, 0) == MATCH_YES)
2752 return SUCCESS;
2753
2754 return restricted_args (e->value.function.actual);
2755 }
2756
2757
2758 /* Check the expressions of an actual arglist. Used by check_restricted. */
2759
2760 static gfc_try
2761 check_arglist (gfc_actual_arglist* arg, gfc_try (*checker) (gfc_expr*))
2762 {
2763 for (; arg; arg = arg->next)
2764 if (checker (arg->expr) == FAILURE)
2765 return FAILURE;
2766
2767 return SUCCESS;
2768 }
2769
2770
2771 /* Check the subscription expressions of a reference chain with a checking
2772 function; used by check_restricted. */
2773
2774 static gfc_try
2775 check_references (gfc_ref* ref, gfc_try (*checker) (gfc_expr*))
2776 {
2777 int dim;
2778
2779 if (!ref)
2780 return SUCCESS;
2781
2782 switch (ref->type)
2783 {
2784 case REF_ARRAY:
2785 for (dim = 0; dim != ref->u.ar.dimen; ++dim)
2786 {
2787 if (checker (ref->u.ar.start[dim]) == FAILURE)
2788 return FAILURE;
2789 if (checker (ref->u.ar.end[dim]) == FAILURE)
2790 return FAILURE;
2791 if (checker (ref->u.ar.stride[dim]) == FAILURE)
2792 return FAILURE;
2793 }
2794 break;
2795
2796 case REF_COMPONENT:
2797 /* Nothing needed, just proceed to next reference. */
2798 break;
2799
2800 case REF_SUBSTRING:
2801 if (checker (ref->u.ss.start) == FAILURE)
2802 return FAILURE;
2803 if (checker (ref->u.ss.end) == FAILURE)
2804 return FAILURE;
2805 break;
2806
2807 default:
2808 gcc_unreachable ();
2809 break;
2810 }
2811
2812 return check_references (ref->next, checker);
2813 }
2814
2815
2816 /* Verify that an expression is a restricted expression. Like its
2817 cousin check_init_expr(), an error message is generated if we
2818 return FAILURE. */
2819
2820 static gfc_try
2821 check_restricted (gfc_expr *e)
2822 {
2823 gfc_symbol* sym;
2824 gfc_try t;
2825
2826 if (e == NULL)
2827 return SUCCESS;
2828
2829 switch (e->expr_type)
2830 {
2831 case EXPR_OP:
2832 t = check_intrinsic_op (e, check_restricted);
2833 if (t == SUCCESS)
2834 t = gfc_simplify_expr (e, 0);
2835
2836 break;
2837
2838 case EXPR_FUNCTION:
2839 if (e->value.function.esym)
2840 {
2841 t = check_arglist (e->value.function.actual, &check_restricted);
2842 if (t == SUCCESS)
2843 t = external_spec_function (e);
2844 }
2845 else
2846 {
2847 if (e->value.function.isym && e->value.function.isym->inquiry)
2848 t = SUCCESS;
2849 else
2850 t = check_arglist (e->value.function.actual, &check_restricted);
2851
2852 if (t == SUCCESS)
2853 t = restricted_intrinsic (e);
2854 }
2855 break;
2856
2857 case EXPR_VARIABLE:
2858 sym = e->symtree->n.sym;
2859 t = FAILURE;
2860
2861 /* If a dummy argument appears in a context that is valid for a
2862 restricted expression in an elemental procedure, it will have
2863 already been simplified away once we get here. Therefore we
2864 don't need to jump through hoops to distinguish valid from
2865 invalid cases. */
2866 if (sym->attr.dummy && sym->ns == gfc_current_ns
2867 && sym->ns->proc_name && sym->ns->proc_name->attr.elemental)
2868 {
2869 gfc_error ("Dummy argument '%s' not allowed in expression at %L",
2870 sym->name, &e->where);
2871 break;
2872 }
2873
2874 if (sym->attr.optional)
2875 {
2876 gfc_error ("Dummy argument '%s' at %L cannot be OPTIONAL",
2877 sym->name, &e->where);
2878 break;
2879 }
2880
2881 if (sym->attr.intent == INTENT_OUT)
2882 {
2883 gfc_error ("Dummy argument '%s' at %L cannot be INTENT(OUT)",
2884 sym->name, &e->where);
2885 break;
2886 }
2887
2888 /* Check reference chain if any. */
2889 if (check_references (e->ref, &check_restricted) == FAILURE)
2890 break;
2891
2892 /* gfc_is_formal_arg broadcasts that a formal argument list is being
2893 processed in resolve.c(resolve_formal_arglist). This is done so
2894 that host associated dummy array indices are accepted (PR23446).
2895 This mechanism also does the same for the specification expressions
2896 of array-valued functions. */
2897 if (e->error
2898 || sym->attr.in_common
2899 || sym->attr.use_assoc
2900 || sym->attr.dummy
2901 || sym->attr.implied_index
2902 || sym->attr.flavor == FL_PARAMETER
2903 || (sym->ns && sym->ns == gfc_current_ns->parent)
2904 || (sym->ns && gfc_current_ns->parent
2905 && sym->ns == gfc_current_ns->parent->parent)
2906 || (sym->ns->proc_name != NULL
2907 && sym->ns->proc_name->attr.flavor == FL_MODULE)
2908 || (gfc_is_formal_arg () && (sym->ns == gfc_current_ns)))
2909 {
2910 t = SUCCESS;
2911 break;
2912 }
2913
2914 gfc_error ("Variable '%s' cannot appear in the expression at %L",
2915 sym->name, &e->where);
2916 /* Prevent a repetition of the error. */
2917 e->error = 1;
2918 break;
2919
2920 case EXPR_NULL:
2921 case EXPR_CONSTANT:
2922 t = SUCCESS;
2923 break;
2924
2925 case EXPR_SUBSTRING:
2926 t = gfc_specification_expr (e->ref->u.ss.start);
2927 if (t == FAILURE)
2928 break;
2929
2930 t = gfc_specification_expr (e->ref->u.ss.end);
2931 if (t == SUCCESS)
2932 t = gfc_simplify_expr (e, 0);
2933
2934 break;
2935
2936 case EXPR_STRUCTURE:
2937 t = gfc_check_constructor (e, check_restricted);
2938 break;
2939
2940 case EXPR_ARRAY:
2941 t = gfc_check_constructor (e, check_restricted);
2942 break;
2943
2944 default:
2945 gfc_internal_error ("check_restricted(): Unknown expression type");
2946 }
2947
2948 return t;
2949 }
2950
2951
2952 /* Check to see that an expression is a specification expression. If
2953 we return FAILURE, an error has been generated. */
2954
2955 gfc_try
2956 gfc_specification_expr (gfc_expr *e)
2957 {
2958 gfc_component *comp;
2959
2960 if (e == NULL)
2961 return SUCCESS;
2962
2963 if (e->ts.type != BT_INTEGER)
2964 {
2965 gfc_error ("Expression at %L must be of INTEGER type, found %s",
2966 &e->where, gfc_basic_typename (e->ts.type));
2967 return FAILURE;
2968 }
2969
2970 if (e->expr_type == EXPR_FUNCTION
2971 && !e->value.function.isym
2972 && !e->value.function.esym
2973 && !gfc_pure (e->symtree->n.sym)
2974 && (!gfc_is_proc_ptr_comp (e, &comp)
2975 || !comp->attr.pure))
2976 {
2977 gfc_error ("Function '%s' at %L must be PURE",
2978 e->symtree->n.sym->name, &e->where);
2979 /* Prevent repeat error messages. */
2980 e->symtree->n.sym->attr.pure = 1;
2981 return FAILURE;
2982 }
2983
2984 if (e->rank != 0)
2985 {
2986 gfc_error ("Expression at %L must be scalar", &e->where);
2987 return FAILURE;
2988 }
2989
2990 if (gfc_simplify_expr (e, 0) == FAILURE)
2991 return FAILURE;
2992
2993 return check_restricted (e);
2994 }
2995
2996
2997 /************** Expression conformance checks. *************/
2998
2999 /* Given two expressions, make sure that the arrays are conformable. */
3000
3001 gfc_try
3002 gfc_check_conformance (gfc_expr *op1, gfc_expr *op2, const char *optype_msgid, ...)
3003 {
3004 int op1_flag, op2_flag, d;
3005 mpz_t op1_size, op2_size;
3006 gfc_try t;
3007
3008 va_list argp;
3009 char buffer[240];
3010
3011 if (op1->rank == 0 || op2->rank == 0)
3012 return SUCCESS;
3013
3014 va_start (argp, optype_msgid);
3015 vsnprintf (buffer, 240, optype_msgid, argp);
3016 va_end (argp);
3017
3018 if (op1->rank != op2->rank)
3019 {
3020 gfc_error ("Incompatible ranks in %s (%d and %d) at %L", _(buffer),
3021 op1->rank, op2->rank, &op1->where);
3022 return FAILURE;
3023 }
3024
3025 t = SUCCESS;
3026
3027 for (d = 0; d < op1->rank; d++)
3028 {
3029 op1_flag = gfc_array_dimen_size (op1, d, &op1_size) == SUCCESS;
3030 op2_flag = gfc_array_dimen_size (op2, d, &op2_size) == SUCCESS;
3031
3032 if (op1_flag && op2_flag && mpz_cmp (op1_size, op2_size) != 0)
3033 {
3034 gfc_error ("Different shape for %s at %L on dimension %d "
3035 "(%d and %d)", _(buffer), &op1->where, d + 1,
3036 (int) mpz_get_si (op1_size),
3037 (int) mpz_get_si (op2_size));
3038
3039 t = FAILURE;
3040 }
3041
3042 if (op1_flag)
3043 mpz_clear (op1_size);
3044 if (op2_flag)
3045 mpz_clear (op2_size);
3046
3047 if (t == FAILURE)
3048 return FAILURE;
3049 }
3050
3051 return SUCCESS;
3052 }
3053
3054
3055 /* Given an assignable expression and an arbitrary expression, make
3056 sure that the assignment can take place. */
3057
3058 gfc_try
3059 gfc_check_assign (gfc_expr *lvalue, gfc_expr *rvalue, int conform)
3060 {
3061 gfc_symbol *sym;
3062 gfc_ref *ref;
3063 int has_pointer;
3064
3065 sym = lvalue->symtree->n.sym;
3066
3067 /* See if this is the component or subcomponent of a pointer. */
3068 has_pointer = sym->attr.pointer;
3069 for (ref = lvalue->ref; ref; ref = ref->next)
3070 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
3071 {
3072 has_pointer = 1;
3073 break;
3074 }
3075
3076 /* 12.5.2.2, Note 12.26: The result variable is very similar to any other
3077 variable local to a function subprogram. Its existence begins when
3078 execution of the function is initiated and ends when execution of the
3079 function is terminated...
3080 Therefore, the left hand side is no longer a variable, when it is: */
3081 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_ST_FUNCTION
3082 && !sym->attr.external)
3083 {
3084 bool bad_proc;
3085 bad_proc = false;
3086
3087 /* (i) Use associated; */
3088 if (sym->attr.use_assoc)
3089 bad_proc = true;
3090
3091 /* (ii) The assignment is in the main program; or */
3092 if (gfc_current_ns->proc_name->attr.is_main_program)
3093 bad_proc = true;
3094
3095 /* (iii) A module or internal procedure... */
3096 if ((gfc_current_ns->proc_name->attr.proc == PROC_INTERNAL
3097 || gfc_current_ns->proc_name->attr.proc == PROC_MODULE)
3098 && gfc_current_ns->parent
3099 && (!(gfc_current_ns->parent->proc_name->attr.function
3100 || gfc_current_ns->parent->proc_name->attr.subroutine)
3101 || gfc_current_ns->parent->proc_name->attr.is_main_program))
3102 {
3103 /* ... that is not a function... */
3104 if (!gfc_current_ns->proc_name->attr.function)
3105 bad_proc = true;
3106
3107 /* ... or is not an entry and has a different name. */
3108 if (!sym->attr.entry && sym->name != gfc_current_ns->proc_name->name)
3109 bad_proc = true;
3110 }
3111
3112 /* (iv) Host associated and not the function symbol or the
3113 parent result. This picks up sibling references, which
3114 cannot be entries. */
3115 if (!sym->attr.entry
3116 && sym->ns == gfc_current_ns->parent
3117 && sym != gfc_current_ns->proc_name
3118 && sym != gfc_current_ns->parent->proc_name->result)
3119 bad_proc = true;
3120
3121 if (bad_proc)
3122 {
3123 gfc_error ("'%s' at %L is not a VALUE", sym->name, &lvalue->where);
3124 return FAILURE;
3125 }
3126 }
3127
3128 if (rvalue->rank != 0 && lvalue->rank != rvalue->rank)
3129 {
3130 gfc_error ("Incompatible ranks %d and %d in assignment at %L",
3131 lvalue->rank, rvalue->rank, &lvalue->where);
3132 return FAILURE;
3133 }
3134
3135 if (lvalue->ts.type == BT_UNKNOWN)
3136 {
3137 gfc_error ("Variable type is UNKNOWN in assignment at %L",
3138 &lvalue->where);
3139 return FAILURE;
3140 }
3141
3142 if (rvalue->expr_type == EXPR_NULL)
3143 {
3144 if (has_pointer && (ref == NULL || ref->next == NULL)
3145 && lvalue->symtree->n.sym->attr.data)
3146 return SUCCESS;
3147 else
3148 {
3149 gfc_error ("NULL appears on right-hand side in assignment at %L",
3150 &rvalue->where);
3151 return FAILURE;
3152 }
3153 }
3154
3155 /* This is possibly a typo: x = f() instead of x => f(). */
3156 if (gfc_option.warn_surprising
3157 && rvalue->expr_type == EXPR_FUNCTION
3158 && rvalue->symtree->n.sym->attr.pointer)
3159 gfc_warning ("POINTER valued function appears on right-hand side of "
3160 "assignment at %L", &rvalue->where);
3161
3162 /* Check size of array assignments. */
3163 if (lvalue->rank != 0 && rvalue->rank != 0
3164 && gfc_check_conformance (lvalue, rvalue, "array assignment") != SUCCESS)
3165 return FAILURE;
3166
3167 if (rvalue->is_boz && lvalue->ts.type != BT_INTEGER
3168 && lvalue->symtree->n.sym->attr.data
3169 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L used to "
3170 "initialize non-integer variable '%s'",
3171 &rvalue->where, lvalue->symtree->n.sym->name)
3172 == FAILURE)
3173 return FAILURE;
3174 else if (rvalue->is_boz && !lvalue->symtree->n.sym->attr.data
3175 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
3176 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
3177 &rvalue->where) == FAILURE)
3178 return FAILURE;
3179
3180 /* Handle the case of a BOZ literal on the RHS. */
3181 if (rvalue->is_boz && lvalue->ts.type != BT_INTEGER)
3182 {
3183 int rc;
3184 if (gfc_option.warn_surprising)
3185 gfc_warning ("BOZ literal at %L is bitwise transferred "
3186 "non-integer symbol '%s'", &rvalue->where,
3187 lvalue->symtree->n.sym->name);
3188 if (!gfc_convert_boz (rvalue, &lvalue->ts))
3189 return FAILURE;
3190 if ((rc = gfc_range_check (rvalue)) != ARITH_OK)
3191 {
3192 if (rc == ARITH_UNDERFLOW)
3193 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
3194 ". This check can be disabled with the option "
3195 "-fno-range-check", &rvalue->where);
3196 else if (rc == ARITH_OVERFLOW)
3197 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
3198 ". This check can be disabled with the option "
3199 "-fno-range-check", &rvalue->where);
3200 else if (rc == ARITH_NAN)
3201 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
3202 ". This check can be disabled with the option "
3203 "-fno-range-check", &rvalue->where);
3204 return FAILURE;
3205 }
3206 }
3207
3208 /* Warn about type-changing conversions for REAL or COMPLEX constants.
3209 If lvalue and rvalue are mixed REAL and complex, gfc_compare_types
3210 will warn anyway, so there is no need to to so here. */
3211
3212 if (rvalue->expr_type == EXPR_CONSTANT && lvalue->ts.type == rvalue->ts.type
3213 && (lvalue->ts.type == BT_REAL || lvalue->ts.type == BT_COMPLEX))
3214 {
3215 if (lvalue->ts.kind < rvalue->ts.kind && gfc_option.gfc_warn_conversion)
3216 {
3217 /* As a special bonus, don't warn about REAL rvalues which are not
3218 changed by the conversion if -Wconversion is specified. */
3219 if (rvalue->ts.type == BT_REAL && mpfr_number_p (rvalue->value.real))
3220 {
3221 /* Calculate the difference between the constant and the rounded
3222 value and check it against zero. */
3223 mpfr_t rv, diff;
3224 gfc_set_model_kind (lvalue->ts.kind);
3225 mpfr_init (rv);
3226 gfc_set_model_kind (rvalue->ts.kind);
3227 mpfr_init (diff);
3228
3229 mpfr_set (rv, rvalue->value.real, GFC_RND_MODE);
3230 mpfr_sub (diff, rv, rvalue->value.real, GFC_RND_MODE);
3231
3232 if (!mpfr_zero_p (diff))
3233 gfc_warning ("Change of value in conversion from "
3234 " %s to %s at %L", gfc_typename (&rvalue->ts),
3235 gfc_typename (&lvalue->ts), &rvalue->where);
3236
3237 mpfr_clear (rv);
3238 mpfr_clear (diff);
3239 }
3240 else
3241 gfc_warning ("Possible change of value in conversion from %s "
3242 "to %s at %L",gfc_typename (&rvalue->ts),
3243 gfc_typename (&lvalue->ts), &rvalue->where);
3244
3245 }
3246 else if (gfc_option.warn_conversion_extra
3247 && lvalue->ts.kind > rvalue->ts.kind)
3248 {
3249 gfc_warning ("Conversion from %s to %s at %L",
3250 gfc_typename (&rvalue->ts),
3251 gfc_typename (&lvalue->ts), &rvalue->where);
3252 }
3253 }
3254
3255 if (gfc_compare_types (&lvalue->ts, &rvalue->ts))
3256 return SUCCESS;
3257
3258 /* Only DATA Statements come here. */
3259 if (!conform)
3260 {
3261 /* Numeric can be converted to any other numeric. And Hollerith can be
3262 converted to any other type. */
3263 if ((gfc_numeric_ts (&lvalue->ts) && gfc_numeric_ts (&rvalue->ts))
3264 || rvalue->ts.type == BT_HOLLERITH)
3265 return SUCCESS;
3266
3267 if (lvalue->ts.type == BT_LOGICAL && rvalue->ts.type == BT_LOGICAL)
3268 return SUCCESS;
3269
3270 gfc_error ("Incompatible types in DATA statement at %L; attempted "
3271 "conversion of %s to %s", &lvalue->where,
3272 gfc_typename (&rvalue->ts), gfc_typename (&lvalue->ts));
3273
3274 return FAILURE;
3275 }
3276
3277 /* Assignment is the only case where character variables of different
3278 kind values can be converted into one another. */
3279 if (lvalue->ts.type == BT_CHARACTER && rvalue->ts.type == BT_CHARACTER)
3280 {
3281 if (lvalue->ts.kind != rvalue->ts.kind)
3282 gfc_convert_chartype (rvalue, &lvalue->ts);
3283
3284 return SUCCESS;
3285 }
3286
3287 return gfc_convert_type (rvalue, &lvalue->ts, 1);
3288 }
3289
3290
3291 /* Check that a pointer assignment is OK. We first check lvalue, and
3292 we only check rvalue if it's not an assignment to NULL() or a
3293 NULLIFY statement. */
3294
3295 gfc_try
3296 gfc_check_pointer_assign (gfc_expr *lvalue, gfc_expr *rvalue)
3297 {
3298 symbol_attribute attr;
3299 gfc_ref *ref;
3300 bool is_pure, is_implicit_pure, rank_remap;
3301 int proc_pointer;
3302
3303 if (lvalue->symtree->n.sym->ts.type == BT_UNKNOWN
3304 && !lvalue->symtree->n.sym->attr.proc_pointer)
3305 {
3306 gfc_error ("Pointer assignment target is not a POINTER at %L",
3307 &lvalue->where);
3308 return FAILURE;
3309 }
3310
3311 if (lvalue->symtree->n.sym->attr.flavor == FL_PROCEDURE
3312 && lvalue->symtree->n.sym->attr.use_assoc
3313 && !lvalue->symtree->n.sym->attr.proc_pointer)
3314 {
3315 gfc_error ("'%s' in the pointer assignment at %L cannot be an "
3316 "l-value since it is a procedure",
3317 lvalue->symtree->n.sym->name, &lvalue->where);
3318 return FAILURE;
3319 }
3320
3321 proc_pointer = lvalue->symtree->n.sym->attr.proc_pointer;
3322
3323 rank_remap = false;
3324 for (ref = lvalue->ref; ref; ref = ref->next)
3325 {
3326 if (ref->type == REF_COMPONENT)
3327 proc_pointer = ref->u.c.component->attr.proc_pointer;
3328
3329 if (ref->type == REF_ARRAY && ref->next == NULL)
3330 {
3331 int dim;
3332
3333 if (ref->u.ar.type == AR_FULL)
3334 break;
3335
3336 if (ref->u.ar.type != AR_SECTION)
3337 {
3338 gfc_error ("Expected bounds specification for '%s' at %L",
3339 lvalue->symtree->n.sym->name, &lvalue->where);
3340 return FAILURE;
3341 }
3342
3343 if (gfc_notify_std (GFC_STD_F2003,"Fortran 2003: Bounds "
3344 "specification for '%s' in pointer assignment "
3345 "at %L", lvalue->symtree->n.sym->name,
3346 &lvalue->where) == FAILURE)
3347 return FAILURE;
3348
3349 /* When bounds are given, all lbounds are necessary and either all
3350 or none of the upper bounds; no strides are allowed. If the
3351 upper bounds are present, we may do rank remapping. */
3352 for (dim = 0; dim < ref->u.ar.dimen; ++dim)
3353 {
3354 if (!ref->u.ar.start[dim]
3355 || ref->u.ar.dimen_type[dim] != DIMEN_RANGE)
3356 {
3357 gfc_error ("Lower bound has to be present at %L",
3358 &lvalue->where);
3359 return FAILURE;
3360 }
3361 if (ref->u.ar.stride[dim])
3362 {
3363 gfc_error ("Stride must not be present at %L",
3364 &lvalue->where);
3365 return FAILURE;
3366 }
3367
3368 if (dim == 0)
3369 rank_remap = (ref->u.ar.end[dim] != NULL);
3370 else
3371 {
3372 if ((rank_remap && !ref->u.ar.end[dim])
3373 || (!rank_remap && ref->u.ar.end[dim]))
3374 {
3375 gfc_error ("Either all or none of the upper bounds"
3376 " must be specified at %L", &lvalue->where);
3377 return FAILURE;
3378 }
3379 }
3380 }
3381 }
3382 }
3383
3384 is_pure = gfc_pure (NULL);
3385 is_implicit_pure = gfc_implicit_pure (NULL);
3386
3387 /* If rvalue is a NULL() or NULLIFY, we're done. Otherwise the type,
3388 kind, etc for lvalue and rvalue must match, and rvalue must be a
3389 pure variable if we're in a pure function. */
3390 if (rvalue->expr_type == EXPR_NULL && rvalue->ts.type == BT_UNKNOWN)
3391 return SUCCESS;
3392
3393 /* F2008, C723 (pointer) and C726 (proc-pointer); for PURE also C1283. */
3394 if (lvalue->expr_type == EXPR_VARIABLE
3395 && gfc_is_coindexed (lvalue))
3396 {
3397 gfc_ref *ref;
3398 for (ref = lvalue->ref; ref; ref = ref->next)
3399 if (ref->type == REF_ARRAY && ref->u.ar.codimen)
3400 {
3401 gfc_error ("Pointer object at %L shall not have a coindex",
3402 &lvalue->where);
3403 return FAILURE;
3404 }
3405 }
3406
3407 /* Checks on rvalue for procedure pointer assignments. */
3408 if (proc_pointer)
3409 {
3410 char err[200];
3411 gfc_symbol *s1,*s2;
3412 gfc_component *comp;
3413 const char *name;
3414
3415 attr = gfc_expr_attr (rvalue);
3416 if (!((rvalue->expr_type == EXPR_NULL)
3417 || (rvalue->expr_type == EXPR_FUNCTION && attr.proc_pointer)
3418 || (rvalue->expr_type == EXPR_VARIABLE && attr.proc_pointer)
3419 || (rvalue->expr_type == EXPR_VARIABLE
3420 && attr.flavor == FL_PROCEDURE)))
3421 {
3422 gfc_error ("Invalid procedure pointer assignment at %L",
3423 &rvalue->where);
3424 return FAILURE;
3425 }
3426 if (attr.abstract)
3427 {
3428 gfc_error ("Abstract interface '%s' is invalid "
3429 "in procedure pointer assignment at %L",
3430 rvalue->symtree->name, &rvalue->where);
3431 return FAILURE;
3432 }
3433 /* Check for C727. */
3434 if (attr.flavor == FL_PROCEDURE)
3435 {
3436 if (attr.proc == PROC_ST_FUNCTION)
3437 {
3438 gfc_error ("Statement function '%s' is invalid "
3439 "in procedure pointer assignment at %L",
3440 rvalue->symtree->name, &rvalue->where);
3441 return FAILURE;
3442 }
3443 if (attr.proc == PROC_INTERNAL &&
3444 gfc_notify_std (GFC_STD_F2008, "Internal procedure '%s' is "
3445 "invalid in procedure pointer assignment at %L",
3446 rvalue->symtree->name, &rvalue->where) == FAILURE)
3447 return FAILURE;
3448 }
3449
3450 /* Ensure that the calling convention is the same. As other attributes
3451 such as DLLEXPORT may differ, one explicitly only tests for the
3452 calling conventions. */
3453 if (rvalue->expr_type == EXPR_VARIABLE
3454 && lvalue->symtree->n.sym->attr.ext_attr
3455 != rvalue->symtree->n.sym->attr.ext_attr)
3456 {
3457 symbol_attribute calls;
3458
3459 calls.ext_attr = 0;
3460 gfc_add_ext_attribute (&calls, EXT_ATTR_CDECL, NULL);
3461 gfc_add_ext_attribute (&calls, EXT_ATTR_STDCALL, NULL);
3462 gfc_add_ext_attribute (&calls, EXT_ATTR_FASTCALL, NULL);
3463
3464 if ((calls.ext_attr & lvalue->symtree->n.sym->attr.ext_attr)
3465 != (calls.ext_attr & rvalue->symtree->n.sym->attr.ext_attr))
3466 {
3467 gfc_error ("Mismatch in the procedure pointer assignment "
3468 "at %L: mismatch in the calling convention",
3469 &rvalue->where);
3470 return FAILURE;
3471 }
3472 }
3473
3474 if (gfc_is_proc_ptr_comp (lvalue, &comp))
3475 s1 = comp->ts.interface;
3476 else
3477 s1 = lvalue->symtree->n.sym;
3478
3479 if (gfc_is_proc_ptr_comp (rvalue, &comp))
3480 {
3481 s2 = comp->ts.interface;
3482 name = comp->name;
3483 }
3484 else if (rvalue->expr_type == EXPR_FUNCTION)
3485 {
3486 s2 = rvalue->symtree->n.sym->result;
3487 name = rvalue->symtree->n.sym->result->name;
3488 }
3489 else
3490 {
3491 s2 = rvalue->symtree->n.sym;
3492 name = rvalue->symtree->n.sym->name;
3493 }
3494
3495 if (s1 && s2 && !gfc_compare_interfaces (s1, s2, name, 0, 1,
3496 err, sizeof(err)))
3497 {
3498 gfc_error ("Interface mismatch in procedure pointer assignment "
3499 "at %L: %s", &rvalue->where, err);
3500 return FAILURE;
3501 }
3502
3503 return SUCCESS;
3504 }
3505
3506 if (!gfc_compare_types (&lvalue->ts, &rvalue->ts))
3507 {
3508 gfc_error ("Different types in pointer assignment at %L; attempted "
3509 "assignment of %s to %s", &lvalue->where,
3510 gfc_typename (&rvalue->ts), gfc_typename (&lvalue->ts));
3511 return FAILURE;
3512 }
3513
3514 if (lvalue->ts.type != BT_CLASS && lvalue->ts.kind != rvalue->ts.kind)
3515 {
3516 gfc_error ("Different kind type parameters in pointer "
3517 "assignment at %L", &lvalue->where);
3518 return FAILURE;
3519 }
3520
3521 if (lvalue->rank != rvalue->rank && !rank_remap)
3522 {
3523 gfc_error ("Different ranks in pointer assignment at %L", &lvalue->where);
3524 return FAILURE;
3525 }
3526
3527 if (lvalue->ts.type == BT_CLASS && rvalue->ts.type == BT_DERIVED)
3528 /* Make sure the vtab is present. */
3529 gfc_find_derived_vtab (rvalue->ts.u.derived);
3530
3531 /* Check rank remapping. */
3532 if (rank_remap)
3533 {
3534 mpz_t lsize, rsize;
3535
3536 /* If this can be determined, check that the target must be at least as
3537 large as the pointer assigned to it is. */
3538 if (gfc_array_size (lvalue, &lsize) == SUCCESS
3539 && gfc_array_size (rvalue, &rsize) == SUCCESS
3540 && mpz_cmp (rsize, lsize) < 0)
3541 {
3542 gfc_error ("Rank remapping target is smaller than size of the"
3543 " pointer (%ld < %ld) at %L",
3544 mpz_get_si (rsize), mpz_get_si (lsize),
3545 &lvalue->where);
3546 return FAILURE;
3547 }
3548
3549 /* The target must be either rank one or it must be simply contiguous
3550 and F2008 must be allowed. */
3551 if (rvalue->rank != 1)
3552 {
3553 if (!gfc_is_simply_contiguous (rvalue, true))
3554 {
3555 gfc_error ("Rank remapping target must be rank 1 or"
3556 " simply contiguous at %L", &rvalue->where);
3557 return FAILURE;
3558 }
3559 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Rank remapping"
3560 " target is not rank 1 at %L", &rvalue->where)
3561 == FAILURE)
3562 return FAILURE;
3563 }
3564 }
3565
3566 /* Now punt if we are dealing with a NULLIFY(X) or X = NULL(X). */
3567 if (rvalue->expr_type == EXPR_NULL)
3568 return SUCCESS;
3569
3570 if (lvalue->ts.type == BT_CHARACTER)
3571 {
3572 gfc_try t = gfc_check_same_strlen (lvalue, rvalue, "pointer assignment");
3573 if (t == FAILURE)
3574 return FAILURE;
3575 }
3576
3577 if (rvalue->expr_type == EXPR_VARIABLE && is_subref_array (rvalue))
3578 lvalue->symtree->n.sym->attr.subref_array_pointer = 1;
3579
3580 attr = gfc_expr_attr (rvalue);
3581
3582 if (rvalue->expr_type == EXPR_FUNCTION && !attr.pointer)
3583 {
3584 gfc_error ("Target expression in pointer assignment "
3585 "at %L must deliver a pointer result",
3586 &rvalue->where);
3587 return FAILURE;
3588 }
3589
3590 if (!attr.target && !attr.pointer)
3591 {
3592 gfc_error ("Pointer assignment target is neither TARGET "
3593 "nor POINTER at %L", &rvalue->where);
3594 return FAILURE;
3595 }
3596
3597 if (is_pure && gfc_impure_variable (rvalue->symtree->n.sym))
3598 {
3599 gfc_error ("Bad target in pointer assignment in PURE "
3600 "procedure at %L", &rvalue->where);
3601 }
3602
3603 if (is_implicit_pure && gfc_impure_variable (rvalue->symtree->n.sym))
3604 gfc_current_ns->proc_name->attr.implicit_pure = 0;
3605
3606
3607 if (gfc_has_vector_index (rvalue))
3608 {
3609 gfc_error ("Pointer assignment with vector subscript "
3610 "on rhs at %L", &rvalue->where);
3611 return FAILURE;
3612 }
3613
3614 if (attr.is_protected && attr.use_assoc
3615 && !(attr.pointer || attr.proc_pointer))
3616 {
3617 gfc_error ("Pointer assignment target has PROTECTED "
3618 "attribute at %L", &rvalue->where);
3619 return FAILURE;
3620 }
3621
3622 /* F2008, C725. For PURE also C1283. */
3623 if (rvalue->expr_type == EXPR_VARIABLE
3624 && gfc_is_coindexed (rvalue))
3625 {
3626 gfc_ref *ref;
3627 for (ref = rvalue->ref; ref; ref = ref->next)
3628 if (ref->type == REF_ARRAY && ref->u.ar.codimen)
3629 {
3630 gfc_error ("Data target at %L shall not have a coindex",
3631 &rvalue->where);
3632 return FAILURE;
3633 }
3634 }
3635
3636 return SUCCESS;
3637 }
3638
3639
3640 /* Relative of gfc_check_assign() except that the lvalue is a single
3641 symbol. Used for initialization assignments. */
3642
3643 gfc_try
3644 gfc_check_assign_symbol (gfc_symbol *sym, gfc_expr *rvalue)
3645 {
3646 gfc_expr lvalue;
3647 gfc_try r;
3648
3649 memset (&lvalue, '\0', sizeof (gfc_expr));
3650
3651 lvalue.expr_type = EXPR_VARIABLE;
3652 lvalue.ts = sym->ts;
3653 if (sym->as)
3654 lvalue.rank = sym->as->rank;
3655 lvalue.symtree = XCNEW (gfc_symtree);
3656 lvalue.symtree->n.sym = sym;
3657 lvalue.where = sym->declared_at;
3658
3659 if (sym->attr.pointer || sym->attr.proc_pointer
3660 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer
3661 && rvalue->expr_type == EXPR_NULL))
3662 r = gfc_check_pointer_assign (&lvalue, rvalue);
3663 else
3664 r = gfc_check_assign (&lvalue, rvalue, 1);
3665
3666 free (lvalue.symtree);
3667
3668 if (r == FAILURE)
3669 return r;
3670
3671 if (sym->attr.pointer && rvalue->expr_type != EXPR_NULL)
3672 {
3673 /* F08:C461. Additional checks for pointer initialization. */
3674 symbol_attribute attr;
3675 attr = gfc_expr_attr (rvalue);
3676 if (attr.allocatable)
3677 {
3678 gfc_error ("Pointer initialization target at %C "
3679 "must not be ALLOCATABLE ");
3680 return FAILURE;
3681 }
3682 if (!attr.target || attr.pointer)
3683 {
3684 gfc_error ("Pointer initialization target at %C "
3685 "must have the TARGET attribute");
3686 return FAILURE;
3687 }
3688 if (!attr.save)
3689 {
3690 gfc_error ("Pointer initialization target at %C "
3691 "must have the SAVE attribute");
3692 return FAILURE;
3693 }
3694 }
3695
3696 if (sym->attr.proc_pointer && rvalue->expr_type != EXPR_NULL)
3697 {
3698 /* F08:C1220. Additional checks for procedure pointer initialization. */
3699 symbol_attribute attr = gfc_expr_attr (rvalue);
3700 if (attr.proc_pointer)
3701 {
3702 gfc_error ("Procedure pointer initialization target at %L "
3703 "may not be a procedure pointer", &rvalue->where);
3704 return FAILURE;
3705 }
3706 }
3707
3708 return SUCCESS;
3709 }
3710
3711
3712 /* Check for default initializer; sym->value is not enough
3713 as it is also set for EXPR_NULL of allocatables. */
3714
3715 bool
3716 gfc_has_default_initializer (gfc_symbol *der)
3717 {
3718 gfc_component *c;
3719
3720 gcc_assert (der->attr.flavor == FL_DERIVED);
3721 for (c = der->components; c; c = c->next)
3722 if (c->ts.type == BT_DERIVED)
3723 {
3724 if (!c->attr.pointer
3725 && gfc_has_default_initializer (c->ts.u.derived))
3726 return true;
3727 }
3728 else
3729 {
3730 if (c->initializer)
3731 return true;
3732 }
3733
3734 return false;
3735 }
3736
3737 /* Get an expression for a default initializer. */
3738
3739 gfc_expr *
3740 gfc_default_initializer (gfc_typespec *ts)
3741 {
3742 gfc_expr *init;
3743 gfc_component *comp;
3744
3745 /* See if we have a default initializer in this, but not in nested
3746 types (otherwise we could use gfc_has_default_initializer()). */
3747 for (comp = ts->u.derived->components; comp; comp = comp->next)
3748 if (comp->initializer || comp->attr.allocatable
3749 || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)->attr.allocatable))
3750 break;
3751
3752 if (!comp)
3753 return NULL;
3754
3755 init = gfc_get_structure_constructor_expr (ts->type, ts->kind,
3756 &ts->u.derived->declared_at);
3757 init->ts = *ts;
3758
3759 for (comp = ts->u.derived->components; comp; comp = comp->next)
3760 {
3761 gfc_constructor *ctor = gfc_constructor_get();
3762
3763 if (comp->initializer)
3764 ctor->expr = gfc_copy_expr (comp->initializer);
3765
3766 if (comp->attr.allocatable
3767 || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)->attr.allocatable))
3768 {
3769 ctor->expr = gfc_get_expr ();
3770 ctor->expr->expr_type = EXPR_NULL;
3771 ctor->expr->ts = comp->ts;
3772 }
3773
3774 gfc_constructor_append (&init->value.constructor, ctor);
3775 }
3776
3777 return init;
3778 }
3779
3780
3781 /* Given a symbol, create an expression node with that symbol as a
3782 variable. If the symbol is array valued, setup a reference of the
3783 whole array. */
3784
3785 gfc_expr *
3786 gfc_get_variable_expr (gfc_symtree *var)
3787 {
3788 gfc_expr *e;
3789
3790 e = gfc_get_expr ();
3791 e->expr_type = EXPR_VARIABLE;
3792 e->symtree = var;
3793 e->ts = var->n.sym->ts;
3794
3795 if (var->n.sym->as != NULL)
3796 {
3797 e->rank = var->n.sym->as->rank;
3798 e->ref = gfc_get_ref ();
3799 e->ref->type = REF_ARRAY;
3800 e->ref->u.ar.type = AR_FULL;
3801 }
3802
3803 return e;
3804 }
3805
3806
3807 gfc_expr *
3808 gfc_lval_expr_from_sym (gfc_symbol *sym)
3809 {
3810 gfc_expr *lval;
3811 lval = gfc_get_expr ();
3812 lval->expr_type = EXPR_VARIABLE;
3813 lval->where = sym->declared_at;
3814 lval->ts = sym->ts;
3815 lval->symtree = gfc_find_symtree (sym->ns->sym_root, sym->name);
3816
3817 /* It will always be a full array. */
3818 lval->rank = sym->as ? sym->as->rank : 0;
3819 if (lval->rank)
3820 {
3821 lval->ref = gfc_get_ref ();
3822 lval->ref->type = REF_ARRAY;
3823 lval->ref->u.ar.type = AR_FULL;
3824 lval->ref->u.ar.dimen = lval->rank;
3825 lval->ref->u.ar.where = sym->declared_at;
3826 lval->ref->u.ar.as = sym->as;
3827 }
3828
3829 return lval;
3830 }
3831
3832
3833 /* Returns the array_spec of a full array expression. A NULL is
3834 returned otherwise. */
3835 gfc_array_spec *
3836 gfc_get_full_arrayspec_from_expr (gfc_expr *expr)
3837 {
3838 gfc_array_spec *as;
3839 gfc_ref *ref;
3840
3841 if (expr->rank == 0)
3842 return NULL;
3843
3844 /* Follow any component references. */
3845 if (expr->expr_type == EXPR_VARIABLE
3846 || expr->expr_type == EXPR_CONSTANT)
3847 {
3848 as = expr->symtree->n.sym->as;
3849 for (ref = expr->ref; ref; ref = ref->next)
3850 {
3851 switch (ref->type)
3852 {
3853 case REF_COMPONENT:
3854 as = ref->u.c.component->as;
3855 continue;
3856
3857 case REF_SUBSTRING:
3858 continue;
3859
3860 case REF_ARRAY:
3861 {
3862 switch (ref->u.ar.type)
3863 {
3864 case AR_ELEMENT:
3865 case AR_SECTION:
3866 case AR_UNKNOWN:
3867 as = NULL;
3868 continue;
3869
3870 case AR_FULL:
3871 break;
3872 }
3873 break;
3874 }
3875 }
3876 }
3877 }
3878 else
3879 as = NULL;
3880
3881 return as;
3882 }
3883
3884
3885 /* General expression traversal function. */
3886
3887 bool
3888 gfc_traverse_expr (gfc_expr *expr, gfc_symbol *sym,
3889 bool (*func)(gfc_expr *, gfc_symbol *, int*),
3890 int f)
3891 {
3892 gfc_array_ref ar;
3893 gfc_ref *ref;
3894 gfc_actual_arglist *args;
3895 gfc_constructor *c;
3896 int i;
3897
3898 if (!expr)
3899 return false;
3900
3901 if ((*func) (expr, sym, &f))
3902 return true;
3903
3904 if (expr->ts.type == BT_CHARACTER
3905 && expr->ts.u.cl
3906 && expr->ts.u.cl->length
3907 && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT
3908 && gfc_traverse_expr (expr->ts.u.cl->length, sym, func, f))
3909 return true;
3910
3911 switch (expr->expr_type)
3912 {
3913 case EXPR_PPC:
3914 case EXPR_COMPCALL:
3915 case EXPR_FUNCTION:
3916 for (args = expr->value.function.actual; args; args = args->next)
3917 {
3918 if (gfc_traverse_expr (args->expr, sym, func, f))
3919 return true;
3920 }
3921 break;
3922
3923 case EXPR_VARIABLE:
3924 case EXPR_CONSTANT:
3925 case EXPR_NULL:
3926 case EXPR_SUBSTRING:
3927 break;
3928
3929 case EXPR_STRUCTURE:
3930 case EXPR_ARRAY:
3931 for (c = gfc_constructor_first (expr->value.constructor);
3932 c; c = gfc_constructor_next (c))
3933 {
3934 if (gfc_traverse_expr (c->expr, sym, func, f))
3935 return true;
3936 if (c->iterator)
3937 {
3938 if (gfc_traverse_expr (c->iterator->var, sym, func, f))
3939 return true;
3940 if (gfc_traverse_expr (c->iterator->start, sym, func, f))
3941 return true;
3942 if (gfc_traverse_expr (c->iterator->end, sym, func, f))
3943 return true;
3944 if (gfc_traverse_expr (c->iterator->step, sym, func, f))
3945 return true;
3946 }
3947 }
3948 break;
3949
3950 case EXPR_OP:
3951 if (gfc_traverse_expr (expr->value.op.op1, sym, func, f))
3952 return true;
3953 if (gfc_traverse_expr (expr->value.op.op2, sym, func, f))
3954 return true;
3955 break;
3956
3957 default:
3958 gcc_unreachable ();
3959 break;
3960 }
3961
3962 ref = expr->ref;
3963 while (ref != NULL)
3964 {
3965 switch (ref->type)
3966 {
3967 case REF_ARRAY:
3968 ar = ref->u.ar;
3969 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
3970 {
3971 if (gfc_traverse_expr (ar.start[i], sym, func, f))
3972 return true;
3973 if (gfc_traverse_expr (ar.end[i], sym, func, f))
3974 return true;
3975 if (gfc_traverse_expr (ar.stride[i], sym, func, f))
3976 return true;
3977 }
3978 break;
3979
3980 case REF_SUBSTRING:
3981 if (gfc_traverse_expr (ref->u.ss.start, sym, func, f))
3982 return true;
3983 if (gfc_traverse_expr (ref->u.ss.end, sym, func, f))
3984 return true;
3985 break;
3986
3987 case REF_COMPONENT:
3988 if (ref->u.c.component->ts.type == BT_CHARACTER
3989 && ref->u.c.component->ts.u.cl
3990 && ref->u.c.component->ts.u.cl->length
3991 && ref->u.c.component->ts.u.cl->length->expr_type
3992 != EXPR_CONSTANT
3993 && gfc_traverse_expr (ref->u.c.component->ts.u.cl->length,
3994 sym, func, f))
3995 return true;
3996
3997 if (ref->u.c.component->as)
3998 for (i = 0; i < ref->u.c.component->as->rank
3999 + ref->u.c.component->as->corank; i++)
4000 {
4001 if (gfc_traverse_expr (ref->u.c.component->as->lower[i],
4002 sym, func, f))
4003 return true;
4004 if (gfc_traverse_expr (ref->u.c.component->as->upper[i],
4005 sym, func, f))
4006 return true;
4007 }
4008 break;
4009
4010 default:
4011 gcc_unreachable ();
4012 }
4013 ref = ref->next;
4014 }
4015 return false;
4016 }
4017
4018 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */
4019
4020 static bool
4021 expr_set_symbols_referenced (gfc_expr *expr,
4022 gfc_symbol *sym ATTRIBUTE_UNUSED,
4023 int *f ATTRIBUTE_UNUSED)
4024 {
4025 if (expr->expr_type != EXPR_VARIABLE)
4026 return false;
4027 gfc_set_sym_referenced (expr->symtree->n.sym);
4028 return false;
4029 }
4030
4031 void
4032 gfc_expr_set_symbols_referenced (gfc_expr *expr)
4033 {
4034 gfc_traverse_expr (expr, NULL, expr_set_symbols_referenced, 0);
4035 }
4036
4037
4038 /* Determine if an expression is a procedure pointer component. If yes, the
4039 argument 'comp' will point to the component (provided that 'comp' was
4040 provided). */
4041
4042 bool
4043 gfc_is_proc_ptr_comp (gfc_expr *expr, gfc_component **comp)
4044 {
4045 gfc_ref *ref;
4046 bool ppc = false;
4047
4048 if (!expr || !expr->ref)
4049 return false;
4050
4051 ref = expr->ref;
4052 while (ref->next)
4053 ref = ref->next;
4054
4055 if (ref->type == REF_COMPONENT)
4056 {
4057 ppc = ref->u.c.component->attr.proc_pointer;
4058 if (ppc && comp)
4059 *comp = ref->u.c.component;
4060 }
4061
4062 return ppc;
4063 }
4064
4065
4066 /* Walk an expression tree and check each variable encountered for being typed.
4067 If strict is not set, a top-level variable is tolerated untyped in -std=gnu
4068 mode as is a basic arithmetic expression using those; this is for things in
4069 legacy-code like:
4070
4071 INTEGER :: arr(n), n
4072 INTEGER :: arr(n + 1), n
4073
4074 The namespace is needed for IMPLICIT typing. */
4075
4076 static gfc_namespace* check_typed_ns;
4077
4078 static bool
4079 expr_check_typed_help (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
4080 int* f ATTRIBUTE_UNUSED)
4081 {
4082 gfc_try t;
4083
4084 if (e->expr_type != EXPR_VARIABLE)
4085 return false;
4086
4087 gcc_assert (e->symtree);
4088 t = gfc_check_symbol_typed (e->symtree->n.sym, check_typed_ns,
4089 true, e->where);
4090
4091 return (t == FAILURE);
4092 }
4093
4094 gfc_try
4095 gfc_expr_check_typed (gfc_expr* e, gfc_namespace* ns, bool strict)
4096 {
4097 bool error_found;
4098
4099 /* If this is a top-level variable or EXPR_OP, do the check with strict given
4100 to us. */
4101 if (!strict)
4102 {
4103 if (e->expr_type == EXPR_VARIABLE && !e->ref)
4104 return gfc_check_symbol_typed (e->symtree->n.sym, ns, strict, e->where);
4105
4106 if (e->expr_type == EXPR_OP)
4107 {
4108 gfc_try t = SUCCESS;
4109
4110 gcc_assert (e->value.op.op1);
4111 t = gfc_expr_check_typed (e->value.op.op1, ns, strict);
4112
4113 if (t == SUCCESS && e->value.op.op2)
4114 t = gfc_expr_check_typed (e->value.op.op2, ns, strict);
4115
4116 return t;
4117 }
4118 }
4119
4120 /* Otherwise, walk the expression and do it strictly. */
4121 check_typed_ns = ns;
4122 error_found = gfc_traverse_expr (e, NULL, &expr_check_typed_help, 0);
4123
4124 return error_found ? FAILURE : SUCCESS;
4125 }
4126
4127 /* Walk an expression tree and replace all symbols with a corresponding symbol
4128 in the formal_ns of "sym". Needed for copying interfaces in PROCEDURE
4129 statements. The boolean return value is required by gfc_traverse_expr. */
4130
4131 static bool
4132 replace_symbol (gfc_expr *expr, gfc_symbol *sym, int *i ATTRIBUTE_UNUSED)
4133 {
4134 if ((expr->expr_type == EXPR_VARIABLE
4135 || (expr->expr_type == EXPR_FUNCTION
4136 && !gfc_is_intrinsic (expr->symtree->n.sym, 0, expr->where)))
4137 && expr->symtree->n.sym->ns == sym->ts.interface->formal_ns)
4138 {
4139 gfc_symtree *stree;
4140 gfc_namespace *ns = sym->formal_ns;
4141 /* Don't use gfc_get_symtree as we prefer to fail badly if we don't find
4142 the symtree rather than create a new one (and probably fail later). */
4143 stree = gfc_find_symtree (ns ? ns->sym_root : gfc_current_ns->sym_root,
4144 expr->symtree->n.sym->name);
4145 gcc_assert (stree);
4146 stree->n.sym->attr = expr->symtree->n.sym->attr;
4147 expr->symtree = stree;
4148 }
4149 return false;
4150 }
4151
4152 void
4153 gfc_expr_replace_symbols (gfc_expr *expr, gfc_symbol *dest)
4154 {
4155 gfc_traverse_expr (expr, dest, &replace_symbol, 0);
4156 }
4157
4158 /* The following is analogous to 'replace_symbol', and needed for copying
4159 interfaces for procedure pointer components. The argument 'sym' must formally
4160 be a gfc_symbol, so that the function can be passed to gfc_traverse_expr.
4161 However, it gets actually passed a gfc_component (i.e. the procedure pointer
4162 component in whose formal_ns the arguments have to be). */
4163
4164 static bool
4165 replace_comp (gfc_expr *expr, gfc_symbol *sym, int *i ATTRIBUTE_UNUSED)
4166 {
4167 gfc_component *comp;
4168 comp = (gfc_component *)sym;
4169 if ((expr->expr_type == EXPR_VARIABLE
4170 || (expr->expr_type == EXPR_FUNCTION
4171 && !gfc_is_intrinsic (expr->symtree->n.sym, 0, expr->where)))
4172 && expr->symtree->n.sym->ns == comp->ts.interface->formal_ns)
4173 {
4174 gfc_symtree *stree;
4175 gfc_namespace *ns = comp->formal_ns;
4176 /* Don't use gfc_get_symtree as we prefer to fail badly if we don't find
4177 the symtree rather than create a new one (and probably fail later). */
4178 stree = gfc_find_symtree (ns ? ns->sym_root : gfc_current_ns->sym_root,
4179 expr->symtree->n.sym->name);
4180 gcc_assert (stree);
4181 stree->n.sym->attr = expr->symtree->n.sym->attr;
4182 expr->symtree = stree;
4183 }
4184 return false;
4185 }
4186
4187 void
4188 gfc_expr_replace_comp (gfc_expr *expr, gfc_component *dest)
4189 {
4190 gfc_traverse_expr (expr, (gfc_symbol *)dest, &replace_comp, 0);
4191 }
4192
4193
4194 bool
4195 gfc_ref_this_image (gfc_ref *ref)
4196 {
4197 int n;
4198
4199 gcc_assert (ref->type == REF_ARRAY && ref->u.ar.codimen > 0);
4200
4201 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
4202 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
4203 return false;
4204
4205 return true;
4206 }
4207
4208
4209 bool
4210 gfc_is_coindexed (gfc_expr *e)
4211 {
4212 gfc_ref *ref;
4213
4214 for (ref = e->ref; ref; ref = ref->next)
4215 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
4216 return !gfc_ref_this_image (ref);
4217
4218 return false;
4219 }
4220
4221
4222 /* Coarrays are variables with a corank but not being coindexed. However, also
4223 the following is a coarray: A subobject of a coarray is a coarray if it does
4224 not have any cosubscripts, vector subscripts, allocatable component
4225 selection, or pointer component selection. (F2008, 2.4.7) */
4226
4227 bool
4228 gfc_is_coarray (gfc_expr *e)
4229 {
4230 gfc_ref *ref;
4231 gfc_symbol *sym;
4232 gfc_component *comp;
4233 bool coindexed;
4234 bool coarray;
4235 int i;
4236
4237 if (e->expr_type != EXPR_VARIABLE)
4238 return false;
4239
4240 coindexed = false;
4241 sym = e->symtree->n.sym;
4242
4243 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
4244 coarray = CLASS_DATA (sym)->attr.codimension;
4245 else
4246 coarray = sym->attr.codimension;
4247
4248 for (ref = e->ref; ref; ref = ref->next)
4249 switch (ref->type)
4250 {
4251 case REF_COMPONENT:
4252 comp = ref->u.c.component;
4253 if (comp->attr.pointer || comp->attr.allocatable)
4254 {
4255 coindexed = false;
4256 if (comp->ts.type == BT_CLASS && comp->attr.class_ok)
4257 coarray = CLASS_DATA (comp)->attr.codimension;
4258 else
4259 coarray = comp->attr.codimension;
4260 }
4261 break;
4262
4263 case REF_ARRAY:
4264 if (!coarray)
4265 break;
4266
4267 if (ref->u.ar.codimen > 0 && !gfc_ref_this_image (ref))
4268 {
4269 coindexed = true;
4270 break;
4271 }
4272
4273 for (i = 0; i < ref->u.ar.dimen; i++)
4274 if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4275 {
4276 coarray = false;
4277 break;
4278 }
4279 break;
4280
4281 case REF_SUBSTRING:
4282 break;
4283 }
4284
4285 return coarray && !coindexed;
4286 }
4287
4288
4289 int
4290 gfc_get_corank (gfc_expr *e)
4291 {
4292 int corank;
4293 gfc_ref *ref;
4294 corank = e->symtree->n.sym->as ? e->symtree->n.sym->as->corank : 0;
4295 for (ref = e->ref; ref; ref = ref->next)
4296 {
4297 if (ref->type == REF_ARRAY)
4298 corank = ref->u.ar.as->corank;
4299 gcc_assert (ref->type != REF_SUBSTRING);
4300 }
4301 return corank;
4302 }
4303
4304
4305 /* Check whether the expression has an ultimate allocatable component.
4306 Being itself allocatable does not count. */
4307 bool
4308 gfc_has_ultimate_allocatable (gfc_expr *e)
4309 {
4310 gfc_ref *ref, *last = NULL;
4311
4312 if (e->expr_type != EXPR_VARIABLE)
4313 return false;
4314
4315 for (ref = e->ref; ref; ref = ref->next)
4316 if (ref->type == REF_COMPONENT)
4317 last = ref;
4318
4319 if (last && last->u.c.component->ts.type == BT_CLASS)
4320 return CLASS_DATA (last->u.c.component)->attr.alloc_comp;
4321 else if (last && last->u.c.component->ts.type == BT_DERIVED)
4322 return last->u.c.component->ts.u.derived->attr.alloc_comp;
4323 else if (last)
4324 return false;
4325
4326 if (e->ts.type == BT_CLASS)
4327 return CLASS_DATA (e)->attr.alloc_comp;
4328 else if (e->ts.type == BT_DERIVED)
4329 return e->ts.u.derived->attr.alloc_comp;
4330 else
4331 return false;
4332 }
4333
4334
4335 /* Check whether the expression has an pointer component.
4336 Being itself a pointer does not count. */
4337 bool
4338 gfc_has_ultimate_pointer (gfc_expr *e)
4339 {
4340 gfc_ref *ref, *last = NULL;
4341
4342 if (e->expr_type != EXPR_VARIABLE)
4343 return false;
4344
4345 for (ref = e->ref; ref; ref = ref->next)
4346 if (ref->type == REF_COMPONENT)
4347 last = ref;
4348
4349 if (last && last->u.c.component->ts.type == BT_CLASS)
4350 return CLASS_DATA (last->u.c.component)->attr.pointer_comp;
4351 else if (last && last->u.c.component->ts.type == BT_DERIVED)
4352 return last->u.c.component->ts.u.derived->attr.pointer_comp;
4353 else if (last)
4354 return false;
4355
4356 if (e->ts.type == BT_CLASS)
4357 return CLASS_DATA (e)->attr.pointer_comp;
4358 else if (e->ts.type == BT_DERIVED)
4359 return e->ts.u.derived->attr.pointer_comp;
4360 else
4361 return false;
4362 }
4363
4364
4365 /* Check whether an expression is "simply contiguous", cf. F2008, 6.5.4.
4366 Note: A scalar is not regarded as "simply contiguous" by the standard.
4367 if bool is not strict, some futher checks are done - for instance,
4368 a "(::1)" is accepted. */
4369
4370 bool
4371 gfc_is_simply_contiguous (gfc_expr *expr, bool strict)
4372 {
4373 bool colon;
4374 int i;
4375 gfc_array_ref *ar = NULL;
4376 gfc_ref *ref, *part_ref = NULL;
4377
4378 if (expr->expr_type == EXPR_FUNCTION)
4379 return expr->value.function.esym
4380 ? expr->value.function.esym->result->attr.contiguous : false;
4381 else if (expr->expr_type != EXPR_VARIABLE)
4382 return false;
4383
4384 if (expr->rank == 0)
4385 return false;
4386
4387 for (ref = expr->ref; ref; ref = ref->next)
4388 {
4389 if (ar)
4390 return false; /* Array shall be last part-ref. */
4391
4392 if (ref->type == REF_COMPONENT)
4393 part_ref = ref;
4394 else if (ref->type == REF_SUBSTRING)
4395 return false;
4396 else if (ref->u.ar.type != AR_ELEMENT)
4397 ar = &ref->u.ar;
4398 }
4399
4400 if ((part_ref && !part_ref->u.c.component->attr.contiguous
4401 && part_ref->u.c.component->attr.pointer)
4402 || (!part_ref && !expr->symtree->n.sym->attr.contiguous
4403 && (expr->symtree->n.sym->attr.pointer
4404 || expr->symtree->n.sym->as->type == AS_ASSUMED_SHAPE)))
4405 return false;
4406
4407 if (!ar || ar->type == AR_FULL)
4408 return true;
4409
4410 gcc_assert (ar->type == AR_SECTION);
4411
4412 /* Check for simply contiguous array */
4413 colon = true;
4414 for (i = 0; i < ar->dimen; i++)
4415 {
4416 if (ar->dimen_type[i] == DIMEN_VECTOR)
4417 return false;
4418
4419 if (ar->dimen_type[i] == DIMEN_ELEMENT)
4420 {
4421 colon = false;
4422 continue;
4423 }
4424
4425 gcc_assert (ar->dimen_type[i] == DIMEN_RANGE);
4426
4427
4428 /* If the previous section was not contiguous, that's an error,
4429 unless we have effective only one element and checking is not
4430 strict. */
4431 if (!colon && (strict || !ar->start[i] || !ar->end[i]
4432 || ar->start[i]->expr_type != EXPR_CONSTANT
4433 || ar->end[i]->expr_type != EXPR_CONSTANT
4434 || mpz_cmp (ar->start[i]->value.integer,
4435 ar->end[i]->value.integer) != 0))
4436 return false;
4437
4438 /* Following the standard, "(::1)" or - if known at compile time -
4439 "(lbound:ubound)" are not simply contigous; if strict
4440 is false, they are regarded as simply contiguous. */
4441 if (ar->stride[i] && (strict || ar->stride[i]->expr_type != EXPR_CONSTANT
4442 || ar->stride[i]->ts.type != BT_INTEGER
4443 || mpz_cmp_si (ar->stride[i]->value.integer, 1) != 0))
4444 return false;
4445
4446 if (ar->start[i]
4447 && (strict || ar->start[i]->expr_type != EXPR_CONSTANT
4448 || !ar->as->lower[i]
4449 || ar->as->lower[i]->expr_type != EXPR_CONSTANT
4450 || mpz_cmp (ar->start[i]->value.integer,
4451 ar->as->lower[i]->value.integer) != 0))
4452 colon = false;
4453
4454 if (ar->end[i]
4455 && (strict || ar->end[i]->expr_type != EXPR_CONSTANT
4456 || !ar->as->upper[i]
4457 || ar->as->upper[i]->expr_type != EXPR_CONSTANT
4458 || mpz_cmp (ar->end[i]->value.integer,
4459 ar->as->upper[i]->value.integer) != 0))
4460 colon = false;
4461 }
4462
4463 return true;
4464 }
4465
4466
4467 /* Build call to an intrinsic procedure. The number of arguments has to be
4468 passed (rather than ending the list with a NULL value) because we may
4469 want to add arguments but with a NULL-expression. */
4470
4471 gfc_expr*
4472 gfc_build_intrinsic_call (const char* name, locus where, unsigned numarg, ...)
4473 {
4474 gfc_expr* result;
4475 gfc_actual_arglist* atail;
4476 gfc_intrinsic_sym* isym;
4477 va_list ap;
4478 unsigned i;
4479
4480 isym = gfc_find_function (name);
4481 gcc_assert (isym);
4482
4483 result = gfc_get_expr ();
4484 result->expr_type = EXPR_FUNCTION;
4485 result->ts = isym->ts;
4486 result->where = where;
4487 result->value.function.name = name;
4488 result->value.function.isym = isym;
4489
4490 va_start (ap, numarg);
4491 atail = NULL;
4492 for (i = 0; i < numarg; ++i)
4493 {
4494 if (atail)
4495 {
4496 atail->next = gfc_get_actual_arglist ();
4497 atail = atail->next;
4498 }
4499 else
4500 atail = result->value.function.actual = gfc_get_actual_arglist ();
4501
4502 atail->expr = va_arg (ap, gfc_expr*);
4503 }
4504 va_end (ap);
4505
4506 return result;
4507 }
4508
4509
4510 /* Check if an expression may appear in a variable definition context
4511 (F2008, 16.6.7) or pointer association context (F2008, 16.6.8).
4512 This is called from the various places when resolving
4513 the pieces that make up such a context.
4514
4515 Optionally, a possible error message can be suppressed if context is NULL
4516 and just the return status (SUCCESS / FAILURE) be requested. */
4517
4518 gfc_try
4519 gfc_check_vardef_context (gfc_expr* e, bool pointer, bool alloc_obj,
4520 const char* context)
4521 {
4522 gfc_symbol* sym = NULL;
4523 bool is_pointer;
4524 bool check_intentin;
4525 bool ptr_component;
4526 symbol_attribute attr;
4527 gfc_ref* ref;
4528
4529 if (e->expr_type == EXPR_VARIABLE)
4530 {
4531 gcc_assert (e->symtree);
4532 sym = e->symtree->n.sym;
4533 }
4534 else if (e->expr_type == EXPR_FUNCTION)
4535 {
4536 gcc_assert (e->symtree);
4537 sym = e->value.function.esym ? e->value.function.esym : e->symtree->n.sym;
4538 }
4539
4540 attr = gfc_expr_attr (e);
4541 if (!pointer && e->expr_type == EXPR_FUNCTION && attr.pointer)
4542 {
4543 if (!(gfc_option.allow_std & GFC_STD_F2008))
4544 {
4545 if (context)
4546 gfc_error ("Fortran 2008: Pointer functions in variable definition"
4547 " context (%s) at %L", context, &e->where);
4548 return FAILURE;
4549 }
4550 }
4551 else if (e->expr_type != EXPR_VARIABLE)
4552 {
4553 if (context)
4554 gfc_error ("Non-variable expression in variable definition context (%s)"
4555 " at %L", context, &e->where);
4556 return FAILURE;
4557 }
4558
4559 if (!pointer && sym->attr.flavor == FL_PARAMETER)
4560 {
4561 if (context)
4562 gfc_error ("Named constant '%s' in variable definition context (%s)"
4563 " at %L", sym->name, context, &e->where);
4564 return FAILURE;
4565 }
4566 if (!pointer && sym->attr.flavor != FL_VARIABLE
4567 && !(sym->attr.flavor == FL_PROCEDURE && sym == sym->result)
4568 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.proc_pointer))
4569 {
4570 if (context)
4571 gfc_error ("'%s' in variable definition context (%s) at %L is not"
4572 " a variable", sym->name, context, &e->where);
4573 return FAILURE;
4574 }
4575
4576 /* Find out whether the expr is a pointer; this also means following
4577 component references to the last one. */
4578 is_pointer = (attr.pointer || attr.proc_pointer);
4579 if (pointer && !is_pointer)
4580 {
4581 if (context)
4582 gfc_error ("Non-POINTER in pointer association context (%s)"
4583 " at %L", context, &e->where);
4584 return FAILURE;
4585 }
4586
4587 /* F2008, C1303. */
4588 if (!alloc_obj
4589 && (attr.lock_comp
4590 || (e->ts.type == BT_DERIVED
4591 && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
4592 && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)))
4593 {
4594 if (context)
4595 gfc_error ("LOCK_TYPE in variable definition context (%s) at %L",
4596 context, &e->where);
4597 return FAILURE;
4598 }
4599
4600 /* INTENT(IN) dummy argument. Check this, unless the object itself is
4601 the component of sub-component of a pointer. Obviously,
4602 procedure pointers are of no interest here. */
4603 check_intentin = true;
4604 ptr_component = sym->attr.pointer;
4605 for (ref = e->ref; ref && check_intentin; ref = ref->next)
4606 {
4607 if (ptr_component && ref->type == REF_COMPONENT)
4608 check_intentin = false;
4609 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
4610 ptr_component = true;
4611 }
4612 if (check_intentin && sym->attr.intent == INTENT_IN)
4613 {
4614 if (pointer && is_pointer)
4615 {
4616 if (context)
4617 gfc_error ("Dummy argument '%s' with INTENT(IN) in pointer"
4618 " association context (%s) at %L",
4619 sym->name, context, &e->where);
4620 return FAILURE;
4621 }
4622 if (!pointer && !is_pointer)
4623 {
4624 if (context)
4625 gfc_error ("Dummy argument '%s' with INTENT(IN) in variable"
4626 " definition context (%s) at %L",
4627 sym->name, context, &e->where);
4628 return FAILURE;
4629 }
4630 }
4631
4632 /* PROTECTED and use-associated. */
4633 if (sym->attr.is_protected && sym->attr.use_assoc && check_intentin)
4634 {
4635 if (pointer && is_pointer)
4636 {
4637 if (context)
4638 gfc_error ("Variable '%s' is PROTECTED and can not appear in a"
4639 " pointer association context (%s) at %L",
4640 sym->name, context, &e->where);
4641 return FAILURE;
4642 }
4643 if (!pointer && !is_pointer)
4644 {
4645 if (context)
4646 gfc_error ("Variable '%s' is PROTECTED and can not appear in a"
4647 " variable definition context (%s) at %L",
4648 sym->name, context, &e->where);
4649 return FAILURE;
4650 }
4651 }
4652
4653 /* Variable not assignable from a PURE procedure but appears in
4654 variable definition context. */
4655 if (!pointer && gfc_pure (NULL) && gfc_impure_variable (sym))
4656 {
4657 if (context)
4658 gfc_error ("Variable '%s' can not appear in a variable definition"
4659 " context (%s) at %L in PURE procedure",
4660 sym->name, context, &e->where);
4661 return FAILURE;
4662 }
4663
4664 if (!pointer && gfc_implicit_pure (NULL) && gfc_impure_variable (sym))
4665 gfc_current_ns->proc_name->attr.implicit_pure = 0;
4666
4667 /* Check variable definition context for associate-names. */
4668 if (!pointer && sym->assoc)
4669 {
4670 const char* name;
4671 gfc_association_list* assoc;
4672
4673 gcc_assert (sym->assoc->target);
4674
4675 /* If this is a SELECT TYPE temporary (the association is used internally
4676 for SELECT TYPE), silently go over to the target. */
4677 if (sym->attr.select_type_temporary)
4678 {
4679 gfc_expr* t = sym->assoc->target;
4680
4681 gcc_assert (t->expr_type == EXPR_VARIABLE);
4682 name = t->symtree->name;
4683
4684 if (t->symtree->n.sym->assoc)
4685 assoc = t->symtree->n.sym->assoc;
4686 else
4687 assoc = sym->assoc;
4688 }
4689 else
4690 {
4691 name = sym->name;
4692 assoc = sym->assoc;
4693 }
4694 gcc_assert (name && assoc);
4695
4696 /* Is association to a valid variable? */
4697 if (!assoc->variable)
4698 {
4699 if (context)
4700 {
4701 if (assoc->target->expr_type == EXPR_VARIABLE)
4702 gfc_error ("'%s' at %L associated to vector-indexed target can"
4703 " not be used in a variable definition context (%s)",
4704 name, &e->where, context);
4705 else
4706 gfc_error ("'%s' at %L associated to expression can"
4707 " not be used in a variable definition context (%s)",
4708 name, &e->where, context);
4709 }
4710 return FAILURE;
4711 }
4712
4713 /* Target must be allowed to appear in a variable definition context. */
4714 if (gfc_check_vardef_context (assoc->target, pointer, false, NULL)
4715 == FAILURE)
4716 {
4717 if (context)
4718 gfc_error ("Associate-name '%s' can not appear in a variable"
4719 " definition context (%s) at %L because its target"
4720 " at %L can not, either",
4721 name, context, &e->where,
4722 &assoc->target->where);
4723 return FAILURE;
4724 }
4725 }
4726
4727 return SUCCESS;
4728 }