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