1 /* Perform type resolution on the various structures. 2 Copyright (C) 2001-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 "bitmap.h" 26 #include "gfortran.h" 27 #include "arith.h" /* For gfc_compare_expr(). */ 28 #include "dependency.h" 29 #include "data.h" 30 #include "target-memory.h" /* for gfc_simplify_transfer */ 31 #include "constructor.h" 32 33 /* Types used in equivalence statements. */ 34 35 enum seq_type 36 { 37 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED 38 }; 39 40 /* Stack to keep track of the nesting of blocks as we move through the 41 code. See resolve_branch() and gfc_resolve_code(). */ 42 43 typedef struct code_stack 44 { 45 struct gfc_code *head, *current; 46 struct code_stack *prev; 47 48 /* This bitmap keeps track of the targets valid for a branch from 49 inside this block except for END {IF|SELECT}s of enclosing 50 blocks. */ 51 bitmap reachable_labels; 52 } 53 code_stack; 54 55 static code_stack *cs_base = NULL; 56 57 58 /* Nonzero if we're inside a FORALL or DO CONCURRENT block. */ 59 60 static int forall_flag; 61 int gfc_do_concurrent_flag; 62 63 /* True when we are resolving an expression that is an actual argument to 64 a procedure. */ 65 static bool actual_arg = false; 66 /* True when we are resolving an expression that is the first actual argument 67 to a procedure. */ 68 static bool first_actual_arg = false; 69 70 71 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */ 72 73 static int omp_workshare_flag; 74 75 /* True if we are processing a formal arglist. The corresponding function 76 resets the flag each time that it is read. */ 77 static bool formal_arg_flag = false; 78 79 /* True if we are resolving a specification expression. */ 80 static bool specification_expr = false; 81 82 /* The id of the last entry seen. */ 83 static int current_entry_id; 84 85 /* We use bitmaps to determine if a branch target is valid. */ 86 static bitmap_obstack labels_obstack; 87 88 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */ 89 static bool inquiry_argument = false; 90 91 92 bool 93 gfc_is_formal_arg (void) 94 { 95 return formal_arg_flag; 96 } 97 98 /* Is the symbol host associated? */ 99 static bool 100 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns) 101 { 102 for (ns = ns->parent; ns; ns = ns->parent) 103 { 104 if (sym->ns == ns) 105 return true; 106 } 107 108 return false; 109 } 110 111 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is 112 an ABSTRACT derived-type. If where is not NULL, an error message with that 113 locus is printed, optionally using name. */ 114 115 static bool 116 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name) 117 { 118 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract) 119 { 120 if (where) 121 { 122 if (name) 123 gfc_error ("%qs at %L is of the ABSTRACT type %qs", 124 name, where, ts->u.derived->name); 125 else 126 gfc_error ("ABSTRACT type %qs used at %L", 127 ts->u.derived->name, where); 128 } 129 130 return false; 131 } 132 133 return true; 134 } 135 136 137 static bool 138 check_proc_interface (gfc_symbol *ifc, locus *where) 139 { 140 /* Several checks for F08:C1216. */ 141 if (ifc->attr.procedure) 142 { 143 gfc_error ("Interface %qs at %L is declared " 144 "in a later PROCEDURE statement", ifc->name, where); 145 return false; 146 } 147 if (ifc->generic) 148 { 149 /* For generic interfaces, check if there is 150 a specific procedure with the same name. */ 151 gfc_interface *gen = ifc->generic; 152 while (gen && strcmp (gen->sym->name, ifc->name) != 0) 153 gen = gen->next; 154 if (!gen) 155 { 156 gfc_error ("Interface %qs at %L may not be generic", 157 ifc->name, where); 158 return false; 159 } 160 } 161 if (ifc->attr.proc == PROC_ST_FUNCTION) 162 { 163 gfc_error ("Interface %qs at %L may not be a statement function", 164 ifc->name, where); 165 return false; 166 } 167 if (gfc_is_intrinsic (ifc, 0, ifc->declared_at) 168 || gfc_is_intrinsic (ifc, 1, ifc->declared_at)) 169 ifc->attr.intrinsic = 1; 170 if (ifc->attr.intrinsic && !gfc_intrinsic_actual_ok (ifc->name, 0)) 171 { 172 gfc_error ("Intrinsic procedure %qs not allowed in " 173 "PROCEDURE statement at %L", ifc->name, where); 174 return false; 175 } 176 if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0') 177 { 178 gfc_error ("Interface %qs at %L must be explicit", ifc->name, where); 179 return false; 180 } 181 return true; 182 } 183 184 185 static void resolve_symbol (gfc_symbol *sym); 186 187 188 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */ 189 190 static bool 191 resolve_procedure_interface (gfc_symbol *sym) 192 { 193 gfc_symbol *ifc = sym->ts.interface; 194 195 if (!ifc) 196 return true; 197 198 if (ifc == sym) 199 { 200 gfc_error ("PROCEDURE %qs at %L may not be used as its own interface", 201 sym->name, &sym->declared_at); 202 return false; 203 } 204 if (!check_proc_interface (ifc, &sym->declared_at)) 205 return false; 206 207 if (ifc->attr.if_source || ifc->attr.intrinsic) 208 { 209 /* Resolve interface and copy attributes. */ 210 resolve_symbol (ifc); 211 if (ifc->attr.intrinsic) 212 gfc_resolve_intrinsic (ifc, &ifc->declared_at); 213 214 if (ifc->result) 215 { 216 sym->ts = ifc->result->ts; 217 sym->attr.allocatable = ifc->result->attr.allocatable; 218 sym->attr.pointer = ifc->result->attr.pointer; 219 sym->attr.dimension = ifc->result->attr.dimension; 220 sym->attr.class_ok = ifc->result->attr.class_ok; 221 sym->as = gfc_copy_array_spec (ifc->result->as); 222 sym->result = sym; 223 } 224 else 225 { 226 sym->ts = ifc->ts; 227 sym->attr.allocatable = ifc->attr.allocatable; 228 sym->attr.pointer = ifc->attr.pointer; 229 sym->attr.dimension = ifc->attr.dimension; 230 sym->attr.class_ok = ifc->attr.class_ok; 231 sym->as = gfc_copy_array_spec (ifc->as); 232 } 233 sym->ts.interface = ifc; 234 sym->attr.function = ifc->attr.function; 235 sym->attr.subroutine = ifc->attr.subroutine; 236 237 sym->attr.pure = ifc->attr.pure; 238 sym->attr.elemental = ifc->attr.elemental; 239 sym->attr.contiguous = ifc->attr.contiguous; 240 sym->attr.recursive = ifc->attr.recursive; 241 sym->attr.always_explicit = ifc->attr.always_explicit; 242 sym->attr.ext_attr |= ifc->attr.ext_attr; 243 sym->attr.is_bind_c = ifc->attr.is_bind_c; 244 /* Copy char length. */ 245 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl) 246 { 247 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl); 248 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved 249 && !gfc_resolve_expr (sym->ts.u.cl->length)) 250 return false; 251 } 252 } 253 254 return true; 255 } 256 257 258 /* Resolve types of formal argument lists. These have to be done early so that 259 the formal argument lists of module procedures can be copied to the 260 containing module before the individual procedures are resolved 261 individually. We also resolve argument lists of procedures in interface 262 blocks because they are self-contained scoping units. 263 264 Since a dummy argument cannot be a non-dummy procedure, the only 265 resort left for untyped names are the IMPLICIT types. */ 266 267 void 268 gfc_resolve_formal_arglist (gfc_symbol *proc) 269 { 270 gfc_formal_arglist *f; 271 gfc_symbol *sym; 272 bool saved_specification_expr; 273 int i; 274 275 if (proc->result != NULL) 276 sym = proc->result; 277 else 278 sym = proc; 279 280 if (gfc_elemental (proc) 281 || sym->attr.pointer || sym->attr.allocatable 282 || (sym->as && sym->as->rank != 0)) 283 { 284 proc->attr.always_explicit = 1; 285 sym->attr.always_explicit = 1; 286 } 287 288 formal_arg_flag = true; 289 290 for (f = proc->formal; f; f = f->next) 291 { 292 gfc_array_spec *as; 293 294 sym = f->sym; 295 296 if (sym == NULL) 297 { 298 /* Alternate return placeholder. */ 299 if (gfc_elemental (proc)) 300 gfc_error ("Alternate return specifier in elemental subroutine " 301 "%qs at %L is not allowed", proc->name, 302 &proc->declared_at); 303 if (proc->attr.function) 304 gfc_error ("Alternate return specifier in function " 305 "%qs at %L is not allowed", proc->name, 306 &proc->declared_at); 307 continue; 308 } 309 else if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL 310 && !resolve_procedure_interface (sym)) 311 return; 312 313 if (strcmp (proc->name, sym->name) == 0) 314 { 315 gfc_error ("Self-referential argument " 316 "%qs at %L is not allowed", sym->name, 317 &proc->declared_at); 318 return; 319 } 320 321 if (sym->attr.if_source != IFSRC_UNKNOWN) 322 gfc_resolve_formal_arglist (sym); 323 324 if (sym->attr.subroutine || sym->attr.external) 325 { 326 if (sym->attr.flavor == FL_UNKNOWN) 327 gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at); 328 } 329 else 330 { 331 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic 332 && (!sym->attr.function || sym->result == sym)) 333 gfc_set_default_type (sym, 1, sym->ns); 334 } 335 336 as = sym->ts.type == BT_CLASS && sym->attr.class_ok 337 ? CLASS_DATA (sym)->as : sym->as; 338 339 saved_specification_expr = specification_expr; 340 specification_expr = true; 341 gfc_resolve_array_spec (as, 0); 342 specification_expr = saved_specification_expr; 343 344 /* We can't tell if an array with dimension (:) is assumed or deferred 345 shape until we know if it has the pointer or allocatable attributes. 346 */ 347 if (as && as->rank > 0 && as->type == AS_DEFERRED 348 && ((sym->ts.type != BT_CLASS 349 && !(sym->attr.pointer || sym->attr.allocatable)) 350 || (sym->ts.type == BT_CLASS 351 && !(CLASS_DATA (sym)->attr.class_pointer 352 || CLASS_DATA (sym)->attr.allocatable))) 353 && sym->attr.flavor != FL_PROCEDURE) 354 { 355 as->type = AS_ASSUMED_SHAPE; 356 for (i = 0; i < as->rank; i++) 357 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1); 358 } 359 360 if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE) 361 || (as && as->type == AS_ASSUMED_RANK) 362 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target 363 || (sym->ts.type == BT_CLASS && sym->attr.class_ok 364 && (CLASS_DATA (sym)->attr.class_pointer 365 || CLASS_DATA (sym)->attr.allocatable 366 || CLASS_DATA (sym)->attr.target)) 367 || sym->attr.optional) 368 { 369 proc->attr.always_explicit = 1; 370 if (proc->result) 371 proc->result->attr.always_explicit = 1; 372 } 373 374 /* If the flavor is unknown at this point, it has to be a variable. 375 A procedure specification would have already set the type. */ 376 377 if (sym->attr.flavor == FL_UNKNOWN) 378 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at); 379 380 if (gfc_pure (proc)) 381 { 382 if (sym->attr.flavor == FL_PROCEDURE) 383 { 384 /* F08:C1279. */ 385 if (!gfc_pure (sym)) 386 { 387 gfc_error ("Dummy procedure %qs of PURE procedure at %L must " 388 "also be PURE", sym->name, &sym->declared_at); 389 continue; 390 } 391 } 392 else if (!sym->attr.pointer) 393 { 394 if (proc->attr.function && sym->attr.intent != INTENT_IN) 395 { 396 if (sym->attr.value) 397 gfc_notify_std (GFC_STD_F2008, "Argument %qs" 398 " of pure function %qs at %L with VALUE " 399 "attribute but without INTENT(IN)", 400 sym->name, proc->name, &sym->declared_at); 401 else 402 gfc_error ("Argument %qs of pure function %qs at %L must " 403 "be INTENT(IN) or VALUE", sym->name, proc->name, 404 &sym->declared_at); 405 } 406 407 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN) 408 { 409 if (sym->attr.value) 410 gfc_notify_std (GFC_STD_F2008, "Argument %qs" 411 " of pure subroutine %qs at %L with VALUE " 412 "attribute but without INTENT", sym->name, 413 proc->name, &sym->declared_at); 414 else 415 gfc_error ("Argument %qs of pure subroutine %qs at %L " 416 "must have its INTENT specified or have the " 417 "VALUE attribute", sym->name, proc->name, 418 &sym->declared_at); 419 } 420 } 421 422 /* F08:C1278a. */ 423 if (sym->ts.type == BT_CLASS && sym->attr.intent == INTENT_OUT) 424 { 425 gfc_error ("INTENT(OUT) argument %qs of pure procedure %qs at %L" 426 " may not be polymorphic", sym->name, proc->name, 427 &sym->declared_at); 428 continue; 429 } 430 } 431 432 if (proc->attr.implicit_pure) 433 { 434 if (sym->attr.flavor == FL_PROCEDURE) 435 { 436 if (!gfc_pure (sym)) 437 proc->attr.implicit_pure = 0; 438 } 439 else if (!sym->attr.pointer) 440 { 441 if (proc->attr.function && sym->attr.intent != INTENT_IN 442 && !sym->value) 443 proc->attr.implicit_pure = 0; 444 445 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN 446 && !sym->value) 447 proc->attr.implicit_pure = 0; 448 } 449 } 450 451 if (gfc_elemental (proc)) 452 { 453 /* F08:C1289. */ 454 if (sym->attr.codimension 455 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym) 456 && CLASS_DATA (sym)->attr.codimension)) 457 { 458 gfc_error ("Coarray dummy argument %qs at %L to elemental " 459 "procedure", sym->name, &sym->declared_at); 460 continue; 461 } 462 463 if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym) 464 && CLASS_DATA (sym)->as)) 465 { 466 gfc_error ("Argument %qs of elemental procedure at %L must " 467 "be scalar", sym->name, &sym->declared_at); 468 continue; 469 } 470 471 if (sym->attr.allocatable 472 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym) 473 && CLASS_DATA (sym)->attr.allocatable)) 474 { 475 gfc_error ("Argument %qs of elemental procedure at %L cannot " 476 "have the ALLOCATABLE attribute", sym->name, 477 &sym->declared_at); 478 continue; 479 } 480 481 if (sym->attr.pointer 482 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym) 483 && CLASS_DATA (sym)->attr.class_pointer)) 484 { 485 gfc_error ("Argument %qs of elemental procedure at %L cannot " 486 "have the POINTER attribute", sym->name, 487 &sym->declared_at); 488 continue; 489 } 490 491 if (sym->attr.flavor == FL_PROCEDURE) 492 { 493 gfc_error ("Dummy procedure %qs not allowed in elemental " 494 "procedure %qs at %L", sym->name, proc->name, 495 &sym->declared_at); 496 continue; 497 } 498 499 /* Fortran 2008 Corrigendum 1, C1290a. */ 500 if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value) 501 { 502 gfc_error ("Argument %qs of elemental procedure %qs at %L must " 503 "have its INTENT specified or have the VALUE " 504 "attribute", sym->name, proc->name, 505 &sym->declared_at); 506 continue; 507 } 508 } 509 510 /* Each dummy shall be specified to be scalar. */ 511 if (proc->attr.proc == PROC_ST_FUNCTION) 512 { 513 if (sym->as != NULL) 514 { 515 /* F03:C1263 (R1238) The function-name and each dummy-arg-name 516 shall be specified, explicitly or implicitly, to be scalar. */ 517 gfc_error ("Argument '%s' of statement function '%s' at %L " 518 "must be scalar", sym->name, proc->name, 519 &proc->declared_at); 520 continue; 521 } 522 523 if (sym->ts.type == BT_CHARACTER) 524 { 525 gfc_charlen *cl = sym->ts.u.cl; 526 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT) 527 { 528 gfc_error ("Character-valued argument %qs of statement " 529 "function at %L must have constant length", 530 sym->name, &sym->declared_at); 531 continue; 532 } 533 } 534 } 535 } 536 formal_arg_flag = false; 537 } 538 539 540 /* Work function called when searching for symbols that have argument lists 541 associated with them. */ 542 543 static void 544 find_arglists (gfc_symbol *sym) 545 { 546 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns 547 || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic) 548 return; 549 550 gfc_resolve_formal_arglist (sym); 551 } 552 553 554 /* Given a namespace, resolve all formal argument lists within the namespace. 555 */ 556 557 static void 558 resolve_formal_arglists (gfc_namespace *ns) 559 { 560 if (ns == NULL) 561 return; 562 563 gfc_traverse_ns (ns, find_arglists); 564 } 565 566 567 static void 568 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns) 569 { 570 bool t; 571 572 if (sym && sym->attr.flavor == FL_PROCEDURE 573 && sym->ns->parent 574 && sym->ns->parent->proc_name 575 && sym->ns->parent->proc_name->attr.flavor == FL_PROCEDURE 576 && !strcmp (sym->name, sym->ns->parent->proc_name->name)) 577 gfc_error ("Contained procedure %qs at %L has the same name as its " 578 "encompassing procedure", sym->name, &sym->declared_at); 579 580 /* If this namespace is not a function or an entry master function, 581 ignore it. */ 582 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE) 583 || sym->attr.entry_master) 584 return; 585 586 if (!sym->result) 587 return; 588 589 /* Try to find out of what the return type is. */ 590 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL) 591 { 592 t = gfc_set_default_type (sym->result, 0, ns); 593 594 if (!t && !sym->result->attr.untyped) 595 { 596 if (sym->result == sym) 597 gfc_error ("Contained function %qs at %L has no IMPLICIT type", 598 sym->name, &sym->declared_at); 599 else if (!sym->result->attr.proc_pointer) 600 gfc_error ("Result %qs of contained function %qs at %L has " 601 "no IMPLICIT type", sym->result->name, sym->name, 602 &sym->result->declared_at); 603 sym->result->attr.untyped = 1; 604 } 605 } 606 607 /* Fortran 2008 Draft Standard, page 535, C418, on type-param-value 608 type, lists the only ways a character length value of * can be used: 609 dummy arguments of procedures, named constants, function results and 610 in allocate statements if the allocate_object is an assumed length dummy 611 in external functions. Internal function results and results of module 612 procedures are not on this list, ergo, not permitted. */ 613 614 if (sym->result->ts.type == BT_CHARACTER) 615 { 616 gfc_charlen *cl = sym->result->ts.u.cl; 617 if ((!cl || !cl->length) && !sym->result->ts.deferred) 618 { 619 /* See if this is a module-procedure and adapt error message 620 accordingly. */ 621 bool module_proc; 622 gcc_assert (ns->parent && ns->parent->proc_name); 623 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE); 624 625 gfc_error (module_proc 626 ? G_("Character-valued module procedure %qs at %L" 627 " must not be assumed length") 628 : G_("Character-valued internal function %qs at %L" 629 " must not be assumed length"), 630 sym->name, &sym->declared_at); 631 } 632 } 633 } 634 635 636 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to 637 introduce duplicates. */ 638 639 static void 640 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args) 641 { 642 gfc_formal_arglist *f, *new_arglist; 643 gfc_symbol *new_sym; 644 645 for (; new_args != NULL; new_args = new_args->next) 646 { 647 new_sym = new_args->sym; 648 /* See if this arg is already in the formal argument list. */ 649 for (f = proc->formal; f; f = f->next) 650 { 651 if (new_sym == f->sym) 652 break; 653 } 654 655 if (f) 656 continue; 657 658 /* Add a new argument. Argument order is not important. */ 659 new_arglist = gfc_get_formal_arglist (); 660 new_arglist->sym = new_sym; 661 new_arglist->next = proc->formal; 662 proc->formal = new_arglist; 663 } 664 } 665 666 667 /* Flag the arguments that are not present in all entries. */ 668 669 static void 670 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args) 671 { 672 gfc_formal_arglist *f, *head; 673 head = new_args; 674 675 for (f = proc->formal; f; f = f->next) 676 { 677 if (f->sym == NULL) 678 continue; 679 680 for (new_args = head; new_args; new_args = new_args->next) 681 { 682 if (new_args->sym == f->sym) 683 break; 684 } 685 686 if (new_args) 687 continue; 688 689 f->sym->attr.not_always_present = 1; 690 } 691 } 692 693 694 /* Resolve alternate entry points. If a symbol has multiple entry points we 695 create a new master symbol for the main routine, and turn the existing 696 symbol into an entry point. */ 697 698 static void 699 resolve_entries (gfc_namespace *ns) 700 { 701 gfc_namespace *old_ns; 702 gfc_code *c; 703 gfc_symbol *proc; 704 gfc_entry_list *el; 705 char name[GFC_MAX_SYMBOL_LEN + 1]; 706 static int master_count = 0; 707 708 if (ns->proc_name == NULL) 709 return; 710 711 /* No need to do anything if this procedure doesn't have alternate entry 712 points. */ 713 if (!ns->entries) 714 return; 715 716 /* We may already have resolved alternate entry points. */ 717 if (ns->proc_name->attr.entry_master) 718 return; 719 720 /* If this isn't a procedure something has gone horribly wrong. */ 721 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE); 722 723 /* Remember the current namespace. */ 724 old_ns = gfc_current_ns; 725 726 gfc_current_ns = ns; 727 728 /* Add the main entry point to the list of entry points. */ 729 el = gfc_get_entry_list (); 730 el->sym = ns->proc_name; 731 el->id = 0; 732 el->next = ns->entries; 733 ns->entries = el; 734 ns->proc_name->attr.entry = 1; 735 736 /* If it is a module function, it needs to be in the right namespace 737 so that gfc_get_fake_result_decl can gather up the results. The 738 need for this arose in get_proc_name, where these beasts were 739 left in their own namespace, to keep prior references linked to 740 the entry declaration.*/ 741 if (ns->proc_name->attr.function 742 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE) 743 el->sym->ns = ns; 744 745 /* Do the same for entries where the master is not a module 746 procedure. These are retained in the module namespace because 747 of the module procedure declaration. */ 748 for (el = el->next; el; el = el->next) 749 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE 750 && el->sym->attr.mod_proc) 751 el->sym->ns = ns; 752 el = ns->entries; 753 754 /* Add an entry statement for it. */ 755 c = gfc_get_code (EXEC_ENTRY); 756 c->ext.entry = el; 757 c->next = ns->code; 758 ns->code = c; 759 760 /* Create a new symbol for the master function. */ 761 /* Give the internal function a unique name (within this file). 762 Also include the function name so the user has some hope of figuring 763 out what is going on. */ 764 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s", 765 master_count++, ns->proc_name->name); 766 gfc_get_ha_symbol (name, &proc); 767 gcc_assert (proc != NULL); 768 769 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL); 770 if (ns->proc_name->attr.subroutine) 771 gfc_add_subroutine (&proc->attr, proc->name, NULL); 772 else 773 { 774 gfc_symbol *sym; 775 gfc_typespec *ts, *fts; 776 gfc_array_spec *as, *fas; 777 gfc_add_function (&proc->attr, proc->name, NULL); 778 proc->result = proc; 779 fas = ns->entries->sym->as; 780 fas = fas ? fas : ns->entries->sym->result->as; 781 fts = &ns->entries->sym->result->ts; 782 if (fts->type == BT_UNKNOWN) 783 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL); 784 for (el = ns->entries->next; el; el = el->next) 785 { 786 ts = &el->sym->result->ts; 787 as = el->sym->as; 788 as = as ? as : el->sym->result->as; 789 if (ts->type == BT_UNKNOWN) 790 ts = gfc_get_default_type (el->sym->result->name, NULL); 791 792 if (! gfc_compare_types (ts, fts) 793 || (el->sym->result->attr.dimension 794 != ns->entries->sym->result->attr.dimension) 795 || (el->sym->result->attr.pointer 796 != ns->entries->sym->result->attr.pointer)) 797 break; 798 else if (as && fas && ns->entries->sym->result != el->sym->result 799 && gfc_compare_array_spec (as, fas) == 0) 800 gfc_error ("Function %s at %L has entries with mismatched " 801 "array specifications", ns->entries->sym->name, 802 &ns->entries->sym->declared_at); 803 /* The characteristics need to match and thus both need to have 804 the same string length, i.e. both len=*, or both len=4. 805 Having both len=<variable> is also possible, but difficult to 806 check at compile time. */ 807 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl 808 && (((ts->u.cl->length && !fts->u.cl->length) 809 ||(!ts->u.cl->length && fts->u.cl->length)) 810 || (ts->u.cl->length 811 && ts->u.cl->length->expr_type 812 != fts->u.cl->length->expr_type) 813 || (ts->u.cl->length 814 && ts->u.cl->length->expr_type == EXPR_CONSTANT 815 && mpz_cmp (ts->u.cl->length->value.integer, 816 fts->u.cl->length->value.integer) != 0))) 817 gfc_notify_std (GFC_STD_GNU, "Function %s at %L with " 818 "entries returning variables of different " 819 "string lengths", ns->entries->sym->name, 820 &ns->entries->sym->declared_at); 821 } 822 823 if (el == NULL) 824 { 825 sym = ns->entries->sym->result; 826 /* All result types the same. */ 827 proc->ts = *fts; 828 if (sym->attr.dimension) 829 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL); 830 if (sym->attr.pointer) 831 gfc_add_pointer (&proc->attr, NULL); 832 } 833 else 834 { 835 /* Otherwise the result will be passed through a union by 836 reference. */ 837 proc->attr.mixed_entry_master = 1; 838 for (el = ns->entries; el; el = el->next) 839 { 840 sym = el->sym->result; 841 if (sym->attr.dimension) 842 { 843 if (el == ns->entries) 844 gfc_error ("FUNCTION result %s cannot be an array in " 845 "FUNCTION %s at %L", sym->name, 846 ns->entries->sym->name, &sym->declared_at); 847 else 848 gfc_error ("ENTRY result %s cannot be an array in " 849 "FUNCTION %s at %L", sym->name, 850 ns->entries->sym->name, &sym->declared_at); 851 } 852 else if (sym->attr.pointer) 853 { 854 if (el == ns->entries) 855 gfc_error ("FUNCTION result %s cannot be a POINTER in " 856 "FUNCTION %s at %L", sym->name, 857 ns->entries->sym->name, &sym->declared_at); 858 else 859 gfc_error ("ENTRY result %s cannot be a POINTER in " 860 "FUNCTION %s at %L", sym->name, 861 ns->entries->sym->name, &sym->declared_at); 862 } 863 else 864 { 865 ts = &sym->ts; 866 if (ts->type == BT_UNKNOWN) 867 ts = gfc_get_default_type (sym->name, NULL); 868 switch (ts->type) 869 { 870 case BT_INTEGER: 871 if (ts->kind == gfc_default_integer_kind) 872 sym = NULL; 873 break; 874 case BT_REAL: 875 if (ts->kind == gfc_default_real_kind 876 || ts->kind == gfc_default_double_kind) 877 sym = NULL; 878 break; 879 case BT_COMPLEX: 880 if (ts->kind == gfc_default_complex_kind) 881 sym = NULL; 882 break; 883 case BT_LOGICAL: 884 if (ts->kind == gfc_default_logical_kind) 885 sym = NULL; 886 break; 887 case BT_UNKNOWN: 888 /* We will issue error elsewhere. */ 889 sym = NULL; 890 break; 891 default: 892 break; 893 } 894 if (sym) 895 { 896 if (el == ns->entries) 897 gfc_error ("FUNCTION result %s cannot be of type %s " 898 "in FUNCTION %s at %L", sym->name, 899 gfc_typename (ts), ns->entries->sym->name, 900 &sym->declared_at); 901 else 902 gfc_error ("ENTRY result %s cannot be of type %s " 903 "in FUNCTION %s at %L", sym->name, 904 gfc_typename (ts), ns->entries->sym->name, 905 &sym->declared_at); 906 } 907 } 908 } 909 } 910 } 911 proc->attr.access = ACCESS_PRIVATE; 912 proc->attr.entry_master = 1; 913 914 /* Merge all the entry point arguments. */ 915 for (el = ns->entries; el; el = el->next) 916 merge_argument_lists (proc, el->sym->formal); 917 918 /* Check the master formal arguments for any that are not 919 present in all entry points. */ 920 for (el = ns->entries; el; el = el->next) 921 check_argument_lists (proc, el->sym->formal); 922 923 /* Use the master function for the function body. */ 924 ns->proc_name = proc; 925 926 /* Finalize the new symbols. */ 927 gfc_commit_symbols (); 928 929 /* Restore the original namespace. */ 930 gfc_current_ns = old_ns; 931 } 932 933 934 /* Resolve common variables. */ 935 static void 936 resolve_common_vars (gfc_common_head *common_block, bool named_common) 937 { 938 gfc_symbol *csym = common_block->head; 939 940 for (; csym; csym = csym->common_next) 941 { 942 /* gfc_add_in_common may have been called before, but the reported errors 943 have been ignored to continue parsing. 944 We do the checks again here. */ 945 if (!csym->attr.use_assoc) 946 { 947 gfc_add_in_common (&csym->attr, csym->name, &common_block->where); 948 gfc_notify_std (GFC_STD_F2018_OBS, "COMMON block at %L", 949 &common_block->where); 950 } 951 952 if (csym->value || csym->attr.data) 953 { 954 if (!csym->ns->is_block_data) 955 gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON " 956 "but only in BLOCK DATA initialization is " 957 "allowed", csym->name, &csym->declared_at); 958 else if (!named_common) 959 gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is " 960 "in a blank COMMON but initialization is only " 961 "allowed in named common blocks", csym->name, 962 &csym->declared_at); 963 } 964 965 if (UNLIMITED_POLY (csym)) 966 gfc_error_now ("%qs in cannot appear in COMMON at %L " 967 "[F2008:C5100]", csym->name, &csym->declared_at); 968 969 if (csym->ts.type != BT_DERIVED) 970 continue; 971 972 if (!(csym->ts.u.derived->attr.sequence 973 || csym->ts.u.derived->attr.is_bind_c)) 974 gfc_error_now ("Derived type variable %qs in COMMON at %L " 975 "has neither the SEQUENCE nor the BIND(C) " 976 "attribute", csym->name, &csym->declared_at); 977 if (csym->ts.u.derived->attr.alloc_comp) 978 gfc_error_now ("Derived type variable %qs in COMMON at %L " 979 "has an ultimate component that is " 980 "allocatable", csym->name, &csym->declared_at); 981 if (gfc_has_default_initializer (csym->ts.u.derived)) 982 gfc_error_now ("Derived type variable %qs in COMMON at %L " 983 "may not have default initializer", csym->name, 984 &csym->declared_at); 985 986 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer) 987 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at); 988 } 989 } 990 991 /* Resolve common blocks. */ 992 static void 993 resolve_common_blocks (gfc_symtree *common_root) 994 { 995 gfc_symbol *sym; 996 gfc_gsymbol * gsym; 997 998 if (common_root == NULL) 999 return; 1000 1001 if (common_root->left) 1002 resolve_common_blocks (common_root->left); 1003 if (common_root->right) 1004 resolve_common_blocks (common_root->right); 1005 1006 resolve_common_vars (common_root->n.common, true); 1007 1008 /* The common name is a global name - in Fortran 2003 also if it has a 1009 C binding name, since Fortran 2008 only the C binding name is a global 1010 identifier. */ 1011 if (!common_root->n.common->binding_label 1012 || gfc_notification_std (GFC_STD_F2008)) 1013 { 1014 gsym = gfc_find_gsymbol (gfc_gsym_root, 1015 common_root->n.common->name); 1016 1017 if (gsym && gfc_notification_std (GFC_STD_F2008) 1018 && gsym->type == GSYM_COMMON 1019 && ((common_root->n.common->binding_label 1020 && (!gsym->binding_label 1021 || strcmp (common_root->n.common->binding_label, 1022 gsym->binding_label) != 0)) 1023 || (!common_root->n.common->binding_label 1024 && gsym->binding_label))) 1025 { 1026 gfc_error ("In Fortran 2003 COMMON %qs block at %L is a global " 1027 "identifier and must thus have the same binding name " 1028 "as the same-named COMMON block at %L: %s vs %s", 1029 common_root->n.common->name, &common_root->n.common->where, 1030 &gsym->where, 1031 common_root->n.common->binding_label 1032 ? common_root->n.common->binding_label : "(blank)", 1033 gsym->binding_label ? gsym->binding_label : "(blank)"); 1034 return; 1035 } 1036 1037 if (gsym && gsym->type != GSYM_COMMON 1038 && !common_root->n.common->binding_label) 1039 { 1040 gfc_error ("COMMON block %qs at %L uses the same global identifier " 1041 "as entity at %L", 1042 common_root->n.common->name, &common_root->n.common->where, 1043 &gsym->where); 1044 return; 1045 } 1046 if (gsym && gsym->type != GSYM_COMMON) 1047 { 1048 gfc_error ("Fortran 2008: COMMON block %qs with binding label at " 1049 "%L sharing the identifier with global non-COMMON-block " 1050 "entity at %L", common_root->n.common->name, 1051 &common_root->n.common->where, &gsym->where); 1052 return; 1053 } 1054 if (!gsym) 1055 { 1056 gsym = gfc_get_gsymbol (common_root->n.common->name, false); 1057 gsym->type = GSYM_COMMON; 1058 gsym->where = common_root->n.common->where; 1059 gsym->defined = 1; 1060 } 1061 gsym->used = 1; 1062 } 1063 1064 if (common_root->n.common->binding_label) 1065 { 1066 gsym = gfc_find_gsymbol (gfc_gsym_root, 1067 common_root->n.common->binding_label); 1068 if (gsym && gsym->type != GSYM_COMMON) 1069 { 1070 gfc_error ("COMMON block at %L with binding label %qs uses the same " 1071 "global identifier as entity at %L", 1072 &common_root->n.common->where, 1073 common_root->n.common->binding_label, &gsym->where); 1074 return; 1075 } 1076 if (!gsym) 1077 { 1078 gsym = gfc_get_gsymbol (common_root->n.common->binding_label, true); 1079 gsym->type = GSYM_COMMON; 1080 gsym->where = common_root->n.common->where; 1081 gsym->defined = 1; 1082 } 1083 gsym->used = 1; 1084 } 1085 1086 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym); 1087 if (sym == NULL) 1088 return; 1089 1090 if (sym->attr.flavor == FL_PARAMETER) 1091 gfc_error ("COMMON block %qs at %L is used as PARAMETER at %L", 1092 sym->name, &common_root->n.common->where, &sym->declared_at); 1093 1094 if (sym->attr.external) 1095 gfc_error ("COMMON block %qs at %L cannot have the EXTERNAL attribute", 1096 sym->name, &common_root->n.common->where); 1097 1098 if (sym->attr.intrinsic) 1099 gfc_error ("COMMON block %qs at %L is also an intrinsic procedure", 1100 sym->name, &common_root->n.common->where); 1101 else if (sym->attr.result 1102 || gfc_is_function_return_value (sym, gfc_current_ns)) 1103 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L " 1104 "that is also a function result", sym->name, 1105 &common_root->n.common->where); 1106 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL 1107 && sym->attr.proc != PROC_ST_FUNCTION) 1108 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L " 1109 "that is also a global procedure", sym->name, 1110 &common_root->n.common->where); 1111 } 1112 1113 1114 /* Resolve contained function types. Because contained functions can call one 1115 another, they have to be worked out before any of the contained procedures 1116 can be resolved. 1117 1118 The good news is that if a function doesn't already have a type, the only 1119 way it can get one is through an IMPLICIT type or a RESULT variable, because 1120 by definition contained functions are contained namespace they're contained 1121 in, not in a sibling or parent namespace. */ 1122 1123 static void 1124 resolve_contained_functions (gfc_namespace *ns) 1125 { 1126 gfc_namespace *child; 1127 gfc_entry_list *el; 1128 1129 resolve_formal_arglists (ns); 1130 1131 for (child = ns->contained; child; child = child->sibling) 1132 { 1133 /* Resolve alternate entry points first. */ 1134 resolve_entries (child); 1135 1136 /* Then check function return types. */ 1137 resolve_contained_fntype (child->proc_name, child); 1138 for (el = child->entries; el; el = el->next) 1139 resolve_contained_fntype (el->sym, child); 1140 } 1141 } 1142 1143 1144 1145 /* A Parameterized Derived Type constructor must contain values for 1146 the PDT KIND parameters or they must have a default initializer. 1147 Go through the constructor picking out the KIND expressions, 1148 storing them in 'param_list' and then call gfc_get_pdt_instance 1149 to obtain the PDT instance. */ 1150 1151 static gfc_actual_arglist *param_list, *param_tail, *param; 1152 1153 static bool 1154 get_pdt_spec_expr (gfc_component *c, gfc_expr *expr) 1155 { 1156 param = gfc_get_actual_arglist (); 1157 if (!param_list) 1158 param_list = param_tail = param; 1159 else 1160 { 1161 param_tail->next = param; 1162 param_tail = param_tail->next; 1163 } 1164 1165 param_tail->name = c->name; 1166 if (expr) 1167 param_tail->expr = gfc_copy_expr (expr); 1168 else if (c->initializer) 1169 param_tail->expr = gfc_copy_expr (c->initializer); 1170 else 1171 { 1172 param_tail->spec_type = SPEC_ASSUMED; 1173 if (c->attr.pdt_kind) 1174 { 1175 gfc_error ("The KIND parameter %qs in the PDT constructor " 1176 "at %C has no value", param->name); 1177 return false; 1178 } 1179 } 1180 1181 return true; 1182 } 1183 1184 static bool 1185 get_pdt_constructor (gfc_expr *expr, gfc_constructor **constr, 1186 gfc_symbol *derived) 1187 { 1188 gfc_constructor *cons = NULL; 1189 gfc_component *comp; 1190 bool t = true; 1191 1192 if (expr && expr->expr_type == EXPR_STRUCTURE) 1193 cons = gfc_constructor_first (expr->value.constructor); 1194 else if (constr) 1195 cons = *constr; 1196 gcc_assert (cons); 1197 1198 comp = derived->components; 1199 1200 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons)) 1201 { 1202 if (cons->expr 1203 && cons->expr->expr_type == EXPR_STRUCTURE 1204 && comp->ts.type == BT_DERIVED) 1205 { 1206 t = get_pdt_constructor (cons->expr, NULL, comp->ts.u.derived); 1207 if (!t) 1208 return t; 1209 } 1210 else if (comp->ts.type == BT_DERIVED) 1211 { 1212 t = get_pdt_constructor (NULL, &cons, comp->ts.u.derived); 1213 if (!t) 1214 return t; 1215 } 1216 else if ((comp->attr.pdt_kind || comp->attr.pdt_len) 1217 && derived->attr.pdt_template) 1218 { 1219 t = get_pdt_spec_expr (comp, cons->expr); 1220 if (!t) 1221 return t; 1222 } 1223 } 1224 return t; 1225 } 1226 1227 1228 static bool resolve_fl_derived0 (gfc_symbol *sym); 1229 static bool resolve_fl_struct (gfc_symbol *sym); 1230 1231 1232 /* Resolve all of the elements of a structure constructor and make sure that 1233 the types are correct. The 'init' flag indicates that the given 1234 constructor is an initializer. */ 1235 1236 static bool 1237 resolve_structure_cons (gfc_expr *expr, int init) 1238 { 1239 gfc_constructor *cons; 1240 gfc_component *comp; 1241 bool t; 1242 symbol_attribute a; 1243 1244 t = true; 1245 1246 if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION) 1247 { 1248 if (expr->ts.u.derived->attr.flavor == FL_DERIVED) 1249 resolve_fl_derived0 (expr->ts.u.derived); 1250 else 1251 resolve_fl_struct (expr->ts.u.derived); 1252 1253 /* If this is a Parameterized Derived Type template, find the 1254 instance corresponding to the PDT kind parameters. */ 1255 if (expr->ts.u.derived->attr.pdt_template) 1256 { 1257 param_list = NULL; 1258 t = get_pdt_constructor (expr, NULL, expr->ts.u.derived); 1259 if (!t) 1260 return t; 1261 gfc_get_pdt_instance (param_list, &expr->ts.u.derived, NULL); 1262 1263 expr->param_list = gfc_copy_actual_arglist (param_list); 1264 1265 if (param_list) 1266 gfc_free_actual_arglist (param_list); 1267 1268 if (!expr->ts.u.derived->attr.pdt_type) 1269 return false; 1270 } 1271 } 1272 1273 cons = gfc_constructor_first (expr->value.constructor); 1274 1275 /* A constructor may have references if it is the result of substituting a 1276 parameter variable. In this case we just pull out the component we 1277 want. */ 1278 if (expr->ref) 1279 comp = expr->ref->u.c.sym->components; 1280 else 1281 comp = expr->ts.u.derived->components; 1282 1283 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons)) 1284 { 1285 int rank; 1286 1287 if (!cons->expr) 1288 continue; 1289 1290 /* Unions use an EXPR_NULL contrived expression to tell the translation 1291 phase to generate an initializer of the appropriate length. 1292 Ignore it here. */ 1293 if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL) 1294 continue; 1295 1296 if (!gfc_resolve_expr (cons->expr)) 1297 { 1298 t = false; 1299 continue; 1300 } 1301 1302 rank = comp->as ? comp->as->rank : 0; 1303 if (comp->ts.type == BT_CLASS 1304 && !comp->ts.u.derived->attr.unlimited_polymorphic 1305 && CLASS_DATA (comp)->as) 1306 rank = CLASS_DATA (comp)->as->rank; 1307 1308 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank 1309 && (comp->attr.allocatable || cons->expr->rank)) 1310 { 1311 gfc_error ("The rank of the element in the structure " 1312 "constructor at %L does not match that of the " 1313 "component (%d/%d)", &cons->expr->where, 1314 cons->expr->rank, rank); 1315 t = false; 1316 } 1317 1318 /* If we don't have the right type, try to convert it. */ 1319 1320 if (!comp->attr.proc_pointer && 1321 !gfc_compare_types (&cons->expr->ts, &comp->ts)) 1322 { 1323 if (strcmp (comp->name, "_extends") == 0) 1324 { 1325 /* Can afford to be brutal with the _extends initializer. 1326 The derived type can get lost because it is PRIVATE 1327 but it is not usage constrained by the standard. */ 1328 cons->expr->ts = comp->ts; 1329 } 1330 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN) 1331 { 1332 gfc_error ("The element in the structure constructor at %L, " 1333 "for pointer component %qs, is %s but should be %s", 1334 &cons->expr->where, comp->name, 1335 gfc_basic_typename (cons->expr->ts.type), 1336 gfc_basic_typename (comp->ts.type)); 1337 t = false; 1338 } 1339 else 1340 { 1341 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1); 1342 if (t) 1343 t = t2; 1344 } 1345 } 1346 1347 /* For strings, the length of the constructor should be the same as 1348 the one of the structure, ensure this if the lengths are known at 1349 compile time and when we are dealing with PARAMETER or structure 1350 constructors. */ 1351 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl 1352 && comp->ts.u.cl->length 1353 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT 1354 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length 1355 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT 1356 && cons->expr->rank != 0 1357 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer, 1358 comp->ts.u.cl->length->value.integer) != 0) 1359 { 1360 if (cons->expr->expr_type == EXPR_VARIABLE 1361 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER) 1362 { 1363 /* Wrap the parameter in an array constructor (EXPR_ARRAY) 1364 to make use of the gfc_resolve_character_array_constructor 1365 machinery. The expression is later simplified away to 1366 an array of string literals. */ 1367 gfc_expr *para = cons->expr; 1368 cons->expr = gfc_get_expr (); 1369 cons->expr->ts = para->ts; 1370 cons->expr->where = para->where; 1371 cons->expr->expr_type = EXPR_ARRAY; 1372 cons->expr->rank = para->rank; 1373 cons->expr->shape = gfc_copy_shape (para->shape, para->rank); 1374 gfc_constructor_append_expr (&cons->expr->value.constructor, 1375 para, &cons->expr->where); 1376 } 1377 1378 if (cons->expr->expr_type == EXPR_ARRAY) 1379 { 1380 /* Rely on the cleanup of the namespace to deal correctly with 1381 the old charlen. (There was a block here that attempted to 1382 remove the charlen but broke the chain in so doing.) */ 1383 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL); 1384 cons->expr->ts.u.cl->length_from_typespec = true; 1385 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length); 1386 gfc_resolve_character_array_constructor (cons->expr); 1387 } 1388 } 1389 1390 if (cons->expr->expr_type == EXPR_NULL 1391 && !(comp->attr.pointer || comp->attr.allocatable 1392 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID 1393 || (comp->ts.type == BT_CLASS 1394 && (CLASS_DATA (comp)->attr.class_pointer 1395 || CLASS_DATA (comp)->attr.allocatable)))) 1396 { 1397 t = false; 1398 gfc_error ("The NULL in the structure constructor at %L is " 1399 "being applied to component %qs, which is neither " 1400 "a POINTER nor ALLOCATABLE", &cons->expr->where, 1401 comp->name); 1402 } 1403 1404 if (comp->attr.proc_pointer && comp->ts.interface) 1405 { 1406 /* Check procedure pointer interface. */ 1407 gfc_symbol *s2 = NULL; 1408 gfc_component *c2; 1409 const char *name; 1410 char err[200]; 1411 1412 c2 = gfc_get_proc_ptr_comp (cons->expr); 1413 if (c2) 1414 { 1415 s2 = c2->ts.interface; 1416 name = c2->name; 1417 } 1418 else if (cons->expr->expr_type == EXPR_FUNCTION) 1419 { 1420 s2 = cons->expr->symtree->n.sym->result; 1421 name = cons->expr->symtree->n.sym->result->name; 1422 } 1423 else if (cons->expr->expr_type != EXPR_NULL) 1424 { 1425 s2 = cons->expr->symtree->n.sym; 1426 name = cons->expr->symtree->n.sym->name; 1427 } 1428 1429 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1, 1430 err, sizeof (err), NULL, NULL)) 1431 { 1432 gfc_error_opt (0, "Interface mismatch for procedure-pointer " 1433 "component %qs in structure constructor at %L:" 1434 " %s", comp->name, &cons->expr->where, err); 1435 return false; 1436 } 1437 } 1438 1439 /* Validate shape, except for dynamic or PDT arrays. */ 1440 if (cons->expr->expr_type == EXPR_ARRAY && rank == cons->expr->rank 1441 && comp->as && !comp->attr.allocatable && !comp->attr.pointer 1442 && !comp->attr.pdt_array) 1443 { 1444 mpz_t len; 1445 mpz_init (len); 1446 for (int n = 0; n < rank; n++) 1447 { 1448 if (comp->as->upper[n]->expr_type != EXPR_CONSTANT 1449 || comp->as->lower[n]->expr_type != EXPR_CONSTANT) 1450 { 1451 gfc_error ("Bad array spec of component %qs referenced in " 1452 "structure constructor at %L", 1453 comp->name, &cons->expr->where); 1454 t = false; 1455 break; 1456 }; 1457 if (cons->expr->shape == NULL) 1458 continue; 1459 mpz_set_ui (len, 1); 1460 mpz_add (len, len, comp->as->upper[n]->value.integer); 1461 mpz_sub (len, len, comp->as->lower[n]->value.integer); 1462 if (mpz_cmp (cons->expr->shape[n], len) != 0) 1463 { 1464 gfc_error ("The shape of component %qs in the structure " 1465 "constructor at %L differs from the shape of the " 1466 "declared component for dimension %d (%ld/%ld)", 1467 comp->name, &cons->expr->where, n+1, 1468 mpz_get_si (cons->expr->shape[n]), 1469 mpz_get_si (len)); 1470 t = false; 1471 } 1472 } 1473 mpz_clear (len); 1474 } 1475 1476 if (!comp->attr.pointer || comp->attr.proc_pointer 1477 || cons->expr->expr_type == EXPR_NULL) 1478 continue; 1479 1480 a = gfc_expr_attr (cons->expr); 1481 1482 if (!a.pointer && !a.target) 1483 { 1484 t = false; 1485 gfc_error ("The element in the structure constructor at %L, " 1486 "for pointer component %qs should be a POINTER or " 1487 "a TARGET", &cons->expr->where, comp->name); 1488 } 1489 1490 if (init) 1491 { 1492 /* F08:C461. Additional checks for pointer initialization. */ 1493 if (a.allocatable) 1494 { 1495 t = false; 1496 gfc_error ("Pointer initialization target at %L " 1497 "must not be ALLOCATABLE", &cons->expr->where); 1498 } 1499 if (!a.save) 1500 { 1501 t = false; 1502 gfc_error ("Pointer initialization target at %L " 1503 "must have the SAVE attribute", &cons->expr->where); 1504 } 1505 } 1506 1507 /* F2003, C1272 (3). */ 1508 bool impure = cons->expr->expr_type == EXPR_VARIABLE 1509 && (gfc_impure_variable (cons->expr->symtree->n.sym) 1510 || gfc_is_coindexed (cons->expr)); 1511 if (impure && gfc_pure (NULL)) 1512 { 1513 t = false; 1514 gfc_error ("Invalid expression in the structure constructor for " 1515 "pointer component %qs at %L in PURE procedure", 1516 comp->name, &cons->expr->where); 1517 } 1518 1519 if (impure) 1520 gfc_unset_implicit_pure (NULL); 1521 } 1522 1523 return t; 1524 } 1525 1526 1527 /****************** Expression name resolution ******************/ 1528 1529 /* Returns 0 if a symbol was not declared with a type or 1530 attribute declaration statement, nonzero otherwise. */ 1531 1532 static int 1533 was_declared (gfc_symbol *sym) 1534 { 1535 symbol_attribute a; 1536 1537 a = sym->attr; 1538 1539 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN) 1540 return 1; 1541 1542 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic 1543 || a.optional || a.pointer || a.save || a.target || a.volatile_ 1544 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN 1545 || a.asynchronous || a.codimension) 1546 return 1; 1547 1548 return 0; 1549 } 1550 1551 1552 /* Determine if a symbol is generic or not. */ 1553 1554 static int 1555 generic_sym (gfc_symbol *sym) 1556 { 1557 gfc_symbol *s; 1558 1559 if (sym->attr.generic || 1560 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name))) 1561 return 1; 1562 1563 if (was_declared (sym) || sym->ns->parent == NULL) 1564 return 0; 1565 1566 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s); 1567 1568 if (s != NULL) 1569 { 1570 if (s == sym) 1571 return 0; 1572 else 1573 return generic_sym (s); 1574 } 1575 1576 return 0; 1577 } 1578 1579 1580 /* Determine if a symbol is specific or not. */ 1581 1582 static int 1583 specific_sym (gfc_symbol *sym) 1584 { 1585 gfc_symbol *s; 1586 1587 if (sym->attr.if_source == IFSRC_IFBODY 1588 || sym->attr.proc == PROC_MODULE 1589 || sym->attr.proc == PROC_INTERNAL 1590 || sym->attr.proc == PROC_ST_FUNCTION 1591 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name)) 1592 || sym->attr.external) 1593 return 1; 1594 1595 if (was_declared (sym) || sym->ns->parent == NULL) 1596 return 0; 1597 1598 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s); 1599 1600 return (s == NULL) ? 0 : specific_sym (s); 1601 } 1602 1603 1604 /* Figure out if the procedure is specific, generic or unknown. */ 1605 1606 enum proc_type 1607 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }; 1608 1609 static proc_type 1610 procedure_kind (gfc_symbol *sym) 1611 { 1612 if (generic_sym (sym)) 1613 return PTYPE_GENERIC; 1614 1615 if (specific_sym (sym)) 1616 return PTYPE_SPECIFIC; 1617 1618 return PTYPE_UNKNOWN; 1619 } 1620 1621 /* Check references to assumed size arrays. The flag need_full_assumed_size 1622 is nonzero when matching actual arguments. */ 1623 1624 static int need_full_assumed_size = 0; 1625 1626 static bool 1627 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e) 1628 { 1629 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE)) 1630 return false; 1631 1632 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong. 1633 What should it be? */ 1634 if (e->ref && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL) 1635 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE) 1636 && (e->ref->u.ar.type == AR_FULL)) 1637 { 1638 gfc_error ("The upper bound in the last dimension must " 1639 "appear in the reference to the assumed size " 1640 "array %qs at %L", sym->name, &e->where); 1641 return true; 1642 } 1643 return false; 1644 } 1645 1646 1647 /* Look for bad assumed size array references in argument expressions 1648 of elemental and array valued intrinsic procedures. Since this is 1649 called from procedure resolution functions, it only recurses at 1650 operators. */ 1651 1652 static bool 1653 resolve_assumed_size_actual (gfc_expr *e) 1654 { 1655 if (e == NULL) 1656 return false; 1657 1658 switch (e->expr_type) 1659 { 1660 case EXPR_VARIABLE: 1661 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e)) 1662 return true; 1663 break; 1664 1665 case EXPR_OP: 1666 if (resolve_assumed_size_actual (e->value.op.op1) 1667 || resolve_assumed_size_actual (e->value.op.op2)) 1668 return true; 1669 break; 1670 1671 default: 1672 break; 1673 } 1674 return false; 1675 } 1676 1677 1678 /* Check a generic procedure, passed as an actual argument, to see if 1679 there is a matching specific name. If none, it is an error, and if 1680 more than one, the reference is ambiguous. */ 1681 static int 1682 count_specific_procs (gfc_expr *e) 1683 { 1684 int n; 1685 gfc_interface *p; 1686 gfc_symbol *sym; 1687 1688 n = 0; 1689 sym = e->symtree->n.sym; 1690 1691 for (p = sym->generic; p; p = p->next) 1692 if (strcmp (sym->name, p->sym->name) == 0) 1693 { 1694 e->symtree = gfc_find_symtree (p->sym->ns->sym_root, 1695 sym->name); 1696 n++; 1697 } 1698 1699 if (n > 1) 1700 gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name, 1701 &e->where); 1702 1703 if (n == 0) 1704 gfc_error ("GENERIC procedure %qs is not allowed as an actual " 1705 "argument at %L", sym->name, &e->where); 1706 1707 return n; 1708 } 1709 1710 1711 /* See if a call to sym could possibly be a not allowed RECURSION because of 1712 a missing RECURSIVE declaration. This means that either sym is the current 1713 context itself, or sym is the parent of a contained procedure calling its 1714 non-RECURSIVE containing procedure. 1715 This also works if sym is an ENTRY. */ 1716 1717 static bool 1718 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context) 1719 { 1720 gfc_symbol* proc_sym; 1721 gfc_symbol* context_proc; 1722 gfc_namespace* real_context; 1723 1724 if (sym->attr.flavor == FL_PROGRAM 1725 || gfc_fl_struct (sym->attr.flavor)) 1726 return false; 1727 1728 /* If we've got an ENTRY, find real procedure. */ 1729 if (sym->attr.entry && sym->ns->entries) 1730 proc_sym = sym->ns->entries->sym; 1731 else 1732 proc_sym = sym; 1733 1734 /* If sym is RECURSIVE, all is well of course. */ 1735 if (proc_sym->attr.recursive || flag_recursive) 1736 return false; 1737 1738 /* Find the context procedure's "real" symbol if it has entries. 1739 We look for a procedure symbol, so recurse on the parents if we don't 1740 find one (like in case of a BLOCK construct). */ 1741 for (real_context = context; ; real_context = real_context->parent) 1742 { 1743 /* We should find something, eventually! */ 1744 gcc_assert (real_context); 1745 1746 context_proc = (real_context->entries ? real_context->entries->sym 1747 : real_context->proc_name); 1748 1749 /* In some special cases, there may not be a proc_name, like for this 1750 invalid code: 1751 real(bad_kind()) function foo () ... 1752 when checking the call to bad_kind (). 1753 In these cases, we simply return here and assume that the 1754 call is ok. */ 1755 if (!context_proc) 1756 return false; 1757 1758 if (context_proc->attr.flavor != FL_LABEL) 1759 break; 1760 } 1761 1762 /* A call from sym's body to itself is recursion, of course. */ 1763 if (context_proc == proc_sym) 1764 return true; 1765 1766 /* The same is true if context is a contained procedure and sym the 1767 containing one. */ 1768 if (context_proc->attr.contained) 1769 { 1770 gfc_symbol* parent_proc; 1771 1772 gcc_assert (context->parent); 1773 parent_proc = (context->parent->entries ? context->parent->entries->sym 1774 : context->parent->proc_name); 1775 1776 if (parent_proc == proc_sym) 1777 return true; 1778 } 1779 1780 return false; 1781 } 1782 1783 1784 /* Resolve an intrinsic procedure: Set its function/subroutine attribute, 1785 its typespec and formal argument list. */ 1786 1787 bool 1788 gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc) 1789 { 1790 gfc_intrinsic_sym* isym = NULL; 1791 const char* symstd; 1792 1793 if (sym->resolve_symbol_called >= 2) 1794 return true; 1795 1796 sym->resolve_symbol_called = 2; 1797 1798 /* Already resolved. */ 1799 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN) 1800 return true; 1801 1802 /* We already know this one is an intrinsic, so we don't call 1803 gfc_is_intrinsic for full checking but rather use gfc_find_function and 1804 gfc_find_subroutine directly to check whether it is a function or 1805 subroutine. */ 1806 1807 if (sym->intmod_sym_id && sym->attr.subroutine) 1808 { 1809 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym); 1810 isym = gfc_intrinsic_subroutine_by_id (id); 1811 } 1812 else if (sym->intmod_sym_id) 1813 { 1814 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym); 1815 isym = gfc_intrinsic_function_by_id (id); 1816 } 1817 else if (!sym->attr.subroutine) 1818 isym = gfc_find_function (sym->name); 1819 1820 if (isym && !sym->attr.subroutine) 1821 { 1822 if (sym->ts.type != BT_UNKNOWN && warn_surprising 1823 && !sym->attr.implicit_type) 1824 gfc_warning (OPT_Wsurprising, 1825 "Type specified for intrinsic function %qs at %L is" 1826 " ignored", sym->name, &sym->declared_at); 1827 1828 if (!sym->attr.function && 1829 !gfc_add_function(&sym->attr, sym->name, loc)) 1830 return false; 1831 1832 sym->ts = isym->ts; 1833 } 1834 else if (isym || (isym = gfc_find_subroutine (sym->name))) 1835 { 1836 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type) 1837 { 1838 gfc_error ("Intrinsic subroutine %qs at %L shall not have a type" 1839 " specifier", sym->name, &sym->declared_at); 1840 return false; 1841 } 1842 1843 if (!sym->attr.subroutine && 1844 !gfc_add_subroutine(&sym->attr, sym->name, loc)) 1845 return false; 1846 } 1847 else 1848 { 1849 gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name, 1850 &sym->declared_at); 1851 return false; 1852 } 1853 1854 gfc_copy_formal_args_intr (sym, isym, NULL); 1855 1856 sym->attr.pure = isym->pure; 1857 sym->attr.elemental = isym->elemental; 1858 1859 /* Check it is actually available in the standard settings. */ 1860 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at)) 1861 { 1862 gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not " 1863 "available in the current standard settings but %s. Use " 1864 "an appropriate %<-std=*%> option or enable " 1865 "%<-fall-intrinsics%> in order to use it.", 1866 sym->name, &sym->declared_at, symstd); 1867 return false; 1868 } 1869 1870 return true; 1871 } 1872 1873 1874 /* Resolve a procedure expression, like passing it to a called procedure or as 1875 RHS for a procedure pointer assignment. */ 1876 1877 static bool 1878 resolve_procedure_expression (gfc_expr* expr) 1879 { 1880 gfc_symbol* sym; 1881 1882 if (expr->expr_type != EXPR_VARIABLE) 1883 return true; 1884 gcc_assert (expr->symtree); 1885 1886 sym = expr->symtree->n.sym; 1887 1888 if (sym->attr.intrinsic) 1889 gfc_resolve_intrinsic (sym, &expr->where); 1890 1891 if (sym->attr.flavor != FL_PROCEDURE 1892 || (sym->attr.function && sym->result == sym)) 1893 return true; 1894 1895 /* A non-RECURSIVE procedure that is used as procedure expression within its 1896 own body is in danger of being called recursively. */ 1897 if (is_illegal_recursion (sym, gfc_current_ns)) 1898 gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling" 1899 " itself recursively. Declare it RECURSIVE or use" 1900 " %<-frecursive%>", sym->name, &expr->where); 1901 1902 return true; 1903 } 1904 1905 1906 /* Check that name is not a derived type. */ 1907 1908 static bool 1909 is_dt_name (const char *name) 1910 { 1911 gfc_symbol *dt_list, *dt_first; 1912 1913 dt_list = dt_first = gfc_derived_types; 1914 for (; dt_list; dt_list = dt_list->dt_next) 1915 { 1916 if (strcmp(dt_list->name, name) == 0) 1917 return true; 1918 if (dt_first == dt_list->dt_next) 1919 break; 1920 } 1921 return false; 1922 } 1923 1924 1925 /* Resolve an actual argument list. Most of the time, this is just 1926 resolving the expressions in the list. 1927 The exception is that we sometimes have to decide whether arguments 1928 that look like procedure arguments are really simple variable 1929 references. */ 1930 1931 static bool 1932 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype, 1933 bool no_formal_args) 1934 { 1935 gfc_symbol *sym; 1936 gfc_symtree *parent_st; 1937 gfc_expr *e; 1938 gfc_component *comp; 1939 int save_need_full_assumed_size; 1940 bool return_value = false; 1941 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg; 1942 1943 actual_arg = true; 1944 first_actual_arg = true; 1945 1946 for (; arg; arg = arg->next) 1947 { 1948 e = arg->expr; 1949 if (e == NULL) 1950 { 1951 /* Check the label is a valid branching target. */ 1952 if (arg->label) 1953 { 1954 if (arg->label->defined == ST_LABEL_UNKNOWN) 1955 { 1956 gfc_error ("Label %d referenced at %L is never defined", 1957 arg->label->value, &arg->label->where); 1958 goto cleanup; 1959 } 1960 } 1961 first_actual_arg = false; 1962 continue; 1963 } 1964 1965 if (e->expr_type == EXPR_VARIABLE 1966 && e->symtree->n.sym->attr.generic 1967 && no_formal_args 1968 && count_specific_procs (e) != 1) 1969 goto cleanup; 1970 1971 if (e->ts.type != BT_PROCEDURE) 1972 { 1973 save_need_full_assumed_size = need_full_assumed_size; 1974 if (e->expr_type != EXPR_VARIABLE) 1975 need_full_assumed_size = 0; 1976 if (!gfc_resolve_expr (e)) 1977 goto cleanup; 1978 need_full_assumed_size = save_need_full_assumed_size; 1979 goto argument_list; 1980 } 1981 1982 /* See if the expression node should really be a variable reference. */ 1983 1984 sym = e->symtree->n.sym; 1985 1986 if (sym->attr.flavor == FL_PROCEDURE && is_dt_name (sym->name)) 1987 { 1988 gfc_error ("Derived type %qs is used as an actual " 1989 "argument at %L", sym->name, &e->where); 1990 goto cleanup; 1991 } 1992 1993 if (sym->attr.flavor == FL_PROCEDURE 1994 || sym->attr.intrinsic 1995 || sym->attr.external) 1996 { 1997 int actual_ok; 1998 1999 /* If a procedure is not already determined to be something else 2000 check if it is intrinsic. */ 2001 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where)) 2002 sym->attr.intrinsic = 1; 2003 2004 if (sym->attr.proc == PROC_ST_FUNCTION) 2005 { 2006 gfc_error ("Statement function %qs at %L is not allowed as an " 2007 "actual argument", sym->name, &e->where); 2008 } 2009 2010 actual_ok = gfc_intrinsic_actual_ok (sym->name, 2011 sym->attr.subroutine); 2012 if (sym->attr.intrinsic && actual_ok == 0) 2013 { 2014 gfc_error ("Intrinsic %qs at %L is not allowed as an " 2015 "actual argument", sym->name, &e->where); 2016 } 2017 2018 if (sym->attr.contained && !sym->attr.use_assoc 2019 && sym->ns->proc_name->attr.flavor != FL_MODULE) 2020 { 2021 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is" 2022 " used as actual argument at %L", 2023 sym->name, &e->where)) 2024 goto cleanup; 2025 } 2026 2027 if (sym->attr.elemental && !sym->attr.intrinsic) 2028 { 2029 gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not " 2030 "allowed as an actual argument at %L", sym->name, 2031 &e->where); 2032 } 2033 2034 /* Check if a generic interface has a specific procedure 2035 with the same name before emitting an error. */ 2036 if (sym->attr.generic && count_specific_procs (e) != 1) 2037 goto cleanup; 2038 2039 /* Just in case a specific was found for the expression. */ 2040 sym = e->symtree->n.sym; 2041 2042 /* If the symbol is the function that names the current (or 2043 parent) scope, then we really have a variable reference. */ 2044 2045 if (gfc_is_function_return_value (sym, sym->ns)) 2046 goto got_variable; 2047 2048 /* If all else fails, see if we have a specific intrinsic. */ 2049 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic) 2050 { 2051 gfc_intrinsic_sym *isym; 2052 2053 isym = gfc_find_function (sym->name); 2054 if (isym == NULL || !isym->specific) 2055 { 2056 gfc_error ("Unable to find a specific INTRINSIC procedure " 2057 "for the reference %qs at %L", sym->name, 2058 &e->where); 2059 goto cleanup; 2060 } 2061 sym->ts = isym->ts; 2062 sym->attr.intrinsic = 1; 2063 sym->attr.function = 1; 2064 } 2065 2066 if (!gfc_resolve_expr (e)) 2067 goto cleanup; 2068 goto argument_list; 2069 } 2070 2071 /* See if the name is a module procedure in a parent unit. */ 2072 2073 if (was_declared (sym) || sym->ns->parent == NULL) 2074 goto got_variable; 2075 2076 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st)) 2077 { 2078 gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where); 2079 goto cleanup; 2080 } 2081 2082 if (parent_st == NULL) 2083 goto got_variable; 2084 2085 sym = parent_st->n.sym; 2086 e->symtree = parent_st; /* Point to the right thing. */ 2087 2088 if (sym->attr.flavor == FL_PROCEDURE 2089 || sym->attr.intrinsic 2090 || sym->attr.external) 2091 { 2092 if (!gfc_resolve_expr (e)) 2093 goto cleanup; 2094 goto argument_list; 2095 } 2096 2097 got_variable: 2098 e->expr_type = EXPR_VARIABLE; 2099 e->ts = sym->ts; 2100 if ((sym->as != NULL && sym->ts.type != BT_CLASS) 2101 || (sym->ts.type == BT_CLASS && sym->attr.class_ok 2102 && CLASS_DATA (sym)->as)) 2103 { 2104 e->rank = sym->ts.type == BT_CLASS 2105 ? CLASS_DATA (sym)->as->rank : sym->as->rank; 2106 e->ref = gfc_get_ref (); 2107 e->ref->type = REF_ARRAY; 2108 e->ref->u.ar.type = AR_FULL; 2109 e->ref->u.ar.as = sym->ts.type == BT_CLASS 2110 ? CLASS_DATA (sym)->as : sym->as; 2111 } 2112 2113 /* Expressions are assigned a default ts.type of BT_PROCEDURE in 2114 primary.c (match_actual_arg). If above code determines that it 2115 is a variable instead, it needs to be resolved as it was not 2116 done at the beginning of this function. */ 2117 save_need_full_assumed_size = need_full_assumed_size; 2118 if (e->expr_type != EXPR_VARIABLE) 2119 need_full_assumed_size = 0; 2120 if (!gfc_resolve_expr (e)) 2121 goto cleanup; 2122 need_full_assumed_size = save_need_full_assumed_size; 2123 2124 argument_list: 2125 /* Check argument list functions %VAL, %LOC and %REF. There is 2126 nothing to do for %REF. */ 2127 if (arg->name && arg->name[0] == '%') 2128 { 2129 if (strcmp ("%VAL", arg->name) == 0) 2130 { 2131 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED) 2132 { 2133 gfc_error ("By-value argument at %L is not of numeric " 2134 "type", &e->where); 2135 goto cleanup; 2136 } 2137 2138 if (e->rank) 2139 { 2140 gfc_error ("By-value argument at %L cannot be an array or " 2141 "an array section", &e->where); 2142 goto cleanup; 2143 } 2144 2145 /* Intrinsics are still PROC_UNKNOWN here. However, 2146 since same file external procedures are not resolvable 2147 in gfortran, it is a good deal easier to leave them to 2148 intrinsic.c. */ 2149 if (ptype != PROC_UNKNOWN 2150 && ptype != PROC_DUMMY 2151 && ptype != PROC_EXTERNAL 2152 && ptype != PROC_MODULE) 2153 { 2154 gfc_error ("By-value argument at %L is not allowed " 2155 "in this context", &e->where); 2156 goto cleanup; 2157 } 2158 } 2159 2160 /* Statement functions have already been excluded above. */ 2161 else if (strcmp ("%LOC", arg->name) == 0 2162 && e->ts.type == BT_PROCEDURE) 2163 { 2164 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL) 2165 { 2166 gfc_error ("Passing internal procedure at %L by location " 2167 "not allowed", &e->where); 2168 goto cleanup; 2169 } 2170 } 2171 } 2172 2173 comp = gfc_get_proc_ptr_comp(e); 2174 if (e->expr_type == EXPR_VARIABLE 2175 && comp && comp->attr.elemental) 2176 { 2177 gfc_error ("ELEMENTAL procedure pointer component %qs is not " 2178 "allowed as an actual argument at %L", comp->name, 2179 &e->where); 2180 } 2181 2182 /* Fortran 2008, C1237. */ 2183 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e) 2184 && gfc_has_ultimate_pointer (e)) 2185 { 2186 gfc_error ("Coindexed actual argument at %L with ultimate pointer " 2187 "component", &e->where); 2188 goto cleanup; 2189 } 2190 2191 first_actual_arg = false; 2192 } 2193 2194 return_value = true; 2195 2196 cleanup: 2197 actual_arg = actual_arg_sav; 2198 first_actual_arg = first_actual_arg_sav; 2199 2200 return return_value; 2201 } 2202 2203 2204 /* Do the checks of the actual argument list that are specific to elemental 2205 procedures. If called with c == NULL, we have a function, otherwise if 2206 expr == NULL, we have a subroutine. */ 2207 2208 static bool 2209 resolve_elemental_actual (gfc_expr *expr, gfc_code *c) 2210 { 2211 gfc_actual_arglist *arg0; 2212 gfc_actual_arglist *arg; 2213 gfc_symbol *esym = NULL; 2214 gfc_intrinsic_sym *isym = NULL; 2215 gfc_expr *e = NULL; 2216 gfc_intrinsic_arg *iformal = NULL; 2217 gfc_formal_arglist *eformal = NULL; 2218 bool formal_optional = false; 2219 bool set_by_optional = false; 2220 int i; 2221 int rank = 0; 2222 2223 /* Is this an elemental procedure? */ 2224 if (expr && expr->value.function.actual != NULL) 2225 { 2226 if (expr->value.function.esym != NULL 2227 && expr->value.function.esym->attr.elemental) 2228 { 2229 arg0 = expr->value.function.actual; 2230 esym = expr->value.function.esym; 2231 } 2232 else if (expr->value.function.isym != NULL 2233 && expr->value.function.isym->elemental) 2234 { 2235 arg0 = expr->value.function.actual; 2236 isym = expr->value.function.isym; 2237 } 2238 else 2239 return true; 2240 } 2241 else if (c && c->ext.actual != NULL) 2242 { 2243 arg0 = c->ext.actual; 2244 2245 if (c->resolved_sym) 2246 esym = c->resolved_sym; 2247 else 2248 esym = c->symtree->n.sym; 2249 gcc_assert (esym); 2250 2251 if (!esym->attr.elemental) 2252 return true; 2253 } 2254 else 2255 return true; 2256 2257 /* The rank of an elemental is the rank of its array argument(s). */ 2258 for (arg = arg0; arg; arg = arg->next) 2259 { 2260 if (arg->expr != NULL && arg->expr->rank != 0) 2261 { 2262 rank = arg->expr->rank; 2263 if (arg->expr->expr_type == EXPR_VARIABLE 2264 && arg->expr->symtree->n.sym->attr.optional) 2265 set_by_optional = true; 2266 2267 /* Function specific; set the result rank and shape. */ 2268 if (expr) 2269 { 2270 expr->rank = rank; 2271 if (!expr->shape && arg->expr->shape) 2272 { 2273 expr->shape = gfc_get_shape (rank); 2274 for (i = 0; i < rank; i++) 2275 mpz_init_set (expr->shape[i], arg->expr->shape[i]); 2276 } 2277 } 2278 break; 2279 } 2280 } 2281 2282 /* If it is an array, it shall not be supplied as an actual argument 2283 to an elemental procedure unless an array of the same rank is supplied 2284 as an actual argument corresponding to a nonoptional dummy argument of 2285 that elemental procedure(12.4.1.5). */ 2286 formal_optional = false; 2287 if (isym) 2288 iformal = isym->formal; 2289 else 2290 eformal = esym->formal; 2291 2292 for (arg = arg0; arg; arg = arg->next) 2293 { 2294 if (eformal) 2295 { 2296 if (eformal->sym && eformal->sym->attr.optional) 2297 formal_optional = true; 2298 eformal = eformal->next; 2299 } 2300 else if (isym && iformal) 2301 { 2302 if (iformal->optional) 2303 formal_optional = true; 2304 iformal = iformal->next; 2305 } 2306 else if (isym) 2307 formal_optional = true; 2308 2309 if (pedantic && arg->expr != NULL 2310 && arg->expr->expr_type == EXPR_VARIABLE 2311 && arg->expr->symtree->n.sym->attr.optional 2312 && formal_optional 2313 && arg->expr->rank 2314 && (set_by_optional || arg->expr->rank != rank) 2315 && !(isym && isym->id == GFC_ISYM_CONVERSION)) 2316 { 2317 gfc_warning (OPT_Wpedantic, 2318 "%qs at %L is an array and OPTIONAL; IF IT IS " 2319 "MISSING, it cannot be the actual argument of an " 2320 "ELEMENTAL procedure unless there is a non-optional " 2321 "argument with the same rank (12.4.1.5)", 2322 arg->expr->symtree->n.sym->name, &arg->expr->where); 2323 } 2324 } 2325 2326 for (arg = arg0; arg; arg = arg->next) 2327 { 2328 if (arg->expr == NULL || arg->expr->rank == 0) 2329 continue; 2330 2331 /* Being elemental, the last upper bound of an assumed size array 2332 argument must be present. */ 2333 if (resolve_assumed_size_actual (arg->expr)) 2334 return false; 2335 2336 /* Elemental procedure's array actual arguments must conform. */ 2337 if (e != NULL) 2338 { 2339 if (!gfc_check_conformance (arg->expr, e, "elemental procedure")) 2340 return false; 2341 } 2342 else 2343 e = arg->expr; 2344 } 2345 2346 /* INTENT(OUT) is only allowed for subroutines; if any actual argument 2347 is an array, the intent inout/out variable needs to be also an array. */ 2348 if (rank > 0 && esym && expr == NULL) 2349 for (eformal = esym->formal, arg = arg0; arg && eformal; 2350 arg = arg->next, eformal = eformal->next) 2351 if ((eformal->sym->attr.intent == INTENT_OUT 2352 || eformal->sym->attr.intent == INTENT_INOUT) 2353 && arg->expr && arg->expr->rank == 0) 2354 { 2355 gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of " 2356 "ELEMENTAL subroutine %qs is a scalar, but another " 2357 "actual argument is an array", &arg->expr->where, 2358 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT" 2359 : "INOUT", eformal->sym->name, esym->name); 2360 return false; 2361 } 2362 return true; 2363 } 2364 2365 2366 /* This function does the checking of references to global procedures 2367 as defined in sections 18.1 and 14.1, respectively, of the Fortran 2368 77 and 95 standards. It checks for a gsymbol for the name, making 2369 one if it does not already exist. If it already exists, then the 2370 reference being resolved must correspond to the type of gsymbol. 2371 Otherwise, the new symbol is equipped with the attributes of the 2372 reference. The corresponding code that is called in creating 2373 global entities is parse.c. 2374 2375 In addition, for all but -std=legacy, the gsymbols are used to 2376 check the interfaces of external procedures from the same file. 2377 The namespace of the gsymbol is resolved and then, once this is 2378 done the interface is checked. */ 2379 2380 2381 static bool 2382 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns) 2383 { 2384 if (!gsym_ns->proc_name->attr.recursive) 2385 return true; 2386 2387 if (sym->ns == gsym_ns) 2388 return false; 2389 2390 if (sym->ns->parent && sym->ns->parent == gsym_ns) 2391 return false; 2392 2393 return true; 2394 } 2395 2396 static bool 2397 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns) 2398 { 2399 if (gsym_ns->entries) 2400 { 2401 gfc_entry_list *entry = gsym_ns->entries; 2402 2403 for (; entry; entry = entry->next) 2404 { 2405 if (strcmp (sym->name, entry->sym->name) == 0) 2406 { 2407 if (strcmp (gsym_ns->proc_name->name, 2408 sym->ns->proc_name->name) == 0) 2409 return false; 2410 2411 if (sym->ns->parent 2412 && strcmp (gsym_ns->proc_name->name, 2413 sym->ns->parent->proc_name->name) == 0) 2414 return false; 2415 } 2416 } 2417 } 2418 return true; 2419 } 2420 2421 2422 /* Check for the requirement of an explicit interface. F08:12.4.2.2. */ 2423 2424 bool 2425 gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len) 2426 { 2427 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym); 2428 2429 for ( ; arg; arg = arg->next) 2430 { 2431 if (!arg->sym) 2432 continue; 2433 2434 if (arg->sym->attr.allocatable) /* (2a) */ 2435 { 2436 strncpy (errmsg, _("allocatable argument"), err_len); 2437 return true; 2438 } 2439 else if (arg->sym->attr.asynchronous) 2440 { 2441 strncpy (errmsg, _("asynchronous argument"), err_len); 2442 return true; 2443 } 2444 else if (arg->sym->attr.optional) 2445 { 2446 strncpy (errmsg, _("optional argument"), err_len); 2447 return true; 2448 } 2449 else if (arg->sym->attr.pointer) 2450 { 2451 strncpy (errmsg, _("pointer argument"), err_len); 2452 return true; 2453 } 2454 else if (arg->sym->attr.target) 2455 { 2456 strncpy (errmsg, _("target argument"), err_len); 2457 return true; 2458 } 2459 else if (arg->sym->attr.value) 2460 { 2461 strncpy (errmsg, _("value argument"), err_len); 2462 return true; 2463 } 2464 else if (arg->sym->attr.volatile_) 2465 { 2466 strncpy (errmsg, _("volatile argument"), err_len); 2467 return true; 2468 } 2469 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */ 2470 { 2471 strncpy (errmsg, _("assumed-shape argument"), err_len); 2472 return true; 2473 } 2474 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */ 2475 { 2476 strncpy (errmsg, _("assumed-rank argument"), err_len); 2477 return true; 2478 } 2479 else if (arg->sym->attr.codimension) /* (2c) */ 2480 { 2481 strncpy (errmsg, _("coarray argument"), err_len); 2482 return true; 2483 } 2484 else if (false) /* (2d) TODO: parametrized derived type */ 2485 { 2486 strncpy (errmsg, _("parametrized derived type argument"), err_len); 2487 return true; 2488 } 2489 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */ 2490 { 2491 strncpy (errmsg, _("polymorphic argument"), err_len); 2492 return true; 2493 } 2494 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) 2495 { 2496 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len); 2497 return true; 2498 } 2499 else if (arg->sym->ts.type == BT_ASSUMED) 2500 { 2501 /* As assumed-type is unlimited polymorphic (cf. above). 2502 See also TS 29113, Note 6.1. */ 2503 strncpy (errmsg, _("assumed-type argument"), err_len); 2504 return true; 2505 } 2506 } 2507 2508 if (sym->attr.function) 2509 { 2510 gfc_symbol *res = sym->result ? sym->result : sym; 2511 2512 if (res->attr.dimension) /* (3a) */ 2513 { 2514 strncpy (errmsg, _("array result"), err_len); 2515 return true; 2516 } 2517 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */ 2518 { 2519 strncpy (errmsg, _("pointer or allocatable result"), err_len); 2520 return true; 2521 } 2522 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl 2523 && res->ts.u.cl->length 2524 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */ 2525 { 2526 strncpy (errmsg, _("result with non-constant character length"), err_len); 2527 return true; 2528 } 2529 } 2530 2531 if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */ 2532 { 2533 strncpy (errmsg, _("elemental procedure"), err_len); 2534 return true; 2535 } 2536 else if (sym->attr.is_bind_c) /* (5) */ 2537 { 2538 strncpy (errmsg, _("bind(c) procedure"), err_len); 2539 return true; 2540 } 2541 2542 return false; 2543 } 2544 2545 2546 static void 2547 resolve_global_procedure (gfc_symbol *sym, locus *where, int sub) 2548 { 2549 gfc_gsymbol * gsym; 2550 gfc_namespace *ns; 2551 enum gfc_symbol_type type; 2552 char reason[200]; 2553 2554 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION; 2555 2556 gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name, 2557 sym->binding_label != NULL); 2558 2559 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type)) 2560 gfc_global_used (gsym, where); 2561 2562 if ((sym->attr.if_source == IFSRC_UNKNOWN 2563 || sym->attr.if_source == IFSRC_IFBODY) 2564 && gsym->type != GSYM_UNKNOWN 2565 && !gsym->binding_label 2566 && gsym->ns 2567 && gsym->ns->proc_name 2568 && not_in_recursive (sym, gsym->ns) 2569 && not_entry_self_reference (sym, gsym->ns)) 2570 { 2571 gfc_symbol *def_sym; 2572 def_sym = gsym->ns->proc_name; 2573 2574 if (gsym->ns->resolved != -1) 2575 { 2576 2577 /* Resolve the gsymbol namespace if needed. */ 2578 if (!gsym->ns->resolved) 2579 { 2580 gfc_symbol *old_dt_list; 2581 2582 /* Stash away derived types so that the backend_decls 2583 do not get mixed up. */ 2584 old_dt_list = gfc_derived_types; 2585 gfc_derived_types = NULL; 2586 2587 gfc_resolve (gsym->ns); 2588 2589 /* Store the new derived types with the global namespace. */ 2590 if (gfc_derived_types) 2591 gsym->ns->derived_types = gfc_derived_types; 2592 2593 /* Restore the derived types of this namespace. */ 2594 gfc_derived_types = old_dt_list; 2595 } 2596 2597 /* Make sure that translation for the gsymbol occurs before 2598 the procedure currently being resolved. */ 2599 ns = gfc_global_ns_list; 2600 for (; ns && ns != gsym->ns; ns = ns->sibling) 2601 { 2602 if (ns->sibling == gsym->ns) 2603 { 2604 ns->sibling = gsym->ns->sibling; 2605 gsym->ns->sibling = gfc_global_ns_list; 2606 gfc_global_ns_list = gsym->ns; 2607 break; 2608 } 2609 } 2610 2611 /* This can happen if a binding name has been specified. */ 2612 if (gsym->binding_label && gsym->sym_name != def_sym->name) 2613 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym); 2614 2615 if (def_sym->attr.entry_master || def_sym->attr.entry) 2616 { 2617 gfc_entry_list *entry; 2618 for (entry = gsym->ns->entries; entry; entry = entry->next) 2619 if (strcmp (entry->sym->name, sym->name) == 0) 2620 { 2621 def_sym = entry->sym; 2622 break; 2623 } 2624 } 2625 } 2626 2627 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts)) 2628 { 2629 gfc_error ("Return type mismatch of function %qs at %L (%s/%s)", 2630 sym->name, &sym->declared_at, gfc_typename (&sym->ts), 2631 gfc_typename (&def_sym->ts)); 2632 goto done; 2633 } 2634 2635 if (sym->attr.if_source == IFSRC_UNKNOWN 2636 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason))) 2637 { 2638 gfc_error ("Explicit interface required for %qs at %L: %s", 2639 sym->name, &sym->declared_at, reason); 2640 goto done; 2641 } 2642 2643 bool bad_result_characteristics; 2644 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1, 2645 reason, sizeof(reason), NULL, NULL, 2646 &bad_result_characteristics)) 2647 { 2648 /* Turn erros into warnings with -std=gnu and -std=legacy, 2649 unless a function returns a wrong type, which can lead 2650 to all kinds of ICEs and wrong code. */ 2651 2652 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU) 2653 && !bad_result_characteristics) 2654 gfc_errors_to_warnings (true); 2655 2656 gfc_error ("Interface mismatch in global procedure %qs at %L: %s", 2657 sym->name, &sym->declared_at, reason); 2658 gfc_errors_to_warnings (false); 2659 goto done; 2660 } 2661 } 2662 2663 done: 2664 2665 if (gsym->type == GSYM_UNKNOWN) 2666 { 2667 gsym->type = type; 2668 gsym->where = *where; 2669 } 2670 2671 gsym->used = 1; 2672 } 2673 2674 2675 /************* Function resolution *************/ 2676 2677 /* Resolve a function call known to be generic. 2678 Section 14.1.2.4.1. */ 2679 2680 static match 2681 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym) 2682 { 2683 gfc_symbol *s; 2684 2685 if (sym->attr.generic) 2686 { 2687 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual); 2688 if (s != NULL) 2689 { 2690 expr->value.function.name = s->name; 2691 expr->value.function.esym = s; 2692 2693 if (s->ts.type != BT_UNKNOWN) 2694 expr->ts = s->ts; 2695 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN) 2696 expr->ts = s->result->ts; 2697 2698 if (s->as != NULL) 2699 expr->rank = s->as->rank; 2700 else if (s->result != NULL && s->result->as != NULL) 2701 expr->rank = s->result->as->rank; 2702 2703 gfc_set_sym_referenced (expr->value.function.esym); 2704 2705 return MATCH_YES; 2706 } 2707 2708 /* TODO: Need to search for elemental references in generic 2709 interface. */ 2710 } 2711 2712 if (sym->attr.intrinsic) 2713 return gfc_intrinsic_func_interface (expr, 0); 2714 2715 return MATCH_NO; 2716 } 2717 2718 2719 static bool 2720 resolve_generic_f (gfc_expr *expr) 2721 { 2722 gfc_symbol *sym; 2723 match m; 2724 gfc_interface *intr = NULL; 2725 2726 sym = expr->symtree->n.sym; 2727 2728 for (;;) 2729 { 2730 m = resolve_generic_f0 (expr, sym); 2731 if (m == MATCH_YES) 2732 return true; 2733 else if (m == MATCH_ERROR) 2734 return false; 2735 2736 generic: 2737 if (!intr) 2738 for (intr = sym->generic; intr; intr = intr->next) 2739 if (gfc_fl_struct (intr->sym->attr.flavor)) 2740 break; 2741 2742 if (sym->ns->parent == NULL) 2743 break; 2744 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym); 2745 2746 if (sym == NULL) 2747 break; 2748 if (!generic_sym (sym)) 2749 goto generic; 2750 } 2751 2752 /* Last ditch attempt. See if the reference is to an intrinsic 2753 that possesses a matching interface. 14.1.2.4 */ 2754 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where)) 2755 { 2756 if (gfc_init_expr_flag) 2757 gfc_error ("Function %qs in initialization expression at %L " 2758 "must be an intrinsic function", 2759 expr->symtree->n.sym->name, &expr->where); 2760 else 2761 gfc_error ("There is no specific function for the generic %qs " 2762 "at %L", expr->symtree->n.sym->name, &expr->where); 2763 return false; 2764 } 2765 2766 if (intr) 2767 { 2768 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL, 2769 NULL, false)) 2770 return false; 2771 if (!gfc_use_derived (expr->ts.u.derived)) 2772 return false; 2773 return resolve_structure_cons (expr, 0); 2774 } 2775 2776 m = gfc_intrinsic_func_interface (expr, 0); 2777 if (m == MATCH_YES) 2778 return true; 2779 2780 if (m == MATCH_NO) 2781 gfc_error ("Generic function %qs at %L is not consistent with a " 2782 "specific intrinsic interface", expr->symtree->n.sym->name, 2783 &expr->where); 2784 2785 return false; 2786 } 2787 2788 2789 /* Resolve a function call known to be specific. */ 2790 2791 static match 2792 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr) 2793 { 2794 match m; 2795 2796 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY) 2797 { 2798 if (sym->attr.dummy) 2799 { 2800 sym->attr.proc = PROC_DUMMY; 2801 goto found; 2802 } 2803 2804 sym->attr.proc = PROC_EXTERNAL; 2805 goto found; 2806 } 2807 2808 if (sym->attr.proc == PROC_MODULE 2809 || sym->attr.proc == PROC_ST_FUNCTION 2810 || sym->attr.proc == PROC_INTERNAL) 2811 goto found; 2812 2813 if (sym->attr.intrinsic) 2814 { 2815 m = gfc_intrinsic_func_interface (expr, 1); 2816 if (m == MATCH_YES) 2817 return MATCH_YES; 2818 if (m == MATCH_NO) 2819 gfc_error ("Function %qs at %L is INTRINSIC but is not compatible " 2820 "with an intrinsic", sym->name, &expr->where); 2821 2822 return MATCH_ERROR; 2823 } 2824 2825 return MATCH_NO; 2826 2827 found: 2828 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where); 2829 2830 if (sym->result) 2831 expr->ts = sym->result->ts; 2832 else 2833 expr->ts = sym->ts; 2834 expr->value.function.name = sym->name; 2835 expr->value.function.esym = sym; 2836 /* Prevent crash when sym->ts.u.derived->components is not set due to previous 2837 error(s). */ 2838 if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym)) 2839 return MATCH_ERROR; 2840 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as) 2841 expr->rank = CLASS_DATA (sym)->as->rank; 2842 else if (sym->as != NULL) 2843 expr->rank = sym->as->rank; 2844 2845 return MATCH_YES; 2846 } 2847 2848 2849 static bool 2850 resolve_specific_f (gfc_expr *expr) 2851 { 2852 gfc_symbol *sym; 2853 match m; 2854 2855 sym = expr->symtree->n.sym; 2856 2857 for (;;) 2858 { 2859 m = resolve_specific_f0 (sym, expr); 2860 if (m == MATCH_YES) 2861 return true; 2862 if (m == MATCH_ERROR) 2863 return false; 2864 2865 if (sym->ns->parent == NULL) 2866 break; 2867 2868 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym); 2869 2870 if (sym == NULL) 2871 break; 2872 } 2873 2874 gfc_error ("Unable to resolve the specific function %qs at %L", 2875 expr->symtree->n.sym->name, &expr->where); 2876 2877 return true; 2878 } 2879 2880 /* Recursively append candidate SYM to CANDIDATES. Store the number of 2881 candidates in CANDIDATES_LEN. */ 2882 2883 static void 2884 lookup_function_fuzzy_find_candidates (gfc_symtree *sym, 2885 char **&candidates, 2886 size_t &candidates_len) 2887 { 2888 gfc_symtree *p; 2889 2890 if (sym == NULL) 2891 return; 2892 if ((sym->n.sym->ts.type != BT_UNKNOWN || sym->n.sym->attr.external) 2893 && sym->n.sym->attr.flavor == FL_PROCEDURE) 2894 vec_push (candidates, candidates_len, sym->name); 2895 2896 p = sym->left; 2897 if (p) 2898 lookup_function_fuzzy_find_candidates (p, candidates, candidates_len); 2899 2900 p = sym->right; 2901 if (p) 2902 lookup_function_fuzzy_find_candidates (p, candidates, candidates_len); 2903 } 2904 2905 2906 /* Lookup function FN fuzzily, taking names in SYMROOT into account. */ 2907 2908 const char* 2909 gfc_lookup_function_fuzzy (const char *fn, gfc_symtree *symroot) 2910 { 2911 char **candidates = NULL; 2912 size_t candidates_len = 0; 2913 lookup_function_fuzzy_find_candidates (symroot, candidates, candidates_len); 2914 return gfc_closest_fuzzy_match (fn, candidates); 2915 } 2916 2917 2918 /* Resolve a procedure call not known to be generic nor specific. */ 2919 2920 static bool 2921 resolve_unknown_f (gfc_expr *expr) 2922 { 2923 gfc_symbol *sym; 2924 gfc_typespec *ts; 2925 2926 sym = expr->symtree->n.sym; 2927 2928 if (sym->attr.dummy) 2929 { 2930 sym->attr.proc = PROC_DUMMY; 2931 expr->value.function.name = sym->name; 2932 goto set_type; 2933 } 2934 2935 /* See if we have an intrinsic function reference. */ 2936 2937 if (gfc_is_intrinsic (sym, 0, expr->where)) 2938 { 2939 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES) 2940 return true; 2941 return false; 2942 } 2943 2944 /* The reference is to an external name. */ 2945 2946 sym->attr.proc = PROC_EXTERNAL; 2947 expr->value.function.name = sym->name; 2948 expr->value.function.esym = expr->symtree->n.sym; 2949 2950 if (sym->as != NULL) 2951 expr->rank = sym->as->rank; 2952 2953 /* Type of the expression is either the type of the symbol or the 2954 default type of the symbol. */ 2955 2956 set_type: 2957 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where); 2958 2959 if (sym->ts.type != BT_UNKNOWN) 2960 expr->ts = sym->ts; 2961 else 2962 { 2963 ts = gfc_get_default_type (sym->name, sym->ns); 2964 2965 if (ts->type == BT_UNKNOWN) 2966 { 2967 const char *guessed 2968 = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root); 2969 if (guessed) 2970 gfc_error ("Function %qs at %L has no IMPLICIT type" 2971 "; did you mean %qs?", 2972 sym->name, &expr->where, guessed); 2973 else 2974 gfc_error ("Function %qs at %L has no IMPLICIT type", 2975 sym->name, &expr->where); 2976 return false; 2977 } 2978 else 2979 expr->ts = *ts; 2980 } 2981 2982 return true; 2983 } 2984 2985 2986 /* Return true, if the symbol is an external procedure. */ 2987 static bool 2988 is_external_proc (gfc_symbol *sym) 2989 { 2990 if (!sym->attr.dummy && !sym->attr.contained 2991 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at) 2992 && sym->attr.proc != PROC_ST_FUNCTION 2993 && !sym->attr.proc_pointer 2994 && !sym->attr.use_assoc 2995 && sym->name) 2996 return true; 2997 2998 return false; 2999 } 3000 3001 3002 /* Figure out if a function reference is pure or not. Also set the name 3003 of the function for a potential error message. Return nonzero if the 3004 function is PURE, zero if not. */ 3005 static int 3006 pure_stmt_function (gfc_expr *, gfc_symbol *); 3007 3008 int 3009 gfc_pure_function (gfc_expr *e, const char **name) 3010 { 3011 int pure; 3012 gfc_component *comp; 3013 3014 *name = NULL; 3015 3016 if (e->symtree != NULL 3017 && e->symtree->n.sym != NULL 3018 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION) 3019 return pure_stmt_function (e, e->symtree->n.sym); 3020 3021 comp = gfc_get_proc_ptr_comp (e); 3022 if (comp) 3023 { 3024 pure = gfc_pure (comp->ts.interface); 3025 *name = comp->name; 3026 } 3027 else if (e->value.function.esym) 3028 { 3029 pure = gfc_pure (e->value.function.esym); 3030 *name = e->value.function.esym->name; 3031 } 3032 else if (e->value.function.isym) 3033 { 3034 pure = e->value.function.isym->pure 3035 || e->value.function.isym->elemental; 3036 *name = e->value.function.isym->name; 3037 } 3038 else 3039 { 3040 /* Implicit functions are not pure. */ 3041 pure = 0; 3042 *name = e->value.function.name; 3043 } 3044 3045 return pure; 3046 } 3047 3048 3049 /* Check if the expression is a reference to an implicitly pure function. */ 3050 3051 int 3052 gfc_implicit_pure_function (gfc_expr *e) 3053 { 3054 gfc_component *comp = gfc_get_proc_ptr_comp (e); 3055 if (comp) 3056 return gfc_implicit_pure (comp->ts.interface); 3057 else if (e->value.function.esym) 3058 return gfc_implicit_pure (e->value.function.esym); 3059 else 3060 return 0; 3061 } 3062 3063 3064 static bool 3065 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym, 3066 int *f ATTRIBUTE_UNUSED) 3067 { 3068 const char *name; 3069 3070 /* Don't bother recursing into other statement functions 3071 since they will be checked individually for purity. */ 3072 if (e->expr_type != EXPR_FUNCTION 3073 || !e->symtree 3074 || e->symtree->n.sym == sym 3075 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION) 3076 return false; 3077 3078 return gfc_pure_function (e, &name) ? false : true; 3079 } 3080 3081 3082 static int 3083 pure_stmt_function (gfc_expr *e, gfc_symbol *sym) 3084 { 3085 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1; 3086 } 3087 3088 3089 /* Check if an impure function is allowed in the current context. */ 3090 3091 static bool check_pure_function (gfc_expr *e) 3092 { 3093 const char *name = NULL; 3094 if (!gfc_pure_function (e, &name) && name) 3095 { 3096 if (forall_flag) 3097 { 3098 gfc_error ("Reference to impure function %qs at %L inside a " 3099 "FORALL %s", name, &e->where, 3100 forall_flag == 2 ? "mask" : "block"); 3101 return false; 3102 } 3103 else if (gfc_do_concurrent_flag) 3104 { 3105 gfc_error ("Reference to impure function %qs at %L inside a " 3106 "DO CONCURRENT %s", name, &e->where, 3107 gfc_do_concurrent_flag == 2 ? "mask" : "block"); 3108 return false; 3109 } 3110 else if (gfc_pure (NULL)) 3111 { 3112 gfc_error ("Reference to impure function %qs at %L " 3113 "within a PURE procedure", name, &e->where); 3114 return false; 3115 } 3116 if (!gfc_implicit_pure_function (e)) 3117 gfc_unset_implicit_pure (NULL); 3118 } 3119 return true; 3120 } 3121 3122 3123 /* Update current procedure's array_outer_dependency flag, considering 3124 a call to procedure SYM. */ 3125 3126 static void 3127 update_current_proc_array_outer_dependency (gfc_symbol *sym) 3128 { 3129 /* Check to see if this is a sibling function that has not yet 3130 been resolved. */ 3131 gfc_namespace *sibling = gfc_current_ns->sibling; 3132 for (; sibling; sibling = sibling->sibling) 3133 { 3134 if (sibling->proc_name == sym) 3135 { 3136 gfc_resolve (sibling); 3137 break; 3138 } 3139 } 3140 3141 /* If SYM has references to outer arrays, so has the procedure calling 3142 SYM. If SYM is a procedure pointer, we can assume the worst. */ 3143 if ((sym->attr.array_outer_dependency || sym->attr.proc_pointer) 3144 && gfc_current_ns->proc_name) 3145 gfc_current_ns->proc_name->attr.array_outer_dependency = 1; 3146 } 3147 3148 3149 /* Resolve a function call, which means resolving the arguments, then figuring 3150 out which entity the name refers to. */ 3151 3152 static bool 3153 resolve_function (gfc_expr *expr) 3154 { 3155 gfc_actual_arglist *arg; 3156 gfc_symbol *sym; 3157 bool t; 3158 int temp; 3159 procedure_type p = PROC_INTRINSIC; 3160 bool no_formal_args; 3161 3162 sym = NULL; 3163 if (expr->symtree) 3164 sym = expr->symtree->n.sym; 3165 3166 /* If this is a procedure pointer component, it has already been resolved. */ 3167 if (gfc_is_proc_ptr_comp (expr)) 3168 return true; 3169 3170 /* Avoid re-resolving the arguments of caf_get, which can lead to inserting 3171 another caf_get. */ 3172 if (sym && sym->attr.intrinsic 3173 && (sym->intmod_sym_id == GFC_ISYM_CAF_GET 3174 || sym->intmod_sym_id == GFC_ISYM_CAF_SEND)) 3175 return true; 3176 3177 if (expr->ref) 3178 { 3179 gfc_error ("Unexpected junk after %qs at %L", expr->symtree->n.sym->name, 3180 &expr->where); 3181 return false; 3182 } 3183 3184 if (sym && sym->attr.intrinsic 3185 && !gfc_resolve_intrinsic (sym, &expr->where)) 3186 return false; 3187 3188 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine)) 3189 { 3190 gfc_error ("%qs at %L is not a function", sym->name, &expr->where); 3191 return false; 3192 } 3193 3194 /* If this is a deferred TBP with an abstract interface (which may 3195 of course be referenced), expr->value.function.esym will be set. */ 3196 if (sym && sym->attr.abstract && !expr->value.function.esym) 3197 { 3198 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L", 3199 sym->name, &expr->where); 3200 return false; 3201 } 3202 3203 /* If this is a deferred TBP with an abstract interface, its result 3204 cannot be an assumed length character (F2003: C418). */ 3205 if (sym && sym->attr.abstract && sym->attr.function 3206 && sym->result->ts.u.cl 3207 && sym->result->ts.u.cl->length == NULL 3208 && !sym->result->ts.deferred) 3209 { 3210 gfc_error ("ABSTRACT INTERFACE %qs at %L must not have an assumed " 3211 "character length result (F2008: C418)", sym->name, 3212 &sym->declared_at); 3213 return false; 3214 } 3215 3216 /* Switch off assumed size checking and do this again for certain kinds 3217 of procedure, once the procedure itself is resolved. */ 3218 need_full_assumed_size++; 3219 3220 if (expr->symtree && expr->symtree->n.sym) 3221 p = expr->symtree->n.sym->attr.proc; 3222 3223 if (expr->value.function.isym && expr->value.function.isym->inquiry) 3224 inquiry_argument = true; 3225 no_formal_args = sym && is_external_proc (sym) 3226 && gfc_sym_get_dummy_args (sym) == NULL; 3227 3228 if (!resolve_actual_arglist (expr->value.function.actual, 3229 p, no_formal_args)) 3230 { 3231 inquiry_argument = false; 3232 return false; 3233 } 3234 3235 inquiry_argument = false; 3236 3237 /* Resume assumed_size checking. */ 3238 need_full_assumed_size--; 3239 3240 /* If the procedure is external, check for usage. */ 3241 if (sym && is_external_proc (sym)) 3242 resolve_global_procedure (sym, &expr->where, 0); 3243 3244 if (sym && sym->ts.type == BT_CHARACTER 3245 && sym->ts.u.cl 3246 && sym->ts.u.cl->length == NULL 3247 && !sym->attr.dummy 3248 && !sym->ts.deferred 3249 && expr->value.function.esym == NULL 3250 && !sym->attr.contained) 3251 { 3252 /* Internal procedures are taken care of in resolve_contained_fntype. */ 3253 gfc_error ("Function %qs is declared CHARACTER(*) and cannot " 3254 "be used at %L since it is not a dummy argument", 3255 sym->name, &expr->where); 3256 return false; 3257 } 3258 3259 /* See if function is already resolved. */ 3260 3261 if (expr->value.function.name != NULL 3262 || expr->value.function.isym != NULL) 3263 { 3264 if (expr->ts.type == BT_UNKNOWN) 3265 expr->ts = sym->ts; 3266 t = true; 3267 } 3268 else 3269 { 3270 /* Apply the rules of section 14.1.2. */ 3271 3272 switch (procedure_kind (sym)) 3273 { 3274 case PTYPE_GENERIC: 3275 t = resolve_generic_f (expr); 3276 break; 3277 3278 case PTYPE_SPECIFIC: 3279 t = resolve_specific_f (expr); 3280 break; 3281 3282 case PTYPE_UNKNOWN: 3283 t = resolve_unknown_f (expr); 3284 break; 3285 3286 default: 3287 gfc_internal_error ("resolve_function(): bad function type"); 3288 } 3289 } 3290 3291 /* If the expression is still a function (it might have simplified), 3292 then we check to see if we are calling an elemental function. */ 3293 3294 if (expr->expr_type != EXPR_FUNCTION) 3295 return t; 3296 3297 /* Walk the argument list looking for invalid BOZ. */ 3298 for (arg = expr->value.function.actual; arg; arg = arg->next) 3299 if (arg->expr && arg->expr->ts.type == BT_BOZ) 3300 { 3301 gfc_error ("A BOZ literal constant at %L cannot appear as an " 3302 "actual argument in a function reference", 3303 &arg->expr->where); 3304 return false; 3305 } 3306 3307 temp = need_full_assumed_size; 3308 need_full_assumed_size = 0; 3309 3310 if (!resolve_elemental_actual (expr, NULL)) 3311 return false; 3312 3313 if (omp_workshare_flag 3314 && expr->value.function.esym 3315 && ! gfc_elemental (expr->value.function.esym)) 3316 { 3317 gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed " 3318 "in WORKSHARE construct", expr->value.function.esym->name, 3319 &expr->where); 3320 t = false; 3321 } 3322 3323 #define GENERIC_ID expr->value.function.isym->id 3324 else if (expr->value.function.actual != NULL 3325 && expr->value.function.isym != NULL 3326 && GENERIC_ID != GFC_ISYM_LBOUND 3327 && GENERIC_ID != GFC_ISYM_LCOBOUND 3328 && GENERIC_ID != GFC_ISYM_UCOBOUND 3329 && GENERIC_ID != GFC_ISYM_LEN 3330 && GENERIC_ID != GFC_ISYM_LOC 3331 && GENERIC_ID != GFC_ISYM_C_LOC 3332 && GENERIC_ID != GFC_ISYM_PRESENT) 3333 { 3334 /* Array intrinsics must also have the last upper bound of an 3335 assumed size array argument. UBOUND and SIZE have to be 3336 excluded from the check if the second argument is anything 3337 than a constant. */ 3338 3339 for (arg = expr->value.function.actual; arg; arg = arg->next) 3340 { 3341 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE) 3342 && arg == expr->value.function.actual 3343 && arg->next != NULL && arg->next->expr) 3344 { 3345 if (arg->next->expr->expr_type != EXPR_CONSTANT) 3346 break; 3347 3348 if (arg->next->name && strcmp (arg->next->name, "kind") == 0) 3349 break; 3350 3351 if ((int)mpz_get_si (arg->next->expr->value.integer) 3352 < arg->expr->rank) 3353 break; 3354 } 3355 3356 if (arg->expr != NULL 3357 && arg->expr->rank > 0 3358 && resolve_assumed_size_actual (arg->expr)) 3359 return false; 3360 } 3361 } 3362 #undef GENERIC_ID 3363 3364 need_full_assumed_size = temp; 3365 3366 if (!check_pure_function(expr)) 3367 t = false; 3368 3369 /* Functions without the RECURSIVE attribution are not allowed to 3370 * call themselves. */ 3371 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive) 3372 { 3373 gfc_symbol *esym; 3374 esym = expr->value.function.esym; 3375 3376 if (is_illegal_recursion (esym, gfc_current_ns)) 3377 { 3378 if (esym->attr.entry && esym->ns->entries) 3379 gfc_error ("ENTRY %qs at %L cannot be called recursively, as" 3380 " function %qs is not RECURSIVE", 3381 esym->name, &expr->where, esym->ns->entries->sym->name); 3382 else 3383 gfc_error ("Function %qs at %L cannot be called recursively, as it" 3384 " is not RECURSIVE", esym->name, &expr->where); 3385 3386 t = false; 3387 } 3388 } 3389 3390 /* Character lengths of use associated functions may contains references to 3391 symbols not referenced from the current program unit otherwise. Make sure 3392 those symbols are marked as referenced. */ 3393 3394 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym 3395 && expr->value.function.esym->attr.use_assoc) 3396 { 3397 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length); 3398 } 3399 3400 /* Make sure that the expression has a typespec that works. */ 3401 if (expr->ts.type == BT_UNKNOWN) 3402 { 3403 if (expr->symtree->n.sym->result 3404 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN 3405 && !expr->symtree->n.sym->result->attr.proc_pointer) 3406 expr->ts = expr->symtree->n.sym->result->ts; 3407 } 3408 3409 if (!expr->ref && !expr->value.function.isym) 3410 { 3411 if (expr->value.function.esym) 3412 update_current_proc_array_outer_dependency (expr->value.function.esym); 3413 else 3414 update_current_proc_array_outer_dependency (sym); 3415 } 3416 else if (expr->ref) 3417 /* typebound procedure: Assume the worst. */ 3418 gfc_current_ns->proc_name->attr.array_outer_dependency = 1; 3419 3420 return t; 3421 } 3422 3423 3424 /************* Subroutine resolution *************/ 3425 3426 static bool 3427 pure_subroutine (gfc_symbol *sym, const char *name, locus *loc) 3428 { 3429 if (gfc_pure (sym)) 3430 return true; 3431 3432 if (forall_flag) 3433 { 3434 gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE", 3435 name, loc); 3436 return false; 3437 } 3438 else if (gfc_do_concurrent_flag) 3439 { 3440 gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not " 3441 "PURE", name, loc); 3442 return false; 3443 } 3444 else if (gfc_pure (NULL)) 3445 { 3446 gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc); 3447 return false; 3448 } 3449 3450 gfc_unset_implicit_pure (NULL); 3451 return true; 3452 } 3453 3454 3455 static match 3456 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym) 3457 { 3458 gfc_symbol *s; 3459 3460 if (sym->attr.generic) 3461 { 3462 s = gfc_search_interface (sym->generic, 1, &c->ext.actual); 3463 if (s != NULL) 3464 { 3465 c->resolved_sym = s; 3466 if (!pure_subroutine (s, s->name, &c->loc)) 3467 return MATCH_ERROR; 3468 return MATCH_YES; 3469 } 3470 3471 /* TODO: Need to search for elemental references in generic interface. */ 3472 } 3473 3474 if (sym->attr.intrinsic) 3475 return gfc_intrinsic_sub_interface (c, 0); 3476 3477 return MATCH_NO; 3478 } 3479 3480 3481 static bool 3482 resolve_generic_s (gfc_code *c) 3483 { 3484 gfc_symbol *sym; 3485 match m; 3486 3487 sym = c->symtree->n.sym; 3488 3489 for (;;) 3490 { 3491 m = resolve_generic_s0 (c, sym); 3492 if (m == MATCH_YES) 3493 return true; 3494 else if (m == MATCH_ERROR) 3495 return false; 3496 3497 generic: 3498 if (sym->ns->parent == NULL) 3499 break; 3500 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym); 3501 3502 if (sym == NULL) 3503 break; 3504 if (!generic_sym (sym)) 3505 goto generic; 3506 } 3507 3508 /* Last ditch attempt. See if the reference is to an intrinsic 3509 that possesses a matching interface. 14.1.2.4 */ 3510 sym = c->symtree->n.sym; 3511 3512 if (!gfc_is_intrinsic (sym, 1, c->loc)) 3513 { 3514 gfc_error ("There is no specific subroutine for the generic %qs at %L", 3515 sym->name, &c->loc); 3516 return false; 3517 } 3518 3519 m = gfc_intrinsic_sub_interface (c, 0); 3520 if (m == MATCH_YES) 3521 return true; 3522 if (m == MATCH_NO) 3523 gfc_error ("Generic subroutine %qs at %L is not consistent with an " 3524 "intrinsic subroutine interface", sym->name, &c->loc); 3525 3526 return false; 3527 } 3528 3529 3530 /* Resolve a subroutine call known to be specific. */ 3531 3532 static match 3533 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym) 3534 { 3535 match m; 3536 3537 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY) 3538 { 3539 if (sym->attr.dummy) 3540 { 3541 sym->attr.proc = PROC_DUMMY; 3542 goto found; 3543 } 3544 3545 sym->attr.proc = PROC_EXTERNAL; 3546 goto found; 3547 } 3548 3549 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL) 3550 goto found; 3551 3552 if (sym->attr.intrinsic) 3553 { 3554 m = gfc_intrinsic_sub_interface (c, 1); 3555 if (m == MATCH_YES) 3556 return MATCH_YES; 3557 if (m == MATCH_NO) 3558 gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible " 3559 "with an intrinsic", sym->name, &c->loc); 3560 3561 return MATCH_ERROR; 3562 } 3563 3564 return MATCH_NO; 3565 3566 found: 3567 gfc_procedure_use (sym, &c->ext.actual, &c->loc); 3568 3569 c->resolved_sym = sym; 3570 if (!pure_subroutine (sym, sym->name, &c->loc)) 3571 return MATCH_ERROR; 3572 3573 return MATCH_YES; 3574 } 3575 3576 3577 static bool 3578 resolve_specific_s (gfc_code *c) 3579 { 3580 gfc_symbol *sym; 3581 match m; 3582 3583 sym = c->symtree->n.sym; 3584 3585 for (;;) 3586 { 3587 m = resolve_specific_s0 (c, sym); 3588 if (m == MATCH_YES) 3589 return true; 3590 if (m == MATCH_ERROR) 3591 return false; 3592 3593 if (sym->ns->parent == NULL) 3594 break; 3595 3596 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym); 3597 3598 if (sym == NULL) 3599 break; 3600 } 3601 3602 sym = c->symtree->n.sym; 3603 gfc_error ("Unable to resolve the specific subroutine %qs at %L", 3604 sym->name, &c->loc); 3605 3606 return false; 3607 } 3608 3609 3610 /* Resolve a subroutine call not known to be generic nor specific. */ 3611 3612 static bool 3613 resolve_unknown_s (gfc_code *c) 3614 { 3615 gfc_symbol *sym; 3616 3617 sym = c->symtree->n.sym; 3618 3619 if (sym->attr.dummy) 3620 { 3621 sym->attr.proc = PROC_DUMMY; 3622 goto found; 3623 } 3624 3625 /* See if we have an intrinsic function reference. */ 3626 3627 if (gfc_is_intrinsic (sym, 1, c->loc)) 3628 { 3629 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES) 3630 return true; 3631 return false; 3632 } 3633 3634 /* The reference is to an external name. */ 3635 3636 found: 3637 gfc_procedure_use (sym, &c->ext.actual, &c->loc); 3638 3639 c->resolved_sym = sym; 3640 3641 return pure_subroutine (sym, sym->name, &c->loc); 3642 } 3643 3644 3645 /* Resolve a subroutine call. Although it was tempting to use the same code 3646 for functions, subroutines and functions are stored differently and this 3647 makes things awkward. */ 3648 3649 static bool 3650 resolve_call (gfc_code *c) 3651 { 3652 bool t; 3653 procedure_type ptype = PROC_INTRINSIC; 3654 gfc_symbol *csym, *sym; 3655 bool no_formal_args; 3656 3657 csym = c->symtree ? c->symtree->n.sym : NULL; 3658 3659 if (csym && csym->ts.type != BT_UNKNOWN) 3660 { 3661 gfc_error ("%qs at %L has a type, which is not consistent with " 3662 "the CALL at %L", csym->name, &csym->declared_at, &c->loc); 3663 return false; 3664 } 3665 3666 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns) 3667 { 3668 gfc_symtree *st; 3669 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st); 3670 sym = st ? st->n.sym : NULL; 3671 if (sym && csym != sym 3672 && sym->ns == gfc_current_ns 3673 && sym->attr.flavor == FL_PROCEDURE 3674 && sym->attr.contained) 3675 { 3676 sym->refs++; 3677 if (csym->attr.generic) 3678 c->symtree->n.sym = sym; 3679 else 3680 c->symtree = st; 3681 csym = c->symtree->n.sym; 3682 } 3683 } 3684 3685 /* If this ia a deferred TBP, c->expr1 will be set. */ 3686 if (!c->expr1 && csym) 3687 { 3688 if (csym->attr.abstract) 3689 { 3690 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L", 3691 csym->name, &c->loc); 3692 return false; 3693 } 3694 3695 /* Subroutines without the RECURSIVE attribution are not allowed to 3696 call themselves. */ 3697 if (is_illegal_recursion (csym, gfc_current_ns)) 3698 { 3699 if (csym->attr.entry && csym->ns->entries) 3700 gfc_error ("ENTRY %qs at %L cannot be called recursively, " 3701 "as subroutine %qs is not RECURSIVE", 3702 csym->name, &c->loc, csym->ns->entries->sym->name); 3703 else 3704 gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, " 3705 "as it is not RECURSIVE", csym->name, &c->loc); 3706 3707 t = false; 3708 } 3709 } 3710 3711 /* Switch off assumed size checking and do this again for certain kinds 3712 of procedure, once the procedure itself is resolved. */ 3713 need_full_assumed_size++; 3714 3715 if (csym) 3716 ptype = csym->attr.proc; 3717 3718 no_formal_args = csym && is_external_proc (csym) 3719 && gfc_sym_get_dummy_args (csym) == NULL; 3720 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args)) 3721 return false; 3722 3723 /* Resume assumed_size checking. */ 3724 need_full_assumed_size--; 3725 3726 /* If external, check for usage. */ 3727 if (csym && is_external_proc (csym)) 3728 resolve_global_procedure (csym, &c->loc, 1); 3729 3730 t = true; 3731 if (c->resolved_sym == NULL) 3732 { 3733 c->resolved_isym = NULL; 3734 switch (procedure_kind (csym)) 3735 { 3736 case PTYPE_GENERIC: 3737 t = resolve_generic_s (c); 3738 break; 3739 3740 case PTYPE_SPECIFIC: 3741 t = resolve_specific_s (c); 3742 break; 3743 3744 case PTYPE_UNKNOWN: 3745 t = resolve_unknown_s (c); 3746 break; 3747 3748 default: 3749 gfc_internal_error ("resolve_subroutine(): bad function type"); 3750 } 3751 } 3752 3753 /* Some checks of elemental subroutine actual arguments. */ 3754 if (!resolve_elemental_actual (NULL, c)) 3755 return false; 3756 3757 if (!c->expr1) 3758 update_current_proc_array_outer_dependency (csym); 3759 else 3760 /* Typebound procedure: Assume the worst. */ 3761 gfc_current_ns->proc_name->attr.array_outer_dependency = 1; 3762 3763 return t; 3764 } 3765 3766 3767 /* Compare the shapes of two arrays that have non-NULL shapes. If both 3768 op1->shape and op2->shape are non-NULL return true if their shapes 3769 match. If both op1->shape and op2->shape are non-NULL return false 3770 if their shapes do not match. If either op1->shape or op2->shape is 3771 NULL, return true. */ 3772 3773 static bool 3774 compare_shapes (gfc_expr *op1, gfc_expr *op2) 3775 { 3776 bool t; 3777 int i; 3778 3779 t = true; 3780 3781 if (op1->shape != NULL && op2->shape != NULL) 3782 { 3783 for (i = 0; i < op1->rank; i++) 3784 { 3785 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0) 3786 { 3787 gfc_error ("Shapes for operands at %L and %L are not conformable", 3788 &op1->where, &op2->where); 3789 t = false; 3790 break; 3791 } 3792 } 3793 } 3794 3795 return t; 3796 } 3797 3798 /* Convert a logical operator to the corresponding bitwise intrinsic call. 3799 For example A .AND. B becomes IAND(A, B). */ 3800 static gfc_expr * 3801 logical_to_bitwise (gfc_expr *e) 3802 { 3803 gfc_expr *tmp, *op1, *op2; 3804 gfc_isym_id isym; 3805 gfc_actual_arglist *args = NULL; 3806 3807 gcc_assert (e->expr_type == EXPR_OP); 3808 3809 isym = GFC_ISYM_NONE; 3810 op1 = e->value.op.op1; 3811 op2 = e->value.op.op2; 3812 3813 switch (e->value.op.op) 3814 { 3815 case INTRINSIC_NOT: 3816 isym = GFC_ISYM_NOT; 3817 break; 3818 case INTRINSIC_AND: 3819 isym = GFC_ISYM_IAND; 3820 break; 3821 case INTRINSIC_OR: 3822 isym = GFC_ISYM_IOR; 3823 break; 3824 case INTRINSIC_NEQV: 3825 isym = GFC_ISYM_IEOR; 3826 break; 3827 case INTRINSIC_EQV: 3828 /* "Bitwise eqv" is just the complement of NEQV === IEOR. 3829 Change the old expression to NEQV, which will get replaced by IEOR, 3830 and wrap it in NOT. */ 3831 tmp = gfc_copy_expr (e); 3832 tmp->value.op.op = INTRINSIC_NEQV; 3833 tmp = logical_to_bitwise (tmp); 3834 isym = GFC_ISYM_NOT; 3835 op1 = tmp; 3836 op2 = NULL; 3837 break; 3838 default: 3839 gfc_internal_error ("logical_to_bitwise(): Bad intrinsic"); 3840 } 3841 3842 /* Inherit the original operation's operands as arguments. */ 3843 args = gfc_get_actual_arglist (); 3844 args->expr = op1; 3845 if (op2) 3846 { 3847 args->next = gfc_get_actual_arglist (); 3848 args->next->expr = op2; 3849 } 3850 3851 /* Convert the expression to a function call. */ 3852 e->expr_type = EXPR_FUNCTION; 3853 e->value.function.actual = args; 3854 e->value.function.isym = gfc_intrinsic_function_by_id (isym); 3855 e->value.function.name = e->value.function.isym->name; 3856 e->value.function.esym = NULL; 3857 3858 /* Make up a pre-resolved function call symtree if we need to. */ 3859 if (!e->symtree || !e->symtree->n.sym) 3860 { 3861 gfc_symbol *sym; 3862 gfc_get_ha_sym_tree (e->value.function.isym->name, &e->symtree); 3863 sym = e->symtree->n.sym; 3864 sym->result = sym; 3865 sym->attr.flavor = FL_PROCEDURE; 3866 sym->attr.function = 1; 3867 sym->attr.elemental = 1; 3868 sym->attr.pure = 1; 3869 sym->attr.referenced = 1; 3870 gfc_intrinsic_symbol (sym); 3871 gfc_commit_symbol (sym); 3872 } 3873 3874 args->name = e->value.function.isym->formal->name; 3875 if (e->value.function.isym->formal->next) 3876 args->next->name = e->value.function.isym->formal->next->name; 3877 3878 return e; 3879 } 3880 3881 /* Recursively append candidate UOP to CANDIDATES. Store the number of 3882 candidates in CANDIDATES_LEN. */ 3883 static void 3884 lookup_uop_fuzzy_find_candidates (gfc_symtree *uop, 3885 char **&candidates, 3886 size_t &candidates_len) 3887 { 3888 gfc_symtree *p; 3889 3890 if (uop == NULL) 3891 return; 3892 3893 /* Not sure how to properly filter here. Use all for a start. 3894 n.uop.op is NULL for empty interface operators (is that legal?) disregard 3895 these as i suppose they don't make terribly sense. */ 3896 3897 if (uop->n.uop->op != NULL) 3898 vec_push (candidates, candidates_len, uop->name); 3899 3900 p = uop->left; 3901 if (p) 3902 lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len); 3903 3904 p = uop->right; 3905 if (p) 3906 lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len); 3907 } 3908 3909 /* Lookup user-operator OP fuzzily, taking names in UOP into account. */ 3910 3911 static const char* 3912 lookup_uop_fuzzy (const char *op, gfc_symtree *uop) 3913 { 3914 char **candidates = NULL; 3915 size_t candidates_len = 0; 3916 lookup_uop_fuzzy_find_candidates (uop, candidates, candidates_len); 3917 return gfc_closest_fuzzy_match (op, candidates); 3918 } 3919 3920 3921 /* Callback finding an impure function as an operand to an .and. or 3922 .or. expression. Remember the last function warned about to 3923 avoid double warnings when recursing. */ 3924 3925 static int 3926 impure_function_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED, 3927 void *data) 3928 { 3929 gfc_expr *f = *e; 3930 const char *name; 3931 static gfc_expr *last = NULL; 3932 bool *found = (bool *) data; 3933 3934 if (f->expr_type == EXPR_FUNCTION) 3935 { 3936 *found = 1; 3937 if (f != last && !gfc_pure_function (f, &name) 3938 && !gfc_implicit_pure_function (f)) 3939 { 3940 if (name) 3941 gfc_warning (OPT_Wfunction_elimination, 3942 "Impure function %qs at %L might not be evaluated", 3943 name, &f->where); 3944 else 3945 gfc_warning (OPT_Wfunction_elimination, 3946 "Impure function at %L might not be evaluated", 3947 &f->where); 3948 } 3949 last = f; 3950 } 3951 3952 return 0; 3953 } 3954 3955 /* Return true if TYPE is character based, false otherwise. */ 3956 3957 static int 3958 is_character_based (bt type) 3959 { 3960 return type == BT_CHARACTER || type == BT_HOLLERITH; 3961 } 3962 3963 3964 /* If expression is a hollerith, convert it to character and issue a warning 3965 for the conversion. */ 3966 3967 static void 3968 convert_hollerith_to_character (gfc_expr *e) 3969 { 3970 if (e->ts.type == BT_HOLLERITH) 3971 { 3972 gfc_typespec t; 3973 gfc_clear_ts (&t); 3974 t.type = BT_CHARACTER; 3975 t.kind = e->ts.kind; 3976 gfc_convert_type_warn (e, &t, 2, 1); 3977 } 3978 } 3979 3980 /* Convert to numeric and issue a warning for the conversion. */ 3981 3982 static void 3983 convert_to_numeric (gfc_expr *a, gfc_expr *b) 3984 { 3985 gfc_typespec t; 3986 gfc_clear_ts (&t); 3987 t.type = b->ts.type; 3988 t.kind = b->ts.kind; 3989 gfc_convert_type_warn (a, &t, 2, 1); 3990 } 3991 3992 /* Resolve an operator expression node. This can involve replacing the 3993 operation with a user defined function call. */ 3994 3995 static bool 3996 resolve_operator (gfc_expr *e) 3997 { 3998 gfc_expr *op1, *op2; 3999 /* One error uses 3 names; additional space for wording (also via gettext). */ 4000 char msg[3*GFC_MAX_SYMBOL_LEN + 1 + 50]; 4001 bool dual_locus_error; 4002 bool t = true; 4003 4004 /* Resolve all subnodes-- give them types. */ 4005 4006 switch (e->value.op.op) 4007 { 4008 default: 4009 if (!gfc_resolve_expr (e->value.op.op2)) 4010 return false; 4011 4012 /* Fall through. */ 4013 4014 case INTRINSIC_NOT: 4015 case INTRINSIC_UPLUS: 4016 case INTRINSIC_UMINUS: 4017 case INTRINSIC_PARENTHESES: 4018 if (!gfc_resolve_expr (e->value.op.op1)) 4019 return false; 4020 if (e->value.op.op1 4021 && e->value.op.op1->ts.type == BT_BOZ && !e->value.op.op2) 4022 { 4023 gfc_error ("BOZ literal constant at %L cannot be an operand of " 4024 "unary operator %qs", &e->value.op.op1->where, 4025 gfc_op2string (e->value.op.op)); 4026 return false; 4027 } 4028 break; 4029 } 4030 4031 /* Typecheck the new node. */ 4032 4033 op1 = e->value.op.op1; 4034 op2 = e->value.op.op2; 4035 if (op1 == NULL && op2 == NULL) 4036 return false; 4037 4038 dual_locus_error = false; 4039 4040 /* op1 and op2 cannot both be BOZ. */ 4041 if (op1 && op1->ts.type == BT_BOZ 4042 && op2 && op2->ts.type == BT_BOZ) 4043 { 4044 gfc_error ("Operands at %L and %L cannot appear as operands of " 4045 "binary operator %qs", &op1->where, &op2->where, 4046 gfc_op2string (e->value.op.op)); 4047 return false; 4048 } 4049 4050 if ((op1 && op1->expr_type == EXPR_NULL) 4051 || (op2 && op2->expr_type == EXPR_NULL)) 4052 { 4053 snprintf (msg, sizeof (msg), 4054 _("Invalid context for NULL() pointer at %%L")); 4055 goto bad_op; 4056 } 4057 4058 switch (e->value.op.op) 4059 { 4060 case INTRINSIC_UPLUS: 4061 case INTRINSIC_UMINUS: 4062 if (op1->ts.type == BT_INTEGER 4063 || op1->ts.type == BT_REAL 4064 || op1->ts.type == BT_COMPLEX) 4065 { 4066 e->ts = op1->ts; 4067 break; 4068 } 4069 4070 snprintf (msg, sizeof (msg), 4071 _("Operand of unary numeric operator %%<%s%%> at %%L is %s"), 4072 gfc_op2string (e->value.op.op), gfc_typename (e)); 4073 goto bad_op; 4074 4075 case INTRINSIC_PLUS: 4076 case INTRINSIC_MINUS: 4077 case INTRINSIC_TIMES: 4078 case INTRINSIC_DIVIDE: 4079 case INTRINSIC_POWER: 4080 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts)) 4081 { 4082 gfc_type_convert_binary (e, 1); 4083 break; 4084 } 4085 4086 if (op1->ts.type == BT_DERIVED || op2->ts.type == BT_DERIVED) 4087 snprintf (msg, sizeof (msg), 4088 _("Unexpected derived-type entities in binary intrinsic " 4089 "numeric operator %%<%s%%> at %%L"), 4090 gfc_op2string (e->value.op.op)); 4091 else 4092 snprintf (msg, sizeof(msg), 4093 _("Operands of binary numeric operator %%<%s%%> at %%L are %s/%s"), 4094 gfc_op2string (e->value.op.op), gfc_typename (op1), 4095 gfc_typename (op2)); 4096 goto bad_op; 4097 4098 case INTRINSIC_CONCAT: 4099 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER 4100 && op1->ts.kind == op2->ts.kind) 4101 { 4102 e->ts.type = BT_CHARACTER; 4103 e->ts.kind = op1->ts.kind; 4104 break; 4105 } 4106 4107 snprintf (msg, sizeof (msg), 4108 _("Operands of string concatenation operator at %%L are %s/%s"), 4109 gfc_typename (op1), gfc_typename (op2)); 4110 goto bad_op; 4111 4112 case INTRINSIC_AND: 4113 case INTRINSIC_OR: 4114 case INTRINSIC_EQV: 4115 case INTRINSIC_NEQV: 4116 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL) 4117 { 4118 e->ts.type = BT_LOGICAL; 4119 e->ts.kind = gfc_kind_max (op1, op2); 4120 if (op1->ts.kind < e->ts.kind) 4121 gfc_convert_type (op1, &e->ts, 2); 4122 else if (op2->ts.kind < e->ts.kind) 4123 gfc_convert_type (op2, &e->ts, 2); 4124 4125 if (flag_frontend_optimize && 4126 (e->value.op.op == INTRINSIC_AND || e->value.op.op == INTRINSIC_OR)) 4127 { 4128 /* Warn about short-circuiting 4129 with impure function as second operand. */ 4130 bool op2_f = false; 4131 gfc_expr_walker (&op2, impure_function_callback, &op2_f); 4132 } 4133 break; 4134 } 4135 4136 /* Logical ops on integers become bitwise ops with -fdec. */ 4137 else if (flag_dec 4138 && (op1->ts.type == BT_INTEGER || op2->ts.type == BT_INTEGER)) 4139 { 4140 e->ts.type = BT_INTEGER; 4141 e->ts.kind = gfc_kind_max (op1, op2); 4142 if (op1->ts.type != e->ts.type || op1->ts.kind != e->ts.kind) 4143 gfc_convert_type (op1, &e->ts, 1); 4144 if (op2->ts.type != e->ts.type || op2->ts.kind != e->ts.kind) 4145 gfc_convert_type (op2, &e->ts, 1); 4146 e = logical_to_bitwise (e); 4147 goto simplify_op; 4148 } 4149 4150 snprintf (msg, sizeof (msg), 4151 _("Operands of logical operator %%<%s%%> at %%L are %s/%s"), 4152 gfc_op2string (e->value.op.op), gfc_typename (op1), 4153 gfc_typename (op2)); 4154 4155 goto bad_op; 4156 4157 case INTRINSIC_NOT: 4158 /* Logical ops on integers become bitwise ops with -fdec. */ 4159 if (flag_dec && op1->ts.type == BT_INTEGER) 4160 { 4161 e->ts.type = BT_INTEGER; 4162 e->ts.kind = op1->ts.kind; 4163 e = logical_to_bitwise (e); 4164 goto simplify_op; 4165 } 4166 4167 if (op1->ts.type == BT_LOGICAL) 4168 { 4169 e->ts.type = BT_LOGICAL; 4170 e->ts.kind = op1->ts.kind; 4171 break; 4172 } 4173 4174 snprintf (msg, sizeof (msg), _("Operand of .not. operator at %%L is %s"), 4175 gfc_typename (op1)); 4176 goto bad_op; 4177 4178 case INTRINSIC_GT: 4179 case INTRINSIC_GT_OS: 4180 case INTRINSIC_GE: 4181 case INTRINSIC_GE_OS: 4182 case INTRINSIC_LT: 4183 case INTRINSIC_LT_OS: 4184 case INTRINSIC_LE: 4185 case INTRINSIC_LE_OS: 4186 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX) 4187 { 4188 strcpy (msg, _("COMPLEX quantities cannot be compared at %L")); 4189 goto bad_op; 4190 } 4191 4192 /* Fall through. */ 4193 4194 case INTRINSIC_EQ: 4195 case INTRINSIC_EQ_OS: 4196 case INTRINSIC_NE: 4197 case INTRINSIC_NE_OS: 4198 4199 if (flag_dec 4200 && is_character_based (op1->ts.type) 4201 && is_character_based (op2->ts.type)) 4202 { 4203 convert_hollerith_to_character (op1); 4204 convert_hollerith_to_character (op2); 4205 } 4206 4207 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER 4208 && op1->ts.kind == op2->ts.kind) 4209 { 4210 e->ts.type = BT_LOGICAL; 4211 e->ts.kind = gfc_default_logical_kind; 4212 break; 4213 } 4214 4215 /* If op1 is BOZ, then op2 is not!. Try to convert to type of op2. */ 4216 if (op1->ts.type == BT_BOZ) 4217 { 4218 if (gfc_invalid_boz ("BOZ literal constant near %L cannot appear as " 4219 "an operand of a relational operator", 4220 &op1->where)) 4221 return false; 4222 4223 if (op2->ts.type == BT_INTEGER && !gfc_boz2int (op1, op2->ts.kind)) 4224 return false; 4225 4226 if (op2->ts.type == BT_REAL && !gfc_boz2real (op1, op2->ts.kind)) 4227 return false; 4228 } 4229 4230 /* If op2 is BOZ, then op1 is not!. Try to convert to type of op2. */ 4231 if (op2->ts.type == BT_BOZ) 4232 { 4233 if (gfc_invalid_boz ("BOZ literal constant near %L cannot appear as " 4234 "an operand of a relational operator", 4235 &op2->where)) 4236 return false; 4237 4238 if (op1->ts.type == BT_INTEGER && !gfc_boz2int (op2, op1->ts.kind)) 4239 return false; 4240 4241 if (op1->ts.type == BT_REAL && !gfc_boz2real (op2, op1->ts.kind)) 4242 return false; 4243 } 4244 if (flag_dec 4245 && op1->ts.type == BT_HOLLERITH && gfc_numeric_ts (&op2->ts)) 4246 convert_to_numeric (op1, op2); 4247 4248 if (flag_dec 4249 && gfc_numeric_ts (&op1->ts) && op2->ts.type == BT_HOLLERITH) 4250 convert_to_numeric (op2, op1); 4251 4252 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts)) 4253 { 4254 gfc_type_convert_binary (e, 1); 4255 4256 e->ts.type = BT_LOGICAL; 4257 e->ts.kind = gfc_default_logical_kind; 4258 4259 if (warn_compare_reals) 4260 { 4261 gfc_intrinsic_op op = e->value.op.op; 4262 4263 /* Type conversion has made sure that the types of op1 and op2 4264 agree, so it is only necessary to check the first one. */ 4265 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX) 4266 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS 4267 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS)) 4268 { 4269 const char *msg; 4270 4271 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS) 4272 msg = "Equality comparison for %s at %L"; 4273 else 4274 msg = "Inequality comparison for %s at %L"; 4275 4276 gfc_warning (OPT_Wcompare_reals, msg, 4277 gfc_typename (op1), &op1->where); 4278 } 4279 } 4280 4281 break; 4282 } 4283 4284 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL) 4285 snprintf (msg, sizeof (msg), 4286 _("Logicals at %%L must be compared with %s instead of %s"), 4287 (e->value.op.op == INTRINSIC_EQ 4288 || e->value.op.op == INTRINSIC_EQ_OS) 4289 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op)); 4290 else 4291 snprintf (msg, sizeof (msg), 4292 _("Operands of comparison operator %%<%s%%> at %%L are %s/%s"), 4293 gfc_op2string (e->value.op.op), gfc_typename (op1), 4294 gfc_typename (op2)); 4295 4296 goto bad_op; 4297 4298 case INTRINSIC_USER: 4299 if (e->value.op.uop->op == NULL) 4300 { 4301 const char *name = e->value.op.uop->name; 4302 const char *guessed; 4303 guessed = lookup_uop_fuzzy (name, e->value.op.uop->ns->uop_root); 4304 if (guessed) 4305 snprintf (msg, sizeof (msg), 4306 _("Unknown operator %%<%s%%> at %%L; did you mean '%s'?"), 4307 name, guessed); 4308 else 4309 snprintf (msg, sizeof (msg), _("Unknown operator %%<%s%%> at %%L"), 4310 name); 4311 } 4312 else if (op2 == NULL) 4313 snprintf (msg, sizeof (msg), 4314 _("Operand of user operator %%<%s%%> at %%L is %s"), 4315 e->value.op.uop->name, gfc_typename (op1)); 4316 else 4317 { 4318 snprintf (msg, sizeof (msg), 4319 _("Operands of user operator %%<%s%%> at %%L are %s/%s"), 4320 e->value.op.uop->name, gfc_typename (op1), 4321 gfc_typename (op2)); 4322 e->value.op.uop->op->sym->attr.referenced = 1; 4323 } 4324 4325 goto bad_op; 4326 4327 case INTRINSIC_PARENTHESES: 4328 e->ts = op1->ts; 4329 if (e->ts.type == BT_CHARACTER) 4330 e->ts.u.cl = op1->ts.u.cl; 4331 break; 4332 4333 default: 4334 gfc_internal_error ("resolve_operator(): Bad intrinsic"); 4335 } 4336 4337 /* Deal with arrayness of an operand through an operator. */ 4338 4339 switch (e->value.op.op) 4340 { 4341 case INTRINSIC_PLUS: 4342 case INTRINSIC_MINUS: 4343 case INTRINSIC_TIMES: 4344 case INTRINSIC_DIVIDE: 4345 case INTRINSIC_POWER: 4346 case INTRINSIC_CONCAT: 4347 case INTRINSIC_AND: 4348 case INTRINSIC_OR: 4349 case INTRINSIC_EQV: 4350 case INTRINSIC_NEQV: 4351 case INTRINSIC_EQ: 4352 case INTRINSIC_EQ_OS: 4353 case INTRINSIC_NE: 4354 case INTRINSIC_NE_OS: 4355 case INTRINSIC_GT: 4356 case INTRINSIC_GT_OS: 4357 case INTRINSIC_GE: 4358 case INTRINSIC_GE_OS: 4359 case INTRINSIC_LT: 4360 case INTRINSIC_LT_OS: 4361 case INTRINSIC_LE: 4362 case INTRINSIC_LE_OS: 4363 4364 if (op1->rank == 0 && op2->rank == 0) 4365 e->rank = 0; 4366 4367 if (op1->rank == 0 && op2->rank != 0) 4368 { 4369 e->rank = op2->rank; 4370 4371 if (e->shape == NULL) 4372 e->shape = gfc_copy_shape (op2->shape, op2->rank); 4373 } 4374 4375 if (op1->rank != 0 && op2->rank == 0) 4376 { 4377 e->rank = op1->rank; 4378 4379 if (e->shape == NULL) 4380 e->shape = gfc_copy_shape (op1->shape, op1->rank); 4381 } 4382 4383 if (op1->rank != 0 && op2->rank != 0) 4384 { 4385 if (op1->rank == op2->rank) 4386 { 4387 e->rank = op1->rank; 4388 if (e->shape == NULL) 4389 { 4390 t = compare_shapes (op1, op2); 4391 if (!t) 4392 e->shape = NULL; 4393 else 4394 e->shape = gfc_copy_shape (op1->shape, op1->rank); 4395 } 4396 } 4397 else 4398 { 4399 /* Allow higher level expressions to work. */ 4400 e->rank = 0; 4401 4402 /* Try user-defined operators, and otherwise throw an error. */ 4403 dual_locus_error = true; 4404 snprintf (msg, sizeof (msg), 4405 _("Inconsistent ranks for operator at %%L and %%L")); 4406 goto bad_op; 4407 } 4408 } 4409 4410 break; 4411 4412 case INTRINSIC_PARENTHESES: 4413 case INTRINSIC_NOT: 4414 case INTRINSIC_UPLUS: 4415 case INTRINSIC_UMINUS: 4416 /* Simply copy arrayness attribute */ 4417 e->rank = op1->rank; 4418 4419 if (e->shape == NULL) 4420 e->shape = gfc_copy_shape (op1->shape, op1->rank); 4421 4422 break; 4423 4424 default: 4425 break; 4426 } 4427 4428 simplify_op: 4429 4430 /* Attempt to simplify the expression. */ 4431 if (t) 4432 { 4433 t = gfc_simplify_expr (e, 0); 4434 /* Some calls do not succeed in simplification and return false 4435 even though there is no error; e.g. variable references to 4436 PARAMETER arrays. */ 4437 if (!gfc_is_constant_expr (e)) 4438 t = true; 4439 } 4440 return t; 4441 4442 bad_op: 4443 4444 { 4445 match m = gfc_extend_expr (e); 4446 if (m == MATCH_YES) 4447 return true; 4448 if (m == MATCH_ERROR) 4449 return false; 4450 } 4451 4452 if (dual_locus_error) 4453 gfc_error (msg, &op1->where, &op2->where); 4454 else 4455 gfc_error (msg, &e->where); 4456 4457 return false; 4458 } 4459 4460 4461 /************** Array resolution subroutines **************/ 4462 4463 enum compare_result 4464 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }; 4465 4466 /* Compare two integer expressions. */ 4467 4468 static compare_result 4469 compare_bound (gfc_expr *a, gfc_expr *b) 4470 { 4471 int i; 4472 4473 if (a == NULL || a->expr_type != EXPR_CONSTANT 4474 || b == NULL || b->expr_type != EXPR_CONSTANT) 4475 return CMP_UNKNOWN; 4476 4477 /* If either of the types isn't INTEGER, we must have 4478 raised an error earlier. */ 4479 4480 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER) 4481 return CMP_UNKNOWN; 4482 4483 i = mpz_cmp (a->value.integer, b->value.integer); 4484 4485 if (i < 0) 4486 return CMP_LT; 4487 if (i > 0) 4488 return CMP_GT; 4489 return CMP_EQ; 4490 } 4491 4492 4493 /* Compare an integer expression with an integer. */ 4494 4495 static compare_result 4496 compare_bound_int (gfc_expr *a, int b) 4497 { 4498 int i; 4499 4500 if (a == NULL || a->expr_type != EXPR_CONSTANT) 4501 return CMP_UNKNOWN; 4502 4503 if (a->ts.type != BT_INTEGER) 4504 gfc_internal_error ("compare_bound_int(): Bad expression"); 4505 4506 i = mpz_cmp_si (a->value.integer, b); 4507 4508 if (i < 0) 4509 return CMP_LT; 4510 if (i > 0) 4511 return CMP_GT; 4512 return CMP_EQ; 4513 } 4514 4515 4516 /* Compare an integer expression with a mpz_t. */ 4517 4518 static compare_result 4519 compare_bound_mpz_t (gfc_expr *a, mpz_t b) 4520 { 4521 int i; 4522 4523 if (a == NULL || a->expr_type != EXPR_CONSTANT) 4524 return CMP_UNKNOWN; 4525 4526 if (a->ts.type != BT_INTEGER) 4527 gfc_internal_error ("compare_bound_int(): Bad expression"); 4528 4529 i = mpz_cmp (a->value.integer, b); 4530 4531 if (i < 0) 4532 return CMP_LT; 4533 if (i > 0) 4534 return CMP_GT; 4535 return CMP_EQ; 4536 } 4537 4538 4539 /* Compute the last value of a sequence given by a triplet. 4540 Return 0 if it wasn't able to compute the last value, or if the 4541 sequence if empty, and 1 otherwise. */ 4542 4543 static int 4544 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end, 4545 gfc_expr *stride, mpz_t last) 4546 { 4547 mpz_t rem; 4548 4549 if (start == NULL || start->expr_type != EXPR_CONSTANT 4550 || end == NULL || end->expr_type != EXPR_CONSTANT 4551 || (stride != NULL && stride->expr_type != EXPR_CONSTANT)) 4552 return 0; 4553 4554 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER 4555 || (stride != NULL && stride->ts.type != BT_INTEGER)) 4556 return 0; 4557 4558 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ) 4559 { 4560 if (compare_bound (start, end) == CMP_GT) 4561 return 0; 4562 mpz_set (last, end->value.integer); 4563 return 1; 4564 } 4565 4566 if (compare_bound_int (stride, 0) == CMP_GT) 4567 { 4568 /* Stride is positive */ 4569 if (mpz_cmp (start->value.integer, end->value.integer) > 0) 4570 return 0; 4571 } 4572 else 4573 { 4574 /* Stride is negative */ 4575 if (mpz_cmp (start->value.integer, end->value.integer) < 0) 4576 return 0; 4577 } 4578 4579 mpz_init (rem); 4580 mpz_sub (rem, end->value.integer, start->value.integer); 4581 mpz_tdiv_r (rem, rem, stride->value.integer); 4582 mpz_sub (last, end->value.integer, rem); 4583 mpz_clear (rem); 4584 4585 return 1; 4586 } 4587 4588 4589 /* Compare a single dimension of an array reference to the array 4590 specification. */ 4591 4592 static bool 4593 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as) 4594 { 4595 mpz_t last_value; 4596 4597 if (ar->dimen_type[i] == DIMEN_STAR) 4598 { 4599 gcc_assert (ar->stride[i] == NULL); 4600 /* This implies [*] as [*:] and [*:3] are not possible. */ 4601 if (ar->start[i] == NULL) 4602 { 4603 gcc_assert (ar->end[i] == NULL); 4604 return true; 4605 } 4606 } 4607 4608 /* Given start, end and stride values, calculate the minimum and 4609 maximum referenced indexes. */ 4610 4611 switch (ar->dimen_type[i]) 4612 { 4613 case DIMEN_VECTOR: 4614 case DIMEN_THIS_IMAGE: 4615 break; 4616 4617 case DIMEN_STAR: 4618 case DIMEN_ELEMENT: 4619 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT) 4620 { 4621 if (i < as->rank) 4622 gfc_warning (0, "Array reference at %L is out of bounds " 4623 "(%ld < %ld) in dimension %d", &ar->c_where[i], 4624 mpz_get_si (ar->start[i]->value.integer), 4625 mpz_get_si (as->lower[i]->value.integer), i+1); 4626 else 4627 gfc_warning (0, "Array reference at %L is out of bounds " 4628 "(%ld < %ld) in codimension %d", &ar->c_where[i], 4629 mpz_get_si (ar->start[i]->value.integer), 4630 mpz_get_si (as->lower[i]->value.integer), 4631 i + 1 - as->rank); 4632 return true; 4633 } 4634 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT) 4635 { 4636 if (i < as->rank) 4637 gfc_warning (0, "Array reference at %L is out of bounds " 4638 "(%ld > %ld) in dimension %d", &ar->c_where[i], 4639 mpz_get_si (ar->start[i]->value.integer), 4640 mpz_get_si (as->upper[i]->value.integer), i+1); 4641 else 4642 gfc_warning (0, "Array reference at %L is out of bounds " 4643 "(%ld > %ld) in codimension %d", &ar->c_where[i], 4644 mpz_get_si (ar->start[i]->value.integer), 4645 mpz_get_si (as->upper[i]->value.integer), 4646 i + 1 - as->rank); 4647 return true; 4648 } 4649 4650 break; 4651 4652 case DIMEN_RANGE: 4653 { 4654 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i]) 4655 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i]) 4656 4657 compare_result comp_start_end = compare_bound (AR_START, AR_END); 4658 4659 /* Check for zero stride, which is not allowed. */ 4660 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ) 4661 { 4662 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]); 4663 return false; 4664 } 4665 4666 /* if start == len || (stride > 0 && start < len) 4667 || (stride < 0 && start > len), 4668 then the array section contains at least one element. In this 4669 case, there is an out-of-bounds access if 4670 (start < lower || start > upper). */ 4671 if (compare_bound (AR_START, AR_END) == CMP_EQ 4672 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT 4673 || ar->stride[i] == NULL) && comp_start_end == CMP_LT) 4674 || (compare_bound_int (ar->stride[i], 0) == CMP_LT 4675 && comp_start_end == CMP_GT)) 4676 { 4677 if (compare_bound (AR_START, as->lower[i]) == CMP_LT) 4678 { 4679 gfc_warning (0, "Lower array reference at %L is out of bounds " 4680 "(%ld < %ld) in dimension %d", &ar->c_where[i], 4681 mpz_get_si (AR_START->value.integer), 4682 mpz_get_si (as->lower[i]->value.integer), i+1); 4683 return true; 4684 } 4685 if (compare_bound (AR_START, as->upper[i]) == CMP_GT) 4686 { 4687 gfc_warning (0, "Lower array reference at %L is out of bounds " 4688 "(%ld > %ld) in dimension %d", &ar->c_where[i], 4689 mpz_get_si (AR_START->value.integer), 4690 mpz_get_si (as->upper[i]->value.integer), i+1); 4691 return true; 4692 } 4693 } 4694 4695 /* If we can compute the highest index of the array section, 4696 then it also has to be between lower and upper. */ 4697 mpz_init (last_value); 4698 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i], 4699 last_value)) 4700 { 4701 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT) 4702 { 4703 gfc_warning (0, "Upper array reference at %L is out of bounds " 4704 "(%ld < %ld) in dimension %d", &ar->c_where[i], 4705 mpz_get_si (last_value), 4706 mpz_get_si (as->lower[i]->value.integer), i+1); 4707 mpz_clear (last_value); 4708 return true; 4709 } 4710 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT) 4711 { 4712 gfc_warning (0, "Upper array reference at %L is out of bounds " 4713 "(%ld > %ld) in dimension %d", &ar->c_where[i], 4714 mpz_get_si (last_value), 4715 mpz_get_si (as->upper[i]->value.integer), i+1); 4716 mpz_clear (last_value); 4717 return true; 4718 } 4719 } 4720 mpz_clear (last_value); 4721 4722 #undef AR_START 4723 #undef AR_END 4724 } 4725 break; 4726 4727 default: 4728 gfc_internal_error ("check_dimension(): Bad array reference"); 4729 } 4730 4731 return true; 4732 } 4733 4734 4735 /* Compare an array reference with an array specification. */ 4736 4737 static bool 4738 compare_spec_to_ref (gfc_array_ref *ar) 4739 { 4740 gfc_array_spec *as; 4741 int i; 4742 4743 as = ar->as; 4744 i = as->rank - 1; 4745 /* TODO: Full array sections are only allowed as actual parameters. */ 4746 if (as->type == AS_ASSUMED_SIZE 4747 && (/*ar->type == AR_FULL 4748 ||*/ (ar->type == AR_SECTION 4749 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL))) 4750 { 4751 gfc_error ("Rightmost upper bound of assumed size array section " 4752 "not specified at %L", &ar->where); 4753 return false; 4754 } 4755 4756 if (ar->type == AR_FULL) 4757 return true; 4758 4759 if (as->rank != ar->dimen) 4760 { 4761 gfc_error ("Rank mismatch in array reference at %L (%d/%d)", 4762 &ar->where, ar->dimen, as->rank); 4763 return false; 4764 } 4765 4766 /* ar->codimen == 0 is a local array. */ 4767 if (as->corank != ar->codimen && ar->codimen != 0) 4768 { 4769 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)", 4770 &ar->where, ar->codimen, as->corank); 4771 return false; 4772 } 4773 4774 for (i = 0; i < as->rank; i++) 4775 if (!check_dimension (i, ar, as)) 4776 return false; 4777 4778 /* Local access has no coarray spec. */ 4779 if (ar->codimen != 0) 4780 for (i = as->rank; i < as->rank + as->corank; i++) 4781 { 4782 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate 4783 && ar->dimen_type[i] != DIMEN_THIS_IMAGE) 4784 { 4785 gfc_error ("Coindex of codimension %d must be a scalar at %L", 4786 i + 1 - as->rank, &ar->where); 4787 return false; 4788 } 4789 if (!check_dimension (i, ar, as)) 4790 return false; 4791 } 4792 4793 return true; 4794 } 4795 4796 4797 /* Resolve one part of an array index. */ 4798 4799 static bool 4800 gfc_resolve_index_1 (gfc_expr *index, int check_scalar, 4801 int force_index_integer_kind) 4802 { 4803 gfc_typespec ts; 4804 4805 if (index == NULL) 4806 return true; 4807 4808 if (!gfc_resolve_expr (index)) 4809 return false; 4810 4811 if (check_scalar && index->rank != 0) 4812 { 4813 gfc_error ("Array index at %L must be scalar", &index->where); 4814 return false; 4815 } 4816 4817 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL) 4818 { 4819 gfc_error ("Array index at %L must be of INTEGER type, found %s", 4820 &index->where, gfc_basic_typename (index->ts.type)); 4821 return false; 4822 } 4823 4824 if (index->ts.type == BT_REAL) 4825 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L", 4826 &index->where)) 4827 return false; 4828 4829 if ((index->ts.kind != gfc_index_integer_kind 4830 && force_index_integer_kind) 4831 || index->ts.type != BT_INTEGER) 4832 { 4833 gfc_clear_ts (&ts); 4834 ts.type = BT_INTEGER; 4835 ts.kind = gfc_index_integer_kind; 4836 4837 gfc_convert_type_warn (index, &ts, 2, 0); 4838 } 4839 4840 return true; 4841 } 4842 4843 /* Resolve one part of an array index. */ 4844 4845 bool 4846 gfc_resolve_index (gfc_expr *index, int check_scalar) 4847 { 4848 return gfc_resolve_index_1 (index, check_scalar, 1); 4849 } 4850 4851 /* Resolve a dim argument to an intrinsic function. */ 4852 4853 bool 4854 gfc_resolve_dim_arg (gfc_expr *dim) 4855 { 4856 if (dim == NULL) 4857 return true; 4858 4859 if (!gfc_resolve_expr (dim)) 4860 return false; 4861 4862 if (dim->rank != 0) 4863 { 4864 gfc_error ("Argument dim at %L must be scalar", &dim->where); 4865 return false; 4866 4867 } 4868 4869 if (dim->ts.type != BT_INTEGER) 4870 { 4871 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where); 4872 return false; 4873 } 4874 4875 if (dim->ts.kind != gfc_index_integer_kind) 4876 { 4877 gfc_typespec ts; 4878 4879 gfc_clear_ts (&ts); 4880 ts.type = BT_INTEGER; 4881 ts.kind = gfc_index_integer_kind; 4882 4883 gfc_convert_type_warn (dim, &ts, 2, 0); 4884 } 4885 4886 return true; 4887 } 4888 4889 /* Given an expression that contains array references, update those array 4890 references to point to the right array specifications. While this is 4891 filled in during matching, this information is difficult to save and load 4892 in a module, so we take care of it here. 4893 4894 The idea here is that the original array reference comes from the 4895 base symbol. We traverse the list of reference structures, setting 4896 the stored reference to references. Component references can 4897 provide an additional array specification. */ 4898 static void 4899 resolve_assoc_var (gfc_symbol* sym, bool resolve_target); 4900 4901 static void 4902 find_array_spec (gfc_expr *e) 4903 { 4904 gfc_array_spec *as; 4905 gfc_component *c; 4906 gfc_ref *ref; 4907 bool class_as = false; 4908 4909 if (e->symtree->n.sym->assoc) 4910 { 4911 if (e->symtree->n.sym->assoc->target) 4912 gfc_resolve_expr (e->symtree->n.sym->assoc->target); 4913 resolve_assoc_var (e->symtree->n.sym, false); 4914 } 4915 4916 if (e->symtree->n.sym->ts.type == BT_CLASS) 4917 { 4918 as = CLASS_DATA (e->symtree->n.sym)->as; 4919 class_as = true; 4920 } 4921 else 4922 as = e->symtree->n.sym->as; 4923 4924 for (ref = e->ref; ref; ref = ref->next) 4925 switch (ref->type) 4926 { 4927 case REF_ARRAY: 4928 if (as == NULL) 4929 gfc_internal_error ("find_array_spec(): Missing spec"); 4930 4931 ref->u.ar.as = as; 4932 as = NULL; 4933 break; 4934 4935 case REF_COMPONENT: 4936 c = ref->u.c.component; 4937 if (c->attr.dimension) 4938 { 4939 if (as != NULL && !(class_as && as == c->as)) 4940 gfc_internal_error ("find_array_spec(): unused as(1)"); 4941 as = c->as; 4942 } 4943 4944 break; 4945 4946 case REF_SUBSTRING: 4947 case REF_INQUIRY: 4948 break; 4949 } 4950 4951 if (as != NULL) 4952 gfc_internal_error ("find_array_spec(): unused as(2)"); 4953 } 4954 4955 4956 /* Resolve an array reference. */ 4957 4958 static bool 4959 resolve_array_ref (gfc_array_ref *ar) 4960 { 4961 int i, check_scalar; 4962 gfc_expr *e; 4963 4964 for (i = 0; i < ar->dimen + ar->codimen; i++) 4965 { 4966 check_scalar = ar->dimen_type[i] == DIMEN_RANGE; 4967 4968 /* Do not force gfc_index_integer_kind for the start. We can 4969 do fine with any integer kind. This avoids temporary arrays 4970 created for indexing with a vector. */ 4971 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0)) 4972 return false; 4973 if (!gfc_resolve_index (ar->end[i], check_scalar)) 4974 return false; 4975 if (!gfc_resolve_index (ar->stride[i], check_scalar)) 4976 return false; 4977 4978 e = ar->start[i]; 4979 4980 if (ar->dimen_type[i] == DIMEN_UNKNOWN) 4981 switch (e->rank) 4982 { 4983 case 0: 4984 ar->dimen_type[i] = DIMEN_ELEMENT; 4985 break; 4986 4987 case 1: 4988 ar->dimen_type[i] = DIMEN_VECTOR; 4989 if (e->expr_type == EXPR_VARIABLE 4990 && e->symtree->n.sym->ts.type == BT_DERIVED) 4991 ar->start[i] = gfc_get_parentheses (e); 4992 break; 4993 4994 default: 4995 gfc_error ("Array index at %L is an array of rank %d", 4996 &ar->c_where[i], e->rank); 4997 return false; 4998 } 4999 5000 /* Fill in the upper bound, which may be lower than the 5001 specified one for something like a(2:10:5), which is 5002 identical to a(2:7:5). Only relevant for strides not equal 5003 to one. Don't try a division by zero. */ 5004 if (ar->dimen_type[i] == DIMEN_RANGE 5005 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT 5006 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0 5007 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0) 5008 { 5009 mpz_t size, end; 5010 5011 if (gfc_ref_dimen_size (ar, i, &size, &end)) 5012 { 5013 if (ar->end[i] == NULL) 5014 { 5015 ar->end[i] = 5016 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind, 5017 &ar->where); 5018 mpz_set (ar->end[i]->value.integer, end); 5019 } 5020 else if (ar->end[i]->ts.type == BT_INTEGER 5021 && ar->end[i]->expr_type == EXPR_CONSTANT) 5022 { 5023 mpz_set (ar->end[i]->value.integer, end); 5024 } 5025 else 5026 gcc_unreachable (); 5027 5028 mpz_clear (size); 5029 mpz_clear (end); 5030 } 5031 } 5032 } 5033 5034 if (ar->type == AR_FULL) 5035 { 5036 if (ar->as->rank == 0) 5037 ar->type = AR_ELEMENT; 5038 5039 /* Make sure array is the same as array(:,:), this way 5040 we don't need to special case all the time. */ 5041 ar->dimen = ar->as->rank; 5042 for (i = 0; i < ar->dimen; i++) 5043 { 5044 ar->dimen_type[i] = DIMEN_RANGE; 5045 5046 gcc_assert (ar->start[i] == NULL); 5047 gcc_assert (ar->end[i] == NULL); 5048 gcc_assert (ar->stride[i] == NULL); 5049 } 5050 } 5051 5052 /* If the reference type is unknown, figure out what kind it is. */ 5053 5054 if (ar->type == AR_UNKNOWN) 5055 { 5056 ar->type = AR_ELEMENT; 5057 for (i = 0; i < ar->dimen; i++) 5058 if (ar->dimen_type[i] == DIMEN_RANGE 5059 || ar->dimen_type[i] == DIMEN_VECTOR) 5060 { 5061 ar->type = AR_SECTION; 5062 break; 5063 } 5064 } 5065 5066 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar)) 5067 return false; 5068 5069 if (ar->as->corank && ar->codimen == 0) 5070 { 5071 int n; 5072 ar->codimen = ar->as->corank; 5073 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++) 5074 ar->dimen_type[n] = DIMEN_THIS_IMAGE; 5075 } 5076 5077 return true; 5078 } 5079 5080 5081 bool 5082 gfc_resolve_substring (gfc_ref *ref, bool *equal_length) 5083 { 5084 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false); 5085 5086 if (ref->u.ss.start != NULL) 5087 { 5088 if (!gfc_resolve_expr (ref->u.ss.start)) 5089 return false; 5090 5091 if (ref->u.ss.start->ts.type != BT_INTEGER) 5092 { 5093 gfc_error ("Substring start index at %L must be of type INTEGER", 5094 &ref->u.ss.start->where); 5095 return false; 5096 } 5097 5098 if (ref->u.ss.start->rank != 0) 5099 { 5100 gfc_error ("Substring start index at %L must be scalar", 5101 &ref->u.ss.start->where); 5102 return false; 5103 } 5104 5105 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT 5106 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ 5107 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT)) 5108 { 5109 gfc_error ("Substring start index at %L is less than one", 5110 &ref->u.ss.start->where); 5111 return false; 5112 } 5113 } 5114 5115 if (ref->u.ss.end != NULL) 5116 { 5117 if (!gfc_resolve_expr (ref->u.ss.end)) 5118 return false; 5119 5120 if (ref->u.ss.end->ts.type != BT_INTEGER) 5121 { 5122 gfc_error ("Substring end index at %L must be of type INTEGER", 5123 &ref->u.ss.end->where); 5124 return false; 5125 } 5126 5127 if (ref->u.ss.end->rank != 0) 5128 { 5129 gfc_error ("Substring end index at %L must be scalar", 5130 &ref->u.ss.end->where); 5131 return false; 5132 } 5133 5134 if (ref->u.ss.length != NULL 5135 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT 5136 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ 5137 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT)) 5138 { 5139 gfc_error ("Substring end index at %L exceeds the string length", 5140 &ref->u.ss.start->where); 5141 return false; 5142 } 5143 5144 if (compare_bound_mpz_t (ref->u.ss.end, 5145 gfc_integer_kinds[k].huge) == CMP_GT 5146 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ 5147 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT)) 5148 { 5149 gfc_error ("Substring end index at %L is too large", 5150 &ref->u.ss.end->where); 5151 return false; 5152 } 5153 /* If the substring has the same length as the original 5154 variable, the reference itself can be deleted. */ 5155 5156 if (ref->u.ss.length != NULL 5157 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_EQ 5158 && compare_bound_int (ref->u.ss.start, 1) == CMP_EQ) 5159 *equal_length = true; 5160 } 5161 5162 return true; 5163 } 5164 5165 5166 /* This function supplies missing substring charlens. */ 5167 5168 void 5169 gfc_resolve_substring_charlen (gfc_expr *e) 5170 { 5171 gfc_ref *char_ref; 5172 gfc_expr *start, *end; 5173 gfc_typespec *ts = NULL; 5174 mpz_t diff; 5175 5176 for (char_ref = e->ref; char_ref; char_ref = char_ref->next) 5177 { 5178 if (char_ref->type == REF_SUBSTRING || char_ref->type == REF_INQUIRY) 5179 break; 5180 if (char_ref->type == REF_COMPONENT) 5181 ts = &char_ref->u.c.component->ts; 5182 } 5183 5184 if (!char_ref || char_ref->type == REF_INQUIRY) 5185 return; 5186 5187 gcc_assert (char_ref->next == NULL); 5188 5189 if (e->ts.u.cl) 5190 { 5191 if (e->ts.u.cl->length) 5192 gfc_free_expr (e->ts.u.cl->length); 5193 else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy) 5194 return; 5195 } 5196 5197 e->ts.type = BT_CHARACTER; 5198 e->ts.kind = gfc_default_character_kind; 5199 5200 if (!e->ts.u.cl) 5201 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL); 5202 5203 if (char_ref->u.ss.start) 5204 start = gfc_copy_expr (char_ref->u.ss.start); 5205 else 5206 start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1); 5207 5208 if (char_ref->u.ss.end) 5209 end = gfc_copy_expr (char_ref->u.ss.end); 5210 else if (e->expr_type == EXPR_VARIABLE) 5211 { 5212 if (!ts) 5213 ts = &e->symtree->n.sym->ts; 5214 end = gfc_copy_expr (ts->u.cl->length); 5215 } 5216 else 5217 end = NULL; 5218 5219 if (!start || !end) 5220 { 5221 gfc_free_expr (start); 5222 gfc_free_expr (end); 5223 return; 5224 } 5225 5226 /* Length = (end - start + 1). 5227 Check first whether it has a constant length. */ 5228 if (gfc_dep_difference (end, start, &diff)) 5229 { 5230 gfc_expr *len = gfc_get_constant_expr (BT_INTEGER, gfc_charlen_int_kind, 5231 &e->where); 5232 5233 mpz_add_ui (len->value.integer, diff, 1); 5234 mpz_clear (diff); 5235 e->ts.u.cl->length = len; 5236 /* The check for length < 0 is handled below */ 5237 } 5238 else 5239 { 5240 e->ts.u.cl->length = gfc_subtract (end, start); 5241 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length, 5242 gfc_get_int_expr (gfc_charlen_int_kind, 5243 NULL, 1)); 5244 } 5245 5246 /* F2008, 6.4.1: Both the starting point and the ending point shall 5247 be within the range 1, 2, ..., n unless the starting point exceeds 5248 the ending point, in which case the substring has length zero. */ 5249 5250 if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0) 5251 mpz_set_si (e->ts.u.cl->length->value.integer, 0); 5252 5253 e->ts.u.cl->length->ts.type = BT_INTEGER; 5254 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind; 5255 5256 /* Make sure that the length is simplified. */ 5257 gfc_simplify_expr (e->ts.u.cl->length, 1); 5258 gfc_resolve_expr (e->ts.u.cl->length); 5259 } 5260 5261 5262 /* Resolve subtype references. */ 5263 5264 bool 5265 gfc_resolve_ref (gfc_expr *expr) 5266 { 5267 int current_part_dimension, n_components, seen_part_dimension, dim; 5268 gfc_ref *ref, **prev, *array_ref; 5269 bool equal_length; 5270 5271 for (ref = expr->ref; ref; ref = ref->next) 5272 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL) 5273 { 5274 find_array_spec (expr); 5275 break; 5276 } 5277 5278 for (prev = &expr->ref; *prev != NULL; 5279 prev = *prev == NULL ? prev : &(*prev)->next) 5280 switch ((*prev)->type) 5281 { 5282 case REF_ARRAY: 5283 if (!resolve_array_ref (&(*prev)->u.ar)) 5284 return false; 5285 break; 5286 5287 case REF_COMPONENT: 5288 case REF_INQUIRY: 5289 break; 5290 5291 case REF_SUBSTRING: 5292 equal_length = false; 5293 if (!gfc_resolve_substring (*prev, &equal_length)) 5294 return false; 5295 5296 if (expr->expr_type != EXPR_SUBSTRING && equal_length) 5297 { 5298 /* Remove the reference and move the charlen, if any. */ 5299 ref = *prev; 5300 *prev = ref->next; 5301 ref->next = NULL; 5302 expr->ts.u.cl = ref->u.ss.length; 5303 ref->u.ss.length = NULL; 5304 gfc_free_ref_list (ref); 5305 } 5306 break; 5307 } 5308 5309 /* Check constraints on part references. */ 5310 5311 current_part_dimension = 0; 5312 seen_part_dimension = 0; 5313 n_components = 0; 5314 array_ref = NULL; 5315 5316 for (ref = expr->ref; ref; ref = ref->next) 5317 { 5318 switch (ref->type) 5319 { 5320 case REF_ARRAY: 5321 array_ref = ref; 5322 switch (ref->u.ar.type) 5323 { 5324 case AR_FULL: 5325 /* Coarray scalar. */ 5326 if (ref->u.ar.as->rank == 0) 5327 { 5328 current_part_dimension = 0; 5329 break; 5330 } 5331 /* Fall through. */ 5332 case AR_SECTION: 5333 current_part_dimension = 1; 5334 break; 5335 5336 case AR_ELEMENT: 5337 array_ref = NULL; 5338 current_part_dimension = 0; 5339 break; 5340 5341 case AR_UNKNOWN: 5342 gfc_internal_error ("resolve_ref(): Bad array reference"); 5343 } 5344 5345 break; 5346 5347 case REF_COMPONENT: 5348 if (current_part_dimension || seen_part_dimension) 5349 { 5350 /* F03:C614. */ 5351 if (ref->u.c.component->attr.pointer 5352 || ref->u.c.component->attr.proc_pointer 5353 || (ref->u.c.component->ts.type == BT_CLASS 5354 && CLASS_DATA (ref->u.c.component)->attr.pointer)) 5355 { 5356 gfc_error ("Component to the right of a part reference " 5357 "with nonzero rank must not have the POINTER " 5358 "attribute at %L", &expr->where); 5359 return false; 5360 } 5361 else if (ref->u.c.component->attr.allocatable 5362 || (ref->u.c.component->ts.type == BT_CLASS 5363 && CLASS_DATA (ref->u.c.component)->attr.allocatable)) 5364 5365 { 5366 gfc_error ("Component to the right of a part reference " 5367 "with nonzero rank must not have the ALLOCATABLE " 5368 "attribute at %L", &expr->where); 5369 return false; 5370 } 5371 } 5372 5373 n_components++; 5374 break; 5375 5376 case REF_SUBSTRING: 5377 break; 5378 5379 case REF_INQUIRY: 5380 /* Implement requirement in note 9.7 of F2018 that the result of the 5381 LEN inquiry be a scalar. */ 5382 if (ref->u.i == INQUIRY_LEN && array_ref && expr->ts.deferred) 5383 { 5384 array_ref->u.ar.type = AR_ELEMENT; 5385 expr->rank = 0; 5386 /* INQUIRY_LEN is not evaluated from the rest of the expr 5387 but directly from the string length. This means that setting 5388 the array indices to one does not matter but might trigger 5389 a runtime bounds error. Suppress the check. */ 5390 expr->no_bounds_check = 1; 5391 for (dim = 0; dim < array_ref->u.ar.dimen; dim++) 5392 { 5393 array_ref->u.ar.dimen_type[dim] = DIMEN_ELEMENT; 5394 if (array_ref->u.ar.start[dim]) 5395 gfc_free_expr (array_ref->u.ar.start[dim]); 5396 array_ref->u.ar.start[dim] 5397 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1); 5398 if (array_ref->u.ar.end[dim]) 5399 gfc_free_expr (array_ref->u.ar.end[dim]); 5400 if (array_ref->u.ar.stride[dim]) 5401 gfc_free_expr (array_ref->u.ar.stride[dim]); 5402 } 5403 } 5404 break; 5405 } 5406 5407 if (((ref->type == REF_COMPONENT && n_components > 1) 5408 || ref->next == NULL) 5409 && current_part_dimension 5410 && seen_part_dimension) 5411 { 5412 gfc_error ("Two or more part references with nonzero rank must " 5413 "not be specified at %L", &expr->where); 5414 return false; 5415 } 5416 5417 if (ref->type == REF_COMPONENT) 5418 { 5419 if (current_part_dimension) 5420 seen_part_dimension = 1; 5421 5422 /* reset to make sure */ 5423 current_part_dimension = 0; 5424 } 5425 } 5426 5427 return true; 5428 } 5429 5430 5431 /* Given an expression, determine its shape. This is easier than it sounds. 5432 Leaves the shape array NULL if it is not possible to determine the shape. */ 5433 5434 static void 5435 expression_shape (gfc_expr *e) 5436 { 5437 mpz_t array[GFC_MAX_DIMENSIONS]; 5438 int i; 5439 5440 if (e->rank <= 0 || e->shape != NULL) 5441 return; 5442 5443 for (i = 0; i < e->rank; i++) 5444 if (!gfc_array_dimen_size (e, i, &array[i])) 5445 goto fail; 5446 5447 e->shape = gfc_get_shape (e->rank); 5448 5449 memcpy (e->shape, array, e->rank * sizeof (mpz_t)); 5450 5451 return; 5452 5453 fail: 5454 for (i--; i >= 0; i--) 5455 mpz_clear (array[i]); 5456 } 5457 5458 5459 /* Given a variable expression node, compute the rank of the expression by 5460 examining the base symbol and any reference structures it may have. */ 5461 5462 void 5463 gfc_expression_rank (gfc_expr *e) 5464 { 5465 gfc_ref *ref; 5466 int i, rank; 5467 5468 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that 5469 could lead to serious confusion... */ 5470 gcc_assert (e->expr_type != EXPR_COMPCALL); 5471 5472 if (e->ref == NULL) 5473 { 5474 if (e->expr_type == EXPR_ARRAY) 5475 goto done; 5476 /* Constructors can have a rank different from one via RESHAPE(). */ 5477 5478 e->rank = ((e->symtree == NULL || e->symtree->n.sym->as == NULL) 5479 ? 0 : e->symtree->n.sym->as->rank); 5480 goto done; 5481 } 5482 5483 rank = 0; 5484 5485 for (ref = e->ref; ref; ref = ref->next) 5486 { 5487 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer 5488 && ref->u.c.component->attr.function && !ref->next) 5489 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0; 5490 5491 if (ref->type != REF_ARRAY) 5492 continue; 5493 5494 if (ref->u.ar.type == AR_FULL) 5495 { 5496 rank = ref->u.ar.as->rank; 5497 break; 5498 } 5499 5500 if (ref->u.ar.type == AR_SECTION) 5501 { 5502 /* Figure out the rank of the section. */ 5503 if (rank != 0) 5504 gfc_internal_error ("gfc_expression_rank(): Two array specs"); 5505 5506 for (i = 0; i < ref->u.ar.dimen; i++) 5507 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE 5508 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR) 5509 rank++; 5510 5511 break; 5512 } 5513 } 5514 5515 e->rank = rank; 5516 5517 done: 5518 expression_shape (e); 5519 } 5520 5521 5522 static void 5523 add_caf_get_intrinsic (gfc_expr *e) 5524 { 5525 gfc_expr *wrapper, *tmp_expr; 5526 gfc_ref *ref; 5527 int n; 5528 5529 for (ref = e->ref; ref; ref = ref->next) 5530 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0) 5531 break; 5532 if (ref == NULL) 5533 return; 5534 5535 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++) 5536 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT) 5537 return; 5538 5539 tmp_expr = XCNEW (gfc_expr); 5540 *tmp_expr = *e; 5541 wrapper = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_CAF_GET, 5542 "caf_get", tmp_expr->where, 1, tmp_expr); 5543 wrapper->ts = e->ts; 5544 wrapper->rank = e->rank; 5545 if (e->rank) 5546 wrapper->shape = gfc_copy_shape (e->shape, e->rank); 5547 *e = *wrapper; 5548 free (wrapper); 5549 } 5550 5551 5552 static void 5553 remove_caf_get_intrinsic (gfc_expr *e) 5554 { 5555 gcc_assert (e->expr_type == EXPR_FUNCTION && e->value.function.isym 5556 && e->value.function.isym->id == GFC_ISYM_CAF_GET); 5557 gfc_expr *e2 = e->value.function.actual->expr; 5558 e->value.function.actual->expr = NULL; 5559 gfc_free_actual_arglist (e->value.function.actual); 5560 gfc_free_shape (&e->shape, e->rank); 5561 *e = *e2; 5562 free (e2); 5563 } 5564 5565 5566 /* Resolve a variable expression. */ 5567 5568 static bool 5569 resolve_variable (gfc_expr *e) 5570 { 5571 gfc_symbol *sym; 5572 bool t; 5573 5574 t = true; 5575 5576 if (e->symtree == NULL) 5577 return false; 5578 sym = e->symtree->n.sym; 5579 5580 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*) 5581 as ts.type is set to BT_ASSUMED in resolve_symbol. */ 5582 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) 5583 { 5584 if (!actual_arg || inquiry_argument) 5585 { 5586 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only " 5587 "be used as actual argument", sym->name, &e->where); 5588 return false; 5589 } 5590 } 5591 /* TS 29113, 407b. */ 5592 else if (e->ts.type == BT_ASSUMED) 5593 { 5594 if (!actual_arg) 5595 { 5596 gfc_error ("Assumed-type variable %s at %L may only be used " 5597 "as actual argument", sym->name, &e->where); 5598 return false; 5599 } 5600 else if (inquiry_argument && !first_actual_arg) 5601 { 5602 /* FIXME: It doesn't work reliably as inquiry_argument is not set 5603 for all inquiry functions in resolve_function; the reason is 5604 that the function-name resolution happens too late in that 5605 function. */ 5606 gfc_error ("Assumed-type variable %s at %L as actual argument to " 5607 "an inquiry function shall be the first argument", 5608 sym->name, &e->where); 5609 return false; 5610 } 5611 } 5612 /* TS 29113, C535b. */ 5613 else if (((sym->ts.type == BT_CLASS && sym->attr.class_ok 5614 && sym->ts.u.derived && CLASS_DATA (sym) 5615 && CLASS_DATA (sym)->as 5616 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK) 5617 || (sym->ts.type != BT_CLASS && sym->as 5618 && sym->as->type == AS_ASSUMED_RANK)) 5619 && !sym->attr.select_rank_temporary) 5620 { 5621 if (!actual_arg 5622 && !(cs_base && cs_base->current 5623 && cs_base->current->op == EXEC_SELECT_RANK)) 5624 { 5625 gfc_error ("Assumed-rank variable %s at %L may only be used as " 5626 "actual argument", sym->name, &e->where); 5627 return false; 5628 } 5629 else if (inquiry_argument && !first_actual_arg) 5630 { 5631 /* FIXME: It doesn't work reliably as inquiry_argument is not set 5632 for all inquiry functions in resolve_function; the reason is 5633 that the function-name resolution happens too late in that 5634 function. */ 5635 gfc_error ("Assumed-rank variable %s at %L as actual argument " 5636 "to an inquiry function shall be the first argument", 5637 sym->name, &e->where); 5638 return false; 5639 } 5640 } 5641 5642 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref 5643 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL 5644 && e->ref->next == NULL)) 5645 { 5646 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have " 5647 "a subobject reference", sym->name, &e->ref->u.ar.where); 5648 return false; 5649 } 5650 /* TS 29113, 407b. */ 5651 else if (e->ts.type == BT_ASSUMED && e->ref 5652 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL 5653 && e->ref->next == NULL)) 5654 { 5655 gfc_error ("Assumed-type variable %s at %L shall not have a subobject " 5656 "reference", sym->name, &e->ref->u.ar.where); 5657 return false; 5658 } 5659 5660 /* TS 29113, C535b. */ 5661 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok 5662 && sym->ts.u.derived && CLASS_DATA (sym) 5663 && CLASS_DATA (sym)->as 5664 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK) 5665 || (sym->ts.type != BT_CLASS && sym->as 5666 && sym->as->type == AS_ASSUMED_RANK)) 5667 && e->ref 5668 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL 5669 && e->ref->next == NULL)) 5670 { 5671 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject " 5672 "reference", sym->name, &e->ref->u.ar.where); 5673 return false; 5674 } 5675 5676 /* For variables that are used in an associate (target => object) where 5677 the object's basetype is array valued while the target is scalar, 5678 the ts' type of the component refs is still array valued, which 5679 can't be translated that way. */ 5680 if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS 5681 && sym->assoc->target && sym->assoc->target->ts.type == BT_CLASS 5682 && sym->assoc->target->ts.u.derived 5683 && CLASS_DATA (sym->assoc->target) 5684 && CLASS_DATA (sym->assoc->target)->as) 5685 { 5686 gfc_ref *ref = e->ref; 5687 while (ref) 5688 { 5689 switch (ref->type) 5690 { 5691 case REF_COMPONENT: 5692 ref->u.c.sym = sym->ts.u.derived; 5693 /* Stop the loop. */ 5694 ref = NULL; 5695 break; 5696 default: 5697 ref = ref->next; 5698 break; 5699 } 5700 } 5701 } 5702 5703 /* If this is an associate-name, it may be parsed with an array reference 5704 in error even though the target is scalar. Fail directly in this case. 5705 TODO Understand why class scalar expressions must be excluded. */ 5706 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0)) 5707 { 5708 if (sym->ts.type == BT_CLASS) 5709 gfc_fix_class_refs (e); 5710 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY) 5711 return false; 5712 else if (sym->attr.dimension && (!e->ref || e->ref->type != REF_ARRAY)) 5713 { 5714 /* This can happen because the parser did not detect that the 5715 associate name is an array and the expression had no array 5716 part_ref. */ 5717 gfc_ref *ref = gfc_get_ref (); 5718 ref->type = REF_ARRAY; 5719 ref->u.ar = *gfc_get_array_ref(); 5720 ref->u.ar.type = AR_FULL; 5721 if (sym->as) 5722 { 5723 ref->u.ar.as = sym->as; 5724 ref->u.ar.dimen = sym->as->rank; 5725 } 5726 ref->next = e->ref; 5727 e->ref = ref; 5728 5729 } 5730 } 5731 5732 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic) 5733 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived); 5734 5735 /* On the other hand, the parser may not have known this is an array; 5736 in this case, we have to add a FULL reference. */ 5737 if (sym->assoc && sym->attr.dimension && !e->ref) 5738 { 5739 e->ref = gfc_get_ref (); 5740 e->ref->type = REF_ARRAY; 5741 e->ref->u.ar.type = AR_FULL; 5742 e->ref->u.ar.dimen = 0; 5743 } 5744 5745 /* Like above, but for class types, where the checking whether an array 5746 ref is present is more complicated. Furthermore make sure not to add 5747 the full array ref to _vptr or _len refs. */ 5748 if (sym->assoc && sym->ts.type == BT_CLASS && sym->ts.u.derived 5749 && CLASS_DATA (sym) 5750 && CLASS_DATA (sym)->attr.dimension 5751 && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype)) 5752 { 5753 gfc_ref *ref, *newref; 5754 5755 newref = gfc_get_ref (); 5756 newref->type = REF_ARRAY; 5757 newref->u.ar.type = AR_FULL; 5758 newref->u.ar.dimen = 0; 5759 /* Because this is an associate var and the first ref either is a ref to 5760 the _data component or not, no traversal of the ref chain is 5761 needed. The array ref needs to be inserted after the _data ref, 5762 or when that is not present, which may happend for polymorphic 5763 types, then at the first position. */ 5764 ref = e->ref; 5765 if (!ref) 5766 e->ref = newref; 5767 else if (ref->type == REF_COMPONENT 5768 && strcmp ("_data", ref->u.c.component->name) == 0) 5769 { 5770 if (!ref->next || ref->next->type != REF_ARRAY) 5771 { 5772 newref->next = ref->next; 5773 ref->next = newref; 5774 } 5775 else 5776 /* Array ref present already. */ 5777 gfc_free_ref_list (newref); 5778 } 5779 else if (ref->type == REF_ARRAY) 5780 /* Array ref present already. */ 5781 gfc_free_ref_list (newref); 5782 else 5783 { 5784 newref->next = ref; 5785 e->ref = newref; 5786 } 5787 } 5788 5789 if (e->ref && !gfc_resolve_ref (e)) 5790 return false; 5791 5792 if (sym->attr.flavor == FL_PROCEDURE 5793 && (!sym->attr.function 5794 || (sym->attr.function && sym->result 5795 && sym->result->attr.proc_pointer 5796 && !sym->result->attr.function))) 5797 { 5798 e->ts.type = BT_PROCEDURE; 5799 goto resolve_procedure; 5800 } 5801 5802 if (sym->ts.type != BT_UNKNOWN) 5803 gfc_variable_attr (e, &e->ts); 5804 else if (sym->attr.flavor == FL_PROCEDURE 5805 && sym->attr.function && sym->result 5806 && sym->result->ts.type != BT_UNKNOWN 5807 && sym->result->attr.proc_pointer) 5808 e->ts = sym->result->ts; 5809 else 5810 { 5811 /* Must be a simple variable reference. */ 5812 if (!gfc_set_default_type (sym, 1, sym->ns)) 5813 return false; 5814 e->ts = sym->ts; 5815 } 5816 5817 if (check_assumed_size_reference (sym, e)) 5818 return false; 5819 5820 /* Deal with forward references to entries during gfc_resolve_code, to 5821 satisfy, at least partially, 12.5.2.5. */ 5822 if (gfc_current_ns->entries 5823 && current_entry_id == sym->entry_id 5824 && cs_base 5825 && cs_base->current 5826 && cs_base->current->op != EXEC_ENTRY) 5827 { 5828 gfc_entry_list *entry; 5829 gfc_formal_arglist *formal; 5830 int n; 5831 bool seen, saved_specification_expr; 5832 5833 /* If the symbol is a dummy... */ 5834 if (sym->attr.dummy && sym->ns == gfc_current_ns) 5835 { 5836 entry = gfc_current_ns->entries; 5837 seen = false; 5838 5839 /* ...test if the symbol is a parameter of previous entries. */ 5840 for (; entry && entry->id <= current_entry_id; entry = entry->next) 5841 for (formal = entry->sym->formal; formal; formal = formal->next) 5842 { 5843 if (formal->sym && sym->name == formal->sym->name) 5844 { 5845 seen = true; 5846 break; 5847 } 5848 } 5849 5850 /* If it has not been seen as a dummy, this is an error. */ 5851 if (!seen) 5852 { 5853 if (specification_expr) 5854 gfc_error ("Variable %qs, used in a specification expression" 5855 ", is referenced at %L before the ENTRY statement " 5856 "in which it is a parameter", 5857 sym->name, &cs_base->current->loc); 5858 else 5859 gfc_error ("Variable %qs is used at %L before the ENTRY " 5860 "statement in which it is a parameter", 5861 sym->name, &cs_base->current->loc); 5862 t = false; 5863 } 5864 } 5865 5866 /* Now do the same check on the specification expressions. */ 5867 saved_specification_expr = specification_expr; 5868 specification_expr = true; 5869 if (sym->ts.type == BT_CHARACTER 5870 && !gfc_resolve_expr (sym->ts.u.cl->length)) 5871 t = false; 5872 5873 if (sym->as) 5874 for (n = 0; n < sym->as->rank; n++) 5875 { 5876 if (!gfc_resolve_expr (sym->as->lower[n])) 5877 t = false; 5878 if (!gfc_resolve_expr (sym->as->upper[n])) 5879 t = false; 5880 } 5881 specification_expr = saved_specification_expr; 5882 5883 if (t) 5884 /* Update the symbol's entry level. */ 5885 sym->entry_id = current_entry_id + 1; 5886 } 5887 5888 /* If a symbol has been host_associated mark it. This is used latter, 5889 to identify if aliasing is possible via host association. */ 5890 if (sym->attr.flavor == FL_VARIABLE 5891 && gfc_current_ns->parent 5892 && (gfc_current_ns->parent == sym->ns 5893 || (gfc_current_ns->parent->parent 5894 && gfc_current_ns->parent->parent == sym->ns))) 5895 sym->attr.host_assoc = 1; 5896 5897 if (gfc_current_ns->proc_name 5898 && sym->attr.dimension 5899 && (sym->ns != gfc_current_ns 5900 || sym->attr.use_assoc 5901 || sym->attr.in_common)) 5902 gfc_current_ns->proc_name->attr.array_outer_dependency = 1; 5903 5904 resolve_procedure: 5905 if (t && !resolve_procedure_expression (e)) 5906 t = false; 5907 5908 /* F2008, C617 and C1229. */ 5909 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED) 5910 && gfc_is_coindexed (e)) 5911 { 5912 gfc_ref *ref, *ref2 = NULL; 5913 5914 for (ref = e->ref; ref; ref = ref->next) 5915 { 5916 if (ref->type == REF_COMPONENT) 5917 ref2 = ref; 5918 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0) 5919 break; 5920 } 5921 5922 for ( ; ref; ref = ref->next) 5923 if (ref->type == REF_COMPONENT) 5924 break; 5925 5926 /* Expression itself is not coindexed object. */ 5927 if (ref && e->ts.type == BT_CLASS) 5928 { 5929 gfc_error ("Polymorphic subobject of coindexed object at %L", 5930 &e->where); 5931 t = false; 5932 } 5933 5934 /* Expression itself is coindexed object. */ 5935 if (ref == NULL) 5936 { 5937 gfc_component *c; 5938 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components; 5939 for ( ; c; c = c->next) 5940 if (c->attr.allocatable && c->ts.type == BT_CLASS) 5941 { 5942 gfc_error ("Coindexed object with polymorphic allocatable " 5943 "subcomponent at %L", &e->where); 5944 t = false; 5945 break; 5946 } 5947 } 5948 } 5949 5950 if (t) 5951 gfc_expression_rank (e); 5952 5953 if (t && flag_coarray == GFC_FCOARRAY_LIB && gfc_is_coindexed (e)) 5954 add_caf_get_intrinsic (e); 5955 5956 /* Simplify cases where access to a parameter array results in a 5957 single constant. Suppress errors since those will have been 5958 issued before, as warnings. */ 5959 if (e->rank == 0 && sym->as && sym->attr.flavor == FL_PARAMETER) 5960 { 5961 gfc_push_suppress_errors (); 5962 gfc_simplify_expr (e, 1); 5963 gfc_pop_suppress_errors (); 5964 } 5965 5966 return t; 5967 } 5968 5969 5970 /* Checks to see that the correct symbol has been host associated. 5971 The only situation where this arises is that in which a twice 5972 contained function is parsed after the host association is made. 5973 Therefore, on detecting this, change the symbol in the expression 5974 and convert the array reference into an actual arglist if the old 5975 symbol is a variable. */ 5976 static bool 5977 check_host_association (gfc_expr *e) 5978 { 5979 gfc_symbol *sym, *old_sym; 5980 gfc_symtree *st; 5981 int n; 5982 gfc_ref *ref; 5983 gfc_actual_arglist *arg, *tail = NULL; 5984 bool retval = e->expr_type == EXPR_FUNCTION; 5985 5986 /* If the expression is the result of substitution in 5987 interface.c(gfc_extend_expr) because there is no way in 5988 which the host association can be wrong. */ 5989 if (e->symtree == NULL 5990 || e->symtree->n.sym == NULL 5991 || e->user_operator) 5992 return retval; 5993 5994 old_sym = e->symtree->n.sym; 5995 5996 if (gfc_current_ns->parent 5997 && old_sym->ns != gfc_current_ns) 5998 { 5999 /* Use the 'USE' name so that renamed module symbols are 6000 correctly handled. */ 6001 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym); 6002 6003 if (sym && old_sym != sym 6004 && sym->ts.type == old_sym->ts.type 6005 && sym->attr.flavor == FL_PROCEDURE 6006 && sym->attr.contained) 6007 { 6008 /* Clear the shape, since it might not be valid. */ 6009 gfc_free_shape (&e->shape, e->rank); 6010 6011 /* Give the expression the right symtree! */ 6012 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st); 6013 gcc_assert (st != NULL); 6014 6015 if (old_sym->attr.flavor == FL_PROCEDURE 6016 || e->expr_type == EXPR_FUNCTION) 6017 { 6018 /* Original was function so point to the new symbol, since 6019 the actual argument list is already attached to the 6020 expression. */ 6021 e->value.function.esym = NULL; 6022 e->symtree = st; 6023 } 6024 else 6025 { 6026 /* Original was variable so convert array references into 6027 an actual arglist. This does not need any checking now 6028 since resolve_function will take care of it. */ 6029 e->value.function.actual = NULL; 6030 e->expr_type = EXPR_FUNCTION; 6031 e->symtree = st; 6032 6033 /* Ambiguity will not arise if the array reference is not 6034 the last reference. */ 6035 for (ref = e->ref; ref; ref = ref->next) 6036 if (ref->type == REF_ARRAY && ref->next == NULL) 6037 break; 6038 6039 gcc_assert (ref->type == REF_ARRAY); 6040 6041 /* Grab the start expressions from the array ref and 6042 copy them into actual arguments. */ 6043 for (n = 0; n < ref->u.ar.dimen; n++) 6044 { 6045 arg = gfc_get_actual_arglist (); 6046 arg->expr = gfc_copy_expr (ref->u.ar.start[n]); 6047 if (e->value.function.actual == NULL) 6048 tail = e->value.function.actual = arg; 6049 else 6050 { 6051 tail->next = arg; 6052 tail = arg; 6053 } 6054 } 6055 6056 /* Dump the reference list and set the rank. */ 6057 gfc_free_ref_list (e->ref); 6058 e->ref = NULL; 6059 e->rank = sym->as ? sym->as->rank : 0; 6060 } 6061 6062 gfc_resolve_expr (e); 6063 sym->refs++; 6064 } 6065 } 6066 /* This might have changed! */ 6067 return e->expr_type == EXPR_FUNCTION; 6068 } 6069 6070 6071 static void 6072 gfc_resolve_character_operator (gfc_expr *e) 6073 { 6074 gfc_expr *op1 = e->value.op.op1; 6075 gfc_expr *op2 = e->value.op.op2; 6076 gfc_expr *e1 = NULL; 6077 gfc_expr *e2 = NULL; 6078 6079 gcc_assert (e->value.op.op == INTRINSIC_CONCAT); 6080 6081 if (op1->ts.u.cl && op1->ts.u.cl->length) 6082 e1 = gfc_copy_expr (op1->ts.u.cl->length); 6083 else if (op1->expr_type == EXPR_CONSTANT) 6084 e1 = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 6085 op1->value.character.length); 6086 6087 if (op2->ts.u.cl && op2->ts.u.cl->length) 6088 e2 = gfc_copy_expr (op2->ts.u.cl->length); 6089 else if (op2->expr_type == EXPR_CONSTANT) 6090 e2 = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 6091 op2->value.character.length); 6092 6093 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL); 6094 6095 if (!e1 || !e2) 6096 { 6097 gfc_free_expr (e1); 6098 gfc_free_expr (e2); 6099 6100 return; 6101 } 6102 6103 e->ts.u.cl->length = gfc_add (e1, e2); 6104 e->ts.u.cl->length->ts.type = BT_INTEGER; 6105 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind; 6106 gfc_simplify_expr (e->ts.u.cl->length, 0); 6107 gfc_resolve_expr (e->ts.u.cl->length); 6108 6109 return; 6110 } 6111 6112 6113 /* Ensure that an character expression has a charlen and, if possible, a 6114 length expression. */ 6115 6116 static void 6117 fixup_charlen (gfc_expr *e) 6118 { 6119 /* The cases fall through so that changes in expression type and the need 6120 for multiple fixes are picked up. In all circumstances, a charlen should 6121 be available for the middle end to hang a backend_decl on. */ 6122 switch (e->expr_type) 6123 { 6124 case EXPR_OP: 6125 gfc_resolve_character_operator (e); 6126 /* FALLTHRU */ 6127 6128 case EXPR_ARRAY: 6129 if (e->expr_type == EXPR_ARRAY) 6130 gfc_resolve_character_array_constructor (e); 6131 /* FALLTHRU */ 6132 6133 case EXPR_SUBSTRING: 6134 if (!e->ts.u.cl && e->ref) 6135 gfc_resolve_substring_charlen (e); 6136 /* FALLTHRU */ 6137 6138 default: 6139 if (!e->ts.u.cl) 6140 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL); 6141 6142 break; 6143 } 6144 } 6145 6146 6147 /* Update an actual argument to include the passed-object for type-bound 6148 procedures at the right position. */ 6149 6150 static gfc_actual_arglist* 6151 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos, 6152 const char *name) 6153 { 6154 gcc_assert (argpos > 0); 6155 6156 if (argpos == 1) 6157 { 6158 gfc_actual_arglist* result; 6159 6160 result = gfc_get_actual_arglist (); 6161 result->expr = po; 6162 result->next = lst; 6163 if (name) 6164 result->name = name; 6165 6166 return result; 6167 } 6168 6169 if (lst) 6170 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name); 6171 else 6172 lst = update_arglist_pass (NULL, po, argpos - 1, name); 6173 return lst; 6174 } 6175 6176 6177 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */ 6178 6179 static gfc_expr* 6180 extract_compcall_passed_object (gfc_expr* e) 6181 { 6182 gfc_expr* po; 6183 6184 if (e->expr_type == EXPR_UNKNOWN) 6185 { 6186 gfc_error ("Error in typebound call at %L", 6187 &e->where); 6188 return NULL; 6189 } 6190 6191 gcc_assert (e->expr_type == EXPR_COMPCALL); 6192 6193 if (e->value.compcall.base_object) 6194 po = gfc_copy_expr (e->value.compcall.base_object); 6195 else 6196 { 6197 po = gfc_get_expr (); 6198 po->expr_type = EXPR_VARIABLE; 6199 po->symtree = e->symtree; 6200 po->ref = gfc_copy_ref (e->ref); 6201 po->where = e->where; 6202 } 6203 6204 if (!gfc_resolve_expr (po)) 6205 return NULL; 6206 6207 return po; 6208 } 6209 6210 6211 /* Update the arglist of an EXPR_COMPCALL expression to include the 6212 passed-object. */ 6213 6214 static bool 6215 update_compcall_arglist (gfc_expr* e) 6216 { 6217 gfc_expr* po; 6218 gfc_typebound_proc* tbp; 6219 6220 tbp = e->value.compcall.tbp; 6221 6222 if (tbp->error) 6223 return false; 6224 6225 po = extract_compcall_passed_object (e); 6226 if (!po) 6227 return false; 6228 6229 if (tbp->nopass || e->value.compcall.ignore_pass) 6230 { 6231 gfc_free_expr (po); 6232 return true; 6233 } 6234 6235 if (tbp->pass_arg_num <= 0) 6236 return false; 6237 6238 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po, 6239 tbp->pass_arg_num, 6240 tbp->pass_arg); 6241 6242 return true; 6243 } 6244 6245 6246 /* Extract the passed object from a PPC call (a copy of it). */ 6247 6248 static gfc_expr* 6249 extract_ppc_passed_object (gfc_expr *e) 6250 { 6251 gfc_expr *po; 6252 gfc_ref **ref; 6253 6254 po = gfc_get_expr (); 6255 po->expr_type = EXPR_VARIABLE; 6256 po->symtree = e->symtree; 6257 po->ref = gfc_copy_ref (e->ref); 6258 po->where = e->where; 6259 6260 /* Remove PPC reference. */ 6261 ref = &po->ref; 6262 while ((*ref)->next) 6263 ref = &(*ref)->next; 6264 gfc_free_ref_list (*ref); 6265 *ref = NULL; 6266 6267 if (!gfc_resolve_expr (po)) 6268 return NULL; 6269 6270 return po; 6271 } 6272 6273 6274 /* Update the actual arglist of a procedure pointer component to include the 6275 passed-object. */ 6276 6277 static bool 6278 update_ppc_arglist (gfc_expr* e) 6279 { 6280 gfc_expr* po; 6281 gfc_component *ppc; 6282 gfc_typebound_proc* tb; 6283 6284 ppc = gfc_get_proc_ptr_comp (e); 6285 if (!ppc) 6286 return false; 6287 6288 tb = ppc->tb; 6289 6290 if (tb->error) 6291 return false; 6292 else if (tb->nopass) 6293 return true; 6294 6295 po = extract_ppc_passed_object (e); 6296 if (!po) 6297 return false; 6298 6299 /* F08:R739. */ 6300 if (po->rank != 0) 6301 { 6302 gfc_error ("Passed-object at %L must be scalar", &e->where); 6303 return false; 6304 } 6305 6306 /* F08:C611. */ 6307 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract) 6308 { 6309 gfc_error ("Base object for procedure-pointer component call at %L is of" 6310 " ABSTRACT type %qs", &e->where, po->ts.u.derived->name); 6311 return false; 6312 } 6313 6314 gcc_assert (tb->pass_arg_num > 0); 6315 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po, 6316 tb->pass_arg_num, 6317 tb->pass_arg); 6318 6319 return true; 6320 } 6321 6322 6323 /* Check that the object a TBP is called on is valid, i.e. it must not be 6324 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */ 6325 6326 static bool 6327 check_typebound_baseobject (gfc_expr* e) 6328 { 6329 gfc_expr* base; 6330 bool return_value = false; 6331 6332 base = extract_compcall_passed_object (e); 6333 if (!base) 6334 return false; 6335 6336 if (base->ts.type != BT_DERIVED && base->ts.type != BT_CLASS) 6337 { 6338 gfc_error ("Error in typebound call at %L", &e->where); 6339 goto cleanup; 6340 } 6341 6342 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok) 6343 return false; 6344 6345 /* F08:C611. */ 6346 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract) 6347 { 6348 gfc_error ("Base object for type-bound procedure call at %L is of" 6349 " ABSTRACT type %qs", &e->where, base->ts.u.derived->name); 6350 goto cleanup; 6351 } 6352 6353 /* F08:C1230. If the procedure called is NOPASS, 6354 the base object must be scalar. */ 6355 if (e->value.compcall.tbp->nopass && base->rank != 0) 6356 { 6357 gfc_error ("Base object for NOPASS type-bound procedure call at %L must" 6358 " be scalar", &e->where); 6359 goto cleanup; 6360 } 6361 6362 return_value = true; 6363 6364 cleanup: 6365 gfc_free_expr (base); 6366 return return_value; 6367 } 6368 6369 6370 /* Resolve a call to a type-bound procedure, either function or subroutine, 6371 statically from the data in an EXPR_COMPCALL expression. The adapted 6372 arglist and the target-procedure symtree are returned. */ 6373 6374 static bool 6375 resolve_typebound_static (gfc_expr* e, gfc_symtree** target, 6376 gfc_actual_arglist** actual) 6377 { 6378 gcc_assert (e->expr_type == EXPR_COMPCALL); 6379 gcc_assert (!e->value.compcall.tbp->is_generic); 6380 6381 /* Update the actual arglist for PASS. */ 6382 if (!update_compcall_arglist (e)) 6383 return false; 6384 6385 *actual = e->value.compcall.actual; 6386 *target = e->value.compcall.tbp->u.specific; 6387 6388 gfc_free_ref_list (e->ref); 6389 e->ref = NULL; 6390 e->value.compcall.actual = NULL; 6391 6392 /* If we find a deferred typebound procedure, check for derived types 6393 that an overriding typebound procedure has not been missed. */ 6394 if (e->value.compcall.name 6395 && !e->value.compcall.tbp->non_overridable 6396 && e->value.compcall.base_object 6397 && e->value.compcall.base_object->ts.type == BT_DERIVED) 6398 { 6399 gfc_symtree *st; 6400 gfc_symbol *derived; 6401 6402 /* Use the derived type of the base_object. */ 6403 derived = e->value.compcall.base_object->ts.u.derived; 6404 st = NULL; 6405 6406 /* If necessary, go through the inheritance chain. */ 6407 while (!st && derived) 6408 { 6409 /* Look for the typebound procedure 'name'. */ 6410 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root) 6411 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root, 6412 e->value.compcall.name); 6413 if (!st) 6414 derived = gfc_get_derived_super_type (derived); 6415 } 6416 6417 /* Now find the specific name in the derived type namespace. */ 6418 if (st && st->n.tb && st->n.tb->u.specific) 6419 gfc_find_sym_tree (st->n.tb->u.specific->name, 6420 derived->ns, 1, &st); 6421 if (st) 6422 *target = st; 6423 } 6424 return true; 6425 } 6426 6427 6428 /* Get the ultimate declared type from an expression. In addition, 6429 return the last class/derived type reference and the copy of the 6430 reference list. If check_types is set true, derived types are 6431 identified as well as class references. */ 6432 static gfc_symbol* 6433 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref, 6434 gfc_expr *e, bool check_types) 6435 { 6436 gfc_symbol *declared; 6437 gfc_ref *ref; 6438 6439 declared = NULL; 6440 if (class_ref) 6441 *class_ref = NULL; 6442 if (new_ref) 6443 *new_ref = gfc_copy_ref (e->ref); 6444 6445 for (ref = e->ref; ref; ref = ref->next) 6446 { 6447 if (ref->type != REF_COMPONENT) 6448 continue; 6449 6450 if ((ref->u.c.component->ts.type == BT_CLASS 6451 || (check_types && gfc_bt_struct (ref->u.c.component->ts.type))) 6452 && ref->u.c.component->attr.flavor != FL_PROCEDURE) 6453 { 6454 declared = ref->u.c.component->ts.u.derived; 6455 if (class_ref) 6456 *class_ref = ref; 6457 } 6458 } 6459 6460 if (declared == NULL) 6461 declared = e->symtree->n.sym->ts.u.derived; 6462 6463 return declared; 6464 } 6465 6466 6467 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out 6468 which of the specific bindings (if any) matches the arglist and transform 6469 the expression into a call of that binding. */ 6470 6471 static bool 6472 resolve_typebound_generic_call (gfc_expr* e, const char **name) 6473 { 6474 gfc_typebound_proc* genproc; 6475 const char* genname; 6476 gfc_symtree *st; 6477 gfc_symbol *derived; 6478 6479 gcc_assert (e->expr_type == EXPR_COMPCALL); 6480 genname = e->value.compcall.name; 6481 genproc = e->value.compcall.tbp; 6482 6483 if (!genproc->is_generic) 6484 return true; 6485 6486 /* Try the bindings on this type and in the inheritance hierarchy. */ 6487 for (; genproc; genproc = genproc->overridden) 6488 { 6489 gfc_tbp_generic* g; 6490 6491 gcc_assert (genproc->is_generic); 6492 for (g = genproc->u.generic; g; g = g->next) 6493 { 6494 gfc_symbol* target; 6495 gfc_actual_arglist* args; 6496 bool matches; 6497 6498 gcc_assert (g->specific); 6499 6500 if (g->specific->error) 6501 continue; 6502 6503 target = g->specific->u.specific->n.sym; 6504 6505 /* Get the right arglist by handling PASS/NOPASS. */ 6506 args = gfc_copy_actual_arglist (e->value.compcall.actual); 6507 if (!g->specific->nopass) 6508 { 6509 gfc_expr* po; 6510 po = extract_compcall_passed_object (e); 6511 if (!po) 6512 { 6513 gfc_free_actual_arglist (args); 6514 return false; 6515 } 6516 6517 gcc_assert (g->specific->pass_arg_num > 0); 6518 gcc_assert (!g->specific->error); 6519 args = update_arglist_pass (args, po, g->specific->pass_arg_num, 6520 g->specific->pass_arg); 6521 } 6522 resolve_actual_arglist (args, target->attr.proc, 6523 is_external_proc (target) 6524 && gfc_sym_get_dummy_args (target) == NULL); 6525 6526 /* Check if this arglist matches the formal. */ 6527 matches = gfc_arglist_matches_symbol (&args, target); 6528 6529 /* Clean up and break out of the loop if we've found it. */ 6530 gfc_free_actual_arglist (args); 6531 if (matches) 6532 { 6533 e->value.compcall.tbp = g->specific; 6534 genname = g->specific_st->name; 6535 /* Pass along the name for CLASS methods, where the vtab 6536 procedure pointer component has to be referenced. */ 6537 if (name) 6538 *name = genname; 6539 goto success; 6540 } 6541 } 6542 } 6543 6544 /* Nothing matching found! */ 6545 gfc_error ("Found no matching specific binding for the call to the GENERIC" 6546 " %qs at %L", genname, &e->where); 6547 return false; 6548 6549 success: 6550 /* Make sure that we have the right specific instance for the name. */ 6551 derived = get_declared_from_expr (NULL, NULL, e, true); 6552 6553 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where); 6554 if (st) 6555 e->value.compcall.tbp = st->n.tb; 6556 6557 return true; 6558 } 6559 6560 6561 /* Resolve a call to a type-bound subroutine. */ 6562 6563 static bool 6564 resolve_typebound_call (gfc_code* c, const char **name, bool *overridable) 6565 { 6566 gfc_actual_arglist* newactual; 6567 gfc_symtree* target; 6568 6569 /* Check that's really a SUBROUTINE. */ 6570 if (!c->expr1->value.compcall.tbp->subroutine) 6571 { 6572 if (!c->expr1->value.compcall.tbp->is_generic 6573 && c->expr1->value.compcall.tbp->u.specific 6574 && c->expr1->value.compcall.tbp->u.specific->n.sym 6575 && c->expr1->value.compcall.tbp->u.specific->n.sym->attr.subroutine) 6576 c->expr1->value.compcall.tbp->subroutine = 1; 6577 else 6578 { 6579 gfc_error ("%qs at %L should be a SUBROUTINE", 6580 c->expr1->value.compcall.name, &c->loc); 6581 return false; 6582 } 6583 } 6584 6585 if (!check_typebound_baseobject (c->expr1)) 6586 return false; 6587 6588 /* Pass along the name for CLASS methods, where the vtab 6589 procedure pointer component has to be referenced. */ 6590 if (name) 6591 *name = c->expr1->value.compcall.name; 6592 6593 if (!resolve_typebound_generic_call (c->expr1, name)) 6594 return false; 6595 6596 /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */ 6597 if (overridable) 6598 *overridable = !c->expr1->value.compcall.tbp->non_overridable; 6599 6600 /* Transform into an ordinary EXEC_CALL for now. */ 6601 6602 if (!resolve_typebound_static (c->expr1, &target, &newactual)) 6603 return false; 6604 6605 c->ext.actual = newactual; 6606 c->symtree = target; 6607 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL); 6608 6609 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual); 6610 6611 gfc_free_expr (c->expr1); 6612 c->expr1 = gfc_get_expr (); 6613 c->expr1->expr_type = EXPR_FUNCTION; 6614 c->expr1->symtree = target; 6615 c->expr1->where = c->loc; 6616 6617 return resolve_call (c); 6618 } 6619 6620 6621 /* Resolve a component-call expression. */ 6622 static bool 6623 resolve_compcall (gfc_expr* e, const char **name) 6624 { 6625 gfc_actual_arglist* newactual; 6626 gfc_symtree* target; 6627 6628 /* Check that's really a FUNCTION. */ 6629 if (!e->value.compcall.tbp->function) 6630 { 6631 gfc_error ("%qs at %L should be a FUNCTION", 6632 e->value.compcall.name, &e->where); 6633 return false; 6634 } 6635 6636 6637 /* These must not be assign-calls! */ 6638 gcc_assert (!e->value.compcall.assign); 6639 6640 if (!check_typebound_baseobject (e)) 6641 return false; 6642 6643 /* Pass along the name for CLASS methods, where the vtab 6644 procedure pointer component has to be referenced. */ 6645 if (name) 6646 *name = e->value.compcall.name; 6647 6648 if (!resolve_typebound_generic_call (e, name)) 6649 return false; 6650 gcc_assert (!e->value.compcall.tbp->is_generic); 6651 6652 /* Take the rank from the function's symbol. */ 6653 if (e->value.compcall.tbp->u.specific->n.sym->as) 6654 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank; 6655 6656 /* For now, we simply transform it into an EXPR_FUNCTION call with the same 6657 arglist to the TBP's binding target. */ 6658 6659 if (!resolve_typebound_static (e, &target, &newactual)) 6660 return false; 6661 6662 e->value.function.actual = newactual; 6663 e->value.function.name = NULL; 6664 e->value.function.esym = target->n.sym; 6665 e->value.function.isym = NULL; 6666 e->symtree = target; 6667 e->ts = target->n.sym->ts; 6668 e->expr_type = EXPR_FUNCTION; 6669 6670 /* Resolution is not necessary if this is a class subroutine; this 6671 function only has to identify the specific proc. Resolution of 6672 the call will be done next in resolve_typebound_call. */ 6673 return gfc_resolve_expr (e); 6674 } 6675 6676 6677 static bool resolve_fl_derived (gfc_symbol *sym); 6678 6679 6680 /* Resolve a typebound function, or 'method'. First separate all 6681 the non-CLASS references by calling resolve_compcall directly. */ 6682 6683 static bool 6684 resolve_typebound_function (gfc_expr* e) 6685 { 6686 gfc_symbol *declared; 6687 gfc_component *c; 6688 gfc_ref *new_ref; 6689 gfc_ref *class_ref; 6690 gfc_symtree *st; 6691 const char *name; 6692 gfc_typespec ts; 6693 gfc_expr *expr; 6694 bool overridable; 6695 6696 st = e->symtree; 6697 6698 /* Deal with typebound operators for CLASS objects. */ 6699 expr = e->value.compcall.base_object; 6700 overridable = !e->value.compcall.tbp->non_overridable; 6701 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name) 6702 { 6703 /* Since the typebound operators are generic, we have to ensure 6704 that any delays in resolution are corrected and that the vtab 6705 is present. */ 6706 ts = expr->ts; 6707 declared = ts.u.derived; 6708 c = gfc_find_component (declared, "_vptr", true, true, NULL); 6709 if (c->ts.u.derived == NULL) 6710 c->ts.u.derived = gfc_find_derived_vtab (declared); 6711 6712 if (!resolve_compcall (e, &name)) 6713 return false; 6714 6715 /* Use the generic name if it is there. */ 6716 name = name ? name : e->value.function.esym->name; 6717 e->symtree = expr->symtree; 6718 e->ref = gfc_copy_ref (expr->ref); 6719 get_declared_from_expr (&class_ref, NULL, e, false); 6720 6721 /* Trim away the extraneous references that emerge from nested 6722 use of interface.c (extend_expr). */ 6723 if (class_ref && class_ref->next) 6724 { 6725 gfc_free_ref_list (class_ref->next); 6726 class_ref->next = NULL; 6727 } 6728 else if (e->ref && !class_ref && expr->ts.type != BT_CLASS) 6729 { 6730 gfc_free_ref_list (e->ref); 6731 e->ref = NULL; 6732 } 6733 6734 gfc_add_vptr_component (e); 6735 gfc_add_component_ref (e, name); 6736 e->value.function.esym = NULL; 6737 if (expr->expr_type != EXPR_VARIABLE) 6738 e->base_expr = expr; 6739 return true; 6740 } 6741 6742 if (st == NULL) 6743 return resolve_compcall (e, NULL); 6744 6745 if (!gfc_resolve_ref (e)) 6746 return false; 6747 6748 /* Get the CLASS declared type. */ 6749 declared = get_declared_from_expr (&class_ref, &new_ref, e, true); 6750 6751 if (!resolve_fl_derived (declared)) 6752 return false; 6753 6754 /* Weed out cases of the ultimate component being a derived type. */ 6755 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type)) 6756 || (!class_ref && st->n.sym->ts.type != BT_CLASS)) 6757 { 6758 gfc_free_ref_list (new_ref); 6759 return resolve_compcall (e, NULL); 6760 } 6761 6762 c = gfc_find_component (declared, "_data", true, true, NULL); 6763 6764 /* Treat the call as if it is a typebound procedure, in order to roll 6765 out the correct name for the specific function. */ 6766 if (!resolve_compcall (e, &name)) 6767 { 6768 gfc_free_ref_list (new_ref); 6769 return false; 6770 } 6771 ts = e->ts; 6772 6773 if (overridable) 6774 { 6775 /* Convert the expression to a procedure pointer component call. */ 6776 e->value.function.esym = NULL; 6777 e->symtree = st; 6778 6779 if (new_ref) 6780 e->ref = new_ref; 6781 6782 /* '_vptr' points to the vtab, which contains the procedure pointers. */ 6783 gfc_add_vptr_component (e); 6784 gfc_add_component_ref (e, name); 6785 6786 /* Recover the typespec for the expression. This is really only 6787 necessary for generic procedures, where the additional call 6788 to gfc_add_component_ref seems to throw the collection of the 6789 correct typespec. */ 6790 e->ts = ts; 6791 } 6792 else if (new_ref) 6793 gfc_free_ref_list (new_ref); 6794 6795 return true; 6796 } 6797 6798 /* Resolve a typebound subroutine, or 'method'. First separate all 6799 the non-CLASS references by calling resolve_typebound_call 6800 directly. */ 6801 6802 static bool 6803 resolve_typebound_subroutine (gfc_code *code) 6804 { 6805 gfc_symbol *declared; 6806 gfc_component *c; 6807 gfc_ref *new_ref; 6808 gfc_ref *class_ref; 6809 gfc_symtree *st; 6810 const char *name; 6811 gfc_typespec ts; 6812 gfc_expr *expr; 6813 bool overridable; 6814 6815 st = code->expr1->symtree; 6816 6817 /* Deal with typebound operators for CLASS objects. */ 6818 expr = code->expr1->value.compcall.base_object; 6819 overridable = !code->expr1->value.compcall.tbp->non_overridable; 6820 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name) 6821 { 6822 /* If the base_object is not a variable, the corresponding actual 6823 argument expression must be stored in e->base_expression so 6824 that the corresponding tree temporary can be used as the base 6825 object in gfc_conv_procedure_call. */ 6826 if (expr->expr_type != EXPR_VARIABLE) 6827 { 6828 gfc_actual_arglist *args; 6829 6830 args= code->expr1->value.function.actual; 6831 for (; args; args = args->next) 6832 if (expr == args->expr) 6833 expr = args->expr; 6834 } 6835 6836 /* Since the typebound operators are generic, we have to ensure 6837 that any delays in resolution are corrected and that the vtab 6838 is present. */ 6839 declared = expr->ts.u.derived; 6840 c = gfc_find_component (declared, "_vptr", true, true, NULL); 6841 if (c->ts.u.derived == NULL) 6842 c->ts.u.derived = gfc_find_derived_vtab (declared); 6843 6844 if (!resolve_typebound_call (code, &name, NULL)) 6845 return false; 6846 6847 /* Use the generic name if it is there. */ 6848 name = name ? name : code->expr1->value.function.esym->name; 6849 code->expr1->symtree = expr->symtree; 6850 code->expr1->ref = gfc_copy_ref (expr->ref); 6851 6852 /* Trim away the extraneous references that emerge from nested 6853 use of interface.c (extend_expr). */ 6854 get_declared_from_expr (&class_ref, NULL, code->expr1, false); 6855 if (class_ref && class_ref->next) 6856 { 6857 gfc_free_ref_list (class_ref->next); 6858 class_ref->next = NULL; 6859 } 6860 else if (code->expr1->ref && !class_ref) 6861 { 6862 gfc_free_ref_list (code->expr1->ref); 6863 code->expr1->ref = NULL; 6864 } 6865 6866 /* Now use the procedure in the vtable. */ 6867 gfc_add_vptr_component (code->expr1); 6868 gfc_add_component_ref (code->expr1, name); 6869 code->expr1->value.function.esym = NULL; 6870 if (expr->expr_type != EXPR_VARIABLE) 6871 code->expr1->base_expr = expr; 6872 return true; 6873 } 6874 6875 if (st == NULL) 6876 return resolve_typebound_call (code, NULL, NULL); 6877 6878 if (!gfc_resolve_ref (code->expr1)) 6879 return false; 6880 6881 /* Get the CLASS declared type. */ 6882 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true); 6883 6884 /* Weed out cases of the ultimate component being a derived type. */ 6885 if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type)) 6886 || (!class_ref && st->n.sym->ts.type != BT_CLASS)) 6887 { 6888 gfc_free_ref_list (new_ref); 6889 return resolve_typebound_call (code, NULL, NULL); 6890 } 6891 6892 if (!resolve_typebound_call (code, &name, &overridable)) 6893 { 6894 gfc_free_ref_list (new_ref); 6895 return false; 6896 } 6897 ts = code->expr1->ts; 6898 6899 if (overridable) 6900 { 6901 /* Convert the expression to a procedure pointer component call. */ 6902 code->expr1->value.function.esym = NULL; 6903 code->expr1->symtree = st; 6904 6905 if (new_ref) 6906 code->expr1->ref = new_ref; 6907 6908 /* '_vptr' points to the vtab, which contains the procedure pointers. */ 6909 gfc_add_vptr_component (code->expr1); 6910 gfc_add_component_ref (code->expr1, name); 6911 6912 /* Recover the typespec for the expression. This is really only 6913 necessary for generic procedures, where the additional call 6914 to gfc_add_component_ref seems to throw the collection of the 6915 correct typespec. */ 6916 code->expr1->ts = ts; 6917 } 6918 else if (new_ref) 6919 gfc_free_ref_list (new_ref); 6920 6921 return true; 6922 } 6923 6924 6925 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */ 6926 6927 static bool 6928 resolve_ppc_call (gfc_code* c) 6929 { 6930 gfc_component *comp; 6931 6932 comp = gfc_get_proc_ptr_comp (c->expr1); 6933 gcc_assert (comp != NULL); 6934 6935 c->resolved_sym = c->expr1->symtree->n.sym; 6936 c->expr1->expr_type = EXPR_VARIABLE; 6937 6938 if (!comp->attr.subroutine) 6939 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where); 6940 6941 if (!gfc_resolve_ref (c->expr1)) 6942 return false; 6943 6944 if (!update_ppc_arglist (c->expr1)) 6945 return false; 6946 6947 c->ext.actual = c->expr1->value.compcall.actual; 6948 6949 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc, 6950 !(comp->ts.interface 6951 && comp->ts.interface->formal))) 6952 return false; 6953 6954 if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where)) 6955 return false; 6956 6957 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where); 6958 6959 return true; 6960 } 6961 6962 6963 /* Resolve a Function Call to a Procedure Pointer Component (Function). */ 6964 6965 static bool 6966 resolve_expr_ppc (gfc_expr* e) 6967 { 6968 gfc_component *comp; 6969 6970 comp = gfc_get_proc_ptr_comp (e); 6971 gcc_assert (comp != NULL); 6972 6973 /* Convert to EXPR_FUNCTION. */ 6974 e->expr_type = EXPR_FUNCTION; 6975 e->value.function.isym = NULL; 6976 e->value.function.actual = e->value.compcall.actual; 6977 e->ts = comp->ts; 6978 if (comp->as != NULL) 6979 e->rank = comp->as->rank; 6980 6981 if (!comp->attr.function) 6982 gfc_add_function (&comp->attr, comp->name, &e->where); 6983 6984 if (!gfc_resolve_ref (e)) 6985 return false; 6986 6987 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc, 6988 !(comp->ts.interface 6989 && comp->ts.interface->formal))) 6990 return false; 6991 6992 if (!update_ppc_arglist (e)) 6993 return false; 6994 6995 if (!check_pure_function(e)) 6996 return false; 6997 6998 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where); 6999 7000 return true; 7001 } 7002 7003 7004 static bool 7005 gfc_is_expandable_expr (gfc_expr *e) 7006 { 7007 gfc_constructor *con; 7008 7009 if (e->expr_type == EXPR_ARRAY) 7010 { 7011 /* Traverse the constructor looking for variables that are flavor 7012 parameter. Parameters must be expanded since they are fully used at 7013 compile time. */ 7014 con = gfc_constructor_first (e->value.constructor); 7015 for (; con; con = gfc_constructor_next (con)) 7016 { 7017 if (con->expr->expr_type == EXPR_VARIABLE 7018 && con->expr->symtree 7019 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER 7020 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE)) 7021 return true; 7022 if (con->expr->expr_type == EXPR_ARRAY 7023 && gfc_is_expandable_expr (con->expr)) 7024 return true; 7025 } 7026 } 7027 7028 return false; 7029 } 7030 7031 7032 /* Sometimes variables in specification expressions of the result 7033 of module procedures in submodules wind up not being the 'real' 7034 dummy. Find this, if possible, in the namespace of the first 7035 formal argument. */ 7036 7037 static void 7038 fixup_unique_dummy (gfc_expr *e) 7039 { 7040 gfc_symtree *st = NULL; 7041 gfc_symbol *s = NULL; 7042 7043 if (e->symtree->n.sym->ns->proc_name 7044 && e->symtree->n.sym->ns->proc_name->formal) 7045 s = e->symtree->n.sym->ns->proc_name->formal->sym; 7046 7047 if (s != NULL) 7048 st = gfc_find_symtree (s->ns->sym_root, e->symtree->n.sym->name); 7049 7050 if (st != NULL 7051 && st->n.sym != NULL 7052 && st->n.sym->attr.dummy) 7053 e->symtree = st; 7054 } 7055 7056 /* Resolve an expression. That is, make sure that types of operands agree 7057 with their operators, intrinsic operators are converted to function calls 7058 for overloaded types and unresolved function references are resolved. */ 7059 7060 bool 7061 gfc_resolve_expr (gfc_expr *e) 7062 { 7063 bool t; 7064 bool inquiry_save, actual_arg_save, first_actual_arg_save; 7065 7066 if (e == NULL || e->do_not_resolve_again) 7067 return true; 7068 7069 /* inquiry_argument only applies to variables. */ 7070 inquiry_save = inquiry_argument; 7071 actual_arg_save = actual_arg; 7072 first_actual_arg_save = first_actual_arg; 7073 7074 if (e->expr_type != EXPR_VARIABLE) 7075 { 7076 inquiry_argument = false; 7077 actual_arg = false; 7078 first_actual_arg = false; 7079 } 7080 else if (e->symtree != NULL 7081 && *e->symtree->name == '@' 7082 && e->symtree->n.sym->attr.dummy) 7083 { 7084 /* Deal with submodule specification expressions that are not 7085 found to be referenced in module.c(read_cleanup). */ 7086 fixup_unique_dummy (e); 7087 } 7088 7089 switch (e->expr_type) 7090 { 7091 case EXPR_OP: 7092 t = resolve_operator (e); 7093 break; 7094 7095 case EXPR_FUNCTION: 7096 case EXPR_VARIABLE: 7097 7098 if (check_host_association (e)) 7099 t = resolve_function (e); 7100 else 7101 t = resolve_variable (e); 7102 7103 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref 7104 && e->ref->type != REF_SUBSTRING) 7105 gfc_resolve_substring_charlen (e); 7106 7107 break; 7108 7109 case EXPR_COMPCALL: 7110 t = resolve_typebound_function (e); 7111 break; 7112 7113 case EXPR_SUBSTRING: 7114 t = gfc_resolve_ref (e); 7115 break; 7116 7117 case EXPR_CONSTANT: 7118 case EXPR_NULL: 7119 t = true; 7120 break; 7121 7122 case EXPR_PPC: 7123 t = resolve_expr_ppc (e); 7124 break; 7125 7126 case EXPR_ARRAY: 7127 t = false; 7128 if (!gfc_resolve_ref (e)) 7129 break; 7130 7131 t = gfc_resolve_array_constructor (e); 7132 /* Also try to expand a constructor. */ 7133 if (t) 7134 { 7135 gfc_expression_rank (e); 7136 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e)) 7137 gfc_expand_constructor (e, false); 7138 } 7139 7140 /* This provides the opportunity for the length of constructors with 7141 character valued function elements to propagate the string length 7142 to the expression. */ 7143 if (t && e->ts.type == BT_CHARACTER) 7144 { 7145 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER 7146 here rather then add a duplicate test for it above. */ 7147 gfc_expand_constructor (e, false); 7148 t = gfc_resolve_character_array_constructor (e); 7149 } 7150 7151 break; 7152 7153 case EXPR_STRUCTURE: 7154 t = gfc_resolve_ref (e); 7155 if (!t) 7156 break; 7157 7158 t = resolve_structure_cons (e, 0); 7159 if (!t) 7160 break; 7161 7162 t = gfc_simplify_expr (e, 0); 7163 break; 7164 7165 default: 7166 gfc_internal_error ("gfc_resolve_expr(): Bad expression type"); 7167 } 7168 7169 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl) 7170 fixup_charlen (e); 7171 7172 inquiry_argument = inquiry_save; 7173 actual_arg = actual_arg_save; 7174 first_actual_arg = first_actual_arg_save; 7175 7176 /* For some reason, resolving these expressions a second time mangles 7177 the typespec of the expression itself. */ 7178 if (t && e->expr_type == EXPR_VARIABLE 7179 && e->symtree->n.sym->attr.select_rank_temporary 7180 && UNLIMITED_POLY (e->symtree->n.sym)) 7181 e->do_not_resolve_again = 1; 7182 7183 return t; 7184 } 7185 7186 7187 /* Resolve an expression from an iterator. They must be scalar and have 7188 INTEGER or (optionally) REAL type. */ 7189 7190 static bool 7191 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok, 7192 const char *name_msgid) 7193 { 7194 if (!gfc_resolve_expr (expr)) 7195 return false; 7196 7197 if (expr->rank != 0) 7198 { 7199 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where); 7200 return false; 7201 } 7202 7203 if (expr->ts.type != BT_INTEGER) 7204 { 7205 if (expr->ts.type == BT_REAL) 7206 { 7207 if (real_ok) 7208 return gfc_notify_std (GFC_STD_F95_DEL, 7209 "%s at %L must be integer", 7210 _(name_msgid), &expr->where); 7211 else 7212 { 7213 gfc_error ("%s at %L must be INTEGER", _(name_msgid), 7214 &expr->where); 7215 return false; 7216 } 7217 } 7218 else 7219 { 7220 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where); 7221 return false; 7222 } 7223 } 7224 return true; 7225 } 7226 7227 7228 /* Resolve the expressions in an iterator structure. If REAL_OK is 7229 false allow only INTEGER type iterators, otherwise allow REAL types. 7230 Set own_scope to true for ac-implied-do and data-implied-do as those 7231 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */ 7232 7233 bool 7234 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope) 7235 { 7236 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")) 7237 return false; 7238 7239 if (!gfc_check_vardef_context (iter->var, false, false, own_scope, 7240 _("iterator variable"))) 7241 return false; 7242 7243 if (!gfc_resolve_iterator_expr (iter->start, real_ok, 7244 "Start expression in DO loop")) 7245 return false; 7246 7247 if (!gfc_resolve_iterator_expr (iter->end, real_ok, 7248 "End expression in DO loop")) 7249 return false; 7250 7251 if (!gfc_resolve_iterator_expr (iter->step, real_ok, 7252 "Step expression in DO loop")) 7253 return false; 7254 7255 /* Convert start, end, and step to the same type as var. */ 7256 if (iter->start->ts.kind != iter->var->ts.kind 7257 || iter->start->ts.type != iter->var->ts.type) 7258 gfc_convert_type (iter->start, &iter->var->ts, 1); 7259 7260 if (iter->end->ts.kind != iter->var->ts.kind 7261 || iter->end->ts.type != iter->var->ts.type) 7262 gfc_convert_type (iter->end, &iter->var->ts, 1); 7263 7264 if (iter->step->ts.kind != iter->var->ts.kind 7265 || iter->step->ts.type != iter->var->ts.type) 7266 gfc_convert_type (iter->step, &iter->var->ts, 1); 7267 7268 if (iter->step->expr_type == EXPR_CONSTANT) 7269 { 7270 if ((iter->step->ts.type == BT_INTEGER 7271 && mpz_cmp_ui (iter->step->value.integer, 0) == 0) 7272 || (iter->step->ts.type == BT_REAL 7273 && mpfr_sgn (iter->step->value.real) == 0)) 7274 { 7275 gfc_error ("Step expression in DO loop at %L cannot be zero", 7276 &iter->step->where); 7277 return false; 7278 } 7279 } 7280 7281 if (iter->start->expr_type == EXPR_CONSTANT 7282 && iter->end->expr_type == EXPR_CONSTANT 7283 && iter->step->expr_type == EXPR_CONSTANT) 7284 { 7285 int sgn, cmp; 7286 if (iter->start->ts.type == BT_INTEGER) 7287 { 7288 sgn = mpz_cmp_ui (iter->step->value.integer, 0); 7289 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer); 7290 } 7291 else 7292 { 7293 sgn = mpfr_sgn (iter->step->value.real); 7294 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real); 7295 } 7296 if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))) 7297 gfc_warning (OPT_Wzerotrip, 7298 "DO loop at %L will be executed zero times", 7299 &iter->step->where); 7300 } 7301 7302 if (iter->end->expr_type == EXPR_CONSTANT 7303 && iter->end->ts.type == BT_INTEGER 7304 && iter->step->expr_type == EXPR_CONSTANT 7305 && iter->step->ts.type == BT_INTEGER 7306 && (mpz_cmp_si (iter->step->value.integer, -1L) == 0 7307 || mpz_cmp_si (iter->step->value.integer, 1L) == 0)) 7308 { 7309 bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0; 7310 int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false); 7311 7312 if (is_step_positive 7313 && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0) 7314 gfc_warning (OPT_Wundefined_do_loop, 7315 "DO loop at %L is undefined as it overflows", 7316 &iter->step->where); 7317 else if (!is_step_positive 7318 && mpz_cmp (iter->end->value.integer, 7319 gfc_integer_kinds[k].min_int) == 0) 7320 gfc_warning (OPT_Wundefined_do_loop, 7321 "DO loop at %L is undefined as it underflows", 7322 &iter->step->where); 7323 } 7324 7325 return true; 7326 } 7327 7328 7329 /* Traversal function for find_forall_index. f == 2 signals that 7330 that variable itself is not to be checked - only the references. */ 7331 7332 static bool 7333 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f) 7334 { 7335 if (expr->expr_type != EXPR_VARIABLE) 7336 return false; 7337 7338 /* A scalar assignment */ 7339 if (!expr->ref || *f == 1) 7340 { 7341 if (expr->symtree->n.sym == sym) 7342 return true; 7343 else 7344 return false; 7345 } 7346 7347 if (*f == 2) 7348 *f = 1; 7349 return false; 7350 } 7351 7352 7353 /* Check whether the FORALL index appears in the expression or not. 7354 Returns true if SYM is found in EXPR. */ 7355 7356 bool 7357 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f) 7358 { 7359 if (gfc_traverse_expr (expr, sym, forall_index, f)) 7360 return true; 7361 else 7362 return false; 7363 } 7364 7365 7366 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained 7367 to be a scalar INTEGER variable. The subscripts and stride are scalar 7368 INTEGERs, and if stride is a constant it must be nonzero. 7369 Furthermore "A subscript or stride in a forall-triplet-spec shall 7370 not contain a reference to any index-name in the 7371 forall-triplet-spec-list in which it appears." (7.5.4.1) */ 7372 7373 static void 7374 resolve_forall_iterators (gfc_forall_iterator *it) 7375 { 7376 gfc_forall_iterator *iter, *iter2; 7377 7378 for (iter = it; iter; iter = iter->next) 7379 { 7380 if (gfc_resolve_expr (iter->var) 7381 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0)) 7382 gfc_error ("FORALL index-name at %L must be a scalar INTEGER", 7383 &iter->var->where); 7384 7385 if (gfc_resolve_expr (iter->start) 7386 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0)) 7387 gfc_error ("FORALL start expression at %L must be a scalar INTEGER", 7388 &iter->start->where); 7389 if (iter->var->ts.kind != iter->start->ts.kind) 7390 gfc_convert_type (iter->start, &iter->var->ts, 1); 7391 7392 if (gfc_resolve_expr (iter->end) 7393 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0)) 7394 gfc_error ("FORALL end expression at %L must be a scalar INTEGER", 7395 &iter->end->where); 7396 if (iter->var->ts.kind != iter->end->ts.kind) 7397 gfc_convert_type (iter->end, &iter->var->ts, 1); 7398 7399 if (gfc_resolve_expr (iter->stride)) 7400 { 7401 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0) 7402 gfc_error ("FORALL stride expression at %L must be a scalar %s", 7403 &iter->stride->where, "INTEGER"); 7404 7405 if (iter->stride->expr_type == EXPR_CONSTANT 7406 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0) 7407 gfc_error ("FORALL stride expression at %L cannot be zero", 7408 &iter->stride->where); 7409 } 7410 if (iter->var->ts.kind != iter->stride->ts.kind) 7411 gfc_convert_type (iter->stride, &iter->var->ts, 1); 7412 } 7413 7414 for (iter = it; iter; iter = iter->next) 7415 for (iter2 = iter; iter2; iter2 = iter2->next) 7416 { 7417 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0) 7418 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0) 7419 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0)) 7420 gfc_error ("FORALL index %qs may not appear in triplet " 7421 "specification at %L", iter->var->symtree->name, 7422 &iter2->start->where); 7423 } 7424 } 7425 7426 7427 /* Given a pointer to a symbol that is a derived type, see if it's 7428 inaccessible, i.e. if it's defined in another module and the components are 7429 PRIVATE. The search is recursive if necessary. Returns zero if no 7430 inaccessible components are found, nonzero otherwise. */ 7431 7432 static int 7433 derived_inaccessible (gfc_symbol *sym) 7434 { 7435 gfc_component *c; 7436 7437 if (sym->attr.use_assoc && sym->attr.private_comp) 7438 return 1; 7439 7440 for (c = sym->components; c; c = c->next) 7441 { 7442 /* Prevent an infinite loop through this function. */ 7443 if (c->ts.type == BT_DERIVED && c->attr.pointer 7444 && sym == c->ts.u.derived) 7445 continue; 7446 7447 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived)) 7448 return 1; 7449 } 7450 7451 return 0; 7452 } 7453 7454 7455 /* Resolve the argument of a deallocate expression. The expression must be 7456 a pointer or a full array. */ 7457 7458 static bool 7459 resolve_deallocate_expr (gfc_expr *e) 7460 { 7461 symbol_attribute attr; 7462 int allocatable, pointer; 7463 gfc_ref *ref; 7464 gfc_symbol *sym; 7465 gfc_component *c; 7466 bool unlimited; 7467 7468 if (!gfc_resolve_expr (e)) 7469 return false; 7470 7471 if (e->expr_type != EXPR_VARIABLE) 7472 goto bad; 7473 7474 sym = e->symtree->n.sym; 7475 unlimited = UNLIMITED_POLY(sym); 7476 7477 if (sym->ts.type == BT_CLASS) 7478 { 7479 allocatable = CLASS_DATA (sym)->attr.allocatable; 7480 pointer = CLASS_DATA (sym)->attr.class_pointer; 7481 } 7482 else 7483 { 7484 allocatable = sym->attr.allocatable; 7485 pointer = sym->attr.pointer; 7486 } 7487 for (ref = e->ref; ref; ref = ref->next) 7488 { 7489 switch (ref->type) 7490 { 7491 case REF_ARRAY: 7492 if (ref->u.ar.type != AR_FULL 7493 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0 7494 && ref->u.ar.codimen && gfc_ref_this_image (ref))) 7495 allocatable = 0; 7496 break; 7497 7498 case REF_COMPONENT: 7499 c = ref->u.c.component; 7500 if (c->ts.type == BT_CLASS) 7501 { 7502 allocatable = CLASS_DATA (c)->attr.allocatable; 7503 pointer = CLASS_DATA (c)->attr.class_pointer; 7504 } 7505 else 7506 { 7507 allocatable = c->attr.allocatable; 7508 pointer = c->attr.pointer; 7509 } 7510 break; 7511 7512 case REF_SUBSTRING: 7513 case REF_INQUIRY: 7514 allocatable = 0; 7515 break; 7516 } 7517 } 7518 7519 attr = gfc_expr_attr (e); 7520 7521 if (allocatable == 0 && attr.pointer == 0 && !unlimited) 7522 { 7523 bad: 7524 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER", 7525 &e->where); 7526 return false; 7527 } 7528 7529 /* F2008, C644. */ 7530 if (gfc_is_coindexed (e)) 7531 { 7532 gfc_error ("Coindexed allocatable object at %L", &e->where); 7533 return false; 7534 } 7535 7536 if (pointer 7537 && !gfc_check_vardef_context (e, true, true, false, 7538 _("DEALLOCATE object"))) 7539 return false; 7540 if (!gfc_check_vardef_context (e, false, true, false, 7541 _("DEALLOCATE object"))) 7542 return false; 7543 7544 return true; 7545 } 7546 7547 7548 /* Returns true if the expression e contains a reference to the symbol sym. */ 7549 static bool 7550 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED) 7551 { 7552 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym) 7553 return true; 7554 7555 return false; 7556 } 7557 7558 bool 7559 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e) 7560 { 7561 return gfc_traverse_expr (e, sym, sym_in_expr, 0); 7562 } 7563 7564 7565 /* Given the expression node e for an allocatable/pointer of derived type to be 7566 allocated, get the expression node to be initialized afterwards (needed for 7567 derived types with default initializers, and derived types with allocatable 7568 components that need nullification.) */ 7569 7570 gfc_expr * 7571 gfc_expr_to_initialize (gfc_expr *e) 7572 { 7573 gfc_expr *result; 7574 gfc_ref *ref; 7575 int i; 7576 7577 result = gfc_copy_expr (e); 7578 7579 /* Change the last array reference from AR_ELEMENT to AR_FULL. */ 7580 for (ref = result->ref; ref; ref = ref->next) 7581 if (ref->type == REF_ARRAY && ref->next == NULL) 7582 { 7583 if (ref->u.ar.dimen == 0 7584 && ref->u.ar.as && ref->u.ar.as->corank) 7585 return result; 7586 7587 ref->u.ar.type = AR_FULL; 7588 7589 for (i = 0; i < ref->u.ar.dimen; i++) 7590 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL; 7591 7592 break; 7593 } 7594 7595 gfc_free_shape (&result->shape, result->rank); 7596 7597 /* Recalculate rank, shape, etc. */ 7598 gfc_resolve_expr (result); 7599 return result; 7600 } 7601 7602 7603 /* If the last ref of an expression is an array ref, return a copy of the 7604 expression with that one removed. Otherwise, a copy of the original 7605 expression. This is used for allocate-expressions and pointer assignment 7606 LHS, where there may be an array specification that needs to be stripped 7607 off when using gfc_check_vardef_context. */ 7608 7609 static gfc_expr* 7610 remove_last_array_ref (gfc_expr* e) 7611 { 7612 gfc_expr* e2; 7613 gfc_ref** r; 7614 7615 e2 = gfc_copy_expr (e); 7616 for (r = &e2->ref; *r; r = &(*r)->next) 7617 if ((*r)->type == REF_ARRAY && !(*r)->next) 7618 { 7619 gfc_free_ref_list (*r); 7620 *r = NULL; 7621 break; 7622 } 7623 7624 return e2; 7625 } 7626 7627 7628 /* Used in resolve_allocate_expr to check that a allocation-object and 7629 a source-expr are conformable. This does not catch all possible 7630 cases; in particular a runtime checking is needed. */ 7631 7632 static bool 7633 conformable_arrays (gfc_expr *e1, gfc_expr *e2) 7634 { 7635 gfc_ref *tail; 7636 for (tail = e2->ref; tail && tail->next; tail = tail->next); 7637 7638 /* First compare rank. */ 7639 if ((tail && (!tail->u.ar.as || e1->rank != tail->u.ar.as->rank)) 7640 || (!tail && e1->rank != e2->rank)) 7641 { 7642 gfc_error ("Source-expr at %L must be scalar or have the " 7643 "same rank as the allocate-object at %L", 7644 &e1->where, &e2->where); 7645 return false; 7646 } 7647 7648 if (e1->shape) 7649 { 7650 int i; 7651 mpz_t s; 7652 7653 mpz_init (s); 7654 7655 for (i = 0; i < e1->rank; i++) 7656 { 7657 if (tail->u.ar.start[i] == NULL) 7658 break; 7659 7660 if (tail->u.ar.end[i]) 7661 { 7662 mpz_set (s, tail->u.ar.end[i]->value.integer); 7663 mpz_sub (s, s, tail->u.ar.start[i]->value.integer); 7664 mpz_add_ui (s, s, 1); 7665 } 7666 else 7667 { 7668 mpz_set (s, tail->u.ar.start[i]->value.integer); 7669 } 7670 7671 if (mpz_cmp (e1->shape[i], s) != 0) 7672 { 7673 gfc_error ("Source-expr at %L and allocate-object at %L must " 7674 "have the same shape", &e1->where, &e2->where); 7675 mpz_clear (s); 7676 return false; 7677 } 7678 } 7679 7680 mpz_clear (s); 7681 } 7682 7683 return true; 7684 } 7685 7686 7687 /* Resolve the expression in an ALLOCATE statement, doing the additional 7688 checks to see whether the expression is OK or not. The expression must 7689 have a trailing array reference that gives the size of the array. */ 7690 7691 static bool 7692 resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec) 7693 { 7694 int i, pointer, allocatable, dimension, is_abstract; 7695 int codimension; 7696 bool coindexed; 7697 bool unlimited; 7698 symbol_attribute attr; 7699 gfc_ref *ref, *ref2; 7700 gfc_expr *e2; 7701 gfc_array_ref *ar; 7702 gfc_symbol *sym = NULL; 7703 gfc_alloc *a; 7704 gfc_component *c; 7705 bool t; 7706 7707 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR 7708 checking of coarrays. */ 7709 for (ref = e->ref; ref; ref = ref->next) 7710 if (ref->next == NULL) 7711 break; 7712 7713 if (ref && ref->type == REF_ARRAY) 7714 ref->u.ar.in_allocate = true; 7715 7716 if (!gfc_resolve_expr (e)) 7717 goto failure; 7718 7719 /* Make sure the expression is allocatable or a pointer. If it is 7720 pointer, the next-to-last reference must be a pointer. */ 7721 7722 ref2 = NULL; 7723 if (e->symtree) 7724 sym = e->symtree->n.sym; 7725 7726 /* Check whether ultimate component is abstract and CLASS. */ 7727 is_abstract = 0; 7728 7729 /* Is the allocate-object unlimited polymorphic? */ 7730 unlimited = UNLIMITED_POLY(e); 7731 7732 if (e->expr_type != EXPR_VARIABLE) 7733 { 7734 allocatable = 0; 7735 attr = gfc_expr_attr (e); 7736 pointer = attr.pointer; 7737 dimension = attr.dimension; 7738 codimension = attr.codimension; 7739 } 7740 else 7741 { 7742 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)) 7743 { 7744 allocatable = CLASS_DATA (sym)->attr.allocatable; 7745 pointer = CLASS_DATA (sym)->attr.class_pointer; 7746 dimension = CLASS_DATA (sym)->attr.dimension; 7747 codimension = CLASS_DATA (sym)->attr.codimension; 7748 is_abstract = CLASS_DATA (sym)->attr.abstract; 7749 } 7750 else 7751 { 7752 allocatable = sym->attr.allocatable; 7753 pointer = sym->attr.pointer; 7754 dimension = sym->attr.dimension; 7755 codimension = sym->attr.codimension; 7756 } 7757 7758 coindexed = false; 7759 7760 for (ref = e->ref; ref; ref2 = ref, ref = ref->next) 7761 { 7762 switch (ref->type) 7763 { 7764 case REF_ARRAY: 7765 if (ref->u.ar.codimen > 0) 7766 { 7767 int n; 7768 for (n = ref->u.ar.dimen; 7769 n < ref->u.ar.dimen + ref->u.ar.codimen; n++) 7770 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE) 7771 { 7772 coindexed = true; 7773 break; 7774 } 7775 } 7776 7777 if (ref->next != NULL) 7778 pointer = 0; 7779 break; 7780 7781 case REF_COMPONENT: 7782 /* F2008, C644. */ 7783 if (coindexed) 7784 { 7785 gfc_error ("Coindexed allocatable object at %L", 7786 &e->where); 7787 goto failure; 7788 } 7789 7790 c = ref->u.c.component; 7791 if (c->ts.type == BT_CLASS) 7792 { 7793 allocatable = CLASS_DATA (c)->attr.allocatable; 7794 pointer = CLASS_DATA (c)->attr.class_pointer; 7795 dimension = CLASS_DATA (c)->attr.dimension; 7796 codimension = CLASS_DATA (c)->attr.codimension; 7797 is_abstract = CLASS_DATA (c)->attr.abstract; 7798 } 7799 else 7800 { 7801 allocatable = c->attr.allocatable; 7802 pointer = c->attr.pointer; 7803 dimension = c->attr.dimension; 7804 codimension = c->attr.codimension; 7805 is_abstract = c->attr.abstract; 7806 } 7807 break; 7808 7809 case REF_SUBSTRING: 7810 case REF_INQUIRY: 7811 allocatable = 0; 7812 pointer = 0; 7813 break; 7814 } 7815 } 7816 } 7817 7818 /* Check for F08:C628. */ 7819 if (allocatable == 0 && pointer == 0 && !unlimited) 7820 { 7821 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER", 7822 &e->where); 7823 goto failure; 7824 } 7825 7826 /* Some checks for the SOURCE tag. */ 7827 if (code->expr3) 7828 { 7829 /* Check F03:C631. */ 7830 if (!gfc_type_compatible (&e->ts, &code->expr3->ts)) 7831 { 7832 gfc_error ("Type of entity at %L is type incompatible with " 7833 "source-expr at %L", &e->where, &code->expr3->where); 7834 goto failure; 7835 } 7836 7837 /* Check F03:C632 and restriction following Note 6.18. */ 7838 if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e)) 7839 goto failure; 7840 7841 /* Check F03:C633. */ 7842 if (code->expr3->ts.kind != e->ts.kind && !unlimited) 7843 { 7844 gfc_error ("The allocate-object at %L and the source-expr at %L " 7845 "shall have the same kind type parameter", 7846 &e->where, &code->expr3->where); 7847 goto failure; 7848 } 7849 7850 /* Check F2008, C642. */ 7851 if (code->expr3->ts.type == BT_DERIVED 7852 && ((codimension && gfc_expr_attr (code->expr3).lock_comp) 7853 || (code->expr3->ts.u.derived->from_intmod 7854 == INTMOD_ISO_FORTRAN_ENV 7855 && code->expr3->ts.u.derived->intmod_sym_id 7856 == ISOFORTRAN_LOCK_TYPE))) 7857 { 7858 gfc_error ("The source-expr at %L shall neither be of type " 7859 "LOCK_TYPE nor have a LOCK_TYPE component if " 7860 "allocate-object at %L is a coarray", 7861 &code->expr3->where, &e->where); 7862 goto failure; 7863 } 7864 7865 /* Check TS18508, C702/C703. */ 7866 if (code->expr3->ts.type == BT_DERIVED 7867 && ((codimension && gfc_expr_attr (code->expr3).event_comp) 7868 || (code->expr3->ts.u.derived->from_intmod 7869 == INTMOD_ISO_FORTRAN_ENV 7870 && code->expr3->ts.u.derived->intmod_sym_id 7871 == ISOFORTRAN_EVENT_TYPE))) 7872 { 7873 gfc_error ("The source-expr at %L shall neither be of type " 7874 "EVENT_TYPE nor have a EVENT_TYPE component if " 7875 "allocate-object at %L is a coarray", 7876 &code->expr3->where, &e->where); 7877 goto failure; 7878 } 7879 } 7880 7881 /* Check F08:C629. */ 7882 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN 7883 && !code->expr3) 7884 { 7885 gcc_assert (e->ts.type == BT_CLASS); 7886 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a " 7887 "type-spec or source-expr", sym->name, &e->where); 7888 goto failure; 7889 } 7890 7891 /* Check F08:C632. */ 7892 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred 7893 && !UNLIMITED_POLY (e)) 7894 { 7895 int cmp; 7896 7897 if (!e->ts.u.cl->length) 7898 goto failure; 7899 7900 cmp = gfc_dep_compare_expr (e->ts.u.cl->length, 7901 code->ext.alloc.ts.u.cl->length); 7902 if (cmp == 1 || cmp == -1 || cmp == -3) 7903 { 7904 gfc_error ("Allocating %s at %L with type-spec requires the same " 7905 "character-length parameter as in the declaration", 7906 sym->name, &e->where); 7907 goto failure; 7908 } 7909 } 7910 7911 /* In the variable definition context checks, gfc_expr_attr is used 7912 on the expression. This is fooled by the array specification 7913 present in e, thus we have to eliminate that one temporarily. */ 7914 e2 = remove_last_array_ref (e); 7915 t = true; 7916 if (t && pointer) 7917 t = gfc_check_vardef_context (e2, true, true, false, 7918 _("ALLOCATE object")); 7919 if (t) 7920 t = gfc_check_vardef_context (e2, false, true, false, 7921 _("ALLOCATE object")); 7922 gfc_free_expr (e2); 7923 if (!t) 7924 goto failure; 7925 7926 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension 7927 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED) 7928 { 7929 /* For class arrays, the initialization with SOURCE is done 7930 using _copy and trans_call. It is convenient to exploit that 7931 when the allocated type is different from the declared type but 7932 no SOURCE exists by setting expr3. */ 7933 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts); 7934 } 7935 else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED 7936 && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV 7937 && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE) 7938 { 7939 /* We have to zero initialize the integer variable. */ 7940 code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0); 7941 } 7942 7943 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3)) 7944 { 7945 /* Make sure the vtab symbol is present when 7946 the module variables are generated. */ 7947 gfc_typespec ts = e->ts; 7948 if (code->expr3) 7949 ts = code->expr3->ts; 7950 else if (code->ext.alloc.ts.type == BT_DERIVED) 7951 ts = code->ext.alloc.ts; 7952 7953 /* Finding the vtab also publishes the type's symbol. Therefore this 7954 statement is necessary. */ 7955 gfc_find_derived_vtab (ts.u.derived); 7956 } 7957 else if (unlimited && !UNLIMITED_POLY (code->expr3)) 7958 { 7959 /* Again, make sure the vtab symbol is present when 7960 the module variables are generated. */ 7961 gfc_typespec *ts = NULL; 7962 if (code->expr3) 7963 ts = &code->expr3->ts; 7964 else 7965 ts = &code->ext.alloc.ts; 7966 7967 gcc_assert (ts); 7968 7969 /* Finding the vtab also publishes the type's symbol. Therefore this 7970 statement is necessary. */ 7971 gfc_find_vtab (ts); 7972 } 7973 7974 if (dimension == 0 && codimension == 0) 7975 goto success; 7976 7977 /* Make sure the last reference node is an array specification. */ 7978 7979 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL 7980 || (dimension && ref2->u.ar.dimen == 0)) 7981 { 7982 /* F08:C633. */ 7983 if (code->expr3) 7984 { 7985 if (!gfc_notify_std (GFC_STD_F2008, "Array specification required " 7986 "in ALLOCATE statement at %L", &e->where)) 7987 goto failure; 7988 if (code->expr3->rank != 0) 7989 *array_alloc_wo_spec = true; 7990 else 7991 { 7992 gfc_error ("Array specification or array-valued SOURCE= " 7993 "expression required in ALLOCATE statement at %L", 7994 &e->where); 7995 goto failure; 7996 } 7997 } 7998 else 7999 { 8000 gfc_error ("Array specification required in ALLOCATE statement " 8001 "at %L", &e->where); 8002 goto failure; 8003 } 8004 } 8005 8006 /* Make sure that the array section reference makes sense in the 8007 context of an ALLOCATE specification. */ 8008 8009 ar = &ref2->u.ar; 8010 8011 if (codimension) 8012 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++) 8013 { 8014 switch (ar->dimen_type[i]) 8015 { 8016 case DIMEN_THIS_IMAGE: 8017 gfc_error ("Coarray specification required in ALLOCATE statement " 8018 "at %L", &e->where); 8019 goto failure; 8020 8021 case DIMEN_RANGE: 8022 if (ar->start[i] == 0 || ar->end[i] == 0) 8023 { 8024 /* If ar->stride[i] is NULL, we issued a previous error. */ 8025 if (ar->stride[i] == NULL) 8026 gfc_error ("Bad array specification in ALLOCATE statement " 8027 "at %L", &e->where); 8028 goto failure; 8029 } 8030 else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1) 8031 { 8032 gfc_error ("Upper cobound is less than lower cobound at %L", 8033 &ar->start[i]->where); 8034 goto failure; 8035 } 8036 break; 8037 8038 case DIMEN_ELEMENT: 8039 if (ar->start[i]->expr_type == EXPR_CONSTANT) 8040 { 8041 gcc_assert (ar->start[i]->ts.type == BT_INTEGER); 8042 if (mpz_cmp_si (ar->start[i]->value.integer, 1) < 0) 8043 { 8044 gfc_error ("Upper cobound is less than lower cobound " 8045 "of 1 at %L", &ar->start[i]->where); 8046 goto failure; 8047 } 8048 } 8049 break; 8050 8051 case DIMEN_STAR: 8052 break; 8053 8054 default: 8055 gfc_error ("Bad array specification in ALLOCATE statement at %L", 8056 &e->where); 8057 goto failure; 8058 8059 } 8060 } 8061 for (i = 0; i < ar->dimen; i++) 8062 { 8063 if (ar->type == AR_ELEMENT || ar->type == AR_FULL) 8064 goto check_symbols; 8065 8066 switch (ar->dimen_type[i]) 8067 { 8068 case DIMEN_ELEMENT: 8069 break; 8070 8071 case DIMEN_RANGE: 8072 if (ar->start[i] != NULL 8073 && ar->end[i] != NULL 8074 && ar->stride[i] == NULL) 8075 break; 8076 8077 /* Fall through. */ 8078 8079 case DIMEN_UNKNOWN: 8080 case DIMEN_VECTOR: 8081 case DIMEN_STAR: 8082 case DIMEN_THIS_IMAGE: 8083 gfc_error ("Bad array specification in ALLOCATE statement at %L", 8084 &e->where); 8085 goto failure; 8086 } 8087 8088 check_symbols: 8089 for (a = code->ext.alloc.list; a; a = a->next) 8090 { 8091 sym = a->expr->symtree->n.sym; 8092 8093 /* TODO - check derived type components. */ 8094 if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS) 8095 continue; 8096 8097 if ((ar->start[i] != NULL 8098 && gfc_find_sym_in_expr (sym, ar->start[i])) 8099 || (ar->end[i] != NULL 8100 && gfc_find_sym_in_expr (sym, ar->end[i]))) 8101 { 8102 gfc_error ("%qs must not appear in the array specification at " 8103 "%L in the same ALLOCATE statement where it is " 8104 "itself allocated", sym->name, &ar->where); 8105 goto failure; 8106 } 8107 } 8108 } 8109 8110 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++) 8111 { 8112 if (ar->dimen_type[i] == DIMEN_ELEMENT 8113 || ar->dimen_type[i] == DIMEN_RANGE) 8114 { 8115 if (i == (ar->dimen + ar->codimen - 1)) 8116 { 8117 gfc_error ("Expected '*' in coindex specification in ALLOCATE " 8118 "statement at %L", &e->where); 8119 goto failure; 8120 } 8121 continue; 8122 } 8123 8124 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1) 8125 && ar->stride[i] == NULL) 8126 break; 8127 8128 gfc_error ("Bad coarray specification in ALLOCATE statement at %L", 8129 &e->where); 8130 goto failure; 8131 } 8132 8133 success: 8134 return true; 8135 8136 failure: 8137 return false; 8138 } 8139 8140 8141 static void 8142 resolve_allocate_deallocate (gfc_code *code, const char *fcn) 8143 { 8144 gfc_expr *stat, *errmsg, *pe, *qe; 8145 gfc_alloc *a, *p, *q; 8146 8147 stat = code->expr1; 8148 errmsg = code->expr2; 8149 8150 /* Check the stat variable. */ 8151 if (stat) 8152 { 8153 gfc_check_vardef_context (stat, false, false, false, 8154 _("STAT variable")); 8155 8156 if ((stat->ts.type != BT_INTEGER 8157 && !(stat->ref && (stat->ref->type == REF_ARRAY 8158 || stat->ref->type == REF_COMPONENT))) 8159 || stat->rank > 0) 8160 gfc_error ("Stat-variable at %L must be a scalar INTEGER " 8161 "variable", &stat->where); 8162 8163 for (p = code->ext.alloc.list; p; p = p->next) 8164 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name) 8165 { 8166 gfc_ref *ref1, *ref2; 8167 bool found = true; 8168 8169 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2; 8170 ref1 = ref1->next, ref2 = ref2->next) 8171 { 8172 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT) 8173 continue; 8174 if (ref1->u.c.component->name != ref2->u.c.component->name) 8175 { 8176 found = false; 8177 break; 8178 } 8179 } 8180 8181 if (found) 8182 { 8183 gfc_error ("Stat-variable at %L shall not be %sd within " 8184 "the same %s statement", &stat->where, fcn, fcn); 8185 break; 8186 } 8187 } 8188 } 8189 8190 /* Check the errmsg variable. */ 8191 if (errmsg) 8192 { 8193 if (!stat) 8194 gfc_warning (0, "ERRMSG at %L is useless without a STAT tag", 8195 &errmsg->where); 8196 8197 gfc_check_vardef_context (errmsg, false, false, false, 8198 _("ERRMSG variable")); 8199 8200 /* F18:R928 alloc-opt is ERRMSG = errmsg-variable 8201 F18:R930 errmsg-variable is scalar-default-char-variable 8202 F18:R906 default-char-variable is variable 8203 F18:C906 default-char-variable shall be default character. */ 8204 if ((errmsg->ts.type != BT_CHARACTER 8205 && !(errmsg->ref 8206 && (errmsg->ref->type == REF_ARRAY 8207 || errmsg->ref->type == REF_COMPONENT))) 8208 || errmsg->rank > 0 8209 || errmsg->ts.kind != gfc_default_character_kind) 8210 gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER " 8211 "variable", &errmsg->where); 8212 8213 for (p = code->ext.alloc.list; p; p = p->next) 8214 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name) 8215 { 8216 gfc_ref *ref1, *ref2; 8217 bool found = true; 8218 8219 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2; 8220 ref1 = ref1->next, ref2 = ref2->next) 8221 { 8222 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT) 8223 continue; 8224 if (ref1->u.c.component->name != ref2->u.c.component->name) 8225 { 8226 found = false; 8227 break; 8228 } 8229 } 8230 8231 if (found) 8232 { 8233 gfc_error ("Errmsg-variable at %L shall not be %sd within " 8234 "the same %s statement", &errmsg->where, fcn, fcn); 8235 break; 8236 } 8237 } 8238 } 8239 8240 /* Check that an allocate-object appears only once in the statement. */ 8241 8242 for (p = code->ext.alloc.list; p; p = p->next) 8243 { 8244 pe = p->expr; 8245 for (q = p->next; q; q = q->next) 8246 { 8247 qe = q->expr; 8248 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name) 8249 { 8250 /* This is a potential collision. */ 8251 gfc_ref *pr = pe->ref; 8252 gfc_ref *qr = qe->ref; 8253 8254 /* Follow the references until 8255 a) They start to differ, in which case there is no error; 8256 you can deallocate a%b and a%c in a single statement 8257 b) Both of them stop, which is an error 8258 c) One of them stops, which is also an error. */ 8259 while (1) 8260 { 8261 if (pr == NULL && qr == NULL) 8262 { 8263 gfc_error ("Allocate-object at %L also appears at %L", 8264 &pe->where, &qe->where); 8265 break; 8266 } 8267 else if (pr != NULL && qr == NULL) 8268 { 8269 gfc_error ("Allocate-object at %L is subobject of" 8270 " object at %L", &pe->where, &qe->where); 8271 break; 8272 } 8273 else if (pr == NULL && qr != NULL) 8274 { 8275 gfc_error ("Allocate-object at %L is subobject of" 8276 " object at %L", &qe->where, &pe->where); 8277 break; 8278 } 8279 /* Here, pr != NULL && qr != NULL */ 8280 gcc_assert(pr->type == qr->type); 8281 if (pr->type == REF_ARRAY) 8282 { 8283 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)), 8284 which are legal. */ 8285 gcc_assert (qr->type == REF_ARRAY); 8286 8287 if (pr->next && qr->next) 8288 { 8289 int i; 8290 gfc_array_ref *par = &(pr->u.ar); 8291 gfc_array_ref *qar = &(qr->u.ar); 8292 8293 for (i=0; i<par->dimen; i++) 8294 { 8295 if ((par->start[i] != NULL 8296 || qar->start[i] != NULL) 8297 && gfc_dep_compare_expr (par->start[i], 8298 qar->start[i]) != 0) 8299 goto break_label; 8300 } 8301 } 8302 } 8303 else 8304 { 8305 if (pr->u.c.component->name != qr->u.c.component->name) 8306 break; 8307 } 8308 8309 pr = pr->next; 8310 qr = qr->next; 8311 } 8312 break_label: 8313 ; 8314 } 8315 } 8316 } 8317 8318 if (strcmp (fcn, "ALLOCATE") == 0) 8319 { 8320 bool arr_alloc_wo_spec = false; 8321 8322 /* Resolving the expr3 in the loop over all objects to allocate would 8323 execute loop invariant code for each loop item. Therefore do it just 8324 once here. */ 8325 if (code->expr3 && code->expr3->mold 8326 && code->expr3->ts.type == BT_DERIVED) 8327 { 8328 /* Default initialization via MOLD (non-polymorphic). */ 8329 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts); 8330 if (rhs != NULL) 8331 { 8332 gfc_resolve_expr (rhs); 8333 gfc_free_expr (code->expr3); 8334 code->expr3 = rhs; 8335 } 8336 } 8337 for (a = code->ext.alloc.list; a; a = a->next) 8338 resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec); 8339 8340 if (arr_alloc_wo_spec && code->expr3) 8341 { 8342 /* Mark the allocate to have to take the array specification 8343 from the expr3. */ 8344 code->ext.alloc.arr_spec_from_expr3 = 1; 8345 } 8346 } 8347 else 8348 { 8349 for (a = code->ext.alloc.list; a; a = a->next) 8350 resolve_deallocate_expr (a->expr); 8351 } 8352 } 8353 8354 8355 /************ SELECT CASE resolution subroutines ************/ 8356 8357 /* Callback function for our mergesort variant. Determines interval 8358 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for 8359 op1 > op2. Assumes we're not dealing with the default case. 8360 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:). 8361 There are nine situations to check. */ 8362 8363 static int 8364 compare_cases (const gfc_case *op1, const gfc_case *op2) 8365 { 8366 int retval; 8367 8368 if (op1->low == NULL) /* op1 = (:L) */ 8369 { 8370 /* op2 = (:N), so overlap. */ 8371 retval = 0; 8372 /* op2 = (M:) or (M:N), L < M */ 8373 if (op2->low != NULL 8374 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0) 8375 retval = -1; 8376 } 8377 else if (op1->high == NULL) /* op1 = (K:) */ 8378 { 8379 /* op2 = (M:), so overlap. */ 8380 retval = 0; 8381 /* op2 = (:N) or (M:N), K > N */ 8382 if (op2->high != NULL 8383 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0) 8384 retval = 1; 8385 } 8386 else /* op1 = (K:L) */ 8387 { 8388 if (op2->low == NULL) /* op2 = (:N), K > N */ 8389 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0) 8390 ? 1 : 0; 8391 else if (op2->high == NULL) /* op2 = (M:), L < M */ 8392 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0) 8393 ? -1 : 0; 8394 else /* op2 = (M:N) */ 8395 { 8396 retval = 0; 8397 /* L < M */ 8398 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0) 8399 retval = -1; 8400 /* K > N */ 8401 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0) 8402 retval = 1; 8403 } 8404 } 8405 8406 return retval; 8407 } 8408 8409 8410 /* Merge-sort a double linked case list, detecting overlap in the 8411 process. LIST is the head of the double linked case list before it 8412 is sorted. Returns the head of the sorted list if we don't see any 8413 overlap, or NULL otherwise. */ 8414 8415 static gfc_case * 8416 check_case_overlap (gfc_case *list) 8417 { 8418 gfc_case *p, *q, *e, *tail; 8419 int insize, nmerges, psize, qsize, cmp, overlap_seen; 8420 8421 /* If the passed list was empty, return immediately. */ 8422 if (!list) 8423 return NULL; 8424 8425 overlap_seen = 0; 8426 insize = 1; 8427 8428 /* Loop unconditionally. The only exit from this loop is a return 8429 statement, when we've finished sorting the case list. */ 8430 for (;;) 8431 { 8432 p = list; 8433 list = NULL; 8434 tail = NULL; 8435 8436 /* Count the number of merges we do in this pass. */ 8437 nmerges = 0; 8438 8439 /* Loop while there exists a merge to be done. */ 8440 while (p) 8441 { 8442 int i; 8443 8444 /* Count this merge. */ 8445 nmerges++; 8446 8447 /* Cut the list in two pieces by stepping INSIZE places 8448 forward in the list, starting from P. */ 8449 psize = 0; 8450 q = p; 8451 for (i = 0; i < insize; i++) 8452 { 8453 psize++; 8454 q = q->right; 8455 if (!q) 8456 break; 8457 } 8458 qsize = insize; 8459 8460 /* Now we have two lists. Merge them! */ 8461 while (psize > 0 || (qsize > 0 && q != NULL)) 8462 { 8463 /* See from which the next case to merge comes from. */ 8464 if (psize == 0) 8465 { 8466 /* P is empty so the next case must come from Q. */ 8467 e = q; 8468 q = q->right; 8469 qsize--; 8470 } 8471 else if (qsize == 0 || q == NULL) 8472 { 8473 /* Q is empty. */ 8474 e = p; 8475 p = p->right; 8476 psize--; 8477 } 8478 else 8479 { 8480 cmp = compare_cases (p, q); 8481 if (cmp < 0) 8482 { 8483 /* The whole case range for P is less than the 8484 one for Q. */ 8485 e = p; 8486 p = p->right; 8487 psize--; 8488 } 8489 else if (cmp > 0) 8490 { 8491 /* The whole case range for Q is greater than 8492 the case range for P. */ 8493 e = q; 8494 q = q->right; 8495 qsize--; 8496 } 8497 else 8498 { 8499 /* The cases overlap, or they are the same 8500 element in the list. Either way, we must 8501 issue an error and get the next case from P. */ 8502 /* FIXME: Sort P and Q by line number. */ 8503 gfc_error ("CASE label at %L overlaps with CASE " 8504 "label at %L", &p->where, &q->where); 8505 overlap_seen = 1; 8506 e = p; 8507 p = p->right; 8508 psize--; 8509 } 8510 } 8511 8512 /* Add the next element to the merged list. */ 8513 if (tail) 8514 tail->right = e; 8515 else 8516 list = e; 8517 e->left = tail; 8518 tail = e; 8519 } 8520 8521 /* P has now stepped INSIZE places along, and so has Q. So 8522 they're the same. */ 8523 p = q; 8524 } 8525 tail->right = NULL; 8526 8527 /* If we have done only one merge or none at all, we've 8528 finished sorting the cases. */ 8529 if (nmerges <= 1) 8530 { 8531 if (!overlap_seen) 8532 return list; 8533 else 8534 return NULL; 8535 } 8536 8537 /* Otherwise repeat, merging lists twice the size. */ 8538 insize *= 2; 8539 } 8540 } 8541 8542 8543 /* Check to see if an expression is suitable for use in a CASE statement. 8544 Makes sure that all case expressions are scalar constants of the same 8545 type. Return false if anything is wrong. */ 8546 8547 static bool 8548 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr) 8549 { 8550 if (e == NULL) return true; 8551 8552 if (e->ts.type != case_expr->ts.type) 8553 { 8554 gfc_error ("Expression in CASE statement at %L must be of type %s", 8555 &e->where, gfc_basic_typename (case_expr->ts.type)); 8556 return false; 8557 } 8558 8559 /* C805 (R808) For a given case-construct, each case-value shall be of 8560 the same type as case-expr. For character type, length differences 8561 are allowed, but the kind type parameters shall be the same. */ 8562 8563 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind) 8564 { 8565 gfc_error ("Expression in CASE statement at %L must be of kind %d", 8566 &e->where, case_expr->ts.kind); 8567 return false; 8568 } 8569 8570 /* Convert the case value kind to that of case expression kind, 8571 if needed */ 8572 8573 if (e->ts.kind != case_expr->ts.kind) 8574 gfc_convert_type_warn (e, &case_expr->ts, 2, 0); 8575 8576 if (e->rank != 0) 8577 { 8578 gfc_error ("Expression in CASE statement at %L must be scalar", 8579 &e->where); 8580 return false; 8581 } 8582 8583 return true; 8584 } 8585 8586 8587 /* Given a completely parsed select statement, we: 8588 8589 - Validate all expressions and code within the SELECT. 8590 - Make sure that the selection expression is not of the wrong type. 8591 - Make sure that no case ranges overlap. 8592 - Eliminate unreachable cases and unreachable code resulting from 8593 removing case labels. 8594 8595 The standard does allow unreachable cases, e.g. CASE (5:3). But 8596 they are a hassle for code generation, and to prevent that, we just 8597 cut them out here. This is not necessary for overlapping cases 8598 because they are illegal and we never even try to generate code. 8599 8600 We have the additional caveat that a SELECT construct could have 8601 been a computed GOTO in the source code. Fortunately we can fairly 8602 easily work around that here: The case_expr for a "real" SELECT CASE 8603 is in code->expr1, but for a computed GOTO it is in code->expr2. All 8604 we have to do is make sure that the case_expr is a scalar integer 8605 expression. */ 8606 8607 static void 8608 resolve_select (gfc_code *code, bool select_type) 8609 { 8610 gfc_code *body; 8611 gfc_expr *case_expr; 8612 gfc_case *cp, *default_case, *tail, *head; 8613 int seen_unreachable; 8614 int seen_logical; 8615 int ncases; 8616 bt type; 8617 bool t; 8618 8619 if (code->expr1 == NULL) 8620 { 8621 /* This was actually a computed GOTO statement. */ 8622 case_expr = code->expr2; 8623 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0) 8624 gfc_error ("Selection expression in computed GOTO statement " 8625 "at %L must be a scalar integer expression", 8626 &case_expr->where); 8627 8628 /* Further checking is not necessary because this SELECT was built 8629 by the compiler, so it should always be OK. Just move the 8630 case_expr from expr2 to expr so that we can handle computed 8631 GOTOs as normal SELECTs from here on. */ 8632 code->expr1 = code->expr2; 8633 code->expr2 = NULL; 8634 return; 8635 } 8636 8637 case_expr = code->expr1; 8638 type = case_expr->ts.type; 8639 8640 /* F08:C830. */ 8641 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER) 8642 { 8643 gfc_error ("Argument of SELECT statement at %L cannot be %s", 8644 &case_expr->where, gfc_typename (case_expr)); 8645 8646 /* Punt. Going on here just produce more garbage error messages. */ 8647 return; 8648 } 8649 8650 /* F08:R842. */ 8651 if (!select_type && case_expr->rank != 0) 8652 { 8653 gfc_error ("Argument of SELECT statement at %L must be a scalar " 8654 "expression", &case_expr->where); 8655 8656 /* Punt. */ 8657 return; 8658 } 8659 8660 /* Raise a warning if an INTEGER case value exceeds the range of 8661 the case-expr. Later, all expressions will be promoted to the 8662 largest kind of all case-labels. */ 8663 8664 if (type == BT_INTEGER) 8665 for (body = code->block; body; body = body->block) 8666 for (cp = body->ext.block.case_list; cp; cp = cp->next) 8667 { 8668 if (cp->low 8669 && gfc_check_integer_range (cp->low->value.integer, 8670 case_expr->ts.kind) != ARITH_OK) 8671 gfc_warning (0, "Expression in CASE statement at %L is " 8672 "not in the range of %s", &cp->low->where, 8673 gfc_typename (case_expr)); 8674 8675 if (cp->high 8676 && cp->low != cp->high 8677 && gfc_check_integer_range (cp->high->value.integer, 8678 case_expr->ts.kind) != ARITH_OK) 8679 gfc_warning (0, "Expression in CASE statement at %L is " 8680 "not in the range of %s", &cp->high->where, 8681 gfc_typename (case_expr)); 8682 } 8683 8684 /* PR 19168 has a long discussion concerning a mismatch of the kinds 8685 of the SELECT CASE expression and its CASE values. Walk the lists 8686 of case values, and if we find a mismatch, promote case_expr to 8687 the appropriate kind. */ 8688 8689 if (type == BT_LOGICAL || type == BT_INTEGER) 8690 { 8691 for (body = code->block; body; body = body->block) 8692 { 8693 /* Walk the case label list. */ 8694 for (cp = body->ext.block.case_list; cp; cp = cp->next) 8695 { 8696 /* Intercept the DEFAULT case. It does not have a kind. */ 8697 if (cp->low == NULL && cp->high == NULL) 8698 continue; 8699 8700 /* Unreachable case ranges are discarded, so ignore. */ 8701 if (cp->low != NULL && cp->high != NULL 8702 && cp->low != cp->high 8703 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0) 8704 continue; 8705 8706 if (cp->low != NULL 8707 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low)) 8708 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0); 8709 8710 if (cp->high != NULL 8711 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high)) 8712 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0); 8713 } 8714 } 8715 } 8716 8717 /* Assume there is no DEFAULT case. */ 8718 default_case = NULL; 8719 head = tail = NULL; 8720 ncases = 0; 8721 seen_logical = 0; 8722 8723 for (body = code->block; body; body = body->block) 8724 { 8725 /* Assume the CASE list is OK, and all CASE labels can be matched. */ 8726 t = true; 8727 seen_unreachable = 0; 8728 8729 /* Walk the case label list, making sure that all case labels 8730 are legal. */ 8731 for (cp = body->ext.block.case_list; cp; cp = cp->next) 8732 { 8733 /* Count the number of cases in the whole construct. */ 8734 ncases++; 8735 8736 /* Intercept the DEFAULT case. */ 8737 if (cp->low == NULL && cp->high == NULL) 8738 { 8739 if (default_case != NULL) 8740 { 8741 gfc_error ("The DEFAULT CASE at %L cannot be followed " 8742 "by a second DEFAULT CASE at %L", 8743 &default_case->where, &cp->where); 8744 t = false; 8745 break; 8746 } 8747 else 8748 { 8749 default_case = cp; 8750 continue; 8751 } 8752 } 8753 8754 /* Deal with single value cases and case ranges. Errors are 8755 issued from the validation function. */ 8756 if (!validate_case_label_expr (cp->low, case_expr) 8757 || !validate_case_label_expr (cp->high, case_expr)) 8758 { 8759 t = false; 8760 break; 8761 } 8762 8763 if (type == BT_LOGICAL 8764 && ((cp->low == NULL || cp->high == NULL) 8765 || cp->low != cp->high)) 8766 { 8767 gfc_error ("Logical range in CASE statement at %L is not " 8768 "allowed", &cp->low->where); 8769 t = false; 8770 break; 8771 } 8772 8773 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT) 8774 { 8775 int value; 8776 value = cp->low->value.logical == 0 ? 2 : 1; 8777 if (value & seen_logical) 8778 { 8779 gfc_error ("Constant logical value in CASE statement " 8780 "is repeated at %L", 8781 &cp->low->where); 8782 t = false; 8783 break; 8784 } 8785 seen_logical |= value; 8786 } 8787 8788 if (cp->low != NULL && cp->high != NULL 8789 && cp->low != cp->high 8790 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0) 8791 { 8792 if (warn_surprising) 8793 gfc_warning (OPT_Wsurprising, 8794 "Range specification at %L can never be matched", 8795 &cp->where); 8796 8797 cp->unreachable = 1; 8798 seen_unreachable = 1; 8799 } 8800 else 8801 { 8802 /* If the case range can be matched, it can also overlap with 8803 other cases. To make sure it does not, we put it in a 8804 double linked list here. We sort that with a merge sort 8805 later on to detect any overlapping cases. */ 8806 if (!head) 8807 { 8808 head = tail = cp; 8809 head->right = head->left = NULL; 8810 } 8811 else 8812 { 8813 tail->right = cp; 8814 tail->right->left = tail; 8815 tail = tail->right; 8816 tail->right = NULL; 8817 } 8818 } 8819 } 8820 8821 /* It there was a failure in the previous case label, give up 8822 for this case label list. Continue with the next block. */ 8823 if (!t) 8824 continue; 8825 8826 /* See if any case labels that are unreachable have been seen. 8827 If so, we eliminate them. This is a bit of a kludge because 8828 the case lists for a single case statement (label) is a 8829 single forward linked lists. */ 8830 if (seen_unreachable) 8831 { 8832 /* Advance until the first case in the list is reachable. */ 8833 while (body->ext.block.case_list != NULL 8834 && body->ext.block.case_list->unreachable) 8835 { 8836 gfc_case *n = body->ext.block.case_list; 8837 body->ext.block.case_list = body->ext.block.case_list->next; 8838 n->next = NULL; 8839 gfc_free_case_list (n); 8840 } 8841 8842 /* Strip all other unreachable cases. */ 8843 if (body->ext.block.case_list) 8844 { 8845 for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next) 8846 { 8847 if (cp->next->unreachable) 8848 { 8849 gfc_case *n = cp->next; 8850 cp->next = cp->next->next; 8851 n->next = NULL; 8852 gfc_free_case_list (n); 8853 } 8854 } 8855 } 8856 } 8857 } 8858 8859 /* See if there were overlapping cases. If the check returns NULL, 8860 there was overlap. In that case we don't do anything. If head 8861 is non-NULL, we prepend the DEFAULT case. The sorted list can 8862 then used during code generation for SELECT CASE constructs with 8863 a case expression of a CHARACTER type. */ 8864 if (head) 8865 { 8866 head = check_case_overlap (head); 8867 8868 /* Prepend the default_case if it is there. */ 8869 if (head != NULL && default_case) 8870 { 8871 default_case->left = NULL; 8872 default_case->right = head; 8873 head->left = default_case; 8874 } 8875 } 8876 8877 /* Eliminate dead blocks that may be the result if we've seen 8878 unreachable case labels for a block. */ 8879 for (body = code; body && body->block; body = body->block) 8880 { 8881 if (body->block->ext.block.case_list == NULL) 8882 { 8883 /* Cut the unreachable block from the code chain. */ 8884 gfc_code *c = body->block; 8885 body->block = c->block; 8886 8887 /* Kill the dead block, but not the blocks below it. */ 8888 c->block = NULL; 8889 gfc_free_statements (c); 8890 } 8891 } 8892 8893 /* More than two cases is legal but insane for logical selects. 8894 Issue a warning for it. */ 8895 if (warn_surprising && type == BT_LOGICAL && ncases > 2) 8896 gfc_warning (OPT_Wsurprising, 8897 "Logical SELECT CASE block at %L has more that two cases", 8898 &code->loc); 8899 } 8900 8901 8902 /* Check if a derived type is extensible. */ 8903 8904 bool 8905 gfc_type_is_extensible (gfc_symbol *sym) 8906 { 8907 return !(sym->attr.is_bind_c || sym->attr.sequence 8908 || (sym->attr.is_class 8909 && sym->components->ts.u.derived->attr.unlimited_polymorphic)); 8910 } 8911 8912 8913 static void 8914 resolve_types (gfc_namespace *ns); 8915 8916 /* Resolve an associate-name: Resolve target and ensure the type-spec is 8917 correct as well as possibly the array-spec. */ 8918 8919 static void 8920 resolve_assoc_var (gfc_symbol* sym, bool resolve_target) 8921 { 8922 gfc_expr* target; 8923 8924 gcc_assert (sym->assoc); 8925 gcc_assert (sym->attr.flavor == FL_VARIABLE); 8926 8927 /* If this is for SELECT TYPE, the target may not yet be set. In that 8928 case, return. Resolution will be called later manually again when 8929 this is done. */ 8930 target = sym->assoc->target; 8931 if (!target) 8932 return; 8933 gcc_assert (!sym->assoc->dangling); 8934 8935 if (resolve_target && !gfc_resolve_expr (target)) 8936 return; 8937 8938 /* For variable targets, we get some attributes from the target. */ 8939 if (target->expr_type == EXPR_VARIABLE) 8940 { 8941 gfc_symbol *tsym, *dsym; 8942 8943 gcc_assert (target->symtree); 8944 tsym = target->symtree->n.sym; 8945 8946 if (gfc_expr_attr (target).proc_pointer) 8947 { 8948 gfc_error ("Associating entity %qs at %L is a procedure pointer", 8949 tsym->name, &target->where); 8950 return; 8951 } 8952 8953 if (tsym->attr.flavor == FL_PROCEDURE && tsym->generic 8954 && (dsym = gfc_find_dt_in_generic (tsym)) != NULL 8955 && dsym->attr.flavor == FL_DERIVED) 8956 { 8957 gfc_error ("Derived type %qs cannot be used as a variable at %L", 8958 tsym->name, &target->where); 8959 return; 8960 } 8961 8962 if (tsym->attr.flavor == FL_PROCEDURE) 8963 { 8964 bool is_error = true; 8965 if (tsym->attr.function && tsym->result == tsym) 8966 for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent) 8967 if (tsym == ns->proc_name) 8968 { 8969 is_error = false; 8970 break; 8971 } 8972 if (is_error) 8973 { 8974 gfc_error ("Associating entity %qs at %L is a procedure name", 8975 tsym->name, &target->where); 8976 return; 8977 } 8978 } 8979 8980 sym->attr.asynchronous = tsym->attr.asynchronous; 8981 sym->attr.volatile_ = tsym->attr.volatile_; 8982 8983 sym->attr.target = tsym->attr.target 8984 || gfc_expr_attr (target).pointer; 8985 if (is_subref_array (target)) 8986 sym->attr.subref_array_pointer = 1; 8987 } 8988 else if (target->ts.type == BT_PROCEDURE) 8989 { 8990 gfc_error ("Associating selector-expression at %L yields a procedure", 8991 &target->where); 8992 return; 8993 } 8994 8995 if (target->expr_type == EXPR_NULL) 8996 { 8997 gfc_error ("Selector at %L cannot be NULL()", &target->where); 8998 return; 8999 } 9000 else if (target->ts.type == BT_UNKNOWN) 9001 { 9002 gfc_error ("Selector at %L has no type", &target->where); 9003 return; 9004 } 9005 9006 /* Get type if this was not already set. Note that it can be 9007 some other type than the target in case this is a SELECT TYPE 9008 selector! So we must not update when the type is already there. */ 9009 if (sym->ts.type == BT_UNKNOWN) 9010 sym->ts = target->ts; 9011 9012 gcc_assert (sym->ts.type != BT_UNKNOWN); 9013 9014 /* See if this is a valid association-to-variable. */ 9015 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE 9016 && !gfc_has_vector_subscript (target)); 9017 9018 /* Finally resolve if this is an array or not. */ 9019 if (sym->attr.dimension && target->rank == 0) 9020 { 9021 /* primary.c makes the assumption that a reference to an associate 9022 name followed by a left parenthesis is an array reference. */ 9023 if (sym->ts.type != BT_CHARACTER) 9024 gfc_error ("Associate-name %qs at %L is used as array", 9025 sym->name, &sym->declared_at); 9026 sym->attr.dimension = 0; 9027 return; 9028 } 9029 9030 9031 /* We cannot deal with class selectors that need temporaries. */ 9032 if (target->ts.type == BT_CLASS 9033 && gfc_ref_needs_temporary_p (target->ref)) 9034 { 9035 gfc_error ("CLASS selector at %L needs a temporary which is not " 9036 "yet implemented", &target->where); 9037 return; 9038 } 9039 9040 if (target->ts.type == BT_CLASS) 9041 gfc_fix_class_refs (target); 9042 9043 if (target->rank != 0 && !sym->attr.select_rank_temporary) 9044 { 9045 gfc_array_spec *as; 9046 /* The rank may be incorrectly guessed at parsing, therefore make sure 9047 it is corrected now. */ 9048 if (sym->ts.type != BT_CLASS && (!sym->as || sym->assoc->rankguessed)) 9049 { 9050 if (!sym->as) 9051 sym->as = gfc_get_array_spec (); 9052 as = sym->as; 9053 as->rank = target->rank; 9054 as->type = AS_DEFERRED; 9055 as->corank = gfc_get_corank (target); 9056 sym->attr.dimension = 1; 9057 if (as->corank != 0) 9058 sym->attr.codimension = 1; 9059 } 9060 else if (sym->ts.type == BT_CLASS 9061 && CLASS_DATA (sym) 9062 && (!CLASS_DATA (sym)->as || sym->assoc->rankguessed)) 9063 { 9064 if (!CLASS_DATA (sym)->as) 9065 CLASS_DATA (sym)->as = gfc_get_array_spec (); 9066 as = CLASS_DATA (sym)->as; 9067 as->rank = target->rank; 9068 as->type = AS_DEFERRED; 9069 as->corank = gfc_get_corank (target); 9070 CLASS_DATA (sym)->attr.dimension = 1; 9071 if (as->corank != 0) 9072 CLASS_DATA (sym)->attr.codimension = 1; 9073 } 9074 } 9075 else if (!sym->attr.select_rank_temporary) 9076 { 9077 /* target's rank is 0, but the type of the sym is still array valued, 9078 which has to be corrected. */ 9079 if (sym->ts.type == BT_CLASS && sym->ts.u.derived 9080 && CLASS_DATA (sym) && CLASS_DATA (sym)->as) 9081 { 9082 gfc_array_spec *as; 9083 symbol_attribute attr; 9084 /* The associated variable's type is still the array type 9085 correct this now. */ 9086 gfc_typespec *ts = &target->ts; 9087 gfc_ref *ref; 9088 gfc_component *c; 9089 for (ref = target->ref; ref != NULL; ref = ref->next) 9090 { 9091 switch (ref->type) 9092 { 9093 case REF_COMPONENT: 9094 ts = &ref->u.c.component->ts; 9095 break; 9096 case REF_ARRAY: 9097 if (ts->type == BT_CLASS) 9098 ts = &ts->u.derived->components->ts; 9099 break; 9100 default: 9101 break; 9102 } 9103 } 9104 /* Create a scalar instance of the current class type. Because the 9105 rank of a class array goes into its name, the type has to be 9106 rebuild. The alternative of (re-)setting just the attributes 9107 and as in the current type, destroys the type also in other 9108 places. */ 9109 as = NULL; 9110 sym->ts = *ts; 9111 sym->ts.type = BT_CLASS; 9112 attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr; 9113 attr.class_ok = 0; 9114 attr.associate_var = 1; 9115 attr.dimension = attr.codimension = 0; 9116 attr.class_pointer = 1; 9117 if (!gfc_build_class_symbol (&sym->ts, &attr, &as)) 9118 gcc_unreachable (); 9119 /* Make sure the _vptr is set. */ 9120 c = gfc_find_component (sym->ts.u.derived, "_vptr", true, true, NULL); 9121 if (c->ts.u.derived == NULL) 9122 c->ts.u.derived = gfc_find_derived_vtab (sym->ts.u.derived); 9123 CLASS_DATA (sym)->attr.pointer = 1; 9124 CLASS_DATA (sym)->attr.class_pointer = 1; 9125 gfc_set_sym_referenced (sym->ts.u.derived); 9126 gfc_commit_symbol (sym->ts.u.derived); 9127 /* _vptr now has the _vtab in it, change it to the _vtype. */ 9128 if (c->ts.u.derived->attr.vtab) 9129 c->ts.u.derived = c->ts.u.derived->ts.u.derived; 9130 c->ts.u.derived->ns->types_resolved = 0; 9131 resolve_types (c->ts.u.derived->ns); 9132 } 9133 } 9134 9135 /* Mark this as an associate variable. */ 9136 sym->attr.associate_var = 1; 9137 9138 /* Fix up the type-spec for CHARACTER types. */ 9139 if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary) 9140 { 9141 if (!sym->ts.u.cl) 9142 sym->ts.u.cl = target->ts.u.cl; 9143 9144 if (sym->ts.deferred 9145 && sym->ts.u.cl == target->ts.u.cl) 9146 { 9147 sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL); 9148 sym->ts.deferred = 1; 9149 } 9150 9151 if (!sym->ts.u.cl->length 9152 && !sym->ts.deferred 9153 && target->expr_type == EXPR_CONSTANT) 9154 { 9155 sym->ts.u.cl->length = 9156 gfc_get_int_expr (gfc_charlen_int_kind, NULL, 9157 target->value.character.length); 9158 } 9159 else if ((!sym->ts.u.cl->length 9160 || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT) 9161 && target->expr_type != EXPR_VARIABLE) 9162 { 9163 if (!sym->ts.deferred) 9164 { 9165 sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL); 9166 sym->ts.deferred = 1; 9167 } 9168 9169 /* This is reset in trans-stmt.c after the assignment 9170 of the target expression to the associate name. */ 9171 sym->attr.allocatable = 1; 9172 } 9173 } 9174 9175 /* If the target is a good class object, so is the associate variable. */ 9176 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok) 9177 sym->attr.class_ok = 1; 9178 } 9179 9180 9181 /* Ensure that SELECT TYPE expressions have the correct rank and a full 9182 array reference, where necessary. The symbols are artificial and so 9183 the dimension attribute and arrayspec can also be set. In addition, 9184 sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS. 9185 This is corrected here as well.*/ 9186 9187 static void 9188 fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2, 9189 int rank, gfc_ref *ref) 9190 { 9191 gfc_ref *nref = (*expr1)->ref; 9192 gfc_symbol *sym1 = (*expr1)->symtree->n.sym; 9193 gfc_symbol *sym2 = expr2 ? expr2->symtree->n.sym : NULL; 9194 (*expr1)->rank = rank; 9195 if (sym1->ts.type == BT_CLASS) 9196 { 9197 if ((*expr1)->ts.type != BT_CLASS) 9198 (*expr1)->ts = sym1->ts; 9199 9200 CLASS_DATA (sym1)->attr.dimension = 1; 9201 if (CLASS_DATA (sym1)->as == NULL && sym2) 9202 CLASS_DATA (sym1)->as 9203 = gfc_copy_array_spec (CLASS_DATA (sym2)->as); 9204 } 9205 else 9206 { 9207 sym1->attr.dimension = 1; 9208 if (sym1->as == NULL && sym2) 9209 sym1->as = gfc_copy_array_spec (sym2->as); 9210 } 9211 9212 for (; nref; nref = nref->next) 9213 if (nref->next == NULL) 9214 break; 9215 9216 if (ref && nref && nref->type != REF_ARRAY) 9217 nref->next = gfc_copy_ref (ref); 9218 else if (ref && !nref) 9219 (*expr1)->ref = gfc_copy_ref (ref); 9220 } 9221 9222 9223 static gfc_expr * 9224 build_loc_call (gfc_expr *sym_expr) 9225 { 9226 gfc_expr *loc_call; 9227 loc_call = gfc_get_expr (); 9228 loc_call->expr_type = EXPR_FUNCTION; 9229 gfc_get_sym_tree ("_loc", gfc_current_ns, &loc_call->symtree, false); 9230 loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE; 9231 loc_call->symtree->n.sym->attr.intrinsic = 1; 9232 loc_call->symtree->n.sym->result = loc_call->symtree->n.sym; 9233 gfc_commit_symbol (loc_call->symtree->n.sym); 9234 loc_call->ts.type = BT_INTEGER; 9235 loc_call->ts.kind = gfc_index_integer_kind; 9236 loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC); 9237 loc_call->value.function.actual = gfc_get_actual_arglist (); 9238 loc_call->value.function.actual->expr = sym_expr; 9239 loc_call->where = sym_expr->where; 9240 return loc_call; 9241 } 9242 9243 /* Resolve a SELECT TYPE statement. */ 9244 9245 static void 9246 resolve_select_type (gfc_code *code, gfc_namespace *old_ns) 9247 { 9248 gfc_symbol *selector_type; 9249 gfc_code *body, *new_st, *if_st, *tail; 9250 gfc_code *class_is = NULL, *default_case = NULL; 9251 gfc_case *c; 9252 gfc_symtree *st; 9253 char name[GFC_MAX_SYMBOL_LEN + 12 + 1]; 9254 gfc_namespace *ns; 9255 int error = 0; 9256 int rank = 0; 9257 gfc_ref* ref = NULL; 9258 gfc_expr *selector_expr = NULL; 9259 9260 ns = code->ext.block.ns; 9261 gfc_resolve (ns); 9262 9263 /* Check for F03:C813. */ 9264 if (code->expr1->ts.type != BT_CLASS 9265 && !(code->expr2 && code->expr2->ts.type == BT_CLASS)) 9266 { 9267 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement " 9268 "at %L", &code->loc); 9269 return; 9270 } 9271 9272 if (!code->expr1->symtree->n.sym->attr.class_ok) 9273 return; 9274 9275 if (code->expr2) 9276 { 9277 gfc_ref *ref2 = NULL; 9278 for (ref = code->expr2->ref; ref != NULL; ref = ref->next) 9279 if (ref->type == REF_COMPONENT 9280 && ref->u.c.component->ts.type == BT_CLASS) 9281 ref2 = ref; 9282 9283 if (ref2) 9284 { 9285 if (code->expr1->symtree->n.sym->attr.untyped) 9286 code->expr1->symtree->n.sym->ts = ref2->u.c.component->ts; 9287 selector_type = CLASS_DATA (ref2->u.c.component)->ts.u.derived; 9288 } 9289 else 9290 { 9291 if (code->expr1->symtree->n.sym->attr.untyped) 9292 code->expr1->symtree->n.sym->ts = code->expr2->ts; 9293 selector_type = CLASS_DATA (code->expr2) 9294 ? CLASS_DATA (code->expr2)->ts.u.derived : code->expr2->ts.u.derived; 9295 } 9296 9297 if (code->expr2->rank 9298 && code->expr1->ts.type == BT_CLASS 9299 && CLASS_DATA (code->expr1)->as) 9300 CLASS_DATA (code->expr1)->as->rank = code->expr2->rank; 9301 9302 /* F2008: C803 The selector expression must not be coindexed. */ 9303 if (gfc_is_coindexed (code->expr2)) 9304 { 9305 gfc_error ("Selector at %L must not be coindexed", 9306 &code->expr2->where); 9307 return; 9308 } 9309 9310 } 9311 else 9312 { 9313 selector_type = CLASS_DATA (code->expr1)->ts.u.derived; 9314 9315 if (gfc_is_coindexed (code->expr1)) 9316 { 9317 gfc_error ("Selector at %L must not be coindexed", 9318 &code->expr1->where); 9319 return; 9320 } 9321 } 9322 9323 /* Loop over TYPE IS / CLASS IS cases. */ 9324 for (body = code->block; body; body = body->block) 9325 { 9326 c = body->ext.block.case_list; 9327 9328 if (!error) 9329 { 9330 /* Check for repeated cases. */ 9331 for (tail = code->block; tail; tail = tail->block) 9332 { 9333 gfc_case *d = tail->ext.block.case_list; 9334 if (tail == body) 9335 break; 9336 9337 if (c->ts.type == d->ts.type 9338 && ((c->ts.type == BT_DERIVED 9339 && c->ts.u.derived && d->ts.u.derived 9340 && !strcmp (c->ts.u.derived->name, 9341 d->ts.u.derived->name)) 9342 || c->ts.type == BT_UNKNOWN 9343 || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS) 9344 && c->ts.kind == d->ts.kind))) 9345 { 9346 gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L", 9347 &c->where, &d->where); 9348 return; 9349 } 9350 } 9351 } 9352 9353 /* Check F03:C815. */ 9354 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS) 9355 && selector_type 9356 && !selector_type->attr.unlimited_polymorphic 9357 && !gfc_type_is_extensible (c->ts.u.derived)) 9358 { 9359 gfc_error ("Derived type %qs at %L must be extensible", 9360 c->ts.u.derived->name, &c->where); 9361 error++; 9362 continue; 9363 } 9364 9365 /* Check F03:C816. */ 9366 if (c->ts.type != BT_UNKNOWN 9367 && selector_type && !selector_type->attr.unlimited_polymorphic 9368 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS) 9369 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived))) 9370 { 9371 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS) 9372 gfc_error ("Derived type %qs at %L must be an extension of %qs", 9373 c->ts.u.derived->name, &c->where, selector_type->name); 9374 else 9375 gfc_error ("Unexpected intrinsic type %qs at %L", 9376 gfc_basic_typename (c->ts.type), &c->where); 9377 error++; 9378 continue; 9379 } 9380 9381 /* Check F03:C814. */ 9382 if (c->ts.type == BT_CHARACTER 9383 && (c->ts.u.cl->length != NULL || c->ts.deferred)) 9384 { 9385 gfc_error ("The type-spec at %L shall specify that each length " 9386 "type parameter is assumed", &c->where); 9387 error++; 9388 continue; 9389 } 9390 9391 /* Intercept the DEFAULT case. */ 9392 if (c->ts.type == BT_UNKNOWN) 9393 { 9394 /* Check F03:C818. */ 9395 if (default_case) 9396 { 9397 gfc_error ("The DEFAULT CASE at %L cannot be followed " 9398 "by a second DEFAULT CASE at %L", 9399 &default_case->ext.block.case_list->where, &c->where); 9400 error++; 9401 continue; 9402 } 9403 9404 default_case = body; 9405 } 9406 } 9407 9408 if (error > 0) 9409 return; 9410 9411 /* Transform SELECT TYPE statement to BLOCK and associate selector to 9412 target if present. If there are any EXIT statements referring to the 9413 SELECT TYPE construct, this is no problem because the gfc_code 9414 reference stays the same and EXIT is equally possible from the BLOCK 9415 it is changed to. */ 9416 code->op = EXEC_BLOCK; 9417 if (code->expr2) 9418 { 9419 gfc_association_list* assoc; 9420 9421 assoc = gfc_get_association_list (); 9422 assoc->st = code->expr1->symtree; 9423 assoc->target = gfc_copy_expr (code->expr2); 9424 assoc->target->where = code->expr2->where; 9425 /* assoc->variable will be set by resolve_assoc_var. */ 9426 9427 code->ext.block.assoc = assoc; 9428 code->expr1->symtree->n.sym->assoc = assoc; 9429 9430 resolve_assoc_var (code->expr1->symtree->n.sym, false); 9431 } 9432 else 9433 code->ext.block.assoc = NULL; 9434 9435 /* Ensure that the selector rank and arrayspec are available to 9436 correct expressions in which they might be missing. */ 9437 if (code->expr2 && code->expr2->rank) 9438 { 9439 rank = code->expr2->rank; 9440 for (ref = code->expr2->ref; ref; ref = ref->next) 9441 if (ref->next == NULL) 9442 break; 9443 if (ref && ref->type == REF_ARRAY) 9444 ref = gfc_copy_ref (ref); 9445 9446 /* Fixup expr1 if necessary. */ 9447 if (rank) 9448 fixup_array_ref (&code->expr1, code->expr2, rank, ref); 9449 } 9450 else if (code->expr1->rank) 9451 { 9452 rank = code->expr1->rank; 9453 for (ref = code->expr1->ref; ref; ref = ref->next) 9454 if (ref->next == NULL) 9455 break; 9456 if (ref && ref->type == REF_ARRAY) 9457 ref = gfc_copy_ref (ref); 9458 } 9459 9460 /* Add EXEC_SELECT to switch on type. */ 9461 new_st = gfc_get_code (code->op); 9462 new_st->expr1 = code->expr1; 9463 new_st->expr2 = code->expr2; 9464 new_st->block = code->block; 9465 code->expr1 = code->expr2 = NULL; 9466 code->block = NULL; 9467 if (!ns->code) 9468 ns->code = new_st; 9469 else 9470 ns->code->next = new_st; 9471 code = new_st; 9472 code->op = EXEC_SELECT_TYPE; 9473 9474 /* Use the intrinsic LOC function to generate an integer expression 9475 for the vtable of the selector. Note that the rank of the selector 9476 expression has to be set to zero. */ 9477 gfc_add_vptr_component (code->expr1); 9478 code->expr1->rank = 0; 9479 code->expr1 = build_loc_call (code->expr1); 9480 selector_expr = code->expr1->value.function.actual->expr; 9481 9482 /* Loop over TYPE IS / CLASS IS cases. */ 9483 for (body = code->block; body; body = body->block) 9484 { 9485 gfc_symbol *vtab; 9486 gfc_expr *e; 9487 c = body->ext.block.case_list; 9488 9489 /* Generate an index integer expression for address of the 9490 TYPE/CLASS vtable and store it in c->low. The hash expression 9491 is stored in c->high and is used to resolve intrinsic cases. */ 9492 if (c->ts.type != BT_UNKNOWN) 9493 { 9494 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS) 9495 { 9496 vtab = gfc_find_derived_vtab (c->ts.u.derived); 9497 gcc_assert (vtab); 9498 c->high = gfc_get_int_expr (gfc_integer_4_kind, NULL, 9499 c->ts.u.derived->hash_value); 9500 } 9501 else 9502 { 9503 vtab = gfc_find_vtab (&c->ts); 9504 gcc_assert (vtab && CLASS_DATA (vtab)->initializer); 9505 e = CLASS_DATA (vtab)->initializer; 9506 c->high = gfc_copy_expr (e); 9507 if (c->high->ts.kind != gfc_integer_4_kind) 9508 { 9509 gfc_typespec ts; 9510 ts.kind = gfc_integer_4_kind; 9511 ts.type = BT_INTEGER; 9512 gfc_convert_type_warn (c->high, &ts, 2, 0); 9513 } 9514 } 9515 9516 e = gfc_lval_expr_from_sym (vtab); 9517 c->low = build_loc_call (e); 9518 } 9519 else 9520 continue; 9521 9522 /* Associate temporary to selector. This should only be done 9523 when this case is actually true, so build a new ASSOCIATE 9524 that does precisely this here (instead of using the 9525 'global' one). */ 9526 9527 if (c->ts.type == BT_CLASS) 9528 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name); 9529 else if (c->ts.type == BT_DERIVED) 9530 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name); 9531 else if (c->ts.type == BT_CHARACTER) 9532 { 9533 HOST_WIDE_INT charlen = 0; 9534 if (c->ts.u.cl && c->ts.u.cl->length 9535 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT) 9536 charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer); 9537 snprintf (name, sizeof (name), 9538 "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d", 9539 gfc_basic_typename (c->ts.type), charlen, c->ts.kind); 9540 } 9541 else 9542 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type), 9543 c->ts.kind); 9544 9545 st = gfc_find_symtree (ns->sym_root, name); 9546 gcc_assert (st->n.sym->assoc); 9547 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree); 9548 st->n.sym->assoc->target->where = selector_expr->where; 9549 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN) 9550 { 9551 gfc_add_data_component (st->n.sym->assoc->target); 9552 /* Fixup the target expression if necessary. */ 9553 if (rank) 9554 fixup_array_ref (&st->n.sym->assoc->target, NULL, rank, ref); 9555 } 9556 9557 new_st = gfc_get_code (EXEC_BLOCK); 9558 new_st->ext.block.ns = gfc_build_block_ns (ns); 9559 new_st->ext.block.ns->code = body->next; 9560 body->next = new_st; 9561 9562 /* Chain in the new list only if it is marked as dangling. Otherwise 9563 there is a CASE label overlap and this is already used. Just ignore, 9564 the error is diagnosed elsewhere. */ 9565 if (st->n.sym->assoc->dangling) 9566 { 9567 new_st->ext.block.assoc = st->n.sym->assoc; 9568 st->n.sym->assoc->dangling = 0; 9569 } 9570 9571 resolve_assoc_var (st->n.sym, false); 9572 } 9573 9574 /* Take out CLASS IS cases for separate treatment. */ 9575 body = code; 9576 while (body && body->block) 9577 { 9578 if (body->block->ext.block.case_list->ts.type == BT_CLASS) 9579 { 9580 /* Add to class_is list. */ 9581 if (class_is == NULL) 9582 { 9583 class_is = body->block; 9584 tail = class_is; 9585 } 9586 else 9587 { 9588 for (tail = class_is; tail->block; tail = tail->block) ; 9589 tail->block = body->block; 9590 tail = tail->block; 9591 } 9592 /* Remove from EXEC_SELECT list. */ 9593 body->block = body->block->block; 9594 tail->block = NULL; 9595 } 9596 else 9597 body = body->block; 9598 } 9599 9600 if (class_is) 9601 { 9602 gfc_symbol *vtab; 9603 9604 if (!default_case) 9605 { 9606 /* Add a default case to hold the CLASS IS cases. */ 9607 for (tail = code; tail->block; tail = tail->block) ; 9608 tail->block = gfc_get_code (EXEC_SELECT_TYPE); 9609 tail = tail->block; 9610 tail->ext.block.case_list = gfc_get_case (); 9611 tail->ext.block.case_list->ts.type = BT_UNKNOWN; 9612 tail->next = NULL; 9613 default_case = tail; 9614 } 9615 9616 /* More than one CLASS IS block? */ 9617 if (class_is->block) 9618 { 9619 gfc_code **c1,*c2; 9620 bool swapped; 9621 /* Sort CLASS IS blocks by extension level. */ 9622 do 9623 { 9624 swapped = false; 9625 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block)) 9626 { 9627 c2 = (*c1)->block; 9628 /* F03:C817 (check for doubles). */ 9629 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value 9630 == c2->ext.block.case_list->ts.u.derived->hash_value) 9631 { 9632 gfc_error ("Double CLASS IS block in SELECT TYPE " 9633 "statement at %L", 9634 &c2->ext.block.case_list->where); 9635 return; 9636 } 9637 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension 9638 < c2->ext.block.case_list->ts.u.derived->attr.extension) 9639 { 9640 /* Swap. */ 9641 (*c1)->block = c2->block; 9642 c2->block = *c1; 9643 *c1 = c2; 9644 swapped = true; 9645 } 9646 } 9647 } 9648 while (swapped); 9649 } 9650 9651 /* Generate IF chain. */ 9652 if_st = gfc_get_code (EXEC_IF); 9653 new_st = if_st; 9654 for (body = class_is; body; body = body->block) 9655 { 9656 new_st->block = gfc_get_code (EXEC_IF); 9657 new_st = new_st->block; 9658 /* Set up IF condition: Call _gfortran_is_extension_of. */ 9659 new_st->expr1 = gfc_get_expr (); 9660 new_st->expr1->expr_type = EXPR_FUNCTION; 9661 new_st->expr1->ts.type = BT_LOGICAL; 9662 new_st->expr1->ts.kind = 4; 9663 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of")); 9664 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym); 9665 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF; 9666 /* Set up arguments. */ 9667 new_st->expr1->value.function.actual = gfc_get_actual_arglist (); 9668 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree); 9669 new_st->expr1->value.function.actual->expr->where = code->loc; 9670 new_st->expr1->where = code->loc; 9671 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr); 9672 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived); 9673 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name); 9674 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist (); 9675 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st); 9676 new_st->expr1->value.function.actual->next->expr->where = code->loc; 9677 new_st->next = body->next; 9678 } 9679 if (default_case->next) 9680 { 9681 new_st->block = gfc_get_code (EXEC_IF); 9682 new_st = new_st->block; 9683 new_st->next = default_case->next; 9684 } 9685 9686 /* Replace CLASS DEFAULT code by the IF chain. */ 9687 default_case->next = if_st; 9688 } 9689 9690 /* Resolve the internal code. This cannot be done earlier because 9691 it requires that the sym->assoc of selectors is set already. */ 9692 gfc_current_ns = ns; 9693 gfc_resolve_blocks (code->block, gfc_current_ns); 9694 gfc_current_ns = old_ns; 9695 9696 if (ref) 9697 free (ref); 9698 } 9699 9700 9701 /* Resolve a SELECT RANK statement. */ 9702 9703 static void 9704 resolve_select_rank (gfc_code *code, gfc_namespace *old_ns) 9705 { 9706 gfc_namespace *ns; 9707 gfc_code *body, *new_st, *tail; 9708 gfc_case *c; 9709 char tname[GFC_MAX_SYMBOL_LEN + 7]; 9710 char name[2 * GFC_MAX_SYMBOL_LEN]; 9711 gfc_symtree *st; 9712 gfc_expr *selector_expr = NULL; 9713 int case_value; 9714 HOST_WIDE_INT charlen = 0; 9715 9716 ns = code->ext.block.ns; 9717 gfc_resolve (ns); 9718 9719 code->op = EXEC_BLOCK; 9720 if (code->expr2) 9721 { 9722 gfc_association_list* assoc; 9723 9724 assoc = gfc_get_association_list (); 9725 assoc->st = code->expr1->symtree; 9726 assoc->target = gfc_copy_expr (code->expr2); 9727 assoc->target->where = code->expr2->where; 9728 /* assoc->variable will be set by resolve_assoc_var. */ 9729 9730 code->ext.block.assoc = assoc; 9731 code->expr1->symtree->n.sym->assoc = assoc; 9732 9733 resolve_assoc_var (code->expr1->symtree->n.sym, false); 9734 } 9735 else 9736 code->ext.block.assoc = NULL; 9737 9738 /* Loop over RANK cases. Note that returning on the errors causes a 9739 cascade of further errors because the case blocks do not compile 9740 correctly. */ 9741 for (body = code->block; body; body = body->block) 9742 { 9743 c = body->ext.block.case_list; 9744 if (c->low) 9745 case_value = (int) mpz_get_si (c->low->value.integer); 9746 else 9747 case_value = -2; 9748 9749 /* Check for repeated cases. */ 9750 for (tail = code->block; tail; tail = tail->block) 9751 { 9752 gfc_case *d = tail->ext.block.case_list; 9753 int case_value2; 9754 9755 if (tail == body) 9756 break; 9757 9758 /* Check F2018: C1153. */ 9759 if (!c->low && !d->low) 9760 gfc_error ("RANK DEFAULT at %L is repeated at %L", 9761 &c->where, &d->where); 9762 9763 if (!c->low || !d->low) 9764 continue; 9765 9766 /* Check F2018: C1153. */ 9767 case_value2 = (int) mpz_get_si (d->low->value.integer); 9768 if ((case_value == case_value2) && case_value == -1) 9769 gfc_error ("RANK (*) at %L is repeated at %L", 9770 &c->where, &d->where); 9771 else if (case_value == case_value2) 9772 gfc_error ("RANK (%i) at %L is repeated at %L", 9773 case_value, &c->where, &d->where); 9774 } 9775 9776 if (!c->low) 9777 continue; 9778 9779 /* Check F2018: C1155. */ 9780 if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable 9781 || gfc_expr_attr (code->expr1).pointer)) 9782 gfc_error ("RANK (*) at %L cannot be used with the pointer or " 9783 "allocatable selector at %L", &c->where, &code->expr1->where); 9784 9785 if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable 9786 || gfc_expr_attr (code->expr1).pointer)) 9787 gfc_error ("RANK (*) at %L cannot be used with the pointer or " 9788 "allocatable selector at %L", &c->where, &code->expr1->where); 9789 } 9790 9791 /* Add EXEC_SELECT to switch on rank. */ 9792 new_st = gfc_get_code (code->op); 9793 new_st->expr1 = code->expr1; 9794 new_st->expr2 = code->expr2; 9795 new_st->block = code->block; 9796 code->expr1 = code->expr2 = NULL; 9797 code->block = NULL; 9798 if (!ns->code) 9799 ns->code = new_st; 9800 else 9801 ns->code->next = new_st; 9802 code = new_st; 9803 code->op = EXEC_SELECT_RANK; 9804 9805 selector_expr = code->expr1; 9806 9807 /* Loop over SELECT RANK cases. */ 9808 for (body = code->block; body; body = body->block) 9809 { 9810 c = body->ext.block.case_list; 9811 int case_value; 9812 9813 /* Pass on the default case. */ 9814 if (c->low == NULL) 9815 continue; 9816 9817 /* Associate temporary to selector. This should only be done 9818 when this case is actually true, so build a new ASSOCIATE 9819 that does precisely this here (instead of using the 9820 'global' one). */ 9821 if (c->ts.type == BT_CHARACTER && c->ts.u.cl && c->ts.u.cl->length 9822 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT) 9823 charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer); 9824 9825 if (c->ts.type == BT_CLASS) 9826 sprintf (tname, "class_%s", c->ts.u.derived->name); 9827 else if (c->ts.type == BT_DERIVED) 9828 sprintf (tname, "type_%s", c->ts.u.derived->name); 9829 else if (c->ts.type != BT_CHARACTER) 9830 sprintf (tname, "%s_%d", gfc_basic_typename (c->ts.type), c->ts.kind); 9831 else 9832 sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d", 9833 gfc_basic_typename (c->ts.type), charlen, c->ts.kind); 9834 9835 case_value = (int) mpz_get_si (c->low->value.integer); 9836 if (case_value >= 0) 9837 sprintf (name, "__tmp_%s_rank_%d", tname, case_value); 9838 else 9839 sprintf (name, "__tmp_%s_rank_m%d", tname, -case_value); 9840 9841 st = gfc_find_symtree (ns->sym_root, name); 9842 gcc_assert (st->n.sym->assoc); 9843 9844 st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree); 9845 st->n.sym->assoc->target->where = selector_expr->where; 9846 9847 new_st = gfc_get_code (EXEC_BLOCK); 9848 new_st->ext.block.ns = gfc_build_block_ns (ns); 9849 new_st->ext.block.ns->code = body->next; 9850 body->next = new_st; 9851 9852 /* Chain in the new list only if it is marked as dangling. Otherwise 9853 there is a CASE label overlap and this is already used. Just ignore, 9854 the error is diagnosed elsewhere. */ 9855 if (st->n.sym->assoc->dangling) 9856 { 9857 new_st->ext.block.assoc = st->n.sym->assoc; 9858 st->n.sym->assoc->dangling = 0; 9859 } 9860 9861 resolve_assoc_var (st->n.sym, false); 9862 } 9863 9864 gfc_current_ns = ns; 9865 gfc_resolve_blocks (code->block, gfc_current_ns); 9866 gfc_current_ns = old_ns; 9867 } 9868 9869 9870 /* Resolve a transfer statement. This is making sure that: 9871 -- a derived type being transferred has only non-pointer components 9872 -- a derived type being transferred doesn't have private components, unless 9873 it's being transferred from the module where the type was defined 9874 -- we're not trying to transfer a whole assumed size array. */ 9875 9876 static void 9877 resolve_transfer (gfc_code *code) 9878 { 9879 gfc_symbol *sym, *derived; 9880 gfc_ref *ref; 9881 gfc_expr *exp; 9882 bool write = false; 9883 bool formatted = false; 9884 gfc_dt *dt = code->ext.dt; 9885 gfc_symbol *dtio_sub = NULL; 9886 9887 exp = code->expr1; 9888 9889 while (exp != NULL && exp->expr_type == EXPR_OP 9890 && exp->value.op.op == INTRINSIC_PARENTHESES) 9891 exp = exp->value.op.op1; 9892 9893 if (exp && exp->expr_type == EXPR_NULL 9894 && code->ext.dt) 9895 { 9896 gfc_error ("Invalid context for NULL () intrinsic at %L", 9897 &exp->where); 9898 return; 9899 } 9900 9901 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE 9902 && exp->expr_type != EXPR_FUNCTION 9903 && exp->expr_type != EXPR_STRUCTURE)) 9904 return; 9905 9906 /* If we are reading, the variable will be changed. Note that 9907 code->ext.dt may be NULL if the TRANSFER is related to 9908 an INQUIRE statement -- but in this case, we are not reading, either. */ 9909 if (dt && dt->dt_io_kind->value.iokind == M_READ 9910 && !gfc_check_vardef_context (exp, false, false, false, 9911 _("item in READ"))) 9912 return; 9913 9914 const gfc_typespec *ts = exp->expr_type == EXPR_STRUCTURE 9915 || exp->expr_type == EXPR_FUNCTION 9916 ? &exp->ts : &exp->symtree->n.sym->ts; 9917 9918 /* Go to actual component transferred. */ 9919 for (ref = exp->ref; ref; ref = ref->next) 9920 if (ref->type == REF_COMPONENT) 9921 ts = &ref->u.c.component->ts; 9922 9923 if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE 9924 && (ts->type == BT_DERIVED || ts->type == BT_CLASS)) 9925 { 9926 derived = ts->u.derived; 9927 9928 /* Determine when to use the formatted DTIO procedure. */ 9929 if (dt && (dt->format_expr || dt->format_label)) 9930 formatted = true; 9931 9932 write = dt->dt_io_kind->value.iokind == M_WRITE 9933 || dt->dt_io_kind->value.iokind == M_PRINT; 9934 dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted); 9935 9936 if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE) 9937 { 9938 dt->udtio = exp; 9939 sym = exp->symtree->n.sym->ns->proc_name; 9940 /* Check to see if this is a nested DTIO call, with the 9941 dummy as the io-list object. */ 9942 if (sym && sym == dtio_sub && sym->formal 9943 && sym->formal->sym == exp->symtree->n.sym 9944 && exp->ref == NULL) 9945 { 9946 if (!sym->attr.recursive) 9947 { 9948 gfc_error ("DTIO %s procedure at %L must be recursive", 9949 sym->name, &sym->declared_at); 9950 return; 9951 } 9952 } 9953 } 9954 } 9955 9956 if (ts->type == BT_CLASS && dtio_sub == NULL) 9957 { 9958 gfc_error ("Data transfer element at %L cannot be polymorphic unless " 9959 "it is processed by a defined input/output procedure", 9960 &code->loc); 9961 return; 9962 } 9963 9964 if (ts->type == BT_DERIVED) 9965 { 9966 /* Check that transferred derived type doesn't contain POINTER 9967 components unless it is processed by a defined input/output 9968 procedure". */ 9969 if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL) 9970 { 9971 gfc_error ("Data transfer element at %L cannot have POINTER " 9972 "components unless it is processed by a defined " 9973 "input/output procedure", &code->loc); 9974 return; 9975 } 9976 9977 /* F08:C935. */ 9978 if (ts->u.derived->attr.proc_pointer_comp) 9979 { 9980 gfc_error ("Data transfer element at %L cannot have " 9981 "procedure pointer components", &code->loc); 9982 return; 9983 } 9984 9985 if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL) 9986 { 9987 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE " 9988 "components unless it is processed by a defined " 9989 "input/output procedure", &code->loc); 9990 return; 9991 } 9992 9993 /* C_PTR and C_FUNPTR have private components which means they cannot 9994 be printed. However, if -std=gnu and not -pedantic, allow 9995 the component to be printed to help debugging. */ 9996 if (ts->u.derived->ts.f90_type == BT_VOID) 9997 { 9998 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L " 9999 "cannot have PRIVATE components", &code->loc)) 10000 return; 10001 } 10002 else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL) 10003 { 10004 gfc_error ("Data transfer element at %L cannot have " 10005 "PRIVATE components unless it is processed by " 10006 "a defined input/output procedure", &code->loc); 10007 return; 10008 } 10009 } 10010 10011 if (exp->expr_type == EXPR_STRUCTURE) 10012 return; 10013 10014 sym = exp->symtree->n.sym; 10015 10016 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref 10017 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL) 10018 { 10019 gfc_error ("Data transfer element at %L cannot be a full reference to " 10020 "an assumed-size array", &code->loc); 10021 return; 10022 } 10023 } 10024 10025 10026 /*********** Toplevel code resolution subroutines ***********/ 10027 10028 /* Find the set of labels that are reachable from this block. We also 10029 record the last statement in each block. */ 10030 10031 static void 10032 find_reachable_labels (gfc_code *block) 10033 { 10034 gfc_code *c; 10035 10036 if (!block) 10037 return; 10038 10039 cs_base->reachable_labels = bitmap_alloc (&labels_obstack); 10040 10041 /* Collect labels in this block. We don't keep those corresponding 10042 to END {IF|SELECT}, these are checked in resolve_branch by going 10043 up through the code_stack. */ 10044 for (c = block; c; c = c->next) 10045 { 10046 if (c->here && c->op != EXEC_END_NESTED_BLOCK) 10047 bitmap_set_bit (cs_base->reachable_labels, c->here->value); 10048 } 10049 10050 /* Merge with labels from parent block. */ 10051 if (cs_base->prev) 10052 { 10053 gcc_assert (cs_base->prev->reachable_labels); 10054 bitmap_ior_into (cs_base->reachable_labels, 10055 cs_base->prev->reachable_labels); 10056 } 10057 } 10058 10059 10060 static void 10061 resolve_lock_unlock_event (gfc_code *code) 10062 { 10063 if (code->expr1->expr_type == EXPR_FUNCTION 10064 && code->expr1->value.function.isym 10065 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET) 10066 remove_caf_get_intrinsic (code->expr1); 10067 10068 if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK) 10069 && (code->expr1->ts.type != BT_DERIVED 10070 || code->expr1->expr_type != EXPR_VARIABLE 10071 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV 10072 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE 10073 || code->expr1->rank != 0 10074 || (!gfc_is_coarray (code->expr1) && 10075 !gfc_is_coindexed (code->expr1)))) 10076 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE", 10077 &code->expr1->where); 10078 else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT) 10079 && (code->expr1->ts.type != BT_DERIVED 10080 || code->expr1->expr_type != EXPR_VARIABLE 10081 || code->expr1->ts.u.derived->from_intmod 10082 != INTMOD_ISO_FORTRAN_ENV 10083 || code->expr1->ts.u.derived->intmod_sym_id 10084 != ISOFORTRAN_EVENT_TYPE 10085 || code->expr1->rank != 0)) 10086 gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE", 10087 &code->expr1->where); 10088 else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1) 10089 && !gfc_is_coindexed (code->expr1)) 10090 gfc_error ("Event variable argument at %L must be a coarray or coindexed", 10091 &code->expr1->where); 10092 else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1)) 10093 gfc_error ("Event variable argument at %L must be a coarray but not " 10094 "coindexed", &code->expr1->where); 10095 10096 /* Check STAT. */ 10097 if (code->expr2 10098 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0 10099 || code->expr2->expr_type != EXPR_VARIABLE)) 10100 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable", 10101 &code->expr2->where); 10102 10103 if (code->expr2 10104 && !gfc_check_vardef_context (code->expr2, false, false, false, 10105 _("STAT variable"))) 10106 return; 10107 10108 /* Check ERRMSG. */ 10109 if (code->expr3 10110 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0 10111 || code->expr3->expr_type != EXPR_VARIABLE)) 10112 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable", 10113 &code->expr3->where); 10114 10115 if (code->expr3 10116 && !gfc_check_vardef_context (code->expr3, false, false, false, 10117 _("ERRMSG variable"))) 10118 return; 10119 10120 /* Check for LOCK the ACQUIRED_LOCK. */ 10121 if (code->op != EXEC_EVENT_WAIT && code->expr4 10122 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0 10123 || code->expr4->expr_type != EXPR_VARIABLE)) 10124 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL " 10125 "variable", &code->expr4->where); 10126 10127 if (code->op != EXEC_EVENT_WAIT && code->expr4 10128 && !gfc_check_vardef_context (code->expr4, false, false, false, 10129 _("ACQUIRED_LOCK variable"))) 10130 return; 10131 10132 /* Check for EVENT WAIT the UNTIL_COUNT. */ 10133 if (code->op == EXEC_EVENT_WAIT && code->expr4) 10134 { 10135 if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER 10136 || code->expr4->rank != 0) 10137 gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER " 10138 "expression", &code->expr4->where); 10139 } 10140 } 10141 10142 10143 static void 10144 resolve_critical (gfc_code *code) 10145 { 10146 gfc_symtree *symtree; 10147 gfc_symbol *lock_type; 10148 char name[GFC_MAX_SYMBOL_LEN]; 10149 static int serial = 0; 10150 10151 if (flag_coarray != GFC_FCOARRAY_LIB) 10152 return; 10153 10154 symtree = gfc_find_symtree (gfc_current_ns->sym_root, 10155 GFC_PREFIX ("lock_type")); 10156 if (symtree) 10157 lock_type = symtree->n.sym; 10158 else 10159 { 10160 if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree, 10161 false) != 0) 10162 gcc_unreachable (); 10163 lock_type = symtree->n.sym; 10164 lock_type->attr.flavor = FL_DERIVED; 10165 lock_type->attr.zero_comp = 1; 10166 lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV; 10167 lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE; 10168 } 10169 10170 sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++); 10171 if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0) 10172 gcc_unreachable (); 10173 10174 code->resolved_sym = symtree->n.sym; 10175 symtree->n.sym->attr.flavor = FL_VARIABLE; 10176 symtree->n.sym->attr.referenced = 1; 10177 symtree->n.sym->attr.artificial = 1; 10178 symtree->n.sym->attr.codimension = 1; 10179 symtree->n.sym->ts.type = BT_DERIVED; 10180 symtree->n.sym->ts.u.derived = lock_type; 10181 symtree->n.sym->as = gfc_get_array_spec (); 10182 symtree->n.sym->as->corank = 1; 10183 symtree->n.sym->as->type = AS_EXPLICIT; 10184 symtree->n.sym->as->cotype = AS_EXPLICIT; 10185 symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, 10186 NULL, 1); 10187 gfc_commit_symbols(); 10188 } 10189 10190 10191 static void 10192 resolve_sync (gfc_code *code) 10193 { 10194 /* Check imageset. The * case matches expr1 == NULL. */ 10195 if (code->expr1) 10196 { 10197 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1) 10198 gfc_error ("Imageset argument at %L must be a scalar or rank-1 " 10199 "INTEGER expression", &code->expr1->where); 10200 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0 10201 && mpz_cmp_si (code->expr1->value.integer, 1) < 0) 10202 gfc_error ("Imageset argument at %L must between 1 and num_images()", 10203 &code->expr1->where); 10204 else if (code->expr1->expr_type == EXPR_ARRAY 10205 && gfc_simplify_expr (code->expr1, 0)) 10206 { 10207 gfc_constructor *cons; 10208 cons = gfc_constructor_first (code->expr1->value.constructor); 10209 for (; cons; cons = gfc_constructor_next (cons)) 10210 if (cons->expr->expr_type == EXPR_CONSTANT 10211 && mpz_cmp_si (cons->expr->value.integer, 1) < 0) 10212 gfc_error ("Imageset argument at %L must between 1 and " 10213 "num_images()", &cons->expr->where); 10214 } 10215 } 10216 10217 /* Check STAT. */ 10218 gfc_resolve_expr (code->expr2); 10219 if (code->expr2 10220 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0 10221 || code->expr2->expr_type != EXPR_VARIABLE)) 10222 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable", 10223 &code->expr2->where); 10224 10225 /* Check ERRMSG. */ 10226 gfc_resolve_expr (code->expr3); 10227 if (code->expr3 10228 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0 10229 || code->expr3->expr_type != EXPR_VARIABLE)) 10230 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable", 10231 &code->expr3->where); 10232 } 10233 10234 10235 /* Given a branch to a label, see if the branch is conforming. 10236 The code node describes where the branch is located. */ 10237 10238 static void 10239 resolve_branch (gfc_st_label *label, gfc_code *code) 10240 { 10241 code_stack *stack; 10242 10243 if (label == NULL) 10244 return; 10245 10246 /* Step one: is this a valid branching target? */ 10247 10248 if (label->defined == ST_LABEL_UNKNOWN) 10249 { 10250 gfc_error ("Label %d referenced at %L is never defined", label->value, 10251 &code->loc); 10252 return; 10253 } 10254 10255 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET) 10256 { 10257 gfc_error ("Statement at %L is not a valid branch target statement " 10258 "for the branch statement at %L", &label->where, &code->loc); 10259 return; 10260 } 10261 10262 /* Step two: make sure this branch is not a branch to itself ;-) */ 10263 10264 if (code->here == label) 10265 { 10266 gfc_warning (0, 10267 "Branch at %L may result in an infinite loop", &code->loc); 10268 return; 10269 } 10270 10271 /* Step three: See if the label is in the same block as the 10272 branching statement. The hard work has been done by setting up 10273 the bitmap reachable_labels. */ 10274 10275 if (bitmap_bit_p (cs_base->reachable_labels, label->value)) 10276 { 10277 /* Check now whether there is a CRITICAL construct; if so, check 10278 whether the label is still visible outside of the CRITICAL block, 10279 which is invalid. */ 10280 for (stack = cs_base; stack; stack = stack->prev) 10281 { 10282 if (stack->current->op == EXEC_CRITICAL 10283 && bitmap_bit_p (stack->reachable_labels, label->value)) 10284 gfc_error ("GOTO statement at %L leaves CRITICAL construct for " 10285 "label at %L", &code->loc, &label->where); 10286 else if (stack->current->op == EXEC_DO_CONCURRENT 10287 && bitmap_bit_p (stack->reachable_labels, label->value)) 10288 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct " 10289 "for label at %L", &code->loc, &label->where); 10290 } 10291 10292 return; 10293 } 10294 10295 /* Step four: If we haven't found the label in the bitmap, it may 10296 still be the label of the END of the enclosing block, in which 10297 case we find it by going up the code_stack. */ 10298 10299 for (stack = cs_base; stack; stack = stack->prev) 10300 { 10301 if (stack->current->next && stack->current->next->here == label) 10302 break; 10303 if (stack->current->op == EXEC_CRITICAL) 10304 { 10305 /* Note: A label at END CRITICAL does not leave the CRITICAL 10306 construct as END CRITICAL is still part of it. */ 10307 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label" 10308 " at %L", &code->loc, &label->where); 10309 return; 10310 } 10311 else if (stack->current->op == EXEC_DO_CONCURRENT) 10312 { 10313 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for " 10314 "label at %L", &code->loc, &label->where); 10315 return; 10316 } 10317 } 10318 10319 if (stack) 10320 { 10321 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK); 10322 return; 10323 } 10324 10325 /* The label is not in an enclosing block, so illegal. This was 10326 allowed in Fortran 66, so we allow it as extension. No 10327 further checks are necessary in this case. */ 10328 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block " 10329 "as the GOTO statement at %L", &label->where, 10330 &code->loc); 10331 return; 10332 } 10333 10334 10335 /* Check whether EXPR1 has the same shape as EXPR2. */ 10336 10337 static bool 10338 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2) 10339 { 10340 mpz_t shape[GFC_MAX_DIMENSIONS]; 10341 mpz_t shape2[GFC_MAX_DIMENSIONS]; 10342 bool result = false; 10343 int i; 10344 10345 /* Compare the rank. */ 10346 if (expr1->rank != expr2->rank) 10347 return result; 10348 10349 /* Compare the size of each dimension. */ 10350 for (i=0; i<expr1->rank; i++) 10351 { 10352 if (!gfc_array_dimen_size (expr1, i, &shape[i])) 10353 goto ignore; 10354 10355 if (!gfc_array_dimen_size (expr2, i, &shape2[i])) 10356 goto ignore; 10357 10358 if (mpz_cmp (shape[i], shape2[i])) 10359 goto over; 10360 } 10361 10362 /* When either of the two expression is an assumed size array, we 10363 ignore the comparison of dimension sizes. */ 10364 ignore: 10365 result = true; 10366 10367 over: 10368 gfc_clear_shape (shape, i); 10369 gfc_clear_shape (shape2, i); 10370 return result; 10371 } 10372 10373 10374 /* Check whether a WHERE assignment target or a WHERE mask expression 10375 has the same shape as the outmost WHERE mask expression. */ 10376 10377 static void 10378 resolve_where (gfc_code *code, gfc_expr *mask) 10379 { 10380 gfc_code *cblock; 10381 gfc_code *cnext; 10382 gfc_expr *e = NULL; 10383 10384 cblock = code->block; 10385 10386 /* Store the first WHERE mask-expr of the WHERE statement or construct. 10387 In case of nested WHERE, only the outmost one is stored. */ 10388 if (mask == NULL) /* outmost WHERE */ 10389 e = cblock->expr1; 10390 else /* inner WHERE */ 10391 e = mask; 10392 10393 while (cblock) 10394 { 10395 if (cblock->expr1) 10396 { 10397 /* Check if the mask-expr has a consistent shape with the 10398 outmost WHERE mask-expr. */ 10399 if (!resolve_where_shape (cblock->expr1, e)) 10400 gfc_error ("WHERE mask at %L has inconsistent shape", 10401 &cblock->expr1->where); 10402 } 10403 10404 /* the assignment statement of a WHERE statement, or the first 10405 statement in where-body-construct of a WHERE construct */ 10406 cnext = cblock->next; 10407 while (cnext) 10408 { 10409 switch (cnext->op) 10410 { 10411 /* WHERE assignment statement */ 10412 case EXEC_ASSIGN: 10413 10414 /* Check shape consistent for WHERE assignment target. */ 10415 if (e && !resolve_where_shape (cnext->expr1, e)) 10416 gfc_error ("WHERE assignment target at %L has " 10417 "inconsistent shape", &cnext->expr1->where); 10418 break; 10419 10420 10421 case EXEC_ASSIGN_CALL: 10422 resolve_call (cnext); 10423 if (!cnext->resolved_sym->attr.elemental) 10424 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L", 10425 &cnext->ext.actual->expr->where); 10426 break; 10427 10428 /* WHERE or WHERE construct is part of a where-body-construct */ 10429 case EXEC_WHERE: 10430 resolve_where (cnext, e); 10431 break; 10432 10433 default: 10434 gfc_error ("Unsupported statement inside WHERE at %L", 10435 &cnext->loc); 10436 } 10437 /* the next statement within the same where-body-construct */ 10438 cnext = cnext->next; 10439 } 10440 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */ 10441 cblock = cblock->block; 10442 } 10443 } 10444 10445 10446 /* Resolve assignment in FORALL construct. 10447 NVAR is the number of FORALL index variables, and VAR_EXPR records the 10448 FORALL index variables. */ 10449 10450 static void 10451 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr) 10452 { 10453 int n; 10454 10455 for (n = 0; n < nvar; n++) 10456 { 10457 gfc_symbol *forall_index; 10458 10459 forall_index = var_expr[n]->symtree->n.sym; 10460 10461 /* Check whether the assignment target is one of the FORALL index 10462 variable. */ 10463 if ((code->expr1->expr_type == EXPR_VARIABLE) 10464 && (code->expr1->symtree->n.sym == forall_index)) 10465 gfc_error ("Assignment to a FORALL index variable at %L", 10466 &code->expr1->where); 10467 else 10468 { 10469 /* If one of the FORALL index variables doesn't appear in the 10470 assignment variable, then there could be a many-to-one 10471 assignment. Emit a warning rather than an error because the 10472 mask could be resolving this problem. */ 10473 if (!find_forall_index (code->expr1, forall_index, 0)) 10474 gfc_warning (0, "The FORALL with index %qs is not used on the " 10475 "left side of the assignment at %L and so might " 10476 "cause multiple assignment to this object", 10477 var_expr[n]->symtree->name, &code->expr1->where); 10478 } 10479 } 10480 } 10481 10482 10483 /* Resolve WHERE statement in FORALL construct. */ 10484 10485 static void 10486 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar, 10487 gfc_expr **var_expr) 10488 { 10489 gfc_code *cblock; 10490 gfc_code *cnext; 10491 10492 cblock = code->block; 10493 while (cblock) 10494 { 10495 /* the assignment statement of a WHERE statement, or the first 10496 statement in where-body-construct of a WHERE construct */ 10497 cnext = cblock->next; 10498 while (cnext) 10499 { 10500 switch (cnext->op) 10501 { 10502 /* WHERE assignment statement */ 10503 case EXEC_ASSIGN: 10504 gfc_resolve_assign_in_forall (cnext, nvar, var_expr); 10505 break; 10506 10507 /* WHERE operator assignment statement */ 10508 case EXEC_ASSIGN_CALL: 10509 resolve_call (cnext); 10510 if (!cnext->resolved_sym->attr.elemental) 10511 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L", 10512 &cnext->ext.actual->expr->where); 10513 break; 10514 10515 /* WHERE or WHERE construct is part of a where-body-construct */ 10516 case EXEC_WHERE: 10517 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr); 10518 break; 10519 10520 default: 10521 gfc_error ("Unsupported statement inside WHERE at %L", 10522 &cnext->loc); 10523 } 10524 /* the next statement within the same where-body-construct */ 10525 cnext = cnext->next; 10526 } 10527 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */ 10528 cblock = cblock->block; 10529 } 10530 } 10531 10532 10533 /* Traverse the FORALL body to check whether the following errors exist: 10534 1. For assignment, check if a many-to-one assignment happens. 10535 2. For WHERE statement, check the WHERE body to see if there is any 10536 many-to-one assignment. */ 10537 10538 static void 10539 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr) 10540 { 10541 gfc_code *c; 10542 10543 c = code->block->next; 10544 while (c) 10545 { 10546 switch (c->op) 10547 { 10548 case EXEC_ASSIGN: 10549 case EXEC_POINTER_ASSIGN: 10550 gfc_resolve_assign_in_forall (c, nvar, var_expr); 10551 break; 10552 10553 case EXEC_ASSIGN_CALL: 10554 resolve_call (c); 10555 break; 10556 10557 /* Because the gfc_resolve_blocks() will handle the nested FORALL, 10558 there is no need to handle it here. */ 10559 case EXEC_FORALL: 10560 break; 10561 case EXEC_WHERE: 10562 gfc_resolve_where_code_in_forall(c, nvar, var_expr); 10563 break; 10564 default: 10565 break; 10566 } 10567 /* The next statement in the FORALL body. */ 10568 c = c->next; 10569 } 10570 } 10571 10572 10573 /* Counts the number of iterators needed inside a forall construct, including 10574 nested forall constructs. This is used to allocate the needed memory 10575 in gfc_resolve_forall. */ 10576 10577 static int 10578 gfc_count_forall_iterators (gfc_code *code) 10579 { 10580 int max_iters, sub_iters, current_iters; 10581 gfc_forall_iterator *fa; 10582 10583 gcc_assert(code->op == EXEC_FORALL); 10584 max_iters = 0; 10585 current_iters = 0; 10586 10587 for (fa = code->ext.forall_iterator; fa; fa = fa->next) 10588 current_iters ++; 10589 10590 code = code->block->next; 10591 10592 while (code) 10593 { 10594 if (code->op == EXEC_FORALL) 10595 { 10596 sub_iters = gfc_count_forall_iterators (code); 10597 if (sub_iters > max_iters) 10598 max_iters = sub_iters; 10599 } 10600 code = code->next; 10601 } 10602 10603 return current_iters + max_iters; 10604 } 10605 10606 10607 /* Given a FORALL construct, first resolve the FORALL iterator, then call 10608 gfc_resolve_forall_body to resolve the FORALL body. */ 10609 10610 static void 10611 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save) 10612 { 10613 static gfc_expr **var_expr; 10614 static int total_var = 0; 10615 static int nvar = 0; 10616 int i, old_nvar, tmp; 10617 gfc_forall_iterator *fa; 10618 10619 old_nvar = nvar; 10620 10621 if (!gfc_notify_std (GFC_STD_F2018_OBS, "FORALL construct at %L", &code->loc)) 10622 return; 10623 10624 /* Start to resolve a FORALL construct */ 10625 if (forall_save == 0) 10626 { 10627 /* Count the total number of FORALL indices in the nested FORALL 10628 construct in order to allocate the VAR_EXPR with proper size. */ 10629 total_var = gfc_count_forall_iterators (code); 10630 10631 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */ 10632 var_expr = XCNEWVEC (gfc_expr *, total_var); 10633 } 10634 10635 /* The information about FORALL iterator, including FORALL indices start, end 10636 and stride. An outer FORALL indice cannot appear in start, end or stride. */ 10637 for (fa = code->ext.forall_iterator; fa; fa = fa->next) 10638 { 10639 /* Fortran 20008: C738 (R753). */ 10640 if (fa->var->ref && fa->var->ref->type == REF_ARRAY) 10641 { 10642 gfc_error ("FORALL index-name at %L must be a scalar variable " 10643 "of type integer", &fa->var->where); 10644 continue; 10645 } 10646 10647 /* Check if any outer FORALL index name is the same as the current 10648 one. */ 10649 for (i = 0; i < nvar; i++) 10650 { 10651 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym) 10652 gfc_error ("An outer FORALL construct already has an index " 10653 "with this name %L", &fa->var->where); 10654 } 10655 10656 /* Record the current FORALL index. */ 10657 var_expr[nvar] = gfc_copy_expr (fa->var); 10658 10659 nvar++; 10660 10661 /* No memory leak. */ 10662 gcc_assert (nvar <= total_var); 10663 } 10664 10665 /* Resolve the FORALL body. */ 10666 gfc_resolve_forall_body (code, nvar, var_expr); 10667 10668 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */ 10669 gfc_resolve_blocks (code->block, ns); 10670 10671 tmp = nvar; 10672 nvar = old_nvar; 10673 /* Free only the VAR_EXPRs allocated in this frame. */ 10674 for (i = nvar; i < tmp; i++) 10675 gfc_free_expr (var_expr[i]); 10676 10677 if (nvar == 0) 10678 { 10679 /* We are in the outermost FORALL construct. */ 10680 gcc_assert (forall_save == 0); 10681 10682 /* VAR_EXPR is not needed any more. */ 10683 free (var_expr); 10684 total_var = 0; 10685 } 10686 } 10687 10688 10689 /* Resolve a BLOCK construct statement. */ 10690 10691 static void 10692 resolve_block_construct (gfc_code* code) 10693 { 10694 /* Resolve the BLOCK's namespace. */ 10695 gfc_resolve (code->ext.block.ns); 10696 10697 /* For an ASSOCIATE block, the associations (and their targets) are already 10698 resolved during resolve_symbol. */ 10699 } 10700 10701 10702 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and 10703 DO code nodes. */ 10704 10705 void 10706 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns) 10707 { 10708 bool t; 10709 10710 for (; b; b = b->block) 10711 { 10712 t = gfc_resolve_expr (b->expr1); 10713 if (!gfc_resolve_expr (b->expr2)) 10714 t = false; 10715 10716 switch (b->op) 10717 { 10718 case EXEC_IF: 10719 if (t && b->expr1 != NULL 10720 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0)) 10721 gfc_error ("IF clause at %L requires a scalar LOGICAL expression", 10722 &b->expr1->where); 10723 break; 10724 10725 case EXEC_WHERE: 10726 if (t 10727 && b->expr1 != NULL 10728 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0)) 10729 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array", 10730 &b->expr1->where); 10731 break; 10732 10733 case EXEC_GOTO: 10734 resolve_branch (b->label1, b); 10735 break; 10736 10737 case EXEC_BLOCK: 10738 resolve_block_construct (b); 10739 break; 10740 10741 case EXEC_SELECT: 10742 case EXEC_SELECT_TYPE: 10743 case EXEC_SELECT_RANK: 10744 case EXEC_FORALL: 10745 case EXEC_DO: 10746 case EXEC_DO_WHILE: 10747 case EXEC_DO_CONCURRENT: 10748 case EXEC_CRITICAL: 10749 case EXEC_READ: 10750 case EXEC_WRITE: 10751 case EXEC_IOLENGTH: 10752 case EXEC_WAIT: 10753 break; 10754 10755 case EXEC_OMP_ATOMIC: 10756 case EXEC_OACC_ATOMIC: 10757 { 10758 gfc_omp_atomic_op aop 10759 = (gfc_omp_atomic_op) (b->ext.omp_atomic & GFC_OMP_ATOMIC_MASK); 10760 10761 /* Verify this before calling gfc_resolve_code, which might 10762 change it. */ 10763 gcc_assert (b->next && b->next->op == EXEC_ASSIGN); 10764 gcc_assert (((aop != GFC_OMP_ATOMIC_CAPTURE) 10765 && b->next->next == NULL) 10766 || ((aop == GFC_OMP_ATOMIC_CAPTURE) 10767 && b->next->next != NULL 10768 && b->next->next->op == EXEC_ASSIGN 10769 && b->next->next->next == NULL)); 10770 } 10771 break; 10772 10773 case EXEC_OACC_PARALLEL_LOOP: 10774 case EXEC_OACC_PARALLEL: 10775 case EXEC_OACC_KERNELS_LOOP: 10776 case EXEC_OACC_KERNELS: 10777 case EXEC_OACC_SERIAL_LOOP: 10778 case EXEC_OACC_SERIAL: 10779 case EXEC_OACC_DATA: 10780 case EXEC_OACC_HOST_DATA: 10781 case EXEC_OACC_LOOP: 10782 case EXEC_OACC_UPDATE: 10783 case EXEC_OACC_WAIT: 10784 case EXEC_OACC_CACHE: 10785 case EXEC_OACC_ENTER_DATA: 10786 case EXEC_OACC_EXIT_DATA: 10787 case EXEC_OACC_ROUTINE: 10788 case EXEC_OMP_CRITICAL: 10789 case EXEC_OMP_DISTRIBUTE: 10790 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO: 10791 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD: 10792 case EXEC_OMP_DISTRIBUTE_SIMD: 10793 case EXEC_OMP_DO: 10794 case EXEC_OMP_DO_SIMD: 10795 case EXEC_OMP_MASTER: 10796 case EXEC_OMP_ORDERED: 10797 case EXEC_OMP_PARALLEL: 10798 case EXEC_OMP_PARALLEL_DO: 10799 case EXEC_OMP_PARALLEL_DO_SIMD: 10800 case EXEC_OMP_PARALLEL_SECTIONS: 10801 case EXEC_OMP_PARALLEL_WORKSHARE: 10802 case EXEC_OMP_SECTIONS: 10803 case EXEC_OMP_SIMD: 10804 case EXEC_OMP_SINGLE: 10805 case EXEC_OMP_TARGET: 10806 case EXEC_OMP_TARGET_DATA: 10807 case EXEC_OMP_TARGET_ENTER_DATA: 10808 case EXEC_OMP_TARGET_EXIT_DATA: 10809 case EXEC_OMP_TARGET_PARALLEL: 10810 case EXEC_OMP_TARGET_PARALLEL_DO: 10811 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD: 10812 case EXEC_OMP_TARGET_SIMD: 10813 case EXEC_OMP_TARGET_TEAMS: 10814 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE: 10815 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO: 10816 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: 10817 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD: 10818 case EXEC_OMP_TARGET_UPDATE: 10819 case EXEC_OMP_TASK: 10820 case EXEC_OMP_TASKGROUP: 10821 case EXEC_OMP_TASKLOOP: 10822 case EXEC_OMP_TASKLOOP_SIMD: 10823 case EXEC_OMP_TASKWAIT: 10824 case EXEC_OMP_TASKYIELD: 10825 case EXEC_OMP_TEAMS: 10826 case EXEC_OMP_TEAMS_DISTRIBUTE: 10827 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO: 10828 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: 10829 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD: 10830 case EXEC_OMP_WORKSHARE: 10831 break; 10832 10833 default: 10834 gfc_internal_error ("gfc_resolve_blocks(): Bad block type"); 10835 } 10836 10837 gfc_resolve_code (b->next, ns); 10838 } 10839 } 10840 10841 10842 /* Does everything to resolve an ordinary assignment. Returns true 10843 if this is an interface assignment. */ 10844 static bool 10845 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns) 10846 { 10847 bool rval = false; 10848 gfc_expr *lhs; 10849 gfc_expr *rhs; 10850 int n; 10851 gfc_ref *ref; 10852 symbol_attribute attr; 10853 10854 if (gfc_extend_assign (code, ns)) 10855 { 10856 gfc_expr** rhsptr; 10857 10858 if (code->op == EXEC_ASSIGN_CALL) 10859 { 10860 lhs = code->ext.actual->expr; 10861 rhsptr = &code->ext.actual->next->expr; 10862 } 10863 else 10864 { 10865 gfc_actual_arglist* args; 10866 gfc_typebound_proc* tbp; 10867 10868 gcc_assert (code->op == EXEC_COMPCALL); 10869 10870 args = code->expr1->value.compcall.actual; 10871 lhs = args->expr; 10872 rhsptr = &args->next->expr; 10873 10874 tbp = code->expr1->value.compcall.tbp; 10875 gcc_assert (!tbp->is_generic); 10876 } 10877 10878 /* Make a temporary rhs when there is a default initializer 10879 and rhs is the same symbol as the lhs. */ 10880 if ((*rhsptr)->expr_type == EXPR_VARIABLE 10881 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED 10882 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived) 10883 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym)) 10884 *rhsptr = gfc_get_parentheses (*rhsptr); 10885 10886 return true; 10887 } 10888 10889 lhs = code->expr1; 10890 rhs = code->expr2; 10891 10892 if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL) 10893 && rhs->ts.type == BT_CHARACTER 10894 && (rhs->expr_type != EXPR_CONSTANT || !flag_dec_char_conversions)) 10895 { 10896 /* Use of -fdec-char-conversions allows assignment of character data 10897 to non-character variables. This not permited for nonconstant 10898 strings. */ 10899 gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs), 10900 gfc_typename (lhs), &rhs->where); 10901 return false; 10902 } 10903 10904 /* Handle the case of a BOZ literal on the RHS. */ 10905 if (rhs->ts.type == BT_BOZ) 10906 { 10907 if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA " 10908 "statement value nor an actual argument of " 10909 "INT/REAL/DBLE/CMPLX intrinsic subprogram", 10910 &rhs->where)) 10911 return false; 10912 10913 switch (lhs->ts.type) 10914 { 10915 case BT_INTEGER: 10916 if (!gfc_boz2int (rhs, lhs->ts.kind)) 10917 return false; 10918 break; 10919 case BT_REAL: 10920 if (!gfc_boz2real (rhs, lhs->ts.kind)) 10921 return false; 10922 break; 10923 default: 10924 gfc_error ("Invalid use of BOZ literal constant at %L", &rhs->where); 10925 return false; 10926 } 10927 } 10928 10929 if (lhs->ts.type == BT_CHARACTER && warn_character_truncation) 10930 { 10931 HOST_WIDE_INT llen = 0, rlen = 0; 10932 if (lhs->ts.u.cl != NULL 10933 && lhs->ts.u.cl->length != NULL 10934 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT) 10935 llen = gfc_mpz_get_hwi (lhs->ts.u.cl->length->value.integer); 10936 10937 if (rhs->expr_type == EXPR_CONSTANT) 10938 rlen = rhs->value.character.length; 10939 10940 else if (rhs->ts.u.cl != NULL 10941 && rhs->ts.u.cl->length != NULL 10942 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT) 10943 rlen = gfc_mpz_get_hwi (rhs->ts.u.cl->length->value.integer); 10944 10945 if (rlen && llen && rlen > llen) 10946 gfc_warning_now (OPT_Wcharacter_truncation, 10947 "CHARACTER expression will be truncated " 10948 "in assignment (%ld/%ld) at %L", 10949 (long) llen, (long) rlen, &code->loc); 10950 } 10951 10952 /* Ensure that a vector index expression for the lvalue is evaluated 10953 to a temporary if the lvalue symbol is referenced in it. */ 10954 if (lhs->rank) 10955 { 10956 for (ref = lhs->ref; ref; ref= ref->next) 10957 if (ref->type == REF_ARRAY) 10958 { 10959 for (n = 0; n < ref->u.ar.dimen; n++) 10960 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR 10961 && gfc_find_sym_in_expr (lhs->symtree->n.sym, 10962 ref->u.ar.start[n])) 10963 ref->u.ar.start[n] 10964 = gfc_get_parentheses (ref->u.ar.start[n]); 10965 } 10966 } 10967 10968 if (gfc_pure (NULL)) 10969 { 10970 if (lhs->ts.type == BT_DERIVED 10971 && lhs->expr_type == EXPR_VARIABLE 10972 && lhs->ts.u.derived->attr.pointer_comp 10973 && rhs->expr_type == EXPR_VARIABLE 10974 && (gfc_impure_variable (rhs->symtree->n.sym) 10975 || gfc_is_coindexed (rhs))) 10976 { 10977 /* F2008, C1283. */ 10978 if (gfc_is_coindexed (rhs)) 10979 gfc_error ("Coindexed expression at %L is assigned to " 10980 "a derived type variable with a POINTER " 10981 "component in a PURE procedure", 10982 &rhs->where); 10983 else 10984 /* F2008, C1283 (4). */ 10985 gfc_error ("In a pure subprogram an INTENT(IN) dummy argument " 10986 "shall not be used as the expr at %L of an intrinsic " 10987 "assignment statement in which the variable is of a " 10988 "derived type if the derived type has a pointer " 10989 "component at any level of component selection.", 10990 &rhs->where); 10991 return rval; 10992 } 10993 10994 /* Fortran 2008, C1283. */ 10995 if (gfc_is_coindexed (lhs)) 10996 { 10997 gfc_error ("Assignment to coindexed variable at %L in a PURE " 10998 "procedure", &rhs->where); 10999 return rval; 11000 } 11001 } 11002 11003 if (gfc_implicit_pure (NULL)) 11004 { 11005 if (lhs->expr_type == EXPR_VARIABLE 11006 && lhs->symtree->n.sym != gfc_current_ns->proc_name 11007 && lhs->symtree->n.sym->ns != gfc_current_ns) 11008 gfc_unset_implicit_pure (NULL); 11009 11010 if (lhs->ts.type == BT_DERIVED 11011 && lhs->expr_type == EXPR_VARIABLE 11012 && lhs->ts.u.derived->attr.pointer_comp 11013 && rhs->expr_type == EXPR_VARIABLE 11014 && (gfc_impure_variable (rhs->symtree->n.sym) 11015 || gfc_is_coindexed (rhs))) 11016 gfc_unset_implicit_pure (NULL); 11017 11018 /* Fortran 2008, C1283. */ 11019 if (gfc_is_coindexed (lhs)) 11020 gfc_unset_implicit_pure (NULL); 11021 } 11022 11023 /* F2008, 7.2.1.2. */ 11024 attr = gfc_expr_attr (lhs); 11025 if (lhs->ts.type == BT_CLASS && attr.allocatable) 11026 { 11027 if (attr.codimension) 11028 { 11029 gfc_error ("Assignment to polymorphic coarray at %L is not " 11030 "permitted", &lhs->where); 11031 return false; 11032 } 11033 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable " 11034 "polymorphic variable at %L", &lhs->where)) 11035 return false; 11036 if (!flag_realloc_lhs) 11037 { 11038 gfc_error ("Assignment to an allocatable polymorphic variable at %L " 11039 "requires %<-frealloc-lhs%>", &lhs->where); 11040 return false; 11041 } 11042 } 11043 else if (lhs->ts.type == BT_CLASS) 11044 { 11045 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic " 11046 "assignment at %L - check that there is a matching specific " 11047 "subroutine for '=' operator", &lhs->where); 11048 return false; 11049 } 11050 11051 bool lhs_coindexed = gfc_is_coindexed (lhs); 11052 11053 /* F2008, Section 7.2.1.2. */ 11054 if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs)) 11055 { 11056 gfc_error ("Coindexed variable must not have an allocatable ultimate " 11057 "component in assignment at %L", &lhs->where); 11058 return false; 11059 } 11060 11061 /* Assign the 'data' of a class object to a derived type. */ 11062 if (lhs->ts.type == BT_DERIVED 11063 && rhs->ts.type == BT_CLASS 11064 && rhs->expr_type != EXPR_ARRAY) 11065 gfc_add_data_component (rhs); 11066 11067 /* Make sure there is a vtable and, in particular, a _copy for the 11068 rhs type. */ 11069 if (lhs->ts.type == BT_CLASS && rhs->ts.type != BT_CLASS) 11070 gfc_find_vtab (&rhs->ts); 11071 11072 bool caf_convert_to_send = flag_coarray == GFC_FCOARRAY_LIB 11073 && (lhs_coindexed 11074 || (code->expr2->expr_type == EXPR_FUNCTION 11075 && code->expr2->value.function.isym 11076 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET 11077 && (code->expr1->rank == 0 || code->expr2->rank != 0) 11078 && !gfc_expr_attr (rhs).allocatable 11079 && !gfc_has_vector_subscript (rhs))); 11080 11081 gfc_check_assign (lhs, rhs, 1, !caf_convert_to_send); 11082 11083 /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a coindexed variable. 11084 Additionally, insert this code when the RHS is a CAF as we then use the 11085 GFC_ISYM_CAF_SEND intrinsic just to avoid a temporary; but do not do so if 11086 the LHS is (re)allocatable or has a vector subscript. If the LHS is a 11087 noncoindexed array and the RHS is a coindexed scalar, use the normal code 11088 path. */ 11089 if (caf_convert_to_send) 11090 { 11091 if (code->expr2->expr_type == EXPR_FUNCTION 11092 && code->expr2->value.function.isym 11093 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET) 11094 remove_caf_get_intrinsic (code->expr2); 11095 code->op = EXEC_CALL; 11096 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree, true); 11097 code->resolved_sym = code->symtree->n.sym; 11098 code->resolved_sym->attr.flavor = FL_PROCEDURE; 11099 code->resolved_sym->attr.intrinsic = 1; 11100 code->resolved_sym->attr.subroutine = 1; 11101 code->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND); 11102 gfc_commit_symbol (code->resolved_sym); 11103 code->ext.actual = gfc_get_actual_arglist (); 11104 code->ext.actual->expr = lhs; 11105 code->ext.actual->next = gfc_get_actual_arglist (); 11106 code->ext.actual->next->expr = rhs; 11107 code->expr1 = NULL; 11108 code->expr2 = NULL; 11109 } 11110 11111 return false; 11112 } 11113 11114 11115 /* Add a component reference onto an expression. */ 11116 11117 static void 11118 add_comp_ref (gfc_expr *e, gfc_component *c) 11119 { 11120 gfc_ref **ref; 11121 ref = &(e->ref); 11122 while (*ref) 11123 ref = &((*ref)->next); 11124 *ref = gfc_get_ref (); 11125 (*ref)->type = REF_COMPONENT; 11126 (*ref)->u.c.sym = e->ts.u.derived; 11127 (*ref)->u.c.component = c; 11128 e->ts = c->ts; 11129 11130 /* Add a full array ref, as necessary. */ 11131 if (c->as) 11132 { 11133 gfc_add_full_array_ref (e, c->as); 11134 e->rank = c->as->rank; 11135 } 11136 } 11137 11138 11139 /* Build an assignment. Keep the argument 'op' for future use, so that 11140 pointer assignments can be made. */ 11141 11142 static gfc_code * 11143 build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2, 11144 gfc_component *comp1, gfc_component *comp2, locus loc) 11145 { 11146 gfc_code *this_code; 11147 11148 this_code = gfc_get_code (op); 11149 this_code->next = NULL; 11150 this_code->expr1 = gfc_copy_expr (expr1); 11151 this_code->expr2 = gfc_copy_expr (expr2); 11152 this_code->loc = loc; 11153 if (comp1 && comp2) 11154 { 11155 add_comp_ref (this_code->expr1, comp1); 11156 add_comp_ref (this_code->expr2, comp2); 11157 } 11158 11159 return this_code; 11160 } 11161 11162 11163 /* Makes a temporary variable expression based on the characteristics of 11164 a given variable expression. */ 11165 11166 static gfc_expr* 11167 get_temp_from_expr (gfc_expr *e, gfc_namespace *ns) 11168 { 11169 static int serial = 0; 11170 char name[GFC_MAX_SYMBOL_LEN]; 11171 gfc_symtree *tmp; 11172 gfc_array_spec *as; 11173 gfc_array_ref *aref; 11174 gfc_ref *ref; 11175 11176 sprintf (name, GFC_PREFIX("DA%d"), serial++); 11177 gfc_get_sym_tree (name, ns, &tmp, false); 11178 gfc_add_type (tmp->n.sym, &e->ts, NULL); 11179 11180 if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_CHARACTER) 11181 tmp->n.sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind, 11182 NULL, 11183 e->value.character.length); 11184 11185 as = NULL; 11186 ref = NULL; 11187 aref = NULL; 11188 11189 /* Obtain the arrayspec for the temporary. */ 11190 if (e->rank && e->expr_type != EXPR_ARRAY 11191 && e->expr_type != EXPR_FUNCTION 11192 && e->expr_type != EXPR_OP) 11193 { 11194 aref = gfc_find_array_ref (e); 11195 if (e->expr_type == EXPR_VARIABLE 11196 && e->symtree->n.sym->as == aref->as) 11197 as = aref->as; 11198 else 11199 { 11200 for (ref = e->ref; ref; ref = ref->next) 11201 if (ref->type == REF_COMPONENT 11202 && ref->u.c.component->as == aref->as) 11203 { 11204 as = aref->as; 11205 break; 11206 } 11207 } 11208 } 11209 11210 /* Add the attributes and the arrayspec to the temporary. */ 11211 tmp->n.sym->attr = gfc_expr_attr (e); 11212 tmp->n.sym->attr.function = 0; 11213 tmp->n.sym->attr.result = 0; 11214 tmp->n.sym->attr.flavor = FL_VARIABLE; 11215 tmp->n.sym->attr.dummy = 0; 11216 tmp->n.sym->attr.intent = INTENT_UNKNOWN; 11217 11218 if (as) 11219 { 11220 tmp->n.sym->as = gfc_copy_array_spec (as); 11221 if (!ref) 11222 ref = e->ref; 11223 if (as->type == AS_DEFERRED) 11224 tmp->n.sym->attr.allocatable = 1; 11225 } 11226 else if (e->rank && (e->expr_type == EXPR_ARRAY 11227 || e->expr_type == EXPR_FUNCTION 11228 || e->expr_type == EXPR_OP)) 11229 { 11230 tmp->n.sym->as = gfc_get_array_spec (); 11231 tmp->n.sym->as->type = AS_DEFERRED; 11232 tmp->n.sym->as->rank = e->rank; 11233 tmp->n.sym->attr.allocatable = 1; 11234 tmp->n.sym->attr.dimension = 1; 11235 } 11236 else 11237 tmp->n.sym->attr.dimension = 0; 11238 11239 gfc_set_sym_referenced (tmp->n.sym); 11240 gfc_commit_symbol (tmp->n.sym); 11241 e = gfc_lval_expr_from_sym (tmp->n.sym); 11242 11243 /* Should the lhs be a section, use its array ref for the 11244 temporary expression. */ 11245 if (aref && aref->type != AR_FULL) 11246 { 11247 gfc_free_ref_list (e->ref); 11248 e->ref = gfc_copy_ref (ref); 11249 } 11250 return e; 11251 } 11252 11253 11254 /* Add one line of code to the code chain, making sure that 'head' and 11255 'tail' are appropriately updated. */ 11256 11257 static void 11258 add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail) 11259 { 11260 gcc_assert (this_code); 11261 if (*head == NULL) 11262 *head = *tail = *this_code; 11263 else 11264 *tail = gfc_append_code (*tail, *this_code); 11265 *this_code = NULL; 11266 } 11267 11268 11269 /* Counts the potential number of part array references that would 11270 result from resolution of typebound defined assignments. */ 11271 11272 static int 11273 nonscalar_typebound_assign (gfc_symbol *derived, int depth) 11274 { 11275 gfc_component *c; 11276 int c_depth = 0, t_depth; 11277 11278 for (c= derived->components; c; c = c->next) 11279 { 11280 if ((!gfc_bt_struct (c->ts.type) 11281 || c->attr.pointer 11282 || c->attr.allocatable 11283 || c->attr.proc_pointer_comp 11284 || c->attr.class_pointer 11285 || c->attr.proc_pointer) 11286 && !c->attr.defined_assign_comp) 11287 continue; 11288 11289 if (c->as && c_depth == 0) 11290 c_depth = 1; 11291 11292 if (c->ts.u.derived->attr.defined_assign_comp) 11293 t_depth = nonscalar_typebound_assign (c->ts.u.derived, 11294 c->as ? 1 : 0); 11295 else 11296 t_depth = 0; 11297 11298 c_depth = t_depth > c_depth ? t_depth : c_depth; 11299 } 11300 return depth + c_depth; 11301 } 11302 11303 11304 /* Implement 7.2.1.3 of the F08 standard: 11305 "An intrinsic assignment where the variable is of derived type is 11306 performed as if each component of the variable were assigned from the 11307 corresponding component of expr using pointer assignment (7.2.2) for 11308 each pointer component, defined assignment for each nonpointer 11309 nonallocatable component of a type that has a type-bound defined 11310 assignment consistent with the component, intrinsic assignment for 11311 each other nonpointer nonallocatable component, ..." 11312 11313 The pointer assignments are taken care of by the intrinsic 11314 assignment of the structure itself. This function recursively adds 11315 defined assignments where required. The recursion is accomplished 11316 by calling gfc_resolve_code. 11317 11318 When the lhs in a defined assignment has intent INOUT, we need a 11319 temporary for the lhs. In pseudo-code: 11320 11321 ! Only call function lhs once. 11322 if (lhs is not a constant or an variable) 11323 temp_x = expr2 11324 expr2 => temp_x 11325 ! Do the intrinsic assignment 11326 expr1 = expr2 11327 ! Now do the defined assignments 11328 do over components with typebound defined assignment [%cmp] 11329 #if one component's assignment procedure is INOUT 11330 t1 = expr1 11331 #if expr2 non-variable 11332 temp_x = expr2 11333 expr2 => temp_x 11334 # endif 11335 expr1 = expr2 11336 # for each cmp 11337 t1%cmp {defined=} expr2%cmp 11338 expr1%cmp = t1%cmp 11339 #else 11340 expr1 = expr2 11341 11342 # for each cmp 11343 expr1%cmp {defined=} expr2%cmp 11344 #endif 11345 */ 11346 11347 /* The temporary assignments have to be put on top of the additional 11348 code to avoid the result being changed by the intrinsic assignment. 11349 */ 11350 static int component_assignment_level = 0; 11351 static gfc_code *tmp_head = NULL, *tmp_tail = NULL; 11352 11353 static void 11354 generate_component_assignments (gfc_code **code, gfc_namespace *ns) 11355 { 11356 gfc_component *comp1, *comp2; 11357 gfc_code *this_code = NULL, *head = NULL, *tail = NULL; 11358 gfc_expr *t1; 11359 int error_count, depth; 11360 11361 gfc_get_errors (NULL, &error_count); 11362 11363 /* Filter out continuing processing after an error. */ 11364 if (error_count 11365 || (*code)->expr1->ts.type != BT_DERIVED 11366 || (*code)->expr2->ts.type != BT_DERIVED) 11367 return; 11368 11369 /* TODO: Handle more than one part array reference in assignments. */ 11370 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived, 11371 (*code)->expr1->rank ? 1 : 0); 11372 if (depth > 1) 11373 { 11374 gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not " 11375 "done because multiple part array references would " 11376 "occur in intermediate expressions.", &(*code)->loc); 11377 return; 11378 } 11379 11380 component_assignment_level++; 11381 11382 /* Create a temporary so that functions get called only once. */ 11383 if ((*code)->expr2->expr_type != EXPR_VARIABLE 11384 && (*code)->expr2->expr_type != EXPR_CONSTANT) 11385 { 11386 gfc_expr *tmp_expr; 11387 11388 /* Assign the rhs to the temporary. */ 11389 tmp_expr = get_temp_from_expr ((*code)->expr1, ns); 11390 this_code = build_assignment (EXEC_ASSIGN, 11391 tmp_expr, (*code)->expr2, 11392 NULL, NULL, (*code)->loc); 11393 /* Add the code and substitute the rhs expression. */ 11394 add_code_to_chain (&this_code, &tmp_head, &tmp_tail); 11395 gfc_free_expr ((*code)->expr2); 11396 (*code)->expr2 = tmp_expr; 11397 } 11398 11399 /* Do the intrinsic assignment. This is not needed if the lhs is one 11400 of the temporaries generated here, since the intrinsic assignment 11401 to the final result already does this. */ 11402 if ((*code)->expr1->symtree->n.sym->name[2] != '@') 11403 { 11404 this_code = build_assignment (EXEC_ASSIGN, 11405 (*code)->expr1, (*code)->expr2, 11406 NULL, NULL, (*code)->loc); 11407 add_code_to_chain (&this_code, &head, &tail); 11408 } 11409 11410 comp1 = (*code)->expr1->ts.u.derived->components; 11411 comp2 = (*code)->expr2->ts.u.derived->components; 11412 11413 t1 = NULL; 11414 for (; comp1; comp1 = comp1->next, comp2 = comp2->next) 11415 { 11416 bool inout = false; 11417 11418 /* The intrinsic assignment does the right thing for pointers 11419 of all kinds and allocatable components. */ 11420 if (!gfc_bt_struct (comp1->ts.type) 11421 || comp1->attr.pointer 11422 || comp1->attr.allocatable 11423 || comp1->attr.proc_pointer_comp 11424 || comp1->attr.class_pointer 11425 || comp1->attr.proc_pointer) 11426 continue; 11427 11428 /* Make an assigment for this component. */ 11429 this_code = build_assignment (EXEC_ASSIGN, 11430 (*code)->expr1, (*code)->expr2, 11431 comp1, comp2, (*code)->loc); 11432 11433 /* Convert the assignment if there is a defined assignment for 11434 this type. Otherwise, using the call from gfc_resolve_code, 11435 recurse into its components. */ 11436 gfc_resolve_code (this_code, ns); 11437 11438 if (this_code->op == EXEC_ASSIGN_CALL) 11439 { 11440 gfc_formal_arglist *dummy_args; 11441 gfc_symbol *rsym; 11442 /* Check that there is a typebound defined assignment. If not, 11443 then this must be a module defined assignment. We cannot 11444 use the defined_assign_comp attribute here because it must 11445 be this derived type that has the defined assignment and not 11446 a parent type. */ 11447 if (!(comp1->ts.u.derived->f2k_derived 11448 && comp1->ts.u.derived->f2k_derived 11449 ->tb_op[INTRINSIC_ASSIGN])) 11450 { 11451 gfc_free_statements (this_code); 11452 this_code = NULL; 11453 continue; 11454 } 11455 11456 /* If the first argument of the subroutine has intent INOUT 11457 a temporary must be generated and used instead. */ 11458 rsym = this_code->resolved_sym; 11459 dummy_args = gfc_sym_get_dummy_args (rsym); 11460 if (dummy_args 11461 && dummy_args->sym->attr.intent == INTENT_INOUT) 11462 { 11463 gfc_code *temp_code; 11464 inout = true; 11465 11466 /* Build the temporary required for the assignment and put 11467 it at the head of the generated code. */ 11468 if (!t1) 11469 { 11470 t1 = get_temp_from_expr ((*code)->expr1, ns); 11471 temp_code = build_assignment (EXEC_ASSIGN, 11472 t1, (*code)->expr1, 11473 NULL, NULL, (*code)->loc); 11474 11475 /* For allocatable LHS, check whether it is allocated. Note 11476 that allocatable components with defined assignment are 11477 not yet support. See PR 57696. */ 11478 if ((*code)->expr1->symtree->n.sym->attr.allocatable) 11479 { 11480 gfc_code *block; 11481 gfc_expr *e = 11482 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym); 11483 block = gfc_get_code (EXEC_IF); 11484 block->block = gfc_get_code (EXEC_IF); 11485 block->block->expr1 11486 = gfc_build_intrinsic_call (ns, 11487 GFC_ISYM_ALLOCATED, "allocated", 11488 (*code)->loc, 1, e); 11489 block->block->next = temp_code; 11490 temp_code = block; 11491 } 11492 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail); 11493 } 11494 11495 /* Replace the first actual arg with the component of the 11496 temporary. */ 11497 gfc_free_expr (this_code->ext.actual->expr); 11498 this_code->ext.actual->expr = gfc_copy_expr (t1); 11499 add_comp_ref (this_code->ext.actual->expr, comp1); 11500 11501 /* If the LHS variable is allocatable and wasn't allocated and 11502 the temporary is allocatable, pointer assign the address of 11503 the freshly allocated LHS to the temporary. */ 11504 if ((*code)->expr1->symtree->n.sym->attr.allocatable 11505 && gfc_expr_attr ((*code)->expr1).allocatable) 11506 { 11507 gfc_code *block; 11508 gfc_expr *cond; 11509 11510 cond = gfc_get_expr (); 11511 cond->ts.type = BT_LOGICAL; 11512 cond->ts.kind = gfc_default_logical_kind; 11513 cond->expr_type = EXPR_OP; 11514 cond->where = (*code)->loc; 11515 cond->value.op.op = INTRINSIC_NOT; 11516 cond->value.op.op1 = gfc_build_intrinsic_call (ns, 11517 GFC_ISYM_ALLOCATED, "allocated", 11518 (*code)->loc, 1, gfc_copy_expr (t1)); 11519 block = gfc_get_code (EXEC_IF); 11520 block->block = gfc_get_code (EXEC_IF); 11521 block->block->expr1 = cond; 11522 block->block->next = build_assignment (EXEC_POINTER_ASSIGN, 11523 t1, (*code)->expr1, 11524 NULL, NULL, (*code)->loc); 11525 add_code_to_chain (&block, &head, &tail); 11526 } 11527 } 11528 } 11529 else if (this_code->op == EXEC_ASSIGN && !this_code->next) 11530 { 11531 /* Don't add intrinsic assignments since they are already 11532 effected by the intrinsic assignment of the structure. */ 11533 gfc_free_statements (this_code); 11534 this_code = NULL; 11535 continue; 11536 } 11537 11538 add_code_to_chain (&this_code, &head, &tail); 11539 11540 if (t1 && inout) 11541 { 11542 /* Transfer the value to the final result. */ 11543 this_code = build_assignment (EXEC_ASSIGN, 11544 (*code)->expr1, t1, 11545 comp1, comp2, (*code)->loc); 11546 add_code_to_chain (&this_code, &head, &tail); 11547 } 11548 } 11549 11550 /* Put the temporary assignments at the top of the generated code. */ 11551 if (tmp_head && component_assignment_level == 1) 11552 { 11553 gfc_append_code (tmp_head, head); 11554 head = tmp_head; 11555 tmp_head = tmp_tail = NULL; 11556 } 11557 11558 // If we did a pointer assignment - thus, we need to ensure that the LHS is 11559 // not accidentally deallocated. Hence, nullify t1. 11560 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable 11561 && gfc_expr_attr ((*code)->expr1).allocatable) 11562 { 11563 gfc_code *block; 11564 gfc_expr *cond; 11565 gfc_expr *e; 11566 11567 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym); 11568 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated", 11569 (*code)->loc, 2, gfc_copy_expr (t1), e); 11570 block = gfc_get_code (EXEC_IF); 11571 block->block = gfc_get_code (EXEC_IF); 11572 block->block->expr1 = cond; 11573 block->block->next = build_assignment (EXEC_POINTER_ASSIGN, 11574 t1, gfc_get_null_expr (&(*code)->loc), 11575 NULL, NULL, (*code)->loc); 11576 gfc_append_code (tail, block); 11577 tail = block; 11578 } 11579 11580 /* Now attach the remaining code chain to the input code. Step on 11581 to the end of the new code since resolution is complete. */ 11582 gcc_assert ((*code)->op == EXEC_ASSIGN); 11583 tail->next = (*code)->next; 11584 /* Overwrite 'code' because this would place the intrinsic assignment 11585 before the temporary for the lhs is created. */ 11586 gfc_free_expr ((*code)->expr1); 11587 gfc_free_expr ((*code)->expr2); 11588 **code = *head; 11589 if (head != tail) 11590 free (head); 11591 *code = tail; 11592 11593 component_assignment_level--; 11594 } 11595 11596 11597 /* F2008: Pointer function assignments are of the form: 11598 ptr_fcn (args) = expr 11599 This function breaks these assignments into two statements: 11600 temporary_pointer => ptr_fcn(args) 11601 temporary_pointer = expr */ 11602 11603 static bool 11604 resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns) 11605 { 11606 gfc_expr *tmp_ptr_expr; 11607 gfc_code *this_code; 11608 gfc_component *comp; 11609 gfc_symbol *s; 11610 11611 if ((*code)->expr1->expr_type != EXPR_FUNCTION) 11612 return false; 11613 11614 /* Even if standard does not support this feature, continue to build 11615 the two statements to avoid upsetting frontend_passes.c. */ 11616 gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at " 11617 "%L", &(*code)->loc); 11618 11619 comp = gfc_get_proc_ptr_comp ((*code)->expr1); 11620 11621 if (comp) 11622 s = comp->ts.interface; 11623 else 11624 s = (*code)->expr1->symtree->n.sym; 11625 11626 if (s == NULL || !s->result->attr.pointer) 11627 { 11628 gfc_error ("The function result on the lhs of the assignment at " 11629 "%L must have the pointer attribute.", 11630 &(*code)->expr1->where); 11631 (*code)->op = EXEC_NOP; 11632 return false; 11633 } 11634 11635 tmp_ptr_expr = get_temp_from_expr ((*code)->expr2, ns); 11636 11637 /* get_temp_from_expression is set up for ordinary assignments. To that 11638 end, where array bounds are not known, arrays are made allocatable. 11639 Change the temporary to a pointer here. */ 11640 tmp_ptr_expr->symtree->n.sym->attr.pointer = 1; 11641 tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0; 11642 tmp_ptr_expr->where = (*code)->loc; 11643 11644 this_code = build_assignment (EXEC_ASSIGN, 11645 tmp_ptr_expr, (*code)->expr2, 11646 NULL, NULL, (*code)->loc); 11647 this_code->next = (*code)->next; 11648 (*code)->next = this_code; 11649 (*code)->op = EXEC_POINTER_ASSIGN; 11650 (*code)->expr2 = (*code)->expr1; 11651 (*code)->expr1 = tmp_ptr_expr; 11652 11653 return true; 11654 } 11655 11656 11657 /* Deferred character length assignments from an operator expression 11658 require a temporary because the character length of the lhs can 11659 change in the course of the assignment. */ 11660 11661 static bool 11662 deferred_op_assign (gfc_code **code, gfc_namespace *ns) 11663 { 11664 gfc_expr *tmp_expr; 11665 gfc_code *this_code; 11666 11667 if (!((*code)->expr1->ts.type == BT_CHARACTER 11668 && (*code)->expr1->ts.deferred && (*code)->expr1->rank 11669 && (*code)->expr2->expr_type == EXPR_OP)) 11670 return false; 11671 11672 if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1)) 11673 return false; 11674 11675 if (gfc_expr_attr ((*code)->expr1).pointer) 11676 return false; 11677 11678 tmp_expr = get_temp_from_expr ((*code)->expr1, ns); 11679 tmp_expr->where = (*code)->loc; 11680 11681 /* A new charlen is required to ensure that the variable string 11682 length is different to that of the original lhs. */ 11683 tmp_expr->ts.u.cl = gfc_get_charlen(); 11684 tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl; 11685 tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next; 11686 (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl; 11687 11688 tmp_expr->symtree->n.sym->ts.deferred = 1; 11689 11690 this_code = build_assignment (EXEC_ASSIGN, 11691 (*code)->expr1, 11692 gfc_copy_expr (tmp_expr), 11693 NULL, NULL, (*code)->loc); 11694 11695 (*code)->expr1 = tmp_expr; 11696 11697 this_code->next = (*code)->next; 11698 (*code)->next = this_code; 11699 11700 return true; 11701 } 11702 11703 11704 /* Given a block of code, recursively resolve everything pointed to by this 11705 code block. */ 11706 11707 void 11708 gfc_resolve_code (gfc_code *code, gfc_namespace *ns) 11709 { 11710 int omp_workshare_save; 11711 int forall_save, do_concurrent_save; 11712 code_stack frame; 11713 bool t; 11714 11715 frame.prev = cs_base; 11716 frame.head = code; 11717 cs_base = &frame; 11718 11719 find_reachable_labels (code); 11720 11721 for (; code; code = code->next) 11722 { 11723 frame.current = code; 11724 forall_save = forall_flag; 11725 do_concurrent_save = gfc_do_concurrent_flag; 11726 11727 if (code->op == EXEC_FORALL) 11728 { 11729 forall_flag = 1; 11730 gfc_resolve_forall (code, ns, forall_save); 11731 forall_flag = 2; 11732 } 11733 else if (code->block) 11734 { 11735 omp_workshare_save = -1; 11736 switch (code->op) 11737 { 11738 case EXEC_OACC_PARALLEL_LOOP: 11739 case EXEC_OACC_PARALLEL: 11740 case EXEC_OACC_KERNELS_LOOP: 11741 case EXEC_OACC_KERNELS: 11742 case EXEC_OACC_SERIAL_LOOP: 11743 case EXEC_OACC_SERIAL: 11744 case EXEC_OACC_DATA: 11745 case EXEC_OACC_HOST_DATA: 11746 case EXEC_OACC_LOOP: 11747 gfc_resolve_oacc_blocks (code, ns); 11748 break; 11749 case EXEC_OMP_PARALLEL_WORKSHARE: 11750 omp_workshare_save = omp_workshare_flag; 11751 omp_workshare_flag = 1; 11752 gfc_resolve_omp_parallel_blocks (code, ns); 11753 break; 11754 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO: 11755 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD: 11756 case EXEC_OMP_PARALLEL: 11757 case EXEC_OMP_PARALLEL_DO: 11758 case EXEC_OMP_PARALLEL_DO_SIMD: 11759 case EXEC_OMP_PARALLEL_SECTIONS: 11760 case EXEC_OMP_TARGET_PARALLEL: 11761 case EXEC_OMP_TARGET_PARALLEL_DO: 11762 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD: 11763 case EXEC_OMP_TARGET_TEAMS: 11764 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE: 11765 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO: 11766 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: 11767 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD: 11768 case EXEC_OMP_TASK: 11769 case EXEC_OMP_TASKLOOP: 11770 case EXEC_OMP_TASKLOOP_SIMD: 11771 case EXEC_OMP_TEAMS: 11772 case EXEC_OMP_TEAMS_DISTRIBUTE: 11773 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO: 11774 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: 11775 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD: 11776 omp_workshare_save = omp_workshare_flag; 11777 omp_workshare_flag = 0; 11778 gfc_resolve_omp_parallel_blocks (code, ns); 11779 break; 11780 case EXEC_OMP_DISTRIBUTE: 11781 case EXEC_OMP_DISTRIBUTE_SIMD: 11782 case EXEC_OMP_DO: 11783 case EXEC_OMP_DO_SIMD: 11784 case EXEC_OMP_SIMD: 11785 case EXEC_OMP_TARGET_SIMD: 11786 gfc_resolve_omp_do_blocks (code, ns); 11787 break; 11788 case EXEC_SELECT_TYPE: 11789 case EXEC_SELECT_RANK: 11790 /* Blocks are handled in resolve_select_type/rank because we 11791 have to transform the SELECT TYPE into ASSOCIATE first. */ 11792 break; 11793 case EXEC_DO_CONCURRENT: 11794 gfc_do_concurrent_flag = 1; 11795 gfc_resolve_blocks (code->block, ns); 11796 gfc_do_concurrent_flag = 2; 11797 break; 11798 case EXEC_OMP_WORKSHARE: 11799 omp_workshare_save = omp_workshare_flag; 11800 omp_workshare_flag = 1; 11801 /* FALL THROUGH */ 11802 default: 11803 gfc_resolve_blocks (code->block, ns); 11804 break; 11805 } 11806 11807 if (omp_workshare_save != -1) 11808 omp_workshare_flag = omp_workshare_save; 11809 } 11810 start: 11811 t = true; 11812 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC) 11813 t = gfc_resolve_expr (code->expr1); 11814 forall_flag = forall_save; 11815 gfc_do_concurrent_flag = do_concurrent_save; 11816 11817 if (!gfc_resolve_expr (code->expr2)) 11818 t = false; 11819 11820 if (code->op == EXEC_ALLOCATE 11821 && !gfc_resolve_expr (code->expr3)) 11822 t = false; 11823 11824 switch (code->op) 11825 { 11826 case EXEC_NOP: 11827 case EXEC_END_BLOCK: 11828 case EXEC_END_NESTED_BLOCK: 11829 case EXEC_CYCLE: 11830 case EXEC_PAUSE: 11831 case EXEC_STOP: 11832 case EXEC_ERROR_STOP: 11833 case EXEC_EXIT: 11834 case EXEC_CONTINUE: 11835 case EXEC_DT_END: 11836 case EXEC_ASSIGN_CALL: 11837 break; 11838 11839 case EXEC_CRITICAL: 11840 resolve_critical (code); 11841 break; 11842 11843 case EXEC_SYNC_ALL: 11844 case EXEC_SYNC_IMAGES: 11845 case EXEC_SYNC_MEMORY: 11846 resolve_sync (code); 11847 break; 11848 11849 case EXEC_LOCK: 11850 case EXEC_UNLOCK: 11851 case EXEC_EVENT_POST: 11852 case EXEC_EVENT_WAIT: 11853 resolve_lock_unlock_event (code); 11854 break; 11855 11856 case EXEC_FAIL_IMAGE: 11857 case EXEC_FORM_TEAM: 11858 case EXEC_CHANGE_TEAM: 11859 case EXEC_END_TEAM: 11860 case EXEC_SYNC_TEAM: 11861 break; 11862 11863 case EXEC_ENTRY: 11864 /* Keep track of which entry we are up to. */ 11865 current_entry_id = code->ext.entry->id; 11866 break; 11867 11868 case EXEC_WHERE: 11869 resolve_where (code, NULL); 11870 break; 11871 11872 case EXEC_GOTO: 11873 if (code->expr1 != NULL) 11874 { 11875 if (code->expr1->expr_type != EXPR_VARIABLE 11876 || code->expr1->ts.type != BT_INTEGER 11877 || (code->expr1->ref 11878 && code->expr1->ref->type == REF_ARRAY) 11879 || code->expr1->symtree == NULL 11880 || (code->expr1->symtree->n.sym 11881 && (code->expr1->symtree->n.sym->attr.flavor 11882 == FL_PARAMETER))) 11883 gfc_error ("ASSIGNED GOTO statement at %L requires a " 11884 "scalar INTEGER variable", &code->expr1->where); 11885 else if (code->expr1->symtree->n.sym 11886 && code->expr1->symtree->n.sym->attr.assign != 1) 11887 gfc_error ("Variable %qs has not been assigned a target " 11888 "label at %L", code->expr1->symtree->n.sym->name, 11889 &code->expr1->where); 11890 } 11891 else 11892 resolve_branch (code->label1, code); 11893 break; 11894 11895 case EXEC_RETURN: 11896 if (code->expr1 != NULL 11897 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank)) 11898 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-" 11899 "INTEGER return specifier", &code->expr1->where); 11900 break; 11901 11902 case EXEC_INIT_ASSIGN: 11903 case EXEC_END_PROCEDURE: 11904 break; 11905 11906 case EXEC_ASSIGN: 11907 if (!t) 11908 break; 11909 11910 if (code->expr1->ts.type == BT_CLASS) 11911 gfc_find_vtab (&code->expr2->ts); 11912 11913 /* Remove a GFC_ISYM_CAF_GET inserted for a coindexed variable on 11914 the LHS. */ 11915 if (code->expr1->expr_type == EXPR_FUNCTION 11916 && code->expr1->value.function.isym 11917 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET) 11918 remove_caf_get_intrinsic (code->expr1); 11919 11920 /* If this is a pointer function in an lvalue variable context, 11921 the new code will have to be resolved afresh. This is also the 11922 case with an error, where the code is transformed into NOP to 11923 prevent ICEs downstream. */ 11924 if (resolve_ptr_fcn_assign (&code, ns) 11925 || code->op == EXEC_NOP) 11926 goto start; 11927 11928 if (!gfc_check_vardef_context (code->expr1, false, false, false, 11929 _("assignment"))) 11930 break; 11931 11932 if (resolve_ordinary_assign (code, ns)) 11933 { 11934 if (code->op == EXEC_COMPCALL) 11935 goto compcall; 11936 else 11937 goto call; 11938 } 11939 11940 /* Check for dependencies in deferred character length array 11941 assignments and generate a temporary, if necessary. */ 11942 if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns)) 11943 break; 11944 11945 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */ 11946 if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED 11947 && code->expr1->ts.u.derived 11948 && code->expr1->ts.u.derived->attr.defined_assign_comp) 11949 generate_component_assignments (&code, ns); 11950 11951 break; 11952 11953 case EXEC_LABEL_ASSIGN: 11954 if (code->label1->defined == ST_LABEL_UNKNOWN) 11955 gfc_error ("Label %d referenced at %L is never defined", 11956 code->label1->value, &code->label1->where); 11957 if (t 11958 && (code->expr1->expr_type != EXPR_VARIABLE 11959 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER 11960 || code->expr1->symtree->n.sym->ts.kind 11961 != gfc_default_integer_kind 11962 || code->expr1->symtree->n.sym->as != NULL)) 11963 gfc_error ("ASSIGN statement at %L requires a scalar " 11964 "default INTEGER variable", &code->expr1->where); 11965 break; 11966 11967 case EXEC_POINTER_ASSIGN: 11968 { 11969 gfc_expr* e; 11970 11971 if (!t) 11972 break; 11973 11974 /* This is both a variable definition and pointer assignment 11975 context, so check both of them. For rank remapping, a final 11976 array ref may be present on the LHS and fool gfc_expr_attr 11977 used in gfc_check_vardef_context. Remove it. */ 11978 e = remove_last_array_ref (code->expr1); 11979 t = gfc_check_vardef_context (e, true, false, false, 11980 _("pointer assignment")); 11981 if (t) 11982 t = gfc_check_vardef_context (e, false, false, false, 11983 _("pointer assignment")); 11984 gfc_free_expr (e); 11985 11986 t = gfc_check_pointer_assign (code->expr1, code->expr2, !t) && t; 11987 11988 if (!t) 11989 break; 11990 11991 /* Assigning a class object always is a regular assign. */ 11992 if (code->expr2->ts.type == BT_CLASS 11993 && code->expr1->ts.type == BT_CLASS 11994 && !CLASS_DATA (code->expr2)->attr.dimension 11995 && !(gfc_expr_attr (code->expr1).proc_pointer 11996 && code->expr2->expr_type == EXPR_VARIABLE 11997 && code->expr2->symtree->n.sym->attr.flavor 11998 == FL_PROCEDURE)) 11999 code->op = EXEC_ASSIGN; 12000 break; 12001 } 12002 12003 case EXEC_ARITHMETIC_IF: 12004 { 12005 gfc_expr *e = code->expr1; 12006 12007 gfc_resolve_expr (e); 12008 if (e->expr_type == EXPR_NULL) 12009 gfc_error ("Invalid NULL at %L", &e->where); 12010 12011 if (t && (e->rank > 0 12012 || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER))) 12013 gfc_error ("Arithmetic IF statement at %L requires a scalar " 12014 "REAL or INTEGER expression", &e->where); 12015 12016 resolve_branch (code->label1, code); 12017 resolve_branch (code->label2, code); 12018 resolve_branch (code->label3, code); 12019 } 12020 break; 12021 12022 case EXEC_IF: 12023 if (t && code->expr1 != NULL 12024 && (code->expr1->ts.type != BT_LOGICAL 12025 || code->expr1->rank != 0)) 12026 gfc_error ("IF clause at %L requires a scalar LOGICAL expression", 12027 &code->expr1->where); 12028 break; 12029 12030 case EXEC_CALL: 12031 call: 12032 resolve_call (code); 12033 break; 12034 12035 case EXEC_COMPCALL: 12036 compcall: 12037 resolve_typebound_subroutine (code); 12038 break; 12039 12040 case EXEC_CALL_PPC: 12041 resolve_ppc_call (code); 12042 break; 12043 12044 case EXEC_SELECT: 12045 /* Select is complicated. Also, a SELECT construct could be 12046 a transformed computed GOTO. */ 12047 resolve_select (code, false); 12048 break; 12049 12050 case EXEC_SELECT_TYPE: 12051 resolve_select_type (code, ns); 12052 break; 12053 12054 case EXEC_SELECT_RANK: 12055 resolve_select_rank (code, ns); 12056 break; 12057 12058 case EXEC_BLOCK: 12059 resolve_block_construct (code); 12060 break; 12061 12062 case EXEC_DO: 12063 if (code->ext.iterator != NULL) 12064 { 12065 gfc_iterator *iter = code->ext.iterator; 12066 if (gfc_resolve_iterator (iter, true, false)) 12067 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym, 12068 true); 12069 } 12070 break; 12071 12072 case EXEC_DO_WHILE: 12073 if (code->expr1 == NULL) 12074 gfc_internal_error ("gfc_resolve_code(): No expression on " 12075 "DO WHILE"); 12076 if (t 12077 && (code->expr1->rank != 0 12078 || code->expr1->ts.type != BT_LOGICAL)) 12079 gfc_error ("Exit condition of DO WHILE loop at %L must be " 12080 "a scalar LOGICAL expression", &code->expr1->where); 12081 break; 12082 12083 case EXEC_ALLOCATE: 12084 if (t) 12085 resolve_allocate_deallocate (code, "ALLOCATE"); 12086 12087 break; 12088 12089 case EXEC_DEALLOCATE: 12090 if (t) 12091 resolve_allocate_deallocate (code, "DEALLOCATE"); 12092 12093 break; 12094 12095 case EXEC_OPEN: 12096 if (!gfc_resolve_open (code->ext.open, &code->loc)) 12097 break; 12098 12099 resolve_branch (code->ext.open->err, code); 12100 break; 12101 12102 case EXEC_CLOSE: 12103 if (!gfc_resolve_close (code->ext.close, &code->loc)) 12104 break; 12105 12106 resolve_branch (code->ext.close->err, code); 12107 break; 12108 12109 case EXEC_BACKSPACE: 12110 case EXEC_ENDFILE: 12111 case EXEC_REWIND: 12112 case EXEC_FLUSH: 12113 if (!gfc_resolve_filepos (code->ext.filepos, &code->loc)) 12114 break; 12115 12116 resolve_branch (code->ext.filepos->err, code); 12117 break; 12118 12119 case EXEC_INQUIRE: 12120 if (!gfc_resolve_inquire (code->ext.inquire)) 12121 break; 12122 12123 resolve_branch (code->ext.inquire->err, code); 12124 break; 12125 12126 case EXEC_IOLENGTH: 12127 gcc_assert (code->ext.inquire != NULL); 12128 if (!gfc_resolve_inquire (code->ext.inquire)) 12129 break; 12130 12131 resolve_branch (code->ext.inquire->err, code); 12132 break; 12133 12134 case EXEC_WAIT: 12135 if (!gfc_resolve_wait (code->ext.wait)) 12136 break; 12137 12138 resolve_branch (code->ext.wait->err, code); 12139 resolve_branch (code->ext.wait->end, code); 12140 resolve_branch (code->ext.wait->eor, code); 12141 break; 12142 12143 case EXEC_READ: 12144 case EXEC_WRITE: 12145 if (!gfc_resolve_dt (code, code->ext.dt, &code->loc)) 12146 break; 12147 12148 resolve_branch (code->ext.dt->err, code); 12149 resolve_branch (code->ext.dt->end, code); 12150 resolve_branch (code->ext.dt->eor, code); 12151 break; 12152 12153 case EXEC_TRANSFER: 12154 resolve_transfer (code); 12155 break; 12156 12157 case EXEC_DO_CONCURRENT: 12158 case EXEC_FORALL: 12159 resolve_forall_iterators (code->ext.forall_iterator); 12160 12161 if (code->expr1 != NULL 12162 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank)) 12163 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL " 12164 "expression", &code->expr1->where); 12165 break; 12166 12167 case EXEC_OACC_PARALLEL_LOOP: 12168 case EXEC_OACC_PARALLEL: 12169 case EXEC_OACC_KERNELS_LOOP: 12170 case EXEC_OACC_KERNELS: 12171 case EXEC_OACC_SERIAL_LOOP: 12172 case EXEC_OACC_SERIAL: 12173 case EXEC_OACC_DATA: 12174 case EXEC_OACC_HOST_DATA: 12175 case EXEC_OACC_LOOP: 12176 case EXEC_OACC_UPDATE: 12177 case EXEC_OACC_WAIT: 12178 case EXEC_OACC_CACHE: 12179 case EXEC_OACC_ENTER_DATA: 12180 case EXEC_OACC_EXIT_DATA: 12181 case EXEC_OACC_ATOMIC: 12182 case EXEC_OACC_DECLARE: 12183 gfc_resolve_oacc_directive (code, ns); 12184 break; 12185 12186 case EXEC_OMP_ATOMIC: 12187 case EXEC_OMP_BARRIER: 12188 case EXEC_OMP_CANCEL: 12189 case EXEC_OMP_CANCELLATION_POINT: 12190 case EXEC_OMP_CRITICAL: 12191 case EXEC_OMP_FLUSH: 12192 case EXEC_OMP_DISTRIBUTE: 12193 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO: 12194 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD: 12195 case EXEC_OMP_DISTRIBUTE_SIMD: 12196 case EXEC_OMP_DO: 12197 case EXEC_OMP_DO_SIMD: 12198 case EXEC_OMP_MASTER: 12199 case EXEC_OMP_ORDERED: 12200 case EXEC_OMP_SECTIONS: 12201 case EXEC_OMP_SIMD: 12202 case EXEC_OMP_SINGLE: 12203 case EXEC_OMP_TARGET: 12204 case EXEC_OMP_TARGET_DATA: 12205 case EXEC_OMP_TARGET_ENTER_DATA: 12206 case EXEC_OMP_TARGET_EXIT_DATA: 12207 case EXEC_OMP_TARGET_PARALLEL: 12208 case EXEC_OMP_TARGET_PARALLEL_DO: 12209 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD: 12210 case EXEC_OMP_TARGET_SIMD: 12211 case EXEC_OMP_TARGET_TEAMS: 12212 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE: 12213 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO: 12214 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: 12215 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD: 12216 case EXEC_OMP_TARGET_UPDATE: 12217 case EXEC_OMP_TASK: 12218 case EXEC_OMP_TASKGROUP: 12219 case EXEC_OMP_TASKLOOP: 12220 case EXEC_OMP_TASKLOOP_SIMD: 12221 case EXEC_OMP_TASKWAIT: 12222 case EXEC_OMP_TASKYIELD: 12223 case EXEC_OMP_TEAMS: 12224 case EXEC_OMP_TEAMS_DISTRIBUTE: 12225 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO: 12226 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD: 12227 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD: 12228 case EXEC_OMP_WORKSHARE: 12229 gfc_resolve_omp_directive (code, ns); 12230 break; 12231 12232 case EXEC_OMP_PARALLEL: 12233 case EXEC_OMP_PARALLEL_DO: 12234 case EXEC_OMP_PARALLEL_DO_SIMD: 12235 case EXEC_OMP_PARALLEL_SECTIONS: 12236 case EXEC_OMP_PARALLEL_WORKSHARE: 12237 omp_workshare_save = omp_workshare_flag; 12238 omp_workshare_flag = 0; 12239 gfc_resolve_omp_directive (code, ns); 12240 omp_workshare_flag = omp_workshare_save; 12241 break; 12242 12243 default: 12244 gfc_internal_error ("gfc_resolve_code(): Bad statement code"); 12245 } 12246 } 12247 12248 cs_base = frame.prev; 12249 } 12250 12251 12252 /* Resolve initial values and make sure they are compatible with 12253 the variable. */ 12254 12255 static void 12256 resolve_values (gfc_symbol *sym) 12257 { 12258 bool t; 12259 12260 if (sym->value == NULL) 12261 return; 12262 12263 if (sym->value->expr_type == EXPR_STRUCTURE) 12264 t= resolve_structure_cons (sym->value, 1); 12265 else 12266 t = gfc_resolve_expr (sym->value); 12267 12268 if (!t) 12269 return; 12270 12271 gfc_check_assign_symbol (sym, NULL, sym->value); 12272 } 12273 12274 12275 /* Verify any BIND(C) derived types in the namespace so we can report errors 12276 for them once, rather than for each variable declared of that type. */ 12277 12278 static void 12279 resolve_bind_c_derived_types (gfc_symbol *derived_sym) 12280 { 12281 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED 12282 && derived_sym->attr.is_bind_c == 1) 12283 verify_bind_c_derived_type (derived_sym); 12284 12285 return; 12286 } 12287 12288 12289 /* Check the interfaces of DTIO procedures associated with derived 12290 type 'sym'. These procedures can either have typebound bindings or 12291 can appear in DTIO generic interfaces. */ 12292 12293 static void 12294 gfc_verify_DTIO_procedures (gfc_symbol *sym) 12295 { 12296 if (!sym || sym->attr.flavor != FL_DERIVED) 12297 return; 12298 12299 gfc_check_dtio_interfaces (sym); 12300 12301 return; 12302 } 12303 12304 /* Verify that any binding labels used in a given namespace do not collide 12305 with the names or binding labels of any global symbols. Multiple INTERFACE 12306 for the same procedure are permitted. */ 12307 12308 static void 12309 gfc_verify_binding_labels (gfc_symbol *sym) 12310 { 12311 gfc_gsymbol *gsym; 12312 const char *module; 12313 12314 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c 12315 || sym->attr.flavor == FL_DERIVED || !sym->binding_label) 12316 return; 12317 12318 gsym = gfc_find_case_gsymbol (gfc_gsym_root, sym->binding_label); 12319 12320 if (sym->module) 12321 module = sym->module; 12322 else if (sym->ns && sym->ns->proc_name 12323 && sym->ns->proc_name->attr.flavor == FL_MODULE) 12324 module = sym->ns->proc_name->name; 12325 else if (sym->ns && sym->ns->parent 12326 && sym->ns && sym->ns->parent->proc_name 12327 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE) 12328 module = sym->ns->parent->proc_name->name; 12329 else 12330 module = NULL; 12331 12332 if (!gsym 12333 || (!gsym->defined 12334 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE))) 12335 { 12336 if (!gsym) 12337 gsym = gfc_get_gsymbol (sym->binding_label, true); 12338 gsym->where = sym->declared_at; 12339 gsym->sym_name = sym->name; 12340 gsym->binding_label = sym->binding_label; 12341 gsym->ns = sym->ns; 12342 gsym->mod_name = module; 12343 if (sym->attr.function) 12344 gsym->type = GSYM_FUNCTION; 12345 else if (sym->attr.subroutine) 12346 gsym->type = GSYM_SUBROUTINE; 12347 /* Mark as variable/procedure as defined, unless its an INTERFACE. */ 12348 gsym->defined = sym->attr.if_source != IFSRC_IFBODY; 12349 return; 12350 } 12351 12352 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN) 12353 { 12354 gfc_error ("Variable %qs with binding label %qs at %L uses the same global " 12355 "identifier as entity at %L", sym->name, 12356 sym->binding_label, &sym->declared_at, &gsym->where); 12357 /* Clear the binding label to prevent checking multiple times. */ 12358 sym->binding_label = NULL; 12359 return; 12360 } 12361 12362 if (sym->attr.flavor == FL_VARIABLE && module 12363 && (strcmp (module, gsym->mod_name) != 0 12364 || strcmp (sym->name, gsym->sym_name) != 0)) 12365 { 12366 /* This can only happen if the variable is defined in a module - if it 12367 isn't the same module, reject it. */ 12368 gfc_error ("Variable %qs from module %qs with binding label %qs at %L " 12369 "uses the same global identifier as entity at %L from module %qs", 12370 sym->name, module, sym->binding_label, 12371 &sym->declared_at, &gsym->where, gsym->mod_name); 12372 sym->binding_label = NULL; 12373 return; 12374 } 12375 12376 if ((sym->attr.function || sym->attr.subroutine) 12377 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION) 12378 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY)) 12379 && (sym != gsym->ns->proc_name && sym->attr.entry == 0) 12380 && (module != gsym->mod_name 12381 || strcmp (gsym->sym_name, sym->name) != 0 12382 || (module && strcmp (module, gsym->mod_name) != 0))) 12383 { 12384 /* Print an error if the procedure is defined multiple times; we have to 12385 exclude references to the same procedure via module association or 12386 multiple checks for the same procedure. */ 12387 gfc_error ("Procedure %qs with binding label %qs at %L uses the same " 12388 "global identifier as entity at %L", sym->name, 12389 sym->binding_label, &sym->declared_at, &gsym->where); 12390 sym->binding_label = NULL; 12391 } 12392 } 12393 12394 12395 /* Resolve an index expression. */ 12396 12397 static bool 12398 resolve_index_expr (gfc_expr *e) 12399 { 12400 if (!gfc_resolve_expr (e)) 12401 return false; 12402 12403 if (!gfc_simplify_expr (e, 0)) 12404 return false; 12405 12406 if (!gfc_specification_expr (e)) 12407 return false; 12408 12409 return true; 12410 } 12411 12412 12413 /* Resolve a charlen structure. */ 12414 12415 static bool 12416 resolve_charlen (gfc_charlen *cl) 12417 { 12418 int k; 12419 bool saved_specification_expr; 12420 12421 if (cl->resolved) 12422 return true; 12423 12424 cl->resolved = 1; 12425 saved_specification_expr = specification_expr; 12426 specification_expr = true; 12427 12428 if (cl->length_from_typespec) 12429 { 12430 if (!gfc_resolve_expr (cl->length)) 12431 { 12432 specification_expr = saved_specification_expr; 12433 return false; 12434 } 12435 12436 if (!gfc_simplify_expr (cl->length, 0)) 12437 { 12438 specification_expr = saved_specification_expr; 12439 return false; 12440 } 12441 12442 /* cl->length has been resolved. It should have an integer type. */ 12443 if (cl->length 12444 && (cl->length->ts.type != BT_INTEGER || cl->length->rank != 0)) 12445 { 12446 gfc_error ("Scalar INTEGER expression expected at %L", 12447 &cl->length->where); 12448 return false; 12449 } 12450 } 12451 else 12452 { 12453 if (!resolve_index_expr (cl->length)) 12454 { 12455 specification_expr = saved_specification_expr; 12456 return false; 12457 } 12458 } 12459 12460 /* F2008, 4.4.3.2: If the character length parameter value evaluates to 12461 a negative value, the length of character entities declared is zero. */ 12462 if (cl->length && cl->length->expr_type == EXPR_CONSTANT 12463 && mpz_sgn (cl->length->value.integer) < 0) 12464 gfc_replace_expr (cl->length, 12465 gfc_get_int_expr (gfc_charlen_int_kind, NULL, 0)); 12466 12467 /* Check that the character length is not too large. */ 12468 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false); 12469 if (cl->length && cl->length->expr_type == EXPR_CONSTANT 12470 && cl->length->ts.type == BT_INTEGER 12471 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0) 12472 { 12473 gfc_error ("String length at %L is too large", &cl->length->where); 12474 specification_expr = saved_specification_expr; 12475 return false; 12476 } 12477 12478 specification_expr = saved_specification_expr; 12479 return true; 12480 } 12481 12482 12483 /* Test for non-constant shape arrays. */ 12484 12485 static bool 12486 is_non_constant_shape_array (gfc_symbol *sym) 12487 { 12488 gfc_expr *e; 12489 int i; 12490 bool not_constant; 12491 12492 not_constant = false; 12493 if (sym->as != NULL) 12494 { 12495 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that 12496 has not been simplified; parameter array references. Do the 12497 simplification now. */ 12498 for (i = 0; i < sym->as->rank + sym->as->corank; i++) 12499 { 12500 if (i == GFC_MAX_DIMENSIONS) 12501 break; 12502 12503 e = sym->as->lower[i]; 12504 if (e && (!resolve_index_expr(e) 12505 || !gfc_is_constant_expr (e))) 12506 not_constant = true; 12507 e = sym->as->upper[i]; 12508 if (e && (!resolve_index_expr(e) 12509 || !gfc_is_constant_expr (e))) 12510 not_constant = true; 12511 } 12512 } 12513 return not_constant; 12514 } 12515 12516 /* Given a symbol and an initialization expression, add code to initialize 12517 the symbol to the function entry. */ 12518 static void 12519 build_init_assign (gfc_symbol *sym, gfc_expr *init) 12520 { 12521 gfc_expr *lval; 12522 gfc_code *init_st; 12523 gfc_namespace *ns = sym->ns; 12524 12525 /* Search for the function namespace if this is a contained 12526 function without an explicit result. */ 12527 if (sym->attr.function && sym == sym->result 12528 && sym->name != sym->ns->proc_name->name) 12529 { 12530 ns = ns->contained; 12531 for (;ns; ns = ns->sibling) 12532 if (strcmp (ns->proc_name->name, sym->name) == 0) 12533 break; 12534 } 12535 12536 if (ns == NULL) 12537 { 12538 gfc_free_expr (init); 12539 return; 12540 } 12541 12542 /* Build an l-value expression for the result. */ 12543 lval = gfc_lval_expr_from_sym (sym); 12544 12545 /* Add the code at scope entry. */ 12546 init_st = gfc_get_code (EXEC_INIT_ASSIGN); 12547 init_st->next = ns->code; 12548 ns->code = init_st; 12549 12550 /* Assign the default initializer to the l-value. */ 12551 init_st->loc = sym->declared_at; 12552 init_st->expr1 = lval; 12553 init_st->expr2 = init; 12554 } 12555 12556 12557 /* Whether or not we can generate a default initializer for a symbol. */ 12558 12559 static bool 12560 can_generate_init (gfc_symbol *sym) 12561 { 12562 symbol_attribute *a; 12563 if (!sym) 12564 return false; 12565 a = &sym->attr; 12566 12567 /* These symbols should never have a default initialization. */ 12568 return !( 12569 a->allocatable 12570 || a->external 12571 || a->pointer 12572 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym) 12573 && (CLASS_DATA (sym)->attr.class_pointer 12574 || CLASS_DATA (sym)->attr.proc_pointer)) 12575 || a->in_equivalence 12576 || a->in_common 12577 || a->data 12578 || sym->module 12579 || a->cray_pointee 12580 || a->cray_pointer 12581 || sym->assoc 12582 || (!a->referenced && !a->result) 12583 || (a->dummy && a->intent != INTENT_OUT) 12584 || (a->function && sym != sym->result) 12585 ); 12586 } 12587 12588 12589 /* Assign the default initializer to a derived type variable or result. */ 12590 12591 static void 12592 apply_default_init (gfc_symbol *sym) 12593 { 12594 gfc_expr *init = NULL; 12595 12596 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function) 12597 return; 12598 12599 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived) 12600 init = gfc_generate_initializer (&sym->ts, can_generate_init (sym)); 12601 12602 if (init == NULL && sym->ts.type != BT_CLASS) 12603 return; 12604 12605 build_init_assign (sym, init); 12606 sym->attr.referenced = 1; 12607 } 12608 12609 12610 /* Build an initializer for a local. Returns null if the symbol should not have 12611 a default initialization. */ 12612 12613 static gfc_expr * 12614 build_default_init_expr (gfc_symbol *sym) 12615 { 12616 /* These symbols should never have a default initialization. */ 12617 if (sym->attr.allocatable 12618 || sym->attr.external 12619 || sym->attr.dummy 12620 || sym->attr.pointer 12621 || sym->attr.in_equivalence 12622 || sym->attr.in_common 12623 || sym->attr.data 12624 || sym->module 12625 || sym->attr.cray_pointee 12626 || sym->attr.cray_pointer 12627 || sym->assoc) 12628 return NULL; 12629 12630 /* Get the appropriate init expression. */ 12631 return gfc_build_default_init_expr (&sym->ts, &sym->declared_at); 12632 } 12633 12634 /* Add an initialization expression to a local variable. */ 12635 static void 12636 apply_default_init_local (gfc_symbol *sym) 12637 { 12638 gfc_expr *init = NULL; 12639 12640 /* The symbol should be a variable or a function return value. */ 12641 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function) 12642 || (sym->attr.function && sym->result != sym)) 12643 return; 12644 12645 /* Try to build the initializer expression. If we can't initialize 12646 this symbol, then init will be NULL. */ 12647 init = build_default_init_expr (sym); 12648 if (init == NULL) 12649 return; 12650 12651 /* For saved variables, we don't want to add an initializer at function 12652 entry, so we just add a static initializer. Note that automatic variables 12653 are stack allocated even with -fno-automatic; we have also to exclude 12654 result variable, which are also nonstatic. */ 12655 if (!sym->attr.automatic 12656 && (sym->attr.save || sym->ns->save_all 12657 || (flag_max_stack_var_size == 0 && !sym->attr.result 12658 && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive) 12659 && (!sym->attr.dimension || !is_non_constant_shape_array (sym))))) 12660 { 12661 /* Don't clobber an existing initializer! */ 12662 gcc_assert (sym->value == NULL); 12663 sym->value = init; 12664 return; 12665 } 12666 12667 build_init_assign (sym, init); 12668 } 12669 12670 12671 /* Resolution of common features of flavors variable and procedure. */ 12672 12673 static bool 12674 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag) 12675 { 12676 gfc_array_spec *as; 12677 12678 if (sym->ts.type == BT_CLASS && sym->attr.class_ok 12679 && sym->ts.u.derived && CLASS_DATA (sym)) 12680 as = CLASS_DATA (sym)->as; 12681 else 12682 as = sym->as; 12683 12684 /* Constraints on deferred shape variable. */ 12685 if (as == NULL || as->type != AS_DEFERRED) 12686 { 12687 bool pointer, allocatable, dimension; 12688 12689 if (sym->ts.type == BT_CLASS && sym->attr.class_ok 12690 && sym->ts.u.derived && CLASS_DATA (sym)) 12691 { 12692 pointer = CLASS_DATA (sym)->attr.class_pointer; 12693 allocatable = CLASS_DATA (sym)->attr.allocatable; 12694 dimension = CLASS_DATA (sym)->attr.dimension; 12695 } 12696 else 12697 { 12698 pointer = sym->attr.pointer && !sym->attr.select_type_temporary; 12699 allocatable = sym->attr.allocatable; 12700 dimension = sym->attr.dimension; 12701 } 12702 12703 if (allocatable) 12704 { 12705 if (dimension && as->type != AS_ASSUMED_RANK) 12706 { 12707 gfc_error ("Allocatable array %qs at %L must have a deferred " 12708 "shape or assumed rank", sym->name, &sym->declared_at); 12709 return false; 12710 } 12711 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object " 12712 "%qs at %L may not be ALLOCATABLE", 12713 sym->name, &sym->declared_at)) 12714 return false; 12715 } 12716 12717 if (pointer && dimension && as->type != AS_ASSUMED_RANK) 12718 { 12719 gfc_error ("Array pointer %qs at %L must have a deferred shape or " 12720 "assumed rank", sym->name, &sym->declared_at); 12721 sym->error = 1; 12722 return false; 12723 } 12724 } 12725 else 12726 { 12727 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer 12728 && sym->ts.type != BT_CLASS && !sym->assoc) 12729 { 12730 gfc_error ("Array %qs at %L cannot have a deferred shape", 12731 sym->name, &sym->declared_at); 12732 return false; 12733 } 12734 } 12735 12736 /* Constraints on polymorphic variables. */ 12737 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym)) 12738 { 12739 /* F03:C502. */ 12740 if (sym->attr.class_ok 12741 && sym->ts.u.derived 12742 && !sym->attr.select_type_temporary 12743 && !UNLIMITED_POLY (sym) 12744 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived)) 12745 { 12746 gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible", 12747 CLASS_DATA (sym)->ts.u.derived->name, sym->name, 12748 &sym->declared_at); 12749 return false; 12750 } 12751 12752 /* F03:C509. */ 12753 /* Assume that use associated symbols were checked in the module ns. 12754 Class-variables that are associate-names are also something special 12755 and excepted from the test. */ 12756 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc) 12757 { 12758 gfc_error ("CLASS variable %qs at %L must be dummy, allocatable " 12759 "or pointer", sym->name, &sym->declared_at); 12760 return false; 12761 } 12762 } 12763 12764 return true; 12765 } 12766 12767 12768 /* Additional checks for symbols with flavor variable and derived 12769 type. To be called from resolve_fl_variable. */ 12770 12771 static bool 12772 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag) 12773 { 12774 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS); 12775 12776 /* Check to see if a derived type is blocked from being host 12777 associated by the presence of another class I symbol in the same 12778 namespace. 14.6.1.3 of the standard and the discussion on 12779 comp.lang.fortran. */ 12780 if (sym->ts.u.derived 12781 && sym->ns != sym->ts.u.derived->ns 12782 && !sym->ts.u.derived->attr.use_assoc 12783 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY) 12784 { 12785 gfc_symbol *s; 12786 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s); 12787 if (s && s->attr.generic) 12788 s = gfc_find_dt_in_generic (s); 12789 if (s && !gfc_fl_struct (s->attr.flavor)) 12790 { 12791 gfc_error ("The type %qs cannot be host associated at %L " 12792 "because it is blocked by an incompatible object " 12793 "of the same name declared at %L", 12794 sym->ts.u.derived->name, &sym->declared_at, 12795 &s->declared_at); 12796 return false; 12797 } 12798 } 12799 12800 /* 4th constraint in section 11.3: "If an object of a type for which 12801 component-initialization is specified (R429) appears in the 12802 specification-part of a module and does not have the ALLOCATABLE 12803 or POINTER attribute, the object shall have the SAVE attribute." 12804 12805 The check for initializers is performed with 12806 gfc_has_default_initializer because gfc_default_initializer generates 12807 a hidden default for allocatable components. */ 12808 if (!(sym->value || no_init_flag) && sym->ns->proc_name 12809 && sym->ns->proc_name->attr.flavor == FL_MODULE 12810 && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save 12811 && !sym->attr.pointer && !sym->attr.allocatable 12812 && gfc_has_default_initializer (sym->ts.u.derived) 12813 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable " 12814 "%qs at %L, needed due to the default " 12815 "initialization", sym->name, &sym->declared_at)) 12816 return false; 12817 12818 /* Assign default initializer. */ 12819 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable) 12820 && (!no_init_flag || sym->attr.intent == INTENT_OUT)) 12821 sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym)); 12822 12823 return true; 12824 } 12825 12826 12827 /* F2008, C402 (R401): A colon shall not be used as a type-param-value 12828 except in the declaration of an entity or component that has the POINTER 12829 or ALLOCATABLE attribute. */ 12830 12831 static bool 12832 deferred_requirements (gfc_symbol *sym) 12833 { 12834 if (sym->ts.deferred 12835 && !(sym->attr.pointer 12836 || sym->attr.allocatable 12837 || sym->attr.associate_var 12838 || sym->attr.omp_udr_artificial_var)) 12839 { 12840 /* If a function has a result variable, only check the variable. */ 12841 if (sym->result && sym->name != sym->result->name) 12842 return true; 12843 12844 gfc_error ("Entity %qs at %L has a deferred type parameter and " 12845 "requires either the POINTER or ALLOCATABLE attribute", 12846 sym->name, &sym->declared_at); 12847 return false; 12848 } 12849 return true; 12850 } 12851 12852 12853 /* Resolve symbols with flavor variable. */ 12854 12855 static bool 12856 resolve_fl_variable (gfc_symbol *sym, int mp_flag) 12857 { 12858 const char *auto_save_msg = "Automatic object %qs at %L cannot have the " 12859 "SAVE attribute"; 12860 12861 if (!resolve_fl_var_and_proc (sym, mp_flag)) 12862 return false; 12863 12864 /* Set this flag to check that variables are parameters of all entries. 12865 This check is effected by the call to gfc_resolve_expr through 12866 is_non_constant_shape_array. */ 12867 bool saved_specification_expr = specification_expr; 12868 specification_expr = true; 12869 12870 if (sym->ns->proc_name 12871 && (sym->ns->proc_name->attr.flavor == FL_MODULE 12872 || sym->ns->proc_name->attr.is_main_program) 12873 && !sym->attr.use_assoc 12874 && !sym->attr.allocatable 12875 && !sym->attr.pointer 12876 && is_non_constant_shape_array (sym)) 12877 { 12878 /* F08:C541. The shape of an array defined in a main program or module 12879 * needs to be constant. */ 12880 gfc_error ("The module or main program array %qs at %L must " 12881 "have constant shape", sym->name, &sym->declared_at); 12882 specification_expr = saved_specification_expr; 12883 return false; 12884 } 12885 12886 /* Constraints on deferred type parameter. */ 12887 if (!deferred_requirements (sym)) 12888 return false; 12889 12890 if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var) 12891 { 12892 /* Make sure that character string variables with assumed length are 12893 dummy arguments. */ 12894 gfc_expr *e = NULL; 12895 12896 if (sym->ts.u.cl) 12897 e = sym->ts.u.cl->length; 12898 else 12899 return false; 12900 12901 if (e == NULL && !sym->attr.dummy && !sym->attr.result 12902 && !sym->ts.deferred && !sym->attr.select_type_temporary 12903 && !sym->attr.omp_udr_artificial_var) 12904 { 12905 gfc_error ("Entity with assumed character length at %L must be a " 12906 "dummy argument or a PARAMETER", &sym->declared_at); 12907 specification_expr = saved_specification_expr; 12908 return false; 12909 } 12910 12911 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e)) 12912 { 12913 gfc_error (auto_save_msg, sym->name, &sym->declared_at); 12914 specification_expr = saved_specification_expr; 12915 return false; 12916 } 12917 12918 if (!gfc_is_constant_expr (e) 12919 && !(e->expr_type == EXPR_VARIABLE 12920 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)) 12921 { 12922 if (!sym->attr.use_assoc && sym->ns->proc_name 12923 && (sym->ns->proc_name->attr.flavor == FL_MODULE 12924 || sym->ns->proc_name->attr.is_main_program)) 12925 { 12926 gfc_error ("%qs at %L must have constant character length " 12927 "in this context", sym->name, &sym->declared_at); 12928 specification_expr = saved_specification_expr; 12929 return false; 12930 } 12931 if (sym->attr.in_common) 12932 { 12933 gfc_error ("COMMON variable %qs at %L must have constant " 12934 "character length", sym->name, &sym->declared_at); 12935 specification_expr = saved_specification_expr; 12936 return false; 12937 } 12938 } 12939 } 12940 12941 if (sym->value == NULL && sym->attr.referenced) 12942 apply_default_init_local (sym); /* Try to apply a default initialization. */ 12943 12944 /* Determine if the symbol may not have an initializer. */ 12945 int no_init_flag = 0, automatic_flag = 0; 12946 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy 12947 || sym->attr.intrinsic || sym->attr.result) 12948 no_init_flag = 1; 12949 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer 12950 && is_non_constant_shape_array (sym)) 12951 { 12952 no_init_flag = automatic_flag = 1; 12953 12954 /* Also, they must not have the SAVE attribute. 12955 SAVE_IMPLICIT is checked below. */ 12956 if (sym->as && sym->attr.codimension) 12957 { 12958 int corank = sym->as->corank; 12959 sym->as->corank = 0; 12960 no_init_flag = automatic_flag = is_non_constant_shape_array (sym); 12961 sym->as->corank = corank; 12962 } 12963 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT) 12964 { 12965 gfc_error (auto_save_msg, sym->name, &sym->declared_at); 12966 specification_expr = saved_specification_expr; 12967 return false; 12968 } 12969 } 12970 12971 /* Ensure that any initializer is simplified. */ 12972 if (sym->value) 12973 gfc_simplify_expr (sym->value, 1); 12974 12975 /* Reject illegal initializers. */ 12976 if (!sym->mark && sym->value) 12977 { 12978 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS 12979 && CLASS_DATA (sym)->attr.allocatable)) 12980 gfc_error ("Allocatable %qs at %L cannot have an initializer", 12981 sym->name, &sym->declared_at); 12982 else if (sym->attr.external) 12983 gfc_error ("External %qs at %L cannot have an initializer", 12984 sym->name, &sym->declared_at); 12985 else if (sym->attr.dummy 12986 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT)) 12987 gfc_error ("Dummy %qs at %L cannot have an initializer", 12988 sym->name, &sym->declared_at); 12989 else if (sym->attr.intrinsic) 12990 gfc_error ("Intrinsic %qs at %L cannot have an initializer", 12991 sym->name, &sym->declared_at); 12992 else if (sym->attr.result) 12993 gfc_error ("Function result %qs at %L cannot have an initializer", 12994 sym->name, &sym->declared_at); 12995 else if (automatic_flag) 12996 gfc_error ("Automatic array %qs at %L cannot have an initializer", 12997 sym->name, &sym->declared_at); 12998 else 12999 goto no_init_error; 13000 specification_expr = saved_specification_expr; 13001 return false; 13002 } 13003 13004 no_init_error: 13005 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS) 13006 { 13007 bool res = resolve_fl_variable_derived (sym, no_init_flag); 13008 specification_expr = saved_specification_expr; 13009 return res; 13010 } 13011 13012 specification_expr = saved_specification_expr; 13013 return true; 13014 } 13015 13016 13017 /* Compare the dummy characteristics of a module procedure interface 13018 declaration with the corresponding declaration in a submodule. */ 13019 static gfc_formal_arglist *new_formal; 13020 static char errmsg[200]; 13021 13022 static void 13023 compare_fsyms (gfc_symbol *sym) 13024 { 13025 gfc_symbol *fsym; 13026 13027 if (sym == NULL || new_formal == NULL) 13028 return; 13029 13030 fsym = new_formal->sym; 13031 13032 if (sym == fsym) 13033 return; 13034 13035 if (strcmp (sym->name, fsym->name) == 0) 13036 { 13037 if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200)) 13038 gfc_error ("%s at %L", errmsg, &fsym->declared_at); 13039 } 13040 } 13041 13042 13043 /* Resolve a procedure. */ 13044 13045 static bool 13046 resolve_fl_procedure (gfc_symbol *sym, int mp_flag) 13047 { 13048 gfc_formal_arglist *arg; 13049 bool allocatable_or_pointer; 13050 13051 if (sym->attr.function 13052 && !resolve_fl_var_and_proc (sym, mp_flag)) 13053 return false; 13054 13055 /* Constraints on deferred type parameter. */ 13056 if (!deferred_requirements (sym)) 13057 return false; 13058 13059 if (sym->ts.type == BT_CHARACTER) 13060 { 13061 gfc_charlen *cl = sym->ts.u.cl; 13062 13063 if (cl && cl->length && gfc_is_constant_expr (cl->length) 13064 && !resolve_charlen (cl)) 13065 return false; 13066 13067 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT) 13068 && sym->attr.proc == PROC_ST_FUNCTION) 13069 { 13070 gfc_error ("Character-valued statement function %qs at %L must " 13071 "have constant length", sym->name, &sym->declared_at); 13072 return false; 13073 } 13074 } 13075 13076 /* Ensure that derived type for are not of a private type. Internal 13077 module procedures are excluded by 2.2.3.3 - i.e., they are not 13078 externally accessible and can access all the objects accessible in 13079 the host. */ 13080 if (!(sym->ns->parent && sym->ns->parent->proc_name 13081 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE) 13082 && gfc_check_symbol_access (sym)) 13083 { 13084 gfc_interface *iface; 13085 13086 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next) 13087 { 13088 if (arg->sym 13089 && arg->sym->ts.type == BT_DERIVED 13090 && arg->sym->ts.u.derived 13091 && !arg->sym->ts.u.derived->attr.use_assoc 13092 && !gfc_check_symbol_access (arg->sym->ts.u.derived) 13093 && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type " 13094 "and cannot be a dummy argument" 13095 " of %qs, which is PUBLIC at %L", 13096 arg->sym->name, sym->name, 13097 &sym->declared_at)) 13098 { 13099 /* Stop this message from recurring. */ 13100 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC; 13101 return false; 13102 } 13103 } 13104 13105 /* PUBLIC interfaces may expose PRIVATE procedures that take types 13106 PRIVATE to the containing module. */ 13107 for (iface = sym->generic; iface; iface = iface->next) 13108 { 13109 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next) 13110 { 13111 if (arg->sym 13112 && arg->sym->ts.type == BT_DERIVED 13113 && !arg->sym->ts.u.derived->attr.use_assoc 13114 && !gfc_check_symbol_access (arg->sym->ts.u.derived) 13115 && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in " 13116 "PUBLIC interface %qs at %L " 13117 "takes dummy arguments of %qs which " 13118 "is PRIVATE", iface->sym->name, 13119 sym->name, &iface->sym->declared_at, 13120 gfc_typename(&arg->sym->ts))) 13121 { 13122 /* Stop this message from recurring. */ 13123 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC; 13124 return false; 13125 } 13126 } 13127 } 13128 } 13129 13130 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION 13131 && !sym->attr.proc_pointer) 13132 { 13133 gfc_error ("Function %qs at %L cannot have an initializer", 13134 sym->name, &sym->declared_at); 13135 13136 /* Make sure no second error is issued for this. */ 13137 sym->value->error = 1; 13138 return false; 13139 } 13140 13141 /* An external symbol may not have an initializer because it is taken to be 13142 a procedure. Exception: Procedure Pointers. */ 13143 if (sym->attr.external && sym->value && !sym->attr.proc_pointer) 13144 { 13145 gfc_error ("External object %qs at %L may not have an initializer", 13146 sym->name, &sym->declared_at); 13147 return false; 13148 } 13149 13150 /* An elemental function is required to return a scalar 12.7.1 */ 13151 if (sym->attr.elemental && sym->attr.function 13152 && (sym->as || (sym->ts.type == BT_CLASS && sym->attr.class_ok 13153 && CLASS_DATA (sym)->as))) 13154 { 13155 gfc_error ("ELEMENTAL function %qs at %L must have a scalar " 13156 "result", sym->name, &sym->declared_at); 13157 /* Reset so that the error only occurs once. */ 13158 sym->attr.elemental = 0; 13159 return false; 13160 } 13161 13162 if (sym->attr.proc == PROC_ST_FUNCTION 13163 && (sym->attr.allocatable || sym->attr.pointer)) 13164 { 13165 gfc_error ("Statement function %qs at %L may not have pointer or " 13166 "allocatable attribute", sym->name, &sym->declared_at); 13167 return false; 13168 } 13169 13170 /* 5.1.1.5 of the Standard: A function name declared with an asterisk 13171 char-len-param shall not be array-valued, pointer-valued, recursive 13172 or pure. ....snip... A character value of * may only be used in the 13173 following ways: (i) Dummy arg of procedure - dummy associates with 13174 actual length; (ii) To declare a named constant; or (iii) External 13175 function - but length must be declared in calling scoping unit. */ 13176 if (sym->attr.function 13177 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred 13178 && sym->ts.u.cl && sym->ts.u.cl->length == NULL) 13179 { 13180 if ((sym->as && sym->as->rank) || (sym->attr.pointer) 13181 || (sym->attr.recursive) || (sym->attr.pure)) 13182 { 13183 if (sym->as && sym->as->rank) 13184 gfc_error ("CHARACTER(*) function %qs at %L cannot be " 13185 "array-valued", sym->name, &sym->declared_at); 13186 13187 if (sym->attr.pointer) 13188 gfc_error ("CHARACTER(*) function %qs at %L cannot be " 13189 "pointer-valued", sym->name, &sym->declared_at); 13190 13191 if (sym->attr.pure) 13192 gfc_error ("CHARACTER(*) function %qs at %L cannot be " 13193 "pure", sym->name, &sym->declared_at); 13194 13195 if (sym->attr.recursive) 13196 gfc_error ("CHARACTER(*) function %qs at %L cannot be " 13197 "recursive", sym->name, &sym->declared_at); 13198 13199 return false; 13200 } 13201 13202 /* Appendix B.2 of the standard. Contained functions give an 13203 error anyway. Deferred character length is an F2003 feature. 13204 Don't warn on intrinsic conversion functions, which start 13205 with two underscores. */ 13206 if (!sym->attr.contained && !sym->ts.deferred 13207 && (sym->name[0] != '_' || sym->name[1] != '_')) 13208 gfc_notify_std (GFC_STD_F95_OBS, 13209 "CHARACTER(*) function %qs at %L", 13210 sym->name, &sym->declared_at); 13211 } 13212 13213 /* F2008, C1218. */ 13214 if (sym->attr.elemental) 13215 { 13216 if (sym->attr.proc_pointer) 13217 { 13218 const char* name = (sym->attr.result ? sym->ns->proc_name->name 13219 : sym->name); 13220 gfc_error ("Procedure pointer %qs at %L shall not be elemental", 13221 name, &sym->declared_at); 13222 return false; 13223 } 13224 if (sym->attr.dummy) 13225 { 13226 gfc_error ("Dummy procedure %qs at %L shall not be elemental", 13227 sym->name, &sym->declared_at); 13228 return false; 13229 } 13230 } 13231 13232 /* F2018, C15100: "The result of an elemental function shall be scalar, 13233 and shall not have the POINTER or ALLOCATABLE attribute." The scalar 13234 pointer is tested and caught elsewhere. */ 13235 if (sym->result) 13236 allocatable_or_pointer = sym->result->ts.type == BT_CLASS 13237 && CLASS_DATA (sym->result) ? 13238 (CLASS_DATA (sym->result)->attr.allocatable 13239 || CLASS_DATA (sym->result)->attr.pointer) : 13240 (sym->result->attr.allocatable 13241 || sym->result->attr.pointer); 13242 13243 if (sym->attr.elemental && sym->result 13244 && allocatable_or_pointer) 13245 { 13246 gfc_error ("Function result variable %qs at %L of elemental " 13247 "function %qs shall not have an ALLOCATABLE or POINTER " 13248 "attribute", sym->result->name, 13249 &sym->result->declared_at, sym->name); 13250 return false; 13251 } 13252 13253 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1) 13254 { 13255 gfc_formal_arglist *curr_arg; 13256 int has_non_interop_arg = 0; 13257 13258 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common, 13259 sym->common_block)) 13260 { 13261 /* Clear these to prevent looking at them again if there was an 13262 error. */ 13263 sym->attr.is_bind_c = 0; 13264 sym->attr.is_c_interop = 0; 13265 sym->ts.is_c_interop = 0; 13266 } 13267 else 13268 { 13269 /* So far, no errors have been found. */ 13270 sym->attr.is_c_interop = 1; 13271 sym->ts.is_c_interop = 1; 13272 } 13273 13274 curr_arg = gfc_sym_get_dummy_args (sym); 13275 while (curr_arg != NULL) 13276 { 13277 /* Skip implicitly typed dummy args here. */ 13278 if (curr_arg->sym && curr_arg->sym->attr.implicit_type == 0) 13279 if (!gfc_verify_c_interop_param (curr_arg->sym)) 13280 /* If something is found to fail, record the fact so we 13281 can mark the symbol for the procedure as not being 13282 BIND(C) to try and prevent multiple errors being 13283 reported. */ 13284 has_non_interop_arg = 1; 13285 13286 curr_arg = curr_arg->next; 13287 } 13288 13289 /* See if any of the arguments were not interoperable and if so, clear 13290 the procedure symbol to prevent duplicate error messages. */ 13291 if (has_non_interop_arg != 0) 13292 { 13293 sym->attr.is_c_interop = 0; 13294 sym->ts.is_c_interop = 0; 13295 sym->attr.is_bind_c = 0; 13296 } 13297 } 13298 13299 if (!sym->attr.proc_pointer) 13300 { 13301 if (sym->attr.save == SAVE_EXPLICIT) 13302 { 13303 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute " 13304 "in %qs at %L", sym->name, &sym->declared_at); 13305 return false; 13306 } 13307 if (sym->attr.intent) 13308 { 13309 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute " 13310 "in %qs at %L", sym->name, &sym->declared_at); 13311 return false; 13312 } 13313 if (sym->attr.subroutine && sym->attr.result) 13314 { 13315 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute " 13316 "in %qs at %L", sym->ns->proc_name->name, &sym->declared_at); 13317 return false; 13318 } 13319 if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure 13320 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure) 13321 || sym->attr.contained)) 13322 { 13323 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute " 13324 "in %qs at %L", sym->name, &sym->declared_at); 13325 return false; 13326 } 13327 if (strcmp ("ppr@", sym->name) == 0) 13328 { 13329 gfc_error ("Procedure pointer result %qs at %L " 13330 "is missing the pointer attribute", 13331 sym->ns->proc_name->name, &sym->declared_at); 13332 return false; 13333 } 13334 } 13335 13336 /* Assume that a procedure whose body is not known has references 13337 to external arrays. */ 13338 if (sym->attr.if_source != IFSRC_DECL) 13339 sym->attr.array_outer_dependency = 1; 13340 13341 /* Compare the characteristics of a module procedure with the 13342 interface declaration. Ideally this would be done with 13343 gfc_compare_interfaces but, at present, the formal interface 13344 cannot be copied to the ts.interface. */ 13345 if (sym->attr.module_procedure 13346 && sym->attr.if_source == IFSRC_DECL) 13347 { 13348 gfc_symbol *iface; 13349 char name[2*GFC_MAX_SYMBOL_LEN + 1]; 13350 char *module_name; 13351 char *submodule_name; 13352 strcpy (name, sym->ns->proc_name->name); 13353 module_name = strtok (name, "."); 13354 submodule_name = strtok (NULL, "."); 13355 13356 iface = sym->tlink; 13357 sym->tlink = NULL; 13358 13359 /* Make sure that the result uses the correct charlen for deferred 13360 length results. */ 13361 if (iface && sym->result 13362 && iface->ts.type == BT_CHARACTER 13363 && iface->ts.deferred) 13364 sym->result->ts.u.cl = iface->ts.u.cl; 13365 13366 if (iface == NULL) 13367 goto check_formal; 13368 13369 /* Check the procedure characteristics. */ 13370 if (sym->attr.elemental != iface->attr.elemental) 13371 { 13372 gfc_error ("Mismatch in ELEMENTAL attribute between MODULE " 13373 "PROCEDURE at %L and its interface in %s", 13374 &sym->declared_at, module_name); 13375 return false; 13376 } 13377 13378 if (sym->attr.pure != iface->attr.pure) 13379 { 13380 gfc_error ("Mismatch in PURE attribute between MODULE " 13381 "PROCEDURE at %L and its interface in %s", 13382 &sym->declared_at, module_name); 13383 return false; 13384 } 13385 13386 if (sym->attr.recursive != iface->attr.recursive) 13387 { 13388 gfc_error ("Mismatch in RECURSIVE attribute between MODULE " 13389 "PROCEDURE at %L and its interface in %s", 13390 &sym->declared_at, module_name); 13391 return false; 13392 } 13393 13394 /* Check the result characteristics. */ 13395 if (!gfc_check_result_characteristics (sym, iface, errmsg, 200)) 13396 { 13397 gfc_error ("%s between the MODULE PROCEDURE declaration " 13398 "in MODULE %qs and the declaration at %L in " 13399 "(SUB)MODULE %qs", 13400 errmsg, module_name, &sym->declared_at, 13401 submodule_name ? submodule_name : module_name); 13402 return false; 13403 } 13404 13405 check_formal: 13406 /* Check the characteristics of the formal arguments. */ 13407 if (sym->formal && sym->formal_ns) 13408 { 13409 for (arg = sym->formal; arg && arg->sym; arg = arg->next) 13410 { 13411 new_formal = arg; 13412 gfc_traverse_ns (sym->formal_ns, compare_fsyms); 13413 } 13414 } 13415 } 13416 return true; 13417 } 13418 13419 13420 /* Resolve a list of finalizer procedures. That is, after they have hopefully 13421 been defined and we now know their defined arguments, check that they fulfill 13422 the requirements of the standard for procedures used as finalizers. */ 13423 13424 static bool 13425 gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable) 13426 { 13427 gfc_finalizer* list; 13428 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */ 13429 bool result = true; 13430 bool seen_scalar = false; 13431 gfc_symbol *vtab; 13432 gfc_component *c; 13433 gfc_symbol *parent = gfc_get_derived_super_type (derived); 13434 13435 if (parent) 13436 gfc_resolve_finalizers (parent, finalizable); 13437 13438 /* Ensure that derived-type components have a their finalizers resolved. */ 13439 bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers; 13440 for (c = derived->components; c; c = c->next) 13441 if (c->ts.type == BT_DERIVED 13442 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable) 13443 { 13444 bool has_final2 = false; 13445 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2)) 13446 return false; /* Error. */ 13447 has_final = has_final || has_final2; 13448 } 13449 /* Return early if not finalizable. */ 13450 if (!has_final) 13451 { 13452 if (finalizable) 13453 *finalizable = false; 13454 return true; 13455 } 13456 13457 /* Walk over the list of finalizer-procedures, check them, and if any one 13458 does not fit in with the standard's definition, print an error and remove 13459 it from the list. */ 13460 prev_link = &derived->f2k_derived->finalizers; 13461 for (list = derived->f2k_derived->finalizers; list; list = *prev_link) 13462 { 13463 gfc_formal_arglist *dummy_args; 13464 gfc_symbol* arg; 13465 gfc_finalizer* i; 13466 int my_rank; 13467 13468 /* Skip this finalizer if we already resolved it. */ 13469 if (list->proc_tree) 13470 { 13471 if (list->proc_tree->n.sym->formal->sym->as == NULL 13472 || list->proc_tree->n.sym->formal->sym->as->rank == 0) 13473 seen_scalar = true; 13474 prev_link = &(list->next); 13475 continue; 13476 } 13477 13478 /* Check this exists and is a SUBROUTINE. */ 13479 if (!list->proc_sym->attr.subroutine) 13480 { 13481 gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE", 13482 list->proc_sym->name, &list->where); 13483 goto error; 13484 } 13485 13486 /* We should have exactly one argument. */ 13487 dummy_args = gfc_sym_get_dummy_args (list->proc_sym); 13488 if (!dummy_args || dummy_args->next) 13489 { 13490 gfc_error ("FINAL procedure at %L must have exactly one argument", 13491 &list->where); 13492 goto error; 13493 } 13494 arg = dummy_args->sym; 13495 13496 /* This argument must be of our type. */ 13497 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived) 13498 { 13499 gfc_error ("Argument of FINAL procedure at %L must be of type %qs", 13500 &arg->declared_at, derived->name); 13501 goto error; 13502 } 13503 13504 /* It must neither be a pointer nor allocatable nor optional. */ 13505 if (arg->attr.pointer) 13506 { 13507 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER", 13508 &arg->declared_at); 13509 goto error; 13510 } 13511 if (arg->attr.allocatable) 13512 { 13513 gfc_error ("Argument of FINAL procedure at %L must not be" 13514 " ALLOCATABLE", &arg->declared_at); 13515 goto error; 13516 } 13517 if (arg->attr.optional) 13518 { 13519 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL", 13520 &arg->declared_at); 13521 goto error; 13522 } 13523 13524 /* It must not be INTENT(OUT). */ 13525 if (arg->attr.intent == INTENT_OUT) 13526 { 13527 gfc_error ("Argument of FINAL procedure at %L must not be" 13528 " INTENT(OUT)", &arg->declared_at); 13529 goto error; 13530 } 13531 13532 /* Warn if the procedure is non-scalar and not assumed shape. */ 13533 if (warn_surprising && arg->as && arg->as->rank != 0 13534 && arg->as->type != AS_ASSUMED_SHAPE) 13535 gfc_warning (OPT_Wsurprising, 13536 "Non-scalar FINAL procedure at %L should have assumed" 13537 " shape argument", &arg->declared_at); 13538 13539 /* Check that it does not match in kind and rank with a FINAL procedure 13540 defined earlier. To really loop over the *earlier* declarations, 13541 we need to walk the tail of the list as new ones were pushed at the 13542 front. */ 13543 /* TODO: Handle kind parameters once they are implemented. */ 13544 my_rank = (arg->as ? arg->as->rank : 0); 13545 for (i = list->next; i; i = i->next) 13546 { 13547 gfc_formal_arglist *dummy_args; 13548 13549 /* Argument list might be empty; that is an error signalled earlier, 13550 but we nevertheless continued resolving. */ 13551 dummy_args = gfc_sym_get_dummy_args (i->proc_sym); 13552 if (dummy_args) 13553 { 13554 gfc_symbol* i_arg = dummy_args->sym; 13555 const int i_rank = (i_arg->as ? i_arg->as->rank : 0); 13556 if (i_rank == my_rank) 13557 { 13558 gfc_error ("FINAL procedure %qs declared at %L has the same" 13559 " rank (%d) as %qs", 13560 list->proc_sym->name, &list->where, my_rank, 13561 i->proc_sym->name); 13562 goto error; 13563 } 13564 } 13565 } 13566 13567 /* Is this the/a scalar finalizer procedure? */ 13568 if (my_rank == 0) 13569 seen_scalar = true; 13570 13571 /* Find the symtree for this procedure. */ 13572 gcc_assert (!list->proc_tree); 13573 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym); 13574 13575 prev_link = &list->next; 13576 continue; 13577 13578 /* Remove wrong nodes immediately from the list so we don't risk any 13579 troubles in the future when they might fail later expectations. */ 13580 error: 13581 i = list; 13582 *prev_link = list->next; 13583 gfc_free_finalizer (i); 13584 result = false; 13585 } 13586 13587 if (result == false) 13588 return false; 13589 13590 /* Warn if we haven't seen a scalar finalizer procedure (but we know there 13591 were nodes in the list, must have been for arrays. It is surely a good 13592 idea to have a scalar version there if there's something to finalize. */ 13593 if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar) 13594 gfc_warning (OPT_Wsurprising, 13595 "Only array FINAL procedures declared for derived type %qs" 13596 " defined at %L, suggest also scalar one", 13597 derived->name, &derived->declared_at); 13598 13599 vtab = gfc_find_derived_vtab (derived); 13600 c = vtab->ts.u.derived->components->next->next->next->next->next; 13601 gfc_set_sym_referenced (c->initializer->symtree->n.sym); 13602 13603 if (finalizable) 13604 *finalizable = true; 13605 13606 return true; 13607 } 13608 13609 13610 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */ 13611 13612 static bool 13613 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2, 13614 const char* generic_name, locus where) 13615 { 13616 gfc_symbol *sym1, *sym2; 13617 const char *pass1, *pass2; 13618 gfc_formal_arglist *dummy_args; 13619 13620 gcc_assert (t1->specific && t2->specific); 13621 gcc_assert (!t1->specific->is_generic); 13622 gcc_assert (!t2->specific->is_generic); 13623 gcc_assert (t1->is_operator == t2->is_operator); 13624 13625 sym1 = t1->specific->u.specific->n.sym; 13626 sym2 = t2->specific->u.specific->n.sym; 13627 13628 if (sym1 == sym2) 13629 return true; 13630 13631 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */ 13632 if (sym1->attr.subroutine != sym2->attr.subroutine 13633 || sym1->attr.function != sym2->attr.function) 13634 { 13635 gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for" 13636 " GENERIC %qs at %L", 13637 sym1->name, sym2->name, generic_name, &where); 13638 return false; 13639 } 13640 13641 /* Determine PASS arguments. */ 13642 if (t1->specific->nopass) 13643 pass1 = NULL; 13644 else if (t1->specific->pass_arg) 13645 pass1 = t1->specific->pass_arg; 13646 else 13647 { 13648 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym); 13649 if (dummy_args) 13650 pass1 = dummy_args->sym->name; 13651 else 13652 pass1 = NULL; 13653 } 13654 if (t2->specific->nopass) 13655 pass2 = NULL; 13656 else if (t2->specific->pass_arg) 13657 pass2 = t2->specific->pass_arg; 13658 else 13659 { 13660 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym); 13661 if (dummy_args) 13662 pass2 = dummy_args->sym->name; 13663 else 13664 pass2 = NULL; 13665 } 13666 13667 /* Compare the interfaces. */ 13668 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0, 13669 NULL, 0, pass1, pass2)) 13670 { 13671 gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous", 13672 sym1->name, sym2->name, generic_name, &where); 13673 return false; 13674 } 13675 13676 return true; 13677 } 13678 13679 13680 /* Worker function for resolving a generic procedure binding; this is used to 13681 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures. 13682 13683 The difference between those cases is finding possible inherited bindings 13684 that are overridden, as one has to look for them in tb_sym_root, 13685 tb_uop_root or tb_op, respectively. Thus the caller must already find 13686 the super-type and set p->overridden correctly. */ 13687 13688 static bool 13689 resolve_tb_generic_targets (gfc_symbol* super_type, 13690 gfc_typebound_proc* p, const char* name) 13691 { 13692 gfc_tbp_generic* target; 13693 gfc_symtree* first_target; 13694 gfc_symtree* inherited; 13695 13696 gcc_assert (p && p->is_generic); 13697 13698 /* Try to find the specific bindings for the symtrees in our target-list. */ 13699 gcc_assert (p->u.generic); 13700 for (target = p->u.generic; target; target = target->next) 13701 if (!target->specific) 13702 { 13703 gfc_typebound_proc* overridden_tbp; 13704 gfc_tbp_generic* g; 13705 const char* target_name; 13706 13707 target_name = target->specific_st->name; 13708 13709 /* Defined for this type directly. */ 13710 if (target->specific_st->n.tb && !target->specific_st->n.tb->error) 13711 { 13712 target->specific = target->specific_st->n.tb; 13713 goto specific_found; 13714 } 13715 13716 /* Look for an inherited specific binding. */ 13717 if (super_type) 13718 { 13719 inherited = gfc_find_typebound_proc (super_type, NULL, target_name, 13720 true, NULL); 13721 13722 if (inherited) 13723 { 13724 gcc_assert (inherited->n.tb); 13725 target->specific = inherited->n.tb; 13726 goto specific_found; 13727 } 13728 } 13729 13730 gfc_error ("Undefined specific binding %qs as target of GENERIC %qs" 13731 " at %L", target_name, name, &p->where); 13732 return false; 13733 13734 /* Once we've found the specific binding, check it is not ambiguous with 13735 other specifics already found or inherited for the same GENERIC. */ 13736 specific_found: 13737 gcc_assert (target->specific); 13738 13739 /* This must really be a specific binding! */ 13740 if (target->specific->is_generic) 13741 { 13742 gfc_error ("GENERIC %qs at %L must target a specific binding," 13743 " %qs is GENERIC, too", name, &p->where, target_name); 13744 return false; 13745 } 13746 13747 /* Check those already resolved on this type directly. */ 13748 for (g = p->u.generic; g; g = g->next) 13749 if (g != target && g->specific 13750 && !check_generic_tbp_ambiguity (target, g, name, p->where)) 13751 return false; 13752 13753 /* Check for ambiguity with inherited specific targets. */ 13754 for (overridden_tbp = p->overridden; overridden_tbp; 13755 overridden_tbp = overridden_tbp->overridden) 13756 if (overridden_tbp->is_generic) 13757 { 13758 for (g = overridden_tbp->u.generic; g; g = g->next) 13759 { 13760 gcc_assert (g->specific); 13761 if (!check_generic_tbp_ambiguity (target, g, name, p->where)) 13762 return false; 13763 } 13764 } 13765 } 13766 13767 /* If we attempt to "overwrite" a specific binding, this is an error. */ 13768 if (p->overridden && !p->overridden->is_generic) 13769 { 13770 gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with" 13771 " the same name", name, &p->where); 13772 return false; 13773 } 13774 13775 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as 13776 all must have the same attributes here. */ 13777 first_target = p->u.generic->specific->u.specific; 13778 gcc_assert (first_target); 13779 p->subroutine = first_target->n.sym->attr.subroutine; 13780 p->function = first_target->n.sym->attr.function; 13781 13782 return true; 13783 } 13784 13785 13786 /* Resolve a GENERIC procedure binding for a derived type. */ 13787 13788 static bool 13789 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st) 13790 { 13791 gfc_symbol* super_type; 13792 13793 /* Find the overridden binding if any. */ 13794 st->n.tb->overridden = NULL; 13795 super_type = gfc_get_derived_super_type (derived); 13796 if (super_type) 13797 { 13798 gfc_symtree* overridden; 13799 overridden = gfc_find_typebound_proc (super_type, NULL, st->name, 13800 true, NULL); 13801 13802 if (overridden && overridden->n.tb) 13803 st->n.tb->overridden = overridden->n.tb; 13804 } 13805 13806 /* Resolve using worker function. */ 13807 return resolve_tb_generic_targets (super_type, st->n.tb, st->name); 13808 } 13809 13810 13811 /* Retrieve the target-procedure of an operator binding and do some checks in 13812 common for intrinsic and user-defined type-bound operators. */ 13813 13814 static gfc_symbol* 13815 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where) 13816 { 13817 gfc_symbol* target_proc; 13818 13819 gcc_assert (target->specific && !target->specific->is_generic); 13820 target_proc = target->specific->u.specific->n.sym; 13821 gcc_assert (target_proc); 13822 13823 /* F08:C468. All operator bindings must have a passed-object dummy argument. */ 13824 if (target->specific->nopass) 13825 { 13826 gfc_error ("Type-bound operator at %L cannot be NOPASS", &where); 13827 return NULL; 13828 } 13829 13830 return target_proc; 13831 } 13832 13833 13834 /* Resolve a type-bound intrinsic operator. */ 13835 13836 static bool 13837 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op, 13838 gfc_typebound_proc* p) 13839 { 13840 gfc_symbol* super_type; 13841 gfc_tbp_generic* target; 13842 13843 /* If there's already an error here, do nothing (but don't fail again). */ 13844 if (p->error) 13845 return true; 13846 13847 /* Operators should always be GENERIC bindings. */ 13848 gcc_assert (p->is_generic); 13849 13850 /* Look for an overridden binding. */ 13851 super_type = gfc_get_derived_super_type (derived); 13852 if (super_type && super_type->f2k_derived) 13853 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL, 13854 op, true, NULL); 13855 else 13856 p->overridden = NULL; 13857 13858 /* Resolve general GENERIC properties using worker function. */ 13859 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op))) 13860 goto error; 13861 13862 /* Check the targets to be procedures of correct interface. */ 13863 for (target = p->u.generic; target; target = target->next) 13864 { 13865 gfc_symbol* target_proc; 13866 13867 target_proc = get_checked_tb_operator_target (target, p->where); 13868 if (!target_proc) 13869 goto error; 13870 13871 if (!gfc_check_operator_interface (target_proc, op, p->where)) 13872 goto error; 13873 13874 /* Add target to non-typebound operator list. */ 13875 if (!target->specific->deferred && !derived->attr.use_assoc 13876 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns) 13877 { 13878 gfc_interface *head, *intr; 13879 13880 /* Preempt 'gfc_check_new_interface' for submodules, where the 13881 mechanism for handling module procedures winds up resolving 13882 operator interfaces twice and would otherwise cause an error. */ 13883 for (intr = derived->ns->op[op]; intr; intr = intr->next) 13884 if (intr->sym == target_proc 13885 && target_proc->attr.used_in_submodule) 13886 return true; 13887 13888 if (!gfc_check_new_interface (derived->ns->op[op], 13889 target_proc, p->where)) 13890 return false; 13891 head = derived->ns->op[op]; 13892 intr = gfc_get_interface (); 13893 intr->sym = target_proc; 13894 intr->where = p->where; 13895 intr->next = head; 13896 derived->ns->op[op] = intr; 13897 } 13898 } 13899 13900 return true; 13901 13902 error: 13903 p->error = 1; 13904 return false; 13905 } 13906 13907 13908 /* Resolve a type-bound user operator (tree-walker callback). */ 13909 13910 static gfc_symbol* resolve_bindings_derived; 13911 static bool resolve_bindings_result; 13912 13913 static bool check_uop_procedure (gfc_symbol* sym, locus where); 13914 13915 static void 13916 resolve_typebound_user_op (gfc_symtree* stree) 13917 { 13918 gfc_symbol* super_type; 13919 gfc_tbp_generic* target; 13920 13921 gcc_assert (stree && stree->n.tb); 13922 13923 if (stree->n.tb->error) 13924 return; 13925 13926 /* Operators should always be GENERIC bindings. */ 13927 gcc_assert (stree->n.tb->is_generic); 13928 13929 /* Find overridden procedure, if any. */ 13930 super_type = gfc_get_derived_super_type (resolve_bindings_derived); 13931 if (super_type && super_type->f2k_derived) 13932 { 13933 gfc_symtree* overridden; 13934 overridden = gfc_find_typebound_user_op (super_type, NULL, 13935 stree->name, true, NULL); 13936 13937 if (overridden && overridden->n.tb) 13938 stree->n.tb->overridden = overridden->n.tb; 13939 } 13940 else 13941 stree->n.tb->overridden = NULL; 13942 13943 /* Resolve basically using worker function. */ 13944 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name)) 13945 goto error; 13946 13947 /* Check the targets to be functions of correct interface. */ 13948 for (target = stree->n.tb->u.generic; target; target = target->next) 13949 { 13950 gfc_symbol* target_proc; 13951 13952 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where); 13953 if (!target_proc) 13954 goto error; 13955 13956 if (!check_uop_procedure (target_proc, stree->n.tb->where)) 13957 goto error; 13958 } 13959 13960 return; 13961 13962 error: 13963 resolve_bindings_result = false; 13964 stree->n.tb->error = 1; 13965 } 13966 13967 13968 /* Resolve the type-bound procedures for a derived type. */ 13969 13970 static void 13971 resolve_typebound_procedure (gfc_symtree* stree) 13972 { 13973 gfc_symbol* proc; 13974 locus where; 13975 gfc_symbol* me_arg; 13976 gfc_symbol* super_type; 13977 gfc_component* comp; 13978 13979 gcc_assert (stree); 13980 13981 /* Undefined specific symbol from GENERIC target definition. */ 13982 if (!stree->n.tb) 13983 return; 13984 13985 if (stree->n.tb->error) 13986 return; 13987 13988 /* If this is a GENERIC binding, use that routine. */ 13989 if (stree->n.tb->is_generic) 13990 { 13991 if (!resolve_typebound_generic (resolve_bindings_derived, stree)) 13992 goto error; 13993 return; 13994 } 13995 13996 /* Get the target-procedure to check it. */ 13997 gcc_assert (!stree->n.tb->is_generic); 13998 gcc_assert (stree->n.tb->u.specific); 13999 proc = stree->n.tb->u.specific->n.sym; 14000 where = stree->n.tb->where; 14001 14002 /* Default access should already be resolved from the parser. */ 14003 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN); 14004 14005 if (stree->n.tb->deferred) 14006 { 14007 if (!check_proc_interface (proc, &where)) 14008 goto error; 14009 } 14010 else 14011 { 14012 /* If proc has not been resolved at this point, proc->name may 14013 actually be a USE associated entity. See PR fortran/89647. */ 14014 if (!proc->resolve_symbol_called 14015 && proc->attr.function == 0 && proc->attr.subroutine == 0) 14016 { 14017 gfc_symbol *tmp; 14018 gfc_find_symbol (proc->name, gfc_current_ns->parent, 1, &tmp); 14019 if (tmp && tmp->attr.use_assoc) 14020 { 14021 proc->module = tmp->module; 14022 proc->attr.proc = tmp->attr.proc; 14023 proc->attr.function = tmp->attr.function; 14024 proc->attr.subroutine = tmp->attr.subroutine; 14025 proc->attr.use_assoc = tmp->attr.use_assoc; 14026 proc->ts = tmp->ts; 14027 proc->result = tmp->result; 14028 } 14029 } 14030 14031 /* Check for F08:C465. */ 14032 if ((!proc->attr.subroutine && !proc->attr.function) 14033 || (proc->attr.proc != PROC_MODULE 14034 && proc->attr.if_source != IFSRC_IFBODY) 14035 || proc->attr.abstract) 14036 { 14037 gfc_error ("%qs must be a module procedure or an external " 14038 "procedure with an explicit interface at %L", 14039 proc->name, &where); 14040 goto error; 14041 } 14042 } 14043 14044 stree->n.tb->subroutine = proc->attr.subroutine; 14045 stree->n.tb->function = proc->attr.function; 14046 14047 /* Find the super-type of the current derived type. We could do this once and 14048 store in a global if speed is needed, but as long as not I believe this is 14049 more readable and clearer. */ 14050 super_type = gfc_get_derived_super_type (resolve_bindings_derived); 14051 14052 /* If PASS, resolve and check arguments if not already resolved / loaded 14053 from a .mod file. */ 14054 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0) 14055 { 14056 gfc_formal_arglist *dummy_args; 14057 14058 dummy_args = gfc_sym_get_dummy_args (proc); 14059 if (stree->n.tb->pass_arg) 14060 { 14061 gfc_formal_arglist *i; 14062 14063 /* If an explicit passing argument name is given, walk the arg-list 14064 and look for it. */ 14065 14066 me_arg = NULL; 14067 stree->n.tb->pass_arg_num = 1; 14068 for (i = dummy_args; i; i = i->next) 14069 { 14070 if (!strcmp (i->sym->name, stree->n.tb->pass_arg)) 14071 { 14072 me_arg = i->sym; 14073 break; 14074 } 14075 ++stree->n.tb->pass_arg_num; 14076 } 14077 14078 if (!me_arg) 14079 { 14080 gfc_error ("Procedure %qs with PASS(%s) at %L has no" 14081 " argument %qs", 14082 proc->name, stree->n.tb->pass_arg, &where, 14083 stree->n.tb->pass_arg); 14084 goto error; 14085 } 14086 } 14087 else 14088 { 14089 /* Otherwise, take the first one; there should in fact be at least 14090 one. */ 14091 stree->n.tb->pass_arg_num = 1; 14092 if (!dummy_args) 14093 { 14094 gfc_error ("Procedure %qs with PASS at %L must have at" 14095 " least one argument", proc->name, &where); 14096 goto error; 14097 } 14098 me_arg = dummy_args->sym; 14099 } 14100 14101 /* Now check that the argument-type matches and the passed-object 14102 dummy argument is generally fine. */ 14103 14104 gcc_assert (me_arg); 14105 14106 if (me_arg->ts.type != BT_CLASS) 14107 { 14108 gfc_error ("Non-polymorphic passed-object dummy argument of %qs" 14109 " at %L", proc->name, &where); 14110 goto error; 14111 } 14112 14113 if (CLASS_DATA (me_arg)->ts.u.derived 14114 != resolve_bindings_derived) 14115 { 14116 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of" 14117 " the derived-type %qs", me_arg->name, proc->name, 14118 me_arg->name, &where, resolve_bindings_derived->name); 14119 goto error; 14120 } 14121 14122 gcc_assert (me_arg->ts.type == BT_CLASS); 14123 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0) 14124 { 14125 gfc_error ("Passed-object dummy argument of %qs at %L must be" 14126 " scalar", proc->name, &where); 14127 goto error; 14128 } 14129 if (CLASS_DATA (me_arg)->attr.allocatable) 14130 { 14131 gfc_error ("Passed-object dummy argument of %qs at %L must not" 14132 " be ALLOCATABLE", proc->name, &where); 14133 goto error; 14134 } 14135 if (CLASS_DATA (me_arg)->attr.class_pointer) 14136 { 14137 gfc_error ("Passed-object dummy argument of %qs at %L must not" 14138 " be POINTER", proc->name, &where); 14139 goto error; 14140 } 14141 } 14142 14143 /* If we are extending some type, check that we don't override a procedure 14144 flagged NON_OVERRIDABLE. */ 14145 stree->n.tb->overridden = NULL; 14146 if (super_type) 14147 { 14148 gfc_symtree* overridden; 14149 overridden = gfc_find_typebound_proc (super_type, NULL, 14150 stree->name, true, NULL); 14151 14152 if (overridden) 14153 { 14154 if (overridden->n.tb) 14155 stree->n.tb->overridden = overridden->n.tb; 14156 14157 if (!gfc_check_typebound_override (stree, overridden)) 14158 goto error; 14159 } 14160 } 14161 14162 /* See if there's a name collision with a component directly in this type. */ 14163 for (comp = resolve_bindings_derived->components; comp; comp = comp->next) 14164 if (!strcmp (comp->name, stree->name)) 14165 { 14166 gfc_error ("Procedure %qs at %L has the same name as a component of" 14167 " %qs", 14168 stree->name, &where, resolve_bindings_derived->name); 14169 goto error; 14170 } 14171 14172 /* Try to find a name collision with an inherited component. */ 14173 if (super_type && gfc_find_component (super_type, stree->name, true, true, 14174 NULL)) 14175 { 14176 gfc_error ("Procedure %qs at %L has the same name as an inherited" 14177 " component of %qs", 14178 stree->name, &where, resolve_bindings_derived->name); 14179 goto error; 14180 } 14181 14182 stree->n.tb->error = 0; 14183 return; 14184 14185 error: 14186 resolve_bindings_result = false; 14187 stree->n.tb->error = 1; 14188 } 14189 14190 14191 static bool 14192 resolve_typebound_procedures (gfc_symbol* derived) 14193 { 14194 int op; 14195 gfc_symbol* super_type; 14196 14197 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root) 14198 return true; 14199 14200 super_type = gfc_get_derived_super_type (derived); 14201 if (super_type) 14202 resolve_symbol (super_type); 14203 14204 resolve_bindings_derived = derived; 14205 resolve_bindings_result = true; 14206 14207 if (derived->f2k_derived->tb_sym_root) 14208 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root, 14209 &resolve_typebound_procedure); 14210 14211 if (derived->f2k_derived->tb_uop_root) 14212 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root, 14213 &resolve_typebound_user_op); 14214 14215 for (op = 0; op != GFC_INTRINSIC_OPS; ++op) 14216 { 14217 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op]; 14218 if (p && !resolve_typebound_intrinsic_op (derived, 14219 (gfc_intrinsic_op)op, p)) 14220 resolve_bindings_result = false; 14221 } 14222 14223 return resolve_bindings_result; 14224 } 14225 14226 14227 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c 14228 to give all identical derived types the same backend_decl. */ 14229 static void 14230 add_dt_to_dt_list (gfc_symbol *derived) 14231 { 14232 if (!derived->dt_next) 14233 { 14234 if (gfc_derived_types) 14235 { 14236 derived->dt_next = gfc_derived_types->dt_next; 14237 gfc_derived_types->dt_next = derived; 14238 } 14239 else 14240 { 14241 derived->dt_next = derived; 14242 } 14243 gfc_derived_types = derived; 14244 } 14245 } 14246 14247 14248 /* Ensure that a derived-type is really not abstract, meaning that every 14249 inherited DEFERRED binding is overridden by a non-DEFERRED one. */ 14250 14251 static bool 14252 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st) 14253 { 14254 if (!st) 14255 return true; 14256 14257 if (!ensure_not_abstract_walker (sub, st->left)) 14258 return false; 14259 if (!ensure_not_abstract_walker (sub, st->right)) 14260 return false; 14261 14262 if (st->n.tb && st->n.tb->deferred) 14263 { 14264 gfc_symtree* overriding; 14265 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL); 14266 if (!overriding) 14267 return false; 14268 gcc_assert (overriding->n.tb); 14269 if (overriding->n.tb->deferred) 14270 { 14271 gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because" 14272 " %qs is DEFERRED and not overridden", 14273 sub->name, &sub->declared_at, st->name); 14274 return false; 14275 } 14276 } 14277 14278 return true; 14279 } 14280 14281 static bool 14282 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor) 14283 { 14284 /* The algorithm used here is to recursively travel up the ancestry of sub 14285 and for each ancestor-type, check all bindings. If any of them is 14286 DEFERRED, look it up starting from sub and see if the found (overriding) 14287 binding is not DEFERRED. 14288 This is not the most efficient way to do this, but it should be ok and is 14289 clearer than something sophisticated. */ 14290 14291 gcc_assert (ancestor && !sub->attr.abstract); 14292 14293 if (!ancestor->attr.abstract) 14294 return true; 14295 14296 /* Walk bindings of this ancestor. */ 14297 if (ancestor->f2k_derived) 14298 { 14299 bool t; 14300 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root); 14301 if (!t) 14302 return false; 14303 } 14304 14305 /* Find next ancestor type and recurse on it. */ 14306 ancestor = gfc_get_derived_super_type (ancestor); 14307 if (ancestor) 14308 return ensure_not_abstract (sub, ancestor); 14309 14310 return true; 14311 } 14312 14313 14314 /* This check for typebound defined assignments is done recursively 14315 since the order in which derived types are resolved is not always in 14316 order of the declarations. */ 14317 14318 static void 14319 check_defined_assignments (gfc_symbol *derived) 14320 { 14321 gfc_component *c; 14322 14323 for (c = derived->components; c; c = c->next) 14324 { 14325 if (!gfc_bt_struct (c->ts.type) 14326 || c->attr.pointer 14327 || c->attr.allocatable 14328 || c->attr.proc_pointer_comp 14329 || c->attr.class_pointer 14330 || c->attr.proc_pointer) 14331 continue; 14332 14333 if (c->ts.u.derived->attr.defined_assign_comp 14334 || (c->ts.u.derived->f2k_derived 14335 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN])) 14336 { 14337 derived->attr.defined_assign_comp = 1; 14338 return; 14339 } 14340 14341 check_defined_assignments (c->ts.u.derived); 14342 if (c->ts.u.derived->attr.defined_assign_comp) 14343 { 14344 derived->attr.defined_assign_comp = 1; 14345 return; 14346 } 14347 } 14348 } 14349 14350 14351 /* Resolve a single component of a derived type or structure. */ 14352 14353 static bool 14354 resolve_component (gfc_component *c, gfc_symbol *sym) 14355 { 14356 gfc_symbol *super_type; 14357 symbol_attribute *attr; 14358 14359 if (c->attr.artificial) 14360 return true; 14361 14362 /* Do not allow vtype components to be resolved in nameless namespaces 14363 such as block data because the procedure pointers will cause ICEs 14364 and vtables are not needed in these contexts. */ 14365 if (sym->attr.vtype && sym->attr.use_assoc 14366 && sym->ns->proc_name == NULL) 14367 return true; 14368 14369 /* F2008, C442. */ 14370 if ((!sym->attr.is_class || c != sym->components) 14371 && c->attr.codimension 14372 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED))) 14373 { 14374 gfc_error ("Coarray component %qs at %L must be allocatable with " 14375 "deferred shape", c->name, &c->loc); 14376 return false; 14377 } 14378 14379 /* F2008, C443. */ 14380 if (c->attr.codimension && c->ts.type == BT_DERIVED 14381 && c->ts.u.derived->ts.is_iso_c) 14382 { 14383 gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) " 14384 "shall not be a coarray", c->name, &c->loc); 14385 return false; 14386 } 14387 14388 /* F2008, C444. */ 14389 if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp 14390 && (c->attr.codimension || c->attr.pointer || c->attr.dimension 14391 || c->attr.allocatable)) 14392 { 14393 gfc_error ("Component %qs at %L with coarray component " 14394 "shall be a nonpointer, nonallocatable scalar", 14395 c->name, &c->loc); 14396 return false; 14397 } 14398 14399 /* F2008, C448. */ 14400 if (c->ts.type == BT_CLASS) 14401 { 14402 if (c->attr.class_ok && CLASS_DATA (c)) 14403 { 14404 attr = &(CLASS_DATA (c)->attr); 14405 14406 /* Fix up contiguous attribute. */ 14407 if (c->attr.contiguous) 14408 attr->contiguous = 1; 14409 } 14410 else 14411 attr = NULL; 14412 } 14413 else 14414 attr = &c->attr; 14415 14416 if (attr && attr->contiguous && (!attr->dimension || !attr->pointer)) 14417 { 14418 gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but " 14419 "is not an array pointer", c->name, &c->loc); 14420 return false; 14421 } 14422 14423 /* F2003, 15.2.1 - length has to be one. */ 14424 if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER 14425 && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL 14426 || !gfc_is_constant_expr (c->ts.u.cl->length) 14427 || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0)) 14428 { 14429 gfc_error ("Component %qs of BIND(C) type at %L must have length one", 14430 c->name, &c->loc); 14431 return false; 14432 } 14433 14434 if (c->attr.proc_pointer && c->ts.interface) 14435 { 14436 gfc_symbol *ifc = c->ts.interface; 14437 14438 if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc)) 14439 { 14440 c->tb->error = 1; 14441 return false; 14442 } 14443 14444 if (ifc->attr.if_source || ifc->attr.intrinsic) 14445 { 14446 /* Resolve interface and copy attributes. */ 14447 if (ifc->formal && !ifc->formal_ns) 14448 resolve_symbol (ifc); 14449 if (ifc->attr.intrinsic) 14450 gfc_resolve_intrinsic (ifc, &ifc->declared_at); 14451 14452 if (ifc->result) 14453 { 14454 c->ts = ifc->result->ts; 14455 c->attr.allocatable = ifc->result->attr.allocatable; 14456 c->attr.pointer = ifc->result->attr.pointer; 14457 c->attr.dimension = ifc->result->attr.dimension; 14458 c->as = gfc_copy_array_spec (ifc->result->as); 14459 c->attr.class_ok = ifc->result->attr.class_ok; 14460 } 14461 else 14462 { 14463 c->ts = ifc->ts; 14464 c->attr.allocatable = ifc->attr.allocatable; 14465 c->attr.pointer = ifc->attr.pointer; 14466 c->attr.dimension = ifc->attr.dimension; 14467 c->as = gfc_copy_array_spec (ifc->as); 14468 c->attr.class_ok = ifc->attr.class_ok; 14469 } 14470 c->ts.interface = ifc; 14471 c->attr.function = ifc->attr.function; 14472 c->attr.subroutine = ifc->attr.subroutine; 14473 14474 c->attr.pure = ifc->attr.pure; 14475 c->attr.elemental = ifc->attr.elemental; 14476 c->attr.recursive = ifc->attr.recursive; 14477 c->attr.always_explicit = ifc->attr.always_explicit; 14478 c->attr.ext_attr |= ifc->attr.ext_attr; 14479 /* Copy char length. */ 14480 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl) 14481 { 14482 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl); 14483 if (cl->length && !cl->resolved 14484 && !gfc_resolve_expr (cl->length)) 14485 { 14486 c->tb->error = 1; 14487 return false; 14488 } 14489 c->ts.u.cl = cl; 14490 } 14491 } 14492 } 14493 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN) 14494 { 14495 /* Since PPCs are not implicitly typed, a PPC without an explicit 14496 interface must be a subroutine. */ 14497 gfc_add_subroutine (&c->attr, c->name, &c->loc); 14498 } 14499 14500 /* Procedure pointer components: Check PASS arg. */ 14501 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0 14502 && !sym->attr.vtype) 14503 { 14504 gfc_symbol* me_arg; 14505 14506 if (c->tb->pass_arg) 14507 { 14508 gfc_formal_arglist* i; 14509 14510 /* If an explicit passing argument name is given, walk the arg-list 14511 and look for it. */ 14512 14513 me_arg = NULL; 14514 c->tb->pass_arg_num = 1; 14515 for (i = c->ts.interface->formal; i; i = i->next) 14516 { 14517 if (!strcmp (i->sym->name, c->tb->pass_arg)) 14518 { 14519 me_arg = i->sym; 14520 break; 14521 } 14522 c->tb->pass_arg_num++; 14523 } 14524 14525 if (!me_arg) 14526 { 14527 gfc_error ("Procedure pointer component %qs with PASS(%s) " 14528 "at %L has no argument %qs", c->name, 14529 c->tb->pass_arg, &c->loc, c->tb->pass_arg); 14530 c->tb->error = 1; 14531 return false; 14532 } 14533 } 14534 else 14535 { 14536 /* Otherwise, take the first one; there should in fact be at least 14537 one. */ 14538 c->tb->pass_arg_num = 1; 14539 if (!c->ts.interface->formal) 14540 { 14541 gfc_error ("Procedure pointer component %qs with PASS at %L " 14542 "must have at least one argument", 14543 c->name, &c->loc); 14544 c->tb->error = 1; 14545 return false; 14546 } 14547 me_arg = c->ts.interface->formal->sym; 14548 } 14549 14550 /* Now check that the argument-type matches. */ 14551 gcc_assert (me_arg); 14552 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS) 14553 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym) 14554 || (me_arg->ts.type == BT_CLASS 14555 && CLASS_DATA (me_arg)->ts.u.derived != sym)) 14556 { 14557 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of" 14558 " the derived type %qs", me_arg->name, c->name, 14559 me_arg->name, &c->loc, sym->name); 14560 c->tb->error = 1; 14561 return false; 14562 } 14563 14564 /* Check for F03:C453. */ 14565 if (CLASS_DATA (me_arg)->attr.dimension) 14566 { 14567 gfc_error ("Argument %qs of %qs with PASS(%s) at %L " 14568 "must be scalar", me_arg->name, c->name, me_arg->name, 14569 &c->loc); 14570 c->tb->error = 1; 14571 return false; 14572 } 14573 14574 if (CLASS_DATA (me_arg)->attr.class_pointer) 14575 { 14576 gfc_error ("Argument %qs of %qs with PASS(%s) at %L " 14577 "may not have the POINTER attribute", me_arg->name, 14578 c->name, me_arg->name, &c->loc); 14579 c->tb->error = 1; 14580 return false; 14581 } 14582 14583 if (CLASS_DATA (me_arg)->attr.allocatable) 14584 { 14585 gfc_error ("Argument %qs of %qs with PASS(%s) at %L " 14586 "may not be ALLOCATABLE", me_arg->name, c->name, 14587 me_arg->name, &c->loc); 14588 c->tb->error = 1; 14589 return false; 14590 } 14591 14592 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS) 14593 { 14594 gfc_error ("Non-polymorphic passed-object dummy argument of %qs" 14595 " at %L", c->name, &c->loc); 14596 return false; 14597 } 14598 14599 } 14600 14601 /* Check type-spec if this is not the parent-type component. */ 14602 if (((sym->attr.is_class 14603 && (!sym->components->ts.u.derived->attr.extension 14604 || c != sym->components->ts.u.derived->components)) 14605 || (!sym->attr.is_class 14606 && (!sym->attr.extension || c != sym->components))) 14607 && !sym->attr.vtype 14608 && !resolve_typespec_used (&c->ts, &c->loc, c->name)) 14609 return false; 14610 14611 super_type = gfc_get_derived_super_type (sym); 14612 14613 /* If this type is an extension, set the accessibility of the parent 14614 component. */ 14615 if (super_type 14616 && ((sym->attr.is_class 14617 && c == sym->components->ts.u.derived->components) 14618 || (!sym->attr.is_class && c == sym->components)) 14619 && strcmp (super_type->name, c->name) == 0) 14620 c->attr.access = super_type->attr.access; 14621 14622 /* If this type is an extension, see if this component has the same name 14623 as an inherited type-bound procedure. */ 14624 if (super_type && !sym->attr.is_class 14625 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL)) 14626 { 14627 gfc_error ("Component %qs of %qs at %L has the same name as an" 14628 " inherited type-bound procedure", 14629 c->name, sym->name, &c->loc); 14630 return false; 14631 } 14632 14633 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer 14634 && !c->ts.deferred) 14635 { 14636 if (c->ts.u.cl->length == NULL 14637 || (!resolve_charlen(c->ts.u.cl)) 14638 || !gfc_is_constant_expr (c->ts.u.cl->length)) 14639 { 14640 gfc_error ("Character length of component %qs needs to " 14641 "be a constant specification expression at %L", 14642 c->name, 14643 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc); 14644 return false; 14645 } 14646 } 14647 14648 if (c->ts.type == BT_CHARACTER && c->ts.deferred 14649 && !c->attr.pointer && !c->attr.allocatable) 14650 { 14651 gfc_error ("Character component %qs of %qs at %L with deferred " 14652 "length must be a POINTER or ALLOCATABLE", 14653 c->name, sym->name, &c->loc); 14654 return false; 14655 } 14656 14657 /* Add the hidden deferred length field. */ 14658 if (c->ts.type == BT_CHARACTER 14659 && (c->ts.deferred || c->attr.pdt_string) 14660 && !c->attr.function 14661 && !sym->attr.is_class) 14662 { 14663 char name[GFC_MAX_SYMBOL_LEN+9]; 14664 gfc_component *strlen; 14665 sprintf (name, "_%s_length", c->name); 14666 strlen = gfc_find_component (sym, name, true, true, NULL); 14667 if (strlen == NULL) 14668 { 14669 if (!gfc_add_component (sym, name, &strlen)) 14670 return false; 14671 strlen->ts.type = BT_INTEGER; 14672 strlen->ts.kind = gfc_charlen_int_kind; 14673 strlen->attr.access = ACCESS_PRIVATE; 14674 strlen->attr.artificial = 1; 14675 } 14676 } 14677 14678 if (c->ts.type == BT_DERIVED 14679 && sym->component_access != ACCESS_PRIVATE 14680 && gfc_check_symbol_access (sym) 14681 && !is_sym_host_assoc (c->ts.u.derived, sym->ns) 14682 && !c->ts.u.derived->attr.use_assoc 14683 && !gfc_check_symbol_access (c->ts.u.derived) 14684 && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a " 14685 "PRIVATE type and cannot be a component of " 14686 "%qs, which is PUBLIC at %L", c->name, 14687 sym->name, &sym->declared_at)) 14688 return false; 14689 14690 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS) 14691 { 14692 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) " 14693 "type %s", c->name, &c->loc, sym->name); 14694 return false; 14695 } 14696 14697 if (sym->attr.sequence) 14698 { 14699 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0) 14700 { 14701 gfc_error ("Component %s of SEQUENCE type declared at %L does " 14702 "not have the SEQUENCE attribute", 14703 c->ts.u.derived->name, &sym->declared_at); 14704 return false; 14705 } 14706 } 14707 14708 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic) 14709 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived); 14710 else if (c->ts.type == BT_CLASS && c->attr.class_ok 14711 && CLASS_DATA (c)->ts.u.derived->attr.generic) 14712 CLASS_DATA (c)->ts.u.derived 14713 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived); 14714 14715 /* If an allocatable component derived type is of the same type as 14716 the enclosing derived type, we need a vtable generating so that 14717 the __deallocate procedure is created. */ 14718 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS) 14719 && c->ts.u.derived == sym && c->attr.allocatable == 1) 14720 gfc_find_vtab (&c->ts); 14721 14722 /* Ensure that all the derived type components are put on the 14723 derived type list; even in formal namespaces, where derived type 14724 pointer components might not have been declared. */ 14725 if (c->ts.type == BT_DERIVED 14726 && c->ts.u.derived 14727 && c->ts.u.derived->components 14728 && c->attr.pointer 14729 && sym != c->ts.u.derived) 14730 add_dt_to_dt_list (c->ts.u.derived); 14731 14732 if (!gfc_resolve_array_spec (c->as, 14733 !(c->attr.pointer || c->attr.proc_pointer 14734 || c->attr.allocatable))) 14735 return false; 14736 14737 if (c->initializer && !sym->attr.vtype 14738 && !c->attr.pdt_kind && !c->attr.pdt_len 14739 && !gfc_check_assign_symbol (sym, c, c->initializer)) 14740 return false; 14741 14742 return true; 14743 } 14744 14745 14746 /* Be nice about the locus for a structure expression - show the locus of the 14747 first non-null sub-expression if we can. */ 14748 14749 static locus * 14750 cons_where (gfc_expr *struct_expr) 14751 { 14752 gfc_constructor *cons; 14753 14754 gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE); 14755 14756 cons = gfc_constructor_first (struct_expr->value.constructor); 14757 for (; cons; cons = gfc_constructor_next (cons)) 14758 { 14759 if (cons->expr && cons->expr->expr_type != EXPR_NULL) 14760 return &cons->expr->where; 14761 } 14762 14763 return &struct_expr->where; 14764 } 14765 14766 /* Resolve the components of a structure type. Much less work than derived 14767 types. */ 14768 14769 static bool 14770 resolve_fl_struct (gfc_symbol *sym) 14771 { 14772 gfc_component *c; 14773 gfc_expr *init = NULL; 14774 bool success; 14775 14776 /* Make sure UNIONs do not have overlapping initializers. */ 14777 if (sym->attr.flavor == FL_UNION) 14778 { 14779 for (c = sym->components; c; c = c->next) 14780 { 14781 if (init && c->initializer) 14782 { 14783 gfc_error ("Conflicting initializers in union at %L and %L", 14784 cons_where (init), cons_where (c->initializer)); 14785 gfc_free_expr (c->initializer); 14786 c->initializer = NULL; 14787 } 14788 if (init == NULL) 14789 init = c->initializer; 14790 } 14791 } 14792 14793 success = true; 14794 for (c = sym->components; c; c = c->next) 14795 if (!resolve_component (c, sym)) 14796 success = false; 14797 14798 if (!success) 14799 return false; 14800 14801 if (sym->components) 14802 add_dt_to_dt_list (sym); 14803 14804 return true; 14805 } 14806 14807 14808 /* Resolve the components of a derived type. This does not have to wait until 14809 resolution stage, but can be done as soon as the dt declaration has been 14810 parsed. */ 14811 14812 static bool 14813 resolve_fl_derived0 (gfc_symbol *sym) 14814 { 14815 gfc_symbol* super_type; 14816 gfc_component *c; 14817 gfc_formal_arglist *f; 14818 bool success; 14819 14820 if (sym->attr.unlimited_polymorphic) 14821 return true; 14822 14823 super_type = gfc_get_derived_super_type (sym); 14824 14825 /* F2008, C432. */ 14826 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp) 14827 { 14828 gfc_error ("As extending type %qs at %L has a coarray component, " 14829 "parent type %qs shall also have one", sym->name, 14830 &sym->declared_at, super_type->name); 14831 return false; 14832 } 14833 14834 /* Ensure the extended type gets resolved before we do. */ 14835 if (super_type && !resolve_fl_derived0 (super_type)) 14836 return false; 14837 14838 /* An ABSTRACT type must be extensible. */ 14839 if (sym->attr.abstract && !gfc_type_is_extensible (sym)) 14840 { 14841 gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT", 14842 sym->name, &sym->declared_at); 14843 return false; 14844 } 14845 14846 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components 14847 : sym->components; 14848 14849 success = true; 14850 for ( ; c != NULL; c = c->next) 14851 if (!resolve_component (c, sym)) 14852 success = false; 14853 14854 if (!success) 14855 return false; 14856 14857 /* Now add the caf token field, where needed. */ 14858 if (flag_coarray != GFC_FCOARRAY_NONE 14859 && !sym->attr.is_class && !sym->attr.vtype) 14860 { 14861 for (c = sym->components; c; c = c->next) 14862 if (!c->attr.dimension && !c->attr.codimension 14863 && (c->attr.allocatable || c->attr.pointer)) 14864 { 14865 char name[GFC_MAX_SYMBOL_LEN+9]; 14866 gfc_component *token; 14867 sprintf (name, "_caf_%s", c->name); 14868 token = gfc_find_component (sym, name, true, true, NULL); 14869 if (token == NULL) 14870 { 14871 if (!gfc_add_component (sym, name, &token)) 14872 return false; 14873 token->ts.type = BT_VOID; 14874 token->ts.kind = gfc_default_integer_kind; 14875 token->attr.access = ACCESS_PRIVATE; 14876 token->attr.artificial = 1; 14877 token->attr.caf_token = 1; 14878 } 14879 } 14880 } 14881 14882 check_defined_assignments (sym); 14883 14884 if (!sym->attr.defined_assign_comp && super_type) 14885 sym->attr.defined_assign_comp 14886 = super_type->attr.defined_assign_comp; 14887 14888 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that 14889 all DEFERRED bindings are overridden. */ 14890 if (super_type && super_type->attr.abstract && !sym->attr.abstract 14891 && !sym->attr.is_class 14892 && !ensure_not_abstract (sym, super_type)) 14893 return false; 14894 14895 /* Check that there is a component for every PDT parameter. */ 14896 if (sym->attr.pdt_template) 14897 { 14898 for (f = sym->formal; f; f = f->next) 14899 { 14900 if (!f->sym) 14901 continue; 14902 c = gfc_find_component (sym, f->sym->name, true, true, NULL); 14903 if (c == NULL) 14904 { 14905 gfc_error ("Parameterized type %qs does not have a component " 14906 "corresponding to parameter %qs at %L", sym->name, 14907 f->sym->name, &sym->declared_at); 14908 break; 14909 } 14910 } 14911 } 14912 14913 /* Add derived type to the derived type list. */ 14914 add_dt_to_dt_list (sym); 14915 14916 return true; 14917 } 14918 14919 14920 /* The following procedure does the full resolution of a derived type, 14921 including resolution of all type-bound procedures (if present). In contrast 14922 to 'resolve_fl_derived0' this can only be done after the module has been 14923 parsed completely. */ 14924 14925 static bool 14926 resolve_fl_derived (gfc_symbol *sym) 14927 { 14928 gfc_symbol *gen_dt = NULL; 14929 14930 if (sym->attr.unlimited_polymorphic) 14931 return true; 14932 14933 if (!sym->attr.is_class) 14934 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt); 14935 if (gen_dt && gen_dt->generic && gen_dt->generic->next 14936 && (!gen_dt->generic->sym->attr.use_assoc 14937 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module) 14938 && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function " 14939 "%qs at %L being the same name as derived " 14940 "type at %L", sym->name, 14941 gen_dt->generic->sym == sym 14942 ? gen_dt->generic->next->sym->name 14943 : gen_dt->generic->sym->name, 14944 gen_dt->generic->sym == sym 14945 ? &gen_dt->generic->next->sym->declared_at 14946 : &gen_dt->generic->sym->declared_at, 14947 &sym->declared_at)) 14948 return false; 14949 14950 if (sym->components == NULL && !sym->attr.zero_comp && !sym->attr.use_assoc) 14951 { 14952 gfc_error ("Derived type %qs at %L has not been declared", 14953 sym->name, &sym->declared_at); 14954 return false; 14955 } 14956 14957 /* Resolve the finalizer procedures. */ 14958 if (!gfc_resolve_finalizers (sym, NULL)) 14959 return false; 14960 14961 if (sym->attr.is_class && sym->ts.u.derived == NULL) 14962 { 14963 /* Fix up incomplete CLASS symbols. */ 14964 gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL); 14965 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL); 14966 14967 /* Nothing more to do for unlimited polymorphic entities. */ 14968 if (data->ts.u.derived->attr.unlimited_polymorphic) 14969 return true; 14970 else if (vptr->ts.u.derived == NULL) 14971 { 14972 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived); 14973 gcc_assert (vtab); 14974 vptr->ts.u.derived = vtab->ts.u.derived; 14975 if (!resolve_fl_derived0 (vptr->ts.u.derived)) 14976 return false; 14977 } 14978 } 14979 14980 if (!resolve_fl_derived0 (sym)) 14981 return false; 14982 14983 /* Resolve the type-bound procedures. */ 14984 if (!resolve_typebound_procedures (sym)) 14985 return false; 14986 14987 /* Generate module vtables subject to their accessibility and their not 14988 being vtables or pdt templates. If this is not done class declarations 14989 in external procedures wind up with their own version and so SELECT TYPE 14990 fails because the vptrs do not have the same address. */ 14991 if (gfc_option.allow_std & GFC_STD_F2003 14992 && sym->ns->proc_name 14993 && sym->ns->proc_name->attr.flavor == FL_MODULE 14994 && sym->attr.access != ACCESS_PRIVATE 14995 && !(sym->attr.use_assoc || sym->attr.vtype || sym->attr.pdt_template)) 14996 { 14997 gfc_symbol *vtab = gfc_find_derived_vtab (sym); 14998 gfc_set_sym_referenced (vtab); 14999 } 15000 15001 return true; 15002 } 15003 15004 15005 static bool 15006 resolve_fl_namelist (gfc_symbol *sym) 15007 { 15008 gfc_namelist *nl; 15009 gfc_symbol *nlsym; 15010 15011 for (nl = sym->namelist; nl; nl = nl->next) 15012 { 15013 /* Check again, the check in match only works if NAMELIST comes 15014 after the decl. */ 15015 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE) 15016 { 15017 gfc_error ("Assumed size array %qs in namelist %qs at %L is not " 15018 "allowed", nl->sym->name, sym->name, &sym->declared_at); 15019 return false; 15020 } 15021 15022 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE 15023 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs " 15024 "with assumed shape in namelist %qs at %L", 15025 nl->sym->name, sym->name, &sym->declared_at)) 15026 return false; 15027 15028 if (is_non_constant_shape_array (nl->sym) 15029 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs " 15030 "with nonconstant shape in namelist %qs at %L", 15031 nl->sym->name, sym->name, &sym->declared_at)) 15032 return false; 15033 15034 if (nl->sym->ts.type == BT_CHARACTER 15035 && (nl->sym->ts.u.cl->length == NULL 15036 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length)) 15037 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with " 15038 "nonconstant character length in " 15039 "namelist %qs at %L", nl->sym->name, 15040 sym->name, &sym->declared_at)) 15041 return false; 15042 15043 } 15044 15045 /* Reject PRIVATE objects in a PUBLIC namelist. */ 15046 if (gfc_check_symbol_access (sym)) 15047 { 15048 for (nl = sym->namelist; nl; nl = nl->next) 15049 { 15050 if (!nl->sym->attr.use_assoc 15051 && !is_sym_host_assoc (nl->sym, sym->ns) 15052 && !gfc_check_symbol_access (nl->sym)) 15053 { 15054 gfc_error ("NAMELIST object %qs was declared PRIVATE and " 15055 "cannot be member of PUBLIC namelist %qs at %L", 15056 nl->sym->name, sym->name, &sym->declared_at); 15057 return false; 15058 } 15059 15060 if (nl->sym->ts.type == BT_DERIVED 15061 && (nl->sym->ts.u.derived->attr.alloc_comp 15062 || nl->sym->ts.u.derived->attr.pointer_comp)) 15063 { 15064 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in " 15065 "namelist %qs at %L with ALLOCATABLE " 15066 "or POINTER components", nl->sym->name, 15067 sym->name, &sym->declared_at)) 15068 return false; 15069 return true; 15070 } 15071 15072 /* Types with private components that came here by USE-association. */ 15073 if (nl->sym->ts.type == BT_DERIVED 15074 && derived_inaccessible (nl->sym->ts.u.derived)) 15075 { 15076 gfc_error ("NAMELIST object %qs has use-associated PRIVATE " 15077 "components and cannot be member of namelist %qs at %L", 15078 nl->sym->name, sym->name, &sym->declared_at); 15079 return false; 15080 } 15081 15082 /* Types with private components that are defined in the same module. */ 15083 if (nl->sym->ts.type == BT_DERIVED 15084 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns) 15085 && nl->sym->ts.u.derived->attr.private_comp) 15086 { 15087 gfc_error ("NAMELIST object %qs has PRIVATE components and " 15088 "cannot be a member of PUBLIC namelist %qs at %L", 15089 nl->sym->name, sym->name, &sym->declared_at); 15090 return false; 15091 } 15092 } 15093 } 15094 15095 15096 /* 14.1.2 A module or internal procedure represent local entities 15097 of the same type as a namelist member and so are not allowed. */ 15098 for (nl = sym->namelist; nl; nl = nl->next) 15099 { 15100 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE) 15101 continue; 15102 15103 if (nl->sym->attr.function && nl->sym == nl->sym->result) 15104 if ((nl->sym == sym->ns->proc_name) 15105 || 15106 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name)) 15107 continue; 15108 15109 nlsym = NULL; 15110 if (nl->sym->name) 15111 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym); 15112 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE) 15113 { 15114 gfc_error ("PROCEDURE attribute conflicts with NAMELIST " 15115 "attribute in %qs at %L", nlsym->name, 15116 &sym->declared_at); 15117 return false; 15118 } 15119 } 15120 15121 return true; 15122 } 15123 15124 15125 static bool 15126 resolve_fl_parameter (gfc_symbol *sym) 15127 { 15128 /* A parameter array's shape needs to be constant. */ 15129 if (sym->as != NULL 15130 && (sym->as->type == AS_DEFERRED 15131 || is_non_constant_shape_array (sym))) 15132 { 15133 gfc_error ("Parameter array %qs at %L cannot be automatic " 15134 "or of deferred shape", sym->name, &sym->declared_at); 15135 return false; 15136 } 15137 15138 /* Constraints on deferred type parameter. */ 15139 if (!deferred_requirements (sym)) 15140 return false; 15141 15142 /* Make sure a parameter that has been implicitly typed still 15143 matches the implicit type, since PARAMETER statements can precede 15144 IMPLICIT statements. */ 15145 if (sym->attr.implicit_type 15146 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name, 15147 sym->ns))) 15148 { 15149 gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a " 15150 "later IMPLICIT type", sym->name, &sym->declared_at); 15151 return false; 15152 } 15153 15154 /* Make sure the types of derived parameters are consistent. This 15155 type checking is deferred until resolution because the type may 15156 refer to a derived type from the host. */ 15157 if (sym->ts.type == BT_DERIVED 15158 && !gfc_compare_types (&sym->ts, &sym->value->ts)) 15159 { 15160 gfc_error ("Incompatible derived type in PARAMETER at %L", 15161 &sym->value->where); 15162 return false; 15163 } 15164 15165 /* F03:C509,C514. */ 15166 if (sym->ts.type == BT_CLASS) 15167 { 15168 gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute", 15169 sym->name, &sym->declared_at); 15170 return false; 15171 } 15172 15173 return true; 15174 } 15175 15176 15177 /* Called by resolve_symbol to check PDTs. */ 15178 15179 static void 15180 resolve_pdt (gfc_symbol* sym) 15181 { 15182 gfc_symbol *derived = NULL; 15183 gfc_actual_arglist *param; 15184 gfc_component *c; 15185 bool const_len_exprs = true; 15186 bool assumed_len_exprs = false; 15187 symbol_attribute *attr; 15188 15189 if (sym->ts.type == BT_DERIVED) 15190 { 15191 derived = sym->ts.u.derived; 15192 attr = &(sym->attr); 15193 } 15194 else if (sym->ts.type == BT_CLASS) 15195 { 15196 derived = CLASS_DATA (sym)->ts.u.derived; 15197 attr = &(CLASS_DATA (sym)->attr); 15198 } 15199 else 15200 gcc_unreachable (); 15201 15202 gcc_assert (derived->attr.pdt_type); 15203 15204 for (param = sym->param_list; param; param = param->next) 15205 { 15206 c = gfc_find_component (derived, param->name, false, true, NULL); 15207 gcc_assert (c); 15208 if (c->attr.pdt_kind) 15209 continue; 15210 15211 if (param->expr && !gfc_is_constant_expr (param->expr) 15212 && c->attr.pdt_len) 15213 const_len_exprs = false; 15214 else if (param->spec_type == SPEC_ASSUMED) 15215 assumed_len_exprs = true; 15216 15217 if (param->spec_type == SPEC_DEFERRED 15218 && !attr->allocatable && !attr->pointer) 15219 gfc_error ("The object %qs at %L has a deferred LEN " 15220 "parameter %qs and is neither allocatable " 15221 "nor a pointer", sym->name, &sym->declared_at, 15222 param->name); 15223 15224 } 15225 15226 if (!const_len_exprs 15227 && (sym->ns->proc_name->attr.is_main_program 15228 || sym->ns->proc_name->attr.flavor == FL_MODULE 15229 || sym->attr.save != SAVE_NONE)) 15230 gfc_error ("The AUTOMATIC object %qs at %L must not have the " 15231 "SAVE attribute or be a variable declared in the " 15232 "main program, a module or a submodule(F08/C513)", 15233 sym->name, &sym->declared_at); 15234 15235 if (assumed_len_exprs && !(sym->attr.dummy 15236 || sym->attr.select_type_temporary || sym->attr.associate_var)) 15237 gfc_error ("The object %qs at %L with ASSUMED type parameters " 15238 "must be a dummy or a SELECT TYPE selector(F08/4.2)", 15239 sym->name, &sym->declared_at); 15240 } 15241 15242 15243 /* Do anything necessary to resolve a symbol. Right now, we just 15244 assume that an otherwise unknown symbol is a variable. This sort 15245 of thing commonly happens for symbols in module. */ 15246 15247 static void 15248 resolve_symbol (gfc_symbol *sym) 15249 { 15250 int check_constant, mp_flag; 15251 gfc_symtree *symtree; 15252 gfc_symtree *this_symtree; 15253 gfc_namespace *ns; 15254 gfc_component *c; 15255 symbol_attribute class_attr; 15256 gfc_array_spec *as; 15257 bool saved_specification_expr; 15258 15259 if (sym->resolve_symbol_called >= 1) 15260 return; 15261 sym->resolve_symbol_called = 1; 15262 15263 /* No symbol will ever have union type; only components can be unions. 15264 Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION 15265 (just like derived type declaration symbols have flavor FL_DERIVED). */ 15266 gcc_assert (sym->ts.type != BT_UNION); 15267 15268 /* Coarrayed polymorphic objects with allocatable or pointer components are 15269 yet unsupported for -fcoarray=lib. */ 15270 if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS 15271 && sym->ts.u.derived && CLASS_DATA (sym) 15272 && CLASS_DATA (sym)->attr.codimension 15273 && CLASS_DATA (sym)->ts.u.derived 15274 && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp 15275 || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp)) 15276 { 15277 gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) " 15278 "type coarrays at %L are unsupported", &sym->declared_at); 15279 return; 15280 } 15281 15282 if (sym->attr.artificial) 15283 return; 15284 15285 if (sym->attr.unlimited_polymorphic) 15286 return; 15287 15288 if (sym->attr.flavor == FL_UNKNOWN 15289 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic 15290 && !sym->attr.generic && !sym->attr.external 15291 && sym->attr.if_source == IFSRC_UNKNOWN 15292 && sym->ts.type == BT_UNKNOWN)) 15293 { 15294 15295 /* If we find that a flavorless symbol is an interface in one of the 15296 parent namespaces, find its symtree in this namespace, free the 15297 symbol and set the symtree to point to the interface symbol. */ 15298 for (ns = gfc_current_ns->parent; ns; ns = ns->parent) 15299 { 15300 symtree = gfc_find_symtree (ns->sym_root, sym->name); 15301 if (symtree && (symtree->n.sym->generic || 15302 (symtree->n.sym->attr.flavor == FL_PROCEDURE 15303 && sym->ns->construct_entities))) 15304 { 15305 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root, 15306 sym->name); 15307 if (this_symtree->n.sym == sym) 15308 { 15309 symtree->n.sym->refs++; 15310 gfc_release_symbol (sym); 15311 this_symtree->n.sym = symtree->n.sym; 15312 return; 15313 } 15314 } 15315 } 15316 15317 /* Otherwise give it a flavor according to such attributes as 15318 it has. */ 15319 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0 15320 && sym->attr.intrinsic == 0) 15321 sym->attr.flavor = FL_VARIABLE; 15322 else if (sym->attr.flavor == FL_UNKNOWN) 15323 { 15324 sym->attr.flavor = FL_PROCEDURE; 15325 if (sym->attr.dimension) 15326 sym->attr.function = 1; 15327 } 15328 } 15329 15330 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function) 15331 gfc_add_function (&sym->attr, sym->name, &sym->declared_at); 15332 15333 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL 15334 && !resolve_procedure_interface (sym)) 15335 return; 15336 15337 if (sym->attr.is_protected && !sym->attr.proc_pointer 15338 && (sym->attr.procedure || sym->attr.external)) 15339 { 15340 if (sym->attr.external) 15341 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute " 15342 "at %L", &sym->declared_at); 15343 else 15344 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute " 15345 "at %L", &sym->declared_at); 15346 15347 return; 15348 } 15349 15350 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym)) 15351 return; 15352 15353 else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION) 15354 && !resolve_fl_struct (sym)) 15355 return; 15356 15357 /* Symbols that are module procedures with results (functions) have 15358 the types and array specification copied for type checking in 15359 procedures that call them, as well as for saving to a module 15360 file. These symbols can't stand the scrutiny that their results 15361 can. */ 15362 mp_flag = (sym->result != NULL && sym->result != sym); 15363 15364 /* Make sure that the intrinsic is consistent with its internal 15365 representation. This needs to be done before assigning a default 15366 type to avoid spurious warnings. */ 15367 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic 15368 && !gfc_resolve_intrinsic (sym, &sym->declared_at)) 15369 return; 15370 15371 /* Resolve associate names. */ 15372 if (sym->assoc) 15373 resolve_assoc_var (sym, true); 15374 15375 /* Assign default type to symbols that need one and don't have one. */ 15376 if (sym->ts.type == BT_UNKNOWN) 15377 { 15378 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER) 15379 { 15380 gfc_set_default_type (sym, 1, NULL); 15381 } 15382 15383 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external 15384 && !sym->attr.function && !sym->attr.subroutine 15385 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN) 15386 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at); 15387 15388 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function) 15389 { 15390 /* The specific case of an external procedure should emit an error 15391 in the case that there is no implicit type. */ 15392 if (!mp_flag) 15393 { 15394 if (!sym->attr.mixed_entry_master) 15395 gfc_set_default_type (sym, sym->attr.external, NULL); 15396 } 15397 else 15398 { 15399 /* Result may be in another namespace. */ 15400 resolve_symbol (sym->result); 15401 15402 if (!sym->result->attr.proc_pointer) 15403 { 15404 sym->ts = sym->result->ts; 15405 sym->as = gfc_copy_array_spec (sym->result->as); 15406 sym->attr.dimension = sym->result->attr.dimension; 15407 sym->attr.pointer = sym->result->attr.pointer; 15408 sym->attr.allocatable = sym->result->attr.allocatable; 15409 sym->attr.contiguous = sym->result->attr.contiguous; 15410 } 15411 } 15412 } 15413 } 15414 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function) 15415 { 15416 bool saved_specification_expr = specification_expr; 15417 specification_expr = true; 15418 gfc_resolve_array_spec (sym->result->as, false); 15419 specification_expr = saved_specification_expr; 15420 } 15421 15422 if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived) 15423 { 15424 as = CLASS_DATA (sym)->as; 15425 class_attr = CLASS_DATA (sym)->attr; 15426 class_attr.pointer = class_attr.class_pointer; 15427 } 15428 else 15429 { 15430 class_attr = sym->attr; 15431 as = sym->as; 15432 } 15433 15434 /* F2008, C530. */ 15435 if (sym->attr.contiguous 15436 && (!class_attr.dimension 15437 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK 15438 && !class_attr.pointer))) 15439 { 15440 gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an " 15441 "array pointer or an assumed-shape or assumed-rank array", 15442 sym->name, &sym->declared_at); 15443 return; 15444 } 15445 15446 /* Assumed size arrays and assumed shape arrays must be dummy 15447 arguments. Array-spec's of implied-shape should have been resolved to 15448 AS_EXPLICIT already. */ 15449 15450 if (as) 15451 { 15452 /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad 15453 specification expression. */ 15454 if (as->type == AS_IMPLIED_SHAPE) 15455 { 15456 int i; 15457 for (i=0; i<as->rank; i++) 15458 { 15459 if (as->lower[i] != NULL && as->upper[i] == NULL) 15460 { 15461 gfc_error ("Bad specification for assumed size array at %L", 15462 &as->lower[i]->where); 15463 return; 15464 } 15465 } 15466 gcc_unreachable(); 15467 } 15468 15469 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed) 15470 || as->type == AS_ASSUMED_SHAPE) 15471 && !sym->attr.dummy && !sym->attr.select_type_temporary) 15472 { 15473 if (as->type == AS_ASSUMED_SIZE) 15474 gfc_error ("Assumed size array at %L must be a dummy argument", 15475 &sym->declared_at); 15476 else 15477 gfc_error ("Assumed shape array at %L must be a dummy argument", 15478 &sym->declared_at); 15479 return; 15480 } 15481 /* TS 29113, C535a. */ 15482 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy 15483 && !sym->attr.select_type_temporary 15484 && !(cs_base && cs_base->current 15485 && cs_base->current->op == EXEC_SELECT_RANK)) 15486 { 15487 gfc_error ("Assumed-rank array at %L must be a dummy argument", 15488 &sym->declared_at); 15489 return; 15490 } 15491 if (as->type == AS_ASSUMED_RANK 15492 && (sym->attr.codimension || sym->attr.value)) 15493 { 15494 gfc_error ("Assumed-rank array at %L may not have the VALUE or " 15495 "CODIMENSION attribute", &sym->declared_at); 15496 return; 15497 } 15498 } 15499 15500 /* Make sure symbols with known intent or optional are really dummy 15501 variable. Because of ENTRY statement, this has to be deferred 15502 until resolution time. */ 15503 15504 if (!sym->attr.dummy 15505 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN)) 15506 { 15507 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at); 15508 return; 15509 } 15510 15511 if (sym->attr.value && !sym->attr.dummy) 15512 { 15513 gfc_error ("%qs at %L cannot have the VALUE attribute because " 15514 "it is not a dummy argument", sym->name, &sym->declared_at); 15515 return; 15516 } 15517 15518 if (sym->attr.value && sym->ts.type == BT_CHARACTER) 15519 { 15520 gfc_charlen *cl = sym->ts.u.cl; 15521 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT) 15522 { 15523 gfc_error ("Character dummy variable %qs at %L with VALUE " 15524 "attribute must have constant length", 15525 sym->name, &sym->declared_at); 15526 return; 15527 } 15528 15529 if (sym->ts.is_c_interop 15530 && mpz_cmp_si (cl->length->value.integer, 1) != 0) 15531 { 15532 gfc_error ("C interoperable character dummy variable %qs at %L " 15533 "with VALUE attribute must have length one", 15534 sym->name, &sym->declared_at); 15535 return; 15536 } 15537 } 15538 15539 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c 15540 && sym->ts.u.derived->attr.generic) 15541 { 15542 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived); 15543 if (!sym->ts.u.derived) 15544 { 15545 gfc_error ("The derived type %qs at %L is of type %qs, " 15546 "which has not been defined", sym->name, 15547 &sym->declared_at, sym->ts.u.derived->name); 15548 sym->ts.type = BT_UNKNOWN; 15549 return; 15550 } 15551 } 15552 15553 /* Use the same constraints as TYPE(*), except for the type check 15554 and that only scalars and assumed-size arrays are permitted. */ 15555 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) 15556 { 15557 if (!sym->attr.dummy) 15558 { 15559 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be " 15560 "a dummy argument", sym->name, &sym->declared_at); 15561 return; 15562 } 15563 15564 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER 15565 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL 15566 && sym->ts.type != BT_COMPLEX) 15567 { 15568 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be " 15569 "of type TYPE(*) or of an numeric intrinsic type", 15570 sym->name, &sym->declared_at); 15571 return; 15572 } 15573 15574 if (sym->attr.allocatable || sym->attr.codimension 15575 || sym->attr.pointer || sym->attr.value) 15576 { 15577 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not " 15578 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE " 15579 "attribute", sym->name, &sym->declared_at); 15580 return; 15581 } 15582 15583 if (sym->attr.intent == INTENT_OUT) 15584 { 15585 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not " 15586 "have the INTENT(OUT) attribute", 15587 sym->name, &sym->declared_at); 15588 return; 15589 } 15590 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE) 15591 { 15592 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall " 15593 "either be a scalar or an assumed-size array", 15594 sym->name, &sym->declared_at); 15595 return; 15596 } 15597 15598 /* Set the type to TYPE(*) and add a dimension(*) to ensure 15599 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with 15600 packing. */ 15601 sym->ts.type = BT_ASSUMED; 15602 sym->as = gfc_get_array_spec (); 15603 sym->as->type = AS_ASSUMED_SIZE; 15604 sym->as->rank = 1; 15605 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1); 15606 } 15607 else if (sym->ts.type == BT_ASSUMED) 15608 { 15609 /* TS 29113, C407a. */ 15610 if (!sym->attr.dummy) 15611 { 15612 gfc_error ("Assumed type of variable %s at %L is only permitted " 15613 "for dummy variables", sym->name, &sym->declared_at); 15614 return; 15615 } 15616 if (sym->attr.allocatable || sym->attr.codimension 15617 || sym->attr.pointer || sym->attr.value) 15618 { 15619 gfc_error ("Assumed-type variable %s at %L may not have the " 15620 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute", 15621 sym->name, &sym->declared_at); 15622 return; 15623 } 15624 if (sym->attr.intent == INTENT_OUT) 15625 { 15626 gfc_error ("Assumed-type variable %s at %L may not have the " 15627 "INTENT(OUT) attribute", 15628 sym->name, &sym->declared_at); 15629 return; 15630 } 15631 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT) 15632 { 15633 gfc_error ("Assumed-type variable %s at %L shall not be an " 15634 "explicit-shape array", sym->name, &sym->declared_at); 15635 return; 15636 } 15637 } 15638 15639 /* If the symbol is marked as bind(c), that it is declared at module level 15640 scope and verify its type and kind. Do not do the latter for symbols 15641 that are implicitly typed because that is handled in 15642 gfc_set_default_type. Handle dummy arguments and procedure definitions 15643 separately. Also, anything that is use associated is not handled here 15644 but instead is handled in the module it is declared in. Finally, derived 15645 type definitions are allowed to be BIND(C) since that only implies that 15646 they're interoperable, and they are checked fully for interoperability 15647 when a variable is declared of that type. */ 15648 if (sym->attr.is_bind_c && sym->attr.use_assoc == 0 15649 && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE 15650 && sym->attr.flavor != FL_DERIVED) 15651 { 15652 bool t = true; 15653 15654 /* First, make sure the variable is declared at the 15655 module-level scope (J3/04-007, Section 15.3). */ 15656 if (sym->ns->proc_name->attr.flavor != FL_MODULE && 15657 sym->attr.in_common == 0) 15658 { 15659 gfc_error ("Variable %qs at %L cannot be BIND(C) because it " 15660 "is neither a COMMON block nor declared at the " 15661 "module level scope", sym->name, &(sym->declared_at)); 15662 t = false; 15663 } 15664 else if (sym->ts.type == BT_CHARACTER 15665 && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL 15666 || !gfc_is_constant_expr (sym->ts.u.cl->length) 15667 || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0)) 15668 { 15669 gfc_error ("BIND(C) Variable %qs at %L must have length one", 15670 sym->name, &sym->declared_at); 15671 t = false; 15672 } 15673 else if (sym->common_head != NULL && sym->attr.implicit_type == 0) 15674 { 15675 t = verify_com_block_vars_c_interop (sym->common_head); 15676 } 15677 else if (sym->attr.implicit_type == 0) 15678 { 15679 /* If type() declaration, we need to verify that the components 15680 of the given type are all C interoperable, etc. */ 15681 if (sym->ts.type == BT_DERIVED && 15682 sym->ts.u.derived->attr.is_c_interop != 1) 15683 { 15684 /* Make sure the user marked the derived type as BIND(C). If 15685 not, call the verify routine. This could print an error 15686 for the derived type more than once if multiple variables 15687 of that type are declared. */ 15688 if (sym->ts.u.derived->attr.is_bind_c != 1) 15689 verify_bind_c_derived_type (sym->ts.u.derived); 15690 t = false; 15691 } 15692 15693 /* Verify the variable itself as C interoperable if it 15694 is BIND(C). It is not possible for this to succeed if 15695 the verify_bind_c_derived_type failed, so don't have to handle 15696 any error returned by verify_bind_c_derived_type. */ 15697 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common, 15698 sym->common_block); 15699 } 15700 15701 if (!t) 15702 { 15703 /* clear the is_bind_c flag to prevent reporting errors more than 15704 once if something failed. */ 15705 sym->attr.is_bind_c = 0; 15706 return; 15707 } 15708 } 15709 15710 /* If a derived type symbol has reached this point, without its 15711 type being declared, we have an error. Notice that most 15712 conditions that produce undefined derived types have already 15713 been dealt with. However, the likes of: 15714 implicit type(t) (t) ..... call foo (t) will get us here if 15715 the type is not declared in the scope of the implicit 15716 statement. Change the type to BT_UNKNOWN, both because it is so 15717 and to prevent an ICE. */ 15718 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c 15719 && sym->ts.u.derived->components == NULL 15720 && !sym->ts.u.derived->attr.zero_comp) 15721 { 15722 gfc_error ("The derived type %qs at %L is of type %qs, " 15723 "which has not been defined", sym->name, 15724 &sym->declared_at, sym->ts.u.derived->name); 15725 sym->ts.type = BT_UNKNOWN; 15726 return; 15727 } 15728 15729 /* Make sure that the derived type has been resolved and that the 15730 derived type is visible in the symbol's namespace, if it is a 15731 module function and is not PRIVATE. */ 15732 if (sym->ts.type == BT_DERIVED 15733 && sym->ts.u.derived->attr.use_assoc 15734 && sym->ns->proc_name 15735 && sym->ns->proc_name->attr.flavor == FL_MODULE 15736 && !resolve_fl_derived (sym->ts.u.derived)) 15737 return; 15738 15739 /* Unless the derived-type declaration is use associated, Fortran 95 15740 does not allow public entries of private derived types. 15741 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation 15742 161 in 95-006r3. */ 15743 if (sym->ts.type == BT_DERIVED 15744 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE 15745 && !sym->ts.u.derived->attr.use_assoc 15746 && gfc_check_symbol_access (sym) 15747 && !gfc_check_symbol_access (sym->ts.u.derived) 15748 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE " 15749 "derived type %qs", 15750 (sym->attr.flavor == FL_PARAMETER) 15751 ? "parameter" : "variable", 15752 sym->name, &sym->declared_at, 15753 sym->ts.u.derived->name)) 15754 return; 15755 15756 /* F2008, C1302. */ 15757 if (sym->ts.type == BT_DERIVED 15758 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV 15759 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE) 15760 || sym->ts.u.derived->attr.lock_comp) 15761 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp) 15762 { 15763 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of " 15764 "type LOCK_TYPE must be a coarray", sym->name, 15765 &sym->declared_at); 15766 return; 15767 } 15768 15769 /* TS18508, C702/C703. */ 15770 if (sym->ts.type == BT_DERIVED 15771 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV 15772 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE) 15773 || sym->ts.u.derived->attr.event_comp) 15774 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp) 15775 { 15776 gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of " 15777 "type EVENT_TYPE must be a coarray", sym->name, 15778 &sym->declared_at); 15779 return; 15780 } 15781 15782 /* An assumed-size array with INTENT(OUT) shall not be of a type for which 15783 default initialization is defined (5.1.2.4.4). */ 15784 if (sym->ts.type == BT_DERIVED 15785 && sym->attr.dummy 15786 && sym->attr.intent == INTENT_OUT 15787 && sym->as 15788 && sym->as->type == AS_ASSUMED_SIZE) 15789 { 15790 for (c = sym->ts.u.derived->components; c; c = c->next) 15791 { 15792 if (c->initializer) 15793 { 15794 gfc_error ("The INTENT(OUT) dummy argument %qs at %L is " 15795 "ASSUMED SIZE and so cannot have a default initializer", 15796 sym->name, &sym->declared_at); 15797 return; 15798 } 15799 } 15800 } 15801 15802 /* F2008, C542. */ 15803 if (sym->ts.type == BT_DERIVED && sym->attr.dummy 15804 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp) 15805 { 15806 gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be " 15807 "INTENT(OUT)", sym->name, &sym->declared_at); 15808 return; 15809 } 15810 15811 /* TS18508. */ 15812 if (sym->ts.type == BT_DERIVED && sym->attr.dummy 15813 && sym->attr.intent == INTENT_OUT && sym->attr.event_comp) 15814 { 15815 gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be " 15816 "INTENT(OUT)", sym->name, &sym->declared_at); 15817 return; 15818 } 15819 15820 /* F2008, C525. */ 15821 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp) 15822 || (sym->ts.type == BT_CLASS && sym->attr.class_ok 15823 && sym->ts.u.derived && CLASS_DATA (sym) 15824 && CLASS_DATA (sym)->attr.coarray_comp)) 15825 || class_attr.codimension) 15826 && (sym->attr.result || sym->result == sym)) 15827 { 15828 gfc_error ("Function result %qs at %L shall not be a coarray or have " 15829 "a coarray component", sym->name, &sym->declared_at); 15830 return; 15831 } 15832 15833 /* F2008, C524. */ 15834 if (sym->attr.codimension && sym->ts.type == BT_DERIVED 15835 && sym->ts.u.derived->ts.is_iso_c) 15836 { 15837 gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) " 15838 "shall not be a coarray", sym->name, &sym->declared_at); 15839 return; 15840 } 15841 15842 /* F2008, C525. */ 15843 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp) 15844 || (sym->ts.type == BT_CLASS && sym->attr.class_ok 15845 && sym->ts.u.derived && CLASS_DATA (sym) 15846 && CLASS_DATA (sym)->attr.coarray_comp)) 15847 && (class_attr.codimension || class_attr.pointer || class_attr.dimension 15848 || class_attr.allocatable)) 15849 { 15850 gfc_error ("Variable %qs at %L with coarray component shall be a " 15851 "nonpointer, nonallocatable scalar, which is not a coarray", 15852 sym->name, &sym->declared_at); 15853 return; 15854 } 15855 15856 /* F2008, C526. The function-result case was handled above. */ 15857 if (class_attr.codimension 15858 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save 15859 || sym->attr.select_type_temporary 15860 || sym->attr.associate_var 15861 || (sym->ns->save_all && !sym->attr.automatic) 15862 || sym->ns->proc_name->attr.flavor == FL_MODULE 15863 || sym->ns->proc_name->attr.is_main_program 15864 || sym->attr.function || sym->attr.result || sym->attr.use_assoc)) 15865 { 15866 gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE " 15867 "nor a dummy argument", sym->name, &sym->declared_at); 15868 return; 15869 } 15870 /* F2008, C528. */ 15871 else if (class_attr.codimension && !sym->attr.select_type_temporary 15872 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED) 15873 { 15874 gfc_error ("Coarray variable %qs at %L shall not have codimensions with " 15875 "deferred shape", sym->name, &sym->declared_at); 15876 return; 15877 } 15878 else if (class_attr.codimension && class_attr.allocatable && as 15879 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED)) 15880 { 15881 gfc_error ("Allocatable coarray variable %qs at %L must have " 15882 "deferred shape", sym->name, &sym->declared_at); 15883 return; 15884 } 15885 15886 /* F2008, C541. */ 15887 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp) 15888 || (sym->ts.type == BT_CLASS && sym->attr.class_ok 15889 && sym->ts.u.derived && CLASS_DATA (sym) 15890 && CLASS_DATA (sym)->attr.coarray_comp)) 15891 || (class_attr.codimension && class_attr.allocatable)) 15892 && sym->attr.dummy && sym->attr.intent == INTENT_OUT) 15893 { 15894 gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an " 15895 "allocatable coarray or have coarray components", 15896 sym->name, &sym->declared_at); 15897 return; 15898 } 15899 15900 if (class_attr.codimension && sym->attr.dummy 15901 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c) 15902 { 15903 gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) " 15904 "procedure %qs", sym->name, &sym->declared_at, 15905 sym->ns->proc_name->name); 15906 return; 15907 } 15908 15909 if (sym->ts.type == BT_LOGICAL 15910 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym) 15911 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name 15912 && sym->ns->proc_name->attr.is_bind_c))) 15913 { 15914 int i; 15915 for (i = 0; gfc_logical_kinds[i].kind; i++) 15916 if (gfc_logical_kinds[i].kind == sym->ts.kind) 15917 break; 15918 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy 15919 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at " 15920 "%L with non-C_Bool kind in BIND(C) procedure " 15921 "%qs", sym->name, &sym->declared_at, 15922 sym->ns->proc_name->name)) 15923 return; 15924 else if (!gfc_logical_kinds[i].c_bool 15925 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable " 15926 "%qs at %L with non-C_Bool kind in " 15927 "BIND(C) procedure %qs", sym->name, 15928 &sym->declared_at, 15929 sym->attr.function ? sym->name 15930 : sym->ns->proc_name->name)) 15931 return; 15932 } 15933 15934 switch (sym->attr.flavor) 15935 { 15936 case FL_VARIABLE: 15937 if (!resolve_fl_variable (sym, mp_flag)) 15938 return; 15939 break; 15940 15941 case FL_PROCEDURE: 15942 if (sym->formal && !sym->formal_ns) 15943 { 15944 /* Check that none of the arguments are a namelist. */ 15945 gfc_formal_arglist *formal = sym->formal; 15946 15947 for (; formal; formal = formal->next) 15948 if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST) 15949 { 15950 gfc_error ("Namelist %qs cannot be an argument to " 15951 "subroutine or function at %L", 15952 formal->sym->name, &sym->declared_at); 15953 return; 15954 } 15955 } 15956 15957 if (!resolve_fl_procedure (sym, mp_flag)) 15958 return; 15959 break; 15960 15961 case FL_NAMELIST: 15962 if (!resolve_fl_namelist (sym)) 15963 return; 15964 break; 15965 15966 case FL_PARAMETER: 15967 if (!resolve_fl_parameter (sym)) 15968 return; 15969 break; 15970 15971 default: 15972 break; 15973 } 15974 15975 /* Resolve array specifier. Check as well some constraints 15976 on COMMON blocks. */ 15977 15978 check_constant = sym->attr.in_common && !sym->attr.pointer; 15979 15980 /* Set the formal_arg_flag so that check_conflict will not throw 15981 an error for host associated variables in the specification 15982 expression for an array_valued function. */ 15983 if ((sym->attr.function || sym->attr.result) && sym->as) 15984 formal_arg_flag = true; 15985 15986 saved_specification_expr = specification_expr; 15987 specification_expr = true; 15988 gfc_resolve_array_spec (sym->as, check_constant); 15989 specification_expr = saved_specification_expr; 15990 15991 formal_arg_flag = false; 15992 15993 /* Resolve formal namespaces. */ 15994 if (sym->formal_ns && sym->formal_ns != gfc_current_ns 15995 && !sym->attr.contained && !sym->attr.intrinsic) 15996 gfc_resolve (sym->formal_ns); 15997 15998 /* Make sure the formal namespace is present. */ 15999 if (sym->formal && !sym->formal_ns) 16000 { 16001 gfc_formal_arglist *formal = sym->formal; 16002 while (formal && !formal->sym) 16003 formal = formal->next; 16004 16005 if (formal) 16006 { 16007 sym->formal_ns = formal->sym->ns; 16008 if (sym->formal_ns && sym->ns != formal->sym->ns) 16009 sym->formal_ns->refs++; 16010 } 16011 } 16012 16013 /* Check threadprivate restrictions. */ 16014 if (sym->attr.threadprivate && !sym->attr.save 16015 && !(sym->ns->save_all && !sym->attr.automatic) 16016 && (!sym->attr.in_common 16017 && sym->module == NULL 16018 && (sym->ns->proc_name == NULL 16019 || sym->ns->proc_name->attr.flavor != FL_MODULE))) 16020 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at); 16021 16022 /* Check omp declare target restrictions. */ 16023 if (sym->attr.omp_declare_target 16024 && sym->attr.flavor == FL_VARIABLE 16025 && !sym->attr.save 16026 && !(sym->ns->save_all && !sym->attr.automatic) 16027 && (!sym->attr.in_common 16028 && sym->module == NULL 16029 && (sym->ns->proc_name == NULL 16030 || sym->ns->proc_name->attr.flavor != FL_MODULE))) 16031 gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd", 16032 sym->name, &sym->declared_at); 16033 16034 /* If we have come this far we can apply default-initializers, as 16035 described in 14.7.5, to those variables that have not already 16036 been assigned one. */ 16037 if (sym->ts.type == BT_DERIVED 16038 && !sym->value 16039 && !sym->attr.allocatable 16040 && !sym->attr.alloc_comp) 16041 { 16042 symbol_attribute *a = &sym->attr; 16043 16044 if ((!a->save && !a->dummy && !a->pointer 16045 && !a->in_common && !a->use_assoc 16046 && a->referenced 16047 && !((a->function || a->result) 16048 && (!a->dimension 16049 || sym->ts.u.derived->attr.alloc_comp 16050 || sym->ts.u.derived->attr.pointer_comp)) 16051 && !(a->function && sym != sym->result)) 16052 || (a->dummy && a->intent == INTENT_OUT && !a->pointer)) 16053 apply_default_init (sym); 16054 else if (a->function && sym->result && a->access != ACCESS_PRIVATE 16055 && (sym->ts.u.derived->attr.alloc_comp 16056 || sym->ts.u.derived->attr.pointer_comp)) 16057 /* Mark the result symbol to be referenced, when it has allocatable 16058 components. */ 16059 sym->result->attr.referenced = 1; 16060 } 16061 16062 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns 16063 && sym->attr.dummy && sym->attr.intent == INTENT_OUT 16064 && !CLASS_DATA (sym)->attr.class_pointer 16065 && !CLASS_DATA (sym)->attr.allocatable) 16066 apply_default_init (sym); 16067 16068 /* If this symbol has a type-spec, check it. */ 16069 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER 16070 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)) 16071 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name)) 16072 return; 16073 16074 if (sym->param_list) 16075 resolve_pdt (sym); 16076 } 16077 16078 16079 /************* Resolve DATA statements *************/ 16080 16081 static struct 16082 { 16083 gfc_data_value *vnode; 16084 mpz_t left; 16085 } 16086 values; 16087 16088 16089 /* Advance the values structure to point to the next value in the data list. */ 16090 16091 static bool 16092 next_data_value (void) 16093 { 16094 while (mpz_cmp_ui (values.left, 0) == 0) 16095 { 16096 16097 if (values.vnode->next == NULL) 16098 return false; 16099 16100 values.vnode = values.vnode->next; 16101 mpz_set (values.left, values.vnode->repeat); 16102 } 16103 16104 return true; 16105 } 16106 16107 16108 static bool 16109 check_data_variable (gfc_data_variable *var, locus *where) 16110 { 16111 gfc_expr *e; 16112 mpz_t size; 16113 mpz_t offset; 16114 bool t; 16115 ar_type mark = AR_UNKNOWN; 16116 int i; 16117 mpz_t section_index[GFC_MAX_DIMENSIONS]; 16118 gfc_ref *ref; 16119 gfc_array_ref *ar; 16120 gfc_symbol *sym; 16121 int has_pointer; 16122 16123 if (!gfc_resolve_expr (var->expr)) 16124 return false; 16125 16126 ar = NULL; 16127 mpz_init_set_si (offset, 0); 16128 e = var->expr; 16129 16130 if (e->expr_type == EXPR_FUNCTION && e->value.function.isym 16131 && e->value.function.isym->id == GFC_ISYM_CAF_GET) 16132 e = e->value.function.actual->expr; 16133 16134 if (e->expr_type != EXPR_VARIABLE) 16135 { 16136 gfc_error ("Expecting definable entity near %L", where); 16137 return false; 16138 } 16139 16140 sym = e->symtree->n.sym; 16141 16142 if (sym->ns->is_block_data && !sym->attr.in_common) 16143 { 16144 gfc_error ("BLOCK DATA element %qs at %L must be in COMMON", 16145 sym->name, &sym->declared_at); 16146 return false; 16147 } 16148 16149 if (e->ref == NULL && sym->as) 16150 { 16151 gfc_error ("DATA array %qs at %L must be specified in a previous" 16152 " declaration", sym->name, where); 16153 return false; 16154 } 16155 16156 if (gfc_is_coindexed (e)) 16157 { 16158 gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name, 16159 where); 16160 return false; 16161 } 16162 16163 has_pointer = sym->attr.pointer; 16164 16165 for (ref = e->ref; ref; ref = ref->next) 16166 { 16167 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer) 16168 has_pointer = 1; 16169 16170 if (has_pointer) 16171 { 16172 if (ref->type == REF_ARRAY && ref->u.ar.type != AR_FULL) 16173 { 16174 gfc_error ("DATA element %qs at %L is a pointer and so must " 16175 "be a full array", sym->name, where); 16176 return false; 16177 } 16178 16179 if (values.vnode->expr->expr_type == EXPR_CONSTANT) 16180 { 16181 gfc_error ("DATA object near %L has the pointer attribute " 16182 "and the corresponding DATA value is not a valid " 16183 "initial-data-target", where); 16184 return false; 16185 } 16186 } 16187 } 16188 16189 if (e->rank == 0 || has_pointer) 16190 { 16191 mpz_init_set_ui (size, 1); 16192 ref = NULL; 16193 } 16194 else 16195 { 16196 ref = e->ref; 16197 16198 /* Find the array section reference. */ 16199 for (ref = e->ref; ref; ref = ref->next) 16200 { 16201 if (ref->type != REF_ARRAY) 16202 continue; 16203 if (ref->u.ar.type == AR_ELEMENT) 16204 continue; 16205 break; 16206 } 16207 gcc_assert (ref); 16208 16209 /* Set marks according to the reference pattern. */ 16210 switch (ref->u.ar.type) 16211 { 16212 case AR_FULL: 16213 mark = AR_FULL; 16214 break; 16215 16216 case AR_SECTION: 16217 ar = &ref->u.ar; 16218 /* Get the start position of array section. */ 16219 gfc_get_section_index (ar, section_index, &offset); 16220 mark = AR_SECTION; 16221 break; 16222 16223 default: 16224 gcc_unreachable (); 16225 } 16226 16227 if (!gfc_array_size (e, &size)) 16228 { 16229 gfc_error ("Nonconstant array section at %L in DATA statement", 16230 where); 16231 mpz_clear (offset); 16232 return false; 16233 } 16234 } 16235 16236 t = true; 16237 16238 while (mpz_cmp_ui (size, 0) > 0) 16239 { 16240 if (!next_data_value ()) 16241 { 16242 gfc_error ("DATA statement at %L has more variables than values", 16243 where); 16244 t = false; 16245 break; 16246 } 16247 16248 t = gfc_check_assign (var->expr, values.vnode->expr, 0); 16249 if (!t) 16250 break; 16251 16252 /* If we have more than one element left in the repeat count, 16253 and we have more than one element left in the target variable, 16254 then create a range assignment. */ 16255 /* FIXME: Only done for full arrays for now, since array sections 16256 seem tricky. */ 16257 if (mark == AR_FULL && ref && ref->next == NULL 16258 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0) 16259 { 16260 mpz_t range; 16261 16262 if (mpz_cmp (size, values.left) >= 0) 16263 { 16264 mpz_init_set (range, values.left); 16265 mpz_sub (size, size, values.left); 16266 mpz_set_ui (values.left, 0); 16267 } 16268 else 16269 { 16270 mpz_init_set (range, size); 16271 mpz_sub (values.left, values.left, size); 16272 mpz_set_ui (size, 0); 16273 } 16274 16275 t = gfc_assign_data_value (var->expr, values.vnode->expr, 16276 offset, &range); 16277 16278 mpz_add (offset, offset, range); 16279 mpz_clear (range); 16280 16281 if (!t) 16282 break; 16283 } 16284 16285 /* Assign initial value to symbol. */ 16286 else 16287 { 16288 mpz_sub_ui (values.left, values.left, 1); 16289 mpz_sub_ui (size, size, 1); 16290 16291 t = gfc_assign_data_value (var->expr, values.vnode->expr, 16292 offset, NULL); 16293 if (!t) 16294 break; 16295 16296 if (mark == AR_FULL) 16297 mpz_add_ui (offset, offset, 1); 16298 16299 /* Modify the array section indexes and recalculate the offset 16300 for next element. */ 16301 else if (mark == AR_SECTION) 16302 gfc_advance_section (section_index, ar, &offset); 16303 } 16304 } 16305 16306 if (mark == AR_SECTION) 16307 { 16308 for (i = 0; i < ar->dimen; i++) 16309 mpz_clear (section_index[i]); 16310 } 16311 16312 mpz_clear (size); 16313 mpz_clear (offset); 16314 16315 return t; 16316 } 16317 16318 16319 static bool traverse_data_var (gfc_data_variable *, locus *); 16320 16321 /* Iterate over a list of elements in a DATA statement. */ 16322 16323 static bool 16324 traverse_data_list (gfc_data_variable *var, locus *where) 16325 { 16326 mpz_t trip; 16327 iterator_stack frame; 16328 gfc_expr *e, *start, *end, *step; 16329 bool retval = true; 16330 16331 mpz_init (frame.value); 16332 mpz_init (trip); 16333 16334 start = gfc_copy_expr (var->iter.start); 16335 end = gfc_copy_expr (var->iter.end); 16336 step = gfc_copy_expr (var->iter.step); 16337 16338 if (!gfc_simplify_expr (start, 1) 16339 || start->expr_type != EXPR_CONSTANT) 16340 { 16341 gfc_error ("start of implied-do loop at %L could not be " 16342 "simplified to a constant value", &start->where); 16343 retval = false; 16344 goto cleanup; 16345 } 16346 if (!gfc_simplify_expr (end, 1) 16347 || end->expr_type != EXPR_CONSTANT) 16348 { 16349 gfc_error ("end of implied-do loop at %L could not be " 16350 "simplified to a constant value", &end->where); 16351 retval = false; 16352 goto cleanup; 16353 } 16354 if (!gfc_simplify_expr (step, 1) 16355 || step->expr_type != EXPR_CONSTANT) 16356 { 16357 gfc_error ("step of implied-do loop at %L could not be " 16358 "simplified to a constant value", &step->where); 16359 retval = false; 16360 goto cleanup; 16361 } 16362 if (mpz_cmp_si (step->value.integer, 0) == 0) 16363 { 16364 gfc_error ("step of implied-do loop at %L shall not be zero", 16365 &step->where); 16366 retval = false; 16367 goto cleanup; 16368 } 16369 16370 mpz_set (trip, end->value.integer); 16371 mpz_sub (trip, trip, start->value.integer); 16372 mpz_add (trip, trip, step->value.integer); 16373 16374 mpz_div (trip, trip, step->value.integer); 16375 16376 mpz_set (frame.value, start->value.integer); 16377 16378 frame.prev = iter_stack; 16379 frame.variable = var->iter.var->symtree; 16380 iter_stack = &frame; 16381 16382 while (mpz_cmp_ui (trip, 0) > 0) 16383 { 16384 if (!traverse_data_var (var->list, where)) 16385 { 16386 retval = false; 16387 goto cleanup; 16388 } 16389 16390 e = gfc_copy_expr (var->expr); 16391 if (!gfc_simplify_expr (e, 1)) 16392 { 16393 gfc_free_expr (e); 16394 retval = false; 16395 goto cleanup; 16396 } 16397 16398 mpz_add (frame.value, frame.value, step->value.integer); 16399 16400 mpz_sub_ui (trip, trip, 1); 16401 } 16402 16403 cleanup: 16404 mpz_clear (frame.value); 16405 mpz_clear (trip); 16406 16407 gfc_free_expr (start); 16408 gfc_free_expr (end); 16409 gfc_free_expr (step); 16410 16411 iter_stack = frame.prev; 16412 return retval; 16413 } 16414 16415 16416 /* Type resolve variables in the variable list of a DATA statement. */ 16417 16418 static bool 16419 traverse_data_var (gfc_data_variable *var, locus *where) 16420 { 16421 bool t; 16422 16423 for (; var; var = var->next) 16424 { 16425 if (var->expr == NULL) 16426 t = traverse_data_list (var, where); 16427 else 16428 t = check_data_variable (var, where); 16429 16430 if (!t) 16431 return false; 16432 } 16433 16434 return true; 16435 } 16436 16437 16438 /* Resolve the expressions and iterators associated with a data statement. 16439 This is separate from the assignment checking because data lists should 16440 only be resolved once. */ 16441 16442 static bool 16443 resolve_data_variables (gfc_data_variable *d) 16444 { 16445 for (; d; d = d->next) 16446 { 16447 if (d->list == NULL) 16448 { 16449 if (!gfc_resolve_expr (d->expr)) 16450 return false; 16451 } 16452 else 16453 { 16454 if (!gfc_resolve_iterator (&d->iter, false, true)) 16455 return false; 16456 16457 if (!resolve_data_variables (d->list)) 16458 return false; 16459 } 16460 } 16461 16462 return true; 16463 } 16464 16465 16466 /* Resolve a single DATA statement. We implement this by storing a pointer to 16467 the value list into static variables, and then recursively traversing the 16468 variables list, expanding iterators and such. */ 16469 16470 static void 16471 resolve_data (gfc_data *d) 16472 { 16473 16474 if (!resolve_data_variables (d->var)) 16475 return; 16476 16477 values.vnode = d->value; 16478 if (d->value == NULL) 16479 mpz_set_ui (values.left, 0); 16480 else 16481 mpz_set (values.left, d->value->repeat); 16482 16483 if (!traverse_data_var (d->var, &d->where)) 16484 return; 16485 16486 /* At this point, we better not have any values left. */ 16487 16488 if (next_data_value ()) 16489 gfc_error ("DATA statement at %L has more values than variables", 16490 &d->where); 16491 } 16492 16493 16494 /* 12.6 Constraint: In a pure subprogram any variable which is in common or 16495 accessed by host or use association, is a dummy argument to a pure function, 16496 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that 16497 is storage associated with any such variable, shall not be used in the 16498 following contexts: (clients of this function). */ 16499 16500 /* Determines if a variable is not 'pure', i.e., not assignable within a pure 16501 procedure. Returns zero if assignment is OK, nonzero if there is a 16502 problem. */ 16503 int 16504 gfc_impure_variable (gfc_symbol *sym) 16505 { 16506 gfc_symbol *proc; 16507 gfc_namespace *ns; 16508 16509 if (sym->attr.use_assoc || sym->attr.in_common) 16510 return 1; 16511 16512 /* Check if the symbol's ns is inside the pure procedure. */ 16513 for (ns = gfc_current_ns; ns; ns = ns->parent) 16514 { 16515 if (ns == sym->ns) 16516 break; 16517 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function) 16518 return 1; 16519 } 16520 16521 proc = sym->ns->proc_name; 16522 if (sym->attr.dummy 16523 && !sym->attr.value 16524 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN) 16525 || proc->attr.function)) 16526 return 1; 16527 16528 /* TODO: Sort out what can be storage associated, if anything, and include 16529 it here. In principle equivalences should be scanned but it does not 16530 seem to be possible to storage associate an impure variable this way. */ 16531 return 0; 16532 } 16533 16534 16535 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the 16536 current namespace is inside a pure procedure. */ 16537 16538 int 16539 gfc_pure (gfc_symbol *sym) 16540 { 16541 symbol_attribute attr; 16542 gfc_namespace *ns; 16543 16544 if (sym == NULL) 16545 { 16546 /* Check if the current namespace or one of its parents 16547 belongs to a pure procedure. */ 16548 for (ns = gfc_current_ns; ns; ns = ns->parent) 16549 { 16550 sym = ns->proc_name; 16551 if (sym == NULL) 16552 return 0; 16553 attr = sym->attr; 16554 if (attr.flavor == FL_PROCEDURE && attr.pure) 16555 return 1; 16556 } 16557 return 0; 16558 } 16559 16560 attr = sym->attr; 16561 16562 return attr.flavor == FL_PROCEDURE && attr.pure; 16563 } 16564 16565 16566 /* Test whether a symbol is implicitly pure or not. For a NULL pointer, 16567 checks if the current namespace is implicitly pure. Note that this 16568 function returns false for a PURE procedure. */ 16569 16570 int 16571 gfc_implicit_pure (gfc_symbol *sym) 16572 { 16573 gfc_namespace *ns; 16574 16575 if (sym == NULL) 16576 { 16577 /* Check if the current procedure is implicit_pure. Walk up 16578 the procedure list until we find a procedure. */ 16579 for (ns = gfc_current_ns; ns; ns = ns->parent) 16580 { 16581 sym = ns->proc_name; 16582 if (sym == NULL) 16583 return 0; 16584 16585 if (sym->attr.flavor == FL_PROCEDURE) 16586 break; 16587 } 16588 } 16589 16590 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure 16591 && !sym->attr.pure; 16592 } 16593 16594 16595 void 16596 gfc_unset_implicit_pure (gfc_symbol *sym) 16597 { 16598 gfc_namespace *ns; 16599 16600 if (sym == NULL) 16601 { 16602 /* Check if the current procedure is implicit_pure. Walk up 16603 the procedure list until we find a procedure. */ 16604 for (ns = gfc_current_ns; ns; ns = ns->parent) 16605 { 16606 sym = ns->proc_name; 16607 if (sym == NULL) 16608 return; 16609 16610 if (sym->attr.flavor == FL_PROCEDURE) 16611 break; 16612 } 16613 } 16614 16615 if (sym->attr.flavor == FL_PROCEDURE) 16616 sym->attr.implicit_pure = 0; 16617 else 16618 sym->attr.pure = 0; 16619 } 16620 16621 16622 /* Test whether the current procedure is elemental or not. */ 16623 16624 int 16625 gfc_elemental (gfc_symbol *sym) 16626 { 16627 symbol_attribute attr; 16628 16629 if (sym == NULL) 16630 sym = gfc_current_ns->proc_name; 16631 if (sym == NULL) 16632 return 0; 16633 attr = sym->attr; 16634 16635 return attr.flavor == FL_PROCEDURE && attr.elemental; 16636 } 16637 16638 16639 /* Warn about unused labels. */ 16640 16641 static void 16642 warn_unused_fortran_label (gfc_st_label *label) 16643 { 16644 if (label == NULL) 16645 return; 16646 16647 warn_unused_fortran_label (label->left); 16648 16649 if (label->defined == ST_LABEL_UNKNOWN) 16650 return; 16651 16652 switch (label->referenced) 16653 { 16654 case ST_LABEL_UNKNOWN: 16655 gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used", 16656 label->value, &label->where); 16657 break; 16658 16659 case ST_LABEL_BAD_TARGET: 16660 gfc_warning (OPT_Wunused_label, 16661 "Label %d at %L defined but cannot be used", 16662 label->value, &label->where); 16663 break; 16664 16665 default: 16666 break; 16667 } 16668 16669 warn_unused_fortran_label (label->right); 16670 } 16671 16672 16673 /* Returns the sequence type of a symbol or sequence. */ 16674 16675 static seq_type 16676 sequence_type (gfc_typespec ts) 16677 { 16678 seq_type result; 16679 gfc_component *c; 16680 16681 switch (ts.type) 16682 { 16683 case BT_DERIVED: 16684 16685 if (ts.u.derived->components == NULL) 16686 return SEQ_NONDEFAULT; 16687 16688 result = sequence_type (ts.u.derived->components->ts); 16689 for (c = ts.u.derived->components->next; c; c = c->next) 16690 if (sequence_type (c->ts) != result) 16691 return SEQ_MIXED; 16692 16693 return result; 16694 16695 case BT_CHARACTER: 16696 if (ts.kind != gfc_default_character_kind) 16697 return SEQ_NONDEFAULT; 16698 16699 return SEQ_CHARACTER; 16700 16701 case BT_INTEGER: 16702 if (ts.kind != gfc_default_integer_kind) 16703 return SEQ_NONDEFAULT; 16704 16705 return SEQ_NUMERIC; 16706 16707 case BT_REAL: 16708 if (!(ts.kind == gfc_default_real_kind 16709 || ts.kind == gfc_default_double_kind)) 16710 return SEQ_NONDEFAULT; 16711 16712 return SEQ_NUMERIC; 16713 16714 case BT_COMPLEX: 16715 if (ts.kind != gfc_default_complex_kind) 16716 return SEQ_NONDEFAULT; 16717 16718 return SEQ_NUMERIC; 16719 16720 case BT_LOGICAL: 16721 if (ts.kind != gfc_default_logical_kind) 16722 return SEQ_NONDEFAULT; 16723 16724 return SEQ_NUMERIC; 16725 16726 default: 16727 return SEQ_NONDEFAULT; 16728 } 16729 } 16730 16731 16732 /* Resolve derived type EQUIVALENCE object. */ 16733 16734 static bool 16735 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e) 16736 { 16737 gfc_component *c = derived->components; 16738 16739 if (!derived) 16740 return true; 16741 16742 /* Shall not be an object of nonsequence derived type. */ 16743 if (!derived->attr.sequence) 16744 { 16745 gfc_error ("Derived type variable %qs at %L must have SEQUENCE " 16746 "attribute to be an EQUIVALENCE object", sym->name, 16747 &e->where); 16748 return false; 16749 } 16750 16751 /* Shall not have allocatable components. */ 16752 if (derived->attr.alloc_comp) 16753 { 16754 gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE " 16755 "components to be an EQUIVALENCE object",sym->name, 16756 &e->where); 16757 return false; 16758 } 16759 16760 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived)) 16761 { 16762 gfc_error ("Derived type variable %qs at %L with default " 16763 "initialization cannot be in EQUIVALENCE with a variable " 16764 "in COMMON", sym->name, &e->where); 16765 return false; 16766 } 16767 16768 for (; c ; c = c->next) 16769 { 16770 if (gfc_bt_struct (c->ts.type) 16771 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e))) 16772 return false; 16773 16774 /* Shall not be an object of sequence derived type containing a pointer 16775 in the structure. */ 16776 if (c->attr.pointer) 16777 { 16778 gfc_error ("Derived type variable %qs at %L with pointer " 16779 "component(s) cannot be an EQUIVALENCE object", 16780 sym->name, &e->where); 16781 return false; 16782 } 16783 } 16784 return true; 16785 } 16786 16787 16788 /* Resolve equivalence object. 16789 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target, 16790 an allocatable array, an object of nonsequence derived type, an object of 16791 sequence derived type containing a pointer at any level of component 16792 selection, an automatic object, a function name, an entry name, a result 16793 name, a named constant, a structure component, or a subobject of any of 16794 the preceding objects. A substring shall not have length zero. A 16795 derived type shall not have components with default initialization nor 16796 shall two objects of an equivalence group be initialized. 16797 Either all or none of the objects shall have an protected attribute. 16798 The simple constraints are done in symbol.c(check_conflict) and the rest 16799 are implemented here. */ 16800 16801 static void 16802 resolve_equivalence (gfc_equiv *eq) 16803 { 16804 gfc_symbol *sym; 16805 gfc_symbol *first_sym; 16806 gfc_expr *e; 16807 gfc_ref *r; 16808 locus *last_where = NULL; 16809 seq_type eq_type, last_eq_type; 16810 gfc_typespec *last_ts; 16811 int object, cnt_protected; 16812 const char *msg; 16813 16814 last_ts = &eq->expr->symtree->n.sym->ts; 16815 16816 first_sym = eq->expr->symtree->n.sym; 16817 16818 cnt_protected = 0; 16819 16820 for (object = 1; eq; eq = eq->eq, object++) 16821 { 16822 e = eq->expr; 16823 16824 e->ts = e->symtree->n.sym->ts; 16825 /* match_varspec might not know yet if it is seeing 16826 array reference or substring reference, as it doesn't 16827 know the types. */ 16828 if (e->ref && e->ref->type == REF_ARRAY) 16829 { 16830 gfc_ref *ref = e->ref; 16831 sym = e->symtree->n.sym; 16832 16833 if (sym->attr.dimension) 16834 { 16835 ref->u.ar.as = sym->as; 16836 ref = ref->next; 16837 } 16838 16839 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */ 16840 if (e->ts.type == BT_CHARACTER 16841 && ref 16842 && ref->type == REF_ARRAY 16843 && ref->u.ar.dimen == 1 16844 && ref->u.ar.dimen_type[0] == DIMEN_RANGE 16845 && ref->u.ar.stride[0] == NULL) 16846 { 16847 gfc_expr *start = ref->u.ar.start[0]; 16848 gfc_expr *end = ref->u.ar.end[0]; 16849 void *mem = NULL; 16850 16851 /* Optimize away the (:) reference. */ 16852 if (start == NULL && end == NULL) 16853 { 16854 if (e->ref == ref) 16855 e->ref = ref->next; 16856 else 16857 e->ref->next = ref->next; 16858 mem = ref; 16859 } 16860 else 16861 { 16862 ref->type = REF_SUBSTRING; 16863 if (start == NULL) 16864 start = gfc_get_int_expr (gfc_charlen_int_kind, 16865 NULL, 1); 16866 ref->u.ss.start = start; 16867 if (end == NULL && e->ts.u.cl) 16868 end = gfc_copy_expr (e->ts.u.cl->length); 16869 ref->u.ss.end = end; 16870 ref->u.ss.length = e->ts.u.cl; 16871 e->ts.u.cl = NULL; 16872 } 16873 ref = ref->next; 16874 free (mem); 16875 } 16876 16877 /* Any further ref is an error. */ 16878 if (ref) 16879 { 16880 gcc_assert (ref->type == REF_ARRAY); 16881 gfc_error ("Syntax error in EQUIVALENCE statement at %L", 16882 &ref->u.ar.where); 16883 continue; 16884 } 16885 } 16886 16887 if (!gfc_resolve_expr (e)) 16888 continue; 16889 16890 sym = e->symtree->n.sym; 16891 16892 if (sym->attr.is_protected) 16893 cnt_protected++; 16894 if (cnt_protected > 0 && cnt_protected != object) 16895 { 16896 gfc_error ("Either all or none of the objects in the " 16897 "EQUIVALENCE set at %L shall have the " 16898 "PROTECTED attribute", 16899 &e->where); 16900 break; 16901 } 16902 16903 /* Shall not equivalence common block variables in a PURE procedure. */ 16904 if (sym->ns->proc_name 16905 && sym->ns->proc_name->attr.pure 16906 && sym->attr.in_common) 16907 { 16908 /* Need to check for symbols that may have entered the pure 16909 procedure via a USE statement. */ 16910 bool saw_sym = false; 16911 if (sym->ns->use_stmts) 16912 { 16913 gfc_use_rename *r; 16914 for (r = sym->ns->use_stmts->rename; r; r = r->next) 16915 if (strcmp(r->use_name, sym->name) == 0) saw_sym = true; 16916 } 16917 else 16918 saw_sym = true; 16919 16920 if (saw_sym) 16921 gfc_error ("COMMON block member %qs at %L cannot be an " 16922 "EQUIVALENCE object in the pure procedure %qs", 16923 sym->name, &e->where, sym->ns->proc_name->name); 16924 break; 16925 } 16926 16927 /* Shall not be a named constant. */ 16928 if (e->expr_type == EXPR_CONSTANT) 16929 { 16930 gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE " 16931 "object", sym->name, &e->where); 16932 continue; 16933 } 16934 16935 if (e->ts.type == BT_DERIVED 16936 && !resolve_equivalence_derived (e->ts.u.derived, sym, e)) 16937 continue; 16938 16939 /* Check that the types correspond correctly: 16940 Note 5.28: 16941 A numeric sequence structure may be equivalenced to another sequence 16942 structure, an object of default integer type, default real type, double 16943 precision real type, default logical type such that components of the 16944 structure ultimately only become associated to objects of the same 16945 kind. A character sequence structure may be equivalenced to an object 16946 of default character kind or another character sequence structure. 16947 Other objects may be equivalenced only to objects of the same type and 16948 kind parameters. */ 16949 16950 /* Identical types are unconditionally OK. */ 16951 if (object == 1 || gfc_compare_types (last_ts, &sym->ts)) 16952 goto identical_types; 16953 16954 last_eq_type = sequence_type (*last_ts); 16955 eq_type = sequence_type (sym->ts); 16956 16957 /* Since the pair of objects is not of the same type, mixed or 16958 non-default sequences can be rejected. */ 16959 16960 msg = "Sequence %s with mixed components in EQUIVALENCE " 16961 "statement at %L with different type objects"; 16962 if ((object ==2 16963 && last_eq_type == SEQ_MIXED 16964 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)) 16965 || (eq_type == SEQ_MIXED 16966 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))) 16967 continue; 16968 16969 msg = "Non-default type object or sequence %s in EQUIVALENCE " 16970 "statement at %L with objects of different type"; 16971 if ((object ==2 16972 && last_eq_type == SEQ_NONDEFAULT 16973 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)) 16974 || (eq_type == SEQ_NONDEFAULT 16975 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))) 16976 continue; 16977 16978 msg ="Non-CHARACTER object %qs in default CHARACTER " 16979 "EQUIVALENCE statement at %L"; 16980 if (last_eq_type == SEQ_CHARACTER 16981 && eq_type != SEQ_CHARACTER 16982 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)) 16983 continue; 16984 16985 msg ="Non-NUMERIC object %qs in default NUMERIC " 16986 "EQUIVALENCE statement at %L"; 16987 if (last_eq_type == SEQ_NUMERIC 16988 && eq_type != SEQ_NUMERIC 16989 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)) 16990 continue; 16991 16992 identical_types: 16993 16994 last_ts =&sym->ts; 16995 last_where = &e->where; 16996 16997 if (!e->ref) 16998 continue; 16999 17000 /* Shall not be an automatic array. */ 17001 if (e->ref->type == REF_ARRAY && is_non_constant_shape_array (sym)) 17002 { 17003 gfc_error ("Array %qs at %L with non-constant bounds cannot be " 17004 "an EQUIVALENCE object", sym->name, &e->where); 17005 continue; 17006 } 17007 17008 r = e->ref; 17009 while (r) 17010 { 17011 /* Shall not be a structure component. */ 17012 if (r->type == REF_COMPONENT) 17013 { 17014 gfc_error ("Structure component %qs at %L cannot be an " 17015 "EQUIVALENCE object", 17016 r->u.c.component->name, &e->where); 17017 break; 17018 } 17019 17020 /* A substring shall not have length zero. */ 17021 if (r->type == REF_SUBSTRING) 17022 { 17023 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT) 17024 { 17025 gfc_error ("Substring at %L has length zero", 17026 &r->u.ss.start->where); 17027 break; 17028 } 17029 } 17030 r = r->next; 17031 } 17032 } 17033 } 17034 17035 17036 /* Function called by resolve_fntype to flag other symbols used in the 17037 length type parameter specification of function results. */ 17038 17039 static bool 17040 flag_fn_result_spec (gfc_expr *expr, 17041 gfc_symbol *sym, 17042 int *f ATTRIBUTE_UNUSED) 17043 { 17044 gfc_namespace *ns; 17045 gfc_symbol *s; 17046 17047 if (expr->expr_type == EXPR_VARIABLE) 17048 { 17049 s = expr->symtree->n.sym; 17050 for (ns = s->ns; ns; ns = ns->parent) 17051 if (!ns->parent) 17052 break; 17053 17054 if (sym == s) 17055 { 17056 gfc_error ("Self reference in character length expression " 17057 "for %qs at %L", sym->name, &expr->where); 17058 return true; 17059 } 17060 17061 if (!s->fn_result_spec 17062 && s->attr.flavor == FL_PARAMETER) 17063 { 17064 /* Function contained in a module.... */ 17065 if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE) 17066 { 17067 gfc_symtree *st; 17068 s->fn_result_spec = 1; 17069 /* Make sure that this symbol is translated as a module 17070 variable. */ 17071 st = gfc_get_unique_symtree (ns); 17072 st->n.sym = s; 17073 s->refs++; 17074 } 17075 /* ... which is use associated and called. */ 17076 else if (s->attr.use_assoc || s->attr.used_in_submodule 17077 || 17078 /* External function matched with an interface. */ 17079 (s->ns->proc_name 17080 && ((s->ns == ns 17081 && s->ns->proc_name->attr.if_source == IFSRC_DECL) 17082 || s->ns->proc_name->attr.if_source == IFSRC_IFBODY) 17083 && s->ns->proc_name->attr.function)) 17084 s->fn_result_spec = 1; 17085 } 17086 } 17087 return false; 17088 } 17089 17090 17091 /* Resolve function and ENTRY types, issue diagnostics if needed. */ 17092 17093 static void 17094 resolve_fntype (gfc_namespace *ns) 17095 { 17096 gfc_entry_list *el; 17097 gfc_symbol *sym; 17098 17099 if (ns->proc_name == NULL || !ns->proc_name->attr.function) 17100 return; 17101 17102 /* If there are any entries, ns->proc_name is the entry master 17103 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */ 17104 if (ns->entries) 17105 sym = ns->entries->sym; 17106 else 17107 sym = ns->proc_name; 17108 if (sym->result == sym 17109 && sym->ts.type == BT_UNKNOWN 17110 && !gfc_set_default_type (sym, 0, NULL) 17111 && !sym->attr.untyped) 17112 { 17113 gfc_error ("Function %qs at %L has no IMPLICIT type", 17114 sym->name, &sym->declared_at); 17115 sym->attr.untyped = 1; 17116 } 17117 17118 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc 17119 && !sym->attr.contained 17120 && !gfc_check_symbol_access (sym->ts.u.derived) 17121 && gfc_check_symbol_access (sym)) 17122 { 17123 gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at " 17124 "%L of PRIVATE type %qs", sym->name, 17125 &sym->declared_at, sym->ts.u.derived->name); 17126 } 17127 17128 if (ns->entries) 17129 for (el = ns->entries->next; el; el = el->next) 17130 { 17131 if (el->sym->result == el->sym 17132 && el->sym->ts.type == BT_UNKNOWN 17133 && !gfc_set_default_type (el->sym, 0, NULL) 17134 && !el->sym->attr.untyped) 17135 { 17136 gfc_error ("ENTRY %qs at %L has no IMPLICIT type", 17137 el->sym->name, &el->sym->declared_at); 17138 el->sym->attr.untyped = 1; 17139 } 17140 } 17141 17142 if (sym->ts.type == BT_CHARACTER) 17143 gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0); 17144 } 17145 17146 17147 /* 12.3.2.1.1 Defined operators. */ 17148 17149 static bool 17150 check_uop_procedure (gfc_symbol *sym, locus where) 17151 { 17152 gfc_formal_arglist *formal; 17153 17154 if (!sym->attr.function) 17155 { 17156 gfc_error ("User operator procedure %qs at %L must be a FUNCTION", 17157 sym->name, &where); 17158 return false; 17159 } 17160 17161 if (sym->ts.type == BT_CHARACTER 17162 && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred) 17163 && !(sym->result && ((sym->result->ts.u.cl 17164 && sym->result->ts.u.cl->length) || sym->result->ts.deferred))) 17165 { 17166 gfc_error ("User operator procedure %qs at %L cannot be assumed " 17167 "character length", sym->name, &where); 17168 return false; 17169 } 17170 17171 formal = gfc_sym_get_dummy_args (sym); 17172 if (!formal || !formal->sym) 17173 { 17174 gfc_error ("User operator procedure %qs at %L must have at least " 17175 "one argument", sym->name, &where); 17176 return false; 17177 } 17178 17179 if (formal->sym->attr.intent != INTENT_IN) 17180 { 17181 gfc_error ("First argument of operator interface at %L must be " 17182 "INTENT(IN)", &where); 17183 return false; 17184 } 17185 17186 if (formal->sym->attr.optional) 17187 { 17188 gfc_error ("First argument of operator interface at %L cannot be " 17189 "optional", &where); 17190 return false; 17191 } 17192 17193 formal = formal->next; 17194 if (!formal || !formal->sym) 17195 return true; 17196 17197 if (formal->sym->attr.intent != INTENT_IN) 17198 { 17199 gfc_error ("Second argument of operator interface at %L must be " 17200 "INTENT(IN)", &where); 17201 return false; 17202 } 17203 17204 if (formal->sym->attr.optional) 17205 { 17206 gfc_error ("Second argument of operator interface at %L cannot be " 17207 "optional", &where); 17208 return false; 17209 } 17210 17211 if (formal->next) 17212 { 17213 gfc_error ("Operator interface at %L must have, at most, two " 17214 "arguments", &where); 17215 return false; 17216 } 17217 17218 return true; 17219 } 17220 17221 static void 17222 gfc_resolve_uops (gfc_symtree *symtree) 17223 { 17224 gfc_interface *itr; 17225 17226 if (symtree == NULL) 17227 return; 17228 17229 gfc_resolve_uops (symtree->left); 17230 gfc_resolve_uops (symtree->right); 17231 17232 for (itr = symtree->n.uop->op; itr; itr = itr->next) 17233 check_uop_procedure (itr->sym, itr->sym->declared_at); 17234 } 17235 17236 17237 /* Examine all of the expressions associated with a program unit, 17238 assign types to all intermediate expressions, make sure that all 17239 assignments are to compatible types and figure out which names 17240 refer to which functions or subroutines. It doesn't check code 17241 block, which is handled by gfc_resolve_code. */ 17242 17243 static void 17244 resolve_types (gfc_namespace *ns) 17245 { 17246 gfc_namespace *n; 17247 gfc_charlen *cl; 17248 gfc_data *d; 17249 gfc_equiv *eq; 17250 gfc_namespace* old_ns = gfc_current_ns; 17251 bool recursive = ns->proc_name && ns->proc_name->attr.recursive; 17252 17253 if (ns->types_resolved) 17254 return; 17255 17256 /* Check that all IMPLICIT types are ok. */ 17257 if (!ns->seen_implicit_none) 17258 { 17259 unsigned letter; 17260 for (letter = 0; letter != GFC_LETTERS; ++letter) 17261 if (ns->set_flag[letter] 17262 && !resolve_typespec_used (&ns->default_type[letter], 17263 &ns->implicit_loc[letter], NULL)) 17264 return; 17265 } 17266 17267 gfc_current_ns = ns; 17268 17269 resolve_entries (ns); 17270 17271 resolve_common_vars (&ns->blank_common, false); 17272 resolve_common_blocks (ns->common_root); 17273 17274 resolve_contained_functions (ns); 17275 17276 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE 17277 && ns->proc_name->attr.if_source == IFSRC_IFBODY) 17278 gfc_resolve_formal_arglist (ns->proc_name); 17279 17280 gfc_traverse_ns (ns, resolve_bind_c_derived_types); 17281 17282 for (cl = ns->cl_list; cl; cl = cl->next) 17283 resolve_charlen (cl); 17284 17285 gfc_traverse_ns (ns, resolve_symbol); 17286 17287 resolve_fntype (ns); 17288 17289 for (n = ns->contained; n; n = n->sibling) 17290 { 17291 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name)) 17292 gfc_error ("Contained procedure %qs at %L of a PURE procedure must " 17293 "also be PURE", n->proc_name->name, 17294 &n->proc_name->declared_at); 17295 17296 resolve_types (n); 17297 } 17298 17299 forall_flag = 0; 17300 gfc_do_concurrent_flag = 0; 17301 gfc_check_interfaces (ns); 17302 17303 gfc_traverse_ns (ns, resolve_values); 17304 17305 if (ns->save_all || (!flag_automatic && !recursive)) 17306 gfc_save_all (ns); 17307 17308 iter_stack = NULL; 17309 for (d = ns->data; d; d = d->next) 17310 resolve_data (d); 17311 17312 iter_stack = NULL; 17313 gfc_traverse_ns (ns, gfc_formalize_init_value); 17314 17315 gfc_traverse_ns (ns, gfc_verify_binding_labels); 17316 17317 for (eq = ns->equiv; eq; eq = eq->next) 17318 resolve_equivalence (eq); 17319 17320 /* Warn about unused labels. */ 17321 if (warn_unused_label) 17322 warn_unused_fortran_label (ns->st_labels); 17323 17324 gfc_resolve_uops (ns->uop_root); 17325 17326 gfc_traverse_ns (ns, gfc_verify_DTIO_procedures); 17327 17328 gfc_resolve_omp_declare_simd (ns); 17329 17330 gfc_resolve_omp_udrs (ns->omp_udr_root); 17331 17332 ns->types_resolved = 1; 17333 17334 gfc_current_ns = old_ns; 17335 } 17336 17337 17338 /* Call gfc_resolve_code recursively. */ 17339 17340 static void 17341 resolve_codes (gfc_namespace *ns) 17342 { 17343 gfc_namespace *n; 17344 bitmap_obstack old_obstack; 17345 17346 if (ns->resolved == 1) 17347 return; 17348 17349 for (n = ns->contained; n; n = n->sibling) 17350 resolve_codes (n); 17351 17352 gfc_current_ns = ns; 17353 17354 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */ 17355 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL)) 17356 cs_base = NULL; 17357 17358 /* Set to an out of range value. */ 17359 current_entry_id = -1; 17360 17361 old_obstack = labels_obstack; 17362 bitmap_obstack_initialize (&labels_obstack); 17363 17364 gfc_resolve_oacc_declare (ns); 17365 gfc_resolve_oacc_routines (ns); 17366 gfc_resolve_omp_local_vars (ns); 17367 gfc_resolve_code (ns->code, ns); 17368 17369 bitmap_obstack_release (&labels_obstack); 17370 labels_obstack = old_obstack; 17371 } 17372 17373 17374 /* This function is called after a complete program unit has been compiled. 17375 Its purpose is to examine all of the expressions associated with a program 17376 unit, assign types to all intermediate expressions, make sure that all 17377 assignments are to compatible types and figure out which names refer to 17378 which functions or subroutines. */ 17379 17380 void 17381 gfc_resolve (gfc_namespace *ns) 17382 { 17383 gfc_namespace *old_ns; 17384 code_stack *old_cs_base; 17385 struct gfc_omp_saved_state old_omp_state; 17386 17387 if (ns->resolved) 17388 return; 17389 17390 ns->resolved = -1; 17391 old_ns = gfc_current_ns; 17392 old_cs_base = cs_base; 17393 17394 /* As gfc_resolve can be called during resolution of an OpenMP construct 17395 body, we should clear any state associated to it, so that say NS's 17396 DO loops are not interpreted as OpenMP loops. */ 17397 if (!ns->construct_entities) 17398 gfc_omp_save_and_clear_state (&old_omp_state); 17399 17400 resolve_types (ns); 17401 component_assignment_level = 0; 17402 resolve_codes (ns); 17403 17404 gfc_current_ns = old_ns; 17405 cs_base = old_cs_base; 17406 ns->resolved = 1; 17407 17408 gfc_run_passes (ns); 17409 17410 if (!ns->construct_entities) 17411 gfc_omp_restore_state (&old_omp_state); 17412 } 17413