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 1854 goto cleanup; 1855 1856 break; 1857 1858 case INQUIRY_KIND: 1859 if (tmp->ts.type == BT_DERIVED || tmp->ts.type == BT_CLASS) 1860 goto cleanup; 1861 1862 if (!gfc_notify_std (GFC_STD_F2003, "KIND part_ref at %C")) 1863 goto cleanup; 1864 1865 *newp = gfc_get_int_expr (gfc_default_integer_kind, 1866 NULL, tmp->ts.kind); 1867 break; 1868 1869 case INQUIRY_RE: 1870 if (tmp->ts.type != BT_COMPLEX || tmp->expr_type != EXPR_CONSTANT) 1871 goto cleanup; 1872 1873 if (!gfc_notify_std (GFC_STD_F2008, "RE part_ref at %C")) 1874 goto cleanup; 1875 1876 *newp = gfc_get_constant_expr (BT_REAL, tmp->ts.kind, &tmp->where); 1877 mpfr_set ((*newp)->value.real, 1878 mpc_realref (tmp->value.complex), GFC_RND_MODE); 1879 break; 1880 1881 case INQUIRY_IM: 1882 if (tmp->ts.type != BT_COMPLEX || tmp->expr_type != EXPR_CONSTANT) 1883 goto cleanup; 1884 1885 if (!gfc_notify_std (GFC_STD_F2008, "IM part_ref at %C")) 1886 goto cleanup; 1887 1888 *newp = gfc_get_constant_expr (BT_REAL, tmp->ts.kind, &tmp->where); 1889 mpfr_set ((*newp)->value.real, 1890 mpc_imagref (tmp->value.complex), GFC_RND_MODE); 1891 break; 1892 } 1893 tmp = gfc_copy_expr (*newp); 1894 } 1895 1896 if (!(*newp)) 1897 goto cleanup; 1898 else if ((*newp)->expr_type != EXPR_CONSTANT) 1899 { 1900 gfc_free_expr (*newp); 1901 goto cleanup; 1902 } 1903 1904 gfc_free_expr (tmp); 1905 return true; 1906 1907 cleanup: 1908 gfc_free_expr (tmp); 1909 return false; 1910 } 1911 1912 1913 1914 /* Simplify a subobject reference of a constructor. This occurs when 1915 parameter variable values are substituted. */ 1916 1917 static bool 1918 simplify_const_ref (gfc_expr *p) 1919 { 1920 gfc_constructor *cons, *c; 1921 gfc_expr *newp = NULL; 1922 gfc_ref *last_ref; 1923 1924 while (p->ref) 1925 { 1926 switch (p->ref->type) 1927 { 1928 case REF_ARRAY: 1929 switch (p->ref->u.ar.type) 1930 { 1931 case AR_ELEMENT: 1932 /* <type/kind spec>, parameter :: x(<int>) = scalar_expr 1933 will generate this. */ 1934 if (p->expr_type != EXPR_ARRAY) 1935 { 1936 remove_subobject_ref (p, NULL); 1937 break; 1938 } 1939 if (!find_array_element (p->value.constructor, &p->ref->u.ar, &cons)) 1940 return false; 1941 1942 if (!cons) 1943 return true; 1944 1945 remove_subobject_ref (p, cons); 1946 break; 1947 1948 case AR_SECTION: 1949 if (!find_array_section (p, p->ref)) 1950 return false; 1951 p->ref->u.ar.type = AR_FULL; 1952 1953 /* Fall through. */ 1954 1955 case AR_FULL: 1956 if (p->ref->next != NULL 1957 && (p->ts.type == BT_CHARACTER || gfc_bt_struct (p->ts.type))) 1958 { 1959 for (c = gfc_constructor_first (p->value.constructor); 1960 c; c = gfc_constructor_next (c)) 1961 { 1962 c->expr->ref = gfc_copy_ref (p->ref->next); 1963 if (!simplify_const_ref (c->expr)) 1964 return false; 1965 } 1966 1967 if (gfc_bt_struct (p->ts.type) 1968 && p->ref->next 1969 && (c = gfc_constructor_first (p->value.constructor))) 1970 { 1971 /* There may have been component references. */ 1972 p->ts = c->expr->ts; 1973 } 1974 1975 last_ref = p->ref; 1976 for (; last_ref->next; last_ref = last_ref->next) {}; 1977 1978 if (p->ts.type == BT_CHARACTER 1979 && last_ref->type == REF_SUBSTRING) 1980 { 1981 /* If this is a CHARACTER array and we possibly took 1982 a substring out of it, update the type-spec's 1983 character length according to the first element 1984 (as all should have the same length). */ 1985 gfc_charlen_t string_len; 1986 if ((c = gfc_constructor_first (p->value.constructor))) 1987 { 1988 const gfc_expr* first = c->expr; 1989 gcc_assert (first->expr_type == EXPR_CONSTANT); 1990 gcc_assert (first->ts.type == BT_CHARACTER); 1991 string_len = first->value.character.length; 1992 } 1993 else 1994 string_len = 0; 1995 1996 if (!p->ts.u.cl) 1997 { 1998 if (p->symtree) 1999 p->ts.u.cl = gfc_new_charlen (p->symtree->n.sym->ns, 2000 NULL); 2001 else 2002 p->ts.u.cl = gfc_new_charlen (gfc_current_ns, 2003 NULL); 2004 } 2005 else 2006 gfc_free_expr (p->ts.u.cl->length); 2007 2008 p->ts.u.cl->length 2009 = gfc_get_int_expr (gfc_charlen_int_kind, 2010 NULL, string_len); 2011 } 2012 } 2013 gfc_free_ref_list (p->ref); 2014 p->ref = NULL; 2015 break; 2016 2017 default: 2018 return true; 2019 } 2020 2021 break; 2022 2023 case REF_COMPONENT: 2024 cons = find_component_ref (p->value.constructor, p->ref); 2025 remove_subobject_ref (p, cons); 2026 break; 2027 2028 case REF_INQUIRY: 2029 if (!find_inquiry_ref (p, &newp)) 2030 return false; 2031 2032 gfc_replace_expr (p, newp); 2033 gfc_free_ref_list (p->ref); 2034 p->ref = NULL; 2035 break; 2036 2037 case REF_SUBSTRING: 2038 if (!find_substring_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 } 2047 2048 return true; 2049 } 2050 2051 2052 /* Simplify a chain of references. */ 2053 2054 static bool 2055 simplify_ref_chain (gfc_ref *ref, int type, gfc_expr **p) 2056 { 2057 int n; 2058 gfc_expr *newp; 2059 2060 for (; ref; ref = ref->next) 2061 { 2062 switch (ref->type) 2063 { 2064 case REF_ARRAY: 2065 for (n = 0; n < ref->u.ar.dimen; n++) 2066 { 2067 if (!gfc_simplify_expr (ref->u.ar.start[n], type)) 2068 return false; 2069 if (!gfc_simplify_expr (ref->u.ar.end[n], type)) 2070 return false; 2071 if (!gfc_simplify_expr (ref->u.ar.stride[n], type)) 2072 return false; 2073 } 2074 break; 2075 2076 case REF_SUBSTRING: 2077 if (!gfc_simplify_expr (ref->u.ss.start, type)) 2078 return false; 2079 if (!gfc_simplify_expr (ref->u.ss.end, type)) 2080 return false; 2081 break; 2082 2083 case REF_INQUIRY: 2084 if (!find_inquiry_ref (*p, &newp)) 2085 return false; 2086 2087 gfc_replace_expr (*p, newp); 2088 gfc_free_ref_list ((*p)->ref); 2089 (*p)->ref = NULL; 2090 return true; 2091 2092 default: 2093 break; 2094 } 2095 } 2096 return true; 2097 } 2098 2099 2100 /* Try to substitute the value of a parameter variable. */ 2101 2102 static bool 2103 simplify_parameter_variable (gfc_expr *p, int type) 2104 { 2105 gfc_expr *e; 2106 bool t; 2107 2108 /* Set rank and check array ref; as resolve_variable calls 2109 gfc_simplify_expr, call gfc_resolve_ref + gfc_expression_rank instead. */ 2110 if (!gfc_resolve_ref (p)) 2111 { 2112 gfc_error_check (); 2113 return false; 2114 } 2115 gfc_expression_rank (p); 2116 2117 /* Is this an inquiry? */ 2118 bool inquiry = false; 2119 gfc_ref* ref = p->ref; 2120 while (ref) 2121 { 2122 if (ref->type == REF_INQUIRY) 2123 break; 2124 ref = ref->next; 2125 } 2126 if (ref && ref->type == REF_INQUIRY) 2127 inquiry = ref->u.i == INQUIRY_LEN || ref->u.i == INQUIRY_KIND; 2128 2129 if (gfc_is_size_zero_array (p)) 2130 { 2131 if (p->expr_type == EXPR_ARRAY) 2132 return true; 2133 2134 e = gfc_get_expr (); 2135 e->expr_type = EXPR_ARRAY; 2136 e->ts = p->ts; 2137 e->rank = p->rank; 2138 e->value.constructor = NULL; 2139 e->shape = gfc_copy_shape (p->shape, p->rank); 2140 e->where = p->where; 2141 /* If %kind and %len are not used then we're done, otherwise 2142 drop through for simplification. */ 2143 if (!inquiry) 2144 { 2145 gfc_replace_expr (p, e); 2146 return true; 2147 } 2148 } 2149 else 2150 { 2151 e = gfc_copy_expr (p->symtree->n.sym->value); 2152 if (e == NULL) 2153 return false; 2154 2155 gfc_free_shape (&e->shape, e->rank); 2156 e->shape = gfc_copy_shape (p->shape, p->rank); 2157 e->rank = p->rank; 2158 2159 if (e->ts.type == BT_CHARACTER && p->ts.u.cl) 2160 e->ts = p->ts; 2161 } 2162 2163 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL) 2164 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, p->ts.u.cl); 2165 2166 /* Do not copy subobject refs for constant. */ 2167 if (e->expr_type != EXPR_CONSTANT && p->ref != NULL) 2168 e->ref = gfc_copy_ref (p->ref); 2169 t = gfc_simplify_expr (e, type); 2170 e->where = p->where; 2171 2172 /* Only use the simplification if it eliminated all subobject references. */ 2173 if (t && !e->ref) 2174 gfc_replace_expr (p, e); 2175 else 2176 gfc_free_expr (e); 2177 2178 return t; 2179 } 2180 2181 2182 static bool 2183 scalarize_intrinsic_call (gfc_expr *, bool init_flag); 2184 2185 /* Given an expression, simplify it by collapsing constant 2186 expressions. Most simplification takes place when the expression 2187 tree is being constructed. If an intrinsic function is simplified 2188 at some point, we get called again to collapse the result against 2189 other constants. 2190 2191 We work by recursively simplifying expression nodes, simplifying 2192 intrinsic functions where possible, which can lead to further 2193 constant collapsing. If an operator has constant operand(s), we 2194 rip the expression apart, and rebuild it, hoping that it becomes 2195 something simpler. 2196 2197 The expression type is defined for: 2198 0 Basic expression parsing 2199 1 Simplifying array constructors -- will substitute 2200 iterator values. 2201 Returns false on error, true otherwise. 2202 NOTE: Will return true even if the expression cannot be simplified. */ 2203 2204 bool 2205 gfc_simplify_expr (gfc_expr *p, int type) 2206 { 2207 gfc_actual_arglist *ap; 2208 gfc_intrinsic_sym* isym = NULL; 2209 2210 2211 if (p == NULL) 2212 return true; 2213 2214 switch (p->expr_type) 2215 { 2216 case EXPR_CONSTANT: 2217 if (p->ref && p->ref->type == REF_INQUIRY) 2218 simplify_ref_chain (p->ref, type, &p); 2219 break; 2220 case EXPR_NULL: 2221 break; 2222 2223 case EXPR_FUNCTION: 2224 // For array-bound functions, we don't need to optimize 2225 // the 'array' argument. In particular, if the argument 2226 // is a PARAMETER, simplifying might convert an EXPR_VARIABLE 2227 // into an EXPR_ARRAY; the latter has lbound = 1, the former 2228 // can have any lbound. 2229 ap = p->value.function.actual; 2230 if (p->value.function.isym && 2231 (p->value.function.isym->id == GFC_ISYM_LBOUND 2232 || p->value.function.isym->id == GFC_ISYM_UBOUND 2233 || p->value.function.isym->id == GFC_ISYM_LCOBOUND 2234 || p->value.function.isym->id == GFC_ISYM_UCOBOUND 2235 || p->value.function.isym->id == GFC_ISYM_SHAPE)) 2236 ap = ap->next; 2237 2238 for ( ; ap; ap = ap->next) 2239 if (!gfc_simplify_expr (ap->expr, type)) 2240 return false; 2241 2242 if (p->value.function.isym != NULL 2243 && gfc_intrinsic_func_interface (p, 1) == MATCH_ERROR) 2244 return false; 2245 2246 if (p->symtree && (p->value.function.isym || p->ts.type == BT_UNKNOWN)) 2247 { 2248 isym = gfc_find_function (p->symtree->n.sym->name); 2249 if (isym && isym->elemental) 2250 scalarize_intrinsic_call (p, false); 2251 } 2252 2253 break; 2254 2255 case EXPR_SUBSTRING: 2256 if (!simplify_ref_chain (p->ref, type, &p)) 2257 return false; 2258 2259 if (gfc_is_constant_expr (p)) 2260 { 2261 gfc_char_t *s; 2262 HOST_WIDE_INT start, end; 2263 2264 start = 0; 2265 if (p->ref && p->ref->u.ss.start) 2266 { 2267 gfc_extract_hwi (p->ref->u.ss.start, &start); 2268 start--; /* Convert from one-based to zero-based. */ 2269 } 2270 2271 end = p->value.character.length; 2272 if (p->ref && p->ref->u.ss.end) 2273 gfc_extract_hwi (p->ref->u.ss.end, &end); 2274 2275 if (end < start) 2276 end = start; 2277 2278 s = gfc_get_wide_string (end - start + 2); 2279 memcpy (s, p->value.character.string + start, 2280 (end - start) * sizeof (gfc_char_t)); 2281 s[end - start + 1] = '\0'; /* TODO: C-style string. */ 2282 free (p->value.character.string); 2283 p->value.character.string = s; 2284 p->value.character.length = end - start; 2285 p->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL); 2286 p->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind, 2287 NULL, 2288 p->value.character.length); 2289 gfc_free_ref_list (p->ref); 2290 p->ref = NULL; 2291 p->expr_type = EXPR_CONSTANT; 2292 } 2293 break; 2294 2295 case EXPR_OP: 2296 if (!simplify_intrinsic_op (p, type)) 2297 return false; 2298 break; 2299 2300 case EXPR_VARIABLE: 2301 /* Only substitute array parameter variables if we are in an 2302 initialization expression, or we want a subsection. */ 2303 if (p->symtree->n.sym->attr.flavor == FL_PARAMETER 2304 && (gfc_init_expr_flag || p->ref 2305 || (p->symtree->n.sym->value 2306 && p->symtree->n.sym->value->expr_type != EXPR_ARRAY))) 2307 { 2308 if (!simplify_parameter_variable (p, type)) 2309 return false; 2310 break; 2311 } 2312 2313 if (type == 1) 2314 { 2315 gfc_simplify_iterator_var (p); 2316 } 2317 2318 /* Simplify subcomponent references. */ 2319 if (!simplify_ref_chain (p->ref, type, &p)) 2320 return false; 2321 2322 break; 2323 2324 case EXPR_STRUCTURE: 2325 case EXPR_ARRAY: 2326 if (!simplify_ref_chain (p->ref, type, &p)) 2327 return false; 2328 2329 /* If the following conditions hold, we found something like kind type 2330 inquiry of the form a(2)%kind while simplify the ref chain. */ 2331 if (p->expr_type == EXPR_CONSTANT && !p->ref && !p->rank && !p->shape) 2332 return true; 2333 2334 if (!simplify_constructor (p->value.constructor, type)) 2335 return false; 2336 2337 if (p->expr_type == EXPR_ARRAY && p->ref && p->ref->type == REF_ARRAY 2338 && p->ref->u.ar.type == AR_FULL) 2339 gfc_expand_constructor (p, false); 2340 2341 if (!simplify_const_ref (p)) 2342 return false; 2343 2344 break; 2345 2346 case EXPR_COMPCALL: 2347 case EXPR_PPC: 2348 break; 2349 2350 case EXPR_UNKNOWN: 2351 gcc_unreachable (); 2352 } 2353 2354 return true; 2355 } 2356 2357 2358 /* Try simplification of an expression via gfc_simplify_expr. 2359 When an error occurs (arithmetic or otherwise), roll back. */ 2360 2361 bool 2362 gfc_try_simplify_expr (gfc_expr *e, int type) 2363 { 2364 gfc_expr *n; 2365 bool t, saved_div0; 2366 2367 if (e == NULL || e->expr_type == EXPR_CONSTANT) 2368 return true; 2369 2370 saved_div0 = gfc_seen_div0; 2371 gfc_seen_div0 = false; 2372 n = gfc_copy_expr (e); 2373 t = gfc_simplify_expr (n, type) && !gfc_seen_div0; 2374 if (t) 2375 gfc_replace_expr (e, n); 2376 else 2377 gfc_free_expr (n); 2378 gfc_seen_div0 = saved_div0; 2379 return t; 2380 } 2381 2382 2383 /* Returns the type of an expression with the exception that iterator 2384 variables are automatically integers no matter what else they may 2385 be declared as. */ 2386 2387 static bt 2388 et0 (gfc_expr *e) 2389 { 2390 if (e->expr_type == EXPR_VARIABLE && gfc_check_iter_variable (e)) 2391 return BT_INTEGER; 2392 2393 return e->ts.type; 2394 } 2395 2396 2397 /* Scalarize an expression for an elemental intrinsic call. */ 2398 2399 static bool 2400 scalarize_intrinsic_call (gfc_expr *e, bool init_flag) 2401 { 2402 gfc_actual_arglist *a, *b; 2403 gfc_constructor_base ctor; 2404 gfc_constructor *args[5] = {}; /* Avoid uninitialized warnings. */ 2405 gfc_constructor *ci, *new_ctor; 2406 gfc_expr *expr, *old, *p; 2407 int n, i, rank[5], array_arg; 2408 2409 if (e == NULL) 2410 return false; 2411 2412 a = e->value.function.actual; 2413 for (; a; a = a->next) 2414 if (a->expr && !gfc_is_constant_expr (a->expr)) 2415 return false; 2416 2417 /* Find which, if any, arguments are arrays. Assume that the old 2418 expression carries the type information and that the first arg 2419 that is an array expression carries all the shape information.*/ 2420 n = array_arg = 0; 2421 a = e->value.function.actual; 2422 for (; a; a = a->next) 2423 { 2424 n++; 2425 if (!a->expr || a->expr->expr_type != EXPR_ARRAY) 2426 continue; 2427 array_arg = n; 2428 expr = gfc_copy_expr (a->expr); 2429 break; 2430 } 2431 2432 if (!array_arg) 2433 return false; 2434 2435 old = gfc_copy_expr (e); 2436 2437 gfc_constructor_free (expr->value.constructor); 2438 expr->value.constructor = NULL; 2439 expr->ts = old->ts; 2440 expr->where = old->where; 2441 expr->expr_type = EXPR_ARRAY; 2442 2443 /* Copy the array argument constructors into an array, with nulls 2444 for the scalars. */ 2445 n = 0; 2446 a = old->value.function.actual; 2447 for (; a; a = a->next) 2448 { 2449 /* Check that this is OK for an initialization expression. */ 2450 if (a->expr && init_flag && !gfc_check_init_expr (a->expr)) 2451 goto cleanup; 2452 2453 rank[n] = 0; 2454 if (a->expr && a->expr->rank && a->expr->expr_type == EXPR_VARIABLE) 2455 { 2456 rank[n] = a->expr->rank; 2457 ctor = a->expr->symtree->n.sym->value->value.constructor; 2458 args[n] = gfc_constructor_first (ctor); 2459 } 2460 else if (a->expr && a->expr->expr_type == EXPR_ARRAY) 2461 { 2462 if (a->expr->rank) 2463 rank[n] = a->expr->rank; 2464 else 2465 rank[n] = 1; 2466 ctor = gfc_constructor_copy (a->expr->value.constructor); 2467 args[n] = gfc_constructor_first (ctor); 2468 } 2469 else 2470 args[n] = NULL; 2471 2472 n++; 2473 } 2474 2475 /* Using the array argument as the master, step through the array 2476 calling the function for each element and advancing the array 2477 constructors together. */ 2478 for (ci = args[array_arg - 1]; ci; ci = gfc_constructor_next (ci)) 2479 { 2480 new_ctor = gfc_constructor_append_expr (&expr->value.constructor, 2481 gfc_copy_expr (old), NULL); 2482 2483 gfc_free_actual_arglist (new_ctor->expr->value.function.actual); 2484 a = NULL; 2485 b = old->value.function.actual; 2486 for (i = 0; i < n; i++) 2487 { 2488 if (a == NULL) 2489 new_ctor->expr->value.function.actual 2490 = a = gfc_get_actual_arglist (); 2491 else 2492 { 2493 a->next = gfc_get_actual_arglist (); 2494 a = a->next; 2495 } 2496 2497 if (args[i]) 2498 a->expr = gfc_copy_expr (args[i]->expr); 2499 else 2500 a->expr = gfc_copy_expr (b->expr); 2501 2502 b = b->next; 2503 } 2504 2505 /* Simplify the function calls. If the simplification fails, the 2506 error will be flagged up down-stream or the library will deal 2507 with it. */ 2508 p = gfc_copy_expr (new_ctor->expr); 2509 2510 if (!gfc_simplify_expr (p, init_flag)) 2511 gfc_free_expr (p); 2512 else 2513 gfc_replace_expr (new_ctor->expr, p); 2514 2515 for (i = 0; i < n; i++) 2516 if (args[i]) 2517 args[i] = gfc_constructor_next (args[i]); 2518 2519 for (i = 1; i < n; i++) 2520 if (rank[i] && ((args[i] != NULL && args[array_arg - 1] == NULL) 2521 || (args[i] == NULL && args[array_arg - 1] != NULL))) 2522 goto compliance; 2523 } 2524 2525 free_expr0 (e); 2526 *e = *expr; 2527 /* Free "expr" but not the pointers it contains. */ 2528 free (expr); 2529 gfc_free_expr (old); 2530 return true; 2531 2532 compliance: 2533 gfc_error_now ("elemental function arguments at %C are not compliant"); 2534 2535 cleanup: 2536 gfc_free_expr (expr); 2537 gfc_free_expr (old); 2538 return false; 2539 } 2540 2541 2542 static bool 2543 check_intrinsic_op (gfc_expr *e, bool (*check_function) (gfc_expr *)) 2544 { 2545 gfc_expr *op1 = e->value.op.op1; 2546 gfc_expr *op2 = e->value.op.op2; 2547 2548 if (!(*check_function)(op1)) 2549 return false; 2550 2551 switch (e->value.op.op) 2552 { 2553 case INTRINSIC_UPLUS: 2554 case INTRINSIC_UMINUS: 2555 if (!numeric_type (et0 (op1))) 2556 goto not_numeric; 2557 break; 2558 2559 case INTRINSIC_EQ: 2560 case INTRINSIC_EQ_OS: 2561 case INTRINSIC_NE: 2562 case INTRINSIC_NE_OS: 2563 case INTRINSIC_GT: 2564 case INTRINSIC_GT_OS: 2565 case INTRINSIC_GE: 2566 case INTRINSIC_GE_OS: 2567 case INTRINSIC_LT: 2568 case INTRINSIC_LT_OS: 2569 case INTRINSIC_LE: 2570 case INTRINSIC_LE_OS: 2571 if (!(*check_function)(op2)) 2572 return false; 2573 2574 if (!(et0 (op1) == BT_CHARACTER && et0 (op2) == BT_CHARACTER) 2575 && !(numeric_type (et0 (op1)) && numeric_type (et0 (op2)))) 2576 { 2577 gfc_error ("Numeric or CHARACTER operands are required in " 2578 "expression at %L", &e->where); 2579 return false; 2580 } 2581 break; 2582 2583 case INTRINSIC_PLUS: 2584 case INTRINSIC_MINUS: 2585 case INTRINSIC_TIMES: 2586 case INTRINSIC_DIVIDE: 2587 case INTRINSIC_POWER: 2588 if (!(*check_function)(op2)) 2589 return false; 2590 2591 if (!numeric_type (et0 (op1)) || !numeric_type (et0 (op2))) 2592 goto not_numeric; 2593 2594 break; 2595 2596 case INTRINSIC_CONCAT: 2597 if (!(*check_function)(op2)) 2598 return false; 2599 2600 if (et0 (op1) != BT_CHARACTER || et0 (op2) != BT_CHARACTER) 2601 { 2602 gfc_error ("Concatenation operator in expression at %L " 2603 "must have two CHARACTER operands", &op1->where); 2604 return false; 2605 } 2606 2607 if (op1->ts.kind != op2->ts.kind) 2608 { 2609 gfc_error ("Concat operator at %L must concatenate strings of the " 2610 "same kind", &e->where); 2611 return false; 2612 } 2613 2614 break; 2615 2616 case INTRINSIC_NOT: 2617 if (et0 (op1) != BT_LOGICAL) 2618 { 2619 gfc_error (".NOT. operator in expression at %L must have a LOGICAL " 2620 "operand", &op1->where); 2621 return false; 2622 } 2623 2624 break; 2625 2626 case INTRINSIC_AND: 2627 case INTRINSIC_OR: 2628 case INTRINSIC_EQV: 2629 case INTRINSIC_NEQV: 2630 if (!(*check_function)(op2)) 2631 return false; 2632 2633 if (et0 (op1) != BT_LOGICAL || et0 (op2) != BT_LOGICAL) 2634 { 2635 gfc_error ("LOGICAL operands are required in expression at %L", 2636 &e->where); 2637 return false; 2638 } 2639 2640 break; 2641 2642 case INTRINSIC_PARENTHESES: 2643 break; 2644 2645 default: 2646 gfc_error ("Only intrinsic operators can be used in expression at %L", 2647 &e->where); 2648 return false; 2649 } 2650 2651 return true; 2652 2653 not_numeric: 2654 gfc_error ("Numeric operands are required in expression at %L", &e->where); 2655 2656 return false; 2657 } 2658 2659 /* F2003, 7.1.7 (3): In init expression, allocatable components 2660 must not be data-initialized. */ 2661 static bool 2662 check_alloc_comp_init (gfc_expr *e) 2663 { 2664 gfc_component *comp; 2665 gfc_constructor *ctor; 2666 2667 gcc_assert (e->expr_type == EXPR_STRUCTURE); 2668 gcc_assert (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS); 2669 2670 for (comp = e->ts.u.derived->components, 2671 ctor = gfc_constructor_first (e->value.constructor); 2672 comp; comp = comp->next, ctor = gfc_constructor_next (ctor)) 2673 { 2674 if (comp->attr.allocatable && ctor->expr 2675 && ctor->expr->expr_type != EXPR_NULL) 2676 { 2677 gfc_error ("Invalid initialization expression for ALLOCATABLE " 2678 "component %qs in structure constructor at %L", 2679 comp->name, &ctor->expr->where); 2680 return false; 2681 } 2682 } 2683 2684 return true; 2685 } 2686 2687 static match 2688 check_init_expr_arguments (gfc_expr *e) 2689 { 2690 gfc_actual_arglist *ap; 2691 2692 for (ap = e->value.function.actual; ap; ap = ap->next) 2693 if (!gfc_check_init_expr (ap->expr)) 2694 return MATCH_ERROR; 2695 2696 return MATCH_YES; 2697 } 2698 2699 static bool check_restricted (gfc_expr *); 2700 2701 /* F95, 7.1.6.1, Initialization expressions, (7) 2702 F2003, 7.1.7 Initialization expression, (8) 2703 F2008, 7.1.12 Constant expression, (4) */ 2704 2705 static match 2706 check_inquiry (gfc_expr *e, int not_restricted) 2707 { 2708 const char *name; 2709 const char *const *functions; 2710 2711 static const char *const inquiry_func_f95[] = { 2712 "lbound", "shape", "size", "ubound", 2713 "bit_size", "len", "kind", 2714 "digits", "epsilon", "huge", "maxexponent", "minexponent", 2715 "precision", "radix", "range", "tiny", 2716 NULL 2717 }; 2718 2719 static const char *const inquiry_func_f2003[] = { 2720 "lbound", "shape", "size", "ubound", 2721 "bit_size", "len", "kind", 2722 "digits", "epsilon", "huge", "maxexponent", "minexponent", 2723 "precision", "radix", "range", "tiny", 2724 "new_line", NULL 2725 }; 2726 2727 /* std=f2008+ or -std=gnu */ 2728 static const char *const inquiry_func_gnu[] = { 2729 "lbound", "shape", "size", "ubound", 2730 "bit_size", "len", "kind", 2731 "digits", "epsilon", "huge", "maxexponent", "minexponent", 2732 "precision", "radix", "range", "tiny", 2733 "new_line", "storage_size", NULL 2734 }; 2735 2736 int i = 0; 2737 gfc_actual_arglist *ap; 2738 gfc_symbol *sym; 2739 gfc_symbol *asym; 2740 2741 if (!e->value.function.isym 2742 || !e->value.function.isym->inquiry) 2743 return MATCH_NO; 2744 2745 /* An undeclared parameter will get us here (PR25018). */ 2746 if (e->symtree == NULL) 2747 return MATCH_NO; 2748 2749 sym = e->symtree->n.sym; 2750 2751 if (sym->from_intmod) 2752 { 2753 if (sym->from_intmod == INTMOD_ISO_FORTRAN_ENV 2754 && sym->intmod_sym_id != ISOFORTRAN_COMPILER_OPTIONS 2755 && sym->intmod_sym_id != ISOFORTRAN_COMPILER_VERSION) 2756 return MATCH_NO; 2757 2758 if (sym->from_intmod == INTMOD_ISO_C_BINDING 2759 && sym->intmod_sym_id != ISOCBINDING_C_SIZEOF) 2760 return MATCH_NO; 2761 } 2762 else 2763 { 2764 name = sym->name; 2765 2766 functions = inquiry_func_gnu; 2767 if (gfc_option.warn_std & GFC_STD_F2003) 2768 functions = inquiry_func_f2003; 2769 if (gfc_option.warn_std & GFC_STD_F95) 2770 functions = inquiry_func_f95; 2771 2772 for (i = 0; functions[i]; i++) 2773 if (strcmp (functions[i], name) == 0) 2774 break; 2775 2776 if (functions[i] == NULL) 2777 return MATCH_ERROR; 2778 } 2779 2780 /* At this point we have an inquiry function with a variable argument. The 2781 type of the variable might be undefined, but we need it now, because the 2782 arguments of these functions are not allowed to be undefined. */ 2783 2784 for (ap = e->value.function.actual; ap; ap = ap->next) 2785 { 2786 if (!ap->expr) 2787 continue; 2788 2789 asym = ap->expr->symtree ? ap->expr->symtree->n.sym : NULL; 2790 2791 if (ap->expr->ts.type == BT_UNKNOWN) 2792 { 2793 if (asym && asym->ts.type == BT_UNKNOWN 2794 && !gfc_set_default_type (asym, 0, gfc_current_ns)) 2795 return MATCH_NO; 2796 2797 ap->expr->ts = asym->ts; 2798 } 2799 2800 if (asym && asym->assoc && asym->assoc->target 2801 && asym->assoc->target->expr_type == EXPR_CONSTANT) 2802 { 2803 gfc_free_expr (ap->expr); 2804 ap->expr = gfc_copy_expr (asym->assoc->target); 2805 } 2806 2807 /* Assumed character length will not reduce to a constant expression 2808 with LEN, as required by the standard. */ 2809 if (i == 5 && not_restricted && asym 2810 && asym->ts.type == BT_CHARACTER 2811 && ((asym->ts.u.cl && asym->ts.u.cl->length == NULL) 2812 || asym->ts.deferred)) 2813 { 2814 gfc_error ("Assumed or deferred character length variable %qs " 2815 "in constant expression at %L", 2816 asym->name, &ap->expr->where); 2817 return MATCH_ERROR; 2818 } 2819 else if (not_restricted && !gfc_check_init_expr (ap->expr)) 2820 return MATCH_ERROR; 2821 2822 if (not_restricted == 0 2823 && ap->expr->expr_type != EXPR_VARIABLE 2824 && !check_restricted (ap->expr)) 2825 return MATCH_ERROR; 2826 2827 if (not_restricted == 0 2828 && ap->expr->expr_type == EXPR_VARIABLE 2829 && asym->attr.dummy && asym->attr.optional) 2830 return MATCH_NO; 2831 } 2832 2833 return MATCH_YES; 2834 } 2835 2836 2837 /* F95, 7.1.6.1, Initialization expressions, (5) 2838 F2003, 7.1.7 Initialization expression, (5) */ 2839 2840 static match 2841 check_transformational (gfc_expr *e) 2842 { 2843 static const char * const trans_func_f95[] = { 2844 "repeat", "reshape", "selected_int_kind", 2845 "selected_real_kind", "transfer", "trim", NULL 2846 }; 2847 2848 static const char * const trans_func_f2003[] = { 2849 "all", "any", "count", "dot_product", "matmul", "null", "pack", 2850 "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind", 2851 "selected_real_kind", "spread", "sum", "transfer", "transpose", 2852 "trim", "unpack", NULL 2853 }; 2854 2855 static const char * const trans_func_f2008[] = { 2856 "all", "any", "count", "dot_product", "matmul", "null", "pack", 2857 "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind", 2858 "selected_real_kind", "spread", "sum", "transfer", "transpose", 2859 "trim", "unpack", "findloc", NULL 2860 }; 2861 2862 int i; 2863 const char *name; 2864 const char *const *functions; 2865 2866 if (!e->value.function.isym 2867 || !e->value.function.isym->transformational) 2868 return MATCH_NO; 2869 2870 name = e->symtree->n.sym->name; 2871 2872 if (gfc_option.allow_std & GFC_STD_F2008) 2873 functions = trans_func_f2008; 2874 else if (gfc_option.allow_std & GFC_STD_F2003) 2875 functions = trans_func_f2003; 2876 else 2877 functions = trans_func_f95; 2878 2879 /* NULL() is dealt with below. */ 2880 if (strcmp ("null", name) == 0) 2881 return MATCH_NO; 2882 2883 for (i = 0; functions[i]; i++) 2884 if (strcmp (functions[i], name) == 0) 2885 break; 2886 2887 if (functions[i] == NULL) 2888 { 2889 gfc_error ("transformational intrinsic %qs at %L is not permitted " 2890 "in an initialization expression", name, &e->where); 2891 return MATCH_ERROR; 2892 } 2893 2894 return check_init_expr_arguments (e); 2895 } 2896 2897 2898 /* F95, 7.1.6.1, Initialization expressions, (6) 2899 F2003, 7.1.7 Initialization expression, (6) */ 2900 2901 static match 2902 check_null (gfc_expr *e) 2903 { 2904 if (strcmp ("null", e->symtree->n.sym->name) != 0) 2905 return MATCH_NO; 2906 2907 return check_init_expr_arguments (e); 2908 } 2909 2910 2911 static match 2912 check_elemental (gfc_expr *e) 2913 { 2914 if (!e->value.function.isym 2915 || !e->value.function.isym->elemental) 2916 return MATCH_NO; 2917 2918 if (e->ts.type != BT_INTEGER 2919 && e->ts.type != BT_CHARACTER 2920 && !gfc_notify_std (GFC_STD_F2003, "Evaluation of nonstandard " 2921 "initialization expression at %L", &e->where)) 2922 return MATCH_ERROR; 2923 2924 return check_init_expr_arguments (e); 2925 } 2926 2927 2928 static match 2929 check_conversion (gfc_expr *e) 2930 { 2931 if (!e->value.function.isym 2932 || !e->value.function.isym->conversion) 2933 return MATCH_NO; 2934 2935 return check_init_expr_arguments (e); 2936 } 2937 2938 2939 /* Verify that an expression is an initialization expression. A side 2940 effect is that the expression tree is reduced to a single constant 2941 node if all goes well. This would normally happen when the 2942 expression is constructed but function references are assumed to be 2943 intrinsics in the context of initialization expressions. If 2944 false is returned an error message has been generated. */ 2945 2946 bool 2947 gfc_check_init_expr (gfc_expr *e) 2948 { 2949 match m; 2950 bool t; 2951 2952 if (e == NULL) 2953 return true; 2954 2955 switch (e->expr_type) 2956 { 2957 case EXPR_OP: 2958 t = check_intrinsic_op (e, gfc_check_init_expr); 2959 if (t) 2960 t = gfc_simplify_expr (e, 0); 2961 2962 break; 2963 2964 case EXPR_FUNCTION: 2965 t = false; 2966 2967 { 2968 bool conversion; 2969 gfc_intrinsic_sym* isym = NULL; 2970 gfc_symbol* sym = e->symtree->n.sym; 2971 2972 /* Simplify here the intrinsics from the IEEE_ARITHMETIC and 2973 IEEE_EXCEPTIONS modules. */ 2974 int mod = sym->from_intmod; 2975 if (mod == INTMOD_NONE && sym->generic) 2976 mod = sym->generic->sym->from_intmod; 2977 if (mod == INTMOD_IEEE_ARITHMETIC || mod == INTMOD_IEEE_EXCEPTIONS) 2978 { 2979 gfc_expr *new_expr = gfc_simplify_ieee_functions (e); 2980 if (new_expr) 2981 { 2982 gfc_replace_expr (e, new_expr); 2983 t = true; 2984 break; 2985 } 2986 } 2987 2988 /* If a conversion function, e.g., __convert_i8_i4, was inserted 2989 into an array constructor, we need to skip the error check here. 2990 Conversion errors are caught below in scalarize_intrinsic_call. */ 2991 conversion = e->value.function.isym 2992 && (e->value.function.isym->conversion == 1); 2993 2994 if (!conversion && (!gfc_is_intrinsic (sym, 0, e->where) 2995 || (m = gfc_intrinsic_func_interface (e, 0)) == MATCH_NO)) 2996 { 2997 gfc_error ("Function %qs in initialization expression at %L " 2998 "must be an intrinsic function", 2999 e->symtree->n.sym->name, &e->where); 3000 break; 3001 } 3002 3003 if ((m = check_conversion (e)) == MATCH_NO 3004 && (m = check_inquiry (e, 1)) == MATCH_NO 3005 && (m = check_null (e)) == MATCH_NO 3006 && (m = check_transformational (e)) == MATCH_NO 3007 && (m = check_elemental (e)) == MATCH_NO) 3008 { 3009 gfc_error ("Intrinsic function %qs at %L is not permitted " 3010 "in an initialization expression", 3011 e->symtree->n.sym->name, &e->where); 3012 m = MATCH_ERROR; 3013 } 3014 3015 if (m == MATCH_ERROR) 3016 return false; 3017 3018 /* Try to scalarize an elemental intrinsic function that has an 3019 array argument. */ 3020 isym = gfc_find_function (e->symtree->n.sym->name); 3021 if (isym && isym->elemental 3022 && (t = scalarize_intrinsic_call (e, true))) 3023 break; 3024 } 3025 3026 if (m == MATCH_YES) 3027 t = gfc_simplify_expr (e, 0); 3028 3029 break; 3030 3031 case EXPR_VARIABLE: 3032 t = true; 3033 3034 /* This occurs when parsing pdt templates. */ 3035 if (gfc_expr_attr (e).pdt_kind) 3036 break; 3037 3038 if (gfc_check_iter_variable (e)) 3039 break; 3040 3041 if (e->symtree->n.sym->attr.flavor == FL_PARAMETER) 3042 { 3043 /* A PARAMETER shall not be used to define itself, i.e. 3044 REAL, PARAMETER :: x = transfer(0, x) 3045 is invalid. */ 3046 if (!e->symtree->n.sym->value) 3047 { 3048 gfc_error ("PARAMETER %qs is used at %L before its definition " 3049 "is complete", e->symtree->n.sym->name, &e->where); 3050 t = false; 3051 } 3052 else 3053 t = simplify_parameter_variable (e, 0); 3054 3055 break; 3056 } 3057 3058 if (gfc_in_match_data ()) 3059 break; 3060 3061 t = false; 3062 3063 if (e->symtree->n.sym->as) 3064 { 3065 switch (e->symtree->n.sym->as->type) 3066 { 3067 case AS_ASSUMED_SIZE: 3068 gfc_error ("Assumed size array %qs at %L is not permitted " 3069 "in an initialization expression", 3070 e->symtree->n.sym->name, &e->where); 3071 break; 3072 3073 case AS_ASSUMED_SHAPE: 3074 gfc_error ("Assumed shape array %qs at %L is not permitted " 3075 "in an initialization expression", 3076 e->symtree->n.sym->name, &e->where); 3077 break; 3078 3079 case AS_DEFERRED: 3080 if (!e->symtree->n.sym->attr.allocatable 3081 && !e->symtree->n.sym->attr.pointer 3082 && e->symtree->n.sym->attr.dummy) 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 else 3087 gfc_error ("Deferred array %qs at %L is not permitted " 3088 "in an initialization expression", 3089 e->symtree->n.sym->name, &e->where); 3090 break; 3091 3092 case AS_EXPLICIT: 3093 gfc_error ("Array %qs at %L is a variable, which does " 3094 "not reduce to a constant expression", 3095 e->symtree->n.sym->name, &e->where); 3096 break; 3097 3098 case AS_ASSUMED_RANK: 3099 gfc_error ("Assumed-rank array %qs at %L is not permitted " 3100 "in an initialization expression", 3101 e->symtree->n.sym->name, &e->where); 3102 break; 3103 3104 default: 3105 gcc_unreachable(); 3106 } 3107 } 3108 else 3109 gfc_error ("Parameter %qs at %L has not been declared or is " 3110 "a variable, which does not reduce to a constant " 3111 "expression", e->symtree->name, &e->where); 3112 3113 break; 3114 3115 case EXPR_CONSTANT: 3116 case EXPR_NULL: 3117 t = true; 3118 break; 3119 3120 case EXPR_SUBSTRING: 3121 if (e->ref) 3122 { 3123 t = gfc_check_init_expr (e->ref->u.ss.start); 3124 if (!t) 3125 break; 3126 3127 t = gfc_check_init_expr (e->ref->u.ss.end); 3128 if (t) 3129 t = gfc_simplify_expr (e, 0); 3130 } 3131 else 3132 t = false; 3133 break; 3134 3135 case EXPR_STRUCTURE: 3136 t = e->ts.is_iso_c ? true : false; 3137 if (t) 3138 break; 3139 3140 t = check_alloc_comp_init (e); 3141 if (!t) 3142 break; 3143 3144 t = gfc_check_constructor (e, gfc_check_init_expr); 3145 if (!t) 3146 break; 3147 3148 break; 3149 3150 case EXPR_ARRAY: 3151 t = gfc_check_constructor (e, gfc_check_init_expr); 3152 if (!t) 3153 break; 3154 3155 t = gfc_expand_constructor (e, true); 3156 if (!t) 3157 break; 3158 3159 t = gfc_check_constructor_type (e); 3160 break; 3161 3162 default: 3163 gfc_internal_error ("check_init_expr(): Unknown expression type"); 3164 } 3165 3166 return t; 3167 } 3168 3169 /* Reduces a general expression to an initialization expression (a constant). 3170 This used to be part of gfc_match_init_expr. 3171 Note that this function doesn't free the given expression on false. */ 3172 3173 bool 3174 gfc_reduce_init_expr (gfc_expr *expr) 3175 { 3176 bool t; 3177 3178 gfc_init_expr_flag = true; 3179 t = gfc_resolve_expr (expr); 3180 if (t) 3181 t = gfc_check_init_expr (expr); 3182 gfc_init_expr_flag = false; 3183 3184 if (!t || !expr) 3185 return false; 3186 3187 if (expr->expr_type == EXPR_ARRAY) 3188 { 3189 if (!gfc_check_constructor_type (expr)) 3190 return false; 3191 if (!gfc_expand_constructor (expr, true)) 3192 return false; 3193 } 3194 3195 return true; 3196 } 3197 3198 3199 /* Match an initialization expression. We work by first matching an 3200 expression, then reducing it to a constant. */ 3201 3202 match 3203 gfc_match_init_expr (gfc_expr **result) 3204 { 3205 gfc_expr *expr; 3206 match m; 3207 bool t; 3208 3209 expr = NULL; 3210 3211 gfc_init_expr_flag = true; 3212 3213 m = gfc_match_expr (&expr); 3214 if (m != MATCH_YES) 3215 { 3216 gfc_init_expr_flag = false; 3217 return m; 3218 } 3219 3220 if (gfc_derived_parameter_expr (expr)) 3221 { 3222 *result = expr; 3223 gfc_init_expr_flag = false; 3224 return m; 3225 } 3226 3227 t = gfc_reduce_init_expr (expr); 3228 if (!t) 3229 { 3230 gfc_free_expr (expr); 3231 gfc_init_expr_flag = false; 3232 return MATCH_ERROR; 3233 } 3234 3235 *result = expr; 3236 gfc_init_expr_flag = false; 3237 3238 return MATCH_YES; 3239 } 3240 3241 3242 /* Given an actual argument list, test to see that each argument is a 3243 restricted expression and optionally if the expression type is 3244 integer or character. */ 3245 3246 static bool 3247 restricted_args (gfc_actual_arglist *a) 3248 { 3249 for (; a; a = a->next) 3250 { 3251 if (!check_restricted (a->expr)) 3252 return false; 3253 } 3254 3255 return true; 3256 } 3257 3258 3259 /************* Restricted/specification expressions *************/ 3260 3261 3262 /* Make sure a non-intrinsic function is a specification function, 3263 * see F08:7.1.11.5. */ 3264 3265 static bool 3266 external_spec_function (gfc_expr *e) 3267 { 3268 gfc_symbol *f; 3269 3270 f = e->value.function.esym; 3271 3272 /* IEEE functions allowed are "a reference to a transformational function 3273 from the intrinsic module IEEE_ARITHMETIC or IEEE_EXCEPTIONS", and 3274 "inquiry function from the intrinsic modules IEEE_ARITHMETIC and 3275 IEEE_EXCEPTIONS". */ 3276 if (f->from_intmod == INTMOD_IEEE_ARITHMETIC 3277 || f->from_intmod == INTMOD_IEEE_EXCEPTIONS) 3278 { 3279 if (!strcmp (f->name, "ieee_selected_real_kind") 3280 || !strcmp (f->name, "ieee_support_rounding") 3281 || !strcmp (f->name, "ieee_support_flag") 3282 || !strcmp (f->name, "ieee_support_halting") 3283 || !strcmp (f->name, "ieee_support_datatype") 3284 || !strcmp (f->name, "ieee_support_denormal") 3285 || !strcmp (f->name, "ieee_support_subnormal") 3286 || !strcmp (f->name, "ieee_support_divide") 3287 || !strcmp (f->name, "ieee_support_inf") 3288 || !strcmp (f->name, "ieee_support_io") 3289 || !strcmp (f->name, "ieee_support_nan") 3290 || !strcmp (f->name, "ieee_support_sqrt") 3291 || !strcmp (f->name, "ieee_support_standard") 3292 || !strcmp (f->name, "ieee_support_underflow_control")) 3293 goto function_allowed; 3294 } 3295 3296 if (f->attr.proc == PROC_ST_FUNCTION) 3297 { 3298 gfc_error ("Specification function %qs at %L cannot be a statement " 3299 "function", f->name, &e->where); 3300 return false; 3301 } 3302 3303 if (f->attr.proc == PROC_INTERNAL) 3304 { 3305 gfc_error ("Specification function %qs at %L cannot be an internal " 3306 "function", f->name, &e->where); 3307 return false; 3308 } 3309 3310 if (!f->attr.pure && !f->attr.elemental) 3311 { 3312 gfc_error ("Specification function %qs at %L must be PURE", f->name, 3313 &e->where); 3314 return false; 3315 } 3316 3317 /* F08:7.1.11.6. */ 3318 if (f->attr.recursive 3319 && !gfc_notify_std (GFC_STD_F2003, 3320 "Specification function %qs " 3321 "at %L cannot be RECURSIVE", f->name, &e->where)) 3322 return false; 3323 3324 function_allowed: 3325 return restricted_args (e->value.function.actual); 3326 } 3327 3328 3329 /* Check to see that a function reference to an intrinsic is a 3330 restricted expression. */ 3331 3332 static bool 3333 restricted_intrinsic (gfc_expr *e) 3334 { 3335 /* TODO: Check constraints on inquiry functions. 7.1.6.2 (7). */ 3336 if (check_inquiry (e, 0) == MATCH_YES) 3337 return true; 3338 3339 return restricted_args (e->value.function.actual); 3340 } 3341 3342 3343 /* Check the expressions of an actual arglist. Used by check_restricted. */ 3344 3345 static bool 3346 check_arglist (gfc_actual_arglist* arg, bool (*checker) (gfc_expr*)) 3347 { 3348 for (; arg; arg = arg->next) 3349 if (!checker (arg->expr)) 3350 return false; 3351 3352 return true; 3353 } 3354 3355 3356 /* Check the subscription expressions of a reference chain with a checking 3357 function; used by check_restricted. */ 3358 3359 static bool 3360 check_references (gfc_ref* ref, bool (*checker) (gfc_expr*)) 3361 { 3362 int dim; 3363 3364 if (!ref) 3365 return true; 3366 3367 switch (ref->type) 3368 { 3369 case REF_ARRAY: 3370 for (dim = 0; dim < ref->u.ar.dimen; ++dim) 3371 { 3372 if (!checker (ref->u.ar.start[dim])) 3373 return false; 3374 if (!checker (ref->u.ar.end[dim])) 3375 return false; 3376 if (!checker (ref->u.ar.stride[dim])) 3377 return false; 3378 } 3379 break; 3380 3381 case REF_COMPONENT: 3382 /* Nothing needed, just proceed to next reference. */ 3383 break; 3384 3385 case REF_SUBSTRING: 3386 if (!checker (ref->u.ss.start)) 3387 return false; 3388 if (!checker (ref->u.ss.end)) 3389 return false; 3390 break; 3391 3392 default: 3393 gcc_unreachable (); 3394 break; 3395 } 3396 3397 return check_references (ref->next, checker); 3398 } 3399 3400 /* Return true if ns is a parent of the current ns. */ 3401 3402 static bool 3403 is_parent_of_current_ns (gfc_namespace *ns) 3404 { 3405 gfc_namespace *p; 3406 for (p = gfc_current_ns->parent; p; p = p->parent) 3407 if (ns == p) 3408 return true; 3409 3410 return false; 3411 } 3412 3413 /* Verify that an expression is a restricted expression. Like its 3414 cousin check_init_expr(), an error message is generated if we 3415 return false. */ 3416 3417 static bool 3418 check_restricted (gfc_expr *e) 3419 { 3420 gfc_symbol* sym; 3421 bool t; 3422 3423 if (e == NULL) 3424 return true; 3425 3426 switch (e->expr_type) 3427 { 3428 case EXPR_OP: 3429 t = check_intrinsic_op (e, check_restricted); 3430 if (t) 3431 t = gfc_simplify_expr (e, 0); 3432 3433 break; 3434 3435 case EXPR_FUNCTION: 3436 if (e->value.function.esym) 3437 { 3438 t = check_arglist (e->value.function.actual, &check_restricted); 3439 if (t) 3440 t = external_spec_function (e); 3441 } 3442 else 3443 { 3444 if (e->value.function.isym && e->value.function.isym->inquiry) 3445 t = true; 3446 else 3447 t = check_arglist (e->value.function.actual, &check_restricted); 3448 3449 if (t) 3450 t = restricted_intrinsic (e); 3451 } 3452 break; 3453 3454 case EXPR_VARIABLE: 3455 sym = e->symtree->n.sym; 3456 t = false; 3457 3458 /* If a dummy argument appears in a context that is valid for a 3459 restricted expression in an elemental procedure, it will have 3460 already been simplified away once we get here. Therefore we 3461 don't need to jump through hoops to distinguish valid from 3462 invalid cases. Allowed in F2008 and F2018. */ 3463 if (gfc_notification_std (GFC_STD_F2008) 3464 && sym->attr.dummy && sym->ns == gfc_current_ns 3465 && sym->ns->proc_name && sym->ns->proc_name->attr.elemental) 3466 { 3467 gfc_error_now ("Dummy argument %qs not " 3468 "allowed in expression at %L", 3469 sym->name, &e->where); 3470 break; 3471 } 3472 3473 if (sym->attr.optional) 3474 { 3475 gfc_error ("Dummy argument %qs at %L cannot be OPTIONAL", 3476 sym->name, &e->where); 3477 break; 3478 } 3479 3480 if (sym->attr.intent == INTENT_OUT) 3481 { 3482 gfc_error ("Dummy argument %qs at %L cannot be INTENT(OUT)", 3483 sym->name, &e->where); 3484 break; 3485 } 3486 3487 /* Check reference chain if any. */ 3488 if (!check_references (e->ref, &check_restricted)) 3489 break; 3490 3491 /* gfc_is_formal_arg broadcasts that a formal argument list is being 3492 processed in resolve.cc(resolve_formal_arglist). This is done so 3493 that host associated dummy array indices are accepted (PR23446). 3494 This mechanism also does the same for the specification expressions 3495 of array-valued functions. */ 3496 if (e->error 3497 || sym->attr.in_common 3498 || sym->attr.use_assoc 3499 || sym->attr.dummy 3500 || sym->attr.implied_index 3501 || sym->attr.flavor == FL_PARAMETER 3502 || is_parent_of_current_ns (sym->ns) 3503 || (sym->ns->proc_name != NULL 3504 && sym->ns->proc_name->attr.flavor == FL_MODULE) 3505 || (gfc_is_formal_arg () && (sym->ns == gfc_current_ns))) 3506 { 3507 t = true; 3508 break; 3509 } 3510 3511 gfc_error ("Variable %qs cannot appear in the expression at %L", 3512 sym->name, &e->where); 3513 /* Prevent a repetition of the error. */ 3514 e->error = 1; 3515 break; 3516 3517 case EXPR_NULL: 3518 case EXPR_CONSTANT: 3519 t = true; 3520 break; 3521 3522 case EXPR_SUBSTRING: 3523 t = gfc_specification_expr (e->ref->u.ss.start); 3524 if (!t) 3525 break; 3526 3527 t = gfc_specification_expr (e->ref->u.ss.end); 3528 if (t) 3529 t = gfc_simplify_expr (e, 0); 3530 3531 break; 3532 3533 case EXPR_STRUCTURE: 3534 t = gfc_check_constructor (e, check_restricted); 3535 break; 3536 3537 case EXPR_ARRAY: 3538 t = gfc_check_constructor (e, check_restricted); 3539 break; 3540 3541 default: 3542 gfc_internal_error ("check_restricted(): Unknown expression type"); 3543 } 3544 3545 return t; 3546 } 3547 3548 3549 /* Check to see that an expression is a specification expression. If 3550 we return false, an error has been generated. */ 3551 3552 bool 3553 gfc_specification_expr (gfc_expr *e) 3554 { 3555 gfc_component *comp; 3556 3557 if (e == NULL) 3558 return true; 3559 3560 if (e->ts.type != BT_INTEGER) 3561 { 3562 gfc_error ("Expression at %L must be of INTEGER type, found %s", 3563 &e->where, gfc_basic_typename (e->ts.type)); 3564 return false; 3565 } 3566 3567 comp = gfc_get_proc_ptr_comp (e); 3568 if (e->expr_type == EXPR_FUNCTION 3569 && !e->value.function.isym 3570 && !e->value.function.esym 3571 && !gfc_pure (e->symtree->n.sym) 3572 && (!comp || !comp->attr.pure)) 3573 { 3574 gfc_error ("Function %qs at %L must be PURE", 3575 e->symtree->n.sym->name, &e->where); 3576 /* Prevent repeat error messages. */ 3577 e->symtree->n.sym->attr.pure = 1; 3578 return false; 3579 } 3580 3581 if (e->rank != 0) 3582 { 3583 gfc_error ("Expression at %L must be scalar", &e->where); 3584 return false; 3585 } 3586 3587 if (!gfc_simplify_expr (e, 0)) 3588 return false; 3589 3590 return check_restricted (e); 3591 } 3592 3593 3594 /************** Expression conformance checks. *************/ 3595 3596 /* Given two expressions, make sure that the arrays are conformable. */ 3597 3598 bool 3599 gfc_check_conformance (gfc_expr *op1, gfc_expr *op2, const char *optype_msgid, ...) 3600 { 3601 int op1_flag, op2_flag, d; 3602 mpz_t op1_size, op2_size; 3603 bool t; 3604 3605 va_list argp; 3606 char buffer[240]; 3607 3608 if (op1->rank == 0 || op2->rank == 0) 3609 return true; 3610 3611 va_start (argp, optype_msgid); 3612 d = vsnprintf (buffer, sizeof (buffer), optype_msgid, argp); 3613 va_end (argp); 3614 if (d < 1 || d >= (int) sizeof (buffer)) /* Reject truncation. */ 3615 gfc_internal_error ("optype_msgid overflow: %d", d); 3616 3617 if (op1->rank != op2->rank) 3618 { 3619 gfc_error ("Incompatible ranks in %s (%d and %d) at %L", _(buffer), 3620 op1->rank, op2->rank, &op1->where); 3621 return false; 3622 } 3623 3624 t = true; 3625 3626 for (d = 0; d < op1->rank; d++) 3627 { 3628 op1_flag = gfc_array_dimen_size(op1, d, &op1_size); 3629 op2_flag = gfc_array_dimen_size(op2, d, &op2_size); 3630 3631 if (op1_flag && op2_flag && mpz_cmp (op1_size, op2_size) != 0) 3632 { 3633 gfc_error ("Different shape for %s at %L on dimension %d " 3634 "(%d and %d)", _(buffer), &op1->where, d + 1, 3635 (int) mpz_get_si (op1_size), 3636 (int) mpz_get_si (op2_size)); 3637 3638 t = false; 3639 } 3640 3641 if (op1_flag) 3642 mpz_clear (op1_size); 3643 if (op2_flag) 3644 mpz_clear (op2_size); 3645 3646 if (!t) 3647 return false; 3648 } 3649 3650 return true; 3651 } 3652 3653 3654 /* Given an assignable expression and an arbitrary expression, make 3655 sure that the assignment can take place. Only add a call to the intrinsic 3656 conversion routines, when allow_convert is set. When this assign is a 3657 coarray call, then the convert is done by the coarray routine implictly and 3658 adding the intrinsic conversion would do harm in most cases. */ 3659 3660 bool 3661 gfc_check_assign (gfc_expr *lvalue, gfc_expr *rvalue, int conform, 3662 bool allow_convert) 3663 { 3664 gfc_symbol *sym; 3665 gfc_ref *ref; 3666 int has_pointer; 3667 3668 sym = lvalue->symtree->n.sym; 3669 3670 /* See if this is the component or subcomponent of a pointer and guard 3671 against assignment to LEN or KIND part-refs. */ 3672 has_pointer = sym->attr.pointer; 3673 for (ref = lvalue->ref; ref; ref = ref->next) 3674 { 3675 if (!has_pointer && ref->type == REF_COMPONENT 3676 && ref->u.c.component->attr.pointer) 3677 has_pointer = 1; 3678 else if (ref->type == REF_INQUIRY 3679 && (ref->u.i == INQUIRY_LEN || ref->u.i == INQUIRY_KIND)) 3680 { 3681 gfc_error ("Assignment to a LEN or KIND part_ref at %L is not " 3682 "allowed", &lvalue->where); 3683 return false; 3684 } 3685 } 3686 3687 /* 12.5.2.2, Note 12.26: The result variable is very similar to any other 3688 variable local to a function subprogram. Its existence begins when 3689 execution of the function is initiated and ends when execution of the 3690 function is terminated... 3691 Therefore, the left hand side is no longer a variable, when it is: */ 3692 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_ST_FUNCTION 3693 && !sym->attr.external) 3694 { 3695 bool bad_proc; 3696 bad_proc = false; 3697 3698 /* (i) Use associated; */ 3699 if (sym->attr.use_assoc) 3700 bad_proc = true; 3701 3702 /* (ii) The assignment is in the main program; or */ 3703 if (gfc_current_ns->proc_name 3704 && gfc_current_ns->proc_name->attr.is_main_program) 3705 bad_proc = true; 3706 3707 /* (iii) A module or internal procedure... */ 3708 if (gfc_current_ns->proc_name 3709 && (gfc_current_ns->proc_name->attr.proc == PROC_INTERNAL 3710 || gfc_current_ns->proc_name->attr.proc == PROC_MODULE) 3711 && gfc_current_ns->parent 3712 && (!(gfc_current_ns->parent->proc_name->attr.function 3713 || gfc_current_ns->parent->proc_name->attr.subroutine) 3714 || gfc_current_ns->parent->proc_name->attr.is_main_program)) 3715 { 3716 /* ... that is not a function... */ 3717 if (gfc_current_ns->proc_name 3718 && !gfc_current_ns->proc_name->attr.function) 3719 bad_proc = true; 3720 3721 /* ... or is not an entry and has a different name. */ 3722 if (!sym->attr.entry && sym->name != gfc_current_ns->proc_name->name) 3723 bad_proc = true; 3724 } 3725 3726 /* (iv) Host associated and not the function symbol or the 3727 parent result. This picks up sibling references, which 3728 cannot be entries. */ 3729 if (!sym->attr.entry 3730 && sym->ns == gfc_current_ns->parent 3731 && sym != gfc_current_ns->proc_name 3732 && sym != gfc_current_ns->parent->proc_name->result) 3733 bad_proc = true; 3734 3735 if (bad_proc) 3736 { 3737 gfc_error ("%qs at %L is not a VALUE", sym->name, &lvalue->where); 3738 return false; 3739 } 3740 } 3741 else 3742 { 3743 /* Reject assigning to an external symbol. For initializers, this 3744 was already done before, in resolve_fl_procedure. */ 3745 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external 3746 && sym->attr.proc != PROC_MODULE && !rvalue->error) 3747 { 3748 gfc_error ("Illegal assignment to external procedure at %L", 3749 &lvalue->where); 3750 return false; 3751 } 3752 } 3753 3754 if (rvalue->rank != 0 && lvalue->rank != rvalue->rank) 3755 { 3756 gfc_error ("Incompatible ranks %d and %d in assignment at %L", 3757 lvalue->rank, rvalue->rank, &lvalue->where); 3758 return false; 3759 } 3760 3761 if (lvalue->ts.type == BT_UNKNOWN) 3762 { 3763 gfc_error ("Variable type is UNKNOWN in assignment at %L", 3764 &lvalue->where); 3765 return false; 3766 } 3767 3768 if (rvalue->expr_type == EXPR_NULL) 3769 { 3770 if (has_pointer && (ref == NULL || ref->next == NULL) 3771 && lvalue->symtree->n.sym->attr.data) 3772 return true; 3773 else 3774 { 3775 gfc_error ("NULL appears on right-hand side in assignment at %L", 3776 &rvalue->where); 3777 return false; 3778 } 3779 } 3780 3781 /* This is possibly a typo: x = f() instead of x => f(). */ 3782 if (warn_surprising 3783 && rvalue->expr_type == EXPR_FUNCTION && gfc_expr_attr (rvalue).pointer) 3784 gfc_warning (OPT_Wsurprising, 3785 "POINTER-valued function appears on right-hand side of " 3786 "assignment at %L", &rvalue->where); 3787 3788 /* Check size of array assignments. */ 3789 if (lvalue->rank != 0 && rvalue->rank != 0 3790 && !gfc_check_conformance (lvalue, rvalue, _("array assignment"))) 3791 return false; 3792 3793 /* Handle the case of a BOZ literal on the RHS. */ 3794 if (rvalue->ts.type == BT_BOZ) 3795 { 3796 if (lvalue->symtree->n.sym->attr.data) 3797 { 3798 if (lvalue->ts.type == BT_INTEGER 3799 && gfc_boz2int (rvalue, lvalue->ts.kind)) 3800 return true; 3801 3802 if (lvalue->ts.type == BT_REAL 3803 && gfc_boz2real (rvalue, lvalue->ts.kind)) 3804 { 3805 if (gfc_invalid_boz ("BOZ literal constant near %L cannot " 3806 "be assigned to a REAL variable", 3807 &rvalue->where)) 3808 return false; 3809 return true; 3810 } 3811 } 3812 3813 if (!lvalue->symtree->n.sym->attr.data 3814 && gfc_invalid_boz ("BOZ literal constant at %L is neither a " 3815 "data-stmt-constant nor an actual argument to " 3816 "INT, REAL, DBLE, or CMPLX intrinsic function", 3817 &rvalue->where)) 3818 return false; 3819 3820 if (lvalue->ts.type == BT_INTEGER 3821 && gfc_boz2int (rvalue, lvalue->ts.kind)) 3822 return true; 3823 3824 if (lvalue->ts.type == BT_REAL 3825 && gfc_boz2real (rvalue, lvalue->ts.kind)) 3826 return true; 3827 3828 gfc_error ("BOZ literal constant near %L cannot be assigned to a " 3829 "%qs variable", &rvalue->where, gfc_typename (lvalue)); 3830 return false; 3831 } 3832 3833 if (gfc_expr_attr (lvalue).pdt_kind || gfc_expr_attr (lvalue).pdt_len) 3834 { 3835 gfc_error ("The assignment to a KIND or LEN component of a " 3836 "parameterized type at %L is not allowed", 3837 &lvalue->where); 3838 return false; 3839 } 3840 3841 if (gfc_compare_types (&lvalue->ts, &rvalue->ts)) 3842 return true; 3843 3844 /* Only DATA Statements come here. */ 3845 if (!conform) 3846 { 3847 locus *where; 3848 3849 /* Numeric can be converted to any other numeric. And Hollerith can be 3850 converted to any other type. */ 3851 if ((gfc_numeric_ts (&lvalue->ts) && gfc_numeric_ts (&rvalue->ts)) 3852 || rvalue->ts.type == BT_HOLLERITH) 3853 return true; 3854 3855 if (flag_dec_char_conversions && (gfc_numeric_ts (&lvalue->ts) 3856 || lvalue->ts.type == BT_LOGICAL) 3857 && rvalue->ts.type == BT_CHARACTER 3858 && rvalue->ts.kind == gfc_default_character_kind) 3859 return true; 3860 3861 if (lvalue->ts.type == BT_LOGICAL && rvalue->ts.type == BT_LOGICAL) 3862 return true; 3863 3864 where = lvalue->where.lb ? &lvalue->where : &rvalue->where; 3865 gfc_error ("Incompatible types in DATA statement at %L; attempted " 3866 "conversion of %s to %s", where, 3867 gfc_typename (rvalue), gfc_typename (lvalue)); 3868 3869 return false; 3870 } 3871 3872 /* Assignment is the only case where character variables of different 3873 kind values can be converted into one another. */ 3874 if (lvalue->ts.type == BT_CHARACTER && rvalue->ts.type == BT_CHARACTER) 3875 { 3876 if (lvalue->ts.kind != rvalue->ts.kind && allow_convert) 3877 return gfc_convert_chartype (rvalue, &lvalue->ts); 3878 else 3879 return true; 3880 } 3881 3882 if (!allow_convert) 3883 return true; 3884 3885 return gfc_convert_type (rvalue, &lvalue->ts, 1); 3886 } 3887 3888 3889 /* Check that a pointer assignment is OK. We first check lvalue, and 3890 we only check rvalue if it's not an assignment to NULL() or a 3891 NULLIFY statement. */ 3892 3893 bool 3894 gfc_check_pointer_assign (gfc_expr *lvalue, gfc_expr *rvalue, 3895 bool suppress_type_test, bool is_init_expr) 3896 { 3897 symbol_attribute attr, lhs_attr; 3898 gfc_ref *ref; 3899 bool is_pure, is_implicit_pure, rank_remap; 3900 int proc_pointer; 3901 bool same_rank; 3902 3903 if (!lvalue->symtree) 3904 return false; 3905 3906 lhs_attr = gfc_expr_attr (lvalue); 3907 if (lvalue->ts.type == BT_UNKNOWN && !lhs_attr.proc_pointer) 3908 { 3909 gfc_error ("Pointer assignment target is not a POINTER at %L", 3910 &lvalue->where); 3911 return false; 3912 } 3913 3914 if (lhs_attr.flavor == FL_PROCEDURE && lhs_attr.use_assoc 3915 && !lhs_attr.proc_pointer) 3916 { 3917 gfc_error ("%qs in the pointer assignment at %L cannot be an " 3918 "l-value since it is a procedure", 3919 lvalue->symtree->n.sym->name, &lvalue->where); 3920 return false; 3921 } 3922 3923 proc_pointer = lvalue->symtree->n.sym->attr.proc_pointer; 3924 3925 rank_remap = false; 3926 same_rank = lvalue->rank == rvalue->rank; 3927 for (ref = lvalue->ref; ref; ref = ref->next) 3928 { 3929 if (ref->type == REF_COMPONENT) 3930 proc_pointer = ref->u.c.component->attr.proc_pointer; 3931 3932 if (ref->type == REF_ARRAY && ref->next == NULL) 3933 { 3934 int dim; 3935 3936 if (ref->u.ar.type == AR_FULL) 3937 break; 3938 3939 if (ref->u.ar.type != AR_SECTION) 3940 { 3941 gfc_error ("Expected bounds specification for %qs at %L", 3942 lvalue->symtree->n.sym->name, &lvalue->where); 3943 return false; 3944 } 3945 3946 if (!gfc_notify_std (GFC_STD_F2003, "Bounds specification " 3947 "for %qs in pointer assignment at %L", 3948 lvalue->symtree->n.sym->name, &lvalue->where)) 3949 return false; 3950 3951 /* Fortran standard (e.g. F2018, 10.2.2 Pointer assignment): 3952 * 3953 * (C1017) If bounds-spec-list is specified, the number of 3954 * bounds-specs shall equal the rank of data-pointer-object. 3955 * 3956 * If bounds-spec-list appears, it specifies the lower bounds. 3957 * 3958 * (C1018) If bounds-remapping-list is specified, the number of 3959 * bounds-remappings shall equal the rank of data-pointer-object. 3960 * 3961 * If bounds-remapping-list appears, it specifies the upper and 3962 * lower bounds of each dimension of the pointer; the pointer target 3963 * shall be simply contiguous or of rank one. 3964 * 3965 * (C1019) If bounds-remapping-list is not specified, the ranks of 3966 * data-pointer-object and data-target shall be the same. 3967 * 3968 * Thus when bounds are given, all lbounds are necessary and either 3969 * all or none of the upper bounds; no strides are allowed. If the 3970 * upper bounds are present, we may do rank remapping. */ 3971 for (dim = 0; dim < ref->u.ar.dimen; ++dim) 3972 { 3973 if (ref->u.ar.stride[dim]) 3974 { 3975 gfc_error ("Stride must not be present at %L", 3976 &lvalue->where); 3977 return false; 3978 } 3979 if (!same_rank && (!ref->u.ar.start[dim] ||!ref->u.ar.end[dim])) 3980 { 3981 gfc_error ("Rank remapping requires a " 3982 "list of %<lower-bound : upper-bound%> " 3983 "specifications at %L", &lvalue->where); 3984 return false; 3985 } 3986 if (!ref->u.ar.start[dim] 3987 || ref->u.ar.dimen_type[dim] != DIMEN_RANGE) 3988 { 3989 gfc_error ("Expected list of %<lower-bound :%> or " 3990 "list of %<lower-bound : upper-bound%> " 3991 "specifications at %L", &lvalue->where); 3992 return false; 3993 } 3994 3995 if (dim == 0) 3996 rank_remap = (ref->u.ar.end[dim] != NULL); 3997 else 3998 { 3999 if ((rank_remap && !ref->u.ar.end[dim])) 4000 { 4001 gfc_error ("Rank remapping requires a " 4002 "list of %<lower-bound : upper-bound%> " 4003 "specifications at %L", &lvalue->where); 4004 return false; 4005 } 4006 if (!rank_remap && ref->u.ar.end[dim]) 4007 { 4008 gfc_error ("Expected list of %<lower-bound :%> or " 4009 "list of %<lower-bound : upper-bound%> " 4010 "specifications at %L", &lvalue->where); 4011 return false; 4012 } 4013 } 4014 } 4015 } 4016 } 4017 4018 is_pure = gfc_pure (NULL); 4019 is_implicit_pure = gfc_implicit_pure (NULL); 4020 4021 /* If rvalue is a NULL() or NULLIFY, we're done. Otherwise the type, 4022 kind, etc for lvalue and rvalue must match, and rvalue must be a 4023 pure variable if we're in a pure function. */ 4024 if (rvalue->expr_type == EXPR_NULL && rvalue->ts.type == BT_UNKNOWN) 4025 return true; 4026 4027 /* F2008, C723 (pointer) and C726 (proc-pointer); for PURE also C1283. */ 4028 if (lvalue->expr_type == EXPR_VARIABLE 4029 && gfc_is_coindexed (lvalue)) 4030 { 4031 gfc_ref *ref; 4032 for (ref = lvalue->ref; ref; ref = ref->next) 4033 if (ref->type == REF_ARRAY && ref->u.ar.codimen) 4034 { 4035 gfc_error ("Pointer object at %L shall not have a coindex", 4036 &lvalue->where); 4037 return false; 4038 } 4039 } 4040 4041 /* Checks on rvalue for procedure pointer assignments. */ 4042 if (proc_pointer) 4043 { 4044 char err[200]; 4045 gfc_symbol *s1,*s2; 4046 gfc_component *comp1, *comp2; 4047 const char *name; 4048 4049 attr = gfc_expr_attr (rvalue); 4050 if (!((rvalue->expr_type == EXPR_NULL) 4051 || (rvalue->expr_type == EXPR_FUNCTION && attr.proc_pointer) 4052 || (rvalue->expr_type == EXPR_VARIABLE && attr.proc_pointer) 4053 || (rvalue->expr_type == EXPR_VARIABLE 4054 && attr.flavor == FL_PROCEDURE))) 4055 { 4056 gfc_error ("Invalid procedure pointer assignment at %L", 4057 &rvalue->where); 4058 return false; 4059 } 4060 4061 if (rvalue->expr_type == EXPR_VARIABLE && !attr.proc_pointer) 4062 { 4063 /* Check for intrinsics. */ 4064 gfc_symbol *sym = rvalue->symtree->n.sym; 4065 if (!sym->attr.intrinsic 4066 && (gfc_is_intrinsic (sym, 0, sym->declared_at) 4067 || gfc_is_intrinsic (sym, 1, sym->declared_at))) 4068 { 4069 sym->attr.intrinsic = 1; 4070 gfc_resolve_intrinsic (sym, &rvalue->where); 4071 attr = gfc_expr_attr (rvalue); 4072 } 4073 /* Check for result of embracing function. */ 4074 if (sym->attr.function && sym->result == sym) 4075 { 4076 gfc_namespace *ns; 4077 4078 for (ns = gfc_current_ns; ns; ns = ns->parent) 4079 if (sym == ns->proc_name) 4080 { 4081 gfc_error ("Function result %qs is invalid as proc-target " 4082 "in procedure pointer assignment at %L", 4083 sym->name, &rvalue->where); 4084 return false; 4085 } 4086 } 4087 } 4088 if (attr.abstract) 4089 { 4090 gfc_error ("Abstract interface %qs is invalid " 4091 "in procedure pointer assignment at %L", 4092 rvalue->symtree->name, &rvalue->where); 4093 return false; 4094 } 4095 /* Check for F08:C729. */ 4096 if (attr.flavor == FL_PROCEDURE) 4097 { 4098 if (attr.proc == PROC_ST_FUNCTION) 4099 { 4100 gfc_error ("Statement function %qs is invalid " 4101 "in procedure pointer assignment at %L", 4102 rvalue->symtree->name, &rvalue->where); 4103 return false; 4104 } 4105 if (attr.proc == PROC_INTERNAL && 4106 !gfc_notify_std(GFC_STD_F2008, "Internal procedure %qs " 4107 "is invalid in procedure pointer assignment " 4108 "at %L", rvalue->symtree->name, &rvalue->where)) 4109 return false; 4110 if (attr.intrinsic && gfc_intrinsic_actual_ok (rvalue->symtree->name, 4111 attr.subroutine) == 0) 4112 { 4113 gfc_error ("Intrinsic %qs at %L is invalid in procedure pointer " 4114 "assignment", rvalue->symtree->name, &rvalue->where); 4115 return false; 4116 } 4117 } 4118 /* Check for F08:C730. */ 4119 if (attr.elemental && !attr.intrinsic) 4120 { 4121 gfc_error ("Nonintrinsic elemental procedure %qs is invalid " 4122 "in procedure pointer assignment at %L", 4123 rvalue->symtree->name, &rvalue->where); 4124 return false; 4125 } 4126 4127 /* Ensure that the calling convention is the same. As other attributes 4128 such as DLLEXPORT may differ, one explicitly only tests for the 4129 calling conventions. */ 4130 if (rvalue->expr_type == EXPR_VARIABLE 4131 && lvalue->symtree->n.sym->attr.ext_attr 4132 != rvalue->symtree->n.sym->attr.ext_attr) 4133 { 4134 symbol_attribute calls; 4135 4136 calls.ext_attr = 0; 4137 gfc_add_ext_attribute (&calls, EXT_ATTR_CDECL, NULL); 4138 gfc_add_ext_attribute (&calls, EXT_ATTR_STDCALL, NULL); 4139 gfc_add_ext_attribute (&calls, EXT_ATTR_FASTCALL, NULL); 4140 4141 if ((calls.ext_attr & lvalue->symtree->n.sym->attr.ext_attr) 4142 != (calls.ext_attr & rvalue->symtree->n.sym->attr.ext_attr)) 4143 { 4144 gfc_error ("Mismatch in the procedure pointer assignment " 4145 "at %L: mismatch in the calling convention", 4146 &rvalue->where); 4147 return false; 4148 } 4149 } 4150 4151 comp1 = gfc_get_proc_ptr_comp (lvalue); 4152 if (comp1) 4153 s1 = comp1->ts.interface; 4154 else 4155 { 4156 s1 = lvalue->symtree->n.sym; 4157 if (s1->ts.interface) 4158 s1 = s1->ts.interface; 4159 } 4160 4161 comp2 = gfc_get_proc_ptr_comp (rvalue); 4162 if (comp2) 4163 { 4164 if (rvalue->expr_type == EXPR_FUNCTION) 4165 { 4166 s2 = comp2->ts.interface->result; 4167 name = s2->name; 4168 } 4169 else 4170 { 4171 s2 = comp2->ts.interface; 4172 name = comp2->name; 4173 } 4174 } 4175 else if (rvalue->expr_type == EXPR_FUNCTION) 4176 { 4177 if (rvalue->value.function.esym) 4178 s2 = rvalue->value.function.esym->result; 4179 else 4180 s2 = rvalue->symtree->n.sym->result; 4181 4182 name = s2->name; 4183 } 4184 else 4185 { 4186 s2 = rvalue->symtree->n.sym; 4187 name = s2->name; 4188 } 4189 4190 if (s2 && s2->attr.proc_pointer && s2->ts.interface) 4191 s2 = s2->ts.interface; 4192 4193 /* Special check for the case of absent interface on the lvalue. 4194 * All other interface checks are done below. */ 4195 if (!s1 && comp1 && comp1->attr.subroutine && s2 && s2->attr.function) 4196 { 4197 gfc_error ("Interface mismatch in procedure pointer assignment " 4198 "at %L: %qs is not a subroutine", &rvalue->where, name); 4199 return false; 4200 } 4201 4202 /* F08:7.2.2.4 (4) */ 4203 if (s2 && gfc_explicit_interface_required (s2, err, sizeof(err))) 4204 { 4205 if (comp1 && !s1) 4206 { 4207 gfc_error ("Explicit interface required for component %qs at %L: %s", 4208 comp1->name, &lvalue->where, err); 4209 return false; 4210 } 4211 else if (s1->attr.if_source == IFSRC_UNKNOWN) 4212 { 4213 gfc_error ("Explicit interface required for %qs at %L: %s", 4214 s1->name, &lvalue->where, err); 4215 return false; 4216 } 4217 } 4218 if (s1 && gfc_explicit_interface_required (s1, err, sizeof(err))) 4219 { 4220 if (comp2 && !s2) 4221 { 4222 gfc_error ("Explicit interface required for component %qs at %L: %s", 4223 comp2->name, &rvalue->where, err); 4224 return false; 4225 } 4226 else if (s2->attr.if_source == IFSRC_UNKNOWN) 4227 { 4228 gfc_error ("Explicit interface required for %qs at %L: %s", 4229 s2->name, &rvalue->where, err); 4230 return false; 4231 } 4232 } 4233 4234 if (s1 == s2 || !s1 || !s2) 4235 return true; 4236 4237 if (!gfc_compare_interfaces (s1, s2, name, 0, 1, 4238 err, sizeof(err), NULL, NULL)) 4239 { 4240 gfc_error ("Interface mismatch in procedure pointer assignment " 4241 "at %L: %s", &rvalue->where, err); 4242 return false; 4243 } 4244 4245 /* Check F2008Cor2, C729. */ 4246 if (!s2->attr.intrinsic && s2->attr.if_source == IFSRC_UNKNOWN 4247 && !s2->attr.external && !s2->attr.subroutine && !s2->attr.function) 4248 { 4249 gfc_error ("Procedure pointer target %qs at %L must be either an " 4250 "intrinsic, host or use associated, referenced or have " 4251 "the EXTERNAL attribute", s2->name, &rvalue->where); 4252 return false; 4253 } 4254 4255 return true; 4256 } 4257 else 4258 { 4259 /* A non-proc pointer cannot point to a constant. */ 4260 if (rvalue->expr_type == EXPR_CONSTANT) 4261 { 4262 gfc_error_now ("Pointer assignment target cannot be a constant at %L", 4263 &rvalue->where); 4264 return false; 4265 } 4266 } 4267 4268 if (!gfc_compare_types (&lvalue->ts, &rvalue->ts)) 4269 { 4270 /* Check for F03:C717. */ 4271 if (UNLIMITED_POLY (rvalue) 4272 && !(UNLIMITED_POLY (lvalue) 4273 || (lvalue->ts.type == BT_DERIVED 4274 && (lvalue->ts.u.derived->attr.is_bind_c 4275 || lvalue->ts.u.derived->attr.sequence)))) 4276 gfc_error ("Data-pointer-object at %L must be unlimited " 4277 "polymorphic, or of a type with the BIND or SEQUENCE " 4278 "attribute, to be compatible with an unlimited " 4279 "polymorphic target", &lvalue->where); 4280 else if (!suppress_type_test) 4281 gfc_error ("Different types in pointer assignment at %L; " 4282 "attempted assignment of %s to %s", &lvalue->where, 4283 gfc_typename (rvalue), gfc_typename (lvalue)); 4284 return false; 4285 } 4286 4287 if (lvalue->ts.type != BT_CLASS && lvalue->ts.kind != rvalue->ts.kind) 4288 { 4289 gfc_error ("Different kind type parameters in pointer " 4290 "assignment at %L", &lvalue->where); 4291 return false; 4292 } 4293 4294 if (lvalue->rank != rvalue->rank && !rank_remap) 4295 { 4296 gfc_error ("Different ranks in pointer assignment at %L", &lvalue->where); 4297 return false; 4298 } 4299 4300 /* Make sure the vtab is present. */ 4301 if (lvalue->ts.type == BT_CLASS && !UNLIMITED_POLY (rvalue)) 4302 gfc_find_vtab (&rvalue->ts); 4303 4304 /* Check rank remapping. */ 4305 if (rank_remap) 4306 { 4307 mpz_t lsize, rsize; 4308 4309 /* If this can be determined, check that the target must be at least as 4310 large as the pointer assigned to it is. */ 4311 if (gfc_array_size (lvalue, &lsize) 4312 && gfc_array_size (rvalue, &rsize) 4313 && mpz_cmp (rsize, lsize) < 0) 4314 { 4315 gfc_error ("Rank remapping target is smaller than size of the" 4316 " pointer (%ld < %ld) at %L", 4317 mpz_get_si (rsize), mpz_get_si (lsize), 4318 &lvalue->where); 4319 return false; 4320 } 4321 4322 /* The target must be either rank one or it must be simply contiguous 4323 and F2008 must be allowed. */ 4324 if (rvalue->rank != 1) 4325 { 4326 if (!gfc_is_simply_contiguous (rvalue, true, false)) 4327 { 4328 gfc_error ("Rank remapping target must be rank 1 or" 4329 " simply contiguous at %L", &rvalue->where); 4330 return false; 4331 } 4332 if (!gfc_notify_std (GFC_STD_F2008, "Rank remapping target is not " 4333 "rank 1 at %L", &rvalue->where)) 4334 return false; 4335 } 4336 } 4337 4338 /* Now punt if we are dealing with a NULLIFY(X) or X = NULL(X). */ 4339 if (rvalue->expr_type == EXPR_NULL) 4340 return true; 4341 4342 if (rvalue->expr_type == EXPR_VARIABLE && is_subref_array (rvalue)) 4343 lvalue->symtree->n.sym->attr.subref_array_pointer = 1; 4344 4345 attr = gfc_expr_attr (rvalue); 4346 4347 if (rvalue->expr_type == EXPR_FUNCTION && !attr.pointer) 4348 { 4349 /* F2008, C725. For PURE also C1283. Sometimes rvalue is a function call 4350 to caf_get. Map this to the same error message as below when it is 4351 still a variable expression. */ 4352 if (rvalue->value.function.isym 4353 && rvalue->value.function.isym->id == GFC_ISYM_CAF_GET) 4354 /* The test above might need to be extend when F08, Note 5.4 has to be 4355 interpreted in the way that target and pointer with the same coindex 4356 are allowed. */ 4357 gfc_error ("Data target at %L shall not have a coindex", 4358 &rvalue->where); 4359 else 4360 gfc_error ("Target expression in pointer assignment " 4361 "at %L must deliver a pointer result", 4362 &rvalue->where); 4363 return false; 4364 } 4365 4366 if (is_init_expr) 4367 { 4368 gfc_symbol *sym; 4369 bool target; 4370 gfc_ref *ref; 4371 4372 if (gfc_is_size_zero_array (rvalue)) 4373 { 4374 gfc_error ("Zero-sized array detected at %L where an entity with " 4375 "the TARGET attribute is expected", &rvalue->where); 4376 return false; 4377 } 4378 else if (!rvalue->symtree) 4379 { 4380 gfc_error ("Pointer assignment target in initialization expression " 4381 "does not have the TARGET attribute at %L", 4382 &rvalue->where); 4383 return false; 4384 } 4385 4386 sym = rvalue->symtree->n.sym; 4387 4388 if (sym->ts.type == BT_CLASS && sym->attr.class_ok) 4389 target = CLASS_DATA (sym)->attr.target; 4390 else 4391 target = sym->attr.target; 4392 4393 if (!target && !proc_pointer) 4394 { 4395 gfc_error ("Pointer assignment target in initialization expression " 4396 "does not have the TARGET attribute at %L", 4397 &rvalue->where); 4398 return false; 4399 } 4400 4401 for (ref = rvalue->ref; ref; ref = ref->next) 4402 { 4403 switch (ref->type) 4404 { 4405 case REF_ARRAY: 4406 for (int n = 0; n < ref->u.ar.dimen; n++) 4407 if (!gfc_is_constant_expr (ref->u.ar.start[n]) 4408 || !gfc_is_constant_expr (ref->u.ar.end[n]) 4409 || !gfc_is_constant_expr (ref->u.ar.stride[n])) 4410 { 4411 gfc_error ("Every subscript of target specification " 4412 "at %L must be a constant expression", 4413 &ref->u.ar.where); 4414 return false; 4415 } 4416 break; 4417 4418 case REF_SUBSTRING: 4419 if (!gfc_is_constant_expr (ref->u.ss.start) 4420 || !gfc_is_constant_expr (ref->u.ss.end)) 4421 { 4422 gfc_error ("Substring starting and ending points of target " 4423 "specification at %L must be constant expressions", 4424 &ref->u.ss.start->where); 4425 return false; 4426 } 4427 break; 4428 4429 default: 4430 break; 4431 } 4432 } 4433 } 4434 else 4435 { 4436 if (!attr.target && !attr.pointer) 4437 { 4438 gfc_error ("Pointer assignment target is neither TARGET " 4439 "nor POINTER at %L", &rvalue->where); 4440 return false; 4441 } 4442 } 4443 4444 if (lvalue->ts.type == BT_CHARACTER) 4445 { 4446 bool t = gfc_check_same_strlen (lvalue, rvalue, "pointer assignment"); 4447 if (!t) 4448 return false; 4449 } 4450 4451 if (is_pure && gfc_impure_variable (rvalue->symtree->n.sym)) 4452 { 4453 gfc_error ("Bad target in pointer assignment in PURE " 4454 "procedure at %L", &rvalue->where); 4455 } 4456 4457 if (is_implicit_pure && gfc_impure_variable (rvalue->symtree->n.sym)) 4458 gfc_unset_implicit_pure (gfc_current_ns->proc_name); 4459 4460 if (gfc_has_vector_index (rvalue)) 4461 { 4462 gfc_error ("Pointer assignment with vector subscript " 4463 "on rhs at %L", &rvalue->where); 4464 return false; 4465 } 4466 4467 if (attr.is_protected && attr.use_assoc 4468 && !(attr.pointer || attr.proc_pointer)) 4469 { 4470 gfc_error ("Pointer assignment target has PROTECTED " 4471 "attribute at %L", &rvalue->where); 4472 return false; 4473 } 4474 4475 /* F2008, C725. For PURE also C1283. */ 4476 if (rvalue->expr_type == EXPR_VARIABLE 4477 && gfc_is_coindexed (rvalue)) 4478 { 4479 gfc_ref *ref; 4480 for (ref = rvalue->ref; ref; ref = ref->next) 4481 if (ref->type == REF_ARRAY && ref->u.ar.codimen) 4482 { 4483 gfc_error ("Data target at %L shall not have a coindex", 4484 &rvalue->where); 4485 return false; 4486 } 4487 } 4488 4489 /* Warn for assignments of contiguous pointers to targets which is not 4490 contiguous. Be lenient in the definition of what counts as 4491 contiguous. */ 4492 4493 if (lhs_attr.contiguous 4494 && lhs_attr.dimension > 0) 4495 { 4496 if (gfc_is_not_contiguous (rvalue)) 4497 { 4498 gfc_error ("Assignment to contiguous pointer from " 4499 "non-contiguous target at %L", &rvalue->where); 4500 return false; 4501 } 4502 if (!gfc_is_simply_contiguous (rvalue, false, true)) 4503 gfc_warning (OPT_Wextra, "Assignment to contiguous pointer from " 4504 "non-contiguous target at %L", &rvalue->where); 4505 } 4506 4507 /* Warn if it is the LHS pointer may lives longer than the RHS target. */ 4508 if (warn_target_lifetime 4509 && rvalue->expr_type == EXPR_VARIABLE 4510 && !rvalue->symtree->n.sym->attr.save 4511 && !rvalue->symtree->n.sym->attr.pointer && !attr.pointer 4512 && !rvalue->symtree->n.sym->attr.host_assoc 4513 && !rvalue->symtree->n.sym->attr.in_common 4514 && !rvalue->symtree->n.sym->attr.use_assoc 4515 && !rvalue->symtree->n.sym->attr.dummy) 4516 { 4517 bool warn; 4518 gfc_namespace *ns; 4519 4520 warn = lvalue->symtree->n.sym->attr.dummy 4521 || lvalue->symtree->n.sym->attr.result 4522 || lvalue->symtree->n.sym->attr.function 4523 || (lvalue->symtree->n.sym->attr.host_assoc 4524 && lvalue->symtree->n.sym->ns 4525 != rvalue->symtree->n.sym->ns) 4526 || lvalue->symtree->n.sym->attr.use_assoc 4527 || lvalue->symtree->n.sym->attr.in_common; 4528 4529 if (rvalue->symtree->n.sym->ns->proc_name 4530 && rvalue->symtree->n.sym->ns->proc_name->attr.flavor != FL_PROCEDURE 4531 && rvalue->symtree->n.sym->ns->proc_name->attr.flavor != FL_PROGRAM) 4532 for (ns = rvalue->symtree->n.sym->ns; 4533 ns && ns->proc_name && ns->proc_name->attr.flavor != FL_PROCEDURE; 4534 ns = ns->parent) 4535 if (ns->parent == lvalue->symtree->n.sym->ns) 4536 { 4537 warn = true; 4538 break; 4539 } 4540 4541 if (warn) 4542 gfc_warning (OPT_Wtarget_lifetime, 4543 "Pointer at %L in pointer assignment might outlive the " 4544 "pointer target", &lvalue->where); 4545 } 4546 4547 return true; 4548 } 4549 4550 4551 /* Relative of gfc_check_assign() except that the lvalue is a single 4552 symbol. Used for initialization assignments. */ 4553 4554 bool 4555 gfc_check_assign_symbol (gfc_symbol *sym, gfc_component *comp, gfc_expr *rvalue) 4556 { 4557 gfc_expr lvalue; 4558 bool r; 4559 bool pointer, proc_pointer; 4560 4561 memset (&lvalue, '\0', sizeof (gfc_expr)); 4562 4563 lvalue.expr_type = EXPR_VARIABLE; 4564 lvalue.ts = sym->ts; 4565 if (sym->as) 4566 lvalue.rank = sym->as->rank; 4567 lvalue.symtree = XCNEW (gfc_symtree); 4568 lvalue.symtree->n.sym = sym; 4569 lvalue.where = sym->declared_at; 4570 4571 if (comp) 4572 { 4573 lvalue.ref = gfc_get_ref (); 4574 lvalue.ref->type = REF_COMPONENT; 4575 lvalue.ref->u.c.component = comp; 4576 lvalue.ref->u.c.sym = sym; 4577 lvalue.ts = comp->ts; 4578 lvalue.rank = comp->as ? comp->as->rank : 0; 4579 lvalue.where = comp->loc; 4580 pointer = comp->ts.type == BT_CLASS && CLASS_DATA (comp) 4581 ? CLASS_DATA (comp)->attr.class_pointer : comp->attr.pointer; 4582 proc_pointer = comp->attr.proc_pointer; 4583 } 4584 else 4585 { 4586 pointer = sym->ts.type == BT_CLASS && CLASS_DATA (sym) 4587 ? CLASS_DATA (sym)->attr.class_pointer : sym->attr.pointer; 4588 proc_pointer = sym->attr.proc_pointer; 4589 } 4590 4591 if (pointer || proc_pointer) 4592 r = gfc_check_pointer_assign (&lvalue, rvalue, false, true); 4593 else 4594 { 4595 /* If a conversion function, e.g., __convert_i8_i4, was inserted 4596 into an array constructor, we should check if it can be reduced 4597 as an initialization expression. */ 4598 if (rvalue->expr_type == EXPR_FUNCTION 4599 && rvalue->value.function.isym 4600 && (rvalue->value.function.isym->conversion == 1)) 4601 gfc_check_init_expr (rvalue); 4602 4603 r = gfc_check_assign (&lvalue, rvalue, 1); 4604 } 4605 4606 free (lvalue.symtree); 4607 free (lvalue.ref); 4608 4609 if (!r) 4610 return r; 4611 4612 if (pointer && rvalue->expr_type != EXPR_NULL && !proc_pointer) 4613 { 4614 /* F08:C461. Additional checks for pointer initialization. */ 4615 symbol_attribute attr; 4616 attr = gfc_expr_attr (rvalue); 4617 if (attr.allocatable) 4618 { 4619 gfc_error ("Pointer initialization target at %L " 4620 "must not be ALLOCATABLE", &rvalue->where); 4621 return false; 4622 } 4623 if (!attr.target || attr.pointer) 4624 { 4625 gfc_error ("Pointer initialization target at %L " 4626 "must have the TARGET attribute", &rvalue->where); 4627 return false; 4628 } 4629 4630 if (!attr.save && rvalue->expr_type == EXPR_VARIABLE 4631 && rvalue->symtree->n.sym->ns->proc_name 4632 && rvalue->symtree->n.sym->ns->proc_name->attr.is_main_program) 4633 { 4634 rvalue->symtree->n.sym->ns->proc_name->attr.save = SAVE_IMPLICIT; 4635 attr.save = SAVE_IMPLICIT; 4636 } 4637 4638 if (!attr.save) 4639 { 4640 gfc_error ("Pointer initialization target at %L " 4641 "must have the SAVE attribute", &rvalue->where); 4642 return false; 4643 } 4644 } 4645 4646 if (proc_pointer && rvalue->expr_type != EXPR_NULL) 4647 { 4648 /* F08:C1220. Additional checks for procedure pointer initialization. */ 4649 symbol_attribute attr = gfc_expr_attr (rvalue); 4650 if (attr.proc_pointer) 4651 { 4652 gfc_error ("Procedure pointer initialization target at %L " 4653 "may not be a procedure pointer", &rvalue->where); 4654 return false; 4655 } 4656 if (attr.proc == PROC_INTERNAL) 4657 { 4658 gfc_error ("Internal procedure %qs is invalid in " 4659 "procedure pointer initialization at %L", 4660 rvalue->symtree->name, &rvalue->where); 4661 return false; 4662 } 4663 if (attr.dummy) 4664 { 4665 gfc_error ("Dummy procedure %qs is invalid in " 4666 "procedure pointer initialization at %L", 4667 rvalue->symtree->name, &rvalue->where); 4668 return false; 4669 } 4670 } 4671 4672 return true; 4673 } 4674 4675 /* Build an initializer for a local integer, real, complex, logical, or 4676 character variable, based on the command line flags finit-local-zero, 4677 finit-integer=, finit-real=, finit-logical=, and finit-character=. 4678 With force, an initializer is ALWAYS generated. */ 4679 4680 static gfc_expr * 4681 gfc_build_init_expr (gfc_typespec *ts, locus *where, bool force) 4682 { 4683 gfc_expr *init_expr; 4684 4685 /* Try to build an initializer expression. */ 4686 init_expr = gfc_get_constant_expr (ts->type, ts->kind, where); 4687 4688 /* If we want to force generation, make sure we default to zero. */ 4689 gfc_init_local_real init_real = flag_init_real; 4690 int init_logical = gfc_option.flag_init_logical; 4691 if (force) 4692 { 4693 if (init_real == GFC_INIT_REAL_OFF) 4694 init_real = GFC_INIT_REAL_ZERO; 4695 if (init_logical == GFC_INIT_LOGICAL_OFF) 4696 init_logical = GFC_INIT_LOGICAL_FALSE; 4697 } 4698 4699 /* We will only initialize integers, reals, complex, logicals, and 4700 characters, and only if the corresponding command-line flags 4701 were set. Otherwise, we free init_expr and return null. */ 4702 switch (ts->type) 4703 { 4704 case BT_INTEGER: 4705 if (force || gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF) 4706 mpz_set_si (init_expr->value.integer, 4707 gfc_option.flag_init_integer_value); 4708 else 4709 { 4710 gfc_free_expr (init_expr); 4711 init_expr = NULL; 4712 } 4713 break; 4714 4715 case BT_REAL: 4716 switch (init_real) 4717 { 4718 case GFC_INIT_REAL_SNAN: 4719 init_expr->is_snan = 1; 4720 /* Fall through. */ 4721 case GFC_INIT_REAL_NAN: 4722 mpfr_set_nan (init_expr->value.real); 4723 break; 4724 4725 case GFC_INIT_REAL_INF: 4726 mpfr_set_inf (init_expr->value.real, 1); 4727 break; 4728 4729 case GFC_INIT_REAL_NEG_INF: 4730 mpfr_set_inf (init_expr->value.real, -1); 4731 break; 4732 4733 case GFC_INIT_REAL_ZERO: 4734 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE); 4735 break; 4736 4737 default: 4738 gfc_free_expr (init_expr); 4739 init_expr = NULL; 4740 break; 4741 } 4742 break; 4743 4744 case BT_COMPLEX: 4745 switch (init_real) 4746 { 4747 case GFC_INIT_REAL_SNAN: 4748 init_expr->is_snan = 1; 4749 /* Fall through. */ 4750 case GFC_INIT_REAL_NAN: 4751 mpfr_set_nan (mpc_realref (init_expr->value.complex)); 4752 mpfr_set_nan (mpc_imagref (init_expr->value.complex)); 4753 break; 4754 4755 case GFC_INIT_REAL_INF: 4756 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1); 4757 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1); 4758 break; 4759 4760 case GFC_INIT_REAL_NEG_INF: 4761 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1); 4762 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1); 4763 break; 4764 4765 case GFC_INIT_REAL_ZERO: 4766 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE); 4767 break; 4768 4769 default: 4770 gfc_free_expr (init_expr); 4771 init_expr = NULL; 4772 break; 4773 } 4774 break; 4775 4776 case BT_LOGICAL: 4777 if (init_logical == GFC_INIT_LOGICAL_FALSE) 4778 init_expr->value.logical = 0; 4779 else if (init_logical == GFC_INIT_LOGICAL_TRUE) 4780 init_expr->value.logical = 1; 4781 else 4782 { 4783 gfc_free_expr (init_expr); 4784 init_expr = NULL; 4785 } 4786 break; 4787 4788 case BT_CHARACTER: 4789 /* For characters, the length must be constant in order to 4790 create a default initializer. */ 4791 if ((force || gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON) 4792 && ts->u.cl->length 4793 && ts->u.cl->length->expr_type == EXPR_CONSTANT) 4794 { 4795 HOST_WIDE_INT char_len = gfc_mpz_get_hwi (ts->u.cl->length->value.integer); 4796 init_expr->value.character.length = char_len; 4797 init_expr->value.character.string = gfc_get_wide_string (char_len+1); 4798 for (size_t i = 0; i < (size_t) char_len; i++) 4799 init_expr->value.character.string[i] 4800 = (unsigned char) gfc_option.flag_init_character_value; 4801 } 4802 else 4803 { 4804 gfc_free_expr (init_expr); 4805 init_expr = NULL; 4806 } 4807 if (!init_expr 4808 && (force || gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON) 4809 && ts->u.cl->length && flag_max_stack_var_size != 0) 4810 { 4811 gfc_actual_arglist *arg; 4812 init_expr = gfc_get_expr (); 4813 init_expr->where = *where; 4814 init_expr->ts = *ts; 4815 init_expr->expr_type = EXPR_FUNCTION; 4816 init_expr->value.function.isym = 4817 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT); 4818 init_expr->value.function.name = "repeat"; 4819 arg = gfc_get_actual_arglist (); 4820 arg->expr = gfc_get_character_expr (ts->kind, where, NULL, 1); 4821 arg->expr->value.character.string[0] = 4822 gfc_option.flag_init_character_value; 4823 arg->next = gfc_get_actual_arglist (); 4824 arg->next->expr = gfc_copy_expr (ts->u.cl->length); 4825 init_expr->value.function.actual = arg; 4826 } 4827 break; 4828 4829 default: 4830 gfc_free_expr (init_expr); 4831 init_expr = NULL; 4832 } 4833 4834 return init_expr; 4835 } 4836 4837 /* Invoke gfc_build_init_expr to create an initializer expression, but do not 4838 * require that an expression be built. */ 4839 4840 gfc_expr * 4841 gfc_build_default_init_expr (gfc_typespec *ts, locus *where) 4842 { 4843 return gfc_build_init_expr (ts, where, false); 4844 } 4845 4846 /* Apply an initialization expression to a typespec. Can be used for symbols or 4847 components. Similar to add_init_expr_to_sym in decl.cc; could probably be 4848 combined with some effort. */ 4849 4850 void 4851 gfc_apply_init (gfc_typespec *ts, symbol_attribute *attr, gfc_expr *init) 4852 { 4853 if (ts->type == BT_CHARACTER && !attr->pointer && init 4854 && ts->u.cl 4855 && ts->u.cl->length 4856 && ts->u.cl->length->expr_type == EXPR_CONSTANT 4857 && ts->u.cl->length->ts.type == BT_INTEGER) 4858 { 4859 HOST_WIDE_INT len = gfc_mpz_get_hwi (ts->u.cl->length->value.integer); 4860 4861 if (init->expr_type == EXPR_CONSTANT) 4862 gfc_set_constant_character_len (len, init, -1); 4863 else if (init 4864 && init->ts.type == BT_CHARACTER 4865 && init->ts.u.cl && init->ts.u.cl->length 4866 && mpz_cmp (ts->u.cl->length->value.integer, 4867 init->ts.u.cl->length->value.integer)) 4868 { 4869 gfc_constructor *ctor; 4870 ctor = gfc_constructor_first (init->value.constructor); 4871 4872 if (ctor) 4873 { 4874 bool has_ts = (init->ts.u.cl 4875 && init->ts.u.cl->length_from_typespec); 4876 4877 /* Remember the length of the first element for checking 4878 that all elements *in the constructor* have the same 4879 length. This need not be the length of the LHS! */ 4880 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT); 4881 gcc_assert (ctor->expr->ts.type == BT_CHARACTER); 4882 gfc_charlen_t first_len = ctor->expr->value.character.length; 4883 4884 for ( ; ctor; ctor = gfc_constructor_next (ctor)) 4885 if (ctor->expr->expr_type == EXPR_CONSTANT) 4886 { 4887 gfc_set_constant_character_len (len, ctor->expr, 4888 has_ts ? -1 : first_len); 4889 if (!ctor->expr->ts.u.cl) 4890 ctor->expr->ts.u.cl 4891 = gfc_new_charlen (gfc_current_ns, ts->u.cl); 4892 else 4893 ctor->expr->ts.u.cl->length 4894 = gfc_copy_expr (ts->u.cl->length); 4895 } 4896 } 4897 } 4898 } 4899 } 4900 4901 4902 /* Check whether an expression is a structure constructor and whether it has 4903 other values than NULL. */ 4904 4905 static bool 4906 is_non_empty_structure_constructor (gfc_expr * e) 4907 { 4908 if (e->expr_type != EXPR_STRUCTURE) 4909 return false; 4910 4911 gfc_constructor *cons = gfc_constructor_first (e->value.constructor); 4912 while (cons) 4913 { 4914 if (!cons->expr || cons->expr->expr_type != EXPR_NULL) 4915 return true; 4916 cons = gfc_constructor_next (cons); 4917 } 4918 return false; 4919 } 4920 4921 4922 /* Check for default initializer; sym->value is not enough 4923 as it is also set for EXPR_NULL of allocatables. */ 4924 4925 bool 4926 gfc_has_default_initializer (gfc_symbol *der) 4927 { 4928 gfc_component *c; 4929 4930 gcc_assert (gfc_fl_struct (der->attr.flavor)); 4931 for (c = der->components; c; c = c->next) 4932 if (gfc_bt_struct (c->ts.type)) 4933 { 4934 if (!c->attr.pointer && !c->attr.proc_pointer 4935 && !(c->attr.allocatable && der == c->ts.u.derived) 4936 && ((c->initializer 4937 && is_non_empty_structure_constructor (c->initializer)) 4938 || gfc_has_default_initializer (c->ts.u.derived))) 4939 return true; 4940 if (c->attr.pointer && c->initializer) 4941 return true; 4942 } 4943 else 4944 { 4945 if (c->initializer) 4946 return true; 4947 } 4948 4949 return false; 4950 } 4951 4952 4953 /* 4954 Generate an initializer expression which initializes the entirety of a union. 4955 A normal structure constructor is insufficient without undue effort, because 4956 components of maps may be oddly aligned/overlapped. (For example if a 4957 character is initialized from one map overtop a real from the other, only one 4958 byte of the real is actually initialized.) Unfortunately we don't know the 4959 size of the union right now, so we can't generate a proper initializer, but 4960 we use a NULL expr as a placeholder and do the right thing later in 4961 gfc_trans_subcomponent_assign. 4962 */ 4963 static gfc_expr * 4964 generate_union_initializer (gfc_component *un) 4965 { 4966 if (un == NULL || un->ts.type != BT_UNION) 4967 return NULL; 4968 4969 gfc_expr *placeholder = gfc_get_null_expr (&un->loc); 4970 placeholder->ts = un->ts; 4971 return placeholder; 4972 } 4973 4974 4975 /* Get the user-specified initializer for a union, if any. This means the user 4976 has said to initialize component(s) of a map. For simplicity's sake we 4977 only allow the user to initialize the first map. We don't have to worry 4978 about overlapping initializers as they are released early in resolution (see 4979 resolve_fl_struct). */ 4980 4981 static gfc_expr * 4982 get_union_initializer (gfc_symbol *union_type, gfc_component **map_p) 4983 { 4984 gfc_component *map; 4985 gfc_expr *init=NULL; 4986 4987 if (!union_type || union_type->attr.flavor != FL_UNION) 4988 return NULL; 4989 4990 for (map = union_type->components; map; map = map->next) 4991 { 4992 if (gfc_has_default_initializer (map->ts.u.derived)) 4993 { 4994 init = gfc_default_initializer (&map->ts); 4995 if (map_p) 4996 *map_p = map; 4997 break; 4998 } 4999 } 5000 5001 if (map_p && !init) 5002 *map_p = NULL; 5003 5004 return init; 5005 } 5006 5007 static bool 5008 class_allocatable (gfc_component *comp) 5009 { 5010 return comp->ts.type == BT_CLASS && comp->attr.class_ok && CLASS_DATA (comp) 5011 && CLASS_DATA (comp)->attr.allocatable; 5012 } 5013 5014 static bool 5015 class_pointer (gfc_component *comp) 5016 { 5017 return comp->ts.type == BT_CLASS && comp->attr.class_ok && CLASS_DATA (comp) 5018 && CLASS_DATA (comp)->attr.pointer; 5019 } 5020 5021 static bool 5022 comp_allocatable (gfc_component *comp) 5023 { 5024 return comp->attr.allocatable || class_allocatable (comp); 5025 } 5026 5027 static bool 5028 comp_pointer (gfc_component *comp) 5029 { 5030 return comp->attr.pointer 5031 || comp->attr.proc_pointer 5032 || comp->attr.class_pointer 5033 || class_pointer (comp); 5034 } 5035 5036 /* Fetch or generate an initializer for the given component. 5037 Only generate an initializer if generate is true. */ 5038 5039 static gfc_expr * 5040 component_initializer (gfc_component *c, bool generate) 5041 { 5042 gfc_expr *init = NULL; 5043 5044 /* Allocatable components always get EXPR_NULL. 5045 Pointer components are only initialized when generating, and only if they 5046 do not already have an initializer. */ 5047 if (comp_allocatable (c) || (generate && comp_pointer (c) && !c->initializer)) 5048 { 5049 init = gfc_get_null_expr (&c->loc); 5050 init->ts = c->ts; 5051 return init; 5052 } 5053 5054 /* See if we can find the initializer immediately. */ 5055 if (c->initializer || !generate) 5056 return c->initializer; 5057 5058 /* Recursively handle derived type components. */ 5059 else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS) 5060 init = gfc_generate_initializer (&c->ts, true); 5061 5062 else if (c->ts.type == BT_UNION && c->ts.u.derived->components) 5063 { 5064 gfc_component *map = NULL; 5065 gfc_constructor *ctor; 5066 gfc_expr *user_init; 5067 5068 /* If we don't have a user initializer and we aren't generating one, this 5069 union has no initializer. */ 5070 user_init = get_union_initializer (c->ts.u.derived, &map); 5071 if (!user_init && !generate) 5072 return NULL; 5073 5074 /* Otherwise use a structure constructor. */ 5075 init = gfc_get_structure_constructor_expr (c->ts.type, c->ts.kind, 5076 &c->loc); 5077 init->ts = c->ts; 5078 5079 /* If we are to generate an initializer for the union, add a constructor 5080 which initializes the whole union first. */ 5081 if (generate) 5082 { 5083 ctor = gfc_constructor_get (); 5084 ctor->expr = generate_union_initializer (c); 5085 gfc_constructor_append (&init->value.constructor, ctor); 5086 } 5087 5088 /* If we found an initializer in one of our maps, apply it. Note this 5089 is applied _after_ the entire-union initializer above if any. */ 5090 if (user_init) 5091 { 5092 ctor = gfc_constructor_get (); 5093 ctor->expr = user_init; 5094 ctor->n.component = map; 5095 gfc_constructor_append (&init->value.constructor, ctor); 5096 } 5097 } 5098 5099 /* Treat simple components like locals. */ 5100 else 5101 { 5102 /* We MUST give an initializer, so force generation. */ 5103 init = gfc_build_init_expr (&c->ts, &c->loc, true); 5104 gfc_apply_init (&c->ts, &c->attr, init); 5105 } 5106 5107 return init; 5108 } 5109 5110 5111 /* Get an expression for a default initializer of a derived type. */ 5112 5113 gfc_expr * 5114 gfc_default_initializer (gfc_typespec *ts) 5115 { 5116 return gfc_generate_initializer (ts, false); 5117 } 5118 5119 /* Generate an initializer expression for an iso_c_binding type 5120 such as c_[fun]ptr. The appropriate initializer is c_null_[fun]ptr. */ 5121 5122 static gfc_expr * 5123 generate_isocbinding_initializer (gfc_symbol *derived) 5124 { 5125 /* The initializers have already been built into the c_null_[fun]ptr symbols 5126 from gen_special_c_interop_ptr. */ 5127 gfc_symtree *npsym = NULL; 5128 if (0 == strcmp (derived->name, "c_ptr")) 5129 gfc_find_sym_tree ("c_null_ptr", gfc_current_ns, true, &npsym); 5130 else if (0 == strcmp (derived->name, "c_funptr")) 5131 gfc_find_sym_tree ("c_null_funptr", gfc_current_ns, true, &npsym); 5132 else 5133 gfc_internal_error ("generate_isocbinding_initializer(): bad iso_c_binding" 5134 " type, expected %<c_ptr%> or %<c_funptr%>"); 5135 if (npsym) 5136 { 5137 gfc_expr *init = gfc_copy_expr (npsym->n.sym->value); 5138 init->symtree = npsym; 5139 init->ts.is_iso_c = true; 5140 return init; 5141 } 5142 5143 return NULL; 5144 } 5145 5146 /* Get or generate an expression for a default initializer of a derived type. 5147 If -finit-derived is specified, generate default initialization expressions 5148 for components that lack them when generate is set. */ 5149 5150 gfc_expr * 5151 gfc_generate_initializer (gfc_typespec *ts, bool generate) 5152 { 5153 gfc_expr *init, *tmp; 5154 gfc_component *comp; 5155 5156 generate = flag_init_derived && generate; 5157 5158 if (ts->u.derived->ts.is_iso_c && generate) 5159 return generate_isocbinding_initializer (ts->u.derived); 5160 5161 /* See if we have a default initializer in this, but not in nested 5162 types (otherwise we could use gfc_has_default_initializer()). 5163 We don't need to check if we are going to generate them. */ 5164 comp = ts->u.derived->components; 5165 if (!generate) 5166 { 5167 for (; comp; comp = comp->next) 5168 if (comp->initializer || comp_allocatable (comp)) 5169 break; 5170 } 5171 5172 if (!comp) 5173 return NULL; 5174 5175 init = gfc_get_structure_constructor_expr (ts->type, ts->kind, 5176 &ts->u.derived->declared_at); 5177 init->ts = *ts; 5178 5179 for (comp = ts->u.derived->components; comp; comp = comp->next) 5180 { 5181 gfc_constructor *ctor = gfc_constructor_get(); 5182 5183 /* Fetch or generate an initializer for the component. */ 5184 tmp = component_initializer (comp, generate); 5185 if (tmp) 5186 { 5187 /* Save the component ref for STRUCTUREs and UNIONs. */ 5188 if (ts->u.derived->attr.flavor == FL_STRUCT 5189 || ts->u.derived->attr.flavor == FL_UNION) 5190 ctor->n.component = comp; 5191 5192 /* If the initializer was not generated, we need a copy. */ 5193 ctor->expr = comp->initializer ? gfc_copy_expr (tmp) : tmp; 5194 if ((comp->ts.type != tmp->ts.type || comp->ts.kind != tmp->ts.kind) 5195 && !comp->attr.pointer && !comp->attr.proc_pointer) 5196 { 5197 bool val; 5198 val = gfc_convert_type_warn (ctor->expr, &comp->ts, 1, false); 5199 if (val == false) 5200 return NULL; 5201 } 5202 } 5203 5204 gfc_constructor_append (&init->value.constructor, ctor); 5205 } 5206 5207 return init; 5208 } 5209 5210 5211 /* Given a symbol, create an expression node with that symbol as a 5212 variable. If the symbol is array valued, setup a reference of the 5213 whole array. */ 5214 5215 gfc_expr * 5216 gfc_get_variable_expr (gfc_symtree *var) 5217 { 5218 gfc_expr *e; 5219 5220 e = gfc_get_expr (); 5221 e->expr_type = EXPR_VARIABLE; 5222 e->symtree = var; 5223 e->ts = var->n.sym->ts; 5224 5225 if (var->n.sym->attr.flavor != FL_PROCEDURE 5226 && ((var->n.sym->as != NULL && var->n.sym->ts.type != BT_CLASS) 5227 || (var->n.sym->ts.type == BT_CLASS && var->n.sym->ts.u.derived 5228 && CLASS_DATA (var->n.sym) 5229 && CLASS_DATA (var->n.sym)->as))) 5230 { 5231 e->rank = var->n.sym->ts.type == BT_CLASS 5232 ? CLASS_DATA (var->n.sym)->as->rank : var->n.sym->as->rank; 5233 e->ref = gfc_get_ref (); 5234 e->ref->type = REF_ARRAY; 5235 e->ref->u.ar.type = AR_FULL; 5236 e->ref->u.ar.as = gfc_copy_array_spec (var->n.sym->ts.type == BT_CLASS 5237 ? CLASS_DATA (var->n.sym)->as 5238 : var->n.sym->as); 5239 } 5240 5241 return e; 5242 } 5243 5244 5245 /* Adds a full array reference to an expression, as needed. */ 5246 5247 void 5248 gfc_add_full_array_ref (gfc_expr *e, gfc_array_spec *as) 5249 { 5250 gfc_ref *ref; 5251 for (ref = e->ref; ref; ref = ref->next) 5252 if (!ref->next) 5253 break; 5254 if (ref) 5255 { 5256 ref->next = gfc_get_ref (); 5257 ref = ref->next; 5258 } 5259 else 5260 { 5261 e->ref = gfc_get_ref (); 5262 ref = e->ref; 5263 } 5264 ref->type = REF_ARRAY; 5265 ref->u.ar.type = AR_FULL; 5266 ref->u.ar.dimen = e->rank; 5267 ref->u.ar.where = e->where; 5268 ref->u.ar.as = as; 5269 } 5270 5271 5272 gfc_expr * 5273 gfc_lval_expr_from_sym (gfc_symbol *sym) 5274 { 5275 gfc_expr *lval; 5276 gfc_array_spec *as; 5277 lval = gfc_get_expr (); 5278 lval->expr_type = EXPR_VARIABLE; 5279 lval->where = sym->declared_at; 5280 lval->ts = sym->ts; 5281 lval->symtree = gfc_find_symtree (sym->ns->sym_root, sym->name); 5282 5283 /* It will always be a full array. */ 5284 as = IS_CLASS_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as; 5285 lval->rank = as ? as->rank : 0; 5286 if (lval->rank) 5287 gfc_add_full_array_ref (lval, as); 5288 return lval; 5289 } 5290 5291 5292 /* Returns the array_spec of a full array expression. A NULL is 5293 returned otherwise. */ 5294 gfc_array_spec * 5295 gfc_get_full_arrayspec_from_expr (gfc_expr *expr) 5296 { 5297 gfc_array_spec *as; 5298 gfc_ref *ref; 5299 5300 if (expr->rank == 0) 5301 return NULL; 5302 5303 /* Follow any component references. */ 5304 if (expr->expr_type == EXPR_VARIABLE 5305 || expr->expr_type == EXPR_CONSTANT) 5306 { 5307 if (expr->symtree) 5308 as = expr->symtree->n.sym->as; 5309 else 5310 as = NULL; 5311 5312 for (ref = expr->ref; ref; ref = ref->next) 5313 { 5314 switch (ref->type) 5315 { 5316 case REF_COMPONENT: 5317 as = ref->u.c.component->as; 5318 continue; 5319 5320 case REF_SUBSTRING: 5321 case REF_INQUIRY: 5322 continue; 5323 5324 case REF_ARRAY: 5325 { 5326 switch (ref->u.ar.type) 5327 { 5328 case AR_ELEMENT: 5329 case AR_SECTION: 5330 case AR_UNKNOWN: 5331 as = NULL; 5332 continue; 5333 5334 case AR_FULL: 5335 break; 5336 } 5337 break; 5338 } 5339 } 5340 } 5341 } 5342 else 5343 as = NULL; 5344 5345 return as; 5346 } 5347 5348 5349 /* General expression traversal function. */ 5350 5351 bool 5352 gfc_traverse_expr (gfc_expr *expr, gfc_symbol *sym, 5353 bool (*func)(gfc_expr *, gfc_symbol *, int*), 5354 int f) 5355 { 5356 gfc_array_ref ar; 5357 gfc_ref *ref; 5358 gfc_actual_arglist *args; 5359 gfc_constructor *c; 5360 int i; 5361 5362 if (!expr) 5363 return false; 5364 5365 if ((*func) (expr, sym, &f)) 5366 return true; 5367 5368 if (expr->ts.type == BT_CHARACTER 5369 && expr->ts.u.cl 5370 && expr->ts.u.cl->length 5371 && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT 5372 && gfc_traverse_expr (expr->ts.u.cl->length, sym, func, f)) 5373 return true; 5374 5375 switch (expr->expr_type) 5376 { 5377 case EXPR_PPC: 5378 case EXPR_COMPCALL: 5379 case EXPR_FUNCTION: 5380 for (args = expr->value.function.actual; args; args = args->next) 5381 { 5382 if (gfc_traverse_expr (args->expr, sym, func, f)) 5383 return true; 5384 } 5385 break; 5386 5387 case EXPR_VARIABLE: 5388 case EXPR_CONSTANT: 5389 case EXPR_NULL: 5390 case EXPR_SUBSTRING: 5391 break; 5392 5393 case EXPR_STRUCTURE: 5394 case EXPR_ARRAY: 5395 for (c = gfc_constructor_first (expr->value.constructor); 5396 c; c = gfc_constructor_next (c)) 5397 { 5398 if (gfc_traverse_expr (c->expr, sym, func, f)) 5399 return true; 5400 if (c->iterator) 5401 { 5402 if (gfc_traverse_expr (c->iterator->var, sym, func, f)) 5403 return true; 5404 if (gfc_traverse_expr (c->iterator->start, sym, func, f)) 5405 return true; 5406 if (gfc_traverse_expr (c->iterator->end, sym, func, f)) 5407 return true; 5408 if (gfc_traverse_expr (c->iterator->step, sym, func, f)) 5409 return true; 5410 } 5411 } 5412 break; 5413 5414 case EXPR_OP: 5415 if (gfc_traverse_expr (expr->value.op.op1, sym, func, f)) 5416 return true; 5417 if (gfc_traverse_expr (expr->value.op.op2, sym, func, f)) 5418 return true; 5419 break; 5420 5421 default: 5422 gcc_unreachable (); 5423 break; 5424 } 5425 5426 ref = expr->ref; 5427 while (ref != NULL) 5428 { 5429 switch (ref->type) 5430 { 5431 case REF_ARRAY: 5432 ar = ref->u.ar; 5433 for (i = 0; i < GFC_MAX_DIMENSIONS; i++) 5434 { 5435 if (gfc_traverse_expr (ar.start[i], sym, func, f)) 5436 return true; 5437 if (gfc_traverse_expr (ar.end[i], sym, func, f)) 5438 return true; 5439 if (gfc_traverse_expr (ar.stride[i], sym, func, f)) 5440 return true; 5441 } 5442 break; 5443 5444 case REF_SUBSTRING: 5445 if (gfc_traverse_expr (ref->u.ss.start, sym, func, f)) 5446 return true; 5447 if (gfc_traverse_expr (ref->u.ss.end, sym, func, f)) 5448 return true; 5449 break; 5450 5451 case REF_COMPONENT: 5452 if (ref->u.c.component->ts.type == BT_CHARACTER 5453 && ref->u.c.component->ts.u.cl 5454 && ref->u.c.component->ts.u.cl->length 5455 && ref->u.c.component->ts.u.cl->length->expr_type 5456 != EXPR_CONSTANT 5457 && gfc_traverse_expr (ref->u.c.component->ts.u.cl->length, 5458 sym, func, f)) 5459 return true; 5460 5461 if (ref->u.c.component->as) 5462 for (i = 0; i < ref->u.c.component->as->rank 5463 + ref->u.c.component->as->corank; i++) 5464 { 5465 if (gfc_traverse_expr (ref->u.c.component->as->lower[i], 5466 sym, func, f)) 5467 return true; 5468 if (gfc_traverse_expr (ref->u.c.component->as->upper[i], 5469 sym, func, f)) 5470 return true; 5471 } 5472 break; 5473 5474 case REF_INQUIRY: 5475 return true; 5476 5477 default: 5478 gcc_unreachable (); 5479 } 5480 ref = ref->next; 5481 } 5482 return false; 5483 } 5484 5485 /* Traverse expr, marking all EXPR_VARIABLE symbols referenced. */ 5486 5487 static bool 5488 expr_set_symbols_referenced (gfc_expr *expr, 5489 gfc_symbol *sym ATTRIBUTE_UNUSED, 5490 int *f ATTRIBUTE_UNUSED) 5491 { 5492 if (expr->expr_type != EXPR_VARIABLE) 5493 return false; 5494 gfc_set_sym_referenced (expr->symtree->n.sym); 5495 return false; 5496 } 5497 5498 void 5499 gfc_expr_set_symbols_referenced (gfc_expr *expr) 5500 { 5501 gfc_traverse_expr (expr, NULL, expr_set_symbols_referenced, 0); 5502 } 5503 5504 5505 /* Determine if an expression is a procedure pointer component and return 5506 the component in that case. Otherwise return NULL. */ 5507 5508 gfc_component * 5509 gfc_get_proc_ptr_comp (gfc_expr *expr) 5510 { 5511 gfc_ref *ref; 5512 5513 if (!expr || !expr->ref) 5514 return NULL; 5515 5516 ref = expr->ref; 5517 while (ref->next) 5518 ref = ref->next; 5519 5520 if (ref->type == REF_COMPONENT 5521 && ref->u.c.component->attr.proc_pointer) 5522 return ref->u.c.component; 5523 5524 return NULL; 5525 } 5526 5527 5528 /* Determine if an expression is a procedure pointer component. */ 5529 5530 bool 5531 gfc_is_proc_ptr_comp (gfc_expr *expr) 5532 { 5533 return (gfc_get_proc_ptr_comp (expr) != NULL); 5534 } 5535 5536 5537 /* Determine if an expression is a function with an allocatable class scalar 5538 result. */ 5539 bool 5540 gfc_is_alloc_class_scalar_function (gfc_expr *expr) 5541 { 5542 if (expr->expr_type == EXPR_FUNCTION 5543 && expr->value.function.esym 5544 && expr->value.function.esym->result 5545 && expr->value.function.esym->result->ts.type == BT_CLASS 5546 && !CLASS_DATA (expr->value.function.esym->result)->attr.dimension 5547 && CLASS_DATA (expr->value.function.esym->result)->attr.allocatable) 5548 return true; 5549 5550 return false; 5551 } 5552 5553 5554 /* Determine if an expression is a function with an allocatable class array 5555 result. */ 5556 bool 5557 gfc_is_class_array_function (gfc_expr *expr) 5558 { 5559 if (expr->expr_type == EXPR_FUNCTION 5560 && expr->value.function.esym 5561 && expr->value.function.esym->result 5562 && expr->value.function.esym->result->ts.type == BT_CLASS 5563 && CLASS_DATA (expr->value.function.esym->result)->attr.dimension 5564 && (CLASS_DATA (expr->value.function.esym->result)->attr.allocatable 5565 || CLASS_DATA (expr->value.function.esym->result)->attr.pointer)) 5566 return true; 5567 5568 return false; 5569 } 5570 5571 5572 /* Walk an expression tree and check each variable encountered for being typed. 5573 If strict is not set, a top-level variable is tolerated untyped in -std=gnu 5574 mode as is a basic arithmetic expression using those; this is for things in 5575 legacy-code like: 5576 5577 INTEGER :: arr(n), n 5578 INTEGER :: arr(n + 1), n 5579 5580 The namespace is needed for IMPLICIT typing. */ 5581 5582 static gfc_namespace* check_typed_ns; 5583 5584 static bool 5585 expr_check_typed_help (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED, 5586 int* f ATTRIBUTE_UNUSED) 5587 { 5588 bool t; 5589 5590 if (e->expr_type != EXPR_VARIABLE) 5591 return false; 5592 5593 gcc_assert (e->symtree); 5594 t = gfc_check_symbol_typed (e->symtree->n.sym, check_typed_ns, 5595 true, e->where); 5596 5597 return (!t); 5598 } 5599 5600 bool 5601 gfc_expr_check_typed (gfc_expr* e, gfc_namespace* ns, bool strict) 5602 { 5603 bool error_found; 5604 5605 /* If this is a top-level variable or EXPR_OP, do the check with strict given 5606 to us. */ 5607 if (!strict) 5608 { 5609 if (e->expr_type == EXPR_VARIABLE && !e->ref) 5610 return gfc_check_symbol_typed (e->symtree->n.sym, ns, strict, e->where); 5611 5612 if (e->expr_type == EXPR_OP) 5613 { 5614 bool t = true; 5615 5616 gcc_assert (e->value.op.op1); 5617 t = gfc_expr_check_typed (e->value.op.op1, ns, strict); 5618 5619 if (t && e->value.op.op2) 5620 t = gfc_expr_check_typed (e->value.op.op2, ns, strict); 5621 5622 return t; 5623 } 5624 } 5625 5626 /* Otherwise, walk the expression and do it strictly. */ 5627 check_typed_ns = ns; 5628 error_found = gfc_traverse_expr (e, NULL, &expr_check_typed_help, 0); 5629 5630 return error_found ? false : true; 5631 } 5632 5633 5634 /* This function returns true if it contains any references to PDT KIND 5635 or LEN parameters. */ 5636 5637 static bool 5638 derived_parameter_expr (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED, 5639 int* f ATTRIBUTE_UNUSED) 5640 { 5641 if (e->expr_type != EXPR_VARIABLE) 5642 return false; 5643 5644 gcc_assert (e->symtree); 5645 if (e->symtree->n.sym->attr.pdt_kind 5646 || e->symtree->n.sym->attr.pdt_len) 5647 return true; 5648 5649 return false; 5650 } 5651 5652 5653 bool 5654 gfc_derived_parameter_expr (gfc_expr *e) 5655 { 5656 return gfc_traverse_expr (e, NULL, &derived_parameter_expr, 0); 5657 } 5658 5659 5660 /* This function returns the overall type of a type parameter spec list. 5661 If all the specs are explicit, SPEC_EXPLICIT is returned. If any of the 5662 parameters are assumed/deferred then SPEC_ASSUMED/DEFERRED is returned 5663 unless derived is not NULL. In this latter case, all the LEN parameters 5664 must be either assumed or deferred for the return argument to be set to 5665 anything other than SPEC_EXPLICIT. */ 5666 5667 gfc_param_spec_type 5668 gfc_spec_list_type (gfc_actual_arglist *param_list, gfc_symbol *derived) 5669 { 5670 gfc_param_spec_type res = SPEC_EXPLICIT; 5671 gfc_component *c; 5672 bool seen_assumed = false; 5673 bool seen_deferred = false; 5674 5675 if (derived == NULL) 5676 { 5677 for (; param_list; param_list = param_list->next) 5678 if (param_list->spec_type == SPEC_ASSUMED 5679 || param_list->spec_type == SPEC_DEFERRED) 5680 return param_list->spec_type; 5681 } 5682 else 5683 { 5684 for (; param_list; param_list = param_list->next) 5685 { 5686 c = gfc_find_component (derived, param_list->name, 5687 true, true, NULL); 5688 gcc_assert (c != NULL); 5689 if (c->attr.pdt_kind) 5690 continue; 5691 else if (param_list->spec_type == SPEC_EXPLICIT) 5692 return SPEC_EXPLICIT; 5693 seen_assumed = param_list->spec_type == SPEC_ASSUMED; 5694 seen_deferred = param_list->spec_type == SPEC_DEFERRED; 5695 if (seen_assumed && seen_deferred) 5696 return SPEC_EXPLICIT; 5697 } 5698 res = seen_assumed ? SPEC_ASSUMED : SPEC_DEFERRED; 5699 } 5700 return res; 5701 } 5702 5703 5704 bool 5705 gfc_ref_this_image (gfc_ref *ref) 5706 { 5707 int n; 5708 5709 gcc_assert (ref->type == REF_ARRAY && ref->u.ar.codimen > 0); 5710 5711 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++) 5712 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE) 5713 return false; 5714 5715 return true; 5716 } 5717 5718 gfc_expr * 5719 gfc_find_team_co (gfc_expr *e) 5720 { 5721 gfc_ref *ref; 5722 5723 for (ref = e->ref; ref; ref = ref->next) 5724 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0) 5725 return ref->u.ar.team; 5726 5727 if (e->value.function.actual->expr) 5728 for (ref = e->value.function.actual->expr->ref; ref; 5729 ref = ref->next) 5730 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0) 5731 return ref->u.ar.team; 5732 5733 return NULL; 5734 } 5735 5736 gfc_expr * 5737 gfc_find_stat_co (gfc_expr *e) 5738 { 5739 gfc_ref *ref; 5740 5741 for (ref = e->ref; ref; ref = ref->next) 5742 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0) 5743 return ref->u.ar.stat; 5744 5745 if (e->value.function.actual->expr) 5746 for (ref = e->value.function.actual->expr->ref; ref; 5747 ref = ref->next) 5748 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0) 5749 return ref->u.ar.stat; 5750 5751 return NULL; 5752 } 5753 5754 bool 5755 gfc_is_coindexed (gfc_expr *e) 5756 { 5757 gfc_ref *ref; 5758 5759 for (ref = e->ref; ref; ref = ref->next) 5760 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0) 5761 return !gfc_ref_this_image (ref); 5762 5763 return false; 5764 } 5765 5766 5767 /* Coarrays are variables with a corank but not being coindexed. However, also 5768 the following is a coarray: A subobject of a coarray is a coarray if it does 5769 not have any cosubscripts, vector subscripts, allocatable component 5770 selection, or pointer component selection. (F2008, 2.4.7) */ 5771 5772 bool 5773 gfc_is_coarray (gfc_expr *e) 5774 { 5775 gfc_ref *ref; 5776 gfc_symbol *sym; 5777 gfc_component *comp; 5778 bool coindexed; 5779 bool coarray; 5780 int i; 5781 5782 if (e->expr_type != EXPR_VARIABLE) 5783 return false; 5784 5785 coindexed = false; 5786 sym = e->symtree->n.sym; 5787 5788 if (sym->ts.type == BT_CLASS && sym->attr.class_ok) 5789 coarray = CLASS_DATA (sym)->attr.codimension; 5790 else 5791 coarray = sym->attr.codimension; 5792 5793 for (ref = e->ref; ref; ref = ref->next) 5794 switch (ref->type) 5795 { 5796 case REF_COMPONENT: 5797 comp = ref->u.c.component; 5798 if (comp->ts.type == BT_CLASS && comp->attr.class_ok 5799 && (CLASS_DATA (comp)->attr.class_pointer 5800 || CLASS_DATA (comp)->attr.allocatable)) 5801 { 5802 coindexed = false; 5803 coarray = CLASS_DATA (comp)->attr.codimension; 5804 } 5805 else if (comp->attr.pointer || comp->attr.allocatable) 5806 { 5807 coindexed = false; 5808 coarray = comp->attr.codimension; 5809 } 5810 break; 5811 5812 case REF_ARRAY: 5813 if (!coarray) 5814 break; 5815 5816 if (ref->u.ar.codimen > 0 && !gfc_ref_this_image (ref)) 5817 { 5818 coindexed = true; 5819 break; 5820 } 5821 5822 for (i = 0; i < ref->u.ar.dimen; i++) 5823 if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR) 5824 { 5825 coarray = false; 5826 break; 5827 } 5828 break; 5829 5830 case REF_SUBSTRING: 5831 case REF_INQUIRY: 5832 break; 5833 } 5834 5835 return coarray && !coindexed; 5836 } 5837 5838 5839 int 5840 gfc_get_corank (gfc_expr *e) 5841 { 5842 int corank; 5843 gfc_ref *ref; 5844 5845 if (!gfc_is_coarray (e)) 5846 return 0; 5847 5848 if (e->ts.type == BT_CLASS && e->ts.u.derived->components) 5849 corank = e->ts.u.derived->components->as 5850 ? e->ts.u.derived->components->as->corank : 0; 5851 else 5852 corank = e->symtree->n.sym->as ? e->symtree->n.sym->as->corank : 0; 5853 5854 for (ref = e->ref; ref; ref = ref->next) 5855 { 5856 if (ref->type == REF_ARRAY) 5857 corank = ref->u.ar.as->corank; 5858 gcc_assert (ref->type != REF_SUBSTRING); 5859 } 5860 5861 return corank; 5862 } 5863 5864 5865 /* Check whether the expression has an ultimate allocatable component. 5866 Being itself allocatable does not count. */ 5867 bool 5868 gfc_has_ultimate_allocatable (gfc_expr *e) 5869 { 5870 gfc_ref *ref, *last = NULL; 5871 5872 if (e->expr_type != EXPR_VARIABLE) 5873 return false; 5874 5875 for (ref = e->ref; ref; ref = ref->next) 5876 if (ref->type == REF_COMPONENT) 5877 last = ref; 5878 5879 if (last && last->u.c.component->ts.type == BT_CLASS) 5880 return CLASS_DATA (last->u.c.component)->attr.alloc_comp; 5881 else if (last && last->u.c.component->ts.type == BT_DERIVED) 5882 return last->u.c.component->ts.u.derived->attr.alloc_comp; 5883 else if (last) 5884 return false; 5885 5886 if (e->ts.type == BT_CLASS) 5887 return CLASS_DATA (e)->attr.alloc_comp; 5888 else if (e->ts.type == BT_DERIVED) 5889 return e->ts.u.derived->attr.alloc_comp; 5890 else 5891 return false; 5892 } 5893 5894 5895 /* Check whether the expression has an pointer component. 5896 Being itself a pointer does not count. */ 5897 bool 5898 gfc_has_ultimate_pointer (gfc_expr *e) 5899 { 5900 gfc_ref *ref, *last = NULL; 5901 5902 if (e->expr_type != EXPR_VARIABLE) 5903 return false; 5904 5905 for (ref = e->ref; ref; ref = ref->next) 5906 if (ref->type == REF_COMPONENT) 5907 last = ref; 5908 5909 if (last && last->u.c.component->ts.type == BT_CLASS) 5910 return CLASS_DATA (last->u.c.component)->attr.pointer_comp; 5911 else if (last && last->u.c.component->ts.type == BT_DERIVED) 5912 return last->u.c.component->ts.u.derived->attr.pointer_comp; 5913 else if (last) 5914 return false; 5915 5916 if (e->ts.type == BT_CLASS) 5917 return CLASS_DATA (e)->attr.pointer_comp; 5918 else if (e->ts.type == BT_DERIVED) 5919 return e->ts.u.derived->attr.pointer_comp; 5920 else 5921 return false; 5922 } 5923 5924 5925 /* Check whether an expression is "simply contiguous", cf. F2008, 6.5.4. 5926 Note: A scalar is not regarded as "simply contiguous" by the standard. 5927 if bool is not strict, some further checks are done - for instance, 5928 a "(::1)" is accepted. */ 5929 5930 bool 5931 gfc_is_simply_contiguous (gfc_expr *expr, bool strict, bool permit_element) 5932 { 5933 bool colon; 5934 int i; 5935 gfc_array_ref *ar = NULL; 5936 gfc_ref *ref, *part_ref = NULL; 5937 gfc_symbol *sym; 5938 5939 if (expr->expr_type == EXPR_ARRAY) 5940 return true; 5941 5942 if (expr->expr_type == EXPR_FUNCTION) 5943 { 5944 if (expr->value.function.isym) 5945 /* TRANSPOSE is the only intrinsic that may return a 5946 non-contiguous array. It's treated as a special case in 5947 gfc_conv_expr_descriptor too. */ 5948 return (expr->value.function.isym->id != GFC_ISYM_TRANSPOSE); 5949 else if (expr->value.function.esym) 5950 /* Only a pointer to an array without the contiguous attribute 5951 can be non-contiguous as a result value. */ 5952 return (expr->value.function.esym->result->attr.contiguous 5953 || !expr->value.function.esym->result->attr.pointer); 5954 else 5955 { 5956 /* Type-bound procedures. */ 5957 gfc_symbol *s = expr->symtree->n.sym; 5958 if (s->ts.type != BT_CLASS && s->ts.type != BT_DERIVED) 5959 return false; 5960 5961 gfc_ref *rc = NULL; 5962 for (gfc_ref *r = expr->ref; r; r = r->next) 5963 if (r->type == REF_COMPONENT) 5964 rc = r; 5965 5966 if (rc == NULL || rc->u.c.component == NULL 5967 || rc->u.c.component->ts.interface == NULL) 5968 return false; 5969 5970 return rc->u.c.component->ts.interface->attr.contiguous; 5971 } 5972 } 5973 else if (expr->expr_type != EXPR_VARIABLE) 5974 return false; 5975 5976 if (!permit_element && expr->rank == 0) 5977 return false; 5978 5979 for (ref = expr->ref; ref; ref = ref->next) 5980 { 5981 if (ar) 5982 return false; /* Array shall be last part-ref. */ 5983 5984 if (ref->type == REF_COMPONENT) 5985 part_ref = ref; 5986 else if (ref->type == REF_SUBSTRING) 5987 return false; 5988 else if (ref->type == REF_INQUIRY) 5989 return false; 5990 else if (ref->u.ar.type != AR_ELEMENT) 5991 ar = &ref->u.ar; 5992 } 5993 5994 sym = expr->symtree->n.sym; 5995 if (expr->ts.type != BT_CLASS 5996 && ((part_ref 5997 && !part_ref->u.c.component->attr.contiguous 5998 && part_ref->u.c.component->attr.pointer) 5999 || (!part_ref 6000 && !sym->attr.contiguous 6001 && (sym->attr.pointer 6002 || (sym->as && sym->as->type == AS_ASSUMED_RANK) 6003 || (sym->as && sym->as->type == AS_ASSUMED_SHAPE))))) 6004 return false; 6005 6006 if (!ar || ar->type == AR_FULL) 6007 return true; 6008 6009 gcc_assert (ar->type == AR_SECTION); 6010 6011 /* Check for simply contiguous array */ 6012 colon = true; 6013 for (i = 0; i < ar->dimen; i++) 6014 { 6015 if (ar->dimen_type[i] == DIMEN_VECTOR) 6016 return false; 6017 6018 if (ar->dimen_type[i] == DIMEN_ELEMENT) 6019 { 6020 colon = false; 6021 continue; 6022 } 6023 6024 gcc_assert (ar->dimen_type[i] == DIMEN_RANGE); 6025 6026 6027 /* If the previous section was not contiguous, that's an error, 6028 unless we have effective only one element and checking is not 6029 strict. */ 6030 if (!colon && (strict || !ar->start[i] || !ar->end[i] 6031 || ar->start[i]->expr_type != EXPR_CONSTANT 6032 || ar->end[i]->expr_type != EXPR_CONSTANT 6033 || mpz_cmp (ar->start[i]->value.integer, 6034 ar->end[i]->value.integer) != 0)) 6035 return false; 6036 6037 /* Following the standard, "(::1)" or - if known at compile time - 6038 "(lbound:ubound)" are not simply contiguous; if strict 6039 is false, they are regarded as simply contiguous. */ 6040 if (ar->stride[i] && (strict || ar->stride[i]->expr_type != EXPR_CONSTANT 6041 || ar->stride[i]->ts.type != BT_INTEGER 6042 || mpz_cmp_si (ar->stride[i]->value.integer, 1) != 0)) 6043 return false; 6044 6045 if (ar->start[i] 6046 && (strict || ar->start[i]->expr_type != EXPR_CONSTANT 6047 || !ar->as->lower[i] 6048 || ar->as->lower[i]->expr_type != EXPR_CONSTANT 6049 || mpz_cmp (ar->start[i]->value.integer, 6050 ar->as->lower[i]->value.integer) != 0)) 6051 colon = false; 6052 6053 if (ar->end[i] 6054 && (strict || ar->end[i]->expr_type != EXPR_CONSTANT 6055 || !ar->as->upper[i] 6056 || ar->as->upper[i]->expr_type != EXPR_CONSTANT 6057 || mpz_cmp (ar->end[i]->value.integer, 6058 ar->as->upper[i]->value.integer) != 0)) 6059 colon = false; 6060 } 6061 6062 return true; 6063 } 6064 6065 /* Return true if the expression is guaranteed to be non-contiguous, 6066 false if we cannot prove anything. It is probably best to call 6067 this after gfc_is_simply_contiguous. If neither of them returns 6068 true, we cannot say (at compile-time). */ 6069 6070 bool 6071 gfc_is_not_contiguous (gfc_expr *array) 6072 { 6073 int i; 6074 gfc_array_ref *ar = NULL; 6075 gfc_ref *ref; 6076 bool previous_incomplete; 6077 6078 for (ref = array->ref; ref; ref = ref->next) 6079 { 6080 /* Array-ref shall be last ref. */ 6081 6082 if (ar && ar->type != AR_ELEMENT) 6083 return true; 6084 6085 if (ref->type == REF_ARRAY) 6086 ar = &ref->u.ar; 6087 } 6088 6089 if (ar == NULL || ar->type != AR_SECTION) 6090 return false; 6091 6092 previous_incomplete = false; 6093 6094 /* Check if we can prove that the array is not contiguous. */ 6095 6096 for (i = 0; i < ar->dimen; i++) 6097 { 6098 mpz_t arr_size, ref_size; 6099 6100 if (gfc_ref_dimen_size (ar, i, &ref_size, NULL)) 6101 { 6102 if (gfc_dep_difference (ar->as->upper[i], ar->as->lower[i], &arr_size)) 6103 { 6104 /* a(2:4,2:) is known to be non-contiguous, but 6105 a(2:4,i:i) can be contiguous. */ 6106 mpz_add_ui (arr_size, arr_size, 1L); 6107 if (previous_incomplete && mpz_cmp_si (ref_size, 1) != 0) 6108 { 6109 mpz_clear (arr_size); 6110 mpz_clear (ref_size); 6111 return true; 6112 } 6113 else if (mpz_cmp (arr_size, ref_size) != 0) 6114 previous_incomplete = true; 6115 6116 mpz_clear (arr_size); 6117 } 6118 6119 /* Check for a(::2), i.e. where the stride is not unity. 6120 This is only done if there is more than one element in 6121 the reference along this dimension. */ 6122 6123 if (mpz_cmp_ui (ref_size, 1) > 0 && ar->type == AR_SECTION 6124 && ar->dimen_type[i] == DIMEN_RANGE 6125 && ar->stride[i] && ar->stride[i]->expr_type == EXPR_CONSTANT 6126 && mpz_cmp_si (ar->stride[i]->value.integer, 1) != 0) 6127 { 6128 mpz_clear (ref_size); 6129 return true; 6130 } 6131 6132 mpz_clear (ref_size); 6133 } 6134 } 6135 /* We didn't find anything definitive. */ 6136 return false; 6137 } 6138 6139 /* Build call to an intrinsic procedure. The number of arguments has to be 6140 passed (rather than ending the list with a NULL value) because we may 6141 want to add arguments but with a NULL-expression. */ 6142 6143 gfc_expr* 6144 gfc_build_intrinsic_call (gfc_namespace *ns, gfc_isym_id id, const char* name, 6145 locus where, unsigned numarg, ...) 6146 { 6147 gfc_expr* result; 6148 gfc_actual_arglist* atail; 6149 gfc_intrinsic_sym* isym; 6150 va_list ap; 6151 unsigned i; 6152 const char *mangled_name = gfc_get_string (GFC_PREFIX ("%s"), name); 6153 6154 isym = gfc_intrinsic_function_by_id (id); 6155 gcc_assert (isym); 6156 6157 result = gfc_get_expr (); 6158 result->expr_type = EXPR_FUNCTION; 6159 result->ts = isym->ts; 6160 result->where = where; 6161 result->value.function.name = mangled_name; 6162 result->value.function.isym = isym; 6163 6164 gfc_get_sym_tree (mangled_name, ns, &result->symtree, false); 6165 gfc_commit_symbol (result->symtree->n.sym); 6166 gcc_assert (result->symtree 6167 && (result->symtree->n.sym->attr.flavor == FL_PROCEDURE 6168 || result->symtree->n.sym->attr.flavor == FL_UNKNOWN)); 6169 result->symtree->n.sym->intmod_sym_id = id; 6170 result->symtree->n.sym->attr.flavor = FL_PROCEDURE; 6171 result->symtree->n.sym->attr.intrinsic = 1; 6172 result->symtree->n.sym->attr.artificial = 1; 6173 6174 va_start (ap, numarg); 6175 atail = NULL; 6176 for (i = 0; i < numarg; ++i) 6177 { 6178 if (atail) 6179 { 6180 atail->next = gfc_get_actual_arglist (); 6181 atail = atail->next; 6182 } 6183 else 6184 atail = result->value.function.actual = gfc_get_actual_arglist (); 6185 6186 atail->expr = va_arg (ap, gfc_expr*); 6187 } 6188 va_end (ap); 6189 6190 return result; 6191 } 6192 6193 6194 /* Check if an expression may appear in a variable definition context 6195 (F2008, 16.6.7) or pointer association context (F2008, 16.6.8). 6196 This is called from the various places when resolving 6197 the pieces that make up such a context. 6198 If own_scope is true (applies to, e.g., ac-implied-do/data-implied-do 6199 variables), some checks are not performed. 6200 6201 Optionally, a possible error message can be suppressed if context is NULL 6202 and just the return status (true / false) be requested. */ 6203 6204 bool 6205 gfc_check_vardef_context (gfc_expr* e, bool pointer, bool alloc_obj, 6206 bool own_scope, const char* context) 6207 { 6208 gfc_symbol* sym = NULL; 6209 bool is_pointer; 6210 bool check_intentin; 6211 bool ptr_component; 6212 symbol_attribute attr; 6213 gfc_ref* ref; 6214 int i; 6215 6216 if (e->expr_type == EXPR_VARIABLE) 6217 { 6218 gcc_assert (e->symtree); 6219 sym = e->symtree->n.sym; 6220 } 6221 else if (e->expr_type == EXPR_FUNCTION) 6222 { 6223 gcc_assert (e->symtree); 6224 sym = e->value.function.esym ? e->value.function.esym : e->symtree->n.sym; 6225 } 6226 6227 attr = gfc_expr_attr (e); 6228 if (!pointer && e->expr_type == EXPR_FUNCTION && attr.pointer) 6229 { 6230 if (!(gfc_option.allow_std & GFC_STD_F2008)) 6231 { 6232 if (context) 6233 gfc_error ("Fortran 2008: Pointer functions in variable definition" 6234 " context (%s) at %L", context, &e->where); 6235 return false; 6236 } 6237 } 6238 else if (e->expr_type != EXPR_VARIABLE) 6239 { 6240 if (context) 6241 gfc_error ("Non-variable expression in variable definition context (%s)" 6242 " at %L", context, &e->where); 6243 return false; 6244 } 6245 6246 if (!pointer && sym->attr.flavor == FL_PARAMETER) 6247 { 6248 if (context) 6249 gfc_error ("Named constant %qs in variable definition context (%s)" 6250 " at %L", sym->name, context, &e->where); 6251 return false; 6252 } 6253 if (!pointer && sym->attr.flavor != FL_VARIABLE 6254 && !(sym->attr.flavor == FL_PROCEDURE && sym == sym->result) 6255 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.proc_pointer) 6256 && !(sym->attr.flavor == FL_PROCEDURE 6257 && sym->attr.function && sym->attr.pointer)) 6258 { 6259 if (context) 6260 gfc_error ("%qs in variable definition context (%s) at %L is not" 6261 " a variable", sym->name, context, &e->where); 6262 return false; 6263 } 6264 6265 /* Find out whether the expr is a pointer; this also means following 6266 component references to the last one. */ 6267 is_pointer = (attr.pointer || attr.proc_pointer); 6268 if (pointer && !is_pointer) 6269 { 6270 if (context) 6271 gfc_error ("Non-POINTER in pointer association context (%s)" 6272 " at %L", context, &e->where); 6273 return false; 6274 } 6275 6276 if (e->ts.type == BT_DERIVED 6277 && e->ts.u.derived == NULL) 6278 { 6279 if (context) 6280 gfc_error ("Type inaccessible in variable definition context (%s) " 6281 "at %L", context, &e->where); 6282 return false; 6283 } 6284 6285 /* F2008, C1303. */ 6286 if (!alloc_obj 6287 && (attr.lock_comp 6288 || (e->ts.type == BT_DERIVED 6289 && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV 6290 && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE))) 6291 { 6292 if (context) 6293 gfc_error ("LOCK_TYPE in variable definition context (%s) at %L", 6294 context, &e->where); 6295 return false; 6296 } 6297 6298 /* TS18508, C702/C203. */ 6299 if (!alloc_obj 6300 && (attr.lock_comp 6301 || (e->ts.type == BT_DERIVED 6302 && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV 6303 && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE))) 6304 { 6305 if (context) 6306 gfc_error ("LOCK_EVENT in variable definition context (%s) at %L", 6307 context, &e->where); 6308 return false; 6309 } 6310 6311 /* INTENT(IN) dummy argument. Check this, unless the object itself is the 6312 component of sub-component of a pointer; we need to distinguish 6313 assignment to a pointer component from pointer-assignment to a pointer 6314 component. Note that (normal) assignment to procedure pointers is not 6315 possible. */ 6316 check_intentin = !own_scope; 6317 ptr_component = (sym->ts.type == BT_CLASS && sym->ts.u.derived 6318 && CLASS_DATA (sym)) 6319 ? CLASS_DATA (sym)->attr.class_pointer : sym->attr.pointer; 6320 for (ref = e->ref; ref && check_intentin; ref = ref->next) 6321 { 6322 if (ptr_component && ref->type == REF_COMPONENT) 6323 check_intentin = false; 6324 if (ref->type == REF_COMPONENT) 6325 { 6326 gfc_component *comp = ref->u.c.component; 6327 ptr_component = (comp->ts.type == BT_CLASS && comp->attr.class_ok) 6328 ? CLASS_DATA (comp)->attr.class_pointer 6329 : comp->attr.pointer; 6330 if (ptr_component && !pointer) 6331 check_intentin = false; 6332 } 6333 if (ref->type == REF_INQUIRY 6334 && (ref->u.i == INQUIRY_KIND || ref->u.i == INQUIRY_LEN)) 6335 { 6336 if (context) 6337 gfc_error ("%qs parameter inquiry for %qs in " 6338 "variable definition context (%s) at %L", 6339 ref->u.i == INQUIRY_KIND ? "KIND" : "LEN", 6340 sym->name, context, &e->where); 6341 return false; 6342 } 6343 } 6344 6345 if (check_intentin 6346 && (sym->attr.intent == INTENT_IN 6347 || (sym->attr.select_type_temporary && sym->assoc 6348 && sym->assoc->target && sym->assoc->target->symtree 6349 && sym->assoc->target->symtree->n.sym->attr.intent == INTENT_IN))) 6350 { 6351 if (pointer && is_pointer) 6352 { 6353 if (context) 6354 gfc_error ("Dummy argument %qs with INTENT(IN) in pointer" 6355 " association context (%s) at %L", 6356 sym->name, context, &e->where); 6357 return false; 6358 } 6359 if (!pointer && !is_pointer && !sym->attr.pointer) 6360 { 6361 const char *name = sym->attr.select_type_temporary 6362 ? sym->assoc->target->symtree->name : sym->name; 6363 if (context) 6364 gfc_error ("Dummy argument %qs with INTENT(IN) in variable" 6365 " definition context (%s) at %L", 6366 name, context, &e->where); 6367 return false; 6368 } 6369 } 6370 6371 /* PROTECTED and use-associated. */ 6372 if (sym->attr.is_protected && sym->attr.use_assoc && check_intentin) 6373 { 6374 if (pointer && is_pointer) 6375 { 6376 if (context) 6377 gfc_error ("Variable %qs is PROTECTED and cannot appear in a" 6378 " pointer association context (%s) at %L", 6379 sym->name, context, &e->where); 6380 return false; 6381 } 6382 if (!pointer && !is_pointer) 6383 { 6384 if (context) 6385 gfc_error ("Variable %qs is PROTECTED and cannot appear in a" 6386 " variable definition context (%s) at %L", 6387 sym->name, context, &e->where); 6388 return false; 6389 } 6390 } 6391 6392 /* Variable not assignable from a PURE procedure but appears in 6393 variable definition context. */ 6394 own_scope = own_scope 6395 || (sym->attr.result && sym->ns->proc_name 6396 && sym == sym->ns->proc_name->result); 6397 if (!pointer && !own_scope && gfc_pure (NULL) && gfc_impure_variable (sym)) 6398 { 6399 if (context) 6400 gfc_error ("Variable %qs cannot appear in a variable definition" 6401 " context (%s) at %L in PURE procedure", 6402 sym->name, context, &e->where); 6403 return false; 6404 } 6405 6406 if (!pointer && context && gfc_implicit_pure (NULL) 6407 && gfc_impure_variable (sym)) 6408 { 6409 gfc_namespace *ns; 6410 gfc_symbol *sym; 6411 6412 for (ns = gfc_current_ns; ns; ns = ns->parent) 6413 { 6414 sym = ns->proc_name; 6415 if (sym == NULL) 6416 break; 6417 if (sym->attr.flavor == FL_PROCEDURE) 6418 { 6419 sym->attr.implicit_pure = 0; 6420 break; 6421 } 6422 } 6423 } 6424 /* Check variable definition context for associate-names. */ 6425 if (!pointer && sym->assoc && !sym->attr.select_rank_temporary) 6426 { 6427 const char* name; 6428 gfc_association_list* assoc; 6429 6430 gcc_assert (sym->assoc->target); 6431 6432 /* If this is a SELECT TYPE temporary (the association is used internally 6433 for SELECT TYPE), silently go over to the target. */ 6434 if (sym->attr.select_type_temporary) 6435 { 6436 gfc_expr* t = sym->assoc->target; 6437 6438 gcc_assert (t->expr_type == EXPR_VARIABLE); 6439 name = t->symtree->name; 6440 6441 if (t->symtree->n.sym->assoc) 6442 assoc = t->symtree->n.sym->assoc; 6443 else 6444 assoc = sym->assoc; 6445 } 6446 else 6447 { 6448 name = sym->name; 6449 assoc = sym->assoc; 6450 } 6451 gcc_assert (name && assoc); 6452 6453 /* Is association to a valid variable? */ 6454 if (!assoc->variable) 6455 { 6456 if (context) 6457 { 6458 if (assoc->target->expr_type == EXPR_VARIABLE) 6459 gfc_error ("%qs at %L associated to vector-indexed target" 6460 " cannot be used in a variable definition" 6461 " context (%s)", 6462 name, &e->where, context); 6463 else 6464 gfc_error ("%qs at %L associated to expression" 6465 " cannot be used in a variable definition" 6466 " context (%s)", 6467 name, &e->where, context); 6468 } 6469 return false; 6470 } 6471 6472 /* Target must be allowed to appear in a variable definition context. */ 6473 if (!gfc_check_vardef_context (assoc->target, pointer, false, false, NULL)) 6474 { 6475 if (context) 6476 gfc_error ("Associate-name %qs cannot appear in a variable" 6477 " definition context (%s) at %L because its target" 6478 " at %L cannot, either", 6479 name, context, &e->where, 6480 &assoc->target->where); 6481 return false; 6482 } 6483 } 6484 6485 /* Check for same value in vector expression subscript. */ 6486 6487 if (e->rank > 0) 6488 for (ref = e->ref; ref != NULL; ref = ref->next) 6489 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION) 6490 for (i = 0; i < GFC_MAX_DIMENSIONS 6491 && ref->u.ar.dimen_type[i] != 0; i++) 6492 if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR) 6493 { 6494 gfc_expr *arr = ref->u.ar.start[i]; 6495 if (arr->expr_type == EXPR_ARRAY) 6496 { 6497 gfc_constructor *c, *n; 6498 gfc_expr *ec, *en; 6499 6500 for (c = gfc_constructor_first (arr->value.constructor); 6501 c != NULL; c = gfc_constructor_next (c)) 6502 { 6503 if (c == NULL || c->iterator != NULL) 6504 continue; 6505 6506 ec = c->expr; 6507 6508 for (n = gfc_constructor_next (c); n != NULL; 6509 n = gfc_constructor_next (n)) 6510 { 6511 if (n->iterator != NULL) 6512 continue; 6513 6514 en = n->expr; 6515 if (gfc_dep_compare_expr (ec, en) == 0) 6516 { 6517 if (context) 6518 gfc_error_now ("Elements with the same value " 6519 "at %L and %L in vector " 6520 "subscript in a variable " 6521 "definition context (%s)", 6522 &(ec->where), &(en->where), 6523 context); 6524 return false; 6525 } 6526 } 6527 } 6528 } 6529 } 6530 6531 return true; 6532 } 6533