LCOV - code coverage report
Current view: top level - gcc/fortran - trans-decl.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 96.2 % 4218 4059
Test Date: 2026-11-14 16:39:50 Functions: 100.0 % 96 96
Legend: Lines:     hit not hit

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

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