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