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