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

Generated by: LCOV version 2.4-beta

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