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