LCOV - code coverage report
Current view: top level - gcc/fortran - trans-decl.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 96.3 % 4134 3979
Test Date: 2026-04-20 14:57:17 Functions: 100.0 % 96 96
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Backend function setup
       2              :    Copyright (C) 2002-2026 Free Software Foundation, Inc.
       3              :    Contributed by Paul Brook
       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              : /* trans-decl.cc -- Handling of backend function and variable decls, etc */
      22              : 
      23              : #include "config.h"
      24              : #include "system.h"
      25              : #include "coretypes.h"
      26              : #include "target.h"
      27              : #include "function.h"
      28              : #include "tree.h"
      29              : #include "gfortran.h"
      30              : #include "gimple-expr.h"      /* For create_tmp_var_raw.  */
      31              : #include "trans.h"
      32              : #include "stringpool.h"
      33              : #include "cgraph.h"
      34              : #include "fold-const.h"
      35              : #include "stor-layout.h"
      36              : #include "varasm.h"
      37              : #include "attribs.h"
      38              : #include "dumpfile.h"
      39              : #include "toplev.h"   /* For announce_function.  */
      40              : #include "debug.h"
      41              : #include "constructor.h"
      42              : #include "trans-types.h"
      43              : #include "trans-array.h"
      44              : #include "trans-const.h"
      45              : /* Only for gfc_trans_code.  Shouldn't need to include this.  */
      46              : #include "trans-stmt.h"
      47              : #include "gomp-constants.h"
      48              : #include "gimplify.h"
      49              : #include "context.h"
      50              : #include "omp-general.h"
      51              : #include "omp-offload.h"
      52              : #include "attr-fnspec.h"
      53              : #include "tree-iterator.h"
      54              : #include "dependency.h"
      55              : 
      56              : #define MAX_LABEL_VALUE 99999
      57              : 
      58              : 
      59              : /* Holds the result of the function if no result variable specified.  */
      60              : 
      61              : static GTY(()) tree current_fake_result_decl;
      62              : static GTY(()) tree parent_fake_result_decl;
      63              : 
      64              : 
      65              : /* Holds the variable DECLs for the current function.  */
      66              : 
      67              : static GTY(()) tree saved_function_decls;
      68              : static GTY(()) tree saved_parent_function_decls;
      69              : 
      70              : /* Holds the variable DECLs that are locals.  */
      71              : 
      72              : static GTY(()) tree saved_local_decls;
      73              : 
      74              : /* The namespace of the module we're currently generating.  Only used while
      75              :    outputting decls for module variables.  Do not rely on this being set.  */
      76              : 
      77              : static gfc_namespace *module_namespace;
      78              : 
      79              : /* The currently processed procedure symbol.  */
      80              : static gfc_symbol* current_procedure_symbol = NULL;
      81              : 
      82              : /* The currently processed module.  */
      83              : static struct module_htab_entry *cur_module;
      84              : 
      85              : /* With -fcoarray=lib: For generating the registering call
      86              :    of static coarrays.  */
      87              : static bool has_coarray_vars_or_accessors;
      88              : static stmtblock_t caf_init_block;
      89              : 
      90              : 
      91              : /* List of static constructor functions.  */
      92              : 
      93              : tree gfc_static_ctors;
      94              : 
      95              : 
      96              : /* Whether we've seen a symbol from an IEEE module in the namespace.  */
      97              : static int seen_ieee_symbol;
      98              : 
      99              : /* Function declarations for builtin library functions.  */
     100              : 
     101              : tree gfor_fndecl_pause_numeric;
     102              : tree gfor_fndecl_pause_string;
     103              : tree gfor_fndecl_stop_numeric;
     104              : tree gfor_fndecl_stop_string;
     105              : tree gfor_fndecl_error_stop_numeric;
     106              : tree gfor_fndecl_error_stop_string;
     107              : tree gfor_fndecl_runtime_error;
     108              : tree gfor_fndecl_runtime_error_at;
     109              : tree gfor_fndecl_runtime_warning_at;
     110              : tree gfor_fndecl_os_error_at;
     111              : tree gfor_fndecl_generate_error;
     112              : tree gfor_fndecl_set_args;
     113              : tree gfor_fndecl_set_fpe;
     114              : tree gfor_fndecl_set_options;
     115              : tree gfor_fndecl_set_convert;
     116              : tree gfor_fndecl_set_record_marker;
     117              : tree gfor_fndecl_set_max_subrecord_length;
     118              : tree gfor_fndecl_ctime;
     119              : tree gfor_fndecl_fdate;
     120              : tree gfor_fndecl_ttynam;
     121              : tree gfor_fndecl_in_pack;
     122              : tree gfor_fndecl_in_unpack;
     123              : tree gfor_fndecl_in_pack_class;
     124              : tree gfor_fndecl_in_unpack_class;
     125              : tree gfor_fndecl_associated;
     126              : tree gfor_fndecl_system_clock4;
     127              : tree gfor_fndecl_system_clock8;
     128              : tree gfor_fndecl_ieee_procedure_entry;
     129              : tree gfor_fndecl_ieee_procedure_exit;
     130              : 
     131              : /* Coarray run-time library function decls.  */
     132              : tree gfor_fndecl_caf_init;
     133              : tree gfor_fndecl_caf_finalize;
     134              : tree gfor_fndecl_caf_this_image;
     135              : tree gfor_fndecl_caf_num_images;
     136              : tree gfor_fndecl_caf_register;
     137              : tree gfor_fndecl_caf_deregister;
     138              : tree gfor_fndecl_caf_register_accessor;
     139              : tree gfor_fndecl_caf_register_accessors_finish;
     140              : tree gfor_fndecl_caf_get_remote_function_index;
     141              : tree gfor_fndecl_caf_get_from_remote;
     142              : tree gfor_fndecl_caf_send_to_remote;
     143              : tree gfor_fndecl_caf_transfer_between_remotes;
     144              : tree gfor_fndecl_caf_sync_all;
     145              : tree gfor_fndecl_caf_sync_memory;
     146              : tree gfor_fndecl_caf_sync_images;
     147              : tree gfor_fndecl_caf_stop_str;
     148              : tree gfor_fndecl_caf_stop_numeric;
     149              : tree gfor_fndecl_caf_error_stop;
     150              : tree gfor_fndecl_caf_error_stop_str;
     151              : tree gfor_fndecl_caf_atomic_def;
     152              : tree gfor_fndecl_caf_atomic_ref;
     153              : tree gfor_fndecl_caf_atomic_cas;
     154              : tree gfor_fndecl_caf_atomic_op;
     155              : tree gfor_fndecl_caf_lock;
     156              : tree gfor_fndecl_caf_unlock;
     157              : tree gfor_fndecl_caf_event_post;
     158              : tree gfor_fndecl_caf_event_wait;
     159              : tree gfor_fndecl_caf_event_query;
     160              : tree gfor_fndecl_caf_fail_image;
     161              : tree gfor_fndecl_caf_failed_images;
     162              : tree gfor_fndecl_caf_image_status;
     163              : tree gfor_fndecl_caf_stopped_images;
     164              : tree gfor_fndecl_caf_form_team;
     165              : tree gfor_fndecl_caf_change_team;
     166              : tree gfor_fndecl_caf_end_team;
     167              : tree gfor_fndecl_caf_sync_team;
     168              : tree gfor_fndecl_caf_get_team;
     169              : tree gfor_fndecl_caf_team_number;
     170              : tree gfor_fndecl_co_broadcast;
     171              : tree gfor_fndecl_co_max;
     172              : tree gfor_fndecl_co_min;
     173              : tree gfor_fndecl_co_reduce;
     174              : tree gfor_fndecl_co_sum;
     175              : tree gfor_fndecl_caf_is_present_on_remote;
     176              : tree gfor_fndecl_caf_random_init;
     177              : 
     178              : 
     179              : /* Math functions.  Many other math functions are handled in
     180              :    trans-intrinsic.cc.  */
     181              : 
     182              : gfc_powdecl_list gfor_fndecl_math_powi[4][3];
     183              : tree gfor_fndecl_unsigned_pow_list[5][5];
     184              : 
     185              : tree gfor_fndecl_math_ishftc4;
     186              : tree gfor_fndecl_math_ishftc8;
     187              : tree gfor_fndecl_math_ishftc16;
     188              : 
     189              : 
     190              : /* String functions.  */
     191              : 
     192              : tree gfor_fndecl_compare_string;
     193              : tree gfor_fndecl_concat_string;
     194              : tree gfor_fndecl_string_len_trim;
     195              : tree gfor_fndecl_string_index;
     196              : tree gfor_fndecl_string_scan;
     197              : tree gfor_fndecl_string_verify;
     198              : tree gfor_fndecl_string_trim;
     199              : tree gfor_fndecl_string_minmax;
     200              : tree gfor_fndecl_string_split;
     201              : tree gfor_fndecl_adjustl;
     202              : tree gfor_fndecl_adjustr;
     203              : tree gfor_fndecl_select_string;
     204              : tree gfor_fndecl_compare_string_char4;
     205              : tree gfor_fndecl_concat_string_char4;
     206              : tree gfor_fndecl_string_len_trim_char4;
     207              : tree gfor_fndecl_string_index_char4;
     208              : tree gfor_fndecl_string_scan_char4;
     209              : tree gfor_fndecl_string_verify_char4;
     210              : tree gfor_fndecl_string_trim_char4;
     211              : tree gfor_fndecl_string_minmax_char4;
     212              : tree gfor_fndecl_string_split_char4;
     213              : tree gfor_fndecl_adjustl_char4;
     214              : tree gfor_fndecl_adjustr_char4;
     215              : tree gfor_fndecl_select_string_char4;
     216              : 
     217              : 
     218              : /* Conversion between character kinds.  */
     219              : tree gfor_fndecl_convert_char1_to_char4;
     220              : tree gfor_fndecl_convert_char4_to_char1;
     221              : 
     222              : 
     223              : /* Other misc. runtime library functions.  */
     224              : tree gfor_fndecl_iargc;
     225              : tree gfor_fndecl_kill;
     226              : tree gfor_fndecl_kill_sub;
     227              : tree gfor_fndecl_is_contiguous0;
     228              : tree gfor_fndecl_fstat_i4_sub;
     229              : tree gfor_fndecl_fstat_i8_sub;
     230              : tree gfor_fndecl_lstat_i4_sub;
     231              : tree gfor_fndecl_lstat_i8_sub;
     232              : tree gfor_fndecl_stat_i4_sub;
     233              : tree gfor_fndecl_stat_i8_sub;
     234              : 
     235              : 
     236              : /* Intrinsic functions implemented in Fortran.  */
     237              : tree gfor_fndecl_sc_kind;
     238              : tree gfor_fndecl_si_kind;
     239              : tree gfor_fndecl_sl_kind;
     240              : tree gfor_fndecl_sr_kind;
     241              : 
     242              : /* BLAS gemm functions.  */
     243              : tree gfor_fndecl_sgemm;
     244              : tree gfor_fndecl_dgemm;
     245              : tree gfor_fndecl_cgemm;
     246              : tree gfor_fndecl_zgemm;
     247              : 
     248              : /* RANDOM_INIT function.  */
     249              : tree gfor_fndecl_random_init;      /* libgfortran, 1 image only.  */
     250              : 
     251              : /* Deep copy helper for recursive allocatable array components.  */
     252              : tree gfor_fndecl_cfi_deep_copy_array;
     253              : 
     254              : static void
     255         5059 : gfc_add_decl_to_parent_function (tree decl)
     256              : {
     257         5059 :   gcc_assert (decl);
     258         5059 :   DECL_CONTEXT (decl) = DECL_CONTEXT (current_function_decl);
     259         5059 :   DECL_NONLOCAL (decl) = 1;
     260         5059 :   DECL_CHAIN (decl) = saved_parent_function_decls;
     261         5059 :   saved_parent_function_decls = decl;
     262         5059 : }
     263              : 
     264              : void
     265       288493 : gfc_add_decl_to_function (tree decl)
     266              : {
     267       288493 :   gcc_assert (decl);
     268       288493 :   TREE_USED (decl) = 1;
     269       288493 :   DECL_CONTEXT (decl) = current_function_decl;
     270       288493 :   DECL_CHAIN (decl) = saved_function_decls;
     271       288493 :   saved_function_decls = decl;
     272       288493 : }
     273              : 
     274              : static void
     275        13013 : add_decl_as_local (tree decl)
     276              : {
     277        13013 :   gcc_assert (decl);
     278        13013 :   TREE_USED (decl) = 1;
     279        13013 :   DECL_CONTEXT (decl) = current_function_decl;
     280        13013 :   DECL_CHAIN (decl) = saved_local_decls;
     281        13013 :   saved_local_decls = decl;
     282        13013 : }
     283              : 
     284              : 
     285              : /* Build a  backend label declaration.  Set TREE_USED for named labels.
     286              :    The context of the label is always the current_function_decl.  All
     287              :    labels are marked artificial.  */
     288              : 
     289              : tree
     290       621499 : gfc_build_label_decl (tree label_id)
     291              : {
     292              :   /* 2^32 temporaries should be enough.  */
     293       621499 :   static unsigned int tmp_num = 1;
     294       621499 :   tree label_decl;
     295       621499 :   char *label_name;
     296              : 
     297       621499 :   if (label_id == NULL_TREE)
     298              :     {
     299              :       /* Build an internal label name.  */
     300       617979 :       ASM_FORMAT_PRIVATE_NAME (label_name, "L", tmp_num++);
     301       617979 :       label_id = get_identifier (label_name);
     302              :     }
     303              :   else
     304       621499 :     label_name = NULL;
     305              : 
     306              :   /* Build the LABEL_DECL node. Labels have no type.  */
     307       621499 :   label_decl = build_decl (input_location,
     308              :                            LABEL_DECL, label_id, void_type_node);
     309       621499 :   DECL_CONTEXT (label_decl) = current_function_decl;
     310       621499 :   SET_DECL_MODE (label_decl, VOIDmode);
     311              : 
     312              :   /* We always define the label as used, even if the original source
     313              :      file never references the label.  We don't want all kinds of
     314              :      spurious warnings for old-style Fortran code with too many
     315              :      labels.  */
     316       621499 :   TREE_USED (label_decl) = 1;
     317              : 
     318       621499 :   DECL_ARTIFICIAL (label_decl) = 1;
     319       621499 :   return label_decl;
     320              : }
     321              : 
     322              : 
     323              : /* Set the backend source location of a decl.  */
     324              : 
     325              : void
     326       182572 : gfc_set_decl_location (tree decl, locus * loc)
     327              : {
     328       182572 :   DECL_SOURCE_LOCATION (decl) = gfc_get_location (loc);
     329       182572 : }
     330              : 
     331              : 
     332              : /* Return the backend label declaration for a given label structure,
     333              :    or create it if it doesn't exist yet.  */
     334              : 
     335              : tree
     336         5850 : gfc_get_label_decl (gfc_st_label * lp)
     337              : {
     338         5850 :   if (lp->backend_decl)
     339              :     return lp->backend_decl;
     340              :   else
     341              :     {
     342         3520 :       char label_name[GFC_MAX_SYMBOL_LEN + 1];
     343         3520 :       tree label_decl;
     344              : 
     345              :       /* Validate the label declaration from the front end.  */
     346         3520 :       gcc_assert (lp != NULL && lp->value <= MAX_LABEL_VALUE);
     347              : 
     348              :       /* Build a mangled name for the label.  */
     349         3520 :       if (lp->omp_region)
     350           11 :         sprintf (label_name, "__label_%d_%.6d", lp->omp_region, lp->value);
     351              :       else
     352         3509 :         sprintf (label_name, "__label_%.6d", lp->value);
     353              : 
     354              :       /* Build the LABEL_DECL node.  */
     355         3520 :       label_decl = gfc_build_label_decl (get_identifier (label_name));
     356              : 
     357              :       /* Tell the debugger where the label came from.  */
     358         3520 :       if (lp->value <= MAX_LABEL_VALUE)   /* An internal label.  */
     359         3520 :         gfc_set_decl_location (label_decl, &lp->where);
     360              :       else
     361            0 :         DECL_ARTIFICIAL (label_decl) = 1;
     362              : 
     363              :       /* Store the label in the label list and return the LABEL_DECL.  */
     364         3520 :       lp->backend_decl = label_decl;
     365         3520 :       return label_decl;
     366              :     }
     367              : }
     368              : 
     369              : /* Return the name of an identifier.  */
     370              : 
     371              : static const char *
     372       415682 : sym_identifier (gfc_symbol *sym)
     373              : {
     374       415682 :   if (sym->attr.is_main_program && strcmp (sym->name, "main") == 0)
     375              :     return "MAIN__";
     376              :   else
     377       411349 :     return sym->name;
     378              : }
     379              : 
     380              : /* Convert a gfc_symbol to an identifier of the same name.  */
     381              : 
     382              : static tree
     383       415682 : gfc_sym_identifier (gfc_symbol * sym)
     384              : {
     385       415682 :   return get_identifier (sym_identifier (sym));
     386              : }
     387              : 
     388              : /* Construct mangled name from symbol name.   */
     389              : 
     390              : static const char *
     391        19740 : mangled_identifier (gfc_symbol *sym)
     392              : {
     393        19740 :   gfc_symbol *proc = sym->ns->proc_name;
     394        19740 :   static char name[3*GFC_MAX_MANGLED_SYMBOL_LEN + 14];
     395              :   /* Prevent the mangling of identifiers that have an assigned
     396              :      binding label (mainly those that are bind(c)).  */
     397              : 
     398        19740 :   if (sym->attr.is_bind_c == 1 && sym->binding_label)
     399              :     return sym->binding_label;
     400              : 
     401        19615 :   if (!sym->fn_result_spec
     402           57 :       || (sym->module && !(proc && proc->attr.flavor == FL_PROCEDURE)))
     403              :     {
     404        19566 :       if (sym->module == NULL)
     405            0 :         return sym_identifier (sym);
     406              :       else
     407        19566 :         snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name);
     408              :     }
     409              :   else
     410              :     {
     411              :       /* This is an entity that is actually local to a module procedure
     412              :          that appears in the result specification expression.  Since
     413              :          sym->module will be a zero length string, we use ns->proc_name
     414              :          to provide the module name instead. */
     415           49 :       if (proc && proc->module)
     416           48 :         snprintf (name, sizeof name, "__%s_MOD__%s_PROC_%s",
     417              :                   proc->module, proc->name, sym->name);
     418              :       else
     419            1 :         snprintf (name, sizeof name, "__%s_PROC_%s",
     420              :                   proc->name, sym->name);
     421              :     }
     422              : 
     423              :   return name;
     424              : }
     425              : 
     426              : /* Get mangled identifier, adding the symbol to the global table if
     427              :    it is not yet already there.  */
     428              : 
     429              : static tree
     430        19591 : gfc_sym_mangled_identifier (gfc_symbol * sym)
     431              : {
     432        19591 :   tree result;
     433        19591 :   gfc_gsymbol *gsym;
     434        19591 :   const char *name;
     435              : 
     436        19591 :   name = mangled_identifier (sym);
     437        19591 :   result = get_identifier (name);
     438              : 
     439        19591 :   gsym = gfc_find_gsymbol (gfc_gsym_root, name);
     440        19591 :   if (gsym == NULL)
     441              :     {
     442        19415 :       gsym = gfc_get_gsymbol (name, false);
     443        19415 :       gsym->ns = sym->ns;
     444        19415 :       gsym->sym_name = sym->name;
     445              :     }
     446              : 
     447        19591 :   return result;
     448              : }
     449              : 
     450              : /* Construct mangled function name from symbol name.  */
     451              : 
     452              : static tree
     453        81116 : gfc_sym_mangled_function_id (gfc_symbol * sym)
     454              : {
     455        81116 :   int has_underscore;
     456        81116 :   char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
     457              : 
     458              :   /* It may be possible to simply use the binding label if it's
     459              :      provided, and remove the other checks.  Then we could use it
     460              :      for other things if we wished.  */
     461        81116 :   if ((sym->attr.is_bind_c == 1 || sym->attr.is_iso_c == 1) &&
     462         3443 :       sym->binding_label)
     463              :     /* use the binding label rather than the mangled name */
     464         3433 :     return get_identifier (sym->binding_label);
     465              : 
     466        77683 :   if ((sym->module == NULL || sym->attr.proc == PROC_EXTERNAL
     467        26450 :       || (sym->module != NULL && (sym->attr.external
     468        25395 :             || sym->attr.if_source == IFSRC_IFBODY)))
     469        52288 :       && !sym->attr.module_procedure)
     470              :     {
     471              :       /* Main program is mangled into MAIN__.  */
     472        51895 :       if (sym->attr.is_main_program)
     473        26136 :         return get_identifier ("MAIN__");
     474              : 
     475              :       /* Intrinsic procedures are never mangled.  */
     476        25759 :       if (sym->attr.proc == PROC_INTRINSIC)
     477        11065 :         return get_identifier (sym->name);
     478              : 
     479        14694 :       if (flag_underscoring)
     480              :         {
     481        13879 :           has_underscore = strchr (sym->name, '_') != 0;
     482        13879 :           if (flag_second_underscore && has_underscore)
     483          201 :             snprintf (name, sizeof name, "%s__", sym->name);
     484              :           else
     485        13678 :             snprintf (name, sizeof name, "%s_", sym->name);
     486        13879 :           return get_identifier (name);
     487              :         }
     488              :       else
     489          815 :         return get_identifier (sym->name);
     490              :     }
     491              :   else
     492              :     {
     493        25788 :       snprintf (name, sizeof name, "__%s_MOD_%s", sym->module, sym->name);
     494        25788 :       return get_identifier (name);
     495              :     }
     496              : }
     497              : 
     498              : 
     499              : void
     500       102984 : gfc_set_decl_assembler_name (tree decl, tree name)
     501              : {
     502       102984 :   tree target_mangled = targetm.mangle_decl_assembler_name (decl, name);
     503       102984 :   SET_DECL_ASSEMBLER_NAME (decl, target_mangled);
     504       102984 : }
     505              : 
     506              : 
     507              : /* Returns true if a variable of specified size should go on the stack.  */
     508              : 
     509              : bool
     510       171705 : gfc_can_put_var_on_stack (tree size)
     511              : {
     512       171705 :   unsigned HOST_WIDE_INT low;
     513              : 
     514       171705 :   if (!INTEGER_CST_P (size))
     515              :     return 0;
     516              : 
     517       164208 :   if (flag_max_stack_var_size < 0)
     518              :     return 1;
     519              : 
     520       136492 :   if (!tree_fits_uhwi_p (size))
     521              :     return 0;
     522              : 
     523       136492 :   low = TREE_INT_CST_LOW (size);
     524       136492 :   if (low > (unsigned HOST_WIDE_INT) flag_max_stack_var_size)
     525          206 :     return 0;
     526              : 
     527              : /* TODO: Set a per-function stack size limit.  */
     528              : 
     529              :   return 1;
     530              : }
     531              : 
     532              : 
     533              : /* gfc_finish_cray_pointee sets DECL_VALUE_EXPR for a Cray pointee to
     534              :    an expression involving its corresponding pointer.  There are
     535              :    2 cases; one for variable size arrays, and one for everything else,
     536              :    because variable-sized arrays require one fewer level of
     537              :    indirection.  */
     538              : 
     539              : static void
     540          288 : gfc_finish_cray_pointee (tree decl, gfc_symbol *sym)
     541              : {
     542          288 :   tree ptr_decl = gfc_get_symbol_decl (sym->cp_pointer);
     543          288 :   tree value;
     544              : 
     545              :   /* Parameters need to be dereferenced.  */
     546          288 :   if (sym->cp_pointer->attr.dummy)
     547            1 :     ptr_decl = build_fold_indirect_ref_loc (input_location,
     548              :                                         ptr_decl);
     549              : 
     550              :   /* Check to see if we're dealing with a variable-sized array.  */
     551          288 :   if (sym->attr.dimension
     552          288 :       && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
     553              :     {
     554              :       /* These decls will be dereferenced later, so we don't dereference
     555              :          them here.  */
     556          140 :       value = convert (TREE_TYPE (decl), ptr_decl);
     557              :     }
     558              :   else
     559              :     {
     560          148 :       ptr_decl = convert (build_pointer_type (TREE_TYPE (decl)),
     561              :                           ptr_decl);
     562          148 :       value = build_fold_indirect_ref_loc (input_location,
     563              :                                        ptr_decl);
     564              :     }
     565              : 
     566          288 :   SET_DECL_VALUE_EXPR (decl, value);
     567          288 :   DECL_HAS_VALUE_EXPR_P (decl) = 1;
     568          288 :   GFC_DECL_CRAY_POINTEE (decl) = 1;
     569          288 : }
     570              : 
     571              : 
     572              : /* Finish processing of a declaration without an initial value.  */
     573              : 
     574              : static void
     575       173956 : gfc_finish_decl (tree decl)
     576              : {
     577       173956 :   gcc_assert (TREE_CODE (decl) == PARM_DECL
     578              :               || DECL_INITIAL (decl) == NULL_TREE);
     579              : 
     580       173956 :   if (!VAR_P (decl))
     581              :     return;
     582              : 
     583          774 :   if (DECL_SIZE (decl) == NULL_TREE
     584          774 :       && COMPLETE_TYPE_P (TREE_TYPE (decl)))
     585            0 :     layout_decl (decl, 0);
     586              : 
     587              :   /* A few consistency checks.  */
     588              :   /* A static variable with an incomplete type is an error if it is
     589              :      initialized. Also if it is not file scope. Otherwise, let it
     590              :      through, but if it is not `extern' then it may cause an error
     591              :      message later.  */
     592              :   /* An automatic variable with an incomplete type is an error.  */
     593              : 
     594              :   /* We should know the storage size.  */
     595          774 :   gcc_assert (DECL_SIZE (decl) != NULL_TREE
     596              :               || (TREE_STATIC (decl)
     597              :                   ? (!DECL_INITIAL (decl) || !DECL_CONTEXT (decl))
     598              :                   : DECL_EXTERNAL (decl)));
     599              : 
     600              :   /* The storage size should be constant.  */
     601          774 :   gcc_assert ((!DECL_EXTERNAL (decl) && !TREE_STATIC (decl))
     602              :               || !DECL_SIZE (decl)
     603              :               || TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST);
     604              : }
     605              : 
     606              : 
     607              : /* Handle setting of GFC_DECL_SCALAR* on DECL.  */
     608              : 
     609              : void
     610       419453 : gfc_finish_decl_attrs (tree decl, symbol_attribute *attr)
     611              : {
     612       419453 :   if (!attr->dimension && !attr->codimension)
     613              :     {
     614              :       /* Handle scalar allocatable variables.  */
     615       332625 :       if (attr->allocatable)
     616              :         {
     617         6797 :           gfc_allocate_lang_decl (decl);
     618         6797 :           GFC_DECL_SCALAR_ALLOCATABLE (decl) = 1;
     619              :         }
     620              :       /* Handle scalar pointer variables.  */
     621       332625 :       if (attr->pointer)
     622              :         {
     623        41581 :           gfc_allocate_lang_decl (decl);
     624        41581 :           GFC_DECL_SCALAR_POINTER (decl) = 1;
     625              :         }
     626       332625 :       if (attr->target)
     627              :         {
     628        25263 :           gfc_allocate_lang_decl (decl);
     629        25263 :           GFC_DECL_SCALAR_TARGET (decl) = 1;
     630              :         }
     631              :     }
     632       419453 : }
     633              : 
     634              : 
     635              : /* Apply symbol attributes to a variable, and add it to the function scope.  */
     636              : 
     637              : static void
     638       184553 : gfc_finish_var_decl (tree decl, gfc_symbol * sym)
     639              : {
     640       184553 :   tree new_type;
     641              : 
     642              :   /* Set DECL_VALUE_EXPR for Cray Pointees.  */
     643       184553 :   if (sym->attr.cray_pointee)
     644          288 :     gfc_finish_cray_pointee (decl, sym);
     645              : 
     646              :   /* TREE_ADDRESSABLE means the address of this variable is actually needed.
     647              :      This is the equivalent of the TARGET variables.
     648              :      We also need to set this if the variable is passed by reference in a
     649              :      CALL statement.  */
     650       184553 :   if (sym->attr.target)
     651        27579 :     TREE_ADDRESSABLE (decl) = 1;
     652              : 
     653              :   /* If it wasn't used we wouldn't be getting it.  */
     654       184553 :   TREE_USED (decl) = 1;
     655              : 
     656       184553 :   if (sym->attr.flavor == FL_PARAMETER
     657         1468 :       && (sym->attr.dimension || sym->ts.type == BT_DERIVED))
     658         1458 :     TREE_READONLY (decl) = 1;
     659              : 
     660              :   /* Chain this decl to the pending declarations.  Don't do pushdecl()
     661              :      because this would add them to the current scope rather than the
     662              :      function scope.  */
     663       184553 :   if (current_function_decl != NULL_TREE)
     664              :     {
     665       165122 :       if (sym->ns->proc_name
     666       165116 :           && (sym->ns->proc_name->backend_decl == current_function_decl
     667        17972 :               || sym->result == sym))
     668       147144 :         gfc_add_decl_to_function (decl);
     669        17978 :       else if (sym->ns->omp_affinity_iterators)
     670              :         {
     671              :           /* Iterator variables are block-local; other variables in the
     672              :              iterator namespace (e.g. implicitly typed host-associated
     673              :              ones used in locator expressions) belong in the enclosing
     674              :              function.  */
     675              :           gfc_symbol *iter;
     676           52 :           for (iter = sym->ns->omp_affinity_iterators; iter;
     677            3 :                iter = iter->tlink)
     678           51 :             if (iter == sym)
     679              :               break;
     680           49 :           if (iter)
     681           48 :             add_decl_as_local (decl);
     682              :           else
     683            1 :             gfc_add_decl_to_function (decl);
     684              :         }
     685        17929 :       else if (sym->ns->proc_name
     686        17923 :                && sym->ns->proc_name->attr.flavor == FL_LABEL)
     687              :         /* This is a BLOCK construct.  */
     688        12965 :         add_decl_as_local (decl);
     689              :       else
     690         4964 :         gfc_add_decl_to_parent_function (decl);
     691              :     }
     692              : 
     693       184553 :   if (sym->attr.cray_pointee)
     694              :     return;
     695              : 
     696       184265 :   if(sym->attr.is_bind_c == 1 && sym->binding_label)
     697              :     {
     698              :       /* We need to put variables that are bind(c) into the common
     699              :          segment of the object file, because this is what C would do.
     700              :          gfortran would typically put them in either the BSS or
     701              :          initialized data segments, and only mark them as common if
     702              :          they were part of common blocks.  However, if they are not put
     703              :          into common space, then C cannot initialize global Fortran
     704              :          variables that it interoperates with and the draft says that
     705              :          either Fortran or C should be able to initialize it (but not
     706              :          both, of course.) (J3/04-007, section 15.3).  */
     707          125 :       TREE_PUBLIC(decl) = 1;
     708          125 :       DECL_COMMON(decl) = 1;
     709          125 :       if (sym->attr.access == ACCESS_PRIVATE && !sym->attr.public_used)
     710              :         {
     711            2 :           DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
     712            2 :           DECL_VISIBILITY_SPECIFIED (decl) = true;
     713              :         }
     714              :     }
     715              : 
     716              :   /* If a variable is USE associated, it's always external.  */
     717       184265 :   if (sym->attr.use_assoc || sym->attr.used_in_submodule)
     718              :     {
     719          113 :       DECL_EXTERNAL (decl) = 1;
     720          113 :       TREE_PUBLIC (decl) = 1;
     721              :     }
     722       184152 :   else if (sym->fn_result_spec && !sym->ns->proc_name->module)
     723              :     {
     724              : 
     725            1 :       if (sym->ns->proc_name->attr.if_source != IFSRC_DECL)
     726            0 :         DECL_EXTERNAL (decl) = 1;
     727              :       else
     728            1 :         TREE_STATIC (decl) = 1;
     729              : 
     730            1 :       TREE_PUBLIC (decl) = 1;
     731              :     }
     732       184151 :   else if (sym->module && !sym->attr.result && !sym->attr.dummy)
     733              :     {
     734              :       /* TODO: Don't set sym->module for result or dummy variables.  */
     735        19422 :       gcc_assert (current_function_decl == NULL_TREE || sym->result == sym);
     736              : 
     737        19422 :       TREE_PUBLIC (decl) = 1;
     738        19422 :       TREE_STATIC (decl) = 1;
     739        19422 :       if (sym->attr.access == ACCESS_PRIVATE && !sym->attr.public_used)
     740              :         {
     741          344 :           DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
     742          344 :           DECL_VISIBILITY_SPECIFIED (decl) = true;
     743              :         }
     744              :     }
     745              : 
     746              :   /* Derived types are a bit peculiar because of the possibility of
     747              :      a default initializer; this must be applied each time the variable
     748              :      comes into scope it therefore need not be static.  These variables
     749              :      are SAVE_NONE but have an initializer.  Otherwise explicitly
     750              :      initialized variables are SAVE_IMPLICIT and explicitly saved are
     751              :      SAVE_EXPLICIT.  */
     752       184265 :   if (!sym->attr.use_assoc
     753       184155 :         && (sym->attr.save != SAVE_NONE || sym->attr.data
     754       148435 :             || (sym->value && sym->ns->proc_name->attr.is_main_program)
     755       144111 :             || (flag_coarray == GFC_FCOARRAY_LIB
     756         2346 :                 && sym->attr.codimension && !sym->attr.allocatable)))
     757        40217 :     TREE_STATIC (decl) = 1;
     758              : 
     759              :   /* If derived-type variables with DTIO procedures are not made static
     760              :      some bits of code referencing them get optimized away.
     761              :      TODO Understand why this is so and fix it.  */
     762       184265 :   if (!sym->attr.use_assoc
     763       184155 :       && ((sym->ts.type == BT_DERIVED
     764        35794 :            && sym->ts.u.derived->attr.has_dtio_procs)
     765       183564 :           || (sym->ts.type == BT_CLASS
     766         4817 :               && CLASS_DATA (sym)->ts.u.derived->attr.has_dtio_procs)))
     767          623 :     TREE_STATIC (decl) = 1;
     768              : 
     769              :   /* Treat asynchronous variables the same as volatile, for now.  */
     770       184265 :   if (sym->attr.volatile_ || sym->attr.asynchronous)
     771              :     {
     772          773 :       TREE_THIS_VOLATILE (decl) = 1;
     773          773 :       TREE_SIDE_EFFECTS (decl) = 1;
     774          773 :       new_type = build_qualified_type (TREE_TYPE (decl), TYPE_QUAL_VOLATILE);
     775          773 :       TREE_TYPE (decl) = new_type;
     776              :     }
     777              : 
     778              :   /* Keep variables larger than max-stack-var-size off stack.  */
     779       184259 :   if (!(sym->ns->proc_name && sym->ns->proc_name->attr.recursive)
     780       161339 :       && !sym->attr.automatic
     781       161326 :       && !sym->attr.associate_var
     782       153116 :       && sym->attr.save != SAVE_EXPLICIT
     783       149431 :       && sym->attr.save != SAVE_IMPLICIT
     784       119452 :       && INTEGER_CST_P (DECL_SIZE_UNIT (decl))
     785       119061 :       && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl))
     786              :          /* Put variable length auto array pointers always into stack.  */
     787          141 :       && (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
     788            2 :           || sym->attr.dimension == 0
     789            2 :           || sym->as->type != AS_EXPLICIT
     790            2 :           || sym->attr.pointer
     791            2 :           || sym->attr.allocatable)
     792       184404 :       && !DECL_ARTIFICIAL (decl))
     793              :     {
     794          138 :       if (flag_max_stack_var_size > 0
     795          131 :           && !(sym->ns->proc_name
     796          131 :                && sym->ns->proc_name->attr.is_main_program))
     797           32 :         gfc_warning (OPT_Wsurprising,
     798              :                      "Array %qs at %L is larger than limit set by "
     799              :                      "%<-fmax-stack-var-size=%>, moved from stack to static "
     800              :                      "storage. This makes the procedure unsafe when called "
     801              :                      "recursively, or concurrently from multiple threads. "
     802              :                      "Consider increasing the %<-fmax-stack-var-size=%> "
     803              :                      "limit (or use %<-frecursive%>, which implies "
     804              :                      "unlimited %<-fmax-stack-var-size%>) - or change the "
     805              :                      "code to use an ALLOCATABLE array. If the variable is "
     806              :                      "never accessed concurrently, this warning can be "
     807              :                      "ignored, and the variable could also be declared with "
     808              :                      "the SAVE attribute.",
     809              :                      sym->name, &sym->declared_at);
     810              : 
     811          138 :       TREE_STATIC (decl) = 1;
     812              : 
     813              :       /* Because the size of this variable isn't known until now, we may have
     814              :          greedily added an initializer to this variable (in build_init_assign)
     815              :          even though the max-stack-var-size indicates the variable should be
     816              :          static. Therefore we rip out the automatic initializer here and
     817              :          replace it with a static one.  */
     818          138 :       gfc_symtree *st = gfc_find_symtree (sym->ns->sym_root, sym->name);
     819          138 :       gfc_code *prev = NULL;
     820          138 :       gfc_code *code = sym->ns->code;
     821          138 :       while (code && code->op == EXEC_INIT_ASSIGN)
     822              :         {
     823              :           /* Look for an initializer meant for this symbol.  */
     824            8 :           if (code->expr1->symtree == st)
     825              :             {
     826            8 :               if (prev)
     827            0 :                 prev->next = code->next;
     828              :               else
     829            8 :                 sym->ns->code = code->next;
     830              : 
     831              :               break;
     832              :             }
     833              : 
     834            0 :           prev = code;
     835            0 :           code = code->next;
     836              :         }
     837          146 :       if (code && code->op == EXEC_INIT_ASSIGN)
     838              :         {
     839              :           /* Keep the init expression for a static initializer.  */
     840            8 :           sym->value = code->expr2;
     841              :           /* Cleanup the defunct code object, without freeing the init expr.  */
     842            8 :           code->expr2 = NULL;
     843            8 :           gfc_free_statement (code);
     844            8 :           free (code);
     845              :         }
     846              :     }
     847              : 
     848       184265 :   if (sym->attr.omp_allocate && TREE_STATIC (decl))
     849              :     {
     850            9 :       struct gfc_omp_namelist *n;
     851           35 :       for (n = sym->ns->omp_allocate; n; n = n->next)
     852           35 :         if (n->sym == sym)
     853              :           break;
     854            9 :       tree alloc = gfc_conv_constant_to_tree (n->u2.allocator);
     855            9 :       tree align = (n->u.align ? gfc_conv_constant_to_tree (n->u.align)
     856              :                                : NULL_TREE);
     857            9 :       DECL_ATTRIBUTES (decl)
     858           18 :         = tree_cons (get_identifier ("omp allocate"),
     859            9 :                      build_tree_list (alloc, align), DECL_ATTRIBUTES (decl));
     860              :     }
     861              : 
     862              :   /* Mark weak variables.  */
     863       184265 :   if (sym->attr.ext_attr & (1 << EXT_ATTR_WEAK))
     864            1 :     declare_weak (decl);
     865              : 
     866              :   /* Handle threadprivate variables.  */
     867       184265 :   if (sym->attr.threadprivate
     868       184265 :       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
     869          151 :     set_decl_tls_model (decl, decl_default_tls_model (decl));
     870              : 
     871       184265 :   gfc_finish_decl_attrs (decl, &sym->attr);
     872              : }
     873              : 
     874              : 
     875              : /* Allocate the lang-specific part of a decl.  */
     876              : 
     877              : void
     878       108296 : gfc_allocate_lang_decl (tree decl)
     879              : {
     880       108296 :   if (DECL_LANG_SPECIFIC (decl) == NULL)
     881       102999 :     DECL_LANG_SPECIFIC (decl) = ggc_cleared_alloc<struct lang_decl> ();
     882       108296 : }
     883              : 
     884              : 
     885              : /* Determine order of two symbol declarations.  */
     886              : 
     887              : static bool
     888         5674 : decl_order (gfc_symbol *sym1, gfc_symbol *sym2)
     889              : {
     890         5674 :   if (sym1->decl_order > sym2->decl_order)
     891              :     return true;
     892              :   else
     893            0 :     return false;
     894              : }
     895              : 
     896              : 
     897              : /* Remember a symbol to generate initialization/cleanup code at function
     898              :    entry/exit.  */
     899              : 
     900              : static void
     901        82752 : gfc_defer_symbol_init (gfc_symbol * sym)
     902              : {
     903        82752 :   gfc_symbol *p;
     904        82752 :   gfc_symbol *last;
     905        82752 :   gfc_symbol *head;
     906              : 
     907              :   /* Don't add a symbol twice.  */
     908        82752 :   if (sym->tlink)
     909              :     return;
     910              : 
     911        77255 :   last = head = sym->ns->proc_name;
     912        77255 :   p = last->tlink;
     913              : 
     914        77255 :   gfc_function_dependency (sym, head);
     915              : 
     916              :   /* Make sure that setup code for dummy variables which are used in the
     917              :      setup of other variables is generated first.  */
     918        77255 :   if (sym->attr.dummy)
     919              :     {
     920              :       /* Find the first dummy arg seen after us, or the first non-dummy arg.
     921              :          This is a circular list, so don't go past the head.  */
     922              :       while (p != head
     923        16754 :              && (!p->attr.dummy || decl_order (p, sym)))
     924              :         {
     925         3315 :           last = p;
     926         3315 :           p = p->tlink;
     927              :         }
     928              :     }
     929        63816 :   else if (sym->fn_result_dep)
     930              :     {
     931              :       /* In the case of non-dummy symbols with dependencies on an old-fashioned
     932              :      function result (ie. proc_name = proc_name->result), make sure that the
     933              :      order in the tlink chain is such that the code appears in declaration
     934              :      order. This ensures that mutual dependencies between these symbols are
     935              :      respected.  */
     936              :       while (p != head
     937          228 :              && (!p->attr.result || decl_order (sym, p)))
     938              :         {
     939          162 :           last = p;
     940          162 :           p = p->tlink;
     941              :         }
     942              :     }
     943              :   /* Insert in between last and p.  */
     944        77255 :   last->tlink = sym;
     945        77255 :   sym->tlink = p;
     946              : }
     947              : 
     948              : 
     949              : /* Used in gfc_get_symbol_decl and gfc_get_derived_type to obtain the
     950              :    backend_decl for a module symbol, if it all ready exists.  If the
     951              :    module gsymbol does not exist, it is created.  If the symbol does
     952              :    not exist, it is added to the gsymbol namespace.  Returns true if
     953              :    an existing backend_decl is found.  */
     954              : 
     955              : bool
     956        14003 : gfc_get_module_backend_decl (gfc_symbol *sym)
     957              : {
     958        14003 :   gfc_gsymbol *gsym;
     959        14003 :   gfc_symbol *s;
     960        14003 :   gfc_symtree *st;
     961              : 
     962        14003 :   gsym =  gfc_find_gsymbol (gfc_gsym_root, sym->module);
     963              : 
     964        14003 :   if (!gsym || (gsym->ns && gsym->type == GSYM_MODULE))
     965              :     {
     966        14003 :       st = NULL;
     967        14003 :       s = NULL;
     968              : 
     969              :       /* Check for a symbol with the same name. */
     970          362 :       if (gsym)
     971        13641 :         gfc_find_symbol (sym->name, gsym->ns, 0, &s);
     972              : 
     973        14003 :       if (!s)
     974              :         {
     975          530 :           if (!gsym)
     976              :             {
     977          362 :               gsym = gfc_get_gsymbol (sym->module, false);
     978          362 :               gsym->type = GSYM_MODULE;
     979          362 :               gsym->ns = gfc_get_namespace (NULL, 0);
     980              :             }
     981              : 
     982          530 :           st = gfc_new_symtree (&gsym->ns->sym_root, sym->name);
     983          530 :           st->n.sym = sym;
     984          530 :           sym->refs++;
     985              :         }
     986        13473 :       else if (gfc_fl_struct (sym->attr.flavor))
     987              :         {
     988        10771 :           if (s && s->attr.flavor == FL_PROCEDURE)
     989              :             {
     990         5813 :               gfc_interface *intr;
     991         5813 :               gcc_assert (s->attr.generic);
     992         5958 :               for (intr = s->generic; intr; intr = intr->next)
     993         5958 :                 if (gfc_fl_struct (intr->sym->attr.flavor))
     994              :                   {
     995         5813 :                     s = intr->sym;
     996         5813 :                     break;
     997              :                   }
     998              :             }
     999              : 
    1000              :           /* Normally we can assume that s is a derived-type symbol since it
    1001              :              shares a name with the derived-type sym. However if sym is a
    1002              :              STRUCTURE, it may in fact share a name with any other basic type
    1003              :              variable. If s is in fact of derived type then we can continue
    1004              :              looking for a duplicate type declaration.  */
    1005        10771 :           if (sym->attr.flavor == FL_STRUCT && s->ts.type == BT_DERIVED)
    1006              :             {
    1007            0 :               s = s->ts.u.derived;
    1008              :             }
    1009              : 
    1010        10771 :           if (gfc_fl_struct (s->attr.flavor) && !s->backend_decl)
    1011              :             {
    1012           25 :               if (s->attr.flavor == FL_UNION)
    1013            0 :                 s->backend_decl = gfc_get_union_type (s);
    1014              :               else
    1015           25 :                 s->backend_decl = gfc_get_derived_type (s);
    1016              :             }
    1017        10771 :           gfc_copy_dt_decls_ifequal (s, sym, true);
    1018        10771 :           return true;
    1019              :         }
    1020         2702 :       else if (s->backend_decl)
    1021              :         {
    1022         2690 :           if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
    1023          532 :             gfc_copy_dt_decls_ifequal (s->ts.u.derived, sym->ts.u.derived,
    1024              :                                        true);
    1025         2158 :           else if (sym->ts.type == BT_CHARACTER)
    1026          312 :             sym->ts.u.cl->backend_decl = s->ts.u.cl->backend_decl;
    1027         2690 :           sym->backend_decl = s->backend_decl;
    1028         2690 :           return true;
    1029              :         }
    1030              :     }
    1031              :   return false;
    1032              : }
    1033              : 
    1034              : 
    1035              : /* Create an array index type variable with function scope.  */
    1036              : 
    1037              : static tree
    1038        45686 : create_index_var (const char * pfx, int nest)
    1039              : {
    1040        45686 :   tree decl;
    1041              : 
    1042        45686 :   decl = gfc_create_var_np (gfc_array_index_type, pfx);
    1043        45686 :   if (nest)
    1044           28 :     gfc_add_decl_to_parent_function (decl);
    1045              :   else
    1046        45658 :     gfc_add_decl_to_function (decl);
    1047        45686 :   return decl;
    1048              : }
    1049              : 
    1050              : 
    1051              : /* Create variables to hold all the non-constant bits of info for a
    1052              :    descriptorless array.  Remember these in the lang-specific part of the
    1053              :    type.  */
    1054              : 
    1055              : static void
    1056        62887 : gfc_build_qualified_array (tree decl, gfc_symbol * sym)
    1057              : {
    1058        62887 :   tree type;
    1059        62887 :   int dim;
    1060        62887 :   int nest;
    1061        62887 :   gfc_namespace* procns;
    1062        62887 :   symbol_attribute *array_attr;
    1063        62887 :   gfc_array_spec *as;
    1064        62887 :   bool is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
    1065              : 
    1066        62887 :   type = TREE_TYPE (decl);
    1067        62887 :   array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
    1068        62887 :   as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
    1069              : 
    1070              :   /* We just use the descriptor, if there is one.  */
    1071        62887 :   if (GFC_DESCRIPTOR_TYPE_P (type))
    1072              :     return;
    1073              : 
    1074        47217 :   gcc_assert (GFC_ARRAY_TYPE_P (type));
    1075        47217 :   procns = gfc_find_proc_namespace (sym->ns);
    1076        94434 :   nest = (procns->proc_name->backend_decl != current_function_decl)
    1077        47217 :          && !sym->attr.contained;
    1078              : 
    1079          847 :   if (array_attr->codimension && flag_coarray == GFC_FCOARRAY_LIB
    1080          535 :       && as->type != AS_ASSUMED_SHAPE
    1081        47729 :       && GFC_TYPE_ARRAY_CAF_TOKEN (type) == NULL_TREE)
    1082              :     {
    1083          512 :       tree token;
    1084          512 :       tree token_type = build_qualified_type (pvoid_type_node,
    1085              :                                               TYPE_QUAL_RESTRICT);
    1086              : 
    1087          512 :       if (sym->module && (sym->attr.use_assoc
    1088           22 :                           || sym->ns->proc_name->attr.flavor == FL_MODULE))
    1089              :         {
    1090           27 :           tree token_name
    1091           27 :                 = get_identifier (gfc_get_string (GFC_PREFIX ("caf_token%s"),
    1092              :                         IDENTIFIER_POINTER (gfc_sym_mangled_identifier (sym))));
    1093           27 :           token = build_decl (DECL_SOURCE_LOCATION (decl), VAR_DECL, token_name,
    1094              :                               token_type);
    1095           27 :           if (sym->attr.use_assoc
    1096           22 :               || (sym->attr.host_assoc && sym->attr.used_in_submodule))
    1097            7 :             DECL_EXTERNAL (token) = 1;
    1098              :           else
    1099           20 :             TREE_STATIC (token) = 1;
    1100              : 
    1101           27 :           TREE_PUBLIC (token) = 1;
    1102              : 
    1103           27 :           if (sym->attr.access == ACCESS_PRIVATE && !sym->attr.public_used)
    1104              :             {
    1105            0 :               DECL_VISIBILITY (token) = VISIBILITY_HIDDEN;
    1106            0 :               DECL_VISIBILITY_SPECIFIED (token) = true;
    1107              :             }
    1108              :         }
    1109              :       else
    1110              :         {
    1111          485 :           token = gfc_create_var_np (token_type, "caf_token");
    1112          485 :           TREE_STATIC (token) = 1;
    1113              :         }
    1114              : 
    1115          512 :       GFC_TYPE_ARRAY_CAF_TOKEN (type) = token;
    1116          512 :       DECL_ARTIFICIAL (token) = 1;
    1117          512 :       DECL_NONALIASED (token) = 1;
    1118              : 
    1119          512 :       if (sym->module && !sym->attr.use_assoc)
    1120              :         {
    1121           22 :           module_htab_entry *mod
    1122           22 :             = cur_module ? cur_module : gfc_find_module (sym->module);
    1123           22 :           pushdecl (token);
    1124           22 :           DECL_CONTEXT (token) = sym->ns->proc_name->backend_decl;
    1125           22 :           gfc_module_add_decl (mod, token);
    1126           22 :         }
    1127          490 :       else if (sym->attr.host_assoc
    1128          490 :                && TREE_CODE (DECL_CONTEXT (current_function_decl))
    1129              :                != TRANSLATION_UNIT_DECL)
    1130            3 :         gfc_add_decl_to_parent_function (token);
    1131              :       else
    1132          487 :         gfc_add_decl_to_function (token);
    1133              :     }
    1134              : 
    1135       112474 :   for (dim = 0; dim < GFC_TYPE_ARRAY_RANK (type); dim++)
    1136              :     {
    1137        65257 :       if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE)
    1138              :         {
    1139          521 :           GFC_TYPE_ARRAY_LBOUND (type, dim) = create_index_var ("lbound", nest);
    1140          521 :           suppress_warning (GFC_TYPE_ARRAY_LBOUND (type, dim));
    1141              :         }
    1142              :       /* Don't try to use the unknown bound for assumed shape arrays.  */
    1143        65257 :       if (GFC_TYPE_ARRAY_UBOUND (type, dim) == NULL_TREE
    1144        65257 :           && (as->type != AS_ASSUMED_SIZE
    1145         2111 :               || dim < GFC_TYPE_ARRAY_RANK (type) - 1))
    1146              :         {
    1147        18569 :           GFC_TYPE_ARRAY_UBOUND (type, dim) = create_index_var ("ubound", nest);
    1148        18569 :           suppress_warning (GFC_TYPE_ARRAY_UBOUND (type, dim));
    1149              :         }
    1150              : 
    1151        65257 :       if (GFC_TYPE_ARRAY_STRIDE (type, dim) == NULL_TREE)
    1152              :         {
    1153        11087 :           GFC_TYPE_ARRAY_STRIDE (type, dim) = create_index_var ("stride", nest);
    1154        11087 :           suppress_warning (GFC_TYPE_ARRAY_STRIDE (type, dim));
    1155              :         }
    1156              :     }
    1157        48338 :   for (dim = GFC_TYPE_ARRAY_RANK (type);
    1158        48338 :        dim < GFC_TYPE_ARRAY_RANK (type) + GFC_TYPE_ARRAY_CORANK (type); dim++)
    1159              :     {
    1160         1121 :       if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE)
    1161              :         {
    1162          114 :           GFC_TYPE_ARRAY_LBOUND (type, dim) = create_index_var ("lbound", nest);
    1163          114 :           suppress_warning (GFC_TYPE_ARRAY_LBOUND (type, dim));
    1164              :         }
    1165              :       /* Don't try to use the unknown ubound for the last coarray dimension.  */
    1166         1121 :       if (GFC_TYPE_ARRAY_UBOUND (type, dim) == NULL_TREE
    1167         1121 :           && dim < GFC_TYPE_ARRAY_RANK (type) + GFC_TYPE_ARRAY_CORANK (type) - 1)
    1168              :         {
    1169           60 :           GFC_TYPE_ARRAY_UBOUND (type, dim) = create_index_var ("ubound", nest);
    1170           60 :           suppress_warning (GFC_TYPE_ARRAY_UBOUND (type, dim));
    1171              :         }
    1172              :     }
    1173        47217 :   if (GFC_TYPE_ARRAY_OFFSET (type) == NULL_TREE)
    1174              :     {
    1175         8588 :       GFC_TYPE_ARRAY_OFFSET (type) = gfc_create_var_np (gfc_array_index_type,
    1176              :                                                         "offset");
    1177         8588 :       suppress_warning (GFC_TYPE_ARRAY_OFFSET (type));
    1178              : 
    1179         8588 :       if (nest)
    1180            8 :         gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type));
    1181              :       else
    1182         8580 :         gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type));
    1183              :     }
    1184              : 
    1185        64307 :   if (GFC_TYPE_ARRAY_SIZE (type) == NULL_TREE && as->rank != 0
    1186        64266 :       && as->type != AS_ASSUMED_SIZE)
    1187              :     {
    1188        15335 :       GFC_TYPE_ARRAY_SIZE (type) = create_index_var ("size", nest);
    1189        15335 :       suppress_warning (GFC_TYPE_ARRAY_SIZE (type));
    1190              :     }
    1191              : 
    1192        47217 :   if (POINTER_TYPE_P (type))
    1193              :     {
    1194        20628 :       gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (type)));
    1195        20628 :       gcc_assert (TYPE_LANG_SPECIFIC (type)
    1196              :                   == TYPE_LANG_SPECIFIC (TREE_TYPE (type)));
    1197        20628 :       type = TREE_TYPE (type);
    1198              :     }
    1199              : 
    1200        47217 :   if (! COMPLETE_TYPE_P (type) && GFC_TYPE_ARRAY_SIZE (type))
    1201              :     {
    1202        15335 :       tree size, range;
    1203              : 
    1204        46005 :       size = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    1205        15335 :                               GFC_TYPE_ARRAY_SIZE (type), gfc_index_one_node);
    1206        15335 :       range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
    1207              :                                 size);
    1208        15335 :       TYPE_DOMAIN (type) = range;
    1209        15335 :       layout_type (type);
    1210              :     }
    1211              : 
    1212        85793 :   if (TYPE_NAME (type) != NULL_TREE && as->rank > 0
    1213        38126 :       && GFC_TYPE_ARRAY_UBOUND (type, as->rank - 1) != NULL_TREE
    1214        84017 :       && VAR_P (GFC_TYPE_ARRAY_UBOUND (type, as->rank - 1)))
    1215              :     {
    1216         7135 :       tree gtype = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
    1217              : 
    1218         7523 :       for (dim = 0; dim < as->rank - 1; dim++)
    1219              :         {
    1220          388 :           gcc_assert (TREE_CODE (gtype) == ARRAY_TYPE);
    1221          388 :           gtype = TREE_TYPE (gtype);
    1222              :         }
    1223         7135 :       gcc_assert (TREE_CODE (gtype) == ARRAY_TYPE);
    1224         7135 :       if (TYPE_MAX_VALUE (TYPE_DOMAIN (gtype)) == NULL)
    1225         7135 :         TYPE_NAME (type) = NULL_TREE;
    1226              :     }
    1227              : 
    1228        47217 :   if (TYPE_NAME (type) == NULL_TREE)
    1229              :     {
    1230        15776 :       tree gtype = TREE_TYPE (type), rtype, type_decl;
    1231              : 
    1232        36290 :       for (dim = as->rank - 1; dim >= 0; dim--)
    1233              :         {
    1234        20514 :           tree lbound, ubound;
    1235        20514 :           lbound = GFC_TYPE_ARRAY_LBOUND (type, dim);
    1236        20514 :           ubound = GFC_TYPE_ARRAY_UBOUND (type, dim);
    1237        20514 :           rtype = build_range_type (gfc_array_index_type, lbound, ubound);
    1238        20514 :           gtype = build_array_type (gtype, rtype);
    1239              :           /* Ensure the bound variables aren't optimized out at -O0.
    1240              :              For -O1 and above they often will be optimized out, but
    1241              :              can be tracked by VTA.  Also set DECL_NAMELESS, so that
    1242              :              the artificial lbound.N or ubound.N DECL_NAME doesn't
    1243              :              end up in debug info.  */
    1244        20514 :           if (lbound
    1245        20514 :               && VAR_P (lbound)
    1246          521 :               && DECL_ARTIFICIAL (lbound)
    1247        21035 :               && DECL_IGNORED_P (lbound))
    1248              :             {
    1249          521 :               if (DECL_NAME (lbound)
    1250          521 :                   && strstr (IDENTIFIER_POINTER (DECL_NAME (lbound)),
    1251              :                              "lbound") != 0)
    1252          521 :                 DECL_NAMELESS (lbound) = 1;
    1253          521 :               DECL_IGNORED_P (lbound) = 0;
    1254              :             }
    1255        20514 :           if (ubound
    1256        20126 :               && VAR_P (ubound)
    1257        18569 :               && DECL_ARTIFICIAL (ubound)
    1258        39083 :               && DECL_IGNORED_P (ubound))
    1259              :             {
    1260        18569 :               if (DECL_NAME (ubound)
    1261        18569 :                   && strstr (IDENTIFIER_POINTER (DECL_NAME (ubound)),
    1262              :                              "ubound") != 0)
    1263        18569 :                 DECL_NAMELESS (ubound) = 1;
    1264        18569 :               DECL_IGNORED_P (ubound) = 0;
    1265              :             }
    1266              :         }
    1267        15776 :       TYPE_NAME (type) = type_decl = build_decl (input_location,
    1268              :                                                  TYPE_DECL, NULL, gtype);
    1269        15776 :       DECL_ORIGINAL_TYPE (type_decl) = gtype;
    1270              :     }
    1271              : }
    1272              : 
    1273              : 
    1274              : /* For some dummy arguments we don't use the actual argument directly.
    1275              :    Instead we create a local decl and use that.  This allows us to perform
    1276              :    initialization, and construct full type information.  */
    1277              : 
    1278              : static tree
    1279        24906 : gfc_build_dummy_array_decl (gfc_symbol * sym, tree dummy)
    1280              : {
    1281        24906 :   tree decl;
    1282        24906 :   tree type;
    1283        24906 :   gfc_array_spec *as;
    1284        24906 :   symbol_attribute *array_attr;
    1285        24906 :   char *name;
    1286        24906 :   gfc_packed packed;
    1287        24906 :   int n;
    1288        24906 :   bool known_size;
    1289        24906 :   bool is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
    1290              : 
    1291              :   /* Use the array as and attr.  */
    1292        24906 :   as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
    1293        24906 :   array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
    1294              : 
    1295              :   /* The dummy is returned for pointer, allocatable or assumed rank arrays.
    1296              :      For class arrays the information if sym is an allocatable or pointer
    1297              :      object needs to be checked explicitly (IS_CLASS_ARRAY can be false for
    1298              :      too many reasons to be of use here).  */
    1299        24906 :   if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
    1300        23041 :       || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
    1301        23041 :       || array_attr->allocatable
    1302        17980 :       || (as && as->type == AS_ASSUMED_RANK))
    1303              :     return dummy;
    1304              : 
    1305              :   /* Add to list of variables if not a fake result variable.
    1306              :      These symbols are set on the symbol only, not on the class component.  */
    1307        14161 :   if (sym->attr.result || sym->attr.dummy)
    1308        13557 :     gfc_defer_symbol_init (sym);
    1309              : 
    1310              :   /* For a class array the array descriptor is in the _data component, while
    1311              :      for a regular array the TREE_TYPE of the dummy is a pointer to the
    1312              :      descriptor.  */
    1313        14161 :   type = TREE_TYPE (is_classarray ? gfc_class_data_get (dummy)
    1314              :                                   : TREE_TYPE (dummy));
    1315              :   /* type now is the array descriptor w/o any indirection.  */
    1316        14161 :   gcc_assert (TREE_CODE (dummy) == PARM_DECL
    1317              :           && POINTER_TYPE_P (TREE_TYPE (dummy)));
    1318              : 
    1319              :   /* Do we know the element size?  */
    1320        14161 :   known_size = sym->ts.type != BT_CHARACTER
    1321        14161 :           || INTEGER_CST_P (sym->ts.u.cl->backend_decl);
    1322              : 
    1323        13281 :   if (known_size && !GFC_DESCRIPTOR_TYPE_P (type))
    1324              :     {
    1325              :       /* For descriptorless arrays with known element size the actual
    1326              :          argument is sufficient.  */
    1327         6744 :       gfc_build_qualified_array (dummy, sym);
    1328         6744 :       return dummy;
    1329              :     }
    1330              : 
    1331         7417 :   if (GFC_DESCRIPTOR_TYPE_P (type))
    1332              :     {
    1333              :       /* Create a descriptorless array pointer.  */
    1334         7048 :       packed = PACKED_NO;
    1335              : 
    1336              :       /* Even when -frepack-arrays is used, symbols with TARGET attribute
    1337              :          are not repacked.  */
    1338         7048 :       if (!flag_repack_arrays || sym->attr.target)
    1339              :         {
    1340         7046 :           if (as->type == AS_ASSUMED_SIZE)
    1341           79 :             packed = PACKED_FULL;
    1342              :         }
    1343              :       else
    1344              :         {
    1345            2 :           if (as->type == AS_EXPLICIT)
    1346              :             {
    1347            3 :               packed = PACKED_FULL;
    1348            3 :               for (n = 0; n < as->rank; n++)
    1349              :                 {
    1350            2 :                   if (!(as->upper[n]
    1351            2 :                         && as->lower[n]
    1352            2 :                         && as->upper[n]->expr_type == EXPR_CONSTANT
    1353            2 :                         && as->lower[n]->expr_type == EXPR_CONSTANT))
    1354              :                     {
    1355              :                       packed = PACKED_PARTIAL;
    1356              :                       break;
    1357              :                     }
    1358              :                 }
    1359              :             }
    1360              :           else
    1361              :             packed = PACKED_PARTIAL;
    1362              :         }
    1363              : 
    1364              :       /* For classarrays the element type is required, but
    1365              :          gfc_typenode_for_spec () returns the array descriptor.  */
    1366         7048 :       type = is_classarray ? gfc_get_element_type (type)
    1367         6150 :                            : gfc_typenode_for_spec (&sym->ts);
    1368         7048 :       type = gfc_get_nodesc_array_type (type, as, packed,
    1369         7048 :                                         !sym->attr.target);
    1370              :     }
    1371              :   else
    1372              :     {
    1373              :       /* We now have an expression for the element size, so create a fully
    1374              :          qualified type.  Reset sym->backend decl or this will just return the
    1375              :          old type.  */
    1376          369 :       DECL_ARTIFICIAL (sym->backend_decl) = 1;
    1377          369 :       sym->backend_decl = NULL_TREE;
    1378          369 :       type = gfc_sym_type (sym);
    1379          369 :       packed = PACKED_FULL;
    1380              :     }
    1381              : 
    1382         7417 :   ASM_FORMAT_PRIVATE_NAME (name, IDENTIFIER_POINTER (DECL_NAME (dummy)), 0);
    1383         7417 :   decl = build_decl (input_location,
    1384              :                      VAR_DECL, get_identifier (name), type);
    1385              : 
    1386         7417 :   DECL_ARTIFICIAL (decl) = 1;
    1387         7417 :   DECL_NAMELESS (decl) = 1;
    1388         7417 :   TREE_PUBLIC (decl) = 0;
    1389         7417 :   TREE_STATIC (decl) = 0;
    1390         7417 :   DECL_EXTERNAL (decl) = 0;
    1391              : 
    1392              :   /* Avoid uninitialized warnings for optional dummy arguments.  */
    1393         7417 :   if ((sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.optional)
    1394         7417 :       || sym->attr.optional)
    1395          804 :     suppress_warning (decl);
    1396              : 
    1397              :   /* We should never get deferred shape arrays here.  We used to because of
    1398              :      frontend bugs.  */
    1399         7417 :   gcc_assert (as->type != AS_DEFERRED);
    1400              : 
    1401         7417 :   if (packed == PACKED_PARTIAL)
    1402            1 :     GFC_DECL_PARTIAL_PACKED_ARRAY (decl) = 1;
    1403         7416 :   else if (packed == PACKED_FULL)
    1404          448 :     GFC_DECL_PACKED_ARRAY (decl) = 1;
    1405              : 
    1406         7417 :   gfc_build_qualified_array (decl, sym);
    1407              : 
    1408         7417 :   if (DECL_LANG_SPECIFIC (dummy))
    1409          982 :     DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (dummy);
    1410              :   else
    1411         6435 :     gfc_allocate_lang_decl (decl);
    1412              : 
    1413         7417 :   GFC_DECL_SAVED_DESCRIPTOR (decl) = dummy;
    1414              : 
    1415         7417 :   if (sym->ns->proc_name->backend_decl == current_function_decl
    1416          518 :       || sym->attr.contained)
    1417         7409 :     gfc_add_decl_to_function (decl);
    1418              :   else
    1419            8 :     gfc_add_decl_to_parent_function (decl);
    1420              : 
    1421              :   return decl;
    1422              : }
    1423              : 
    1424              : /* Return a constant or a variable to use as a string length.  Does not
    1425              :    add the decl to the current scope.  */
    1426              : 
    1427              : static tree
    1428        15763 : gfc_create_string_length (gfc_symbol * sym)
    1429              : {
    1430        15763 :   gcc_assert (sym->ts.u.cl);
    1431        15763 :   gfc_conv_const_charlen (sym->ts.u.cl);
    1432              : 
    1433        15763 :   if (sym->ts.u.cl->backend_decl == NULL_TREE)
    1434              :     {
    1435         3476 :       tree length;
    1436         3476 :       const char *name;
    1437              : 
    1438              :       /* The string length variable shall be in static memory if it is either
    1439              :          explicitly SAVED, a module variable or with -fno-automatic. Only
    1440              :          relevant is "len=:" - otherwise, it is either a constant length or
    1441              :          it is an automatic variable.  */
    1442         6952 :       bool static_length = sym->attr.save
    1443         3299 :                            || sym->ns->proc_name->attr.flavor == FL_MODULE
    1444         6775 :                            || (flag_max_stack_var_size == 0
    1445            2 :                                && sym->ts.deferred && !sym->attr.dummy
    1446            0 :                                && !sym->attr.result && !sym->attr.function);
    1447              : 
    1448              :       /* Also prefix the mangled name. We need to call GFC_PREFIX for static
    1449              :          variables as some systems do not support the "." in the assembler name.
    1450              :          For nonstatic variables, the "." does not appear in assembler.  */
    1451         3299 :       if (static_length)
    1452              :         {
    1453          177 :           if (sym->module)
    1454           54 :             name = gfc_get_string (GFC_PREFIX ("%s_MOD_%s"), sym->module,
    1455              :                                    sym->name);
    1456              :           else
    1457          123 :             name = gfc_get_string (GFC_PREFIX ("%s"), sym->name);
    1458              :         }
    1459         3299 :       else if (sym->module)
    1460            0 :         name = gfc_get_string (".__%s_MOD_%s", sym->module, sym->name);
    1461              :       else
    1462         3299 :         name = gfc_get_string (".%s", sym->name);
    1463              : 
    1464         3476 :       length = build_decl (input_location,
    1465              :                            VAR_DECL, get_identifier (name),
    1466              :                            gfc_charlen_type_node);
    1467         3476 :       DECL_ARTIFICIAL (length) = 1;
    1468         3476 :       TREE_USED (length) = 1;
    1469         3476 :       if (sym->ns->proc_name->tlink != NULL)
    1470         3225 :         gfc_defer_symbol_init (sym);
    1471              : 
    1472         3476 :       sym->ts.u.cl->backend_decl = length;
    1473              : 
    1474         3476 :       if (static_length)
    1475          177 :         TREE_STATIC (length) = 1;
    1476              : 
    1477         3476 :       if (sym->ns->proc_name->attr.flavor == FL_MODULE
    1478           54 :           && (sym->attr.access != ACCESS_PRIVATE || sym->attr.public_used))
    1479           54 :         TREE_PUBLIC (length) = 1;
    1480              :     }
    1481              : 
    1482        15763 :   gcc_assert (sym->ts.u.cl->backend_decl != NULL_TREE);
    1483        15763 :   return sym->ts.u.cl->backend_decl;
    1484              : }
    1485              : 
    1486              : /* If a variable is assigned a label, we add another two auxiliary
    1487              :    variables.  */
    1488              : 
    1489              : static void
    1490           66 : gfc_add_assign_aux_vars (gfc_symbol * sym)
    1491              : {
    1492           66 :   tree addr;
    1493           66 :   tree length;
    1494           66 :   tree decl;
    1495              : 
    1496           66 :   gcc_assert (sym->backend_decl);
    1497              : 
    1498           66 :   decl = sym->backend_decl;
    1499           66 :   gfc_allocate_lang_decl (decl);
    1500           66 :   GFC_DECL_ASSIGN (decl) = 1;
    1501           66 :   length = build_decl (input_location,
    1502              :                        VAR_DECL, create_tmp_var_name (sym->name),
    1503              :                        gfc_charlen_type_node);
    1504           66 :   addr = build_decl (input_location,
    1505              :                      VAR_DECL, create_tmp_var_name (sym->name),
    1506              :                      pvoid_type_node);
    1507           66 :   gfc_finish_var_decl (length, sym);
    1508           66 :   gfc_finish_var_decl (addr, sym);
    1509              :   /*  STRING_LENGTH is also used as flag. Less than -1 means that
    1510              :       ASSIGN_ADDR cannot be used. Equal -1 means that ASSIGN_ADDR is the
    1511              :       target label's address. Otherwise, value is the length of a format string
    1512              :       and ASSIGN_ADDR is its address.  */
    1513           66 :   if (TREE_STATIC (length))
    1514            1 :     DECL_INITIAL (length) = build_int_cst (gfc_charlen_type_node, -2);
    1515              :   else
    1516           65 :     gfc_defer_symbol_init (sym);
    1517              : 
    1518           66 :   GFC_DECL_STRING_LEN (decl) = length;
    1519           66 :   GFC_DECL_ASSIGN_ADDR (decl) = addr;
    1520           66 : }
    1521              : 
    1522              : 
    1523              : static void
    1524       287189 : add_attributes_to_decl (tree *decl_p, const gfc_symbol *sym)
    1525              : {
    1526       287189 :   unsigned id;
    1527       287189 :   tree list = NULL_TREE;
    1528       287189 :   symbol_attribute sym_attr = sym->attr;
    1529              : 
    1530      3159079 :   for (id = 0; id < EXT_ATTR_NUM; id++)
    1531      2871890 :     if (sym_attr.ext_attr & (1 << id) && ext_attr_list[id].middle_end_name)
    1532              :       {
    1533            0 :         tree ident = get_identifier (ext_attr_list[id].middle_end_name);
    1534            0 :         list = tree_cons (ident, NULL_TREE, list);
    1535              :       }
    1536              : 
    1537       287189 :   tree clauses = NULL_TREE;
    1538              : 
    1539       287189 :   if (sym_attr.oacc_routine_lop != OACC_ROUTINE_LOP_NONE)
    1540              :     {
    1541          355 :       omp_clause_code code;
    1542          355 :       switch (sym_attr.oacc_routine_lop)
    1543              :         {
    1544              :         case OACC_ROUTINE_LOP_GANG:
    1545              :           code = OMP_CLAUSE_GANG;
    1546              :           break;
    1547              :         case OACC_ROUTINE_LOP_WORKER:
    1548              :           code = OMP_CLAUSE_WORKER;
    1549              :           break;
    1550              :         case OACC_ROUTINE_LOP_VECTOR:
    1551              :           code = OMP_CLAUSE_VECTOR;
    1552              :           break;
    1553              :         case OACC_ROUTINE_LOP_SEQ:
    1554              :           code = OMP_CLAUSE_SEQ;
    1555              :           break;
    1556            0 :         case OACC_ROUTINE_LOP_NONE:
    1557            0 :         case OACC_ROUTINE_LOP_ERROR:
    1558            0 :         default:
    1559            0 :           gcc_unreachable ();
    1560              :         }
    1561          355 :       tree c = build_omp_clause (UNKNOWN_LOCATION, code);
    1562          355 :       OMP_CLAUSE_CHAIN (c) = clauses;
    1563          355 :       clauses = c;
    1564              : 
    1565          355 :       tree dims = oacc_build_routine_dims (clauses);
    1566          355 :       list = oacc_replace_fn_attrib_attr (list, dims);
    1567              :     }
    1568              : 
    1569       287189 :   if (sym_attr.oacc_routine_nohost)
    1570              :     {
    1571           40 :       tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_NOHOST);
    1572           40 :       OMP_CLAUSE_CHAIN (c) = clauses;
    1573           40 :       clauses = c;
    1574              :     }
    1575              : 
    1576              :   /* FIXME: 'declare_target_link' permits both any and host, but
    1577              :      will fail if one sets OMP_CLAUSE_DEVICE_TYPE_KIND.  */
    1578       287189 :   if (sym_attr.omp_device_type != OMP_DEVICE_TYPE_UNSET
    1579          414 :       && !sym_attr.omp_declare_target_link
    1580          406 :       && !sym_attr.omp_declare_target_indirect /* implies 'any' */)
    1581              :     {
    1582          360 :       tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_DEVICE_TYPE);
    1583          360 :       switch (sym_attr.omp_device_type)
    1584              :         {
    1585            5 :         case OMP_DEVICE_TYPE_HOST:
    1586            5 :           OMP_CLAUSE_DEVICE_TYPE_KIND (c) = OMP_CLAUSE_DEVICE_TYPE_HOST;
    1587            5 :           break;
    1588            4 :         case OMP_DEVICE_TYPE_NOHOST:
    1589            4 :           OMP_CLAUSE_DEVICE_TYPE_KIND (c) = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
    1590            4 :           break;
    1591          351 :         case OMP_DEVICE_TYPE_ANY:
    1592          351 :           OMP_CLAUSE_DEVICE_TYPE_KIND (c) = OMP_CLAUSE_DEVICE_TYPE_ANY;
    1593          351 :           break;
    1594            0 :         default:
    1595            0 :           gcc_unreachable ();
    1596              :         }
    1597          360 :       OMP_CLAUSE_CHAIN (c) = clauses;
    1598          360 :       clauses = c;
    1599              :     }
    1600              : 
    1601              :   /* Also check trans-common.cc when updating/removing the following;
    1602              :      also update f95.c's gfc_gnu_attributes.  */
    1603       287189 :   if (sym_attr.omp_groupprivate)
    1604            6 :     gfc_error ("Sorry, OMP GROUPPRIVATE not implemented, "
    1605            6 :                "used by %qs declared at %L", sym->name, &sym->declared_at);
    1606       287183 :   else if (sym_attr.omp_declare_target_local)
    1607              :     /* Use 'else if' as groupprivate implies 'local'.  */
    1608            0 :     gfc_error ("Sorry, OMP DECLARE TARGET with LOCAL clause not implemented, "
    1609            0 :                "used by %qs declared at %L", sym->name, &sym->declared_at);
    1610              : 
    1611       287189 :   bool has_declare = true;
    1612       287189 :   if (sym_attr.omp_declare_target_link
    1613       287181 :       || sym_attr.oacc_declare_link)
    1614            9 :     list = tree_cons (get_identifier ("omp declare target link"),
    1615              :                       clauses, list);
    1616       287180 :   else if (sym_attr.omp_declare_target
    1617       286302 :            || sym_attr.oacc_declare_create
    1618       286259 :            || sym_attr.oacc_declare_copyin
    1619       286249 :            || sym_attr.oacc_declare_deviceptr
    1620       286249 :            || sym_attr.oacc_declare_device_resident)
    1621          947 :     list = tree_cons (get_identifier ("omp declare target"),
    1622              :                       clauses, list);
    1623              :   else
    1624              :     has_declare = false;
    1625              : 
    1626       287189 :   if (sym_attr.omp_declare_target_indirect)
    1627           46 :     list = tree_cons (get_identifier ("omp declare target indirect"),
    1628              :                       clauses, list);
    1629              : 
    1630       287189 :   decl_attributes (decl_p, list, 0);
    1631              : 
    1632       287189 :   if (has_declare
    1633          956 :       && VAR_P (*decl_p)
    1634          411 :       && sym->ns->proc_name->attr.flavor != FL_MODULE)
    1635              :     {
    1636          150 :       has_declare = false;
    1637          618 :       for (gfc_namespace* ns = sym->ns->contained; ns; ns = ns->sibling)
    1638          471 :         if (ns->proc_name->attr.omp_declare_target)
    1639              :           {
    1640              :             has_declare = true;
    1641              :             break;
    1642              :           }
    1643              :     }
    1644              : 
    1645          956 :   if (has_declare && VAR_P (*decl_p) && has_declare)
    1646              :     {
    1647              :       /* Add to offload_vars; get_create does so for omp_declare_target,
    1648              :          omp_declare_target_link requires manual work.  */
    1649          264 :       gcc_assert (symtab_node::get (*decl_p) == 0);
    1650          264 :       symtab_node *node = symtab_node::get_create (*decl_p);
    1651          264 :       if (node != NULL && sym_attr.omp_declare_target_link)
    1652              :         {
    1653            8 :           node->offloadable = 1;
    1654            8 :           if (ENABLE_OFFLOADING)
    1655              :             {
    1656              :               g->have_offload = true;
    1657              :               if (is_a <varpool_node *> (node))
    1658              :                 vec_safe_push (offload_vars, *decl_p);
    1659              :             }
    1660              :         }
    1661              :     }
    1662       287189 : }
    1663              : 
    1664              : 
    1665              : static void build_function_decl (gfc_symbol * sym, bool global);
    1666              : 
    1667              : 
    1668              : /* Return the decl for a gfc_symbol, create it if it doesn't already
    1669              :    exist.  */
    1670              : 
    1671              : tree
    1672      1835216 : gfc_get_symbol_decl (gfc_symbol * sym)
    1673              : {
    1674      1835216 :   tree decl;
    1675      1835216 :   tree length = NULL_TREE;
    1676      1835216 :   int byref;
    1677      1835216 :   bool intrinsic_array_parameter = false;
    1678      1835216 :   bool fun_or_res;
    1679              : 
    1680      1835216 :   gcc_assert (sym->attr.referenced
    1681              :               || sym->attr.flavor == FL_PROCEDURE
    1682              :               || sym->attr.use_assoc
    1683              :               || sym->attr.used_in_submodule
    1684              :               || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY
    1685              :               || (sym->module && sym->attr.if_source != IFSRC_DECL
    1686              :                   && sym->backend_decl));
    1687              : 
    1688       326065 :   if (sym->attr.dummy && sym->ns->proc_name->attr.is_bind_c
    1689      1859197 :       && is_CFI_desc (sym, NULL))
    1690              :     {
    1691        15489 :       gcc_assert (sym->backend_decl && (sym->ts.type != BT_CHARACTER
    1692              :                                         || sym->ts.u.cl->backend_decl));
    1693              :       return sym->backend_decl;
    1694              :     }
    1695              : 
    1696      1819727 :   if (sym->ns && sym->ns->proc_name && sym->ns->proc_name->attr.function)
    1697       226729 :     byref = gfc_return_by_reference (sym->ns->proc_name);
    1698              :   else
    1699              :     byref = 0;
    1700              : 
    1701              :   /* Make sure that the vtab for the declared type is completed.  */
    1702      1819727 :   if (sym->ts.type == BT_CLASS)
    1703              :     {
    1704        85614 :       gfc_component *c = CLASS_DATA (sym);
    1705        85614 :       if (!c->ts.u.derived->backend_decl)
    1706              :         {
    1707         2541 :           gfc_find_derived_vtab (c->ts.u.derived);
    1708         2541 :           gfc_get_derived_type (sym->ts.u.derived);
    1709              :         }
    1710              :     }
    1711              : 
    1712              :   /* PDT parameterized array components and string_lengths must have the
    1713              :      'len' parameters substituted for the expressions appearing in the
    1714              :      declaration of the entity and memory allocated/deallocated.  */
    1715      1819727 :   if ((sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
    1716       321942 :       && sym->param_list != NULL
    1717         4261 :       && (gfc_current_ns == sym->ns
    1718         2071 :           || (gfc_current_ns == sym->ns->parent
    1719         2071 :               && gfc_current_ns->proc_name->attr.flavor != FL_MODULE))
    1720         3034 :       && !(sym->attr.use_assoc || sym->attr.dummy))
    1721         2775 :     gfc_defer_symbol_init (sym);
    1722              : 
    1723      1819727 :   if ((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.pdt_comp)
    1724           68 :       && (gfc_current_ns == sym->ns
    1725           25 :           || (gfc_current_ns == sym->ns->parent
    1726           24 :               && gfc_current_ns->proc_name->attr.flavor != FL_MODULE))
    1727           43 :       && !(sym->attr.use_assoc || sym->attr.dummy))
    1728           43 :     gfc_defer_symbol_init (sym);
    1729              : 
    1730              :   /* Dummy PDT 'len' parameters should be checked when they are explicit.  */
    1731      1819727 :   if ((sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
    1732       321942 :       && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    1733        11330 :       && sym->param_list != NULL
    1734          454 :       && sym->attr.dummy)
    1735          189 :     gfc_defer_symbol_init (sym);
    1736              : 
    1737              :   /* All deferred character length procedures need to retain the backend
    1738              :      decl, which is a pointer to the character length in the caller's
    1739              :      namespace and to declare a local character length.  */
    1740      1819727 :   if (!byref && sym->attr.function
    1741        19286 :         && sym->ts.type == BT_CHARACTER
    1742         1373 :         && sym->ts.deferred
    1743          268 :         && sym->ts.u.cl->passed_length == NULL
    1744           12 :         && sym->ts.u.cl->backend_decl
    1745            0 :         && TREE_CODE (sym->ts.u.cl->backend_decl) == PARM_DECL)
    1746              :     {
    1747            0 :       sym->ts.u.cl->passed_length = sym->ts.u.cl->backend_decl;
    1748            0 :       gcc_assert (POINTER_TYPE_P (TREE_TYPE (sym->ts.u.cl->passed_length)));
    1749            0 :       sym->ts.u.cl->backend_decl = build_fold_indirect_ref (sym->ts.u.cl->backend_decl);
    1750              :     }
    1751              : 
    1752        17904 :   fun_or_res = byref && (sym->attr.result
    1753        14115 :                          || (sym->attr.function && sym->ts.deferred));
    1754      1819727 :   if ((sym->attr.dummy && ! sym->attr.function) || fun_or_res)
    1755              :     {
    1756              :       /* Return via extra parameter.  */
    1757       313787 :       if (sym->attr.result && byref
    1758         3789 :           && !sym->backend_decl)
    1759              :         {
    1760         2416 :           sym->backend_decl =
    1761         1208 :             DECL_ARGUMENTS (sym->ns->proc_name->backend_decl);
    1762              :           /* For entry master function skip over the __entry
    1763              :              argument.  */
    1764         1208 :           if (sym->ns->proc_name->attr.entry_master)
    1765           83 :             sym->backend_decl = DECL_CHAIN (sym->backend_decl);
    1766              :         }
    1767              : 
    1768              :       /* Automatic array indices in module procedures need the backend_decl
    1769              :          to be extracted from the procedure formal arglist.  */
    1770       313787 :       if (sym->attr.dummy && !sym->backend_decl)
    1771              :         {
    1772           12 :           gfc_formal_arglist *f;
    1773           18 :           for (f = sym->ns->proc_name->formal; f; f = f->next)
    1774              :             {
    1775           18 :               gfc_symbol *fsym = f->sym;
    1776           18 :               if (strcmp (sym->name, fsym->name))
    1777            6 :                 continue;
    1778           12 :               sym->backend_decl = fsym->backend_decl;
    1779           12 :               break;
    1780              :              }
    1781              :         }
    1782              : 
    1783              :       /* Dummy variables should already have been created.  */
    1784       313787 :       gcc_assert (sym->backend_decl);
    1785              : 
    1786              :       /* However, the string length of deferred arrays must be set.  */
    1787       313787 :       if (sym->ts.type == BT_CHARACTER
    1788        28843 :           && sym->ts.deferred
    1789         3604 :           && sym->attr.dimension
    1790         1336 :           && sym->attr.allocatable)
    1791          530 :         gfc_defer_symbol_init (sym);
    1792              : 
    1793       313787 :       if (sym->attr.pointer && sym->attr.dimension && sym->ts.type != BT_CLASS)
    1794        10059 :         GFC_DECL_PTR_ARRAY_P (sym->backend_decl) = 1;
    1795              : 
    1796              :       /* Create a character length variable.  */
    1797       313787 :       if (sym->ts.type == BT_CHARACTER)
    1798              :         {
    1799              :           /* For a deferred dummy, make a new string length variable.  */
    1800        28843 :           if (sym->ts.deferred
    1801         3604 :                 &&
    1802         3604 :              (sym->ts.u.cl->passed_length == sym->ts.u.cl->backend_decl))
    1803            0 :             sym->ts.u.cl->backend_decl = NULL_TREE;
    1804              : 
    1805        28843 :           if (sym->ts.deferred && byref)
    1806              :             {
    1807              :               /* The string length of a deferred char array is stored in the
    1808              :                  parameter at sym->ts.u.cl->backend_decl as a reference and
    1809              :                  marked as a result.  Exempt this variable from generating a
    1810              :                  temporary for it.  */
    1811          747 :               if (sym->attr.result)
    1812              :                 {
    1813              :                   /* We need to insert a indirect ref for param decls.  */
    1814          660 :                   if (sym->ts.u.cl->backend_decl
    1815          660 :                       && TREE_CODE (sym->ts.u.cl->backend_decl) == PARM_DECL)
    1816              :                     {
    1817            0 :                       sym->ts.u.cl->passed_length = sym->ts.u.cl->backend_decl;
    1818            0 :                       sym->ts.u.cl->backend_decl =
    1819            0 :                         build_fold_indirect_ref (sym->ts.u.cl->backend_decl);
    1820              :                     }
    1821              :                 }
    1822              :               /* For all other parameters make sure, that they are copied so
    1823              :                  that the value and any modifications are local to the routine
    1824              :                  by generating a temporary variable.  */
    1825           87 :               else if (sym->attr.function
    1826           75 :                        && sym->ts.u.cl->passed_length == NULL
    1827            0 :                        && sym->ts.u.cl->backend_decl)
    1828              :                 {
    1829            0 :                   sym->ts.u.cl->passed_length = sym->ts.u.cl->backend_decl;
    1830            0 :                   if (POINTER_TYPE_P (TREE_TYPE (sym->ts.u.cl->passed_length)))
    1831            0 :                     sym->ts.u.cl->backend_decl
    1832            0 :                         = build_fold_indirect_ref (sym->ts.u.cl->backend_decl);
    1833              :                   else
    1834            0 :                     sym->ts.u.cl->backend_decl = NULL_TREE;
    1835              :                 }
    1836              :             }
    1837              : 
    1838        28843 :           if (sym->ts.u.cl->backend_decl == NULL_TREE)
    1839            1 :             length = gfc_create_string_length (sym);
    1840              :           else
    1841        28842 :             length = sym->ts.u.cl->backend_decl;
    1842        28843 :           if (VAR_P (length) && DECL_FILE_SCOPE_P (length))
    1843              :             {
    1844              :               /* Add the string length to the same context as the symbol.  */
    1845          688 :               if (DECL_CONTEXT (length) == NULL_TREE)
    1846              :                 {
    1847          688 :                   if (sym->backend_decl == current_function_decl
    1848          688 :                       || (DECL_CONTEXT (sym->backend_decl)
    1849              :                           == current_function_decl))
    1850          687 :                     gfc_add_decl_to_function (length);
    1851              :                   else
    1852            1 :                     gfc_add_decl_to_parent_function (length);
    1853              :                 }
    1854              : 
    1855          688 :               gcc_assert (sym->backend_decl == current_function_decl
    1856              :                           ? DECL_CONTEXT (length) == current_function_decl
    1857              :                           : (DECL_CONTEXT (sym->backend_decl)
    1858              :                              == DECL_CONTEXT (length)));
    1859              : 
    1860          688 :               gfc_defer_symbol_init (sym);
    1861              :             }
    1862              :         }
    1863              : 
    1864              :       /* Use a copy of the descriptor for dummy arrays.  */
    1865       313787 :       if ((sym->attr.dimension || sym->attr.codimension)
    1866       107474 :          && !TREE_USED (sym->backend_decl))
    1867              :         {
    1868        19865 :           decl = gfc_build_dummy_array_decl (sym, sym->backend_decl);
    1869              :           /* Prevent the dummy from being detected as unused if it is copied.  */
    1870        19865 :           if (sym->backend_decl != NULL && decl != sym->backend_decl)
    1871         5546 :             DECL_ARTIFICIAL (sym->backend_decl) = 1;
    1872        19865 :           sym->backend_decl = decl;
    1873              :         }
    1874              : 
    1875              :       /* Returning the descriptor for dummy class arrays is hazardous, because
    1876              :          some caller is expecting an expression to apply the component refs to.
    1877              :          Therefore the descriptor is only created and stored in
    1878              :          sym->backend_decl's GFC_DECL_SAVED_DESCRIPTOR.  The caller is then
    1879              :          responsible to extract it from there, when the descriptor is
    1880              :          desired.  */
    1881        27130 :       if (IS_CLASS_COARRAY_OR_ARRAY (sym)
    1882       324160 :           && (!DECL_LANG_SPECIFIC (sym->backend_decl)
    1883         8021 :               || !GFC_DECL_SAVED_DESCRIPTOR (sym->backend_decl)))
    1884              :         {
    1885         4234 :           decl = gfc_build_dummy_array_decl (sym, sym->backend_decl);
    1886              :           /* Prevent the dummy from being detected as unused if it is copied.  */
    1887         4234 :           if (sym->backend_decl != NULL && decl != sym->backend_decl)
    1888          898 :             DECL_ARTIFICIAL (sym->backend_decl) = 1;
    1889         4234 :           sym->backend_decl = decl;
    1890              :         }
    1891              : 
    1892       313787 :       TREE_USED (sym->backend_decl) = 1;
    1893       313787 :       if (sym->attr.assign && GFC_DECL_ASSIGN (sym->backend_decl) == 0)
    1894            6 :         gfc_add_assign_aux_vars (sym);
    1895              : 
    1896       313787 :       if (sym->ts.type == BT_CLASS && sym->backend_decl
    1897        27130 :           && !IS_CLASS_COARRAY_OR_ARRAY (sym))
    1898        16757 :         GFC_DECL_CLASS (sym->backend_decl) = 1;
    1899              : 
    1900       313787 :       return sym->backend_decl;
    1901              :     }
    1902              : 
    1903        15864 :   if (sym->result == sym && sym->attr.assign
    1904      1505945 :       && GFC_DECL_ASSIGN (sym->backend_decl) == 0)
    1905            1 :     gfc_add_assign_aux_vars (sym);
    1906              : 
    1907      1505940 :   if (sym->backend_decl)
    1908              :     return sym->backend_decl;
    1909              : 
    1910              :   /* Special case for array-valued named constants from intrinsic
    1911              :      procedures; those are inlined.  */
    1912       199318 :   if (sym->attr.use_assoc && sym->attr.flavor == FL_PARAMETER
    1913          138 :       && (sym->from_intmod == INTMOD_ISO_FORTRAN_ENV
    1914          138 :           || sym->from_intmod == INTMOD_ISO_C_BINDING))
    1915       199318 :     intrinsic_array_parameter = true;
    1916              : 
    1917              :   /* If use associated compilation, use the module
    1918              :      declaration.  */
    1919       199318 :   if ((sym->attr.flavor == FL_VARIABLE
    1920        17097 :        || sym->attr.flavor == FL_PARAMETER)
    1921       183774 :       && (sym->attr.use_assoc || sym->attr.used_in_submodule)
    1922         2815 :       && !intrinsic_array_parameter
    1923         2806 :       && sym->module
    1924       202124 :       && gfc_get_module_backend_decl (sym))
    1925              :     {
    1926         2690 :       if (sym->ts.type == BT_CLASS && sym->backend_decl)
    1927           39 :         GFC_DECL_CLASS(sym->backend_decl) = 1;
    1928         2690 :       return sym->backend_decl;
    1929              :     }
    1930              : 
    1931       196628 :   if (sym->attr.flavor == FL_PROCEDURE)
    1932              :     {
    1933              :       /* Catch functions. Only used for actual parameters,
    1934              :          procedure pointers and procptr initialization targets.  */
    1935        15418 :       if (sym->attr.use_assoc
    1936        14590 :           || sym->attr.used_in_submodule
    1937        14584 :           || sym->attr.intrinsic
    1938        13293 :           || sym->attr.if_source != IFSRC_DECL)
    1939              :         {
    1940         3123 :           decl = gfc_get_extern_function_decl (sym);
    1941              :         }
    1942              :       else
    1943              :         {
    1944        12295 :           if (!sym->backend_decl)
    1945        12295 :             build_function_decl (sym, false);
    1946        12295 :           decl = sym->backend_decl;
    1947              :         }
    1948        15418 :       return decl;
    1949              :     }
    1950              : 
    1951       181210 :   if (sym->ts.type == BT_UNKNOWN)
    1952            0 :     gfc_fatal_error ("%s at %L has no default type", sym->name,
    1953              :                      &sym->declared_at);
    1954              : 
    1955       181210 :   if (sym->attr.intrinsic)
    1956            0 :     gfc_internal_error ("intrinsic variable which isn't a procedure");
    1957              : 
    1958              :   /* Create string length decl first so that they can be used in the
    1959              :      type declaration.  For associate names, the target character
    1960              :      length is used. Set 'length' to a constant so that if the
    1961              :      string length is a variable, it is not finished a second time.  */
    1962       181210 :   if (sym->ts.type == BT_CHARACTER)
    1963              :     {
    1964        15777 :       if (sym->attr.associate_var
    1965         1773 :           && sym->ts.deferred
    1966          341 :           && sym->assoc && sym->assoc->target
    1967          341 :           && ((sym->assoc->target->expr_type == EXPR_VARIABLE
    1968          230 :                && sym->assoc->target->symtree->n.sym->ts.type != BT_CHARACTER)
    1969          298 :               || sym->assoc->target->expr_type != EXPR_VARIABLE))
    1970          154 :         sym->ts.u.cl->backend_decl = NULL_TREE;
    1971              : 
    1972        15777 :       if (sym->attr.associate_var
    1973         1773 :           && sym->ts.u.cl->backend_decl
    1974          627 :           && (VAR_P (sym->ts.u.cl->backend_decl)
    1975          362 :               || TREE_CODE (sym->ts.u.cl->backend_decl) == PARM_DECL))
    1976          313 :         length = gfc_index_zero_node;
    1977              :       else
    1978        15464 :         length = gfc_create_string_length (sym);
    1979              :     }
    1980              : 
    1981              :   /* Create the decl for the variable.  */
    1982       181210 :   decl = build_decl (gfc_get_location (&sym->declared_at),
    1983              :                      VAR_DECL, gfc_sym_identifier (sym), gfc_sym_type (sym));
    1984              : 
    1985              :   /* Symbols from modules should have their assembler names mangled.
    1986              :      This is done here rather than in gfc_finish_var_decl because it
    1987              :      is different for string length variables.  */
    1988       181210 :   if (sym->module || sym->fn_result_spec)
    1989              :     {
    1990        19494 :       gfc_set_decl_assembler_name (decl, gfc_sym_mangled_identifier (sym));
    1991        19494 :       if (sym->attr.use_assoc && !intrinsic_array_parameter)
    1992          113 :         DECL_IGNORED_P (decl) = 1;
    1993              :     }
    1994              : 
    1995       181210 :   if (sym->attr.select_type_temporary)
    1996              :     {
    1997         5666 :       DECL_ARTIFICIAL (decl) = 1;
    1998         5666 :       DECL_IGNORED_P (decl) = 1;
    1999              :     }
    2000              : 
    2001       181210 :   if (sym->attr.dimension || sym->attr.codimension)
    2002              :     {
    2003              :       /* Create variables to hold the non-constant bits of array info.  */
    2004        48472 :       gfc_build_qualified_array (decl, sym);
    2005              : 
    2006        48472 :       if (sym->attr.contiguous
    2007        48403 :           || ((sym->attr.allocatable || !sym->attr.dummy) && !sym->attr.pointer))
    2008        43736 :         GFC_DECL_PACKED_ARRAY (decl) = 1;
    2009              :     }
    2010              : 
    2011              :   /* Remember this variable for allocation/cleanup.  */
    2012       133189 :   if (sym->attr.dimension || sym->attr.allocatable || sym->attr.codimension
    2013       130435 :       || (sym->ts.type == BT_CLASS &&
    2014         4817 :           (CLASS_DATA (sym)->attr.dimension
    2015         2700 :            || CLASS_DATA (sym)->attr.allocatable))
    2016       126888 :       || (sym->ts.type == BT_DERIVED
    2017        30246 :           && (sym->ts.u.derived->attr.alloc_comp
    2018        23858 :               || (!sym->attr.pointer && !sym->attr.artificial && !sym->attr.save
    2019         5246 :                   && !sym->ns->proc_name->attr.is_main_program
    2020         2134 :                   && gfc_is_finalizable (sym->ts.u.derived, NULL))))
    2021              :       /* This applies a derived type default initializer.  */
    2022       301461 :       || (sym->ts.type == BT_DERIVED
    2023        23609 :           && sym->attr.save == SAVE_NONE
    2024         6559 :           && !sym->attr.data
    2025         6510 :           && !sym->attr.allocatable
    2026         6510 :           && (sym->value && !sym->ns->proc_name->attr.is_main_program)
    2027          522 :           && !(sym->attr.use_assoc && !intrinsic_array_parameter)))
    2028        61481 :     gfc_defer_symbol_init (sym);
    2029              : 
    2030              :   /* Set the vptr of unlimited polymorphic pointer variables so that
    2031              :      they do not cause segfaults in select type, when the selector
    2032              :      is an intrinsic type.  Arrays are captured above.  */
    2033       181210 :   if (sym->ts.type == BT_CLASS && UNLIMITED_POLY (sym)
    2034         1114 :       && CLASS_DATA (sym)->attr.class_pointer
    2035          679 :       && !CLASS_DATA (sym)->attr.dimension && !sym->attr.dummy
    2036          303 :       && sym->attr.flavor == FL_VARIABLE && !sym->assoc)
    2037          172 :     gfc_defer_symbol_init (sym);
    2038              : 
    2039       181210 :   if (sym->ts.type == BT_CHARACTER
    2040        15777 :       && sym->attr.allocatable
    2041         1907 :       && !sym->attr.dimension
    2042          979 :       && sym->ts.u.cl && sym->ts.u.cl->length
    2043          198 :       && sym->ts.u.cl->length->expr_type == EXPR_VARIABLE)
    2044           27 :     gfc_defer_symbol_init (sym);
    2045              : 
    2046              :   /* Associate names can use the hidden string length variable
    2047              :      of their associated target.  */
    2048       181210 :   if (sym->ts.type == BT_CHARACTER
    2049        15777 :       && TREE_CODE (length) != INTEGER_CST
    2050         3271 :       && TREE_CODE (sym->ts.u.cl->backend_decl) != INDIRECT_REF)
    2051              :     {
    2052         3211 :       length = fold_convert (gfc_charlen_type_node, length);
    2053         3211 :       gfc_finish_var_decl (length, sym);
    2054         3211 :       if (!sym->attr.associate_var
    2055         2185 :           && VAR_P (length)
    2056         2185 :           && sym->value && sym->value->expr_type != EXPR_NULL
    2057            6 :           && sym->value->ts.u.cl->length)
    2058              :         {
    2059            6 :           gfc_expr *len = sym->value->ts.u.cl->length;
    2060            6 :           DECL_INITIAL (length) = gfc_conv_initializer (len, &len->ts,
    2061            6 :                                                         TREE_TYPE (length),
    2062              :                                                         false, false, false);
    2063            6 :           DECL_INITIAL (length) = fold_convert (gfc_charlen_type_node,
    2064              :                                                 DECL_INITIAL (length));
    2065            6 :         }
    2066              :       else
    2067         3205 :         gcc_assert (!sym->value || sym->value->expr_type == EXPR_NULL);
    2068              :     }
    2069              : 
    2070       181210 :   gfc_finish_var_decl (decl, sym);
    2071              : 
    2072       181210 :   if (sym->ts.type == BT_CHARACTER)
    2073              :     /* Character variables need special handling.  */
    2074        15777 :     gfc_allocate_lang_decl (decl);
    2075              : 
    2076       181210 :   if (sym->assoc && sym->attr.subref_array_pointer)
    2077          392 :     sym->attr.pointer = 1;
    2078              : 
    2079       181210 :   if (sym->attr.pointer && sym->attr.dimension
    2080         4903 :       && !sym->ts.deferred
    2081         4614 :       && !(sym->attr.select_type_temporary
    2082         1088 :            && !sym->attr.subref_array_pointer))
    2083         3676 :     GFC_DECL_PTR_ARRAY_P (decl) = 1;
    2084              : 
    2085       181210 :   if (sym->ts.type == BT_CLASS)
    2086         4817 :     GFC_DECL_CLASS(decl) = 1;
    2087              : 
    2088       181210 :   sym->backend_decl = decl;
    2089              : 
    2090       181210 :   if (sym->attr.assign)
    2091           59 :     gfc_add_assign_aux_vars (sym);
    2092              : 
    2093       181210 :   if (intrinsic_array_parameter)
    2094              :     {
    2095            9 :       TREE_STATIC (decl) = 1;
    2096            9 :       DECL_EXTERNAL (decl) = 0;
    2097              :     }
    2098              : 
    2099       181210 :   if (TREE_STATIC (decl)
    2100        40871 :       && !(sym->attr.use_assoc && !intrinsic_array_parameter)
    2101        40871 :       && (sym->attr.save || sym->ns->proc_name->attr.is_main_program
    2102         1641 :           || !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl))
    2103         1602 :           || sym->attr.data || sym->ns->proc_name->attr.flavor == FL_MODULE)
    2104        40701 :       && (flag_coarray != GFC_FCOARRAY_LIB
    2105         2128 :           || !sym->attr.codimension || sym->attr.allocatable)
    2106       221570 :       && !(IS_PDT (sym) || IS_CLASS_PDT (sym)))
    2107              :     {
    2108              :       /* Add static initializer. For procedures, it is only needed if
    2109              :          SAVE is specified otherwise they need to be reinitialized
    2110              :          every time the procedure is entered. The TREE_STATIC is
    2111              :          in this case due to -fmax-stack-var-size=.  */
    2112              : 
    2113        39822 :       DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
    2114        39822 :                                     TREE_TYPE (decl), sym->attr.dimension
    2115        39822 :                                     || (sym->attr.codimension
    2116           35 :                                         && sym->attr.allocatable),
    2117        39295 :                                     sym->attr.pointer || sym->attr.allocatable
    2118        38882 :                                     || sym->ts.type == BT_CLASS,
    2119        39822 :                                     sym->attr.proc_pointer);
    2120              :     }
    2121              : 
    2122       181210 :   if (!TREE_STATIC (decl)
    2123       140339 :       && POINTER_TYPE_P (TREE_TYPE (decl))
    2124        18456 :       && !sym->attr.pointer
    2125        12689 :       && !sym->attr.allocatable
    2126        10497 :       && !sym->attr.proc_pointer
    2127       191707 :       && !sym->attr.select_type_temporary)
    2128         8050 :     DECL_BY_REFERENCE (decl) = 1;
    2129              : 
    2130       181210 :   if (sym->attr.associate_var)
    2131         7233 :     GFC_DECL_ASSOCIATE_VAR_P (decl) = 1;
    2132              : 
    2133              :   /* We only longer mark __def_init as read-only if it actually has an
    2134              :      initializer, it does not needlessly take up space in the
    2135              :      read-only section and can go into the BSS instead, see PR 84487.
    2136              :      Marking this as artificial means that OpenMP will treat this as
    2137              :      predetermined shared.  */
    2138              : 
    2139       181210 :   bool def_init = startswith (sym->name, "__def_init");
    2140              : 
    2141       181210 :   if (sym->attr.vtab || def_init)
    2142              :     {
    2143        19591 :       DECL_ARTIFICIAL (decl) = 1;
    2144        19591 :       if (def_init && sym->value)
    2145         3603 :         TREE_READONLY (decl) = 1;
    2146              :     }
    2147              : 
    2148              :   /* Add attributes to variables.  Functions are handled elsewhere.  */
    2149       181210 :   add_attributes_to_decl (&decl, sym);
    2150              : 
    2151       181210 :   if (sym->ts.deferred && VAR_P (length))
    2152         1831 :     decl_attributes (&length, DECL_ATTRIBUTES (decl), 0);
    2153              : 
    2154       181210 :   return decl;
    2155              : }
    2156              : 
    2157              : 
    2158              : /* Substitute a temporary variable in place of the real one.  */
    2159              : 
    2160              : void
    2161         5829 : gfc_shadow_sym (gfc_symbol * sym, tree decl, gfc_saved_var * save)
    2162              : {
    2163         5829 :   save->attr = sym->attr;
    2164         5829 :   save->decl = sym->backend_decl;
    2165              : 
    2166         5829 :   gfc_clear_attr (&sym->attr);
    2167         5829 :   sym->attr.referenced = 1;
    2168         5829 :   sym->attr.flavor = FL_VARIABLE;
    2169              : 
    2170         5829 :   sym->backend_decl = decl;
    2171         5829 : }
    2172              : 
    2173              : 
    2174              : /* Restore the original variable.  */
    2175              : 
    2176              : void
    2177         5829 : gfc_restore_sym (gfc_symbol * sym, gfc_saved_var * save)
    2178              : {
    2179         5829 :   sym->attr = save->attr;
    2180         5829 :   sym->backend_decl = save->decl;
    2181         5829 : }
    2182              : 
    2183              : 
    2184              : /* Declare a procedure pointer.  */
    2185              : 
    2186              : static tree
    2187          707 : get_proc_pointer_decl (gfc_symbol *sym)
    2188              : {
    2189          707 :   tree decl;
    2190              : 
    2191          707 :   if (sym->module || sym->fn_result_spec)
    2192              :     {
    2193          149 :       const char *name;
    2194          149 :       gfc_gsymbol *gsym;
    2195              : 
    2196          149 :       name = mangled_identifier (sym);
    2197          149 :       gsym = gfc_find_gsymbol (gfc_gsym_root, name);
    2198          149 :       if (gsym != NULL)
    2199              :         {
    2200           79 :           gfc_symbol *s;
    2201           79 :           gfc_find_symbol (sym->name, gsym->ns, 0, &s);
    2202           79 :           if (s && s->backend_decl)
    2203           79 :             return s->backend_decl;
    2204              :         }
    2205              :     }
    2206              : 
    2207          628 :   decl = sym->backend_decl;
    2208          628 :   if (decl)
    2209              :     return decl;
    2210              : 
    2211          628 :   decl = build_decl (input_location,
    2212              :                      VAR_DECL, get_identifier (sym->name),
    2213              :                      build_pointer_type (gfc_get_function_type (sym)));
    2214              : 
    2215          628 :   if (sym->module)
    2216              :     {
    2217              :       /* Apply name mangling.  */
    2218           70 :       gfc_set_decl_assembler_name (decl, gfc_sym_mangled_identifier (sym));
    2219           70 :       if (sym->attr.use_assoc)
    2220            0 :         DECL_IGNORED_P (decl) = 1;
    2221              :     }
    2222              : 
    2223          628 :   if ((sym->ns->proc_name
    2224          628 :       && sym->ns->proc_name->backend_decl == current_function_decl)
    2225           76 :       || sym->attr.contained)
    2226          552 :     gfc_add_decl_to_function (decl);
    2227           76 :   else if (sym->ns->proc_name->attr.flavor != FL_MODULE)
    2228            6 :     gfc_add_decl_to_parent_function (decl);
    2229              : 
    2230          628 :   sym->backend_decl = decl;
    2231              : 
    2232              :   /* If a variable is USE associated, it's always external.  */
    2233          628 :   if (sym->attr.use_assoc)
    2234              :     {
    2235            0 :       DECL_EXTERNAL (decl) = 1;
    2236            0 :       TREE_PUBLIC (decl) = 1;
    2237              :     }
    2238          628 :   else if (sym->module && sym->ns->proc_name->attr.flavor == FL_MODULE)
    2239              :     {
    2240              :       /* This is the declaration of a module variable.  */
    2241           70 :       TREE_PUBLIC (decl) = 1;
    2242           70 :       if (sym->attr.access == ACCESS_PRIVATE && !sym->attr.public_used)
    2243              :         {
    2244            8 :           DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
    2245            8 :           DECL_VISIBILITY_SPECIFIED (decl) = true;
    2246              :         }
    2247           70 :       TREE_STATIC (decl) = 1;
    2248              :     }
    2249              : 
    2250          628 :   if (!sym->attr.use_assoc
    2251          628 :         && (sym->attr.save != SAVE_NONE || sym->attr.data
    2252          491 :               || (sym->value && sym->ns->proc_name->attr.is_main_program)))
    2253          137 :     TREE_STATIC (decl) = 1;
    2254              : 
    2255          628 :   if (TREE_STATIC (decl) && sym->value)
    2256              :     {
    2257              :       /* Add static initializer.  */
    2258           97 :       DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
    2259           97 :                                                   TREE_TYPE (decl),
    2260           97 :                                                   sym->attr.dimension,
    2261              :                                                   false, true);
    2262              :     }
    2263              : 
    2264          628 :   add_attributes_to_decl (&decl, sym);
    2265              : 
    2266              :   /* Handle threadprivate procedure pointers.  */
    2267          628 :   if (sym->attr.threadprivate
    2268          628 :       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
    2269           12 :     set_decl_tls_model (decl, decl_default_tls_model (decl));
    2270              : 
    2271          628 :   return decl;
    2272              : }
    2273              : 
    2274              : static void
    2275              : create_function_arglist (gfc_symbol *sym);
    2276              : 
    2277              : /* Get a basic decl for an external function.  */
    2278              : 
    2279              : tree
    2280        36851 : gfc_get_extern_function_decl (gfc_symbol * sym, gfc_actual_arglist *actual_args,
    2281              :                               const char *fnspec)
    2282              : {
    2283        36851 :   tree type;
    2284        36851 :   tree fndecl;
    2285        36851 :   gfc_expr e;
    2286        36851 :   gfc_intrinsic_sym *isym;
    2287        36851 :   gfc_expr argexpr;
    2288        36851 :   char s[GFC_MAX_SYMBOL_LEN + 23]; /* "_gfortran_f2c_specific" and '\0'.  */
    2289        36851 :   tree name;
    2290        36851 :   tree mangled_name;
    2291        36851 :   gfc_gsymbol *gsym;
    2292              : 
    2293        36851 :   if (sym->backend_decl)
    2294              :     return sym->backend_decl;
    2295              : 
    2296              :   /* We should never be creating external decls for alternate entry points.
    2297              :      The procedure may be an alternate entry point, but we don't want/need
    2298              :      to know that.  */
    2299        36851 :   gcc_assert (!(sym->attr.entry || sym->attr.entry_master));
    2300              : 
    2301        36851 :   if (sym->attr.proc_pointer)
    2302          707 :     return get_proc_pointer_decl (sym);
    2303              : 
    2304              :   /* See if this is an external procedure from the same file.  If so,
    2305              :      return the backend_decl.  If we are looking at a BIND(C)
    2306              :      procedure and the symbol is not BIND(C), or vice versa, we
    2307              :      haven't found the right procedure.  */
    2308              : 
    2309        36144 :   if (sym->binding_label)
    2310              :     {
    2311         2603 :       gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
    2312         2603 :       if (gsym && !gsym->bind_c)
    2313              :         gsym = NULL;
    2314              :     }
    2315        33541 :   else if (sym->module == NULL)
    2316              :     {
    2317        20022 :       gsym = gfc_find_gsymbol (gfc_gsym_root, sym->name);
    2318        20022 :       if (gsym && gsym->bind_c)
    2319              :         gsym = NULL;
    2320              :     }
    2321              :   else
    2322              :     {
    2323              :       /* Procedure from a different module.  */
    2324              :       gsym = NULL;
    2325              :     }
    2326              : 
    2327        11016 :   if (gsym && !gsym->defined)
    2328              :     gsym = NULL;
    2329              : 
    2330              :   /* This can happen because of C binding.  */
    2331         7880 :   if (gsym && gsym->ns && gsym->ns->proc_name
    2332         7880 :       && gsym->ns->proc_name->attr.flavor == FL_MODULE)
    2333          557 :     goto module_sym;
    2334              : 
    2335        35587 :   if ((!sym->attr.use_assoc || sym->attr.if_source != IFSRC_DECL)
    2336        25804 :       && !sym->backend_decl
    2337        25804 :       && gsym && gsym->ns
    2338         7323 :       && ((gsym->type == GSYM_SUBROUTINE) || (gsym->type == GSYM_FUNCTION))
    2339         7323 :       && (gsym->ns->proc_name->backend_decl || !sym->attr.intrinsic))
    2340              :     {
    2341         7323 :       if (!gsym->ns->proc_name->backend_decl)
    2342              :         {
    2343              :           /* By construction, the external function cannot be
    2344              :              a contained procedure.  */
    2345          775 :           location_t old_loc = input_location;
    2346          775 :           push_cfun (NULL);
    2347              : 
    2348          775 :           gfc_create_function_decl (gsym->ns, true);
    2349              : 
    2350          775 :           pop_cfun ();
    2351          775 :           input_location = old_loc;
    2352              :         }
    2353              : 
    2354              :       /* If the namespace has entries, the proc_name is the
    2355              :          entry master.  Find the entry and use its backend_decl.
    2356              :          otherwise, use the proc_name backend_decl.  */
    2357         7323 :       if (gsym->ns->entries)
    2358              :         {
    2359              :           gfc_entry_list *entry = gsym->ns->entries;
    2360              : 
    2361         1409 :           for (; entry; entry = entry->next)
    2362              :             {
    2363         1409 :               if (strcmp (gsym->name, entry->sym->name) == 0)
    2364              :                 {
    2365          859 :                   sym->backend_decl = entry->sym->backend_decl;
    2366          859 :                   break;
    2367              :                 }
    2368              :             }
    2369              :         }
    2370              :       else
    2371         6464 :         sym->backend_decl = gsym->ns->proc_name->backend_decl;
    2372              : 
    2373         7323 :       if (sym->backend_decl)
    2374              :         {
    2375              :           /* Avoid problems of double deallocation of the backend declaration
    2376              :              later in gfc_trans_use_stmts; cf. PR 45087.  */
    2377         7323 :           if (sym->attr.if_source != IFSRC_DECL && sym->attr.use_assoc)
    2378            0 :             sym->attr.use_assoc = 0;
    2379              : 
    2380         7323 :           return sym->backend_decl;
    2381              :         }
    2382              :     }
    2383              : 
    2384              :   /* See if this is a module procedure from the same file.  If so,
    2385              :      return the backend_decl.  */
    2386        28264 :   if (sym->module)
    2387        14389 :     gsym =  gfc_find_gsymbol (gfc_gsym_root, sym->module);
    2388              : 
    2389        28821 : module_sym:
    2390        28821 :   if (gsym && gsym->ns
    2391        11572 :       && (gsym->type == GSYM_MODULE
    2392          557 :           || (gsym->ns->proc_name && gsym->ns->proc_name->attr.flavor == FL_MODULE)))
    2393              :     {
    2394        11572 :       gfc_symbol *s;
    2395              : 
    2396        11572 :       s = NULL;
    2397        11572 :       if (gsym->type == GSYM_MODULE)
    2398        11015 :         gfc_find_symbol (sym->name, gsym->ns, 0, &s);
    2399              :       else
    2400          557 :         gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &s);
    2401              : 
    2402        11572 :       if (s && s->backend_decl)
    2403              :         {
    2404         9540 :           if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
    2405         1162 :             gfc_copy_dt_decls_ifequal (s->ts.u.derived, sym->ts.u.derived,
    2406              :                                        true);
    2407         8378 :           else if (sym->ts.type == BT_CHARACTER)
    2408          411 :             sym->ts.u.cl->backend_decl = s->ts.u.cl->backend_decl;
    2409         9540 :           sym->backend_decl = s->backend_decl;
    2410         9540 :           return sym->backend_decl;
    2411              :         }
    2412              :     }
    2413              : 
    2414        19281 :   if (sym->attr.intrinsic)
    2415              :     {
    2416              :       /* Call the resolution function to get the actual name.  This is
    2417              :          a nasty hack which relies on the resolution functions only looking
    2418              :          at the first argument.  We pass NULL for the second argument
    2419              :          otherwise things like AINT get confused.  */
    2420         1292 :       isym = gfc_find_function (sym->name);
    2421         1292 :       gcc_assert (isym->resolve.f0 != NULL);
    2422              : 
    2423         1292 :       memset (&e, 0, sizeof (e));
    2424         1292 :       e.expr_type = EXPR_FUNCTION;
    2425              : 
    2426         1292 :       memset (&argexpr, 0, sizeof (argexpr));
    2427         1292 :       gcc_assert (isym->formal);
    2428         1292 :       argexpr.ts = isym->formal->ts;
    2429              : 
    2430         1292 :       if (isym->formal->next == NULL)
    2431         1045 :         isym->resolve.f1 (&e, &argexpr);
    2432              :       else
    2433              :         {
    2434          247 :           if (isym->formal->next->next == NULL)
    2435          231 :             isym->resolve.f2 (&e, &argexpr, NULL);
    2436              :           else
    2437              :             {
    2438           16 :               if (isym->formal->next->next->next == NULL)
    2439            0 :                 isym->resolve.f3 (&e, &argexpr, NULL, NULL);
    2440              :               else
    2441              :                 {
    2442              :                   /* All specific intrinsics take less than 5 arguments.  */
    2443           16 :                   gcc_assert (isym->formal->next->next->next->next == NULL);
    2444           16 :                   isym->resolve.f4 (&e, &argexpr, NULL, NULL, NULL);
    2445              :                 }
    2446              :             }
    2447              :         }
    2448              : 
    2449         1292 :       if (flag_f2c
    2450          438 :           && ((e.ts.type == BT_REAL && e.ts.kind == gfc_default_real_kind)
    2451          300 :               || e.ts.type == BT_COMPLEX))
    2452              :         {
    2453              :           /* Specific which needs a different implementation if f2c
    2454              :              calling conventions are used.  */
    2455          240 :           sprintf (s, "_gfortran_f2c_specific%s", e.value.function.name);
    2456              :         }
    2457              :       else
    2458         1052 :         sprintf (s, "_gfortran_specific%s", e.value.function.name);
    2459              : 
    2460         1292 :       name = get_identifier (s);
    2461         1292 :       mangled_name = name;
    2462              :     }
    2463              :   else
    2464              :     {
    2465        17989 :       name = gfc_sym_identifier (sym);
    2466        17989 :       mangled_name = gfc_sym_mangled_function_id (sym);
    2467              :     }
    2468              : 
    2469        19281 :   type = gfc_get_function_type (sym, actual_args, fnspec);
    2470              : 
    2471        19281 :   fndecl = build_decl (gfc_get_location (&sym->declared_at),
    2472              :                        FUNCTION_DECL, name, type);
    2473              : 
    2474              :   /* Initialize DECL_EXTERNAL and TREE_PUBLIC before calling decl_attributes;
    2475              :      TREE_PUBLIC specifies whether a function is globally addressable (i.e.
    2476              :      the opposite of declaring a function as static in C).  */
    2477        19281 :   DECL_EXTERNAL (fndecl) = 1;
    2478        19281 :   TREE_PUBLIC (fndecl) = 1;
    2479              : 
    2480        19281 :   add_attributes_to_decl (&fndecl, sym);
    2481              : 
    2482        19281 :   gfc_set_decl_assembler_name (fndecl, mangled_name);
    2483              : 
    2484              :   /* Set the context of this decl.  */
    2485        19281 :   if (0 && sym->ns && sym->ns->proc_name)
    2486              :     {
    2487              :       /* TODO: Add external decls to the appropriate scope.  */
    2488              :       DECL_CONTEXT (fndecl) = sym->ns->proc_name->backend_decl;
    2489              :     }
    2490              :   else
    2491              :     {
    2492              :       /* Global declaration, e.g. intrinsic subroutine.  */
    2493        19281 :       DECL_CONTEXT (fndecl) = NULL_TREE;
    2494              :     }
    2495              : 
    2496              :   /* Set attributes for PURE functions. A call to PURE function in the
    2497              :      Fortran 95 sense is both pure and without side effects in the C
    2498              :      sense.  */
    2499        19281 :   if (sym->attr.pure || sym->attr.implicit_pure)
    2500              :     {
    2501         2084 :       if (sym->attr.function && !gfc_return_by_reference (sym))
    2502         1854 :         DECL_PURE_P (fndecl) = 1;
    2503              :       /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
    2504              :          parameters and don't use alternate returns (is this
    2505              :          allowed?). In that case, calls to them are meaningless, and
    2506              :          can be optimized away. See also in build_function_decl().  */
    2507         2084 :       TREE_SIDE_EFFECTS (fndecl) = 0;
    2508              :     }
    2509              : 
    2510              :   /* Mark non-returning functions.  */
    2511        19281 :   if (sym->attr.noreturn || sym->attr.ext_attr & (1 << EXT_ATTR_NORETURN))
    2512          111 :       TREE_THIS_VOLATILE(fndecl) = 1;
    2513              : 
    2514        19281 :   sym->backend_decl = fndecl;
    2515              : 
    2516        19281 :   if (DECL_CONTEXT (fndecl) == NULL_TREE)
    2517        19281 :     pushdecl_top_level (fndecl);
    2518              : 
    2519        19281 :   if (sym->formal_ns
    2520        17177 :       && sym->formal_ns->proc_name == sym)
    2521              :     {
    2522        17177 :       if (sym->formal_ns->omp_declare_simd)
    2523           15 :         gfc_trans_omp_declare_simd (sym->formal_ns);
    2524        17177 :       if (flag_openmp)
    2525              :         {
    2526              :           // We need DECL_ARGUMENTS to put attributes on, in case some arguments
    2527              :           // need adjustment
    2528         1812 :           create_function_arglist (sym->formal_ns->proc_name);
    2529         1812 :           gfc_trans_omp_declare_variant (sym->formal_ns, sym->ns);
    2530              :         }
    2531              :     }
    2532              : 
    2533        19281 :   return fndecl;
    2534              : }
    2535              : 
    2536              : 
    2537              : /* Create a declaration for a procedure.  For external functions (in the C
    2538              :    sense) use gfc_get_extern_function_decl.  HAS_ENTRIES is true if this is
    2539              :    a master function with alternate entry points.  */
    2540              : 
    2541              : static void
    2542        98365 : build_function_decl (gfc_symbol * sym, bool global)
    2543              : {
    2544        98365 :   tree fndecl, type;
    2545        98365 :   symbol_attribute attr;
    2546        98365 :   tree result_decl;
    2547        98365 :   gfc_formal_arglist *f;
    2548              : 
    2549       196730 :   bool module_procedure = sym->attr.module_procedure
    2550          446 :                           && sym->ns
    2551          446 :                           && sym->ns->proc_name
    2552        98811 :                           && sym->ns->proc_name->attr.flavor == FL_MODULE;
    2553              : 
    2554        98365 :   gcc_assert (!sym->attr.external || module_procedure);
    2555              : 
    2556        98365 :   if (sym->backend_decl)
    2557        12295 :     return;
    2558              : 
    2559              :   /* Set the line and filename.  sym->declared_at seems to point to the
    2560              :      last statement for subroutines, but it'll do for now.  */
    2561        86070 :   input_location = gfc_get_location (&sym->declared_at);
    2562              : 
    2563              :   /* Allow only one nesting level.  Allow public declarations.  */
    2564        86070 :   gcc_assert (current_function_decl == NULL_TREE
    2565              :               || DECL_FILE_SCOPE_P (current_function_decl)
    2566              :               || (TREE_CODE (DECL_CONTEXT (current_function_decl))
    2567              :                   == FUNCTION_DECL)
    2568              :               || (TREE_CODE (DECL_CONTEXT (current_function_decl))
    2569              :                   == NAMESPACE_DECL));
    2570              : 
    2571        86070 :   type = gfc_get_function_type (sym);
    2572        86070 :   fndecl = build_decl (input_location,
    2573              :                        FUNCTION_DECL, gfc_sym_identifier (sym), type);
    2574              : 
    2575        86070 :   attr = sym->attr;
    2576              : 
    2577              :   /* Initialize DECL_EXTERNAL and TREE_PUBLIC before calling decl_attributes;
    2578              :      TREE_PUBLIC specifies whether a function is globally addressable (i.e.
    2579              :      the opposite of declaring a function as static in C).  */
    2580        86070 :   DECL_EXTERNAL (fndecl) = 0;
    2581              : 
    2582        86070 :   if (sym->attr.access == ACCESS_UNKNOWN && sym->module
    2583        24571 :       && (sym->ns->default_access == ACCESS_PRIVATE
    2584        22590 :           || (sym->ns->default_access == ACCESS_UNKNOWN
    2585        22577 :               && flag_module_private)))
    2586         1981 :     sym->attr.access = ACCESS_PRIVATE;
    2587              : 
    2588        86070 :   if (!current_function_decl
    2589        63098 :       && !sym->attr.entry_master && !sym->attr.is_main_program
    2590        36295 :       && (sym->attr.access != ACCESS_PRIVATE || sym->binding_label
    2591         2171 :           || sym->attr.public_used))
    2592        35484 :     TREE_PUBLIC (fndecl) = 1;
    2593              : 
    2594        86070 :   if (sym->attr.referenced || sym->attr.entry_master)
    2595        39990 :     TREE_USED (fndecl) = 1;
    2596              : 
    2597        86070 :   add_attributes_to_decl (&fndecl, sym);
    2598              : 
    2599              :   /* Figure out the return type of the declared function, and build a
    2600              :      RESULT_DECL for it.  If this is a subroutine with alternate
    2601              :      returns, build a RESULT_DECL for it.  */
    2602        86070 :   result_decl = NULL_TREE;
    2603              :   /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)).  */
    2604        86070 :   if (sym->attr.function)
    2605              :     {
    2606        16049 :       if (gfc_return_by_reference (sym))
    2607         3147 :         type = void_type_node;
    2608              :       else
    2609              :         {
    2610        12902 :           if (sym->result != sym)
    2611         6304 :             result_decl = gfc_sym_identifier (sym->result);
    2612              : 
    2613        12902 :           type = TREE_TYPE (TREE_TYPE (fndecl));
    2614              :         }
    2615              :     }
    2616              :   else
    2617              :     {
    2618              :       /* Look for alternate return placeholders.  */
    2619        70021 :       int has_alternate_returns = 0;
    2620       148958 :       for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
    2621              :         {
    2622        79006 :           if (f->sym == NULL)
    2623              :             {
    2624              :               has_alternate_returns = 1;
    2625              :               break;
    2626              :             }
    2627              :         }
    2628              : 
    2629        70021 :       if (has_alternate_returns)
    2630           69 :         type = integer_type_node;
    2631              :       else
    2632        69952 :         type = void_type_node;
    2633              :     }
    2634              : 
    2635        86070 :   result_decl = build_decl (input_location,
    2636              :                             RESULT_DECL, result_decl, type);
    2637        86070 :   DECL_ARTIFICIAL (result_decl) = 1;
    2638        86070 :   DECL_IGNORED_P (result_decl) = 1;
    2639        86070 :   DECL_CONTEXT (result_decl) = fndecl;
    2640        86070 :   DECL_RESULT (fndecl) = result_decl;
    2641              : 
    2642              :   /* Don't call layout_decl for a RESULT_DECL.
    2643              :      layout_decl (result_decl, 0);  */
    2644              : 
    2645              :   /* TREE_STATIC means the function body is defined here.  */
    2646        86070 :   TREE_STATIC (fndecl) = 1;
    2647              : 
    2648              :   /* Set attributes for PURE functions. A call to a PURE function in the
    2649              :      Fortran 95 sense is both pure and without side effects in the C
    2650              :      sense.  */
    2651        86070 :   if (sym->attr.pure || sym->attr.implicit_pure)
    2652              :     {
    2653              :       /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
    2654              :          including an alternate return. In that case it can also be
    2655              :          marked as PURE. See also in gfc_get_extern_function_decl().  */
    2656        18953 :       if (attr.function && !gfc_return_by_reference (sym))
    2657         4513 :         DECL_PURE_P (fndecl) = 1;
    2658        18953 :       TREE_SIDE_EFFECTS (fndecl) = 0;
    2659              :     }
    2660              : 
    2661              :   /* Mark noinline functions.  */
    2662        86070 :   if (attr.ext_attr & (1 << EXT_ATTR_NOINLINE))
    2663            1 :     DECL_UNINLINABLE (fndecl) = 1;
    2664              : 
    2665              :   /* Mark noreturn functions.  */
    2666        86070 :   if (attr.ext_attr & (1 << EXT_ATTR_NORETURN))
    2667            8 :     TREE_THIS_VOLATILE (fndecl) = 1;
    2668              : 
    2669              :   /* Mark weak functions.  */
    2670        86070 :   if (attr.ext_attr & (1 << EXT_ATTR_WEAK))
    2671            6 :     declare_weak (fndecl);
    2672              : 
    2673              :   /* Layout the function declaration and put it in the binding level
    2674              :      of the current function.  */
    2675              : 
    2676        86070 :   if (global)
    2677          778 :     pushdecl_top_level (fndecl);
    2678              :   else
    2679        85292 :     pushdecl (fndecl);
    2680              : 
    2681              :   /* Perform name mangling if this is a top level or module procedure.  */
    2682        86070 :   if (current_function_decl == NULL_TREE)
    2683        63098 :     gfc_set_decl_assembler_name (fndecl, gfc_sym_mangled_function_id (sym));
    2684              : 
    2685        86070 :   sym->backend_decl = fndecl;
    2686              : }
    2687              : 
    2688              : 
    2689              : /* Create the DECL_ARGUMENTS for a procedure.
    2690              :    NOTE: The arguments added here must match the argument type created by
    2691              :    gfc_get_function_type ().  */
    2692              : 
    2693              : static void
    2694        87882 : create_function_arglist (gfc_symbol * sym)
    2695              : {
    2696        87882 :   tree fndecl;
    2697        87882 :   gfc_formal_arglist *f;
    2698        87882 :   tree typelist, hidden_typelist, optval_typelist;
    2699        87882 :   tree arglist, hidden_arglist, optval_arglist;
    2700        87882 :   tree type;
    2701        87882 :   tree parm;
    2702              : 
    2703        87882 :   fndecl = sym->backend_decl;
    2704              : 
    2705              :   /* Build formal argument list. Make sure that their TREE_CONTEXT is
    2706              :      the new FUNCTION_DECL node.  */
    2707        87882 :   arglist = NULL_TREE;
    2708        87882 :   hidden_arglist = NULL_TREE;
    2709        87882 :   optval_arglist = NULL_TREE;
    2710        87882 :   typelist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
    2711              : 
    2712        87882 :   if (sym->attr.entry_master)
    2713              :     {
    2714          667 :       type = TREE_VALUE (typelist);
    2715          667 :       parm = build_decl (input_location,
    2716              :                          PARM_DECL, get_identifier ("__entry"), type);
    2717              : 
    2718          667 :       DECL_CONTEXT (parm) = fndecl;
    2719          667 :       DECL_ARG_TYPE (parm) = type;
    2720          667 :       TREE_READONLY (parm) = 1;
    2721          667 :       gfc_finish_decl (parm);
    2722          667 :       DECL_ARTIFICIAL (parm) = 1;
    2723              : 
    2724          667 :       arglist = chainon (arglist, parm);
    2725          667 :       typelist = TREE_CHAIN (typelist);
    2726              :     }
    2727              : 
    2728        87882 :   if (gfc_return_by_reference (sym))
    2729              :     {
    2730         3189 :       tree type = TREE_VALUE (typelist), length = NULL;
    2731              : 
    2732         3189 :       if (sym->ts.type == BT_CHARACTER)
    2733              :         {
    2734              :           /* Length of character result.  */
    2735         1638 :           tree len_type = TREE_VALUE (TREE_CHAIN (typelist));
    2736              : 
    2737         1638 :           length = build_decl (input_location,
    2738              :                                PARM_DECL,
    2739              :                                get_identifier (".__result"),
    2740              :                                len_type);
    2741         1638 :           if (POINTER_TYPE_P (len_type))
    2742              :             {
    2743          281 :               sym->ts.u.cl->passed_length = length;
    2744          281 :               TREE_USED (length) = 1;
    2745              :             }
    2746         1357 :           else if (!sym->ts.u.cl->length)
    2747              :             {
    2748          157 :               sym->ts.u.cl->backend_decl = length;
    2749          157 :               TREE_USED (length) = 1;
    2750              :             }
    2751         1638 :           gcc_assert (TREE_CODE (length) == PARM_DECL);
    2752         1638 :           DECL_CONTEXT (length) = fndecl;
    2753         1638 :           DECL_ARG_TYPE (length) = len_type;
    2754         1638 :           TREE_READONLY (length) = 1;
    2755         1638 :           DECL_ARTIFICIAL (length) = 1;
    2756         1638 :           gfc_finish_decl (length);
    2757         1638 :           if (sym->ts.u.cl->backend_decl == NULL
    2758          634 :               || sym->ts.u.cl->backend_decl == length)
    2759              :             {
    2760         1161 :               gfc_symbol *arg;
    2761         1161 :               tree backend_decl;
    2762              : 
    2763         1161 :               if (sym->ts.u.cl->backend_decl == NULL)
    2764              :                 {
    2765         1004 :                   tree len = build_decl (input_location,
    2766              :                                          VAR_DECL,
    2767              :                                          get_identifier ("..__result"),
    2768              :                                          gfc_charlen_type_node);
    2769         1004 :                   DECL_ARTIFICIAL (len) = 1;
    2770         1004 :                   TREE_USED (len) = 1;
    2771         1004 :                   sym->ts.u.cl->backend_decl = len;
    2772              :                 }
    2773              : 
    2774              :               /* Make sure PARM_DECL type doesn't point to incomplete type.  */
    2775         1161 :               arg = sym->result ? sym->result : sym;
    2776         1161 :               backend_decl = arg->backend_decl;
    2777              :               /* Temporary clear it, so that gfc_sym_type creates complete
    2778              :                  type.  */
    2779         1161 :               arg->backend_decl = NULL;
    2780         1161 :               type = gfc_sym_type (arg);
    2781         1161 :               arg->backend_decl = backend_decl;
    2782         1161 :               type = build_reference_type (type);
    2783              :             }
    2784              :         }
    2785              : 
    2786         3189 :       parm = build_decl (input_location,
    2787              :                          PARM_DECL, get_identifier ("__result"), type);
    2788              : 
    2789         3189 :       DECL_CONTEXT (parm) = fndecl;
    2790         3189 :       DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
    2791         3189 :       TREE_READONLY (parm) = 1;
    2792         3189 :       DECL_ARTIFICIAL (parm) = 1;
    2793         3189 :       gfc_finish_decl (parm);
    2794              : 
    2795         3189 :       arglist = chainon (arglist, parm);
    2796         3189 :       typelist = TREE_CHAIN (typelist);
    2797              : 
    2798         3189 :       if (sym->ts.type == BT_CHARACTER)
    2799              :         {
    2800         1638 :           gfc_allocate_lang_decl (parm);
    2801         1638 :           arglist = chainon (arglist, length);
    2802         1638 :           typelist = TREE_CHAIN (typelist);
    2803              :         }
    2804              :     }
    2805              : 
    2806        87882 :   hidden_typelist = typelist;
    2807       191610 :   for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
    2808       103728 :     if (f->sym != NULL)      /* Ignore alternate returns.  */
    2809       103629 :       hidden_typelist = TREE_CHAIN (hidden_typelist);
    2810              : 
    2811              :   /* Advance hidden_typelist over optional+value argument presence flags.  */
    2812        87882 :   optval_typelist = hidden_typelist;
    2813       191610 :   for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
    2814       103728 :     if (f->sym != NULL
    2815       103629 :         && f->sym->attr.optional && f->sym->attr.value
    2816          512 :         && !f->sym->attr.dimension && f->sym->ts.type != BT_CLASS)
    2817          512 :       hidden_typelist = TREE_CHAIN (hidden_typelist);
    2818              : 
    2819       191610 :   for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
    2820              :     {
    2821       103728 :       char name[GFC_MAX_SYMBOL_LEN + 2];
    2822              : 
    2823              :       /* Ignore alternate returns.  */
    2824       103728 :       if (f->sym == NULL)
    2825           99 :         continue;
    2826              : 
    2827       103629 :       type = TREE_VALUE (typelist);
    2828              : 
    2829       103629 :       if (f->sym->ts.type == BT_CHARACTER
    2830         9553 :           && (!sym->attr.is_bind_c || sym->attr.entry_master))
    2831              :         {
    2832         8231 :           tree len_type = TREE_VALUE (hidden_typelist);
    2833         8231 :           tree length = NULL_TREE;
    2834         8231 :           if (!f->sym->ts.deferred)
    2835         7363 :             gcc_assert (len_type == gfc_charlen_type_node);
    2836              :           else
    2837          868 :             gcc_assert (POINTER_TYPE_P (len_type));
    2838              : 
    2839         8231 :           strcpy (&name[1], f->sym->name);
    2840         8231 :           name[0] = '_';
    2841         8231 :           length = build_decl (input_location,
    2842              :                                PARM_DECL, get_identifier (name), len_type);
    2843              : 
    2844         8231 :           hidden_arglist = chainon (hidden_arglist, length);
    2845         8231 :           DECL_CONTEXT (length) = fndecl;
    2846         8231 :           DECL_ARTIFICIAL (length) = 1;
    2847         8231 :           DECL_ARG_TYPE (length) = len_type;
    2848         8231 :           TREE_READONLY (length) = 1;
    2849         8231 :           gfc_finish_decl (length);
    2850              : 
    2851              :           /* Marking the length DECL_HIDDEN_STRING_LENGTH will lead
    2852              :              to tail calls being disabled.  Only do that if we
    2853              :              potentially have broken callers.  */
    2854         8231 :           if (flag_tail_call_workaround
    2855         8231 :               && f->sym->ts.u.cl
    2856         7711 :               && f->sym->ts.u.cl->length
    2857         2616 :               && f->sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
    2858         2351 :               && (flag_tail_call_workaround == 2
    2859         2351 :                   || f->sym->ns->implicit_interface_calls))
    2860           84 :             DECL_HIDDEN_STRING_LENGTH (length) = 1;
    2861              : 
    2862              :           /* Remember the passed value.  */
    2863         8231 :           if (!f->sym->ts.u.cl ||  f->sym->ts.u.cl->passed_length)
    2864              :             {
    2865              :               /* This can happen if the same type is used for multiple
    2866              :                  arguments. We need to copy cl as otherwise
    2867              :                  cl->passed_length gets overwritten.  */
    2868          622 :               f->sym->ts.u.cl = gfc_new_charlen (f->sym->ns, f->sym->ts.u.cl);
    2869              :             }
    2870         8231 :           f->sym->ts.u.cl->passed_length = length;
    2871              : 
    2872              :           /* Use the passed value for assumed length variables.  */
    2873         8231 :           if (!f->sym->ts.u.cl->length)
    2874              :             {
    2875         5615 :               TREE_USED (length) = 1;
    2876         5615 :               gcc_assert (!f->sym->ts.u.cl->backend_decl);
    2877         5615 :               f->sym->ts.u.cl->backend_decl = length;
    2878              :             }
    2879              : 
    2880         8231 :           hidden_typelist = TREE_CHAIN (hidden_typelist);
    2881              : 
    2882         8231 :           if (f->sym->ts.u.cl->backend_decl == NULL
    2883         7933 :               || f->sym->ts.u.cl->backend_decl == length)
    2884              :             {
    2885         5913 :               if (POINTER_TYPE_P (len_type))
    2886          768 :                 f->sym->ts.u.cl->backend_decl
    2887          768 :                   = build_fold_indirect_ref_loc (input_location, length);
    2888         5145 :               else if (f->sym->ts.u.cl->backend_decl == NULL)
    2889          298 :                 gfc_create_string_length (f->sym);
    2890              : 
    2891              :               /* Make sure PARM_DECL type doesn't point to incomplete type.  */
    2892         5913 :               if (f->sym->attr.flavor == FL_PROCEDURE)
    2893           14 :                 type = build_pointer_type (gfc_get_function_type (f->sym));
    2894              :               else
    2895         5899 :                 type = gfc_sym_type (f->sym);
    2896              :             }
    2897              :         }
    2898              :       /* For scalar intrinsic types or derived types, VALUE passes the value,
    2899              :          hence, the optional status cannot be transferred via a NULL pointer.
    2900              :          Thus, we will use a hidden argument in that case.  */
    2901       103629 :       if (f->sym->attr.optional && f->sym->attr.value
    2902          512 :           && !f->sym->attr.dimension && f->sym->ts.type != BT_CLASS)
    2903              :         {
    2904          512 :           tree tmp;
    2905          512 :           strcpy (&name[1], f->sym->name);
    2906          512 :           name[0] = '.';
    2907          512 :           tmp = build_decl (input_location,
    2908              :                             PARM_DECL, get_identifier (name),
    2909              :                             boolean_type_node);
    2910              : 
    2911          512 :           optval_arglist = chainon (optval_arglist, tmp);
    2912          512 :           DECL_CONTEXT (tmp) = fndecl;
    2913          512 :           DECL_ARTIFICIAL (tmp) = 1;
    2914          512 :           DECL_ARG_TYPE (tmp) = boolean_type_node;
    2915          512 :           TREE_READONLY (tmp) = 1;
    2916          512 :           gfc_finish_decl (tmp);
    2917              : 
    2918              :           /* The presence flag must be boolean.  */
    2919          512 :           gcc_assert (TREE_VALUE (optval_typelist) == boolean_type_node);
    2920          512 :           optval_typelist = TREE_CHAIN (optval_typelist);
    2921              :         }
    2922              : 
    2923              :       /* For non-constant length array arguments, make sure they use
    2924              :          a different type node from TYPE_ARG_TYPES type.  */
    2925       103629 :       if (f->sym->attr.dimension
    2926        22889 :           && type == TREE_VALUE (typelist)
    2927        21577 :           && TREE_CODE (type) == POINTER_TYPE
    2928         9808 :           && GFC_ARRAY_TYPE_P (type)
    2929         7960 :           && f->sym->as->type != AS_ASSUMED_SIZE
    2930       109779 :           && ! COMPLETE_TYPE_P (TREE_TYPE (type)))
    2931              :         {
    2932         2618 :           if (f->sym->attr.flavor == FL_PROCEDURE)
    2933            0 :             type = build_pointer_type (gfc_get_function_type (f->sym));
    2934              :           else
    2935         2618 :             type = gfc_sym_type (f->sym);
    2936              :         }
    2937              : 
    2938       103629 :       if (f->sym->attr.proc_pointer)
    2939          146 :         type = build_pointer_type (type);
    2940              : 
    2941       103629 :       if (f->sym->attr.volatile_)
    2942            5 :         type = build_qualified_type (type, TYPE_QUAL_VOLATILE);
    2943              : 
    2944              :       /* Build the argument declaration. For C descriptors, we use a
    2945              :          '_'-prefixed name for the parm_decl and inside the proc the
    2946              :          sym->name. */
    2947       103629 :       tree parm_name;
    2948       103629 :       if (sym->attr.is_bind_c && is_CFI_desc (f->sym, NULL))
    2949              :         {
    2950         1819 :           strcpy (&name[1], f->sym->name);
    2951         1819 :           name[0] = '_';
    2952         1819 :           parm_name = get_identifier (name);
    2953              :         }
    2954              :       else
    2955       101810 :         parm_name = gfc_sym_identifier (f->sym);
    2956       103629 :       parm = build_decl (input_location, PARM_DECL, parm_name, type);
    2957              : 
    2958       103629 :       if (f->sym->attr.volatile_)
    2959              :         {
    2960            5 :           TREE_THIS_VOLATILE (parm) = 1;
    2961            5 :           TREE_SIDE_EFFECTS (parm) = 1;
    2962              :         }
    2963              : 
    2964              :       /* Fill in arg stuff.  */
    2965       103629 :       DECL_CONTEXT (parm) = fndecl;
    2966       103629 :       DECL_ARG_TYPE (parm) = TREE_VALUE (typelist);
    2967              :       /* All implementation args except for VALUE are read-only.  */
    2968       103629 :       if (!f->sym->attr.value)
    2969        94698 :         TREE_READONLY (parm) = 1;
    2970       103629 :       if (POINTER_TYPE_P (type)
    2971        95376 :           && (!f->sym->attr.proc_pointer
    2972        95230 :               && f->sym->attr.flavor != FL_PROCEDURE))
    2973        94448 :         DECL_BY_REFERENCE (parm) = 1;
    2974       103629 :       if (f->sym->attr.optional)
    2975              :         {
    2976         6095 :           gfc_allocate_lang_decl (parm);
    2977         6095 :           GFC_DECL_OPTIONAL_ARGUMENT (parm) = 1;
    2978              :         }
    2979              : 
    2980       103629 :       gfc_finish_decl (parm);
    2981       103629 :       gfc_finish_decl_attrs (parm, &f->sym->attr);
    2982              : 
    2983       103629 :       f->sym->backend_decl = parm;
    2984              : 
    2985              :       /* Coarrays which are descriptorless or assumed-shape pass with
    2986              :          -fcoarray=lib the token and the offset as hidden arguments.  */
    2987       103629 :       if (flag_coarray == GFC_FCOARRAY_LIB
    2988         6899 :           && ((f->sym->ts.type != BT_CLASS && f->sym->attr.codimension
    2989         1526 :                && !f->sym->attr.allocatable)
    2990         5395 :               || (f->sym->ts.type == BT_CLASS
    2991           27 :                   && CLASS_DATA (f->sym)->attr.codimension
    2992           22 :                   && !CLASS_DATA (f->sym)->attr.allocatable)))
    2993              :         {
    2994         1522 :           tree caf_type;
    2995         1522 :           tree token;
    2996         1522 :           tree offset;
    2997              : 
    2998         1522 :           gcc_assert (f->sym->backend_decl != NULL_TREE
    2999              :                       && !sym->attr.is_bind_c);
    3000         1522 :           caf_type = f->sym->ts.type == BT_CLASS
    3001         1522 :                      ? TREE_TYPE (CLASS_DATA (f->sym)->backend_decl)
    3002         1504 :                      : TREE_TYPE (f->sym->backend_decl);
    3003              : 
    3004         1522 :           token = build_decl (input_location, PARM_DECL,
    3005              :                               create_tmp_var_name ("caf_token"),
    3006              :                               build_qualified_type (pvoid_type_node,
    3007              :                                                     TYPE_QUAL_RESTRICT));
    3008         1522 :           if ((f->sym->ts.type != BT_CLASS
    3009         1504 :                && f->sym->as->type != AS_DEFERRED)
    3010           18 :               || (f->sym->ts.type == BT_CLASS
    3011           18 :                   && CLASS_DATA (f->sym)->as->type != AS_DEFERRED))
    3012              :             {
    3013         1522 :               gcc_assert (DECL_LANG_SPECIFIC (f->sym->backend_decl) == NULL
    3014              :                           || GFC_DECL_TOKEN (f->sym->backend_decl) == NULL_TREE);
    3015         1522 :               if (DECL_LANG_SPECIFIC (f->sym->backend_decl) == NULL)
    3016         1517 :                 gfc_allocate_lang_decl (f->sym->backend_decl);
    3017         1522 :               GFC_DECL_TOKEN (f->sym->backend_decl) = token;
    3018              :             }
    3019              :           else
    3020              :             {
    3021            0 :               gcc_assert (GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) == NULL_TREE);
    3022            0 :               GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) = token;
    3023              :             }
    3024              : 
    3025         1522 :           DECL_CONTEXT (token) = fndecl;
    3026         1522 :           DECL_ARTIFICIAL (token) = 1;
    3027         1522 :           DECL_ARG_TYPE (token) = TREE_VALUE (typelist);
    3028         1522 :           TREE_READONLY (token) = 1;
    3029         1522 :           hidden_arglist = chainon (hidden_arglist, token);
    3030         1522 :           hidden_typelist = TREE_CHAIN (hidden_typelist);
    3031         1522 :           gfc_finish_decl (token);
    3032              : 
    3033         1522 :           offset = build_decl (input_location, PARM_DECL,
    3034              :                                create_tmp_var_name ("caf_offset"),
    3035              :                                gfc_array_index_type);
    3036              : 
    3037         1522 :           if ((f->sym->ts.type != BT_CLASS
    3038         1504 :                && f->sym->as->type != AS_DEFERRED)
    3039           18 :               || (f->sym->ts.type == BT_CLASS
    3040           18 :                   && CLASS_DATA (f->sym)->as->type != AS_DEFERRED))
    3041              :             {
    3042         1522 :               gcc_assert (GFC_DECL_CAF_OFFSET (f->sym->backend_decl)
    3043              :                                                == NULL_TREE);
    3044         1522 :               GFC_DECL_CAF_OFFSET (f->sym->backend_decl) = offset;
    3045              :             }
    3046              :           else
    3047              :             {
    3048            0 :               gcc_assert (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) == NULL_TREE);
    3049            0 :               GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) = offset;
    3050              :             }
    3051         1522 :           DECL_CONTEXT (offset) = fndecl;
    3052         1522 :           DECL_ARTIFICIAL (offset) = 1;
    3053         1522 :           DECL_ARG_TYPE (offset) = TREE_VALUE (typelist);
    3054         1522 :           TREE_READONLY (offset) = 1;
    3055         1522 :           hidden_arglist = chainon (hidden_arglist, offset);
    3056         1522 :           hidden_typelist = TREE_CHAIN (hidden_typelist);
    3057         1522 :           gfc_finish_decl (offset);
    3058              :         }
    3059              : 
    3060       103629 :       arglist = chainon (arglist, parm);
    3061       103629 :       typelist = TREE_CHAIN (typelist);
    3062              :     }
    3063              : 
    3064              :   /* Add hidden present status for optional+value arguments.  */
    3065        87882 :   arglist = chainon (arglist, optval_arglist);
    3066              : 
    3067              :   /* Add the hidden string length parameters, unless the procedure
    3068              :      is bind(C).  */
    3069        87882 :   if (!sym->attr.is_bind_c)
    3070        85722 :     arglist = chainon (arglist, hidden_arglist);
    3071              : 
    3072       175764 :   gcc_assert (hidden_typelist == NULL_TREE
    3073              :               || TREE_VALUE (hidden_typelist) == void_type_node);
    3074        87882 :   DECL_ARGUMENTS (fndecl) = arglist;
    3075        87882 : }
    3076              : 
    3077              : /* Do the setup necessary before generating the body of a function.  */
    3078              : 
    3079              : static void
    3080        86070 : trans_function_start (gfc_symbol * sym)
    3081              : {
    3082        86070 :   tree fndecl;
    3083              : 
    3084        86070 :   fndecl = sym->backend_decl;
    3085              : 
    3086              :   /* Let GCC know the current scope is this function.  */
    3087        86070 :   current_function_decl = fndecl;
    3088              : 
    3089              :   /* Let the world know what we're about to do.  */
    3090        86070 :   announce_function (fndecl);
    3091              : 
    3092        86070 :   if (DECL_FILE_SCOPE_P (fndecl))
    3093              :     {
    3094              :       /* Create RTL for function declaration.  */
    3095        37473 :       rest_of_decl_compilation (fndecl, 1, 0);
    3096              :     }
    3097              : 
    3098              :   /* Create RTL for function definition.  */
    3099        86070 :   make_decl_rtl (fndecl);
    3100              : 
    3101        86070 :   allocate_struct_function (fndecl, false);
    3102              : 
    3103              :   /* function.cc requires a push at the start of the function.  */
    3104        86070 :   pushlevel ();
    3105        86070 : }
    3106              : 
    3107              : /* Create thunks for alternate entry points.  */
    3108              : 
    3109              : static void
    3110          667 : build_entry_thunks (gfc_namespace * ns, bool global)
    3111              : {
    3112          667 :   gfc_formal_arglist *formal;
    3113          667 :   gfc_formal_arglist *thunk_formal;
    3114          667 :   gfc_entry_list *el;
    3115          667 :   gfc_symbol *thunk_sym;
    3116          667 :   stmtblock_t body;
    3117          667 :   tree thunk_fndecl;
    3118          667 :   tree tmp;
    3119          667 :   location_t old_loc;
    3120              : 
    3121              :   /* This should always be a toplevel function.  */
    3122          667 :   gcc_assert (current_function_decl == NULL_TREE);
    3123              : 
    3124          667 :   old_loc = input_location;
    3125         2079 :   for (el = ns->entries; el; el = el->next)
    3126              :     {
    3127         1412 :       vec<tree, va_gc> *args = NULL;
    3128         1412 :       vec<tree, va_gc> *string_args = NULL;
    3129              : 
    3130         1412 :       thunk_sym = el->sym;
    3131              : 
    3132         1412 :       build_function_decl (thunk_sym, global);
    3133         1412 :       create_function_arglist (thunk_sym);
    3134              : 
    3135         1412 :       trans_function_start (thunk_sym);
    3136              : 
    3137         1412 :       thunk_fndecl = thunk_sym->backend_decl;
    3138              : 
    3139         1412 :       gfc_init_block (&body);
    3140              : 
    3141              :       /* Pass extra parameter identifying this entry point.  */
    3142         1412 :       tmp = build_int_cst (gfc_array_index_type, el->id);
    3143         1412 :       vec_safe_push (args, tmp);
    3144              : 
    3145              :       /* When the master returns by reference, pass the result reference
    3146              :          and (for CHARACTER) the string length to the master call.  If the
    3147              :          thunk itself also returns by reference these are forwarded from
    3148              :          its own argument list; otherwise (bind(c) CHARACTER entry) we
    3149              :          create local temporaries and load the value after the call.  */
    3150         1412 :       tree result_ref = NULL_TREE;
    3151         1412 :       if (thunk_sym->attr.function
    3152         1412 :           && gfc_return_by_reference (ns->proc_name))
    3153              :         {
    3154          300 :           if (gfc_return_by_reference (thunk_sym))
    3155              :             {
    3156          276 :               tree ref = DECL_ARGUMENTS (current_function_decl);
    3157          276 :               vec_safe_push (args, ref);
    3158          276 :               if (ns->proc_name->ts.type == BT_CHARACTER)
    3159          160 :                 vec_safe_push (args, DECL_CHAIN (ref));
    3160              :             }
    3161              :           else
    3162              :             {
    3163              :               /* The thunk is bind(c) and returns CHARACTER by value, but
    3164              :                  the master returns by reference.  Create a local buffer
    3165              :                  and length to pass to the master call.  */
    3166           24 :               tree chartype = gfc_get_char_type (thunk_sym->ts.kind);
    3167           24 :               tree len;
    3168              : 
    3169           24 :               if (thunk_sym->ts.u.cl && thunk_sym->ts.u.cl->length)
    3170              :                 {
    3171           24 :                   gfc_se se;
    3172           24 :                   gfc_init_se (&se, NULL);
    3173           24 :                   gfc_conv_expr (&se, thunk_sym->ts.u.cl->length);
    3174           24 :                   gfc_add_block_to_block (&body, &se.pre);
    3175           24 :                   len = se.expr;
    3176           24 :                   gfc_add_block_to_block (&body, &se.post);
    3177           24 :                 }
    3178              :               else
    3179            0 :                 len = build_int_cst (gfc_charlen_type_node, 1);
    3180              : 
    3181           24 :               result_ref = build_decl (input_location, VAR_DECL,
    3182              :                                        get_identifier ("__entry_result"),
    3183              :                                        build_array_type (chartype,
    3184              :                                          build_range_type (gfc_array_index_type,
    3185              :                                            gfc_index_one_node,
    3186              :                                            fold_convert (gfc_array_index_type,
    3187              :                                                          len))));
    3188           24 :               DECL_ARTIFICIAL (result_ref) = 1;
    3189           24 :               TREE_USED (result_ref) = 1;
    3190           24 :               DECL_CONTEXT (result_ref) = current_function_decl;
    3191           24 :               layout_decl (result_ref, 0);
    3192           24 :               pushdecl (result_ref);
    3193              : 
    3194           48 :               vec_safe_push (args,
    3195           24 :                              build_fold_addr_expr_loc (input_location,
    3196              :                                                        result_ref));
    3197           24 :               vec_safe_push (args, len);
    3198              :             }
    3199              :         }
    3200              : 
    3201         2977 :       for (formal = gfc_sym_get_dummy_args (ns->proc_name); formal;
    3202         1565 :            formal = formal->next)
    3203              :         {
    3204              :           /* Ignore alternate returns.  */
    3205         1565 :           if (formal->sym == NULL)
    3206           36 :             continue;
    3207              : 
    3208              :           /* We don't have a clever way of identifying arguments, so resort to
    3209              :              a brute-force search.  */
    3210         1529 :           for (thunk_formal = gfc_sym_get_dummy_args (thunk_sym);
    3211         2697 :                thunk_formal;
    3212         1168 :                thunk_formal = thunk_formal->next)
    3213              :             {
    3214         2257 :               if (thunk_formal->sym == formal->sym)
    3215              :                 break;
    3216              :             }
    3217              : 
    3218         1529 :           if (thunk_formal)
    3219              :             {
    3220              :               /* Pass the argument.  */
    3221         1089 :               DECL_ARTIFICIAL (thunk_formal->sym->backend_decl) = 1;
    3222         1089 :               vec_safe_push (args, thunk_formal->sym->backend_decl);
    3223         1089 :               if (formal->sym->ts.type == BT_CHARACTER)
    3224              :                 {
    3225           94 :                   tmp = thunk_formal->sym->ts.u.cl->backend_decl;
    3226           94 :                   vec_safe_push (string_args, tmp);
    3227              :                 }
    3228              :             }
    3229              :           else
    3230              :             {
    3231              :               /* Pass NULL for a missing argument.  */
    3232          440 :               vec_safe_push (args, null_pointer_node);
    3233          440 :               if (formal->sym->ts.type == BT_CHARACTER)
    3234              :                 {
    3235           38 :                   tmp = build_int_cst (gfc_charlen_type_node, 0);
    3236           38 :                   vec_safe_push (string_args, tmp);
    3237              :                 }
    3238              :             }
    3239              :         }
    3240              : 
    3241              :       /* Call the master function.  */
    3242         1412 :       vec_safe_splice (args, string_args);
    3243         1412 :       tmp = ns->proc_name->backend_decl;
    3244         1412 :       tmp = build_call_expr_loc_vec (input_location, tmp, args);
    3245         1412 :       if (result_ref != NULL_TREE)
    3246              :         {
    3247              :           /* The master returns by reference (void) but the bind(c) thunk
    3248              :              returns CHARACTER by value.  Execute the master call, then
    3249              :              load the first character from the local buffer.  */
    3250           24 :           gfc_add_expr_to_block (&body, tmp);
    3251           24 :           tmp = build4_loc (input_location, ARRAY_REF,
    3252           24 :                             TREE_TYPE (TREE_TYPE (result_ref)),
    3253              :                             result_ref, gfc_index_one_node,
    3254              :                             NULL_TREE, NULL_TREE);
    3255           24 :           tmp = fold_convert (TREE_TYPE (DECL_RESULT (current_function_decl)),
    3256              :                               tmp);
    3257           24 :           tmp = fold_build2_loc (input_location, MODIFY_EXPR,
    3258           24 :                              TREE_TYPE (DECL_RESULT (current_function_decl)),
    3259           24 :                              DECL_RESULT (current_function_decl), tmp);
    3260           24 :           tmp = build1_v (RETURN_EXPR, tmp);
    3261              :         }
    3262         1388 :       else if (ns->proc_name->attr.mixed_entry_master)
    3263              :         {
    3264          214 :           tree union_decl, field;
    3265          214 :           tree master_type = TREE_TYPE (ns->proc_name->backend_decl);
    3266              : 
    3267          214 :           union_decl = build_decl (input_location,
    3268              :                                    VAR_DECL, get_identifier ("__result"),
    3269          214 :                                    TREE_TYPE (master_type));
    3270          214 :           DECL_ARTIFICIAL (union_decl) = 1;
    3271          214 :           DECL_EXTERNAL (union_decl) = 0;
    3272          214 :           TREE_PUBLIC (union_decl) = 0;
    3273          214 :           TREE_USED (union_decl) = 1;
    3274          214 :           layout_decl (union_decl, 0);
    3275          214 :           pushdecl (union_decl);
    3276              : 
    3277          214 :           DECL_CONTEXT (union_decl) = current_function_decl;
    3278          214 :           tmp = fold_build2_loc (input_location, MODIFY_EXPR,
    3279          214 :                                  TREE_TYPE (union_decl), union_decl, tmp);
    3280          214 :           gfc_add_expr_to_block (&body, tmp);
    3281              : 
    3282          214 :           for (field = TYPE_FIELDS (TREE_TYPE (union_decl));
    3283          348 :                field; field = DECL_CHAIN (field))
    3284          348 :             if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
    3285          348 :                 thunk_sym->result->name) == 0)
    3286              :               break;
    3287            0 :           gcc_assert (field != NULL_TREE);
    3288          214 :           tmp = fold_build3_loc (input_location, COMPONENT_REF,
    3289          214 :                                  TREE_TYPE (field), union_decl, field,
    3290              :                                  NULL_TREE);
    3291          214 :           tmp = fold_build2_loc (input_location, MODIFY_EXPR,
    3292          214 :                              TREE_TYPE (DECL_RESULT (current_function_decl)),
    3293          214 :                              DECL_RESULT (current_function_decl), tmp);
    3294          214 :           tmp = build1_v (RETURN_EXPR, tmp);
    3295              :         }
    3296         1174 :       else if (TREE_TYPE (DECL_RESULT (current_function_decl))
    3297         1174 :                != void_type_node)
    3298              :         {
    3299          705 :           tmp = fold_build2_loc (input_location, MODIFY_EXPR,
    3300          705 :                              TREE_TYPE (DECL_RESULT (current_function_decl)),
    3301          705 :                              DECL_RESULT (current_function_decl), tmp);
    3302          705 :           tmp = build1_v (RETURN_EXPR, tmp);
    3303              :         }
    3304         1412 :       gfc_add_expr_to_block (&body, tmp);
    3305              : 
    3306              :       /* Finish off this function and send it for code generation.  */
    3307         1412 :       DECL_SAVED_TREE (thunk_fndecl) = gfc_finish_block (&body);
    3308         1412 :       tmp = getdecls ();
    3309         1412 :       poplevel (1, 1);
    3310         1412 :       BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl)) = thunk_fndecl;
    3311         2824 :       DECL_SAVED_TREE (thunk_fndecl)
    3312         2824 :         = fold_build3_loc (DECL_SOURCE_LOCATION (thunk_fndecl), BIND_EXPR,
    3313         1412 :                            void_type_node, tmp, DECL_SAVED_TREE (thunk_fndecl),
    3314         1412 :                            DECL_INITIAL (thunk_fndecl));
    3315              : 
    3316              :       /* Output the GENERIC tree.  */
    3317         1412 :       dump_function (TDI_original, thunk_fndecl);
    3318              : 
    3319              :       /* Store the end of the function, so that we get good line number
    3320              :          info for the epilogue.  */
    3321         1412 :       cfun->function_end_locus = input_location;
    3322              : 
    3323              :       /* We're leaving the context of this function, so zap cfun.
    3324              :          It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
    3325              :          tree_rest_of_compilation.  */
    3326         1412 :       set_cfun (NULL);
    3327              : 
    3328         1412 :       current_function_decl = NULL_TREE;
    3329              : 
    3330         1412 :       cgraph_node::finalize_function (thunk_fndecl, true);
    3331              : 
    3332              :       /* We share the symbols in the formal argument list with other entry
    3333              :          points and the master function.  Clear them so that they are
    3334              :          recreated for each function.  */
    3335         2546 :       for (formal = gfc_sym_get_dummy_args (thunk_sym); formal;
    3336         1134 :            formal = formal->next)
    3337         1134 :         if (formal->sym != NULL)  /* Ignore alternate returns.  */
    3338              :           {
    3339         1089 :             formal->sym->backend_decl = NULL_TREE;
    3340         1089 :             if (formal->sym->ts.type == BT_CHARACTER)
    3341           94 :               formal->sym->ts.u.cl->backend_decl = NULL_TREE;
    3342              :           }
    3343              : 
    3344         1412 :       if (thunk_sym->attr.function)
    3345              :         {
    3346         1194 :           if (thunk_sym->ts.type == BT_CHARACTER)
    3347          186 :             thunk_sym->ts.u.cl->backend_decl = NULL_TREE;
    3348         1194 :           if (thunk_sym->result->ts.type == BT_CHARACTER)
    3349          186 :             thunk_sym->result->ts.u.cl->backend_decl = NULL_TREE;
    3350              :         }
    3351              :     }
    3352              : 
    3353          667 :   input_location = old_loc;
    3354          667 : }
    3355              : 
    3356              : 
    3357              : /* Create a decl for a function, and create any thunks for alternate entry
    3358              :    points. If global is true, generate the function in the global binding
    3359              :    level, otherwise in the current binding level (which can be global).  */
    3360              : 
    3361              : void
    3362        84658 : gfc_create_function_decl (gfc_namespace * ns, bool global)
    3363              : {
    3364              :   /* Create a declaration for the master function.  */
    3365        84658 :   build_function_decl (ns->proc_name, global);
    3366              : 
    3367              :   /* Compile the entry thunks.  */
    3368        84658 :   if (ns->entries)
    3369          667 :     build_entry_thunks (ns, global);
    3370              : 
    3371              :   /* Now create the read argument list.  */
    3372        84658 :   create_function_arglist (ns->proc_name);
    3373              : 
    3374        84658 :   if (ns->omp_declare_simd)
    3375           94 :     gfc_trans_omp_declare_simd (ns);
    3376              : 
    3377              :   /* Handle 'declare variant' directives.  The applicable directives might
    3378              :      be declared in a parent namespace, so this needs to be called even if
    3379              :      there are no local directives.  */
    3380        84658 :   if (flag_openmp)
    3381         8658 :     gfc_trans_omp_declare_variant (ns, NULL);
    3382        84658 : }
    3383              : 
    3384              : /* Return the decl used to hold the function return value.  If
    3385              :    parent_flag is set, the context is the parent_scope.  */
    3386              : 
    3387              : tree
    3388        12923 : gfc_get_fake_result_decl (gfc_symbol * sym, int parent_flag)
    3389              : {
    3390        12923 :   tree decl;
    3391        12923 :   tree length;
    3392        12923 :   tree this_fake_result_decl;
    3393        12923 :   tree this_function_decl;
    3394              : 
    3395        12923 :   char name[GFC_MAX_SYMBOL_LEN + 10];
    3396              : 
    3397        12923 :   if (parent_flag)
    3398              :     {
    3399          167 :       this_fake_result_decl = parent_fake_result_decl;
    3400          167 :       this_function_decl = DECL_CONTEXT (current_function_decl);
    3401              :     }
    3402              :   else
    3403              :     {
    3404        12756 :       this_fake_result_decl = current_fake_result_decl;
    3405        12756 :       this_function_decl = current_function_decl;
    3406              :     }
    3407              : 
    3408        12923 :   if (sym
    3409        12873 :       && sym->ns->proc_name->backend_decl == this_function_decl
    3410         4863 :       && sym->ns->proc_name->attr.entry_master
    3411         2392 :       && sym != sym->ns->proc_name)
    3412              :     {
    3413         1480 :       tree t = NULL, var;
    3414         1480 :       if (this_fake_result_decl != NULL)
    3415         1452 :         for (t = TREE_CHAIN (this_fake_result_decl); t; t = TREE_CHAIN (t))
    3416         1071 :           if (strcmp (IDENTIFIER_POINTER (TREE_PURPOSE (t)), sym->name) == 0)
    3417              :             break;
    3418          958 :       if (t)
    3419          577 :         return TREE_VALUE (t);
    3420          903 :       decl = gfc_get_fake_result_decl (sym->ns->proc_name, parent_flag);
    3421              : 
    3422          903 :       if (parent_flag)
    3423           14 :         this_fake_result_decl = parent_fake_result_decl;
    3424              :       else
    3425          889 :         this_fake_result_decl = current_fake_result_decl;
    3426              : 
    3427          903 :       if (decl && sym->ns->proc_name->attr.mixed_entry_master)
    3428              :         {
    3429          214 :           tree field;
    3430              : 
    3431          214 :           for (field = TYPE_FIELDS (TREE_TYPE (decl));
    3432          348 :                field; field = DECL_CHAIN (field))
    3433          348 :             if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
    3434              :                 sym->name) == 0)
    3435              :               break;
    3436              : 
    3437          214 :           gcc_assert (field != NULL_TREE);
    3438          214 :           decl = fold_build3_loc (input_location, COMPONENT_REF,
    3439          214 :                                   TREE_TYPE (field), decl, field, NULL_TREE);
    3440              :         }
    3441              : 
    3442          903 :       var = create_tmp_var_raw (TREE_TYPE (decl), sym->name);
    3443          903 :       if (parent_flag)
    3444           14 :         gfc_add_decl_to_parent_function (var);
    3445              :       else
    3446          889 :         gfc_add_decl_to_function (var);
    3447              : 
    3448          903 :       SET_DECL_VALUE_EXPR (var, decl);
    3449          903 :       DECL_HAS_VALUE_EXPR_P (var) = 1;
    3450          903 :       GFC_DECL_RESULT (var) = 1;
    3451              : 
    3452          903 :       TREE_CHAIN (this_fake_result_decl)
    3453          903 :           = tree_cons (get_identifier (sym->name), var,
    3454          903 :                        TREE_CHAIN (this_fake_result_decl));
    3455          903 :       return var;
    3456              :     }
    3457              : 
    3458        11443 :   if (this_fake_result_decl != NULL_TREE)
    3459         4002 :     return TREE_VALUE (this_fake_result_decl);
    3460              : 
    3461              :   /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
    3462              :      sym is NULL.  */
    3463         7441 :   if (!sym)
    3464              :     return NULL_TREE;
    3465              : 
    3466         7441 :   if (sym->ts.type == BT_CHARACTER)
    3467              :     {
    3468          845 :       if (sym->ts.u.cl->backend_decl == NULL_TREE)
    3469            0 :         length = gfc_create_string_length (sym);
    3470              :       else
    3471              :         length = sym->ts.u.cl->backend_decl;
    3472          845 :       if (VAR_P (length) && DECL_CONTEXT (length) == NULL_TREE)
    3473          514 :         gfc_add_decl_to_function (length);
    3474              :     }
    3475              : 
    3476         7441 :   if (gfc_return_by_reference (sym))
    3477              :     {
    3478         1606 :       decl = DECL_ARGUMENTS (this_function_decl);
    3479              : 
    3480         1606 :       if (sym->ns->proc_name->backend_decl == this_function_decl
    3481          405 :           && sym->ns->proc_name->attr.entry_master)
    3482           85 :         decl = DECL_CHAIN (decl);
    3483              : 
    3484         1606 :       TREE_USED (decl) = 1;
    3485         1606 :       if (sym->as)
    3486          807 :         decl = gfc_build_dummy_array_decl (sym, decl);
    3487              :     }
    3488              :   else
    3489              :     {
    3490        11670 :       sprintf (name, "__result_%.20s",
    3491         5835 :                IDENTIFIER_POINTER (DECL_NAME (this_function_decl)));
    3492              : 
    3493         5835 :       if (!sym->attr.mixed_entry_master && sym->attr.function)
    3494         5694 :         decl = build_decl (DECL_SOURCE_LOCATION (this_function_decl),
    3495              :                            VAR_DECL, get_identifier (name),
    3496              :                            gfc_sym_type (sym));
    3497              :       else
    3498          141 :         decl = build_decl (DECL_SOURCE_LOCATION (this_function_decl),
    3499              :                            VAR_DECL, get_identifier (name),
    3500          141 :                            TREE_TYPE (TREE_TYPE (this_function_decl)));
    3501         5835 :       DECL_ARTIFICIAL (decl) = 1;
    3502         5835 :       DECL_EXTERNAL (decl) = 0;
    3503         5835 :       TREE_PUBLIC (decl) = 0;
    3504         5835 :       TREE_USED (decl) = 1;
    3505         5835 :       GFC_DECL_RESULT (decl) = 1;
    3506         5835 :       TREE_ADDRESSABLE (decl) = 1;
    3507              : 
    3508         5835 :       layout_decl (decl, 0);
    3509         5835 :       gfc_finish_decl_attrs (decl, &sym->attr);
    3510              : 
    3511         5835 :       if (parent_flag)
    3512           27 :         gfc_add_decl_to_parent_function (decl);
    3513              :       else
    3514         5808 :         gfc_add_decl_to_function (decl);
    3515              :     }
    3516              : 
    3517         7441 :   if (parent_flag)
    3518           39 :     parent_fake_result_decl = build_tree_list (NULL, decl);
    3519              :   else
    3520         7402 :     current_fake_result_decl = build_tree_list (NULL, decl);
    3521              : 
    3522         7441 :   if (sym->attr.assign)
    3523            1 :     DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (sym->backend_decl);
    3524              : 
    3525              :   return decl;
    3526              : }
    3527              : 
    3528              : 
    3529              : /* Builds a function decl.  The remaining parameters are the types of the
    3530              :    function arguments.  Negative nargs indicates a varargs function.  */
    3531              : 
    3532              : static tree
    3533      4715643 : build_library_function_decl_1 (tree name, const char *spec,
    3534              :                                tree rettype, int nargs, va_list p)
    3535              : {
    3536      4715643 :   vec<tree, va_gc> *arglist;
    3537      4715643 :   tree fntype;
    3538      4715643 :   tree fndecl;
    3539      4715643 :   int n;
    3540              : 
    3541              :   /* Library functions must be declared with global scope.  */
    3542      4715643 :   gcc_assert (current_function_decl == NULL_TREE);
    3543              : 
    3544              :   /* Create a list of the argument types.  */
    3545      4715643 :   vec_alloc (arglist, abs (nargs));
    3546     18759366 :   for (n = abs (nargs); n > 0; n--)
    3547              :     {
    3548     14043723 :       tree argtype = va_arg (p, tree);
    3549     14043723 :       arglist->quick_push (argtype);
    3550              :     }
    3551              : 
    3552              :   /* Build the function type and decl.  */
    3553      4715643 :   if (nargs >= 0)
    3554     13515677 :     fntype = build_function_type_vec (rettype, arglist);
    3555              :   else
    3556       565632 :     fntype = build_varargs_function_type_vec (rettype, arglist);
    3557      4715643 :   if (spec)
    3558              :     {
    3559      2751906 :       tree attr_args = build_tree_list (NULL_TREE,
    3560      2751906 :                                         build_string (strlen (spec), spec));
    3561      2751906 :       tree attrs = tree_cons (get_identifier ("fn spec"),
    3562      2751906 :                               attr_args, TYPE_ATTRIBUTES (fntype));
    3563      2751906 :       fntype = build_type_attribute_variant (fntype, attrs);
    3564              :     }
    3565      4715643 :   fndecl = build_decl (input_location,
    3566              :                        FUNCTION_DECL, name, fntype);
    3567              : 
    3568              :   /* Mark this decl as external.  */
    3569      4715643 :   DECL_EXTERNAL (fndecl) = 1;
    3570      4715643 :   TREE_PUBLIC (fndecl) = 1;
    3571              : 
    3572      4715643 :   pushdecl (fndecl);
    3573              : 
    3574      4715643 :   rest_of_decl_compilation (fndecl, 1, 0);
    3575              : 
    3576      4715643 :   return fndecl;
    3577              : }
    3578              : 
    3579              : /* Builds a function decl.  The remaining parameters are the types of the
    3580              :    function arguments.  Negative nargs indicates a varargs function.  */
    3581              : 
    3582              : tree
    3583      1963737 : gfc_build_library_function_decl (tree name, tree rettype, int nargs, ...)
    3584              : {
    3585      1963737 :   tree ret;
    3586      1963737 :   va_list args;
    3587      1963737 :   va_start (args, nargs);
    3588      1963737 :   ret = build_library_function_decl_1 (name, NULL, rettype, nargs, args);
    3589      1963737 :   va_end (args);
    3590      1963737 :   return ret;
    3591              : }
    3592              : 
    3593              : /* Builds a function decl.  The remaining parameters are the types of the
    3594              :    function arguments.  Negative nargs indicates a varargs function.
    3595              :    The SPEC parameter specifies the function argument and return type
    3596              :    specification according to the fnspec function type attribute.  */
    3597              : 
    3598              : tree
    3599      2751906 : gfc_build_library_function_decl_with_spec (tree name, const char *spec,
    3600              :                                            tree rettype, int nargs, ...)
    3601              : {
    3602      2751906 :   tree ret;
    3603      2751906 :   va_list args;
    3604      2751906 :   va_start (args, nargs);
    3605      2751906 :   if (flag_checking)
    3606              :     {
    3607      2751906 :       attr_fnspec fnspec (spec, strlen (spec));
    3608      2751906 :       fnspec.verify ();
    3609              :     }
    3610      2751906 :   ret = build_library_function_decl_1 (name, spec, rettype, nargs, args);
    3611      2751906 :   va_end (args);
    3612      2751906 :   return ret;
    3613              : }
    3614              : 
    3615              : static void
    3616        31424 : gfc_build_intrinsic_function_decls (void)
    3617              : {
    3618        31424 :   tree gfc_int4_type_node = gfc_get_int_type (4);
    3619        31424 :   tree gfc_pint4_type_node = build_pointer_type (gfc_int4_type_node);
    3620        31424 :   tree gfc_int8_type_node = gfc_get_int_type (8);
    3621        31424 :   tree gfc_pint8_type_node = build_pointer_type (gfc_int8_type_node);
    3622        31424 :   tree gfc_int16_type_node = gfc_get_int_type (16);
    3623        31424 :   tree gfc_logical4_type_node = gfc_get_logical_type (4);
    3624        31424 :   tree pchar1_type_node = gfc_get_pchar_type (1);
    3625        31424 :   tree pchar4_type_node = gfc_get_pchar_type (4);
    3626              : 
    3627              :   /* String functions.  */
    3628        31424 :   gfor_fndecl_compare_string = gfc_build_library_function_decl_with_spec (
    3629              :         get_identifier (PREFIX("compare_string")), ". . R . R ",
    3630              :         integer_type_node, 4, gfc_charlen_type_node, pchar1_type_node,
    3631              :         gfc_charlen_type_node, pchar1_type_node);
    3632        31424 :   DECL_PURE_P (gfor_fndecl_compare_string) = 1;
    3633        31424 :   TREE_NOTHROW (gfor_fndecl_compare_string) = 1;
    3634              : 
    3635        31424 :   gfor_fndecl_concat_string = gfc_build_library_function_decl_with_spec (
    3636              :         get_identifier (PREFIX("concat_string")), ". . W . R . R ",
    3637              :         void_type_node, 6, gfc_charlen_type_node, pchar1_type_node,
    3638              :         gfc_charlen_type_node, pchar1_type_node,
    3639              :         gfc_charlen_type_node, pchar1_type_node);
    3640        31424 :   TREE_NOTHROW (gfor_fndecl_concat_string) = 1;
    3641              : 
    3642        31424 :   gfor_fndecl_string_len_trim = gfc_build_library_function_decl_with_spec (
    3643              :         get_identifier (PREFIX("string_len_trim")), ". . R ",
    3644              :         gfc_charlen_type_node, 2, gfc_charlen_type_node, pchar1_type_node);
    3645        31424 :   DECL_PURE_P (gfor_fndecl_string_len_trim) = 1;
    3646        31424 :   TREE_NOTHROW (gfor_fndecl_string_len_trim) = 1;
    3647              : 
    3648        31424 :   gfor_fndecl_string_index = gfc_build_library_function_decl_with_spec (
    3649              :         get_identifier (PREFIX("string_index")), ". . R . R . ",
    3650              :         gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar1_type_node,
    3651              :         gfc_charlen_type_node, pchar1_type_node, gfc_logical4_type_node);
    3652        31424 :   DECL_PURE_P (gfor_fndecl_string_index) = 1;
    3653        31424 :   TREE_NOTHROW (gfor_fndecl_string_index) = 1;
    3654              : 
    3655        31424 :   gfor_fndecl_string_scan = gfc_build_library_function_decl_with_spec (
    3656              :         get_identifier (PREFIX("string_scan")), ". . R . R . ",
    3657              :         gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar1_type_node,
    3658              :         gfc_charlen_type_node, pchar1_type_node, gfc_logical4_type_node);
    3659        31424 :   DECL_PURE_P (gfor_fndecl_string_scan) = 1;
    3660        31424 :   TREE_NOTHROW (gfor_fndecl_string_scan) = 1;
    3661              : 
    3662        31424 :   gfor_fndecl_string_verify = gfc_build_library_function_decl_with_spec (
    3663              :         get_identifier (PREFIX("string_verify")), ". . R . R . ",
    3664              :         gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar1_type_node,
    3665              :         gfc_charlen_type_node, pchar1_type_node, gfc_logical4_type_node);
    3666        31424 :   DECL_PURE_P (gfor_fndecl_string_verify) = 1;
    3667        31424 :   TREE_NOTHROW (gfor_fndecl_string_verify) = 1;
    3668              : 
    3669        31424 :   gfor_fndecl_string_trim = gfc_build_library_function_decl_with_spec (
    3670              :         get_identifier (PREFIX("string_trim")), ". W w . R ",
    3671              :         void_type_node, 4, build_pointer_type (gfc_charlen_type_node),
    3672              :         build_pointer_type (pchar1_type_node), gfc_charlen_type_node,
    3673              :         pchar1_type_node);
    3674              : 
    3675        31424 :   gfor_fndecl_string_minmax = gfc_build_library_function_decl_with_spec (
    3676              :         get_identifier (PREFIX("string_minmax")), ". W w . R ",
    3677              :         void_type_node, -4, build_pointer_type (gfc_charlen_type_node),
    3678              :         build_pointer_type (pchar1_type_node), integer_type_node,
    3679              :         integer_type_node);
    3680              : 
    3681        31424 :   gfor_fndecl_string_split = gfc_build_library_function_decl_with_spec (
    3682              :     get_identifier (PREFIX ("string_split")), ". . R . R . . ",
    3683              :     gfc_charlen_type_node, 6, gfc_charlen_type_node, pchar1_type_node,
    3684              :     gfc_charlen_type_node, pchar1_type_node, gfc_charlen_type_node,
    3685              :     gfc_logical4_type_node);
    3686              : 
    3687        31424 :   {
    3688        31424 :     tree copy_helper_ptr_type;
    3689        31424 :     tree copy_helper_fn_type;
    3690              : 
    3691        31424 :     copy_helper_fn_type = build_function_type_list (void_type_node,
    3692              :                                                     pvoid_type_node,
    3693              :                                                     pvoid_type_node,
    3694              :                                                     NULL_TREE);
    3695        31424 :     copy_helper_ptr_type = build_pointer_type (copy_helper_fn_type);
    3696              : 
    3697        31424 :     gfor_fndecl_cfi_deep_copy_array
    3698        31424 :       = gfc_build_library_function_decl_with_spec (
    3699              :           get_identifier (PREFIX ("cfi_deep_copy_array")), ". R R . ",
    3700              :           void_type_node, 3, pvoid_type_node, pvoid_type_node,
    3701              :           copy_helper_ptr_type);
    3702              :   }
    3703              : 
    3704        31424 :   gfor_fndecl_adjustl = gfc_build_library_function_decl_with_spec (
    3705              :         get_identifier (PREFIX("adjustl")), ". W . R ",
    3706              :         void_type_node, 3, pchar1_type_node, gfc_charlen_type_node,
    3707              :         pchar1_type_node);
    3708        31424 :   TREE_NOTHROW (gfor_fndecl_adjustl) = 1;
    3709              : 
    3710        31424 :   gfor_fndecl_adjustr = gfc_build_library_function_decl_with_spec (
    3711              :         get_identifier (PREFIX("adjustr")), ". W . R ",
    3712              :         void_type_node, 3, pchar1_type_node, gfc_charlen_type_node,
    3713              :         pchar1_type_node);
    3714        31424 :   TREE_NOTHROW (gfor_fndecl_adjustr) = 1;
    3715              : 
    3716        31424 :   gfor_fndecl_select_string =  gfc_build_library_function_decl_with_spec (
    3717              :         get_identifier (PREFIX("select_string")), ". R . R . ",
    3718              :         integer_type_node, 4, pvoid_type_node, integer_type_node,
    3719              :         pchar1_type_node, gfc_charlen_type_node);
    3720        31424 :   DECL_PURE_P (gfor_fndecl_select_string) = 1;
    3721        31424 :   TREE_NOTHROW (gfor_fndecl_select_string) = 1;
    3722              : 
    3723        31424 :   gfor_fndecl_compare_string_char4 = gfc_build_library_function_decl_with_spec (
    3724              :         get_identifier (PREFIX("compare_string_char4")), ". . R . R ",
    3725              :         integer_type_node, 4, gfc_charlen_type_node, pchar4_type_node,
    3726              :         gfc_charlen_type_node, pchar4_type_node);
    3727        31424 :   DECL_PURE_P (gfor_fndecl_compare_string_char4) = 1;
    3728        31424 :   TREE_NOTHROW (gfor_fndecl_compare_string_char4) = 1;
    3729              : 
    3730        31424 :   gfor_fndecl_concat_string_char4 = gfc_build_library_function_decl_with_spec (
    3731              :         get_identifier (PREFIX("concat_string_char4")), ". . W . R . R ",
    3732              :         void_type_node, 6, gfc_charlen_type_node, pchar4_type_node,
    3733              :         gfc_charlen_type_node, pchar4_type_node, gfc_charlen_type_node,
    3734              :         pchar4_type_node);
    3735        31424 :   TREE_NOTHROW (gfor_fndecl_concat_string_char4) = 1;
    3736              : 
    3737        31424 :   gfor_fndecl_string_len_trim_char4 = gfc_build_library_function_decl_with_spec (
    3738              :         get_identifier (PREFIX("string_len_trim_char4")), ". . R ",
    3739              :         gfc_charlen_type_node, 2, gfc_charlen_type_node, pchar4_type_node);
    3740        31424 :   DECL_PURE_P (gfor_fndecl_string_len_trim_char4) = 1;
    3741        31424 :   TREE_NOTHROW (gfor_fndecl_string_len_trim_char4) = 1;
    3742              : 
    3743        31424 :   gfor_fndecl_string_index_char4 = gfc_build_library_function_decl_with_spec (
    3744              :         get_identifier (PREFIX("string_index_char4")), ". . R . R . ",
    3745              :         gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar4_type_node,
    3746              :         gfc_charlen_type_node, pchar4_type_node, gfc_logical4_type_node);
    3747        31424 :   DECL_PURE_P (gfor_fndecl_string_index_char4) = 1;
    3748        31424 :   TREE_NOTHROW (gfor_fndecl_string_index_char4) = 1;
    3749              : 
    3750        31424 :   gfor_fndecl_string_scan_char4 = gfc_build_library_function_decl_with_spec (
    3751              :         get_identifier (PREFIX("string_scan_char4")), ". . R . R . ",
    3752              :         gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar4_type_node,
    3753              :         gfc_charlen_type_node, pchar4_type_node, gfc_logical4_type_node);
    3754        31424 :   DECL_PURE_P (gfor_fndecl_string_scan_char4) = 1;
    3755        31424 :   TREE_NOTHROW (gfor_fndecl_string_scan_char4) = 1;
    3756              : 
    3757        31424 :   gfor_fndecl_string_verify_char4 = gfc_build_library_function_decl_with_spec (
    3758              :         get_identifier (PREFIX("string_verify_char4")), ". . R . R . ",
    3759              :         gfc_charlen_type_node, 5, gfc_charlen_type_node, pchar4_type_node,
    3760              :         gfc_charlen_type_node, pchar4_type_node, gfc_logical4_type_node);
    3761        31424 :   DECL_PURE_P (gfor_fndecl_string_verify_char4) = 1;
    3762        31424 :   TREE_NOTHROW (gfor_fndecl_string_verify_char4) = 1;
    3763              : 
    3764        31424 :   gfor_fndecl_string_trim_char4 = gfc_build_library_function_decl_with_spec (
    3765              :         get_identifier (PREFIX("string_trim_char4")), ". W w . R ",
    3766              :         void_type_node, 4, build_pointer_type (gfc_charlen_type_node),
    3767              :         build_pointer_type (pchar4_type_node), gfc_charlen_type_node,
    3768              :         pchar4_type_node);
    3769              : 
    3770        31424 :   gfor_fndecl_string_minmax_char4 = gfc_build_library_function_decl_with_spec (
    3771              :         get_identifier (PREFIX("string_minmax_char4")), ". W w . R ",
    3772              :         void_type_node, -4, build_pointer_type (gfc_charlen_type_node),
    3773              :         build_pointer_type (pchar4_type_node), integer_type_node,
    3774              :         integer_type_node);
    3775              : 
    3776        31424 :   gfor_fndecl_string_split_char4 = gfc_build_library_function_decl_with_spec (
    3777              :     get_identifier (PREFIX ("string_split_char4")), ". . R . R . . ",
    3778              :     gfc_charlen_type_node, 6, gfc_charlen_type_node, pchar4_type_node,
    3779              :     gfc_charlen_type_node, pchar4_type_node, gfc_charlen_type_node,
    3780              :     gfc_logical4_type_node);
    3781              : 
    3782        31424 :   gfor_fndecl_adjustl_char4 = gfc_build_library_function_decl_with_spec (
    3783              :         get_identifier (PREFIX("adjustl_char4")), ". W . R ",
    3784              :         void_type_node, 3, pchar4_type_node, gfc_charlen_type_node,
    3785              :         pchar4_type_node);
    3786        31424 :   TREE_NOTHROW (gfor_fndecl_adjustl_char4) = 1;
    3787              : 
    3788        31424 :   gfor_fndecl_adjustr_char4 = gfc_build_library_function_decl_with_spec (
    3789              :         get_identifier (PREFIX("adjustr_char4")), ". W . R ",
    3790              :         void_type_node, 3, pchar4_type_node, gfc_charlen_type_node,
    3791              :         pchar4_type_node);
    3792        31424 :   TREE_NOTHROW (gfor_fndecl_adjustr_char4) = 1;
    3793              : 
    3794        31424 :   gfor_fndecl_select_string_char4 = gfc_build_library_function_decl_with_spec (
    3795              :         get_identifier (PREFIX("select_string_char4")), ". R . R . ",
    3796              :         integer_type_node, 4, pvoid_type_node, integer_type_node,
    3797              :         pvoid_type_node, gfc_charlen_type_node);
    3798        31424 :   DECL_PURE_P (gfor_fndecl_select_string_char4) = 1;
    3799        31424 :   TREE_NOTHROW (gfor_fndecl_select_string_char4) = 1;
    3800              : 
    3801              : 
    3802              :   /* Conversion between character kinds.  */
    3803              : 
    3804        31424 :   gfor_fndecl_convert_char1_to_char4 = gfc_build_library_function_decl_with_spec (
    3805              :         get_identifier (PREFIX("convert_char1_to_char4")), ". w . R ",
    3806              :         void_type_node, 3, build_pointer_type (pchar4_type_node),
    3807              :         gfc_charlen_type_node, pchar1_type_node);
    3808              : 
    3809        31424 :   gfor_fndecl_convert_char4_to_char1 = gfc_build_library_function_decl_with_spec (
    3810              :         get_identifier (PREFIX("convert_char4_to_char1")), ". w . R ",
    3811              :         void_type_node, 3, build_pointer_type (pchar1_type_node),
    3812              :         gfc_charlen_type_node, pchar4_type_node);
    3813              : 
    3814              :   /* Misc. functions.  */
    3815              : 
    3816        31424 :   gfor_fndecl_ttynam = gfc_build_library_function_decl_with_spec (
    3817              :         get_identifier (PREFIX("ttynam")), ". W . . ",
    3818              :         void_type_node, 3, pchar_type_node, gfc_charlen_type_node,
    3819              :         integer_type_node);
    3820              : 
    3821        31424 :   gfor_fndecl_fdate = gfc_build_library_function_decl_with_spec (
    3822              :         get_identifier (PREFIX("fdate")), ". W . ",
    3823              :         void_type_node, 2, pchar_type_node, gfc_charlen_type_node);
    3824              : 
    3825        31424 :   gfor_fndecl_ctime = gfc_build_library_function_decl_with_spec (
    3826              :         get_identifier (PREFIX("ctime")), ". W . . ",
    3827              :         void_type_node, 3, pchar_type_node, gfc_charlen_type_node,
    3828              :         gfc_int8_type_node);
    3829              : 
    3830        31424 :   gfor_fndecl_random_init = gfc_build_library_function_decl (
    3831              :         get_identifier (PREFIX("random_init")),
    3832              :         void_type_node, 3, gfc_logical4_type_node, gfc_logical4_type_node,
    3833              :         gfc_int4_type_node);
    3834              : 
    3835              :  // gfor_fndecl_caf_rand_init is defined in the lib-coarray section below.
    3836              : 
    3837        31424 :   gfor_fndecl_sc_kind = gfc_build_library_function_decl_with_spec (
    3838              :         get_identifier (PREFIX("selected_char_kind")), ". . R ",
    3839              :         gfc_int4_type_node, 2, gfc_charlen_type_node, pchar_type_node);
    3840        31424 :   DECL_PURE_P (gfor_fndecl_sc_kind) = 1;
    3841        31424 :   TREE_NOTHROW (gfor_fndecl_sc_kind) = 1;
    3842              : 
    3843        31424 :   gfor_fndecl_si_kind = gfc_build_library_function_decl_with_spec (
    3844              :         get_identifier (PREFIX("selected_int_kind")), ". R ",
    3845              :         gfc_int4_type_node, 1, pvoid_type_node);
    3846        31424 :   DECL_PURE_P (gfor_fndecl_si_kind) = 1;
    3847        31424 :   TREE_NOTHROW (gfor_fndecl_si_kind) = 1;
    3848              : 
    3849        31424 :   gfor_fndecl_sl_kind = gfc_build_library_function_decl_with_spec (
    3850              :         get_identifier (PREFIX("selected_logical_kind")), ". R ",
    3851              :         gfc_int4_type_node, 1, pvoid_type_node);
    3852        31424 :   DECL_PURE_P (gfor_fndecl_sl_kind) = 1;
    3853        31424 :   TREE_NOTHROW (gfor_fndecl_sl_kind) = 1;
    3854              : 
    3855        31424 :   gfor_fndecl_sr_kind = gfc_build_library_function_decl_with_spec (
    3856              :         get_identifier (PREFIX("selected_real_kind2008")), ". R R ",
    3857              :         gfc_int4_type_node, 3, pvoid_type_node, pvoid_type_node,
    3858              :         pvoid_type_node);
    3859        31424 :   DECL_PURE_P (gfor_fndecl_sr_kind) = 1;
    3860        31424 :   TREE_NOTHROW (gfor_fndecl_sr_kind) = 1;
    3861              : 
    3862        31424 :   gfor_fndecl_system_clock4 = gfc_build_library_function_decl (
    3863              :         get_identifier (PREFIX("system_clock_4")),
    3864              :         void_type_node, 3, gfc_pint4_type_node, gfc_pint4_type_node,
    3865              :         gfc_pint4_type_node);
    3866              : 
    3867        31424 :   gfor_fndecl_system_clock8 = gfc_build_library_function_decl (
    3868              :         get_identifier (PREFIX("system_clock_8")),
    3869              :         void_type_node, 3, gfc_pint8_type_node, gfc_pint8_type_node,
    3870              :         gfc_pint8_type_node);
    3871              : 
    3872              :   /* Power functions.  */
    3873        31424 :   {
    3874        31424 :     tree ctype, rtype, itype, jtype;
    3875        31424 :     int rkind, ikind, jkind;
    3876              : #define NIKINDS 3
    3877              : #define NRKINDS 4
    3878              : #define NUKINDS 5
    3879        31424 :     static const int ikinds[NIKINDS] = {4, 8, 16};
    3880        31424 :     static const int rkinds[NRKINDS] = {4, 8, 10, 16};
    3881        31424 :     static const int ukinds[NUKINDS] = {1, 2, 4, 8, 16};
    3882        31424 :     char name[PREFIX_LEN + 12]; /* _gfortran_pow_?n_?n */
    3883              : 
    3884       125696 :     for (ikind=0; ikind < NIKINDS; ikind++)
    3885              :       {
    3886        94272 :         itype = gfc_get_int_type (ikinds[ikind]);
    3887              : 
    3888       471360 :         for (jkind=0; jkind < NIKINDS; jkind++)
    3889              :           {
    3890       282816 :             jtype = gfc_get_int_type (ikinds[jkind]);
    3891       282816 :             if (itype && jtype)
    3892              :               {
    3893       280781 :                 sprintf (name, PREFIX("pow_i%d_i%d"), ikinds[ikind],
    3894              :                         ikinds[jkind]);
    3895       561562 :                 gfor_fndecl_math_powi[jkind][ikind].integer =
    3896       280781 :                   gfc_build_library_function_decl (get_identifier (name),
    3897              :                     jtype, 2, jtype, itype);
    3898       280781 :                 TREE_READONLY (gfor_fndecl_math_powi[jkind][ikind].integer) = 1;
    3899       280781 :                 TREE_NOTHROW (gfor_fndecl_math_powi[jkind][ikind].integer) = 1;
    3900              :               }
    3901              :           }
    3902              : 
    3903       471360 :         for (rkind = 0; rkind < NRKINDS; rkind ++)
    3904              :           {
    3905       377088 :             rtype = gfc_get_real_type (rkinds[rkind]);
    3906       377088 :             if (rtype && itype)
    3907              :               {
    3908       375460 :                 sprintf (name, PREFIX("pow_r%d_i%d"),
    3909              :                          gfc_type_abi_kind (BT_REAL, rkinds[rkind]),
    3910              :                          ikinds[ikind]);
    3911       750920 :                 gfor_fndecl_math_powi[rkind][ikind].real =
    3912       375460 :                   gfc_build_library_function_decl (get_identifier (name),
    3913              :                     rtype, 2, rtype, itype);
    3914       375460 :                 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].real) = 1;
    3915       375460 :                 TREE_NOTHROW (gfor_fndecl_math_powi[rkind][ikind].real) = 1;
    3916              :               }
    3917              : 
    3918       377088 :             ctype = gfc_get_complex_type (rkinds[rkind]);
    3919       377088 :             if (ctype && itype)
    3920              :               {
    3921       375460 :                 sprintf (name, PREFIX("pow_c%d_i%d"),
    3922              :                          gfc_type_abi_kind (BT_REAL, rkinds[rkind]),
    3923              :                          ikinds[ikind]);
    3924       750920 :                 gfor_fndecl_math_powi[rkind][ikind].cmplx =
    3925       375460 :                   gfc_build_library_function_decl (get_identifier (name),
    3926              :                     ctype, 2,ctype, itype);
    3927       375460 :                 TREE_READONLY (gfor_fndecl_math_powi[rkind][ikind].cmplx) = 1;
    3928       375460 :                 TREE_NOTHROW (gfor_fndecl_math_powi[rkind][ikind].cmplx) = 1;
    3929              :               }
    3930              :           }
    3931              :         /* For unsigned types, we have every power for every type.  */
    3932       565632 :         for (int base = 0; base < NUKINDS; base++)
    3933              :           {
    3934       471360 :             tree base_type = gfc_get_unsigned_type (ukinds[base]);
    3935      2828160 :             for (int expon = 0; expon < NUKINDS; expon++)
    3936              :               {
    3937      2356800 :                 tree expon_type = gfc_get_unsigned_type (ukinds[base]);
    3938      2356800 :                 if (base_type && expon_type)
    3939              :                   {
    3940        18375 :                     sprintf (name, PREFIX("pow_m%d_m%d"), ukinds[base],
    3941        18375 :                          ukinds[expon]);
    3942        36750 :                     gfor_fndecl_unsigned_pow_list [base][expon] =
    3943        18375 :                       gfc_build_library_function_decl (get_identifier (name),
    3944              :                          base_type, 2, base_type, expon_type);
    3945        18375 :                     TREE_READONLY (gfor_fndecl_unsigned_pow_list[base][expon]) = 1;
    3946        18375 :                     TREE_NOTHROW (gfor_fndecl_unsigned_pow_list[base][expon]) = 1;
    3947              :                   }
    3948              :               }
    3949              :           }
    3950              :       }
    3951              : #undef NIKINDS
    3952              : #undef NRKINDS
    3953              : #undef NUKINDS
    3954              :   }
    3955              : 
    3956        31424 :   gfor_fndecl_math_ishftc4 = gfc_build_library_function_decl (
    3957              :         get_identifier (PREFIX("ishftc4")),
    3958              :         gfc_int4_type_node, 3, gfc_int4_type_node, gfc_int4_type_node,
    3959              :         gfc_int4_type_node);
    3960        31424 :   TREE_READONLY (gfor_fndecl_math_ishftc4) = 1;
    3961        31424 :   TREE_NOTHROW (gfor_fndecl_math_ishftc4) = 1;
    3962              : 
    3963        31424 :   gfor_fndecl_math_ishftc8 = gfc_build_library_function_decl (
    3964              :         get_identifier (PREFIX("ishftc8")),
    3965              :         gfc_int8_type_node, 3, gfc_int8_type_node, gfc_int4_type_node,
    3966              :         gfc_int4_type_node);
    3967        31424 :   TREE_READONLY (gfor_fndecl_math_ishftc8) = 1;
    3968        31424 :   TREE_NOTHROW (gfor_fndecl_math_ishftc8) = 1;
    3969              : 
    3970        31424 :   if (gfc_int16_type_node)
    3971              :     {
    3972        31017 :       gfor_fndecl_math_ishftc16 = gfc_build_library_function_decl (
    3973              :         get_identifier (PREFIX("ishftc16")),
    3974              :         gfc_int16_type_node, 3, gfc_int16_type_node, gfc_int4_type_node,
    3975              :         gfc_int4_type_node);
    3976        31017 :       TREE_READONLY (gfor_fndecl_math_ishftc16) = 1;
    3977        31017 :       TREE_NOTHROW (gfor_fndecl_math_ishftc16) = 1;
    3978              :     }
    3979              : 
    3980              :   /* BLAS functions.  */
    3981        31424 :   {
    3982        31424 :     tree pint = build_pointer_type (integer_type_node);
    3983        31424 :     tree ps = build_pointer_type (gfc_get_real_type (gfc_default_real_kind));
    3984        31424 :     tree pd = build_pointer_type (gfc_get_real_type (gfc_default_double_kind));
    3985        31424 :     tree pc = build_pointer_type (gfc_get_complex_type (gfc_default_real_kind));
    3986        31424 :     tree pz = build_pointer_type
    3987        31424 :                 (gfc_get_complex_type (gfc_default_double_kind));
    3988              : 
    3989        62848 :     gfor_fndecl_sgemm = gfc_build_library_function_decl
    3990        32199 :                           (get_identifier
    3991              :                              (flag_underscoring ? "sgemm_" : "sgemm"),
    3992              :                            void_type_node, 15, pchar_type_node,
    3993              :                            pchar_type_node, pint, pint, pint, ps, ps, pint,
    3994              :                            ps, pint, ps, ps, pint, integer_type_node,
    3995              :                            integer_type_node);
    3996        62848 :     gfor_fndecl_dgemm = gfc_build_library_function_decl
    3997        32199 :                           (get_identifier
    3998              :                              (flag_underscoring ? "dgemm_" : "dgemm"),
    3999              :                            void_type_node, 15, pchar_type_node,
    4000              :                            pchar_type_node, pint, pint, pint, pd, pd, pint,
    4001              :                            pd, pint, pd, pd, pint, integer_type_node,
    4002              :                            integer_type_node);
    4003        62848 :     gfor_fndecl_cgemm = gfc_build_library_function_decl
    4004        32199 :                           (get_identifier
    4005              :                              (flag_underscoring ? "cgemm_" : "cgemm"),
    4006              :                            void_type_node, 15, pchar_type_node,
    4007              :                            pchar_type_node, pint, pint, pint, pc, pc, pint,
    4008              :                            pc, pint, pc, pc, pint, integer_type_node,
    4009              :                            integer_type_node);
    4010        62848 :     gfor_fndecl_zgemm = gfc_build_library_function_decl
    4011        32199 :                           (get_identifier
    4012              :                              (flag_underscoring ? "zgemm_" : "zgemm"),
    4013              :                            void_type_node, 15, pchar_type_node,
    4014              :                            pchar_type_node, pint, pint, pint, pz, pz, pint,
    4015              :                            pz, pint, pz, pz, pint, integer_type_node,
    4016              :                            integer_type_node);
    4017              :   }
    4018              : 
    4019              :   /* Other functions.  */
    4020        31424 :   gfor_fndecl_iargc = gfc_build_library_function_decl (
    4021              :         get_identifier (PREFIX ("iargc")), gfc_int4_type_node, 0);
    4022        31424 :   TREE_NOTHROW (gfor_fndecl_iargc) = 1;
    4023              : 
    4024        31424 :   gfor_fndecl_kill_sub = gfc_build_library_function_decl (
    4025              :         get_identifier (PREFIX ("kill_sub")), void_type_node,
    4026              :         3, gfc_int4_type_node, gfc_int4_type_node, gfc_pint4_type_node);
    4027              : 
    4028        31424 :   gfor_fndecl_kill = gfc_build_library_function_decl (
    4029              :         get_identifier (PREFIX ("kill")), gfc_int4_type_node,
    4030              :         2, gfc_int4_type_node, gfc_int4_type_node);
    4031              : 
    4032        31424 :   gfor_fndecl_is_contiguous0 = gfc_build_library_function_decl_with_spec (
    4033              :         get_identifier (PREFIX("is_contiguous0")), ". R ",
    4034              :         gfc_int4_type_node, 1, pvoid_type_node);
    4035        31424 :   DECL_PURE_P (gfor_fndecl_is_contiguous0) = 1;
    4036        31424 :   TREE_NOTHROW (gfor_fndecl_is_contiguous0) = 1;
    4037              : 
    4038        31424 :   gfor_fndecl_fstat_i4_sub = gfc_build_library_function_decl (
    4039              :         get_identifier (PREFIX ("fstat_i4_sub")), void_type_node,
    4040              :         3, gfc_pint4_type_node, gfc_pint4_type_node, gfc_pint4_type_node);
    4041              : 
    4042        31424 :   gfor_fndecl_fstat_i8_sub = gfc_build_library_function_decl (
    4043              :         get_identifier (PREFIX ("fstat_i8_sub")), void_type_node,
    4044              :         3, gfc_pint8_type_node, gfc_pint8_type_node, gfc_pint8_type_node);
    4045              : 
    4046        31424 :   gfor_fndecl_lstat_i4_sub = gfc_build_library_function_decl (
    4047              :         get_identifier (PREFIX ("lstat_i4_sub")), void_type_node,
    4048              :         4, pchar_type_node, gfc_pint4_type_node, gfc_pint4_type_node,
    4049              :         gfc_charlen_type_node);
    4050              : 
    4051        31424 :   gfor_fndecl_lstat_i8_sub = gfc_build_library_function_decl (
    4052              :         get_identifier (PREFIX ("lstat_i8_sub")), void_type_node,
    4053              :         4, pchar_type_node, gfc_pint8_type_node, gfc_pint8_type_node,
    4054              :         gfc_charlen_type_node);
    4055              : 
    4056        31424 :   gfor_fndecl_stat_i4_sub = gfc_build_library_function_decl (
    4057              :         get_identifier (PREFIX ("stat_i4_sub")), void_type_node,
    4058              :         4, pchar_type_node, gfc_pint4_type_node, gfc_pint4_type_node,
    4059              :         gfc_charlen_type_node);
    4060              : 
    4061        31424 :   gfor_fndecl_stat_i8_sub = gfc_build_library_function_decl (
    4062              :         get_identifier (PREFIX ("stat_i8_sub")), void_type_node,
    4063              :         4, pchar_type_node, gfc_pint8_type_node, gfc_pint8_type_node,
    4064              :         gfc_charlen_type_node);
    4065        31424 : }
    4066              : 
    4067              : 
    4068              : /* Make prototypes for runtime library functions.  */
    4069              : 
    4070              : void
    4071        31424 : gfc_build_builtin_function_decls (void)
    4072              : {
    4073        31424 :   tree gfc_int8_type_node = gfc_get_int_type (8);
    4074              : 
    4075        31424 :   gfor_fndecl_stop_numeric = gfc_build_library_function_decl (
    4076              :         get_identifier (PREFIX("stop_numeric")),
    4077              :         void_type_node, 2, integer_type_node, boolean_type_node);
    4078              :   /* STOP doesn't return.  */
    4079        31424 :   TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric) = 1;
    4080              : 
    4081        31424 :   gfor_fndecl_stop_string = gfc_build_library_function_decl_with_spec (
    4082              :         get_identifier (PREFIX("stop_string")), ". R . . ",
    4083              :         void_type_node, 3, pchar_type_node, size_type_node,
    4084              :         boolean_type_node);
    4085              :   /* STOP doesn't return.  */
    4086        31424 :   TREE_THIS_VOLATILE (gfor_fndecl_stop_string) = 1;
    4087              : 
    4088        31424 :   gfor_fndecl_error_stop_numeric = gfc_build_library_function_decl (
    4089              :         get_identifier (PREFIX("error_stop_numeric")),
    4090              :         void_type_node, 2, integer_type_node, boolean_type_node);
    4091              :   /* ERROR STOP doesn't return.  */
    4092        31424 :   TREE_THIS_VOLATILE (gfor_fndecl_error_stop_numeric) = 1;
    4093              : 
    4094        31424 :   gfor_fndecl_error_stop_string = gfc_build_library_function_decl_with_spec (
    4095              :         get_identifier (PREFIX("error_stop_string")), ". R . . ",
    4096              :         void_type_node, 3, pchar_type_node, size_type_node,
    4097              :         boolean_type_node);
    4098              :   /* ERROR STOP doesn't return.  */
    4099        31424 :   TREE_THIS_VOLATILE (gfor_fndecl_error_stop_string) = 1;
    4100              : 
    4101        31424 :   gfor_fndecl_pause_numeric = gfc_build_library_function_decl (
    4102              :         get_identifier (PREFIX("pause_numeric")),
    4103              :         void_type_node, 1, gfc_int8_type_node);
    4104              : 
    4105        31424 :   gfor_fndecl_pause_string = gfc_build_library_function_decl_with_spec (
    4106              :         get_identifier (PREFIX("pause_string")), ". R . ",
    4107              :         void_type_node, 2, pchar_type_node, size_type_node);
    4108              : 
    4109        31424 :   gfor_fndecl_runtime_error = gfc_build_library_function_decl_with_spec (
    4110              :         get_identifier (PREFIX("runtime_error")), ". R ",
    4111              :         void_type_node, -1, pchar_type_node);
    4112              :   /* The runtime_error function does not return.  */
    4113        31424 :   TREE_THIS_VOLATILE (gfor_fndecl_runtime_error) = 1;
    4114              : 
    4115        31424 :   gfor_fndecl_runtime_error_at = gfc_build_library_function_decl_with_spec (
    4116              :         get_identifier (PREFIX("runtime_error_at")), ". R R ",
    4117              :         void_type_node, -2, pchar_type_node, pchar_type_node);
    4118              :   /* The runtime_error_at function does not return.  */
    4119        31424 :   TREE_THIS_VOLATILE (gfor_fndecl_runtime_error_at) = 1;
    4120              : 
    4121        31424 :   gfor_fndecl_runtime_warning_at = gfc_build_library_function_decl_with_spec (
    4122              :         get_identifier (PREFIX("runtime_warning_at")), ". R R ",
    4123              :         void_type_node, -2, pchar_type_node, pchar_type_node);
    4124              : 
    4125        31424 :   gfor_fndecl_generate_error = gfc_build_library_function_decl_with_spec (
    4126              :         get_identifier (PREFIX("generate_error")), ". W . R ",
    4127              :         void_type_node, 3, pvoid_type_node, integer_type_node,
    4128              :         pchar_type_node);
    4129              : 
    4130        31424 :   gfor_fndecl_os_error_at = gfc_build_library_function_decl_with_spec (
    4131              :         get_identifier (PREFIX("os_error_at")), ". R R ",
    4132              :         void_type_node, -2, pchar_type_node, pchar_type_node);
    4133              :   /* The os_error_at function does not return.  */
    4134        31424 :   TREE_THIS_VOLATILE (gfor_fndecl_os_error_at) = 1;
    4135              : 
    4136        31424 :   gfor_fndecl_set_args = gfc_build_library_function_decl (
    4137              :         get_identifier (PREFIX("set_args")),
    4138              :         void_type_node, 2, integer_type_node,
    4139              :         build_pointer_type (pchar_type_node));
    4140              : 
    4141        31424 :   gfor_fndecl_set_fpe = gfc_build_library_function_decl (
    4142              :         get_identifier (PREFIX("set_fpe")),
    4143              :         void_type_node, 1, integer_type_node);
    4144              : 
    4145        31424 :   gfor_fndecl_ieee_procedure_entry = gfc_build_library_function_decl (
    4146              :         get_identifier (PREFIX("ieee_procedure_entry")),
    4147              :         void_type_node, 1, pvoid_type_node);
    4148              : 
    4149        31424 :   gfor_fndecl_ieee_procedure_exit = gfc_build_library_function_decl (
    4150              :         get_identifier (PREFIX("ieee_procedure_exit")),
    4151              :         void_type_node, 1, pvoid_type_node);
    4152              : 
    4153              :   /* Keep the array dimension in sync with the call, later in this file.  */
    4154        31424 :   gfor_fndecl_set_options = gfc_build_library_function_decl_with_spec (
    4155              :         get_identifier (PREFIX("set_options")), ". . R ",
    4156              :         void_type_node, 2, integer_type_node,
    4157              :         build_pointer_type (integer_type_node));
    4158              : 
    4159        31424 :   gfor_fndecl_set_convert = gfc_build_library_function_decl (
    4160              :         get_identifier (PREFIX("set_convert")),
    4161              :         void_type_node, 1, integer_type_node);
    4162              : 
    4163        31424 :   gfor_fndecl_set_record_marker = gfc_build_library_function_decl (
    4164              :         get_identifier (PREFIX("set_record_marker")),
    4165              :         void_type_node, 1, integer_type_node);
    4166              : 
    4167        31424 :   gfor_fndecl_set_max_subrecord_length = gfc_build_library_function_decl (
    4168              :         get_identifier (PREFIX("set_max_subrecord_length")),
    4169              :         void_type_node, 1, integer_type_node);
    4170              : 
    4171        31424 :   gfor_fndecl_in_pack = gfc_build_library_function_decl_with_spec (
    4172              :         get_identifier (PREFIX("internal_pack")), ". r ",
    4173              :         pvoid_type_node, 1, pvoid_type_node);
    4174              : 
    4175        31424 :   gfor_fndecl_in_unpack = gfc_build_library_function_decl_with_spec (
    4176              :         get_identifier (PREFIX("internal_unpack")), ". w R ",
    4177              :         void_type_node, 2, pvoid_type_node, pvoid_type_node);
    4178              : 
    4179        31424 :   gfor_fndecl_in_pack_class = gfc_build_library_function_decl_with_spec (
    4180              :     get_identifier (PREFIX ("internal_pack_class")), ". w R r r ",
    4181              :     void_type_node, 4, pvoid_type_node, pvoid_type_node, size_type_node,
    4182              :     integer_type_node);
    4183              : 
    4184        31424 :   gfor_fndecl_in_unpack_class = gfc_build_library_function_decl_with_spec (
    4185              :     get_identifier (PREFIX ("internal_unpack_class")), ". w R r r ",
    4186              :     void_type_node, 4, pvoid_type_node, pvoid_type_node, size_type_node,
    4187              :     integer_type_node);
    4188              : 
    4189        31424 :   gfor_fndecl_associated = gfc_build_library_function_decl_with_spec (
    4190              :     get_identifier (PREFIX ("associated")), ". R R ", integer_type_node, 2,
    4191              :     ppvoid_type_node, ppvoid_type_node);
    4192        31424 :   DECL_PURE_P (gfor_fndecl_associated) = 1;
    4193        31424 :   TREE_NOTHROW (gfor_fndecl_associated) = 1;
    4194              : 
    4195              :   /* Coarray library calls.  */
    4196        31424 :   if (flag_coarray == GFC_FCOARRAY_LIB)
    4197              :     {
    4198          462 :       tree pint_type, pppchar_type, psize_type;
    4199              : 
    4200          462 :       pint_type = build_pointer_type (integer_type_node);
    4201          462 :       pppchar_type
    4202          462 :         = build_pointer_type (build_pointer_type (pchar_type_node));
    4203          462 :       psize_type = build_pointer_type (size_type_node);
    4204              : 
    4205          462 :       gfor_fndecl_caf_init = gfc_build_library_function_decl_with_spec (
    4206              :         get_identifier (PREFIX("caf_init")), ". W W ",
    4207              :         void_type_node, 2, pint_type, pppchar_type);
    4208              : 
    4209          462 :       gfor_fndecl_caf_finalize = gfc_build_library_function_decl (
    4210              :         get_identifier (PREFIX("caf_finalize")), void_type_node, 0);
    4211              : 
    4212          462 :       gfor_fndecl_caf_this_image = gfc_build_library_function_decl_with_spec (
    4213              :         get_identifier (PREFIX ("caf_this_image")), ". r ", integer_type_node,
    4214              :         1, pvoid_type_node);
    4215              : 
    4216          462 :       gfor_fndecl_caf_num_images = gfc_build_library_function_decl (
    4217              :         get_identifier (PREFIX("caf_num_images")), integer_type_node,
    4218              :         2, integer_type_node, integer_type_node);
    4219              : 
    4220          462 :       gfor_fndecl_caf_register = gfc_build_library_function_decl_with_spec (
    4221              :         get_identifier (PREFIX("caf_register")), ". . . W w w w . ",
    4222              :         void_type_node, 7,
    4223              :         size_type_node, integer_type_node, ppvoid_type_node, pvoid_type_node,
    4224              :         pint_type, pchar_type_node, size_type_node);
    4225              : 
    4226          462 :       gfor_fndecl_caf_deregister = gfc_build_library_function_decl_with_spec (
    4227              :         get_identifier (PREFIX("caf_deregister")), ". W . w w . ",
    4228              :         void_type_node, 5,
    4229              :         ppvoid_type_node, integer_type_node, pint_type, pchar_type_node,
    4230              :         size_type_node);
    4231              : 
    4232          462 :       gfor_fndecl_caf_register_accessor
    4233          462 :         = gfc_build_library_function_decl_with_spec (
    4234              :           get_identifier (PREFIX ("caf_register_accessor")), ". r r ",
    4235              :           void_type_node, 2, integer_type_node, pvoid_type_node);
    4236              : 
    4237          462 :       gfor_fndecl_caf_register_accessors_finish
    4238          462 :         = gfc_build_library_function_decl_with_spec (
    4239              :           get_identifier (PREFIX ("caf_register_accessors_finish")), ". ",
    4240              :           void_type_node, 0);
    4241              : 
    4242          462 :       gfor_fndecl_caf_get_remote_function_index
    4243          462 :         = gfc_build_library_function_decl_with_spec (
    4244              :           get_identifier (PREFIX ("caf_get_remote_function_index")), ". r ",
    4245              :           integer_type_node, 1, integer_type_node);
    4246              : 
    4247          462 :       gfor_fndecl_caf_get_from_remote
    4248          462 :         = gfc_build_library_function_decl_with_spec (
    4249              :           get_identifier (PREFIX ("caf_get_from_remote")),
    4250              :           ". r r r r r w w w r r w r w r r ", void_type_node, 15,
    4251              :           pvoid_type_node, pvoid_type_node, psize_type, integer_type_node,
    4252              :           size_type_node, ppvoid_type_node, psize_type, pvoid_type_node,
    4253              :           boolean_type_node, integer_type_node, pvoid_type_node, size_type_node,
    4254              :           pint_type, pvoid_type_node, pint_type);
    4255              : 
    4256          462 :       gfor_fndecl_caf_send_to_remote
    4257          462 :         = gfc_build_library_function_decl_with_spec (
    4258              :           get_identifier (PREFIX ("caf_send_to_remote")),
    4259              :           ". r r r r r r r r r w r w r r ", void_type_node, 14, pvoid_type_node,
    4260              :           pvoid_type_node, psize_type, integer_type_node, size_type_node,
    4261              :           ppvoid_type_node, psize_type, pvoid_type_node, integer_type_node,
    4262              :           pvoid_type_node, size_type_node, pint_type, pvoid_type_node,
    4263              :           pint_type);
    4264              : 
    4265          462 :       gfor_fndecl_caf_transfer_between_remotes
    4266          462 :         = gfc_build_library_function_decl_with_spec (
    4267              :           get_identifier (PREFIX ("caf_transfer_between_remotes")),
    4268              :           ". r r r r r r r r r r r r r r r r w w r r ", void_type_node, 20,
    4269              :           pvoid_type_node, pvoid_type_node, psize_type, integer_type_node,
    4270              :           integer_type_node, pvoid_type_node, size_type_node, pvoid_type_node,
    4271              :           pvoid_type_node, psize_type, integer_type_node, integer_type_node,
    4272              :           pvoid_type_node, size_type_node, size_type_node, boolean_type_node,
    4273              :           pint_type, pint_type, pvoid_type_node, pint_type);
    4274              : 
    4275          462 :       gfor_fndecl_caf_sync_all = gfc_build_library_function_decl_with_spec (
    4276              :         get_identifier (PREFIX ("caf_sync_all")), ". w w . ", void_type_node, 3,
    4277              :         pint_type, pchar_type_node, size_type_node);
    4278              : 
    4279          462 :       gfor_fndecl_caf_sync_memory = gfc_build_library_function_decl_with_spec (
    4280              :         get_identifier (PREFIX("caf_sync_memory")), ". w w . ", void_type_node,
    4281              :         3, pint_type, pchar_type_node, size_type_node);
    4282              : 
    4283          462 :       gfor_fndecl_caf_sync_images = gfc_build_library_function_decl_with_spec (
    4284              :         get_identifier (PREFIX("caf_sync_images")), ". . r w w . ", void_type_node,
    4285              :         5, integer_type_node, pint_type, pint_type,
    4286              :         pchar_type_node, size_type_node);
    4287              : 
    4288          462 :       gfor_fndecl_caf_error_stop = gfc_build_library_function_decl (
    4289              :         get_identifier (PREFIX("caf_error_stop")),
    4290              :         void_type_node, 1, integer_type_node);
    4291              :       /* CAF's ERROR STOP doesn't return.  */
    4292          462 :       TREE_THIS_VOLATILE (gfor_fndecl_caf_error_stop) = 1;
    4293              : 
    4294          462 :       gfor_fndecl_caf_error_stop_str = gfc_build_library_function_decl_with_spec (
    4295              :         get_identifier (PREFIX("caf_error_stop_str")), ". r . ",
    4296              :         void_type_node, 2, pchar_type_node, size_type_node);
    4297              :       /* CAF's ERROR STOP doesn't return.  */
    4298          462 :       TREE_THIS_VOLATILE (gfor_fndecl_caf_error_stop_str) = 1;
    4299              : 
    4300          462 :       gfor_fndecl_caf_stop_numeric = gfc_build_library_function_decl (
    4301              :         get_identifier (PREFIX("caf_stop_numeric")),
    4302              :         void_type_node, 1, integer_type_node);
    4303              :       /* CAF's STOP doesn't return.  */
    4304          462 :       TREE_THIS_VOLATILE (gfor_fndecl_caf_stop_numeric) = 1;
    4305              : 
    4306          462 :       gfor_fndecl_caf_stop_str = gfc_build_library_function_decl_with_spec (
    4307              :         get_identifier (PREFIX("caf_stop_str")), ". r . ",
    4308              :         void_type_node, 2, pchar_type_node, size_type_node);
    4309              :       /* CAF's STOP doesn't return.  */
    4310          462 :       TREE_THIS_VOLATILE (gfor_fndecl_caf_stop_str) = 1;
    4311              : 
    4312          462 :       gfor_fndecl_caf_atomic_def = gfc_build_library_function_decl_with_spec (
    4313              :         get_identifier (PREFIX("caf_atomic_define")), ". r . . w w . . ",
    4314              :         void_type_node, 7, pvoid_type_node, size_type_node, integer_type_node,
    4315              :         pvoid_type_node, pint_type, integer_type_node, integer_type_node);
    4316              : 
    4317          462 :       gfor_fndecl_caf_atomic_ref = gfc_build_library_function_decl_with_spec (
    4318              :         get_identifier (PREFIX("caf_atomic_ref")), ". r . . w w . . ",
    4319              :         void_type_node, 7, pvoid_type_node, size_type_node, integer_type_node,
    4320              :         pvoid_type_node, pint_type, integer_type_node, integer_type_node);
    4321              : 
    4322          462 :       gfor_fndecl_caf_atomic_cas = gfc_build_library_function_decl_with_spec (
    4323              :         get_identifier (PREFIX("caf_atomic_cas")), ". r . . w r r w . . ",
    4324              :         void_type_node, 9, pvoid_type_node, size_type_node, integer_type_node,
    4325              :         pvoid_type_node, pvoid_type_node, pvoid_type_node, pint_type,
    4326              :         integer_type_node, integer_type_node);
    4327              : 
    4328          462 :       gfor_fndecl_caf_atomic_op = gfc_build_library_function_decl_with_spec (
    4329              :         get_identifier (PREFIX("caf_atomic_op")), ". . r . . r w w . . ",
    4330              :         void_type_node, 9, integer_type_node, pvoid_type_node, size_type_node,
    4331              :         integer_type_node, pvoid_type_node, pvoid_type_node, pint_type,
    4332              :         integer_type_node, integer_type_node);
    4333              : 
    4334          462 :       gfor_fndecl_caf_lock = gfc_build_library_function_decl_with_spec (
    4335              :         get_identifier (PREFIX("caf_lock")), ". r . . w w w . ",
    4336              :         void_type_node, 7, pvoid_type_node, size_type_node, integer_type_node,
    4337              :         pint_type, pint_type, pchar_type_node, size_type_node);
    4338              : 
    4339          462 :       gfor_fndecl_caf_unlock = gfc_build_library_function_decl_with_spec (
    4340              :         get_identifier (PREFIX("caf_unlock")), ". r . . w w . ",
    4341              :         void_type_node, 6, pvoid_type_node, size_type_node, integer_type_node,
    4342              :         pint_type, pchar_type_node, size_type_node);
    4343              : 
    4344          462 :       gfor_fndecl_caf_event_post = gfc_build_library_function_decl_with_spec (
    4345              :         get_identifier (PREFIX("caf_event_post")), ". r . . w w . ",
    4346              :         void_type_node, 6, pvoid_type_node, size_type_node, integer_type_node,
    4347              :         pint_type, pchar_type_node, size_type_node);
    4348              : 
    4349          462 :       gfor_fndecl_caf_event_wait = gfc_build_library_function_decl_with_spec (
    4350              :         get_identifier (PREFIX("caf_event_wait")), ". r . . w w . ",
    4351              :         void_type_node, 6, pvoid_type_node, size_type_node, integer_type_node,
    4352              :         pint_type, pchar_type_node, size_type_node);
    4353              : 
    4354          462 :       gfor_fndecl_caf_event_query = gfc_build_library_function_decl_with_spec (
    4355              :         get_identifier (PREFIX("caf_event_query")), ". r . . w w ",
    4356              :         void_type_node, 5, pvoid_type_node, size_type_node, integer_type_node,
    4357              :         pint_type, pint_type);
    4358              : 
    4359          462 :       gfor_fndecl_caf_fail_image = gfc_build_library_function_decl (
    4360              :         get_identifier (PREFIX("caf_fail_image")), void_type_node, 0);
    4361              :       /* CAF's FAIL doesn't return.  */
    4362          462 :       TREE_THIS_VOLATILE (gfor_fndecl_caf_fail_image) = 1;
    4363              : 
    4364          462 :       gfor_fndecl_caf_failed_images
    4365          462 :         = gfc_build_library_function_decl_with_spec (
    4366              :             get_identifier (PREFIX("caf_failed_images")), ". w . r ",
    4367              :             void_type_node, 3, pvoid_type_node, ppvoid_type_node,
    4368              :             integer_type_node);
    4369              : 
    4370          462 :       gfor_fndecl_caf_form_team = gfc_build_library_function_decl_with_spec (
    4371              :         get_identifier (PREFIX ("caf_form_team")), ". r w r w w w ",
    4372              :         void_type_node, 6, integer_type_node, ppvoid_type_node, pint_type,
    4373              :         pint_type, pchar_type_node, size_type_node);
    4374              : 
    4375          462 :       gfor_fndecl_caf_change_team = gfc_build_library_function_decl_with_spec (
    4376              :         get_identifier (PREFIX ("caf_change_team")), ". r w w w ",
    4377              :         void_type_node, 4, pvoid_type_node, pint_type, pchar_type_node,
    4378              :         size_type_node);
    4379              : 
    4380          462 :       gfor_fndecl_caf_end_team = gfc_build_library_function_decl_with_spec (
    4381              :         get_identifier (PREFIX ("caf_end_team")), ". w w w ", void_type_node, 3,
    4382              :         pint_type, pchar_type_node, size_type_node);
    4383              : 
    4384          462 :       gfor_fndecl_caf_get_team = gfc_build_library_function_decl_with_spec (
    4385              :         get_identifier (PREFIX ("caf_get_team")), ". r ", pvoid_type_node, 1,
    4386              :         pint_type);
    4387              : 
    4388          462 :       gfor_fndecl_caf_sync_team = gfc_build_library_function_decl_with_spec (
    4389              :         get_identifier (PREFIX ("caf_sync_team")), ". r w w w ", void_type_node,
    4390              :         4, pvoid_type_node, pint_type, pchar_type_node, size_type_node);
    4391              : 
    4392          462 :       gfor_fndecl_caf_team_number = gfc_build_library_function_decl_with_spec (
    4393              :         get_identifier (PREFIX ("caf_team_number")), ". r ", integer_type_node,
    4394              :         1, pvoid_type_node);
    4395              : 
    4396          462 :       gfor_fndecl_caf_image_status = gfc_build_library_function_decl_with_spec (
    4397              :         get_identifier (PREFIX ("caf_image_status")), ". r r ",
    4398              :         integer_type_node, 2, integer_type_node, ppvoid_type_node);
    4399              : 
    4400          462 :       gfor_fndecl_caf_stopped_images
    4401          462 :         = gfc_build_library_function_decl_with_spec (
    4402              :             get_identifier (PREFIX("caf_stopped_images")), ". w r r ",
    4403              :             void_type_node, 3, pvoid_type_node, ppvoid_type_node,
    4404              :             integer_type_node);
    4405              : 
    4406          462 :       gfor_fndecl_co_broadcast = gfc_build_library_function_decl_with_spec (
    4407              :         get_identifier (PREFIX("caf_co_broadcast")), ". w . w w . ",
    4408              :         void_type_node, 5, pvoid_type_node, integer_type_node,
    4409              :         pint_type, pchar_type_node, size_type_node);
    4410              : 
    4411          462 :       gfor_fndecl_co_max = gfc_build_library_function_decl_with_spec (
    4412              :         get_identifier (PREFIX("caf_co_max")), ". w . w w . . ",
    4413              :         void_type_node, 6, pvoid_type_node, integer_type_node,
    4414              :         pint_type, pchar_type_node, integer_type_node, size_type_node);
    4415              : 
    4416          462 :       gfor_fndecl_co_min = gfc_build_library_function_decl_with_spec (
    4417              :         get_identifier (PREFIX("caf_co_min")), ". w . w w . . ",
    4418              :         void_type_node, 6, pvoid_type_node, integer_type_node,
    4419              :         pint_type, pchar_type_node, integer_type_node, size_type_node);
    4420              : 
    4421          462 :       gfor_fndecl_co_reduce = gfc_build_library_function_decl_with_spec (
    4422              :         get_identifier (PREFIX("caf_co_reduce")), ". w r . . w w . . ",
    4423              :         void_type_node, 8, pvoid_type_node,
    4424              :         build_pointer_type (build_varargs_function_type_list (void_type_node,
    4425              :                                                               NULL_TREE)),
    4426              :         integer_type_node, integer_type_node, pint_type, pchar_type_node,
    4427              :         integer_type_node, size_type_node);
    4428              : 
    4429          462 :       gfor_fndecl_co_sum = gfc_build_library_function_decl_with_spec (
    4430              :         get_identifier (PREFIX("caf_co_sum")), ". w . w w . ",
    4431              :         void_type_node, 5, pvoid_type_node, integer_type_node,
    4432              :         pint_type, pchar_type_node, size_type_node);
    4433              : 
    4434          462 :       gfor_fndecl_caf_is_present_on_remote
    4435          462 :         = gfc_build_library_function_decl_with_spec (
    4436              :           get_identifier (PREFIX ("caf_is_present_on_remote")), ". r r r r r ",
    4437              :           integer_type_node, 5, pvoid_type_node, integer_type_node,
    4438              :           integer_type_node, pvoid_type_node, size_type_node);
    4439              : 
    4440          462 :       gfor_fndecl_caf_random_init = gfc_build_library_function_decl (
    4441              :             get_identifier (PREFIX("caf_random_init")),
    4442              :             void_type_node, 2, logical_type_node, logical_type_node);
    4443              :     }
    4444              : 
    4445        31424 :   gfc_build_intrinsic_function_decls ();
    4446        31424 :   gfc_build_intrinsic_lib_fndecls ();
    4447        31424 :   gfc_build_io_library_fndecls ();
    4448        31424 : }
    4449              : 
    4450              : 
    4451              : /* Evaluate the length of dummy character variables.  */
    4452              : 
    4453              : static void
    4454          774 : gfc_trans_dummy_character (gfc_symbol *sym, gfc_charlen *cl,
    4455              :                            gfc_wrapped_block *block)
    4456              : {
    4457          774 :   stmtblock_t init;
    4458              : 
    4459          774 :   gfc_finish_decl (cl->backend_decl);
    4460              : 
    4461          774 :   gfc_start_block (&init);
    4462              : 
    4463              :   /* Evaluate the string length expression.  */
    4464          774 :   gfc_conv_string_length (cl, NULL, &init);
    4465              : 
    4466          774 :   gfc_trans_vla_type_sizes (sym, &init);
    4467              : 
    4468          774 :   gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
    4469          774 : }
    4470              : 
    4471              : 
    4472              : /* Allocate and cleanup an automatic character variable.  */
    4473              : 
    4474              : static void
    4475          354 : gfc_trans_auto_character_variable (gfc_symbol * sym, gfc_wrapped_block * block)
    4476              : {
    4477          354 :   stmtblock_t init;
    4478          354 :   tree decl;
    4479          354 :   tree tmp;
    4480          354 :   bool back;
    4481              : 
    4482          354 :   gcc_assert (sym->backend_decl);
    4483          354 :   gcc_assert (sym->ts.u.cl && sym->ts.u.cl->length);
    4484              : 
    4485          354 :   gfc_init_block (&init);
    4486              : 
    4487              :   /* In the case of non-dummy symbols with dependencies on an old-fashioned
    4488              :      function result (ie. proc_name = proc_name->result), gfc_add_init_cleanup
    4489              :      must be called with the last, optional argument false so that the process
    4490              :      ing of the character length occurs after the processing of the result.  */
    4491          354 :   back = sym->fn_result_dep;
    4492              : 
    4493              :   /* Evaluate the string length expression.  */
    4494          354 :   gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
    4495              : 
    4496          354 :   gfc_trans_vla_type_sizes (sym, &init);
    4497              : 
    4498          354 :   decl = sym->backend_decl;
    4499              : 
    4500              :   /* Emit a DECL_EXPR for this variable, which will cause the
    4501              :      gimplifier to allocate storage, and all that good stuff.  */
    4502          354 :   tmp = fold_build1_loc (input_location, DECL_EXPR, TREE_TYPE (decl), decl);
    4503          354 :   gfc_add_expr_to_block (&init, tmp);
    4504              : 
    4505          354 :   gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE, back);
    4506          354 : }
    4507              : 
    4508              : /* Set the initial value of ASSIGN statement auxiliary variable explicitly.  */
    4509              : 
    4510              : static void
    4511           64 : gfc_trans_assign_aux_var (gfc_symbol * sym, gfc_wrapped_block * block)
    4512              : {
    4513           64 :   stmtblock_t init;
    4514              : 
    4515           64 :   gcc_assert (sym->backend_decl);
    4516           64 :   gfc_start_block (&init);
    4517              : 
    4518              :   /* Set the initial value to length. See the comments in
    4519              :      function gfc_add_assign_aux_vars in this file.  */
    4520           64 :   gfc_add_modify (&init, GFC_DECL_STRING_LEN (sym->backend_decl),
    4521              :                   build_int_cst (gfc_charlen_type_node, -2));
    4522              : 
    4523           64 :   gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
    4524           64 : }
    4525              : 
    4526              : static void
    4527       161818 : gfc_trans_vla_one_sizepos (tree *tp, stmtblock_t *body)
    4528              : {
    4529       161818 :   tree t = *tp, var, val;
    4530              : 
    4531       161818 :   if (t == NULL || t == error_mark_node)
    4532              :     return;
    4533       151795 :   if (TREE_CONSTANT (t) || DECL_P (t))
    4534              :     return;
    4535              : 
    4536        62473 :   if (TREE_CODE (t) == SAVE_EXPR)
    4537              :     {
    4538        35850 :       if (SAVE_EXPR_RESOLVED_P (t))
    4539              :         {
    4540            0 :           *tp = TREE_OPERAND (t, 0);
    4541            0 :           return;
    4542              :         }
    4543        35850 :       val = TREE_OPERAND (t, 0);
    4544              :     }
    4545              :   else
    4546              :     val = t;
    4547              : 
    4548        62473 :   var = gfc_create_var_np (TREE_TYPE (t), NULL);
    4549        62473 :   gfc_add_decl_to_function (var);
    4550        62473 :   gfc_add_modify (body, var, unshare_expr (val));
    4551        62473 :   if (TREE_CODE (t) == SAVE_EXPR)
    4552        35850 :     TREE_OPERAND (t, 0) = var;
    4553        62473 :   *tp = var;
    4554              : }
    4555              : 
    4556              : static void
    4557        90449 : gfc_trans_vla_type_sizes_1 (tree type, stmtblock_t *body)
    4558              : {
    4559        90449 :   tree t;
    4560              : 
    4561        90449 :   if (type == NULL || type == error_mark_node)
    4562              :     return;
    4563              : 
    4564        90449 :   type = TYPE_MAIN_VARIANT (type);
    4565              : 
    4566        90449 :   if (TREE_CODE (type) == INTEGER_TYPE)
    4567              :     {
    4568        50085 :       gfc_trans_vla_one_sizepos (&TYPE_MIN_VALUE (type), body);
    4569        50085 :       gfc_trans_vla_one_sizepos (&TYPE_MAX_VALUE (type), body);
    4570              : 
    4571        64852 :       for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
    4572              :         {
    4573        14767 :           TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
    4574        14767 :           TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
    4575              :         }
    4576              :     }
    4577        40364 :   else if (TREE_CODE (type) == ARRAY_TYPE)
    4578              :     {
    4579        30824 :       gfc_trans_vla_type_sizes_1 (TREE_TYPE (type), body);
    4580        30824 :       gfc_trans_vla_type_sizes_1 (TYPE_DOMAIN (type), body);
    4581        30824 :       gfc_trans_vla_one_sizepos (&TYPE_SIZE (type), body);
    4582        30824 :       gfc_trans_vla_one_sizepos (&TYPE_SIZE_UNIT (type), body);
    4583              : 
    4584        30824 :       for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
    4585              :         {
    4586            0 :           TYPE_SIZE (t) = TYPE_SIZE (type);
    4587            0 :           TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
    4588              :         }
    4589              :     }
    4590              : }
    4591              : 
    4592              : /* Make sure all type sizes and array domains are either constant,
    4593              :    or variable or parameter decls.  This is a simplified variant
    4594              :    of gimplify_type_sizes, but we can't use it here, as none of the
    4595              :    variables in the expressions have been gimplified yet.
    4596              :    As type sizes and domains for various variable length arrays
    4597              :    contain VAR_DECLs that are only initialized at gfc_trans_deferred_vars
    4598              :    time, without this routine gimplify_type_sizes in the middle-end
    4599              :    could result in the type sizes being gimplified earlier than where
    4600              :    those variables are initialized.  */
    4601              : 
    4602              : void
    4603        27309 : gfc_trans_vla_type_sizes (gfc_symbol *sym, stmtblock_t *body)
    4604              : {
    4605        27309 :   tree type = TREE_TYPE (sym->backend_decl);
    4606              : 
    4607        27309 :   if (TREE_CODE (type) == FUNCTION_TYPE
    4608         1105 :       && (sym->attr.function || sym->attr.result || sym->attr.entry))
    4609              :     {
    4610         1105 :       if (! current_fake_result_decl)
    4611              :         return;
    4612              : 
    4613         1105 :       type = TREE_TYPE (TREE_VALUE (current_fake_result_decl));
    4614              :     }
    4615              : 
    4616        53755 :   while (POINTER_TYPE_P (type))
    4617        26446 :     type = TREE_TYPE (type);
    4618              : 
    4619        27309 :   if (GFC_DESCRIPTOR_TYPE_P (type))
    4620              :     {
    4621         1492 :       tree etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
    4622              : 
    4623         2984 :       while (POINTER_TYPE_P (etype))
    4624         1492 :         etype = TREE_TYPE (etype);
    4625              : 
    4626         1492 :       gfc_trans_vla_type_sizes_1 (etype, body);
    4627              :     }
    4628              : 
    4629        27309 :   gfc_trans_vla_type_sizes_1 (type, body);
    4630              : }
    4631              : 
    4632              : 
    4633              : /* Initialize a derived type by building an lvalue from the symbol
    4634              :    and using trans_assignment to do the work. Set dealloc to false
    4635              :    if no deallocation prior the assignment is needed.  */
    4636              : void
    4637         1465 : gfc_init_default_dt (gfc_symbol * sym, stmtblock_t * block, bool dealloc,
    4638              :                      bool pdt_ok)
    4639              : {
    4640         1465 :   gfc_expr *e;
    4641         1465 :   tree tmp;
    4642         1465 :   tree present;
    4643              : 
    4644         1465 :   gcc_assert (block);
    4645              : 
    4646              :   /* Initialization of PDTs is done elsewhere.  */
    4647         1465 :   if (IS_PDT (sym) && !pdt_ok)
    4648              :     return;
    4649              : 
    4650         1143 :   gcc_assert (!sym->attr.allocatable);
    4651         1143 :   gfc_set_sym_referenced (sym);
    4652         1143 :   e = gfc_lval_expr_from_sym (sym);
    4653         1143 :   tmp = gfc_trans_assignment (e, sym->value, false, dealloc);
    4654         1143 :   if (sym->attr.dummy && (sym->attr.optional
    4655          241 :                           || sym->ns->proc_name->attr.entry_master))
    4656              :     {
    4657           39 :       present = gfc_conv_expr_present (sym);
    4658           39 :       tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), present,
    4659              :                         tmp, build_empty_stmt (input_location));
    4660              :     }
    4661         1143 :   gfc_add_expr_to_block (block, tmp);
    4662         1143 :   gfc_free_expr (e);
    4663              : }
    4664              : 
    4665              : 
    4666              : /* Initialize a PDT, either when the symbol has a value or when all the
    4667              :    components have an initializer.  */
    4668              : static tree
    4669          532 : gfc_init_default_pdt (gfc_symbol *sym, bool dealloc)
    4670              : {
    4671          532 :   stmtblock_t block;
    4672          532 :   tree tmp;
    4673          532 :   gfc_component *c;
    4674              : 
    4675          532 :   if (sym->value && sym->value->symtree
    4676           12 :       && sym->value->symtree->n.sym
    4677           12 :       && !sym->value->symtree->n.sym->attr.artificial)
    4678              :     {
    4679           12 :       tmp = gfc_trans_assignment (gfc_lval_expr_from_sym (sym),
    4680              :                                   sym->value, false, false, true);
    4681           12 :       return tmp;
    4682              :     }
    4683              : 
    4684          520 :   if (!dealloc || !sym->value)
    4685              :     return NULL_TREE;
    4686              : 
    4687              :   /* Allowed in the case where all the components have initializers and
    4688              :      there are no LEN components.  */
    4689          329 :   c = sym->ts.u.derived->components;
    4690          542 :   for (; c; c = c->next)
    4691          522 :     if (c->attr.pdt_len || !c->initializer)
    4692              :       return NULL_TREE;
    4693              : 
    4694           20 :   gfc_init_block (&block);
    4695           20 :   gfc_init_default_dt (sym, &block, dealloc, true);
    4696           20 :   return gfc_finish_block (&block);
    4697              : }
    4698              : 
    4699              : 
    4700              : /* Initialize INTENT(OUT) derived type dummies.  As well as giving
    4701              :    them their default initializer, if they have allocatable
    4702              :    components, they have their allocatable components deallocated.  */
    4703              : 
    4704              : static void
    4705        98610 : init_intent_out_dt (gfc_symbol * proc_sym, gfc_wrapped_block * block)
    4706              : {
    4707        98610 :   stmtblock_t init;
    4708        98610 :   gfc_formal_arglist *f;
    4709        98610 :   tree tmp;
    4710        98610 :   tree present;
    4711        98610 :   gfc_symbol *s;
    4712              : 
    4713        98610 :   gfc_init_block (&init);
    4714       199799 :   for (f = gfc_sym_get_dummy_args (proc_sym); f; f = f->next)
    4715       101189 :     if (f->sym && f->sym->attr.intent == INTENT_OUT
    4716         3990 :         && !f->sym->attr.pointer
    4717         3936 :         && f->sym->ts.type == BT_DERIVED)
    4718              :       {
    4719          516 :         s = f->sym;
    4720          516 :         tmp = NULL_TREE;
    4721              : 
    4722              :         /* Note: Allocatables are excluded as they are already handled
    4723              :            by the caller.  */
    4724          516 :         if (!f->sym->attr.allocatable
    4725          516 :             && gfc_is_finalizable (s->ts.u.derived, NULL))
    4726              :           {
    4727           38 :             stmtblock_t block;
    4728           38 :             gfc_expr *e;
    4729              : 
    4730           38 :             gfc_init_block (&block);
    4731           38 :             s->attr.referenced = 1;
    4732           38 :             e = gfc_lval_expr_from_sym (s);
    4733           38 :             gfc_add_finalizer_call (&block, e);
    4734           38 :             gfc_free_expr (e);
    4735           38 :             tmp = gfc_finish_block (&block);
    4736              :           }
    4737              : 
    4738              :         /* Note: Allocatables are excluded as they are already handled
    4739              :            by the caller.  */
    4740          516 :         if (tmp == NULL_TREE && !s->attr.allocatable
    4741          358 :             && s->ts.u.derived->attr.alloc_comp)
    4742          126 :           tmp = gfc_deallocate_alloc_comp (s->ts.u.derived,
    4743              :                                            s->backend_decl,
    4744          126 :                                            s->as ? s->as->rank : 0);
    4745              : 
    4746          516 :         if (tmp != NULL_TREE && (s->attr.optional
    4747          145 :                                  || s->ns->proc_name->attr.entry_master))
    4748              :           {
    4749           19 :             present = gfc_conv_expr_present (s);
    4750           19 :             tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
    4751              :                               present, tmp, build_empty_stmt (input_location));
    4752              :           }
    4753              : 
    4754          516 :         gfc_add_expr_to_block (&init, tmp);
    4755          516 :         if (s->value && !s->attr.allocatable)
    4756          273 :           gfc_init_default_dt (s, &init, false);
    4757              :       }
    4758       100673 :     else if (f->sym && f->sym->attr.intent == INTENT_OUT
    4759         3474 :              && f->sym->ts.type == BT_CLASS
    4760          624 :              && !CLASS_DATA (f->sym)->attr.class_pointer
    4761          605 :              && !CLASS_DATA (f->sym)->attr.allocatable)
    4762              :       {
    4763          400 :         stmtblock_t block;
    4764          400 :         gfc_expr *e;
    4765              : 
    4766          400 :         gfc_init_block (&block);
    4767          400 :         f->sym->attr.referenced = 1;
    4768          400 :         e = gfc_lval_expr_from_sym (f->sym);
    4769          400 :         gfc_add_finalizer_call (&block, e);
    4770          400 :         gfc_free_expr (e);
    4771          400 :         tmp = gfc_finish_block (&block);
    4772              : 
    4773          400 :         if (f->sym->attr.optional || f->sym->ns->proc_name->attr.entry_master)
    4774              :           {
    4775            6 :             present = gfc_conv_expr_present (f->sym);
    4776            6 :             tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
    4777              :                               present, tmp,
    4778              :                               build_empty_stmt (input_location));
    4779              :           }
    4780          400 :         gfc_add_expr_to_block (&init, tmp);
    4781              :       }
    4782        98610 :   gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
    4783        98610 : }
    4784              : 
    4785              : 
    4786              : /* Helper function to manage deferred string lengths.  */
    4787              : 
    4788              : static tree
    4789          161 : gfc_null_and_pass_deferred_len (gfc_symbol *sym, stmtblock_t *init,
    4790              :                                 location_t loc)
    4791              : {
    4792          161 :   tree tmp;
    4793              : 
    4794              :   /* Character length passed by reference.  */
    4795          161 :   tmp = sym->ts.u.cl->passed_length;
    4796          161 :   tmp = build_fold_indirect_ref_loc (input_location, tmp);
    4797          161 :   tmp = fold_convert (gfc_charlen_type_node, tmp);
    4798              : 
    4799          161 :   if (!sym->attr.dummy || sym->attr.intent == INTENT_OUT)
    4800              :     /* Zero the string length when entering the scope.  */
    4801          161 :     gfc_add_modify (init, sym->ts.u.cl->backend_decl,
    4802              :                     build_int_cst (gfc_charlen_type_node, 0));
    4803              :   else
    4804              :     {
    4805            0 :       tree tmp2;
    4806              : 
    4807            0 :       tmp2 = fold_build2_loc (input_location, MODIFY_EXPR,
    4808              :                               gfc_charlen_type_node,
    4809            0 :                               sym->ts.u.cl->backend_decl, tmp);
    4810            0 :       if (sym->attr.optional)
    4811              :         {
    4812            0 :           tree present = gfc_conv_expr_present (sym);
    4813            0 :           tmp2 = build3_loc (input_location, COND_EXPR,
    4814              :                              void_type_node, present, tmp2,
    4815              :                              build_empty_stmt (input_location));
    4816              :         }
    4817            0 :       gfc_add_expr_to_block (init, tmp2);
    4818              :     }
    4819              : 
    4820          161 :   input_location = loc;
    4821              : 
    4822              :   /* Pass the final character length back.  */
    4823          161 :   if (sym->attr.intent != INTENT_IN)
    4824              :     {
    4825          322 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR,
    4826              :                              gfc_charlen_type_node, tmp,
    4827          161 :                              sym->ts.u.cl->backend_decl);
    4828          161 :       if (sym->attr.optional)
    4829              :         {
    4830            0 :           tree present = gfc_conv_expr_present (sym);
    4831            0 :           tmp = build3_loc (input_location, COND_EXPR,
    4832              :                             void_type_node, present, tmp,
    4833              :                             build_empty_stmt (input_location));
    4834              :         }
    4835              :     }
    4836              :   else
    4837              :     tmp = NULL_TREE;
    4838              : 
    4839          161 :   return tmp;
    4840              : }
    4841              : 
    4842              : 
    4843              : /* Get the result expression for a procedure.  */
    4844              : 
    4845              : static tree
    4846        24861 : get_proc_result (gfc_symbol* sym)
    4847              : {
    4848        24861 :   if (sym->attr.subroutine || sym == sym->result)
    4849              :     {
    4850        12483 :       if (current_fake_result_decl != NULL)
    4851        12294 :         return TREE_VALUE (current_fake_result_decl);
    4852              : 
    4853              :       return NULL_TREE;
    4854              :     }
    4855              : 
    4856        12378 :   return sym->result->backend_decl;
    4857              : }
    4858              : 
    4859              : 
    4860              : /* Generate function entry and exit code, and add it to the function body.
    4861              :    This includes:
    4862              :     Allocation and initialization of array variables.
    4863              :     Allocation of character string variables.
    4864              :     Initialization and possibly repacking of dummy arrays.
    4865              :     Initialization of ASSIGN statement auxiliary variable.
    4866              :     Initialization of ASSOCIATE names.
    4867              :     Automatic deallocation.  */
    4868              : 
    4869              : void
    4870        98610 : gfc_trans_deferred_vars (gfc_symbol * proc_sym, gfc_wrapped_block * block)
    4871              : {
    4872        98610 :   location_t loc;
    4873        98610 :   gfc_symbol *sym;
    4874        98610 :   gfc_formal_arglist *f;
    4875        98610 :   stmtblock_t tmpblock;
    4876        98610 :   bool seen_trans_deferred_array = false;
    4877        98610 :   bool is_pdt_type = false;
    4878        98610 :   tree tmp = NULL;
    4879        98610 :   gfc_expr *e;
    4880        98610 :   gfc_se se;
    4881        98610 :   stmtblock_t init;
    4882              : 
    4883              :   /* Deal with implicit return variables.  Explicit return variables will
    4884              :      already have been added.  */
    4885        98610 :   if (gfc_return_by_reference (proc_sym) && proc_sym->result == proc_sym)
    4886              :     {
    4887         1687 :       if (!current_fake_result_decl)
    4888              :         {
    4889           81 :           gfc_entry_list *el = NULL;
    4890           81 :           if (proc_sym->attr.entry_master)
    4891              :             {
    4892           46 :               for (el = proc_sym->ns->entries; el; el = el->next)
    4893           46 :                 if (el->sym != el->sym->result)
    4894              :                   break;
    4895              :             }
    4896              :           /* TODO: move to the appropriate place in resolve.cc.  */
    4897           81 :           if (warn_return_type > 0 && el == NULL)
    4898            4 :             gfc_warning (OPT_Wreturn_type,
    4899              :                          "Return value of function %qs at %L not set",
    4900              :                          proc_sym->name, &proc_sym->declared_at);
    4901              :         }
    4902         1606 :       else if (proc_sym->as)
    4903              :         {
    4904          807 :           tree result = TREE_VALUE (current_fake_result_decl);
    4905          807 :           loc = input_location;
    4906          807 :           input_location = gfc_get_location (&proc_sym->declared_at);
    4907          807 :           gfc_trans_dummy_array_bias (proc_sym, result, block);
    4908              : 
    4909              :           /* An automatic character length, pointer array result.  */
    4910          807 :           if (proc_sym->ts.type == BT_CHARACTER
    4911           69 :               && VAR_P (proc_sym->ts.u.cl->backend_decl))
    4912              :             {
    4913           51 :               tmp = NULL;
    4914           51 :               if (proc_sym->ts.deferred)
    4915              :                 {
    4916           12 :                   gfc_start_block (&init);
    4917           12 :                   tmp = gfc_null_and_pass_deferred_len (proc_sym, &init, loc);
    4918           12 :                   gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp);
    4919              :                 }
    4920              :               else
    4921           39 :                 gfc_trans_dummy_character (proc_sym, proc_sym->ts.u.cl, block);
    4922              :             }
    4923              :         }
    4924          799 :       else if (proc_sym->ts.type == BT_CHARACTER)
    4925              :         {
    4926          763 :           if (proc_sym->ts.deferred)
    4927              :             {
    4928           91 :               tmp = NULL;
    4929           91 :               loc = input_location;
    4930           91 :               input_location = gfc_get_location (&proc_sym->declared_at);
    4931           91 :               gfc_start_block (&init);
    4932              :               /* Zero the string length on entry.  */
    4933           91 :               gfc_add_modify (&init, proc_sym->ts.u.cl->backend_decl,
    4934              :                               build_int_cst (gfc_charlen_type_node, 0));
    4935              :               /* Null the pointer.  */
    4936           91 :               e = gfc_lval_expr_from_sym (proc_sym);
    4937           91 :               gfc_init_se (&se, NULL);
    4938           91 :               se.want_pointer = 1;
    4939           91 :               gfc_conv_expr (&se, e);
    4940           91 :               gfc_free_expr (e);
    4941           91 :               tmp = se.expr;
    4942           91 :               gfc_add_modify (&init, tmp,
    4943           91 :                               fold_convert (TREE_TYPE (se.expr),
    4944              :                                             null_pointer_node));
    4945           91 :               input_location = loc;
    4946              : 
    4947              :               /* Pass back the string length on exit.  */
    4948           91 :               tmp = proc_sym->ts.u.cl->backend_decl;
    4949           91 :               if (TREE_CODE (tmp) != INDIRECT_REF
    4950           91 :                   && proc_sym->ts.u.cl->passed_length)
    4951              :                 {
    4952           91 :                   tmp = proc_sym->ts.u.cl->passed_length;
    4953           91 :                   tmp = build_fold_indirect_ref_loc (input_location, tmp);
    4954          182 :                   tmp = fold_build2_loc (input_location, MODIFY_EXPR,
    4955           91 :                                          TREE_TYPE (tmp), tmp,
    4956           91 :                                          fold_convert
    4957              :                                          (TREE_TYPE (tmp),
    4958              :                                           proc_sym->ts.u.cl->backend_decl));
    4959              :                 }
    4960              :               else
    4961              :                 tmp = NULL_TREE;
    4962              : 
    4963           91 :               gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp);
    4964              :             }
    4965          672 :           else if (VAR_P (proc_sym->ts.u.cl->backend_decl))
    4966          393 :             gfc_trans_dummy_character (proc_sym, proc_sym->ts.u.cl, block);
    4967              :         }
    4968              :       else
    4969           36 :         gcc_assert (flag_f2c && proc_sym->ts.type == BT_COMPLEX);
    4970              :     }
    4971        96923 :   else if (proc_sym == proc_sym->result && IS_CLASS_ARRAY (proc_sym))
    4972              :     {
    4973              :       /* Nullify explicit return class arrays on entry.  */
    4974           34 :       tree type;
    4975           34 :       tmp = get_proc_result (proc_sym);
    4976           34 :       if (tmp && GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
    4977              :         {
    4978           34 :           gfc_start_block (&init);
    4979           34 :           tmp = gfc_class_data_get (tmp);
    4980           34 :           type = TREE_TYPE (gfc_conv_descriptor_data_get (tmp));
    4981           34 :           gfc_conv_descriptor_data_set (&init, tmp, build_int_cst (type, 0));
    4982           34 :           gfc_add_init_cleanup (block, gfc_finish_block (&init), NULL_TREE);
    4983              :         }
    4984              :     }
    4985              : 
    4986        98610 :   sym = (proc_sym->attr.function
    4987        98610 :          && proc_sym != proc_sym->result) ? proc_sym->result : NULL;
    4988              : 
    4989         7242 :   if (sym && !sym->attr.allocatable && !sym->attr.pointer
    4990         6618 :       && sym->attr.referenced
    4991        13763 :       && IS_PDT (sym) && !gfc_has_default_initializer (sym->ts.u.derived))
    4992              :     {
    4993            6 :       gfc_init_block (&tmpblock);
    4994            6 :       tmp = gfc_allocate_pdt_comp (sym->ts.u.derived,
    4995              :                                    sym->backend_decl,
    4996            6 :                                    sym->as ? sym->as->rank : 0,
    4997            6 :                                    sym->param_list);
    4998            6 :       gfc_add_expr_to_block (&tmpblock, tmp);
    4999            6 :       gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), NULL);
    5000              :     }
    5001              : 
    5002              :   /* Initialize the INTENT(OUT) derived type dummy arguments.  This
    5003              :      should be done here so that the offsets and lbounds of arrays
    5004              :      are available.  */
    5005        98610 :   loc = input_location;
    5006        98610 :   input_location = gfc_get_location (&proc_sym->declared_at);
    5007        98610 :   init_intent_out_dt (proc_sym, block);
    5008        98610 :   input_location = loc;
    5009              : 
    5010              :   /* For some reasons, internal procedures point to the parent's
    5011              :      namespace.  Top-level procedure and variables inside BLOCK are fine.  */
    5012        98610 :   gfc_namespace *omp_ns = proc_sym->ns;
    5013        98610 :   if (proc_sym->ns->proc_name != proc_sym)
    5014       199662 :     for (omp_ns = proc_sym->ns->contained; omp_ns;
    5015       163428 :          omp_ns = omp_ns->sibling)
    5016       199542 :       if (omp_ns->proc_name == proc_sym)
    5017              :         break;
    5018              : 
    5019              :   /* Add 'omp allocate' attribute for gfc_trans_auto_array_allocation and
    5020              :      unset attr.omp_allocate for 'omp allocate allocator(omp_default_mem_alloc),
    5021              :      which has the normal codepath except for an invalid-use check in the ME.
    5022              :      The main processing happens later in this function.  */
    5023        98610 :   for (struct gfc_omp_namelist *n = omp_ns ? omp_ns->omp_allocate : NULL;
    5024        98665 :        n; n = n->next)
    5025           55 :     if (!TREE_STATIC (n->sym->backend_decl))
    5026              :       {
    5027              :         /* Add empty entries - described and to be filled below.  */
    5028           36 :         tree tmp = build_tree_list (NULL_TREE, NULL_TREE);
    5029           36 :         TREE_CHAIN (tmp) = build_tree_list (NULL_TREE, NULL_TREE);
    5030           36 :         DECL_ATTRIBUTES (n->sym->backend_decl)
    5031           36 :           = tree_cons (get_identifier ("omp allocate"), tmp,
    5032           36 :                                        DECL_ATTRIBUTES (n->sym->backend_decl));
    5033           36 :         if (n->u.align == NULL
    5034           29 :             && n->u2.allocator != NULL
    5035            7 :             && n->u2.allocator->expr_type == EXPR_CONSTANT
    5036            2 :             && mpz_cmp_si (n->u2.allocator->value.integer, 1) == 0)
    5037            1 :           n->sym->attr.omp_allocate = 0;
    5038              :        }
    5039              : 
    5040       172855 :   for (sym = proc_sym->tlink; sym != proc_sym; sym = sym->tlink)
    5041              :     {
    5042        74245 :       bool alloc_comp_or_fini = (sym->ts.type == BT_DERIVED)
    5043        74245 :                                 && (sym->ts.u.derived->attr.alloc_comp
    5044         5983 :                                     || gfc_is_finalizable (sym->ts.u.derived,
    5045        74245 :                                                            NULL));
    5046        74245 :       if (sym->assoc || sym->attr.vtab)
    5047         5023 :         continue;
    5048              : 
    5049              :       /* Set the vptr of unlimited polymorphic pointer variables so that
    5050              :          they do not cause segfaults in select type, when the selector
    5051              :          is an intrinsic type.  */
    5052        69222 :       if (sym->ts.type == BT_CLASS && UNLIMITED_POLY (sym)
    5053         1029 :           && sym->attr.flavor == FL_VARIABLE && !sym->assoc
    5054         1029 :           && !sym->attr.dummy && CLASS_DATA (sym)->attr.class_pointer)
    5055              :         {
    5056          308 :           gfc_symbol *vtab;
    5057          308 :           gfc_init_block (&tmpblock);
    5058          308 :           vtab = gfc_find_vtab (&sym->ts);
    5059          308 :           if (!vtab->backend_decl)
    5060              :             {
    5061           48 :               if (!vtab->attr.referenced)
    5062            6 :                 gfc_set_sym_referenced (vtab);
    5063           48 :               gfc_get_symbol_decl (vtab);
    5064              :             }
    5065          308 :           tmp = gfc_class_vptr_get (sym->backend_decl);
    5066          308 :           gfc_add_modify (&tmpblock, tmp,
    5067          308 :                           gfc_build_addr_expr (TREE_TYPE (tmp),
    5068              :                                                vtab->backend_decl));
    5069          308 :           gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), NULL);
    5070              :         }
    5071              : 
    5072        69222 :       if (sym->ts.type == BT_DERIVED
    5073        10737 :           && sym->ts.u.derived
    5074        10737 :           && (sym->ts.u.derived->attr.pdt_type || sym->ts.u.derived->attr.pdt_comp))
    5075              :         {
    5076          684 :           is_pdt_type = true;
    5077          684 :           gfc_init_block (&tmpblock);
    5078              : 
    5079          684 :           if (!sym->attr.dummy && !sym->attr.pointer)
    5080              :             {
    5081          532 :               tmp = gfc_init_default_pdt (sym, true);
    5082          532 :               if (!sym->attr.allocatable && tmp == NULL_TREE)
    5083              :                 {
    5084          398 :                   tmp = gfc_allocate_pdt_comp (sym->ts.u.derived,
    5085              :                                                sym->backend_decl,
    5086          398 :                                                sym->as ? sym->as->rank : 0,
    5087          398 :                                                sym->param_list);
    5088          398 :                   gfc_add_expr_to_block (&tmpblock, tmp);
    5089              :                 }
    5090          102 :               else if (tmp != NULL_TREE)
    5091           32 :                 gfc_add_expr_to_block (&tmpblock, tmp);
    5092              : 
    5093          532 :               if (!sym->attr.result && !sym->ts.u.derived->attr.alloc_comp)
    5094          432 :                 tmp = gfc_deallocate_pdt_comp (sym->ts.u.derived,
    5095              :                                                sym->backend_decl,
    5096          432 :                                                sym->as ? sym->as->rank : 0);
    5097              :               else
    5098              :                 tmp = NULL_TREE;
    5099              : 
    5100          532 :               gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), tmp);
    5101              :             }
    5102          152 :           else if (sym->attr.dummy)
    5103              :             {
    5104           62 :               tmp = gfc_check_pdt_dummy (sym->ts.u.derived,
    5105              :                                          sym->backend_decl,
    5106           62 :                                          sym->as ? sym->as->rank : 0,
    5107           62 :                                          sym->param_list);
    5108           62 :               gfc_add_expr_to_block (&tmpblock, tmp);
    5109           62 :               gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), NULL);
    5110              :             }
    5111              :         }
    5112        68538 :       else if (IS_CLASS_PDT (sym))
    5113              :         {
    5114           12 :           gfc_component *data = CLASS_DATA (sym);
    5115           12 :           is_pdt_type = true;
    5116           12 :           gfc_init_block (&tmpblock);
    5117           12 :           if (!(sym->attr.dummy
    5118           12 :                 || CLASS_DATA (sym)->attr.pointer
    5119           12 :                 || CLASS_DATA (sym)->attr.allocatable))
    5120              :             {
    5121            0 :               tmp = gfc_class_data_get (sym->backend_decl);
    5122            0 :               tmp = gfc_allocate_pdt_comp (data->ts.u.derived, tmp,
    5123            0 :                                            data->as ? data->as->rank : 0,
    5124            0 :                                            sym->param_list);
    5125            0 :               gfc_add_expr_to_block (&tmpblock, tmp);
    5126            0 :               tmp = gfc_class_data_get (sym->backend_decl);
    5127            0 :               if (!sym->attr.result)
    5128            0 :                 tmp = gfc_deallocate_pdt_comp (data->ts.u.derived, tmp,
    5129            0 :                                                data->as ? data->as->rank : 0);
    5130              :               else
    5131              :                 tmp = NULL_TREE;
    5132            0 :               gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), tmp);
    5133              :             }
    5134           12 :           else if (sym->attr.dummy)
    5135              :             {
    5136            0 :               tmp = gfc_class_data_get (sym->backend_decl);
    5137            0 :               tmp = gfc_check_pdt_dummy (data->ts.u.derived, tmp,
    5138            0 :                                          data->as ? data->as->rank : 0,
    5139            0 :                                          sym->param_list);
    5140            0 :               gfc_add_expr_to_block (&tmpblock, tmp);
    5141            0 :               gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), NULL);
    5142              :             }
    5143              :         }
    5144              : 
    5145        69222 :       if (sym->ts.type == BT_CLASS
    5146         3845 :           && (sym->attr.save || flag_max_stack_var_size == 0)
    5147           58 :           && CLASS_DATA (sym)->attr.allocatable)
    5148              :         {
    5149           40 :           tree vptr;
    5150              : 
    5151           40 :           if (UNLIMITED_POLY (sym))
    5152            0 :             vptr = null_pointer_node;
    5153              :           else
    5154              :             {
    5155           40 :               gfc_symbol *vsym;
    5156           40 :               vsym = gfc_find_derived_vtab (sym->ts.u.derived);
    5157           40 :               vptr = gfc_get_symbol_decl (vsym);
    5158           40 :               vptr = gfc_build_addr_expr (NULL, vptr);
    5159              :             }
    5160              : 
    5161           40 :           if (CLASS_DATA (sym)->attr.dimension
    5162            8 :               || (CLASS_DATA (sym)->attr.codimension
    5163            1 :                   && flag_coarray != GFC_FCOARRAY_LIB))
    5164              :             {
    5165           33 :               tmp = gfc_class_data_get (sym->backend_decl);
    5166           33 :               tmp = gfc_build_null_descriptor (TREE_TYPE (tmp));
    5167              :             }
    5168              :           else
    5169            7 :             tmp = null_pointer_node;
    5170              : 
    5171           40 :           DECL_INITIAL (sym->backend_decl)
    5172           40 :                 = gfc_class_set_static_fields (sym->backend_decl, vptr, tmp);
    5173           40 :           TREE_CONSTANT (DECL_INITIAL (sym->backend_decl)) = 1;
    5174           40 :         }
    5175        69182 :       else if ((sym->attr.dimension || sym->attr.codimension
    5176        12410 :                 || (IS_CLASS_COARRAY_OR_ARRAY (sym)
    5177         2036 :                     && !CLASS_DATA (sym)->attr.allocatable)))
    5178              :         {
    5179        57670 :           bool is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
    5180        57670 :           symbol_attribute *array_attr;
    5181        57670 :           gfc_array_spec *as;
    5182        57670 :           array_type type_of_array;
    5183              : 
    5184        57670 :           array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
    5185        57670 :           as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
    5186              :           /* Assumed-size Cray pointees need to be treated as AS_EXPLICIT.  */
    5187        57670 :           type_of_array = as->type;
    5188        57670 :           if (type_of_array == AS_ASSUMED_SIZE && as->cp_was_assumed)
    5189              :             type_of_array = AS_EXPLICIT;
    5190        57599 :           switch (type_of_array)
    5191              :             {
    5192        38064 :             case AS_EXPLICIT:
    5193        38064 :               if (sym->attr.dummy || sym->attr.result)
    5194         6315 :                 gfc_trans_dummy_array_bias (sym, sym->backend_decl, block);
    5195              :               /* Allocatable and pointer arrays need to processed
    5196              :                  explicitly.  */
    5197        31749 :               else if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
    5198        31749 :                        || (sym->ts.type == BT_CLASS
    5199            0 :                            && CLASS_DATA (sym)->attr.class_pointer)
    5200        31749 :                        || array_attr->allocatable)
    5201              :                 {
    5202            0 :                   if (TREE_STATIC (sym->backend_decl))
    5203              :                     {
    5204            0 :                       loc = input_location;
    5205            0 :                       input_location = gfc_get_location (&sym->declared_at);
    5206            0 :                       gfc_trans_static_array_pointer (sym);
    5207            0 :                       input_location = loc;
    5208              :                     }
    5209              :                   else
    5210              :                     {
    5211            0 :                       seen_trans_deferred_array = true;
    5212            0 :                       gfc_trans_deferred_array (sym, block);
    5213              :                     }
    5214              :                 }
    5215        31749 :               else if (sym->attr.codimension
    5216          463 :                        && TREE_STATIC (sym->backend_decl))
    5217              :                 {
    5218          402 :                   gfc_init_block (&tmpblock);
    5219          402 :                   gfc_trans_array_cobounds (TREE_TYPE (sym->backend_decl),
    5220              :                                             &tmpblock, sym);
    5221          402 :                   gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock),
    5222              :                                         NULL_TREE);
    5223          402 :                   continue;
    5224              :                 }
    5225        31347 :               else if (sym->attr.codimension && !sym->attr.dimension)
    5226              :                 {
    5227              :                   /* Scalar coarrays do not need array allocation.  */
    5228           39 :                   continue;
    5229              :                 }
    5230              :               else
    5231              :                 {
    5232        31308 :                   loc = input_location;
    5233        31308 :                   input_location = gfc_get_location (&sym->declared_at);
    5234              : 
    5235        31308 :                   if (alloc_comp_or_fini)
    5236              :                     {
    5237          509 :                       seen_trans_deferred_array = true;
    5238          509 :                       gfc_trans_deferred_array (sym, block);
    5239              :                     }
    5240        30799 :                   else if (sym->ts.type == BT_DERIVED
    5241         1755 :                              && sym->value
    5242          541 :                              && !sym->attr.data
    5243          515 :                              && sym->attr.save == SAVE_NONE)
    5244              :                     {
    5245          289 :                       gfc_start_block (&tmpblock);
    5246          289 :                       gfc_init_default_dt (sym, &tmpblock, false);
    5247          289 :                       gfc_add_init_cleanup (block,
    5248              :                                             gfc_finish_block (&tmpblock),
    5249              :                                             NULL_TREE);
    5250              :                     }
    5251              : 
    5252        31308 :                   gfc_trans_auto_array_allocation (sym->backend_decl,
    5253              :                                                    sym, block);
    5254        31308 :                   input_location = loc;
    5255              :                 }
    5256              :               break;
    5257              : 
    5258         1521 :             case AS_ASSUMED_SIZE:
    5259              :               /* Must be a dummy parameter.  */
    5260         1521 :               gcc_assert (sym->attr.dummy || as->cp_was_assumed);
    5261              : 
    5262              :               /* We should always pass assumed size arrays the g77 way.  */
    5263         1521 :               if (sym->attr.dummy)
    5264         1521 :                 gfc_trans_g77_array (sym, block);
    5265              :               break;
    5266              : 
    5267         5721 :             case AS_ASSUMED_SHAPE:
    5268              :               /* Must be a dummy parameter.  */
    5269         5721 :               gcc_assert (sym->attr.dummy);
    5270              : 
    5271         5721 :               gfc_trans_dummy_array_bias (sym, sym->backend_decl, block);
    5272         5721 :               break;
    5273              : 
    5274        12364 :             case AS_ASSUMED_RANK:
    5275        12364 :             case AS_DEFERRED:
    5276        12364 :               seen_trans_deferred_array = true;
    5277        12364 :               gfc_trans_deferred_array (sym, block);
    5278        12364 :               if (sym->ts.type == BT_CHARACTER && sym->ts.deferred
    5279          748 :                   && sym->attr.result)
    5280              :                 {
    5281           32 :                   gfc_start_block (&init);
    5282           32 :                   loc = input_location;
    5283           32 :                   input_location = gfc_get_location (&sym->declared_at);
    5284           32 :                   tmp = gfc_null_and_pass_deferred_len (sym, &init, loc);
    5285           32 :                   gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp);
    5286              :                 }
    5287              :               break;
    5288              : 
    5289            0 :             default:
    5290            0 :               gcc_unreachable ();
    5291              :             }
    5292        57229 :           if (alloc_comp_or_fini && !seen_trans_deferred_array)
    5293          288 :             gfc_trans_deferred_array (sym, block);
    5294              :         }
    5295        11512 :       else if ((!sym->attr.dummy || sym->ts.deferred)
    5296        11316 :                 && (sym->ts.type == BT_CLASS
    5297         2907 :                 && CLASS_DATA (sym)->attr.class_pointer))
    5298          523 :         gfc_trans_class_array (sym, block);
    5299        10989 :       else if ((!sym->attr.dummy || sym->ts.deferred)
    5300        10793 :                 && (sym->attr.allocatable
    5301         8465 :                     || (sym->attr.pointer && sym->attr.result)
    5302         8414 :                     || (sym->ts.type == BT_CLASS
    5303         2384 :                         && CLASS_DATA (sym)->attr.allocatable)))
    5304              :         {
    5305              :           /* Ensure that the initialization block may be generated also for
    5306              :              dummy and result variables when -fno-automatic is specified, which
    5307              :              sets flag_max_stack_var_size=0.  */
    5308         4763 :           if (!sym->attr.save
    5309         4684 :               && (flag_max_stack_var_size != 0
    5310            3 :                   || sym->attr.dummy
    5311            3 :                   || sym->attr.result))
    5312              :             {
    5313         4684 :               tree descriptor = NULL_TREE;
    5314              : 
    5315         4684 :               loc = input_location;
    5316         4684 :               input_location = gfc_get_location (&sym->declared_at);
    5317         4684 :               gfc_start_block (&init);
    5318              : 
    5319         4684 :               if (sym->ts.type == BT_CHARACTER
    5320          985 :                   && sym->attr.allocatable
    5321          961 :                   && !sym->attr.dimension
    5322          961 :                   && sym->ts.u.cl && sym->ts.u.cl->length
    5323          191 :                   && sym->ts.u.cl->length->expr_type == EXPR_VARIABLE)
    5324           27 :                 gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
    5325              : 
    5326         4684 :               if (!sym->attr.pointer)
    5327              :                 {
    5328              :                   /* Nullify and automatic deallocation of allocatable
    5329              :                      scalars.  */
    5330         4633 :                   e = gfc_lval_expr_from_sym (sym);
    5331         4633 :                   if (sym->ts.type == BT_CLASS)
    5332         2384 :                     gfc_add_data_component (e);
    5333              : 
    5334         4633 :                   gfc_init_se (&se, NULL);
    5335         4633 :                   if (sym->ts.type != BT_CLASS
    5336         2384 :                       || sym->ts.u.derived->attr.dimension
    5337         2384 :                       || sym->ts.u.derived->attr.codimension)
    5338              :                     {
    5339         2249 :                       se.want_pointer = 1;
    5340         2249 :                       gfc_conv_expr (&se, e);
    5341              :                     }
    5342         2384 :                   else if (sym->ts.type == BT_CLASS
    5343         2384 :                            && !CLASS_DATA (sym)->attr.dimension
    5344         1304 :                            && !CLASS_DATA (sym)->attr.codimension)
    5345              :                     {
    5346         1246 :                       se.want_pointer = 1;
    5347         1246 :                       gfc_conv_expr (&se, e);
    5348              :                     }
    5349              :                   else
    5350              :                     {
    5351         1138 :                       se.descriptor_only = 1;
    5352         1138 :                       gfc_conv_expr (&se, e);
    5353         1138 :                       descriptor = se.expr;
    5354         1138 :                       se.expr = gfc_conv_descriptor_data_get (se.expr);
    5355              :                     }
    5356         4633 :                   gfc_free_expr (e);
    5357              : 
    5358         4633 :                   if (!sym->attr.dummy || sym->attr.intent == INTENT_OUT)
    5359              :                     {
    5360              :                       /* Nullify when entering the scope.  */
    5361         4633 :                       if (sym->ts.type == BT_CLASS
    5362         2384 :                           && (CLASS_DATA (sym)->attr.dimension
    5363         1304 :                               || CLASS_DATA (sym)->attr.codimension))
    5364              :                         {
    5365         1138 :                           stmtblock_t nullify;
    5366         1138 :                           gfc_init_block (&nullify);
    5367         1138 :                           gfc_conv_descriptor_data_set (&nullify, descriptor,
    5368              :                                                         null_pointer_node);
    5369         1138 :                           tmp = gfc_finish_block (&nullify);
    5370         1138 :                         }
    5371              :                       else
    5372              :                         {
    5373         3495 :                           tree typed_null = fold_convert (TREE_TYPE (se.expr),
    5374              :                                                           null_pointer_node);
    5375         3495 :                           tmp = fold_build2_loc (input_location, MODIFY_EXPR,
    5376         3495 :                                                  TREE_TYPE (se.expr), se.expr,
    5377              :                                                  typed_null);
    5378              :                         }
    5379         4633 :                       if (sym->attr.optional)
    5380              :                         {
    5381            0 :                           tree present = gfc_conv_expr_present (sym);
    5382            0 :                           tmp = build3_loc (input_location, COND_EXPR,
    5383              :                                             void_type_node, present, tmp,
    5384              :                                             build_empty_stmt (input_location));
    5385              :                         }
    5386         4633 :                       gfc_add_expr_to_block (&init, tmp);
    5387              :                     }
    5388              :                 }
    5389              : 
    5390         4684 :               if ((sym->attr.dummy || sym->attr.result)
    5391          504 :                     && sym->ts.type == BT_CHARACTER
    5392          117 :                     && sym->ts.deferred
    5393          117 :                     && sym->ts.u.cl->passed_length)
    5394          117 :                 tmp = gfc_null_and_pass_deferred_len (sym, &init, loc);
    5395              :               else
    5396              :                 {
    5397         4567 :                   input_location = loc;
    5398         4567 :                   tmp = NULL_TREE;
    5399              :                 }
    5400              : 
    5401              :               /* Initialize descriptor's TKR information.  */
    5402         4684 :               if (sym->ts.type == BT_CLASS)
    5403         2384 :                 gfc_trans_class_array (sym, block);
    5404              : 
    5405              :               /* Deallocate when leaving the scope. Nullifying is not
    5406              :                  needed.  */
    5407         4684 :               if (!sym->attr.result && !sym->attr.dummy && !sym->attr.pointer
    5408         4180 :                   && !sym->ns->proc_name->attr.is_main_program)
    5409              :                 {
    5410         1403 :                   if (sym->ts.type == BT_CLASS
    5411          489 :                       && CLASS_DATA (sym)->attr.codimension)
    5412            6 :                     tmp = gfc_deallocate_with_status (descriptor, NULL_TREE,
    5413              :                                                       NULL_TREE, NULL_TREE,
    5414              :                                                       NULL_TREE, true, NULL,
    5415              :                                                       GFC_CAF_COARRAY_ANALYZE);
    5416              :                   else
    5417              :                     {
    5418         1397 :                       gfc_expr *expr = gfc_lval_expr_from_sym (sym);
    5419         1397 :                       tmp = gfc_deallocate_scalar_with_status (se.expr,
    5420              :                                                                NULL_TREE,
    5421              :                                                                NULL_TREE,
    5422              :                                                                true, expr,
    5423              :                                                                sym->ts);
    5424         1397 :                       gfc_free_expr (expr);
    5425              :                     }
    5426              :                 }
    5427              : 
    5428         4684 :               if (sym->ts.type == BT_CLASS)
    5429              :                 {
    5430              :                   /* Initialize _vptr to declared type.  */
    5431         2384 :                   loc = input_location;
    5432         2384 :                   input_location = gfc_get_location (&sym->declared_at);
    5433              : 
    5434         2384 :                   e = gfc_lval_expr_from_sym (sym);
    5435         2384 :                   gfc_reset_vptr (&init, e);
    5436         2384 :                   gfc_free_expr (e);
    5437         2384 :                   input_location = loc;
    5438              :                 }
    5439              : 
    5440         4684 :               gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp);
    5441              :             }
    5442              :         }
    5443         6226 :       else if (sym->ts.type == BT_CHARACTER && sym->ts.deferred)
    5444              :         {
    5445          244 :           tree tmp = NULL;
    5446          244 :           stmtblock_t init;
    5447              : 
    5448              :           /* If we get to here, all that should be left are pointers.  */
    5449          244 :           gcc_assert (sym->attr.pointer);
    5450              : 
    5451          244 :           if (sym->attr.dummy)
    5452              :             {
    5453            0 :               gfc_start_block (&init);
    5454            0 :               loc = input_location;
    5455            0 :               input_location = gfc_get_location (&sym->declared_at);
    5456            0 :               tmp = gfc_null_and_pass_deferred_len (sym, &init, loc);
    5457            0 :               gfc_add_init_cleanup (block, gfc_finish_block (&init), tmp);
    5458              :             }
    5459          244 :         }
    5460         5982 :       else if (sym->ts.deferred)
    5461            0 :         gfc_fatal_error ("Deferred type parameter not yet supported");
    5462         5982 :       else if (alloc_comp_or_fini)
    5463         4655 :         gfc_trans_deferred_array (sym, block);
    5464         1327 :       else if (sym->ts.type == BT_CHARACTER)
    5465              :         {
    5466          696 :           loc = input_location;
    5467          696 :           input_location = gfc_get_location (&sym->declared_at);
    5468          696 :           if (sym->attr.dummy || sym->attr.result)
    5469          342 :             gfc_trans_dummy_character (sym, sym->ts.u.cl, block);
    5470              :           else
    5471          354 :             gfc_trans_auto_character_variable (sym, block);
    5472          696 :           input_location = loc;
    5473              :         }
    5474          631 :       else if (sym->attr.assign)
    5475              :         {
    5476           64 :           loc = input_location;
    5477           64 :           input_location = gfc_get_location (&sym->declared_at);
    5478           64 :           gfc_trans_assign_aux_var (sym, block);
    5479           64 :           input_location = loc;
    5480              :         }
    5481          567 :       else if (sym->ts.type == BT_DERIVED
    5482          567 :                  && sym->value
    5483          483 :                  && !sym->attr.data
    5484          483 :                  && sym->attr.save == SAVE_NONE)
    5485              :         {
    5486          464 :           gfc_start_block (&tmpblock);
    5487          464 :           gfc_init_default_dt (sym, &tmpblock, false);
    5488          464 :           gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock),
    5489              :                                 NULL_TREE);
    5490              :         }
    5491          103 :       else if (!(UNLIMITED_POLY(sym)) && !is_pdt_type)
    5492            0 :         gcc_unreachable ();
    5493              :     }
    5494              : 
    5495              :   /* Handle 'omp allocate'. This has to be after the block above as
    5496              :      gfc_add_init_cleanup (..., init, ...) puts 'init' of later calls
    5497              :      before earlier calls.  The code is a bit more complex as gfortran does
    5498              :      not really work with bind expressions / BIND_EXPR_VARS properly, i.e.
    5499              :      gimplify_bind_expr needs some help for placing the GOMP_alloc. Thus,
    5500              :      we pass on the location of the allocate-assignment expression and,
    5501              :      if the size is not constant, the size variable if Fortran computes this
    5502              :      differently. We also might add an expression location after which the
    5503              :      code has to be added, e.g. for character len expressions, which affect
    5504              :      the UNIT_SIZE.  */
    5505        98610 :   gfc_expr *last_allocator = NULL;
    5506        98610 :   if (omp_ns && omp_ns->omp_allocate)
    5507              :     {
    5508           29 :       if (!block->init || TREE_CODE (block->init) != STATEMENT_LIST)
    5509              :         {
    5510           22 :           tree tmp = build1_v (LABEL_EXPR, gfc_build_label_decl (NULL_TREE));
    5511           22 :           append_to_statement_list (tmp, &block->init);
    5512              :         }
    5513           29 :       if (!block->cleanup || TREE_CODE (block->cleanup) != STATEMENT_LIST)
    5514              :         {
    5515           29 :           tree tmp = build1_v (LABEL_EXPR, gfc_build_label_decl (NULL_TREE));
    5516           29 :           append_to_statement_list (tmp, &block->cleanup);
    5517              :         }
    5518              :     }
    5519        98610 :   tree init_stmtlist = block->init;
    5520        98610 :   tree cleanup_stmtlist = block->cleanup;
    5521        98610 :   se.expr = NULL_TREE;
    5522        98610 :   for (struct gfc_omp_namelist *n = omp_ns ? omp_ns->omp_allocate : NULL;
    5523        98665 :        n; n = n->next)
    5524              :     {
    5525           55 :       tree align = (n->u.align ? gfc_conv_constant_to_tree (n->u.align) : NULL_TREE);
    5526           55 :       if (last_allocator != n->u2.allocator)
    5527              :         {
    5528           24 :           location_t loc = input_location;
    5529           24 :           gfc_init_se (&se, NULL);
    5530           24 :           if (n->u2.allocator)
    5531              :             {
    5532           22 :               input_location = gfc_get_location (&n->u2.allocator->where);
    5533           22 :               gfc_conv_expr (&se, n->u2.allocator);
    5534              :             }
    5535              :           /* We need to evalulate non-constants - also to find the location
    5536              :              after which the GOMP_alloc has to be added to - also as BLOCK
    5537              :              does not yield a new BIND_EXPR_BODY.  */
    5538           24 :           if (n->u2.allocator
    5539              :               && (!(CONSTANT_CLASS_P (se.expr) && DECL_P (se.expr))
    5540              :                   || se.pre.head || se.post.head))
    5541              :             {
    5542           22 :               stmtblock_t tmpblock;
    5543           22 :               gfc_init_block (&tmpblock);
    5544           22 :               se.expr = gfc_evaluate_now (se.expr, &tmpblock);
    5545              :               /* First post then pre because the new code is inserted
    5546              :                  at the top. */
    5547           22 :               gfc_add_init_cleanup (block, gfc_finish_block (&se.post), NULL);
    5548           22 :               gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock),
    5549              :                                     NULL);
    5550           22 :               gfc_add_init_cleanup (block, gfc_finish_block (&se.pre), NULL);
    5551              :             }
    5552           24 :           last_allocator = n->u2.allocator;
    5553           24 :           input_location = loc;
    5554              :         }
    5555           55 :       if (TREE_STATIC (n->sym->backend_decl))
    5556           19 :         continue;
    5557              :       /* 'omp allocate( {purpose: allocator, value: align},
    5558              :                         {purpose: init-stmtlist, value: cleanup-stmtlist},
    5559              :                         {purpose: size-var, value: last-size-expr} )
    5560              :           where init-stmt/cleanup-stmt is the STATEMENT list to find the
    5561              :           try-final block; last-size-expr is to find the location after
    5562              :           which to add the code and 'size-var' is for the proper size, cf.
    5563              :           gfc_trans_auto_array_allocation - either or both of the latter
    5564              :           can be NULL.  */
    5565           36 :       tree tmp = lookup_attribute ("omp allocate",
    5566           36 :                                    DECL_ATTRIBUTES (n->sym->backend_decl));
    5567           36 :       tmp = TREE_VALUE (tmp);
    5568           36 :       TREE_PURPOSE (tmp) = se.expr;
    5569           36 :       TREE_VALUE (tmp) = align;
    5570           36 :       TREE_PURPOSE (TREE_CHAIN (tmp)) = init_stmtlist;
    5571           36 :       TREE_VALUE (TREE_CHAIN (tmp)) = cleanup_stmtlist;
    5572              :     }
    5573              : 
    5574        98610 :   gfc_init_block (&tmpblock);
    5575              : 
    5576       199799 :   for (f = gfc_sym_get_dummy_args (proc_sym); f; f = f->next)
    5577              :     {
    5578       101189 :       if (f->sym && f->sym->tlink == NULL && f->sym->ts.type == BT_CHARACTER
    5579         7771 :           && f->sym->ts.u.cl->backend_decl)
    5580              :         {
    5581         7769 :           if (TREE_CODE (f->sym->ts.u.cl->backend_decl) == PARM_DECL)
    5582         4158 :             gfc_trans_vla_type_sizes (f->sym, &tmpblock);
    5583              :         }
    5584              :     }
    5585              : 
    5586       101479 :   if (gfc_return_by_reference (proc_sym) && proc_sym->ts.type == BT_CHARACTER
    5587       100059 :       && current_fake_result_decl != NULL)
    5588              :     {
    5589          832 :       gcc_assert (proc_sym->ts.u.cl->backend_decl != NULL);
    5590          832 :       if (TREE_CODE (proc_sym->ts.u.cl->backend_decl) == PARM_DECL)
    5591           69 :         gfc_trans_vla_type_sizes (proc_sym, &tmpblock);
    5592              :     }
    5593              : 
    5594        98610 :   gfc_add_init_cleanup (block, gfc_finish_block (&tmpblock), NULL_TREE);
    5595        98610 : }
    5596              : 
    5597              : 
    5598              : struct module_hasher : ggc_ptr_hash<module_htab_entry>
    5599              : {
    5600              :   typedef const char *compare_type;
    5601              : 
    5602        26512 :   static hashval_t hash (module_htab_entry *s)
    5603              :   {
    5604        26512 :     return htab_hash_string (s->name);
    5605              :   }
    5606              : 
    5607              :   static bool
    5608        31467 :   equal (module_htab_entry *a, const char *b)
    5609              :   {
    5610        31467 :     return !strcmp (a->name, b);
    5611              :   }
    5612              : };
    5613              : 
    5614              : static GTY (()) hash_table<module_hasher> *module_htab;
    5615              : 
    5616              : /* Hash and equality functions for module_htab's decls.  */
    5617              : 
    5618              : hashval_t
    5619       176801 : module_decl_hasher::hash (tree t)
    5620              : {
    5621       176801 :   const_tree n = DECL_NAME (t);
    5622       176801 :   if (n == NULL_TREE)
    5623        25861 :     n = TYPE_NAME (TREE_TYPE (t));
    5624       176801 :   return htab_hash_string (IDENTIFIER_POINTER (n));
    5625              : }
    5626              : 
    5627              : bool
    5628       184093 : module_decl_hasher::equal (tree t1, const char *x2)
    5629              : {
    5630       184093 :   const_tree n1 = DECL_NAME (t1);
    5631       184093 :   if (n1 == NULL_TREE)
    5632        26165 :     n1 = TYPE_NAME (TREE_TYPE (t1));
    5633       184093 :   return strcmp (IDENTIFIER_POINTER (n1), x2) == 0;
    5634              : }
    5635              : 
    5636              : struct module_htab_entry *
    5637        29711 : gfc_find_module (const char *name)
    5638              : {
    5639        29711 :   if (! module_htab)
    5640         8089 :     module_htab = hash_table<module_hasher>::create_ggc (10);
    5641              : 
    5642        29711 :   module_htab_entry **slot
    5643        29711 :     = module_htab->find_slot_with_hash (name, htab_hash_string (name), INSERT);
    5644        29711 :   if (*slot == NULL)
    5645              :     {
    5646        10324 :       module_htab_entry *entry = ggc_cleared_alloc<module_htab_entry> ();
    5647              : 
    5648        10324 :       entry->name = gfc_get_string ("%s", name);
    5649        10324 :       entry->decls = hash_table<module_decl_hasher>::create_ggc (10);
    5650        10324 :       *slot = entry;
    5651              :     }
    5652        29711 :   return *slot;
    5653              : }
    5654              : 
    5655              : void
    5656        53632 : gfc_module_add_decl (struct module_htab_entry *entry, tree decl)
    5657              : {
    5658        53632 :   const char *name;
    5659              : 
    5660        53632 :   if (DECL_NAME (decl))
    5661        45599 :     name = IDENTIFIER_POINTER (DECL_NAME (decl));
    5662              :   else
    5663              :     {
    5664         8033 :       gcc_assert (TREE_CODE (decl) == TYPE_DECL);
    5665         8033 :       name = IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl)));
    5666              :     }
    5667        53632 :   tree *slot
    5668        53632 :     = entry->decls->find_slot_with_hash (name, htab_hash_string (name),
    5669              :                                          INSERT);
    5670        53632 :   if (*slot == NULL)
    5671        53620 :     *slot = decl;
    5672        53632 : }
    5673              : 
    5674              : 
    5675              : /* Generate debugging symbols for namelists. This function must come after
    5676              :    generate_local_decl to ensure that the variables in the namelist are
    5677              :    already declared.  */
    5678              : 
    5679              : static tree
    5680          743 : generate_namelist_decl (gfc_symbol * sym)
    5681              : {
    5682          743 :   gfc_namelist *nml;
    5683          743 :   tree decl;
    5684          743 :   vec<constructor_elt, va_gc> *nml_decls = NULL;
    5685              : 
    5686          743 :   gcc_assert (sym->attr.flavor == FL_NAMELIST);
    5687         2745 :   for (nml = sym->namelist; nml; nml = nml->next)
    5688              :     {
    5689         2002 :       if (nml->sym->backend_decl == NULL_TREE)
    5690              :         {
    5691          220 :           nml->sym->attr.referenced = 1;
    5692          220 :           nml->sym->backend_decl = gfc_get_symbol_decl (nml->sym);
    5693              :         }
    5694         2002 :       DECL_IGNORED_P (nml->sym->backend_decl) = 0;
    5695         2002 :       CONSTRUCTOR_APPEND_ELT (nml_decls, NULL_TREE, nml->sym->backend_decl);
    5696              :     }
    5697              : 
    5698          743 :   decl = make_node (NAMELIST_DECL);
    5699          743 :   TREE_TYPE (decl) = void_type_node;
    5700          743 :   NAMELIST_DECL_ASSOCIATED_DECL (decl) = build_constructor (NULL_TREE, nml_decls);
    5701          743 :   DECL_NAME (decl) = get_identifier (sym->name);
    5702          743 :   return decl;
    5703              : }
    5704              : 
    5705              : 
    5706              : /* Output an initialized decl for a module variable.  */
    5707              : 
    5708              : static void
    5709       142892 : gfc_create_module_variable (gfc_symbol * sym)
    5710              : {
    5711       142892 :   tree decl;
    5712              : 
    5713              :   /* Module functions with alternate entries are dealt with later and
    5714              :      would get caught by the next condition.  */
    5715       142892 :   if (sym->attr.entry)
    5716              :     return;
    5717              : 
    5718              :   /* Make sure we convert the types of the derived types from iso_c_binding
    5719              :      into (void *).  */
    5720       142411 :   if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
    5721        31290 :       && sym->ts.type == BT_DERIVED)
    5722         1448 :     sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
    5723              : 
    5724       142411 :   if (gfc_fl_struct (sym->attr.flavor)
    5725        23045 :       && sym->backend_decl
    5726         8067 :       && TREE_CODE (sym->backend_decl) == RECORD_TYPE)
    5727              :     {
    5728         8033 :       decl = sym->backend_decl;
    5729         8033 :       gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE);
    5730              : 
    5731         8033 :       if (!sym->attr.use_assoc && !sym->attr.used_in_submodule)
    5732              :         {
    5733         7936 :           gcc_assert (TYPE_CONTEXT (decl) == NULL_TREE
    5734              :                       || TYPE_CONTEXT (decl) == sym->ns->proc_name->backend_decl);
    5735         7936 :           gcc_assert (DECL_CONTEXT (TYPE_STUB_DECL (decl)) == NULL_TREE
    5736              :                       || DECL_CONTEXT (TYPE_STUB_DECL (decl))
    5737              :                            == sym->ns->proc_name->backend_decl);
    5738              :         }
    5739         8033 :       TYPE_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
    5740         8033 :       DECL_CONTEXT (TYPE_STUB_DECL (decl)) = sym->ns->proc_name->backend_decl;
    5741         8033 :       gfc_module_add_decl (cur_module, TYPE_STUB_DECL (decl));
    5742              :     }
    5743              : 
    5744              :   /* Only output variables, procedure pointers and array valued,
    5745              :      or derived type, parameters.  */
    5746       142411 :   if (sym->attr.flavor != FL_VARIABLE
    5747       119975 :         && !(sym->attr.flavor == FL_PARAMETER
    5748        39394 :                && (sym->attr.dimension || sym->ts.type == BT_DERIVED))
    5749       118935 :         && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.proc_pointer))
    5750              :     return;
    5751              : 
    5752        23549 :   if ((sym->attr.in_common || sym->attr.in_equivalence) && sym->backend_decl)
    5753              :     {
    5754          461 :       decl = sym->backend_decl;
    5755          461 :       gcc_assert (DECL_FILE_SCOPE_P (decl));
    5756          461 :       gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE);
    5757          461 :       DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
    5758          461 :       gfc_module_add_decl (cur_module, decl);
    5759              :     }
    5760              : 
    5761              :   /* Don't generate variables from other modules. Variables from
    5762              :      COMMONs and Cray pointees will already have been generated.  */
    5763        23549 :   if (sym->attr.use_assoc || sym->attr.used_in_submodule
    5764        19845 :       || sym->attr.in_common || sym->attr.cray_pointee)
    5765              :     return;
    5766              : 
    5767              :   /* Equivalenced variables arrive here after creation.  */
    5768        19526 :   if (sym->backend_decl
    5769          581 :       && (sym->equiv_built || sym->attr.in_equivalence))
    5770              :     return;
    5771              : 
    5772        19439 :   if (sym->backend_decl && !sym->attr.vtab && !sym->attr.target)
    5773            0 :     gfc_internal_error ("backend decl for module variable %qs already exists",
    5774              :                         sym->name);
    5775              : 
    5776        19439 :   if (sym->module && !sym->attr.result && !sym->attr.dummy
    5777        19439 :       && (sym->attr.access == ACCESS_UNKNOWN
    5778         3714 :           && (sym->ns->default_access == ACCESS_PRIVATE
    5779         3424 :               || (sym->ns->default_access == ACCESS_UNKNOWN
    5780         3410 :                   && flag_module_private))))
    5781          291 :     sym->attr.access = ACCESS_PRIVATE;
    5782              : 
    5783        19439 :   if (warn_unused_variable && !sym->attr.referenced
    5784          138 :       && sym->attr.access == ACCESS_PRIVATE)
    5785            3 :     gfc_warning (OPT_Wunused_value,
    5786              :                  "Unused PRIVATE module variable %qs declared at %L",
    5787              :                  sym->name, &sym->declared_at);
    5788              : 
    5789              :   /* We always want module variables to be created.  */
    5790        19439 :   sym->attr.referenced = 1;
    5791              :   /* Create the decl.  */
    5792        19439 :   decl = gfc_get_symbol_decl (sym);
    5793              : 
    5794              :   /* Create the variable.  */
    5795        19439 :   pushdecl (decl);
    5796        19439 :   gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE
    5797              :               || ((sym->ns->parent->proc_name->attr.flavor == FL_MODULE
    5798              :                    || sym->ns->parent->proc_name->attr.flavor == FL_PROCEDURE)
    5799              :                   && sym->fn_result_spec));
    5800        19439 :   DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
    5801        19439 :   rest_of_decl_compilation (decl, 1, 0);
    5802        19439 :   gfc_module_add_decl (cur_module, decl);
    5803              : 
    5804              :   /* Also add length of strings.  */
    5805        19439 :   if (sym->ts.type == BT_CHARACTER)
    5806              :     {
    5807          400 :       tree length;
    5808              : 
    5809          400 :       length = sym->ts.u.cl->backend_decl;
    5810          400 :       gcc_assert (length || sym->attr.proc_pointer);
    5811          399 :       if (length && !INTEGER_CST_P (length))
    5812              :         {
    5813           54 :           pushdecl (length);
    5814           54 :           rest_of_decl_compilation (length, 1, 0);
    5815              :         }
    5816              :     }
    5817              : 
    5818        19439 :   if (sym->attr.codimension && !sym->attr.dummy && !sym->attr.allocatable
    5819           26 :       && sym->attr.referenced && !sym->attr.use_assoc)
    5820           26 :     has_coarray_vars_or_accessors = true;
    5821              : }
    5822              : 
    5823              : /* Emit debug information for USE statements.  */
    5824              : 
    5825              : static void
    5826        93721 : gfc_trans_use_stmts (gfc_namespace * ns)
    5827              : {
    5828        93721 :   gfc_use_list *use_stmt;
    5829       105304 :   for (use_stmt = ns->use_stmts; use_stmt; use_stmt = use_stmt->next)
    5830              :     {
    5831        11583 :       struct module_htab_entry *entry
    5832        11583 :         = gfc_find_module (use_stmt->module_name);
    5833        11583 :       gfc_use_rename *rent;
    5834              : 
    5835        11583 :       if (entry->namespace_decl == NULL)
    5836              :         {
    5837         1261 :           entry->namespace_decl
    5838         1261 :             = build_decl (input_location,
    5839              :                           NAMESPACE_DECL,
    5840              :                           get_identifier (use_stmt->module_name),
    5841              :                           void_type_node);
    5842         1261 :           DECL_EXTERNAL (entry->namespace_decl) = 1;
    5843              :         }
    5844        11583 :       input_location = gfc_get_location (&use_stmt->where);
    5845        11583 :       if (!use_stmt->only_flag)
    5846        10034 :         (*debug_hooks->imported_module_or_decl) (entry->namespace_decl,
    5847              :                                                  NULL_TREE,
    5848        10034 :                                                  ns->proc_name->backend_decl,
    5849              :                                                  false, false);
    5850        14196 :       for (rent = use_stmt->rename; rent; rent = rent->next)
    5851              :         {
    5852         2613 :           tree decl, local_name;
    5853              : 
    5854         2613 :           if (rent->op != INTRINSIC_NONE)
    5855          104 :             continue;
    5856              : 
    5857         2509 :                                                  hashval_t hash = htab_hash_string (rent->use_name);
    5858         2509 :           tree *slot = entry->decls->find_slot_with_hash (rent->use_name, hash,
    5859              :                                                           INSERT);
    5860         2509 :           if (*slot == NULL)
    5861              :             {
    5862         1403 :               gfc_symtree *st;
    5863              : 
    5864         1403 :               st = gfc_find_symtree (ns->sym_root,
    5865         1403 :                                      rent->local_name[0]
    5866              :                                      ? rent->local_name : rent->use_name);
    5867              : 
    5868              :               /* The following can happen if a derived type is renamed.  */
    5869         1403 :               if (!st)
    5870              :                 {
    5871            0 :                   char *name;
    5872            0 :                   name = xstrdup (rent->local_name[0]
    5873              :                                   ? rent->local_name : rent->use_name);
    5874            0 :                   name[0] = (char) TOUPPER ((unsigned char) name[0]);
    5875            0 :                   st = gfc_find_symtree (ns->sym_root, name);
    5876            0 :                   free (name);
    5877            0 :                   gcc_assert (st);
    5878              :                 }
    5879              : 
    5880              :               /* Sometimes, generic interfaces wind up being over-ruled by a
    5881              :                  local symbol (see PR41062).  */
    5882         1403 :               if (!st->n.sym->attr.use_assoc)
    5883              :                 {
    5884            2 :                   *slot = error_mark_node;
    5885            2 :                   entry->decls->clear_slot (slot);
    5886            2 :                   continue;
    5887              :                 }
    5888              : 
    5889         1401 :               if (st->n.sym->backend_decl
    5890          167 :                   && DECL_P (st->n.sym->backend_decl)
    5891          165 :                   && st->n.sym->module
    5892          165 :                   && strcmp (st->n.sym->module, use_stmt->module_name) == 0)
    5893              :                 {
    5894          156 :                   gcc_assert (DECL_EXTERNAL (entry->namespace_decl)
    5895              :                               || !VAR_P (st->n.sym->backend_decl));
    5896          156 :                   decl = copy_node (st->n.sym->backend_decl);
    5897          156 :                   DECL_CONTEXT (decl) = entry->namespace_decl;
    5898          156 :                   DECL_EXTERNAL (decl) = 1;
    5899          156 :                   DECL_IGNORED_P (decl) = 0;
    5900          156 :                   DECL_INITIAL (decl) = NULL_TREE;
    5901              :                 }
    5902         1245 :               else if (st->n.sym->attr.flavor == FL_NAMELIST
    5903            0 :                        && st->n.sym->attr.use_only
    5904            0 :                        && st->n.sym->module
    5905            0 :                        && strcmp (st->n.sym->module, use_stmt->module_name)
    5906              :                           == 0)
    5907              :                 {
    5908            0 :                   decl = generate_namelist_decl (st->n.sym);
    5909            0 :                   DECL_CONTEXT (decl) = entry->namespace_decl;
    5910            0 :                   DECL_EXTERNAL (decl) = 1;
    5911            0 :                   DECL_IGNORED_P (decl) = 0;
    5912            0 :                   DECL_INITIAL (decl) = NULL_TREE;
    5913              :                 }
    5914              :               else
    5915              :                 {
    5916         1245 :                   *slot = error_mark_node;
    5917         1245 :                   entry->decls->clear_slot (slot);
    5918         1245 :                   continue;
    5919              :                 }
    5920          156 :               *slot = decl;
    5921              :             }
    5922         1262 :           decl = (tree) *slot;
    5923         1262 :           if (rent->local_name[0])
    5924          198 :             local_name = get_identifier (rent->local_name);
    5925              :           else
    5926              :             local_name = NULL_TREE;
    5927         1262 :           input_location = gfc_get_location (&rent->where);
    5928         1262 :           (*debug_hooks->imported_module_or_decl) (decl, local_name,
    5929         1262 :                                                    ns->proc_name->backend_decl,
    5930         1262 :                                                    !use_stmt->only_flag,
    5931              :                                                    false);
    5932              :         }
    5933              :     }
    5934        93721 : }
    5935              : 
    5936              : 
    5937              : /* Return true if expr is a constant initializer that gfc_conv_initializer
    5938              :    will handle.  */
    5939              : 
    5940              : static bool
    5941        23162 : check_constant_initializer (gfc_expr *expr, gfc_typespec *ts, bool array,
    5942              :                             bool pointer)
    5943              : {
    5944        23162 :   gfc_constructor *c;
    5945        23162 :   gfc_component *cm;
    5946              : 
    5947        23162 :   if (pointer)
    5948              :     return true;
    5949        23158 :   else if (array)
    5950              :     {
    5951         2579 :       if (expr->expr_type == EXPR_CONSTANT || expr->expr_type == EXPR_NULL)
    5952              :         return true;
    5953         2550 :       else if (expr->expr_type == EXPR_STRUCTURE)
    5954           12 :         return check_constant_initializer (expr, ts, false, false);
    5955         2538 :       else if (expr->expr_type != EXPR_ARRAY)
    5956              :         return false;
    5957         2538 :       for (c = gfc_constructor_first (expr->value.constructor);
    5958       134061 :            c; c = gfc_constructor_next (c))
    5959              :         {
    5960       131523 :           if (c->iterator)
    5961              :             return false;
    5962       131523 :           if (c->expr->expr_type == EXPR_STRUCTURE)
    5963              :             {
    5964          284 :               if (!check_constant_initializer (c->expr, ts, false, false))
    5965              :                 return false;
    5966              :             }
    5967       131239 :           else if (c->expr->expr_type != EXPR_CONSTANT)
    5968              :             return false;
    5969              :         }
    5970              :       return true;
    5971              :     }
    5972        20579 :   else switch (ts->type)
    5973              :     {
    5974          441 :     case_bt_struct:
    5975          441 :       if (expr->expr_type != EXPR_STRUCTURE)
    5976              :         return false;
    5977          441 :       cm = expr->ts.u.derived->components;
    5978          441 :       for (c = gfc_constructor_first (expr->value.constructor);
    5979         1005 :            c; c = gfc_constructor_next (c), cm = cm->next)
    5980              :         {
    5981          572 :           if (!c->expr || cm->attr.allocatable)
    5982           14 :             continue;
    5983          558 :           if (!check_constant_initializer (c->expr, &cm->ts,
    5984              :                                            cm->attr.dimension,
    5985              :                                            cm->attr.pointer))
    5986              :             return false;
    5987              :         }
    5988              :       return true;
    5989        20138 :     default:
    5990        20138 :       return expr->expr_type == EXPR_CONSTANT;
    5991              :     }
    5992              : }
    5993              : 
    5994              : /* Emit debug info for parameters and unreferenced variables with
    5995              :    initializers.  */
    5996              : 
    5997              : static void
    5998      1202738 : gfc_emit_parameter_debug_info (gfc_symbol *sym)
    5999              : {
    6000      1202738 :   tree decl;
    6001              : 
    6002      1202738 :   if (sym->attr.flavor != FL_PARAMETER
    6003       956661 :       && (sym->attr.flavor != FL_VARIABLE || sym->attr.referenced))
    6004              :     return;
    6005              : 
    6006       284574 :   if (sym->backend_decl != NULL
    6007       262721 :       || sym->value == NULL
    6008       233777 :       || sym->attr.use_assoc
    6009        22368 :       || sym->attr.dummy
    6010        22368 :       || sym->attr.result
    6011        22368 :       || sym->attr.function
    6012        22368 :       || sym->attr.intrinsic
    6013        22368 :       || sym->attr.pointer
    6014        22358 :       || sym->attr.allocatable
    6015        22358 :       || sym->attr.cray_pointee
    6016        22358 :       || sym->attr.threadprivate
    6017        22357 :       || sym->attr.is_bind_c
    6018        22357 :       || sym->attr.subref_array_pointer
    6019        22357 :       || sym->attr.assign)
    6020              :     return;
    6021              : 
    6022        22357 :   if (sym->ts.type == BT_CHARACTER)
    6023              :     {
    6024         1340 :       gfc_conv_const_charlen (sym->ts.u.cl);
    6025         1340 :       if (sym->ts.u.cl->backend_decl == NULL
    6026         1340 :           || TREE_CODE (sym->ts.u.cl->backend_decl) != INTEGER_CST)
    6027              :         return;
    6028              :     }
    6029        21017 :   else if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.alloc_comp)
    6030              :     return;
    6031              : 
    6032        22308 :   if (sym->as)
    6033              :     {
    6034         2513 :       int n;
    6035              : 
    6036         2513 :       if (sym->as->type != AS_EXPLICIT)
    6037              :         return;
    6038         5668 :       for (n = 0; n < sym->as->rank; n++)
    6039         3155 :         if (sym->as->lower[n]->expr_type != EXPR_CONSTANT
    6040         3155 :             || sym->as->upper[n] == NULL
    6041         3155 :             || sym->as->upper[n]->expr_type != EXPR_CONSTANT)
    6042              :           return;
    6043              :     }
    6044              : 
    6045        22308 :   if (!check_constant_initializer (sym->value, &sym->ts,
    6046        22308 :                                    sym->attr.dimension, false))
    6047              :     return;
    6048              : 
    6049        22299 :   if (flag_coarray == GFC_FCOARRAY_LIB && sym->attr.codimension)
    6050              :     return;
    6051              : 
    6052              :   /* Create the decl for the variable or constant.  */
    6053        44598 :   decl = build_decl (input_location,
    6054        22299 :                      sym->attr.flavor == FL_PARAMETER ? CONST_DECL : VAR_DECL,
    6055              :                      gfc_sym_identifier (sym), gfc_sym_type (sym));
    6056        22299 :   if (sym->attr.flavor == FL_PARAMETER)
    6057        22062 :     TREE_READONLY (decl) = 1;
    6058        22299 :   gfc_set_decl_location (decl, &sym->declared_at);
    6059        22299 :   if (sym->attr.dimension)
    6060         2513 :     GFC_DECL_PACKED_ARRAY (decl) = 1;
    6061        22299 :   DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
    6062        22299 :   TREE_STATIC (decl) = 1;
    6063        22299 :   TREE_USED (decl) = 1;
    6064        22299 :   if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
    6065         2489 :     TREE_PUBLIC (decl) = 1;
    6066        22299 :   DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
    6067        22299 :                                               TREE_TYPE (decl),
    6068        22299 :                                               sym->attr.dimension,
    6069              :                                               false, false);
    6070        22299 :   debug_hooks->early_global_decl (decl);
    6071              : }
    6072              : 
    6073              : 
    6074              : static void
    6075         6683 : generate_coarray_sym_init (gfc_symbol *sym)
    6076              : {
    6077         6683 :   tree tmp, size, decl, token, desc;
    6078         6683 :   bool is_lock_type, is_event_type;
    6079         6683 :   int reg_type;
    6080         6683 :   gfc_se se;
    6081         6683 :   symbol_attribute attr;
    6082              : 
    6083         6683 :   if (sym->attr.dummy || sym->attr.allocatable || !sym->attr.codimension
    6084          395 :       || sym->attr.use_assoc || !sym->attr.referenced
    6085          391 :       || sym->attr.associate_var
    6086          340 :       || sym->attr.select_type_temporary)
    6087         6343 :     return;
    6088              : 
    6089          340 :   decl = sym->backend_decl;
    6090          340 :   TREE_USED(decl) = 1;
    6091          340 :   gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
    6092              : 
    6093          680 :   is_lock_type = sym->ts.type == BT_DERIVED
    6094          152 :                  && sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    6095          374 :                  && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE;
    6096              : 
    6097          680 :   is_event_type = sym->ts.type == BT_DERIVED
    6098          152 :                   && sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    6099          374 :                   && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE;
    6100              : 
    6101              :   /* FIXME: Workaround for PR middle-end/49106, cf. also PR middle-end/49108
    6102              :      to make sure the variable is not optimized away.  */
    6103          340 :   DECL_PRESERVE_P (DECL_CONTEXT (decl)) = 1;
    6104              : 
    6105              :   /* For lock types, we pass the array size as only the library knows the
    6106              :      size of the variable.  */
    6107          340 :   if (is_lock_type || is_event_type)
    6108           34 :     size = gfc_index_one_node;
    6109              :   else
    6110          306 :     size = TYPE_SIZE_UNIT (gfc_get_element_type (TREE_TYPE (decl)));
    6111              : 
    6112              :   /* Ensure that we do not have size=0 for zero-sized arrays.  */
    6113          340 :   size = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
    6114              :                           fold_convert (size_type_node, size),
    6115              :                           build_int_cst (size_type_node, 1));
    6116              : 
    6117          340 :   if (GFC_TYPE_ARRAY_RANK (TREE_TYPE (decl)))
    6118              :     {
    6119          109 :       tmp = GFC_TYPE_ARRAY_SIZE (TREE_TYPE (decl));
    6120          109 :       size = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
    6121              :                               fold_convert (size_type_node, tmp), size);
    6122              :     }
    6123              : 
    6124          340 :   gcc_assert (GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (decl)) != NULL_TREE);
    6125         1020 :   token = gfc_build_addr_expr (ppvoid_type_node,
    6126          340 :                                GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE(decl)));
    6127          340 :   if (is_lock_type)
    6128           29 :     reg_type = sym->attr.artificial ? GFC_CAF_CRITICAL : GFC_CAF_LOCK_STATIC;
    6129          311 :   else if (is_event_type)
    6130              :     reg_type = GFC_CAF_EVENT_STATIC;
    6131              :   else
    6132          306 :     reg_type = GFC_CAF_COARRAY_STATIC;
    6133              : 
    6134              :   /* Compile the symbol attribute.  */
    6135          340 :   if (sym->ts.type == BT_CLASS)
    6136              :     {
    6137            0 :       attr = CLASS_DATA (sym)->attr;
    6138              :       /* The pointer attribute is always set on classes, overwrite it with the
    6139              :          class_pointer attribute, which denotes the pointer for classes.  */
    6140            0 :       attr.pointer = attr.class_pointer;
    6141              :     }
    6142              :   else
    6143          340 :     attr = sym->attr;
    6144          340 :   gfc_init_se (&se, NULL);
    6145          340 :   desc = gfc_conv_scalar_to_descriptor (&se, decl, attr);
    6146          340 :   gfc_add_block_to_block (&caf_init_block, &se.pre);
    6147              : 
    6148          340 :   tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_register, 7, size,
    6149          340 :                              build_int_cst (integer_type_node, reg_type),
    6150              :                              token, gfc_build_addr_expr (pvoid_type_node, desc),
    6151              :                              null_pointer_node, /* stat.  */
    6152              :                              null_pointer_node, /* errgmsg.  */
    6153              :                              build_zero_cst (size_type_node)); /* errmsg_len.  */
    6154          340 :   gfc_add_expr_to_block (&caf_init_block, tmp);
    6155          340 :   gfc_add_modify (&caf_init_block, decl, fold_convert (TREE_TYPE (decl),
    6156              :                                           gfc_conv_descriptor_data_get (desc)));
    6157              : 
    6158              :   /* Handle "static" initializer.  */
    6159          340 :   if (sym->value)
    6160              :     {
    6161          106 :       if (sym->value->expr_type == EXPR_ARRAY)
    6162              :         {
    6163           12 :           gfc_constructor *c, *cnext;
    6164              : 
    6165              :           /* Test if the array has more than one element.  */
    6166           12 :           c = gfc_constructor_first (sym->value->value.constructor);
    6167           12 :           gcc_assert (c);  /* Empty constructor should not happen here.  */
    6168           12 :           cnext = gfc_constructor_next (c);
    6169              : 
    6170           12 :           if (cnext)
    6171              :             {
    6172              :               /* An EXPR_ARRAY with a rank > 1 here has to come from a
    6173              :                  DATA statement.  Set its rank here as not to confuse
    6174              :                  the following steps.   */
    6175           11 :               sym->value->rank = 1;
    6176              :             }
    6177              :           else
    6178              :             {
    6179              :               /* There is only a single value in the constructor, use
    6180              :                  it directly for the assignment.  */
    6181            1 :               gfc_expr *new_expr;
    6182            1 :               new_expr = gfc_copy_expr (c->expr);
    6183            1 :               gfc_free_expr (sym->value);
    6184            1 :               sym->value = new_expr;
    6185              :             }
    6186              :         }
    6187              : 
    6188          106 :       sym->attr.pointer = 1;
    6189          106 :       tmp = gfc_trans_assignment (gfc_lval_expr_from_sym (sym), sym->value,
    6190              :                                   true, false);
    6191          106 :       sym->attr.pointer = 0;
    6192          106 :       gfc_add_expr_to_block (&caf_init_block, tmp);
    6193              :     }
    6194          234 :   else if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.pointer_comp)
    6195              :     {
    6196           32 :       tmp = gfc_nullify_alloc_comp (sym->ts.u.derived, decl, sym->as
    6197              :                                     ? sym->as->rank : 0,
    6198              :                                     GFC_STRUCTURE_CAF_MODE_IN_COARRAY);
    6199           32 :       gfc_add_expr_to_block (&caf_init_block, tmp);
    6200              :     }
    6201              : }
    6202              : 
    6203              : struct caf_accessor
    6204              : {
    6205              :   struct caf_accessor *next;
    6206              :   gfc_expr *hash, *fdecl;
    6207              : };
    6208              : 
    6209              : static struct caf_accessor *caf_accessor_head = NULL;
    6210              : 
    6211              : void
    6212         1488 : gfc_add_caf_accessor (gfc_expr *h, gfc_expr *f)
    6213              : {
    6214         1488 :   struct caf_accessor *n = XCNEW (struct caf_accessor);
    6215         1488 :   n->next = caf_accessor_head;
    6216         1488 :   n->hash = h;
    6217         1488 :   n->fdecl = f;
    6218         1488 :   caf_accessor_head = n;
    6219         1488 : }
    6220              : 
    6221              : void
    6222          459 : create_caf_accessor_register (stmtblock_t *block)
    6223              : {
    6224          459 :   gfc_se se;
    6225          459 :   tree hash, fdecl;
    6226          459 :   gfc_init_se (&se, NULL);
    6227         1887 :   for (struct caf_accessor *curr = caf_accessor_head; curr;)
    6228              :     {
    6229         1428 :       gfc_conv_expr (&se, curr->hash);
    6230         1428 :       hash = se.expr;
    6231         1428 :       gfc_conv_expr (&se, curr->fdecl);
    6232         1428 :       fdecl = se.expr;
    6233         1428 :       TREE_USED (fdecl) = 1;
    6234         1428 :       TREE_STATIC (fdecl) = 1;
    6235         1428 :       gcc_assert (FUNCTION_POINTER_TYPE_P (TREE_TYPE (fdecl)));
    6236         1428 :       gfc_add_expr_to_block (
    6237              :         block, build_call_expr (gfor_fndecl_caf_register_accessor, 2, hash,
    6238              :                                 /*gfc_build_addr_expr (NULL_TREE,*/ fdecl));
    6239         1428 :       curr = curr->next;
    6240         1428 :       free (caf_accessor_head);
    6241         1428 :       caf_accessor_head = curr;
    6242              :     }
    6243          459 :   gfc_add_expr_to_block (
    6244              :     block, build_call_expr (gfor_fndecl_caf_register_accessors_finish, 0));
    6245          459 : }
    6246              : 
    6247              : /* Generate constructor function to initialize static, nonallocatable
    6248              :    coarrays.  */
    6249              : 
    6250              : static void
    6251          459 : generate_coarray_init (gfc_namespace *ns)
    6252              : {
    6253          459 :   tree fndecl, tmp, decl, save_fn_decl;
    6254              : 
    6255          459 :   save_fn_decl = current_function_decl;
    6256          459 :   push_function_context ();
    6257              : 
    6258          459 :   tmp = build_function_type_list (void_type_node, NULL_TREE);
    6259          459 :   fndecl = build_decl (input_location, FUNCTION_DECL,
    6260              :                        create_tmp_var_name ("_caf_init"), tmp);
    6261              : 
    6262          459 :   DECL_STATIC_CONSTRUCTOR (fndecl) = 1;
    6263          459 :   SET_DECL_INIT_PRIORITY (fndecl, DEFAULT_INIT_PRIORITY);
    6264              : 
    6265          459 :   decl = build_decl (input_location, RESULT_DECL, NULL_TREE, void_type_node);
    6266          459 :   DECL_ARTIFICIAL (decl) = 1;
    6267          459 :   DECL_IGNORED_P (decl) = 1;
    6268          459 :   DECL_CONTEXT (decl) = fndecl;
    6269          459 :   DECL_RESULT (fndecl) = decl;
    6270              : 
    6271          459 :   pushdecl (fndecl);
    6272          459 :   current_function_decl = fndecl;
    6273          459 :   announce_function (fndecl);
    6274              : 
    6275          459 :   rest_of_decl_compilation (fndecl, 0, 0);
    6276          459 :   make_decl_rtl (fndecl);
    6277          459 :   allocate_struct_function (fndecl, false);
    6278              : 
    6279          459 :   pushlevel ();
    6280          459 :   gfc_init_block (&caf_init_block);
    6281              : 
    6282          459 :   create_caf_accessor_register (&caf_init_block);
    6283              : 
    6284          459 :   gfc_traverse_ns (ns, generate_coarray_sym_init);
    6285              : 
    6286          459 :   DECL_SAVED_TREE (fndecl) = gfc_finish_block (&caf_init_block);
    6287          459 :   decl = getdecls ();
    6288              : 
    6289          459 :   poplevel (1, 1);
    6290          459 :   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
    6291              : 
    6292          918 :   DECL_SAVED_TREE (fndecl)
    6293          918 :     = fold_build3_loc (DECL_SOURCE_LOCATION (fndecl), BIND_EXPR, void_type_node,
    6294          918 :                        decl, DECL_SAVED_TREE (fndecl), DECL_INITIAL (fndecl));
    6295          459 :   dump_function (TDI_original, fndecl);
    6296              : 
    6297          459 :   cfun->function_end_locus = input_location;
    6298          459 :   set_cfun (NULL);
    6299              : 
    6300          459 :   if (decl_function_context (fndecl))
    6301          441 :     (void) cgraph_node::create (fndecl);
    6302              :   else
    6303           18 :     cgraph_node::finalize_function (fndecl, true);
    6304              : 
    6305          459 :   pop_function_context ();
    6306          459 :   current_function_decl = save_fn_decl;
    6307          459 : }
    6308              : 
    6309              : 
    6310              : static void
    6311       142892 : create_module_nml_decl (gfc_symbol *sym)
    6312              : {
    6313       142892 :   if (sym->attr.flavor == FL_NAMELIST)
    6314              :     {
    6315           45 :       tree decl = generate_namelist_decl (sym);
    6316           45 :       pushdecl (decl);
    6317           45 :       gcc_assert (sym->ns->proc_name->attr.flavor == FL_MODULE);
    6318           45 :       DECL_CONTEXT (decl) = sym->ns->proc_name->backend_decl;
    6319           45 :       rest_of_decl_compilation (decl, 1, 0);
    6320           45 :       gfc_module_add_decl (cur_module, decl);
    6321              :     }
    6322       142892 : }
    6323              : 
    6324              : static void
    6325       265940 : gfc_handle_omp_declare_variant (gfc_symbol * sym)
    6326              : {
    6327       265940 :   if (sym->attr.external
    6328        86114 :       && sym->formal_ns
    6329        82284 :       && sym->formal_ns->omp_declare_variant)
    6330              :     {
    6331           26 :       gfc_namespace *ns = gfc_current_ns;
    6332           26 :       gfc_current_ns = sym->ns;
    6333           26 :       gfc_get_symbol_decl (sym);
    6334           26 :       gfc_current_ns = ns;
    6335              :     }
    6336       265940 : }
    6337              : 
    6338              : /* Generate all the required code for module variables.  */
    6339              : 
    6340              : void
    6341         9063 : gfc_generate_module_vars (gfc_namespace * ns)
    6342              : {
    6343         9063 :   module_namespace = ns;
    6344         9063 :   cur_module = gfc_find_module (ns->proc_name->name);
    6345              : 
    6346              :   /* Check if the frontend left the namespace in a reasonable state.  */
    6347         9063 :   gcc_assert (ns->proc_name && !ns->proc_name->tlink);
    6348              : 
    6349              :   /* Generate COMMON blocks.  */
    6350         9063 :   gfc_trans_common (ns);
    6351              : 
    6352         9063 :   has_coarray_vars_or_accessors = caf_accessor_head != NULL;
    6353              : 
    6354              :   /* Create decls for all the module variables.  */
    6355         9063 :   gfc_traverse_ns (ns, gfc_create_module_variable);
    6356         9063 :   gfc_traverse_ns (ns, create_module_nml_decl);
    6357              : 
    6358         9063 :   if (flag_coarray == GFC_FCOARRAY_LIB && has_coarray_vars_or_accessors)
    6359           18 :     generate_coarray_init (ns);
    6360              : 
    6361              :   /* For OpenMP, ensure that declare variant in INTERFACE is is processed
    6362              :      especially as some late diagnostic is only done on tree level.  */
    6363         9063 :   if (flag_openmp)
    6364         1073 :     gfc_traverse_ns (ns, gfc_handle_omp_declare_variant);
    6365              : 
    6366         9063 :   cur_module = NULL;
    6367              : 
    6368         9063 :   gfc_trans_use_stmts (ns);
    6369         9063 :   gfc_traverse_ns (ns, gfc_emit_parameter_debug_info);
    6370         9063 : }
    6371              : 
    6372              : 
    6373              : static void
    6374        84658 : gfc_generate_contained_functions (gfc_namespace * parent)
    6375              : {
    6376        84658 :   gfc_namespace *ns;
    6377              : 
    6378              :   /* We create all the prototypes before generating any code.  */
    6379       107623 :   for (ns = parent->contained; ns; ns = ns->sibling)
    6380              :     {
    6381              :       /* Skip namespaces from used modules.  */
    6382        22965 :       if (ns->parent != parent)
    6383            0 :         continue;
    6384              : 
    6385        22965 :       gfc_create_function_decl (ns, false);
    6386              :     }
    6387              : 
    6388       107623 :   for (ns = parent->contained; ns; ns = ns->sibling)
    6389              :     {
    6390              :       /* Skip namespaces from used modules.  */
    6391        22965 :       if (ns->parent != parent)
    6392            0 :         continue;
    6393              : 
    6394        22965 :       gfc_generate_function_code (ns);
    6395              :     }
    6396        84658 : }
    6397              : 
    6398              : 
    6399              : /* Drill down through expressions for the array specification bounds and
    6400              :    character length calling generate_local_decl for all those variables
    6401              :    that have not already been declared.  */
    6402              : 
    6403              : static void
    6404              : generate_local_decl (gfc_symbol *);
    6405              : 
    6406              : /* Traverse expr, marking all EXPR_VARIABLE symbols referenced.  */
    6407              : 
    6408              : static bool
    6409       113692 : expr_decls (gfc_expr *e, gfc_symbol *sym,
    6410              :             int *f ATTRIBUTE_UNUSED)
    6411              : {
    6412       113692 :   if (e->expr_type != EXPR_VARIABLE
    6413         7969 :             || sym == e->symtree->n.sym
    6414         7956 :             || e->symtree->n.sym->mark
    6415          744 :             || e->symtree->n.sym->ns != sym->ns)
    6416              :         return false;
    6417              : 
    6418          744 :   generate_local_decl (e->symtree->n.sym);
    6419          744 :   return false;
    6420              : }
    6421              : 
    6422              : static void
    6423       151348 : generate_expr_decls (gfc_symbol *sym, gfc_expr *e)
    6424              : {
    6425            0 :   gfc_traverse_expr (e, sym, expr_decls, 0);
    6426         1058 : }
    6427              : 
    6428              : 
    6429              : /* Check for dependencies in the character length and array spec.  */
    6430              : 
    6431              : static void
    6432       194487 : generate_dependency_declarations (gfc_symbol *sym)
    6433              : {
    6434       194487 :   int i;
    6435              : 
    6436       194487 :   if (sym->ts.type == BT_CHARACTER
    6437        17316 :       && sym->ts.u.cl
    6438        17316 :       && sym->ts.u.cl->length
    6439        14219 :       && sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
    6440         1058 :     generate_expr_decls (sym, sym->ts.u.cl->length);
    6441              : 
    6442       194487 :   if (sym->as && sym->as->rank)
    6443              :     {
    6444       127193 :       for (i = 0; i < sym->as->rank; i++)
    6445              :         {
    6446        75145 :           generate_expr_decls (sym, sym->as->lower[i]);
    6447        75145 :           generate_expr_decls (sym, sym->as->upper[i]);
    6448              :         }
    6449              :     }
    6450       194487 : }
    6451              : 
    6452              : 
    6453              : /* Generate decls for all local variables.  We do this to ensure correct
    6454              :    handling of expressions which only appear in the specification of
    6455              :    other functions.  */
    6456              : 
    6457              : static void
    6458      1085733 : generate_local_decl (gfc_symbol * sym)
    6459              : {
    6460      1085733 :   if (sym->attr.flavor == FL_VARIABLE)
    6461              :     {
    6462       288905 :       if (sym->attr.codimension && !sym->attr.dummy && !sym->attr.allocatable
    6463          582 :           && sym->attr.referenced && !sym->attr.use_assoc)
    6464          532 :         has_coarray_vars_or_accessors = true;
    6465              : 
    6466       288905 :       if (!sym->attr.dummy && !sym->ns->proc_name->attr.entry_master)
    6467       194487 :         generate_dependency_declarations (sym);
    6468              : 
    6469       288905 :       if (sym->attr.ext_attr & (1 << EXT_ATTR_WEAK))
    6470              :         {
    6471            2 :           if (sym->attr.dummy)
    6472            1 :             gfc_error ("Symbol %qs at %L has the WEAK attribute but is a "
    6473              :                        "dummy argument", sym->name, &sym->declared_at);
    6474              :           else
    6475            1 :             gfc_error ("Symbol %qs at %L has the WEAK attribute but is a "
    6476              :                        "local variable", sym->name, &sym->declared_at);
    6477              :         }
    6478              : 
    6479       288905 :       if (sym->attr.referenced)
    6480       254586 :         gfc_get_symbol_decl (sym);
    6481              : 
    6482              :       /* Warnings for unused dummy arguments.  */
    6483        34319 :       else if (sym->attr.dummy && !sym->attr.in_namelist)
    6484              :         {
    6485              :           /* INTENT(out) dummy arguments are likely meant to be set.  */
    6486         6571 :           if (warn_unused_dummy_argument && sym->attr.intent == INTENT_OUT)
    6487              :             {
    6488            9 :               if (sym->ts.type != BT_DERIVED)
    6489            6 :                 gfc_warning (OPT_Wunused_dummy_argument,
    6490              :                              "Dummy argument %qs at %L was declared "
    6491              :                              "INTENT(OUT) but was not set",  sym->name,
    6492              :                              &sym->declared_at);
    6493            3 :               else if (!gfc_has_default_initializer (sym->ts.u.derived)
    6494            3 :                        && !sym->ts.u.derived->attr.zero_comp)
    6495            1 :                 gfc_warning (OPT_Wunused_dummy_argument,
    6496              :                              "Derived-type dummy argument %qs at %L was "
    6497              :                              "declared INTENT(OUT) but was not set and "
    6498              :                              "does not have a default initializer",
    6499              :                              sym->name, &sym->declared_at);
    6500            9 :               if (sym->backend_decl != NULL_TREE)
    6501            9 :                 suppress_warning (sym->backend_decl);
    6502              :             }
    6503         6562 :           else if (warn_unused_dummy_argument)
    6504              :             {
    6505           12 :               if (!sym->attr.artificial)
    6506            4 :                 gfc_warning (OPT_Wunused_dummy_argument,
    6507              :                              "Unused dummy argument %qs at %L", sym->name,
    6508              :                              &sym->declared_at);
    6509              : 
    6510           12 :               if (sym->backend_decl != NULL_TREE)
    6511            4 :                 suppress_warning (sym->backend_decl);
    6512              :             }
    6513              :         }
    6514              : 
    6515              :       /* Warn for unused variables, but not if they're inside a common
    6516              :          block or a namelist.  */
    6517        27748 :       else if (warn_unused_variable
    6518           45 :                && !(sym->attr.in_common || sym->mark || sym->attr.in_namelist))
    6519              :         {
    6520           43 :           if (sym->attr.use_only)
    6521              :             {
    6522            1 :               gfc_warning (OPT_Wunused_variable,
    6523              :                            "Unused module variable %qs which has been "
    6524              :                            "explicitly imported at %L", sym->name,
    6525              :                            &sym->declared_at);
    6526            1 :               if (sym->backend_decl != NULL_TREE)
    6527            0 :                 suppress_warning (sym->backend_decl);
    6528              :             }
    6529           42 :           else if (!sym->attr.use_assoc)
    6530              :             {
    6531              :               /* Corner case: the symbol may be an entry point.  At this point,
    6532              :                  it may appear to be an unused variable.  Suppress warning.  */
    6533            8 :               bool enter = false;
    6534            8 :               gfc_entry_list *el;
    6535              : 
    6536           14 :               for (el = sym->ns->entries; el; el=el->next)
    6537            6 :                 if (strcmp(sym->name, el->sym->name) == 0)
    6538            2 :                   enter = true;
    6539              : 
    6540            8 :               if (!enter)
    6541            6 :                 gfc_warning (OPT_Wunused_variable,
    6542              :                              "Unused variable %qs declared at %L",
    6543              :                              sym->name, &sym->declared_at);
    6544            8 :               if (sym->backend_decl != NULL_TREE)
    6545            0 :                 suppress_warning (sym->backend_decl);
    6546              :             }
    6547              :         }
    6548              : 
    6549              :       /* For variable length CHARACTER parameters, the PARM_DECL already
    6550              :          references the length variable, so force gfc_get_symbol_decl
    6551              :          even when not referenced.  If optimize > 0, it will be optimized
    6552              :          away anyway.  But do this only after emitting -Wunused-parameter
    6553              :          warning if requested.  */
    6554       288905 :       if (sym->attr.dummy && !sym->attr.referenced
    6555         6574 :             && sym->ts.type == BT_CHARACTER
    6556          815 :             && sym->ts.u.cl->backend_decl != NULL
    6557          810 :             && VAR_P (sym->ts.u.cl->backend_decl))
    6558              :         {
    6559            6 :           sym->attr.referenced = 1;
    6560            6 :           gfc_get_symbol_decl (sym);
    6561              :         }
    6562              : 
    6563              :       /* INTENT(out) dummy arguments and result variables with allocatable
    6564              :          components are reset by default and need to be set referenced to
    6565              :          generate the code for nullification and automatic lengths.  */
    6566       288905 :       if (!sym->attr.referenced
    6567        34313 :             && sym->ts.type == BT_DERIVED
    6568        20431 :             && sym->ts.u.derived->attr.alloc_comp
    6569         1612 :             && !sym->attr.pointer
    6570         1611 :             && ((sym->attr.dummy && sym->attr.intent == INTENT_OUT)
    6571         1575 :                   ||
    6572         1575 :                 (sym->attr.result && sym != sym->result)))
    6573              :         {
    6574           36 :           sym->attr.referenced = 1;
    6575           36 :           gfc_get_symbol_decl (sym);
    6576              :         }
    6577              : 
    6578              :       /* Check for dependencies in the array specification and string
    6579              :         length, adding the necessary declarations to the function.  We
    6580              :         mark the symbol now, as well as in traverse_ns, to prevent
    6581              :         getting stuck in a circular dependency.  */
    6582       288905 :       sym->mark = 1;
    6583              :     }
    6584       796828 :   else if (sym->attr.flavor == FL_PARAMETER)
    6585              :     {
    6586       206763 :       if (warn_unused_parameter
    6587          488 :            && !sym->attr.referenced)
    6588              :         {
    6589          431 :            if (!sym->attr.use_assoc)
    6590            4 :              gfc_warning (OPT_Wunused_parameter,
    6591              :                           "Unused parameter %qs declared at %L", sym->name,
    6592              :                           &sym->declared_at);
    6593          427 :            else if (sym->attr.use_only)
    6594            1 :              gfc_warning (OPT_Wunused_parameter,
    6595              :                           "Unused parameter %qs which has been explicitly "
    6596              :                           "imported at %L", sym->name, &sym->declared_at);
    6597              :         }
    6598              : 
    6599       206763 :       if (sym->ns && sym->ns->construct_entities)
    6600              :         {
    6601              :           /* Construction of the intrinsic modules within a BLOCK
    6602              :              construct, where ONLY and RENAMED entities are included,
    6603              :              seems to be bogus.  This is a workaround that can be removed
    6604              :              if someone ever takes on the task to creating full-fledge
    6605              :              modules.  See PR 69455.  */
    6606           78 :           if (sym->attr.referenced
    6607           78 :               && sym->from_intmod != INTMOD_ISO_C_BINDING
    6608           54 :               && sym->from_intmod != INTMOD_ISO_FORTRAN_ENV)
    6609           26 :             gfc_get_symbol_decl (sym);
    6610           78 :           sym->mark = 1;
    6611              :         }
    6612              :     }
    6613       590065 :   else if (sym->attr.flavor == FL_PROCEDURE)
    6614              :     {
    6615              :       /* TODO: move to the appropriate place in resolve.cc.  */
    6616       490406 :       if (warn_return_type > 0
    6617         4704 :           && sym->attr.function
    6618         3430 :           && sym->result
    6619         3222 :           && sym != sym->result
    6620          444 :           && !sym->result->attr.referenced
    6621           36 :           && !sym->attr.use_assoc
    6622           23 :           && sym->attr.if_source != IFSRC_IFBODY)
    6623              :         {
    6624           23 :           gfc_warning (OPT_Wreturn_type,
    6625              :                        "Return value %qs of function %qs declared at "
    6626              :                        "%L not set", sym->result->name, sym->name,
    6627              :                         &sym->result->declared_at);
    6628              : 
    6629              :           /* Prevents "Unused variable" warning for RESULT variables.  */
    6630           23 :           sym->result->mark = 1;
    6631              :         }
    6632              :     }
    6633              : 
    6634      1085733 :   if (sym->attr.dummy == 1)
    6635              :     {
    6636              :       /* The tree type for scalar character dummy arguments of BIND(C)
    6637              :          procedures, if they are passed by value, should be unsigned char.
    6638              :          The value attribute implies the dummy is a scalar.  */
    6639       101185 :       if (sym->attr.value == 1 && sym->backend_decl != NULL
    6640         8417 :           && sym->ts.type == BT_CHARACTER && sym->ts.is_c_interop
    6641          254 :           && sym->ns->proc_name != NULL && sym->ns->proc_name->attr.is_bind_c)
    6642              :         {
    6643              :           /* We used to modify the tree here. Now it is done earlier in
    6644              :              the front-end, so we only check it here to avoid regressions.  */
    6645          242 :           gcc_assert (TREE_CODE (TREE_TYPE (sym->backend_decl)) == INTEGER_TYPE);
    6646          242 :           gcc_assert (TYPE_UNSIGNED (TREE_TYPE (sym->backend_decl)) == 1);
    6647          242 :           gcc_assert (TYPE_PRECISION (TREE_TYPE (sym->backend_decl)) == CHAR_TYPE_SIZE);
    6648          242 :           gcc_assert (DECL_BY_REFERENCE (sym->backend_decl) == 0);
    6649              :         }
    6650              : 
    6651              :       /* Unused procedure passed as dummy argument.  */
    6652       101185 :       if (sym->attr.flavor == FL_PROCEDURE)
    6653              :         {
    6654          925 :           if (!sym->attr.referenced && !sym->attr.artificial)
    6655              :             {
    6656           58 :               if (warn_unused_dummy_argument)
    6657            2 :                 gfc_warning (OPT_Wunused_dummy_argument,
    6658              :                              "Unused dummy argument %qs at %L", sym->name,
    6659              :                              &sym->declared_at);
    6660              :             }
    6661              : 
    6662              :           /* Silence bogus "unused parameter" warnings from the
    6663              :              middle end.  */
    6664          925 :           if (sym->backend_decl != NULL_TREE)
    6665          924 :                 suppress_warning (sym->backend_decl);
    6666              :         }
    6667              :     }
    6668              : 
    6669              :   /* Make sure we convert the types of the derived types from iso_c_binding
    6670              :      into (void *).  */
    6671      1085733 :   if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c
    6672        79868 :       && sym->ts.type == BT_DERIVED)
    6673         3381 :     sym->backend_decl = gfc_typenode_for_spec (&(sym->ts));
    6674      1085733 : }
    6675              : 
    6676              : 
    6677              : static void
    6678      1085740 : generate_local_nml_decl (gfc_symbol * sym)
    6679              : {
    6680      1085740 :   if (sym->attr.flavor == FL_NAMELIST && !sym->attr.use_assoc)
    6681              :     {
    6682          698 :       tree decl = generate_namelist_decl (sym);
    6683          698 :       pushdecl (decl);
    6684              :     }
    6685      1085740 : }
    6686              : 
    6687              : 
    6688              : static void
    6689        98610 : generate_local_vars (gfc_namespace * ns)
    6690              : {
    6691        98610 :   gfc_traverse_ns (ns, generate_local_decl);
    6692        98610 :   gfc_traverse_ns (ns, generate_local_nml_decl);
    6693        98610 : }
    6694              : 
    6695              : 
    6696              : /* Generate a switch statement to jump to the correct entry point.  Also
    6697              :    creates the label decls for the entry points.  */
    6698              : 
    6699              : static tree
    6700          667 : gfc_trans_entry_master_switch (gfc_entry_list * el)
    6701              : {
    6702          667 :   stmtblock_t block;
    6703          667 :   tree label;
    6704          667 :   tree tmp;
    6705          667 :   tree val;
    6706              : 
    6707          667 :   gfc_init_block (&block);
    6708         2746 :   for (; el; el = el->next)
    6709              :     {
    6710              :       /* Add the case label.  */
    6711         1412 :       label = gfc_build_label_decl (NULL_TREE);
    6712         1412 :       val = build_int_cst (gfc_array_index_type, el->id);
    6713         1412 :       tmp = build_case_label (val, NULL_TREE, label);
    6714         1412 :       gfc_add_expr_to_block (&block, tmp);
    6715              : 
    6716              :       /* And jump to the actual entry point.  */
    6717         1412 :       label = gfc_build_label_decl (NULL_TREE);
    6718         1412 :       tmp = build1_v (GOTO_EXPR, label);
    6719         1412 :       gfc_add_expr_to_block (&block, tmp);
    6720              : 
    6721              :       /* Save the label decl.  */
    6722         1412 :       el->label = label;
    6723              :     }
    6724          667 :   tmp = gfc_finish_block (&block);
    6725              :   /* The first argument selects the entry point.  */
    6726          667 :   val = DECL_ARGUMENTS (current_function_decl);
    6727          667 :   tmp = fold_build2_loc (input_location, SWITCH_EXPR, NULL_TREE, val, tmp);
    6728          667 :   return tmp;
    6729              : }
    6730              : 
    6731              : 
    6732              : /* Add code to string lengths of actual arguments passed to a function against
    6733              :    the expected lengths of the dummy arguments.  */
    6734              : 
    6735              : static void
    6736         2869 : add_argument_checking (stmtblock_t *block, gfc_symbol *sym)
    6737              : {
    6738         2869 :   gfc_formal_arglist *formal;
    6739              : 
    6740         5687 :   for (formal = gfc_sym_get_dummy_args (sym); formal; formal = formal->next)
    6741         2818 :     if (formal->sym && formal->sym->ts.type == BT_CHARACTER
    6742          604 :         && !formal->sym->ts.deferred)
    6743              :       {
    6744          554 :         enum tree_code comparison;
    6745          554 :         tree cond;
    6746          554 :         tree argname;
    6747          554 :         gfc_symbol *fsym;
    6748          554 :         gfc_charlen *cl;
    6749          554 :         const char *message;
    6750              : 
    6751          554 :         fsym = formal->sym;
    6752          554 :         cl = fsym->ts.u.cl;
    6753              : 
    6754          554 :         gcc_assert (cl);
    6755          554 :         gcc_assert (cl->passed_length != NULL_TREE);
    6756          554 :         gcc_assert (cl->backend_decl != NULL_TREE);
    6757              : 
    6758              :         /* For POINTER, ALLOCATABLE and assumed-shape dummy arguments, the
    6759              :            string lengths must match exactly.  Otherwise, it is only required
    6760              :            that the actual string length is *at least* the expected one.
    6761              :            Sequence association allows for a mismatch of the string length
    6762              :            if the actual argument is (part of) an array, but only if the
    6763              :            dummy argument is an array. (See "Sequence association" in
    6764              :            Section 12.4.1.4 for F95 and 12.4.1.5 for F2003.)  */
    6765          554 :         if (fsym->attr.pointer || fsym->attr.allocatable
    6766          446 :             || (fsym->as && (fsym->as->type == AS_ASSUMED_SHAPE
    6767           33 :                              || fsym->as->type == AS_ASSUMED_RANK)))
    6768              :           {
    6769          196 :             comparison = NE_EXPR;
    6770          196 :             message = _("Actual string length does not match the declared one"
    6771              :                         " for dummy argument '%s' (%ld/%ld)");
    6772              :           }
    6773          358 :         else if ((fsym->as && fsym->as->rank != 0) || fsym->attr.artificial)
    6774           33 :           continue;
    6775              :         else
    6776              :           {
    6777          325 :             comparison = LT_EXPR;
    6778          325 :             message = _("Actual string length is shorter than the declared one"
    6779              :                         " for dummy argument '%s' (%ld/%ld)");
    6780              :           }
    6781              : 
    6782              :         /* Build the condition.  For optional arguments, an actual length
    6783              :            of 0 is also acceptable if the associated string is NULL, which
    6784              :            means the argument was not passed.  */
    6785          521 :         cond = fold_build2_loc (input_location, comparison, logical_type_node,
    6786              :                                 cl->passed_length, cl->backend_decl);
    6787          521 :         if (fsym->attr.optional)
    6788              :           {
    6789           45 :             tree not_absent;
    6790           45 :             tree not_0length;
    6791           45 :             tree absent_failed;
    6792              : 
    6793           45 :             not_0length = fold_build2_loc (input_location, NE_EXPR,
    6794              :                                            logical_type_node,
    6795              :                                            cl->passed_length,
    6796              :                                            build_zero_cst
    6797           45 :                                            (TREE_TYPE (cl->passed_length)));
    6798              :             /* The symbol needs to be referenced for gfc_get_symbol_decl.  */
    6799           45 :             fsym->attr.referenced = 1;
    6800           45 :             not_absent = gfc_conv_expr_present (fsym);
    6801              : 
    6802           45 :             absent_failed = fold_build2_loc (input_location, TRUTH_OR_EXPR,
    6803              :                                              logical_type_node, not_0length,
    6804              :                                              not_absent);
    6805              : 
    6806           45 :             cond = fold_build2_loc (input_location, TRUTH_AND_EXPR,
    6807              :                                     logical_type_node, cond, absent_failed);
    6808              :           }
    6809              : 
    6810              :         /* Build the runtime check.  */
    6811          521 :         argname = gfc_build_cstring_const (fsym->name);
    6812          521 :         argname = gfc_build_addr_expr (pchar_type_node, argname);
    6813          521 :         gfc_trans_runtime_check (true, false, cond, block, &fsym->declared_at,
    6814              :                                  message, argname,
    6815              :                                  fold_convert (long_integer_type_node,
    6816              :                                                cl->passed_length),
    6817              :                                  fold_convert (long_integer_type_node,
    6818              :                                                cl->backend_decl));
    6819              :       }
    6820         2869 : }
    6821              : 
    6822              : 
    6823              : static void
    6824        26136 : create_main_function (tree fndecl)
    6825              : {
    6826        26136 :   tree old_context;
    6827        26136 :   tree ftn_main;
    6828        26136 :   tree tmp, decl, result_decl, argc, argv, typelist, arglist;
    6829        26136 :   stmtblock_t body;
    6830              : 
    6831        26136 :   old_context = current_function_decl;
    6832              : 
    6833        26136 :   if (old_context)
    6834              :     {
    6835            0 :       push_function_context ();
    6836            0 :       saved_parent_function_decls = saved_function_decls;
    6837            0 :       saved_function_decls = NULL_TREE;
    6838              :     }
    6839              : 
    6840              :   /* main() function must be declared with global scope.  */
    6841        26136 :   gcc_assert (current_function_decl == NULL_TREE);
    6842              : 
    6843              :   /* Declare the function.  */
    6844        26136 :   tmp =  build_function_type_list (integer_type_node, integer_type_node,
    6845              :                                    build_pointer_type (pchar_type_node),
    6846              :                                    NULL_TREE);
    6847        26136 :   main_identifier_node = get_identifier ("main");
    6848        26136 :   ftn_main = build_decl (input_location, FUNCTION_DECL,
    6849              :                          main_identifier_node, tmp);
    6850        26136 :   DECL_EXTERNAL (ftn_main) = 0;
    6851        26136 :   TREE_PUBLIC (ftn_main) = 1;
    6852        26136 :   TREE_STATIC (ftn_main) = 1;
    6853        26136 :   DECL_ATTRIBUTES (ftn_main)
    6854        26136 :       = tree_cons (get_identifier("externally_visible"), NULL_TREE, NULL_TREE);
    6855              : 
    6856              :   /* Setup the result declaration (for "return 0").  */
    6857        26136 :   result_decl = build_decl (input_location,
    6858              :                             RESULT_DECL, NULL_TREE, integer_type_node);
    6859        26136 :   DECL_ARTIFICIAL (result_decl) = 1;
    6860        26136 :   DECL_IGNORED_P (result_decl) = 1;
    6861        26136 :   DECL_CONTEXT (result_decl) = ftn_main;
    6862        26136 :   DECL_RESULT (ftn_main) = result_decl;
    6863              : 
    6864        26136 :   pushdecl (ftn_main);
    6865              : 
    6866              :   /* Get the arguments.  */
    6867              : 
    6868        26136 :   arglist = NULL_TREE;
    6869        26136 :   typelist = TYPE_ARG_TYPES (TREE_TYPE (ftn_main));
    6870              : 
    6871        26136 :   tmp = TREE_VALUE (typelist);
    6872        26136 :   argc = build_decl (input_location, PARM_DECL, get_identifier ("argc"), tmp);
    6873        26136 :   DECL_CONTEXT (argc) = ftn_main;
    6874        26136 :   DECL_ARG_TYPE (argc) = TREE_VALUE (typelist);
    6875        26136 :   TREE_READONLY (argc) = 1;
    6876        26136 :   gfc_finish_decl (argc);
    6877        26136 :   arglist = chainon (arglist, argc);
    6878              : 
    6879        26136 :   typelist = TREE_CHAIN (typelist);
    6880        26136 :   tmp = TREE_VALUE (typelist);
    6881        26136 :   argv = build_decl (input_location, PARM_DECL, get_identifier ("argv"), tmp);
    6882        26136 :   DECL_CONTEXT (argv) = ftn_main;
    6883        26136 :   DECL_ARG_TYPE (argv) = TREE_VALUE (typelist);
    6884        26136 :   TREE_READONLY (argv) = 1;
    6885        26136 :   DECL_BY_REFERENCE (argv) = 1;
    6886        26136 :   gfc_finish_decl (argv);
    6887        26136 :   arglist = chainon (arglist, argv);
    6888              : 
    6889        26136 :   DECL_ARGUMENTS (ftn_main) = arglist;
    6890        26136 :   current_function_decl = ftn_main;
    6891        26136 :   announce_function (ftn_main);
    6892              : 
    6893        26136 :   rest_of_decl_compilation (ftn_main, 1, 0);
    6894        26136 :   make_decl_rtl (ftn_main);
    6895        26136 :   allocate_struct_function (ftn_main, false);
    6896        26136 :   pushlevel ();
    6897              : 
    6898        26136 :   gfc_init_block (&body);
    6899              : 
    6900              :   /* Call some libgfortran initialization routines, call then MAIN__().  */
    6901              : 
    6902              :   /* Call _gfortran_caf_init (*argc, ***argv).  */
    6903        26136 :   if (flag_coarray == GFC_FCOARRAY_LIB)
    6904              :     {
    6905          396 :       tree pint_type, pppchar_type;
    6906          396 :       pint_type = build_pointer_type (integer_type_node);
    6907          396 :       pppchar_type
    6908          396 :         = build_pointer_type (build_pointer_type (pchar_type_node));
    6909              : 
    6910          396 :       tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_init, 2,
    6911              :                 gfc_build_addr_expr (pint_type, argc),
    6912              :                 gfc_build_addr_expr (pppchar_type, argv));
    6913          396 :       gfc_add_expr_to_block (&body, tmp);
    6914              :     }
    6915              : 
    6916              :   /* Call _gfortran_set_args (argc, argv).  */
    6917        26136 :   TREE_USED (argc) = 1;
    6918        26136 :   TREE_USED (argv) = 1;
    6919        26136 :   tmp = build_call_expr_loc (input_location,
    6920              :                          gfor_fndecl_set_args, 2, argc, argv);
    6921        26136 :   gfc_add_expr_to_block (&body, tmp);
    6922              : 
    6923              :   /* Add a call to set_options to set up the runtime library Fortran
    6924              :      language standard parameters.  */
    6925        26136 :   {
    6926        26136 :     tree array_type, array, var;
    6927        26136 :     vec<constructor_elt, va_gc> *v = NULL;
    6928        26136 :     static const int noptions = 7;
    6929              : 
    6930              :     /* Passing a new option to the library requires three modifications:
    6931              :           + add it to the tree_cons list below
    6932              :           + change the noptions variable above
    6933              :           + modify the library (runtime/compile_options.c)!  */
    6934              : 
    6935        26136 :     CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
    6936              :                             build_int_cst (integer_type_node,
    6937              :                                            gfc_option.warn_std));
    6938        26136 :     CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
    6939              :                             build_int_cst (integer_type_node,
    6940              :                                            gfc_option.allow_std));
    6941        26136 :     CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
    6942              :                             build_int_cst (integer_type_node, pedantic));
    6943        26136 :     CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
    6944              :                             build_int_cst (integer_type_node, flag_backtrace));
    6945        26136 :     CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
    6946              :                             build_int_cst (integer_type_node, flag_sign_zero));
    6947        26136 :     CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
    6948              :                             build_int_cst (integer_type_node,
    6949              :                                            (gfc_option.rtcheck
    6950              :                                             & GFC_RTCHECK_BOUNDS)));
    6951        26136 :     CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
    6952              :                             build_int_cst (integer_type_node,
    6953              :                                            gfc_option.fpe_summary));
    6954              : 
    6955        26136 :     array_type = build_array_type_nelts (integer_type_node, noptions);
    6956        26136 :     array = build_constructor (array_type, v);
    6957        26136 :     TREE_CONSTANT (array) = 1;
    6958        26136 :     TREE_STATIC (array) = 1;
    6959              : 
    6960              :     /* Create a static variable to hold the jump table.  */
    6961        26136 :     var = build_decl (input_location, VAR_DECL,
    6962              :                       create_tmp_var_name ("options"), array_type);
    6963        26136 :     DECL_ARTIFICIAL (var) = 1;
    6964        26136 :     DECL_IGNORED_P (var) = 1;
    6965        26136 :     TREE_CONSTANT (var) = 1;
    6966        26136 :     TREE_STATIC (var) = 1;
    6967        26136 :     TREE_READONLY (var) = 1;
    6968        26136 :     DECL_INITIAL (var) = array;
    6969        26136 :     pushdecl (var);
    6970        26136 :     var = gfc_build_addr_expr (build_pointer_type (integer_type_node), var);
    6971              : 
    6972        26136 :     tmp = build_call_expr_loc (input_location,
    6973              :                            gfor_fndecl_set_options, 2,
    6974              :                            build_int_cst (integer_type_node, noptions), var);
    6975        26136 :     gfc_add_expr_to_block (&body, tmp);
    6976              :   }
    6977              : 
    6978              :   /* If -ffpe-trap option was provided, add a call to set_fpe so that
    6979              :      the library will raise a FPE when needed.  */
    6980        26136 :   if (gfc_option.fpe != 0)
    6981              :     {
    6982            6 :       tmp = build_call_expr_loc (input_location,
    6983              :                              gfor_fndecl_set_fpe, 1,
    6984              :                              build_int_cst (integer_type_node,
    6985            6 :                                             gfc_option.fpe));
    6986            6 :       gfc_add_expr_to_block (&body, tmp);
    6987              :     }
    6988              : 
    6989              :   /* If this is the main program and an -fconvert option was provided,
    6990              :      add a call to set_convert.  */
    6991              : 
    6992        26136 :   if (flag_convert != GFC_FLAG_CONVERT_NATIVE)
    6993              :     {
    6994           12 :       tmp = build_call_expr_loc (input_location,
    6995              :                              gfor_fndecl_set_convert, 1,
    6996           12 :                              build_int_cst (integer_type_node, flag_convert));
    6997           12 :       gfc_add_expr_to_block (&body, tmp);
    6998              :     }
    6999              : 
    7000              :   /* If this is the main program and an -frecord-marker option was provided,
    7001              :      add a call to set_record_marker.  */
    7002              : 
    7003        26136 :   if (flag_record_marker != 0)
    7004              :     {
    7005           18 :       tmp = build_call_expr_loc (input_location,
    7006              :                              gfor_fndecl_set_record_marker, 1,
    7007              :                              build_int_cst (integer_type_node,
    7008           18 :                                             flag_record_marker));
    7009           18 :       gfc_add_expr_to_block (&body, tmp);
    7010              :     }
    7011              : 
    7012        26136 :   if (flag_max_subrecord_length != 0)
    7013              :     {
    7014            6 :       tmp = build_call_expr_loc (input_location,
    7015              :                              gfor_fndecl_set_max_subrecord_length, 1,
    7016              :                              build_int_cst (integer_type_node,
    7017            6 :                                             flag_max_subrecord_length));
    7018            6 :       gfc_add_expr_to_block (&body, tmp);
    7019              :     }
    7020              : 
    7021              :   /* Call MAIN__().  */
    7022        26136 :   tmp = build_call_expr_loc (input_location,
    7023              :                          fndecl, 0);
    7024        26136 :   gfc_add_expr_to_block (&body, tmp);
    7025              : 
    7026              :   /* Mark MAIN__ as used.  */
    7027        26136 :   TREE_USED (fndecl) = 1;
    7028              : 
    7029              :   /* Coarray: Call _gfortran_caf_finalize(void).  */
    7030        26136 :   if (flag_coarray == GFC_FCOARRAY_LIB)
    7031              :     {
    7032          396 :       tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_finalize, 0);
    7033          396 :       gfc_add_expr_to_block (&body, tmp);
    7034              :     }
    7035              : 
    7036              :   /* "return 0".  */
    7037        26136 :   tmp = fold_build2_loc (input_location, MODIFY_EXPR, integer_type_node,
    7038        26136 :                          DECL_RESULT (ftn_main),
    7039              :                          integer_zero_node);
    7040        26136 :   tmp = build1_v (RETURN_EXPR, tmp);
    7041        26136 :   gfc_add_expr_to_block (&body, tmp);
    7042              : 
    7043              : 
    7044        26136 :   DECL_SAVED_TREE (ftn_main) = gfc_finish_block (&body);
    7045        26136 :   decl = getdecls ();
    7046              : 
    7047              :   /* Finish off this function and send it for code generation.  */
    7048        26136 :   poplevel (1, 1);
    7049        26136 :   BLOCK_SUPERCONTEXT (DECL_INITIAL (ftn_main)) = ftn_main;
    7050              : 
    7051        52272 :   DECL_SAVED_TREE (ftn_main)
    7052        52272 :     = fold_build3_loc (DECL_SOURCE_LOCATION (ftn_main), BIND_EXPR,
    7053        26136 :                        void_type_node, decl, DECL_SAVED_TREE (ftn_main),
    7054        26136 :                        DECL_INITIAL (ftn_main));
    7055              : 
    7056              :   /* Output the GENERIC tree.  */
    7057        26136 :   dump_function (TDI_original, ftn_main);
    7058              : 
    7059        26136 :   cgraph_node::finalize_function (ftn_main, true);
    7060              : 
    7061        26136 :   if (old_context)
    7062              :     {
    7063            0 :       pop_function_context ();
    7064            0 :       saved_function_decls = saved_parent_function_decls;
    7065              :     }
    7066        26136 :   current_function_decl = old_context;
    7067        26136 : }
    7068              : 
    7069              : 
    7070              : /* Generate an appropriate return-statement for a procedure.  */
    7071              : 
    7072              : tree
    7073        15045 : gfc_generate_return (void)
    7074              : {
    7075        15045 :   gfc_symbol* sym;
    7076        15045 :   tree result;
    7077        15045 :   tree fndecl;
    7078              : 
    7079        15045 :   sym = current_procedure_symbol;
    7080        15045 :   fndecl = sym->backend_decl;
    7081              : 
    7082        15045 :   if (TREE_TYPE (DECL_RESULT (fndecl)) == void_type_node)
    7083              :     result = NULL_TREE;
    7084              :   else
    7085              :     {
    7086        12752 :       result = get_proc_result (sym);
    7087              : 
    7088              :       /* Set the return value to the dummy result variable.  The
    7089              :          types may be different for scalar default REAL functions
    7090              :          with -ff2c, therefore we have to convert.  */
    7091        12752 :       if (result != NULL_TREE)
    7092              :         {
    7093        12741 :           result = convert (TREE_TYPE (DECL_RESULT (fndecl)), result);
    7094        25482 :           result = fold_build2_loc (input_location, MODIFY_EXPR,
    7095        12741 :                                     TREE_TYPE (result), DECL_RESULT (fndecl),
    7096              :                                     result);
    7097              :         }
    7098              :       else
    7099              :         {
    7100              :           /* If the function does not have a result variable, result is
    7101              :              NULL_TREE, and a 'return' is generated without a variable.
    7102              :              The following generates a 'return __result_XXX' where XXX is
    7103              :              the function name.  */
    7104           11 :           if (sym == sym->result && sym->attr.function && !flag_f2c)
    7105              :             {
    7106            8 :               result = gfc_get_fake_result_decl (sym, 0);
    7107           16 :               result = fold_build2_loc (input_location, MODIFY_EXPR,
    7108            8 :                                         TREE_TYPE (result),
    7109            8 :                                         DECL_RESULT (fndecl), result);
    7110              :             }
    7111              :         }
    7112              :     }
    7113              : 
    7114        15045 :   return build1_v (RETURN_EXPR, result);
    7115              : }
    7116              : 
    7117              : 
    7118              : static void
    7119      1055148 : is_from_ieee_module (gfc_symbol *sym)
    7120              : {
    7121      1055148 :   if (sym->from_intmod == INTMOD_IEEE_FEATURES
    7122              :       || sym->from_intmod == INTMOD_IEEE_EXCEPTIONS
    7123      1055148 :       || sym->from_intmod == INTMOD_IEEE_ARITHMETIC)
    7124       121726 :     seen_ieee_symbol = 1;
    7125      1055148 : }
    7126              : 
    7127              : 
    7128              : static int
    7129        84658 : is_ieee_module_used (gfc_namespace *ns)
    7130              : {
    7131        84658 :   seen_ieee_symbol = 0;
    7132            0 :   gfc_traverse_ns (ns, is_from_ieee_module);
    7133        84658 :   return seen_ieee_symbol;
    7134              : }
    7135              : 
    7136              : 
    7137              : static gfc_omp_clauses *module_oacc_clauses;
    7138              : 
    7139              : 
    7140              : static void
    7141          146 : add_clause (gfc_symbol *sym, gfc_omp_map_op map_op)
    7142              : {
    7143          146 :   gfc_omp_namelist *n;
    7144              : 
    7145          146 :   n = gfc_get_omp_namelist ();
    7146          146 :   n->sym = sym;
    7147          146 :   n->where = sym->declared_at;
    7148          146 :   n->u.map.op = map_op;
    7149              : 
    7150          146 :   if (!module_oacc_clauses)
    7151          124 :     module_oacc_clauses = gfc_get_omp_clauses ();
    7152              : 
    7153          146 :   if (module_oacc_clauses->lists[OMP_LIST_MAP])
    7154           22 :     n->next = module_oacc_clauses->lists[OMP_LIST_MAP];
    7155              : 
    7156          146 :   module_oacc_clauses->lists[OMP_LIST_MAP] = n;
    7157          146 : }
    7158              : 
    7159              : 
    7160              : static void
    7161      1085740 : find_module_oacc_declare_clauses (gfc_symbol *sym)
    7162              : {
    7163      1085740 :   if (sym->attr.use_assoc)
    7164              :     {
    7165       508986 :       gfc_omp_map_op map_op;
    7166              : 
    7167       508986 :       if (sym->attr.oacc_declare_create)
    7168       508986 :         map_op = OMP_MAP_FORCE_ALLOC;
    7169              : 
    7170       508986 :       if (sym->attr.oacc_declare_copyin)
    7171            2 :         map_op = OMP_MAP_FORCE_TO;
    7172              : 
    7173       508986 :       if (sym->attr.oacc_declare_deviceptr)
    7174            0 :         map_op = OMP_MAP_FORCE_DEVICEPTR;
    7175              : 
    7176       508986 :       if (sym->attr.oacc_declare_device_resident)
    7177           34 :         map_op = OMP_MAP_DEVICE_RESIDENT;
    7178              : 
    7179       508986 :       if (sym->attr.oacc_declare_create
    7180       508876 :           || sym->attr.oacc_declare_copyin
    7181       508874 :           || sym->attr.oacc_declare_deviceptr
    7182       508874 :           || sym->attr.oacc_declare_device_resident)
    7183              :         {
    7184          146 :           sym->attr.referenced = 1;
    7185          146 :           add_clause (sym, map_op);
    7186              :         }
    7187              :     }
    7188      1085740 : }
    7189              : 
    7190              : 
    7191              : void
    7192        98610 : finish_oacc_declare (gfc_namespace *ns, gfc_symbol *sym, bool block)
    7193              : {
    7194        98610 :   gfc_code *code;
    7195        98610 :   gfc_oacc_declare *oc;
    7196        98610 :   locus where;
    7197        98610 :   gfc_omp_clauses *omp_clauses = NULL;
    7198        98610 :   gfc_omp_namelist *n, *p;
    7199        98610 :   module_oacc_clauses = NULL;
    7200              : 
    7201        98610 :   gfc_locus_from_location (&where, input_location);
    7202        98610 :   gfc_traverse_ns (ns, find_module_oacc_declare_clauses);
    7203              : 
    7204        98610 :   if (module_oacc_clauses && sym->attr.flavor == FL_PROGRAM)
    7205              :     {
    7206           45 :       gfc_oacc_declare *new_oc;
    7207              : 
    7208           45 :       new_oc = gfc_get_oacc_declare ();
    7209           45 :       new_oc->next = ns->oacc_declare;
    7210           45 :       new_oc->clauses = module_oacc_clauses;
    7211              : 
    7212           45 :       ns->oacc_declare = new_oc;
    7213              :     }
    7214              : 
    7215        98610 :   if (!ns->oacc_declare)
    7216              :     return;
    7217              : 
    7218          190 :   for (oc = ns->oacc_declare; oc; oc = oc->next)
    7219              :     {
    7220          101 :       if (oc->module_var)
    7221            0 :         continue;
    7222              : 
    7223          101 :       if (block)
    7224            2 :         gfc_error ("Sorry, !$ACC DECLARE at %L is not allowed "
    7225              :                    "in BLOCK construct", &oc->loc);
    7226              : 
    7227              : 
    7228          101 :       if (oc->clauses && oc->clauses->lists[OMP_LIST_MAP])
    7229              :         {
    7230           88 :           if (omp_clauses == NULL)
    7231              :             {
    7232           76 :               omp_clauses = oc->clauses;
    7233           76 :               continue;
    7234              :             }
    7235              : 
    7236           48 :           for (n = oc->clauses->lists[OMP_LIST_MAP]; n; p = n, n = n->next)
    7237              :             ;
    7238              : 
    7239           12 :           gcc_assert (p->next == NULL);
    7240              : 
    7241           12 :           p->next = omp_clauses->lists[OMP_LIST_MAP];
    7242           12 :           omp_clauses = oc->clauses;
    7243              :         }
    7244              :     }
    7245              : 
    7246           89 :   if (!omp_clauses)
    7247              :     return;
    7248              : 
    7249          212 :   for (n = omp_clauses->lists[OMP_LIST_MAP]; n; n = n->next)
    7250              :     {
    7251          136 :       switch (n->u.map.op)
    7252              :         {
    7253           27 :           case OMP_MAP_DEVICE_RESIDENT:
    7254           27 :             n->u.map.op = OMP_MAP_FORCE_ALLOC;
    7255           27 :             break;
    7256              : 
    7257              :           default:
    7258              :             break;
    7259              :         }
    7260              :     }
    7261              : 
    7262           76 :   code = XCNEW (gfc_code);
    7263           76 :   code->op = EXEC_OACC_DECLARE;
    7264           76 :   code->loc = where;
    7265              : 
    7266           76 :   code->ext.oacc_declare = gfc_get_oacc_declare ();
    7267           76 :   code->ext.oacc_declare->clauses = omp_clauses;
    7268              : 
    7269           76 :   code->block = XCNEW (gfc_code);
    7270           76 :   code->block->op = EXEC_OACC_DECLARE;
    7271           76 :   code->block->loc = where;
    7272              : 
    7273           76 :   if (ns->code)
    7274           73 :     code->block->next = ns->code;
    7275              : 
    7276           76 :   ns->code = code;
    7277              : 
    7278           76 :   return;
    7279              : }
    7280              : 
    7281              : static void
    7282         1819 : gfc_conv_cfi_to_gfc (stmtblock_t *init, stmtblock_t *finally,
    7283              :                      tree cfi_desc, tree gfc_desc, gfc_symbol *sym)
    7284              : {
    7285         1819 :   stmtblock_t block;
    7286         1819 :   gfc_init_block (&block);
    7287         1819 :   tree cfi = build_fold_indirect_ref_loc (input_location, cfi_desc);
    7288         1819 :   tree idx, etype, tmp, tmp2, size_var = NULL_TREE, rank = NULL_TREE;
    7289         1819 :   bool do_copy_inout = false;
    7290              : 
    7291              :   /* When allocatable + intent out, free the cfi descriptor.  */
    7292         1819 :   if (sym->attr.allocatable && sym->attr.intent == INTENT_OUT)
    7293              :     {
    7294           54 :       tmp = gfc_get_cfi_desc_base_addr (cfi);
    7295           54 :       tree call = builtin_decl_explicit (BUILT_IN_FREE);
    7296           54 :       call = build_call_expr_loc (input_location, call, 1, tmp);
    7297           54 :       gfc_add_expr_to_block (&block, fold_convert (void_type_node, call));
    7298           54 :       gfc_add_modify (&block, tmp,
    7299           54 :                       fold_convert (TREE_TYPE (tmp), null_pointer_node));
    7300              :     }
    7301              : 
    7302              :   /* -fcheck=bound: Do version, rank, attribute, type and is-NULL checks.  */
    7303         1819 :   if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    7304              :     {
    7305          654 :       char *msg;
    7306          654 :       tree tmp3;
    7307          654 :       msg = xasprintf ("Unexpected version %%d (expected %d) in CFI descriptor "
    7308              :                        "passed to dummy argument %s", CFI_VERSION, sym->name);
    7309          654 :       tmp2 = gfc_get_cfi_desc_version (cfi);
    7310          654 :       tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, tmp2,
    7311          654 :                              build_int_cst (TREE_TYPE (tmp2), CFI_VERSION));
    7312          654 :       gfc_trans_runtime_check (true, false, tmp, &block, &sym->declared_at,
    7313              :                                msg, tmp2);
    7314          654 :       free (msg);
    7315              : 
    7316              :       /* Rank check; however, for character(len=*), assumed/explicit-size arrays
    7317              :          are permitted to differ in rank according to the Fortran rules.  */
    7318          654 :       if (sym->as && sym->as->type != AS_ASSUMED_SIZE
    7319          546 :           && sym->as->type != AS_EXPLICIT)
    7320              :         {
    7321          438 :           if (sym->as->rank != -1)
    7322          222 :             msg = xasprintf ("Invalid rank %%d (expected %d) in CFI descriptor "
    7323              :                              "passed to dummy argument %s", sym->as->rank,
    7324              :                              sym->name);
    7325              :           else
    7326          216 :             msg = xasprintf ("Invalid rank %%d (expected 0..%d) in CFI "
    7327              :                              "descriptor passed to dummy argument %s",
    7328              :                              CFI_MAX_RANK, sym->name);
    7329              : 
    7330          438 :           tmp3 = tmp2 = tmp = gfc_get_cfi_desc_rank (cfi);
    7331          438 :           if (sym->as->rank != -1)
    7332          222 :             tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
    7333              :                                    tmp, build_int_cst (signed_char_type_node,
    7334          222 :                                                        sym->as->rank));
    7335              :           else
    7336              :             {
    7337          216 :               tmp = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
    7338          216 :                                      tmp, build_zero_cst (TREE_TYPE (tmp)));
    7339          216 :               tmp2 = fold_build2_loc (input_location, GT_EXPR,
    7340              :                                       boolean_type_node, tmp2,
    7341          216 :                                       build_int_cst (TREE_TYPE (tmp2),
    7342              :                                                      CFI_MAX_RANK));
    7343          216 :               tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR,
    7344              :                                      boolean_type_node, tmp, tmp2);
    7345              :             }
    7346          438 :           gfc_trans_runtime_check (true, false, tmp, &block, &sym->declared_at,
    7347              :                                    msg, tmp3);
    7348          438 :           free (msg);
    7349              :         }
    7350              : 
    7351          654 :       tmp3 = tmp = gfc_get_cfi_desc_attribute (cfi);
    7352          654 :       if (sym->attr.allocatable || sym->attr.pointer)
    7353              :         {
    7354            6 :           int attr = (sym->attr.pointer ? CFI_attribute_pointer
    7355              :                                         : CFI_attribute_allocatable);
    7356           12 :           msg = xasprintf ("Invalid attribute %%d (expected %d) in CFI "
    7357              :                            "descriptor passed to dummy argument %s with %s "
    7358              :                            "attribute", attr, sym->name,
    7359              :                            sym->attr.pointer ? "pointer" : "allocatable");
    7360            6 :           tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
    7361            6 :                                  tmp, build_int_cst (TREE_TYPE (tmp), attr));
    7362            6 :         }
    7363              :       else
    7364              :         {
    7365          648 :           int amin = MIN (CFI_attribute_pointer,
    7366              :                           MIN (CFI_attribute_allocatable, CFI_attribute_other));
    7367          648 :           int amax = MAX (CFI_attribute_pointer,
    7368              :                           MAX (CFI_attribute_allocatable, CFI_attribute_other));
    7369          648 :           msg = xasprintf ("Invalid attribute %%d (expected %d..%d) in CFI "
    7370              :                            "descriptor passed to nonallocatable, nonpointer "
    7371              :                            "dummy argument %s", amin, amax, sym->name);
    7372          648 :           tmp2 = tmp;
    7373          648 :           tmp = fold_build2_loc (input_location, LT_EXPR, boolean_type_node, tmp,
    7374          648 :                              build_int_cst (TREE_TYPE (tmp), amin));
    7375          648 :           tmp2 = fold_build2_loc (input_location, GT_EXPR, boolean_type_node, tmp2,
    7376          648 :                              build_int_cst (TREE_TYPE (tmp2), amax));
    7377          648 :           tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR,
    7378              :                                  boolean_type_node, tmp, tmp2);
    7379          648 :           gfc_trans_runtime_check (true, false, tmp, &block, &sym->declared_at,
    7380              :                                    msg, tmp3);
    7381          648 :           free (msg);
    7382          648 :           msg = xasprintf ("Invalid unallocatated/unassociated CFI "
    7383              :                            "descriptor passed to nonallocatable, nonpointer "
    7384              :                            "dummy argument %s", sym->name);
    7385          648 :           tmp3 = tmp = gfc_get_cfi_desc_base_addr (cfi),
    7386          648 :           tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    7387              :                                  tmp, null_pointer_node);
    7388              :         }
    7389          654 :       gfc_trans_runtime_check (true, false, tmp, &block, &sym->declared_at,
    7390              :                                msg, tmp3);
    7391          654 :       free (msg);
    7392              : 
    7393          654 :       if (sym->ts.type != BT_ASSUMED)
    7394              :         {
    7395          654 :           int type = CFI_type_other;
    7396          654 :           if (sym->ts.f90_type == BT_VOID)
    7397              :             {
    7398            0 :               type = (sym->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR
    7399            0 :                       ? CFI_type_cfunptr : CFI_type_cptr);
    7400              :             }
    7401              :           else
    7402          654 :             switch (sym->ts.type)
    7403              :               {
    7404            6 :                 case BT_INTEGER:
    7405            6 :                 case BT_LOGICAL:
    7406            6 :                 case BT_REAL:
    7407            6 :                 case BT_COMPLEX:
    7408            6 :                   type = CFI_type_from_type_kind (sym->ts.type, sym->ts.kind);
    7409            6 :                   break;
    7410          648 :                 case BT_CHARACTER:
    7411          648 :                   type = CFI_type_from_type_kind (CFI_type_Character,
    7412              :                                                   sym->ts.kind);
    7413          648 :                   break;
    7414            0 :                 case BT_DERIVED:
    7415            0 :                   type = CFI_type_struct;
    7416            0 :                   break;
    7417            0 :                 case BT_VOID:
    7418            0 :                   type = (sym->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR
    7419            0 :                         ? CFI_type_cfunptr : CFI_type_cptr);
    7420              :                   break;
    7421              : 
    7422            0 :               case BT_UNSIGNED:
    7423            0 :                 gfc_internal_error ("Unsigned not yet implemented");
    7424              : 
    7425            0 :                 case BT_ASSUMED:
    7426            0 :                 case BT_CLASS:
    7427            0 :                 case BT_PROCEDURE:
    7428            0 :                 case BT_HOLLERITH:
    7429            0 :                 case BT_UNION:
    7430            0 :                 case BT_BOZ:
    7431            0 :                 case BT_UNKNOWN:
    7432            0 :                   gcc_unreachable ();
    7433              :             }
    7434          654 :           msg = xasprintf ("Unexpected type %%d (expected %d) in CFI descriptor"
    7435              :                            " passed to dummy argument %s", type, sym->name);
    7436          654 :           tmp2 = tmp = gfc_get_cfi_desc_type (cfi);
    7437          654 :           tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
    7438          654 :                                  tmp, build_int_cst (TREE_TYPE (tmp), type));
    7439          654 :           gfc_trans_runtime_check (true, false, tmp, &block, &sym->declared_at,
    7440              :                                msg, tmp2);
    7441          654 :           free (msg);
    7442              :         }
    7443              :     }
    7444              : 
    7445         1819 :   if (!sym->attr.referenced)
    7446           72 :     goto done;
    7447              : 
    7448              :   /* Set string length for len=* and len=:, otherwise, it is already set.  */
    7449         1747 :   if (sym->ts.type == BT_CHARACTER && !sym->ts.u.cl->length)
    7450              :     {
    7451          647 :       tmp = fold_convert (gfc_array_index_type,
    7452              :                           gfc_get_cfi_desc_elem_len (cfi));
    7453          647 :       if (sym->ts.kind != 1)
    7454          197 :         tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    7455              :                                gfc_array_index_type, tmp,
    7456              :                                build_int_cst (gfc_charlen_type_node,
    7457          197 :                                               sym->ts.kind));
    7458          647 :       gfc_add_modify (&block, sym->ts.u.cl->backend_decl, tmp);
    7459              :     }
    7460              : 
    7461         1747 :   if (sym->ts.type == BT_CHARACTER
    7462         1020 :       && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
    7463              :     {
    7464          804 :       gfc_conv_string_length (sym->ts.u.cl, NULL, &block);
    7465          804 :       gfc_trans_vla_type_sizes (sym, &block);
    7466              :     }
    7467              : 
    7468              :   /* gfc->data = cfi->base_addr - or for scalars: gfc = cfi->base_addr.
    7469              :      assumed-size/explicit-size arrays end up here for character(len=*)
    7470              :      only. */
    7471         1747 :   if (!sym->attr.dimension || !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
    7472              :     {
    7473          418 :       tmp = gfc_get_cfi_desc_base_addr (cfi);
    7474          418 :       gfc_add_modify (&block, gfc_desc,
    7475          418 :                       fold_convert (TREE_TYPE (gfc_desc), tmp));
    7476          418 :       if (!sym->attr.dimension)
    7477          164 :         goto done;
    7478              :     }
    7479              : 
    7480         1583 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
    7481              :     {
    7482              :       /* gfc->dtype = ... (from declaration, not from cfi).  */
    7483         1329 :       etype = gfc_get_element_type (TREE_TYPE (gfc_desc));
    7484         1329 :       gfc_add_modify (&block, gfc_conv_descriptor_dtype (gfc_desc),
    7485         1329 :                       gfc_get_dtype_rank_type (sym->as->rank, etype));
    7486              :       /* gfc->data = cfi->base_addr. */
    7487         1329 :       gfc_conv_descriptor_data_set (&block, gfc_desc,
    7488              :                                     gfc_get_cfi_desc_base_addr (cfi));
    7489              :     }
    7490              : 
    7491         1583 :   if (sym->ts.type == BT_ASSUMED)
    7492              :     {
    7493              :       /* For type(*), take elem_len + dtype.type from the actual argument.  */
    7494           19 :       gfc_add_modify (&block, gfc_conv_descriptor_elem_len (gfc_desc),
    7495              :                       gfc_get_cfi_desc_elem_len (cfi));
    7496           19 :       tree cond;
    7497           19 :       tree ctype = gfc_get_cfi_desc_type (cfi);
    7498           19 :       ctype = fold_build2_loc (input_location, BIT_AND_EXPR, TREE_TYPE (ctype),
    7499           19 :                                ctype, build_int_cst (TREE_TYPE (ctype),
    7500              :                                                      CFI_type_mask));
    7501           19 :       tree type = gfc_conv_descriptor_type (gfc_desc);
    7502              : 
    7503              :       /* if (CFI_type_cptr) BT_VOID else BT_UNKNOWN  */
    7504              :       /* Note: BT_VOID is could also be CFI_type_funcptr, but assume c_ptr. */
    7505           19 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, ctype,
    7506           19 :                               build_int_cst (TREE_TYPE (ctype), CFI_type_cptr));
    7507           19 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, type,
    7508           19 :                              build_int_cst (TREE_TYPE (type), BT_VOID));
    7509           19 :       tmp2 = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
    7510              :                               type,
    7511           19 :                               build_int_cst (TREE_TYPE (type), BT_UNKNOWN));
    7512           19 :       tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    7513              :                               tmp, tmp2);
    7514              :       /* if (CFI_type_struct) BT_DERIVED else  < tmp2 >  */
    7515           19 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, ctype,
    7516           19 :                               build_int_cst (TREE_TYPE (ctype),
    7517              :                                              CFI_type_struct));
    7518           19 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, type,
    7519           19 :                              build_int_cst (TREE_TYPE (type), BT_DERIVED));
    7520           19 :       tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    7521              :                               tmp, tmp2);
    7522              :       /* if (CFI_type_Character) BT_CHARACTER else  < tmp2 >  */
    7523              :       /* Note: this is kind=1, CFI_type_ucs4_char is handled in the 'else if'
    7524              :          before (see below, as generated bottom up).  */
    7525           19 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, ctype,
    7526           19 :                               build_int_cst (TREE_TYPE (ctype),
    7527              :                               CFI_type_Character));
    7528           19 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, type,
    7529           19 :                              build_int_cst (TREE_TYPE (type), BT_CHARACTER));
    7530           19 :       tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    7531              :                               tmp, tmp2);
    7532              :       /* if (CFI_type_ucs4_char) BT_CHARACTER else  < tmp2 >  */
    7533              :       /* Note: gfc->elem_len = cfi->elem_len/4.  */
    7534              :       /* However, assuming that CFI_type_ucs4_char cannot be recovered, leave
    7535              :          gfc->elem_len == cfi->elem_len, which helps with operations which use
    7536              :          sizeof() in Fortran and cfi->elem_len in C.  */
    7537           19 :       tmp = gfc_get_cfi_desc_type (cfi);
    7538           19 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, tmp,
    7539           19 :                               build_int_cst (TREE_TYPE (tmp),
    7540              :                                              CFI_type_ucs4_char));
    7541           19 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, type,
    7542           19 :                              build_int_cst (TREE_TYPE (type), BT_CHARACTER));
    7543           19 :       tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    7544              :                               tmp, tmp2);
    7545              :       /* if (CFI_type_Complex) BT_COMPLEX + cfi->elem_len/2 else  < tmp2 >  */
    7546           19 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, ctype,
    7547           19 :                               build_int_cst (TREE_TYPE (ctype),
    7548              :                               CFI_type_Complex));
    7549           19 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, type,
    7550           19 :                              build_int_cst (TREE_TYPE (type), BT_COMPLEX));
    7551           19 :       tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    7552              :                               tmp, tmp2);
    7553              :       /* if (CFI_type_Integer || CFI_type_Logical || CFI_type_Real)
    7554              :            ctype else  <tmp2>  */
    7555           19 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, ctype,
    7556           19 :                               build_int_cst (TREE_TYPE (ctype),
    7557              :                                              CFI_type_Integer));
    7558           19 :       tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, ctype,
    7559           19 :                               build_int_cst (TREE_TYPE (ctype),
    7560              :                                              CFI_type_Logical));
    7561           19 :       cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
    7562              :                               cond, tmp);
    7563           19 :       tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, ctype,
    7564           19 :                               build_int_cst (TREE_TYPE (ctype),
    7565              :                                              CFI_type_Real));
    7566           19 :       cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
    7567              :                               cond, tmp);
    7568           19 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
    7569           19 :                              type, fold_convert (TREE_TYPE (type), ctype));
    7570           19 :       tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    7571              :                               tmp, tmp2);
    7572           19 :       gfc_add_expr_to_block (&block, tmp2);
    7573              :     }
    7574              : 
    7575         1583 :   if (sym->as->rank < 0)
    7576              :     {
    7577              :       /* Set gfc->dtype.rank, if assumed-rank.  */
    7578          587 :       rank = gfc_get_cfi_desc_rank (cfi);
    7579          587 :       gfc_add_modify (&block, gfc_conv_descriptor_rank (gfc_desc), rank);
    7580              :     }
    7581          996 :   else if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
    7582              :     /* In that case, the CFI rank and the declared rank can differ.  */
    7583          254 :     rank = gfc_get_cfi_desc_rank (cfi);
    7584              :   else
    7585          742 :     rank = build_int_cst (signed_char_type_node, sym->as->rank);
    7586              : 
    7587              :   /* With bind(C), the standard requires that both Fortran callers and callees
    7588              :      handle noncontiguous arrays passed to an dummy with 'contiguous' attribute
    7589              :      and with character(len=*) + assumed-size/explicit-size arrays.
    7590              :      cf. Fortran 2018, 18.3.6, paragraph 5 (and for the caller: para. 6). */
    7591         1583 :   if ((sym->ts.type == BT_CHARACTER && !sym->ts.u.cl->length
    7592          550 :        && (sym->as->type == AS_ASSUMED_SIZE || sym->as->type == AS_EXPLICIT))
    7593         1329 :       || sym->attr.contiguous)
    7594              :     {
    7595          517 :       do_copy_inout = true;
    7596          517 :       gcc_assert (!sym->attr.pointer);
    7597          517 :       stmtblock_t block2;
    7598          517 :       tree data;
    7599          517 :       if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
    7600          263 :         data = gfc_conv_descriptor_data_get (gfc_desc);
    7601          254 :       else if (!POINTER_TYPE_P (TREE_TYPE (gfc_desc)))
    7602            0 :         data = gfc_build_addr_expr (NULL, gfc_desc);
    7603              :       else
    7604              :         data = gfc_desc;
    7605              : 
    7606              :       /* Is copy-in/out needed? */
    7607              :       /* do_copyin = rank != 0 && !assumed-size */
    7608          517 :       tree cond_var = gfc_create_var (boolean_type_node, "do_copyin");
    7609          517 :       tree cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
    7610          517 :                                    rank, build_zero_cst (TREE_TYPE (rank)));
    7611              :       /* dim[rank-1].extent != -1 -> assumed size*/
    7612          517 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (rank),
    7613          517 :                              rank, build_int_cst (TREE_TYPE (rank), 1));
    7614          517 :       tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
    7615              :                               gfc_get_cfi_dim_extent (cfi, tmp),
    7616              :                               build_int_cst (gfc_array_index_type, -1));
    7617          517 :       cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    7618              :                               boolean_type_node, cond, tmp);
    7619          517 :       gfc_add_modify (&block, cond_var, cond);
    7620              :       /* if (do_copyin) do_copyin = ... || ... || ... */
    7621          517 :       gfc_init_block (&block2);
    7622              :       /* dim[0].sm != elem_len */
    7623          517 :       tmp = fold_convert (gfc_array_index_type,
    7624              :                           gfc_get_cfi_desc_elem_len (cfi));
    7625          517 :       cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
    7626              :                               gfc_get_cfi_dim_sm (cfi, gfc_index_zero_node),
    7627              :                               tmp);
    7628          517 :       gfc_add_modify (&block2, cond_var, cond);
    7629              : 
    7630              :       /* for (i = 1; i < rank; ++i)
    7631              :            cond &&= dim[i].sm != (dv->dim[i - 1].sm * dv->dim[i - 1].extent) */
    7632          517 :       idx = gfc_create_var (TREE_TYPE (rank), "idx");
    7633          517 :       stmtblock_t loop_body;
    7634          517 :       gfc_init_block (&loop_body);
    7635          517 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (idx),
    7636          517 :                              idx, build_int_cst (TREE_TYPE (idx), 1));
    7637          517 :       tree tmp2 = gfc_get_cfi_dim_sm (cfi, tmp);
    7638          517 :       tmp = gfc_get_cfi_dim_extent (cfi, tmp);
    7639          517 :       tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
    7640              :                              tmp2, tmp);
    7641          517 :       cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
    7642              :                              gfc_get_cfi_dim_sm (cfi, idx), tmp);
    7643          517 :       cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
    7644              :                               cond_var, cond);
    7645          517 :       gfc_add_modify (&loop_body, cond_var, cond);
    7646         1034 :       gfc_simple_for_loop (&block2, idx, build_int_cst (TREE_TYPE (idx), 1),
    7647          517 :                           rank, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
    7648              :                           gfc_finish_block (&loop_body));
    7649          517 :       tmp = build3_v (COND_EXPR, cond_var, gfc_finish_block (&block2),
    7650              :                       build_empty_stmt (input_location));
    7651          517 :       gfc_add_expr_to_block (&block, tmp);
    7652              : 
    7653              :       /* Copy-in body.  */
    7654          517 :       gfc_init_block (&block2);
    7655              :       /* size = dim[0].extent; for (i = 1; i < rank; ++i) size *= dim[i].extent */
    7656          517 :       size_var = gfc_create_var (size_type_node, "size");
    7657          517 :       tmp = fold_convert (size_type_node,
    7658              :                           gfc_get_cfi_dim_extent (cfi, gfc_index_zero_node));
    7659          517 :       gfc_add_modify (&block2, size_var, tmp);
    7660              : 
    7661          517 :       gfc_init_block (&loop_body);
    7662          517 :       tmp = fold_convert (size_type_node,
    7663              :                           gfc_get_cfi_dim_extent (cfi, idx));
    7664          517 :       tmp = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
    7665              :                              size_var, fold_convert (size_type_node, tmp));
    7666          517 :       gfc_add_modify (&loop_body, size_var, tmp);
    7667         1034 :       gfc_simple_for_loop (&block2, idx, build_int_cst (TREE_TYPE (idx), 1),
    7668          517 :                           rank, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
    7669              :                           gfc_finish_block (&loop_body));
    7670              :       /* data = malloc (size * elem_len) */
    7671          517 :       tmp = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
    7672              :                              size_var, gfc_get_cfi_desc_elem_len (cfi));
    7673          517 :       tree call = builtin_decl_explicit (BUILT_IN_MALLOC);
    7674          517 :       call = build_call_expr_loc (input_location, call, 1, tmp);
    7675          517 :       gfc_add_modify (&block2, data, fold_convert (TREE_TYPE (data), call));
    7676              : 
    7677              :       /* Copy the data:
    7678              :          for (idx = 0; idx < size; ++idx)
    7679              :            {
    7680              :              shift = 0;
    7681              :              tmpidx = idx
    7682              :              for (dim = 0; dim < rank; ++dim)
    7683              :                 {
    7684              :                   shift += (tmpidx % extent[d]) * sm[d]
    7685              :                   tmpidx = tmpidx / extend[d]
    7686              :                 }
    7687              :              memcpy (lhs + idx*elem_len, rhs + shift, elem_len)
    7688              :            } .*/
    7689          517 :       idx = gfc_create_var (size_type_node, "arrayidx");
    7690          517 :       gfc_init_block (&loop_body);
    7691          517 :       tree shift = gfc_create_var (size_type_node, "shift");
    7692          517 :       tree tmpidx = gfc_create_var (size_type_node, "tmpidx");
    7693          517 :       gfc_add_modify (&loop_body, shift, build_zero_cst (TREE_TYPE (shift)));
    7694          517 :       gfc_add_modify (&loop_body, tmpidx, idx);
    7695          517 :       stmtblock_t inner_loop;
    7696          517 :       gfc_init_block (&inner_loop);
    7697          517 :       tree dim = gfc_create_var (TREE_TYPE (rank), "dim");
    7698              :       /* shift += (tmpidx % extent[d]) * sm[d] */
    7699          517 :       tmp = fold_build2_loc (input_location, TRUNC_MOD_EXPR,
    7700              :                              size_type_node, tmpidx,
    7701              :                              fold_convert (size_type_node,
    7702              :                                            gfc_get_cfi_dim_extent (cfi, dim)));
    7703          517 :       tmp = fold_build2_loc (input_location, MULT_EXPR,
    7704              :                              size_type_node, tmp,
    7705              :                              fold_convert (size_type_node,
    7706              :                                            gfc_get_cfi_dim_sm (cfi, dim)));
    7707          517 :       gfc_add_modify (&inner_loop, shift,
    7708              :                       fold_build2_loc (input_location, PLUS_EXPR,
    7709              :                                        size_type_node, shift, tmp));
    7710              :       /* tmpidx = tmpidx / extend[d] */
    7711          517 :       tmp = fold_convert (size_type_node, gfc_get_cfi_dim_extent (cfi, dim));
    7712          517 :       gfc_add_modify (&inner_loop, tmpidx,
    7713              :                       fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    7714              :                                        size_type_node, tmpidx, tmp));
    7715         1034 :       gfc_simple_for_loop (&loop_body, dim, build_zero_cst (TREE_TYPE (rank)),
    7716          517 :                            rank, LT_EXPR, build_int_cst (TREE_TYPE (dim), 1),
    7717              :                            gfc_finish_block (&inner_loop));
    7718              :       /* Assign.  */
    7719          517 :       tmp = fold_convert (pchar_type_node, gfc_get_cfi_desc_base_addr (cfi));
    7720          517 :       tmp = fold_build2 (POINTER_PLUS_EXPR, pchar_type_node, tmp, shift);
    7721          517 :       tree lhs;
    7722              :       /* memcpy (lhs + idx*elem_len, rhs + shift, elem_len)  */
    7723          517 :       tree elem_len;
    7724          517 :       if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
    7725          263 :         elem_len = gfc_conv_descriptor_elem_len (gfc_desc);
    7726              :       else
    7727          254 :         elem_len = gfc_get_cfi_desc_elem_len (cfi);
    7728          517 :       lhs = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
    7729              :                              elem_len, idx);
    7730          517 :       lhs = fold_build2_loc (input_location, POINTER_PLUS_EXPR, pchar_type_node,
    7731              :                              fold_convert (pchar_type_node, data), lhs);
    7732          517 :       tmp = fold_convert (pvoid_type_node, tmp);
    7733          517 :       lhs = fold_convert (pvoid_type_node, lhs);
    7734          517 :       call = builtin_decl_explicit (BUILT_IN_MEMCPY);
    7735          517 :       call = build_call_expr_loc (input_location, call, 3, lhs, tmp, elem_len);
    7736          517 :       gfc_add_expr_to_block (&loop_body, fold_convert (void_type_node, call));
    7737         1034 :       gfc_simple_for_loop (&block2, idx, build_zero_cst (TREE_TYPE (idx)),
    7738          517 :                            size_var, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
    7739              :                            gfc_finish_block (&loop_body));
    7740              :       /* if (cond) { block2 }  */
    7741          517 :       tmp = build3_v (COND_EXPR, cond_var, gfc_finish_block (&block2),
    7742              :                       build_empty_stmt (input_location));
    7743          517 :       gfc_add_expr_to_block (&block, tmp);
    7744              :     }
    7745              : 
    7746         1583 :   if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
    7747              :     {
    7748          254 :       tree offset, type;
    7749          254 :       type = TREE_TYPE (gfc_desc);
    7750          254 :       gfc_trans_array_bounds (type, sym, &offset, &block);
    7751          254 :       if (VAR_P (GFC_TYPE_ARRAY_OFFSET (type)))
    7752          144 :         gfc_add_modify (&block, GFC_TYPE_ARRAY_OFFSET (type), offset);
    7753          254 :       goto done;
    7754              :     }
    7755              : 
    7756              :   /* If cfi->data != NULL. */
    7757         1329 :   stmtblock_t block2;
    7758         1329 :   gfc_init_block (&block2);
    7759              : 
    7760              :   /* if do_copy_inout:  gfc->dspan = gfc->dtype.elem_len
    7761              :      We use gfc instead of cfi on the RHS as this might be a constant.  */
    7762         1329 :   tmp = fold_convert (gfc_array_index_type,
    7763              :                       gfc_conv_descriptor_elem_len (gfc_desc));
    7764         1329 :   if (!do_copy_inout)
    7765              :     {
    7766              :       /* gfc->dspan = ((cfi->dim[0].sm % gfc->elem_len)
    7767              :                        ? cfi->dim[0].sm : gfc->elem_len).  */
    7768         1066 :       tree cond;
    7769         1066 :       tree tmp2 = gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]);
    7770         1066 :       cond = fold_build2_loc (input_location, TRUNC_MOD_EXPR,
    7771              :                               gfc_array_index_type, tmp2, tmp);
    7772         1066 :       cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
    7773              :                               cond, gfc_index_zero_node);
    7774         1066 :       tmp = build3_loc (input_location, COND_EXPR, gfc_array_index_type, cond,
    7775              :                         tmp2, tmp);
    7776              :     }
    7777         1329 :   gfc_conv_descriptor_span_set (&block2, gfc_desc, tmp);
    7778              : 
    7779              :   /* Calculate offset + set lbound, ubound and stride.  */
    7780         1329 :   gfc_conv_descriptor_offset_set (&block2, gfc_desc, gfc_index_zero_node);
    7781         1329 :   if (sym->as->rank > 0 && !sym->attr.pointer && !sym->attr.allocatable)
    7782         1274 :     for (int i = 0; i < sym->as->rank; ++i)
    7783              :       {
    7784          718 :         gfc_se se;
    7785          718 :         gfc_init_se (&se, NULL );
    7786          718 :         if (sym->as->lower[i])
    7787              :           {
    7788          718 :             gfc_conv_expr (&se, sym->as->lower[i]);
    7789          718 :             tmp = se.expr;
    7790              :           }
    7791              :         else
    7792            0 :           tmp = gfc_index_one_node;
    7793          718 :         gfc_add_block_to_block (&block2, &se.pre);
    7794          718 :         gfc_conv_descriptor_lbound_set (&block2, gfc_desc, gfc_rank_cst[i],
    7795              :                                         tmp);
    7796          718 :         gfc_add_block_to_block (&block2, &se.post);
    7797              :       }
    7798              : 
    7799              :   /* Loop: for (i = 0; i < rank; ++i).  */
    7800         1329 :   idx = gfc_create_var (TREE_TYPE (rank), "idx");
    7801              : 
    7802              :   /* Loop body.  */
    7803         1329 :   stmtblock_t loop_body;
    7804         1329 :   gfc_init_block (&loop_body);
    7805              :   /* gfc->dim[i].lbound = ... */
    7806         1329 :   if (sym->attr.pointer || sym->attr.allocatable)
    7807              :     {
    7808          276 :       tmp = gfc_get_cfi_dim_lbound (cfi, idx);
    7809          276 :       gfc_conv_descriptor_lbound_set (&loop_body, gfc_desc, idx, tmp);
    7810              :     }
    7811         1053 :   else if (sym->as->rank < 0)
    7812          497 :     gfc_conv_descriptor_lbound_set (&loop_body, gfc_desc, idx,
    7813              :                                     gfc_index_one_node);
    7814              : 
    7815              :   /* gfc->dim[i].ubound = gfc->dim[i].lbound + cfi->dim[i].extent - 1. */
    7816         1329 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    7817              :                              gfc_conv_descriptor_lbound_get (gfc_desc, idx),
    7818              :                              gfc_index_one_node);
    7819         1329 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    7820              :                              gfc_get_cfi_dim_extent (cfi, idx), tmp);
    7821         1329 :   gfc_conv_descriptor_ubound_set (&loop_body, gfc_desc, idx, tmp);
    7822              : 
    7823         1329 :   if (do_copy_inout)
    7824              :     {
    7825              :       /* gfc->dim[i].stride
    7826              :            = idx == 0 ? 1 : gfc->dim[i-1].stride * cfi->dim[i-1].extent */
    7827          263 :       tree cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    7828          263 :                                    idx, build_zero_cst (TREE_TYPE (idx)));
    7829          263 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, TREE_TYPE (idx),
    7830          263 :                              idx, build_int_cst (TREE_TYPE (idx), 1));
    7831          263 :       tree tmp2 = gfc_get_cfi_dim_extent (cfi, tmp);
    7832          263 :       tmp = gfc_conv_descriptor_stride_get (gfc_desc, tmp);
    7833          263 :       tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp2),
    7834              :                              tmp2, tmp);
    7835          263 :       tmp = build3_loc (input_location, COND_EXPR, gfc_array_index_type, cond,
    7836              :                         gfc_index_one_node, tmp);
    7837              :     }
    7838              :   else
    7839              :     {
    7840              :       /* gfc->dim[i].stride = cfi->dim[i].sm / cfi>elem_len */
    7841         1066 :       tmp = gfc_get_cfi_dim_sm (cfi, idx);
    7842         1066 :       tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    7843              :                              gfc_array_index_type, tmp,
    7844              :                              fold_convert (gfc_array_index_type,
    7845              :                                            gfc_get_cfi_desc_elem_len (cfi)));
    7846              :      }
    7847         1329 :   gfc_conv_descriptor_stride_set (&loop_body, gfc_desc, idx, tmp);
    7848              :   /* gfc->offset -= gfc->dim[i].stride * gfc->dim[i].lbound. */
    7849         1329 :   tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    7850              :                              gfc_conv_descriptor_stride_get (gfc_desc, idx),
    7851              :                              gfc_conv_descriptor_lbound_get (gfc_desc, idx));
    7852         1329 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    7853              :                              gfc_conv_descriptor_offset_get (gfc_desc), tmp);
    7854         1329 :   gfc_conv_descriptor_offset_set (&loop_body, gfc_desc, tmp);
    7855              : 
    7856              :   /* Generate loop.  */
    7857         2658 :   gfc_simple_for_loop (&block2, idx, build_zero_cst (TREE_TYPE (idx)),
    7858         1329 :                        rank, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
    7859              :                        gfc_finish_block (&loop_body));
    7860         1329 :   if (sym->attr.allocatable || sym->attr.pointer)
    7861              :     {
    7862          276 :       tmp = gfc_get_cfi_desc_base_addr (cfi),
    7863          276 :       tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
    7864              :                              tmp, null_pointer_node);
    7865          276 :       tmp = build3_v (COND_EXPR, tmp, gfc_finish_block (&block2),
    7866              :                       build_empty_stmt (input_location));
    7867          276 :       gfc_add_expr_to_block (&block, tmp);
    7868              :     }
    7869              :   else
    7870         1053 :     gfc_add_block_to_block (&block, &block2);
    7871              : 
    7872         1819 : done:
    7873              :   /* If optional arg: 'if (arg) { block } else { local_arg = NULL; }'.  */
    7874         1819 :   if (sym->attr.optional)
    7875              :     {
    7876          317 :       tree present = fold_build2_loc (input_location, NE_EXPR,
    7877              :                                       boolean_type_node, cfi_desc,
    7878              :                                       null_pointer_node);
    7879          317 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
    7880              :                              sym->backend_decl,
    7881          317 :                              fold_convert (TREE_TYPE (sym->backend_decl),
    7882              :                                            null_pointer_node));
    7883          317 :       tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block), tmp);
    7884          317 :       gfc_add_expr_to_block (init, tmp);
    7885              :     }
    7886              :   else
    7887         1502 :     gfc_add_block_to_block (init, &block);
    7888              : 
    7889         1819 :   if (!sym->attr.referenced)
    7890          984 :     return;
    7891              : 
    7892              :   /* If pointer not changed, nothing to be done (except copy out)  */
    7893         1747 :   if (!do_copy_inout && ((!sym->attr.pointer && !sym->attr.allocatable)
    7894          381 :                          || sym->attr.intent == INTENT_IN))
    7895              :     return;
    7896              : 
    7897          835 :   gfc_init_block (&block);
    7898              : 
    7899              :   /* For bind(C), Fortran does not permit mixing 'pointer' with 'contiguous' (or
    7900              :      len=*). Thus, when copy out is needed, the bounds ofthe descriptor remain
    7901              :      unchanged.  */
    7902          835 :   if (do_copy_inout)
    7903              :     {
    7904          517 :       tree data, call;
    7905          517 :       if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
    7906          263 :         data = gfc_conv_descriptor_data_get (gfc_desc);
    7907          254 :       else if (!POINTER_TYPE_P (TREE_TYPE (gfc_desc)))
    7908            0 :         data = gfc_build_addr_expr (NULL, gfc_desc);
    7909              :       else
    7910              :         data = gfc_desc;
    7911          517 :       gfc_init_block (&block2);
    7912          517 :       if (sym->attr.intent != INTENT_IN)
    7913              :         {
    7914              :          /* First, create the inner copy-out loop.
    7915              :           for (idx = 0; idx < size; ++idx)
    7916              :            {
    7917              :              shift = 0;
    7918              :              tmpidx = idx
    7919              :              for (dim = 0; dim < rank; ++dim)
    7920              :                 {
    7921              :                   shift += (tmpidx % extent[d]) * sm[d]
    7922              :                   tmpidx = tmpidx / extend[d]
    7923              :                 }
    7924              :              memcpy (lhs + shift, rhs + idx*elem_len, elem_len)
    7925              :            } .*/
    7926          292 :           stmtblock_t loop_body;
    7927          292 :           idx = gfc_create_var (size_type_node, "arrayidx");
    7928          292 :           gfc_init_block (&loop_body);
    7929          292 :           tree shift = gfc_create_var (size_type_node, "shift");
    7930          292 :           tree tmpidx = gfc_create_var (size_type_node, "tmpidx");
    7931          292 :           gfc_add_modify (&loop_body, shift,
    7932          292 :                           build_zero_cst (TREE_TYPE (shift)));
    7933          292 :           gfc_add_modify (&loop_body, tmpidx, idx);
    7934          292 :           stmtblock_t inner_loop;
    7935          292 :           gfc_init_block (&inner_loop);
    7936          292 :           tree dim = gfc_create_var (TREE_TYPE (rank), "dim");
    7937              :           /* shift += (tmpidx % extent[d]) * sm[d] */
    7938          292 :           tmp = fold_convert (size_type_node,
    7939              :                               gfc_get_cfi_dim_extent (cfi, dim));
    7940          292 :           tmp = fold_build2_loc (input_location, TRUNC_MOD_EXPR,
    7941              :                                  size_type_node, tmpidx, tmp);
    7942          292 :           tmp = fold_build2_loc (input_location, MULT_EXPR,
    7943              :                                  size_type_node, tmp,
    7944              :                                  fold_convert (size_type_node,
    7945              :                                                gfc_get_cfi_dim_sm (cfi, dim)));
    7946          292 :           gfc_add_modify (&inner_loop, shift,
    7947              :                       fold_build2_loc (input_location, PLUS_EXPR,
    7948              :                                        size_type_node, shift, tmp));
    7949              :           /* tmpidx = tmpidx / extend[d] */
    7950          292 :           tmp = fold_convert (size_type_node,
    7951              :                               gfc_get_cfi_dim_extent (cfi, dim));
    7952          292 :           gfc_add_modify (&inner_loop, tmpidx,
    7953              :                           fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    7954              :                                            size_type_node, tmpidx, tmp));
    7955          876 :           gfc_simple_for_loop (&loop_body, dim,
    7956          292 :                                build_zero_cst (TREE_TYPE (rank)), rank, LT_EXPR,
    7957          292 :                                build_int_cst (TREE_TYPE (dim), 1),
    7958              :                                gfc_finish_block (&inner_loop));
    7959              :           /* Assign.  */
    7960          292 :           tree rhs;
    7961          292 :           tmp = fold_convert (pchar_type_node,
    7962              :                               gfc_get_cfi_desc_base_addr (cfi));
    7963          292 :           tmp = fold_build2 (POINTER_PLUS_EXPR, pchar_type_node, tmp, shift);
    7964              :           /* memcpy (lhs + shift, rhs + idx*elem_len, elem_len) */
    7965          292 :           tree elem_len;
    7966          292 :           if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (gfc_desc)))
    7967          153 :             elem_len = gfc_conv_descriptor_elem_len (gfc_desc);
    7968              :           else
    7969          139 :             elem_len = gfc_get_cfi_desc_elem_len (cfi);
    7970          292 :           rhs = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
    7971              :                                  elem_len, idx);
    7972          292 :           rhs = fold_build2_loc (input_location, POINTER_PLUS_EXPR,
    7973              :                                  pchar_type_node,
    7974              :                                  fold_convert (pchar_type_node, data), rhs);
    7975          292 :           tmp = fold_convert (pvoid_type_node, tmp);
    7976          292 :           rhs = fold_convert (pvoid_type_node, rhs);
    7977          292 :           call = builtin_decl_explicit (BUILT_IN_MEMCPY);
    7978          292 :           call = build_call_expr_loc (input_location, call, 3, tmp, rhs,
    7979              :                                       elem_len);
    7980          292 :           gfc_add_expr_to_block (&loop_body,
    7981              :                                  fold_convert (void_type_node, call));
    7982          584 :           gfc_simple_for_loop (&block2, idx, build_zero_cst (TREE_TYPE (idx)),
    7983              :                                size_var, LT_EXPR,
    7984          292 :                                build_int_cst (TREE_TYPE (idx), 1),
    7985              :                                gfc_finish_block (&loop_body));
    7986              :         }
    7987          517 :       call = builtin_decl_explicit (BUILT_IN_FREE);
    7988          517 :       call = build_call_expr_loc (input_location, call, 1, data);
    7989          517 :       gfc_add_expr_to_block (&block2, call);
    7990              : 
    7991              :       /* if (cfi->base_addr != gfc->data) { copy out; free(var) }; return  */
    7992          517 :       tree tmp2 = gfc_get_cfi_desc_base_addr (cfi);
    7993          517 :       tmp2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
    7994          517 :                               tmp2, fold_convert (TREE_TYPE (tmp2), data));
    7995          517 :       tmp = build3_v (COND_EXPR, tmp2, gfc_finish_block (&block2),
    7996              :                       build_empty_stmt (input_location));
    7997          517 :       gfc_add_expr_to_block (&block, tmp);
    7998          517 :       goto done_finally;
    7999              :     }
    8000              : 
    8001              :   /* Update pointer + array data data on exit.  */
    8002          318 :   tmp = gfc_get_cfi_desc_base_addr (cfi);
    8003          318 :   tmp2 = (!sym->attr.dimension
    8004          318 :                ? gfc_desc : gfc_conv_descriptor_data_get (gfc_desc));
    8005          318 :   gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), tmp2));
    8006              : 
    8007              :   /* Set string length for len=:, only.  */
    8008          318 :   if (sym->ts.type == BT_CHARACTER && !sym->ts.u.cl->length)
    8009              :     {
    8010           60 :       tmp2 = gfc_get_cfi_desc_elem_len (cfi);
    8011           60 :       tmp = fold_convert (TREE_TYPE (tmp2), sym->ts.u.cl->backend_decl);
    8012           60 :       if (sym->ts.kind != 1)
    8013           48 :         tmp = fold_build2_loc (input_location, MULT_EXPR,
    8014           24 :                                TREE_TYPE (tmp2), tmp,
    8015           24 :                                build_int_cst (TREE_TYPE (tmp2), sym->ts.kind));
    8016           60 :       gfc_add_modify (&block, tmp2, tmp);
    8017              :     }
    8018              : 
    8019          318 :   if (!sym->attr.dimension)
    8020          102 :     goto done_finally;
    8021              : 
    8022          216 :   gfc_init_block (&block2);
    8023              : 
    8024              :   /* Loop: for (i = 0; i < rank; ++i).  */
    8025          216 :   idx = gfc_create_var (TREE_TYPE (rank), "idx");
    8026              : 
    8027              :   /* Loop body.  */
    8028          216 :   gfc_init_block (&loop_body);
    8029              :   /* cfi->dim[i].lower_bound = gfc->dim[i].lbound */
    8030          216 :   gfc_add_modify (&loop_body, gfc_get_cfi_dim_lbound (cfi, idx),
    8031              :                   gfc_conv_descriptor_lbound_get (gfc_desc, idx));
    8032              :   /* cfi->dim[i].extent = gfc->dim[i].ubound - gfc->dim[i].lbound + 1.  */
    8033          216 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    8034              :                              gfc_conv_descriptor_ubound_get (gfc_desc, idx),
    8035              :                              gfc_conv_descriptor_lbound_get (gfc_desc, idx));
    8036          216 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type, tmp,
    8037              :                          gfc_index_one_node);
    8038          216 :   gfc_add_modify (&loop_body, gfc_get_cfi_dim_extent (cfi, idx), tmp);
    8039              :   /* d->dim[n].sm = gfc->dim[i].stride  * gfc->span); */
    8040          216 :   tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    8041              :                              gfc_conv_descriptor_stride_get (gfc_desc, idx),
    8042              :                              gfc_conv_descriptor_span_get (gfc_desc));
    8043          216 :   gfc_add_modify (&loop_body, gfc_get_cfi_dim_sm (cfi, idx), tmp);
    8044              : 
    8045              :   /* Generate loop.  */
    8046          432 :   gfc_simple_for_loop (&block2, idx, build_zero_cst (TREE_TYPE (idx)),
    8047          216 :                        rank, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
    8048              :                        gfc_finish_block (&loop_body));
    8049              :   /* if (gfc->data != NULL) { block2 }.  */
    8050          216 :   tmp = gfc_get_cfi_desc_base_addr (cfi),
    8051          216 :   tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
    8052              :                          tmp, null_pointer_node);
    8053          216 :   tmp = build3_v (COND_EXPR, tmp, gfc_finish_block (&block2),
    8054              :                   build_empty_stmt (input_location));
    8055          216 :   gfc_add_expr_to_block (&block, tmp);
    8056              : 
    8057          835 : done_finally:
    8058              :   /* If optional arg: 'if (arg) { block } else { local_arg = NULL; }'.  */
    8059          835 :   if (sym->attr.optional)
    8060              :     {
    8061          180 :       tree present = fold_build2_loc (input_location, NE_EXPR,
    8062              :                                       boolean_type_node, cfi_desc,
    8063              :                                       null_pointer_node);
    8064          180 :       tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block),
    8065              :                       build_empty_stmt (input_location));
    8066          180 :       gfc_add_expr_to_block (finally, tmp);
    8067              :      }
    8068              :    else
    8069          655 :      gfc_add_block_to_block (finally, &block);
    8070              : }
    8071              : 
    8072              : 
    8073              : static void
    8074          233 : emit_not_set_warning (gfc_symbol *sym)
    8075              : {
    8076          233 :   if (warn_return_type > 0 && sym == sym->result)
    8077           17 :     gfc_warning (OPT_Wreturn_type,
    8078              :                  "Return value of function %qs at %L not set",
    8079              :                  sym->name, &sym->declared_at);
    8080          233 :   if (warn_return_type > 0)
    8081           22 :     suppress_warning (sym->backend_decl);
    8082          233 : }
    8083              : 
    8084              : 
    8085              : /* Generate code for a function.  */
    8086              : 
    8087              : void
    8088        84658 : gfc_generate_function_code (gfc_namespace * ns)
    8089              : {
    8090        84658 :   tree fndecl;
    8091        84658 :   tree old_context;
    8092        84658 :   tree decl;
    8093        84658 :   tree tmp;
    8094        84658 :   tree fpstate = NULL_TREE;
    8095        84658 :   stmtblock_t init, cleanup, outer_block;
    8096        84658 :   stmtblock_t body;
    8097        84658 :   gfc_wrapped_block try_block;
    8098        84658 :   tree recurcheckvar = NULL_TREE;
    8099        84658 :   gfc_symbol *sym;
    8100        84658 :   gfc_symbol *previous_procedure_symbol;
    8101        84658 :   int rank, ieee;
    8102        84658 :   bool is_recursive;
    8103              : 
    8104        84658 :   sym = ns->proc_name;
    8105        84658 :   previous_procedure_symbol = current_procedure_symbol;
    8106        84658 :   current_procedure_symbol = sym;
    8107              : 
    8108              :   /* Initialize sym->tlink so that gfc_trans_deferred_vars does not get
    8109              :      lost or worse.  */
    8110        84658 :   sym->tlink = sym;
    8111              : 
    8112              :   /* Create the declaration for functions with global scope.  */
    8113        84658 :   if (!sym->backend_decl)
    8114        35286 :     gfc_create_function_decl (ns, false);
    8115              : 
    8116        84658 :   fndecl = sym->backend_decl;
    8117        84658 :   old_context = current_function_decl;
    8118              : 
    8119        84658 :   if (old_context)
    8120              :     {
    8121        22965 :       push_function_context ();
    8122        22965 :       saved_parent_function_decls = saved_function_decls;
    8123        22965 :       saved_function_decls = NULL_TREE;
    8124              :     }
    8125              : 
    8126        84658 :   trans_function_start (sym);
    8127        84658 :   gfc_current_locus = sym->declared_at;
    8128              : 
    8129        84658 :   gfc_init_block (&init);
    8130        84658 :   gfc_init_block (&cleanup);
    8131        84658 :   gfc_init_block (&outer_block);
    8132              : 
    8133        84658 :   if (ns->entries && ns->proc_name->ts.type == BT_CHARACTER)
    8134              :     {
    8135              :       /* Copy length backend_decls to all entry point result
    8136              :          symbols.  */
    8137           74 :       gfc_entry_list *el;
    8138           74 :       tree backend_decl;
    8139              : 
    8140           74 :       gfc_conv_const_charlen (ns->proc_name->ts.u.cl);
    8141           74 :       backend_decl = ns->proc_name->result->ts.u.cl->backend_decl;
    8142          258 :       for (el = ns->entries; el; el = el->next)
    8143          184 :         el->sym->result->ts.u.cl->backend_decl = backend_decl;
    8144              :     }
    8145              : 
    8146              :   /* Translate COMMON blocks.  */
    8147        84658 :   gfc_trans_common (ns);
    8148              : 
    8149              :   /* Null the parent fake result declaration if this namespace is
    8150              :      a module function or an external procedures.  */
    8151        84658 :   if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
    8152        59026 :         || ns->parent == NULL)
    8153        61693 :     parent_fake_result_decl = NULL_TREE;
    8154              : 
    8155              :   /* For BIND(C):
    8156              :      - deallocate intent-out allocatable dummy arguments.
    8157              :      - Create GFC variable which will later be populated by convert_CFI_desc  */
    8158        84658 :   if (sym->attr.is_bind_c)
    8159         1899 :     for (gfc_formal_arglist *formal = gfc_sym_get_dummy_args (sym);
    8160         5917 :          formal; formal = formal->next)
    8161              :       {
    8162         4018 :         gfc_symbol *fsym = formal->sym;
    8163         4018 :         if (!is_CFI_desc (fsym, NULL))
    8164         2199 :           continue;
    8165         1819 :         if (!fsym->attr.referenced)
    8166              :           {
    8167           72 :             gfc_conv_cfi_to_gfc (&init, &cleanup, fsym->backend_decl,
    8168              :                                  NULL_TREE, fsym);
    8169           72 :             continue;
    8170              :           }
    8171              :         /* Let's now create a local GFI descriptor. Afterwards:
    8172              :            desc is the local descriptor,
    8173              :            desc_p is a pointer to it
    8174              :              and stored in sym->backend_decl
    8175              :            GFC_DECL_SAVED_DESCRIPTOR (desc_p) contains the CFI descriptor
    8176              :              -> PARM_DECL and before sym->backend_decl.
    8177              :            For scalars, decl == decl_p is a pointer variable.  */
    8178         1747 :         tree desc_p, desc;
    8179         1747 :         location_t loc = gfc_get_location (&sym->declared_at);
    8180         1747 :         if (fsym->ts.type == BT_CHARACTER && !fsym->ts.u.cl->length)
    8181          647 :           fsym->ts.u.cl->backend_decl = gfc_create_var (gfc_array_index_type,
    8182              :                                                         fsym->name);
    8183         1100 :         else if (fsym->ts.type == BT_CHARACTER && !fsym->ts.u.cl->backend_decl)
    8184              :           {
    8185          157 :             gfc_se se;
    8186          157 :             gfc_init_se (&se, NULL );
    8187          157 :             gfc_conv_expr (&se, fsym->ts.u.cl->length);
    8188          157 :             gfc_add_block_to_block (&init, &se.pre);
    8189          157 :             fsym->ts.u.cl->backend_decl = se.expr;
    8190          157 :             gcc_assert(se.post.head == NULL_TREE);
    8191              :           }
    8192              :         /* Nullify, otherwise gfc_sym_type will return the CFI type.  */
    8193         1747 :         tree tmp = fsym->backend_decl;
    8194         1747 :         fsym->backend_decl = NULL;
    8195         1747 :         tree type = gfc_sym_type (fsym);
    8196         1747 :         gcc_assert (POINTER_TYPE_P (type));
    8197         1747 :         if (POINTER_TYPE_P (TREE_TYPE (type)))
    8198              :           /* For instance, allocatable scalars.  */
    8199          105 :           type = TREE_TYPE (type);
    8200         1747 :         if (TREE_CODE (type) == REFERENCE_TYPE)
    8201         1179 :           type = build_pointer_type (TREE_TYPE (type));
    8202         1747 :         desc_p = build_decl (loc, VAR_DECL, get_identifier (fsym->name), type);
    8203         1747 :         if (!fsym->attr.dimension)
    8204              :           desc = desc_p;
    8205         1583 :         else if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (desc_p))))
    8206              :           {
    8207              :             /* Character(len=*) explicit-size/assumed-size array. */
    8208          254 :             desc = desc_p;
    8209          254 :             gfc_build_qualified_array (desc, fsym);
    8210              :           }
    8211              :         else
    8212              :           {
    8213         1329 :             tree size = size_in_bytes (TREE_TYPE (TREE_TYPE (desc_p)));
    8214         1329 :             tree call = builtin_decl_explicit (BUILT_IN_ALLOCA);
    8215         1329 :             call = build_call_expr_loc (input_location, call, 1, size);
    8216         1329 :             gfc_add_modify (&outer_block, desc_p,
    8217         1329 :                             fold_convert (TREE_TYPE(desc_p), call));
    8218         1329 :             desc = build_fold_indirect_ref_loc (input_location, desc_p);
    8219              :           }
    8220         1747 :         pushdecl (desc_p);
    8221         1747 :         if (fsym->attr.optional)
    8222              :           {
    8223          311 :             gfc_allocate_lang_decl (desc_p);
    8224          311 :             GFC_DECL_OPTIONAL_ARGUMENT (desc_p) = 1;
    8225              :           }
    8226         1747 :         fsym->backend_decl = desc_p;
    8227         1747 :         gfc_conv_cfi_to_gfc (&init, &cleanup, tmp, desc, fsym);
    8228              :       }
    8229              : 
    8230              :   /* For OpenMP, ensure that declare variant in INTERFACE is is processed
    8231              :      especially as some late diagnostic is only done on tree level.  */
    8232        84658 :   if (flag_openmp)
    8233         8658 :     gfc_traverse_ns (ns, gfc_handle_omp_declare_variant);
    8234              : 
    8235        84658 :   gfc_generate_contained_functions (ns);
    8236              : 
    8237        84658 :   has_coarray_vars_or_accessors = caf_accessor_head != NULL;
    8238        84658 :   generate_local_vars (ns);
    8239              : 
    8240        84658 :   if (flag_coarray == GFC_FCOARRAY_LIB && has_coarray_vars_or_accessors)
    8241          400 :     generate_coarray_init (ns);
    8242              : 
    8243              :   /* Keep the parent fake result declaration in module functions
    8244              :      or external procedures.  */
    8245        84658 :   if ((ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
    8246        59026 :         || ns->parent == NULL)
    8247        61693 :     current_fake_result_decl = parent_fake_result_decl;
    8248              :   else
    8249        22965 :     current_fake_result_decl = NULL_TREE;
    8250              : 
    8251       169316 :   is_recursive = sym->attr.recursive
    8252        84658 :                  || (sym->attr.entry_master
    8253          667 :                      && sym->ns->entries->sym->attr.recursive);
    8254        84658 :   if ((gfc_option.rtcheck & GFC_RTCHECK_RECURSION)
    8255         1009 :       && !is_recursive && !flag_recursive && !sym->attr.artificial)
    8256              :     {
    8257          840 :       char * msg;
    8258              : 
    8259          840 :       msg = xasprintf ("Recursive call to nonrecursive procedure '%s'",
    8260              :                        sym->name);
    8261          840 :       recurcheckvar = gfc_create_var (logical_type_node, "is_recursive");
    8262          840 :       TREE_STATIC (recurcheckvar) = 1;
    8263          840 :       DECL_INITIAL (recurcheckvar) = logical_false_node;
    8264          840 :       gfc_add_expr_to_block (&init, recurcheckvar);
    8265          840 :       gfc_trans_runtime_check (true, false, recurcheckvar, &init,
    8266              :                                &sym->declared_at, msg);
    8267          840 :       gfc_add_modify (&init, recurcheckvar, logical_true_node);
    8268          840 :       free (msg);
    8269              :     }
    8270              : 
    8271              :   /* Check if an IEEE module is used in the procedure.  If so, save
    8272              :      the floating point state.  */
    8273        84658 :   ieee = is_ieee_module_used (ns);
    8274        84658 :   if (ieee)
    8275          443 :     fpstate = gfc_save_fp_state (&init);
    8276              : 
    8277              :   /* Now generate the code for the body of this function.  */
    8278        84658 :   gfc_init_block (&body);
    8279              : 
    8280        84658 :   if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node
    8281        84658 :         && sym->attr.subroutine)
    8282              :     {
    8283           42 :       tree alternate_return;
    8284           42 :       alternate_return = gfc_get_fake_result_decl (sym, 0);
    8285           42 :       gfc_add_modify (&body, alternate_return, integer_zero_node);
    8286              :     }
    8287              : 
    8288        84658 :   if (ns->entries)
    8289              :     {
    8290              :       /* Jump to the correct entry point.  */
    8291          667 :       tmp = gfc_trans_entry_master_switch (ns->entries);
    8292          667 :       gfc_add_expr_to_block (&body, tmp);
    8293              :     }
    8294              : 
    8295              :   /* If bounds-checking is enabled, generate code to check passed in actual
    8296              :      arguments against the expected dummy argument attributes (e.g. string
    8297              :      lengths).  */
    8298        84658 :   if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && !sym->attr.is_bind_c)
    8299         2869 :     add_argument_checking (&body, sym);
    8300              : 
    8301        84658 :   finish_oacc_declare (ns, sym, false);
    8302              : 
    8303        84658 :   tmp = gfc_trans_code (ns->code);
    8304        84658 :   gfc_add_expr_to_block (&body, tmp);
    8305              : 
    8306              :   /* This permits the return value to be correctly initialized, even when the
    8307              :      function result was not referenced.  */
    8308        84658 :   if (sym->abr_modproc_decl
    8309          247 :       && IS_PDT (sym)
    8310            7 :       && !sym->attr.allocatable
    8311            7 :       && sym->result == sym
    8312        84665 :       && get_proc_result (sym) == NULL_TREE)
    8313              :     {
    8314            1 :       gfc_get_fake_result_decl (sym->result, 0);
    8315              :       /* TODO: move to the appropriate place in resolve.cc.  */
    8316            1 :       emit_not_set_warning (sym);
    8317              :     }
    8318              : 
    8319        84658 :   if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node
    8320        84658 :       || (sym->result && sym->result != sym
    8321         1182 :           && sym->result->ts.type == BT_DERIVED
    8322          167 :           && sym->result->ts.u.derived->attr.alloc_comp))
    8323              :     {
    8324        12068 :       bool artificial_result_decl = false;
    8325        12068 :       tree result = get_proc_result (sym);
    8326        12068 :       gfc_symbol *rsym = sym == sym->result ? sym : sym->result;
    8327              : 
    8328              :       /* Make sure that a function returning an object with
    8329              :          alloc/pointer_components always has a result, where at least
    8330              :          the allocatable/pointer components are set to zero.  */
    8331        12068 :       if (result == NULL_TREE && sym->attr.function
    8332          232 :           && ((sym->result->ts.type == BT_DERIVED
    8333           28 :                && (sym->attr.allocatable
    8334           22 :                    || sym->attr.pointer
    8335           20 :                    || sym->result->ts.u.derived->attr.alloc_comp
    8336           20 :                    || sym->result->ts.u.derived->attr.pointer_comp))
    8337          224 :               || (sym->result->ts.type == BT_CLASS
    8338           38 :                   && (CLASS_DATA (sym->result)->attr.allocatable
    8339           11 :                       || CLASS_DATA (sym->result)->attr.class_pointer
    8340            0 :                       || CLASS_DATA (sym->result)->attr.alloc_comp
    8341            0 :                       || CLASS_DATA (sym->result)->attr.pointer_comp))))
    8342              :         {
    8343           46 :           artificial_result_decl = true;
    8344           46 :           result = gfc_get_fake_result_decl (sym->result, 0);
    8345              :         }
    8346              : 
    8347        12068 :       if (result != NULL_TREE && sym->attr.function && !sym->attr.pointer)
    8348              :         {
    8349        11452 :           if (sym->attr.allocatable && sym->attr.dimension == 0
    8350          101 :               && sym->result == sym)
    8351           75 :             gfc_add_modify (&init, result, fold_convert (TREE_TYPE (result),
    8352              :                                                          null_pointer_node));
    8353        11377 :           else if (sym->ts.type == BT_CLASS
    8354          733 :                    && CLASS_DATA (sym)->attr.allocatable
    8355          503 :                    && CLASS_DATA (sym)->attr.dimension == 0
    8356          316 :                    && sym->result == sym)
    8357              :             {
    8358          135 :               tmp = gfc_class_data_get (result);
    8359          135 :               gfc_add_modify (&init, tmp,
    8360          135 :                               fold_convert (TREE_TYPE (tmp),
    8361              :                                             null_pointer_node));
    8362          135 :               gfc_reset_vptr (&init, nullptr, result,
    8363          135 :                               sym->result->ts.u.derived);
    8364              :             }
    8365        11242 :           else if (sym->ts.type == BT_DERIVED
    8366         1243 :                    && !sym->attr.allocatable)
    8367              :             {
    8368         1218 :               gfc_expr *init_exp;
    8369              :               /* Arrays are not initialized using the default initializer of
    8370              :                  their elements.  Therefore only check if a default
    8371              :                  initializer is available when the result is scalar.  */
    8372         1218 :               init_exp = rsym->as ? NULL
    8373         1185 :                                   : gfc_generate_initializer (&rsym->ts, true);
    8374         1185 :               if (init_exp)
    8375              :                 {
    8376          642 :                   tmp = gfc_trans_structure_assign (result, init_exp, 0);
    8377          642 :                   gfc_free_expr (init_exp);
    8378          642 :                   gfc_add_expr_to_block (&init, tmp);
    8379              :                 }
    8380              : 
    8381         1218 :               if (rsym->ts.u.derived->attr.alloc_comp)
    8382              :                 {
    8383          445 :                   rank = rsym->as ? rsym->as->rank : 0;
    8384          445 :                   tmp = gfc_nullify_alloc_comp (rsym->ts.u.derived, result,
    8385              :                                                 rank);
    8386          445 :                   gfc_prepend_expr_to_block (&body, tmp);
    8387              :                 }
    8388              :             }
    8389              :         }
    8390              : 
    8391        12068 :       if (result == NULL_TREE || artificial_result_decl)
    8392              :         /* TODO: move to the appropriate place in resolve.cc.  */
    8393          232 :         emit_not_set_warning (sym);
    8394              : 
    8395          232 :       if (result != NULL_TREE)
    8396        11882 :         gfc_add_expr_to_block (&body, gfc_generate_return ());
    8397              :     }
    8398              : 
    8399              :   /* Reset recursion-check variable.  */
    8400        84658 :   if (recurcheckvar != NULL_TREE)
    8401              :     {
    8402          840 :       gfc_add_modify (&cleanup, recurcheckvar, logical_false_node);
    8403          840 :       recurcheckvar = NULL;
    8404              :     }
    8405              : 
    8406              :   /* If IEEE modules are loaded, restore the floating-point state.  */
    8407        84658 :   if (ieee)
    8408          443 :     gfc_restore_fp_state (&cleanup, fpstate);
    8409              : 
    8410              :   /* Finish the function body and add init and cleanup code.  */
    8411        84658 :   tmp = gfc_finish_block (&body);
    8412              :   /* Add code to create and cleanup arrays.  */
    8413        84658 :   gfc_start_wrapped_block (&try_block, tmp);
    8414        84658 :   gfc_trans_deferred_vars (sym, &try_block);
    8415        84658 :   gfc_add_init_cleanup (&try_block, gfc_finish_block (&init),
    8416              :                         gfc_finish_block (&cleanup));
    8417              : 
    8418              :   /* Add all the decls we created during processing.  */
    8419        84658 :   decl = nreverse (saved_function_decls);
    8420       462805 :   while (decl)
    8421              :     {
    8422       293489 :       tree next;
    8423              : 
    8424       293489 :       next = DECL_CHAIN (decl);
    8425       293489 :       DECL_CHAIN (decl) = NULL_TREE;
    8426       293489 :       pushdecl (decl);
    8427       293489 :       decl = next;
    8428              :     }
    8429        84658 :   saved_function_decls = NULL_TREE;
    8430              : 
    8431        84658 :   gfc_add_expr_to_block (&outer_block, gfc_finish_wrapped_block (&try_block));
    8432        84658 :   DECL_SAVED_TREE (fndecl) = gfc_finish_block (&outer_block);
    8433        84658 :   decl = getdecls ();
    8434              : 
    8435              :   /* Finish off this function and send it for code generation.  */
    8436        84658 :   poplevel (1, 1);
    8437        84658 :   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
    8438              : 
    8439       169316 :   DECL_SAVED_TREE (fndecl)
    8440       169316 :     = fold_build3_loc (DECL_SOURCE_LOCATION (fndecl), BIND_EXPR, void_type_node,
    8441       169316 :                        decl, DECL_SAVED_TREE (fndecl), DECL_INITIAL (fndecl));
    8442              : 
    8443              :   /* Output the GENERIC tree.  */
    8444        84658 :   dump_function (TDI_original, fndecl);
    8445              : 
    8446              :   /* Store the end of the function, so that we get good line number
    8447              :      info for the epilogue.  */
    8448        84658 :   cfun->function_end_locus = input_location;
    8449              : 
    8450              :   /* We're leaving the context of this function, so zap cfun.
    8451              :      It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
    8452              :      tree_rest_of_compilation.  */
    8453        84658 :   set_cfun (NULL);
    8454              : 
    8455        84658 :   if (old_context)
    8456              :     {
    8457        22965 :       pop_function_context ();
    8458        22965 :       saved_function_decls = saved_parent_function_decls;
    8459              :     }
    8460        84658 :   current_function_decl = old_context;
    8461              : 
    8462        84658 :   if (decl_function_context (fndecl))
    8463              :     {
    8464              :       /* Register this function with cgraph just far enough to get it
    8465              :          added to our parent's nested function list.
    8466              :          If there are static coarrays in this function, the nested _caf_init
    8467              :          function has already called cgraph_create_node, which also created
    8468              :          the cgraph node for this function.  */
    8469        22965 :       if (!has_coarray_vars_or_accessors || flag_coarray != GFC_FCOARRAY_LIB)
    8470        22763 :         (void) cgraph_node::get_create (fndecl);
    8471              :     }
    8472              :   else
    8473        61693 :     cgraph_node::finalize_function (fndecl, true);
    8474              : 
    8475        84658 :   gfc_trans_use_stmts (ns);
    8476        84658 :   gfc_traverse_ns (ns, gfc_emit_parameter_debug_info);
    8477              : 
    8478        84658 :   if (sym->attr.is_main_program)
    8479        26136 :     create_main_function (fndecl);
    8480              : 
    8481        84658 :   current_procedure_symbol = previous_procedure_symbol;
    8482        84658 : }
    8483              : 
    8484              : 
    8485              : void
    8486        31413 : gfc_generate_constructors (void)
    8487              : {
    8488        31413 :   gcc_assert (gfc_static_ctors == NULL_TREE);
    8489              : #if 0
    8490              :   tree fnname;
    8491              :   tree type;
    8492              :   tree fndecl;
    8493              :   tree decl;
    8494              :   tree tmp;
    8495              : 
    8496              :   if (gfc_static_ctors == NULL_TREE)
    8497              :     return;
    8498              : 
    8499              :   fnname = get_file_function_name ("I");
    8500              :   type = build_function_type_list (void_type_node, NULL_TREE);
    8501              : 
    8502              :   fndecl = build_decl (input_location,
    8503              :                        FUNCTION_DECL, fnname, type);
    8504              :   TREE_PUBLIC (fndecl) = 1;
    8505              : 
    8506              :   decl = build_decl (input_location,
    8507              :                      RESULT_DECL, NULL_TREE, void_type_node);
    8508              :   DECL_ARTIFICIAL (decl) = 1;
    8509              :   DECL_IGNORED_P (decl) = 1;
    8510              :   DECL_CONTEXT (decl) = fndecl;
    8511              :   DECL_RESULT (fndecl) = decl;
    8512              : 
    8513              :   pushdecl (fndecl);
    8514              : 
    8515              :   current_function_decl = fndecl;
    8516              : 
    8517              :   rest_of_decl_compilation (fndecl, 1, 0);
    8518              : 
    8519              :   make_decl_rtl (fndecl);
    8520              : 
    8521              :   allocate_struct_function (fndecl, false);
    8522              : 
    8523              :   pushlevel ();
    8524              : 
    8525              :   for (; gfc_static_ctors; gfc_static_ctors = TREE_CHAIN (gfc_static_ctors))
    8526              :     {
    8527              :       tmp = build_call_expr_loc (input_location,
    8528              :                              TREE_VALUE (gfc_static_ctors), 0);
    8529              :       DECL_SAVED_TREE (fndecl) = build_stmt (input_location, EXPR_STMT, tmp);
    8530              :     }
    8531              : 
    8532              :   decl = getdecls ();
    8533              :   poplevel (1, 1);
    8534              : 
    8535              :   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
    8536              :   DECL_SAVED_TREE (fndecl)
    8537              :     = build3_v (BIND_EXPR, decl, DECL_SAVED_TREE (fndecl),
    8538              :                 DECL_INITIAL (fndecl));
    8539              : 
    8540              :   free_after_parsing (cfun);
    8541              :   free_after_compilation (cfun);
    8542              : 
    8543              :   tree_rest_of_compilation (fndecl);
    8544              : 
    8545              :   current_function_decl = NULL_TREE;
    8546              : #endif
    8547        31413 : }
    8548              : 
    8549              : 
    8550              : /* Helper function for checking of variables declared in a BLOCK DATA program
    8551              :    unit.  */
    8552              : 
    8553              : static void
    8554          300 : check_block_data_decls (gfc_symbol * sym)
    8555              : {
    8556          300 :   if (warn_unused_variable
    8557           13 :       && sym->attr.flavor == FL_VARIABLE
    8558            7 :       && !sym->attr.in_common
    8559            3 :       && !sym->attr.artificial)
    8560              :     {
    8561            2 :       gfc_warning (OPT_Wunused_variable,
    8562              :                    "Symbol %qs at %L is declared in a BLOCK DATA "
    8563              :                    "program unit but is not in a COMMON block",
    8564              :                    sym->name, &sym->declared_at);
    8565              :     }
    8566          300 : }
    8567              : 
    8568              : 
    8569              : /* Translates a BLOCK DATA program unit. This means emitting the
    8570              :    commons contained therein plus their initializations. We also emit
    8571              :    a globally visible symbol to make sure that each BLOCK DATA program
    8572              :    unit remains unique.  */
    8573              : 
    8574              : void
    8575           72 : gfc_generate_block_data (gfc_namespace * ns)
    8576              : {
    8577           72 :   tree decl;
    8578           72 :   tree id;
    8579              : 
    8580              :   /* Tell the backend the source location of the block data.  */
    8581           72 :   if (ns->proc_name)
    8582           29 :     input_location = gfc_get_location (&ns->proc_name->declared_at);
    8583              :   else
    8584           43 :     input_location = gfc_get_location (&gfc_current_locus);
    8585              : 
    8586              :   /* Process the DATA statements.  */
    8587           72 :   gfc_trans_common (ns);
    8588              : 
    8589              :   /* Check for variables declared in BLOCK DATA but not used in COMMON.  */
    8590           72 :   gfc_traverse_ns (ns, check_block_data_decls);
    8591              : 
    8592              :   /* Create a global symbol with the mane of the block data.  This is to
    8593              :      generate linker errors if the same name is used twice.  It is never
    8594              :      really used.  */
    8595           72 :   if (ns->proc_name)
    8596           29 :     id = gfc_sym_mangled_function_id (ns->proc_name);
    8597              :   else
    8598           43 :     id = get_identifier ("__BLOCK_DATA__");
    8599              : 
    8600           72 :   decl = build_decl (input_location,
    8601              :                      VAR_DECL, id, gfc_array_index_type);
    8602           72 :   TREE_PUBLIC (decl) = 1;
    8603           72 :   TREE_STATIC (decl) = 1;
    8604           72 :   DECL_IGNORED_P (decl) = 1;
    8605              : 
    8606           72 :   pushdecl (decl);
    8607           72 :   rest_of_decl_compilation (decl, 1, 0);
    8608           72 : }
    8609              : 
    8610              : void
    8611        14000 : gfc_start_saved_local_decls ()
    8612              : {
    8613        14000 :   gcc_checking_assert (current_function_decl != NULL_TREE);
    8614        14000 :   saved_local_decls = NULL_TREE;
    8615        14000 : }
    8616              : 
    8617              : void
    8618        14000 : gfc_stop_saved_local_decls ()
    8619              : {
    8620        14000 :   tree decl = nreverse (saved_local_decls);
    8621        40965 :   while (decl)
    8622              :     {
    8623        12965 :       tree next;
    8624              : 
    8625        12965 :       next = DECL_CHAIN (decl);
    8626        12965 :       DECL_CHAIN (decl) = NULL_TREE;
    8627        12965 :       pushdecl (decl);
    8628        12965 :       decl = next;
    8629              :     }
    8630        14000 :   saved_local_decls = NULL_TREE;
    8631        14000 : }
    8632              : 
    8633              : /* Process the local variables of a BLOCK construct.  */
    8634              : 
    8635              : void
    8636        13952 : gfc_process_block_locals (gfc_namespace* ns)
    8637              : {
    8638        13952 :   gfc_start_saved_local_decls ();
    8639        13952 :   has_coarray_vars_or_accessors = caf_accessor_head != NULL;
    8640              : 
    8641        13952 :   generate_local_vars (ns);
    8642              : 
    8643        13952 :   if (flag_coarray == GFC_FCOARRAY_LIB && has_coarray_vars_or_accessors)
    8644           41 :     generate_coarray_init (ns);
    8645        13952 :   gfc_stop_saved_local_decls ();
    8646        13952 : }
    8647              : 
    8648              : 
    8649              : #include "gt-fortran-trans-decl.h"
        

Generated by: LCOV version 2.4-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.