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

Generated by: LCOV version 2.6-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.