]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | /* Routines for manipulation of expression nodes. |
85ec4feb | 2 | Copyright (C) 2000-2018 Free Software Foundation, Inc. |
6de9cd9a DN |
3 | Contributed by Andy Vaught |
4 | ||
9fc4d79b | 5 | This file is part of GCC. |
6de9cd9a | 6 | |
9fc4d79b TS |
7 | GCC is free software; you can redistribute it and/or modify it under |
8 | the terms of the GNU General Public License as published by the Free | |
d234d788 | 9 | Software Foundation; either version 3, or (at your option) any later |
9fc4d79b | 10 | version. |
6de9cd9a | 11 | |
9fc4d79b TS |
12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
6de9cd9a DN |
16 | |
17 | You should have received a copy of the GNU General Public License | |
d234d788 NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
6de9cd9a DN |
20 | |
21 | #include "config.h" | |
d22e4895 | 22 | #include "system.h" |
953bee7c | 23 | #include "coretypes.h" |
1916bcb5 | 24 | #include "options.h" |
6de9cd9a DN |
25 | #include "gfortran.h" |
26 | #include "arith.h" | |
27 | #include "match.h" | |
00a4618b | 28 | #include "target-memory.h" /* for gfc_convert_boz */ |
b7e75771 | 29 | #include "constructor.h" |
f622221a | 30 | #include "tree.h" |
6de9cd9a | 31 | |
b7e75771 JD |
32 | |
33 | /* The following set of functions provide access to gfc_expr* of | |
34 | various types - actual all but EXPR_FUNCTION and EXPR_VARIABLE. | |
35 | ||
36 | There are two functions available elsewhere that provide | |
37 | slightly different flavours of variables. Namely: | |
38 | expr.c (gfc_get_variable_expr) | |
39 | symbol.c (gfc_lval_expr_from_sym) | |
40 | TODO: Merge these functions, if possible. */ | |
41 | ||
42 | /* Get a new expression node. */ | |
6de9cd9a DN |
43 | |
44 | gfc_expr * | |
45 | gfc_get_expr (void) | |
46 | { | |
47 | gfc_expr *e; | |
48 | ||
ece3f663 | 49 | e = XCNEW (gfc_expr); |
6de9cd9a | 50 | gfc_clear_ts (&e->ts); |
6de9cd9a DN |
51 | e->shape = NULL; |
52 | e->ref = NULL; | |
53 | e->symtree = NULL; | |
6de9cd9a DN |
54 | return e; |
55 | } | |
56 | ||
57 | ||
b7e75771 JD |
58 | /* Get a new expression node that is an array constructor |
59 | of given type and kind. */ | |
6de9cd9a | 60 | |
b7e75771 JD |
61 | gfc_expr * |
62 | gfc_get_array_expr (bt type, int kind, locus *where) | |
6de9cd9a | 63 | { |
b7e75771 | 64 | gfc_expr *e; |
6de9cd9a | 65 | |
b7e75771 JD |
66 | e = gfc_get_expr (); |
67 | e->expr_type = EXPR_ARRAY; | |
68 | e->value.constructor = NULL; | |
69 | e->rank = 1; | |
70 | e->shape = NULL; | |
71 | ||
72 | e->ts.type = type; | |
73 | e->ts.kind = kind; | |
74 | if (where) | |
75 | e->where = *where; | |
76 | ||
77 | return e; | |
6de9cd9a DN |
78 | } |
79 | ||
80 | ||
b7e75771 | 81 | /* Get a new expression node that is the NULL expression. */ |
6de9cd9a | 82 | |
b7e75771 JD |
83 | gfc_expr * |
84 | gfc_get_null_expr (locus *where) | |
6de9cd9a | 85 | { |
b7e75771 | 86 | gfc_expr *e; |
6de9cd9a | 87 | |
b7e75771 JD |
88 | e = gfc_get_expr (); |
89 | e->expr_type = EXPR_NULL; | |
90 | e->ts.type = BT_UNKNOWN; | |
6de9cd9a | 91 | |
b7e75771 JD |
92 | if (where) |
93 | e->where = *where; | |
94 | ||
95 | return e; | |
96 | } | |
97 | ||
98 | ||
99 | /* Get a new expression node that is an operator expression node. */ | |
100 | ||
101 | gfc_expr * | |
102 | gfc_get_operator_expr (locus *where, gfc_intrinsic_op op, | |
103 | gfc_expr *op1, gfc_expr *op2) | |
104 | { | |
105 | gfc_expr *e; | |
106 | ||
107 | e = gfc_get_expr (); | |
108 | e->expr_type = EXPR_OP; | |
109 | e->value.op.op = op; | |
110 | e->value.op.op1 = op1; | |
111 | e->value.op.op2 = op2; | |
112 | ||
113 | if (where) | |
114 | e->where = *where; | |
115 | ||
116 | return e; | |
117 | } | |
118 | ||
119 | ||
120 | /* Get a new expression node that is an structure constructor | |
121 | of given type and kind. */ | |
122 | ||
123 | gfc_expr * | |
124 | gfc_get_structure_constructor_expr (bt type, int kind, locus *where) | |
125 | { | |
126 | gfc_expr *e; | |
127 | ||
128 | e = gfc_get_expr (); | |
129 | e->expr_type = EXPR_STRUCTURE; | |
130 | e->value.constructor = NULL; | |
131 | ||
132 | e->ts.type = type; | |
133 | e->ts.kind = kind; | |
134 | if (where) | |
135 | e->where = *where; | |
136 | ||
137 | return e; | |
138 | } | |
139 | ||
140 | ||
141 | /* Get a new expression node that is an constant of given type and kind. */ | |
142 | ||
143 | gfc_expr * | |
144 | gfc_get_constant_expr (bt type, int kind, locus *where) | |
145 | { | |
146 | gfc_expr *e; | |
147 | ||
148 | if (!where) | |
a4d9b221 TB |
149 | gfc_internal_error ("gfc_get_constant_expr(): locus %<where%> cannot be " |
150 | "NULL"); | |
b7e75771 JD |
151 | |
152 | e = gfc_get_expr (); | |
153 | ||
154 | e->expr_type = EXPR_CONSTANT; | |
155 | e->ts.type = type; | |
156 | e->ts.kind = kind; | |
157 | e->where = *where; | |
158 | ||
159 | switch (type) | |
6de9cd9a | 160 | { |
b7e75771 JD |
161 | case BT_INTEGER: |
162 | mpz_init (e->value.integer); | |
163 | break; | |
6de9cd9a | 164 | |
b7e75771 JD |
165 | case BT_REAL: |
166 | gfc_set_model_kind (kind); | |
167 | mpfr_init (e->value.real); | |
168 | break; | |
6de9cd9a | 169 | |
b7e75771 JD |
170 | case BT_COMPLEX: |
171 | gfc_set_model_kind (kind); | |
172 | mpc_init2 (e->value.complex, mpfr_get_default_prec()); | |
173 | break; | |
6de9cd9a | 174 | |
b7e75771 JD |
175 | default: |
176 | break; | |
6de9cd9a DN |
177 | } |
178 | ||
b7e75771 | 179 | return e; |
6de9cd9a DN |
180 | } |
181 | ||
182 | ||
b7e75771 JD |
183 | /* Get a new expression node that is an string constant. |
184 | If no string is passed, a string of len is allocated, | |
185 | blanked and null-terminated. */ | |
6de9cd9a | 186 | |
b7e75771 | 187 | gfc_expr * |
f622221a | 188 | gfc_get_character_expr (int kind, locus *where, const char *src, gfc_charlen_t len) |
6de9cd9a | 189 | { |
b7e75771 JD |
190 | gfc_expr *e; |
191 | gfc_char_t *dest; | |
6de9cd9a | 192 | |
b7e75771 | 193 | if (!src) |
6de9cd9a | 194 | { |
b7e75771 JD |
195 | dest = gfc_get_wide_string (len + 1); |
196 | gfc_wide_memset (dest, ' ', len); | |
197 | dest[len] = '\0'; | |
198 | } | |
199 | else | |
200 | dest = gfc_char_to_widechar (src); | |
6de9cd9a | 201 | |
b7e75771 JD |
202 | e = gfc_get_constant_expr (BT_CHARACTER, kind, |
203 | where ? where : &gfc_current_locus); | |
204 | e->value.character.string = dest; | |
205 | e->value.character.length = len; | |
206 | ||
207 | return e; | |
208 | } | |
209 | ||
210 | ||
211 | /* Get a new expression node that is an integer constant. */ | |
212 | ||
213 | gfc_expr * | |
f622221a | 214 | gfc_get_int_expr (int kind, locus *where, HOST_WIDE_INT value) |
b7e75771 JD |
215 | { |
216 | gfc_expr *p; | |
217 | p = gfc_get_constant_expr (BT_INTEGER, kind, | |
218 | where ? where : &gfc_current_locus); | |
219 | ||
f622221a JB |
220 | const wide_int w = wi::shwi (value, kind * BITS_PER_UNIT); |
221 | wi::to_mpz (w, p->value.integer, SIGNED); | |
b7e75771 JD |
222 | |
223 | return p; | |
224 | } | |
225 | ||
226 | ||
227 | /* Get a new expression node that is a logical constant. */ | |
228 | ||
229 | gfc_expr * | |
230 | gfc_get_logical_expr (int kind, locus *where, bool value) | |
231 | { | |
232 | gfc_expr *p; | |
233 | p = gfc_get_constant_expr (BT_LOGICAL, kind, | |
234 | where ? where : &gfc_current_locus); | |
235 | ||
236 | p->value.logical = value; | |
237 | ||
238 | return p; | |
239 | } | |
240 | ||
241 | ||
242 | gfc_expr * | |
243 | gfc_get_iokind_expr (locus *where, io_kind k) | |
244 | { | |
245 | gfc_expr *e; | |
246 | ||
247 | /* Set the types to something compatible with iokind. This is needed to | |
248 | get through gfc_free_expr later since iokind really has no Basic Type, | |
249 | BT, of its own. */ | |
250 | ||
251 | e = gfc_get_expr (); | |
252 | e->expr_type = EXPR_CONSTANT; | |
253 | e->ts.type = BT_LOGICAL; | |
254 | e->value.iokind = k; | |
255 | e->where = *where; | |
256 | ||
257 | return e; | |
258 | } | |
259 | ||
260 | ||
261 | /* Given an expression pointer, return a copy of the expression. This | |
262 | subroutine is recursive. */ | |
263 | ||
264 | gfc_expr * | |
265 | gfc_copy_expr (gfc_expr *p) | |
266 | { | |
267 | gfc_expr *q; | |
268 | gfc_char_t *s; | |
269 | char *c; | |
270 | ||
271 | if (p == NULL) | |
272 | return NULL; | |
273 | ||
274 | q = gfc_get_expr (); | |
275 | *q = *p; | |
276 | ||
277 | switch (q->expr_type) | |
278 | { | |
279 | case EXPR_SUBSTRING: | |
280 | s = gfc_get_wide_string (p->value.character.length + 1); | |
281 | q->value.character.string = s; | |
282 | memcpy (s, p->value.character.string, | |
283 | (p->value.character.length + 1) * sizeof (gfc_char_t)); | |
284 | break; | |
285 | ||
286 | case EXPR_CONSTANT: | |
287 | /* Copy target representation, if it exists. */ | |
288 | if (p->representation.string) | |
6de9cd9a | 289 | { |
b7e75771 JD |
290 | c = XCNEWVEC (char, p->representation.length + 1); |
291 | q->representation.string = c; | |
292 | memcpy (c, p->representation.string, (p->representation.length + 1)); | |
293 | } | |
294 | ||
295 | /* Copy the values of any pointer components of p->value. */ | |
296 | switch (q->ts.type) | |
297 | { | |
298 | case BT_INTEGER: | |
299 | mpz_init_set (q->value.integer, p->value.integer); | |
300 | break; | |
301 | ||
302 | case BT_REAL: | |
303 | gfc_set_model_kind (q->ts.kind); | |
304 | mpfr_init (q->value.real); | |
305 | mpfr_set (q->value.real, p->value.real, GFC_RND_MODE); | |
306 | break; | |
307 | ||
308 | case BT_COMPLEX: | |
309 | gfc_set_model_kind (q->ts.kind); | |
310 | mpc_init2 (q->value.complex, mpfr_get_default_prec()); | |
311 | mpc_set (q->value.complex, p->value.complex, GFC_MPC_RND_MODE); | |
312 | break; | |
313 | ||
314 | case BT_CHARACTER: | |
315 | if (p->representation.string) | |
316 | q->value.character.string | |
317 | = gfc_char_to_widechar (q->representation.string); | |
318 | else | |
6de9cd9a | 319 | { |
b7e75771 JD |
320 | s = gfc_get_wide_string (p->value.character.length + 1); |
321 | q->value.character.string = s; | |
6de9cd9a | 322 | |
b7e75771 JD |
323 | /* This is the case for the C_NULL_CHAR named constant. */ |
324 | if (p->value.character.length == 0 | |
325 | && (p->ts.is_c_interop || p->ts.is_iso_c)) | |
326 | { | |
327 | *s = '\0'; | |
328 | /* Need to set the length to 1 to make sure the NUL | |
329 | terminator is copied. */ | |
330 | q->value.character.length = 1; | |
331 | } | |
332 | else | |
333 | memcpy (s, p->value.character.string, | |
334 | (p->value.character.length + 1) * sizeof (gfc_char_t)); | |
335 | } | |
6de9cd9a DN |
336 | break; |
337 | ||
b7e75771 JD |
338 | case BT_HOLLERITH: |
339 | case BT_LOGICAL: | |
f6288c24 | 340 | case_bt_struct: |
b7e75771 | 341 | case BT_CLASS: |
45a69325 | 342 | case BT_ASSUMED: |
b7e75771 JD |
343 | break; /* Already done. */ |
344 | ||
345 | case BT_PROCEDURE: | |
346 | case BT_VOID: | |
347 | /* Should never be reached. */ | |
348 | case BT_UNKNOWN: | |
349 | gfc_internal_error ("gfc_copy_expr(): Bad expr node"); | |
350 | /* Not reached. */ | |
351 | } | |
352 | ||
353 | break; | |
354 | ||
355 | case EXPR_OP: | |
356 | switch (q->value.op.op) | |
357 | { | |
358 | case INTRINSIC_NOT: | |
359 | case INTRINSIC_PARENTHESES: | |
360 | case INTRINSIC_UPLUS: | |
361 | case INTRINSIC_UMINUS: | |
362 | q->value.op.op1 = gfc_copy_expr (p->value.op.op1); | |
6de9cd9a DN |
363 | break; |
364 | ||
b7e75771 JD |
365 | default: /* Binary operators. */ |
366 | q->value.op.op1 = gfc_copy_expr (p->value.op.op1); | |
367 | q->value.op.op2 = gfc_copy_expr (p->value.op.op2); | |
6de9cd9a DN |
368 | break; |
369 | } | |
370 | ||
b7e75771 JD |
371 | break; |
372 | ||
373 | case EXPR_FUNCTION: | |
374 | q->value.function.actual = | |
375 | gfc_copy_actual_arglist (p->value.function.actual); | |
376 | break; | |
377 | ||
378 | case EXPR_COMPCALL: | |
379 | case EXPR_PPC: | |
380 | q->value.compcall.actual = | |
381 | gfc_copy_actual_arglist (p->value.compcall.actual); | |
382 | q->value.compcall.tbp = p->value.compcall.tbp; | |
383 | break; | |
384 | ||
385 | case EXPR_STRUCTURE: | |
386 | case EXPR_ARRAY: | |
387 | q->value.constructor = gfc_constructor_copy (p->value.constructor); | |
388 | break; | |
389 | ||
390 | case EXPR_VARIABLE: | |
391 | case EXPR_NULL: | |
392 | break; | |
6de9cd9a | 393 | } |
b7e75771 JD |
394 | |
395 | q->shape = gfc_copy_shape (p->shape, p->rank); | |
396 | ||
397 | q->ref = gfc_copy_ref (p->ref); | |
398 | ||
5bab4c96 PT |
399 | if (p->param_list) |
400 | q->param_list = gfc_copy_actual_arglist (p->param_list); | |
401 | ||
b7e75771 | 402 | return q; |
6de9cd9a DN |
403 | } |
404 | ||
405 | ||
7d7212ec MM |
406 | void |
407 | gfc_clear_shape (mpz_t *shape, int rank) | |
408 | { | |
409 | int i; | |
410 | ||
411 | for (i = 0; i < rank; i++) | |
412 | mpz_clear (shape[i]); | |
413 | } | |
414 | ||
415 | ||
416 | void | |
417 | gfc_free_shape (mpz_t **shape, int rank) | |
418 | { | |
d54e80ce MM |
419 | if (*shape == NULL) |
420 | return; | |
421 | ||
7d7212ec MM |
422 | gfc_clear_shape (*shape, rank); |
423 | free (*shape); | |
424 | *shape = NULL; | |
425 | } | |
426 | ||
427 | ||
6de9cd9a DN |
428 | /* Workhorse function for gfc_free_expr() that frees everything |
429 | beneath an expression node, but not the node itself. This is | |
430 | useful when we want to simplify a node and replace it with | |
431 | something else or the expression node belongs to another structure. */ | |
432 | ||
433 | static void | |
636dff67 | 434 | free_expr0 (gfc_expr *e) |
6de9cd9a | 435 | { |
6de9cd9a DN |
436 | switch (e->expr_type) |
437 | { | |
438 | case EXPR_CONSTANT: | |
20585ad6 | 439 | /* Free any parts of the value that need freeing. */ |
6de9cd9a DN |
440 | switch (e->ts.type) |
441 | { | |
442 | case BT_INTEGER: | |
443 | mpz_clear (e->value.integer); | |
444 | break; | |
445 | ||
446 | case BT_REAL: | |
f8e566e5 | 447 | mpfr_clear (e->value.real); |
6de9cd9a DN |
448 | break; |
449 | ||
450 | case BT_CHARACTER: | |
cede9502 | 451 | free (e->value.character.string); |
6de9cd9a DN |
452 | break; |
453 | ||
454 | case BT_COMPLEX: | |
eb6f9a86 | 455 | mpc_clear (e->value.complex); |
6de9cd9a DN |
456 | break; |
457 | ||
458 | default: | |
459 | break; | |
460 | } | |
461 | ||
00660189 | 462 | /* Free the representation. */ |
04695783 | 463 | free (e->representation.string); |
20585ad6 | 464 | |
6de9cd9a DN |
465 | break; |
466 | ||
467 | case EXPR_OP: | |
58b03ab2 TS |
468 | if (e->value.op.op1 != NULL) |
469 | gfc_free_expr (e->value.op.op1); | |
470 | if (e->value.op.op2 != NULL) | |
471 | gfc_free_expr (e->value.op.op2); | |
6de9cd9a DN |
472 | break; |
473 | ||
474 | case EXPR_FUNCTION: | |
475 | gfc_free_actual_arglist (e->value.function.actual); | |
476 | break; | |
477 | ||
8e1f752a | 478 | case EXPR_COMPCALL: |
713485cc | 479 | case EXPR_PPC: |
8e1f752a DK |
480 | gfc_free_actual_arglist (e->value.compcall.actual); |
481 | break; | |
482 | ||
6de9cd9a DN |
483 | case EXPR_VARIABLE: |
484 | break; | |
485 | ||
486 | case EXPR_ARRAY: | |
487 | case EXPR_STRUCTURE: | |
b7e75771 | 488 | gfc_constructor_free (e->value.constructor); |
6de9cd9a DN |
489 | break; |
490 | ||
491 | case EXPR_SUBSTRING: | |
cede9502 | 492 | free (e->value.character.string); |
6de9cd9a DN |
493 | break; |
494 | ||
495 | case EXPR_NULL: | |
496 | break; | |
497 | ||
498 | default: | |
499 | gfc_internal_error ("free_expr0(): Bad expr type"); | |
500 | } | |
501 | ||
502 | /* Free a shape array. */ | |
d54e80ce | 503 | gfc_free_shape (&e->shape, e->rank); |
b7e75771 JD |
504 | |
505 | gfc_free_ref_list (e->ref); | |
506 | ||
5bab4c96 PT |
507 | gfc_free_actual_arglist (e->param_list); |
508 | ||
b7e75771 JD |
509 | memset (e, '\0', sizeof (gfc_expr)); |
510 | } | |
511 | ||
512 | ||
513 | /* Free an expression node and everything beneath it. */ | |
514 | ||
515 | void | |
516 | gfc_free_expr (gfc_expr *e) | |
517 | { | |
518 | if (e == NULL) | |
519 | return; | |
520 | free_expr0 (e); | |
cede9502 | 521 | free (e); |
b7e75771 JD |
522 | } |
523 | ||
524 | ||
525 | /* Free an argument list and everything below it. */ | |
526 | ||
527 | void | |
528 | gfc_free_actual_arglist (gfc_actual_arglist *a1) | |
529 | { | |
530 | gfc_actual_arglist *a2; | |
531 | ||
532 | while (a1) | |
533 | { | |
534 | a2 = a1->next; | |
5bab4c96 | 535 | if (a1->expr) |
b7e75771 | 536 | gfc_free_expr (a1->expr); |
cede9502 | 537 | free (a1); |
b7e75771 JD |
538 | a1 = a2; |
539 | } | |
540 | } | |
541 | ||
542 | ||
543 | /* Copy an arglist structure and all of the arguments. */ | |
544 | ||
545 | gfc_actual_arglist * | |
546 | gfc_copy_actual_arglist (gfc_actual_arglist *p) | |
547 | { | |
548 | gfc_actual_arglist *head, *tail, *new_arg; | |
549 | ||
550 | head = tail = NULL; | |
551 | ||
552 | for (; p; p = p->next) | |
553 | { | |
554 | new_arg = gfc_get_actual_arglist (); | |
555 | *new_arg = *p; | |
556 | ||
557 | new_arg->expr = gfc_copy_expr (p->expr); | |
558 | new_arg->next = NULL; | |
559 | ||
560 | if (head == NULL) | |
561 | head = new_arg; | |
562 | else | |
563 | tail->next = new_arg; | |
564 | ||
565 | tail = new_arg; | |
566 | } | |
567 | ||
568 | return head; | |
569 | } | |
570 | ||
571 | ||
572 | /* Free a list of reference structures. */ | |
573 | ||
574 | void | |
575 | gfc_free_ref_list (gfc_ref *p) | |
576 | { | |
577 | gfc_ref *q; | |
578 | int i; | |
579 | ||
580 | for (; p; p = q) | |
581 | { | |
582 | q = p->next; | |
6de9cd9a | 583 | |
b7e75771 JD |
584 | switch (p->type) |
585 | { | |
586 | case REF_ARRAY: | |
587 | for (i = 0; i < GFC_MAX_DIMENSIONS; i++) | |
588 | { | |
589 | gfc_free_expr (p->u.ar.start[i]); | |
590 | gfc_free_expr (p->u.ar.end[i]); | |
591 | gfc_free_expr (p->u.ar.stride[i]); | |
592 | } | |
6de9cd9a | 593 | |
b7e75771 | 594 | break; |
6de9cd9a | 595 | |
b7e75771 JD |
596 | case REF_SUBSTRING: |
597 | gfc_free_expr (p->u.ss.start); | |
598 | gfc_free_expr (p->u.ss.end); | |
599 | break; | |
6de9cd9a | 600 | |
b7e75771 | 601 | case REF_COMPONENT: |
a5fbc2f3 | 602 | case REF_INQUIRY: |
b7e75771 JD |
603 | break; |
604 | } | |
6de9cd9a | 605 | |
cede9502 | 606 | free (p); |
b7e75771 | 607 | } |
6de9cd9a DN |
608 | } |
609 | ||
610 | ||
611 | /* Graft the *src expression onto the *dest subexpression. */ | |
612 | ||
613 | void | |
636dff67 | 614 | gfc_replace_expr (gfc_expr *dest, gfc_expr *src) |
6de9cd9a | 615 | { |
6de9cd9a DN |
616 | free_expr0 (dest); |
617 | *dest = *src; | |
cede9502 | 618 | free (src); |
6de9cd9a DN |
619 | } |
620 | ||
621 | ||
622 | /* Try to extract an integer constant from the passed expression node. | |
51f03c6b JJ |
623 | Return true if some error occurred, false on success. If REPORT_ERROR |
624 | is non-zero, emit error, for positive REPORT_ERROR using gfc_error, | |
625 | for negative using gfc_error_now. */ | |
6de9cd9a | 626 | |
51f03c6b JJ |
627 | bool |
628 | gfc_extract_int (gfc_expr *expr, int *result, int report_error) | |
6de9cd9a | 629 | { |
18a4e7e3 PT |
630 | gfc_ref *ref; |
631 | ||
632 | /* A KIND component is a parameter too. The expression for it | |
633 | is stored in the initializer and should be consistent with | |
634 | the tests below. */ | |
635 | if (gfc_expr_attr(expr).pdt_kind) | |
636 | { | |
637 | for (ref = expr->ref; ref; ref = ref->next) | |
638 | { | |
639 | if (ref->u.c.component->attr.pdt_kind) | |
640 | expr = ref->u.c.component->initializer; | |
641 | } | |
642 | } | |
643 | ||
6de9cd9a | 644 | if (expr->expr_type != EXPR_CONSTANT) |
51f03c6b JJ |
645 | { |
646 | if (report_error > 0) | |
647 | gfc_error ("Constant expression required at %C"); | |
648 | else if (report_error < 0) | |
649 | gfc_error_now ("Constant expression required at %C"); | |
650 | return true; | |
651 | } | |
6de9cd9a DN |
652 | |
653 | if (expr->ts.type != BT_INTEGER) | |
51f03c6b JJ |
654 | { |
655 | if (report_error > 0) | |
656 | gfc_error ("Integer expression required at %C"); | |
657 | else if (report_error < 0) | |
658 | gfc_error_now ("Integer expression required at %C"); | |
659 | return true; | |
660 | } | |
6de9cd9a DN |
661 | |
662 | if ((mpz_cmp_si (expr->value.integer, INT_MAX) > 0) | |
663 | || (mpz_cmp_si (expr->value.integer, INT_MIN) < 0)) | |
664 | { | |
51f03c6b JJ |
665 | if (report_error > 0) |
666 | gfc_error ("Integer value too large in expression at %C"); | |
667 | else if (report_error < 0) | |
668 | gfc_error_now ("Integer value too large in expression at %C"); | |
669 | return true; | |
6de9cd9a DN |
670 | } |
671 | ||
672 | *result = (int) mpz_get_si (expr->value.integer); | |
673 | ||
51f03c6b | 674 | return false; |
6de9cd9a DN |
675 | } |
676 | ||
677 | ||
f622221a JB |
678 | /* Same as gfc_extract_int, but use a HWI. */ |
679 | ||
680 | bool | |
681 | gfc_extract_hwi (gfc_expr *expr, HOST_WIDE_INT *result, int report_error) | |
682 | { | |
683 | gfc_ref *ref; | |
684 | ||
685 | /* A KIND component is a parameter too. The expression for it is | |
686 | stored in the initializer and should be consistent with the tests | |
687 | below. */ | |
688 | if (gfc_expr_attr(expr).pdt_kind) | |
689 | { | |
690 | for (ref = expr->ref; ref; ref = ref->next) | |
691 | { | |
692 | if (ref->u.c.component->attr.pdt_kind) | |
693 | expr = ref->u.c.component->initializer; | |
694 | } | |
695 | } | |
696 | ||
697 | if (expr->expr_type != EXPR_CONSTANT) | |
698 | { | |
699 | if (report_error > 0) | |
700 | gfc_error ("Constant expression required at %C"); | |
701 | else if (report_error < 0) | |
702 | gfc_error_now ("Constant expression required at %C"); | |
703 | return true; | |
704 | } | |
705 | ||
706 | if (expr->ts.type != BT_INTEGER) | |
707 | { | |
708 | if (report_error > 0) | |
709 | gfc_error ("Integer expression required at %C"); | |
710 | else if (report_error < 0) | |
711 | gfc_error_now ("Integer expression required at %C"); | |
712 | return true; | |
713 | } | |
714 | ||
715 | /* Use long_long_integer_type_node to determine when to saturate. */ | |
716 | const wide_int val = wi::from_mpz (long_long_integer_type_node, | |
717 | expr->value.integer, false); | |
718 | ||
719 | if (!wi::fits_shwi_p (val)) | |
720 | { | |
721 | if (report_error > 0) | |
722 | gfc_error ("Integer value too large in expression at %C"); | |
723 | else if (report_error < 0) | |
724 | gfc_error_now ("Integer value too large in expression at %C"); | |
725 | return true; | |
726 | } | |
727 | ||
728 | *result = val.to_shwi (); | |
729 | ||
730 | return false; | |
731 | } | |
732 | ||
733 | ||
6de9cd9a DN |
734 | /* Recursively copy a list of reference structures. */ |
735 | ||
8e1f752a DK |
736 | gfc_ref * |
737 | gfc_copy_ref (gfc_ref *src) | |
6de9cd9a DN |
738 | { |
739 | gfc_array_ref *ar; | |
740 | gfc_ref *dest; | |
741 | ||
742 | if (src == NULL) | |
743 | return NULL; | |
744 | ||
745 | dest = gfc_get_ref (); | |
746 | dest->type = src->type; | |
747 | ||
748 | switch (src->type) | |
749 | { | |
750 | case REF_ARRAY: | |
751 | ar = gfc_copy_array_ref (&src->u.ar); | |
752 | dest->u.ar = *ar; | |
cede9502 | 753 | free (ar); |
6de9cd9a DN |
754 | break; |
755 | ||
756 | case REF_COMPONENT: | |
757 | dest->u.c = src->u.c; | |
758 | break; | |
759 | ||
a5fbc2f3 PT |
760 | case REF_INQUIRY: |
761 | dest->u.i = src->u.i; | |
762 | break; | |
763 | ||
6de9cd9a DN |
764 | case REF_SUBSTRING: |
765 | dest->u.ss = src->u.ss; | |
766 | dest->u.ss.start = gfc_copy_expr (src->u.ss.start); | |
767 | dest->u.ss.end = gfc_copy_expr (src->u.ss.end); | |
768 | break; | |
769 | } | |
770 | ||
8e1f752a | 771 | dest->next = gfc_copy_ref (src->next); |
6de9cd9a DN |
772 | |
773 | return dest; | |
774 | } | |
775 | ||
776 | ||
636dff67 | 777 | /* Detect whether an expression has any vector index array references. */ |
4075a94e PT |
778 | |
779 | int | |
780 | gfc_has_vector_index (gfc_expr *e) | |
781 | { | |
636dff67 | 782 | gfc_ref *ref; |
4075a94e PT |
783 | int i; |
784 | for (ref = e->ref; ref; ref = ref->next) | |
785 | if (ref->type == REF_ARRAY) | |
786 | for (i = 0; i < ref->u.ar.dimen; i++) | |
787 | if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR) | |
788 | return 1; | |
789 | return 0; | |
790 | } | |
791 | ||
792 | ||
6de9cd9a DN |
793 | /* Copy a shape array. */ |
794 | ||
795 | mpz_t * | |
636dff67 | 796 | gfc_copy_shape (mpz_t *shape, int rank) |
6de9cd9a DN |
797 | { |
798 | mpz_t *new_shape; | |
799 | int n; | |
800 | ||
801 | if (shape == NULL) | |
802 | return NULL; | |
803 | ||
804 | new_shape = gfc_get_shape (rank); | |
805 | ||
806 | for (n = 0; n < rank; n++) | |
807 | mpz_init_set (new_shape[n], shape[n]); | |
808 | ||
809 | return new_shape; | |
810 | } | |
811 | ||
812 | ||
94538bd1 | 813 | /* Copy a shape array excluding dimension N, where N is an integer |
eea58adb | 814 | constant expression. Dimensions are numbered in Fortran style -- |
94538bd1 VL |
815 | starting with ONE. |
816 | ||
817 | So, if the original shape array contains R elements | |
818 | { s1 ... sN-1 sN sN+1 ... sR-1 sR} | |
819 | the result contains R-1 elements: | |
820 | { s1 ... sN-1 sN+1 ... sR-1} | |
821 | ||
822 | If anything goes wrong -- N is not a constant, its value is out | |
66e4ab31 | 823 | of range -- or anything else, just returns NULL. */ |
94538bd1 VL |
824 | |
825 | mpz_t * | |
636dff67 | 826 | gfc_copy_shape_excluding (mpz_t *shape, int rank, gfc_expr *dim) |
94538bd1 VL |
827 | { |
828 | mpz_t *new_shape, *s; | |
829 | int i, n; | |
830 | ||
8b704316 | 831 | if (shape == NULL |
94538bd1 VL |
832 | || rank <= 1 |
833 | || dim == NULL | |
8b704316 | 834 | || dim->expr_type != EXPR_CONSTANT |
94538bd1 VL |
835 | || dim->ts.type != BT_INTEGER) |
836 | return NULL; | |
837 | ||
838 | n = mpz_get_si (dim->value.integer); | |
66e4ab31 | 839 | n--; /* Convert to zero based index. */ |
37e860a2 | 840 | if (n < 0 || n >= rank) |
94538bd1 VL |
841 | return NULL; |
842 | ||
636dff67 | 843 | s = new_shape = gfc_get_shape (rank - 1); |
94538bd1 VL |
844 | |
845 | for (i = 0; i < rank; i++) | |
846 | { | |
847 | if (i == n) | |
636dff67 | 848 | continue; |
94538bd1 VL |
849 | mpz_init_set (*s, shape[i]); |
850 | s++; | |
851 | } | |
852 | ||
853 | return new_shape; | |
854 | } | |
855 | ||
636dff67 | 856 | |
6de9cd9a DN |
857 | /* Return the maximum kind of two expressions. In general, higher |
858 | kind numbers mean more precision for numeric types. */ | |
859 | ||
860 | int | |
636dff67 | 861 | gfc_kind_max (gfc_expr *e1, gfc_expr *e2) |
6de9cd9a | 862 | { |
6de9cd9a DN |
863 | return (e1->ts.kind > e2->ts.kind) ? e1->ts.kind : e2->ts.kind; |
864 | } | |
865 | ||
866 | ||
867 | /* Returns nonzero if the type is numeric, zero otherwise. */ | |
868 | ||
869 | static int | |
870 | numeric_type (bt type) | |
871 | { | |
6de9cd9a DN |
872 | return type == BT_COMPLEX || type == BT_REAL || type == BT_INTEGER; |
873 | } | |
874 | ||
875 | ||
876 | /* Returns nonzero if the typespec is a numeric type, zero otherwise. */ | |
877 | ||
878 | int | |
636dff67 | 879 | gfc_numeric_ts (gfc_typespec *ts) |
6de9cd9a | 880 | { |
6de9cd9a DN |
881 | return numeric_type (ts->type); |
882 | } | |
883 | ||
884 | ||
6de9cd9a DN |
885 | /* Return an expression node with an optional argument list attached. |
886 | A variable number of gfc_expr pointers are strung together in an | |
887 | argument list with a NULL pointer terminating the list. */ | |
888 | ||
889 | gfc_expr * | |
636dff67 | 890 | gfc_build_conversion (gfc_expr *e) |
6de9cd9a DN |
891 | { |
892 | gfc_expr *p; | |
893 | ||
894 | p = gfc_get_expr (); | |
895 | p->expr_type = EXPR_FUNCTION; | |
896 | p->symtree = NULL; | |
6de9cd9a DN |
897 | p->value.function.actual = gfc_get_actual_arglist (); |
898 | p->value.function.actual->expr = e; | |
899 | ||
900 | return p; | |
901 | } | |
902 | ||
903 | ||
904 | /* Given an expression node with some sort of numeric binary | |
905 | expression, insert type conversions required to make the operands | |
dcea1b2f DF |
906 | have the same type. Conversion warnings are disabled if wconversion |
907 | is set to 0. | |
6de9cd9a DN |
908 | |
909 | The exception is that the operands of an exponential don't have to | |
910 | have the same type. If possible, the base is promoted to the type | |
911 | of the exponent. For example, 1**2.3 becomes 1.0**2.3, but | |
f7b529fa | 912 | 1.0**2 stays as it is. */ |
6de9cd9a DN |
913 | |
914 | void | |
dcea1b2f | 915 | gfc_type_convert_binary (gfc_expr *e, int wconversion) |
6de9cd9a DN |
916 | { |
917 | gfc_expr *op1, *op2; | |
918 | ||
58b03ab2 TS |
919 | op1 = e->value.op.op1; |
920 | op2 = e->value.op.op2; | |
6de9cd9a DN |
921 | |
922 | if (op1->ts.type == BT_UNKNOWN || op2->ts.type == BT_UNKNOWN) | |
923 | { | |
924 | gfc_clear_ts (&e->ts); | |
925 | return; | |
926 | } | |
927 | ||
928 | /* Kind conversions of same type. */ | |
929 | if (op1->ts.type == op2->ts.type) | |
930 | { | |
6de9cd9a DN |
931 | if (op1->ts.kind == op2->ts.kind) |
932 | { | |
636dff67 | 933 | /* No type conversions. */ |
6de9cd9a DN |
934 | e->ts = op1->ts; |
935 | goto done; | |
936 | } | |
937 | ||
938 | if (op1->ts.kind > op2->ts.kind) | |
dcea1b2f | 939 | gfc_convert_type_warn (op2, &op1->ts, 2, wconversion); |
6de9cd9a | 940 | else |
dcea1b2f | 941 | gfc_convert_type_warn (op1, &op2->ts, 2, wconversion); |
6de9cd9a DN |
942 | |
943 | e->ts = op1->ts; | |
944 | goto done; | |
945 | } | |
946 | ||
947 | /* Integer combined with real or complex. */ | |
948 | if (op2->ts.type == BT_INTEGER) | |
949 | { | |
950 | e->ts = op1->ts; | |
951 | ||
687fcae7 | 952 | /* Special case for ** operator. */ |
a1ee985f | 953 | if (e->value.op.op == INTRINSIC_POWER) |
6de9cd9a DN |
954 | goto done; |
955 | ||
dcea1b2f | 956 | gfc_convert_type_warn (e->value.op.op2, &e->ts, 2, wconversion); |
6de9cd9a DN |
957 | goto done; |
958 | } | |
959 | ||
960 | if (op1->ts.type == BT_INTEGER) | |
961 | { | |
962 | e->ts = op2->ts; | |
dcea1b2f | 963 | gfc_convert_type_warn (e->value.op.op1, &e->ts, 2, wconversion); |
6de9cd9a DN |
964 | goto done; |
965 | } | |
966 | ||
967 | /* Real combined with complex. */ | |
968 | e->ts.type = BT_COMPLEX; | |
969 | if (op1->ts.kind > op2->ts.kind) | |
970 | e->ts.kind = op1->ts.kind; | |
971 | else | |
972 | e->ts.kind = op2->ts.kind; | |
973 | if (op1->ts.type != BT_COMPLEX || op1->ts.kind != e->ts.kind) | |
dcea1b2f | 974 | gfc_convert_type_warn (e->value.op.op1, &e->ts, 2, wconversion); |
6de9cd9a | 975 | if (op2->ts.type != BT_COMPLEX || op2->ts.kind != e->ts.kind) |
dcea1b2f | 976 | gfc_convert_type_warn (e->value.op.op2, &e->ts, 2, wconversion); |
6de9cd9a DN |
977 | |
978 | done: | |
979 | return; | |
980 | } | |
981 | ||
982 | ||
068b961b | 983 | /* Determine if an expression is constant in the sense of F08:7.1.12. |
7a28353e | 984 | * This function expects that the expression has already been simplified. */ |
6de9cd9a | 985 | |
7a28353e | 986 | bool |
636dff67 | 987 | gfc_is_constant_expr (gfc_expr *e) |
6de9cd9a DN |
988 | { |
989 | gfc_constructor *c; | |
990 | gfc_actual_arglist *arg; | |
6de9cd9a DN |
991 | |
992 | if (e == NULL) | |
7a28353e | 993 | return true; |
6de9cd9a DN |
994 | |
995 | switch (e->expr_type) | |
996 | { | |
997 | case EXPR_OP: | |
b7e75771 JD |
998 | return (gfc_is_constant_expr (e->value.op.op1) |
999 | && (e->value.op.op2 == NULL | |
1000 | || gfc_is_constant_expr (e->value.op.op2))); | |
6de9cd9a DN |
1001 | |
1002 | case EXPR_VARIABLE: | |
5bab4c96 PT |
1003 | /* The only context in which this can occur is in a parameterized |
1004 | derived type declaration, so returning true is OK. */ | |
1005 | if (e->symtree->n.sym->attr.pdt_len | |
1006 | || e->symtree->n.sym->attr.pdt_kind) | |
1007 | return true; | |
7a28353e | 1008 | return false; |
6de9cd9a DN |
1009 | |
1010 | case EXPR_FUNCTION: | |
687ea68f TB |
1011 | case EXPR_PPC: |
1012 | case EXPR_COMPCALL: | |
0126595f TB |
1013 | gcc_assert (e->symtree || e->value.function.esym |
1014 | || e->value.function.isym); | |
1015 | ||
6de9cd9a | 1016 | /* Call to intrinsic with at least one argument. */ |
6de9cd9a DN |
1017 | if (e->value.function.isym && e->value.function.actual) |
1018 | { | |
1019 | for (arg = e->value.function.actual; arg; arg = arg->next) | |
b7e75771 | 1020 | if (!gfc_is_constant_expr (arg->expr)) |
7a28353e | 1021 | return false; |
6de9cd9a | 1022 | } |
83f3bd62 | 1023 | |
83f3bd62 JD |
1024 | if (e->value.function.isym |
1025 | && (e->value.function.isym->elemental | |
1026 | || e->value.function.isym->pure | |
1027 | || e->value.function.isym->inquiry | |
1028 | || e->value.function.isym->transformational)) | |
7a28353e | 1029 | return true; |
83f3bd62 | 1030 | |
7a28353e | 1031 | return false; |
6de9cd9a DN |
1032 | |
1033 | case EXPR_CONSTANT: | |
1034 | case EXPR_NULL: | |
7a28353e | 1035 | return true; |
6de9cd9a DN |
1036 | |
1037 | case EXPR_SUBSTRING: | |
b7e75771 JD |
1038 | return e->ref == NULL || (gfc_is_constant_expr (e->ref->u.ss.start) |
1039 | && gfc_is_constant_expr (e->ref->u.ss.end)); | |
6de9cd9a | 1040 | |
8e1e41e7 | 1041 | case EXPR_ARRAY: |
6de9cd9a | 1042 | case EXPR_STRUCTURE: |
8e1e41e7 PT |
1043 | c = gfc_constructor_first (e->value.constructor); |
1044 | if ((e->expr_type == EXPR_ARRAY) && c && c->iterator) | |
1045 | return gfc_constant_ac (e); | |
1046 | ||
1047 | for (; c; c = gfc_constructor_next (c)) | |
6de9cd9a | 1048 | if (!gfc_is_constant_expr (c->expr)) |
7a28353e | 1049 | return false; |
6de9cd9a | 1050 | |
7a28353e | 1051 | return true; |
6de9cd9a | 1052 | |
6de9cd9a DN |
1053 | |
1054 | default: | |
1055 | gfc_internal_error ("gfc_is_constant_expr(): Unknown expression type"); | |
7a28353e | 1056 | return false; |
6de9cd9a | 1057 | } |
6de9cd9a DN |
1058 | } |
1059 | ||
1060 | ||
1d6b7f39 PT |
1061 | /* Is true if an array reference is followed by a component or substring |
1062 | reference. */ | |
1063 | bool | |
1064 | is_subref_array (gfc_expr * e) | |
1065 | { | |
1066 | gfc_ref * ref; | |
1067 | bool seen_array; | |
1068 | ||
1069 | if (e->expr_type != EXPR_VARIABLE) | |
1070 | return false; | |
1071 | ||
1072 | if (e->symtree->n.sym->attr.subref_array_pointer) | |
1073 | return true; | |
1074 | ||
ff3598bc PT |
1075 | if (e->symtree->n.sym->ts.type == BT_CLASS |
1076 | && e->symtree->n.sym->attr.dummy | |
3b949026 | 1077 | && CLASS_DATA (e->symtree->n.sym)->attr.dimension |
ff3598bc PT |
1078 | && CLASS_DATA (e->symtree->n.sym)->attr.class_pointer) |
1079 | return true; | |
1080 | ||
1d6b7f39 PT |
1081 | seen_array = false; |
1082 | for (ref = e->ref; ref; ref = ref->next) | |
1083 | { | |
1084 | if (ref->type == REF_ARRAY | |
1085 | && ref->u.ar.type != AR_ELEMENT) | |
1086 | seen_array = true; | |
1087 | ||
1088 | if (seen_array | |
1089 | && ref->type != REF_ARRAY) | |
1090 | return seen_array; | |
1091 | } | |
1092 | return false; | |
1093 | } | |
1094 | ||
1095 | ||
6de9cd9a DN |
1096 | /* Try to collapse intrinsic expressions. */ |
1097 | ||
524af0d6 | 1098 | static bool |
636dff67 | 1099 | simplify_intrinsic_op (gfc_expr *p, int type) |
6de9cd9a | 1100 | { |
3bed9dd0 | 1101 | gfc_intrinsic_op op; |
6de9cd9a DN |
1102 | gfc_expr *op1, *op2, *result; |
1103 | ||
a1ee985f | 1104 | if (p->value.op.op == INTRINSIC_USER) |
524af0d6 | 1105 | return true; |
6de9cd9a | 1106 | |
58b03ab2 TS |
1107 | op1 = p->value.op.op1; |
1108 | op2 = p->value.op.op2; | |
a1ee985f | 1109 | op = p->value.op.op; |
6de9cd9a | 1110 | |
524af0d6 JB |
1111 | if (!gfc_simplify_expr (op1, type)) |
1112 | return false; | |
1113 | if (!gfc_simplify_expr (op2, type)) | |
1114 | return false; | |
6de9cd9a DN |
1115 | |
1116 | if (!gfc_is_constant_expr (op1) | |
1117 | || (op2 != NULL && !gfc_is_constant_expr (op2))) | |
524af0d6 | 1118 | return true; |
6de9cd9a | 1119 | |
66e4ab31 | 1120 | /* Rip p apart. */ |
58b03ab2 TS |
1121 | p->value.op.op1 = NULL; |
1122 | p->value.op.op2 = NULL; | |
6de9cd9a | 1123 | |
3bed9dd0 | 1124 | switch (op) |
6de9cd9a | 1125 | { |
2414e1d6 | 1126 | case INTRINSIC_PARENTHESES: |
2f118814 TS |
1127 | result = gfc_parentheses (op1); |
1128 | break; | |
1129 | ||
1130 | case INTRINSIC_UPLUS: | |
6de9cd9a DN |
1131 | result = gfc_uplus (op1); |
1132 | break; | |
1133 | ||
1134 | case INTRINSIC_UMINUS: | |
1135 | result = gfc_uminus (op1); | |
1136 | break; | |
1137 | ||
1138 | case INTRINSIC_PLUS: | |
1139 | result = gfc_add (op1, op2); | |
1140 | break; | |
1141 | ||
1142 | case INTRINSIC_MINUS: | |
1143 | result = gfc_subtract (op1, op2); | |
1144 | break; | |
1145 | ||
1146 | case INTRINSIC_TIMES: | |
1147 | result = gfc_multiply (op1, op2); | |
1148 | break; | |
1149 | ||
1150 | case INTRINSIC_DIVIDE: | |
1151 | result = gfc_divide (op1, op2); | |
1152 | break; | |
1153 | ||
1154 | case INTRINSIC_POWER: | |
1155 | result = gfc_power (op1, op2); | |
1156 | break; | |
1157 | ||
1158 | case INTRINSIC_CONCAT: | |
1159 | result = gfc_concat (op1, op2); | |
1160 | break; | |
1161 | ||
1162 | case INTRINSIC_EQ: | |
3bed9dd0 DF |
1163 | case INTRINSIC_EQ_OS: |
1164 | result = gfc_eq (op1, op2, op); | |
6de9cd9a DN |
1165 | break; |
1166 | ||
1167 | case INTRINSIC_NE: | |
3bed9dd0 DF |
1168 | case INTRINSIC_NE_OS: |
1169 | result = gfc_ne (op1, op2, op); | |
6de9cd9a DN |
1170 | break; |
1171 | ||
1172 | case INTRINSIC_GT: | |
3bed9dd0 DF |
1173 | case INTRINSIC_GT_OS: |
1174 | result = gfc_gt (op1, op2, op); | |
6de9cd9a DN |
1175 | break; |
1176 | ||
1177 | case INTRINSIC_GE: | |
3bed9dd0 DF |
1178 | case INTRINSIC_GE_OS: |
1179 | result = gfc_ge (op1, op2, op); | |
6de9cd9a DN |
1180 | break; |
1181 | ||
1182 | case INTRINSIC_LT: | |
3bed9dd0 DF |
1183 | case INTRINSIC_LT_OS: |
1184 | result = gfc_lt (op1, op2, op); | |
6de9cd9a DN |
1185 | break; |
1186 | ||
1187 | case INTRINSIC_LE: | |
3bed9dd0 DF |
1188 | case INTRINSIC_LE_OS: |
1189 | result = gfc_le (op1, op2, op); | |
6de9cd9a DN |
1190 | break; |
1191 | ||
1192 | case INTRINSIC_NOT: | |
1193 | result = gfc_not (op1); | |
1194 | break; | |
1195 | ||
1196 | case INTRINSIC_AND: | |
1197 | result = gfc_and (op1, op2); | |
1198 | break; | |
1199 | ||
1200 | case INTRINSIC_OR: | |
1201 | result = gfc_or (op1, op2); | |
1202 | break; | |
1203 | ||
1204 | case INTRINSIC_EQV: | |
1205 | result = gfc_eqv (op1, op2); | |
1206 | break; | |
1207 | ||
1208 | case INTRINSIC_NEQV: | |
1209 | result = gfc_neqv (op1, op2); | |
1210 | break; | |
1211 | ||
1212 | default: | |
1213 | gfc_internal_error ("simplify_intrinsic_op(): Bad operator"); | |
1214 | } | |
1215 | ||
1216 | if (result == NULL) | |
1217 | { | |
1218 | gfc_free_expr (op1); | |
1219 | gfc_free_expr (op2); | |
524af0d6 | 1220 | return false; |
6de9cd9a DN |
1221 | } |
1222 | ||
0e9a445b PT |
1223 | result->rank = p->rank; |
1224 | result->where = p->where; | |
6de9cd9a DN |
1225 | gfc_replace_expr (p, result); |
1226 | ||
524af0d6 | 1227 | return true; |
6de9cd9a DN |
1228 | } |
1229 | ||
1230 | ||
1231 | /* Subroutine to simplify constructor expressions. Mutually recursive | |
1232 | with gfc_simplify_expr(). */ | |
1233 | ||
524af0d6 | 1234 | static bool |
b7e75771 | 1235 | simplify_constructor (gfc_constructor_base base, int type) |
6de9cd9a | 1236 | { |
b7e75771 | 1237 | gfc_constructor *c; |
28d08315 PT |
1238 | gfc_expr *p; |
1239 | ||
b7e75771 | 1240 | for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c)) |
6de9cd9a DN |
1241 | { |
1242 | if (c->iterator | |
524af0d6 JB |
1243 | && (!gfc_simplify_expr(c->iterator->start, type) |
1244 | || !gfc_simplify_expr (c->iterator->end, type) | |
1245 | || !gfc_simplify_expr (c->iterator->step, type))) | |
1246 | return false; | |
6de9cd9a | 1247 | |
28d08315 PT |
1248 | if (c->expr) |
1249 | { | |
1250 | /* Try and simplify a copy. Replace the original if successful | |
1251 | but keep going through the constructor at all costs. Not | |
1252 | doing so can make a dog's dinner of complicated things. */ | |
1253 | p = gfc_copy_expr (c->expr); | |
1254 | ||
524af0d6 | 1255 | if (!gfc_simplify_expr (p, type)) |
28d08315 PT |
1256 | { |
1257 | gfc_free_expr (p); | |
1258 | continue; | |
1259 | } | |
1260 | ||
1261 | gfc_replace_expr (c->expr, p); | |
1262 | } | |
6de9cd9a DN |
1263 | } |
1264 | ||
524af0d6 | 1265 | return true; |
6de9cd9a DN |
1266 | } |
1267 | ||
1268 | ||
1269 | /* Pull a single array element out of an array constructor. */ | |
1270 | ||
524af0d6 | 1271 | static bool |
b7e75771 | 1272 | find_array_element (gfc_constructor_base base, gfc_array_ref *ar, |
636dff67 | 1273 | gfc_constructor **rval) |
6de9cd9a DN |
1274 | { |
1275 | unsigned long nelemen; | |
1276 | int i; | |
1277 | mpz_t delta; | |
1278 | mpz_t offset; | |
4c6b3ec7 PT |
1279 | mpz_t span; |
1280 | mpz_t tmp; | |
b7e75771 | 1281 | gfc_constructor *cons; |
a4a11197 | 1282 | gfc_expr *e; |
524af0d6 | 1283 | bool t; |
a4a11197 | 1284 | |
524af0d6 | 1285 | t = true; |
a4a11197 | 1286 | e = NULL; |
6de9cd9a DN |
1287 | |
1288 | mpz_init_set_ui (offset, 0); | |
1289 | mpz_init (delta); | |
4c6b3ec7 PT |
1290 | mpz_init (tmp); |
1291 | mpz_init_set_ui (span, 1); | |
6de9cd9a DN |
1292 | for (i = 0; i < ar->dimen; i++) |
1293 | { | |
524af0d6 JB |
1294 | if (!gfc_reduce_init_expr (ar->as->lower[i]) |
1295 | || !gfc_reduce_init_expr (ar->as->upper[i])) | |
138b3340 | 1296 | { |
524af0d6 | 1297 | t = false; |
138b3340 MM |
1298 | cons = NULL; |
1299 | goto depart; | |
1300 | } | |
1301 | ||
36abe895 | 1302 | e = ar->start[i]; |
a4a11197 | 1303 | if (e->expr_type != EXPR_CONSTANT) |
6de9cd9a DN |
1304 | { |
1305 | cons = NULL; | |
a4a11197 | 1306 | goto depart; |
6de9cd9a | 1307 | } |
5bcb0cc3 | 1308 | |
138b3340 MM |
1309 | gcc_assert (ar->as->upper[i]->expr_type == EXPR_CONSTANT |
1310 | && ar->as->lower[i]->expr_type == EXPR_CONSTANT); | |
1311 | ||
5bcb0cc3 | 1312 | /* Check the bounds. */ |
0c6ce8b0 | 1313 | if ((ar->as->upper[i] |
3b35a6f8 L |
1314 | && mpz_cmp (e->value.integer, |
1315 | ar->as->upper[i]->value.integer) > 0) | |
138b3340 MM |
1316 | || (mpz_cmp (e->value.integer, |
1317 | ar->as->lower[i]->value.integer) < 0)) | |
a4a11197 | 1318 | { |
0c6ce8b0 | 1319 | gfc_error ("Index in dimension %d is out of bounds " |
a4a11197 PT |
1320 | "at %L", i + 1, &ar->c_where[i]); |
1321 | cons = NULL; | |
524af0d6 | 1322 | t = false; |
a4a11197 PT |
1323 | goto depart; |
1324 | } | |
1325 | ||
636dff67 | 1326 | mpz_sub (delta, e->value.integer, ar->as->lower[i]->value.integer); |
4c6b3ec7 | 1327 | mpz_mul (delta, delta, span); |
6de9cd9a | 1328 | mpz_add (offset, offset, delta); |
4c6b3ec7 PT |
1329 | |
1330 | mpz_set_ui (tmp, 1); | |
1331 | mpz_add (tmp, tmp, ar->as->upper[i]->value.integer); | |
1332 | mpz_sub (tmp, tmp, ar->as->lower[i]->value.integer); | |
1333 | mpz_mul (span, span, tmp); | |
6de9cd9a DN |
1334 | } |
1335 | ||
b7e75771 JD |
1336 | for (cons = gfc_constructor_first (base), nelemen = mpz_get_ui (offset); |
1337 | cons && nelemen > 0; cons = gfc_constructor_next (cons), nelemen--) | |
3b35a6f8 | 1338 | { |
b7e75771 | 1339 | if (cons->iterator) |
3b35a6f8 | 1340 | { |
b7e75771 JD |
1341 | cons = NULL; |
1342 | goto depart; | |
3b35a6f8 L |
1343 | } |
1344 | } | |
6de9cd9a | 1345 | |
a4a11197 | 1346 | depart: |
6de9cd9a DN |
1347 | mpz_clear (delta); |
1348 | mpz_clear (offset); | |
4c6b3ec7 PT |
1349 | mpz_clear (span); |
1350 | mpz_clear (tmp); | |
a4a11197 PT |
1351 | *rval = cons; |
1352 | return t; | |
6de9cd9a DN |
1353 | } |
1354 | ||
1355 | ||
1356 | /* Find a component of a structure constructor. */ | |
1357 | ||
1358 | static gfc_constructor * | |
b7e75771 | 1359 | find_component_ref (gfc_constructor_base base, gfc_ref *ref) |
6de9cd9a | 1360 | { |
74a1c62f | 1361 | gfc_component *pick = ref->u.c.component; |
b7e75771 | 1362 | gfc_constructor *c = gfc_constructor_first (base); |
6de9cd9a | 1363 | |
74a1c62f JW |
1364 | gfc_symbol *dt = ref->u.c.sym; |
1365 | int ext = dt->attr.extension; | |
1366 | ||
1367 | /* For extended types, check if the desired component is in one of the | |
1368 | * parent types. */ | |
1369 | while (ext > 0 && gfc_find_component (dt->components->ts.u.derived, | |
f6288c24 | 1370 | pick->name, true, true, NULL)) |
74a1c62f JW |
1371 | { |
1372 | dt = dt->components->ts.u.derived; | |
1373 | c = gfc_constructor_first (c->expr->value.constructor); | |
1374 | ext--; | |
1375 | } | |
1376 | ||
1377 | gfc_component *comp = dt->components; | |
6de9cd9a DN |
1378 | while (comp != pick) |
1379 | { | |
1380 | comp = comp->next; | |
b7e75771 | 1381 | c = gfc_constructor_next (c); |
6de9cd9a DN |
1382 | } |
1383 | ||
b7e75771 | 1384 | return c; |
6de9cd9a DN |
1385 | } |
1386 | ||
1387 | ||
1388 | /* Replace an expression with the contents of a constructor, removing | |
1389 | the subobject reference in the process. */ | |
1390 | ||
1391 | static void | |
636dff67 | 1392 | remove_subobject_ref (gfc_expr *p, gfc_constructor *cons) |
6de9cd9a DN |
1393 | { |
1394 | gfc_expr *e; | |
1395 | ||
ff015c5b PT |
1396 | if (cons) |
1397 | { | |
1398 | e = cons->expr; | |
1399 | cons->expr = NULL; | |
1400 | } | |
1401 | else | |
1402 | e = gfc_copy_expr (p); | |
6de9cd9a DN |
1403 | e->ref = p->ref->next; |
1404 | p->ref->next = NULL; | |
1405 | gfc_replace_expr (p, e); | |
1406 | } | |
1407 | ||
1408 | ||
a4a11197 PT |
1409 | /* Pull an array section out of an array constructor. */ |
1410 | ||
524af0d6 | 1411 | static bool |
a4a11197 PT |
1412 | find_array_section (gfc_expr *expr, gfc_ref *ref) |
1413 | { | |
1414 | int idx; | |
1415 | int rank; | |
1416 | int d; | |
abe601c7 | 1417 | int shape_i; |
b1ccc24e | 1418 | int limit; |
a4a11197 | 1419 | long unsigned one = 1; |
abe601c7 | 1420 | bool incr_ctr; |
3e978d30 | 1421 | mpz_t start[GFC_MAX_DIMENSIONS]; |
a4a11197 PT |
1422 | mpz_t end[GFC_MAX_DIMENSIONS]; |
1423 | mpz_t stride[GFC_MAX_DIMENSIONS]; | |
1424 | mpz_t delta[GFC_MAX_DIMENSIONS]; | |
1425 | mpz_t ctr[GFC_MAX_DIMENSIONS]; | |
1426 | mpz_t delta_mpz; | |
1427 | mpz_t tmp_mpz; | |
1428 | mpz_t nelts; | |
1429 | mpz_t ptr; | |
b7e75771 JD |
1430 | gfc_constructor_base base; |
1431 | gfc_constructor *cons, *vecsub[GFC_MAX_DIMENSIONS]; | |
a4a11197 PT |
1432 | gfc_expr *begin; |
1433 | gfc_expr *finish; | |
1434 | gfc_expr *step; | |
1435 | gfc_expr *upper; | |
1436 | gfc_expr *lower; | |
524af0d6 | 1437 | bool t; |
a4a11197 | 1438 | |
524af0d6 | 1439 | t = true; |
a4a11197 PT |
1440 | |
1441 | base = expr->value.constructor; | |
1442 | expr->value.constructor = NULL; | |
1443 | ||
1444 | rank = ref->u.ar.as->rank; | |
1445 | ||
1446 | if (expr->shape == NULL) | |
1447 | expr->shape = gfc_get_shape (rank); | |
1448 | ||
1449 | mpz_init_set_ui (delta_mpz, one); | |
1450 | mpz_init_set_ui (nelts, one); | |
1451 | mpz_init (tmp_mpz); | |
1452 | ||
1453 | /* Do the initialization now, so that we can cleanup without | |
1454 | keeping track of where we were. */ | |
1455 | for (d = 0; d < rank; d++) | |
1456 | { | |
1457 | mpz_init (delta[d]); | |
3e978d30 | 1458 | mpz_init (start[d]); |
a4a11197 PT |
1459 | mpz_init (end[d]); |
1460 | mpz_init (ctr[d]); | |
1461 | mpz_init (stride[d]); | |
abe601c7 | 1462 | vecsub[d] = NULL; |
a4a11197 PT |
1463 | } |
1464 | ||
1465 | /* Build the counters to clock through the array reference. */ | |
abe601c7 | 1466 | shape_i = 0; |
a4a11197 PT |
1467 | for (d = 0; d < rank; d++) |
1468 | { | |
1469 | /* Make this stretch of code easier on the eye! */ | |
1470 | begin = ref->u.ar.start[d]; | |
1471 | finish = ref->u.ar.end[d]; | |
1472 | step = ref->u.ar.stride[d]; | |
1473 | lower = ref->u.ar.as->lower[d]; | |
1474 | upper = ref->u.ar.as->upper[d]; | |
1475 | ||
abe601c7 | 1476 | if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR) /* Vector subscript. */ |
636dff67 | 1477 | { |
b7e75771 | 1478 | gfc_constructor *ci; |
636dff67 | 1479 | gcc_assert (begin); |
945a98a4 | 1480 | |
28ec36ea | 1481 | if (begin->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (begin)) |
945a98a4 | 1482 | { |
524af0d6 | 1483 | t = false; |
945a98a4 TB |
1484 | goto cleanup; |
1485 | } | |
1486 | ||
636dff67 | 1487 | gcc_assert (begin->rank == 1); |
045ac367 | 1488 | /* Zero-sized arrays have no shape and no elements, stop early. */ |
8b704316 | 1489 | if (!begin->shape) |
045ac367 DF |
1490 | { |
1491 | mpz_init_set_ui (nelts, 0); | |
1492 | break; | |
1493 | } | |
a4a11197 | 1494 | |
b7e75771 | 1495 | vecsub[d] = gfc_constructor_first (begin->value.constructor); |
abe601c7 EE |
1496 | mpz_set (ctr[d], vecsub[d]->expr->value.integer); |
1497 | mpz_mul (nelts, nelts, begin->shape[0]); | |
1498 | mpz_set (expr->shape[shape_i++], begin->shape[0]); | |
a4a11197 | 1499 | |
abe601c7 | 1500 | /* Check bounds. */ |
b7e75771 | 1501 | for (ci = vecsub[d]; ci; ci = gfc_constructor_next (ci)) |
abe601c7 | 1502 | { |
b7e75771 JD |
1503 | if (mpz_cmp (ci->expr->value.integer, upper->value.integer) > 0 |
1504 | || mpz_cmp (ci->expr->value.integer, | |
636dff67 | 1505 | lower->value.integer) < 0) |
abe601c7 EE |
1506 | { |
1507 | gfc_error ("index in dimension %d is out of bounds " | |
1508 | "at %L", d + 1, &ref->u.ar.c_where[d]); | |
524af0d6 | 1509 | t = false; |
abe601c7 EE |
1510 | goto cleanup; |
1511 | } | |
1512 | } | |
636dff67 | 1513 | } |
a4a11197 | 1514 | else |
636dff67 | 1515 | { |
abe601c7 | 1516 | if ((begin && begin->expr_type != EXPR_CONSTANT) |
636dff67 SK |
1517 | || (finish && finish->expr_type != EXPR_CONSTANT) |
1518 | || (step && step->expr_type != EXPR_CONSTANT)) | |
abe601c7 | 1519 | { |
524af0d6 | 1520 | t = false; |
abe601c7 EE |
1521 | goto cleanup; |
1522 | } | |
c71d6a56 | 1523 | |
abe601c7 EE |
1524 | /* Obtain the stride. */ |
1525 | if (step) | |
1526 | mpz_set (stride[d], step->value.integer); | |
1527 | else | |
1528 | mpz_set_ui (stride[d], one); | |
a4a11197 | 1529 | |
abe601c7 EE |
1530 | if (mpz_cmp_ui (stride[d], 0) == 0) |
1531 | mpz_set_ui (stride[d], one); | |
a4a11197 | 1532 | |
abe601c7 EE |
1533 | /* Obtain the start value for the index. */ |
1534 | if (begin) | |
1535 | mpz_set (start[d], begin->value.integer); | |
1536 | else | |
1537 | mpz_set (start[d], lower->value.integer); | |
a4a11197 | 1538 | |
abe601c7 | 1539 | mpz_set (ctr[d], start[d]); |
a4a11197 | 1540 | |
abe601c7 EE |
1541 | /* Obtain the end value for the index. */ |
1542 | if (finish) | |
1543 | mpz_set (end[d], finish->value.integer); | |
1544 | else | |
1545 | mpz_set (end[d], upper->value.integer); | |
1546 | ||
1547 | /* Separate 'if' because elements sometimes arrive with | |
1548 | non-null end. */ | |
1549 | if (ref->u.ar.dimen_type[d] == DIMEN_ELEMENT) | |
1550 | mpz_set (end [d], begin->value.integer); | |
1551 | ||
1552 | /* Check the bounds. */ | |
1553 | if (mpz_cmp (ctr[d], upper->value.integer) > 0 | |
1554 | || mpz_cmp (end[d], upper->value.integer) > 0 | |
1555 | || mpz_cmp (ctr[d], lower->value.integer) < 0 | |
1556 | || mpz_cmp (end[d], lower->value.integer) < 0) | |
1557 | { | |
1558 | gfc_error ("index in dimension %d is out of bounds " | |
1559 | "at %L", d + 1, &ref->u.ar.c_where[d]); | |
524af0d6 | 1560 | t = false; |
abe601c7 EE |
1561 | goto cleanup; |
1562 | } | |
a4a11197 | 1563 | |
abe601c7 | 1564 | /* Calculate the number of elements and the shape. */ |
e1e24dc1 | 1565 | mpz_set (tmp_mpz, stride[d]); |
abe601c7 EE |
1566 | mpz_add (tmp_mpz, end[d], tmp_mpz); |
1567 | mpz_sub (tmp_mpz, tmp_mpz, ctr[d]); | |
1568 | mpz_div (tmp_mpz, tmp_mpz, stride[d]); | |
1569 | mpz_mul (nelts, nelts, tmp_mpz); | |
1570 | ||
636dff67 SK |
1571 | /* An element reference reduces the rank of the expression; don't |
1572 | add anything to the shape array. */ | |
8b704316 | 1573 | if (ref->u.ar.dimen_type[d] != DIMEN_ELEMENT) |
abe601c7 EE |
1574 | mpz_set (expr->shape[shape_i++], tmp_mpz); |
1575 | } | |
a4a11197 PT |
1576 | |
1577 | /* Calculate the 'stride' (=delta) for conversion of the | |
1578 | counter values into the index along the constructor. */ | |
1579 | mpz_set (delta[d], delta_mpz); | |
1580 | mpz_sub (tmp_mpz, upper->value.integer, lower->value.integer); | |
1581 | mpz_add_ui (tmp_mpz, tmp_mpz, one); | |
1582 | mpz_mul (delta_mpz, delta_mpz, tmp_mpz); | |
1583 | } | |
1584 | ||
a4a11197 | 1585 | mpz_init (ptr); |
b7e75771 | 1586 | cons = gfc_constructor_first (base); |
a4a11197 PT |
1587 | |
1588 | /* Now clock through the array reference, calculating the index in | |
1589 | the source constructor and transferring the elements to the new | |
328ece7d | 1590 | constructor. */ |
636dff67 | 1591 | for (idx = 0; idx < (int) mpz_get_si (nelts); idx++) |
a4a11197 | 1592 | { |
328ece7d | 1593 | mpz_init_set_ui (ptr, 0); |
a4a11197 | 1594 | |
abe601c7 | 1595 | incr_ctr = true; |
a4a11197 PT |
1596 | for (d = 0; d < rank; d++) |
1597 | { | |
1598 | mpz_set (tmp_mpz, ctr[d]); | |
636dff67 | 1599 | mpz_sub (tmp_mpz, tmp_mpz, ref->u.ar.as->lower[d]->value.integer); |
a4a11197 PT |
1600 | mpz_mul (tmp_mpz, tmp_mpz, delta[d]); |
1601 | mpz_add (ptr, ptr, tmp_mpz); | |
1602 | ||
abe601c7 | 1603 | if (!incr_ctr) continue; |
a4a11197 | 1604 | |
636dff67 | 1605 | if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR) /* Vector subscript. */ |
abe601c7 EE |
1606 | { |
1607 | gcc_assert(vecsub[d]); | |
1608 | ||
b7e75771 JD |
1609 | if (!gfc_constructor_next (vecsub[d])) |
1610 | vecsub[d] = gfc_constructor_first (ref->u.ar.start[d]->value.constructor); | |
abe601c7 EE |
1611 | else |
1612 | { | |
b7e75771 | 1613 | vecsub[d] = gfc_constructor_next (vecsub[d]); |
abe601c7 EE |
1614 | incr_ctr = false; |
1615 | } | |
1616 | mpz_set (ctr[d], vecsub[d]->expr->value.integer); | |
1617 | } | |
a4a11197 | 1618 | else |
abe601c7 | 1619 | { |
8b704316 | 1620 | mpz_add (ctr[d], ctr[d], stride[d]); |
abe601c7 | 1621 | |
636dff67 SK |
1622 | if (mpz_cmp_ui (stride[d], 0) > 0 |
1623 | ? mpz_cmp (ctr[d], end[d]) > 0 | |
1624 | : mpz_cmp (ctr[d], end[d]) < 0) | |
abe601c7 EE |
1625 | mpz_set (ctr[d], start[d]); |
1626 | else | |
1627 | incr_ctr = false; | |
1628 | } | |
a4a11197 PT |
1629 | } |
1630 | ||
b1ccc24e | 1631 | limit = mpz_get_ui (ptr); |
c61819ff | 1632 | if (limit >= flag_max_array_constructor) |
b1ccc24e JD |
1633 | { |
1634 | gfc_error ("The number of elements in the array constructor " | |
1635 | "at %L requires an increase of the allowed %d " | |
1636 | "upper limit. See -fmax-array-constructor " | |
c61819ff | 1637 | "option", &expr->where, flag_max_array_constructor); |
524af0d6 | 1638 | return false; |
b1ccc24e JD |
1639 | } |
1640 | ||
1641 | cons = gfc_constructor_lookup (base, limit); | |
b7e75771 JD |
1642 | gcc_assert (cons); |
1643 | gfc_constructor_append_expr (&expr->value.constructor, | |
1644 | gfc_copy_expr (cons->expr), NULL); | |
a4a11197 PT |
1645 | } |
1646 | ||
1647 | mpz_clear (ptr); | |
a4a11197 PT |
1648 | |
1649 | cleanup: | |
1650 | ||
1651 | mpz_clear (delta_mpz); | |
1652 | mpz_clear (tmp_mpz); | |
1653 | mpz_clear (nelts); | |
1654 | for (d = 0; d < rank; d++) | |
1655 | { | |
1656 | mpz_clear (delta[d]); | |
3e978d30 | 1657 | mpz_clear (start[d]); |
a4a11197 PT |
1658 | mpz_clear (end[d]); |
1659 | mpz_clear (ctr[d]); | |
1660 | mpz_clear (stride[d]); | |
1661 | } | |
b7e75771 | 1662 | gfc_constructor_free (base); |
a4a11197 PT |
1663 | return t; |
1664 | } | |
1665 | ||
1666 | /* Pull a substring out of an expression. */ | |
1667 | ||
524af0d6 | 1668 | static bool |
a4a11197 PT |
1669 | find_substring_ref (gfc_expr *p, gfc_expr **newp) |
1670 | { | |
335d0338 TK |
1671 | gfc_charlen_t end; |
1672 | gfc_charlen_t start; | |
1673 | gfc_charlen_t length; | |
00660189 | 1674 | gfc_char_t *chr; |
a4a11197 PT |
1675 | |
1676 | if (p->ref->u.ss.start->expr_type != EXPR_CONSTANT | |
636dff67 | 1677 | || p->ref->u.ss.end->expr_type != EXPR_CONSTANT) |
524af0d6 | 1678 | return false; |
a4a11197 PT |
1679 | |
1680 | *newp = gfc_copy_expr (p); | |
cede9502 | 1681 | free ((*newp)->value.character.string); |
b35c5f01 | 1682 | |
335d0338 TK |
1683 | end = (gfc_charlen_t) mpz_get_ui (p->ref->u.ss.end->value.integer); |
1684 | start = (gfc_charlen_t) mpz_get_ui (p->ref->u.ss.start->value.integer); | |
1685 | if (end >= start) | |
1686 | length = end - start + 1; | |
1687 | else | |
1688 | length = 0; | |
a4a11197 | 1689 | |
00660189 | 1690 | chr = (*newp)->value.character.string = gfc_get_wide_string (length + 1); |
b35c5f01 | 1691 | (*newp)->value.character.length = length; |
00660189 FXC |
1692 | memcpy (chr, &p->value.character.string[start - 1], |
1693 | length * sizeof (gfc_char_t)); | |
b35c5f01 | 1694 | chr[length] = '\0'; |
524af0d6 | 1695 | return true; |
a4a11197 PT |
1696 | } |
1697 | ||
1698 | ||
a5fbc2f3 PT |
1699 | /* Pull an inquiry result out of an expression. */ |
1700 | ||
1701 | static bool | |
1702 | find_inquiry_ref (gfc_expr *p, gfc_expr **newp) | |
1703 | { | |
1704 | gfc_ref *ref; | |
1705 | gfc_ref *inquiry = NULL; | |
1706 | gfc_expr *tmp; | |
1707 | ||
1708 | tmp = gfc_copy_expr (p); | |
1709 | ||
1710 | if (tmp->ref && tmp->ref->type == REF_INQUIRY) | |
1711 | { | |
1712 | inquiry = tmp->ref; | |
1713 | tmp->ref = NULL; | |
1714 | } | |
1715 | else | |
1716 | { | |
1717 | for (ref = tmp->ref; ref; ref = ref->next) | |
1718 | if (ref->next && ref->next->type == REF_INQUIRY) | |
1719 | { | |
1720 | inquiry = ref->next; | |
1721 | ref->next = NULL; | |
1722 | } | |
1723 | } | |
1724 | ||
1725 | if (!inquiry) | |
1726 | { | |
1727 | gfc_free_expr (tmp); | |
1728 | return false; | |
1729 | } | |
1730 | ||
1731 | gfc_resolve_expr (tmp); | |
1732 | ||
69597e2f PT |
1733 | /* In principle there can be more than one inquiry reference. */ |
1734 | for (; inquiry; inquiry = inquiry->next) | |
a5fbc2f3 | 1735 | { |
69597e2f PT |
1736 | switch (inquiry->u.i) |
1737 | { | |
1738 | case INQUIRY_LEN: | |
1739 | if (tmp->ts.type != BT_CHARACTER) | |
1740 | goto cleanup; | |
a5fbc2f3 | 1741 | |
69597e2f PT |
1742 | if (!gfc_notify_std (GFC_STD_F2003, "LEN part_ref at %C")) |
1743 | goto cleanup; | |
a5fbc2f3 | 1744 | |
69597e2f PT |
1745 | if (!tmp->ts.u.cl->length |
1746 | || tmp->ts.u.cl->length->expr_type != EXPR_CONSTANT) | |
1747 | goto cleanup; | |
a5fbc2f3 | 1748 | |
69597e2f PT |
1749 | *newp = gfc_copy_expr (tmp->ts.u.cl->length); |
1750 | break; | |
a5fbc2f3 | 1751 | |
69597e2f PT |
1752 | case INQUIRY_KIND: |
1753 | if (tmp->ts.type == BT_DERIVED || tmp->ts.type == BT_CLASS) | |
1754 | goto cleanup; | |
a5fbc2f3 | 1755 | |
69597e2f PT |
1756 | if (!gfc_notify_std (GFC_STD_F2003, "KIND part_ref at %C")) |
1757 | goto cleanup; | |
a5fbc2f3 | 1758 | |
69597e2f PT |
1759 | *newp = gfc_get_int_expr (gfc_default_integer_kind, |
1760 | NULL, tmp->ts.kind); | |
1761 | break; | |
a5fbc2f3 | 1762 | |
69597e2f PT |
1763 | case INQUIRY_RE: |
1764 | if (tmp->ts.type != BT_COMPLEX || tmp->expr_type != EXPR_CONSTANT) | |
1765 | goto cleanup; | |
a5fbc2f3 | 1766 | |
69597e2f PT |
1767 | if (!gfc_notify_std (GFC_STD_F2008, "RE part_ref at %C")) |
1768 | goto cleanup; | |
a5fbc2f3 | 1769 | |
69597e2f PT |
1770 | *newp = gfc_get_constant_expr (BT_REAL, tmp->ts.kind, &tmp->where); |
1771 | mpfr_set ((*newp)->value.real, | |
1772 | mpc_realref (p->value.complex), GFC_RND_MODE); | |
1773 | break; | |
a5fbc2f3 | 1774 | |
69597e2f PT |
1775 | case INQUIRY_IM: |
1776 | if (tmp->ts.type != BT_COMPLEX || tmp->expr_type != EXPR_CONSTANT) | |
1777 | goto cleanup; | |
a5fbc2f3 | 1778 | |
69597e2f PT |
1779 | if (!gfc_notify_std (GFC_STD_F2008, "IM part_ref at %C")) |
1780 | goto cleanup; | |
a5fbc2f3 | 1781 | |
69597e2f PT |
1782 | *newp = gfc_get_constant_expr (BT_REAL, tmp->ts.kind, &tmp->where); |
1783 | mpfr_set ((*newp)->value.real, | |
1784 | mpc_imagref (p->value.complex), GFC_RND_MODE); | |
1785 | break; | |
1786 | } | |
1787 | tmp = gfc_copy_expr (*newp); | |
a5fbc2f3 PT |
1788 | } |
1789 | ||
1790 | if (!(*newp)) | |
1791 | goto cleanup; | |
1792 | else if ((*newp)->expr_type != EXPR_CONSTANT) | |
1793 | { | |
1794 | gfc_free_expr (*newp); | |
1795 | goto cleanup; | |
1796 | } | |
1797 | ||
1798 | gfc_free_expr (tmp); | |
1799 | return true; | |
1800 | ||
1801 | cleanup: | |
1802 | gfc_free_expr (tmp); | |
1803 | return false; | |
1804 | } | |
1805 | ||
1806 | ||
a4a11197 | 1807 | |
6de9cd9a DN |
1808 | /* Simplify a subobject reference of a constructor. This occurs when |
1809 | parameter variable values are substituted. */ | |
1810 | ||
524af0d6 | 1811 | static bool |
636dff67 | 1812 | simplify_const_ref (gfc_expr *p) |
6de9cd9a | 1813 | { |
b7e75771 | 1814 | gfc_constructor *cons, *c; |
a5fbc2f3 | 1815 | gfc_expr *newp = NULL; |
ff015c5b | 1816 | gfc_ref *last_ref; |
6de9cd9a DN |
1817 | |
1818 | while (p->ref) | |
1819 | { | |
1820 | switch (p->ref->type) | |
1821 | { | |
1822 | case REF_ARRAY: | |
1823 | switch (p->ref->u.ar.type) | |
1824 | { | |
1825 | case AR_ELEMENT: | |
ff015c5b PT |
1826 | /* <type/kind spec>, parameter :: x(<int>) = scalar_expr |
1827 | will generate this. */ | |
1828 | if (p->expr_type != EXPR_ARRAY) | |
1829 | { | |
1830 | remove_subobject_ref (p, NULL); | |
1831 | break; | |
1832 | } | |
524af0d6 JB |
1833 | if (!find_array_element (p->value.constructor, &p->ref->u.ar, &cons)) |
1834 | return false; | |
a4a11197 | 1835 | |
6de9cd9a | 1836 | if (!cons) |
524af0d6 | 1837 | return true; |
a4a11197 | 1838 | |
6de9cd9a DN |
1839 | remove_subobject_ref (p, cons); |
1840 | break; | |
1841 | ||
a4a11197 | 1842 | case AR_SECTION: |
524af0d6 JB |
1843 | if (!find_array_section (p, p->ref)) |
1844 | return false; | |
a4a11197 PT |
1845 | p->ref->u.ar.type = AR_FULL; |
1846 | ||
66e4ab31 | 1847 | /* Fall through. */ |
a4a11197 | 1848 | |
6de9cd9a | 1849 | case AR_FULL: |
a4a11197 | 1850 | if (p->ref->next != NULL |
f6288c24 | 1851 | && (p->ts.type == BT_CHARACTER || gfc_bt_struct (p->ts.type))) |
6de9cd9a | 1852 | { |
b7e75771 JD |
1853 | for (c = gfc_constructor_first (p->value.constructor); |
1854 | c; c = gfc_constructor_next (c)) | |
a4a11197 | 1855 | { |
b7e75771 | 1856 | c->expr->ref = gfc_copy_ref (p->ref->next); |
524af0d6 JB |
1857 | if (!simplify_const_ref (c->expr)) |
1858 | return false; | |
d5551618 DK |
1859 | } |
1860 | ||
f6288c24 | 1861 | if (gfc_bt_struct (p->ts.type) |
ff015c5b | 1862 | && p->ref->next |
b7e75771 | 1863 | && (c = gfc_constructor_first (p->value.constructor))) |
d5551618 | 1864 | { |
ff015c5b | 1865 | /* There may have been component references. */ |
b7e75771 | 1866 | p->ts = c->expr->ts; |
ff015c5b | 1867 | } |
d5551618 | 1868 | |
ff015c5b PT |
1869 | last_ref = p->ref; |
1870 | for (; last_ref->next; last_ref = last_ref->next) {}; | |
d5551618 | 1871 | |
ff015c5b PT |
1872 | if (p->ts.type == BT_CHARACTER |
1873 | && last_ref->type == REF_SUBSTRING) | |
1874 | { | |
1875 | /* If this is a CHARACTER array and we possibly took | |
1876 | a substring out of it, update the type-spec's | |
1877 | character length according to the first element | |
1878 | (as all should have the same length). */ | |
f622221a | 1879 | gfc_charlen_t string_len; |
b7e75771 | 1880 | if ((c = gfc_constructor_first (p->value.constructor))) |
d5551618 | 1881 | { |
b7e75771 | 1882 | const gfc_expr* first = c->expr; |
d5551618 DK |
1883 | gcc_assert (first->expr_type == EXPR_CONSTANT); |
1884 | gcc_assert (first->ts.type == BT_CHARACTER); | |
1885 | string_len = first->value.character.length; | |
1886 | } | |
1887 | else | |
1888 | string_len = 0; | |
1889 | ||
bc21d315 | 1890 | if (!p->ts.u.cl) |
b76e28c6 JW |
1891 | p->ts.u.cl = gfc_new_charlen (p->symtree->n.sym->ns, |
1892 | NULL); | |
1893 | else | |
1894 | gfc_free_expr (p->ts.u.cl->length); | |
1895 | ||
b7e75771 | 1896 | p->ts.u.cl->length |
f622221a | 1897 | = gfc_get_int_expr (gfc_charlen_int_kind, |
b7e75771 | 1898 | NULL, string_len); |
a4a11197 | 1899 | } |
6de9cd9a | 1900 | } |
a4a11197 PT |
1901 | gfc_free_ref_list (p->ref); |
1902 | p->ref = NULL; | |
6de9cd9a DN |
1903 | break; |
1904 | ||
1905 | default: | |
524af0d6 | 1906 | return true; |
6de9cd9a DN |
1907 | } |
1908 | ||
1909 | break; | |
1910 | ||
1911 | case REF_COMPONENT: | |
1912 | cons = find_component_ref (p->value.constructor, p->ref); | |
1913 | remove_subobject_ref (p, cons); | |
1914 | break; | |
1915 | ||
a5fbc2f3 PT |
1916 | case REF_INQUIRY: |
1917 | if (!find_inquiry_ref (p, &newp)) | |
1918 | return false; | |
1919 | ||
1920 | gfc_replace_expr (p, newp); | |
1921 | gfc_free_ref_list (p->ref); | |
1922 | p->ref = NULL; | |
1923 | break; | |
1924 | ||
6de9cd9a | 1925 | case REF_SUBSTRING: |
a5fbc2f3 | 1926 | if (!find_substring_ref (p, &newp)) |
524af0d6 | 1927 | return false; |
a4a11197 PT |
1928 | |
1929 | gfc_replace_expr (p, newp); | |
1930 | gfc_free_ref_list (p->ref); | |
1931 | p->ref = NULL; | |
1932 | break; | |
6de9cd9a DN |
1933 | } |
1934 | } | |
1935 | ||
524af0d6 | 1936 | return true; |
6de9cd9a DN |
1937 | } |
1938 | ||
1939 | ||
1940 | /* Simplify a chain of references. */ | |
1941 | ||
524af0d6 | 1942 | static bool |
a5fbc2f3 | 1943 | simplify_ref_chain (gfc_ref *ref, int type, gfc_expr **p) |
6de9cd9a DN |
1944 | { |
1945 | int n; | |
a5fbc2f3 | 1946 | gfc_expr *newp; |
6de9cd9a DN |
1947 | |
1948 | for (; ref; ref = ref->next) | |
1949 | { | |
1950 | switch (ref->type) | |
1951 | { | |
1952 | case REF_ARRAY: | |
1953 | for (n = 0; n < ref->u.ar.dimen; n++) | |
1954 | { | |
524af0d6 JB |
1955 | if (!gfc_simplify_expr (ref->u.ar.start[n], type)) |
1956 | return false; | |
1957 | if (!gfc_simplify_expr (ref->u.ar.end[n], type)) | |
1958 | return false; | |
1959 | if (!gfc_simplify_expr (ref->u.ar.stride[n], type)) | |
1960 | return false; | |
6de9cd9a DN |
1961 | } |
1962 | break; | |
1963 | ||
1964 | case REF_SUBSTRING: | |
524af0d6 JB |
1965 | if (!gfc_simplify_expr (ref->u.ss.start, type)) |
1966 | return false; | |
1967 | if (!gfc_simplify_expr (ref->u.ss.end, type)) | |
1968 | return false; | |
6de9cd9a DN |
1969 | break; |
1970 | ||
a5fbc2f3 PT |
1971 | case REF_INQUIRY: |
1972 | if (!find_inquiry_ref (*p, &newp)) | |
1973 | return false; | |
1974 | ||
1975 | gfc_replace_expr (*p, newp); | |
1976 | gfc_free_ref_list ((*p)->ref); | |
1977 | (*p)->ref = NULL; | |
dca00617 | 1978 | return true; |
a5fbc2f3 | 1979 | |
6de9cd9a DN |
1980 | default: |
1981 | break; | |
1982 | } | |
1983 | } | |
524af0d6 | 1984 | return true; |
6de9cd9a DN |
1985 | } |
1986 | ||
1987 | ||
1988 | /* Try to substitute the value of a parameter variable. */ | |
66e4ab31 | 1989 | |
524af0d6 | 1990 | static bool |
636dff67 | 1991 | simplify_parameter_variable (gfc_expr *p, int type) |
6de9cd9a DN |
1992 | { |
1993 | gfc_expr *e; | |
524af0d6 | 1994 | bool t; |
6de9cd9a | 1995 | |
5867bb9a TK |
1996 | if (gfc_is_size_zero_array (p)) |
1997 | { | |
1998 | if (p->expr_type == EXPR_ARRAY) | |
1999 | return true; | |
2000 | ||
2001 | e = gfc_get_expr (); | |
2002 | e->expr_type = EXPR_ARRAY; | |
2003 | e->ts = p->ts; | |
2004 | e->rank = p->rank; | |
2005 | e->value.constructor = NULL; | |
2006 | e->shape = gfc_copy_shape (p->shape, p->rank); | |
2007 | e->where = p->where; | |
2008 | gfc_replace_expr (p, e); | |
2009 | return true; | |
2010 | } | |
2011 | ||
6de9cd9a | 2012 | e = gfc_copy_expr (p->symtree->n.sym->value); |
a4a11197 | 2013 | if (e == NULL) |
524af0d6 | 2014 | return false; |
a4a11197 | 2015 | |
b9703d98 EE |
2016 | e->rank = p->rank; |
2017 | ||
c2fee3de DE |
2018 | /* Do not copy subobject refs for constant. */ |
2019 | if (e->expr_type != EXPR_CONSTANT && p->ref != NULL) | |
8e1f752a | 2020 | e->ref = gfc_copy_ref (p->ref); |
6de9cd9a DN |
2021 | t = gfc_simplify_expr (e, type); |
2022 | ||
66e4ab31 | 2023 | /* Only use the simplification if it eliminated all subobject references. */ |
524af0d6 | 2024 | if (t && !e->ref) |
6de9cd9a DN |
2025 | gfc_replace_expr (p, e); |
2026 | else | |
2027 | gfc_free_expr (e); | |
2028 | ||
2029 | return t; | |
2030 | } | |
2031 | ||
87c789f1 PT |
2032 | |
2033 | static bool | |
2034 | scalarize_intrinsic_call (gfc_expr *, bool init_flag); | |
2035 | ||
6de9cd9a DN |
2036 | /* Given an expression, simplify it by collapsing constant |
2037 | expressions. Most simplification takes place when the expression | |
2038 | tree is being constructed. If an intrinsic function is simplified | |
2039 | at some point, we get called again to collapse the result against | |
2040 | other constants. | |
2041 | ||
2042 | We work by recursively simplifying expression nodes, simplifying | |
2043 | intrinsic functions where possible, which can lead to further | |
2044 | constant collapsing. If an operator has constant operand(s), we | |
2045 | rip the expression apart, and rebuild it, hoping that it becomes | |
2046 | something simpler. | |
2047 | ||
2048 | The expression type is defined for: | |
2049 | 0 Basic expression parsing | |
2050 | 1 Simplifying array constructors -- will substitute | |
636dff67 | 2051 | iterator values. |
524af0d6 JB |
2052 | Returns false on error, true otherwise. |
2053 | NOTE: Will return true even if the expression can not be simplified. */ | |
6de9cd9a | 2054 | |
524af0d6 | 2055 | bool |
636dff67 | 2056 | gfc_simplify_expr (gfc_expr *p, int type) |
6de9cd9a DN |
2057 | { |
2058 | gfc_actual_arglist *ap; | |
87c789f1 PT |
2059 | gfc_intrinsic_sym* isym = NULL; |
2060 | ||
6de9cd9a DN |
2061 | |
2062 | if (p == NULL) | |
524af0d6 | 2063 | return true; |
6de9cd9a DN |
2064 | |
2065 | switch (p->expr_type) | |
2066 | { | |
2067 | case EXPR_CONSTANT: | |
a5fbc2f3 PT |
2068 | if (p->ref && p->ref->type == REF_INQUIRY) |
2069 | simplify_ref_chain (p->ref, type, &p); | |
2070 | break; | |
6de9cd9a DN |
2071 | case EXPR_NULL: |
2072 | break; | |
2073 | ||
2074 | case EXPR_FUNCTION: | |
47b92d22 TB |
2075 | // For array-bound functions, we don't need to optimize |
2076 | // the 'array' argument. In particular, if the argument | |
2077 | // is a PARAMETER, simplifying might convert an EXPR_VARIABLE | |
2078 | // into an EXPR_ARRAY; the latter has lbound = 1, the former | |
2079 | // can have any lbound. | |
2080 | ap = p->value.function.actual; | |
2081 | if (p->value.function.isym && | |
2082 | (p->value.function.isym->id == GFC_ISYM_LBOUND | |
2083 | || p->value.function.isym->id == GFC_ISYM_UBOUND | |
2084 | || p->value.function.isym->id == GFC_ISYM_LCOBOUND | |
2085 | || p->value.function.isym->id == GFC_ISYM_UCOBOUND)) | |
2086 | ap = ap->next; | |
2087 | ||
2088 | for ( ; ap; ap = ap->next) | |
524af0d6 JB |
2089 | if (!gfc_simplify_expr (ap->expr, type)) |
2090 | return false; | |
6de9cd9a DN |
2091 | |
2092 | if (p->value.function.isym != NULL | |
2093 | && gfc_intrinsic_func_interface (p, 1) == MATCH_ERROR) | |
524af0d6 | 2094 | return false; |
6de9cd9a | 2095 | |
87c789f1 PT |
2096 | if (p->expr_type == EXPR_FUNCTION) |
2097 | { | |
2098 | if (p->symtree) | |
2099 | isym = gfc_find_function (p->symtree->n.sym->name); | |
2100 | if (isym && isym->elemental) | |
2101 | scalarize_intrinsic_call (p, false); | |
2102 | } | |
2103 | ||
6de9cd9a DN |
2104 | break; |
2105 | ||
2106 | case EXPR_SUBSTRING: | |
a5fbc2f3 | 2107 | if (!simplify_ref_chain (p->ref, type, &p)) |
524af0d6 | 2108 | return false; |
6de9cd9a | 2109 | |
c2fee3de DE |
2110 | if (gfc_is_constant_expr (p)) |
2111 | { | |
00660189 | 2112 | gfc_char_t *s; |
f622221a | 2113 | HOST_WIDE_INT start, end; |
c2fee3de | 2114 | |
e8d4f3fc | 2115 | start = 0; |
9a251aa1 FXC |
2116 | if (p->ref && p->ref->u.ss.start) |
2117 | { | |
f622221a | 2118 | gfc_extract_hwi (p->ref->u.ss.start, &start); |
9a251aa1 FXC |
2119 | start--; /* Convert from one-based to zero-based. */ |
2120 | } | |
9a251aa1 | 2121 | |
e8d4f3fc | 2122 | end = p->value.character.length; |
9a251aa1 | 2123 | if (p->ref && p->ref->u.ss.end) |
f622221a | 2124 | gfc_extract_hwi (p->ref->u.ss.end, &end); |
9a251aa1 | 2125 | |
b8bc0ff7 FXC |
2126 | if (end < start) |
2127 | end = start; | |
7d0300ed | 2128 | |
00660189 FXC |
2129 | s = gfc_get_wide_string (end - start + 2); |
2130 | memcpy (s, p->value.character.string + start, | |
2131 | (end - start) * sizeof (gfc_char_t)); | |
636dff67 | 2132 | s[end - start + 1] = '\0'; /* TODO: C-style string. */ |
cede9502 | 2133 | free (p->value.character.string); |
c2fee3de DE |
2134 | p->value.character.string = s; |
2135 | p->value.character.length = end - start; | |
b76e28c6 | 2136 | p->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL); |
f622221a | 2137 | p->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind, |
b7e75771 JD |
2138 | NULL, |
2139 | p->value.character.length); | |
c2fee3de DE |
2140 | gfc_free_ref_list (p->ref); |
2141 | p->ref = NULL; | |
2142 | p->expr_type = EXPR_CONSTANT; | |
2143 | } | |
6de9cd9a DN |
2144 | break; |
2145 | ||
2146 | case EXPR_OP: | |
524af0d6 JB |
2147 | if (!simplify_intrinsic_op (p, type)) |
2148 | return false; | |
6de9cd9a DN |
2149 | break; |
2150 | ||
2151 | case EXPR_VARIABLE: | |
2152 | /* Only substitute array parameter variables if we are in an | |
636dff67 | 2153 | initialization expression, or we want a subsection. */ |
6de9cd9a | 2154 | if (p->symtree->n.sym->attr.flavor == FL_PARAMETER |
f2cbd86c | 2155 | && (gfc_init_expr_flag || p->ref |
22c30bc0 | 2156 | || p->symtree->n.sym->value->expr_type != EXPR_ARRAY)) |
6de9cd9a | 2157 | { |
524af0d6 JB |
2158 | if (!simplify_parameter_variable (p, type)) |
2159 | return false; | |
6de9cd9a DN |
2160 | break; |
2161 | } | |
2162 | ||
2163 | if (type == 1) | |
2164 | { | |
2165 | gfc_simplify_iterator_var (p); | |
2166 | } | |
2167 | ||
2168 | /* Simplify subcomponent references. */ | |
a5fbc2f3 | 2169 | if (!simplify_ref_chain (p->ref, type, &p)) |
524af0d6 | 2170 | return false; |
6de9cd9a DN |
2171 | |
2172 | break; | |
2173 | ||
2174 | case EXPR_STRUCTURE: | |
2175 | case EXPR_ARRAY: | |
a5fbc2f3 | 2176 | if (!simplify_ref_chain (p->ref, type, &p)) |
524af0d6 | 2177 | return false; |
6de9cd9a | 2178 | |
524af0d6 JB |
2179 | if (!simplify_constructor (p->value.constructor, type)) |
2180 | return false; | |
6de9cd9a | 2181 | |
636dff67 SK |
2182 | if (p->expr_type == EXPR_ARRAY && p->ref && p->ref->type == REF_ARRAY |
2183 | && p->ref->u.ar.type == AR_FULL) | |
928f0490 | 2184 | gfc_expand_constructor (p, false); |
6de9cd9a | 2185 | |
524af0d6 JB |
2186 | if (!simplify_const_ref (p)) |
2187 | return false; | |
6de9cd9a DN |
2188 | |
2189 | break; | |
8e1f752a DK |
2190 | |
2191 | case EXPR_COMPCALL: | |
713485cc | 2192 | case EXPR_PPC: |
8e1f752a | 2193 | break; |
6de9cd9a DN |
2194 | } |
2195 | ||
524af0d6 | 2196 | return true; |
6de9cd9a DN |
2197 | } |
2198 | ||
2199 | ||
2200 | /* Returns the type of an expression with the exception that iterator | |
2201 | variables are automatically integers no matter what else they may | |
2202 | be declared as. */ | |
2203 | ||
2204 | static bt | |
636dff67 | 2205 | et0 (gfc_expr *e) |
6de9cd9a | 2206 | { |
524af0d6 | 2207 | if (e->expr_type == EXPR_VARIABLE && gfc_check_iter_variable (e)) |
6de9cd9a DN |
2208 | return BT_INTEGER; |
2209 | ||
2210 | return e->ts.type; | |
2211 | } | |
2212 | ||
2213 | ||
396b2c19 PT |
2214 | /* Scalarize an expression for an elemental intrinsic call. */ |
2215 | ||
524af0d6 | 2216 | static bool |
87c789f1 | 2217 | scalarize_intrinsic_call (gfc_expr *e, bool init_flag) |
396b2c19 PT |
2218 | { |
2219 | gfc_actual_arglist *a, *b; | |
b7e75771 | 2220 | gfc_constructor_base ctor; |
3995f3a2 | 2221 | gfc_constructor *args[5] = {}; /* Avoid uninitialized warnings. */ |
b7e75771 | 2222 | gfc_constructor *ci, *new_ctor; |
396b2c19 | 2223 | gfc_expr *expr, *old; |
679d9637 | 2224 | int n, i, rank[5], array_arg; |
87c789f1 PT |
2225 | int errors = 0; |
2226 | ||
2227 | if (e == NULL) | |
2228 | return false; | |
2229 | ||
2230 | a = e->value.function.actual; | |
2231 | for (; a; a = a->next) | |
2232 | if (a->expr && !gfc_is_constant_expr (a->expr)) | |
2233 | return false; | |
8b704316 | 2234 | |
679d9637 PT |
2235 | /* Find which, if any, arguments are arrays. Assume that the old |
2236 | expression carries the type information and that the first arg | |
2237 | that is an array expression carries all the shape information.*/ | |
2238 | n = array_arg = 0; | |
05e6ff80 | 2239 | a = e->value.function.actual; |
679d9637 PT |
2240 | for (; a; a = a->next) |
2241 | { | |
2242 | n++; | |
c8d599e1 | 2243 | if (!a->expr || a->expr->expr_type != EXPR_ARRAY) |
679d9637 PT |
2244 | continue; |
2245 | array_arg = n; | |
2246 | expr = gfc_copy_expr (a->expr); | |
2247 | break; | |
2248 | } | |
2249 | ||
2250 | if (!array_arg) | |
524af0d6 | 2251 | return false; |
05e6ff80 PT |
2252 | |
2253 | old = gfc_copy_expr (e); | |
679d9637 | 2254 | |
b7e75771 | 2255 | gfc_constructor_free (expr->value.constructor); |
396b2c19 | 2256 | expr->value.constructor = NULL; |
396b2c19 | 2257 | expr->ts = old->ts; |
679d9637 | 2258 | expr->where = old->where; |
396b2c19 PT |
2259 | expr->expr_type = EXPR_ARRAY; |
2260 | ||
2261 | /* Copy the array argument constructors into an array, with nulls | |
2262 | for the scalars. */ | |
2263 | n = 0; | |
2264 | a = old->value.function.actual; | |
2265 | for (; a; a = a->next) | |
2266 | { | |
2267 | /* Check that this is OK for an initialization expression. */ | |
87c789f1 | 2268 | if (a->expr && init_flag && !gfc_check_init_expr (a->expr)) |
396b2c19 PT |
2269 | goto cleanup; |
2270 | ||
2271 | rank[n] = 0; | |
2272 | if (a->expr && a->expr->rank && a->expr->expr_type == EXPR_VARIABLE) | |
2273 | { | |
2274 | rank[n] = a->expr->rank; | |
2275 | ctor = a->expr->symtree->n.sym->value->value.constructor; | |
b7e75771 | 2276 | args[n] = gfc_constructor_first (ctor); |
396b2c19 PT |
2277 | } |
2278 | else if (a->expr && a->expr->expr_type == EXPR_ARRAY) | |
2279 | { | |
2280 | if (a->expr->rank) | |
2281 | rank[n] = a->expr->rank; | |
2282 | else | |
2283 | rank[n] = 1; | |
b7e75771 JD |
2284 | ctor = gfc_constructor_copy (a->expr->value.constructor); |
2285 | args[n] = gfc_constructor_first (ctor); | |
396b2c19 PT |
2286 | } |
2287 | else | |
2288 | args[n] = NULL; | |
b7e75771 | 2289 | |
396b2c19 PT |
2290 | n++; |
2291 | } | |
2292 | ||
87c789f1 | 2293 | gfc_get_errors (NULL, &errors); |
396b2c19 | 2294 | |
05e6ff80 | 2295 | /* Using the array argument as the master, step through the array |
396b2c19 PT |
2296 | calling the function for each element and advancing the array |
2297 | constructors together. */ | |
b7e75771 | 2298 | for (ci = args[array_arg - 1]; ci; ci = gfc_constructor_next (ci)) |
396b2c19 | 2299 | { |
b7e75771 JD |
2300 | new_ctor = gfc_constructor_append_expr (&expr->value.constructor, |
2301 | gfc_copy_expr (old), NULL); | |
2302 | ||
2303 | gfc_free_actual_arglist (new_ctor->expr->value.function.actual); | |
2304 | a = NULL; | |
2305 | b = old->value.function.actual; | |
2306 | for (i = 0; i < n; i++) | |
2307 | { | |
2308 | if (a == NULL) | |
2309 | new_ctor->expr->value.function.actual | |
2310 | = a = gfc_get_actual_arglist (); | |
396b2c19 PT |
2311 | else |
2312 | { | |
b7e75771 JD |
2313 | a->next = gfc_get_actual_arglist (); |
2314 | a = a->next; | |
396b2c19 | 2315 | } |
396b2c19 | 2316 | |
b7e75771 JD |
2317 | if (args[i]) |
2318 | a->expr = gfc_copy_expr (args[i]->expr); | |
2319 | else | |
2320 | a->expr = gfc_copy_expr (b->expr); | |
2321 | ||
2322 | b = b->next; | |
2323 | } | |
396b2c19 | 2324 | |
b7e75771 JD |
2325 | /* Simplify the function calls. If the simplification fails, the |
2326 | error will be flagged up down-stream or the library will deal | |
2327 | with it. */ | |
87c789f1 PT |
2328 | if (errors == 0) |
2329 | gfc_simplify_expr (new_ctor->expr, 0); | |
396b2c19 | 2330 | |
b7e75771 JD |
2331 | for (i = 0; i < n; i++) |
2332 | if (args[i]) | |
2333 | args[i] = gfc_constructor_next (args[i]); | |
396b2c19 | 2334 | |
b7e75771 JD |
2335 | for (i = 1; i < n; i++) |
2336 | if (rank[i] && ((args[i] != NULL && args[array_arg - 1] == NULL) | |
2337 | || (args[i] == NULL && args[array_arg - 1] != NULL))) | |
2338 | goto compliance; | |
396b2c19 PT |
2339 | } |
2340 | ||
2341 | free_expr0 (e); | |
2342 | *e = *expr; | |
efb63364 TB |
2343 | /* Free "expr" but not the pointers it contains. */ |
2344 | free (expr); | |
396b2c19 | 2345 | gfc_free_expr (old); |
524af0d6 | 2346 | return true; |
396b2c19 PT |
2347 | |
2348 | compliance: | |
2349 | gfc_error_now ("elemental function arguments at %C are not compliant"); | |
2350 | ||
2351 | cleanup: | |
2352 | gfc_free_expr (expr); | |
2353 | gfc_free_expr (old); | |
524af0d6 | 2354 | return false; |
396b2c19 PT |
2355 | } |
2356 | ||
2357 | ||
524af0d6 JB |
2358 | static bool |
2359 | check_intrinsic_op (gfc_expr *e, bool (*check_function) (gfc_expr *)) | |
6de9cd9a | 2360 | { |
58b03ab2 TS |
2361 | gfc_expr *op1 = e->value.op.op1; |
2362 | gfc_expr *op2 = e->value.op.op2; | |
6de9cd9a | 2363 | |
524af0d6 JB |
2364 | if (!(*check_function)(op1)) |
2365 | return false; | |
6de9cd9a | 2366 | |
a1ee985f | 2367 | switch (e->value.op.op) |
6de9cd9a DN |
2368 | { |
2369 | case INTRINSIC_UPLUS: | |
2370 | case INTRINSIC_UMINUS: | |
58b03ab2 | 2371 | if (!numeric_type (et0 (op1))) |
6de9cd9a DN |
2372 | goto not_numeric; |
2373 | break; | |
2374 | ||
2375 | case INTRINSIC_EQ: | |
3bed9dd0 | 2376 | case INTRINSIC_EQ_OS: |
6de9cd9a | 2377 | case INTRINSIC_NE: |
3bed9dd0 | 2378 | case INTRINSIC_NE_OS: |
6de9cd9a | 2379 | case INTRINSIC_GT: |
3bed9dd0 | 2380 | case INTRINSIC_GT_OS: |
6de9cd9a | 2381 | case INTRINSIC_GE: |
3bed9dd0 | 2382 | case INTRINSIC_GE_OS: |
6de9cd9a | 2383 | case INTRINSIC_LT: |
3bed9dd0 | 2384 | case INTRINSIC_LT_OS: |
6de9cd9a | 2385 | case INTRINSIC_LE: |
3bed9dd0 | 2386 | case INTRINSIC_LE_OS: |
524af0d6 JB |
2387 | if (!(*check_function)(op2)) |
2388 | return false; | |
8b704316 | 2389 | |
58b03ab2 TS |
2390 | if (!(et0 (op1) == BT_CHARACTER && et0 (op2) == BT_CHARACTER) |
2391 | && !(numeric_type (et0 (op1)) && numeric_type (et0 (op2)))) | |
e063a048 TS |
2392 | { |
2393 | gfc_error ("Numeric or CHARACTER operands are required in " | |
2394 | "expression at %L", &e->where); | |
524af0d6 | 2395 | return false; |
e063a048 TS |
2396 | } |
2397 | break; | |
6de9cd9a DN |
2398 | |
2399 | case INTRINSIC_PLUS: | |
2400 | case INTRINSIC_MINUS: | |
2401 | case INTRINSIC_TIMES: | |
2402 | case INTRINSIC_DIVIDE: | |
2403 | case INTRINSIC_POWER: | |
524af0d6 JB |
2404 | if (!(*check_function)(op2)) |
2405 | return false; | |
6de9cd9a | 2406 | |
58b03ab2 | 2407 | if (!numeric_type (et0 (op1)) || !numeric_type (et0 (op2))) |
6de9cd9a DN |
2408 | goto not_numeric; |
2409 | ||
6de9cd9a DN |
2410 | break; |
2411 | ||
2412 | case INTRINSIC_CONCAT: | |
524af0d6 JB |
2413 | if (!(*check_function)(op2)) |
2414 | return false; | |
6de9cd9a | 2415 | |
58b03ab2 | 2416 | if (et0 (op1) != BT_CHARACTER || et0 (op2) != BT_CHARACTER) |
6de9cd9a DN |
2417 | { |
2418 | gfc_error ("Concatenation operator in expression at %L " | |
58b03ab2 | 2419 | "must have two CHARACTER operands", &op1->where); |
524af0d6 | 2420 | return false; |
6de9cd9a DN |
2421 | } |
2422 | ||
58b03ab2 | 2423 | if (op1->ts.kind != op2->ts.kind) |
6de9cd9a DN |
2424 | { |
2425 | gfc_error ("Concat operator at %L must concatenate strings of the " | |
2426 | "same kind", &e->where); | |
524af0d6 | 2427 | return false; |
6de9cd9a DN |
2428 | } |
2429 | ||
2430 | break; | |
2431 | ||
2432 | case INTRINSIC_NOT: | |
58b03ab2 | 2433 | if (et0 (op1) != BT_LOGICAL) |
6de9cd9a DN |
2434 | { |
2435 | gfc_error (".NOT. operator in expression at %L must have a LOGICAL " | |
58b03ab2 | 2436 | "operand", &op1->where); |
524af0d6 | 2437 | return false; |
6de9cd9a DN |
2438 | } |
2439 | ||
2440 | break; | |
2441 | ||
2442 | case INTRINSIC_AND: | |
2443 | case INTRINSIC_OR: | |
2444 | case INTRINSIC_EQV: | |
2445 | case INTRINSIC_NEQV: | |
524af0d6 JB |
2446 | if (!(*check_function)(op2)) |
2447 | return false; | |
6de9cd9a | 2448 | |
58b03ab2 | 2449 | if (et0 (op1) != BT_LOGICAL || et0 (op2) != BT_LOGICAL) |
6de9cd9a DN |
2450 | { |
2451 | gfc_error ("LOGICAL operands are required in expression at %L", | |
2452 | &e->where); | |
524af0d6 | 2453 | return false; |
6de9cd9a DN |
2454 | } |
2455 | ||
2456 | break; | |
2457 | ||
083cc293 TS |
2458 | case INTRINSIC_PARENTHESES: |
2459 | break; | |
2460 | ||
6de9cd9a DN |
2461 | default: |
2462 | gfc_error ("Only intrinsic operators can be used in expression at %L", | |
2463 | &e->where); | |
524af0d6 | 2464 | return false; |
6de9cd9a DN |
2465 | } |
2466 | ||
524af0d6 | 2467 | return true; |
6de9cd9a DN |
2468 | |
2469 | not_numeric: | |
2470 | gfc_error ("Numeric operands are required in expression at %L", &e->where); | |
2471 | ||
524af0d6 | 2472 | return false; |
6de9cd9a DN |
2473 | } |
2474 | ||
604df116 DF |
2475 | /* F2003, 7.1.7 (3): In init expression, allocatable components |
2476 | must not be data-initialized. */ | |
524af0d6 | 2477 | static bool |
604df116 DF |
2478 | check_alloc_comp_init (gfc_expr *e) |
2479 | { | |
b7e75771 | 2480 | gfc_component *comp; |
604df116 DF |
2481 | gfc_constructor *ctor; |
2482 | ||
2483 | gcc_assert (e->expr_type == EXPR_STRUCTURE); | |
103c4f75 | 2484 | gcc_assert (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS); |
604df116 | 2485 | |
b7e75771 JD |
2486 | for (comp = e->ts.u.derived->components, |
2487 | ctor = gfc_constructor_first (e->value.constructor); | |
2488 | comp; comp = comp->next, ctor = gfc_constructor_next (ctor)) | |
604df116 | 2489 | { |
7430df97 | 2490 | if (comp->attr.allocatable && ctor->expr |
604df116 DF |
2491 | && ctor->expr->expr_type != EXPR_NULL) |
2492 | { | |
c4100eae MLI |
2493 | gfc_error ("Invalid initialization expression for ALLOCATABLE " |
2494 | "component %qs in structure constructor at %L", | |
2495 | comp->name, &ctor->expr->where); | |
524af0d6 | 2496 | return false; |
604df116 DF |
2497 | } |
2498 | } | |
2499 | ||
524af0d6 | 2500 | return true; |
604df116 | 2501 | } |
6de9cd9a | 2502 | |
e1633d82 DF |
2503 | static match |
2504 | check_init_expr_arguments (gfc_expr *e) | |
2505 | { | |
2506 | gfc_actual_arglist *ap; | |
6de9cd9a | 2507 | |
e1633d82 | 2508 | for (ap = e->value.function.actual; ap; ap = ap->next) |
524af0d6 | 2509 | if (!gfc_check_init_expr (ap->expr)) |
e1633d82 | 2510 | return MATCH_ERROR; |
6de9cd9a | 2511 | |
e1633d82 DF |
2512 | return MATCH_YES; |
2513 | } | |
2514 | ||
524af0d6 | 2515 | static bool check_restricted (gfc_expr *); |
ebb479cd | 2516 | |
e1633d82 DF |
2517 | /* F95, 7.1.6.1, Initialization expressions, (7) |
2518 | F2003, 7.1.7 Initialization expression, (8) */ | |
2519 | ||
2520 | static match | |
636dff67 | 2521 | check_inquiry (gfc_expr *e, int not_restricted) |
6de9cd9a DN |
2522 | { |
2523 | const char *name; | |
e1633d82 DF |
2524 | const char *const *functions; |
2525 | ||
2526 | static const char *const inquiry_func_f95[] = { | |
2527 | "lbound", "shape", "size", "ubound", | |
2528 | "bit_size", "len", "kind", | |
2529 | "digits", "epsilon", "huge", "maxexponent", "minexponent", | |
2530 | "precision", "radix", "range", "tiny", | |
2531 | NULL | |
2532 | }; | |
6de9cd9a | 2533 | |
e1633d82 DF |
2534 | static const char *const inquiry_func_f2003[] = { |
2535 | "lbound", "shape", "size", "ubound", | |
2536 | "bit_size", "len", "kind", | |
2537 | "digits", "epsilon", "huge", "maxexponent", "minexponent", | |
2538 | "precision", "radix", "range", "tiny", | |
2539 | "new_line", NULL | |
6de9cd9a DN |
2540 | }; |
2541 | ||
cadddfdd | 2542 | int i = 0; |
e1633d82 DF |
2543 | gfc_actual_arglist *ap; |
2544 | ||
2545 | if (!e->value.function.isym | |
2546 | || !e->value.function.isym->inquiry) | |
2547 | return MATCH_NO; | |
6de9cd9a | 2548 | |
e7f79e12 PT |
2549 | /* An undeclared parameter will get us here (PR25018). */ |
2550 | if (e->symtree == NULL) | |
e1633d82 | 2551 | return MATCH_NO; |
e7f79e12 | 2552 | |
cadddfdd TB |
2553 | if (e->symtree->n.sym->from_intmod) |
2554 | { | |
2555 | if (e->symtree->n.sym->from_intmod == INTMOD_ISO_FORTRAN_ENV | |
2556 | && e->symtree->n.sym->intmod_sym_id != ISOFORTRAN_COMPILER_OPTIONS | |
2557 | && e->symtree->n.sym->intmod_sym_id != ISOFORTRAN_COMPILER_VERSION) | |
2558 | return MATCH_NO; | |
6de9cd9a | 2559 | |
cadddfdd TB |
2560 | if (e->symtree->n.sym->from_intmod == INTMOD_ISO_C_BINDING |
2561 | && e->symtree->n.sym->intmod_sym_id != ISOCBINDING_C_SIZEOF) | |
2562 | return MATCH_NO; | |
2563 | } | |
2564 | else | |
2565 | { | |
2566 | name = e->symtree->n.sym->name; | |
2567 | ||
2568 | functions = (gfc_option.warn_std & GFC_STD_F2003) | |
e1633d82 | 2569 | ? inquiry_func_f2003 : inquiry_func_f95; |
6de9cd9a | 2570 | |
cadddfdd TB |
2571 | for (i = 0; functions[i]; i++) |
2572 | if (strcmp (functions[i], name) == 0) | |
2573 | break; | |
6de9cd9a | 2574 | |
21c0a521 DM |
2575 | if (functions[i] == NULL) |
2576 | return MATCH_ERROR; | |
cadddfdd | 2577 | } |
6de9cd9a | 2578 | |
c2b27658 EE |
2579 | /* At this point we have an inquiry function with a variable argument. The |
2580 | type of the variable might be undefined, but we need it now, because the | |
e1633d82 | 2581 | arguments of these functions are not allowed to be undefined. */ |
6de9cd9a | 2582 | |
e1633d82 | 2583 | for (ap = e->value.function.actual; ap; ap = ap->next) |
6de9cd9a | 2584 | { |
e1633d82 DF |
2585 | if (!ap->expr) |
2586 | continue; | |
2587 | ||
2588 | if (ap->expr->ts.type == BT_UNKNOWN) | |
2589 | { | |
2590 | if (ap->expr->symtree->n.sym->ts.type == BT_UNKNOWN | |
524af0d6 | 2591 | && !gfc_set_default_type (ap->expr->symtree->n.sym, 0, gfc_current_ns)) |
e1633d82 | 2592 | return MATCH_NO; |
6de9cd9a | 2593 | |
e1633d82 DF |
2594 | ap->expr->ts = ap->expr->symtree->n.sym->ts; |
2595 | } | |
2596 | ||
2597 | /* Assumed character length will not reduce to a constant expression | |
2598 | with LEN, as required by the standard. */ | |
8132af9d | 2599 | if (i == 5 && not_restricted && ap->expr->symtree |
e1633d82 | 2600 | && ap->expr->symtree->n.sym->ts.type == BT_CHARACTER |
e69afb29 SK |
2601 | && (ap->expr->symtree->n.sym->ts.u.cl->length == NULL |
2602 | || ap->expr->symtree->n.sym->ts.deferred)) | |
e1633d82 | 2603 | { |
c4100eae | 2604 | gfc_error ("Assumed or deferred character length variable %qs " |
2f029c08 | 2605 | "in constant expression at %L", |
e69afb29 SK |
2606 | ap->expr->symtree->n.sym->name, |
2607 | &ap->expr->where); | |
e1633d82 DF |
2608 | return MATCH_ERROR; |
2609 | } | |
524af0d6 | 2610 | else if (not_restricted && !gfc_check_init_expr (ap->expr)) |
e1633d82 | 2611 | return MATCH_ERROR; |
ebb479cd PT |
2612 | |
2613 | if (not_restricted == 0 | |
2614 | && ap->expr->expr_type != EXPR_VARIABLE | |
524af0d6 | 2615 | && !check_restricted (ap->expr)) |
ebb479cd | 2616 | return MATCH_ERROR; |
26865ab4 SK |
2617 | |
2618 | if (not_restricted == 0 | |
2619 | && ap->expr->expr_type == EXPR_VARIABLE | |
2620 | && ap->expr->symtree->n.sym->attr.dummy | |
2621 | && ap->expr->symtree->n.sym->attr.optional) | |
2622 | return MATCH_NO; | |
6de9cd9a DN |
2623 | } |
2624 | ||
e1633d82 DF |
2625 | return MATCH_YES; |
2626 | } | |
2627 | ||
e7f79e12 | 2628 | |
e1633d82 DF |
2629 | /* F95, 7.1.6.1, Initialization expressions, (5) |
2630 | F2003, 7.1.7 Initialization expression, (5) */ | |
2631 | ||
2632 | static match | |
2633 | check_transformational (gfc_expr *e) | |
2634 | { | |
2635 | static const char * const trans_func_f95[] = { | |
2636 | "repeat", "reshape", "selected_int_kind", | |
2637 | "selected_real_kind", "transfer", "trim", NULL | |
2638 | }; | |
2639 | ||
8ec259c1 | 2640 | static const char * const trans_func_f2003[] = { |
a16d978f DF |
2641 | "all", "any", "count", "dot_product", "matmul", "null", "pack", |
2642 | "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind", | |
c430a6f9 DF |
2643 | "selected_real_kind", "spread", "sum", "transfer", "transpose", |
2644 | "trim", "unpack", NULL | |
8ec259c1 DF |
2645 | }; |
2646 | ||
01ce9e31 TK |
2647 | static const char * const trans_func_f2008[] = { |
2648 | "all", "any", "count", "dot_product", "matmul", "null", "pack", | |
2649 | "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind", | |
2650 | "selected_real_kind", "spread", "sum", "transfer", "transpose", | |
2651 | "trim", "unpack", "findloc", NULL | |
2652 | }; | |
2653 | ||
e1633d82 DF |
2654 | int i; |
2655 | const char *name; | |
8ec259c1 | 2656 | const char *const *functions; |
e1633d82 DF |
2657 | |
2658 | if (!e->value.function.isym | |
2659 | || !e->value.function.isym->transformational) | |
2660 | return MATCH_NO; | |
2661 | ||
2662 | name = e->symtree->n.sym->name; | |
2663 | ||
01ce9e31 TK |
2664 | if (gfc_option.allow_std & GFC_STD_F2008) |
2665 | functions = trans_func_f2008; | |
2666 | else if (gfc_option.allow_std & GFC_STD_F2003) | |
2667 | functions = trans_func_f2003; | |
2668 | else | |
2669 | functions = trans_func_f95; | |
8ec259c1 | 2670 | |
e1633d82 DF |
2671 | /* NULL() is dealt with below. */ |
2672 | if (strcmp ("null", name) == 0) | |
2673 | return MATCH_NO; | |
2674 | ||
8ec259c1 DF |
2675 | for (i = 0; functions[i]; i++) |
2676 | if (strcmp (functions[i], name) == 0) | |
2677 | break; | |
e1633d82 | 2678 | |
8ec259c1 | 2679 | if (functions[i] == NULL) |
5ab0eadf | 2680 | { |
c4100eae MLI |
2681 | gfc_error ("transformational intrinsic %qs at %L is not permitted " |
2682 | "in an initialization expression", name, &e->where); | |
5ab0eadf DF |
2683 | return MATCH_ERROR; |
2684 | } | |
e1633d82 DF |
2685 | |
2686 | return check_init_expr_arguments (e); | |
2687 | } | |
2688 | ||
2689 | ||
2690 | /* F95, 7.1.6.1, Initialization expressions, (6) | |
2691 | F2003, 7.1.7 Initialization expression, (6) */ | |
2692 | ||
2693 | static match | |
2694 | check_null (gfc_expr *e) | |
2695 | { | |
2696 | if (strcmp ("null", e->symtree->n.sym->name) != 0) | |
2697 | return MATCH_NO; | |
2698 | ||
2699 | return check_init_expr_arguments (e); | |
2700 | } | |
2701 | ||
2702 | ||
2703 | static match | |
2704 | check_elemental (gfc_expr *e) | |
2705 | { | |
2706 | if (!e->value.function.isym | |
2707 | || !e->value.function.isym->elemental) | |
2708 | return MATCH_NO; | |
2709 | ||
c2916401 DF |
2710 | if (e->ts.type != BT_INTEGER |
2711 | && e->ts.type != BT_CHARACTER | |
524af0d6 JB |
2712 | && !gfc_notify_std (GFC_STD_F2003, "Evaluation of nonstandard " |
2713 | "initialization expression at %L", &e->where)) | |
e1633d82 DF |
2714 | return MATCH_ERROR; |
2715 | ||
2716 | return check_init_expr_arguments (e); | |
2717 | } | |
2718 | ||
2719 | ||
2720 | static match | |
2721 | check_conversion (gfc_expr *e) | |
2722 | { | |
2723 | if (!e->value.function.isym | |
2724 | || !e->value.function.isym->conversion) | |
2725 | return MATCH_NO; | |
2726 | ||
2727 | return check_init_expr_arguments (e); | |
6de9cd9a DN |
2728 | } |
2729 | ||
2730 | ||
2731 | /* Verify that an expression is an initialization expression. A side | |
2732 | effect is that the expression tree is reduced to a single constant | |
2733 | node if all goes well. This would normally happen when the | |
2734 | expression is constructed but function references are assumed to be | |
2735 | intrinsics in the context of initialization expressions. If | |
524af0d6 | 2736 | false is returned an error message has been generated. */ |
6de9cd9a | 2737 | |
524af0d6 | 2738 | bool |
7ac6a832 | 2739 | gfc_check_init_expr (gfc_expr *e) |
6de9cd9a | 2740 | { |
6de9cd9a | 2741 | match m; |
524af0d6 | 2742 | bool t; |
6de9cd9a DN |
2743 | |
2744 | if (e == NULL) | |
524af0d6 | 2745 | return true; |
6de9cd9a DN |
2746 | |
2747 | switch (e->expr_type) | |
2748 | { | |
2749 | case EXPR_OP: | |
7ac6a832 | 2750 | t = check_intrinsic_op (e, gfc_check_init_expr); |
524af0d6 | 2751 | if (t) |
6de9cd9a DN |
2752 | t = gfc_simplify_expr (e, 0); |
2753 | ||
2754 | break; | |
2755 | ||
2756 | case EXPR_FUNCTION: | |
524af0d6 | 2757 | t = false; |
396b2c19 | 2758 | |
21779d2e | 2759 | { |
3e6ab828 SK |
2760 | bool conversion; |
2761 | gfc_intrinsic_sym* isym = NULL; | |
8b198102 FXC |
2762 | gfc_symbol* sym = e->symtree->n.sym; |
2763 | ||
0e360db9 FXC |
2764 | /* Simplify here the intrinsics from the IEEE_ARITHMETIC and |
2765 | IEEE_EXCEPTIONS modules. */ | |
2766 | int mod = sym->from_intmod; | |
2767 | if (mod == INTMOD_NONE && sym->generic) | |
2768 | mod = sym->generic->sym->from_intmod; | |
2769 | if (mod == INTMOD_IEEE_ARITHMETIC || mod == INTMOD_IEEE_EXCEPTIONS) | |
8b198102 | 2770 | { |
0e360db9 | 2771 | gfc_expr *new_expr = gfc_simplify_ieee_functions (e); |
8b198102 FXC |
2772 | if (new_expr) |
2773 | { | |
2774 | gfc_replace_expr (e, new_expr); | |
2775 | t = true; | |
2776 | break; | |
2777 | } | |
2778 | } | |
c3005b0f | 2779 | |
3e6ab828 SK |
2780 | /* If a conversion function, e.g., __convert_i8_i4, was inserted |
2781 | into an array constructor, we need to skip the error check here. | |
2782 | Conversion errors are caught below in scalarize_intrinsic_call. */ | |
2783 | conversion = e->value.function.isym | |
2784 | && (e->value.function.isym->conversion == 1); | |
2785 | ||
2786 | if (!conversion && (!gfc_is_intrinsic (sym, 0, e->where) | |
2787 | || (m = gfc_intrinsic_func_interface (e, 0)) != MATCH_YES)) | |
21779d2e | 2788 | { |
c4100eae | 2789 | gfc_error ("Function %qs in initialization expression at %L " |
21779d2e DF |
2790 | "must be an intrinsic function", |
2791 | e->symtree->n.sym->name, &e->where); | |
2792 | break; | |
2793 | } | |
6de9cd9a | 2794 | |
21779d2e DF |
2795 | if ((m = check_conversion (e)) == MATCH_NO |
2796 | && (m = check_inquiry (e, 1)) == MATCH_NO | |
2797 | && (m = check_null (e)) == MATCH_NO | |
2798 | && (m = check_transformational (e)) == MATCH_NO | |
2799 | && (m = check_elemental (e)) == MATCH_NO) | |
2800 | { | |
c4100eae | 2801 | gfc_error ("Intrinsic function %qs at %L is not permitted " |
21779d2e DF |
2802 | "in an initialization expression", |
2803 | e->symtree->n.sym->name, &e->where); | |
2804 | m = MATCH_ERROR; | |
2805 | } | |
6de9cd9a | 2806 | |
40885767 | 2807 | if (m == MATCH_ERROR) |
524af0d6 | 2808 | return false; |
40885767 | 2809 | |
21779d2e DF |
2810 | /* Try to scalarize an elemental intrinsic function that has an |
2811 | array argument. */ | |
2812 | isym = gfc_find_function (e->symtree->n.sym->name); | |
2813 | if (isym && isym->elemental | |
87c789f1 | 2814 | && (t = scalarize_intrinsic_call (e, true))) |
21779d2e DF |
2815 | break; |
2816 | } | |
6de9cd9a | 2817 | |
e1633d82 | 2818 | if (m == MATCH_YES) |
fd8e2796 | 2819 | t = gfc_simplify_expr (e, 0); |
e1633d82 | 2820 | |
6de9cd9a DN |
2821 | break; |
2822 | ||
2823 | case EXPR_VARIABLE: | |
524af0d6 | 2824 | t = true; |
6de9cd9a | 2825 | |
5bab4c96 | 2826 | /* This occurs when parsing pdt templates. */ |
18a4e7e3 | 2827 | if (gfc_expr_attr (e).pdt_kind) |
5bab4c96 PT |
2828 | break; |
2829 | ||
524af0d6 | 2830 | if (gfc_check_iter_variable (e)) |
6de9cd9a DN |
2831 | break; |
2832 | ||
2833 | if (e->symtree->n.sym->attr.flavor == FL_PARAMETER) | |
2834 | { | |
106dbde4 DF |
2835 | /* A PARAMETER shall not be used to define itself, i.e. |
2836 | REAL, PARAMETER :: x = transfer(0, x) | |
2837 | is invalid. */ | |
2838 | if (!e->symtree->n.sym->value) | |
2839 | { | |
c4100eae MLI |
2840 | gfc_error ("PARAMETER %qs is used at %L before its definition " |
2841 | "is complete", e->symtree->n.sym->name, &e->where); | |
524af0d6 | 2842 | t = false; |
106dbde4 DF |
2843 | } |
2844 | else | |
2845 | t = simplify_parameter_variable (e, 0); | |
2846 | ||
6de9cd9a DN |
2847 | break; |
2848 | } | |
2849 | ||
2220652d PT |
2850 | if (gfc_in_match_data ()) |
2851 | break; | |
2852 | ||
524af0d6 | 2853 | t = false; |
e1633d82 DF |
2854 | |
2855 | if (e->symtree->n.sym->as) | |
2856 | { | |
2857 | switch (e->symtree->n.sym->as->type) | |
2858 | { | |
2859 | case AS_ASSUMED_SIZE: | |
c4100eae | 2860 | gfc_error ("Assumed size array %qs at %L is not permitted " |
e1633d82 DF |
2861 | "in an initialization expression", |
2862 | e->symtree->n.sym->name, &e->where); | |
5ab0eadf | 2863 | break; |
e1633d82 DF |
2864 | |
2865 | case AS_ASSUMED_SHAPE: | |
c4100eae | 2866 | gfc_error ("Assumed shape array %qs at %L is not permitted " |
e1633d82 DF |
2867 | "in an initialization expression", |
2868 | e->symtree->n.sym->name, &e->where); | |
5ab0eadf | 2869 | break; |
e1633d82 DF |
2870 | |
2871 | case AS_DEFERRED: | |
03b2d3de SK |
2872 | if (!e->symtree->n.sym->attr.allocatable |
2873 | && !e->symtree->n.sym->attr.pointer | |
2874 | && e->symtree->n.sym->attr.dummy) | |
2875 | gfc_error ("Assumed-shape array %qs at %L is not permitted " | |
2876 | "in an initialization expression", | |
2877 | e->symtree->n.sym->name, &e->where); | |
2878 | else | |
2879 | gfc_error ("Deferred array %qs at %L is not permitted " | |
2880 | "in an initialization expression", | |
2881 | e->symtree->n.sym->name, &e->where); | |
5ab0eadf | 2882 | break; |
e1633d82 | 2883 | |
106dbde4 | 2884 | case AS_EXPLICIT: |
c4100eae | 2885 | gfc_error ("Array %qs at %L is a variable, which does " |
106dbde4 DF |
2886 | "not reduce to a constant expression", |
2887 | e->symtree->n.sym->name, &e->where); | |
2888 | break; | |
2889 | ||
e1633d82 DF |
2890 | default: |
2891 | gcc_unreachable(); | |
2892 | } | |
2893 | } | |
2894 | else | |
c4100eae | 2895 | gfc_error ("Parameter %qs at %L has not been declared or is " |
e1633d82 | 2896 | "a variable, which does not reduce to a constant " |
f9c5fe06 | 2897 | "expression", e->symtree->name, &e->where); |
e1633d82 | 2898 | |
6de9cd9a DN |
2899 | break; |
2900 | ||
2901 | case EXPR_CONSTANT: | |
2902 | case EXPR_NULL: | |
524af0d6 | 2903 | t = true; |
6de9cd9a DN |
2904 | break; |
2905 | ||
2906 | case EXPR_SUBSTRING: | |
31088369 SK |
2907 | if (e->ref) |
2908 | { | |
2909 | t = gfc_check_init_expr (e->ref->u.ss.start); | |
2910 | if (!t) | |
2911 | break; | |
6de9cd9a | 2912 | |
31088369 SK |
2913 | t = gfc_check_init_expr (e->ref->u.ss.end); |
2914 | if (t) | |
2915 | t = gfc_simplify_expr (e, 0); | |
2916 | } | |
2917 | else | |
2918 | t = false; | |
6de9cd9a DN |
2919 | break; |
2920 | ||
2921 | case EXPR_STRUCTURE: | |
524af0d6 JB |
2922 | t = e->ts.is_iso_c ? true : false; |
2923 | if (t) | |
604df116 DF |
2924 | break; |
2925 | ||
2926 | t = check_alloc_comp_init (e); | |
524af0d6 | 2927 | if (!t) |
604df116 DF |
2928 | break; |
2929 | ||
7ac6a832 | 2930 | t = gfc_check_constructor (e, gfc_check_init_expr); |
524af0d6 | 2931 | if (!t) |
604df116 DF |
2932 | break; |
2933 | ||
6de9cd9a DN |
2934 | break; |
2935 | ||
2936 | case EXPR_ARRAY: | |
7ac6a832 | 2937 | t = gfc_check_constructor (e, gfc_check_init_expr); |
524af0d6 | 2938 | if (!t) |
6de9cd9a DN |
2939 | break; |
2940 | ||
928f0490 | 2941 | t = gfc_expand_constructor (e, true); |
524af0d6 | 2942 | if (!t) |
6de9cd9a DN |
2943 | break; |
2944 | ||
2945 | t = gfc_check_constructor_type (e); | |
2946 | break; | |
2947 | ||
2948 | default: | |
2949 | gfc_internal_error ("check_init_expr(): Unknown expression type"); | |
2950 | } | |
2951 | ||
2952 | return t; | |
2953 | } | |
2954 | ||
d3d0b9e0 MM |
2955 | /* Reduces a general expression to an initialization expression (a constant). |
2956 | This used to be part of gfc_match_init_expr. | |
524af0d6 | 2957 | Note that this function doesn't free the given expression on false. */ |
6de9cd9a | 2958 | |
524af0d6 | 2959 | bool |
d3d0b9e0 | 2960 | gfc_reduce_init_expr (gfc_expr *expr) |
6de9cd9a | 2961 | { |
524af0d6 | 2962 | bool t; |
6de9cd9a | 2963 | |
f2cbd86c | 2964 | gfc_init_expr_flag = true; |
6de9cd9a | 2965 | t = gfc_resolve_expr (expr); |
524af0d6 | 2966 | if (t) |
7ac6a832 | 2967 | t = gfc_check_init_expr (expr); |
f2cbd86c | 2968 | gfc_init_expr_flag = false; |
6de9cd9a | 2969 | |
524af0d6 JB |
2970 | if (!t) |
2971 | return false; | |
6de9cd9a | 2972 | |
f2ff577a | 2973 | if (expr->expr_type == EXPR_ARRAY) |
e7f79e12 | 2974 | { |
524af0d6 JB |
2975 | if (!gfc_check_constructor_type (expr)) |
2976 | return false; | |
2977 | if (!gfc_expand_constructor (expr, true)) | |
2978 | return false; | |
d3d0b9e0 MM |
2979 | } |
2980 | ||
524af0d6 | 2981 | return true; |
d3d0b9e0 MM |
2982 | } |
2983 | ||
2984 | ||
2985 | /* Match an initialization expression. We work by first matching an | |
f2cbd86c | 2986 | expression, then reducing it to a constant. */ |
d3d0b9e0 MM |
2987 | |
2988 | match | |
2989 | gfc_match_init_expr (gfc_expr **result) | |
2990 | { | |
2991 | gfc_expr *expr; | |
2992 | match m; | |
524af0d6 | 2993 | bool t; |
d3d0b9e0 MM |
2994 | |
2995 | expr = NULL; | |
2996 | ||
f2cbd86c | 2997 | gfc_init_expr_flag = true; |
6bb62671 | 2998 | |
d3d0b9e0 MM |
2999 | m = gfc_match_expr (&expr); |
3000 | if (m != MATCH_YES) | |
6bb62671 | 3001 | { |
f2cbd86c | 3002 | gfc_init_expr_flag = false; |
6bb62671 SK |
3003 | return m; |
3004 | } | |
d3d0b9e0 | 3005 | |
5bab4c96 PT |
3006 | if (gfc_derived_parameter_expr (expr)) |
3007 | { | |
3008 | *result = expr; | |
3009 | gfc_init_expr_flag = false; | |
3010 | return m; | |
3011 | } | |
3012 | ||
d3d0b9e0 | 3013 | t = gfc_reduce_init_expr (expr); |
524af0d6 | 3014 | if (!t) |
d3d0b9e0 MM |
3015 | { |
3016 | gfc_free_expr (expr); | |
f2cbd86c | 3017 | gfc_init_expr_flag = false; |
e7f79e12 PT |
3018 | return MATCH_ERROR; |
3019 | } | |
6de9cd9a DN |
3020 | |
3021 | *result = expr; | |
f2cbd86c | 3022 | gfc_init_expr_flag = false; |
6de9cd9a DN |
3023 | |
3024 | return MATCH_YES; | |
3025 | } | |
3026 | ||
3027 | ||
6de9cd9a DN |
3028 | /* Given an actual argument list, test to see that each argument is a |
3029 | restricted expression and optionally if the expression type is | |
3030 | integer or character. */ | |
3031 | ||
524af0d6 | 3032 | static bool |
636dff67 | 3033 | restricted_args (gfc_actual_arglist *a) |
6de9cd9a | 3034 | { |
6de9cd9a DN |
3035 | for (; a; a = a->next) |
3036 | { | |
524af0d6 JB |
3037 | if (!check_restricted (a->expr)) |
3038 | return false; | |
6de9cd9a DN |
3039 | } |
3040 | ||
524af0d6 | 3041 | return true; |
6de9cd9a DN |
3042 | } |
3043 | ||
3044 | ||
3045 | /************* Restricted/specification expressions *************/ | |
3046 | ||
3047 | ||
068b961b JW |
3048 | /* Make sure a non-intrinsic function is a specification function, |
3049 | * see F08:7.1.11.5. */ | |
6de9cd9a | 3050 | |
524af0d6 | 3051 | static bool |
636dff67 | 3052 | external_spec_function (gfc_expr *e) |
6de9cd9a DN |
3053 | { |
3054 | gfc_symbol *f; | |
3055 | ||
3056 | f = e->value.function.esym; | |
3057 | ||
0e360db9 FXC |
3058 | /* IEEE functions allowed are "a reference to a transformational function |
3059 | from the intrinsic module IEEE_ARITHMETIC or IEEE_EXCEPTIONS", and | |
3060 | "inquiry function from the intrinsic modules IEEE_ARITHMETIC and | |
3061 | IEEE_EXCEPTIONS". */ | |
3062 | if (f->from_intmod == INTMOD_IEEE_ARITHMETIC | |
3063 | || f->from_intmod == INTMOD_IEEE_EXCEPTIONS) | |
3064 | { | |
3065 | if (!strcmp (f->name, "ieee_selected_real_kind") | |
3066 | || !strcmp (f->name, "ieee_support_rounding") | |
3067 | || !strcmp (f->name, "ieee_support_flag") | |
3068 | || !strcmp (f->name, "ieee_support_halting") | |
3069 | || !strcmp (f->name, "ieee_support_datatype") | |
3070 | || !strcmp (f->name, "ieee_support_denormal") | |
ede9dea5 | 3071 | || !strcmp (f->name, "ieee_support_subnormal") |
0e360db9 FXC |
3072 | || !strcmp (f->name, "ieee_support_divide") |
3073 | || !strcmp (f->name, "ieee_support_inf") | |
3074 | || !strcmp (f->name, "ieee_support_io") | |
3075 | || !strcmp (f->name, "ieee_support_nan") | |
3076 | || !strcmp (f->name, "ieee_support_sqrt") | |
3077 | || !strcmp (f->name, "ieee_support_standard") | |
3078 | || !strcmp (f->name, "ieee_support_underflow_control")) | |
3079 | goto function_allowed; | |
3080 | } | |
3081 | ||
6de9cd9a DN |
3082 | if (f->attr.proc == PROC_ST_FUNCTION) |
3083 | { | |
c4100eae | 3084 | gfc_error ("Specification function %qs at %L cannot be a statement " |
6de9cd9a | 3085 | "function", f->name, &e->where); |
524af0d6 | 3086 | return false; |
6de9cd9a DN |
3087 | } |
3088 | ||
3089 | if (f->attr.proc == PROC_INTERNAL) | |
3090 | { | |
c4100eae | 3091 | gfc_error ("Specification function %qs at %L cannot be an internal " |
6de9cd9a | 3092 | "function", f->name, &e->where); |
524af0d6 | 3093 | return false; |
6de9cd9a DN |
3094 | } |
3095 | ||
98cb5a54 | 3096 | if (!f->attr.pure && !f->attr.elemental) |
6de9cd9a | 3097 | { |
c4100eae | 3098 | gfc_error ("Specification function %qs at %L must be PURE", f->name, |
6de9cd9a | 3099 | &e->where); |
524af0d6 | 3100 | return false; |
6de9cd9a DN |
3101 | } |
3102 | ||
b349a81a JW |
3103 | /* F08:7.1.11.6. */ |
3104 | if (f->attr.recursive | |
3105 | && !gfc_notify_std (GFC_STD_F2003, | |
2f029c08 | 3106 | "Specification function %qs " |
b349a81a | 3107 | "at %L cannot be RECURSIVE", f->name, &e->where)) |
524af0d6 | 3108 | return false; |
6de9cd9a | 3109 | |
0e360db9 | 3110 | function_allowed: |
40e929f3 | 3111 | return restricted_args (e->value.function.actual); |
6de9cd9a DN |
3112 | } |
3113 | ||
3114 | ||
3115 | /* Check to see that a function reference to an intrinsic is a | |
40e929f3 | 3116 | restricted expression. */ |
6de9cd9a | 3117 | |
524af0d6 | 3118 | static bool |
636dff67 | 3119 | restricted_intrinsic (gfc_expr *e) |
6de9cd9a | 3120 | { |
40e929f3 | 3121 | /* TODO: Check constraints on inquiry functions. 7.1.6.2 (7). */ |
e1633d82 | 3122 | if (check_inquiry (e, 0) == MATCH_YES) |
524af0d6 | 3123 | return true; |
6de9cd9a | 3124 | |
40e929f3 | 3125 | return restricted_args (e->value.function.actual); |
6de9cd9a DN |
3126 | } |
3127 | ||
3128 | ||
a3d3c0f5 DK |
3129 | /* Check the expressions of an actual arglist. Used by check_restricted. */ |
3130 | ||
524af0d6 JB |
3131 | static bool |
3132 | check_arglist (gfc_actual_arglist* arg, bool (*checker) (gfc_expr*)) | |
a3d3c0f5 DK |
3133 | { |
3134 | for (; arg; arg = arg->next) | |
524af0d6 JB |
3135 | if (!checker (arg->expr)) |
3136 | return false; | |
a3d3c0f5 | 3137 | |
524af0d6 | 3138 | return true; |
a3d3c0f5 DK |
3139 | } |
3140 | ||
3141 | ||
3142 | /* Check the subscription expressions of a reference chain with a checking | |
3143 | function; used by check_restricted. */ | |
3144 | ||
524af0d6 JB |
3145 | static bool |
3146 | check_references (gfc_ref* ref, bool (*checker) (gfc_expr*)) | |
a3d3c0f5 DK |
3147 | { |
3148 | int dim; | |
3149 | ||
3150 | if (!ref) | |
524af0d6 | 3151 | return true; |
a3d3c0f5 DK |
3152 | |
3153 | switch (ref->type) | |
3154 | { | |
3155 | case REF_ARRAY: | |
3156 | for (dim = 0; dim != ref->u.ar.dimen; ++dim) | |
3157 | { | |
524af0d6 JB |
3158 | if (!checker (ref->u.ar.start[dim])) |
3159 | return false; | |
3160 | if (!checker (ref->u.ar.end[dim])) | |
3161 | return false; | |
3162 | if (!checker (ref->u.ar.stride[dim])) | |
3163 | return false; | |
a3d3c0f5 DK |
3164 | } |
3165 | break; | |
3166 | ||
3167 | case REF_COMPONENT: | |
3168 | /* Nothing needed, just proceed to next reference. */ | |
3169 | break; | |
3170 | ||
3171 | case REF_SUBSTRING: | |
524af0d6 JB |
3172 | if (!checker (ref->u.ss.start)) |
3173 | return false; | |
3174 | if (!checker (ref->u.ss.end)) | |
3175 | return false; | |
a3d3c0f5 DK |
3176 | break; |
3177 | ||
3178 | default: | |
3179 | gcc_unreachable (); | |
3180 | break; | |
3181 | } | |
3182 | ||
3183 | return check_references (ref->next, checker); | |
3184 | } | |
3185 | ||
1aae3f05 TK |
3186 | /* Return true if ns is a parent of the current ns. */ |
3187 | ||
3188 | static bool | |
3189 | is_parent_of_current_ns (gfc_namespace *ns) | |
3190 | { | |
3191 | gfc_namespace *p; | |
3192 | for (p = gfc_current_ns->parent; p; p = p->parent) | |
3193 | if (ns == p) | |
3194 | return true; | |
3195 | ||
3196 | return false; | |
3197 | } | |
a3d3c0f5 | 3198 | |
6de9cd9a DN |
3199 | /* Verify that an expression is a restricted expression. Like its |
3200 | cousin check_init_expr(), an error message is generated if we | |
524af0d6 | 3201 | return false. */ |
6de9cd9a | 3202 | |
524af0d6 | 3203 | static bool |
636dff67 | 3204 | check_restricted (gfc_expr *e) |
6de9cd9a | 3205 | { |
a3d3c0f5 | 3206 | gfc_symbol* sym; |
524af0d6 | 3207 | bool t; |
6de9cd9a DN |
3208 | |
3209 | if (e == NULL) | |
524af0d6 | 3210 | return true; |
6de9cd9a DN |
3211 | |
3212 | switch (e->expr_type) | |
3213 | { | |
3214 | case EXPR_OP: | |
3215 | t = check_intrinsic_op (e, check_restricted); | |
524af0d6 | 3216 | if (t) |
6de9cd9a DN |
3217 | t = gfc_simplify_expr (e, 0); |
3218 | ||
3219 | break; | |
3220 | ||
3221 | case EXPR_FUNCTION: | |
a3d3c0f5 DK |
3222 | if (e->value.function.esym) |
3223 | { | |
3224 | t = check_arglist (e->value.function.actual, &check_restricted); | |
524af0d6 | 3225 | if (t) |
a3d3c0f5 DK |
3226 | t = external_spec_function (e); |
3227 | } | |
3228 | else | |
3229 | { | |
3230 | if (e->value.function.isym && e->value.function.isym->inquiry) | |
524af0d6 | 3231 | t = true; |
a3d3c0f5 DK |
3232 | else |
3233 | t = check_arglist (e->value.function.actual, &check_restricted); | |
3234 | ||
524af0d6 | 3235 | if (t) |
a3d3c0f5 DK |
3236 | t = restricted_intrinsic (e); |
3237 | } | |
6de9cd9a DN |
3238 | break; |
3239 | ||
3240 | case EXPR_VARIABLE: | |
3241 | sym = e->symtree->n.sym; | |
524af0d6 | 3242 | t = false; |
6de9cd9a | 3243 | |
c4d4556f TS |
3244 | /* If a dummy argument appears in a context that is valid for a |
3245 | restricted expression in an elemental procedure, it will have | |
3246 | already been simplified away once we get here. Therefore we | |
3247 | don't need to jump through hoops to distinguish valid from | |
3248 | invalid cases. */ | |
3249 | if (sym->attr.dummy && sym->ns == gfc_current_ns | |
3250 | && sym->ns->proc_name && sym->ns->proc_name->attr.elemental) | |
3251 | { | |
c4100eae | 3252 | gfc_error ("Dummy argument %qs not allowed in expression at %L", |
c4d4556f TS |
3253 | sym->name, &e->where); |
3254 | break; | |
3255 | } | |
3256 | ||
6de9cd9a DN |
3257 | if (sym->attr.optional) |
3258 | { | |
c4100eae | 3259 | gfc_error ("Dummy argument %qs at %L cannot be OPTIONAL", |
6de9cd9a DN |
3260 | sym->name, &e->where); |
3261 | break; | |
3262 | } | |
3263 | ||
3264 | if (sym->attr.intent == INTENT_OUT) | |
3265 | { | |
c4100eae | 3266 | gfc_error ("Dummy argument %qs at %L cannot be INTENT(OUT)", |
6de9cd9a DN |
3267 | sym->name, &e->where); |
3268 | break; | |
3269 | } | |
3270 | ||
a3d3c0f5 | 3271 | /* Check reference chain if any. */ |
524af0d6 | 3272 | if (!check_references (e->ref, &check_restricted)) |
a3d3c0f5 DK |
3273 | break; |
3274 | ||
636dff67 SK |
3275 | /* gfc_is_formal_arg broadcasts that a formal argument list is being |
3276 | processed in resolve.c(resolve_formal_arglist). This is done so | |
3277 | that host associated dummy array indices are accepted (PR23446). | |
3278 | This mechanism also does the same for the specification expressions | |
3279 | of array-valued functions. */ | |
ebb479cd PT |
3280 | if (e->error |
3281 | || sym->attr.in_common | |
3282 | || sym->attr.use_assoc | |
3283 | || sym->attr.dummy | |
3284 | || sym->attr.implied_index | |
a3d3c0f5 | 3285 | || sym->attr.flavor == FL_PARAMETER |
1aae3f05 | 3286 | || is_parent_of_current_ns (sym->ns) |
ebb479cd PT |
3287 | || (sym->ns->proc_name != NULL |
3288 | && sym->ns->proc_name->attr.flavor == FL_MODULE) | |
3289 | || (gfc_is_formal_arg () && (sym->ns == gfc_current_ns))) | |
6de9cd9a | 3290 | { |
524af0d6 | 3291 | t = true; |
6de9cd9a DN |
3292 | break; |
3293 | } | |
3294 | ||
c4100eae | 3295 | gfc_error ("Variable %qs cannot appear in the expression at %L", |
6de9cd9a | 3296 | sym->name, &e->where); |
ebb479cd PT |
3297 | /* Prevent a repetition of the error. */ |
3298 | e->error = 1; | |
6de9cd9a DN |
3299 | break; |
3300 | ||
3301 | case EXPR_NULL: | |
3302 | case EXPR_CONSTANT: | |
524af0d6 | 3303 | t = true; |
6de9cd9a DN |
3304 | break; |
3305 | ||
3306 | case EXPR_SUBSTRING: | |
eac33acc | 3307 | t = gfc_specification_expr (e->ref->u.ss.start); |
524af0d6 | 3308 | if (!t) |
6de9cd9a DN |
3309 | break; |
3310 | ||
eac33acc | 3311 | t = gfc_specification_expr (e->ref->u.ss.end); |
524af0d6 | 3312 | if (t) |
6de9cd9a DN |
3313 | t = gfc_simplify_expr (e, 0); |
3314 | ||
3315 | break; | |
3316 | ||
3317 | case EXPR_STRUCTURE: | |
3318 | t = gfc_check_constructor (e, check_restricted); | |
3319 | break; | |
3320 | ||
3321 | case EXPR_ARRAY: | |
3322 | t = gfc_check_constructor (e, check_restricted); | |
3323 | break; | |
3324 | ||
3325 | default: | |
3326 | gfc_internal_error ("check_restricted(): Unknown expression type"); | |
3327 | } | |
3328 | ||
3329 | return t; | |
3330 | } | |
3331 | ||
3332 | ||
3333 | /* Check to see that an expression is a specification expression. If | |
524af0d6 | 3334 | we return false, an error has been generated. */ |
6de9cd9a | 3335 | |
524af0d6 | 3336 | bool |
636dff67 | 3337 | gfc_specification_expr (gfc_expr *e) |
6de9cd9a | 3338 | { |
687ea68f | 3339 | gfc_component *comp; |
66e4ab31 | 3340 | |
110eec24 | 3341 | if (e == NULL) |
524af0d6 | 3342 | return true; |
6de9cd9a DN |
3343 | |
3344 | if (e->ts.type != BT_INTEGER) | |
3345 | { | |
acb388a0 JD |
3346 | gfc_error ("Expression at %L must be of INTEGER type, found %s", |
3347 | &e->where, gfc_basic_typename (e->ts.type)); | |
524af0d6 | 3348 | return false; |
6de9cd9a DN |
3349 | } |
3350 | ||
2a573572 | 3351 | comp = gfc_get_proc_ptr_comp (e); |
98a36c7c | 3352 | if (e->expr_type == EXPR_FUNCTION |
2a573572 MM |
3353 | && !e->value.function.isym |
3354 | && !e->value.function.esym | |
3355 | && !gfc_pure (e->symtree->n.sym) | |
3356 | && (!comp || !comp->attr.pure)) | |
98a36c7c | 3357 | { |
c4100eae | 3358 | gfc_error ("Function %qs at %L must be PURE", |
98a36c7c PT |
3359 | e->symtree->n.sym->name, &e->where); |
3360 | /* Prevent repeat error messages. */ | |
3361 | e->symtree->n.sym->attr.pure = 1; | |
524af0d6 | 3362 | return false; |
98a36c7c PT |
3363 | } |
3364 | ||
6de9cd9a DN |
3365 | if (e->rank != 0) |
3366 | { | |
3367 | gfc_error ("Expression at %L must be scalar", &e->where); | |
524af0d6 | 3368 | return false; |
6de9cd9a DN |
3369 | } |
3370 | ||
524af0d6 JB |
3371 | if (!gfc_simplify_expr (e, 0)) |
3372 | return false; | |
6de9cd9a DN |
3373 | |
3374 | return check_restricted (e); | |
3375 | } | |
3376 | ||
3377 | ||
3378 | /************** Expression conformance checks. *************/ | |
3379 | ||
3380 | /* Given two expressions, make sure that the arrays are conformable. */ | |
3381 | ||
524af0d6 | 3382 | bool |
ca8a8795 | 3383 | gfc_check_conformance (gfc_expr *op1, gfc_expr *op2, const char *optype_msgid, ...) |
6de9cd9a DN |
3384 | { |
3385 | int op1_flag, op2_flag, d; | |
3386 | mpz_t op1_size, op2_size; | |
524af0d6 | 3387 | bool t; |
6de9cd9a | 3388 | |
ca8a8795 DF |
3389 | va_list argp; |
3390 | char buffer[240]; | |
3391 | ||
6de9cd9a | 3392 | if (op1->rank == 0 || op2->rank == 0) |
524af0d6 | 3393 | return true; |
6de9cd9a | 3394 | |
ca8a8795 DF |
3395 | va_start (argp, optype_msgid); |
3396 | vsnprintf (buffer, 240, optype_msgid, argp); | |
3397 | va_end (argp); | |
3398 | ||
6de9cd9a DN |
3399 | if (op1->rank != op2->rank) |
3400 | { | |
ca8a8795 | 3401 | gfc_error ("Incompatible ranks in %s (%d and %d) at %L", _(buffer), |
3c7b91d3 | 3402 | op1->rank, op2->rank, &op1->where); |
524af0d6 | 3403 | return false; |
6de9cd9a DN |
3404 | } |
3405 | ||
524af0d6 | 3406 | t = true; |
6de9cd9a DN |
3407 | |
3408 | for (d = 0; d < op1->rank; d++) | |
3409 | { | |
524af0d6 JB |
3410 | op1_flag = gfc_array_dimen_size(op1, d, &op1_size); |
3411 | op2_flag = gfc_array_dimen_size(op2, d, &op2_size); | |
6de9cd9a DN |
3412 | |
3413 | if (op1_flag && op2_flag && mpz_cmp (op1_size, op2_size) != 0) | |
3414 | { | |
7e49f965 | 3415 | gfc_error ("Different shape for %s at %L on dimension %d " |
ca8a8795 | 3416 | "(%d and %d)", _(buffer), &op1->where, d + 1, |
31043f6c | 3417 | (int) mpz_get_si (op1_size), |
6de9cd9a DN |
3418 | (int) mpz_get_si (op2_size)); |
3419 | ||
524af0d6 | 3420 | t = false; |
6de9cd9a DN |
3421 | } |
3422 | ||
3423 | if (op1_flag) | |
3424 | mpz_clear (op1_size); | |
3425 | if (op2_flag) | |
3426 | mpz_clear (op2_size); | |
3427 | ||
524af0d6 JB |
3428 | if (!t) |
3429 | return false; | |
6de9cd9a DN |
3430 | } |
3431 | ||
524af0d6 | 3432 | return true; |
6de9cd9a DN |
3433 | } |
3434 | ||
3435 | ||
3436 | /* Given an assignable expression and an arbitrary expression, make | |
3c9f5092 AV |
3437 | sure that the assignment can take place. Only add a call to the intrinsic |
3438 | conversion routines, when allow_convert is set. When this assign is a | |
3439 | coarray call, then the convert is done by the coarray routine implictly and | |
3440 | adding the intrinsic conversion would do harm in most cases. */ | |
6de9cd9a | 3441 | |
524af0d6 | 3442 | bool |
3c9f5092 AV |
3443 | gfc_check_assign (gfc_expr *lvalue, gfc_expr *rvalue, int conform, |
3444 | bool allow_convert) | |
6de9cd9a DN |
3445 | { |
3446 | gfc_symbol *sym; | |
f17facac TB |
3447 | gfc_ref *ref; |
3448 | int has_pointer; | |
6de9cd9a DN |
3449 | |
3450 | sym = lvalue->symtree->n.sym; | |
3451 | ||
a5fbc2f3 PT |
3452 | /* See if this is the component or subcomponent of a pointer and guard |
3453 | against assignment to LEN or KIND part-refs. */ | |
f17facac | 3454 | has_pointer = sym->attr.pointer; |
f17facac | 3455 | for (ref = lvalue->ref; ref; ref = ref->next) |
a5fbc2f3 PT |
3456 | { |
3457 | if (!has_pointer && ref->type == REF_COMPONENT | |
3458 | && ref->u.c.component->attr.pointer) | |
3459 | has_pointer = 1; | |
3460 | else if (ref->type == REF_INQUIRY | |
3461 | && (ref->u.i == INQUIRY_LEN || ref->u.i == INQUIRY_KIND)) | |
3462 | { | |
3463 | gfc_error ("Assignment to a LEN or KIND part_ref at %L is not " | |
3464 | "allowed", &lvalue->where); | |
3465 | return false; | |
3466 | } | |
3467 | } | |
f17facac | 3468 | |
66e4ab31 SK |
3469 | /* 12.5.2.2, Note 12.26: The result variable is very similar to any other |
3470 | variable local to a function subprogram. Its existence begins when | |
3471 | execution of the function is initiated and ends when execution of the | |
3472 | function is terminated... | |
3473 | Therefore, the left hand side is no longer a variable, when it is: */ | |
636dff67 SK |
3474 | if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_ST_FUNCTION |
3475 | && !sym->attr.external) | |
2990f854 | 3476 | { |
f5f701ad PT |
3477 | bool bad_proc; |
3478 | bad_proc = false; | |
3479 | ||
66e4ab31 | 3480 | /* (i) Use associated; */ |
f5f701ad PT |
3481 | if (sym->attr.use_assoc) |
3482 | bad_proc = true; | |
3483 | ||
e2ae1407 | 3484 | /* (ii) The assignment is in the main program; or */ |
bfeeb145 SK |
3485 | if (gfc_current_ns->proc_name |
3486 | && gfc_current_ns->proc_name->attr.is_main_program) | |
f5f701ad PT |
3487 | bad_proc = true; |
3488 | ||
66e4ab31 | 3489 | /* (iii) A module or internal procedure... */ |
bfeeb145 SK |
3490 | if (gfc_current_ns->proc_name |
3491 | && (gfc_current_ns->proc_name->attr.proc == PROC_INTERNAL | |
3492 | || gfc_current_ns->proc_name->attr.proc == PROC_MODULE) | |
f5f701ad PT |
3493 | && gfc_current_ns->parent |
3494 | && (!(gfc_current_ns->parent->proc_name->attr.function | |
636dff67 | 3495 | || gfc_current_ns->parent->proc_name->attr.subroutine) |
f5f701ad PT |
3496 | || gfc_current_ns->parent->proc_name->attr.is_main_program)) |
3497 | { | |
8b704316 | 3498 | /* ... that is not a function... */ |
bfeeb145 SK |
3499 | if (gfc_current_ns->proc_name |
3500 | && !gfc_current_ns->proc_name->attr.function) | |
f5f701ad PT |
3501 | bad_proc = true; |
3502 | ||
66e4ab31 | 3503 | /* ... or is not an entry and has a different name. */ |
f5f701ad PT |
3504 | if (!sym->attr.entry && sym->name != gfc_current_ns->proc_name->name) |
3505 | bad_proc = true; | |
3506 | } | |
2990f854 | 3507 | |
db39d0c2 PT |
3508 | /* (iv) Host associated and not the function symbol or the |
3509 | parent result. This picks up sibling references, which | |
3510 | cannot be entries. */ | |
3511 | if (!sym->attr.entry | |
3512 | && sym->ns == gfc_current_ns->parent | |
3513 | && sym != gfc_current_ns->proc_name | |
3514 | && sym != gfc_current_ns->parent->proc_name->result) | |
3515 | bad_proc = true; | |
3516 | ||
f5f701ad PT |
3517 | if (bad_proc) |
3518 | { | |
c4100eae | 3519 | gfc_error ("%qs at %L is not a VALUE", sym->name, &lvalue->where); |
524af0d6 | 3520 | return false; |
f5f701ad PT |
3521 | } |
3522 | } | |
83fad929 TK |
3523 | else |
3524 | { | |
3525 | /* Reject assigning to an external symbol. For initializers, this | |
3526 | was already done before, in resolve_fl_procedure. */ | |
3527 | if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external | |
3528 | && sym->attr.proc != PROC_MODULE && !rvalue->error) | |
3529 | { | |
3530 | gfc_error ("Illegal assignment to external procedure at %L", | |
3531 | &lvalue->where); | |
3532 | return false; | |
3533 | } | |
3534 | } | |
2990f854 | 3535 | |
6de9cd9a DN |
3536 | if (rvalue->rank != 0 && lvalue->rank != rvalue->rank) |
3537 | { | |
7dea5a95 TS |
3538 | gfc_error ("Incompatible ranks %d and %d in assignment at %L", |
3539 | lvalue->rank, rvalue->rank, &lvalue->where); | |
524af0d6 | 3540 | return false; |
6de9cd9a DN |
3541 | } |
3542 | ||
3543 | if (lvalue->ts.type == BT_UNKNOWN) | |
3544 | { | |
3545 | gfc_error ("Variable type is UNKNOWN in assignment at %L", | |
3546 | &lvalue->where); | |
524af0d6 | 3547 | return false; |
6de9cd9a DN |
3548 | } |
3549 | ||
37775e79 | 3550 | if (rvalue->expr_type == EXPR_NULL) |
8b704316 | 3551 | { |
e49be8f7 | 3552 | if (has_pointer && (ref == NULL || ref->next == NULL) |
37775e79 | 3553 | && lvalue->symtree->n.sym->attr.data) |
524af0d6 | 3554 | return true; |
37775e79 JD |
3555 | else |
3556 | { | |
3557 | gfc_error ("NULL appears on right-hand side in assignment at %L", | |
3558 | &rvalue->where); | |
524af0d6 | 3559 | return false; |
37775e79 JD |
3560 | } |
3561 | } | |
7dea5a95 | 3562 | |
66e4ab31 | 3563 | /* This is possibly a typo: x = f() instead of x => f(). */ |
73e42eef | 3564 | if (warn_surprising |
8988cde6 | 3565 | && rvalue->expr_type == EXPR_FUNCTION && gfc_expr_attr (rvalue).pointer) |
48749dbc MLI |
3566 | gfc_warning (OPT_Wsurprising, |
3567 | "POINTER-valued function appears on right-hand side of " | |
6d1c50cc TS |
3568 | "assignment at %L", &rvalue->where); |
3569 | ||
6de9cd9a DN |
3570 | /* Check size of array assignments. */ |
3571 | if (lvalue->rank != 0 && rvalue->rank != 0 | |
524af0d6 JB |
3572 | && !gfc_check_conformance (lvalue, rvalue, "array assignment")) |
3573 | return false; | |
6de9cd9a | 3574 | |
00a4618b TB |
3575 | if (rvalue->is_boz && lvalue->ts.type != BT_INTEGER |
3576 | && lvalue->symtree->n.sym->attr.data | |
524af0d6 | 3577 | && !gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L used to " |
bf9f15ee | 3578 | "initialize non-integer variable %qs", |
524af0d6 JB |
3579 | &rvalue->where, lvalue->symtree->n.sym->name)) |
3580 | return false; | |
00a4618b | 3581 | else if (rvalue->is_boz && !lvalue->symtree->n.sym->attr.data |
524af0d6 JB |
3582 | && !gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L outside " |
3583 | "a DATA statement and outside INT/REAL/DBLE/CMPLX", | |
3584 | &rvalue->where)) | |
3585 | return false; | |
00a4618b TB |
3586 | |
3587 | /* Handle the case of a BOZ literal on the RHS. */ | |
3588 | if (rvalue->is_boz && lvalue->ts.type != BT_INTEGER) | |
3589 | { | |
4956b1f1 | 3590 | int rc; |
73e42eef | 3591 | if (warn_surprising) |
48749dbc MLI |
3592 | gfc_warning (OPT_Wsurprising, |
3593 | "BOZ literal at %L is bitwise transferred " | |
3594 | "non-integer symbol %qs", &rvalue->where, | |
3595 | lvalue->symtree->n.sym->name); | |
c7abc45c | 3596 | if (!gfc_convert_boz (rvalue, &lvalue->ts)) |
524af0d6 | 3597 | return false; |
4956b1f1 TB |
3598 | if ((rc = gfc_range_check (rvalue)) != ARITH_OK) |
3599 | { | |
3600 | if (rc == ARITH_UNDERFLOW) | |
3601 | gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L" | |
3602 | ". This check can be disabled with the option " | |
a4d9b221 | 3603 | "%<-fno-range-check%>", &rvalue->where); |
4956b1f1 TB |
3604 | else if (rc == ARITH_OVERFLOW) |
3605 | gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L" | |
3606 | ". This check can be disabled with the option " | |
a4d9b221 | 3607 | "%<-fno-range-check%>", &rvalue->where); |
4956b1f1 TB |
3608 | else if (rc == ARITH_NAN) |
3609 | gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L" | |
3610 | ". This check can be disabled with the option " | |
a4d9b221 | 3611 | "%<-fno-range-check%>", &rvalue->where); |
524af0d6 | 3612 | return false; |
4956b1f1 | 3613 | } |
00a4618b TB |
3614 | } |
3615 | ||
5bab4c96 PT |
3616 | if (gfc_expr_attr (lvalue).pdt_kind || gfc_expr_attr (lvalue).pdt_len) |
3617 | { | |
3618 | gfc_error ("The assignment to a KIND or LEN component of a " | |
3619 | "parameterized type at %L is not allowed", | |
3620 | &lvalue->where); | |
3621 | return false; | |
3622 | } | |
3623 | ||
6de9cd9a | 3624 | if (gfc_compare_types (&lvalue->ts, &rvalue->ts)) |
524af0d6 | 3625 | return true; |
6de9cd9a | 3626 | |
c4e3543d | 3627 | /* Only DATA Statements come here. */ |
6de9cd9a DN |
3628 | if (!conform) |
3629 | { | |
47feeb36 SK |
3630 | locus *where; |
3631 | ||
d3642f89 FW |
3632 | /* Numeric can be converted to any other numeric. And Hollerith can be |
3633 | converted to any other type. */ | |
3634 | if ((gfc_numeric_ts (&lvalue->ts) && gfc_numeric_ts (&rvalue->ts)) | |
3635 | || rvalue->ts.type == BT_HOLLERITH) | |
524af0d6 | 3636 | return true; |
6de9cd9a | 3637 | |
f240b896 | 3638 | if (lvalue->ts.type == BT_LOGICAL && rvalue->ts.type == BT_LOGICAL) |
524af0d6 | 3639 | return true; |
f240b896 | 3640 | |
47feeb36 | 3641 | where = lvalue->where.lb ? &lvalue->where : &rvalue->where; |
c4e3543d | 3642 | gfc_error ("Incompatible types in DATA statement at %L; attempted " |
47feeb36 | 3643 | "conversion of %s to %s", where, |
c4e3543d | 3644 | gfc_typename (&rvalue->ts), gfc_typename (&lvalue->ts)); |
6de9cd9a | 3645 | |
524af0d6 | 3646 | return false; |
6de9cd9a DN |
3647 | } |
3648 | ||
d393bbd7 FXC |
3649 | /* Assignment is the only case where character variables of different |
3650 | kind values can be converted into one another. */ | |
3651 | if (lvalue->ts.type == BT_CHARACTER && rvalue->ts.type == BT_CHARACTER) | |
3652 | { | |
3c9f5092 | 3653 | if (lvalue->ts.kind != rvalue->ts.kind && allow_convert) |
6ed022af JW |
3654 | return gfc_convert_chartype (rvalue, &lvalue->ts); |
3655 | else | |
3656 | return true; | |
d393bbd7 FXC |
3657 | } |
3658 | ||
3c9f5092 AV |
3659 | if (!allow_convert) |
3660 | return true; | |
3661 | ||
6de9cd9a DN |
3662 | return gfc_convert_type (rvalue, &lvalue->ts, 1); |
3663 | } | |
3664 | ||
3665 | ||
3666 | /* Check that a pointer assignment is OK. We first check lvalue, and | |
3667 | we only check rvalue if it's not an assignment to NULL() or a | |
3668 | NULLIFY statement. */ | |
3669 | ||
524af0d6 | 3670 | bool |
83fad929 TK |
3671 | gfc_check_pointer_assign (gfc_expr *lvalue, gfc_expr *rvalue, |
3672 | bool suppress_type_test) | |
6de9cd9a | 3673 | { |
e35e87dc | 3674 | symbol_attribute attr, lhs_attr; |
f17facac | 3675 | gfc_ref *ref; |
f1f39033 | 3676 | bool is_pure, is_implicit_pure, rank_remap; |
8c91ab34 | 3677 | int proc_pointer; |
6de9cd9a | 3678 | |
e35e87dc TB |
3679 | lhs_attr = gfc_expr_attr (lvalue); |
3680 | if (lvalue->ts.type == BT_UNKNOWN && !lhs_attr.proc_pointer) | |
6de9cd9a DN |
3681 | { |
3682 | gfc_error ("Pointer assignment target is not a POINTER at %L", | |
3683 | &lvalue->where); | |
524af0d6 | 3684 | return false; |
6de9cd9a DN |
3685 | } |
3686 | ||
e35e87dc TB |
3687 | if (lhs_attr.flavor == FL_PROCEDURE && lhs_attr.use_assoc |
3688 | && !lhs_attr.proc_pointer) | |
2990f854 | 3689 | { |
c4100eae | 3690 | gfc_error ("%qs in the pointer assignment at %L cannot be an " |
2990f854 PT |
3691 | "l-value since it is a procedure", |
3692 | lvalue->symtree->n.sym->name, &lvalue->where); | |
524af0d6 | 3693 | return false; |
2990f854 PT |
3694 | } |
3695 | ||
713485cc | 3696 | proc_pointer = lvalue->symtree->n.sym->attr.proc_pointer; |
f17facac | 3697 | |
99d821c0 | 3698 | rank_remap = false; |
f17facac TB |
3699 | for (ref = lvalue->ref; ref; ref = ref->next) |
3700 | { | |
6596e2fe | 3701 | if (ref->type == REF_COMPONENT) |
8c91ab34 | 3702 | proc_pointer = ref->u.c.component->attr.proc_pointer; |
54799fcd TB |
3703 | |
3704 | if (ref->type == REF_ARRAY && ref->next == NULL) | |
3705 | { | |
99d821c0 DK |
3706 | int dim; |
3707 | ||
54799fcd TB |
3708 | if (ref->u.ar.type == AR_FULL) |
3709 | break; | |
3710 | ||
3711 | if (ref->u.ar.type != AR_SECTION) | |
3712 | { | |
c4100eae | 3713 | gfc_error ("Expected bounds specification for %qs at %L", |
54799fcd | 3714 | lvalue->symtree->n.sym->name, &lvalue->where); |
524af0d6 | 3715 | return false; |
54799fcd TB |
3716 | } |
3717 | ||
524af0d6 | 3718 | if (!gfc_notify_std (GFC_STD_F2003, "Bounds specification " |
bf9f15ee | 3719 | "for %qs in pointer assignment at %L", |
524af0d6 JB |
3720 | lvalue->symtree->n.sym->name, &lvalue->where)) |
3721 | return false; | |
54799fcd | 3722 | |
99d821c0 DK |
3723 | /* When bounds are given, all lbounds are necessary and either all |
3724 | or none of the upper bounds; no strides are allowed. If the | |
3725 | upper bounds are present, we may do rank remapping. */ | |
3726 | for (dim = 0; dim < ref->u.ar.dimen; ++dim) | |
3727 | { | |
73cd74f8 TB |
3728 | if (!ref->u.ar.start[dim] |
3729 | || ref->u.ar.dimen_type[dim] != DIMEN_RANGE) | |
99d821c0 DK |
3730 | { |
3731 | gfc_error ("Lower bound has to be present at %L", | |
3732 | &lvalue->where); | |
524af0d6 | 3733 | return false; |
99d821c0 DK |
3734 | } |
3735 | if (ref->u.ar.stride[dim]) | |
3736 | { | |
3737 | gfc_error ("Stride must not be present at %L", | |
3738 | &lvalue->where); | |
524af0d6 | 3739 | return false; |
99d821c0 DK |
3740 | } |
3741 | ||
3742 | if (dim == 0) | |
3743 | rank_remap = (ref->u.ar.end[dim] != NULL); | |
3744 | else | |
3745 | { | |
3746 | if ((rank_remap && !ref->u.ar.end[dim]) | |
3747 | || (!rank_remap && ref->u.ar.end[dim])) | |
3748 | { | |
3749 | gfc_error ("Either all or none of the upper bounds" | |
3750 | " must be specified at %L", &lvalue->where); | |
524af0d6 | 3751 | return false; |
99d821c0 DK |
3752 | } |
3753 | } | |
3754 | } | |
54799fcd | 3755 | } |
f17facac TB |
3756 | } |
3757 | ||
6de9cd9a | 3758 | is_pure = gfc_pure (NULL); |
f1f39033 | 3759 | is_implicit_pure = gfc_implicit_pure (NULL); |
6de9cd9a | 3760 | |
6de9cd9a DN |
3761 | /* If rvalue is a NULL() or NULLIFY, we're done. Otherwise the type, |
3762 | kind, etc for lvalue and rvalue must match, and rvalue must be a | |
3763 | pure variable if we're in a pure function. */ | |
def66134 | 3764 | if (rvalue->expr_type == EXPR_NULL && rvalue->ts.type == BT_UNKNOWN) |
524af0d6 | 3765 | return true; |
7d76d73a | 3766 | |
d3a9eea2 TB |
3767 | /* F2008, C723 (pointer) and C726 (proc-pointer); for PURE also C1283. */ |
3768 | if (lvalue->expr_type == EXPR_VARIABLE | |
3769 | && gfc_is_coindexed (lvalue)) | |
3770 | { | |
3771 | gfc_ref *ref; | |
3772 | for (ref = lvalue->ref; ref; ref = ref->next) | |
3773 | if (ref->type == REF_ARRAY && ref->u.ar.codimen) | |
3774 | { | |
3775 | gfc_error ("Pointer object at %L shall not have a coindex", | |
3776 | &lvalue->where); | |
524af0d6 | 3777 | return false; |
d3a9eea2 TB |
3778 | } |
3779 | } | |
3780 | ||
726d8566 | 3781 | /* Checks on rvalue for procedure pointer assignments. */ |
713485cc | 3782 | if (proc_pointer) |
726d8566 | 3783 | { |
8ad15a0a | 3784 | char err[200]; |
889dc035 | 3785 | gfc_symbol *s1,*s2; |
eba5aec8 | 3786 | gfc_component *comp1, *comp2; |
889dc035 JW |
3787 | const char *name; |
3788 | ||
726d8566 JW |
3789 | attr = gfc_expr_attr (rvalue); |
3790 | if (!((rvalue->expr_type == EXPR_NULL) | |
3791 | || (rvalue->expr_type == EXPR_FUNCTION && attr.proc_pointer) | |
713485cc | 3792 | || (rvalue->expr_type == EXPR_VARIABLE && attr.proc_pointer) |
726d8566 JW |
3793 | || (rvalue->expr_type == EXPR_VARIABLE |
3794 | && attr.flavor == FL_PROCEDURE))) | |
3795 | { | |
3796 | gfc_error ("Invalid procedure pointer assignment at %L", | |
3797 | &rvalue->where); | |
524af0d6 | 3798 | return false; |
726d8566 | 3799 | } |
83fad929 | 3800 | |
2dda89a8 JW |
3801 | if (rvalue->expr_type == EXPR_VARIABLE && !attr.proc_pointer) |
3802 | { | |
3803 | /* Check for intrinsics. */ | |
3804 | gfc_symbol *sym = rvalue->symtree->n.sym; | |
3805 | if (!sym->attr.intrinsic | |
2dda89a8 JW |
3806 | && (gfc_is_intrinsic (sym, 0, sym->declared_at) |
3807 | || gfc_is_intrinsic (sym, 1, sym->declared_at))) | |
3808 | { | |
3809 | sym->attr.intrinsic = 1; | |
3810 | gfc_resolve_intrinsic (sym, &rvalue->where); | |
3811 | attr = gfc_expr_attr (rvalue); | |
3812 | } | |
37bfd49f | 3813 | /* Check for result of embracing function. */ |
cadddfdd | 3814 | if (sym->attr.function && sym->result == sym) |
37bfd49f | 3815 | { |
cadddfdd TB |
3816 | gfc_namespace *ns; |
3817 | ||
3818 | for (ns = gfc_current_ns; ns; ns = ns->parent) | |
3819 | if (sym == ns->proc_name) | |
3820 | { | |
c4100eae | 3821 | gfc_error ("Function result %qs is invalid as proc-target " |
cadddfdd TB |
3822 | "in procedure pointer assignment at %L", |
3823 | sym->name, &rvalue->where); | |
524af0d6 | 3824 | return false; |
cadddfdd | 3825 | } |
37bfd49f | 3826 | } |
2dda89a8 | 3827 | } |
fb7ca5a7 JW |
3828 | if (attr.abstract) |
3829 | { | |
c4100eae | 3830 | gfc_error ("Abstract interface %qs is invalid " |
fb7ca5a7 JW |
3831 | "in procedure pointer assignment at %L", |
3832 | rvalue->symtree->name, &rvalue->where); | |
524af0d6 | 3833 | return false; |
fb7ca5a7 | 3834 | } |
58c1ae36 | 3835 | /* Check for F08:C729. */ |
210aee68 JW |
3836 | if (attr.flavor == FL_PROCEDURE) |
3837 | { | |
3838 | if (attr.proc == PROC_ST_FUNCTION) | |
3839 | { | |
c4100eae | 3840 | gfc_error ("Statement function %qs is invalid " |
210aee68 JW |
3841 | "in procedure pointer assignment at %L", |
3842 | rvalue->symtree->name, &rvalue->where); | |
524af0d6 | 3843 | return false; |
210aee68 JW |
3844 | } |
3845 | if (attr.proc == PROC_INTERNAL && | |
a4d9b221 | 3846 | !gfc_notify_std(GFC_STD_F2008, "Internal procedure %qs " |
524af0d6 JB |
3847 | "is invalid in procedure pointer assignment " |
3848 | "at %L", rvalue->symtree->name, &rvalue->where)) | |
3849 | return false; | |
2dda89a8 JW |
3850 | if (attr.intrinsic && gfc_intrinsic_actual_ok (rvalue->symtree->name, |
3851 | attr.subroutine) == 0) | |
3852 | { | |
c4100eae | 3853 | gfc_error ("Intrinsic %qs at %L is invalid in procedure pointer " |
2dda89a8 | 3854 | "assignment", rvalue->symtree->name, &rvalue->where); |
524af0d6 | 3855 | return false; |
2dda89a8 | 3856 | } |
210aee68 | 3857 | } |
58c1ae36 JW |
3858 | /* Check for F08:C730. */ |
3859 | if (attr.elemental && !attr.intrinsic) | |
3860 | { | |
c4100eae | 3861 | gfc_error ("Nonintrinsic elemental procedure %qs is invalid " |
62732c30 | 3862 | "in procedure pointer assignment at %L", |
58c1ae36 | 3863 | rvalue->symtree->name, &rvalue->where); |
524af0d6 | 3864 | return false; |
58c1ae36 | 3865 | } |
08a6b8e0 TB |
3866 | |
3867 | /* Ensure that the calling convention is the same. As other attributes | |
3868 | such as DLLEXPORT may differ, one explicitly only tests for the | |
3869 | calling conventions. */ | |
3870 | if (rvalue->expr_type == EXPR_VARIABLE | |
3871 | && lvalue->symtree->n.sym->attr.ext_attr | |
3872 | != rvalue->symtree->n.sym->attr.ext_attr) | |
3873 | { | |
c0e18b82 | 3874 | symbol_attribute calls; |
08a6b8e0 | 3875 | |
c0e18b82 TB |
3876 | calls.ext_attr = 0; |
3877 | gfc_add_ext_attribute (&calls, EXT_ATTR_CDECL, NULL); | |
3878 | gfc_add_ext_attribute (&calls, EXT_ATTR_STDCALL, NULL); | |
3879 | gfc_add_ext_attribute (&calls, EXT_ATTR_FASTCALL, NULL); | |
08a6b8e0 | 3880 | |
c0e18b82 TB |
3881 | if ((calls.ext_attr & lvalue->symtree->n.sym->attr.ext_attr) |
3882 | != (calls.ext_attr & rvalue->symtree->n.sym->attr.ext_attr)) | |
08a6b8e0 TB |
3883 | { |
3884 | gfc_error ("Mismatch in the procedure pointer assignment " | |
3885 | "at %L: mismatch in the calling convention", | |
3886 | &rvalue->where); | |
524af0d6 | 3887 | return false; |
08a6b8e0 TB |
3888 | } |
3889 | } | |
3890 | ||
eba5aec8 JW |
3891 | comp1 = gfc_get_proc_ptr_comp (lvalue); |
3892 | if (comp1) | |
3893 | s1 = comp1->ts.interface; | |
889dc035 | 3894 | else |
899d52c6 PT |
3895 | { |
3896 | s1 = lvalue->symtree->n.sym; | |
3897 | if (s1->ts.interface) | |
3898 | s1 = s1->ts.interface; | |
3899 | } | |
889dc035 | 3900 | |
eba5aec8 JW |
3901 | comp2 = gfc_get_proc_ptr_comp (rvalue); |
3902 | if (comp2) | |
889dc035 | 3903 | { |
a4a76e52 JW |
3904 | if (rvalue->expr_type == EXPR_FUNCTION) |
3905 | { | |
eba5aec8 | 3906 | s2 = comp2->ts.interface->result; |
899d52c6 | 3907 | name = s2->name; |
a4a76e52 JW |
3908 | } |
3909 | else | |
3910 | { | |
eba5aec8 JW |
3911 | s2 = comp2->ts.interface; |
3912 | name = comp2->name; | |
a4a76e52 | 3913 | } |
889dc035 JW |
3914 | } |
3915 | else if (rvalue->expr_type == EXPR_FUNCTION) | |
3916 | { | |
bafa0782 JW |
3917 | if (rvalue->value.function.esym) |
3918 | s2 = rvalue->value.function.esym->result; | |
3919 | else | |
3920 | s2 = rvalue->symtree->n.sym->result; | |
3921 | ||
899d52c6 | 3922 | name = s2->name; |
889dc035 JW |
3923 | } |
3924 | else | |
3925 | { | |
3926 | s2 = rvalue->symtree->n.sym; | |
899d52c6 PT |
3927 | name = s2->name; |
3928 | } | |
3929 | ||
feb6eab0 | 3930 | if (s2 && s2->attr.proc_pointer && s2->ts.interface) |
899d52c6 PT |
3931 | s2 = s2->ts.interface; |
3932 | ||
eba5aec8 JW |
3933 | /* Special check for the case of absent interface on the lvalue. |
3934 | * All other interface checks are done below. */ | |
3935 | if (!s1 && comp1 && comp1->attr.subroutine && s2 && s2->attr.function) | |
3936 | { | |
3937 | gfc_error ("Interface mismatch in procedure pointer assignment " | |
2f029c08 | 3938 | "at %L: %qs is not a subroutine", &rvalue->where, name); |
eba5aec8 JW |
3939 | return false; |
3940 | } | |
3941 | ||
96486998 | 3942 | /* F08:7.2.2.4 (4) */ |
99827b5c | 3943 | if (s2 && gfc_explicit_interface_required (s2, err, sizeof(err))) |
96486998 | 3944 | { |
99827b5c JW |
3945 | if (comp1 && !s1) |
3946 | { | |
3947 | gfc_error ("Explicit interface required for component %qs at %L: %s", | |
3948 | comp1->name, &lvalue->where, err); | |
3949 | return false; | |
3950 | } | |
3951 | else if (s1->attr.if_source == IFSRC_UNKNOWN) | |
3952 | { | |
3953 | gfc_error ("Explicit interface required for %qs at %L: %s", | |
3954 | s1->name, &lvalue->where, err); | |
3955 | return false; | |
3956 | } | |
96486998 | 3957 | } |
99827b5c | 3958 | if (s1 && gfc_explicit_interface_required (s1, err, sizeof(err))) |
96486998 | 3959 | { |
99827b5c JW |
3960 | if (comp2 && !s2) |
3961 | { | |
3962 | gfc_error ("Explicit interface required for component %qs at %L: %s", | |
3963 | comp2->name, &rvalue->where, err); | |
3964 | return false; | |
3965 | } | |
3966 | else if (s2->attr.if_source == IFSRC_UNKNOWN) | |
3967 | { | |
3968 | gfc_error ("Explicit interface required for %qs at %L: %s", | |
3969 | s2->name, &rvalue->where, err); | |
3970 | return false; | |
3971 | } | |
96486998 JW |
3972 | } |
3973 | ||
99827b5c JW |
3974 | if (s1 == s2 || !s1 || !s2) |
3975 | return true; | |
3976 | ||
899d52c6 PT |
3977 | if (!gfc_compare_interfaces (s1, s2, name, 0, 1, |
3978 | err, sizeof(err), NULL, NULL)) | |
3979 | { | |
3980 | gfc_error ("Interface mismatch in procedure pointer assignment " | |
3981 | "at %L: %s", &rvalue->where, err); | |
524af0d6 | 3982 | return false; |
889dc035 JW |
3983 | } |
3984 | ||
ab1668f6 TB |
3985 | /* Check F2008Cor2, C729. */ |
3986 | if (!s2->attr.intrinsic && s2->attr.if_source == IFSRC_UNKNOWN | |
3987 | && !s2->attr.external && !s2->attr.subroutine && !s2->attr.function) | |
3988 | { | |
c4100eae | 3989 | gfc_error ("Procedure pointer target %qs at %L must be either an " |
ab1668f6 TB |
3990 | "intrinsic, host or use associated, referenced or have " |
3991 | "the EXTERNAL attribute", s2->name, &rvalue->where); | |
3992 | return false; | |
3993 | } | |
3994 | ||
524af0d6 | 3995 | return true; |
726d8566 | 3996 | } |
83fad929 TK |
3997 | else |
3998 | { | |
3999 | /* A non-proc pointer cannot point to a constant. */ | |
4000 | if (rvalue->expr_type == EXPR_CONSTANT) | |
4001 | { | |
4002 | gfc_error_now ("Pointer assignment target cannot be a constant at %L", | |
4003 | &rvalue->where); | |
4004 | return false; | |
4005 | } | |
4006 | } | |
8fb74da4 | 4007 | |
93d76687 | 4008 | if (!gfc_compare_types (&lvalue->ts, &rvalue->ts)) |
6de9cd9a | 4009 | { |
8b704316 PT |
4010 | /* Check for F03:C717. */ |
4011 | if (UNLIMITED_POLY (rvalue) | |
4012 | && !(UNLIMITED_POLY (lvalue) | |
4013 | || (lvalue->ts.type == BT_DERIVED | |
4014 | && (lvalue->ts.u.derived->attr.is_bind_c | |
4015 | || lvalue->ts.u.derived->attr.sequence)))) | |
83be3fe5 DH |
4016 | gfc_error ("Data-pointer-object at %L must be unlimited " |
4017 | "polymorphic, or of a type with the BIND or SEQUENCE " | |
4018 | "attribute, to be compatible with an unlimited " | |
4019 | "polymorphic target", &lvalue->where); | |
83fad929 | 4020 | else if (!suppress_type_test) |
8b704316 PT |
4021 | gfc_error ("Different types in pointer assignment at %L; " |
4022 | "attempted assignment of %s to %s", &lvalue->where, | |
4023 | gfc_typename (&rvalue->ts), | |
4024 | gfc_typename (&lvalue->ts)); | |
524af0d6 | 4025 | return false; |
7d76d73a | 4026 | } |
6de9cd9a | 4027 | |
cf2b3c22 | 4028 | if (lvalue->ts.type != BT_CLASS && lvalue->ts.kind != rvalue->ts.kind) |
7d76d73a | 4029 | { |
31043f6c | 4030 | gfc_error ("Different kind type parameters in pointer " |
7d76d73a | 4031 | "assignment at %L", &lvalue->where); |
524af0d6 | 4032 | return false; |
7d76d73a | 4033 | } |
6de9cd9a | 4034 | |
99d821c0 | 4035 | if (lvalue->rank != rvalue->rank && !rank_remap) |
def66134 | 4036 | { |
99d821c0 | 4037 | gfc_error ("Different ranks in pointer assignment at %L", &lvalue->where); |
524af0d6 | 4038 | return false; |
def66134 SK |
4039 | } |
4040 | ||
7289d1c9 JW |
4041 | /* Make sure the vtab is present. */ |
4042 | if (lvalue->ts.type == BT_CLASS && !UNLIMITED_POLY (rvalue)) | |
4043 | gfc_find_vtab (&rvalue->ts); | |
611c64f0 | 4044 | |
99d821c0 DK |
4045 | /* Check rank remapping. */ |
4046 | if (rank_remap) | |
4047 | { | |
4048 | mpz_t lsize, rsize; | |
4049 | ||
4050 | /* If this can be determined, check that the target must be at least as | |
4051 | large as the pointer assigned to it is. */ | |
524af0d6 JB |
4052 | if (gfc_array_size (lvalue, &lsize) |
4053 | && gfc_array_size (rvalue, &rsize) | |
99d821c0 DK |
4054 | && mpz_cmp (rsize, lsize) < 0) |
4055 | { | |
4056 | gfc_error ("Rank remapping target is smaller than size of the" | |
4057 | " pointer (%ld < %ld) at %L", | |
4058 | mpz_get_si (rsize), mpz_get_si (lsize), | |
4059 | &lvalue->where); | |
524af0d6 | 4060 | return false; |
99d821c0 DK |
4061 | } |
4062 | ||
4063 | /* The target must be either rank one or it must be simply contiguous | |
4064 | and F2008 must be allowed. */ | |
4065 | if (rvalue->rank != 1) | |
4066 | { | |
460263d0 | 4067 | if (!gfc_is_simply_contiguous (rvalue, true, false)) |
99d821c0 DK |
4068 | { |
4069 | gfc_error ("Rank remapping target must be rank 1 or" | |
4070 | " simply contiguous at %L", &rvalue->where); | |
524af0d6 | 4071 | return false; |
99d821c0 | 4072 | } |
524af0d6 JB |
4073 | if (!gfc_notify_std (GFC_STD_F2008, "Rank remapping target is not " |
4074 | "rank 1 at %L", &rvalue->where)) | |
4075 | return false; | |
99d821c0 DK |
4076 | } |
4077 | } | |
4078 | ||
def66134 SK |
4079 | /* Now punt if we are dealing with a NULLIFY(X) or X = NULL(X). */ |
4080 | if (rvalue->expr_type == EXPR_NULL) | |
524af0d6 | 4081 | return true; |
def66134 | 4082 | |
fb5bc08b | 4083 | if (lvalue->ts.type == BT_CHARACTER) |
2990f854 | 4084 | { |
524af0d6 JB |
4085 | bool t = gfc_check_same_strlen (lvalue, rvalue, "pointer assignment"); |
4086 | if (!t) | |
4087 | return false; | |
2990f854 PT |
4088 | } |
4089 | ||
1d6b7f39 PT |
4090 | if (rvalue->expr_type == EXPR_VARIABLE && is_subref_array (rvalue)) |
4091 | lvalue->symtree->n.sym->attr.subref_array_pointer = 1; | |
4092 | ||
7d76d73a | 4093 | attr = gfc_expr_attr (rvalue); |
7f1f7ffb TB |
4094 | |
4095 | if (rvalue->expr_type == EXPR_FUNCTION && !attr.pointer) | |
4096 | { | |
de91486c AV |
4097 | /* F2008, C725. For PURE also C1283. Sometimes rvalue is a function call |
4098 | to caf_get. Map this to the same error message as below when it is | |
4099 | still a variable expression. */ | |
4100 | if (rvalue->value.function.isym | |
4101 | && rvalue->value.function.isym->id == GFC_ISYM_CAF_GET) | |
4102 | /* The test above might need to be extend when F08, Note 5.4 has to be | |
4103 | interpreted in the way that target and pointer with the same coindex | |
4104 | are allowed. */ | |
4105 | gfc_error ("Data target at %L shall not have a coindex", | |
4106 | &rvalue->where); | |
4107 | else | |
4108 | gfc_error ("Target expression in pointer assignment " | |
4109 | "at %L must deliver a pointer result", | |
4110 | &rvalue->where); | |
524af0d6 | 4111 | return false; |
7f1f7ffb TB |
4112 | } |
4113 | ||
7d76d73a TS |
4114 | if (!attr.target && !attr.pointer) |
4115 | { | |
31043f6c | 4116 | gfc_error ("Pointer assignment target is neither TARGET " |
7d76d73a | 4117 | "nor POINTER at %L", &rvalue->where); |
524af0d6 | 4118 | return false; |
7d76d73a | 4119 | } |
6de9cd9a | 4120 | |
7d76d73a TS |
4121 | if (is_pure && gfc_impure_variable (rvalue->symtree->n.sym)) |
4122 | { | |
31043f6c | 4123 | gfc_error ("Bad target in pointer assignment in PURE " |
7d76d73a TS |
4124 | "procedure at %L", &rvalue->where); |
4125 | } | |
6de9cd9a | 4126 | |
f1f39033 | 4127 | if (is_implicit_pure && gfc_impure_variable (rvalue->symtree->n.sym)) |
ccd7751b | 4128 | gfc_unset_implicit_pure (gfc_current_ns->proc_name); |
f1f39033 | 4129 | |
4075a94e PT |
4130 | if (gfc_has_vector_index (rvalue)) |
4131 | { | |
4132 | gfc_error ("Pointer assignment with vector subscript " | |
4133 | "on rhs at %L", &rvalue->where); | |
524af0d6 | 4134 | return false; |
4075a94e PT |
4135 | } |
4136 | ||
3dcc3ef2 TB |
4137 | if (attr.is_protected && attr.use_assoc |
4138 | && !(attr.pointer || attr.proc_pointer)) | |
ee7e677f | 4139 | { |
df2fba9e | 4140 | gfc_error ("Pointer assignment target has PROTECTED " |
636dff67 | 4141 | "attribute at %L", &rvalue->where); |
524af0d6 | 4142 | return false; |
ee7e677f TB |
4143 | } |
4144 | ||
d3a9eea2 TB |
4145 | /* F2008, C725. For PURE also C1283. */ |
4146 | if (rvalue->expr_type == EXPR_VARIABLE | |
4147 | && gfc_is_coindexed (rvalue)) | |
4148 | { | |
4149 | gfc_ref *ref; | |
4150 | for (ref = rvalue->ref; ref; ref = ref->next) | |
4151 | if (ref->type == REF_ARRAY && ref->u.ar.codimen) | |
4152 | { | |
4153 | gfc_error ("Data target at %L shall not have a coindex", | |
4154 | &rvalue->where); | |
524af0d6 | 4155 | return false; |
d3a9eea2 TB |
4156 | } |
4157 | } | |
4158 | ||
ed9984a0 | 4159 | /* Warn for assignments of contiguous pointers to targets which is not |
5abde510 | 4160 | contiguous. Be lenient in the definition of what counts as |
a4f759de | 4161 | contiguous. */ |
5abde510 TK |
4162 | |
4163 | if (lhs_attr.contiguous && !gfc_is_simply_contiguous (rvalue, false, true)) | |
ed9984a0 CP |
4164 | gfc_warning (OPT_Wextra, "Assignment to contiguous pointer from " |
4165 | "non-contiguous target at %L", &rvalue->where); | |
5abde510 | 4166 | |
f657024b | 4167 | /* Warn if it is the LHS pointer may lives longer than the RHS target. */ |
73e42eef | 4168 | if (warn_target_lifetime |
f657024b TB |
4169 | && rvalue->expr_type == EXPR_VARIABLE |
4170 | && !rvalue->symtree->n.sym->attr.save | |
c11384aa JW |
4171 | && !rvalue->symtree->n.sym->attr.pointer && !attr.pointer |
4172 | && !rvalue->symtree->n.sym->attr.host_assoc | |
f657024b TB |
4173 | && !rvalue->symtree->n.sym->attr.in_common |
4174 | && !rvalue->symtree->n.sym->attr.use_assoc | |
4175 | && !rvalue->symtree->n.sym->attr.dummy) | |
4176 | { | |
4177 | bool warn; | |
4178 | gfc_namespace *ns; | |
4179 | ||
4180 | warn = lvalue->symtree->n.sym->attr.dummy | |
4181 | || lvalue->symtree->n.sym->attr.result | |
ed0ba472 | 4182 | || lvalue->symtree->n.sym->attr.function |
916bad55 TB |
4183 | || (lvalue->symtree->n.sym->attr.host_assoc |
4184 | && lvalue->symtree->n.sym->ns | |
4185 | != rvalue->symtree->n.sym->ns) | |
f657024b TB |
4186 | || lvalue->symtree->n.sym->attr.use_assoc |
4187 | || lvalue->symtree->n.sym->attr.in_common; | |
4188 | ||
4189 | if (rvalue->symtree->n.sym->ns->proc_name | |
4190 | && rvalue->symtree->n.sym->ns->proc_name->attr.flavor != FL_PROCEDURE | |
4191 | && rvalue->symtree->n.sym->ns->proc_name->attr.flavor != FL_PROGRAM) | |
4192 | for (ns = rvalue->symtree->n.sym->ns; | |
1216b4d2 | 4193 | ns && ns->proc_name && ns->proc_name->attr.flavor != FL_PROCEDURE; |
f657024b TB |
4194 | ns = ns->parent) |
4195 | if (ns->parent == lvalue->symtree->n.sym->ns) | |
502af491 PCC |
4196 | { |
4197 | warn = true; | |
4198 | break; | |
4199 | } | |
f657024b TB |
4200 | |
4201 | if (warn) | |
48749dbc MLI |
4202 | gfc_warning (OPT_Wtarget_lifetime, |
4203 | "Pointer at %L in pointer assignment might outlive the " | |
f657024b TB |
4204 | "pointer target", &lvalue->where); |
4205 | } | |
4206 | ||
524af0d6 | 4207 | return true; |
6de9cd9a DN |
4208 | } |
4209 | ||
4210 | ||
4211 | /* Relative of gfc_check_assign() except that the lvalue is a single | |
597073ac | 4212 | symbol. Used for initialization assignments. */ |
6de9cd9a | 4213 | |
524af0d6 | 4214 | bool |
e35e87dc | 4215 | gfc_check_assign_symbol (gfc_symbol *sym, gfc_component *comp, gfc_expr *rvalue) |
6de9cd9a DN |
4216 | { |
4217 | gfc_expr lvalue; | |
524af0d6 | 4218 | bool r; |
e35e87dc | 4219 | bool pointer, proc_pointer; |
6de9cd9a DN |
4220 | |
4221 | memset (&lvalue, '\0', sizeof (gfc_expr)); | |
4222 | ||
4223 | lvalue.expr_type = EXPR_VARIABLE; | |
4224 | lvalue.ts = sym->ts; | |
4225 | if (sym->as) | |
4226 | lvalue.rank = sym->as->rank; | |
93acb62c | 4227 | lvalue.symtree = XCNEW (gfc_symtree); |
6de9cd9a DN |
4228 | lvalue.symtree->n.sym = sym; |
4229 | lvalue.where = sym->declared_at; | |
4230 | ||
e35e87dc TB |
4231 | if (comp) |
4232 | { | |
4233 | lvalue.ref = gfc_get_ref (); | |
4234 | lvalue.ref->type = REF_COMPONENT; | |
4235 | lvalue.ref->u.c.component = comp; | |
4236 | lvalue.ref->u.c.sym = sym; | |
4237 | lvalue.ts = comp->ts; | |
4238 | lvalue.rank = comp->as ? comp->as->rank : 0; | |
4239 | lvalue.where = comp->loc; | |
4240 | pointer = comp->ts.type == BT_CLASS && CLASS_DATA (comp) | |
4241 | ? CLASS_DATA (comp)->attr.class_pointer : comp->attr.pointer; | |
4242 | proc_pointer = comp->attr.proc_pointer; | |
4243 | } | |
4244 | else | |
4245 | { | |
4246 | pointer = sym->ts.type == BT_CLASS && CLASS_DATA (sym) | |
4247 | ? CLASS_DATA (sym)->attr.class_pointer : sym->attr.pointer; | |
4248 | proc_pointer = sym->attr.proc_pointer; | |
4249 | } | |
4250 | ||
4251 | if (pointer || proc_pointer) | |
597073ac PB |
4252 | r = gfc_check_pointer_assign (&lvalue, rvalue); |
4253 | else | |
3e6ab828 SK |
4254 | { |
4255 | /* If a conversion function, e.g., __convert_i8_i4, was inserted | |
4256 | into an array constructor, we should check if it can be reduced | |
4257 | as an initialization expression. */ | |
4258 | if (rvalue->expr_type == EXPR_FUNCTION | |
4259 | && rvalue->value.function.isym | |
4260 | && (rvalue->value.function.isym->conversion == 1)) | |
4261 | gfc_check_init_expr (rvalue); | |
4262 | ||
4263 | r = gfc_check_assign (&lvalue, rvalue, 1); | |
4264 | } | |
6de9cd9a | 4265 | |
cede9502 | 4266 | free (lvalue.symtree); |
e3816ac9 | 4267 | free (lvalue.ref); |
6de9cd9a | 4268 | |
524af0d6 | 4269 | if (!r) |
80f95228 | 4270 | return r; |
8b704316 | 4271 | |
e35e87dc | 4272 | if (pointer && rvalue->expr_type != EXPR_NULL) |
80f95228 JW |
4273 | { |
4274 | /* F08:C461. Additional checks for pointer initialization. */ | |
4275 | symbol_attribute attr; | |
4276 | attr = gfc_expr_attr (rvalue); | |
4277 | if (attr.allocatable) | |
4278 | { | |
e35e87dc TB |
4279 | gfc_error ("Pointer initialization target at %L " |
4280 | "must not be ALLOCATABLE", &rvalue->where); | |
524af0d6 | 4281 | return false; |
80f95228 | 4282 | } |
7522a064 | 4283 | if (!attr.target || attr.pointer) |
80f95228 | 4284 | { |
e35e87dc TB |
4285 | gfc_error ("Pointer initialization target at %L " |
4286 | "must have the TARGET attribute", &rvalue->where); | |
524af0d6 | 4287 | return false; |
80f95228 | 4288 | } |
e35e87dc TB |
4289 | |
4290 | if (!attr.save && rvalue->expr_type == EXPR_VARIABLE | |
4291 | && rvalue->symtree->n.sym->ns->proc_name | |
4292 | && rvalue->symtree->n.sym->ns->proc_name->attr.is_main_program) | |
4293 | { | |
4294 | rvalue->symtree->n.sym->ns->proc_name->attr.save = SAVE_IMPLICIT; | |
4295 | attr.save = SAVE_IMPLICIT; | |
4296 | } | |
4297 | ||
80f95228 JW |
4298 | if (!attr.save) |
4299 | { | |
e35e87dc TB |
4300 | gfc_error ("Pointer initialization target at %L " |
4301 | "must have the SAVE attribute", &rvalue->where); | |
524af0d6 | 4302 | return false; |
80f95228 JW |
4303 | } |
4304 | } | |
8b704316 | 4305 | |
e35e87dc | 4306 | if (proc_pointer && rvalue->expr_type != EXPR_NULL) |
7522a064 JW |
4307 | { |
4308 | /* F08:C1220. Additional checks for procedure pointer initialization. */ | |
4309 | symbol_attribute attr = gfc_expr_attr (rvalue); | |
4310 | if (attr.proc_pointer) | |
4311 | { | |
4312 | gfc_error ("Procedure pointer initialization target at %L " | |
4313 | "may not be a procedure pointer", &rvalue->where); | |
524af0d6 | 4314 | return false; |
7522a064 JW |
4315 | } |
4316 | } | |
80f95228 | 4317 | |
524af0d6 | 4318 | return true; |
6de9cd9a | 4319 | } |
54b4ba60 | 4320 | |
13051352 FR |
4321 | /* Invoke gfc_build_init_expr to create an initializer expression, but do not |
4322 | * require that an expression be built. */ | |
4323 | ||
4324 | gfc_expr * | |
4325 | gfc_build_default_init_expr (gfc_typespec *ts, locus *where) | |
4326 | { | |
4327 | return gfc_build_init_expr (ts, where, false); | |
4328 | } | |
54b4ba60 | 4329 | |
7fc61626 FR |
4330 | /* Build an initializer for a local integer, real, complex, logical, or |
4331 | character variable, based on the command line flags finit-local-zero, | |
13051352 FR |
4332 | finit-integer=, finit-real=, finit-logical=, and finit-character=. |
4333 | With force, an initializer is ALWAYS generated. */ | |
7fc61626 FR |
4334 | |
4335 | gfc_expr * | |
13051352 | 4336 | gfc_build_init_expr (gfc_typespec *ts, locus *where, bool force) |
7fc61626 | 4337 | { |
7fc61626 | 4338 | gfc_expr *init_expr; |
7fc61626 FR |
4339 | |
4340 | /* Try to build an initializer expression. */ | |
4341 | init_expr = gfc_get_constant_expr (ts->type, ts->kind, where); | |
4342 | ||
13051352 FR |
4343 | /* If we want to force generation, make sure we default to zero. */ |
4344 | gfc_init_local_real init_real = flag_init_real; | |
4345 | int init_logical = gfc_option.flag_init_logical; | |
4346 | if (force) | |
4347 | { | |
4348 | if (init_real == GFC_INIT_REAL_OFF) | |
4349 | init_real = GFC_INIT_REAL_ZERO; | |
4350 | if (init_logical == GFC_INIT_LOGICAL_OFF) | |
4351 | init_logical = GFC_INIT_LOGICAL_FALSE; | |
4352 | } | |
4353 | ||
7fc61626 FR |
4354 | /* We will only initialize integers, reals, complex, logicals, and |
4355 | characters, and only if the corresponding command-line flags | |
4356 | were set. Otherwise, we free init_expr and return null. */ | |
4357 | switch (ts->type) | |
4358 | { | |
4359 | case BT_INTEGER: | |
13051352 | 4360 | if (force || gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF) |
7fc61626 FR |
4361 | mpz_set_si (init_expr->value.integer, |
4362 | gfc_option.flag_init_integer_value); | |
4363 | else | |
4364 | { | |
4365 | gfc_free_expr (init_expr); | |
4366 | init_expr = NULL; | |
4367 | } | |
4368 | break; | |
4369 | ||
4370 | case BT_REAL: | |
13051352 | 4371 | switch (init_real) |
7fc61626 FR |
4372 | { |
4373 | case GFC_INIT_REAL_SNAN: | |
4374 | init_expr->is_snan = 1; | |
4375 | /* Fall through. */ | |
4376 | case GFC_INIT_REAL_NAN: | |
4377 | mpfr_set_nan (init_expr->value.real); | |
4378 | break; | |
4379 | ||
4380 | case GFC_INIT_REAL_INF: | |
4381 | mpfr_set_inf (init_expr->value.real, 1); | |
4382 | break; | |
4383 | ||
4384 | case GFC_INIT_REAL_NEG_INF: | |
4385 | mpfr_set_inf (init_expr->value.real, -1); | |
4386 | break; | |
4387 | ||
4388 | case GFC_INIT_REAL_ZERO: | |
4389 | mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE); | |
4390 | break; | |
4391 | ||
4392 | default: | |
4393 | gfc_free_expr (init_expr); | |
4394 | init_expr = NULL; | |
4395 | break; | |
4396 | } | |
4397 | break; | |
4398 | ||
4399 | case BT_COMPLEX: | |
13051352 | 4400 | switch (init_real) |
7fc61626 FR |
4401 | { |
4402 | case GFC_INIT_REAL_SNAN: | |
4403 | init_expr->is_snan = 1; | |
4404 | /* Fall through. */ | |
4405 | case GFC_INIT_REAL_NAN: | |
4406 | mpfr_set_nan (mpc_realref (init_expr->value.complex)); | |
4407 | mpfr_set_nan (mpc_imagref (init_expr->value.complex)); | |
4408 | break; | |
4409 | ||
4410 | case GFC_INIT_REAL_INF: | |
4411 | mpfr_set_inf (mpc_realref (init_expr->value.complex), 1); | |
4412 | mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1); | |
4413 | break; | |
4414 | ||
4415 | case GFC_INIT_REAL_NEG_INF: | |
4416 | mpfr_set_inf (mpc_realref (init_expr->value.complex), -1); | |
4417 | mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1); | |
4418 | break; | |
4419 | ||
4420 | case GFC_INIT_REAL_ZERO: | |
4421 | mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE); | |
4422 | break; | |
4423 | ||
4424 | default: | |
4425 | gfc_free_expr (init_expr); | |
4426 | init_expr = NULL; | |
4427 | break; | |
4428 | } | |
4429 | break; | |
4430 | ||
4431 | case BT_LOGICAL: | |
13051352 | 4432 | if (init_logical == GFC_INIT_LOGICAL_FALSE) |
7fc61626 | 4433 | init_expr->value.logical = 0; |
13051352 | 4434 | else if (init_logical == GFC_INIT_LOGICAL_TRUE) |
7fc61626 FR |
4435 | init_expr->value.logical = 1; |
4436 | else | |
4437 | { | |
4438 | gfc_free_expr (init_expr); | |
4439 | init_expr = NULL; | |
4440 | } | |
4441 | break; | |
4442 | ||
4443 | case BT_CHARACTER: | |
4444 | /* For characters, the length must be constant in order to | |
4445 | create a default initializer. */ | |
13051352 | 4446 | if ((force || gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON) |
7fc61626 FR |
4447 | && ts->u.cl->length |
4448 | && ts->u.cl->length->expr_type == EXPR_CONSTANT) | |
4449 | { | |
6b271a2e | 4450 | HOST_WIDE_INT char_len = gfc_mpz_get_hwi (ts->u.cl->length->value.integer); |
7fc61626 FR |
4451 | init_expr->value.character.length = char_len; |
4452 | init_expr->value.character.string = gfc_get_wide_string (char_len+1); | |
6b271a2e | 4453 | for (size_t i = 0; i < (size_t) char_len; i++) |
7fc61626 FR |
4454 | init_expr->value.character.string[i] |
4455 | = (unsigned char) gfc_option.flag_init_character_value; | |
4456 | } | |
4457 | else | |
4458 | { | |
4459 | gfc_free_expr (init_expr); | |
4460 | init_expr = NULL; | |
4461 | } | |
13051352 FR |
4462 | if (!init_expr |
4463 | && (force || gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON) | |
7fc61626 FR |
4464 | && ts->u.cl->length && flag_max_stack_var_size != 0) |
4465 | { | |
4466 | gfc_actual_arglist *arg; | |
4467 | init_expr = gfc_get_expr (); | |
4468 | init_expr->where = *where; | |
4469 | init_expr->ts = *ts; | |
4470 | init_expr->expr_type = EXPR_FUNCTION; | |
4471 | init_expr->value.function.isym = | |
4472 | gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT); | |
4473 | init_expr->value.function.name = "repeat"; | |
4474 | arg = gfc_get_actual_arglist (); | |
4475 | arg->expr = gfc_get_character_expr (ts->kind, where, NULL, 1); | |
4476 | arg->expr->value.character.string[0] = | |
4477 | gfc_option.flag_init_character_value; | |
4478 | arg->next = gfc_get_actual_arglist (); | |
4479 | arg->next->expr = gfc_copy_expr (ts->u.cl->length); | |
4480 | init_expr->value.function.actual = arg; | |
4481 | } | |
4482 | break; | |
4483 | ||
4484 | default: | |
4485 | gfc_free_expr (init_expr); | |
4486 | init_expr = NULL; | |
4487 | } | |
4488 | ||
4489 | return init_expr; | |
4490 | } | |
4491 | ||
4492 | /* Apply an initialization expression to a typespec. Can be used for symbols or | |
4493 | components. Similar to add_init_expr_to_sym in decl.c; could probably be | |
4494 | combined with some effort. */ | |
4495 | ||
4496 | void | |
4497 | gfc_apply_init (gfc_typespec *ts, symbol_attribute *attr, gfc_expr *init) | |
4498 | { | |
4499 | if (ts->type == BT_CHARACTER && !attr->pointer && init | |
4500 | && ts->u.cl | |
36f972b7 SK |
4501 | && ts->u.cl->length |
4502 | && ts->u.cl->length->expr_type == EXPR_CONSTANT | |
4503 | && ts->u.cl->length->ts.type == BT_INTEGER) | |
7fc61626 | 4504 | { |
6b271a2e | 4505 | HOST_WIDE_INT len = gfc_mpz_get_hwi (ts->u.cl->length->value.integer); |
7fc61626 FR |
4506 | |
4507 | if (init->expr_type == EXPR_CONSTANT) | |
4508 | gfc_set_constant_character_len (len, init, -1); | |
4509 | else if (init | |
aefd636b | 4510 | && init->ts.type == BT_CHARACTER |
fcf79237 | 4511 | && init->ts.u.cl && init->ts.u.cl->length |
7fc61626 FR |
4512 | && mpz_cmp (ts->u.cl->length->value.integer, |
4513 | init->ts.u.cl->length->value.integer)) | |
4514 | { | |
4515 | gfc_constructor *ctor; | |
4516 | ctor = gfc_constructor_first (init->value.constructor); | |
4517 | ||
4518 | if (ctor) | |
4519 | { | |
7fc61626 FR |
4520 | bool has_ts = (init->ts.u.cl |
4521 | && init->ts.u.cl->length_from_typespec); | |
4522 | ||
4523 | /* Remember the length of the first element for checking | |
4524 | that all elements *in the constructor* have the same | |
4525 | length. This need not be the length of the LHS! */ | |
4526 | gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT); | |
4527 | gcc_assert (ctor->expr->ts.type == BT_CHARACTER); | |
6b271a2e | 4528 | gfc_charlen_t first_len = ctor->expr->value.character.length; |
7fc61626 FR |
4529 | |
4530 | for ( ; ctor; ctor = gfc_constructor_next (ctor)) | |
4531 | if (ctor->expr->expr_type == EXPR_CONSTANT) | |
4532 | { | |
4533 | gfc_set_constant_character_len (len, ctor->expr, | |
4534 | has_ts ? -1 : first_len); | |
7e98cccb SK |
4535 | if (!ctor->expr->ts.u.cl) |
4536 | ctor->expr->ts.u.cl | |
4537 | = gfc_new_charlen (gfc_current_ns, ts->u.cl); | |
4538 | else | |
4539 | ctor->expr->ts.u.cl->length | |
4540 | = gfc_copy_expr (ts->u.cl->length); | |
7fc61626 FR |
4541 | } |
4542 | } | |
4543 | } | |
4544 | } | |
4545 | } | |
4546 | ||
4547 | ||
cc03bf7a AV |
4548 | /* Check whether an expression is a structure constructor and whether it has |
4549 | other values than NULL. */ | |
4550 | ||
4551 | bool | |
4552 | is_non_empty_structure_constructor (gfc_expr * e) | |
4553 | { | |
4554 | if (e->expr_type != EXPR_STRUCTURE) | |
4555 | return false; | |
4556 | ||
4557 | gfc_constructor *cons = gfc_constructor_first (e->value.constructor); | |
4558 | while (cons) | |
4559 | { | |
4560 | if (!cons->expr || cons->expr->expr_type != EXPR_NULL) | |
4561 | return true; | |
4562 | cons = gfc_constructor_next (cons); | |
4563 | } | |
4564 | return false; | |
4565 | } | |
4566 | ||
4567 | ||
16e520b6 DF |
4568 | /* Check for default initializer; sym->value is not enough |
4569 | as it is also set for EXPR_NULL of allocatables. */ | |
4570 | ||
4571 | bool | |
4572 | gfc_has_default_initializer (gfc_symbol *der) | |
4573 | { | |
4574 | gfc_component *c; | |
4575 | ||
f6288c24 | 4576 | gcc_assert (gfc_fl_struct (der->attr.flavor)); |
16e520b6 | 4577 | for (c = der->components; c; c = c->next) |
f6288c24 | 4578 | if (gfc_bt_struct (c->ts.type)) |
16e520b6 | 4579 | { |
33247762 | 4580 | if (!c->attr.pointer && !c->attr.proc_pointer |
bf9f15ee | 4581 | && !(c->attr.allocatable && der == c->ts.u.derived) |
cc03bf7a AV |
4582 | && ((c->initializer |
4583 | && is_non_empty_structure_constructor (c->initializer)) | |
4584 | || gfc_has_default_initializer (c->ts.u.derived))) | |
16e520b6 | 4585 | return true; |
0173c67b TB |
4586 | if (c->attr.pointer && c->initializer) |
4587 | return true; | |
16e520b6 DF |
4588 | } |
4589 | else | |
4590 | { | |
4591 | if (c->initializer) | |
4592 | return true; | |
4593 | } | |
4594 | ||
4595 | return false; | |
4596 | } | |
4597 | ||
0173c67b | 4598 | |
f8da53e0 FR |
4599 | /* |
4600 | Generate an initializer expression which initializes the entirety of a union. | |
4601 | A normal structure constructor is insufficient without undue effort, because | |
4602 | components of maps may be oddly aligned/overlapped. (For example if a | |
4603 | character is initialized from one map overtop a real from the other, only one | |
4604 | byte of the real is actually initialized.) Unfortunately we don't know the | |
4605 | size of the union right now, so we can't generate a proper initializer, but | |
4606 | we use a NULL expr as a placeholder and do the right thing later in | |
4607 | gfc_trans_subcomponent_assign. | |
4608 | */ | |
4609 | static gfc_expr * | |
4610 | generate_union_initializer (gfc_component *un) | |
4611 | { | |
4612 | if (un == NULL || un->ts.type != BT_UNION) | |
4613 | return NULL; | |
4614 | ||
4615 | gfc_expr *placeholder = gfc_get_null_expr (&un->loc); | |
4616 | placeholder->ts = un->ts; | |
4617 | return placeholder; | |
4618 | } | |
4619 | ||
4620 | ||
4621 | /* Get the user-specified initializer for a union, if any. This means the user | |
4622 | has said to initialize component(s) of a map. For simplicity's sake we | |
4623 | only allow the user to initialize the first map. We don't have to worry | |
4624 | about overlapping initializers as they are released early in resolution (see | |
4625 | resolve_fl_struct). */ | |
4626 | ||
4627 | static gfc_expr * | |
4628 | get_union_initializer (gfc_symbol *union_type, gfc_component **map_p) | |
4629 | { | |
4630 | gfc_component *map; | |
4631 | gfc_expr *init=NULL; | |
4632 | ||
4633 | if (!union_type || union_type->attr.flavor != FL_UNION) | |
4634 | return NULL; | |
4635 | ||
4636 | for (map = union_type->components; map; map = map->next) | |
4637 | { | |
4638 | if (gfc_has_default_initializer (map->ts.u.derived)) | |
4639 | { | |
4640 | init = gfc_default_initializer (&map->ts); | |
4641 | if (map_p) | |
4642 | *map_p = map; | |
4643 | break; | |
4644 | } | |
4645 | } | |
4646 | ||
4647 | if (map_p && !init) | |
4648 | *map_p = NULL; | |
4649 | ||
4650 | return init; | |
4651 | } | |
4652 | ||
245471c6 FR |
4653 | static bool |
4654 | class_allocatable (gfc_component *comp) | |
4655 | { | |
4656 | return comp->ts.type == BT_CLASS && CLASS_DATA (comp) | |
4657 | && CLASS_DATA (comp)->attr.allocatable; | |
4658 | } | |
4659 | ||
4660 | static bool | |
4661 | class_pointer (gfc_component *comp) | |
4662 | { | |
4663 | return comp->ts.type == BT_CLASS && CLASS_DATA (comp) | |
4664 | && CLASS_DATA (comp)->attr.pointer; | |
4665 | } | |
4666 | ||
4667 | static bool | |
4668 | comp_allocatable (gfc_component *comp) | |
4669 | { | |
4670 | return comp->attr.allocatable || class_allocatable (comp); | |
4671 | } | |
4672 | ||
4673 | static bool | |
4674 | comp_pointer (gfc_component *comp) | |
4675 | { | |
4676 | return comp->attr.pointer | |
4677 | || comp->attr.pointer | |
4678 | || comp->attr.proc_pointer | |
4679 | || comp->attr.class_pointer | |
4680 | || class_pointer (comp); | |
4681 | } | |
4682 | ||
7fc61626 FR |
4683 | /* Fetch or generate an initializer for the given component. |
4684 | Only generate an initializer if generate is true. */ | |
4685 | ||
4686 | static gfc_expr * | |
245471c6 | 4687 | component_initializer (gfc_component *c, bool generate) |
7fc61626 FR |
4688 | { |
4689 | gfc_expr *init = NULL; | |
4690 | ||
245471c6 FR |
4691 | /* Allocatable components always get EXPR_NULL. |
4692 | Pointer components are only initialized when generating, and only if they | |
4693 | do not already have an initializer. */ | |
4694 | if (comp_allocatable (c) || (generate && comp_pointer (c) && !c->initializer)) | |
4695 | { | |
4696 | init = gfc_get_null_expr (&c->loc); | |
4697 | init->ts = c->ts; | |
4698 | return init; | |
4699 | } | |
4700 | ||
4701 | /* See if we can find the initializer immediately. */ | |
4702 | if (c->initializer || !generate) | |
7fc61626 FR |
4703 | return c->initializer; |
4704 | ||
4705 | /* Recursively handle derived type components. */ | |
245471c6 | 4706 | else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS) |
7fc61626 FR |
4707 | init = gfc_generate_initializer (&c->ts, true); |
4708 | ||
f8da53e0 FR |
4709 | else if (c->ts.type == BT_UNION && c->ts.u.derived->components) |
4710 | { | |
4711 | gfc_component *map = NULL; | |
4712 | gfc_constructor *ctor; | |
4713 | gfc_expr *user_init; | |
4714 | ||
4715 | /* If we don't have a user initializer and we aren't generating one, this | |
4716 | union has no initializer. */ | |
4717 | user_init = get_union_initializer (c->ts.u.derived, &map); | |
4718 | if (!user_init && !generate) | |
4719 | return NULL; | |
4720 | ||
4721 | /* Otherwise use a structure constructor. */ | |
4722 | init = gfc_get_structure_constructor_expr (c->ts.type, c->ts.kind, | |
4723 | &c->loc); | |
4724 | init->ts = c->ts; | |
4725 | ||
4726 | /* If we are to generate an initializer for the union, add a constructor | |
4727 | which initializes the whole union first. */ | |
4728 | if (generate) | |
4729 | { | |
4730 | ctor = gfc_constructor_get (); | |
4731 | ctor->expr = generate_union_initializer (c); | |
4732 | gfc_constructor_append (&init->value.constructor, ctor); | |
4733 | } | |
4734 | ||
4735 | /* If we found an initializer in one of our maps, apply it. Note this | |
4736 | is applied _after_ the entire-union initializer above if any. */ | |
4737 | if (user_init) | |
4738 | { | |
4739 | ctor = gfc_constructor_get (); | |
4740 | ctor->expr = user_init; | |
4741 | ctor->n.component = map; | |
4742 | gfc_constructor_append (&init->value.constructor, ctor); | |
4743 | } | |
4744 | } | |
4745 | ||
7fc61626 FR |
4746 | /* Treat simple components like locals. */ |
4747 | else | |
4748 | { | |
13051352 FR |
4749 | /* We MUST give an initializer, so force generation. */ |
4750 | init = gfc_build_init_expr (&c->ts, &c->loc, true); | |
7fc61626 FR |
4751 | gfc_apply_init (&c->ts, &c->attr, init); |
4752 | } | |
4753 | ||
245471c6 | 4754 | return init; |
7fc61626 FR |
4755 | } |
4756 | ||
4757 | ||
4758 | /* Get an expression for a default initializer of a derived type. */ | |
54b4ba60 PB |
4759 | |
4760 | gfc_expr * | |
4761 | gfc_default_initializer (gfc_typespec *ts) | |
4762 | { | |
7fc61626 FR |
4763 | return gfc_generate_initializer (ts, false); |
4764 | } | |
4765 | ||
d66a58d3 FR |
4766 | /* Generate an initializer expression for an iso_c_binding type |
4767 | such as c_[fun]ptr. The appropriate initializer is c_null_[fun]ptr. */ | |
4768 | ||
4769 | static gfc_expr * | |
4770 | generate_isocbinding_initializer (gfc_symbol *derived) | |
4771 | { | |
4772 | /* The initializers have already been built into the c_null_[fun]ptr symbols | |
4773 | from gen_special_c_interop_ptr. */ | |
4774 | gfc_symtree *npsym = NULL; | |
4775 | if (0 == strcmp (derived->name, "c_ptr")) | |
4776 | gfc_find_sym_tree ("c_null_ptr", gfc_current_ns, true, &npsym); | |
4777 | else if (0 == strcmp (derived->name, "c_funptr")) | |
4778 | gfc_find_sym_tree ("c_null_funptr", gfc_current_ns, true, &npsym); | |
4779 | else | |
4780 | gfc_internal_error ("generate_isocbinding_initializer(): bad iso_c_binding" | |
4781 | " type, expected %<c_ptr%> or %<c_funptr%>"); | |
4782 | if (npsym) | |
4783 | { | |
4784 | gfc_expr *init = gfc_copy_expr (npsym->n.sym->value); | |
4785 | init->symtree = npsym; | |
4786 | init->ts.is_iso_c = true; | |
4787 | return init; | |
4788 | } | |
4789 | ||
4790 | return NULL; | |
4791 | } | |
7fc61626 | 4792 | |
bf9f15ee | 4793 | /* Get or generate an expression for a default initializer of a derived type. |
7fc61626 FR |
4794 | If -finit-derived is specified, generate default initialization expressions |
4795 | for components that lack them when generate is set. */ | |
4796 | ||
4797 | gfc_expr * | |
4798 | gfc_generate_initializer (gfc_typespec *ts, bool generate) | |
4799 | { | |
4800 | gfc_expr *init, *tmp; | |
b7e75771 | 4801 | gfc_component *comp; |
d66a58d3 | 4802 | |
7fc61626 | 4803 | generate = flag_init_derived && generate; |
54b4ba60 | 4804 | |
d66a58d3 FR |
4805 | if (ts->u.derived->ts.is_iso_c && generate) |
4806 | return generate_isocbinding_initializer (ts->u.derived); | |
4807 | ||
16e520b6 | 4808 | /* See if we have a default initializer in this, but not in nested |
7fc61626 FR |
4809 | types (otherwise we could use gfc_has_default_initializer()). |
4810 | We don't need to check if we are going to generate them. */ | |
4811 | comp = ts->u.derived->components; | |
4812 | if (!generate) | |
4813 | { | |
4814 | for (; comp; comp = comp->next) | |
245471c6 | 4815 | if (comp->initializer || comp_allocatable (comp)) |
7fc61626 FR |
4816 | break; |
4817 | } | |
54b4ba60 | 4818 | |
b7e75771 | 4819 | if (!comp) |
54b4ba60 PB |
4820 | return NULL; |
4821 | ||
b7e75771 JD |
4822 | init = gfc_get_structure_constructor_expr (ts->type, ts->kind, |
4823 | &ts->u.derived->declared_at); | |
54b4ba60 | 4824 | init->ts = *ts; |
7e49f965 | 4825 | |
b7e75771 | 4826 | for (comp = ts->u.derived->components; comp; comp = comp->next) |
54b4ba60 | 4827 | { |
b7e75771 | 4828 | gfc_constructor *ctor = gfc_constructor_get(); |
54b4ba60 | 4829 | |
7fc61626 | 4830 | /* Fetch or generate an initializer for the component. */ |
245471c6 | 4831 | tmp = component_initializer (comp, generate); |
7fc61626 | 4832 | if (tmp) |
0b673c09 | 4833 | { |
e75eb64f FR |
4834 | /* Save the component ref for STRUCTUREs and UNIONs. */ |
4835 | if (ts->u.derived->attr.flavor == FL_STRUCT | |
4836 | || ts->u.derived->attr.flavor == FL_UNION) | |
4837 | ctor->n.component = comp; | |
7fc61626 FR |
4838 | |
4839 | /* If the initializer was not generated, we need a copy. */ | |
4840 | ctor->expr = comp->initializer ? gfc_copy_expr (tmp) : tmp; | |
245471c6 | 4841 | if ((comp->ts.type != tmp->ts.type || comp->ts.kind != tmp->ts.kind) |
0b673c09 | 4842 | && !comp->attr.pointer && !comp->attr.proc_pointer) |
0735a1c8 SK |
4843 | { |
4844 | bool val; | |
4845 | val = gfc_convert_type_warn (ctor->expr, &comp->ts, 1, false); | |
4846 | if (val == false) | |
4847 | return NULL; | |
4848 | } | |
0b673c09 | 4849 | } |
5046aff5 | 4850 | |
b7e75771 | 4851 | gfc_constructor_append (&init->value.constructor, ctor); |
54b4ba60 | 4852 | } |
b7e75771 | 4853 | |
54b4ba60 PB |
4854 | return init; |
4855 | } | |
294fbfc8 TS |
4856 | |
4857 | ||
4858 | /* Given a symbol, create an expression node with that symbol as a | |
4859 | variable. If the symbol is array valued, setup a reference of the | |
4860 | whole array. */ | |
4861 | ||
4862 | gfc_expr * | |
636dff67 | 4863 | gfc_get_variable_expr (gfc_symtree *var) |
294fbfc8 TS |
4864 | { |
4865 | gfc_expr *e; | |
4866 | ||
4867 | e = gfc_get_expr (); | |
4868 | e->expr_type = EXPR_VARIABLE; | |
4869 | e->symtree = var; | |
4870 | e->ts = var->n.sym->ts; | |
4871 | ||
1251a8be JW |
4872 | if (var->n.sym->attr.flavor != FL_PROCEDURE |
4873 | && ((var->n.sym->as != NULL && var->n.sym->ts.type != BT_CLASS) | |
4874 | || (var->n.sym->ts.type == BT_CLASS && CLASS_DATA (var->n.sym) | |
4875 | && CLASS_DATA (var->n.sym)->as))) | |
294fbfc8 | 4876 | { |
102344e2 TB |
4877 | e->rank = var->n.sym->ts.type == BT_CLASS |
4878 | ? CLASS_DATA (var->n.sym)->as->rank : var->n.sym->as->rank; | |
294fbfc8 TS |
4879 | e->ref = gfc_get_ref (); |
4880 | e->ref->type = REF_ARRAY; | |
4881 | e->ref->u.ar.type = AR_FULL; | |
8f75db9f PT |
4882 | e->ref->u.ar.as = gfc_copy_array_spec (var->n.sym->ts.type == BT_CLASS |
4883 | ? CLASS_DATA (var->n.sym)->as | |
4884 | : var->n.sym->as); | |
294fbfc8 TS |
4885 | } |
4886 | ||
4887 | return e; | |
4888 | } | |
4889 | ||
47992a4a | 4890 | |
4d382327 AF |
4891 | /* Adds a full array reference to an expression, as needed. */ |
4892 | ||
4893 | void | |
4894 | gfc_add_full_array_ref (gfc_expr *e, gfc_array_spec *as) | |
4895 | { | |
4896 | gfc_ref *ref; | |
4897 | for (ref = e->ref; ref; ref = ref->next) | |
4898 | if (!ref->next) | |
4899 | break; | |
4900 | if (ref) | |
4901 | { | |
4902 | ref->next = gfc_get_ref (); | |
4903 | ref = ref->next; | |
4904 | } | |
4905 | else | |
4906 | { | |
4907 | e->ref = gfc_get_ref (); | |
4908 | ref = e->ref; | |
4909 | } | |
4910 | ref->type = REF_ARRAY; | |
4911 | ref->u.ar.type = AR_FULL; | |
4912 | ref->u.ar.dimen = e->rank; | |
4913 | ref->u.ar.where = e->where; | |
4914 | ref->u.ar.as = as; | |
4915 | } | |
4916 | ||
4917 | ||
0d87fa8c JW |
4918 | gfc_expr * |
4919 | gfc_lval_expr_from_sym (gfc_symbol *sym) | |
4920 | { | |
4921 | gfc_expr *lval; | |
f3b0bb7a | 4922 | gfc_array_spec *as; |
0d87fa8c JW |
4923 | lval = gfc_get_expr (); |
4924 | lval->expr_type = EXPR_VARIABLE; | |
4925 | lval->where = sym->declared_at; | |
4926 | lval->ts = sym->ts; | |
4927 | lval->symtree = gfc_find_symtree (sym->ns->sym_root, sym->name); | |
4928 | ||
4929 | /* It will always be a full array. */ | |
f3b0bb7a AV |
4930 | as = IS_CLASS_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as; |
4931 | lval->rank = as ? as->rank : 0; | |
0d87fa8c | 4932 | if (lval->rank) |
f3b0bb7a | 4933 | gfc_add_full_array_ref (lval, as); |
0d87fa8c JW |
4934 | return lval; |
4935 | } | |
4936 | ||
4937 | ||
b7d1d8b4 PT |
4938 | /* Returns the array_spec of a full array expression. A NULL is |
4939 | returned otherwise. */ | |
4940 | gfc_array_spec * | |
4941 | gfc_get_full_arrayspec_from_expr (gfc_expr *expr) | |
4942 | { | |
4943 | gfc_array_spec *as; | |
4944 | gfc_ref *ref; | |
4945 | ||
4946 | if (expr->rank == 0) | |
4947 | return NULL; | |
4948 | ||
4949 | /* Follow any component references. */ | |
4950 | if (expr->expr_type == EXPR_VARIABLE | |
4951 | || expr->expr_type == EXPR_CONSTANT) | |
4952 | { | |
650f7d09 TK |
4953 | if (expr->symtree) |
4954 | as = expr->symtree->n.sym->as; | |
4955 | else | |
4956 | as = NULL; | |
4957 | ||
b7d1d8b4 PT |
4958 | for (ref = expr->ref; ref; ref = ref->next) |
4959 | { | |
4960 | switch (ref->type) | |
4961 | { | |
4962 | case REF_COMPONENT: | |
4963 | as = ref->u.c.component->as; | |
4964 | continue; | |
4965 | ||
4966 | case REF_SUBSTRING: | |
a5fbc2f3 | 4967 | case REF_INQUIRY: |
b7d1d8b4 PT |
4968 | continue; |
4969 | ||
4970 | case REF_ARRAY: | |
4971 | { | |
4972 | switch (ref->u.ar.type) | |
4973 | { | |
4974 | case AR_ELEMENT: | |
4975 | case AR_SECTION: | |
4976 | case AR_UNKNOWN: | |
4977 | as = NULL; | |
4978 | continue; | |
4979 | ||
4980 | case AR_FULL: | |
4981 | break; | |
4982 | } | |
4983 | break; | |
4984 | } | |
4985 | } | |
4986 | } | |
4987 | } | |
4988 | else | |
4989 | as = NULL; | |
4990 | ||
4991 | return as; | |
4992 | } | |
4993 | ||
4994 | ||
640670c7 | 4995 | /* General expression traversal function. */ |
47992a4a | 4996 | |
640670c7 PT |
4997 | bool |
4998 | gfc_traverse_expr (gfc_expr *expr, gfc_symbol *sym, | |
4999 | bool (*func)(gfc_expr *, gfc_symbol *, int*), | |
5000 | int f) | |
47992a4a | 5001 | { |
640670c7 | 5002 | gfc_array_ref ar; |
47992a4a | 5003 | gfc_ref *ref; |
640670c7 PT |
5004 | gfc_actual_arglist *args; |
5005 | gfc_constructor *c; | |
47992a4a EE |
5006 | int i; |
5007 | ||
640670c7 PT |
5008 | if (!expr) |
5009 | return false; | |
47992a4a | 5010 | |
908a2235 PT |
5011 | if ((*func) (expr, sym, &f)) |
5012 | return true; | |
47992a4a | 5013 | |
908a2235 | 5014 | if (expr->ts.type == BT_CHARACTER |
bc21d315 JW |
5015 | && expr->ts.u.cl |
5016 | && expr->ts.u.cl->length | |
5017 | && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT | |
5018 | && gfc_traverse_expr (expr->ts.u.cl->length, sym, func, f)) | |
908a2235 | 5019 | return true; |
47992a4a | 5020 | |
908a2235 PT |
5021 | switch (expr->expr_type) |
5022 | { | |
687ea68f TB |
5023 | case EXPR_PPC: |
5024 | case EXPR_COMPCALL: | |
640670c7 PT |
5025 | case EXPR_FUNCTION: |
5026 | for (args = expr->value.function.actual; args; args = args->next) | |
5027 | { | |
5028 | if (gfc_traverse_expr (args->expr, sym, func, f)) | |
5029 | return true; | |
5030 | } | |
47992a4a EE |
5031 | break; |
5032 | ||
908a2235 | 5033 | case EXPR_VARIABLE: |
47992a4a EE |
5034 | case EXPR_CONSTANT: |
5035 | case EXPR_NULL: | |
5036 | case EXPR_SUBSTRING: | |
5037 | break; | |
5038 | ||
5039 | case EXPR_STRUCTURE: | |
5040 | case EXPR_ARRAY: | |
b7e75771 JD |
5041 | for (c = gfc_constructor_first (expr->value.constructor); |
5042 | c; c = gfc_constructor_next (c)) | |
908a2235 PT |
5043 | { |
5044 | if (gfc_traverse_expr (c->expr, sym, func, f)) | |
5045 | return true; | |
5046 | if (c->iterator) | |
5047 | { | |
5048 | if (gfc_traverse_expr (c->iterator->var, sym, func, f)) | |
5049 | return true; | |
5050 | if (gfc_traverse_expr (c->iterator->start, sym, func, f)) | |
5051 | return true; | |
5052 | if (gfc_traverse_expr (c->iterator->end, sym, func, f)) | |
5053 | return true; | |
5054 | if (gfc_traverse_expr (c->iterator->step, sym, func, f)) | |
5055 | return true; | |
5056 | } | |
5057 | } | |
47992a4a EE |
5058 | break; |
5059 | ||
640670c7 PT |
5060 | case EXPR_OP: |
5061 | if (gfc_traverse_expr (expr->value.op.op1, sym, func, f)) | |
5062 | return true; | |
5063 | if (gfc_traverse_expr (expr->value.op.op2, sym, func, f)) | |
5064 | return true; | |
5065 | break; | |
5066 | ||
47992a4a EE |
5067 | default: |
5068 | gcc_unreachable (); | |
5069 | break; | |
5070 | } | |
5071 | ||
640670c7 PT |
5072 | ref = expr->ref; |
5073 | while (ref != NULL) | |
5074 | { | |
47992a4a | 5075 | switch (ref->type) |
636dff67 | 5076 | { |
640670c7 PT |
5077 | case REF_ARRAY: |
5078 | ar = ref->u.ar; | |
5079 | for (i = 0; i < GFC_MAX_DIMENSIONS; i++) | |
636dff67 | 5080 | { |
640670c7 PT |
5081 | if (gfc_traverse_expr (ar.start[i], sym, func, f)) |
5082 | return true; | |
5083 | if (gfc_traverse_expr (ar.end[i], sym, func, f)) | |
5084 | return true; | |
5085 | if (gfc_traverse_expr (ar.stride[i], sym, func, f)) | |
5086 | return true; | |
636dff67 SK |
5087 | } |
5088 | break; | |
640670c7 | 5089 | |
636dff67 | 5090 | case REF_SUBSTRING: |
640670c7 PT |
5091 | if (gfc_traverse_expr (ref->u.ss.start, sym, func, f)) |
5092 | return true; | |
5093 | if (gfc_traverse_expr (ref->u.ss.end, sym, func, f)) | |
5094 | return true; | |
636dff67 | 5095 | break; |
640670c7 | 5096 | |
908a2235 PT |
5097 | case REF_COMPONENT: |
5098 | if (ref->u.c.component->ts.type == BT_CHARACTER | |
bc21d315 JW |
5099 | && ref->u.c.component->ts.u.cl |
5100 | && ref->u.c.component->ts.u.cl->length | |
5101 | && ref->u.c.component->ts.u.cl->length->expr_type | |
908a2235 | 5102 | != EXPR_CONSTANT |
bc21d315 | 5103 | && gfc_traverse_expr (ref->u.c.component->ts.u.cl->length, |
908a2235 PT |
5104 | sym, func, f)) |
5105 | return true; | |
5106 | ||
5107 | if (ref->u.c.component->as) | |
d3a9eea2 TB |
5108 | for (i = 0; i < ref->u.c.component->as->rank |
5109 | + ref->u.c.component->as->corank; i++) | |
908a2235 PT |
5110 | { |
5111 | if (gfc_traverse_expr (ref->u.c.component->as->lower[i], | |
5112 | sym, func, f)) | |
5113 | return true; | |
5114 | if (gfc_traverse_expr (ref->u.c.component->as->upper[i], | |
5115 | sym, func, f)) | |
5116 | return true; | |
5117 | } | |
5118 | break; | |
640670c7 | 5119 | |
a5fbc2f3 PT |
5120 | case REF_INQUIRY: |
5121 | return true; | |
5122 | ||
636dff67 SK |
5123 | default: |
5124 | gcc_unreachable (); | |
636dff67 | 5125 | } |
640670c7 PT |
5126 | ref = ref->next; |
5127 | } | |
5128 | return false; | |
5129 | } | |
5130 | ||
5131 | /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */ | |
5132 | ||
5133 | static bool | |
5134 | expr_set_symbols_referenced (gfc_expr *expr, | |
5135 | gfc_symbol *sym ATTRIBUTE_UNUSED, | |
5136 | int *f ATTRIBUTE_UNUSED) | |
5137 | { | |
908a2235 PT |
5138 | if (expr->expr_type != EXPR_VARIABLE) |
5139 | return false; | |
640670c7 PT |
5140 | gfc_set_sym_referenced (expr->symtree->n.sym); |
5141 | return false; | |
5142 | } | |
5143 | ||
5144 | void | |
5145 | gfc_expr_set_symbols_referenced (gfc_expr *expr) | |
5146 | { | |
5147 | gfc_traverse_expr (expr, NULL, expr_set_symbols_referenced, 0); | |
47992a4a | 5148 | } |
f37e928c DK |
5149 | |
5150 | ||
2a573572 MM |
5151 | /* Determine if an expression is a procedure pointer component and return |
5152 | the component in that case. Otherwise return NULL. */ | |
713485cc | 5153 | |
2a573572 MM |
5154 | gfc_component * |
5155 | gfc_get_proc_ptr_comp (gfc_expr *expr) | |
713485cc JW |
5156 | { |
5157 | gfc_ref *ref; | |
713485cc JW |
5158 | |
5159 | if (!expr || !expr->ref) | |
2a573572 | 5160 | return NULL; |
713485cc JW |
5161 | |
5162 | ref = expr->ref; | |
5163 | while (ref->next) | |
5164 | ref = ref->next; | |
5165 | ||
2a573572 MM |
5166 | if (ref->type == REF_COMPONENT |
5167 | && ref->u.c.component->attr.proc_pointer) | |
5168 | return ref->u.c.component; | |
5169 | ||
5170 | return NULL; | |
5171 | } | |
5172 | ||
713485cc | 5173 | |
2a573572 MM |
5174 | /* Determine if an expression is a procedure pointer component. */ |
5175 | ||
5176 | bool | |
5177 | gfc_is_proc_ptr_comp (gfc_expr *expr) | |
5178 | { | |
5179 | return (gfc_get_proc_ptr_comp (expr) != NULL); | |
713485cc JW |
5180 | } |
5181 | ||
5182 | ||
43a68a9d PT |
5183 | /* Determine if an expression is a function with an allocatable class scalar |
5184 | result. */ | |
5185 | bool | |
5186 | gfc_is_alloc_class_scalar_function (gfc_expr *expr) | |
5187 | { | |
5188 | if (expr->expr_type == EXPR_FUNCTION | |
5189 | && expr->value.function.esym | |
5190 | && expr->value.function.esym->result | |
5191 | && expr->value.function.esym->result->ts.type == BT_CLASS | |
5192 | && !CLASS_DATA (expr->value.function.esym->result)->attr.dimension | |
5193 | && CLASS_DATA (expr->value.function.esym->result)->attr.allocatable) | |
5194 | return true; | |
5195 | ||
5196 | return false; | |
5197 | } | |
5198 | ||
5199 | ||
5200 | /* Determine if an expression is a function with an allocatable class array | |
5201 | result. */ | |
5202 | bool | |
a6b22eea | 5203 | gfc_is_class_array_function (gfc_expr *expr) |
43a68a9d PT |
5204 | { |
5205 | if (expr->expr_type == EXPR_FUNCTION | |
5206 | && expr->value.function.esym | |
5207 | && expr->value.function.esym->result | |
5208 | && expr->value.function.esym->result->ts.type == BT_CLASS | |
5209 | && CLASS_DATA (expr->value.function.esym->result)->attr.dimension | |
a6b22eea PT |
5210 | && (CLASS_DATA (expr->value.function.esym->result)->attr.allocatable |
5211 | || CLASS_DATA (expr->value.function.esym->result)->attr.pointer)) | |
43a68a9d PT |
5212 | return true; |
5213 | ||
5214 | return false; | |
5215 | } | |
5216 | ||
5217 | ||
f37e928c DK |
5218 | /* Walk an expression tree and check each variable encountered for being typed. |
5219 | If strict is not set, a top-level variable is tolerated untyped in -std=gnu | |
ed42adef DK |
5220 | mode as is a basic arithmetic expression using those; this is for things in |
5221 | legacy-code like: | |
f37e928c DK |
5222 | |
5223 | INTEGER :: arr(n), n | |
ed42adef | 5224 | INTEGER :: arr(n + 1), n |
f37e928c DK |
5225 | |
5226 | The namespace is needed for IMPLICIT typing. */ | |
5227 | ||
3df684e2 DK |
5228 | static gfc_namespace* check_typed_ns; |
5229 | ||
5230 | static bool | |
5231 | expr_check_typed_help (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED, | |
5232 | int* f ATTRIBUTE_UNUSED) | |
f37e928c | 5233 | { |
524af0d6 | 5234 | bool t; |
f37e928c | 5235 | |
3df684e2 DK |
5236 | if (e->expr_type != EXPR_VARIABLE) |
5237 | return false; | |
f37e928c | 5238 | |
3df684e2 DK |
5239 | gcc_assert (e->symtree); |
5240 | t = gfc_check_symbol_typed (e->symtree->n.sym, check_typed_ns, | |
5241 | true, e->where); | |
f37e928c | 5242 | |
524af0d6 | 5243 | return (!t); |
3df684e2 | 5244 | } |
f37e928c | 5245 | |
524af0d6 | 5246 | bool |
3df684e2 DK |
5247 | gfc_expr_check_typed (gfc_expr* e, gfc_namespace* ns, bool strict) |
5248 | { | |
5249 | bool error_found; | |
f37e928c | 5250 | |
ed42adef DK |
5251 | /* If this is a top-level variable or EXPR_OP, do the check with strict given |
5252 | to us. */ | |
5253 | if (!strict) | |
5254 | { | |
5255 | if (e->expr_type == EXPR_VARIABLE && !e->ref) | |
5256 | return gfc_check_symbol_typed (e->symtree->n.sym, ns, strict, e->where); | |
5257 | ||
5258 | if (e->expr_type == EXPR_OP) | |
5259 | { | |
524af0d6 | 5260 | bool t = true; |
ed42adef DK |
5261 | |
5262 | gcc_assert (e->value.op.op1); | |
5263 | t = gfc_expr_check_typed (e->value.op.op1, ns, strict); | |
5264 | ||
524af0d6 | 5265 | if (t && e->value.op.op2) |
ed42adef DK |
5266 | t = gfc_expr_check_typed (e->value.op.op2, ns, strict); |
5267 | ||
5268 | return t; | |
5269 | } | |
5270 | } | |
f37e928c | 5271 | |
3df684e2 DK |
5272 | /* Otherwise, walk the expression and do it strictly. */ |
5273 | check_typed_ns = ns; | |
5274 | error_found = gfc_traverse_expr (e, NULL, &expr_check_typed_help, 0); | |
f37e928c | 5275 | |
524af0d6 | 5276 | return error_found ? false : true; |
f37e928c | 5277 | } |
c6acea9d | 5278 | |
4e5d3db2 | 5279 | |
5bab4c96 PT |
5280 | /* This function returns true if it contains any references to PDT KIND |
5281 | or LEN parameters. */ | |
5282 | ||
5283 | static bool | |
5284 | derived_parameter_expr (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED, | |
5285 | int* f ATTRIBUTE_UNUSED) | |
5286 | { | |
5287 | if (e->expr_type != EXPR_VARIABLE) | |
5288 | return false; | |
5289 | ||
5290 | gcc_assert (e->symtree); | |
5291 | if (e->symtree->n.sym->attr.pdt_kind | |
5292 | || e->symtree->n.sym->attr.pdt_len) | |
5293 | return true; | |
5294 | ||
5295 | return false; | |
5296 | } | |
5297 | ||
5298 | ||
5299 | bool | |
5300 | gfc_derived_parameter_expr (gfc_expr *e) | |
5301 | { | |
5302 | return gfc_traverse_expr (e, NULL, &derived_parameter_expr, 0); | |
5303 | } | |
5304 | ||
5305 | ||
5306 | /* This function returns the overall type of a type parameter spec list. | |
5307 | If all the specs are explicit, SPEC_EXPLICIT is returned. If any of the | |
5308 | parameters are assumed/deferred then SPEC_ASSUMED/DEFERRED is returned | |
5309 | unless derived is not NULL. In this latter case, all the LEN parameters | |
5310 | must be either assumed or deferred for the return argument to be set to | |
5311 | anything other than SPEC_EXPLICIT. */ | |
5312 | ||
5313 | gfc_param_spec_type | |
5314 | gfc_spec_list_type (gfc_actual_arglist *param_list, gfc_symbol *derived) | |
5315 | { | |
5316 | gfc_param_spec_type res = SPEC_EXPLICIT; | |
5317 | gfc_component *c; | |
5318 | bool seen_assumed = false; | |
5319 | bool seen_deferred = false; | |
5320 | ||
5321 | if (derived == NULL) | |
5322 | { | |
5323 | for (; param_list; param_list = param_list->next) | |
5324 | if (param_list->spec_type == SPEC_ASSUMED | |
5325 | || param_list->spec_type == SPEC_DEFERRED) | |
5326 | return param_list->spec_type; | |
5327 | } | |
5328 | else | |
5329 | { | |
5330 | for (; param_list; param_list = param_list->next) | |
5331 | { | |
5332 | c = gfc_find_component (derived, param_list->name, | |
5333 | true, true, NULL); | |
5334 | gcc_assert (c != NULL); | |
5335 | if (c->attr.pdt_kind) | |
5336 | continue; | |
5337 | else if (param_list->spec_type == SPEC_EXPLICIT) | |
5338 | return SPEC_EXPLICIT; | |
5339 | seen_assumed = param_list->spec_type == SPEC_ASSUMED; | |
5340 | seen_deferred = param_list->spec_type == SPEC_DEFERRED; | |
5341 | if (seen_assumed && seen_deferred) | |
5342 | return SPEC_EXPLICIT; | |
5343 | } | |
5344 | res = seen_assumed ? SPEC_ASSUMED : SPEC_DEFERRED; | |
5345 | } | |
5346 | return res; | |
5347 | } | |
5348 | ||
5349 | ||
badd9e69 TB |
5350 | bool |
5351 | gfc_ref_this_image (gfc_ref *ref) | |
5352 | { | |
5353 | int n; | |
5354 | ||
5355 | gcc_assert (ref->type == REF_ARRAY && ref->u.ar.codimen > 0); | |
5356 | ||
5357 | for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++) | |
5358 | if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE) | |
5359 | return false; | |
5360 | ||
5361 | return true; | |
5362 | } | |
5363 | ||
20d0bfce | 5364 | gfc_expr * |
f8862a1b DR |
5365 | gfc_find_team_co (gfc_expr *e) |
5366 | { | |
5367 | gfc_ref *ref; | |
5368 | ||
5369 | for (ref = e->ref; ref; ref = ref->next) | |
5370 | if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0) | |
5371 | return ref->u.ar.team; | |
5372 | ||
5373 | if (e->value.function.actual->expr) | |
5374 | for (ref = e->value.function.actual->expr->ref; ref; | |
5375 | ref = ref->next) | |
5376 | if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0) | |
5377 | return ref->u.ar.team; | |
5378 | ||
5379 | return NULL; | |
5380 | } | |
5381 | ||
5382 | gfc_expr * | |
5383 | gfc_find_stat_co (gfc_expr *e) | |
20d0bfce AF |
5384 | { |
5385 | gfc_ref *ref; | |
5386 | ||
5387 | for (ref = e->ref; ref; ref = ref->next) | |
5388 | if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0) | |
5389 | return ref->u.ar.stat; | |
5390 | ||
4971dd80 AV |
5391 | if (e->value.function.actual->expr) |
5392 | for (ref = e->value.function.actual->expr->ref; ref; | |
5393 | ref = ref->next) | |
20d0bfce AF |
5394 | if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0) |
5395 | return ref->u.ar.stat; | |
5396 | ||
5397 | return NULL; | |
5398 | } | |
badd9e69 | 5399 | |
d3a9eea2 TB |
5400 | bool |
5401 | gfc_is_coindexed (gfc_expr *e) | |
5402 | { | |
5403 | gfc_ref *ref; | |
5404 | ||
5405 | for (ref = e->ref; ref; ref = ref->next) | |
5406 | if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0) | |
badd9e69 | 5407 | return !gfc_ref_this_image (ref); |
d3a9eea2 TB |
5408 | |
5409 | return false; | |
5410 | } | |
5411 | ||
5412 | ||
394d3a2e TB |
5413 | /* Coarrays are variables with a corank but not being coindexed. However, also |
5414 | the following is a coarray: A subobject of a coarray is a coarray if it does | |
5415 | not have any cosubscripts, vector subscripts, allocatable component | |
5416 | selection, or pointer component selection. (F2008, 2.4.7) */ | |
5417 | ||
5418 | bool | |
5419 | gfc_is_coarray (gfc_expr *e) | |
5420 | { | |
5421 | gfc_ref *ref; | |
5422 | gfc_symbol *sym; | |
5423 | gfc_component *comp; | |
5424 | bool coindexed; | |
5425 | bool coarray; | |
5426 | int i; | |
5427 | ||
5428 | if (e->expr_type != EXPR_VARIABLE) | |
5429 | return false; | |
5430 | ||
5431 | coindexed = false; | |
5432 | sym = e->symtree->n.sym; | |
5433 | ||
5434 | if (sym->ts.type == BT_CLASS && sym->attr.class_ok) | |
5435 | coarray = CLASS_DATA (sym)->attr.codimension; | |
5436 | else | |
5437 | coarray = sym->attr.codimension; | |
5438 | ||
5439 | for (ref = e->ref; ref; ref = ref->next) | |
5440 | switch (ref->type) | |
5441 | { | |
5442 | case REF_COMPONENT: | |
5443 | comp = ref->u.c.component; | |
5d81ddd0 TB |
5444 | if (comp->ts.type == BT_CLASS && comp->attr.class_ok |
5445 | && (CLASS_DATA (comp)->attr.class_pointer | |
5446 | || CLASS_DATA (comp)->attr.allocatable)) | |
394d3a2e TB |
5447 | { |
5448 | coindexed = false; | |
5d81ddd0 TB |
5449 | coarray = CLASS_DATA (comp)->attr.codimension; |
5450 | } | |
5451 | else if (comp->attr.pointer || comp->attr.allocatable) | |
5452 | { | |
5453 | coindexed = false; | |
5454 | coarray = comp->attr.codimension; | |
394d3a2e TB |
5455 | } |
5456 | break; | |
5457 | ||
5458 | case REF_ARRAY: | |
5459 | if (!coarray) | |
5460 | break; | |
5461 | ||
5462 | if (ref->u.ar.codimen > 0 && !gfc_ref_this_image (ref)) | |
5463 | { | |
5464 | coindexed = true; | |
5465 | break; | |
5466 | } | |
5467 | ||
5468 | for (i = 0; i < ref->u.ar.dimen; i++) | |
5469 | if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR) | |
5470 | { | |
5471 | coarray = false; | |
5472 | break; | |
5473 | } | |
5474 | break; | |
5475 | ||
5476 | case REF_SUBSTRING: | |
a5fbc2f3 | 5477 | case REF_INQUIRY: |
394d3a2e TB |
5478 | break; |
5479 | } | |
5480 | ||
5481 | return coarray && !coindexed; | |
5482 | } | |
5483 | ||
5484 | ||
4dc694b2 | 5485 | int |
7aa0849a TB |
5486 | gfc_get_corank (gfc_expr *e) |
5487 | { | |
5488 | int corank; | |
5489 | gfc_ref *ref; | |
89010691 MM |
5490 | |
5491 | if (!gfc_is_coarray (e)) | |
5492 | return 0; | |
5493 | ||
c49ea23d PT |
5494 | if (e->ts.type == BT_CLASS && e->ts.u.derived->components) |
5495 | corank = e->ts.u.derived->components->as | |
5496 | ? e->ts.u.derived->components->as->corank : 0; | |
8b704316 | 5497 | else |
c49ea23d | 5498 | corank = e->symtree->n.sym->as ? e->symtree->n.sym->as->corank : 0; |
89010691 | 5499 | |
7aa0849a TB |
5500 | for (ref = e->ref; ref; ref = ref->next) |
5501 | { | |
5502 | if (ref->type == REF_ARRAY) | |
5503 | corank = ref->u.ar.as->corank; | |
5504 | gcc_assert (ref->type != REF_SUBSTRING); | |
5505 | } | |
89010691 | 5506 | |
7aa0849a TB |
5507 | return corank; |
5508 | } | |
5509 | ||
5510 | ||
d3a9eea2 TB |
5511 | /* Check whether the expression has an ultimate allocatable component. |
5512 | Being itself allocatable does not count. */ | |
5513 | bool | |
5514 | gfc_has_ultimate_allocatable (gfc_expr *e) | |
5515 | { | |
5516 | gfc_ref *ref, *last = NULL; | |
5517 | ||
5518 | if (e->expr_type != EXPR_VARIABLE) | |
5519 | return false; | |
5520 | ||
5521 | for (ref = e->ref; ref; ref = ref->next) | |
5522 | if (ref->type == REF_COMPONENT) | |
5523 | last = ref; | |
5524 | ||
5525 | if (last && last->u.c.component->ts.type == BT_CLASS) | |
7a08eda1 | 5526 | return CLASS_DATA (last->u.c.component)->attr.alloc_comp; |
d3a9eea2 TB |
5527 | else if (last && last->u.c.component->ts.type == BT_DERIVED) |
5528 | return last->u.c.component->ts.u.derived->attr.alloc_comp; | |
5529 | else if (last) | |
5530 | return false; | |
5531 | ||
5532 | if (e->ts.type == BT_CLASS) | |
7a08eda1 | 5533 | return CLASS_DATA (e)->attr.alloc_comp; |
d3a9eea2 TB |
5534 | else if (e->ts.type == BT_DERIVED) |
5535 | return e->ts.u.derived->attr.alloc_comp; | |
5536 | else | |
5537 | return false; | |
5538 | } | |
5539 | ||
5540 | ||
5541 | /* Check whether the expression has an pointer component. | |
5542 | Being itself a pointer does not count. */ | |
5543 | bool | |
5544 | gfc_has_ultimate_pointer (gfc_expr *e) | |
5545 | { | |
5546 | gfc_ref *ref, *last = NULL; | |
5547 | ||
5548 | if (e->expr_type != EXPR_VARIABLE) | |
5549 | return false; | |
5550 | ||
5551 | for (ref = e->ref; ref; ref = ref->next) | |
5552 | if (ref->type == REF_COMPONENT) | |
5553 | last = ref; | |
8b704316 | 5554 | |
d3a9eea2 | 5555 | if (last && last->u.c.component->ts.type == BT_CLASS) |
7a08eda1 | 5556 | return CLASS_DATA (last->u.c.component)->attr.pointer_comp; |
d3a9eea2 TB |
5557 | else if (last && last->u.c.component->ts.type == BT_DERIVED) |
5558 | return last->u.c.component->ts.u.derived->attr.pointer_comp; | |
5559 | else if (last) | |
5560 | return false; | |
5561 | ||
5562 | if (e->ts.type == BT_CLASS) | |
7a08eda1 | 5563 | return CLASS_DATA (e)->attr.pointer_comp; |
d3a9eea2 TB |
5564 | else if (e->ts.type == BT_DERIVED) |
5565 | return e->ts.u.derived->attr.pointer_comp; | |
5566 | else | |
5567 | return false; | |
5568 | } | |
fe4e525c TB |
5569 | |
5570 | ||
5571 | /* Check whether an expression is "simply contiguous", cf. F2008, 6.5.4. | |
5572 | Note: A scalar is not regarded as "simply contiguous" by the standard. | |
eea58adb | 5573 | if bool is not strict, some further checks are done - for instance, |
fe4e525c TB |
5574 | a "(::1)" is accepted. */ |
5575 | ||
5576 | bool | |
460263d0 | 5577 | gfc_is_simply_contiguous (gfc_expr *expr, bool strict, bool permit_element) |
fe4e525c TB |
5578 | { |
5579 | bool colon; | |
5580 | int i; | |
5581 | gfc_array_ref *ar = NULL; | |
5582 | gfc_ref *ref, *part_ref = NULL; | |
c49ea23d | 5583 | gfc_symbol *sym; |
fe4e525c TB |
5584 | |
5585 | if (expr->expr_type == EXPR_FUNCTION) | |
a814e35b TK |
5586 | { |
5587 | if (expr->value.function.esym) | |
5588 | return expr->value.function.esym->result->attr.contiguous; | |
5589 | else | |
5590 | { | |
f5da9bfb JW |
5591 | /* Type-bound procedures. */ |
5592 | gfc_symbol *s = expr->symtree->n.sym; | |
5593 | if (s->ts.type != BT_CLASS && s->ts.type != BT_DERIVED) | |
a814e35b | 5594 | return false; |
87c789f1 | 5595 | |
f5da9bfb JW |
5596 | gfc_ref *rc = NULL; |
5597 | for (gfc_ref *r = expr->ref; r; r = r->next) | |
a814e35b TK |
5598 | if (r->type == REF_COMPONENT) |
5599 | rc = r; | |
5600 | ||
5601 | if (rc == NULL || rc->u.c.component == NULL | |
5602 | || rc->u.c.component->ts.interface == NULL) | |
5603 | return false; | |
5604 | ||
5605 | return rc->u.c.component->ts.interface->attr.contiguous; | |
5606 | } | |
5607 | } | |
fe4e525c TB |
5608 | else if (expr->expr_type != EXPR_VARIABLE) |
5609 | return false; | |
5610 | ||
460263d0 | 5611 | if (!permit_element && expr->rank == 0) |
fe4e525c TB |
5612 | return false; |
5613 | ||
5614 | for (ref = expr->ref; ref; ref = ref->next) | |
5615 | { | |
5616 | if (ar) | |
1cc0e193 | 5617 | return false; /* Array shall be last part-ref. */ |
fe4e525c TB |
5618 | |
5619 | if (ref->type == REF_COMPONENT) | |
5620 | part_ref = ref; | |
5621 | else if (ref->type == REF_SUBSTRING) | |
5622 | return false; | |
5623 | else if (ref->u.ar.type != AR_ELEMENT) | |
5624 | ar = &ref->u.ar; | |
5625 | } | |
5626 | ||
c49ea23d PT |
5627 | sym = expr->symtree->n.sym; |
5628 | if (expr->ts.type != BT_CLASS | |
d4319ef8 SK |
5629 | && ((part_ref |
5630 | && !part_ref->u.c.component->attr.contiguous | |
5631 | && part_ref->u.c.component->attr.pointer) | |
5632 | || (!part_ref | |
5633 | && !sym->attr.contiguous | |
5634 | && (sym->attr.pointer | |
5635 | || (sym->as && sym->as->type == AS_ASSUMED_RANK) | |
5636 | || (sym->as && sym->as->type == AS_ASSUMED_SHAPE))))) | |
fe4e525c TB |
5637 | return false; |
5638 | ||
5639 | if (!ar || ar->type == AR_FULL) | |
5640 | return true; | |
5641 | ||
5642 | gcc_assert (ar->type == AR_SECTION); | |
5643 | ||
5644 | /* Check for simply contiguous array */ | |
5645 | colon = true; | |
5646 | for (i = 0; i < ar->dimen; i++) | |
5647 | { | |
5648 | if (ar->dimen_type[i] == DIMEN_VECTOR) | |
5649 | return false; | |
5650 | ||
5651 | if (ar->dimen_type[i] == DIMEN_ELEMENT) | |
5652 | { | |
5653 | colon = false; | |
5654 | continue; | |
5655 | } | |
5656 | ||
5657 | gcc_assert (ar->dimen_type[i] == DIMEN_RANGE); | |
5658 | ||
5659 | ||
5660 | /* If the previous section was not contiguous, that's an error, | |
5661 | unless we have effective only one element and checking is not | |
5662 | strict. */ | |
5663 | if (!colon && (strict || !ar->start[i] || !ar->end[i] | |
5664 | || ar->start[i]->expr_type != EXPR_CONSTANT | |
5665 | || ar->end[i]->expr_type != EXPR_CONSTANT | |
5666 | || mpz_cmp (ar->start[i]->value.integer, | |
5667 | ar->end[i]->value.integer) != 0)) | |
5668 | return false; | |
5669 | ||
5670 | /* Following the standard, "(::1)" or - if known at compile time - | |
eea58adb | 5671 | "(lbound:ubound)" are not simply contiguous; if strict |
fe4e525c TB |
5672 | is false, they are regarded as simply contiguous. */ |
5673 | if (ar->stride[i] && (strict || ar->stride[i]->expr_type != EXPR_CONSTANT | |
5674 | || ar->stride[i]->ts.type != BT_INTEGER | |
5675 | || mpz_cmp_si (ar->stride[i]->value.integer, 1) != 0)) | |
5676 | return false; | |
5677 | ||
5678 | if (ar->start[i] | |
5679 | && (strict || ar->start[i]->expr_type != EXPR_CONSTANT | |
5680 | || !ar->as->lower[i] | |
5681 | || ar->as->lower[i]->expr_type != EXPR_CONSTANT | |
5682 | || mpz_cmp (ar->start[i]->value.integer, | |
5683 | ar->as->lower[i]->value.integer) != 0)) | |
5684 | colon = false; | |
5685 | ||
5686 | if (ar->end[i] | |
5687 | && (strict || ar->end[i]->expr_type != EXPR_CONSTANT | |
5688 | || !ar->as->upper[i] | |
5689 | || ar->as->upper[i]->expr_type != EXPR_CONSTANT | |
5690 | || mpz_cmp (ar->end[i]->value.integer, | |
5691 | ar->as->upper[i]->value.integer) != 0)) | |
5692 | colon = false; | |
5693 | } | |
8b704316 | 5694 | |
fe4e525c TB |
5695 | return true; |
5696 | } | |
69dcd06a DK |
5697 | |
5698 | ||
5699 | /* Build call to an intrinsic procedure. The number of arguments has to be | |
5700 | passed (rather than ending the list with a NULL value) because we may | |
5701 | want to add arguments but with a NULL-expression. */ | |
5702 | ||
5703 | gfc_expr* | |
6838c137 TB |
5704 | gfc_build_intrinsic_call (gfc_namespace *ns, gfc_isym_id id, const char* name, |
5705 | locus where, unsigned numarg, ...) | |
69dcd06a DK |
5706 | { |
5707 | gfc_expr* result; | |
5708 | gfc_actual_arglist* atail; | |
5709 | gfc_intrinsic_sym* isym; | |
5710 | va_list ap; | |
5711 | unsigned i; | |
6838c137 | 5712 | const char *mangled_name = gfc_get_string (GFC_PREFIX ("%s"), name); |
69dcd06a | 5713 | |
6838c137 | 5714 | isym = gfc_intrinsic_function_by_id (id); |
69dcd06a | 5715 | gcc_assert (isym); |
8b704316 | 5716 | |
69dcd06a DK |
5717 | result = gfc_get_expr (); |
5718 | result->expr_type = EXPR_FUNCTION; | |
5719 | result->ts = isym->ts; | |
5720 | result->where = where; | |
6838c137 | 5721 | result->value.function.name = mangled_name; |
69dcd06a DK |
5722 | result->value.function.isym = isym; |
5723 | ||
6838c137 TB |
5724 | gfc_get_sym_tree (mangled_name, ns, &result->symtree, false); |
5725 | gfc_commit_symbol (result->symtree->n.sym); | |
8e19c582 TB |
5726 | gcc_assert (result->symtree |
5727 | && (result->symtree->n.sym->attr.flavor == FL_PROCEDURE | |
5728 | || result->symtree->n.sym->attr.flavor == FL_UNKNOWN)); | |
6838c137 TB |
5729 | result->symtree->n.sym->intmod_sym_id = id; |
5730 | result->symtree->n.sym->attr.flavor = FL_PROCEDURE; | |
5731 | result->symtree->n.sym->attr.intrinsic = 1; | |
9606f3c9 | 5732 | result->symtree->n.sym->attr.artificial = 1; |
8e19c582 | 5733 | |
69dcd06a DK |
5734 | va_start (ap, numarg); |
5735 | atail = NULL; | |
5736 | for (i = 0; i < numarg; ++i) | |
5737 | { | |
5738 | if (atail) | |
5739 | { | |
5740 | atail->next = gfc_get_actual_arglist (); | |
5741 | atail = atail->next; | |
5742 | } | |
5743 | else | |
5744 | atail = result->value.function.actual = gfc_get_actual_arglist (); | |
5745 | ||
5746 | atail->expr = va_arg (ap, gfc_expr*); | |
5747 | } | |
5748 | va_end (ap); | |
5749 | ||
5750 | return result; | |
5751 | } | |
8c91ab34 DK |
5752 | |
5753 | ||
5754 | /* Check if an expression may appear in a variable definition context | |
5755 | (F2008, 16.6.7) or pointer association context (F2008, 16.6.8). | |
5756 | This is called from the various places when resolving | |
5757 | the pieces that make up such a context. | |
57bf28ea TB |
5758 | If own_scope is true (applies to, e.g., ac-implied-do/data-implied-do |
5759 | variables), some checks are not performed. | |
8c91ab34 DK |
5760 | |
5761 | Optionally, a possible error message can be suppressed if context is NULL | |
524af0d6 | 5762 | and just the return status (true / false) be requested. */ |
8c91ab34 | 5763 | |
524af0d6 | 5764 | bool |
fea54935 | 5765 | gfc_check_vardef_context (gfc_expr* e, bool pointer, bool alloc_obj, |
57bf28ea | 5766 | bool own_scope, const char* context) |
8c91ab34 | 5767 | { |
a300121e | 5768 | gfc_symbol* sym = NULL; |
8c91ab34 DK |
5769 | bool is_pointer; |
5770 | bool check_intentin; | |
5771 | bool ptr_component; | |
5772 | symbol_attribute attr; | |
5773 | gfc_ref* ref; | |
e2679323 | 5774 | int i; |
8c91ab34 | 5775 | |
a300121e TB |
5776 | if (e->expr_type == EXPR_VARIABLE) |
5777 | { | |
5778 | gcc_assert (e->symtree); | |
5779 | sym = e->symtree->n.sym; | |
5780 | } | |
5781 | else if (e->expr_type == EXPR_FUNCTION) | |
5782 | { | |
5783 | gcc_assert (e->symtree); | |
5784 | sym = e->value.function.esym ? e->value.function.esym : e->symtree->n.sym; | |
5785 | } | |
5786 | ||
49860194 JW |
5787 | attr = gfc_expr_attr (e); |
5788 | if (!pointer && e->expr_type == EXPR_FUNCTION && attr.pointer) | |
9b565d65 TB |
5789 | { |
5790 | if (!(gfc_option.allow_std & GFC_STD_F2008)) | |
5791 | { | |
5792 | if (context) | |
5793 | gfc_error ("Fortran 2008: Pointer functions in variable definition" | |
5794 | " context (%s) at %L", context, &e->where); | |
524af0d6 | 5795 | return false; |
9b565d65 TB |
5796 | } |
5797 | } | |
5798 | else if (e->expr_type != EXPR_VARIABLE) | |
8c91ab34 DK |
5799 | { |
5800 | if (context) | |
5801 | gfc_error ("Non-variable expression in variable definition context (%s)" | |
5802 | " at %L", context, &e->where); | |
524af0d6 | 5803 | return false; |
8c91ab34 DK |
5804 | } |
5805 | ||
8c91ab34 DK |
5806 | if (!pointer && sym->attr.flavor == FL_PARAMETER) |
5807 | { | |
5808 | if (context) | |
c4100eae | 5809 | gfc_error ("Named constant %qs in variable definition context (%s)" |
8c91ab34 | 5810 | " at %L", sym->name, context, &e->where); |
524af0d6 | 5811 | return false; |
8c91ab34 DK |
5812 | } |
5813 | if (!pointer && sym->attr.flavor != FL_VARIABLE | |
5814 | && !(sym->attr.flavor == FL_PROCEDURE && sym == sym->result) | |
5815 | && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.proc_pointer)) | |
5816 | { | |
5817 | if (context) | |
c4100eae | 5818 | gfc_error ("%qs in variable definition context (%s) at %L is not" |
8c91ab34 | 5819 | " a variable", sym->name, context, &e->where); |
524af0d6 | 5820 | return false; |
8c91ab34 DK |
5821 | } |
5822 | ||
5823 | /* Find out whether the expr is a pointer; this also means following | |
5824 | component references to the last one. */ | |
8c91ab34 | 5825 | is_pointer = (attr.pointer || attr.proc_pointer); |
f637ebc1 | 5826 | if (pointer && !is_pointer) |
8c91ab34 DK |
5827 | { |
5828 | if (context) | |
5829 | gfc_error ("Non-POINTER in pointer association context (%s)" | |
5830 | " at %L", context, &e->where); | |
524af0d6 | 5831 | return false; |
8c91ab34 DK |
5832 | } |
5833 | ||
79124116 PT |
5834 | if (e->ts.type == BT_DERIVED |
5835 | && e->ts.u.derived == NULL) | |
5836 | { | |
5837 | if (context) | |
5838 | gfc_error ("Type inaccessible in variable definition context (%s) " | |
5839 | "at %L", context, &e->where); | |
5840 | return false; | |
5841 | } | |
5842 | ||
fea54935 TB |
5843 | /* F2008, C1303. */ |
5844 | if (!alloc_obj | |
5845 | && (attr.lock_comp | |
5846 | || (e->ts.type == BT_DERIVED | |
5847 | && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV | |
5848 | && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE))) | |
5849 | { | |
5850 | if (context) | |
5851 | gfc_error ("LOCK_TYPE in variable definition context (%s) at %L", | |
5852 | context, &e->where); | |
524af0d6 | 5853 | return false; |
fea54935 TB |
5854 | } |
5855 | ||
5df445a2 TB |
5856 | /* TS18508, C702/C203. */ |
5857 | if (!alloc_obj | |
5858 | && (attr.lock_comp | |
5859 | || (e->ts.type == BT_DERIVED | |
5860 | && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV | |
5861 | && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE))) | |
5862 | { | |
5863 | if (context) | |
5864 | gfc_error ("LOCK_EVENT in variable definition context (%s) at %L", | |
5865 | context, &e->where); | |
5866 | return false; | |
5867 | } | |
5868 | ||
4a101681 TB |
5869 | /* INTENT(IN) dummy argument. Check this, unless the object itself is the |
5870 | component of sub-component of a pointer; we need to distinguish | |
5871 | assignment to a pointer component from pointer-assignment to a pointer | |
5872 | component. Note that (normal) assignment to procedure pointers is not | |
5873 | possible. */ | |
57bf28ea | 5874 | check_intentin = !own_scope; |
fba5a793 SK |
5875 | ptr_component = (sym->ts.type == BT_CLASS && sym->ts.u.derived |
5876 | && CLASS_DATA (sym)) | |
f18075ff | 5877 | ? CLASS_DATA (sym)->attr.class_pointer : sym->attr.pointer; |
8c91ab34 DK |
5878 | for (ref = e->ref; ref && check_intentin; ref = ref->next) |
5879 | { | |
5880 | if (ptr_component && ref->type == REF_COMPONENT) | |
5881 | check_intentin = false; | |
5882 | if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer) | |
4a101681 TB |
5883 | { |
5884 | ptr_component = true; | |
5885 | if (!pointer) | |
5886 | check_intentin = false; | |
5887 | } | |
8c91ab34 DK |
5888 | } |
5889 | if (check_intentin && sym->attr.intent == INTENT_IN) | |
5890 | { | |
5891 | if (pointer && is_pointer) | |
5892 | { | |
5893 | if (context) | |
c4100eae | 5894 | gfc_error ("Dummy argument %qs with INTENT(IN) in pointer" |
8c91ab34 DK |
5895 | " association context (%s) at %L", |
5896 | sym->name, context, &e->where); | |
524af0d6 | 5897 | return false; |
8c91ab34 | 5898 | } |
6fd7dd57 | 5899 | if (!pointer && !is_pointer && !sym->attr.pointer) |
8c91ab34 DK |
5900 | { |
5901 | if (context) | |
c4100eae | 5902 | gfc_error ("Dummy argument %qs with INTENT(IN) in variable" |
8c91ab34 DK |
5903 | " definition context (%s) at %L", |
5904 | sym->name, context, &e->where); | |
524af0d6 | 5905 | return false; |
8c91ab34 DK |
5906 | } |
5907 | } | |
5908 | ||
5909 | /* PROTECTED and use-associated. */ | |
57bf28ea | 5910 | if (sym->attr.is_protected && sym->attr.use_assoc && check_intentin) |
8c91ab34 DK |
5911 | { |
5912 | if (pointer && is_pointer) | |
5913 | { | |
5914 | if (context) | |
c4100eae | 5915 | gfc_error ("Variable %qs is PROTECTED and can not appear in a" |
8c91ab34 DK |
5916 | " pointer association context (%s) at %L", |
5917 | sym->name, context, &e->where); | |
524af0d6 | 5918 | return false; |
8c91ab34 DK |
5919 | } |
5920 | if (!pointer && !is_pointer) | |
5921 | { | |
5922 | if (context) | |
c4100eae | 5923 | gfc_error ("Variable %qs is PROTECTED and can not appear in a" |
8c91ab34 DK |
5924 | " variable definition context (%s) at %L", |
5925 | sym->name, context, &e->where); | |
524af0d6 | 5926 | return false; |
8c91ab34 DK |
5927 | } |
5928 | } | |
5929 | ||
5930 | /* Variable not assignable from a PURE procedure but appears in | |
5931 | variable definition context. */ | |
57bf28ea | 5932 | if (!pointer && !own_scope && gfc_pure (NULL) && gfc_impure_variable (sym)) |
8c91ab34 DK |
5933 | { |
5934 | if (context) | |
c4100eae | 5935 | gfc_error ("Variable %qs can not appear in a variable definition" |
8c91ab34 DK |
5936 | " context (%s) at %L in PURE procedure", |
5937 | sym->name, context, &e->where); | |
524af0d6 | 5938 | return false; |
8c91ab34 DK |
5939 | } |
5940 | ||
f29041dd TK |
5941 | if (!pointer && context && gfc_implicit_pure (NULL) |
5942 | && gfc_impure_variable (sym)) | |
5943 | { | |
5944 | gfc_namespace *ns; | |
5945 | gfc_symbol *sym; | |
f1f39033 | 5946 | |
f29041dd TK |
5947 | for (ns = gfc_current_ns; ns; ns = ns->parent) |
5948 | { | |
5949 | sym = ns->proc_name; | |
5950 | if (sym == NULL) | |
5951 | break; | |
5952 | if (sym->attr.flavor == FL_PROCEDURE) | |
5953 | { | |
5954 | sym->attr.implicit_pure = 0; | |
5955 | break; | |
5956 | } | |
5957 | } | |
5958 | } | |
8c91ab34 DK |
5959 | /* Check variable definition context for associate-names. */ |
5960 | if (!pointer && sym->assoc) | |
5961 | { | |
5962 | const char* name; | |
5963 | gfc_association_list* assoc; | |
5964 | ||
5965 | gcc_assert (sym->assoc->target); | |
5966 | ||
5967 | /* If this is a SELECT TYPE temporary (the association is used internally | |
5968 | for SELECT TYPE), silently go over to the target. */ | |
5969 | if (sym->attr.select_type_temporary) | |
5970 | { | |
5971 | gfc_expr* t = sym->assoc->target; | |
5972 | ||
5973 | gcc_assert (t->expr_type == EXPR_VARIABLE); | |
5974 | name = t->symtree->name; | |
5975 | ||
5976 | if (t->symtree->n.sym->assoc) | |
5977 | assoc = t->symtree->n.sym->assoc; | |
5978 | else | |
5979 | assoc = sym->assoc; | |
5980 | } | |
5981 | else | |
5982 | { | |
5983 | name = sym->name; | |
5984 | assoc = sym->assoc; | |
5985 | } | |
5986 | gcc_assert (name && assoc); | |
5987 | ||
5988 | /* Is association to a valid variable? */ | |
5989 | if (!assoc->variable) | |
5990 | { | |
5991 | if (context) | |
5992 | { | |
5993 | if (assoc->target->expr_type == EXPR_VARIABLE) | |
c4100eae | 5994 | gfc_error ("%qs at %L associated to vector-indexed target can" |
8c91ab34 DK |
5995 | " not be used in a variable definition context (%s)", |
5996 | name, &e->where, context); | |
5997 | else | |
c4100eae | 5998 | gfc_error ("%qs at %L associated to expression can" |
8c91ab34 DK |
5999 | " not be used in a variable definition context (%s)", |
6000 | name, &e->where, context); | |
6001 | } | |
524af0d6 | 6002 | return false; |
8c91ab34 DK |
6003 | } |
6004 | ||
6005 | /* Target must be allowed to appear in a variable definition context. */ | |
524af0d6 | 6006 | if (!gfc_check_vardef_context (assoc->target, pointer, false, false, NULL)) |
8c91ab34 DK |
6007 | { |
6008 | if (context) | |
fea70c99 | 6009 | gfc_error ("Associate-name %qs can not appear in a variable" |
8c91ab34 DK |
6010 | " definition context (%s) at %L because its target" |
6011 | " at %L can not, either", | |
6012 | name, context, &e->where, | |
6013 | &assoc->target->where); | |
524af0d6 | 6014 | return false; |
8c91ab34 DK |
6015 | } |
6016 | } | |
6017 | ||
e2679323 TK |
6018 | /* Check for same value in vector expression subscript. */ |
6019 | ||
6020 | if (e->rank > 0) | |
6021 | for (ref = e->ref; ref != NULL; ref = ref->next) | |
6022 | if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION) | |
6023 | for (i = 0; i < GFC_MAX_DIMENSIONS | |
6024 | && ref->u.ar.dimen_type[i] != 0; i++) | |
6025 | if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR) | |
6026 | { | |
6027 | gfc_expr *arr = ref->u.ar.start[i]; | |
6028 | if (arr->expr_type == EXPR_ARRAY) | |
6029 | { | |
6030 | gfc_constructor *c, *n; | |
6031 | gfc_expr *ec, *en; | |
bf9f15ee | 6032 | |
e2679323 TK |
6033 | for (c = gfc_constructor_first (arr->value.constructor); |
6034 | c != NULL; c = gfc_constructor_next (c)) | |
6035 | { | |
6036 | if (c == NULL || c->iterator != NULL) | |
6037 | continue; | |
bf9f15ee | 6038 | |
e2679323 TK |
6039 | ec = c->expr; |
6040 | ||
6041 | for (n = gfc_constructor_next (c); n != NULL; | |
6042 | n = gfc_constructor_next (n)) | |
6043 | { | |
6044 | if (n->iterator != NULL) | |
6045 | continue; | |
bf9f15ee | 6046 | |
e2679323 TK |
6047 | en = n->expr; |
6048 | if (gfc_dep_compare_expr (ec, en) == 0) | |
6049 | { | |
aa9ca5ca | 6050 | if (context) |
fea70c99 MLI |
6051 | gfc_error_now ("Elements with the same value " |
6052 | "at %L and %L in vector " | |
6053 | "subscript in a variable " | |
6054 | "definition context (%s)", | |
6055 | &(ec->where), &(en->where), | |
6056 | context); | |
e2679323 TK |
6057 | return false; |
6058 | } | |
6059 | } | |
6060 | } | |
6061 | } | |
6062 | } | |
bf9f15ee | 6063 | |
524af0d6 | 6064 | return true; |
8c91ab34 | 6065 | } |