LCOV - code coverage report
Current view: top level - gcc/fortran - trans-decl.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 96.2 % 4133 3978
Test Date: 2026-05-11 19:44:49 Functions: 100.0 % 96 96
Legend: Lines:     hit not hit

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