LCOV - code coverage report
Current view: top level - gcc/cp - cp-lang.cc (source / functions) Hit Total Coverage
Test: gcc.info Lines: 56 58 96.6 %
Date: 2023-07-19 08:18:47 Functions: 12 13 92.3 %

          Line data    Source code
       1             : /* Language-dependent hooks for C++.
       2             :    Copyright (C) 2001-2023 Free Software Foundation, Inc.
       3             :    Contributed by Alexandre Oliva  <aoliva@redhat.com>
       4             : 
       5             : This file is part of GCC.
       6             : 
       7             : GCC is free software; you can redistribute it and/or modify
       8             : it under the terms of the GNU General Public License as published by
       9             : the Free Software Foundation; either version 3, or (at your option)
      10             : any later version.
      11             : 
      12             : GCC is distributed in the hope that it will be useful,
      13             : but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             : GNU General Public License 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             : #include "config.h"
      22             : #include "system.h"
      23             : #include "coretypes.h"
      24             : #include "cp-tree.h"
      25             : #include "stor-layout.h"
      26             : #include "langhooks.h"
      27             : #include "langhooks-def.h"
      28             : #include "cp-objcp-common.h"
      29             : 
      30             : enum c_language_kind c_language = clk_cxx;
      31             : static const char * cxx_dwarf_name (tree t, int verbosity);
      32             : static enum classify_record cp_classify_record (tree type);
      33             : static tree cp_eh_personality (void);
      34             : static tree get_template_innermost_arguments_folded (const_tree);
      35             : static tree get_template_argument_pack_elems_folded (const_tree);
      36             : static tree cxx_enum_underlying_base_type (const_tree);
      37             : static tree *cxx_omp_get_decl_init (tree);
      38             : static void cxx_omp_finish_decl_inits (void);
      39             : static const char *cp_get_sarif_source_language (const char *);
      40             : 
      41             : /* Lang hooks common to C++ and ObjC++ are declared in cp/cp-objcp-common.h;
      42             :    consequently, there should be very few hooks below.  */
      43             : 
      44             : #undef LANG_HOOKS_NAME
      45             : #define LANG_HOOKS_NAME "GNU C++"
      46             : #undef LANG_HOOKS_INIT
      47             : #define LANG_HOOKS_INIT cxx_init
      48             : #undef LANG_HOOKS_CLASSIFY_RECORD
      49             : #define LANG_HOOKS_CLASSIFY_RECORD cp_classify_record
      50             : #undef LANG_HOOKS_GENERIC_TYPE_P
      51             : #define LANG_HOOKS_GENERIC_TYPE_P class_tmpl_impl_spec_p
      52             : 
      53             : #undef LANG_HOOKS_GET_INNERMOST_GENERIC_PARMS
      54             : #define LANG_HOOKS_GET_INNERMOST_GENERIC_PARMS \
      55             :         get_primary_template_innermost_parameters
      56             : #undef LANG_HOOKS_GET_INNERMOST_GENERIC_ARGS
      57             : #define LANG_HOOKS_GET_INNERMOST_GENERIC_ARGS \
      58             :         get_template_innermost_arguments_folded
      59             : #undef LANG_HOOKS_FUNCTION_PARAMETER_PACK_P
      60             : #define LANG_HOOKS_FUNCTION_PARAMETER_PACK_P \
      61             :         function_parameter_pack_p
      62             : #undef LANG_HOOKS_GET_ARGUMENT_PACK_ELEMS
      63             : #define LANG_HOOKS_GET_ARGUMENT_PACK_ELEMS \
      64             :         get_template_argument_pack_elems_folded
      65             : #undef LANG_HOOKS_GENERIC_GENERIC_PARAMETER_DECL_P
      66             : #define LANG_HOOKS_GENERIC_GENERIC_PARAMETER_DECL_P \
      67             :         template_template_parameter_p
      68             : #undef LANG_HOOKS_FUNCTION_PARM_EXPANDED_FROM_PACK_P
      69             : #define LANG_HOOKS_FUNCTION_PARM_EXPANDED_FROM_PACK_P \
      70             :         function_parameter_expanded_from_pack_p
      71             : #undef LANG_HOOKS_GET_GENERIC_FUNCTION_DECL
      72             : #define LANG_HOOKS_GET_GENERIC_FUNCTION_DECL get_function_template_decl
      73             : #undef LANG_HOOKS_DWARF_NAME
      74             : #define LANG_HOOKS_DWARF_NAME cxx_dwarf_name
      75             : #undef LANG_HOOKS_INIT_TS
      76             : #define LANG_HOOKS_INIT_TS cp_common_init_ts
      77             : #undef LANG_HOOKS_EH_PERSONALITY
      78             : #define LANG_HOOKS_EH_PERSONALITY cp_eh_personality
      79             : #undef LANG_HOOKS_EH_RUNTIME_TYPE
      80             : #define LANG_HOOKS_EH_RUNTIME_TYPE build_eh_type_type
      81             : #undef LANG_HOOKS_ENUM_UNDERLYING_BASE_TYPE
      82             : #define LANG_HOOKS_ENUM_UNDERLYING_BASE_TYPE cxx_enum_underlying_base_type
      83             : #undef LANG_HOOKS_PREPROCESS_MAIN_FILE
      84             : #define LANG_HOOKS_PREPROCESS_MAIN_FILE module_begin_main_file
      85             : #undef LANG_HOOKS_PREPROCESS_OPTIONS
      86             : #define LANG_HOOKS_PREPROCESS_OPTIONS module_preprocess_options
      87             : #undef LANG_HOOKS_PREPROCESS_TOKEN
      88             : #define LANG_HOOKS_PREPROCESS_TOKEN module_token_pre
      89             : 
      90             : #if CHECKING_P
      91             : #undef LANG_HOOKS_RUN_LANG_SELFTESTS
      92             : #define LANG_HOOKS_RUN_LANG_SELFTESTS selftest::run_cp_tests
      93             : #endif /* #if CHECKING_P */
      94             : 
      95             : #undef LANG_HOOKS_GET_SUBSTRING_LOCATION
      96             : #define LANG_HOOKS_GET_SUBSTRING_LOCATION c_get_substring_location
      97             : 
      98             : #undef LANG_HOOKS_OMP_GET_DECL_INIT
      99             : #define LANG_HOOKS_OMP_GET_DECL_INIT cxx_omp_get_decl_init
     100             : 
     101             : #undef LANG_HOOKS_OMP_FINISH_DECL_INITS
     102             : #define LANG_HOOKS_OMP_FINISH_DECL_INITS cxx_omp_finish_decl_inits
     103             : 
     104             : #undef LANG_HOOKS_GET_SARIF_SOURCE_LANGUAGE
     105             : #define LANG_HOOKS_GET_SARIF_SOURCE_LANGUAGE cp_get_sarif_source_language
     106             : 
     107             : /* Each front end provides its own lang hook initializer.  */
     108             : struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
     109             : 
     110             : /* Lang hook routines common to C++ and ObjC++ appear in cp/cp-objcp-common.cc;
     111             :    there should be very few routines below.  */
     112             : 
     113             : /* The following function does something real, but only in Objective-C++.  */
     114             : 
     115             : tree
     116   282466487 : objcp_tsubst_copy_and_build (tree /*t*/,
     117             :                              tree /*args*/,
     118             :                              tsubst_flags_t /*complain*/,
     119             :                              tree /*in_decl*/)
     120             : {
     121   282466487 :   return NULL_TREE;
     122             : }
     123             : 
     124             : static const char *
     125   120349620 : cxx_dwarf_name (tree t, int verbosity)
     126             : {
     127   120349620 :   gcc_assert (DECL_P (t));
     128             : 
     129   120349620 :   if (DECL_NAME (t) && IDENTIFIER_ANON_P (DECL_NAME (t)))
     130             :     return NULL;
     131   119166806 :   if (verbosity >= 2)
     132    24710174 :     return decl_as_dwarf_string (t,
     133             :                                  TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME
     134    24710174 :                                  | TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
     135             : 
     136    94456632 :   return lang_decl_dwarf_name (t, verbosity, false);
     137             : }
     138             : 
     139             : static enum classify_record
     140    24573614 : cp_classify_record (tree type)
     141             : {
     142    24573614 :   if (TYPE_LANG_SPECIFIC (type)
     143    24573614 :       && CLASSTYPE_DECLARED_CLASS (type))
     144     2736220 :     return RECORD_IS_CLASS;
     145             : 
     146             :   return RECORD_IS_STRUCT;
     147             : }
     148             : 
     149             : static GTY(()) tree cp_eh_personality_decl;
     150             : 
     151             : static tree
     152      580965 : cp_eh_personality (void)
     153             : {
     154      580965 :   if (!cp_eh_personality_decl)
     155       25543 :     cp_eh_personality_decl = build_personality_function ("gxx");
     156             : 
     157      580965 :   return cp_eh_personality_decl;
     158             : }
     159             : 
     160             : /* This is a subroutine of fold_cplus_constants.  It returns TRUE if T
     161             :    is a C++ specific constant that needs to be folded further before
     162             :    being passed to the debug info emitter.  */
     163             : 
     164             : static bool
     165    37294996 : template_arg_needs_folding (const_tree t)
     166             : {
     167             :   /* For now only PTRMEM_CST nodes are to be folded further.  */
     168           0 :   if (TREE_CODE (t) == PTRMEM_CST)
     169           0 :     return true;
     170             :   return false;
     171             : }
     172             : 
     173             : /* Fold the elements of the TREE_VEC C which are C++ specific nodes
     174             :    that would need folding so that they can be processed by the debug
     175             :    info emitter. This is a subroutine of
     176             :    get_template_innermost_arguments_folded and
     177             :    get_template_argument_pack_elems_folded.  */
     178             : 
     179             : static tree
     180    49270932 : fold_cplus_constants (const_tree c)
     181             : {
     182    49270932 :   tree folded_elems, elems = CONST_CAST_TREE (c);
     183    49270932 :   int vec_len, i;
     184             : 
     185    49270932 :   if (elems == NULL_TREE || elems == error_mark_node)
     186             :     return elems;
     187             : 
     188    22806029 :   vec_len = TREE_VEC_LENGTH (elems);
     189             : 
     190             :   /* First check if there is at least one element that needs
     191             :      folding. If there is none, we just return ELEMS. Otherwise create
     192             :      and return a new tree vector that contains the folded versions of
     193             :      ELEMS. This is to avoid allocating memory if we don't need
     194             :      to.  */
     195    60100828 :   for (i = 0; i < vec_len; ++i)
     196             :     {
     197    37294996 :       if (template_arg_needs_folding (TREE_VEC_ELT (elems, i)))
     198             :         break;
     199             :     }
     200    22806029 :   if (i == vec_len)
     201             :     return elems;
     202             : 
     203         197 :   folded_elems = make_tree_vec (vec_len);
     204         709 :   for (i = 0; i < vec_len; ++i)
     205             :     {
     206         512 :       tree elem = TREE_VEC_ELT (elems, i);
     207         512 :       TREE_VEC_ELT (folded_elems, i) =  
     208         512 :         (elem && !TYPE_P (elem)) ? cplus_expand_constant (elem) : elem;
     209             : 
     210             :     }
     211             :   return folded_elems;
     212             : }
     213             : 
     214             : /* The C++ implementation of the LANG_HOOKS_GET_INNERMOST_GENERIC_ARGS
     215             :    hook. It returns the innermost template arguments of type T, and
     216             :    makes sure those arguments are folded enough for the debug info
     217             :    emitter.  */
     218             : 
     219             : static tree
     220    19730549 : get_template_innermost_arguments_folded (const_tree t)
     221             : {
     222    19730549 :   return fold_cplus_constants (get_template_innermost_arguments (t));
     223             : }
     224             : 
     225             : static tree
     226    29540383 : get_template_argument_pack_elems_folded (const_tree t)
     227             : {
     228    29540383 :   return fold_cplus_constants (get_template_argument_pack_elems (t));
     229             : }
     230             : 
     231             : /* The C++ version of the enum_underlying_base_type langhook.
     232             :    See also cp/semantics.cc (finish_underlying_type).  */
     233             : 
     234             : static
     235     1068824 : tree cxx_enum_underlying_base_type (const_tree type)
     236             : {
     237     1068824 :   tree underlying_type = ENUM_UNDERLYING_TYPE (type);
     238             : 
     239     1068824 :   if (! ENUM_FIXED_UNDERLYING_TYPE_P (type))
     240      886659 :     underlying_type
     241      886659 :       = c_common_type_for_mode (TYPE_MODE (underlying_type),
     242      886659 :                                 TYPE_UNSIGNED (underlying_type));
     243             : 
     244     1068824 :   return underlying_type;
     245             : }
     246             : 
     247             : /* The C++ version of the omp_get_decl_init langhook returns the static
     248             :    initializer for a variable declaration if present, otherwise it
     249             :    tries to find and return the dynamic initializer.  If not present,
     250             :    it returns NULL.  */
     251             : 
     252             : static tree *
     253       11235 : cxx_omp_get_decl_init (tree decl)
     254             : {
     255       11235 :   if (DECL_INITIAL (decl))
     256        5950 :     return &DECL_INITIAL (decl);
     257             : 
     258        5285 :   return hash_map_safe_get (dynamic_initializers, decl);
     259             : }
     260             : 
     261             : /* The C++ version of the omp_finish_decl_inits langhook allows GC to
     262             :    reclaim the memory used by the hash-map used to hold dynamic initializer
     263             :    information.  */
     264             : 
     265             : static void
     266        3423 : cxx_omp_finish_decl_inits (void)
     267             : {
     268        3423 :   dynamic_initializers = NULL;
     269        3423 : }
     270             : 
     271             : /* Get a value for the SARIF v2.1.0 "artifact.sourceLanguage" property,
     272             :    based on the list in SARIF v2.1.0 Appendix J.  */
     273             : 
     274             : static const char *
     275          36 : cp_get_sarif_source_language (const char *)
     276             : {
     277          36 :   return "cplusplus";
     278             : }
     279             : 
     280             : #if CHECKING_P
     281             : 
     282             : namespace selftest {
     283             : 
     284             : /* Implementation of LANG_HOOKS_RUN_LANG_SELFTESTS for the C++ frontend.  */
     285             : 
     286             : void
     287           1 : run_cp_tests (void)
     288             : {
     289             :   /* Run selftests shared within the C family.  */
     290           1 :   c_family_tests ();
     291             : 
     292             :   /* Additional C++-specific tests.  */
     293           1 :   cp_pt_cc_tests ();
     294           1 :   cp_tree_cc_tests ();
     295           1 : }
     296             : 
     297             : } // namespace selftest
     298             : 
     299             : #endif /* #if CHECKING_P */
     300             : 
     301             : 
     302             : #include "gt-cp-cp-lang.h"
     303             : #include "gtype-cp.h"

Generated by: LCOV version 1.16