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