LCOV - code coverage report
Current view: top level - gcc/m2/gm2-gcc - m2decl.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 90.1 % 161 145
Test Date: 2023-09-09 13:19:57 Functions: 78.9 % 19 15
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* m2decl.cc provides an interface to GCC decl trees.
       2                 :             : 
       3                 :             : Copyright (C) 2012-2023 Free Software Foundation, Inc.
       4                 :             : Contributed by Gaius Mulley <gaius@glam.ac.uk>.
       5                 :             : 
       6                 :             : This file is part of GNU Modula-2.
       7                 :             : 
       8                 :             : GNU Modula-2 is free software; you can redistribute it and/or modify
       9                 :             : it under the terms of the GNU General Public License as published by
      10                 :             : the Free Software Foundation; either version 3, or (at your option)
      11                 :             : any later version.
      12                 :             : 
      13                 :             : GNU Modula-2 is distributed in the hope that it will be useful, but
      14                 :             : WITHOUT ANY WARRANTY; without even the implied warranty of
      15                 :             : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16                 :             : General Public License for more details.
      17                 :             : 
      18                 :             : You should have received a copy of the GNU General Public License
      19                 :             : along with GNU Modula-2; see the file COPYING3.  If not see
      20                 :             : <http://www.gnu.org/licenses/>.  */
      21                 :             : 
      22                 :             : #include "gcc-consolidation.h"
      23                 :             : 
      24                 :             : #include "../gm2-lang.h"
      25                 :             : #include "../m2-tree.h"
      26                 :             : 
      27                 :             : #define m2decl_c
      28                 :             : #include "m2assert.h"
      29                 :             : #include "m2block.h"
      30                 :             : #include "m2decl.h"
      31                 :             : #include "m2expr.h"
      32                 :             : #include "m2tree.h"
      33                 :             : #include "m2treelib.h"
      34                 :             : #include "m2type.h"
      35                 :             : #include "m2convert.h"
      36                 :             : 
      37                 :             : extern GTY (()) tree current_function_decl;
      38                 :             : 
      39                 :             : /* Used in BuildStartFunctionType.  */
      40                 :             : static GTY (()) tree param_type_list;
      41                 :             : static GTY (()) tree param_list = NULL_TREE; /* Ready for the next time we
      42                 :             :                                                 call/define a function.  */
      43                 :             : #if 0
      44                 :             : tree
      45                 :             : m2decl_DeclareM2linkStaticInitialization (location_t location,
      46                 :             :                                           int ScaffoldStatic)
      47                 :             : {
      48                 :             :   m2block_pushGlobalScope ();
      49                 :             :   /* Generate: int M2LINK_StaticInitialization = ScaffoldStatic;  */
      50                 :             :   tree init = m2decl_BuildIntegerConstant (ScaffoldStatic);
      51                 :             :   tree static_init = m2decl_DeclareKnownVariable (location, "m2pim_M2LINK_StaticInitialization",
      52                 :             :                                                   integer_type_node,
      53                 :             :                                                   TRUE, FALSE, FALSE, TRUE, NULL_TREE, init);
      54                 :             :   m2block_popGlobalScope ();
      55                 :             :   return static_init;
      56                 :             : }
      57                 :             : 
      58                 :             : 
      59                 :             : tree
      60                 :             : m2decl_DeclareM2linkForcedModuleInitOrder (location_t location,
      61                 :             :                                            const char *RuntimeOverride)
      62                 :             : {
      63                 :             :   m2block_pushGlobalScope ();
      64                 :             :   /* Generate: const char *ForcedModuleInitOrder = RuntimeOverride;  */
      65                 :             :   tree ptr_to_char = build_pointer_type (char_type_node);
      66                 :             :   TYPE_READONLY (ptr_to_char) = TRUE;
      67                 :             :   tree init = m2decl_BuildPtrToTypeString (location, RuntimeOverride, ptr_to_char);
      68                 :             :   tree forced_order = m2decl_DeclareKnownVariable (location, "m2pim_M2LINK_ForcedModuleInitOrder",
      69                 :             :                                                    ptr_to_char,
      70                 :             :                                                    TRUE, FALSE, FALSE, TRUE, NULL_TREE, init);
      71                 :             :   m2block_popGlobalScope ();
      72                 :             :   return forced_order;
      73                 :             : }
      74                 :             : #endif
      75                 :             : 
      76                 :             : 
      77                 :             : /* DeclareKnownVariable declares a variable to GCC.  */
      78                 :             : 
      79                 :             : tree
      80                 :      482910 : m2decl_DeclareKnownVariable (location_t location, const char *name, tree type,
      81                 :             :                              bool exported, bool imported, bool istemporary,
      82                 :             :                              bool isglobal, tree scope, tree initial)
      83                 :             : {
      84                 :      482910 :   tree id;
      85                 :      482910 :   tree decl;
      86                 :             : 
      87                 :      482910 :   m2assert_AssertLocation (location);
      88                 :      482910 :   ASSERT (m2tree_is_type (type), type);
      89                 :      482910 :   ASSERT_BOOL (isglobal);
      90                 :             : 
      91                 :      482910 :   id = get_identifier (name);
      92                 :      482910 :   type = m2tree_skip_type_decl (type);
      93                 :      482910 :   decl = build_decl (location, VAR_DECL, id, type);
      94                 :             : 
      95                 :      482910 :   DECL_SOURCE_LOCATION (decl) = location;
      96                 :             : 
      97                 :      482910 :   DECL_EXTERNAL (decl) = imported;
      98                 :      482910 :   TREE_STATIC (decl) = isglobal;
      99                 :      482910 :   TREE_PUBLIC (decl) = exported || imported;
     100                 :             : 
     101                 :      482910 :   gcc_assert ((istemporary == 0) || (istemporary == 1));
     102                 :             : 
     103                 :             :   /* The variable was not declared by GCC, but by the front end.  */
     104                 :      482910 :   DECL_ARTIFICIAL (decl) = istemporary;
     105                 :             :   /* If istemporary then we don't want debug info for it.  */
     106                 :      482910 :   DECL_IGNORED_P (decl) = istemporary;
     107                 :             :   /* If istemporary we don't want even the fancy names of those printed in
     108                 :             :      -fdump-final-insns= dumps.  */
     109                 :      482910 :   DECL_NAMELESS (decl) = istemporary;
     110                 :             : 
     111                 :             :   /* Make the variable writable.  */
     112                 :      482910 :   TREE_READONLY (decl) = 0;
     113                 :             : 
     114                 :      482910 :   DECL_CONTEXT (decl) = scope;
     115                 :             : 
     116                 :      482910 :   if (initial)
     117                 :           0 :     DECL_INITIAL (decl) = initial;
     118                 :             : 
     119                 :      482910 :   m2block_pushDecl (decl);
     120                 :             : 
     121                 :      482910 :   if (DECL_SIZE (decl) == 0)
     122                 :           0 :     error ("storage size of %qD has not been resolved", decl);
     123                 :             : 
     124                 :      482910 :   if ((TREE_PUBLIC (decl) == 0) && DECL_EXTERNAL (decl))
     125                 :           0 :     internal_error ("inconsistent because %qs",
     126                 :             :                     "PUBLIC_DECL(decl) == 0 && DECL_EXTERNAL(decl) == 1");
     127                 :             : 
     128                 :      482910 :   m2block_addDeclExpr (build_stmt (location, DECL_EXPR, decl));
     129                 :             : 
     130                 :      482910 :   return decl;
     131                 :             : }
     132                 :             : 
     133                 :             : /* DeclareKnownConstant - given a constant, value, of, type, create a
     134                 :             :    constant in the GCC symbol table.  Note that the name of the
     135                 :             :    constant is not used as _all_ constants are declared in the global
     136                 :             :    scope.  The front end deals with scoping rules - here we declare
     137                 :             :    all constants with no names in the global scope.  This allows
     138                 :             :    M2SubExp and constant folding routines the liberty of operating
     139                 :             :    with quadruples which all assume constants can always be
     140                 :             :    referenced.  */
     141                 :             : 
     142                 :             : tree
     143                 :       47223 : m2decl_DeclareKnownConstant (location_t location, tree type, tree value)
     144                 :             : {
     145                 :       47223 :   tree id = make_node (IDENTIFIER_NODE); /* Ignore the name of the constant. */
     146                 :       47223 :   tree decl;
     147                 :             : 
     148                 :       47223 :   m2assert_AssertLocation (location);
     149                 :       47223 :   m2expr_ConstantExpressionWarning (value);
     150                 :       47223 :   type = m2tree_skip_type_decl (type);
     151                 :       47223 :   layout_type (type);
     152                 :             : 
     153                 :       47223 :   decl = build_decl (location, CONST_DECL, id, type);
     154                 :             : 
     155                 :       47223 :   DECL_INITIAL (decl) = value;
     156                 :       47223 :   TREE_TYPE (decl) = type;
     157                 :             : 
     158                 :       47223 :   decl = m2block_global_constant (decl);
     159                 :             : 
     160                 :       47223 :   return decl;
     161                 :             : }
     162                 :             : 
     163                 :             : /* BuildParameterDeclaration - creates and returns one parameter
     164                 :             :    from, name, and, type.  It appends this parameter to the internal
     165                 :             :    param_type_list.  */
     166                 :             : 
     167                 :             : tree
     168                 :     3889256 : m2decl_BuildParameterDeclaration (location_t location, char *name, tree type,
     169                 :             :                                   bool isreference)
     170                 :             : {
     171                 :     3889256 :   tree parm_decl;
     172                 :             : 
     173                 :     3889256 :   m2assert_AssertLocation (location);
     174                 :     3889256 :   ASSERT_BOOL (isreference);
     175                 :     3889256 :   type = m2tree_skip_type_decl (type);
     176                 :     3889256 :   layout_type (type);
     177                 :     3889256 :   if (isreference)
     178                 :      106633 :     type = build_reference_type (type);
     179                 :             : 
     180                 :     3889256 :   if (name == NULL)
     181                 :       69912 :     parm_decl = build_decl (location, PARM_DECL, NULL, type);
     182                 :             :   else
     183                 :     3819344 :     parm_decl = build_decl (location, PARM_DECL, get_identifier (name), type);
     184                 :     3889256 :   DECL_ARG_TYPE (parm_decl) = type;
     185                 :     3889256 :   if (isreference)
     186                 :      106633 :     TREE_READONLY (parm_decl) = TRUE;
     187                 :             : 
     188                 :     3889256 :   param_list = chainon (parm_decl, param_list);
     189                 :     3889256 :   layout_type (type);
     190                 :     3889256 :   param_type_list = tree_cons (NULL_TREE, type, param_type_list);
     191                 :     3889256 :   return parm_decl;
     192                 :             : }
     193                 :             : 
     194                 :             : /* BuildStartFunctionDeclaration - initializes global variables ready
     195                 :             :    for building a function.  */
     196                 :             : 
     197                 :             : void
     198                 :     1801150 : m2decl_BuildStartFunctionDeclaration (bool uses_varargs)
     199                 :             : {
     200                 :     1801150 :   if (uses_varargs)
     201                 :       13816 :     param_type_list = NULL_TREE;
     202                 :             :   else
     203                 :     1787334 :     param_type_list = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
     204                 :     1801150 :   param_list = NULL_TREE; /* Ready for when we define a function.  */
     205                 :     1801150 : }
     206                 :             : 
     207                 :             : /* BuildEndFunctionDeclaration - build a function which will return a
     208                 :             :    value of returntype.  The arguments have been created by
     209                 :             :    BuildParameterDeclaration.  */
     210                 :             : 
     211                 :             : tree
     212                 :     1801150 : m2decl_BuildEndFunctionDeclaration (location_t location_begin,
     213                 :             :                                     location_t location_end, const char *name,
     214                 :             :                                     tree returntype, bool isexternal,
     215                 :             :                                     bool isnested, bool ispublic, bool isnoreturn)
     216                 :             : {
     217                 :     1801150 :   tree fntype;
     218                 :     1801150 :   tree fndecl;
     219                 :             : 
     220                 :     1801150 :   m2assert_AssertLocation (location_begin);
     221                 :     1801150 :   m2assert_AssertLocation (location_end);
     222                 :     1801150 :   ASSERT_BOOL (isexternal);
     223                 :     1801150 :   ASSERT_BOOL (isnested);
     224                 :     1801150 :   ASSERT_BOOL (ispublic);
     225                 :     1801150 :   returntype = m2tree_skip_type_decl (returntype);
     226                 :             :   /* The function type depends on the return type and type of args,
     227                 :             :      both of which we have created in BuildParameterDeclaration */
     228                 :     1801150 :   if (returntype == NULL_TREE)
     229                 :     1066037 :     returntype = void_type_node;
     230                 :      735113 :   else if (TREE_CODE (returntype) == FUNCTION_TYPE)
     231                 :           0 :     returntype = ptr_type_node;
     232                 :             : 
     233                 :     1801150 :   fntype = build_function_type (returntype, param_type_list);
     234                 :     1801150 :   fndecl = build_decl (location_begin, FUNCTION_DECL, get_identifier (name),
     235                 :             :                        fntype);
     236                 :             : 
     237                 :     1801150 :   if (isexternal)
     238                 :     1689615 :     ASSERT_CONDITION (ispublic);
     239                 :             : 
     240                 :     1801150 :   DECL_EXTERNAL (fndecl) = isexternal;
     241                 :     1801150 :   TREE_PUBLIC (fndecl) = ispublic;
     242                 :     1801150 :   TREE_STATIC (fndecl) = (!isexternal);
     243                 :     1801150 :   DECL_ARGUMENTS (fndecl) = param_list;
     244                 :     3602300 :   DECL_RESULT (fndecl)
     245                 :     1801150 :       = build_decl (location_end, RESULT_DECL, NULL_TREE, returntype);
     246                 :     1801150 :   DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
     247                 :     1801150 :   TREE_TYPE (fndecl) = fntype;
     248                 :     1801150 :   TREE_THIS_VOLATILE (fndecl) = isnoreturn;
     249                 :             : 
     250                 :     1801150 :   DECL_SOURCE_LOCATION (fndecl) = location_begin;
     251                 :             : 
     252                 :             :   /* Prevent the optimizer from removing it if it is public.  */
     253                 :     1801150 :   if (TREE_PUBLIC (fndecl))
     254                 :     1701761 :     gm2_mark_addressable (fndecl);
     255                 :             : 
     256                 :     1801150 :   m2block_pushDecl (fndecl);
     257                 :             : 
     258                 :     1801150 :   rest_of_decl_compilation (fndecl, 1, 0);
     259                 :     1801150 :   param_list
     260                 :     1801150 :       = NULL_TREE; /* Ready for the next time we call/define a function.  */
     261                 :     1801150 :   return fndecl;
     262                 :             : }
     263                 :             : 
     264                 :             : /* BuildModuleCtor creates the per module constructor used as part of
     265                 :             :    the dynamic linking scaffold.  */
     266                 :             : 
     267                 :             : void
     268                 :       11601 : m2decl_BuildModuleCtor (tree module_ctor)
     269                 :             : {
     270                 :       11601 :   decl_init_priority_insert (module_ctor, DEFAULT_INIT_PRIORITY);
     271                 :       11601 : }
     272                 :             : 
     273                 :             : /* DeclareModuleCtor configures the function to be used as a ctor.  */
     274                 :             : 
     275                 :             : tree
     276                 :       11601 : m2decl_DeclareModuleCtor (tree decl)
     277                 :             : {
     278                 :             :   /* Declare module_ctor ().  */
     279                 :       11601 :   TREE_PUBLIC (decl) = 1;
     280                 :       11601 :   DECL_ARTIFICIAL (decl) = 1;
     281                 :       11601 :   DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
     282                 :       11601 :   DECL_VISIBILITY_SPECIFIED (decl) = 1;
     283                 :       11601 :   DECL_STATIC_CONSTRUCTOR (decl) = 1;
     284                 :       11601 :   return decl;
     285                 :             : }
     286                 :             : 
     287                 :             : /* ConstantStringExceedsZType return TRUE if str cannot be represented in the ZTYPE.  */
     288                 :             : 
     289                 :             : bool
     290                 :      249758 : m2decl_ConstantStringExceedsZType (location_t location,
     291                 :             :                                    const char *str, unsigned int base,
     292                 :             :                                    bool issueError)
     293                 :             : {
     294                 :      249758 :   widest_int wval;
     295                 :      249758 :   return m2expr_StrToWideInt (location, str, base, wval, issueError);
     296                 :             : }
     297                 :             : 
     298                 :             : /* BuildConstLiteralNumber - returns a GCC TREE built from the
     299                 :             :    string, str.  It assumes that, str, represents a legal number in
     300                 :             :    Modula-2.  It always returns a positive value.  */
     301                 :             : 
     302                 :             : tree
     303                 :      458036 : m2decl_BuildConstLiteralNumber (location_t location, const char *str,
     304                 :             :                                 unsigned int base, bool issueError)
     305                 :             : {
     306                 :      458036 :   widest_int wval;
     307                 :      458036 :   tree value;
     308                 :      458036 :   bool overflow = m2expr_StrToWideInt (location, str, base, wval, issueError);
     309                 :      458036 :   value = wide_int_to_tree (m2type_GetM2ZType (), wval);
     310                 :             : 
     311                 :      458036 :   if (issueError && (overflow || m2expr_TreeOverflow (value)))
     312                 :           0 :     error_at (location, "constant %qs is too large", str);
     313                 :             : 
     314                 :      458036 :   return m2block_RememberConstant (value);
     315                 :             : }
     316                 :             : 
     317                 :             : /* BuildCStringConstant - creates a string constant given a, string,
     318                 :             :    and, length.  */
     319                 :             : 
     320                 :             : tree
     321                 :       22384 : m2decl_BuildCStringConstant (const char *string, int length)
     322                 :             : {
     323                 :       22384 :   tree elem, index, type;
     324                 :             : 
     325                 :             :   /* +1 ensures that we always nul terminate our strings.  */
     326                 :       22384 :   elem = build_type_variant (char_type_node, 1, 0);
     327                 :       22384 :   index = build_index_type (build_int_cst (integer_type_node, length + 1));
     328                 :       22384 :   type = build_array_type (elem, index);
     329                 :       22384 :   return m2decl_BuildStringConstantType (length + 1, string, type);
     330                 :             : }
     331                 :             : 
     332                 :             : /* BuildStringConstant - creates a string constant given a, string,
     333                 :             :    and, length.  */
     334                 :             : 
     335                 :             : tree
     336                 :      261860 : m2decl_BuildStringConstant (const char *string, int length)
     337                 :             : {
     338                 :      261860 :   tree elem, index, type;
     339                 :             : 
     340                 :      261860 :   elem = build_type_variant (char_type_node, 1, 0);
     341                 :      261860 :   index = build_index_type (build_int_cst (integer_type_node, length));
     342                 :      261860 :   type = build_array_type (elem, index);
     343                 :      261860 :   return m2decl_BuildStringConstantType (length, string, type);
     344                 :             :   // maybe_wrap_with_location
     345                 :             : }
     346                 :             : 
     347                 :             : 
     348                 :             : tree
     349                 :           0 : m2decl_BuildPtrToTypeString (location_t location, const char *string, tree type)
     350                 :             : {
     351                 :           0 :   if ((string == NULL) || (strlen (string) == 0))
     352                 :           0 :     return m2convert_BuildConvert (location, type,
     353                 :             :                                    m2decl_BuildIntegerConstant (0),
     354                 :           0 :                                    FALSE);
     355                 :           0 :   return build_string_literal (strlen (string), string);
     356                 :             : }
     357                 :             : 
     358                 :             : 
     359                 :             : /* BuildIntegerConstant - return a tree containing the integer value.  */
     360                 :             : 
     361                 :             : tree
     362                 :     4766436 : m2decl_BuildIntegerConstant (int value)
     363                 :             : {
     364                 :     4766436 :   switch (value)
     365                 :             :     {
     366                 :             : 
     367                 :     1247332 :     case 0:
     368                 :     1247332 :       return integer_zero_node;
     369                 :      420611 :     case 1:
     370                 :      420611 :       return integer_one_node;
     371                 :             : 
     372                 :     3098493 :     default:
     373                 :     3098493 :       return m2block_RememberConstant (
     374                 :             :           build_int_cst (integer_type_node, value));
     375                 :             :     }
     376                 :             : }
     377                 :             : 
     378                 :             : /* BuildStringConstantType - builds a string constant with a type.  */
     379                 :             : 
     380                 :             : tree
     381                 :      287412 : m2decl_BuildStringConstantType (int length, const char *string, tree type)
     382                 :             : {
     383                 :      287412 :   tree id = build_string (length, string);
     384                 :             : 
     385                 :      287412 :   TREE_TYPE (id) = type;
     386                 :      287412 :   TREE_CONSTANT (id) = TRUE;
     387                 :      287412 :   TREE_READONLY (id) = TRUE;
     388                 :      287412 :   TREE_STATIC (id) = TRUE;
     389                 :             : 
     390                 :      287412 :   return m2block_RememberConstant (id);
     391                 :             : }
     392                 :             : 
     393                 :             : /* GetBitsPerWord - returns the number of bits in a WORD.  */
     394                 :             : 
     395                 :             : int
     396                 :           0 : m2decl_GetBitsPerWord (void)
     397                 :             : {
     398                 :           0 :   return BITS_PER_WORD;
     399                 :             : }
     400                 :             : 
     401                 :             : /* GetBitsPerInt - returns the number of bits in a INTEGER.  */
     402                 :             : 
     403                 :             : int
     404                 :       23304 : m2decl_GetBitsPerInt (void)
     405                 :             : {
     406                 :       23304 :   return INT_TYPE_SIZE;
     407                 :             : }
     408                 :             : 
     409                 :             : /* GetBitsPerBitset - returns the number of bits in a BITSET.  */
     410                 :             : 
     411                 :             : int
     412                 :       36017 : m2decl_GetBitsPerBitset (void)
     413                 :             : {
     414                 :       36017 :   return SET_WORD_SIZE;
     415                 :             : }
     416                 :             : 
     417                 :             : /* GetBitsPerUnit - returns the number of bits in a UNIT.  */
     418                 :             : 
     419                 :             : int
     420                 :           0 : m2decl_GetBitsPerUnit (void)
     421                 :             : {
     422                 :           0 :   return BITS_PER_UNIT;
     423                 :             : }
     424                 :             : 
     425                 :             : /* m2decl_GetDeclContext - returns the DECL_CONTEXT of tree, t.  */
     426                 :             : 
     427                 :             : tree
     428                 :           0 : m2decl_GetDeclContext (tree t)
     429                 :             : {
     430                 :           0 :   return DECL_CONTEXT (t);
     431                 :             : }
     432                 :             : 
     433                 :             : #include "gt-m2-m2decl.h"
        

Generated by: LCOV version 2.0-1

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.